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

1.200   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.199 2012/08/27 05:38:00 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.61      noro        8: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94      noro        9: int nd_dcomp;
                     10: NM _nm_free_list;
                     11: ND _nd_free_list;
                     12: ND_pairs _ndp_free_list;
1.150     noro       13: NODE nd_hcf;
1.32      noro       14:
1.146     noro       15: static NODE nd_subst;
                     16: static VL nd_vc;
1.121     noro       17: static int nd_ntrans;
1.117     noro       18: static int nd_nalg;
1.103     noro       19: #if 0
1.74      noro       20: static int ndv_alloc;
1.103     noro       21: #endif
1.87      noro       22: #if 1
1.69      noro       23: static int nd_f4_nsp=0x7fffffff;
1.87      noro       24: #else
                     25: static int nd_f4_nsp=50;
                     26: #endif
1.42      noro       27: static double nd_scale=2;
1.61      noro       28: static UINT **nd_bound;
1.42      noro       29: static struct order_spec *nd_ord;
                     30: static EPOS nd_epos;
1.43      noro       31: static BlockMask nd_blockmask;
1.42      noro       32: static int nd_nvar;
                     33: static int nd_isrlex;
                     34: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro       35: static UINT nd_mask[32];
                     36: static UINT nd_mask0,nd_mask1;
1.42      noro       37:
1.20      noro       38: static NDV *nd_ps;
1.53      noro       39: static NDV *nd_ps_trace;
1.42      noro       40: static RHist *nd_psh;
                     41: static int nd_psn,nd_pslen;
                     42: static RHist *nd_red;
1.96      noro       43: static int *nd_work_vector;
                     44: static int **nd_matrix;
                     45: static int nd_matrix_len;
1.97      noro       46: static struct weight_or_block *nd_worb;
                     47: static int nd_worb_len;
1.42      noro       48: static int nd_found,nd_create,nd_notfirst;
                     49: static int nmv_adv;
1.77      noro       50: static int nd_demand;
1.174     noro       51: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
1.167     noro       52: static NODE nd_tracelist;
                     53: static NODE nd_alltracelist;
1.195     noro       54: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect;
1.187     noro       55: static int *nd_gbblock;
1.1       noro       56:
1.119     noro       57: NumberField get_numberfield();
1.114     noro       58: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     59: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
1.152     ohara      60: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
1.118     noro       61: int nd_monic(int m,ND *p);
1.129     noro       62: NDV plain_vect_to_ndv_q(Q *mat,int col,UINT *s0vect);
1.157     noro       63: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     64: NDV pltondv(VL vl,VL dvl,LIST p);
                     65: void pltozpl(LIST l,Q *cont,LIST *pp);
1.159     noro       66: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
1.167     noro       67: void nmtodp(int mod,NM m,DP *r);
                     68: NODE reverse_node(NODE n);
                     69: P ndc_div(int mod,union oNDC a,union oNDC b);
                     70: P ndctop(int mod,union oNDC c);
                     71: void finalize_tracelist(int i,P cont);
                     72: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.172     noro       73: void parse_nd_option(NODE opt);
1.198     noro       74: void dltondl(int n,DL dl,UINT *r);
                     75: DP ndvtodp(int mod,NDV p);
1.114     noro       76:
1.149     noro       77: extern int Denominator,DP_Multiple;
                     78:
1.1       noro       79: void nd_free_private_storage()
                     80: {
1.157     noro       81:     _nm_free_list = 0;
                     82:     _ndp_free_list = 0;
1.71      noro       83: #if 0
1.157     noro       84:     GC_gcollect();
1.71      noro       85: #endif
1.1       noro       86: }
                     87:
                     88: void _NM_alloc()
                     89: {
1.157     noro       90:     NM p;
                     91:     int i;
1.1       noro       92:
1.157     noro       93:     for ( i = 0; i < 1024; i++ ) {
1.200   ! noro       94:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro       95:         p->next = _nm_free_list; _nm_free_list = p;
                     96:     }
1.1       noro       97: }
                     98:
                     99: void _ND_alloc()
                    100: {
1.157     noro      101:     ND p;
                    102:     int i;
1.1       noro      103:
1.157     noro      104:     for ( i = 0; i < 1024; i++ ) {
1.200   ! noro      105:         p = (ND)MALLOC(sizeof(struct oND));
1.157     noro      106:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    107:     }
1.1       noro      108: }
                    109:
                    110: void _NDP_alloc()
                    111: {
1.157     noro      112:     ND_pairs p;
                    113:     int i;
1.1       noro      114:
1.157     noro      115:     for ( i = 0; i < 1024; i++ ) {
1.200   ! noro      116:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
1.157     noro      117:             +(nd_wpd-1)*sizeof(UINT));
                    118:         p->next = _ndp_free_list; _ndp_free_list = p;
                    119:     }
1.1       noro      120: }
                    121:
1.30      noro      122: INLINE int nd_length(ND p)
1.1       noro      123: {
1.157     noro      124:     NM m;
                    125:     int i;
1.1       noro      126:
1.157     noro      127:     if ( !p )
                    128:         return 0;
                    129:     else {
                    130:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    131:         return i;
                    132:     }
1.1       noro      133: }
                    134:
1.61      noro      135: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      136: {
1.157     noro      137:     UINT u1,u2;
                    138:     int i,j;
1.1       noro      139:
1.157     noro      140:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    141:
                    142:     if ( TD(d1) < TD(d2) ) return 0;
1.65      noro      143: #if USE_UNROLL
1.157     noro      144:     switch ( nd_bpe ) {
                    145:         case 3:
                    146:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    147:                 u1 = d1[i]; u2 = d2[i];
                    148:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    149:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    150:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    151:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    152:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    153:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    154:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    155:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    156:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    157:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    158:             }
                    159:             return 1;
                    160:             break;
                    161:         case 4:
                    162:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    163:                 u1 = d1[i]; u2 = d2[i];
                    164:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    165:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    166:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    167:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    168:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    169:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    170:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    171:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    172:             }
                    173:             return 1;
                    174:             break;
                    175:         case 6:
                    176:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    177:                 u1 = d1[i]; u2 = d2[i];
                    178:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    179:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    180:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    181:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    182:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    183:             }
                    184:             return 1;
                    185:             break;
                    186:         case 8:
                    187:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    188:                 u1 = d1[i]; u2 = d2[i];
                    189:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    190:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    191:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    192:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    193:             }
                    194:             return 1;
                    195:             break;
                    196:         case 16:
                    197:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    198:                 u1 = d1[i]; u2 = d2[i];
                    199:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    200:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    201:             }
                    202:             return 1;
                    203:             break;
                    204:         case 32:
                    205:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    206:                 if ( d1[i] < d2[i] ) return 0;
                    207:             return 1;
                    208:             break;
                    209:         default:
                    210:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    211:                 u1 = d1[i]; u2 = d2[i];
                    212:                 for ( j = 0; j < nd_epw; j++ )
                    213:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    214:             }
                    215:             return 1;
                    216:     }
1.65      noro      217: #else
1.157     noro      218:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    219:         u1 = d1[i]; u2 = d2[i];
                    220:         for ( j = 0; j < nd_epw; j++ )
                    221:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    222:     }
                    223:     return 1;
1.65      noro      224: #endif
1.1       noro      225: }
                    226:
1.61      noro      227: /*
                    228:  * If the current order is a block order,
                    229:  * then the last block is length 1 and contains
                    230:  * the homo variable. Otherwise, the original
                    231:  * order is either 0 or 2.
                    232:  */
                    233:
1.164     noro      234: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
1.23      noro      235: {
1.157     noro      236:     int w,i,e,n,omask0;
1.61      noro      237:
1.157     noro      238:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    239:     n = nd_nvar-1;
                    240:     ndl_zero(r);
                    241:     for ( i = 0; i < n; i++ ) {
                    242:         e = GET_EXP_OLD(d,i);
                    243:         PUT_EXP(r,i,e);
                    244:     }
                    245:     w = TD(d);
                    246:     PUT_EXP(r,nd_nvar-1,weight-w);
1.164     noro      247:     if ( nd_module ) MPOS(r) = d[ompos];
1.157     noro      248:     TD(r) = weight;
                    249:     if ( nd_blockmask ) ndl_weight_mask(r);
1.61      noro      250: }
                    251:
                    252: void ndl_dehomogenize(UINT *d)
                    253: {
1.157     noro      254:     UINT mask;
                    255:     UINT h;
                    256:     int i,bits;
                    257:
                    258:     if ( nd_blockmask ) {
                    259:         h = GET_EXP(d,nd_nvar-1);
                    260:         XOR_EXP(d,nd_nvar-1,h);
                    261:         TD(d) -= h;
                    262:         ndl_weight_mask(d);
                    263:     } else {
                    264:         if ( nd_isrlex ) {
                    265:             if ( nd_bpe == 32 ) {
                    266:                 h = d[nd_exporigin];
                    267:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    268:                     d[i-1] = d[i];
                    269:                 d[i-1] = 0;
                    270:                 TD(d) -= h;
                    271:             } else {
                    272:                 bits = nd_epw*nd_bpe;
                    273:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    274:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    275:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    276:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    277:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    278:                 TD(d) -= h;
                    279:             }
                    280:         } else {
                    281:             h = GET_EXP(d,nd_nvar-1);
                    282:             XOR_EXP(d,nd_nvar-1,h);
                    283:             TD(d) -= h;
                    284:         }
                    285:     }
1.23      noro      286: }
                    287:
1.61      noro      288: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      289: {
1.157     noro      290:     UINT t1,t2,u,u1,u2;
                    291:     int i,j,l;
                    292:
                    293:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    294:         error("ndl_lcm : inconsistent monomials");
                    295: #if USE_UNROLL
                    296:     switch ( nd_bpe ) {
                    297:         case 3:
                    298:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    299:                 u1 = d1[i]; u2 = d2[i];
                    300:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    301:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    302:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    303:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    304:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    305:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    306:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    307:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    308:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    309:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    310:                 d[i] = u;
                    311:             }
                    312:             break;
                    313:         case 4:
                    314:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    315:                 u1 = d1[i]; u2 = d2[i];
                    316:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    317:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    318:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    319:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    320:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    321:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    322:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    323:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    324:                 d[i] = u;
                    325:             }
                    326:             break;
                    327:         case 6:
                    328:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    329:                 u1 = d1[i]; u2 = d2[i];
                    330:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    331:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    332:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    333:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    334:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    335:                 d[i] = u;
                    336:             }
                    337:             break;
                    338:         case 8:
                    339:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    340:                 u1 = d1[i]; u2 = d2[i];
                    341:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    342:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    343:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    344:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    345:                 d[i] = u;
                    346:             }
                    347:             break;
                    348:         case 16:
                    349:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    350:                 u1 = d1[i]; u2 = d2[i];
                    351:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    352:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    353:                 d[i] = u;
                    354:             }
                    355:             break;
                    356:         case 32:
                    357:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    358:                 u1 = d1[i]; u2 = d2[i];
                    359:                 d[i] = u1>u2?u1:u2;
                    360:             }
                    361:             break;
                    362:         default:
                    363:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    364:                 u1 = d1[i]; u2 = d2[i];
                    365:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    366:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    367:                 }
                    368:                 d[i] = u;
                    369:             }
                    370:             break;
                    371:     }
                    372: #else
                    373:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    374:         u1 = d1[i]; u2 = d2[i];
                    375:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    376:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    377:         }
                    378:         d[i] = u;
                    379:     }
                    380: #endif
1.163     noro      381:     if ( nd_module ) MPOS(d) = MPOS(d1);
1.157     noro      382:     TD(d) = ndl_weight(d);
                    383:     if ( nd_blockmask ) ndl_weight_mask(d);
                    384: }
                    385:
1.159     noro      386: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
1.157     noro      387: {
                    388:     UINT t1,t2,u,u1,u2;
                    389:     int i,j,l;
1.1       noro      390:
1.157     noro      391:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    392:         u1 = d1[i]; u2 = d2[i];
                    393:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    394:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    395:         }
                    396:         d[i] = u;
                    397:     }
1.57      noro      398: }
                    399:
1.61      noro      400: int ndl_weight(UINT *d)
1.1       noro      401: {
1.157     noro      402:     UINT t,u;
                    403:     int i,j;
1.1       noro      404:
1.157     noro      405:     if ( current_dl_weight_vector )
                    406:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    407:             u = GET_EXP(d,i);
                    408:             t += MUL_WEIGHT(u,i);
                    409:         }
                    410:     else
                    411:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    412:             u = d[i];
                    413:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    414:                 t += (u&nd_mask0);
                    415:         }
1.167     noro      416:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    417:         t += current_module_weight_vector[MPOS(d)];
1.157     noro      418:     return t;
1.1       noro      419: }
                    420:
1.61      noro      421: void ndl_weight_mask(UINT *d)
1.43      noro      422: {
1.157     noro      423:     UINT t,u;
                    424:     UINT *mask;
                    425:     int i,j,k,l;
                    426:
                    427:     l = nd_blockmask->n;
                    428:     for ( k = 0; k < l; k++ ) {
                    429:         mask = nd_blockmask->mask[k];
                    430:         if ( current_dl_weight_vector )
                    431:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    432:                 u = GET_EXP_MASK(d,i,mask);
                    433:                 t += MUL_WEIGHT(u,i);
                    434:             }
                    435:         else
                    436:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    437:                 u = d[i]&mask[i];
                    438:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    439:                     t += (u&nd_mask0);
                    440:             }
                    441:         d[k+1] = t;
                    442:     }
1.43      noro      443: }
                    444:
1.61      noro      445: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      446: {
1.157     noro      447:     int i;
1.1       noro      448:
1.157     noro      449:     d1 += nd_exporigin;
                    450:     d2 += nd_exporigin;
                    451:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    452:         if ( *d1 > *d2 )
                    453:             return nd_isrlex ? -1 : 1;
                    454:         else if ( *d1 < *d2 )
                    455:             return nd_isrlex ? 1 : -1;
                    456:     return 0;
1.1       noro      457: }
                    458:
1.61      noro      459: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      460: {
1.157     noro      461:     int i,l,j,ord_o,ord_l;
                    462:     struct order_pair *op;
                    463:     UINT t1,t2,m;
                    464:     UINT *mask;
                    465:
                    466:     l = nd_blockmask->n;
                    467:     op = nd_blockmask->order_pair;
                    468:     for ( j = 0; j < l; j++ ) {
                    469:         mask = nd_blockmask->mask[j];
                    470:         ord_o = op[j].order;
                    471:         if ( ord_o < 2 )
                    472:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    473:             else if ( t1 < t2 ) return -1;
                    474:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    475:             m = mask[i];
                    476:             t1 = d1[i]&m;
                    477:             t2 = d2[i]&m;
                    478:             if ( t1 > t2 )
                    479:                 return !ord_o ? -1 : 1;
                    480:             else if ( t1 < t2 )
                    481:                 return !ord_o ? 1 : -1;
                    482:         }
                    483:     }
                    484:     return 0;
1.43      noro      485: }
                    486:
1.96      noro      487: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    488: {
1.157     noro      489:     int i,j,s;
                    490:     int *v;
1.96      noro      491:
1.157     noro      492:     for ( j = 0; j < nd_nvar; j++ )
                    493:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    494:     for ( i = 0; i < nd_matrix_len; i++ ) {
                    495:         v = nd_matrix[i];
                    496:         for ( j = 0, s = 0; j < nd_nvar; j++ )
                    497:             s += v[j]*nd_work_vector[j];
                    498:         if ( s > 0 ) return 1;
                    499:         else if ( s < 0 ) return -1;
                    500:     }
                    501:     return 0;
1.96      noro      502: }
                    503:
1.97      noro      504: int ndl_composite_compare(UINT *d1,UINT *d2)
                    505: {
1.157     noro      506:     int i,j,s,start,end,len,o;
                    507:     int *v;
                    508:     struct sparse_weight *sw;
                    509:
                    510:     for ( j = 0; j < nd_nvar; j++ )
                    511:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    512:     for ( i = 0; i < nd_worb_len; i++ ) {
                    513:         len = nd_worb[i].length;
                    514:         switch ( nd_worb[i].type ) {
                    515:             case IS_DENSE_WEIGHT:
                    516:                 v = nd_worb[i].body.dense_weight;
                    517:                 for ( j = 0, s = 0; j < len; j++ )
                    518:                     s += v[j]*nd_work_vector[j];
                    519:                 if ( s > 0 ) return 1;
                    520:                 else if ( s < 0 ) return -1;
                    521:                 break;
                    522:             case IS_SPARSE_WEIGHT:
                    523:                 sw = nd_worb[i].body.sparse_weight;
                    524:                 for ( j = 0, s = 0; j < len; j++ )
                    525:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    526:                 if ( s > 0 ) return 1;
                    527:                 else if ( s < 0 ) return -1;
                    528:                 break;
                    529:             case IS_BLOCK:
                    530:                 o = nd_worb[i].body.block.order;
                    531:                 start = nd_worb[i].body.block.start;
                    532:                 switch ( o ) {
                    533:                     case 0:
                    534:                         end = start+len;
                    535:                         for ( j = start, s = 0; j < end; j++ )
                    536:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    537:                         if ( s > 0 ) return 1;
                    538:                         else if ( s < 0 ) return -1;
                    539:                         for ( j = end-1; j >= start; j-- )
                    540:                             if ( nd_work_vector[j] < 0 ) return 1;
                    541:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    542:                         break;
                    543:                     case 1:
                    544:                         end = start+len;
                    545:                         for ( j = start, s = 0; j < end; j++ )
                    546:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    547:                         if ( s > 0 ) return 1;
                    548:                         else if ( s < 0 ) return -1;
                    549:                         for ( j = start; j < end; j++ )
                    550:                             if ( nd_work_vector[j] > 0 ) return 1;
                    551:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    552:                         break;
                    553:                     case 2:
                    554:                         for ( j = start; j < end; j++ )
                    555:                             if ( nd_work_vector[j] > 0 ) return 1;
                    556:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    557:                         break;
                    558:                 }
                    559:                 break;
                    560:         }
                    561:     }
                    562:     return 0;
1.97      noro      563: }
                    564:
1.58      noro      565: /* TDH -> WW -> TD-> RL */
                    566:
1.61      noro      567: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      568: {
1.157     noro      569:     int i,m,e1,e2;
1.58      noro      570:
1.157     noro      571:     if ( TD(d1) > TD(d2) ) return 1;
                    572:     else if ( TD(d1) < TD(d2) ) return -1;
                    573:     m = nd_nvar>>1;
                    574:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    575:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    576:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    577:     }
                    578:     if ( e1 > e2 ) return 1;
                    579:     else if ( e1 < e2 ) return -1;
                    580:     return ndl_lex_compare(d1,d2);
                    581: }
                    582:
                    583: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
                    584: {
1.164     noro      585:     int i,c;
1.157     noro      586:
1.160     noro      587:     if ( nd_ispot ) {
1.174     noro      588:                if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    589:             if ( TD(d1) > TD(d2) ) return 1;
                    590:             else if ( TD(d1) < TD(d2) ) return -1;
                    591:             if ( c = ndl_lex_compare(d1,d2) ) return c;
                    592:             if ( MPOS(d1) < MPOS(d2) ) return 1;
                    593:             else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    594:             return 0;
                    595:                }
1.157     noro      596:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    597:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    598:     }
                    599:     if ( TD(d1) > TD(d2) ) return 1;
                    600:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      601:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      602:     if ( !nd_ispot ) {
1.157     noro      603:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    604:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    605:     }
                    606:     return 0;
                    607: }
                    608:
                    609: int ndl_module_glex_compare(UINT *d1,UINT *d2)
                    610: {
1.164     noro      611:     int i,c;
1.157     noro      612:
1.160     noro      613:     if ( nd_ispot ) {
1.157     noro      614:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    615:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    616:     }
                    617:     if ( TD(d1) > TD(d2) ) return 1;
                    618:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      619:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      620:     if ( !nd_ispot ) {
1.157     noro      621:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    622:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    623:     }
                    624:     return 0;
                    625: }
                    626:
                    627: int ndl_module_lex_compare(UINT *d1,UINT *d2)
                    628: {
1.164     noro      629:     int i,c;
1.157     noro      630:
1.160     noro      631:     if ( nd_ispot ) {
1.157     noro      632:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    633:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    634:     }
1.167     noro      635:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      636:     if ( !nd_ispot ) {
1.157     noro      637:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    638:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    639:     }
                    640:     return 0;
                    641: }
                    642:
                    643: int ndl_module_block_compare(UINT *d1,UINT *d2)
                    644: {
                    645:     int i,c;
                    646:
1.160     noro      647:     if ( nd_ispot ) {
1.157     noro      648:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    649:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    650:     }
                    651:     if ( c = ndl_block_compare(d1,d2) ) return c;
1.160     noro      652:     if ( !nd_ispot ) {
1.157     noro      653:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    654:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    655:     }
                    656:     return 0;
                    657: }
                    658:
                    659: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
                    660: {
                    661:     int i,c;
                    662:
1.160     noro      663:     if ( nd_ispot ) {
1.157     noro      664:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    665:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    666:     }
                    667:     if ( c = ndl_matrix_compare(d1,d2) ) return c;
1.160     noro      668:     if ( !nd_ispot ) {
1.157     noro      669:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    670:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    671:     }
                    672:     return 0;
                    673: }
                    674:
                    675: int ndl_module_composite_compare(UINT *d1,UINT *d2)
                    676: {
                    677:     int i,c;
                    678:
1.160     noro      679:     if ( nd_ispot ) {
1.157     noro      680:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    681:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    682:     }
                    683:     if ( c = ndl_composite_compare(d1,d2) ) return c;
1.160     noro      684:     if ( !nd_ispot ) {
1.157     noro      685:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    686:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    687:     }
                    688:     return 0;
1.58      noro      689: }
                    690:
1.61      noro      691: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      692: {
1.157     noro      693:     int i;
1.1       noro      694:
1.157     noro      695:     switch ( nd_wpd ) {
                    696:         case 2:
                    697:             if ( TD(d2) != TD(d1) ) return 0;
                    698:             if ( d2[1] != d1[1] ) return 0;
                    699:             return 1;
                    700:             break;
                    701:         case 3:
                    702:             if ( TD(d2) != TD(d1) ) return 0;
                    703:             if ( d2[1] != d1[1] ) return 0;
                    704:             if ( d2[2] != d1[2] ) return 0;
                    705:             return 1;
                    706:             break;
                    707:         default:
                    708:             for ( i = 0; i < nd_wpd; i++ )
                    709:                 if ( *d1++ != *d2++ ) return 0;
                    710:             return 1;
                    711:             break;
                    712:     }
1.1       noro      713: }
                    714:
1.61      noro      715: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      716: {
1.157     noro      717:     int i;
1.6       noro      718:
1.157     noro      719:     switch ( nd_wpd ) {
                    720:         case 2:
                    721:             TD(d2) = TD(d1);
                    722:             d2[1] = d1[1];
                    723:             break;
                    724:         case 3:
                    725:             TD(d2) = TD(d1);
                    726:             d2[1] = d1[1];
                    727:             d2[2] = d1[2];
                    728:             break;
                    729:         default:
                    730:             for ( i = 0; i < nd_wpd; i++ )
                    731:                 d2[i] = d1[i];
                    732:             break;
                    733:     }
1.6       noro      734: }
                    735:
1.61      noro      736: INLINE void ndl_zero(UINT *d)
                    737: {
1.157     noro      738:     int i;
                    739:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
1.61      noro      740: }
                    741:
                    742: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      743: {
1.157     noro      744:     int i;
1.1       noro      745:
1.162     noro      746:     if ( nd_module ) {
                    747:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
1.167     noro      748:         error("ndl_add : invalid operation");
1.162     noro      749:     }
1.43      noro      750: #if 1
1.157     noro      751:     switch ( nd_wpd ) {
                    752:         case 2:
                    753:             TD(d) = TD(d1)+TD(d2);
                    754:             d[1] = d1[1]+d2[1];
                    755:             break;
                    756:         case 3:
                    757:             TD(d) = TD(d1)+TD(d2);
                    758:             d[1] = d1[1]+d2[1];
                    759:             d[2] = d1[2]+d2[2];
                    760:             break;
                    761:         default:
                    762:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    763:             break;
                    764:     }
1.43      noro      765: #else
1.157     noro      766:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.43      noro      767: #endif
1.6       noro      768: }
                    769:
1.55      noro      770: /* d1 += d2 */
1.61      noro      771: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      772: {
1.157     noro      773:     int i;
1.55      noro      774:
1.162     noro      775:     if ( nd_module ) {
                    776:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    777:             error("ndl_addto : invalid operation");
                    778:     }
1.55      noro      779: #if 1
1.157     noro      780:     switch ( nd_wpd ) {
                    781:         case 2:
                    782:             TD(d1) += TD(d2);
                    783:             d1[1] += d2[1];
                    784:             break;
                    785:         case 3:
                    786:             TD(d1) += TD(d2);
                    787:             d1[1] += d2[1];
                    788:             d1[2] += d2[2];
                    789:             break;
                    790:         default:
                    791:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    792:             break;
                    793:     }
1.55      noro      794: #else
1.157     noro      795:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
1.55      noro      796: #endif
                    797: }
                    798:
1.61      noro      799: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      800: {
1.157     noro      801:     int i;
1.6       noro      802:
1.157     noro      803:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      804: }
                    805:
1.61      noro      806: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      807: {
1.157     noro      808:     UINT t1,t2,u,u1,u2;
                    809:     int i,j;
1.1       noro      810:
1.157     noro      811:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1.65      noro      812: #if USE_UNROLL
1.157     noro      813:     switch ( nd_bpe ) {
                    814:         case 3:
                    815:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    816:                 u1 = d1[i]; u2 = d2[i];
                    817:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    818:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    819:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    820:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    821:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    822:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    823:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    824:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    825:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    826:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    827:             }
                    828:             return 1;
                    829:             break;
                    830:         case 4:
                    831:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    832:                 u1 = d1[i]; u2 = d2[i];
                    833:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    834:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    835:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    836:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    837:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    838:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    839:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    840:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                    841:             }
                    842:             return 1;
                    843:             break;
                    844:         case 6:
                    845:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    846:                 u1 = d1[i]; u2 = d2[i];
                    847:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                    848:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    849:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    850:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    851:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                    852:             }
                    853:             return 1;
                    854:             break;
                    855:         case 8:
                    856:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    857:                 u1 = d1[i]; u2 = d2[i];
                    858:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                    859:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    860:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    861:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                    862:             }
                    863:             return 1;
                    864:             break;
                    865:         case 16:
                    866:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    867:                 u1 = d1[i]; u2 = d2[i];
                    868:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                    869:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                    870:             }
                    871:             return 1;
                    872:             break;
                    873:         case 32:
                    874:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    875:                 if ( d1[i] && d2[i] ) return 0;
                    876:             return 1;
                    877:             break;
                    878:         default:
                    879:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    880:                 u1 = d1[i]; u2 = d2[i];
                    881:                 for ( j = 0; j < nd_epw; j++ ) {
                    882:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    883:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                    884:                 }
                    885:             }
                    886:             return 1;
                    887:             break;
                    888:     }
1.65      noro      889: #else
1.157     noro      890:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    891:         u1 = d1[i]; u2 = d2[i];
                    892:         for ( j = 0; j < nd_epw; j++ ) {
                    893:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    894:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                    895:         }
                    896:     }
                    897:     return 1;
1.65      noro      898: #endif
1.1       noro      899: }
                    900:
1.114     noro      901: int ndl_check_bound(UINT *d1,UINT *d2)
1.1       noro      902: {
1.157     noro      903:     UINT u2;
                    904:     int i,j,ind,k;
1.1       noro      905:
1.157     noro      906:     ind = 0;
1.65      noro      907: #if USE_UNROLL
1.157     noro      908:     switch ( nd_bpe ) {
                    909:         case 3:
                    910:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    911:                 u2 = d2[i];
                    912:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                    913:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                    914:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                    915:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                    916:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                    917:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                    918:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                    919:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                    920:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                    921:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                    922:             }
                    923:             return 0;
                    924:             break;
                    925:         case 4:
                    926:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    927:                 u2 = d2[i];
                    928:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    929:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    930:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    931:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    932:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    933:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    934:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    935:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    936:             }
                    937:             return 0;
                    938:             break;
                    939:         case 6:
                    940:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    941:                 u2 = d2[i];
                    942:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    943:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    944:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    945:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    946:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    947:             }
                    948:             return 0;
                    949:             break;
                    950:         case 8:
                    951:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    952:                 u2 = d2[i];
                    953:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    954:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    955:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    956:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    957:             }
                    958:             return 0;
                    959:             break;
                    960:         case 16:
                    961:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    962:                 u2 = d2[i];
                    963:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    964:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    965:             }
                    966:             return 0;
                    967:             break;
                    968:         case 32:
                    969:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    970:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                    971:             return 0;
                    972:             break;
                    973:         default:
                    974:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    975:                 u2 = d2[i];
                    976:                 k = (nd_epw-1)*nd_bpe;
                    977:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    978:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    979:             }
                    980:             return 0;
                    981:             break;
                    982:     }
1.65      noro      983: #else
1.157     noro      984:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    985:         u2 = d2[i];
                    986:         k = (nd_epw-1)*nd_bpe;
                    987:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    988:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    989:     }
                    990:     return 0;
1.65      noro      991: #endif
1.1       noro      992: }
                    993:
1.114     noro      994: int ndl_check_bound2(int index,UINT *d2)
                    995: {
1.157     noro      996:     return ndl_check_bound(nd_bound[index],d2);
1.114     noro      997: }
                    998:
1.61      noro      999: INLINE int ndl_hash_value(UINT *d)
1.1       noro     1000: {
1.157     noro     1001:     int i;
                   1002:     int r;
1.1       noro     1003:
1.157     noro     1004:     r = 0;
                   1005:     for ( i = 0; i < nd_wpd; i++ )
                   1006:         r = ((r<<16)+d[i])%REDTAB_LEN;
                   1007:     return r;
1.1       noro     1008: }
                   1009:
1.63      noro     1010: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1011: {
1.157     noro     1012:     RHist r;
                   1013:     int d,k,i;
1.1       noro     1014:
1.157     noro     1015:     d = ndl_hash_value(dg);
                   1016:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1017:         if ( ndl_equal(dg,DL(r)) ) {
                   1018:             if ( k > 0 ) nd_notfirst++;
                   1019:             nd_found++;
                   1020:             return r->index;
                   1021:         }
                   1022:     }
                   1023:     if ( Reverse )
                   1024:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1025:             r = nd_psh[i];
                   1026:             if ( ndl_reducible(dg,DL(r)) ) {
                   1027:                 nd_create++;
                   1028:                 nd_append_red(dg,i);
                   1029:                 return i;
                   1030:             }
                   1031:         }
                   1032:     else
                   1033:         for ( i = 0; i < nd_psn; i++ ) {
                   1034:             r = nd_psh[i];
                   1035:             if ( ndl_reducible(dg,DL(r)) ) {
                   1036:                 nd_create++;
                   1037:                 nd_append_red(dg,i);
                   1038:                 return i;
                   1039:             }
                   1040:         }
                   1041:     return -1;
1.1       noro     1042: }
                   1043:
1.63      noro     1044: ND nd_merge(ND p1,ND p2)
                   1045: {
1.157     noro     1046:     int n,c;
                   1047:     int t,can,td1,td2;
                   1048:     ND r;
                   1049:     NM m1,m2,mr0,mr,s;
                   1050:
                   1051:     if ( !p1 ) return p2;
                   1052:     else if ( !p2 ) return p1;
                   1053:     else {
                   1054:         can = 0;
                   1055:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1056:             c = DL_COMPARE(DL(m1),DL(m2));
                   1057:             switch ( c ) {
                   1058:                 case 0:
                   1059:                     s = m1; m1 = NEXT(m1);
                   1060:                     can++; NEXTNM2(mr0,mr,s);
                   1061:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1062:                     break;
                   1063:                 case 1:
                   1064:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1065:                     break;
                   1066:                 case -1:
                   1067:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1068:                     break;
                   1069:             }
                   1070:         }
                   1071:         if ( !mr0 )
                   1072:             if ( m1 ) mr0 = m1;
                   1073:             else if ( m2 ) mr0 = m2;
                   1074:             else return 0;
                   1075:         else if ( m1 ) NEXT(mr) = m1;
                   1076:         else if ( m2 ) NEXT(mr) = m2;
                   1077:         else NEXT(mr) = 0;
                   1078:         BDY(p1) = mr0;
                   1079:         SG(p1) = MAX(SG(p1),SG(p2));
                   1080:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1081:         FREEND(p2);
                   1082:         return p1;
                   1083:     }
1.63      noro     1084: }
                   1085:
1.31      noro     1086: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1087: {
1.157     noro     1088:     int n,c;
                   1089:     int t,can,td1,td2;
                   1090:     ND r;
                   1091:     NM m1,m2,mr0,mr,s;
                   1092:
                   1093:     if ( !p1 ) return p2;
                   1094:     else if ( !p2 ) return p1;
                   1095:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1096:     else if ( !mod ) return nd_add_q(p1,p2);
                   1097:     else {
                   1098:         can = 0;
                   1099:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1100:             c = DL_COMPARE(DL(m1),DL(m2));
                   1101:             switch ( c ) {
                   1102:                 case 0:
                   1103:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1104:                     if ( t < 0 ) t += mod;
                   1105:                     s = m1; m1 = NEXT(m1);
                   1106:                     if ( t ) {
                   1107:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1108:                     } else {
                   1109:                         can += 2; FREENM(s);
                   1110:                     }
                   1111:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1112:                     break;
                   1113:                 case 1:
                   1114:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1115:                     break;
                   1116:                 case -1:
                   1117:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1118:                     break;
                   1119:             }
                   1120:         }
                   1121:         if ( !mr0 )
                   1122:             if ( m1 ) mr0 = m1;
                   1123:             else if ( m2 ) mr0 = m2;
                   1124:             else return 0;
                   1125:         else if ( m1 ) NEXT(mr) = m1;
                   1126:         else if ( m2 ) NEXT(mr) = m2;
                   1127:         else NEXT(mr) = 0;
                   1128:         BDY(p1) = mr0;
                   1129:         SG(p1) = MAX(SG(p1),SG(p2));
                   1130:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1131:         FREEND(p2);
                   1132:         return p1;
                   1133:     }
1.95      noro     1134: }
                   1135:
                   1136: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1137:  * two NM seems to make gcc optimizer get confused, so the part is
                   1138:  * done in a function.
                   1139:  */
                   1140:
1.113     noro     1141: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro     1142: {
1.157     noro     1143:     NM s;
                   1144:     P t;
                   1145:     int can;
                   1146:
                   1147:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1148:     s = *m1; *m1 = NEXT(*m1);
                   1149:     if ( t ) {
                   1150:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1151:     } else {
                   1152:         can = 2; FREENM(s);
                   1153:     }
                   1154:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1155:     return can;
1.95      noro     1156: }
                   1157:
1.113     noro     1158: ND nd_add_q(ND p1,ND p2)
1.95      noro     1159: {
1.157     noro     1160:     int n,c,can;
                   1161:     ND r;
                   1162:     NM m1,m2,mr0,mr,s;
                   1163:     P t;
                   1164:
                   1165:     if ( !p1 ) return p2;
                   1166:     else if ( !p2 ) return p1;
                   1167:     else {
                   1168:         can = 0;
                   1169:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1170:             c = DL_COMPARE(DL(m1),DL(m2));
                   1171:             switch ( c ) {
                   1172:                 case 0:
1.95      noro     1173: #if defined(__x86_64__)
1.157     noro     1174:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95      noro     1175: #else
1.157     noro     1176:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1177:                     s = m1; m1 = NEXT(m1);
                   1178:                     if ( t ) {
                   1179:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1180:                     } else {
                   1181:                         can += 2; FREENM(s);
                   1182:                     }
                   1183:                     s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro     1184: #endif
1.157     noro     1185:                     break;
                   1186:                 case 1:
                   1187:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1188:                     break;
                   1189:                 case -1:
                   1190:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1191:                     break;
                   1192:             }
                   1193:         }
                   1194:         if ( !mr0 )
                   1195:             if ( m1 ) mr0 = m1;
                   1196:             else if ( m2 ) mr0 = m2;
                   1197:             else return 0;
                   1198:         else if ( m1 ) NEXT(mr) = m1;
                   1199:         else if ( m2 ) NEXT(mr) = m2;
                   1200:         else NEXT(mr) = 0;
                   1201:         BDY(p1) = mr0;
                   1202:         SG(p1) = MAX(SG(p1),SG(p2));
                   1203:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1204:         FREEND(p2);
                   1205:         return p1;
                   1206:     }
1.17      noro     1207: }
                   1208:
1.71      noro     1209: ND nd_add_sf(ND p1,ND p2)
                   1210: {
1.157     noro     1211:     int n,c,can;
                   1212:     ND r;
                   1213:     NM m1,m2,mr0,mr,s;
                   1214:     int t;
                   1215:
                   1216:     if ( !p1 ) return p2;
                   1217:     else if ( !p2 ) return p1;
                   1218:     else {
                   1219:         can = 0;
                   1220:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1221:             c = DL_COMPARE(DL(m1),DL(m2));
                   1222:             switch ( c ) {
                   1223:                 case 0:
                   1224:                     t = _addsf(CM(m1),CM(m2));
                   1225:                     s = m1; m1 = NEXT(m1);
                   1226:                     if ( t ) {
                   1227:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1228:                     } else {
                   1229:                         can += 2; FREENM(s);
                   1230:                     }
                   1231:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1232:                     break;
                   1233:                 case 1:
                   1234:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1235:                     break;
                   1236:                 case -1:
                   1237:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1238:                     break;
                   1239:             }
                   1240:         }
                   1241:         if ( !mr0 )
                   1242:             if ( m1 ) mr0 = m1;
                   1243:             else if ( m2 ) mr0 = m2;
                   1244:             else return 0;
                   1245:         else if ( m1 ) NEXT(mr) = m1;
                   1246:         else if ( m2 ) NEXT(mr) = m2;
                   1247:         else NEXT(mr) = 0;
                   1248:         BDY(p1) = mr0;
                   1249:         SG(p1) = MAX(SG(p1),SG(p2));
                   1250:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1251:         FREEND(p2);
                   1252:         return p1;
                   1253:     }
1.71      noro     1254: }
                   1255:
1.167     noro     1256: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1.146     noro     1257: {
1.157     noro     1258:     int c,c1,c2;
                   1259:     Q cg,cred,gcd,tq;
                   1260:     P cgp,credp,gcdp;
                   1261:     Obj tr,tr1;
                   1262:
1.167     noro     1263:     if ( mod == -1 ) {
1.157     noro     1264:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.167     noro     1265:         *divp = (Obj)ONE;
                   1266:     } else if ( mod ) {
1.157     noro     1267:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1268:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.167     noro     1269:         *divp = (Obj)ONE;
1.157     noro     1270:     } else if ( nd_vc ) {
                   1271:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1272:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1273:         chsgnp(cgp,&CP(mul));
                   1274:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1275:         if ( dn ) {
                   1276:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1277:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1278:         }
1.167     noro     1279:         *divp = (Obj)credp;
1.157     noro     1280:     } else {
                   1281:         igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1282:         chsgnq(cg,&CQ(mul));
                   1283:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1284:         if ( dn ) {
                   1285:             mulq(dn->z,cred,&tq); dn->z = tq;
                   1286:         }
1.167     noro     1287:         *divp = (Obj)cred;
1.157     noro     1288:     }
                   1289:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.146     noro     1290: }
                   1291:
1.1       noro     1292: /* ret=1 : success, ret=0 : overflow */
1.146     noro     1293: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1294: {
1.157     noro     1295:     NM m,mrd,tail;
                   1296:     NM mul;
                   1297:     int n,sugar,psugar,sugar0,stat,index;
                   1298:     int c,c1,c2,dummy;
                   1299:     RHist h;
                   1300:     NDV p,red;
1.167     noro     1301:     Q cg,cred,gcd,tq,qq,iq;
                   1302:     DP dmul;
                   1303:     NODE node;
                   1304:     LIST hist;
1.157     noro     1305:     double hmag;
                   1306:     P tp,tp1;
1.167     noro     1307:     Obj tr,tr1,div;
                   1308:     union oNDC hg;
                   1309:     P cont;
1.157     noro     1310:
                   1311:     if ( dn ) {
                   1312:         if ( mod )
                   1313:             dn->m = 1;
                   1314:         else if ( nd_vc )
                   1315:             dn->r = (R)ONE;
                   1316:         else
                   1317:             dn->z = ONE;
                   1318:     }
                   1319:     if ( !g ) {
                   1320:         *rp = d;
                   1321:         return 1;
                   1322:     }
                   1323:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1324:
                   1325:     sugar0 = sugar = SG(g);
                   1326:     n = NV(g);
                   1327:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1328:     if ( d )
                   1329:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1330:     for ( ; g; ) {
                   1331:         index = ndl_find_reducer(HDL(g));
                   1332:         if ( index >= 0 ) {
                   1333:             h = nd_psh[index];
                   1334:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1335:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1336:                 nd_free(g); nd_free(d);
                   1337:                 return 0;
                   1338:             }
                   1339:             p = nd_demand ? ndv_load(index) : ps[index];
1.167     noro     1340:             /* d+g -> div*(d+g)+mul*p */
                   1341:             g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1.172     noro     1342:             if ( nd_gentrace ) {
1.167     noro     1343:                 /* Trace=[div,index,mul,ONE] */
                   1344:                 STOQ(index,iq);
                   1345:                 nmtodp(mod,mul,&dmul);
                   1346:                 node = mknode(4,div,iq,dmul,ONE);
                   1347:             }
1.157     noro     1348:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.193     noro     1349:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1.167     noro     1350:                 hg = HCU(g);
1.157     noro     1351:                 nd_removecont2(d,g);
1.172     noro     1352:                 if ( dn || nd_gentrace ) {
1.167     noro     1353:                     /* overwrite cont : Trace=[div,index,mul,cont] */
                   1354:                     cont = ndc_div(mod,hg,HCU(g));
                   1355:                     if ( dn ) {
                   1356:                         if ( nd_vc ) {
                   1357:                             divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
                   1358:                             reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
                   1359:                         } else divq(dn->z,(Q)cont,&dn->z);
1.157     noro     1360:                     }
1.172     noro     1361:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1.157     noro     1362:                 }
                   1363:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1364:             }
1.167     noro     1365:             MKLIST(hist,node);
                   1366:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.157     noro     1367:         } else if ( !full ) {
                   1368:             *rp = g;
                   1369:             return 1;
                   1370:         } else {
                   1371:             m = BDY(g);
                   1372:             if ( NEXT(m) ) {
                   1373:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1374:             } else {
                   1375:                 FREEND(g); g = 0;
                   1376:             }
                   1377:             if ( d ) {
                   1378:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1379:             } else {
                   1380:                 MKND(n,m,1,d); tail = BDY(d);
                   1381:             }
                   1382:         }
                   1383:     }
                   1384:     if ( d ) SG(d) = sugar;
                   1385:     *rp = d;
                   1386:     return 1;
1.1       noro     1387: }
1.28      noro     1388:
1.53      noro     1389: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1390: {
1.157     noro     1391:     int hindex,index;
                   1392:     NDV p;
                   1393:     ND u,d,red;
                   1394:     NODE l;
                   1395:     NM mul,m,mrd,tail;
                   1396:     int sugar,psugar,n,h_reducible;
                   1397:     PGeoBucket bucket;
                   1398:     int c,c1,c2;
                   1399:     Q cg,cred,gcd,zzz;
                   1400:     RHist h;
                   1401:     double hmag,gmag;
                   1402:     int count = 0;
                   1403:     int hcount = 0;
                   1404:
                   1405:     if ( !g ) {
                   1406:         *rp = 0;
                   1407:         return 1;
                   1408:     }
                   1409:     sugar = SG(g);
                   1410:     n = NV(g);
                   1411:     if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1412:     bucket = create_pbucket();
                   1413:     add_pbucket(mod,bucket,g);
                   1414:     d = 0;
                   1415:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1416:     while ( 1 ) {
                   1417:         hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   1418:         if ( hindex < 0 ) {
                   1419:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1420:             if ( d ) SG(d) = sugar;
                   1421:             *rp = d;
                   1422:             return 1;
                   1423:         }
                   1424:         g = bucket->body[hindex];
                   1425:         index = ndl_find_reducer(HDL(g));
                   1426:         if ( index >= 0 ) {
                   1427:             count++;
                   1428:             if ( !d ) hcount++;
                   1429:             h = nd_psh[index];
                   1430:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1431:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1432:                 nd_free(d);
                   1433:                 free_pbucket(bucket);
                   1434:                 *rp = 0;
                   1435:                 return 0;
                   1436:             }
                   1437:             p = ps[index];
                   1438:             if ( mod == -1 )
                   1439:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1440:              else if ( mod ) {
                   1441:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1442:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1443:             } else {
                   1444:                 igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1445:                 chsgnq(cg,&CQ(mul));
                   1446:                 nd_mul_c_q(d,(P)cred);
                   1447:                 mulq_pbucket(bucket,cred);
                   1448:                 g = bucket->body[hindex];
                   1449:                 gmag = (double)p_mag((P)HCQ(g));
                   1450:             }
                   1451:             red = ndv_mul_nm(mod,mul,p);
                   1452:             bucket->body[hindex] = nd_remove_head(g);
                   1453:             red = nd_remove_head(red);
                   1454:             add_pbucket(mod,bucket,red);
                   1455:             psugar = SG(p)+TD(DL(mul));
                   1456:             sugar = MAX(sugar,psugar);
                   1457:             if ( !mod && hmag && (gmag > hmag) ) {
                   1458:                 g = normalize_pbucket(mod,bucket);
                   1459:                 if ( !g ) {
                   1460:                     if ( d ) SG(d) = sugar;
                   1461:                     *rp = d;
                   1462:                     return 1;
                   1463:                 }
                   1464:                 nd_removecont2(d,g);
                   1465:                 hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1466:                 add_pbucket(mod,bucket,g);
                   1467:             }
                   1468:         } else if ( !full ) {
                   1469:             g = normalize_pbucket(mod,bucket);
                   1470:             if ( g ) SG(g) = sugar;
                   1471:             *rp = g;
                   1472:             return 1;
                   1473:         } else {
                   1474:             m = BDY(g);
                   1475:             if ( NEXT(m) ) {
                   1476:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1477:             } else {
                   1478:                 FREEND(g); g = 0;
                   1479:             }
                   1480:             bucket->body[hindex] = g;
                   1481:             NEXT(m) = 0;
                   1482:             if ( d ) {
                   1483:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1484:             } else {
                   1485:                 MKND(n,m,1,d); tail = BDY(d);
                   1486:             }
                   1487:         }
                   1488:     }
1.25      noro     1489: }
1.27      noro     1490:
1.61      noro     1491: /* input : list of NDV, cand : list of NDV */
1.28      noro     1492:
1.170     noro     1493: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1494: {
1.157     noro     1495:     int n,i,stat;
                   1496:     ND nf,d;
                   1497:     NDV r;
                   1498:     NODE t,s;
                   1499:     union oNDC dn;
1.168     noro     1500:     Q q;
                   1501:     LIST list;
1.45      noro     1502:
1.172     noro     1503:     ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1.157     noro     1504:     n = length(cand);
1.28      noro     1505:
1.172     noro     1506:        if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1.157     noro     1507:     /* membercheck : list is a subset of Id(cand) ? */
1.168     noro     1508:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1.45      noro     1509: again:
1.168     noro     1510:                nd_tracelist = 0;
1.157     noro     1511:         if ( nd_bpe > obpe )
                   1512:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1513:         else
                   1514:             r = (NDV)BDY(t);
1.171     noro     1515:         d = ndvtond(m,r);
                   1516:         stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1.157     noro     1517:         if ( !stat ) {
                   1518:             nd_reconstruct(0,0);
                   1519:             goto again;
                   1520:         } else if ( nf ) return 0;
1.172     noro     1521:                if ( nd_gentrace ) {
1.168     noro     1522:                        nd_tracelist = reverse_node(nd_tracelist);
                   1523:                        MKLIST(list,nd_tracelist);
                   1524:                        STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
                   1525:                        MKNODE(s,list,nd_alltracelist);
                   1526:                        nd_alltracelist = s; nd_tracelist = 0;
                   1527:                }
1.157     noro     1528:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1529:     }
                   1530:     if ( DP_Print ) { printf("\n"); }
                   1531:     return 1;
1.23      noro     1532: }
1.1       noro     1533:
                   1534: ND nd_remove_head(ND p)
                   1535: {
1.157     noro     1536:     NM m;
1.1       noro     1537:
1.157     noro     1538:     m = BDY(p);
                   1539:     if ( !NEXT(m) ) {
                   1540:         FREEND(p); p = 0;
                   1541:     } else {
                   1542:         BDY(p) = NEXT(m); LEN(p)--;
                   1543:     }
                   1544:     FREENM(m);
                   1545:     return p;
1.1       noro     1546: }
                   1547:
1.69      noro     1548: ND nd_separate_head(ND p,ND *head)
                   1549: {
1.157     noro     1550:     NM m,m0;
                   1551:     ND r;
1.69      noro     1552:
1.157     noro     1553:     m = BDY(p);
                   1554:     if ( !NEXT(m) ) {
                   1555:         *head = p; p = 0;
                   1556:     } else {
                   1557:         m0 = m;
                   1558:         BDY(p) = NEXT(m); LEN(p)--;
                   1559:         NEXT(m0) = 0;
                   1560:         MKND(NV(p),m0,1,r);
                   1561:         *head = r;
                   1562:     }
                   1563:     return p;
1.69      noro     1564: }
                   1565:
1.1       noro     1566: PGeoBucket create_pbucket()
                   1567: {
                   1568:     PGeoBucket g;
1.157     noro     1569:
                   1570:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1571:     g->m = -1;
                   1572:     return g;
1.1       noro     1573: }
                   1574:
1.25      noro     1575: void free_pbucket(PGeoBucket b) {
1.157     noro     1576:     int i;
1.25      noro     1577:
1.157     noro     1578:     for ( i = 0; i <= b->m; i++ )
                   1579:         if ( b->body[i] ) {
                   1580:             nd_free(b->body[i]);
                   1581:             b->body[i] = 0;
                   1582:         }
1.200   ! noro     1583:     GCFREE(b);
1.25      noro     1584: }
                   1585:
1.63      noro     1586: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1587: {
1.157     noro     1588:     int l,i,k,m;
1.63      noro     1589:
1.157     noro     1590:     if ( !d )
                   1591:         return;
                   1592:     l = LEN(d);
                   1593:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1594:     /* 2^(k-1) < l <= 2^k (=m) */
                   1595:     d = nd_merge(g->body[k],d);
                   1596:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1597:         g->body[k] = 0;
                   1598:         d = nd_merge(g->body[k+1],d);
                   1599:     }
                   1600:     g->body[k] = d;
                   1601:     g->m = MAX(g->m,k);
1.63      noro     1602: }
                   1603:
1.31      noro     1604: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1605: {
1.157     noro     1606:     int l,i,k,m;
1.1       noro     1607:
1.157     noro     1608:     if ( !d )
                   1609:         return;
                   1610:     l = LEN(d);
                   1611:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1612:     /* 2^(k-1) < l <= 2^k (=m) */
                   1613:     d = nd_add(mod,g->body[k],d);
                   1614:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1615:         g->body[k] = 0;
                   1616:         d = nd_add(mod,g->body[k+1],d);
                   1617:     }
                   1618:     g->body[k] = d;
                   1619:     g->m = MAX(g->m,k);
1.1       noro     1620: }
                   1621:
1.113     noro     1622: void mulq_pbucket(PGeoBucket g,Q c)
1.26      noro     1623: {
1.157     noro     1624:     int k;
1.26      noro     1625:
1.157     noro     1626:     for ( k = 0; k <= g->m; k++ )
                   1627:         nd_mul_c_q(g->body[k],(P)c);
1.26      noro     1628: }
                   1629:
1.63      noro     1630: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1631: {
1.157     noro     1632:     int j,i,k,c;
                   1633:     NM head;
                   1634:
                   1635:     k = g->m;
                   1636:     j = -1;
                   1637:     for ( i = 0; i <= k; i++ ) {
                   1638:         if ( !g->body[i] ) continue;
                   1639:         if ( j < 0 ) j = i;
                   1640:         else {
                   1641:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1642:             if ( c > 0 )
                   1643:                 j = i;
                   1644:             else if ( c == 0 )
                   1645:                 g->body[i] = nd_remove_head(g->body[i]);
                   1646:         }
                   1647:     }
                   1648:     if ( j < 0 ) return 0;
                   1649:     else {
                   1650:         head = BDY(g->body[j]);
                   1651:         if ( !NEXT(head) ) {
                   1652:             FREEND(g->body[j]);
                   1653:             g->body[j] = 0;
                   1654:         } else {
                   1655:             BDY(g->body[j]) = NEXT(head);
                   1656:             LEN(g->body[j])--;
                   1657:         }
                   1658:         return head;
                   1659:     }
1.63      noro     1660: }
                   1661:
1.19      noro     1662: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1663: {
1.157     noro     1664:     int j,i,c,k,nv,sum;
                   1665:     UINT *di,*dj;
                   1666:     ND gi,gj;
                   1667:
                   1668:     k = g->m;
                   1669:     while ( 1 ) {
                   1670:         j = -1;
                   1671:         for ( i = 0; i <= k; i++ ) {
                   1672:             if ( !(gi = g->body[i]) )
                   1673:                 continue;
                   1674:             if ( j < 0 ) {
                   1675:                 j = i;
                   1676:                 gj = g->body[j];
                   1677:                 dj = HDL(gj);
                   1678:                 sum = HCM(gj);
                   1679:             } else {
                   1680:                 c = DL_COMPARE(HDL(gi),dj);
                   1681:                 if ( c > 0 ) {
                   1682:                     if ( sum ) HCM(gj) = sum;
                   1683:                     else g->body[j] = nd_remove_head(gj);
                   1684:                     j = i;
                   1685:                     gj = g->body[j];
                   1686:                     dj = HDL(gj);
                   1687:                     sum = HCM(gj);
                   1688:                 } else if ( c == 0 ) {
                   1689:                     if ( mod == -1 )
                   1690:                         sum = _addsf(sum,HCM(gi));
                   1691:                     else {
                   1692:                         sum = sum+HCM(gi)-mod;
                   1693:                         if ( sum < 0 ) sum += mod;
                   1694:                     }
                   1695:                     g->body[i] = nd_remove_head(gi);
                   1696:                 }
                   1697:             }
                   1698:         }
                   1699:         if ( j < 0 ) return -1;
                   1700:         else if ( sum ) {
                   1701:             HCM(gj) = sum;
                   1702:             return j;
                   1703:         } else
                   1704:             g->body[j] = nd_remove_head(gj);
                   1705:     }
1.26      noro     1706: }
                   1707:
1.113     noro     1708: int head_pbucket_q(PGeoBucket g)
1.26      noro     1709: {
1.157     noro     1710:     int j,i,c,k,nv;
                   1711:     Q sum,t;
                   1712:     ND gi,gj;
                   1713:
                   1714:     k = g->m;
                   1715:     while ( 1 ) {
                   1716:         j = -1;
                   1717:         for ( i = 0; i <= k; i++ ) {
                   1718:             if ( !(gi = g->body[i]) ) continue;
                   1719:             if ( j < 0 ) {
                   1720:                 j = i;
                   1721:                 gj = g->body[j];
                   1722:                 sum = HCQ(gj);
                   1723:             } else {
                   1724:                 nv = NV(gi);
                   1725:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1726:                 if ( c > 0 ) {
                   1727:                     if ( sum ) HCQ(gj) = sum;
                   1728:                     else g->body[j] = nd_remove_head(gj);
                   1729:                     j = i;
                   1730:                     gj = g->body[j];
                   1731:                     sum = HCQ(gj);
                   1732:                 } else if ( c == 0 ) {
                   1733:                     addq(sum,HCQ(gi),&t);
                   1734:                     sum = t;
                   1735:                     g->body[i] = nd_remove_head(gi);
                   1736:                 }
                   1737:             }
                   1738:         }
                   1739:         if ( j < 0 ) return -1;
                   1740:         else if ( sum ) {
                   1741:             HCQ(gj) = sum;
                   1742:             return j;
                   1743:         } else
                   1744:             g->body[j] = nd_remove_head(gj);
                   1745:     }
1.1       noro     1746: }
                   1747:
1.25      noro     1748: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1749: {
1.157     noro     1750:     int i;
                   1751:     ND r,t;
1.1       noro     1752:
1.157     noro     1753:     r = 0;
                   1754:     for ( i = 0; i <= g->m; i++ ) {
                   1755:         r = nd_add(mod,r,g->body[i]);
                   1756:         g->body[i] = 0;
                   1757:     }
                   1758:     g->m = -1;
                   1759:     return r;
1.1       noro     1760: }
                   1761:
1.150     noro     1762: #if 0
                   1763: void register_hcf(NDV p)
                   1764: {
1.157     noro     1765:     DCP dc,t;
                   1766:     P hc,h;
                   1767:     int c;
                   1768:     NODE l,l1,prev;
                   1769:
                   1770:     hc = p->body->c.p;
                   1771:     if ( !nd_vc || NUM(hc) ) return;
                   1772:     fctrp(nd_vc,hc,&dc);
                   1773:     for ( t = dc; t; t = NEXT(t) ) {
                   1774:         h = t->c;
                   1775:         if ( NUM(h) ) continue;
                   1776:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1777:             c = compp(nd_vc,h,(P)BDY(l));
                   1778:             if ( c >= 0 ) break;
                   1779:         }
                   1780:         if ( !l || c > 0  ) {
                   1781:             MKNODE(l1,h,l);
                   1782:             if ( !prev )
                   1783:                 nd_hcf = l1;
                   1784:             else
                   1785:                 NEXT(prev) = l1;
                   1786:         }
                   1787:     }
1.150     noro     1788: }
                   1789: #else
                   1790: void register_hcf(NDV p)
                   1791: {
1.157     noro     1792:     DCP dc,t;
                   1793:     P hc,h,q;
                   1794:     Q dmy;
                   1795:     int c;
                   1796:     NODE l,l1,prev;
                   1797:
                   1798:     hc = p->body->c.p;
                   1799:     if ( NUM(hc) ) return;
                   1800:     ptozp(hc,1,&dmy,&h);
1.150     noro     1801: #if 1
1.157     noro     1802:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   1803:         while ( 1 ) {
                   1804:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   1805:             else break;
                   1806:         }
                   1807:     }
                   1808:     if ( NUM(h) ) return;
1.150     noro     1809: #endif
1.157     noro     1810:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1811:         c = compp(nd_vc,h,(P)BDY(l));
                   1812:         if ( c >= 0 ) break;
                   1813:     }
                   1814:     if ( !l || c > 0  ) {
                   1815:         MKNODE(l1,h,l);
                   1816:         if ( !prev )
                   1817:             nd_hcf = l1;
                   1818:         else
                   1819:             NEXT(prev) = l1;
                   1820:     }
1.150     noro     1821: }
                   1822: #endif
                   1823:
1.122     noro     1824: int do_diagonalize(int sugar,int m)
1.92      noro     1825: {
1.157     noro     1826:     int i,nh,stat;
                   1827:     NODE r,g,t;
                   1828:     ND h,nf,s,head;
                   1829:     NDV nfv;
                   1830:     Q q,num,den;
1.167     noro     1831:     P nm,nmp,dn,mnp,dnp,cont,cont1;
                   1832:     union oNDC hc;
                   1833:     NODE node;
                   1834:     LIST l;
                   1835:     Q iq;
1.157     noro     1836:
                   1837:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     1838:         if ( nd_gentrace ) {
1.167     noro     1839:             /* Trace = [1,index,1,1] */
                   1840:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   1841:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   1842:         }
1.157     noro     1843:         if ( nd_demand )
                   1844:             nfv = ndv_load(i);
                   1845:         else
                   1846:             nfv = nd_ps[i];
                   1847:         s = ndvtond(m,nfv);
                   1848:         s = nd_separate_head(s,&head);
                   1849:         stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
                   1850:         if ( !stat ) return 0;
                   1851:         ndv_free(nfv);
1.167     noro     1852:         hc = HCU(nf); nd_removecont(m,nf);
                   1853:         cont = ndc_div(m,hc,HCU(nf));
1.172     noro     1854:                if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     1855:         nfv = ndtondv(m,nf);
                   1856:         nd_free(nf);
                   1857:         nd_bound[i] = ndv_compute_bound(nfv);
                   1858:         if ( !m ) register_hcf(nfv);
                   1859:         if ( nd_demand ) {
                   1860:             ndv_save(nfv,i);
                   1861:             ndv_free(nfv);
                   1862:         } else
                   1863:             nd_ps[i] = nfv;
                   1864:     }
                   1865:     return 1;
1.92      noro     1866: }
                   1867:
1.27      noro     1868: /* return value = 0 => input is not a GB */
                   1869:
1.168     noro     1870: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
1.1       noro     1871: {
1.157     noro     1872:     int i,nh,sugar,stat;
                   1873:     NODE r,g,t;
                   1874:     ND_pairs d;
                   1875:     ND_pairs l;
                   1876:     ND h,nf,s,head,nf1;
                   1877:     NDV nfv;
                   1878:     Q q,num,den;
1.167     noro     1879:     union oNDC dn,hc;
1.157     noro     1880:     int diag_count = 0;
1.167     noro     1881:     P cont;
                   1882:     LIST list;
1.157     noro     1883:
                   1884:     g = 0; d = 0;
                   1885:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     1886:         d = update_pairs(d,g,i,gensyz);
1.157     noro     1887:         g = update_base(g,i);
                   1888:     }
                   1889:     sugar = 0;
                   1890:     while ( d ) {
1.1       noro     1891: again:
1.157     noro     1892:         l = nd_minp(d,&d);
                   1893:         if ( SG(l) != sugar ) {
                   1894:             if ( ishomo ) {
                   1895:                 diag_count = 0;
                   1896:                 stat = do_diagonalize(sugar,m);
                   1897:                 if ( !stat ) {
                   1898:                     NEXT(l) = d; d = l;
                   1899:                     d = nd_reconstruct(0,d);
                   1900:                     goto again;
                   1901:                 }
                   1902:             }
                   1903:             sugar = SG(l);
                   1904:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   1905:         }
                   1906:         stat = nd_sp(m,0,l,&h);
                   1907:         if ( !stat ) {
                   1908:             NEXT(l) = d; d = l;
                   1909:             d = nd_reconstruct(0,d);
                   1910:             goto again;
                   1911:         }
1.41      noro     1912: #if USE_GEOBUCKET
1.172     noro     1913:         stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
1.167     noro     1914:                :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1915: #else
1.157     noro     1916:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1917: #endif
1.157     noro     1918:         if ( !stat ) {
                   1919:             NEXT(l) = d; d = l;
                   1920:             d = nd_reconstruct(0,d);
                   1921:             goto again;
                   1922:         } else if ( nf ) {
1.168     noro     1923:             if ( checkonly || gensyz ) return 0;
1.192     noro     1924:                        if ( nd_newelim ) {
                   1925:                                if ( nd_module ) {
                   1926:                                        if ( MPOS(HDL(nf)) > 1 ) return 0;
                   1927:                                } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   1928:                        }
1.157     noro     1929:             if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     1930:             hc = HCU(nf);
1.157     noro     1931:             nd_removecont(m,nf);
                   1932:             if ( !m && nd_nalg ) {
                   1933:                 nd_monic(0,&nf);
                   1934:                 nd_removecont(m,nf);
                   1935:             }
1.172     noro     1936:             if ( nd_gentrace ) {
1.167     noro     1937:                                cont = ndc_div(m,hc,HCU(nf));
                   1938:                                if ( m || !UNIQ(cont) ) {
1.196     noro     1939:                     t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     1940:                     MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   1941:                                        nd_tracelist = t;
                   1942:                                }
                   1943:             }
1.157     noro     1944:             nfv = ndtondv(m,nf); nd_free(nf);
                   1945:             nh = ndv_newps(m,nfv,0);
                   1946:             if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   1947:                 diag_count = 0;
                   1948:                 stat = do_diagonalize(sugar,m);
                   1949:                 if ( !stat ) {
                   1950:                     NEXT(l) = d; d = l;
                   1951:                     d = nd_reconstruct(1,d);
                   1952:                     goto again;
                   1953:                 }
                   1954:             }
1.168     noro     1955:             d = update_pairs(d,g,nh,0);
1.157     noro     1956:             g = update_base(g,nh);
                   1957:             FREENDP(l);
                   1958:         } else {
1.172     noro     1959:                    if ( nd_gentrace && gensyz ) {
1.168     noro     1960:                 nd_tracelist = reverse_node(nd_tracelist);
                   1961:                                MKLIST(list,nd_tracelist);
                   1962:                 STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   1963:                 MKNODE(t,list,nd_alltracelist);
                   1964:                                nd_alltracelist = t; nd_tracelist = 0;
                   1965:                        }
1.157     noro     1966:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   1967:             FREENDP(l);
                   1968:         }
                   1969:     }
1.167     noro     1970:        conv_ilist(nd_demand,0,g,indp);
1.157     noro     1971:     if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
                   1972:     return g;
1.1       noro     1973: }
                   1974:
1.122     noro     1975: int do_diagonalize_trace(int sugar,int m)
1.91      noro     1976: {
1.157     noro     1977:     int i,nh,stat;
                   1978:     NODE r,g,t;
                   1979:     ND h,nf,nfq,s,head;
                   1980:     NDV nfv,nfqv;
                   1981:     Q q,den,num;
1.167     noro     1982:     union oNDC hc;
                   1983:     NODE node;
                   1984:     LIST l;
                   1985:     Q iq;
                   1986:     P cont,cont1;
1.157     noro     1987:
                   1988:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     1989:         if ( nd_gentrace ) {
1.167     noro     1990:             /* Trace = [1,index,1,1] */
                   1991:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   1992:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   1993:         }
1.157     noro     1994:         /* for nd_ps */
                   1995:         s = ndvtond(m,nd_ps[i]);
                   1996:         s = nd_separate_head(s,&head);
                   1997:         stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1998:         if ( !stat ) return 0;
                   1999:         nf = nd_add(m,head,nf);
                   2000:         ndv_free(nd_ps[i]);
                   2001:         nd_ps[i] = ndtondv(m,nf);
                   2002:         nd_free(nf);
                   2003:
                   2004:         /* for nd_ps_trace */
                   2005:         if ( nd_demand )
                   2006:             nfv = ndv_load(i);
                   2007:         else
                   2008:             nfv = nd_ps_trace[i];
                   2009:         s = ndvtond(0,nfv);
                   2010:         s = nd_separate_head(s,&head);
                   2011:         stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
                   2012:         if ( !stat ) return 0;
                   2013:         ndv_free(nfv);
1.167     noro     2014:         hc = HCU(nf); nd_removecont(0,nf);
                   2015:                cont = ndc_div(0,hc,HCU(nf));
1.172     noro     2016:         if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2017:         nfv = ndtondv(0,nf);
                   2018:         nd_free(nf);
                   2019:         nd_bound[i] = ndv_compute_bound(nfv);
                   2020:         register_hcf(nfv);
                   2021:         if ( nd_demand ) {
                   2022:             ndv_save(nfv,i);
                   2023:             ndv_free(nfv);
                   2024:         } else
                   2025:             nd_ps_trace[i] = nfv;
                   2026:     }
                   2027:     return 1;
1.91      noro     2028: }
                   2029:
1.118     noro     2030: static struct oEGT eg_invdalg;
                   2031: struct oEGT eg_le;
                   2032:
1.147     noro     2033: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2034: {
1.157     noro     2035:     NODE tn;
                   2036:     P p1;
1.147     noro     2037:
1.157     noro     2038:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2039:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2040:     }
                   2041:     *r = p;
1.147     noro     2042: }
                   2043:
1.167     noro     2044: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20      noro     2045: {
1.157     noro     2046:     int i,nh,sugar,stat;
                   2047:     NODE r,g,t;
                   2048:     ND_pairs d;
                   2049:     ND_pairs l;
                   2050:     ND h,nf,nfq,s,head;
                   2051:     NDV nfv,nfqv;
                   2052:     Q q,den,num;
                   2053:     P hc;
1.167     noro     2054:     union oNDC dn,hnfq;
1.157     noro     2055:     struct oEGT eg_monic,egm0,egm1;
                   2056:     int diag_count = 0;
1.167     noro     2057:     P cont;
                   2058:     LIST list;
1.157     noro     2059:
                   2060:     init_eg(&eg_monic);
                   2061:     init_eg(&eg_invdalg);
                   2062:     init_eg(&eg_le);
                   2063:     g = 0; d = 0;
                   2064:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2065:         d = update_pairs(d,g,i,0);
1.157     noro     2066:         g = update_base(g,i);
                   2067:     }
                   2068:     sugar = 0;
                   2069:     while ( d ) {
1.20      noro     2070: again:
1.157     noro     2071:         l = nd_minp(d,&d);
                   2072:         if ( SG(l) != sugar ) {
1.130     noro     2073: #if 1
1.157     noro     2074:             if ( ishomo ) {
                   2075:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2076:                 stat = do_diagonalize_trace(sugar,m);
                   2077:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2078:                 diag_count = 0;
                   2079:                 if ( !stat ) {
                   2080:                     NEXT(l) = d; d = l;
                   2081:                     d = nd_reconstruct(1,d);
                   2082:                     goto again;
                   2083:                 }
                   2084:             }
1.130     noro     2085: #endif
1.157     noro     2086:             sugar = SG(l);
                   2087:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2088:         }
                   2089:         stat = nd_sp(m,0,l,&h);
                   2090:         if ( !stat ) {
                   2091:             NEXT(l) = d; d = l;
                   2092:             d = nd_reconstruct(1,d);
                   2093:             goto again;
                   2094:         }
1.41      noro     2095: #if USE_GEOBUCKET
1.157     noro     2096:         stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     2097: #else
1.157     noro     2098:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2099: #endif
1.157     noro     2100:         if ( !stat ) {
                   2101:             NEXT(l) = d; d = l;
                   2102:             d = nd_reconstruct(1,d);
                   2103:             goto again;
                   2104:         } else if ( nf ) {
                   2105:             if ( nd_demand ) {
                   2106:                 nfqv = ndv_load(nd_psn);
                   2107:                 nfq = ndvtond(0,nfqv);
                   2108:             } else
                   2109:                 nfq = 0;
                   2110:             if ( !nfq ) {
                   2111:                 if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   2112:                     NEXT(l) = d; d = l;
                   2113:                     d = nd_reconstruct(1,d);
                   2114:                     goto again;
                   2115:                 }
                   2116:             }
                   2117:             if ( nfq ) {
                   2118:                 /* m|HC(nfq) => failure */
                   2119:                 if ( nd_vc ) {
                   2120:                     nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
                   2121:                 } else
                   2122:                     q = HCQ(nfq);
                   2123:                 if ( !rem(NM(q),m) ) return 0;
                   2124:
                   2125:                 if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2126:                 hnfq = HCU(nfq);
1.157     noro     2127:                 if ( nd_nalg ) {
                   2128:                     /* m|DN(HC(nf)^(-1)) => failure */
                   2129:                     get_eg(&egm0);
                   2130:                     if ( !nd_monic(m,&nfq) ) return 0;
                   2131:                     get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2132:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2133:                     nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2134:                 } else {
                   2135:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2136:                     nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2137:                 }
1.172     noro     2138:                 if ( nd_gentrace ) {
1.167     noro     2139:                                   cont = ndc_div(0,hnfq,HCU(nfqv));
                   2140:                                   if ( !UNIQ(cont) ) {
1.196     noro     2141:                        t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2142:                        MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2143:                                           nd_tracelist = t;
                   2144:                                   }
                   2145:                 }
1.157     noro     2146:                 nh = ndv_newps(0,nfv,nfqv);
                   2147:                 if ( ishomo && ++diag_count == diag_period ) {
                   2148:                     diag_count = 0;
                   2149:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2150:                     stat = do_diagonalize_trace(sugar,m);
                   2151:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2152:                     if ( !stat ) {
                   2153:                         NEXT(l) = d; d = l;
                   2154:                         d = nd_reconstruct(1,d);
                   2155:                         goto again;
                   2156:                     }
                   2157:                 }
1.168     noro     2158:                 d = update_pairs(d,g,nh,0);
1.157     noro     2159:                 g = update_base(g,nh);
                   2160:             } else {
                   2161:                 if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2162:             }
                   2163:         } else {
                   2164:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2165:         }
                   2166:         FREENDP(l);
                   2167:     }
                   2168:     if ( nd_nalg ) {
                   2169:         print_eg("monic",&eg_monic);
                   2170:         print_eg("invdalg",&eg_invdalg);
                   2171:         print_eg("le",&eg_le);
                   2172:     }
1.167     noro     2173:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     2174:     if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   2175:     return g;
1.20      noro     2176: }
                   2177:
1.23      noro     2178: int ndv_compare(NDV *p1,NDV *p2)
                   2179: {
1.157     noro     2180:     return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2181: }
                   2182:
                   2183: int ndv_compare_rev(NDV *p1,NDV *p2)
                   2184: {
1.157     noro     2185:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2186: }
                   2187:
1.167     noro     2188: int ndvi_compare(NDVI p1,NDVI p2)
                   2189: {
                   2190:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2191: }
                   2192:
                   2193: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   2194: {
                   2195:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2196: }
                   2197:
1.61      noro     2198: NODE ndv_reduceall(int m,NODE f)
1.23      noro     2199: {
1.167     noro     2200:     int i,j,n,stat;
1.157     noro     2201:     ND nf,g,head;
                   2202:     NODE t,a0,a;
                   2203:     union oNDC dn;
                   2204:     Q q,num,den;
1.167     noro     2205:     NODE node;
                   2206:        LIST l;
                   2207:        Q iq,jq;
                   2208:     int *perm;
                   2209:     union oNDC hc;
                   2210:     P cont,cont1;
1.23      noro     2211:
1.173     noro     2212:     if ( nd_nora ) return f;
1.157     noro     2213:     n = length(f);
                   2214:     ndv_setup(m,0,f,0,1);
1.167     noro     2215:        perm = (int *)MALLOC(n*sizeof(int));
1.172     noro     2216:        if ( nd_gentrace ) {
1.167     noro     2217:            for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2218:                    perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2219:        }
1.157     noro     2220:     for ( i = 0; i < n; ) {
1.172     noro     2221:         if ( nd_gentrace ) {
1.167     noro     2222:             /* Trace = [1,index,1,1] */
                   2223:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2224:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2225:         }
1.157     noro     2226:         g = ndvtond(m,nd_ps[i]);
                   2227:         g = nd_separate_head(g,&head);
                   2228:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2229:         if ( !stat )
                   2230:             nd_reconstruct(0,0);
                   2231:         else {
                   2232:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2233:             ndv_free(nd_ps[i]);
1.167     noro     2234:             hc = HCU(nf); nd_removecont(m,nf);
1.172     noro     2235:             if ( nd_gentrace ) {
1.167     noro     2236:                                for ( t = nd_tracelist; t; t = NEXT(t) ) {
                   2237:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2238:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2239:                 }
                   2240:                 cont = ndc_div(m,hc,HCU(nf));
                   2241:                 finalize_tracelist(perm[i],cont);
                   2242:             }
1.157     noro     2243:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2244:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2245:             i++;
                   2246:         }
                   2247:     }
                   2248:     if ( DP_Print ) { printf("\n"); }
                   2249:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2250:         NEXTNODE(a0,a);
1.172     noro     2251:                if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
1.167     noro     2252:                else {
                   2253:                        for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2254:                        BDY(a) = (pointer)nd_ps[j];
                   2255:                }
1.157     noro     2256:     }
                   2257:     NEXT(a) = 0;
                   2258:     return a0;
1.23      noro     2259: }
                   2260:
1.168     noro     2261: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2262: {
1.157     noro     2263:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2264:
1.157     noro     2265:     if ( !g ) return d;
1.168     noro     2266:        /* for testing */
1.172     noro     2267:        if ( gensyz && nd_gensyz == 2 ) {
1.168     noro     2268:        d1 = nd_newpairs(g,t);
                   2269:        if ( !d )
                   2270:                 return d1;
                   2271:        else {
                   2272:                 nd = d;
                   2273:                 while ( NEXT(nd) ) nd = NEXT(nd);
                   2274:                 NEXT(nd) = d1;
                   2275:                 return d;
                   2276:        }
                   2277:        }
1.157     noro     2278:     d = crit_B(d,t);
                   2279:     d1 = nd_newpairs(g,t);
                   2280:     d1 = crit_M(d1);
                   2281:     d1 = crit_F(d1);
1.168     noro     2282:     if ( gensyz || do_weyl )
1.157     noro     2283:         head = d1;
                   2284:     else {
                   2285:         prev = 0; cur = head = d1;
                   2286:         while ( cur ) {
                   2287:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2288:                 remove = cur;
                   2289:                 if ( !prev ) head = cur = NEXT(cur);
                   2290:                 else cur = NEXT(prev) = NEXT(cur);
                   2291:                 FREENDP(remove);
                   2292:             } else {
                   2293:                 prev = cur; cur = NEXT(cur);
                   2294:             }
                   2295:         }
                   2296:     }
                   2297:     if ( !d )
                   2298:         return head;
                   2299:     else {
                   2300:         nd = d;
                   2301:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2302:         NEXT(nd) = head;
                   2303:         return d;
                   2304:     }
1.1       noro     2305: }
                   2306:
1.157     noro     2307:
1.1       noro     2308: ND_pairs nd_newpairs( NODE g, int t )
                   2309: {
1.157     noro     2310:     NODE h;
                   2311:     UINT *dl;
1.187     noro     2312:     int ts,s,i,t0,min,max;
1.157     noro     2313:     ND_pairs r,r0;
                   2314:
                   2315:     dl = DL(nd_psh[t]);
                   2316:     ts = SG(nd_psh[t]) - TD(dl);
1.195     noro     2317:        if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
1.157     noro     2318:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2319:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2320:                 continue;
1.187     noro     2321:                if ( nd_gbblock ) {
                   2322:                        t0 = (long)BDY(h);
                   2323:                        for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   2324:                                min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   2325:                                if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   2326:                                        break;
                   2327:                        }
1.188     noro     2328:                        if ( nd_gbblock[i] >= 0 )
1.187     noro     2329:                                continue;
                   2330:                }
1.157     noro     2331:         NEXTND_pairs(r0,r);
1.159     noro     2332:         r->i1 = (long)BDY(h);
1.157     noro     2333:         r->i2 = t;
                   2334:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2335:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2336:         SG(r) = MAX(s,ts) + TD(LCM(r));
                   2337:     }
                   2338:     if ( r0 ) NEXT(r) = 0;
                   2339:     return r0;
1.1       noro     2340: }
                   2341:
1.157     noro     2342: /* kokokara */
                   2343:
1.1       noro     2344: ND_pairs crit_B( ND_pairs d, int s )
                   2345: {
1.157     noro     2346:     ND_pairs cur,head,prev,remove;
                   2347:     UINT *t,*tl,*lcm;
                   2348:     int td,tdl;
                   2349:
                   2350:     if ( !d ) return 0;
                   2351:     t = DL(nd_psh[s]);
                   2352:     prev = 0;
                   2353:     head = cur = d;
                   2354:     lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2355:     while ( cur ) {
                   2356:         tl = cur->lcm;
1.163     noro     2357:         if ( ndl_reducible(tl,t) ) {
                   2358:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2359:             if ( !ndl_equal(lcm,tl) ) {
                   2360:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2361:                 if (!ndl_equal(lcm,tl)) {
                   2362:                     remove = cur;
                   2363:                     if ( !prev ) {
                   2364:                         head = cur = NEXT(cur);
                   2365:                     } else {
                   2366:                         cur = NEXT(prev) = NEXT(cur);
                   2367:                     }
                   2368:                     FREENDP(remove);
                   2369:                 } else {
                   2370:                     prev = cur; cur = NEXT(cur);
                   2371:                 }
                   2372:             } else {
                   2373:                     prev = cur; cur = NEXT(cur);
                   2374:             }
1.157     noro     2375:         } else {
                   2376:             prev = cur; cur = NEXT(cur);
                   2377:         }
                   2378:     }
                   2379:     return head;
1.1       noro     2380: }
                   2381:
                   2382: ND_pairs crit_M( ND_pairs d1 )
                   2383: {
1.157     noro     2384:     ND_pairs e,d2,d3,dd,p;
                   2385:     UINT *id,*jd;
1.1       noro     2386:
1.157     noro     2387:     if ( !d1 ) return d1;
                   2388:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2389:         if ( !(d2 = NEXT(e)) ) {
                   2390:             NEXT(e) = dd;
                   2391:             return e;
                   2392:         }
                   2393:         id = LCM(e);
                   2394:         for ( d3 = 0; d2; d2 = p ) {
                   2395:             p = NEXT(d2);
                   2396:             jd = LCM(d2);
                   2397:             if ( ndl_equal(jd,id) )
                   2398:                 ;
                   2399:             else if ( TD(jd) > TD(id) )
                   2400:                 if ( ndl_reducible(jd,id) ) continue;
                   2401:                 else ;
                   2402:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2403:             NEXT(d2) = d3;
                   2404:             d3 = d2;
                   2405:         }
                   2406:         NEXT(e) = dd;
                   2407:         dd = e;
                   2408:         continue;
                   2409:         /**/
                   2410:     delit:    NEXT(d2) = d3;
                   2411:         d3 = d2;
                   2412:         for ( ; p; p = d2 ) {
                   2413:             d2 = NEXT(p);
                   2414:             NEXT(p) = d3;
                   2415:             d3 = p;
                   2416:         }
                   2417:         FREENDP(e);
                   2418:     }
                   2419:     return dd;
1.1       noro     2420: }
                   2421:
                   2422: ND_pairs crit_F( ND_pairs d1 )
                   2423: {
1.157     noro     2424:     ND_pairs rest, head,remove;
                   2425:     ND_pairs last, p, r, w;
                   2426:     int s;
                   2427:
                   2428:     if ( !d1 ) return d1;
                   2429:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2430:         r = w = equivalent_pairs(p,&rest);
                   2431:         s = SG(r);
                   2432:         w = NEXT(w);
                   2433:         while ( w ) {
                   2434:             if ( crit_2(w->i1,w->i2) ) {
                   2435:                 r = w;
                   2436:                 w = NEXT(w);
                   2437:                 while ( w ) {
                   2438:                     remove = w;
                   2439:                     w = NEXT(w);
                   2440:                     FREENDP(remove);
                   2441:                 }
                   2442:                 break;
                   2443:             } else if ( SG(w) < s ) {
                   2444:                 FREENDP(r);
                   2445:                 r = w;
                   2446:                 s = SG(r);
                   2447:                 w = NEXT(w);
                   2448:             } else {
                   2449:                 remove = w;
                   2450:                 w = NEXT(w);
                   2451:                 FREENDP(remove);
                   2452:             }
                   2453:         }
                   2454:         if ( last ) NEXT(last) = r;
                   2455:         else head = r;
                   2456:         NEXT(last = r) = 0;
                   2457:         p = rest;
                   2458:         if ( !p ) return head;
                   2459:     }
                   2460:     if ( !last ) return p;
                   2461:     NEXT(last) = p;
                   2462:     return head;
1.1       noro     2463: }
                   2464:
                   2465: int crit_2( int dp1, int dp2 )
                   2466: {
1.157     noro     2467:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2468: }
                   2469:
1.40      noro     2470: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2471: {
1.157     noro     2472:     ND_pairs w,p,r,s;
                   2473:     UINT *d;
1.1       noro     2474:
1.157     noro     2475:     w = d1;
                   2476:     d = LCM(w);
                   2477:     s = NEXT(w);
                   2478:     NEXT(w) = 0;
                   2479:     for ( r = 0; s; s = p ) {
                   2480:         p = NEXT(s);
                   2481:         if ( ndl_equal(d,LCM(s)) ) {
                   2482:             NEXT(s) = w; w = s;
                   2483:         } else {
                   2484:             NEXT(s) = r; r = s;
                   2485:         }
                   2486:     }
                   2487:     *prest = r;
                   2488:     return w;
1.1       noro     2489: }
                   2490:
                   2491: NODE update_base(NODE nd,int ndp)
                   2492: {
1.157     noro     2493:     UINT *dl, *dln;
                   2494:     NODE last, p, head;
1.1       noro     2495:
1.157     noro     2496:     dl = DL(nd_psh[ndp]);
                   2497:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2498:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2499:         if ( ndl_reducible( dln, dl ) ) {
                   2500:             p = NEXT(p);
                   2501:             if ( last ) NEXT(last) = p;
                   2502:         } else {
                   2503:             if ( !last ) head = p;
                   2504:             p = NEXT(last = p);
                   2505:         }
                   2506:     }
                   2507:     head = append_one(head,ndp);
                   2508:     return head;
1.1       noro     2509: }
                   2510:
                   2511: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2512: {
1.157     noro     2513:     ND_pairs m,ml,p,l;
                   2514:     UINT *lcm;
                   2515:     int s,td,len,tlen,c,c1;
                   2516:
                   2517:     if ( !(p = NEXT(m = d)) ) {
                   2518:         *prest = p;
                   2519:         NEXT(m) = 0;
                   2520:         return m;
                   2521:     }
                   2522:     s = SG(m);
                   2523:     if ( !NoSugar ) {
                   2524:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2525:             if ( (SG(p) < s)
                   2526:                 || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2527:                 ml = l; m = p; s = SG(m);
                   2528:             }
                   2529:     } else {
                   2530:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2531:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2532:                 ml = l; m = p; s = SG(m);
                   2533:             }
                   2534:     }
                   2535:     if ( !ml ) *prest = NEXT(m);
                   2536:     else {
                   2537:         NEXT(ml) = NEXT(m);
                   2538:         *prest = d;
                   2539:     }
                   2540:     NEXT(m) = 0;
                   2541:     return m;
1.1       noro     2542: }
                   2543:
1.63      noro     2544: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2545: {
1.157     noro     2546:     int msugar,i;
                   2547:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2548:
1.157     noro     2549:     for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2550:         if ( SG(t) < msugar ) msugar = SG(t);
                   2551:     dm0 = 0; dr0 = 0;
                   2552:     for ( i = 0, t = d; t; t = NEXT(t) )
                   2553:         if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2554:             if ( dm0 ) NEXT(dm) = t;
                   2555:             else dm0 = t;
                   2556:             dm = t;
                   2557:             i++;
                   2558:         } else {
                   2559:             if ( dr0 ) NEXT(dr) = t;
                   2560:             else dr0 = t;
                   2561:             dr = t;
                   2562:         }
                   2563:     NEXT(dm) = 0;
                   2564:     if ( dr0 ) NEXT(dr) = 0;
                   2565:     *prest = dr0;
                   2566:     return dm0;
1.63      noro     2567: }
                   2568:
1.77      noro     2569: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2570: {
1.157     noro     2571:     int len;
                   2572:     RHist r;
                   2573:     NDV b;
1.167     noro     2574:     NODE tn;
                   2575:     LIST l;
                   2576:     Q iq;
1.157     noro     2577:
                   2578:     if ( nd_psn == nd_pslen ) {
                   2579:         nd_pslen *= 2;
                   2580:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   2581:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2582:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2583:         nd_bound = (UINT **)
                   2584:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   2585:     }
                   2586:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2587:     nd_ps[nd_psn] = a;
                   2588:     if ( aq ) {
                   2589:         nd_ps_trace[nd_psn] = aq;
                   2590:         register_hcf(aq);
                   2591:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   2592:         SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
                   2593:     } else {
                   2594:         if ( !m ) register_hcf(a);
                   2595:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   2596:         SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
                   2597:     }
                   2598:     if ( nd_demand ) {
                   2599:         if ( aq ) {
                   2600:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2601:             nd_ps_trace[nd_psn] = 0;
                   2602:         } else {
                   2603:             ndv_save(nd_ps[nd_psn],nd_psn);
                   2604:             nd_ps[nd_psn] = 0;
                   2605:         }
                   2606:     }
1.172     noro     2607:     if ( nd_gentrace ) {
1.167     noro     2608:         /* reverse the tracelist and append it to alltracelist */
                   2609:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2610:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2611:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2612:     }
1.157     noro     2613:     return nd_psn++;
1.1       noro     2614: }
                   2615:
1.167     noro     2616: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     2617: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     2618:
1.177     noro     2619: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2620: {
1.157     noro     2621:     int i,j,td,len,max;
1.167     noro     2622:     NODE s,s0,f0,tn;
1.157     noro     2623:     UINT *d;
                   2624:     RHist r;
1.167     noro     2625:     NDVI w;
1.157     noro     2626:     NDV a,am;
1.167     noro     2627:     union oNDC hc;
                   2628:     NODE node;
                   2629:     P hcp;
                   2630:     Q iq,jq,hcq;
                   2631:     LIST l;
1.157     noro     2632:
                   2633:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2634:     /* initialize the tracelist */
                   2635:     nd_tracelist = 0;
1.157     noro     2636:
                   2637:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.167     noro     2638:     w = (NDVI)ALLOCA(nd_psn*sizeof(struct oNDVI));
                   2639:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2640:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2641:     if ( !dont_sort ) {
                   2642:         /* XXX heuristic */
                   2643:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2644:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2645:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     2646:         else
1.167     noro     2647:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2648:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     2649:     }
                   2650:     nd_pslen = 2*nd_psn;
                   2651:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2652:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2653:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   2654:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   2655:     nd_hcf = 0;
                   2656:
                   2657:     if ( trace && nd_vc )
                   2658:         makesubst(nd_vc,&nd_subst);
                   2659:     else
                   2660:         nd_subst = 0;
                   2661:
                   2662:     if ( !nd_red )
                   2663:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   2664:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   2665:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     2666:         hc = HCU(w[i].p);
1.157     noro     2667:         if ( trace ) {
1.167     noro     2668:             a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.157     noro     2669:             if ( !dont_removecont) ndv_removecont(0,a);
                   2670:             register_hcf(a);
                   2671:             am = nd_ps[i] = ndv_dup(mod,a);
                   2672:             ndv_mod(mod,am);
1.177     noro     2673:            if ( DL_COMPARE(HDL(am),HDL(a)) )
                   2674:                return 0;
1.157     noro     2675:             ndv_removecont(mod,am);
                   2676:         } else {
1.167     noro     2677:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.157     noro     2678:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   2679:             if ( !mod ) register_hcf(a);
                   2680:         }
1.172     noro     2681:         if ( nd_gentrace ) {
1.167     noro     2682:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     2683:                        if ( !dont_removecont )
                   2684:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     2685:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   2686:         }
1.157     noro     2687:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   2688:         nd_bound[i] = ndv_compute_bound(a);
                   2689:         nd_psh[i] = r;
                   2690:         if ( nd_demand ) {
                   2691:             if ( trace ) {
                   2692:                 ndv_save(nd_ps_trace[i],i);
                   2693:                 nd_ps_trace[i] = 0;
                   2694:             } else {
                   2695:                 ndv_save(nd_ps[i],i);
                   2696:                 nd_ps[i] = 0;
                   2697:             }
                   2698:         }
                   2699:     }
1.172     noro     2700:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     2701:     return 1;
1.20      noro     2702: }
                   2703:
1.119     noro     2704: struct order_spec *append_block(struct order_spec *spec,
                   2705:     int nv,int nalg,int ord);
                   2706:
1.121     noro     2707: extern VECT current_dl_weight_vector_obj;
                   2708: static VECT prev_weight_vector_obj;
                   2709:
1.120     noro     2710: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     2711:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     2712: {
1.157     noro     2713:     NODE alist,t,s,r0,r,arg;
                   2714:     VL tv;
                   2715:     P poly;
                   2716:     DP d;
                   2717:     Alg alpha,dp;
                   2718:     DAlg inv,da,hc;
                   2719:     MP m;
                   2720:     int i,nvar,nalg,n;
                   2721:     NumberField nf;
                   2722:     LIST f1,f2;
                   2723:     struct order_spec *current_spec;
                   2724:     VECT obj,obj0;
                   2725:     Obj tmp;
                   2726:
                   2727:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   2728:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   2729:
                   2730:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   2731:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   2732:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   2733:         tv->v = tv->v->priv;
                   2734:     }
                   2735:     NEXT(t) = 0;
                   2736:
1.167     noro     2737:     /* simplification, making polynomials monic */
1.157     noro     2738:     setfield_dalg(alist);
                   2739:     obj_algtodalg(f,&f1);
                   2740:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   2741:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   2742:         hc = (DAlg)BDY(d)->c;
                   2743:         if ( NID(hc) == N_DA ) {
                   2744:             invdalg(hc,&inv);
                   2745:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   2746:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   2747:             }
                   2748:         }
                   2749:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   2750:     }
                   2751:     obj_dalgtoalg(f1,&f);
                   2752:
                   2753:     /* append alg vars to the var list */
                   2754:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   2755:     NEXT(tv) = av;
                   2756:
                   2757:     /* append a block to ord */
                   2758:     *ord1p = append_block(ord,nvar,nalg,2);
                   2759:
                   2760:     /* create generator list */
                   2761:     nf = get_numberfield();
                   2762:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   2763:         MKAlg(nf->defpoly[i],dp);
                   2764:         MKNODE(s,dp,t); t = s;
                   2765:     }
                   2766:     MKLIST(f1,t);
                   2767:     *alistp = alist;
                   2768:     algobjtorat(f1,f1p);
                   2769:
                   2770:     /* creating a new weight vector */
                   2771:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   2772:     n = nvar+nalg+1;
                   2773:     MKVECT(obj,n);
                   2774:     if ( obj0 && obj0->len == nvar )
                   2775:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   2776:     else
                   2777:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   2778:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   2779:     BDY(obj)[n-1] = (pointer)ONE;
                   2780:     arg = mknode(1,obj);
                   2781:     Pdp_set_weight(arg,&tmp);
1.121     noro     2782: }
                   2783:
                   2784: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   2785: {
1.157     noro     2786:     NODE s,t,u0,u;
                   2787:     P p;
                   2788:     VL tv;
                   2789:     Obj obj,tmp;
                   2790:     NODE arg;
                   2791:
                   2792:     u0 = 0;
                   2793:     for ( t = r; t; t = NEXT(t) ) {
                   2794:         p = (P)BDY(t);
                   2795:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   2796:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   2797:         }
                   2798:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   2799:             NEXTNODE(u0,u);
                   2800:             BDY(u) = (pointer)p;
                   2801:         }
                   2802:     }
                   2803:     arg = mknode(1,prev_weight_vector_obj);
                   2804:     Pdp_set_weight(arg,&tmp);
1.121     noro     2805:
1.157     noro     2806:     return u0;
1.120     noro     2807: }
                   2808:
1.199     noro     2809: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2810: {
1.157     noro     2811:     VL tv,fv,vv,vc,av;
                   2812:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2813:     int e,max,nvar,i;
                   2814:     NDV b;
1.184     noro     2815:     int ishomo,nalg,mrank,trank,wmax,len;
                   2816:        NMV a;
1.157     noro     2817:     Alg alpha,dp;
                   2818:     P p,zp;
                   2819:     Q dmy;
                   2820:     LIST f1,f2,zpl;
                   2821:     Obj obj;
                   2822:     NumberField nf;
                   2823:     struct order_spec *ord1;
1.170     noro     2824:     NODE tr,tl1,tl2,tl3,tl4;
                   2825:     LIST l1,l2,l3,l4,l5;
1.167     noro     2826:        int j;
1.198     noro     2827:        Q jq,bpe;
1.167     noro     2828:     int *perm;
1.170     noro     2829:     EPOS oepos;
1.194     noro     2830:     int obpe,oadv,ompos,cbpe;
1.1       noro     2831:
1.174     noro     2832:     nd_module = 0;
1.157     noro     2833:     if ( !m && Demand ) nd_demand = 1;
                   2834:     else nd_demand = 0;
1.172     noro     2835:     parse_nd_option(current_option);
1.78      noro     2836:
1.157     noro     2837:     if ( DP_Multiple )
                   2838:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     2839: #if 0
1.157     noro     2840:     ndv_alloc = 0;
1.103     noro     2841: #endif
1.157     noro     2842:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2843:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2844:     switch ( ord->id ) {
                   2845:         case 1:
                   2846:             if ( ord->nv != nvar )
                   2847:                 error("nd_{gr,f4} : invalid order specification");
                   2848:             break;
                   2849:         default:
                   2850:             break;
                   2851:     }
                   2852:     nd_nalg = 0;
                   2853:     av = 0;
                   2854:     if ( !m ) {
                   2855:         get_algtree((Obj)f,&av);
                   2856:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2857:         nd_ntrans = nvar;
                   2858:         nd_nalg = nalg;
                   2859:         /* #i -> t#i */
                   2860:         if ( nalg ) {
                   2861:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2862:             ord = ord1;
                   2863:             f = f1;
                   2864:         }
                   2865:         nvar += nalg;
                   2866:     }
                   2867:     nd_init_ord(ord);
                   2868:     mrank = 0;
1.178     noro     2869:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     2870:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2871:             if ( nd_module ) {
                   2872:                 s = BDY((LIST)BDY(t));
                   2873:                 trank = length(s);
                   2874:                 mrank = MAX(mrank,trank);
                   2875:                 for ( ; s; s = NEXT(s) ) {
                   2876:                     e = getdeg(tv->v,(P)BDY(s));
                   2877:                     max = MAX(e,max);
                   2878:                 }
                   2879:             } else {
                   2880:                 e = getdeg(tv->v,(P)BDY(t));
                   2881:                 max = MAX(e,max);
                   2882:             }
                   2883:         }
                   2884:     nd_setup_parameters(nvar,max);
1.170     noro     2885:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     2886:     ishomo = 1;
                   2887:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     2888:         if ( nd_module ) {
1.172     noro     2889:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     2890:                        else zpl = (LIST)BDY(t);
1.157     noro     2891:             b = (pointer)pltondv(CO,vv,zpl);
                   2892:         } else {
1.172     noro     2893:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     2894:                        else zp = (P)BDY(t);
1.157     noro     2895:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     2896:         }
1.157     noro     2897:         if ( ishomo )
                   2898:             ishomo = ishomo && ndv_ishomo(b);
                   2899:         if ( m ) ndv_mod(m,b);
                   2900:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   2901:     }
                   2902:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     2903:
                   2904:        if ( !ishomo && homo ) {
                   2905:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   2906:             b = (NDV)BDY(t); len = LEN(b);
                   2907:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   2908:                 wmax = MAX(TD(DL(a)),wmax);
                   2909:         }
                   2910:         homogenize_order(ord,nvar,&ord1);
                   2911:         nd_init_ord(ord1);
                   2912:         nd_setup_parameters(nvar+1,wmax);
                   2913:         for ( t = fd0; t; t = NEXT(t) )
                   2914:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   2915:     }
                   2916:
1.187     noro     2917:     ndv_setup(m,0,fd0,nd_gbblock?1:0,0);
1.172     noro     2918:     if ( nd_gentrace ) {
1.167     noro     2919:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   2920:     }
1.186     noro     2921:     x = f4?nd_f4(m,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.192     noro     2922:        if ( !x ) {
                   2923:                *rp = 0; return;
                   2924:        }
1.184     noro     2925:        if ( !ishomo && homo ) {
                   2926:                /* dehomogenization */
                   2927:                for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   2928:                nd_init_ord(ord);
                   2929:                nd_setup_parameters(nvar,0);
                   2930:        }
1.157     noro     2931:     nd_demand = 0;
1.195     noro     2932:        if ( nd_module && nd_intersect ) {
                   2933:                for ( j = nd_psn-1, x = 0; j >= 0; j-- )
                   2934:                        if ( MPOS(DL(nd_psh[j])) > 1 ) {
                   2935:                                MKNODE(xx,(pointer)j,x); x = xx;
                   2936:                        }
                   2937:          conv_ilist(nd_demand,0,x,0);
                   2938:          goto FINAL;
                   2939:        }
1.167     noro     2940:     x = ndv_reducebase(x,perm);
1.172     noro     2941:     if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     2942:     x = ndv_reduceall(m,x);
1.194     noro     2943:     cbpe = nd_bpe;
1.172     noro     2944:     if ( nd_gentrace ) {
1.170     noro     2945:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   2946:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198     noro     2947:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   2948:         if ( nd_gensyz ) {
                   2949:                nd_gb(m,0,1,1,0);
1.170     noro     2950:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   2951:         } else tl4 = 0;
                   2952:     }
1.194     noro     2953:     nd_bpe = cbpe;
                   2954:     nd_setup_parameters(nd_nvar,0);
1.195     noro     2955: FINAL:
1.157     noro     2956:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   2957:         NEXTNODE(r0,r);
1.194     noro     2958:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199     noro     2959:         else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   2960:                else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157     noro     2961:     }
                   2962:     if ( r0 ) NEXT(r) = 0;
                   2963:     if ( nalg )
                   2964:         r0 = postprocess_algcoef(av,alist,r0);
                   2965:     MKLIST(*rp,r0);
1.172     noro     2966:     if ( nd_gentrace ) {
1.167     noro     2967:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.170     noro     2968:        tl3 = reverse_node(tl3);
1.167     noro     2969:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   2970:         for ( t = tl2; t; t = NEXT(t) ) {
                   2971:                        /* s = [i,[*,j,*,*],...] */
                   2972:             s = BDY((LIST)BDY(t));
                   2973:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   2974:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   2975:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   2976:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   2977:             }
                   2978:                }
                   2979:                for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   2980:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   2981:                }
1.198     noro     2982:       MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   2983:       MKLIST(l5,tl4);
                   2984:          STOQ(nd_bpe,bpe);
                   2985:       tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     2986:     }
1.103     noro     2987: #if 0
1.157     noro     2988:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2989: #endif
1.127     noro     2990: }
                   2991:
                   2992: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   2993: {
1.157     noro     2994:     VL tv,fv,vv,vc,av;
                   2995:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2996:     int e,max,nvar,i;
                   2997:     NDV b;
                   2998:     int ishomo,nalg;
                   2999:     Alg alpha,dp;
                   3000:     P p,zp;
                   3001:     Q dmy;
                   3002:     LIST f1,f2;
                   3003:     Obj obj;
                   3004:     NumberField nf;
                   3005:     struct order_spec *ord1;
1.167     noro     3006:     int *perm;
1.157     noro     3007:
1.197     noro     3008:     parse_nd_option(current_option);
1.157     noro     3009:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3010:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3011:     switch ( ord->id ) {
                   3012:         case 1:
                   3013:             if ( ord->nv != nvar )
                   3014:                 error("nd_check : invalid order specification");
                   3015:             break;
                   3016:         default:
                   3017:             break;
                   3018:     }
                   3019:     nd_nalg = 0;
                   3020:     av = 0;
                   3021:     if ( !m ) {
                   3022:         get_algtree((Obj)f,&av);
                   3023:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3024:         nd_ntrans = nvar;
                   3025:         nd_nalg = nalg;
                   3026:         /* #i -> t#i */
                   3027:         if ( nalg ) {
                   3028:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3029:             ord = ord1;
                   3030:             f = f1;
                   3031:         }
                   3032:         nvar += nalg;
                   3033:     }
                   3034:     nd_init_ord(ord);
1.178     noro     3035:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3036:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3037:             e = getdeg(tv->v,(P)BDY(t));
                   3038:             max = MAX(e,max);
                   3039:         }
                   3040:     nd_setup_parameters(nvar,max);
                   3041:     ishomo = 1;
                   3042:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3043:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3044:         b = (pointer)ptondv(CO,vv,zp);
                   3045:         if ( ishomo )
                   3046:             ishomo = ishomo && ndv_ishomo(b);
                   3047:         if ( m ) ndv_mod(m,b);
                   3048:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3049:     }
                   3050:     if ( fd0 ) NEXT(fd) = 0;
                   3051:     ndv_setup(m,0,fd0,0,1);
                   3052:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3053:         x = update_base(x,i);
                   3054:     if ( do_check ) {
1.168     noro     3055:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3056:         if ( !x ) {
                   3057:             *rp = 0;
                   3058:             return;
                   3059:         }
                   3060:     } else {
1.175     noro     3061: #if 0
                   3062:                /* bug ? */
1.157     noro     3063:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3064:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3065: #else
                   3066:                conv_ilist(0,0,x,&perm);
                   3067: #endif
1.157     noro     3068:     }
1.167     noro     3069:     x = ndv_reducebase(x,perm);
1.157     noro     3070:     x = ndv_reduceall(m,x);
                   3071:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3072:         NEXTNODE(r0,r);
                   3073:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3074:     }
                   3075:     if ( r0 ) NEXT(r) = 0;
                   3076:     if ( nalg )
                   3077:         r0 = postprocess_algcoef(av,alist,r0);
                   3078:     MKLIST(*rp,r0);
1.20      noro     3079: }
                   3080:
1.198     noro     3081: NDV recompute_trace(NODE trace,NDV *p,int m);
                   3082: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   3083:
                   3084: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   3085: {
                   3086:        int c,c1,c2,i;
                   3087:        NM mul,m,tail;
                   3088:        ND d,r,rm;
                   3089:        NODE sj;
                   3090:        NDV red;
                   3091:        Obj mj;
                   3092:        static int afo=0;
                   3093:
                   3094:        afo++;
                   3095:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   3096:        CM(mul) = 1;
                   3097:        tail = 0;
                   3098:        for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   3099:                sj = BDY((LIST)BDY(ti));
                   3100:                if ( ARG0(sj) ) {
                   3101:                        red = p[QTOS((Q)ARG1(sj))];
                   3102:                        mj = (Obj)ARG2(sj);
                   3103:                        if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   3104:                        else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   3105:                        rm = ndv_mul_nm(mod,mul,red);
                   3106:                        if ( !r ) r = rm;
                   3107:                        else {
                   3108:                                for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   3109:                                        if ( d ) {
                   3110:                                                NEXT(tail) = m; tail = m; LEN(d)++;
                   3111:                                        } else {
                   3112:                                                MKND(nd_nvar,m,1,d); tail = BDY(d);
                   3113:                                        }
                   3114:                                }
                   3115:                                if ( !m ) return 0; /* failure */
                   3116:                                else {
                   3117:                                        BDY(r) = m;
                   3118:                                        c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   3119:                                        DMAR(c1,c2,0,mod,c);
                   3120:                                        nd_mul_c(mod,rm,c);
                   3121:                                        r = nd_add(mod,r,rm);
                   3122:                                }
                   3123:                        }
                   3124:                }
                   3125:        }
                   3126:        if ( tail ) NEXT(tail) = 0;
                   3127:        d = nd_add(mod,d,r);
                   3128:        nd_mul_c(mod,d,invm(HCM(d),mod));
                   3129:        return ndtondv(mod,d);
                   3130: }
                   3131:
                   3132: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   3133: {
                   3134:     VL tv,fv,vv,vc,av;
                   3135:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3136:     int e,max,nvar,i;
                   3137:     NDV b;
                   3138:     int ishomo,nalg;
                   3139:     Alg alpha,dp;
                   3140:     P p,zp;
                   3141:     Q dmy;
                   3142:     LIST f1,f2;
                   3143:     Obj obj;
                   3144:     NumberField nf;
                   3145:     struct order_spec *ord1;
                   3146:        NODE permtrace,intred,ind,perm,trace,ti;
                   3147:        int len,n,j;
                   3148:        NDV *db,*pb;
                   3149:
                   3150:     parse_nd_option(current_option);
                   3151:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3152:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3153:     switch ( ord->id ) {
                   3154:         case 1:
                   3155:             if ( ord->nv != nvar )
                   3156:                 error("nd_check : invalid order specification");
                   3157:             break;
                   3158:         default:
                   3159:             break;
                   3160:     }
                   3161:     nd_init_ord(ord);
                   3162:        nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   3163:     nd_setup_parameters(nvar,0);
                   3164:
                   3165:        len = length(BDY(f));
                   3166:        db = (NDV *)MALLOC(len*sizeof(NDV *));
                   3167:        for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   3168:            ptozp((P)BDY(t),1,&dmy,&zp);
                   3169:            b = ptondv(CO,vv,zp);
                   3170:         ndv_mod(m,b);
                   3171:                ndv_mul_c(m,b,invm(HCM(b),m));
                   3172:                db[i] = b;
                   3173:     }
                   3174:
                   3175:        permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   3176:        intred = BDY((LIST)ARG3(BDY(tlist)));
                   3177:        ind = BDY((LIST)ARG4(BDY(tlist)));
                   3178:        perm = BDY((LIST)ARG0(permtrace));
                   3179:        trace = NEXT(permtrace);
                   3180:
                   3181:        for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   3182:                j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
                   3183:                if ( j > i ) i = j;
                   3184:        }
                   3185:        n = i+1;
                   3186:        pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   3187:        for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   3188:                ti = BDY((LIST)BDY(t));
                   3189:                pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
                   3190:        }
                   3191:        for ( t = trace; t; t = NEXT(t) ) {
                   3192:                ti = BDY((LIST)BDY(t));
                   3193:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3194:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3195:        if ( DP_Print ) {
                   3196:                        fprintf(asir_out,"."); fflush(asir_out);
                   3197:        }
                   3198:        }
                   3199:        for ( t = intred; t; t = NEXT(t) ) {
                   3200:                ti = BDY((LIST)BDY(t));
                   3201:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3202:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3203:        if ( DP_Print ) {
                   3204:                        fprintf(asir_out,"*"); fflush(asir_out);
                   3205:        }
                   3206:        }
                   3207:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   3208:         NEXTNODE(r0,r);
                   3209:                b = pb[QTOS((Q)BDY(t))];
                   3210:         ndv_mul_c(m,b,invm(HCM(b),m));
                   3211: #if 0
                   3212:         BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
                   3213: #else
                   3214:         BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
                   3215: #endif
                   3216:     }
                   3217:     if ( r0 ) NEXT(r) = 0;
                   3218:     MKLIST(*rp,r0);
                   3219:     if ( DP_Print ) fprintf(asir_out,"\n");
                   3220: }
                   3221:
1.133     noro     3222: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3223: {
1.157     noro     3224:     VL tv,fv,vv,vc,av;
                   3225:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3226:     int m,nocheck,nvar,mindex,e,max;
                   3227:     NDV c;
                   3228:     NMV a;
                   3229:     P p,zp;
                   3230:     Q dmy;
                   3231:     EPOS oepos;
1.164     noro     3232:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3233:     Alg alpha,dp;
                   3234:     P poly;
1.158     noro     3235:     LIST f1,f2,zpl;
1.157     noro     3236:     Obj obj;
                   3237:     NumberField nf;
                   3238:     struct order_spec *ord1;
                   3239:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3240:     NODE tr,tl1,tl2,tl3,tl4;
                   3241:     LIST l1,l2,l3,l4,l5;
1.167     noro     3242:     int *perm;
1.168     noro     3243:     int j,ret;
1.198     noro     3244:     Q jq,bpe;
1.157     noro     3245:
1.167     noro     3246:     nd_module = 0;
1.172     noro     3247:     parse_nd_option(current_option);
1.157     noro     3248:     if ( DP_Multiple )
                   3249:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3250:
                   3251:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3252:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3253:     switch ( ord->id ) {
                   3254:         case 1:
                   3255:             if ( ord->nv != nvar )
                   3256:                 error("nd_gr_trace : invalid order specification");
                   3257:             break;
                   3258:         default:
                   3259:             break;
                   3260:     }
                   3261:
                   3262:     get_algtree((Obj)f,&av);
                   3263:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3264:     nd_ntrans = nvar;
                   3265:     nd_nalg = nalg;
                   3266:     /* #i -> t#i */
                   3267:     if ( nalg ) {
                   3268:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3269:         ord = ord1;
                   3270:         f = f1;
                   3271:     }
                   3272:     nvar += nalg;
                   3273:
                   3274:     nocheck = 0;
                   3275:     mindex = 0;
                   3276:
                   3277:     if ( Demand ) nd_demand = 1;
                   3278:     else nd_demand = 0;
                   3279:
                   3280:     /* setup modulus */
                   3281:     if ( trace < 0 ) {
                   3282:         trace = -trace;
                   3283:         nocheck = 1;
                   3284:     }
                   3285:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3286:     nd_init_ord(ord);
                   3287:     mrank = 0;
1.178     noro     3288:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3289:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3290:             if ( nd_module ) {
                   3291:                 s = BDY((LIST)BDY(t));
                   3292:                 trank = length(s);
                   3293:                 mrank = MAX(mrank,trank);
                   3294:                 for ( ; s; s = NEXT(s) ) {
                   3295:                     e = getdeg(tv->v,(P)BDY(s));
                   3296:                     max = MAX(e,max);
                   3297:                 }
                   3298:             } else {
                   3299:                 e = getdeg(tv->v,(P)BDY(t));
                   3300:                 max = MAX(e,max);
                   3301:             }
1.157     noro     3302:         }
                   3303:     nd_setup_parameters(nvar,max);
1.164     noro     3304:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3305:     ishomo = 1;
                   3306:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3307:         if ( nd_module ) {
1.172     noro     3308:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3309:                        else zpl = (LIST)BDY(t);
1.158     noro     3310:             c = (pointer)pltondv(CO,vv,zpl);
                   3311:         } else {
1.172     noro     3312:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3313:                        else zp = (P)BDY(t);
1.158     noro     3314:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3315:         }
1.157     noro     3316:         if ( ishomo )
                   3317:             ishomo = ishomo && ndv_ishomo(c);
                   3318:         if ( c ) {
                   3319:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3320:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3321:         }
                   3322:     }
                   3323:     if ( in0 ) NEXT(in) = 0;
                   3324:     if ( fd0 ) NEXT(fd) = 0;
                   3325:     if ( !ishomo && homo ) {
                   3326:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3327:             c = (NDV)BDY(t); len = LEN(c);
                   3328:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3329:                 wmax = MAX(TD(DL(a)),wmax);
                   3330:         }
                   3331:         homogenize_order(ord,nvar,&ord1);
                   3332:         nd_init_ord(ord1);
                   3333:         nd_setup_parameters(nvar+1,wmax);
                   3334:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3335:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3336:     }
                   3337:     while ( 1 ) {
1.183     noro     3338:                tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3339:         if ( Demand )
                   3340:             nd_demand = 1;
1.187     noro     3341:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3342:         if ( nd_gentrace ) {
1.167     noro     3343:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3344:         }
1.177     noro     3345:         if ( ret )
                   3346:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3347:         if ( !ret || !cand ) {
1.157     noro     3348:             /* failure */
                   3349:             if ( trace > 1 ) { *rp = 0; return; }
                   3350:             else m = get_lprime(++mindex);
                   3351:             continue;
                   3352:         }
                   3353:         if ( !ishomo && homo ) {
                   3354:             /* dehomogenization */
                   3355:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3356:             nd_init_ord(ord);
                   3357:             nd_setup_parameters(nvar,0);
                   3358:         }
                   3359:         nd_demand = 0;
1.167     noro     3360:         cand = ndv_reducebase(cand,perm);
1.172     noro     3361:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3362:         cand = ndv_reduceall(0,cand);
                   3363:         cbpe = nd_bpe;
1.172     noro     3364:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3365:         get_eg(&eg0);
1.157     noro     3366:         if ( nocheck )
                   3367:             break;
1.170     noro     3368:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3369:             if ( nd_gentrace ) {
1.168     noro     3370:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3371:                    } else tl3 = 0;
                   3372:             /* gbcheck : cand is a GB of Id(cand) ? */
1.172     noro     3373:             ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3374:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3375:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3376:                    } else tl4 = 0;
                   3377:                }
                   3378:                if ( ret ) break;
1.157     noro     3379:         else if ( trace > 1 ) {
                   3380:             /* failure */
                   3381:             *rp = 0; return;
                   3382:         } else {
                   3383:             /* try the next modulus */
                   3384:             m = get_lprime(++mindex);
                   3385:             /* reset the parameters */
                   3386:             if ( !ishomo && homo ) {
                   3387:                 nd_init_ord(ord1);
                   3388:                 nd_setup_parameters(nvar+1,wmax);
                   3389:             } else {
                   3390:                 nd_init_ord(ord);
                   3391:                 nd_setup_parameters(nvar,max);
                   3392:             }
                   3393:         }
                   3394:     }
                   3395:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3396:     if ( DP_Print )
                   3397:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3398:     /* dp->p */
                   3399:     nd_bpe = cbpe;
                   3400:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3401:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3402:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3403:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3404:     }
1.157     noro     3405:     if ( nalg )
                   3406:         cand = postprocess_algcoef(av,alist,cand);
                   3407:     MKLIST(*rp,cand);
1.172     noro     3408:     if ( nd_gentrace ) {
1.167     noro     3409:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3410:                tl3 = reverse_node(tl3);
1.167     noro     3411:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3412:         for ( t = tl2; t; t = NEXT(t) ) {
                   3413:                        /* s = [i,[*,j,*,*],...] */
                   3414:             s = BDY((LIST)BDY(t));
                   3415:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3416:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3417:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3418:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3419:             }
                   3420:                }
                   3421:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3422:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3423:                }
1.168     noro     3424:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3425:                MKLIST(l5,tl4);
1.198     noro     3426:                STOQ(nd_bpe,bpe);
                   3427:         tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     3428:     }
1.157     noro     3429: }
1.52      noro     3430:
1.157     noro     3431: /* XXX : module element is not considered  */
1.1       noro     3432:
1.61      noro     3433: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3434: {
1.157     noro     3435:     UINT *d;
                   3436:     int i,j,l,s,ord_l;
                   3437:     struct order_pair *op;
                   3438:
                   3439:     d = dl->d;
                   3440:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3441:     if ( nd_blockmask ) {
                   3442:         l = nd_blockmask->n;
                   3443:         op = nd_blockmask->order_pair;
                   3444:         for ( j = 0, s = 0; j < l; j++ ) {
                   3445:             ord_l = op[j].length;
                   3446:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3447:         }
                   3448:         TD(r) = ndl_weight(r);
                   3449:         ndl_weight_mask(r);
                   3450:     } else {
                   3451:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3452:         TD(r) = ndl_weight(r);
                   3453:     }
1.1       noro     3454: }
                   3455:
1.61      noro     3456: DL ndltodl(int n,UINT *ndl)
1.1       noro     3457: {
1.157     noro     3458:     DL dl;
                   3459:     int *d;
                   3460:     int i,j,l,s,ord_l;
                   3461:     struct order_pair *op;
                   3462:
                   3463:     NEWDL(dl,n);
                   3464:     dl->td = TD(ndl);
                   3465:     d = dl->d;
                   3466:     if ( nd_blockmask ) {
                   3467:         l = nd_blockmask->n;
                   3468:         op = nd_blockmask->order_pair;
                   3469:         for ( j = 0, s = 0; j < l; j++ ) {
                   3470:             ord_l = op[j].length;
                   3471:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3472:         }
                   3473:     } else {
                   3474:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3475:     }
                   3476:     return dl;
1.1       noro     3477: }
                   3478:
1.167     noro     3479: void nmtodp(int mod,NM m,DP *r)
                   3480: {
                   3481:     DP dp;
                   3482:     MP mr;
                   3483:
                   3484:     NEWMP(mr);
                   3485:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3486:     mr->c = ndctop(mod,m->c);
                   3487:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3488:     *r = dp;
                   3489: }
                   3490:
1.61      noro     3491: void ndl_print(UINT *dl)
1.1       noro     3492: {
1.157     noro     3493:     int n;
                   3494:     int i,j,l,ord_l,s,s0;
                   3495:     struct order_pair *op;
                   3496:
                   3497:     n = nd_nvar;
                   3498:     printf("<<");
                   3499:     if ( nd_blockmask ) {
                   3500:         l = nd_blockmask->n;
                   3501:         op = nd_blockmask->order_pair;
                   3502:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3503:             ord_l = op[j].length;
                   3504:             for ( i = 0; i < ord_l; i++, s++ )
                   3505:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3506:         }
                   3507:     } else {
                   3508:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3509:     }
                   3510:     printf(">>");
                   3511:     if ( MPOS(dl) )
                   3512:         printf("*e%d",MPOS(dl));
1.1       noro     3513: }
                   3514:
                   3515: void nd_print(ND p)
                   3516: {
1.157     noro     3517:     NM m;
1.1       noro     3518:
1.157     noro     3519:     if ( !p )
                   3520:         printf("0\n");
                   3521:     else {
                   3522:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3523:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3524:             else printf("+%d*",CM(m));
                   3525:             ndl_print(DL(m));
                   3526:         }
                   3527:         printf("\n");
                   3528:     }
1.1       noro     3529: }
                   3530:
1.113     noro     3531: void nd_print_q(ND p)
1.16      noro     3532: {
1.157     noro     3533:     NM m;
1.16      noro     3534:
1.157     noro     3535:     if ( !p )
                   3536:         printf("0\n");
                   3537:     else {
                   3538:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3539:             printf("+");
                   3540:             printexpr(CO,(Obj)CQ(m));
                   3541:             printf("*");
                   3542:             ndl_print(DL(m));
                   3543:         }
                   3544:         printf("\n");
                   3545:     }
1.16      noro     3546: }
                   3547:
1.1       noro     3548: void ndp_print(ND_pairs d)
                   3549: {
1.157     noro     3550:     ND_pairs t;
1.1       noro     3551:
1.157     noro     3552:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3553:     printf("\n");
1.1       noro     3554: }
                   3555:
1.20      noro     3556: void nd_removecont(int mod,ND p)
1.16      noro     3557: {
1.157     noro     3558:     int i,n;
                   3559:     Q *w;
                   3560:     Q dvr,t;
                   3561:     NM m;
                   3562:     struct oVECT v;
                   3563:     N q,r;
                   3564:
                   3565:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   3566:     else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   3567:     else {
                   3568:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3569:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3570:         v.len = n;
                   3571:         v.body = (pointer *)w;
                   3572:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3573:         removecont_array((P *)w,n,1);
                   3574:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3575:     }
1.16      noro     3576: }
                   3577:
1.21      noro     3578: void nd_removecont2(ND p1,ND p2)
                   3579: {
1.157     noro     3580:     int i,n1,n2,n;
                   3581:     Q *w;
                   3582:     Q dvr,t;
                   3583:     NM m;
                   3584:     struct oVECT v;
                   3585:     N q,r;
                   3586:
                   3587:     n1 = nd_length(p1);
                   3588:     n2 = nd_length(p2);
                   3589:     n = n1+n2;
                   3590:     w = (Q *)ALLOCA(n*sizeof(Q));
                   3591:     v.len = n;
                   3592:     v.body = (pointer *)w;
                   3593:     i = 0;
                   3594:     if ( p1 )
                   3595:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3596:     if ( p2 )
                   3597:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3598:     removecont_array((P *)w,n,1);
                   3599:     i = 0;
                   3600:     if ( p1 )
                   3601:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3602:     if ( p2 )
                   3603:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     3604: }
                   3605:
1.20      noro     3606: void ndv_removecont(int mod,NDV p)
1.16      noro     3607: {
1.157     noro     3608:     int i,len,all_p;
                   3609:     Q *c;
                   3610:     P *w;
                   3611:     Q dvr,t;
                   3612:     P g,cont,tp;
                   3613:     NMV m;
                   3614:
                   3615:     if ( mod == -1 )
                   3616:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   3617:     else if ( mod )
                   3618:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   3619:     else {
                   3620:         len = p->len;
                   3621:         w = (P *)ALLOCA(len*sizeof(P));
                   3622:         c = (Q *)ALLOCA(len*sizeof(Q));
                   3623:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   3624:             ptozp(CP(m),1,&c[i],&w[i]);
                   3625:             all_p = all_p && !NUM(w[i]);
                   3626:         }
                   3627:         if ( all_p ) {
                   3628:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   3629:             mulp(nd_vc,(P)dvr,g,&cont);
                   3630:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3631:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   3632:             }
                   3633:         } else {
                   3634:             sortbynm((Q *)c,len);
                   3635:             qltozl((Q *)c,len,&dvr);
                   3636:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3637:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   3638:             }
                   3639:         }
                   3640:     }
1.21      noro     3641: }
                   3642:
1.157     noro     3643: /* koko */
                   3644:
1.164     noro     3645: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     3646: {
1.157     noro     3647:     int len,i,max;
                   3648:     NMV m,mr0,mr,t;
1.61      noro     3649:
1.157     noro     3650:     len = p->len;
1.178     noro     3651:     for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157     noro     3652:         max = MAX(max,TD(DL(m)));
                   3653:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3654:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   3655:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3656:     t = (NMV)ALLOCA(nmv_adv);
                   3657:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     3658:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     3659:         CQ(mr) = CQ(m);
                   3660:         ndl_copy(DL(t),DL(mr));
                   3661:     }
                   3662:     NV(p)++;
                   3663:     BDY(p) = mr0;
1.61      noro     3664: }
                   3665:
1.45      noro     3666: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     3667: {
1.164     noro     3668:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     3669:     int pos;
1.157     noro     3670:     Q *w;
                   3671:     Q dvr,t;
                   3672:     NMV m,r;
                   3673:
                   3674:     len = p->len;
                   3675:     newnvar = nd_nvar-1;
                   3676:     newexporigin = nd_get_exporigin(ord);
1.167     noro     3677:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     3678:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   3679:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   3680:         ndl_dehomogenize(DL(m));
                   3681:     if ( newwpd != nd_wpd ) {
                   3682:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   3683:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   3684:             CQ(r) = CQ(m);
1.167     noro     3685:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     3686:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   3687:             adj = nd_exporigin-newexporigin;
                   3688:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     3689:             if ( nd_module ) {
                   3690:                 DL(r)[newmpos] = pos;
                   3691:             }
1.157     noro     3692:         }
                   3693:     }
                   3694:     NV(p)--;
1.23      noro     3695: }
                   3696:
1.150     noro     3697: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   3698: {
1.157     noro     3699:     int i;
                   3700:     P *tpl,*tpl1;
                   3701:     NODE l;
                   3702:     P h,gcd,t;
                   3703:
                   3704:     tpl = (P *)ALLOCA(m*sizeof(P));
                   3705:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   3706:     bcopy(pl,tpl,m*sizeof(P));
                   3707:     gcd = (P)ONE;
                   3708:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   3709:         h = (P)BDY(l);
                   3710:         while ( 1 ) {
                   3711:             for ( i = 0; i < m; i++ )
                   3712:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   3713:                     break;
                   3714:             if ( i == m ) {
                   3715:                 bcopy(tpl1,tpl,m*sizeof(P));
                   3716:                 mulp(vl,gcd,h,&t); gcd = t;
                   3717:             } else
                   3718:                 break;
                   3719:         }
                   3720:     }
                   3721:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   3722:     if ( full ) {
                   3723:         heu_nezgcdnpz(vl,tpl,m,&t);
                   3724:         mulp(vl,gcd,t,pr);
                   3725:     } else
                   3726:         *pr = gcd;
1.150     noro     3727: }
                   3728:
                   3729: void removecont_array(P *p,int n,int full)
1.146     noro     3730: {
1.157     noro     3731:     int all_p,all_q,i;
                   3732:     Q *c;
                   3733:     P *w;
                   3734:     P t,s;
                   3735:
                   3736:     for ( all_q = 1, i = 0; i < n; i++ )
                   3737:         all_q = all_q && NUM(p[i]);
                   3738:     if ( all_q ) {
                   3739:         removecont_array_q((Q *)p,n);
                   3740:     } else {
                   3741:         c = (Q *)ALLOCA(n*sizeof(Q));
                   3742:         w = (P *)ALLOCA(n*sizeof(P));
                   3743:         for ( i = 0; i < n; i++ ) {
                   3744:             ptozp(p[i],1,&c[i],&w[i]);
                   3745:         }
                   3746:         removecont_array_q(c,n);
                   3747:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   3748:         for ( i = 0; i < n; i++ ) {
                   3749:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   3750:         }
                   3751:     }
1.146     noro     3752: }
                   3753:
                   3754: void removecont_array_q(Q *c,int n)
1.21      noro     3755: {
1.157     noro     3756:     struct oVECT v;
                   3757:     Q d0,d1,a,u,u1,gcd;
                   3758:     int i,j;
                   3759:     N qn,rn,gn;
                   3760:     Q *q,*r;
                   3761:
                   3762:     q = (Q *)ALLOCA(n*sizeof(Q));
                   3763:     r = (Q *)ALLOCA(n*sizeof(Q));
                   3764:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   3765:     igcdv_estimate(&v,&d0);
                   3766:     for ( i = 0; i < n; i++ ) {
                   3767:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   3768:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   3769:         NTOQ(rn,SGN(c[i]),r[i]);
                   3770:     }
                   3771:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   3772:     if ( i < n ) {
                   3773:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   3774:         igcdv(&v,&d1);
                   3775:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   3776:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   3777:         for ( i = 0; i < n; i++ ) {
                   3778:             mulq(a,q[i],&u);
                   3779:             if ( r[i] ) {
                   3780:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   3781:                 addq(u,u1,&q[i]);
                   3782:             } else
                   3783:                 q[i] = u;
                   3784:         }
                   3785:     }
                   3786:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     3787: }
                   3788:
1.19      noro     3789: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     3790: {
1.157     noro     3791:     NM m;
                   3792:     int c,c1;
1.1       noro     3793:
1.157     noro     3794:     if ( !p ) return;
                   3795:     if ( mul == 1 ) return;
                   3796:     if ( mod == -1 )
                   3797:         for ( m = BDY(p); m; m = NEXT(m) )
                   3798:             CM(m) = _mulsf(CM(m),mul);
                   3799:     else
                   3800:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3801:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3802:         }
1.1       noro     3803: }
                   3804:
1.146     noro     3805: void nd_mul_c_q(ND p,P mul)
1.16      noro     3806: {
1.157     noro     3807:     NM m;
                   3808:     P c;
1.16      noro     3809:
1.157     noro     3810:     if ( !p ) return;
                   3811:     if ( UNIQ(mul) ) return;
                   3812:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3813:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   3814:     }
1.16      noro     3815: }
                   3816:
1.61      noro     3817: void nd_mul_c_p(VL vl,ND p,P mul)
                   3818: {
1.157     noro     3819:     NM m;
                   3820:     P c;
1.61      noro     3821:
1.157     noro     3822:     if ( !p ) return;
                   3823:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3824:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3825:     }
1.61      noro     3826: }
                   3827:
1.1       noro     3828: void nd_free(ND p)
                   3829: {
1.157     noro     3830:     NM t,s;
1.1       noro     3831:
1.157     noro     3832:     if ( !p ) return;
                   3833:     t = BDY(p);
                   3834:     while ( t ) {
                   3835:         s = NEXT(t);
                   3836:         FREENM(t);
                   3837:         t = s;
                   3838:     }
                   3839:     FREEND(p);
1.1       noro     3840: }
                   3841:
1.23      noro     3842: void ndv_free(NDV p)
                   3843: {
1.200   ! noro     3844:     GCFREE(BDY(p));
1.23      noro     3845: }
                   3846:
1.61      noro     3847: void nd_append_red(UINT *d,int i)
1.1       noro     3848: {
1.157     noro     3849:     RHist m,m0;
                   3850:     int h;
1.1       noro     3851:
1.157     noro     3852:     NEWRHist(m);
                   3853:     h = ndl_hash_value(d);
                   3854:     m->index = i;
                   3855:     ndl_copy(d,DL(m));
                   3856:     NEXT(m) = nd_red[h];
                   3857:     nd_red[h] = m;
1.1       noro     3858: }
                   3859:
1.61      noro     3860: UINT *ndv_compute_bound(NDV p)
1.1       noro     3861: {
1.157     noro     3862:     UINT *d1,*d2,*t;
                   3863:     UINT u;
                   3864:     int i,j,k,l,len,ind;
                   3865:     NMV m;
                   3866:
                   3867:     if ( !p )
                   3868:         return 0;
                   3869:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3870:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3871:     len = LEN(p);
                   3872:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3873:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     3874:         ndl_max(DL(m),d1,d2);
1.157     noro     3875:         t = d1; d1 = d2; d2 = t;
                   3876:     }
                   3877:     l = nd_nvar+31;
                   3878:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3879:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3880:         u = d1[i];
                   3881:         k = (nd_epw-1)*nd_bpe;
                   3882:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3883:             t[ind] = (u>>k)&nd_mask0;
                   3884:     }
                   3885:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3886:     return t;
1.1       noro     3887: }
                   3888:
1.99      noro     3889: UINT *nd_compute_bound(ND p)
                   3890: {
1.157     noro     3891:     UINT *d1,*d2,*t;
                   3892:     UINT u;
                   3893:     int i,j,k,l,len,ind;
                   3894:     NM m;
                   3895:
                   3896:     if ( !p )
                   3897:         return 0;
                   3898:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3899:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3900:     len = LEN(p);
                   3901:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   3902:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   3903:         ndl_lcm(DL(m),d1,d2);
                   3904:         t = d1; d1 = d2; d2 = t;
                   3905:     }
                   3906:     l = nd_nvar+31;
                   3907:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3908:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3909:         u = d1[i];
                   3910:         k = (nd_epw-1)*nd_bpe;
                   3911:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3912:             t[ind] = (u>>k)&nd_mask0;
                   3913:     }
                   3914:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3915:     return t;
1.99      noro     3916: }
                   3917:
1.157     noro     3918: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   3919: /* of a term. In this case we need additional 1 word. */
                   3920:
1.48      noro     3921: int nd_get_exporigin(struct order_spec *ord)
                   3922: {
1.157     noro     3923:     switch ( ord->id ) {
                   3924:         case 0: case 2: case 256: case 258:
                   3925:             return 1+nd_module;
                   3926:         case 1: case 257:
                   3927:             /* block order */
                   3928:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   3929:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   3930:             return ord->ord.block.length+1+nd_module;
                   3931:         case 3: case 259:
                   3932:             error("nd_get_exporigin : composite order is not supported yet.");
                   3933:     }
1.48      noro     3934: }
                   3935:
1.61      noro     3936: void nd_setup_parameters(int nvar,int max) {
1.157     noro     3937:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   3938:     struct order_pair *op;
1.48      noro     3939:
1.157     noro     3940:     nd_nvar = nvar;
                   3941:     if ( max ) {
                   3942:         /* XXX */
                   3943:         if ( do_weyl ) nd_bpe = 32;
                   3944:         else if ( max < 2 ) nd_bpe = 1;
                   3945:         else if ( max < 4 ) nd_bpe = 2;
                   3946:         else if ( max < 8 ) nd_bpe = 3;
                   3947:         else if ( max < 16 ) nd_bpe = 4;
                   3948:         else if ( max < 32 ) nd_bpe = 5;
                   3949:         else if ( max < 64 ) nd_bpe = 6;
                   3950:         else if ( max < 256 ) nd_bpe = 8;
                   3951:         else if ( max < 1024 ) nd_bpe = 10;
                   3952:         else if ( max < 65536 ) nd_bpe = 16;
                   3953:         else nd_bpe = 32;
                   3954:     }
                   3955:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   3956:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3957:     nd_exporigin = nd_get_exporigin(nd_ord);
                   3958:     wpd = nd_exporigin+elen;
                   3959:     if ( nd_module )
                   3960:         nd_mpos = nd_exporigin-1;
                   3961:     else
                   3962:         nd_mpos = -1;
                   3963:     if ( wpd != nd_wpd ) {
                   3964:         nd_free_private_storage();
                   3965:         nd_wpd = wpd;
                   3966:     }
                   3967:     if ( nd_bpe < 32 ) {
                   3968:         nd_mask0 = (1<<nd_bpe)-1;
                   3969:     } else {
                   3970:         nd_mask0 = 0xffffffff;
                   3971:     }
                   3972:     bzero(nd_mask,sizeof(nd_mask));
                   3973:     nd_mask1 = 0;
                   3974:     for ( i = 0; i < nd_epw; i++ ) {
                   3975:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3976:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3977:     }
                   3978:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   3979:     nd_epos = nd_create_epos(nd_ord);
                   3980:     nd_blockmask = nd_create_blockmask(nd_ord);
                   3981:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3982: }
                   3983:
1.103     noro     3984: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3985: {
1.157     noro     3986:     int i,obpe,oadv,h;
                   3987:     static NM prev_nm_free_list;
                   3988:     static ND_pairs prev_ndp_free_list;
                   3989:     RHist mr0,mr;
                   3990:     RHist r;
                   3991:     RHist *old_red;
                   3992:     ND_pairs s0,s,t;
                   3993:     EPOS oepos;
                   3994:
                   3995:     obpe = nd_bpe;
                   3996:     oadv = nmv_adv;
                   3997:     oepos = nd_epos;
                   3998:     if ( obpe < 2 ) nd_bpe = 2;
                   3999:     else if ( obpe < 3 ) nd_bpe = 3;
                   4000:     else if ( obpe < 4 ) nd_bpe = 4;
                   4001:     else if ( obpe < 5 ) nd_bpe = 5;
                   4002:     else if ( obpe < 6 ) nd_bpe = 6;
                   4003:     else if ( obpe < 8 ) nd_bpe = 8;
                   4004:     else if ( obpe < 10 ) nd_bpe = 10;
                   4005:     else if ( obpe < 16 ) nd_bpe = 16;
                   4006:     else if ( obpe < 32 ) nd_bpe = 32;
                   4007:     else error("nd_reconstruct : exponent too large");
                   4008:
                   4009:     nd_setup_parameters(nd_nvar,0);
                   4010:     prev_nm_free_list = _nm_free_list;
                   4011:     prev_ndp_free_list = _ndp_free_list;
                   4012:     _nm_free_list = 0;
                   4013:     _ndp_free_list = 0;
                   4014:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4015:     if ( trace )
                   4016:         for ( i = nd_psn-1; i >= 0; i-- )
                   4017:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   4018:     s0 = 0;
                   4019:     for ( t = d; t; t = NEXT(t) ) {
                   4020:         NEXTND_pairs(s0,s);
                   4021:         s->i1 = t->i1;
                   4022:         s->i2 = t->i2;
                   4023:         SG(s) = SG(t);
                   4024:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4025:     }
                   4026:
                   4027:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   4028:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4029:         old_red[i] = nd_red[i];
                   4030:         nd_red[i] = 0;
                   4031:     }
                   4032:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4033:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4034:             NEWRHist(mr);
                   4035:             mr->index = r->index;
                   4036:             SG(mr) = SG(r);
                   4037:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4038:             h = ndl_hash_value(DL(mr));
                   4039:             NEXT(mr) = nd_red[h];
                   4040:             nd_red[h] = mr;
                   4041:         }
                   4042:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4043:     old_red = 0;
                   4044:     for ( i = 0; i < nd_psn; i++ ) {
                   4045:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4046:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4047:         nd_psh[i] = r;
                   4048:     }
                   4049:     if ( s0 ) NEXT(s) = 0;
                   4050:     prev_nm_free_list = 0;
                   4051:     prev_ndp_free_list = 0;
1.71      noro     4052: #if 0
1.157     noro     4053:     GC_gcollect();
1.71      noro     4054: #endif
1.157     noro     4055:     return s0;
1.1       noro     4056: }
                   4057:
1.61      noro     4058: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4059: {
1.157     noro     4060:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4061:     struct order_pair *op;
1.1       noro     4062:
1.157     noro     4063:     n = nd_nvar;
                   4064:     oepw = (sizeof(UINT)*8)/obpe;
                   4065:     omask0 = (1<<obpe)-1;
                   4066:     TD(r) = TD(d);
                   4067:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4068:     if ( nd_blockmask ) {
                   4069:         l = nd_blockmask->n;
                   4070:         op = nd_blockmask->order_pair;
                   4071:         for ( i = 1; i < nd_exporigin; i++ )
                   4072:             r[i] = d[i];
                   4073:         for ( j = 0, s = 0; j < l; j++ ) {
                   4074:             ord_l = op[j].length;
                   4075:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4076:                 ei =  GET_EXP_OLD(d,s);
                   4077:                 PUT_EXP(r,s,ei);
                   4078:             }
                   4079:         }
                   4080:     } else {
                   4081:         for ( i = 0; i < n; i++ ) {
                   4082:             ei = GET_EXP_OLD(d,i);
                   4083:             PUT_EXP(r,i,ei);
                   4084:         }
                   4085:     }
                   4086:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4087: }
1.3       noro     4088:
1.6       noro     4089: ND nd_copy(ND p)
                   4090: {
1.157     noro     4091:     NM m,mr,mr0;
                   4092:     int c,n;
                   4093:     ND r;
                   4094:
                   4095:     if ( !p )
                   4096:         return 0;
                   4097:     else {
                   4098:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4099:             NEXTNM(mr0,mr);
                   4100:             CM(mr) = CM(m);
                   4101:             ndl_copy(DL(m),DL(mr));
                   4102:         }
                   4103:         NEXT(mr) = 0;
                   4104:         MKND(NV(p),mr0,LEN(p),r);
                   4105:         SG(r) = SG(p);
                   4106:         return r;
                   4107:     }
1.6       noro     4108: }
                   4109:
1.53      noro     4110: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4111: {
1.157     noro     4112:     NM m1,m2;
                   4113:     NDV p1,p2;
                   4114:     ND t1,t2;
                   4115:     UINT *lcm;
                   4116:     P gp,tp;
1.167     noro     4117:     Q g,t,iq;
1.157     noro     4118:     int td;
1.167     noro     4119:     LIST hist;
                   4120:     NODE node;
                   4121:     DP d;
1.157     noro     4122:
                   4123:     if ( !mod && nd_demand ) {
                   4124:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4125:     } else {
                   4126:         if ( trace ) {
                   4127:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4128:         } else {
                   4129:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4130:         }
                   4131:     }
                   4132:     lcm = LCM(p);
                   4133:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4134:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4135:         FREENM(m1); return 0;
                   4136:     }
                   4137:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4138:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4139:         FREENM(m1); FREENM(m2); return 0;
                   4140:     }
                   4141:
                   4142:     if ( mod == -1 ) {
                   4143:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   4144:     } else if ( mod ) {
                   4145:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   4146:     } else if ( nd_vc ) {
                   4147:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4148:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4149:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4150:     } else {
                   4151:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4152:     }
                   4153:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4154:     *rp = nd_add(mod,t1,t2);
1.172     noro     4155:     if ( nd_gentrace ) {
1.167     noro     4156:         /* nd_tracelist is initialized */
                   4157:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4158:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4159:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4160:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4161:         nd_tracelist = node;
                   4162:     }
1.157     noro     4163:     FREENM(m1); FREENM(m2);
                   4164:     return 1;
1.11      noro     4165: }
                   4166:
1.19      noro     4167: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4168: {
1.157     noro     4169:     NMV m;
                   4170:     int c,c1,len,i;
1.11      noro     4171:
1.157     noro     4172:     if ( !p ) return;
                   4173:     len = LEN(p);
                   4174:     if ( mod == -1 )
                   4175:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4176:             CM(m) = _mulsf(CM(m),mul);
                   4177:     else
                   4178:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4179:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4180:         }
1.11      noro     4181: }
                   4182:
1.113     noro     4183: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4184: {
1.157     noro     4185:     NMV m;
                   4186:     Q c;
                   4187:     int len,i;
                   4188:
                   4189:     if ( !p ) return;
                   4190:     len = LEN(p);
                   4191:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4192:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4193:     }
1.16      noro     4194: }
                   4195:
1.55      noro     4196: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4197:     int n2,i,j,l,n,tlen;
                   4198:     UINT *d0;
                   4199:     NM *tab,*psum;
                   4200:     ND s,r;
                   4201:     NM t;
                   4202:     NMV m1;
                   4203:
                   4204:     if ( !p ) return 0;
                   4205:     n = NV(p); n2 = n>>1;
                   4206:     d0 = DL(m0);
                   4207:     l = LEN(p);
                   4208:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4209:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4210:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4211:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4212:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4213:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4214:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4215:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4216:         for ( j = 0; j < tlen; j++ ) {
                   4217:             if ( tab[j] ) {
                   4218:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4219:             }
                   4220:         }
                   4221:     }
                   4222:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4223:         if ( psum[i] ) {
                   4224:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4225:             MKND(n,psum[i],j,s);
                   4226:             r = nd_add(mod,r,s);
                   4227:         }
                   4228:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4229:     return r;
1.55      noro     4230: }
                   4231:
1.56      noro     4232: /* product of monomials */
                   4233: /* XXX block order is not handled correctly */
                   4234:
1.55      noro     4235: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4236: {
1.157     noro     4237:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4238:     UINT *d0,*d1,*d,*dt,*ctab;
                   4239:     Q *ctab_q;
                   4240:     Q q,q1;
                   4241:     UINT c0,c1,c;
                   4242:     NM *p;
                   4243:     NM m,t;
                   4244:     int mpos;
                   4245:
                   4246:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4247:     if ( !m0 || !m1 ) return;
                   4248:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4249:     if ( nd_module )
                   4250:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4251:
1.157     noro     4252:     NEWNM(m); d = DL(m);
                   4253:     if ( mod ) {
                   4254:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4255:     } else if ( nd_vc )
                   4256:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4257:        else
1.157     noro     4258:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4259:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4260:     homo = n&1 ? 1 : 0;
                   4261:     if ( homo ) {
                   4262:         /* offset of h-degree */
                   4263:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4264:         PUT_EXP(DL(m),n-1,h);
                   4265:         TD(DL(m)) = h;
                   4266:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4267:     }
                   4268:     tab[0] = m;
                   4269:     NEWNM(m); d = DL(m);
                   4270:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4271:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4272:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4273:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4274:         a += l; b += k;
                   4275:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4276:         if ( !k || !l ) {
                   4277:             for ( j = 0; j < curlen; j++ )
                   4278:                 if ( t = tab[j] ) {
                   4279:                     dt = DL(t);
                   4280:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4281:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4282:                 }
                   4283:             curlen *= k+1;
                   4284:             continue;
                   4285:         }
                   4286:         min = MIN(k,l);
                   4287:         if ( mod ) {
                   4288:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4289:             mkwcm(k,l,mod,ctab);
                   4290:         } else {
                   4291:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4292:             mkwc(k,l,ctab_q);
                   4293:         }
                   4294:         for ( j = min; j >= 0; j-- ) {
                   4295:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4296:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4297:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4298:             if ( homo ) {
                   4299:                 TD(d) = s;
                   4300:                 PUT_EXP(d,n-1,s-h);
                   4301:             } else TD(d) = h;
                   4302:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4303:             if ( mod ) c = ctab[j];
                   4304:             else q = ctab_q[j];
                   4305:             p = tab+curlen*j;
                   4306:             if ( j == 0 ) {
                   4307:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4308:                     if ( tab[u] ) {
                   4309:                         ndl_addto(DL(tab[u]),d);
                   4310:                         if ( mod ) {
                   4311:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4312:                         } else if ( nd_vc )
                   4313:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4314:                                                else {
1.157     noro     4315:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4316:                         }
                   4317:                     }
                   4318:                 }
                   4319:             } else {
                   4320:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4321:                     if ( tab[u] ) {
                   4322:                         NEWNM(t);
                   4323:                         ndl_add(DL(tab[u]),d,DL(t));
                   4324:                         if ( mod ) {
                   4325:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4326:                         } else if ( nd_vc )
1.180     noro     4327:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4328:                                                else
1.157     noro     4329:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4330:                         *p = t;
                   4331:                     }
                   4332:                 }
                   4333:             }
                   4334:         }
                   4335:         curlen *= k+1;
                   4336:     }
                   4337:     FREENM(m);
1.167     noro     4338:     if ( nd_module ) {
1.166     noro     4339:         mpos = MPOS(d1);
1.167     noro     4340:         for ( i = 0; i < tlen; i++ )
                   4341:             if ( tab[i] ) {
                   4342:                 d = DL(tab[i]);
                   4343:                 MPOS(d) = mpos;
                   4344:                 TD(d) = ndl_weight(d);
                   4345:             }
                   4346:     }
1.55      noro     4347: }
                   4348:
1.63      noro     4349: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4350: {
1.157     noro     4351:     NM mr,mr0;
                   4352:     NMV m;
                   4353:     UINT *d,*dt,*dm;
                   4354:     int c,n,td,i,c1,c2,len;
                   4355:     Q q;
                   4356:     ND r;
                   4357:
                   4358:     if ( !p ) return 0;
                   4359:     else {
                   4360:         n = NV(p); m = BDY(p);
                   4361:         d = DL(m0);
                   4362:         len = LEN(p);
                   4363:         mr0 = 0;
                   4364:         td = TD(d);
                   4365:         c = CM(m0);
                   4366:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4367:             NEXTNM(mr0,mr);
                   4368:             CM(mr) = 1;
                   4369:             ndl_add(DL(m),d,DL(mr));
                   4370:         }
                   4371:         NEXT(mr) = 0;
                   4372:         MKND(NV(p),mr0,len,r);
                   4373:         SG(r) = SG(p) + TD(d);
                   4374:         return r;
                   4375:     }
1.63      noro     4376: }
                   4377:
1.55      noro     4378: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4379: {
1.157     noro     4380:     NM mr,mr0;
                   4381:     NMV m;
                   4382:     UINT *d,*dt,*dm;
                   4383:     int c,n,td,i,c1,c2,len;
                   4384:     P q;
                   4385:     ND r;
                   4386:
                   4387:     if ( !p ) return 0;
                   4388:     else if ( do_weyl )
                   4389:         if ( mod == -1 )
                   4390:             error("ndv_mul_nm : not implemented (weyl)");
                   4391:         else
                   4392:             return weyl_ndv_mul_nm(mod,m0,p);
                   4393:     else {
                   4394:         n = NV(p); m = BDY(p);
                   4395:         d = DL(m0);
                   4396:         len = LEN(p);
                   4397:         mr0 = 0;
                   4398:         td = TD(d);
                   4399:         if ( mod == -1 ) {
                   4400:             c = CM(m0);
                   4401:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4402:                 NEXTNM(mr0,mr);
                   4403:                 CM(mr) = _mulsf(CM(m),c);
                   4404:                 ndl_add(DL(m),d,DL(mr));
                   4405:             }
                   4406:         } else if ( mod ) {
                   4407:             c = CM(m0);
                   4408:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4409:                 NEXTNM(mr0,mr);
                   4410:                 c1 = CM(m);
                   4411:                 DMAR(c1,c,0,mod,c2);
                   4412:                 CM(mr) = c2;
                   4413:                 ndl_add(DL(m),d,DL(mr));
                   4414:             }
                   4415:         } else {
                   4416:             q = CP(m0);
                   4417:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4418:                 NEXTNM(mr0,mr);
                   4419:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4420:                 ndl_add(DL(m),d,DL(mr));
                   4421:             }
                   4422:         }
                   4423:         NEXT(mr) = 0;
                   4424:         MKND(NV(p),mr0,len,r);
                   4425:         SG(r) = SG(p) + TD(d);
                   4426:         return r;
                   4427:     }
1.4       noro     4428: }
                   4429:
1.104     noro     4430: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4431: {
1.157     noro     4432:     NM mq0,mq;
                   4433:     NMV tm;
                   4434:     Q q;
                   4435:     int i,nv,sg,c,c1,c2,hindex;
                   4436:     ND p,t,r;
                   4437:     N tnm;
                   4438:
                   4439:     if ( bucket->m < 0 ) return 0;
                   4440:     else {
                   4441:         nv = NV(d);
                   4442:         mq0 = 0;
                   4443:         tm = (NMV)ALLOCA(nmv_adv);
                   4444:         while ( 1 ) {
                   4445:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4446:             if ( hindex < 0 ) break;
                   4447:             p = bucket->body[hindex];
                   4448:             NEXTNM(mq0,mq);
                   4449:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4450:             ndl_copy(DL(mq),DL(tm));
                   4451:             if ( mod ) {
                   4452:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4453:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4454:                 CM(tm) = mod-c;
                   4455:             } else {
                   4456:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4457:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4458:                 chsgnq(CQ(mq),&CQ(tm));
                   4459:             }
                   4460:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4461:             bucket->body[hindex] = nd_remove_head(p);
                   4462:             t = nd_remove_head(t);
                   4463:             add_pbucket(mod,bucket,t);
                   4464:         }
                   4465:         if ( !mq0 )
                   4466:             r = 0;
                   4467:         else {
                   4468:             NEXT(mq) = 0;
                   4469:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4470:             MKND(nv,mq0,i,r);
                   4471:             /* XXX */
                   4472:             SG(r) = HTD(r);
                   4473:         }
                   4474:         return r;
                   4475:     }
1.99      noro     4476: }
                   4477:
1.43      noro     4478: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4479: {
1.157     noro     4480:     NMV m,mr,mr0,t;
                   4481:     int len,i,k;
1.11      noro     4482:
1.157     noro     4483:     if ( !p ) return;
                   4484:     m = BDY(p); len = LEN(p);
                   4485:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4486:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4487:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4488:     t = (NMV)ALLOCA(nmv_adv);
                   4489:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4490:         CQ(t) = CQ(m);
                   4491:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4492:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4493:         CQ(mr) = CQ(t);
                   4494:         ndl_copy(DL(t),DL(mr));
                   4495:     }
                   4496:     BDY(p) = mr0;
1.61      noro     4497: }
                   4498:
                   4499: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4500: {
1.157     noro     4501:     NMV m,mr,mr0;
                   4502:     int len,i;
                   4503:     NDV r;
                   4504:
                   4505:     if ( !p ) return 0;
                   4506:     m = BDY(p); len = LEN(p);
                   4507:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4508:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4509:         ndl_zero(DL(mr));
                   4510:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4511:         CQ(mr) = CQ(m);
                   4512:     }
                   4513:     MKNDV(NV(p),mr0,len,r);
                   4514:     SG(r) = SG(p);
                   4515:     return r;
1.11      noro     4516: }
                   4517:
1.61      noro     4518: /* duplicate p */
                   4519:
                   4520: NDV ndv_dup(int mod,NDV p)
1.3       noro     4521: {
1.157     noro     4522:     NDV d;
                   4523:     NMV t,m,m0;
                   4524:     int i,len;
                   4525:
                   4526:     if ( !p ) return 0;
                   4527:     len = LEN(p);
                   4528:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4529:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4530:         ndl_copy(DL(t),DL(m));
                   4531:         CQ(m) = CQ(t);
                   4532:     }
                   4533:     MKNDV(NV(p),m0,len,d);
                   4534:     SG(d) = SG(p);
                   4535:     return d;
1.23      noro     4536: }
                   4537:
1.63      noro     4538: ND nd_dup(ND p)
                   4539: {
1.157     noro     4540:     ND d;
                   4541:     NM t,m,m0;
1.63      noro     4542:
1.157     noro     4543:     if ( !p ) return 0;
                   4544:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4545:         NEXTNM(m0,m);
                   4546:         ndl_copy(DL(t),DL(m));
                   4547:         CQ(m) = CQ(t);
                   4548:     }
                   4549:     if ( m0 ) NEXT(m) = 0;
                   4550:     MKND(NV(p),m0,LEN(p),d);
                   4551:     SG(d) = SG(p);
                   4552:     return d;
1.63      noro     4553: }
                   4554:
1.61      noro     4555: /* XXX if p->len == 0 then it represents 0 */
                   4556:
                   4557: void ndv_mod(int mod,NDV p)
                   4558: {
1.157     noro     4559:     NMV t,d;
                   4560:     int r,s,u;
                   4561:     int i,len,dlen;
                   4562:     P cp;
                   4563:     Q c;
                   4564:     Obj gfs;
                   4565:
                   4566:     if ( !p ) return;
                   4567:     len = LEN(p);
                   4568:     dlen = 0;
                   4569:     if ( mod == -1 )
                   4570:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4571:             simp_ff((Obj)CP(t),&gfs);
                   4572:             r = FTOIF(CONT((GFS)gfs));
                   4573:             CM(d) = r;
                   4574:             ndl_copy(DL(t),DL(d));
                   4575:             NMV_ADV(d);
                   4576:             dlen++;
                   4577:         }
                   4578:     else
                   4579:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4580:             if ( nd_vc ) {
                   4581:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4582:                 c = (Q)cp;
                   4583:             } else
                   4584:                 c = CQ(t);
                   4585:             r = rem(NM(c),mod);
                   4586:             if ( r ) {
                   4587:                 if ( SGN(c) < 0 )
                   4588:                     r = mod-r;
                   4589:                 if ( DN(c) ) {
                   4590:                     s = rem(DN(c),mod);
                   4591:                     if ( !s )
                   4592:                         error("ndv_mod : division by 0");
                   4593:                     s = invm(s,mod);
                   4594:                     DMAR(r,s,0,mod,u); r = u;
                   4595:                 }
                   4596:                 CM(d) = r;
                   4597:                 ndl_copy(DL(t),DL(d));
                   4598:                 NMV_ADV(d);
                   4599:                 dlen++;
                   4600:             }
                   4601:         }
                   4602:     LEN(p) = dlen;
1.61      noro     4603: }
                   4604:
                   4605: NDV ptondv(VL vl,VL dvl,P p)
                   4606: {
1.157     noro     4607:     ND nd;
                   4608:
                   4609:     nd = ptond(vl,dvl,p);
                   4610:     return ndtondv(0,nd);
                   4611: }
1.61      noro     4612:
1.157     noro     4613: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4614: {
                   4615:     NODE nd,nd1;
                   4616:     int n;
                   4617:     P *pl;
                   4618:     Q *cl;
                   4619:     int i;
                   4620:     P dmy;
                   4621:     Q dvr;
                   4622:     LIST r;
                   4623:
                   4624:     nd = BDY(l); n = length(nd);
                   4625:     pl = (P *)ALLOCA(n*sizeof(P));
                   4626:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4627:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4628:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4629:     qltozl(cl,n,&dvr);
                   4630:     nd = BDY(l);
                   4631:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4632:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4633:     }
                   4634:     nd = 0;
                   4635:     for ( i = n-1; i >= 0; i-- ) {
                   4636:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4637:     }
                   4638:     MKLIST(r,nd);
                   4639:     *pp = r;
                   4640: }
                   4641:
                   4642: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4643:
                   4644: NDV pltondv(VL vl,VL dvl,LIST p)
                   4645: {
                   4646:     int i;
                   4647:     NODE t;
                   4648:     ND r,ri;
                   4649:     NM m;
                   4650:
                   4651:     if ( !nd_module ) error("pltond : module order must be set");
                   4652:     r = 0;
                   4653:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4654:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4655:         if ( ri )
                   4656:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4657:                 MPOS(DL(m)) = i;
                   4658:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4659:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4660:             }
1.157     noro     4661:         r = nd_add(0,r,ri);
                   4662:     }
                   4663:     return ndtondv(0,r);
1.61      noro     4664: }
                   4665:
                   4666: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4667: {
1.157     noro     4668:     int n,i,j,k,e;
                   4669:     VL tvl;
                   4670:     V v;
                   4671:     DCP dc;
                   4672:     DCP *w;
                   4673:     ND r,s,t,u;
                   4674:     P x;
                   4675:     int c;
                   4676:     UINT *d;
                   4677:     NM m,m0;
                   4678:
                   4679:     if ( !p )
                   4680:         return 0;
                   4681:     else if ( NUM(p) ) {
                   4682:         NEWNM(m);
                   4683:         ndl_zero(DL(m));
                   4684:         CQ(m) = (Q)p;
                   4685:         NEXT(m) = 0;
                   4686:         MKND(nd_nvar,m,1,r);
                   4687:         SG(r) = 0;
                   4688:         return r;
                   4689:     } else {
                   4690:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4691:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4692:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4693:         for ( i = 0, tvl = dvl, v = VR(p);
                   4694:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4695:         if ( !tvl ) {
                   4696:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4697:                 t = ptond(vl,dvl,COEF(w[j]));
                   4698:                 pwrp(vl,x,DEG(w[j]),&p);
                   4699:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4700:             }
                   4701:             return s;
                   4702:         } else {
                   4703:             NEWNM(m0); d = DL(m0);
                   4704:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4705:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4706:                 TD(d) = MUL_WEIGHT(e,i);
                   4707:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4708:                 if ( nd_module ) MPOS(d) = 0;
                   4709:                 t = ptond(vl,dvl,COEF(w[j]));
                   4710:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4711:                     ndl_addto(DL(m),d);
                   4712:                 SG(t) += TD(d);
                   4713:                 s = nd_add(0,s,t);
                   4714:             }
                   4715:             FREENM(m0);
                   4716:             return s;
                   4717:         }
                   4718:     }
1.61      noro     4719: }
                   4720:
                   4721: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4722: {
1.157     noro     4723:     VL tvl;
                   4724:     int len,n,j,i,e;
                   4725:     NMV m;
                   4726:     Q q;
                   4727:     P c;
                   4728:     UINT *d;
                   4729:     P s,r,u,t,w;
                   4730:     GFS gfs;
                   4731:
                   4732:     if ( !p ) return 0;
                   4733:     else {
                   4734:         len = LEN(p);
                   4735:         n = NV(p);
                   4736:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4737:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4738:             if ( mod == -1 ) {
                   4739:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4740:             } else if ( mod ) {
                   4741:                 STOQ(CM(m),q); c = (P)q;
                   4742:             } else
                   4743:                 c = CP(m);
                   4744:             d = DL(m);
                   4745:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4746:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4747:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4748:             }
                   4749:             addp(vl,s,t,&u); s = u;
                   4750:         }
                   4751:         return s;
                   4752:     }
                   4753: }
                   4754:
                   4755: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4756: {
                   4757:     VL tvl;
                   4758:     int len,n,j,i,e;
                   4759:     NMV m;
                   4760:     Q q;
                   4761:     P c;
                   4762:     UINT *d;
                   4763:     P s,r,u,t,w;
                   4764:     GFS gfs;
                   4765:     P *a;
                   4766:     LIST l;
                   4767:     NODE nd,nd1;
                   4768:
                   4769:     if ( !p ) return 0;
                   4770:     else {
                   4771:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4772:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4773:         len = LEN(p);
                   4774:         n = NV(p);
                   4775:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4776:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4777:             if ( mod == -1 ) {
                   4778:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4779:             } else if ( mod ) {
                   4780:                 STOQ(CM(m),q); c = (P)q;
                   4781:             } else
                   4782:                 c = CP(m);
                   4783:             d = DL(m);
                   4784:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4785:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4786:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4787:             }
                   4788:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4789:         }
                   4790:         nd = 0;
                   4791:         for ( i = rank; i > 0; i-- ) {
                   4792:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4793:         }
                   4794:         MKLIST(l,nd);
                   4795:         return l;
                   4796:     }
1.3       noro     4797: }
                   4798:
1.61      noro     4799: NDV ndtondv(int mod,ND p)
1.11      noro     4800: {
1.157     noro     4801:     NDV d;
                   4802:     NMV m,m0;
                   4803:     NM t;
                   4804:     int i,len;
                   4805:
                   4806:     if ( !p ) return 0;
                   4807:     len = LEN(p);
                   4808:     if ( mod )
1.200   ! noro     4809:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     4810:     else
                   4811:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4812: #if 0
1.157     noro     4813:     ndv_alloc += nmv_adv*len;
1.103     noro     4814: #endif
1.157     noro     4815:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4816:         ndl_copy(DL(t),DL(m));
                   4817:         CQ(m) = CQ(t);
                   4818:     }
                   4819:     MKNDV(NV(p),m0,len,d);
                   4820:     SG(d) = SG(p);
                   4821:     return d;
1.11      noro     4822: }
                   4823:
1.61      noro     4824: ND ndvtond(int mod,NDV p)
1.11      noro     4825: {
1.157     noro     4826:     ND d;
                   4827:     NM m,m0;
                   4828:     NMV t;
                   4829:     int i,len;
                   4830:
                   4831:     if ( !p ) return 0;
                   4832:     m0 = 0;
                   4833:     len = p->len;
                   4834:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4835:         NEXTNM(m0,m);
                   4836:         ndl_copy(DL(t),DL(m));
                   4837:         CQ(m) = CQ(t);
                   4838:     }
                   4839:     NEXT(m) = 0;
                   4840:     MKND(NV(p),m0,len,d);
                   4841:     SG(d) = SG(p);
                   4842:     return d;
1.11      noro     4843: }
                   4844:
1.198     noro     4845: DP ndvtodp(int mod,NDV p)
                   4846: {
                   4847:     MP m,m0;
                   4848:        DP d;
                   4849:     NMV t;
                   4850:     int i,len;
                   4851:
                   4852:     if ( !p ) return 0;
                   4853:     m0 = 0;
                   4854:     len = p->len;
                   4855:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4856:         NEXTMP(m0,m);
                   4857:        m->dl = ndltodl(nd_nvar,DL(t));
                   4858:        m->c = ndctop(mod,t->c);
                   4859:     }
                   4860:     NEXT(m) = 0;
                   4861:        MKDP(nd_nvar,m0,d);
                   4862:     SG(d) = SG(p);
                   4863:     return d;
                   4864: }
                   4865:
1.3       noro     4866: void ndv_print(NDV p)
                   4867: {
1.157     noro     4868:     NMV m;
                   4869:     int i,len;
1.3       noro     4870:
1.157     noro     4871:     if ( !p ) printf("0\n");
                   4872:     else {
                   4873:         len = LEN(p);
                   4874:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4875:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   4876:             else printf("+%d*",CM(m));
                   4877:             ndl_print(DL(m));
                   4878:         }
                   4879:         printf("\n");
                   4880:     }
1.16      noro     4881: }
                   4882:
1.113     noro     4883: void ndv_print_q(NDV p)
1.16      noro     4884: {
1.157     noro     4885:     NMV m;
                   4886:     int i,len;
1.16      noro     4887:
1.157     noro     4888:     if ( !p ) printf("0\n");
                   4889:     else {
                   4890:         len = LEN(p);
                   4891:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4892:             printf("+");
                   4893:             printexpr(CO,(Obj)CQ(m));
                   4894:             printf("*");
                   4895:             ndl_print(DL(m));
                   4896:         }
                   4897:         printf("\n");
                   4898:     }
1.25      noro     4899: }
                   4900:
1.167     noro     4901: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     4902: {
1.157     noro     4903:     int len,i,j;
1.167     noro     4904:     NDVI w;
1.157     noro     4905:     NODE t,t0;
                   4906:
                   4907:     len = length(x);
1.167     noro     4908:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   4909:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   4910:         w[i].p = BDY(t); w[i].i = perm[i];
                   4911:     }
1.157     noro     4912:     for ( i = 0; i < len; i++ ) {
                   4913:         for ( j = 0; j < i; j++ ) {
1.167     noro     4914:             if ( w[i].p && w[j].p )
                   4915:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   4916:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     4917:         }
                   4918:     }
1.167     noro     4919:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   4920:         if ( w[i].p ) {
                   4921:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   4922:             perm[j++] = w[i].i;
                   4923:         }
1.157     noro     4924:     }
                   4925:     NEXT(t) = 0; x = t0;
                   4926:     return x;
1.11      noro     4927: }
1.32      noro     4928:
1.43      noro     4929: /* XXX incomplete */
                   4930:
1.32      noro     4931: void nd_init_ord(struct order_spec *ord)
                   4932: {
1.157     noro     4933:     nd_module = (ord->id >= 256);
                   4934:     switch ( ord->id ) {
                   4935:         case 0:
                   4936:             switch ( ord->ord.simple ) {
                   4937:                 case 0:
                   4938:                     nd_dcomp = 1;
                   4939:                     nd_isrlex = 1;
                   4940:                     break;
                   4941:                 case 1:
                   4942:                     nd_dcomp = 1;
                   4943:                     nd_isrlex = 0;
                   4944:                     break;
                   4945:                 case 2:
                   4946:                     nd_dcomp = 0;
                   4947:                     nd_isrlex = 0;
                   4948:                     ndl_compare_function = ndl_lex_compare;
                   4949:                     break;
                   4950:                 case 11:
                   4951:                     /* XXX */
                   4952:                     nd_dcomp = 0;
                   4953:                     nd_isrlex = 1;
                   4954:                     ndl_compare_function = ndl_ww_lex_compare;
                   4955:                     break;
                   4956:                 default:
                   4957:                     error("nd_gr : unsupported order");
                   4958:             }
                   4959:             break;
                   4960:         case 1:
                   4961:             /* block order */
                   4962:             /* XXX */
                   4963:             nd_dcomp = -1;
                   4964:             nd_isrlex = 0;
                   4965:             ndl_compare_function = ndl_block_compare;
                   4966:             break;
                   4967:         case 2:
                   4968:             /* matrix order */
                   4969:             /* XXX */
                   4970:             nd_dcomp = -1;
                   4971:             nd_isrlex = 0;
                   4972:             nd_matrix_len = ord->ord.matrix.row;
                   4973:             nd_matrix = ord->ord.matrix.matrix;
                   4974:             ndl_compare_function = ndl_matrix_compare;
                   4975:             break;
                   4976:         case 3:
                   4977:             /* composite order */
                   4978:             nd_dcomp = -1;
                   4979:             nd_isrlex = 0;
                   4980:             nd_worb_len = ord->ord.composite.length;
                   4981:             nd_worb = ord->ord.composite.w_or_b;
                   4982:             ndl_compare_function = ndl_composite_compare;
                   4983:             break;
                   4984:
                   4985:         /* module order */
                   4986:         case 256:
1.160     noro     4987:             nd_ispot = ord->ispot;
1.174     noro     4988:             nd_pot_nelim = ord->pot_nelim;
1.157     noro     4989:             nd_dcomp = -1;
                   4990:             switch ( ord->ord.simple ) {
                   4991:                 case 0:
1.167     noro     4992:                     nd_isrlex = 1;
1.157     noro     4993:                     ndl_compare_function = ndl_module_grlex_compare;
                   4994:                     break;
                   4995:                 case 1:
1.167     noro     4996:                     nd_isrlex = 0;
1.157     noro     4997:                     ndl_compare_function = ndl_module_glex_compare;
                   4998:                     break;
                   4999:                 case 2:
1.167     noro     5000:                     nd_isrlex = 0;
1.157     noro     5001:                     ndl_compare_function = ndl_module_lex_compare;
                   5002:                     break;
                   5003:                 default:
                   5004:                     error("nd_gr : unsupported order");
                   5005:             }
                   5006:             break;
                   5007:         case 257:
                   5008:             /* block order */
1.174     noro     5009:             nd_ispot = ord->ispot;
                   5010:             nd_pot_nelim = ord->pot_nelim;
                   5011:             nd_dcomp = -1;
                   5012:             nd_isrlex = 0;
1.157     noro     5013:             ndl_compare_function = ndl_module_block_compare;
                   5014:             break;
                   5015:         case 258:
                   5016:             /* matrix order */
1.174     noro     5017:             nd_ispot = ord->ispot;
                   5018:             nd_pot_nelim = ord->pot_nelim;
                   5019:             nd_dcomp = -1;
                   5020:             nd_isrlex = 0;
1.157     noro     5021:             nd_matrix_len = ord->ord.matrix.row;
                   5022:             nd_matrix = ord->ord.matrix.matrix;
                   5023:             ndl_compare_function = ndl_module_matrix_compare;
                   5024:             break;
                   5025:         case 259:
                   5026:             /* composite order */
1.174     noro     5027:             nd_ispot = ord->ispot;
                   5028:             nd_pot_nelim = ord->pot_nelim;
                   5029:             nd_dcomp = -1;
                   5030:             nd_isrlex = 0;
1.157     noro     5031:             nd_worb_len = ord->ord.composite.length;
                   5032:             nd_worb = ord->ord.composite.w_or_b;
                   5033:             ndl_compare_function = ndl_module_composite_compare;
                   5034:             break;
                   5035:     }
                   5036:     nd_ord = ord;
1.32      noro     5037: }
                   5038:
1.43      noro     5039: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5040: {
1.157     noro     5041:     int n,i,j,s,l;
                   5042:     UINT *t;
                   5043:     BlockMask bm;
                   5044:
                   5045:     /* we only create mask table for block order */
1.164     noro     5046:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5047:         return 0;
                   5048:     n = ord->ord.block.length;
                   5049:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5050:     bm->n = n;
                   5051:     bm->order_pair = ord->ord.block.order_pair;
                   5052:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5053:     for ( i = 0, s = 0; i < n; i++ ) {
                   5054:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5055:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5056:         l = bm->order_pair[i].length;
                   5057:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5058:     }
                   5059:     return bm;
1.57      noro     5060: }
                   5061:
                   5062: EPOS nd_create_epos(struct order_spec *ord)
                   5063: {
1.157     noro     5064:     int i,j,l,s,ord_l,ord_o;
                   5065:     EPOS epos;
                   5066:     struct order_pair *op;
                   5067:
                   5068:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5069:     switch ( ord->id ) {
1.164     noro     5070:         case 0: case 256:
1.157     noro     5071:             if ( nd_isrlex ) {
                   5072:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5073:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5074:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5075:                 }
                   5076:             } else {
                   5077:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5078:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5079:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5080:                 }
                   5081:             }
                   5082:             break;
1.164     noro     5083:         case 1: case 257:
1.157     noro     5084:             /* block order */
                   5085:             l = ord->ord.block.length;
                   5086:             op = ord->ord.block.order_pair;
                   5087:             for ( j = 0, s = 0; j < l; j++ ) {
                   5088:                 ord_o = op[j].order;
                   5089:                 ord_l = op[j].length;
                   5090:                 if ( !ord_o )
                   5091:                     for ( i = 0; i < ord_l; i++ ) {
                   5092:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5093:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5094:                     }
                   5095:                 else
                   5096:                     for ( i = 0; i < ord_l; i++ ) {
                   5097:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5098:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5099:                     }
                   5100:                 s += ord_l;
                   5101:             }
                   5102:             break;
                   5103:         case 2:
                   5104:             /* matrix order */
                   5105:         case 3:
                   5106:             /* composite order */
1.167     noro     5107:         default:
1.157     noro     5108:             for ( i = 0; i < nd_nvar; i++ ) {
                   5109:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5110:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5111:             }
                   5112:             break;
                   5113:     }
                   5114:     return epos;
1.43      noro     5115: }
1.59      noro     5116:
                   5117: /* external interface */
                   5118:
1.191     noro     5119: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5120: {
1.157     noro     5121:     NODE t,in0,in;
1.191     noro     5122:     ND ndf,nf;
                   5123:     NDV ndvf;
1.157     noro     5124:     VL vv,tv;
1.191     noro     5125:     int stat,nvar,max,mrank;
1.157     noro     5126:     union oNDC dn;
                   5127:     Q cont;
                   5128:     P pp;
1.191     noro     5129:     LIST ppl;
1.157     noro     5130:
                   5131:     if ( !f ) {
                   5132:         *rp = 0;
                   5133:         return;
                   5134:     }
                   5135:     pltovl(v,&vv);
                   5136:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5137:
1.191     noro     5138:     /* max=65536 implies nd_bpe=32 */
                   5139:     max = 65536;
1.157     noro     5140:
1.191     noro     5141:        nd_module = 0;
                   5142:        /* nd_module will be set if ord is a module ordering */
1.157     noro     5143:     nd_init_ord(ord);
                   5144:     nd_setup_parameters(nvar,max);
1.191     noro     5145:     if ( nd_module && OID(f) != O_LIST )
                   5146:         error("nd_nf_p : the first argument must be a list");
                   5147:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5148:     /* conversion to ndv */
                   5149:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5150:         NEXTNODE(in0,in);
1.191     noro     5151:         if ( nd_module ) {
                   5152:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5153:                || length(BDY((LIST)BDY(t))) != mrank )
                   5154:               error("nd_nf_p : inconsistent basis element");
                   5155:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5156:           else ppl = (LIST)BDY(t);
                   5157:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5158:         } else {
                   5159:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5160:           else pp = (P)BDY(t);
                   5161:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5162:         }
1.157     noro     5163:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5164:     }
1.191     noro     5165:     if ( in0 ) NEXT(in) = 0;
                   5166:
                   5167:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5168:     else ndvf = ptondv(CO,vv,(P)f);
                   5169:     if ( m ) ndv_mod(m,ndvf);
                   5170:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5171:
                   5172:     /* dont sort, dont removecont */
                   5173:     ndv_setup(m,0,in0,1,1);
                   5174:     nd_scale=2;
1.191     noro     5175:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5176:     if ( !stat )
                   5177:         error("nd_nf_p : exponent too large");
                   5178:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5179:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5180: }
                   5181:
                   5182: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5183: {
1.157     noro     5184:     NM m;
                   5185:     UINT *t,*s;
                   5186:     int i;
                   5187:
                   5188:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5189:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5190:         t = DL(m);
                   5191:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5192:         r[i] = CM(m);
                   5193:     }
                   5194:     for ( i = 0; !r[i]; i++ );
                   5195:     return i;
1.63      noro     5196: }
                   5197:
1.113     noro     5198: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5199: {
1.157     noro     5200:     NM m;
                   5201:     UINT *t,*s;
                   5202:     int i;
                   5203:
                   5204:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5205:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5206:         t = DL(m);
                   5207:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5208:         r[i] = CQ(m);
                   5209:     }
                   5210:     for ( i = 0; !r[i]; i++ );
                   5211:     return i;
1.74      noro     5212: }
                   5213:
1.129     noro     5214: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5215: {
1.157     noro     5216:     NM m;
                   5217:     NMV mr;
                   5218:     UINT *d,*t,*s;
                   5219:     NDV p;
                   5220:     int i,j,len;
                   5221:     Q *r;
                   5222:
                   5223:     m = pair->mul;
                   5224:     d = DL(m);
                   5225:     p = nd_ps[pair->index];
                   5226:     len = LEN(p);
                   5227:     r = (Q *)CALLOC(n,sizeof(Q));
                   5228:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5229:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5230:         ndl_add(d,DL(mr),t);
                   5231:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5232:         r[i] = CQ(mr);
                   5233:     }
                   5234:     return r;
1.129     noro     5235: }
                   5236:
1.67      noro     5237: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     5238: {
1.157     noro     5239:     NM m;
                   5240:     NMV mr;
                   5241:     UINT *d,*t,*s;
                   5242:     NDV p;
                   5243:     unsigned char *ivc;
                   5244:     unsigned short *ivs;
                   5245:     UINT *v,*ivi,*s0v;
                   5246:     int i,j,len,prev,diff,cdiff;
                   5247:     IndArray r;
1.198     noro     5248: struct oEGT eg0,eg1;
1.157     noro     5249:
                   5250:     m = pair->mul;
                   5251:     d = DL(m);
                   5252:     p = nd_ps[pair->index];
                   5253:     len = LEN(p);
                   5254:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5255:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.198     noro     5256: get_eg(&eg0);
1.157     noro     5257:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5258:         ndl_add(d,DL(mr),t);
                   5259:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5260:         v[j] = i;
                   5261:     }
1.198     noro     5262: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5263:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5264:     r->head = v[0];
                   5265:     diff = 0;
                   5266:     for ( i = 1; i < len; i++ ) {
                   5267:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5268:     }
                   5269:     if ( diff < 256 ) {
                   5270:         r->width = 1;
                   5271:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5272:         r->index.c = ivc;
                   5273:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5274:     } else if ( diff < 65536 ) {
                   5275:         r->width = 2;
                   5276:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5277:         r->index.s = ivs;
                   5278:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5279:     } else {
                   5280:         r->width = 4;
                   5281:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5282:         r->index.i = ivi;
                   5283:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5284:     }
                   5285:     return r;
1.64      noro     5286: }
                   5287:
1.135     noro     5288: int compress_array(Q *svect,Q *cvect,int n)
                   5289: {
1.157     noro     5290:     int i,j;
1.135     noro     5291:
1.157     noro     5292:     for ( i = j = 0; i < n; i++ )
                   5293:         if ( svect[i] ) cvect[j++] = svect[i];
                   5294:     return j;
1.135     noro     5295: }
                   5296:
                   5297: void expand_array(Q *svect,Q *cvect,int n)
                   5298: {
1.157     noro     5299:     int i,j;
1.135     noro     5300:
1.157     noro     5301:     for ( i = j = 0; j < n;  i++  )
                   5302:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5303: }
                   5304:
1.133     noro     5305: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5306: {
1.157     noro     5307:     int i,j,k,len,pos,prev,nz;
                   5308:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5309:     IndArray ivect;
                   5310:     unsigned char *ivc;
                   5311:     unsigned short *ivs;
                   5312:     unsigned int *ivi;
                   5313:     NDV redv;
                   5314:     NMV mr;
                   5315:     NODE rp;
                   5316:     int maxrs;
                   5317:     double hmag;
                   5318:     Q *cvect;
                   5319:
                   5320:     maxrs = 0;
                   5321:     for ( i = 0; i < col && !svect[i]; i++ );
                   5322:     if ( i == col ) return maxrs;
                   5323:     hmag = p_mag((P)svect[i])*nd_scale;
                   5324:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5325:     for ( i = 0; i < nred; i++ ) {
                   5326:         ivect = imat[i];
                   5327:         k = ivect->head;
                   5328:         if ( svect[k] ) {
                   5329:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5330:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5331:             len = LEN(redv); mr = BDY(redv);
                   5332:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5333:             chsgnq(cs,&mcs);
                   5334:             if ( !UNIQ(cr) ) {
                   5335:                 for ( j = 0; j < col; j++ ) {
                   5336:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5337:                 }
                   5338:             }
                   5339:             svect[k] = 0; prev = k;
                   5340:             switch ( ivect->width ) {
                   5341:                 case 1:
                   5342:                     ivc = ivect->index.c;
                   5343:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5344:                         pos = prev+ivc[j]; prev = pos;
                   5345:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5346:                     }
                   5347:                     break;
                   5348:                 case 2:
                   5349:                     ivs = ivect->index.s;
                   5350:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5351:                         pos = prev+ivs[j]; prev = pos;
                   5352:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5353:                     }
                   5354:                     break;
                   5355:                 case 4:
                   5356:                     ivi = ivect->index.i;
                   5357:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5358:                         pos = prev+ivi[j]; prev = pos;
                   5359:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5360:                     }
                   5361:                     break;
                   5362:             }
                   5363:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5364:             if ( j == col ) break;
                   5365:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5366:                 nz = compress_array(svect,cvect,col);
                   5367:                 removecont_array((P *)cvect,nz,1);
                   5368:                 expand_array(svect,cvect,nz);
                   5369:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5370:             }
                   5371:         }
                   5372:     }
                   5373:     nz = compress_array(svect,cvect,col);
                   5374:     removecont_array((P *)cvect,nz,1);
                   5375:     expand_array(svect,cvect,nz);
                   5376:     if ( DP_Print ) {
                   5377:         fprintf(asir_out,"-"); fflush(asir_out);
                   5378:     }
                   5379:     return maxrs;
1.107     noro     5380: }
                   5381:
1.76      noro     5382: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5383: {
1.157     noro     5384:     int i,j,k,len,pos,prev;
                   5385:     UINT c,c1,c2,c3,up,lo,dmy;
                   5386:     IndArray ivect;
                   5387:     unsigned char *ivc;
                   5388:     unsigned short *ivs;
                   5389:     unsigned int *ivi;
                   5390:     NDV redv;
                   5391:     NMV mr;
                   5392:     NODE rp;
                   5393:     int maxrs;
                   5394:
                   5395:     maxrs = 0;
                   5396:     for ( i = 0; i < nred; i++ ) {
                   5397:         ivect = imat[i];
                   5398:         k = ivect->head; svect[k] %= m;
                   5399:         if ( c = svect[k] ) {
                   5400:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5401:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5402:             len = LEN(redv); mr = BDY(redv);
                   5403:             svect[k] = 0; prev = k;
                   5404:             switch ( ivect->width ) {
                   5405:                 case 1:
                   5406:                     ivc = ivect->index.c;
                   5407:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5408:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5409:                         prev = pos;
                   5410:                         DMA(c1,c,c2,up,lo);
                   5411:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5412:                         } else svect[pos] = lo;
                   5413:                     }
                   5414:                     break;
                   5415:                 case 2:
                   5416:                     ivs = ivect->index.s;
                   5417:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5418:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5419:                         prev = pos;
                   5420:                         DMA(c1,c,c2,up,lo);
                   5421:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5422:                         } else svect[pos] = lo;
                   5423:                     }
                   5424:                     break;
                   5425:                 case 4:
                   5426:                     ivi = ivect->index.i;
                   5427:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5428:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5429:                         prev = pos;
                   5430:                         DMA(c1,c,c2,up,lo);
                   5431:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5432:                         } else svect[pos] = lo;
                   5433:                     }
                   5434:                     break;
                   5435:             }
                   5436:         }
                   5437:     }
                   5438:     for ( i = 0; i < col; i++ )
                   5439:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5440:     return maxrs;
1.65      noro     5441: }
                   5442:
1.76      noro     5443: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5444: {
1.157     noro     5445:     int i,j,k,len,pos,prev;
                   5446:     UINT c,c1,c2,c3,up,lo,dmy;
                   5447:     IndArray ivect;
                   5448:     unsigned char *ivc;
                   5449:     unsigned short *ivs;
                   5450:     unsigned int *ivi;
                   5451:     NDV redv;
                   5452:     NMV mr;
                   5453:     NODE rp;
                   5454:     int maxrs;
                   5455:
                   5456:     maxrs = 0;
                   5457:     for ( i = 0; i < nred; i++ ) {
                   5458:         ivect = imat[i];
                   5459:         k = ivect->head; svect[k] %= m;
                   5460:         if ( c = svect[k] ) {
                   5461:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5462:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5463:             len = LEN(redv); mr = BDY(redv);
                   5464:             svect[k] = 0; prev = k;
                   5465:             switch ( ivect->width ) {
                   5466:                 case 1:
                   5467:                     ivc = ivect->index.c;
                   5468:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5469:                         pos = prev+ivc[j]; prev = pos;
                   5470:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5471:                     }
                   5472:                     break;
                   5473:                 case 2:
                   5474:                     ivs = ivect->index.s;
                   5475:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5476:                         pos = prev+ivs[j]; prev = pos;
                   5477:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5478:                     }
                   5479:                     break;
                   5480:                 case 4:
                   5481:                     ivi = ivect->index.i;
                   5482:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5483:                         pos = prev+ivi[j]; prev = pos;
                   5484:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5485:                     }
                   5486:                     break;
                   5487:             }
                   5488:         }
                   5489:     }
                   5490:     return maxrs;
1.72      noro     5491: }
                   5492:
1.65      noro     5493: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5494: {
1.157     noro     5495:     int j,k,len;
                   5496:     UINT *p;
                   5497:     UINT c;
                   5498:     NDV r;
                   5499:     NMV mr0,mr;
                   5500:
                   5501:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5502:     if ( !len ) return 0;
                   5503:     else {
1.200   ! noro     5504:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     5505: #if 0
1.157     noro     5506:         ndv_alloc += nmv_adv*len;
1.103     noro     5507: #endif
1.157     noro     5508:         mr = mr0;
                   5509:         p = s0vect;
                   5510:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5511:             if ( !rhead[j] ) {
                   5512:                 if ( c = vect[k++] ) {
                   5513:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5514:                 }
                   5515:             }
                   5516:         MKNDV(nd_nvar,mr0,len,r);
                   5517:         return r;
                   5518:     }
1.65      noro     5519: }
                   5520:
1.129     noro     5521: /* for preprocessed vector */
                   5522:
1.113     noro     5523: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5524: {
1.157     noro     5525:     int j,k,len;
                   5526:     UINT *p;
                   5527:     Q c;
                   5528:     NDV r;
                   5529:     NMV mr0,mr;
                   5530:
                   5531:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5532:     if ( !len ) return 0;
                   5533:     else {
1.200   ! noro     5534:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     5535: #if 0
1.157     noro     5536:         ndv_alloc += nmv_adv*len;
1.107     noro     5537: #endif
1.157     noro     5538:         mr = mr0;
                   5539:         p = s0vect;
                   5540:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5541:             if ( !rhead[j] ) {
                   5542:                 if ( c = vect[k++] ) {
                   5543:                     if ( DN(c) )
                   5544:                         error("afo");
                   5545:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5546:                 }
                   5547:             }
                   5548:         MKNDV(nd_nvar,mr0,len,r);
                   5549:         return r;
                   5550:     }
1.107     noro     5551: }
                   5552:
1.129     noro     5553: /* for plain vector */
                   5554:
                   5555: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5556: {
1.157     noro     5557:     int j,k,len;
                   5558:     UINT *p;
                   5559:     Q c;
                   5560:     NDV r;
                   5561:     NMV mr0,mr;
                   5562:
                   5563:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5564:     if ( !len ) return 0;
                   5565:     else {
1.200   ! noro     5566:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     5567: #if 0
1.157     noro     5568:         ndv_alloc += nmv_adv*len;
1.129     noro     5569: #endif
1.157     noro     5570:         mr = mr0;
                   5571:         p = s0vect;
                   5572:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5573:             if ( c = vect[k] ) {
                   5574:                 if ( DN(c) )
                   5575:                     error("afo");
                   5576:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5577:             }
                   5578:         MKNDV(nd_nvar,mr0,len,r);
                   5579:         return r;
                   5580:     }
1.129     noro     5581: }
                   5582:
1.133     noro     5583: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5584: {
1.157     noro     5585:     ND_pairs t;
                   5586:     NODE sp0,sp;
                   5587:     int stat;
                   5588:     ND spol;
                   5589:
                   5590:     for ( t = l; t; t = NEXT(t) ) {
                   5591:         stat = nd_sp(m,trace,t,&spol);
                   5592:         if ( !stat ) return 0;
                   5593:         if ( spol ) {
                   5594:             add_pbucket_symbolic(bucket,spol);
                   5595:         }
                   5596:     }
                   5597:     return 1;
1.65      noro     5598: }
                   5599:
1.133     noro     5600: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5601: {
1.157     noro     5602:     NODE rp0,rp;
                   5603:     NM mul,head,s0,s;
                   5604:     int index,col,i,sugar;
                   5605:     RHist h;
                   5606:     UINT *s0v,*p;
                   5607:     NM_ind_pair pair;
                   5608:     ND red;
                   5609:     NDV *ps;
                   5610:
                   5611:     s0 = 0; rp0 = 0; col = 0;
                   5612:     ps = trace?nd_ps_trace:nd_ps;
                   5613:     while ( 1 ) {
                   5614:         head = remove_head_pbucket_symbolic(bucket);
                   5615:         if ( !head ) break;
                   5616:         if ( !s0 ) s0 = head;
                   5617:         else NEXT(s) = head;
                   5618:         s = head;
                   5619:         index = ndl_find_reducer(DL(head));
                   5620:         if ( index >= 0 ) {
                   5621:             h = nd_psh[index];
                   5622:             NEWNM(mul);
                   5623:             ndl_sub(DL(head),DL(h),DL(mul));
                   5624:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5625:             sugar = TD(DL(mul))+SG(ps[index]);
                   5626:             MKNM_ind_pair(pair,mul,index,sugar);
                   5627:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5628:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5629:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5630:         }
                   5631:         col++;
                   5632:     }
                   5633:     if ( rp0 ) NEXT(rp) = 0;
                   5634:     NEXT(s) = 0;
                   5635:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5636:     for ( i = 0, p = s0v, s = s0; i < col;
                   5637:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5638:     *s0vect = s0v;
                   5639:     *r = rp0;
                   5640:     return col;
1.65      noro     5641: }
                   5642:
1.198     noro     5643:
1.167     noro     5644: NODE nd_f4(int m,int **indp)
1.69      noro     5645: {
1.157     noro     5646:     int i,nh,stat,index;
                   5647:     NODE r,g;
                   5648:     ND_pairs d,l,t;
                   5649:     ND spol,red;
                   5650:     NDV nf,redv;
                   5651:     NM s0,s;
                   5652:     NODE rp0,srp0,nflist;
                   5653:     int nsp,nred,col,rank,len,k,j,a;
                   5654:     UINT c;
                   5655:     UINT **spmat;
                   5656:     UINT *s0vect,*svect,*p,*v;
                   5657:     int *colstat;
                   5658:     IndArray *imat;
                   5659:     int *rhead;
                   5660:     int spcol,sprow;
                   5661:     int sugar;
                   5662:     PGeoBucket bucket;
                   5663:     struct oEGT eg0,eg1,eg_f4;
1.69      noro     5664:
1.103     noro     5665: #if 0
1.157     noro     5666:     ndv_alloc = 0;
1.103     noro     5667: #endif
1.157     noro     5668:     g = 0; d = 0;
                   5669:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5670:         d = update_pairs(d,g,i,0);
1.157     noro     5671:         g = update_base(g,i);
                   5672:     }
                   5673:     while ( d ) {
                   5674:         get_eg(&eg0);
                   5675:         l = nd_minsugarp(d,&d);
                   5676:         sugar = SG(l);
                   5677:         bucket = create_pbucket();
                   5678:         stat = nd_sp_f4(m,0,l,bucket);
                   5679:         if ( !stat ) {
                   5680:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5681:             NEXT(t) = d; d = l;
                   5682:             d = nd_reconstruct(0,d);
                   5683:             continue;
                   5684:         }
                   5685:         if ( bucket->m < 0 ) continue;
                   5686:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5687:         if ( !col ) {
                   5688:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5689:             NEXT(t) = d; d = l;
                   5690:             d = nd_reconstruct(0,d);
                   5691:             continue;
                   5692:         }
                   5693:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5694:         if ( DP_Print )
                   5695:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5696:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5697:         if ( 1 )
                   5698:             nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
                   5699:         else
                   5700:             nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
                   5701:         /* adding new bases */
                   5702:         for ( r = nflist; r; r = NEXT(r) ) {
                   5703:             nf = (NDV)BDY(r);
                   5704:             ndv_removecont(m,nf);
                   5705:             if ( !m && nd_nalg ) {
                   5706:                 ND nf1;
                   5707:
                   5708:                 nf1 = ndvtond(m,nf);
                   5709:                 nd_monic(0,&nf1);
                   5710:                 nd_removecont(m,nf1);
                   5711:                 nf = ndtondv(m,nf1);
                   5712:             }
                   5713:             nh = ndv_newps(m,nf,0);
1.168     noro     5714:             d = update_pairs(d,g,nh,0);
1.157     noro     5715:             g = update_base(g,nh);
                   5716:         }
                   5717:     }
1.103     noro     5718: #if 0
1.157     noro     5719:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5720: #endif
1.167     noro     5721:        conv_ilist(0,0,g,indp);
1.157     noro     5722:     return g;
1.69      noro     5723: }
1.74      noro     5724:
1.167     noro     5725: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5726: {
1.157     noro     5727:     int i,nh,stat,index;
                   5728:     NODE r,g;
                   5729:     ND_pairs d,l,l0,t;
                   5730:     ND spol,red;
                   5731:     NDV nf,redv,nfqv,nfv;
                   5732:     NM s0,s;
                   5733:     NODE rp0,srp0,nflist;
                   5734:     int nsp,nred,col,rank,len,k,j,a;
                   5735:     UINT c;
                   5736:     UINT **spmat;
                   5737:     UINT *s0vect,*svect,*p,*v;
                   5738:     int *colstat;
                   5739:     IndArray *imat;
                   5740:     int *rhead;
                   5741:     int spcol,sprow;
                   5742:     int sugar;
                   5743:     PGeoBucket bucket;
                   5744:     struct oEGT eg0,eg1,eg_f4;
                   5745:
                   5746:     g = 0; d = 0;
                   5747:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5748:         d = update_pairs(d,g,i,0);
1.157     noro     5749:         g = update_base(g,i);
                   5750:     }
                   5751:     while ( d ) {
                   5752:         get_eg(&eg0);
                   5753:         l = nd_minsugarp(d,&d);
                   5754:         sugar = SG(l);
                   5755:         bucket = create_pbucket();
                   5756:         stat = nd_sp_f4(m,0,l,bucket);
                   5757:         if ( !stat ) {
                   5758:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5759:             NEXT(t) = d; d = l;
                   5760:             d = nd_reconstruct(1,d);
                   5761:             continue;
                   5762:         }
                   5763:         if ( bucket->m < 0 ) continue;
                   5764:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5765:         if ( !col ) {
                   5766:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5767:             NEXT(t) = d; d = l;
                   5768:             d = nd_reconstruct(1,d);
                   5769:             continue;
                   5770:         }
                   5771:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5772:         if ( DP_Print )
                   5773:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5774:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5775:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5776:         if ( !l0 ) continue;
                   5777:         l = l0;
                   5778:
                   5779:         /* over Q */
                   5780:         bucket = create_pbucket();
                   5781:         stat = nd_sp_f4(0,1,l,bucket);
                   5782:         if ( !stat ) {
                   5783:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5784:             NEXT(t) = d; d = l;
                   5785:             d = nd_reconstruct(1,d);
                   5786:             continue;
                   5787:         }
                   5788:         if ( bucket->m < 0 ) continue;
                   5789:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5790:         if ( !col ) {
                   5791:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5792:             NEXT(t) = d; d = l;
                   5793:             d = nd_reconstruct(1,d);
                   5794:             continue;
                   5795:         }
                   5796:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5797:         /* adding new bases */
                   5798:         for ( r = nflist; r; r = NEXT(r) ) {
                   5799:             nfqv = (NDV)BDY(r);
                   5800:             ndv_removecont(0,nfqv);
                   5801:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5802:             if ( nd_nalg ) {
                   5803:                 ND nf1;
                   5804:
                   5805:                 nf1 = ndvtond(m,nfqv);
                   5806:                 nd_monic(0,&nf1);
                   5807:                 nd_removecont(0,nf1);
                   5808:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5809:             }
                   5810:             nfv = ndv_dup(0,nfqv);
                   5811:             ndv_mod(m,nfv);
                   5812:             ndv_removecont(m,nfv);
                   5813:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     5814:             d = update_pairs(d,g,nh,0);
1.157     noro     5815:             g = update_base(g,nh);
                   5816:         }
                   5817:     }
1.133     noro     5818: #if 0
1.157     noro     5819:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     5820: #endif
1.167     noro     5821:        conv_ilist(0,1,g,indp);
1.157     noro     5822:     return g;
1.133     noro     5823: }
                   5824:
1.176     noro     5825: NODE nd_f4_pseudo_trace(int m,int **indp)
                   5826: {
                   5827:     int i,nh,stat,index;
                   5828:     NODE r,g;
                   5829:     ND_pairs d,l,l0,t;
                   5830:     ND spol,red;
                   5831:     NDV nf,redv,nfqv,nfv;
                   5832:     NM s0,s;
                   5833:     NODE rp0,srp0,nflist;
                   5834:     int nsp,nred,col,rank,len,k,j,a;
                   5835:     UINT c;
                   5836:     UINT **spmat;
                   5837:     UINT *s0vect,*svect,*p,*v;
                   5838:     int *colstat;
                   5839:     IndArray *imat;
                   5840:     int *rhead;
                   5841:     int spcol,sprow;
                   5842:     int sugar;
                   5843:     PGeoBucket bucket;
                   5844:     struct oEGT eg0,eg1,eg_f4;
                   5845:
                   5846:     g = 0; d = 0;
                   5847:     for ( i = 0; i < nd_psn; i++ ) {
                   5848:         d = update_pairs(d,g,i,0);
                   5849:         g = update_base(g,i);
                   5850:     }
                   5851:     while ( d ) {
                   5852:         get_eg(&eg0);
                   5853:         l = nd_minsugarp(d,&d);
                   5854:         sugar = SG(l);
                   5855:         bucket = create_pbucket();
                   5856:         stat = nd_sp_f4(m,0,l,bucket);
                   5857:         if ( !stat ) {
                   5858:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5859:             NEXT(t) = d; d = l;
                   5860:             d = nd_reconstruct(1,d);
                   5861:             continue;
                   5862:         }
                   5863:         if ( bucket->m < 0 ) continue;
                   5864:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5865:         if ( !col ) {
                   5866:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5867:             NEXT(t) = d; d = l;
                   5868:             d = nd_reconstruct(1,d);
                   5869:             continue;
                   5870:         }
                   5871:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5872:         if ( DP_Print )
                   5873:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5874:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5875:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5876:         if ( !l0 ) continue;
                   5877:         l = l0;
                   5878:
                   5879:         /* over Q */
                   5880:                while ( 1 ) {
                   5881:                bucket = create_pbucket();
                   5882:                stat = nd_sp_f4(0,1,l,bucket);
                   5883:                if ( !stat ) {
                   5884:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5885:                NEXT(t) = d; d = l;
                   5886:                d = nd_reconstruct(1,d);
                   5887:                continue;
                   5888:                }
                   5889:                if ( bucket->m < 0 ) continue;
                   5890:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5891:                if ( !col ) {
                   5892:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5893:                NEXT(t) = d; d = l;
                   5894:                d = nd_reconstruct(1,d);
                   5895:                continue;
                   5896:                }
                   5897:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5898:                }
                   5899:
                   5900:         /* adding new bases */
                   5901:         for ( r = nflist; r; r = NEXT(r) ) {
                   5902:             nfqv = (NDV)BDY(r);
                   5903:             ndv_removecont(0,nfqv);
                   5904:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5905:             if ( nd_nalg ) {
                   5906:                 ND nf1;
                   5907:
                   5908:                 nf1 = ndvtond(m,nfqv);
                   5909:                 nd_monic(0,&nf1);
                   5910:                 nd_removecont(0,nf1);
                   5911:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5912:             }
                   5913:             nfv = ndv_dup(0,nfqv);
                   5914:             ndv_mod(m,nfv);
                   5915:             ndv_removecont(m,nfv);
                   5916:             nh = ndv_newps(0,nfv,nfqv);
                   5917:             d = update_pairs(d,g,nh,0);
                   5918:             g = update_base(g,nh);
                   5919:         }
                   5920:     }
                   5921: #if 0
                   5922:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5923: #endif
                   5924:        conv_ilist(0,1,g,indp);
                   5925:     return g;
                   5926: }
                   5927:
1.133     noro     5928: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     5929: {
1.157     noro     5930:     IndArray *imat;
                   5931:     int nsp,nred,i;
                   5932:     int *rhead;
                   5933:     NODE r0,rp;
                   5934:     ND_pairs sp;
                   5935:     NM_ind_pair *rvect;
1.198     noro     5936: init_eg(&eg_search);
1.157     noro     5937:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5938:     nred = length(rp0);
                   5939:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   5940:     rhead = (int *)ALLOCA(col*sizeof(int));
                   5941:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5942:
                   5943:     /* construction of index arrays */
                   5944:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   5945:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   5946:         rvect[i] = (NM_ind_pair)BDY(rp);
                   5947:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   5948:         rhead[imat[i]->head] = 1;
                   5949:     }
                   5950:     if ( m )
                   5951:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   5952:     else
                   5953:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.198     noro     5954: print_eg("search",&eg_search);
1.157     noro     5955:     return r0;
1.106     noro     5956: }
1.74      noro     5957:
1.106     noro     5958: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     5959:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     5960: {
1.157     noro     5961:     int spcol,sprow,a;
                   5962:     int i,j,k,l,rank;
                   5963:     NODE r0,r;
                   5964:     ND_pairs sp;
                   5965:     ND spol;
                   5966:     int **spmat;
                   5967:     UINT *svect,*v;
                   5968:     int *colstat;
                   5969:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5970:     int maxrs;
                   5971:     int *spsugar;
                   5972:     ND_pairs *spactive;
                   5973:
                   5974:     spcol = col-nred;
                   5975:     get_eg(&eg0);
                   5976:     /* elimination (1st step) */
                   5977:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   5978:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   5979:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   5980:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   5981:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5982:         nd_sp(m,0,sp,&spol);
                   5983:         if ( !spol ) continue;
                   5984:         nd_to_vect(m,s0vect,col,spol,svect);
                   5985:         if ( m == -1 )
                   5986:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   5987:         else
                   5988:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   5989:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5990:         if ( i < col ) {
                   5991:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   5992:             for ( j = k = 0; j < col; j++ )
                   5993:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5994:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5995:             if ( nz )
                   5996:             spactive[sprow] = sp;
                   5997:             sprow++;
                   5998:         }
                   5999:         nd_free(spol);
                   6000:     }
                   6001:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6002:     if ( DP_Print ) {
                   6003:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6004:         fflush(asir_out);
                   6005:     }
                   6006:     /* free index arrays */
1.200   ! noro     6007:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     6008:
                   6009:     /* elimination (2nd step) */
                   6010:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6011:     if ( m == -1 )
                   6012:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6013:     else
                   6014:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   6015:     r0 = 0;
                   6016:     for ( i = 0; i < rank; i++ ) {
                   6017:         NEXTNODE(r0,r); BDY(r) =
                   6018:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6019:         SG((NDV)BDY(r)) = spsugar[i];
1.200   ! noro     6020:         GCFREE(spmat[i]);
1.157     noro     6021:     }
                   6022:     if ( r0 ) NEXT(r) = 0;
                   6023:
1.200   ! noro     6024:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     6025:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6026:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6027:     if ( DP_Print ) {
                   6028:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6029:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6030:             nsp,nred,sprow,spcol,rank);
                   6031:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6032:     }
                   6033:     if ( nz ) {
                   6034:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   6035:         if ( rank > 0 ) {
                   6036:             NEXT(spactive[rank-1]) = 0;
                   6037:             *nz = spactive[0];
                   6038:         } else
                   6039:             *nz = 0;
                   6040:     }
                   6041:     return r0;
1.74      noro     6042: }
                   6043:
1.133     noro     6044: #if 1
                   6045: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     6046:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6047: {
1.157     noro     6048:     int spcol,sprow,a;
                   6049:     int i,j,k,l,rank;
                   6050:     NODE r0,r;
                   6051:     ND_pairs sp;
                   6052:     ND spol;
                   6053:     Q **spmat;
                   6054:     Q *svect,*v;
                   6055:     int *colstat;
                   6056:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   6057:     int maxrs;
                   6058:     int *spsugar;
                   6059:     pointer *w;
                   6060:
                   6061:     spcol = col-nred;
                   6062:     get_eg(&eg0);
                   6063:     /* elimination (1st step) */
                   6064:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   6065:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   6066:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   6067:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6068:         nd_sp(0,trace,sp,&spol);
                   6069:         if ( !spol ) continue;
                   6070:         nd_to_vect_q(s0vect,col,spol,svect);
                   6071:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   6072:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6073:         if ( i < col ) {
                   6074:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   6075:             for ( j = k = 0; j < col; j++ )
                   6076:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6077:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6078:             sprow++;
                   6079:         }
                   6080: /*        nd_free(spol); */
                   6081:     }
                   6082:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6083:     if ( DP_Print ) {
                   6084:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6085:         fflush(asir_out);
                   6086:     }
                   6087:     /* free index arrays */
1.200   ! noro     6088: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     6089:
                   6090:     /* elimination (2nd step) */
                   6091:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6092:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   6093:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   6094:     for ( i = 0; i < rank; i++ ) {
                   6095:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   6096:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.200   ! noro     6097: /*        GCFREE(spmat[i]); */
1.157     noro     6098:     }
1.138     noro     6099: #if 0
1.157     noro     6100:     qsort(w,rank,sizeof(NDV),
                   6101:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     6102: #endif
1.157     noro     6103:     r0 = 0;
                   6104:     for ( i = 0; i < rank; i++ ) {
                   6105:         NEXTNODE(r0,r); BDY(r) = w[i];
                   6106:     }
                   6107:     if ( r0 ) NEXT(r) = 0;
                   6108:
1.200   ! noro     6109: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     6110:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6111:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6112:     if ( DP_Print ) {
                   6113:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6114:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6115:             nsp,nred,sprow,spcol,rank);
                   6116:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6117:     }
                   6118:     return r0;
1.107     noro     6119: }
1.129     noro     6120: #else
                   6121: void printm(Q **mat,int row,int col)
                   6122: {
1.157     noro     6123:     int i,j;
                   6124:     printf("[");
                   6125:     for ( i = 0; i < row; i++ ) {
                   6126:         for ( j = 0; j < col; j++ ) {
                   6127:             printexpr(CO,mat[i][j]); printf(" ");
                   6128:         }
                   6129:         printf("]\n");
                   6130:     }
1.129     noro     6131: }
                   6132:
                   6133: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   6134:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6135: {
1.157     noro     6136:     int row,a;
                   6137:     int i,j,rank;
                   6138:     NODE r0,r;
                   6139:     ND_pairs sp;
                   6140:     ND spol;
                   6141:     Q **mat;
                   6142:     int *colstat;
                   6143:     int *sugar;
                   6144:
                   6145:     row = nsp+nred;
                   6146:     /* make the matrix */
                   6147:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   6148:     sugar = (int *)ALLOCA(row*sizeof(int));
                   6149:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6150:         nd_sp(0,0,sp,&spol);
                   6151:         if ( !spol ) continue;
                   6152:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   6153:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   6154:         sugar[row] = SG(spol);
                   6155:         row++;
                   6156:     }
                   6157:     for ( i = 0; i < nred; i++, row++ ) {
                   6158:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   6159:         sugar[row] = rvect[i]->sugar;
                   6160:     }
                   6161:     /* elimination */
                   6162:     colstat = (int *)ALLOCA(col*sizeof(int));
                   6163:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   6164:     r0 = 0;
                   6165:     for ( i = 0; i < rank; i++ ) {
                   6166:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   6167:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   6168:         if ( rhead[j] ) continue;
                   6169:         NEXTNODE(r0,r); BDY(r) =
                   6170:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   6171:         SG((NDV)BDY(r)) = sugar[i];
                   6172:     }
                   6173:     if ( r0 ) NEXT(r) = 0;
                   6174:     printf("\n");
                   6175:     return r0;
1.129     noro     6176: }
                   6177: #endif
1.107     noro     6178:
1.74      noro     6179: FILE *nd_write,*nd_read;
                   6180:
                   6181: void nd_send_int(int a) {
1.157     noro     6182:     write_int(nd_write,&a);
1.74      noro     6183: }
                   6184:
                   6185: void nd_send_intarray(int *p,int len) {
1.157     noro     6186:     write_intarray(nd_write,p,len);
1.74      noro     6187: }
                   6188:
                   6189: int nd_recv_int() {
1.157     noro     6190:     int a;
1.74      noro     6191:
1.157     noro     6192:     read_int(nd_read,&a);
                   6193:     return a;
1.74      noro     6194: }
                   6195:
                   6196: void nd_recv_intarray(int *p,int len) {
1.157     noro     6197:     read_intarray(nd_read,p,len);
1.74      noro     6198: }
                   6199:
                   6200: void nd_send_ndv(NDV p) {
1.157     noro     6201:     int len,i;
                   6202:     NMV m;
1.74      noro     6203:
1.157     noro     6204:     if ( !p ) nd_send_int(0);
                   6205:     else {
                   6206:         len = LEN(p);
                   6207:         nd_send_int(len);
                   6208:         m = BDY(p);
                   6209:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6210:             nd_send_int(CM(m));
                   6211:             nd_send_intarray(DL(m),nd_wpd);
                   6212:         }
                   6213:     }
1.74      noro     6214: }
                   6215:
                   6216: void nd_send_nd(ND p) {
1.157     noro     6217:     int len,i;
                   6218:     NM m;
1.74      noro     6219:
1.157     noro     6220:     if ( !p ) nd_send_int(0);
                   6221:     else {
                   6222:         len = LEN(p);
                   6223:         nd_send_int(len);
                   6224:         m = BDY(p);
                   6225:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   6226:             nd_send_int(CM(m));
                   6227:             nd_send_intarray(DL(m),nd_wpd);
                   6228:         }
                   6229:     }
1.74      noro     6230: }
1.65      noro     6231:
1.74      noro     6232: NDV nd_recv_ndv()
                   6233: {
1.157     noro     6234:     int len,i;
                   6235:     NMV m,m0;
                   6236:     NDV r;
                   6237:
                   6238:     len = nd_recv_int();
                   6239:     if ( !len ) return 0;
                   6240:     else {
1.200   ! noro     6241:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6242: #if 0
1.157     noro     6243:         ndv_alloc += len*nmv_adv;
1.103     noro     6244: #endif
1.157     noro     6245:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6246:             CM(m) = nd_recv_int();
                   6247:             nd_recv_intarray(DL(m),nd_wpd);
                   6248:         }
                   6249:         MKNDV(nd_nvar,m0,len,r);
                   6250:         return r;
                   6251:     }
1.74      noro     6252: }
1.65      noro     6253:
1.74      noro     6254: int ox_exec_f4_red(Q proc)
                   6255: {
1.157     noro     6256:     Obj obj;
                   6257:     STRING fname;
                   6258:     NODE arg;
                   6259:     int s;
                   6260:     extern int ox_need_conv,ox_file_io;
                   6261:
                   6262:     MKSTR(fname,"nd_exec_f4_red");
                   6263:     arg = mknode(2,proc,fname);
                   6264:     Pox_cmo_rpc(arg,&obj);
                   6265:     s = get_ox_server_id(QTOS(proc));
                   6266:     nd_write = iofp[s].out;
                   6267:     nd_read = iofp[s].in;
                   6268:     ox_need_conv = ox_file_io = 0;
                   6269:     return s;
1.74      noro     6270: }
                   6271:
1.133     noro     6272: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     6273: {
1.157     noro     6274:     int nsp,nred;
                   6275:     int i,rank,s;
                   6276:     NODE rp,r0,r;
                   6277:     ND_pairs sp;
                   6278:     NM_ind_pair pair;
                   6279:     NMV nmv;
                   6280:     NM nm;
                   6281:     NDV nf;
                   6282:     Obj proc,dmy;
                   6283:
                   6284:     ox_launch_main(0,0,&proc);
                   6285:     s = ox_exec_f4_red((Q)proc);
                   6286:
                   6287:     nd_send_int(m);
                   6288:     nd_send_int(nd_nvar);
                   6289:     nd_send_int(nd_bpe);
                   6290:     nd_send_int(nd_wpd);
                   6291:     nd_send_int(nmv_adv);
                   6292:
                   6293:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   6294:
                   6295:     nd_send_int(nd_psn);
                   6296:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   6297:
                   6298:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6299:     nd_send_int(nsp);
                   6300:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   6301:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   6302:     }
                   6303:
                   6304:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   6305:
                   6306:     nred = length(rp0); nd_send_int(nred);
                   6307:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   6308:         pair = (NM_ind_pair)BDY(rp);
                   6309:         nd_send_int(pair->index);
                   6310:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   6311:     }
                   6312:     fflush(nd_write);
                   6313:     rank = nd_recv_int();
                   6314:     fprintf(asir_out,"rank=%d\n",rank);
                   6315:     r0 = 0;
                   6316:     for ( i = 0; i < rank; i++ ) {
                   6317:         nf = nd_recv_ndv();
                   6318:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   6319:     }
                   6320:     Pox_shutdown(mknode(1,proc),&dmy);
                   6321:     return r0;
1.74      noro     6322: }
                   6323:
                   6324: /* server side */
                   6325:
                   6326: void nd_exec_f4_red_dist()
                   6327: {
1.157     noro     6328:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   6329:     NM_ind_pair *rp0;
                   6330:     NDV nf;
                   6331:     UINT *s0vect;
                   6332:     IndArray *imat;
                   6333:     int *rhead;
                   6334:     int **spmat;
                   6335:     UINT *svect,*v;
                   6336:     ND_pairs *sp0;
                   6337:     int *colstat;
                   6338:     int a,sprow,rank;
                   6339:     struct order_spec *ord;
                   6340:     Obj ordspec;
                   6341:     ND spol;
                   6342:     int maxrs;
                   6343:     int *spsugar;
                   6344:
                   6345:     nd_read = iofp[0].in;
                   6346:     nd_write = iofp[0].out;
                   6347:     m = nd_recv_int();
                   6348:     nd_nvar = nd_recv_int();
                   6349:     nd_bpe = nd_recv_int();
                   6350:     nd_wpd = nd_recv_int();
                   6351:     nmv_adv = nd_recv_int();
                   6352:
                   6353:     loadobj(nd_read,&ordspec);
                   6354:     create_order_spec(0,ordspec,&ord);
                   6355:     nd_init_ord(ord);
                   6356:     nd_setup_parameters(nd_nvar,0);
                   6357:
                   6358:     nd_psn = nd_recv_int();
                   6359:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   6360:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   6361:     for ( i = 0; i < nd_psn; i++ ) {
                   6362:         nd_ps[i] = nd_recv_ndv();
                   6363:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   6364:     }
                   6365:
                   6366:     nsp = nd_recv_int();
                   6367:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   6368:     for ( i = 0; i < nsp; i++ ) {
                   6369:         NEWND_pairs(sp0[i]);
                   6370:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6371:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6372:     }
                   6373:
                   6374:     col = nd_recv_int();
                   6375:     s0size = col*nd_wpd;
                   6376:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6377:     nd_recv_intarray(s0vect,s0size);
                   6378:
                   6379:     nred = nd_recv_int();
                   6380:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6381:     for ( i = 0; i < nred; i++ ) {
                   6382:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6383:         rp0[i]->index = nd_recv_int();
                   6384:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6385:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6386:     }
                   6387:
                   6388:     spcol = col-nred;
                   6389:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6390:     rhead = (int *)MALLOC(col*sizeof(int));
                   6391:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6392:
                   6393:     /* construction of index arrays */
                   6394:     for ( i = 0; i < nred; i++ ) {
                   6395:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6396:         rhead[imat[i]->head] = 1;
                   6397:     }
                   6398:
                   6399:     /* elimination (1st step) */
                   6400:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6401:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6402:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6403:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6404:         nd_sp(m,0,sp0[a],&spol);
                   6405:         if ( !spol ) continue;
                   6406:         nd_to_vect(m,s0vect,col,spol,svect);
                   6407:         if ( m == -1 )
                   6408:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6409:         else
                   6410:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6411:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6412:         if ( i < col ) {
                   6413:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6414:             for ( j = k = 0; j < col; j++ )
                   6415:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6416:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6417:             sprow++;
                   6418:         }
                   6419:         nd_free(spol);
                   6420:     }
                   6421:     /* elimination (2nd step) */
                   6422:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6423:     if ( m == -1 )
                   6424:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6425:     else
                   6426:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6427:     nd_send_int(rank);
                   6428:     for ( i = 0; i < rank; i++ ) {
                   6429:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6430:         nd_send_ndv(nf);
                   6431:     }
                   6432:     fflush(nd_write);
1.107     noro     6433: }
                   6434:
1.113     noro     6435: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6436: {
1.176     noro     6437:     int i,j,t,c,rank,inv;
1.157     noro     6438:     int *ci,*ri;
                   6439:     Q dn;
                   6440:     MAT m,nm;
                   6441:
                   6442:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6443:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6444:     for ( i = 0; i < row; i++ )
                   6445:         for ( j = 0; j < col; j++ )
                   6446:             mat0[i][j] = 0;
                   6447:     c = col-rank;
                   6448:     for ( i = 0; i < rank; i++ ) {
                   6449:         mat0[i][ri[i]] = dn;
                   6450:         for ( j = 0; j < c; j++ )
                   6451:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6452:     }
                   6453:     return rank;
1.76      noro     6454: }
                   6455:
1.133     noro     6456: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6457: {
1.157     noro     6458:     int i,j,k,l,inv,a,rank,s;
                   6459:     unsigned int *t,*pivot,*pk;
                   6460:     unsigned int **mat;
                   6461:     ND_pairs pair;
                   6462:
                   6463:     mat = (unsigned int **)mat0;
                   6464:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6465:         for ( i = rank; i < row; i++ )
                   6466:             mat[i][j] %= md;
                   6467:         for ( i = rank; i < row; i++ )
                   6468:             if ( mat[i][j] )
                   6469:                 break;
                   6470:         if ( i == row ) {
                   6471:             colstat[j] = 0;
                   6472:             continue;
                   6473:         } else
                   6474:             colstat[j] = 1;
                   6475:         if ( i != rank ) {
                   6476:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6477:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6478:             if ( spactive ) {
                   6479:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6480:                 spactive[rank] = pair;
                   6481:             }
                   6482:         }
                   6483:         pivot = mat[rank];
                   6484:         s = sugar[rank];
                   6485:         inv = invm(pivot[j],md);
                   6486:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6487:             if ( *pk ) {
                   6488:                 if ( *pk >= (unsigned int)md )
                   6489:                     *pk %= md;
                   6490:                 DMAR(*pk,inv,0,md,*pk)
                   6491:             }
                   6492:         for ( i = rank+1; i < row; i++ ) {
                   6493:             t = mat[i];
                   6494:             if ( a = t[j] ) {
                   6495:                 sugar[i] = MAX(sugar[i],s);
                   6496:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6497:             }
                   6498:         }
                   6499:         rank++;
                   6500:     }
                   6501:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6502:         if ( colstat[j] ) {
                   6503:             pivot = mat[l];
                   6504:             s = sugar[l];
                   6505:             for ( i = 0; i < l; i++ ) {
                   6506:                 t = mat[i];
                   6507:                 t[j] %= md;
                   6508:                 if ( a = t[j] ) {
                   6509:                     sugar[i] = MAX(sugar[i],s);
                   6510:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6511:                 }
                   6512:             }
                   6513:             l--;
                   6514:         }
                   6515:     for ( j = 0, l = 0; l < rank; j++ )
                   6516:         if ( colstat[j] ) {
                   6517:             t = mat[l];
                   6518:             for ( k = j; k < col; k++ )
                   6519:                 if ( t[k] >= (unsigned int)md )
                   6520:                     t[k] %= md;
                   6521:             l++;
                   6522:         }
                   6523:     return rank;
1.76      noro     6524: }
                   6525:
                   6526: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6527: {
1.157     noro     6528:     int i,j,k,l,inv,a,rank,s;
                   6529:     unsigned int *t,*pivot,*pk;
                   6530:     unsigned int **mat;
                   6531:
                   6532:     mat = (unsigned int **)mat0;
                   6533:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6534:         for ( i = rank; i < row; i++ )
                   6535:             if ( mat[i][j] )
                   6536:                 break;
                   6537:         if ( i == row ) {
                   6538:             colstat[j] = 0;
                   6539:             continue;
                   6540:         } else
                   6541:             colstat[j] = 1;
                   6542:         if ( i != rank ) {
                   6543:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6544:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6545:         }
                   6546:         pivot = mat[rank];
                   6547:         s = sugar[rank];
                   6548:         inv = _invsf(pivot[j]);
                   6549:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6550:             if ( *pk )
                   6551:                 *pk = _mulsf(*pk,inv);
                   6552:         for ( i = rank+1; i < row; i++ ) {
                   6553:             t = mat[i];
                   6554:             if ( a = t[j] ) {
                   6555:                 sugar[i] = MAX(sugar[i],s);
                   6556:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6557:             }
                   6558:         }
                   6559:         rank++;
                   6560:     }
                   6561:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6562:         if ( colstat[j] ) {
                   6563:             pivot = mat[l];
                   6564:             s = sugar[l];
                   6565:             for ( i = 0; i < l; i++ ) {
                   6566:                 t = mat[i];
                   6567:                 if ( a = t[j] ) {
                   6568:                     sugar[i] = MAX(sugar[i],s);
                   6569:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6570:                 }
                   6571:             }
                   6572:             l--;
                   6573:         }
                   6574:     return rank;
1.77      noro     6575: }
                   6576:
                   6577: int ndv_ishomo(NDV p)
                   6578: {
1.157     noro     6579:     NMV m;
                   6580:     int len,h;
1.77      noro     6581:
1.157     noro     6582:     if ( !p ) return 1;
                   6583:     len = LEN(p);
                   6584:     m = BDY(p);
                   6585:     h = TD(DL(m));
                   6586:     NMV_ADV(m);
                   6587:     for ( len--; len; len--, NMV_ADV(m) )
                   6588:         if ( TD(DL(m)) != h ) return 0;
                   6589:     return 1;
1.77      noro     6590: }
                   6591:
                   6592: void ndv_save(NDV p,int index)
                   6593: {
1.157     noro     6594:     FILE *s;
                   6595:     char name[BUFSIZ];
                   6596:     short id;
                   6597:     int nv,sugar,len,n,i,td,e,j;
                   6598:     NMV m;
                   6599:     unsigned int *dl;
                   6600:     int mpos;
                   6601:
                   6602:     sprintf(name,"%s/%d",Demand,index);
                   6603:     s = fopen(name,"w");
                   6604:     savevl(s,0);
                   6605:     if ( !p ) {
                   6606:         saveobj(s,0);
                   6607:         return;
                   6608:     }
                   6609:     id = O_DP;
                   6610:     nv = NV(p);
                   6611:     sugar = SG(p);
                   6612:     len = LEN(p);
                   6613:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6614:     write_int(s,&len);
                   6615:
                   6616:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6617:         saveobj(s,(Obj)CQ(m));
                   6618:         dl = DL(m);
                   6619:         td = TD(dl);
                   6620:         write_int(s,&td);
                   6621:         for ( j = 0; j < nv; j++ ) {
                   6622:             e = GET_EXP(dl,j);
                   6623:             write_int(s,&e);
                   6624:         }
                   6625:         if ( nd_module ) {
                   6626:             mpos = MPOS(dl); write_int(s,&mpos);
                   6627:         }
                   6628:     }
                   6629:     fclose(s);
1.77      noro     6630: }
                   6631:
                   6632: NDV ndv_load(int index)
                   6633: {
1.157     noro     6634:     FILE *s;
                   6635:     char name[BUFSIZ];
                   6636:     short id;
                   6637:     int nv,sugar,len,n,i,td,e,j;
                   6638:     NDV d;
                   6639:     NMV m0,m;
                   6640:     unsigned int *dl;
                   6641:     Obj obj;
                   6642:     int mpos;
                   6643:
                   6644:     sprintf(name,"%s/%d",Demand,index);
                   6645:     s = fopen(name,"r");
                   6646:     if ( !s ) return 0;
                   6647:
                   6648:     skipvl(s);
                   6649:     read_short(s,&id);
                   6650:     if ( !id ) return 0;
                   6651:     read_int(s,&nv);
                   6652:     read_int(s,&sugar);
                   6653:     read_int(s,&len);
                   6654:
                   6655:     m0 = m = MALLOC(len*nmv_adv);
                   6656:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6657:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6658:         dl = DL(m);
                   6659:         ndl_zero(dl);
                   6660:         read_int(s,&td); TD(dl) = td;
                   6661:         for ( j = 0; j < nv; j++ ) {
                   6662:             read_int(s,&e);
                   6663:             PUT_EXP(dl,j,e);
                   6664:         }
                   6665:         if ( nd_module ) {
                   6666:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6667:         }
                   6668:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6669:     }
                   6670:     fclose(s);
                   6671:     MKNDV(nv,m0,len,d);
                   6672:     SG(d) = sugar;
                   6673:     return d;
1.99      noro     6674: }
                   6675:
1.102     noro     6676: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6677: {
1.157     noro     6678:     VL fv,tv;
                   6679:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6680:     pointer **m;
                   6681:     Q mone;
                   6682:     P **w;
                   6683:     P mp,r;
                   6684:     NDV **dm;
                   6685:     NDV *t,*mi,*mj;
                   6686:     NDV d,s,mij,mjj;
                   6687:     ND u;
                   6688:     NMV nmv;
                   6689:     UINT *bound;
                   6690:     PGeoBucket bucket;
                   6691:     struct order_spec *ord;
                   6692:     Q dq,dt,ds;
                   6693:     N gn,qn,dn0,nm,dn;
                   6694:
                   6695:     create_order_spec(0,0,&ord);
                   6696:     nd_init_ord(ord);
                   6697:     get_vars((Obj)f,&fv);
                   6698:     if ( f->row != f->col )
                   6699:         error("nd_det : non-square matrix");
                   6700:     n = f->row;
                   6701:     m = f->body;
                   6702:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6703:
                   6704:     if ( !nvar ) {
                   6705:         if ( !mod )
                   6706:             detp(CO,(P **)m,n,rp);
                   6707:         else {
                   6708:             w = (P **)almat_pointer(n,n);
                   6709:             for ( i = 0; i < n; i++ )
                   6710:                 for ( j = 0; j < n; j++ )
                   6711:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6712:             detmp(CO,mod,w,n,&mp);
                   6713:             mptop(mp,rp);
                   6714:         }
                   6715:         return;
                   6716:     }
                   6717:
                   6718:     if ( !mod ) {
                   6719:         w = (P **)almat_pointer(n,n);
                   6720:         dq = ONE;
                   6721:         for ( i = 0; i < n; i++ ) {
                   6722:             dn0 = ONEN;
                   6723:             for ( j = 0; j < n; j++ ) {
                   6724:                 if ( !m[i][j] ) continue;
                   6725:                 lgp(m[i][j],&nm,&dn);
                   6726:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   6727:             }
                   6728:             if ( !UNIN(dn0) ) {
                   6729:                 NTOQ(dn0,1,ds);
                   6730:                 for ( j = 0; j < n; j++ )
                   6731:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   6732:                 mulq(dq,ds,&dt); dq = dt;
                   6733:             } else
                   6734:                 for ( j = 0; j < n; j++ )
                   6735:                     w[i][j] = (P)m[i][j];
                   6736:         }
                   6737:         m = (pointer **)w;
                   6738:     }
                   6739:
1.178     noro     6740:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6741:         for ( j = 0; j < n; j++ )
                   6742:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   6743:                 e = getdeg(tv->v,(P)m[i][j]);
                   6744:                 max = MAX(e,max);
                   6745:             }
                   6746:     nd_setup_parameters(nvar,max);
                   6747:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     6748:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6749:         for ( j = 0; j < n; j++ ) {
                   6750:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   6751:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   6752:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   6753:         }
                   6754:     d = ptondv(CO,fv,(P)ONE);
                   6755:     if ( mod ) ndv_mod(mod,d);
                   6756:     chsgnq(ONE,&mone);
                   6757:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   6758:         if ( DP_Print ) fprintf(stderr,".",j);
                   6759:         for ( i = j; i < n && !dm[i][j]; i++ );
                   6760:         if ( i == n ) {
                   6761:             *rp = 0;
                   6762:             return;
                   6763:         }
                   6764:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   6765:         for ( k = j; k < n; k++ )
                   6766:             for ( l = j; l < n; l++ )
                   6767:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   6768:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   6769:                 }
                   6770:         if ( k0 != j ) {
                   6771:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   6772:             sgn = -sgn;
                   6773:         }
                   6774:         if ( l0 != j ) {
                   6775:             for ( k = j; k < n; k++ ) {
                   6776:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   6777:             }
                   6778:             sgn = -sgn;
                   6779:         }
                   6780:         bound = nd_det_compute_bound(dm,n,j);
                   6781:         for ( k = 0; k < nd_nvar; k++ )
                   6782:             if ( bound[k]*2 > nd_mask0 ) break;
                   6783:         if ( k < nd_nvar )
                   6784:             nd_det_reconstruct(dm,n,j,d);
                   6785:
                   6786:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   6787: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   6788:             mi = dm[i]; mij = mi[j];
                   6789:             if ( mod )
                   6790:                 ndv_mul_c(mod,mij,mod-1);
                   6791:             else
                   6792:                 ndv_mul_c_q(mij,mone);
                   6793:             for ( k = j+1; k < n; k++ ) {
                   6794: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   6795:                 bucket = create_pbucket();
                   6796:                 if ( mi[k] ) {
                   6797:                     nmv = BDY(mjj); len = LEN(mjj);
                   6798:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6799:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   6800:                         add_pbucket(mod,bucket,u);
                   6801:                     }
                   6802:                 }
                   6803:                 if ( mj[k] && mij ) {
                   6804:                     nmv = BDY(mij); len = LEN(mij);
                   6805:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6806:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   6807:                         add_pbucket(mod,bucket,u);
                   6808:                     }
                   6809:                 }
                   6810:                 u = nd_quo(mod,bucket,d);
                   6811:                 mi[k] = ndtondv(mod,u);
                   6812:             }
                   6813: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   6814:         }
                   6815:         d = mjj;
                   6816:     }
                   6817:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   6818:     if ( sgn < 0 )
                   6819:         if ( mod )
                   6820:             ndv_mul_c(mod,d,mod-1);
                   6821:         else
                   6822:             ndv_mul_c_q(d,mone);
                   6823:     r = ndvtop(mod,CO,fv,d);
                   6824:     if ( !mod && !UNIQ(dq) )
                   6825:         divsp(CO,r,(P)dq,rp);
                   6826:     else
                   6827:         *rp = r;
1.99      noro     6828: }
                   6829:
1.102     noro     6830: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     6831: {
1.157     noro     6832:     NM mr,mr0;
                   6833:     NM tnm;
                   6834:     NMV m;
                   6835:     UINT *d0,*dt,*dm;
                   6836:     int c,n,td,i,c1,c2,len;
                   6837:     Q q;
                   6838:     ND r;
                   6839:
                   6840:     if ( !p ) return 0;
                   6841:     else {
                   6842:         n = NV(p); m = BDY(p); len = LEN(p);
                   6843:         d0 = DL(m0);
                   6844:         td = TD(d);
                   6845:         mr0 = 0;
                   6846:         NEWNM(tnm);
                   6847:         if ( mod ) {
                   6848:             c = CM(m0);
                   6849:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6850:                 ndl_add(DL(m),d0,DL(tnm));
                   6851:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6852:                     NEXTNM(mr0,mr);
                   6853:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   6854:                     ndl_copy(DL(tnm),DL(mr));
                   6855:                 }
                   6856:             }
                   6857:         } else {
                   6858:             q = CQ(m0);
                   6859:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6860:                 ndl_add(DL(m),d0,DL(tnm));
                   6861:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6862:                     NEXTNM(mr0,mr);
                   6863:                     mulq(CQ(m),q,&CQ(mr));
                   6864:                     ndl_copy(DL(tnm),DL(mr));
                   6865:                 }
                   6866:             }
                   6867:         }
                   6868:         if ( !mr0 )
                   6869:             return 0;
                   6870:         else {
                   6871:             NEXT(mr) = 0;
                   6872:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6873:             MKND(NV(p),mr0,len,r);
                   6874:             SG(r) = SG(p) + TD(d0);
                   6875:             return r;
                   6876:         }
                   6877:     }
1.114     noro     6878: }
                   6879:
                   6880: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   6881: {
1.157     noro     6882:     int i,obpe,oadv,h,k,l;
                   6883:     static NM prev_nm_free_list;
                   6884:     EPOS oepos;
                   6885:
                   6886:     obpe = nd_bpe;
                   6887:     oadv = nmv_adv;
                   6888:     oepos = nd_epos;
                   6889:     if ( obpe < 2 ) nd_bpe = 2;
                   6890:     else if ( obpe < 3 ) nd_bpe = 3;
                   6891:     else if ( obpe < 4 ) nd_bpe = 4;
                   6892:     else if ( obpe < 5 ) nd_bpe = 5;
                   6893:     else if ( obpe < 6 ) nd_bpe = 6;
                   6894:     else if ( obpe < 8 ) nd_bpe = 8;
                   6895:     else if ( obpe < 10 ) nd_bpe = 10;
                   6896:     else if ( obpe < 16 ) nd_bpe = 16;
                   6897:     else if ( obpe < 32 ) nd_bpe = 32;
                   6898:     else error("nd_det_reconstruct : exponent too large");
                   6899:
                   6900:     nd_setup_parameters(nd_nvar,0);
                   6901:     prev_nm_free_list = _nm_free_list;
                   6902:     _nm_free_list = 0;
                   6903:     for ( k = j; k < n; k++ )
                   6904:         for (l = j; l < n; l++ )
                   6905:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   6906:     ndv_realloc(d,obpe,oadv,oepos);
                   6907:     prev_nm_free_list = 0;
1.114     noro     6908: #if 0
1.157     noro     6909:     GC_gcollect();
1.114     noro     6910: #endif
                   6911: }
                   6912:
1.153     noro     6913: /* returns a UINT array containing degree bounds */
                   6914:
1.114     noro     6915: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   6916: {
1.157     noro     6917:     UINT *d0,*d1,*d,*t,*r;
                   6918:     int k,l,i;
1.114     noro     6919:
1.157     noro     6920:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   6921:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   6922:     for ( k = j; k < n; k++ )
                   6923:         for ( l = j; l < n; l++ )
                   6924:             if ( dm[k][l] ) {
                   6925:                 d = ndv_compute_bound(dm[k][l]);
                   6926:                 for ( i = 0; i < nd_nvar; i++ )
                   6927:                     d0[i] = MAX(d0[i],d[i]);
                   6928:             }
                   6929:     return d0;
1.117     noro     6930: }
                   6931:
                   6932: DL nd_separate_d(UINT *d,UINT *trans)
                   6933: {
1.157     noro     6934:     int n,td,i,e,j;
                   6935:     DL a;
1.117     noro     6936:
1.157     noro     6937:     ndl_zero(trans);
                   6938:     td = 0;
                   6939:     for ( i = 0; i < nd_ntrans; i++ ) {
                   6940:         e = GET_EXP(d,i);
                   6941:         PUT_EXP(trans,i,e);
                   6942:         td += MUL_WEIGHT(e,i);
                   6943:     }
                   6944:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   6945:         /* homogenized */
                   6946:         i = nd_nvar-1;
                   6947:         e = GET_EXP(d,i);
                   6948:         PUT_EXP(trans,i,e);
                   6949:         td += MUL_WEIGHT(e,i);
                   6950:     }
                   6951:     TD(trans) = td;
                   6952:     if ( nd_blockmask) ndl_weight_mask(trans);
                   6953:     NEWDL(a,nd_nalg);
                   6954:     td = 0;
                   6955:     for ( i = 0; i < nd_nalg; i++ ) {
                   6956:         j = nd_ntrans+i;
                   6957:         e = GET_EXP(d,j);
                   6958:         a->d[i] = e;
                   6959:         td += e;
                   6960:     }
                   6961:     a->td = td;
                   6962:     return a;
1.117     noro     6963: }
                   6964:
1.118     noro     6965: int nd_monic(int mod,ND *p)
1.117     noro     6966: {
1.157     noro     6967:     UINT *trans,*t;
                   6968:     DL alg;
                   6969:     MP mp0,mp;
                   6970:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   6971:     ND r;
                   6972:     DL dl;
                   6973:     DP nm;
                   6974:     NDV ndv;
                   6975:     DAlg inv,cd;
                   6976:     ND s,c;
                   6977:     Q l,mul;
                   6978:     N ln;
                   6979:     int n,ntrans,i,e,td,is_lc,len;
                   6980:     NumberField nf;
                   6981:     struct oEGT eg0,eg1;
                   6982:
                   6983:     if ( !(nf = get_numberfield()) )
                   6984:         error("nd_monic : current_numberfield is not set");
                   6985:
                   6986:     /* Q coef -> DAlg coef */
                   6987:     NEWNM(ma0); ma = ma0;
                   6988:     m = BDY(*p);
                   6989:     is_lc = 1;
                   6990:     while ( 1 ) {
                   6991:         NEWMP(mp0); mp = mp0;
                   6992:         mp->c = (P)CQ(m);
                   6993:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   6994:         NEWNM(mb);
                   6995:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6996:             alg = nd_separate_d(DL(m),DL(mb));
                   6997:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   6998:                 break;
                   6999:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   7000:         }
                   7001:         NEXT(mp) = 0;
                   7002:         MKDP(nd_nalg,mp0,nm);
                   7003:         MKDAlg(nm,ONE,cd);
                   7004:         if ( is_lc == 1 ) {
                   7005:             /* if the lc is a rational number, we have nothing to do */
                   7006:             if ( !mp0->dl->td )
                   7007:                 return 1;
                   7008:
                   7009:             get_eg(&eg0);
                   7010:             invdalg(cd,&inv);
                   7011:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   7012:             /* check the validity of inv */
                   7013:             if ( mod && !rem(NM(inv->dn),mod) )
                   7014:                 return 0;
                   7015:             CA(ma) = nf->one;
                   7016:             is_lc = 0;
                   7017:             ln = ONEN;
                   7018:         } else {
                   7019:             muldalg(cd,inv,&CA(ma));
                   7020:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   7021:         }
                   7022:         if ( m ) {
                   7023:             NEXT(ma) = mb; ma = mb;
                   7024:         } else {
                   7025:             NEXT(ma) = 0;
                   7026:             break;
                   7027:         }
                   7028:     }
                   7029:     /* l = lcm(denoms) */
                   7030:     NTOQ(ln,1,l);
                   7031:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   7032:         divq(l,CA(m)->dn,&mul);
                   7033:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   7034:             NEXTNM(mr0,mr);
                   7035:             mulq((Q)mp->c,mul,&CQ(mr));
                   7036:             dl = mp->dl;
                   7037:             td = TD(DL(m));
                   7038:             ndl_copy(DL(m),DL(mr));
                   7039:             for ( i = 0; i < nd_nalg; i++ ) {
                   7040:                 e = dl->d[i];
                   7041:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   7042:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   7043:             }
1.163     noro     7044:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     7045:             TD(DL(mr)) = td;
                   7046:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   7047:         }
                   7048:     }
                   7049:     NEXT(mr) = 0;
                   7050:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   7051:     MKND(NV(*p),mr0,len,r);
                   7052:     /* XXX */
                   7053:     SG(r) = SG(*p);
                   7054:     nd_free(*p);
                   7055:     *p = r;
                   7056:     return 1;
1.59      noro     7057: }
1.167     noro     7058:
                   7059: NODE reverse_node(NODE n)
                   7060: {
                   7061:     NODE t,t1;
                   7062:
                   7063:     for ( t = 0; n; n = NEXT(n) ) {
                   7064:         MKNODE(t1,BDY(n),t); t = t1;
                   7065:     }
                   7066:     return t;
                   7067: }
                   7068:
                   7069: P ndc_div(int mod,union oNDC a,union oNDC b)
                   7070: {
                   7071:     union oNDC c;
                   7072:     int inv,t;
                   7073:
                   7074:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   7075:     else if ( mod ) {
                   7076:         inv = invm(b.m,mod);
                   7077:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   7078:     } else if ( nd_vc )
                   7079:        divsp(nd_vc,a.p,b.p,&c.p);
                   7080:     else
                   7081:        divq(a.z,b.z,&c.z);
                   7082:     return ndctop(mod,c);
                   7083: }
                   7084:
                   7085: P ndctop(int mod,union oNDC c)
                   7086: {
                   7087:     Q q;
                   7088:     int e;
                   7089:     GFS gfs;
                   7090:
                   7091:     if ( mod == -1 ) {
                   7092:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   7093:     } else if ( mod ) {
                   7094:         STOQ(c.m,q); return (P)q;
                   7095:     } else
                   7096:         return (P)c.p;
                   7097: }
                   7098:
                   7099: /* [0,0,0,cont] = p -> p/cont */
                   7100:
                   7101: void finalize_tracelist(int i,P cont)
                   7102: {
                   7103:         LIST l;
                   7104:         NODE node;
                   7105:      Q iq;
                   7106:
                   7107:         if ( !UNIQ(cont) ) {
1.196     noro     7108:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     7109:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   7110:                 nd_tracelist = node;
                   7111:         }
                   7112:      STOQ(i,iq);
                   7113:      nd_tracelist = reverse_node(nd_tracelist);
                   7114:      MKLIST(l,nd_tracelist);
                   7115:      node = mknode(2,iq,l); MKLIST(l,node);
                   7116:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   7117:      nd_alltracelist = node; nd_tracelist = 0;
                   7118: }
                   7119:
                   7120: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   7121: {
                   7122:     int n,i,j;
                   7123:        int *ind;
                   7124:        NODE t;
                   7125:
                   7126:     n = length(g);
                   7127:        ind = (int *)MALLOC(n*sizeof(int));
                   7128:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   7129:                j = (long)BDY(t); ind[i] = j;
                   7130:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   7131:        }
                   7132:        if ( indp ) *indp = ind;
                   7133: }
1.172     noro     7134:
                   7135: void parse_nd_option(NODE opt)
                   7136: {
1.187     noro     7137:     NODE t,p,u;
                   7138:        int i,s;
1.172     noro     7139:     char *key;
                   7140:     Obj value;
                   7141:
1.187     noro     7142:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.195     noro     7143:        nd_newelim = 0; nd_intersect = 0;
1.172     noro     7144:     for ( t = opt; t; t = NEXT(t) ) {
                   7145:         p = BDY((LIST)BDY(t));
                   7146:         key = BDY((STRING)BDY(p));
                   7147:         value = (Obj)BDY(NEXT(p));
                   7148:         if ( !strcmp(key,"gentrace") )
                   7149:             nd_gentrace = value?1:0;
                   7150:         else if ( !strcmp(key,"gensyz") )
                   7151:             nd_gensyz = value?1:0;
1.173     noro     7152:         else if ( !strcmp(key,"nora") )
                   7153:             nd_nora = value?1:0;
1.187     noro     7154:         else if ( !strcmp(key,"gbblock") ) {
                   7155:                        if ( !value || OID(value) != O_LIST )
                   7156:                                error("nd_* : invalid value for gbblock option");
                   7157:                        u = BDY((LIST)value);
1.189     noro     7158:             nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.187     noro     7159:                        for ( i = 0; u; u = NEXT(u) ) {
                   7160:                                p = BDY((LIST)BDY(u));
                   7161:                                s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   7162:                                nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   7163:                        }
                   7164:                        nd_gbblock[i] = -1;
1.192     noro     7165:                } else if ( !strcmp(key,"newelim") )
                   7166:             nd_newelim = value?1:0;
1.195     noro     7167:                else if ( !strcmp(key,"intersect") )
                   7168:             nd_intersect = value?1:0;
1.172     noro     7169:     }
                   7170: }

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