Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.23
1.23 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.22 2019/11/21 01:54:01 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;
21:
22: Obj nd_top_weight;
23:
24: static NODE nd_subst;
25: static VL nd_vc;
26: static int nd_ntrans;
27: static int nd_nalg;
28: #if 0
29: static int ndv_alloc;
30: #endif
31: #if 1
32: static int nd_f4_nsp=0x7fffffff;
33: #else
34: static int nd_f4_nsp=50;
35: #endif
36: static double nd_scale=2;
37: static UINT **nd_bound;
38: static struct order_spec *nd_ord;
39: static EPOS nd_epos;
40: static BlockMask nd_blockmask;
41: static int nd_nvar;
42: static int nd_isrlex;
43: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
44: static UINT nd_mask[32];
45: static UINT nd_mask0,nd_mask1;
46:
47: static NDV *nd_ps;
48: static NDV *nd_ps_trace;
49: static NDV *nd_ps_sym;
50: static NDV *nd_ps_trace_sym;
51: static RHist *nd_psh;
52: static int nd_psn,nd_pslen;
53: static RHist *nd_red;
54: static int *nd_work_vector;
55: static int **nd_matrix;
56: static int nd_matrix_len;
57: static struct weight_or_block *nd_worb;
58: static int nd_worb_len;
59: static int nd_found,nd_create,nd_notfirst;
60: static int nmv_adv;
61: static int nd_demand;
1.21 noro 62: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 63: static int nd_module_rank,nd_poly_weight_len;
64: static int *nd_poly_weight,*nd_module_weight;
65: static NODE nd_tracelist;
66: static NODE nd_alltracelist;
67: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
68: static int *nd_gbblock;
69: static NODE nd_nzlist,nd_check_splist;
70: static int nd_splist;
71: static int *nd_sugarweight;
72: static int nd_f4red,nd_rank0,nd_last_nonzero;
73:
74: NumberField get_numberfield();
75: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
76: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
77: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
78: int nd_monic(int m,ND *p);
79: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
80: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
81: NDV pltondv(VL vl,VL dvl,LIST p);
82: void pltozpl(LIST l,Q *cont,LIST *pp);
83: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
84: void nmtodp(int mod,NM m,DP *r);
1.15 noro 85: void ndltodp(UINT *d,DP *r);
1.1 noro 86: NODE reverse_node(NODE n);
87: P ndc_div(int mod,union oNDC a,union oNDC b);
88: P ndctop(int mod,union oNDC c);
89: void finalize_tracelist(int i,P cont);
90: void conv_ilist(int demand,int trace,NODE g,int **indp);
91: void parse_nd_option(NODE opt);
92: void dltondl(int n,DL dl,UINT *r);
93: DP ndvtodp(int mod,NDV p);
94: DP ndtodp(int mod,ND p);
1.16 noro 95: DPM ndvtodpm(int mod,NDV p);
96: NDV dpmtondv(int mod,DPM p);
97: int dpm_getdeg(DPM p,int *rank);
98: void dpm_ptozp(DPM p,Z *cont,DPM *r);
99: int compdmm(int nv,DMM a,DMM b);
1.1 noro 100:
101: void Pdp_set_weight(NODE,VECT *);
102: void Pox_cmo_rpc(NODE,Obj *);
103:
104: ND nd_add_lf(ND p1,ND p2);
105: void nd_mul_c_lf(ND p,Z mul);
106: void ndv_mul_c_lf(NDV p,Z mul);
107: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
108: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
109: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
110: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
111: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
112: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
113: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
114: NODE nd_f4_lf_trace_main(int m,int **indp);
115: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
116:
117: extern int lf_lazy;
118: extern Z current_mod_lf;
119:
120: extern int Denominator,DP_Multiple,MaxDeg;
121:
122: #define BLEN (8*sizeof(unsigned long))
123:
124: typedef struct matrix {
125: int row,col;
126: unsigned long **a;
127: } *matrix;
128:
129:
130: void nd_free_private_storage()
131: {
132: _nm_free_list = 0;
133: _ndp_free_list = 0;
134: #if 0
135: GC_gcollect();
136: #endif
137: }
138:
139: void _NM_alloc()
140: {
141: NM p;
142: int i;
143:
144: for ( i = 0; i < 1024; i++ ) {
145: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
146: p->next = _nm_free_list; _nm_free_list = p;
147: }
148: }
149:
150: matrix alloc_matrix(int row,int col)
151: {
152: unsigned long **a;
153: int i,len,blen;
154: matrix mat;
155:
156: mat = (matrix)MALLOC(sizeof(struct matrix));
157: mat->row = row;
158: mat->col = col;
159: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
160: return mat;
161: }
162:
163:
164: void _ND_alloc()
165: {
166: ND p;
167: int i;
168:
169: for ( i = 0; i < 1024; i++ ) {
170: p = (ND)MALLOC(sizeof(struct oND));
171: p->body = (NM)_nd_free_list; _nd_free_list = p;
172: }
173: }
174:
175: void _NDP_alloc()
176: {
177: ND_pairs p;
178: int i;
179:
180: for ( i = 0; i < 1024; i++ ) {
181: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
182: +(nd_wpd-1)*sizeof(UINT));
183: p->next = _ndp_free_list; _ndp_free_list = p;
184: }
185: }
186:
187: INLINE int nd_length(ND p)
188: {
189: NM m;
190: int i;
191:
192: if ( !p )
193: return 0;
194: else {
195: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
196: return i;
197: }
198: }
199:
200: extern int dp_negative_weight;
201:
202: INLINE int ndl_reducible(UINT *d1,UINT *d2)
203: {
204: UINT u1,u2;
205: int i,j;
206:
207: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
208:
209: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
210: #if USE_UNROLL
211: switch ( nd_bpe ) {
212: case 3:
213: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
214: u1 = d1[i]; u2 = d2[i];
215: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
216: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
217: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
218: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
219: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
220: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
221: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
222: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
223: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
224: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
225: }
226: return 1;
227: break;
228: case 4:
229: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
230: u1 = d1[i]; u2 = d2[i];
231: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
232: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
233: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
234: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
235: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
236: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
237: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
238: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
239: }
240: return 1;
241: break;
242: case 6:
243: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
244: u1 = d1[i]; u2 = d2[i];
245: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
246: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
247: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
248: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
249: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
250: }
251: return 1;
252: break;
253: case 8:
254: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
255: u1 = d1[i]; u2 = d2[i];
256: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
257: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
258: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
259: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
260: }
261: return 1;
262: break;
263: case 16:
264: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
265: u1 = d1[i]; u2 = d2[i];
266: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
267: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
268: }
269: return 1;
270: break;
271: case 32:
272: for ( i = nd_exporigin; i < nd_wpd; i++ )
273: if ( d1[i] < d2[i] ) return 0;
274: return 1;
275: break;
276: default:
277: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
278: u1 = d1[i]; u2 = d2[i];
279: for ( j = 0; j < nd_epw; j++ )
280: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
281: }
282: return 1;
283: }
284: #else
285: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
286: u1 = d1[i]; u2 = d2[i];
287: for ( j = 0; j < nd_epw; j++ )
288: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
289: }
290: return 1;
291: #endif
292: }
293:
294: /*
295: * If the current order is a block order,
296: * then the last block is length 1 and contains
297: * the homo variable. Otherwise, the original
298: * order is either 0 or 2.
299: */
300:
301: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
302: {
303: int w,i,e,n,omask0;
304:
305: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
306: n = nd_nvar-1;
307: ndl_zero(r);
308: for ( i = 0; i < n; i++ ) {
309: e = GET_EXP_OLD(d,i);
310: PUT_EXP(r,i,e);
311: }
312: w = TD(d);
313: PUT_EXP(r,nd_nvar-1,weight-w);
314: if ( nd_module ) MPOS(r) = d[ompos];
315: TD(r) = weight;
316: if ( nd_blockmask ) ndl_weight_mask(r);
317: }
318:
319: void ndl_dehomogenize(UINT *d)
320: {
321: UINT mask;
322: UINT h;
323: int i,bits;
324:
325: if ( nd_blockmask ) {
326: h = GET_EXP(d,nd_nvar-1);
327: XOR_EXP(d,nd_nvar-1,h);
328: TD(d) -= h;
329: ndl_weight_mask(d);
330: } else {
331: if ( nd_isrlex ) {
332: if ( nd_bpe == 32 ) {
333: h = d[nd_exporigin];
334: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
335: d[i-1] = d[i];
336: d[i-1] = 0;
337: TD(d) -= h;
338: } else {
339: bits = nd_epw*nd_bpe;
340: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
341: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
342: for ( i = nd_exporigin; i < nd_wpd; i++ )
343: d[i] = ((d[i]<<nd_bpe)&mask)
344: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
345: TD(d) -= h;
346: }
347: } else {
348: h = GET_EXP(d,nd_nvar-1);
349: XOR_EXP(d,nd_nvar-1,h);
350: TD(d) -= h;
351: }
352: }
353: }
354:
355: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
356: {
357: UINT t1,t2,u,u1,u2;
358: int i,j,l;
359:
360: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
361: error("ndl_lcm : inconsistent monomials");
362: #if USE_UNROLL
363: switch ( nd_bpe ) {
364: case 3:
365: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
366: u1 = d1[i]; u2 = d2[i];
367: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
368: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
369: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
370: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
371: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
372: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
373: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
374: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
375: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
376: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
377: d[i] = u;
378: }
379: break;
380: case 4:
381: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
382: u1 = d1[i]; u2 = d2[i];
383: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
384: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
385: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
386: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
387: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
388: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
389: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
390: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
391: d[i] = u;
392: }
393: break;
394: case 6:
395: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
396: u1 = d1[i]; u2 = d2[i];
397: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
398: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
400: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
401: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
402: d[i] = u;
403: }
404: break;
405: case 8:
406: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
407: u1 = d1[i]; u2 = d2[i];
408: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
409: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
410: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
411: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
412: d[i] = u;
413: }
414: break;
415: case 16:
416: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
417: u1 = d1[i]; u2 = d2[i];
418: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
419: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
420: d[i] = u;
421: }
422: break;
423: case 32:
424: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
425: u1 = d1[i]; u2 = d2[i];
426: d[i] = u1>u2?u1:u2;
427: }
428: break;
429: default:
430: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
431: u1 = d1[i]; u2 = d2[i];
432: for ( j = 0, u = 0; j < nd_epw; j++ ) {
433: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
434: }
435: d[i] = u;
436: }
437: break;
438: }
439: #else
440: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
441: u1 = d1[i]; u2 = d2[i];
442: for ( j = 0, u = 0; j < nd_epw; j++ ) {
443: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
444: }
445: d[i] = u;
446: }
447: #endif
448: if ( nd_module ) MPOS(d) = MPOS(d1);
449: TD(d) = ndl_weight(d);
450: if ( nd_blockmask ) ndl_weight_mask(d);
451: }
452:
453: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
454: {
455: UINT t1,t2,u,u1,u2;
456: int i,j,l;
457:
458: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
459: u1 = d1[i]; u2 = d2[i];
460: for ( j = 0, u = 0; j < nd_epw; j++ ) {
461: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
462: }
463: d[i] = u;
464: }
465: }
466:
467: int ndl_weight(UINT *d)
468: {
469: UINT t,u;
470: int i,j;
471:
472: if ( current_dl_weight_vector )
473: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
474: u = GET_EXP(d,i);
475: t += MUL_WEIGHT(u,i);
476: }
477: else
478: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
479: u = d[i];
480: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
481: t += (u&nd_mask0);
482: }
1.20 noro 483: if ( nd_module && nd_module_rank && MPOS(d) )
484: t += nd_module_weight[MPOS(d)-1];
485: for ( i = nd_exporigin; i < nd_wpd; i++ )
486: if ( d[i] && !t )
487: printf("afo\n");
1.1 noro 488: return t;
489: }
490:
491: /* for sugarweight */
492:
493: int ndl_weight2(UINT *d)
494: {
495: int t,u;
496: int i,j;
497:
498: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
499: u = GET_EXP(d,i);
500: t += nd_sugarweight[i]*u;
501: }
1.20 noro 502: if ( nd_module && nd_module_rank && MPOS(d) )
503: t += nd_module_weight[MPOS(d)-1];
1.1 noro 504: return t;
505: }
506:
507: void ndl_weight_mask(UINT *d)
508: {
509: UINT t,u;
510: UINT *mask;
511: int i,j,k,l;
512:
513: l = nd_blockmask->n;
514: for ( k = 0; k < l; k++ ) {
515: mask = nd_blockmask->mask[k];
516: if ( current_dl_weight_vector )
517: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
518: u = GET_EXP_MASK(d,i,mask);
519: t += MUL_WEIGHT(u,i);
520: }
521: else
522: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
523: u = d[i]&mask[i];
524: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
525: t += (u&nd_mask0);
526: }
527: d[k+1] = t;
528: }
529: }
530:
1.21 noro 531: int ndl_glex_compare(UINT *d1,UINT *d2)
532: {
533: if ( TD(d1) > TD(d2) ) return 1;
534: else if ( TD(d1) < TD(d2) ) return -1;
535: else return ndl_lex_compare(d1,d2);
536: }
537:
1.1 noro 538: int ndl_lex_compare(UINT *d1,UINT *d2)
539: {
540: int i;
541:
542: d1 += nd_exporigin;
543: d2 += nd_exporigin;
544: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
545: if ( *d1 > *d2 )
546: return nd_isrlex ? -1 : 1;
547: else if ( *d1 < *d2 )
548: return nd_isrlex ? 1 : -1;
549: return 0;
550: }
551:
552: int ndl_block_compare(UINT *d1,UINT *d2)
553: {
554: int i,l,j,ord_o,ord_l;
555: struct order_pair *op;
556: UINT t1,t2,m;
557: UINT *mask;
558:
559: l = nd_blockmask->n;
560: op = nd_blockmask->order_pair;
561: for ( j = 0; j < l; j++ ) {
562: mask = nd_blockmask->mask[j];
563: ord_o = op[j].order;
564: if ( ord_o < 2 ) {
565: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
566: else if ( t1 < t2 ) return -1;
567: }
568: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
569: m = mask[i];
570: t1 = d1[i]&m;
571: t2 = d2[i]&m;
572: if ( t1 > t2 )
573: return !ord_o ? -1 : 1;
574: else if ( t1 < t2 )
575: return !ord_o ? 1 : -1;
576: }
577: }
578: return 0;
579: }
580:
581: int ndl_matrix_compare(UINT *d1,UINT *d2)
582: {
583: int i,j,s,row;
584: int *v;
585: Z **mat;
586: Z *w;
587: Z t1;
588: Z t,t2;
589:
1.6 noro 590: for ( j = 0; j < nd_nvar; j++ )
591: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 592: if ( nd_top_weight ) {
593: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 594: mat = (Z **)&BDY((VECT)nd_top_weight);
595: row = 1;
1.1 noro 596: } else {
597: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 598: row = ((MAT)nd_top_weight)->row;
1.1 noro 599: }
600: for ( i = 0; i < row; i++ ) {
1.6 noro 601: w = mat[i];
1.1 noro 602: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 603: STOZ(nd_work_vector[j],t1);
1.1 noro 604: mulz(w[j],t1,&t2);
605: addz(t,t2,&t1);
606: t = t1;
607: }
1.6 noro 608: if ( t ) {
609: s = sgnz(t);
1.1 noro 610: if ( s > 0 ) return 1;
611: else if ( s < 0 ) return -1;
1.6 noro 612: }
1.1 noro 613: }
1.6 noro 614: }
615: for ( i = 0; i < nd_matrix_len; i++ ) {
616: v = nd_matrix[i];
617: for ( j = 0, s = 0; j < nd_nvar; j++ )
618: s += v[j]*nd_work_vector[j];
619: if ( s > 0 ) return 1;
620: else if ( s < 0 ) return -1;
621: }
1.1 noro 622: if ( !ndl_equal(d1,d2) )
1.6 noro 623: error("ndl_matrix_compare : invalid matrix");
624: return 0;
1.1 noro 625: }
626:
627: int ndl_composite_compare(UINT *d1,UINT *d2)
628: {
629: int i,j,s,start,end,len,o;
630: int *v;
631: struct sparse_weight *sw;
632:
633: for ( j = 0; j < nd_nvar; j++ )
634: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
635: for ( i = 0; i < nd_worb_len; i++ ) {
636: len = nd_worb[i].length;
637: switch ( nd_worb[i].type ) {
638: case IS_DENSE_WEIGHT:
639: v = nd_worb[i].body.dense_weight;
640: for ( j = 0, s = 0; j < len; j++ )
641: s += v[j]*nd_work_vector[j];
642: if ( s > 0 ) return 1;
643: else if ( s < 0 ) return -1;
644: break;
645: case IS_SPARSE_WEIGHT:
646: sw = nd_worb[i].body.sparse_weight;
647: for ( j = 0, s = 0; j < len; j++ )
648: s += sw[j].value*nd_work_vector[sw[j].pos];
649: if ( s > 0 ) return 1;
650: else if ( s < 0 ) return -1;
651: break;
652: case IS_BLOCK:
653: o = nd_worb[i].body.block.order;
654: start = nd_worb[i].body.block.start;
655: switch ( o ) {
656: case 0:
657: end = start+len;
658: for ( j = start, s = 0; j < end; j++ )
659: s += MUL_WEIGHT(nd_work_vector[j],j);
660: if ( s > 0 ) return 1;
661: else if ( s < 0 ) return -1;
662: for ( j = end-1; j >= start; j-- )
663: if ( nd_work_vector[j] < 0 ) return 1;
664: else if ( nd_work_vector[j] > 0 ) return -1;
665: break;
666: case 1:
667: end = start+len;
668: for ( j = start, s = 0; j < end; j++ )
669: s += MUL_WEIGHT(nd_work_vector[j],j);
670: if ( s > 0 ) return 1;
671: else if ( s < 0 ) return -1;
672: for ( j = start; j < end; j++ )
673: if ( nd_work_vector[j] > 0 ) return 1;
674: else if ( nd_work_vector[j] < 0 ) return -1;
675: break;
676: case 2:
677: end = start+len;
678: for ( j = start; j < end; j++ )
679: if ( nd_work_vector[j] > 0 ) return 1;
680: else if ( nd_work_vector[j] < 0 ) return -1;
681: break;
682: }
683: break;
684: }
685: }
686: return 0;
687: }
688:
689: /* TDH -> WW -> TD-> RL */
690:
691: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
692: {
693: int i,m,e1,e2;
694:
695: if ( TD(d1) > TD(d2) ) return 1;
696: else if ( TD(d1) < TD(d2) ) return -1;
697: m = nd_nvar>>1;
698: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
699: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
700: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
701: }
702: if ( e1 > e2 ) return 1;
703: else if ( e1 < e2 ) return -1;
704: return ndl_lex_compare(d1,d2);
705: }
706:
1.21 noro 707: // common function for module glex and grlex comparison
708: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 709: {
1.21 noro 710: int c;
1.1 noro 711:
1.21 noro 712: switch ( nd_module_ordtype ) {
713: case 0:
714: if ( TD(d1) > TD(d2) ) return 1;
715: else if ( TD(d1) < TD(d2) ) return -1;
716: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
717: else if ( MPOS(d1) < MPOS(d2) ) return 1;
718: else if ( MPOS(d1) > MPOS(d2) ) return -1;
719: else return 0;
720: break;
1.1 noro 721:
1.21 noro 722: case 1:
1.19 noro 723: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
724: if ( TD(d1) > TD(d2) ) return 1;
725: else if ( TD(d1) < TD(d2) ) return -1;
726: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
727: if ( MPOS(d1) < MPOS(d2) ) return 1;
728: else if ( MPOS(d1) > MPOS(d2) ) return -1;
729: }
730: if ( MPOS(d1) < MPOS(d2) ) return 1;
731: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 732: else if ( TD(d1) > TD(d2) ) return 1;
733: else if ( TD(d1) < TD(d2) ) return -1;
734: else return ndl_lex_compare(d1,d2);
735: break;
1.1 noro 736:
1.21 noro 737: case 2: // weight -> POT
738: if ( TD(d1) > TD(d2) ) return 1;
739: else if ( TD(d1) < TD(d2) ) return -1;
740: else if ( MPOS(d1) < MPOS(d2) ) return 1;
741: else if ( MPOS(d1) > MPOS(d2) ) return -1;
742: else return ndl_lex_compare(d1,d2);
743: break;
1.1 noro 744:
1.21 noro 745: default:
746: error("ndl_module_glex_compare : invalid module_ordtype");
747: }
1.1 noro 748: }
749:
1.21 noro 750: // common for module comparison
751: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 752: {
1.21 noro 753: int c;
1.1 noro 754:
1.21 noro 755: switch ( nd_module_ordtype ) {
756: case 0:
1.23 ! noro 757: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 758: else if ( MPOS(d1) > MPOS(d2) ) return -1;
759: else if ( MPOS(d1) < MPOS(d2) ) return 1;
760: else return 0;
761: break;
1.1 noro 762:
1.21 noro 763: case 1:
764: if ( MPOS(d1) < MPOS(d2) ) return 1;
765: else if ( MPOS(d1) > MPOS(d2) ) return -1;
766: else return (*ndl_base_compare_function)(d1,d2);
767: break;
1.1 noro 768:
1.21 noro 769: case 2: // weight -> POT
770: if ( TD(d1) > TD(d2) ) return 1;
771: else if ( TD(d1) < TD(d2) ) return -1;
772: else if ( MPOS(d1) < MPOS(d2) ) return 1;
773: else if ( MPOS(d1) > MPOS(d2) ) return -1;
774: else return (*ndl_base_compare_function)(d1,d2);
775: break;
1.1 noro 776:
1.21 noro 777: default:
778: error("ndl_module_compare : invalid module_ordtype");
779: }
1.1 noro 780: }
781:
1.21 noro 782: extern DMMstack dmm_stack;
783: void _addtodl(int n,DL d1,DL d2);
784: int _eqdl(int n,DL d1,DL d2);
785:
786: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
787: {
788: int pos1,pos2,t,j;
789: DMM *in;
790: DMMstack s;
791: static DL d1=0;
792: static DL d2=0;
793: static int dlen=0;
794:
795: pos1 = MPOS(m1); pos2 = MPOS(m2);
796: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
797: if ( nd_nvar > dlen ) {
798: NEWDL(d1,nd_nvar);
799: NEWDL(d2,nd_nvar);
800: dlen = nd_nvar;
801: }
802: d1->td = TD(m1);
803: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
804: d2->td = TD(m2);
805: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
806: for ( s = dmm_stack; s; s = NEXT(s) ) {
807: in = s->in;
808: _addtodl(nd_nvar,in[pos1]->dl,d1);
809: _addtodl(nd_nvar,in[pos2]->dl,d2);
810: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
811: if ( pos1 < pos2 ) return 1;
812: else if ( pos1 > pos2 ) return -1;
813: else return 0;
814: }
815: pos1 = in[pos1]->pos;
816: pos2 = in[pos2]->pos;
817: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
818: }
819: // comparison by the bottom order
820: LAST:
821: switch ( nd_base_ordtype ) {
822: case 0:
823: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
824: if ( t ) return t;
825: else if ( pos1 < pos2 ) return 1;
826: else if ( pos1 > pos2 ) return -1;
827: else return 0;
828: break;
829: case 1:
830: if ( pos1 < pos2 ) return 1;
831: else if ( pos1 > pos2 ) return -1;
832: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
833: break;
834: case 2:
835: if ( d1->td > d2->td ) return 1;
836: else if ( d1->td < d2->td ) return -1;
837: else if ( pos1 < pos2 ) return 1;
838: else if ( pos1 > pos2 ) return -1;
839: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
840: break;
841: default:
842: error("ndl_schreyer_compare : invalid base ordtype");
843: }
1.1 noro 844: }
845:
846: INLINE int ndl_equal(UINT *d1,UINT *d2)
847: {
848: int i;
849:
850: switch ( nd_wpd ) {
851: case 2:
852: if ( TD(d2) != TD(d1) ) return 0;
853: if ( d2[1] != d1[1] ) return 0;
854: return 1;
855: break;
856: case 3:
857: if ( TD(d2) != TD(d1) ) return 0;
858: if ( d2[1] != d1[1] ) return 0;
859: if ( d2[2] != d1[2] ) return 0;
860: return 1;
861: break;
862: default:
863: for ( i = 0; i < nd_wpd; i++ )
864: if ( *d1++ != *d2++ ) return 0;
865: return 1;
866: break;
867: }
868: }
869:
870: INLINE void ndl_copy(UINT *d1,UINT *d2)
871: {
872: int i;
873:
874: switch ( nd_wpd ) {
875: case 2:
876: TD(d2) = TD(d1);
877: d2[1] = d1[1];
878: break;
879: case 3:
880: TD(d2) = TD(d1);
881: d2[1] = d1[1];
882: d2[2] = d1[2];
883: break;
884: default:
885: for ( i = 0; i < nd_wpd; i++ )
886: d2[i] = d1[i];
887: break;
888: }
889: }
890:
891: INLINE void ndl_zero(UINT *d)
892: {
893: int i;
894: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
895: }
896:
897: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
898: {
899: int i;
900:
901: if ( nd_module ) {
902: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
903: error("ndl_add : invalid operation");
904: }
905: #if 1
906: switch ( nd_wpd ) {
907: case 2:
908: TD(d) = TD(d1)+TD(d2);
909: d[1] = d1[1]+d2[1];
910: break;
911: case 3:
912: TD(d) = TD(d1)+TD(d2);
913: d[1] = d1[1]+d2[1];
914: d[2] = d1[2]+d2[2];
915: break;
916: default:
917: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
918: break;
919: }
920: #else
921: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
922: #endif
923: }
924:
925: /* d1 += d2 */
926: INLINE void ndl_addto(UINT *d1,UINT *d2)
927: {
928: int i;
929:
930: if ( nd_module ) {
931: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
932: error("ndl_addto : invalid operation");
933: }
934: #if 1
935: switch ( nd_wpd ) {
936: case 2:
937: TD(d1) += TD(d2);
938: d1[1] += d2[1];
939: break;
940: case 3:
941: TD(d1) += TD(d2);
942: d1[1] += d2[1];
943: d1[2] += d2[2];
944: break;
945: default:
946: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
947: break;
948: }
949: #else
950: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
951: #endif
952: }
953:
954: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
955: {
956: int i;
957:
958: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
959: }
960:
961: int ndl_disjoint(UINT *d1,UINT *d2)
962: {
963: UINT t1,t2,u,u1,u2;
964: int i,j;
965:
966: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
967: #if USE_UNROLL
968: switch ( nd_bpe ) {
969: case 3:
970: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
971: u1 = d1[i]; u2 = d2[i];
972: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
973: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
974: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
975: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
976: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
977: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
978: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
979: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
980: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
981: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
982: }
983: return 1;
984: break;
985: case 4:
986: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
987: u1 = d1[i]; u2 = d2[i];
988: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
989: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
990: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
991: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
992: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
993: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
994: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
995: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
996: }
997: return 1;
998: break;
999: case 6:
1000: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1001: u1 = d1[i]; u2 = d2[i];
1002: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1003: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1004: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1005: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1007: }
1008: return 1;
1009: break;
1010: case 8:
1011: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1012: u1 = d1[i]; u2 = d2[i];
1013: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1015: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1017: }
1018: return 1;
1019: break;
1020: case 16:
1021: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1022: u1 = d1[i]; u2 = d2[i];
1023: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1025: }
1026: return 1;
1027: break;
1028: case 32:
1029: for ( i = nd_exporigin; i < nd_wpd; i++ )
1030: if ( d1[i] && d2[i] ) return 0;
1031: return 1;
1032: break;
1033: default:
1034: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1035: u1 = d1[i]; u2 = d2[i];
1036: for ( j = 0; j < nd_epw; j++ ) {
1037: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1038: u1 >>= nd_bpe; u2 >>= nd_bpe;
1039: }
1040: }
1041: return 1;
1042: break;
1043: }
1044: #else
1045: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1046: u1 = d1[i]; u2 = d2[i];
1047: for ( j = 0; j < nd_epw; j++ ) {
1048: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1049: u1 >>= nd_bpe; u2 >>= nd_bpe;
1050: }
1051: }
1052: return 1;
1053: #endif
1054: }
1055:
1056: int ndl_check_bound(UINT *d1,UINT *d2)
1057: {
1058: UINT u2;
1059: int i,j,ind,k;
1060:
1061: ind = 0;
1062: #if USE_UNROLL
1063: switch ( nd_bpe ) {
1064: case 3:
1065: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1066: u2 = d2[i];
1067: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1068: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1069: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1070: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1071: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1072: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1073: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1074: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1075: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1076: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1077: }
1078: return 0;
1079: break;
1080: case 4:
1081: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1082: u2 = d2[i];
1083: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1084: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1085: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1086: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1087: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1088: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1089: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1090: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1091: }
1092: return 0;
1093: break;
1094: case 6:
1095: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1096: u2 = d2[i];
1097: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1098: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1099: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1100: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1101: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1102: }
1103: return 0;
1104: break;
1105: case 8:
1106: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1107: u2 = d2[i];
1108: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1109: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1110: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1111: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1112: }
1113: return 0;
1114: break;
1115: case 16:
1116: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1117: u2 = d2[i];
1118: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1119: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1120: }
1121: return 0;
1122: break;
1123: case 32:
1124: for ( i = nd_exporigin; i < nd_wpd; i++ )
1125: if ( d1[i]+d2[i]<d1[i] ) return 1;
1126: return 0;
1127: break;
1128: default:
1129: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1130: u2 = d2[i];
1131: k = (nd_epw-1)*nd_bpe;
1132: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1133: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1134: }
1135: return 0;
1136: break;
1137: }
1138: #else
1139: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1140: u2 = d2[i];
1141: k = (nd_epw-1)*nd_bpe;
1142: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1143: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1144: }
1145: return 0;
1146: #endif
1147: }
1148:
1149: int ndl_check_bound2(int index,UINT *d2)
1150: {
1151: return ndl_check_bound(nd_bound[index],d2);
1152: }
1153:
1154: INLINE int ndl_hash_value(UINT *d)
1155: {
1156: int i;
1.11 noro 1157: UINT r;
1.1 noro 1158:
1159: r = 0;
1160: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1161: r = (r*1511+d[i]);
1.11 noro 1162: r %= REDTAB_LEN;
1.1 noro 1163: return r;
1164: }
1165:
1166: INLINE int ndl_find_reducer(UINT *dg)
1167: {
1168: RHist r;
1169: int d,k,i;
1170:
1171: d = ndl_hash_value(dg);
1172: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1173: if ( ndl_equal(dg,DL(r)) ) {
1174: if ( k > 0 ) nd_notfirst++;
1175: nd_found++;
1176: return r->index;
1177: }
1178: }
1179: if ( Reverse )
1180: for ( i = nd_psn-1; i >= 0; i-- ) {
1181: r = nd_psh[i];
1182: if ( ndl_reducible(dg,DL(r)) ) {
1183: nd_create++;
1184: nd_append_red(dg,i);
1185: return i;
1186: }
1187: }
1188: else
1189: for ( i = 0; i < nd_psn; i++ ) {
1190: r = nd_psh[i];
1191: if ( ndl_reducible(dg,DL(r)) ) {
1192: nd_create++;
1193: nd_append_red(dg,i);
1194: return i;
1195: }
1196: }
1197: return -1;
1198: }
1199:
1200: ND nd_merge(ND p1,ND p2)
1201: {
1202: int n,c;
1203: int t,can,td1,td2;
1204: ND r;
1205: NM m1,m2,mr0,mr,s;
1206:
1207: if ( !p1 ) return p2;
1208: else if ( !p2 ) return p1;
1209: else {
1210: can = 0;
1211: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1212: c = DL_COMPARE(DL(m1),DL(m2));
1213: switch ( c ) {
1214: case 0:
1215: s = m1; m1 = NEXT(m1);
1216: can++; NEXTNM2(mr0,mr,s);
1217: s = m2; m2 = NEXT(m2); FREENM(s);
1218: break;
1219: case 1:
1220: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1221: break;
1222: case -1:
1223: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1224: break;
1225: }
1226: }
1227: if ( !mr0 )
1228: if ( m1 ) mr0 = m1;
1229: else if ( m2 ) mr0 = m2;
1230: else return 0;
1231: else if ( m1 ) NEXT(mr) = m1;
1232: else if ( m2 ) NEXT(mr) = m2;
1233: else NEXT(mr) = 0;
1234: BDY(p1) = mr0;
1235: SG(p1) = MAX(SG(p1),SG(p2));
1236: LEN(p1) = LEN(p1)+LEN(p2)-can;
1237: FREEND(p2);
1238: return p1;
1239: }
1240: }
1241:
1242: ND nd_add(int mod,ND p1,ND p2)
1243: {
1244: int n,c;
1245: int t,can,td1,td2;
1246: ND r;
1247: NM m1,m2,mr0,mr,s;
1248:
1.11 noro 1249: Nnd_add++;
1.1 noro 1250: if ( !p1 ) return p2;
1251: else if ( !p2 ) return p1;
1252: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1253: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1254: else if ( !mod ) return nd_add_q(p1,p2);
1255: else {
1256: can = 0;
1257: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1258: c = DL_COMPARE(DL(m1),DL(m2));
1259: switch ( c ) {
1260: case 0:
1261: t = ((CM(m1))+(CM(m2))) - mod;
1262: if ( t < 0 ) t += mod;
1263: s = m1; m1 = NEXT(m1);
1264: if ( t ) {
1265: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1266: } else {
1267: can += 2; FREENM(s);
1268: }
1269: s = m2; m2 = NEXT(m2); FREENM(s);
1270: break;
1271: case 1:
1272: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1273: break;
1274: case -1:
1275: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1276: break;
1277: }
1278: }
1279: if ( !mr0 )
1280: if ( m1 ) mr0 = m1;
1281: else if ( m2 ) mr0 = m2;
1282: else return 0;
1283: else if ( m1 ) NEXT(mr) = m1;
1284: else if ( m2 ) NEXT(mr) = m2;
1285: else NEXT(mr) = 0;
1286: BDY(p1) = mr0;
1287: SG(p1) = MAX(SG(p1),SG(p2));
1288: LEN(p1) = LEN(p1)+LEN(p2)-can;
1289: FREEND(p2);
1290: return p1;
1291: }
1292: }
1293:
1294: /* XXX on opteron, the inlined manipulation of destructive additon of
1295: * two NM seems to make gcc optimizer get confused, so the part is
1296: * done in a function.
1297: */
1298:
1299: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1300: {
1301: NM s;
1302: P t;
1303: int can;
1304:
1305: addp(nd_vc,CP(*m1),CP(*m2),&t);
1306: s = *m1; *m1 = NEXT(*m1);
1307: if ( t ) {
1308: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1309: } else {
1310: can = 2; FREENM(s);
1311: }
1312: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1313: return can;
1314: }
1315:
1316: ND nd_add_q(ND p1,ND p2)
1317: {
1318: int n,c,can;
1319: ND r;
1320: NM m1,m2,mr0,mr,s;
1321: P t;
1322:
1323: if ( !p1 ) return p2;
1324: else if ( !p2 ) return p1;
1325: else {
1326: can = 0;
1327: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1328: c = DL_COMPARE(DL(m1),DL(m2));
1329: switch ( c ) {
1330: case 0:
1331: #if defined(__x86_64__)
1332: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1333: #else
1334: addp(nd_vc,CP(m1),CP(m2),&t);
1335: s = m1; m1 = NEXT(m1);
1336: if ( t ) {
1337: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1338: } else {
1339: can += 2; FREENM(s);
1340: }
1341: s = m2; m2 = NEXT(m2); FREENM(s);
1342: #endif
1343: break;
1344: case 1:
1345: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1346: break;
1347: case -1:
1348: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1349: break;
1350: }
1351: }
1352: if ( !mr0 )
1353: if ( m1 ) mr0 = m1;
1354: else if ( m2 ) mr0 = m2;
1355: else return 0;
1356: else if ( m1 ) NEXT(mr) = m1;
1357: else if ( m2 ) NEXT(mr) = m2;
1358: else NEXT(mr) = 0;
1359: BDY(p1) = mr0;
1360: SG(p1) = MAX(SG(p1),SG(p2));
1361: LEN(p1) = LEN(p1)+LEN(p2)-can;
1362: FREEND(p2);
1363: return p1;
1364: }
1365: }
1366:
1367: ND nd_add_sf(ND p1,ND p2)
1368: {
1369: int n,c,can;
1370: ND r;
1371: NM m1,m2,mr0,mr,s;
1372: int t;
1373:
1374: if ( !p1 ) return p2;
1375: else if ( !p2 ) return p1;
1376: else {
1377: can = 0;
1378: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1379: c = DL_COMPARE(DL(m1),DL(m2));
1380: switch ( c ) {
1381: case 0:
1382: t = _addsf(CM(m1),CM(m2));
1383: s = m1; m1 = NEXT(m1);
1384: if ( t ) {
1385: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1386: } else {
1387: can += 2; FREENM(s);
1388: }
1389: s = m2; m2 = NEXT(m2); FREENM(s);
1390: break;
1391: case 1:
1392: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1393: break;
1394: case -1:
1395: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1396: break;
1397: }
1398: }
1399: if ( !mr0 )
1400: if ( m1 ) mr0 = m1;
1401: else if ( m2 ) mr0 = m2;
1402: else return 0;
1403: else if ( m1 ) NEXT(mr) = m1;
1404: else if ( m2 ) NEXT(mr) = m2;
1405: else NEXT(mr) = 0;
1406: BDY(p1) = mr0;
1407: SG(p1) = MAX(SG(p1),SG(p2));
1408: LEN(p1) = LEN(p1)+LEN(p2)-can;
1409: FREEND(p2);
1410: return p1;
1411: }
1412: }
1413:
1414:
1415: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1416: {
1417: int c,c1,c2;
1418: Z cg,cred,gcd,tq;
1419: P cgp,credp,gcdp;
1420: Obj tr,tr1;
1421:
1422: if ( mod == -1 ) {
1423: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1424: *divp = (Obj)ONE;
1425: } else if ( mod == -2 ) {
1426: Z inv,t;
1427: divlf(ONE,HCZ(p),&inv);
1428: chsgnlf(HCZ(g),&t);
1429: mullf(inv,t,&CZ(mul));
1430: *divp = (Obj)ONE;
1431: } else if ( mod ) {
1432: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1433: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1434: *divp = (Obj)ONE;
1435: } else if ( nd_vc ) {
1436: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1437: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1438: chsgnp(cgp,&CP(mul));
1439: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1440: if ( dn ) {
1441: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1442: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1443: }
1444: *divp = (Obj)credp;
1445: } else {
1.6 noro 1446: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1447: chsgnz(cg,&CZ(mul));
1.1 noro 1448: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1449: if ( dn ) {
1450: mulz(dn->z,cred,&tq); dn->z = tq;
1451: }
1452: *divp = (Obj)cred;
1453: }
1454: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1455: }
1456:
1457: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1458: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1459: {
1460: NM m,mrd,tail;
1461: NM mul;
1462: int n,sugar,psugar,sugar0,stat,index;
1463: int c,c1,c2,dummy;
1464: RHist h;
1465: NDV p,red;
1466: Q cg,cred,gcd,tq,qq;
1467: Z iq;
1468: DP dmul;
1469: NODE node;
1470: LIST hist;
1471: double hmag;
1472: P tp,tp1;
1473: Obj tr,tr1,div;
1474: union oNDC hg;
1475: P cont;
1476:
1477: if ( !g ) {
1478: *rp = d;
1479: return 1;
1480: }
1481: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1482:
1483: sugar0 = sugar = SG(g);
1484: n = NV(g);
1485: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1486: if ( d )
1487: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1488: for ( ; g; ) {
1489: index = ndl_find_reducer(HDL(g));
1490: if ( index >= 0 ) {
1491: h = nd_psh[index];
1492: ndl_sub(HDL(g),DL(h),DL(mul));
1493: if ( ndl_check_bound2(index,DL(mul)) ) {
1494: nd_free(g); nd_free(d);
1495: return 0;
1496: }
1497: p = nd_demand ? ndv_load(index) : ps[index];
1498: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1499: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1500: if ( nd_gentrace ) {
1501: /* Trace=[div,index,mul,ONE] */
1.6 noro 1502: STOZ(index,iq);
1.1 noro 1503: nmtodp(mod,mul,&dmul);
1504: node = mknode(4,div,iq,dmul,ONE);
1505: }
1506: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1507: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1508: hg = HCU(g);
1509: nd_removecont2(d,g);
1.6 noro 1510: if ( nd_gentrace ) {
1.1 noro 1511: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1512: /* exact division */
1.1 noro 1513: cont = ndc_div(mod,hg,HCU(g));
1514: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1515: }
1516: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1517: }
1518: MKLIST(hist,node);
1519: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1520: } else if ( !full ) {
1521: *rp = g;
1522: return 1;
1523: } else {
1524: m = BDY(g);
1525: if ( NEXT(m) ) {
1526: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1527: } else {
1528: FREEND(g); g = 0;
1529: }
1530: if ( d ) {
1531: NEXT(tail)=m; tail=m; LEN(d)++;
1532: } else {
1533: MKND(n,m,1,d); tail = BDY(d);
1534: }
1535: }
1536: }
1537: if ( d ) SG(d) = sugar;
1538: *rp = d;
1539: return 1;
1540: }
1541:
1542: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1543: {
1544: int hindex,index;
1545: NDV p;
1546: ND u,d,red;
1547: NODE l;
1548: NM mul,m,mrd,tail;
1549: int sugar,psugar,n,h_reducible;
1550: PGeoBucket bucket;
1551: int c,c1,c2;
1552: Z cg,cred,gcd,zzz;
1553: RHist h;
1554: double hmag,gmag;
1555: int count = 0;
1556: int hcount = 0;
1557:
1558: if ( !g ) {
1559: *rp = 0;
1560: return 1;
1561: }
1562: sugar = SG(g);
1563: n = NV(g);
1.6 noro 1564: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1565: bucket = create_pbucket();
1566: add_pbucket(mod,bucket,g);
1567: d = 0;
1568: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1569: while ( 1 ) {
1570: if ( mod > 0 || mod == -1 )
1571: hindex = head_pbucket(mod,bucket);
1572: else if ( mod == -2 )
1573: hindex = head_pbucket_lf(bucket);
1574: else
1575: hindex = head_pbucket_q(bucket);
1576: if ( hindex < 0 ) {
1577: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1578: if ( d ) SG(d) = sugar;
1579: *rp = d;
1580: return 1;
1581: }
1582: g = bucket->body[hindex];
1583: index = ndl_find_reducer(HDL(g));
1584: if ( index >= 0 ) {
1585: count++;
1586: if ( !d ) hcount++;
1587: h = nd_psh[index];
1588: ndl_sub(HDL(g),DL(h),DL(mul));
1589: if ( ndl_check_bound2(index,DL(mul)) ) {
1590: nd_free(d);
1591: free_pbucket(bucket);
1592: *rp = 0;
1593: return 0;
1594: }
1595: p = ps[index];
1596: if ( mod == -1 )
1597: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1598: else if ( mod == -2 ) {
1599: Z inv,t;
1600: divlf(ONE,HCZ(p),&inv);
1601: chsgnlf(HCZ(g),&t);
1602: mullf(inv,t,&CZ(mul));
1603: } else if ( mod ) {
1604: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1605: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1606: } else {
1.6 noro 1607: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1608: chsgnz(cg,&CZ(mul));
1.1 noro 1609: nd_mul_c_q(d,(P)cred);
1610: mulq_pbucket(bucket,cred);
1611: g = bucket->body[hindex];
1.6 noro 1612: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1613: }
1614: red = ndv_mul_nm(mod,mul,p);
1615: bucket->body[hindex] = nd_remove_head(g);
1616: red = nd_remove_head(red);
1617: add_pbucket(mod,bucket,red);
1618: psugar = SG(p)+TD(DL(mul));
1619: sugar = MAX(sugar,psugar);
1620: if ( !mod && hmag && (gmag > hmag) ) {
1621: g = normalize_pbucket(mod,bucket);
1622: if ( !g ) {
1623: if ( d ) SG(d) = sugar;
1624: *rp = d;
1625: return 1;
1626: }
1627: nd_removecont2(d,g);
1.6 noro 1628: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1629: add_pbucket(mod,bucket,g);
1630: }
1631: } else if ( !full ) {
1632: g = normalize_pbucket(mod,bucket);
1633: if ( g ) SG(g) = sugar;
1634: *rp = g;
1635: return 1;
1636: } else {
1637: m = BDY(g);
1638: if ( NEXT(m) ) {
1639: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1640: } else {
1641: FREEND(g); g = 0;
1642: }
1643: bucket->body[hindex] = g;
1644: NEXT(m) = 0;
1645: if ( d ) {
1646: NEXT(tail)=m; tail=m; LEN(d)++;
1647: } else {
1648: MKND(n,m,1,d); tail = BDY(d);
1649: }
1650: }
1651: }
1652: }
1653:
1654: /* input : list of NDV, cand : list of NDV */
1655:
1656: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1657: {
1658: int n,i,stat;
1659: ND nf,d;
1660: NDV r;
1661: NODE t,s;
1662: union oNDC dn;
1663: Z q;
1664: LIST list;
1665:
1666: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1667: n = length(cand);
1668:
1669: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1670: /* membercheck : list is a subset of Id(cand) ? */
1671: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1672: again:
1673: nd_tracelist = 0;
1674: if ( nd_bpe > obpe )
1675: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1676: else
1677: r = (NDV)BDY(t);
1678: #if 0
1679: // moved to nd_f4_lf_trace()
1680: if ( m == -2 ) ndv_mod(m,r);
1681: #endif
1682: d = ndvtond(m,r);
1.6 noro 1683: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1684: if ( !stat ) {
1685: nd_reconstruct(0,0);
1686: goto again;
1687: } else if ( nf ) return 0;
1688: if ( nd_gentrace ) {
1689: nd_tracelist = reverse_node(nd_tracelist);
1690: MKLIST(list,nd_tracelist);
1.6 noro 1691: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1692: MKNODE(s,list,nd_alltracelist);
1693: nd_alltracelist = s; nd_tracelist = 0;
1694: }
1695: if ( DP_Print ) { printf("."); fflush(stdout); }
1696: }
1697: if ( DP_Print ) { printf("\n"); }
1698: return 1;
1699: }
1700:
1701: ND nd_remove_head(ND p)
1702: {
1703: NM m;
1704:
1705: m = BDY(p);
1706: if ( !NEXT(m) ) {
1707: FREEND(p); p = 0;
1708: } else {
1709: BDY(p) = NEXT(m); LEN(p)--;
1710: }
1711: FREENM(m);
1712: return p;
1713: }
1714:
1715: ND nd_separate_head(ND p,ND *head)
1716: {
1717: NM m,m0;
1718: ND r;
1719:
1720: m = BDY(p);
1721: if ( !NEXT(m) ) {
1722: *head = p; p = 0;
1723: } else {
1724: m0 = m;
1725: BDY(p) = NEXT(m); LEN(p)--;
1726: NEXT(m0) = 0;
1727: MKND(NV(p),m0,1,r);
1728: *head = r;
1729: }
1730: return p;
1731: }
1732:
1733: PGeoBucket create_pbucket()
1734: {
1735: PGeoBucket g;
1736:
1737: g = CALLOC(1,sizeof(struct oPGeoBucket));
1738: g->m = -1;
1739: return g;
1740: }
1741:
1742: void free_pbucket(PGeoBucket b) {
1743: int i;
1744:
1745: for ( i = 0; i <= b->m; i++ )
1746: if ( b->body[i] ) {
1747: nd_free(b->body[i]);
1748: b->body[i] = 0;
1749: }
1750: GCFREE(b);
1751: }
1752:
1753: void add_pbucket_symbolic(PGeoBucket g,ND d)
1754: {
1755: int l,i,k,m;
1756:
1757: if ( !d )
1758: return;
1759: l = LEN(d);
1760: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1761: /* 2^(k-1) < l <= 2^k (=m) */
1762: d = nd_merge(g->body[k],d);
1763: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1764: g->body[k] = 0;
1765: d = nd_merge(g->body[k+1],d);
1766: }
1767: g->body[k] = d;
1768: g->m = MAX(g->m,k);
1769: }
1770:
1771: void add_pbucket(int mod,PGeoBucket g,ND d)
1772: {
1773: int l,i,k,m;
1774:
1775: if ( !d )
1776: return;
1777: l = LEN(d);
1778: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1779: /* 2^(k-1) < l <= 2^k (=m) */
1780: d = nd_add(mod,g->body[k],d);
1781: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1782: g->body[k] = 0;
1783: d = nd_add(mod,g->body[k+1],d);
1784: }
1785: g->body[k] = d;
1786: g->m = MAX(g->m,k);
1787: }
1788:
1789: void mulq_pbucket(PGeoBucket g,Z c)
1790: {
1791: int k;
1792:
1793: for ( k = 0; k <= g->m; k++ )
1794: nd_mul_c_q(g->body[k],(P)c);
1795: }
1796:
1797: NM remove_head_pbucket_symbolic(PGeoBucket g)
1798: {
1799: int j,i,k,c;
1800: NM head;
1801:
1802: k = g->m;
1803: j = -1;
1804: for ( i = 0; i <= k; i++ ) {
1805: if ( !g->body[i] ) continue;
1806: if ( j < 0 ) j = i;
1807: else {
1808: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1809: if ( c > 0 )
1810: j = i;
1811: else if ( c == 0 )
1812: g->body[i] = nd_remove_head(g->body[i]);
1813: }
1814: }
1815: if ( j < 0 ) return 0;
1816: else {
1817: head = BDY(g->body[j]);
1818: if ( !NEXT(head) ) {
1819: FREEND(g->body[j]);
1820: g->body[j] = 0;
1821: } else {
1822: BDY(g->body[j]) = NEXT(head);
1823: LEN(g->body[j])--;
1824: }
1825: return head;
1826: }
1827: }
1828:
1829: int head_pbucket(int mod,PGeoBucket g)
1830: {
1831: int j,i,c,k,nv,sum;
1832: UINT *di,*dj;
1833: ND gi,gj;
1834:
1835: k = g->m;
1836: while ( 1 ) {
1837: j = -1;
1838: for ( i = 0; i <= k; i++ ) {
1839: if ( !(gi = g->body[i]) )
1840: continue;
1841: if ( j < 0 ) {
1842: j = i;
1843: gj = g->body[j];
1844: dj = HDL(gj);
1845: sum = HCM(gj);
1846: } else {
1847: c = DL_COMPARE(HDL(gi),dj);
1848: if ( c > 0 ) {
1849: if ( sum ) HCM(gj) = sum;
1850: else g->body[j] = nd_remove_head(gj);
1851: j = i;
1852: gj = g->body[j];
1853: dj = HDL(gj);
1854: sum = HCM(gj);
1855: } else if ( c == 0 ) {
1856: if ( mod == -1 )
1857: sum = _addsf(sum,HCM(gi));
1858: else {
1859: sum = sum+HCM(gi)-mod;
1860: if ( sum < 0 ) sum += mod;
1861: }
1862: g->body[i] = nd_remove_head(gi);
1863: }
1864: }
1865: }
1866: if ( j < 0 ) return -1;
1867: else if ( sum ) {
1868: HCM(gj) = sum;
1869: return j;
1870: } else
1871: g->body[j] = nd_remove_head(gj);
1872: }
1873: }
1874:
1875: int head_pbucket_q(PGeoBucket g)
1876: {
1877: int j,i,c,k,nv;
1878: Z sum,t;
1879: ND gi,gj;
1880:
1881: k = g->m;
1882: while ( 1 ) {
1883: j = -1;
1884: for ( i = 0; i <= k; i++ ) {
1885: if ( !(gi = g->body[i]) ) continue;
1886: if ( j < 0 ) {
1887: j = i;
1888: gj = g->body[j];
1.6 noro 1889: sum = HCZ(gj);
1.1 noro 1890: } else {
1891: nv = NV(gi);
1892: c = DL_COMPARE(HDL(gi),HDL(gj));
1893: if ( c > 0 ) {
1.6 noro 1894: if ( sum ) HCZ(gj) = sum;
1.1 noro 1895: else g->body[j] = nd_remove_head(gj);
1896: j = i;
1897: gj = g->body[j];
1.6 noro 1898: sum = HCZ(gj);
1.1 noro 1899: } else if ( c == 0 ) {
1.6 noro 1900: addz(sum,HCZ(gi),&t);
1.1 noro 1901: sum = t;
1902: g->body[i] = nd_remove_head(gi);
1903: }
1904: }
1905: }
1906: if ( j < 0 ) return -1;
1907: else if ( sum ) {
1.6 noro 1908: HCZ(gj) = sum;
1.1 noro 1909: return j;
1910: } else
1911: g->body[j] = nd_remove_head(gj);
1912: }
1913: }
1914:
1915: int head_pbucket_lf(PGeoBucket g)
1916: {
1917: int j,i,c,k,nv;
1918: Z sum,t;
1919: ND gi,gj;
1920:
1921: k = g->m;
1922: while ( 1 ) {
1923: j = -1;
1924: for ( i = 0; i <= k; i++ ) {
1925: if ( !(gi = g->body[i]) ) continue;
1926: if ( j < 0 ) {
1927: j = i;
1928: gj = g->body[j];
1929: sum = HCZ(gj);
1930: } else {
1931: nv = NV(gi);
1932: c = DL_COMPARE(HDL(gi),HDL(gj));
1933: if ( c > 0 ) {
1934: if ( sum ) HCZ(gj) = sum;
1935: else g->body[j] = nd_remove_head(gj);
1936: j = i;
1937: gj = g->body[j];
1938: sum = HCZ(gj);
1939: } else if ( c == 0 ) {
1940: addlf(sum,HCZ(gi),&t);
1941: sum = t;
1942: g->body[i] = nd_remove_head(gi);
1943: }
1944: }
1945: }
1946: if ( j < 0 ) return -1;
1947: else if ( sum ) {
1948: HCZ(gj) = sum;
1949: return j;
1950: } else
1951: g->body[j] = nd_remove_head(gj);
1952: }
1953: }
1954:
1955: ND normalize_pbucket(int mod,PGeoBucket g)
1956: {
1957: int i;
1958: ND r,t;
1959:
1960: r = 0;
1961: for ( i = 0; i <= g->m; i++ ) {
1962: r = nd_add(mod,r,g->body[i]);
1963: g->body[i] = 0;
1964: }
1965: g->m = -1;
1966: return r;
1967: }
1968:
1969: #if 0
1970: void register_hcf(NDV p)
1971: {
1972: DCP dc,t;
1973: P hc,h;
1974: int c;
1975: NODE l,l1,prev;
1976:
1977: hc = p->body->c.p;
1978: if ( !nd_vc || NUM(hc) ) return;
1979: fctrp(nd_vc,hc,&dc);
1980: for ( t = dc; t; t = NEXT(t) ) {
1981: h = t->c;
1982: if ( NUM(h) ) continue;
1983: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1984: c = compp(nd_vc,h,(P)BDY(l));
1985: if ( c >= 0 ) break;
1986: }
1987: if ( !l || c > 0 ) {
1988: MKNODE(l1,h,l);
1989: if ( !prev )
1990: nd_hcf = l1;
1991: else
1992: NEXT(prev) = l1;
1993: }
1994: }
1995: }
1996: #else
1997: void register_hcf(NDV p)
1998: {
1999: DCP dc,t;
2000: P hc,h,q;
2001: Q dmy;
2002: int c;
2003: NODE l,l1,prev;
2004:
2005: hc = p->body->c.p;
2006: if ( NUM(hc) ) return;
2007: ptozp(hc,1,&dmy,&h);
2008: #if 1
2009: for ( l = nd_hcf; l; l = NEXT(l) ) {
2010: while ( 1 ) {
2011: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2012: else break;
2013: }
2014: }
2015: if ( NUM(h) ) return;
2016: #endif
2017: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2018: c = compp(nd_vc,h,(P)BDY(l));
2019: if ( c >= 0 ) break;
2020: }
2021: if ( !l || c > 0 ) {
2022: MKNODE(l1,h,l);
2023: if ( !prev )
2024: nd_hcf = l1;
2025: else
2026: NEXT(prev) = l1;
2027: }
2028: }
2029: #endif
2030:
2031: int do_diagonalize(int sugar,int m)
2032: {
1.6 noro 2033: int i,nh,stat;
2034: NODE r,g,t;
2035: ND h,nf,s,head;
2036: NDV nfv;
2037: Q q;
2038: P nm,nmp,dn,mnp,dnp,cont,cont1;
2039: union oNDC hc;
2040: NODE node;
2041: LIST l;
2042: Z iq;
1.1 noro 2043:
1.6 noro 2044: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2045: if ( nd_gentrace ) {
2046: /* Trace = [1,index,1,1] */
2047: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2048: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2049: }
2050: if ( nd_demand )
2051: nfv = ndv_load(i);
2052: else
2053: nfv = nd_ps[i];
2054: s = ndvtond(m,nfv);
2055: s = nd_separate_head(s,&head);
2056: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2057: if ( !stat ) return 0;
2058: ndv_free(nfv);
2059: hc = HCU(nf); nd_removecont(m,nf);
2060: /* exact division */
2061: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2062: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2063: nfv = ndtondv(m,nf);
2064: nd_free(nf);
2065: nd_bound[i] = ndv_compute_bound(nfv);
2066: if ( !m ) register_hcf(nfv);
2067: if ( nd_demand ) {
2068: ndv_save(nfv,i);
2069: ndv_free(nfv);
2070: } else
2071: nd_ps[i] = nfv;
2072: }
2073: return 1;
1.1 noro 2074: }
2075:
2076: LIST compute_splist()
2077: {
2078: NODE g,tn0,tn,node;
2079: LIST l0;
2080: ND_pairs d,t;
2081: int i;
2082: Z i1,i2;
2083:
2084: g = 0; d = 0;
2085: for ( i = 0; i < nd_psn; i++ ) {
2086: d = update_pairs(d,g,i,0);
2087: g = update_base(g,i);
2088: }
2089: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2090: NEXTNODE(tn0,tn);
1.6 noro 2091: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2092: node = mknode(2,i1,i2); MKLIST(l0,node);
2093: BDY(tn) = l0;
2094: }
2095: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2096: return l0;
2097: }
2098:
2099: /* return value = 0 => input is not a GB */
2100:
2101: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2102: {
1.6 noro 2103: int i,nh,sugar,stat;
2104: NODE r,g,t;
2105: ND_pairs d;
2106: ND_pairs l;
2107: ND h,nf,s,head,nf1;
2108: NDV nfv;
2109: Z q;
2110: union oNDC dn,hc;
2111: int diag_count = 0;
2112: P cont;
2113: LIST list;
2114:
1.11 noro 2115: Nnd_add = 0;
1.6 noro 2116: g = 0; d = 0;
2117: for ( i = 0; i < nd_psn; i++ ) {
2118: d = update_pairs(d,g,i,gensyz);
2119: g = update_base(g,i);
2120: }
2121: sugar = 0;
2122: while ( d ) {
1.1 noro 2123: again:
1.6 noro 2124: l = nd_minp(d,&d);
2125: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2126: if ( SG(l) != sugar ) {
2127: if ( ishomo ) {
2128: diag_count = 0;
2129: stat = do_diagonalize(sugar,m);
1.1 noro 2130: if ( !stat ) {
1.6 noro 2131: NEXT(l) = d; d = l;
2132: d = nd_reconstruct(0,d);
2133: goto again;
1.1 noro 2134: }
1.6 noro 2135: }
2136: sugar = SG(l);
2137: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2138: }
2139: stat = nd_sp(m,0,l,&h);
2140: if ( !stat ) {
2141: NEXT(l) = d; d = l;
2142: d = nd_reconstruct(0,d);
2143: goto again;
2144: }
1.1 noro 2145: #if USE_GEOBUCKET
1.6 noro 2146: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2147: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2148: #else
1.6 noro 2149: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2150: #endif
1.6 noro 2151: if ( !stat ) {
2152: NEXT(l) = d; d = l;
2153: d = nd_reconstruct(0,d);
2154: goto again;
2155: } else if ( nf ) {
2156: if ( checkonly || gensyz ) return 0;
1.1 noro 2157: if ( nd_newelim ) {
2158: if ( nd_module ) {
2159: if ( MPOS(HDL(nf)) > 1 ) return 0;
2160: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2161: }
1.6 noro 2162: if ( DP_Print ) { printf("+"); fflush(stdout); }
2163: hc = HCU(nf);
2164: nd_removecont(m,nf);
2165: if ( !m && nd_nalg ) {
2166: nd_monic(0,&nf);
2167: nd_removecont(m,nf);
2168: }
2169: if ( nd_gentrace ) {
2170: /* exact division */
1.1 noro 2171: cont = ndc_div(m,hc,HCU(nf));
2172: if ( m || !UNIQ(cont) ) {
1.6 noro 2173: t = mknode(4,NULLP,NULLP,NULLP,cont);
2174: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2175: nd_tracelist = t;
2176: }
2177: }
1.6 noro 2178: nfv = ndtondv(m,nf); nd_free(nf);
2179: nh = ndv_newps(m,nfv,0,0);
2180: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2181: diag_count = 0;
2182: stat = do_diagonalize(sugar,m);
2183: if ( !stat ) {
2184: NEXT(l) = d; d = l;
2185: d = nd_reconstruct(1,d);
2186: goto again;
1.1 noro 2187: }
1.6 noro 2188: }
2189: d = update_pairs(d,g,nh,0);
2190: g = update_base(g,nh);
2191: FREENDP(l);
2192: } else {
2193: if ( nd_gentrace && gensyz ) {
2194: nd_tracelist = reverse_node(nd_tracelist);
2195: MKLIST(list,nd_tracelist);
2196: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2197: MKNODE(t,list,nd_alltracelist);
2198: nd_alltracelist = t; nd_tracelist = 0;
2199: }
2200: if ( DP_Print ) { printf("."); fflush(stdout); }
2201: FREENDP(l);
2202: }
2203: }
2204: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2205: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.1 noro 2206: return g;
2207: }
2208:
2209: /* splist = [[i1,i2],...] */
2210:
2211: int check_splist(int m,NODE splist)
2212: {
2213: NODE t,p;
2214: ND_pairs d,r,l;
2215: int stat;
2216: ND h,nf;
2217:
2218: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2219: p = BDY((LIST)BDY(t));
1.6 noro 2220: NEXTND_pairs(d,r);
2221: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2222: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2223: SG(r) = TD(LCM(r)); /* XXX */
2224: }
2225: if ( d ) NEXT(r) = 0;
2226:
1.6 noro 2227: while ( d ) {
1.1 noro 2228: again:
1.6 noro 2229: l = nd_minp(d,&d);
2230: stat = nd_sp(m,0,l,&h);
2231: if ( !stat ) {
2232: NEXT(l) = d; d = l;
2233: d = nd_reconstruct(0,d);
2234: goto again;
2235: }
2236: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2237: if ( !stat ) {
2238: NEXT(l) = d; d = l;
2239: d = nd_reconstruct(0,d);
2240: goto again;
2241: } else if ( nf ) return 0;
1.1 noro 2242: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2243: }
1.1 noro 2244: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2245: return 1;
2246: }
2247:
2248: int check_splist_f4(int m,NODE splist)
2249: {
2250: UINT *s0vect;
1.6 noro 2251: PGeoBucket bucket;
1.1 noro 2252: NODE p,rp0,t;
2253: ND_pairs d,r,l,ll;
2254: int col,stat;
2255:
2256: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2257: p = BDY((LIST)BDY(t));
1.6 noro 2258: NEXTND_pairs(d,r);
2259: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2260: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2261: SG(r) = TD(LCM(r)); /* XXX */
2262: }
2263: if ( d ) NEXT(r) = 0;
2264:
1.6 noro 2265: while ( d ) {
2266: l = nd_minsugarp(d,&d);
2267: bucket = create_pbucket();
2268: stat = nd_sp_f4(m,0,l,bucket);
2269: if ( !stat ) {
2270: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2271: NEXT(ll) = d; d = l;
2272: d = nd_reconstruct(0,d);
2273: continue;
2274: }
2275: if ( bucket->m < 0 ) continue;
2276: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2277: if ( !col ) {
2278: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2279: NEXT(ll) = d; d = l;
2280: d = nd_reconstruct(0,d);
2281: continue;
1.1 noro 2282: }
1.6 noro 2283: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2284: }
2285: return 1;
1.1 noro 2286: }
2287:
2288: int do_diagonalize_trace(int sugar,int m)
2289: {
1.6 noro 2290: int i,nh,stat;
2291: NODE r,g,t;
2292: ND h,nf,nfq,s,head;
2293: NDV nfv,nfqv;
2294: Q q,den,num;
2295: union oNDC hc;
2296: NODE node;
2297: LIST l;
2298: Z iq;
2299: P cont,cont1;
1.1 noro 2300:
1.6 noro 2301: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2302: if ( nd_gentrace ) {
2303: /* Trace = [1,index,1,1] */
2304: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2305: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2306: }
2307: /* for nd_ps */
2308: s = ndvtond(m,nd_ps[i]);
2309: s = nd_separate_head(s,&head);
2310: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2311: if ( !stat ) return 0;
2312: nf = nd_add(m,head,nf);
2313: ndv_free(nd_ps[i]);
2314: nd_ps[i] = ndtondv(m,nf);
2315: nd_free(nf);
2316:
2317: /* for nd_ps_trace */
2318: if ( nd_demand )
2319: nfv = ndv_load(i);
2320: else
2321: nfv = nd_ps_trace[i];
2322: s = ndvtond(0,nfv);
2323: s = nd_separate_head(s,&head);
2324: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2325: if ( !stat ) return 0;
2326: ndv_free(nfv);
2327: hc = HCU(nf); nd_removecont(0,nf);
2328: /* exact division */
1.1 noro 2329: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2330: if ( nd_gentrace ) finalize_tracelist(i,cont);
2331: nfv = ndtondv(0,nf);
2332: nd_free(nf);
2333: nd_bound[i] = ndv_compute_bound(nfv);
2334: register_hcf(nfv);
2335: if ( nd_demand ) {
2336: ndv_save(nfv,i);
2337: ndv_free(nfv);
2338: } else
2339: nd_ps_trace[i] = nfv;
2340: }
2341: return 1;
1.1 noro 2342: }
2343:
2344: static struct oEGT eg_invdalg;
2345: struct oEGT eg_le;
2346:
2347: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2348: {
2349: NODE tn;
2350: P p1;
2351:
2352: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2353: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2354: }
2355: *r = p;
2356: }
2357:
2358: NODE nd_gb_trace(int m,int ishomo,int **indp)
2359: {
1.6 noro 2360: int i,nh,sugar,stat;
2361: NODE r,g,t;
2362: ND_pairs d;
2363: ND_pairs l;
2364: ND h,nf,nfq,s,head;
2365: NDV nfv,nfqv;
2366: Z q,den,num;
2367: P hc;
2368: union oNDC dn,hnfq;
2369: struct oEGT eg_monic,egm0,egm1;
2370: int diag_count = 0;
2371: P cont;
2372: LIST list;
2373:
2374: init_eg(&eg_monic);
2375: init_eg(&eg_invdalg);
2376: init_eg(&eg_le);
2377: g = 0; d = 0;
2378: for ( i = 0; i < nd_psn; i++ ) {
2379: d = update_pairs(d,g,i,0);
2380: g = update_base(g,i);
2381: }
2382: sugar = 0;
2383: while ( d ) {
1.1 noro 2384: again:
1.6 noro 2385: l = nd_minp(d,&d);
2386: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2387: if ( SG(l) != sugar ) {
1.1 noro 2388: #if 1
1.6 noro 2389: if ( ishomo ) {
2390: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2391: stat = do_diagonalize_trace(sugar,m);
2392: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2393: diag_count = 0;
1.1 noro 2394: if ( !stat ) {
1.6 noro 2395: NEXT(l) = d; d = l;
2396: d = nd_reconstruct(1,d);
2397: goto again;
1.1 noro 2398: }
1.6 noro 2399: }
2400: #endif
2401: sugar = SG(l);
2402: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2403: }
2404: stat = nd_sp(m,0,l,&h);
2405: if ( !stat ) {
2406: NEXT(l) = d; d = l;
2407: d = nd_reconstruct(1,d);
2408: goto again;
2409: }
1.1 noro 2410: #if USE_GEOBUCKET
1.6 noro 2411: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2412: #else
1.6 noro 2413: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2414: #endif
1.6 noro 2415: if ( !stat ) {
2416: NEXT(l) = d; d = l;
2417: d = nd_reconstruct(1,d);
2418: goto again;
2419: } else if ( nf ) {
2420: if ( nd_demand ) {
2421: nfqv = ndv_load(nd_psn);
2422: nfq = ndvtond(0,nfqv);
2423: } else
2424: nfq = 0;
2425: if ( !nfq ) {
2426: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2427: NEXT(l) = d; d = l;
2428: d = nd_reconstruct(1,d);
2429: goto again;
2430: }
2431: }
2432: if ( nfq ) {
2433: /* m|HC(nfq) => failure */
2434: if ( nd_vc ) {
2435: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2436: } else
2437: q = HCZ(nfq);
2438: if ( !remqi((Q)q,m) ) return 0;
2439:
2440: if ( DP_Print ) { printf("+"); fflush(stdout); }
2441: hnfq = HCU(nfq);
2442: if ( nd_nalg ) {
2443: /* m|DN(HC(nf)^(-1)) => failure */
2444: get_eg(&egm0);
2445: if ( !nd_monic(m,&nfq) ) return 0;
2446: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2447: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2448: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2449: } else {
2450: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2451: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2452: }
2453: if ( nd_gentrace ) {
2454: /* exact division */
2455: cont = ndc_div(0,hnfq,HCU(nfqv));
2456: if ( !UNIQ(cont) ) {
2457: t = mknode(4,NULLP,NULLP,NULLP,cont);
2458: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2459: nd_tracelist = t;
2460: }
2461: }
2462: nh = ndv_newps(0,nfv,nfqv,0);
2463: if ( ishomo && ++diag_count == diag_period ) {
2464: diag_count = 0;
2465: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2466: stat = do_diagonalize_trace(sugar,m);
2467: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2468: if ( !stat ) {
1.1 noro 2469: NEXT(l) = d; d = l;
2470: d = nd_reconstruct(1,d);
2471: goto again;
1.6 noro 2472: }
1.1 noro 2473: }
1.6 noro 2474: d = update_pairs(d,g,nh,0);
2475: g = update_base(g,nh);
2476: } else {
2477: if ( DP_Print ) { printf("*"); fflush(stdout); }
2478: }
2479: } else {
2480: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2481: }
1.6 noro 2482: FREENDP(l);
2483: }
2484: if ( nd_nalg ) {
2485: if ( DP_Print ) {
2486: print_eg("monic",&eg_monic);
2487: print_eg("invdalg",&eg_invdalg);
2488: print_eg("le",&eg_le);
1.1 noro 2489: }
1.6 noro 2490: }
1.1 noro 2491: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2492: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2493: return g;
1.1 noro 2494: }
2495:
2496: int ndv_compare(NDV *p1,NDV *p2)
2497: {
2498: return DL_COMPARE(HDL(*p1),HDL(*p2));
2499: }
2500:
2501: int ndv_compare_rev(NDV *p1,NDV *p2)
2502: {
2503: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2504: }
2505:
2506: int ndvi_compare(NDVI p1,NDVI p2)
2507: {
2508: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2509: }
2510:
2511: int ndvi_compare_rev(NDVI p1,NDVI p2)
2512: {
2513: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2514: }
2515:
2516: NODE ndv_reduceall(int m,NODE f)
2517: {
2518: int i,j,n,stat;
2519: ND nf,g,head;
2520: NODE t,a0,a;
2521: union oNDC dn;
2522: Q q,num,den;
2523: NODE node;
2524: LIST l;
2525: Z iq,jq;
2526: int *perm;
2527: union oNDC hc;
2528: P cont,cont1;
2529:
2530: if ( nd_nora ) return f;
2531: n = length(f);
2532: ndv_setup(m,0,f,0,1);
2533: perm = (int *)MALLOC(n*sizeof(int));
2534: if ( nd_gentrace ) {
2535: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2536: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2537: }
2538: for ( i = 0; i < n; ) {
2539: if ( nd_gentrace ) {
2540: /* Trace = [1,index,1,1] */
1.6 noro 2541: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2542: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2543: }
2544: g = ndvtond(m,nd_ps[i]);
2545: g = nd_separate_head(g,&head);
1.6 noro 2546: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 2547: if ( !stat )
2548: nd_reconstruct(0,0);
2549: else {
2550: if ( DP_Print ) { printf("."); fflush(stdout); }
2551: ndv_free(nd_ps[i]);
2552: hc = HCU(nf); nd_removecont(m,nf);
2553: if ( nd_gentrace ) {
2554: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 2555: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
2556: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 2557: }
1.6 noro 2558: /* exact division */
1.1 noro 2559: cont = ndc_div(m,hc,HCU(nf));
2560: finalize_tracelist(perm[i],cont);
2561: }
2562: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2563: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2564: i++;
2565: }
2566: }
2567: if ( DP_Print ) { printf("\n"); }
2568: for ( a0 = 0, i = 0; i < n; i++ ) {
2569: NEXTNODE(a0,a);
2570: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2571: else {
2572: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2573: BDY(a) = (pointer)nd_ps[j];
2574: }
2575: }
2576: NEXT(a) = 0;
2577: return a0;
2578: }
2579:
2580: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
2581: {
2582: ND_pairs d1,nd,cur,head,prev,remove;
2583:
2584: if ( !g ) return d;
2585: /* for testing */
2586: if ( gensyz && nd_gensyz == 2 ) {
2587: d1 = nd_newpairs(g,t);
2588: if ( !d )
2589: return d1;
2590: else {
2591: nd = d;
2592: while ( NEXT(nd) ) nd = NEXT(nd);
2593: NEXT(nd) = d1;
2594: return d;
2595: }
2596: }
2597: d = crit_B(d,t);
2598: d1 = nd_newpairs(g,t);
2599: d1 = crit_M(d1);
2600: d1 = crit_F(d1);
2601: if ( gensyz || do_weyl )
2602: head = d1;
2603: else {
2604: prev = 0; cur = head = d1;
2605: while ( cur ) {
2606: if ( crit_2( cur->i1,cur->i2 ) ) {
2607: remove = cur;
2608: if ( !prev ) head = cur = NEXT(cur);
2609: else cur = NEXT(prev) = NEXT(cur);
2610: FREENDP(remove);
2611: } else {
2612: prev = cur; cur = NEXT(cur);
2613: }
2614: }
2615: }
2616: if ( !d )
2617: return head;
2618: else {
2619: nd = d;
2620: while ( NEXT(nd) ) nd = NEXT(nd);
2621: NEXT(nd) = head;
2622: return d;
2623: }
2624: }
2625:
2626:
2627: ND_pairs nd_newpairs( NODE g, int t )
2628: {
2629: NODE h;
2630: UINT *dl;
2631: int ts,s,i,t0,min,max;
2632: ND_pairs r,r0;
2633:
2634: dl = DL(nd_psh[t]);
2635: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 2636: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 2637: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
2638: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
2639: continue;
2640: if ( nd_gbblock ) {
2641: t0 = (long)BDY(h);
2642: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2643: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2644: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2645: break;
2646: }
2647: if ( nd_gbblock[i] >= 0 )
2648: continue;
2649: }
2650: NEXTND_pairs(r0,r);
2651: r->i1 = (long)BDY(h);
2652: r->i2 = t;
2653: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2654: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2655: SG(r) = MAX(s,ts) + TD(LCM(r));
2656: /* experimental */
2657: if ( nd_sugarweight )
2658: r->sugar2 = ndl_weight2(r->lcm);
2659: }
2660: if ( r0 ) NEXT(r) = 0;
2661: return r0;
2662: }
2663:
2664: /* ipair = [i1,i2],[i1,i2],... */
2665: ND_pairs nd_ipairtospair(NODE ipair)
2666: {
2667: int s1,s2;
2668: NODE tn,t;
2669: ND_pairs r,r0;
2670:
2671: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2672: NEXTND_pairs(r0,r);
2673: tn = BDY((LIST)BDY(t));
1.6 noro 2674: r->i1 = ZTOS((Q)ARG0(tn));
2675: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 2676: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2677: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2678: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2679: SG(r) = MAX(s1,s2) + TD(LCM(r));
2680: /* experimental */
2681: if ( nd_sugarweight )
2682: r->sugar2 = ndl_weight2(r->lcm);
2683: }
2684: if ( r0 ) NEXT(r) = 0;
2685: return r0;
2686: }
2687:
2688: /* kokokara */
2689:
2690: ND_pairs crit_B( ND_pairs d, int s )
2691: {
2692: ND_pairs cur,head,prev,remove;
2693: UINT *t,*tl,*lcm;
2694: int td,tdl;
2695:
2696: if ( !d ) return 0;
2697: t = DL(nd_psh[s]);
2698: prev = 0;
2699: head = cur = d;
2700: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
2701: while ( cur ) {
2702: tl = cur->lcm;
2703: if ( ndl_reducible(tl,t) ) {
2704: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
2705: if ( !ndl_equal(lcm,tl) ) {
2706: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2707: if (!ndl_equal(lcm,tl)) {
2708: remove = cur;
2709: if ( !prev ) {
2710: head = cur = NEXT(cur);
2711: } else {
2712: cur = NEXT(prev) = NEXT(cur);
2713: }
2714: FREENDP(remove);
2715: } else {
2716: prev = cur; cur = NEXT(cur);
2717: }
2718: } else {
2719: prev = cur; cur = NEXT(cur);
2720: }
2721: } else {
2722: prev = cur; cur = NEXT(cur);
2723: }
2724: }
2725: return head;
2726: }
2727:
2728: ND_pairs crit_M( ND_pairs d1 )
2729: {
2730: ND_pairs e,d2,d3,dd,p;
2731: UINT *id,*jd;
2732:
2733: if ( !d1 ) return d1;
2734: for ( dd = 0, e = d1; e; e = d3 ) {
2735: if ( !(d2 = NEXT(e)) ) {
2736: NEXT(e) = dd;
2737: return e;
2738: }
2739: id = LCM(e);
2740: for ( d3 = 0; d2; d2 = p ) {
2741: p = NEXT(d2);
2742: jd = LCM(d2);
2743: if ( ndl_equal(jd,id) )
2744: ;
2745: else if ( TD(jd) > TD(id) )
2746: if ( ndl_reducible(jd,id) ) continue;
2747: else ;
2748: else if ( ndl_reducible(id,jd) ) goto delit;
2749: NEXT(d2) = d3;
2750: d3 = d2;
2751: }
2752: NEXT(e) = dd;
2753: dd = e;
2754: continue;
2755: /**/
2756: delit: NEXT(d2) = d3;
2757: d3 = d2;
2758: for ( ; p; p = d2 ) {
2759: d2 = NEXT(p);
2760: NEXT(p) = d3;
2761: d3 = p;
2762: }
2763: FREENDP(e);
2764: }
2765: return dd;
2766: }
2767:
2768: ND_pairs crit_F( ND_pairs d1 )
2769: {
2770: ND_pairs rest, head,remove;
2771: ND_pairs last, p, r, w;
2772: int s;
2773:
2774: if ( !d1 ) return d1;
2775: for ( head = last = 0, p = d1; NEXT(p); ) {
2776: r = w = equivalent_pairs(p,&rest);
2777: s = SG(r);
2778: w = NEXT(w);
2779: while ( w ) {
2780: if ( crit_2(w->i1,w->i2) ) {
2781: r = w;
2782: w = NEXT(w);
2783: while ( w ) {
2784: remove = w;
2785: w = NEXT(w);
2786: FREENDP(remove);
2787: }
2788: break;
2789: } else if ( SG(w) < s ) {
2790: FREENDP(r);
2791: r = w;
2792: s = SG(r);
2793: w = NEXT(w);
2794: } else {
2795: remove = w;
2796: w = NEXT(w);
2797: FREENDP(remove);
2798: }
2799: }
2800: if ( last ) NEXT(last) = r;
2801: else head = r;
2802: NEXT(last = r) = 0;
2803: p = rest;
2804: if ( !p ) return head;
2805: }
2806: if ( !last ) return p;
2807: NEXT(last) = p;
2808: return head;
2809: }
2810:
2811: int crit_2( int dp1, int dp2 )
2812: {
2813: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
2814: }
2815:
2816: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
2817: {
2818: ND_pairs w,p,r,s;
2819: UINT *d;
2820:
2821: w = d1;
2822: d = LCM(w);
2823: s = NEXT(w);
2824: NEXT(w) = 0;
2825: for ( r = 0; s; s = p ) {
2826: p = NEXT(s);
2827: if ( ndl_equal(d,LCM(s)) ) {
2828: NEXT(s) = w; w = s;
2829: } else {
2830: NEXT(s) = r; r = s;
2831: }
2832: }
2833: *prest = r;
2834: return w;
2835: }
2836:
2837: NODE update_base(NODE nd,int ndp)
2838: {
2839: UINT *dl, *dln;
2840: NODE last, p, head;
2841:
2842: dl = DL(nd_psh[ndp]);
2843: for ( head = last = 0, p = nd; p; ) {
2844: dln = DL(nd_psh[(long)BDY(p)]);
2845: if ( ndl_reducible( dln, dl ) ) {
2846: p = NEXT(p);
2847: if ( last ) NEXT(last) = p;
2848: } else {
2849: if ( !last ) head = p;
2850: p = NEXT(last = p);
2851: }
2852: }
2853: head = append_one(head,ndp);
2854: return head;
2855: }
2856:
2857: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2858: {
2859: ND_pairs m,ml,p,l;
2860: UINT *lcm;
2861: int s,td,len,tlen,c,c1;
2862:
2863: if ( !(p = NEXT(m = d)) ) {
2864: *prest = p;
2865: NEXT(m) = 0;
2866: return m;
2867: }
2868: if ( !NoSugar ) {
2869: if ( nd_sugarweight ) {
2870: s = m->sugar2;
2871: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2872: if ( (p->sugar2 < s)
2873: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2874: ml = l; m = p; s = m->sugar2;
2875: }
2876: } else {
2877: s = SG(m);
2878: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2879: if ( (SG(p) < s)
2880: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2881: ml = l; m = p; s = SG(m);
2882: }
2883: }
2884: } else {
2885: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2886: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2887: ml = l; m = p; s = SG(m);
2888: }
2889: }
2890: if ( !ml ) *prest = NEXT(m);
2891: else {
2892: NEXT(ml) = NEXT(m);
2893: *prest = d;
2894: }
2895: NEXT(m) = 0;
2896: return m;
2897: }
2898:
2899: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2900: {
2901: int msugar,i;
2902: ND_pairs t,dm0,dm,dr0,dr;
2903:
2904: if ( nd_sugarweight ) {
2905: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2906: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2907: dm0 = 0; dr0 = 0;
2908: for ( i = 0, t = d; t; t = NEXT(t) )
2909: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2910: if ( dm0 ) NEXT(dm) = t;
2911: else dm0 = t;
2912: dm = t;
2913: i++;
2914: } else {
2915: if ( dr0 ) NEXT(dr) = t;
2916: else dr0 = t;
2917: dr = t;
2918: }
2919: } else {
2920: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2921: if ( SG(t) < msugar ) msugar = SG(t);
2922: dm0 = 0; dr0 = 0;
2923: for ( i = 0, t = d; t; t = NEXT(t) )
2924: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2925: if ( dm0 ) NEXT(dm) = t;
2926: else dm0 = t;
2927: dm = t;
2928: i++;
2929: } else {
2930: if ( dr0 ) NEXT(dr) = t;
2931: else dr0 = t;
2932: dr = t;
2933: }
2934: }
2935: NEXT(dm) = 0;
2936: if ( dr0 ) NEXT(dr) = 0;
2937: *prest = dr0;
2938: return dm0;
2939: }
2940:
2941: int nd_tdeg(NDV c)
2942: {
2943: int wmax = 0;
2944: int i,len;
2945: NMV a;
2946:
2947: len = LEN(c);
2948: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2949: wmax = MAX(TD(DL(a)),wmax);
2950: return wmax;
2951: }
2952:
2953: int ndv_newps(int m,NDV a,NDV aq,int f4)
2954: {
2955: int len;
2956: RHist r;
2957: NDV b;
2958: NODE tn;
2959: LIST l;
2960: Z iq;
2961:
2962: if ( nd_psn == nd_pslen ) {
2963: nd_pslen *= 2;
2964: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
2965: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2966: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2967: nd_bound = (UINT **)
2968: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
2969: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2970: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
2971: }
2972: NEWRHist(r); nd_psh[nd_psn] = r;
2973: nd_ps[nd_psn] = a;
2974: if ( aq ) {
2975: nd_ps_trace[nd_psn] = aq;
2976: if ( !m ) {
2977: register_hcf(aq);
2978: } else if ( m == -2 ) {
2979: /* do nothing */
2980: } else
2981: error("ndv_newps : invalud modulus");
2982: nd_bound[nd_psn] = ndv_compute_bound(aq);
2983: #if 1
2984: SG(r) = SG(aq);
2985: #else
2986: SG(r) = nd_tdeg(aq);
2987: #endif
2988: ndl_copy(HDL(aq),DL(r));
2989: } else {
2990: if ( !m ) register_hcf(a);
2991: nd_bound[nd_psn] = ndv_compute_bound(a);
2992: #if 1
2993: SG(r) = SG(a);
2994: #else
2995: SG(r) = nd_tdeg(a);
2996: #endif
2997: ndl_copy(HDL(a),DL(r));
2998: }
2999: if ( nd_demand ) {
3000: if ( aq ) {
3001: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3002: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3003: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3004: nd_ps_trace[nd_psn] = 0;
3005: } else {
3006: ndv_save(nd_ps[nd_psn],nd_psn);
3007: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3008: nd_ps[nd_psn] = 0;
3009: }
3010: }
3011: if ( nd_gentrace ) {
3012: /* reverse the tracelist and append it to alltracelist */
3013: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3014: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3015: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3016: }
3017: return nd_psn++;
3018: }
3019:
3020: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3021: /* return 1 if success, 0 if failure (HC(a mod p)) */
3022:
3023: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
3024: {
1.6 noro 3025: int i,j,td,len,max;
3026: NODE s,s0,f0,tn;
3027: UINT *d;
3028: RHist r;
3029: NDVI w;
3030: NDV a,am;
3031: union oNDC hc;
3032: NODE node;
3033: P hcp;
3034: Z iq,jq;
3035: LIST l;
3036:
3037: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3038: /* initialize the tracelist */
3039: nd_tracelist = 0;
3040:
3041: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3042: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3043: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3044: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3045: if ( !dont_sort ) {
3046: /* XXX heuristic */
3047: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
3048: qsort(w,nd_psn,sizeof(struct oNDVI),
3049: (int (*)(const void *,const void *))ndvi_compare_rev);
3050: else
3051: qsort(w,nd_psn,sizeof(struct oNDVI),
3052: (int (*)(const void *,const void *))ndvi_compare);
3053: }
3054: nd_pslen = 2*nd_psn;
3055: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3056: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3057: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3058: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3059: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3060: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3061: nd_hcf = 0;
1.1 noro 3062:
1.6 noro 3063: if ( trace && nd_vc )
3064: makesubst(nd_vc,&nd_subst);
3065: else
3066: nd_subst = 0;
1.1 noro 3067:
1.6 noro 3068: if ( !nd_red )
3069: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3070: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3071: for ( i = 0; i < nd_psn; i++ ) {
3072: hc = HCU(w[i].p);
3073: if ( trace ) {
3074: if ( mod == -2 ) {
3075: /* over a large finite field */
3076: /* trace = small modulus */
3077: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3078: ndv_mod(-2,a);
3079: if ( !dont_removecont) ndv_removecont(-2,a);
3080: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3081: ndv_mod(trace,am);
3082: if ( DL_COMPARE(HDL(am),HDL(a)) )
3083: return 0;
3084: ndv_removecont(trace,am);
3085: } else {
3086: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3087: if ( !dont_removecont) ndv_removecont(0,a);
3088: register_hcf(a);
3089: am = nd_ps[i] = ndv_dup(mod,a);
3090: ndv_mod(mod,am);
3091: if ( DL_COMPARE(HDL(am),HDL(a)) )
3092: return 0;
3093: ndv_removecont(mod,am);
3094: }
3095: } else {
3096: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3097: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3098: if ( !mod ) register_hcf(a);
1.1 noro 3099: }
1.6 noro 3100: if ( nd_gentrace ) {
3101: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3102: /* exact division */
1.1 noro 3103: if ( !dont_removecont )
1.6 noro 3104: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3105: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3106: }
3107: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3108: nd_bound[i] = ndv_compute_bound(a);
3109: nd_psh[i] = r;
3110: if ( nd_demand ) {
3111: if ( trace ) {
3112: ndv_save(nd_ps_trace[i],i);
3113: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3114: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3115: nd_ps_trace[i] = 0;
3116: } else {
3117: ndv_save(nd_ps[i],i);
3118: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3119: nd_ps[i] = 0;
3120: }
1.1 noro 3121: }
1.6 noro 3122: }
3123: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3124: return 1;
1.1 noro 3125: }
3126:
3127: struct order_spec *append_block(struct order_spec *spec,
3128: int nv,int nalg,int ord);
3129:
3130: extern VECT current_dl_weight_vector_obj;
3131: static VECT prev_weight_vector_obj;
3132:
3133: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3134: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3135: {
3136: NODE alist,t,s,r0,r,arg;
3137: VL tv;
3138: P poly;
3139: DP d;
3140: Alg alpha,dp;
3141: DAlg inv,da,hc;
3142: MP m;
3143: int i,nvar,nalg,n;
3144: NumberField nf;
3145: LIST f1,f2;
3146: struct order_spec *current_spec;
3147: VECT obj,obj0;
3148: VECT tmp;
3149:
3150: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3151: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3152:
3153: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3154: NEXTNODE(alist,t); MKV(tv->v,poly);
3155: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3156: tv->v = tv->v->priv;
3157: }
3158: NEXT(t) = 0;
3159:
3160: /* simplification, making polynomials monic */
3161: setfield_dalg(alist);
3162: obj_algtodalg((Obj)f,(Obj *)&f1);
3163: for ( t = BDY(f); t; t = NEXT(t) ) {
3164: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3165: hc = (DAlg)BDY(d)->c;
3166: if ( NID(hc) == N_DA ) {
3167: invdalg(hc,&inv);
3168: for ( m = BDY(d); m; m = NEXT(m) ) {
3169: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3170: }
3171: }
3172: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3173: }
3174: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3175:
3176: /* append alg vars to the var list */
3177: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3178: NEXT(tv) = av;
3179:
3180: /* append a block to ord */
3181: *ord1p = append_block(ord,nvar,nalg,2);
3182:
3183: /* create generator list */
3184: nf = get_numberfield();
3185: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3186: MKAlg(nf->defpoly[i],dp);
3187: MKNODE(s,dp,t); t = s;
3188: }
3189: MKLIST(f1,t);
3190: *alistp = alist;
3191: algobjtorat((Obj)f1,(Obj *)f1p);
3192:
3193: /* creating a new weight vector */
3194: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3195: n = nvar+nalg+1;
3196: MKVECT(obj,n);
3197: if ( obj0 && obj0->len == nvar )
3198: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3199: else
3200: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3201: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3202: BDY(obj)[n-1] = (pointer)ONE;
3203: arg = mknode(1,obj);
3204: Pdp_set_weight(arg,&tmp);
3205: }
3206:
3207: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3208: {
3209: NODE s,t,u0,u;
3210: P p;
3211: VL tv;
3212: Obj obj;
3213: VECT tmp;
3214: NODE arg;
3215:
3216: u0 = 0;
3217: for ( t = r; t; t = NEXT(t) ) {
3218: p = (P)BDY(t);
3219: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3220: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3221: }
3222: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3223: NEXTNODE(u0,u);
3224: BDY(u) = (pointer)p;
3225: }
3226: }
3227: arg = mknode(1,prev_weight_vector_obj);
3228: Pdp_set_weight(arg,&tmp);
3229:
3230: return u0;
3231: }
3232:
3233: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3234: {
3235: VL tv,fv,vv,vc,av;
3236: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3237: int e,max,nvar,i;
3238: NDV b;
3239: int ishomo,nalg,mrank,trank,wmax,len;
3240: NMV a;
3241: Alg alpha,dp;
3242: P p,zp;
3243: Q dmy;
3244: LIST f1,f2,zpl;
3245: Obj obj;
3246: NumberField nf;
3247: struct order_spec *ord1;
3248: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3249: LIST l1,l2,l3,l4,l5;
3250: int j;
3251: Z jq,bpe,last_nonzero;
3252: int *perm;
3253: EPOS oepos;
3254: int obpe,oadv,ompos,cbpe;
1.15 noro 3255: VECT hvect;
1.1 noro 3256:
3257: nd_module = 0;
3258: if ( !m && Demand ) nd_demand = 1;
3259: else nd_demand = 0;
3260: parse_nd_option(current_option);
3261:
3262: if ( DP_Multiple )
3263: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3264: #if 0
3265: ndv_alloc = 0;
3266: #endif
3267: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3268: if ( m && nd_vc )
3269: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3270: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3271: switch ( ord->id ) {
3272: case 1:
3273: if ( ord->nv != nvar )
3274: error("nd_{gr,f4} : invalid order specification");
3275: break;
3276: default:
3277: break;
3278: }
3279: nd_nalg = 0;
3280: av = 0;
3281: if ( !m ) {
3282: get_algtree((Obj)f,&av);
3283: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3284: nd_ntrans = nvar;
3285: nd_nalg = nalg;
3286: /* #i -> t#i */
3287: if ( nalg ) {
3288: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3289: ord = ord1;
3290: f = f1;
3291: }
3292: nvar += nalg;
3293: }
3294: nd_init_ord(ord);
3295: mrank = 0;
3296: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3297: for ( tv = vv; tv; tv = NEXT(tv) ) {
3298: if ( nd_module ) {
1.16 noro 3299: if ( OID(BDY(t)) == O_DPM ) {
3300: e = dpm_getdeg((DPM)BDY(t),&trank);
3301: max = MAX(e,max);
3302: mrank = MAX(mrank,trank);
3303: } else {
3304: s = BDY((LIST)BDY(t));
3305: trank = length(s);
3306: mrank = MAX(mrank,trank);
3307: for ( ; s; s = NEXT(s) ) {
3308: e = getdeg(tv->v,(P)BDY(s));
3309: max = MAX(e,max);
3310: }
1.1 noro 3311: }
3312: } else {
3313: e = getdeg(tv->v,(P)BDY(t));
3314: max = MAX(e,max);
3315: }
3316: }
3317: nd_setup_parameters(nvar,nd_nzlist?0:max);
3318: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3319: ishomo = 1;
3320: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3321: if ( nd_module ) {
1.16 noro 3322: if ( OID(BDY(t)) == O_DPM ) {
3323: Z cont;
3324: DPM zdpm;
3325:
3326: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
3327: else zdpm = (DPM)BDY(t);
3328: b = (pointer)dpmtondv(m,zdpm);
3329: } else {
3330: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3331: else zpl = (LIST)BDY(t);
1.1 noro 3332: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 3333: }
1.1 noro 3334: } else {
3335: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3336: else zp = (P)BDY(t);
3337: b = (pointer)ptondv(CO,vv,zp);
3338: }
3339: if ( ishomo )
3340: ishomo = ishomo && ndv_ishomo(b);
3341: if ( m ) ndv_mod(m,b);
3342: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3343: }
3344: if ( fd0 ) NEXT(fd) = 0;
3345:
3346: if ( !ishomo && homo ) {
3347: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3348: b = (NDV)BDY(t); len = LEN(b);
3349: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3350: wmax = MAX(TD(DL(a)),wmax);
3351: }
3352: homogenize_order(ord,nvar,&ord1);
3353: nd_init_ord(ord1);
3354: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
3355: for ( t = fd0; t; t = NEXT(t) )
3356: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3357: }
3358:
3359: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
3360: if ( nd_gentrace ) {
3361: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3362: }
3363: if ( nd_splist ) {
3364: *rp = compute_splist();
3365: return;
3366: }
3367: if ( nd_check_splist ) {
3368: if ( f4 ) {
3369: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3370: else *rp = 0;
3371: } else {
3372: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3373: else *rp = 0;
3374: }
3375: return;
3376: }
3377: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
3378: if ( !x ) {
3379: *rp = 0; return;
3380: }
1.15 noro 3381: if ( nd_gentrace ) {
3382: MKVECT(hvect,nd_psn);
3383: for ( i = 0; i < nd_psn; i++ )
3384: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
3385: }
1.1 noro 3386: if ( !ishomo && homo ) {
3387: /* dehomogenization */
3388: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3389: nd_init_ord(ord);
3390: nd_setup_parameters(nvar,0);
3391: }
3392: nd_demand = 0;
3393: if ( nd_module && nd_intersect ) {
3394: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 3395: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 3396: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
3397: }
3398: conv_ilist(nd_demand,0,x,0);
3399: goto FINAL;
3400: }
3401: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
3402: x = ndv_reducebase(x,perm);
3403: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3404: x = ndv_reduceall(m,x);
3405: cbpe = nd_bpe;
3406: if ( nd_gentrace && !f4 ) {
3407: tl2 = nd_alltracelist; nd_alltracelist = 0;
3408: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
3409: tl3 = nd_alltracelist; nd_alltracelist = 0;
3410: if ( nd_gensyz ) {
3411: nd_gb(m,0,1,1,0);
3412: tl4 = nd_alltracelist; nd_alltracelist = 0;
3413: } else tl4 = 0;
3414: }
3415: nd_bpe = cbpe;
3416: nd_setup_parameters(nd_nvar,0);
3417: FINAL:
3418: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 3419: NEXTNODE(r0,r);
3420: if ( nd_module ) {
3421: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
3422: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
3423: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
3424: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 3425: }
3426: if ( r0 ) NEXT(r) = 0;
3427: if ( !m && nd_nalg )
3428: r0 = postprocess_algcoef(av,alist,r0);
3429: MKLIST(*rp,r0);
3430: if ( nd_gentrace ) {
3431: if ( f4 ) {
1.6 noro 3432: STOZ(16,bpe);
3433: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 3434: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 3435: } else {
3436: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3437: tl3 = reverse_node(tl3);
3438: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3439: for ( t = tl2; t; t = NEXT(t) ) {
3440: /* s = [i,[*,j,*,*],...] */
3441: s = BDY((LIST)BDY(t));
1.6 noro 3442: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3443: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3444: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3445: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3446: }
3447: }
3448: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3449: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3450: }
3451: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3452: MKLIST(l5,tl4);
1.6 noro 3453: STOZ(nd_bpe,bpe);
1.15 noro 3454: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3455: }
3456: }
3457: #if 0
3458: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
3459: #endif
3460: }
3461:
3462: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3463: {
3464: VL tv,fv,vv,vc,av;
3465: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3466: int e,max,nvar,i;
3467: NDV b;
3468: int ishomo,nalg;
3469: Alg alpha,dp;
3470: P p,zp;
3471: Q dmy;
3472: LIST f1,f2;
3473: Obj obj;
3474: NumberField nf;
3475: struct order_spec *ord1;
3476: int *perm;
3477:
3478: parse_nd_option(current_option);
3479: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3480: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3481: switch ( ord->id ) {
3482: case 1:
3483: if ( ord->nv != nvar )
3484: error("nd_check : invalid order specification");
3485: break;
3486: default:
3487: break;
3488: }
3489: nd_nalg = 0;
3490: av = 0;
3491: if ( !m ) {
3492: get_algtree((Obj)f,&av);
3493: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3494: nd_ntrans = nvar;
3495: nd_nalg = nalg;
3496: /* #i -> t#i */
3497: if ( nalg ) {
3498: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3499: ord = ord1;
3500: f = f1;
3501: }
3502: nvar += nalg;
3503: }
3504: nd_init_ord(ord);
3505: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3506: for ( tv = vv; tv; tv = NEXT(tv) ) {
3507: e = getdeg(tv->v,(P)BDY(t));
3508: max = MAX(e,max);
3509: }
3510: nd_setup_parameters(nvar,max);
3511: ishomo = 1;
3512: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3513: ptozp((P)BDY(t),1,&dmy,&zp);
3514: b = (pointer)ptondv(CO,vv,zp);
3515: if ( ishomo )
3516: ishomo = ishomo && ndv_ishomo(b);
3517: if ( m ) ndv_mod(m,b);
3518: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3519: }
3520: if ( fd0 ) NEXT(fd) = 0;
3521: ndv_setup(m,0,fd0,0,1);
3522: for ( x = 0, i = 0; i < nd_psn; i++ )
3523: x = update_base(x,i);
3524: if ( do_check ) {
3525: x = nd_gb(m,ishomo,1,0,&perm);
3526: if ( !x ) {
3527: *rp = 0;
3528: return;
3529: }
3530: } else {
3531: #if 0
3532: /* bug ? */
3533: for ( t = x; t; t = NEXT(t) )
3534: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
3535: #else
3536: conv_ilist(0,0,x,&perm);
3537: #endif
3538: }
3539: x = ndv_reducebase(x,perm);
3540: x = ndv_reduceall(m,x);
3541: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3542: NEXTNODE(r0,r);
3543: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3544: }
3545: if ( r0 ) NEXT(r) = 0;
3546: if ( !m && nd_nalg )
3547: r0 = postprocess_algcoef(av,alist,r0);
3548: MKLIST(*rp,r0);
3549: }
3550:
3551: NDV recompute_trace(NODE trace,NDV *p,int m);
3552: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3553:
3554: NDV recompute_trace(NODE ti,NDV *p,int mod)
3555: {
3556: int c,c1,c2,i;
3557: NM mul,m,tail;
3558: ND d,r,rm;
3559: NODE sj;
3560: NDV red;
3561: Obj mj;
3562:
3563: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3564: CM(mul) = 1;
3565: tail = 0;
3566: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3567: sj = BDY((LIST)BDY(ti));
3568: if ( ARG0(sj) ) {
1.6 noro 3569: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 3570: mj = (Obj)ARG2(sj);
3571: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3572: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3573: rm = ndv_mul_nm(mod,mul,red);
3574: if ( !r ) r = rm;
3575: else {
3576: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3577: if ( d ) {
3578: NEXT(tail) = m; tail = m; LEN(d)++;
3579: } else {
3580: MKND(nd_nvar,m,1,d); tail = BDY(d);
3581: }
3582: }
3583: if ( !m ) return 0; /* failure */
3584: else {
3585: BDY(r) = m;
3586: if ( mod > 0 || mod == -1 ) {
3587: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3588: DMAR(c1,c2,0,mod,c);
3589: nd_mul_c(mod,rm,c);
3590: } else {
3591: Z t,u;
3592:
3593: chsgnlf(HCZ(r),&t);
3594: divlf(t,HCZ(rm),&u);
3595: nd_mul_c_lf(rm,u);
3596: }
3597: r = nd_add(mod,r,rm);
3598: }
3599: }
3600: }
3601: }
3602: if ( tail ) NEXT(tail) = 0;
3603: d = nd_add(mod,d,r);
3604: nd_mul_c(mod,d,invm(HCM(d),mod));
3605: return ndtondv(mod,d);
3606: }
3607:
3608: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3609: {
3610: VL tv,fv,vv,vc,av;
3611: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3612: int e,max,nvar,i;
3613: NDV b;
3614: int ishomo,nalg;
3615: Alg alpha,dp;
3616: P p,zp;
3617: Q dmy;
3618: LIST f1,f2;
3619: Obj obj;
3620: NumberField nf;
3621: struct order_spec *ord1;
3622: NODE permtrace,intred,ind,perm,trace,ti;
3623: int len,n,j;
3624: NDV *db,*pb;
3625:
3626: parse_nd_option(current_option);
3627: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3628: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3629: switch ( ord->id ) {
3630: case 1:
3631: if ( ord->nv != nvar )
3632: error("nd_check : invalid order specification");
3633: break;
3634: default:
3635: break;
3636: }
3637: nd_init_ord(ord);
1.6 noro 3638: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 3639: nd_setup_parameters(nvar,0);
3640:
3641: len = length(BDY(f));
3642: db = (NDV *)MALLOC(len*sizeof(NDV *));
3643: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3644: ptozp((P)BDY(t),1,&dmy,&zp);
3645: b = ptondv(CO,vv,zp);
3646: ndv_mod(m,b);
3647: ndv_mul_c(m,b,invm(HCM(b),m));
3648: db[i] = b;
3649: }
3650:
3651: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3652: intred = BDY((LIST)ARG3(BDY(tlist)));
3653: ind = BDY((LIST)ARG4(BDY(tlist)));
3654: perm = BDY((LIST)ARG0(permtrace));
3655: trace = NEXT(permtrace);
3656:
3657: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 3658: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 3659: if ( j > i ) i = j;
3660: }
3661: n = i+1;
3662: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3663: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3664: ti = BDY((LIST)BDY(t));
1.6 noro 3665: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 3666: }
3667: for ( t = trace; t; t = NEXT(t) ) {
3668: ti = BDY((LIST)BDY(t));
1.6 noro 3669: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3670: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3671: if ( DP_Print ) {
3672: fprintf(asir_out,"."); fflush(asir_out);
3673: }
3674: }
3675: for ( t = intred; t; t = NEXT(t) ) {
3676: ti = BDY((LIST)BDY(t));
1.6 noro 3677: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3678: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3679: if ( DP_Print ) {
3680: fprintf(asir_out,"*"); fflush(asir_out);
3681: }
3682: }
3683: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3684: NEXTNODE(r0,r);
1.6 noro 3685: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 3686: ndv_mul_c(m,b,invm(HCM(b),m));
3687: #if 0
1.6 noro 3688: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3689: #else
1.6 noro 3690: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3691: #endif
3692: }
3693: if ( r0 ) NEXT(r) = 0;
3694: MKLIST(*rp,r0);
3695: if ( DP_Print ) fprintf(asir_out,"\n");
3696: }
3697:
1.16 noro 3698: 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 3699: {
3700: VL tv,fv,vv,vc,av;
3701: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3702: int m,nocheck,nvar,mindex,e,max;
3703: NDV c;
3704: NMV a;
3705: P p,zp;
3706: Q dmy;
3707: EPOS oepos;
3708: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
3709: Alg alpha,dp;
3710: P poly;
3711: LIST f1,f2,zpl;
3712: Obj obj;
3713: NumberField nf;
3714: struct order_spec *ord1;
3715: struct oEGT eg_check,eg0,eg1;
3716: NODE tr,tl1,tl2,tl3,tl4;
3717: LIST l1,l2,l3,l4,l5;
3718: int *perm;
3719: int j,ret;
3720: Z jq,bpe;
1.15 noro 3721: VECT hvect;
1.1 noro 3722:
3723: nd_module = 0;
3724: nd_lf = 0;
3725: parse_nd_option(current_option);
3726: if ( nd_lf ) {
3727: if ( f4 )
3728: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3729: else
3730: error("nd_gr_trace is not implemented yet over a large finite field");
3731: return;
3732: }
3733: if ( DP_Multiple )
3734: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3735:
3736: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3737: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3738: switch ( ord->id ) {
3739: case 1:
3740: if ( ord->nv != nvar )
3741: error("nd_gr_trace : invalid order specification");
3742: break;
3743: default:
3744: break;
3745: }
3746:
3747: get_algtree((Obj)f,&av);
3748: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3749: nd_ntrans = nvar;
3750: nd_nalg = nalg;
3751: /* #i -> t#i */
3752: if ( nalg ) {
3753: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3754: ord = ord1;
3755: f = f1;
3756: }
3757: nvar += nalg;
3758:
3759: nocheck = 0;
3760: mindex = 0;
3761:
3762: if ( Demand ) nd_demand = 1;
3763: else nd_demand = 0;
3764:
3765: /* setup modulus */
3766: if ( trace < 0 ) {
3767: trace = -trace;
3768: nocheck = 1;
3769: }
3770: m = trace > 1 ? trace : get_lprime(mindex);
3771: nd_init_ord(ord);
3772: mrank = 0;
3773: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3774: for ( tv = vv; tv; tv = NEXT(tv) ) {
3775: if ( nd_module ) {
1.16 noro 3776: if ( OID(BDY(t)) == O_DPM ) {
3777: e = dpm_getdeg((DPM)BDY(t),&trank);
3778: max = MAX(e,max);
3779: mrank = MAX(mrank,trank);
3780: } else {
1.1 noro 3781: s = BDY((LIST)BDY(t));
3782: trank = length(s);
3783: mrank = MAX(mrank,trank);
3784: for ( ; s; s = NEXT(s) ) {
3785: e = getdeg(tv->v,(P)BDY(s));
3786: max = MAX(e,max);
3787: }
1.16 noro 3788: }
1.1 noro 3789: } else {
3790: e = getdeg(tv->v,(P)BDY(t));
3791: max = MAX(e,max);
3792: }
3793: }
3794: nd_setup_parameters(nvar,max);
3795: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3796: ishomo = 1;
3797: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3798: if ( nd_module ) {
1.16 noro 3799: if ( OID(BDY(t)) == O_DPM ) {
3800: Z cont;
3801: DPM zdpm;
3802:
1.17 noro 3803: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 3804: else zdpm = (DPM)BDY(t);
3805: c = (pointer)dpmtondv(m,zdpm);
3806: } else {
3807: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3808: else zpl = (LIST)BDY(t);
1.1 noro 3809: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 3810: }
1.1 noro 3811: } else {
1.16 noro 3812: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3813: else zp = (P)BDY(t);
3814: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 3815: }
3816: if ( ishomo )
3817: ishomo = ishomo && ndv_ishomo(c);
3818: if ( c ) {
3819: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3820: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3821: }
3822: }
3823: if ( in0 ) NEXT(in) = 0;
3824: if ( fd0 ) NEXT(fd) = 0;
3825: if ( !ishomo && homo ) {
3826: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3827: c = (NDV)BDY(t); len = LEN(c);
3828: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3829: wmax = MAX(TD(DL(a)),wmax);
3830: }
3831: homogenize_order(ord,nvar,&ord1);
3832: nd_init_ord(ord1);
3833: nd_setup_parameters(nvar+1,wmax);
3834: for ( t = fd0; t; t = NEXT(t) )
3835: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3836: }
3837: if ( MaxDeg > 0 ) nocheck = 1;
3838: while ( 1 ) {
3839: tl1 = tl2 = tl3 = tl4 = 0;
3840: if ( Demand )
3841: nd_demand = 1;
3842: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
3843: if ( nd_gentrace ) {
3844: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3845: }
3846: if ( ret )
3847: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3848: if ( !ret || !cand ) {
3849: /* failure */
3850: if ( trace > 1 ) { *rp = 0; return; }
3851: else m = get_lprime(++mindex);
3852: continue;
3853: }
1.15 noro 3854: if ( nd_gentrace ) {
3855: MKVECT(hvect,nd_psn);
3856: for ( i = 0; i < nd_psn; i++ )
3857: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
3858: }
1.1 noro 3859: if ( !ishomo && homo ) {
3860: /* dehomogenization */
3861: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3862: nd_init_ord(ord);
3863: nd_setup_parameters(nvar,0);
3864: }
3865: nd_demand = 0;
3866: cand = ndv_reducebase(cand,perm);
3867: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3868: cand = ndv_reduceall(0,cand);
3869: cbpe = nd_bpe;
3870: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
3871: get_eg(&eg0);
3872: if ( nocheck )
3873: break;
3874: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
3875: if ( nd_gentrace ) {
3876: tl3 = nd_alltracelist; nd_alltracelist = 0;
3877: } else tl3 = 0;
3878: /* gbcheck : cand is a GB of Id(cand) ? */
3879: if ( nd_vc || nd_gentrace || nd_gensyz )
3880: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3881: else
3882: ret = nd_f4(0,1,0)!=0;
3883: if ( nd_gentrace && nd_gensyz ) {
3884: tl4 = nd_alltracelist; nd_alltracelist = 0;
3885: } else tl4 = 0;
3886: }
3887: if ( ret ) break;
3888: else if ( trace > 1 ) {
3889: /* failure */
3890: *rp = 0; return;
3891: } else {
3892: /* try the next modulus */
3893: m = get_lprime(++mindex);
3894: /* reset the parameters */
3895: if ( !ishomo && homo ) {
3896: nd_init_ord(ord1);
3897: nd_setup_parameters(nvar+1,wmax);
3898: } else {
3899: nd_init_ord(ord);
3900: nd_setup_parameters(nvar,max);
3901: }
3902: }
3903: }
3904: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3905: if ( DP_Print )
1.6 noro 3906: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 3907: /* dp->p */
3908: nd_bpe = cbpe;
3909: nd_setup_parameters(nd_nvar,0);
3910: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 3911: if ( nd_module ) {
1.17 noro 3912: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 3913: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 3914: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
3915: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 3916: }
3917: if ( nd_nalg )
3918: cand = postprocess_algcoef(av,alist,cand);
3919: MKLIST(*rp,cand);
3920: if ( nd_gentrace ) {
3921: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3922: tl3 = reverse_node(tl3);
3923: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3924: for ( t = tl2; t; t = NEXT(t) ) {
3925: /* s = [i,[*,j,*,*],...] */
3926: s = BDY((LIST)BDY(t));
1.6 noro 3927: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3928: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3929: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3930: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3931: }
3932: }
3933: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3934: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3935: }
3936: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3937: MKLIST(l5,tl4);
1.6 noro 3938: STOZ(nd_bpe,bpe);
1.15 noro 3939: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3940: }
3941: }
3942:
3943: /* XXX : module element is not considered */
3944:
3945: void dltondl(int n,DL dl,UINT *r)
3946: {
3947: UINT *d;
3948: int i,j,l,s,ord_l;
3949: struct order_pair *op;
3950:
3951: d = (unsigned int *)dl->d;
3952: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3953: if ( nd_blockmask ) {
3954: l = nd_blockmask->n;
3955: op = nd_blockmask->order_pair;
3956: for ( j = 0, s = 0; j < l; j++ ) {
3957: ord_l = op[j].length;
3958: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3959: }
3960: TD(r) = ndl_weight(r);
3961: ndl_weight_mask(r);
3962: } else {
3963: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3964: TD(r) = ndl_weight(r);
3965: }
3966: }
3967:
3968: DL ndltodl(int n,UINT *ndl)
3969: {
3970: DL dl;
3971: int *d;
3972: int i,j,l,s,ord_l;
3973: struct order_pair *op;
3974:
3975: NEWDL(dl,n);
3976: dl->td = TD(ndl);
3977: d = dl->d;
3978: if ( nd_blockmask ) {
3979: l = nd_blockmask->n;
3980: op = nd_blockmask->order_pair;
3981: for ( j = 0, s = 0; j < l; j++ ) {
3982: ord_l = op[j].length;
3983: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3984: }
3985: } else {
3986: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3987: }
3988: return dl;
3989: }
3990:
3991: void nmtodp(int mod,NM m,DP *r)
3992: {
3993: DP dp;
3994: MP mr;
3995:
3996: NEWMP(mr);
3997: mr->dl = ndltodl(nd_nvar,DL(m));
3998: mr->c = (Obj)ndctop(mod,m->c);
3999: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4000: *r = dp;
4001: }
4002:
1.15 noro 4003: void ndltodp(UINT *d,DP *r)
4004: {
4005: DP dp;
4006: MP mr;
4007:
4008: NEWMP(mr);
4009: mr->dl = ndltodl(nd_nvar,d);
4010: mr->c = (Obj)ONE;
4011: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4012: *r = dp;
4013: }
4014:
1.1 noro 4015: void ndl_print(UINT *dl)
4016: {
4017: int n;
4018: int i,j,l,ord_l,s,s0;
4019: struct order_pair *op;
4020:
4021: n = nd_nvar;
4022: printf("<<");
4023: if ( nd_blockmask ) {
4024: l = nd_blockmask->n;
4025: op = nd_blockmask->order_pair;
4026: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4027: ord_l = op[j].length;
4028: for ( i = 0; i < ord_l; i++, s++ )
4029: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4030: }
4031: } else {
4032: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4033: }
4034: printf(">>");
4035: if ( nd_module && MPOS(dl) )
4036: printf("*e%d",MPOS(dl));
4037: }
4038:
4039: void nd_print(ND p)
4040: {
4041: NM m;
4042:
4043: if ( !p )
4044: printf("0\n");
4045: else {
4046: for ( m = BDY(p); m; m = NEXT(m) ) {
4047: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
4048: else printf("+%d*",CM(m));
4049: ndl_print(DL(m));
4050: }
4051: printf("\n");
4052: }
4053: }
4054:
4055: void nd_print_q(ND p)
4056: {
4057: NM m;
4058:
4059: if ( !p )
4060: printf("0\n");
4061: else {
4062: for ( m = BDY(p); m; m = NEXT(m) ) {
4063: printf("+");
1.6 noro 4064: printexpr(CO,(Obj)CZ(m));
1.1 noro 4065: printf("*");
4066: ndl_print(DL(m));
4067: }
4068: printf("\n");
4069: }
4070: }
4071:
4072: void ndp_print(ND_pairs d)
4073: {
4074: ND_pairs t;
4075:
4076: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4077: printf("\n");
4078: }
4079:
4080: void nd_removecont(int mod,ND p)
4081: {
4082: int i,n;
4083: Z *w;
4084: NM m;
4085: struct oVECT v;
4086:
4087: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4088: else if ( mod == -2 ) {
4089: Z inv;
4090: divlf(ONE,HCZ(p),&inv);
4091: nd_mul_c_lf(p,inv);
4092: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4093: else {
4094: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4095: w = (Z *)MALLOC(n*sizeof(Q));
4096: v.len = n;
4097: v.body = (pointer *)w;
1.6 noro 4098: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4099: removecont_array((P *)w,n,1);
1.6 noro 4100: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4101: }
4102: }
4103:
4104: void nd_removecont2(ND p1,ND p2)
4105: {
4106: int i,n1,n2,n;
4107: Z *w;
4108: NM m;
4109: struct oVECT v;
4110:
4111: n1 = nd_length(p1);
4112: n2 = nd_length(p2);
4113: n = n1+n2;
4114: w = (Z *)MALLOC(n*sizeof(Q));
4115: v.len = n;
4116: v.body = (pointer *)w;
4117: i = 0;
4118: if ( p1 )
1.6 noro 4119: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4120: if ( p2 )
1.6 noro 4121: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4122: removecont_array((P *)w,n,1);
4123: i = 0;
4124: if ( p1 )
1.6 noro 4125: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4126: if ( p2 )
1.6 noro 4127: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4128: }
4129:
4130: void ndv_removecont(int mod,NDV p)
4131: {
4132: int i,len,all_p;
4133: Z *c;
4134: P *w;
4135: Z dvr,t;
4136: P g,cont,tp;
4137: NMV m;
4138:
4139: if ( mod == -1 )
4140: ndv_mul_c(mod,p,_invsf(HCM(p)));
4141: else if ( mod == -2 ) {
4142: Z inv;
4143: divlf(ONE,HCZ(p),&inv);
4144: ndv_mul_c_lf(p,inv);
4145: } else if ( mod )
4146: ndv_mul_c(mod,p,invm(HCM(p),mod));
4147: else {
4148: len = p->len;
4149: w = (P *)MALLOC(len*sizeof(P));
4150: c = (Z *)MALLOC(len*sizeof(Q));
4151: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4152: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4153: all_p = all_p && !NUM(w[i]);
4154: }
4155: if ( all_p ) {
4156: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4157: mulp(nd_vc,(P)dvr,g,&cont);
4158: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4159: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4160: }
4161: } else {
4162: sortbynm((Q *)c,len);
4163: qltozl((Q *)c,len,&dvr);
4164: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4165: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4166: }
4167: }
4168: }
4169: }
4170:
4171: /* koko */
4172:
4173: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4174: {
4175: int len,i,max;
4176: NMV m,mr0,mr,t;
4177:
4178: len = p->len;
1.14 noro 4179: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 4180: max = MAX(max,TD(DL(m)));
4181: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4182: m = (NMV)((char *)mr0+(len-1)*oadv);
4183: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4184: t = (NMV)MALLOC(nmv_adv);
4185: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4186: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4187: CZ(mr) = CZ(m);
1.1 noro 4188: ndl_copy(DL(t),DL(mr));
4189: }
4190: NV(p)++;
4191: BDY(p) = mr0;
4192: }
4193:
4194: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4195: {
4196: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4197: int pos;
4198: Q *w;
4199: Q dvr,t;
4200: NMV m,r;
4201:
4202: len = p->len;
4203: newnvar = nd_nvar-1;
4204: newexporigin = nd_get_exporigin(ord);
4205: if ( nd_module ) newmpos = newexporigin-1;
4206: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4207: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4208: ndl_dehomogenize(DL(m));
4209: if ( newwpd != nd_wpd ) {
4210: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4211: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 4212: CZ(r) = CZ(m);
1.1 noro 4213: if ( nd_module ) pos = MPOS(DL(m));
4214: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4215: adj = nd_exporigin-newexporigin;
4216: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
4217: if ( nd_module ) {
4218: DL(r)[newmpos] = pos;
4219: }
4220: }
4221: }
4222: NV(p)--;
4223: }
4224:
4225: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4226: {
4227: int i;
4228: P *tpl,*tpl1;
4229: NODE l;
4230: P h,gcd,t;
4231:
4232: tpl = (P *)MALLOC(m*sizeof(P));
4233: tpl1 = (P *)MALLOC(m*sizeof(P));
4234: bcopy(pl,tpl,m*sizeof(P));
4235: gcd = (P)ONE;
4236: for ( l = nd_hcf; l; l = NEXT(l) ) {
4237: h = (P)BDY(l);
4238: while ( 1 ) {
4239: for ( i = 0; i < m; i++ )
4240: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4241: break;
4242: if ( i == m ) {
4243: bcopy(tpl1,tpl,m*sizeof(P));
4244: mulp(vl,gcd,h,&t); gcd = t;
4245: } else
4246: break;
4247: }
4248: }
4249: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4250: if ( full ) {
4251: heu_nezgcdnpz(vl,tpl,m,&t);
4252: mulp(vl,gcd,t,pr);
4253: } else
4254: *pr = gcd;
4255: }
4256:
4257: void removecont_array(P *p,int n,int full)
4258: {
4259: int all_p,all_q,i;
4260: Z *c;
4261: P *w;
4262: P t,s;
4263:
4264: for ( all_q = 1, i = 0; i < n; i++ )
4265: all_q = all_q && NUM(p[i]);
4266: if ( all_q ) {
4267: removecont_array_q((Z *)p,n);
4268: } else {
4269: c = (Z *)MALLOC(n*sizeof(Z));
4270: w = (P *)MALLOC(n*sizeof(P));
4271: for ( i = 0; i < n; i++ ) {
4272: ptozp(p[i],1,(Q *)&c[i],&w[i]);
4273: }
4274: removecont_array_q(c,n);
4275: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4276: for ( i = 0; i < n; i++ ) {
4277: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4278: }
4279: }
4280: }
4281:
4282: /* c is an int array */
4283:
4284: void removecont_array_q(Z *c,int n)
4285: {
4286: struct oVECT v;
4287: Z d0,d1,a,u,u1,gcd;
4288: int i,j;
4289: Z *q,*r;
4290:
4291: q = (Z *)MALLOC(n*sizeof(Z));
4292: r = (Z *)MALLOC(n*sizeof(Z));
4293: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4294: gcdvz_estimate(&v,&d0);
4295: for ( i = 0; i < n; i++ ) {
4296: divqrz(c[i],d0,&q[i],&r[i]);
4297: }
4298: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4299: if ( i < n ) {
4300: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4301: gcdvz(&v,&d1);
4302: gcdz(d0,d1,&gcd);
1.6 noro 4303: /* exact division */
4304: divsz(d0,gcd,&a);
1.1 noro 4305: for ( i = 0; i < n; i++ ) {
4306: mulz(a,q[i],&u);
4307: if ( r[i] ) {
1.6 noro 4308: /* exact division */
4309: divsz(r[i],gcd,&u1);
1.1 noro 4310: addz(u,u1,&q[i]);
4311: } else
4312: q[i] = u;
4313: }
4314: }
4315: for ( i = 0; i < n; i++ ) c[i] = q[i];
4316: }
4317:
1.4 noro 4318: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
4319:
4320: void mpz_removecont_array(mpz_t *c,int n)
4321: {
4322: mpz_t d0,a,u,u1,gcd;
4323: int i,j;
1.13 noro 4324: static mpz_t *q,*r;
4325: static int c_len = 0;
1.4 noro 4326:
4327: for ( i = 0; i < n; i++ )
4328: if ( mpz_sgn(c[i]) ) break;
4329: if ( i == n ) return;
4330: gcdv_mpz_estimate(d0,c,n);
1.13 noro 4331: if ( n > c_len ) {
4332: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4333: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4334: c_len = n;
4335: }
1.4 noro 4336: for ( i = 0; i < n; i++ ) {
4337: mpz_init(q[i]); mpz_init(r[i]);
4338: mpz_fdiv_qr(q[i],r[i],c[i],d0);
4339: }
4340: for ( i = 0; i < n; i++ )
4341: if ( mpz_sgn(r[i]) ) break;
4342: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
4343: if ( i < n ) {
4344: mpz_gcd(gcd,d0,r[i]);
4345: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
4346: mpz_div(a,d0,gcd);
4347: for ( i = 0; i < n; i++ ) {
4348: mpz_mul(u,a,q[i]);
4349: if ( mpz_sgn(r[i]) ) {
4350: mpz_div(u1,r[i],gcd);
4351: mpz_add(q[i],u,u1);
4352: } else
4353: mpz_set(q[i],u);
4354: }
4355: }
4356: for ( i = 0; i < n; i++ )
4357: mpz_set(c[i],q[i]);
4358: }
4359:
1.1 noro 4360: void nd_mul_c(int mod,ND p,int mul)
4361: {
4362: NM m;
4363: int c,c1;
4364:
4365: if ( !p ) return;
4366: if ( mul == 1 ) return;
4367: if ( mod == -1 )
4368: for ( m = BDY(p); m; m = NEXT(m) )
4369: CM(m) = _mulsf(CM(m),mul);
4370: else
4371: for ( m = BDY(p); m; m = NEXT(m) ) {
4372: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4373: }
4374: }
4375:
4376: void nd_mul_c_lf(ND p,Z mul)
4377: {
4378: NM m;
4379: Z c;
4380:
4381: if ( !p ) return;
4382: if ( UNIZ(mul) ) return;
4383: for ( m = BDY(p); m; m = NEXT(m) ) {
4384: mullf(CZ(m),mul,&c); CZ(m) = c;
4385: }
4386: }
4387:
4388: void nd_mul_c_q(ND p,P mul)
4389: {
4390: NM m;
4391: P c;
4392:
4393: if ( !p ) return;
4394: if ( UNIQ(mul) ) return;
4395: for ( m = BDY(p); m; m = NEXT(m) ) {
4396: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4397: }
4398: }
4399:
4400: void nd_mul_c_p(VL vl,ND p,P mul)
4401: {
4402: NM m;
4403: P c;
4404:
4405: if ( !p ) return;
4406: for ( m = BDY(p); m; m = NEXT(m) ) {
4407: mulp(vl,CP(m),mul,&c); CP(m) = c;
4408: }
4409: }
4410:
4411: void nd_free(ND p)
4412: {
4413: NM t,s;
4414:
4415: if ( !p ) return;
4416: t = BDY(p);
4417: while ( t ) {
4418: s = NEXT(t);
4419: FREENM(t);
4420: t = s;
4421: }
4422: FREEND(p);
4423: }
4424:
4425: void ndv_free(NDV p)
4426: {
4427: GCFREE(BDY(p));
4428: }
4429:
4430: void nd_append_red(UINT *d,int i)
4431: {
4432: RHist m,m0;
4433: int h;
4434:
4435: NEWRHist(m);
4436: h = ndl_hash_value(d);
4437: m->index = i;
4438: ndl_copy(d,DL(m));
4439: NEXT(m) = nd_red[h];
4440: nd_red[h] = m;
4441: }
4442:
4443: UINT *ndv_compute_bound(NDV p)
4444: {
4445: UINT *d1,*d2,*t;
4446: UINT u;
4447: int i,j,k,l,len,ind;
4448: NMV m;
4449:
4450: if ( !p )
4451: return 0;
4452: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4453: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4454: len = LEN(p);
4455: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4456: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4457: ndl_max(DL(m),d1,d2);
4458: t = d1; d1 = d2; d2 = t;
4459: }
4460: l = nd_nvar+31;
4461: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4462: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4463: u = d1[i];
4464: k = (nd_epw-1)*nd_bpe;
4465: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4466: t[ind] = (u>>k)&nd_mask0;
4467: }
4468: for ( ; ind < l; ind++ ) t[ind] = 0;
4469: return t;
4470: }
4471:
4472: UINT *nd_compute_bound(ND p)
4473: {
4474: UINT *d1,*d2,*t;
4475: UINT u;
4476: int i,j,k,l,len,ind;
4477: NM m;
4478:
4479: if ( !p )
4480: return 0;
4481: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4482: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4483: len = LEN(p);
4484: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4485: for ( m = NEXT(m); m; m = NEXT(m) ) {
4486: ndl_lcm(DL(m),d1,d2);
4487: t = d1; d1 = d2; d2 = t;
4488: }
4489: l = nd_nvar+31;
4490: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4491: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4492: u = d1[i];
4493: k = (nd_epw-1)*nd_bpe;
4494: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4495: t[ind] = (u>>k)&nd_mask0;
4496: }
4497: for ( ; ind < l; ind++ ) t[ind] = 0;
4498: return t;
4499: }
4500:
4501: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4502: /* of a term. In this case we need additional 1 word. */
4503:
4504: int nd_get_exporigin(struct order_spec *ord)
4505: {
4506: switch ( ord->id ) {
1.21 noro 4507: case 0: case 2: case 256: case 258: case 300:
1.1 noro 4508: return 1+nd_module;
4509: case 1: case 257:
4510: /* block order */
4511: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4512: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4513: return ord->ord.block.length+1+nd_module;
4514: case 3: case 259:
4515: #if 0
4516: error("nd_get_exporigin : composite order is not supported yet.");
4517: #else
4518: return 1+nd_module;
4519: #endif
4520: default:
4521: error("nd_get_exporigin : ivalid argument.");
4522: return 0;
4523: }
4524: }
4525:
4526: void nd_setup_parameters(int nvar,int max) {
4527: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4528: struct order_pair *op;
4529:
4530: nd_nvar = nvar;
4531: if ( max ) {
4532: /* XXX */
4533: if ( do_weyl ) nd_bpe = 32;
4534: else if ( max < 2 ) nd_bpe = 1;
4535: else if ( max < 4 ) nd_bpe = 2;
4536: else if ( max < 8 ) nd_bpe = 3;
4537: else if ( max < 16 ) nd_bpe = 4;
4538: else if ( max < 32 ) nd_bpe = 5;
4539: else if ( max < 64 ) nd_bpe = 6;
4540: else if ( max < 256 ) nd_bpe = 8;
4541: else if ( max < 1024 ) nd_bpe = 10;
4542: else if ( max < 65536 ) nd_bpe = 16;
4543: else nd_bpe = 32;
4544: }
4545: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4546: UINT t;
4547: int st;
4548: int *v;
4549: /* t = max(weights) */
4550: t = 0;
4551: if ( current_dl_weight_vector )
4552: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4553: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4554: if ( t < st ) t = st;
4555: }
4556: if ( nd_matrix )
4557: for ( i = 0; i < nd_matrix_len; i++ )
4558: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4559: if ( (st=v[j]) < 0 ) st = -st;
4560: if ( t < st ) t = st;
4561: }
4562: /* i = bitsize of t */
4563: for ( i = 0; t; t >>=1, i++ );
4564: /* i += bitsize of nd_nvar */
4565: for ( t = nd_nvar; t; t >>=1, i++);
4566: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4567: if ( (nd_bpe+i) >= 31 )
4568: error("nd_setup_parameters : too large weight");
4569: }
4570: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4571: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4572: nd_exporigin = nd_get_exporigin(nd_ord);
4573: wpd = nd_exporigin+elen;
4574: if ( nd_module )
4575: nd_mpos = nd_exporigin-1;
4576: else
4577: nd_mpos = -1;
4578: if ( wpd != nd_wpd ) {
4579: nd_free_private_storage();
4580: nd_wpd = wpd;
4581: }
4582: if ( nd_bpe < 32 ) {
4583: nd_mask0 = (1<<nd_bpe)-1;
4584: } else {
4585: nd_mask0 = 0xffffffff;
4586: }
4587: bzero(nd_mask,sizeof(nd_mask));
4588: nd_mask1 = 0;
4589: for ( i = 0; i < nd_epw; i++ ) {
4590: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4591: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4592: }
4593: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4594: nd_epos = nd_create_epos(nd_ord);
4595: nd_blockmask = nd_create_blockmask(nd_ord);
4596: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4597: }
4598:
4599: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4600: {
4601: int i,obpe,oadv,h;
4602: static NM prev_nm_free_list;
4603: static ND_pairs prev_ndp_free_list;
4604: RHist mr0,mr;
4605: RHist r;
4606: RHist *old_red;
4607: ND_pairs s0,s,t;
4608: EPOS oepos;
4609:
4610: obpe = nd_bpe;
4611: oadv = nmv_adv;
4612: oepos = nd_epos;
4613: if ( obpe < 2 ) nd_bpe = 2;
4614: else if ( obpe < 3 ) nd_bpe = 3;
4615: else if ( obpe < 4 ) nd_bpe = 4;
4616: else if ( obpe < 5 ) nd_bpe = 5;
4617: else if ( obpe < 6 ) nd_bpe = 6;
4618: else if ( obpe < 8 ) nd_bpe = 8;
4619: else if ( obpe < 10 ) nd_bpe = 10;
4620: else if ( obpe < 16 ) nd_bpe = 16;
4621: else if ( obpe < 32 ) nd_bpe = 32;
4622: else error("nd_reconstruct : exponent too large");
4623:
4624: nd_setup_parameters(nd_nvar,0);
4625: prev_nm_free_list = _nm_free_list;
4626: prev_ndp_free_list = _ndp_free_list;
4627: _nm_free_list = 0;
4628: _ndp_free_list = 0;
4629: for ( i = nd_psn-1; i >= 0; i-- ) {
4630: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4631: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4632: }
4633: if ( trace )
4634: for ( i = nd_psn-1; i >= 0; i-- ) {
4635: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4636: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4637: }
4638: s0 = 0;
4639: for ( t = d; t; t = NEXT(t) ) {
4640: NEXTND_pairs(s0,s);
4641: s->i1 = t->i1;
4642: s->i2 = t->i2;
4643: SG(s) = SG(t);
4644: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4645: }
4646:
4647: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4648: for ( i = 0; i < REDTAB_LEN; i++ ) {
4649: old_red[i] = nd_red[i];
4650: nd_red[i] = 0;
4651: }
4652: for ( i = 0; i < REDTAB_LEN; i++ )
4653: for ( r = old_red[i]; r; r = NEXT(r) ) {
4654: NEWRHist(mr);
4655: mr->index = r->index;
4656: SG(mr) = SG(r);
4657: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4658: h = ndl_hash_value(DL(mr));
4659: NEXT(mr) = nd_red[h];
4660: nd_red[h] = mr;
4661: }
4662: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4663: old_red = 0;
4664: for ( i = 0; i < nd_psn; i++ ) {
4665: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4666: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4667: nd_psh[i] = r;
4668: }
4669: if ( s0 ) NEXT(s) = 0;
4670: prev_nm_free_list = 0;
4671: prev_ndp_free_list = 0;
4672: #if 0
4673: GC_gcollect();
4674: #endif
4675: return s0;
4676: }
4677:
4678: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4679: {
4680: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4681: struct order_pair *op;
4682:
4683: n = nd_nvar;
4684: oepw = (sizeof(UINT)*8)/obpe;
4685: omask0 = (1<<obpe)-1;
4686: TD(r) = TD(d);
4687: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4688: if ( nd_blockmask ) {
4689: l = nd_blockmask->n;
4690: op = nd_blockmask->order_pair;
4691: for ( i = 1; i < nd_exporigin; i++ )
4692: r[i] = d[i];
4693: for ( j = 0, s = 0; j < l; j++ ) {
4694: ord_l = op[j].length;
4695: for ( i = 0; i < ord_l; i++, s++ ) {
4696: ei = GET_EXP_OLD(d,s);
4697: PUT_EXP(r,s,ei);
4698: }
4699: }
4700: } else {
4701: for ( i = 0; i < n; i++ ) {
4702: ei = GET_EXP_OLD(d,i);
4703: PUT_EXP(r,i,ei);
4704: }
4705: }
4706: if ( nd_module ) MPOS(r) = MPOS(d);
4707: }
4708:
4709: ND nd_copy(ND p)
4710: {
4711: NM m,mr,mr0;
4712: int c,n;
4713: ND r;
4714:
4715: if ( !p )
4716: return 0;
4717: else {
4718: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4719: NEXTNM(mr0,mr);
4720: CM(mr) = CM(m);
4721: ndl_copy(DL(m),DL(mr));
4722: }
4723: NEXT(mr) = 0;
4724: MKND(NV(p),mr0,LEN(p),r);
4725: SG(r) = SG(p);
4726: return r;
4727: }
4728: }
4729:
4730: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4731: {
4732: NM m1,m2;
4733: NDV p1,p2;
4734: ND t1,t2;
4735: UINT *lcm;
4736: P gp,tp;
4737: Z g,t;
4738: Z iq;
4739: int td;
4740: LIST hist;
4741: NODE node;
4742: DP d;
4743:
4744: if ( !mod && nd_demand ) {
4745: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4746: } else {
4747: if ( trace ) {
4748: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4749: } else {
4750: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4751: }
4752: }
4753: lcm = LCM(p);
4754: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4755: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4756: FREENM(m1); return 0;
4757: }
4758: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4759: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4760: FREENM(m1); FREENM(m2); return 0;
4761: }
4762:
4763: if ( mod == -1 ) {
4764: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4765: } else if ( mod > 0 ) {
4766: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4767: } else if ( mod == -2 ) {
4768: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4769: } else if ( nd_vc ) {
4770: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4771: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4772: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4773: } else {
1.6 noro 4774: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 4775: }
4776: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4777: *rp = nd_add(mod,t1,t2);
4778: if ( nd_gentrace ) {
4779: /* nd_tracelist is initialized */
1.6 noro 4780: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4781: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 4782: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4783: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4784: nd_tracelist = node;
4785: }
4786: FREENM(m1); FREENM(m2);
4787: return 1;
4788: }
4789:
4790: void ndv_mul_c(int mod,NDV p,int mul)
4791: {
4792: NMV m;
4793: int c,c1,len,i;
4794:
4795: if ( !p ) return;
4796: len = LEN(p);
4797: if ( mod == -1 )
4798: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4799: CM(m) = _mulsf(CM(m),mul);
4800: else
4801: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4802: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4803: }
4804: }
4805:
4806: void ndv_mul_c_lf(NDV p,Z mul)
4807: {
4808: NMV m;
4809: Z c;
4810: int len,i;
4811:
4812: if ( !p ) return;
4813: len = LEN(p);
4814: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4815: mullf(CZ(m),mul,&c); CZ(m) = c;
4816: }
4817: }
4818:
4819: /* for nd_det */
4820: void ndv_mul_c_q(NDV p,Z mul)
4821: {
4822: NMV m;
4823: Z c;
4824: int len,i;
4825:
4826: if ( !p ) return;
4827: len = LEN(p);
4828: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 4829: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 4830: }
4831: }
4832:
4833: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4834: int n2,i,j,l,n,tlen;
4835: UINT *d0;
4836: NM *tab,*psum;
4837: ND s,r;
4838: NM t;
4839: NMV m1;
4840:
4841: if ( !p ) return 0;
4842: n = NV(p); n2 = n>>1;
4843: d0 = DL(m0);
4844: l = LEN(p);
4845: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4846: tab = (NM *)MALLOC(tlen*sizeof(NM));
4847: psum = (NM *)MALLOC(tlen*sizeof(NM));
4848: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4849: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4850: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4851: /* m0(NM) * m1(NMV) => tab(NM) */
4852: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4853: for ( j = 0; j < tlen; j++ ) {
4854: if ( tab[j] ) {
4855: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4856: }
4857: }
4858: }
4859: for ( i = tlen-1, r = 0; i >= 0; i-- )
4860: if ( psum[i] ) {
4861: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4862: MKND(n,psum[i],j,s);
4863: r = nd_add(mod,r,s);
4864: }
4865: if ( r ) SG(r) = SG(p)+TD(d0);
4866: return r;
4867: }
4868:
4869: /* product of monomials */
4870: /* XXX block order is not handled correctly */
4871:
4872: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4873: {
4874: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4875: UINT *d0,*d1,*d,*dt,*ctab;
4876: Z *ctab_q;
4877: Z q,q1;
4878: UINT c0,c1,c;
4879: NM *p;
4880: NM m,t;
4881: int mpos;
4882:
4883: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4884: if ( !m0 || !m1 ) return;
4885: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4886: if ( nd_module )
4887: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4888:
4889: NEWNM(m); d = DL(m);
4890: if ( mod ) {
4891: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4892: } else if ( nd_vc )
4893: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4894: else
1.6 noro 4895: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 4896: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4897: homo = n&1 ? 1 : 0;
4898: if ( homo ) {
4899: /* offset of h-degree */
4900: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4901: PUT_EXP(DL(m),n-1,h);
4902: TD(DL(m)) = h;
4903: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4904: }
4905: tab[0] = m;
4906: NEWNM(m); d = DL(m);
4907: for ( i = 0, curlen = 1; i < n2; i++ ) {
4908: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4909: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4910: /* xi^a*(Di^k*xi^l)*Di^b */
4911: a += l; b += k;
4912: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4913: if ( !k || !l ) {
4914: for ( j = 0; j < curlen; j++ )
4915: if ( (t = tab[j]) != 0 ) {
4916: dt = DL(t);
4917: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4918: if ( nd_blockmask ) ndl_weight_mask(dt);
4919: }
4920: curlen *= k+1;
4921: continue;
4922: }
4923: min = MIN(k,l);
4924: if ( mod ) {
4925: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4926: mkwcm(k,l,mod,(int *)ctab);
4927: } else {
4928: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4929: mkwc(k,l,ctab_q);
4930: }
4931: for ( j = min; j >= 0; j-- ) {
4932: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4933: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4934: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4935: if ( homo ) {
4936: TD(d) = s;
4937: PUT_EXP(d,n-1,s-h);
4938: } else TD(d) = h;
4939: if ( nd_blockmask ) ndl_weight_mask(d);
4940: if ( mod ) c = ctab[j];
4941: else q = ctab_q[j];
4942: p = tab+curlen*j;
4943: if ( j == 0 ) {
4944: for ( u = 0; u < curlen; u++, p++ ) {
4945: if ( tab[u] ) {
4946: ndl_addto(DL(tab[u]),d);
4947: if ( mod ) {
4948: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4949: } else if ( nd_vc )
4950: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4951: else {
1.6 noro 4952: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 4953: }
4954: }
4955: }
4956: } else {
4957: for ( u = 0; u < curlen; u++, p++ ) {
4958: if ( tab[u] ) {
4959: NEWNM(t);
4960: ndl_add(DL(tab[u]),d,DL(t));
4961: if ( mod ) {
4962: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4963: } else if ( nd_vc )
4964: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4965: else
1.6 noro 4966: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 4967: *p = t;
4968: }
4969: }
4970: }
4971: }
4972: curlen *= k+1;
4973: }
4974: FREENM(m);
4975: if ( nd_module ) {
4976: mpos = MPOS(d1);
4977: for ( i = 0; i < tlen; i++ )
4978: if ( tab[i] ) {
4979: d = DL(tab[i]);
4980: MPOS(d) = mpos;
4981: TD(d) = ndl_weight(d);
4982: }
4983: }
4984: }
4985:
4986: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4987: {
4988: NM mr,mr0;
4989: NMV m;
4990: UINT *d,*dt,*dm;
4991: int c,n,td,i,c1,c2,len;
4992: Q q;
4993: ND r;
4994:
4995: if ( !p ) return 0;
4996: else {
4997: n = NV(p); m = BDY(p);
4998: d = DL(m0);
4999: len = LEN(p);
5000: mr0 = 0;
5001: td = TD(d);
5002: c = CM(m0);
5003: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5004: NEXTNM(mr0,mr);
5005: CM(mr) = 1;
5006: ndl_add(DL(m),d,DL(mr));
5007: }
5008: NEXT(mr) = 0;
5009: MKND(NV(p),mr0,len,r);
5010: SG(r) = SG(p) + TD(d);
5011: return r;
5012: }
5013: }
5014:
5015: ND ndv_mul_nm(int mod,NM m0,NDV p)
5016: {
5017: NM mr,mr0;
5018: NMV m;
5019: UINT *d,*dt,*dm;
5020: int c,n,td,i,c1,c2,len;
5021: P q;
5022: ND r;
5023:
5024: if ( !p ) return 0;
5025: else if ( do_weyl ) {
5026: if ( mod < 0 ) {
5027: error("ndv_mul_nm : not implemented (weyl)");
5028: return 0;
5029: } else
5030: return weyl_ndv_mul_nm(mod,m0,p);
5031: } else {
5032: n = NV(p); m = BDY(p);
5033: d = DL(m0);
5034: len = LEN(p);
5035: mr0 = 0;
5036: td = TD(d);
5037: if ( mod == -1 ) {
5038: c = CM(m0);
5039: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5040: NEXTNM(mr0,mr);
5041: CM(mr) = _mulsf(CM(m),c);
5042: ndl_add(DL(m),d,DL(mr));
5043: }
5044: } else if ( mod == -2 ) {
5045: Z cl;
5046: cl = CZ(m0);
5047: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5048: NEXTNM(mr0,mr);
5049: mullf(CZ(m),cl,&CZ(mr));
5050: ndl_add(DL(m),d,DL(mr));
5051: }
5052: } else if ( mod ) {
5053: c = CM(m0);
5054: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5055: NEXTNM(mr0,mr);
5056: c1 = CM(m);
5057: DMAR(c1,c,0,mod,c2);
5058: CM(mr) = c2;
5059: ndl_add(DL(m),d,DL(mr));
5060: }
5061: } else {
5062: q = CP(m0);
5063: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5064: NEXTNM(mr0,mr);
5065: mulp(nd_vc,CP(m),q,&CP(mr));
5066: ndl_add(DL(m),d,DL(mr));
5067: }
5068: }
5069: NEXT(mr) = 0;
5070: MKND(NV(p),mr0,len,r);
5071: SG(r) = SG(p) + TD(d);
5072: return r;
5073: }
5074: }
5075:
5076: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
5077: {
5078: NM mq0,mq;
5079: NMV tm;
5080: Q q;
5081: int i,nv,sg,c,c1,c2,hindex;
5082: ND p,t,r;
5083:
5084: if ( bucket->m < 0 ) return 0;
5085: else {
5086: nv = NV(d);
5087: mq0 = 0;
5088: tm = (NMV)MALLOC(nmv_adv);
5089: while ( 1 ) {
5090: if ( mod > 0 || mod == -1 )
5091: hindex = head_pbucket(mod,bucket);
5092: else if ( mod == -2 )
5093: hindex = head_pbucket_lf(bucket);
5094: else
5095: hindex = head_pbucket_q(bucket);
5096: if ( hindex < 0 ) break;
5097: p = bucket->body[hindex];
5098: NEXTNM(mq0,mq);
5099: ndl_sub(HDL(p),HDL(d),DL(mq));
5100: ndl_copy(DL(mq),DL(tm));
5101: if ( mod ) {
5102: c1 = invm(HCM(d),mod); c2 = HCM(p);
5103: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5104: CM(tm) = mod-c;
5105: } else {
1.6 noro 5106: divsz(HCZ(p),HCZ(d),&CZ(mq));
5107: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5108: }
5109: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5110: bucket->body[hindex] = nd_remove_head(p);
5111: t = nd_remove_head(t);
5112: add_pbucket(mod,bucket,t);
5113: }
5114: if ( !mq0 )
5115: r = 0;
5116: else {
5117: NEXT(mq) = 0;
5118: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5119: MKND(nv,mq0,i,r);
5120: /* XXX */
5121: SG(r) = HTD(r);
5122: }
5123: return r;
5124: }
5125: }
5126:
5127: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5128: {
5129: NMV m,mr,mr0,t;
5130: int len,i,k;
5131:
5132: if ( !p ) return;
5133: m = BDY(p); len = LEN(p);
5134: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5135: m = (NMV)((char *)mr0+(len-1)*oadv);
5136: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5137: t = (NMV)MALLOC(nmv_adv);
5138: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5139: CZ(t) = CZ(m);
1.1 noro 5140: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5141: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5142: CZ(mr) = CZ(t);
1.1 noro 5143: ndl_copy(DL(t),DL(mr));
5144: }
5145: BDY(p) = mr0;
5146: }
5147:
5148: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5149: {
5150: NMV m,mr,mr0;
5151: int len,i;
5152: NDV r;
5153:
5154: if ( !p ) return 0;
5155: m = BDY(p); len = LEN(p);
5156: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5157: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5158: ndl_zero(DL(mr));
5159: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5160: CZ(mr) = CZ(m);
1.1 noro 5161: }
5162: MKNDV(NV(p),mr0,len,r);
5163: SG(r) = SG(p);
5164: return r;
5165: }
5166:
5167: /* duplicate p */
5168:
5169: NDV ndv_dup(int mod,NDV p)
5170: {
5171: NDV d;
5172: NMV t,m,m0;
5173: int i,len;
5174:
5175: if ( !p ) return 0;
5176: len = LEN(p);
5177: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5178: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5179: ndl_copy(DL(t),DL(m));
1.6 noro 5180: CZ(m) = CZ(t);
1.1 noro 5181: }
5182: MKNDV(NV(p),m0,len,d);
5183: SG(d) = SG(p);
5184: return d;
5185: }
5186:
5187: NDV ndv_symbolic(int mod,NDV p)
5188: {
5189: NDV d;
5190: NMV t,m,m0;
5191: int i,len;
5192:
5193: if ( !p ) return 0;
5194: len = LEN(p);
5195: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5196: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5197: ndl_copy(DL(t),DL(m));
1.6 noro 5198: CZ(m) = ONE;
1.1 noro 5199: }
5200: MKNDV(NV(p),m0,len,d);
5201: SG(d) = SG(p);
5202: return d;
5203: }
5204:
5205: ND nd_dup(ND p)
5206: {
5207: ND d;
5208: NM t,m,m0;
5209:
5210: if ( !p ) return 0;
5211: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5212: NEXTNM(m0,m);
5213: ndl_copy(DL(t),DL(m));
1.6 noro 5214: CZ(m) = CZ(t);
1.1 noro 5215: }
5216: if ( m0 ) NEXT(m) = 0;
5217: MKND(NV(p),m0,LEN(p),d);
5218: SG(d) = SG(p);
5219: return d;
5220: }
5221:
5222: /* XXX if p->len == 0 then it represents 0 */
5223:
5224: void ndv_mod(int mod,NDV p)
5225: {
5226: NMV t,d;
5227: int r,s,u;
5228: int i,len,dlen;
5229: P cp;
5230: Z c;
5231: Obj gfs;
5232:
5233: if ( !p ) return;
5234: len = LEN(p);
5235: dlen = 0;
5236: if ( mod == -1 )
5237: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5238: simp_ff((Obj)CP(t),&gfs);
5239: if ( gfs ) {
5240: r = FTOIF(CONT((GFS)gfs));
5241: CM(d) = r;
5242: ndl_copy(DL(t),DL(d));
5243: NMV_ADV(d);
5244: dlen++;
5245: }
5246: }
5247: else if ( mod == -2 )
5248: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5249: simp_ff((Obj)CP(t),&gfs);
5250: if ( gfs ) {
5251: lmtolf((LM)gfs,&CZ(d));
5252: ndl_copy(DL(t),DL(d));
5253: NMV_ADV(d);
5254: dlen++;
5255: }
5256: }
5257: else
5258: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5259: if ( nd_vc ) {
5260: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5261: c = (Z)cp;
5262: } else
1.6 noro 5263: c = CZ(t);
1.1 noro 5264: r = remqi((Q)c,mod);
5265: if ( r ) {
5266: CM(d) = r;
5267: ndl_copy(DL(t),DL(d));
5268: NMV_ADV(d);
5269: dlen++;
5270: }
5271: }
5272: LEN(p) = dlen;
5273: }
5274:
5275: NDV ptondv(VL vl,VL dvl,P p)
5276: {
5277: ND nd;
5278:
5279: nd = ptond(vl,dvl,p);
5280: return ndtondv(0,nd);
5281: }
5282:
5283: void pltozpl(LIST l,Q *cont,LIST *pp)
5284: {
1.16 noro 5285: NODE nd,nd1;
5286: int n;
5287: P *pl;
5288: Q *cl;
5289: int i;
5290: P dmy;
5291: Z dvr,inv;
5292: LIST r;
5293:
5294: nd = BDY(l); n = length(nd);
5295: pl = (P *)MALLOC(n*sizeof(P));
5296: cl = (Q *)MALLOC(n*sizeof(Q));
5297: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5298: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5299: }
5300: qltozl(cl,n,&dvr);
5301: divz(ONE,dvr,&inv);
5302: nd = BDY(l);
5303: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5304: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5305: nd = 0;
5306: for ( i = n-1; i >= 0; i-- ) {
5307: MKNODE(nd1,pl[i],nd); nd = nd1;
5308: }
5309: MKLIST(r,nd);
5310: *pp = r;
1.1 noro 5311: }
5312:
5313: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5314:
5315: NDV pltondv(VL vl,VL dvl,LIST p)
5316: {
5317: int i;
5318: NODE t;
5319: ND r,ri;
5320: NM m;
5321:
5322: if ( !nd_module ) error("pltond : module order must be set");
5323: r = 0;
5324: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5325: ri = ptond(vl,dvl,(P)BDY(t));
5326: if ( ri )
5327: for ( m = BDY(ri); m; m = NEXT(m) ) {
5328: MPOS(DL(m)) = i;
5329: TD(DL(m)) = ndl_weight(DL(m));
5330: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5331: }
5332: r = nd_add(0,r,ri);
5333: }
5334: return ndtondv(0,r);
5335: }
5336:
5337: ND ptond(VL vl,VL dvl,P p)
5338: {
5339: int n,i,j,k,e;
5340: VL tvl;
5341: V v;
5342: DCP dc;
5343: DCP *w;
5344: ND r,s,t,u;
5345: P x;
5346: int c;
5347: UINT *d;
5348: NM m,m0;
5349:
5350: if ( !p )
5351: return 0;
5352: else if ( NUM(p) ) {
5353: NEWNM(m);
5354: ndl_zero(DL(m));
5355: if ( !INT((Q)p) )
5356: error("ptond : input must be integer-coefficient");
1.6 noro 5357: CZ(m) = (Z)p;
1.1 noro 5358: NEXT(m) = 0;
5359: MKND(nd_nvar,m,1,r);
5360: SG(r) = 0;
5361: return r;
5362: } else {
5363: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5364: w = (DCP *)MALLOC(k*sizeof(DCP));
5365: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5366: for ( i = 0, tvl = dvl, v = VR(p);
5367: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5368: if ( !tvl ) {
5369: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5370: t = ptond(vl,dvl,COEF(w[j]));
5371: pwrp(vl,x,DEG(w[j]),&p);
5372: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5373: }
5374: return s;
5375: } else {
5376: NEWNM(m0); d = DL(m0);
5377: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 5378: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 5379: TD(d) = MUL_WEIGHT(e,i);
5380: if ( nd_blockmask) ndl_weight_mask(d);
5381: if ( nd_module ) MPOS(d) = 0;
5382: t = ptond(vl,dvl,COEF(w[j]));
5383: for ( m = BDY(t); m; m = NEXT(m) )
5384: ndl_addto(DL(m),d);
5385: SG(t) += TD(d);
5386: s = nd_add(0,s,t);
5387: }
5388: FREENM(m0);
5389: return s;
5390: }
5391: }
5392: }
5393:
5394: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5395: {
5396: VL tvl;
5397: int len,n,j,i,e;
5398: NMV m;
5399: Z q;
5400: P c;
5401: UINT *d;
5402: P s,r,u,t,w;
5403: GFS gfs;
5404:
5405: if ( !p ) return 0;
5406: else {
5407: len = LEN(p);
5408: n = NV(p);
5409: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5410: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5411: if ( mod == -1 ) {
5412: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5413: } else if ( mod == -2 ) {
5414: c = (P)CZ(m);
5415: } else if ( mod > 0 ) {
1.6 noro 5416: STOZ(CM(m),q); c = (P)q;
1.1 noro 5417: } else
5418: c = CP(m);
5419: d = DL(m);
5420: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5421: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5422: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5423: }
5424: addp(vl,s,t,&u); s = u;
5425: }
5426: return s;
5427: }
5428: }
5429:
5430: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5431: {
5432: VL tvl;
5433: int len,n,j,i,e;
5434: NMV m;
5435: Z q;
5436: P c;
5437: UINT *d;
5438: P s,r,u,t,w;
5439: GFS gfs;
5440: P *a;
5441: LIST l;
5442: NODE nd,nd1;
5443:
5444: if ( !p ) return 0;
5445: else {
5446: a = (P *)MALLOC((rank+1)*sizeof(P));
5447: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5448: len = LEN(p);
5449: n = NV(p);
5450: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5451: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5452: if ( mod == -1 ) {
5453: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5454: } else if ( mod ) {
1.6 noro 5455: STOZ(CM(m),q); c = (P)q;
1.1 noro 5456: } else
5457: c = CP(m);
5458: d = DL(m);
5459: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5460: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5461: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5462: }
5463: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5464: }
5465: nd = 0;
5466: for ( i = rank; i > 0; i-- ) {
5467: MKNODE(nd1,a[i],nd); nd = nd1;
5468: }
5469: MKLIST(l,nd);
5470: return l;
5471: }
5472: }
5473:
5474: NDV ndtondv(int mod,ND p)
5475: {
5476: NDV d;
5477: NMV m,m0;
5478: NM t;
5479: int i,len;
5480:
5481: if ( !p ) return 0;
5482: len = LEN(p);
5483: if ( mod > 0 || mod == -1 )
5484: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5485: else
5486: m0 = m = MALLOC(len*nmv_adv);
5487: #if 0
5488: ndv_alloc += nmv_adv*len;
5489: #endif
5490: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5491: ndl_copy(DL(t),DL(m));
1.6 noro 5492: CZ(m) = CZ(t);
1.1 noro 5493: }
5494: MKNDV(NV(p),m0,len,d);
5495: SG(d) = SG(p);
5496: return d;
5497: }
5498:
1.16 noro 5499: static int dmm_comp_nv;
5500:
5501: int dmm_comp(DMM *a,DMM *b)
5502: {
5503: return -compdmm(dmm_comp_nv,*a,*b);
5504: }
5505:
5506: void dmm_sort_by_ord(DMM *a,int len,int nv)
5507: {
5508: dmm_comp_nv = nv;
5509: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
5510: }
5511:
5512: void dpm_sort(DPM p,DPM *rp)
5513: {
5514: DMM t,t1;
5515: int len,i,n;
5516: DMM *a;
5517: DPM d;
5518:
5519: if ( !p ) *rp = 0;
5520: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
5521: a = (DMM *)MALLOC(len*sizeof(DMM));
5522: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
5523: n = p->nv;
5524: dmm_sort_by_ord(a,len,n);
5525: t = 0;
5526: for ( i = len-1; i >= 0; i-- ) {
5527: NEWDMM(t1);
5528: t1->c = a[i]->c;
5529: t1->dl = a[i]->dl;
5530: t1->pos = a[i]->pos;
5531: t1->next = t;
5532: t = t1;
5533: }
5534: MKDPM(n,t,d);
5535: SG(d) = SG(p);
5536: *rp = d;
5537: }
5538:
1.18 noro 5539: int dpm_comp(DPM *a,DPM *b)
5540: {
1.22 noro 5541: return -compdpm(CO,*a,*b);
1.18 noro 5542: }
5543:
5544: NODE dpm_sort_list(NODE l)
5545: {
5546: int i,len;
5547: NODE t,t1;
5548: DPM *a;
5549:
5550: len = length(l);
5551: a = (DPM *)MALLOC(len*sizeof(DPM));
5552: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
5553: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
5554: t = 0;
5555: for ( i = len-1; i >= 0; i-- ) {
5556: MKNODE(t1,(pointer)a[i],t); t = t1;
5557: }
5558: return t;
5559: }
5560:
1.20 noro 5561: int nmv_comp(NMV a,NMV b)
5562: {
1.21 noro 5563: int t;
5564: t = DL_COMPARE(a->dl,b->dl);
5565: return -t;
1.20 noro 5566: }
5567:
1.16 noro 5568: NDV dpmtondv(int mod,DPM p)
5569: {
5570: NDV d;
5571: NMV m,m0;
5572: DMM t;
5573: DMM *a;
5574: int i,len,n;
5575:
5576: if ( !p ) return 0;
5577: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
5578: a = (DMM *)MALLOC(len*sizeof(DMM));
5579: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
5580: n = p->nv;
5581: dmm_sort_by_ord(a,len,n);
5582: if ( mod > 0 || mod == -1 )
5583: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5584: else
5585: m0 = m = MALLOC(len*nmv_adv);
5586: #if 0
5587: ndv_alloc += nmv_adv*len;
5588: #endif
5589: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5590: dltondl(n,a[i]->dl,DL(m));
5591: MPOS(DL(m)) = a[i]->pos;
1.20 noro 5592: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 5593: CZ(m) = (Z)a[i]->c;
5594: }
1.20 noro 5595: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 5596: MKNDV(NV(p),m0,len,d);
5597: SG(d) = SG(p);
5598: return d;
5599: }
5600:
1.1 noro 5601: ND ndvtond(int mod,NDV p)
5602: {
5603: ND d;
5604: NM m,m0;
5605: NMV t;
5606: int i,len;
5607:
5608: if ( !p ) return 0;
5609: m0 = 0;
5610: len = p->len;
5611: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5612: NEXTNM(m0,m);
5613: ndl_copy(DL(t),DL(m));
1.6 noro 5614: CZ(m) = CZ(t);
1.1 noro 5615: }
5616: NEXT(m) = 0;
5617: MKND(NV(p),m0,len,d);
5618: SG(d) = SG(p);
5619: return d;
5620: }
5621:
5622: DP ndvtodp(int mod,NDV p)
5623: {
5624: MP m,m0;
5625: DP d;
5626: NMV t;
5627: int i,len;
5628:
5629: if ( !p ) return 0;
5630: m0 = 0;
5631: len = p->len;
5632: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5633: NEXTMP(m0,m);
5634: m->dl = ndltodl(nd_nvar,DL(t));
5635: m->c = (Obj)ndctop(mod,t->c);
5636: }
5637: NEXT(m) = 0;
5638: MKDP(nd_nvar,m0,d);
5639: SG(d) = SG(p);
5640: return d;
5641: }
5642:
1.16 noro 5643: DPM ndvtodpm(int mod,NDV p)
5644: {
5645: DMM m,m0;
5646: DPM d;
5647: NMV t;
5648: int i,len;
5649:
5650: if ( !p ) return 0;
5651: m0 = 0;
5652: len = p->len;
5653: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5654: NEXTDMM(m0,m);
5655: m->dl = ndltodl(nd_nvar,DL(t));
5656: m->c = (Obj)ndctop(mod,t->c);
5657: m->pos = MPOS(DL(t));
5658: }
5659: NEXT(m) = 0;
5660: MKDPM(nd_nvar,m0,d);
5661: SG(d) = SG(p);
5662: return d;
5663: }
5664:
5665:
1.1 noro 5666: DP ndtodp(int mod,ND p)
5667: {
5668: MP m,m0;
5669: DP d;
5670: NM t;
5671: int i,len;
5672:
5673: if ( !p ) return 0;
5674: m0 = 0;
5675: len = p->len;
5676: for ( t = BDY(p); t; t = NEXT(t) ) {
5677: NEXTMP(m0,m);
5678: m->dl = ndltodl(nd_nvar,DL(t));
5679: m->c = (Obj)ndctop(mod,t->c);
5680: }
5681: NEXT(m) = 0;
5682: MKDP(nd_nvar,m0,d);
5683: SG(d) = SG(p);
5684: return d;
5685: }
5686:
5687: void ndv_print(NDV p)
5688: {
5689: NMV m;
5690: int i,len;
5691:
5692: if ( !p ) printf("0\n");
5693: else {
5694: len = LEN(p);
5695: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5696: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5697: else printf("+%d*",CM(m));
5698: ndl_print(DL(m));
5699: }
5700: printf("\n");
5701: }
5702: }
5703:
5704: void ndv_print_q(NDV p)
5705: {
5706: NMV m;
5707: int i,len;
5708:
5709: if ( !p ) printf("0\n");
5710: else {
5711: len = LEN(p);
5712: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5713: printf("+");
1.6 noro 5714: printexpr(CO,(Obj)CZ(m));
1.1 noro 5715: printf("*");
5716: ndl_print(DL(m));
5717: }
5718: printf("\n");
5719: }
5720: }
5721:
5722: NODE ndv_reducebase(NODE x,int *perm)
5723: {
5724: int len,i,j;
5725: NDVI w;
5726: NODE t,t0;
5727:
5728: len = length(x);
5729: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5730: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5731: w[i].p = BDY(t); w[i].i = perm[i];
5732: }
5733: for ( i = 0; i < len; i++ ) {
5734: for ( j = 0; j < i; j++ ) {
5735: if ( w[i].p && w[j].p ) {
5736: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5737: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5738: }
5739: }
5740: }
5741: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5742: if ( w[i].p ) {
5743: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5744: perm[j++] = w[i].i;
5745: }
5746: }
5747: NEXT(t) = 0; x = t0;
5748: return x;
5749: }
5750:
5751: /* XXX incomplete */
5752:
1.21 noro 5753: extern DMMstack dmm_stack;
5754: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 5755:
1.1 noro 5756: void nd_init_ord(struct order_spec *ord)
5757: {
5758: nd_module = (ord->id >= 256);
5759: if ( nd_module ) {
5760: nd_dcomp = -1;
1.21 noro 5761: nd_module_ordtype = ord->module_ordtype;
1.1 noro 5762: nd_pot_nelim = ord->pot_nelim;
5763: nd_poly_weight_len = ord->nv;
5764: nd_poly_weight = ord->top_weight;
5765: nd_module_rank = ord->module_rank;
5766: nd_module_weight = ord->module_top_weight;
5767: }
5768: nd_matrix = 0;
5769: nd_matrix_len = 0;
5770: switch ( ord->id ) {
5771: case 0:
5772: switch ( ord->ord.simple ) {
5773: case 0:
5774: nd_dcomp = 1;
5775: nd_isrlex = 1;
5776: break;
5777: case 1:
5778: nd_dcomp = 1;
5779: nd_isrlex = 0;
5780: break;
5781: case 2:
5782: nd_dcomp = 0;
5783: nd_isrlex = 0;
5784: ndl_compare_function = ndl_lex_compare;
5785: break;
5786: case 11:
5787: /* XXX */
5788: nd_dcomp = 0;
5789: nd_isrlex = 1;
5790: ndl_compare_function = ndl_ww_lex_compare;
5791: break;
5792: default:
5793: error("nd_gr : unsupported order");
5794: }
5795: break;
5796: case 1:
5797: /* block order */
5798: /* XXX */
5799: nd_dcomp = -1;
5800: nd_isrlex = 0;
5801: ndl_compare_function = ndl_block_compare;
5802: break;
5803: case 2:
5804: /* matrix order */
5805: /* XXX */
5806: nd_dcomp = -1;
5807: nd_isrlex = 0;
5808: nd_matrix_len = ord->ord.matrix.row;
5809: nd_matrix = ord->ord.matrix.matrix;
5810: ndl_compare_function = ndl_matrix_compare;
5811: break;
5812: case 3:
5813: /* composite order */
5814: nd_dcomp = -1;
5815: nd_isrlex = 0;
5816: nd_worb_len = ord->ord.composite.length;
5817: nd_worb = ord->ord.composite.w_or_b;
5818: ndl_compare_function = ndl_composite_compare;
5819: break;
5820:
5821: /* module order */
5822: case 256:
5823: switch ( ord->ord.simple ) {
5824: case 0:
1.21 noro 5825: nd_dcomp = 0;
1.1 noro 5826: nd_isrlex = 1;
1.21 noro 5827: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 5828: break;
5829: case 1:
1.21 noro 5830: nd_dcomp = 0;
1.1 noro 5831: nd_isrlex = 0;
5832: ndl_compare_function = ndl_module_glex_compare;
5833: break;
5834: case 2:
1.21 noro 5835: nd_dcomp = 0;
1.1 noro 5836: nd_isrlex = 0;
1.21 noro 5837: ndl_compare_function = ndl_module_compare;
5838: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 5839: break;
5840: default:
1.21 noro 5841: error("nd_init_ord : unsupported order");
1.1 noro 5842: }
5843: break;
5844: case 257:
5845: /* block order */
5846: nd_isrlex = 0;
1.21 noro 5847: ndl_compare_function = ndl_module_compare;
5848: ndl_base_compare_function = ndl_block_compare;
1.1 noro 5849: break;
5850: case 258:
5851: /* matrix order */
5852: nd_isrlex = 0;
5853: nd_matrix_len = ord->ord.matrix.row;
5854: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 5855: ndl_compare_function = ndl_module_compare;
5856: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 5857: break;
5858: case 259:
5859: /* composite order */
5860: nd_isrlex = 0;
5861: nd_worb_len = ord->ord.composite.length;
5862: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 5863: ndl_compare_function = ndl_module_compare;
5864: ndl_base_compare_function = ndl_composite_compare;
5865: break;
5866: case 300:
5867: /* schreyer order */
5868: if ( ord->base->id != 256 )
5869: error("nd_init_ord : unsupported base order");
5870: ndl_compare_function = ndl_module_schreyer_compare;
5871: dmm_stack = ord->dmmstack;
5872: switch ( ord->base->ord.simple ) {
5873: case 0:
5874: nd_isrlex = 1;
5875: ndl_base_compare_function = ndl_glex_compare;
5876: dl_base_compare_function = cmpdl_revgradlex;
5877: break;
5878: case 1:
5879: nd_isrlex = 0;
5880: ndl_base_compare_function = ndl_glex_compare;
5881: dl_base_compare_function = cmpdl_gradlex;
5882: break;
5883: case 2:
5884: nd_isrlex = 0;
5885: ndl_base_compare_function = ndl_lex_compare;
5886: dl_base_compare_function = cmpdl_lex;
5887: break;
5888: default:
5889: error("nd_init_ord : unsupported order");
5890: }
1.1 noro 5891: break;
5892: }
5893: nd_ord = ord;
5894: }
5895:
5896: BlockMask nd_create_blockmask(struct order_spec *ord)
5897: {
5898: int n,i,j,s,l;
5899: UINT *t;
5900: BlockMask bm;
5901:
5902: /* we only create mask table for block order */
5903: if ( ord->id != 1 && ord->id != 257 )
5904: return 0;
5905: n = ord->ord.block.length;
5906: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5907: bm->n = n;
5908: bm->order_pair = ord->ord.block.order_pair;
5909: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5910: for ( i = 0, s = 0; i < n; i++ ) {
5911: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5912: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5913: l = bm->order_pair[i].length;
5914: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5915: }
5916: return bm;
5917: }
5918:
5919: EPOS nd_create_epos(struct order_spec *ord)
5920: {
5921: int i,j,l,s,ord_l,ord_o;
5922: EPOS epos;
5923: struct order_pair *op;
5924:
5925: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5926: switch ( ord->id ) {
1.21 noro 5927: case 0: case 256: case 300:
1.1 noro 5928: if ( nd_isrlex ) {
5929: for ( i = 0; i < nd_nvar; i++ ) {
5930: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5931: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5932: }
5933: } else {
5934: for ( i = 0; i < nd_nvar; i++ ) {
5935: epos[i].i = nd_exporigin + i/nd_epw;
5936: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5937: }
5938: }
5939: break;
5940: case 1: case 257:
5941: /* block order */
5942: l = ord->ord.block.length;
5943: op = ord->ord.block.order_pair;
5944: for ( j = 0, s = 0; j < l; j++ ) {
5945: ord_o = op[j].order;
5946: ord_l = op[j].length;
5947: if ( !ord_o )
5948: for ( i = 0; i < ord_l; i++ ) {
5949: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5950: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5951: }
5952: else
5953: for ( i = 0; i < ord_l; i++ ) {
5954: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5955: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5956: }
5957: s += ord_l;
5958: }
5959: break;
5960: case 2:
5961: /* matrix order */
5962: case 3:
5963: /* composite order */
5964: default:
5965: for ( i = 0; i < nd_nvar; i++ ) {
5966: epos[i].i = nd_exporigin + i/nd_epw;
5967: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5968: }
5969: break;
5970: }
5971: return epos;
5972: }
5973:
5974: /* external interface */
5975:
5976: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5977: {
5978: NODE t,in0,in;
5979: ND ndf,nf;
5980: NDV ndvf;
5981: VL vv,tv;
5982: int stat,nvar,max,mrank;
5983: union oNDC dn;
5984: Q cont;
5985: P pp;
5986: LIST ppl;
5987:
5988: if ( !f ) {
5989: *rp = 0;
5990: return;
5991: }
5992: pltovl(v,&vv);
5993: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5994:
5995: /* max=65536 implies nd_bpe=32 */
5996: max = 65536;
5997:
5998: nd_module = 0;
5999: /* nd_module will be set if ord is a module ordering */
6000: nd_init_ord(ord);
6001: nd_setup_parameters(nvar,max);
6002: if ( nd_module && OID(f) != O_LIST )
6003: error("nd_nf_p : the first argument must be a list");
6004: if ( nd_module ) mrank = length(BDY((LIST)f));
6005: /* conversion to ndv */
6006: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
6007: NEXTNODE(in0,in);
6008: if ( nd_module ) {
6009: if ( !BDY(t) || OID(BDY(t)) != O_LIST
6010: || length(BDY((LIST)BDY(t))) != mrank )
6011: error("nd_nf_p : inconsistent basis element");
6012: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
6013: else ppl = (LIST)BDY(t);
6014: BDY(in) = (pointer)pltondv(CO,vv,ppl);
6015: } else {
6016: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
6017: else pp = (P)BDY(t);
6018: BDY(in) = (pointer)ptondv(CO,vv,pp);
6019: }
6020: if ( m ) ndv_mod(m,(NDV)BDY(in));
6021: }
6022: if ( in0 ) NEXT(in) = 0;
6023:
6024: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
6025: else ndvf = ptondv(CO,vv,(P)f);
6026: if ( m ) ndv_mod(m,ndvf);
6027: ndf = (pointer)ndvtond(m,ndvf);
6028:
6029: /* dont sort, dont removecont */
6030: ndv_setup(m,0,in0,1,1);
6031: nd_scale=2;
1.6 noro 6032: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 6033: if ( !stat )
6034: error("nd_nf_p : exponent too large");
6035: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
6036: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
6037: }
6038:
6039: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
6040: {
6041: NM m;
6042: UINT *t,*s;
6043: int i;
6044:
6045: for ( i = 0; i < n; i++ ) r[i] = 0;
6046: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6047: t = DL(m);
6048: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6049: r[i] = CM(m);
6050: }
6051: for ( i = 0; !r[i]; i++ );
6052: return i;
6053: }
6054:
6055: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
6056: {
6057: NM m;
6058: UINT *t,*s;
6059: int i;
6060:
6061: for ( i = 0; i < n; i++ ) r[i] = 0;
6062: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6063: t = DL(m);
6064: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6065: r[i] = CZ(m);
1.1 noro 6066: }
6067: for ( i = 0; !r[i]; i++ );
6068: return i;
6069: }
6070:
6071: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
6072: {
6073: NM m;
6074: UINT *t,*s;
6075: int i;
6076:
6077: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
6078: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6079: t = DL(m);
6080: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6081: mpz_set(r[i],BDY(CZ(m)));
6082: }
6083: for ( i = 0; !mpz_sgn(r[i]); i++ );
6084: return i;
6085: }
6086:
6087: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
6088: {
6089: NM m;
6090: unsigned long *v;
6091: int i,j,h,size;
6092: UINT *s,*t;
6093:
6094: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
6095: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
6096: bzero(v,size);
6097: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
6098: t = DL(m);
6099: h = ndl_hash_value(t);
6100: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6101: v[i/BLEN] |= 1L <<(i%BLEN);
6102: }
6103: return v;
6104: }
6105:
6106: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
6107: {
6108: NMV mr;
6109: UINT *d,*t,*s;
6110: int i,j,len,h,head;
6111:
6112: d = DL(m);
6113: len = LEN(p);
6114: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6115: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6116: ndl_add(d,DL(mr),t);
6117: h = ndl_hash_value(t);
6118: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6119: if ( j == 0 ) head = i;
6120: v[i/BLEN] |= 1L <<(i%BLEN);
6121: }
6122: return head;
6123: }
6124:
6125: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
6126: {
6127: NM m;
6128: NMV mr;
6129: UINT *d,*t,*s;
6130: NDV p;
6131: int i,j,len;
6132: Z *r;
6133:
6134: m = pair->mul;
6135: d = DL(m);
6136: p = nd_ps[pair->index];
6137: len = LEN(p);
6138: r = (Z *)CALLOC(n,sizeof(Q));
6139: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6140: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6141: ndl_add(d,DL(mr),t);
6142: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6143: r[i] = CZ(mr);
1.1 noro 6144: }
6145: return r;
6146: }
6147:
1.11 noro 6148: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 6149: {
6150: NM m;
6151: NMV mr;
1.11 noro 6152: UINT *d,*t,*s,*u;
1.1 noro 6153: NDV p;
6154: unsigned char *ivc;
6155: unsigned short *ivs;
6156: UINT *v,*ivi,*s0v;
1.11 noro 6157: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 6158: IndArray r;
6159:
6160: m = pair->mul;
6161: d = DL(m);
6162: if ( trace )
6163: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
6164: else
6165: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
6166:
6167: len = LEN(p);
6168: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6169: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 6170: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
6171: ndl_add(d,DL(mr),t);
6172: st = prev;
6173: ed = n;
6174: while ( ed > st ) {
6175: md = (st+ed)/2;
6176: u = s0+md*nd_wpd;
6177: c = DL_COMPARE(u,t);
6178: if ( c == 0 ) break;
6179: else if ( c > 0 ) st = md;
6180: else ed = md;
6181: }
6182: prev = v[j] = md;
1.1 noro 6183: }
6184: r = (IndArray)MALLOC(sizeof(struct oIndArray));
6185: r->head = v[0];
6186: diff = 0;
6187: for ( i = 1; i < len; i++ ) {
6188: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
6189: }
6190: if ( diff < 256 ) {
6191: r->width = 1;
6192: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
6193: r->index.c = ivc;
6194: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
6195: } else if ( diff < 65536 ) {
6196: r->width = 2;
6197: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
6198: r->index.s = ivs;
6199: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
6200: } else {
6201: r->width = 4;
6202: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
6203: r->index.i = ivi;
6204: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
6205: }
6206: return r;
6207: }
6208:
6209: int compress_array(Z *svect,Z *cvect,int n)
6210: {
6211: int i,j;
6212:
6213: for ( i = j = 0; i < n; i++ )
6214: if ( svect[i] ) cvect[j++] = svect[i];
6215: return j;
6216: }
6217:
6218: void expand_array(Z *svect,Z *cvect,int n)
6219: {
6220: int i,j;
6221:
6222: for ( i = j = 0; j < n; i++ )
6223: if ( svect[i] ) svect[i] = cvect[j++];
6224: }
6225:
1.8 noro 6226: #if 0
1.1 noro 6227: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6228: {
6229: int i,j,k,len,pos,prev,nz;
6230: Z cs,mcs,c1,c2,cr,gcd,t;
6231: IndArray ivect;
6232: unsigned char *ivc;
6233: unsigned short *ivs;
6234: unsigned int *ivi;
6235: NDV redv;
6236: NMV mr;
6237: NODE rp;
6238: int maxrs;
6239: double hmag;
6240: Z *cvect;
1.3 noro 6241: int l;
1.1 noro 6242:
6243: maxrs = 0;
6244: for ( i = 0; i < col && !svect[i]; i++ );
6245: if ( i == col ) return maxrs;
6246: hmag = p_mag((P)svect[i])*nd_scale;
6247: cvect = (Z *)MALLOC(col*sizeof(Q));
6248: for ( i = 0; i < nred; i++ ) {
6249: ivect = imat[i];
6250: k = ivect->head;
6251: if ( svect[k] ) {
6252: maxrs = MAX(maxrs,rp0[i]->sugar);
6253: redv = nd_demand?ndv_load(rp0[i]->index)
6254: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6255: len = LEN(redv); mr = BDY(redv);
1.6 noro 6256: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6257: chsgnz(cs,&mcs);
6258: if ( !UNIQ(cr) ) {
6259: for ( j = 0; j < col; j++ ) {
6260: mulz(svect[j],cr,&c1); svect[j] = c1;
6261: }
6262: }
6263: svect[k] = 0; prev = k;
6264: switch ( ivect->width ) {
6265: case 1:
6266: ivc = ivect->index.c;
6267: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6268: pos = prev+ivc[j]; prev = pos;
1.6 noro 6269: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6270: }
6271: break;
6272: case 2:
6273: ivs = ivect->index.s;
6274: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6275: pos = prev+ivs[j]; prev = pos;
1.6 noro 6276: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6277: }
6278: break;
6279: case 4:
6280: ivi = ivect->index.i;
6281: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6282: pos = prev+ivi[j]; prev = pos;
1.6 noro 6283: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6284: }
6285: break;
6286: }
6287: for ( j = k+1; j < col && !svect[j]; j++ );
6288: if ( j == col ) break;
6289: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6290: nz = compress_array(svect,cvect,col);
6291: removecont_array((P *)cvect,nz,1);
6292: expand_array(svect,cvect,nz);
6293: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6294: }
6295: }
6296: }
6297: nz = compress_array(svect,cvect,col);
6298: removecont_array((P *)cvect,nz,1);
6299: expand_array(svect,cvect,nz);
6300: if ( DP_Print ) {
6301: fprintf(asir_out,"-"); fflush(asir_out);
6302: }
6303: return maxrs;
6304: }
1.4 noro 6305: #else
1.9 noro 6306:
1.4 noro 6307: /* direct mpz version */
6308: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6309: {
6310: int i,j,k,len,pos,prev;
6311: mpz_t cs,cr,gcd;
6312: IndArray ivect;
6313: unsigned char *ivc;
6314: unsigned short *ivs;
6315: unsigned int *ivi;
6316: NDV redv;
6317: NMV mr;
6318: NODE rp;
6319: int maxrs;
6320: double hmag;
6321: int l;
1.13 noro 6322: static mpz_t *svect;
6323: static int svect_len=0;
1.4 noro 6324:
6325: maxrs = 0;
6326: for ( i = 0; i < col && !svect0[i]; i++ );
6327: if ( i == col ) return maxrs;
6328: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 6329: if ( col > svect_len ) {
6330: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
6331: svect_len = col;
6332: }
1.4 noro 6333: for ( i = 0; i < col; i++ ) {
6334: mpz_init(svect[i]);
6335: if ( svect0[i] )
6336: mpz_set(svect[i],BDY(svect0[i]));
6337: else
6338: mpz_set_ui(svect[i],0);
6339: }
6340: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6341: for ( i = 0; i < nred; i++ ) {
6342: ivect = imat[i];
6343: k = ivect->head;
6344: if ( mpz_sgn(svect[k]) ) {
6345: maxrs = MAX(maxrs,rp0[i]->sugar);
6346: redv = nd_demand?ndv_load(rp0[i]->index)
6347: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6348: len = LEN(redv); mr = BDY(redv);
1.6 noro 6349: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6350: mpz_div(cs,svect[k],gcd);
1.6 noro 6351: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6352: mpz_neg(cs,cs);
1.9 noro 6353: if ( MUNIMPZ(cr) )
6354: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6355: else if ( !UNIMPZ(cr) )
6356: for ( j = 0; j < col; j++ ) {
6357: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6358: }
1.4 noro 6359: mpz_set_ui(svect[k],0);
6360: prev = k;
6361: switch ( ivect->width ) {
6362: case 1:
6363: ivc = ivect->index.c;
6364: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6365: pos = prev+ivc[j]; prev = pos;
1.6 noro 6366: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6367: }
6368: break;
6369: case 2:
6370: ivs = ivect->index.s;
6371: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6372: pos = prev+ivs[j]; prev = pos;
1.6 noro 6373: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6374: }
6375: break;
6376: case 4:
6377: ivi = ivect->index.i;
6378: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6379: pos = prev+ivi[j]; prev = pos;
1.6 noro 6380: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6381: }
6382: break;
6383: }
6384: for ( j = k+1; j < col && !svect[j]; j++ );
6385: if ( j == col ) break;
6386: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6387: mpz_removecont_array(svect,col);
6388: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6389: }
6390: }
6391: }
6392: mpz_removecont_array(svect,col);
6393: if ( DP_Print ) {
6394: fprintf(asir_out,"-"); fflush(asir_out);
6395: }
6396: for ( i = 0; i < col; i++ )
6397: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6398: else svect0[i] = 0;
6399: return maxrs;
6400: }
6401: #endif
1.1 noro 6402:
6403: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6404: {
6405: int i,j,k,len,pos,prev;
6406: UINT c,c1,c2,c3,up,lo,dmy;
6407: IndArray ivect;
6408: unsigned char *ivc;
6409: unsigned short *ivs;
6410: unsigned int *ivi;
6411: NDV redv;
6412: NMV mr;
6413: NODE rp;
6414: int maxrs;
6415:
6416: maxrs = 0;
6417: for ( i = 0; i < nred; i++ ) {
6418: ivect = imat[i];
6419: k = ivect->head; svect[k] %= m;
6420: if ( (c = svect[k]) != 0 ) {
6421: maxrs = MAX(maxrs,rp0[i]->sugar);
6422: c = m-c; redv = nd_ps[rp0[i]->index];
6423: len = LEN(redv); mr = BDY(redv);
6424: svect[k] = 0; prev = k;
6425: switch ( ivect->width ) {
6426: case 1:
6427: ivc = ivect->index.c;
6428: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6429: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6430: if ( c1 ) {
6431: c2 = svect[pos];
6432: DMA(c1,c,c2,up,lo);
6433: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6434: } else svect[pos] = lo;
6435: }
6436: }
6437: break;
6438: case 2:
6439: ivs = ivect->index.s;
6440: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6441: pos = prev+ivs[j]; c1 = CM(mr);
6442: prev = pos;
6443: if ( c1 ) {
6444: c2 = svect[pos];
6445: DMA(c1,c,c2,up,lo);
6446: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6447: } else svect[pos] = lo;
6448: }
6449: }
6450: break;
6451: case 4:
6452: ivi = ivect->index.i;
6453: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6454: pos = prev+ivi[j]; c1 = CM(mr);
6455: prev = pos;
6456: if ( c1 ) {
6457: c2 = svect[pos];
6458: DMA(c1,c,c2,up,lo);
6459: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6460: } else svect[pos] = lo;
6461: }
6462: }
6463: break;
6464: }
6465: }
6466: }
6467: for ( i = 0; i < col; i++ )
6468: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6469: return maxrs;
6470: }
6471:
6472: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6473: {
6474: int i,j,k,len,pos,prev;
6475: UINT c,c1,c2,c3,up,lo,dmy;
6476: IndArray ivect;
6477: unsigned char *ivc;
6478: unsigned short *ivs;
6479: unsigned int *ivi;
6480: NDV redv;
6481: NMV mr;
6482: NODE rp;
6483: int maxrs;
6484:
6485: maxrs = 0;
6486: for ( i = 0; i < nred; i++ ) {
6487: ivect = imat[i];
6488: k = ivect->head;
6489: if ( (c = svect[k]) != 0 ) {
6490: maxrs = MAX(maxrs,rp0[i]->sugar);
6491: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6492: len = LEN(redv); mr = BDY(redv);
6493: svect[k] = 0; prev = k;
6494: switch ( ivect->width ) {
6495: case 1:
6496: ivc = ivect->index.c;
6497: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6498: pos = prev+ivc[j]; prev = pos;
6499: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6500: }
6501: break;
6502: case 2:
6503: ivs = ivect->index.s;
6504: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6505: pos = prev+ivs[j]; prev = pos;
6506: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6507: }
6508: break;
6509: case 4:
6510: ivi = ivect->index.i;
6511: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6512: pos = prev+ivi[j]; prev = pos;
6513: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6514: }
6515: break;
6516: }
6517: }
6518: }
6519: return maxrs;
6520: }
6521:
6522: ND nd_add_lf(ND p1,ND p2)
6523: {
6524: int n,c,can;
6525: ND r;
6526: NM m1,m2,mr0,mr,s;
6527: Z t;
6528:
6529: if ( !p1 ) return p2;
6530: else if ( !p2 ) return p1;
6531: else {
6532: can = 0;
6533: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6534: c = DL_COMPARE(DL(m1),DL(m2));
6535: switch ( c ) {
6536: case 0:
6537: addlf(CZ(m1),CZ(m2),&t);
6538: s = m1; m1 = NEXT(m1);
6539: if ( t ) {
6540: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6541: } else {
6542: can += 2; FREENM(s);
6543: }
6544: s = m2; m2 = NEXT(m2); FREENM(s);
6545: break;
6546: case 1:
6547: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6548: break;
6549: case -1:
6550: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6551: break;
6552: }
6553: }
6554: if ( !mr0 )
6555: if ( m1 ) mr0 = m1;
6556: else if ( m2 ) mr0 = m2;
6557: else return 0;
6558: else if ( m1 ) NEXT(mr) = m1;
6559: else if ( m2 ) NEXT(mr) = m2;
6560: else NEXT(mr) = 0;
6561: BDY(p1) = mr0;
6562: SG(p1) = MAX(SG(p1),SG(p2));
6563: LEN(p1) = LEN(p1)+LEN(p2)-can;
6564: FREEND(p2);
6565: return p1;
6566: }
6567: }
6568:
6569: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6570: {
6571: int i,j,k,len,pos,prev;
6572: mpz_t c,mc,c1;
6573: IndArray ivect;
6574: unsigned char *ivc;
6575: unsigned short *ivs;
6576: unsigned int *ivi;
6577: NDV redv;
6578: NMV mr;
6579: NODE rp;
6580: int maxrs;
6581:
6582: maxrs = 0;
6583: lf_lazy = 1;
6584: for ( i = 0; i < nred; i++ ) {
6585: ivect = imat[i];
6586: k = ivect->head;
6587: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6588: if ( mpz_sgn(svect[k]) ) {
6589: maxrs = MAX(maxrs,rp0[i]->sugar);
6590: mpz_neg(svect[k],svect[k]);
6591: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6592: len = LEN(redv); mr = BDY(redv);
6593: prev = k;
6594: switch ( ivect->width ) {
6595: case 1:
6596: ivc = ivect->index.c;
6597: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6598: pos = prev+ivc[j]; prev = pos;
6599: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6600: }
6601: break;
6602: case 2:
6603: ivs = ivect->index.s;
6604: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6605: pos = prev+ivs[j]; prev = pos;
6606: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6607: }
6608: break;
6609: case 4:
6610: ivi = ivect->index.i;
6611: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6612: pos = prev+ivi[j]; prev = pos;
6613: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6614: }
6615: break;
6616: }
6617: mpz_set_ui(svect[k],0);
6618: }
6619: }
6620: lf_lazy=0;
6621: for ( i = 0; i < col; i++ ) {
6622: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6623: }
6624: return maxrs;
6625: }
6626:
6627: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6628: {
6629: int i,j,k,l,rank,s;
6630: mpz_t a,a1,inv;
6631: mpz_t *t,*pivot,*pk;
6632: mpz_t **mat;
6633: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6634: int size,size1;
6635:
6636: mpz_init(inv);
6637: mpz_init(a);
6638: mat = (mpz_t **)mat0;
6639: size = 0;
6640: for ( rank = 0, j = 0; j < col; j++ ) {
6641: for ( i = rank; i < row; i++ ) {
6642: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6643: }
6644: for ( i = rank; i < row; i++ )
6645: if ( mpz_sgn(mat[i][j]) )
6646: break;
6647: if ( i == row ) {
6648: colstat[j] = 0;
6649: continue;
6650: } else
6651: colstat[j] = 1;
6652: if ( i != rank ) {
6653: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6654: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6655: }
6656: pivot = mat[rank];
6657: s = sugar[rank];
6658: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6659: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6660: if ( mpz_sgn(*pk) ) {
6661: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6662: }
6663: for ( i = rank+1; i < row; i++ ) {
6664: t = mat[i];
6665: if ( mpz_sgn(t[j]) ) {
6666: sugar[i] = MAX(sugar[i],s);
6667: mpz_neg(a,t[j]);
6668: red_by_vect_lf(t+j,pivot+j,a,col-j);
6669: }
6670: }
6671: rank++;
6672: }
6673: for ( j = col-1, l = rank-1; j >= 0; j-- )
6674: if ( colstat[j] ) {
6675: pivot = mat[l];
6676: s = sugar[l];
6677: for ( k = j; k < col; k++ )
6678: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6679: for ( i = 0; i < l; i++ ) {
6680: t = mat[i];
6681: if ( mpz_sgn(t[j]) ) {
6682: sugar[i] = MAX(sugar[i],s);
6683: mpz_neg(a,t[j]);
6684: red_by_vect_lf(t+j,pivot+j,a,col-j);
6685: }
6686: }
6687: l--;
6688: }
6689: for ( j = 0, l = 0; l < rank; j++ )
6690: if ( colstat[j] ) {
6691: t = mat[l];
6692: for ( k = j; k < col; k++ ) {
6693: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6694: }
6695: l++;
6696: }
6697: return rank;
6698: }
6699:
6700:
6701: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6702: {
6703: int j,k,len;
6704: UINT *p;
6705: UINT c;
6706: NDV r;
6707: NMV mr0,mr;
6708:
6709: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6710: if ( !len ) return 0;
6711: else {
6712: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6713: #if 0
6714: ndv_alloc += nmv_adv*len;
6715: #endif
6716: mr = mr0;
6717: p = s0vect;
6718: for ( j = k = 0; j < col; j++, p += nd_wpd )
6719: if ( !rhead[j] ) {
6720: if ( (c = vect[k++]) != 0 ) {
6721: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6722: }
6723: }
6724: MKNDV(nd_nvar,mr0,len,r);
6725: return r;
6726: }
6727: }
6728:
6729: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6730: {
6731: int j,k,len;
6732: UINT *p;
6733: NDV r;
6734: NMV mr0,mr;
6735:
6736: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6737: if ( !len ) return 0;
6738: else {
6739: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6740: mr = mr0;
6741: p = s0vect;
6742: for ( j = 0; j < col; j++, p += nd_wpd )
6743: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6744: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6745: }
6746: MKNDV(nd_nvar,mr0,len,r);
6747: return r;
6748: }
6749: }
6750:
6751: /* for preprocessed vector */
6752:
6753: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6754: {
1.6 noro 6755: int j,k,len;
6756: UINT *p;
6757: Z c;
6758: NDV r;
6759: NMV mr0,mr;
1.1 noro 6760:
1.6 noro 6761: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6762: if ( !len ) return 0;
6763: else {
6764: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6765: #if 0
1.6 noro 6766: ndv_alloc += nmv_adv*len;
1.1 noro 6767: #endif
1.6 noro 6768: mr = mr0;
6769: p = s0vect;
6770: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6771: if ( !rhead[j] ) {
6772: if ( (c = vect[k++]) != 0 ) {
6773: if ( !INT(c) )
6774: error("vect_to_ndv_q : components must be integers");
6775: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6776: }
6777: }
1.1 noro 6778: }
1.6 noro 6779: MKNDV(nd_nvar,mr0,len,r);
6780: return r;
6781: }
1.1 noro 6782: }
6783:
6784: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6785: {
6786: int j,k,len;
6787: UINT *p;
6788: mpz_t c;
6789: NDV r;
6790: NMV mr0,mr;
6791:
6792: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6793: if ( !len ) return 0;
6794: else {
6795: mr0 = (NMV)MALLOC(nmv_adv*len);
6796: #if 0
6797: ndv_alloc += nmv_adv*len;
6798: #endif
6799: mr = mr0;
6800: p = s0vect;
6801: for ( j = k = 0; j < col; j++, p += nd_wpd )
6802: if ( !rhead[j] ) {
6803: c[0] = vect[k++][0];
6804: if ( mpz_sgn(c) ) {
6805: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6806: }
6807: }
6808: MKNDV(nd_nvar,mr0,len,r);
6809: return r;
6810: }
6811: }
6812:
6813: /* for plain vector */
6814:
6815: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6816: {
6817: int j,k,len;
6818: UINT *p;
6819: Z c;
6820: NDV r;
6821: NMV mr0,mr;
6822:
6823: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6824: if ( !len ) return 0;
6825: else {
6826: mr0 = (NMV)MALLOC(nmv_adv*len);
6827: #if 0
6828: ndv_alloc += nmv_adv*len;
6829: #endif
6830: mr = mr0;
6831: p = s0vect;
6832: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6833: if ( (c = vect[k]) != 0 ) {
6834: if ( !INT(c) )
1.6 noro 6835: error("plain_vect_to_ndv_q : components must be integers");
6836: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6837: }
6838: MKNDV(nd_nvar,mr0,len,r);
6839: return r;
6840: }
6841: }
6842:
6843: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6844: {
6845: ND_pairs t;
6846: NODE sp0,sp;
6847: int stat;
6848: ND spol;
6849:
6850: for ( t = l; t; t = NEXT(t) ) {
6851: stat = nd_sp(m,trace,t,&spol);
6852: if ( !stat ) return 0;
6853: if ( spol ) {
6854: add_pbucket_symbolic(bucket,spol);
6855: }
6856: }
6857: return 1;
6858: }
6859:
6860: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6861: {
6862: NODE rp0,rp;
6863: NM mul,head,s0,s;
6864: int index,col,i,sugar;
6865: RHist h;
6866: UINT *s0v,*p;
6867: NM_ind_pair pair;
6868: ND red;
6869: NDV *ps;
6870:
6871: s0 = 0; rp0 = 0; col = 0;
6872: if ( nd_demand )
6873: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6874: else
6875: ps = trace?nd_ps_trace:nd_ps;
6876: while ( 1 ) {
6877: head = remove_head_pbucket_symbolic(bucket);
6878: if ( !head ) break;
6879: if ( !s0 ) s0 = head;
6880: else NEXT(s) = head;
6881: s = head;
6882: index = ndl_find_reducer(DL(head));
6883: if ( index >= 0 ) {
6884: h = nd_psh[index];
6885: NEWNM(mul);
6886: ndl_sub(DL(head),DL(h),DL(mul));
6887: if ( ndl_check_bound2(index,DL(mul)) )
6888: return 0;
6889: sugar = TD(DL(mul))+SG(ps[index]);
6890: MKNM_ind_pair(pair,mul,index,sugar);
6891: red = ndv_mul_nm_symbolic(mul,ps[index]);
6892: add_pbucket_symbolic(bucket,nd_remove_head(red));
6893: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6894: }
6895: col++;
6896: }
6897: if ( rp0 ) NEXT(rp) = 0;
6898: NEXT(s) = 0;
6899: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6900: for ( i = 0, p = s0v, s = s0; i < col;
6901: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6902: *s0vect = s0v;
6903: *r = rp0;
6904: return col;
6905: }
6906:
6907: void print_ndp(ND_pairs l)
6908: {
6909: ND_pairs t;
6910:
6911: for ( t = l; t; t = NEXT(t) )
6912: printf("[%d,%d] ",t->i1,t->i2);
6913: printf("\n");
6914: }
6915:
6916: NODE nd_f4(int m,int checkonly,int **indp)
6917: {
6918: int i,nh,stat,index,f4red;
6919: NODE r,g,tn0,tn,node;
6920: ND_pairs d,l,t,ll0,ll,lh;
6921: LIST l0,l1;
6922: ND spol,red;
6923: NDV nf,redv;
6924: NM s0,s;
6925: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6926: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6927: UINT c;
6928: UINT **spmat;
6929: UINT *s0vect,*svect,*p,*v;
6930: int *colstat;
6931: IndArray *imat;
6932: int *rhead;
6933: int spcol,sprow;
6934: int sugar,sugarh;
6935: PGeoBucket bucket;
6936: struct oEGT eg0,eg1,eg_f4;
6937: Z i1,i2,sugarq;
1.12 noro 6938:
6939: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6940: #if 0
6941: ndv_alloc = 0;
6942: #endif
1.11 noro 6943: Nf4_red=0;
1.1 noro 6944: g = 0; d = 0;
6945: for ( i = 0; i < nd_psn; i++ ) {
6946: d = update_pairs(d,g,i,0);
6947: g = update_base(g,i);
6948: }
6949: nzlist = 0;
6950: nzlist_t = nd_nzlist;
6951: f4red = 1;
6952: nd_last_nonzero = 0;
6953: while ( d ) {
6954: get_eg(&eg0);
6955: l = nd_minsugarp(d,&d);
6956: sugar = nd_sugarweight?l->sugar2:SG(l);
6957: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6958: if ( nzlist_t ) {
6959: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6960: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6961: tn = BDY((LIST)ARG1(node));
6962: if ( !tn ) {
6963: nzlist_t = NEXT(nzlist_t);
6964: continue;
6965: }
6966: /* tn = [[i1,i2],...] */
6967: lh = nd_ipairtospair(tn);
6968: }
6969: bucket = create_pbucket();
6970: stat = nd_sp_f4(m,0,l,bucket);
6971: if ( !stat ) {
6972: for ( t = l; NEXT(t); t = NEXT(t) );
6973: NEXT(t) = d; d = l;
6974: d = nd_reconstruct(0,d);
6975: continue;
6976: }
6977: if ( bucket->m < 0 ) continue;
6978: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6979: if ( !col ) {
6980: for ( t = l; NEXT(t); t = NEXT(t) );
6981: NEXT(t) = d; d = l;
6982: d = nd_reconstruct(0,d);
6983: continue;
6984: }
1.12 noro 6985: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6986: if ( DP_Print )
1.6 noro 6987: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6988: sugar,eg_f4.exectime);
1.1 noro 6989: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6990: if ( checkonly && nflist ) return 0;
6991: /* adding new bases */
6992: if ( nflist ) nd_last_nonzero = f4red;
6993: for ( r = nflist; r; r = NEXT(r) ) {
6994: nf = (NDV)BDY(r);
6995: ndv_removecont(m,nf);
6996: if ( !m && nd_nalg ) {
6997: ND nf1;
6998:
6999: nf1 = ndvtond(m,nf);
7000: nd_monic(0,&nf1);
7001: nd_removecont(m,nf1);
7002: nf = ndtondv(m,nf1);
7003: }
7004: nh = ndv_newps(m,nf,0,1);
7005: d = update_pairs(d,g,nh,0);
7006: g = update_base(g,nh);
7007: }
7008: if ( DP_Print ) {
7009: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
7010: }
7011: if ( nd_gentrace ) {
7012: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
7013: NEXTNODE(tn0,tn);
1.6 noro 7014: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 7015: node = mknode(2,i1,i2); MKLIST(l0,node);
7016: BDY(tn) = l0;
7017: }
7018: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 7019: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 7020: MKNODE(node,l1,nzlist); nzlist = node;
7021: }
7022: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
7023: f4red++;
7024: if ( nd_f4red && f4red > nd_f4red ) break;
7025: if ( nd_rank0 && !nflist ) break;
7026: }
7027: if ( nd_gentrace ) {
7028: MKLIST(l0,reverse_node(nzlist));
7029: MKNODE(nd_alltracelist,l0,0);
7030: }
7031: #if 0
7032: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7033: #endif
1.12 noro 7034: if ( DP_Print ) {
7035: fprintf(asir_out,"number of red=%d,",Nf4_red);
7036: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
7037: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
7038: }
1.1 noro 7039: conv_ilist(nd_demand,0,g,indp);
7040: return g;
7041: }
7042:
7043: NODE nd_f4_trace(int m,int **indp)
7044: {
7045: int i,nh,stat,index;
7046: NODE r,g;
7047: ND_pairs d,l,l0,t;
7048: ND spol,red;
7049: NDV nf,redv,nfqv,nfv;
7050: NM s0,s;
7051: NODE rp0,srp0,nflist;
7052: int nsp,nred,col,rank,len,k,j,a;
7053: UINT c;
7054: UINT **spmat;
7055: UINT *s0vect,*svect,*p,*v;
7056: int *colstat;
7057: IndArray *imat;
7058: int *rhead;
7059: int spcol,sprow;
7060: int sugar;
7061: PGeoBucket bucket;
7062: struct oEGT eg0,eg1,eg_f4;
7063:
7064: g = 0; d = 0;
7065: for ( i = 0; i < nd_psn; i++ ) {
7066: d = update_pairs(d,g,i,0);
7067: g = update_base(g,i);
7068: }
7069: while ( d ) {
7070: get_eg(&eg0);
7071: l = nd_minsugarp(d,&d);
7072: sugar = SG(l);
7073: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7074: bucket = create_pbucket();
7075: stat = nd_sp_f4(m,0,l,bucket);
7076: if ( !stat ) {
7077: for ( t = l; NEXT(t); t = NEXT(t) );
7078: NEXT(t) = d; d = l;
7079: d = nd_reconstruct(1,d);
7080: continue;
7081: }
7082: if ( bucket->m < 0 ) continue;
7083: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7084: if ( !col ) {
7085: for ( t = l; NEXT(t); t = NEXT(t) );
7086: NEXT(t) = d; d = l;
7087: d = nd_reconstruct(1,d);
7088: continue;
7089: }
7090: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
7091: if ( DP_Print )
1.3 noro 7092: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 7093: sugar,eg_f4.exectime);
1.1 noro 7094: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
7095: if ( !l0 ) continue;
7096: l = l0;
7097:
7098: /* over Q */
7099: bucket = create_pbucket();
7100: stat = nd_sp_f4(0,1,l,bucket);
7101: if ( !stat ) {
7102: for ( t = l; NEXT(t); t = NEXT(t) );
7103: NEXT(t) = d; d = l;
7104: d = nd_reconstruct(1,d);
7105: continue;
7106: }
7107: if ( bucket->m < 0 ) continue;
7108: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7109: if ( !col ) {
7110: for ( t = l; NEXT(t); t = NEXT(t) );
7111: NEXT(t) = d; d = l;
7112: d = nd_reconstruct(1,d);
7113: continue;
7114: }
7115: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7116: /* adding new bases */
7117: for ( r = nflist; r; r = NEXT(r) ) {
7118: nfqv = (NDV)BDY(r);
7119: ndv_removecont(0,nfqv);
1.6 noro 7120: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 7121: if ( nd_nalg ) {
7122: ND nf1;
7123:
7124: nf1 = ndvtond(m,nfqv);
7125: nd_monic(0,&nf1);
7126: nd_removecont(0,nf1);
7127: nfqv = ndtondv(0,nf1); nd_free(nf1);
7128: }
7129: nfv = ndv_dup(0,nfqv);
7130: ndv_mod(m,nfv);
7131: ndv_removecont(m,nfv);
7132: nh = ndv_newps(0,nfv,nfqv,1);
7133: d = update_pairs(d,g,nh,0);
7134: g = update_base(g,nh);
7135: }
7136: }
7137: #if 0
7138: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7139: #endif
7140: conv_ilist(nd_demand,1,g,indp);
7141: return g;
7142: }
7143:
7144: int rref(matrix mat,int *sugar)
7145: {
7146: int row,col,i,j,k,l,s,wcol,wj;
7147: unsigned long bj;
7148: unsigned long **a;
7149: unsigned long *ai,*ak,*as,*t;
7150: int *pivot;
7151:
7152: row = mat->row;
7153: col = mat->col;
7154: a = mat->a;
7155: wcol = (col+BLEN-1)/BLEN;
7156: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7157: i = 0;
7158: for ( j = 0; j < col; j++ ) {
7159: wj = j/BLEN; bj = 1L<<(j%BLEN);
7160: for ( k = i; k < row; k++ )
7161: if ( a[k][wj] & bj ) break;
7162: if ( k == row ) continue;
7163: pivot[i] = j;
7164: if ( k != i ) {
7165: t = a[i]; a[i] = a[k]; a[k] = t;
7166: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7167: }
7168: ai = a[i];
7169: for ( k = i+1; k < row; k++ ) {
7170: ak = a[k];
7171: if ( ak[wj] & bj ) {
7172: for ( l = wj; l < wcol; l++ )
7173: ak[l] ^= ai[l];
7174: sugar[k] = MAX(sugar[k],sugar[i]);
7175: }
7176: }
7177: i++;
7178: }
7179: for ( k = i-1; k >= 0; k-- ) {
7180: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
7181: ak = a[k];
7182: for ( s = 0; s < k; s++ ) {
7183: as = a[s];
7184: if ( as[wj] & bj ) {
7185: for ( l = wj; l < wcol; l++ )
7186: as[l] ^= ak[l];
7187: sugar[s] = MAX(sugar[s],sugar[k]);
7188: }
7189: }
7190: }
7191: return i;
7192: }
7193:
7194: void print_matrix(matrix mat)
7195: {
7196: int row,col,i,j;
7197: unsigned long *ai;
7198:
7199: row = mat->row;
7200: col = mat->col;
7201: printf("%d x %d\n",row,col);
7202: for ( i = 0; i < row; i++ ) {
7203: ai = mat->a[i];
7204: for ( j = 0; j < col; j++ ) {
7205: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
7206: else putchar('0');
7207: }
7208: putchar('\n');
7209: }
7210: }
7211:
7212: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
7213:
7214: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
7215: {
7216: int row,col,wcol,wj,i,j;
7217: unsigned long bj;
7218: unsigned long *ai;
7219: unsigned long **a;
7220: int len;
7221: int *pos;
7222:
7223: row = mat->row;
7224: col = mat->col;
7225: wcol = (col+BLEN-1)/BLEN;
7226: pos = (int *)MALLOC(wcol*sizeof(int));
7227: bzero(pos,wcol*sizeof(int));
7228: for ( i = j = 0; i < wcol; i++ )
7229: if ( v[i] ) pos[j++] = i;;
7230: len = j;
7231: wj = rhead/BLEN;
7232: bj = 1L<<rhead%BLEN;
7233: a = mat->a;
7234: for ( i = 0; i < row; i++ ) {
7235: ai = a[i];
7236: if ( ai[wj]&bj ) {
7237: for ( j = 0; j < len; j++ )
7238: ai[pos[j]] ^= v[pos[j]];
7239: sugar[i] = MAX(sugar[i],rsugar);
7240: }
7241: }
7242: }
7243:
7244: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7245: {
7246: int nsp,nred,i,i0,k,rank,row;
7247: NODE r0,rp;
7248: ND_pairs sp;
7249: ND spol;
7250: NM_ind_pair rt;
7251: int *s0hash;
7252: UINT *s;
7253: int *pivot,*sugar,*head;
7254: matrix mat;
7255: NM m;
7256: NODE r;
7257: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7258: int rhead,rsugar,size;
7259: unsigned long *v;
7260:
7261: get_eg(&eg0);
7262: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7263: nred = length(rp0);
7264: mat = alloc_matrix(nsp,col);
7265: s0hash = (int *)MALLOC(col*sizeof(int));
7266: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7267: s0hash[i] = ndl_hash_value(s);
7268:
7269: sugar = (int *)MALLOC(nsp*sizeof(int));
7270: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7271: nd_sp(2,0,sp,&spol);
7272: if ( spol ) {
7273: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7274: sugar[i] = SG(spol);
7275: i++;
7276: }
7277: }
7278: mat->row = i;
7279: if ( DP_Print ) {
7280: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7281: }
7282: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7283: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7284: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7285: rt = (NM_ind_pair)BDY(rp);
7286: bzero(v,size);
7287: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7288: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7289: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7290: }
7291:
7292: get_eg(&eg1);
7293: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7294: rank = rref(mat,sugar);
7295:
7296: for ( i = 0, r0 = 0; i < rank; i++ ) {
7297: NEXTNODE(r0,r);
7298: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7299: SG((NDV)BDY(r)) = sugar[i];
7300: }
7301: if ( r0 ) NEXT(r) = 0;
7302: get_eg(&eg2);
7303: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7304: if ( DP_Print ) {
7305: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7306: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7307: fflush(asir_out);
7308: }
7309: return r0;
7310: }
7311:
7312:
7313: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7314: {
7315: IndArray *imat;
1.11 noro 7316: int nsp,nred,i,start;
1.1 noro 7317: int *rhead;
7318: NODE r0,rp;
7319: ND_pairs sp;
7320: NM_ind_pair *rvect;
7321: UINT *s;
7322: int *s0hash;
1.11 noro 7323: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7324:
7325: if ( m == 2 && nd_rref2 )
7326: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7327:
7328: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7329: nred = length(rp0);
7330: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7331: rhead = (int *)MALLOC(col*sizeof(int));
7332: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7333:
7334: /* construction of index arrays */
1.11 noro 7335: get_eg(&eg0);
1.1 noro 7336: if ( DP_Print ) {
1.11 noro 7337: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7338: fflush(asir_out);
1.1 noro 7339: }
7340: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7341: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7342: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7343: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7344: rhead[imat[i]->head] = 1;
1.11 noro 7345: start = imat[i]->head;
7346: }
1.12 noro 7347: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7348: if ( DP_Print ) {
7349: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7350: fflush(asir_out);
1.1 noro 7351: }
7352: if ( m > 0 )
1.7 noro 7353: #if SIZEOF_LONG==8
1.1 noro 7354: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7355: #else
7356: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7357: #endif
7358: else if ( m == -1 )
7359: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7360: else if ( m == -2 )
7361: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7362: else
7363: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7364: return r0;
7365: }
7366:
7367: /* for Fp, 2<=p<2^16 */
7368:
7369: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7370: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7371: {
7372: int spcol,sprow,a;
7373: int i,j,k,l,rank;
7374: NODE r0,r;
7375: ND_pairs sp;
7376: ND spol;
7377: UINT **spmat;
7378: UINT *svect,*v;
7379: int *colstat;
7380: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7381: int maxrs;
7382: int *spsugar;
7383: ND_pairs *spactive;
7384:
7385: spcol = col-nred;
7386: get_eg(&eg0);
7387: /* elimination (1st step) */
7388: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7389: svect = (UINT *)MALLOC(col*sizeof(UINT));
7390: spsugar = (int *)MALLOC(nsp*sizeof(int));
7391: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7392: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7393: nd_sp(m,0,sp,&spol);
7394: if ( !spol ) continue;
7395: nd_to_vect(m,s0vect,col,spol,svect);
7396: if ( m == -1 )
7397: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7398: else
7399: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7400: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7401: if ( i < col ) {
7402: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7403: for ( j = k = 0; j < col; j++ )
7404: if ( !rhead[j] ) v[k++] = svect[j];
7405: spsugar[sprow] = MAX(maxrs,SG(spol));
7406: if ( nz )
7407: spactive[sprow] = sp;
7408: sprow++;
7409: }
7410: nd_free(spol);
7411: }
7412: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7413: if ( DP_Print ) {
1.5 noro 7414: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7415: fflush(asir_out);
7416: }
7417: /* free index arrays */
7418: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7419:
7420: /* elimination (2nd step) */
7421: colstat = (int *)MALLOC(spcol*sizeof(int));
7422: if ( m == -1 )
7423: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7424: else
7425: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7426: r0 = 0;
7427: for ( i = 0; i < rank; i++ ) {
7428: NEXTNODE(r0,r); BDY(r) =
7429: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7430: SG((NDV)BDY(r)) = spsugar[i];
7431: GCFREE(spmat[i]);
7432: }
7433: if ( r0 ) NEXT(r) = 0;
7434:
7435: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7436: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7437: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7438: if ( DP_Print ) {
1.5 noro 7439: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7440: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7441: nsp,nred,sprow,spcol,rank);
1.5 noro 7442: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7443: }
7444: if ( nz ) {
7445: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7446: if ( rank > 0 ) {
7447: NEXT(spactive[rank-1]) = 0;
7448: *nz = spactive[0];
7449: } else
7450: *nz = 0;
7451: }
7452: return r0;
7453: }
7454:
7455:
7456: /* for small finite fields */
7457:
7458: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7459: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7460: {
7461: int spcol,sprow,a;
7462: int i,j,k,l,rank;
7463: NODE r0,r;
7464: ND_pairs sp;
7465: ND spol;
7466: UINT **spmat;
7467: UINT *svect,*v;
7468: int *colstat;
7469: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7470: int maxrs;
7471: int *spsugar;
7472: ND_pairs *spactive;
7473:
7474: spcol = col-nred;
7475: get_eg(&eg0);
7476: /* elimination (1st step) */
7477: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7478: svect = (UINT *)MALLOC(col*sizeof(UINT));
7479: spsugar = (int *)MALLOC(nsp*sizeof(int));
7480: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7481: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7482: nd_sp(m,0,sp,&spol);
7483: if ( !spol ) continue;
7484: nd_to_vect(m,s0vect,col,spol,svect);
7485: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7486: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7487: if ( i < col ) {
7488: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7489: for ( j = k = 0; j < col; j++ )
7490: if ( !rhead[j] ) v[k++] = svect[j];
7491: spsugar[sprow] = MAX(maxrs,SG(spol));
7492: if ( nz )
7493: spactive[sprow] = sp;
7494: sprow++;
7495: }
7496: nd_free(spol);
7497: }
7498: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7499: if ( DP_Print ) {
1.5 noro 7500: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7501: fflush(asir_out);
7502: }
7503: /* free index arrays */
7504: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7505:
7506: /* elimination (2nd step) */
7507: colstat = (int *)MALLOC(spcol*sizeof(int));
7508: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7509: r0 = 0;
7510: for ( i = 0; i < rank; i++ ) {
7511: NEXTNODE(r0,r); BDY(r) =
7512: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7513: SG((NDV)BDY(r)) = spsugar[i];
7514: GCFREE(spmat[i]);
7515: }
7516: if ( r0 ) NEXT(r) = 0;
7517:
7518: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7519: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7520: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7521: if ( DP_Print ) {
1.5 noro 7522: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7523: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7524: nsp,nred,sprow,spcol,rank);
1.5 noro 7525: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7526: }
7527: if ( nz ) {
7528: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7529: if ( rank > 0 ) {
7530: NEXT(spactive[rank-1]) = 0;
7531: *nz = spactive[0];
7532: } else
7533: *nz = 0;
7534: }
7535: return r0;
7536: }
7537:
7538: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7539: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7540: {
7541: int spcol,sprow,a;
7542: int i,j,k,l,rank;
7543: NODE r0,r;
7544: ND_pairs sp;
7545: ND spol;
7546: mpz_t **spmat;
7547: mpz_t *svect,*v;
7548: int *colstat;
7549: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7550: int maxrs;
7551: int *spsugar;
7552: pointer *w;
7553:
7554: spcol = col-nred;
7555: get_eg(&eg0);
7556: /* elimination (1st step) */
7557: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7558: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7559: spsugar = (int *)MALLOC(nsp*sizeof(int));
7560: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7561: nd_sp(m,trace,sp,&spol);
7562: if ( !spol ) continue;
7563: nd_to_vect_lf(s0vect,col,spol,svect);
7564: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7565: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7566: if ( i < col ) {
7567: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7568: for ( j = k = 0; j < col; j++ )
7569: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7570: spsugar[sprow] = MAX(maxrs,SG(spol));
7571: sprow++;
7572: }
7573: /* nd_free(spol); */
7574: }
7575: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7576: if ( DP_Print ) {
1.5 noro 7577: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7578: fflush(asir_out);
7579: }
7580: /* free index arrays */
7581: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7582:
7583: /* elimination (2nd step) */
7584: colstat = (int *)MALLOC(spcol*sizeof(int));
7585: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7586: w = (pointer *)MALLOC(rank*sizeof(pointer));
7587: for ( i = 0; i < rank; i++ ) {
7588: #if 0
7589: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7590: SG((NDV)w[rank-i-1]) = spsugar[i];
7591: #else
7592: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7593: SG((NDV)w[i]) = spsugar[i];
7594: #endif
7595: /* GCFREE(spmat[i]); */
7596:
7597: }
7598: #if 0
7599: qsort(w,rank,sizeof(NDV),
7600: (int (*)(const void *,const void *))ndv_compare);
7601: #endif
7602: r0 = 0;
7603: for ( i = 0; i < rank; i++ ) {
7604: NEXTNODE(r0,r); BDY(r) = w[i];
7605: }
7606: if ( r0 ) NEXT(r) = 0;
7607:
7608: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7609: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7610: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7611: if ( DP_Print ) {
1.5 noro 7612: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7613: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7614: nsp,nred,sprow,spcol,rank);
1.5 noro 7615: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7616: }
7617: return r0;
7618: }
7619:
7620: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7621: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7622: {
7623: int spcol,sprow,a;
7624: int i,j,k,l,rank;
7625: NODE r0,r;
7626: ND_pairs sp;
7627: ND spol;
7628: Z **spmat;
7629: Z *svect,*v;
7630: int *colstat;
7631: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7632: int maxrs;
7633: int *spsugar;
7634: pointer *w;
7635:
7636: spcol = col-nred;
7637: get_eg(&eg0);
7638: /* elimination (1st step) */
7639: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7640: svect = (Z *)MALLOC(col*sizeof(Q));
7641: spsugar = (int *)MALLOC(nsp*sizeof(int));
7642: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7643: nd_sp(0,trace,sp,&spol);
7644: if ( !spol ) continue;
7645: nd_to_vect_q(s0vect,col,spol,svect);
7646: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7647: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7648: if ( i < col ) {
7649: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7650: for ( j = k = 0; j < col; j++ )
7651: if ( !rhead[j] ) v[k++] = svect[j];
7652: spsugar[sprow] = MAX(maxrs,SG(spol));
7653: sprow++;
7654: }
7655: /* nd_free(spol); */
7656: }
7657: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7658: if ( DP_Print ) {
1.5 noro 7659: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7660: fflush(asir_out);
7661: }
7662: /* free index arrays */
7663: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7664:
7665: /* elimination (2nd step) */
7666: colstat = (int *)MALLOC(spcol*sizeof(int));
7667: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7668: w = (pointer *)MALLOC(rank*sizeof(pointer));
7669: for ( i = 0; i < rank; i++ ) {
7670: #if 0
7671: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7672: SG((NDV)w[rank-i-1]) = spsugar[i];
7673: #else
7674: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7675: SG((NDV)w[i]) = spsugar[i];
7676: #endif
7677: /* GCFREE(spmat[i]); */
7678: }
7679: #if 0
7680: qsort(w,rank,sizeof(NDV),
7681: (int (*)(const void *,const void *))ndv_compare);
7682: #endif
7683: r0 = 0;
7684: for ( i = 0; i < rank; i++ ) {
7685: NEXTNODE(r0,r); BDY(r) = w[i];
7686: }
7687: if ( r0 ) NEXT(r) = 0;
7688:
7689: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7690: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7691: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7692: if ( DP_Print ) {
1.5 noro 7693: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7694: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7695: nsp,nred,sprow,spcol,rank);
1.5 noro 7696: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7697: }
7698: return r0;
7699: }
7700:
7701: FILE *nd_write,*nd_read;
7702:
7703: void nd_send_int(int a) {
7704: write_int(nd_write,(unsigned int *)&a);
7705: }
7706:
7707: void nd_send_intarray(int *p,int len) {
7708: write_intarray(nd_write,(unsigned int *)p,len);
7709: }
7710:
7711: int nd_recv_int() {
7712: int a;
7713:
7714: read_int(nd_read,(unsigned int *)&a);
7715: return a;
7716: }
7717:
7718: void nd_recv_intarray(int *p,int len) {
7719: read_intarray(nd_read,(unsigned int *)p,len);
7720: }
7721:
7722: void nd_send_ndv(NDV p) {
7723: int len,i;
7724: NMV m;
7725:
7726: if ( !p ) nd_send_int(0);
7727: else {
7728: len = LEN(p);
7729: nd_send_int(len);
7730: m = BDY(p);
7731: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7732: nd_send_int(CM(m));
7733: nd_send_intarray((int *)DL(m),nd_wpd);
7734: }
7735: }
7736: }
7737:
7738: void nd_send_nd(ND p) {
7739: int len,i;
7740: NM m;
7741:
7742: if ( !p ) nd_send_int(0);
7743: else {
7744: len = LEN(p);
7745: nd_send_int(len);
7746: m = BDY(p);
7747: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7748: nd_send_int(CM(m));
7749: nd_send_intarray((int *)DL(m),nd_wpd);
7750: }
7751: }
7752: }
7753:
7754: NDV nd_recv_ndv()
7755: {
7756: int len,i;
7757: NMV m,m0;
7758: NDV r;
7759:
7760: len = nd_recv_int();
7761: if ( !len ) return 0;
7762: else {
7763: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7764: #if 0
7765: ndv_alloc += len*nmv_adv;
7766: #endif
7767: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7768: CM(m) = nd_recv_int();
7769: nd_recv_intarray((int *)DL(m),nd_wpd);
7770: }
7771: MKNDV(nd_nvar,m0,len,r);
7772: return r;
7773: }
7774: }
7775:
7776: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7777: {
7778: int i,j,t,c,rank,inv;
7779: int *ci,*ri;
7780: Z dn;
7781: MAT m,nm;
7782:
7783: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7784: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7785: for ( i = 0; i < row; i++ )
7786: for ( j = 0; j < col; j++ )
7787: mat0[i][j] = 0;
7788: c = col-rank;
7789: for ( i = 0; i < rank; i++ ) {
7790: mat0[i][ri[i]] = dn;
7791: for ( j = 0; j < c; j++ )
7792: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7793: }
7794: return rank;
7795: }
7796:
7797: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7798: {
7799: int i,j,k,l,inv,a,rank,s;
7800: unsigned int *t,*pivot,*pk;
7801: unsigned int **mat;
7802: ND_pairs pair;
7803:
7804: mat = (unsigned int **)mat0;
7805: for ( rank = 0, j = 0; j < col; j++ ) {
7806: for ( i = rank; i < row; i++ )
7807: mat[i][j] %= md;
7808: for ( i = rank; i < row; i++ )
7809: if ( mat[i][j] )
7810: break;
7811: if ( i == row ) {
7812: colstat[j] = 0;
7813: continue;
7814: } else
7815: colstat[j] = 1;
7816: if ( i != rank ) {
7817: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7818: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7819: if ( spactive ) {
7820: pair = spactive[i]; spactive[i] = spactive[rank];
7821: spactive[rank] = pair;
7822: }
7823: }
7824: pivot = mat[rank];
7825: s = sugar[rank];
7826: inv = invm(pivot[j],md);
7827: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7828: if ( *pk ) {
7829: if ( *pk >= (unsigned int)md )
7830: *pk %= md;
7831: DMAR(*pk,inv,0,md,*pk)
7832: }
7833: for ( i = rank+1; i < row; i++ ) {
7834: t = mat[i];
7835: if ( (a = t[j]) != 0 ) {
7836: sugar[i] = MAX(sugar[i],s);
7837: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7838: }
7839: }
7840: rank++;
7841: }
7842: for ( j = col-1, l = rank-1; j >= 0; j-- )
7843: if ( colstat[j] ) {
7844: pivot = mat[l];
7845: s = sugar[l];
7846: for ( i = 0; i < l; i++ ) {
7847: t = mat[i];
7848: t[j] %= md;
7849: if ( (a = t[j]) != 0 ) {
7850: sugar[i] = MAX(sugar[i],s);
7851: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7852: }
7853: }
7854: l--;
7855: }
7856: for ( j = 0, l = 0; l < rank; j++ )
7857: if ( colstat[j] ) {
7858: t = mat[l];
7859: for ( k = j; k < col; k++ )
7860: if ( t[k] >= (unsigned int)md )
7861: t[k] %= md;
7862: l++;
7863: }
7864: return rank;
7865: }
7866:
7867:
1.7 noro 7868: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7869: {
1.7 noro 7870: int i,j,k,l,inv,a,rank,s;
7871: unsigned int *t,*pivot,*pk;
7872: unsigned int **mat;
7873:
7874: mat = (unsigned int **)mat0;
7875: for ( rank = 0, j = 0; j < col; j++ ) {
7876: for ( i = rank; i < row; i++ )
7877: if ( mat[i][j] )
7878: break;
7879: if ( i == row ) {
7880: colstat[j] = 0;
7881: continue;
7882: } else
7883: colstat[j] = 1;
7884: if ( i != rank ) {
7885: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7886: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7887: }
7888: pivot = mat[rank];
7889: s = sugar[rank];
7890: inv = _invsf(pivot[j]);
7891: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7892: if ( *pk )
7893: *pk = _mulsf(*pk,inv);
7894: for ( i = rank+1; i < row; i++ ) {
7895: t = mat[i];
7896: if ( (a = t[j]) != 0 ) {
7897: sugar[i] = MAX(sugar[i],s);
7898: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7899: }
7900: }
7901: rank++;
7902: }
7903: for ( j = col-1, l = rank-1; j >= 0; j-- )
7904: if ( colstat[j] ) {
7905: pivot = mat[l];
7906: s = sugar[l];
7907: for ( i = 0; i < l; i++ ) {
7908: t = mat[i];
7909: if ( (a = t[j]) != 0 ) {
7910: sugar[i] = MAX(sugar[i],s);
7911: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7912: }
7913: }
7914: l--;
7915: }
7916: return rank;
7917: }
1.1 noro 7918:
1.7 noro 7919: int ndv_ishomo(NDV p)
7920: {
7921: NMV m;
7922: int len,h;
1.1 noro 7923:
7924: if ( !p ) return 1;
7925: len = LEN(p);
7926: m = BDY(p);
7927: h = TD(DL(m));
7928: NMV_ADV(m);
7929: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 7930: if ( TD(DL(m)) != h ) {
7931: return 0;
7932: }
1.1 noro 7933: return 1;
7934: }
7935:
7936: void ndv_save(NDV p,int index)
7937: {
7938: FILE *s;
7939: char name[BUFSIZ];
7940: short id;
7941: int nv,sugar,len,n,i,td,e,j;
7942: NMV m;
7943: unsigned int *dl;
7944: int mpos;
7945:
7946: sprintf(name,"%s/%d",Demand,index);
7947: s = fopen(name,"w");
7948: savevl(s,0);
7949: if ( !p ) {
7950: saveobj(s,0);
7951: return;
7952: }
7953: id = O_DP;
7954: nv = NV(p);
7955: sugar = SG(p);
7956: len = LEN(p);
7957: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7958: write_int(s,(unsigned int *)&len);
7959:
7960: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7961: saveobj(s,(Obj)CZ(m));
1.1 noro 7962: dl = DL(m);
7963: td = TD(dl);
7964: write_int(s,(unsigned int *)&td);
7965: for ( j = 0; j < nv; j++ ) {
7966: e = GET_EXP(dl,j);
7967: write_int(s,(unsigned int *)&e);
7968: }
7969: if ( nd_module ) {
7970: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7971: }
7972: }
7973: fclose(s);
7974: }
7975:
7976: void nd_save_mod(ND p,int index)
7977: {
7978: FILE *s;
7979: char name[BUFSIZ];
7980: int nv,sugar,len,c;
7981: NM m;
7982:
7983: sprintf(name,"%s/%d",Demand,index);
7984: s = fopen(name,"w");
7985: if ( !p ) {
7986: len = 0;
7987: write_int(s,(unsigned int *)&len);
7988: fclose(s);
7989: return;
7990: }
7991: nv = NV(p);
7992: sugar = SG(p);
7993: len = LEN(p);
7994: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7995: for ( m = BDY(p); m; m = NEXT(m) ) {
7996: c = CM(m); write_int(s,(unsigned int *)&c);
7997: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7998: }
7999: fclose(s);
8000: }
8001:
8002: NDV ndv_load(int index)
8003: {
8004: FILE *s;
8005: char name[BUFSIZ];
8006: short id;
8007: int nv,sugar,len,n,i,td,e,j;
8008: NDV d;
8009: NMV m0,m;
8010: unsigned int *dl;
8011: Obj obj;
8012: int mpos;
8013:
8014: sprintf(name,"%s/%d",Demand,index);
8015: s = fopen(name,"r");
8016: if ( !s ) return 0;
8017:
8018: skipvl(s);
8019: read_short(s,(unsigned short *)&id);
8020: if ( !id ) return 0;
8021: read_int(s,(unsigned int *)&nv);
8022: read_int(s,(unsigned int *)&sugar);
8023: read_int(s,(unsigned int *)&len);
8024:
8025: m0 = m = MALLOC(len*nmv_adv);
8026: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8027: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 8028: dl = DL(m);
8029: ndl_zero(dl);
8030: read_int(s,(unsigned int *)&td); TD(dl) = td;
8031: for ( j = 0; j < nv; j++ ) {
8032: read_int(s,(unsigned int *)&e);
8033: PUT_EXP(dl,j,e);
8034: }
8035: if ( nd_module ) {
8036: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
8037: }
8038: if ( nd_blockmask ) ndl_weight_mask(dl);
8039: }
8040: fclose(s);
8041: MKNDV(nv,m0,len,d);
8042: SG(d) = sugar;
8043: return d;
8044: }
8045:
8046: ND nd_load_mod(int index)
8047: {
8048: FILE *s;
8049: char name[BUFSIZ];
8050: int nv,sugar,len,i,c;
8051: ND d;
8052: NM m0,m;
8053:
8054: sprintf(name,"%s/%d",Demand,index);
8055: s = fopen(name,"r");
8056: /* if the file does not exist, it means p[index]=0 */
8057: if ( !s ) return 0;
8058:
8059: read_int(s,(unsigned int *)&nv);
8060: if ( !nv ) { fclose(s); return 0; }
8061:
8062: read_int(s,(unsigned int *)&sugar);
8063: read_int(s,(unsigned int *)&len);
8064: for ( m0 = 0, i = 0; i < len; i++ ) {
8065: NEXTNM(m0,m);
8066: read_int(s,(unsigned int *)&c); CM(m) = c;
8067: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
8068: }
8069: NEXT(m) = 0;
8070: MKND(nv,m0,len,d);
8071: SG(d) = sugar;
8072: fclose(s);
8073: return d;
8074: }
8075:
8076: void nd_det(int mod,MAT f,P *rp)
8077: {
8078: VL fv,tv;
8079: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
8080: pointer **m;
8081: P **w;
8082: P mp,r;
8083: NDV **dm;
8084: NDV *t,*mi,*mj;
8085: NDV d,s,mij,mjj;
8086: ND u;
8087: NMV nmv;
8088: UINT *bound;
8089: PGeoBucket bucket;
8090: struct order_spec *ord;
8091: Z dq,dt,ds;
8092: Z mone;
8093: Z gn,qn,dn0,nm,dn;
8094:
8095: create_order_spec(0,0,&ord);
8096: nd_init_ord(ord);
8097: get_vars((Obj)f,&fv);
8098: if ( f->row != f->col )
8099: error("nd_det : non-square matrix");
8100: n = f->row;
8101: m = f->body;
8102: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8103:
8104: if ( !nvar ) {
8105: if ( !mod )
8106: detp(CO,(P **)m,n,rp);
8107: else {
8108: w = (P **)almat_pointer(n,n);
8109: for ( i = 0; i < n; i++ )
8110: for ( j = 0; j < n; j++ )
8111: ptomp(mod,(P)m[i][j],&w[i][j]);
8112: detmp(CO,mod,w,n,&mp);
8113: mptop(mp,rp);
8114: }
8115: return;
8116: }
8117:
8118: if ( !mod ) {
8119: w = (P **)almat_pointer(n,n);
8120: dq = ONE;
8121: for ( i = 0; i < n; i++ ) {
8122: dn0 = ONE;
8123: for ( j = 0; j < n; j++ ) {
8124: if ( !m[i][j] ) continue;
8125: lgp(m[i][j],&nm,&dn);
1.6 noro 8126: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 8127: }
8128: if ( !UNIZ(dn0) ) {
8129: ds = dn0;
8130: for ( j = 0; j < n; j++ )
8131: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8132: mulz(dq,ds,&dt); dq = dt;
8133: } else
8134: for ( j = 0; j < n; j++ )
8135: w[i][j] = (P)m[i][j];
8136: }
8137: m = (pointer **)w;
8138: }
8139:
8140: for ( i = 0, max = 1; i < n; i++ )
8141: for ( j = 0; j < n; j++ )
8142: for ( tv = fv; tv; tv = NEXT(tv) ) {
8143: e = getdeg(tv->v,(P)m[i][j]);
8144: max = MAX(e,max);
8145: }
8146: nd_setup_parameters(nvar,max);
8147: dm = (NDV **)almat_pointer(n,n);
8148: for ( i = 0, max = 1; i < n; i++ )
8149: for ( j = 0; j < n; j++ ) {
8150: dm[i][j] = ptondv(CO,fv,m[i][j]);
8151: if ( mod ) ndv_mod(mod,dm[i][j]);
8152: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8153: }
8154: d = ptondv(CO,fv,(P)ONE);
8155: if ( mod ) ndv_mod(mod,d);
8156: chsgnz(ONE,&mone);
8157: for ( j = 0, sgn = 1; j < n; j++ ) {
8158: if ( DP_Print ) {
8159: fprintf(asir_out,".");
8160: }
8161: for ( i = j; i < n && !dm[i][j]; i++ );
8162: if ( i == n ) {
8163: *rp = 0;
8164: return;
8165: }
8166: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8167: for ( k = j; k < n; k++ )
8168: for ( l = j; l < n; l++ )
8169: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8170: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8171: }
8172: if ( k0 != j ) {
8173: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8174: sgn = -sgn;
8175: }
8176: if ( l0 != j ) {
8177: for ( k = j; k < n; k++ ) {
8178: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8179: }
8180: sgn = -sgn;
8181: }
8182: bound = nd_det_compute_bound(dm,n,j);
8183: for ( k = 0; k < nd_nvar; k++ )
8184: if ( bound[k]*2 > nd_mask0 ) break;
8185: if ( k < nd_nvar )
8186: nd_det_reconstruct(dm,n,j,d);
8187:
8188: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8189: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8190: mi = dm[i]; mij = mi[j];
8191: if ( mod )
8192: ndv_mul_c(mod,mij,mod-1);
8193: else
8194: ndv_mul_c_q(mij,mone);
8195: for ( k = j+1; k < n; k++ ) {
8196: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
8197: bucket = create_pbucket();
8198: if ( mi[k] ) {
8199: nmv = BDY(mjj); len = LEN(mjj);
8200: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8201: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8202: add_pbucket(mod,bucket,u);
8203: }
8204: }
8205: if ( mj[k] && mij ) {
8206: nmv = BDY(mij); len = LEN(mij);
8207: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8208: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8209: add_pbucket(mod,bucket,u);
8210: }
8211: }
8212: u = nd_quo(mod,bucket,d);
8213: mi[k] = ndtondv(mod,u);
8214: }
8215: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
8216: }
8217: d = mjj;
8218: }
8219: if ( DP_Print ) {
8220: fprintf(asir_out,"\n");
8221: }
8222: if ( sgn < 0 ) {
8223: if ( mod )
8224: ndv_mul_c(mod,d,mod-1);
8225: else
8226: ndv_mul_c_q(d,mone);
8227: }
8228: r = ndvtop(mod,CO,fv,d);
8229: if ( !mod && !UNIQ(dq) )
8230: divsp(CO,r,(P)dq,rp);
8231: else
8232: *rp = r;
8233: }
8234:
8235: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8236: {
8237: NM mr,mr0;
8238: NM tnm;
8239: NMV m;
8240: UINT *d0,*dt,*dm;
8241: int c,n,td,i,c1,c2,len;
8242: Z q;
8243: ND r;
8244:
8245: if ( !p ) return 0;
8246: else {
8247: n = NV(p); m = BDY(p); len = LEN(p);
8248: d0 = DL(m0);
8249: td = TD(d);
8250: mr0 = 0;
8251: NEWNM(tnm);
8252: if ( mod ) {
8253: c = CM(m0);
8254: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8255: ndl_add(DL(m),d0,DL(tnm));
8256: if ( ndl_reducible(DL(tnm),d) ) {
8257: NEXTNM(mr0,mr);
8258: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8259: ndl_copy(DL(tnm),DL(mr));
8260: }
8261: }
8262: } else {
1.6 noro 8263: q = CZ(m0);
1.1 noro 8264: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8265: ndl_add(DL(m),d0,DL(tnm));
8266: if ( ndl_reducible(DL(tnm),d) ) {
8267: NEXTNM(mr0,mr);
1.6 noro 8268: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8269: ndl_copy(DL(tnm),DL(mr));
8270: }
8271: }
8272: }
8273: if ( !mr0 )
8274: return 0;
8275: else {
8276: NEXT(mr) = 0;
8277: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8278: MKND(NV(p),mr0,len,r);
8279: SG(r) = SG(p) + TD(d0);
8280: return r;
8281: }
8282: }
8283: }
8284:
8285: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8286: {
8287: int i,obpe,oadv,h,k,l;
8288: static NM prev_nm_free_list;
8289: EPOS oepos;
8290:
8291: obpe = nd_bpe;
8292: oadv = nmv_adv;
8293: oepos = nd_epos;
8294: if ( obpe < 2 ) nd_bpe = 2;
8295: else if ( obpe < 3 ) nd_bpe = 3;
8296: else if ( obpe < 4 ) nd_bpe = 4;
8297: else if ( obpe < 5 ) nd_bpe = 5;
8298: else if ( obpe < 6 ) nd_bpe = 6;
8299: else if ( obpe < 8 ) nd_bpe = 8;
8300: else if ( obpe < 10 ) nd_bpe = 10;
8301: else if ( obpe < 16 ) nd_bpe = 16;
8302: else if ( obpe < 32 ) nd_bpe = 32;
8303: else error("nd_det_reconstruct : exponent too large");
8304:
8305: nd_setup_parameters(nd_nvar,0);
8306: prev_nm_free_list = _nm_free_list;
8307: _nm_free_list = 0;
8308: for ( k = j; k < n; k++ )
8309: for (l = j; l < n; l++ )
8310: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8311: ndv_realloc(d,obpe,oadv,oepos);
8312: prev_nm_free_list = 0;
8313: #if 0
8314: GC_gcollect();
8315: #endif
8316: }
8317:
8318: /* returns a UINT array containing degree bounds */
8319:
8320: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8321: {
8322: UINT *d0,*d1,*d,*t,*r;
8323: int k,l,i;
8324:
8325: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8326: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8327: for ( k = j; k < n; k++ )
8328: for ( l = j; l < n; l++ )
8329: if ( dm[k][l] ) {
8330: d = ndv_compute_bound(dm[k][l]);
8331: for ( i = 0; i < nd_nvar; i++ )
8332: d0[i] = MAX(d0[i],d[i]);
8333: }
8334: return d0;
8335: }
8336:
8337: DL nd_separate_d(UINT *d,UINT *trans)
8338: {
8339: int n,td,i,e,j;
8340: DL a;
8341:
8342: ndl_zero(trans);
8343: td = 0;
8344: for ( i = 0; i < nd_ntrans; i++ ) {
8345: e = GET_EXP(d,i);
8346: PUT_EXP(trans,i,e);
8347: td += MUL_WEIGHT(e,i);
8348: }
8349: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8350: /* homogenized */
8351: i = nd_nvar-1;
8352: e = GET_EXP(d,i);
8353: PUT_EXP(trans,i,e);
8354: td += MUL_WEIGHT(e,i);
8355: }
8356: TD(trans) = td;
8357: if ( nd_blockmask) ndl_weight_mask(trans);
8358: NEWDL(a,nd_nalg);
8359: td = 0;
8360: for ( i = 0; i < nd_nalg; i++ ) {
8361: j = nd_ntrans+i;
8362: e = GET_EXP(d,j);
8363: a->d[i] = e;
8364: td += e;
8365: }
8366: a->td = td;
8367: return a;
8368: }
8369:
8370: int nd_monic(int mod,ND *p)
8371: {
8372: UINT *trans,*t;
8373: DL alg;
8374: MP mp0,mp;
8375: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8376: ND r;
8377: DL dl;
8378: DP nm;
8379: NDV ndv;
8380: DAlg inv,cd;
8381: ND s,c;
8382: Z l,mul;
8383: Z ln;
8384: int n,ntrans,i,e,td,is_lc,len;
8385: NumberField nf;
8386: struct oEGT eg0,eg1;
8387:
8388: if ( !(nf = get_numberfield()) )
8389: error("nd_monic : current_numberfield is not set");
8390:
8391: /* Q coef -> DAlg coef */
8392: NEWNM(ma0); ma = ma0;
8393: m = BDY(*p);
8394: is_lc = 1;
8395: while ( 1 ) {
8396: NEWMP(mp0); mp = mp0;
1.6 noro 8397: mp->c = (Obj)CZ(m);
1.1 noro 8398: mp->dl = nd_separate_d(DL(m),DL(ma));
8399: NEWNM(mb);
8400: for ( m = NEXT(m); m; m = NEXT(m) ) {
8401: alg = nd_separate_d(DL(m),DL(mb));
8402: if ( !ndl_equal(DL(ma),DL(mb)) )
8403: break;
1.6 noro 8404: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8405: }
8406: NEXT(mp) = 0;
8407: MKDP(nd_nalg,mp0,nm);
8408: MKDAlg(nm,ONE,cd);
8409: if ( is_lc == 1 ) {
8410: /* if the lc is a rational number, we have nothing to do */
8411: if ( !mp0->dl->td )
8412: return 1;
8413:
8414: get_eg(&eg0);
8415: invdalg(cd,&inv);
8416: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8417: /* check the validity of inv */
8418: if ( mod && !remqi((Q)inv->dn,mod) )
8419: return 0;
8420: CA(ma) = nf->one;
8421: is_lc = 0;
8422: ln = ONE;
8423: } else {
8424: muldalg(cd,inv,&CA(ma));
8425: lcmz(ln,CA(ma)->dn,&ln);
8426: }
8427: if ( m ) {
8428: NEXT(ma) = mb; ma = mb;
8429: } else {
8430: NEXT(ma) = 0;
8431: break;
8432: }
8433: }
8434: /* l = lcm(denoms) */
8435: l = ln;
8436: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8437: divsz(l,CA(m)->dn,&mul);
1.1 noro 8438: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8439: NEXTNM(mr0,mr);
1.6 noro 8440: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8441: dl = mp->dl;
8442: td = TD(DL(m));
8443: ndl_copy(DL(m),DL(mr));
8444: for ( i = 0; i < nd_nalg; i++ ) {
8445: e = dl->d[i];
8446: PUT_EXP(DL(mr),i+nd_ntrans,e);
8447: td += MUL_WEIGHT(e,i+nd_ntrans);
8448: }
8449: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8450: TD(DL(mr)) = td;
8451: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8452: }
8453: }
8454: NEXT(mr) = 0;
8455: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8456: MKND(NV(*p),mr0,len,r);
8457: /* XXX */
8458: SG(r) = SG(*p);
8459: nd_free(*p);
8460: *p = r;
8461: return 1;
8462: }
8463:
8464: NODE reverse_node(NODE n)
8465: {
8466: NODE t,t1;
8467:
8468: for ( t = 0; n; n = NEXT(n) ) {
8469: MKNODE(t1,BDY(n),t); t = t1;
8470: }
8471: return t;
8472: }
8473:
8474: P ndc_div(int mod,union oNDC a,union oNDC b)
8475: {
8476: union oNDC c;
8477: int inv,t;
8478:
8479: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8480: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8481: else if ( mod ) {
8482: inv = invm(b.m,mod);
8483: DMAR(a.m,inv,0,mod,t); c.m = t;
8484: } else if ( nd_vc )
8485: divsp(nd_vc,a.p,b.p,&c.p);
8486: else
8487: divsz(a.z,b.z,&c.z);
8488: return ndctop(mod,c);
8489: }
8490:
8491: P ndctop(int mod,union oNDC c)
8492: {
8493: Z q;
8494: int e;
8495: GFS gfs;
8496:
8497: if ( mod == -1 ) {
8498: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8499: } else if ( mod == -2 ) {
1.10 noro 8500: q = c.z; return (P)q;
1.1 noro 8501: } else if ( mod > 0 ) {
1.6 noro 8502: STOZ(c.m,q); return (P)q;
1.1 noro 8503: } else
8504: return (P)c.p;
8505: }
8506:
8507: /* [0,0,0,cont] = p -> p/cont */
8508:
8509: void finalize_tracelist(int i,P cont)
8510: {
8511: LIST l;
8512: NODE node;
8513: Z iq;
8514:
8515: if ( !UNIQ(cont) ) {
8516: node = mknode(4,NULLP,NULLP,NULLP,cont);
8517: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8518: nd_tracelist = node;
8519: }
1.6 noro 8520: STOZ(i,iq);
1.1 noro 8521: nd_tracelist = reverse_node(nd_tracelist);
8522: MKLIST(l,nd_tracelist);
8523: node = mknode(2,iq,l); MKLIST(l,node);
8524: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8525: nd_alltracelist = node; nd_tracelist = 0;
8526: }
8527:
8528: void conv_ilist(int demand,int trace,NODE g,int **indp)
8529: {
8530: int n,i,j;
8531: int *ind;
8532: NODE t;
8533:
8534: n = length(g);
8535: ind = (int *)MALLOC(n*sizeof(int));
8536: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8537: j = (long)BDY(t); ind[i] = j;
8538: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8539: }
8540: if ( indp ) *indp = ind;
8541: }
8542:
8543: void parse_nd_option(NODE opt)
8544: {
8545: NODE t,p,u;
8546: int i,s,n;
8547: char *key;
8548: Obj value;
8549:
8550: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8551: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8552: nd_splist = 0; nd_check_splist = 0;
8553: nd_sugarweight = 0;
8554: nd_f4red =0;
8555: nd_rank0 = 0;
8556: for ( t = opt; t; t = NEXT(t) ) {
8557: p = BDY((LIST)BDY(t));
8558: key = BDY((STRING)BDY(p));
8559: value = (Obj)BDY(NEXT(p));
8560: if ( !strcmp(key,"gentrace") )
8561: nd_gentrace = value?1:0;
8562: else if ( !strcmp(key,"gensyz") )
8563: nd_gensyz = value?1:0;
8564: else if ( !strcmp(key,"nora") )
8565: nd_nora = value?1:0;
8566: else if ( !strcmp(key,"gbblock") ) {
8567: if ( value && OID(value) == O_LIST ) {
8568: u = BDY((LIST)value);
8569: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8570: for ( i = 0; u; u = NEXT(u) ) {
8571: p = BDY((LIST)BDY(u));
1.6 noro 8572: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8573: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8574: }
8575: nd_gbblock[i] = -1;
8576: } else
8577: nd_gbblock = 0;
8578: } else if ( !strcmp(key,"newelim") )
8579: nd_newelim = value?1:0;
8580: else if ( !strcmp(key,"intersect") )
8581: nd_intersect = value?1:0;
1.17 noro 8582: else if ( !strcmp(key,"syzgen") )
8583: nd_intersect = ZTOS((Q)value);
1.1 noro 8584: else if ( !strcmp(key,"lf") )
8585: nd_lf = value?1:0;
8586: else if ( !strcmp(key,"trace") ) {
8587: if ( value ) {
8588: u = BDY((LIST)value);
8589: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8590: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8591: }
8592: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8593: nd_f4red = ZTOS((Q)value);
1.1 noro 8594: } else if ( !strcmp(key,"rank0") ) {
8595: nd_rank0 = value?1:0;
8596: } else if ( !strcmp(key,"splist") ) {
8597: nd_splist = value?1:0;
8598: } else if ( !strcmp(key,"check_splist") ) {
8599: nd_check_splist = BDY((LIST)value);
8600: } else if ( !strcmp(key,"sugarweight") ) {
8601: u = BDY((LIST)value);
8602: n = length(u);
8603: nd_sugarweight = MALLOC(n*sizeof(int));
8604: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8605: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8606: }
8607: }
8608: }
8609:
8610: ND mdptond(DP d);
8611: ND nd_mul_nm(int mod,NM m0,ND p);
8612: ND nd_mul_nm_lf(NM m0,ND p);
8613: ND *btog(NODE ti,ND **p,int nb,int mod);
8614: ND btog_one(NODE ti,ND *p,int nb,int mod);
8615: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8616: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8617:
8618: /* d:monomial */
8619: ND mdptond(DP d)
8620: {
8621: NM m;
8622: ND r;
8623:
8624: if ( OID(d) == 1 )
8625: r = ptond(CO,CO,(P)d);
8626: else {
8627: NEWNM(m);
8628: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8629: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8630: NEXT(m) = 0;
8631: MKND(NV(d),m,1,r);
8632: }
8633: return r;
8634: }
8635:
8636: ND nd_mul_nm(int mod,NM m0,ND p)
8637: {
8638: UINT *d0;
8639: int c0,c1,c;
8640: NM tm,mr,mr0;
8641: ND r;
8642:
8643: if ( !p ) return 0;
8644: d0 = DL(m0);
8645: c0 = CM(m0);
8646: mr0 = 0;
8647: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8648: NEXTNM(mr0,mr);
8649: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8650: ndl_add(d0,DL(tm),DL(mr));
8651: }
8652: NEXT(mr) = 0;
8653: MKND(NV(p),mr0,LEN(p),r);
8654: return r;
8655: }
8656:
8657: ND nd_mul_nm_lf(NM m0,ND p)
8658: {
8659: UINT *d0;
8660: Z c0,c1,c;
8661: NM tm,mr,mr0;
8662: ND r;
8663:
8664: if ( !p ) return 0;
8665: d0 = DL(m0);
8666: c0 = CZ(m0);
8667: mr0 = 0;
8668: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8669: NEXTNM(mr0,mr);
8670: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8671: ndl_add(d0,DL(tm),DL(mr));
8672: }
8673: NEXT(mr) = 0;
8674: MKND(NV(p),mr0,LEN(p),r);
8675: return r;
8676: }
8677:
8678: ND *btog(NODE ti,ND **p,int nb,int mod)
8679: {
8680: PGeoBucket *r;
8681: int i,ci;
8682: NODE t,s;
8683: ND m,tp;
8684: ND *pi,*rd;
8685: P c;
8686:
8687: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8688: for ( i = 0; i < nb; i++ )
8689: r[i] = create_pbucket();
8690: for ( t = ti; t; t = NEXT(t) ) {
8691: s = BDY((LIST)BDY(t));
8692: if ( ARG0(s) ) {
8693: m = mdptond((DP)ARG2(s));
1.6 noro 8694: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8695: if ( (ci = ((MQ)c)->cont) != 0 ) {
8696: HCM(m) = ci;
1.6 noro 8697: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8698: for ( i = 0; i < nb; i++ ) {
8699: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8700: add_pbucket(mod,r[i],tp);
8701: }
8702: }
8703: ci = 1;
8704: } else {
8705: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8706: ci = invm(ci,mod);
8707: }
8708: }
8709: rd = (ND *)MALLOC(nb*sizeof(ND));
8710: for ( i = 0; i < nb; i++ )
8711: rd[i] = normalize_pbucket(mod,r[i]);
8712: if ( ci != 1 )
8713: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8714: return rd;
8715: }
8716:
8717: /* YYY */
8718: ND *btog_lf(NODE ti,ND **p,int nb)
8719: {
8720: PGeoBucket *r;
8721: int i;
8722: NODE t,s;
8723: ND m,tp;
8724: ND *pi,*rd;
8725: LM lm;
8726: Z lf,c;
8727:
8728: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8729: for ( i = 0; i < nb; i++ )
8730: r[i] = create_pbucket();
8731: for ( t = ti; t; t = NEXT(t) ) {
8732: s = BDY((LIST)BDY(t));
8733: if ( ARG0(s) ) {
8734: m = mdptond((DP)ARG2(s));
1.6 noro 8735: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8736: if ( lm ) {
8737: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8738: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8739: for ( i = 0; i < nb; i++ ) {
8740: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8741: add_pbucket(-2,r[i],tp);
8742: }
8743: }
8744: c = ONE;
8745: } else {
8746: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8747: }
8748: }
8749: rd = (ND *)MALLOC(nb*sizeof(ND));
8750: for ( i = 0; i < nb; i++ )
8751: rd[i] = normalize_pbucket(-2,r[i]);
8752: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8753: return rd;
8754: }
8755:
8756: ND btog_one(NODE ti,ND *p,int nb,int mod)
8757: {
8758: PGeoBucket r;
8759: int i,ci,j;
8760: NODE t,s;
8761: ND m,tp;
8762: ND pi,rd;
8763: P c;
8764:
8765: r = create_pbucket();
8766: for ( t = ti; t; t = NEXT(t) ) {
8767: s = BDY((LIST)BDY(t));
8768: if ( ARG0(s) ) {
8769: m = mdptond((DP)ARG2(s));
1.6 noro 8770: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8771: if ( (ci = ((MQ)c)->cont) != 0 ) {
8772: HCM(m) = ci;
1.6 noro 8773: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8774: if ( !pi ) {
8775: pi = nd_load_mod(j);
8776: tp = nd_mul_nm(mod,BDY(m),pi);
8777: nd_free(pi);
8778: add_pbucket(mod,r,tp);
8779: } else {
8780: tp = nd_mul_nm(mod,BDY(m),pi);
8781: add_pbucket(mod,r,tp);
8782: }
8783: }
8784: ci = 1;
8785: } else {
8786: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8787: ci = invm(ci,mod);
8788: }
8789: }
8790: rd = normalize_pbucket(mod,r);
8791: free_pbucket(r);
8792: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8793: return rd;
8794: }
8795:
8796: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8797:
8798: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8799: {
8800: int i,j,n,m,nb,pi0,pi1,nvar;
8801: VL fv,tv,vv;
8802: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8803: ND **p;
8804: ND *c;
8805: ND u;
8806: P inv;
8807: MAT mat;
8808:
8809: if ( mod == -2 )
8810: return nd_btog_lf(f,v,ord,tlist,rp);
8811:
8812: parse_nd_option(current_option);
8813: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8814: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8815: switch ( ord->id ) {
8816: case 1:
8817: if ( ord->nv != nvar )
8818: error("nd_check : invalid order specification");
8819: break;
8820: default:
8821: break;
8822: }
8823: nd_init_ord(ord);
8824: #if 0
1.6 noro 8825: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8826: #else
8827: nd_bpe = 32;
8828: #endif
8829: nd_setup_parameters(nvar,0);
8830: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8831: intred = BDY((LIST)ARG3(BDY(tlist)));
8832: ind = BDY((LIST)ARG4(BDY(tlist)));
8833: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8834: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8835: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8836: if ( j > i ) i = j;
8837: }
8838: n = i+1;
8839: nb = length(BDY(f));
8840: p = (ND **)MALLOC(n*sizeof(ND *));
8841: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8842: pi = BDY((LIST)BDY(t));
1.6 noro 8843: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8844: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8845: ptomp(mod,(P)ARG2(pi),&inv);
8846: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8847: u = ptond(CO,vv,(P)ONE);
8848: HCM(u) = ((MQ)inv)->cont;
8849: c[pi1] = u;
8850: }
8851: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8852: printf("%d ",i); fflush(stdout);
8853: ti = BDY((LIST)BDY(t));
1.6 noro 8854: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8855: }
8856: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8857: printf("%d ",i); fflush(stdout);
8858: ti = BDY((LIST)BDY(t));
1.6 noro 8859: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8860: }
8861: m = length(ind);
8862: MKMAT(mat,nb,m);
8863: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8864: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8865: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8866: return mat;
8867: }
8868:
8869: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8870: {
8871: int i,j,n,m,nb,pi0,pi1,nvar;
8872: VL fv,tv,vv;
8873: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8874: ND **p;
8875: ND *c;
8876: ND u;
8877: MAT mat;
8878: LM lm;
8879: Z lf,inv;
8880:
8881: parse_nd_option(current_option);
8882: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8883: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8884: switch ( ord->id ) {
8885: case 1:
8886: if ( ord->nv != nvar )
8887: error("nd_check : invalid order specification");
8888: break;
8889: default:
8890: break;
8891: }
8892: nd_init_ord(ord);
8893: #if 0
1.6 noro 8894: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8895: #else
8896: nd_bpe = 32;
8897: #endif
8898: nd_setup_parameters(nvar,0);
8899: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8900: intred = BDY((LIST)ARG3(BDY(tlist)));
8901: ind = BDY((LIST)ARG4(BDY(tlist)));
8902: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8903: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8904: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8905: if ( j > i ) i = j;
8906: }
8907: n = i+1;
8908: nb = length(BDY(f));
8909: p = (ND **)MALLOC(n*sizeof(ND *));
8910: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8911: pi = BDY((LIST)BDY(t));
1.6 noro 8912: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8913: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8914: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8915: u = ptond(CO,vv,(P)ONE);
8916: HCZ(u) = inv;
8917: c[pi1] = u;
8918: }
8919: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8920: printf("%d ",i); fflush(stdout);
8921: ti = BDY((LIST)BDY(t));
1.6 noro 8922: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8923: }
8924: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8925: printf("%d ",i); fflush(stdout);
8926: ti = BDY((LIST)BDY(t));
1.6 noro 8927: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8928: }
8929: m = length(ind);
8930: MKMAT(mat,nb,m);
8931: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8932: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8933: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8934: return mat;
8935: }
8936:
8937: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8938: LIST tlist,int pos,MAT *rp)
8939: {
8940: int i,j,n,m,nb,pi0,pi1,nvar;
8941: VL fv,tv,vv;
8942: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8943: ND *p;
8944: ND *c;
8945: ND u;
8946: P inv;
8947: VECT vect;
8948:
8949: if ( mod == -2 )
8950: error("nd_btog_one : not implemented yet for a large finite field");
8951:
8952: parse_nd_option(current_option);
8953: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8954: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8955: switch ( ord->id ) {
8956: case 1:
8957: if ( ord->nv != nvar )
8958: error("nd_check : invalid order specification");
8959: break;
8960: default:
8961: break;
8962: }
8963: nd_init_ord(ord);
8964: #if 0
1.6 noro 8965: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8966: #else
8967: nd_bpe = 32;
8968: #endif
8969: nd_setup_parameters(nvar,0);
8970: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8971: intred = BDY((LIST)ARG3(BDY(tlist)));
8972: ind = BDY((LIST)ARG4(BDY(tlist)));
8973: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8974: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8975: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8976: if ( j > i ) i = j;
8977: }
8978: n = i+1;
8979: nb = length(BDY(f));
8980: p = (ND *)MALLOC(n*sizeof(ND *));
8981: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8982: pi = BDY((LIST)BDY(t));
1.6 noro 8983: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8984: if ( pi1 == pos ) {
8985: ptomp(mod,(P)ARG2(pi),&inv);
8986: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8987: u = ptond(CO,vv,(P)ONE);
8988: HCM(u) = ((MQ)inv)->cont;
8989: p[pi0] = u;
8990: }
8991: }
8992: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8993: printf("%d ",i); fflush(stdout);
8994: ti = BDY((LIST)BDY(t));
1.6 noro 8995: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8996: if ( Demand ) {
8997: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8998: }
8999: }
9000: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9001: printf("%d ",i); fflush(stdout);
9002: ti = BDY((LIST)BDY(t));
1.6 noro 9003: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9004: if ( Demand ) {
9005: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
9006: }
9007: }
9008: m = length(ind);
9009: MKVECT(vect,m);
9010: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 9011: u = p[ZTOS((Q)BDY(t))];
1.1 noro 9012: if ( !u ) {
1.6 noro 9013: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 9014: BDY(vect)[j] = ndtodp(mod,u);
9015: nd_free(u);
9016: } else
9017: BDY(vect)[j] = ndtodp(mod,u);
9018: }
9019: return vect;
9020: }
9021:
9022: void ndv_print_lf(NDV p)
9023: {
9024: NMV m;
9025: int i,len;
9026:
9027: if ( !p ) printf("0\n");
9028: else {
9029: len = LEN(p);
9030: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
9031: printf("+");
9032: mpz_out_str(asir_out,10,BDY(CZ(m)));
9033: printf("*");
9034: ndl_print(DL(m));
9035: }
9036: printf("\n");
9037: }
9038: }
9039:
9040: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
9041: {
9042: VL tv,fv,vv,vc,av;
9043: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
9044: int m,nocheck,nvar,mindex,e,max;
9045: NDV c;
9046: NMV a;
9047: P p,zp;
9048: Q dmy;
9049: EPOS oepos;
9050: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
9051: Alg alpha,dp;
9052: P poly;
9053: LIST f1,f2,zpl;
9054: Obj obj;
9055: NumberField nf;
9056: struct order_spec *ord1;
9057: struct oEGT eg_check,eg0,eg1;
9058: NODE tr,tl1,tl2,tl3,tl4;
9059: LIST l1,l2,l3,l4,l5;
9060: int *perm;
9061: int j,ret;
9062: NODE retn;
9063: Q jq,bpe;
9064:
9065: nd_module = 0;
9066: parse_nd_option(current_option);
9067: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9068: if ( nd_vc )
9069: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
9070: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9071: switch ( ord->id ) {
9072: case 1:
9073: if ( ord->nv != nvar )
9074: error("nd_f4_lf_trace : invalid order specification");
9075: break;
9076: default:
9077: break;
9078: }
9079:
9080: nd_ntrans = nvar;
9081: nd_nalg = 0;
9082:
9083: nocheck = 0;
9084: mindex = 0;
9085:
9086: /* do not use on-demand load/save */
9087: nd_demand = 0;
9088: m = trace > 1 ? trace : get_lprime(mindex);
9089: nd_init_ord(ord);
9090: mrank = 0;
9091: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9092: for ( tv = vv; tv; tv = NEXT(tv) ) {
9093: if ( nd_module ) {
9094: s = BDY((LIST)BDY(t));
9095: trank = length(s);
9096: mrank = MAX(mrank,trank);
9097: for ( ; s; s = NEXT(s) ) {
9098: e = getdeg(tv->v,(P)BDY(s));
9099: max = MAX(e,max);
9100: }
9101: } else {
9102: e = getdeg(tv->v,(P)BDY(t));
9103: max = MAX(e,max);
9104: }
9105: }
9106: nd_setup_parameters(nvar,max);
9107: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9108: ishomo = 1;
9109: /* XXX */
9110: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9111: if ( nd_module ) {
9112: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9113: } else {
9114: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9115: }
9116: if ( ishomo )
9117: ishomo = ishomo && ndv_ishomo(c);
9118: if ( c ) {
9119: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9120: ndv_mod(-2,c);
9121: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9122: }
9123: }
9124: if ( in0 ) NEXT(in) = 0;
9125: if ( fd0 ) NEXT(fd) = 0;
9126: if ( !ishomo && homo ) {
9127: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9128: c = (NDV)BDY(t); len = LEN(c);
9129: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9130: wmax = MAX(TD(DL(a)),wmax);
9131: }
9132: homogenize_order(ord,nvar,&ord1);
9133: nd_init_ord(ord1);
9134: nd_setup_parameters(nvar+1,wmax);
9135: for ( t = fd0; t; t = NEXT(t) )
9136: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9137: }
9138: if ( MaxDeg > 0 ) nocheck = 1;
9139: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9140: if ( ret )
9141: cand = nd_f4_lf_trace_main(m,&perm);
9142: if ( !ret || !cand ) {
9143: *rp = 0; return;
9144: }
9145: if ( !ishomo && homo ) {
9146: /* dehomogenization */
9147: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9148: nd_init_ord(ord);
9149: nd_setup_parameters(nvar,0);
9150: }
9151: cand = ndv_reducebase(cand,perm);
9152: cand = ndv_reduceall(-2,cand);
9153: cbpe = nd_bpe;
9154: get_eg(&eg0);
9155: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9156: /* gbcheck : cand is a GB of Id(cand) ? */
9157: retn = nd_f4(-2,0,0);
9158: }
9159: if ( !retn ) {
9160: /* failure */
9161: *rp = 0; return;
9162: }
9163: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9164: if ( DP_Print )
1.5 noro 9165: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 9166: /* dp->p */
9167: nd_bpe = cbpe;
9168: nd_setup_parameters(nd_nvar,0);
9169: for ( r = cand; r; r = NEXT(r) ) {
9170: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9171: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9172: }
9173: MKLIST(*rp,cand);
9174: }
9175:
9176: NODE nd_f4_lf_trace_main(int m,int **indp)
9177: {
9178: int i,nh,stat,index;
9179: NODE r,rm,g;
9180: ND_pairs d,l,l0,t;
9181: ND spol,red;
9182: NDV nf,redv,nfqv,nfv;
9183: NM s0,s;
9184: NODE rp0,srp0,nflist,nflist_lf;
9185: int nsp,nred,col,rank,len,k,j,a;
9186: UINT c;
9187: UINT **spmat;
9188: UINT *s0vect,*svect,*p,*v;
9189: int *colstat;
9190: IndArray *imat;
9191: int *rhead;
9192: int spcol,sprow;
9193: int sugar;
9194: PGeoBucket bucket;
9195: struct oEGT eg0,eg1,eg_f4;
9196:
9197: g = 0; d = 0;
9198: for ( i = 0; i < nd_psn; i++ ) {
9199: d = update_pairs(d,g,i,0);
9200: g = update_base(g,i);
9201: }
9202: while ( d ) {
9203: get_eg(&eg0);
9204: l = nd_minsugarp(d,&d);
9205: sugar = SG(l);
9206: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9207: bucket = create_pbucket();
9208: stat = nd_sp_f4(m,0,l,bucket);
9209: if ( !stat ) {
9210: for ( t = l; NEXT(t); t = NEXT(t) );
9211: NEXT(t) = d; d = l;
9212: d = nd_reconstruct(1,d);
9213: continue;
9214: }
9215: if ( bucket->m < 0 ) continue;
9216: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9217: if ( !col ) {
9218: for ( t = l; NEXT(t); t = NEXT(t) );
9219: NEXT(t) = d; d = l;
9220: d = nd_reconstruct(1,d);
9221: continue;
9222: }
9223: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9224: if ( DP_Print )
1.5 noro 9225: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 9226: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9227: if ( !l0 ) continue;
9228: l = l0;
9229:
9230: /* over LF */
9231: bucket = create_pbucket();
9232: stat = nd_sp_f4(-2,1,l,bucket);
9233: if ( !stat ) {
9234: for ( t = l; NEXT(t); t = NEXT(t) );
9235: NEXT(t) = d; d = l;
9236: d = nd_reconstruct(1,d);
9237: continue;
9238: }
9239: if ( bucket->m < 0 ) continue;
9240: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9241: if ( !col ) {
9242: for ( t = l; NEXT(t); t = NEXT(t) );
9243: NEXT(t) = d; d = l;
9244: d = nd_reconstruct(1,d);
9245: continue;
9246: }
9247: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9248: /* adding new bases */
9249: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9250: nfv = (NDV)BDY(rm);
9251: nfqv = (NDV)BDY(r);
9252: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9253: ndv_removecont(m,nfv);
9254: ndv_removecont(-2,nfqv);
9255: nh = ndv_newps(-2,nfv,nfqv,1);
9256: d = update_pairs(d,g,nh,0);
9257: g = update_base(g,nh);
9258: }
9259: if ( r || rm ) return 0;
9260: }
9261: conv_ilist(nd_demand,1,g,indp);
9262: return g;
9263: }
9264:
1.7 noro 9265: #if SIZEOF_LONG==8
9266:
9267: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9268: {
9269: int j,k,len;
9270: UINT *p;
9271: UINT c;
9272: NDV r;
9273: NMV mr0,mr;
9274:
9275: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9276: if ( !len ) return 0;
9277: else {
9278: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9279: #if 0
9280: ndv_alloc += nmv_adv*len;
9281: #endif
9282: mr = mr0;
9283: p = s0vect;
9284: for ( j = k = 0; j < col; j++, p += nd_wpd )
9285: if ( !rhead[j] ) {
9286: if ( (c = (UINT)vect[k++]) != 0 ) {
9287: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9288: }
9289: }
9290: MKNDV(nd_nvar,mr0,len,r);
9291: return r;
9292: }
9293: }
9294:
9295: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9296: {
9297: NM m;
1.11 noro 9298: UINT *t,*s,*u;
9299: int i,st,ed,md,prev,c;
1.7 noro 9300:
9301: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9302: prev = 0;
9303: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9304: t = DL(m);
9305: st = prev;
9306: ed = n;
9307: while ( ed > st ) {
9308: md = (st+ed)/2;
9309: u = s0+md*nd_wpd;
9310: c = DL_COMPARE(u,t);
9311: if ( c == 0 ) break;
9312: else if ( c > 0 ) st = md;
9313: else ed = md;
9314: }
9315: r[md] = (mp_limb_t)CM(m);
9316: prev = md;
1.7 noro 9317: }
9318: for ( i = 0; !r[i]; i++ );
9319: return i;
9320: }
9321:
9322: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9323:
9324: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9325: {
9326: int i,j,k,len,pos,prev;
9327: mp_limb_t a,c,c1,c2;
9328: IndArray ivect;
9329: unsigned char *ivc;
9330: unsigned short *ivs;
9331: unsigned int *ivi;
9332: NDV redv;
9333: NMV mr;
9334: NODE rp;
9335: int maxrs;
9336:
9337: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9338: maxrs = 0;
9339: for ( i = 0; i < nred; i++ ) {
9340: ivect = imat[i];
9341: k = ivect->head;
9342: a = svect[k]; c = cvect[k];
9343: MOD128(a,c,m);
9344: svect[k] = a; cvect[k] = 0;
9345: if ( (c = svect[k]) != 0 ) {
1.11 noro 9346: Nf4_red++;
1.7 noro 9347: maxrs = MAX(maxrs,rp0[i]->sugar);
9348: c = m-c; redv = nd_ps[rp0[i]->index];
9349: len = LEN(redv); mr = BDY(redv);
9350: svect[k] = 0; prev = k;
9351: switch ( ivect->width ) {
9352: case 1:
9353: ivc = ivect->index.c;
9354: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9355: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 9356: c2 = svect[pos]+c1*c;
9357: if ( c2 < svect[pos] ) cvect[pos]++;
9358: svect[pos] = c2;
1.7 noro 9359: }
9360: break;
9361: case 2:
9362: ivs = ivect->index.s;
9363: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9364: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 9365: c2 = svect[pos]+c1*c;
9366: if ( c2 < svect[pos] ) cvect[pos]++;
9367: svect[pos] = c2;
1.7 noro 9368: }
9369: break;
9370: case 4:
9371: ivi = ivect->index.i;
9372: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9373: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 9374: c2 = svect[pos]+c1*c;
9375: if ( c2 < svect[pos] ) cvect[pos]++;
9376: svect[pos] = c2;
1.7 noro 9377: }
9378: break;
9379: }
9380: }
9381: }
9382: for ( i = 0; i < col; i++ ) {
9383: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9384: }
9385: return maxrs;
9386: }
9387:
9388: /* for Fp, 2^15=<p<2^29 */
9389:
9390: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9391: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9392: {
9393: int spcol,sprow,a;
9394: int i,j,k,l,rank;
9395: NODE r0,r;
9396: ND_pairs sp;
9397: ND spol;
9398: mp_limb_t **spmat;
9399: mp_limb_t *svect,*cvect;
9400: mp_limb_t *v;
9401: int *colstat;
9402: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9403: int maxrs;
9404: int *spsugar;
9405: ND_pairs *spactive;
9406:
9407: spcol = col-nred;
9408: get_eg(&eg0);
9409: /* elimination (1st step) */
9410: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9411: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9412: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9413: spsugar = (int *)MALLOC(nsp*sizeof(int));
9414: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9415: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9416: nd_sp(m,0,sp,&spol);
9417: if ( !spol ) continue;
9418: nd_to_vect64(m,s0vect,col,spol,svect);
9419: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9420: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9421: if ( i < col ) {
9422: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9423: for ( j = k = 0; j < col; j++ )
9424: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9425: spsugar[sprow] = MAX(maxrs,SG(spol));
9426: if ( nz )
9427: spactive[sprow] = sp;
9428: sprow++;
9429: }
9430: nd_free(spol);
9431: }
1.12 noro 9432: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9433: if ( DP_Print ) {
9434: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9435: fflush(asir_out);
9436: }
9437: /* free index arrays */
9438: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9439:
9440: /* elimination (2nd step) */
9441: colstat = (int *)MALLOC(spcol*sizeof(int));
9442: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9443: r0 = 0;
9444: for ( i = 0; i < rank; i++ ) {
9445: NEXTNODE(r0,r); BDY(r) =
9446: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9447: SG((NDV)BDY(r)) = spsugar[i];
9448: GCFREE(spmat[i]);
9449: }
9450: if ( r0 ) NEXT(r) = 0;
9451:
9452: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 9453: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9454: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9455: if ( DP_Print ) {
9456: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9457: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9458: nsp,nred,sprow,spcol,rank);
9459: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9460: }
9461: if ( nz ) {
9462: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9463: if ( rank > 0 ) {
9464: NEXT(spactive[rank-1]) = 0;
9465: *nz = spactive[0];
9466: } else
9467: *nz = 0;
9468: }
9469: return r0;
9470: }
9471:
9472: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9473: {
9474: int i,j,k,l,rank,s;
9475: mp_limb_t inv;
9476: mp_limb_t a;
9477: UINT c;
9478: mp_limb_t *t,*pivot,*pk;
9479: UINT *ck;
9480: UINT **cmat;
9481: UINT *ct;
9482: ND_pairs pair;
9483:
9484: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9485: for ( i = 0; i < row; i++ ) {
9486: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9487: bzero(cmat[i],col*sizeof(UINT));
9488: }
9489:
9490: for ( rank = 0, j = 0; j < col; j++ ) {
9491: for ( i = rank; i < row; i++ ) {
9492: a = mat[i][j]; c = cmat[i][j];
9493: MOD128(a,c,md);
9494: mat[i][j] = a; cmat[i][j] = 0;
9495: }
9496: for ( i = rank; i < row; i++ )
9497: if ( mat[i][j] )
9498: break;
9499: if ( i == row ) {
9500: colstat[j] = 0;
9501: continue;
9502: } else
9503: colstat[j] = 1;
9504: if ( i != rank ) {
9505: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9506: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9507: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9508: if ( spactive ) {
9509: pair = spactive[i]; spactive[i] = spactive[rank];
9510: spactive[rank] = pair;
9511: }
9512: }
9513: /* column j is normalized */
9514: s = sugar[rank];
9515: inv = invm((UINT)mat[rank][j],md);
9516: /* normalize pivot row */
9517: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9518: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9519: }
9520: for ( i = rank+1; i < row; i++ ) {
9521: if ( (a = mat[i][j]) != 0 ) {
9522: sugar[i] = MAX(sugar[i],s);
9523: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9524: Nf4_red++;
1.7 noro 9525: }
9526: }
9527: rank++;
9528: }
9529: for ( j = col-1, l = rank-1; j >= 0; j-- )
9530: if ( colstat[j] ) {
9531: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9532: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9533: }
9534: s = sugar[l];
9535: for ( i = 0; i < l; i++ ) {
9536: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9537: if ( a ) {
9538: sugar[i] = MAX(sugar[i],s);
9539: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9540: Nf4_red++;
1.7 noro 9541: }
9542: }
9543: l--;
9544: }
9545: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9546: GCFREE(cmat);
9547: return rank;
9548: }
9549: #endif
9550:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>