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

1.214   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.213 2013/09/27 02:45:17 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.214   ! noro     2041: int check_splist_f4(int m,NODE splist)
        !          2042: {
        !          2043:        UINT *s0vect;
        !          2044:     PGeoBucket bucket;
        !          2045:        NODE p,rp0,t;
        !          2046:        ND_pairs d,r,l,ll;
        !          2047:        int col,stat;
        !          2048:
        !          2049:        for ( d = 0, t = splist; t; t = NEXT(t) ) {
        !          2050:                p = BDY((LIST)BDY(t));
        !          2051:         NEXTND_pairs(d,r);
        !          2052:         r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
        !          2053:         ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
        !          2054:                SG(r) = TD(LCM(r)); /* XXX */
        !          2055:        }
        !          2056:        if ( d ) NEXT(r) = 0;
        !          2057:
        !          2058:     while ( d ) {
        !          2059:         l = nd_minsugarp(d,&d);
        !          2060:         bucket = create_pbucket();
        !          2061:         stat = nd_sp_f4(m,0,l,bucket);
        !          2062:         if ( !stat ) {
        !          2063:             for ( ll = l; NEXT(ll); ll = NEXT(ll) );
        !          2064:             NEXT(ll) = d; d = l;
        !          2065:             d = nd_reconstruct(0,d);
        !          2066:             continue;
        !          2067:         }
        !          2068:         if ( bucket->m < 0 ) continue;
        !          2069:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
        !          2070:         if ( !col ) {
        !          2071:             for ( ll = l; NEXT(ll); ll = NEXT(ll) );
        !          2072:             NEXT(ll) = d; d = l;
        !          2073:             d = nd_reconstruct(0,d);
        !          2074:             continue;
        !          2075:         }
        !          2076:         if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
        !          2077:     }
        !          2078:     return 1;
        !          2079: }
        !          2080:
1.122     noro     2081: int do_diagonalize_trace(int sugar,int m)
1.91      noro     2082: {
1.157     noro     2083:     int i,nh,stat;
                   2084:     NODE r,g,t;
                   2085:     ND h,nf,nfq,s,head;
                   2086:     NDV nfv,nfqv;
                   2087:     Q q,den,num;
1.167     noro     2088:     union oNDC hc;
                   2089:     NODE node;
                   2090:     LIST l;
                   2091:     Q iq;
                   2092:     P cont,cont1;
1.157     noro     2093:
                   2094:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     2095:         if ( nd_gentrace ) {
1.167     noro     2096:             /* Trace = [1,index,1,1] */
                   2097:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2098:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2099:         }
1.157     noro     2100:         /* for nd_ps */
                   2101:         s = ndvtond(m,nd_ps[i]);
                   2102:         s = nd_separate_head(s,&head);
                   2103:         stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2104:         if ( !stat ) return 0;
                   2105:         nf = nd_add(m,head,nf);
                   2106:         ndv_free(nd_ps[i]);
                   2107:         nd_ps[i] = ndtondv(m,nf);
                   2108:         nd_free(nf);
                   2109:
                   2110:         /* for nd_ps_trace */
                   2111:         if ( nd_demand )
                   2112:             nfv = ndv_load(i);
                   2113:         else
                   2114:             nfv = nd_ps_trace[i];
                   2115:         s = ndvtond(0,nfv);
                   2116:         s = nd_separate_head(s,&head);
                   2117:         stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
                   2118:         if ( !stat ) return 0;
                   2119:         ndv_free(nfv);
1.167     noro     2120:         hc = HCU(nf); nd_removecont(0,nf);
                   2121:                cont = ndc_div(0,hc,HCU(nf));
1.172     noro     2122:         if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2123:         nfv = ndtondv(0,nf);
                   2124:         nd_free(nf);
                   2125:         nd_bound[i] = ndv_compute_bound(nfv);
                   2126:         register_hcf(nfv);
                   2127:         if ( nd_demand ) {
                   2128:             ndv_save(nfv,i);
                   2129:             ndv_free(nfv);
                   2130:         } else
                   2131:             nd_ps_trace[i] = nfv;
                   2132:     }
                   2133:     return 1;
1.91      noro     2134: }
                   2135:
1.118     noro     2136: static struct oEGT eg_invdalg;
                   2137: struct oEGT eg_le;
                   2138:
1.147     noro     2139: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2140: {
1.157     noro     2141:     NODE tn;
                   2142:     P p1;
1.147     noro     2143:
1.157     noro     2144:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2145:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2146:     }
                   2147:     *r = p;
1.147     noro     2148: }
                   2149:
1.167     noro     2150: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20      noro     2151: {
1.157     noro     2152:     int i,nh,sugar,stat;
                   2153:     NODE r,g,t;
                   2154:     ND_pairs d;
                   2155:     ND_pairs l;
                   2156:     ND h,nf,nfq,s,head;
                   2157:     NDV nfv,nfqv;
                   2158:     Q q,den,num;
                   2159:     P hc;
1.167     noro     2160:     union oNDC dn,hnfq;
1.157     noro     2161:     struct oEGT eg_monic,egm0,egm1;
                   2162:     int diag_count = 0;
1.167     noro     2163:     P cont;
                   2164:     LIST list;
1.157     noro     2165:
                   2166:     init_eg(&eg_monic);
                   2167:     init_eg(&eg_invdalg);
                   2168:     init_eg(&eg_le);
                   2169:     g = 0; d = 0;
                   2170:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2171:         d = update_pairs(d,g,i,0);
1.157     noro     2172:         g = update_base(g,i);
                   2173:     }
                   2174:     sugar = 0;
                   2175:     while ( d ) {
1.20      noro     2176: again:
1.157     noro     2177:         l = nd_minp(d,&d);
                   2178:         if ( SG(l) != sugar ) {
1.130     noro     2179: #if 1
1.157     noro     2180:             if ( ishomo ) {
                   2181:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2182:                 stat = do_diagonalize_trace(sugar,m);
                   2183:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2184:                 diag_count = 0;
                   2185:                 if ( !stat ) {
                   2186:                     NEXT(l) = d; d = l;
                   2187:                     d = nd_reconstruct(1,d);
                   2188:                     goto again;
                   2189:                 }
                   2190:             }
1.130     noro     2191: #endif
1.157     noro     2192:             sugar = SG(l);
                   2193:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2194:         }
                   2195:         stat = nd_sp(m,0,l,&h);
                   2196:         if ( !stat ) {
                   2197:             NEXT(l) = d; d = l;
                   2198:             d = nd_reconstruct(1,d);
                   2199:             goto again;
                   2200:         }
1.41      noro     2201: #if USE_GEOBUCKET
1.157     noro     2202:         stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     2203: #else
1.157     noro     2204:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2205: #endif
1.157     noro     2206:         if ( !stat ) {
                   2207:             NEXT(l) = d; d = l;
                   2208:             d = nd_reconstruct(1,d);
                   2209:             goto again;
                   2210:         } else if ( nf ) {
                   2211:             if ( nd_demand ) {
                   2212:                 nfqv = ndv_load(nd_psn);
                   2213:                 nfq = ndvtond(0,nfqv);
                   2214:             } else
                   2215:                 nfq = 0;
                   2216:             if ( !nfq ) {
                   2217:                 if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   2218:                     NEXT(l) = d; d = l;
                   2219:                     d = nd_reconstruct(1,d);
                   2220:                     goto again;
                   2221:                 }
                   2222:             }
                   2223:             if ( nfq ) {
                   2224:                 /* m|HC(nfq) => failure */
                   2225:                 if ( nd_vc ) {
                   2226:                     nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
                   2227:                 } else
                   2228:                     q = HCQ(nfq);
                   2229:                 if ( !rem(NM(q),m) ) return 0;
                   2230:
                   2231:                 if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2232:                 hnfq = HCU(nfq);
1.157     noro     2233:                 if ( nd_nalg ) {
                   2234:                     /* m|DN(HC(nf)^(-1)) => failure */
                   2235:                     get_eg(&egm0);
                   2236:                     if ( !nd_monic(m,&nfq) ) return 0;
                   2237:                     get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2238:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2239:                     nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2240:                 } else {
                   2241:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2242:                     nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2243:                 }
1.172     noro     2244:                 if ( nd_gentrace ) {
1.167     noro     2245:                                   cont = ndc_div(0,hnfq,HCU(nfqv));
                   2246:                                   if ( !UNIQ(cont) ) {
1.196     noro     2247:                        t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2248:                        MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2249:                                           nd_tracelist = t;
                   2250:                                   }
                   2251:                 }
1.157     noro     2252:                 nh = ndv_newps(0,nfv,nfqv);
                   2253:                 if ( ishomo && ++diag_count == diag_period ) {
                   2254:                     diag_count = 0;
                   2255:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2256:                     stat = do_diagonalize_trace(sugar,m);
                   2257:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2258:                     if ( !stat ) {
                   2259:                         NEXT(l) = d; d = l;
                   2260:                         d = nd_reconstruct(1,d);
                   2261:                         goto again;
                   2262:                     }
                   2263:                 }
1.168     noro     2264:                 d = update_pairs(d,g,nh,0);
1.157     noro     2265:                 g = update_base(g,nh);
                   2266:             } else {
                   2267:                 if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2268:             }
                   2269:         } else {
                   2270:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2271:         }
                   2272:         FREENDP(l);
                   2273:     }
                   2274:     if ( nd_nalg ) {
                   2275:         print_eg("monic",&eg_monic);
                   2276:         print_eg("invdalg",&eg_invdalg);
                   2277:         print_eg("le",&eg_le);
                   2278:     }
1.167     noro     2279:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     2280:     if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   2281:     return g;
1.20      noro     2282: }
                   2283:
1.23      noro     2284: int ndv_compare(NDV *p1,NDV *p2)
                   2285: {
1.157     noro     2286:     return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2287: }
                   2288:
                   2289: int ndv_compare_rev(NDV *p1,NDV *p2)
                   2290: {
1.157     noro     2291:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2292: }
                   2293:
1.167     noro     2294: int ndvi_compare(NDVI p1,NDVI p2)
                   2295: {
                   2296:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2297: }
                   2298:
                   2299: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   2300: {
                   2301:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2302: }
                   2303:
1.61      noro     2304: NODE ndv_reduceall(int m,NODE f)
1.23      noro     2305: {
1.167     noro     2306:     int i,j,n,stat;
1.157     noro     2307:     ND nf,g,head;
                   2308:     NODE t,a0,a;
                   2309:     union oNDC dn;
                   2310:     Q q,num,den;
1.167     noro     2311:     NODE node;
                   2312:        LIST l;
                   2313:        Q iq,jq;
                   2314:     int *perm;
                   2315:     union oNDC hc;
                   2316:     P cont,cont1;
1.23      noro     2317:
1.173     noro     2318:     if ( nd_nora ) return f;
1.157     noro     2319:     n = length(f);
                   2320:     ndv_setup(m,0,f,0,1);
1.167     noro     2321:        perm = (int *)MALLOC(n*sizeof(int));
1.172     noro     2322:        if ( nd_gentrace ) {
1.167     noro     2323:            for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2324:                    perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2325:        }
1.157     noro     2326:     for ( i = 0; i < n; ) {
1.172     noro     2327:         if ( nd_gentrace ) {
1.167     noro     2328:             /* Trace = [1,index,1,1] */
                   2329:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2330:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2331:         }
1.157     noro     2332:         g = ndvtond(m,nd_ps[i]);
                   2333:         g = nd_separate_head(g,&head);
                   2334:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2335:         if ( !stat )
                   2336:             nd_reconstruct(0,0);
                   2337:         else {
                   2338:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2339:             ndv_free(nd_ps[i]);
1.167     noro     2340:             hc = HCU(nf); nd_removecont(m,nf);
1.172     noro     2341:             if ( nd_gentrace ) {
1.167     noro     2342:                                for ( t = nd_tracelist; t; t = NEXT(t) ) {
                   2343:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2344:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2345:                 }
                   2346:                 cont = ndc_div(m,hc,HCU(nf));
                   2347:                 finalize_tracelist(perm[i],cont);
                   2348:             }
1.157     noro     2349:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2350:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2351:             i++;
                   2352:         }
                   2353:     }
                   2354:     if ( DP_Print ) { printf("\n"); }
                   2355:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2356:         NEXTNODE(a0,a);
1.172     noro     2357:                if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
1.167     noro     2358:                else {
                   2359:                        for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2360:                        BDY(a) = (pointer)nd_ps[j];
                   2361:                }
1.157     noro     2362:     }
                   2363:     NEXT(a) = 0;
                   2364:     return a0;
1.23      noro     2365: }
                   2366:
1.168     noro     2367: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2368: {
1.157     noro     2369:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2370:
1.157     noro     2371:     if ( !g ) return d;
1.168     noro     2372:        /* for testing */
1.172     noro     2373:        if ( gensyz && nd_gensyz == 2 ) {
1.168     noro     2374:        d1 = nd_newpairs(g,t);
                   2375:        if ( !d )
                   2376:                 return d1;
                   2377:        else {
                   2378:                 nd = d;
                   2379:                 while ( NEXT(nd) ) nd = NEXT(nd);
                   2380:                 NEXT(nd) = d1;
                   2381:                 return d;
                   2382:        }
                   2383:        }
1.157     noro     2384:     d = crit_B(d,t);
                   2385:     d1 = nd_newpairs(g,t);
                   2386:     d1 = crit_M(d1);
                   2387:     d1 = crit_F(d1);
1.168     noro     2388:     if ( gensyz || do_weyl )
1.157     noro     2389:         head = d1;
                   2390:     else {
                   2391:         prev = 0; cur = head = d1;
                   2392:         while ( cur ) {
                   2393:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2394:                 remove = cur;
                   2395:                 if ( !prev ) head = cur = NEXT(cur);
                   2396:                 else cur = NEXT(prev) = NEXT(cur);
                   2397:                 FREENDP(remove);
                   2398:             } else {
                   2399:                 prev = cur; cur = NEXT(cur);
                   2400:             }
                   2401:         }
                   2402:     }
                   2403:     if ( !d )
                   2404:         return head;
                   2405:     else {
                   2406:         nd = d;
                   2407:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2408:         NEXT(nd) = head;
                   2409:         return d;
                   2410:     }
1.1       noro     2411: }
                   2412:
1.157     noro     2413:
1.1       noro     2414: ND_pairs nd_newpairs( NODE g, int t )
                   2415: {
1.157     noro     2416:     NODE h;
                   2417:     UINT *dl;
1.187     noro     2418:     int ts,s,i,t0,min,max;
1.157     noro     2419:     ND_pairs r,r0;
                   2420:
                   2421:     dl = DL(nd_psh[t]);
                   2422:     ts = SG(nd_psh[t]) - TD(dl);
1.195     noro     2423:        if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
1.157     noro     2424:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2425:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2426:                 continue;
1.187     noro     2427:                if ( nd_gbblock ) {
                   2428:                        t0 = (long)BDY(h);
                   2429:                        for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   2430:                                min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   2431:                                if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   2432:                                        break;
                   2433:                        }
1.188     noro     2434:                        if ( nd_gbblock[i] >= 0 )
1.187     noro     2435:                                continue;
                   2436:                }
1.157     noro     2437:         NEXTND_pairs(r0,r);
1.159     noro     2438:         r->i1 = (long)BDY(h);
1.157     noro     2439:         r->i2 = t;
                   2440:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2441:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2442:         SG(r) = MAX(s,ts) + TD(LCM(r));
                   2443:     }
                   2444:     if ( r0 ) NEXT(r) = 0;
                   2445:     return r0;
1.1       noro     2446: }
                   2447:
1.157     noro     2448: /* kokokara */
                   2449:
1.1       noro     2450: ND_pairs crit_B( ND_pairs d, int s )
                   2451: {
1.157     noro     2452:     ND_pairs cur,head,prev,remove;
                   2453:     UINT *t,*tl,*lcm;
                   2454:     int td,tdl;
                   2455:
                   2456:     if ( !d ) return 0;
                   2457:     t = DL(nd_psh[s]);
                   2458:     prev = 0;
                   2459:     head = cur = d;
                   2460:     lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2461:     while ( cur ) {
                   2462:         tl = cur->lcm;
1.163     noro     2463:         if ( ndl_reducible(tl,t) ) {
                   2464:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2465:             if ( !ndl_equal(lcm,tl) ) {
                   2466:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2467:                 if (!ndl_equal(lcm,tl)) {
                   2468:                     remove = cur;
                   2469:                     if ( !prev ) {
                   2470:                         head = cur = NEXT(cur);
                   2471:                     } else {
                   2472:                         cur = NEXT(prev) = NEXT(cur);
                   2473:                     }
                   2474:                     FREENDP(remove);
                   2475:                 } else {
                   2476:                     prev = cur; cur = NEXT(cur);
                   2477:                 }
                   2478:             } else {
                   2479:                     prev = cur; cur = NEXT(cur);
                   2480:             }
1.157     noro     2481:         } else {
                   2482:             prev = cur; cur = NEXT(cur);
                   2483:         }
                   2484:     }
                   2485:     return head;
1.1       noro     2486: }
                   2487:
                   2488: ND_pairs crit_M( ND_pairs d1 )
                   2489: {
1.157     noro     2490:     ND_pairs e,d2,d3,dd,p;
                   2491:     UINT *id,*jd;
1.1       noro     2492:
1.157     noro     2493:     if ( !d1 ) return d1;
                   2494:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2495:         if ( !(d2 = NEXT(e)) ) {
                   2496:             NEXT(e) = dd;
                   2497:             return e;
                   2498:         }
                   2499:         id = LCM(e);
                   2500:         for ( d3 = 0; d2; d2 = p ) {
                   2501:             p = NEXT(d2);
                   2502:             jd = LCM(d2);
                   2503:             if ( ndl_equal(jd,id) )
                   2504:                 ;
                   2505:             else if ( TD(jd) > TD(id) )
                   2506:                 if ( ndl_reducible(jd,id) ) continue;
                   2507:                 else ;
                   2508:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2509:             NEXT(d2) = d3;
                   2510:             d3 = d2;
                   2511:         }
                   2512:         NEXT(e) = dd;
                   2513:         dd = e;
                   2514:         continue;
                   2515:         /**/
                   2516:     delit:    NEXT(d2) = d3;
                   2517:         d3 = d2;
                   2518:         for ( ; p; p = d2 ) {
                   2519:             d2 = NEXT(p);
                   2520:             NEXT(p) = d3;
                   2521:             d3 = p;
                   2522:         }
                   2523:         FREENDP(e);
                   2524:     }
                   2525:     return dd;
1.1       noro     2526: }
                   2527:
                   2528: ND_pairs crit_F( ND_pairs d1 )
                   2529: {
1.157     noro     2530:     ND_pairs rest, head,remove;
                   2531:     ND_pairs last, p, r, w;
                   2532:     int s;
                   2533:
                   2534:     if ( !d1 ) return d1;
                   2535:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2536:         r = w = equivalent_pairs(p,&rest);
                   2537:         s = SG(r);
                   2538:         w = NEXT(w);
                   2539:         while ( w ) {
                   2540:             if ( crit_2(w->i1,w->i2) ) {
                   2541:                 r = w;
                   2542:                 w = NEXT(w);
                   2543:                 while ( w ) {
                   2544:                     remove = w;
                   2545:                     w = NEXT(w);
                   2546:                     FREENDP(remove);
                   2547:                 }
                   2548:                 break;
                   2549:             } else if ( SG(w) < s ) {
                   2550:                 FREENDP(r);
                   2551:                 r = w;
                   2552:                 s = SG(r);
                   2553:                 w = NEXT(w);
                   2554:             } else {
                   2555:                 remove = w;
                   2556:                 w = NEXT(w);
                   2557:                 FREENDP(remove);
                   2558:             }
                   2559:         }
                   2560:         if ( last ) NEXT(last) = r;
                   2561:         else head = r;
                   2562:         NEXT(last = r) = 0;
                   2563:         p = rest;
                   2564:         if ( !p ) return head;
                   2565:     }
                   2566:     if ( !last ) return p;
                   2567:     NEXT(last) = p;
                   2568:     return head;
1.1       noro     2569: }
                   2570:
                   2571: int crit_2( int dp1, int dp2 )
                   2572: {
1.157     noro     2573:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2574: }
                   2575:
1.40      noro     2576: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2577: {
1.157     noro     2578:     ND_pairs w,p,r,s;
                   2579:     UINT *d;
1.1       noro     2580:
1.157     noro     2581:     w = d1;
                   2582:     d = LCM(w);
                   2583:     s = NEXT(w);
                   2584:     NEXT(w) = 0;
                   2585:     for ( r = 0; s; s = p ) {
                   2586:         p = NEXT(s);
                   2587:         if ( ndl_equal(d,LCM(s)) ) {
                   2588:             NEXT(s) = w; w = s;
                   2589:         } else {
                   2590:             NEXT(s) = r; r = s;
                   2591:         }
                   2592:     }
                   2593:     *prest = r;
                   2594:     return w;
1.1       noro     2595: }
                   2596:
                   2597: NODE update_base(NODE nd,int ndp)
                   2598: {
1.157     noro     2599:     UINT *dl, *dln;
                   2600:     NODE last, p, head;
1.1       noro     2601:
1.157     noro     2602:     dl = DL(nd_psh[ndp]);
                   2603:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2604:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2605:         if ( ndl_reducible( dln, dl ) ) {
                   2606:             p = NEXT(p);
                   2607:             if ( last ) NEXT(last) = p;
                   2608:         } else {
                   2609:             if ( !last ) head = p;
                   2610:             p = NEXT(last = p);
                   2611:         }
                   2612:     }
                   2613:     head = append_one(head,ndp);
                   2614:     return head;
1.1       noro     2615: }
                   2616:
                   2617: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2618: {
1.157     noro     2619:     ND_pairs m,ml,p,l;
                   2620:     UINT *lcm;
                   2621:     int s,td,len,tlen,c,c1;
                   2622:
                   2623:     if ( !(p = NEXT(m = d)) ) {
                   2624:         *prest = p;
                   2625:         NEXT(m) = 0;
                   2626:         return m;
                   2627:     }
                   2628:     s = SG(m);
                   2629:     if ( !NoSugar ) {
                   2630:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2631:             if ( (SG(p) < s)
                   2632:                 || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2633:                 ml = l; m = p; s = SG(m);
                   2634:             }
                   2635:     } else {
                   2636:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2637:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2638:                 ml = l; m = p; s = SG(m);
                   2639:             }
                   2640:     }
                   2641:     if ( !ml ) *prest = NEXT(m);
                   2642:     else {
                   2643:         NEXT(ml) = NEXT(m);
                   2644:         *prest = d;
                   2645:     }
                   2646:     NEXT(m) = 0;
                   2647:     return m;
1.1       noro     2648: }
                   2649:
1.63      noro     2650: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2651: {
1.157     noro     2652:     int msugar,i;
                   2653:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2654:
1.157     noro     2655:     for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2656:         if ( SG(t) < msugar ) msugar = SG(t);
                   2657:     dm0 = 0; dr0 = 0;
                   2658:     for ( i = 0, t = d; t; t = NEXT(t) )
                   2659:         if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2660:             if ( dm0 ) NEXT(dm) = t;
                   2661:             else dm0 = t;
                   2662:             dm = t;
                   2663:             i++;
                   2664:         } else {
                   2665:             if ( dr0 ) NEXT(dr) = t;
                   2666:             else dr0 = t;
                   2667:             dr = t;
                   2668:         }
                   2669:     NEXT(dm) = 0;
                   2670:     if ( dr0 ) NEXT(dr) = 0;
                   2671:     *prest = dr0;
                   2672:     return dm0;
1.63      noro     2673: }
                   2674:
1.77      noro     2675: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2676: {
1.157     noro     2677:     int len;
                   2678:     RHist r;
                   2679:     NDV b;
1.167     noro     2680:     NODE tn;
                   2681:     LIST l;
                   2682:     Q iq;
1.157     noro     2683:
                   2684:     if ( nd_psn == nd_pslen ) {
                   2685:         nd_pslen *= 2;
                   2686:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   2687:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2688:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2689:         nd_bound = (UINT **)
                   2690:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   2691:     }
                   2692:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2693:     nd_ps[nd_psn] = a;
                   2694:     if ( aq ) {
                   2695:         nd_ps_trace[nd_psn] = aq;
                   2696:         register_hcf(aq);
                   2697:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   2698:         SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
                   2699:     } else {
                   2700:         if ( !m ) register_hcf(a);
                   2701:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   2702:         SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
                   2703:     }
                   2704:     if ( nd_demand ) {
                   2705:         if ( aq ) {
                   2706:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2707:             nd_ps_trace[nd_psn] = 0;
                   2708:         } else {
                   2709:             ndv_save(nd_ps[nd_psn],nd_psn);
                   2710:             nd_ps[nd_psn] = 0;
                   2711:         }
                   2712:     }
1.172     noro     2713:     if ( nd_gentrace ) {
1.167     noro     2714:         /* reverse the tracelist and append it to alltracelist */
                   2715:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2716:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2717:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2718:     }
1.157     noro     2719:     return nd_psn++;
1.1       noro     2720: }
                   2721:
1.167     noro     2722: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     2723: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     2724:
1.177     noro     2725: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2726: {
1.157     noro     2727:     int i,j,td,len,max;
1.167     noro     2728:     NODE s,s0,f0,tn;
1.157     noro     2729:     UINT *d;
                   2730:     RHist r;
1.167     noro     2731:     NDVI w;
1.157     noro     2732:     NDV a,am;
1.167     noro     2733:     union oNDC hc;
                   2734:     NODE node;
                   2735:     P hcp;
                   2736:     Q iq,jq,hcq;
                   2737:     LIST l;
1.157     noro     2738:
                   2739:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2740:     /* initialize the tracelist */
                   2741:     nd_tracelist = 0;
1.157     noro     2742:
                   2743:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.167     noro     2744:     w = (NDVI)ALLOCA(nd_psn*sizeof(struct oNDVI));
                   2745:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2746:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2747:     if ( !dont_sort ) {
                   2748:         /* XXX heuristic */
                   2749:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2750:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2751:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     2752:         else
1.167     noro     2753:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2754:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     2755:     }
                   2756:     nd_pslen = 2*nd_psn;
                   2757:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2758:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   2759:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   2760:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   2761:     nd_hcf = 0;
                   2762:
                   2763:     if ( trace && nd_vc )
                   2764:         makesubst(nd_vc,&nd_subst);
                   2765:     else
                   2766:         nd_subst = 0;
                   2767:
                   2768:     if ( !nd_red )
                   2769:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   2770:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   2771:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     2772:         hc = HCU(w[i].p);
1.157     noro     2773:         if ( trace ) {
1.167     noro     2774:             a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.157     noro     2775:             if ( !dont_removecont) ndv_removecont(0,a);
                   2776:             register_hcf(a);
                   2777:             am = nd_ps[i] = ndv_dup(mod,a);
                   2778:             ndv_mod(mod,am);
1.177     noro     2779:            if ( DL_COMPARE(HDL(am),HDL(a)) )
                   2780:                return 0;
1.157     noro     2781:             ndv_removecont(mod,am);
                   2782:         } else {
1.167     noro     2783:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.157     noro     2784:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   2785:             if ( !mod ) register_hcf(a);
                   2786:         }
1.172     noro     2787:         if ( nd_gentrace ) {
1.167     noro     2788:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     2789:                        if ( !dont_removecont )
                   2790:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     2791:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   2792:         }
1.157     noro     2793:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   2794:         nd_bound[i] = ndv_compute_bound(a);
                   2795:         nd_psh[i] = r;
                   2796:         if ( nd_demand ) {
                   2797:             if ( trace ) {
                   2798:                 ndv_save(nd_ps_trace[i],i);
                   2799:                 nd_ps_trace[i] = 0;
                   2800:             } else {
                   2801:                 ndv_save(nd_ps[i],i);
                   2802:                 nd_ps[i] = 0;
                   2803:             }
                   2804:         }
                   2805:     }
1.172     noro     2806:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     2807:     return 1;
1.20      noro     2808: }
                   2809:
1.119     noro     2810: struct order_spec *append_block(struct order_spec *spec,
                   2811:     int nv,int nalg,int ord);
                   2812:
1.121     noro     2813: extern VECT current_dl_weight_vector_obj;
                   2814: static VECT prev_weight_vector_obj;
                   2815:
1.120     noro     2816: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     2817:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     2818: {
1.157     noro     2819:     NODE alist,t,s,r0,r,arg;
                   2820:     VL tv;
                   2821:     P poly;
                   2822:     DP d;
                   2823:     Alg alpha,dp;
                   2824:     DAlg inv,da,hc;
                   2825:     MP m;
                   2826:     int i,nvar,nalg,n;
                   2827:     NumberField nf;
                   2828:     LIST f1,f2;
                   2829:     struct order_spec *current_spec;
                   2830:     VECT obj,obj0;
                   2831:     Obj tmp;
                   2832:
                   2833:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   2834:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   2835:
                   2836:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   2837:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   2838:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   2839:         tv->v = tv->v->priv;
                   2840:     }
                   2841:     NEXT(t) = 0;
                   2842:
1.167     noro     2843:     /* simplification, making polynomials monic */
1.157     noro     2844:     setfield_dalg(alist);
                   2845:     obj_algtodalg(f,&f1);
                   2846:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   2847:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   2848:         hc = (DAlg)BDY(d)->c;
                   2849:         if ( NID(hc) == N_DA ) {
                   2850:             invdalg(hc,&inv);
                   2851:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   2852:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   2853:             }
                   2854:         }
                   2855:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   2856:     }
                   2857:     obj_dalgtoalg(f1,&f);
                   2858:
                   2859:     /* append alg vars to the var list */
                   2860:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   2861:     NEXT(tv) = av;
                   2862:
                   2863:     /* append a block to ord */
                   2864:     *ord1p = append_block(ord,nvar,nalg,2);
                   2865:
                   2866:     /* create generator list */
                   2867:     nf = get_numberfield();
                   2868:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   2869:         MKAlg(nf->defpoly[i],dp);
                   2870:         MKNODE(s,dp,t); t = s;
                   2871:     }
                   2872:     MKLIST(f1,t);
                   2873:     *alistp = alist;
                   2874:     algobjtorat(f1,f1p);
                   2875:
                   2876:     /* creating a new weight vector */
                   2877:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   2878:     n = nvar+nalg+1;
                   2879:     MKVECT(obj,n);
                   2880:     if ( obj0 && obj0->len == nvar )
                   2881:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   2882:     else
                   2883:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   2884:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   2885:     BDY(obj)[n-1] = (pointer)ONE;
                   2886:     arg = mknode(1,obj);
                   2887:     Pdp_set_weight(arg,&tmp);
1.121     noro     2888: }
                   2889:
                   2890: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   2891: {
1.157     noro     2892:     NODE s,t,u0,u;
                   2893:     P p;
                   2894:     VL tv;
                   2895:     Obj obj,tmp;
                   2896:     NODE arg;
                   2897:
                   2898:     u0 = 0;
                   2899:     for ( t = r; t; t = NEXT(t) ) {
                   2900:         p = (P)BDY(t);
                   2901:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   2902:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   2903:         }
                   2904:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   2905:             NEXTNODE(u0,u);
                   2906:             BDY(u) = (pointer)p;
                   2907:         }
                   2908:     }
                   2909:     arg = mknode(1,prev_weight_vector_obj);
                   2910:     Pdp_set_weight(arg,&tmp);
1.121     noro     2911:
1.157     noro     2912:     return u0;
1.120     noro     2913: }
                   2914:
1.199     noro     2915: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2916: {
1.157     noro     2917:     VL tv,fv,vv,vc,av;
                   2918:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2919:     int e,max,nvar,i;
                   2920:     NDV b;
1.184     noro     2921:     int ishomo,nalg,mrank,trank,wmax,len;
                   2922:        NMV a;
1.157     noro     2923:     Alg alpha,dp;
                   2924:     P p,zp;
                   2925:     Q dmy;
                   2926:     LIST f1,f2,zpl;
                   2927:     Obj obj;
                   2928:     NumberField nf;
                   2929:     struct order_spec *ord1;
1.208     noro     2930:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
1.170     noro     2931:     LIST l1,l2,l3,l4,l5;
1.167     noro     2932:        int j;
1.198     noro     2933:        Q jq,bpe;
1.167     noro     2934:     int *perm;
1.170     noro     2935:     EPOS oepos;
1.194     noro     2936:     int obpe,oadv,ompos,cbpe;
1.1       noro     2937:
1.174     noro     2938:     nd_module = 0;
1.157     noro     2939:     if ( !m && Demand ) nd_demand = 1;
                   2940:     else nd_demand = 0;
1.172     noro     2941:     parse_nd_option(current_option);
1.78      noro     2942:
1.157     noro     2943:     if ( DP_Multiple )
                   2944:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     2945: #if 0
1.157     noro     2946:     ndv_alloc = 0;
1.103     noro     2947: #endif
1.157     noro     2948:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   2949:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2950:     switch ( ord->id ) {
                   2951:         case 1:
                   2952:             if ( ord->nv != nvar )
                   2953:                 error("nd_{gr,f4} : invalid order specification");
                   2954:             break;
                   2955:         default:
                   2956:             break;
                   2957:     }
                   2958:     nd_nalg = 0;
                   2959:     av = 0;
                   2960:     if ( !m ) {
                   2961:         get_algtree((Obj)f,&av);
                   2962:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2963:         nd_ntrans = nvar;
                   2964:         nd_nalg = nalg;
                   2965:         /* #i -> t#i */
                   2966:         if ( nalg ) {
                   2967:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2968:             ord = ord1;
                   2969:             f = f1;
                   2970:         }
                   2971:         nvar += nalg;
                   2972:     }
                   2973:     nd_init_ord(ord);
                   2974:     mrank = 0;
1.178     noro     2975:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     2976:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2977:             if ( nd_module ) {
                   2978:                 s = BDY((LIST)BDY(t));
                   2979:                 trank = length(s);
                   2980:                 mrank = MAX(mrank,trank);
                   2981:                 for ( ; s; s = NEXT(s) ) {
                   2982:                     e = getdeg(tv->v,(P)BDY(s));
                   2983:                     max = MAX(e,max);
                   2984:                 }
                   2985:             } else {
                   2986:                 e = getdeg(tv->v,(P)BDY(t));
                   2987:                 max = MAX(e,max);
                   2988:             }
                   2989:         }
1.208     noro     2990:     nd_setup_parameters(nvar,nd_nzlist?0:max);
1.170     noro     2991:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     2992:     ishomo = 1;
                   2993:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     2994:         if ( nd_module ) {
1.172     noro     2995:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     2996:                        else zpl = (LIST)BDY(t);
1.157     noro     2997:             b = (pointer)pltondv(CO,vv,zpl);
                   2998:         } else {
1.172     noro     2999:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3000:                        else zp = (P)BDY(t);
1.157     noro     3001:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     3002:         }
1.157     noro     3003:         if ( ishomo )
                   3004:             ishomo = ishomo && ndv_ishomo(b);
                   3005:         if ( m ) ndv_mod(m,b);
                   3006:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3007:     }
                   3008:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     3009:
                   3010:        if ( !ishomo && homo ) {
                   3011:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   3012:             b = (NDV)BDY(t); len = LEN(b);
                   3013:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   3014:                 wmax = MAX(TD(DL(a)),wmax);
                   3015:         }
                   3016:         homogenize_order(ord,nvar,&ord1);
                   3017:         nd_init_ord(ord1);
                   3018:         nd_setup_parameters(nvar+1,wmax);
                   3019:         for ( t = fd0; t; t = NEXT(t) )
                   3020:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   3021:     }
                   3022:
1.211     noro     3023:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
1.172     noro     3024:     if ( nd_gentrace ) {
1.167     noro     3025:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3026:     }
1.209     noro     3027:        if ( nd_splist ) {
                   3028:                *rp = compute_splist();
                   3029:                return;
                   3030:        }
                   3031:        if ( nd_check_splist ) {
1.214   ! noro     3032:         if ( f4 ) {
        !          3033:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
        !          3034:             else *rp = 0;
        !          3035:         } else {
        !          3036:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
        !          3037:             else *rp = 0;
        !          3038:         }
1.209     noro     3039:                return;
                   3040:        }
1.186     noro     3041:     x = f4?nd_f4(m,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.192     noro     3042:        if ( !x ) {
                   3043:                *rp = 0; return;
                   3044:        }
1.184     noro     3045:        if ( !ishomo && homo ) {
                   3046:                /* dehomogenization */
                   3047:                for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3048:                nd_init_ord(ord);
                   3049:                nd_setup_parameters(nvar,0);
                   3050:        }
1.157     noro     3051:     nd_demand = 0;
1.195     noro     3052:        if ( nd_module && nd_intersect ) {
                   3053:                for ( j = nd_psn-1, x = 0; j >= 0; j-- )
                   3054:                        if ( MPOS(DL(nd_psh[j])) > 1 ) {
                   3055:                                MKNODE(xx,(pointer)j,x); x = xx;
                   3056:                        }
                   3057:          conv_ilist(nd_demand,0,x,0);
                   3058:          goto FINAL;
                   3059:        }
1.208     noro     3060:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
1.167     noro     3061:     x = ndv_reducebase(x,perm);
1.208     noro     3062:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3063:     x = ndv_reduceall(m,x);
1.194     noro     3064:     cbpe = nd_bpe;
1.208     noro     3065:     if ( nd_gentrace && !f4 ) {
1.170     noro     3066:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   3067:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198     noro     3068:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3069:         if ( nd_gensyz ) {
                   3070:                nd_gb(m,0,1,1,0);
1.170     noro     3071:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3072:         } else tl4 = 0;
                   3073:     }
1.194     noro     3074:     nd_bpe = cbpe;
                   3075:     nd_setup_parameters(nd_nvar,0);
1.195     noro     3076: FINAL:
1.157     noro     3077:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3078:         NEXTNODE(r0,r);
1.194     noro     3079:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199     noro     3080:         else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   3081:                else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157     noro     3082:     }
                   3083:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3084:     if ( !m && nd_nalg )
1.157     noro     3085:         r0 = postprocess_algcoef(av,alist,r0);
                   3086:     MKLIST(*rp,r0);
1.172     noro     3087:     if ( nd_gentrace ) {
1.208     noro     3088:        if ( f4 ) {
                   3089:             STOQ(16,bpe);
                   3090:             tr = mknode(4,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe); MKLIST(*rp,tr);
                   3091:         } else {
                   3092:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   3093:             tl3 = reverse_node(tl3);
                   3094:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3095:             for ( t = tl2; t; t = NEXT(t) ) {
                   3096:             /* s = [i,[*,j,*,*],...] */
                   3097:                 s = BDY((LIST)BDY(t));
                   3098:                 j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3099:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3100:                     j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3101:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3102:                 }
                   3103:             }
                   3104:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   3105:                 STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.167     noro     3106:             }
1.208     noro     3107:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3108:             MKLIST(l5,tl4);
                   3109:             STOQ(nd_bpe,bpe);
                   3110:             tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
                   3111:         }
1.167     noro     3112:     }
1.103     noro     3113: #if 0
1.157     noro     3114:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     3115: #endif
1.127     noro     3116: }
                   3117:
                   3118: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   3119: {
1.157     noro     3120:     VL tv,fv,vv,vc,av;
                   3121:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3122:     int e,max,nvar,i;
                   3123:     NDV b;
                   3124:     int ishomo,nalg;
                   3125:     Alg alpha,dp;
                   3126:     P p,zp;
                   3127:     Q dmy;
                   3128:     LIST f1,f2;
                   3129:     Obj obj;
                   3130:     NumberField nf;
                   3131:     struct order_spec *ord1;
1.167     noro     3132:     int *perm;
1.157     noro     3133:
1.197     noro     3134:     parse_nd_option(current_option);
1.157     noro     3135:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3136:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3137:     switch ( ord->id ) {
                   3138:         case 1:
                   3139:             if ( ord->nv != nvar )
                   3140:                 error("nd_check : invalid order specification");
                   3141:             break;
                   3142:         default:
                   3143:             break;
                   3144:     }
                   3145:     nd_nalg = 0;
                   3146:     av = 0;
                   3147:     if ( !m ) {
                   3148:         get_algtree((Obj)f,&av);
                   3149:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3150:         nd_ntrans = nvar;
                   3151:         nd_nalg = nalg;
                   3152:         /* #i -> t#i */
                   3153:         if ( nalg ) {
                   3154:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3155:             ord = ord1;
                   3156:             f = f1;
                   3157:         }
                   3158:         nvar += nalg;
                   3159:     }
                   3160:     nd_init_ord(ord);
1.178     noro     3161:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3162:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3163:             e = getdeg(tv->v,(P)BDY(t));
                   3164:             max = MAX(e,max);
                   3165:         }
                   3166:     nd_setup_parameters(nvar,max);
                   3167:     ishomo = 1;
                   3168:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3169:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3170:         b = (pointer)ptondv(CO,vv,zp);
                   3171:         if ( ishomo )
                   3172:             ishomo = ishomo && ndv_ishomo(b);
                   3173:         if ( m ) ndv_mod(m,b);
                   3174:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3175:     }
                   3176:     if ( fd0 ) NEXT(fd) = 0;
                   3177:     ndv_setup(m,0,fd0,0,1);
                   3178:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3179:         x = update_base(x,i);
                   3180:     if ( do_check ) {
1.168     noro     3181:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3182:         if ( !x ) {
                   3183:             *rp = 0;
                   3184:             return;
                   3185:         }
                   3186:     } else {
1.175     noro     3187: #if 0
                   3188:                /* bug ? */
1.157     noro     3189:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3190:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3191: #else
                   3192:                conv_ilist(0,0,x,&perm);
                   3193: #endif
1.157     noro     3194:     }
1.167     noro     3195:     x = ndv_reducebase(x,perm);
1.157     noro     3196:     x = ndv_reduceall(m,x);
                   3197:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3198:         NEXTNODE(r0,r);
                   3199:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3200:     }
                   3201:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3202:     if ( !m && nd_nalg )
1.157     noro     3203:         r0 = postprocess_algcoef(av,alist,r0);
                   3204:     MKLIST(*rp,r0);
1.20      noro     3205: }
                   3206:
1.198     noro     3207: NDV recompute_trace(NODE trace,NDV *p,int m);
                   3208: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   3209:
                   3210: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   3211: {
                   3212:        int c,c1,c2,i;
                   3213:        NM mul,m,tail;
                   3214:        ND d,r,rm;
                   3215:        NODE sj;
                   3216:        NDV red;
                   3217:        Obj mj;
                   3218:        static int afo=0;
                   3219:
                   3220:        afo++;
                   3221:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   3222:        CM(mul) = 1;
                   3223:        tail = 0;
                   3224:        for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   3225:                sj = BDY((LIST)BDY(ti));
                   3226:                if ( ARG0(sj) ) {
                   3227:                        red = p[QTOS((Q)ARG1(sj))];
                   3228:                        mj = (Obj)ARG2(sj);
                   3229:                        if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   3230:                        else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   3231:                        rm = ndv_mul_nm(mod,mul,red);
                   3232:                        if ( !r ) r = rm;
                   3233:                        else {
                   3234:                                for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   3235:                                        if ( d ) {
                   3236:                                                NEXT(tail) = m; tail = m; LEN(d)++;
                   3237:                                        } else {
                   3238:                                                MKND(nd_nvar,m,1,d); tail = BDY(d);
                   3239:                                        }
                   3240:                                }
                   3241:                                if ( !m ) return 0; /* failure */
                   3242:                                else {
                   3243:                                        BDY(r) = m;
                   3244:                                        c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   3245:                                        DMAR(c1,c2,0,mod,c);
                   3246:                                        nd_mul_c(mod,rm,c);
                   3247:                                        r = nd_add(mod,r,rm);
                   3248:                                }
                   3249:                        }
                   3250:                }
                   3251:        }
                   3252:        if ( tail ) NEXT(tail) = 0;
                   3253:        d = nd_add(mod,d,r);
                   3254:        nd_mul_c(mod,d,invm(HCM(d),mod));
                   3255:        return ndtondv(mod,d);
                   3256: }
                   3257:
                   3258: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   3259: {
                   3260:     VL tv,fv,vv,vc,av;
                   3261:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3262:     int e,max,nvar,i;
                   3263:     NDV b;
                   3264:     int ishomo,nalg;
                   3265:     Alg alpha,dp;
                   3266:     P p,zp;
                   3267:     Q dmy;
                   3268:     LIST f1,f2;
                   3269:     Obj obj;
                   3270:     NumberField nf;
                   3271:     struct order_spec *ord1;
                   3272:        NODE permtrace,intred,ind,perm,trace,ti;
                   3273:        int len,n,j;
                   3274:        NDV *db,*pb;
                   3275:
                   3276:     parse_nd_option(current_option);
                   3277:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3278:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3279:     switch ( ord->id ) {
                   3280:         case 1:
                   3281:             if ( ord->nv != nvar )
                   3282:                 error("nd_check : invalid order specification");
                   3283:             break;
                   3284:         default:
                   3285:             break;
                   3286:     }
                   3287:     nd_init_ord(ord);
                   3288:        nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   3289:     nd_setup_parameters(nvar,0);
                   3290:
                   3291:        len = length(BDY(f));
                   3292:        db = (NDV *)MALLOC(len*sizeof(NDV *));
                   3293:        for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   3294:            ptozp((P)BDY(t),1,&dmy,&zp);
                   3295:            b = ptondv(CO,vv,zp);
                   3296:         ndv_mod(m,b);
                   3297:                ndv_mul_c(m,b,invm(HCM(b),m));
                   3298:                db[i] = b;
                   3299:     }
                   3300:
                   3301:        permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   3302:        intred = BDY((LIST)ARG3(BDY(tlist)));
                   3303:        ind = BDY((LIST)ARG4(BDY(tlist)));
                   3304:        perm = BDY((LIST)ARG0(permtrace));
                   3305:        trace = NEXT(permtrace);
                   3306:
                   3307:        for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   3308:                j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
                   3309:                if ( j > i ) i = j;
                   3310:        }
                   3311:        n = i+1;
                   3312:        pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   3313:        for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   3314:                ti = BDY((LIST)BDY(t));
                   3315:                pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
                   3316:        }
                   3317:        for ( t = trace; t; t = NEXT(t) ) {
                   3318:                ti = BDY((LIST)BDY(t));
                   3319:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3320:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3321:        if ( DP_Print ) {
                   3322:                        fprintf(asir_out,"."); fflush(asir_out);
                   3323:        }
                   3324:        }
                   3325:        for ( t = intred; t; t = NEXT(t) ) {
                   3326:                ti = BDY((LIST)BDY(t));
                   3327:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3328:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3329:        if ( DP_Print ) {
                   3330:                        fprintf(asir_out,"*"); fflush(asir_out);
                   3331:        }
                   3332:        }
                   3333:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   3334:         NEXTNODE(r0,r);
                   3335:                b = pb[QTOS((Q)BDY(t))];
                   3336:         ndv_mul_c(m,b,invm(HCM(b),m));
                   3337: #if 0
                   3338:         BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
                   3339: #else
                   3340:         BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
                   3341: #endif
                   3342:     }
                   3343:     if ( r0 ) NEXT(r) = 0;
                   3344:     MKLIST(*rp,r0);
                   3345:     if ( DP_Print ) fprintf(asir_out,"\n");
                   3346: }
                   3347:
1.133     noro     3348: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3349: {
1.157     noro     3350:     VL tv,fv,vv,vc,av;
                   3351:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3352:     int m,nocheck,nvar,mindex,e,max;
                   3353:     NDV c;
                   3354:     NMV a;
                   3355:     P p,zp;
                   3356:     Q dmy;
                   3357:     EPOS oepos;
1.164     noro     3358:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3359:     Alg alpha,dp;
                   3360:     P poly;
1.158     noro     3361:     LIST f1,f2,zpl;
1.157     noro     3362:     Obj obj;
                   3363:     NumberField nf;
                   3364:     struct order_spec *ord1;
                   3365:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3366:     NODE tr,tl1,tl2,tl3,tl4;
                   3367:     LIST l1,l2,l3,l4,l5;
1.167     noro     3368:     int *perm;
1.168     noro     3369:     int j,ret;
1.198     noro     3370:     Q jq,bpe;
1.157     noro     3371:
1.167     noro     3372:     nd_module = 0;
1.172     noro     3373:     parse_nd_option(current_option);
1.157     noro     3374:     if ( DP_Multiple )
                   3375:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3376:
                   3377:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3378:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3379:     switch ( ord->id ) {
                   3380:         case 1:
                   3381:             if ( ord->nv != nvar )
                   3382:                 error("nd_gr_trace : invalid order specification");
                   3383:             break;
                   3384:         default:
                   3385:             break;
                   3386:     }
                   3387:
                   3388:     get_algtree((Obj)f,&av);
                   3389:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3390:     nd_ntrans = nvar;
                   3391:     nd_nalg = nalg;
                   3392:     /* #i -> t#i */
                   3393:     if ( nalg ) {
                   3394:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3395:         ord = ord1;
                   3396:         f = f1;
                   3397:     }
                   3398:     nvar += nalg;
                   3399:
                   3400:     nocheck = 0;
                   3401:     mindex = 0;
                   3402:
                   3403:     if ( Demand ) nd_demand = 1;
                   3404:     else nd_demand = 0;
                   3405:
                   3406:     /* setup modulus */
                   3407:     if ( trace < 0 ) {
                   3408:         trace = -trace;
                   3409:         nocheck = 1;
                   3410:     }
                   3411:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3412:     nd_init_ord(ord);
                   3413:     mrank = 0;
1.178     noro     3414:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3415:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3416:             if ( nd_module ) {
                   3417:                 s = BDY((LIST)BDY(t));
                   3418:                 trank = length(s);
                   3419:                 mrank = MAX(mrank,trank);
                   3420:                 for ( ; s; s = NEXT(s) ) {
                   3421:                     e = getdeg(tv->v,(P)BDY(s));
                   3422:                     max = MAX(e,max);
                   3423:                 }
                   3424:             } else {
                   3425:                 e = getdeg(tv->v,(P)BDY(t));
                   3426:                 max = MAX(e,max);
                   3427:             }
1.157     noro     3428:         }
                   3429:     nd_setup_parameters(nvar,max);
1.164     noro     3430:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3431:     ishomo = 1;
                   3432:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3433:         if ( nd_module ) {
1.172     noro     3434:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3435:                        else zpl = (LIST)BDY(t);
1.158     noro     3436:             c = (pointer)pltondv(CO,vv,zpl);
                   3437:         } else {
1.172     noro     3438:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3439:                        else zp = (P)BDY(t);
1.158     noro     3440:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3441:         }
1.157     noro     3442:         if ( ishomo )
                   3443:             ishomo = ishomo && ndv_ishomo(c);
                   3444:         if ( c ) {
                   3445:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3446:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3447:         }
                   3448:     }
                   3449:     if ( in0 ) NEXT(in) = 0;
                   3450:     if ( fd0 ) NEXT(fd) = 0;
                   3451:     if ( !ishomo && homo ) {
                   3452:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3453:             c = (NDV)BDY(t); len = LEN(c);
                   3454:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3455:                 wmax = MAX(TD(DL(a)),wmax);
                   3456:         }
                   3457:         homogenize_order(ord,nvar,&ord1);
                   3458:         nd_init_ord(ord1);
                   3459:         nd_setup_parameters(nvar+1,wmax);
                   3460:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3461:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3462:     }
                   3463:     while ( 1 ) {
1.183     noro     3464:                tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3465:         if ( Demand )
                   3466:             nd_demand = 1;
1.187     noro     3467:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3468:         if ( nd_gentrace ) {
1.167     noro     3469:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3470:         }
1.177     noro     3471:         if ( ret )
                   3472:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3473:         if ( !ret || !cand ) {
1.157     noro     3474:             /* failure */
                   3475:             if ( trace > 1 ) { *rp = 0; return; }
                   3476:             else m = get_lprime(++mindex);
                   3477:             continue;
                   3478:         }
                   3479:         if ( !ishomo && homo ) {
                   3480:             /* dehomogenization */
                   3481:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3482:             nd_init_ord(ord);
                   3483:             nd_setup_parameters(nvar,0);
                   3484:         }
                   3485:         nd_demand = 0;
1.167     noro     3486:         cand = ndv_reducebase(cand,perm);
1.172     noro     3487:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3488:         cand = ndv_reduceall(0,cand);
                   3489:         cbpe = nd_bpe;
1.172     noro     3490:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3491:         get_eg(&eg0);
1.157     noro     3492:         if ( nocheck )
                   3493:             break;
1.170     noro     3494:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3495:             if ( nd_gentrace ) {
1.168     noro     3496:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3497:                    } else tl3 = 0;
                   3498:             /* gbcheck : cand is a GB of Id(cand) ? */
1.172     noro     3499:             ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3500:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3501:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3502:                    } else tl4 = 0;
                   3503:                }
                   3504:                if ( ret ) break;
1.157     noro     3505:         else if ( trace > 1 ) {
                   3506:             /* failure */
                   3507:             *rp = 0; return;
                   3508:         } else {
                   3509:             /* try the next modulus */
                   3510:             m = get_lprime(++mindex);
                   3511:             /* reset the parameters */
                   3512:             if ( !ishomo && homo ) {
                   3513:                 nd_init_ord(ord1);
                   3514:                 nd_setup_parameters(nvar+1,wmax);
                   3515:             } else {
                   3516:                 nd_init_ord(ord);
                   3517:                 nd_setup_parameters(nvar,max);
                   3518:             }
                   3519:         }
                   3520:     }
                   3521:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3522:     if ( DP_Print )
                   3523:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3524:     /* dp->p */
                   3525:     nd_bpe = cbpe;
                   3526:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3527:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3528:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3529:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3530:     }
1.208     noro     3531:     if ( nd_nalg )
1.157     noro     3532:         cand = postprocess_algcoef(av,alist,cand);
                   3533:     MKLIST(*rp,cand);
1.172     noro     3534:     if ( nd_gentrace ) {
1.167     noro     3535:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3536:                tl3 = reverse_node(tl3);
1.167     noro     3537:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3538:         for ( t = tl2; t; t = NEXT(t) ) {
                   3539:                        /* s = [i,[*,j,*,*],...] */
                   3540:             s = BDY((LIST)BDY(t));
                   3541:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3542:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3543:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3544:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3545:             }
                   3546:                }
                   3547:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3548:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3549:                }
1.168     noro     3550:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3551:                MKLIST(l5,tl4);
1.198     noro     3552:                STOQ(nd_bpe,bpe);
                   3553:         tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     3554:     }
1.157     noro     3555: }
1.52      noro     3556:
1.157     noro     3557: /* XXX : module element is not considered  */
1.1       noro     3558:
1.61      noro     3559: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3560: {
1.157     noro     3561:     UINT *d;
                   3562:     int i,j,l,s,ord_l;
                   3563:     struct order_pair *op;
                   3564:
                   3565:     d = dl->d;
                   3566:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3567:     if ( nd_blockmask ) {
                   3568:         l = nd_blockmask->n;
                   3569:         op = nd_blockmask->order_pair;
                   3570:         for ( j = 0, s = 0; j < l; j++ ) {
                   3571:             ord_l = op[j].length;
                   3572:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3573:         }
                   3574:         TD(r) = ndl_weight(r);
                   3575:         ndl_weight_mask(r);
                   3576:     } else {
                   3577:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3578:         TD(r) = ndl_weight(r);
                   3579:     }
1.1       noro     3580: }
                   3581:
1.61      noro     3582: DL ndltodl(int n,UINT *ndl)
1.1       noro     3583: {
1.157     noro     3584:     DL dl;
                   3585:     int *d;
                   3586:     int i,j,l,s,ord_l;
                   3587:     struct order_pair *op;
                   3588:
                   3589:     NEWDL(dl,n);
                   3590:     dl->td = TD(ndl);
                   3591:     d = dl->d;
                   3592:     if ( nd_blockmask ) {
                   3593:         l = nd_blockmask->n;
                   3594:         op = nd_blockmask->order_pair;
                   3595:         for ( j = 0, s = 0; j < l; j++ ) {
                   3596:             ord_l = op[j].length;
                   3597:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3598:         }
                   3599:     } else {
                   3600:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3601:     }
                   3602:     return dl;
1.1       noro     3603: }
                   3604:
1.167     noro     3605: void nmtodp(int mod,NM m,DP *r)
                   3606: {
                   3607:     DP dp;
                   3608:     MP mr;
                   3609:
                   3610:     NEWMP(mr);
                   3611:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3612:     mr->c = ndctop(mod,m->c);
                   3613:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3614:     *r = dp;
                   3615: }
                   3616:
1.61      noro     3617: void ndl_print(UINT *dl)
1.1       noro     3618: {
1.157     noro     3619:     int n;
                   3620:     int i,j,l,ord_l,s,s0;
                   3621:     struct order_pair *op;
                   3622:
                   3623:     n = nd_nvar;
                   3624:     printf("<<");
                   3625:     if ( nd_blockmask ) {
                   3626:         l = nd_blockmask->n;
                   3627:         op = nd_blockmask->order_pair;
                   3628:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3629:             ord_l = op[j].length;
                   3630:             for ( i = 0; i < ord_l; i++, s++ )
                   3631:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3632:         }
                   3633:     } else {
                   3634:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3635:     }
                   3636:     printf(">>");
                   3637:     if ( MPOS(dl) )
                   3638:         printf("*e%d",MPOS(dl));
1.1       noro     3639: }
                   3640:
                   3641: void nd_print(ND p)
                   3642: {
1.157     noro     3643:     NM m;
1.1       noro     3644:
1.157     noro     3645:     if ( !p )
                   3646:         printf("0\n");
                   3647:     else {
                   3648:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3649:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3650:             else printf("+%d*",CM(m));
                   3651:             ndl_print(DL(m));
                   3652:         }
                   3653:         printf("\n");
                   3654:     }
1.1       noro     3655: }
                   3656:
1.113     noro     3657: void nd_print_q(ND p)
1.16      noro     3658: {
1.157     noro     3659:     NM m;
1.16      noro     3660:
1.157     noro     3661:     if ( !p )
                   3662:         printf("0\n");
                   3663:     else {
                   3664:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3665:             printf("+");
                   3666:             printexpr(CO,(Obj)CQ(m));
                   3667:             printf("*");
                   3668:             ndl_print(DL(m));
                   3669:         }
                   3670:         printf("\n");
                   3671:     }
1.16      noro     3672: }
                   3673:
1.1       noro     3674: void ndp_print(ND_pairs d)
                   3675: {
1.157     noro     3676:     ND_pairs t;
1.1       noro     3677:
1.157     noro     3678:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3679:     printf("\n");
1.1       noro     3680: }
                   3681:
1.20      noro     3682: void nd_removecont(int mod,ND p)
1.16      noro     3683: {
1.157     noro     3684:     int i,n;
                   3685:     Q *w;
                   3686:     Q dvr,t;
                   3687:     NM m;
                   3688:     struct oVECT v;
                   3689:     N q,r;
                   3690:
                   3691:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   3692:     else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   3693:     else {
                   3694:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3695:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3696:         v.len = n;
                   3697:         v.body = (pointer *)w;
                   3698:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3699:         removecont_array((P *)w,n,1);
                   3700:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3701:     }
1.16      noro     3702: }
                   3703:
1.21      noro     3704: void nd_removecont2(ND p1,ND p2)
                   3705: {
1.157     noro     3706:     int i,n1,n2,n;
                   3707:     Q *w;
                   3708:     Q dvr,t;
                   3709:     NM m;
                   3710:     struct oVECT v;
                   3711:     N q,r;
                   3712:
                   3713:     n1 = nd_length(p1);
                   3714:     n2 = nd_length(p2);
                   3715:     n = n1+n2;
                   3716:     w = (Q *)ALLOCA(n*sizeof(Q));
                   3717:     v.len = n;
                   3718:     v.body = (pointer *)w;
                   3719:     i = 0;
                   3720:     if ( p1 )
                   3721:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3722:     if ( p2 )
                   3723:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3724:     removecont_array((P *)w,n,1);
                   3725:     i = 0;
                   3726:     if ( p1 )
                   3727:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3728:     if ( p2 )
                   3729:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     3730: }
                   3731:
1.20      noro     3732: void ndv_removecont(int mod,NDV p)
1.16      noro     3733: {
1.157     noro     3734:     int i,len,all_p;
                   3735:     Q *c;
                   3736:     P *w;
                   3737:     Q dvr,t;
                   3738:     P g,cont,tp;
                   3739:     NMV m;
                   3740:
                   3741:     if ( mod == -1 )
                   3742:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   3743:     else if ( mod )
                   3744:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   3745:     else {
                   3746:         len = p->len;
                   3747:         w = (P *)ALLOCA(len*sizeof(P));
                   3748:         c = (Q *)ALLOCA(len*sizeof(Q));
                   3749:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   3750:             ptozp(CP(m),1,&c[i],&w[i]);
                   3751:             all_p = all_p && !NUM(w[i]);
                   3752:         }
                   3753:         if ( all_p ) {
                   3754:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   3755:             mulp(nd_vc,(P)dvr,g,&cont);
                   3756:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3757:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   3758:             }
                   3759:         } else {
                   3760:             sortbynm((Q *)c,len);
                   3761:             qltozl((Q *)c,len,&dvr);
                   3762:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   3763:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   3764:             }
                   3765:         }
                   3766:     }
1.21      noro     3767: }
                   3768:
1.157     noro     3769: /* koko */
                   3770:
1.164     noro     3771: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     3772: {
1.157     noro     3773:     int len,i,max;
                   3774:     NMV m,mr0,mr,t;
1.61      noro     3775:
1.157     noro     3776:     len = p->len;
1.178     noro     3777:     for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157     noro     3778:         max = MAX(max,TD(DL(m)));
                   3779:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3780:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   3781:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3782:     t = (NMV)ALLOCA(nmv_adv);
                   3783:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     3784:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     3785:         CQ(mr) = CQ(m);
                   3786:         ndl_copy(DL(t),DL(mr));
                   3787:     }
                   3788:     NV(p)++;
                   3789:     BDY(p) = mr0;
1.61      noro     3790: }
                   3791:
1.45      noro     3792: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     3793: {
1.164     noro     3794:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     3795:     int pos;
1.157     noro     3796:     Q *w;
                   3797:     Q dvr,t;
                   3798:     NMV m,r;
                   3799:
                   3800:     len = p->len;
                   3801:     newnvar = nd_nvar-1;
                   3802:     newexporigin = nd_get_exporigin(ord);
1.167     noro     3803:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     3804:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   3805:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   3806:         ndl_dehomogenize(DL(m));
                   3807:     if ( newwpd != nd_wpd ) {
                   3808:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   3809:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   3810:             CQ(r) = CQ(m);
1.167     noro     3811:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     3812:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   3813:             adj = nd_exporigin-newexporigin;
                   3814:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     3815:             if ( nd_module ) {
                   3816:                 DL(r)[newmpos] = pos;
                   3817:             }
1.157     noro     3818:         }
                   3819:     }
                   3820:     NV(p)--;
1.23      noro     3821: }
                   3822:
1.150     noro     3823: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   3824: {
1.157     noro     3825:     int i;
                   3826:     P *tpl,*tpl1;
                   3827:     NODE l;
                   3828:     P h,gcd,t;
                   3829:
                   3830:     tpl = (P *)ALLOCA(m*sizeof(P));
                   3831:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   3832:     bcopy(pl,tpl,m*sizeof(P));
                   3833:     gcd = (P)ONE;
                   3834:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   3835:         h = (P)BDY(l);
                   3836:         while ( 1 ) {
                   3837:             for ( i = 0; i < m; i++ )
                   3838:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   3839:                     break;
                   3840:             if ( i == m ) {
                   3841:                 bcopy(tpl1,tpl,m*sizeof(P));
                   3842:                 mulp(vl,gcd,h,&t); gcd = t;
                   3843:             } else
                   3844:                 break;
                   3845:         }
                   3846:     }
                   3847:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   3848:     if ( full ) {
                   3849:         heu_nezgcdnpz(vl,tpl,m,&t);
                   3850:         mulp(vl,gcd,t,pr);
                   3851:     } else
                   3852:         *pr = gcd;
1.150     noro     3853: }
                   3854:
                   3855: void removecont_array(P *p,int n,int full)
1.146     noro     3856: {
1.157     noro     3857:     int all_p,all_q,i;
                   3858:     Q *c;
                   3859:     P *w;
                   3860:     P t,s;
                   3861:
                   3862:     for ( all_q = 1, i = 0; i < n; i++ )
                   3863:         all_q = all_q && NUM(p[i]);
                   3864:     if ( all_q ) {
                   3865:         removecont_array_q((Q *)p,n);
                   3866:     } else {
                   3867:         c = (Q *)ALLOCA(n*sizeof(Q));
                   3868:         w = (P *)ALLOCA(n*sizeof(P));
                   3869:         for ( i = 0; i < n; i++ ) {
                   3870:             ptozp(p[i],1,&c[i],&w[i]);
                   3871:         }
                   3872:         removecont_array_q(c,n);
                   3873:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   3874:         for ( i = 0; i < n; i++ ) {
                   3875:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   3876:         }
                   3877:     }
1.146     noro     3878: }
                   3879:
                   3880: void removecont_array_q(Q *c,int n)
1.21      noro     3881: {
1.157     noro     3882:     struct oVECT v;
                   3883:     Q d0,d1,a,u,u1,gcd;
                   3884:     int i,j;
                   3885:     N qn,rn,gn;
                   3886:     Q *q,*r;
                   3887:
                   3888:     q = (Q *)ALLOCA(n*sizeof(Q));
                   3889:     r = (Q *)ALLOCA(n*sizeof(Q));
                   3890:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   3891:     igcdv_estimate(&v,&d0);
                   3892:     for ( i = 0; i < n; i++ ) {
                   3893:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   3894:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   3895:         NTOQ(rn,SGN(c[i]),r[i]);
                   3896:     }
                   3897:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   3898:     if ( i < n ) {
                   3899:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   3900:         igcdv(&v,&d1);
                   3901:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   3902:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   3903:         for ( i = 0; i < n; i++ ) {
                   3904:             mulq(a,q[i],&u);
                   3905:             if ( r[i] ) {
                   3906:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   3907:                 addq(u,u1,&q[i]);
                   3908:             } else
                   3909:                 q[i] = u;
                   3910:         }
                   3911:     }
                   3912:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     3913: }
                   3914:
1.19      noro     3915: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     3916: {
1.157     noro     3917:     NM m;
                   3918:     int c,c1;
1.1       noro     3919:
1.157     noro     3920:     if ( !p ) return;
                   3921:     if ( mul == 1 ) return;
                   3922:     if ( mod == -1 )
                   3923:         for ( m = BDY(p); m; m = NEXT(m) )
                   3924:             CM(m) = _mulsf(CM(m),mul);
                   3925:     else
                   3926:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3927:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3928:         }
1.1       noro     3929: }
                   3930:
1.146     noro     3931: void nd_mul_c_q(ND p,P mul)
1.16      noro     3932: {
1.157     noro     3933:     NM m;
                   3934:     P c;
1.16      noro     3935:
1.157     noro     3936:     if ( !p ) return;
                   3937:     if ( UNIQ(mul) ) return;
                   3938:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3939:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   3940:     }
1.16      noro     3941: }
                   3942:
1.61      noro     3943: void nd_mul_c_p(VL vl,ND p,P mul)
                   3944: {
1.157     noro     3945:     NM m;
                   3946:     P c;
1.61      noro     3947:
1.157     noro     3948:     if ( !p ) return;
                   3949:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   3950:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3951:     }
1.61      noro     3952: }
                   3953:
1.1       noro     3954: void nd_free(ND p)
                   3955: {
1.157     noro     3956:     NM t,s;
1.1       noro     3957:
1.157     noro     3958:     if ( !p ) return;
                   3959:     t = BDY(p);
                   3960:     while ( t ) {
                   3961:         s = NEXT(t);
                   3962:         FREENM(t);
                   3963:         t = s;
                   3964:     }
                   3965:     FREEND(p);
1.1       noro     3966: }
                   3967:
1.23      noro     3968: void ndv_free(NDV p)
                   3969: {
1.200     noro     3970:     GCFREE(BDY(p));
1.23      noro     3971: }
                   3972:
1.61      noro     3973: void nd_append_red(UINT *d,int i)
1.1       noro     3974: {
1.157     noro     3975:     RHist m,m0;
                   3976:     int h;
1.1       noro     3977:
1.157     noro     3978:     NEWRHist(m);
                   3979:     h = ndl_hash_value(d);
                   3980:     m->index = i;
                   3981:     ndl_copy(d,DL(m));
                   3982:     NEXT(m) = nd_red[h];
                   3983:     nd_red[h] = m;
1.1       noro     3984: }
                   3985:
1.61      noro     3986: UINT *ndv_compute_bound(NDV p)
1.1       noro     3987: {
1.157     noro     3988:     UINT *d1,*d2,*t;
                   3989:     UINT u;
                   3990:     int i,j,k,l,len,ind;
                   3991:     NMV m;
                   3992:
                   3993:     if ( !p )
                   3994:         return 0;
                   3995:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3996:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3997:     len = LEN(p);
                   3998:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3999:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     4000:         ndl_max(DL(m),d1,d2);
1.157     noro     4001:         t = d1; d1 = d2; d2 = t;
                   4002:     }
                   4003:     l = nd_nvar+31;
                   4004:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4005:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4006:         u = d1[i];
                   4007:         k = (nd_epw-1)*nd_bpe;
                   4008:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4009:             t[ind] = (u>>k)&nd_mask0;
                   4010:     }
                   4011:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4012:     return t;
1.1       noro     4013: }
                   4014:
1.99      noro     4015: UINT *nd_compute_bound(ND p)
                   4016: {
1.157     noro     4017:     UINT *d1,*d2,*t;
                   4018:     UINT u;
                   4019:     int i,j,k,l,len,ind;
                   4020:     NM m;
                   4021:
                   4022:     if ( !p )
                   4023:         return 0;
                   4024:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4025:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4026:     len = LEN(p);
                   4027:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   4028:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   4029:         ndl_lcm(DL(m),d1,d2);
                   4030:         t = d1; d1 = d2; d2 = t;
                   4031:     }
                   4032:     l = nd_nvar+31;
                   4033:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4034:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4035:         u = d1[i];
                   4036:         k = (nd_epw-1)*nd_bpe;
                   4037:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4038:             t[ind] = (u>>k)&nd_mask0;
                   4039:     }
                   4040:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4041:     return t;
1.99      noro     4042: }
                   4043:
1.157     noro     4044: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   4045: /* of a term. In this case we need additional 1 word. */
                   4046:
1.48      noro     4047: int nd_get_exporigin(struct order_spec *ord)
                   4048: {
1.157     noro     4049:     switch ( ord->id ) {
                   4050:         case 0: case 2: case 256: case 258:
                   4051:             return 1+nd_module;
                   4052:         case 1: case 257:
                   4053:             /* block order */
                   4054:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   4055:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   4056:             return ord->ord.block.length+1+nd_module;
                   4057:         case 3: case 259:
                   4058:             error("nd_get_exporigin : composite order is not supported yet.");
                   4059:     }
1.48      noro     4060: }
                   4061:
1.61      noro     4062: void nd_setup_parameters(int nvar,int max) {
1.157     noro     4063:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   4064:     struct order_pair *op;
1.48      noro     4065:
1.157     noro     4066:     nd_nvar = nvar;
                   4067:     if ( max ) {
                   4068:         /* XXX */
                   4069:         if ( do_weyl ) nd_bpe = 32;
                   4070:         else if ( max < 2 ) nd_bpe = 1;
                   4071:         else if ( max < 4 ) nd_bpe = 2;
                   4072:         else if ( max < 8 ) nd_bpe = 3;
                   4073:         else if ( max < 16 ) nd_bpe = 4;
                   4074:         else if ( max < 32 ) nd_bpe = 5;
                   4075:         else if ( max < 64 ) nd_bpe = 6;
                   4076:         else if ( max < 256 ) nd_bpe = 8;
                   4077:         else if ( max < 1024 ) nd_bpe = 10;
                   4078:         else if ( max < 65536 ) nd_bpe = 16;
                   4079:         else nd_bpe = 32;
                   4080:     }
1.203     noro     4081:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
1.201     noro     4082:         UINT t;
1.203     noro     4083:                int st;
                   4084:         int *v;
                   4085:        /* t = max(weights) */
                   4086:         t = 0;
                   4087:         if ( current_dl_weight_vector )
                   4088:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   4089:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   4090:                 if ( t < st ) t = st;
                   4091:             }
                   4092:         if ( nd_matrix )
                   4093:             for ( i = 0; i < nd_matrix_len; i++ )
                   4094:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   4095:                     if ( (st=v[j]) < 0 ) st = -st;
                   4096:                     if ( t < st ) t = st;
                   4097:                                }
                   4098:         /* i = bitsize of t */
                   4099:         for ( i = 0; t; t >>=1, i++ );
                   4100:         /* i += bitsize of nd_nvar */
                   4101:         for ( t = nd_nvar; t; t >>=1, i++);
                   4102:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   4103:         if ( (nd_bpe+i) >= 31 )
                   4104:             error("nd_setup_parameters : too large weight");
                   4105:     }
1.157     noro     4106:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   4107:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   4108:     nd_exporigin = nd_get_exporigin(nd_ord);
                   4109:     wpd = nd_exporigin+elen;
                   4110:     if ( nd_module )
                   4111:         nd_mpos = nd_exporigin-1;
                   4112:     else
                   4113:         nd_mpos = -1;
                   4114:     if ( wpd != nd_wpd ) {
                   4115:         nd_free_private_storage();
                   4116:         nd_wpd = wpd;
                   4117:     }
                   4118:     if ( nd_bpe < 32 ) {
                   4119:         nd_mask0 = (1<<nd_bpe)-1;
                   4120:     } else {
                   4121:         nd_mask0 = 0xffffffff;
                   4122:     }
                   4123:     bzero(nd_mask,sizeof(nd_mask));
                   4124:     nd_mask1 = 0;
                   4125:     for ( i = 0; i < nd_epw; i++ ) {
                   4126:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   4127:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   4128:     }
                   4129:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   4130:     nd_epos = nd_create_epos(nd_ord);
                   4131:     nd_blockmask = nd_create_blockmask(nd_ord);
                   4132:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     4133: }
                   4134:
1.103     noro     4135: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     4136: {
1.157     noro     4137:     int i,obpe,oadv,h;
                   4138:     static NM prev_nm_free_list;
                   4139:     static ND_pairs prev_ndp_free_list;
                   4140:     RHist mr0,mr;
                   4141:     RHist r;
                   4142:     RHist *old_red;
                   4143:     ND_pairs s0,s,t;
                   4144:     EPOS oepos;
                   4145:
                   4146:     obpe = nd_bpe;
                   4147:     oadv = nmv_adv;
                   4148:     oepos = nd_epos;
                   4149:     if ( obpe < 2 ) nd_bpe = 2;
                   4150:     else if ( obpe < 3 ) nd_bpe = 3;
                   4151:     else if ( obpe < 4 ) nd_bpe = 4;
                   4152:     else if ( obpe < 5 ) nd_bpe = 5;
                   4153:     else if ( obpe < 6 ) nd_bpe = 6;
                   4154:     else if ( obpe < 8 ) nd_bpe = 8;
                   4155:     else if ( obpe < 10 ) nd_bpe = 10;
                   4156:     else if ( obpe < 16 ) nd_bpe = 16;
                   4157:     else if ( obpe < 32 ) nd_bpe = 32;
                   4158:     else error("nd_reconstruct : exponent too large");
                   4159:
                   4160:     nd_setup_parameters(nd_nvar,0);
                   4161:     prev_nm_free_list = _nm_free_list;
                   4162:     prev_ndp_free_list = _ndp_free_list;
                   4163:     _nm_free_list = 0;
                   4164:     _ndp_free_list = 0;
                   4165:     for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4166:     if ( trace )
                   4167:         for ( i = nd_psn-1; i >= 0; i-- )
                   4168:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   4169:     s0 = 0;
                   4170:     for ( t = d; t; t = NEXT(t) ) {
                   4171:         NEXTND_pairs(s0,s);
                   4172:         s->i1 = t->i1;
                   4173:         s->i2 = t->i2;
                   4174:         SG(s) = SG(t);
                   4175:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4176:     }
                   4177:
                   4178:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   4179:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4180:         old_red[i] = nd_red[i];
                   4181:         nd_red[i] = 0;
                   4182:     }
                   4183:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4184:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4185:             NEWRHist(mr);
                   4186:             mr->index = r->index;
                   4187:             SG(mr) = SG(r);
                   4188:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4189:             h = ndl_hash_value(DL(mr));
                   4190:             NEXT(mr) = nd_red[h];
                   4191:             nd_red[h] = mr;
                   4192:         }
                   4193:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4194:     old_red = 0;
                   4195:     for ( i = 0; i < nd_psn; i++ ) {
                   4196:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4197:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4198:         nd_psh[i] = r;
                   4199:     }
                   4200:     if ( s0 ) NEXT(s) = 0;
                   4201:     prev_nm_free_list = 0;
                   4202:     prev_ndp_free_list = 0;
1.71      noro     4203: #if 0
1.157     noro     4204:     GC_gcollect();
1.71      noro     4205: #endif
1.157     noro     4206:     return s0;
1.1       noro     4207: }
                   4208:
1.61      noro     4209: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4210: {
1.157     noro     4211:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4212:     struct order_pair *op;
1.1       noro     4213:
1.157     noro     4214:     n = nd_nvar;
                   4215:     oepw = (sizeof(UINT)*8)/obpe;
                   4216:     omask0 = (1<<obpe)-1;
                   4217:     TD(r) = TD(d);
                   4218:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4219:     if ( nd_blockmask ) {
                   4220:         l = nd_blockmask->n;
                   4221:         op = nd_blockmask->order_pair;
                   4222:         for ( i = 1; i < nd_exporigin; i++ )
                   4223:             r[i] = d[i];
                   4224:         for ( j = 0, s = 0; j < l; j++ ) {
                   4225:             ord_l = op[j].length;
                   4226:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4227:                 ei =  GET_EXP_OLD(d,s);
                   4228:                 PUT_EXP(r,s,ei);
                   4229:             }
                   4230:         }
                   4231:     } else {
                   4232:         for ( i = 0; i < n; i++ ) {
                   4233:             ei = GET_EXP_OLD(d,i);
                   4234:             PUT_EXP(r,i,ei);
                   4235:         }
                   4236:     }
                   4237:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4238: }
1.3       noro     4239:
1.6       noro     4240: ND nd_copy(ND p)
                   4241: {
1.157     noro     4242:     NM m,mr,mr0;
                   4243:     int c,n;
                   4244:     ND r;
                   4245:
                   4246:     if ( !p )
                   4247:         return 0;
                   4248:     else {
                   4249:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4250:             NEXTNM(mr0,mr);
                   4251:             CM(mr) = CM(m);
                   4252:             ndl_copy(DL(m),DL(mr));
                   4253:         }
                   4254:         NEXT(mr) = 0;
                   4255:         MKND(NV(p),mr0,LEN(p),r);
                   4256:         SG(r) = SG(p);
                   4257:         return r;
                   4258:     }
1.6       noro     4259: }
                   4260:
1.53      noro     4261: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4262: {
1.157     noro     4263:     NM m1,m2;
                   4264:     NDV p1,p2;
                   4265:     ND t1,t2;
                   4266:     UINT *lcm;
                   4267:     P gp,tp;
1.167     noro     4268:     Q g,t,iq;
1.157     noro     4269:     int td;
1.167     noro     4270:     LIST hist;
                   4271:     NODE node;
                   4272:     DP d;
1.157     noro     4273:
                   4274:     if ( !mod && nd_demand ) {
                   4275:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4276:     } else {
                   4277:         if ( trace ) {
                   4278:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4279:         } else {
                   4280:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4281:         }
                   4282:     }
                   4283:     lcm = LCM(p);
                   4284:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4285:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4286:         FREENM(m1); return 0;
                   4287:     }
                   4288:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4289:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4290:         FREENM(m1); FREENM(m2); return 0;
                   4291:     }
                   4292:
                   4293:     if ( mod == -1 ) {
                   4294:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   4295:     } else if ( mod ) {
                   4296:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   4297:     } else if ( nd_vc ) {
                   4298:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4299:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4300:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4301:     } else {
                   4302:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4303:     }
                   4304:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4305:     *rp = nd_add(mod,t1,t2);
1.172     noro     4306:     if ( nd_gentrace ) {
1.167     noro     4307:         /* nd_tracelist is initialized */
                   4308:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4309:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4310:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4311:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4312:         nd_tracelist = node;
                   4313:     }
1.157     noro     4314:     FREENM(m1); FREENM(m2);
                   4315:     return 1;
1.11      noro     4316: }
                   4317:
1.19      noro     4318: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4319: {
1.157     noro     4320:     NMV m;
                   4321:     int c,c1,len,i;
1.11      noro     4322:
1.157     noro     4323:     if ( !p ) return;
                   4324:     len = LEN(p);
                   4325:     if ( mod == -1 )
                   4326:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4327:             CM(m) = _mulsf(CM(m),mul);
                   4328:     else
                   4329:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4330:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4331:         }
1.11      noro     4332: }
                   4333:
1.113     noro     4334: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4335: {
1.157     noro     4336:     NMV m;
                   4337:     Q c;
                   4338:     int len,i;
                   4339:
                   4340:     if ( !p ) return;
                   4341:     len = LEN(p);
                   4342:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4343:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4344:     }
1.16      noro     4345: }
                   4346:
1.55      noro     4347: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4348:     int n2,i,j,l,n,tlen;
                   4349:     UINT *d0;
                   4350:     NM *tab,*psum;
                   4351:     ND s,r;
                   4352:     NM t;
                   4353:     NMV m1;
                   4354:
                   4355:     if ( !p ) return 0;
                   4356:     n = NV(p); n2 = n>>1;
                   4357:     d0 = DL(m0);
                   4358:     l = LEN(p);
                   4359:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4360:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4361:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4362:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4363:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4364:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4365:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4366:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4367:         for ( j = 0; j < tlen; j++ ) {
                   4368:             if ( tab[j] ) {
                   4369:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4370:             }
                   4371:         }
                   4372:     }
                   4373:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4374:         if ( psum[i] ) {
                   4375:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4376:             MKND(n,psum[i],j,s);
                   4377:             r = nd_add(mod,r,s);
                   4378:         }
                   4379:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4380:     return r;
1.55      noro     4381: }
                   4382:
1.56      noro     4383: /* product of monomials */
                   4384: /* XXX block order is not handled correctly */
                   4385:
1.55      noro     4386: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4387: {
1.157     noro     4388:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4389:     UINT *d0,*d1,*d,*dt,*ctab;
                   4390:     Q *ctab_q;
                   4391:     Q q,q1;
                   4392:     UINT c0,c1,c;
                   4393:     NM *p;
                   4394:     NM m,t;
                   4395:     int mpos;
                   4396:
                   4397:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4398:     if ( !m0 || !m1 ) return;
                   4399:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4400:     if ( nd_module )
                   4401:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4402:
1.157     noro     4403:     NEWNM(m); d = DL(m);
                   4404:     if ( mod ) {
                   4405:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4406:     } else if ( nd_vc )
                   4407:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4408:        else
1.157     noro     4409:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4410:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4411:     homo = n&1 ? 1 : 0;
                   4412:     if ( homo ) {
                   4413:         /* offset of h-degree */
                   4414:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4415:         PUT_EXP(DL(m),n-1,h);
                   4416:         TD(DL(m)) = h;
                   4417:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4418:     }
                   4419:     tab[0] = m;
                   4420:     NEWNM(m); d = DL(m);
                   4421:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4422:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4423:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4424:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4425:         a += l; b += k;
                   4426:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4427:         if ( !k || !l ) {
                   4428:             for ( j = 0; j < curlen; j++ )
                   4429:                 if ( t = tab[j] ) {
                   4430:                     dt = DL(t);
                   4431:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4432:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4433:                 }
                   4434:             curlen *= k+1;
                   4435:             continue;
                   4436:         }
                   4437:         min = MIN(k,l);
                   4438:         if ( mod ) {
                   4439:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4440:             mkwcm(k,l,mod,ctab);
                   4441:         } else {
                   4442:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4443:             mkwc(k,l,ctab_q);
                   4444:         }
                   4445:         for ( j = min; j >= 0; j-- ) {
                   4446:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4447:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4448:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4449:             if ( homo ) {
                   4450:                 TD(d) = s;
                   4451:                 PUT_EXP(d,n-1,s-h);
                   4452:             } else TD(d) = h;
                   4453:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4454:             if ( mod ) c = ctab[j];
                   4455:             else q = ctab_q[j];
                   4456:             p = tab+curlen*j;
                   4457:             if ( j == 0 ) {
                   4458:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4459:                     if ( tab[u] ) {
                   4460:                         ndl_addto(DL(tab[u]),d);
                   4461:                         if ( mod ) {
                   4462:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4463:                         } else if ( nd_vc )
                   4464:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4465:                                                else {
1.157     noro     4466:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4467:                         }
                   4468:                     }
                   4469:                 }
                   4470:             } else {
                   4471:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4472:                     if ( tab[u] ) {
                   4473:                         NEWNM(t);
                   4474:                         ndl_add(DL(tab[u]),d,DL(t));
                   4475:                         if ( mod ) {
                   4476:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4477:                         } else if ( nd_vc )
1.180     noro     4478:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4479:                                                else
1.157     noro     4480:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4481:                         *p = t;
                   4482:                     }
                   4483:                 }
                   4484:             }
                   4485:         }
                   4486:         curlen *= k+1;
                   4487:     }
                   4488:     FREENM(m);
1.167     noro     4489:     if ( nd_module ) {
1.166     noro     4490:         mpos = MPOS(d1);
1.167     noro     4491:         for ( i = 0; i < tlen; i++ )
                   4492:             if ( tab[i] ) {
                   4493:                 d = DL(tab[i]);
                   4494:                 MPOS(d) = mpos;
                   4495:                 TD(d) = ndl_weight(d);
                   4496:             }
                   4497:     }
1.55      noro     4498: }
                   4499:
1.63      noro     4500: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4501: {
1.157     noro     4502:     NM mr,mr0;
                   4503:     NMV m;
                   4504:     UINT *d,*dt,*dm;
                   4505:     int c,n,td,i,c1,c2,len;
                   4506:     Q q;
                   4507:     ND r;
                   4508:
                   4509:     if ( !p ) return 0;
                   4510:     else {
                   4511:         n = NV(p); m = BDY(p);
                   4512:         d = DL(m0);
                   4513:         len = LEN(p);
                   4514:         mr0 = 0;
                   4515:         td = TD(d);
                   4516:         c = CM(m0);
                   4517:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4518:             NEXTNM(mr0,mr);
                   4519:             CM(mr) = 1;
                   4520:             ndl_add(DL(m),d,DL(mr));
                   4521:         }
                   4522:         NEXT(mr) = 0;
                   4523:         MKND(NV(p),mr0,len,r);
                   4524:         SG(r) = SG(p) + TD(d);
                   4525:         return r;
                   4526:     }
1.63      noro     4527: }
                   4528:
1.55      noro     4529: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4530: {
1.157     noro     4531:     NM mr,mr0;
                   4532:     NMV m;
                   4533:     UINT *d,*dt,*dm;
                   4534:     int c,n,td,i,c1,c2,len;
                   4535:     P q;
                   4536:     ND r;
                   4537:
                   4538:     if ( !p ) return 0;
                   4539:     else if ( do_weyl )
                   4540:         if ( mod == -1 )
                   4541:             error("ndv_mul_nm : not implemented (weyl)");
                   4542:         else
                   4543:             return weyl_ndv_mul_nm(mod,m0,p);
                   4544:     else {
                   4545:         n = NV(p); m = BDY(p);
                   4546:         d = DL(m0);
                   4547:         len = LEN(p);
                   4548:         mr0 = 0;
                   4549:         td = TD(d);
                   4550:         if ( mod == -1 ) {
                   4551:             c = CM(m0);
                   4552:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4553:                 NEXTNM(mr0,mr);
                   4554:                 CM(mr) = _mulsf(CM(m),c);
                   4555:                 ndl_add(DL(m),d,DL(mr));
                   4556:             }
                   4557:         } else if ( mod ) {
                   4558:             c = CM(m0);
                   4559:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4560:                 NEXTNM(mr0,mr);
                   4561:                 c1 = CM(m);
                   4562:                 DMAR(c1,c,0,mod,c2);
                   4563:                 CM(mr) = c2;
                   4564:                 ndl_add(DL(m),d,DL(mr));
                   4565:             }
                   4566:         } else {
                   4567:             q = CP(m0);
                   4568:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4569:                 NEXTNM(mr0,mr);
                   4570:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4571:                 ndl_add(DL(m),d,DL(mr));
                   4572:             }
                   4573:         }
                   4574:         NEXT(mr) = 0;
                   4575:         MKND(NV(p),mr0,len,r);
                   4576:         SG(r) = SG(p) + TD(d);
                   4577:         return r;
                   4578:     }
1.4       noro     4579: }
                   4580:
1.104     noro     4581: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4582: {
1.157     noro     4583:     NM mq0,mq;
                   4584:     NMV tm;
                   4585:     Q q;
                   4586:     int i,nv,sg,c,c1,c2,hindex;
                   4587:     ND p,t,r;
                   4588:     N tnm;
                   4589:
                   4590:     if ( bucket->m < 0 ) return 0;
                   4591:     else {
                   4592:         nv = NV(d);
                   4593:         mq0 = 0;
                   4594:         tm = (NMV)ALLOCA(nmv_adv);
                   4595:         while ( 1 ) {
                   4596:             hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
                   4597:             if ( hindex < 0 ) break;
                   4598:             p = bucket->body[hindex];
                   4599:             NEXTNM(mq0,mq);
                   4600:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4601:             ndl_copy(DL(mq),DL(tm));
                   4602:             if ( mod ) {
                   4603:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4604:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4605:                 CM(tm) = mod-c;
                   4606:             } else {
                   4607:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4608:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4609:                 chsgnq(CQ(mq),&CQ(tm));
                   4610:             }
                   4611:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4612:             bucket->body[hindex] = nd_remove_head(p);
                   4613:             t = nd_remove_head(t);
                   4614:             add_pbucket(mod,bucket,t);
                   4615:         }
                   4616:         if ( !mq0 )
                   4617:             r = 0;
                   4618:         else {
                   4619:             NEXT(mq) = 0;
                   4620:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4621:             MKND(nv,mq0,i,r);
                   4622:             /* XXX */
                   4623:             SG(r) = HTD(r);
                   4624:         }
                   4625:         return r;
                   4626:     }
1.99      noro     4627: }
                   4628:
1.43      noro     4629: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4630: {
1.157     noro     4631:     NMV m,mr,mr0,t;
                   4632:     int len,i,k;
1.11      noro     4633:
1.157     noro     4634:     if ( !p ) return;
                   4635:     m = BDY(p); len = LEN(p);
                   4636:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4637:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4638:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4639:     t = (NMV)ALLOCA(nmv_adv);
                   4640:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4641:         CQ(t) = CQ(m);
                   4642:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4643:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4644:         CQ(mr) = CQ(t);
                   4645:         ndl_copy(DL(t),DL(mr));
                   4646:     }
                   4647:     BDY(p) = mr0;
1.61      noro     4648: }
                   4649:
                   4650: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4651: {
1.157     noro     4652:     NMV m,mr,mr0;
                   4653:     int len,i;
                   4654:     NDV r;
                   4655:
                   4656:     if ( !p ) return 0;
                   4657:     m = BDY(p); len = LEN(p);
                   4658:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   4659:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   4660:         ndl_zero(DL(mr));
                   4661:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   4662:         CQ(mr) = CQ(m);
                   4663:     }
                   4664:     MKNDV(NV(p),mr0,len,r);
                   4665:     SG(r) = SG(p);
                   4666:     return r;
1.11      noro     4667: }
                   4668:
1.61      noro     4669: /* duplicate p */
                   4670:
                   4671: NDV ndv_dup(int mod,NDV p)
1.3       noro     4672: {
1.157     noro     4673:     NDV d;
                   4674:     NMV t,m,m0;
                   4675:     int i,len;
                   4676:
                   4677:     if ( !p ) return 0;
                   4678:     len = LEN(p);
                   4679:     m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   4680:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   4681:         ndl_copy(DL(t),DL(m));
                   4682:         CQ(m) = CQ(t);
                   4683:     }
                   4684:     MKNDV(NV(p),m0,len,d);
                   4685:     SG(d) = SG(p);
                   4686:     return d;
1.23      noro     4687: }
                   4688:
1.63      noro     4689: ND nd_dup(ND p)
                   4690: {
1.157     noro     4691:     ND d;
                   4692:     NM t,m,m0;
1.63      noro     4693:
1.157     noro     4694:     if ( !p ) return 0;
                   4695:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   4696:         NEXTNM(m0,m);
                   4697:         ndl_copy(DL(t),DL(m));
                   4698:         CQ(m) = CQ(t);
                   4699:     }
                   4700:     if ( m0 ) NEXT(m) = 0;
                   4701:     MKND(NV(p),m0,LEN(p),d);
                   4702:     SG(d) = SG(p);
                   4703:     return d;
1.63      noro     4704: }
                   4705:
1.61      noro     4706: /* XXX if p->len == 0 then it represents 0 */
                   4707:
                   4708: void ndv_mod(int mod,NDV p)
                   4709: {
1.157     noro     4710:     NMV t,d;
                   4711:     int r,s,u;
                   4712:     int i,len,dlen;
                   4713:     P cp;
                   4714:     Q c;
                   4715:     Obj gfs;
                   4716:
                   4717:     if ( !p ) return;
                   4718:     len = LEN(p);
                   4719:     dlen = 0;
                   4720:     if ( mod == -1 )
                   4721:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4722:             simp_ff((Obj)CP(t),&gfs);
                   4723:             r = FTOIF(CONT((GFS)gfs));
                   4724:             CM(d) = r;
                   4725:             ndl_copy(DL(t),DL(d));
                   4726:             NMV_ADV(d);
                   4727:             dlen++;
                   4728:         }
                   4729:     else
                   4730:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   4731:             if ( nd_vc ) {
                   4732:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   4733:                 c = (Q)cp;
                   4734:             } else
                   4735:                 c = CQ(t);
                   4736:             r = rem(NM(c),mod);
                   4737:             if ( r ) {
                   4738:                 if ( SGN(c) < 0 )
                   4739:                     r = mod-r;
                   4740:                 if ( DN(c) ) {
                   4741:                     s = rem(DN(c),mod);
                   4742:                     if ( !s )
                   4743:                         error("ndv_mod : division by 0");
                   4744:                     s = invm(s,mod);
                   4745:                     DMAR(r,s,0,mod,u); r = u;
                   4746:                 }
                   4747:                 CM(d) = r;
                   4748:                 ndl_copy(DL(t),DL(d));
                   4749:                 NMV_ADV(d);
                   4750:                 dlen++;
                   4751:             }
                   4752:         }
                   4753:     LEN(p) = dlen;
1.61      noro     4754: }
                   4755:
                   4756: NDV ptondv(VL vl,VL dvl,P p)
                   4757: {
1.157     noro     4758:     ND nd;
                   4759:
                   4760:     nd = ptond(vl,dvl,p);
                   4761:     return ndtondv(0,nd);
                   4762: }
1.61      noro     4763:
1.157     noro     4764: void pltozpl(LIST l,Q *cont,LIST *pp)
                   4765: {
                   4766:     NODE nd,nd1;
                   4767:     int n;
                   4768:     P *pl;
                   4769:     Q *cl;
                   4770:     int i;
                   4771:     P dmy;
                   4772:     Q dvr;
                   4773:     LIST r;
                   4774:
                   4775:     nd = BDY(l); n = length(nd);
                   4776:     pl = (P *)ALLOCA(n*sizeof(P));
                   4777:     cl = (Q *)ALLOCA(n*sizeof(P));
                   4778:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   4779:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   4780:     qltozl(cl,n,&dvr);
                   4781:     nd = BDY(l);
                   4782:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   4783:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   4784:     }
                   4785:     nd = 0;
                   4786:     for ( i = n-1; i >= 0; i-- ) {
                   4787:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   4788:     }
                   4789:     MKLIST(r,nd);
                   4790:     *pp = r;
                   4791: }
                   4792:
                   4793: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   4794:
                   4795: NDV pltondv(VL vl,VL dvl,LIST p)
                   4796: {
                   4797:     int i;
                   4798:     NODE t;
                   4799:     ND r,ri;
                   4800:     NM m;
                   4801:
                   4802:     if ( !nd_module ) error("pltond : module order must be set");
                   4803:     r = 0;
                   4804:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   4805:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     4806:         if ( ri )
                   4807:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     4808:                 MPOS(DL(m)) = i;
                   4809:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     4810:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4811:             }
1.157     noro     4812:         r = nd_add(0,r,ri);
                   4813:     }
                   4814:     return ndtondv(0,r);
1.61      noro     4815: }
                   4816:
                   4817: ND ptond(VL vl,VL dvl,P p)
1.23      noro     4818: {
1.157     noro     4819:     int n,i,j,k,e;
                   4820:     VL tvl;
                   4821:     V v;
                   4822:     DCP dc;
                   4823:     DCP *w;
                   4824:     ND r,s,t,u;
                   4825:     P x;
                   4826:     int c;
                   4827:     UINT *d;
                   4828:     NM m,m0;
                   4829:
                   4830:     if ( !p )
                   4831:         return 0;
                   4832:     else if ( NUM(p) ) {
                   4833:         NEWNM(m);
                   4834:         ndl_zero(DL(m));
                   4835:         CQ(m) = (Q)p;
                   4836:         NEXT(m) = 0;
                   4837:         MKND(nd_nvar,m,1,r);
                   4838:         SG(r) = 0;
                   4839:         return r;
                   4840:     } else {
                   4841:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   4842:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   4843:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   4844:         for ( i = 0, tvl = dvl, v = VR(p);
                   4845:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   4846:         if ( !tvl ) {
                   4847:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   4848:                 t = ptond(vl,dvl,COEF(w[j]));
                   4849:                 pwrp(vl,x,DEG(w[j]),&p);
                   4850:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   4851:             }
                   4852:             return s;
                   4853:         } else {
                   4854:             NEWNM(m0); d = DL(m0);
                   4855:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   4856:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   4857:                 TD(d) = MUL_WEIGHT(e,i);
                   4858:                 if ( nd_blockmask) ndl_weight_mask(d);
                   4859:                 if ( nd_module ) MPOS(d) = 0;
                   4860:                 t = ptond(vl,dvl,COEF(w[j]));
                   4861:                 for ( m = BDY(t); m; m = NEXT(m) )
                   4862:                     ndl_addto(DL(m),d);
                   4863:                 SG(t) += TD(d);
                   4864:                 s = nd_add(0,s,t);
                   4865:             }
                   4866:             FREENM(m0);
                   4867:             return s;
                   4868:         }
                   4869:     }
1.61      noro     4870: }
                   4871:
                   4872: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   4873: {
1.157     noro     4874:     VL tvl;
                   4875:     int len,n,j,i,e;
                   4876:     NMV m;
                   4877:     Q q;
                   4878:     P c;
                   4879:     UINT *d;
                   4880:     P s,r,u,t,w;
                   4881:     GFS gfs;
                   4882:
                   4883:     if ( !p ) return 0;
                   4884:     else {
                   4885:         len = LEN(p);
                   4886:         n = NV(p);
                   4887:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4888:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   4889:             if ( mod == -1 ) {
                   4890:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4891:             } else if ( mod ) {
                   4892:                 STOQ(CM(m),q); c = (P)q;
                   4893:             } else
                   4894:                 c = CP(m);
                   4895:             d = DL(m);
                   4896:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4897:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4898:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4899:             }
                   4900:             addp(vl,s,t,&u); s = u;
                   4901:         }
                   4902:         return s;
                   4903:     }
                   4904: }
                   4905:
                   4906: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   4907: {
                   4908:     VL tvl;
                   4909:     int len,n,j,i,e;
                   4910:     NMV m;
                   4911:     Q q;
                   4912:     P c;
                   4913:     UINT *d;
                   4914:     P s,r,u,t,w;
                   4915:     GFS gfs;
                   4916:     P *a;
                   4917:     LIST l;
                   4918:     NODE nd,nd1;
                   4919:
                   4920:     if ( !p ) return 0;
                   4921:     else {
                   4922:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   4923:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   4924:         len = LEN(p);
                   4925:         n = NV(p);
                   4926:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   4927:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   4928:             if ( mod == -1 ) {
                   4929:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   4930:             } else if ( mod ) {
                   4931:                 STOQ(CM(m),q); c = (P)q;
                   4932:             } else
                   4933:                 c = CP(m);
                   4934:             d = DL(m);
                   4935:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   4936:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   4937:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   4938:             }
                   4939:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   4940:         }
                   4941:         nd = 0;
                   4942:         for ( i = rank; i > 0; i-- ) {
                   4943:             MKNODE(nd1,a[i],nd); nd = nd1;
                   4944:         }
                   4945:         MKLIST(l,nd);
                   4946:         return l;
                   4947:     }
1.3       noro     4948: }
                   4949:
1.61      noro     4950: NDV ndtondv(int mod,ND p)
1.11      noro     4951: {
1.157     noro     4952:     NDV d;
                   4953:     NMV m,m0;
                   4954:     NM t;
                   4955:     int i,len;
                   4956:
                   4957:     if ( !p ) return 0;
                   4958:     len = LEN(p);
                   4959:     if ( mod )
1.200     noro     4960:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     4961:     else
                   4962:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     4963: #if 0
1.157     noro     4964:     ndv_alloc += nmv_adv*len;
1.103     noro     4965: #endif
1.157     noro     4966:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   4967:         ndl_copy(DL(t),DL(m));
                   4968:         CQ(m) = CQ(t);
                   4969:     }
                   4970:     MKNDV(NV(p),m0,len,d);
                   4971:     SG(d) = SG(p);
                   4972:     return d;
1.11      noro     4973: }
                   4974:
1.61      noro     4975: ND ndvtond(int mod,NDV p)
1.11      noro     4976: {
1.157     noro     4977:     ND d;
                   4978:     NM m,m0;
                   4979:     NMV t;
                   4980:     int i,len;
                   4981:
                   4982:     if ( !p ) return 0;
                   4983:     m0 = 0;
                   4984:     len = p->len;
                   4985:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   4986:         NEXTNM(m0,m);
                   4987:         ndl_copy(DL(t),DL(m));
                   4988:         CQ(m) = CQ(t);
                   4989:     }
                   4990:     NEXT(m) = 0;
                   4991:     MKND(NV(p),m0,len,d);
                   4992:     SG(d) = SG(p);
                   4993:     return d;
1.11      noro     4994: }
                   4995:
1.198     noro     4996: DP ndvtodp(int mod,NDV p)
                   4997: {
                   4998:     MP m,m0;
                   4999:        DP d;
                   5000:     NMV t;
                   5001:     int i,len;
                   5002:
                   5003:     if ( !p ) return 0;
                   5004:     m0 = 0;
                   5005:     len = p->len;
                   5006:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5007:         NEXTMP(m0,m);
                   5008:        m->dl = ndltodl(nd_nvar,DL(t));
                   5009:        m->c = ndctop(mod,t->c);
                   5010:     }
                   5011:     NEXT(m) = 0;
                   5012:        MKDP(nd_nvar,m0,d);
                   5013:     SG(d) = SG(p);
                   5014:     return d;
                   5015: }
                   5016:
1.204     noro     5017: DP ndtodp(int mod,ND p)
                   5018: {
                   5019:     MP m,m0;
                   5020:        DP d;
                   5021:     NM t;
                   5022:     int i,len;
                   5023:
                   5024:     if ( !p ) return 0;
                   5025:     m0 = 0;
                   5026:     len = p->len;
                   5027:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   5028:         NEXTMP(m0,m);
                   5029:        m->dl = ndltodl(nd_nvar,DL(t));
                   5030:        m->c = ndctop(mod,t->c);
                   5031:     }
                   5032:     NEXT(m) = 0;
                   5033:        MKDP(nd_nvar,m0,d);
                   5034:     SG(d) = SG(p);
                   5035:     return d;
                   5036: }
                   5037:
1.3       noro     5038: void ndv_print(NDV p)
                   5039: {
1.157     noro     5040:     NMV m;
                   5041:     int i,len;
1.3       noro     5042:
1.157     noro     5043:     if ( !p ) printf("0\n");
                   5044:     else {
                   5045:         len = LEN(p);
                   5046:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5047:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   5048:             else printf("+%d*",CM(m));
                   5049:             ndl_print(DL(m));
                   5050:         }
                   5051:         printf("\n");
                   5052:     }
1.16      noro     5053: }
                   5054:
1.113     noro     5055: void ndv_print_q(NDV p)
1.16      noro     5056: {
1.157     noro     5057:     NMV m;
                   5058:     int i,len;
1.16      noro     5059:
1.157     noro     5060:     if ( !p ) printf("0\n");
                   5061:     else {
                   5062:         len = LEN(p);
                   5063:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5064:             printf("+");
                   5065:             printexpr(CO,(Obj)CQ(m));
                   5066:             printf("*");
                   5067:             ndl_print(DL(m));
                   5068:         }
                   5069:         printf("\n");
                   5070:     }
1.25      noro     5071: }
                   5072:
1.167     noro     5073: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     5074: {
1.157     noro     5075:     int len,i,j;
1.167     noro     5076:     NDVI w;
1.157     noro     5077:     NODE t,t0;
                   5078:
                   5079:     len = length(x);
1.167     noro     5080:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   5081:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   5082:         w[i].p = BDY(t); w[i].i = perm[i];
                   5083:     }
1.157     noro     5084:     for ( i = 0; i < len; i++ ) {
                   5085:         for ( j = 0; j < i; j++ ) {
1.167     noro     5086:             if ( w[i].p && w[j].p )
                   5087:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   5088:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     5089:         }
                   5090:     }
1.167     noro     5091:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   5092:         if ( w[i].p ) {
                   5093:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   5094:             perm[j++] = w[i].i;
                   5095:         }
1.157     noro     5096:     }
                   5097:     NEXT(t) = 0; x = t0;
                   5098:     return x;
1.11      noro     5099: }
1.32      noro     5100:
1.43      noro     5101: /* XXX incomplete */
                   5102:
1.32      noro     5103: void nd_init_ord(struct order_spec *ord)
                   5104: {
1.157     noro     5105:     nd_module = (ord->id >= 256);
1.203     noro     5106:        nd_matrix = 0;
                   5107:        nd_matrix_len = 0;
1.157     noro     5108:     switch ( ord->id ) {
                   5109:         case 0:
                   5110:             switch ( ord->ord.simple ) {
                   5111:                 case 0:
                   5112:                     nd_dcomp = 1;
                   5113:                     nd_isrlex = 1;
                   5114:                     break;
                   5115:                 case 1:
                   5116:                     nd_dcomp = 1;
                   5117:                     nd_isrlex = 0;
                   5118:                     break;
                   5119:                 case 2:
                   5120:                     nd_dcomp = 0;
                   5121:                     nd_isrlex = 0;
                   5122:                     ndl_compare_function = ndl_lex_compare;
                   5123:                     break;
                   5124:                 case 11:
                   5125:                     /* XXX */
                   5126:                     nd_dcomp = 0;
                   5127:                     nd_isrlex = 1;
                   5128:                     ndl_compare_function = ndl_ww_lex_compare;
                   5129:                     break;
                   5130:                 default:
                   5131:                     error("nd_gr : unsupported order");
                   5132:             }
                   5133:             break;
                   5134:         case 1:
                   5135:             /* block order */
                   5136:             /* XXX */
                   5137:             nd_dcomp = -1;
                   5138:             nd_isrlex = 0;
                   5139:             ndl_compare_function = ndl_block_compare;
                   5140:             break;
                   5141:         case 2:
                   5142:             /* matrix order */
                   5143:             /* XXX */
                   5144:             nd_dcomp = -1;
                   5145:             nd_isrlex = 0;
                   5146:             nd_matrix_len = ord->ord.matrix.row;
                   5147:             nd_matrix = ord->ord.matrix.matrix;
                   5148:             ndl_compare_function = ndl_matrix_compare;
                   5149:             break;
                   5150:         case 3:
                   5151:             /* composite order */
                   5152:             nd_dcomp = -1;
                   5153:             nd_isrlex = 0;
                   5154:             nd_worb_len = ord->ord.composite.length;
                   5155:             nd_worb = ord->ord.composite.w_or_b;
                   5156:             ndl_compare_function = ndl_composite_compare;
                   5157:             break;
                   5158:
                   5159:         /* module order */
                   5160:         case 256:
1.160     noro     5161:             nd_ispot = ord->ispot;
1.174     noro     5162:             nd_pot_nelim = ord->pot_nelim;
1.157     noro     5163:             nd_dcomp = -1;
                   5164:             switch ( ord->ord.simple ) {
                   5165:                 case 0:
1.167     noro     5166:                     nd_isrlex = 1;
1.157     noro     5167:                     ndl_compare_function = ndl_module_grlex_compare;
                   5168:                     break;
                   5169:                 case 1:
1.167     noro     5170:                     nd_isrlex = 0;
1.157     noro     5171:                     ndl_compare_function = ndl_module_glex_compare;
                   5172:                     break;
                   5173:                 case 2:
1.167     noro     5174:                     nd_isrlex = 0;
1.157     noro     5175:                     ndl_compare_function = ndl_module_lex_compare;
                   5176:                     break;
                   5177:                 default:
                   5178:                     error("nd_gr : unsupported order");
                   5179:             }
                   5180:             break;
                   5181:         case 257:
                   5182:             /* block order */
1.174     noro     5183:             nd_ispot = ord->ispot;
                   5184:             nd_pot_nelim = ord->pot_nelim;
                   5185:             nd_dcomp = -1;
                   5186:             nd_isrlex = 0;
1.157     noro     5187:             ndl_compare_function = ndl_module_block_compare;
                   5188:             break;
                   5189:         case 258:
                   5190:             /* matrix order */
1.174     noro     5191:             nd_ispot = ord->ispot;
                   5192:             nd_pot_nelim = ord->pot_nelim;
                   5193:             nd_dcomp = -1;
                   5194:             nd_isrlex = 0;
1.157     noro     5195:             nd_matrix_len = ord->ord.matrix.row;
                   5196:             nd_matrix = ord->ord.matrix.matrix;
                   5197:             ndl_compare_function = ndl_module_matrix_compare;
                   5198:             break;
                   5199:         case 259:
                   5200:             /* composite order */
1.174     noro     5201:             nd_ispot = ord->ispot;
                   5202:             nd_pot_nelim = ord->pot_nelim;
                   5203:             nd_dcomp = -1;
                   5204:             nd_isrlex = 0;
1.157     noro     5205:             nd_worb_len = ord->ord.composite.length;
                   5206:             nd_worb = ord->ord.composite.w_or_b;
                   5207:             ndl_compare_function = ndl_module_composite_compare;
                   5208:             break;
                   5209:     }
                   5210:     nd_ord = ord;
1.32      noro     5211: }
                   5212:
1.43      noro     5213: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5214: {
1.157     noro     5215:     int n,i,j,s,l;
                   5216:     UINT *t;
                   5217:     BlockMask bm;
                   5218:
                   5219:     /* we only create mask table for block order */
1.164     noro     5220:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5221:         return 0;
                   5222:     n = ord->ord.block.length;
                   5223:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5224:     bm->n = n;
                   5225:     bm->order_pair = ord->ord.block.order_pair;
                   5226:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5227:     for ( i = 0, s = 0; i < n; i++ ) {
                   5228:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5229:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5230:         l = bm->order_pair[i].length;
                   5231:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5232:     }
                   5233:     return bm;
1.57      noro     5234: }
                   5235:
                   5236: EPOS nd_create_epos(struct order_spec *ord)
                   5237: {
1.157     noro     5238:     int i,j,l,s,ord_l,ord_o;
                   5239:     EPOS epos;
                   5240:     struct order_pair *op;
                   5241:
                   5242:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5243:     switch ( ord->id ) {
1.164     noro     5244:         case 0: case 256:
1.157     noro     5245:             if ( nd_isrlex ) {
                   5246:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5247:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5248:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5249:                 }
                   5250:             } else {
                   5251:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5252:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5253:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5254:                 }
                   5255:             }
                   5256:             break;
1.164     noro     5257:         case 1: case 257:
1.157     noro     5258:             /* block order */
                   5259:             l = ord->ord.block.length;
                   5260:             op = ord->ord.block.order_pair;
                   5261:             for ( j = 0, s = 0; j < l; j++ ) {
                   5262:                 ord_o = op[j].order;
                   5263:                 ord_l = op[j].length;
                   5264:                 if ( !ord_o )
                   5265:                     for ( i = 0; i < ord_l; i++ ) {
                   5266:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5267:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5268:                     }
                   5269:                 else
                   5270:                     for ( i = 0; i < ord_l; i++ ) {
                   5271:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5272:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5273:                     }
                   5274:                 s += ord_l;
                   5275:             }
                   5276:             break;
                   5277:         case 2:
                   5278:             /* matrix order */
                   5279:         case 3:
                   5280:             /* composite order */
1.167     noro     5281:         default:
1.157     noro     5282:             for ( i = 0; i < nd_nvar; i++ ) {
                   5283:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5284:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5285:             }
                   5286:             break;
                   5287:     }
                   5288:     return epos;
1.43      noro     5289: }
1.59      noro     5290:
                   5291: /* external interface */
                   5292:
1.191     noro     5293: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5294: {
1.157     noro     5295:     NODE t,in0,in;
1.191     noro     5296:     ND ndf,nf;
                   5297:     NDV ndvf;
1.157     noro     5298:     VL vv,tv;
1.191     noro     5299:     int stat,nvar,max,mrank;
1.157     noro     5300:     union oNDC dn;
                   5301:     Q cont;
                   5302:     P pp;
1.191     noro     5303:     LIST ppl;
1.157     noro     5304:
                   5305:     if ( !f ) {
                   5306:         *rp = 0;
                   5307:         return;
                   5308:     }
                   5309:     pltovl(v,&vv);
                   5310:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5311:
1.191     noro     5312:     /* max=65536 implies nd_bpe=32 */
                   5313:     max = 65536;
1.157     noro     5314:
1.191     noro     5315:        nd_module = 0;
                   5316:        /* nd_module will be set if ord is a module ordering */
1.157     noro     5317:     nd_init_ord(ord);
                   5318:     nd_setup_parameters(nvar,max);
1.191     noro     5319:     if ( nd_module && OID(f) != O_LIST )
                   5320:         error("nd_nf_p : the first argument must be a list");
                   5321:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5322:     /* conversion to ndv */
                   5323:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5324:         NEXTNODE(in0,in);
1.191     noro     5325:         if ( nd_module ) {
                   5326:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5327:                || length(BDY((LIST)BDY(t))) != mrank )
                   5328:               error("nd_nf_p : inconsistent basis element");
                   5329:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5330:           else ppl = (LIST)BDY(t);
                   5331:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5332:         } else {
                   5333:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5334:           else pp = (P)BDY(t);
                   5335:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5336:         }
1.157     noro     5337:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5338:     }
1.191     noro     5339:     if ( in0 ) NEXT(in) = 0;
                   5340:
                   5341:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5342:     else ndvf = ptondv(CO,vv,(P)f);
                   5343:     if ( m ) ndv_mod(m,ndvf);
                   5344:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5345:
                   5346:     /* dont sort, dont removecont */
                   5347:     ndv_setup(m,0,in0,1,1);
                   5348:     nd_scale=2;
1.191     noro     5349:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5350:     if ( !stat )
                   5351:         error("nd_nf_p : exponent too large");
                   5352:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5353:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5354: }
                   5355:
                   5356: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5357: {
1.157     noro     5358:     NM m;
                   5359:     UINT *t,*s;
                   5360:     int i;
                   5361:
                   5362:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5363:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5364:         t = DL(m);
                   5365:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5366:         r[i] = CM(m);
                   5367:     }
                   5368:     for ( i = 0; !r[i]; i++ );
                   5369:     return i;
1.63      noro     5370: }
                   5371:
1.113     noro     5372: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5373: {
1.157     noro     5374:     NM m;
                   5375:     UINT *t,*s;
                   5376:     int i;
                   5377:
                   5378:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5379:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5380:         t = DL(m);
                   5381:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5382:         r[i] = CQ(m);
                   5383:     }
                   5384:     for ( i = 0; !r[i]; i++ );
                   5385:     return i;
1.74      noro     5386: }
                   5387:
1.129     noro     5388: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5389: {
1.157     noro     5390:     NM m;
                   5391:     NMV mr;
                   5392:     UINT *d,*t,*s;
                   5393:     NDV p;
                   5394:     int i,j,len;
                   5395:     Q *r;
                   5396:
                   5397:     m = pair->mul;
                   5398:     d = DL(m);
                   5399:     p = nd_ps[pair->index];
                   5400:     len = LEN(p);
                   5401:     r = (Q *)CALLOC(n,sizeof(Q));
                   5402:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5403:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5404:         ndl_add(d,DL(mr),t);
                   5405:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5406:         r[i] = CQ(mr);
                   5407:     }
                   5408:     return r;
1.129     noro     5409: }
                   5410:
1.210     noro     5411: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
1.64      noro     5412: {
1.157     noro     5413:     NM m;
                   5414:     NMV mr;
                   5415:     UINT *d,*t,*s;
                   5416:     NDV p;
                   5417:     unsigned char *ivc;
                   5418:     unsigned short *ivs;
                   5419:     UINT *v,*ivi,*s0v;
1.210     noro     5420:     int i,j,len,prev,diff,cdiff,h;
1.157     noro     5421:     IndArray r;
1.198     noro     5422: struct oEGT eg0,eg1;
1.157     noro     5423:
                   5424:     m = pair->mul;
                   5425:     d = DL(m);
                   5426:     p = nd_ps[pair->index];
                   5427:     len = LEN(p);
                   5428:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5429:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.198     noro     5430: get_eg(&eg0);
1.157     noro     5431:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5432:         ndl_add(d,DL(mr),t);
1.210     noro     5433:                h = ndl_hash_value(t);
                   5434:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.157     noro     5435:         v[j] = i;
                   5436:     }
1.198     noro     5437: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5438:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5439:     r->head = v[0];
                   5440:     diff = 0;
                   5441:     for ( i = 1; i < len; i++ ) {
                   5442:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5443:     }
                   5444:     if ( diff < 256 ) {
                   5445:         r->width = 1;
                   5446:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5447:         r->index.c = ivc;
                   5448:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5449:     } else if ( diff < 65536 ) {
                   5450:         r->width = 2;
                   5451:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5452:         r->index.s = ivs;
                   5453:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5454:     } else {
                   5455:         r->width = 4;
                   5456:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5457:         r->index.i = ivi;
                   5458:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5459:     }
                   5460:     return r;
1.64      noro     5461: }
                   5462:
1.135     noro     5463: int compress_array(Q *svect,Q *cvect,int n)
                   5464: {
1.157     noro     5465:     int i,j;
1.135     noro     5466:
1.157     noro     5467:     for ( i = j = 0; i < n; i++ )
                   5468:         if ( svect[i] ) cvect[j++] = svect[i];
                   5469:     return j;
1.135     noro     5470: }
                   5471:
                   5472: void expand_array(Q *svect,Q *cvect,int n)
                   5473: {
1.157     noro     5474:     int i,j;
1.135     noro     5475:
1.157     noro     5476:     for ( i = j = 0; j < n;  i++  )
                   5477:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5478: }
                   5479:
1.133     noro     5480: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5481: {
1.157     noro     5482:     int i,j,k,len,pos,prev,nz;
                   5483:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5484:     IndArray ivect;
                   5485:     unsigned char *ivc;
                   5486:     unsigned short *ivs;
                   5487:     unsigned int *ivi;
                   5488:     NDV redv;
                   5489:     NMV mr;
                   5490:     NODE rp;
                   5491:     int maxrs;
                   5492:     double hmag;
                   5493:     Q *cvect;
                   5494:
                   5495:     maxrs = 0;
                   5496:     for ( i = 0; i < col && !svect[i]; i++ );
                   5497:     if ( i == col ) return maxrs;
                   5498:     hmag = p_mag((P)svect[i])*nd_scale;
                   5499:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5500:     for ( i = 0; i < nred; i++ ) {
                   5501:         ivect = imat[i];
                   5502:         k = ivect->head;
                   5503:         if ( svect[k] ) {
                   5504:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5505:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   5506:             len = LEN(redv); mr = BDY(redv);
                   5507:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5508:             chsgnq(cs,&mcs);
                   5509:             if ( !UNIQ(cr) ) {
                   5510:                 for ( j = 0; j < col; j++ ) {
                   5511:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5512:                 }
                   5513:             }
                   5514:             svect[k] = 0; prev = k;
                   5515:             switch ( ivect->width ) {
                   5516:                 case 1:
                   5517:                     ivc = ivect->index.c;
                   5518:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5519:                         pos = prev+ivc[j]; prev = pos;
                   5520:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5521:                     }
                   5522:                     break;
                   5523:                 case 2:
                   5524:                     ivs = ivect->index.s;
                   5525:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5526:                         pos = prev+ivs[j]; prev = pos;
                   5527:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5528:                     }
                   5529:                     break;
                   5530:                 case 4:
                   5531:                     ivi = ivect->index.i;
                   5532:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5533:                         pos = prev+ivi[j]; prev = pos;
                   5534:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5535:                     }
                   5536:                     break;
                   5537:             }
                   5538:             for ( j = k+1; j < col && !svect[j]; j++ );
                   5539:             if ( j == col ) break;
                   5540:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   5541:                 nz = compress_array(svect,cvect,col);
                   5542:                 removecont_array((P *)cvect,nz,1);
                   5543:                 expand_array(svect,cvect,nz);
                   5544:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   5545:             }
                   5546:         }
                   5547:     }
                   5548:     nz = compress_array(svect,cvect,col);
                   5549:     removecont_array((P *)cvect,nz,1);
                   5550:     expand_array(svect,cvect,nz);
                   5551:     if ( DP_Print ) {
                   5552:         fprintf(asir_out,"-"); fflush(asir_out);
                   5553:     }
                   5554:     return maxrs;
1.107     noro     5555: }
                   5556:
1.76      noro     5557: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     5558: {
1.157     noro     5559:     int i,j,k,len,pos,prev;
                   5560:     UINT c,c1,c2,c3,up,lo,dmy;
                   5561:     IndArray ivect;
                   5562:     unsigned char *ivc;
                   5563:     unsigned short *ivs;
                   5564:     unsigned int *ivi;
                   5565:     NDV redv;
                   5566:     NMV mr;
                   5567:     NODE rp;
                   5568:     int maxrs;
                   5569:
                   5570:     maxrs = 0;
                   5571:     for ( i = 0; i < nred; i++ ) {
                   5572:         ivect = imat[i];
                   5573:         k = ivect->head; svect[k] %= m;
                   5574:         if ( c = svect[k] ) {
                   5575:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5576:             c = m-c; redv = nd_ps[rp0[i]->index];
                   5577:             len = LEN(redv); mr = BDY(redv);
                   5578:             svect[k] = 0; prev = k;
                   5579:             switch ( ivect->width ) {
                   5580:                 case 1:
                   5581:                     ivc = ivect->index.c;
                   5582:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5583:                         pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   5584:                         prev = pos;
                   5585:                         DMA(c1,c,c2,up,lo);
                   5586:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5587:                         } else svect[pos] = lo;
                   5588:                     }
                   5589:                     break;
                   5590:                 case 2:
                   5591:                     ivs = ivect->index.s;
                   5592:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5593:                         pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   5594:                         prev = pos;
                   5595:                         DMA(c1,c,c2,up,lo);
                   5596:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5597:                         } else svect[pos] = lo;
                   5598:                     }
                   5599:                     break;
                   5600:                 case 4:
                   5601:                     ivi = ivect->index.i;
                   5602:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5603:                         pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   5604:                         prev = pos;
                   5605:                         DMA(c1,c,c2,up,lo);
                   5606:                         if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   5607:                         } else svect[pos] = lo;
                   5608:                     }
                   5609:                     break;
                   5610:             }
                   5611:         }
                   5612:     }
                   5613:     for ( i = 0; i < col; i++ )
                   5614:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   5615:     return maxrs;
1.65      noro     5616: }
                   5617:
1.76      noro     5618: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     5619: {
1.157     noro     5620:     int i,j,k,len,pos,prev;
                   5621:     UINT c,c1,c2,c3,up,lo,dmy;
                   5622:     IndArray ivect;
                   5623:     unsigned char *ivc;
                   5624:     unsigned short *ivs;
                   5625:     unsigned int *ivi;
                   5626:     NDV redv;
                   5627:     NMV mr;
                   5628:     NODE rp;
                   5629:     int maxrs;
                   5630:
                   5631:     maxrs = 0;
                   5632:     for ( i = 0; i < nred; i++ ) {
                   5633:         ivect = imat[i];
                   5634:         k = ivect->head; svect[k] %= m;
                   5635:         if ( c = svect[k] ) {
                   5636:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   5637:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   5638:             len = LEN(redv); mr = BDY(redv);
                   5639:             svect[k] = 0; prev = k;
                   5640:             switch ( ivect->width ) {
                   5641:                 case 1:
                   5642:                     ivc = ivect->index.c;
                   5643:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5644:                         pos = prev+ivc[j]; prev = pos;
                   5645:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5646:                     }
                   5647:                     break;
                   5648:                 case 2:
                   5649:                     ivs = ivect->index.s;
                   5650:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5651:                         pos = prev+ivs[j]; prev = pos;
                   5652:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5653:                     }
                   5654:                     break;
                   5655:                 case 4:
                   5656:                     ivi = ivect->index.i;
                   5657:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5658:                         pos = prev+ivi[j]; prev = pos;
                   5659:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   5660:                     }
                   5661:                     break;
                   5662:             }
                   5663:         }
                   5664:     }
                   5665:     return maxrs;
1.72      noro     5666: }
                   5667:
1.65      noro     5668: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   5669: {
1.157     noro     5670:     int j,k,len;
                   5671:     UINT *p;
                   5672:     UINT c;
                   5673:     NDV r;
                   5674:     NMV mr0,mr;
                   5675:
                   5676:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5677:     if ( !len ) return 0;
                   5678:     else {
1.200     noro     5679:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     5680: #if 0
1.157     noro     5681:         ndv_alloc += nmv_adv*len;
1.103     noro     5682: #endif
1.157     noro     5683:         mr = mr0;
                   5684:         p = s0vect;
                   5685:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5686:             if ( !rhead[j] ) {
                   5687:                 if ( c = vect[k++] ) {
                   5688:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   5689:                 }
                   5690:             }
                   5691:         MKNDV(nd_nvar,mr0,len,r);
                   5692:         return r;
                   5693:     }
1.65      noro     5694: }
                   5695:
1.129     noro     5696: /* for preprocessed vector */
                   5697:
1.113     noro     5698: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     5699: {
1.157     noro     5700:     int j,k,len;
                   5701:     UINT *p;
                   5702:     Q c;
                   5703:     NDV r;
                   5704:     NMV mr0,mr;
                   5705:
                   5706:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   5707:     if ( !len ) return 0;
                   5708:     else {
1.200     noro     5709:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     5710: #if 0
1.157     noro     5711:         ndv_alloc += nmv_adv*len;
1.107     noro     5712: #endif
1.157     noro     5713:         mr = mr0;
                   5714:         p = s0vect;
                   5715:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   5716:             if ( !rhead[j] ) {
                   5717:                 if ( c = vect[k++] ) {
                   5718:                     if ( DN(c) )
                   5719:                         error("afo");
                   5720:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5721:                 }
                   5722:             }
                   5723:         MKNDV(nd_nvar,mr0,len,r);
                   5724:         return r;
                   5725:     }
1.107     noro     5726: }
                   5727:
1.129     noro     5728: /* for plain vector */
                   5729:
                   5730: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   5731: {
1.157     noro     5732:     int j,k,len;
                   5733:     UINT *p;
                   5734:     Q c;
                   5735:     NDV r;
                   5736:     NMV mr0,mr;
                   5737:
                   5738:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   5739:     if ( !len ) return 0;
                   5740:     else {
1.200     noro     5741:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     5742: #if 0
1.157     noro     5743:         ndv_alloc += nmv_adv*len;
1.129     noro     5744: #endif
1.157     noro     5745:         mr = mr0;
                   5746:         p = s0vect;
                   5747:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   5748:             if ( c = vect[k] ) {
                   5749:                 if ( DN(c) )
                   5750:                     error("afo");
                   5751:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   5752:             }
                   5753:         MKNDV(nd_nvar,mr0,len,r);
                   5754:         return r;
                   5755:     }
1.129     noro     5756: }
                   5757:
1.133     noro     5758: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     5759: {
1.157     noro     5760:     ND_pairs t;
                   5761:     NODE sp0,sp;
                   5762:     int stat;
                   5763:     ND spol;
                   5764:
                   5765:     for ( t = l; t; t = NEXT(t) ) {
                   5766:         stat = nd_sp(m,trace,t,&spol);
                   5767:         if ( !stat ) return 0;
                   5768:         if ( spol ) {
                   5769:             add_pbucket_symbolic(bucket,spol);
                   5770:         }
                   5771:     }
                   5772:     return 1;
1.65      noro     5773: }
                   5774:
1.133     noro     5775: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     5776: {
1.157     noro     5777:     NODE rp0,rp;
                   5778:     NM mul,head,s0,s;
                   5779:     int index,col,i,sugar;
                   5780:     RHist h;
                   5781:     UINT *s0v,*p;
                   5782:     NM_ind_pair pair;
                   5783:     ND red;
                   5784:     NDV *ps;
                   5785:
                   5786:     s0 = 0; rp0 = 0; col = 0;
                   5787:     ps = trace?nd_ps_trace:nd_ps;
                   5788:     while ( 1 ) {
                   5789:         head = remove_head_pbucket_symbolic(bucket);
                   5790:         if ( !head ) break;
                   5791:         if ( !s0 ) s0 = head;
                   5792:         else NEXT(s) = head;
                   5793:         s = head;
                   5794:         index = ndl_find_reducer(DL(head));
                   5795:         if ( index >= 0 ) {
                   5796:             h = nd_psh[index];
                   5797:             NEWNM(mul);
                   5798:             ndl_sub(DL(head),DL(h),DL(mul));
                   5799:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   5800:             sugar = TD(DL(mul))+SG(ps[index]);
                   5801:             MKNM_ind_pair(pair,mul,index,sugar);
                   5802:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   5803:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   5804:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   5805:         }
                   5806:         col++;
                   5807:     }
                   5808:     if ( rp0 ) NEXT(rp) = 0;
                   5809:     NEXT(s) = 0;
                   5810:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   5811:     for ( i = 0, p = s0v, s = s0; i < col;
                   5812:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   5813:     *s0vect = s0v;
                   5814:     *r = rp0;
                   5815:     return col;
1.65      noro     5816: }
                   5817:
1.167     noro     5818: NODE nd_f4(int m,int **indp)
1.69      noro     5819: {
1.157     noro     5820:     int i,nh,stat,index;
1.208     noro     5821:     NODE r,g,tn0,tn,node;
                   5822:     ND_pairs d,l,t,ll0,ll;
                   5823:        LIST l0,l1;
1.157     noro     5824:     ND spol,red;
                   5825:     NDV nf,redv;
                   5826:     NM s0,s;
1.208     noro     5827:     NODE rp0,srp0,nflist,nzlist;
                   5828:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
1.157     noro     5829:     UINT c;
                   5830:     UINT **spmat;
                   5831:     UINT *s0vect,*svect,*p,*v;
                   5832:     int *colstat;
                   5833:     IndArray *imat;
                   5834:     int *rhead;
                   5835:     int spcol,sprow;
                   5836:     int sugar;
                   5837:     PGeoBucket bucket;
                   5838:     struct oEGT eg0,eg1,eg_f4;
1.208     noro     5839:     Q i1,i2,sugarq;
1.103     noro     5840: #if 0
1.157     noro     5841:     ndv_alloc = 0;
1.103     noro     5842: #endif
1.157     noro     5843:     g = 0; d = 0;
                   5844:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5845:         d = update_pairs(d,g,i,0);
1.157     noro     5846:         g = update_base(g,i);
                   5847:     }
1.208     noro     5848:        nzlist = 0;
1.157     noro     5849:     while ( d ) {
                   5850:         get_eg(&eg0);
                   5851:         l = nd_minsugarp(d,&d);
                   5852:         sugar = SG(l);
1.208     noro     5853:         if ( nd_nzlist ) {
                   5854:             for ( tn = nd_nzlist; tn; tn = NEXT(tn) ) {
                   5855:                 node = BDY((LIST)BDY(tn));
                   5856:                            if ( QTOS((Q)ARG0(node)) == sugar ) break;
                   5857:             }
1.209     noro     5858:             if ( !tn ) error("nd_f4 : inconsistent non-zero list");
1.208     noro     5859:                        for ( t = l, ll0 = 0; t; t = NEXT(t) ) {
                   5860:                 for ( tn = BDY((LIST)ARG1(node)); tn; tn = NEXT(tn) ) {
                   5861:                                  i1s = QTOS((Q)ARG0(BDY((LIST)BDY(tn))));
                   5862:                                  i2s = QTOS((Q)ARG1(BDY((LIST)BDY(tn))));
                   5863:                                  if ( t->i1 == i1s && t->i2 == i2s ) break;
                   5864:                                }
                   5865:                            if ( tn ) {
                   5866:                                    if ( !ll0 ) ll0 = t;
                   5867:                                        else NEXT(ll) = t;
                   5868:                                        ll = t;
                   5869:                                }
                   5870:             }
                   5871:                        if ( ll0 ) NEXT(ll) = 0;
                   5872:                    l = ll0;
                   5873:         }
1.157     noro     5874:         bucket = create_pbucket();
                   5875:         stat = nd_sp_f4(m,0,l,bucket);
                   5876:         if ( !stat ) {
                   5877:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5878:             NEXT(t) = d; d = l;
                   5879:             d = nd_reconstruct(0,d);
                   5880:             continue;
                   5881:         }
                   5882:         if ( bucket->m < 0 ) continue;
                   5883:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5884:         if ( !col ) {
                   5885:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5886:             NEXT(t) = d; d = l;
                   5887:             d = nd_reconstruct(0,d);
                   5888:             continue;
                   5889:         }
                   5890:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5891:         if ( DP_Print )
                   5892:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5893:                 sugar,eg_f4.exectime+eg_f4.gctime);
1.210     noro     5894:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
1.157     noro     5895:         /* adding new bases */
                   5896:         for ( r = nflist; r; r = NEXT(r) ) {
                   5897:             nf = (NDV)BDY(r);
                   5898:             ndv_removecont(m,nf);
                   5899:             if ( !m && nd_nalg ) {
                   5900:                 ND nf1;
                   5901:
                   5902:                 nf1 = ndvtond(m,nf);
                   5903:                 nd_monic(0,&nf1);
                   5904:                 nd_removecont(m,nf1);
                   5905:                 nf = ndtondv(m,nf1);
                   5906:             }
                   5907:             nh = ndv_newps(m,nf,0);
1.168     noro     5908:             d = update_pairs(d,g,nh,0);
1.157     noro     5909:             g = update_base(g,nh);
                   5910:         }
1.208     noro     5911:         if ( nd_gentrace ) {
                   5912:                        for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   5913:                                NEXTNODE(tn0,tn);
                   5914:                 STOQ(t->i1,i1); STOQ(t->i2,i2);
                   5915:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   5916:                                BDY(tn) = l0;
                   5917:                        }
                   5918:                        if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   5919:             STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
                   5920:             MKNODE(node,l1,nzlist); nzlist = node;
                   5921:         }
                   5922:     }
                   5923:     if ( nd_gentrace ) {
                   5924:                MKLIST(l0,reverse_node(nzlist));
                   5925:         MKNODE(nd_alltracelist,l0,0);
1.157     noro     5926:     }
1.103     noro     5927: #if 0
1.157     noro     5928:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     5929: #endif
1.167     noro     5930:        conv_ilist(0,0,g,indp);
1.157     noro     5931:     return g;
1.69      noro     5932: }
1.74      noro     5933:
1.167     noro     5934: NODE nd_f4_trace(int m,int **indp)
1.133     noro     5935: {
1.157     noro     5936:     int i,nh,stat,index;
                   5937:     NODE r,g;
                   5938:     ND_pairs d,l,l0,t;
                   5939:     ND spol,red;
                   5940:     NDV nf,redv,nfqv,nfv;
                   5941:     NM s0,s;
                   5942:     NODE rp0,srp0,nflist;
                   5943:     int nsp,nred,col,rank,len,k,j,a;
                   5944:     UINT c;
                   5945:     UINT **spmat;
                   5946:     UINT *s0vect,*svect,*p,*v;
                   5947:     int *colstat;
                   5948:     IndArray *imat;
                   5949:     int *rhead;
                   5950:     int spcol,sprow;
                   5951:     int sugar;
                   5952:     PGeoBucket bucket;
                   5953:     struct oEGT eg0,eg1,eg_f4;
                   5954:
                   5955:     g = 0; d = 0;
                   5956:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     5957:         d = update_pairs(d,g,i,0);
1.157     noro     5958:         g = update_base(g,i);
                   5959:     }
                   5960:     while ( d ) {
                   5961:         get_eg(&eg0);
                   5962:         l = nd_minsugarp(d,&d);
                   5963:         sugar = SG(l);
                   5964:         bucket = create_pbucket();
                   5965:         stat = nd_sp_f4(m,0,l,bucket);
                   5966:         if ( !stat ) {
                   5967:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5968:             NEXT(t) = d; d = l;
                   5969:             d = nd_reconstruct(1,d);
                   5970:             continue;
                   5971:         }
                   5972:         if ( bucket->m < 0 ) continue;
                   5973:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   5974:         if ( !col ) {
                   5975:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5976:             NEXT(t) = d; d = l;
                   5977:             d = nd_reconstruct(1,d);
                   5978:             continue;
                   5979:         }
                   5980:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   5981:         if ( DP_Print )
                   5982:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   5983:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   5984:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   5985:         if ( !l0 ) continue;
                   5986:         l = l0;
                   5987:
                   5988:         /* over Q */
                   5989:         bucket = create_pbucket();
                   5990:         stat = nd_sp_f4(0,1,l,bucket);
                   5991:         if ( !stat ) {
                   5992:             for ( t = l; NEXT(t); t = NEXT(t) );
                   5993:             NEXT(t) = d; d = l;
                   5994:             d = nd_reconstruct(1,d);
                   5995:             continue;
                   5996:         }
                   5997:         if ( bucket->m < 0 ) continue;
                   5998:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   5999:         if ( !col ) {
                   6000:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6001:             NEXT(t) = d; d = l;
                   6002:             d = nd_reconstruct(1,d);
                   6003:             continue;
                   6004:         }
                   6005:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   6006:         /* adding new bases */
                   6007:         for ( r = nflist; r; r = NEXT(r) ) {
                   6008:             nfqv = (NDV)BDY(r);
                   6009:             ndv_removecont(0,nfqv);
                   6010:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   6011:             if ( nd_nalg ) {
                   6012:                 ND nf1;
                   6013:
                   6014:                 nf1 = ndvtond(m,nfqv);
                   6015:                 nd_monic(0,&nf1);
                   6016:                 nd_removecont(0,nf1);
                   6017:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   6018:             }
                   6019:             nfv = ndv_dup(0,nfqv);
                   6020:             ndv_mod(m,nfv);
                   6021:             ndv_removecont(m,nfv);
                   6022:             nh = ndv_newps(0,nfv,nfqv);
1.168     noro     6023:             d = update_pairs(d,g,nh,0);
1.157     noro     6024:             g = update_base(g,nh);
                   6025:         }
                   6026:     }
1.133     noro     6027: #if 0
1.157     noro     6028:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     6029: #endif
1.167     noro     6030:        conv_ilist(0,1,g,indp);
1.157     noro     6031:     return g;
1.133     noro     6032: }
                   6033:
1.176     noro     6034: NODE nd_f4_pseudo_trace(int m,int **indp)
                   6035: {
                   6036:     int i,nh,stat,index;
                   6037:     NODE r,g;
                   6038:     ND_pairs d,l,l0,t;
                   6039:     ND spol,red;
                   6040:     NDV nf,redv,nfqv,nfv;
                   6041:     NM s0,s;
                   6042:     NODE rp0,srp0,nflist;
                   6043:     int nsp,nred,col,rank,len,k,j,a;
                   6044:     UINT c;
                   6045:     UINT **spmat;
                   6046:     UINT *s0vect,*svect,*p,*v;
                   6047:     int *colstat;
                   6048:     IndArray *imat;
                   6049:     int *rhead;
                   6050:     int spcol,sprow;
                   6051:     int sugar;
                   6052:     PGeoBucket bucket;
                   6053:     struct oEGT eg0,eg1,eg_f4;
                   6054:
                   6055:     g = 0; d = 0;
                   6056:     for ( i = 0; i < nd_psn; i++ ) {
                   6057:         d = update_pairs(d,g,i,0);
                   6058:         g = update_base(g,i);
                   6059:     }
                   6060:     while ( d ) {
                   6061:         get_eg(&eg0);
                   6062:         l = nd_minsugarp(d,&d);
                   6063:         sugar = SG(l);
                   6064:         bucket = create_pbucket();
                   6065:         stat = nd_sp_f4(m,0,l,bucket);
                   6066:         if ( !stat ) {
                   6067:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6068:             NEXT(t) = d; d = l;
                   6069:             d = nd_reconstruct(1,d);
                   6070:             continue;
                   6071:         }
                   6072:         if ( bucket->m < 0 ) continue;
                   6073:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6074:         if ( !col ) {
                   6075:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6076:             NEXT(t) = d; d = l;
                   6077:             d = nd_reconstruct(1,d);
                   6078:             continue;
                   6079:         }
                   6080:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6081:         if ( DP_Print )
                   6082:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6083:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   6084:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   6085:         if ( !l0 ) continue;
                   6086:         l = l0;
                   6087:
                   6088:         /* over Q */
                   6089:                while ( 1 ) {
                   6090:                bucket = create_pbucket();
                   6091:                stat = nd_sp_f4(0,1,l,bucket);
                   6092:                if ( !stat ) {
                   6093:                for ( t = l; NEXT(t); t = NEXT(t) );
                   6094:                NEXT(t) = d; d = l;
                   6095:                d = nd_reconstruct(1,d);
                   6096:                continue;
                   6097:                }
                   6098:                if ( bucket->m < 0 ) continue;
                   6099:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   6100:                if ( !col ) {
                   6101:                for ( t = l; NEXT(t); t = NEXT(t) );
                   6102:                NEXT(t) = d; d = l;
                   6103:                d = nd_reconstruct(1,d);
                   6104:                continue;
                   6105:                }
                   6106:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   6107:                }
                   6108:
                   6109:         /* adding new bases */
                   6110:         for ( r = nflist; r; r = NEXT(r) ) {
                   6111:             nfqv = (NDV)BDY(r);
                   6112:             ndv_removecont(0,nfqv);
                   6113:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   6114:             if ( nd_nalg ) {
                   6115:                 ND nf1;
                   6116:
                   6117:                 nf1 = ndvtond(m,nfqv);
                   6118:                 nd_monic(0,&nf1);
                   6119:                 nd_removecont(0,nf1);
                   6120:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   6121:             }
                   6122:             nfv = ndv_dup(0,nfqv);
                   6123:             ndv_mod(m,nfv);
                   6124:             ndv_removecont(m,nfv);
                   6125:             nh = ndv_newps(0,nfv,nfqv);
                   6126:             d = update_pairs(d,g,nh,0);
                   6127:             g = update_base(g,nh);
                   6128:         }
                   6129:     }
                   6130: #if 0
                   6131:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   6132: #endif
                   6133:        conv_ilist(0,1,g,indp);
                   6134:     return g;
                   6135: }
                   6136:
1.133     noro     6137: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     6138: {
1.157     noro     6139:     IndArray *imat;
                   6140:     int nsp,nred,i;
                   6141:     int *rhead;
                   6142:     NODE r0,rp;
                   6143:     ND_pairs sp;
                   6144:     NM_ind_pair *rvect;
1.210     noro     6145:     UINT *s;
                   6146:     int *s0hash;
                   6147:
1.198     noro     6148: init_eg(&eg_search);
1.157     noro     6149:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6150:     nred = length(rp0);
                   6151:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   6152:     rhead = (int *)ALLOCA(col*sizeof(int));
                   6153:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6154:
                   6155:     /* construction of index arrays */
                   6156:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
1.210     noro     6157:     s0hash = (int *)ALLOCA(col*sizeof(int));
                   6158:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   6159:         s0hash[i] = ndl_hash_value(s);
1.157     noro     6160:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   6161:         rvect[i] = (NM_ind_pair)BDY(rp);
1.210     noro     6162:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,s0hash,rvect[i]);
1.157     noro     6163:         rhead[imat[i]->head] = 1;
                   6164:     }
                   6165:     if ( m )
                   6166:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   6167:     else
                   6168:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.198     noro     6169: print_eg("search",&eg_search);
1.157     noro     6170:     return r0;
1.106     noro     6171: }
1.74      noro     6172:
1.106     noro     6173: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     6174:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     6175: {
1.157     noro     6176:     int spcol,sprow,a;
                   6177:     int i,j,k,l,rank;
                   6178:     NODE r0,r;
                   6179:     ND_pairs sp;
                   6180:     ND spol;
                   6181:     int **spmat;
                   6182:     UINT *svect,*v;
                   6183:     int *colstat;
                   6184:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   6185:     int maxrs;
                   6186:     int *spsugar;
                   6187:     ND_pairs *spactive;
                   6188:
                   6189:     spcol = col-nred;
                   6190:     get_eg(&eg0);
                   6191:     /* elimination (1st step) */
                   6192:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   6193:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
                   6194:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6195:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   6196:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6197:         nd_sp(m,0,sp,&spol);
                   6198:         if ( !spol ) continue;
                   6199:         nd_to_vect(m,s0vect,col,spol,svect);
                   6200:         if ( m == -1 )
                   6201:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   6202:         else
                   6203:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   6204:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6205:         if ( i < col ) {
                   6206:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   6207:             for ( j = k = 0; j < col; j++ )
                   6208:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6209:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6210:             if ( nz )
                   6211:             spactive[sprow] = sp;
                   6212:             sprow++;
                   6213:         }
                   6214:         nd_free(spol);
                   6215:     }
                   6216:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6217:     if ( DP_Print ) {
                   6218:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6219:         fflush(asir_out);
                   6220:     }
                   6221:     /* free index arrays */
1.200     noro     6222:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     6223:
                   6224:     /* elimination (2nd step) */
                   6225:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6226:     if ( m == -1 )
                   6227:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6228:     else
                   6229:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   6230:     r0 = 0;
                   6231:     for ( i = 0; i < rank; i++ ) {
                   6232:         NEXTNODE(r0,r); BDY(r) =
                   6233:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6234:         SG((NDV)BDY(r)) = spsugar[i];
1.200     noro     6235:         GCFREE(spmat[i]);
1.157     noro     6236:     }
                   6237:     if ( r0 ) NEXT(r) = 0;
                   6238:
1.200     noro     6239:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     6240:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6241:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6242:     if ( DP_Print ) {
                   6243:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6244:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6245:             nsp,nred,sprow,spcol,rank);
                   6246:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6247:     }
                   6248:     if ( nz ) {
                   6249:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   6250:         if ( rank > 0 ) {
                   6251:             NEXT(spactive[rank-1]) = 0;
                   6252:             *nz = spactive[0];
                   6253:         } else
                   6254:             *nz = 0;
                   6255:     }
                   6256:     return r0;
1.74      noro     6257: }
                   6258:
1.133     noro     6259: #if 1
                   6260: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     6261:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6262: {
1.157     noro     6263:     int spcol,sprow,a;
                   6264:     int i,j,k,l,rank;
                   6265:     NODE r0,r;
                   6266:     ND_pairs sp;
                   6267:     ND spol;
                   6268:     Q **spmat;
                   6269:     Q *svect,*v;
                   6270:     int *colstat;
                   6271:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   6272:     int maxrs;
                   6273:     int *spsugar;
                   6274:     pointer *w;
                   6275:
                   6276:     spcol = col-nred;
                   6277:     get_eg(&eg0);
                   6278:     /* elimination (1st step) */
                   6279:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   6280:     svect = (Q *)ALLOCA(col*sizeof(Q));
                   6281:     spsugar = (int *)ALLOCA(nsp*sizeof(Q));
                   6282:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6283:         nd_sp(0,trace,sp,&spol);
                   6284:         if ( !spol ) continue;
                   6285:         nd_to_vect_q(s0vect,col,spol,svect);
                   6286:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   6287:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6288:         if ( i < col ) {
                   6289:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   6290:             for ( j = k = 0; j < col; j++ )
                   6291:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6292:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6293:             sprow++;
                   6294:         }
                   6295: /*        nd_free(spol); */
                   6296:     }
                   6297:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   6298:     if ( DP_Print ) {
                   6299:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   6300:         fflush(asir_out);
                   6301:     }
                   6302:     /* free index arrays */
1.200     noro     6303: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     6304:
                   6305:     /* elimination (2nd step) */
                   6306:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6307:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   6308:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   6309:     for ( i = 0; i < rank; i++ ) {
1.213     noro     6310: #if 0
1.157     noro     6311:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   6312:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.213     noro     6313: #else
                   6314:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   6315:         SG((NDV)w[i]) = spsugar[i];
                   6316: #endif
1.200     noro     6317: /*        GCFREE(spmat[i]); */
1.157     noro     6318:     }
1.138     noro     6319: #if 0
1.157     noro     6320:     qsort(w,rank,sizeof(NDV),
                   6321:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     6322: #endif
1.157     noro     6323:     r0 = 0;
                   6324:     for ( i = 0; i < rank; i++ ) {
                   6325:         NEXTNODE(r0,r); BDY(r) = w[i];
                   6326:     }
                   6327:     if ( r0 ) NEXT(r) = 0;
                   6328:
1.200     noro     6329: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     6330:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   6331:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   6332:     if ( DP_Print ) {
                   6333:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   6334:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   6335:             nsp,nred,sprow,spcol,rank);
                   6336:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   6337:     }
                   6338:     return r0;
1.107     noro     6339: }
1.129     noro     6340: #else
                   6341: void printm(Q **mat,int row,int col)
                   6342: {
1.157     noro     6343:     int i,j;
                   6344:     printf("[");
                   6345:     for ( i = 0; i < row; i++ ) {
                   6346:         for ( j = 0; j < col; j++ ) {
                   6347:             printexpr(CO,mat[i][j]); printf(" ");
                   6348:         }
                   6349:         printf("]\n");
                   6350:     }
1.129     noro     6351: }
                   6352:
                   6353: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   6354:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   6355: {
1.157     noro     6356:     int row,a;
                   6357:     int i,j,rank;
                   6358:     NODE r0,r;
                   6359:     ND_pairs sp;
                   6360:     ND spol;
                   6361:     Q **mat;
                   6362:     int *colstat;
                   6363:     int *sugar;
                   6364:
                   6365:     row = nsp+nred;
                   6366:     /* make the matrix */
                   6367:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   6368:     sugar = (int *)ALLOCA(row*sizeof(int));
                   6369:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   6370:         nd_sp(0,0,sp,&spol);
                   6371:         if ( !spol ) continue;
                   6372:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   6373:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   6374:         sugar[row] = SG(spol);
                   6375:         row++;
                   6376:     }
                   6377:     for ( i = 0; i < nred; i++, row++ ) {
                   6378:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   6379:         sugar[row] = rvect[i]->sugar;
                   6380:     }
                   6381:     /* elimination */
                   6382:     colstat = (int *)ALLOCA(col*sizeof(int));
                   6383:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   6384:     r0 = 0;
                   6385:     for ( i = 0; i < rank; i++ ) {
                   6386:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   6387:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   6388:         if ( rhead[j] ) continue;
                   6389:         NEXTNODE(r0,r); BDY(r) =
                   6390:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   6391:         SG((NDV)BDY(r)) = sugar[i];
                   6392:     }
                   6393:     if ( r0 ) NEXT(r) = 0;
                   6394:     printf("\n");
                   6395:     return r0;
1.129     noro     6396: }
                   6397: #endif
1.107     noro     6398:
1.74      noro     6399: FILE *nd_write,*nd_read;
                   6400:
                   6401: void nd_send_int(int a) {
1.157     noro     6402:     write_int(nd_write,&a);
1.74      noro     6403: }
                   6404:
                   6405: void nd_send_intarray(int *p,int len) {
1.157     noro     6406:     write_intarray(nd_write,p,len);
1.74      noro     6407: }
                   6408:
                   6409: int nd_recv_int() {
1.157     noro     6410:     int a;
1.74      noro     6411:
1.157     noro     6412:     read_int(nd_read,&a);
                   6413:     return a;
1.74      noro     6414: }
                   6415:
                   6416: void nd_recv_intarray(int *p,int len) {
1.157     noro     6417:     read_intarray(nd_read,p,len);
1.74      noro     6418: }
                   6419:
                   6420: void nd_send_ndv(NDV p) {
1.157     noro     6421:     int len,i;
                   6422:     NMV m;
1.74      noro     6423:
1.157     noro     6424:     if ( !p ) nd_send_int(0);
                   6425:     else {
                   6426:         len = LEN(p);
                   6427:         nd_send_int(len);
                   6428:         m = BDY(p);
                   6429:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6430:             nd_send_int(CM(m));
                   6431:             nd_send_intarray(DL(m),nd_wpd);
                   6432:         }
                   6433:     }
1.74      noro     6434: }
                   6435:
                   6436: void nd_send_nd(ND p) {
1.157     noro     6437:     int len,i;
                   6438:     NM m;
1.74      noro     6439:
1.157     noro     6440:     if ( !p ) nd_send_int(0);
                   6441:     else {
                   6442:         len = LEN(p);
                   6443:         nd_send_int(len);
                   6444:         m = BDY(p);
                   6445:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   6446:             nd_send_int(CM(m));
                   6447:             nd_send_intarray(DL(m),nd_wpd);
                   6448:         }
                   6449:     }
1.74      noro     6450: }
1.65      noro     6451:
1.74      noro     6452: NDV nd_recv_ndv()
                   6453: {
1.157     noro     6454:     int len,i;
                   6455:     NMV m,m0;
                   6456:     NDV r;
                   6457:
                   6458:     len = nd_recv_int();
                   6459:     if ( !len ) return 0;
                   6460:     else {
1.200     noro     6461:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6462: #if 0
1.157     noro     6463:         ndv_alloc += len*nmv_adv;
1.103     noro     6464: #endif
1.157     noro     6465:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6466:             CM(m) = nd_recv_int();
                   6467:             nd_recv_intarray(DL(m),nd_wpd);
                   6468:         }
                   6469:         MKNDV(nd_nvar,m0,len,r);
                   6470:         return r;
                   6471:     }
1.74      noro     6472: }
1.65      noro     6473:
1.74      noro     6474: int ox_exec_f4_red(Q proc)
                   6475: {
1.157     noro     6476:     Obj obj;
                   6477:     STRING fname;
                   6478:     NODE arg;
                   6479:     int s;
                   6480:     extern int ox_need_conv,ox_file_io;
                   6481:
                   6482:     MKSTR(fname,"nd_exec_f4_red");
                   6483:     arg = mknode(2,proc,fname);
                   6484:     Pox_cmo_rpc(arg,&obj);
                   6485:     s = get_ox_server_id(QTOS(proc));
                   6486:     nd_write = iofp[s].out;
                   6487:     nd_read = iofp[s].in;
                   6488:     ox_need_conv = ox_file_io = 0;
                   6489:     return s;
1.74      noro     6490: }
                   6491:
1.210     noro     6492: #if 0
1.133     noro     6493: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     6494: {
1.157     noro     6495:     int nsp,nred;
                   6496:     int i,rank,s;
                   6497:     NODE rp,r0,r;
                   6498:     ND_pairs sp;
                   6499:     NM_ind_pair pair;
                   6500:     NMV nmv;
                   6501:     NM nm;
                   6502:     NDV nf;
                   6503:     Obj proc,dmy;
                   6504:
                   6505:     ox_launch_main(0,0,&proc);
                   6506:     s = ox_exec_f4_red((Q)proc);
                   6507:
                   6508:     nd_send_int(m);
                   6509:     nd_send_int(nd_nvar);
                   6510:     nd_send_int(nd_bpe);
                   6511:     nd_send_int(nd_wpd);
                   6512:     nd_send_int(nmv_adv);
                   6513:
                   6514:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   6515:
                   6516:     nd_send_int(nd_psn);
                   6517:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   6518:
                   6519:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6520:     nd_send_int(nsp);
                   6521:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   6522:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   6523:     }
                   6524:
                   6525:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   6526:
                   6527:     nred = length(rp0); nd_send_int(nred);
                   6528:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   6529:         pair = (NM_ind_pair)BDY(rp);
                   6530:         nd_send_int(pair->index);
                   6531:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   6532:     }
                   6533:     fflush(nd_write);
                   6534:     rank = nd_recv_int();
                   6535:     fprintf(asir_out,"rank=%d\n",rank);
                   6536:     r0 = 0;
                   6537:     for ( i = 0; i < rank; i++ ) {
                   6538:         nf = nd_recv_ndv();
                   6539:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   6540:     }
                   6541:     Pox_shutdown(mknode(1,proc),&dmy);
                   6542:     return r0;
1.74      noro     6543: }
                   6544:
                   6545: /* server side */
                   6546:
                   6547: void nd_exec_f4_red_dist()
                   6548: {
1.157     noro     6549:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   6550:     NM_ind_pair *rp0;
                   6551:     NDV nf;
                   6552:     UINT *s0vect;
                   6553:     IndArray *imat;
                   6554:     int *rhead;
                   6555:     int **spmat;
                   6556:     UINT *svect,*v;
                   6557:     ND_pairs *sp0;
                   6558:     int *colstat;
                   6559:     int a,sprow,rank;
                   6560:     struct order_spec *ord;
                   6561:     Obj ordspec;
                   6562:     ND spol;
                   6563:     int maxrs;
                   6564:     int *spsugar;
                   6565:
                   6566:     nd_read = iofp[0].in;
                   6567:     nd_write = iofp[0].out;
                   6568:     m = nd_recv_int();
                   6569:     nd_nvar = nd_recv_int();
                   6570:     nd_bpe = nd_recv_int();
                   6571:     nd_wpd = nd_recv_int();
                   6572:     nmv_adv = nd_recv_int();
                   6573:
                   6574:     loadobj(nd_read,&ordspec);
                   6575:     create_order_spec(0,ordspec,&ord);
                   6576:     nd_init_ord(ord);
                   6577:     nd_setup_parameters(nd_nvar,0);
                   6578:
                   6579:     nd_psn = nd_recv_int();
                   6580:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   6581:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   6582:     for ( i = 0; i < nd_psn; i++ ) {
                   6583:         nd_ps[i] = nd_recv_ndv();
                   6584:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   6585:     }
                   6586:
                   6587:     nsp = nd_recv_int();
                   6588:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   6589:     for ( i = 0; i < nsp; i++ ) {
                   6590:         NEWND_pairs(sp0[i]);
                   6591:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   6592:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   6593:     }
                   6594:
                   6595:     col = nd_recv_int();
                   6596:     s0size = col*nd_wpd;
                   6597:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   6598:     nd_recv_intarray(s0vect,s0size);
                   6599:
                   6600:     nred = nd_recv_int();
                   6601:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   6602:     for ( i = 0; i < nred; i++ ) {
                   6603:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   6604:         rp0[i]->index = nd_recv_int();
                   6605:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   6606:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   6607:     }
                   6608:
                   6609:     spcol = col-nred;
                   6610:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   6611:     rhead = (int *)MALLOC(col*sizeof(int));
                   6612:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   6613:
                   6614:     /* construction of index arrays */
                   6615:     for ( i = 0; i < nred; i++ ) {
                   6616:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   6617:         rhead[imat[i]->head] = 1;
                   6618:     }
                   6619:
                   6620:     /* elimination (1st step) */
                   6621:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   6622:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   6623:     spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
                   6624:     for ( a = sprow = 0; a < nsp; a++ ) {
                   6625:         nd_sp(m,0,sp0[a],&spol);
                   6626:         if ( !spol ) continue;
                   6627:         nd_to_vect(m,s0vect,col,spol,svect);
                   6628:         if ( m == -1 )
                   6629:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   6630:         else
                   6631:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   6632:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   6633:         if ( i < col ) {
                   6634:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   6635:             for ( j = k = 0; j < col; j++ )
                   6636:                 if ( !rhead[j] ) v[k++] = svect[j];
                   6637:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   6638:             sprow++;
                   6639:         }
                   6640:         nd_free(spol);
                   6641:     }
                   6642:     /* elimination (2nd step) */
                   6643:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   6644:     if ( m == -1 )
                   6645:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   6646:     else
                   6647:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   6648:     nd_send_int(rank);
                   6649:     for ( i = 0; i < rank; i++ ) {
                   6650:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   6651:         nd_send_ndv(nf);
                   6652:     }
                   6653:     fflush(nd_write);
1.107     noro     6654: }
1.210     noro     6655: #endif
1.107     noro     6656:
1.113     noro     6657: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     6658: {
1.176     noro     6659:     int i,j,t,c,rank,inv;
1.157     noro     6660:     int *ci,*ri;
                   6661:     Q dn;
                   6662:     MAT m,nm;
                   6663:
                   6664:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   6665:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   6666:     for ( i = 0; i < row; i++ )
                   6667:         for ( j = 0; j < col; j++ )
                   6668:             mat0[i][j] = 0;
                   6669:     c = col-rank;
                   6670:     for ( i = 0; i < rank; i++ ) {
                   6671:         mat0[i][ri[i]] = dn;
                   6672:         for ( j = 0; j < c; j++ )
                   6673:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   6674:     }
                   6675:     return rank;
1.76      noro     6676: }
                   6677:
1.133     noro     6678: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     6679: {
1.157     noro     6680:     int i,j,k,l,inv,a,rank,s;
                   6681:     unsigned int *t,*pivot,*pk;
                   6682:     unsigned int **mat;
                   6683:     ND_pairs pair;
                   6684:
                   6685:     mat = (unsigned int **)mat0;
                   6686:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6687:         for ( i = rank; i < row; i++ )
                   6688:             mat[i][j] %= md;
                   6689:         for ( i = rank; i < row; i++ )
                   6690:             if ( mat[i][j] )
                   6691:                 break;
                   6692:         if ( i == row ) {
                   6693:             colstat[j] = 0;
                   6694:             continue;
                   6695:         } else
                   6696:             colstat[j] = 1;
                   6697:         if ( i != rank ) {
                   6698:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6699:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6700:             if ( spactive ) {
                   6701:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   6702:                 spactive[rank] = pair;
                   6703:             }
                   6704:         }
                   6705:         pivot = mat[rank];
                   6706:         s = sugar[rank];
                   6707:         inv = invm(pivot[j],md);
                   6708:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6709:             if ( *pk ) {
                   6710:                 if ( *pk >= (unsigned int)md )
                   6711:                     *pk %= md;
                   6712:                 DMAR(*pk,inv,0,md,*pk)
                   6713:             }
                   6714:         for ( i = rank+1; i < row; i++ ) {
                   6715:             t = mat[i];
                   6716:             if ( a = t[j] ) {
                   6717:                 sugar[i] = MAX(sugar[i],s);
                   6718:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6719:             }
                   6720:         }
                   6721:         rank++;
                   6722:     }
                   6723:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6724:         if ( colstat[j] ) {
                   6725:             pivot = mat[l];
                   6726:             s = sugar[l];
                   6727:             for ( i = 0; i < l; i++ ) {
                   6728:                 t = mat[i];
                   6729:                 t[j] %= md;
                   6730:                 if ( a = t[j] ) {
                   6731:                     sugar[i] = MAX(sugar[i],s);
                   6732:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   6733:                 }
                   6734:             }
                   6735:             l--;
                   6736:         }
                   6737:     for ( j = 0, l = 0; l < rank; j++ )
                   6738:         if ( colstat[j] ) {
                   6739:             t = mat[l];
                   6740:             for ( k = j; k < col; k++ )
                   6741:                 if ( t[k] >= (unsigned int)md )
                   6742:                     t[k] %= md;
                   6743:             l++;
                   6744:         }
                   6745:     return rank;
1.76      noro     6746: }
                   6747:
                   6748: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   6749: {
1.157     noro     6750:     int i,j,k,l,inv,a,rank,s;
                   6751:     unsigned int *t,*pivot,*pk;
                   6752:     unsigned int **mat;
                   6753:
                   6754:     mat = (unsigned int **)mat0;
                   6755:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6756:         for ( i = rank; i < row; i++ )
                   6757:             if ( mat[i][j] )
                   6758:                 break;
                   6759:         if ( i == row ) {
                   6760:             colstat[j] = 0;
                   6761:             continue;
                   6762:         } else
                   6763:             colstat[j] = 1;
                   6764:         if ( i != rank ) {
                   6765:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6766:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6767:         }
                   6768:         pivot = mat[rank];
                   6769:         s = sugar[rank];
                   6770:         inv = _invsf(pivot[j]);
                   6771:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6772:             if ( *pk )
                   6773:                 *pk = _mulsf(*pk,inv);
                   6774:         for ( i = rank+1; i < row; i++ ) {
                   6775:             t = mat[i];
                   6776:             if ( a = t[j] ) {
                   6777:                 sugar[i] = MAX(sugar[i],s);
                   6778:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6779:             }
                   6780:         }
                   6781:         rank++;
                   6782:     }
                   6783:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6784:         if ( colstat[j] ) {
                   6785:             pivot = mat[l];
                   6786:             s = sugar[l];
                   6787:             for ( i = 0; i < l; i++ ) {
                   6788:                 t = mat[i];
                   6789:                 if ( a = t[j] ) {
                   6790:                     sugar[i] = MAX(sugar[i],s);
                   6791:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   6792:                 }
                   6793:             }
                   6794:             l--;
                   6795:         }
                   6796:     return rank;
1.77      noro     6797: }
                   6798:
                   6799: int ndv_ishomo(NDV p)
                   6800: {
1.157     noro     6801:     NMV m;
                   6802:     int len,h;
1.77      noro     6803:
1.157     noro     6804:     if ( !p ) return 1;
                   6805:     len = LEN(p);
                   6806:     m = BDY(p);
                   6807:     h = TD(DL(m));
                   6808:     NMV_ADV(m);
                   6809:     for ( len--; len; len--, NMV_ADV(m) )
                   6810:         if ( TD(DL(m)) != h ) return 0;
                   6811:     return 1;
1.77      noro     6812: }
                   6813:
                   6814: void ndv_save(NDV p,int index)
                   6815: {
1.157     noro     6816:     FILE *s;
                   6817:     char name[BUFSIZ];
                   6818:     short id;
                   6819:     int nv,sugar,len,n,i,td,e,j;
                   6820:     NMV m;
                   6821:     unsigned int *dl;
                   6822:     int mpos;
                   6823:
                   6824:     sprintf(name,"%s/%d",Demand,index);
                   6825:     s = fopen(name,"w");
                   6826:     savevl(s,0);
                   6827:     if ( !p ) {
                   6828:         saveobj(s,0);
                   6829:         return;
                   6830:     }
                   6831:     id = O_DP;
                   6832:     nv = NV(p);
                   6833:     sugar = SG(p);
                   6834:     len = LEN(p);
                   6835:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   6836:     write_int(s,&len);
                   6837:
                   6838:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6839:         saveobj(s,(Obj)CQ(m));
                   6840:         dl = DL(m);
                   6841:         td = TD(dl);
                   6842:         write_int(s,&td);
                   6843:         for ( j = 0; j < nv; j++ ) {
                   6844:             e = GET_EXP(dl,j);
                   6845:             write_int(s,&e);
                   6846:         }
                   6847:         if ( nd_module ) {
                   6848:             mpos = MPOS(dl); write_int(s,&mpos);
                   6849:         }
                   6850:     }
                   6851:     fclose(s);
1.77      noro     6852: }
                   6853:
1.206     noro     6854: void nd_save_mod(ND p,int index)
                   6855: {
                   6856:     FILE *s;
                   6857:     char name[BUFSIZ];
                   6858:     int nv,sugar,len,c;
                   6859:     NM m;
                   6860:
                   6861:     sprintf(name,"%s/%d",Demand,index);
                   6862:     s = fopen(name,"w");
                   6863:     if ( !p ) {
                   6864:                len = 0;
                   6865:        write_int(s,&len);
                   6866:                fclose(s);
                   6867:         return;
                   6868:     }
                   6869:     nv = NV(p);
                   6870:     sugar = SG(p);
                   6871:     len = LEN(p);
                   6872:     write_int(s,&nv); write_int(s,&sugar); write_int(s,&len);
                   6873:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   6874:          c = CM(m); write_int(s,&c);
                   6875:          write_intarray(s,DL(m),nd_wpd);
                   6876:        }
                   6877:     fclose(s);
                   6878: }
                   6879:
1.77      noro     6880: NDV ndv_load(int index)
                   6881: {
1.157     noro     6882:     FILE *s;
                   6883:     char name[BUFSIZ];
                   6884:     short id;
                   6885:     int nv,sugar,len,n,i,td,e,j;
                   6886:     NDV d;
                   6887:     NMV m0,m;
                   6888:     unsigned int *dl;
                   6889:     Obj obj;
                   6890:     int mpos;
                   6891:
                   6892:     sprintf(name,"%s/%d",Demand,index);
                   6893:     s = fopen(name,"r");
                   6894:     if ( !s ) return 0;
                   6895:
                   6896:     skipvl(s);
                   6897:     read_short(s,&id);
                   6898:     if ( !id ) return 0;
                   6899:     read_int(s,&nv);
                   6900:     read_int(s,&sugar);
                   6901:     read_int(s,&len);
                   6902:
                   6903:     m0 = m = MALLOC(len*nmv_adv);
                   6904:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6905:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   6906:         dl = DL(m);
                   6907:         ndl_zero(dl);
                   6908:         read_int(s,&td); TD(dl) = td;
                   6909:         for ( j = 0; j < nv; j++ ) {
                   6910:             read_int(s,&e);
                   6911:             PUT_EXP(dl,j,e);
                   6912:         }
                   6913:         if ( nd_module ) {
                   6914:             read_int(s,&mpos); MPOS(dl) = mpos;
                   6915:         }
                   6916:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   6917:     }
                   6918:     fclose(s);
                   6919:     MKNDV(nv,m0,len,d);
                   6920:     SG(d) = sugar;
                   6921:     return d;
1.99      noro     6922: }
                   6923:
1.206     noro     6924: ND nd_load_mod(int index)
                   6925: {
                   6926:     FILE *s;
                   6927:     char name[BUFSIZ];
                   6928:     int nv,sugar,len,i,c;
                   6929:        ND d;
                   6930:     NM m0,m;
                   6931:
                   6932:     sprintf(name,"%s/%d",Demand,index);
                   6933:     s = fopen(name,"r");
                   6934:        /* if the file does not exist, it means p[index]=0 */
                   6935:     if ( !s ) return 0;
                   6936:
                   6937:     read_int(s,&nv);
                   6938:        if ( !nv ) { fclose(s); return 0; }
                   6939:
                   6940:     read_int(s,&sugar);
                   6941:     read_int(s,&len);
                   6942:        for ( m0 = 0, i = 0; i < len; i++ ) {
                   6943:                NEXTNM(m0,m);
                   6944:                read_int(s,&c); CM(m) = c;
                   6945:                read_intarray(s,DL(m),nd_wpd);
                   6946:        }
                   6947:        NEXT(m) = 0;
                   6948:     MKND(nv,m0,len,d);
                   6949:     SG(d) = sugar;
                   6950:        fclose(s);
                   6951:     return d;
                   6952: }
                   6953:
1.102     noro     6954: void nd_det(int mod,MAT f,P *rp)
1.99      noro     6955: {
1.157     noro     6956:     VL fv,tv;
                   6957:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   6958:     pointer **m;
                   6959:     Q mone;
                   6960:     P **w;
                   6961:     P mp,r;
                   6962:     NDV **dm;
                   6963:     NDV *t,*mi,*mj;
                   6964:     NDV d,s,mij,mjj;
                   6965:     ND u;
                   6966:     NMV nmv;
                   6967:     UINT *bound;
                   6968:     PGeoBucket bucket;
                   6969:     struct order_spec *ord;
                   6970:     Q dq,dt,ds;
                   6971:     N gn,qn,dn0,nm,dn;
                   6972:
                   6973:     create_order_spec(0,0,&ord);
                   6974:     nd_init_ord(ord);
                   6975:     get_vars((Obj)f,&fv);
                   6976:     if ( f->row != f->col )
                   6977:         error("nd_det : non-square matrix");
                   6978:     n = f->row;
                   6979:     m = f->body;
                   6980:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   6981:
                   6982:     if ( !nvar ) {
                   6983:         if ( !mod )
                   6984:             detp(CO,(P **)m,n,rp);
                   6985:         else {
                   6986:             w = (P **)almat_pointer(n,n);
                   6987:             for ( i = 0; i < n; i++ )
                   6988:                 for ( j = 0; j < n; j++ )
                   6989:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   6990:             detmp(CO,mod,w,n,&mp);
                   6991:             mptop(mp,rp);
                   6992:         }
                   6993:         return;
                   6994:     }
                   6995:
                   6996:     if ( !mod ) {
                   6997:         w = (P **)almat_pointer(n,n);
                   6998:         dq = ONE;
                   6999:         for ( i = 0; i < n; i++ ) {
                   7000:             dn0 = ONEN;
                   7001:             for ( j = 0; j < n; j++ ) {
                   7002:                 if ( !m[i][j] ) continue;
                   7003:                 lgp(m[i][j],&nm,&dn);
                   7004:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   7005:             }
                   7006:             if ( !UNIN(dn0) ) {
                   7007:                 NTOQ(dn0,1,ds);
                   7008:                 for ( j = 0; j < n; j++ )
                   7009:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   7010:                 mulq(dq,ds,&dt); dq = dt;
                   7011:             } else
                   7012:                 for ( j = 0; j < n; j++ )
                   7013:                     w[i][j] = (P)m[i][j];
                   7014:         }
                   7015:         m = (pointer **)w;
                   7016:     }
                   7017:
1.178     noro     7018:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     7019:         for ( j = 0; j < n; j++ )
                   7020:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   7021:                 e = getdeg(tv->v,(P)m[i][j]);
                   7022:                 max = MAX(e,max);
                   7023:             }
                   7024:     nd_setup_parameters(nvar,max);
                   7025:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     7026:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     7027:         for ( j = 0; j < n; j++ ) {
                   7028:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   7029:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   7030:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   7031:         }
                   7032:     d = ptondv(CO,fv,(P)ONE);
                   7033:     if ( mod ) ndv_mod(mod,d);
                   7034:     chsgnq(ONE,&mone);
                   7035:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   7036:         if ( DP_Print ) fprintf(stderr,".",j);
                   7037:         for ( i = j; i < n && !dm[i][j]; i++ );
                   7038:         if ( i == n ) {
                   7039:             *rp = 0;
                   7040:             return;
                   7041:         }
                   7042:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   7043:         for ( k = j; k < n; k++ )
                   7044:             for ( l = j; l < n; l++ )
                   7045:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   7046:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   7047:                 }
                   7048:         if ( k0 != j ) {
                   7049:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   7050:             sgn = -sgn;
                   7051:         }
                   7052:         if ( l0 != j ) {
                   7053:             for ( k = j; k < n; k++ ) {
                   7054:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   7055:             }
                   7056:             sgn = -sgn;
                   7057:         }
                   7058:         bound = nd_det_compute_bound(dm,n,j);
                   7059:         for ( k = 0; k < nd_nvar; k++ )
                   7060:             if ( bound[k]*2 > nd_mask0 ) break;
                   7061:         if ( k < nd_nvar )
                   7062:             nd_det_reconstruct(dm,n,j,d);
                   7063:
                   7064:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   7065: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   7066:             mi = dm[i]; mij = mi[j];
                   7067:             if ( mod )
                   7068:                 ndv_mul_c(mod,mij,mod-1);
                   7069:             else
                   7070:                 ndv_mul_c_q(mij,mone);
                   7071:             for ( k = j+1; k < n; k++ ) {
                   7072: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   7073:                 bucket = create_pbucket();
                   7074:                 if ( mi[k] ) {
                   7075:                     nmv = BDY(mjj); len = LEN(mjj);
                   7076:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   7077:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   7078:                         add_pbucket(mod,bucket,u);
                   7079:                     }
                   7080:                 }
                   7081:                 if ( mj[k] && mij ) {
                   7082:                     nmv = BDY(mij); len = LEN(mij);
                   7083:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   7084:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   7085:                         add_pbucket(mod,bucket,u);
                   7086:                     }
                   7087:                 }
                   7088:                 u = nd_quo(mod,bucket,d);
                   7089:                 mi[k] = ndtondv(mod,u);
                   7090:             }
                   7091: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   7092:         }
                   7093:         d = mjj;
                   7094:     }
                   7095:     if ( DP_Print ) fprintf(stderr,"\n",k);
                   7096:     if ( sgn < 0 )
                   7097:         if ( mod )
                   7098:             ndv_mul_c(mod,d,mod-1);
                   7099:         else
                   7100:             ndv_mul_c_q(d,mone);
                   7101:     r = ndvtop(mod,CO,fv,d);
                   7102:     if ( !mod && !UNIQ(dq) )
                   7103:         divsp(CO,r,(P)dq,rp);
                   7104:     else
                   7105:         *rp = r;
1.99      noro     7106: }
                   7107:
1.102     noro     7108: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     7109: {
1.157     noro     7110:     NM mr,mr0;
                   7111:     NM tnm;
                   7112:     NMV m;
                   7113:     UINT *d0,*dt,*dm;
                   7114:     int c,n,td,i,c1,c2,len;
                   7115:     Q q;
                   7116:     ND r;
                   7117:
                   7118:     if ( !p ) return 0;
                   7119:     else {
                   7120:         n = NV(p); m = BDY(p); len = LEN(p);
                   7121:         d0 = DL(m0);
                   7122:         td = TD(d);
                   7123:         mr0 = 0;
                   7124:         NEWNM(tnm);
                   7125:         if ( mod ) {
                   7126:             c = CM(m0);
                   7127:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7128:                 ndl_add(DL(m),d0,DL(tnm));
                   7129:                 if ( ndl_reducible(DL(tnm),d) ) {
                   7130:                     NEXTNM(mr0,mr);
                   7131:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   7132:                     ndl_copy(DL(tnm),DL(mr));
                   7133:                 }
                   7134:             }
                   7135:         } else {
                   7136:             q = CQ(m0);
                   7137:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7138:                 ndl_add(DL(m),d0,DL(tnm));
                   7139:                 if ( ndl_reducible(DL(tnm),d) ) {
                   7140:                     NEXTNM(mr0,mr);
                   7141:                     mulq(CQ(m),q,&CQ(mr));
                   7142:                     ndl_copy(DL(tnm),DL(mr));
                   7143:                 }
                   7144:             }
                   7145:         }
                   7146:         if ( !mr0 )
                   7147:             return 0;
                   7148:         else {
                   7149:             NEXT(mr) = 0;
                   7150:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   7151:             MKND(NV(p),mr0,len,r);
                   7152:             SG(r) = SG(p) + TD(d0);
                   7153:             return r;
                   7154:         }
                   7155:     }
1.114     noro     7156: }
                   7157:
                   7158: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   7159: {
1.157     noro     7160:     int i,obpe,oadv,h,k,l;
                   7161:     static NM prev_nm_free_list;
                   7162:     EPOS oepos;
                   7163:
                   7164:     obpe = nd_bpe;
                   7165:     oadv = nmv_adv;
                   7166:     oepos = nd_epos;
                   7167:     if ( obpe < 2 ) nd_bpe = 2;
                   7168:     else if ( obpe < 3 ) nd_bpe = 3;
                   7169:     else if ( obpe < 4 ) nd_bpe = 4;
                   7170:     else if ( obpe < 5 ) nd_bpe = 5;
                   7171:     else if ( obpe < 6 ) nd_bpe = 6;
                   7172:     else if ( obpe < 8 ) nd_bpe = 8;
                   7173:     else if ( obpe < 10 ) nd_bpe = 10;
                   7174:     else if ( obpe < 16 ) nd_bpe = 16;
                   7175:     else if ( obpe < 32 ) nd_bpe = 32;
                   7176:     else error("nd_det_reconstruct : exponent too large");
                   7177:
                   7178:     nd_setup_parameters(nd_nvar,0);
                   7179:     prev_nm_free_list = _nm_free_list;
                   7180:     _nm_free_list = 0;
                   7181:     for ( k = j; k < n; k++ )
                   7182:         for (l = j; l < n; l++ )
                   7183:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   7184:     ndv_realloc(d,obpe,oadv,oepos);
                   7185:     prev_nm_free_list = 0;
1.114     noro     7186: #if 0
1.157     noro     7187:     GC_gcollect();
1.114     noro     7188: #endif
                   7189: }
                   7190:
1.153     noro     7191: /* returns a UINT array containing degree bounds */
                   7192:
1.114     noro     7193: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   7194: {
1.157     noro     7195:     UINT *d0,*d1,*d,*t,*r;
                   7196:     int k,l,i;
1.114     noro     7197:
1.157     noro     7198:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   7199:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   7200:     for ( k = j; k < n; k++ )
                   7201:         for ( l = j; l < n; l++ )
                   7202:             if ( dm[k][l] ) {
                   7203:                 d = ndv_compute_bound(dm[k][l]);
                   7204:                 for ( i = 0; i < nd_nvar; i++ )
                   7205:                     d0[i] = MAX(d0[i],d[i]);
                   7206:             }
                   7207:     return d0;
1.117     noro     7208: }
                   7209:
                   7210: DL nd_separate_d(UINT *d,UINT *trans)
                   7211: {
1.157     noro     7212:     int n,td,i,e,j;
                   7213:     DL a;
1.117     noro     7214:
1.157     noro     7215:     ndl_zero(trans);
                   7216:     td = 0;
                   7217:     for ( i = 0; i < nd_ntrans; i++ ) {
                   7218:         e = GET_EXP(d,i);
                   7219:         PUT_EXP(trans,i,e);
                   7220:         td += MUL_WEIGHT(e,i);
                   7221:     }
                   7222:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   7223:         /* homogenized */
                   7224:         i = nd_nvar-1;
                   7225:         e = GET_EXP(d,i);
                   7226:         PUT_EXP(trans,i,e);
                   7227:         td += MUL_WEIGHT(e,i);
                   7228:     }
                   7229:     TD(trans) = td;
                   7230:     if ( nd_blockmask) ndl_weight_mask(trans);
                   7231:     NEWDL(a,nd_nalg);
                   7232:     td = 0;
                   7233:     for ( i = 0; i < nd_nalg; i++ ) {
                   7234:         j = nd_ntrans+i;
                   7235:         e = GET_EXP(d,j);
                   7236:         a->d[i] = e;
                   7237:         td += e;
                   7238:     }
                   7239:     a->td = td;
                   7240:     return a;
1.117     noro     7241: }
                   7242:
1.118     noro     7243: int nd_monic(int mod,ND *p)
1.117     noro     7244: {
1.157     noro     7245:     UINT *trans,*t;
                   7246:     DL alg;
                   7247:     MP mp0,mp;
                   7248:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   7249:     ND r;
                   7250:     DL dl;
                   7251:     DP nm;
                   7252:     NDV ndv;
                   7253:     DAlg inv,cd;
                   7254:     ND s,c;
                   7255:     Q l,mul;
                   7256:     N ln;
                   7257:     int n,ntrans,i,e,td,is_lc,len;
                   7258:     NumberField nf;
                   7259:     struct oEGT eg0,eg1;
                   7260:
                   7261:     if ( !(nf = get_numberfield()) )
                   7262:         error("nd_monic : current_numberfield is not set");
                   7263:
                   7264:     /* Q coef -> DAlg coef */
                   7265:     NEWNM(ma0); ma = ma0;
                   7266:     m = BDY(*p);
                   7267:     is_lc = 1;
                   7268:     while ( 1 ) {
                   7269:         NEWMP(mp0); mp = mp0;
                   7270:         mp->c = (P)CQ(m);
                   7271:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   7272:         NEWNM(mb);
                   7273:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   7274:             alg = nd_separate_d(DL(m),DL(mb));
                   7275:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   7276:                 break;
                   7277:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   7278:         }
                   7279:         NEXT(mp) = 0;
                   7280:         MKDP(nd_nalg,mp0,nm);
                   7281:         MKDAlg(nm,ONE,cd);
                   7282:         if ( is_lc == 1 ) {
                   7283:             /* if the lc is a rational number, we have nothing to do */
                   7284:             if ( !mp0->dl->td )
                   7285:                 return 1;
                   7286:
                   7287:             get_eg(&eg0);
                   7288:             invdalg(cd,&inv);
                   7289:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   7290:             /* check the validity of inv */
                   7291:             if ( mod && !rem(NM(inv->dn),mod) )
                   7292:                 return 0;
                   7293:             CA(ma) = nf->one;
                   7294:             is_lc = 0;
                   7295:             ln = ONEN;
                   7296:         } else {
                   7297:             muldalg(cd,inv,&CA(ma));
                   7298:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   7299:         }
                   7300:         if ( m ) {
                   7301:             NEXT(ma) = mb; ma = mb;
                   7302:         } else {
                   7303:             NEXT(ma) = 0;
                   7304:             break;
                   7305:         }
                   7306:     }
                   7307:     /* l = lcm(denoms) */
                   7308:     NTOQ(ln,1,l);
                   7309:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   7310:         divq(l,CA(m)->dn,&mul);
                   7311:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   7312:             NEXTNM(mr0,mr);
                   7313:             mulq((Q)mp->c,mul,&CQ(mr));
                   7314:             dl = mp->dl;
                   7315:             td = TD(DL(m));
                   7316:             ndl_copy(DL(m),DL(mr));
                   7317:             for ( i = 0; i < nd_nalg; i++ ) {
                   7318:                 e = dl->d[i];
                   7319:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   7320:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   7321:             }
1.163     noro     7322:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     7323:             TD(DL(mr)) = td;
                   7324:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   7325:         }
                   7326:     }
                   7327:     NEXT(mr) = 0;
                   7328:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   7329:     MKND(NV(*p),mr0,len,r);
                   7330:     /* XXX */
                   7331:     SG(r) = SG(*p);
                   7332:     nd_free(*p);
                   7333:     *p = r;
                   7334:     return 1;
1.59      noro     7335: }
1.167     noro     7336:
                   7337: NODE reverse_node(NODE n)
                   7338: {
                   7339:     NODE t,t1;
                   7340:
                   7341:     for ( t = 0; n; n = NEXT(n) ) {
                   7342:         MKNODE(t1,BDY(n),t); t = t1;
                   7343:     }
                   7344:     return t;
                   7345: }
                   7346:
                   7347: P ndc_div(int mod,union oNDC a,union oNDC b)
                   7348: {
                   7349:     union oNDC c;
                   7350:     int inv,t;
                   7351:
                   7352:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
                   7353:     else if ( mod ) {
                   7354:         inv = invm(b.m,mod);
                   7355:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   7356:     } else if ( nd_vc )
                   7357:        divsp(nd_vc,a.p,b.p,&c.p);
                   7358:     else
                   7359:        divq(a.z,b.z,&c.z);
                   7360:     return ndctop(mod,c);
                   7361: }
                   7362:
                   7363: P ndctop(int mod,union oNDC c)
                   7364: {
                   7365:     Q q;
                   7366:     int e;
                   7367:     GFS gfs;
                   7368:
                   7369:     if ( mod == -1 ) {
                   7370:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   7371:     } else if ( mod ) {
                   7372:         STOQ(c.m,q); return (P)q;
                   7373:     } else
                   7374:         return (P)c.p;
                   7375: }
                   7376:
                   7377: /* [0,0,0,cont] = p -> p/cont */
                   7378:
                   7379: void finalize_tracelist(int i,P cont)
                   7380: {
                   7381:         LIST l;
                   7382:         NODE node;
                   7383:      Q iq;
                   7384:
                   7385:         if ( !UNIQ(cont) ) {
1.196     noro     7386:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     7387:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   7388:                 nd_tracelist = node;
                   7389:         }
                   7390:      STOQ(i,iq);
                   7391:      nd_tracelist = reverse_node(nd_tracelist);
                   7392:      MKLIST(l,nd_tracelist);
                   7393:      node = mknode(2,iq,l); MKLIST(l,node);
                   7394:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   7395:      nd_alltracelist = node; nd_tracelist = 0;
                   7396: }
                   7397:
                   7398: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   7399: {
                   7400:     int n,i,j;
                   7401:        int *ind;
                   7402:        NODE t;
                   7403:
                   7404:     n = length(g);
                   7405:        ind = (int *)MALLOC(n*sizeof(int));
                   7406:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   7407:                j = (long)BDY(t); ind[i] = j;
                   7408:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   7409:        }
                   7410:        if ( indp ) *indp = ind;
                   7411: }
1.172     noro     7412:
                   7413: void parse_nd_option(NODE opt)
                   7414: {
1.187     noro     7415:     NODE t,p,u;
                   7416:        int i,s;
1.172     noro     7417:     char *key;
                   7418:     Obj value;
                   7419:
1.187     noro     7420:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.208     noro     7421:        nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
1.209     noro     7422:        nd_splist = 0; nd_check_splist = 0;
1.172     noro     7423:     for ( t = opt; t; t = NEXT(t) ) {
                   7424:         p = BDY((LIST)BDY(t));
                   7425:         key = BDY((STRING)BDY(p));
                   7426:         value = (Obj)BDY(NEXT(p));
                   7427:         if ( !strcmp(key,"gentrace") )
                   7428:             nd_gentrace = value?1:0;
                   7429:         else if ( !strcmp(key,"gensyz") )
                   7430:             nd_gensyz = value?1:0;
1.173     noro     7431:         else if ( !strcmp(key,"nora") )
                   7432:             nd_nora = value?1:0;
1.187     noro     7433:         else if ( !strcmp(key,"gbblock") ) {
                   7434:                        if ( !value || OID(value) != O_LIST )
                   7435:                                error("nd_* : invalid value for gbblock option");
                   7436:                        u = BDY((LIST)value);
1.189     noro     7437:             nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.187     noro     7438:                        for ( i = 0; u; u = NEXT(u) ) {
                   7439:                                p = BDY((LIST)BDY(u));
                   7440:                                s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   7441:                                nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   7442:                        }
                   7443:                        nd_gbblock[i] = -1;
1.192     noro     7444:                } else if ( !strcmp(key,"newelim") )
                   7445:             nd_newelim = value?1:0;
1.195     noro     7446:                else if ( !strcmp(key,"intersect") )
                   7447:             nd_intersect = value?1:0;
1.208     noro     7448:                else if ( !strcmp(key,"trace") ) {
                   7449:            u = BDY((LIST)value);
                   7450:                   nd_nzlist = BDY((LIST)ARG2(u));
                   7451:                   nd_bpe = QTOS((Q)ARG3(u));
1.209     noro     7452:                } else if ( !strcmp(key,"splist") )
                   7453:             nd_splist = value?1:0;
                   7454:                else if ( !strcmp(key,"check_splist") ) {
                   7455:                        nd_check_splist = BDY((LIST)value);
1.208     noro     7456:                }
1.172     noro     7457:     }
                   7458: }
1.204     noro     7459:
                   7460: ND mdptond(DP d);
                   7461: ND nd_mul_nm(int mod,NM m0,ND p);
1.207     noro     7462: ND *btog(NODE ti,ND **p,int nb,int mod);
                   7463: ND btog_one(NODE ti,ND *p,int nb,int mod);
1.204     noro     7464: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
1.205     noro     7465: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
1.204     noro     7466:
                   7467: /* d:monomial */
                   7468: ND mdptond(DP d)
                   7469: {
                   7470:   NM m;
                   7471:   ND r;
                   7472:
                   7473:   if ( OID(d) == 1 )
                   7474:        r = ptond(CO,CO,(P)d);
                   7475:   else {
                   7476:     NEWNM(m);
                   7477:     dltondl(NV(d),BDY(d)->dl,DL(m));
                   7478:     CQ(m) = (Q)BDY(d)->c;
                   7479:     NEXT(m) = 0;
                   7480:     MKND(NV(d),m,1,r);
                   7481:   }
                   7482:     return r;
                   7483: }
                   7484:
                   7485: ND nd_mul_nm(int mod,NM m0,ND p)
                   7486: {
                   7487:   UINT *d0;
                   7488:   int c0,c1,c;
                   7489:   NM tm,mr,mr0;
                   7490:   ND r;
                   7491:
                   7492:   if ( !p ) return 0;
                   7493:   d0 = DL(m0);
                   7494:   c0 = CM(m0);
                   7495:   mr0 = 0;
                   7496:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   7497:     NEXTNM(mr0,mr);
                   7498:        c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   7499:        ndl_add(d0,DL(tm),DL(mr));
                   7500:   }
                   7501:   NEXT(mr) = 0;
                   7502:   MKND(NV(p),mr0,LEN(p),r);
                   7503:   return r;
                   7504: }
                   7505:
1.207     noro     7506: ND *btog(NODE ti,ND **p,int nb,int mod)
1.204     noro     7507: {
                   7508:   PGeoBucket *r;
                   7509:   int i,ci;
                   7510:   NODE t,s;
                   7511:   ND m,tp;
                   7512:   ND *pi,*rd;
                   7513:   P c;
                   7514:
                   7515:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   7516:   for ( i = 0; i < nb; i++ )
                   7517:        r[i] = create_pbucket();
                   7518:   for ( t = ti; t; t = NEXT(t) ) {
                   7519:        s = BDY((LIST)BDY(t));
                   7520:     if ( ARG0(s) ) {
                   7521:          m = mdptond((DP)ARG2(s));
                   7522:          ptomp(mod,(P)HCQ(m),&c);
                   7523:          if ( ci = ((MQ)c)->cont ) {
                   7524:            HCM(m) = ci;
                   7525:            pi = p[QTOS((Q)ARG1(s))];
                   7526:            for ( i = 0; i < nb; i++ ) {
                   7527:                  tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   7528:              add_pbucket(mod,r[i],tp);
                   7529:            }
                   7530:          }
                   7531:          ci = 1;
                   7532:     } else {
                   7533:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   7534:          ci = invm(ci,mod);
                   7535:        }
                   7536:   }
                   7537:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   7538:   for ( i = 0; i < nb; i++ )
                   7539:        rd[i] = normalize_pbucket(mod,r[i]);
                   7540:   if ( ci != 1 )
                   7541:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   7542:    return rd;
                   7543: }
                   7544:
1.207     noro     7545: ND btog_one(NODE ti,ND *p,int nb,int mod)
1.205     noro     7546: {
                   7547:   PGeoBucket r;
1.206     noro     7548:   int i,ci,j;
1.205     noro     7549:   NODE t,s;
                   7550:   ND m,tp;
                   7551:   ND pi,rd;
                   7552:   P c;
                   7553:
                   7554:   r = create_pbucket();
                   7555:   for ( t = ti; t; t = NEXT(t) ) {
                   7556:        s = BDY((LIST)BDY(t));
                   7557:     if ( ARG0(s) ) {
                   7558:          m = mdptond((DP)ARG2(s));
                   7559:          ptomp(mod,(P)HCQ(m),&c);
                   7560:          if ( ci = ((MQ)c)->cont ) {
                   7561:            HCM(m) = ci;
1.206     noro     7562:            pi = p[j=QTOS((Q)ARG1(s))];
                   7563:                if ( !pi ) {
                   7564:                  pi = nd_load_mod(j);
                   7565:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   7566:                  nd_free(pi);
                   7567:              add_pbucket(mod,r,tp);
                   7568:                } else {
                   7569:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   7570:              add_pbucket(mod,r,tp);
                   7571:            }
1.205     noro     7572:          }
                   7573:          ci = 1;
                   7574:     } else {
                   7575:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   7576:          ci = invm(ci,mod);
                   7577:        }
                   7578:   }
                   7579:   rd = normalize_pbucket(mod,r);
1.206     noro     7580:   free_pbucket(r);
1.205     noro     7581:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   7582:   return rd;
                   7583: }
1.204     noro     7584:
                   7585: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   7586: {
                   7587:   int i,j,n,m,nb,pi0,pi1,nvar;
                   7588:   VL fv,tv,vv;
                   7589:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   7590:   ND **p;
                   7591:   ND *c;
                   7592:   ND u;
                   7593:   P inv;
                   7594:   MAT mat;
                   7595:
                   7596:   parse_nd_option(current_option);
                   7597:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   7598:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7599:   switch ( ord->id ) {
                   7600:     case 1:
                   7601:       if ( ord->nv != nvar )
                   7602:         error("nd_check : invalid order specification");
                   7603:       break;
                   7604:     default:
                   7605:       break;
                   7606:   }
                   7607:   nd_init_ord(ord);
                   7608: #if 0
                   7609:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   7610: #else
                   7611:   nd_bpe = 32;
                   7612: #endif
                   7613:   nd_setup_parameters(nvar,0);
                   7614:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   7615:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   7616:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   7617:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   7618:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   7619:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   7620:        if ( j > i ) i = j;
                   7621:   }
                   7622:   n = i+1;
                   7623:   nb = length(BDY(f));
                   7624:   p = (ND **)MALLOC(n*sizeof(ND *));
                   7625:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   7626:     pi = BDY((LIST)BDY(t));
                   7627:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   7628:        p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   7629:        ptomp(mod,(P)ARG2(pi),&inv);
                   7630:        u = ptond(CO,vv,(P)ONE);
                   7631:        HCM(u) = ((MQ)inv)->cont;
                   7632:        c[pi1] = u;
                   7633:   }
                   7634:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   7635:        printf("%d ",i); fflush(stdout);
                   7636:     ti = BDY((LIST)BDY(t));
1.207     noro     7637:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     7638:     if ( j == 441 )
                   7639:                printf("afo");
                   7640:   }
                   7641:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   7642:        printf("%d ",i); fflush(stdout);
                   7643:     ti = BDY((LIST)BDY(t));
1.207     noro     7644:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     7645:     if ( j == 441 )
                   7646:                printf("afo");
                   7647:   }
                   7648:   m = length(ind);
                   7649:   MKMAT(mat,nb,m);
                   7650:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
                   7651:     for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
                   7652:                BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   7653:   return mat;
                   7654: }
                   7655:
1.205     noro     7656: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   7657:   LIST tlist,int pos,MAT *rp)
                   7658: {
                   7659:   int i,j,n,m,nb,pi0,pi1,nvar;
                   7660:   VL fv,tv,vv;
                   7661:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   7662:   ND *p;
                   7663:   ND *c;
                   7664:   ND u;
                   7665:   P inv;
                   7666:   VECT vect;
                   7667:
                   7668:   parse_nd_option(current_option);
                   7669:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   7670:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7671:   switch ( ord->id ) {
                   7672:     case 1:
                   7673:       if ( ord->nv != nvar )
                   7674:         error("nd_check : invalid order specification");
                   7675:       break;
                   7676:     default:
                   7677:       break;
                   7678:   }
                   7679:   nd_init_ord(ord);
                   7680: #if 0
                   7681:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   7682: #else
                   7683:   nd_bpe = 32;
                   7684: #endif
                   7685:   nd_setup_parameters(nvar,0);
                   7686:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   7687:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   7688:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   7689:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   7690:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   7691:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   7692:        if ( j > i ) i = j;
                   7693:   }
                   7694:   n = i+1;
                   7695:   nb = length(BDY(f));
                   7696:   p = (ND *)MALLOC(n*sizeof(ND *));
                   7697:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   7698:     pi = BDY((LIST)BDY(t));
                   7699:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   7700:        if ( pi1 == pos ) {
                   7701:          ptomp(mod,(P)ARG2(pi),&inv);
                   7702:          u = ptond(CO,vv,(P)ONE);
                   7703:          HCM(u) = ((MQ)inv)->cont;
                   7704:          p[pi0] = u;
                   7705:     }
                   7706:   }
                   7707:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   7708:        printf("%d ",i); fflush(stdout);
                   7709:     ti = BDY((LIST)BDY(t));
1.207     noro     7710:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     7711:     if ( Demand ) {
                   7712:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   7713:        }
1.205     noro     7714:   }
                   7715:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   7716:        printf("%d ",i); fflush(stdout);
                   7717:     ti = BDY((LIST)BDY(t));
1.207     noro     7718:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     7719:     if ( Demand ) {
                   7720:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   7721:        }
1.205     noro     7722:   }
                   7723:   m = length(ind);
                   7724:   MKVECT(vect,m);
1.206     noro     7725:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
                   7726:        u = p[QTOS((Q)BDY(t))];
                   7727:        if ( !u ) {
                   7728:          u = nd_load_mod(QTOS((Q)BDY(t)));
                   7729:          BDY(vect)[j] = ndtodp(mod,u);
                   7730:          nd_free(u);
                   7731:        } else
                   7732:          BDY(vect)[j] = ndtodp(mod,u);
                   7733:   }
1.205     noro     7734:   return vect;
                   7735: }

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