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

1.175   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.174 2009/06/01 07:31:54 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 {
1.175   ! noro     3000: #if 0
        !          3001:                /* bug ? */
1.157     noro     3002:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3003:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175   ! noro     3004: #else
        !          3005:                conv_ilist(0,0,x,&perm);
        !          3006: #endif
1.157     noro     3007:     }
1.167     noro     3008:     x = ndv_reducebase(x,perm);
1.157     noro     3009:     x = ndv_reduceall(m,x);
                   3010:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3011:         NEXTNODE(r0,r);
                   3012:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3013:     }
                   3014:     if ( r0 ) NEXT(r) = 0;
                   3015:     if ( nalg )
                   3016:         r0 = postprocess_algcoef(av,alist,r0);
                   3017:     MKLIST(*rp,r0);
1.20      noro     3018: }
                   3019:
1.133     noro     3020: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3021: {
1.157     noro     3022:     VL tv,fv,vv,vc,av;
                   3023:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3024:     int m,nocheck,nvar,mindex,e,max;
                   3025:     NDV c;
                   3026:     NMV a;
                   3027:     P p,zp;
                   3028:     Q dmy;
                   3029:     EPOS oepos;
1.164     noro     3030:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3031:     Alg alpha,dp;
                   3032:     P poly;
1.158     noro     3033:     LIST f1,f2,zpl;
1.157     noro     3034:     Obj obj;
                   3035:     NumberField nf;
                   3036:     struct order_spec *ord1;
                   3037:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3038:     NODE tr,tl1,tl2,tl3,tl4;
                   3039:     LIST l1,l2,l3,l4,l5;
1.167     noro     3040:     int *perm;
1.168     noro     3041:     int j,ret;
1.167     noro     3042:     Q jq;
1.157     noro     3043:
1.167     noro     3044:     nd_module = 0;
1.172     noro     3045:     parse_nd_option(current_option);
1.157     noro     3046:     if ( DP_Multiple )
                   3047:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3048:
                   3049:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3050:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3051:     switch ( ord->id ) {
                   3052:         case 1:
                   3053:             if ( ord->nv != nvar )
                   3054:                 error("nd_gr_trace : invalid order specification");
                   3055:             break;
                   3056:         default:
                   3057:             break;
                   3058:     }
                   3059:
                   3060:     get_algtree((Obj)f,&av);
                   3061:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3062:     nd_ntrans = nvar;
                   3063:     nd_nalg = nalg;
                   3064:     /* #i -> t#i */
                   3065:     if ( nalg ) {
                   3066:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3067:         ord = ord1;
                   3068:         f = f1;
                   3069:     }
                   3070:     nvar += nalg;
                   3071:
                   3072:     nocheck = 0;
                   3073:     mindex = 0;
                   3074:
                   3075:     if ( Demand ) nd_demand = 1;
                   3076:     else nd_demand = 0;
                   3077:
                   3078:     /* setup modulus */
                   3079:     if ( trace < 0 ) {
                   3080:         trace = -trace;
                   3081:         nocheck = 1;
                   3082:     }
                   3083:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3084:     nd_init_ord(ord);
                   3085:     mrank = 0;
1.157     noro     3086:     for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   3087:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3088:             if ( nd_module ) {
                   3089:                 s = BDY((LIST)BDY(t));
                   3090:                 trank = length(s);
                   3091:                 mrank = MAX(mrank,trank);
                   3092:                 for ( ; s; s = NEXT(s) ) {
                   3093:                     e = getdeg(tv->v,(P)BDY(s));
                   3094:                     max = MAX(e,max);
                   3095:                 }
                   3096:             } else {
                   3097:                 e = getdeg(tv->v,(P)BDY(t));
                   3098:                 max = MAX(e,max);
                   3099:             }
1.157     noro     3100:         }
                   3101:     nd_setup_parameters(nvar,max);
1.164     noro     3102:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3103:     ishomo = 1;
                   3104:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3105:         if ( nd_module ) {
1.172     noro     3106:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3107:                        else zpl = (LIST)BDY(t);
1.158     noro     3108:             c = (pointer)pltondv(CO,vv,zpl);
                   3109:         } else {
1.172     noro     3110:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3111:                        else zp = (P)BDY(t);
1.158     noro     3112:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3113:         }
1.157     noro     3114:         if ( ishomo )
                   3115:             ishomo = ishomo && ndv_ishomo(c);
                   3116:         if ( c ) {
                   3117:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3118:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3119:         }
                   3120:     }
                   3121:     if ( in0 ) NEXT(in) = 0;
                   3122:     if ( fd0 ) NEXT(fd) = 0;
                   3123:     if ( !ishomo && homo ) {
                   3124:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3125:             c = (NDV)BDY(t); len = LEN(c);
                   3126:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3127:                 wmax = MAX(TD(DL(a)),wmax);
                   3128:         }
                   3129:         homogenize_order(ord,nvar,&ord1);
                   3130:         nd_init_ord(ord1);
                   3131:         nd_setup_parameters(nvar+1,wmax);
                   3132:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3133:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3134:     }
                   3135:     while ( 1 ) {
                   3136:         if ( Demand )
                   3137:             nd_demand = 1;
                   3138:         ndv_setup(m,1,fd0,0,0);
1.172     noro     3139:         if ( nd_gentrace ) {
1.167     noro     3140:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3141:         }
                   3142:         cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
1.157     noro     3143:         if ( !cand ) {
                   3144:             /* failure */
                   3145:             if ( trace > 1 ) { *rp = 0; return; }
                   3146:             else m = get_lprime(++mindex);
                   3147:             continue;
                   3148:         }
                   3149:         if ( !ishomo && homo ) {
                   3150:             /* dehomogenization */
                   3151:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3152:             nd_init_ord(ord);
                   3153:             nd_setup_parameters(nvar,0);
                   3154:         }
                   3155:         nd_demand = 0;
1.167     noro     3156:         cand = ndv_reducebase(cand,perm);
1.172     noro     3157:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3158:         cand = ndv_reduceall(0,cand);
                   3159:         cbpe = nd_bpe;
1.172     noro     3160:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3161:         get_eg(&eg0);
1.157     noro     3162:         if ( nocheck )
                   3163:             break;
1.170     noro     3164:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3165:             if ( nd_gentrace ) {
1.168     noro     3166:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3167:                    } else tl3 = 0;
                   3168:             /* gbcheck : cand is a GB of Id(cand) ? */
1.172     noro     3169:             ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3170:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3171:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3172:                    } else tl4 = 0;
                   3173:                }
                   3174:                if ( ret ) break;
1.157     noro     3175:         else if ( trace > 1 ) {
                   3176:             /* failure */
                   3177:             *rp = 0; return;
                   3178:         } else {
                   3179:             /* try the next modulus */
                   3180:             m = get_lprime(++mindex);
                   3181:             /* reset the parameters */
                   3182:             if ( !ishomo && homo ) {
                   3183:                 nd_init_ord(ord1);
                   3184:                 nd_setup_parameters(nvar+1,wmax);
                   3185:             } else {
                   3186:                 nd_init_ord(ord);
                   3187:                 nd_setup_parameters(nvar,max);
                   3188:             }
                   3189:         }
                   3190:     }
                   3191:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3192:     if ( DP_Print )
                   3193:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3194:     /* dp->p */
                   3195:     nd_bpe = cbpe;
                   3196:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3197:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3198:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3199:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3200:     }
1.157     noro     3201:     if ( nalg )
                   3202:         cand = postprocess_algcoef(av,alist,cand);
                   3203:     MKLIST(*rp,cand);
1.172     noro     3204:     if ( nd_gentrace ) {
1.167     noro     3205:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3206:                tl3 = reverse_node(tl3);
1.167     noro     3207:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3208:         for ( t = tl2; t; t = NEXT(t) ) {
                   3209:                        /* s = [i,[*,j,*,*],...] */
                   3210:             s = BDY((LIST)BDY(t));
                   3211:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3212:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3213:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3214:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3215:             }
                   3216:                }
                   3217:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3218:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3219:                }
1.168     noro     3220:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3221:                MKLIST(l5,tl4);
1.169     noro     3222:         tr = mknode(7,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5); MKLIST(*rp,tr);
1.167     noro     3223:     }
1.157     noro     3224: }
1.52      noro     3225:
1.157     noro     3226: /* XXX : module element is not considered  */
1.1       noro     3227:
1.61      noro     3228: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3229: {
1.157     noro     3230:     UINT *d;
                   3231:     int i,j,l,s,ord_l;
                   3232:     struct order_pair *op;
                   3233:
                   3234:     d = dl->d;
                   3235:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3236:     if ( nd_blockmask ) {
                   3237:         l = nd_blockmask->n;
                   3238:         op = nd_blockmask->order_pair;
                   3239:         for ( j = 0, s = 0; j < l; j++ ) {
                   3240:             ord_l = op[j].length;
                   3241:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3242:         }
                   3243:         TD(r) = ndl_weight(r);
                   3244:         ndl_weight_mask(r);
                   3245:     } else {
                   3246:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3247:         TD(r) = ndl_weight(r);
                   3248:     }
1.1       noro     3249: }
                   3250:
1.61      noro     3251: DL ndltodl(int n,UINT *ndl)
1.1       noro     3252: {
1.157     noro     3253:     DL dl;
                   3254:     int *d;
                   3255:     int i,j,l,s,ord_l;
                   3256:     struct order_pair *op;
                   3257:
                   3258:     NEWDL(dl,n);
                   3259:     dl->td = TD(ndl);
                   3260:     d = dl->d;
                   3261:     if ( nd_blockmask ) {
                   3262:         l = nd_blockmask->n;
                   3263:         op = nd_blockmask->order_pair;
                   3264:         for ( j = 0, s = 0; j < l; j++ ) {
                   3265:             ord_l = op[j].length;
                   3266:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3267:         }
                   3268:     } else {
                   3269:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3270:     }
                   3271:     return dl;
1.1       noro     3272: }
                   3273:
1.167     noro     3274: void nmtodp(int mod,NM m,DP *r)
                   3275: {
                   3276:     DP dp;
                   3277:     MP mr;
                   3278:
                   3279:     NEWMP(mr);
                   3280:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3281:     mr->c = ndctop(mod,m->c);
                   3282:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3283:     *r = dp;
                   3284: }
                   3285:
1.61      noro     3286: void ndl_print(UINT *dl)
1.1       noro     3287: {
1.157     noro     3288:     int n;
                   3289:     int i,j,l,ord_l,s,s0;
                   3290:     struct order_pair *op;
                   3291:
                   3292:     n = nd_nvar;
                   3293:     printf("<<");
                   3294:     if ( nd_blockmask ) {
                   3295:         l = nd_blockmask->n;
                   3296:         op = nd_blockmask->order_pair;
                   3297:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3298:             ord_l = op[j].length;
                   3299:             for ( i = 0; i < ord_l; i++, s++ )
                   3300:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3301:         }
                   3302:     } else {
                   3303:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3304:     }
                   3305:     printf(">>");
                   3306:     if ( MPOS(dl) )
                   3307:         printf("*e%d",MPOS(dl));
1.1       noro     3308: }
                   3309:
                   3310: void nd_print(ND p)
                   3311: {
1.157     noro     3312:     NM m;
1.1       noro     3313:
1.157     noro     3314:     if ( !p )
                   3315:         printf("0\n");
                   3316:     else {
                   3317:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3318:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3319:             else printf("+%d*",CM(m));
                   3320:             ndl_print(DL(m));
                   3321:         }
                   3322:         printf("\n");
                   3323:     }
1.1       noro     3324: }
                   3325:
1.113     noro     3326: void nd_print_q(ND p)
1.16      noro     3327: {
1.157     noro     3328:     NM m;
1.16      noro     3329:
1.157     noro     3330:     if ( !p )
                   3331:         printf("0\n");
                   3332:     else {
                   3333:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3334:             printf("+");
                   3335:             printexpr(CO,(Obj)CQ(m));
                   3336:             printf("*");
                   3337:             ndl_print(DL(m));
                   3338:         }
                   3339:         printf("\n");
                   3340:     }
1.16      noro     3341: }
                   3342:
1.1       noro     3343: void ndp_print(ND_pairs d)
                   3344: {
1.157     noro     3345:     ND_pairs t;
1.1       noro     3346:
1.157     noro     3347:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3348:     printf("\n");
1.1       noro     3349: }
                   3350:
1.20      noro     3351: void nd_removecont(int mod,ND p)
1.16      noro     3352: {
1.157     noro     3353:     int i,n;
                   3354:     Q *w;
                   3355:     Q dvr,t;
                   3356:     NM m;
                   3357:     struct oVECT v;
                   3358:     N q,r;
                   3359:
                   3360:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   3361:     else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   3362:     else {
                   3363:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3364:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3365:         v.len = n;
                   3366:         v.body = (pointer *)w;
                   3367:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3368:         removecont_array((P *)w,n,1);
                   3369:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3370:     }
1.16      noro     3371: }
                   3372:
1.21      noro     3373: void nd_removecont2(ND p1,ND p2)
                   3374: {
1.157     noro     3375:     int i,n1,n2,n;
                   3376:     Q *w;
                   3377:     Q dvr,t;
                   3378:     NM m;
                   3379:     struct oVECT v;
                   3380:     N q,r;
                   3381:
                   3382:     n1 = nd_length(p1);
                   3383:     n2 = nd_length(p2);
                   3384:     n = n1+n2;
                   3385:     w = (Q *)ALLOCA(n*sizeof(Q));
                   3386:     v.len = n;
                   3387:     v.body = (pointer *)w;
                   3388:     i = 0;
                   3389:     if ( p1 )
                   3390:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3391:     if ( p2 )
                   3392:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3393:     removecont_array((P *)w,n,1);
                   3394:     i = 0;
                   3395:     if ( p1 )
                   3396:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3397:     if ( p2 )
                   3398:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     3399: }
                   3400:
1.20      noro     3401: void ndv_removecont(int mod,NDV p)
1.16      noro     3402: {
1.157     noro     3403:     int i,len,all_p;
                   3404:     Q *c;
                   3405:     P *w;
                   3406:     Q dvr,t;
                   3407:     P g,cont,tp;
                   3408:     NMV m;
                   3409:
                   3410:     if ( mod == -1 )
                   3411:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   3412:     else if ( mod )
                   3413:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   3414:     else {
                   3415:         len = p->len;
                   3416:         w = (P *)ALLOCA(len*sizeof(P));
                   3417:         c = (Q *)ALLOCA(len*sizeof(Q));
                   3418:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   3419:             ptozp(CP(m),1,&c[i],&w[i]);
                   3420:             all_p = all_p && !NUM(w[i]);
                   3421:         }
                   3422:         if ( all_p ) {
                   3423:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   3424:             mulp(nd_vc,(P)dvr,g,&cont);
                   3425:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3426:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   3427:             }
                   3428:         } else {
                   3429:             sortbynm((Q *)c,len);
                   3430:             qltozl((Q *)c,len,&dvr);
                   3431:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3432:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   3433:             }
                   3434:         }
                   3435:     }
1.21      noro     3436: }
                   3437:
1.157     noro     3438: /* koko */
                   3439:
1.164     noro     3440: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     3441: {
1.157     noro     3442:     int len,i,max;
                   3443:     NMV m,mr0,mr,t;
1.61      noro     3444:
1.157     noro     3445:     len = p->len;
                   3446:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   3447:         max = MAX(max,TD(DL(m)));
                   3448:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3449:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   3450:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3451:     t = (NMV)ALLOCA(nmv_adv);
                   3452:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     3453:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     3454:         CQ(mr) = CQ(m);
                   3455:         ndl_copy(DL(t),DL(mr));
                   3456:     }
                   3457:     NV(p)++;
                   3458:     BDY(p) = mr0;
1.61      noro     3459: }
                   3460:
1.45      noro     3461: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     3462: {
1.164     noro     3463:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     3464:     int pos;
1.157     noro     3465:     Q *w;
                   3466:     Q dvr,t;
                   3467:     NMV m,r;
                   3468:
                   3469:     len = p->len;
                   3470:     newnvar = nd_nvar-1;
                   3471:     newexporigin = nd_get_exporigin(ord);
1.167     noro     3472:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     3473:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   3474:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   3475:         ndl_dehomogenize(DL(m));
                   3476:     if ( newwpd != nd_wpd ) {
                   3477:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   3478:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   3479:             CQ(r) = CQ(m);
1.167     noro     3480:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     3481:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   3482:             adj = nd_exporigin-newexporigin;
                   3483:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     3484:             if ( nd_module ) {
                   3485:                 DL(r)[newmpos] = pos;
                   3486:             }
1.157     noro     3487:         }
                   3488:     }
                   3489:     NV(p)--;
1.23      noro     3490: }
                   3491:
1.150     noro     3492: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   3493: {
1.157     noro     3494:     int i;
                   3495:     P *tpl,*tpl1;
                   3496:     NODE l;
                   3497:     P h,gcd,t;
                   3498:
                   3499:     tpl = (P *)ALLOCA(m*sizeof(P));
                   3500:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   3501:     bcopy(pl,tpl,m*sizeof(P));
                   3502:     gcd = (P)ONE;
                   3503:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   3504:         h = (P)BDY(l);
                   3505:         while ( 1 ) {
                   3506:             for ( i = 0; i < m; i++ )
                   3507:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   3508:                     break;
                   3509:             if ( i == m ) {
                   3510:                 bcopy(tpl1,tpl,m*sizeof(P));
                   3511:                 mulp(vl,gcd,h,&t); gcd = t;
                   3512:             } else
                   3513:                 break;
                   3514:         }
                   3515:     }
                   3516:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   3517:     if ( full ) {
                   3518:         heu_nezgcdnpz(vl,tpl,m,&t);
                   3519:         mulp(vl,gcd,t,pr);
                   3520:     } else
                   3521:         *pr = gcd;
1.150     noro     3522: }
                   3523:
                   3524: void removecont_array(P *p,int n,int full)
1.146     noro     3525: {
1.157     noro     3526:     int all_p,all_q,i;
                   3527:     Q *c;
                   3528:     P *w;
                   3529:     P t,s;
                   3530:
                   3531:     for ( all_q = 1, i = 0; i < n; i++ )
                   3532:         all_q = all_q && NUM(p[i]);
                   3533:     if ( all_q ) {
                   3534:         removecont_array_q((Q *)p,n);
                   3535:     } else {
                   3536:         c = (Q *)ALLOCA(n*sizeof(Q));
                   3537:         w = (P *)ALLOCA(n*sizeof(P));
                   3538:         for ( i = 0; i < n; i++ ) {
                   3539:             ptozp(p[i],1,&c[i],&w[i]);
                   3540:         }
                   3541:         removecont_array_q(c,n);
                   3542:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   3543:         for ( i = 0; i < n; i++ ) {
                   3544:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   3545:         }
                   3546:     }
1.146     noro     3547: }
                   3548:
                   3549: void removecont_array_q(Q *c,int n)
1.21      noro     3550: {
1.157     noro     3551:     struct oVECT v;
                   3552:     Q d0,d1,a,u,u1,gcd;
                   3553:     int i,j;
                   3554:     N qn,rn,gn;
                   3555:     Q *q,*r;
                   3556:
                   3557:     q = (Q *)ALLOCA(n*sizeof(Q));
                   3558:     r = (Q *)ALLOCA(n*sizeof(Q));
                   3559:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   3560:     igcdv_estimate(&v,&d0);
                   3561:     for ( i = 0; i < n; i++ ) {
                   3562:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   3563:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   3564:         NTOQ(rn,SGN(c[i]),r[i]);
                   3565:     }
                   3566:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   3567:     if ( i < n ) {
                   3568:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   3569:         igcdv(&v,&d1);
                   3570:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   3571:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   3572:         for ( i = 0; i < n; i++ ) {
                   3573:             mulq(a,q[i],&u);
                   3574:             if ( r[i] ) {
                   3575:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   3576:                 addq(u,u1,&q[i]);
                   3577:             } else
                   3578:                 q[i] = u;
                   3579:         }
                   3580:     }
                   3581:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     3582: }
                   3583:
1.19      noro     3584: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     3585: {
1.157     noro     3586:     NM m;
                   3587:     int c,c1;
1.1       noro     3588:
1.157     noro     3589:     if ( !p ) return;
                   3590:     if ( mul == 1 ) return;
                   3591:     if ( mod == -1 )
                   3592:         for ( m = BDY(p); m; m = NEXT(m) )
                   3593:             CM(m) = _mulsf(CM(m),mul);
                   3594:     else
                   3595:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3596:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3597:         }
1.1       noro     3598: }
                   3599:
1.146     noro     3600: void nd_mul_c_q(ND p,P mul)
1.16      noro     3601: {
1.157     noro     3602:     NM m;
                   3603:     P c;
1.16      noro     3604:
1.157     noro     3605:     if ( !p ) return;
                   3606:     if ( UNIQ(mul) ) return;
                   3607:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3608:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   3609:     }
1.16      noro     3610: }
                   3611:
1.61      noro     3612: void nd_mul_c_p(VL vl,ND p,P mul)
                   3613: {
1.157     noro     3614:     NM m;
                   3615:     P c;
1.61      noro     3616:
1.157     noro     3617:     if ( !p ) return;
                   3618:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3619:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3620:     }
1.61      noro     3621: }
                   3622:
1.1       noro     3623: void nd_free(ND p)
                   3624: {
1.157     noro     3625:     NM t,s;
1.1       noro     3626:
1.157     noro     3627:     if ( !p ) return;
                   3628:     t = BDY(p);
                   3629:     while ( t ) {
                   3630:         s = NEXT(t);
                   3631:         FREENM(t);
                   3632:         t = s;
                   3633:     }
                   3634:     FREEND(p);
1.1       noro     3635: }
                   3636:
1.23      noro     3637: void ndv_free(NDV p)
                   3638: {
1.157     noro     3639:     GC_free(BDY(p));
1.23      noro     3640: }
                   3641:
1.61      noro     3642: void nd_append_red(UINT *d,int i)
1.1       noro     3643: {
1.157     noro     3644:     RHist m,m0;
                   3645:     int h;
1.1       noro     3646:
1.157     noro     3647:     NEWRHist(m);
                   3648:     h = ndl_hash_value(d);
                   3649:     m->index = i;
                   3650:     ndl_copy(d,DL(m));
                   3651:     NEXT(m) = nd_red[h];
                   3652:     nd_red[h] = m;
1.1       noro     3653: }
                   3654:
1.61      noro     3655: UINT *ndv_compute_bound(NDV p)
1.1       noro     3656: {
1.157     noro     3657:     UINT *d1,*d2,*t;
                   3658:     UINT u;
                   3659:     int i,j,k,l,len,ind;
                   3660:     NMV m;
                   3661:
                   3662:     if ( !p )
                   3663:         return 0;
                   3664:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3665:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3666:     len = LEN(p);
                   3667:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3668:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     3669:         ndl_max(DL(m),d1,d2);
1.157     noro     3670:         t = d1; d1 = d2; d2 = t;
                   3671:     }
                   3672:     l = nd_nvar+31;
                   3673:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3674:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3675:         u = d1[i];
                   3676:         k = (nd_epw-1)*nd_bpe;
                   3677:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3678:             t[ind] = (u>>k)&nd_mask0;
                   3679:     }
                   3680:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3681:     return t;
1.1       noro     3682: }
                   3683:
1.99      noro     3684: UINT *nd_compute_bound(ND p)
                   3685: {
1.157     noro     3686:     UINT *d1,*d2,*t;
                   3687:     UINT u;
                   3688:     int i,j,k,l,len,ind;
                   3689:     NM m;
                   3690:
                   3691:     if ( !p )
                   3692:         return 0;
                   3693:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3694:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3695:     len = LEN(p);
                   3696:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   3697:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   3698:         ndl_lcm(DL(m),d1,d2);
                   3699:         t = d1; d1 = d2; d2 = t;
                   3700:     }
                   3701:     l = nd_nvar+31;
                   3702:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3703:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3704:         u = d1[i];
                   3705:         k = (nd_epw-1)*nd_bpe;
                   3706:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3707:             t[ind] = (u>>k)&nd_mask0;
                   3708:     }
                   3709:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   3710:     return t;
1.99      noro     3711: }
                   3712:
1.157     noro     3713: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   3714: /* of a term. In this case we need additional 1 word. */
                   3715:
1.48      noro     3716: int nd_get_exporigin(struct order_spec *ord)
                   3717: {
1.157     noro     3718:     switch ( ord->id ) {
                   3719:         case 0: case 2: case 256: case 258:
                   3720:             return 1+nd_module;
                   3721:         case 1: case 257:
                   3722:             /* block order */
                   3723:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   3724:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   3725:             return ord->ord.block.length+1+nd_module;
                   3726:         case 3: case 259:
                   3727:             error("nd_get_exporigin : composite order is not supported yet.");
                   3728:     }
1.48      noro     3729: }
                   3730:
1.61      noro     3731: void nd_setup_parameters(int nvar,int max) {
1.157     noro     3732:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   3733:     struct order_pair *op;
1.48      noro     3734:
1.157     noro     3735:     nd_nvar = nvar;
                   3736:     if ( max ) {
                   3737:         /* XXX */
                   3738:         if ( do_weyl ) nd_bpe = 32;
                   3739:         else if ( max < 2 ) nd_bpe = 1;
                   3740:         else if ( max < 4 ) nd_bpe = 2;
                   3741:         else if ( max < 8 ) nd_bpe = 3;
                   3742:         else if ( max < 16 ) nd_bpe = 4;
                   3743:         else if ( max < 32 ) nd_bpe = 5;
                   3744:         else if ( max < 64 ) nd_bpe = 6;
                   3745:         else if ( max < 256 ) nd_bpe = 8;
                   3746:         else if ( max < 1024 ) nd_bpe = 10;
                   3747:         else if ( max < 65536 ) nd_bpe = 16;
                   3748:         else nd_bpe = 32;
                   3749:     }
                   3750:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   3751:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3752:     nd_exporigin = nd_get_exporigin(nd_ord);
                   3753:     wpd = nd_exporigin+elen;
                   3754:     if ( nd_module )
                   3755:         nd_mpos = nd_exporigin-1;
                   3756:     else
                   3757:         nd_mpos = -1;
                   3758:     if ( wpd != nd_wpd ) {
                   3759:         nd_free_private_storage();
                   3760:         nd_wpd = wpd;
                   3761:     }
                   3762:     if ( nd_bpe < 32 ) {
                   3763:         nd_mask0 = (1<<nd_bpe)-1;
                   3764:     } else {
                   3765:         nd_mask0 = 0xffffffff;
                   3766:     }
                   3767:     bzero(nd_mask,sizeof(nd_mask));
                   3768:     nd_mask1 = 0;
                   3769:     for ( i = 0; i < nd_epw; i++ ) {
                   3770:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3771:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3772:     }
                   3773:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   3774:     nd_epos = nd_create_epos(nd_ord);
                   3775:     nd_blockmask = nd_create_blockmask(nd_ord);
                   3776:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3777: }
                   3778:
1.103     noro     3779: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3780: {
1.157     noro     3781:     int i,obpe,oadv,h;
                   3782:     static NM prev_nm_free_list;
                   3783:     static ND_pairs prev_ndp_free_list;
                   3784:     RHist mr0,mr;
                   3785:     RHist r;
                   3786:     RHist *old_red;
                   3787:     ND_pairs s0,s,t;
                   3788:     EPOS oepos;
                   3789:
                   3790:     obpe = nd_bpe;
                   3791:     oadv = nmv_adv;
                   3792:     oepos = nd_epos;
                   3793:     if ( obpe < 2 ) nd_bpe = 2;
                   3794:     else if ( obpe < 3 ) nd_bpe = 3;
                   3795:     else if ( obpe < 4 ) nd_bpe = 4;
                   3796:     else if ( obpe < 5 ) nd_bpe = 5;
                   3797:     else if ( obpe < 6 ) nd_bpe = 6;
                   3798:     else if ( obpe < 8 ) nd_bpe = 8;
                   3799:     else if ( obpe < 10 ) nd_bpe = 10;
                   3800:     else if ( obpe < 16 ) nd_bpe = 16;
                   3801:     else if ( obpe < 32 ) nd_bpe = 32;
                   3802:     else error("nd_reconstruct : exponent too large");
                   3803:
                   3804:     nd_setup_parameters(nd_nvar,0);
                   3805:     prev_nm_free_list = _nm_free_list;
                   3806:     prev_ndp_free_list = _ndp_free_list;
                   3807:     _nm_free_list = 0;
                   3808:     _ndp_free_list = 0;
                   3809:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   3810:     if ( trace )
                   3811:         for ( i = nd_psn-1; i >= 0; i-- )
                   3812:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   3813:     s0 = 0;
                   3814:     for ( t = d; t; t = NEXT(t) ) {
                   3815:         NEXTND_pairs(s0,s);
                   3816:         s->i1 = t->i1;
                   3817:         s->i2 = t->i2;
                   3818:         SG(s) = SG(t);
                   3819:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   3820:     }
                   3821:
                   3822:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   3823:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   3824:         old_red[i] = nd_red[i];
                   3825:         nd_red[i] = 0;
                   3826:     }
                   3827:     for ( i = 0; i < REDTAB_LEN; i++ )
                   3828:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   3829:             NEWRHist(mr);
                   3830:             mr->index = r->index;
                   3831:             SG(mr) = SG(r);
                   3832:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   3833:             h = ndl_hash_value(DL(mr));
                   3834:             NEXT(mr) = nd_red[h];
                   3835:             nd_red[h] = mr;
                   3836:         }
                   3837:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   3838:     old_red = 0;
                   3839:     for ( i = 0; i < nd_psn; i++ ) {
                   3840:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   3841:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   3842:         nd_psh[i] = r;
                   3843:     }
                   3844:     if ( s0 ) NEXT(s) = 0;
                   3845:     prev_nm_free_list = 0;
                   3846:     prev_ndp_free_list = 0;
1.71      noro     3847: #if 0
1.157     noro     3848:     GC_gcollect();
1.71      noro     3849: #endif
1.157     noro     3850:     return s0;
1.1       noro     3851: }
                   3852:
1.61      noro     3853: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     3854: {
1.157     noro     3855:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   3856:     struct order_pair *op;
1.1       noro     3857:
1.157     noro     3858:     n = nd_nvar;
                   3859:     oepw = (sizeof(UINT)*8)/obpe;
                   3860:     omask0 = (1<<obpe)-1;
                   3861:     TD(r) = TD(d);
                   3862:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   3863:     if ( nd_blockmask ) {
                   3864:         l = nd_blockmask->n;
                   3865:         op = nd_blockmask->order_pair;
                   3866:         for ( i = 1; i < nd_exporigin; i++ )
                   3867:             r[i] = d[i];
                   3868:         for ( j = 0, s = 0; j < l; j++ ) {
                   3869:             ord_l = op[j].length;
                   3870:             for ( i = 0; i < ord_l; i++, s++ ) {
                   3871:                 ei =  GET_EXP_OLD(d,s);
                   3872:                 PUT_EXP(r,s,ei);
                   3873:             }
                   3874:         }
                   3875:     } else {
                   3876:         for ( i = 0; i < n; i++ ) {
                   3877:             ei = GET_EXP_OLD(d,i);
                   3878:             PUT_EXP(r,i,ei);
                   3879:         }
                   3880:     }
                   3881:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     3882: }
1.3       noro     3883:
1.6       noro     3884: ND nd_copy(ND p)
                   3885: {
1.157     noro     3886:     NM m,mr,mr0;
                   3887:     int c,n;
                   3888:     ND r;
                   3889:
                   3890:     if ( !p )
                   3891:         return 0;
                   3892:     else {
                   3893:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3894:             NEXTNM(mr0,mr);
                   3895:             CM(mr) = CM(m);
                   3896:             ndl_copy(DL(m),DL(mr));
                   3897:         }
                   3898:         NEXT(mr) = 0;
                   3899:         MKND(NV(p),mr0,LEN(p),r);
                   3900:         SG(r) = SG(p);
                   3901:         return r;
                   3902:     }
1.6       noro     3903: }
                   3904:
1.53      noro     3905: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     3906: {
1.157     noro     3907:     NM m1,m2;
                   3908:     NDV p1,p2;
                   3909:     ND t1,t2;
                   3910:     UINT *lcm;
                   3911:     P gp,tp;
1.167     noro     3912:     Q g,t,iq;
1.157     noro     3913:     int td;
1.167     noro     3914:     LIST hist;
                   3915:     NODE node;
                   3916:     DP d;
1.157     noro     3917:
                   3918:     if ( !mod && nd_demand ) {
                   3919:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   3920:     } else {
                   3921:         if ( trace ) {
                   3922:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3923:         } else {
                   3924:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3925:         }
                   3926:     }
                   3927:     lcm = LCM(p);
                   3928:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   3929:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   3930:         FREENM(m1); return 0;
                   3931:     }
                   3932:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   3933:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   3934:         FREENM(m1); FREENM(m2); return 0;
                   3935:     }
                   3936:
                   3937:     if ( mod == -1 ) {
                   3938:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   3939:     } else if ( mod ) {
                   3940:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   3941:     } else if ( nd_vc ) {
                   3942:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   3943:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   3944:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   3945:     } else {
                   3946:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   3947:     }
                   3948:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   3949:     *rp = nd_add(mod,t1,t2);
1.172     noro     3950:     if ( nd_gentrace ) {
1.167     noro     3951:         /* nd_tracelist is initialized */
                   3952:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   3953:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   3954:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   3955:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   3956:         nd_tracelist = node;
                   3957:     }
1.157     noro     3958:     FREENM(m1); FREENM(m2);
                   3959:     return 1;
1.11      noro     3960: }
                   3961:
1.19      noro     3962: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     3963: {
1.157     noro     3964:     NMV m;
                   3965:     int c,c1,len,i;
1.11      noro     3966:
1.157     noro     3967:     if ( !p ) return;
                   3968:     len = LEN(p);
                   3969:     if ( mod == -1 )
                   3970:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3971:             CM(m) = _mulsf(CM(m),mul);
                   3972:     else
                   3973:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3974:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3975:         }
1.11      noro     3976: }
                   3977:
1.113     noro     3978: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     3979: {
1.157     noro     3980:     NMV m;
                   3981:     Q c;
                   3982:     int len,i;
                   3983:
                   3984:     if ( !p ) return;
                   3985:     len = LEN(p);
                   3986:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3987:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   3988:     }
1.16      noro     3989: }
                   3990:
1.55      noro     3991: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     3992:     int n2,i,j,l,n,tlen;
                   3993:     UINT *d0;
                   3994:     NM *tab,*psum;
                   3995:     ND s,r;
                   3996:     NM t;
                   3997:     NMV m1;
                   3998:
                   3999:     if ( !p ) return 0;
                   4000:     n = NV(p); n2 = n>>1;
                   4001:     d0 = DL(m0);
                   4002:     l = LEN(p);
                   4003:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4004:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4005:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4006:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4007:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4008:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4009:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4010:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4011:         for ( j = 0; j < tlen; j++ ) {
                   4012:             if ( tab[j] ) {
                   4013:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4014:             }
                   4015:         }
                   4016:     }
                   4017:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4018:         if ( psum[i] ) {
                   4019:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4020:             MKND(n,psum[i],j,s);
                   4021:             r = nd_add(mod,r,s);
                   4022:         }
                   4023:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4024:     return r;
1.55      noro     4025: }
                   4026:
1.56      noro     4027: /* product of monomials */
                   4028: /* XXX block order is not handled correctly */
                   4029:
1.55      noro     4030: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4031: {
1.157     noro     4032:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4033:     UINT *d0,*d1,*d,*dt,*ctab;
                   4034:     Q *ctab_q;
                   4035:     Q q,q1;
                   4036:     UINT c0,c1,c;
                   4037:     NM *p;
                   4038:     NM m,t;
                   4039:     int mpos;
                   4040:
                   4041:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4042:     if ( !m0 || !m1 ) return;
                   4043:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4044:     if ( nd_module )
                   4045:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4046:
1.157     noro     4047:     NEWNM(m); d = DL(m);
                   4048:     if ( mod ) {
                   4049:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   4050:     } else
                   4051:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4052:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4053:     homo = n&1 ? 1 : 0;
                   4054:     if ( homo ) {
                   4055:         /* offset of h-degree */
                   4056:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4057:         PUT_EXP(DL(m),n-1,h);
                   4058:         TD(DL(m)) = h;
                   4059:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4060:     }
                   4061:     tab[0] = m;
                   4062:     NEWNM(m); d = DL(m);
                   4063:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4064:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4065:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4066:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4067:         a += l; b += k;
                   4068:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4069:         if ( !k || !l ) {
                   4070:             for ( j = 0; j < curlen; j++ )
                   4071:                 if ( t = tab[j] ) {
                   4072:                     dt = DL(t);
                   4073:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4074:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4075:                 }
                   4076:             curlen *= k+1;
                   4077:             continue;
                   4078:         }
                   4079:         min = MIN(k,l);
                   4080:         if ( mod ) {
                   4081:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4082:             mkwcm(k,l,mod,ctab);
                   4083:         } else {
                   4084:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4085:             mkwc(k,l,ctab_q);
                   4086:         }
                   4087:         for ( j = min; j >= 0; j-- ) {
                   4088:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4089:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4090:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4091:             if ( homo ) {
                   4092:                 TD(d) = s;
                   4093:                 PUT_EXP(d,n-1,s-h);
                   4094:             } else TD(d) = h;
                   4095:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4096:             if ( mod ) c = ctab[j];
                   4097:             else q = ctab_q[j];
                   4098:             p = tab+curlen*j;
                   4099:             if ( j == 0 ) {
                   4100:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4101:                     if ( tab[u] ) {
                   4102:                         ndl_addto(DL(tab[u]),d);
                   4103:                         if ( mod ) {
                   4104:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   4105:                         } else {
                   4106:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4107:                         }
                   4108:                     }
                   4109:                 }
                   4110:             } else {
                   4111:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4112:                     if ( tab[u] ) {
                   4113:                         NEWNM(t);
                   4114:                         ndl_add(DL(tab[u]),d,DL(t));
                   4115:                         if ( mod ) {
                   4116:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   4117:                         } else
                   4118:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4119:                         *p = t;
                   4120:                     }
                   4121:                 }
                   4122:             }
                   4123:         }
                   4124:         curlen *= k+1;
                   4125:     }
                   4126:     FREENM(m);
1.167     noro     4127:     if ( nd_module ) {
1.166     noro     4128:         mpos = MPOS(d1);
1.167     noro     4129:         for ( i = 0; i < tlen; i++ )
                   4130:             if ( tab[i] ) {
                   4131:                 d = DL(tab[i]);
                   4132:                 MPOS(d) = mpos;
                   4133:                 TD(d) = ndl_weight(d);
                   4134:             }
                   4135:     }
1.55      noro     4136: }
                   4137:
1.63      noro     4138: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4139: {
1.157     noro     4140:     NM mr,mr0;
                   4141:     NMV m;
                   4142:     UINT *d,*dt,*dm;
                   4143:     int c,n,td,i,c1,c2,len;
                   4144:     Q q;
                   4145:     ND r;
                   4146:
                   4147:     if ( !p ) return 0;
                   4148:     else {
                   4149:         n = NV(p); m = BDY(p);
                   4150:         d = DL(m0);
                   4151:         len = LEN(p);
                   4152:         mr0 = 0;
                   4153:         td = TD(d);
                   4154:         c = CM(m0);
                   4155:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4156:             NEXTNM(mr0,mr);
                   4157:             CM(mr) = 1;
                   4158:             ndl_add(DL(m),d,DL(mr));
                   4159:         }
                   4160:         NEXT(mr) = 0;
                   4161:         MKND(NV(p),mr0,len,r);
                   4162:         SG(r) = SG(p) + TD(d);
                   4163:         return r;
                   4164:     }
1.63      noro     4165: }
                   4166:
1.55      noro     4167: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4168: {
1.157     noro     4169:     NM mr,mr0;
                   4170:     NMV m;
                   4171:     UINT *d,*dt,*dm;
                   4172:     int c,n,td,i,c1,c2,len;
                   4173:     P q;
                   4174:     ND r;
                   4175:
                   4176:     if ( !p ) return 0;
                   4177:     else if ( do_weyl )
                   4178:         if ( mod == -1 )
                   4179:             error("ndv_mul_nm : not implemented (weyl)");
                   4180:         else
                   4181:             return weyl_ndv_mul_nm(mod,m0,p);
                   4182:     else {
                   4183:         n = NV(p); m = BDY(p);
                   4184:         d = DL(m0);
                   4185:         len = LEN(p);
                   4186:         mr0 = 0;
                   4187:         td = TD(d);
                   4188:         if ( mod == -1 ) {
                   4189:             c = CM(m0);
                   4190:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4191:                 NEXTNM(mr0,mr);
                   4192:                 CM(mr) = _mulsf(CM(m),c);
                   4193:                 ndl_add(DL(m),d,DL(mr));
                   4194:             }
                   4195:         } else if ( mod ) {
                   4196:             c = CM(m0);
                   4197:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4198:                 NEXTNM(mr0,mr);
                   4199:                 c1 = CM(m);
                   4200:                 DMAR(c1,c,0,mod,c2);
                   4201:                 CM(mr) = c2;
                   4202:                 ndl_add(DL(m),d,DL(mr));
                   4203:             }
                   4204:         } else {
                   4205:             q = CP(m0);
                   4206:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4207:                 NEXTNM(mr0,mr);
                   4208:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4209:                 ndl_add(DL(m),d,DL(mr));
                   4210:             }
                   4211:         }
                   4212:         NEXT(mr) = 0;
                   4213:         MKND(NV(p),mr0,len,r);
                   4214:         SG(r) = SG(p) + TD(d);
                   4215:         return r;
                   4216:     }
1.4       noro     4217: }
                   4218:
1.104     noro     4219: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4220: {
1.157     noro     4221:     NM mq0,mq;
                   4222:     NMV tm;
                   4223:     Q q;
                   4224:     int i,nv,sg,c,c1,c2,hindex;
                   4225:     ND p,t,r;
                   4226:     N tnm;
                   4227:
                   4228:     if ( bucket->m < 0 ) return 0;
                   4229:     else {
                   4230:         nv = NV(d);
                   4231:         mq0 = 0;
                   4232:         tm = (NMV)ALLOCA(nmv_adv);
                   4233:         while ( 1 ) {
                   4234:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4235:             if ( hindex < 0 ) break;
                   4236:             p = bucket->body[hindex];
                   4237:             NEXTNM(mq0,mq);
                   4238:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4239:             ndl_copy(DL(mq),DL(tm));
                   4240:             if ( mod ) {
                   4241:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4242:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4243:                 CM(tm) = mod-c;
                   4244:             } else {
                   4245:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4246:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4247:                 chsgnq(CQ(mq),&CQ(tm));
                   4248:             }
                   4249:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4250:             bucket->body[hindex] = nd_remove_head(p);
                   4251:             t = nd_remove_head(t);
                   4252:             add_pbucket(mod,bucket,t);
                   4253:         }
                   4254:         if ( !mq0 )
                   4255:             r = 0;
                   4256:         else {
                   4257:             NEXT(mq) = 0;
                   4258:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4259:             MKND(nv,mq0,i,r);
                   4260:             /* XXX */
                   4261:             SG(r) = HTD(r);
                   4262:         }
                   4263:         return r;
                   4264:     }
1.99      noro     4265: }
                   4266:
1.43      noro     4267: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4268: {
1.157     noro     4269:     NMV m,mr,mr0,t;
                   4270:     int len,i,k;
1.11      noro     4271:
1.157     noro     4272:     if ( !p ) return;
                   4273:     m = BDY(p); len = LEN(p);
                   4274:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4275:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4276:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4277:     t = (NMV)ALLOCA(nmv_adv);
                   4278:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4279:         CQ(t) = CQ(m);
                   4280:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4281:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4282:         CQ(mr) = CQ(t);
                   4283:         ndl_copy(DL(t),DL(mr));
                   4284:     }
                   4285:     BDY(p) = mr0;
1.61      noro     4286: }
                   4287:
                   4288: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4289: {
1.157     noro     4290:     NMV m,mr,mr0;
                   4291:     int len,i;
                   4292:     NDV r;
                   4293:
                   4294:     if ( !p ) return 0;
                   4295:     m = BDY(p); len = LEN(p);
                   4296:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4297:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4298:         ndl_zero(DL(mr));
                   4299:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4300:         CQ(mr) = CQ(m);
                   4301:     }
                   4302:     MKNDV(NV(p),mr0,len,r);
                   4303:     SG(r) = SG(p);
                   4304:     return r;
1.11      noro     4305: }
                   4306:
1.61      noro     4307: /* duplicate p */
                   4308:
                   4309: NDV ndv_dup(int mod,NDV p)
1.3       noro     4310: {
1.157     noro     4311:     NDV d;
                   4312:     NMV t,m,m0;
                   4313:     int i,len;
                   4314:
                   4315:     if ( !p ) return 0;
                   4316:     len = LEN(p);
                   4317:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4318:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4319:         ndl_copy(DL(t),DL(m));
                   4320:         CQ(m) = CQ(t);
                   4321:     }
                   4322:     MKNDV(NV(p),m0,len,d);
                   4323:     SG(d) = SG(p);
                   4324:     return d;
1.23      noro     4325: }
                   4326:
1.63      noro     4327: ND nd_dup(ND p)
                   4328: {
1.157     noro     4329:     ND d;
                   4330:     NM t,m,m0;
1.63      noro     4331:
1.157     noro     4332:     if ( !p ) return 0;
                   4333:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4334:         NEXTNM(m0,m);
                   4335:         ndl_copy(DL(t),DL(m));
                   4336:         CQ(m) = CQ(t);
                   4337:     }
                   4338:     if ( m0 ) NEXT(m) = 0;
                   4339:     MKND(NV(p),m0,LEN(p),d);
                   4340:     SG(d) = SG(p);
                   4341:     return d;
1.63      noro     4342: }
                   4343:
1.61      noro     4344: /* XXX if p->len == 0 then it represents 0 */
                   4345:
                   4346: void ndv_mod(int mod,NDV p)
                   4347: {
1.157     noro     4348:     NMV t,d;
                   4349:     int r,s,u;
                   4350:     int i,len,dlen;
                   4351:     P cp;
                   4352:     Q c;
                   4353:     Obj gfs;
                   4354:
                   4355:     if ( !p ) return;
                   4356:     len = LEN(p);
                   4357:     dlen = 0;
                   4358:     if ( mod == -1 )
                   4359:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4360:             simp_ff((Obj)CP(t),&gfs);
                   4361:             r = FTOIF(CONT((GFS)gfs));
                   4362:             CM(d) = r;
                   4363:             ndl_copy(DL(t),DL(d));
                   4364:             NMV_ADV(d);
                   4365:             dlen++;
                   4366:         }
                   4367:     else
                   4368:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4369:             if ( nd_vc ) {
                   4370:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4371:                 c = (Q)cp;
                   4372:             } else
                   4373:                 c = CQ(t);
                   4374:             r = rem(NM(c),mod);
                   4375:             if ( r ) {
                   4376:                 if ( SGN(c) < 0 )
                   4377:                     r = mod-r;
                   4378:                 if ( DN(c) ) {
                   4379:                     s = rem(DN(c),mod);
                   4380:                     if ( !s )
                   4381:                         error("ndv_mod : division by 0");
                   4382:                     s = invm(s,mod);
                   4383:                     DMAR(r,s,0,mod,u); r = u;
                   4384:                 }
                   4385:                 CM(d) = r;
                   4386:                 ndl_copy(DL(t),DL(d));
                   4387:                 NMV_ADV(d);
                   4388:                 dlen++;
                   4389:             }
                   4390:         }
                   4391:     LEN(p) = dlen;
1.61      noro     4392: }
                   4393:
                   4394: NDV ptondv(VL vl,VL dvl,P p)
                   4395: {
1.157     noro     4396:     ND nd;
                   4397:
                   4398:     nd = ptond(vl,dvl,p);
                   4399:     return ndtondv(0,nd);
                   4400: }
1.61      noro     4401:
1.157     noro     4402: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4403: {
                   4404:     NODE nd,nd1;
                   4405:     int n;
                   4406:     P *pl;
                   4407:     Q *cl;
                   4408:     int i;
                   4409:     P dmy;
                   4410:     Q dvr;
                   4411:     LIST r;
                   4412:
                   4413:     nd = BDY(l); n = length(nd);
                   4414:     pl = (P *)ALLOCA(n*sizeof(P));
                   4415:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4416:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4417:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4418:     qltozl(cl,n,&dvr);
                   4419:     nd = BDY(l);
                   4420:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4421:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4422:     }
                   4423:     nd = 0;
                   4424:     for ( i = n-1; i >= 0; i-- ) {
                   4425:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4426:     }
                   4427:     MKLIST(r,nd);
                   4428:     *pp = r;
                   4429: }
                   4430:
                   4431: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4432:
                   4433: NDV pltondv(VL vl,VL dvl,LIST p)
                   4434: {
                   4435:     int i;
                   4436:     NODE t;
                   4437:     ND r,ri;
                   4438:     NM m;
                   4439:
                   4440:     if ( !nd_module ) error("pltond : module order must be set");
                   4441:     r = 0;
                   4442:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4443:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4444:         if ( ri )
                   4445:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4446:                 MPOS(DL(m)) = i;
                   4447:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4448:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4449:             }
1.157     noro     4450:         r = nd_add(0,r,ri);
                   4451:     }
                   4452:     return ndtondv(0,r);
1.61      noro     4453: }
                   4454:
                   4455: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4456: {
1.157     noro     4457:     int n,i,j,k,e;
                   4458:     VL tvl;
                   4459:     V v;
                   4460:     DCP dc;
                   4461:     DCP *w;
                   4462:     ND r,s,t,u;
                   4463:     P x;
                   4464:     int c;
                   4465:     UINT *d;
                   4466:     NM m,m0;
                   4467:
                   4468:     if ( !p )
                   4469:         return 0;
                   4470:     else if ( NUM(p) ) {
                   4471:         NEWNM(m);
                   4472:         ndl_zero(DL(m));
                   4473:         CQ(m) = (Q)p;
                   4474:         NEXT(m) = 0;
                   4475:         MKND(nd_nvar,m,1,r);
                   4476:         SG(r) = 0;
                   4477:         return r;
                   4478:     } else {
                   4479:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4480:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4481:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4482:         for ( i = 0, tvl = dvl, v = VR(p);
                   4483:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4484:         if ( !tvl ) {
                   4485:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4486:                 t = ptond(vl,dvl,COEF(w[j]));
                   4487:                 pwrp(vl,x,DEG(w[j]),&p);
                   4488:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4489:             }
                   4490:             return s;
                   4491:         } else {
                   4492:             NEWNM(m0); d = DL(m0);
                   4493:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4494:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4495:                 TD(d) = MUL_WEIGHT(e,i);
                   4496:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4497:                 if ( nd_module ) MPOS(d) = 0;
                   4498:                 t = ptond(vl,dvl,COEF(w[j]));
                   4499:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4500:                     ndl_addto(DL(m),d);
                   4501:                 SG(t) += TD(d);
                   4502:                 s = nd_add(0,s,t);
                   4503:             }
                   4504:             FREENM(m0);
                   4505:             return s;
                   4506:         }
                   4507:     }
1.61      noro     4508: }
                   4509:
                   4510: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4511: {
1.157     noro     4512:     VL tvl;
                   4513:     int len,n,j,i,e;
                   4514:     NMV m;
                   4515:     Q q;
                   4516:     P c;
                   4517:     UINT *d;
                   4518:     P s,r,u,t,w;
                   4519:     GFS gfs;
                   4520:
                   4521:     if ( !p ) return 0;
                   4522:     else {
                   4523:         len = LEN(p);
                   4524:         n = NV(p);
                   4525:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4526:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4527:             if ( mod == -1 ) {
                   4528:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4529:             } else if ( mod ) {
                   4530:                 STOQ(CM(m),q); c = (P)q;
                   4531:             } else
                   4532:                 c = CP(m);
                   4533:             d = DL(m);
                   4534:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4535:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4536:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4537:             }
                   4538:             addp(vl,s,t,&u); s = u;
                   4539:         }
                   4540:         return s;
                   4541:     }
                   4542: }
                   4543:
                   4544: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4545: {
                   4546:     VL tvl;
                   4547:     int len,n,j,i,e;
                   4548:     NMV m;
                   4549:     Q q;
                   4550:     P c;
                   4551:     UINT *d;
                   4552:     P s,r,u,t,w;
                   4553:     GFS gfs;
                   4554:     P *a;
                   4555:     LIST l;
                   4556:     NODE nd,nd1;
                   4557:
                   4558:     if ( !p ) return 0;
                   4559:     else {
                   4560:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4561:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4562:         len = LEN(p);
                   4563:         n = NV(p);
                   4564:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4565:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4566:             if ( mod == -1 ) {
                   4567:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4568:             } else if ( mod ) {
                   4569:                 STOQ(CM(m),q); c = (P)q;
                   4570:             } else
                   4571:                 c = CP(m);
                   4572:             d = DL(m);
                   4573:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4574:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4575:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4576:             }
                   4577:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4578:         }
                   4579:         nd = 0;
                   4580:         for ( i = rank; i > 0; i-- ) {
                   4581:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4582:         }
                   4583:         MKLIST(l,nd);
                   4584:         return l;
                   4585:     }
1.3       noro     4586: }
                   4587:
1.61      noro     4588: NDV ndtondv(int mod,ND p)
1.11      noro     4589: {
1.157     noro     4590:     NDV d;
                   4591:     NMV m,m0;
                   4592:     NM t;
                   4593:     int i,len;
                   4594:
                   4595:     if ( !p ) return 0;
                   4596:     len = LEN(p);
                   4597:     if ( mod )
                   4598:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   4599:     else
                   4600:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4601: #if 0
1.157     noro     4602:     ndv_alloc += nmv_adv*len;
1.103     noro     4603: #endif
1.157     noro     4604:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4605:         ndl_copy(DL(t),DL(m));
                   4606:         CQ(m) = CQ(t);
                   4607:     }
                   4608:     MKNDV(NV(p),m0,len,d);
                   4609:     SG(d) = SG(p);
                   4610:     return d;
1.11      noro     4611: }
                   4612:
1.61      noro     4613: ND ndvtond(int mod,NDV p)
1.11      noro     4614: {
1.157     noro     4615:     ND d;
                   4616:     NM m,m0;
                   4617:     NMV t;
                   4618:     int i,len;
                   4619:
                   4620:     if ( !p ) return 0;
                   4621:     m0 = 0;
                   4622:     len = p->len;
                   4623:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4624:         NEXTNM(m0,m);
                   4625:         ndl_copy(DL(t),DL(m));
                   4626:         CQ(m) = CQ(t);
                   4627:     }
                   4628:     NEXT(m) = 0;
                   4629:     MKND(NV(p),m0,len,d);
                   4630:     SG(d) = SG(p);
                   4631:     return d;
1.11      noro     4632: }
                   4633:
1.3       noro     4634: void ndv_print(NDV p)
                   4635: {
1.157     noro     4636:     NMV m;
                   4637:     int i,len;
1.3       noro     4638:
1.157     noro     4639:     if ( !p ) printf("0\n");
                   4640:     else {
                   4641:         len = LEN(p);
                   4642:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4643:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   4644:             else printf("+%d*",CM(m));
                   4645:             ndl_print(DL(m));
                   4646:         }
                   4647:         printf("\n");
                   4648:     }
1.16      noro     4649: }
                   4650:
1.113     noro     4651: void ndv_print_q(NDV p)
1.16      noro     4652: {
1.157     noro     4653:     NMV m;
                   4654:     int i,len;
1.16      noro     4655:
1.157     noro     4656:     if ( !p ) printf("0\n");
                   4657:     else {
                   4658:         len = LEN(p);
                   4659:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4660:             printf("+");
                   4661:             printexpr(CO,(Obj)CQ(m));
                   4662:             printf("*");
                   4663:             ndl_print(DL(m));
                   4664:         }
                   4665:         printf("\n");
                   4666:     }
1.25      noro     4667: }
                   4668:
1.167     noro     4669: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     4670: {
1.157     noro     4671:     int len,i,j;
1.167     noro     4672:     NDVI w;
1.157     noro     4673:     NODE t,t0;
                   4674:
                   4675:     len = length(x);
1.167     noro     4676:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   4677:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   4678:         w[i].p = BDY(t); w[i].i = perm[i];
                   4679:     }
1.157     noro     4680:     for ( i = 0; i < len; i++ ) {
                   4681:         for ( j = 0; j < i; j++ ) {
1.167     noro     4682:             if ( w[i].p && w[j].p )
                   4683:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   4684:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     4685:         }
                   4686:     }
1.167     noro     4687:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   4688:         if ( w[i].p ) {
                   4689:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   4690:             perm[j++] = w[i].i;
                   4691:         }
1.157     noro     4692:     }
                   4693:     NEXT(t) = 0; x = t0;
                   4694:     return x;
1.11      noro     4695: }
1.32      noro     4696:
1.43      noro     4697: /* XXX incomplete */
                   4698:
1.32      noro     4699: void nd_init_ord(struct order_spec *ord)
                   4700: {
1.157     noro     4701:     nd_module = (ord->id >= 256);
                   4702:     switch ( ord->id ) {
                   4703:         case 0:
                   4704:             switch ( ord->ord.simple ) {
                   4705:                 case 0:
                   4706:                     nd_dcomp = 1;
                   4707:                     nd_isrlex = 1;
                   4708:                     break;
                   4709:                 case 1:
                   4710:                     nd_dcomp = 1;
                   4711:                     nd_isrlex = 0;
                   4712:                     break;
                   4713:                 case 2:
                   4714:                     nd_dcomp = 0;
                   4715:                     nd_isrlex = 0;
                   4716:                     ndl_compare_function = ndl_lex_compare;
                   4717:                     break;
                   4718:                 case 11:
                   4719:                     /* XXX */
                   4720:                     nd_dcomp = 0;
                   4721:                     nd_isrlex = 1;
                   4722:                     ndl_compare_function = ndl_ww_lex_compare;
                   4723:                     break;
                   4724:                 default:
                   4725:                     error("nd_gr : unsupported order");
                   4726:             }
                   4727:             break;
                   4728:         case 1:
                   4729:             /* block order */
                   4730:             /* XXX */
                   4731:             nd_dcomp = -1;
                   4732:             nd_isrlex = 0;
                   4733:             ndl_compare_function = ndl_block_compare;
                   4734:             break;
                   4735:         case 2:
                   4736:             /* matrix order */
                   4737:             /* XXX */
                   4738:             nd_dcomp = -1;
                   4739:             nd_isrlex = 0;
                   4740:             nd_matrix_len = ord->ord.matrix.row;
                   4741:             nd_matrix = ord->ord.matrix.matrix;
                   4742:             ndl_compare_function = ndl_matrix_compare;
                   4743:             break;
                   4744:         case 3:
                   4745:             /* composite order */
                   4746:             nd_dcomp = -1;
                   4747:             nd_isrlex = 0;
                   4748:             nd_worb_len = ord->ord.composite.length;
                   4749:             nd_worb = ord->ord.composite.w_or_b;
                   4750:             ndl_compare_function = ndl_composite_compare;
                   4751:             break;
                   4752:
                   4753:         /* module order */
                   4754:         case 256:
1.160     noro     4755:             nd_ispot = ord->ispot;
1.174     noro     4756:             nd_pot_nelim = ord->pot_nelim;
1.157     noro     4757:             nd_dcomp = -1;
                   4758:             switch ( ord->ord.simple ) {
                   4759:                 case 0:
1.167     noro     4760:                     nd_isrlex = 1;
1.157     noro     4761:                     ndl_compare_function = ndl_module_grlex_compare;
                   4762:                     break;
                   4763:                 case 1:
1.167     noro     4764:                     nd_isrlex = 0;
1.157     noro     4765:                     ndl_compare_function = ndl_module_glex_compare;
                   4766:                     break;
                   4767:                 case 2:
1.167     noro     4768:                     nd_isrlex = 0;
1.157     noro     4769:                     ndl_compare_function = ndl_module_lex_compare;
                   4770:                     break;
                   4771:                 default:
                   4772:                     error("nd_gr : unsupported order");
                   4773:             }
                   4774:             break;
                   4775:         case 257:
                   4776:             /* block order */
1.174     noro     4777:             nd_ispot = ord->ispot;
                   4778:             nd_pot_nelim = ord->pot_nelim;
                   4779:             nd_dcomp = -1;
                   4780:             nd_isrlex = 0;
1.157     noro     4781:             ndl_compare_function = ndl_module_block_compare;
                   4782:             break;
                   4783:         case 258:
                   4784:             /* matrix order */
1.174     noro     4785:             nd_ispot = ord->ispot;
                   4786:             nd_pot_nelim = ord->pot_nelim;
                   4787:             nd_dcomp = -1;
                   4788:             nd_isrlex = 0;
1.157     noro     4789:             nd_matrix_len = ord->ord.matrix.row;
                   4790:             nd_matrix = ord->ord.matrix.matrix;
                   4791:             ndl_compare_function = ndl_module_matrix_compare;
                   4792:             break;
                   4793:         case 259:
                   4794:             /* composite order */
1.174     noro     4795:             nd_ispot = ord->ispot;
                   4796:             nd_pot_nelim = ord->pot_nelim;
                   4797:             nd_dcomp = -1;
                   4798:             nd_isrlex = 0;
1.157     noro     4799:             nd_worb_len = ord->ord.composite.length;
                   4800:             nd_worb = ord->ord.composite.w_or_b;
                   4801:             ndl_compare_function = ndl_module_composite_compare;
                   4802:             break;
                   4803:     }
                   4804:     nd_ord = ord;
1.32      noro     4805: }
                   4806:
1.43      noro     4807: BlockMask nd_create_blockmask(struct order_spec *ord)
                   4808: {
1.157     noro     4809:     int n,i,j,s,l;
                   4810:     UINT *t;
                   4811:     BlockMask bm;
                   4812:
                   4813:     /* we only create mask table for block order */
1.164     noro     4814:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     4815:         return 0;
                   4816:     n = ord->ord.block.length;
                   4817:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   4818:     bm->n = n;
                   4819:     bm->order_pair = ord->ord.block.order_pair;
                   4820:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   4821:     for ( i = 0, s = 0; i < n; i++ ) {
                   4822:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   4823:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   4824:         l = bm->order_pair[i].length;
                   4825:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   4826:     }
                   4827:     return bm;
1.57      noro     4828: }
                   4829:
                   4830: EPOS nd_create_epos(struct order_spec *ord)
                   4831: {
1.157     noro     4832:     int i,j,l,s,ord_l,ord_o;
                   4833:     EPOS epos;
                   4834:     struct order_pair *op;
                   4835:
                   4836:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   4837:     switch ( ord->id ) {
1.164     noro     4838:         case 0: case 256:
1.157     noro     4839:             if ( nd_isrlex ) {
                   4840:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4841:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   4842:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   4843:                 }
                   4844:             } else {
                   4845:                 for ( i = 0; i < nd_nvar; i++ ) {
                   4846:                     epos[i].i = nd_exporigin + i/nd_epw;
                   4847:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4848:                 }
                   4849:             }
                   4850:             break;
1.164     noro     4851:         case 1: case 257:
1.157     noro     4852:             /* block order */
                   4853:             l = ord->ord.block.length;
                   4854:             op = ord->ord.block.order_pair;
                   4855:             for ( j = 0, s = 0; j < l; j++ ) {
                   4856:                 ord_o = op[j].order;
                   4857:                 ord_l = op[j].length;
                   4858:                 if ( !ord_o )
                   4859:                     for ( i = 0; i < ord_l; i++ ) {
                   4860:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   4861:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   4862:                     }
                   4863:                 else
                   4864:                     for ( i = 0; i < ord_l; i++ ) {
                   4865:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   4866:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   4867:                     }
                   4868:                 s += ord_l;
                   4869:             }
                   4870:             break;
                   4871:         case 2:
                   4872:             /* matrix order */
                   4873:         case 3:
                   4874:             /* composite order */
1.167     noro     4875:         default:
1.157     noro     4876:             for ( i = 0; i < nd_nvar; i++ ) {
                   4877:                 epos[i].i = nd_exporigin + i/nd_epw;
                   4878:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4879:             }
                   4880:             break;
                   4881:     }
                   4882:     return epos;
1.43      noro     4883: }
1.59      noro     4884:
                   4885: /* external interface */
                   4886:
                   4887: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   4888: {
1.157     noro     4889:     NODE t,in0,in;
                   4890:     ND nd,nf;
                   4891:     NDV ndv;
                   4892:     VL vv,tv;
                   4893:     int stat,nvar,max,e;
                   4894:     union oNDC dn;
                   4895:     Q cont;
                   4896:     P pp;
                   4897:
                   4898:     if ( !f ) {
                   4899:         *rp = 0;
                   4900:         return;
                   4901:     }
                   4902:     pltovl(v,&vv);
                   4903:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4904:
                   4905:     /* get the degree bound */
                   4906:     for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   4907:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4908:             e = getdeg(tv->v,(P)BDY(t));
                   4909:             max = MAX(e,max);
                   4910:         }
                   4911:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4912:         e = getdeg(tv->v,f);
                   4913:         max = MAX(e,max);
                   4914:     }
                   4915:
                   4916:     nd_init_ord(ord);
                   4917:     nd_setup_parameters(nvar,max);
                   4918:
                   4919:     /* conversion to ndv */
                   4920:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   4921:         NEXTNODE(in0,in);
                   4922:         ptozp((P)BDY(t),1,&cont,&pp);
                   4923:         BDY(in) = (pointer)ptondv(CO,vv,pp);
                   4924:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   4925:     }
                   4926:     NEXTNODE(in0,in);
                   4927:     BDY(in) = (pointer)ptondv(CO,vv,f);
                   4928:     if ( m ) ndv_mod(m,(NDV)BDY(in));
                   4929:     NEXT(in) = 0;
                   4930:
                   4931:     /* dont sort, dont removecont */
                   4932:     ndv_setup(m,0,in0,1,1);
                   4933:     nd_psn--;
                   4934:     nd_scale=2;
                   4935:     while ( 1 ) {
                   4936:         nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
                   4937:         stat = nd_nf(m,0,nd,nd_ps,1,0,&nf);
                   4938:         if ( !stat ) {
                   4939:             nd_psn++;
                   4940:             nd_reconstruct(0,0);
                   4941:             nd_psn--;
                   4942:         } else
                   4943:             break;
                   4944:     }
                   4945:     *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     4946: }
                   4947:
                   4948: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   4949: {
1.157     noro     4950:     NM m;
                   4951:     UINT *t,*s;
                   4952:     int i;
                   4953:
                   4954:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   4955:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   4956:         t = DL(m);
                   4957:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4958:         r[i] = CM(m);
                   4959:     }
                   4960:     for ( i = 0; !r[i]; i++ );
                   4961:     return i;
1.63      noro     4962: }
                   4963:
1.113     noro     4964: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     4965: {
1.157     noro     4966:     NM m;
                   4967:     UINT *t,*s;
                   4968:     int i;
                   4969:
                   4970:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   4971:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   4972:         t = DL(m);
                   4973:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4974:         r[i] = CQ(m);
                   4975:     }
                   4976:     for ( i = 0; !r[i]; i++ );
                   4977:     return i;
1.74      noro     4978: }
                   4979:
1.129     noro     4980: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   4981: {
1.157     noro     4982:     NM m;
                   4983:     NMV mr;
                   4984:     UINT *d,*t,*s;
                   4985:     NDV p;
                   4986:     int i,j,len;
                   4987:     Q *r;
                   4988:
                   4989:     m = pair->mul;
                   4990:     d = DL(m);
                   4991:     p = nd_ps[pair->index];
                   4992:     len = LEN(p);
                   4993:     r = (Q *)CALLOC(n,sizeof(Q));
                   4994:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4995:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   4996:         ndl_add(d,DL(mr),t);
                   4997:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4998:         r[i] = CQ(mr);
                   4999:     }
                   5000:     return r;
1.129     noro     5001: }
                   5002:
1.67      noro     5003: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     5004: {
1.157     noro     5005:     NM m;
                   5006:     NMV mr;
                   5007:     UINT *d,*t,*s;
                   5008:     NDV p;
                   5009:     unsigned char *ivc;
                   5010:     unsigned short *ivs;
                   5011:     UINT *v,*ivi,*s0v;
                   5012:     int i,j,len,prev,diff,cdiff;
                   5013:     IndArray r;
                   5014:
                   5015:     m = pair->mul;
                   5016:     d = DL(m);
                   5017:     p = nd_ps[pair->index];
                   5018:     len = LEN(p);
                   5019:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5020:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
                   5021:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5022:         ndl_add(d,DL(mr),t);
                   5023:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5024:         v[j] = i;
                   5025:     }
                   5026:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5027:     r->head = v[0];
                   5028:     diff = 0;
                   5029:     for ( i = 1; i < len; i++ ) {
                   5030:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5031:     }
                   5032:     if ( diff < 256 ) {
                   5033:         r->width = 1;
                   5034:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5035:         r->index.c = ivc;
                   5036:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5037:     } else if ( diff < 65536 ) {
                   5038:         r->width = 2;
                   5039:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5040:         r->index.s = ivs;
                   5041:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5042:     } else {
                   5043:         r->width = 4;
                   5044:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5045:         r->index.i = ivi;
                   5046:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5047:     }
                   5048:     return r;
1.64      noro     5049: }
                   5050:
1.135     noro     5051: int compress_array(Q *svect,Q *cvect,int n)
                   5052: {
1.157     noro     5053:     int i,j;
1.135     noro     5054:
1.157     noro     5055:     for ( i = j = 0; i < n; i++ )
                   5056:         if ( svect[i] ) cvect[j++] = svect[i];
                   5057:     return j;
1.135     noro     5058: }
                   5059:
                   5060: void expand_array(Q *svect,Q *cvect,int n)
                   5061: {
1.157     noro     5062:     int i,j;
1.135     noro     5063:
1.157     noro     5064:     for ( i = j = 0; j < n;  i++  )
                   5065:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5066: }
                   5067:
1.133     noro     5068: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5069: {
1.157     noro     5070:     int i,j,k,len,pos,prev,nz;
                   5071:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5072:     IndArray ivect;
                   5073:     unsigned char *ivc;
                   5074:     unsigned short *ivs;
                   5075:     unsigned int *ivi;
                   5076:     NDV redv;
                   5077:     NMV mr;
                   5078:     NODE rp;
                   5079:     int maxrs;
                   5080:     double hmag;
                   5081:     Q *cvect;
                   5082:
                   5083:     maxrs = 0;
                   5084:     for ( i = 0; i < col && !svect[i]; i++ );
                   5085:     if ( i == col ) return maxrs;
                   5086:     hmag = p_mag((P)svect[i])*nd_scale;
                   5087:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5088:     for ( i = 0; i < nred; i++ ) {
                   5089:         ivect = imat[i];
                   5090:         k = ivect->head;
                   5091:         if ( svect[k] ) {
                   5092:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5093:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5094:             len = LEN(redv); mr = BDY(redv);
                   5095:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5096:             chsgnq(cs,&mcs);
                   5097:             if ( !UNIQ(cr) ) {
                   5098:                 for ( j = 0; j < col; j++ ) {
                   5099:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5100:                 }
                   5101:             }
                   5102:             svect[k] = 0; prev = k;
                   5103:             switch ( ivect->width ) {
                   5104:                 case 1:
                   5105:                     ivc = ivect->index.c;
                   5106:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5107:                         pos = prev+ivc[j]; prev = pos;
                   5108:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5109:                     }
                   5110:                     break;
                   5111:                 case 2:
                   5112:                     ivs = ivect->index.s;
                   5113:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5114:                         pos = prev+ivs[j]; prev = pos;
                   5115:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5116:                     }
                   5117:                     break;
                   5118:                 case 4:
                   5119:                     ivi = ivect->index.i;
                   5120:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5121:                         pos = prev+ivi[j]; prev = pos;
                   5122:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5123:                     }
                   5124:                     break;
                   5125:             }
                   5126:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5127:             if ( j == col ) break;
                   5128:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5129:                 nz = compress_array(svect,cvect,col);
                   5130:                 removecont_array((P *)cvect,nz,1);
                   5131:                 expand_array(svect,cvect,nz);
                   5132:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5133:             }
                   5134:         }
                   5135:     }
                   5136:     nz = compress_array(svect,cvect,col);
                   5137:     removecont_array((P *)cvect,nz,1);
                   5138:     expand_array(svect,cvect,nz);
                   5139:     if ( DP_Print ) {
                   5140:         fprintf(asir_out,"-"); fflush(asir_out);
                   5141:     }
                   5142:     return maxrs;
1.107     noro     5143: }
                   5144:
1.76      noro     5145: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5146: {
1.157     noro     5147:     int i,j,k,len,pos,prev;
                   5148:     UINT c,c1,c2,c3,up,lo,dmy;
                   5149:     IndArray ivect;
                   5150:     unsigned char *ivc;
                   5151:     unsigned short *ivs;
                   5152:     unsigned int *ivi;
                   5153:     NDV redv;
                   5154:     NMV mr;
                   5155:     NODE rp;
                   5156:     int maxrs;
                   5157:
                   5158:     maxrs = 0;
                   5159:     for ( i = 0; i < nred; i++ ) {
                   5160:         ivect = imat[i];
                   5161:         k = ivect->head; svect[k] %= m;
                   5162:         if ( c = svect[k] ) {
                   5163:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5164:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5165:             len = LEN(redv); mr = BDY(redv);
                   5166:             svect[k] = 0; prev = k;
                   5167:             switch ( ivect->width ) {
                   5168:                 case 1:
                   5169:                     ivc = ivect->index.c;
                   5170:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5171:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5172:                         prev = pos;
                   5173:                         DMA(c1,c,c2,up,lo);
                   5174:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5175:                         } else svect[pos] = lo;
                   5176:                     }
                   5177:                     break;
                   5178:                 case 2:
                   5179:                     ivs = ivect->index.s;
                   5180:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5181:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5182:                         prev = pos;
                   5183:                         DMA(c1,c,c2,up,lo);
                   5184:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5185:                         } else svect[pos] = lo;
                   5186:                     }
                   5187:                     break;
                   5188:                 case 4:
                   5189:                     ivi = ivect->index.i;
                   5190:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5191:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5192:                         prev = pos;
                   5193:                         DMA(c1,c,c2,up,lo);
                   5194:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5195:                         } else svect[pos] = lo;
                   5196:                     }
                   5197:                     break;
                   5198:             }
                   5199:         }
                   5200:     }
                   5201:     for ( i = 0; i < col; i++ )
                   5202:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5203:     return maxrs;
1.65      noro     5204: }
                   5205:
1.76      noro     5206: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5207: {
1.157     noro     5208:     int i,j,k,len,pos,prev;
                   5209:     UINT c,c1,c2,c3,up,lo,dmy;
                   5210:     IndArray ivect;
                   5211:     unsigned char *ivc;
                   5212:     unsigned short *ivs;
                   5213:     unsigned int *ivi;
                   5214:     NDV redv;
                   5215:     NMV mr;
                   5216:     NODE rp;
                   5217:     int maxrs;
                   5218:
                   5219:     maxrs = 0;
                   5220:     for ( i = 0; i < nred; i++ ) {
                   5221:         ivect = imat[i];
                   5222:         k = ivect->head; svect[k] %= m;
                   5223:         if ( c = svect[k] ) {
                   5224:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5225:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5226:             len = LEN(redv); mr = BDY(redv);
                   5227:             svect[k] = 0; prev = k;
                   5228:             switch ( ivect->width ) {
                   5229:                 case 1:
                   5230:                     ivc = ivect->index.c;
                   5231:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5232:                         pos = prev+ivc[j]; prev = pos;
                   5233:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5234:                     }
                   5235:                     break;
                   5236:                 case 2:
                   5237:                     ivs = ivect->index.s;
                   5238:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5239:                         pos = prev+ivs[j]; prev = pos;
                   5240:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5241:                     }
                   5242:                     break;
                   5243:                 case 4:
                   5244:                     ivi = ivect->index.i;
                   5245:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5246:                         pos = prev+ivi[j]; prev = pos;
                   5247:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5248:                     }
                   5249:                     break;
                   5250:             }
                   5251:         }
                   5252:     }
                   5253:     return maxrs;
1.72      noro     5254: }
                   5255:
1.65      noro     5256: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5257: {
1.157     noro     5258:     int j,k,len;
                   5259:     UINT *p;
                   5260:     UINT c;
                   5261:     NDV r;
                   5262:     NMV mr0,mr;
                   5263:
                   5264:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5265:     if ( !len ) return 0;
                   5266:     else {
                   5267:         mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5268: #if 0
1.157     noro     5269:         ndv_alloc += nmv_adv*len;
1.103     noro     5270: #endif
1.157     noro     5271:         mr = mr0;
                   5272:         p = s0vect;
                   5273:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5274:             if ( !rhead[j] ) {
                   5275:                 if ( c = vect[k++] ) {
                   5276:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5277:                 }
                   5278:             }
                   5279:         MKNDV(nd_nvar,mr0,len,r);
                   5280:         return r;
                   5281:     }
1.65      noro     5282: }
                   5283:
1.129     noro     5284: /* for preprocessed vector */
                   5285:
1.113     noro     5286: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5287: {
1.157     noro     5288:     int j,k,len;
                   5289:     UINT *p;
                   5290:     Q c;
                   5291:     NDV r;
                   5292:     NMV mr0,mr;
                   5293:
                   5294:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5295:     if ( !len ) return 0;
                   5296:     else {
                   5297:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     5298: #if 0
1.157     noro     5299:         ndv_alloc += nmv_adv*len;
1.107     noro     5300: #endif
1.157     noro     5301:         mr = mr0;
                   5302:         p = s0vect;
                   5303:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5304:             if ( !rhead[j] ) {
                   5305:                 if ( c = vect[k++] ) {
                   5306:                     if ( DN(c) )
                   5307:                         error("afo");
                   5308:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5309:                 }
                   5310:             }
                   5311:         MKNDV(nd_nvar,mr0,len,r);
                   5312:         return r;
                   5313:     }
1.107     noro     5314: }
                   5315:
1.129     noro     5316: /* for plain vector */
                   5317:
                   5318: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5319: {
1.157     noro     5320:     int j,k,len;
                   5321:     UINT *p;
                   5322:     Q c;
                   5323:     NDV r;
                   5324:     NMV mr0,mr;
                   5325:
                   5326:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5327:     if ( !len ) return 0;
                   5328:     else {
                   5329:         mr0 = (NMV)GC_malloc(nmv_adv*len);
1.129     noro     5330: #if 0
1.157     noro     5331:         ndv_alloc += nmv_adv*len;
1.129     noro     5332: #endif
1.157     noro     5333:         mr = mr0;
                   5334:         p = s0vect;
                   5335:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5336:             if ( c = vect[k] ) {
                   5337:                 if ( DN(c) )
                   5338:                     error("afo");
                   5339:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5340:             }
                   5341:         MKNDV(nd_nvar,mr0,len,r);
                   5342:         return r;
                   5343:     }
1.129     noro     5344: }
                   5345:
1.133     noro     5346: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5347: {
1.157     noro     5348:     ND_pairs t;
                   5349:     NODE sp0,sp;
                   5350:     int stat;
                   5351:     ND spol;
                   5352:
                   5353:     for ( t = l; t; t = NEXT(t) ) {
                   5354:         stat = nd_sp(m,trace,t,&spol);
                   5355:         if ( !stat ) return 0;
                   5356:         if ( spol ) {
                   5357:             add_pbucket_symbolic(bucket,spol);
                   5358:         }
                   5359:     }
                   5360:     return 1;
1.65      noro     5361: }
                   5362:
1.133     noro     5363: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5364: {
1.157     noro     5365:     NODE rp0,rp;
                   5366:     NM mul,head,s0,s;
                   5367:     int index,col,i,sugar;
                   5368:     RHist h;
                   5369:     UINT *s0v,*p;
                   5370:     NM_ind_pair pair;
                   5371:     ND red;
                   5372:     NDV *ps;
                   5373:
                   5374:     s0 = 0; rp0 = 0; col = 0;
                   5375:     ps = trace?nd_ps_trace:nd_ps;
                   5376:     while ( 1 ) {
                   5377:         head = remove_head_pbucket_symbolic(bucket);
                   5378:         if ( !head ) break;
                   5379:         if ( !s0 ) s0 = head;
                   5380:         else NEXT(s) = head;
                   5381:         s = head;
                   5382:         index = ndl_find_reducer(DL(head));
                   5383:         if ( index >= 0 ) {
                   5384:             h = nd_psh[index];
                   5385:             NEWNM(mul);
                   5386:             ndl_sub(DL(head),DL(h),DL(mul));
                   5387:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5388:             sugar = TD(DL(mul))+SG(ps[index]);
                   5389:             MKNM_ind_pair(pair,mul,index,sugar);
                   5390:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5391:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5392:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5393:         }
                   5394:         col++;
                   5395:     }
                   5396:     if ( rp0 ) NEXT(rp) = 0;
                   5397:     NEXT(s) = 0;
                   5398:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5399:     for ( i = 0, p = s0v, s = s0; i < col;
                   5400:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5401:     *s0vect = s0v;
                   5402:     *r = rp0;
                   5403:     return col;
1.65      noro     5404: }
                   5405:
1.167     noro     5406: NODE nd_f4(int m,int **indp)
1.69      noro     5407: {
1.157     noro     5408:     int i,nh,stat,index;
                   5409:     NODE r,g;
                   5410:     ND_pairs d,l,t;
                   5411:     ND spol,red;
                   5412:     NDV nf,redv;
                   5413:     NM s0,s;
                   5414:     NODE rp0,srp0,nflist;
                   5415:     int nsp,nred,col,rank,len,k,j,a;
                   5416:     UINT c;
                   5417:     UINT **spmat;
                   5418:     UINT *s0vect,*svect,*p,*v;
                   5419:     int *colstat;
                   5420:     IndArray *imat;
                   5421:     int *rhead;
                   5422:     int spcol,sprow;
                   5423:     int sugar;
                   5424:     PGeoBucket bucket;
                   5425:     struct oEGT eg0,eg1,eg_f4;
1.69      noro     5426:
1.103     noro     5427: #if 0
1.157     noro     5428:     ndv_alloc = 0;
1.103     noro     5429: #endif
1.157     noro     5430:     g = 0; d = 0;
                   5431:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5432:         d = update_pairs(d,g,i,0);
1.157     noro     5433:         g = update_base(g,i);
                   5434:     }
                   5435:     while ( d ) {
                   5436:         get_eg(&eg0);
                   5437:         l = nd_minsugarp(d,&d);
                   5438:         sugar = SG(l);
                   5439:         bucket = create_pbucket();
                   5440:         stat = nd_sp_f4(m,0,l,bucket);
                   5441:         if ( !stat ) {
                   5442:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5443:             NEXT(t) = d; d = l;
                   5444:             d = nd_reconstruct(0,d);
                   5445:             continue;
                   5446:         }
                   5447:         if ( bucket->m < 0 ) continue;
                   5448:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5449:         if ( !col ) {
                   5450:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5451:             NEXT(t) = d; d = l;
                   5452:             d = nd_reconstruct(0,d);
                   5453:             continue;
                   5454:         }
                   5455:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5456:         if ( DP_Print )
                   5457:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5458:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5459:         if ( 1 )
                   5460:             nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
                   5461:         else
                   5462:             nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
                   5463:         /* adding new bases */
                   5464:         for ( r = nflist; r; r = NEXT(r) ) {
                   5465:             nf = (NDV)BDY(r);
                   5466:             ndv_removecont(m,nf);
                   5467:             if ( !m && nd_nalg ) {
                   5468:                 ND nf1;
                   5469:
                   5470:                 nf1 = ndvtond(m,nf);
                   5471:                 nd_monic(0,&nf1);
                   5472:                 nd_removecont(m,nf1);
                   5473:                 nf = ndtondv(m,nf1);
                   5474:             }
                   5475:             nh = ndv_newps(m,nf,0);
1.168     noro     5476:             d = update_pairs(d,g,nh,0);
1.157     noro     5477:             g = update_base(g,nh);
                   5478:         }
                   5479:     }
1.103     noro     5480: #if 0
1.157     noro     5481:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5482: #endif
1.167     noro     5483:        conv_ilist(0,0,g,indp);
1.157     noro     5484:     return g;
1.69      noro     5485: }
1.74      noro     5486:
1.167     noro     5487: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5488: {
1.157     noro     5489:     int i,nh,stat,index;
                   5490:     NODE r,g;
                   5491:     ND_pairs d,l,l0,t;
                   5492:     ND spol,red;
                   5493:     NDV nf,redv,nfqv,nfv;
                   5494:     NM s0,s;
                   5495:     NODE rp0,srp0,nflist;
                   5496:     int nsp,nred,col,rank,len,k,j,a;
                   5497:     UINT c;
                   5498:     UINT **spmat;
                   5499:     UINT *s0vect,*svect,*p,*v;
                   5500:     int *colstat;
                   5501:     IndArray *imat;
                   5502:     int *rhead;
                   5503:     int spcol,sprow;
                   5504:     int sugar;
                   5505:     PGeoBucket bucket;
                   5506:     struct oEGT eg0,eg1,eg_f4;
                   5507:
                   5508:     g = 0; d = 0;
                   5509:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5510:         d = update_pairs(d,g,i,0);
1.157     noro     5511:         g = update_base(g,i);
                   5512:     }
                   5513:     while ( d ) {
                   5514:         get_eg(&eg0);
                   5515:         l = nd_minsugarp(d,&d);
                   5516:         sugar = SG(l);
                   5517:         bucket = create_pbucket();
                   5518:         stat = nd_sp_f4(m,0,l,bucket);
                   5519:         if ( !stat ) {
                   5520:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5521:             NEXT(t) = d; d = l;
                   5522:             d = nd_reconstruct(1,d);
                   5523:             continue;
                   5524:         }
                   5525:         if ( bucket->m < 0 ) continue;
                   5526:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5527:         if ( !col ) {
                   5528:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5529:             NEXT(t) = d; d = l;
                   5530:             d = nd_reconstruct(1,d);
                   5531:             continue;
                   5532:         }
                   5533:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5534:         if ( DP_Print )
                   5535:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5536:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5537:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5538:         if ( !l0 ) continue;
                   5539:         l = l0;
                   5540:
                   5541:         /* over Q */
                   5542:         bucket = create_pbucket();
                   5543:         stat = nd_sp_f4(0,1,l,bucket);
                   5544:         if ( !stat ) {
                   5545:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5546:             NEXT(t) = d; d = l;
                   5547:             d = nd_reconstruct(1,d);
                   5548:             continue;
                   5549:         }
                   5550:         if ( bucket->m < 0 ) continue;
                   5551:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5552:         if ( !col ) {
                   5553:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5554:             NEXT(t) = d; d = l;
                   5555:             d = nd_reconstruct(1,d);
                   5556:             continue;
                   5557:         }
                   5558:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   5559:         /* adding new bases */
                   5560:         for ( r = nflist; r; r = NEXT(r) ) {
                   5561:             nfqv = (NDV)BDY(r);
                   5562:             ndv_removecont(0,nfqv);
                   5563:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   5564:             if ( nd_nalg ) {
                   5565:                 ND nf1;
                   5566:
                   5567:                 nf1 = ndvtond(m,nfqv);
                   5568:                 nd_monic(0,&nf1);
                   5569:                 nd_removecont(0,nf1);
                   5570:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   5571:             }
                   5572:             nfv = ndv_dup(0,nfqv);
                   5573:             ndv_mod(m,nfv);
                   5574:             ndv_removecont(m,nfv);
                   5575:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     5576:             d = update_pairs(d,g,nh,0);
1.157     noro     5577:             g = update_base(g,nh);
                   5578:         }
                   5579:     }
1.133     noro     5580: #if 0
1.157     noro     5581:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     5582: #endif
1.167     noro     5583:        conv_ilist(0,1,g,indp);
1.157     noro     5584:     return g;
1.133     noro     5585: }
                   5586:
                   5587: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     5588: {
1.157     noro     5589:     IndArray *imat;
                   5590:     int nsp,nred,i;
                   5591:     int *rhead;
                   5592:     NODE r0,rp;
                   5593:     ND_pairs sp;
                   5594:     NM_ind_pair *rvect;
                   5595:
                   5596:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5597:     nred = length(rp0);
                   5598:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   5599:     rhead = (int *)ALLOCA(col*sizeof(int));
                   5600:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5601:
                   5602:     /* construction of index arrays */
                   5603:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   5604:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   5605:         rvect[i] = (NM_ind_pair)BDY(rp);
                   5606:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   5607:         rhead[imat[i]->head] = 1;
                   5608:     }
                   5609:     if ( m )
                   5610:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   5611:     else
                   5612:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   5613:     return r0;
1.106     noro     5614: }
1.74      noro     5615:
1.106     noro     5616: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     5617:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     5618: {
1.157     noro     5619:     int spcol,sprow,a;
                   5620:     int i,j,k,l,rank;
                   5621:     NODE r0,r;
                   5622:     ND_pairs sp;
                   5623:     ND spol;
                   5624:     int **spmat;
                   5625:     UINT *svect,*v;
                   5626:     int *colstat;
                   5627:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5628:     int maxrs;
                   5629:     int *spsugar;
                   5630:     ND_pairs *spactive;
                   5631:
                   5632:     spcol = col-nred;
                   5633:     get_eg(&eg0);
                   5634:     /* elimination (1st step) */
                   5635:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   5636:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   5637:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   5638:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   5639:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5640:         nd_sp(m,0,sp,&spol);
                   5641:         if ( !spol ) continue;
                   5642:         nd_to_vect(m,s0vect,col,spol,svect);
                   5643:         if ( m == -1 )
                   5644:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   5645:         else
                   5646:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   5647:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5648:         if ( i < col ) {
                   5649:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   5650:             for ( j = k = 0; j < col; j++ )
                   5651:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5652:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5653:             if ( nz )
                   5654:             spactive[sprow] = sp;
                   5655:             sprow++;
                   5656:         }
                   5657:         nd_free(spol);
                   5658:     }
                   5659:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5660:     if ( DP_Print ) {
                   5661:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5662:         fflush(asir_out);
                   5663:     }
                   5664:     /* free index arrays */
                   5665:     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   5666:
                   5667:     /* elimination (2nd step) */
                   5668:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5669:     if ( m == -1 )
                   5670:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   5671:     else
                   5672:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   5673:     r0 = 0;
                   5674:     for ( i = 0; i < rank; i++ ) {
                   5675:         NEXTNODE(r0,r); BDY(r) =
                   5676:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   5677:         SG((NDV)BDY(r)) = spsugar[i];
                   5678:         GC_free(spmat[i]);
                   5679:     }
                   5680:     if ( r0 ) NEXT(r) = 0;
                   5681:
                   5682:     for ( ; i < sprow; i++ ) GC_free(spmat[i]);
                   5683:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5684:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5685:     if ( DP_Print ) {
                   5686:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5687:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5688:             nsp,nred,sprow,spcol,rank);
                   5689:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5690:     }
                   5691:     if ( nz ) {
                   5692:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   5693:         if ( rank > 0 ) {
                   5694:             NEXT(spactive[rank-1]) = 0;
                   5695:             *nz = spactive[0];
                   5696:         } else
                   5697:             *nz = 0;
                   5698:     }
                   5699:     return r0;
1.74      noro     5700: }
                   5701:
1.133     noro     5702: #if 1
                   5703: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     5704:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5705: {
1.157     noro     5706:     int spcol,sprow,a;
                   5707:     int i,j,k,l,rank;
                   5708:     NODE r0,r;
                   5709:     ND_pairs sp;
                   5710:     ND spol;
                   5711:     Q **spmat;
                   5712:     Q *svect,*v;
                   5713:     int *colstat;
                   5714:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   5715:     int maxrs;
                   5716:     int *spsugar;
                   5717:     pointer *w;
                   5718:
                   5719:     spcol = col-nred;
                   5720:     get_eg(&eg0);
                   5721:     /* elimination (1st step) */
                   5722:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   5723:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   5724:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   5725:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5726:         nd_sp(0,trace,sp,&spol);
                   5727:         if ( !spol ) continue;
                   5728:         nd_to_vect_q(s0vect,col,spol,svect);
                   5729:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   5730:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5731:         if ( i < col ) {
                   5732:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   5733:             for ( j = k = 0; j < col; j++ )
                   5734:                 if ( !rhead[j] ) v[k++] = svect[j];
                   5735:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   5736:             sprow++;
                   5737:         }
                   5738: /*        nd_free(spol); */
                   5739:     }
                   5740:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   5741:     if ( DP_Print ) {
                   5742:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   5743:         fflush(asir_out);
                   5744:     }
                   5745:     /* free index arrays */
                   5746: /*    for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
                   5747:
                   5748:     /* elimination (2nd step) */
                   5749:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5750:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   5751:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   5752:     for ( i = 0; i < rank; i++ ) {
                   5753:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   5754:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   5755: /*        GC_free(spmat[i]); */
                   5756:     }
1.138     noro     5757: #if 0
1.157     noro     5758:     qsort(w,rank,sizeof(NDV),
                   5759:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     5760: #endif
1.157     noro     5761:     r0 = 0;
                   5762:     for ( i = 0; i < rank; i++ ) {
                   5763:         NEXTNODE(r0,r); BDY(r) = w[i];
                   5764:     }
                   5765:     if ( r0 ) NEXT(r) = 0;
                   5766:
                   5767: /*    for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
                   5768:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   5769:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   5770:     if ( DP_Print ) {
                   5771:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   5772:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   5773:             nsp,nred,sprow,spcol,rank);
                   5774:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   5775:     }
                   5776:     return r0;
1.107     noro     5777: }
1.129     noro     5778: #else
                   5779: void printm(Q **mat,int row,int col)
                   5780: {
1.157     noro     5781:     int i,j;
                   5782:     printf("[");
                   5783:     for ( i = 0; i < row; i++ ) {
                   5784:         for ( j = 0; j < col; j++ ) {
                   5785:             printexpr(CO,mat[i][j]); printf(" ");
                   5786:         }
                   5787:         printf("]\n");
                   5788:     }
1.129     noro     5789: }
                   5790:
                   5791: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   5792:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5793: {
1.157     noro     5794:     int row,a;
                   5795:     int i,j,rank;
                   5796:     NODE r0,r;
                   5797:     ND_pairs sp;
                   5798:     ND spol;
                   5799:     Q **mat;
                   5800:     int *colstat;
                   5801:     int *sugar;
                   5802:
                   5803:     row = nsp+nred;
                   5804:     /* make the matrix */
                   5805:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   5806:     sugar = (int *)ALLOCA(row*sizeof(int));
                   5807:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5808:         nd_sp(0,0,sp,&spol);
                   5809:         if ( !spol ) continue;
                   5810:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   5811:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   5812:         sugar[row] = SG(spol);
                   5813:         row++;
                   5814:     }
                   5815:     for ( i = 0; i < nred; i++, row++ ) {
                   5816:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   5817:         sugar[row] = rvect[i]->sugar;
                   5818:     }
                   5819:     /* elimination */
                   5820:     colstat = (int *)ALLOCA(col*sizeof(int));
                   5821:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   5822:     r0 = 0;
                   5823:     for ( i = 0; i < rank; i++ ) {
                   5824:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   5825:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   5826:         if ( rhead[j] ) continue;
                   5827:         NEXTNODE(r0,r); BDY(r) =
                   5828:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   5829:         SG((NDV)BDY(r)) = sugar[i];
                   5830:     }
                   5831:     if ( r0 ) NEXT(r) = 0;
                   5832:     printf("\n");
                   5833:     return r0;
1.129     noro     5834: }
                   5835: #endif
1.107     noro     5836:
1.74      noro     5837: FILE *nd_write,*nd_read;
                   5838:
                   5839: void nd_send_int(int a) {
1.157     noro     5840:     write_int(nd_write,&a);
1.74      noro     5841: }
                   5842:
                   5843: void nd_send_intarray(int *p,int len) {
1.157     noro     5844:     write_intarray(nd_write,p,len);
1.74      noro     5845: }
                   5846:
                   5847: int nd_recv_int() {
1.157     noro     5848:     int a;
1.74      noro     5849:
1.157     noro     5850:     read_int(nd_read,&a);
                   5851:     return a;
1.74      noro     5852: }
                   5853:
                   5854: void nd_recv_intarray(int *p,int len) {
1.157     noro     5855:     read_intarray(nd_read,p,len);
1.74      noro     5856: }
                   5857:
                   5858: void nd_send_ndv(NDV p) {
1.157     noro     5859:     int len,i;
                   5860:     NMV m;
1.74      noro     5861:
1.157     noro     5862:     if ( !p ) nd_send_int(0);
                   5863:     else {
                   5864:         len = LEN(p);
                   5865:         nd_send_int(len);
                   5866:         m = BDY(p);
                   5867:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5868:             nd_send_int(CM(m));
                   5869:             nd_send_intarray(DL(m),nd_wpd);
                   5870:         }
                   5871:     }
1.74      noro     5872: }
                   5873:
                   5874: void nd_send_nd(ND p) {
1.157     noro     5875:     int len,i;
                   5876:     NM m;
1.74      noro     5877:
1.157     noro     5878:     if ( !p ) nd_send_int(0);
                   5879:     else {
                   5880:         len = LEN(p);
                   5881:         nd_send_int(len);
                   5882:         m = BDY(p);
                   5883:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   5884:             nd_send_int(CM(m));
                   5885:             nd_send_intarray(DL(m),nd_wpd);
                   5886:         }
                   5887:     }
1.74      noro     5888: }
1.65      noro     5889:
1.74      noro     5890: NDV nd_recv_ndv()
                   5891: {
1.157     noro     5892:     int len,i;
                   5893:     NMV m,m0;
                   5894:     NDV r;
                   5895:
                   5896:     len = nd_recv_int();
                   5897:     if ( !len ) return 0;
                   5898:     else {
                   5899:         m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5900: #if 0
1.157     noro     5901:         ndv_alloc += len*nmv_adv;
1.103     noro     5902: #endif
1.157     noro     5903:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5904:             CM(m) = nd_recv_int();
                   5905:             nd_recv_intarray(DL(m),nd_wpd);
                   5906:         }
                   5907:         MKNDV(nd_nvar,m0,len,r);
                   5908:         return r;
                   5909:     }
1.74      noro     5910: }
1.65      noro     5911:
1.74      noro     5912: int ox_exec_f4_red(Q proc)
                   5913: {
1.157     noro     5914:     Obj obj;
                   5915:     STRING fname;
                   5916:     NODE arg;
                   5917:     int s;
                   5918:     extern int ox_need_conv,ox_file_io;
                   5919:
                   5920:     MKSTR(fname,"nd_exec_f4_red");
                   5921:     arg = mknode(2,proc,fname);
                   5922:     Pox_cmo_rpc(arg,&obj);
                   5923:     s = get_ox_server_id(QTOS(proc));
                   5924:     nd_write = iofp[s].out;
                   5925:     nd_read = iofp[s].in;
                   5926:     ox_need_conv = ox_file_io = 0;
                   5927:     return s;
1.74      noro     5928: }
                   5929:
1.133     noro     5930: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     5931: {
1.157     noro     5932:     int nsp,nred;
                   5933:     int i,rank,s;
                   5934:     NODE rp,r0,r;
                   5935:     ND_pairs sp;
                   5936:     NM_ind_pair pair;
                   5937:     NMV nmv;
                   5938:     NM nm;
                   5939:     NDV nf;
                   5940:     Obj proc,dmy;
                   5941:
                   5942:     ox_launch_main(0,0,&proc);
                   5943:     s = ox_exec_f4_red((Q)proc);
                   5944:
                   5945:     nd_send_int(m);
                   5946:     nd_send_int(nd_nvar);
                   5947:     nd_send_int(nd_bpe);
                   5948:     nd_send_int(nd_wpd);
                   5949:     nd_send_int(nmv_adv);
                   5950:
                   5951:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   5952:
                   5953:     nd_send_int(nd_psn);
                   5954:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   5955:
                   5956:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5957:     nd_send_int(nsp);
                   5958:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   5959:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   5960:     }
                   5961:
                   5962:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   5963:
                   5964:     nred = length(rp0); nd_send_int(nred);
                   5965:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   5966:         pair = (NM_ind_pair)BDY(rp);
                   5967:         nd_send_int(pair->index);
                   5968:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   5969:     }
                   5970:     fflush(nd_write);
                   5971:     rank = nd_recv_int();
                   5972:     fprintf(asir_out,"rank=%d\n",rank);
                   5973:     r0 = 0;
                   5974:     for ( i = 0; i < rank; i++ ) {
                   5975:         nf = nd_recv_ndv();
                   5976:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   5977:     }
                   5978:     Pox_shutdown(mknode(1,proc),&dmy);
                   5979:     return r0;
1.74      noro     5980: }
                   5981:
                   5982: /* server side */
                   5983:
                   5984: void nd_exec_f4_red_dist()
                   5985: {
1.157     noro     5986:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   5987:     NM_ind_pair *rp0;
                   5988:     NDV nf;
                   5989:     UINT *s0vect;
                   5990:     IndArray *imat;
                   5991:     int *rhead;
                   5992:     int **spmat;
                   5993:     UINT *svect,*v;
                   5994:     ND_pairs *sp0;
                   5995:     int *colstat;
                   5996:     int a,sprow,rank;
                   5997:     struct order_spec *ord;
                   5998:     Obj ordspec;
                   5999:     ND spol;
                   6000:     int maxrs;
                   6001:     int *spsugar;
                   6002:
                   6003:     nd_read = iofp[0].in;
                   6004:     nd_write = iofp[0].out;
                   6005:     m = nd_recv_int();
                   6006:     nd_nvar = nd_recv_int();
                   6007:     nd_bpe = nd_recv_int();
                   6008:     nd_wpd = nd_recv_int();
                   6009:     nmv_adv = nd_recv_int();
                   6010:
                   6011:     loadobj(nd_read,&ordspec);
                   6012:     create_order_spec(0,ordspec,&ord);
                   6013:     nd_init_ord(ord);
                   6014:     nd_setup_parameters(nd_nvar,0);
                   6015:
                   6016:     nd_psn = nd_recv_int();
                   6017:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   6018:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   6019:     for ( i = 0; i < nd_psn; i++ ) {
                   6020:         nd_ps[i] = nd_recv_ndv();
                   6021:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   6022:     }
                   6023:
                   6024:     nsp = nd_recv_int();
                   6025:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   6026:     for ( i = 0; i < nsp; i++ ) {
                   6027:         NEWND_pairs(sp0[i]);
                   6028:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6029:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6030:     }
                   6031:
                   6032:     col = nd_recv_int();
                   6033:     s0size = col*nd_wpd;
                   6034:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6035:     nd_recv_intarray(s0vect,s0size);
                   6036:
                   6037:     nred = nd_recv_int();
                   6038:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6039:     for ( i = 0; i < nred; i++ ) {
                   6040:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6041:         rp0[i]->index = nd_recv_int();
                   6042:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6043:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6044:     }
                   6045:
                   6046:     spcol = col-nred;
                   6047:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6048:     rhead = (int *)MALLOC(col*sizeof(int));
                   6049:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6050:
                   6051:     /* construction of index arrays */
                   6052:     for ( i = 0; i < nred; i++ ) {
                   6053:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6054:         rhead[imat[i]->head] = 1;
                   6055:     }
                   6056:
                   6057:     /* elimination (1st step) */
                   6058:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6059:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6060:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6061:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6062:         nd_sp(m,0,sp0[a],&spol);
                   6063:         if ( !spol ) continue;
                   6064:         nd_to_vect(m,s0vect,col,spol,svect);
                   6065:         if ( m == -1 )
                   6066:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6067:         else
                   6068:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6069:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6070:         if ( i < col ) {
                   6071:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6072:             for ( j = k = 0; j < col; j++ )
                   6073:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6074:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6075:             sprow++;
                   6076:         }
                   6077:         nd_free(spol);
                   6078:     }
                   6079:     /* elimination (2nd step) */
                   6080:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6081:     if ( m == -1 )
                   6082:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6083:     else
                   6084:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6085:     nd_send_int(rank);
                   6086:     for ( i = 0; i < rank; i++ ) {
                   6087:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6088:         nd_send_ndv(nf);
                   6089:     }
                   6090:     fflush(nd_write);
1.107     noro     6091: }
                   6092:
1.113     noro     6093: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6094: {
1.157     noro     6095:     int mod,i,j,t,c,rank,rank0,inv;
                   6096:     int *ci,*ri;
                   6097:     Q dn;
                   6098:     MAT m,nm;
                   6099:     int **wmat;
                   6100:
                   6101:     /* XXX */
                   6102:     mod = 99999989;
                   6103:     wmat = (int **)ALLOCA(row*sizeof(int *));
                   6104:     for ( i = 0; i < row; i++ ) {
                   6105:         wmat[i] = (int *)ALLOCA(col*sizeof(int));
                   6106:         for ( j = 0; j < col; j++ ) {
                   6107:             if ( mat0[i][j] ) {
                   6108:                 t = rem(NM(mat0[i][j]),mod);
                   6109:                 if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   6110:                 wmat[i][j] = t;
                   6111:             } else
                   6112:                 wmat[i][j] = 0;
                   6113:         }
                   6114:     }
                   6115:     rank0 = nd_gauss_elim_mod(wmat,sugar,0,row,col,mod,colstat);
                   6116:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6117:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6118:     if ( rank != rank0 )
                   6119:         error("afo");
                   6120:     for ( i = 0; i < row; i++ )
                   6121:         for ( j = 0; j < col; j++ )
                   6122:             mat0[i][j] = 0;
                   6123:     c = col-rank;
                   6124:     for ( i = 0; i < rank; i++ ) {
                   6125:         mat0[i][ri[i]] = dn;
                   6126:         for ( j = 0; j < c; j++ )
                   6127:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6128:     }
                   6129:     inv = invm(rem(NM(dn),mod),mod);
                   6130:     if ( SGN(dn) < 0 ) inv = mod-inv;
                   6131:     for ( i = 0; i < row; i++ )
                   6132:         for ( j = 0; j < col; j++ ) {
                   6133:             if ( mat0[i][j] ) {
                   6134:                 t = rem(NM(mat0[i][j]),mod);
                   6135:                 if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   6136:             } else
                   6137:                 t = 0;
                   6138:             c = dmar(t,inv,0,mod);
                   6139:             if ( wmat[i][j] != c )
                   6140:                 error("afo");
                   6141:         }
                   6142:     return rank;
1.76      noro     6143: }
                   6144:
1.133     noro     6145: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6146: {
1.157     noro     6147:     int i,j,k,l,inv,a,rank,s;
                   6148:     unsigned int *t,*pivot,*pk;
                   6149:     unsigned int **mat;
                   6150:     ND_pairs pair;
                   6151:
                   6152:     mat = (unsigned int **)mat0;
                   6153:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6154:         for ( i = rank; i < row; i++ )
                   6155:             mat[i][j] %= md;
                   6156:         for ( i = rank; i < row; i++ )
                   6157:             if ( mat[i][j] )
                   6158:                 break;
                   6159:         if ( i == row ) {
                   6160:             colstat[j] = 0;
                   6161:             continue;
                   6162:         } else
                   6163:             colstat[j] = 1;
                   6164:         if ( i != rank ) {
                   6165:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6166:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6167:             if ( spactive ) {
                   6168:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6169:                 spactive[rank] = pair;
                   6170:             }
                   6171:         }
                   6172:         pivot = mat[rank];
                   6173:         s = sugar[rank];
                   6174:         inv = invm(pivot[j],md);
                   6175:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6176:             if ( *pk ) {
                   6177:                 if ( *pk >= (unsigned int)md )
                   6178:                     *pk %= md;
                   6179:                 DMAR(*pk,inv,0,md,*pk)
                   6180:             }
                   6181:         for ( i = rank+1; i < row; i++ ) {
                   6182:             t = mat[i];
                   6183:             if ( a = t[j] ) {
                   6184:                 sugar[i] = MAX(sugar[i],s);
                   6185:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6186:             }
                   6187:         }
                   6188:         rank++;
                   6189:     }
                   6190:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6191:         if ( colstat[j] ) {
                   6192:             pivot = mat[l];
                   6193:             s = sugar[l];
                   6194:             for ( i = 0; i < l; i++ ) {
                   6195:                 t = mat[i];
                   6196:                 t[j] %= md;
                   6197:                 if ( a = t[j] ) {
                   6198:                     sugar[i] = MAX(sugar[i],s);
                   6199:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6200:                 }
                   6201:             }
                   6202:             l--;
                   6203:         }
                   6204:     for ( j = 0, l = 0; l < rank; j++ )
                   6205:         if ( colstat[j] ) {
                   6206:             t = mat[l];
                   6207:             for ( k = j; k < col; k++ )
                   6208:                 if ( t[k] >= (unsigned int)md )
                   6209:                     t[k] %= md;
                   6210:             l++;
                   6211:         }
                   6212:     return rank;
1.76      noro     6213: }
                   6214:
                   6215: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6216: {
1.157     noro     6217:     int i,j,k,l,inv,a,rank,s;
                   6218:     unsigned int *t,*pivot,*pk;
                   6219:     unsigned int **mat;
                   6220:
                   6221:     mat = (unsigned int **)mat0;
                   6222:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6223:         for ( i = rank; i < row; i++ )
                   6224:             if ( mat[i][j] )
                   6225:                 break;
                   6226:         if ( i == row ) {
                   6227:             colstat[j] = 0;
                   6228:             continue;
                   6229:         } else
                   6230:             colstat[j] = 1;
                   6231:         if ( i != rank ) {
                   6232:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6233:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6234:         }
                   6235:         pivot = mat[rank];
                   6236:         s = sugar[rank];
                   6237:         inv = _invsf(pivot[j]);
                   6238:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6239:             if ( *pk )
                   6240:                 *pk = _mulsf(*pk,inv);
                   6241:         for ( i = rank+1; i < row; i++ ) {
                   6242:             t = mat[i];
                   6243:             if ( a = t[j] ) {
                   6244:                 sugar[i] = MAX(sugar[i],s);
                   6245:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6246:             }
                   6247:         }
                   6248:         rank++;
                   6249:     }
                   6250:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6251:         if ( colstat[j] ) {
                   6252:             pivot = mat[l];
                   6253:             s = sugar[l];
                   6254:             for ( i = 0; i < l; i++ ) {
                   6255:                 t = mat[i];
                   6256:                 if ( a = t[j] ) {
                   6257:                     sugar[i] = MAX(sugar[i],s);
                   6258:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6259:                 }
                   6260:             }
                   6261:             l--;
                   6262:         }
                   6263:     return rank;
1.77      noro     6264: }
                   6265:
                   6266: int ndv_ishomo(NDV p)
                   6267: {
1.157     noro     6268:     NMV m;
                   6269:     int len,h;
1.77      noro     6270:
1.157     noro     6271:     if ( !p ) return 1;
                   6272:     len = LEN(p);
                   6273:     m = BDY(p);
                   6274:     h = TD(DL(m));
                   6275:     NMV_ADV(m);
                   6276:     for ( len--; len; len--, NMV_ADV(m) )
                   6277:         if ( TD(DL(m)) != h ) return 0;
                   6278:     return 1;
1.77      noro     6279: }
                   6280:
                   6281: void ndv_save(NDV p,int index)
                   6282: {
1.157     noro     6283:     FILE *s;
                   6284:     char name[BUFSIZ];
                   6285:     short id;
                   6286:     int nv,sugar,len,n,i,td,e,j;
                   6287:     NMV m;
                   6288:     unsigned int *dl;
                   6289:     int mpos;
                   6290:
                   6291:     sprintf(name,"%s/%d",Demand,index);
                   6292:     s = fopen(name,"w");
                   6293:     savevl(s,0);
                   6294:     if ( !p ) {
                   6295:         saveobj(s,0);
                   6296:         return;
                   6297:     }
                   6298:     id = O_DP;
                   6299:     nv = NV(p);
                   6300:     sugar = SG(p);
                   6301:     len = LEN(p);
                   6302:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6303:     write_int(s,&len);
                   6304:
                   6305:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6306:         saveobj(s,(Obj)CQ(m));
                   6307:         dl = DL(m);
                   6308:         td = TD(dl);
                   6309:         write_int(s,&td);
                   6310:         for ( j = 0; j < nv; j++ ) {
                   6311:             e = GET_EXP(dl,j);
                   6312:             write_int(s,&e);
                   6313:         }
                   6314:         if ( nd_module ) {
                   6315:             mpos = MPOS(dl); write_int(s,&mpos);
                   6316:         }
                   6317:     }
                   6318:     fclose(s);
1.77      noro     6319: }
                   6320:
                   6321: NDV ndv_load(int index)
                   6322: {
1.157     noro     6323:     FILE *s;
                   6324:     char name[BUFSIZ];
                   6325:     short id;
                   6326:     int nv,sugar,len,n,i,td,e,j;
                   6327:     NDV d;
                   6328:     NMV m0,m;
                   6329:     unsigned int *dl;
                   6330:     Obj obj;
                   6331:     int mpos;
                   6332:
                   6333:     sprintf(name,"%s/%d",Demand,index);
                   6334:     s = fopen(name,"r");
                   6335:     if ( !s ) return 0;
                   6336:
                   6337:     skipvl(s);
                   6338:     read_short(s,&id);
                   6339:     if ( !id ) return 0;
                   6340:     read_int(s,&nv);
                   6341:     read_int(s,&sugar);
                   6342:     read_int(s,&len);
                   6343:
                   6344:     m0 = m = MALLOC(len*nmv_adv);
                   6345:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6346:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6347:         dl = DL(m);
                   6348:         ndl_zero(dl);
                   6349:         read_int(s,&td); TD(dl) = td;
                   6350:         for ( j = 0; j < nv; j++ ) {
                   6351:             read_int(s,&e);
                   6352:             PUT_EXP(dl,j,e);
                   6353:         }
                   6354:         if ( nd_module ) {
                   6355:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6356:         }
                   6357:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6358:     }
                   6359:     fclose(s);
                   6360:     MKNDV(nv,m0,len,d);
                   6361:     SG(d) = sugar;
                   6362:     return d;
1.99      noro     6363: }
                   6364:
1.102     noro     6365: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6366: {
1.157     noro     6367:     VL fv,tv;
                   6368:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6369:     pointer **m;
                   6370:     Q mone;
                   6371:     P **w;
                   6372:     P mp,r;
                   6373:     NDV **dm;
                   6374:     NDV *t,*mi,*mj;
                   6375:     NDV d,s,mij,mjj;
                   6376:     ND u;
                   6377:     NMV nmv;
                   6378:     UINT *bound;
                   6379:     PGeoBucket bucket;
                   6380:     struct order_spec *ord;
                   6381:     Q dq,dt,ds;
                   6382:     N gn,qn,dn0,nm,dn;
                   6383:
                   6384:     create_order_spec(0,0,&ord);
                   6385:     nd_init_ord(ord);
                   6386:     get_vars((Obj)f,&fv);
                   6387:     if ( f->row != f->col )
                   6388:         error("nd_det : non-square matrix");
                   6389:     n = f->row;
                   6390:     m = f->body;
                   6391:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6392:
                   6393:     if ( !nvar ) {
                   6394:         if ( !mod )
                   6395:             detp(CO,(P **)m,n,rp);
                   6396:         else {
                   6397:             w = (P **)almat_pointer(n,n);
                   6398:             for ( i = 0; i < n; i++ )
                   6399:                 for ( j = 0; j < n; j++ )
                   6400:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6401:             detmp(CO,mod,w,n,&mp);
                   6402:             mptop(mp,rp);
                   6403:         }
                   6404:         return;
                   6405:     }
                   6406:
                   6407:     if ( !mod ) {
                   6408:         w = (P **)almat_pointer(n,n);
                   6409:         dq = ONE;
                   6410:         for ( i = 0; i < n; i++ ) {
                   6411:             dn0 = ONEN;
                   6412:             for ( j = 0; j < n; j++ ) {
                   6413:                 if ( !m[i][j] ) continue;
                   6414:                 lgp(m[i][j],&nm,&dn);
                   6415:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   6416:             }
                   6417:             if ( !UNIN(dn0) ) {
                   6418:                 NTOQ(dn0,1,ds);
                   6419:                 for ( j = 0; j < n; j++ )
                   6420:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   6421:                 mulq(dq,ds,&dt); dq = dt;
                   6422:             } else
                   6423:                 for ( j = 0; j < n; j++ )
                   6424:                     w[i][j] = (P)m[i][j];
                   6425:         }
                   6426:         m = (pointer **)w;
                   6427:     }
                   6428:
                   6429:     for ( i = 0, max = 0; i < n; i++ )
                   6430:         for ( j = 0; j < n; j++ )
                   6431:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   6432:                 e = getdeg(tv->v,(P)m[i][j]);
                   6433:                 max = MAX(e,max);
                   6434:             }
                   6435:     nd_setup_parameters(nvar,max);
                   6436:     dm = (NDV **)almat_pointer(n,n);
                   6437:     for ( i = 0, max = 0; i < n; i++ )
                   6438:         for ( j = 0; j < n; j++ ) {
                   6439:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   6440:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   6441:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   6442:         }
                   6443:     d = ptondv(CO,fv,(P)ONE);
                   6444:     if ( mod ) ndv_mod(mod,d);
                   6445:     chsgnq(ONE,&mone);
                   6446:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   6447:         if ( DP_Print ) fprintf(stderr,".",j);
                   6448:         for ( i = j; i < n && !dm[i][j]; i++ );
                   6449:         if ( i == n ) {
                   6450:             *rp = 0;
                   6451:             return;
                   6452:         }
                   6453:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   6454:         for ( k = j; k < n; k++ )
                   6455:             for ( l = j; l < n; l++ )
                   6456:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   6457:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   6458:                 }
                   6459:         if ( k0 != j ) {
                   6460:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   6461:             sgn = -sgn;
                   6462:         }
                   6463:         if ( l0 != j ) {
                   6464:             for ( k = j; k < n; k++ ) {
                   6465:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   6466:             }
                   6467:             sgn = -sgn;
                   6468:         }
                   6469:         bound = nd_det_compute_bound(dm,n,j);
                   6470:         for ( k = 0; k < nd_nvar; k++ )
                   6471:             if ( bound[k]*2 > nd_mask0 ) break;
                   6472:         if ( k < nd_nvar )
                   6473:             nd_det_reconstruct(dm,n,j,d);
                   6474:
                   6475:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   6476: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   6477:             mi = dm[i]; mij = mi[j];
                   6478:             if ( mod )
                   6479:                 ndv_mul_c(mod,mij,mod-1);
                   6480:             else
                   6481:                 ndv_mul_c_q(mij,mone);
                   6482:             for ( k = j+1; k < n; k++ ) {
                   6483: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   6484:                 bucket = create_pbucket();
                   6485:                 if ( mi[k] ) {
                   6486:                     nmv = BDY(mjj); len = LEN(mjj);
                   6487:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6488:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   6489:                         add_pbucket(mod,bucket,u);
                   6490:                     }
                   6491:                 }
                   6492:                 if ( mj[k] && mij ) {
                   6493:                     nmv = BDY(mij); len = LEN(mij);
                   6494:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   6495:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   6496:                         add_pbucket(mod,bucket,u);
                   6497:                     }
                   6498:                 }
                   6499:                 u = nd_quo(mod,bucket,d);
                   6500:                 mi[k] = ndtondv(mod,u);
                   6501:             }
                   6502: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   6503:         }
                   6504:         d = mjj;
                   6505:     }
                   6506:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   6507:     if ( sgn < 0 )
                   6508:         if ( mod )
                   6509:             ndv_mul_c(mod,d,mod-1);
                   6510:         else
                   6511:             ndv_mul_c_q(d,mone);
                   6512:     r = ndvtop(mod,CO,fv,d);
                   6513:     if ( !mod && !UNIQ(dq) )
                   6514:         divsp(CO,r,(P)dq,rp);
                   6515:     else
                   6516:         *rp = r;
1.99      noro     6517: }
                   6518:
1.102     noro     6519: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     6520: {
1.157     noro     6521:     NM mr,mr0;
                   6522:     NM tnm;
                   6523:     NMV m;
                   6524:     UINT *d0,*dt,*dm;
                   6525:     int c,n,td,i,c1,c2,len;
                   6526:     Q q;
                   6527:     ND r;
                   6528:
                   6529:     if ( !p ) return 0;
                   6530:     else {
                   6531:         n = NV(p); m = BDY(p); len = LEN(p);
                   6532:         d0 = DL(m0);
                   6533:         td = TD(d);
                   6534:         mr0 = 0;
                   6535:         NEWNM(tnm);
                   6536:         if ( mod ) {
                   6537:             c = CM(m0);
                   6538:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6539:                 ndl_add(DL(m),d0,DL(tnm));
                   6540:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6541:                     NEXTNM(mr0,mr);
                   6542:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   6543:                     ndl_copy(DL(tnm),DL(mr));
                   6544:                 }
                   6545:             }
                   6546:         } else {
                   6547:             q = CQ(m0);
                   6548:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6549:                 ndl_add(DL(m),d0,DL(tnm));
                   6550:                 if ( ndl_reducible(DL(tnm),d) ) {
                   6551:                     NEXTNM(mr0,mr);
                   6552:                     mulq(CQ(m),q,&CQ(mr));
                   6553:                     ndl_copy(DL(tnm),DL(mr));
                   6554:                 }
                   6555:             }
                   6556:         }
                   6557:         if ( !mr0 )
                   6558:             return 0;
                   6559:         else {
                   6560:             NEXT(mr) = 0;
                   6561:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6562:             MKND(NV(p),mr0,len,r);
                   6563:             SG(r) = SG(p) + TD(d0);
                   6564:             return r;
                   6565:         }
                   6566:     }
1.114     noro     6567: }
                   6568:
                   6569: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   6570: {
1.157     noro     6571:     int i,obpe,oadv,h,k,l;
                   6572:     static NM prev_nm_free_list;
                   6573:     EPOS oepos;
                   6574:
                   6575:     obpe = nd_bpe;
                   6576:     oadv = nmv_adv;
                   6577:     oepos = nd_epos;
                   6578:     if ( obpe < 2 ) nd_bpe = 2;
                   6579:     else if ( obpe < 3 ) nd_bpe = 3;
                   6580:     else if ( obpe < 4 ) nd_bpe = 4;
                   6581:     else if ( obpe < 5 ) nd_bpe = 5;
                   6582:     else if ( obpe < 6 ) nd_bpe = 6;
                   6583:     else if ( obpe < 8 ) nd_bpe = 8;
                   6584:     else if ( obpe < 10 ) nd_bpe = 10;
                   6585:     else if ( obpe < 16 ) nd_bpe = 16;
                   6586:     else if ( obpe < 32 ) nd_bpe = 32;
                   6587:     else error("nd_det_reconstruct : exponent too large");
                   6588:
                   6589:     nd_setup_parameters(nd_nvar,0);
                   6590:     prev_nm_free_list = _nm_free_list;
                   6591:     _nm_free_list = 0;
                   6592:     for ( k = j; k < n; k++ )
                   6593:         for (l = j; l < n; l++ )
                   6594:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   6595:     ndv_realloc(d,obpe,oadv,oepos);
                   6596:     prev_nm_free_list = 0;
1.114     noro     6597: #if 0
1.157     noro     6598:     GC_gcollect();
1.114     noro     6599: #endif
                   6600: }
                   6601:
1.153     noro     6602: /* returns a UINT array containing degree bounds */
                   6603:
1.114     noro     6604: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   6605: {
1.157     noro     6606:     UINT *d0,*d1,*d,*t,*r;
                   6607:     int k,l,i;
1.114     noro     6608:
1.157     noro     6609:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   6610:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   6611:     for ( k = j; k < n; k++ )
                   6612:         for ( l = j; l < n; l++ )
                   6613:             if ( dm[k][l] ) {
                   6614:                 d = ndv_compute_bound(dm[k][l]);
                   6615:                 for ( i = 0; i < nd_nvar; i++ )
                   6616:                     d0[i] = MAX(d0[i],d[i]);
                   6617:             }
                   6618:     return d0;
1.117     noro     6619: }
                   6620:
                   6621: DL nd_separate_d(UINT *d,UINT *trans)
                   6622: {
1.157     noro     6623:     int n,td,i,e,j;
                   6624:     DL a;
1.117     noro     6625:
1.157     noro     6626:     ndl_zero(trans);
                   6627:     td = 0;
                   6628:     for ( i = 0; i < nd_ntrans; i++ ) {
                   6629:         e = GET_EXP(d,i);
                   6630:         PUT_EXP(trans,i,e);
                   6631:         td += MUL_WEIGHT(e,i);
                   6632:     }
                   6633:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   6634:         /* homogenized */
                   6635:         i = nd_nvar-1;
                   6636:         e = GET_EXP(d,i);
                   6637:         PUT_EXP(trans,i,e);
                   6638:         td += MUL_WEIGHT(e,i);
                   6639:     }
                   6640:     TD(trans) = td;
                   6641:     if ( nd_blockmask) ndl_weight_mask(trans);
                   6642:     NEWDL(a,nd_nalg);
                   6643:     td = 0;
                   6644:     for ( i = 0; i < nd_nalg; i++ ) {
                   6645:         j = nd_ntrans+i;
                   6646:         e = GET_EXP(d,j);
                   6647:         a->d[i] = e;
                   6648:         td += e;
                   6649:     }
                   6650:     a->td = td;
                   6651:     return a;
1.117     noro     6652: }
                   6653:
1.118     noro     6654: int nd_monic(int mod,ND *p)
1.117     noro     6655: {
1.157     noro     6656:     UINT *trans,*t;
                   6657:     DL alg;
                   6658:     MP mp0,mp;
                   6659:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   6660:     ND r;
                   6661:     DL dl;
                   6662:     DP nm;
                   6663:     NDV ndv;
                   6664:     DAlg inv,cd;
                   6665:     ND s,c;
                   6666:     Q l,mul;
                   6667:     N ln;
                   6668:     int n,ntrans,i,e,td,is_lc,len;
                   6669:     NumberField nf;
                   6670:     struct oEGT eg0,eg1;
                   6671:
                   6672:     if ( !(nf = get_numberfield()) )
                   6673:         error("nd_monic : current_numberfield is not set");
                   6674:
                   6675:     /* Q coef -> DAlg coef */
                   6676:     NEWNM(ma0); ma = ma0;
                   6677:     m = BDY(*p);
                   6678:     is_lc = 1;
                   6679:     while ( 1 ) {
                   6680:         NEWMP(mp0); mp = mp0;
                   6681:         mp->c = (P)CQ(m);
                   6682:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   6683:         NEWNM(mb);
                   6684:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6685:             alg = nd_separate_d(DL(m),DL(mb));
                   6686:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   6687:                 break;
                   6688:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   6689:         }
                   6690:         NEXT(mp) = 0;
                   6691:         MKDP(nd_nalg,mp0,nm);
                   6692:         MKDAlg(nm,ONE,cd);
                   6693:         if ( is_lc == 1 ) {
                   6694:             /* if the lc is a rational number, we have nothing to do */
                   6695:             if ( !mp0->dl->td )
                   6696:                 return 1;
                   6697:
                   6698:             get_eg(&eg0);
                   6699:             invdalg(cd,&inv);
                   6700:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   6701:             /* check the validity of inv */
                   6702:             if ( mod && !rem(NM(inv->dn),mod) )
                   6703:                 return 0;
                   6704:             CA(ma) = nf->one;
                   6705:             is_lc = 0;
                   6706:             ln = ONEN;
                   6707:         } else {
                   6708:             muldalg(cd,inv,&CA(ma));
                   6709:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   6710:         }
                   6711:         if ( m ) {
                   6712:             NEXT(ma) = mb; ma = mb;
                   6713:         } else {
                   6714:             NEXT(ma) = 0;
                   6715:             break;
                   6716:         }
                   6717:     }
                   6718:     /* l = lcm(denoms) */
                   6719:     NTOQ(ln,1,l);
                   6720:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   6721:         divq(l,CA(m)->dn,&mul);
                   6722:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   6723:             NEXTNM(mr0,mr);
                   6724:             mulq((Q)mp->c,mul,&CQ(mr));
                   6725:             dl = mp->dl;
                   6726:             td = TD(DL(m));
                   6727:             ndl_copy(DL(m),DL(mr));
                   6728:             for ( i = 0; i < nd_nalg; i++ ) {
                   6729:                 e = dl->d[i];
                   6730:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   6731:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   6732:             }
1.163     noro     6733:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     6734:             TD(DL(mr)) = td;
                   6735:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   6736:         }
                   6737:     }
                   6738:     NEXT(mr) = 0;
                   6739:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   6740:     MKND(NV(*p),mr0,len,r);
                   6741:     /* XXX */
                   6742:     SG(r) = SG(*p);
                   6743:     nd_free(*p);
                   6744:     *p = r;
                   6745:     return 1;
1.59      noro     6746: }
1.167     noro     6747:
                   6748: NODE reverse_node(NODE n)
                   6749: {
                   6750:     NODE t,t1;
                   6751:
                   6752:     for ( t = 0; n; n = NEXT(n) ) {
                   6753:         MKNODE(t1,BDY(n),t); t = t1;
                   6754:     }
                   6755:     return t;
                   6756: }
                   6757:
                   6758: P ndc_div(int mod,union oNDC a,union oNDC b)
                   6759: {
                   6760:     union oNDC c;
                   6761:     int inv,t;
                   6762:
                   6763:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   6764:     else if ( mod ) {
                   6765:         inv = invm(b.m,mod);
                   6766:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   6767:     } else if ( nd_vc )
                   6768:        divsp(nd_vc,a.p,b.p,&c.p);
                   6769:     else
                   6770:        divq(a.z,b.z,&c.z);
                   6771:     return ndctop(mod,c);
                   6772: }
                   6773:
                   6774: P ndctop(int mod,union oNDC c)
                   6775: {
                   6776:     Q q;
                   6777:     int e;
                   6778:     GFS gfs;
                   6779:
                   6780:     if ( mod == -1 ) {
                   6781:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   6782:     } else if ( mod ) {
                   6783:         STOQ(c.m,q); return (P)q;
                   6784:     } else
                   6785:         return (P)c.p;
                   6786: }
                   6787:
                   6788: /* [0,0,0,cont] = p -> p/cont */
                   6789:
                   6790: void finalize_tracelist(int i,P cont)
                   6791: {
                   6792:         LIST l;
                   6793:         NODE node;
                   6794:      Q iq;
                   6795:
                   6796:         if ( !UNIQ(cont) ) {
                   6797:          node = mknode(4,0,0,0,cont);
                   6798:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   6799:                 nd_tracelist = node;
                   6800:         }
                   6801:      STOQ(i,iq);
                   6802:      nd_tracelist = reverse_node(nd_tracelist);
                   6803:      MKLIST(l,nd_tracelist);
                   6804:      node = mknode(2,iq,l); MKLIST(l,node);
                   6805:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   6806:      nd_alltracelist = node; nd_tracelist = 0;
                   6807: }
                   6808:
                   6809: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   6810: {
                   6811:     int n,i,j;
                   6812:        int *ind;
                   6813:        NODE t;
                   6814:
                   6815:     n = length(g);
                   6816:        ind = (int *)MALLOC(n*sizeof(int));
                   6817:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   6818:                j = (long)BDY(t); ind[i] = j;
                   6819:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   6820:        }
                   6821:        if ( indp ) *indp = ind;
                   6822: }
1.172     noro     6823:
                   6824: void parse_nd_option(NODE opt)
                   6825: {
                   6826:     NODE t,p;
                   6827:     char *key;
                   6828:     Obj value;
                   6829:
1.173     noro     6830:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0;
1.172     noro     6831:     for ( t = opt; t; t = NEXT(t) ) {
                   6832:         p = BDY((LIST)BDY(t));
                   6833:         key = BDY((STRING)BDY(p));
                   6834:         value = (Obj)BDY(NEXT(p));
                   6835:         if ( !strcmp(key,"gentrace") )
                   6836:             nd_gentrace = value?1:0;
                   6837:         else if ( !strcmp(key,"gensyz") )
                   6838:             nd_gensyz = value?1:0;
1.173     noro     6839:         else if ( !strcmp(key,"nora") )
                   6840:             nd_nora = value?1:0;
1.172     noro     6841:     }
                   6842: }

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