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

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

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