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