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