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

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

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