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