Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.26
1.26 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.25 2020/06/19 22:58:48 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.11 noro 5: int Nnd_add,Nf4_red;
1.12 noro 6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 7:
8: int diag_period = 6;
9: int weight_check = 1;
10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 11: /* for general module order */
1.19 noro 12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: int (*dl_base_compare_function)(int nv,DL a,DL b);
14: int nd_base_ordtype;
1.1 noro 15: int nd_dcomp;
16: int nd_rref2;
17: NM _nm_free_list;
18: ND _nd_free_list;
19: ND_pairs _ndp_free_list;
20: NODE nd_hcf;
1.26 ! noro 21: int Nsyz;
1.1 noro 22:
23: Obj nd_top_weight;
24:
25: static NODE nd_subst;
26: static VL nd_vc;
27: static int nd_ntrans;
28: static int nd_nalg;
29: #if 0
30: static int ndv_alloc;
31: #endif
32: #if 1
33: static int nd_f4_nsp=0x7fffffff;
34: #else
35: static int nd_f4_nsp=50;
36: #endif
37: static double nd_scale=2;
38: static UINT **nd_bound;
39: static struct order_spec *nd_ord;
40: static EPOS nd_epos;
41: static BlockMask nd_blockmask;
42: static int nd_nvar;
43: static int nd_isrlex;
44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
45: static UINT nd_mask[32];
46: static UINT nd_mask0,nd_mask1;
47:
48: static NDV *nd_ps;
49: static NDV *nd_ps_trace;
50: static NDV *nd_ps_sym;
51: static NDV *nd_ps_trace_sym;
52: static RHist *nd_psh;
53: static int nd_psn,nd_pslen;
54: static RHist *nd_red;
55: static int *nd_work_vector;
56: static int **nd_matrix;
57: static int nd_matrix_len;
58: static struct weight_or_block *nd_worb;
59: static int nd_worb_len;
60: static int nd_found,nd_create,nd_notfirst;
61: static int nmv_adv;
62: static int nd_demand;
1.21 noro 63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 64: static int nd_module_rank,nd_poly_weight_len;
65: static int *nd_poly_weight,*nd_module_weight;
66: static NODE nd_tracelist;
67: static NODE nd_alltracelist;
68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
69: static int *nd_gbblock;
70: static NODE nd_nzlist,nd_check_splist;
71: static int nd_splist;
72: static int *nd_sugarweight;
73: static int nd_f4red,nd_rank0,nd_last_nonzero;
74:
75: NumberField get_numberfield();
76: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
77: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
78: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
79: int nd_monic(int m,ND *p);
80: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
81: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
82: NDV pltondv(VL vl,VL dvl,LIST p);
83: void pltozpl(LIST l,Q *cont,LIST *pp);
84: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
85: void nmtodp(int mod,NM m,DP *r);
1.15 noro 86: void ndltodp(UINT *d,DP *r);
1.1 noro 87: NODE reverse_node(NODE n);
88: P ndc_div(int mod,union oNDC a,union oNDC b);
89: P ndctop(int mod,union oNDC c);
90: void finalize_tracelist(int i,P cont);
91: void conv_ilist(int demand,int trace,NODE g,int **indp);
92: void parse_nd_option(NODE opt);
93: void dltondl(int n,DL dl,UINT *r);
94: DP ndvtodp(int mod,NDV p);
95: DP ndtodp(int mod,ND p);
1.16 noro 96: DPM ndvtodpm(int mod,NDV p);
97: NDV dpmtondv(int mod,DPM p);
98: int dpm_getdeg(DPM p,int *rank);
99: void dpm_ptozp(DPM p,Z *cont,DPM *r);
100: int compdmm(int nv,DMM a,DMM b);
1.1 noro 101:
102: void Pdp_set_weight(NODE,VECT *);
103: void Pox_cmo_rpc(NODE,Obj *);
104:
105: ND nd_add_lf(ND p1,ND p2);
106: void nd_mul_c_lf(ND p,Z mul);
107: void ndv_mul_c_lf(NDV p,Z mul);
108: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
109: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
110: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
111: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
112: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
113: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
114: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
115: NODE nd_f4_lf_trace_main(int m,int **indp);
116: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
117:
118: extern int lf_lazy;
119: extern Z current_mod_lf;
120:
121: extern int Denominator,DP_Multiple,MaxDeg;
122:
123: #define BLEN (8*sizeof(unsigned long))
124:
125: typedef struct matrix {
126: int row,col;
127: unsigned long **a;
128: } *matrix;
129:
130:
131: void nd_free_private_storage()
132: {
133: _nm_free_list = 0;
134: _ndp_free_list = 0;
135: #if 0
136: GC_gcollect();
137: #endif
138: }
139:
140: void _NM_alloc()
141: {
142: NM p;
143: int i;
144:
145: for ( i = 0; i < 1024; i++ ) {
146: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
147: p->next = _nm_free_list; _nm_free_list = p;
148: }
149: }
150:
151: matrix alloc_matrix(int row,int col)
152: {
153: unsigned long **a;
154: int i,len,blen;
155: matrix mat;
156:
157: mat = (matrix)MALLOC(sizeof(struct matrix));
158: mat->row = row;
159: mat->col = col;
160: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
161: return mat;
162: }
163:
164:
165: void _ND_alloc()
166: {
167: ND p;
168: int i;
169:
170: for ( i = 0; i < 1024; i++ ) {
171: p = (ND)MALLOC(sizeof(struct oND));
172: p->body = (NM)_nd_free_list; _nd_free_list = p;
173: }
174: }
175:
176: void _NDP_alloc()
177: {
178: ND_pairs p;
179: int i;
180:
181: for ( i = 0; i < 1024; i++ ) {
182: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
183: +(nd_wpd-1)*sizeof(UINT));
184: p->next = _ndp_free_list; _ndp_free_list = p;
185: }
186: }
187:
188: INLINE int nd_length(ND p)
189: {
190: NM m;
191: int i;
192:
193: if ( !p )
194: return 0;
195: else {
196: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
197: return i;
198: }
199: }
200:
201: extern int dp_negative_weight;
202:
203: INLINE int ndl_reducible(UINT *d1,UINT *d2)
204: {
205: UINT u1,u2;
206: int i,j;
207:
208: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
209:
210: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
211: #if USE_UNROLL
212: switch ( nd_bpe ) {
213: case 3:
214: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
215: u1 = d1[i]; u2 = d2[i];
216: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
217: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
218: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
219: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
220: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
221: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
222: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
223: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
224: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
225: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
226: }
227: return 1;
228: break;
229: case 4:
230: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
231: u1 = d1[i]; u2 = d2[i];
232: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
233: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
234: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
235: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
236: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
237: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
238: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
239: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
240: }
241: return 1;
242: break;
243: case 6:
244: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
245: u1 = d1[i]; u2 = d2[i];
246: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
247: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
248: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
249: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
250: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
251: }
252: return 1;
253: break;
254: case 8:
255: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
256: u1 = d1[i]; u2 = d2[i];
257: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
258: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
259: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
260: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
261: }
262: return 1;
263: break;
264: case 16:
265: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
266: u1 = d1[i]; u2 = d2[i];
267: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
268: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
269: }
270: return 1;
271: break;
272: case 32:
273: for ( i = nd_exporigin; i < nd_wpd; i++ )
274: if ( d1[i] < d2[i] ) return 0;
275: return 1;
276: break;
277: default:
278: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
279: u1 = d1[i]; u2 = d2[i];
280: for ( j = 0; j < nd_epw; j++ )
281: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
282: }
283: return 1;
284: }
285: #else
286: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
287: u1 = d1[i]; u2 = d2[i];
288: for ( j = 0; j < nd_epw; j++ )
289: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
290: }
291: return 1;
292: #endif
293: }
294:
295: /*
296: * If the current order is a block order,
297: * then the last block is length 1 and contains
298: * the homo variable. Otherwise, the original
299: * order is either 0 or 2.
300: */
301:
302: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
303: {
304: int w,i,e,n,omask0;
305:
306: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
307: n = nd_nvar-1;
308: ndl_zero(r);
309: for ( i = 0; i < n; i++ ) {
310: e = GET_EXP_OLD(d,i);
311: PUT_EXP(r,i,e);
312: }
313: w = TD(d);
314: PUT_EXP(r,nd_nvar-1,weight-w);
315: if ( nd_module ) MPOS(r) = d[ompos];
316: TD(r) = weight;
317: if ( nd_blockmask ) ndl_weight_mask(r);
318: }
319:
320: void ndl_dehomogenize(UINT *d)
321: {
322: UINT mask;
323: UINT h;
324: int i,bits;
325:
326: if ( nd_blockmask ) {
327: h = GET_EXP(d,nd_nvar-1);
328: XOR_EXP(d,nd_nvar-1,h);
329: TD(d) -= h;
330: ndl_weight_mask(d);
331: } else {
332: if ( nd_isrlex ) {
333: if ( nd_bpe == 32 ) {
334: h = d[nd_exporigin];
335: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
336: d[i-1] = d[i];
337: d[i-1] = 0;
338: TD(d) -= h;
339: } else {
340: bits = nd_epw*nd_bpe;
341: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
342: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
343: for ( i = nd_exporigin; i < nd_wpd; i++ )
344: d[i] = ((d[i]<<nd_bpe)&mask)
345: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
346: TD(d) -= h;
347: }
348: } else {
349: h = GET_EXP(d,nd_nvar-1);
350: XOR_EXP(d,nd_nvar-1,h);
351: TD(d) -= h;
352: }
353: }
354: }
355:
356: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
357: {
358: UINT t1,t2,u,u1,u2;
359: int i,j,l;
360:
361: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
362: error("ndl_lcm : inconsistent monomials");
363: #if USE_UNROLL
364: switch ( nd_bpe ) {
365: case 3:
366: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
367: u1 = d1[i]; u2 = d2[i];
368: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
369: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
370: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
371: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
372: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
373: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
374: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
375: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
376: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
377: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
378: d[i] = u;
379: }
380: break;
381: case 4:
382: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
383: u1 = d1[i]; u2 = d2[i];
384: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
385: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
386: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
387: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
388: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
389: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
390: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
391: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
392: d[i] = u;
393: }
394: break;
395: case 6:
396: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
397: u1 = d1[i]; u2 = d2[i];
398: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
399: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
400: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
401: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
403: d[i] = u;
404: }
405: break;
406: case 8:
407: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
408: u1 = d1[i]; u2 = d2[i];
409: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
410: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
411: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
412: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
413: d[i] = u;
414: }
415: break;
416: case 16:
417: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
418: u1 = d1[i]; u2 = d2[i];
419: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
420: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
421: d[i] = u;
422: }
423: break;
424: case 32:
425: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
426: u1 = d1[i]; u2 = d2[i];
427: d[i] = u1>u2?u1:u2;
428: }
429: break;
430: default:
431: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
432: u1 = d1[i]; u2 = d2[i];
433: for ( j = 0, u = 0; j < nd_epw; j++ ) {
434: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
435: }
436: d[i] = u;
437: }
438: break;
439: }
440: #else
441: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
442: u1 = d1[i]; u2 = d2[i];
443: for ( j = 0, u = 0; j < nd_epw; j++ ) {
444: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
445: }
446: d[i] = u;
447: }
448: #endif
449: if ( nd_module ) MPOS(d) = MPOS(d1);
450: TD(d) = ndl_weight(d);
451: if ( nd_blockmask ) ndl_weight_mask(d);
452: }
453:
454: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
455: {
456: UINT t1,t2,u,u1,u2;
457: int i,j,l;
458:
459: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
460: u1 = d1[i]; u2 = d2[i];
461: for ( j = 0, u = 0; j < nd_epw; j++ ) {
462: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
463: }
464: d[i] = u;
465: }
466: }
467:
468: int ndl_weight(UINT *d)
469: {
470: UINT t,u;
471: int i,j;
472:
473: if ( current_dl_weight_vector )
474: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
475: u = GET_EXP(d,i);
476: t += MUL_WEIGHT(u,i);
477: }
478: else
479: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
480: u = d[i];
481: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
482: t += (u&nd_mask0);
483: }
1.20 noro 484: if ( nd_module && nd_module_rank && MPOS(d) )
485: t += nd_module_weight[MPOS(d)-1];
486: for ( i = nd_exporigin; i < nd_wpd; i++ )
487: if ( d[i] && !t )
488: printf("afo\n");
1.1 noro 489: return t;
490: }
491:
492: /* for sugarweight */
493:
494: int ndl_weight2(UINT *d)
495: {
496: int t,u;
497: int i,j;
498:
499: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
500: u = GET_EXP(d,i);
501: t += nd_sugarweight[i]*u;
502: }
1.20 noro 503: if ( nd_module && nd_module_rank && MPOS(d) )
504: t += nd_module_weight[MPOS(d)-1];
1.1 noro 505: return t;
506: }
507:
508: void ndl_weight_mask(UINT *d)
509: {
510: UINT t,u;
511: UINT *mask;
512: int i,j,k,l;
513:
514: l = nd_blockmask->n;
515: for ( k = 0; k < l; k++ ) {
516: mask = nd_blockmask->mask[k];
517: if ( current_dl_weight_vector )
518: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
519: u = GET_EXP_MASK(d,i,mask);
520: t += MUL_WEIGHT(u,i);
521: }
522: else
523: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
524: u = d[i]&mask[i];
525: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
526: t += (u&nd_mask0);
527: }
528: d[k+1] = t;
529: }
530: }
531:
1.21 noro 532: int ndl_glex_compare(UINT *d1,UINT *d2)
533: {
534: if ( TD(d1) > TD(d2) ) return 1;
535: else if ( TD(d1) < TD(d2) ) return -1;
536: else return ndl_lex_compare(d1,d2);
537: }
538:
1.1 noro 539: int ndl_lex_compare(UINT *d1,UINT *d2)
540: {
541: int i;
542:
543: d1 += nd_exporigin;
544: d2 += nd_exporigin;
545: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
546: if ( *d1 > *d2 )
547: return nd_isrlex ? -1 : 1;
548: else if ( *d1 < *d2 )
549: return nd_isrlex ? 1 : -1;
550: return 0;
551: }
552:
553: int ndl_block_compare(UINT *d1,UINT *d2)
554: {
555: int i,l,j,ord_o,ord_l;
556: struct order_pair *op;
557: UINT t1,t2,m;
558: UINT *mask;
559:
560: l = nd_blockmask->n;
561: op = nd_blockmask->order_pair;
562: for ( j = 0; j < l; j++ ) {
563: mask = nd_blockmask->mask[j];
564: ord_o = op[j].order;
565: if ( ord_o < 2 ) {
566: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
567: else if ( t1 < t2 ) return -1;
568: }
569: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
570: m = mask[i];
571: t1 = d1[i]&m;
572: t2 = d2[i]&m;
573: if ( t1 > t2 )
574: return !ord_o ? -1 : 1;
575: else if ( t1 < t2 )
576: return !ord_o ? 1 : -1;
577: }
578: }
579: return 0;
580: }
581:
582: int ndl_matrix_compare(UINT *d1,UINT *d2)
583: {
584: int i,j,s,row;
585: int *v;
586: Z **mat;
587: Z *w;
588: Z t1;
589: Z t,t2;
590:
1.6 noro 591: for ( j = 0; j < nd_nvar; j++ )
592: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 593: if ( nd_top_weight ) {
594: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 595: mat = (Z **)&BDY((VECT)nd_top_weight);
596: row = 1;
1.1 noro 597: } else {
598: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 599: row = ((MAT)nd_top_weight)->row;
1.1 noro 600: }
601: for ( i = 0; i < row; i++ ) {
1.6 noro 602: w = mat[i];
1.1 noro 603: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 604: STOZ(nd_work_vector[j],t1);
1.1 noro 605: mulz(w[j],t1,&t2);
606: addz(t,t2,&t1);
607: t = t1;
608: }
1.6 noro 609: if ( t ) {
610: s = sgnz(t);
1.1 noro 611: if ( s > 0 ) return 1;
612: else if ( s < 0 ) return -1;
1.6 noro 613: }
1.1 noro 614: }
1.6 noro 615: }
616: for ( i = 0; i < nd_matrix_len; i++ ) {
617: v = nd_matrix[i];
618: for ( j = 0, s = 0; j < nd_nvar; j++ )
619: s += v[j]*nd_work_vector[j];
620: if ( s > 0 ) return 1;
621: else if ( s < 0 ) return -1;
622: }
1.1 noro 623: if ( !ndl_equal(d1,d2) )
1.6 noro 624: error("ndl_matrix_compare : invalid matrix");
625: return 0;
1.1 noro 626: }
627:
628: int ndl_composite_compare(UINT *d1,UINT *d2)
629: {
630: int i,j,s,start,end,len,o;
631: int *v;
632: struct sparse_weight *sw;
633:
634: for ( j = 0; j < nd_nvar; j++ )
635: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
636: for ( i = 0; i < nd_worb_len; i++ ) {
637: len = nd_worb[i].length;
638: switch ( nd_worb[i].type ) {
639: case IS_DENSE_WEIGHT:
640: v = nd_worb[i].body.dense_weight;
641: for ( j = 0, s = 0; j < len; j++ )
642: s += v[j]*nd_work_vector[j];
643: if ( s > 0 ) return 1;
644: else if ( s < 0 ) return -1;
645: break;
646: case IS_SPARSE_WEIGHT:
647: sw = nd_worb[i].body.sparse_weight;
648: for ( j = 0, s = 0; j < len; j++ )
649: s += sw[j].value*nd_work_vector[sw[j].pos];
650: if ( s > 0 ) return 1;
651: else if ( s < 0 ) return -1;
652: break;
653: case IS_BLOCK:
654: o = nd_worb[i].body.block.order;
655: start = nd_worb[i].body.block.start;
656: switch ( o ) {
657: case 0:
658: end = start+len;
659: for ( j = start, s = 0; j < end; j++ )
660: s += MUL_WEIGHT(nd_work_vector[j],j);
661: if ( s > 0 ) return 1;
662: else if ( s < 0 ) return -1;
663: for ( j = end-1; j >= start; j-- )
664: if ( nd_work_vector[j] < 0 ) return 1;
665: else if ( nd_work_vector[j] > 0 ) return -1;
666: break;
667: case 1:
668: end = start+len;
669: for ( j = start, s = 0; j < end; j++ )
670: s += MUL_WEIGHT(nd_work_vector[j],j);
671: if ( s > 0 ) return 1;
672: else if ( s < 0 ) return -1;
673: for ( j = start; j < end; j++ )
674: if ( nd_work_vector[j] > 0 ) return 1;
675: else if ( nd_work_vector[j] < 0 ) return -1;
676: break;
677: case 2:
678: end = start+len;
679: for ( j = start; j < end; j++ )
680: if ( nd_work_vector[j] > 0 ) return 1;
681: else if ( nd_work_vector[j] < 0 ) return -1;
682: break;
683: }
684: break;
685: }
686: }
687: return 0;
688: }
689:
690: /* TDH -> WW -> TD-> RL */
691:
692: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
693: {
694: int i,m,e1,e2;
695:
696: if ( TD(d1) > TD(d2) ) return 1;
697: else if ( TD(d1) < TD(d2) ) return -1;
698: m = nd_nvar>>1;
699: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
700: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
701: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
702: }
703: if ( e1 > e2 ) return 1;
704: else if ( e1 < e2 ) return -1;
705: return ndl_lex_compare(d1,d2);
706: }
707:
1.21 noro 708: // common function for module glex and grlex comparison
709: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 710: {
1.21 noro 711: int c;
1.1 noro 712:
1.21 noro 713: switch ( nd_module_ordtype ) {
714: case 0:
715: if ( TD(d1) > TD(d2) ) return 1;
716: else if ( TD(d1) < TD(d2) ) return -1;
717: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
718: else if ( MPOS(d1) < MPOS(d2) ) return 1;
719: else if ( MPOS(d1) > MPOS(d2) ) return -1;
720: else return 0;
721: break;
1.1 noro 722:
1.21 noro 723: case 1:
1.19 noro 724: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
725: if ( TD(d1) > TD(d2) ) return 1;
726: else if ( TD(d1) < TD(d2) ) return -1;
727: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
728: if ( MPOS(d1) < MPOS(d2) ) return 1;
729: else if ( MPOS(d1) > MPOS(d2) ) return -1;
730: }
731: if ( MPOS(d1) < MPOS(d2) ) return 1;
732: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 733: else if ( TD(d1) > TD(d2) ) return 1;
734: else if ( TD(d1) < TD(d2) ) return -1;
735: else return ndl_lex_compare(d1,d2);
736: break;
1.1 noro 737:
1.21 noro 738: case 2: // weight -> POT
739: if ( TD(d1) > TD(d2) ) return 1;
740: else if ( TD(d1) < TD(d2) ) return -1;
741: else if ( MPOS(d1) < MPOS(d2) ) return 1;
742: else if ( MPOS(d1) > MPOS(d2) ) return -1;
743: else return ndl_lex_compare(d1,d2);
744: break;
1.1 noro 745:
1.21 noro 746: default:
747: error("ndl_module_glex_compare : invalid module_ordtype");
748: }
1.1 noro 749: }
750:
1.21 noro 751: // common for module comparison
752: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 753: {
1.21 noro 754: int c;
1.1 noro 755:
1.21 noro 756: switch ( nd_module_ordtype ) {
757: case 0:
1.23 noro 758: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 759: else if ( MPOS(d1) > MPOS(d2) ) return -1;
760: else if ( MPOS(d1) < MPOS(d2) ) return 1;
761: else return 0;
762: break;
1.1 noro 763:
1.21 noro 764: case 1:
765: if ( MPOS(d1) < MPOS(d2) ) return 1;
766: else if ( MPOS(d1) > MPOS(d2) ) return -1;
767: else return (*ndl_base_compare_function)(d1,d2);
768: break;
1.1 noro 769:
1.21 noro 770: case 2: // weight -> POT
771: if ( TD(d1) > TD(d2) ) return 1;
772: else if ( TD(d1) < TD(d2) ) return -1;
773: else if ( MPOS(d1) < MPOS(d2) ) return 1;
774: else if ( MPOS(d1) > MPOS(d2) ) return -1;
775: else return (*ndl_base_compare_function)(d1,d2);
776: break;
1.1 noro 777:
1.21 noro 778: default:
779: error("ndl_module_compare : invalid module_ordtype");
780: }
1.1 noro 781: }
782:
1.21 noro 783: extern DMMstack dmm_stack;
784: void _addtodl(int n,DL d1,DL d2);
785: int _eqdl(int n,DL d1,DL d2);
786:
787: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
788: {
789: int pos1,pos2,t,j;
790: DMM *in;
791: DMMstack s;
792: static DL d1=0;
793: static DL d2=0;
794: static int dlen=0;
795:
796: pos1 = MPOS(m1); pos2 = MPOS(m2);
797: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
798: if ( nd_nvar > dlen ) {
799: NEWDL(d1,nd_nvar);
800: NEWDL(d2,nd_nvar);
801: dlen = nd_nvar;
802: }
803: d1->td = TD(m1);
804: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
805: d2->td = TD(m2);
806: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
807: for ( s = dmm_stack; s; s = NEXT(s) ) {
808: in = s->in;
809: _addtodl(nd_nvar,in[pos1]->dl,d1);
810: _addtodl(nd_nvar,in[pos2]->dl,d2);
811: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
812: if ( pos1 < pos2 ) return 1;
813: else if ( pos1 > pos2 ) return -1;
814: else return 0;
815: }
816: pos1 = in[pos1]->pos;
817: pos2 = in[pos2]->pos;
818: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
819: }
820: // comparison by the bottom order
821: LAST:
822: switch ( nd_base_ordtype ) {
823: case 0:
824: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
825: if ( t ) return t;
826: else if ( pos1 < pos2 ) return 1;
827: else if ( pos1 > pos2 ) return -1;
828: else return 0;
829: break;
830: case 1:
831: if ( pos1 < pos2 ) return 1;
832: else if ( pos1 > pos2 ) return -1;
833: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
834: break;
835: case 2:
836: if ( d1->td > d2->td ) return 1;
837: else if ( d1->td < d2->td ) return -1;
838: else if ( pos1 < pos2 ) return 1;
839: else if ( pos1 > pos2 ) return -1;
840: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
841: break;
842: default:
843: error("ndl_schreyer_compare : invalid base ordtype");
844: }
1.1 noro 845: }
846:
847: INLINE int ndl_equal(UINT *d1,UINT *d2)
848: {
849: int i;
850:
851: switch ( nd_wpd ) {
852: case 2:
853: if ( TD(d2) != TD(d1) ) return 0;
854: if ( d2[1] != d1[1] ) return 0;
855: return 1;
856: break;
857: case 3:
858: if ( TD(d2) != TD(d1) ) return 0;
859: if ( d2[1] != d1[1] ) return 0;
860: if ( d2[2] != d1[2] ) return 0;
861: return 1;
862: break;
863: default:
864: for ( i = 0; i < nd_wpd; i++ )
865: if ( *d1++ != *d2++ ) return 0;
866: return 1;
867: break;
868: }
869: }
870:
871: INLINE void ndl_copy(UINT *d1,UINT *d2)
872: {
873: int i;
874:
875: switch ( nd_wpd ) {
876: case 2:
877: TD(d2) = TD(d1);
878: d2[1] = d1[1];
879: break;
880: case 3:
881: TD(d2) = TD(d1);
882: d2[1] = d1[1];
883: d2[2] = d1[2];
884: break;
885: default:
886: for ( i = 0; i < nd_wpd; i++ )
887: d2[i] = d1[i];
888: break;
889: }
890: }
891:
892: INLINE void ndl_zero(UINT *d)
893: {
894: int i;
895: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
896: }
897:
898: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
899: {
900: int i;
901:
902: if ( nd_module ) {
903: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
904: error("ndl_add : invalid operation");
905: }
906: #if 1
907: switch ( nd_wpd ) {
908: case 2:
909: TD(d) = TD(d1)+TD(d2);
910: d[1] = d1[1]+d2[1];
911: break;
912: case 3:
913: TD(d) = TD(d1)+TD(d2);
914: d[1] = d1[1]+d2[1];
915: d[2] = d1[2]+d2[2];
916: break;
917: default:
918: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
919: break;
920: }
921: #else
922: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
923: #endif
924: }
925:
926: /* d1 += d2 */
927: INLINE void ndl_addto(UINT *d1,UINT *d2)
928: {
929: int i;
930:
931: if ( nd_module ) {
932: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
933: error("ndl_addto : invalid operation");
934: }
935: #if 1
936: switch ( nd_wpd ) {
937: case 2:
938: TD(d1) += TD(d2);
939: d1[1] += d2[1];
940: break;
941: case 3:
942: TD(d1) += TD(d2);
943: d1[1] += d2[1];
944: d1[2] += d2[2];
945: break;
946: default:
947: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
948: break;
949: }
950: #else
951: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
952: #endif
953: }
954:
1.24 noro 955: /* d1 -= d2 */
956: INLINE void ndl_subfrom(UINT *d1,UINT *d2)
957: {
958: int i;
959:
960: if ( nd_module ) {
961: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
962: error("ndl_addto : invalid operation");
963: }
964: #if 1
965: switch ( nd_wpd ) {
966: case 2:
967: TD(d1) -= TD(d2);
968: d1[1] -= d2[1];
969: break;
970: case 3:
971: TD(d1) -= TD(d2);
972: d1[1] -= d2[1];
973: d1[2] -= d2[2];
974: break;
975: default:
976: for ( i = 0; i < nd_wpd; i++ ) d1[i] -= d2[i];
977: break;
978: }
979: #else
980: for ( i = 0; i < nd_wpd; i++ ) d1[i] -= d2[i];
981: #endif
982: }
983:
1.1 noro 984: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
985: {
986: int i;
987:
988: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
989: }
990:
991: int ndl_disjoint(UINT *d1,UINT *d2)
992: {
993: UINT t1,t2,u,u1,u2;
994: int i,j;
995:
996: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
997: #if USE_UNROLL
998: switch ( nd_bpe ) {
999: case 3:
1000: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1001: u1 = d1[i]; u2 = d2[i];
1002: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
1003: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
1004: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
1005: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
1007: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
1012: }
1013: return 1;
1014: break;
1015: case 4:
1016: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1017: u1 = d1[i]; u2 = d2[i];
1018: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1019: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1020: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1021: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1022: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1023: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1026: }
1027: return 1;
1028: break;
1029: case 6:
1030: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1031: u1 = d1[i]; u2 = d2[i];
1032: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1033: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1034: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1035: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1036: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1037: }
1038: return 1;
1039: break;
1040: case 8:
1041: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1042: u1 = d1[i]; u2 = d2[i];
1043: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1044: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1045: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1046: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1047: }
1048: return 1;
1049: break;
1050: case 16:
1051: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1052: u1 = d1[i]; u2 = d2[i];
1053: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1054: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1055: }
1056: return 1;
1057: break;
1058: case 32:
1059: for ( i = nd_exporigin; i < nd_wpd; i++ )
1060: if ( d1[i] && d2[i] ) return 0;
1061: return 1;
1062: break;
1063: default:
1064: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1065: u1 = d1[i]; u2 = d2[i];
1066: for ( j = 0; j < nd_epw; j++ ) {
1067: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1068: u1 >>= nd_bpe; u2 >>= nd_bpe;
1069: }
1070: }
1071: return 1;
1072: break;
1073: }
1074: #else
1075: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1076: u1 = d1[i]; u2 = d2[i];
1077: for ( j = 0; j < nd_epw; j++ ) {
1078: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1079: u1 >>= nd_bpe; u2 >>= nd_bpe;
1080: }
1081: }
1082: return 1;
1083: #endif
1084: }
1085:
1086: int ndl_check_bound(UINT *d1,UINT *d2)
1087: {
1088: UINT u2;
1089: int i,j,ind,k;
1090:
1091: ind = 0;
1092: #if USE_UNROLL
1093: switch ( nd_bpe ) {
1094: case 3:
1095: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1096: u2 = d2[i];
1097: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1098: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1099: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1100: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1101: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1102: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1103: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1104: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1105: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1106: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1107: }
1108: return 0;
1109: break;
1110: case 4:
1111: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1112: u2 = d2[i];
1113: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1114: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1115: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1116: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1117: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1118: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1119: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1120: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1121: }
1122: return 0;
1123: break;
1124: case 6:
1125: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1126: u2 = d2[i];
1127: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1128: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1129: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1130: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1131: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1132: }
1133: return 0;
1134: break;
1135: case 8:
1136: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1137: u2 = d2[i];
1138: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1139: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1140: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1141: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1142: }
1143: return 0;
1144: break;
1145: case 16:
1146: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1147: u2 = d2[i];
1148: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1149: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1150: }
1151: return 0;
1152: break;
1153: case 32:
1154: for ( i = nd_exporigin; i < nd_wpd; i++ )
1155: if ( d1[i]+d2[i]<d1[i] ) return 1;
1156: return 0;
1157: break;
1158: default:
1159: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1160: u2 = d2[i];
1161: k = (nd_epw-1)*nd_bpe;
1162: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1163: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1164: }
1165: return 0;
1166: break;
1167: }
1168: #else
1169: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1170: u2 = d2[i];
1171: k = (nd_epw-1)*nd_bpe;
1172: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1173: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1174: }
1175: return 0;
1176: #endif
1177: }
1178:
1179: int ndl_check_bound2(int index,UINT *d2)
1180: {
1181: return ndl_check_bound(nd_bound[index],d2);
1182: }
1183:
1184: INLINE int ndl_hash_value(UINT *d)
1185: {
1186: int i;
1.11 noro 1187: UINT r;
1.1 noro 1188:
1189: r = 0;
1190: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1191: r = (r*1511+d[i]);
1.11 noro 1192: r %= REDTAB_LEN;
1.1 noro 1193: return r;
1194: }
1195:
1196: INLINE int ndl_find_reducer(UINT *dg)
1197: {
1198: RHist r;
1199: int d,k,i;
1200:
1201: d = ndl_hash_value(dg);
1202: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1203: if ( ndl_equal(dg,DL(r)) ) {
1204: if ( k > 0 ) nd_notfirst++;
1205: nd_found++;
1206: return r->index;
1207: }
1208: }
1209: if ( Reverse )
1210: for ( i = nd_psn-1; i >= 0; i-- ) {
1211: r = nd_psh[i];
1212: if ( ndl_reducible(dg,DL(r)) ) {
1213: nd_create++;
1214: nd_append_red(dg,i);
1215: return i;
1216: }
1217: }
1218: else
1219: for ( i = 0; i < nd_psn; i++ ) {
1220: r = nd_psh[i];
1221: if ( ndl_reducible(dg,DL(r)) ) {
1222: nd_create++;
1223: nd_append_red(dg,i);
1224: return i;
1225: }
1226: }
1227: return -1;
1228: }
1229:
1.24 noro 1230: // ret=0,...,nd_psn-1 => reducer found
1231: // ret=nd_psn => reducer not found
1232: // ret=-1 => singular top reducible
1233:
1234: int comp_sig(SIG s1,SIG s2);
1235: void _ndltodl(UINT *ndl,DL dl);
1236:
1237: void print_sig(SIG s)
1238: {
1239: int i;
1240:
1241: fprintf(asir_out,"<<");
1242: for ( i = 0; i < nd_nvar; i++ ) {
1243: fprintf(asir_out,"%d",s->dl->d[i]);
1244: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1245: }
1246: fprintf(asir_out,">>*e%d",s->pos);
1247: }
1248:
1249: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1250: {
1251: RHist r;
1252: int i,singular,ret;
1.26 ! noro 1253: static int wpd,nvar;
1.24 noro 1254: static SIG quo;
1255: static UINT *tmp;
1256:
1.26 ! noro 1257: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
! 1258: if ( wpd != nd_wpd ) {
1.24 noro 1259: wpd = nd_wpd;
1260: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1261: }
1262: singular = 0;
1263: for ( i = 0; i < nd_psn; i++ ) {
1264: r = nd_psh[i];
1265: if ( ndl_reducible(dg,DL(r)) ) {
1266: ndl_copy(dg,tmp);
1267: ndl_subfrom(tmp,DL(r));
1268: _ndltodl(tmp,DL(quo));
1269: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1270: quo->pos = nd_psh[i]->sig->pos;
1271: ret = comp_sig(sig,quo);
1272: if ( ret > 0 ) { singular = 0; break; }
1273: if ( ret == 0 ) { singular = 1; }
1274: }
1275: }
1276: if ( singular ) return -1;
1277: else return i;
1278: }
1279:
1.1 noro 1280: ND nd_merge(ND p1,ND p2)
1281: {
1282: int n,c;
1283: int t,can,td1,td2;
1284: ND r;
1285: NM m1,m2,mr0,mr,s;
1286:
1287: if ( !p1 ) return p2;
1288: else if ( !p2 ) return p1;
1289: else {
1290: can = 0;
1291: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1292: c = DL_COMPARE(DL(m1),DL(m2));
1293: switch ( c ) {
1294: case 0:
1295: s = m1; m1 = NEXT(m1);
1296: can++; NEXTNM2(mr0,mr,s);
1297: s = m2; m2 = NEXT(m2); FREENM(s);
1298: break;
1299: case 1:
1300: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1301: break;
1302: case -1:
1303: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1304: break;
1305: }
1306: }
1307: if ( !mr0 )
1308: if ( m1 ) mr0 = m1;
1309: else if ( m2 ) mr0 = m2;
1310: else return 0;
1311: else if ( m1 ) NEXT(mr) = m1;
1312: else if ( m2 ) NEXT(mr) = m2;
1313: else NEXT(mr) = 0;
1314: BDY(p1) = mr0;
1315: SG(p1) = MAX(SG(p1),SG(p2));
1316: LEN(p1) = LEN(p1)+LEN(p2)-can;
1317: FREEND(p2);
1318: return p1;
1319: }
1320: }
1321:
1322: ND nd_add(int mod,ND p1,ND p2)
1323: {
1324: int n,c;
1325: int t,can,td1,td2;
1326: ND r;
1327: NM m1,m2,mr0,mr,s;
1328:
1.11 noro 1329: Nnd_add++;
1.1 noro 1330: if ( !p1 ) return p2;
1331: else if ( !p2 ) return p1;
1332: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1333: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1334: else if ( !mod ) return nd_add_q(p1,p2);
1335: else {
1336: can = 0;
1337: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1338: c = DL_COMPARE(DL(m1),DL(m2));
1339: switch ( c ) {
1340: case 0:
1341: t = ((CM(m1))+(CM(m2))) - mod;
1342: if ( t < 0 ) t += mod;
1343: s = m1; m1 = NEXT(m1);
1344: if ( t ) {
1345: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1346: } else {
1347: can += 2; FREENM(s);
1348: }
1349: s = m2; m2 = NEXT(m2); FREENM(s);
1350: break;
1351: case 1:
1352: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1353: break;
1354: case -1:
1355: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1356: break;
1357: }
1358: }
1359: if ( !mr0 )
1360: if ( m1 ) mr0 = m1;
1361: else if ( m2 ) mr0 = m2;
1362: else return 0;
1363: else if ( m1 ) NEXT(mr) = m1;
1364: else if ( m2 ) NEXT(mr) = m2;
1365: else NEXT(mr) = 0;
1366: BDY(p1) = mr0;
1367: SG(p1) = MAX(SG(p1),SG(p2));
1368: LEN(p1) = LEN(p1)+LEN(p2)-can;
1369: FREEND(p2);
1370: return p1;
1371: }
1372: }
1373:
1374: /* XXX on opteron, the inlined manipulation of destructive additon of
1375: * two NM seems to make gcc optimizer get confused, so the part is
1376: * done in a function.
1377: */
1378:
1379: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1380: {
1381: NM s;
1382: P t;
1383: int can;
1384:
1385: addp(nd_vc,CP(*m1),CP(*m2),&t);
1386: s = *m1; *m1 = NEXT(*m1);
1387: if ( t ) {
1388: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1389: } else {
1390: can = 2; FREENM(s);
1391: }
1392: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1393: return can;
1394: }
1395:
1396: ND nd_add_q(ND p1,ND p2)
1397: {
1398: int n,c,can;
1399: ND r;
1400: NM m1,m2,mr0,mr,s;
1401: P t;
1402:
1403: if ( !p1 ) return p2;
1404: else if ( !p2 ) return p1;
1405: else {
1406: can = 0;
1407: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1408: c = DL_COMPARE(DL(m1),DL(m2));
1409: switch ( c ) {
1410: case 0:
1411: #if defined(__x86_64__)
1412: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1413: #else
1414: addp(nd_vc,CP(m1),CP(m2),&t);
1415: s = m1; m1 = NEXT(m1);
1416: if ( t ) {
1417: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1418: } else {
1419: can += 2; FREENM(s);
1420: }
1421: s = m2; m2 = NEXT(m2); FREENM(s);
1422: #endif
1423: break;
1424: case 1:
1425: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1426: break;
1427: case -1:
1428: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1429: break;
1430: }
1431: }
1432: if ( !mr0 )
1433: if ( m1 ) mr0 = m1;
1434: else if ( m2 ) mr0 = m2;
1435: else return 0;
1436: else if ( m1 ) NEXT(mr) = m1;
1437: else if ( m2 ) NEXT(mr) = m2;
1438: else NEXT(mr) = 0;
1439: BDY(p1) = mr0;
1440: SG(p1) = MAX(SG(p1),SG(p2));
1441: LEN(p1) = LEN(p1)+LEN(p2)-can;
1442: FREEND(p2);
1443: return p1;
1444: }
1445: }
1446:
1447: ND nd_add_sf(ND p1,ND p2)
1448: {
1449: int n,c,can;
1450: ND r;
1451: NM m1,m2,mr0,mr,s;
1452: int t;
1453:
1454: if ( !p1 ) return p2;
1455: else if ( !p2 ) return p1;
1456: else {
1457: can = 0;
1458: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1459: c = DL_COMPARE(DL(m1),DL(m2));
1460: switch ( c ) {
1461: case 0:
1462: t = _addsf(CM(m1),CM(m2));
1463: s = m1; m1 = NEXT(m1);
1464: if ( t ) {
1465: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1466: } else {
1467: can += 2; FREENM(s);
1468: }
1469: s = m2; m2 = NEXT(m2); FREENM(s);
1470: break;
1471: case 1:
1472: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1473: break;
1474: case -1:
1475: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1476: break;
1477: }
1478: }
1479: if ( !mr0 )
1480: if ( m1 ) mr0 = m1;
1481: else if ( m2 ) mr0 = m2;
1482: else return 0;
1483: else if ( m1 ) NEXT(mr) = m1;
1484: else if ( m2 ) NEXT(mr) = m2;
1485: else NEXT(mr) = 0;
1486: BDY(p1) = mr0;
1487: SG(p1) = MAX(SG(p1),SG(p2));
1488: LEN(p1) = LEN(p1)+LEN(p2)-can;
1489: FREEND(p2);
1490: return p1;
1491: }
1492: }
1493:
1494:
1495: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1496: {
1497: int c,c1,c2;
1498: Z cg,cred,gcd,tq;
1499: P cgp,credp,gcdp;
1500: Obj tr,tr1;
1501:
1502: if ( mod == -1 ) {
1503: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1504: *divp = (Obj)ONE;
1505: } else if ( mod == -2 ) {
1506: Z inv,t;
1507: divlf(ONE,HCZ(p),&inv);
1508: chsgnlf(HCZ(g),&t);
1509: mullf(inv,t,&CZ(mul));
1510: *divp = (Obj)ONE;
1511: } else if ( mod ) {
1512: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1513: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1514: *divp = (Obj)ONE;
1515: } else if ( nd_vc ) {
1516: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1517: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1518: chsgnp(cgp,&CP(mul));
1519: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1520: if ( dn ) {
1521: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1522: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1523: }
1524: *divp = (Obj)credp;
1525: } else {
1.6 noro 1526: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1527: chsgnz(cg,&CZ(mul));
1.1 noro 1528: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1529: if ( dn ) {
1530: mulz(dn->z,cred,&tq); dn->z = tq;
1531: }
1532: *divp = (Obj)cred;
1533: }
1534: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1535: }
1536:
1537: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1538: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1539: {
1540: NM m,mrd,tail;
1541: NM mul;
1542: int n,sugar,psugar,sugar0,stat,index;
1543: int c,c1,c2,dummy;
1544: RHist h;
1545: NDV p,red;
1546: Q cg,cred,gcd,tq,qq;
1547: Z iq;
1548: DP dmul;
1549: NODE node;
1550: LIST hist;
1551: double hmag;
1552: P tp,tp1;
1553: Obj tr,tr1,div;
1554: union oNDC hg;
1555: P cont;
1556:
1557: if ( !g ) {
1558: *rp = d;
1559: return 1;
1560: }
1561: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1562:
1563: sugar0 = sugar = SG(g);
1564: n = NV(g);
1565: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1566: if ( d )
1567: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1568: for ( ; g; ) {
1569: index = ndl_find_reducer(HDL(g));
1570: if ( index >= 0 ) {
1571: h = nd_psh[index];
1572: ndl_sub(HDL(g),DL(h),DL(mul));
1573: if ( ndl_check_bound2(index,DL(mul)) ) {
1574: nd_free(g); nd_free(d);
1575: return 0;
1576: }
1577: p = nd_demand ? ndv_load(index) : ps[index];
1578: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1579: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1580: if ( nd_gentrace ) {
1581: /* Trace=[div,index,mul,ONE] */
1.6 noro 1582: STOZ(index,iq);
1.1 noro 1583: nmtodp(mod,mul,&dmul);
1584: node = mknode(4,div,iq,dmul,ONE);
1585: }
1586: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1587: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1588: hg = HCU(g);
1589: nd_removecont2(d,g);
1.6 noro 1590: if ( nd_gentrace ) {
1.1 noro 1591: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1592: /* exact division */
1.1 noro 1593: cont = ndc_div(mod,hg,HCU(g));
1594: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1595: }
1596: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1597: }
1598: MKLIST(hist,node);
1599: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1600: } else if ( !full ) {
1601: *rp = g;
1602: return 1;
1603: } else {
1604: m = BDY(g);
1605: if ( NEXT(m) ) {
1606: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1607: } else {
1608: FREEND(g); g = 0;
1609: }
1610: if ( d ) {
1611: NEXT(tail)=m; tail=m; LEN(d)++;
1612: } else {
1613: MKND(n,m,1,d); tail = BDY(d);
1614: }
1615: }
1616: }
1617: if ( d ) SG(d) = sugar;
1618: *rp = d;
1619: return 1;
1620: }
1621:
1.24 noro 1622: // ret=1 => success
1623: // ret=0 => overflow
1624: // ret=-1 => singular top reducible
1625:
1626: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1627: {
1628: NM m,mrd,tail;
1629: NM mul;
1630: int n,sugar,psugar,sugar0,stat,index;
1631: int c,c1,c2,dummy;
1632: RHist h;
1633: NDV p,red;
1634: Q cg,cred,gcd,tq,qq;
1635: Z iq;
1636: DP dmul;
1637: NODE node;
1638: LIST hist;
1639: double hmag;
1640: P tp,tp1;
1641: Obj tr,tr1,div;
1642: union oNDC hg;
1643: P cont;
1644: SIG sig;
1645:
1646: if ( !g ) {
1647: *rp = d;
1648: return 1;
1649: }
1650: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1651:
1652: sugar0 = sugar = SG(g);
1653: n = NV(g);
1654: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1655: if ( d )
1656: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1657: sig = g->sig;
1658: for ( ; g; ) {
1659: index = ndl_find_reducer_s(HDL(g),sig);
1660: if ( index >= 0 && index < nd_psn ) {
1661: // reducer found
1662: h = nd_psh[index];
1663: ndl_sub(HDL(g),DL(h),DL(mul));
1664: if ( ndl_check_bound2(index,DL(mul)) ) {
1665: nd_free(g); nd_free(d);
1666: return 0;
1667: }
1668: p = ps[index];
1669: /* d+g -> div*(d+g)+mul*p */
1670: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1671: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1672: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1673: hg = HCU(g);
1674: nd_removecont2(d,g);
1675: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1676: }
1677: } else if ( index == -1 ) {
1678: // singular top reducible
1679: return -1;
1680: } else if ( !full ) {
1681: *rp = g;
1682: g->sig = sig;
1683: return 1;
1684: } else {
1685: m = BDY(g);
1686: if ( NEXT(m) ) {
1687: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1688: } else {
1689: FREEND(g); g = 0;
1690: }
1691: if ( d ) {
1692: NEXT(tail)=m; tail=m; LEN(d)++;
1693: } else {
1694: MKND(n,m,1,d); tail = BDY(d);
1695: }
1696: }
1697: }
1698: if ( d ) {
1699: SG(d) = sugar;
1700: d->sig = sig;
1701: }
1702: *rp = d;
1703: return 1;
1704: }
1705:
1.1 noro 1706: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1707: {
1708: int hindex,index;
1709: NDV p;
1710: ND u,d,red;
1711: NODE l;
1712: NM mul,m,mrd,tail;
1713: int sugar,psugar,n,h_reducible;
1714: PGeoBucket bucket;
1715: int c,c1,c2;
1716: Z cg,cred,gcd,zzz;
1717: RHist h;
1718: double hmag,gmag;
1719: int count = 0;
1720: int hcount = 0;
1721:
1722: if ( !g ) {
1723: *rp = 0;
1724: return 1;
1725: }
1726: sugar = SG(g);
1727: n = NV(g);
1.6 noro 1728: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1729: bucket = create_pbucket();
1730: add_pbucket(mod,bucket,g);
1731: d = 0;
1732: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1733: while ( 1 ) {
1734: if ( mod > 0 || mod == -1 )
1735: hindex = head_pbucket(mod,bucket);
1736: else if ( mod == -2 )
1737: hindex = head_pbucket_lf(bucket);
1738: else
1739: hindex = head_pbucket_q(bucket);
1740: if ( hindex < 0 ) {
1741: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1742: if ( d ) SG(d) = sugar;
1743: *rp = d;
1744: return 1;
1745: }
1746: g = bucket->body[hindex];
1747: index = ndl_find_reducer(HDL(g));
1748: if ( index >= 0 ) {
1749: count++;
1750: if ( !d ) hcount++;
1751: h = nd_psh[index];
1752: ndl_sub(HDL(g),DL(h),DL(mul));
1753: if ( ndl_check_bound2(index,DL(mul)) ) {
1754: nd_free(d);
1755: free_pbucket(bucket);
1756: *rp = 0;
1757: return 0;
1758: }
1759: p = ps[index];
1760: if ( mod == -1 )
1761: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1762: else if ( mod == -2 ) {
1763: Z inv,t;
1764: divlf(ONE,HCZ(p),&inv);
1765: chsgnlf(HCZ(g),&t);
1766: mullf(inv,t,&CZ(mul));
1767: } else if ( mod ) {
1768: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1769: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1770: } else {
1.6 noro 1771: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1772: chsgnz(cg,&CZ(mul));
1.1 noro 1773: nd_mul_c_q(d,(P)cred);
1774: mulq_pbucket(bucket,cred);
1775: g = bucket->body[hindex];
1.6 noro 1776: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1777: }
1778: red = ndv_mul_nm(mod,mul,p);
1779: bucket->body[hindex] = nd_remove_head(g);
1780: red = nd_remove_head(red);
1781: add_pbucket(mod,bucket,red);
1782: psugar = SG(p)+TD(DL(mul));
1783: sugar = MAX(sugar,psugar);
1784: if ( !mod && hmag && (gmag > hmag) ) {
1785: g = normalize_pbucket(mod,bucket);
1786: if ( !g ) {
1787: if ( d ) SG(d) = sugar;
1788: *rp = d;
1789: return 1;
1790: }
1791: nd_removecont2(d,g);
1.6 noro 1792: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1793: add_pbucket(mod,bucket,g);
1794: }
1795: } else if ( !full ) {
1796: g = normalize_pbucket(mod,bucket);
1797: if ( g ) SG(g) = sugar;
1798: *rp = g;
1799: return 1;
1800: } else {
1801: m = BDY(g);
1802: if ( NEXT(m) ) {
1803: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1804: } else {
1805: FREEND(g); g = 0;
1806: }
1807: bucket->body[hindex] = g;
1808: NEXT(m) = 0;
1809: if ( d ) {
1810: NEXT(tail)=m; tail=m; LEN(d)++;
1811: } else {
1812: MKND(n,m,1,d); tail = BDY(d);
1813: }
1814: }
1815: }
1816: }
1817:
1.25 noro 1818: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1819: {
1820: int hindex,index;
1821: NDV p;
1822: ND u,d,red;
1823: NODE l;
1824: NM mul,m,mrd,tail;
1825: int sugar,psugar,n,h_reducible;
1826: PGeoBucket bucket;
1827: int c,c1,c2;
1828: Z cg,cred,gcd,zzz;
1829: RHist h;
1830: double hmag,gmag;
1831: int count = 0;
1832: int hcount = 0;
1833: SIG sig;
1834:
1835: if ( !g ) {
1836: *rp = 0;
1837: return 1;
1838: }
1839: sugar = SG(g);
1840: n = NV(g);
1841: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1842: bucket = create_pbucket();
1843: add_pbucket(mod,bucket,g);
1844: d = 0;
1845: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1846: sig = g->sig;
1847: while ( 1 ) {
1848: if ( mod > 0 || mod == -1 )
1849: hindex = head_pbucket(mod,bucket);
1850: else if ( mod == -2 )
1851: hindex = head_pbucket_lf(bucket);
1852: else
1853: hindex = head_pbucket_q(bucket);
1854: if ( hindex < 0 ) {
1855: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1856: if ( d ) {
1857: SG(d) = sugar;
1858: d->sig = sig;
1859: }
1860: *rp = d;
1861: return 1;
1862: }
1863: g = bucket->body[hindex];
1864: index = ndl_find_reducer_s(HDL(g),sig);
1865: if ( index >= 0 && index < nd_psn ) {
1866: count++;
1867: if ( !d ) hcount++;
1868: h = nd_psh[index];
1869: ndl_sub(HDL(g),DL(h),DL(mul));
1870: if ( ndl_check_bound2(index,DL(mul)) ) {
1871: nd_free(d);
1872: free_pbucket(bucket);
1873: *rp = 0;
1874: return 0;
1875: }
1876: p = ps[index];
1877: if ( mod == -1 )
1878: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1879: else if ( mod == -2 ) {
1880: Z inv,t;
1881: divlf(ONE,HCZ(p),&inv);
1882: chsgnlf(HCZ(g),&t);
1883: mullf(inv,t,&CZ(mul));
1884: } else if ( mod ) {
1885: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1886: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1887: } else {
1888: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1889: chsgnz(cg,&CZ(mul));
1890: nd_mul_c_q(d,(P)cred);
1891: mulq_pbucket(bucket,cred);
1892: g = bucket->body[hindex];
1893: gmag = (double)p_mag((P)HCZ(g));
1894: }
1895: red = ndv_mul_nm(mod,mul,p);
1896: bucket->body[hindex] = nd_remove_head(g);
1897: red = nd_remove_head(red);
1898: add_pbucket(mod,bucket,red);
1899: psugar = SG(p)+TD(DL(mul));
1900: sugar = MAX(sugar,psugar);
1901: if ( !mod && hmag && (gmag > hmag) ) {
1902: g = normalize_pbucket(mod,bucket);
1903: if ( !g ) {
1904: if ( d ) {
1905: SG(d) = sugar;
1906: d->sig = sig;
1907: }
1908: *rp = d;
1909: return 1;
1910: }
1911: nd_removecont2(d,g);
1912: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1913: add_pbucket(mod,bucket,g);
1914: }
1915: } else if ( index == -1 ) {
1916: // singular top reducible
1917: return -1;
1918: } else if ( !full ) {
1919: g = normalize_pbucket(mod,bucket);
1920: if ( g ) {
1921: SG(g) = sugar;
1922: g->sig = sig;
1923: }
1924: *rp = g;
1925: return 1;
1926: } else {
1927: m = BDY(g);
1928: if ( NEXT(m) ) {
1929: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1930: } else {
1931: FREEND(g); g = 0;
1932: }
1933: bucket->body[hindex] = g;
1934: NEXT(m) = 0;
1935: if ( d ) {
1936: NEXT(tail)=m; tail=m; LEN(d)++;
1937: } else {
1938: MKND(n,m,1,d); tail = BDY(d);
1939: }
1940: }
1941: }
1942: }
1943:
1.1 noro 1944: /* input : list of NDV, cand : list of NDV */
1945:
1946: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1947: {
1948: int n,i,stat;
1949: ND nf,d;
1950: NDV r;
1951: NODE t,s;
1952: union oNDC dn;
1953: Z q;
1954: LIST list;
1955:
1.24 noro 1956: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1957: n = length(cand);
1958:
1959: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1960: /* membercheck : list is a subset of Id(cand) ? */
1961: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1962: again:
1963: nd_tracelist = 0;
1964: if ( nd_bpe > obpe )
1965: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1966: else
1967: r = (NDV)BDY(t);
1968: #if 0
1969: // moved to nd_f4_lf_trace()
1970: if ( m == -2 ) ndv_mod(m,r);
1971: #endif
1972: d = ndvtond(m,r);
1.6 noro 1973: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1974: if ( !stat ) {
1975: nd_reconstruct(0,0);
1976: goto again;
1977: } else if ( nf ) return 0;
1978: if ( nd_gentrace ) {
1979: nd_tracelist = reverse_node(nd_tracelist);
1980: MKLIST(list,nd_tracelist);
1.6 noro 1981: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1982: MKNODE(s,list,nd_alltracelist);
1983: nd_alltracelist = s; nd_tracelist = 0;
1984: }
1985: if ( DP_Print ) { printf("."); fflush(stdout); }
1986: }
1987: if ( DP_Print ) { printf("\n"); }
1988: return 1;
1989: }
1990:
1991: ND nd_remove_head(ND p)
1992: {
1993: NM m;
1994:
1995: m = BDY(p);
1996: if ( !NEXT(m) ) {
1997: FREEND(p); p = 0;
1998: } else {
1999: BDY(p) = NEXT(m); LEN(p)--;
2000: }
2001: FREENM(m);
2002: return p;
2003: }
2004:
2005: ND nd_separate_head(ND p,ND *head)
2006: {
2007: NM m,m0;
2008: ND r;
2009:
2010: m = BDY(p);
2011: if ( !NEXT(m) ) {
2012: *head = p; p = 0;
2013: } else {
2014: m0 = m;
2015: BDY(p) = NEXT(m); LEN(p)--;
2016: NEXT(m0) = 0;
2017: MKND(NV(p),m0,1,r);
2018: *head = r;
2019: }
2020: return p;
2021: }
2022:
2023: PGeoBucket create_pbucket()
2024: {
2025: PGeoBucket g;
2026:
2027: g = CALLOC(1,sizeof(struct oPGeoBucket));
2028: g->m = -1;
2029: return g;
2030: }
2031:
2032: void free_pbucket(PGeoBucket b) {
2033: int i;
2034:
2035: for ( i = 0; i <= b->m; i++ )
2036: if ( b->body[i] ) {
2037: nd_free(b->body[i]);
2038: b->body[i] = 0;
2039: }
2040: GCFREE(b);
2041: }
2042:
2043: void add_pbucket_symbolic(PGeoBucket g,ND d)
2044: {
2045: int l,i,k,m;
2046:
2047: if ( !d )
2048: return;
2049: l = LEN(d);
2050: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2051: /* 2^(k-1) < l <= 2^k (=m) */
2052: d = nd_merge(g->body[k],d);
2053: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2054: g->body[k] = 0;
2055: d = nd_merge(g->body[k+1],d);
2056: }
2057: g->body[k] = d;
2058: g->m = MAX(g->m,k);
2059: }
2060:
2061: void add_pbucket(int mod,PGeoBucket g,ND d)
2062: {
2063: int l,i,k,m;
2064:
2065: if ( !d )
2066: return;
2067: l = LEN(d);
2068: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2069: /* 2^(k-1) < l <= 2^k (=m) */
2070: d = nd_add(mod,g->body[k],d);
2071: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2072: g->body[k] = 0;
2073: d = nd_add(mod,g->body[k+1],d);
2074: }
2075: g->body[k] = d;
2076: g->m = MAX(g->m,k);
2077: }
2078:
2079: void mulq_pbucket(PGeoBucket g,Z c)
2080: {
2081: int k;
2082:
2083: for ( k = 0; k <= g->m; k++ )
2084: nd_mul_c_q(g->body[k],(P)c);
2085: }
2086:
2087: NM remove_head_pbucket_symbolic(PGeoBucket g)
2088: {
2089: int j,i,k,c;
2090: NM head;
2091:
2092: k = g->m;
2093: j = -1;
2094: for ( i = 0; i <= k; i++ ) {
2095: if ( !g->body[i] ) continue;
2096: if ( j < 0 ) j = i;
2097: else {
2098: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2099: if ( c > 0 )
2100: j = i;
2101: else if ( c == 0 )
2102: g->body[i] = nd_remove_head(g->body[i]);
2103: }
2104: }
2105: if ( j < 0 ) return 0;
2106: else {
2107: head = BDY(g->body[j]);
2108: if ( !NEXT(head) ) {
2109: FREEND(g->body[j]);
2110: g->body[j] = 0;
2111: } else {
2112: BDY(g->body[j]) = NEXT(head);
2113: LEN(g->body[j])--;
2114: }
2115: return head;
2116: }
2117: }
2118:
2119: int head_pbucket(int mod,PGeoBucket g)
2120: {
2121: int j,i,c,k,nv,sum;
2122: UINT *di,*dj;
2123: ND gi,gj;
2124:
2125: k = g->m;
2126: while ( 1 ) {
2127: j = -1;
2128: for ( i = 0; i <= k; i++ ) {
2129: if ( !(gi = g->body[i]) )
2130: continue;
2131: if ( j < 0 ) {
2132: j = i;
2133: gj = g->body[j];
2134: dj = HDL(gj);
2135: sum = HCM(gj);
2136: } else {
2137: c = DL_COMPARE(HDL(gi),dj);
2138: if ( c > 0 ) {
2139: if ( sum ) HCM(gj) = sum;
2140: else g->body[j] = nd_remove_head(gj);
2141: j = i;
2142: gj = g->body[j];
2143: dj = HDL(gj);
2144: sum = HCM(gj);
2145: } else if ( c == 0 ) {
2146: if ( mod == -1 )
2147: sum = _addsf(sum,HCM(gi));
2148: else {
2149: sum = sum+HCM(gi)-mod;
2150: if ( sum < 0 ) sum += mod;
2151: }
2152: g->body[i] = nd_remove_head(gi);
2153: }
2154: }
2155: }
2156: if ( j < 0 ) return -1;
2157: else if ( sum ) {
2158: HCM(gj) = sum;
2159: return j;
2160: } else
2161: g->body[j] = nd_remove_head(gj);
2162: }
2163: }
2164:
2165: int head_pbucket_q(PGeoBucket g)
2166: {
2167: int j,i,c,k,nv;
2168: Z sum,t;
2169: ND gi,gj;
2170:
2171: k = g->m;
2172: while ( 1 ) {
2173: j = -1;
2174: for ( i = 0; i <= k; i++ ) {
2175: if ( !(gi = g->body[i]) ) continue;
2176: if ( j < 0 ) {
2177: j = i;
2178: gj = g->body[j];
1.6 noro 2179: sum = HCZ(gj);
1.1 noro 2180: } else {
2181: nv = NV(gi);
2182: c = DL_COMPARE(HDL(gi),HDL(gj));
2183: if ( c > 0 ) {
1.6 noro 2184: if ( sum ) HCZ(gj) = sum;
1.1 noro 2185: else g->body[j] = nd_remove_head(gj);
2186: j = i;
2187: gj = g->body[j];
1.6 noro 2188: sum = HCZ(gj);
1.1 noro 2189: } else if ( c == 0 ) {
1.6 noro 2190: addz(sum,HCZ(gi),&t);
1.1 noro 2191: sum = t;
2192: g->body[i] = nd_remove_head(gi);
2193: }
2194: }
2195: }
2196: if ( j < 0 ) return -1;
2197: else if ( sum ) {
1.6 noro 2198: HCZ(gj) = sum;
1.1 noro 2199: return j;
2200: } else
2201: g->body[j] = nd_remove_head(gj);
2202: }
2203: }
2204:
2205: int head_pbucket_lf(PGeoBucket g)
2206: {
2207: int j,i,c,k,nv;
2208: Z sum,t;
2209: ND gi,gj;
2210:
2211: k = g->m;
2212: while ( 1 ) {
2213: j = -1;
2214: for ( i = 0; i <= k; i++ ) {
2215: if ( !(gi = g->body[i]) ) continue;
2216: if ( j < 0 ) {
2217: j = i;
2218: gj = g->body[j];
2219: sum = HCZ(gj);
2220: } else {
2221: nv = NV(gi);
2222: c = DL_COMPARE(HDL(gi),HDL(gj));
2223: if ( c > 0 ) {
2224: if ( sum ) HCZ(gj) = sum;
2225: else g->body[j] = nd_remove_head(gj);
2226: j = i;
2227: gj = g->body[j];
2228: sum = HCZ(gj);
2229: } else if ( c == 0 ) {
2230: addlf(sum,HCZ(gi),&t);
2231: sum = t;
2232: g->body[i] = nd_remove_head(gi);
2233: }
2234: }
2235: }
2236: if ( j < 0 ) return -1;
2237: else if ( sum ) {
2238: HCZ(gj) = sum;
2239: return j;
2240: } else
2241: g->body[j] = nd_remove_head(gj);
2242: }
2243: }
2244:
2245: ND normalize_pbucket(int mod,PGeoBucket g)
2246: {
2247: int i;
2248: ND r,t;
2249:
2250: r = 0;
2251: for ( i = 0; i <= g->m; i++ ) {
2252: r = nd_add(mod,r,g->body[i]);
2253: g->body[i] = 0;
2254: }
2255: g->m = -1;
2256: return r;
2257: }
2258:
2259: #if 0
2260: void register_hcf(NDV p)
2261: {
2262: DCP dc,t;
2263: P hc,h;
2264: int c;
2265: NODE l,l1,prev;
2266:
2267: hc = p->body->c.p;
2268: if ( !nd_vc || NUM(hc) ) return;
2269: fctrp(nd_vc,hc,&dc);
2270: for ( t = dc; t; t = NEXT(t) ) {
2271: h = t->c;
2272: if ( NUM(h) ) continue;
2273: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2274: c = compp(nd_vc,h,(P)BDY(l));
2275: if ( c >= 0 ) break;
2276: }
2277: if ( !l || c > 0 ) {
2278: MKNODE(l1,h,l);
2279: if ( !prev )
2280: nd_hcf = l1;
2281: else
2282: NEXT(prev) = l1;
2283: }
2284: }
2285: }
2286: #else
2287: void register_hcf(NDV p)
2288: {
2289: DCP dc,t;
2290: P hc,h,q;
2291: Q dmy;
2292: int c;
2293: NODE l,l1,prev;
2294:
2295: hc = p->body->c.p;
2296: if ( NUM(hc) ) return;
2297: ptozp(hc,1,&dmy,&h);
2298: #if 1
2299: for ( l = nd_hcf; l; l = NEXT(l) ) {
2300: while ( 1 ) {
2301: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2302: else break;
2303: }
2304: }
2305: if ( NUM(h) ) return;
2306: #endif
2307: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2308: c = compp(nd_vc,h,(P)BDY(l));
2309: if ( c >= 0 ) break;
2310: }
2311: if ( !l || c > 0 ) {
2312: MKNODE(l1,h,l);
2313: if ( !prev )
2314: nd_hcf = l1;
2315: else
2316: NEXT(prev) = l1;
2317: }
2318: }
2319: #endif
2320:
2321: int do_diagonalize(int sugar,int m)
2322: {
1.6 noro 2323: int i,nh,stat;
2324: NODE r,g,t;
2325: ND h,nf,s,head;
2326: NDV nfv;
2327: Q q;
2328: P nm,nmp,dn,mnp,dnp,cont,cont1;
2329: union oNDC hc;
2330: NODE node;
2331: LIST l;
2332: Z iq;
1.1 noro 2333:
1.6 noro 2334: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2335: if ( nd_gentrace ) {
2336: /* Trace = [1,index,1,1] */
2337: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2338: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2339: }
2340: if ( nd_demand )
2341: nfv = ndv_load(i);
2342: else
2343: nfv = nd_ps[i];
2344: s = ndvtond(m,nfv);
2345: s = nd_separate_head(s,&head);
2346: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2347: if ( !stat ) return 0;
2348: ndv_free(nfv);
2349: hc = HCU(nf); nd_removecont(m,nf);
2350: /* exact division */
2351: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2352: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2353: nfv = ndtondv(m,nf);
2354: nd_free(nf);
2355: nd_bound[i] = ndv_compute_bound(nfv);
2356: if ( !m ) register_hcf(nfv);
2357: if ( nd_demand ) {
2358: ndv_save(nfv,i);
2359: ndv_free(nfv);
2360: } else
2361: nd_ps[i] = nfv;
2362: }
2363: return 1;
1.1 noro 2364: }
2365:
2366: LIST compute_splist()
2367: {
2368: NODE g,tn0,tn,node;
2369: LIST l0;
2370: ND_pairs d,t;
2371: int i;
2372: Z i1,i2;
2373:
2374: g = 0; d = 0;
2375: for ( i = 0; i < nd_psn; i++ ) {
2376: d = update_pairs(d,g,i,0);
2377: g = update_base(g,i);
2378: }
2379: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2380: NEXTNODE(tn0,tn);
1.6 noro 2381: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2382: node = mknode(2,i1,i2); MKLIST(l0,node);
2383: BDY(tn) = l0;
2384: }
2385: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2386: return l0;
2387: }
2388:
2389: /* return value = 0 => input is not a GB */
2390:
2391: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2392: {
1.6 noro 2393: int i,nh,sugar,stat;
2394: NODE r,g,t;
2395: ND_pairs d;
2396: ND_pairs l;
2397: ND h,nf,s,head,nf1;
2398: NDV nfv;
2399: Z q;
2400: union oNDC dn,hc;
2401: int diag_count = 0;
2402: P cont;
2403: LIST list;
2404:
1.11 noro 2405: Nnd_add = 0;
1.6 noro 2406: g = 0; d = 0;
2407: for ( i = 0; i < nd_psn; i++ ) {
2408: d = update_pairs(d,g,i,gensyz);
2409: g = update_base(g,i);
2410: }
2411: sugar = 0;
2412: while ( d ) {
1.1 noro 2413: again:
1.6 noro 2414: l = nd_minp(d,&d);
2415: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2416: if ( SG(l) != sugar ) {
2417: if ( ishomo ) {
2418: diag_count = 0;
2419: stat = do_diagonalize(sugar,m);
1.1 noro 2420: if ( !stat ) {
1.6 noro 2421: NEXT(l) = d; d = l;
2422: d = nd_reconstruct(0,d);
2423: goto again;
1.1 noro 2424: }
1.6 noro 2425: }
2426: sugar = SG(l);
2427: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2428: }
2429: stat = nd_sp(m,0,l,&h);
2430: if ( !stat ) {
2431: NEXT(l) = d; d = l;
2432: d = nd_reconstruct(0,d);
2433: goto again;
2434: }
1.1 noro 2435: #if USE_GEOBUCKET
1.6 noro 2436: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2437: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2438: #else
1.6 noro 2439: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2440: #endif
1.6 noro 2441: if ( !stat ) {
2442: NEXT(l) = d; d = l;
2443: d = nd_reconstruct(0,d);
2444: goto again;
2445: } else if ( nf ) {
2446: if ( checkonly || gensyz ) return 0;
1.1 noro 2447: if ( nd_newelim ) {
2448: if ( nd_module ) {
2449: if ( MPOS(HDL(nf)) > 1 ) return 0;
2450: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2451: }
1.6 noro 2452: if ( DP_Print ) { printf("+"); fflush(stdout); }
2453: hc = HCU(nf);
2454: nd_removecont(m,nf);
2455: if ( !m && nd_nalg ) {
2456: nd_monic(0,&nf);
2457: nd_removecont(m,nf);
2458: }
2459: if ( nd_gentrace ) {
2460: /* exact division */
1.1 noro 2461: cont = ndc_div(m,hc,HCU(nf));
2462: if ( m || !UNIQ(cont) ) {
1.6 noro 2463: t = mknode(4,NULLP,NULLP,NULLP,cont);
2464: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2465: nd_tracelist = t;
2466: }
2467: }
1.6 noro 2468: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2469: nh = ndv_newps(m,nfv,0);
1.6 noro 2470: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2471: diag_count = 0;
2472: stat = do_diagonalize(sugar,m);
2473: if ( !stat ) {
2474: NEXT(l) = d; d = l;
2475: d = nd_reconstruct(1,d);
2476: goto again;
1.1 noro 2477: }
1.6 noro 2478: }
2479: d = update_pairs(d,g,nh,0);
2480: g = update_base(g,nh);
2481: FREENDP(l);
2482: } else {
2483: if ( nd_gentrace && gensyz ) {
2484: nd_tracelist = reverse_node(nd_tracelist);
2485: MKLIST(list,nd_tracelist);
2486: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2487: MKNODE(t,list,nd_alltracelist);
2488: nd_alltracelist = t; nd_tracelist = 0;
2489: }
2490: if ( DP_Print ) { printf("."); fflush(stdout); }
2491: FREENDP(l);
2492: }
2493: }
2494: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2495: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.1 noro 2496: return g;
2497: }
2498:
1.24 noro 2499: ND_pairs update_pairs_s(ND_pairs d,NODE g,int t,NODE syz);
2500: ND_pairs nd_newpairs_s( NODE g, int t ,NODE syz);
2501:
2502: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2503: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2504:
2505: void _copydl(int n,DL d1,DL d2);
2506: void _subfromdl(int n,DL d1,DL d2);
2507: extern int (*cmpdl)(int n,DL d1,DL d2);
2508:
2509: NODE insert_sig(NODE l,SIG s)
2510: {
2511: int pos;
2512: DL sig;
2513: struct oNODE root;
2514: NODE p,prev,r;
2515: SIG t;
2516:
2517: pos = s->pos; sig = DL(s);
2518: root.next = l; prev = &root;
2519: for ( p = l; p; p = p->next ) {
2520: t = (SIG)p->body;
2521: if ( t->pos == pos ) {
2522: if ( _dl_redble(DL(t),sig,nd_nvar) )
2523: return root.next;
2524: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2525: // remove p
2526: prev->next = p->next;
2527: } else
2528: prev = p;
2529: }
2530: NEWNODE(r); r->body = (pointer)s;
2531: r->next = root.next;
2532: return r;
2533: }
2534:
2535: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2536: {
2537: struct oND_pairs root;
2538: ND_pairs prev,p;
2539: SIG spsig;
2540:
2541: root.next = d;
2542: prev = &root; p = d;
2543: while ( p ) {
2544: spsig = p->sig;
1.26 ! noro 2545: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2546: // remove p
2547: prev->next = p->next;
1.26 ! noro 2548: Nsyz++;
! 2549: } else
1.24 noro 2550: prev = p;
2551: p = p->next;
2552: }
2553: return (ND_pairs)root.next;
2554: }
2555:
1.26 ! noro 2556: struct oEGT eg_create,eg_newpairs,eg_merge;
! 2557:
1.24 noro 2558: NODE nd_sba_buch(int m,int ishomo,int **indp)
2559: {
2560: int i,nh,sugar,stat;
2561: NODE r,g,t;
2562: ND_pairs d;
2563: ND_pairs l;
2564: ND h,nf,s,head,nf1;
2565: NDV nfv;
2566: Z q;
2567: union oNDC dn,hc;
2568: P cont;
2569: LIST list;
2570: SIG sig;
2571: NODE syzlist;
1.26 ! noro 2572: int Nredundant;
! 2573: static int wpd,nvar;
! 2574: static DL lcm,quo,mul;
! 2575: struct oEGT eg1,eg2,eg_update;
1.24 noro 2576:
2577: syzlist = 0;
1.26 ! noro 2578: Nsyz = 0;
1.24 noro 2579: Nnd_add = 0;
1.26 ! noro 2580: Nredundant = 0;
1.24 noro 2581: g = 0; d = 0;
2582: for ( i = 0; i < nd_psn; i++ ) {
2583: d = update_pairs_s(d,g,i,0);
2584: g = append_one(g,i);
2585: }
2586: sugar = 0;
1.26 ! noro 2587: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
! 2588: init_eg(&eg_create);
! 2589: init_eg(&eg_merge);
1.24 noro 2590: while ( d ) {
2591: again:
1.26 ! noro 2592: if ( DP_Print ) {
! 2593: int len;
! 2594: ND_pairs td;
! 2595: for ( td = d, len=0; td; td = td->next, len++);
! 2596: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
! 2597: }
1.24 noro 2598: l = d; d = d->next;
2599: sig = l->sig;
2600: _ndltodl(l->lcm,lcm);
2601: for ( i = 0; i < nd_psn; i++ ) {
2602: if ( sig->pos == nd_psh[i]->sig->pos &&
2603: _dl_redble(DL(nd_psh[i]->sig),DL(sig),nd_nvar) ) {
1.26 ! noro 2604: _copydl(nd_nvar,DL(sig),quo);
! 2605: _subfromdl(nd_nvar,DL(nd_psh[i]->sig),quo);
! 2606: _ndltodl(DL(nd_psh[i]),mul);
! 2607: _addtodl(nd_nvar,quo,mul);
! 2608: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
1.24 noro 2609: break;
2610: }
2611: }
2612: if ( i < nd_psn ) {
2613: if ( DP_Print ) fprintf(asir_out,"M");
1.26 ! noro 2614: Nredundant++;
1.24 noro 2615: continue;
2616: }
2617: if ( SG(l) != sugar ) {
2618: sugar = SG(l);
2619: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2620: }
2621: stat = nd_sp(m,0,l,&h);
2622: if ( !stat ) {
2623: NEXT(l) = d; d = l;
2624: d = nd_reconstruct(0,d);
2625: goto again;
2626: }
1.25 noro 2627: #if USE_GEOBUCKET
1.24 noro 2628: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2629: #else
2630: stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2631: #endif
2632: if ( !stat ) {
2633: NEXT(l) = d; d = l;
2634: d = nd_reconstruct(0,d);
2635: goto again;
2636: } else if ( stat == -1 ) {
2637: if ( DP_Print ) { printf("S"); fflush(stdout); }
2638: } else if ( nf ) {
2639: if ( DP_Print ) { printf("+"); fflush(stdout); }
2640: hc = HCU(nf);
2641: nd_removecont(m,nf);
2642: nfv = ndtondv(m,nf); nd_free(nf);
2643: nh = ndv_newps(m,nfv,0);
1.26 ! noro 2644:
1.24 noro 2645: d = update_pairs_s(d,g,nh,syzlist);
2646: g = append_one(g,nh);
2647: FREENDP(l);
2648: } else {
2649: // syzygy
2650: d = remove_spair_s(d,sig);
2651: syzlist = insert_sig(syzlist,sig);
2652: if ( DP_Print ) { printf("."); fflush(stdout); }
2653: FREENDP(l);
2654: }
2655: }
2656: conv_ilist(nd_demand,0,g,indp);
1.26 ! noro 2657: if ( DP_Print ) {
! 2658: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
! 2659: fflush(stdout);
! 2660: print_eg("create",&eg_create);
! 2661: print_eg("merge",&eg_merge);
! 2662: }
1.24 noro 2663: return g;
2664: }
2665:
1.1 noro 2666: /* splist = [[i1,i2],...] */
2667:
2668: int check_splist(int m,NODE splist)
2669: {
2670: NODE t,p;
2671: ND_pairs d,r,l;
2672: int stat;
2673: ND h,nf;
2674:
2675: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2676: p = BDY((LIST)BDY(t));
1.6 noro 2677: NEXTND_pairs(d,r);
2678: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2679: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2680: SG(r) = TD(LCM(r)); /* XXX */
2681: }
2682: if ( d ) NEXT(r) = 0;
2683:
1.6 noro 2684: while ( d ) {
1.1 noro 2685: again:
1.6 noro 2686: l = nd_minp(d,&d);
2687: stat = nd_sp(m,0,l,&h);
2688: if ( !stat ) {
2689: NEXT(l) = d; d = l;
2690: d = nd_reconstruct(0,d);
2691: goto again;
2692: }
2693: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2694: if ( !stat ) {
2695: NEXT(l) = d; d = l;
2696: d = nd_reconstruct(0,d);
2697: goto again;
2698: } else if ( nf ) return 0;
1.1 noro 2699: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2700: }
1.1 noro 2701: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2702: return 1;
2703: }
2704:
2705: int check_splist_f4(int m,NODE splist)
2706: {
2707: UINT *s0vect;
1.6 noro 2708: PGeoBucket bucket;
1.1 noro 2709: NODE p,rp0,t;
2710: ND_pairs d,r,l,ll;
2711: int col,stat;
2712:
2713: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2714: p = BDY((LIST)BDY(t));
1.6 noro 2715: NEXTND_pairs(d,r);
2716: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2717: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2718: SG(r) = TD(LCM(r)); /* XXX */
2719: }
2720: if ( d ) NEXT(r) = 0;
2721:
1.6 noro 2722: while ( d ) {
2723: l = nd_minsugarp(d,&d);
2724: bucket = create_pbucket();
2725: stat = nd_sp_f4(m,0,l,bucket);
2726: if ( !stat ) {
2727: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2728: NEXT(ll) = d; d = l;
2729: d = nd_reconstruct(0,d);
2730: continue;
2731: }
2732: if ( bucket->m < 0 ) continue;
2733: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2734: if ( !col ) {
2735: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2736: NEXT(ll) = d; d = l;
2737: d = nd_reconstruct(0,d);
2738: continue;
1.1 noro 2739: }
1.6 noro 2740: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2741: }
2742: return 1;
1.1 noro 2743: }
2744:
2745: int do_diagonalize_trace(int sugar,int m)
2746: {
1.6 noro 2747: int i,nh,stat;
2748: NODE r,g,t;
2749: ND h,nf,nfq,s,head;
2750: NDV nfv,nfqv;
2751: Q q,den,num;
2752: union oNDC hc;
2753: NODE node;
2754: LIST l;
2755: Z iq;
2756: P cont,cont1;
1.1 noro 2757:
1.6 noro 2758: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2759: if ( nd_gentrace ) {
2760: /* Trace = [1,index,1,1] */
2761: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2762: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2763: }
2764: /* for nd_ps */
2765: s = ndvtond(m,nd_ps[i]);
2766: s = nd_separate_head(s,&head);
2767: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2768: if ( !stat ) return 0;
2769: nf = nd_add(m,head,nf);
2770: ndv_free(nd_ps[i]);
2771: nd_ps[i] = ndtondv(m,nf);
2772: nd_free(nf);
2773:
2774: /* for nd_ps_trace */
2775: if ( nd_demand )
2776: nfv = ndv_load(i);
2777: else
2778: nfv = nd_ps_trace[i];
2779: s = ndvtond(0,nfv);
2780: s = nd_separate_head(s,&head);
2781: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2782: if ( !stat ) return 0;
2783: ndv_free(nfv);
2784: hc = HCU(nf); nd_removecont(0,nf);
2785: /* exact division */
1.1 noro 2786: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2787: if ( nd_gentrace ) finalize_tracelist(i,cont);
2788: nfv = ndtondv(0,nf);
2789: nd_free(nf);
2790: nd_bound[i] = ndv_compute_bound(nfv);
2791: register_hcf(nfv);
2792: if ( nd_demand ) {
2793: ndv_save(nfv,i);
2794: ndv_free(nfv);
2795: } else
2796: nd_ps_trace[i] = nfv;
2797: }
2798: return 1;
1.1 noro 2799: }
2800:
2801: static struct oEGT eg_invdalg;
2802: struct oEGT eg_le;
2803:
2804: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2805: {
2806: NODE tn;
2807: P p1;
2808:
2809: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2810: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2811: }
2812: *r = p;
2813: }
2814:
2815: NODE nd_gb_trace(int m,int ishomo,int **indp)
2816: {
1.6 noro 2817: int i,nh,sugar,stat;
2818: NODE r,g,t;
2819: ND_pairs d;
2820: ND_pairs l;
2821: ND h,nf,nfq,s,head;
2822: NDV nfv,nfqv;
2823: Z q,den,num;
2824: P hc;
2825: union oNDC dn,hnfq;
2826: struct oEGT eg_monic,egm0,egm1;
2827: int diag_count = 0;
2828: P cont;
2829: LIST list;
2830:
2831: init_eg(&eg_monic);
2832: init_eg(&eg_invdalg);
2833: init_eg(&eg_le);
2834: g = 0; d = 0;
2835: for ( i = 0; i < nd_psn; i++ ) {
2836: d = update_pairs(d,g,i,0);
2837: g = update_base(g,i);
2838: }
2839: sugar = 0;
2840: while ( d ) {
1.1 noro 2841: again:
1.6 noro 2842: l = nd_minp(d,&d);
2843: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2844: if ( SG(l) != sugar ) {
1.1 noro 2845: #if 1
1.6 noro 2846: if ( ishomo ) {
2847: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2848: stat = do_diagonalize_trace(sugar,m);
2849: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2850: diag_count = 0;
1.1 noro 2851: if ( !stat ) {
1.6 noro 2852: NEXT(l) = d; d = l;
2853: d = nd_reconstruct(1,d);
2854: goto again;
1.1 noro 2855: }
1.6 noro 2856: }
2857: #endif
2858: sugar = SG(l);
2859: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2860: }
2861: stat = nd_sp(m,0,l,&h);
2862: if ( !stat ) {
2863: NEXT(l) = d; d = l;
2864: d = nd_reconstruct(1,d);
2865: goto again;
2866: }
1.1 noro 2867: #if USE_GEOBUCKET
1.6 noro 2868: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2869: #else
1.6 noro 2870: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2871: #endif
1.6 noro 2872: if ( !stat ) {
2873: NEXT(l) = d; d = l;
2874: d = nd_reconstruct(1,d);
2875: goto again;
2876: } else if ( nf ) {
2877: if ( nd_demand ) {
2878: nfqv = ndv_load(nd_psn);
2879: nfq = ndvtond(0,nfqv);
2880: } else
2881: nfq = 0;
2882: if ( !nfq ) {
2883: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2884: NEXT(l) = d; d = l;
2885: d = nd_reconstruct(1,d);
2886: goto again;
2887: }
2888: }
2889: if ( nfq ) {
2890: /* m|HC(nfq) => failure */
2891: if ( nd_vc ) {
2892: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2893: } else
2894: q = HCZ(nfq);
2895: if ( !remqi((Q)q,m) ) return 0;
2896:
2897: if ( DP_Print ) { printf("+"); fflush(stdout); }
2898: hnfq = HCU(nfq);
2899: if ( nd_nalg ) {
2900: /* m|DN(HC(nf)^(-1)) => failure */
2901: get_eg(&egm0);
2902: if ( !nd_monic(m,&nfq) ) return 0;
2903: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2904: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2905: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2906: } else {
2907: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2908: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2909: }
2910: if ( nd_gentrace ) {
2911: /* exact division */
2912: cont = ndc_div(0,hnfq,HCU(nfqv));
2913: if ( !UNIQ(cont) ) {
2914: t = mknode(4,NULLP,NULLP,NULLP,cont);
2915: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2916: nd_tracelist = t;
2917: }
2918: }
1.24 noro 2919: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 2920: if ( ishomo && ++diag_count == diag_period ) {
2921: diag_count = 0;
2922: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2923: stat = do_diagonalize_trace(sugar,m);
2924: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2925: if ( !stat ) {
1.1 noro 2926: NEXT(l) = d; d = l;
2927: d = nd_reconstruct(1,d);
2928: goto again;
1.6 noro 2929: }
1.1 noro 2930: }
1.6 noro 2931: d = update_pairs(d,g,nh,0);
2932: g = update_base(g,nh);
2933: } else {
2934: if ( DP_Print ) { printf("*"); fflush(stdout); }
2935: }
2936: } else {
2937: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2938: }
1.6 noro 2939: FREENDP(l);
2940: }
2941: if ( nd_nalg ) {
2942: if ( DP_Print ) {
2943: print_eg("monic",&eg_monic);
2944: print_eg("invdalg",&eg_invdalg);
2945: print_eg("le",&eg_le);
1.1 noro 2946: }
1.6 noro 2947: }
1.1 noro 2948: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2949: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2950: return g;
1.1 noro 2951: }
2952:
2953: int ndv_compare(NDV *p1,NDV *p2)
2954: {
2955: return DL_COMPARE(HDL(*p1),HDL(*p2));
2956: }
2957:
2958: int ndv_compare_rev(NDV *p1,NDV *p2)
2959: {
2960: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2961: }
2962:
2963: int ndvi_compare(NDVI p1,NDVI p2)
2964: {
2965: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2966: }
2967:
2968: int ndvi_compare_rev(NDVI p1,NDVI p2)
2969: {
2970: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2971: }
2972:
2973: NODE ndv_reduceall(int m,NODE f)
2974: {
2975: int i,j,n,stat;
2976: ND nf,g,head;
2977: NODE t,a0,a;
2978: union oNDC dn;
2979: Q q,num,den;
2980: NODE node;
2981: LIST l;
2982: Z iq,jq;
2983: int *perm;
2984: union oNDC hc;
2985: P cont,cont1;
2986:
2987: if ( nd_nora ) return f;
2988: n = length(f);
1.24 noro 2989: ndv_setup(m,0,f,0,1,0);
1.1 noro 2990: perm = (int *)MALLOC(n*sizeof(int));
2991: if ( nd_gentrace ) {
2992: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2993: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2994: }
2995: for ( i = 0; i < n; ) {
2996: if ( nd_gentrace ) {
2997: /* Trace = [1,index,1,1] */
1.6 noro 2998: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2999: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3000: }
3001: g = ndvtond(m,nd_ps[i]);
3002: g = nd_separate_head(g,&head);
1.6 noro 3003: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3004: if ( !stat )
3005: nd_reconstruct(0,0);
3006: else {
3007: if ( DP_Print ) { printf("."); fflush(stdout); }
3008: ndv_free(nd_ps[i]);
3009: hc = HCU(nf); nd_removecont(m,nf);
3010: if ( nd_gentrace ) {
3011: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3012: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3013: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3014: }
1.6 noro 3015: /* exact division */
1.1 noro 3016: cont = ndc_div(m,hc,HCU(nf));
3017: finalize_tracelist(perm[i],cont);
3018: }
3019: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3020: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3021: i++;
3022: }
3023: }
3024: if ( DP_Print ) { printf("\n"); }
3025: for ( a0 = 0, i = 0; i < n; i++ ) {
3026: NEXTNODE(a0,a);
3027: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3028: else {
3029: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3030: BDY(a) = (pointer)nd_ps[j];
3031: }
3032: }
3033: NEXT(a) = 0;
3034: return a0;
3035: }
3036:
3037: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3038: {
3039: ND_pairs d1,nd,cur,head,prev,remove;
3040:
3041: if ( !g ) return d;
3042: /* for testing */
3043: if ( gensyz && nd_gensyz == 2 ) {
3044: d1 = nd_newpairs(g,t);
3045: if ( !d )
3046: return d1;
3047: else {
3048: nd = d;
3049: while ( NEXT(nd) ) nd = NEXT(nd);
3050: NEXT(nd) = d1;
3051: return d;
3052: }
3053: }
3054: d = crit_B(d,t);
3055: d1 = nd_newpairs(g,t);
3056: d1 = crit_M(d1);
3057: d1 = crit_F(d1);
3058: if ( gensyz || do_weyl )
3059: head = d1;
3060: else {
3061: prev = 0; cur = head = d1;
3062: while ( cur ) {
3063: if ( crit_2( cur->i1,cur->i2 ) ) {
3064: remove = cur;
3065: if ( !prev ) head = cur = NEXT(cur);
3066: else cur = NEXT(prev) = NEXT(cur);
3067: FREENDP(remove);
3068: } else {
3069: prev = cur; cur = NEXT(cur);
3070: }
3071: }
3072: }
3073: if ( !d )
3074: return head;
3075: else {
3076: nd = d;
3077: while ( NEXT(nd) ) nd = NEXT(nd);
3078: NEXT(nd) = head;
3079: return d;
3080: }
3081: }
3082:
1.24 noro 3083: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3084:
3085: ND_pairs update_pairs_s( ND_pairs d, NODE /* of index */ g, int t,NODE syz)
3086: {
3087: ND_pairs d1;
1.26 ! noro 3088: struct oEGT eg1,eg2,eg3;
1.24 noro 3089:
3090: if ( !g ) return d;
1.26 ! noro 3091: get_eg(&eg1);
1.24 noro 3092: d1 = nd_newpairs_s(g,t,syz);
1.26 ! noro 3093: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3094: d = merge_pairs_s(d,d1);
1.26 ! noro 3095: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3096: return d;
3097: }
1.1 noro 3098:
3099: ND_pairs nd_newpairs( NODE g, int t )
3100: {
3101: NODE h;
3102: UINT *dl;
3103: int ts,s,i,t0,min,max;
3104: ND_pairs r,r0;
3105:
3106: dl = DL(nd_psh[t]);
3107: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3108: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3109: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3110: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3111: continue;
3112: if ( nd_gbblock ) {
3113: t0 = (long)BDY(h);
3114: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3115: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3116: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3117: break;
3118: }
3119: if ( nd_gbblock[i] >= 0 )
3120: continue;
3121: }
3122: NEXTND_pairs(r0,r);
3123: r->i1 = (long)BDY(h);
3124: r->i2 = t;
3125: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3126: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3127: SG(r) = MAX(s,ts) + TD(LCM(r));
3128: /* experimental */
3129: if ( nd_sugarweight )
3130: r->sugar2 = ndl_weight2(r->lcm);
3131: }
3132: if ( r0 ) NEXT(r) = 0;
3133: return r0;
3134: }
3135:
1.24 noro 3136:
3137: int comp_sig(SIG s1,SIG s2)
3138: {
3139: #if 0
3140: if ( s1->pos > s2->pos ) return 1;
3141: else if ( s1->pos < s2->pos ) return -1;
3142: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3143: #else
3144: static DL m1,m2;
3145: static int nvar;
3146: int ret;
3147:
1.26 ! noro 3148: if ( nvar != nd_nvar ) {
! 3149: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24 noro 3150: }
3151: _ndltodl(DL(nd_psh[s1->pos]),m1);
3152: _ndltodl(DL(nd_psh[s2->pos]),m2);
3153: _addtodl(nd_nvar,s1->dl,m1);
3154: _addtodl(nd_nvar,s2->dl,m2);
3155: ret = (*cmpdl)(nd_nvar,m1,m2);
3156: if ( ret != 0 ) return ret;
3157: else if ( s1->pos > s2->pos ) return 1;
3158: else if ( s1->pos < s2->pos ) return -1;
3159: else return 0;
3160: #endif
3161: }
3162:
3163: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3164: {
3165: int ret,s1,s2;
3166: RHist p1,p2;
3167: static int wpd;
3168: static UINT *lcm;
3169:
3170: sp->i1 = i1;
3171: sp->i2 = i2;
3172: p1 = nd_psh[i1];
3173: p2 = nd_psh[i2];
3174: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3175: s1 = SG(p1)-TD(DL(p1));
3176: s2 = SG(p2)-TD(DL(p2));
3177: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3178:
1.26 ! noro 3179: if ( wpd != nd_wpd ) {
1.24 noro 3180: wpd = nd_wpd;
3181: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3182: }
3183: // DL(sig1) <- sp->lcm
3184: // DL(sig1) -= DL(p1)
3185: // DL(sig1) += DL(p1->sig)
3186: ndl_copy(sp->lcm,lcm);
3187: ndl_subfrom(lcm,DL(p1));
3188: _ndltodl(lcm,DL(sig1));
3189: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3190: sig1->pos = p1->sig->pos;
3191:
3192: // DL(sig2) <- sp->lcm
3193: // DL(sig2) -= DL(p2)
3194: // DL(sig2) += DL(p2->sig)
3195: ndl_copy(sp->lcm,lcm);
3196: ndl_subfrom(lcm,DL(p2));
3197: _ndltodl(lcm,DL(sig2));
3198: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3199: sig2->pos = p2->sig->pos;
3200:
3201: ret = comp_sig(sig1,sig2);
3202: if ( ret == 0 ) return 0;
3203: else if ( ret > 0 ) sp->sig = sig1;
3204: else sp->sig = sig2;
3205: return 1;
3206: }
3207:
3208: SIG dup_sig(SIG sig)
3209: {
3210: SIG r;
3211:
3212: if ( !sig ) return 0;
3213: else {
3214: NEWSIG(r);
3215: _copydl(nd_nvar,DL(sig),DL(r));
3216: r->pos = sig->pos;
3217: return r;
3218: }
3219: }
3220:
3221: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3222: {
3223: to->i1 = from->i1;
3224: to->i2 = from->i2;
3225: to->sugar = from->sugar;
3226: to->sugar2 = from->sugar2;
3227: ndl_copy(from->lcm,to->lcm);
3228: to->sig = dup_sig(from->sig);
3229: }
3230:
3231: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3232: {
3233: struct oND_pairs root;
3234: ND_pairs q1,q2,r0,r;
3235: int ret;
3236:
3237: r = &root;
3238: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3239: ret = comp_sig(q1->sig,q2->sig);
3240: if ( ret < 0 ) {
3241: r->next = q1; r = q1; q1 = q1->next;
3242: } else if ( ret > 0 ) {
3243: r->next = q2; r = q2; q2 = q2->next;
3244: } else {
3245: ret = DL_COMPARE(q1->lcm,q2->lcm);
3246: if ( ret < 0 ) {
3247: r->next = q1; r = q1; q1 = q1->next;
3248: q2 = q2->next;
3249: } else {
3250: r->next = q2; r = q2; q2 = q2->next;
3251: q1 = q1->next;
3252: }
3253: }
3254: }
3255: if ( q1 ) {
3256: r->next = q1;
3257: } else {
3258: r->next = q2;
3259: }
3260: return root.next;
3261: }
3262:
3263: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3264: {
3265: ND_pairs p,prev;
3266: int ret;
3267:
3268: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3269: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3270: break;
3271: }
3272: if ( ret == 0 ) {
3273: ret = DL_COMPARE(s->lcm,p->lcm);
3274: if ( ret < 0 ) {
3275: // replace p with s
3276: s->next = p->next;
3277: if ( prev == 0 ) {
3278: return s;
3279: } else {
3280: prev->next = s;
3281: return l;
3282: }
3283: } else
3284: return l;
3285: } else {
3286: // insert s between prev and p
3287: s->next = p;
3288: if ( prev == 0 ) {
3289: return s;
3290: } else {
3291: prev->next = s;
3292: return l;
3293: }
3294: }
3295: }
3296:
3297: ND_pairs nd_newpairs_s( NODE g, int t, NODE syz)
3298: {
3299: NODE h,s;
3300: UINT *dl;
3301: int ts,ret;
3302: ND_pairs r,r0,_sp,sp;
3303: SIG _sig1,_sig2,spsig,tsig;
1.26 ! noro 3304: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3305:
3306: dl = DL(nd_psh[t]);
3307: ts = SG(nd_psh[t]) - TD(dl);
3308: NEWND_pairs(_sp);
3309: NEWSIG(_sig1); NEWSIG(_sig2);
3310: r0 = 0;
3311: for ( h = g; h; h = NEXT(h) ) {
3312: ret = _create_spair_s((long)BDY(h),t,_sp,_sig1,_sig2);
3313: if ( ret ) {
3314: spsig = _sp->sig;
3315: for ( s = syz; s; s = s->next ) {
3316: tsig = (SIG)s->body;
3317: if ( tsig->pos == spsig->pos && _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
3318: break;
3319: }
3320: if ( s == 0 ) {
3321: NEWND_pairs(sp);
3322: dup_ND_pairs(sp,_sp);
3323: r0 = insert_pair_s(r0,sp);
1.26 ! noro 3324: } else
! 3325: Nsyz++;
1.24 noro 3326: }
3327: }
3328: return r0;
3329: }
3330:
1.1 noro 3331: /* ipair = [i1,i2],[i1,i2],... */
3332: ND_pairs nd_ipairtospair(NODE ipair)
3333: {
3334: int s1,s2;
3335: NODE tn,t;
3336: ND_pairs r,r0;
3337:
3338: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3339: NEXTND_pairs(r0,r);
3340: tn = BDY((LIST)BDY(t));
1.6 noro 3341: r->i1 = ZTOS((Q)ARG0(tn));
3342: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3343: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3344: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3345: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3346: SG(r) = MAX(s1,s2) + TD(LCM(r));
3347: /* experimental */
3348: if ( nd_sugarweight )
3349: r->sugar2 = ndl_weight2(r->lcm);
3350: }
3351: if ( r0 ) NEXT(r) = 0;
3352: return r0;
3353: }
3354:
3355: /* kokokara */
3356:
3357: ND_pairs crit_B( ND_pairs d, int s )
3358: {
3359: ND_pairs cur,head,prev,remove;
3360: UINT *t,*tl,*lcm;
3361: int td,tdl;
3362:
3363: if ( !d ) return 0;
3364: t = DL(nd_psh[s]);
3365: prev = 0;
3366: head = cur = d;
3367: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3368: while ( cur ) {
3369: tl = cur->lcm;
3370: if ( ndl_reducible(tl,t) ) {
3371: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3372: if ( !ndl_equal(lcm,tl) ) {
3373: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3374: if (!ndl_equal(lcm,tl)) {
3375: remove = cur;
3376: if ( !prev ) {
3377: head = cur = NEXT(cur);
3378: } else {
3379: cur = NEXT(prev) = NEXT(cur);
3380: }
3381: FREENDP(remove);
3382: } else {
3383: prev = cur; cur = NEXT(cur);
3384: }
3385: } else {
3386: prev = cur; cur = NEXT(cur);
3387: }
3388: } else {
3389: prev = cur; cur = NEXT(cur);
3390: }
3391: }
3392: return head;
3393: }
3394:
3395: ND_pairs crit_M( ND_pairs d1 )
3396: {
3397: ND_pairs e,d2,d3,dd,p;
3398: UINT *id,*jd;
3399:
3400: if ( !d1 ) return d1;
3401: for ( dd = 0, e = d1; e; e = d3 ) {
3402: if ( !(d2 = NEXT(e)) ) {
3403: NEXT(e) = dd;
3404: return e;
3405: }
3406: id = LCM(e);
3407: for ( d3 = 0; d2; d2 = p ) {
3408: p = NEXT(d2);
3409: jd = LCM(d2);
3410: if ( ndl_equal(jd,id) )
3411: ;
3412: else if ( TD(jd) > TD(id) )
3413: if ( ndl_reducible(jd,id) ) continue;
3414: else ;
3415: else if ( ndl_reducible(id,jd) ) goto delit;
3416: NEXT(d2) = d3;
3417: d3 = d2;
3418: }
3419: NEXT(e) = dd;
3420: dd = e;
3421: continue;
3422: /**/
3423: delit: NEXT(d2) = d3;
3424: d3 = d2;
3425: for ( ; p; p = d2 ) {
3426: d2 = NEXT(p);
3427: NEXT(p) = d3;
3428: d3 = p;
3429: }
3430: FREENDP(e);
3431: }
3432: return dd;
3433: }
3434:
3435: ND_pairs crit_F( ND_pairs d1 )
3436: {
3437: ND_pairs rest, head,remove;
3438: ND_pairs last, p, r, w;
3439: int s;
3440:
3441: if ( !d1 ) return d1;
3442: for ( head = last = 0, p = d1; NEXT(p); ) {
3443: r = w = equivalent_pairs(p,&rest);
3444: s = SG(r);
3445: w = NEXT(w);
3446: while ( w ) {
3447: if ( crit_2(w->i1,w->i2) ) {
3448: r = w;
3449: w = NEXT(w);
3450: while ( w ) {
3451: remove = w;
3452: w = NEXT(w);
3453: FREENDP(remove);
3454: }
3455: break;
3456: } else if ( SG(w) < s ) {
3457: FREENDP(r);
3458: r = w;
3459: s = SG(r);
3460: w = NEXT(w);
3461: } else {
3462: remove = w;
3463: w = NEXT(w);
3464: FREENDP(remove);
3465: }
3466: }
3467: if ( last ) NEXT(last) = r;
3468: else head = r;
3469: NEXT(last = r) = 0;
3470: p = rest;
3471: if ( !p ) return head;
3472: }
3473: if ( !last ) return p;
3474: NEXT(last) = p;
3475: return head;
3476: }
3477:
3478: int crit_2( int dp1, int dp2 )
3479: {
3480: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3481: }
3482:
3483: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3484: {
3485: ND_pairs w,p,r,s;
3486: UINT *d;
3487:
3488: w = d1;
3489: d = LCM(w);
3490: s = NEXT(w);
3491: NEXT(w) = 0;
3492: for ( r = 0; s; s = p ) {
3493: p = NEXT(s);
3494: if ( ndl_equal(d,LCM(s)) ) {
3495: NEXT(s) = w; w = s;
3496: } else {
3497: NEXT(s) = r; r = s;
3498: }
3499: }
3500: *prest = r;
3501: return w;
3502: }
3503:
3504: NODE update_base(NODE nd,int ndp)
3505: {
3506: UINT *dl, *dln;
3507: NODE last, p, head;
3508:
3509: dl = DL(nd_psh[ndp]);
3510: for ( head = last = 0, p = nd; p; ) {
3511: dln = DL(nd_psh[(long)BDY(p)]);
3512: if ( ndl_reducible( dln, dl ) ) {
3513: p = NEXT(p);
3514: if ( last ) NEXT(last) = p;
3515: } else {
3516: if ( !last ) head = p;
3517: p = NEXT(last = p);
3518: }
3519: }
3520: head = append_one(head,ndp);
3521: return head;
3522: }
3523:
3524: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3525: {
3526: ND_pairs m,ml,p,l;
3527: UINT *lcm;
3528: int s,td,len,tlen,c,c1;
3529:
3530: if ( !(p = NEXT(m = d)) ) {
3531: *prest = p;
3532: NEXT(m) = 0;
3533: return m;
3534: }
3535: if ( !NoSugar ) {
3536: if ( nd_sugarweight ) {
3537: s = m->sugar2;
3538: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3539: if ( (p->sugar2 < s)
3540: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3541: ml = l; m = p; s = m->sugar2;
3542: }
3543: } else {
3544: s = SG(m);
3545: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3546: if ( (SG(p) < s)
3547: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3548: ml = l; m = p; s = SG(m);
3549: }
3550: }
3551: } else {
3552: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3553: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3554: ml = l; m = p; s = SG(m);
3555: }
3556: }
3557: if ( !ml ) *prest = NEXT(m);
3558: else {
3559: NEXT(ml) = NEXT(m);
3560: *prest = d;
3561: }
3562: NEXT(m) = 0;
3563: return m;
3564: }
3565:
3566: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3567: {
3568: int msugar,i;
3569: ND_pairs t,dm0,dm,dr0,dr;
3570:
3571: if ( nd_sugarweight ) {
3572: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3573: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3574: dm0 = 0; dr0 = 0;
3575: for ( i = 0, t = d; t; t = NEXT(t) )
3576: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3577: if ( dm0 ) NEXT(dm) = t;
3578: else dm0 = t;
3579: dm = t;
3580: i++;
3581: } else {
3582: if ( dr0 ) NEXT(dr) = t;
3583: else dr0 = t;
3584: dr = t;
3585: }
3586: } else {
3587: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3588: if ( SG(t) < msugar ) msugar = SG(t);
3589: dm0 = 0; dr0 = 0;
3590: for ( i = 0, t = d; t; t = NEXT(t) )
3591: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3592: if ( dm0 ) NEXT(dm) = t;
3593: else dm0 = t;
3594: dm = t;
3595: i++;
3596: } else {
3597: if ( dr0 ) NEXT(dr) = t;
3598: else dr0 = t;
3599: dr = t;
3600: }
3601: }
3602: NEXT(dm) = 0;
3603: if ( dr0 ) NEXT(dr) = 0;
3604: *prest = dr0;
3605: return dm0;
3606: }
3607:
3608: int nd_tdeg(NDV c)
3609: {
3610: int wmax = 0;
3611: int i,len;
3612: NMV a;
3613:
3614: len = LEN(c);
3615: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3616: wmax = MAX(TD(DL(a)),wmax);
3617: return wmax;
3618: }
3619:
1.24 noro 3620: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3621: {
3622: int len;
3623: RHist r;
3624: NDV b;
3625: NODE tn;
3626: LIST l;
3627: Z iq;
3628:
3629: if ( nd_psn == nd_pslen ) {
3630: nd_pslen *= 2;
3631: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3632: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3633: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3634: nd_bound = (UINT **)
3635: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3636: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3637: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3638: }
3639: NEWRHist(r); nd_psh[nd_psn] = r;
3640: nd_ps[nd_psn] = a;
3641: if ( aq ) {
3642: nd_ps_trace[nd_psn] = aq;
3643: if ( !m ) {
3644: register_hcf(aq);
3645: } else if ( m == -2 ) {
3646: /* do nothing */
3647: } else
3648: error("ndv_newps : invalud modulus");
3649: nd_bound[nd_psn] = ndv_compute_bound(aq);
3650: #if 1
3651: SG(r) = SG(aq);
3652: #else
3653: SG(r) = nd_tdeg(aq);
3654: #endif
3655: ndl_copy(HDL(aq),DL(r));
1.24 noro 3656: r->sig = dup_sig(aq->sig);
1.1 noro 3657: } else {
3658: if ( !m ) register_hcf(a);
3659: nd_bound[nd_psn] = ndv_compute_bound(a);
3660: #if 1
3661: SG(r) = SG(a);
3662: #else
3663: SG(r) = nd_tdeg(a);
3664: #endif
3665: ndl_copy(HDL(a),DL(r));
1.24 noro 3666: r->sig = dup_sig(a->sig);
1.1 noro 3667: }
3668: if ( nd_demand ) {
3669: if ( aq ) {
3670: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3671: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3672: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3673: nd_ps_trace[nd_psn] = 0;
3674: } else {
3675: ndv_save(nd_ps[nd_psn],nd_psn);
3676: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3677: nd_ps[nd_psn] = 0;
3678: }
3679: }
3680: if ( nd_gentrace ) {
3681: /* reverse the tracelist and append it to alltracelist */
3682: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3683: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3684: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3685: }
3686: return nd_psn++;
3687: }
3688:
3689: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3690: /* return 1 if success, 0 if failure (HC(a mod p)) */
3691:
1.24 noro 3692: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3693: {
1.6 noro 3694: int i,j,td,len,max;
3695: NODE s,s0,f0,tn;
3696: UINT *d;
3697: RHist r;
3698: NDVI w;
3699: NDV a,am;
3700: union oNDC hc;
3701: NODE node;
3702: P hcp;
3703: Z iq,jq;
3704: LIST l;
3705:
3706: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3707: /* initialize the tracelist */
3708: nd_tracelist = 0;
3709:
3710: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3711: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3712: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3713: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3714: if ( !dont_sort ) {
3715: /* XXX heuristic */
1.26 ! noro 3716: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 3717: qsort(w,nd_psn,sizeof(struct oNDVI),
3718: (int (*)(const void *,const void *))ndvi_compare_rev);
3719: else
3720: qsort(w,nd_psn,sizeof(struct oNDVI),
3721: (int (*)(const void *,const void *))ndvi_compare);
3722: }
3723: nd_pslen = 2*nd_psn;
3724: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3725: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3726: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3727: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3728: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3729: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3730: nd_hcf = 0;
1.1 noro 3731:
1.6 noro 3732: if ( trace && nd_vc )
3733: makesubst(nd_vc,&nd_subst);
3734: else
3735: nd_subst = 0;
1.1 noro 3736:
1.6 noro 3737: if ( !nd_red )
3738: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3739: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3740: for ( i = 0; i < nd_psn; i++ ) {
3741: hc = HCU(w[i].p);
3742: if ( trace ) {
3743: if ( mod == -2 ) {
3744: /* over a large finite field */
3745: /* trace = small modulus */
3746: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3747: ndv_mod(-2,a);
3748: if ( !dont_removecont) ndv_removecont(-2,a);
3749: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3750: ndv_mod(trace,am);
3751: if ( DL_COMPARE(HDL(am),HDL(a)) )
3752: return 0;
3753: ndv_removecont(trace,am);
3754: } else {
3755: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3756: if ( !dont_removecont) ndv_removecont(0,a);
3757: register_hcf(a);
3758: am = nd_ps[i] = ndv_dup(mod,a);
3759: ndv_mod(mod,am);
3760: if ( DL_COMPARE(HDL(am),HDL(a)) )
3761: return 0;
3762: ndv_removecont(mod,am);
3763: }
3764: } else {
3765: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3766: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3767: if ( !mod ) register_hcf(a);
1.1 noro 3768: }
1.6 noro 3769: if ( nd_gentrace ) {
3770: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3771: /* exact division */
1.1 noro 3772: if ( !dont_removecont )
1.6 noro 3773: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3774: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3775: }
3776: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3777: nd_bound[i] = ndv_compute_bound(a);
3778: nd_psh[i] = r;
3779: if ( nd_demand ) {
3780: if ( trace ) {
3781: ndv_save(nd_ps_trace[i],i);
3782: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3783: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3784: nd_ps_trace[i] = 0;
3785: } else {
3786: ndv_save(nd_ps[i],i);
3787: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3788: nd_ps[i] = 0;
3789: }
1.1 noro 3790: }
1.6 noro 3791: }
1.24 noro 3792: if ( sba ) {
3793: // setup signatures
3794: for ( i = 0; i < nd_psn; i++ ) {
3795: SIG sig;
3796:
3797: NEWSIG(sig); sig->pos = i;
3798: nd_ps[i]->sig = sig;
3799: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
3800: nd_psh[i]->sig = sig;
3801: if ( trace ) {
3802: nd_ps_trace[i]->sig = sig;
3803: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
3804: }
3805: }
3806: }
1.6 noro 3807: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3808: return 1;
1.1 noro 3809: }
3810:
3811: struct order_spec *append_block(struct order_spec *spec,
3812: int nv,int nalg,int ord);
3813:
3814: extern VECT current_dl_weight_vector_obj;
3815: static VECT prev_weight_vector_obj;
3816:
3817: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3818: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3819: {
3820: NODE alist,t,s,r0,r,arg;
3821: VL tv;
3822: P poly;
3823: DP d;
3824: Alg alpha,dp;
3825: DAlg inv,da,hc;
3826: MP m;
3827: int i,nvar,nalg,n;
3828: NumberField nf;
3829: LIST f1,f2;
3830: struct order_spec *current_spec;
3831: VECT obj,obj0;
3832: VECT tmp;
3833:
3834: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3835: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3836:
3837: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3838: NEXTNODE(alist,t); MKV(tv->v,poly);
3839: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3840: tv->v = tv->v->priv;
3841: }
3842: NEXT(t) = 0;
3843:
3844: /* simplification, making polynomials monic */
3845: setfield_dalg(alist);
3846: obj_algtodalg((Obj)f,(Obj *)&f1);
3847: for ( t = BDY(f); t; t = NEXT(t) ) {
3848: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3849: hc = (DAlg)BDY(d)->c;
3850: if ( NID(hc) == N_DA ) {
3851: invdalg(hc,&inv);
3852: for ( m = BDY(d); m; m = NEXT(m) ) {
3853: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3854: }
3855: }
3856: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3857: }
3858: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3859:
3860: /* append alg vars to the var list */
3861: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3862: NEXT(tv) = av;
3863:
3864: /* append a block to ord */
3865: *ord1p = append_block(ord,nvar,nalg,2);
3866:
3867: /* create generator list */
3868: nf = get_numberfield();
3869: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3870: MKAlg(nf->defpoly[i],dp);
3871: MKNODE(s,dp,t); t = s;
3872: }
3873: MKLIST(f1,t);
3874: *alistp = alist;
3875: algobjtorat((Obj)f1,(Obj *)f1p);
3876:
3877: /* creating a new weight vector */
3878: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3879: n = nvar+nalg+1;
3880: MKVECT(obj,n);
3881: if ( obj0 && obj0->len == nvar )
3882: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3883: else
3884: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3885: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3886: BDY(obj)[n-1] = (pointer)ONE;
3887: arg = mknode(1,obj);
3888: Pdp_set_weight(arg,&tmp);
3889: }
3890:
3891: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3892: {
3893: NODE s,t,u0,u;
3894: P p;
3895: VL tv;
3896: Obj obj;
3897: VECT tmp;
3898: NODE arg;
3899:
3900: u0 = 0;
3901: for ( t = r; t; t = NEXT(t) ) {
3902: p = (P)BDY(t);
3903: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3904: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3905: }
3906: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3907: NEXTNODE(u0,u);
3908: BDY(u) = (pointer)p;
3909: }
3910: }
3911: arg = mknode(1,prev_weight_vector_obj);
3912: Pdp_set_weight(arg,&tmp);
3913:
3914: return u0;
3915: }
3916:
3917: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3918: {
3919: VL tv,fv,vv,vc,av;
3920: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3921: int e,max,nvar,i;
3922: NDV b;
3923: int ishomo,nalg,mrank,trank,wmax,len;
3924: NMV a;
3925: Alg alpha,dp;
3926: P p,zp;
3927: Q dmy;
3928: LIST f1,f2,zpl;
3929: Obj obj;
3930: NumberField nf;
3931: struct order_spec *ord1;
3932: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3933: LIST l1,l2,l3,l4,l5;
3934: int j;
3935: Z jq,bpe,last_nonzero;
3936: int *perm;
3937: EPOS oepos;
3938: int obpe,oadv,ompos,cbpe;
1.15 noro 3939: VECT hvect;
1.1 noro 3940:
3941: nd_module = 0;
3942: if ( !m && Demand ) nd_demand = 1;
3943: else nd_demand = 0;
3944: parse_nd_option(current_option);
3945:
3946: if ( DP_Multiple )
3947: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3948: #if 0
3949: ndv_alloc = 0;
3950: #endif
3951: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3952: if ( m && nd_vc )
3953: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3954: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3955: switch ( ord->id ) {
3956: case 1:
3957: if ( ord->nv != nvar )
3958: error("nd_{gr,f4} : invalid order specification");
3959: break;
3960: default:
3961: break;
3962: }
3963: nd_nalg = 0;
3964: av = 0;
3965: if ( !m ) {
3966: get_algtree((Obj)f,&av);
3967: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3968: nd_ntrans = nvar;
3969: nd_nalg = nalg;
3970: /* #i -> t#i */
3971: if ( nalg ) {
3972: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3973: ord = ord1;
3974: f = f1;
3975: }
3976: nvar += nalg;
3977: }
3978: nd_init_ord(ord);
3979: mrank = 0;
3980: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3981: for ( tv = vv; tv; tv = NEXT(tv) ) {
3982: if ( nd_module ) {
1.16 noro 3983: if ( OID(BDY(t)) == O_DPM ) {
3984: e = dpm_getdeg((DPM)BDY(t),&trank);
3985: max = MAX(e,max);
3986: mrank = MAX(mrank,trank);
3987: } else {
3988: s = BDY((LIST)BDY(t));
3989: trank = length(s);
3990: mrank = MAX(mrank,trank);
3991: for ( ; s; s = NEXT(s) ) {
3992: e = getdeg(tv->v,(P)BDY(s));
3993: max = MAX(e,max);
3994: }
1.1 noro 3995: }
3996: } else {
3997: e = getdeg(tv->v,(P)BDY(t));
3998: max = MAX(e,max);
3999: }
4000: }
4001: nd_setup_parameters(nvar,nd_nzlist?0:max);
4002: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4003: ishomo = 1;
4004: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4005: if ( nd_module ) {
1.16 noro 4006: if ( OID(BDY(t)) == O_DPM ) {
4007: Z cont;
4008: DPM zdpm;
4009:
4010: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4011: else zdpm = (DPM)BDY(t);
4012: b = (pointer)dpmtondv(m,zdpm);
4013: } else {
4014: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4015: else zpl = (LIST)BDY(t);
1.1 noro 4016: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4017: }
1.1 noro 4018: } else {
4019: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4020: else zp = (P)BDY(t);
4021: b = (pointer)ptondv(CO,vv,zp);
4022: }
4023: if ( ishomo )
4024: ishomo = ishomo && ndv_ishomo(b);
4025: if ( m ) ndv_mod(m,b);
4026: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4027: }
4028: if ( fd0 ) NEXT(fd) = 0;
4029:
4030: if ( !ishomo && homo ) {
4031: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4032: b = (NDV)BDY(t); len = LEN(b);
4033: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4034: wmax = MAX(TD(DL(a)),wmax);
4035: }
4036: homogenize_order(ord,nvar,&ord1);
4037: nd_init_ord(ord1);
4038: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4039: for ( t = fd0; t; t = NEXT(t) )
4040: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4041: }
4042:
1.24 noro 4043: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4044: if ( nd_gentrace ) {
4045: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4046: }
4047: if ( nd_splist ) {
4048: *rp = compute_splist();
4049: return;
4050: }
4051: if ( nd_check_splist ) {
4052: if ( f4 ) {
4053: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4054: else *rp = 0;
4055: } else {
4056: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4057: else *rp = 0;
4058: }
4059: return;
4060: }
4061: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4062: if ( !x ) {
4063: *rp = 0; return;
4064: }
1.15 noro 4065: if ( nd_gentrace ) {
4066: MKVECT(hvect,nd_psn);
4067: for ( i = 0; i < nd_psn; i++ )
4068: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4069: }
1.1 noro 4070: if ( !ishomo && homo ) {
4071: /* dehomogenization */
4072: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4073: nd_init_ord(ord);
4074: nd_setup_parameters(nvar,0);
4075: }
4076: nd_demand = 0;
4077: if ( nd_module && nd_intersect ) {
4078: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4079: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4080: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4081: }
4082: conv_ilist(nd_demand,0,x,0);
4083: goto FINAL;
4084: }
4085: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4086: x = ndv_reducebase(x,perm);
4087: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4088: x = ndv_reduceall(m,x);
4089: cbpe = nd_bpe;
4090: if ( nd_gentrace && !f4 ) {
4091: tl2 = nd_alltracelist; nd_alltracelist = 0;
4092: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4093: tl3 = nd_alltracelist; nd_alltracelist = 0;
4094: if ( nd_gensyz ) {
4095: nd_gb(m,0,1,1,0);
4096: tl4 = nd_alltracelist; nd_alltracelist = 0;
4097: } else tl4 = 0;
4098: }
4099: nd_bpe = cbpe;
4100: nd_setup_parameters(nd_nvar,0);
4101: FINAL:
4102: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4103: NEXTNODE(r0,r);
4104: if ( nd_module ) {
4105: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4106: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4107: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4108: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4109: }
4110: if ( r0 ) NEXT(r) = 0;
4111: if ( !m && nd_nalg )
4112: r0 = postprocess_algcoef(av,alist,r0);
4113: MKLIST(*rp,r0);
4114: if ( nd_gentrace ) {
4115: if ( f4 ) {
1.6 noro 4116: STOZ(16,bpe);
4117: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4118: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4119: } else {
4120: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4121: tl3 = reverse_node(tl3);
4122: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4123: for ( t = tl2; t; t = NEXT(t) ) {
4124: /* s = [i,[*,j,*,*],...] */
4125: s = BDY((LIST)BDY(t));
1.6 noro 4126: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4127: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4128: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4129: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4130: }
4131: }
4132: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4133: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4134: }
4135: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4136: MKLIST(l5,tl4);
1.6 noro 4137: STOZ(nd_bpe,bpe);
1.15 noro 4138: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4139: }
4140: }
4141: #if 0
4142: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4143: #endif
4144: }
4145:
1.24 noro 4146: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,struct order_spec *ord,LIST *rp)
4147: {
4148: VL tv,fv,vv,vc,av;
4149: NODE fd,fd0,r,r0,t,x,s,xx;
4150: int e,max,nvar,i;
4151: NDV b;
4152: int ishomo,nalg,wmax,len;
4153: NMV a;
4154: P p,zp;
4155: Q dmy;
4156: struct order_spec *ord1;
4157: int j;
4158: int *perm;
4159: EPOS oepos;
4160: int obpe,oadv,ompos,cbpe;
4161:
4162: nd_module = 0;
4163: nd_demand = 0;
4164: parse_nd_option(current_option);
4165:
4166: if ( DP_Multiple )
4167: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4168: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4169: if ( m && nd_vc )
4170: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4171: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4172: switch ( ord->id ) {
4173: case 1:
4174: if ( ord->nv != nvar )
4175: error("nd_sba : invalid order specification");
4176: break;
4177: default:
4178: break;
4179: }
4180: nd_nalg = 0;
4181: nd_init_ord(ord);
4182: // for SIG comparison
4183: initd(ord);
4184: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4185: for ( tv = vv; tv; tv = NEXT(tv) ) {
4186: e = getdeg(tv->v,(P)BDY(t));
4187: max = MAX(e,max);
4188: }
4189: }
1.25 noro 4190: nd_setup_parameters(nvar,max);
1.24 noro 4191: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4192: ishomo = 1;
4193: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4194: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4195: else zp = (P)BDY(t);
4196: b = (pointer)ptondv(CO,vv,zp);
4197: if ( ishomo )
4198: ishomo = ishomo && ndv_ishomo(b);
4199: if ( m ) ndv_mod(m,b);
4200: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4201: }
4202: if ( fd0 ) NEXT(fd) = 0;
4203:
4204: if ( !ishomo && homo ) {
4205: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4206: b = (NDV)BDY(t); len = LEN(b);
4207: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4208: wmax = MAX(TD(DL(a)),wmax);
4209: }
4210: homogenize_order(ord,nvar,&ord1);
4211: nd_init_ord(ord1);
4212: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4213: for ( t = fd0; t; t = NEXT(t) )
4214: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4215: }
4216:
4217: ndv_setup(m,0,fd0,0,0,1);
4218: x = nd_sba_buch(m,ishomo || homo,&perm);
4219: if ( !x ) {
4220: *rp = 0; return;
4221: }
4222: if ( !ishomo && homo ) {
4223: /* dehomogenization */
4224: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4225: nd_init_ord(ord);
4226: nd_setup_parameters(nvar,0);
4227: }
4228: nd_demand = 0;
4229: x = ndv_reducebase(x,perm);
4230: x = ndv_reduceall(m,x);
4231: nd_setup_parameters(nd_nvar,0);
4232: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4233: NEXTNODE(r0,r);
4234: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4235: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4236: }
4237: if ( r0 ) NEXT(r) = 0;
4238: MKLIST(*rp,r0);
4239: }
4240:
1.1 noro 4241: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4242: {
4243: VL tv,fv,vv,vc,av;
4244: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4245: int e,max,nvar,i;
4246: NDV b;
4247: int ishomo,nalg;
4248: Alg alpha,dp;
4249: P p,zp;
4250: Q dmy;
4251: LIST f1,f2;
4252: Obj obj;
4253: NumberField nf;
4254: struct order_spec *ord1;
4255: int *perm;
4256:
4257: parse_nd_option(current_option);
4258: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4259: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4260: switch ( ord->id ) {
4261: case 1:
4262: if ( ord->nv != nvar )
4263: error("nd_check : invalid order specification");
4264: break;
4265: default:
4266: break;
4267: }
4268: nd_nalg = 0;
4269: av = 0;
4270: if ( !m ) {
4271: get_algtree((Obj)f,&av);
4272: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4273: nd_ntrans = nvar;
4274: nd_nalg = nalg;
4275: /* #i -> t#i */
4276: if ( nalg ) {
4277: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4278: ord = ord1;
4279: f = f1;
4280: }
4281: nvar += nalg;
4282: }
4283: nd_init_ord(ord);
4284: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4285: for ( tv = vv; tv; tv = NEXT(tv) ) {
4286: e = getdeg(tv->v,(P)BDY(t));
4287: max = MAX(e,max);
4288: }
4289: nd_setup_parameters(nvar,max);
4290: ishomo = 1;
4291: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4292: ptozp((P)BDY(t),1,&dmy,&zp);
4293: b = (pointer)ptondv(CO,vv,zp);
4294: if ( ishomo )
4295: ishomo = ishomo && ndv_ishomo(b);
4296: if ( m ) ndv_mod(m,b);
4297: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4298: }
4299: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4300: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4301: for ( x = 0, i = 0; i < nd_psn; i++ )
4302: x = update_base(x,i);
4303: if ( do_check ) {
4304: x = nd_gb(m,ishomo,1,0,&perm);
4305: if ( !x ) {
4306: *rp = 0;
4307: return;
4308: }
4309: } else {
4310: #if 0
4311: /* bug ? */
4312: for ( t = x; t; t = NEXT(t) )
4313: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4314: #else
4315: conv_ilist(0,0,x,&perm);
4316: #endif
4317: }
4318: x = ndv_reducebase(x,perm);
4319: x = ndv_reduceall(m,x);
4320: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4321: NEXTNODE(r0,r);
4322: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4323: }
4324: if ( r0 ) NEXT(r) = 0;
4325: if ( !m && nd_nalg )
4326: r0 = postprocess_algcoef(av,alist,r0);
4327: MKLIST(*rp,r0);
4328: }
4329:
4330: NDV recompute_trace(NODE trace,NDV *p,int m);
4331: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4332:
4333: NDV recompute_trace(NODE ti,NDV *p,int mod)
4334: {
4335: int c,c1,c2,i;
4336: NM mul,m,tail;
4337: ND d,r,rm;
4338: NODE sj;
4339: NDV red;
4340: Obj mj;
4341:
4342: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4343: CM(mul) = 1;
4344: tail = 0;
4345: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4346: sj = BDY((LIST)BDY(ti));
4347: if ( ARG0(sj) ) {
1.6 noro 4348: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4349: mj = (Obj)ARG2(sj);
4350: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4351: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4352: rm = ndv_mul_nm(mod,mul,red);
4353: if ( !r ) r = rm;
4354: else {
4355: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4356: if ( d ) {
4357: NEXT(tail) = m; tail = m; LEN(d)++;
4358: } else {
4359: MKND(nd_nvar,m,1,d); tail = BDY(d);
4360: }
4361: }
4362: if ( !m ) return 0; /* failure */
4363: else {
4364: BDY(r) = m;
4365: if ( mod > 0 || mod == -1 ) {
4366: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4367: DMAR(c1,c2,0,mod,c);
4368: nd_mul_c(mod,rm,c);
4369: } else {
4370: Z t,u;
4371:
4372: chsgnlf(HCZ(r),&t);
4373: divlf(t,HCZ(rm),&u);
4374: nd_mul_c_lf(rm,u);
4375: }
4376: r = nd_add(mod,r,rm);
4377: }
4378: }
4379: }
4380: }
4381: if ( tail ) NEXT(tail) = 0;
4382: d = nd_add(mod,d,r);
4383: nd_mul_c(mod,d,invm(HCM(d),mod));
4384: return ndtondv(mod,d);
4385: }
4386:
4387: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4388: {
4389: VL tv,fv,vv,vc,av;
4390: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4391: int e,max,nvar,i;
4392: NDV b;
4393: int ishomo,nalg;
4394: Alg alpha,dp;
4395: P p,zp;
4396: Q dmy;
4397: LIST f1,f2;
4398: Obj obj;
4399: NumberField nf;
4400: struct order_spec *ord1;
4401: NODE permtrace,intred,ind,perm,trace,ti;
4402: int len,n,j;
4403: NDV *db,*pb;
4404:
4405: parse_nd_option(current_option);
4406: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4407: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4408: switch ( ord->id ) {
4409: case 1:
4410: if ( ord->nv != nvar )
4411: error("nd_check : invalid order specification");
4412: break;
4413: default:
4414: break;
4415: }
4416: nd_init_ord(ord);
1.6 noro 4417: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4418: nd_setup_parameters(nvar,0);
4419:
4420: len = length(BDY(f));
4421: db = (NDV *)MALLOC(len*sizeof(NDV *));
4422: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4423: ptozp((P)BDY(t),1,&dmy,&zp);
4424: b = ptondv(CO,vv,zp);
4425: ndv_mod(m,b);
4426: ndv_mul_c(m,b,invm(HCM(b),m));
4427: db[i] = b;
4428: }
4429:
4430: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4431: intred = BDY((LIST)ARG3(BDY(tlist)));
4432: ind = BDY((LIST)ARG4(BDY(tlist)));
4433: perm = BDY((LIST)ARG0(permtrace));
4434: trace = NEXT(permtrace);
4435:
4436: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4437: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4438: if ( j > i ) i = j;
4439: }
4440: n = i+1;
4441: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4442: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4443: ti = BDY((LIST)BDY(t));
1.6 noro 4444: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4445: }
4446: for ( t = trace; t; t = NEXT(t) ) {
4447: ti = BDY((LIST)BDY(t));
1.6 noro 4448: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4449: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4450: if ( DP_Print ) {
4451: fprintf(asir_out,"."); fflush(asir_out);
4452: }
4453: }
4454: for ( t = intred; t; t = NEXT(t) ) {
4455: ti = BDY((LIST)BDY(t));
1.6 noro 4456: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4457: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4458: if ( DP_Print ) {
4459: fprintf(asir_out,"*"); fflush(asir_out);
4460: }
4461: }
4462: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4463: NEXTNODE(r0,r);
1.6 noro 4464: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4465: ndv_mul_c(m,b,invm(HCM(b),m));
4466: #if 0
1.6 noro 4467: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4468: #else
1.6 noro 4469: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4470: #endif
4471: }
4472: if ( r0 ) NEXT(r) = 0;
4473: MKLIST(*rp,r0);
4474: if ( DP_Print ) fprintf(asir_out,"\n");
4475: }
4476:
1.16 noro 4477: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1 noro 4478: {
4479: VL tv,fv,vv,vc,av;
4480: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4481: int m,nocheck,nvar,mindex,e,max;
4482: NDV c;
4483: NMV a;
4484: P p,zp;
4485: Q dmy;
4486: EPOS oepos;
4487: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4488: Alg alpha,dp;
4489: P poly;
4490: LIST f1,f2,zpl;
4491: Obj obj;
4492: NumberField nf;
4493: struct order_spec *ord1;
4494: struct oEGT eg_check,eg0,eg1;
4495: NODE tr,tl1,tl2,tl3,tl4;
4496: LIST l1,l2,l3,l4,l5;
4497: int *perm;
4498: int j,ret;
4499: Z jq,bpe;
1.15 noro 4500: VECT hvect;
1.1 noro 4501:
4502: nd_module = 0;
4503: nd_lf = 0;
4504: parse_nd_option(current_option);
4505: if ( nd_lf ) {
4506: if ( f4 )
4507: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4508: else
4509: error("nd_gr_trace is not implemented yet over a large finite field");
4510: return;
4511: }
4512: if ( DP_Multiple )
4513: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4514:
4515: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4516: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4517: switch ( ord->id ) {
4518: case 1:
4519: if ( ord->nv != nvar )
4520: error("nd_gr_trace : invalid order specification");
4521: break;
4522: default:
4523: break;
4524: }
4525:
4526: get_algtree((Obj)f,&av);
4527: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4528: nd_ntrans = nvar;
4529: nd_nalg = nalg;
4530: /* #i -> t#i */
4531: if ( nalg ) {
4532: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4533: ord = ord1;
4534: f = f1;
4535: }
4536: nvar += nalg;
4537:
4538: nocheck = 0;
4539: mindex = 0;
4540:
4541: if ( Demand ) nd_demand = 1;
4542: else nd_demand = 0;
4543:
4544: /* setup modulus */
4545: if ( trace < 0 ) {
4546: trace = -trace;
4547: nocheck = 1;
4548: }
4549: m = trace > 1 ? trace : get_lprime(mindex);
4550: nd_init_ord(ord);
4551: mrank = 0;
4552: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4553: for ( tv = vv; tv; tv = NEXT(tv) ) {
4554: if ( nd_module ) {
1.16 noro 4555: if ( OID(BDY(t)) == O_DPM ) {
4556: e = dpm_getdeg((DPM)BDY(t),&trank);
4557: max = MAX(e,max);
4558: mrank = MAX(mrank,trank);
4559: } else {
1.1 noro 4560: s = BDY((LIST)BDY(t));
4561: trank = length(s);
4562: mrank = MAX(mrank,trank);
4563: for ( ; s; s = NEXT(s) ) {
4564: e = getdeg(tv->v,(P)BDY(s));
4565: max = MAX(e,max);
4566: }
1.16 noro 4567: }
1.1 noro 4568: } else {
4569: e = getdeg(tv->v,(P)BDY(t));
4570: max = MAX(e,max);
4571: }
4572: }
4573: nd_setup_parameters(nvar,max);
4574: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4575: ishomo = 1;
4576: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4577: if ( nd_module ) {
1.16 noro 4578: if ( OID(BDY(t)) == O_DPM ) {
4579: Z cont;
4580: DPM zdpm;
4581:
1.17 noro 4582: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4583: else zdpm = (DPM)BDY(t);
4584: c = (pointer)dpmtondv(m,zdpm);
4585: } else {
4586: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4587: else zpl = (LIST)BDY(t);
1.1 noro 4588: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4589: }
1.1 noro 4590: } else {
1.16 noro 4591: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4592: else zp = (P)BDY(t);
4593: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4594: }
4595: if ( ishomo )
4596: ishomo = ishomo && ndv_ishomo(c);
4597: if ( c ) {
4598: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4599: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4600: }
4601: }
4602: if ( in0 ) NEXT(in) = 0;
4603: if ( fd0 ) NEXT(fd) = 0;
4604: if ( !ishomo && homo ) {
4605: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4606: c = (NDV)BDY(t); len = LEN(c);
4607: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4608: wmax = MAX(TD(DL(a)),wmax);
4609: }
4610: homogenize_order(ord,nvar,&ord1);
4611: nd_init_ord(ord1);
4612: nd_setup_parameters(nvar+1,wmax);
4613: for ( t = fd0; t; t = NEXT(t) )
4614: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4615: }
4616: if ( MaxDeg > 0 ) nocheck = 1;
4617: while ( 1 ) {
4618: tl1 = tl2 = tl3 = tl4 = 0;
4619: if ( Demand )
4620: nd_demand = 1;
1.24 noro 4621: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4622: if ( nd_gentrace ) {
4623: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4624: }
4625: if ( ret )
4626: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4627: if ( !ret || !cand ) {
4628: /* failure */
4629: if ( trace > 1 ) { *rp = 0; return; }
4630: else m = get_lprime(++mindex);
4631: continue;
4632: }
1.15 noro 4633: if ( nd_gentrace ) {
4634: MKVECT(hvect,nd_psn);
4635: for ( i = 0; i < nd_psn; i++ )
4636: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4637: }
1.1 noro 4638: if ( !ishomo && homo ) {
4639: /* dehomogenization */
4640: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4641: nd_init_ord(ord);
4642: nd_setup_parameters(nvar,0);
4643: }
4644: nd_demand = 0;
4645: cand = ndv_reducebase(cand,perm);
4646: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4647: cand = ndv_reduceall(0,cand);
4648: cbpe = nd_bpe;
4649: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4650: get_eg(&eg0);
4651: if ( nocheck )
4652: break;
4653: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4654: if ( nd_gentrace ) {
4655: tl3 = nd_alltracelist; nd_alltracelist = 0;
4656: } else tl3 = 0;
4657: /* gbcheck : cand is a GB of Id(cand) ? */
4658: if ( nd_vc || nd_gentrace || nd_gensyz )
4659: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4660: else
4661: ret = nd_f4(0,1,0)!=0;
4662: if ( nd_gentrace && nd_gensyz ) {
4663: tl4 = nd_alltracelist; nd_alltracelist = 0;
4664: } else tl4 = 0;
4665: }
4666: if ( ret ) break;
4667: else if ( trace > 1 ) {
4668: /* failure */
4669: *rp = 0; return;
4670: } else {
4671: /* try the next modulus */
4672: m = get_lprime(++mindex);
4673: /* reset the parameters */
4674: if ( !ishomo && homo ) {
4675: nd_init_ord(ord1);
4676: nd_setup_parameters(nvar+1,wmax);
4677: } else {
4678: nd_init_ord(ord);
4679: nd_setup_parameters(nvar,max);
4680: }
4681: }
4682: }
4683: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4684: if ( DP_Print )
1.6 noro 4685: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4686: /* dp->p */
4687: nd_bpe = cbpe;
4688: nd_setup_parameters(nd_nvar,0);
4689: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4690: if ( nd_module ) {
1.17 noro 4691: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4692: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4693: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4694: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4695: }
4696: if ( nd_nalg )
4697: cand = postprocess_algcoef(av,alist,cand);
4698: MKLIST(*rp,cand);
4699: if ( nd_gentrace ) {
4700: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4701: tl3 = reverse_node(tl3);
4702: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4703: for ( t = tl2; t; t = NEXT(t) ) {
4704: /* s = [i,[*,j,*,*],...] */
4705: s = BDY((LIST)BDY(t));
1.6 noro 4706: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4707: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4708: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4709: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4710: }
4711: }
4712: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4713: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4714: }
4715: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4716: MKLIST(l5,tl4);
1.6 noro 4717: STOZ(nd_bpe,bpe);
1.15 noro 4718: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4719: }
4720: }
4721:
4722: /* XXX : module element is not considered */
4723:
4724: void dltondl(int n,DL dl,UINT *r)
4725: {
4726: UINT *d;
4727: int i,j,l,s,ord_l;
4728: struct order_pair *op;
4729:
4730: d = (unsigned int *)dl->d;
4731: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
4732: if ( nd_blockmask ) {
4733: l = nd_blockmask->n;
4734: op = nd_blockmask->order_pair;
4735: for ( j = 0, s = 0; j < l; j++ ) {
4736: ord_l = op[j].length;
4737: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
4738: }
4739: TD(r) = ndl_weight(r);
4740: ndl_weight_mask(r);
4741: } else {
4742: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
4743: TD(r) = ndl_weight(r);
4744: }
4745: }
4746:
4747: DL ndltodl(int n,UINT *ndl)
4748: {
4749: DL dl;
4750: int *d;
4751: int i,j,l,s,ord_l;
4752: struct order_pair *op;
4753:
4754: NEWDL(dl,n);
4755: dl->td = TD(ndl);
4756: d = dl->d;
4757: if ( nd_blockmask ) {
4758: l = nd_blockmask->n;
4759: op = nd_blockmask->order_pair;
4760: for ( j = 0, s = 0; j < l; j++ ) {
4761: ord_l = op[j].length;
4762: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4763: }
4764: } else {
4765: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4766: }
4767: return dl;
4768: }
4769:
1.24 noro 4770: void _ndltodl(UINT *ndl,DL dl)
4771: {
4772: int *d;
4773: int i,j,l,s,ord_l,n;
4774: struct order_pair *op;
4775:
4776: n = nd_nvar;
4777: dl->td = TD(ndl);
4778: d = dl->d;
4779: if ( nd_blockmask ) {
4780: l = nd_blockmask->n;
4781: op = nd_blockmask->order_pair;
4782: for ( j = 0, s = 0; j < l; j++ ) {
4783: ord_l = op[j].length;
4784: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4785: }
4786: } else {
4787: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4788: }
4789: }
4790:
1.1 noro 4791: void nmtodp(int mod,NM m,DP *r)
4792: {
4793: DP dp;
4794: MP mr;
4795:
4796: NEWMP(mr);
4797: mr->dl = ndltodl(nd_nvar,DL(m));
4798: mr->c = (Obj)ndctop(mod,m->c);
4799: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4800: *r = dp;
4801: }
4802:
1.15 noro 4803: void ndltodp(UINT *d,DP *r)
4804: {
4805: DP dp;
4806: MP mr;
4807:
4808: NEWMP(mr);
4809: mr->dl = ndltodl(nd_nvar,d);
4810: mr->c = (Obj)ONE;
4811: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4812: *r = dp;
4813: }
4814:
1.1 noro 4815: void ndl_print(UINT *dl)
4816: {
4817: int n;
4818: int i,j,l,ord_l,s,s0;
4819: struct order_pair *op;
4820:
4821: n = nd_nvar;
4822: printf("<<");
4823: if ( nd_blockmask ) {
4824: l = nd_blockmask->n;
4825: op = nd_blockmask->order_pair;
4826: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4827: ord_l = op[j].length;
4828: for ( i = 0; i < ord_l; i++, s++ )
4829: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4830: }
4831: } else {
4832: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4833: }
4834: printf(">>");
4835: if ( nd_module && MPOS(dl) )
4836: printf("*e%d",MPOS(dl));
4837: }
4838:
4839: void nd_print(ND p)
4840: {
4841: NM m;
4842:
4843: if ( !p )
4844: printf("0\n");
4845: else {
4846: for ( m = BDY(p); m; m = NEXT(m) ) {
4847: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
4848: else printf("+%d*",CM(m));
4849: ndl_print(DL(m));
4850: }
4851: printf("\n");
4852: }
4853: }
4854:
4855: void nd_print_q(ND p)
4856: {
4857: NM m;
4858:
4859: if ( !p )
4860: printf("0\n");
4861: else {
4862: for ( m = BDY(p); m; m = NEXT(m) ) {
4863: printf("+");
1.6 noro 4864: printexpr(CO,(Obj)CZ(m));
1.1 noro 4865: printf("*");
4866: ndl_print(DL(m));
4867: }
4868: printf("\n");
4869: }
4870: }
4871:
4872: void ndp_print(ND_pairs d)
4873: {
4874: ND_pairs t;
4875:
4876: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4877: printf("\n");
4878: }
4879:
4880: void nd_removecont(int mod,ND p)
4881: {
4882: int i,n;
4883: Z *w;
4884: NM m;
4885: struct oVECT v;
4886:
4887: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4888: else if ( mod == -2 ) {
4889: Z inv;
4890: divlf(ONE,HCZ(p),&inv);
4891: nd_mul_c_lf(p,inv);
4892: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4893: else {
4894: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4895: w = (Z *)MALLOC(n*sizeof(Q));
4896: v.len = n;
4897: v.body = (pointer *)w;
1.6 noro 4898: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4899: removecont_array((P *)w,n,1);
1.6 noro 4900: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4901: }
4902: }
4903:
4904: void nd_removecont2(ND p1,ND p2)
4905: {
4906: int i,n1,n2,n;
4907: Z *w;
4908: NM m;
4909: struct oVECT v;
4910:
4911: n1 = nd_length(p1);
4912: n2 = nd_length(p2);
4913: n = n1+n2;
4914: w = (Z *)MALLOC(n*sizeof(Q));
4915: v.len = n;
4916: v.body = (pointer *)w;
4917: i = 0;
4918: if ( p1 )
1.6 noro 4919: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4920: if ( p2 )
1.6 noro 4921: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4922: removecont_array((P *)w,n,1);
4923: i = 0;
4924: if ( p1 )
1.6 noro 4925: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4926: if ( p2 )
1.6 noro 4927: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4928: }
4929:
4930: void ndv_removecont(int mod,NDV p)
4931: {
4932: int i,len,all_p;
4933: Z *c;
4934: P *w;
4935: Z dvr,t;
4936: P g,cont,tp;
4937: NMV m;
4938:
4939: if ( mod == -1 )
4940: ndv_mul_c(mod,p,_invsf(HCM(p)));
4941: else if ( mod == -2 ) {
4942: Z inv;
4943: divlf(ONE,HCZ(p),&inv);
4944: ndv_mul_c_lf(p,inv);
4945: } else if ( mod )
4946: ndv_mul_c(mod,p,invm(HCM(p),mod));
4947: else {
4948: len = p->len;
4949: w = (P *)MALLOC(len*sizeof(P));
4950: c = (Z *)MALLOC(len*sizeof(Q));
4951: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4952: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4953: all_p = all_p && !NUM(w[i]);
4954: }
4955: if ( all_p ) {
4956: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4957: mulp(nd_vc,(P)dvr,g,&cont);
4958: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4959: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4960: }
4961: } else {
4962: sortbynm((Q *)c,len);
4963: qltozl((Q *)c,len,&dvr);
4964: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4965: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4966: }
4967: }
4968: }
4969: }
4970:
4971: /* koko */
4972:
4973: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4974: {
4975: int len,i,max;
4976: NMV m,mr0,mr,t;
4977:
4978: len = p->len;
1.14 noro 4979: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 4980: max = MAX(max,TD(DL(m)));
4981: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4982: m = (NMV)((char *)mr0+(len-1)*oadv);
4983: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4984: t = (NMV)MALLOC(nmv_adv);
4985: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4986: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4987: CZ(mr) = CZ(m);
1.1 noro 4988: ndl_copy(DL(t),DL(mr));
4989: }
4990: NV(p)++;
4991: BDY(p) = mr0;
4992: }
4993:
4994: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4995: {
4996: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4997: int pos;
4998: Q *w;
4999: Q dvr,t;
5000: NMV m,r;
5001:
5002: len = p->len;
5003: newnvar = nd_nvar-1;
5004: newexporigin = nd_get_exporigin(ord);
5005: if ( nd_module ) newmpos = newexporigin-1;
5006: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5007: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5008: ndl_dehomogenize(DL(m));
5009: if ( newwpd != nd_wpd ) {
5010: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5011: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5012: CZ(r) = CZ(m);
1.1 noro 5013: if ( nd_module ) pos = MPOS(DL(m));
5014: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5015: adj = nd_exporigin-newexporigin;
5016: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5017: if ( nd_module ) {
5018: DL(r)[newmpos] = pos;
5019: }
5020: }
5021: }
5022: NV(p)--;
5023: }
5024:
5025: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5026: {
5027: int i;
5028: P *tpl,*tpl1;
5029: NODE l;
5030: P h,gcd,t;
5031:
5032: tpl = (P *)MALLOC(m*sizeof(P));
5033: tpl1 = (P *)MALLOC(m*sizeof(P));
5034: bcopy(pl,tpl,m*sizeof(P));
5035: gcd = (P)ONE;
5036: for ( l = nd_hcf; l; l = NEXT(l) ) {
5037: h = (P)BDY(l);
5038: while ( 1 ) {
5039: for ( i = 0; i < m; i++ )
5040: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5041: break;
5042: if ( i == m ) {
5043: bcopy(tpl1,tpl,m*sizeof(P));
5044: mulp(vl,gcd,h,&t); gcd = t;
5045: } else
5046: break;
5047: }
5048: }
5049: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5050: if ( full ) {
5051: heu_nezgcdnpz(vl,tpl,m,&t);
5052: mulp(vl,gcd,t,pr);
5053: } else
5054: *pr = gcd;
5055: }
5056:
5057: void removecont_array(P *p,int n,int full)
5058: {
5059: int all_p,all_q,i;
5060: Z *c;
5061: P *w;
5062: P t,s;
5063:
5064: for ( all_q = 1, i = 0; i < n; i++ )
5065: all_q = all_q && NUM(p[i]);
5066: if ( all_q ) {
5067: removecont_array_q((Z *)p,n);
5068: } else {
5069: c = (Z *)MALLOC(n*sizeof(Z));
5070: w = (P *)MALLOC(n*sizeof(P));
5071: for ( i = 0; i < n; i++ ) {
5072: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5073: }
5074: removecont_array_q(c,n);
5075: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5076: for ( i = 0; i < n; i++ ) {
5077: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5078: }
5079: }
5080: }
5081:
5082: /* c is an int array */
5083:
5084: void removecont_array_q(Z *c,int n)
5085: {
5086: struct oVECT v;
5087: Z d0,d1,a,u,u1,gcd;
5088: int i,j;
5089: Z *q,*r;
5090:
5091: q = (Z *)MALLOC(n*sizeof(Z));
5092: r = (Z *)MALLOC(n*sizeof(Z));
5093: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5094: gcdvz_estimate(&v,&d0);
5095: for ( i = 0; i < n; i++ ) {
5096: divqrz(c[i],d0,&q[i],&r[i]);
5097: }
5098: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5099: if ( i < n ) {
5100: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5101: gcdvz(&v,&d1);
5102: gcdz(d0,d1,&gcd);
1.6 noro 5103: /* exact division */
5104: divsz(d0,gcd,&a);
1.1 noro 5105: for ( i = 0; i < n; i++ ) {
5106: mulz(a,q[i],&u);
5107: if ( r[i] ) {
1.6 noro 5108: /* exact division */
5109: divsz(r[i],gcd,&u1);
1.1 noro 5110: addz(u,u1,&q[i]);
5111: } else
5112: q[i] = u;
5113: }
5114: }
5115: for ( i = 0; i < n; i++ ) c[i] = q[i];
5116: }
5117:
1.4 noro 5118: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5119:
5120: void mpz_removecont_array(mpz_t *c,int n)
5121: {
5122: mpz_t d0,a,u,u1,gcd;
5123: int i,j;
1.13 noro 5124: static mpz_t *q,*r;
5125: static int c_len = 0;
1.4 noro 5126:
5127: for ( i = 0; i < n; i++ )
5128: if ( mpz_sgn(c[i]) ) break;
5129: if ( i == n ) return;
5130: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5131: if ( n > c_len ) {
5132: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5133: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5134: c_len = n;
5135: }
1.4 noro 5136: for ( i = 0; i < n; i++ ) {
5137: mpz_init(q[i]); mpz_init(r[i]);
5138: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5139: }
5140: for ( i = 0; i < n; i++ )
5141: if ( mpz_sgn(r[i]) ) break;
5142: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5143: if ( i < n ) {
5144: mpz_gcd(gcd,d0,r[i]);
5145: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5146: mpz_div(a,d0,gcd);
5147: for ( i = 0; i < n; i++ ) {
5148: mpz_mul(u,a,q[i]);
5149: if ( mpz_sgn(r[i]) ) {
5150: mpz_div(u1,r[i],gcd);
5151: mpz_add(q[i],u,u1);
5152: } else
5153: mpz_set(q[i],u);
5154: }
5155: }
5156: for ( i = 0; i < n; i++ )
5157: mpz_set(c[i],q[i]);
5158: }
5159:
1.1 noro 5160: void nd_mul_c(int mod,ND p,int mul)
5161: {
5162: NM m;
5163: int c,c1;
5164:
5165: if ( !p ) return;
5166: if ( mul == 1 ) return;
5167: if ( mod == -1 )
5168: for ( m = BDY(p); m; m = NEXT(m) )
5169: CM(m) = _mulsf(CM(m),mul);
5170: else
5171: for ( m = BDY(p); m; m = NEXT(m) ) {
5172: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5173: }
5174: }
5175:
5176: void nd_mul_c_lf(ND p,Z mul)
5177: {
5178: NM m;
5179: Z c;
5180:
5181: if ( !p ) return;
5182: if ( UNIZ(mul) ) return;
5183: for ( m = BDY(p); m; m = NEXT(m) ) {
5184: mullf(CZ(m),mul,&c); CZ(m) = c;
5185: }
5186: }
5187:
5188: void nd_mul_c_q(ND p,P mul)
5189: {
5190: NM m;
5191: P c;
5192:
5193: if ( !p ) return;
5194: if ( UNIQ(mul) ) return;
5195: for ( m = BDY(p); m; m = NEXT(m) ) {
5196: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5197: }
5198: }
5199:
5200: void nd_mul_c_p(VL vl,ND p,P mul)
5201: {
5202: NM m;
5203: P c;
5204:
5205: if ( !p ) return;
5206: for ( m = BDY(p); m; m = NEXT(m) ) {
5207: mulp(vl,CP(m),mul,&c); CP(m) = c;
5208: }
5209: }
5210:
5211: void nd_free(ND p)
5212: {
5213: NM t,s;
5214:
5215: if ( !p ) return;
5216: t = BDY(p);
5217: while ( t ) {
5218: s = NEXT(t);
5219: FREENM(t);
5220: t = s;
5221: }
5222: FREEND(p);
5223: }
5224:
5225: void ndv_free(NDV p)
5226: {
5227: GCFREE(BDY(p));
5228: }
5229:
5230: void nd_append_red(UINT *d,int i)
5231: {
5232: RHist m,m0;
5233: int h;
5234:
5235: NEWRHist(m);
5236: h = ndl_hash_value(d);
5237: m->index = i;
5238: ndl_copy(d,DL(m));
5239: NEXT(m) = nd_red[h];
5240: nd_red[h] = m;
5241: }
5242:
5243: UINT *ndv_compute_bound(NDV p)
5244: {
5245: UINT *d1,*d2,*t;
5246: UINT u;
5247: int i,j,k,l,len,ind;
5248: NMV m;
5249:
5250: if ( !p )
5251: return 0;
5252: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5253: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5254: len = LEN(p);
5255: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5256: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5257: ndl_max(DL(m),d1,d2);
5258: t = d1; d1 = d2; d2 = t;
5259: }
5260: l = nd_nvar+31;
5261: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5262: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5263: u = d1[i];
5264: k = (nd_epw-1)*nd_bpe;
5265: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5266: t[ind] = (u>>k)&nd_mask0;
5267: }
5268: for ( ; ind < l; ind++ ) t[ind] = 0;
5269: return t;
5270: }
5271:
5272: UINT *nd_compute_bound(ND p)
5273: {
5274: UINT *d1,*d2,*t;
5275: UINT u;
5276: int i,j,k,l,len,ind;
5277: NM m;
5278:
5279: if ( !p )
5280: return 0;
5281: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5282: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5283: len = LEN(p);
5284: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5285: for ( m = NEXT(m); m; m = NEXT(m) ) {
5286: ndl_lcm(DL(m),d1,d2);
5287: t = d1; d1 = d2; d2 = t;
5288: }
5289: l = nd_nvar+31;
5290: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5291: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5292: u = d1[i];
5293: k = (nd_epw-1)*nd_bpe;
5294: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5295: t[ind] = (u>>k)&nd_mask0;
5296: }
5297: for ( ; ind < l; ind++ ) t[ind] = 0;
5298: return t;
5299: }
5300:
5301: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5302: /* of a term. In this case we need additional 1 word. */
5303:
5304: int nd_get_exporigin(struct order_spec *ord)
5305: {
5306: switch ( ord->id ) {
1.21 noro 5307: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5308: return 1+nd_module;
5309: case 1: case 257:
5310: /* block order */
5311: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5312: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5313: return ord->ord.block.length+1+nd_module;
5314: case 3: case 259:
5315: #if 0
5316: error("nd_get_exporigin : composite order is not supported yet.");
5317: #else
5318: return 1+nd_module;
5319: #endif
5320: default:
5321: error("nd_get_exporigin : ivalid argument.");
5322: return 0;
5323: }
5324: }
5325:
5326: void nd_setup_parameters(int nvar,int max) {
5327: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5328: struct order_pair *op;
5329:
5330: nd_nvar = nvar;
5331: if ( max ) {
5332: /* XXX */
5333: if ( do_weyl ) nd_bpe = 32;
5334: else if ( max < 2 ) nd_bpe = 1;
5335: else if ( max < 4 ) nd_bpe = 2;
5336: else if ( max < 8 ) nd_bpe = 3;
5337: else if ( max < 16 ) nd_bpe = 4;
5338: else if ( max < 32 ) nd_bpe = 5;
5339: else if ( max < 64 ) nd_bpe = 6;
5340: else if ( max < 256 ) nd_bpe = 8;
5341: else if ( max < 1024 ) nd_bpe = 10;
5342: else if ( max < 65536 ) nd_bpe = 16;
5343: else nd_bpe = 32;
5344: }
5345: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5346: UINT t;
5347: int st;
5348: int *v;
5349: /* t = max(weights) */
5350: t = 0;
5351: if ( current_dl_weight_vector )
5352: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5353: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5354: if ( t < st ) t = st;
5355: }
5356: if ( nd_matrix )
5357: for ( i = 0; i < nd_matrix_len; i++ )
5358: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5359: if ( (st=v[j]) < 0 ) st = -st;
5360: if ( t < st ) t = st;
5361: }
5362: /* i = bitsize of t */
5363: for ( i = 0; t; t >>=1, i++ );
5364: /* i += bitsize of nd_nvar */
5365: for ( t = nd_nvar; t; t >>=1, i++);
5366: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5367: if ( (nd_bpe+i) >= 31 )
5368: error("nd_setup_parameters : too large weight");
5369: }
5370: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5371: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5372: nd_exporigin = nd_get_exporigin(nd_ord);
5373: wpd = nd_exporigin+elen;
5374: if ( nd_module )
5375: nd_mpos = nd_exporigin-1;
5376: else
5377: nd_mpos = -1;
5378: if ( wpd != nd_wpd ) {
5379: nd_free_private_storage();
5380: nd_wpd = wpd;
5381: }
5382: if ( nd_bpe < 32 ) {
5383: nd_mask0 = (1<<nd_bpe)-1;
5384: } else {
5385: nd_mask0 = 0xffffffff;
5386: }
5387: bzero(nd_mask,sizeof(nd_mask));
5388: nd_mask1 = 0;
5389: for ( i = 0; i < nd_epw; i++ ) {
5390: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5391: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5392: }
5393: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5394: nd_epos = nd_create_epos(nd_ord);
5395: nd_blockmask = nd_create_blockmask(nd_ord);
5396: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5397: }
5398:
5399: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5400: {
5401: int i,obpe,oadv,h;
5402: static NM prev_nm_free_list;
5403: static ND_pairs prev_ndp_free_list;
5404: RHist mr0,mr;
5405: RHist r;
5406: RHist *old_red;
5407: ND_pairs s0,s,t;
5408: EPOS oepos;
5409:
5410: obpe = nd_bpe;
5411: oadv = nmv_adv;
5412: oepos = nd_epos;
5413: if ( obpe < 2 ) nd_bpe = 2;
5414: else if ( obpe < 3 ) nd_bpe = 3;
5415: else if ( obpe < 4 ) nd_bpe = 4;
5416: else if ( obpe < 5 ) nd_bpe = 5;
5417: else if ( obpe < 6 ) nd_bpe = 6;
5418: else if ( obpe < 8 ) nd_bpe = 8;
5419: else if ( obpe < 10 ) nd_bpe = 10;
5420: else if ( obpe < 16 ) nd_bpe = 16;
5421: else if ( obpe < 32 ) nd_bpe = 32;
5422: else error("nd_reconstruct : exponent too large");
5423:
5424: nd_setup_parameters(nd_nvar,0);
5425: prev_nm_free_list = _nm_free_list;
5426: prev_ndp_free_list = _ndp_free_list;
5427: _nm_free_list = 0;
5428: _ndp_free_list = 0;
5429: for ( i = nd_psn-1; i >= 0; i-- ) {
5430: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5431: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5432: }
5433: if ( trace )
5434: for ( i = nd_psn-1; i >= 0; i-- ) {
5435: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5436: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5437: }
5438: s0 = 0;
5439: for ( t = d; t; t = NEXT(t) ) {
5440: NEXTND_pairs(s0,s);
5441: s->i1 = t->i1;
5442: s->i2 = t->i2;
1.24 noro 5443: s->sig = t->sig;
1.1 noro 5444: SG(s) = SG(t);
5445: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5446: }
5447:
5448: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5449: for ( i = 0; i < REDTAB_LEN; i++ ) {
5450: old_red[i] = nd_red[i];
5451: nd_red[i] = 0;
5452: }
5453: for ( i = 0; i < REDTAB_LEN; i++ )
5454: for ( r = old_red[i]; r; r = NEXT(r) ) {
5455: NEWRHist(mr);
5456: mr->index = r->index;
5457: SG(mr) = SG(r);
5458: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5459: h = ndl_hash_value(DL(mr));
5460: NEXT(mr) = nd_red[h];
5461: nd_red[h] = mr;
1.24 noro 5462: mr->sig = r->sig;
1.1 noro 5463: }
5464: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5465: old_red = 0;
5466: for ( i = 0; i < nd_psn; i++ ) {
5467: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5468: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5469: r->sig = nd_psh[i]->sig;
1.1 noro 5470: nd_psh[i] = r;
5471: }
5472: if ( s0 ) NEXT(s) = 0;
5473: prev_nm_free_list = 0;
5474: prev_ndp_free_list = 0;
5475: #if 0
5476: GC_gcollect();
5477: #endif
5478: return s0;
5479: }
5480:
5481: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5482: {
5483: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5484: struct order_pair *op;
5485:
5486: n = nd_nvar;
5487: oepw = (sizeof(UINT)*8)/obpe;
5488: omask0 = (1<<obpe)-1;
5489: TD(r) = TD(d);
5490: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5491: if ( nd_blockmask ) {
5492: l = nd_blockmask->n;
5493: op = nd_blockmask->order_pair;
5494: for ( i = 1; i < nd_exporigin; i++ )
5495: r[i] = d[i];
5496: for ( j = 0, s = 0; j < l; j++ ) {
5497: ord_l = op[j].length;
5498: for ( i = 0; i < ord_l; i++, s++ ) {
5499: ei = GET_EXP_OLD(d,s);
5500: PUT_EXP(r,s,ei);
5501: }
5502: }
5503: } else {
5504: for ( i = 0; i < n; i++ ) {
5505: ei = GET_EXP_OLD(d,i);
5506: PUT_EXP(r,i,ei);
5507: }
5508: }
5509: if ( nd_module ) MPOS(r) = MPOS(d);
5510: }
5511:
5512: ND nd_copy(ND p)
5513: {
5514: NM m,mr,mr0;
5515: int c,n;
5516: ND r;
5517:
5518: if ( !p )
5519: return 0;
5520: else {
5521: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5522: NEXTNM(mr0,mr);
5523: CM(mr) = CM(m);
5524: ndl_copy(DL(m),DL(mr));
5525: }
5526: NEXT(mr) = 0;
5527: MKND(NV(p),mr0,LEN(p),r);
5528: SG(r) = SG(p);
5529: return r;
5530: }
5531: }
5532:
5533: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5534: {
5535: NM m1,m2;
5536: NDV p1,p2;
5537: ND t1,t2;
5538: UINT *lcm;
5539: P gp,tp;
5540: Z g,t;
5541: Z iq;
5542: int td;
5543: LIST hist;
5544: NODE node;
5545: DP d;
5546:
5547: if ( !mod && nd_demand ) {
5548: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5549: } else {
5550: if ( trace ) {
5551: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5552: } else {
5553: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5554: }
5555: }
5556: lcm = LCM(p);
5557: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5558: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5559: FREENM(m1); return 0;
5560: }
5561: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5562: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5563: FREENM(m1); FREENM(m2); return 0;
5564: }
5565:
5566: if ( mod == -1 ) {
5567: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5568: } else if ( mod > 0 ) {
5569: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5570: } else if ( mod == -2 ) {
5571: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5572: } else if ( nd_vc ) {
5573: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5574: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5575: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5576: } else {
1.6 noro 5577: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5578: }
5579: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5580: *rp = nd_add(mod,t1,t2);
5581: if ( nd_gentrace ) {
5582: /* nd_tracelist is initialized */
1.6 noro 5583: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5584: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5585: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5586: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5587: nd_tracelist = node;
5588: }
1.24 noro 5589: if ( *rp )
5590: (*rp)->sig = p->sig;
1.1 noro 5591: FREENM(m1); FREENM(m2);
5592: return 1;
5593: }
5594:
5595: void ndv_mul_c(int mod,NDV p,int mul)
5596: {
5597: NMV m;
5598: int c,c1,len,i;
5599:
5600: if ( !p ) return;
5601: len = LEN(p);
5602: if ( mod == -1 )
5603: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5604: CM(m) = _mulsf(CM(m),mul);
5605: else
5606: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5607: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5608: }
5609: }
5610:
5611: void ndv_mul_c_lf(NDV p,Z mul)
5612: {
5613: NMV m;
5614: Z c;
5615: int len,i;
5616:
5617: if ( !p ) return;
5618: len = LEN(p);
5619: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5620: mullf(CZ(m),mul,&c); CZ(m) = c;
5621: }
5622: }
5623:
5624: /* for nd_det */
5625: void ndv_mul_c_q(NDV p,Z mul)
5626: {
5627: NMV m;
5628: Z c;
5629: int len,i;
5630:
5631: if ( !p ) return;
5632: len = LEN(p);
5633: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 5634: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 5635: }
5636: }
5637:
5638: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
5639: int n2,i,j,l,n,tlen;
5640: UINT *d0;
5641: NM *tab,*psum;
5642: ND s,r;
5643: NM t;
5644: NMV m1;
5645:
5646: if ( !p ) return 0;
5647: n = NV(p); n2 = n>>1;
5648: d0 = DL(m0);
5649: l = LEN(p);
5650: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
5651: tab = (NM *)MALLOC(tlen*sizeof(NM));
5652: psum = (NM *)MALLOC(tlen*sizeof(NM));
5653: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
5654: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
5655: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
5656: /* m0(NM) * m1(NMV) => tab(NM) */
5657: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
5658: for ( j = 0; j < tlen; j++ ) {
5659: if ( tab[j] ) {
5660: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
5661: }
5662: }
5663: }
5664: for ( i = tlen-1, r = 0; i >= 0; i-- )
5665: if ( psum[i] ) {
5666: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
5667: MKND(n,psum[i],j,s);
5668: r = nd_add(mod,r,s);
5669: }
5670: if ( r ) SG(r) = SG(p)+TD(d0);
5671: return r;
5672: }
5673:
5674: /* product of monomials */
5675: /* XXX block order is not handled correctly */
5676:
5677: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
5678: {
5679: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
5680: UINT *d0,*d1,*d,*dt,*ctab;
5681: Z *ctab_q;
5682: Z q,q1;
5683: UINT c0,c1,c;
5684: NM *p;
5685: NM m,t;
5686: int mpos;
5687:
5688: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
5689: if ( !m0 || !m1 ) return;
5690: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
5691: if ( nd_module )
5692: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
5693:
5694: NEWNM(m); d = DL(m);
5695: if ( mod ) {
5696: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
5697: } else if ( nd_vc )
5698: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
5699: else
1.6 noro 5700: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 5701: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
5702: homo = n&1 ? 1 : 0;
5703: if ( homo ) {
5704: /* offset of h-degree */
5705: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
5706: PUT_EXP(DL(m),n-1,h);
5707: TD(DL(m)) = h;
5708: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5709: }
5710: tab[0] = m;
5711: NEWNM(m); d = DL(m);
5712: for ( i = 0, curlen = 1; i < n2; i++ ) {
5713: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
5714: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
5715: /* xi^a*(Di^k*xi^l)*Di^b */
5716: a += l; b += k;
5717: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
5718: if ( !k || !l ) {
5719: for ( j = 0; j < curlen; j++ )
5720: if ( (t = tab[j]) != 0 ) {
5721: dt = DL(t);
5722: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
5723: if ( nd_blockmask ) ndl_weight_mask(dt);
5724: }
5725: curlen *= k+1;
5726: continue;
5727: }
5728: min = MIN(k,l);
5729: if ( mod ) {
5730: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
5731: mkwcm(k,l,mod,(int *)ctab);
5732: } else {
5733: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
5734: mkwc(k,l,ctab_q);
5735: }
5736: for ( j = min; j >= 0; j-- ) {
5737: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
5738: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
5739: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
5740: if ( homo ) {
5741: TD(d) = s;
5742: PUT_EXP(d,n-1,s-h);
5743: } else TD(d) = h;
5744: if ( nd_blockmask ) ndl_weight_mask(d);
5745: if ( mod ) c = ctab[j];
5746: else q = ctab_q[j];
5747: p = tab+curlen*j;
5748: if ( j == 0 ) {
5749: for ( u = 0; u < curlen; u++, p++ ) {
5750: if ( tab[u] ) {
5751: ndl_addto(DL(tab[u]),d);
5752: if ( mod ) {
5753: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
5754: } else if ( nd_vc )
5755: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
5756: else {
1.6 noro 5757: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 5758: }
5759: }
5760: }
5761: } else {
5762: for ( u = 0; u < curlen; u++, p++ ) {
5763: if ( tab[u] ) {
5764: NEWNM(t);
5765: ndl_add(DL(tab[u]),d,DL(t));
5766: if ( mod ) {
5767: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
5768: } else if ( nd_vc )
5769: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
5770: else
1.6 noro 5771: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 5772: *p = t;
5773: }
5774: }
5775: }
5776: }
5777: curlen *= k+1;
5778: }
5779: FREENM(m);
5780: if ( nd_module ) {
5781: mpos = MPOS(d1);
5782: for ( i = 0; i < tlen; i++ )
5783: if ( tab[i] ) {
5784: d = DL(tab[i]);
5785: MPOS(d) = mpos;
5786: TD(d) = ndl_weight(d);
5787: }
5788: }
5789: }
5790:
5791: ND ndv_mul_nm_symbolic(NM m0,NDV p)
5792: {
5793: NM mr,mr0;
5794: NMV m;
5795: UINT *d,*dt,*dm;
5796: int c,n,td,i,c1,c2,len;
5797: Q q;
5798: ND r;
5799:
5800: if ( !p ) return 0;
5801: else {
5802: n = NV(p); m = BDY(p);
5803: d = DL(m0);
5804: len = LEN(p);
5805: mr0 = 0;
5806: td = TD(d);
5807: c = CM(m0);
5808: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5809: NEXTNM(mr0,mr);
5810: CM(mr) = 1;
5811: ndl_add(DL(m),d,DL(mr));
5812: }
5813: NEXT(mr) = 0;
5814: MKND(NV(p),mr0,len,r);
5815: SG(r) = SG(p) + TD(d);
5816: return r;
5817: }
5818: }
5819:
5820: ND ndv_mul_nm(int mod,NM m0,NDV p)
5821: {
5822: NM mr,mr0;
5823: NMV m;
5824: UINT *d,*dt,*dm;
5825: int c,n,td,i,c1,c2,len;
5826: P q;
5827: ND r;
5828:
5829: if ( !p ) return 0;
5830: else if ( do_weyl ) {
5831: if ( mod < 0 ) {
5832: error("ndv_mul_nm : not implemented (weyl)");
5833: return 0;
5834: } else
5835: return weyl_ndv_mul_nm(mod,m0,p);
5836: } else {
5837: n = NV(p); m = BDY(p);
5838: d = DL(m0);
5839: len = LEN(p);
5840: mr0 = 0;
5841: td = TD(d);
5842: if ( mod == -1 ) {
5843: c = CM(m0);
5844: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5845: NEXTNM(mr0,mr);
5846: CM(mr) = _mulsf(CM(m),c);
5847: ndl_add(DL(m),d,DL(mr));
5848: }
5849: } else if ( mod == -2 ) {
5850: Z cl;
5851: cl = CZ(m0);
5852: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5853: NEXTNM(mr0,mr);
5854: mullf(CZ(m),cl,&CZ(mr));
5855: ndl_add(DL(m),d,DL(mr));
5856: }
5857: } else if ( mod ) {
5858: c = CM(m0);
5859: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5860: NEXTNM(mr0,mr);
5861: c1 = CM(m);
5862: DMAR(c1,c,0,mod,c2);
5863: CM(mr) = c2;
5864: ndl_add(DL(m),d,DL(mr));
5865: }
5866: } else {
5867: q = CP(m0);
5868: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5869: NEXTNM(mr0,mr);
5870: mulp(nd_vc,CP(m),q,&CP(mr));
5871: ndl_add(DL(m),d,DL(mr));
5872: }
5873: }
5874: NEXT(mr) = 0;
5875: MKND(NV(p),mr0,len,r);
5876: SG(r) = SG(p) + TD(d);
5877: return r;
5878: }
5879: }
5880:
5881: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
5882: {
5883: NM mq0,mq;
5884: NMV tm;
5885: Q q;
5886: int i,nv,sg,c,c1,c2,hindex;
5887: ND p,t,r;
5888:
5889: if ( bucket->m < 0 ) return 0;
5890: else {
5891: nv = NV(d);
5892: mq0 = 0;
5893: tm = (NMV)MALLOC(nmv_adv);
5894: while ( 1 ) {
5895: if ( mod > 0 || mod == -1 )
5896: hindex = head_pbucket(mod,bucket);
5897: else if ( mod == -2 )
5898: hindex = head_pbucket_lf(bucket);
5899: else
5900: hindex = head_pbucket_q(bucket);
5901: if ( hindex < 0 ) break;
5902: p = bucket->body[hindex];
5903: NEXTNM(mq0,mq);
5904: ndl_sub(HDL(p),HDL(d),DL(mq));
5905: ndl_copy(DL(mq),DL(tm));
5906: if ( mod ) {
5907: c1 = invm(HCM(d),mod); c2 = HCM(p);
5908: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5909: CM(tm) = mod-c;
5910: } else {
1.6 noro 5911: divsz(HCZ(p),HCZ(d),&CZ(mq));
5912: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5913: }
5914: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5915: bucket->body[hindex] = nd_remove_head(p);
5916: t = nd_remove_head(t);
5917: add_pbucket(mod,bucket,t);
5918: }
5919: if ( !mq0 )
5920: r = 0;
5921: else {
5922: NEXT(mq) = 0;
5923: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5924: MKND(nv,mq0,i,r);
5925: /* XXX */
5926: SG(r) = HTD(r);
5927: }
5928: return r;
5929: }
5930: }
5931:
5932: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5933: {
5934: NMV m,mr,mr0,t;
5935: int len,i,k;
5936:
5937: if ( !p ) return;
5938: m = BDY(p); len = LEN(p);
5939: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5940: m = (NMV)((char *)mr0+(len-1)*oadv);
5941: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5942: t = (NMV)MALLOC(nmv_adv);
5943: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5944: CZ(t) = CZ(m);
1.1 noro 5945: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5946: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5947: CZ(mr) = CZ(t);
1.1 noro 5948: ndl_copy(DL(t),DL(mr));
5949: }
5950: BDY(p) = mr0;
5951: }
5952:
5953: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5954: {
5955: NMV m,mr,mr0;
5956: int len,i;
5957: NDV r;
5958:
5959: if ( !p ) return 0;
5960: m = BDY(p); len = LEN(p);
5961: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5962: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5963: ndl_zero(DL(mr));
5964: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5965: CZ(mr) = CZ(m);
1.1 noro 5966: }
5967: MKNDV(NV(p),mr0,len,r);
5968: SG(r) = SG(p);
1.24 noro 5969: r->sig = p->sig;
1.1 noro 5970: return r;
5971: }
5972:
5973: /* duplicate p */
5974:
5975: NDV ndv_dup(int mod,NDV p)
5976: {
5977: NDV d;
5978: NMV t,m,m0;
5979: int i,len;
5980:
5981: if ( !p ) return 0;
5982: len = LEN(p);
5983: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5984: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5985: ndl_copy(DL(t),DL(m));
1.6 noro 5986: CZ(m) = CZ(t);
1.1 noro 5987: }
5988: MKNDV(NV(p),m0,len,d);
5989: SG(d) = SG(p);
5990: return d;
5991: }
5992:
5993: NDV ndv_symbolic(int mod,NDV p)
5994: {
5995: NDV d;
5996: NMV t,m,m0;
5997: int i,len;
5998:
5999: if ( !p ) return 0;
6000: len = LEN(p);
6001: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6002: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6003: ndl_copy(DL(t),DL(m));
1.6 noro 6004: CZ(m) = ONE;
1.1 noro 6005: }
6006: MKNDV(NV(p),m0,len,d);
6007: SG(d) = SG(p);
6008: return d;
6009: }
6010:
6011: ND nd_dup(ND p)
6012: {
6013: ND d;
6014: NM t,m,m0;
6015:
6016: if ( !p ) return 0;
6017: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6018: NEXTNM(m0,m);
6019: ndl_copy(DL(t),DL(m));
1.6 noro 6020: CZ(m) = CZ(t);
1.1 noro 6021: }
6022: if ( m0 ) NEXT(m) = 0;
6023: MKND(NV(p),m0,LEN(p),d);
6024: SG(d) = SG(p);
6025: return d;
6026: }
6027:
6028: /* XXX if p->len == 0 then it represents 0 */
6029:
6030: void ndv_mod(int mod,NDV p)
6031: {
6032: NMV t,d;
6033: int r,s,u;
6034: int i,len,dlen;
6035: P cp;
6036: Z c;
6037: Obj gfs;
6038:
6039: if ( !p ) return;
6040: len = LEN(p);
6041: dlen = 0;
6042: if ( mod == -1 )
6043: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6044: simp_ff((Obj)CP(t),&gfs);
6045: if ( gfs ) {
6046: r = FTOIF(CONT((GFS)gfs));
6047: CM(d) = r;
6048: ndl_copy(DL(t),DL(d));
6049: NMV_ADV(d);
6050: dlen++;
6051: }
6052: }
6053: else if ( mod == -2 )
6054: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6055: simp_ff((Obj)CP(t),&gfs);
6056: if ( gfs ) {
6057: lmtolf((LM)gfs,&CZ(d));
6058: ndl_copy(DL(t),DL(d));
6059: NMV_ADV(d);
6060: dlen++;
6061: }
6062: }
6063: else
6064: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6065: if ( nd_vc ) {
6066: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6067: c = (Z)cp;
6068: } else
1.6 noro 6069: c = CZ(t);
1.1 noro 6070: r = remqi((Q)c,mod);
6071: if ( r ) {
6072: CM(d) = r;
6073: ndl_copy(DL(t),DL(d));
6074: NMV_ADV(d);
6075: dlen++;
6076: }
6077: }
6078: LEN(p) = dlen;
6079: }
6080:
6081: NDV ptondv(VL vl,VL dvl,P p)
6082: {
6083: ND nd;
6084:
6085: nd = ptond(vl,dvl,p);
6086: return ndtondv(0,nd);
6087: }
6088:
6089: void pltozpl(LIST l,Q *cont,LIST *pp)
6090: {
1.16 noro 6091: NODE nd,nd1;
6092: int n;
6093: P *pl;
6094: Q *cl;
6095: int i;
6096: P dmy;
6097: Z dvr,inv;
6098: LIST r;
6099:
6100: nd = BDY(l); n = length(nd);
6101: pl = (P *)MALLOC(n*sizeof(P));
6102: cl = (Q *)MALLOC(n*sizeof(Q));
6103: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6104: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6105: }
6106: qltozl(cl,n,&dvr);
6107: divz(ONE,dvr,&inv);
6108: nd = BDY(l);
6109: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6110: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6111: nd = 0;
6112: for ( i = n-1; i >= 0; i-- ) {
6113: MKNODE(nd1,pl[i],nd); nd = nd1;
6114: }
6115: MKLIST(r,nd);
6116: *pp = r;
1.1 noro 6117: }
6118:
6119: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6120:
6121: NDV pltondv(VL vl,VL dvl,LIST p)
6122: {
6123: int i;
6124: NODE t;
6125: ND r,ri;
6126: NM m;
6127:
6128: if ( !nd_module ) error("pltond : module order must be set");
6129: r = 0;
6130: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6131: ri = ptond(vl,dvl,(P)BDY(t));
6132: if ( ri )
6133: for ( m = BDY(ri); m; m = NEXT(m) ) {
6134: MPOS(DL(m)) = i;
6135: TD(DL(m)) = ndl_weight(DL(m));
6136: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6137: }
6138: r = nd_add(0,r,ri);
6139: }
6140: return ndtondv(0,r);
6141: }
6142:
6143: ND ptond(VL vl,VL dvl,P p)
6144: {
6145: int n,i,j,k,e;
6146: VL tvl;
6147: V v;
6148: DCP dc;
6149: DCP *w;
6150: ND r,s,t,u;
6151: P x;
6152: int c;
6153: UINT *d;
6154: NM m,m0;
6155:
6156: if ( !p )
6157: return 0;
6158: else if ( NUM(p) ) {
6159: NEWNM(m);
6160: ndl_zero(DL(m));
6161: if ( !INT((Q)p) )
6162: error("ptond : input must be integer-coefficient");
1.6 noro 6163: CZ(m) = (Z)p;
1.1 noro 6164: NEXT(m) = 0;
6165: MKND(nd_nvar,m,1,r);
6166: SG(r) = 0;
6167: return r;
6168: } else {
6169: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6170: w = (DCP *)MALLOC(k*sizeof(DCP));
6171: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6172: for ( i = 0, tvl = dvl, v = VR(p);
6173: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6174: if ( !tvl ) {
6175: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6176: t = ptond(vl,dvl,COEF(w[j]));
6177: pwrp(vl,x,DEG(w[j]),&p);
6178: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6179: }
6180: return s;
6181: } else {
6182: NEWNM(m0); d = DL(m0);
6183: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6184: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6185: TD(d) = MUL_WEIGHT(e,i);
6186: if ( nd_blockmask) ndl_weight_mask(d);
6187: if ( nd_module ) MPOS(d) = 0;
6188: t = ptond(vl,dvl,COEF(w[j]));
6189: for ( m = BDY(t); m; m = NEXT(m) )
6190: ndl_addto(DL(m),d);
6191: SG(t) += TD(d);
6192: s = nd_add(0,s,t);
6193: }
6194: FREENM(m0);
6195: return s;
6196: }
6197: }
6198: }
6199:
6200: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6201: {
6202: VL tvl;
6203: int len,n,j,i,e;
6204: NMV m;
6205: Z q;
6206: P c;
6207: UINT *d;
6208: P s,r,u,t,w;
6209: GFS gfs;
6210:
6211: if ( !p ) return 0;
6212: else {
6213: len = LEN(p);
6214: n = NV(p);
6215: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6216: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6217: if ( mod == -1 ) {
6218: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6219: } else if ( mod == -2 ) {
6220: c = (P)CZ(m);
6221: } else if ( mod > 0 ) {
1.6 noro 6222: STOZ(CM(m),q); c = (P)q;
1.1 noro 6223: } else
6224: c = CP(m);
6225: d = DL(m);
6226: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6227: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6228: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6229: }
6230: addp(vl,s,t,&u); s = u;
6231: }
6232: return s;
6233: }
6234: }
6235:
6236: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6237: {
6238: VL tvl;
6239: int len,n,j,i,e;
6240: NMV m;
6241: Z q;
6242: P c;
6243: UINT *d;
6244: P s,r,u,t,w;
6245: GFS gfs;
6246: P *a;
6247: LIST l;
6248: NODE nd,nd1;
6249:
6250: if ( !p ) return 0;
6251: else {
6252: a = (P *)MALLOC((rank+1)*sizeof(P));
6253: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6254: len = LEN(p);
6255: n = NV(p);
6256: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6257: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6258: if ( mod == -1 ) {
6259: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6260: } else if ( mod ) {
1.6 noro 6261: STOZ(CM(m),q); c = (P)q;
1.1 noro 6262: } else
6263: c = CP(m);
6264: d = DL(m);
6265: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6266: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6267: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6268: }
6269: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6270: }
6271: nd = 0;
6272: for ( i = rank; i > 0; i-- ) {
6273: MKNODE(nd1,a[i],nd); nd = nd1;
6274: }
6275: MKLIST(l,nd);
6276: return l;
6277: }
6278: }
6279:
6280: NDV ndtondv(int mod,ND p)
6281: {
6282: NDV d;
6283: NMV m,m0;
6284: NM t;
6285: int i,len;
6286:
6287: if ( !p ) return 0;
6288: len = LEN(p);
6289: if ( mod > 0 || mod == -1 )
6290: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6291: else
6292: m0 = m = MALLOC(len*nmv_adv);
6293: #if 0
6294: ndv_alloc += nmv_adv*len;
6295: #endif
6296: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6297: ndl_copy(DL(t),DL(m));
1.6 noro 6298: CZ(m) = CZ(t);
1.1 noro 6299: }
6300: MKNDV(NV(p),m0,len,d);
6301: SG(d) = SG(p);
1.24 noro 6302: d->sig = p->sig;
1.1 noro 6303: return d;
6304: }
6305:
1.16 noro 6306: static int dmm_comp_nv;
6307:
6308: int dmm_comp(DMM *a,DMM *b)
6309: {
6310: return -compdmm(dmm_comp_nv,*a,*b);
6311: }
6312:
6313: void dmm_sort_by_ord(DMM *a,int len,int nv)
6314: {
6315: dmm_comp_nv = nv;
6316: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6317: }
6318:
6319: void dpm_sort(DPM p,DPM *rp)
6320: {
6321: DMM t,t1;
6322: int len,i,n;
6323: DMM *a;
6324: DPM d;
6325:
6326: if ( !p ) *rp = 0;
6327: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6328: a = (DMM *)MALLOC(len*sizeof(DMM));
6329: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6330: n = p->nv;
6331: dmm_sort_by_ord(a,len,n);
6332: t = 0;
6333: for ( i = len-1; i >= 0; i-- ) {
6334: NEWDMM(t1);
6335: t1->c = a[i]->c;
6336: t1->dl = a[i]->dl;
6337: t1->pos = a[i]->pos;
6338: t1->next = t;
6339: t = t1;
6340: }
6341: MKDPM(n,t,d);
6342: SG(d) = SG(p);
6343: *rp = d;
6344: }
6345:
1.18 noro 6346: int dpm_comp(DPM *a,DPM *b)
6347: {
1.22 noro 6348: return -compdpm(CO,*a,*b);
1.18 noro 6349: }
6350:
6351: NODE dpm_sort_list(NODE l)
6352: {
6353: int i,len;
6354: NODE t,t1;
6355: DPM *a;
6356:
6357: len = length(l);
6358: a = (DPM *)MALLOC(len*sizeof(DPM));
6359: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6360: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6361: t = 0;
6362: for ( i = len-1; i >= 0; i-- ) {
6363: MKNODE(t1,(pointer)a[i],t); t = t1;
6364: }
6365: return t;
6366: }
6367:
1.20 noro 6368: int nmv_comp(NMV a,NMV b)
6369: {
1.21 noro 6370: int t;
6371: t = DL_COMPARE(a->dl,b->dl);
6372: return -t;
1.20 noro 6373: }
6374:
1.16 noro 6375: NDV dpmtondv(int mod,DPM p)
6376: {
6377: NDV d;
6378: NMV m,m0;
6379: DMM t;
6380: DMM *a;
6381: int i,len,n;
6382:
6383: if ( !p ) return 0;
6384: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6385: a = (DMM *)MALLOC(len*sizeof(DMM));
6386: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6387: n = p->nv;
6388: dmm_sort_by_ord(a,len,n);
6389: if ( mod > 0 || mod == -1 )
6390: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6391: else
6392: m0 = m = MALLOC(len*nmv_adv);
6393: #if 0
6394: ndv_alloc += nmv_adv*len;
6395: #endif
6396: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6397: dltondl(n,a[i]->dl,DL(m));
6398: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6399: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6400: CZ(m) = (Z)a[i]->c;
6401: }
1.20 noro 6402: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6403: MKNDV(NV(p),m0,len,d);
6404: SG(d) = SG(p);
6405: return d;
6406: }
6407:
1.1 noro 6408: ND ndvtond(int mod,NDV p)
6409: {
6410: ND d;
6411: NM m,m0;
6412: NMV t;
6413: int i,len;
6414:
6415: if ( !p ) return 0;
6416: m0 = 0;
6417: len = p->len;
6418: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6419: NEXTNM(m0,m);
6420: ndl_copy(DL(t),DL(m));
1.6 noro 6421: CZ(m) = CZ(t);
1.1 noro 6422: }
6423: NEXT(m) = 0;
6424: MKND(NV(p),m0,len,d);
6425: SG(d) = SG(p);
6426: return d;
6427: }
6428:
6429: DP ndvtodp(int mod,NDV p)
6430: {
6431: MP m,m0;
6432: DP d;
6433: NMV t;
6434: int i,len;
6435:
6436: if ( !p ) return 0;
6437: m0 = 0;
6438: len = p->len;
6439: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6440: NEXTMP(m0,m);
6441: m->dl = ndltodl(nd_nvar,DL(t));
6442: m->c = (Obj)ndctop(mod,t->c);
6443: }
6444: NEXT(m) = 0;
6445: MKDP(nd_nvar,m0,d);
6446: SG(d) = SG(p);
6447: return d;
6448: }
6449:
1.16 noro 6450: DPM ndvtodpm(int mod,NDV p)
6451: {
6452: DMM m,m0;
6453: DPM d;
6454: NMV t;
6455: int i,len;
6456:
6457: if ( !p ) return 0;
6458: m0 = 0;
6459: len = p->len;
6460: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6461: NEXTDMM(m0,m);
6462: m->dl = ndltodl(nd_nvar,DL(t));
6463: m->c = (Obj)ndctop(mod,t->c);
6464: m->pos = MPOS(DL(t));
6465: }
6466: NEXT(m) = 0;
6467: MKDPM(nd_nvar,m0,d);
6468: SG(d) = SG(p);
6469: return d;
6470: }
6471:
6472:
1.1 noro 6473: DP ndtodp(int mod,ND p)
6474: {
6475: MP m,m0;
6476: DP d;
6477: NM t;
6478: int i,len;
6479:
6480: if ( !p ) return 0;
6481: m0 = 0;
6482: len = p->len;
6483: for ( t = BDY(p); t; t = NEXT(t) ) {
6484: NEXTMP(m0,m);
6485: m->dl = ndltodl(nd_nvar,DL(t));
6486: m->c = (Obj)ndctop(mod,t->c);
6487: }
6488: NEXT(m) = 0;
6489: MKDP(nd_nvar,m0,d);
6490: SG(d) = SG(p);
6491: return d;
6492: }
6493:
6494: void ndv_print(NDV p)
6495: {
6496: NMV m;
6497: int i,len;
6498:
6499: if ( !p ) printf("0\n");
6500: else {
6501: len = LEN(p);
6502: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6503: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
6504: else printf("+%d*",CM(m));
6505: ndl_print(DL(m));
6506: }
6507: printf("\n");
6508: }
6509: }
6510:
6511: void ndv_print_q(NDV p)
6512: {
6513: NMV m;
6514: int i,len;
6515:
6516: if ( !p ) printf("0\n");
6517: else {
6518: len = LEN(p);
6519: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6520: printf("+");
1.6 noro 6521: printexpr(CO,(Obj)CZ(m));
1.1 noro 6522: printf("*");
6523: ndl_print(DL(m));
6524: }
6525: printf("\n");
6526: }
6527: }
6528:
6529: NODE ndv_reducebase(NODE x,int *perm)
6530: {
6531: int len,i,j;
6532: NDVI w;
6533: NODE t,t0;
6534:
6535: len = length(x);
6536: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6537: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6538: w[i].p = BDY(t); w[i].i = perm[i];
6539: }
6540: for ( i = 0; i < len; i++ ) {
6541: for ( j = 0; j < i; j++ ) {
6542: if ( w[i].p && w[j].p ) {
6543: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6544: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6545: }
6546: }
6547: }
6548: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6549: if ( w[i].p ) {
6550: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6551: perm[j++] = w[i].i;
6552: }
6553: }
6554: NEXT(t) = 0; x = t0;
6555: return x;
6556: }
6557:
6558: /* XXX incomplete */
6559:
1.21 noro 6560: extern DMMstack dmm_stack;
6561: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6562:
1.1 noro 6563: void nd_init_ord(struct order_spec *ord)
6564: {
6565: nd_module = (ord->id >= 256);
6566: if ( nd_module ) {
6567: nd_dcomp = -1;
1.21 noro 6568: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6569: nd_pot_nelim = ord->pot_nelim;
6570: nd_poly_weight_len = ord->nv;
6571: nd_poly_weight = ord->top_weight;
6572: nd_module_rank = ord->module_rank;
6573: nd_module_weight = ord->module_top_weight;
6574: }
6575: nd_matrix = 0;
6576: nd_matrix_len = 0;
6577: switch ( ord->id ) {
6578: case 0:
6579: switch ( ord->ord.simple ) {
6580: case 0:
6581: nd_dcomp = 1;
6582: nd_isrlex = 1;
6583: break;
6584: case 1:
6585: nd_dcomp = 1;
6586: nd_isrlex = 0;
6587: break;
6588: case 2:
6589: nd_dcomp = 0;
6590: nd_isrlex = 0;
6591: ndl_compare_function = ndl_lex_compare;
6592: break;
6593: case 11:
6594: /* XXX */
6595: nd_dcomp = 0;
6596: nd_isrlex = 1;
6597: ndl_compare_function = ndl_ww_lex_compare;
6598: break;
6599: default:
6600: error("nd_gr : unsupported order");
6601: }
6602: break;
6603: case 1:
6604: /* block order */
6605: /* XXX */
6606: nd_dcomp = -1;
6607: nd_isrlex = 0;
6608: ndl_compare_function = ndl_block_compare;
6609: break;
6610: case 2:
6611: /* matrix order */
6612: /* XXX */
6613: nd_dcomp = -1;
6614: nd_isrlex = 0;
6615: nd_matrix_len = ord->ord.matrix.row;
6616: nd_matrix = ord->ord.matrix.matrix;
6617: ndl_compare_function = ndl_matrix_compare;
6618: break;
6619: case 3:
6620: /* composite order */
6621: nd_dcomp = -1;
6622: nd_isrlex = 0;
6623: nd_worb_len = ord->ord.composite.length;
6624: nd_worb = ord->ord.composite.w_or_b;
6625: ndl_compare_function = ndl_composite_compare;
6626: break;
6627:
6628: /* module order */
6629: case 256:
6630: switch ( ord->ord.simple ) {
6631: case 0:
1.21 noro 6632: nd_dcomp = 0;
1.1 noro 6633: nd_isrlex = 1;
1.21 noro 6634: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 6635: break;
6636: case 1:
1.21 noro 6637: nd_dcomp = 0;
1.1 noro 6638: nd_isrlex = 0;
6639: ndl_compare_function = ndl_module_glex_compare;
6640: break;
6641: case 2:
1.21 noro 6642: nd_dcomp = 0;
1.1 noro 6643: nd_isrlex = 0;
1.21 noro 6644: ndl_compare_function = ndl_module_compare;
6645: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 6646: break;
6647: default:
1.21 noro 6648: error("nd_init_ord : unsupported order");
1.1 noro 6649: }
6650: break;
6651: case 257:
6652: /* block order */
6653: nd_isrlex = 0;
1.21 noro 6654: ndl_compare_function = ndl_module_compare;
6655: ndl_base_compare_function = ndl_block_compare;
1.1 noro 6656: break;
6657: case 258:
6658: /* matrix order */
6659: nd_isrlex = 0;
6660: nd_matrix_len = ord->ord.matrix.row;
6661: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 6662: ndl_compare_function = ndl_module_compare;
6663: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 6664: break;
6665: case 259:
6666: /* composite order */
6667: nd_isrlex = 0;
6668: nd_worb_len = ord->ord.composite.length;
6669: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 6670: ndl_compare_function = ndl_module_compare;
6671: ndl_base_compare_function = ndl_composite_compare;
6672: break;
6673: case 300:
6674: /* schreyer order */
6675: if ( ord->base->id != 256 )
6676: error("nd_init_ord : unsupported base order");
6677: ndl_compare_function = ndl_module_schreyer_compare;
6678: dmm_stack = ord->dmmstack;
6679: switch ( ord->base->ord.simple ) {
6680: case 0:
6681: nd_isrlex = 1;
6682: ndl_base_compare_function = ndl_glex_compare;
6683: dl_base_compare_function = cmpdl_revgradlex;
6684: break;
6685: case 1:
6686: nd_isrlex = 0;
6687: ndl_base_compare_function = ndl_glex_compare;
6688: dl_base_compare_function = cmpdl_gradlex;
6689: break;
6690: case 2:
6691: nd_isrlex = 0;
6692: ndl_base_compare_function = ndl_lex_compare;
6693: dl_base_compare_function = cmpdl_lex;
6694: break;
6695: default:
6696: error("nd_init_ord : unsupported order");
6697: }
1.1 noro 6698: break;
6699: }
6700: nd_ord = ord;
6701: }
6702:
6703: BlockMask nd_create_blockmask(struct order_spec *ord)
6704: {
6705: int n,i,j,s,l;
6706: UINT *t;
6707: BlockMask bm;
6708:
6709: /* we only create mask table for block order */
6710: if ( ord->id != 1 && ord->id != 257 )
6711: return 0;
6712: n = ord->ord.block.length;
6713: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
6714: bm->n = n;
6715: bm->order_pair = ord->ord.block.order_pair;
6716: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
6717: for ( i = 0, s = 0; i < n; i++ ) {
6718: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
6719: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
6720: l = bm->order_pair[i].length;
6721: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
6722: }
6723: return bm;
6724: }
6725:
6726: EPOS nd_create_epos(struct order_spec *ord)
6727: {
6728: int i,j,l,s,ord_l,ord_o;
6729: EPOS epos;
6730: struct order_pair *op;
6731:
6732: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
6733: switch ( ord->id ) {
1.21 noro 6734: case 0: case 256: case 300:
1.1 noro 6735: if ( nd_isrlex ) {
6736: for ( i = 0; i < nd_nvar; i++ ) {
6737: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
6738: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
6739: }
6740: } else {
6741: for ( i = 0; i < nd_nvar; i++ ) {
6742: epos[i].i = nd_exporigin + i/nd_epw;
6743: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6744: }
6745: }
6746: break;
6747: case 1: case 257:
6748: /* block order */
6749: l = ord->ord.block.length;
6750: op = ord->ord.block.order_pair;
6751: for ( j = 0, s = 0; j < l; j++ ) {
6752: ord_o = op[j].order;
6753: ord_l = op[j].length;
6754: if ( !ord_o )
6755: for ( i = 0; i < ord_l; i++ ) {
6756: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
6757: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
6758: }
6759: else
6760: for ( i = 0; i < ord_l; i++ ) {
6761: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
6762: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
6763: }
6764: s += ord_l;
6765: }
6766: break;
6767: case 2:
6768: /* matrix order */
6769: case 3:
6770: /* composite order */
6771: default:
6772: for ( i = 0; i < nd_nvar; i++ ) {
6773: epos[i].i = nd_exporigin + i/nd_epw;
6774: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6775: }
6776: break;
6777: }
6778: return epos;
6779: }
6780:
6781: /* external interface */
6782:
6783: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
6784: {
6785: NODE t,in0,in;
6786: ND ndf,nf;
6787: NDV ndvf;
6788: VL vv,tv;
6789: int stat,nvar,max,mrank;
6790: union oNDC dn;
6791: Q cont;
6792: P pp;
6793: LIST ppl;
6794:
6795: if ( !f ) {
6796: *rp = 0;
6797: return;
6798: }
6799: pltovl(v,&vv);
6800: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
6801:
6802: /* max=65536 implies nd_bpe=32 */
6803: max = 65536;
6804:
6805: nd_module = 0;
6806: /* nd_module will be set if ord is a module ordering */
6807: nd_init_ord(ord);
6808: nd_setup_parameters(nvar,max);
6809: if ( nd_module && OID(f) != O_LIST )
6810: error("nd_nf_p : the first argument must be a list");
6811: if ( nd_module ) mrank = length(BDY((LIST)f));
6812: /* conversion to ndv */
6813: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
6814: NEXTNODE(in0,in);
6815: if ( nd_module ) {
6816: if ( !BDY(t) || OID(BDY(t)) != O_LIST
6817: || length(BDY((LIST)BDY(t))) != mrank )
6818: error("nd_nf_p : inconsistent basis element");
6819: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
6820: else ppl = (LIST)BDY(t);
6821: BDY(in) = (pointer)pltondv(CO,vv,ppl);
6822: } else {
6823: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
6824: else pp = (P)BDY(t);
6825: BDY(in) = (pointer)ptondv(CO,vv,pp);
6826: }
6827: if ( m ) ndv_mod(m,(NDV)BDY(in));
6828: }
6829: if ( in0 ) NEXT(in) = 0;
6830:
6831: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
6832: else ndvf = ptondv(CO,vv,(P)f);
6833: if ( m ) ndv_mod(m,ndvf);
6834: ndf = (pointer)ndvtond(m,ndvf);
6835:
6836: /* dont sort, dont removecont */
1.24 noro 6837: ndv_setup(m,0,in0,1,1,0);
1.1 noro 6838: nd_scale=2;
1.6 noro 6839: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 6840: if ( !stat )
6841: error("nd_nf_p : exponent too large");
6842: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
6843: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
6844: }
6845:
6846: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
6847: {
6848: NM m;
6849: UINT *t,*s;
6850: int i;
6851:
6852: for ( i = 0; i < n; i++ ) r[i] = 0;
6853: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6854: t = DL(m);
6855: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6856: r[i] = CM(m);
6857: }
6858: for ( i = 0; !r[i]; i++ );
6859: return i;
6860: }
6861:
6862: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
6863: {
6864: NM m;
6865: UINT *t,*s;
6866: int i;
6867:
6868: for ( i = 0; i < n; i++ ) r[i] = 0;
6869: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6870: t = DL(m);
6871: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6872: r[i] = CZ(m);
1.1 noro 6873: }
6874: for ( i = 0; !r[i]; i++ );
6875: return i;
6876: }
6877:
6878: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
6879: {
6880: NM m;
6881: UINT *t,*s;
6882: int i;
6883:
6884: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
6885: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6886: t = DL(m);
6887: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6888: mpz_set(r[i],BDY(CZ(m)));
6889: }
6890: for ( i = 0; !mpz_sgn(r[i]); i++ );
6891: return i;
6892: }
6893:
6894: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
6895: {
6896: NM m;
6897: unsigned long *v;
6898: int i,j,h,size;
6899: UINT *s,*t;
6900:
6901: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
6902: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
6903: bzero(v,size);
6904: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
6905: t = DL(m);
6906: h = ndl_hash_value(t);
6907: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6908: v[i/BLEN] |= 1L <<(i%BLEN);
6909: }
6910: return v;
6911: }
6912:
6913: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
6914: {
6915: NMV mr;
6916: UINT *d,*t,*s;
6917: int i,j,len,h,head;
6918:
6919: d = DL(m);
6920: len = LEN(p);
6921: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6922: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6923: ndl_add(d,DL(mr),t);
6924: h = ndl_hash_value(t);
6925: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6926: if ( j == 0 ) head = i;
6927: v[i/BLEN] |= 1L <<(i%BLEN);
6928: }
6929: return head;
6930: }
6931:
6932: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
6933: {
6934: NM m;
6935: NMV mr;
6936: UINT *d,*t,*s;
6937: NDV p;
6938: int i,j,len;
6939: Z *r;
6940:
6941: m = pair->mul;
6942: d = DL(m);
6943: p = nd_ps[pair->index];
6944: len = LEN(p);
6945: r = (Z *)CALLOC(n,sizeof(Q));
6946: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6947: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6948: ndl_add(d,DL(mr),t);
6949: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6950: r[i] = CZ(mr);
1.1 noro 6951: }
6952: return r;
6953: }
6954:
1.11 noro 6955: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 6956: {
6957: NM m;
6958: NMV mr;
1.11 noro 6959: UINT *d,*t,*s,*u;
1.1 noro 6960: NDV p;
6961: unsigned char *ivc;
6962: unsigned short *ivs;
6963: UINT *v,*ivi,*s0v;
1.11 noro 6964: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 6965: IndArray r;
6966:
6967: m = pair->mul;
6968: d = DL(m);
6969: if ( trace )
6970: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
6971: else
6972: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
6973:
6974: len = LEN(p);
6975: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6976: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 6977: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
6978: ndl_add(d,DL(mr),t);
6979: st = prev;
6980: ed = n;
6981: while ( ed > st ) {
6982: md = (st+ed)/2;
6983: u = s0+md*nd_wpd;
6984: c = DL_COMPARE(u,t);
6985: if ( c == 0 ) break;
6986: else if ( c > 0 ) st = md;
6987: else ed = md;
6988: }
6989: prev = v[j] = md;
1.1 noro 6990: }
6991: r = (IndArray)MALLOC(sizeof(struct oIndArray));
6992: r->head = v[0];
6993: diff = 0;
6994: for ( i = 1; i < len; i++ ) {
6995: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
6996: }
6997: if ( diff < 256 ) {
6998: r->width = 1;
6999: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7000: r->index.c = ivc;
7001: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7002: } else if ( diff < 65536 ) {
7003: r->width = 2;
7004: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7005: r->index.s = ivs;
7006: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7007: } else {
7008: r->width = 4;
7009: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7010: r->index.i = ivi;
7011: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7012: }
7013: return r;
7014: }
7015:
7016: int compress_array(Z *svect,Z *cvect,int n)
7017: {
7018: int i,j;
7019:
7020: for ( i = j = 0; i < n; i++ )
7021: if ( svect[i] ) cvect[j++] = svect[i];
7022: return j;
7023: }
7024:
7025: void expand_array(Z *svect,Z *cvect,int n)
7026: {
7027: int i,j;
7028:
7029: for ( i = j = 0; j < n; i++ )
7030: if ( svect[i] ) svect[i] = cvect[j++];
7031: }
7032:
1.8 noro 7033: #if 0
1.1 noro 7034: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7035: {
7036: int i,j,k,len,pos,prev,nz;
7037: Z cs,mcs,c1,c2,cr,gcd,t;
7038: IndArray ivect;
7039: unsigned char *ivc;
7040: unsigned short *ivs;
7041: unsigned int *ivi;
7042: NDV redv;
7043: NMV mr;
7044: NODE rp;
7045: int maxrs;
7046: double hmag;
7047: Z *cvect;
1.3 noro 7048: int l;
1.1 noro 7049:
7050: maxrs = 0;
7051: for ( i = 0; i < col && !svect[i]; i++ );
7052: if ( i == col ) return maxrs;
7053: hmag = p_mag((P)svect[i])*nd_scale;
7054: cvect = (Z *)MALLOC(col*sizeof(Q));
7055: for ( i = 0; i < nred; i++ ) {
7056: ivect = imat[i];
7057: k = ivect->head;
7058: if ( svect[k] ) {
7059: maxrs = MAX(maxrs,rp0[i]->sugar);
7060: redv = nd_demand?ndv_load(rp0[i]->index)
7061: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7062: len = LEN(redv); mr = BDY(redv);
1.6 noro 7063: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7064: chsgnz(cs,&mcs);
7065: if ( !UNIQ(cr) ) {
7066: for ( j = 0; j < col; j++ ) {
7067: mulz(svect[j],cr,&c1); svect[j] = c1;
7068: }
7069: }
7070: svect[k] = 0; prev = k;
7071: switch ( ivect->width ) {
7072: case 1:
7073: ivc = ivect->index.c;
7074: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7075: pos = prev+ivc[j]; prev = pos;
1.6 noro 7076: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7077: }
7078: break;
7079: case 2:
7080: ivs = ivect->index.s;
7081: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7082: pos = prev+ivs[j]; prev = pos;
1.6 noro 7083: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7084: }
7085: break;
7086: case 4:
7087: ivi = ivect->index.i;
7088: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7089: pos = prev+ivi[j]; prev = pos;
1.6 noro 7090: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7091: }
7092: break;
7093: }
7094: for ( j = k+1; j < col && !svect[j]; j++ );
7095: if ( j == col ) break;
7096: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7097: nz = compress_array(svect,cvect,col);
7098: removecont_array((P *)cvect,nz,1);
7099: expand_array(svect,cvect,nz);
7100: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7101: }
7102: }
7103: }
7104: nz = compress_array(svect,cvect,col);
7105: removecont_array((P *)cvect,nz,1);
7106: expand_array(svect,cvect,nz);
7107: if ( DP_Print ) {
7108: fprintf(asir_out,"-"); fflush(asir_out);
7109: }
7110: return maxrs;
7111: }
1.4 noro 7112: #else
1.9 noro 7113:
1.4 noro 7114: /* direct mpz version */
7115: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7116: {
7117: int i,j,k,len,pos,prev;
7118: mpz_t cs,cr,gcd;
7119: IndArray ivect;
7120: unsigned char *ivc;
7121: unsigned short *ivs;
7122: unsigned int *ivi;
7123: NDV redv;
7124: NMV mr;
7125: NODE rp;
7126: int maxrs;
7127: double hmag;
7128: int l;
1.13 noro 7129: static mpz_t *svect;
7130: static int svect_len=0;
1.4 noro 7131:
7132: maxrs = 0;
7133: for ( i = 0; i < col && !svect0[i]; i++ );
7134: if ( i == col ) return maxrs;
7135: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7136: if ( col > svect_len ) {
7137: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7138: svect_len = col;
7139: }
1.4 noro 7140: for ( i = 0; i < col; i++ ) {
7141: mpz_init(svect[i]);
7142: if ( svect0[i] )
7143: mpz_set(svect[i],BDY(svect0[i]));
7144: else
7145: mpz_set_ui(svect[i],0);
7146: }
7147: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7148: for ( i = 0; i < nred; i++ ) {
7149: ivect = imat[i];
7150: k = ivect->head;
7151: if ( mpz_sgn(svect[k]) ) {
7152: maxrs = MAX(maxrs,rp0[i]->sugar);
7153: redv = nd_demand?ndv_load(rp0[i]->index)
7154: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7155: len = LEN(redv); mr = BDY(redv);
1.6 noro 7156: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7157: mpz_div(cs,svect[k],gcd);
1.6 noro 7158: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7159: mpz_neg(cs,cs);
1.9 noro 7160: if ( MUNIMPZ(cr) )
7161: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7162: else if ( !UNIMPZ(cr) )
7163: for ( j = 0; j < col; j++ ) {
7164: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7165: }
1.4 noro 7166: mpz_set_ui(svect[k],0);
7167: prev = k;
7168: switch ( ivect->width ) {
7169: case 1:
7170: ivc = ivect->index.c;
7171: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7172: pos = prev+ivc[j]; prev = pos;
1.6 noro 7173: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7174: }
7175: break;
7176: case 2:
7177: ivs = ivect->index.s;
7178: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7179: pos = prev+ivs[j]; prev = pos;
1.6 noro 7180: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7181: }
7182: break;
7183: case 4:
7184: ivi = ivect->index.i;
7185: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7186: pos = prev+ivi[j]; prev = pos;
1.6 noro 7187: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7188: }
7189: break;
7190: }
7191: for ( j = k+1; j < col && !svect[j]; j++ );
7192: if ( j == col ) break;
7193: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7194: mpz_removecont_array(svect,col);
7195: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7196: }
7197: }
7198: }
7199: mpz_removecont_array(svect,col);
7200: if ( DP_Print ) {
7201: fprintf(asir_out,"-"); fflush(asir_out);
7202: }
7203: for ( i = 0; i < col; i++ )
7204: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7205: else svect0[i] = 0;
7206: return maxrs;
7207: }
7208: #endif
1.1 noro 7209:
7210: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7211: {
7212: int i,j,k,len,pos,prev;
7213: UINT c,c1,c2,c3,up,lo,dmy;
7214: IndArray ivect;
7215: unsigned char *ivc;
7216: unsigned short *ivs;
7217: unsigned int *ivi;
7218: NDV redv;
7219: NMV mr;
7220: NODE rp;
7221: int maxrs;
7222:
7223: maxrs = 0;
7224: for ( i = 0; i < nred; i++ ) {
7225: ivect = imat[i];
7226: k = ivect->head; svect[k] %= m;
7227: if ( (c = svect[k]) != 0 ) {
7228: maxrs = MAX(maxrs,rp0[i]->sugar);
7229: c = m-c; redv = nd_ps[rp0[i]->index];
7230: len = LEN(redv); mr = BDY(redv);
7231: svect[k] = 0; prev = k;
7232: switch ( ivect->width ) {
7233: case 1:
7234: ivc = ivect->index.c;
7235: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7236: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
7237: if ( c1 ) {
7238: c2 = svect[pos];
7239: DMA(c1,c,c2,up,lo);
7240: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7241: } else svect[pos] = lo;
7242: }
7243: }
7244: break;
7245: case 2:
7246: ivs = ivect->index.s;
7247: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7248: pos = prev+ivs[j]; c1 = CM(mr);
7249: prev = pos;
7250: if ( c1 ) {
7251: c2 = svect[pos];
7252: DMA(c1,c,c2,up,lo);
7253: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7254: } else svect[pos] = lo;
7255: }
7256: }
7257: break;
7258: case 4:
7259: ivi = ivect->index.i;
7260: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7261: pos = prev+ivi[j]; c1 = CM(mr);
7262: prev = pos;
7263: if ( c1 ) {
7264: c2 = svect[pos];
7265: DMA(c1,c,c2,up,lo);
7266: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7267: } else svect[pos] = lo;
7268: }
7269: }
7270: break;
7271: }
7272: }
7273: }
7274: for ( i = 0; i < col; i++ )
7275: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7276: return maxrs;
7277: }
7278:
7279: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7280: {
7281: int i,j,k,len,pos,prev;
7282: UINT c,c1,c2,c3,up,lo,dmy;
7283: IndArray ivect;
7284: unsigned char *ivc;
7285: unsigned short *ivs;
7286: unsigned int *ivi;
7287: NDV redv;
7288: NMV mr;
7289: NODE rp;
7290: int maxrs;
7291:
7292: maxrs = 0;
7293: for ( i = 0; i < nred; i++ ) {
7294: ivect = imat[i];
7295: k = ivect->head;
7296: if ( (c = svect[k]) != 0 ) {
7297: maxrs = MAX(maxrs,rp0[i]->sugar);
7298: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7299: len = LEN(redv); mr = BDY(redv);
7300: svect[k] = 0; prev = k;
7301: switch ( ivect->width ) {
7302: case 1:
7303: ivc = ivect->index.c;
7304: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7305: pos = prev+ivc[j]; prev = pos;
7306: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7307: }
7308: break;
7309: case 2:
7310: ivs = ivect->index.s;
7311: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7312: pos = prev+ivs[j]; prev = pos;
7313: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7314: }
7315: break;
7316: case 4:
7317: ivi = ivect->index.i;
7318: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7319: pos = prev+ivi[j]; prev = pos;
7320: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7321: }
7322: break;
7323: }
7324: }
7325: }
7326: return maxrs;
7327: }
7328:
7329: ND nd_add_lf(ND p1,ND p2)
7330: {
7331: int n,c,can;
7332: ND r;
7333: NM m1,m2,mr0,mr,s;
7334: Z t;
7335:
7336: if ( !p1 ) return p2;
7337: else if ( !p2 ) return p1;
7338: else {
7339: can = 0;
7340: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7341: c = DL_COMPARE(DL(m1),DL(m2));
7342: switch ( c ) {
7343: case 0:
7344: addlf(CZ(m1),CZ(m2),&t);
7345: s = m1; m1 = NEXT(m1);
7346: if ( t ) {
7347: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7348: } else {
7349: can += 2; FREENM(s);
7350: }
7351: s = m2; m2 = NEXT(m2); FREENM(s);
7352: break;
7353: case 1:
7354: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7355: break;
7356: case -1:
7357: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7358: break;
7359: }
7360: }
7361: if ( !mr0 )
7362: if ( m1 ) mr0 = m1;
7363: else if ( m2 ) mr0 = m2;
7364: else return 0;
7365: else if ( m1 ) NEXT(mr) = m1;
7366: else if ( m2 ) NEXT(mr) = m2;
7367: else NEXT(mr) = 0;
7368: BDY(p1) = mr0;
7369: SG(p1) = MAX(SG(p1),SG(p2));
7370: LEN(p1) = LEN(p1)+LEN(p2)-can;
7371: FREEND(p2);
7372: return p1;
7373: }
7374: }
7375:
7376: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7377: {
7378: int i,j,k,len,pos,prev;
7379: mpz_t c,mc,c1;
7380: IndArray ivect;
7381: unsigned char *ivc;
7382: unsigned short *ivs;
7383: unsigned int *ivi;
7384: NDV redv;
7385: NMV mr;
7386: NODE rp;
7387: int maxrs;
7388:
7389: maxrs = 0;
7390: lf_lazy = 1;
7391: for ( i = 0; i < nred; i++ ) {
7392: ivect = imat[i];
7393: k = ivect->head;
7394: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7395: if ( mpz_sgn(svect[k]) ) {
7396: maxrs = MAX(maxrs,rp0[i]->sugar);
7397: mpz_neg(svect[k],svect[k]);
7398: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7399: len = LEN(redv); mr = BDY(redv);
7400: prev = k;
7401: switch ( ivect->width ) {
7402: case 1:
7403: ivc = ivect->index.c;
7404: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7405: pos = prev+ivc[j]; prev = pos;
7406: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7407: }
7408: break;
7409: case 2:
7410: ivs = ivect->index.s;
7411: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7412: pos = prev+ivs[j]; prev = pos;
7413: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7414: }
7415: break;
7416: case 4:
7417: ivi = ivect->index.i;
7418: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7419: pos = prev+ivi[j]; prev = pos;
7420: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7421: }
7422: break;
7423: }
7424: mpz_set_ui(svect[k],0);
7425: }
7426: }
7427: lf_lazy=0;
7428: for ( i = 0; i < col; i++ ) {
7429: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7430: }
7431: return maxrs;
7432: }
7433:
7434: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7435: {
7436: int i,j,k,l,rank,s;
7437: mpz_t a,a1,inv;
7438: mpz_t *t,*pivot,*pk;
7439: mpz_t **mat;
7440: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7441: int size,size1;
7442:
7443: mpz_init(inv);
7444: mpz_init(a);
7445: mat = (mpz_t **)mat0;
7446: size = 0;
7447: for ( rank = 0, j = 0; j < col; j++ ) {
7448: for ( i = rank; i < row; i++ ) {
7449: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7450: }
7451: for ( i = rank; i < row; i++ )
7452: if ( mpz_sgn(mat[i][j]) )
7453: break;
7454: if ( i == row ) {
7455: colstat[j] = 0;
7456: continue;
7457: } else
7458: colstat[j] = 1;
7459: if ( i != rank ) {
7460: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7461: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7462: }
7463: pivot = mat[rank];
7464: s = sugar[rank];
7465: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7466: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7467: if ( mpz_sgn(*pk) ) {
7468: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7469: }
7470: for ( i = rank+1; i < row; i++ ) {
7471: t = mat[i];
7472: if ( mpz_sgn(t[j]) ) {
7473: sugar[i] = MAX(sugar[i],s);
7474: mpz_neg(a,t[j]);
7475: red_by_vect_lf(t+j,pivot+j,a,col-j);
7476: }
7477: }
7478: rank++;
7479: }
7480: for ( j = col-1, l = rank-1; j >= 0; j-- )
7481: if ( colstat[j] ) {
7482: pivot = mat[l];
7483: s = sugar[l];
7484: for ( k = j; k < col; k++ )
7485: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7486: for ( i = 0; i < l; i++ ) {
7487: t = mat[i];
7488: if ( mpz_sgn(t[j]) ) {
7489: sugar[i] = MAX(sugar[i],s);
7490: mpz_neg(a,t[j]);
7491: red_by_vect_lf(t+j,pivot+j,a,col-j);
7492: }
7493: }
7494: l--;
7495: }
7496: for ( j = 0, l = 0; l < rank; j++ )
7497: if ( colstat[j] ) {
7498: t = mat[l];
7499: for ( k = j; k < col; k++ ) {
7500: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7501: }
7502: l++;
7503: }
7504: return rank;
7505: }
7506:
7507:
7508: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7509: {
7510: int j,k,len;
7511: UINT *p;
7512: UINT c;
7513: NDV r;
7514: NMV mr0,mr;
7515:
7516: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7517: if ( !len ) return 0;
7518: else {
7519: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7520: #if 0
7521: ndv_alloc += nmv_adv*len;
7522: #endif
7523: mr = mr0;
7524: p = s0vect;
7525: for ( j = k = 0; j < col; j++, p += nd_wpd )
7526: if ( !rhead[j] ) {
7527: if ( (c = vect[k++]) != 0 ) {
7528: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7529: }
7530: }
7531: MKNDV(nd_nvar,mr0,len,r);
7532: return r;
7533: }
7534: }
7535:
7536: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7537: {
7538: int j,k,len;
7539: UINT *p;
7540: NDV r;
7541: NMV mr0,mr;
7542:
7543: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7544: if ( !len ) return 0;
7545: else {
7546: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7547: mr = mr0;
7548: p = s0vect;
7549: for ( j = 0; j < col; j++, p += nd_wpd )
7550: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7551: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7552: }
7553: MKNDV(nd_nvar,mr0,len,r);
7554: return r;
7555: }
7556: }
7557:
7558: /* for preprocessed vector */
7559:
7560: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7561: {
1.6 noro 7562: int j,k,len;
7563: UINT *p;
7564: Z c;
7565: NDV r;
7566: NMV mr0,mr;
1.1 noro 7567:
1.6 noro 7568: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7569: if ( !len ) return 0;
7570: else {
7571: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7572: #if 0
1.6 noro 7573: ndv_alloc += nmv_adv*len;
1.1 noro 7574: #endif
1.6 noro 7575: mr = mr0;
7576: p = s0vect;
7577: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7578: if ( !rhead[j] ) {
7579: if ( (c = vect[k++]) != 0 ) {
7580: if ( !INT(c) )
7581: error("vect_to_ndv_q : components must be integers");
7582: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7583: }
7584: }
1.1 noro 7585: }
1.6 noro 7586: MKNDV(nd_nvar,mr0,len,r);
7587: return r;
7588: }
1.1 noro 7589: }
7590:
7591: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
7592: {
7593: int j,k,len;
7594: UINT *p;
7595: mpz_t c;
7596: NDV r;
7597: NMV mr0,mr;
7598:
7599: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
7600: if ( !len ) return 0;
7601: else {
7602: mr0 = (NMV)MALLOC(nmv_adv*len);
7603: #if 0
7604: ndv_alloc += nmv_adv*len;
7605: #endif
7606: mr = mr0;
7607: p = s0vect;
7608: for ( j = k = 0; j < col; j++, p += nd_wpd )
7609: if ( !rhead[j] ) {
7610: c[0] = vect[k++][0];
7611: if ( mpz_sgn(c) ) {
7612: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
7613: }
7614: }
7615: MKNDV(nd_nvar,mr0,len,r);
7616: return r;
7617: }
7618: }
7619:
7620: /* for plain vector */
7621:
7622: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
7623: {
7624: int j,k,len;
7625: UINT *p;
7626: Z c;
7627: NDV r;
7628: NMV mr0,mr;
7629:
7630: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7631: if ( !len ) return 0;
7632: else {
7633: mr0 = (NMV)MALLOC(nmv_adv*len);
7634: #if 0
7635: ndv_alloc += nmv_adv*len;
7636: #endif
7637: mr = mr0;
7638: p = s0vect;
7639: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
7640: if ( (c = vect[k]) != 0 ) {
7641: if ( !INT(c) )
1.6 noro 7642: error("plain_vect_to_ndv_q : components must be integers");
7643: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 7644: }
7645: MKNDV(nd_nvar,mr0,len,r);
7646: return r;
7647: }
7648: }
7649:
7650: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
7651: {
7652: ND_pairs t;
7653: NODE sp0,sp;
7654: int stat;
7655: ND spol;
7656:
7657: for ( t = l; t; t = NEXT(t) ) {
7658: stat = nd_sp(m,trace,t,&spol);
7659: if ( !stat ) return 0;
7660: if ( spol ) {
7661: add_pbucket_symbolic(bucket,spol);
7662: }
7663: }
7664: return 1;
7665: }
7666:
7667: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
7668: {
7669: NODE rp0,rp;
7670: NM mul,head,s0,s;
7671: int index,col,i,sugar;
7672: RHist h;
7673: UINT *s0v,*p;
7674: NM_ind_pair pair;
7675: ND red;
7676: NDV *ps;
7677:
7678: s0 = 0; rp0 = 0; col = 0;
7679: if ( nd_demand )
7680: ps = trace?nd_ps_trace_sym:nd_ps_sym;
7681: else
7682: ps = trace?nd_ps_trace:nd_ps;
7683: while ( 1 ) {
7684: head = remove_head_pbucket_symbolic(bucket);
7685: if ( !head ) break;
7686: if ( !s0 ) s0 = head;
7687: else NEXT(s) = head;
7688: s = head;
7689: index = ndl_find_reducer(DL(head));
7690: if ( index >= 0 ) {
7691: h = nd_psh[index];
7692: NEWNM(mul);
7693: ndl_sub(DL(head),DL(h),DL(mul));
7694: if ( ndl_check_bound2(index,DL(mul)) )
7695: return 0;
7696: sugar = TD(DL(mul))+SG(ps[index]);
7697: MKNM_ind_pair(pair,mul,index,sugar);
7698: red = ndv_mul_nm_symbolic(mul,ps[index]);
7699: add_pbucket_symbolic(bucket,nd_remove_head(red));
7700: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
7701: }
7702: col++;
7703: }
7704: if ( rp0 ) NEXT(rp) = 0;
7705: NEXT(s) = 0;
7706: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
7707: for ( i = 0, p = s0v, s = s0; i < col;
7708: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
7709: *s0vect = s0v;
7710: *r = rp0;
7711: return col;
7712: }
7713:
7714: void print_ndp(ND_pairs l)
7715: {
7716: ND_pairs t;
7717:
7718: for ( t = l; t; t = NEXT(t) )
7719: printf("[%d,%d] ",t->i1,t->i2);
7720: printf("\n");
7721: }
7722:
7723: NODE nd_f4(int m,int checkonly,int **indp)
7724: {
7725: int i,nh,stat,index,f4red;
7726: NODE r,g,tn0,tn,node;
7727: ND_pairs d,l,t,ll0,ll,lh;
7728: LIST l0,l1;
7729: ND spol,red;
7730: NDV nf,redv;
7731: NM s0,s;
7732: NODE rp0,srp0,nflist,nzlist,nzlist_t;
7733: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
7734: UINT c;
7735: UINT **spmat;
7736: UINT *s0vect,*svect,*p,*v;
7737: int *colstat;
7738: IndArray *imat;
7739: int *rhead;
7740: int spcol,sprow;
7741: int sugar,sugarh;
7742: PGeoBucket bucket;
7743: struct oEGT eg0,eg1,eg_f4;
7744: Z i1,i2,sugarq;
1.12 noro 7745:
7746: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 7747: #if 0
7748: ndv_alloc = 0;
7749: #endif
1.11 noro 7750: Nf4_red=0;
1.1 noro 7751: g = 0; d = 0;
7752: for ( i = 0; i < nd_psn; i++ ) {
7753: d = update_pairs(d,g,i,0);
7754: g = update_base(g,i);
7755: }
7756: nzlist = 0;
7757: nzlist_t = nd_nzlist;
7758: f4red = 1;
7759: nd_last_nonzero = 0;
7760: while ( d ) {
7761: get_eg(&eg0);
7762: l = nd_minsugarp(d,&d);
7763: sugar = nd_sugarweight?l->sugar2:SG(l);
7764: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7765: if ( nzlist_t ) {
7766: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 7767: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 7768: tn = BDY((LIST)ARG1(node));
7769: if ( !tn ) {
7770: nzlist_t = NEXT(nzlist_t);
7771: continue;
7772: }
7773: /* tn = [[i1,i2],...] */
7774: lh = nd_ipairtospair(tn);
7775: }
7776: bucket = create_pbucket();
7777: stat = nd_sp_f4(m,0,l,bucket);
7778: if ( !stat ) {
7779: for ( t = l; NEXT(t); t = NEXT(t) );
7780: NEXT(t) = d; d = l;
7781: d = nd_reconstruct(0,d);
7782: continue;
7783: }
7784: if ( bucket->m < 0 ) continue;
7785: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7786: if ( !col ) {
7787: for ( t = l; NEXT(t); t = NEXT(t) );
7788: NEXT(t) = d; d = l;
7789: d = nd_reconstruct(0,d);
7790: continue;
7791: }
1.12 noro 7792: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 7793: if ( DP_Print )
1.6 noro 7794: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 7795: sugar,eg_f4.exectime);
1.1 noro 7796: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
7797: if ( checkonly && nflist ) return 0;
7798: /* adding new bases */
7799: if ( nflist ) nd_last_nonzero = f4red;
7800: for ( r = nflist; r; r = NEXT(r) ) {
7801: nf = (NDV)BDY(r);
7802: ndv_removecont(m,nf);
7803: if ( !m && nd_nalg ) {
7804: ND nf1;
7805:
7806: nf1 = ndvtond(m,nf);
7807: nd_monic(0,&nf1);
7808: nd_removecont(m,nf1);
7809: nf = ndtondv(m,nf1);
7810: }
1.24 noro 7811: nh = ndv_newps(m,nf,0);
1.1 noro 7812: d = update_pairs(d,g,nh,0);
7813: g = update_base(g,nh);
7814: }
7815: if ( DP_Print ) {
7816: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
7817: }
7818: if ( nd_gentrace ) {
7819: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
7820: NEXTNODE(tn0,tn);
1.6 noro 7821: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 7822: node = mknode(2,i1,i2); MKLIST(l0,node);
7823: BDY(tn) = l0;
7824: }
7825: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 7826: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 7827: MKNODE(node,l1,nzlist); nzlist = node;
7828: }
7829: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
7830: f4red++;
7831: if ( nd_f4red && f4red > nd_f4red ) break;
7832: if ( nd_rank0 && !nflist ) break;
7833: }
7834: if ( nd_gentrace ) {
7835: MKLIST(l0,reverse_node(nzlist));
7836: MKNODE(nd_alltracelist,l0,0);
7837: }
7838: #if 0
7839: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7840: #endif
1.12 noro 7841: if ( DP_Print ) {
7842: fprintf(asir_out,"number of red=%d,",Nf4_red);
7843: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
7844: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
7845: }
1.1 noro 7846: conv_ilist(nd_demand,0,g,indp);
7847: return g;
7848: }
7849:
7850: NODE nd_f4_trace(int m,int **indp)
7851: {
7852: int i,nh,stat,index;
7853: NODE r,g;
7854: ND_pairs d,l,l0,t;
7855: ND spol,red;
7856: NDV nf,redv,nfqv,nfv;
7857: NM s0,s;
7858: NODE rp0,srp0,nflist;
7859: int nsp,nred,col,rank,len,k,j,a;
7860: UINT c;
7861: UINT **spmat;
7862: UINT *s0vect,*svect,*p,*v;
7863: int *colstat;
7864: IndArray *imat;
7865: int *rhead;
7866: int spcol,sprow;
7867: int sugar;
7868: PGeoBucket bucket;
7869: struct oEGT eg0,eg1,eg_f4;
7870:
7871: g = 0; d = 0;
7872: for ( i = 0; i < nd_psn; i++ ) {
7873: d = update_pairs(d,g,i,0);
7874: g = update_base(g,i);
7875: }
7876: while ( d ) {
7877: get_eg(&eg0);
7878: l = nd_minsugarp(d,&d);
7879: sugar = SG(l);
7880: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7881: bucket = create_pbucket();
7882: stat = nd_sp_f4(m,0,l,bucket);
7883: if ( !stat ) {
7884: for ( t = l; NEXT(t); t = NEXT(t) );
7885: NEXT(t) = d; d = l;
7886: d = nd_reconstruct(1,d);
7887: continue;
7888: }
7889: if ( bucket->m < 0 ) continue;
7890: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7891: if ( !col ) {
7892: for ( t = l; NEXT(t); t = NEXT(t) );
7893: NEXT(t) = d; d = l;
7894: d = nd_reconstruct(1,d);
7895: continue;
7896: }
7897: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
7898: if ( DP_Print )
1.3 noro 7899: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 7900: sugar,eg_f4.exectime);
1.1 noro 7901: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
7902: if ( !l0 ) continue;
7903: l = l0;
7904:
7905: /* over Q */
7906: bucket = create_pbucket();
7907: stat = nd_sp_f4(0,1,l,bucket);
7908: if ( !stat ) {
7909: for ( t = l; NEXT(t); t = NEXT(t) );
7910: NEXT(t) = d; d = l;
7911: d = nd_reconstruct(1,d);
7912: continue;
7913: }
7914: if ( bucket->m < 0 ) continue;
7915: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7916: if ( !col ) {
7917: for ( t = l; NEXT(t); t = NEXT(t) );
7918: NEXT(t) = d; d = l;
7919: d = nd_reconstruct(1,d);
7920: continue;
7921: }
7922: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7923: /* adding new bases */
7924: for ( r = nflist; r; r = NEXT(r) ) {
7925: nfqv = (NDV)BDY(r);
7926: ndv_removecont(0,nfqv);
1.6 noro 7927: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 7928: if ( nd_nalg ) {
7929: ND nf1;
7930:
7931: nf1 = ndvtond(m,nfqv);
7932: nd_monic(0,&nf1);
7933: nd_removecont(0,nf1);
7934: nfqv = ndtondv(0,nf1); nd_free(nf1);
7935: }
7936: nfv = ndv_dup(0,nfqv);
7937: ndv_mod(m,nfv);
7938: ndv_removecont(m,nfv);
1.24 noro 7939: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 7940: d = update_pairs(d,g,nh,0);
7941: g = update_base(g,nh);
7942: }
7943: }
7944: #if 0
7945: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7946: #endif
7947: conv_ilist(nd_demand,1,g,indp);
7948: return g;
7949: }
7950:
7951: int rref(matrix mat,int *sugar)
7952: {
7953: int row,col,i,j,k,l,s,wcol,wj;
7954: unsigned long bj;
7955: unsigned long **a;
7956: unsigned long *ai,*ak,*as,*t;
7957: int *pivot;
7958:
7959: row = mat->row;
7960: col = mat->col;
7961: a = mat->a;
7962: wcol = (col+BLEN-1)/BLEN;
7963: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7964: i = 0;
7965: for ( j = 0; j < col; j++ ) {
7966: wj = j/BLEN; bj = 1L<<(j%BLEN);
7967: for ( k = i; k < row; k++ )
7968: if ( a[k][wj] & bj ) break;
7969: if ( k == row ) continue;
7970: pivot[i] = j;
7971: if ( k != i ) {
7972: t = a[i]; a[i] = a[k]; a[k] = t;
7973: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7974: }
7975: ai = a[i];
7976: for ( k = i+1; k < row; k++ ) {
7977: ak = a[k];
7978: if ( ak[wj] & bj ) {
7979: for ( l = wj; l < wcol; l++ )
7980: ak[l] ^= ai[l];
7981: sugar[k] = MAX(sugar[k],sugar[i]);
7982: }
7983: }
7984: i++;
7985: }
7986: for ( k = i-1; k >= 0; k-- ) {
7987: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
7988: ak = a[k];
7989: for ( s = 0; s < k; s++ ) {
7990: as = a[s];
7991: if ( as[wj] & bj ) {
7992: for ( l = wj; l < wcol; l++ )
7993: as[l] ^= ak[l];
7994: sugar[s] = MAX(sugar[s],sugar[k]);
7995: }
7996: }
7997: }
7998: return i;
7999: }
8000:
8001: void print_matrix(matrix mat)
8002: {
8003: int row,col,i,j;
8004: unsigned long *ai;
8005:
8006: row = mat->row;
8007: col = mat->col;
8008: printf("%d x %d\n",row,col);
8009: for ( i = 0; i < row; i++ ) {
8010: ai = mat->a[i];
8011: for ( j = 0; j < col; j++ ) {
8012: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8013: else putchar('0');
8014: }
8015: putchar('\n');
8016: }
8017: }
8018:
8019: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8020:
8021: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8022: {
8023: int row,col,wcol,wj,i,j;
8024: unsigned long bj;
8025: unsigned long *ai;
8026: unsigned long **a;
8027: int len;
8028: int *pos;
8029:
8030: row = mat->row;
8031: col = mat->col;
8032: wcol = (col+BLEN-1)/BLEN;
8033: pos = (int *)MALLOC(wcol*sizeof(int));
8034: bzero(pos,wcol*sizeof(int));
8035: for ( i = j = 0; i < wcol; i++ )
8036: if ( v[i] ) pos[j++] = i;;
8037: len = j;
8038: wj = rhead/BLEN;
8039: bj = 1L<<rhead%BLEN;
8040: a = mat->a;
8041: for ( i = 0; i < row; i++ ) {
8042: ai = a[i];
8043: if ( ai[wj]&bj ) {
8044: for ( j = 0; j < len; j++ )
8045: ai[pos[j]] ^= v[pos[j]];
8046: sugar[i] = MAX(sugar[i],rsugar);
8047: }
8048: }
8049: }
8050:
8051: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8052: {
8053: int nsp,nred,i,i0,k,rank,row;
8054: NODE r0,rp;
8055: ND_pairs sp;
8056: ND spol;
8057: NM_ind_pair rt;
8058: int *s0hash;
8059: UINT *s;
8060: int *pivot,*sugar,*head;
8061: matrix mat;
8062: NM m;
8063: NODE r;
8064: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8065: int rhead,rsugar,size;
8066: unsigned long *v;
8067:
8068: get_eg(&eg0);
8069: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8070: nred = length(rp0);
8071: mat = alloc_matrix(nsp,col);
8072: s0hash = (int *)MALLOC(col*sizeof(int));
8073: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8074: s0hash[i] = ndl_hash_value(s);
8075:
8076: sugar = (int *)MALLOC(nsp*sizeof(int));
8077: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8078: nd_sp(2,0,sp,&spol);
8079: if ( spol ) {
8080: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8081: sugar[i] = SG(spol);
8082: i++;
8083: }
8084: }
8085: mat->row = i;
8086: if ( DP_Print ) {
8087: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8088: }
8089: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8090: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8091: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8092: rt = (NM_ind_pair)BDY(rp);
8093: bzero(v,size);
8094: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8095: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8096: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8097: }
8098:
8099: get_eg(&eg1);
8100: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8101: rank = rref(mat,sugar);
8102:
8103: for ( i = 0, r0 = 0; i < rank; i++ ) {
8104: NEXTNODE(r0,r);
8105: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8106: SG((NDV)BDY(r)) = sugar[i];
8107: }
8108: if ( r0 ) NEXT(r) = 0;
8109: get_eg(&eg2);
8110: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8111: if ( DP_Print ) {
8112: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8113: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8114: fflush(asir_out);
8115: }
8116: return r0;
8117: }
8118:
8119:
8120: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8121: {
8122: IndArray *imat;
1.11 noro 8123: int nsp,nred,i,start;
1.1 noro 8124: int *rhead;
8125: NODE r0,rp;
8126: ND_pairs sp;
8127: NM_ind_pair *rvect;
8128: UINT *s;
8129: int *s0hash;
1.11 noro 8130: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8131:
8132: if ( m == 2 && nd_rref2 )
8133: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8134:
8135: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8136: nred = length(rp0);
8137: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8138: rhead = (int *)MALLOC(col*sizeof(int));
8139: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8140:
8141: /* construction of index arrays */
1.11 noro 8142: get_eg(&eg0);
1.1 noro 8143: if ( DP_Print ) {
1.11 noro 8144: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8145: fflush(asir_out);
1.1 noro 8146: }
8147: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8148: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8149: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8150: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8151: rhead[imat[i]->head] = 1;
1.11 noro 8152: start = imat[i]->head;
8153: }
1.12 noro 8154: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8155: if ( DP_Print ) {
8156: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8157: fflush(asir_out);
1.1 noro 8158: }
8159: if ( m > 0 )
1.7 noro 8160: #if SIZEOF_LONG==8
1.1 noro 8161: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8162: #else
8163: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8164: #endif
8165: else if ( m == -1 )
8166: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8167: else if ( m == -2 )
8168: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8169: else
8170: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8171: return r0;
8172: }
8173:
8174: /* for Fp, 2<=p<2^16 */
8175:
8176: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8177: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8178: {
8179: int spcol,sprow,a;
8180: int i,j,k,l,rank;
8181: NODE r0,r;
8182: ND_pairs sp;
8183: ND spol;
8184: UINT **spmat;
8185: UINT *svect,*v;
8186: int *colstat;
8187: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8188: int maxrs;
8189: int *spsugar;
8190: ND_pairs *spactive;
8191:
8192: spcol = col-nred;
8193: get_eg(&eg0);
8194: /* elimination (1st step) */
8195: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8196: svect = (UINT *)MALLOC(col*sizeof(UINT));
8197: spsugar = (int *)MALLOC(nsp*sizeof(int));
8198: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8199: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8200: nd_sp(m,0,sp,&spol);
8201: if ( !spol ) continue;
8202: nd_to_vect(m,s0vect,col,spol,svect);
8203: if ( m == -1 )
8204: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8205: else
8206: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
8207: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8208: if ( i < col ) {
8209: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8210: for ( j = k = 0; j < col; j++ )
8211: if ( !rhead[j] ) v[k++] = svect[j];
8212: spsugar[sprow] = MAX(maxrs,SG(spol));
8213: if ( nz )
8214: spactive[sprow] = sp;
8215: sprow++;
8216: }
8217: nd_free(spol);
8218: }
8219: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8220: if ( DP_Print ) {
1.5 noro 8221: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8222: fflush(asir_out);
8223: }
8224: /* free index arrays */
8225: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8226:
8227: /* elimination (2nd step) */
8228: colstat = (int *)MALLOC(spcol*sizeof(int));
8229: if ( m == -1 )
8230: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8231: else
8232: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8233: r0 = 0;
8234: for ( i = 0; i < rank; i++ ) {
8235: NEXTNODE(r0,r); BDY(r) =
8236: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8237: SG((NDV)BDY(r)) = spsugar[i];
8238: GCFREE(spmat[i]);
8239: }
8240: if ( r0 ) NEXT(r) = 0;
8241:
8242: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8243: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8244: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8245: if ( DP_Print ) {
1.5 noro 8246: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8247: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8248: nsp,nred,sprow,spcol,rank);
1.5 noro 8249: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8250: }
8251: if ( nz ) {
8252: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8253: if ( rank > 0 ) {
8254: NEXT(spactive[rank-1]) = 0;
8255: *nz = spactive[0];
8256: } else
8257: *nz = 0;
8258: }
8259: return r0;
8260: }
8261:
8262:
8263: /* for small finite fields */
8264:
8265: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8266: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8267: {
8268: int spcol,sprow,a;
8269: int i,j,k,l,rank;
8270: NODE r0,r;
8271: ND_pairs sp;
8272: ND spol;
8273: UINT **spmat;
8274: UINT *svect,*v;
8275: int *colstat;
8276: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8277: int maxrs;
8278: int *spsugar;
8279: ND_pairs *spactive;
8280:
8281: spcol = col-nred;
8282: get_eg(&eg0);
8283: /* elimination (1st step) */
8284: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8285: svect = (UINT *)MALLOC(col*sizeof(UINT));
8286: spsugar = (int *)MALLOC(nsp*sizeof(int));
8287: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8288: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8289: nd_sp(m,0,sp,&spol);
8290: if ( !spol ) continue;
8291: nd_to_vect(m,s0vect,col,spol,svect);
8292: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8293: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8294: if ( i < col ) {
8295: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8296: for ( j = k = 0; j < col; j++ )
8297: if ( !rhead[j] ) v[k++] = svect[j];
8298: spsugar[sprow] = MAX(maxrs,SG(spol));
8299: if ( nz )
8300: spactive[sprow] = sp;
8301: sprow++;
8302: }
8303: nd_free(spol);
8304: }
8305: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8306: if ( DP_Print ) {
1.5 noro 8307: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8308: fflush(asir_out);
8309: }
8310: /* free index arrays */
8311: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8312:
8313: /* elimination (2nd step) */
8314: colstat = (int *)MALLOC(spcol*sizeof(int));
8315: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8316: r0 = 0;
8317: for ( i = 0; i < rank; i++ ) {
8318: NEXTNODE(r0,r); BDY(r) =
8319: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8320: SG((NDV)BDY(r)) = spsugar[i];
8321: GCFREE(spmat[i]);
8322: }
8323: if ( r0 ) NEXT(r) = 0;
8324:
8325: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8326: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8327: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8328: if ( DP_Print ) {
1.5 noro 8329: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8330: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8331: nsp,nred,sprow,spcol,rank);
1.5 noro 8332: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8333: }
8334: if ( nz ) {
8335: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8336: if ( rank > 0 ) {
8337: NEXT(spactive[rank-1]) = 0;
8338: *nz = spactive[0];
8339: } else
8340: *nz = 0;
8341: }
8342: return r0;
8343: }
8344:
8345: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8346: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8347: {
8348: int spcol,sprow,a;
8349: int i,j,k,l,rank;
8350: NODE r0,r;
8351: ND_pairs sp;
8352: ND spol;
8353: mpz_t **spmat;
8354: mpz_t *svect,*v;
8355: int *colstat;
8356: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8357: int maxrs;
8358: int *spsugar;
8359: pointer *w;
8360:
8361: spcol = col-nred;
8362: get_eg(&eg0);
8363: /* elimination (1st step) */
8364: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8365: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8366: spsugar = (int *)MALLOC(nsp*sizeof(int));
8367: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8368: nd_sp(m,trace,sp,&spol);
8369: if ( !spol ) continue;
8370: nd_to_vect_lf(s0vect,col,spol,svect);
8371: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8372: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8373: if ( i < col ) {
8374: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8375: for ( j = k = 0; j < col; j++ )
8376: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8377: spsugar[sprow] = MAX(maxrs,SG(spol));
8378: sprow++;
8379: }
8380: /* nd_free(spol); */
8381: }
8382: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8383: if ( DP_Print ) {
1.5 noro 8384: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8385: fflush(asir_out);
8386: }
8387: /* free index arrays */
8388: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8389:
8390: /* elimination (2nd step) */
8391: colstat = (int *)MALLOC(spcol*sizeof(int));
8392: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8393: w = (pointer *)MALLOC(rank*sizeof(pointer));
8394: for ( i = 0; i < rank; i++ ) {
8395: #if 0
8396: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8397: SG((NDV)w[rank-i-1]) = spsugar[i];
8398: #else
8399: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8400: SG((NDV)w[i]) = spsugar[i];
8401: #endif
8402: /* GCFREE(spmat[i]); */
8403:
8404: }
8405: #if 0
8406: qsort(w,rank,sizeof(NDV),
8407: (int (*)(const void *,const void *))ndv_compare);
8408: #endif
8409: r0 = 0;
8410: for ( i = 0; i < rank; i++ ) {
8411: NEXTNODE(r0,r); BDY(r) = w[i];
8412: }
8413: if ( r0 ) NEXT(r) = 0;
8414:
8415: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8416: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8417: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8418: if ( DP_Print ) {
1.5 noro 8419: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8420: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8421: nsp,nred,sprow,spcol,rank);
1.5 noro 8422: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8423: }
8424: return r0;
8425: }
8426:
8427: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8428: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8429: {
8430: int spcol,sprow,a;
8431: int i,j,k,l,rank;
8432: NODE r0,r;
8433: ND_pairs sp;
8434: ND spol;
8435: Z **spmat;
8436: Z *svect,*v;
8437: int *colstat;
8438: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8439: int maxrs;
8440: int *spsugar;
8441: pointer *w;
8442:
8443: spcol = col-nred;
8444: get_eg(&eg0);
8445: /* elimination (1st step) */
8446: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8447: svect = (Z *)MALLOC(col*sizeof(Q));
8448: spsugar = (int *)MALLOC(nsp*sizeof(int));
8449: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8450: nd_sp(0,trace,sp,&spol);
8451: if ( !spol ) continue;
8452: nd_to_vect_q(s0vect,col,spol,svect);
8453: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8454: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8455: if ( i < col ) {
8456: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8457: for ( j = k = 0; j < col; j++ )
8458: if ( !rhead[j] ) v[k++] = svect[j];
8459: spsugar[sprow] = MAX(maxrs,SG(spol));
8460: sprow++;
8461: }
8462: /* nd_free(spol); */
8463: }
8464: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8465: if ( DP_Print ) {
1.5 noro 8466: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8467: fflush(asir_out);
8468: }
8469: /* free index arrays */
8470: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8471:
8472: /* elimination (2nd step) */
8473: colstat = (int *)MALLOC(spcol*sizeof(int));
8474: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8475: w = (pointer *)MALLOC(rank*sizeof(pointer));
8476: for ( i = 0; i < rank; i++ ) {
8477: #if 0
8478: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8479: SG((NDV)w[rank-i-1]) = spsugar[i];
8480: #else
8481: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8482: SG((NDV)w[i]) = spsugar[i];
8483: #endif
8484: /* GCFREE(spmat[i]); */
8485: }
8486: #if 0
8487: qsort(w,rank,sizeof(NDV),
8488: (int (*)(const void *,const void *))ndv_compare);
8489: #endif
8490: r0 = 0;
8491: for ( i = 0; i < rank; i++ ) {
8492: NEXTNODE(r0,r); BDY(r) = w[i];
8493: }
8494: if ( r0 ) NEXT(r) = 0;
8495:
8496: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8497: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8498: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8499: if ( DP_Print ) {
1.5 noro 8500: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8501: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8502: nsp,nred,sprow,spcol,rank);
1.5 noro 8503: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8504: }
8505: return r0;
8506: }
8507:
8508: FILE *nd_write,*nd_read;
8509:
8510: void nd_send_int(int a) {
8511: write_int(nd_write,(unsigned int *)&a);
8512: }
8513:
8514: void nd_send_intarray(int *p,int len) {
8515: write_intarray(nd_write,(unsigned int *)p,len);
8516: }
8517:
8518: int nd_recv_int() {
8519: int a;
8520:
8521: read_int(nd_read,(unsigned int *)&a);
8522: return a;
8523: }
8524:
8525: void nd_recv_intarray(int *p,int len) {
8526: read_intarray(nd_read,(unsigned int *)p,len);
8527: }
8528:
8529: void nd_send_ndv(NDV p) {
8530: int len,i;
8531: NMV m;
8532:
8533: if ( !p ) nd_send_int(0);
8534: else {
8535: len = LEN(p);
8536: nd_send_int(len);
8537: m = BDY(p);
8538: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8539: nd_send_int(CM(m));
8540: nd_send_intarray((int *)DL(m),nd_wpd);
8541: }
8542: }
8543: }
8544:
8545: void nd_send_nd(ND p) {
8546: int len,i;
8547: NM m;
8548:
8549: if ( !p ) nd_send_int(0);
8550: else {
8551: len = LEN(p);
8552: nd_send_int(len);
8553: m = BDY(p);
8554: for ( i = 0; i < len; i++, m = NEXT(m) ) {
8555: nd_send_int(CM(m));
8556: nd_send_intarray((int *)DL(m),nd_wpd);
8557: }
8558: }
8559: }
8560:
8561: NDV nd_recv_ndv()
8562: {
8563: int len,i;
8564: NMV m,m0;
8565: NDV r;
8566:
8567: len = nd_recv_int();
8568: if ( !len ) return 0;
8569: else {
8570: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8571: #if 0
8572: ndv_alloc += len*nmv_adv;
8573: #endif
8574: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8575: CM(m) = nd_recv_int();
8576: nd_recv_intarray((int *)DL(m),nd_wpd);
8577: }
8578: MKNDV(nd_nvar,m0,len,r);
8579: return r;
8580: }
8581: }
8582:
8583: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
8584: {
8585: int i,j,t,c,rank,inv;
8586: int *ci,*ri;
8587: Z dn;
8588: MAT m,nm;
8589:
8590: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
8591: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
8592: for ( i = 0; i < row; i++ )
8593: for ( j = 0; j < col; j++ )
8594: mat0[i][j] = 0;
8595: c = col-rank;
8596: for ( i = 0; i < rank; i++ ) {
8597: mat0[i][ri[i]] = dn;
8598: for ( j = 0; j < c; j++ )
8599: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
8600: }
8601: return rank;
8602: }
8603:
8604: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
8605: {
8606: int i,j,k,l,inv,a,rank,s;
8607: unsigned int *t,*pivot,*pk;
8608: unsigned int **mat;
8609: ND_pairs pair;
8610:
8611: mat = (unsigned int **)mat0;
8612: for ( rank = 0, j = 0; j < col; j++ ) {
8613: for ( i = rank; i < row; i++ )
8614: mat[i][j] %= md;
8615: for ( i = rank; i < row; i++ )
8616: if ( mat[i][j] )
8617: break;
8618: if ( i == row ) {
8619: colstat[j] = 0;
8620: continue;
8621: } else
8622: colstat[j] = 1;
8623: if ( i != rank ) {
8624: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8625: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8626: if ( spactive ) {
8627: pair = spactive[i]; spactive[i] = spactive[rank];
8628: spactive[rank] = pair;
8629: }
8630: }
8631: pivot = mat[rank];
8632: s = sugar[rank];
8633: inv = invm(pivot[j],md);
8634: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8635: if ( *pk ) {
8636: if ( *pk >= (unsigned int)md )
8637: *pk %= md;
8638: DMAR(*pk,inv,0,md,*pk)
8639: }
8640: for ( i = rank+1; i < row; i++ ) {
8641: t = mat[i];
8642: if ( (a = t[j]) != 0 ) {
8643: sugar[i] = MAX(sugar[i],s);
8644: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8645: }
8646: }
8647: rank++;
8648: }
8649: for ( j = col-1, l = rank-1; j >= 0; j-- )
8650: if ( colstat[j] ) {
8651: pivot = mat[l];
8652: s = sugar[l];
8653: for ( i = 0; i < l; i++ ) {
8654: t = mat[i];
8655: t[j] %= md;
8656: if ( (a = t[j]) != 0 ) {
8657: sugar[i] = MAX(sugar[i],s);
8658: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8659: }
8660: }
8661: l--;
8662: }
8663: for ( j = 0, l = 0; l < rank; j++ )
8664: if ( colstat[j] ) {
8665: t = mat[l];
8666: for ( k = j; k < col; k++ )
8667: if ( t[k] >= (unsigned int)md )
8668: t[k] %= md;
8669: l++;
8670: }
8671: return rank;
8672: }
8673:
8674:
1.7 noro 8675: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 8676: {
1.7 noro 8677: int i,j,k,l,inv,a,rank,s;
8678: unsigned int *t,*pivot,*pk;
8679: unsigned int **mat;
8680:
8681: mat = (unsigned int **)mat0;
8682: for ( rank = 0, j = 0; j < col; j++ ) {
8683: for ( i = rank; i < row; i++ )
8684: if ( mat[i][j] )
8685: break;
8686: if ( i == row ) {
8687: colstat[j] = 0;
8688: continue;
8689: } else
8690: colstat[j] = 1;
8691: if ( i != rank ) {
8692: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8693: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8694: }
8695: pivot = mat[rank];
8696: s = sugar[rank];
8697: inv = _invsf(pivot[j]);
8698: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8699: if ( *pk )
8700: *pk = _mulsf(*pk,inv);
8701: for ( i = rank+1; i < row; i++ ) {
8702: t = mat[i];
8703: if ( (a = t[j]) != 0 ) {
8704: sugar[i] = MAX(sugar[i],s);
8705: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8706: }
8707: }
8708: rank++;
8709: }
8710: for ( j = col-1, l = rank-1; j >= 0; j-- )
8711: if ( colstat[j] ) {
8712: pivot = mat[l];
8713: s = sugar[l];
8714: for ( i = 0; i < l; i++ ) {
8715: t = mat[i];
8716: if ( (a = t[j]) != 0 ) {
8717: sugar[i] = MAX(sugar[i],s);
8718: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8719: }
8720: }
8721: l--;
8722: }
8723: return rank;
8724: }
1.1 noro 8725:
1.7 noro 8726: int ndv_ishomo(NDV p)
8727: {
8728: NMV m;
8729: int len,h;
1.1 noro 8730:
8731: if ( !p ) return 1;
8732: len = LEN(p);
8733: m = BDY(p);
8734: h = TD(DL(m));
8735: NMV_ADV(m);
8736: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 8737: if ( TD(DL(m)) != h ) {
8738: return 0;
8739: }
1.1 noro 8740: return 1;
8741: }
8742:
8743: void ndv_save(NDV p,int index)
8744: {
8745: FILE *s;
8746: char name[BUFSIZ];
8747: short id;
8748: int nv,sugar,len,n,i,td,e,j;
8749: NMV m;
8750: unsigned int *dl;
8751: int mpos;
8752:
8753: sprintf(name,"%s/%d",Demand,index);
8754: s = fopen(name,"w");
8755: savevl(s,0);
8756: if ( !p ) {
8757: saveobj(s,0);
8758: return;
8759: }
8760: id = O_DP;
8761: nv = NV(p);
8762: sugar = SG(p);
8763: len = LEN(p);
8764: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
8765: write_int(s,(unsigned int *)&len);
8766:
8767: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8768: saveobj(s,(Obj)CZ(m));
1.1 noro 8769: dl = DL(m);
8770: td = TD(dl);
8771: write_int(s,(unsigned int *)&td);
8772: for ( j = 0; j < nv; j++ ) {
8773: e = GET_EXP(dl,j);
8774: write_int(s,(unsigned int *)&e);
8775: }
8776: if ( nd_module ) {
8777: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
8778: }
8779: }
8780: fclose(s);
8781: }
8782:
8783: void nd_save_mod(ND p,int index)
8784: {
8785: FILE *s;
8786: char name[BUFSIZ];
8787: int nv,sugar,len,c;
8788: NM m;
8789:
8790: sprintf(name,"%s/%d",Demand,index);
8791: s = fopen(name,"w");
8792: if ( !p ) {
8793: len = 0;
8794: write_int(s,(unsigned int *)&len);
8795: fclose(s);
8796: return;
8797: }
8798: nv = NV(p);
8799: sugar = SG(p);
8800: len = LEN(p);
8801: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
8802: for ( m = BDY(p); m; m = NEXT(m) ) {
8803: c = CM(m); write_int(s,(unsigned int *)&c);
8804: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
8805: }
8806: fclose(s);
8807: }
8808:
8809: NDV ndv_load(int index)
8810: {
8811: FILE *s;
8812: char name[BUFSIZ];
8813: short id;
8814: int nv,sugar,len,n,i,td,e,j;
8815: NDV d;
8816: NMV m0,m;
8817: unsigned int *dl;
8818: Obj obj;
8819: int mpos;
8820:
8821: sprintf(name,"%s/%d",Demand,index);
8822: s = fopen(name,"r");
8823: if ( !s ) return 0;
8824:
8825: skipvl(s);
8826: read_short(s,(unsigned short *)&id);
8827: if ( !id ) return 0;
8828: read_int(s,(unsigned int *)&nv);
8829: read_int(s,(unsigned int *)&sugar);
8830: read_int(s,(unsigned int *)&len);
8831:
8832: m0 = m = MALLOC(len*nmv_adv);
8833: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8834: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 8835: dl = DL(m);
8836: ndl_zero(dl);
8837: read_int(s,(unsigned int *)&td); TD(dl) = td;
8838: for ( j = 0; j < nv; j++ ) {
8839: read_int(s,(unsigned int *)&e);
8840: PUT_EXP(dl,j,e);
8841: }
8842: if ( nd_module ) {
8843: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
8844: }
8845: if ( nd_blockmask ) ndl_weight_mask(dl);
8846: }
8847: fclose(s);
8848: MKNDV(nv,m0,len,d);
8849: SG(d) = sugar;
8850: return d;
8851: }
8852:
8853: ND nd_load_mod(int index)
8854: {
8855: FILE *s;
8856: char name[BUFSIZ];
8857: int nv,sugar,len,i,c;
8858: ND d;
8859: NM m0,m;
8860:
8861: sprintf(name,"%s/%d",Demand,index);
8862: s = fopen(name,"r");
8863: /* if the file does not exist, it means p[index]=0 */
8864: if ( !s ) return 0;
8865:
8866: read_int(s,(unsigned int *)&nv);
8867: if ( !nv ) { fclose(s); return 0; }
8868:
8869: read_int(s,(unsigned int *)&sugar);
8870: read_int(s,(unsigned int *)&len);
8871: for ( m0 = 0, i = 0; i < len; i++ ) {
8872: NEXTNM(m0,m);
8873: read_int(s,(unsigned int *)&c); CM(m) = c;
8874: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
8875: }
8876: NEXT(m) = 0;
8877: MKND(nv,m0,len,d);
8878: SG(d) = sugar;
8879: fclose(s);
8880: return d;
8881: }
8882:
8883: void nd_det(int mod,MAT f,P *rp)
8884: {
8885: VL fv,tv;
8886: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
8887: pointer **m;
8888: P **w;
8889: P mp,r;
8890: NDV **dm;
8891: NDV *t,*mi,*mj;
8892: NDV d,s,mij,mjj;
8893: ND u;
8894: NMV nmv;
8895: UINT *bound;
8896: PGeoBucket bucket;
8897: struct order_spec *ord;
8898: Z dq,dt,ds;
8899: Z mone;
8900: Z gn,qn,dn0,nm,dn;
8901:
8902: create_order_spec(0,0,&ord);
8903: nd_init_ord(ord);
8904: get_vars((Obj)f,&fv);
8905: if ( f->row != f->col )
8906: error("nd_det : non-square matrix");
8907: n = f->row;
8908: m = f->body;
8909: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8910:
8911: if ( !nvar ) {
8912: if ( !mod )
8913: detp(CO,(P **)m,n,rp);
8914: else {
8915: w = (P **)almat_pointer(n,n);
8916: for ( i = 0; i < n; i++ )
8917: for ( j = 0; j < n; j++ )
8918: ptomp(mod,(P)m[i][j],&w[i][j]);
8919: detmp(CO,mod,w,n,&mp);
8920: mptop(mp,rp);
8921: }
8922: return;
8923: }
8924:
8925: if ( !mod ) {
8926: w = (P **)almat_pointer(n,n);
8927: dq = ONE;
8928: for ( i = 0; i < n; i++ ) {
8929: dn0 = ONE;
8930: for ( j = 0; j < n; j++ ) {
8931: if ( !m[i][j] ) continue;
8932: lgp(m[i][j],&nm,&dn);
1.6 noro 8933: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 8934: }
8935: if ( !UNIZ(dn0) ) {
8936: ds = dn0;
8937: for ( j = 0; j < n; j++ )
8938: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8939: mulz(dq,ds,&dt); dq = dt;
8940: } else
8941: for ( j = 0; j < n; j++ )
8942: w[i][j] = (P)m[i][j];
8943: }
8944: m = (pointer **)w;
8945: }
8946:
8947: for ( i = 0, max = 1; i < n; i++ )
8948: for ( j = 0; j < n; j++ )
8949: for ( tv = fv; tv; tv = NEXT(tv) ) {
8950: e = getdeg(tv->v,(P)m[i][j]);
8951: max = MAX(e,max);
8952: }
8953: nd_setup_parameters(nvar,max);
8954: dm = (NDV **)almat_pointer(n,n);
8955: for ( i = 0, max = 1; i < n; i++ )
8956: for ( j = 0; j < n; j++ ) {
8957: dm[i][j] = ptondv(CO,fv,m[i][j]);
8958: if ( mod ) ndv_mod(mod,dm[i][j]);
8959: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8960: }
8961: d = ptondv(CO,fv,(P)ONE);
8962: if ( mod ) ndv_mod(mod,d);
8963: chsgnz(ONE,&mone);
8964: for ( j = 0, sgn = 1; j < n; j++ ) {
8965: if ( DP_Print ) {
8966: fprintf(asir_out,".");
8967: }
8968: for ( i = j; i < n && !dm[i][j]; i++ );
8969: if ( i == n ) {
8970: *rp = 0;
8971: return;
8972: }
8973: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8974: for ( k = j; k < n; k++ )
8975: for ( l = j; l < n; l++ )
8976: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8977: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8978: }
8979: if ( k0 != j ) {
8980: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8981: sgn = -sgn;
8982: }
8983: if ( l0 != j ) {
8984: for ( k = j; k < n; k++ ) {
8985: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8986: }
8987: sgn = -sgn;
8988: }
8989: bound = nd_det_compute_bound(dm,n,j);
8990: for ( k = 0; k < nd_nvar; k++ )
8991: if ( bound[k]*2 > nd_mask0 ) break;
8992: if ( k < nd_nvar )
8993: nd_det_reconstruct(dm,n,j,d);
8994:
8995: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8996: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8997: mi = dm[i]; mij = mi[j];
8998: if ( mod )
8999: ndv_mul_c(mod,mij,mod-1);
9000: else
9001: ndv_mul_c_q(mij,mone);
9002: for ( k = j+1; k < n; k++ ) {
9003: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9004: bucket = create_pbucket();
9005: if ( mi[k] ) {
9006: nmv = BDY(mjj); len = LEN(mjj);
9007: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9008: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9009: add_pbucket(mod,bucket,u);
9010: }
9011: }
9012: if ( mj[k] && mij ) {
9013: nmv = BDY(mij); len = LEN(mij);
9014: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9015: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9016: add_pbucket(mod,bucket,u);
9017: }
9018: }
9019: u = nd_quo(mod,bucket,d);
9020: mi[k] = ndtondv(mod,u);
9021: }
9022: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9023: }
9024: d = mjj;
9025: }
9026: if ( DP_Print ) {
9027: fprintf(asir_out,"\n");
9028: }
9029: if ( sgn < 0 ) {
9030: if ( mod )
9031: ndv_mul_c(mod,d,mod-1);
9032: else
9033: ndv_mul_c_q(d,mone);
9034: }
9035: r = ndvtop(mod,CO,fv,d);
9036: if ( !mod && !UNIQ(dq) )
9037: divsp(CO,r,(P)dq,rp);
9038: else
9039: *rp = r;
9040: }
9041:
9042: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9043: {
9044: NM mr,mr0;
9045: NM tnm;
9046: NMV m;
9047: UINT *d0,*dt,*dm;
9048: int c,n,td,i,c1,c2,len;
9049: Z q;
9050: ND r;
9051:
9052: if ( !p ) return 0;
9053: else {
9054: n = NV(p); m = BDY(p); len = LEN(p);
9055: d0 = DL(m0);
9056: td = TD(d);
9057: mr0 = 0;
9058: NEWNM(tnm);
9059: if ( mod ) {
9060: c = CM(m0);
9061: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9062: ndl_add(DL(m),d0,DL(tnm));
9063: if ( ndl_reducible(DL(tnm),d) ) {
9064: NEXTNM(mr0,mr);
9065: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9066: ndl_copy(DL(tnm),DL(mr));
9067: }
9068: }
9069: } else {
1.6 noro 9070: q = CZ(m0);
1.1 noro 9071: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9072: ndl_add(DL(m),d0,DL(tnm));
9073: if ( ndl_reducible(DL(tnm),d) ) {
9074: NEXTNM(mr0,mr);
1.6 noro 9075: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9076: ndl_copy(DL(tnm),DL(mr));
9077: }
9078: }
9079: }
9080: if ( !mr0 )
9081: return 0;
9082: else {
9083: NEXT(mr) = 0;
9084: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9085: MKND(NV(p),mr0,len,r);
9086: SG(r) = SG(p) + TD(d0);
9087: return r;
9088: }
9089: }
9090: }
9091:
9092: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9093: {
9094: int i,obpe,oadv,h,k,l;
9095: static NM prev_nm_free_list;
9096: EPOS oepos;
9097:
9098: obpe = nd_bpe;
9099: oadv = nmv_adv;
9100: oepos = nd_epos;
9101: if ( obpe < 2 ) nd_bpe = 2;
9102: else if ( obpe < 3 ) nd_bpe = 3;
9103: else if ( obpe < 4 ) nd_bpe = 4;
9104: else if ( obpe < 5 ) nd_bpe = 5;
9105: else if ( obpe < 6 ) nd_bpe = 6;
9106: else if ( obpe < 8 ) nd_bpe = 8;
9107: else if ( obpe < 10 ) nd_bpe = 10;
9108: else if ( obpe < 16 ) nd_bpe = 16;
9109: else if ( obpe < 32 ) nd_bpe = 32;
9110: else error("nd_det_reconstruct : exponent too large");
9111:
9112: nd_setup_parameters(nd_nvar,0);
9113: prev_nm_free_list = _nm_free_list;
9114: _nm_free_list = 0;
9115: for ( k = j; k < n; k++ )
9116: for (l = j; l < n; l++ )
9117: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9118: ndv_realloc(d,obpe,oadv,oepos);
9119: prev_nm_free_list = 0;
9120: #if 0
9121: GC_gcollect();
9122: #endif
9123: }
9124:
9125: /* returns a UINT array containing degree bounds */
9126:
9127: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9128: {
9129: UINT *d0,*d1,*d,*t,*r;
9130: int k,l,i;
9131:
9132: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9133: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9134: for ( k = j; k < n; k++ )
9135: for ( l = j; l < n; l++ )
9136: if ( dm[k][l] ) {
9137: d = ndv_compute_bound(dm[k][l]);
9138: for ( i = 0; i < nd_nvar; i++ )
9139: d0[i] = MAX(d0[i],d[i]);
9140: }
9141: return d0;
9142: }
9143:
9144: DL nd_separate_d(UINT *d,UINT *trans)
9145: {
9146: int n,td,i,e,j;
9147: DL a;
9148:
9149: ndl_zero(trans);
9150: td = 0;
9151: for ( i = 0; i < nd_ntrans; i++ ) {
9152: e = GET_EXP(d,i);
9153: PUT_EXP(trans,i,e);
9154: td += MUL_WEIGHT(e,i);
9155: }
9156: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9157: /* homogenized */
9158: i = nd_nvar-1;
9159: e = GET_EXP(d,i);
9160: PUT_EXP(trans,i,e);
9161: td += MUL_WEIGHT(e,i);
9162: }
9163: TD(trans) = td;
9164: if ( nd_blockmask) ndl_weight_mask(trans);
9165: NEWDL(a,nd_nalg);
9166: td = 0;
9167: for ( i = 0; i < nd_nalg; i++ ) {
9168: j = nd_ntrans+i;
9169: e = GET_EXP(d,j);
9170: a->d[i] = e;
9171: td += e;
9172: }
9173: a->td = td;
9174: return a;
9175: }
9176:
9177: int nd_monic(int mod,ND *p)
9178: {
9179: UINT *trans,*t;
9180: DL alg;
9181: MP mp0,mp;
9182: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9183: ND r;
9184: DL dl;
9185: DP nm;
9186: NDV ndv;
9187: DAlg inv,cd;
9188: ND s,c;
9189: Z l,mul;
9190: Z ln;
9191: int n,ntrans,i,e,td,is_lc,len;
9192: NumberField nf;
9193: struct oEGT eg0,eg1;
9194:
9195: if ( !(nf = get_numberfield()) )
9196: error("nd_monic : current_numberfield is not set");
9197:
9198: /* Q coef -> DAlg coef */
9199: NEWNM(ma0); ma = ma0;
9200: m = BDY(*p);
9201: is_lc = 1;
9202: while ( 1 ) {
9203: NEWMP(mp0); mp = mp0;
1.6 noro 9204: mp->c = (Obj)CZ(m);
1.1 noro 9205: mp->dl = nd_separate_d(DL(m),DL(ma));
9206: NEWNM(mb);
9207: for ( m = NEXT(m); m; m = NEXT(m) ) {
9208: alg = nd_separate_d(DL(m),DL(mb));
9209: if ( !ndl_equal(DL(ma),DL(mb)) )
9210: break;
1.6 noro 9211: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9212: }
9213: NEXT(mp) = 0;
9214: MKDP(nd_nalg,mp0,nm);
9215: MKDAlg(nm,ONE,cd);
9216: if ( is_lc == 1 ) {
9217: /* if the lc is a rational number, we have nothing to do */
9218: if ( !mp0->dl->td )
9219: return 1;
9220:
9221: get_eg(&eg0);
9222: invdalg(cd,&inv);
9223: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9224: /* check the validity of inv */
9225: if ( mod && !remqi((Q)inv->dn,mod) )
9226: return 0;
9227: CA(ma) = nf->one;
9228: is_lc = 0;
9229: ln = ONE;
9230: } else {
9231: muldalg(cd,inv,&CA(ma));
9232: lcmz(ln,CA(ma)->dn,&ln);
9233: }
9234: if ( m ) {
9235: NEXT(ma) = mb; ma = mb;
9236: } else {
9237: NEXT(ma) = 0;
9238: break;
9239: }
9240: }
9241: /* l = lcm(denoms) */
9242: l = ln;
9243: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9244: divsz(l,CA(m)->dn,&mul);
1.1 noro 9245: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9246: NEXTNM(mr0,mr);
1.6 noro 9247: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9248: dl = mp->dl;
9249: td = TD(DL(m));
9250: ndl_copy(DL(m),DL(mr));
9251: for ( i = 0; i < nd_nalg; i++ ) {
9252: e = dl->d[i];
9253: PUT_EXP(DL(mr),i+nd_ntrans,e);
9254: td += MUL_WEIGHT(e,i+nd_ntrans);
9255: }
9256: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9257: TD(DL(mr)) = td;
9258: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9259: }
9260: }
9261: NEXT(mr) = 0;
9262: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9263: MKND(NV(*p),mr0,len,r);
9264: /* XXX */
9265: SG(r) = SG(*p);
9266: nd_free(*p);
9267: *p = r;
9268: return 1;
9269: }
9270:
9271: NODE reverse_node(NODE n)
9272: {
9273: NODE t,t1;
9274:
9275: for ( t = 0; n; n = NEXT(n) ) {
9276: MKNODE(t1,BDY(n),t); t = t1;
9277: }
9278: return t;
9279: }
9280:
9281: P ndc_div(int mod,union oNDC a,union oNDC b)
9282: {
9283: union oNDC c;
9284: int inv,t;
9285:
9286: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9287: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9288: else if ( mod ) {
9289: inv = invm(b.m,mod);
9290: DMAR(a.m,inv,0,mod,t); c.m = t;
9291: } else if ( nd_vc )
9292: divsp(nd_vc,a.p,b.p,&c.p);
9293: else
9294: divsz(a.z,b.z,&c.z);
9295: return ndctop(mod,c);
9296: }
9297:
9298: P ndctop(int mod,union oNDC c)
9299: {
9300: Z q;
9301: int e;
9302: GFS gfs;
9303:
9304: if ( mod == -1 ) {
9305: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9306: } else if ( mod == -2 ) {
1.10 noro 9307: q = c.z; return (P)q;
1.1 noro 9308: } else if ( mod > 0 ) {
1.6 noro 9309: STOZ(c.m,q); return (P)q;
1.1 noro 9310: } else
9311: return (P)c.p;
9312: }
9313:
9314: /* [0,0,0,cont] = p -> p/cont */
9315:
9316: void finalize_tracelist(int i,P cont)
9317: {
9318: LIST l;
9319: NODE node;
9320: Z iq;
9321:
9322: if ( !UNIQ(cont) ) {
9323: node = mknode(4,NULLP,NULLP,NULLP,cont);
9324: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9325: nd_tracelist = node;
9326: }
1.6 noro 9327: STOZ(i,iq);
1.1 noro 9328: nd_tracelist = reverse_node(nd_tracelist);
9329: MKLIST(l,nd_tracelist);
9330: node = mknode(2,iq,l); MKLIST(l,node);
9331: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9332: nd_alltracelist = node; nd_tracelist = 0;
9333: }
9334:
9335: void conv_ilist(int demand,int trace,NODE g,int **indp)
9336: {
9337: int n,i,j;
9338: int *ind;
9339: NODE t;
9340:
9341: n = length(g);
9342: ind = (int *)MALLOC(n*sizeof(int));
9343: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9344: j = (long)BDY(t); ind[i] = j;
9345: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9346: }
9347: if ( indp ) *indp = ind;
9348: }
9349:
9350: void parse_nd_option(NODE opt)
9351: {
9352: NODE t,p,u;
9353: int i,s,n;
9354: char *key;
9355: Obj value;
9356:
9357: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
9358: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9359: nd_splist = 0; nd_check_splist = 0;
9360: nd_sugarweight = 0;
9361: nd_f4red =0;
9362: nd_rank0 = 0;
9363: for ( t = opt; t; t = NEXT(t) ) {
9364: p = BDY((LIST)BDY(t));
9365: key = BDY((STRING)BDY(p));
9366: value = (Obj)BDY(NEXT(p));
9367: if ( !strcmp(key,"gentrace") )
9368: nd_gentrace = value?1:0;
9369: else if ( !strcmp(key,"gensyz") )
9370: nd_gensyz = value?1:0;
9371: else if ( !strcmp(key,"nora") )
9372: nd_nora = value?1:0;
9373: else if ( !strcmp(key,"gbblock") ) {
9374: if ( value && OID(value) == O_LIST ) {
9375: u = BDY((LIST)value);
9376: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
9377: for ( i = 0; u; u = NEXT(u) ) {
9378: p = BDY((LIST)BDY(u));
1.6 noro 9379: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9380: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9381: }
9382: nd_gbblock[i] = -1;
9383: } else
9384: nd_gbblock = 0;
9385: } else if ( !strcmp(key,"newelim") )
9386: nd_newelim = value?1:0;
9387: else if ( !strcmp(key,"intersect") )
9388: nd_intersect = value?1:0;
1.17 noro 9389: else if ( !strcmp(key,"syzgen") )
9390: nd_intersect = ZTOS((Q)value);
1.1 noro 9391: else if ( !strcmp(key,"lf") )
9392: nd_lf = value?1:0;
9393: else if ( !strcmp(key,"trace") ) {
9394: if ( value ) {
9395: u = BDY((LIST)value);
9396: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 9397: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 9398: }
9399: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 9400: nd_f4red = ZTOS((Q)value);
1.1 noro 9401: } else if ( !strcmp(key,"rank0") ) {
9402: nd_rank0 = value?1:0;
9403: } else if ( !strcmp(key,"splist") ) {
9404: nd_splist = value?1:0;
9405: } else if ( !strcmp(key,"check_splist") ) {
9406: nd_check_splist = BDY((LIST)value);
9407: } else if ( !strcmp(key,"sugarweight") ) {
9408: u = BDY((LIST)value);
9409: n = length(u);
9410: nd_sugarweight = MALLOC(n*sizeof(int));
9411: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 9412: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 9413: }
9414: }
9415: }
9416:
9417: ND mdptond(DP d);
9418: ND nd_mul_nm(int mod,NM m0,ND p);
9419: ND nd_mul_nm_lf(NM m0,ND p);
9420: ND *btog(NODE ti,ND **p,int nb,int mod);
9421: ND btog_one(NODE ti,ND *p,int nb,int mod);
9422: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
9423: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
9424:
9425: /* d:monomial */
9426: ND mdptond(DP d)
9427: {
9428: NM m;
9429: ND r;
9430:
9431: if ( OID(d) == 1 )
9432: r = ptond(CO,CO,(P)d);
9433: else {
9434: NEWNM(m);
9435: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 9436: CZ(m) = (Z)BDY(d)->c;
1.1 noro 9437: NEXT(m) = 0;
9438: MKND(NV(d),m,1,r);
9439: }
9440: return r;
9441: }
9442:
9443: ND nd_mul_nm(int mod,NM m0,ND p)
9444: {
9445: UINT *d0;
9446: int c0,c1,c;
9447: NM tm,mr,mr0;
9448: ND r;
9449:
9450: if ( !p ) return 0;
9451: d0 = DL(m0);
9452: c0 = CM(m0);
9453: mr0 = 0;
9454: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9455: NEXTNM(mr0,mr);
9456: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
9457: ndl_add(d0,DL(tm),DL(mr));
9458: }
9459: NEXT(mr) = 0;
9460: MKND(NV(p),mr0,LEN(p),r);
9461: return r;
9462: }
9463:
9464: ND nd_mul_nm_lf(NM m0,ND p)
9465: {
9466: UINT *d0;
9467: Z c0,c1,c;
9468: NM tm,mr,mr0;
9469: ND r;
9470:
9471: if ( !p ) return 0;
9472: d0 = DL(m0);
9473: c0 = CZ(m0);
9474: mr0 = 0;
9475: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9476: NEXTNM(mr0,mr);
9477: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
9478: ndl_add(d0,DL(tm),DL(mr));
9479: }
9480: NEXT(mr) = 0;
9481: MKND(NV(p),mr0,LEN(p),r);
9482: return r;
9483: }
9484:
9485: ND *btog(NODE ti,ND **p,int nb,int mod)
9486: {
9487: PGeoBucket *r;
9488: int i,ci;
9489: NODE t,s;
9490: ND m,tp;
9491: ND *pi,*rd;
9492: P c;
9493:
9494: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9495: for ( i = 0; i < nb; i++ )
9496: r[i] = create_pbucket();
9497: for ( t = ti; t; t = NEXT(t) ) {
9498: s = BDY((LIST)BDY(t));
9499: if ( ARG0(s) ) {
9500: m = mdptond((DP)ARG2(s));
1.6 noro 9501: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9502: if ( (ci = ((MQ)c)->cont) != 0 ) {
9503: HCM(m) = ci;
1.6 noro 9504: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9505: for ( i = 0; i < nb; i++ ) {
9506: tp = nd_mul_nm(mod,BDY(m),pi[i]);
9507: add_pbucket(mod,r[i],tp);
9508: }
9509: }
9510: ci = 1;
9511: } else {
9512: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9513: ci = invm(ci,mod);
9514: }
9515: }
9516: rd = (ND *)MALLOC(nb*sizeof(ND));
9517: for ( i = 0; i < nb; i++ )
9518: rd[i] = normalize_pbucket(mod,r[i]);
9519: if ( ci != 1 )
9520: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
9521: return rd;
9522: }
9523:
9524: /* YYY */
9525: ND *btog_lf(NODE ti,ND **p,int nb)
9526: {
9527: PGeoBucket *r;
9528: int i;
9529: NODE t,s;
9530: ND m,tp;
9531: ND *pi,*rd;
9532: LM lm;
9533: Z lf,c;
9534:
9535: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9536: for ( i = 0; i < nb; i++ )
9537: r[i] = create_pbucket();
9538: for ( t = ti; t; t = NEXT(t) ) {
9539: s = BDY((LIST)BDY(t));
9540: if ( ARG0(s) ) {
9541: m = mdptond((DP)ARG2(s));
1.6 noro 9542: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 9543: if ( lm ) {
9544: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 9545: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9546: for ( i = 0; i < nb; i++ ) {
9547: tp = nd_mul_nm_lf(BDY(m),pi[i]);
9548: add_pbucket(-2,r[i],tp);
9549: }
9550: }
9551: c = ONE;
9552: } else {
9553: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
9554: }
9555: }
9556: rd = (ND *)MALLOC(nb*sizeof(ND));
9557: for ( i = 0; i < nb; i++ )
9558: rd[i] = normalize_pbucket(-2,r[i]);
9559: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
9560: return rd;
9561: }
9562:
9563: ND btog_one(NODE ti,ND *p,int nb,int mod)
9564: {
9565: PGeoBucket r;
9566: int i,ci,j;
9567: NODE t,s;
9568: ND m,tp;
9569: ND pi,rd;
9570: P c;
9571:
9572: r = create_pbucket();
9573: for ( t = ti; t; t = NEXT(t) ) {
9574: s = BDY((LIST)BDY(t));
9575: if ( ARG0(s) ) {
9576: m = mdptond((DP)ARG2(s));
1.6 noro 9577: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9578: if ( (ci = ((MQ)c)->cont) != 0 ) {
9579: HCM(m) = ci;
1.6 noro 9580: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 9581: if ( !pi ) {
9582: pi = nd_load_mod(j);
9583: tp = nd_mul_nm(mod,BDY(m),pi);
9584: nd_free(pi);
9585: add_pbucket(mod,r,tp);
9586: } else {
9587: tp = nd_mul_nm(mod,BDY(m),pi);
9588: add_pbucket(mod,r,tp);
9589: }
9590: }
9591: ci = 1;
9592: } else {
9593: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9594: ci = invm(ci,mod);
9595: }
9596: }
9597: rd = normalize_pbucket(mod,r);
9598: free_pbucket(r);
9599: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
9600: return rd;
9601: }
9602:
9603: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
9604:
9605: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
9606: {
9607: int i,j,n,m,nb,pi0,pi1,nvar;
9608: VL fv,tv,vv;
9609: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9610: ND **p;
9611: ND *c;
9612: ND u;
9613: P inv;
9614: MAT mat;
9615:
9616: if ( mod == -2 )
9617: return nd_btog_lf(f,v,ord,tlist,rp);
9618:
9619: parse_nd_option(current_option);
9620: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9621: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9622: switch ( ord->id ) {
9623: case 1:
9624: if ( ord->nv != nvar )
9625: error("nd_check : invalid order specification");
9626: break;
9627: default:
9628: break;
9629: }
9630: nd_init_ord(ord);
9631: #if 0
1.6 noro 9632: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9633: #else
9634: nd_bpe = 32;
9635: #endif
9636: nd_setup_parameters(nvar,0);
9637: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9638: intred = BDY((LIST)ARG3(BDY(tlist)));
9639: ind = BDY((LIST)ARG4(BDY(tlist)));
9640: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9641: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9642: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9643: if ( j > i ) i = j;
9644: }
9645: n = i+1;
9646: nb = length(BDY(f));
9647: p = (ND **)MALLOC(n*sizeof(ND *));
9648: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9649: pi = BDY((LIST)BDY(t));
1.6 noro 9650: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9651: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9652: ptomp(mod,(P)ARG2(pi),&inv);
9653: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9654: u = ptond(CO,vv,(P)ONE);
9655: HCM(u) = ((MQ)inv)->cont;
9656: c[pi1] = u;
9657: }
9658: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9659: printf("%d ",i); fflush(stdout);
9660: ti = BDY((LIST)BDY(t));
1.6 noro 9661: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9662: }
9663: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9664: printf("%d ",i); fflush(stdout);
9665: ti = BDY((LIST)BDY(t));
1.6 noro 9666: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9667: }
9668: m = length(ind);
9669: MKMAT(mat,nb,m);
9670: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9671: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9672: BDY(mat)[i][j] = ndtodp(mod,c[i]);
9673: return mat;
9674: }
9675:
9676: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
9677: {
9678: int i,j,n,m,nb,pi0,pi1,nvar;
9679: VL fv,tv,vv;
9680: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9681: ND **p;
9682: ND *c;
9683: ND u;
9684: MAT mat;
9685: LM lm;
9686: Z lf,inv;
9687:
9688: parse_nd_option(current_option);
9689: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9690: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9691: switch ( ord->id ) {
9692: case 1:
9693: if ( ord->nv != nvar )
9694: error("nd_check : invalid order specification");
9695: break;
9696: default:
9697: break;
9698: }
9699: nd_init_ord(ord);
9700: #if 0
1.6 noro 9701: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9702: #else
9703: nd_bpe = 32;
9704: #endif
9705: nd_setup_parameters(nvar,0);
9706: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9707: intred = BDY((LIST)ARG3(BDY(tlist)));
9708: ind = BDY((LIST)ARG4(BDY(tlist)));
9709: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9710: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9711: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9712: if ( j > i ) i = j;
9713: }
9714: n = i+1;
9715: nb = length(BDY(f));
9716: p = (ND **)MALLOC(n*sizeof(ND *));
9717: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9718: pi = BDY((LIST)BDY(t));
1.6 noro 9719: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9720: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9721: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
9722: u = ptond(CO,vv,(P)ONE);
9723: HCZ(u) = inv;
9724: c[pi1] = u;
9725: }
9726: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9727: printf("%d ",i); fflush(stdout);
9728: ti = BDY((LIST)BDY(t));
1.6 noro 9729: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9730: }
9731: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9732: printf("%d ",i); fflush(stdout);
9733: ti = BDY((LIST)BDY(t));
1.6 noro 9734: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9735: }
9736: m = length(ind);
9737: MKMAT(mat,nb,m);
9738: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9739: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9740: BDY(mat)[i][j] = ndtodp(-2,c[i]);
9741: return mat;
9742: }
9743:
9744: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
9745: LIST tlist,int pos,MAT *rp)
9746: {
9747: int i,j,n,m,nb,pi0,pi1,nvar;
9748: VL fv,tv,vv;
9749: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9750: ND *p;
9751: ND *c;
9752: ND u;
9753: P inv;
9754: VECT vect;
9755:
9756: if ( mod == -2 )
9757: error("nd_btog_one : not implemented yet for a large finite field");
9758:
9759: parse_nd_option(current_option);
9760: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9761: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9762: switch ( ord->id ) {
9763: case 1:
9764: if ( ord->nv != nvar )
9765: error("nd_check : invalid order specification");
9766: break;
9767: default:
9768: break;
9769: }
9770: nd_init_ord(ord);
9771: #if 0
1.6 noro 9772: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9773: #else
9774: nd_bpe = 32;
9775: #endif
9776: nd_setup_parameters(nvar,0);
9777: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9778: intred = BDY((LIST)ARG3(BDY(tlist)));
9779: ind = BDY((LIST)ARG4(BDY(tlist)));
9780: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9781: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9782: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9783: if ( j > i ) i = j;
9784: }
9785: n = i+1;
9786: nb = length(BDY(f));
9787: p = (ND *)MALLOC(n*sizeof(ND *));
9788: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9789: pi = BDY((LIST)BDY(t));
1.6 noro 9790: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9791: if ( pi1 == pos ) {
9792: ptomp(mod,(P)ARG2(pi),&inv);
9793: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9794: u = ptond(CO,vv,(P)ONE);
9795: HCM(u) = ((MQ)inv)->cont;
9796: p[pi0] = u;
9797: }
9798: }
9799: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9800: printf("%d ",i); fflush(stdout);
9801: ti = BDY((LIST)BDY(t));
1.6 noro 9802: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9803: if ( Demand ) {
9804: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
9805: }
9806: }
9807: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9808: printf("%d ",i); fflush(stdout);
9809: ti = BDY((LIST)BDY(t));
1.6 noro 9810: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9811: if ( Demand ) {
9812: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
9813: }
9814: }
9815: m = length(ind);
9816: MKVECT(vect,m);
9817: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 9818: u = p[ZTOS((Q)BDY(t))];
1.1 noro 9819: if ( !u ) {
1.6 noro 9820: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 9821: BDY(vect)[j] = ndtodp(mod,u);
9822: nd_free(u);
9823: } else
9824: BDY(vect)[j] = ndtodp(mod,u);
9825: }
9826: return vect;
9827: }
9828:
9829: void ndv_print_lf(NDV p)
9830: {
9831: NMV m;
9832: int i,len;
9833:
9834: if ( !p ) printf("0\n");
9835: else {
9836: len = LEN(p);
9837: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
9838: printf("+");
9839: mpz_out_str(asir_out,10,BDY(CZ(m)));
9840: printf("*");
9841: ndl_print(DL(m));
9842: }
9843: printf("\n");
9844: }
9845: }
9846:
9847: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
9848: {
9849: VL tv,fv,vv,vc,av;
9850: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
9851: int m,nocheck,nvar,mindex,e,max;
9852: NDV c;
9853: NMV a;
9854: P p,zp;
9855: Q dmy;
9856: EPOS oepos;
9857: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
9858: Alg alpha,dp;
9859: P poly;
9860: LIST f1,f2,zpl;
9861: Obj obj;
9862: NumberField nf;
9863: struct order_spec *ord1;
9864: struct oEGT eg_check,eg0,eg1;
9865: NODE tr,tl1,tl2,tl3,tl4;
9866: LIST l1,l2,l3,l4,l5;
9867: int *perm;
9868: int j,ret;
9869: NODE retn;
9870: Q jq,bpe;
9871:
9872: nd_module = 0;
9873: parse_nd_option(current_option);
9874: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9875: if ( nd_vc )
9876: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
9877: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9878: switch ( ord->id ) {
9879: case 1:
9880: if ( ord->nv != nvar )
9881: error("nd_f4_lf_trace : invalid order specification");
9882: break;
9883: default:
9884: break;
9885: }
9886:
9887: nd_ntrans = nvar;
9888: nd_nalg = 0;
9889:
9890: nocheck = 0;
9891: mindex = 0;
9892:
9893: /* do not use on-demand load/save */
9894: nd_demand = 0;
9895: m = trace > 1 ? trace : get_lprime(mindex);
9896: nd_init_ord(ord);
9897: mrank = 0;
9898: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9899: for ( tv = vv; tv; tv = NEXT(tv) ) {
9900: if ( nd_module ) {
9901: s = BDY((LIST)BDY(t));
9902: trank = length(s);
9903: mrank = MAX(mrank,trank);
9904: for ( ; s; s = NEXT(s) ) {
9905: e = getdeg(tv->v,(P)BDY(s));
9906: max = MAX(e,max);
9907: }
9908: } else {
9909: e = getdeg(tv->v,(P)BDY(t));
9910: max = MAX(e,max);
9911: }
9912: }
9913: nd_setup_parameters(nvar,max);
9914: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9915: ishomo = 1;
9916: /* XXX */
9917: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9918: if ( nd_module ) {
9919: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9920: } else {
9921: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9922: }
9923: if ( ishomo )
9924: ishomo = ishomo && ndv_ishomo(c);
9925: if ( c ) {
9926: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9927: ndv_mod(-2,c);
9928: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9929: }
9930: }
9931: if ( in0 ) NEXT(in) = 0;
9932: if ( fd0 ) NEXT(fd) = 0;
9933: if ( !ishomo && homo ) {
9934: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9935: c = (NDV)BDY(t); len = LEN(c);
9936: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9937: wmax = MAX(TD(DL(a)),wmax);
9938: }
9939: homogenize_order(ord,nvar,&ord1);
9940: nd_init_ord(ord1);
9941: nd_setup_parameters(nvar+1,wmax);
9942: for ( t = fd0; t; t = NEXT(t) )
9943: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9944: }
9945: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 9946: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 9947: if ( ret )
9948: cand = nd_f4_lf_trace_main(m,&perm);
9949: if ( !ret || !cand ) {
9950: *rp = 0; return;
9951: }
9952: if ( !ishomo && homo ) {
9953: /* dehomogenization */
9954: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9955: nd_init_ord(ord);
9956: nd_setup_parameters(nvar,0);
9957: }
9958: cand = ndv_reducebase(cand,perm);
9959: cand = ndv_reduceall(-2,cand);
9960: cbpe = nd_bpe;
9961: get_eg(&eg0);
9962: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9963: /* gbcheck : cand is a GB of Id(cand) ? */
9964: retn = nd_f4(-2,0,0);
9965: }
9966: if ( !retn ) {
9967: /* failure */
9968: *rp = 0; return;
9969: }
9970: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9971: if ( DP_Print )
1.5 noro 9972: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 9973: /* dp->p */
9974: nd_bpe = cbpe;
9975: nd_setup_parameters(nd_nvar,0);
9976: for ( r = cand; r; r = NEXT(r) ) {
9977: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9978: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9979: }
9980: MKLIST(*rp,cand);
9981: }
9982:
9983: NODE nd_f4_lf_trace_main(int m,int **indp)
9984: {
9985: int i,nh,stat,index;
9986: NODE r,rm,g;
9987: ND_pairs d,l,l0,t;
9988: ND spol,red;
9989: NDV nf,redv,nfqv,nfv;
9990: NM s0,s;
9991: NODE rp0,srp0,nflist,nflist_lf;
9992: int nsp,nred,col,rank,len,k,j,a;
9993: UINT c;
9994: UINT **spmat;
9995: UINT *s0vect,*svect,*p,*v;
9996: int *colstat;
9997: IndArray *imat;
9998: int *rhead;
9999: int spcol,sprow;
10000: int sugar;
10001: PGeoBucket bucket;
10002: struct oEGT eg0,eg1,eg_f4;
10003:
10004: g = 0; d = 0;
10005: for ( i = 0; i < nd_psn; i++ ) {
10006: d = update_pairs(d,g,i,0);
10007: g = update_base(g,i);
10008: }
10009: while ( d ) {
10010: get_eg(&eg0);
10011: l = nd_minsugarp(d,&d);
10012: sugar = SG(l);
10013: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10014: bucket = create_pbucket();
10015: stat = nd_sp_f4(m,0,l,bucket);
10016: if ( !stat ) {
10017: for ( t = l; NEXT(t); t = NEXT(t) );
10018: NEXT(t) = d; d = l;
10019: d = nd_reconstruct(1,d);
10020: continue;
10021: }
10022: if ( bucket->m < 0 ) continue;
10023: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10024: if ( !col ) {
10025: for ( t = l; NEXT(t); t = NEXT(t) );
10026: NEXT(t) = d; d = l;
10027: d = nd_reconstruct(1,d);
10028: continue;
10029: }
10030: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10031: if ( DP_Print )
1.5 noro 10032: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10033: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10034: if ( !l0 ) continue;
10035: l = l0;
10036:
10037: /* over LF */
10038: bucket = create_pbucket();
10039: stat = nd_sp_f4(-2,1,l,bucket);
10040: if ( !stat ) {
10041: for ( t = l; NEXT(t); t = NEXT(t) );
10042: NEXT(t) = d; d = l;
10043: d = nd_reconstruct(1,d);
10044: continue;
10045: }
10046: if ( bucket->m < 0 ) continue;
10047: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10048: if ( !col ) {
10049: for ( t = l; NEXT(t); t = NEXT(t) );
10050: NEXT(t) = d; d = l;
10051: d = nd_reconstruct(1,d);
10052: continue;
10053: }
10054: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10055: /* adding new bases */
10056: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10057: nfv = (NDV)BDY(rm);
10058: nfqv = (NDV)BDY(r);
10059: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10060: ndv_removecont(m,nfv);
10061: ndv_removecont(-2,nfqv);
1.24 noro 10062: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10063: d = update_pairs(d,g,nh,0);
10064: g = update_base(g,nh);
10065: }
10066: if ( r || rm ) return 0;
10067: }
10068: conv_ilist(nd_demand,1,g,indp);
10069: return g;
10070: }
10071:
1.7 noro 10072: #if SIZEOF_LONG==8
10073:
10074: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10075: {
10076: int j,k,len;
10077: UINT *p;
10078: UINT c;
10079: NDV r;
10080: NMV mr0,mr;
10081:
10082: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10083: if ( !len ) return 0;
10084: else {
10085: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10086: #if 0
10087: ndv_alloc += nmv_adv*len;
10088: #endif
10089: mr = mr0;
10090: p = s0vect;
10091: for ( j = k = 0; j < col; j++, p += nd_wpd )
10092: if ( !rhead[j] ) {
10093: if ( (c = (UINT)vect[k++]) != 0 ) {
10094: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10095: }
10096: }
10097: MKNDV(nd_nvar,mr0,len,r);
10098: return r;
10099: }
10100: }
10101:
10102: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10103: {
10104: NM m;
1.11 noro 10105: UINT *t,*s,*u;
10106: int i,st,ed,md,prev,c;
1.7 noro 10107:
10108: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10109: prev = 0;
10110: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10111: t = DL(m);
10112: st = prev;
10113: ed = n;
10114: while ( ed > st ) {
10115: md = (st+ed)/2;
10116: u = s0+md*nd_wpd;
10117: c = DL_COMPARE(u,t);
10118: if ( c == 0 ) break;
10119: else if ( c > 0 ) st = md;
10120: else ed = md;
10121: }
10122: r[md] = (mp_limb_t)CM(m);
10123: prev = md;
1.7 noro 10124: }
10125: for ( i = 0; !r[i]; i++ );
10126: return i;
10127: }
10128:
10129: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10130:
10131: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
10132: {
10133: int i,j,k,len,pos,prev;
10134: mp_limb_t a,c,c1,c2;
10135: IndArray ivect;
10136: unsigned char *ivc;
10137: unsigned short *ivs;
10138: unsigned int *ivi;
10139: NDV redv;
10140: NMV mr;
10141: NODE rp;
10142: int maxrs;
10143:
10144: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10145: maxrs = 0;
10146: for ( i = 0; i < nred; i++ ) {
10147: ivect = imat[i];
10148: k = ivect->head;
10149: a = svect[k]; c = cvect[k];
10150: MOD128(a,c,m);
10151: svect[k] = a; cvect[k] = 0;
10152: if ( (c = svect[k]) != 0 ) {
1.11 noro 10153: Nf4_red++;
1.7 noro 10154: maxrs = MAX(maxrs,rp0[i]->sugar);
10155: c = m-c; redv = nd_ps[rp0[i]->index];
10156: len = LEN(redv); mr = BDY(redv);
10157: svect[k] = 0; prev = k;
10158: switch ( ivect->width ) {
10159: case 1:
10160: ivc = ivect->index.c;
10161: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10162: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10163: c2 = svect[pos]+c1*c;
10164: if ( c2 < svect[pos] ) cvect[pos]++;
10165: svect[pos] = c2;
1.7 noro 10166: }
10167: break;
10168: case 2:
10169: ivs = ivect->index.s;
10170: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10171: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10172: c2 = svect[pos]+c1*c;
10173: if ( c2 < svect[pos] ) cvect[pos]++;
10174: svect[pos] = c2;
1.7 noro 10175: }
10176: break;
10177: case 4:
10178: ivi = ivect->index.i;
10179: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10180: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10181: c2 = svect[pos]+c1*c;
10182: if ( c2 < svect[pos] ) cvect[pos]++;
10183: svect[pos] = c2;
1.7 noro 10184: }
10185: break;
10186: }
10187: }
10188: }
10189: for ( i = 0; i < col; i++ ) {
10190: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10191: }
10192: return maxrs;
10193: }
10194:
10195: /* for Fp, 2^15=<p<2^29 */
10196:
10197: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10198: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10199: {
10200: int spcol,sprow,a;
10201: int i,j,k,l,rank;
10202: NODE r0,r;
10203: ND_pairs sp;
10204: ND spol;
10205: mp_limb_t **spmat;
10206: mp_limb_t *svect,*cvect;
10207: mp_limb_t *v;
10208: int *colstat;
10209: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10210: int maxrs;
10211: int *spsugar;
10212: ND_pairs *spactive;
10213:
10214: spcol = col-nred;
10215: get_eg(&eg0);
10216: /* elimination (1st step) */
10217: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10218: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10219: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10220: spsugar = (int *)MALLOC(nsp*sizeof(int));
10221: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10222: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10223: nd_sp(m,0,sp,&spol);
10224: if ( !spol ) continue;
10225: nd_to_vect64(m,s0vect,col,spol,svect);
10226: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
10227: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10228: if ( i < col ) {
10229: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10230: for ( j = k = 0; j < col; j++ )
10231: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10232: spsugar[sprow] = MAX(maxrs,SG(spol));
10233: if ( nz )
10234: spactive[sprow] = sp;
10235: sprow++;
10236: }
10237: nd_free(spol);
10238: }
1.12 noro 10239: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10240: if ( DP_Print ) {
10241: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10242: fflush(asir_out);
10243: }
10244: /* free index arrays */
10245: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10246:
10247: /* elimination (2nd step) */
10248: colstat = (int *)MALLOC(spcol*sizeof(int));
10249: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10250: r0 = 0;
10251: for ( i = 0; i < rank; i++ ) {
10252: NEXTNODE(r0,r); BDY(r) =
10253: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10254: SG((NDV)BDY(r)) = spsugar[i];
10255: GCFREE(spmat[i]);
10256: }
10257: if ( r0 ) NEXT(r) = 0;
10258:
10259: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10260: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10261: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10262: if ( DP_Print ) {
10263: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10264: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10265: nsp,nred,sprow,spcol,rank);
10266: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10267: }
10268: if ( nz ) {
10269: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10270: if ( rank > 0 ) {
10271: NEXT(spactive[rank-1]) = 0;
10272: *nz = spactive[0];
10273: } else
10274: *nz = 0;
10275: }
10276: return r0;
10277: }
10278:
10279: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10280: {
10281: int i,j,k,l,rank,s;
10282: mp_limb_t inv;
10283: mp_limb_t a;
10284: UINT c;
10285: mp_limb_t *t,*pivot,*pk;
10286: UINT *ck;
10287: UINT **cmat;
10288: UINT *ct;
10289: ND_pairs pair;
10290:
10291: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10292: for ( i = 0; i < row; i++ ) {
10293: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10294: bzero(cmat[i],col*sizeof(UINT));
10295: }
10296:
10297: for ( rank = 0, j = 0; j < col; j++ ) {
10298: for ( i = rank; i < row; i++ ) {
10299: a = mat[i][j]; c = cmat[i][j];
10300: MOD128(a,c,md);
10301: mat[i][j] = a; cmat[i][j] = 0;
10302: }
10303: for ( i = rank; i < row; i++ )
10304: if ( mat[i][j] )
10305: break;
10306: if ( i == row ) {
10307: colstat[j] = 0;
10308: continue;
10309: } else
10310: colstat[j] = 1;
10311: if ( i != rank ) {
10312: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10313: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10314: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10315: if ( spactive ) {
10316: pair = spactive[i]; spactive[i] = spactive[rank];
10317: spactive[rank] = pair;
10318: }
10319: }
10320: /* column j is normalized */
10321: s = sugar[rank];
10322: inv = invm((UINT)mat[rank][j],md);
10323: /* normalize pivot row */
10324: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10325: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10326: }
10327: for ( i = rank+1; i < row; i++ ) {
10328: if ( (a = mat[i][j]) != 0 ) {
10329: sugar[i] = MAX(sugar[i],s);
10330: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10331: Nf4_red++;
1.7 noro 10332: }
10333: }
10334: rank++;
10335: }
10336: for ( j = col-1, l = rank-1; j >= 0; j-- )
10337: if ( colstat[j] ) {
10338: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10339: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10340: }
10341: s = sugar[l];
10342: for ( i = 0; i < l; i++ ) {
10343: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10344: if ( a ) {
10345: sugar[i] = MAX(sugar[i],s);
10346: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10347: Nf4_red++;
1.7 noro 10348: }
10349: }
10350: l--;
10351: }
10352: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10353: GCFREE(cmat);
10354: return rank;
10355: }
10356: #endif
10357:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>