Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.119
1.119 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.118 2004/12/04 09:39:27 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.63 noro 2217: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1 noro 2218: {
1.119 ! noro 2219: VL tv,fv,vv,vc,av;
! 2220: NODE fd,fd0,r,r0,t,x,s,xx,alist;
! 2221: int e,max,nvar,i;
1.69 noro 2222: NDV b;
1.119 ! noro 2223: int ishomo,nalg;
! 2224: Alg alpha,dp;
! 2225: P p;
! 2226: LIST f1,f2;
! 2227: Obj obj;
! 2228: NumberField nf;
1.1 noro 2229:
1.78 noro 2230: if ( !m && Demand ) nd_demand = 1;
2231: else nd_demand = 0;
2232:
1.103 noro 2233: #if 0
1.74 noro 2234: ndv_alloc = 0;
1.103 noro 2235: #endif
1.1 noro 2236: get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74 noro 2237: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88 noro 2238: switch ( ord->id ) {
2239: case 1:
2240: if ( ord->nv != nvar )
2241: error("nd_{gr,f4} : invalid order specification");
2242: break;
2243: default:
2244: break;
2245: }
1.119 ! noro 2246: nd_nalg = 0;
! 2247: if ( !m ) {
! 2248: get_algtree((Obj)f,&av);
! 2249: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
! 2250: nd_nalg = nalg;
! 2251: /* #i -> t#i */
! 2252: if ( nalg ) {
! 2253: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
! 2254: NEXTNODE(alist,t); MKV(tv->v,p);
! 2255: MKAlg(p,alpha); BDY(t) = (pointer)alpha;
! 2256: tv->v = tv->v->priv;
! 2257: }
! 2258: NEXT(t) = 0;
! 2259: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
! 2260: NEXT(tv) = av;
! 2261: ord = append_block(ord,nvar,nalg,2);
! 2262: nvar += nalg;
! 2263: setfield_dalg(alist);
! 2264: nf = get_numberfield();
! 2265: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
! 2266: MKAlg(nf->defpoly[i],dp);
! 2267: MKNODE(s,dp,t); t = s;
! 2268: }
! 2269: MKLIST(f1,t);
! 2270: algobjtorat(f1,&f2); f = f2;
! 2271: }
! 2272: }
1.32 noro 2273: nd_init_ord(ord);
1.61 noro 2274: for ( t = BDY(f), max = 0; t; t = NEXT(t) )
2275: for ( tv = vv; tv; tv = NEXT(tv) ) {
2276: e = getdeg(tv->v,(P)BDY(t));
2277: max = MAX(e,max);
2278: }
2279: nd_setup_parameters(nvar,max);
1.77 noro 2280: ishomo = 1;
1.1 noro 2281: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61 noro 2282: b = (pointer)ptondv(CO,vv,(P)BDY(t));
1.77 noro 2283: if ( ishomo )
2284: ishomo = ishomo && ndv_ishomo(b);
1.69 noro 2285: if ( m ) ndv_mod(m,b);
1.61 noro 2286: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1 noro 2287: }
2288: if ( fd0 ) NEXT(fd) = 0;
1.61 noro 2289: ndv_setup(m,0,fd0);
1.77 noro 2290: x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82 noro 2291: nd_demand = 0;
1.61 noro 2292: x = ndv_reducebase(x);
2293: x = ndv_reduceall(m,x);
1.23 noro 2294: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1 noro 2295: NEXTNODE(r0,r);
1.61 noro 2296: BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.119 ! noro 2297: if ( nalg ) {
! 2298: p = BDY(r);
! 2299: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
! 2300: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
! 2301: }
! 2302: BDY(r) = p;
! 2303: }
1.20 noro 2304: }
2305: if ( r0 ) NEXT(r) = 0;
2306: MKLIST(*rp,r0);
1.103 noro 2307: #if 0
1.74 noro 2308: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103 noro 2309: #endif
1.20 noro 2310: }
2311:
1.52 noro 2312: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20 noro 2313: {
1.89 noro 2314: struct order_spec *ord1;
1.119 ! noro 2315: VL tv,fv,vv,vc,av;
! 2316: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
1.61 noro 2317: int m,nocheck,nvar,mindex,e,max;
2318: NDV c;
2319: NMV a;
1.27 noro 2320: P p;
1.61 noro 2321: EPOS oepos;
1.119 ! noro 2322: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg;
! 2323: Alg alpha,dp;
! 2324: P poly;
! 2325: LIST f1,f2;
! 2326: Obj obj;
! 2327: NumberField nf;
1.20 noro 2328:
2329: get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74 noro 2330: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88 noro 2331: switch ( ord->id ) {
2332: case 1:
2333: if ( ord->nv != nvar )
2334: error("nd_gr_trace : invalid order specification");
2335: break;
2336: default:
2337: break;
2338: }
1.119 ! noro 2339:
! 2340: get_algtree((Obj)f,&av);
! 2341: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
! 2342: nd_nalg = nalg;
! 2343: /* #i -> t#i */
! 2344: if ( nalg ) {
! 2345: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
! 2346: NEXTNODE(alist,t); MKV(tv->v,poly);
! 2347: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
! 2348: tv->v = tv->v->priv;
! 2349: }
! 2350: NEXT(t) = 0;
! 2351: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
! 2352: NEXT(tv) = av;
! 2353: ord = append_block(ord,nvar,nalg,2);
! 2354: nvar += nalg;
! 2355: setfield_dalg(alist);
! 2356: nf = get_numberfield();
! 2357: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
! 2358: MKAlg(nf->defpoly[i],dp);
! 2359: MKNODE(s,dp,t); t = s;
! 2360: }
! 2361: MKLIST(f1,t);
! 2362: algobjtorat(f1,&f2); f = f2;
! 2363: }
! 2364:
1.52 noro 2365: nocheck = 0;
2366: mindex = 0;
2367:
1.78 noro 2368: if ( Demand ) nd_demand = 1;
2369: else nd_demand = 0;
2370:
1.52 noro 2371: /* setup modulus */
2372: if ( trace < 0 ) {
2373: trace = -trace;
2374: nocheck = 1;
2375: }
2376: m = trace > 1 ? trace : get_lprime(mindex);
1.61 noro 2377: for ( t = BDY(f), max = 0; t; t = NEXT(t) )
2378: for ( tv = vv; tv; tv = NEXT(tv) ) {
2379: e = getdeg(tv->v,(P)BDY(t));
2380: max = MAX(e,max);
1.23 noro 2381: }
1.61 noro 2382: nd_init_ord(ord);
2383: nd_setup_parameters(nvar,max);
2384: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82 noro 2385: ishomo = 1;
1.61 noro 2386: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
2387: c = ptondv(CO,vv,(P)BDY(t));
1.82 noro 2388: if ( ishomo )
2389: ishomo = ishomo && ndv_ishomo(c);
1.61 noro 2390: if ( c ) {
2391: NEXTNODE(in0,in); BDY(in) = (pointer)c;
2392: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23 noro 2393: }
1.61 noro 2394: }
2395: if ( in0 ) NEXT(in) = 0;
2396: if ( fd0 ) NEXT(fd) = 0;
1.82 noro 2397: if ( !ishomo && homo ) {
1.61 noro 2398: for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
2399: c = (NDV)BDY(t); len = LEN(c);
2400: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2401: wmax = MAX(TD(DL(a)),wmax);
2402: }
2403: homogenize_order(ord,nvar,&ord1);
1.89 noro 2404: nd_init_ord(ord1);
1.61 noro 2405: nd_setup_parameters(nvar+1,wmax);
2406: for ( t = fd0; t; t = NEXT(t) )
2407: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27 noro 2408: }
1.52 noro 2409: while ( 1 ) {
1.77 noro 2410: if ( Demand )
2411: nd_demand = 1;
1.61 noro 2412: ndv_setup(m,1,fd0);
1.82 noro 2413: cand = nd_gb_trace(m,ishomo || homo);
1.52 noro 2414: if ( !cand ) {
2415: /* failure */
1.61 noro 2416: if ( trace > 1 ) { *rp = 0; return; }
2417: else m = get_lprime(++mindex);
1.52 noro 2418: continue;
2419: }
1.82 noro 2420: if ( !ishomo && homo ) {
1.27 noro 2421: /* dehomogenization */
1.61 noro 2422: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45 noro 2423: nd_init_ord(ord);
1.61 noro 2424: nd_setup_parameters(nvar,0);
1.27 noro 2425: }
1.77 noro 2426: nd_demand = 0;
1.61 noro 2427: cand = ndv_reducebase(cand);
2428: cand = ndv_reduceall(0,cand);
1.78 noro 2429: cbpe = nd_bpe;
1.61 noro 2430: if ( nocheck )
2431: break;
2432: if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52 noro 2433: /* success */
2434: break;
2435: else if ( trace > 1 ) {
2436: /* failure */
2437: *rp = 0; return;
1.61 noro 2438: } else {
1.52 noro 2439: /* try the next modulus */
2440: m = get_lprime(++mindex);
1.61 noro 2441: /* reset the parameters */
1.82 noro 2442: if ( !ishomo && homo ) {
1.89 noro 2443: nd_init_ord(ord1);
1.61 noro 2444: nd_setup_parameters(nvar+1,wmax);
2445: } else {
2446: nd_init_ord(ord);
2447: nd_setup_parameters(nvar,max);
2448: }
2449: }
1.52 noro 2450: }
1.27 noro 2451: /* dp->p */
1.61 noro 2452: nd_bpe = cbpe;
1.73 noro 2453: nd_setup_parameters(nd_nvar,0);
1.119 ! noro 2454: for ( r = cand; r; r = NEXT(r) ) {
! 2455: BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
! 2456: if ( nalg ) {
! 2457: poly = BDY(r);
! 2458: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
! 2459: substr(CO,0,(Obj)poly,tv->v,(Obj)BDY(s),&obj); poly = (P)obj;
! 2460: }
! 2461: BDY(r) = poly;
! 2462: }
! 2463: }
1.27 noro 2464: MKLIST(*rp,cand);
1.1 noro 2465: }
2466:
1.61 noro 2467: void dltondl(int n,DL dl,UINT *r)
1.1 noro 2468: {
1.61 noro 2469: UINT *d;
1.57 noro 2470: int i,j,l,s,ord_l;
1.43 noro 2471: struct order_pair *op;
1.1 noro 2472:
2473: d = dl->d;
1.41 noro 2474: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 2475: if ( nd_blockmask ) {
2476: l = nd_blockmask->n;
2477: op = nd_blockmask->order_pair;
2478: for ( j = 0, s = 0; j < l; j++ ) {
2479: ord_l = op[j].length;
1.57 noro 2480: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43 noro 2481: }
2482: TD(r) = ndl_weight(r);
1.61 noro 2483: ndl_weight_mask(r);
1.43 noro 2484: } else {
1.56 noro 2485: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43 noro 2486: TD(r) = ndl_weight(r);
2487: }
1.1 noro 2488: }
2489:
1.61 noro 2490: DL ndltodl(int n,UINT *ndl)
1.1 noro 2491: {
2492: DL dl;
2493: int *d;
1.57 noro 2494: int i,j,l,s,ord_l;
1.43 noro 2495: struct order_pair *op;
1.1 noro 2496:
2497: NEWDL(dl,n);
1.34 noro 2498: dl->td = TD(ndl);
1.1 noro 2499: d = dl->d;
1.43 noro 2500: if ( nd_blockmask ) {
2501: l = nd_blockmask->n;
2502: op = nd_blockmask->order_pair;
2503: for ( j = 0, s = 0; j < l; j++ ) {
2504: ord_l = op[j].length;
1.57 noro 2505: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43 noro 2506: }
2507: } else {
1.56 noro 2508: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43 noro 2509: }
1.1 noro 2510: return dl;
2511: }
2512:
1.61 noro 2513: void ndl_print(UINT *dl)
1.1 noro 2514: {
2515: int n;
1.57 noro 2516: int i,j,l,ord_l,s,s0;
1.43 noro 2517: struct order_pair *op;
1.1 noro 2518:
2519: n = nd_nvar;
2520: printf("<<");
1.43 noro 2521: if ( nd_blockmask ) {
2522: l = nd_blockmask->n;
2523: op = nd_blockmask->order_pair;
2524: for ( j = 0, s = s0 = 0; j < l; j++ ) {
2525: ord_l = op[j].length;
1.57 noro 2526: for ( i = 0; i < ord_l; i++, s++ )
2527: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43 noro 2528: }
2529: } else {
1.56 noro 2530: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43 noro 2531: }
1.1 noro 2532: printf(">>");
2533: }
2534:
2535: void nd_print(ND p)
2536: {
2537: NM m;
2538:
2539: if ( !p )
2540: printf("0\n");
2541: else {
2542: for ( m = BDY(p); m; m = NEXT(m) ) {
1.71 noro 2543: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
2544: else printf("+%d*",CM(m));
1.14 noro 2545: ndl_print(DL(m));
1.1 noro 2546: }
2547: printf("\n");
2548: }
2549: }
2550:
1.113 noro 2551: void nd_print_q(ND p)
1.16 noro 2552: {
2553: NM m;
2554:
2555: if ( !p )
2556: printf("0\n");
2557: else {
2558: for ( m = BDY(p); m; m = NEXT(m) ) {
2559: printf("+");
1.113 noro 2560: printexpr(CO,(Obj)CQ(m));
1.16 noro 2561: printf("*");
2562: ndl_print(DL(m));
2563: }
2564: printf("\n");
2565: }
2566: }
2567:
1.1 noro 2568: void ndp_print(ND_pairs d)
2569: {
2570: ND_pairs t;
2571:
1.34 noro 2572: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1 noro 2573: printf("\n");
2574: }
2575:
1.20 noro 2576: void nd_removecont(int mod,ND p)
1.16 noro 2577: {
2578: int i,n;
1.113 noro 2579: Q *w;
2580: Q dvr,t;
1.16 noro 2581: NM m;
1.21 noro 2582: struct oVECT v;
1.113 noro 2583: N q,r;
1.16 noro 2584:
1.71 noro 2585: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
2586: else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20 noro 2587: else {
2588: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.113 noro 2589: w = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2590: v.len = n;
2591: v.body = (pointer *)w;
1.113 noro 2592: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2593: removecont_array(w,n);
1.113 noro 2594: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16 noro 2595: }
2596: }
2597:
1.21 noro 2598: void nd_removecont2(ND p1,ND p2)
2599: {
2600: int i,n1,n2,n;
1.113 noro 2601: Q *w;
2602: Q dvr,t;
1.21 noro 2603: NM m;
2604: struct oVECT v;
1.113 noro 2605: N q,r;
1.21 noro 2606:
2607: if ( !p1 ) {
2608: nd_removecont(0,p2); return;
2609: } else if ( !p2 ) {
2610: nd_removecont(0,p1); return;
2611: }
2612: n1 = nd_length(p1);
2613: n2 = nd_length(p2);
2614: n = n1+n2;
1.113 noro 2615: w = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2616: v.len = n;
2617: v.body = (pointer *)w;
1.113 noro 2618: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
2619: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2620: removecont_array(w,n);
1.113 noro 2621: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
2622: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21 noro 2623: }
2624:
1.20 noro 2625: void ndv_removecont(int mod,NDV p)
1.16 noro 2626: {
2627: int i,len;
1.113 noro 2628: Q *w;
2629: Q dvr,t;
1.16 noro 2630: NMV m;
2631:
1.71 noro 2632: if ( mod == -1 )
2633: ndv_mul_c(mod,p,_invsf(HCM(p)));
2634: else if ( mod )
1.20 noro 2635: ndv_mul_c(mod,p,invm(HCM(p),mod));
2636: else {
2637: len = p->len;
1.113 noro 2638: w = (Q *)ALLOCA(len*sizeof(Q));
2639: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
2640: sortbynm(w,len);
2641: qltozl(w,len,&dvr);
2642: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
2643: divq(CQ(m),dvr,&t); CQ(m) = t;
2644: }
1.16 noro 2645: }
1.21 noro 2646: }
2647:
1.61 noro 2648: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
2649: {
2650: int len,i,max;
2651: NMV m,mr0,mr,t;
2652:
2653: len = p->len;
2654: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
2655: max = MAX(max,TD(DL(m)));
2656: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
2657: m = (NMV)((char *)mr0+(len-1)*oadv);
2658: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
2659: t = (NMV)ALLOCA(nmv_adv);
2660: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
2661: ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
1.113 noro 2662: CQ(mr) = CQ(m);
1.61 noro 2663: ndl_copy(DL(t),DL(mr));
2664: }
2665: NV(p)++;
2666: BDY(p) = mr0;
2667: }
2668:
1.45 noro 2669: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23 noro 2670: {
1.45 noro 2671: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.113 noro 2672: Q *w;
2673: Q dvr,t;
1.23 noro 2674: NMV m,r;
2675:
2676: len = p->len;
2677: newnvar = nd_nvar-1;
1.48 noro 2678: newexporigin = nd_get_exporigin(ord);
1.45 noro 2679: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23 noro 2680: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34 noro 2681: ndl_dehomogenize(DL(m));
1.23 noro 2682: if ( newwpd != nd_wpd ) {
1.90 noro 2683: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
1.61 noro 2684: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.113 noro 2685: CQ(r) = CQ(m);
1.45 noro 2686: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
2687: adj = nd_exporigin-newexporigin;
2688: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23 noro 2689: }
2690: }
2691: NV(p)--;
2692: }
2693:
1.113 noro 2694: void removecont_array(Q *c,int n)
1.21 noro 2695: {
2696: struct oVECT v;
1.113 noro 2697: Q d0,d1,a,u,u1,gcd;
1.110 noro 2698: int i,j;
1.113 noro 2699: N qn,rn,gn;
2700: Q *q,*r;
1.21 noro 2701:
1.113 noro 2702: q = (Q *)ALLOCA(n*sizeof(Q));
2703: r = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2704: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
1.113 noro 2705: igcdv_estimate(&v,&d0);
2706: for ( i = 0; i < n; i++ ) {
2707: divn(NM(c[i]),NM(d0),&qn,&rn);
2708: NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
2709: NTOQ(rn,SGN(c[i]),r[i]);
2710: }
1.34 noro 2711: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21 noro 2712: if ( i < n ) {
2713: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
1.113 noro 2714: igcdv(&v,&d1);
2715: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
2716: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
1.21 noro 2717: for ( i = 0; i < n; i++ ) {
1.113 noro 2718: mulq(a,q[i],&u);
2719: if ( r[i] ) {
2720: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
2721: addq(u,u1,&q[i]);
2722: } else
2723: q[i] = u;
1.21 noro 2724: }
2725: }
1.34 noro 2726: for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16 noro 2727: }
2728:
1.19 noro 2729: void nd_mul_c(int mod,ND p,int mul)
1.1 noro 2730: {
2731: NM m;
2732: int c,c1;
2733:
1.34 noro 2734: if ( !p ) return;
1.115 noro 2735: if ( mul == 1 ) return;
1.71 noro 2736: if ( mod == -1 )
2737: for ( m = BDY(p); m; m = NEXT(m) )
2738: CM(m) = _mulsf(CM(m),mul);
2739: else
2740: for ( m = BDY(p); m; m = NEXT(m) ) {
2741: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
2742: }
1.1 noro 2743: }
2744:
1.113 noro 2745: void nd_mul_c_q(ND p,Q mul)
1.16 noro 2746: {
2747: NM m;
1.113 noro 2748: Q c;
1.16 noro 2749:
1.113 noro 2750: if ( !p ) return;
1.115 noro 2751: if ( UNIQ(mul) ) return;
1.16 noro 2752: for ( m = BDY(p); m; m = NEXT(m) ) {
1.113 noro 2753: mulq(CQ(m),mul,&c); CQ(m) = c;
1.16 noro 2754: }
2755: }
2756:
1.61 noro 2757: void nd_mul_c_p(VL vl,ND p,P mul)
2758: {
2759: NM m;
2760: P c;
2761:
2762: if ( !p ) return;
2763: for ( m = BDY(p); m; m = NEXT(m) ) {
2764: mulp(vl,CP(m),mul,&c); CP(m) = c;
2765: }
2766: }
2767:
1.1 noro 2768: void nd_free(ND p)
2769: {
2770: NM t,s;
2771:
1.34 noro 2772: if ( !p ) return;
1.1 noro 2773: t = BDY(p);
2774: while ( t ) {
2775: s = NEXT(t);
2776: FREENM(t);
2777: t = s;
2778: }
2779: FREEND(p);
2780: }
2781:
1.23 noro 2782: void ndv_free(NDV p)
2783: {
2784: GC_free(BDY(p));
2785: }
2786:
1.61 noro 2787: void nd_append_red(UINT *d,int i)
1.1 noro 2788: {
1.13 noro 2789: RHist m,m0;
1.1 noro 2790: int h;
2791:
1.13 noro 2792: NEWRHist(m);
1.34 noro 2793: h = ndl_hash_value(d);
1.13 noro 2794: m->index = i;
1.14 noro 2795: ndl_copy(d,DL(m));
1.1 noro 2796: NEXT(m) = nd_red[h];
2797: nd_red[h] = m;
2798: }
2799:
1.61 noro 2800: UINT *ndv_compute_bound(NDV p)
1.1 noro 2801: {
1.61 noro 2802: UINT *d1,*d2,*t;
2803: UINT u;
1.57 noro 2804: int i,j,k,l,len,ind;
1.45 noro 2805: NMV m;
1.1 noro 2806:
2807: if ( !p )
2808: return 0;
1.61 noro 2809: d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
2810: d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45 noro 2811: len = LEN(p);
2812: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
2813: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14 noro 2814: ndl_lcm(DL(m),d1,d2);
1.1 noro 2815: t = d1; d1 = d2; d2 = t;
2816: }
1.12 noro 2817: l = nd_nvar+31;
1.61 noro 2818: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57 noro 2819: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
2820: u = d1[i];
2821: k = (nd_epw-1)*nd_bpe;
2822: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
2823: t[ind] = (u>>k)&nd_mask0;
2824: }
2825: for ( ; ind < l; ind++ ) t[ind] = 0;
1.1 noro 2826: return t;
2827: }
2828:
1.99 noro 2829: UINT *nd_compute_bound(ND p)
2830: {
2831: UINT *d1,*d2,*t;
2832: UINT u;
2833: int i,j,k,l,len,ind;
2834: NM m;
2835:
2836: if ( !p )
2837: return 0;
2838: d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
2839: d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
2840: len = LEN(p);
2841: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
2842: for ( m = NEXT(m); m; m = NEXT(m) ) {
2843: ndl_lcm(DL(m),d1,d2);
2844: t = d1; d1 = d2; d2 = t;
2845: }
2846: l = nd_nvar+31;
2847: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
2848: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
2849: u = d1[i];
2850: k = (nd_epw-1)*nd_bpe;
2851: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
2852: t[ind] = (u>>k)&nd_mask0;
2853: }
2854: for ( ; ind < l; ind++ ) t[ind] = 0;
2855: return t;
2856: }
2857:
1.48 noro 2858: int nd_get_exporigin(struct order_spec *ord)
2859: {
1.51 noro 2860: switch ( ord->id ) {
1.96 noro 2861: case 0: case 2:
1.48 noro 2862: return 1;
1.41 noro 2863: case 1:
2864: /* block order */
1.43 noro 2865: /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48 noro 2866: return ord->ord.block.length+1;
1.96 noro 2867: case 3:
2868: error("nd_get_exporigin : composite order is not supported yet.");
1.41 noro 2869: }
1.48 noro 2870: }
2871:
1.61 noro 2872: void nd_setup_parameters(int nvar,int max) {
1.79 noro 2873: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57 noro 2874: struct order_pair *op;
1.48 noro 2875:
1.73 noro 2876: nd_nvar = nvar;
1.79 noro 2877: if ( max ) {
1.83 noro 2878: /* XXX */
2879: if ( do_weyl ) nd_bpe = 32;
2880: else if ( max < 2 ) nd_bpe = 1;
1.79 noro 2881: else if ( max < 4 ) nd_bpe = 2;
2882: else if ( max < 8 ) nd_bpe = 3;
2883: else if ( max < 16 ) nd_bpe = 4;
2884: else if ( max < 32 ) nd_bpe = 5;
2885: else if ( max < 64 ) nd_bpe = 6;
2886: else if ( max < 256 ) nd_bpe = 8;
2887: else if ( max < 1024 ) nd_bpe = 10;
2888: else if ( max < 65536 ) nd_bpe = 16;
2889: else nd_bpe = 32;
2890: }
1.61 noro 2891: nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48 noro 2892: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
2893: nd_exporigin = nd_get_exporigin(nd_ord);
1.79 noro 2894: wpd = nd_exporigin+elen;
2895: if ( wpd != nd_wpd ) {
2896: nd_free_private_storage();
2897: nd_wpd = wpd;
2898: }
1.1 noro 2899: if ( nd_bpe < 32 ) {
2900: nd_mask0 = (1<<nd_bpe)-1;
2901: } else {
2902: nd_mask0 = 0xffffffff;
2903: }
2904: bzero(nd_mask,sizeof(nd_mask));
2905: nd_mask1 = 0;
2906: for ( i = 0; i < nd_epw; i++ ) {
2907: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
2908: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
2909: }
1.90 noro 2910: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
1.57 noro 2911: nd_epos = nd_create_epos(nd_ord);
1.43 noro 2912: nd_blockmask = nd_create_blockmask(nd_ord);
1.96 noro 2913: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1 noro 2914: }
2915:
1.103 noro 2916: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1 noro 2917: {
1.37 noro 2918: int i,obpe,oadv,h;
1.71 noro 2919: static NM prev_nm_free_list;
2920: static ND_pairs prev_ndp_free_list;
1.13 noro 2921: RHist mr0,mr;
2922: RHist r;
1.37 noro 2923: RHist *old_red;
1.71 noro 2924: ND_pairs s0,s,t;
1.43 noro 2925: EPOS oepos;
1.15 noro 2926:
1.1 noro 2927: obpe = nd_bpe;
1.11 noro 2928: oadv = nmv_adv;
1.43 noro 2929: oepos = nd_epos;
1.63 noro 2930: if ( obpe < 2 ) nd_bpe = 2;
2931: else if ( obpe < 3 ) nd_bpe = 3;
1.62 noro 2932: else if ( obpe < 4 ) nd_bpe = 4;
1.63 noro 2933: else if ( obpe < 5 ) nd_bpe = 5;
1.34 noro 2934: else if ( obpe < 6 ) nd_bpe = 6;
2935: else if ( obpe < 8 ) nd_bpe = 8;
1.63 noro 2936: else if ( obpe < 10 ) nd_bpe = 10;
1.34 noro 2937: else if ( obpe < 16 ) nd_bpe = 16;
2938: else if ( obpe < 32 ) nd_bpe = 32;
2939: else error("nd_reconstruct : exponent too large");
1.5 noro 2940:
1.73 noro 2941: nd_setup_parameters(nd_nvar,0);
1.1 noro 2942: prev_nm_free_list = _nm_free_list;
2943: prev_ndp_free_list = _ndp_free_list;
2944: _nm_free_list = 0;
2945: _ndp_free_list = 0;
1.53 noro 2946: for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
2947: if ( trace )
2948: for ( i = nd_psn-1; i >= 0; i-- )
2949: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1 noro 2950: s0 = 0;
2951: for ( t = d; t; t = NEXT(t) ) {
2952: NEXTND_pairs(s0,s);
2953: s->i1 = t->i1;
2954: s->i2 = t->i2;
1.14 noro 2955: SG(s) = SG(t);
1.61 noro 2956: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1 noro 2957: }
1.37 noro 2958:
2959: old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6 noro 2960: for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37 noro 2961: old_red[i] = nd_red[i];
2962: nd_red[i] = 0;
2963: }
2964: for ( i = 0; i < REDTAB_LEN; i++ )
2965: for ( r = old_red[i]; r; r = NEXT(r) ) {
2966: NEWRHist(mr);
1.13 noro 2967: mr->index = r->index;
1.20 noro 2968: SG(mr) = SG(r);
1.61 noro 2969: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37 noro 2970: h = ndl_hash_value(DL(mr));
2971: NEXT(mr) = nd_red[h];
2972: nd_red[h] = mr;
1.6 noro 2973: }
1.37 noro 2974: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
2975: old_red = 0;
1.11 noro 2976: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 2977: NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61 noro 2978: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13 noro 2979: nd_psh[i] = r;
1.11 noro 2980: }
1.1 noro 2981: if ( s0 ) NEXT(s) = 0;
2982: prev_nm_free_list = 0;
2983: prev_ndp_free_list = 0;
1.71 noro 2984: #if 0
1.1 noro 2985: GC_gcollect();
1.71 noro 2986: #endif
1.1 noro 2987: return s0;
2988: }
2989:
1.61 noro 2990: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1 noro 2991: {
1.57 noro 2992: int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43 noro 2993: struct order_pair *op;
1.1 noro 2994:
2995: n = nd_nvar;
1.61 noro 2996: oepw = (sizeof(UINT)*8)/obpe;
1.43 noro 2997: omask0 = (1<<obpe)-1;
1.34 noro 2998: TD(r) = TD(d);
1.41 noro 2999: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 3000: if ( nd_blockmask ) {
3001: l = nd_blockmask->n;
3002: op = nd_blockmask->order_pair;
3003: for ( i = 1; i < nd_exporigin; i++ )
3004: r[i] = d[i];
3005: for ( j = 0, s = 0; j < l; j++ ) {
3006: ord_l = op[j].length;
1.57 noro 3007: for ( i = 0; i < ord_l; i++, s++ ) {
3008: ei = GET_EXP_OLD(d,s);
3009: PUT_EXP(r,s,ei);
3010: }
1.1 noro 3011: }
1.43 noro 3012: } else {
1.56 noro 3013: for ( i = 0; i < n; i++ ) {
3014: ei = GET_EXP_OLD(d,i);
3015: PUT_EXP(r,i,ei);
3016: }
1.1 noro 3017: }
3018: }
1.3 noro 3019:
1.6 noro 3020: ND nd_copy(ND p)
3021: {
3022: NM m,mr,mr0;
1.41 noro 3023: int c,n;
1.6 noro 3024: ND r;
3025:
3026: if ( !p )
3027: return 0;
3028: else {
3029: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
3030: NEXTNM(mr0,mr);
1.14 noro 3031: CM(mr) = CM(m);
3032: ndl_copy(DL(m),DL(mr));
1.6 noro 3033: }
3034: NEXT(mr) = 0;
1.31 noro 3035: MKND(NV(p),mr0,LEN(p),r);
1.14 noro 3036: SG(r) = SG(p);
1.6 noro 3037: return r;
3038: }
3039: }
3040:
1.53 noro 3041: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11 noro 3042: {
3043: NM m;
3044: NDV p1,p2;
3045: ND t1,t2;
1.61 noro 3046: UINT *lcm;
1.31 noro 3047: int td;
1.11 noro 3048:
1.77 noro 3049: if ( !mod && nd_demand ) {
3050: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53 noro 3051: } else {
1.77 noro 3052: if ( trace ) {
3053: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
3054: } else {
3055: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
3056: }
1.20 noro 3057: }
1.34 noro 3058: lcm = LCM(p);
1.11 noro 3059: NEWNM(m);
1.113 noro 3060: CQ(m) = HCQ(p2);
1.34 noro 3061: ndl_sub(lcm,HDL(p1),DL(m));
1.56 noro 3062: if ( ndl_check_bound2(p->i1,DL(m)) )
3063: return 0;
1.55 noro 3064: t1 = ndv_mul_nm(mod,m,p1);
1.71 noro 3065: if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
3066: else if ( mod ) CM(m) = mod-HCM(p1);
1.113 noro 3067: else chsgnq(HCQ(p1),&CQ(m));
1.34 noro 3068: ndl_sub(lcm,HDL(p2),DL(m));
1.14 noro 3069: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 3070: nd_free(t1);
3071: return 0;
3072: }
1.55 noro 3073: t2 = ndv_mul_nm(mod,m,p2);
1.31 noro 3074: *rp = nd_add(mod,t1,t2);
1.11 noro 3075: FREENM(m);
3076: return 1;
3077: }
3078:
1.19 noro 3079: void ndv_mul_c(int mod,NDV p,int mul)
1.11 noro 3080: {
3081: NMV m;
3082: int c,c1,len,i;
3083:
1.34 noro 3084: if ( !p ) return;
1.14 noro 3085: len = LEN(p);
1.71 noro 3086: if ( mod == -1 )
3087: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
3088: CM(m) = _mulsf(CM(m),mul);
3089: else
3090: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3091: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
3092: }
1.11 noro 3093: }
3094:
1.113 noro 3095: void ndv_mul_c_q(NDV p,Q mul)
1.16 noro 3096: {
3097: NMV m;
1.113 noro 3098: Q c;
1.16 noro 3099: int len,i;
3100:
1.34 noro 3101: if ( !p ) return;
1.16 noro 3102: len = LEN(p);
3103: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113 noro 3104: mulq(CQ(m),mul,&c); CQ(m) = c;
1.16 noro 3105: }
3106: }
3107:
1.55 noro 3108: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
3109: int n2,i,j,l,n,tlen;
1.61 noro 3110: UINT *d0;
1.55 noro 3111: NM *tab,*psum;
3112: ND s,r;
3113: NM t;
3114: NMV m1;
3115:
3116: if ( !p ) return 0;
3117: n = NV(p); n2 = n>>1;
3118: d0 = DL(m0);
3119: l = LEN(p);
3120: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
3121: tab = (NM *)ALLOCA(tlen*sizeof(NM));
3122: psum = (NM *)ALLOCA(tlen*sizeof(NM));
3123: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56 noro 3124: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
3125: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55 noro 3126: /* m0(NM) * m1(NMV) => tab(NM) */
1.56 noro 3127: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55 noro 3128: for ( j = 0; j < tlen; j++ ) {
3129: if ( tab[j] ) {
3130: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
3131: }
3132: }
3133: }
3134: for ( i = tlen-1, r = 0; i >= 0; i-- )
3135: if ( psum[i] ) {
3136: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
3137: MKND(n,psum[i],j,s);
3138: r = nd_add(mod,r,s);
3139: }
1.56 noro 3140: if ( r ) SG(r) = SG(p)+TD(d0);
3141: return r;
1.55 noro 3142: }
3143:
1.56 noro 3144: /* product of monomials */
3145: /* XXX block order is not handled correctly */
3146:
1.55 noro 3147: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
3148: {
1.56 noro 3149: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61 noro 3150: UINT *d0,*d1,*d,*dt,*ctab;
1.113 noro 3151: Q *ctab_q;
3152: Q q,q1;
1.61 noro 3153: UINT c0,c1,c;
1.55 noro 3154: NM *p;
3155: NM m,t;
3156:
3157: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
3158: if ( !m0 || !m1 ) return;
3159: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
3160: NEWNM(m); d = DL(m);
1.56 noro 3161: if ( mod ) {
3162: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
3163: } else
1.113 noro 3164: mulq(CQ(m0),CQ(m1),&CQ(m));
1.55 noro 3165: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
3166: homo = n&1 ? 1 : 0;
3167: if ( homo ) {
3168: /* offset of h-degree */
3169: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
3170: PUT_EXP(DL(m),n-1,h);
3171: TD(DL(m)) = h;
1.61 noro 3172: if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55 noro 3173: }
3174: tab[0] = m;
3175: NEWNM(m); d = DL(m);
1.57 noro 3176: for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55 noro 3177: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
3178: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
3179: /* xi^a*(Di^k*xi^l)*Di^b */
3180: a += l; b += k;
1.56 noro 3181: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55 noro 3182: if ( !k || !l ) {
3183: for ( j = 0; j < curlen; j++ )
1.56 noro 3184: if ( t = tab[j] ) {
3185: dt = DL(t);
3186: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61 noro 3187: if ( nd_blockmask ) ndl_weight_mask(dt);
1.55 noro 3188: }
3189: curlen *= k+1;
3190: continue;
3191: }
3192: min = MIN(k,l);
1.56 noro 3193: if ( mod ) {
1.61 noro 3194: ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56 noro 3195: mkwcm(k,l,mod,ctab);
3196: } else {
1.113 noro 3197: ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
3198: mkwc(k,l,ctab_q);
1.56 noro 3199: }
1.57 noro 3200: for ( j = min; j >= 0; j-- ) {
1.56 noro 3201: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55 noro 3202: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56 noro 3203: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55 noro 3204: if ( homo ) {
3205: TD(d) = s;
1.56 noro 3206: PUT_EXP(d,n-1,s-h);
1.55 noro 3207: } else TD(d) = h;
1.61 noro 3208: if ( nd_blockmask ) ndl_weight_mask(d);
1.56 noro 3209: if ( mod ) c = ctab[j];
1.113 noro 3210: else q = ctab_q[j];
1.57 noro 3211: p = tab+curlen*j;
3212: if ( j == 0 ) {
3213: for ( u = 0; u < curlen; u++, p++ ) {
3214: if ( tab[u] ) {
3215: ndl_addto(DL(tab[u]),d);
3216: if ( mod ) {
3217: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
3218: } else {
1.113 noro 3219: mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
1.57 noro 3220: }
3221: }
1.56 noro 3222: }
1.57 noro 3223: } else {
3224: for ( u = 0; u < curlen; u++, p++ ) {
3225: if ( tab[u] ) {
3226: NEWNM(t);
3227: ndl_add(DL(tab[u]),d,DL(t));
3228: if ( mod ) {
3229: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
3230: } else
1.113 noro 3231: mulq(CQ(tab[u]),q,&CQ(t));
1.57 noro 3232: *p = t;
3233: }
1.55 noro 3234: }
3235: }
3236: }
3237: curlen *= k+1;
3238: }
3239: FREENM(m);
3240: }
3241:
1.63 noro 3242: ND ndv_mul_nm_symbolic(NM m0,NDV p)
3243: {
3244: NM mr,mr0;
3245: NMV m;
3246: UINT *d,*dt,*dm;
3247: int c,n,td,i,c1,c2,len;
1.113 noro 3248: Q q;
1.63 noro 3249: ND r;
3250:
3251: if ( !p ) return 0;
3252: else {
3253: n = NV(p); m = BDY(p);
3254: d = DL(m0);
3255: len = LEN(p);
3256: mr0 = 0;
3257: td = TD(d);
3258: c = CM(m0);
3259: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3260: NEXTNM(mr0,mr);
3261: CM(mr) = 1;
3262: ndl_add(DL(m),d,DL(mr));
3263: }
3264: NEXT(mr) = 0;
3265: MKND(NV(p),mr0,len,r);
3266: SG(r) = SG(p) + TD(d);
3267: return r;
3268: }
3269: }
3270:
1.55 noro 3271: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9 noro 3272: {
3273: NM mr,mr0;
3274: NMV m;
1.61 noro 3275: UINT *d,*dt,*dm;
1.9 noro 3276: int c,n,td,i,c1,c2,len;
1.113 noro 3277: Q q;
1.9 noro 3278: ND r;
3279:
1.34 noro 3280: if ( !p ) return 0;
1.55 noro 3281: else if ( do_weyl )
1.71 noro 3282: if ( mod == -1 )
3283: error("ndv_mul_nm : not implemented (weyl)");
3284: else
3285: return weyl_ndv_mul_nm(mod,m0,p);
1.9 noro 3286: else {
3287: n = NV(p); m = BDY(p);
1.34 noro 3288: d = DL(m0);
1.14 noro 3289: len = LEN(p);
1.9 noro 3290: mr0 = 0;
1.34 noro 3291: td = TD(d);
1.71 noro 3292: if ( mod == -1 ) {
3293: c = CM(m0);
3294: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3295: NEXTNM(mr0,mr);
3296: CM(mr) = _mulsf(CM(m),c);
3297: ndl_add(DL(m),d,DL(mr));
3298: }
3299: } else if ( mod ) {
1.16 noro 3300: c = CM(m0);
3301: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3302: NEXTNM(mr0,mr);
3303: c1 = CM(m);
1.19 noro 3304: DMAR(c1,c,0,mod,c2);
1.16 noro 3305: CM(mr) = c2;
3306: ndl_add(DL(m),d,DL(mr));
3307: }
3308: } else {
1.113 noro 3309: q = CQ(m0);
1.16 noro 3310: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3311: NEXTNM(mr0,mr);
1.113 noro 3312: mulq(CQ(m),q,&CQ(mr));
1.16 noro 3313: ndl_add(DL(m),d,DL(mr));
3314: }
1.4 noro 3315: }
1.9 noro 3316: NEXT(mr) = 0;
1.31 noro 3317: MKND(NV(p),mr0,len,r);
1.34 noro 3318: SG(r) = SG(p) + TD(d);
1.9 noro 3319: return r;
1.4 noro 3320: }
3321: }
3322:
1.104 noro 3323: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99 noro 3324: {
3325: NM mq0,mq;
1.102 noro 3326: NMV tm;
1.113 noro 3327: Q q;
1.104 noro 3328: int i,nv,sg,c,c1,c2,hindex;
3329: ND p,t,r;
3330: N tnm;
3331:
1.99 noro 3332: if ( !p ) return 0;
3333: else {
1.104 noro 3334: nv = NV(d);
1.99 noro 3335: mq0 = 0;
1.102 noro 3336: tm = (NMV)ALLOCA(nmv_adv);
1.104 noro 3337: while ( 1 ) {
1.113 noro 3338: hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.104 noro 3339: if ( hindex < 0 ) break;
3340: p = bucket->body[hindex];
1.99 noro 3341: NEXTNM(mq0,mq);
3342: ndl_sub(HDL(p),HDL(d),DL(mq));
1.102 noro 3343: ndl_copy(DL(mq),DL(tm));
3344: if ( mod ) {
3345: c1 = invm(HCM(d),mod); c2 = HCM(p);
3346: DMAR(c1,c2,0,mod,c); CM(mq) = c;
3347: CM(tm) = mod-c;
3348: } else {
1.113 noro 3349: divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
3350: NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
3351: chsgnq(CQ(mq),&CQ(tm));
1.102 noro 3352: }
3353: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
1.104 noro 3354: bucket->body[hindex] = nd_remove_head(p);
3355: t = nd_remove_head(t);
3356: add_pbucket(mod,bucket,t);
3357: }
3358: if ( !mq0 )
3359: r = 0;
3360: else {
3361: NEXT(mq) = 0;
3362: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
3363: MKND(nv,mq0,i,r);
3364: /* XXX */
3365: SG(r) = HTD(r);
1.99 noro 3366: }
3367: return r;
3368: }
3369: }
3370:
1.43 noro 3371: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11 noro 3372: {
1.13 noro 3373: NMV m,mr,mr0,t;
3374: int len,i,k;
1.11 noro 3375:
1.61 noro 3376: if ( !p ) return;
3377: m = BDY(p); len = LEN(p);
3378: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
3379: m = (NMV)((char *)mr0+(len-1)*oadv);
3380: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
3381: t = (NMV)ALLOCA(nmv_adv);
3382: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.113 noro 3383: CQ(t) = CQ(m);
1.61 noro 3384: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
3385: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.113 noro 3386: CQ(mr) = CQ(t);
1.61 noro 3387: ndl_copy(DL(t),DL(mr));
3388: }
3389: BDY(p) = mr0;
3390: }
3391:
3392: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
3393: {
3394: NMV m,mr,mr0;
3395: int len,i;
3396: NDV r;
1.11 noro 3397:
1.61 noro 3398: if ( !p ) return 0;
3399: m = BDY(p); len = LEN(p);
3400: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
3401: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
3402: ndl_zero(DL(mr));
3403: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.113 noro 3404: CQ(mr) = CQ(m);
1.11 noro 3405: }
1.61 noro 3406: MKNDV(NV(p),mr0,len,r);
3407: SG(r) = SG(p);
3408: return r;
1.11 noro 3409: }
3410:
1.61 noro 3411: /* duplicate p */
3412:
3413: NDV ndv_dup(int mod,NDV p)
1.3 noro 3414: {
3415: NDV d;
1.61 noro 3416: NMV t,m,m0;
1.3 noro 3417: int i,len;
3418:
1.34 noro 3419: if ( !p ) return 0;
1.31 noro 3420: len = LEN(p);
1.34 noro 3421: m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61 noro 3422: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14 noro 3423: ndl_copy(DL(t),DL(m));
1.113 noro 3424: CQ(m) = CQ(t);
1.3 noro 3425: }
3426: MKNDV(NV(p),m0,len,d);
1.23 noro 3427: SG(d) = SG(p);
3428: return d;
3429: }
3430:
1.63 noro 3431: ND nd_dup(ND p)
3432: {
3433: ND d;
3434: NM t,m,m0;
3435:
3436: if ( !p ) return 0;
3437: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
3438: NEXTNM(m0,m);
3439: ndl_copy(DL(t),DL(m));
1.113 noro 3440: CQ(m) = CQ(t);
1.63 noro 3441: }
3442: if ( m0 ) NEXT(m) = 0;
3443: MKND(NV(p),m0,LEN(p),d);
3444: SG(d) = SG(p);
3445: return d;
3446: }
3447:
1.61 noro 3448: /* XXX if p->len == 0 then it represents 0 */
3449:
3450: void ndv_mod(int mod,NDV p)
3451: {
3452: NMV t,d;
3453: int r;
3454: int i,len,dlen;
1.71 noro 3455: Obj gfs;
1.61 noro 3456:
3457: if ( !p ) return;
3458: len = LEN(p);
3459: dlen = 0;
1.71 noro 3460: if ( mod == -1 )
3461: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
3462: simp_ff((Obj)CP(t),&gfs);
3463: r = FTOIF(CONT((GFS)gfs));
1.61 noro 3464: CM(d) = r;
3465: ndl_copy(DL(t),DL(d));
3466: NMV_ADV(d);
3467: dlen++;
3468: }
1.71 noro 3469: else
3470: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.113 noro 3471: r = rem(NM(CQ(t)),mod);
1.71 noro 3472: if ( r ) {
1.113 noro 3473: if ( SGN(CQ(t)) < 0 )
3474: r = mod-r;
1.71 noro 3475: CM(d) = r;
3476: ndl_copy(DL(t),DL(d));
3477: NMV_ADV(d);
3478: dlen++;
3479: }
3480: }
1.61 noro 3481: LEN(p) = dlen;
3482: }
3483:
3484: NDV ptondv(VL vl,VL dvl,P p)
3485: {
3486: ND nd;
3487:
3488: nd = ptond(vl,dvl,p);
3489: return ndtondv(0,nd);
3490: }
3491:
3492: ND ptond(VL vl,VL dvl,P p)
1.23 noro 3493: {
1.61 noro 3494: int n,i,j,k,e;
3495: VL tvl;
3496: V v;
3497: DCP dc;
3498: DCP *w;
3499: ND r,s,t,u;
3500: P x;
3501: int c;
3502: UINT *d;
1.23 noro 3503: NM m,m0;
1.61 noro 3504:
3505: if ( !p )
3506: return 0;
3507: else if ( NUM(p) ) {
3508: NEWNM(m);
3509: ndl_zero(DL(m));
1.113 noro 3510: CQ(m) = (Q)p;
1.61 noro 3511: NEXT(m) = 0;
3512: MKND(nd_nvar,m,1,r);
3513: SG(r) = 0;
3514: return r;
3515: } else {
3516: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
3517: w = (DCP *)ALLOCA(k*sizeof(DCP));
3518: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
3519: for ( i = 0, tvl = dvl, v = VR(p);
1.98 noro 3520: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
1.61 noro 3521: if ( !tvl ) {
3522: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
3523: t = ptond(vl,dvl,COEF(w[j]));
3524: pwrp(vl,x,DEG(w[j]),&p);
3525: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
3526: }
3527: return s;
3528: } else {
3529: NEWNM(m0); d = DL(m0);
3530: for ( j = k-1, s = 0; j >= 0; j-- ) {
3531: ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
3532: TD(d) = MUL_WEIGHT(e,i);
3533: if ( nd_blockmask) ndl_weight_mask(d);
3534: t = ptond(vl,dvl,COEF(w[j]));
3535: for ( m = BDY(t); m; m = NEXT(m) )
3536: ndl_addto(DL(m),d);
3537: SG(t) += TD(d);
3538: s = nd_add(0,s,t);
3539: }
3540: FREENM(m0);
3541: return s;
3542: }
3543: }
3544: }
3545:
3546: P ndvtop(int mod,VL vl,VL dvl,NDV p)
3547: {
3548: VL tvl;
3549: int len,n,j,i,e;
3550: NMV m;
3551: Q q;
3552: P c;
3553: UINT *d;
3554: P s,r,u,t,w;
1.71 noro 3555: GFS gfs;
1.23 noro 3556:
1.34 noro 3557: if ( !p ) return 0;
1.61 noro 3558: else {
3559: len = LEN(p);
3560: n = NV(p);
3561: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
3562: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71 noro 3563: if ( mod == -1 ) {
3564: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
3565: } else if ( mod ) {
1.61 noro 3566: STOQ(CM(m),q); c = (P)q;
3567: } else
1.113 noro 3568: c = CP(m);
1.61 noro 3569: d = DL(m);
3570: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
3571: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
3572: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
3573: }
3574: addp(vl,s,t,&u); s = u;
3575: }
3576: return s;
1.23 noro 3577: }
1.3 noro 3578: }
3579:
1.61 noro 3580: NDV ndtondv(int mod,ND p)
1.11 noro 3581: {
3582: NDV d;
1.61 noro 3583: NMV m,m0;
3584: NM t;
3585: int i,len;
1.11 noro 3586:
1.34 noro 3587: if ( !p ) return 0;
1.61 noro 3588: len = LEN(p);
1.74 noro 3589: if ( mod )
3590: m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
3591: else
3592: m0 = m = MALLOC(len*nmv_adv);
1.103 noro 3593: #if 0
1.74 noro 3594: ndv_alloc += nmv_adv*len;
1.103 noro 3595: #endif
1.61 noro 3596: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
3597: ndl_copy(DL(t),DL(m));
1.113 noro 3598: CQ(m) = CQ(t);
1.11 noro 3599: }
1.61 noro 3600: MKNDV(NV(p),m0,len,d);
1.14 noro 3601: SG(d) = SG(p);
1.11 noro 3602: return d;
3603: }
3604:
1.61 noro 3605: ND ndvtond(int mod,NDV p)
1.11 noro 3606: {
1.61 noro 3607: ND d;
3608: NM m,m0;
1.11 noro 3609: NMV t;
1.61 noro 3610: int i,len;
1.11 noro 3611:
1.34 noro 3612: if ( !p ) return 0;
1.11 noro 3613: m0 = 0;
1.61 noro 3614: len = p->len;
3615: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
3616: NEXTNM(m0,m);
3617: ndl_copy(DL(t),DL(m));
1.113 noro 3618: CQ(m) = CQ(t);
1.11 noro 3619: }
3620: NEXT(m) = 0;
1.61 noro 3621: MKND(NV(p),m0,len,d);
1.14 noro 3622: SG(d) = SG(p);
1.11 noro 3623: return d;
3624: }
3625:
1.3 noro 3626: void ndv_print(NDV p)
3627: {
3628: NMV m;
3629: int i,len;
3630:
1.34 noro 3631: if ( !p ) printf("0\n");
1.3 noro 3632: else {
1.14 noro 3633: len = LEN(p);
1.3 noro 3634: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71 noro 3635: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3636: else printf("+%d*",CM(m));
1.16 noro 3637: ndl_print(DL(m));
3638: }
3639: printf("\n");
3640: }
3641: }
3642:
1.113 noro 3643: void ndv_print_q(NDV p)
1.16 noro 3644: {
3645: NMV m;
3646: int i,len;
3647:
1.34 noro 3648: if ( !p ) printf("0\n");
1.16 noro 3649: else {
3650: len = LEN(p);
3651: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3652: printf("+");
1.113 noro 3653: printexpr(CO,(Obj)CQ(m));
1.16 noro 3654: printf("*");
1.14 noro 3655: ndl_print(DL(m));
1.3 noro 3656: }
3657: printf("\n");
3658: }
1.25 noro 3659: }
3660:
1.61 noro 3661: NODE ndv_reducebase(NODE x)
1.27 noro 3662: {
3663: int len,i,j;
3664: NDV *w;
3665: NODE t,t0;
3666:
3667: len = length(x);
3668: w = (NDV *)ALLOCA(len*sizeof(NDV));
3669: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
3670: for ( i = 0; i < len; i++ ) {
3671: for ( j = 0; j < i; j++ ) {
3672: if ( w[i] && w[j] )
3673: if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
3674: else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
3675: }
3676: }
3677: for ( i = len-1, t0 = 0; i >= 0; i-- ) {
3678: if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
3679: }
3680: NEXT(t) = 0; x = t0;
3681: return x;
1.11 noro 3682: }
1.32 noro 3683:
1.43 noro 3684: /* XXX incomplete */
3685:
1.32 noro 3686: void nd_init_ord(struct order_spec *ord)
3687: {
1.43 noro 3688: switch ( ord->id ) {
1.32 noro 3689: case 0:
1.43 noro 3690: switch ( ord->ord.simple ) {
3691: case 0:
3692: nd_dcomp = 1;
3693: nd_isrlex = 1;
3694: break;
3695: case 1:
3696: nd_dcomp = 1;
3697: nd_isrlex = 0;
3698: break;
3699: case 2:
3700: nd_dcomp = 0;
3701: nd_isrlex = 0;
1.45 noro 3702: ndl_compare_function = ndl_lex_compare;
1.58 noro 3703: break;
3704: case 11:
3705: /* XXX */
3706: nd_dcomp = 0;
3707: nd_isrlex = 1;
3708: ndl_compare_function = ndl_ww_lex_compare;
1.43 noro 3709: break;
3710: default:
3711: error("nd_gr : unsupported order");
3712: }
1.32 noro 3713: break;
3714: case 1:
1.96 noro 3715: /* block order */
1.43 noro 3716: /* XXX */
3717: nd_dcomp = -1;
1.32 noro 3718: nd_isrlex = 0;
1.45 noro 3719: ndl_compare_function = ndl_block_compare;
1.34 noro 3720: break;
1.43 noro 3721: case 2:
1.96 noro 3722: /* matrix order */
3723: /* XXX */
3724: nd_dcomp = -1;
3725: nd_isrlex = 0;
3726: nd_matrix_len = ord->ord.matrix.row;
3727: nd_matrix = ord->ord.matrix.matrix;
3728: ndl_compare_function = ndl_matrix_compare;
3729: break;
3730: case 3:
1.97 noro 3731: /* composite order */
3732: nd_dcomp = -1;
3733: nd_isrlex = 0;
3734: nd_worb_len = ord->ord.composite.length;
3735: nd_worb = ord->ord.composite.w_or_b;
3736: ndl_compare_function = ndl_composite_compare;
1.32 noro 3737: break;
3738: }
1.41 noro 3739: nd_ord = ord;
1.32 noro 3740: }
3741:
1.43 noro 3742: BlockMask nd_create_blockmask(struct order_spec *ord)
3743: {
3744: int n,i,j,s,l;
1.61 noro 3745: UINT *t;
1.43 noro 3746: BlockMask bm;
3747:
1.96 noro 3748: /* we only create mask table for block order */
3749: if ( ord->id != 1 )
1.43 noro 3750: return 0;
3751: n = ord->ord.block.length;
3752: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
3753: bm->n = n;
3754: bm->order_pair = ord->ord.block.order_pair;
1.61 noro 3755: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43 noro 3756: for ( i = 0, s = 0; i < n; i++ ) {
1.61 noro 3757: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43 noro 3758: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
3759: l = bm->order_pair[i].length;
3760: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
3761: }
3762: return bm;
1.57 noro 3763: }
3764:
3765: EPOS nd_create_epos(struct order_spec *ord)
3766: {
3767: int i,j,l,s,ord_l,ord_o;
3768: EPOS epos;
3769: struct order_pair *op;
3770:
3771: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
3772: switch ( ord->id ) {
3773: case 0:
3774: if ( nd_isrlex ) {
3775: for ( i = 0; i < nd_nvar; i++ ) {
3776: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
3777: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
3778: }
3779: } else {
3780: for ( i = 0; i < nd_nvar; i++ ) {
3781: epos[i].i = nd_exporigin + i/nd_epw;
3782: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
3783: }
3784: }
3785: break;
3786: case 1:
3787: /* block order */
3788: l = ord->ord.block.length;
3789: op = ord->ord.block.order_pair;
3790: for ( j = 0, s = 0; j < l; j++ ) {
3791: ord_o = op[j].order;
3792: ord_l = op[j].length;
3793: if ( !ord_o )
3794: for ( i = 0; i < ord_l; i++ ) {
3795: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
3796: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
3797: }
3798: else
3799: for ( i = 0; i < ord_l; i++ ) {
3800: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
3801: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
3802: }
3803: s += ord_l;
3804: }
3805: break;
3806: case 2:
1.96 noro 3807: /* matrix order */
3808: case 3:
3809: /* composite order */
3810: for ( i = 0; i < nd_nvar; i++ ) {
3811: epos[i].i = nd_exporigin + i/nd_epw;
3812: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
3813: }
3814: break;
1.57 noro 3815: }
3816: return epos;
1.43 noro 3817: }
1.59 noro 3818:
3819: /* external interface */
3820:
3821: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
3822: {
1.61 noro 3823: NODE t,in0,in;
1.59 noro 3824: ND nd,nf;
1.61 noro 3825: NDV ndv;
3826: VL vv,tv;
3827: int stat,nvar,max,e;
1.113 noro 3828: union oNDC dn;
1.59 noro 3829:
1.116 noro 3830: if ( !f ) {
3831: *rp = 0;
3832: return;
3833: }
1.59 noro 3834: pltovl(v,&vv);
1.74 noro 3835: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61 noro 3836:
3837: /* get the degree bound */
3838: for ( t = BDY(g), max = 0; t; t = NEXT(t) )
3839: for ( tv = vv; tv; tv = NEXT(tv) ) {
3840: e = getdeg(tv->v,(P)BDY(t));
3841: max = MAX(e,max);
3842: }
3843: for ( tv = vv; tv; tv = NEXT(tv) ) {
3844: e = getdeg(tv->v,f);
3845: max = MAX(e,max);
3846: }
3847:
1.59 noro 3848: nd_init_ord(ord);
1.61 noro 3849: nd_setup_parameters(nvar,max);
3850:
3851: /* conversion to ndv */
3852: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
3853: NEXTNODE(in0,in);
3854: BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
1.69 noro 3855: if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61 noro 3856: }
3857: NEXTNODE(in0,in);
3858: BDY(in) = (pointer)ptondv(CO,vv,f);
1.69 noro 3859: if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61 noro 3860: NEXT(in) = 0;
3861:
3862: ndv_setup(m,0,in0);
1.59 noro 3863: nd_psn--;
3864: nd_scale=2;
3865: while ( 1 ) {
3866: nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.69 noro 3867: stat = nd_nf(m,nd,nd_ps,1,0,&nf);
1.59 noro 3868: if ( !stat ) {
3869: nd_psn++;
1.103 noro 3870: nd_reconstruct(0,0);
1.59 noro 3871: nd_psn--;
3872: } else
3873: break;
3874: }
1.61 noro 3875: *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63 noro 3876: }
3877:
3878: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
3879: {
3880: NM m;
3881: UINT *t,*s;
3882: int i;
3883:
3884: for ( i = 0; i < n; i++ ) r[i] = 0;
3885: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
3886: t = DL(m);
3887: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
3888: r[i] = CM(m);
3889: }
3890: for ( i = 0; !r[i]; i++ );
3891: return i;
3892: }
3893:
1.113 noro 3894: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74 noro 3895: {
1.107 noro 3896: NM m;
1.74 noro 3897: UINT *t,*s;
1.107 noro 3898: int i;
1.74 noro 3899:
3900: for ( i = 0; i < n; i++ ) r[i] = 0;
1.107 noro 3901: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
1.74 noro 3902: t = DL(m);
3903: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.113 noro 3904: r[i] = CQ(m);
1.74 noro 3905: }
3906: for ( i = 0; !r[i]; i++ );
3907: return i;
3908: }
3909:
1.67 noro 3910: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64 noro 3911: {
3912: NM m;
3913: NMV mr;
3914: UINT *d,*t,*s;
3915: NDV p;
1.67 noro 3916: unsigned char *ivc;
3917: unsigned short *ivs;
1.81 noro 3918: UINT *v,*ivi,*s0v;
1.67 noro 3919: int i,j,len,prev,diff,cdiff;
3920: IndArray r;
1.64 noro 3921:
3922: m = pair->mul;
3923: d = DL(m);
3924: p = nd_ps[pair->index];
3925: len = LEN(p);
3926: t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67 noro 3927: v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64 noro 3928: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
3929: ndl_add(d,DL(mr),t);
3930: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67 noro 3931: v[j] = i;
3932: }
1.81 noro 3933: r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67 noro 3934: r->head = v[0];
3935: diff = 0;
3936: for ( i = 1; i < len; i++ ) {
3937: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
3938: }
3939: if ( diff < 256 ) {
3940: r->width = 1;
3941: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
3942: r->index.c = ivc;
3943: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
3944: } else if ( diff < 65536 ) {
3945: r->width = 2;
3946: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
3947: r->index.s = ivs;
3948: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
3949: } else {
3950: r->width = 4;
3951: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
3952: r->index.i = ivi;
3953: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64 noro 3954: }
1.67 noro 3955: return r;
1.64 noro 3956: }
3957:
1.65 noro 3958:
1.113 noro 3959: int ndv_reduce_vect_q(Q *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107 noro 3960: {
3961: int i,j,k,len,pos,prev;
1.113 noro 3962: Q cs,mcs,c1,c2,cr,gcd,t;
1.107 noro 3963: IndArray ivect;
3964: unsigned char *ivc;
3965: unsigned short *ivs;
3966: unsigned int *ivi;
3967: NDV redv;
3968: NMV mr;
3969: NODE rp;
3970: int maxrs;
3971:
3972: maxrs = 0;
3973: for ( i = 0; i < nred; i++ ) {
3974: ivect = imat[i];
3975: k = ivect->head;
3976: if ( svect[k] ) {
3977: maxrs = MAX(maxrs,rp0[i]->sugar);
3978: redv = nd_ps[rp0[i]->index];
3979: len = LEN(redv); mr = BDY(redv);
1.113 noro 3980: igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
3981: chsgnq(cs,&mcs);
3982: if ( !UNIQ(cr) ) {
3983: for ( j = 0; j < col; j++ ) {
3984: mulq(svect[j],cr,&c1); svect[j] = c1;
3985: }
3986: }
1.107 noro 3987: svect[k] = 0; prev = k;
3988: switch ( ivect->width ) {
3989: case 1:
3990: ivc = ivect->index.c;
3991: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
3992: pos = prev+ivc[j]; prev = pos;
1.113 noro 3993: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107 noro 3994: }
3995: break;
3996: case 2:
3997: ivs = ivect->index.s;
3998: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
3999: pos = prev+ivs[j]; prev = pos;
1.113 noro 4000: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107 noro 4001: }
4002: break;
4003: case 4:
4004: ivi = ivect->index.i;
4005: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4006: pos = prev+ivi[j]; prev = pos;
1.113 noro 4007: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107 noro 4008: }
4009: break;
4010: }
4011: }
4012: }
4013: return maxrs;
4014: }
4015:
1.76 noro 4016: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65 noro 4017: {
1.67 noro 4018: int i,j,k,len,pos,prev;
1.66 noro 4019: UINT c,c1,c2,c3,up,lo,dmy;
1.67 noro 4020: IndArray ivect;
4021: unsigned char *ivc;
4022: unsigned short *ivs;
4023: unsigned int *ivi;
1.65 noro 4024: NDV redv;
1.67 noro 4025: NMV mr;
1.65 noro 4026: NODE rp;
1.76 noro 4027: int maxrs;
1.65 noro 4028:
1.76 noro 4029: maxrs = 0;
1.74 noro 4030: for ( i = 0; i < nred; i++ ) {
1.65 noro 4031: ivect = imat[i];
1.67 noro 4032: k = ivect->head; svect[k] %= m;
1.65 noro 4033: if ( c = svect[k] ) {
1.76 noro 4034: maxrs = MAX(maxrs,rp0[i]->sugar);
1.74 noro 4035: c = m-c; redv = nd_ps[rp0[i]->index];
1.67 noro 4036: len = LEN(redv); mr = BDY(redv);
4037: svect[k] = 0; prev = k;
4038: switch ( ivect->width ) {
4039: case 1:
4040: ivc = ivect->index.c;
4041: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4042: pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
4043: prev = pos;
4044: DMA(c1,c,c2,up,lo);
4045: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4046: } else svect[pos] = lo;
4047: }
4048: break;
4049: case 2:
4050: ivs = ivect->index.s;
4051: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4052: pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
4053: prev = pos;
4054: DMA(c1,c,c2,up,lo);
4055: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4056: } else svect[pos] = lo;
4057: }
4058: break;
4059: case 4:
4060: ivi = ivect->index.i;
4061: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4062: pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
4063: prev = pos;
4064: DMA(c1,c,c2,up,lo);
4065: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4066: } else svect[pos] = lo;
4067: }
4068: break;
1.65 noro 4069: }
4070: }
4071: }
1.66 noro 4072: for ( i = 0; i < col; i++ )
4073: if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76 noro 4074: return maxrs;
1.65 noro 4075: }
4076:
1.76 noro 4077: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72 noro 4078: {
4079: int i,j,k,len,pos,prev;
4080: UINT c,c1,c2,c3,up,lo,dmy;
4081: IndArray ivect;
4082: unsigned char *ivc;
4083: unsigned short *ivs;
4084: unsigned int *ivi;
4085: NDV redv;
4086: NMV mr;
4087: NODE rp;
1.76 noro 4088: int maxrs;
1.72 noro 4089:
1.76 noro 4090: maxrs = 0;
1.74 noro 4091: for ( i = 0; i < nred; i++ ) {
1.72 noro 4092: ivect = imat[i];
4093: k = ivect->head; svect[k] %= m;
4094: if ( c = svect[k] ) {
1.76 noro 4095: maxrs = MAX(maxrs,rp0[i]->sugar);
1.74 noro 4096: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72 noro 4097: len = LEN(redv); mr = BDY(redv);
4098: svect[k] = 0; prev = k;
4099: switch ( ivect->width ) {
4100: case 1:
4101: ivc = ivect->index.c;
4102: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4103: pos = prev+ivc[j]; prev = pos;
4104: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4105: }
4106: break;
4107: case 2:
4108: ivs = ivect->index.s;
4109: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4110: pos = prev+ivs[j]; prev = pos;
4111: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4112: }
4113: break;
4114: case 4:
4115: ivi = ivect->index.i;
4116: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4117: pos = prev+ivi[j]; prev = pos;
4118: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4119: }
4120: break;
4121: }
4122: }
4123: }
1.76 noro 4124: return maxrs;
1.72 noro 4125: }
4126:
1.65 noro 4127: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
4128: {
4129: int j,k,len;
4130: UINT *p;
4131: UINT c;
4132: NDV r;
4133: NMV mr0,mr;
4134:
4135: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
4136: if ( !len ) return 0;
4137: else {
1.74 noro 4138: mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103 noro 4139: #if 0
1.74 noro 4140: ndv_alloc += nmv_adv*len;
1.103 noro 4141: #endif
1.65 noro 4142: mr = mr0;
4143: p = s0vect;
4144: for ( j = k = 0; j < col; j++, p += nd_wpd )
4145: if ( !rhead[j] ) {
4146: if ( c = vect[k++] ) {
4147: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
4148: }
4149: }
4150: MKNDV(nd_nvar,mr0,len,r);
4151: return r;
4152: }
4153: }
4154:
1.113 noro 4155: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107 noro 4156: {
4157: int j,k,len;
4158: UINT *p;
1.113 noro 4159: Q c;
1.107 noro 4160: NDV r;
4161: NMV mr0,mr;
4162:
4163: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
4164: if ( !len ) return 0;
4165: else {
1.109 noro 4166: mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107 noro 4167: #if 0
4168: ndv_alloc += nmv_adv*len;
4169: #endif
4170: mr = mr0;
4171: p = s0vect;
4172: for ( j = k = 0; j < col; j++, p += nd_wpd )
4173: if ( !rhead[j] ) {
4174: if ( c = vect[k++] ) {
1.113 noro 4175: if ( DN(c) )
4176: error("afo");
4177: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
1.107 noro 4178: }
4179: }
4180: MKNDV(nd_nvar,mr0,len,r);
4181: return r;
4182: }
4183: }
4184:
1.74 noro 4185: int nd_sp_f4(int m,ND_pairs l,PGeoBucket bucket)
1.65 noro 4186: {
4187: ND_pairs t;
4188: NODE sp0,sp;
4189: int stat;
4190: ND spol;
4191:
4192: for ( t = l; t; t = NEXT(t) ) {
4193: stat = nd_sp(m,0,t,&spol);
4194: if ( !stat ) return 0;
4195: if ( spol ) {
4196: add_pbucket_symbolic(bucket,spol);
4197: }
4198: }
1.68 noro 4199: return 1;
1.65 noro 4200: }
4201:
4202: int nd_symbolic_preproc(PGeoBucket bucket,UINT **s0vect,NODE *r)
4203: {
4204: NODE rp0,rp;
4205: NM mul,head,s0,s;
1.76 noro 4206: int index,col,i,sugar;
1.65 noro 4207: RHist h;
4208: UINT *s0v,*p;
4209: NM_ind_pair pair;
4210: ND red;
4211:
4212: s0 = 0; rp0 = 0; col = 0;
4213: while ( 1 ) {
4214: head = remove_head_pbucket_symbolic(bucket);
4215: if ( !head ) break;
4216: if ( !s0 ) s0 = head;
4217: else NEXT(s) = head;
4218: s = head;
4219: index = ndl_find_reducer(DL(head));
4220: if ( index >= 0 ) {
4221: h = nd_psh[index];
4222: NEWNM(mul);
4223: ndl_sub(DL(head),DL(h),DL(mul));
4224: if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.76 noro 4225: sugar = TD(DL(mul))+SG(nd_ps[index]);
4226: MKNM_ind_pair(pair,mul,index,sugar);
1.65 noro 4227: red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
4228: add_pbucket_symbolic(bucket,nd_remove_head(red));
4229: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
4230: }
4231: col++;
4232: }
1.72 noro 4233: if ( rp0 ) NEXT(rp) = 0;
4234: NEXT(s) = 0;
1.65 noro 4235: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
4236: for ( i = 0, p = s0v, s = s0; i < col;
4237: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
4238: *s0vect = s0v;
4239: *r = rp0;
4240: return col;
4241: }
4242:
1.69 noro 4243: NODE nd_f4(int m)
4244: {
4245: int i,nh,stat,index;
4246: NODE r,g;
4247: ND_pairs d,l,t;
4248: ND spol,red;
4249: NDV nf,redv;
4250: NM s0,s;
1.84 noro 4251: NODE rp0,srp0,nflist;
1.69 noro 4252: int nsp,nred,col,rank,len,k,j,a;
4253: UINT c;
1.74 noro 4254: UINT **spmat;
1.69 noro 4255: UINT *s0vect,*svect,*p,*v;
4256: int *colstat;
4257: IndArray *imat;
4258: int *rhead;
4259: int spcol,sprow;
4260: int sugar;
4261: PGeoBucket bucket;
4262: struct oEGT eg0,eg1,eg_f4;
4263:
1.103 noro 4264: #if 0
1.74 noro 4265: ndv_alloc = 0;
1.103 noro 4266: #endif
1.69 noro 4267: g = 0; d = 0;
4268: for ( i = 0; i < nd_psn; i++ ) {
4269: d = update_pairs(d,g,i);
4270: g = update_base(g,i);
4271: }
4272: while ( d ) {
4273: get_eg(&eg0);
4274: l = nd_minsugarp(d,&d);
4275: sugar = SG(l);
4276: bucket = create_pbucket();
1.74 noro 4277: stat = nd_sp_f4(m,l,bucket);
1.69 noro 4278: if ( !stat ) {
4279: for ( t = l; NEXT(t); t = NEXT(t) );
4280: NEXT(t) = d; d = l;
1.103 noro 4281: d = nd_reconstruct(0,d);
1.69 noro 4282: continue;
4283: }
1.86 noro 4284: if ( bucket->m < 0 ) continue;
1.69 noro 4285: col = nd_symbolic_preproc(bucket,&s0vect,&rp0);
4286: if ( !col ) {
4287: for ( t = l; NEXT(t); t = NEXT(t) );
4288: NEXT(t) = d; d = l;
1.103 noro 4289: d = nd_reconstruct(0,d);
1.69 noro 4290: continue;
4291: }
4292: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74 noro 4293: if ( DP_Print )
4294: fprintf(asir_out,"sugar=%d,symb=%fsec,",
4295: sugar,eg_f4.exectime+eg_f4.gctime);
4296: if ( 1 )
4297: nflist = nd_f4_red(m,l,s0vect,col,rp0);
4298: else
4299: nflist = nd_f4_red_dist(m,l,s0vect,col,rp0);
1.69 noro 4300: /* adding new bases */
1.74 noro 4301: for ( r = nflist; r; r = NEXT(r) ) {
4302: nf = (NDV)BDY(r);
1.69 noro 4303: ndv_removecont(m,nf);
1.77 noro 4304: nh = ndv_newps(m,nf,0);
1.69 noro 4305: d = update_pairs(d,g,nh);
4306: g = update_base(g,nh);
4307: }
4308: }
4309: for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.103 noro 4310: #if 0
1.74 noro 4311: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103 noro 4312: #endif
1.69 noro 4313: return g;
4314: }
1.74 noro 4315:
4316: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
1.63 noro 4317: {
1.67 noro 4318: IndArray *imat;
1.106 noro 4319: int nsp,nred,i;
1.65 noro 4320: int *rhead;
1.106 noro 4321: NODE r0,rp;
1.74 noro 4322: ND_pairs sp;
4323: NM_ind_pair *rvect;
1.63 noro 4324:
1.74 noro 4325: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
1.106 noro 4326: nred = length(rp0);
1.74 noro 4327: imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
4328: rhead = (int *)ALLOCA(col*sizeof(int));
4329: for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63 noro 4330:
1.74 noro 4331: /* construction of index arrays */
4332: rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
4333: for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
4334: rvect[i] = (NM_ind_pair)BDY(rp);
4335: imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
4336: rhead[imat[i]->head] = 1;
4337: }
1.107 noro 4338: if ( m )
4339: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
4340: else
1.113 noro 4341: r0 = nd_f4_red_q_main(sp0,nsp,s0vect,col,rvect,rhead,imat,nred);
1.106 noro 4342: return r0;
4343: }
1.74 noro 4344:
1.106 noro 4345: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
4346: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
4347: {
4348: int spcol,sprow,a;
4349: int i,j,k,l,rank;
4350: NODE r0,r;
4351: ND_pairs sp;
4352: ND spol;
4353: int **spmat;
4354: UINT *svect,*v;
4355: int *colstat;
4356: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
4357: int maxrs;
4358: int *spsugar;
4359:
4360: spcol = col-nred;
4361: get_eg(&eg0);
1.74 noro 4362: /* elimination (1st step) */
4363: spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
4364: svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76 noro 4365: spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74 noro 4366: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
4367: nd_sp(m,0,sp,&spol);
1.75 noro 4368: if ( !spol ) continue;
1.74 noro 4369: nd_to_vect(m,s0vect,col,spol,svect);
1.76 noro 4370: if ( m == -1 )
4371: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
4372: else
4373: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74 noro 4374: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
4375: if ( i < col ) {
4376: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
4377: for ( j = k = 0; j < col; j++ )
4378: if ( !rhead[j] ) v[k++] = svect[j];
1.76 noro 4379: spsugar[sprow] = MAX(maxrs,SG(spol));
1.74 noro 4380: sprow++;
4381: }
1.76 noro 4382: nd_free(spol);
1.74 noro 4383: }
1.80 noro 4384: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
4385: if ( DP_Print ) {
4386: fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
4387: fflush(asir_out);
4388: }
1.74 noro 4389: /* free index arrays */
4390: for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
4391:
4392: /* elimination (2nd step) */
4393: colstat = (int *)ALLOCA(spcol*sizeof(int));
4394: if ( m == -1 )
1.76 noro 4395: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4396: else
1.76 noro 4397: rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4398: r0 = 0;
4399: for ( i = 0; i < rank; i++ ) {
4400: NEXTNODE(r0,r); BDY(r) =
4401: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76 noro 4402: SG((NDV)BDY(r)) = spsugar[i];
1.74 noro 4403: GC_free(spmat[i]);
4404: }
1.109 noro 4405: if ( r0 ) NEXT(r) = 0;
1.74 noro 4406: for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80 noro 4407: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
4408: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74 noro 4409: if ( DP_Print ) {
1.80 noro 4410: fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74 noro 4411: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
4412: nsp,nred,sprow,spcol,rank);
4413: fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63 noro 4414: }
1.74 noro 4415: return r0;
4416: }
4417:
1.113 noro 4418: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.107 noro 4419: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
4420: {
4421: int spcol,sprow,a;
4422: int i,j,k,l,rank;
4423: NODE r0,r;
4424: ND_pairs sp;
4425: ND spol;
1.113 noro 4426: Q **spmat;
4427: Q *svect,*v;
1.107 noro 4428: int *colstat;
4429: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
4430: int maxrs;
4431: int *spsugar;
4432:
4433: spcol = col-nred;
4434: get_eg(&eg0);
4435: /* elimination (1st step) */
1.113 noro 4436: spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
4437: svect = (Q *)ALLOCA(col*sizeof(Q));
4438: spsugar = (int *)ALLOCA(nsp*sizeof(Q));
1.107 noro 4439: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
4440: nd_sp(0,0,sp,&spol);
4441: if ( !spol ) continue;
1.113 noro 4442: nd_to_vect_q(s0vect,col,spol,svect);
4443: maxrs = ndv_reduce_vect_q(svect,col,imat,rvect,nred);
1.107 noro 4444: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
4445: if ( i < col ) {
1.113 noro 4446: spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
1.107 noro 4447: for ( j = k = 0; j < col; j++ )
4448: if ( !rhead[j] ) v[k++] = svect[j];
4449: spsugar[sprow] = MAX(maxrs,SG(spol));
4450: sprow++;
4451: }
1.109 noro 4452: /* nd_free(spol); */
1.107 noro 4453: }
4454: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
4455: if ( DP_Print ) {
4456: fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
4457: fflush(asir_out);
4458: }
4459: /* free index arrays */
1.109 noro 4460: /* for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
1.107 noro 4461:
4462: /* elimination (2nd step) */
4463: colstat = (int *)ALLOCA(spcol*sizeof(int));
1.113 noro 4464: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.107 noro 4465: r0 = 0;
4466: for ( i = 0; i < rank; i++ ) {
4467: NEXTNODE(r0,r); BDY(r) =
1.113 noro 4468: (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
1.107 noro 4469: SG((NDV)BDY(r)) = spsugar[i];
1.109 noro 4470: /* GC_free(spmat[i]); */
1.107 noro 4471: }
1.109 noro 4472: if ( r0 ) NEXT(r) = 0;
4473:
4474: /* for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
1.107 noro 4475: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
4476: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
4477: if ( DP_Print ) {
4478: fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
4479: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
4480: nsp,nred,sprow,spcol,rank);
4481: fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
4482: }
4483: return r0;
4484: }
4485:
1.74 noro 4486: FILE *nd_write,*nd_read;
4487:
4488: void nd_send_int(int a) {
4489: write_int(nd_write,&a);
4490: }
4491:
4492: void nd_send_intarray(int *p,int len) {
4493: write_intarray(nd_write,p,len);
4494: }
4495:
4496: int nd_recv_int() {
4497: int a;
4498:
4499: read_int(nd_read,&a);
4500: return a;
4501: }
4502:
4503: void nd_recv_intarray(int *p,int len) {
4504: read_intarray(nd_read,p,len);
4505: }
4506:
4507: void nd_send_ndv(NDV p) {
4508: int len,i;
4509: NMV m;
4510:
4511: if ( !p ) nd_send_int(0);
4512: else {
4513: len = LEN(p);
4514: nd_send_int(len);
4515: m = BDY(p);
4516: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4517: nd_send_int(CM(m));
4518: nd_send_intarray(DL(m),nd_wpd);
1.68 noro 4519: }
1.74 noro 4520: }
4521: }
4522:
4523: void nd_send_nd(ND p) {
4524: int len,i;
4525: NM m;
4526:
4527: if ( !p ) nd_send_int(0);
4528: else {
4529: len = LEN(p);
4530: nd_send_int(len);
4531: m = BDY(p);
4532: for ( i = 0; i < len; i++, m = NEXT(m) ) {
4533: nd_send_int(CM(m));
4534: nd_send_intarray(DL(m),nd_wpd);
1.65 noro 4535: }
1.74 noro 4536: }
4537: }
1.65 noro 4538:
1.74 noro 4539: NDV nd_recv_ndv()
4540: {
4541: int len,i;
4542: NMV m,m0;
4543: NDV r;
1.65 noro 4544:
1.74 noro 4545: len = nd_recv_int();
4546: if ( !len ) return 0;
4547: else {
4548: m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103 noro 4549: #if 0
1.74 noro 4550: ndv_alloc += len*nmv_adv;
1.103 noro 4551: #endif
1.74 noro 4552: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4553: CM(m) = nd_recv_int();
4554: nd_recv_intarray(DL(m),nd_wpd);
1.71 noro 4555: }
1.74 noro 4556: MKNDV(nd_nvar,m0,len,r);
4557: return r;
4558: }
4559: }
1.65 noro 4560:
1.74 noro 4561: int ox_exec_f4_red(Q proc)
4562: {
4563: Obj obj;
4564: STRING fname;
4565: NODE arg;
4566: int s;
4567: extern int ox_need_conv,ox_file_io;
4568:
4569: MKSTR(fname,"nd_exec_f4_red");
4570: arg = mknode(2,proc,fname);
4571: Pox_cmo_rpc(arg,&obj);
4572: s = get_ox_server_id(QTOS(proc));
4573: nd_write = iofp[s].out;
4574: nd_read = iofp[s].in;
4575: ox_need_conv = ox_file_io = 0;
4576: return s;
4577: }
4578:
4579: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
4580: {
4581: int nsp,nred;
4582: int i,rank,s;
4583: NODE rp,r0,r;
4584: ND_pairs sp;
4585: NM_ind_pair pair;
4586: NMV nmv;
4587: NM nm;
4588: NDV nf;
4589: Obj proc,dmy;
4590:
4591: ox_launch_main(0,0,&proc);
4592: s = ox_exec_f4_red((Q)proc);
4593:
4594: nd_send_int(m);
4595: nd_send_int(nd_nvar);
4596: nd_send_int(nd_bpe);
4597: nd_send_int(nd_wpd);
4598: nd_send_int(nmv_adv);
4599:
1.89 noro 4600: saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74 noro 4601:
4602: nd_send_int(nd_psn);
4603: for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
4604:
4605: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
4606: nd_send_int(nsp);
4607: for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
4608: nd_send_int(sp->i1); nd_send_int(sp->i2);
4609: }
4610:
4611: nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
4612:
4613: nred = length(rp0); nd_send_int(nred);
4614: for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
4615: pair = (NM_ind_pair)BDY(rp);
4616: nd_send_int(pair->index);
4617: nd_send_intarray(pair->mul->dl,nd_wpd);
4618: }
4619: fflush(nd_write);
4620: rank = nd_recv_int();
4621: fprintf(asir_out,"rank=%d\n",rank);
4622: r0 = 0;
4623: for ( i = 0; i < rank; i++ ) {
4624: nf = nd_recv_ndv();
4625: NEXTNODE(r0,r); BDY(r) = (pointer)nf;
4626: }
4627: Pox_shutdown(mknode(1,proc),&dmy);
4628: return r0;
4629: }
4630:
4631: /* server side */
4632:
4633: void nd_exec_f4_red_dist()
4634: {
4635: int m,i,nsp,col,s0size,nred,spcol,j,k;
4636: NM_ind_pair *rp0;
4637: NDV nf;
4638: UINT *s0vect;
4639: IndArray *imat;
4640: int *rhead;
4641: int **spmat;
4642: UINT *svect,*v;
4643: ND_pairs *sp0;
4644: int *colstat;
4645: int a,sprow,rank;
1.89 noro 4646: struct order_spec *ord;
1.74 noro 4647: Obj ordspec;
4648: ND spol;
1.76 noro 4649: int maxrs;
4650: int *spsugar;
1.74 noro 4651:
4652: nd_read = iofp[0].in;
4653: nd_write = iofp[0].out;
4654: m = nd_recv_int();
4655: nd_nvar = nd_recv_int();
4656: nd_bpe = nd_recv_int();
4657: nd_wpd = nd_recv_int();
4658: nmv_adv = nd_recv_int();
4659:
4660: loadobj(nd_read,&ordspec);
1.89 noro 4661: create_order_spec(0,ordspec,&ord);
4662: nd_init_ord(ord);
1.74 noro 4663: nd_setup_parameters(nd_nvar,0);
4664:
4665: nd_psn = nd_recv_int();
4666: nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
4667: nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
4668: for ( i = 0; i < nd_psn; i++ ) {
4669: nd_ps[i] = nd_recv_ndv();
4670: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
4671: }
4672:
4673: nsp = nd_recv_int();
4674: sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
4675: for ( i = 0; i < nsp; i++ ) {
4676: NEWND_pairs(sp0[i]);
4677: sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
4678: ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
4679: }
4680:
4681: col = nd_recv_int();
4682: s0size = col*nd_wpd;
4683: s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
4684: nd_recv_intarray(s0vect,s0size);
4685:
4686: nred = nd_recv_int();
4687: rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
4688: for ( i = 0; i < nred; i++ ) {
4689: rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
4690: rp0[i]->index = nd_recv_int();
4691: rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4692: nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
4693: }
4694:
4695: spcol = col-nred;
4696: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
4697: rhead = (int *)MALLOC(col*sizeof(int));
4698: for ( i = 0; i < col; i++ ) rhead[i] = 0;
4699:
4700: /* construction of index arrays */
4701: for ( i = 0; i < nred; i++ ) {
4702: imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
4703: rhead[imat[i]->head] = 1;
4704: }
4705:
4706: /* elimination (1st step) */
4707: spmat = (int **)MALLOC(nsp*sizeof(UINT *));
4708: svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76 noro 4709: spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74 noro 4710: for ( a = sprow = 0; a < nsp; a++ ) {
4711: nd_sp(m,0,sp0[a],&spol);
1.75 noro 4712: if ( !spol ) continue;
1.74 noro 4713: nd_to_vect(m,s0vect,col,spol,svect);
1.76 noro 4714: if ( m == -1 )
4715: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
4716: else
4717: maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74 noro 4718: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
4719: if ( i < col ) {
4720: spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
4721: for ( j = k = 0; j < col; j++ )
4722: if ( !rhead[j] ) v[k++] = svect[j];
1.76 noro 4723: spsugar[sprow] = MAX(maxrs,SG(spol));
1.74 noro 4724: sprow++;
4725: }
1.76 noro 4726: nd_free(spol);
1.74 noro 4727: }
4728: /* elimination (2nd step) */
4729: colstat = (int *)ALLOCA(spcol*sizeof(int));
4730: if ( m == -1 )
1.76 noro 4731: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4732: else
1.76 noro 4733: rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4734: nd_send_int(rank);
4735: for ( i = 0; i < rank; i++ ) {
4736: nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
4737: nd_send_ndv(nf);
1.63 noro 4738: }
1.74 noro 4739: fflush(nd_write);
1.107 noro 4740: }
4741:
1.113 noro 4742: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107 noro 4743: {
1.109 noro 4744: int mod,i,j,t,c,rank,rank0,inv;
4745: int *ci,*ri;
1.113 noro 4746: Q dn;
1.109 noro 4747: MAT m,nm;
1.108 noro 4748: int **wmat;
4749:
4750: /* XXX */
4751: mod = 99999989;
4752: wmat = (int **)ALLOCA(row*sizeof(int *));
4753: for ( i = 0; i < row; i++ ) {
4754: wmat[i] = (int *)ALLOCA(col*sizeof(int));
4755: for ( j = 0; j < col; j++ ) {
1.113 noro 4756: if ( mat0[i][j] ) {
4757: t = rem(NM(mat0[i][j]),mod);
4758: if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
4759: wmat[i][j] = t;
4760: } else
1.108 noro 4761: wmat[i][j] = 0;
4762: }
4763: }
1.109 noro 4764: rank0 = nd_gauss_elim_mod(wmat,sugar,row,col,mod,colstat);
4765: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.113 noro 4766: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.109 noro 4767: if ( rank != rank0 )
4768: error("afo");
4769: for ( i = 0; i < row; i++ )
4770: for ( j = 0; j < col; j++ )
4771: mat0[i][j] = 0;
4772: c = col-rank;
4773: for ( i = 0; i < rank; i++ ) {
1.113 noro 4774: mat0[i][ri[i]] = dn;
1.109 noro 4775: for ( j = 0; j < c; j++ )
1.113 noro 4776: mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
1.109 noro 4777: }
1.113 noro 4778: inv = invm(rem(NM(dn),mod),mod);
4779: if ( SGN(dn) < 0 ) inv = mod-inv;
1.109 noro 4780: for ( i = 0; i < row; i++ )
4781: for ( j = 0; j < col; j++ ) {
1.113 noro 4782: if ( mat0[i][j] ) {
4783: t = rem(NM(mat0[i][j]),mod);
4784: if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
4785: } else
1.109 noro 4786: t = 0;
4787: c = dmar(t,inv,0,mod);
4788: if ( wmat[i][j] != c )
4789: error("afo");
4790: }
4791: return rank;
1.76 noro 4792: }
4793:
4794: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat)
4795: {
4796: int i,j,k,l,inv,a,rank,s;
4797: unsigned int *t,*pivot,*pk;
4798: unsigned int **mat;
4799:
4800: mat = (unsigned int **)mat0;
4801: for ( rank = 0, j = 0; j < col; j++ ) {
4802: for ( i = rank; i < row; i++ )
4803: mat[i][j] %= md;
4804: for ( i = rank; i < row; i++ )
4805: if ( mat[i][j] )
4806: break;
4807: if ( i == row ) {
4808: colstat[j] = 0;
4809: continue;
4810: } else
4811: colstat[j] = 1;
4812: if ( i != rank ) {
4813: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
4814: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
4815: }
4816: pivot = mat[rank];
4817: s = sugar[rank];
4818: inv = invm(pivot[j],md);
4819: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
4820: if ( *pk ) {
4821: if ( *pk >= (unsigned int)md )
4822: *pk %= md;
4823: DMAR(*pk,inv,0,md,*pk)
4824: }
4825: for ( i = rank+1; i < row; i++ ) {
4826: t = mat[i];
4827: if ( a = t[j] ) {
4828: sugar[i] = MAX(sugar[i],s);
4829: red_by_vect(md,t+j,pivot+j,md-a,col-j);
4830: }
4831: }
4832: rank++;
4833: }
4834: for ( j = col-1, l = rank-1; j >= 0; j-- )
4835: if ( colstat[j] ) {
4836: pivot = mat[l];
4837: s = sugar[l];
4838: for ( i = 0; i < l; i++ ) {
4839: t = mat[i];
4840: t[j] %= md;
4841: if ( a = t[j] ) {
4842: sugar[i] = MAX(sugar[i],s);
4843: red_by_vect(md,t+j,pivot+j,md-a,col-j);
4844: }
4845: }
4846: l--;
4847: }
4848: for ( j = 0, l = 0; l < rank; j++ )
4849: if ( colstat[j] ) {
4850: t = mat[l];
4851: for ( k = j; k < col; k++ )
4852: if ( t[k] >= (unsigned int)md )
4853: t[k] %= md;
4854: l++;
4855: }
4856: return rank;
4857: }
4858:
4859: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
4860: {
4861: int i,j,k,l,inv,a,rank,s;
4862: unsigned int *t,*pivot,*pk;
4863: unsigned int **mat;
4864:
4865: mat = (unsigned int **)mat0;
4866: for ( rank = 0, j = 0; j < col; j++ ) {
4867: for ( i = rank; i < row; i++ )
4868: if ( mat[i][j] )
4869: break;
4870: if ( i == row ) {
4871: colstat[j] = 0;
4872: continue;
4873: } else
4874: colstat[j] = 1;
4875: if ( i != rank ) {
4876: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
4877: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
4878: }
4879: pivot = mat[rank];
4880: s = sugar[rank];
4881: inv = _invsf(pivot[j]);
4882: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
4883: if ( *pk )
4884: *pk = _mulsf(*pk,inv);
4885: for ( i = rank+1; i < row; i++ ) {
4886: t = mat[i];
4887: if ( a = t[j] ) {
4888: sugar[i] = MAX(sugar[i],s);
4889: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
4890: }
4891: }
4892: rank++;
4893: }
4894: for ( j = col-1, l = rank-1; j >= 0; j-- )
4895: if ( colstat[j] ) {
4896: pivot = mat[l];
4897: s = sugar[l];
4898: for ( i = 0; i < l; i++ ) {
4899: t = mat[i];
4900: if ( a = t[j] ) {
4901: sugar[i] = MAX(sugar[i],s);
4902: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
4903: }
4904: }
4905: l--;
4906: }
4907: return rank;
1.77 noro 4908: }
4909:
4910: int ndv_ishomo(NDV p)
4911: {
4912: NMV m;
4913: int len,h;
4914:
4915: if ( !p ) return 1;
4916: len = LEN(p);
4917: m = BDY(p);
4918: h = TD(DL(m));
4919: NMV_ADV(m);
4920: for ( len--; len; len--, NMV_ADV(m) )
4921: if ( TD(DL(m)) != h ) return 0;
4922: return 1;
4923: }
4924:
4925: void ndv_save(NDV p,int index)
4926: {
4927: FILE *s;
4928: char name[BUFSIZ];
4929: short id;
4930: int nv,sugar,len,n,i,td,e,j;
4931: NMV m;
4932: unsigned int *dl;
4933:
4934: sprintf(name,"%s/%d",Demand,index);
4935: s = fopen(name,"w");
4936: savevl(s,0);
4937: if ( !p ) {
4938: saveobj(s,0);
4939: return;
4940: }
4941: id = O_DP;
4942: nv = NV(p);
4943: sugar = SG(p);
4944: len = LEN(p);
4945: write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
4946: write_int(s,&len);
4947:
4948: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113 noro 4949: saveobj(s,(Obj)CQ(m));
1.77 noro 4950: dl = DL(m);
4951: td = TD(dl);
4952: write_int(s,&td);
4953: for ( j = 0; j < nv; j++ ) {
4954: e = GET_EXP(dl,j);
4955: write_int(s,&e);
4956: }
4957: }
4958: fclose(s);
4959: }
4960:
4961: NDV ndv_load(int index)
4962: {
4963: FILE *s;
4964: char name[BUFSIZ];
4965: short id;
4966: int nv,sugar,len,n,i,td,e,j;
4967: NDV d;
4968: NMV m0,m;
4969: unsigned int *dl;
4970: Obj obj;
4971:
4972: sprintf(name,"%s/%d",Demand,index);
4973: s = fopen(name,"r");
4974: if ( !s ) return 0;
4975:
4976: skipvl(s);
4977: read_short(s,&id);
4978: if ( !id ) return 0;
4979: read_int(s,&nv);
4980: read_int(s,&sugar);
4981: read_int(s,&len);
4982:
4983: m0 = m = MALLOC(len*nmv_adv);
4984: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.113 noro 4985: loadobj(s,&obj); CQ(m) = (Q)obj;
1.77 noro 4986: dl = DL(m);
4987: ndl_zero(dl);
4988: read_int(s,&td); TD(dl) = td;
4989: for ( j = 0; j < nv; j++ ) {
4990: read_int(s,&e);
4991: PUT_EXP(dl,j,e);
4992: }
4993: if ( nd_blockmask ) ndl_weight_mask(dl);
4994: }
4995: fclose(s);
4996: MKNDV(nv,m0,len,d);
4997: SG(d) = sugar;
4998: return d;
1.99 noro 4999: }
5000:
1.102 noro 5001: void nd_det(int mod,MAT f,P *rp)
1.99 noro 5002: {
5003: VL fv,tv;
1.102 noro 5004: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
1.99 noro 5005: pointer **m;
1.113 noro 5006: Q mone;
1.102 noro 5007: NDV **dm;
5008: NDV *t,*mi,*mj;
5009: NDV d,s,mij,mjj;
5010: ND u;
5011: NMV nmv;
1.114 noro 5012: UINT *bound;
1.99 noro 5013: PGeoBucket bucket;
5014: struct order_spec *ord;
5015:
5016: create_order_spec(0,0,&ord);
5017: nd_init_ord(ord);
5018: get_vars((Obj)f,&fv);
5019: if ( f->row != f->col )
5020: error("nd_det : non-square matrix");
5021: n = f->row;
5022: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
5023: m = f->body;
5024: for ( i = 0, max = 0; i < n; i++ )
5025: for ( j = 0; j < n; j++ )
5026: for ( tv = fv; tv; tv = NEXT(tv) ) {
5027: e = getdeg(tv->v,(P)m[i][j]);
5028: max = MAX(e,max);
5029: }
1.114 noro 5030: nd_setup_parameters(nvar,max);
1.102 noro 5031: dm = (NDV **)almat_pointer(n,n);
1.99 noro 5032: for ( i = 0, max = 0; i < n; i++ )
1.102 noro 5033: for ( j = 0; j < n; j++ ) {
5034: dm[i][j] = ptondv(CO,fv,m[i][j]);
5035: if ( mod ) ndv_mod(mod,dm[i][j]);
5036: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
5037: }
5038: d = ptondv(CO,fv,(P)ONE);
5039: if ( mod ) ndv_mod(mod,d);
1.113 noro 5040: chsgnq(ONE,&mone);
1.99 noro 5041: for ( j = 0, sgn = 1; j < n; j++ ) {
1.104 noro 5042: if ( DP_Print ) fprintf(stderr,"j=%d\n",j);
1.99 noro 5043: for ( i = j; i < n && !dm[i][j]; i++ );
5044: if ( i == n ) {
5045: *rp = 0;
5046: return;
5047: }
1.102 noro 5048: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
1.99 noro 5049: for ( k = j; k < n; k++ )
5050: for ( l = j; l < n; l++ )
1.102 noro 5051: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
5052: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
1.99 noro 5053: }
5054: if ( k0 != j ) {
5055: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
5056: sgn = -sgn;
5057: }
5058: if ( l0 != j ) {
5059: for ( k = j; k < n; k++ ) {
5060: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
5061: }
5062: sgn = -sgn;
5063: }
1.114 noro 5064: bound = nd_det_compute_bound(dm,n,j);
5065: if ( ndl_check_bound(bound,bound) )
5066: nd_det_reconstruct(dm,n,j,d);
5067:
1.99 noro 5068: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.114 noro 5069: /* if ( DP_Print ) fprintf(stderr," i=%d\n ",i); */
1.99 noro 5070: mi = dm[i]; mij = mi[j];
1.102 noro 5071: if ( mod )
5072: ndv_mul_c(mod,mij,mod-1);
5073: else
1.113 noro 5074: ndv_mul_c_q(mij,mone);
1.99 noro 5075: for ( k = j+1; k < n; k++ ) {
1.114 noro 5076: /* if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
1.99 noro 5077: bucket = create_pbucket();
1.104 noro 5078: if ( mi[k] ) {
1.102 noro 5079: nmv = BDY(mjj); len = LEN(mjj);
5080: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
5081: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
5082: add_pbucket(mod,bucket,u);
1.99 noro 5083: }
1.104 noro 5084: }
1.99 noro 5085: if ( mj[k] && mij ) {
1.102 noro 5086: nmv = BDY(mij); len = LEN(mij);
5087: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
5088: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
5089: add_pbucket(mod,bucket,u);
1.99 noro 5090: }
5091: }
1.104 noro 5092: u = nd_quo(mod,bucket,d);
1.102 noro 5093: mi[k] = ndtondv(mod,u);
1.99 noro 5094: }
1.114 noro 5095: /* if ( DP_Print ) fprintf(stderr,"\n",k); */
1.99 noro 5096: }
5097: d = mjj;
5098: }
1.100 noro 5099: if ( sgn < 0 )
1.102 noro 5100: if ( mod )
5101: ndv_mul_c(mod,d,mod-1);
5102: else
1.113 noro 5103: ndv_mul_c_q(d,mone);
1.102 noro 5104: *rp = ndvtop(mod,CO,fv,d);
1.99 noro 5105: }
5106:
1.102 noro 5107: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99 noro 5108: {
5109: NM mr,mr0;
1.102 noro 5110: NM tnm;
5111: NMV m;
1.99 noro 5112: UINT *d0,*dt,*dm;
5113: int c,n,td,i,c1,c2,len;
1.113 noro 5114: Q q;
1.99 noro 5115: ND r;
5116:
5117: if ( !p ) return 0;
5118: else {
1.102 noro 5119: n = NV(p); m = BDY(p); len = LEN(p);
1.99 noro 5120: d0 = DL(m0);
1.102 noro 5121: td = TD(d);
1.99 noro 5122: mr0 = 0;
5123: NEWNM(tnm);
1.102 noro 5124: if ( mod ) {
5125: c = CM(m0);
5126: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5127: ndl_add(DL(m),d0,DL(tnm));
5128: if ( ndl_reducible(DL(tnm),d) ) {
5129: NEXTNM(mr0,mr);
5130: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
1.104 noro 5131: ndl_copy(DL(tnm),DL(mr));
1.102 noro 5132: }
5133: }
5134: } else {
1.113 noro 5135: q = CQ(m0);
1.102 noro 5136: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5137: ndl_add(DL(m),d0,DL(tnm));
5138: if ( ndl_reducible(DL(tnm),d) ) {
5139: NEXTNM(mr0,mr);
1.113 noro 5140: mulq(CQ(m),q,&CQ(mr));
1.104 noro 5141: ndl_copy(DL(tnm),DL(mr));
1.102 noro 5142: }
1.99 noro 5143: }
5144: }
5145: if ( !mr0 )
5146: return 0;
5147: else {
5148: NEXT(mr) = 0;
1.105 noro 5149: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
1.99 noro 5150: MKND(NV(p),mr0,len,r);
5151: SG(r) = SG(p) + TD(d0);
5152: return r;
5153: }
5154: }
1.114 noro 5155: }
5156:
5157: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
5158: {
5159: int i,obpe,oadv,h,k,l;
5160: static NM prev_nm_free_list;
5161: EPOS oepos;
5162:
5163: obpe = nd_bpe;
5164: oadv = nmv_adv;
5165: oepos = nd_epos;
5166: if ( obpe < 2 ) nd_bpe = 2;
5167: else if ( obpe < 3 ) nd_bpe = 3;
5168: else if ( obpe < 4 ) nd_bpe = 4;
5169: else if ( obpe < 5 ) nd_bpe = 5;
5170: else if ( obpe < 6 ) nd_bpe = 6;
5171: else if ( obpe < 8 ) nd_bpe = 8;
5172: else if ( obpe < 10 ) nd_bpe = 10;
5173: else if ( obpe < 16 ) nd_bpe = 16;
5174: else if ( obpe < 32 ) nd_bpe = 32;
5175: else error("nd_det_reconstruct : exponent too large");
5176:
5177: nd_setup_parameters(nd_nvar,0);
5178: prev_nm_free_list = _nm_free_list;
5179: _nm_free_list = 0;
5180: for ( k = j; k < n; k++ )
5181: for (l = j; l < n; l++ )
5182: ndv_realloc(dm[k][l],obpe,oadv,oepos);
5183: ndv_realloc(d,obpe,oadv,oepos);
5184: prev_nm_free_list = 0;
5185: #if 0
5186: GC_gcollect();
5187: #endif
5188: }
5189:
5190: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
5191: {
5192: UINT *d0,*d1,*d,*t,*r;
5193: int k,l;
5194:
5195: d0 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
5196: d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
5197: for ( k = 0; k < nd_wpd; k++ ) d0[k] = 0;
5198: for ( k = j; k < n; k++ )
5199: for ( l = j; l < n; l++ )
5200: if ( dm[k][l] ) {
5201: d = ndv_compute_bound(dm[k][l]);
5202: ndl_lcm(d,d0,d1);
5203: t = d1; d1 = d0; d0 = t;
5204: }
5205: r = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
5206: for ( k = 0; k < nd_wpd; k++ ) r[k] = d0[k];
5207: return r;
1.117 noro 5208: }
5209:
5210: DL nd_separate_d(UINT *d,UINT *trans)
5211: {
5212: int n,ntrans,td,i,e;
5213: DL a;
5214:
5215: n = nd_nvar; ntrans = n-nd_nalg;
5216: ndl_zero(trans);
5217: td = 0;
5218: for ( i = 0; i < ntrans; i++ ) {
5219: e = GET_EXP(d,i);
5220: PUT_EXP(trans,i,e);
5221: td += MUL_WEIGHT(e,i);
5222: }
5223: TD(trans) = td;
5224: if ( nd_blockmask) ndl_weight_mask(trans);
5225: NEWDL(a,nd_nalg);
5226: td = 0;
5227: for ( ; i < n; i++ ) {
5228: e = GET_EXP(d,i);
5229: a->d[i-ntrans] = e;
5230: td += e;
5231: }
5232: a->td = td;
5233: return a;
5234: }
5235:
1.118 noro 5236: int nd_monic(int mod,ND *p)
1.117 noro 5237: {
5238: UINT *trans,*t;
5239: DL alg;
5240: MP mp0,mp;
1.118 noro 5241: NM m,m0,m1,ma0,ma,mb,mr0,mr;
5242: ND r;
1.117 noro 5243: DL dl;
5244: DP nm;
5245: NDV ndv;
1.118 noro 5246: DAlg inv,cd;
1.117 noro 5247: ND s,c;
1.118 noro 5248: Q l,mul;
5249: N ln;
5250: int n,ntrans,i,e,td,is_lc,len;
5251: NumberField nf;
5252: struct oEGT eg0,eg1;
5253:
5254: if ( !(nf = get_numberfield()) )
5255: error("nd_monic : current_numberfield is not set");
1.117 noro 5256:
5257: n = nd_nvar; ntrans = n-nd_nalg;
1.118 noro 5258: /* Q coef -> DAlg coef */
5259: NEWNM(ma0); ma = ma0;
5260: m = BDY(*p);
5261: is_lc = 1;
5262: while ( 1 ) {
5263: NEWMP(mp0); mp = mp0;
5264: mp->c = (P)CQ(m);
5265: mp->dl = nd_separate_d(DL(m),DL(ma));
5266: NEWNM(mb);
5267: for ( m = NEXT(m); m; m = NEXT(m) ) {
5268: alg = nd_separate_d(DL(m),DL(mb));
5269: if ( !ndl_equal(DL(ma),DL(mb)) )
5270: break;
5271: NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
5272: }
5273: NEXT(mp) = 0;
5274: MKDP(nd_nalg,mp0,nm);
5275: MKDAlg(nm,ONE,cd);
5276: if ( is_lc == 1 ) {
5277: /* if the lc is a rational number, we have nothing to do */
5278: if ( !mp0->dl->td )
5279: return 1;
5280:
5281: get_eg(&eg0);
5282: invdalg(cd,&inv);
5283: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
5284: /* check the validity of inv */
5285: if ( mod && !rem(NM(inv->dn),mod) )
5286: return 0;
5287: CA(ma) = nf->one;
5288: is_lc = 0;
5289: ln = ONEN;
5290: } else {
5291: muldalg(cd,inv,&CA(ma));
5292: lcmn(ln,NM(CA(ma)->dn),&ln);
5293: }
5294: if ( m ) {
5295: NEXT(ma) = mb; ma = mb;
5296: } else {
5297: NEXT(ma) = 0;
5298: break;
5299: }
5300: }
5301: /* l = lcm(denoms) */
5302: NTOQ(ln,1,l);
5303: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
5304: divq(l,CA(m)->dn,&mul);
5305: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
5306: NEXTNM(mr0,mr);
5307: mulq((Q)mp->c,mul,&CQ(mr));
5308: dl = mp->dl;
5309: td = TD(DL(m));
5310: ndl_copy(DL(m),DL(mr));
5311: for ( i = ntrans; i < n; i++ ) {
5312: e = dl->d[i-ntrans];
5313: PUT_EXP(DL(mr),i,e);
5314: td += MUL_WEIGHT(e,i);
5315: }
5316: TD(DL(mr)) = td;
5317: if ( nd_blockmask) ndl_weight_mask(DL(mr));
5318: }
1.117 noro 5319: }
1.118 noro 5320: NEXT(mr) = 0;
5321: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
5322: MKND(NV(*p),mr0,len,r);
5323: /* XXX */
5324: SG(r) = SG(*p);
5325: nd_free(*p);
5326: *p = r;
5327: return 1;
1.59 noro 5328: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>