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

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

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