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