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

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

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