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