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

1.201   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.200 2012/12/17 07:20:44 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:     }
1.201   ! noro     3955:     if ( !do_weyl && current_dl_weight_vector ) {
        !          3956:         UINT t;
        !          3957:                /* t = max(weights) */
        !          3958:         for ( i = 0, t = 0; i < nd_nvar; i++ )
        !          3959:                        if ( t < current_dl_weight_vector[i] )
        !          3960:                                t = current_dl_weight_vector[i];
        !          3961:                /* i = bitsize of t */
        !          3962:                for ( i = 0; t; t >>=1, i++ );
        !          3963:                /* i += bitsize of nd_nvar */
        !          3964:                for ( t = nd_nvar; t; t >>=1, i++);
        !          3965:                /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
        !          3966:                if ( (nd_bpe+i) >= 31 )
        !          3967:                        error("nd_setup_parameters : too large weight");
        !          3968:        }
1.157     noro     3969:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   3970:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3971:     nd_exporigin = nd_get_exporigin(nd_ord);
                   3972:     wpd = nd_exporigin+elen;
                   3973:     if ( nd_module )
                   3974:         nd_mpos = nd_exporigin-1;
                   3975:     else
                   3976:         nd_mpos = -1;
                   3977:     if ( wpd != nd_wpd ) {
                   3978:         nd_free_private_storage();
                   3979:         nd_wpd = wpd;
                   3980:     }
                   3981:     if ( nd_bpe < 32 ) {
                   3982:         nd_mask0 = (1<<nd_bpe)-1;
                   3983:     } else {
                   3984:         nd_mask0 = 0xffffffff;
                   3985:     }
                   3986:     bzero(nd_mask,sizeof(nd_mask));
                   3987:     nd_mask1 = 0;
                   3988:     for ( i = 0; i < nd_epw; i++ ) {
                   3989:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3990:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3991:     }
                   3992:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   3993:     nd_epos = nd_create_epos(nd_ord);
                   3994:     nd_blockmask = nd_create_blockmask(nd_ord);
                   3995:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3996: }
                   3997:
1.103     noro     3998: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3999: {
1.157     noro     4000:     int i,obpe,oadv,h;
                   4001:     static NM prev_nm_free_list;
                   4002:     static ND_pairs prev_ndp_free_list;
                   4003:     RHist mr0,mr;
                   4004:     RHist r;
                   4005:     RHist *old_red;
                   4006:     ND_pairs s0,s,t;
                   4007:     EPOS oepos;
                   4008:
                   4009:     obpe = nd_bpe;
                   4010:     oadv = nmv_adv;
                   4011:     oepos = nd_epos;
                   4012:     if ( obpe < 2 ) nd_bpe = 2;
                   4013:     else if ( obpe < 3 ) nd_bpe = 3;
                   4014:     else if ( obpe < 4 ) nd_bpe = 4;
                   4015:     else if ( obpe < 5 ) nd_bpe = 5;
                   4016:     else if ( obpe < 6 ) nd_bpe = 6;
                   4017:     else if ( obpe < 8 ) nd_bpe = 8;
                   4018:     else if ( obpe < 10 ) nd_bpe = 10;
                   4019:     else if ( obpe < 16 ) nd_bpe = 16;
                   4020:     else if ( obpe < 32 ) nd_bpe = 32;
                   4021:     else error("nd_reconstruct : exponent too large");
                   4022:
                   4023:     nd_setup_parameters(nd_nvar,0);
                   4024:     prev_nm_free_list = _nm_free_list;
                   4025:     prev_ndp_free_list = _ndp_free_list;
                   4026:     _nm_free_list = 0;
                   4027:     _ndp_free_list = 0;
                   4028:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4029:     if ( trace )
                   4030:         for ( i = nd_psn-1; i >= 0; i-- )
                   4031:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   4032:     s0 = 0;
                   4033:     for ( t = d; t; t = NEXT(t) ) {
                   4034:         NEXTND_pairs(s0,s);
                   4035:         s->i1 = t->i1;
                   4036:         s->i2 = t->i2;
                   4037:         SG(s) = SG(t);
                   4038:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4039:     }
                   4040:
                   4041:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   4042:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4043:         old_red[i] = nd_red[i];
                   4044:         nd_red[i] = 0;
                   4045:     }
                   4046:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4047:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4048:             NEWRHist(mr);
                   4049:             mr->index = r->index;
                   4050:             SG(mr) = SG(r);
                   4051:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4052:             h = ndl_hash_value(DL(mr));
                   4053:             NEXT(mr) = nd_red[h];
                   4054:             nd_red[h] = mr;
                   4055:         }
                   4056:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4057:     old_red = 0;
                   4058:     for ( i = 0; i < nd_psn; i++ ) {
                   4059:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4060:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4061:         nd_psh[i] = r;
                   4062:     }
                   4063:     if ( s0 ) NEXT(s) = 0;
                   4064:     prev_nm_free_list = 0;
                   4065:     prev_ndp_free_list = 0;
1.71      noro     4066: #if 0
1.157     noro     4067:     GC_gcollect();
1.71      noro     4068: #endif
1.157     noro     4069:     return s0;
1.1       noro     4070: }
                   4071:
1.61      noro     4072: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4073: {
1.157     noro     4074:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4075:     struct order_pair *op;
1.1       noro     4076:
1.157     noro     4077:     n = nd_nvar;
                   4078:     oepw = (sizeof(UINT)*8)/obpe;
                   4079:     omask0 = (1<<obpe)-1;
                   4080:     TD(r) = TD(d);
                   4081:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4082:     if ( nd_blockmask ) {
                   4083:         l = nd_blockmask->n;
                   4084:         op = nd_blockmask->order_pair;
                   4085:         for ( i = 1; i < nd_exporigin; i++ )
                   4086:             r[i] = d[i];
                   4087:         for ( j = 0, s = 0; j < l; j++ ) {
                   4088:             ord_l = op[j].length;
                   4089:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4090:                 ei =  GET_EXP_OLD(d,s);
                   4091:                 PUT_EXP(r,s,ei);
                   4092:             }
                   4093:         }
                   4094:     } else {
                   4095:         for ( i = 0; i < n; i++ ) {
                   4096:             ei = GET_EXP_OLD(d,i);
                   4097:             PUT_EXP(r,i,ei);
                   4098:         }
                   4099:     }
                   4100:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4101: }
1.3       noro     4102:
1.6       noro     4103: ND nd_copy(ND p)
                   4104: {
1.157     noro     4105:     NM m,mr,mr0;
                   4106:     int c,n;
                   4107:     ND r;
                   4108:
                   4109:     if ( !p )
                   4110:         return 0;
                   4111:     else {
                   4112:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4113:             NEXTNM(mr0,mr);
                   4114:             CM(mr) = CM(m);
                   4115:             ndl_copy(DL(m),DL(mr));
                   4116:         }
                   4117:         NEXT(mr) = 0;
                   4118:         MKND(NV(p),mr0,LEN(p),r);
                   4119:         SG(r) = SG(p);
                   4120:         return r;
                   4121:     }
1.6       noro     4122: }
                   4123:
1.53      noro     4124: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4125: {
1.157     noro     4126:     NM m1,m2;
                   4127:     NDV p1,p2;
                   4128:     ND t1,t2;
                   4129:     UINT *lcm;
                   4130:     P gp,tp;
1.167     noro     4131:     Q g,t,iq;
1.157     noro     4132:     int td;
1.167     noro     4133:     LIST hist;
                   4134:     NODE node;
                   4135:     DP d;
1.157     noro     4136:
                   4137:     if ( !mod && nd_demand ) {
                   4138:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4139:     } else {
                   4140:         if ( trace ) {
                   4141:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4142:         } else {
                   4143:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4144:         }
                   4145:     }
                   4146:     lcm = LCM(p);
                   4147:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4148:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4149:         FREENM(m1); return 0;
                   4150:     }
                   4151:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4152:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4153:         FREENM(m1); FREENM(m2); return 0;
                   4154:     }
                   4155:
                   4156:     if ( mod == -1 ) {
                   4157:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   4158:     } else if ( mod ) {
                   4159:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   4160:     } else if ( nd_vc ) {
                   4161:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4162:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4163:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4164:     } else {
                   4165:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4166:     }
                   4167:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4168:     *rp = nd_add(mod,t1,t2);
1.172     noro     4169:     if ( nd_gentrace ) {
1.167     noro     4170:         /* nd_tracelist is initialized */
                   4171:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4172:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4173:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4174:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4175:         nd_tracelist = node;
                   4176:     }
1.157     noro     4177:     FREENM(m1); FREENM(m2);
                   4178:     return 1;
1.11      noro     4179: }
                   4180:
1.19      noro     4181: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4182: {
1.157     noro     4183:     NMV m;
                   4184:     int c,c1,len,i;
1.11      noro     4185:
1.157     noro     4186:     if ( !p ) return;
                   4187:     len = LEN(p);
                   4188:     if ( mod == -1 )
                   4189:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4190:             CM(m) = _mulsf(CM(m),mul);
                   4191:     else
                   4192:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4193:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4194:         }
1.11      noro     4195: }
                   4196:
1.113     noro     4197: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4198: {
1.157     noro     4199:     NMV m;
                   4200:     Q c;
                   4201:     int len,i;
                   4202:
                   4203:     if ( !p ) return;
                   4204:     len = LEN(p);
                   4205:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4206:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4207:     }
1.16      noro     4208: }
                   4209:
1.55      noro     4210: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4211:     int n2,i,j,l,n,tlen;
                   4212:     UINT *d0;
                   4213:     NM *tab,*psum;
                   4214:     ND s,r;
                   4215:     NM t;
                   4216:     NMV m1;
                   4217:
                   4218:     if ( !p ) return 0;
                   4219:     n = NV(p); n2 = n>>1;
                   4220:     d0 = DL(m0);
                   4221:     l = LEN(p);
                   4222:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4223:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4224:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4225:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4226:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4227:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4228:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4229:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4230:         for ( j = 0; j < tlen; j++ ) {
                   4231:             if ( tab[j] ) {
                   4232:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4233:             }
                   4234:         }
                   4235:     }
                   4236:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4237:         if ( psum[i] ) {
                   4238:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4239:             MKND(n,psum[i],j,s);
                   4240:             r = nd_add(mod,r,s);
                   4241:         }
                   4242:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4243:     return r;
1.55      noro     4244: }
                   4245:
1.56      noro     4246: /* product of monomials */
                   4247: /* XXX block order is not handled correctly */
                   4248:
1.55      noro     4249: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4250: {
1.157     noro     4251:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4252:     UINT *d0,*d1,*d,*dt,*ctab;
                   4253:     Q *ctab_q;
                   4254:     Q q,q1;
                   4255:     UINT c0,c1,c;
                   4256:     NM *p;
                   4257:     NM m,t;
                   4258:     int mpos;
                   4259:
                   4260:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4261:     if ( !m0 || !m1 ) return;
                   4262:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4263:     if ( nd_module )
                   4264:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4265:
1.157     noro     4266:     NEWNM(m); d = DL(m);
                   4267:     if ( mod ) {
                   4268:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4269:     } else if ( nd_vc )
                   4270:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4271:        else
1.157     noro     4272:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4273:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4274:     homo = n&1 ? 1 : 0;
                   4275:     if ( homo ) {
                   4276:         /* offset of h-degree */
                   4277:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4278:         PUT_EXP(DL(m),n-1,h);
                   4279:         TD(DL(m)) = h;
                   4280:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4281:     }
                   4282:     tab[0] = m;
                   4283:     NEWNM(m); d = DL(m);
                   4284:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4285:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4286:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4287:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4288:         a += l; b += k;
                   4289:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4290:         if ( !k || !l ) {
                   4291:             for ( j = 0; j < curlen; j++ )
                   4292:                 if ( t = tab[j] ) {
                   4293:                     dt = DL(t);
                   4294:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4295:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4296:                 }
                   4297:             curlen *= k+1;
                   4298:             continue;
                   4299:         }
                   4300:         min = MIN(k,l);
                   4301:         if ( mod ) {
                   4302:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4303:             mkwcm(k,l,mod,ctab);
                   4304:         } else {
                   4305:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4306:             mkwc(k,l,ctab_q);
                   4307:         }
                   4308:         for ( j = min; j >= 0; j-- ) {
                   4309:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4310:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4311:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4312:             if ( homo ) {
                   4313:                 TD(d) = s;
                   4314:                 PUT_EXP(d,n-1,s-h);
                   4315:             } else TD(d) = h;
                   4316:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4317:             if ( mod ) c = ctab[j];
                   4318:             else q = ctab_q[j];
                   4319:             p = tab+curlen*j;
                   4320:             if ( j == 0 ) {
                   4321:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4322:                     if ( tab[u] ) {
                   4323:                         ndl_addto(DL(tab[u]),d);
                   4324:                         if ( mod ) {
                   4325:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4326:                         } else if ( nd_vc )
                   4327:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4328:                                                else {
1.157     noro     4329:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4330:                         }
                   4331:                     }
                   4332:                 }
                   4333:             } else {
                   4334:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4335:                     if ( tab[u] ) {
                   4336:                         NEWNM(t);
                   4337:                         ndl_add(DL(tab[u]),d,DL(t));
                   4338:                         if ( mod ) {
                   4339:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4340:                         } else if ( nd_vc )
1.180     noro     4341:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4342:                                                else
1.157     noro     4343:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4344:                         *p = t;
                   4345:                     }
                   4346:                 }
                   4347:             }
                   4348:         }
                   4349:         curlen *= k+1;
                   4350:     }
                   4351:     FREENM(m);
1.167     noro     4352:     if ( nd_module ) {
1.166     noro     4353:         mpos = MPOS(d1);
1.167     noro     4354:         for ( i = 0; i < tlen; i++ )
                   4355:             if ( tab[i] ) {
                   4356:                 d = DL(tab[i]);
                   4357:                 MPOS(d) = mpos;
                   4358:                 TD(d) = ndl_weight(d);
                   4359:             }
                   4360:     }
1.55      noro     4361: }
                   4362:
1.63      noro     4363: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4364: {
1.157     noro     4365:     NM mr,mr0;
                   4366:     NMV m;
                   4367:     UINT *d,*dt,*dm;
                   4368:     int c,n,td,i,c1,c2,len;
                   4369:     Q q;
                   4370:     ND r;
                   4371:
                   4372:     if ( !p ) return 0;
                   4373:     else {
                   4374:         n = NV(p); m = BDY(p);
                   4375:         d = DL(m0);
                   4376:         len = LEN(p);
                   4377:         mr0 = 0;
                   4378:         td = TD(d);
                   4379:         c = CM(m0);
                   4380:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4381:             NEXTNM(mr0,mr);
                   4382:             CM(mr) = 1;
                   4383:             ndl_add(DL(m),d,DL(mr));
                   4384:         }
                   4385:         NEXT(mr) = 0;
                   4386:         MKND(NV(p),mr0,len,r);
                   4387:         SG(r) = SG(p) + TD(d);
                   4388:         return r;
                   4389:     }
1.63      noro     4390: }
                   4391:
1.55      noro     4392: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4393: {
1.157     noro     4394:     NM mr,mr0;
                   4395:     NMV m;
                   4396:     UINT *d,*dt,*dm;
                   4397:     int c,n,td,i,c1,c2,len;
                   4398:     P q;
                   4399:     ND r;
                   4400:
                   4401:     if ( !p ) return 0;
                   4402:     else if ( do_weyl )
                   4403:         if ( mod == -1 )
                   4404:             error("ndv_mul_nm : not implemented (weyl)");
                   4405:         else
                   4406:             return weyl_ndv_mul_nm(mod,m0,p);
                   4407:     else {
                   4408:         n = NV(p); m = BDY(p);
                   4409:         d = DL(m0);
                   4410:         len = LEN(p);
                   4411:         mr0 = 0;
                   4412:         td = TD(d);
                   4413:         if ( mod == -1 ) {
                   4414:             c = CM(m0);
                   4415:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4416:                 NEXTNM(mr0,mr);
                   4417:                 CM(mr) = _mulsf(CM(m),c);
                   4418:                 ndl_add(DL(m),d,DL(mr));
                   4419:             }
                   4420:         } else if ( mod ) {
                   4421:             c = CM(m0);
                   4422:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4423:                 NEXTNM(mr0,mr);
                   4424:                 c1 = CM(m);
                   4425:                 DMAR(c1,c,0,mod,c2);
                   4426:                 CM(mr) = c2;
                   4427:                 ndl_add(DL(m),d,DL(mr));
                   4428:             }
                   4429:         } else {
                   4430:             q = CP(m0);
                   4431:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4432:                 NEXTNM(mr0,mr);
                   4433:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4434:                 ndl_add(DL(m),d,DL(mr));
                   4435:             }
                   4436:         }
                   4437:         NEXT(mr) = 0;
                   4438:         MKND(NV(p),mr0,len,r);
                   4439:         SG(r) = SG(p) + TD(d);
                   4440:         return r;
                   4441:     }
1.4       noro     4442: }
                   4443:
1.104     noro     4444: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4445: {
1.157     noro     4446:     NM mq0,mq;
                   4447:     NMV tm;
                   4448:     Q q;
                   4449:     int i,nv,sg,c,c1,c2,hindex;
                   4450:     ND p,t,r;
                   4451:     N tnm;
                   4452:
                   4453:     if ( bucket->m < 0 ) return 0;
                   4454:     else {
                   4455:         nv = NV(d);
                   4456:         mq0 = 0;
                   4457:         tm = (NMV)ALLOCA(nmv_adv);
                   4458:         while ( 1 ) {
                   4459:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4460:             if ( hindex < 0 ) break;
                   4461:             p = bucket->body[hindex];
                   4462:             NEXTNM(mq0,mq);
                   4463:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4464:             ndl_copy(DL(mq),DL(tm));
                   4465:             if ( mod ) {
                   4466:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4467:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4468:                 CM(tm) = mod-c;
                   4469:             } else {
                   4470:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4471:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4472:                 chsgnq(CQ(mq),&CQ(tm));
                   4473:             }
                   4474:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4475:             bucket->body[hindex] = nd_remove_head(p);
                   4476:             t = nd_remove_head(t);
                   4477:             add_pbucket(mod,bucket,t);
                   4478:         }
                   4479:         if ( !mq0 )
                   4480:             r = 0;
                   4481:         else {
                   4482:             NEXT(mq) = 0;
                   4483:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4484:             MKND(nv,mq0,i,r);
                   4485:             /* XXX */
                   4486:             SG(r) = HTD(r);
                   4487:         }
                   4488:         return r;
                   4489:     }
1.99      noro     4490: }
                   4491:
1.43      noro     4492: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4493: {
1.157     noro     4494:     NMV m,mr,mr0,t;
                   4495:     int len,i,k;
1.11      noro     4496:
1.157     noro     4497:     if ( !p ) return;
                   4498:     m = BDY(p); len = LEN(p);
                   4499:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4500:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4501:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4502:     t = (NMV)ALLOCA(nmv_adv);
                   4503:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4504:         CQ(t) = CQ(m);
                   4505:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4506:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4507:         CQ(mr) = CQ(t);
                   4508:         ndl_copy(DL(t),DL(mr));
                   4509:     }
                   4510:     BDY(p) = mr0;
1.61      noro     4511: }
                   4512:
                   4513: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4514: {
1.157     noro     4515:     NMV m,mr,mr0;
                   4516:     int len,i;
                   4517:     NDV r;
                   4518:
                   4519:     if ( !p ) return 0;
                   4520:     m = BDY(p); len = LEN(p);
                   4521:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4522:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4523:         ndl_zero(DL(mr));
                   4524:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4525:         CQ(mr) = CQ(m);
                   4526:     }
                   4527:     MKNDV(NV(p),mr0,len,r);
                   4528:     SG(r) = SG(p);
                   4529:     return r;
1.11      noro     4530: }
                   4531:
1.61      noro     4532: /* duplicate p */
                   4533:
                   4534: NDV ndv_dup(int mod,NDV p)
1.3       noro     4535: {
1.157     noro     4536:     NDV d;
                   4537:     NMV t,m,m0;
                   4538:     int i,len;
                   4539:
                   4540:     if ( !p ) return 0;
                   4541:     len = LEN(p);
                   4542:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4543:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4544:         ndl_copy(DL(t),DL(m));
                   4545:         CQ(m) = CQ(t);
                   4546:     }
                   4547:     MKNDV(NV(p),m0,len,d);
                   4548:     SG(d) = SG(p);
                   4549:     return d;
1.23      noro     4550: }
                   4551:
1.63      noro     4552: ND nd_dup(ND p)
                   4553: {
1.157     noro     4554:     ND d;
                   4555:     NM t,m,m0;
1.63      noro     4556:
1.157     noro     4557:     if ( !p ) return 0;
                   4558:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4559:         NEXTNM(m0,m);
                   4560:         ndl_copy(DL(t),DL(m));
                   4561:         CQ(m) = CQ(t);
                   4562:     }
                   4563:     if ( m0 ) NEXT(m) = 0;
                   4564:     MKND(NV(p),m0,LEN(p),d);
                   4565:     SG(d) = SG(p);
                   4566:     return d;
1.63      noro     4567: }
                   4568:
1.61      noro     4569: /* XXX if p->len == 0 then it represents 0 */
                   4570:
                   4571: void ndv_mod(int mod,NDV p)
                   4572: {
1.157     noro     4573:     NMV t,d;
                   4574:     int r,s,u;
                   4575:     int i,len,dlen;
                   4576:     P cp;
                   4577:     Q c;
                   4578:     Obj gfs;
                   4579:
                   4580:     if ( !p ) return;
                   4581:     len = LEN(p);
                   4582:     dlen = 0;
                   4583:     if ( mod == -1 )
                   4584:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4585:             simp_ff((Obj)CP(t),&gfs);
                   4586:             r = FTOIF(CONT((GFS)gfs));
                   4587:             CM(d) = r;
                   4588:             ndl_copy(DL(t),DL(d));
                   4589:             NMV_ADV(d);
                   4590:             dlen++;
                   4591:         }
                   4592:     else
                   4593:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4594:             if ( nd_vc ) {
                   4595:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4596:                 c = (Q)cp;
                   4597:             } else
                   4598:                 c = CQ(t);
                   4599:             r = rem(NM(c),mod);
                   4600:             if ( r ) {
                   4601:                 if ( SGN(c) < 0 )
                   4602:                     r = mod-r;
                   4603:                 if ( DN(c) ) {
                   4604:                     s = rem(DN(c),mod);
                   4605:                     if ( !s )
                   4606:                         error("ndv_mod : division by 0");
                   4607:                     s = invm(s,mod);
                   4608:                     DMAR(r,s,0,mod,u); r = u;
                   4609:                 }
                   4610:                 CM(d) = r;
                   4611:                 ndl_copy(DL(t),DL(d));
                   4612:                 NMV_ADV(d);
                   4613:                 dlen++;
                   4614:             }
                   4615:         }
                   4616:     LEN(p) = dlen;
1.61      noro     4617: }
                   4618:
                   4619: NDV ptondv(VL vl,VL dvl,P p)
                   4620: {
1.157     noro     4621:     ND nd;
                   4622:
                   4623:     nd = ptond(vl,dvl,p);
                   4624:     return ndtondv(0,nd);
                   4625: }
1.61      noro     4626:
1.157     noro     4627: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4628: {
                   4629:     NODE nd,nd1;
                   4630:     int n;
                   4631:     P *pl;
                   4632:     Q *cl;
                   4633:     int i;
                   4634:     P dmy;
                   4635:     Q dvr;
                   4636:     LIST r;
                   4637:
                   4638:     nd = BDY(l); n = length(nd);
                   4639:     pl = (P *)ALLOCA(n*sizeof(P));
                   4640:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4641:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4642:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4643:     qltozl(cl,n,&dvr);
                   4644:     nd = BDY(l);
                   4645:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4646:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4647:     }
                   4648:     nd = 0;
                   4649:     for ( i = n-1; i >= 0; i-- ) {
                   4650:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4651:     }
                   4652:     MKLIST(r,nd);
                   4653:     *pp = r;
                   4654: }
                   4655:
                   4656: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4657:
                   4658: NDV pltondv(VL vl,VL dvl,LIST p)
                   4659: {
                   4660:     int i;
                   4661:     NODE t;
                   4662:     ND r,ri;
                   4663:     NM m;
                   4664:
                   4665:     if ( !nd_module ) error("pltond : module order must be set");
                   4666:     r = 0;
                   4667:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4668:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4669:         if ( ri )
                   4670:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4671:                 MPOS(DL(m)) = i;
                   4672:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4673:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4674:             }
1.157     noro     4675:         r = nd_add(0,r,ri);
                   4676:     }
                   4677:     return ndtondv(0,r);
1.61      noro     4678: }
                   4679:
                   4680: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4681: {
1.157     noro     4682:     int n,i,j,k,e;
                   4683:     VL tvl;
                   4684:     V v;
                   4685:     DCP dc;
                   4686:     DCP *w;
                   4687:     ND r,s,t,u;
                   4688:     P x;
                   4689:     int c;
                   4690:     UINT *d;
                   4691:     NM m,m0;
                   4692:
                   4693:     if ( !p )
                   4694:         return 0;
                   4695:     else if ( NUM(p) ) {
                   4696:         NEWNM(m);
                   4697:         ndl_zero(DL(m));
                   4698:         CQ(m) = (Q)p;
                   4699:         NEXT(m) = 0;
                   4700:         MKND(nd_nvar,m,1,r);
                   4701:         SG(r) = 0;
                   4702:         return r;
                   4703:     } else {
                   4704:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4705:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4706:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4707:         for ( i = 0, tvl = dvl, v = VR(p);
                   4708:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4709:         if ( !tvl ) {
                   4710:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4711:                 t = ptond(vl,dvl,COEF(w[j]));
                   4712:                 pwrp(vl,x,DEG(w[j]),&p);
                   4713:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4714:             }
                   4715:             return s;
                   4716:         } else {
                   4717:             NEWNM(m0); d = DL(m0);
                   4718:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4719:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4720:                 TD(d) = MUL_WEIGHT(e,i);
                   4721:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4722:                 if ( nd_module ) MPOS(d) = 0;
                   4723:                 t = ptond(vl,dvl,COEF(w[j]));
                   4724:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4725:                     ndl_addto(DL(m),d);
                   4726:                 SG(t) += TD(d);
                   4727:                 s = nd_add(0,s,t);
                   4728:             }
                   4729:             FREENM(m0);
                   4730:             return s;
                   4731:         }
                   4732:     }
1.61      noro     4733: }
                   4734:
                   4735: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4736: {
1.157     noro     4737:     VL tvl;
                   4738:     int len,n,j,i,e;
                   4739:     NMV m;
                   4740:     Q q;
                   4741:     P c;
                   4742:     UINT *d;
                   4743:     P s,r,u,t,w;
                   4744:     GFS gfs;
                   4745:
                   4746:     if ( !p ) return 0;
                   4747:     else {
                   4748:         len = LEN(p);
                   4749:         n = NV(p);
                   4750:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4751:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4752:             if ( mod == -1 ) {
                   4753:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4754:             } else if ( mod ) {
                   4755:                 STOQ(CM(m),q); c = (P)q;
                   4756:             } else
                   4757:                 c = CP(m);
                   4758:             d = DL(m);
                   4759:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4760:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4761:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4762:             }
                   4763:             addp(vl,s,t,&u); s = u;
                   4764:         }
                   4765:         return s;
                   4766:     }
                   4767: }
                   4768:
                   4769: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4770: {
                   4771:     VL tvl;
                   4772:     int len,n,j,i,e;
                   4773:     NMV m;
                   4774:     Q q;
                   4775:     P c;
                   4776:     UINT *d;
                   4777:     P s,r,u,t,w;
                   4778:     GFS gfs;
                   4779:     P *a;
                   4780:     LIST l;
                   4781:     NODE nd,nd1;
                   4782:
                   4783:     if ( !p ) return 0;
                   4784:     else {
                   4785:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4786:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4787:         len = LEN(p);
                   4788:         n = NV(p);
                   4789:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4790:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4791:             if ( mod == -1 ) {
                   4792:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4793:             } else if ( mod ) {
                   4794:                 STOQ(CM(m),q); c = (P)q;
                   4795:             } else
                   4796:                 c = CP(m);
                   4797:             d = DL(m);
                   4798:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4799:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4800:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4801:             }
                   4802:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4803:         }
                   4804:         nd = 0;
                   4805:         for ( i = rank; i > 0; i-- ) {
                   4806:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4807:         }
                   4808:         MKLIST(l,nd);
                   4809:         return l;
                   4810:     }
1.3       noro     4811: }
                   4812:
1.61      noro     4813: NDV ndtondv(int mod,ND p)
1.11      noro     4814: {
1.157     noro     4815:     NDV d;
                   4816:     NMV m,m0;
                   4817:     NM t;
                   4818:     int i,len;
                   4819:
                   4820:     if ( !p ) return 0;
                   4821:     len = LEN(p);
                   4822:     if ( mod )
1.200     noro     4823:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     4824:     else
                   4825:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4826: #if 0
1.157     noro     4827:     ndv_alloc += nmv_adv*len;
1.103     noro     4828: #endif
1.157     noro     4829:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4830:         ndl_copy(DL(t),DL(m));
                   4831:         CQ(m) = CQ(t);
                   4832:     }
                   4833:     MKNDV(NV(p),m0,len,d);
                   4834:     SG(d) = SG(p);
                   4835:     return d;
1.11      noro     4836: }
                   4837:
1.61      noro     4838: ND ndvtond(int mod,NDV p)
1.11      noro     4839: {
1.157     noro     4840:     ND d;
                   4841:     NM m,m0;
                   4842:     NMV t;
                   4843:     int i,len;
                   4844:
                   4845:     if ( !p ) return 0;
                   4846:     m0 = 0;
                   4847:     len = p->len;
                   4848:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4849:         NEXTNM(m0,m);
                   4850:         ndl_copy(DL(t),DL(m));
                   4851:         CQ(m) = CQ(t);
                   4852:     }
                   4853:     NEXT(m) = 0;
                   4854:     MKND(NV(p),m0,len,d);
                   4855:     SG(d) = SG(p);
                   4856:     return d;
1.11      noro     4857: }
                   4858:
1.198     noro     4859: DP ndvtodp(int mod,NDV p)
                   4860: {
                   4861:     MP m,m0;
                   4862:        DP d;
                   4863:     NMV t;
                   4864:     int i,len;
                   4865:
                   4866:     if ( !p ) return 0;
                   4867:     m0 = 0;
                   4868:     len = p->len;
                   4869:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4870:         NEXTMP(m0,m);
                   4871:        m->dl = ndltodl(nd_nvar,DL(t));
                   4872:        m->c = ndctop(mod,t->c);
                   4873:     }
                   4874:     NEXT(m) = 0;
                   4875:        MKDP(nd_nvar,m0,d);
                   4876:     SG(d) = SG(p);
                   4877:     return d;
                   4878: }
                   4879:
1.3       noro     4880: void ndv_print(NDV p)
                   4881: {
1.157     noro     4882:     NMV m;
                   4883:     int i,len;
1.3       noro     4884:
1.157     noro     4885:     if ( !p ) printf("0\n");
                   4886:     else {
                   4887:         len = LEN(p);
                   4888:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4889:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   4890:             else printf("+%d*",CM(m));
                   4891:             ndl_print(DL(m));
                   4892:         }
                   4893:         printf("\n");
                   4894:     }
1.16      noro     4895: }
                   4896:
1.113     noro     4897: void ndv_print_q(NDV p)
1.16      noro     4898: {
1.157     noro     4899:     NMV m;
                   4900:     int i,len;
1.16      noro     4901:
1.157     noro     4902:     if ( !p ) printf("0\n");
                   4903:     else {
                   4904:         len = LEN(p);
                   4905:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4906:             printf("+");
                   4907:             printexpr(CO,(Obj)CQ(m));
                   4908:             printf("*");
                   4909:             ndl_print(DL(m));
                   4910:         }
                   4911:         printf("\n");
                   4912:     }
1.25      noro     4913: }
                   4914:
1.167     noro     4915: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     4916: {
1.157     noro     4917:     int len,i,j;
1.167     noro     4918:     NDVI w;
1.157     noro     4919:     NODE t,t0;
                   4920:
                   4921:     len = length(x);
1.167     noro     4922:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   4923:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   4924:         w[i].p = BDY(t); w[i].i = perm[i];
                   4925:     }
1.157     noro     4926:     for ( i = 0; i < len; i++ ) {
                   4927:         for ( j = 0; j < i; j++ ) {
1.167     noro     4928:             if ( w[i].p && w[j].p )
                   4929:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   4930:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     4931:         }
                   4932:     }
1.167     noro     4933:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   4934:         if ( w[i].p ) {
                   4935:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   4936:             perm[j++] = w[i].i;
                   4937:         }
1.157     noro     4938:     }
                   4939:     NEXT(t) = 0; x = t0;
                   4940:     return x;
1.11      noro     4941: }
1.32      noro     4942:
1.43      noro     4943: /* XXX incomplete */
                   4944:
1.32      noro     4945: void nd_init_ord(struct order_spec *ord)
                   4946: {
1.157     noro     4947:     nd_module = (ord->id >= 256);
                   4948:     switch ( ord->id ) {
                   4949:         case 0:
                   4950:             switch ( ord->ord.simple ) {
                   4951:                 case 0:
                   4952:                     nd_dcomp = 1;
                   4953:                     nd_isrlex = 1;
                   4954:                     break;
                   4955:                 case 1:
                   4956:                     nd_dcomp = 1;
                   4957:                     nd_isrlex = 0;
                   4958:                     break;
                   4959:                 case 2:
                   4960:                     nd_dcomp = 0;
                   4961:                     nd_isrlex = 0;
                   4962:                     ndl_compare_function = ndl_lex_compare;
                   4963:                     break;
                   4964:                 case 11:
                   4965:                     /* XXX */
                   4966:                     nd_dcomp = 0;
                   4967:                     nd_isrlex = 1;
                   4968:                     ndl_compare_function = ndl_ww_lex_compare;
                   4969:                     break;
                   4970:                 default:
                   4971:                     error("nd_gr : unsupported order");
                   4972:             }
                   4973:             break;
                   4974:         case 1:
                   4975:             /* block order */
                   4976:             /* XXX */
                   4977:             nd_dcomp = -1;
                   4978:             nd_isrlex = 0;
                   4979:             ndl_compare_function = ndl_block_compare;
                   4980:             break;
                   4981:         case 2:
                   4982:             /* matrix order */
                   4983:             /* XXX */
                   4984:             nd_dcomp = -1;
                   4985:             nd_isrlex = 0;
                   4986:             nd_matrix_len = ord->ord.matrix.row;
                   4987:             nd_matrix = ord->ord.matrix.matrix;
                   4988:             ndl_compare_function = ndl_matrix_compare;
                   4989:             break;
                   4990:         case 3:
                   4991:             /* composite order */
                   4992:             nd_dcomp = -1;
                   4993:             nd_isrlex = 0;
                   4994:             nd_worb_len = ord->ord.composite.length;
                   4995:             nd_worb = ord->ord.composite.w_or_b;
                   4996:             ndl_compare_function = ndl_composite_compare;
                   4997:             break;
                   4998:
                   4999:         /* module order */
                   5000:         case 256:
1.160     noro     5001:             nd_ispot = ord->ispot;
1.174     noro     5002:             nd_pot_nelim = ord->pot_nelim;
1.157     noro     5003:             nd_dcomp = -1;
                   5004:             switch ( ord->ord.simple ) {
                   5005:                 case 0:
1.167     noro     5006:                     nd_isrlex = 1;
1.157     noro     5007:                     ndl_compare_function = ndl_module_grlex_compare;
                   5008:                     break;
                   5009:                 case 1:
1.167     noro     5010:                     nd_isrlex = 0;
1.157     noro     5011:                     ndl_compare_function = ndl_module_glex_compare;
                   5012:                     break;
                   5013:                 case 2:
1.167     noro     5014:                     nd_isrlex = 0;
1.157     noro     5015:                     ndl_compare_function = ndl_module_lex_compare;
                   5016:                     break;
                   5017:                 default:
                   5018:                     error("nd_gr : unsupported order");
                   5019:             }
                   5020:             break;
                   5021:         case 257:
                   5022:             /* block order */
1.174     noro     5023:             nd_ispot = ord->ispot;
                   5024:             nd_pot_nelim = ord->pot_nelim;
                   5025:             nd_dcomp = -1;
                   5026:             nd_isrlex = 0;
1.157     noro     5027:             ndl_compare_function = ndl_module_block_compare;
                   5028:             break;
                   5029:         case 258:
                   5030:             /* matrix order */
1.174     noro     5031:             nd_ispot = ord->ispot;
                   5032:             nd_pot_nelim = ord->pot_nelim;
                   5033:             nd_dcomp = -1;
                   5034:             nd_isrlex = 0;
1.157     noro     5035:             nd_matrix_len = ord->ord.matrix.row;
                   5036:             nd_matrix = ord->ord.matrix.matrix;
                   5037:             ndl_compare_function = ndl_module_matrix_compare;
                   5038:             break;
                   5039:         case 259:
                   5040:             /* composite order */
1.174     noro     5041:             nd_ispot = ord->ispot;
                   5042:             nd_pot_nelim = ord->pot_nelim;
                   5043:             nd_dcomp = -1;
                   5044:             nd_isrlex = 0;
1.157     noro     5045:             nd_worb_len = ord->ord.composite.length;
                   5046:             nd_worb = ord->ord.composite.w_or_b;
                   5047:             ndl_compare_function = ndl_module_composite_compare;
                   5048:             break;
                   5049:     }
                   5050:     nd_ord = ord;
1.32      noro     5051: }
                   5052:
1.43      noro     5053: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5054: {
1.157     noro     5055:     int n,i,j,s,l;
                   5056:     UINT *t;
                   5057:     BlockMask bm;
                   5058:
                   5059:     /* we only create mask table for block order */
1.164     noro     5060:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5061:         return 0;
                   5062:     n = ord->ord.block.length;
                   5063:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5064:     bm->n = n;
                   5065:     bm->order_pair = ord->ord.block.order_pair;
                   5066:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5067:     for ( i = 0, s = 0; i < n; i++ ) {
                   5068:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5069:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5070:         l = bm->order_pair[i].length;
                   5071:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5072:     }
                   5073:     return bm;
1.57      noro     5074: }
                   5075:
                   5076: EPOS nd_create_epos(struct order_spec *ord)
                   5077: {
1.157     noro     5078:     int i,j,l,s,ord_l,ord_o;
                   5079:     EPOS epos;
                   5080:     struct order_pair *op;
                   5081:
                   5082:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5083:     switch ( ord->id ) {
1.164     noro     5084:         case 0: case 256:
1.157     noro     5085:             if ( nd_isrlex ) {
                   5086:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5087:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5088:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5089:                 }
                   5090:             } else {
                   5091:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5092:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5093:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5094:                 }
                   5095:             }
                   5096:             break;
1.164     noro     5097:         case 1: case 257:
1.157     noro     5098:             /* block order */
                   5099:             l = ord->ord.block.length;
                   5100:             op = ord->ord.block.order_pair;
                   5101:             for ( j = 0, s = 0; j < l; j++ ) {
                   5102:                 ord_o = op[j].order;
                   5103:                 ord_l = op[j].length;
                   5104:                 if ( !ord_o )
                   5105:                     for ( i = 0; i < ord_l; i++ ) {
                   5106:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5107:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5108:                     }
                   5109:                 else
                   5110:                     for ( i = 0; i < ord_l; i++ ) {
                   5111:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5112:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5113:                     }
                   5114:                 s += ord_l;
                   5115:             }
                   5116:             break;
                   5117:         case 2:
                   5118:             /* matrix order */
                   5119:         case 3:
                   5120:             /* composite order */
1.167     noro     5121:         default:
1.157     noro     5122:             for ( i = 0; i < nd_nvar; i++ ) {
                   5123:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5124:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5125:             }
                   5126:             break;
                   5127:     }
                   5128:     return epos;
1.43      noro     5129: }
1.59      noro     5130:
                   5131: /* external interface */
                   5132:
1.191     noro     5133: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5134: {
1.157     noro     5135:     NODE t,in0,in;
1.191     noro     5136:     ND ndf,nf;
                   5137:     NDV ndvf;
1.157     noro     5138:     VL vv,tv;
1.191     noro     5139:     int stat,nvar,max,mrank;
1.157     noro     5140:     union oNDC dn;
                   5141:     Q cont;
                   5142:     P pp;
1.191     noro     5143:     LIST ppl;
1.157     noro     5144:
                   5145:     if ( !f ) {
                   5146:         *rp = 0;
                   5147:         return;
                   5148:     }
                   5149:     pltovl(v,&vv);
                   5150:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5151:
1.191     noro     5152:     /* max=65536 implies nd_bpe=32 */
                   5153:     max = 65536;
1.157     noro     5154:
1.191     noro     5155:        nd_module = 0;
                   5156:        /* nd_module will be set if ord is a module ordering */
1.157     noro     5157:     nd_init_ord(ord);
                   5158:     nd_setup_parameters(nvar,max);
1.191     noro     5159:     if ( nd_module && OID(f) != O_LIST )
                   5160:         error("nd_nf_p : the first argument must be a list");
                   5161:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5162:     /* conversion to ndv */
                   5163:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5164:         NEXTNODE(in0,in);
1.191     noro     5165:         if ( nd_module ) {
                   5166:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5167:                || length(BDY((LIST)BDY(t))) != mrank )
                   5168:               error("nd_nf_p : inconsistent basis element");
                   5169:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5170:           else ppl = (LIST)BDY(t);
                   5171:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5172:         } else {
                   5173:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5174:           else pp = (P)BDY(t);
                   5175:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5176:         }
1.157     noro     5177:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5178:     }
1.191     noro     5179:     if ( in0 ) NEXT(in) = 0;
                   5180:
                   5181:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5182:     else ndvf = ptondv(CO,vv,(P)f);
                   5183:     if ( m ) ndv_mod(m,ndvf);
                   5184:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5185:
                   5186:     /* dont sort, dont removecont */
                   5187:     ndv_setup(m,0,in0,1,1);
                   5188:     nd_scale=2;
1.191     noro     5189:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5190:     if ( !stat )
                   5191:         error("nd_nf_p : exponent too large");
                   5192:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5193:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5194: }
                   5195:
                   5196: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5197: {
1.157     noro     5198:     NM m;
                   5199:     UINT *t,*s;
                   5200:     int i;
                   5201:
                   5202:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5203:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5204:         t = DL(m);
                   5205:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5206:         r[i] = CM(m);
                   5207:     }
                   5208:     for ( i = 0; !r[i]; i++ );
                   5209:     return i;
1.63      noro     5210: }
                   5211:
1.113     noro     5212: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5213: {
1.157     noro     5214:     NM m;
                   5215:     UINT *t,*s;
                   5216:     int i;
                   5217:
                   5218:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5219:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5220:         t = DL(m);
                   5221:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5222:         r[i] = CQ(m);
                   5223:     }
                   5224:     for ( i = 0; !r[i]; i++ );
                   5225:     return i;
1.74      noro     5226: }
                   5227:
1.129     noro     5228: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5229: {
1.157     noro     5230:     NM m;
                   5231:     NMV mr;
                   5232:     UINT *d,*t,*s;
                   5233:     NDV p;
                   5234:     int i,j,len;
                   5235:     Q *r;
                   5236:
                   5237:     m = pair->mul;
                   5238:     d = DL(m);
                   5239:     p = nd_ps[pair->index];
                   5240:     len = LEN(p);
                   5241:     r = (Q *)CALLOC(n,sizeof(Q));
                   5242:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5243:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5244:         ndl_add(d,DL(mr),t);
                   5245:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5246:         r[i] = CQ(mr);
                   5247:     }
                   5248:     return r;
1.129     noro     5249: }
                   5250:
1.67      noro     5251: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     5252: {
1.157     noro     5253:     NM m;
                   5254:     NMV mr;
                   5255:     UINT *d,*t,*s;
                   5256:     NDV p;
                   5257:     unsigned char *ivc;
                   5258:     unsigned short *ivs;
                   5259:     UINT *v,*ivi,*s0v;
                   5260:     int i,j,len,prev,diff,cdiff;
                   5261:     IndArray r;
1.198     noro     5262: struct oEGT eg0,eg1;
1.157     noro     5263:
                   5264:     m = pair->mul;
                   5265:     d = DL(m);
                   5266:     p = nd_ps[pair->index];
                   5267:     len = LEN(p);
                   5268:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5269:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.198     noro     5270: get_eg(&eg0);
1.157     noro     5271:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5272:         ndl_add(d,DL(mr),t);
                   5273:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5274:         v[j] = i;
                   5275:     }
1.198     noro     5276: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5277:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5278:     r->head = v[0];
                   5279:     diff = 0;
                   5280:     for ( i = 1; i < len; i++ ) {
                   5281:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5282:     }
                   5283:     if ( diff < 256 ) {
                   5284:         r->width = 1;
                   5285:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5286:         r->index.c = ivc;
                   5287:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5288:     } else if ( diff < 65536 ) {
                   5289:         r->width = 2;
                   5290:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5291:         r->index.s = ivs;
                   5292:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5293:     } else {
                   5294:         r->width = 4;
                   5295:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5296:         r->index.i = ivi;
                   5297:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5298:     }
                   5299:     return r;
1.64      noro     5300: }
                   5301:
1.135     noro     5302: int compress_array(Q *svect,Q *cvect,int n)
                   5303: {
1.157     noro     5304:     int i,j;
1.135     noro     5305:
1.157     noro     5306:     for ( i = j = 0; i < n; i++ )
                   5307:         if ( svect[i] ) cvect[j++] = svect[i];
                   5308:     return j;
1.135     noro     5309: }
                   5310:
                   5311: void expand_array(Q *svect,Q *cvect,int n)
                   5312: {
1.157     noro     5313:     int i,j;
1.135     noro     5314:
1.157     noro     5315:     for ( i = j = 0; j < n;  i++  )
                   5316:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5317: }
                   5318:
1.133     noro     5319: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5320: {
1.157     noro     5321:     int i,j,k,len,pos,prev,nz;
                   5322:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5323:     IndArray ivect;
                   5324:     unsigned char *ivc;
                   5325:     unsigned short *ivs;
                   5326:     unsigned int *ivi;
                   5327:     NDV redv;
                   5328:     NMV mr;
                   5329:     NODE rp;
                   5330:     int maxrs;
                   5331:     double hmag;
                   5332:     Q *cvect;
                   5333:
                   5334:     maxrs = 0;
                   5335:     for ( i = 0; i < col && !svect[i]; i++ );
                   5336:     if ( i == col ) return maxrs;
                   5337:     hmag = p_mag((P)svect[i])*nd_scale;
                   5338:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5339:     for ( i = 0; i < nred; i++ ) {
                   5340:         ivect = imat[i];
                   5341:         k = ivect->head;
                   5342:         if ( svect[k] ) {
                   5343:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5344:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5345:             len = LEN(redv); mr = BDY(redv);
                   5346:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5347:             chsgnq(cs,&mcs);
                   5348:             if ( !UNIQ(cr) ) {
                   5349:                 for ( j = 0; j < col; j++ ) {
                   5350:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5351:                 }
                   5352:             }
                   5353:             svect[k] = 0; prev = k;
                   5354:             switch ( ivect->width ) {
                   5355:                 case 1:
                   5356:                     ivc = ivect->index.c;
                   5357:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5358:                         pos = prev+ivc[j]; prev = pos;
                   5359:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5360:                     }
                   5361:                     break;
                   5362:                 case 2:
                   5363:                     ivs = ivect->index.s;
                   5364:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5365:                         pos = prev+ivs[j]; prev = pos;
                   5366:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5367:                     }
                   5368:                     break;
                   5369:                 case 4:
                   5370:                     ivi = ivect->index.i;
                   5371:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5372:                         pos = prev+ivi[j]; prev = pos;
                   5373:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5374:                     }
                   5375:                     break;
                   5376:             }
                   5377:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5378:             if ( j == col ) break;
                   5379:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5380:                 nz = compress_array(svect,cvect,col);
                   5381:                 removecont_array((P *)cvect,nz,1);
                   5382:                 expand_array(svect,cvect,nz);
                   5383:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5384:             }
                   5385:         }
                   5386:     }
                   5387:     nz = compress_array(svect,cvect,col);
                   5388:     removecont_array((P *)cvect,nz,1);
                   5389:     expand_array(svect,cvect,nz);
                   5390:     if ( DP_Print ) {
                   5391:         fprintf(asir_out,"-"); fflush(asir_out);
                   5392:     }
                   5393:     return maxrs;
1.107     noro     5394: }
                   5395:
1.76      noro     5396: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5397: {
1.157     noro     5398:     int i,j,k,len,pos,prev;
                   5399:     UINT c,c1,c2,c3,up,lo,dmy;
                   5400:     IndArray ivect;
                   5401:     unsigned char *ivc;
                   5402:     unsigned short *ivs;
                   5403:     unsigned int *ivi;
                   5404:     NDV redv;
                   5405:     NMV mr;
                   5406:     NODE rp;
                   5407:     int maxrs;
                   5408:
                   5409:     maxrs = 0;
                   5410:     for ( i = 0; i < nred; i++ ) {
                   5411:         ivect = imat[i];
                   5412:         k = ivect->head; svect[k] %= m;
                   5413:         if ( c = svect[k] ) {
                   5414:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5415:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5416:             len = LEN(redv); mr = BDY(redv);
                   5417:             svect[k] = 0; prev = k;
                   5418:             switch ( ivect->width ) {
                   5419:                 case 1:
                   5420:                     ivc = ivect->index.c;
                   5421:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5422:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5423:                         prev = pos;
                   5424:                         DMA(c1,c,c2,up,lo);
                   5425:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5426:                         } else svect[pos] = lo;
                   5427:                     }
                   5428:                     break;
                   5429:                 case 2:
                   5430:                     ivs = ivect->index.s;
                   5431:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5432:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5433:                         prev = pos;
                   5434:                         DMA(c1,c,c2,up,lo);
                   5435:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5436:                         } else svect[pos] = lo;
                   5437:                     }
                   5438:                     break;
                   5439:                 case 4:
                   5440:                     ivi = ivect->index.i;
                   5441:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5442:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5443:                         prev = pos;
                   5444:                         DMA(c1,c,c2,up,lo);
                   5445:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5446:                         } else svect[pos] = lo;
                   5447:                     }
                   5448:                     break;
                   5449:             }
                   5450:         }
                   5451:     }
                   5452:     for ( i = 0; i < col; i++ )
                   5453:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5454:     return maxrs;
1.65      noro     5455: }
                   5456:
1.76      noro     5457: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5458: {
1.157     noro     5459:     int i,j,k,len,pos,prev;
                   5460:     UINT c,c1,c2,c3,up,lo,dmy;
                   5461:     IndArray ivect;
                   5462:     unsigned char *ivc;
                   5463:     unsigned short *ivs;
                   5464:     unsigned int *ivi;
                   5465:     NDV redv;
                   5466:     NMV mr;
                   5467:     NODE rp;
                   5468:     int maxrs;
                   5469:
                   5470:     maxrs = 0;
                   5471:     for ( i = 0; i < nred; i++ ) {
                   5472:         ivect = imat[i];
                   5473:         k = ivect->head; svect[k] %= m;
                   5474:         if ( c = svect[k] ) {
                   5475:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5476:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5477:             len = LEN(redv); mr = BDY(redv);
                   5478:             svect[k] = 0; prev = k;
                   5479:             switch ( ivect->width ) {
                   5480:                 case 1:
                   5481:                     ivc = ivect->index.c;
                   5482:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5483:                         pos = prev+ivc[j]; prev = pos;
                   5484:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5485:                     }
                   5486:                     break;
                   5487:                 case 2:
                   5488:                     ivs = ivect->index.s;
                   5489:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5490:                         pos = prev+ivs[j]; prev = pos;
                   5491:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5492:                     }
                   5493:                     break;
                   5494:                 case 4:
                   5495:                     ivi = ivect->index.i;
                   5496:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5497:                         pos = prev+ivi[j]; prev = pos;
                   5498:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5499:                     }
                   5500:                     break;
                   5501:             }
                   5502:         }
                   5503:     }
                   5504:     return maxrs;
1.72      noro     5505: }
                   5506:
1.65      noro     5507: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5508: {
1.157     noro     5509:     int j,k,len;
                   5510:     UINT *p;
                   5511:     UINT c;
                   5512:     NDV r;
                   5513:     NMV mr0,mr;
                   5514:
                   5515:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5516:     if ( !len ) return 0;
                   5517:     else {
1.200     noro     5518:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     5519: #if 0
1.157     noro     5520:         ndv_alloc += nmv_adv*len;
1.103     noro     5521: #endif
1.157     noro     5522:         mr = mr0;
                   5523:         p = s0vect;
                   5524:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5525:             if ( !rhead[j] ) {
                   5526:                 if ( c = vect[k++] ) {
                   5527:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5528:                 }
                   5529:             }
                   5530:         MKNDV(nd_nvar,mr0,len,r);
                   5531:         return r;
                   5532:     }
1.65      noro     5533: }
                   5534:
1.129     noro     5535: /* for preprocessed vector */
                   5536:
1.113     noro     5537: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5538: {
1.157     noro     5539:     int j,k,len;
                   5540:     UINT *p;
                   5541:     Q c;
                   5542:     NDV r;
                   5543:     NMV mr0,mr;
                   5544:
                   5545:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5546:     if ( !len ) return 0;
                   5547:     else {
1.200     noro     5548:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     5549: #if 0
1.157     noro     5550:         ndv_alloc += nmv_adv*len;
1.107     noro     5551: #endif
1.157     noro     5552:         mr = mr0;
                   5553:         p = s0vect;
                   5554:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5555:             if ( !rhead[j] ) {
                   5556:                 if ( c = vect[k++] ) {
                   5557:                     if ( DN(c) )
                   5558:                         error("afo");
                   5559:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5560:                 }
                   5561:             }
                   5562:         MKNDV(nd_nvar,mr0,len,r);
                   5563:         return r;
                   5564:     }
1.107     noro     5565: }
                   5566:
1.129     noro     5567: /* for plain vector */
                   5568:
                   5569: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5570: {
1.157     noro     5571:     int j,k,len;
                   5572:     UINT *p;
                   5573:     Q c;
                   5574:     NDV r;
                   5575:     NMV mr0,mr;
                   5576:
                   5577:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5578:     if ( !len ) return 0;
                   5579:     else {
1.200     noro     5580:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     5581: #if 0
1.157     noro     5582:         ndv_alloc += nmv_adv*len;
1.129     noro     5583: #endif
1.157     noro     5584:         mr = mr0;
                   5585:         p = s0vect;
                   5586:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5587:             if ( c = vect[k] ) {
                   5588:                 if ( DN(c) )
                   5589:                     error("afo");
                   5590:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5591:             }
                   5592:         MKNDV(nd_nvar,mr0,len,r);
                   5593:         return r;
                   5594:     }
1.129     noro     5595: }
                   5596:
1.133     noro     5597: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5598: {
1.157     noro     5599:     ND_pairs t;
                   5600:     NODE sp0,sp;
                   5601:     int stat;
                   5602:     ND spol;
                   5603:
                   5604:     for ( t = l; t; t = NEXT(t) ) {
                   5605:         stat = nd_sp(m,trace,t,&spol);
                   5606:         if ( !stat ) return 0;
                   5607:         if ( spol ) {
                   5608:             add_pbucket_symbolic(bucket,spol);
                   5609:         }
                   5610:     }
                   5611:     return 1;
1.65      noro     5612: }
                   5613:
1.133     noro     5614: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5615: {
1.157     noro     5616:     NODE rp0,rp;
                   5617:     NM mul,head,s0,s;
                   5618:     int index,col,i,sugar;
                   5619:     RHist h;
                   5620:     UINT *s0v,*p;
                   5621:     NM_ind_pair pair;
                   5622:     ND red;
                   5623:     NDV *ps;
                   5624:
                   5625:     s0 = 0; rp0 = 0; col = 0;
                   5626:     ps = trace?nd_ps_trace:nd_ps;
                   5627:     while ( 1 ) {
                   5628:         head = remove_head_pbucket_symbolic(bucket);
                   5629:         if ( !head ) break;
                   5630:         if ( !s0 ) s0 = head;
                   5631:         else NEXT(s) = head;
                   5632:         s = head;
                   5633:         index = ndl_find_reducer(DL(head));
                   5634:         if ( index >= 0 ) {
                   5635:             h = nd_psh[index];
                   5636:             NEWNM(mul);
                   5637:             ndl_sub(DL(head),DL(h),DL(mul));
                   5638:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5639:             sugar = TD(DL(mul))+SG(ps[index]);
                   5640:             MKNM_ind_pair(pair,mul,index,sugar);
                   5641:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5642:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5643:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5644:         }
                   5645:         col++;
                   5646:     }
                   5647:     if ( rp0 ) NEXT(rp) = 0;
                   5648:     NEXT(s) = 0;
                   5649:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5650:     for ( i = 0, p = s0v, s = s0; i < col;
                   5651:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5652:     *s0vect = s0v;
                   5653:     *r = rp0;
                   5654:     return col;
1.65      noro     5655: }
                   5656:
1.198     noro     5657:
1.167     noro     5658: NODE nd_f4(int m,int **indp)
1.69      noro     5659: {
1.157     noro     5660:     int i,nh,stat,index;
                   5661:     NODE r,g;
                   5662:     ND_pairs d,l,t;
                   5663:     ND spol,red;
                   5664:     NDV nf,redv;
                   5665:     NM s0,s;
                   5666:     NODE rp0,srp0,nflist;
                   5667:     int nsp,nred,col,rank,len,k,j,a;
                   5668:     UINT c;
                   5669:     UINT **spmat;
                   5670:     UINT *s0vect,*svect,*p,*v;
                   5671:     int *colstat;
                   5672:     IndArray *imat;
                   5673:     int *rhead;
                   5674:     int spcol,sprow;
                   5675:     int sugar;
                   5676:     PGeoBucket bucket;
                   5677:     struct oEGT eg0,eg1,eg_f4;
1.69      noro     5678:
1.103     noro     5679: #if 0
1.157     noro     5680:     ndv_alloc = 0;
1.103     noro     5681: #endif
1.157     noro     5682:     g = 0; d = 0;
                   5683:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5684:         d = update_pairs(d,g,i,0);
1.157     noro     5685:         g = update_base(g,i);
                   5686:     }
                   5687:     while ( d ) {
                   5688:         get_eg(&eg0);
                   5689:         l = nd_minsugarp(d,&d);
                   5690:         sugar = SG(l);
                   5691:         bucket = create_pbucket();
                   5692:         stat = nd_sp_f4(m,0,l,bucket);
                   5693:         if ( !stat ) {
                   5694:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5695:             NEXT(t) = d; d = l;
                   5696:             d = nd_reconstruct(0,d);
                   5697:             continue;
                   5698:         }
                   5699:         if ( bucket->m < 0 ) continue;
                   5700:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5701:         if ( !col ) {
                   5702:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5703:             NEXT(t) = d; d = l;
                   5704:             d = nd_reconstruct(0,d);
                   5705:             continue;
                   5706:         }
                   5707:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5708:         if ( DP_Print )
                   5709:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5710:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5711:         if ( 1 )
                   5712:             nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
                   5713:         else
                   5714:             nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
                   5715:         /* adding new bases */
                   5716:         for ( r = nflist; r; r = NEXT(r) ) {
                   5717:             nf = (NDV)BDY(r);
                   5718:             ndv_removecont(m,nf);
                   5719:             if ( !m && nd_nalg ) {
                   5720:                 ND nf1;
                   5721:
                   5722:                 nf1 = ndvtond(m,nf);
                   5723:                 nd_monic(0,&nf1);
                   5724:                 nd_removecont(m,nf1);
                   5725:                 nf = ndtondv(m,nf1);
                   5726:             }
                   5727:             nh = ndv_newps(m,nf,0);
1.168     noro     5728:             d = update_pairs(d,g,nh,0);
1.157     noro     5729:             g = update_base(g,nh);
                   5730:         }
                   5731:     }
1.103     noro     5732: #if 0
1.157     noro     5733:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5734: #endif
1.167     noro     5735:        conv_ilist(0,0,g,indp);
1.157     noro     5736:     return g;
1.69      noro     5737: }
1.74      noro     5738:
1.167     noro     5739: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5740: {
1.157     noro     5741:     int i,nh,stat,index;
                   5742:     NODE r,g;
                   5743:     ND_pairs d,l,l0,t;
                   5744:     ND spol,red;
                   5745:     NDV nf,redv,nfqv,nfv;
                   5746:     NM s0,s;
                   5747:     NODE rp0,srp0,nflist;
                   5748:     int nsp,nred,col,rank,len,k,j,a;
                   5749:     UINT c;
                   5750:     UINT **spmat;
                   5751:     UINT *s0vect,*svect,*p,*v;
                   5752:     int *colstat;
                   5753:     IndArray *imat;
                   5754:     int *rhead;
                   5755:     int spcol,sprow;
                   5756:     int sugar;
                   5757:     PGeoBucket bucket;
                   5758:     struct oEGT eg0,eg1,eg_f4;
                   5759:
                   5760:     g = 0; d = 0;
                   5761:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5762:         d = update_pairs(d,g,i,0);
1.157     noro     5763:         g = update_base(g,i);
                   5764:     }
                   5765:     while ( d ) {
                   5766:         get_eg(&eg0);
                   5767:         l = nd_minsugarp(d,&d);
                   5768:         sugar = SG(l);
                   5769:         bucket = create_pbucket();
                   5770:         stat = nd_sp_f4(m,0,l,bucket);
                   5771:         if ( !stat ) {
                   5772:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5773:             NEXT(t) = d; d = l;
                   5774:             d = nd_reconstruct(1,d);
                   5775:             continue;
                   5776:         }
                   5777:         if ( bucket->m < 0 ) continue;
                   5778:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5779:         if ( !col ) {
                   5780:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5781:             NEXT(t) = d; d = l;
                   5782:             d = nd_reconstruct(1,d);
                   5783:             continue;
                   5784:         }
                   5785:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5786:         if ( DP_Print )
                   5787:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5788:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5789:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5790:         if ( !l0 ) continue;
                   5791:         l = l0;
                   5792:
                   5793:         /* over Q */
                   5794:         bucket = create_pbucket();
                   5795:         stat = nd_sp_f4(0,1,l,bucket);
                   5796:         if ( !stat ) {
                   5797:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5798:             NEXT(t) = d; d = l;
                   5799:             d = nd_reconstruct(1,d);
                   5800:             continue;
                   5801:         }
                   5802:         if ( bucket->m < 0 ) continue;
                   5803:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5804:         if ( !col ) {
                   5805:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5806:             NEXT(t) = d; d = l;
                   5807:             d = nd_reconstruct(1,d);
                   5808:             continue;
                   5809:         }
                   5810:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5811:         /* adding new bases */
                   5812:         for ( r = nflist; r; r = NEXT(r) ) {
                   5813:             nfqv = (NDV)BDY(r);
                   5814:             ndv_removecont(0,nfqv);
                   5815:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5816:             if ( nd_nalg ) {
                   5817:                 ND nf1;
                   5818:
                   5819:                 nf1 = ndvtond(m,nfqv);
                   5820:                 nd_monic(0,&nf1);
                   5821:                 nd_removecont(0,nf1);
                   5822:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5823:             }
                   5824:             nfv = ndv_dup(0,nfqv);
                   5825:             ndv_mod(m,nfv);
                   5826:             ndv_removecont(m,nfv);
                   5827:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     5828:             d = update_pairs(d,g,nh,0);
1.157     noro     5829:             g = update_base(g,nh);
                   5830:         }
                   5831:     }
1.133     noro     5832: #if 0
1.157     noro     5833:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     5834: #endif
1.167     noro     5835:        conv_ilist(0,1,g,indp);
1.157     noro     5836:     return g;
1.133     noro     5837: }
                   5838:
1.176     noro     5839: NODE nd_f4_pseudo_trace(int m,int **indp)
                   5840: {
                   5841:     int i,nh,stat,index;
                   5842:     NODE r,g;
                   5843:     ND_pairs d,l,l0,t;
                   5844:     ND spol,red;
                   5845:     NDV nf,redv,nfqv,nfv;
                   5846:     NM s0,s;
                   5847:     NODE rp0,srp0,nflist;
                   5848:     int nsp,nred,col,rank,len,k,j,a;
                   5849:     UINT c;
                   5850:     UINT **spmat;
                   5851:     UINT *s0vect,*svect,*p,*v;
                   5852:     int *colstat;
                   5853:     IndArray *imat;
                   5854:     int *rhead;
                   5855:     int spcol,sprow;
                   5856:     int sugar;
                   5857:     PGeoBucket bucket;
                   5858:     struct oEGT eg0,eg1,eg_f4;
                   5859:
                   5860:     g = 0; d = 0;
                   5861:     for ( i = 0; i < nd_psn; i++ ) {
                   5862:         d = update_pairs(d,g,i,0);
                   5863:         g = update_base(g,i);
                   5864:     }
                   5865:     while ( d ) {
                   5866:         get_eg(&eg0);
                   5867:         l = nd_minsugarp(d,&d);
                   5868:         sugar = SG(l);
                   5869:         bucket = create_pbucket();
                   5870:         stat = nd_sp_f4(m,0,l,bucket);
                   5871:         if ( !stat ) {
                   5872:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5873:             NEXT(t) = d; d = l;
                   5874:             d = nd_reconstruct(1,d);
                   5875:             continue;
                   5876:         }
                   5877:         if ( bucket->m < 0 ) continue;
                   5878:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5879:         if ( !col ) {
                   5880:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5881:             NEXT(t) = d; d = l;
                   5882:             d = nd_reconstruct(1,d);
                   5883:             continue;
                   5884:         }
                   5885:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5886:         if ( DP_Print )
                   5887:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5888:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5889:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5890:         if ( !l0 ) continue;
                   5891:         l = l0;
                   5892:
                   5893:         /* over Q */
                   5894:                while ( 1 ) {
                   5895:                bucket = create_pbucket();
                   5896:                stat = nd_sp_f4(0,1,l,bucket);
                   5897:                if ( !stat ) {
                   5898:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5899:                NEXT(t) = d; d = l;
                   5900:                d = nd_reconstruct(1,d);
                   5901:                continue;
                   5902:                }
                   5903:                if ( bucket->m < 0 ) continue;
                   5904:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5905:                if ( !col ) {
                   5906:                for ( t = l; NEXT(t); t = NEXT(t) );
                   5907:                NEXT(t) = d; d = l;
                   5908:                d = nd_reconstruct(1,d);
                   5909:                continue;
                   5910:                }
                   5911:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5912:                }
                   5913:
                   5914:         /* adding new bases */
                   5915:         for ( r = nflist; r; r = NEXT(r) ) {
                   5916:             nfqv = (NDV)BDY(r);
                   5917:             ndv_removecont(0,nfqv);
                   5918:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5919:             if ( nd_nalg ) {
                   5920:                 ND nf1;
                   5921:
                   5922:                 nf1 = ndvtond(m,nfqv);
                   5923:                 nd_monic(0,&nf1);
                   5924:                 nd_removecont(0,nf1);
                   5925:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5926:             }
                   5927:             nfv = ndv_dup(0,nfqv);
                   5928:             ndv_mod(m,nfv);
                   5929:             ndv_removecont(m,nfv);
                   5930:             nh = ndv_newps(0,nfv,nfqv);
                   5931:             d = update_pairs(d,g,nh,0);
                   5932:             g = update_base(g,nh);
                   5933:         }
                   5934:     }
                   5935: #if 0
                   5936:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5937: #endif
                   5938:        conv_ilist(0,1,g,indp);
                   5939:     return g;
                   5940: }
                   5941:
1.133     noro     5942: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     5943: {
1.157     noro     5944:     IndArray *imat;
                   5945:     int nsp,nred,i;
                   5946:     int *rhead;
                   5947:     NODE r0,rp;
                   5948:     ND_pairs sp;
                   5949:     NM_ind_pair *rvect;
1.198     noro     5950: init_eg(&eg_search);
1.157     noro     5951:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5952:     nred = length(rp0);
                   5953:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   5954:     rhead = (int *)ALLOCA(col*sizeof(int));
                   5955:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5956:
                   5957:     /* construction of index arrays */
                   5958:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   5959:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   5960:         rvect[i] = (NM_ind_pair)BDY(rp);
                   5961:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   5962:         rhead[imat[i]->head] = 1;
                   5963:     }
                   5964:     if ( m )
                   5965:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   5966:     else
                   5967:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.198     noro     5968: print_eg("search",&eg_search);
1.157     noro     5969:     return r0;
1.106     noro     5970: }
1.74      noro     5971:
1.106     noro     5972: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     5973:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     5974: {
1.157     noro     5975:     int spcol,sprow,a;
                   5976:     int i,j,k,l,rank;
                   5977:     NODE r0,r;
                   5978:     ND_pairs sp;
                   5979:     ND spol;
                   5980:     int **spmat;
                   5981:     UINT *svect,*v;
                   5982:     int *colstat;
                   5983:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5984:     int maxrs;
                   5985:     int *spsugar;
                   5986:     ND_pairs *spactive;
                   5987:
                   5988:     spcol = col-nred;
                   5989:     get_eg(&eg0);
                   5990:     /* elimination (1st step) */
                   5991:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   5992:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   5993:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   5994:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   5995:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5996:         nd_sp(m,0,sp,&spol);
                   5997:         if ( !spol ) continue;
                   5998:         nd_to_vect(m,s0vect,col,spol,svect);
                   5999:         if ( m == -1 )
                   6000:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   6001:         else
                   6002:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   6003:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6004:         if ( i < col ) {
                   6005:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   6006:             for ( j = k = 0; j < col; j++ )
                   6007:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6008:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6009:             if ( nz )
                   6010:             spactive[sprow] = sp;
                   6011:             sprow++;
                   6012:         }
                   6013:         nd_free(spol);
                   6014:     }
                   6015:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6016:     if ( DP_Print ) {
                   6017:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6018:         fflush(asir_out);
                   6019:     }
                   6020:     /* free index arrays */
1.200     noro     6021:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     6022:
                   6023:     /* elimination (2nd step) */
                   6024:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6025:     if ( m == -1 )
                   6026:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6027:     else
                   6028:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   6029:     r0 = 0;
                   6030:     for ( i = 0; i < rank; i++ ) {
                   6031:         NEXTNODE(r0,r); BDY(r) =
                   6032:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6033:         SG((NDV)BDY(r)) = spsugar[i];
1.200     noro     6034:         GCFREE(spmat[i]);
1.157     noro     6035:     }
                   6036:     if ( r0 ) NEXT(r) = 0;
                   6037:
1.200     noro     6038:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     6039:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6040:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6041:     if ( DP_Print ) {
                   6042:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6043:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6044:             nsp,nred,sprow,spcol,rank);
                   6045:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6046:     }
                   6047:     if ( nz ) {
                   6048:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   6049:         if ( rank > 0 ) {
                   6050:             NEXT(spactive[rank-1]) = 0;
                   6051:             *nz = spactive[0];
                   6052:         } else
                   6053:             *nz = 0;
                   6054:     }
                   6055:     return r0;
1.74      noro     6056: }
                   6057:
1.133     noro     6058: #if 1
                   6059: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     6060:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6061: {
1.157     noro     6062:     int spcol,sprow,a;
                   6063:     int i,j,k,l,rank;
                   6064:     NODE r0,r;
                   6065:     ND_pairs sp;
                   6066:     ND spol;
                   6067:     Q **spmat;
                   6068:     Q *svect,*v;
                   6069:     int *colstat;
                   6070:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   6071:     int maxrs;
                   6072:     int *spsugar;
                   6073:     pointer *w;
                   6074:
                   6075:     spcol = col-nred;
                   6076:     get_eg(&eg0);
                   6077:     /* elimination (1st step) */
                   6078:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   6079:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   6080:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   6081:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6082:         nd_sp(0,trace,sp,&spol);
                   6083:         if ( !spol ) continue;
                   6084:         nd_to_vect_q(s0vect,col,spol,svect);
                   6085:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   6086:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6087:         if ( i < col ) {
                   6088:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   6089:             for ( j = k = 0; j < col; j++ )
                   6090:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6091:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6092:             sprow++;
                   6093:         }
                   6094: /*        nd_free(spol); */
                   6095:     }
                   6096:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6097:     if ( DP_Print ) {
                   6098:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6099:         fflush(asir_out);
                   6100:     }
                   6101:     /* free index arrays */
1.200     noro     6102: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     6103:
                   6104:     /* elimination (2nd step) */
                   6105:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6106:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   6107:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   6108:     for ( i = 0; i < rank; i++ ) {
                   6109:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   6110:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.200     noro     6111: /*        GCFREE(spmat[i]); */
1.157     noro     6112:     }
1.138     noro     6113: #if 0
1.157     noro     6114:     qsort(w,rank,sizeof(NDV),
                   6115:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     6116: #endif
1.157     noro     6117:     r0 = 0;
                   6118:     for ( i = 0; i < rank; i++ ) {
                   6119:         NEXTNODE(r0,r); BDY(r) = w[i];
                   6120:     }
                   6121:     if ( r0 ) NEXT(r) = 0;
                   6122:
1.200     noro     6123: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     6124:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6125:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6126:     if ( DP_Print ) {
                   6127:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6128:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6129:             nsp,nred,sprow,spcol,rank);
                   6130:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6131:     }
                   6132:     return r0;
1.107     noro     6133: }
1.129     noro     6134: #else
                   6135: void printm(Q **mat,int row,int col)
                   6136: {
1.157     noro     6137:     int i,j;
                   6138:     printf("[");
                   6139:     for ( i = 0; i < row; i++ ) {
                   6140:         for ( j = 0; j < col; j++ ) {
                   6141:             printexpr(CO,mat[i][j]); printf(" ");
                   6142:         }
                   6143:         printf("]\n");
                   6144:     }
1.129     noro     6145: }
                   6146:
                   6147: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   6148:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6149: {
1.157     noro     6150:     int row,a;
                   6151:     int i,j,rank;
                   6152:     NODE r0,r;
                   6153:     ND_pairs sp;
                   6154:     ND spol;
                   6155:     Q **mat;
                   6156:     int *colstat;
                   6157:     int *sugar;
                   6158:
                   6159:     row = nsp+nred;
                   6160:     /* make the matrix */
                   6161:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   6162:     sugar = (int *)ALLOCA(row*sizeof(int));
                   6163:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6164:         nd_sp(0,0,sp,&spol);
                   6165:         if ( !spol ) continue;
                   6166:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   6167:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   6168:         sugar[row] = SG(spol);
                   6169:         row++;
                   6170:     }
                   6171:     for ( i = 0; i < nred; i++, row++ ) {
                   6172:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   6173:         sugar[row] = rvect[i]->sugar;
                   6174:     }
                   6175:     /* elimination */
                   6176:     colstat = (int *)ALLOCA(col*sizeof(int));
                   6177:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   6178:     r0 = 0;
                   6179:     for ( i = 0; i < rank; i++ ) {
                   6180:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   6181:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   6182:         if ( rhead[j] ) continue;
                   6183:         NEXTNODE(r0,r); BDY(r) =
                   6184:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   6185:         SG((NDV)BDY(r)) = sugar[i];
                   6186:     }
                   6187:     if ( r0 ) NEXT(r) = 0;
                   6188:     printf("\n");
                   6189:     return r0;
1.129     noro     6190: }
                   6191: #endif
1.107     noro     6192:
1.74      noro     6193: FILE *nd_write,*nd_read;
                   6194:
                   6195: void nd_send_int(int a) {
1.157     noro     6196:     write_int(nd_write,&a);
1.74      noro     6197: }
                   6198:
                   6199: void nd_send_intarray(int *p,int len) {
1.157     noro     6200:     write_intarray(nd_write,p,len);
1.74      noro     6201: }
                   6202:
                   6203: int nd_recv_int() {
1.157     noro     6204:     int a;
1.74      noro     6205:
1.157     noro     6206:     read_int(nd_read,&a);
                   6207:     return a;
1.74      noro     6208: }
                   6209:
                   6210: void nd_recv_intarray(int *p,int len) {
1.157     noro     6211:     read_intarray(nd_read,p,len);
1.74      noro     6212: }
                   6213:
                   6214: void nd_send_ndv(NDV p) {
1.157     noro     6215:     int len,i;
                   6216:     NMV m;
1.74      noro     6217:
1.157     noro     6218:     if ( !p ) nd_send_int(0);
                   6219:     else {
                   6220:         len = LEN(p);
                   6221:         nd_send_int(len);
                   6222:         m = BDY(p);
                   6223:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6224:             nd_send_int(CM(m));
                   6225:             nd_send_intarray(DL(m),nd_wpd);
                   6226:         }
                   6227:     }
1.74      noro     6228: }
                   6229:
                   6230: void nd_send_nd(ND p) {
1.157     noro     6231:     int len,i;
                   6232:     NM m;
1.74      noro     6233:
1.157     noro     6234:     if ( !p ) nd_send_int(0);
                   6235:     else {
                   6236:         len = LEN(p);
                   6237:         nd_send_int(len);
                   6238:         m = BDY(p);
                   6239:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   6240:             nd_send_int(CM(m));
                   6241:             nd_send_intarray(DL(m),nd_wpd);
                   6242:         }
                   6243:     }
1.74      noro     6244: }
1.65      noro     6245:
1.74      noro     6246: NDV nd_recv_ndv()
                   6247: {
1.157     noro     6248:     int len,i;
                   6249:     NMV m,m0;
                   6250:     NDV r;
                   6251:
                   6252:     len = nd_recv_int();
                   6253:     if ( !len ) return 0;
                   6254:     else {
1.200     noro     6255:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6256: #if 0
1.157     noro     6257:         ndv_alloc += len*nmv_adv;
1.103     noro     6258: #endif
1.157     noro     6259:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6260:             CM(m) = nd_recv_int();
                   6261:             nd_recv_intarray(DL(m),nd_wpd);
                   6262:         }
                   6263:         MKNDV(nd_nvar,m0,len,r);
                   6264:         return r;
                   6265:     }
1.74      noro     6266: }
1.65      noro     6267:
1.74      noro     6268: int ox_exec_f4_red(Q proc)
                   6269: {
1.157     noro     6270:     Obj obj;
                   6271:     STRING fname;
                   6272:     NODE arg;
                   6273:     int s;
                   6274:     extern int ox_need_conv,ox_file_io;
                   6275:
                   6276:     MKSTR(fname,"nd_exec_f4_red");
                   6277:     arg = mknode(2,proc,fname);
                   6278:     Pox_cmo_rpc(arg,&obj);
                   6279:     s = get_ox_server_id(QTOS(proc));
                   6280:     nd_write = iofp[s].out;
                   6281:     nd_read = iofp[s].in;
                   6282:     ox_need_conv = ox_file_io = 0;
                   6283:     return s;
1.74      noro     6284: }
                   6285:
1.133     noro     6286: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     6287: {
1.157     noro     6288:     int nsp,nred;
                   6289:     int i,rank,s;
                   6290:     NODE rp,r0,r;
                   6291:     ND_pairs sp;
                   6292:     NM_ind_pair pair;
                   6293:     NMV nmv;
                   6294:     NM nm;
                   6295:     NDV nf;
                   6296:     Obj proc,dmy;
                   6297:
                   6298:     ox_launch_main(0,0,&proc);
                   6299:     s = ox_exec_f4_red((Q)proc);
                   6300:
                   6301:     nd_send_int(m);
                   6302:     nd_send_int(nd_nvar);
                   6303:     nd_send_int(nd_bpe);
                   6304:     nd_send_int(nd_wpd);
                   6305:     nd_send_int(nmv_adv);
                   6306:
                   6307:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   6308:
                   6309:     nd_send_int(nd_psn);
                   6310:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   6311:
                   6312:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6313:     nd_send_int(nsp);
                   6314:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   6315:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   6316:     }
                   6317:
                   6318:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   6319:
                   6320:     nred = length(rp0); nd_send_int(nred);
                   6321:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   6322:         pair = (NM_ind_pair)BDY(rp);
                   6323:         nd_send_int(pair->index);
                   6324:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   6325:     }
                   6326:     fflush(nd_write);
                   6327:     rank = nd_recv_int();
                   6328:     fprintf(asir_out,"rank=%d\n",rank);
                   6329:     r0 = 0;
                   6330:     for ( i = 0; i < rank; i++ ) {
                   6331:         nf = nd_recv_ndv();
                   6332:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   6333:     }
                   6334:     Pox_shutdown(mknode(1,proc),&dmy);
                   6335:     return r0;
1.74      noro     6336: }
                   6337:
                   6338: /* server side */
                   6339:
                   6340: void nd_exec_f4_red_dist()
                   6341: {
1.157     noro     6342:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   6343:     NM_ind_pair *rp0;
                   6344:     NDV nf;
                   6345:     UINT *s0vect;
                   6346:     IndArray *imat;
                   6347:     int *rhead;
                   6348:     int **spmat;
                   6349:     UINT *svect,*v;
                   6350:     ND_pairs *sp0;
                   6351:     int *colstat;
                   6352:     int a,sprow,rank;
                   6353:     struct order_spec *ord;
                   6354:     Obj ordspec;
                   6355:     ND spol;
                   6356:     int maxrs;
                   6357:     int *spsugar;
                   6358:
                   6359:     nd_read = iofp[0].in;
                   6360:     nd_write = iofp[0].out;
                   6361:     m = nd_recv_int();
                   6362:     nd_nvar = nd_recv_int();
                   6363:     nd_bpe = nd_recv_int();
                   6364:     nd_wpd = nd_recv_int();
                   6365:     nmv_adv = nd_recv_int();
                   6366:
                   6367:     loadobj(nd_read,&ordspec);
                   6368:     create_order_spec(0,ordspec,&ord);
                   6369:     nd_init_ord(ord);
                   6370:     nd_setup_parameters(nd_nvar,0);
                   6371:
                   6372:     nd_psn = nd_recv_int();
                   6373:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   6374:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   6375:     for ( i = 0; i < nd_psn; i++ ) {
                   6376:         nd_ps[i] = nd_recv_ndv();
                   6377:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   6378:     }
                   6379:
                   6380:     nsp = nd_recv_int();
                   6381:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   6382:     for ( i = 0; i < nsp; i++ ) {
                   6383:         NEWND_pairs(sp0[i]);
                   6384:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6385:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6386:     }
                   6387:
                   6388:     col = nd_recv_int();
                   6389:     s0size = col*nd_wpd;
                   6390:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6391:     nd_recv_intarray(s0vect,s0size);
                   6392:
                   6393:     nred = nd_recv_int();
                   6394:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6395:     for ( i = 0; i < nred; i++ ) {
                   6396:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6397:         rp0[i]->index = nd_recv_int();
                   6398:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6399:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6400:     }
                   6401:
                   6402:     spcol = col-nred;
                   6403:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6404:     rhead = (int *)MALLOC(col*sizeof(int));
                   6405:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6406:
                   6407:     /* construction of index arrays */
                   6408:     for ( i = 0; i < nred; i++ ) {
                   6409:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6410:         rhead[imat[i]->head] = 1;
                   6411:     }
                   6412:
                   6413:     /* elimination (1st step) */
                   6414:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6415:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6416:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6417:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6418:         nd_sp(m,0,sp0[a],&spol);
                   6419:         if ( !spol ) continue;
                   6420:         nd_to_vect(m,s0vect,col,spol,svect);
                   6421:         if ( m == -1 )
                   6422:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6423:         else
                   6424:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6425:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6426:         if ( i < col ) {
                   6427:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6428:             for ( j = k = 0; j < col; j++ )
                   6429:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6430:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6431:             sprow++;
                   6432:         }
                   6433:         nd_free(spol);
                   6434:     }
                   6435:     /* elimination (2nd step) */
                   6436:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6437:     if ( m == -1 )
                   6438:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6439:     else
                   6440:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6441:     nd_send_int(rank);
                   6442:     for ( i = 0; i < rank; i++ ) {
                   6443:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6444:         nd_send_ndv(nf);
                   6445:     }
                   6446:     fflush(nd_write);
1.107     noro     6447: }
                   6448:
1.113     noro     6449: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6450: {
1.176     noro     6451:     int i,j,t,c,rank,inv;
1.157     noro     6452:     int *ci,*ri;
                   6453:     Q dn;
                   6454:     MAT m,nm;
                   6455:
                   6456:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6457:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6458:     for ( i = 0; i < row; i++ )
                   6459:         for ( j = 0; j < col; j++ )
                   6460:             mat0[i][j] = 0;
                   6461:     c = col-rank;
                   6462:     for ( i = 0; i < rank; i++ ) {
                   6463:         mat0[i][ri[i]] = dn;
                   6464:         for ( j = 0; j < c; j++ )
                   6465:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6466:     }
                   6467:     return rank;
1.76      noro     6468: }
                   6469:
1.133     noro     6470: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6471: {
1.157     noro     6472:     int i,j,k,l,inv,a,rank,s;
                   6473:     unsigned int *t,*pivot,*pk;
                   6474:     unsigned int **mat;
                   6475:     ND_pairs pair;
                   6476:
                   6477:     mat = (unsigned int **)mat0;
                   6478:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6479:         for ( i = rank; i < row; i++ )
                   6480:             mat[i][j] %= md;
                   6481:         for ( i = rank; i < row; i++ )
                   6482:             if ( mat[i][j] )
                   6483:                 break;
                   6484:         if ( i == row ) {
                   6485:             colstat[j] = 0;
                   6486:             continue;
                   6487:         } else
                   6488:             colstat[j] = 1;
                   6489:         if ( i != rank ) {
                   6490:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6491:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6492:             if ( spactive ) {
                   6493:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6494:                 spactive[rank] = pair;
                   6495:             }
                   6496:         }
                   6497:         pivot = mat[rank];
                   6498:         s = sugar[rank];
                   6499:         inv = invm(pivot[j],md);
                   6500:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6501:             if ( *pk ) {
                   6502:                 if ( *pk >= (unsigned int)md )
                   6503:                     *pk %= md;
                   6504:                 DMAR(*pk,inv,0,md,*pk)
                   6505:             }
                   6506:         for ( i = rank+1; i < row; i++ ) {
                   6507:             t = mat[i];
                   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:         rank++;
                   6514:     }
                   6515:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6516:         if ( colstat[j] ) {
                   6517:             pivot = mat[l];
                   6518:             s = sugar[l];
                   6519:             for ( i = 0; i < l; i++ ) {
                   6520:                 t = mat[i];
                   6521:                 t[j] %= md;
                   6522:                 if ( a = t[j] ) {
                   6523:                     sugar[i] = MAX(sugar[i],s);
                   6524:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6525:                 }
                   6526:             }
                   6527:             l--;
                   6528:         }
                   6529:     for ( j = 0, l = 0; l < rank; j++ )
                   6530:         if ( colstat[j] ) {
                   6531:             t = mat[l];
                   6532:             for ( k = j; k < col; k++ )
                   6533:                 if ( t[k] >= (unsigned int)md )
                   6534:                     t[k] %= md;
                   6535:             l++;
                   6536:         }
                   6537:     return rank;
1.76      noro     6538: }
                   6539:
                   6540: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6541: {
1.157     noro     6542:     int i,j,k,l,inv,a,rank,s;
                   6543:     unsigned int *t,*pivot,*pk;
                   6544:     unsigned int **mat;
                   6545:
                   6546:     mat = (unsigned int **)mat0;
                   6547:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6548:         for ( i = rank; i < row; i++ )
                   6549:             if ( mat[i][j] )
                   6550:                 break;
                   6551:         if ( i == row ) {
                   6552:             colstat[j] = 0;
                   6553:             continue;
                   6554:         } else
                   6555:             colstat[j] = 1;
                   6556:         if ( i != rank ) {
                   6557:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6558:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6559:         }
                   6560:         pivot = mat[rank];
                   6561:         s = sugar[rank];
                   6562:         inv = _invsf(pivot[j]);
                   6563:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6564:             if ( *pk )
                   6565:                 *pk = _mulsf(*pk,inv);
                   6566:         for ( i = rank+1; i < row; i++ ) {
                   6567:             t = mat[i];
                   6568:             if ( a = t[j] ) {
                   6569:                 sugar[i] = MAX(sugar[i],s);
                   6570:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6571:             }
                   6572:         }
                   6573:         rank++;
                   6574:     }
                   6575:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6576:         if ( colstat[j] ) {
                   6577:             pivot = mat[l];
                   6578:             s = sugar[l];
                   6579:             for ( i = 0; i < l; i++ ) {
                   6580:                 t = mat[i];
                   6581:                 if ( a = t[j] ) {
                   6582:                     sugar[i] = MAX(sugar[i],s);
                   6583:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6584:                 }
                   6585:             }
                   6586:             l--;
                   6587:         }
                   6588:     return rank;
1.77      noro     6589: }
                   6590:
                   6591: int ndv_ishomo(NDV p)
                   6592: {
1.157     noro     6593:     NMV m;
                   6594:     int len,h;
1.77      noro     6595:
1.157     noro     6596:     if ( !p ) return 1;
                   6597:     len = LEN(p);
                   6598:     m = BDY(p);
                   6599:     h = TD(DL(m));
                   6600:     NMV_ADV(m);
                   6601:     for ( len--; len; len--, NMV_ADV(m) )
                   6602:         if ( TD(DL(m)) != h ) return 0;
                   6603:     return 1;
1.77      noro     6604: }
                   6605:
                   6606: void ndv_save(NDV p,int index)
                   6607: {
1.157     noro     6608:     FILE *s;
                   6609:     char name[BUFSIZ];
                   6610:     short id;
                   6611:     int nv,sugar,len,n,i,td,e,j;
                   6612:     NMV m;
                   6613:     unsigned int *dl;
                   6614:     int mpos;
                   6615:
                   6616:     sprintf(name,"%s/%d",Demand,index);
                   6617:     s = fopen(name,"w");
                   6618:     savevl(s,0);
                   6619:     if ( !p ) {
                   6620:         saveobj(s,0);
                   6621:         return;
                   6622:     }
                   6623:     id = O_DP;
                   6624:     nv = NV(p);
                   6625:     sugar = SG(p);
                   6626:     len = LEN(p);
                   6627:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6628:     write_int(s,&len);
                   6629:
                   6630:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6631:         saveobj(s,(Obj)CQ(m));
                   6632:         dl = DL(m);
                   6633:         td = TD(dl);
                   6634:         write_int(s,&td);
                   6635:         for ( j = 0; j < nv; j++ ) {
                   6636:             e = GET_EXP(dl,j);
                   6637:             write_int(s,&e);
                   6638:         }
                   6639:         if ( nd_module ) {
                   6640:             mpos = MPOS(dl); write_int(s,&mpos);
                   6641:         }
                   6642:     }
                   6643:     fclose(s);
1.77      noro     6644: }
                   6645:
                   6646: NDV ndv_load(int index)
                   6647: {
1.157     noro     6648:     FILE *s;
                   6649:     char name[BUFSIZ];
                   6650:     short id;
                   6651:     int nv,sugar,len,n,i,td,e,j;
                   6652:     NDV d;
                   6653:     NMV m0,m;
                   6654:     unsigned int *dl;
                   6655:     Obj obj;
                   6656:     int mpos;
                   6657:
                   6658:     sprintf(name,"%s/%d",Demand,index);
                   6659:     s = fopen(name,"r");
                   6660:     if ( !s ) return 0;
                   6661:
                   6662:     skipvl(s);
                   6663:     read_short(s,&id);
                   6664:     if ( !id ) return 0;
                   6665:     read_int(s,&nv);
                   6666:     read_int(s,&sugar);
                   6667:     read_int(s,&len);
                   6668:
                   6669:     m0 = m = MALLOC(len*nmv_adv);
                   6670:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6671:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6672:         dl = DL(m);
                   6673:         ndl_zero(dl);
                   6674:         read_int(s,&td); TD(dl) = td;
                   6675:         for ( j = 0; j < nv; j++ ) {
                   6676:             read_int(s,&e);
                   6677:             PUT_EXP(dl,j,e);
                   6678:         }
                   6679:         if ( nd_module ) {
                   6680:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6681:         }
                   6682:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6683:     }
                   6684:     fclose(s);
                   6685:     MKNDV(nv,m0,len,d);
                   6686:     SG(d) = sugar;
                   6687:     return d;
1.99      noro     6688: }
                   6689:
1.102     noro     6690: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6691: {
1.157     noro     6692:     VL fv,tv;
                   6693:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6694:     pointer **m;
                   6695:     Q mone;
                   6696:     P **w;
                   6697:     P mp,r;
                   6698:     NDV **dm;
                   6699:     NDV *t,*mi,*mj;
                   6700:     NDV d,s,mij,mjj;
                   6701:     ND u;
                   6702:     NMV nmv;
                   6703:     UINT *bound;
                   6704:     PGeoBucket bucket;
                   6705:     struct order_spec *ord;
                   6706:     Q dq,dt,ds;
                   6707:     N gn,qn,dn0,nm,dn;
                   6708:
                   6709:     create_order_spec(0,0,&ord);
                   6710:     nd_init_ord(ord);
                   6711:     get_vars((Obj)f,&fv);
                   6712:     if ( f->row != f->col )
                   6713:         error("nd_det : non-square matrix");
                   6714:     n = f->row;
                   6715:     m = f->body;
                   6716:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6717:
                   6718:     if ( !nvar ) {
                   6719:         if ( !mod )
                   6720:             detp(CO,(P **)m,n,rp);
                   6721:         else {
                   6722:             w = (P **)almat_pointer(n,n);
                   6723:             for ( i = 0; i < n; i++ )
                   6724:                 for ( j = 0; j < n; j++ )
                   6725:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6726:             detmp(CO,mod,w,n,&mp);
                   6727:             mptop(mp,rp);
                   6728:         }
                   6729:         return;
                   6730:     }
                   6731:
                   6732:     if ( !mod ) {
                   6733:         w = (P **)almat_pointer(n,n);
                   6734:         dq = ONE;
                   6735:         for ( i = 0; i < n; i++ ) {
                   6736:             dn0 = ONEN;
                   6737:             for ( j = 0; j < n; j++ ) {
                   6738:                 if ( !m[i][j] ) continue;
                   6739:                 lgp(m[i][j],&nm,&dn);
                   6740:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   6741:             }
                   6742:             if ( !UNIN(dn0) ) {
                   6743:                 NTOQ(dn0,1,ds);
                   6744:                 for ( j = 0; j < n; j++ )
                   6745:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   6746:                 mulq(dq,ds,&dt); dq = dt;
                   6747:             } else
                   6748:                 for ( j = 0; j < n; j++ )
                   6749:                     w[i][j] = (P)m[i][j];
                   6750:         }
                   6751:         m = (pointer **)w;
                   6752:     }
                   6753:
1.178     noro     6754:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6755:         for ( j = 0; j < n; j++ )
                   6756:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   6757:                 e = getdeg(tv->v,(P)m[i][j]);
                   6758:                 max = MAX(e,max);
                   6759:             }
                   6760:     nd_setup_parameters(nvar,max);
                   6761:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     6762:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     6763:         for ( j = 0; j < n; j++ ) {
                   6764:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   6765:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   6766:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   6767:         }
                   6768:     d = ptondv(CO,fv,(P)ONE);
                   6769:     if ( mod ) ndv_mod(mod,d);
                   6770:     chsgnq(ONE,&mone);
                   6771:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   6772:         if ( DP_Print ) fprintf(stderr,".",j);
                   6773:         for ( i = j; i < n && !dm[i][j]; i++ );
                   6774:         if ( i == n ) {
                   6775:             *rp = 0;
                   6776:             return;
                   6777:         }
                   6778:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   6779:         for ( k = j; k < n; k++ )
                   6780:             for ( l = j; l < n; l++ )
                   6781:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   6782:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   6783:                 }
                   6784:         if ( k0 != j ) {
                   6785:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   6786:             sgn = -sgn;
                   6787:         }
                   6788:         if ( l0 != j ) {
                   6789:             for ( k = j; k < n; k++ ) {
                   6790:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   6791:             }
                   6792:             sgn = -sgn;
                   6793:         }
                   6794:         bound = nd_det_compute_bound(dm,n,j);
                   6795:         for ( k = 0; k < nd_nvar; k++ )
                   6796:             if ( bound[k]*2 > nd_mask0 ) break;
                   6797:         if ( k < nd_nvar )
                   6798:             nd_det_reconstruct(dm,n,j,d);
                   6799:
                   6800:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   6801: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   6802:             mi = dm[i]; mij = mi[j];
                   6803:             if ( mod )
                   6804:                 ndv_mul_c(mod,mij,mod-1);
                   6805:             else
                   6806:                 ndv_mul_c_q(mij,mone);
                   6807:             for ( k = j+1; k < n; k++ ) {
                   6808: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   6809:                 bucket = create_pbucket();
                   6810:                 if ( mi[k] ) {
                   6811:                     nmv = BDY(mjj); len = LEN(mjj);
                   6812:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6813:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   6814:                         add_pbucket(mod,bucket,u);
                   6815:                     }
                   6816:                 }
                   6817:                 if ( mj[k] && mij ) {
                   6818:                     nmv = BDY(mij); len = LEN(mij);
                   6819:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6820:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   6821:                         add_pbucket(mod,bucket,u);
                   6822:                     }
                   6823:                 }
                   6824:                 u = nd_quo(mod,bucket,d);
                   6825:                 mi[k] = ndtondv(mod,u);
                   6826:             }
                   6827: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   6828:         }
                   6829:         d = mjj;
                   6830:     }
                   6831:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   6832:     if ( sgn < 0 )
                   6833:         if ( mod )
                   6834:             ndv_mul_c(mod,d,mod-1);
                   6835:         else
                   6836:             ndv_mul_c_q(d,mone);
                   6837:     r = ndvtop(mod,CO,fv,d);
                   6838:     if ( !mod && !UNIQ(dq) )
                   6839:         divsp(CO,r,(P)dq,rp);
                   6840:     else
                   6841:         *rp = r;
1.99      noro     6842: }
                   6843:
1.102     noro     6844: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     6845: {
1.157     noro     6846:     NM mr,mr0;
                   6847:     NM tnm;
                   6848:     NMV m;
                   6849:     UINT *d0,*dt,*dm;
                   6850:     int c,n,td,i,c1,c2,len;
                   6851:     Q q;
                   6852:     ND r;
                   6853:
                   6854:     if ( !p ) return 0;
                   6855:     else {
                   6856:         n = NV(p); m = BDY(p); len = LEN(p);
                   6857:         d0 = DL(m0);
                   6858:         td = TD(d);
                   6859:         mr0 = 0;
                   6860:         NEWNM(tnm);
                   6861:         if ( mod ) {
                   6862:             c = CM(m0);
                   6863:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6864:                 ndl_add(DL(m),d0,DL(tnm));
                   6865:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6866:                     NEXTNM(mr0,mr);
                   6867:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   6868:                     ndl_copy(DL(tnm),DL(mr));
                   6869:                 }
                   6870:             }
                   6871:         } else {
                   6872:             q = CQ(m0);
                   6873:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6874:                 ndl_add(DL(m),d0,DL(tnm));
                   6875:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6876:                     NEXTNM(mr0,mr);
                   6877:                     mulq(CQ(m),q,&CQ(mr));
                   6878:                     ndl_copy(DL(tnm),DL(mr));
                   6879:                 }
                   6880:             }
                   6881:         }
                   6882:         if ( !mr0 )
                   6883:             return 0;
                   6884:         else {
                   6885:             NEXT(mr) = 0;
                   6886:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6887:             MKND(NV(p),mr0,len,r);
                   6888:             SG(r) = SG(p) + TD(d0);
                   6889:             return r;
                   6890:         }
                   6891:     }
1.114     noro     6892: }
                   6893:
                   6894: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   6895: {
1.157     noro     6896:     int i,obpe,oadv,h,k,l;
                   6897:     static NM prev_nm_free_list;
                   6898:     EPOS oepos;
                   6899:
                   6900:     obpe = nd_bpe;
                   6901:     oadv = nmv_adv;
                   6902:     oepos = nd_epos;
                   6903:     if ( obpe < 2 ) nd_bpe = 2;
                   6904:     else if ( obpe < 3 ) nd_bpe = 3;
                   6905:     else if ( obpe < 4 ) nd_bpe = 4;
                   6906:     else if ( obpe < 5 ) nd_bpe = 5;
                   6907:     else if ( obpe < 6 ) nd_bpe = 6;
                   6908:     else if ( obpe < 8 ) nd_bpe = 8;
                   6909:     else if ( obpe < 10 ) nd_bpe = 10;
                   6910:     else if ( obpe < 16 ) nd_bpe = 16;
                   6911:     else if ( obpe < 32 ) nd_bpe = 32;
                   6912:     else error("nd_det_reconstruct : exponent too large");
                   6913:
                   6914:     nd_setup_parameters(nd_nvar,0);
                   6915:     prev_nm_free_list = _nm_free_list;
                   6916:     _nm_free_list = 0;
                   6917:     for ( k = j; k < n; k++ )
                   6918:         for (l = j; l < n; l++ )
                   6919:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   6920:     ndv_realloc(d,obpe,oadv,oepos);
                   6921:     prev_nm_free_list = 0;
1.114     noro     6922: #if 0
1.157     noro     6923:     GC_gcollect();
1.114     noro     6924: #endif
                   6925: }
                   6926:
1.153     noro     6927: /* returns a UINT array containing degree bounds */
                   6928:
1.114     noro     6929: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   6930: {
1.157     noro     6931:     UINT *d0,*d1,*d,*t,*r;
                   6932:     int k,l,i;
1.114     noro     6933:
1.157     noro     6934:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   6935:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   6936:     for ( k = j; k < n; k++ )
                   6937:         for ( l = j; l < n; l++ )
                   6938:             if ( dm[k][l] ) {
                   6939:                 d = ndv_compute_bound(dm[k][l]);
                   6940:                 for ( i = 0; i < nd_nvar; i++ )
                   6941:                     d0[i] = MAX(d0[i],d[i]);
                   6942:             }
                   6943:     return d0;
1.117     noro     6944: }
                   6945:
                   6946: DL nd_separate_d(UINT *d,UINT *trans)
                   6947: {
1.157     noro     6948:     int n,td,i,e,j;
                   6949:     DL a;
1.117     noro     6950:
1.157     noro     6951:     ndl_zero(trans);
                   6952:     td = 0;
                   6953:     for ( i = 0; i < nd_ntrans; i++ ) {
                   6954:         e = GET_EXP(d,i);
                   6955:         PUT_EXP(trans,i,e);
                   6956:         td += MUL_WEIGHT(e,i);
                   6957:     }
                   6958:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   6959:         /* homogenized */
                   6960:         i = nd_nvar-1;
                   6961:         e = GET_EXP(d,i);
                   6962:         PUT_EXP(trans,i,e);
                   6963:         td += MUL_WEIGHT(e,i);
                   6964:     }
                   6965:     TD(trans) = td;
                   6966:     if ( nd_blockmask) ndl_weight_mask(trans);
                   6967:     NEWDL(a,nd_nalg);
                   6968:     td = 0;
                   6969:     for ( i = 0; i < nd_nalg; i++ ) {
                   6970:         j = nd_ntrans+i;
                   6971:         e = GET_EXP(d,j);
                   6972:         a->d[i] = e;
                   6973:         td += e;
                   6974:     }
                   6975:     a->td = td;
                   6976:     return a;
1.117     noro     6977: }
                   6978:
1.118     noro     6979: int nd_monic(int mod,ND *p)
1.117     noro     6980: {
1.157     noro     6981:     UINT *trans,*t;
                   6982:     DL alg;
                   6983:     MP mp0,mp;
                   6984:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   6985:     ND r;
                   6986:     DL dl;
                   6987:     DP nm;
                   6988:     NDV ndv;
                   6989:     DAlg inv,cd;
                   6990:     ND s,c;
                   6991:     Q l,mul;
                   6992:     N ln;
                   6993:     int n,ntrans,i,e,td,is_lc,len;
                   6994:     NumberField nf;
                   6995:     struct oEGT eg0,eg1;
                   6996:
                   6997:     if ( !(nf = get_numberfield()) )
                   6998:         error("nd_monic : current_numberfield is not set");
                   6999:
                   7000:     /* Q coef -> DAlg coef */
                   7001:     NEWNM(ma0); ma = ma0;
                   7002:     m = BDY(*p);
                   7003:     is_lc = 1;
                   7004:     while ( 1 ) {
                   7005:         NEWMP(mp0); mp = mp0;
                   7006:         mp->c = (P)CQ(m);
                   7007:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   7008:         NEWNM(mb);
                   7009:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   7010:             alg = nd_separate_d(DL(m),DL(mb));
                   7011:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   7012:                 break;
                   7013:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   7014:         }
                   7015:         NEXT(mp) = 0;
                   7016:         MKDP(nd_nalg,mp0,nm);
                   7017:         MKDAlg(nm,ONE,cd);
                   7018:         if ( is_lc == 1 ) {
                   7019:             /* if the lc is a rational number, we have nothing to do */
                   7020:             if ( !mp0->dl->td )
                   7021:                 return 1;
                   7022:
                   7023:             get_eg(&eg0);
                   7024:             invdalg(cd,&inv);
                   7025:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   7026:             /* check the validity of inv */
                   7027:             if ( mod && !rem(NM(inv->dn),mod) )
                   7028:                 return 0;
                   7029:             CA(ma) = nf->one;
                   7030:             is_lc = 0;
                   7031:             ln = ONEN;
                   7032:         } else {
                   7033:             muldalg(cd,inv,&CA(ma));
                   7034:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   7035:         }
                   7036:         if ( m ) {
                   7037:             NEXT(ma) = mb; ma = mb;
                   7038:         } else {
                   7039:             NEXT(ma) = 0;
                   7040:             break;
                   7041:         }
                   7042:     }
                   7043:     /* l = lcm(denoms) */
                   7044:     NTOQ(ln,1,l);
                   7045:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   7046:         divq(l,CA(m)->dn,&mul);
                   7047:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   7048:             NEXTNM(mr0,mr);
                   7049:             mulq((Q)mp->c,mul,&CQ(mr));
                   7050:             dl = mp->dl;
                   7051:             td = TD(DL(m));
                   7052:             ndl_copy(DL(m),DL(mr));
                   7053:             for ( i = 0; i < nd_nalg; i++ ) {
                   7054:                 e = dl->d[i];
                   7055:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   7056:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   7057:             }
1.163     noro     7058:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     7059:             TD(DL(mr)) = td;
                   7060:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   7061:         }
                   7062:     }
                   7063:     NEXT(mr) = 0;
                   7064:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   7065:     MKND(NV(*p),mr0,len,r);
                   7066:     /* XXX */
                   7067:     SG(r) = SG(*p);
                   7068:     nd_free(*p);
                   7069:     *p = r;
                   7070:     return 1;
1.59      noro     7071: }
1.167     noro     7072:
                   7073: NODE reverse_node(NODE n)
                   7074: {
                   7075:     NODE t,t1;
                   7076:
                   7077:     for ( t = 0; n; n = NEXT(n) ) {
                   7078:         MKNODE(t1,BDY(n),t); t = t1;
                   7079:     }
                   7080:     return t;
                   7081: }
                   7082:
                   7083: P ndc_div(int mod,union oNDC a,union oNDC b)
                   7084: {
                   7085:     union oNDC c;
                   7086:     int inv,t;
                   7087:
                   7088:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   7089:     else if ( mod ) {
                   7090:         inv = invm(b.m,mod);
                   7091:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   7092:     } else if ( nd_vc )
                   7093:        divsp(nd_vc,a.p,b.p,&c.p);
                   7094:     else
                   7095:        divq(a.z,b.z,&c.z);
                   7096:     return ndctop(mod,c);
                   7097: }
                   7098:
                   7099: P ndctop(int mod,union oNDC c)
                   7100: {
                   7101:     Q q;
                   7102:     int e;
                   7103:     GFS gfs;
                   7104:
                   7105:     if ( mod == -1 ) {
                   7106:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   7107:     } else if ( mod ) {
                   7108:         STOQ(c.m,q); return (P)q;
                   7109:     } else
                   7110:         return (P)c.p;
                   7111: }
                   7112:
                   7113: /* [0,0,0,cont] = p -> p/cont */
                   7114:
                   7115: void finalize_tracelist(int i,P cont)
                   7116: {
                   7117:         LIST l;
                   7118:         NODE node;
                   7119:      Q iq;
                   7120:
                   7121:         if ( !UNIQ(cont) ) {
1.196     noro     7122:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     7123:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   7124:                 nd_tracelist = node;
                   7125:         }
                   7126:      STOQ(i,iq);
                   7127:      nd_tracelist = reverse_node(nd_tracelist);
                   7128:      MKLIST(l,nd_tracelist);
                   7129:      node = mknode(2,iq,l); MKLIST(l,node);
                   7130:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   7131:      nd_alltracelist = node; nd_tracelist = 0;
                   7132: }
                   7133:
                   7134: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   7135: {
                   7136:     int n,i,j;
                   7137:        int *ind;
                   7138:        NODE t;
                   7139:
                   7140:     n = length(g);
                   7141:        ind = (int *)MALLOC(n*sizeof(int));
                   7142:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   7143:                j = (long)BDY(t); ind[i] = j;
                   7144:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   7145:        }
                   7146:        if ( indp ) *indp = ind;
                   7147: }
1.172     noro     7148:
                   7149: void parse_nd_option(NODE opt)
                   7150: {
1.187     noro     7151:     NODE t,p,u;
                   7152:        int i,s;
1.172     noro     7153:     char *key;
                   7154:     Obj value;
                   7155:
1.187     noro     7156:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.195     noro     7157:        nd_newelim = 0; nd_intersect = 0;
1.172     noro     7158:     for ( t = opt; t; t = NEXT(t) ) {
                   7159:         p = BDY((LIST)BDY(t));
                   7160:         key = BDY((STRING)BDY(p));
                   7161:         value = (Obj)BDY(NEXT(p));
                   7162:         if ( !strcmp(key,"gentrace") )
                   7163:             nd_gentrace = value?1:0;
                   7164:         else if ( !strcmp(key,"gensyz") )
                   7165:             nd_gensyz = value?1:0;
1.173     noro     7166:         else if ( !strcmp(key,"nora") )
                   7167:             nd_nora = value?1:0;
1.187     noro     7168:         else if ( !strcmp(key,"gbblock") ) {
                   7169:                        if ( !value || OID(value) != O_LIST )
                   7170:                                error("nd_* : invalid value for gbblock option");
                   7171:                        u = BDY((LIST)value);
1.189     noro     7172:             nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.187     noro     7173:                        for ( i = 0; u; u = NEXT(u) ) {
                   7174:                                p = BDY((LIST)BDY(u));
                   7175:                                s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   7176:                                nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   7177:                        }
                   7178:                        nd_gbblock[i] = -1;
1.192     noro     7179:                } else if ( !strcmp(key,"newelim") )
                   7180:             nd_newelim = value?1:0;
1.195     noro     7181:                else if ( !strcmp(key,"intersect") )
                   7182:             nd_intersect = value?1:0;
1.172     noro     7183:     }
                   7184: }

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