Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.27
1.27 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.26 2020/06/23 01:49:58 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.11 noro 5: int Nnd_add,Nf4_red;
1.12 noro 6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 7:
8: int diag_period = 6;
9: int weight_check = 1;
10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 11: /* for general module order */
1.19 noro 12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: int (*dl_base_compare_function)(int nv,DL a,DL b);
14: int nd_base_ordtype;
1.1 noro 15: int nd_dcomp;
16: int nd_rref2;
17: NM _nm_free_list;
18: ND _nd_free_list;
19: ND_pairs _ndp_free_list;
20: NODE nd_hcf;
1.26 noro 21: int Nsyz;
1.1 noro 22:
23: Obj nd_top_weight;
24:
25: static NODE nd_subst;
26: static VL nd_vc;
27: static int nd_ntrans;
28: static int nd_nalg;
29: #if 0
30: static int ndv_alloc;
31: #endif
32: #if 1
33: static int nd_f4_nsp=0x7fffffff;
34: #else
35: static int nd_f4_nsp=50;
36: #endif
37: static double nd_scale=2;
38: static UINT **nd_bound;
39: static struct order_spec *nd_ord;
40: static EPOS nd_epos;
41: static BlockMask nd_blockmask;
42: static int nd_nvar;
43: static int nd_isrlex;
44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
45: static UINT nd_mask[32];
46: static UINT nd_mask0,nd_mask1;
47:
48: static NDV *nd_ps;
49: static NDV *nd_ps_trace;
50: static NDV *nd_ps_sym;
51: static NDV *nd_ps_trace_sym;
52: static RHist *nd_psh;
53: static int nd_psn,nd_pslen;
54: static RHist *nd_red;
55: static int *nd_work_vector;
56: static int **nd_matrix;
57: static int nd_matrix_len;
58: static struct weight_or_block *nd_worb;
59: static int nd_worb_len;
60: static int nd_found,nd_create,nd_notfirst;
61: static int nmv_adv;
62: static int nd_demand;
1.21 noro 63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 64: static int nd_module_rank,nd_poly_weight_len;
65: static int *nd_poly_weight,*nd_module_weight;
66: static NODE nd_tracelist;
67: static NODE nd_alltracelist;
68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
69: static int *nd_gbblock;
70: static NODE nd_nzlist,nd_check_splist;
71: static int nd_splist;
72: static int *nd_sugarweight;
73: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 ! noro 74: static DL *nd_sba_hm;
1.1 noro 75:
76: NumberField get_numberfield();
77: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
78: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
79: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
80: int nd_monic(int m,ND *p);
81: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
82: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
83: NDV pltondv(VL vl,VL dvl,LIST p);
84: void pltozpl(LIST l,Q *cont,LIST *pp);
85: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
86: void nmtodp(int mod,NM m,DP *r);
1.15 noro 87: void ndltodp(UINT *d,DP *r);
1.1 noro 88: NODE reverse_node(NODE n);
89: P ndc_div(int mod,union oNDC a,union oNDC b);
90: P ndctop(int mod,union oNDC c);
91: void finalize_tracelist(int i,P cont);
92: void conv_ilist(int demand,int trace,NODE g,int **indp);
93: void parse_nd_option(NODE opt);
94: void dltondl(int n,DL dl,UINT *r);
95: DP ndvtodp(int mod,NDV p);
96: DP ndtodp(int mod,ND p);
1.16 noro 97: DPM ndvtodpm(int mod,NDV p);
98: NDV dpmtondv(int mod,DPM p);
99: int dpm_getdeg(DPM p,int *rank);
100: void dpm_ptozp(DPM p,Z *cont,DPM *r);
101: int compdmm(int nv,DMM a,DMM b);
1.1 noro 102:
103: void Pdp_set_weight(NODE,VECT *);
104: void Pox_cmo_rpc(NODE,Obj *);
105:
106: ND nd_add_lf(ND p1,ND p2);
107: void nd_mul_c_lf(ND p,Z mul);
108: void ndv_mul_c_lf(NDV p,Z mul);
109: NODE nd_f4_red_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_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
112: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
113: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
114: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
115: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
116: NODE nd_f4_lf_trace_main(int m,int **indp);
117: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
118:
119: extern int lf_lazy;
120: extern Z current_mod_lf;
121:
122: extern int Denominator,DP_Multiple,MaxDeg;
123:
124: #define BLEN (8*sizeof(unsigned long))
125:
126: typedef struct matrix {
127: int row,col;
128: unsigned long **a;
129: } *matrix;
130:
131:
132: void nd_free_private_storage()
133: {
134: _nm_free_list = 0;
135: _ndp_free_list = 0;
136: #if 0
137: GC_gcollect();
138: #endif
139: }
140:
141: void _NM_alloc()
142: {
143: NM p;
144: int i;
145:
146: for ( i = 0; i < 1024; i++ ) {
147: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
148: p->next = _nm_free_list; _nm_free_list = p;
149: }
150: }
151:
152: matrix alloc_matrix(int row,int col)
153: {
154: unsigned long **a;
155: int i,len,blen;
156: matrix mat;
157:
158: mat = (matrix)MALLOC(sizeof(struct matrix));
159: mat->row = row;
160: mat->col = col;
161: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
162: return mat;
163: }
164:
165:
166: void _ND_alloc()
167: {
168: ND p;
169: int i;
170:
171: for ( i = 0; i < 1024; i++ ) {
172: p = (ND)MALLOC(sizeof(struct oND));
173: p->body = (NM)_nd_free_list; _nd_free_list = p;
174: }
175: }
176:
177: void _NDP_alloc()
178: {
179: ND_pairs p;
180: int i;
181:
182: for ( i = 0; i < 1024; i++ ) {
183: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
184: +(nd_wpd-1)*sizeof(UINT));
185: p->next = _ndp_free_list; _ndp_free_list = p;
186: }
187: }
188:
189: INLINE int nd_length(ND p)
190: {
191: NM m;
192: int i;
193:
194: if ( !p )
195: return 0;
196: else {
197: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
198: return i;
199: }
200: }
201:
202: extern int dp_negative_weight;
203:
204: INLINE int ndl_reducible(UINT *d1,UINT *d2)
205: {
206: UINT u1,u2;
207: int i,j;
208:
209: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
210:
211: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
212: #if USE_UNROLL
213: switch ( nd_bpe ) {
214: case 3:
215: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
216: u1 = d1[i]; u2 = d2[i];
217: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
218: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
219: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
220: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
221: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
222: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
223: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
224: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
225: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
226: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
227: }
228: return 1;
229: break;
230: case 4:
231: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
232: u1 = d1[i]; u2 = d2[i];
233: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
234: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
235: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
236: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
237: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
238: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
239: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
240: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
241: }
242: return 1;
243: break;
244: case 6:
245: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
246: u1 = d1[i]; u2 = d2[i];
247: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
248: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
249: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
250: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
251: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
252: }
253: return 1;
254: break;
255: case 8:
256: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
257: u1 = d1[i]; u2 = d2[i];
258: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
259: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
260: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
261: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
262: }
263: return 1;
264: break;
265: case 16:
266: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
267: u1 = d1[i]; u2 = d2[i];
268: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
269: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
270: }
271: return 1;
272: break;
273: case 32:
274: for ( i = nd_exporigin; i < nd_wpd; i++ )
275: if ( d1[i] < d2[i] ) return 0;
276: return 1;
277: break;
278: default:
279: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
280: u1 = d1[i]; u2 = d2[i];
281: for ( j = 0; j < nd_epw; j++ )
282: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
283: }
284: return 1;
285: }
286: #else
287: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
288: u1 = d1[i]; u2 = d2[i];
289: for ( j = 0; j < nd_epw; j++ )
290: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
291: }
292: return 1;
293: #endif
294: }
295:
296: /*
297: * If the current order is a block order,
298: * then the last block is length 1 and contains
299: * the homo variable. Otherwise, the original
300: * order is either 0 or 2.
301: */
302:
303: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
304: {
305: int w,i,e,n,omask0;
306:
307: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
308: n = nd_nvar-1;
309: ndl_zero(r);
310: for ( i = 0; i < n; i++ ) {
311: e = GET_EXP_OLD(d,i);
312: PUT_EXP(r,i,e);
313: }
314: w = TD(d);
315: PUT_EXP(r,nd_nvar-1,weight-w);
316: if ( nd_module ) MPOS(r) = d[ompos];
317: TD(r) = weight;
318: if ( nd_blockmask ) ndl_weight_mask(r);
319: }
320:
321: void ndl_dehomogenize(UINT *d)
322: {
323: UINT mask;
324: UINT h;
325: int i,bits;
326:
327: if ( nd_blockmask ) {
328: h = GET_EXP(d,nd_nvar-1);
329: XOR_EXP(d,nd_nvar-1,h);
330: TD(d) -= h;
331: ndl_weight_mask(d);
332: } else {
333: if ( nd_isrlex ) {
334: if ( nd_bpe == 32 ) {
335: h = d[nd_exporigin];
336: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
337: d[i-1] = d[i];
338: d[i-1] = 0;
339: TD(d) -= h;
340: } else {
341: bits = nd_epw*nd_bpe;
342: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
343: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
344: for ( i = nd_exporigin; i < nd_wpd; i++ )
345: d[i] = ((d[i]<<nd_bpe)&mask)
346: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
347: TD(d) -= h;
348: }
349: } else {
350: h = GET_EXP(d,nd_nvar-1);
351: XOR_EXP(d,nd_nvar-1,h);
352: TD(d) -= h;
353: }
354: }
355: }
356:
357: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
358: {
359: UINT t1,t2,u,u1,u2;
360: int i,j,l;
361:
362: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
363: error("ndl_lcm : inconsistent monomials");
364: #if USE_UNROLL
365: switch ( nd_bpe ) {
366: case 3:
367: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
368: u1 = d1[i]; u2 = d2[i];
369: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
370: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
371: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
372: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
373: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
374: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
375: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
376: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
377: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
378: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
379: d[i] = u;
380: }
381: break;
382: case 4:
383: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
384: u1 = d1[i]; u2 = d2[i];
385: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
386: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
387: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
388: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
389: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
390: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
391: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
392: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
393: d[i] = u;
394: }
395: break;
396: case 6:
397: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
398: u1 = d1[i]; u2 = d2[i];
399: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
400: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
401: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
402: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
403: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
404: d[i] = u;
405: }
406: break;
407: case 8:
408: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
409: u1 = d1[i]; u2 = d2[i];
410: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
411: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
412: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
414: d[i] = u;
415: }
416: break;
417: case 16:
418: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
419: u1 = d1[i]; u2 = d2[i];
420: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
421: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
422: d[i] = u;
423: }
424: break;
425: case 32:
426: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
427: u1 = d1[i]; u2 = d2[i];
428: d[i] = u1>u2?u1:u2;
429: }
430: break;
431: default:
432: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
433: u1 = d1[i]; u2 = d2[i];
434: for ( j = 0, u = 0; j < nd_epw; j++ ) {
435: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
436: }
437: d[i] = u;
438: }
439: break;
440: }
441: #else
442: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
443: u1 = d1[i]; u2 = d2[i];
444: for ( j = 0, u = 0; j < nd_epw; j++ ) {
445: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
446: }
447: d[i] = u;
448: }
449: #endif
450: if ( nd_module ) MPOS(d) = MPOS(d1);
451: TD(d) = ndl_weight(d);
452: if ( nd_blockmask ) ndl_weight_mask(d);
453: }
454:
455: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
456: {
457: UINT t1,t2,u,u1,u2;
458: int i,j,l;
459:
460: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
461: u1 = d1[i]; u2 = d2[i];
462: for ( j = 0, u = 0; j < nd_epw; j++ ) {
463: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
464: }
465: d[i] = u;
466: }
467: }
468:
469: int ndl_weight(UINT *d)
470: {
471: UINT t,u;
472: int i,j;
473:
474: if ( current_dl_weight_vector )
475: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
476: u = GET_EXP(d,i);
477: t += MUL_WEIGHT(u,i);
478: }
479: else
480: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
481: u = d[i];
482: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
483: t += (u&nd_mask0);
484: }
1.20 noro 485: if ( nd_module && nd_module_rank && MPOS(d) )
486: t += nd_module_weight[MPOS(d)-1];
487: for ( i = nd_exporigin; i < nd_wpd; i++ )
488: if ( d[i] && !t )
489: printf("afo\n");
1.1 noro 490: return t;
491: }
492:
493: /* for sugarweight */
494:
495: int ndl_weight2(UINT *d)
496: {
497: int t,u;
498: int i,j;
499:
500: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
501: u = GET_EXP(d,i);
502: t += nd_sugarweight[i]*u;
503: }
1.20 noro 504: if ( nd_module && nd_module_rank && MPOS(d) )
505: t += nd_module_weight[MPOS(d)-1];
1.1 noro 506: return t;
507: }
508:
509: void ndl_weight_mask(UINT *d)
510: {
511: UINT t,u;
512: UINT *mask;
513: int i,j,k,l;
514:
515: l = nd_blockmask->n;
516: for ( k = 0; k < l; k++ ) {
517: mask = nd_blockmask->mask[k];
518: if ( current_dl_weight_vector )
519: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
520: u = GET_EXP_MASK(d,i,mask);
521: t += MUL_WEIGHT(u,i);
522: }
523: else
524: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
525: u = d[i]&mask[i];
526: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
527: t += (u&nd_mask0);
528: }
529: d[k+1] = t;
530: }
531: }
532:
1.21 noro 533: int ndl_glex_compare(UINT *d1,UINT *d2)
534: {
535: if ( TD(d1) > TD(d2) ) return 1;
536: else if ( TD(d1) < TD(d2) ) return -1;
537: else return ndl_lex_compare(d1,d2);
538: }
539:
1.1 noro 540: int ndl_lex_compare(UINT *d1,UINT *d2)
541: {
542: int i;
543:
544: d1 += nd_exporigin;
545: d2 += nd_exporigin;
546: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
547: if ( *d1 > *d2 )
548: return nd_isrlex ? -1 : 1;
549: else if ( *d1 < *d2 )
550: return nd_isrlex ? 1 : -1;
551: return 0;
552: }
553:
554: int ndl_block_compare(UINT *d1,UINT *d2)
555: {
556: int i,l,j,ord_o,ord_l;
557: struct order_pair *op;
558: UINT t1,t2,m;
559: UINT *mask;
560:
561: l = nd_blockmask->n;
562: op = nd_blockmask->order_pair;
563: for ( j = 0; j < l; j++ ) {
564: mask = nd_blockmask->mask[j];
565: ord_o = op[j].order;
566: if ( ord_o < 2 ) {
567: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
568: else if ( t1 < t2 ) return -1;
569: }
570: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
571: m = mask[i];
572: t1 = d1[i]&m;
573: t2 = d2[i]&m;
574: if ( t1 > t2 )
575: return !ord_o ? -1 : 1;
576: else if ( t1 < t2 )
577: return !ord_o ? 1 : -1;
578: }
579: }
580: return 0;
581: }
582:
583: int ndl_matrix_compare(UINT *d1,UINT *d2)
584: {
585: int i,j,s,row;
586: int *v;
587: Z **mat;
588: Z *w;
589: Z t1;
590: Z t,t2;
591:
1.6 noro 592: for ( j = 0; j < nd_nvar; j++ )
593: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 594: if ( nd_top_weight ) {
595: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 596: mat = (Z **)&BDY((VECT)nd_top_weight);
597: row = 1;
1.1 noro 598: } else {
599: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 600: row = ((MAT)nd_top_weight)->row;
1.1 noro 601: }
602: for ( i = 0; i < row; i++ ) {
1.6 noro 603: w = mat[i];
1.1 noro 604: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 605: STOZ(nd_work_vector[j],t1);
1.1 noro 606: mulz(w[j],t1,&t2);
607: addz(t,t2,&t1);
608: t = t1;
609: }
1.6 noro 610: if ( t ) {
611: s = sgnz(t);
1.1 noro 612: if ( s > 0 ) return 1;
613: else if ( s < 0 ) return -1;
1.6 noro 614: }
1.1 noro 615: }
1.6 noro 616: }
617: for ( i = 0; i < nd_matrix_len; i++ ) {
618: v = nd_matrix[i];
619: for ( j = 0, s = 0; j < nd_nvar; j++ )
620: s += v[j]*nd_work_vector[j];
621: if ( s > 0 ) return 1;
622: else if ( s < 0 ) return -1;
623: }
1.1 noro 624: if ( !ndl_equal(d1,d2) )
1.6 noro 625: error("ndl_matrix_compare : invalid matrix");
626: return 0;
1.1 noro 627: }
628:
629: int ndl_composite_compare(UINT *d1,UINT *d2)
630: {
631: int i,j,s,start,end,len,o;
632: int *v;
633: struct sparse_weight *sw;
634:
635: for ( j = 0; j < nd_nvar; j++ )
636: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
637: for ( i = 0; i < nd_worb_len; i++ ) {
638: len = nd_worb[i].length;
639: switch ( nd_worb[i].type ) {
640: case IS_DENSE_WEIGHT:
641: v = nd_worb[i].body.dense_weight;
642: for ( j = 0, s = 0; j < len; j++ )
643: s += v[j]*nd_work_vector[j];
644: if ( s > 0 ) return 1;
645: else if ( s < 0 ) return -1;
646: break;
647: case IS_SPARSE_WEIGHT:
648: sw = nd_worb[i].body.sparse_weight;
649: for ( j = 0, s = 0; j < len; j++ )
650: s += sw[j].value*nd_work_vector[sw[j].pos];
651: if ( s > 0 ) return 1;
652: else if ( s < 0 ) return -1;
653: break;
654: case IS_BLOCK:
655: o = nd_worb[i].body.block.order;
656: start = nd_worb[i].body.block.start;
657: switch ( o ) {
658: case 0:
659: end = start+len;
660: for ( j = start, s = 0; j < end; j++ )
661: s += MUL_WEIGHT(nd_work_vector[j],j);
662: if ( s > 0 ) return 1;
663: else if ( s < 0 ) return -1;
664: for ( j = end-1; j >= start; j-- )
665: if ( nd_work_vector[j] < 0 ) return 1;
666: else if ( nd_work_vector[j] > 0 ) return -1;
667: break;
668: case 1:
669: end = start+len;
670: for ( j = start, s = 0; j < end; j++ )
671: s += MUL_WEIGHT(nd_work_vector[j],j);
672: if ( s > 0 ) return 1;
673: else if ( s < 0 ) return -1;
674: for ( j = start; j < end; j++ )
675: if ( nd_work_vector[j] > 0 ) return 1;
676: else if ( nd_work_vector[j] < 0 ) return -1;
677: break;
678: case 2:
679: end = start+len;
680: for ( j = start; j < end; j++ )
681: if ( nd_work_vector[j] > 0 ) return 1;
682: else if ( nd_work_vector[j] < 0 ) return -1;
683: break;
684: }
685: break;
686: }
687: }
688: return 0;
689: }
690:
691: /* TDH -> WW -> TD-> RL */
692:
693: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
694: {
695: int i,m,e1,e2;
696:
697: if ( TD(d1) > TD(d2) ) return 1;
698: else if ( TD(d1) < TD(d2) ) return -1;
699: m = nd_nvar>>1;
700: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
701: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
702: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
703: }
704: if ( e1 > e2 ) return 1;
705: else if ( e1 < e2 ) return -1;
706: return ndl_lex_compare(d1,d2);
707: }
708:
1.21 noro 709: // common function for module glex and grlex comparison
710: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 711: {
1.21 noro 712: int c;
1.1 noro 713:
1.21 noro 714: switch ( nd_module_ordtype ) {
715: case 0:
716: if ( TD(d1) > TD(d2) ) return 1;
717: else if ( TD(d1) < TD(d2) ) return -1;
718: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
719: else if ( MPOS(d1) < MPOS(d2) ) return 1;
720: else if ( MPOS(d1) > MPOS(d2) ) return -1;
721: else return 0;
722: break;
1.1 noro 723:
1.21 noro 724: case 1:
1.19 noro 725: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
726: if ( TD(d1) > TD(d2) ) return 1;
727: else if ( TD(d1) < TD(d2) ) return -1;
728: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
729: if ( MPOS(d1) < MPOS(d2) ) return 1;
730: else if ( MPOS(d1) > MPOS(d2) ) return -1;
731: }
732: if ( MPOS(d1) < MPOS(d2) ) return 1;
733: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 734: else if ( TD(d1) > TD(d2) ) return 1;
735: else if ( TD(d1) < TD(d2) ) return -1;
736: else return ndl_lex_compare(d1,d2);
737: break;
1.1 noro 738:
1.21 noro 739: case 2: // weight -> POT
740: if ( TD(d1) > TD(d2) ) return 1;
741: else if ( TD(d1) < TD(d2) ) return -1;
742: else if ( MPOS(d1) < MPOS(d2) ) return 1;
743: else if ( MPOS(d1) > MPOS(d2) ) return -1;
744: else return ndl_lex_compare(d1,d2);
745: break;
1.1 noro 746:
1.21 noro 747: default:
748: error("ndl_module_glex_compare : invalid module_ordtype");
749: }
1.1 noro 750: }
751:
1.21 noro 752: // common for module comparison
753: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 754: {
1.21 noro 755: int c;
1.1 noro 756:
1.21 noro 757: switch ( nd_module_ordtype ) {
758: case 0:
1.23 noro 759: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 760: else if ( MPOS(d1) > MPOS(d2) ) return -1;
761: else if ( MPOS(d1) < MPOS(d2) ) return 1;
762: else return 0;
763: break;
1.1 noro 764:
1.21 noro 765: case 1:
766: if ( MPOS(d1) < MPOS(d2) ) return 1;
767: else if ( MPOS(d1) > MPOS(d2) ) return -1;
768: else return (*ndl_base_compare_function)(d1,d2);
769: break;
1.1 noro 770:
1.21 noro 771: case 2: // weight -> POT
772: if ( TD(d1) > TD(d2) ) return 1;
773: else if ( TD(d1) < TD(d2) ) return -1;
774: else if ( MPOS(d1) < MPOS(d2) ) return 1;
775: else if ( MPOS(d1) > MPOS(d2) ) return -1;
776: else return (*ndl_base_compare_function)(d1,d2);
777: break;
1.1 noro 778:
1.21 noro 779: default:
780: error("ndl_module_compare : invalid module_ordtype");
781: }
1.1 noro 782: }
783:
1.21 noro 784: extern DMMstack dmm_stack;
785: void _addtodl(int n,DL d1,DL d2);
786: int _eqdl(int n,DL d1,DL d2);
787:
788: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
789: {
790: int pos1,pos2,t,j;
791: DMM *in;
792: DMMstack s;
793: static DL d1=0;
794: static DL d2=0;
795: static int dlen=0;
796:
797: pos1 = MPOS(m1); pos2 = MPOS(m2);
798: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
799: if ( nd_nvar > dlen ) {
800: NEWDL(d1,nd_nvar);
801: NEWDL(d2,nd_nvar);
802: dlen = nd_nvar;
803: }
804: d1->td = TD(m1);
805: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
806: d2->td = TD(m2);
807: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
808: for ( s = dmm_stack; s; s = NEXT(s) ) {
809: in = s->in;
810: _addtodl(nd_nvar,in[pos1]->dl,d1);
811: _addtodl(nd_nvar,in[pos2]->dl,d2);
812: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
813: if ( pos1 < pos2 ) return 1;
814: else if ( pos1 > pos2 ) return -1;
815: else return 0;
816: }
817: pos1 = in[pos1]->pos;
818: pos2 = in[pos2]->pos;
819: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
820: }
821: // comparison by the bottom order
822: LAST:
823: switch ( nd_base_ordtype ) {
824: case 0:
825: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
826: if ( t ) return t;
827: else if ( pos1 < pos2 ) return 1;
828: else if ( pos1 > pos2 ) return -1;
829: else return 0;
830: break;
831: case 1:
832: if ( pos1 < pos2 ) return 1;
833: else if ( pos1 > pos2 ) return -1;
834: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
835: break;
836: case 2:
837: if ( d1->td > d2->td ) return 1;
838: else if ( d1->td < d2->td ) return -1;
839: else if ( pos1 < pos2 ) return 1;
840: else if ( pos1 > pos2 ) return -1;
841: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
842: break;
843: default:
844: error("ndl_schreyer_compare : invalid base ordtype");
845: }
1.1 noro 846: }
847:
848: INLINE int ndl_equal(UINT *d1,UINT *d2)
849: {
850: int i;
851:
852: switch ( nd_wpd ) {
853: case 2:
854: if ( TD(d2) != TD(d1) ) return 0;
855: if ( d2[1] != d1[1] ) return 0;
856: return 1;
857: break;
858: case 3:
859: if ( TD(d2) != TD(d1) ) return 0;
860: if ( d2[1] != d1[1] ) return 0;
861: if ( d2[2] != d1[2] ) return 0;
862: return 1;
863: break;
864: default:
865: for ( i = 0; i < nd_wpd; i++ )
866: if ( *d1++ != *d2++ ) return 0;
867: return 1;
868: break;
869: }
870: }
871:
872: INLINE void ndl_copy(UINT *d1,UINT *d2)
873: {
874: int i;
875:
876: switch ( nd_wpd ) {
877: case 2:
878: TD(d2) = TD(d1);
879: d2[1] = d1[1];
880: break;
881: case 3:
882: TD(d2) = TD(d1);
883: d2[1] = d1[1];
884: d2[2] = d1[2];
885: break;
886: default:
887: for ( i = 0; i < nd_wpd; i++ )
888: d2[i] = d1[i];
889: break;
890: }
891: }
892:
893: INLINE void ndl_zero(UINT *d)
894: {
895: int i;
896: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
897: }
898:
899: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
900: {
901: int i;
902:
903: if ( nd_module ) {
904: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
905: error("ndl_add : invalid operation");
906: }
907: #if 1
908: switch ( nd_wpd ) {
909: case 2:
910: TD(d) = TD(d1)+TD(d2);
911: d[1] = d1[1]+d2[1];
912: break;
913: case 3:
914: TD(d) = TD(d1)+TD(d2);
915: d[1] = d1[1]+d2[1];
916: d[2] = d1[2]+d2[2];
917: break;
918: default:
919: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
920: break;
921: }
922: #else
923: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
924: #endif
925: }
926:
927: /* d1 += d2 */
928: INLINE void ndl_addto(UINT *d1,UINT *d2)
929: {
930: int i;
931:
932: if ( nd_module ) {
933: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
934: error("ndl_addto : invalid operation");
935: }
936: #if 1
937: switch ( nd_wpd ) {
938: case 2:
939: TD(d1) += TD(d2);
940: d1[1] += d2[1];
941: break;
942: case 3:
943: TD(d1) += TD(d2);
944: d1[1] += d2[1];
945: d1[2] += d2[2];
946: break;
947: default:
948: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
949: break;
950: }
951: #else
952: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
953: #endif
954: }
955:
956: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
957: {
958: int i;
959:
960: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
961: }
962:
963: int ndl_disjoint(UINT *d1,UINT *d2)
964: {
965: UINT t1,t2,u,u1,u2;
966: int i,j;
967:
968: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
969: #if USE_UNROLL
970: switch ( nd_bpe ) {
971: case 3:
972: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
973: u1 = d1[i]; u2 = d2[i];
974: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
975: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
976: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
977: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
978: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
979: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
980: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
981: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
982: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
983: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
984: }
985: return 1;
986: break;
987: case 4:
988: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
989: u1 = d1[i]; u2 = d2[i];
990: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
991: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
992: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
993: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
994: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
995: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
996: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
997: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
998: }
999: return 1;
1000: break;
1001: case 6:
1002: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1003: u1 = d1[i]; u2 = d2[i];
1004: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1005: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1007: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1009: }
1010: return 1;
1011: break;
1012: case 8:
1013: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1014: u1 = d1[i]; u2 = d2[i];
1015: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1017: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1018: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1019: }
1020: return 1;
1021: break;
1022: case 16:
1023: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1024: u1 = d1[i]; u2 = d2[i];
1025: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1027: }
1028: return 1;
1029: break;
1030: case 32:
1031: for ( i = nd_exporigin; i < nd_wpd; i++ )
1032: if ( d1[i] && d2[i] ) return 0;
1033: return 1;
1034: break;
1035: default:
1036: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1037: u1 = d1[i]; u2 = d2[i];
1038: for ( j = 0; j < nd_epw; j++ ) {
1039: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1040: u1 >>= nd_bpe; u2 >>= nd_bpe;
1041: }
1042: }
1043: return 1;
1044: break;
1045: }
1046: #else
1047: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1048: u1 = d1[i]; u2 = d2[i];
1049: for ( j = 0; j < nd_epw; j++ ) {
1050: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1051: u1 >>= nd_bpe; u2 >>= nd_bpe;
1052: }
1053: }
1054: return 1;
1055: #endif
1056: }
1057:
1058: int ndl_check_bound(UINT *d1,UINT *d2)
1059: {
1060: UINT u2;
1061: int i,j,ind,k;
1062:
1063: ind = 0;
1064: #if USE_UNROLL
1065: switch ( nd_bpe ) {
1066: case 3:
1067: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1068: u2 = d2[i];
1069: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1070: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1071: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1072: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1073: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1074: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1075: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1076: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1077: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1078: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1079: }
1080: return 0;
1081: break;
1082: case 4:
1083: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1084: u2 = d2[i];
1085: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1086: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1087: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1088: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1089: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1090: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1091: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1092: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1093: }
1094: return 0;
1095: break;
1096: case 6:
1097: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1098: u2 = d2[i];
1099: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1100: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1101: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1102: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1103: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1104: }
1105: return 0;
1106: break;
1107: case 8:
1108: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1109: u2 = d2[i];
1110: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1111: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1112: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1113: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1114: }
1115: return 0;
1116: break;
1117: case 16:
1118: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1119: u2 = d2[i];
1120: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1121: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1122: }
1123: return 0;
1124: break;
1125: case 32:
1126: for ( i = nd_exporigin; i < nd_wpd; i++ )
1127: if ( d1[i]+d2[i]<d1[i] ) return 1;
1128: return 0;
1129: break;
1130: default:
1131: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1132: u2 = d2[i];
1133: k = (nd_epw-1)*nd_bpe;
1134: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1135: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1136: }
1137: return 0;
1138: break;
1139: }
1140: #else
1141: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1142: u2 = d2[i];
1143: k = (nd_epw-1)*nd_bpe;
1144: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1145: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1146: }
1147: return 0;
1148: #endif
1149: }
1150:
1151: int ndl_check_bound2(int index,UINT *d2)
1152: {
1153: return ndl_check_bound(nd_bound[index],d2);
1154: }
1155:
1156: INLINE int ndl_hash_value(UINT *d)
1157: {
1158: int i;
1.11 noro 1159: UINT r;
1.1 noro 1160:
1161: r = 0;
1162: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1163: r = (r*1511+d[i]);
1.11 noro 1164: r %= REDTAB_LEN;
1.1 noro 1165: return r;
1166: }
1167:
1168: INLINE int ndl_find_reducer(UINT *dg)
1169: {
1170: RHist r;
1171: int d,k,i;
1172:
1173: d = ndl_hash_value(dg);
1174: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1175: if ( ndl_equal(dg,DL(r)) ) {
1176: if ( k > 0 ) nd_notfirst++;
1177: nd_found++;
1178: return r->index;
1179: }
1180: }
1181: if ( Reverse )
1182: for ( i = nd_psn-1; i >= 0; i-- ) {
1183: r = nd_psh[i];
1184: if ( ndl_reducible(dg,DL(r)) ) {
1185: nd_create++;
1186: nd_append_red(dg,i);
1187: return i;
1188: }
1189: }
1190: else
1191: for ( i = 0; i < nd_psn; i++ ) {
1192: r = nd_psh[i];
1193: if ( ndl_reducible(dg,DL(r)) ) {
1194: nd_create++;
1195: nd_append_red(dg,i);
1196: return i;
1197: }
1198: }
1199: return -1;
1200: }
1201:
1.24 noro 1202: // ret=0,...,nd_psn-1 => reducer found
1203: // ret=nd_psn => reducer not found
1204: // ret=-1 => singular top reducible
1205:
1206: int comp_sig(SIG s1,SIG s2);
1207: void _ndltodl(UINT *ndl,DL dl);
1208:
1209: void print_sig(SIG s)
1210: {
1211: int i;
1212:
1213: fprintf(asir_out,"<<");
1214: for ( i = 0; i < nd_nvar; i++ ) {
1215: fprintf(asir_out,"%d",s->dl->d[i]);
1216: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1217: }
1218: fprintf(asir_out,">>*e%d",s->pos);
1219: }
1220:
1221: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1222: {
1223: RHist r;
1.27 ! noro 1224: int i,singular,ret,d,k;
1.26 noro 1225: static int wpd,nvar;
1.24 noro 1226: static SIG quo;
1227: static UINT *tmp;
1228:
1.26 noro 1229: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1230: if ( wpd != nd_wpd ) {
1.24 noro 1231: wpd = nd_wpd;
1232: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1233: }
1.27 ! noro 1234: d = ndl_hash_value(dg);
! 1235: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
! 1236: if ( ndl_equal(dg,DL(r)) ) {
! 1237: return r->index;
! 1238: }
! 1239: }
1.24 noro 1240: singular = 0;
1241: for ( i = 0; i < nd_psn; i++ ) {
1242: r = nd_psh[i];
1243: if ( ndl_reducible(dg,DL(r)) ) {
1.27 ! noro 1244: ndl_sub(dg,DL(r),tmp);
1.24 noro 1245: _ndltodl(tmp,DL(quo));
1246: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1247: quo->pos = nd_psh[i]->sig->pos;
1248: ret = comp_sig(sig,quo);
1249: if ( ret > 0 ) { singular = 0; break; }
1250: if ( ret == 0 ) { singular = 1; }
1251: }
1252: }
1253: if ( singular ) return -1;
1.27 ! noro 1254: else if ( i < nd_psn )
! 1255: nd_append_red(dg,i);
! 1256: return i;
1.24 noro 1257: }
1258:
1.1 noro 1259: ND nd_merge(ND p1,ND p2)
1260: {
1261: int n,c;
1262: int t,can,td1,td2;
1263: ND r;
1264: NM m1,m2,mr0,mr,s;
1265:
1266: if ( !p1 ) return p2;
1267: else if ( !p2 ) return p1;
1268: else {
1269: can = 0;
1270: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1271: c = DL_COMPARE(DL(m1),DL(m2));
1272: switch ( c ) {
1273: case 0:
1274: s = m1; m1 = NEXT(m1);
1275: can++; NEXTNM2(mr0,mr,s);
1276: s = m2; m2 = NEXT(m2); FREENM(s);
1277: break;
1278: case 1:
1279: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1280: break;
1281: case -1:
1282: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1283: break;
1284: }
1285: }
1286: if ( !mr0 )
1287: if ( m1 ) mr0 = m1;
1288: else if ( m2 ) mr0 = m2;
1289: else return 0;
1290: else if ( m1 ) NEXT(mr) = m1;
1291: else if ( m2 ) NEXT(mr) = m2;
1292: else NEXT(mr) = 0;
1293: BDY(p1) = mr0;
1294: SG(p1) = MAX(SG(p1),SG(p2));
1295: LEN(p1) = LEN(p1)+LEN(p2)-can;
1296: FREEND(p2);
1297: return p1;
1298: }
1299: }
1300:
1301: ND nd_add(int mod,ND p1,ND p2)
1302: {
1303: int n,c;
1304: int t,can,td1,td2;
1305: ND r;
1306: NM m1,m2,mr0,mr,s;
1307:
1.11 noro 1308: Nnd_add++;
1.1 noro 1309: if ( !p1 ) return p2;
1310: else if ( !p2 ) return p1;
1311: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1312: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1313: else if ( !mod ) return nd_add_q(p1,p2);
1314: else {
1315: can = 0;
1316: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1317: c = DL_COMPARE(DL(m1),DL(m2));
1318: switch ( c ) {
1319: case 0:
1320: t = ((CM(m1))+(CM(m2))) - mod;
1321: if ( t < 0 ) t += mod;
1322: s = m1; m1 = NEXT(m1);
1323: if ( t ) {
1324: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1325: } else {
1326: can += 2; FREENM(s);
1327: }
1328: s = m2; m2 = NEXT(m2); FREENM(s);
1329: break;
1330: case 1:
1331: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1332: break;
1333: case -1:
1334: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1335: break;
1336: }
1337: }
1338: if ( !mr0 )
1339: if ( m1 ) mr0 = m1;
1340: else if ( m2 ) mr0 = m2;
1341: else return 0;
1342: else if ( m1 ) NEXT(mr) = m1;
1343: else if ( m2 ) NEXT(mr) = m2;
1344: else NEXT(mr) = 0;
1345: BDY(p1) = mr0;
1346: SG(p1) = MAX(SG(p1),SG(p2));
1347: LEN(p1) = LEN(p1)+LEN(p2)-can;
1348: FREEND(p2);
1349: return p1;
1350: }
1351: }
1352:
1353: /* XXX on opteron, the inlined manipulation of destructive additon of
1354: * two NM seems to make gcc optimizer get confused, so the part is
1355: * done in a function.
1356: */
1357:
1358: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1359: {
1360: NM s;
1361: P t;
1362: int can;
1363:
1364: addp(nd_vc,CP(*m1),CP(*m2),&t);
1365: s = *m1; *m1 = NEXT(*m1);
1366: if ( t ) {
1367: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1368: } else {
1369: can = 2; FREENM(s);
1370: }
1371: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1372: return can;
1373: }
1374:
1375: ND nd_add_q(ND p1,ND p2)
1376: {
1377: int n,c,can;
1378: ND r;
1379: NM m1,m2,mr0,mr,s;
1380: P t;
1381:
1382: if ( !p1 ) return p2;
1383: else if ( !p2 ) return p1;
1384: else {
1385: can = 0;
1386: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1387: c = DL_COMPARE(DL(m1),DL(m2));
1388: switch ( c ) {
1389: case 0:
1390: #if defined(__x86_64__)
1391: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1392: #else
1393: addp(nd_vc,CP(m1),CP(m2),&t);
1394: s = m1; m1 = NEXT(m1);
1395: if ( t ) {
1396: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1397: } else {
1398: can += 2; FREENM(s);
1399: }
1400: s = m2; m2 = NEXT(m2); FREENM(s);
1401: #endif
1402: break;
1403: case 1:
1404: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1405: break;
1406: case -1:
1407: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1408: break;
1409: }
1410: }
1411: if ( !mr0 )
1412: if ( m1 ) mr0 = m1;
1413: else if ( m2 ) mr0 = m2;
1414: else return 0;
1415: else if ( m1 ) NEXT(mr) = m1;
1416: else if ( m2 ) NEXT(mr) = m2;
1417: else NEXT(mr) = 0;
1418: BDY(p1) = mr0;
1419: SG(p1) = MAX(SG(p1),SG(p2));
1420: LEN(p1) = LEN(p1)+LEN(p2)-can;
1421: FREEND(p2);
1422: return p1;
1423: }
1424: }
1425:
1426: ND nd_add_sf(ND p1,ND p2)
1427: {
1428: int n,c,can;
1429: ND r;
1430: NM m1,m2,mr0,mr,s;
1431: int t;
1432:
1433: if ( !p1 ) return p2;
1434: else if ( !p2 ) return p1;
1435: else {
1436: can = 0;
1437: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1438: c = DL_COMPARE(DL(m1),DL(m2));
1439: switch ( c ) {
1440: case 0:
1441: t = _addsf(CM(m1),CM(m2));
1442: s = m1; m1 = NEXT(m1);
1443: if ( t ) {
1444: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1445: } else {
1446: can += 2; FREENM(s);
1447: }
1448: s = m2; m2 = NEXT(m2); FREENM(s);
1449: break;
1450: case 1:
1451: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1452: break;
1453: case -1:
1454: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1455: break;
1456: }
1457: }
1458: if ( !mr0 )
1459: if ( m1 ) mr0 = m1;
1460: else if ( m2 ) mr0 = m2;
1461: else return 0;
1462: else if ( m1 ) NEXT(mr) = m1;
1463: else if ( m2 ) NEXT(mr) = m2;
1464: else NEXT(mr) = 0;
1465: BDY(p1) = mr0;
1466: SG(p1) = MAX(SG(p1),SG(p2));
1467: LEN(p1) = LEN(p1)+LEN(p2)-can;
1468: FREEND(p2);
1469: return p1;
1470: }
1471: }
1472:
1473:
1474: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1475: {
1476: int c,c1,c2;
1477: Z cg,cred,gcd,tq;
1478: P cgp,credp,gcdp;
1479: Obj tr,tr1;
1480:
1481: if ( mod == -1 ) {
1482: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1483: *divp = (Obj)ONE;
1484: } else if ( mod == -2 ) {
1485: Z inv,t;
1486: divlf(ONE,HCZ(p),&inv);
1487: chsgnlf(HCZ(g),&t);
1488: mullf(inv,t,&CZ(mul));
1489: *divp = (Obj)ONE;
1490: } else if ( mod ) {
1491: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1492: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1493: *divp = (Obj)ONE;
1494: } else if ( nd_vc ) {
1495: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1496: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1497: chsgnp(cgp,&CP(mul));
1498: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1499: if ( dn ) {
1500: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1501: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1502: }
1503: *divp = (Obj)credp;
1504: } else {
1.6 noro 1505: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1506: chsgnz(cg,&CZ(mul));
1.1 noro 1507: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1508: if ( dn ) {
1509: mulz(dn->z,cred,&tq); dn->z = tq;
1510: }
1511: *divp = (Obj)cred;
1512: }
1513: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1514: }
1515:
1516: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1517: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1518: {
1519: NM m,mrd,tail;
1520: NM mul;
1521: int n,sugar,psugar,sugar0,stat,index;
1522: int c,c1,c2,dummy;
1523: RHist h;
1524: NDV p,red;
1525: Q cg,cred,gcd,tq,qq;
1526: Z iq;
1527: DP dmul;
1528: NODE node;
1529: LIST hist;
1530: double hmag;
1531: P tp,tp1;
1532: Obj tr,tr1,div;
1533: union oNDC hg;
1534: P cont;
1535:
1536: if ( !g ) {
1537: *rp = d;
1538: return 1;
1539: }
1540: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1541:
1542: sugar0 = sugar = SG(g);
1543: n = NV(g);
1544: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1545: if ( d )
1546: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1547: for ( ; g; ) {
1548: index = ndl_find_reducer(HDL(g));
1549: if ( index >= 0 ) {
1550: h = nd_psh[index];
1551: ndl_sub(HDL(g),DL(h),DL(mul));
1552: if ( ndl_check_bound2(index,DL(mul)) ) {
1553: nd_free(g); nd_free(d);
1554: return 0;
1555: }
1556: p = nd_demand ? ndv_load(index) : ps[index];
1557: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1558: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1559: if ( nd_gentrace ) {
1560: /* Trace=[div,index,mul,ONE] */
1.6 noro 1561: STOZ(index,iq);
1.1 noro 1562: nmtodp(mod,mul,&dmul);
1563: node = mknode(4,div,iq,dmul,ONE);
1564: }
1565: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1566: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1567: hg = HCU(g);
1568: nd_removecont2(d,g);
1.6 noro 1569: if ( nd_gentrace ) {
1.1 noro 1570: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1571: /* exact division */
1.1 noro 1572: cont = ndc_div(mod,hg,HCU(g));
1573: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1574: }
1575: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1576: }
1577: MKLIST(hist,node);
1578: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1579: } else if ( !full ) {
1580: *rp = g;
1581: return 1;
1582: } else {
1583: m = BDY(g);
1584: if ( NEXT(m) ) {
1585: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1586: } else {
1587: FREEND(g); g = 0;
1588: }
1589: if ( d ) {
1590: NEXT(tail)=m; tail=m; LEN(d)++;
1591: } else {
1592: MKND(n,m,1,d); tail = BDY(d);
1593: }
1594: }
1595: }
1596: if ( d ) SG(d) = sugar;
1597: *rp = d;
1598: return 1;
1599: }
1600:
1.24 noro 1601: // ret=1 => success
1602: // ret=0 => overflow
1603: // ret=-1 => singular top reducible
1604:
1605: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1606: {
1607: NM m,mrd,tail;
1608: NM mul;
1609: int n,sugar,psugar,sugar0,stat,index;
1610: int c,c1,c2,dummy;
1611: RHist h;
1612: NDV p,red;
1613: Q cg,cred,gcd,tq,qq;
1614: Z iq;
1615: DP dmul;
1616: NODE node;
1617: LIST hist;
1618: double hmag;
1619: P tp,tp1;
1620: Obj tr,tr1,div;
1621: union oNDC hg;
1622: P cont;
1623: SIG sig;
1624:
1625: if ( !g ) {
1626: *rp = d;
1627: return 1;
1628: }
1629: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1630:
1631: sugar0 = sugar = SG(g);
1632: n = NV(g);
1633: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1634: if ( d )
1635: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1636: sig = g->sig;
1637: for ( ; g; ) {
1638: index = ndl_find_reducer_s(HDL(g),sig);
1639: if ( index >= 0 && index < nd_psn ) {
1640: // reducer found
1641: h = nd_psh[index];
1642: ndl_sub(HDL(g),DL(h),DL(mul));
1643: if ( ndl_check_bound2(index,DL(mul)) ) {
1644: nd_free(g); nd_free(d);
1645: return 0;
1646: }
1647: p = ps[index];
1648: /* d+g -> div*(d+g)+mul*p */
1649: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1650: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1651: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1652: hg = HCU(g);
1653: nd_removecont2(d,g);
1654: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1655: }
1656: } else if ( index == -1 ) {
1657: // singular top reducible
1658: return -1;
1659: } else if ( !full ) {
1660: *rp = g;
1661: g->sig = sig;
1662: return 1;
1663: } else {
1664: m = BDY(g);
1665: if ( NEXT(m) ) {
1666: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1667: } else {
1668: FREEND(g); g = 0;
1669: }
1670: if ( d ) {
1671: NEXT(tail)=m; tail=m; LEN(d)++;
1672: } else {
1673: MKND(n,m,1,d); tail = BDY(d);
1674: }
1675: }
1676: }
1677: if ( d ) {
1678: SG(d) = sugar;
1679: d->sig = sig;
1680: }
1681: *rp = d;
1682: return 1;
1683: }
1684:
1.1 noro 1685: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1686: {
1687: int hindex,index;
1688: NDV p;
1689: ND u,d,red;
1690: NODE l;
1691: NM mul,m,mrd,tail;
1692: int sugar,psugar,n,h_reducible;
1693: PGeoBucket bucket;
1694: int c,c1,c2;
1695: Z cg,cred,gcd,zzz;
1696: RHist h;
1697: double hmag,gmag;
1698: int count = 0;
1699: int hcount = 0;
1700:
1701: if ( !g ) {
1702: *rp = 0;
1703: return 1;
1704: }
1705: sugar = SG(g);
1706: n = NV(g);
1.6 noro 1707: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1708: bucket = create_pbucket();
1709: add_pbucket(mod,bucket,g);
1710: d = 0;
1711: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1712: while ( 1 ) {
1713: if ( mod > 0 || mod == -1 )
1714: hindex = head_pbucket(mod,bucket);
1715: else if ( mod == -2 )
1716: hindex = head_pbucket_lf(bucket);
1717: else
1718: hindex = head_pbucket_q(bucket);
1719: if ( hindex < 0 ) {
1720: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1721: if ( d ) SG(d) = sugar;
1722: *rp = d;
1723: return 1;
1724: }
1725: g = bucket->body[hindex];
1726: index = ndl_find_reducer(HDL(g));
1727: if ( index >= 0 ) {
1728: count++;
1729: if ( !d ) hcount++;
1730: h = nd_psh[index];
1731: ndl_sub(HDL(g),DL(h),DL(mul));
1732: if ( ndl_check_bound2(index,DL(mul)) ) {
1733: nd_free(d);
1734: free_pbucket(bucket);
1735: *rp = 0;
1736: return 0;
1737: }
1738: p = ps[index];
1739: if ( mod == -1 )
1740: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1741: else if ( mod == -2 ) {
1742: Z inv,t;
1743: divlf(ONE,HCZ(p),&inv);
1744: chsgnlf(HCZ(g),&t);
1745: mullf(inv,t,&CZ(mul));
1746: } else if ( mod ) {
1747: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1748: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1749: } else {
1.6 noro 1750: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1751: chsgnz(cg,&CZ(mul));
1.1 noro 1752: nd_mul_c_q(d,(P)cred);
1753: mulq_pbucket(bucket,cred);
1754: g = bucket->body[hindex];
1.6 noro 1755: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1756: }
1757: red = ndv_mul_nm(mod,mul,p);
1758: bucket->body[hindex] = nd_remove_head(g);
1759: red = nd_remove_head(red);
1760: add_pbucket(mod,bucket,red);
1761: psugar = SG(p)+TD(DL(mul));
1762: sugar = MAX(sugar,psugar);
1763: if ( !mod && hmag && (gmag > hmag) ) {
1764: g = normalize_pbucket(mod,bucket);
1765: if ( !g ) {
1766: if ( d ) SG(d) = sugar;
1767: *rp = d;
1768: return 1;
1769: }
1770: nd_removecont2(d,g);
1.6 noro 1771: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1772: add_pbucket(mod,bucket,g);
1773: }
1774: } else if ( !full ) {
1775: g = normalize_pbucket(mod,bucket);
1776: if ( g ) SG(g) = sugar;
1777: *rp = g;
1778: return 1;
1779: } else {
1780: m = BDY(g);
1781: if ( NEXT(m) ) {
1782: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1783: } else {
1784: FREEND(g); g = 0;
1785: }
1786: bucket->body[hindex] = g;
1787: NEXT(m) = 0;
1788: if ( d ) {
1789: NEXT(tail)=m; tail=m; LEN(d)++;
1790: } else {
1791: MKND(n,m,1,d); tail = BDY(d);
1792: }
1793: }
1794: }
1795: }
1796:
1.25 noro 1797: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1798: {
1799: int hindex,index;
1800: NDV p;
1801: ND u,d,red;
1802: NODE l;
1803: NM mul,m,mrd,tail;
1804: int sugar,psugar,n,h_reducible;
1805: PGeoBucket bucket;
1806: int c,c1,c2;
1807: Z cg,cred,gcd,zzz;
1808: RHist h;
1809: double hmag,gmag;
1810: int count = 0;
1811: int hcount = 0;
1812: SIG sig;
1813:
1814: if ( !g ) {
1815: *rp = 0;
1816: return 1;
1817: }
1818: sugar = SG(g);
1819: n = NV(g);
1820: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1821: bucket = create_pbucket();
1822: add_pbucket(mod,bucket,g);
1823: d = 0;
1824: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1825: sig = g->sig;
1826: while ( 1 ) {
1827: if ( mod > 0 || mod == -1 )
1828: hindex = head_pbucket(mod,bucket);
1829: else if ( mod == -2 )
1830: hindex = head_pbucket_lf(bucket);
1831: else
1832: hindex = head_pbucket_q(bucket);
1833: if ( hindex < 0 ) {
1834: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1835: if ( d ) {
1836: SG(d) = sugar;
1837: d->sig = sig;
1838: }
1839: *rp = d;
1840: return 1;
1841: }
1842: g = bucket->body[hindex];
1843: index = ndl_find_reducer_s(HDL(g),sig);
1844: if ( index >= 0 && index < nd_psn ) {
1845: count++;
1846: if ( !d ) hcount++;
1847: h = nd_psh[index];
1848: ndl_sub(HDL(g),DL(h),DL(mul));
1849: if ( ndl_check_bound2(index,DL(mul)) ) {
1850: nd_free(d);
1851: free_pbucket(bucket);
1852: *rp = 0;
1853: return 0;
1854: }
1855: p = ps[index];
1856: if ( mod == -1 )
1857: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1858: else if ( mod == -2 ) {
1859: Z inv,t;
1860: divlf(ONE,HCZ(p),&inv);
1861: chsgnlf(HCZ(g),&t);
1862: mullf(inv,t,&CZ(mul));
1863: } else if ( mod ) {
1864: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1865: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1866: } else {
1867: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1868: chsgnz(cg,&CZ(mul));
1869: nd_mul_c_q(d,(P)cred);
1870: mulq_pbucket(bucket,cred);
1871: g = bucket->body[hindex];
1872: gmag = (double)p_mag((P)HCZ(g));
1873: }
1874: red = ndv_mul_nm(mod,mul,p);
1875: bucket->body[hindex] = nd_remove_head(g);
1876: red = nd_remove_head(red);
1877: add_pbucket(mod,bucket,red);
1878: psugar = SG(p)+TD(DL(mul));
1879: sugar = MAX(sugar,psugar);
1880: if ( !mod && hmag && (gmag > hmag) ) {
1881: g = normalize_pbucket(mod,bucket);
1882: if ( !g ) {
1883: if ( d ) {
1884: SG(d) = sugar;
1885: d->sig = sig;
1886: }
1887: *rp = d;
1888: return 1;
1889: }
1890: nd_removecont2(d,g);
1891: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1892: add_pbucket(mod,bucket,g);
1893: }
1894: } else if ( index == -1 ) {
1895: // singular top reducible
1896: return -1;
1897: } else if ( !full ) {
1898: g = normalize_pbucket(mod,bucket);
1899: if ( g ) {
1900: SG(g) = sugar;
1901: g->sig = sig;
1902: }
1903: *rp = g;
1904: return 1;
1905: } else {
1906: m = BDY(g);
1907: if ( NEXT(m) ) {
1908: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1909: } else {
1910: FREEND(g); g = 0;
1911: }
1912: bucket->body[hindex] = g;
1913: NEXT(m) = 0;
1914: if ( d ) {
1915: NEXT(tail)=m; tail=m; LEN(d)++;
1916: } else {
1917: MKND(n,m,1,d); tail = BDY(d);
1918: }
1919: }
1920: }
1921: }
1922:
1.1 noro 1923: /* input : list of NDV, cand : list of NDV */
1924:
1925: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1926: {
1927: int n,i,stat;
1928: ND nf,d;
1929: NDV r;
1930: NODE t,s;
1931: union oNDC dn;
1932: Z q;
1933: LIST list;
1934:
1.24 noro 1935: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1936: n = length(cand);
1937:
1938: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1939: /* membercheck : list is a subset of Id(cand) ? */
1940: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1941: again:
1942: nd_tracelist = 0;
1943: if ( nd_bpe > obpe )
1944: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1945: else
1946: r = (NDV)BDY(t);
1947: #if 0
1948: // moved to nd_f4_lf_trace()
1949: if ( m == -2 ) ndv_mod(m,r);
1950: #endif
1951: d = ndvtond(m,r);
1.6 noro 1952: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1953: if ( !stat ) {
1954: nd_reconstruct(0,0);
1955: goto again;
1956: } else if ( nf ) return 0;
1957: if ( nd_gentrace ) {
1958: nd_tracelist = reverse_node(nd_tracelist);
1959: MKLIST(list,nd_tracelist);
1.6 noro 1960: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1961: MKNODE(s,list,nd_alltracelist);
1962: nd_alltracelist = s; nd_tracelist = 0;
1963: }
1964: if ( DP_Print ) { printf("."); fflush(stdout); }
1965: }
1966: if ( DP_Print ) { printf("\n"); }
1967: return 1;
1968: }
1969:
1970: ND nd_remove_head(ND p)
1971: {
1972: NM m;
1973:
1974: m = BDY(p);
1975: if ( !NEXT(m) ) {
1976: FREEND(p); p = 0;
1977: } else {
1978: BDY(p) = NEXT(m); LEN(p)--;
1979: }
1980: FREENM(m);
1981: return p;
1982: }
1983:
1984: ND nd_separate_head(ND p,ND *head)
1985: {
1986: NM m,m0;
1987: ND r;
1988:
1989: m = BDY(p);
1990: if ( !NEXT(m) ) {
1991: *head = p; p = 0;
1992: } else {
1993: m0 = m;
1994: BDY(p) = NEXT(m); LEN(p)--;
1995: NEXT(m0) = 0;
1996: MKND(NV(p),m0,1,r);
1997: *head = r;
1998: }
1999: return p;
2000: }
2001:
2002: PGeoBucket create_pbucket()
2003: {
2004: PGeoBucket g;
2005:
2006: g = CALLOC(1,sizeof(struct oPGeoBucket));
2007: g->m = -1;
2008: return g;
2009: }
2010:
2011: void free_pbucket(PGeoBucket b) {
2012: int i;
2013:
2014: for ( i = 0; i <= b->m; i++ )
2015: if ( b->body[i] ) {
2016: nd_free(b->body[i]);
2017: b->body[i] = 0;
2018: }
2019: GCFREE(b);
2020: }
2021:
2022: void add_pbucket_symbolic(PGeoBucket g,ND d)
2023: {
2024: int l,i,k,m;
2025:
2026: if ( !d )
2027: return;
2028: l = LEN(d);
2029: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2030: /* 2^(k-1) < l <= 2^k (=m) */
2031: d = nd_merge(g->body[k],d);
2032: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2033: g->body[k] = 0;
2034: d = nd_merge(g->body[k+1],d);
2035: }
2036: g->body[k] = d;
2037: g->m = MAX(g->m,k);
2038: }
2039:
2040: void add_pbucket(int mod,PGeoBucket g,ND d)
2041: {
2042: int l,i,k,m;
2043:
2044: if ( !d )
2045: return;
2046: l = LEN(d);
2047: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2048: /* 2^(k-1) < l <= 2^k (=m) */
2049: d = nd_add(mod,g->body[k],d);
2050: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2051: g->body[k] = 0;
2052: d = nd_add(mod,g->body[k+1],d);
2053: }
2054: g->body[k] = d;
2055: g->m = MAX(g->m,k);
2056: }
2057:
2058: void mulq_pbucket(PGeoBucket g,Z c)
2059: {
2060: int k;
2061:
2062: for ( k = 0; k <= g->m; k++ )
2063: nd_mul_c_q(g->body[k],(P)c);
2064: }
2065:
2066: NM remove_head_pbucket_symbolic(PGeoBucket g)
2067: {
2068: int j,i,k,c;
2069: NM head;
2070:
2071: k = g->m;
2072: j = -1;
2073: for ( i = 0; i <= k; i++ ) {
2074: if ( !g->body[i] ) continue;
2075: if ( j < 0 ) j = i;
2076: else {
2077: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2078: if ( c > 0 )
2079: j = i;
2080: else if ( c == 0 )
2081: g->body[i] = nd_remove_head(g->body[i]);
2082: }
2083: }
2084: if ( j < 0 ) return 0;
2085: else {
2086: head = BDY(g->body[j]);
2087: if ( !NEXT(head) ) {
2088: FREEND(g->body[j]);
2089: g->body[j] = 0;
2090: } else {
2091: BDY(g->body[j]) = NEXT(head);
2092: LEN(g->body[j])--;
2093: }
2094: return head;
2095: }
2096: }
2097:
2098: int head_pbucket(int mod,PGeoBucket g)
2099: {
2100: int j,i,c,k,nv,sum;
2101: UINT *di,*dj;
2102: ND gi,gj;
2103:
2104: k = g->m;
2105: while ( 1 ) {
2106: j = -1;
2107: for ( i = 0; i <= k; i++ ) {
2108: if ( !(gi = g->body[i]) )
2109: continue;
2110: if ( j < 0 ) {
2111: j = i;
2112: gj = g->body[j];
2113: dj = HDL(gj);
2114: sum = HCM(gj);
2115: } else {
2116: c = DL_COMPARE(HDL(gi),dj);
2117: if ( c > 0 ) {
2118: if ( sum ) HCM(gj) = sum;
2119: else g->body[j] = nd_remove_head(gj);
2120: j = i;
2121: gj = g->body[j];
2122: dj = HDL(gj);
2123: sum = HCM(gj);
2124: } else if ( c == 0 ) {
2125: if ( mod == -1 )
2126: sum = _addsf(sum,HCM(gi));
2127: else {
2128: sum = sum+HCM(gi)-mod;
2129: if ( sum < 0 ) sum += mod;
2130: }
2131: g->body[i] = nd_remove_head(gi);
2132: }
2133: }
2134: }
2135: if ( j < 0 ) return -1;
2136: else if ( sum ) {
2137: HCM(gj) = sum;
2138: return j;
2139: } else
2140: g->body[j] = nd_remove_head(gj);
2141: }
2142: }
2143:
2144: int head_pbucket_q(PGeoBucket g)
2145: {
2146: int j,i,c,k,nv;
2147: Z sum,t;
2148: ND gi,gj;
2149:
2150: k = g->m;
2151: while ( 1 ) {
2152: j = -1;
2153: for ( i = 0; i <= k; i++ ) {
2154: if ( !(gi = g->body[i]) ) continue;
2155: if ( j < 0 ) {
2156: j = i;
2157: gj = g->body[j];
1.6 noro 2158: sum = HCZ(gj);
1.1 noro 2159: } else {
2160: nv = NV(gi);
2161: c = DL_COMPARE(HDL(gi),HDL(gj));
2162: if ( c > 0 ) {
1.6 noro 2163: if ( sum ) HCZ(gj) = sum;
1.1 noro 2164: else g->body[j] = nd_remove_head(gj);
2165: j = i;
2166: gj = g->body[j];
1.6 noro 2167: sum = HCZ(gj);
1.1 noro 2168: } else if ( c == 0 ) {
1.6 noro 2169: addz(sum,HCZ(gi),&t);
1.1 noro 2170: sum = t;
2171: g->body[i] = nd_remove_head(gi);
2172: }
2173: }
2174: }
2175: if ( j < 0 ) return -1;
2176: else if ( sum ) {
1.6 noro 2177: HCZ(gj) = sum;
1.1 noro 2178: return j;
2179: } else
2180: g->body[j] = nd_remove_head(gj);
2181: }
2182: }
2183:
2184: int head_pbucket_lf(PGeoBucket g)
2185: {
2186: int j,i,c,k,nv;
2187: Z sum,t;
2188: ND gi,gj;
2189:
2190: k = g->m;
2191: while ( 1 ) {
2192: j = -1;
2193: for ( i = 0; i <= k; i++ ) {
2194: if ( !(gi = g->body[i]) ) continue;
2195: if ( j < 0 ) {
2196: j = i;
2197: gj = g->body[j];
2198: sum = HCZ(gj);
2199: } else {
2200: nv = NV(gi);
2201: c = DL_COMPARE(HDL(gi),HDL(gj));
2202: if ( c > 0 ) {
2203: if ( sum ) HCZ(gj) = sum;
2204: else g->body[j] = nd_remove_head(gj);
2205: j = i;
2206: gj = g->body[j];
2207: sum = HCZ(gj);
2208: } else if ( c == 0 ) {
2209: addlf(sum,HCZ(gi),&t);
2210: sum = t;
2211: g->body[i] = nd_remove_head(gi);
2212: }
2213: }
2214: }
2215: if ( j < 0 ) return -1;
2216: else if ( sum ) {
2217: HCZ(gj) = sum;
2218: return j;
2219: } else
2220: g->body[j] = nd_remove_head(gj);
2221: }
2222: }
2223:
2224: ND normalize_pbucket(int mod,PGeoBucket g)
2225: {
2226: int i;
2227: ND r,t;
2228:
2229: r = 0;
2230: for ( i = 0; i <= g->m; i++ ) {
2231: r = nd_add(mod,r,g->body[i]);
2232: g->body[i] = 0;
2233: }
2234: g->m = -1;
2235: return r;
2236: }
2237:
2238: #if 0
2239: void register_hcf(NDV p)
2240: {
2241: DCP dc,t;
2242: P hc,h;
2243: int c;
2244: NODE l,l1,prev;
2245:
2246: hc = p->body->c.p;
2247: if ( !nd_vc || NUM(hc) ) return;
2248: fctrp(nd_vc,hc,&dc);
2249: for ( t = dc; t; t = NEXT(t) ) {
2250: h = t->c;
2251: if ( NUM(h) ) continue;
2252: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2253: c = compp(nd_vc,h,(P)BDY(l));
2254: if ( c >= 0 ) break;
2255: }
2256: if ( !l || c > 0 ) {
2257: MKNODE(l1,h,l);
2258: if ( !prev )
2259: nd_hcf = l1;
2260: else
2261: NEXT(prev) = l1;
2262: }
2263: }
2264: }
2265: #else
2266: void register_hcf(NDV p)
2267: {
2268: DCP dc,t;
2269: P hc,h,q;
2270: Q dmy;
2271: int c;
2272: NODE l,l1,prev;
2273:
2274: hc = p->body->c.p;
2275: if ( NUM(hc) ) return;
2276: ptozp(hc,1,&dmy,&h);
2277: #if 1
2278: for ( l = nd_hcf; l; l = NEXT(l) ) {
2279: while ( 1 ) {
2280: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2281: else break;
2282: }
2283: }
2284: if ( NUM(h) ) return;
2285: #endif
2286: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2287: c = compp(nd_vc,h,(P)BDY(l));
2288: if ( c >= 0 ) break;
2289: }
2290: if ( !l || c > 0 ) {
2291: MKNODE(l1,h,l);
2292: if ( !prev )
2293: nd_hcf = l1;
2294: else
2295: NEXT(prev) = l1;
2296: }
2297: }
2298: #endif
2299:
2300: int do_diagonalize(int sugar,int m)
2301: {
1.6 noro 2302: int i,nh,stat;
2303: NODE r,g,t;
2304: ND h,nf,s,head;
2305: NDV nfv;
2306: Q q;
2307: P nm,nmp,dn,mnp,dnp,cont,cont1;
2308: union oNDC hc;
2309: NODE node;
2310: LIST l;
2311: Z iq;
1.1 noro 2312:
1.6 noro 2313: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2314: if ( nd_gentrace ) {
2315: /* Trace = [1,index,1,1] */
2316: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2317: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2318: }
2319: if ( nd_demand )
2320: nfv = ndv_load(i);
2321: else
2322: nfv = nd_ps[i];
2323: s = ndvtond(m,nfv);
2324: s = nd_separate_head(s,&head);
2325: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2326: if ( !stat ) return 0;
2327: ndv_free(nfv);
2328: hc = HCU(nf); nd_removecont(m,nf);
2329: /* exact division */
2330: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2331: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2332: nfv = ndtondv(m,nf);
2333: nd_free(nf);
2334: nd_bound[i] = ndv_compute_bound(nfv);
2335: if ( !m ) register_hcf(nfv);
2336: if ( nd_demand ) {
2337: ndv_save(nfv,i);
2338: ndv_free(nfv);
2339: } else
2340: nd_ps[i] = nfv;
2341: }
2342: return 1;
1.1 noro 2343: }
2344:
2345: LIST compute_splist()
2346: {
2347: NODE g,tn0,tn,node;
2348: LIST l0;
2349: ND_pairs d,t;
2350: int i;
2351: Z i1,i2;
2352:
2353: g = 0; d = 0;
2354: for ( i = 0; i < nd_psn; i++ ) {
2355: d = update_pairs(d,g,i,0);
2356: g = update_base(g,i);
2357: }
2358: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2359: NEXTNODE(tn0,tn);
1.6 noro 2360: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2361: node = mknode(2,i1,i2); MKLIST(l0,node);
2362: BDY(tn) = l0;
2363: }
2364: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2365: return l0;
2366: }
2367:
2368: /* return value = 0 => input is not a GB */
2369:
2370: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2371: {
1.6 noro 2372: int i,nh,sugar,stat;
2373: NODE r,g,t;
2374: ND_pairs d;
2375: ND_pairs l;
2376: ND h,nf,s,head,nf1;
2377: NDV nfv;
2378: Z q;
2379: union oNDC dn,hc;
2380: int diag_count = 0;
2381: P cont;
2382: LIST list;
1.27 ! noro 2383: struct oEGT eg1,eg2,eg_update;
1.6 noro 2384:
1.27 ! noro 2385: init_eg(&eg_update);
1.11 noro 2386: Nnd_add = 0;
1.6 noro 2387: g = 0; d = 0;
2388: for ( i = 0; i < nd_psn; i++ ) {
2389: d = update_pairs(d,g,i,gensyz);
2390: g = update_base(g,i);
2391: }
2392: sugar = 0;
2393: while ( d ) {
1.1 noro 2394: again:
1.6 noro 2395: l = nd_minp(d,&d);
2396: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2397: if ( SG(l) != sugar ) {
2398: if ( ishomo ) {
2399: diag_count = 0;
2400: stat = do_diagonalize(sugar,m);
1.1 noro 2401: if ( !stat ) {
1.6 noro 2402: NEXT(l) = d; d = l;
2403: d = nd_reconstruct(0,d);
2404: goto again;
1.1 noro 2405: }
1.6 noro 2406: }
2407: sugar = SG(l);
2408: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2409: }
2410: stat = nd_sp(m,0,l,&h);
2411: if ( !stat ) {
2412: NEXT(l) = d; d = l;
2413: d = nd_reconstruct(0,d);
2414: goto again;
2415: }
1.1 noro 2416: #if USE_GEOBUCKET
1.6 noro 2417: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2418: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2419: #else
1.6 noro 2420: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2421: #endif
1.6 noro 2422: if ( !stat ) {
2423: NEXT(l) = d; d = l;
2424: d = nd_reconstruct(0,d);
2425: goto again;
2426: } else if ( nf ) {
2427: if ( checkonly || gensyz ) return 0;
1.1 noro 2428: if ( nd_newelim ) {
2429: if ( nd_module ) {
2430: if ( MPOS(HDL(nf)) > 1 ) return 0;
2431: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2432: }
1.6 noro 2433: if ( DP_Print ) { printf("+"); fflush(stdout); }
2434: hc = HCU(nf);
2435: nd_removecont(m,nf);
2436: if ( !m && nd_nalg ) {
2437: nd_monic(0,&nf);
2438: nd_removecont(m,nf);
2439: }
2440: if ( nd_gentrace ) {
2441: /* exact division */
1.1 noro 2442: cont = ndc_div(m,hc,HCU(nf));
2443: if ( m || !UNIQ(cont) ) {
1.6 noro 2444: t = mknode(4,NULLP,NULLP,NULLP,cont);
2445: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2446: nd_tracelist = t;
2447: }
2448: }
1.6 noro 2449: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2450: nh = ndv_newps(m,nfv,0);
1.6 noro 2451: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2452: diag_count = 0;
2453: stat = do_diagonalize(sugar,m);
2454: if ( !stat ) {
2455: NEXT(l) = d; d = l;
2456: d = nd_reconstruct(1,d);
2457: goto again;
1.1 noro 2458: }
1.6 noro 2459: }
1.27 ! noro 2460: get_eg(&eg1);
1.6 noro 2461: d = update_pairs(d,g,nh,0);
1.27 ! noro 2462: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2463: g = update_base(g,nh);
2464: FREENDP(l);
2465: } else {
2466: if ( nd_gentrace && gensyz ) {
2467: nd_tracelist = reverse_node(nd_tracelist);
2468: MKLIST(list,nd_tracelist);
2469: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2470: MKNODE(t,list,nd_alltracelist);
2471: nd_alltracelist = t; nd_tracelist = 0;
2472: }
2473: if ( DP_Print ) { printf("."); fflush(stdout); }
2474: FREENDP(l);
2475: }
2476: }
2477: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2478: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.27 ! noro 2479: print_eg("update",&eg_update);
1.1 noro 2480: return g;
2481: }
2482:
1.24 noro 2483: ND_pairs update_pairs_s(ND_pairs d,NODE g,int t,NODE syz);
2484: ND_pairs nd_newpairs_s( NODE g, int t ,NODE syz);
2485:
2486: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2487: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2488:
2489: void _copydl(int n,DL d1,DL d2);
2490: void _subfromdl(int n,DL d1,DL d2);
2491: extern int (*cmpdl)(int n,DL d1,DL d2);
2492:
2493: NODE insert_sig(NODE l,SIG s)
2494: {
2495: int pos;
2496: DL sig;
2497: struct oNODE root;
2498: NODE p,prev,r;
2499: SIG t;
2500:
2501: pos = s->pos; sig = DL(s);
2502: root.next = l; prev = &root;
2503: for ( p = l; p; p = p->next ) {
2504: t = (SIG)p->body;
2505: if ( t->pos == pos ) {
2506: if ( _dl_redble(DL(t),sig,nd_nvar) )
2507: return root.next;
2508: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2509: // remove p
2510: prev->next = p->next;
2511: } else
2512: prev = p;
2513: }
1.27 ! noro 2514: NEWNODE(r); r->body = (pointer)s; r->next = 0;
! 2515: for ( p = &root; p->next; p = p->next );
! 2516: p->next = r;
! 2517: // r->next = root.next;
! 2518: // return r;
! 2519: return root.next;
1.24 noro 2520: }
2521:
2522: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2523: {
2524: struct oND_pairs root;
2525: ND_pairs prev,p;
2526: SIG spsig;
2527:
2528: root.next = d;
2529: prev = &root; p = d;
2530: while ( p ) {
2531: spsig = p->sig;
1.26 noro 2532: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2533: // remove p
2534: prev->next = p->next;
1.26 noro 2535: Nsyz++;
2536: } else
1.24 noro 2537: prev = p;
2538: p = p->next;
2539: }
2540: return (ND_pairs)root.next;
2541: }
2542:
1.26 noro 2543: struct oEGT eg_create,eg_newpairs,eg_merge;
2544:
1.24 noro 2545: NODE nd_sba_buch(int m,int ishomo,int **indp)
2546: {
2547: int i,nh,sugar,stat;
2548: NODE r,g,t;
2549: ND_pairs d;
2550: ND_pairs l;
2551: ND h,nf,s,head,nf1;
2552: NDV nfv;
2553: Z q;
2554: union oNDC dn,hc;
2555: P cont;
2556: LIST list;
2557: SIG sig;
2558: NODE syzlist;
1.26 noro 2559: int Nredundant;
2560: static int wpd,nvar;
2561: static DL lcm,quo,mul;
1.27 ! noro 2562: struct oEGT eg1,eg2,eg_update,eg_remove;
1.24 noro 2563:
1.27 ! noro 2564: init_eg(&eg_remove);
1.24 noro 2565: syzlist = 0;
1.26 noro 2566: Nsyz = 0;
1.24 noro 2567: Nnd_add = 0;
1.26 noro 2568: Nredundant = 0;
1.24 noro 2569: g = 0; d = 0;
2570: for ( i = 0; i < nd_psn; i++ ) {
2571: d = update_pairs_s(d,g,i,0);
2572: g = append_one(g,i);
2573: }
2574: sugar = 0;
1.26 noro 2575: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2576: init_eg(&eg_create);
2577: init_eg(&eg_merge);
1.24 noro 2578: while ( d ) {
2579: again:
1.26 noro 2580: if ( DP_Print ) {
2581: int len;
2582: ND_pairs td;
2583: for ( td = d, len=0; td; td = td->next, len++);
2584: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2585: }
1.24 noro 2586: l = d; d = d->next;
2587: sig = l->sig;
2588: _ndltodl(l->lcm,lcm);
2589: for ( i = 0; i < nd_psn; i++ ) {
2590: if ( sig->pos == nd_psh[i]->sig->pos &&
2591: _dl_redble(DL(nd_psh[i]->sig),DL(sig),nd_nvar) ) {
1.26 noro 2592: _copydl(nd_nvar,DL(sig),quo);
2593: _subfromdl(nd_nvar,DL(nd_psh[i]->sig),quo);
2594: _ndltodl(DL(nd_psh[i]),mul);
2595: _addtodl(nd_nvar,quo,mul);
2596: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
1.24 noro 2597: break;
2598: }
2599: }
2600: if ( i < nd_psn ) {
2601: if ( DP_Print ) fprintf(asir_out,"M");
1.26 noro 2602: Nredundant++;
1.24 noro 2603: continue;
2604: }
2605: if ( SG(l) != sugar ) {
2606: sugar = SG(l);
2607: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2608: }
2609: stat = nd_sp(m,0,l,&h);
2610: if ( !stat ) {
2611: NEXT(l) = d; d = l;
2612: d = nd_reconstruct(0,d);
2613: goto again;
2614: }
1.25 noro 2615: #if USE_GEOBUCKET
1.24 noro 2616: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2617: #else
2618: stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2619: #endif
2620: if ( !stat ) {
2621: NEXT(l) = d; d = l;
2622: d = nd_reconstruct(0,d);
2623: goto again;
2624: } else if ( stat == -1 ) {
2625: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 ! noro 2626: FREENDP(l);
1.24 noro 2627: } else if ( nf ) {
2628: if ( DP_Print ) { printf("+"); fflush(stdout); }
2629: hc = HCU(nf);
2630: nd_removecont(m,nf);
2631: nfv = ndtondv(m,nf); nd_free(nf);
2632: nh = ndv_newps(m,nfv,0);
1.26 noro 2633:
1.24 noro 2634: d = update_pairs_s(d,g,nh,syzlist);
2635: g = append_one(g,nh);
2636: FREENDP(l);
2637: } else {
2638: // syzygy
1.27 ! noro 2639: get_eg(&eg1);
1.24 noro 2640: d = remove_spair_s(d,sig);
1.27 ! noro 2641: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.24 noro 2642: syzlist = insert_sig(syzlist,sig);
2643: if ( DP_Print ) { printf("."); fflush(stdout); }
2644: FREENDP(l);
2645: }
2646: }
2647: conv_ilist(nd_demand,0,g,indp);
1.26 noro 2648: if ( DP_Print ) {
2649: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
2650: fflush(stdout);
2651: print_eg("create",&eg_create);
2652: print_eg("merge",&eg_merge);
1.27 ! noro 2653: print_eg("remove",&eg_remove);
! 2654: printf("\n");
1.26 noro 2655: }
1.24 noro 2656: return g;
2657: }
2658:
1.1 noro 2659: /* splist = [[i1,i2],...] */
2660:
2661: int check_splist(int m,NODE splist)
2662: {
2663: NODE t,p;
2664: ND_pairs d,r,l;
2665: int stat;
2666: ND h,nf;
2667:
2668: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2669: p = BDY((LIST)BDY(t));
1.6 noro 2670: NEXTND_pairs(d,r);
2671: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2672: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2673: SG(r) = TD(LCM(r)); /* XXX */
2674: }
2675: if ( d ) NEXT(r) = 0;
2676:
1.6 noro 2677: while ( d ) {
1.1 noro 2678: again:
1.6 noro 2679: l = nd_minp(d,&d);
2680: stat = nd_sp(m,0,l,&h);
2681: if ( !stat ) {
2682: NEXT(l) = d; d = l;
2683: d = nd_reconstruct(0,d);
2684: goto again;
2685: }
2686: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2687: if ( !stat ) {
2688: NEXT(l) = d; d = l;
2689: d = nd_reconstruct(0,d);
2690: goto again;
2691: } else if ( nf ) return 0;
1.1 noro 2692: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2693: }
1.1 noro 2694: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2695: return 1;
2696: }
2697:
2698: int check_splist_f4(int m,NODE splist)
2699: {
2700: UINT *s0vect;
1.6 noro 2701: PGeoBucket bucket;
1.1 noro 2702: NODE p,rp0,t;
2703: ND_pairs d,r,l,ll;
2704: int col,stat;
2705:
2706: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2707: p = BDY((LIST)BDY(t));
1.6 noro 2708: NEXTND_pairs(d,r);
2709: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2710: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2711: SG(r) = TD(LCM(r)); /* XXX */
2712: }
2713: if ( d ) NEXT(r) = 0;
2714:
1.6 noro 2715: while ( d ) {
2716: l = nd_minsugarp(d,&d);
2717: bucket = create_pbucket();
2718: stat = nd_sp_f4(m,0,l,bucket);
2719: if ( !stat ) {
2720: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2721: NEXT(ll) = d; d = l;
2722: d = nd_reconstruct(0,d);
2723: continue;
2724: }
2725: if ( bucket->m < 0 ) continue;
2726: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2727: if ( !col ) {
2728: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2729: NEXT(ll) = d; d = l;
2730: d = nd_reconstruct(0,d);
2731: continue;
1.1 noro 2732: }
1.6 noro 2733: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2734: }
2735: return 1;
1.1 noro 2736: }
2737:
2738: int do_diagonalize_trace(int sugar,int m)
2739: {
1.6 noro 2740: int i,nh,stat;
2741: NODE r,g,t;
2742: ND h,nf,nfq,s,head;
2743: NDV nfv,nfqv;
2744: Q q,den,num;
2745: union oNDC hc;
2746: NODE node;
2747: LIST l;
2748: Z iq;
2749: P cont,cont1;
1.1 noro 2750:
1.6 noro 2751: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2752: if ( nd_gentrace ) {
2753: /* Trace = [1,index,1,1] */
2754: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2755: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2756: }
2757: /* for nd_ps */
2758: s = ndvtond(m,nd_ps[i]);
2759: s = nd_separate_head(s,&head);
2760: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2761: if ( !stat ) return 0;
2762: nf = nd_add(m,head,nf);
2763: ndv_free(nd_ps[i]);
2764: nd_ps[i] = ndtondv(m,nf);
2765: nd_free(nf);
2766:
2767: /* for nd_ps_trace */
2768: if ( nd_demand )
2769: nfv = ndv_load(i);
2770: else
2771: nfv = nd_ps_trace[i];
2772: s = ndvtond(0,nfv);
2773: s = nd_separate_head(s,&head);
2774: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2775: if ( !stat ) return 0;
2776: ndv_free(nfv);
2777: hc = HCU(nf); nd_removecont(0,nf);
2778: /* exact division */
1.1 noro 2779: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2780: if ( nd_gentrace ) finalize_tracelist(i,cont);
2781: nfv = ndtondv(0,nf);
2782: nd_free(nf);
2783: nd_bound[i] = ndv_compute_bound(nfv);
2784: register_hcf(nfv);
2785: if ( nd_demand ) {
2786: ndv_save(nfv,i);
2787: ndv_free(nfv);
2788: } else
2789: nd_ps_trace[i] = nfv;
2790: }
2791: return 1;
1.1 noro 2792: }
2793:
2794: static struct oEGT eg_invdalg;
2795: struct oEGT eg_le;
2796:
2797: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2798: {
2799: NODE tn;
2800: P p1;
2801:
2802: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2803: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2804: }
2805: *r = p;
2806: }
2807:
2808: NODE nd_gb_trace(int m,int ishomo,int **indp)
2809: {
1.6 noro 2810: int i,nh,sugar,stat;
2811: NODE r,g,t;
2812: ND_pairs d;
2813: ND_pairs l;
2814: ND h,nf,nfq,s,head;
2815: NDV nfv,nfqv;
2816: Z q,den,num;
2817: P hc;
2818: union oNDC dn,hnfq;
2819: struct oEGT eg_monic,egm0,egm1;
2820: int diag_count = 0;
2821: P cont;
2822: LIST list;
2823:
2824: init_eg(&eg_monic);
2825: init_eg(&eg_invdalg);
2826: init_eg(&eg_le);
2827: g = 0; d = 0;
2828: for ( i = 0; i < nd_psn; i++ ) {
2829: d = update_pairs(d,g,i,0);
2830: g = update_base(g,i);
2831: }
2832: sugar = 0;
2833: while ( d ) {
1.1 noro 2834: again:
1.6 noro 2835: l = nd_minp(d,&d);
2836: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2837: if ( SG(l) != sugar ) {
1.1 noro 2838: #if 1
1.6 noro 2839: if ( ishomo ) {
2840: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2841: stat = do_diagonalize_trace(sugar,m);
2842: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2843: diag_count = 0;
1.1 noro 2844: if ( !stat ) {
1.6 noro 2845: NEXT(l) = d; d = l;
2846: d = nd_reconstruct(1,d);
2847: goto again;
1.1 noro 2848: }
1.6 noro 2849: }
2850: #endif
2851: sugar = SG(l);
2852: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2853: }
2854: stat = nd_sp(m,0,l,&h);
2855: if ( !stat ) {
2856: NEXT(l) = d; d = l;
2857: d = nd_reconstruct(1,d);
2858: goto again;
2859: }
1.1 noro 2860: #if USE_GEOBUCKET
1.6 noro 2861: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2862: #else
1.6 noro 2863: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2864: #endif
1.6 noro 2865: if ( !stat ) {
2866: NEXT(l) = d; d = l;
2867: d = nd_reconstruct(1,d);
2868: goto again;
2869: } else if ( nf ) {
2870: if ( nd_demand ) {
2871: nfqv = ndv_load(nd_psn);
2872: nfq = ndvtond(0,nfqv);
2873: } else
2874: nfq = 0;
2875: if ( !nfq ) {
2876: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2877: NEXT(l) = d; d = l;
2878: d = nd_reconstruct(1,d);
2879: goto again;
2880: }
2881: }
2882: if ( nfq ) {
2883: /* m|HC(nfq) => failure */
2884: if ( nd_vc ) {
2885: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2886: } else
2887: q = HCZ(nfq);
2888: if ( !remqi((Q)q,m) ) return 0;
2889:
2890: if ( DP_Print ) { printf("+"); fflush(stdout); }
2891: hnfq = HCU(nfq);
2892: if ( nd_nalg ) {
2893: /* m|DN(HC(nf)^(-1)) => failure */
2894: get_eg(&egm0);
2895: if ( !nd_monic(m,&nfq) ) return 0;
2896: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2897: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2898: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2899: } else {
2900: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2901: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2902: }
2903: if ( nd_gentrace ) {
2904: /* exact division */
2905: cont = ndc_div(0,hnfq,HCU(nfqv));
2906: if ( !UNIQ(cont) ) {
2907: t = mknode(4,NULLP,NULLP,NULLP,cont);
2908: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2909: nd_tracelist = t;
2910: }
2911: }
1.24 noro 2912: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 2913: if ( ishomo && ++diag_count == diag_period ) {
2914: diag_count = 0;
2915: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2916: stat = do_diagonalize_trace(sugar,m);
2917: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2918: if ( !stat ) {
1.1 noro 2919: NEXT(l) = d; d = l;
2920: d = nd_reconstruct(1,d);
2921: goto again;
1.6 noro 2922: }
1.1 noro 2923: }
1.6 noro 2924: d = update_pairs(d,g,nh,0);
2925: g = update_base(g,nh);
2926: } else {
2927: if ( DP_Print ) { printf("*"); fflush(stdout); }
2928: }
2929: } else {
2930: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2931: }
1.6 noro 2932: FREENDP(l);
2933: }
2934: if ( nd_nalg ) {
2935: if ( DP_Print ) {
2936: print_eg("monic",&eg_monic);
2937: print_eg("invdalg",&eg_invdalg);
2938: print_eg("le",&eg_le);
1.1 noro 2939: }
1.6 noro 2940: }
1.1 noro 2941: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2942: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2943: return g;
1.1 noro 2944: }
2945:
2946: int ndv_compare(NDV *p1,NDV *p2)
2947: {
2948: return DL_COMPARE(HDL(*p1),HDL(*p2));
2949: }
2950:
2951: int ndv_compare_rev(NDV *p1,NDV *p2)
2952: {
2953: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2954: }
2955:
2956: int ndvi_compare(NDVI p1,NDVI p2)
2957: {
2958: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2959: }
2960:
2961: int ndvi_compare_rev(NDVI p1,NDVI p2)
2962: {
2963: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2964: }
2965:
2966: NODE ndv_reduceall(int m,NODE f)
2967: {
2968: int i,j,n,stat;
2969: ND nf,g,head;
2970: NODE t,a0,a;
2971: union oNDC dn;
2972: Q q,num,den;
2973: NODE node;
2974: LIST l;
2975: Z iq,jq;
2976: int *perm;
2977: union oNDC hc;
2978: P cont,cont1;
2979:
2980: if ( nd_nora ) return f;
2981: n = length(f);
1.24 noro 2982: ndv_setup(m,0,f,0,1,0);
1.1 noro 2983: perm = (int *)MALLOC(n*sizeof(int));
2984: if ( nd_gentrace ) {
2985: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2986: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2987: }
2988: for ( i = 0; i < n; ) {
2989: if ( nd_gentrace ) {
2990: /* Trace = [1,index,1,1] */
1.6 noro 2991: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2992: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2993: }
2994: g = ndvtond(m,nd_ps[i]);
2995: g = nd_separate_head(g,&head);
1.6 noro 2996: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 2997: if ( !stat )
2998: nd_reconstruct(0,0);
2999: else {
3000: if ( DP_Print ) { printf("."); fflush(stdout); }
3001: ndv_free(nd_ps[i]);
3002: hc = HCU(nf); nd_removecont(m,nf);
3003: if ( nd_gentrace ) {
3004: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3005: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3006: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3007: }
1.6 noro 3008: /* exact division */
1.1 noro 3009: cont = ndc_div(m,hc,HCU(nf));
3010: finalize_tracelist(perm[i],cont);
3011: }
3012: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3013: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3014: i++;
3015: }
3016: }
3017: if ( DP_Print ) { printf("\n"); }
3018: for ( a0 = 0, i = 0; i < n; i++ ) {
3019: NEXTNODE(a0,a);
3020: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3021: else {
3022: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3023: BDY(a) = (pointer)nd_ps[j];
3024: }
3025: }
3026: NEXT(a) = 0;
3027: return a0;
3028: }
3029:
3030: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3031: {
3032: ND_pairs d1,nd,cur,head,prev,remove;
3033:
3034: if ( !g ) return d;
3035: /* for testing */
3036: if ( gensyz && nd_gensyz == 2 ) {
3037: d1 = nd_newpairs(g,t);
3038: if ( !d )
3039: return d1;
3040: else {
3041: nd = d;
3042: while ( NEXT(nd) ) nd = NEXT(nd);
3043: NEXT(nd) = d1;
3044: return d;
3045: }
3046: }
3047: d = crit_B(d,t);
3048: d1 = nd_newpairs(g,t);
3049: d1 = crit_M(d1);
3050: d1 = crit_F(d1);
3051: if ( gensyz || do_weyl )
3052: head = d1;
3053: else {
3054: prev = 0; cur = head = d1;
3055: while ( cur ) {
3056: if ( crit_2( cur->i1,cur->i2 ) ) {
3057: remove = cur;
3058: if ( !prev ) head = cur = NEXT(cur);
3059: else cur = NEXT(prev) = NEXT(cur);
3060: FREENDP(remove);
3061: } else {
3062: prev = cur; cur = NEXT(cur);
3063: }
3064: }
3065: }
3066: if ( !d )
3067: return head;
3068: else {
3069: nd = d;
3070: while ( NEXT(nd) ) nd = NEXT(nd);
3071: NEXT(nd) = head;
3072: return d;
3073: }
3074: }
3075:
1.24 noro 3076: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3077:
3078: ND_pairs update_pairs_s( ND_pairs d, NODE /* of index */ g, int t,NODE syz)
3079: {
3080: ND_pairs d1;
1.26 noro 3081: struct oEGT eg1,eg2,eg3;
1.24 noro 3082:
3083: if ( !g ) return d;
1.26 noro 3084: get_eg(&eg1);
1.24 noro 3085: d1 = nd_newpairs_s(g,t,syz);
1.26 noro 3086: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3087: d = merge_pairs_s(d,d1);
1.26 noro 3088: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3089: return d;
3090: }
1.1 noro 3091:
3092: ND_pairs nd_newpairs( NODE g, int t )
3093: {
3094: NODE h;
3095: UINT *dl;
3096: int ts,s,i,t0,min,max;
3097: ND_pairs r,r0;
3098:
3099: dl = DL(nd_psh[t]);
3100: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3101: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3102: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3103: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3104: continue;
3105: if ( nd_gbblock ) {
3106: t0 = (long)BDY(h);
3107: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3108: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3109: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3110: break;
3111: }
3112: if ( nd_gbblock[i] >= 0 )
3113: continue;
3114: }
3115: NEXTND_pairs(r0,r);
3116: r->i1 = (long)BDY(h);
3117: r->i2 = t;
3118: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3119: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3120: SG(r) = MAX(s,ts) + TD(LCM(r));
3121: /* experimental */
3122: if ( nd_sugarweight )
3123: r->sugar2 = ndl_weight2(r->lcm);
3124: }
3125: if ( r0 ) NEXT(r) = 0;
3126: return r0;
3127: }
3128:
1.24 noro 3129:
3130: int comp_sig(SIG s1,SIG s2)
3131: {
3132: #if 0
3133: if ( s1->pos > s2->pos ) return 1;
3134: else if ( s1->pos < s2->pos ) return -1;
3135: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3136: #else
3137: static DL m1,m2;
3138: static int nvar;
3139: int ret;
3140:
1.26 noro 3141: if ( nvar != nd_nvar ) {
3142: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24 noro 3143: }
1.27 ! noro 3144: // _ndltodl(DL(nd_psh[s1->pos]),m1);
! 3145: // _ndltodl(DL(nd_psh[s2->pos]),m2);
! 3146: _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
! 3147: _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24 noro 3148: _addtodl(nd_nvar,s1->dl,m1);
3149: _addtodl(nd_nvar,s2->dl,m2);
3150: ret = (*cmpdl)(nd_nvar,m1,m2);
3151: if ( ret != 0 ) return ret;
3152: else if ( s1->pos > s2->pos ) return 1;
3153: else if ( s1->pos < s2->pos ) return -1;
3154: else return 0;
3155: #endif
3156: }
3157:
3158: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3159: {
3160: int ret,s1,s2;
3161: RHist p1,p2;
3162: static int wpd;
3163: static UINT *lcm;
3164:
3165: sp->i1 = i1;
3166: sp->i2 = i2;
3167: p1 = nd_psh[i1];
3168: p2 = nd_psh[i2];
3169: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3170: s1 = SG(p1)-TD(DL(p1));
3171: s2 = SG(p2)-TD(DL(p2));
3172: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3173:
1.26 noro 3174: if ( wpd != nd_wpd ) {
1.24 noro 3175: wpd = nd_wpd;
3176: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3177: }
3178: // DL(sig1) <- sp->lcm
3179: // DL(sig1) -= DL(p1)
3180: // DL(sig1) += DL(p1->sig)
1.27 ! noro 3181: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3182: _ndltodl(lcm,DL(sig1));
3183: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3184: sig1->pos = p1->sig->pos;
3185:
3186: // DL(sig2) <- sp->lcm
3187: // DL(sig2) -= DL(p2)
3188: // DL(sig2) += DL(p2->sig)
1.27 ! noro 3189: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3190: _ndltodl(lcm,DL(sig2));
3191: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3192: sig2->pos = p2->sig->pos;
3193:
3194: ret = comp_sig(sig1,sig2);
3195: if ( ret == 0 ) return 0;
3196: else if ( ret > 0 ) sp->sig = sig1;
3197: else sp->sig = sig2;
3198: return 1;
3199: }
3200:
3201: SIG dup_sig(SIG sig)
3202: {
3203: SIG r;
3204:
3205: if ( !sig ) return 0;
3206: else {
3207: NEWSIG(r);
3208: _copydl(nd_nvar,DL(sig),DL(r));
3209: r->pos = sig->pos;
3210: return r;
3211: }
3212: }
3213:
3214: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3215: {
3216: to->i1 = from->i1;
3217: to->i2 = from->i2;
3218: to->sugar = from->sugar;
3219: to->sugar2 = from->sugar2;
3220: ndl_copy(from->lcm,to->lcm);
3221: to->sig = dup_sig(from->sig);
3222: }
3223:
3224: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3225: {
3226: struct oND_pairs root;
3227: ND_pairs q1,q2,r0,r;
3228: int ret;
3229:
3230: r = &root;
3231: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3232: ret = comp_sig(q1->sig,q2->sig);
3233: if ( ret < 0 ) {
3234: r->next = q1; r = q1; q1 = q1->next;
3235: } else if ( ret > 0 ) {
3236: r->next = q2; r = q2; q2 = q2->next;
3237: } else {
3238: ret = DL_COMPARE(q1->lcm,q2->lcm);
3239: if ( ret < 0 ) {
3240: r->next = q1; r = q1; q1 = q1->next;
3241: q2 = q2->next;
3242: } else {
3243: r->next = q2; r = q2; q2 = q2->next;
3244: q1 = q1->next;
3245: }
3246: }
3247: }
3248: if ( q1 ) {
3249: r->next = q1;
3250: } else {
3251: r->next = q2;
3252: }
3253: return root.next;
3254: }
3255:
3256: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3257: {
3258: ND_pairs p,prev;
3259: int ret;
3260:
3261: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3262: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3263: break;
3264: }
3265: if ( ret == 0 ) {
3266: ret = DL_COMPARE(s->lcm,p->lcm);
3267: if ( ret < 0 ) {
3268: // replace p with s
3269: s->next = p->next;
3270: if ( prev == 0 ) {
3271: return s;
3272: } else {
3273: prev->next = s;
3274: return l;
3275: }
3276: } else
3277: return l;
3278: } else {
3279: // insert s between prev and p
3280: s->next = p;
3281: if ( prev == 0 ) {
3282: return s;
3283: } else {
3284: prev->next = s;
3285: return l;
3286: }
3287: }
3288: }
3289:
3290: ND_pairs nd_newpairs_s( NODE g, int t, NODE syz)
3291: {
3292: NODE h,s;
3293: UINT *dl;
1.27 ! noro 3294: int ts,ret,i;
1.24 noro 3295: ND_pairs r,r0,_sp,sp;
3296: SIG _sig1,_sig2,spsig,tsig;
1.26 noro 3297: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3298:
3299: NEWND_pairs(_sp);
3300: NEWSIG(_sig1); NEWSIG(_sig2);
3301: r0 = 0;
1.27 ! noro 3302: for ( i = 0; i < t; i++ ) {
! 3303: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
! 3304: // for ( h = g; h; h = NEXT(h) ) {
! 3305: // ret = _create_spair_s((long)BDY(h),t,_sp,_sig1,_sig2);
1.24 noro 3306: if ( ret ) {
3307: spsig = _sp->sig;
3308: for ( s = syz; s; s = s->next ) {
3309: tsig = (SIG)s->body;
3310: if ( tsig->pos == spsig->pos && _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
3311: break;
3312: }
3313: if ( s == 0 ) {
3314: NEWND_pairs(sp);
3315: dup_ND_pairs(sp,_sp);
3316: r0 = insert_pair_s(r0,sp);
1.26 noro 3317: } else
3318: Nsyz++;
1.24 noro 3319: }
3320: }
3321: return r0;
3322: }
3323:
1.1 noro 3324: /* ipair = [i1,i2],[i1,i2],... */
3325: ND_pairs nd_ipairtospair(NODE ipair)
3326: {
3327: int s1,s2;
3328: NODE tn,t;
3329: ND_pairs r,r0;
3330:
3331: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3332: NEXTND_pairs(r0,r);
3333: tn = BDY((LIST)BDY(t));
1.6 noro 3334: r->i1 = ZTOS((Q)ARG0(tn));
3335: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3336: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3337: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3338: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3339: SG(r) = MAX(s1,s2) + TD(LCM(r));
3340: /* experimental */
3341: if ( nd_sugarweight )
3342: r->sugar2 = ndl_weight2(r->lcm);
3343: }
3344: if ( r0 ) NEXT(r) = 0;
3345: return r0;
3346: }
3347:
3348: /* kokokara */
3349:
3350: ND_pairs crit_B( ND_pairs d, int s )
3351: {
3352: ND_pairs cur,head,prev,remove;
3353: UINT *t,*tl,*lcm;
3354: int td,tdl;
3355:
3356: if ( !d ) return 0;
3357: t = DL(nd_psh[s]);
3358: prev = 0;
3359: head = cur = d;
3360: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3361: while ( cur ) {
3362: tl = cur->lcm;
3363: if ( ndl_reducible(tl,t) ) {
3364: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3365: if ( !ndl_equal(lcm,tl) ) {
3366: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3367: if (!ndl_equal(lcm,tl)) {
3368: remove = cur;
3369: if ( !prev ) {
3370: head = cur = NEXT(cur);
3371: } else {
3372: cur = NEXT(prev) = NEXT(cur);
3373: }
3374: FREENDP(remove);
3375: } else {
3376: prev = cur; cur = NEXT(cur);
3377: }
3378: } else {
3379: prev = cur; cur = NEXT(cur);
3380: }
3381: } else {
3382: prev = cur; cur = NEXT(cur);
3383: }
3384: }
3385: return head;
3386: }
3387:
3388: ND_pairs crit_M( ND_pairs d1 )
3389: {
3390: ND_pairs e,d2,d3,dd,p;
3391: UINT *id,*jd;
3392:
3393: if ( !d1 ) return d1;
3394: for ( dd = 0, e = d1; e; e = d3 ) {
3395: if ( !(d2 = NEXT(e)) ) {
3396: NEXT(e) = dd;
3397: return e;
3398: }
3399: id = LCM(e);
3400: for ( d3 = 0; d2; d2 = p ) {
3401: p = NEXT(d2);
3402: jd = LCM(d2);
3403: if ( ndl_equal(jd,id) )
3404: ;
3405: else if ( TD(jd) > TD(id) )
3406: if ( ndl_reducible(jd,id) ) continue;
3407: else ;
3408: else if ( ndl_reducible(id,jd) ) goto delit;
3409: NEXT(d2) = d3;
3410: d3 = d2;
3411: }
3412: NEXT(e) = dd;
3413: dd = e;
3414: continue;
3415: /**/
3416: delit: NEXT(d2) = d3;
3417: d3 = d2;
3418: for ( ; p; p = d2 ) {
3419: d2 = NEXT(p);
3420: NEXT(p) = d3;
3421: d3 = p;
3422: }
3423: FREENDP(e);
3424: }
3425: return dd;
3426: }
3427:
3428: ND_pairs crit_F( ND_pairs d1 )
3429: {
3430: ND_pairs rest, head,remove;
3431: ND_pairs last, p, r, w;
3432: int s;
3433:
3434: if ( !d1 ) return d1;
3435: for ( head = last = 0, p = d1; NEXT(p); ) {
3436: r = w = equivalent_pairs(p,&rest);
3437: s = SG(r);
3438: w = NEXT(w);
3439: while ( w ) {
3440: if ( crit_2(w->i1,w->i2) ) {
3441: r = w;
3442: w = NEXT(w);
3443: while ( w ) {
3444: remove = w;
3445: w = NEXT(w);
3446: FREENDP(remove);
3447: }
3448: break;
3449: } else if ( SG(w) < s ) {
3450: FREENDP(r);
3451: r = w;
3452: s = SG(r);
3453: w = NEXT(w);
3454: } else {
3455: remove = w;
3456: w = NEXT(w);
3457: FREENDP(remove);
3458: }
3459: }
3460: if ( last ) NEXT(last) = r;
3461: else head = r;
3462: NEXT(last = r) = 0;
3463: p = rest;
3464: if ( !p ) return head;
3465: }
3466: if ( !last ) return p;
3467: NEXT(last) = p;
3468: return head;
3469: }
3470:
3471: int crit_2( int dp1, int dp2 )
3472: {
3473: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3474: }
3475:
3476: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3477: {
3478: ND_pairs w,p,r,s;
3479: UINT *d;
3480:
3481: w = d1;
3482: d = LCM(w);
3483: s = NEXT(w);
3484: NEXT(w) = 0;
3485: for ( r = 0; s; s = p ) {
3486: p = NEXT(s);
3487: if ( ndl_equal(d,LCM(s)) ) {
3488: NEXT(s) = w; w = s;
3489: } else {
3490: NEXT(s) = r; r = s;
3491: }
3492: }
3493: *prest = r;
3494: return w;
3495: }
3496:
3497: NODE update_base(NODE nd,int ndp)
3498: {
3499: UINT *dl, *dln;
3500: NODE last, p, head;
3501:
3502: dl = DL(nd_psh[ndp]);
3503: for ( head = last = 0, p = nd; p; ) {
3504: dln = DL(nd_psh[(long)BDY(p)]);
3505: if ( ndl_reducible( dln, dl ) ) {
3506: p = NEXT(p);
3507: if ( last ) NEXT(last) = p;
3508: } else {
3509: if ( !last ) head = p;
3510: p = NEXT(last = p);
3511: }
3512: }
3513: head = append_one(head,ndp);
3514: return head;
3515: }
3516:
3517: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3518: {
3519: ND_pairs m,ml,p,l;
3520: UINT *lcm;
3521: int s,td,len,tlen,c,c1;
3522:
3523: if ( !(p = NEXT(m = d)) ) {
3524: *prest = p;
3525: NEXT(m) = 0;
3526: return m;
3527: }
3528: if ( !NoSugar ) {
3529: if ( nd_sugarweight ) {
3530: s = m->sugar2;
3531: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3532: if ( (p->sugar2 < s)
3533: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3534: ml = l; m = p; s = m->sugar2;
3535: }
3536: } else {
3537: s = SG(m);
3538: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3539: if ( (SG(p) < s)
3540: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3541: ml = l; m = p; s = SG(m);
3542: }
3543: }
3544: } else {
3545: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3546: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3547: ml = l; m = p; s = SG(m);
3548: }
3549: }
3550: if ( !ml ) *prest = NEXT(m);
3551: else {
3552: NEXT(ml) = NEXT(m);
3553: *prest = d;
3554: }
3555: NEXT(m) = 0;
3556: return m;
3557: }
3558:
3559: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3560: {
3561: int msugar,i;
3562: ND_pairs t,dm0,dm,dr0,dr;
3563:
3564: if ( nd_sugarweight ) {
3565: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3566: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3567: dm0 = 0; dr0 = 0;
3568: for ( i = 0, t = d; t; t = NEXT(t) )
3569: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3570: if ( dm0 ) NEXT(dm) = t;
3571: else dm0 = t;
3572: dm = t;
3573: i++;
3574: } else {
3575: if ( dr0 ) NEXT(dr) = t;
3576: else dr0 = t;
3577: dr = t;
3578: }
3579: } else {
3580: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3581: if ( SG(t) < msugar ) msugar = SG(t);
3582: dm0 = 0; dr0 = 0;
3583: for ( i = 0, t = d; t; t = NEXT(t) )
3584: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3585: if ( dm0 ) NEXT(dm) = t;
3586: else dm0 = t;
3587: dm = t;
3588: i++;
3589: } else {
3590: if ( dr0 ) NEXT(dr) = t;
3591: else dr0 = t;
3592: dr = t;
3593: }
3594: }
3595: NEXT(dm) = 0;
3596: if ( dr0 ) NEXT(dr) = 0;
3597: *prest = dr0;
3598: return dm0;
3599: }
3600:
3601: int nd_tdeg(NDV c)
3602: {
3603: int wmax = 0;
3604: int i,len;
3605: NMV a;
3606:
3607: len = LEN(c);
3608: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3609: wmax = MAX(TD(DL(a)),wmax);
3610: return wmax;
3611: }
3612:
1.24 noro 3613: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3614: {
3615: int len;
3616: RHist r;
3617: NDV b;
3618: NODE tn;
3619: LIST l;
3620: Z iq;
3621:
3622: if ( nd_psn == nd_pslen ) {
3623: nd_pslen *= 2;
3624: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3625: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3626: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3627: nd_bound = (UINT **)
3628: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3629: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3630: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3631: }
3632: NEWRHist(r); nd_psh[nd_psn] = r;
3633: nd_ps[nd_psn] = a;
3634: if ( aq ) {
3635: nd_ps_trace[nd_psn] = aq;
3636: if ( !m ) {
3637: register_hcf(aq);
3638: } else if ( m == -2 ) {
3639: /* do nothing */
3640: } else
3641: error("ndv_newps : invalud modulus");
3642: nd_bound[nd_psn] = ndv_compute_bound(aq);
3643: #if 1
3644: SG(r) = SG(aq);
3645: #else
3646: SG(r) = nd_tdeg(aq);
3647: #endif
3648: ndl_copy(HDL(aq),DL(r));
1.24 noro 3649: r->sig = dup_sig(aq->sig);
1.1 noro 3650: } else {
3651: if ( !m ) register_hcf(a);
3652: nd_bound[nd_psn] = ndv_compute_bound(a);
3653: #if 1
3654: SG(r) = SG(a);
3655: #else
3656: SG(r) = nd_tdeg(a);
3657: #endif
3658: ndl_copy(HDL(a),DL(r));
1.24 noro 3659: r->sig = dup_sig(a->sig);
1.1 noro 3660: }
3661: if ( nd_demand ) {
3662: if ( aq ) {
3663: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3664: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3665: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3666: nd_ps_trace[nd_psn] = 0;
3667: } else {
3668: ndv_save(nd_ps[nd_psn],nd_psn);
3669: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3670: nd_ps[nd_psn] = 0;
3671: }
3672: }
3673: if ( nd_gentrace ) {
3674: /* reverse the tracelist and append it to alltracelist */
3675: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3676: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3677: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3678: }
3679: return nd_psn++;
3680: }
3681:
3682: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3683: /* return 1 if success, 0 if failure (HC(a mod p)) */
3684:
1.24 noro 3685: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3686: {
1.6 noro 3687: int i,j,td,len,max;
3688: NODE s,s0,f0,tn;
3689: UINT *d;
3690: RHist r;
3691: NDVI w;
3692: NDV a,am;
3693: union oNDC hc;
3694: NODE node;
3695: P hcp;
3696: Z iq,jq;
3697: LIST l;
3698:
3699: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3700: /* initialize the tracelist */
3701: nd_tracelist = 0;
3702:
3703: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3704: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3705: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3706: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3707: if ( !dont_sort ) {
3708: /* XXX heuristic */
1.26 noro 3709: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 3710: qsort(w,nd_psn,sizeof(struct oNDVI),
3711: (int (*)(const void *,const void *))ndvi_compare_rev);
3712: else
3713: qsort(w,nd_psn,sizeof(struct oNDVI),
3714: (int (*)(const void *,const void *))ndvi_compare);
3715: }
3716: nd_pslen = 2*nd_psn;
3717: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3718: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3719: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3720: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3721: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3722: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3723: nd_hcf = 0;
1.1 noro 3724:
1.6 noro 3725: if ( trace && nd_vc )
3726: makesubst(nd_vc,&nd_subst);
3727: else
3728: nd_subst = 0;
1.1 noro 3729:
1.6 noro 3730: if ( !nd_red )
3731: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3732: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3733: for ( i = 0; i < nd_psn; i++ ) {
3734: hc = HCU(w[i].p);
3735: if ( trace ) {
3736: if ( mod == -2 ) {
3737: /* over a large finite field */
3738: /* trace = small modulus */
3739: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3740: ndv_mod(-2,a);
3741: if ( !dont_removecont) ndv_removecont(-2,a);
3742: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3743: ndv_mod(trace,am);
3744: if ( DL_COMPARE(HDL(am),HDL(a)) )
3745: return 0;
3746: ndv_removecont(trace,am);
3747: } else {
3748: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3749: if ( !dont_removecont) ndv_removecont(0,a);
3750: register_hcf(a);
3751: am = nd_ps[i] = ndv_dup(mod,a);
3752: ndv_mod(mod,am);
3753: if ( DL_COMPARE(HDL(am),HDL(a)) )
3754: return 0;
3755: ndv_removecont(mod,am);
3756: }
3757: } else {
3758: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3759: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3760: if ( !mod ) register_hcf(a);
1.1 noro 3761: }
1.6 noro 3762: if ( nd_gentrace ) {
3763: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3764: /* exact division */
1.1 noro 3765: if ( !dont_removecont )
1.6 noro 3766: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3767: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3768: }
3769: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3770: nd_bound[i] = ndv_compute_bound(a);
3771: nd_psh[i] = r;
3772: if ( nd_demand ) {
3773: if ( trace ) {
3774: ndv_save(nd_ps_trace[i],i);
3775: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3776: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3777: nd_ps_trace[i] = 0;
3778: } else {
3779: ndv_save(nd_ps[i],i);
3780: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3781: nd_ps[i] = 0;
3782: }
1.1 noro 3783: }
1.6 noro 3784: }
1.24 noro 3785: if ( sba ) {
1.27 ! noro 3786: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 3787: // setup signatures
1.27 ! noro 3788: for ( i = 0; i < nd_psn; i++ ) {
! 3789: SIG sig;
1.24 noro 3790:
1.27 ! noro 3791: NEWSIG(sig); sig->pos = i;
! 3792: nd_ps[i]->sig = sig;
! 3793: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
! 3794: nd_psh[i]->sig = sig;
! 3795: if ( trace ) {
! 3796: nd_ps_trace[i]->sig = sig;
! 3797: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
! 3798: }
! 3799: NEWDL(nd_sba_hm[i],nd_nvar);
! 3800: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
! 3801: }
1.24 noro 3802: }
1.6 noro 3803: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3804: return 1;
1.1 noro 3805: }
3806:
3807: struct order_spec *append_block(struct order_spec *spec,
3808: int nv,int nalg,int ord);
3809:
3810: extern VECT current_dl_weight_vector_obj;
3811: static VECT prev_weight_vector_obj;
3812:
3813: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3814: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3815: {
3816: NODE alist,t,s,r0,r,arg;
3817: VL tv;
3818: P poly;
3819: DP d;
3820: Alg alpha,dp;
3821: DAlg inv,da,hc;
3822: MP m;
3823: int i,nvar,nalg,n;
3824: NumberField nf;
3825: LIST f1,f2;
3826: struct order_spec *current_spec;
3827: VECT obj,obj0;
3828: VECT tmp;
3829:
3830: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3831: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3832:
3833: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3834: NEXTNODE(alist,t); MKV(tv->v,poly);
3835: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3836: tv->v = tv->v->priv;
3837: }
3838: NEXT(t) = 0;
3839:
3840: /* simplification, making polynomials monic */
3841: setfield_dalg(alist);
3842: obj_algtodalg((Obj)f,(Obj *)&f1);
3843: for ( t = BDY(f); t; t = NEXT(t) ) {
3844: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3845: hc = (DAlg)BDY(d)->c;
3846: if ( NID(hc) == N_DA ) {
3847: invdalg(hc,&inv);
3848: for ( m = BDY(d); m; m = NEXT(m) ) {
3849: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3850: }
3851: }
3852: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3853: }
3854: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3855:
3856: /* append alg vars to the var list */
3857: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3858: NEXT(tv) = av;
3859:
3860: /* append a block to ord */
3861: *ord1p = append_block(ord,nvar,nalg,2);
3862:
3863: /* create generator list */
3864: nf = get_numberfield();
3865: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3866: MKAlg(nf->defpoly[i],dp);
3867: MKNODE(s,dp,t); t = s;
3868: }
3869: MKLIST(f1,t);
3870: *alistp = alist;
3871: algobjtorat((Obj)f1,(Obj *)f1p);
3872:
3873: /* creating a new weight vector */
3874: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3875: n = nvar+nalg+1;
3876: MKVECT(obj,n);
3877: if ( obj0 && obj0->len == nvar )
3878: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3879: else
3880: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3881: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3882: BDY(obj)[n-1] = (pointer)ONE;
3883: arg = mknode(1,obj);
3884: Pdp_set_weight(arg,&tmp);
3885: }
3886:
3887: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3888: {
3889: NODE s,t,u0,u;
3890: P p;
3891: VL tv;
3892: Obj obj;
3893: VECT tmp;
3894: NODE arg;
3895:
3896: u0 = 0;
3897: for ( t = r; t; t = NEXT(t) ) {
3898: p = (P)BDY(t);
3899: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3900: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3901: }
3902: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3903: NEXTNODE(u0,u);
3904: BDY(u) = (pointer)p;
3905: }
3906: }
3907: arg = mknode(1,prev_weight_vector_obj);
3908: Pdp_set_weight(arg,&tmp);
3909:
3910: return u0;
3911: }
3912:
3913: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3914: {
3915: VL tv,fv,vv,vc,av;
3916: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3917: int e,max,nvar,i;
3918: NDV b;
3919: int ishomo,nalg,mrank,trank,wmax,len;
3920: NMV a;
3921: Alg alpha,dp;
3922: P p,zp;
3923: Q dmy;
3924: LIST f1,f2,zpl;
3925: Obj obj;
3926: NumberField nf;
3927: struct order_spec *ord1;
3928: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3929: LIST l1,l2,l3,l4,l5;
3930: int j;
3931: Z jq,bpe,last_nonzero;
3932: int *perm;
3933: EPOS oepos;
3934: int obpe,oadv,ompos,cbpe;
1.15 noro 3935: VECT hvect;
1.1 noro 3936:
3937: nd_module = 0;
3938: if ( !m && Demand ) nd_demand = 1;
3939: else nd_demand = 0;
3940: parse_nd_option(current_option);
3941:
3942: if ( DP_Multiple )
3943: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3944: #if 0
3945: ndv_alloc = 0;
3946: #endif
3947: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3948: if ( m && nd_vc )
3949: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3950: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3951: switch ( ord->id ) {
3952: case 1:
3953: if ( ord->nv != nvar )
3954: error("nd_{gr,f4} : invalid order specification");
3955: break;
3956: default:
3957: break;
3958: }
3959: nd_nalg = 0;
3960: av = 0;
3961: if ( !m ) {
3962: get_algtree((Obj)f,&av);
3963: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3964: nd_ntrans = nvar;
3965: nd_nalg = nalg;
3966: /* #i -> t#i */
3967: if ( nalg ) {
3968: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3969: ord = ord1;
3970: f = f1;
3971: }
3972: nvar += nalg;
3973: }
3974: nd_init_ord(ord);
3975: mrank = 0;
3976: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3977: for ( tv = vv; tv; tv = NEXT(tv) ) {
3978: if ( nd_module ) {
1.16 noro 3979: if ( OID(BDY(t)) == O_DPM ) {
3980: e = dpm_getdeg((DPM)BDY(t),&trank);
3981: max = MAX(e,max);
3982: mrank = MAX(mrank,trank);
3983: } else {
3984: s = BDY((LIST)BDY(t));
3985: trank = length(s);
3986: mrank = MAX(mrank,trank);
3987: for ( ; s; s = NEXT(s) ) {
3988: e = getdeg(tv->v,(P)BDY(s));
3989: max = MAX(e,max);
3990: }
1.1 noro 3991: }
3992: } else {
3993: e = getdeg(tv->v,(P)BDY(t));
3994: max = MAX(e,max);
3995: }
3996: }
3997: nd_setup_parameters(nvar,nd_nzlist?0:max);
3998: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3999: ishomo = 1;
4000: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4001: if ( nd_module ) {
1.16 noro 4002: if ( OID(BDY(t)) == O_DPM ) {
4003: Z cont;
4004: DPM zdpm;
4005:
4006: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4007: else zdpm = (DPM)BDY(t);
4008: b = (pointer)dpmtondv(m,zdpm);
4009: } else {
4010: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4011: else zpl = (LIST)BDY(t);
1.1 noro 4012: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4013: }
1.1 noro 4014: } else {
4015: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4016: else zp = (P)BDY(t);
4017: b = (pointer)ptondv(CO,vv,zp);
4018: }
4019: if ( ishomo )
4020: ishomo = ishomo && ndv_ishomo(b);
4021: if ( m ) ndv_mod(m,b);
4022: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4023: }
4024: if ( fd0 ) NEXT(fd) = 0;
4025:
4026: if ( !ishomo && homo ) {
4027: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4028: b = (NDV)BDY(t); len = LEN(b);
4029: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4030: wmax = MAX(TD(DL(a)),wmax);
4031: }
4032: homogenize_order(ord,nvar,&ord1);
4033: nd_init_ord(ord1);
4034: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4035: for ( t = fd0; t; t = NEXT(t) )
4036: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4037: }
4038:
1.24 noro 4039: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4040: if ( nd_gentrace ) {
4041: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4042: }
4043: if ( nd_splist ) {
4044: *rp = compute_splist();
4045: return;
4046: }
4047: if ( nd_check_splist ) {
4048: if ( f4 ) {
4049: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4050: else *rp = 0;
4051: } else {
4052: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4053: else *rp = 0;
4054: }
4055: return;
4056: }
4057: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4058: if ( !x ) {
4059: *rp = 0; return;
4060: }
1.15 noro 4061: if ( nd_gentrace ) {
4062: MKVECT(hvect,nd_psn);
4063: for ( i = 0; i < nd_psn; i++ )
4064: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4065: }
1.1 noro 4066: if ( !ishomo && homo ) {
4067: /* dehomogenization */
4068: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4069: nd_init_ord(ord);
4070: nd_setup_parameters(nvar,0);
4071: }
4072: nd_demand = 0;
4073: if ( nd_module && nd_intersect ) {
4074: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4075: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4076: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4077: }
4078: conv_ilist(nd_demand,0,x,0);
4079: goto FINAL;
4080: }
4081: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4082: x = ndv_reducebase(x,perm);
4083: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4084: x = ndv_reduceall(m,x);
4085: cbpe = nd_bpe;
4086: if ( nd_gentrace && !f4 ) {
4087: tl2 = nd_alltracelist; nd_alltracelist = 0;
4088: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4089: tl3 = nd_alltracelist; nd_alltracelist = 0;
4090: if ( nd_gensyz ) {
4091: nd_gb(m,0,1,1,0);
4092: tl4 = nd_alltracelist; nd_alltracelist = 0;
4093: } else tl4 = 0;
4094: }
4095: nd_bpe = cbpe;
4096: nd_setup_parameters(nd_nvar,0);
4097: FINAL:
4098: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4099: NEXTNODE(r0,r);
4100: if ( nd_module ) {
4101: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4102: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4103: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4104: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4105: }
4106: if ( r0 ) NEXT(r) = 0;
4107: if ( !m && nd_nalg )
4108: r0 = postprocess_algcoef(av,alist,r0);
4109: MKLIST(*rp,r0);
4110: if ( nd_gentrace ) {
4111: if ( f4 ) {
1.6 noro 4112: STOZ(16,bpe);
4113: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4114: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4115: } else {
4116: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4117: tl3 = reverse_node(tl3);
4118: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4119: for ( t = tl2; t; t = NEXT(t) ) {
4120: /* s = [i,[*,j,*,*],...] */
4121: s = BDY((LIST)BDY(t));
1.6 noro 4122: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4123: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4124: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4125: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4126: }
4127: }
4128: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4129: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4130: }
4131: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4132: MKLIST(l5,tl4);
1.6 noro 4133: STOZ(nd_bpe,bpe);
1.15 noro 4134: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4135: }
4136: }
4137: #if 0
4138: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4139: #endif
4140: }
4141:
1.24 noro 4142: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,struct order_spec *ord,LIST *rp)
4143: {
4144: VL tv,fv,vv,vc,av;
4145: NODE fd,fd0,r,r0,t,x,s,xx;
4146: int e,max,nvar,i;
4147: NDV b;
4148: int ishomo,nalg,wmax,len;
4149: NMV a;
4150: P p,zp;
4151: Q dmy;
4152: struct order_spec *ord1;
4153: int j;
4154: int *perm;
4155: EPOS oepos;
4156: int obpe,oadv,ompos,cbpe;
4157:
4158: nd_module = 0;
4159: nd_demand = 0;
4160: parse_nd_option(current_option);
4161:
4162: if ( DP_Multiple )
4163: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4164: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4165: if ( m && nd_vc )
4166: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4167: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4168: switch ( ord->id ) {
4169: case 1:
4170: if ( ord->nv != nvar )
4171: error("nd_sba : invalid order specification");
4172: break;
4173: default:
4174: break;
4175: }
4176: nd_nalg = 0;
4177: nd_init_ord(ord);
4178: // for SIG comparison
4179: initd(ord);
4180: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4181: for ( tv = vv; tv; tv = NEXT(tv) ) {
4182: e = getdeg(tv->v,(P)BDY(t));
4183: max = MAX(e,max);
4184: }
4185: }
1.25 noro 4186: nd_setup_parameters(nvar,max);
1.24 noro 4187: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4188: ishomo = 1;
4189: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4190: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4191: else zp = (P)BDY(t);
4192: b = (pointer)ptondv(CO,vv,zp);
4193: if ( ishomo )
4194: ishomo = ishomo && ndv_ishomo(b);
4195: if ( m ) ndv_mod(m,b);
4196: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4197: }
4198: if ( fd0 ) NEXT(fd) = 0;
4199:
4200: if ( !ishomo && homo ) {
4201: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4202: b = (NDV)BDY(t); len = LEN(b);
4203: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4204: wmax = MAX(TD(DL(a)),wmax);
4205: }
4206: homogenize_order(ord,nvar,&ord1);
4207: nd_init_ord(ord1);
4208: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4209: for ( t = fd0; t; t = NEXT(t) )
4210: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4211: }
4212:
4213: ndv_setup(m,0,fd0,0,0,1);
4214: x = nd_sba_buch(m,ishomo || homo,&perm);
4215: if ( !x ) {
4216: *rp = 0; return;
4217: }
4218: if ( !ishomo && homo ) {
4219: /* dehomogenization */
4220: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4221: nd_init_ord(ord);
4222: nd_setup_parameters(nvar,0);
4223: }
4224: nd_demand = 0;
4225: x = ndv_reducebase(x,perm);
4226: x = ndv_reduceall(m,x);
4227: nd_setup_parameters(nd_nvar,0);
4228: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4229: NEXTNODE(r0,r);
4230: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4231: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4232: }
4233: if ( r0 ) NEXT(r) = 0;
4234: MKLIST(*rp,r0);
4235: }
4236:
1.1 noro 4237: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4238: {
4239: VL tv,fv,vv,vc,av;
4240: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4241: int e,max,nvar,i;
4242: NDV b;
4243: int ishomo,nalg;
4244: Alg alpha,dp;
4245: P p,zp;
4246: Q dmy;
4247: LIST f1,f2;
4248: Obj obj;
4249: NumberField nf;
4250: struct order_spec *ord1;
4251: int *perm;
4252:
4253: parse_nd_option(current_option);
4254: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4255: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4256: switch ( ord->id ) {
4257: case 1:
4258: if ( ord->nv != nvar )
4259: error("nd_check : invalid order specification");
4260: break;
4261: default:
4262: break;
4263: }
4264: nd_nalg = 0;
4265: av = 0;
4266: if ( !m ) {
4267: get_algtree((Obj)f,&av);
4268: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4269: nd_ntrans = nvar;
4270: nd_nalg = nalg;
4271: /* #i -> t#i */
4272: if ( nalg ) {
4273: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4274: ord = ord1;
4275: f = f1;
4276: }
4277: nvar += nalg;
4278: }
4279: nd_init_ord(ord);
4280: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4281: for ( tv = vv; tv; tv = NEXT(tv) ) {
4282: e = getdeg(tv->v,(P)BDY(t));
4283: max = MAX(e,max);
4284: }
4285: nd_setup_parameters(nvar,max);
4286: ishomo = 1;
4287: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4288: ptozp((P)BDY(t),1,&dmy,&zp);
4289: b = (pointer)ptondv(CO,vv,zp);
4290: if ( ishomo )
4291: ishomo = ishomo && ndv_ishomo(b);
4292: if ( m ) ndv_mod(m,b);
4293: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4294: }
4295: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4296: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4297: for ( x = 0, i = 0; i < nd_psn; i++ )
4298: x = update_base(x,i);
4299: if ( do_check ) {
4300: x = nd_gb(m,ishomo,1,0,&perm);
4301: if ( !x ) {
4302: *rp = 0;
4303: return;
4304: }
4305: } else {
4306: #if 0
4307: /* bug ? */
4308: for ( t = x; t; t = NEXT(t) )
4309: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4310: #else
4311: conv_ilist(0,0,x,&perm);
4312: #endif
4313: }
4314: x = ndv_reducebase(x,perm);
4315: x = ndv_reduceall(m,x);
4316: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4317: NEXTNODE(r0,r);
4318: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4319: }
4320: if ( r0 ) NEXT(r) = 0;
4321: if ( !m && nd_nalg )
4322: r0 = postprocess_algcoef(av,alist,r0);
4323: MKLIST(*rp,r0);
4324: }
4325:
4326: NDV recompute_trace(NODE trace,NDV *p,int m);
4327: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4328:
4329: NDV recompute_trace(NODE ti,NDV *p,int mod)
4330: {
4331: int c,c1,c2,i;
4332: NM mul,m,tail;
4333: ND d,r,rm;
4334: NODE sj;
4335: NDV red;
4336: Obj mj;
4337:
4338: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4339: CM(mul) = 1;
4340: tail = 0;
4341: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4342: sj = BDY((LIST)BDY(ti));
4343: if ( ARG0(sj) ) {
1.6 noro 4344: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4345: mj = (Obj)ARG2(sj);
4346: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4347: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4348: rm = ndv_mul_nm(mod,mul,red);
4349: if ( !r ) r = rm;
4350: else {
4351: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4352: if ( d ) {
4353: NEXT(tail) = m; tail = m; LEN(d)++;
4354: } else {
4355: MKND(nd_nvar,m,1,d); tail = BDY(d);
4356: }
4357: }
4358: if ( !m ) return 0; /* failure */
4359: else {
4360: BDY(r) = m;
4361: if ( mod > 0 || mod == -1 ) {
4362: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4363: DMAR(c1,c2,0,mod,c);
4364: nd_mul_c(mod,rm,c);
4365: } else {
4366: Z t,u;
4367:
4368: chsgnlf(HCZ(r),&t);
4369: divlf(t,HCZ(rm),&u);
4370: nd_mul_c_lf(rm,u);
4371: }
4372: r = nd_add(mod,r,rm);
4373: }
4374: }
4375: }
4376: }
4377: if ( tail ) NEXT(tail) = 0;
4378: d = nd_add(mod,d,r);
4379: nd_mul_c(mod,d,invm(HCM(d),mod));
4380: return ndtondv(mod,d);
4381: }
4382:
4383: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4384: {
4385: VL tv,fv,vv,vc,av;
4386: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4387: int e,max,nvar,i;
4388: NDV b;
4389: int ishomo,nalg;
4390: Alg alpha,dp;
4391: P p,zp;
4392: Q dmy;
4393: LIST f1,f2;
4394: Obj obj;
4395: NumberField nf;
4396: struct order_spec *ord1;
4397: NODE permtrace,intred,ind,perm,trace,ti;
4398: int len,n,j;
4399: NDV *db,*pb;
4400:
4401: parse_nd_option(current_option);
4402: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4403: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4404: switch ( ord->id ) {
4405: case 1:
4406: if ( ord->nv != nvar )
4407: error("nd_check : invalid order specification");
4408: break;
4409: default:
4410: break;
4411: }
4412: nd_init_ord(ord);
1.6 noro 4413: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4414: nd_setup_parameters(nvar,0);
4415:
4416: len = length(BDY(f));
4417: db = (NDV *)MALLOC(len*sizeof(NDV *));
4418: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4419: ptozp((P)BDY(t),1,&dmy,&zp);
4420: b = ptondv(CO,vv,zp);
4421: ndv_mod(m,b);
4422: ndv_mul_c(m,b,invm(HCM(b),m));
4423: db[i] = b;
4424: }
4425:
4426: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4427: intred = BDY((LIST)ARG3(BDY(tlist)));
4428: ind = BDY((LIST)ARG4(BDY(tlist)));
4429: perm = BDY((LIST)ARG0(permtrace));
4430: trace = NEXT(permtrace);
4431:
4432: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4433: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4434: if ( j > i ) i = j;
4435: }
4436: n = i+1;
4437: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4438: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4439: ti = BDY((LIST)BDY(t));
1.6 noro 4440: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4441: }
4442: for ( t = trace; t; t = NEXT(t) ) {
4443: ti = BDY((LIST)BDY(t));
1.6 noro 4444: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4445: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4446: if ( DP_Print ) {
4447: fprintf(asir_out,"."); fflush(asir_out);
4448: }
4449: }
4450: for ( t = intred; t; t = NEXT(t) ) {
4451: ti = BDY((LIST)BDY(t));
1.6 noro 4452: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4453: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4454: if ( DP_Print ) {
4455: fprintf(asir_out,"*"); fflush(asir_out);
4456: }
4457: }
4458: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4459: NEXTNODE(r0,r);
1.6 noro 4460: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4461: ndv_mul_c(m,b,invm(HCM(b),m));
4462: #if 0
1.6 noro 4463: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4464: #else
1.6 noro 4465: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4466: #endif
4467: }
4468: if ( r0 ) NEXT(r) = 0;
4469: MKLIST(*rp,r0);
4470: if ( DP_Print ) fprintf(asir_out,"\n");
4471: }
4472:
1.16 noro 4473: 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 4474: {
4475: VL tv,fv,vv,vc,av;
4476: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4477: int m,nocheck,nvar,mindex,e,max;
4478: NDV c;
4479: NMV a;
4480: P p,zp;
4481: Q dmy;
4482: EPOS oepos;
4483: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4484: Alg alpha,dp;
4485: P poly;
4486: LIST f1,f2,zpl;
4487: Obj obj;
4488: NumberField nf;
4489: struct order_spec *ord1;
4490: struct oEGT eg_check,eg0,eg1;
4491: NODE tr,tl1,tl2,tl3,tl4;
4492: LIST l1,l2,l3,l4,l5;
4493: int *perm;
4494: int j,ret;
4495: Z jq,bpe;
1.15 noro 4496: VECT hvect;
1.1 noro 4497:
4498: nd_module = 0;
4499: nd_lf = 0;
4500: parse_nd_option(current_option);
4501: if ( nd_lf ) {
4502: if ( f4 )
4503: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4504: else
4505: error("nd_gr_trace is not implemented yet over a large finite field");
4506: return;
4507: }
4508: if ( DP_Multiple )
4509: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4510:
4511: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4512: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4513: switch ( ord->id ) {
4514: case 1:
4515: if ( ord->nv != nvar )
4516: error("nd_gr_trace : invalid order specification");
4517: break;
4518: default:
4519: break;
4520: }
4521:
4522: get_algtree((Obj)f,&av);
4523: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4524: nd_ntrans = nvar;
4525: nd_nalg = nalg;
4526: /* #i -> t#i */
4527: if ( nalg ) {
4528: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4529: ord = ord1;
4530: f = f1;
4531: }
4532: nvar += nalg;
4533:
4534: nocheck = 0;
4535: mindex = 0;
4536:
4537: if ( Demand ) nd_demand = 1;
4538: else nd_demand = 0;
4539:
4540: /* setup modulus */
4541: if ( trace < 0 ) {
4542: trace = -trace;
4543: nocheck = 1;
4544: }
4545: m = trace > 1 ? trace : get_lprime(mindex);
4546: nd_init_ord(ord);
4547: mrank = 0;
4548: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4549: for ( tv = vv; tv; tv = NEXT(tv) ) {
4550: if ( nd_module ) {
1.16 noro 4551: if ( OID(BDY(t)) == O_DPM ) {
4552: e = dpm_getdeg((DPM)BDY(t),&trank);
4553: max = MAX(e,max);
4554: mrank = MAX(mrank,trank);
4555: } else {
1.1 noro 4556: s = BDY((LIST)BDY(t));
4557: trank = length(s);
4558: mrank = MAX(mrank,trank);
4559: for ( ; s; s = NEXT(s) ) {
4560: e = getdeg(tv->v,(P)BDY(s));
4561: max = MAX(e,max);
4562: }
1.16 noro 4563: }
1.1 noro 4564: } else {
4565: e = getdeg(tv->v,(P)BDY(t));
4566: max = MAX(e,max);
4567: }
4568: }
4569: nd_setup_parameters(nvar,max);
4570: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4571: ishomo = 1;
4572: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4573: if ( nd_module ) {
1.16 noro 4574: if ( OID(BDY(t)) == O_DPM ) {
4575: Z cont;
4576: DPM zdpm;
4577:
1.17 noro 4578: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4579: else zdpm = (DPM)BDY(t);
4580: c = (pointer)dpmtondv(m,zdpm);
4581: } else {
4582: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4583: else zpl = (LIST)BDY(t);
1.1 noro 4584: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4585: }
1.1 noro 4586: } else {
1.16 noro 4587: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4588: else zp = (P)BDY(t);
4589: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4590: }
4591: if ( ishomo )
4592: ishomo = ishomo && ndv_ishomo(c);
4593: if ( c ) {
4594: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4595: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4596: }
4597: }
4598: if ( in0 ) NEXT(in) = 0;
4599: if ( fd0 ) NEXT(fd) = 0;
4600: if ( !ishomo && homo ) {
4601: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4602: c = (NDV)BDY(t); len = LEN(c);
4603: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4604: wmax = MAX(TD(DL(a)),wmax);
4605: }
4606: homogenize_order(ord,nvar,&ord1);
4607: nd_init_ord(ord1);
4608: nd_setup_parameters(nvar+1,wmax);
4609: for ( t = fd0; t; t = NEXT(t) )
4610: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4611: }
4612: if ( MaxDeg > 0 ) nocheck = 1;
4613: while ( 1 ) {
4614: tl1 = tl2 = tl3 = tl4 = 0;
4615: if ( Demand )
4616: nd_demand = 1;
1.24 noro 4617: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4618: if ( nd_gentrace ) {
4619: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4620: }
4621: if ( ret )
4622: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4623: if ( !ret || !cand ) {
4624: /* failure */
4625: if ( trace > 1 ) { *rp = 0; return; }
4626: else m = get_lprime(++mindex);
4627: continue;
4628: }
1.15 noro 4629: if ( nd_gentrace ) {
4630: MKVECT(hvect,nd_psn);
4631: for ( i = 0; i < nd_psn; i++ )
4632: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4633: }
1.1 noro 4634: if ( !ishomo && homo ) {
4635: /* dehomogenization */
4636: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4637: nd_init_ord(ord);
4638: nd_setup_parameters(nvar,0);
4639: }
4640: nd_demand = 0;
4641: cand = ndv_reducebase(cand,perm);
4642: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4643: cand = ndv_reduceall(0,cand);
4644: cbpe = nd_bpe;
4645: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4646: get_eg(&eg0);
4647: if ( nocheck )
4648: break;
4649: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4650: if ( nd_gentrace ) {
4651: tl3 = nd_alltracelist; nd_alltracelist = 0;
4652: } else tl3 = 0;
4653: /* gbcheck : cand is a GB of Id(cand) ? */
4654: if ( nd_vc || nd_gentrace || nd_gensyz )
4655: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4656: else
4657: ret = nd_f4(0,1,0)!=0;
4658: if ( nd_gentrace && nd_gensyz ) {
4659: tl4 = nd_alltracelist; nd_alltracelist = 0;
4660: } else tl4 = 0;
4661: }
4662: if ( ret ) break;
4663: else if ( trace > 1 ) {
4664: /* failure */
4665: *rp = 0; return;
4666: } else {
4667: /* try the next modulus */
4668: m = get_lprime(++mindex);
4669: /* reset the parameters */
4670: if ( !ishomo && homo ) {
4671: nd_init_ord(ord1);
4672: nd_setup_parameters(nvar+1,wmax);
4673: } else {
4674: nd_init_ord(ord);
4675: nd_setup_parameters(nvar,max);
4676: }
4677: }
4678: }
4679: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4680: if ( DP_Print )
1.6 noro 4681: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4682: /* dp->p */
4683: nd_bpe = cbpe;
4684: nd_setup_parameters(nd_nvar,0);
4685: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4686: if ( nd_module ) {
1.17 noro 4687: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4688: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4689: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4690: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4691: }
4692: if ( nd_nalg )
4693: cand = postprocess_algcoef(av,alist,cand);
4694: MKLIST(*rp,cand);
4695: if ( nd_gentrace ) {
4696: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4697: tl3 = reverse_node(tl3);
4698: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4699: for ( t = tl2; t; t = NEXT(t) ) {
4700: /* s = [i,[*,j,*,*],...] */
4701: s = BDY((LIST)BDY(t));
1.6 noro 4702: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4703: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4704: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4705: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4706: }
4707: }
4708: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4709: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4710: }
4711: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4712: MKLIST(l5,tl4);
1.6 noro 4713: STOZ(nd_bpe,bpe);
1.15 noro 4714: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4715: }
4716: }
4717:
4718: /* XXX : module element is not considered */
4719:
4720: void dltondl(int n,DL dl,UINT *r)
4721: {
4722: UINT *d;
4723: int i,j,l,s,ord_l;
4724: struct order_pair *op;
4725:
4726: d = (unsigned int *)dl->d;
4727: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
4728: if ( nd_blockmask ) {
4729: l = nd_blockmask->n;
4730: op = nd_blockmask->order_pair;
4731: for ( j = 0, s = 0; j < l; j++ ) {
4732: ord_l = op[j].length;
4733: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
4734: }
4735: TD(r) = ndl_weight(r);
4736: ndl_weight_mask(r);
4737: } else {
4738: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
4739: TD(r) = ndl_weight(r);
4740: }
4741: }
4742:
4743: DL ndltodl(int n,UINT *ndl)
4744: {
4745: DL dl;
4746: int *d;
4747: int i,j,l,s,ord_l;
4748: struct order_pair *op;
4749:
4750: NEWDL(dl,n);
4751: dl->td = TD(ndl);
4752: d = dl->d;
4753: if ( nd_blockmask ) {
4754: l = nd_blockmask->n;
4755: op = nd_blockmask->order_pair;
4756: for ( j = 0, s = 0; j < l; j++ ) {
4757: ord_l = op[j].length;
4758: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4759: }
4760: } else {
4761: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4762: }
4763: return dl;
4764: }
4765:
1.24 noro 4766: void _ndltodl(UINT *ndl,DL dl)
4767: {
4768: int *d;
4769: int i,j,l,s,ord_l,n;
4770: struct order_pair *op;
4771:
4772: n = nd_nvar;
4773: dl->td = TD(ndl);
4774: d = dl->d;
4775: if ( nd_blockmask ) {
4776: l = nd_blockmask->n;
4777: op = nd_blockmask->order_pair;
4778: for ( j = 0, s = 0; j < l; j++ ) {
4779: ord_l = op[j].length;
4780: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4781: }
4782: } else {
4783: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4784: }
4785: }
4786:
1.1 noro 4787: void nmtodp(int mod,NM m,DP *r)
4788: {
4789: DP dp;
4790: MP mr;
4791:
4792: NEWMP(mr);
4793: mr->dl = ndltodl(nd_nvar,DL(m));
4794: mr->c = (Obj)ndctop(mod,m->c);
4795: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4796: *r = dp;
4797: }
4798:
1.15 noro 4799: void ndltodp(UINT *d,DP *r)
4800: {
4801: DP dp;
4802: MP mr;
4803:
4804: NEWMP(mr);
4805: mr->dl = ndltodl(nd_nvar,d);
4806: mr->c = (Obj)ONE;
4807: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4808: *r = dp;
4809: }
4810:
1.1 noro 4811: void ndl_print(UINT *dl)
4812: {
4813: int n;
4814: int i,j,l,ord_l,s,s0;
4815: struct order_pair *op;
4816:
4817: n = nd_nvar;
4818: printf("<<");
4819: if ( nd_blockmask ) {
4820: l = nd_blockmask->n;
4821: op = nd_blockmask->order_pair;
4822: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4823: ord_l = op[j].length;
4824: for ( i = 0; i < ord_l; i++, s++ )
4825: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4826: }
4827: } else {
4828: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4829: }
4830: printf(">>");
4831: if ( nd_module && MPOS(dl) )
4832: printf("*e%d",MPOS(dl));
4833: }
4834:
4835: void nd_print(ND p)
4836: {
4837: NM m;
4838:
4839: if ( !p )
4840: printf("0\n");
4841: else {
4842: for ( m = BDY(p); m; m = NEXT(m) ) {
4843: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
4844: else printf("+%d*",CM(m));
4845: ndl_print(DL(m));
4846: }
4847: printf("\n");
4848: }
4849: }
4850:
4851: void nd_print_q(ND p)
4852: {
4853: NM m;
4854:
4855: if ( !p )
4856: printf("0\n");
4857: else {
4858: for ( m = BDY(p); m; m = NEXT(m) ) {
4859: printf("+");
1.6 noro 4860: printexpr(CO,(Obj)CZ(m));
1.1 noro 4861: printf("*");
4862: ndl_print(DL(m));
4863: }
4864: printf("\n");
4865: }
4866: }
4867:
4868: void ndp_print(ND_pairs d)
4869: {
4870: ND_pairs t;
4871:
4872: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4873: printf("\n");
4874: }
4875:
4876: void nd_removecont(int mod,ND p)
4877: {
4878: int i,n;
4879: Z *w;
4880: NM m;
4881: struct oVECT v;
4882:
4883: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4884: else if ( mod == -2 ) {
4885: Z inv;
4886: divlf(ONE,HCZ(p),&inv);
4887: nd_mul_c_lf(p,inv);
4888: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4889: else {
4890: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4891: w = (Z *)MALLOC(n*sizeof(Q));
4892: v.len = n;
4893: v.body = (pointer *)w;
1.6 noro 4894: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4895: removecont_array((P *)w,n,1);
1.6 noro 4896: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4897: }
4898: }
4899:
4900: void nd_removecont2(ND p1,ND p2)
4901: {
4902: int i,n1,n2,n;
4903: Z *w;
4904: NM m;
4905: struct oVECT v;
4906:
4907: n1 = nd_length(p1);
4908: n2 = nd_length(p2);
4909: n = n1+n2;
4910: w = (Z *)MALLOC(n*sizeof(Q));
4911: v.len = n;
4912: v.body = (pointer *)w;
4913: i = 0;
4914: if ( p1 )
1.6 noro 4915: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4916: if ( p2 )
1.6 noro 4917: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4918: removecont_array((P *)w,n,1);
4919: i = 0;
4920: if ( p1 )
1.6 noro 4921: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4922: if ( p2 )
1.6 noro 4923: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4924: }
4925:
4926: void ndv_removecont(int mod,NDV p)
4927: {
4928: int i,len,all_p;
4929: Z *c;
4930: P *w;
4931: Z dvr,t;
4932: P g,cont,tp;
4933: NMV m;
4934:
4935: if ( mod == -1 )
4936: ndv_mul_c(mod,p,_invsf(HCM(p)));
4937: else if ( mod == -2 ) {
4938: Z inv;
4939: divlf(ONE,HCZ(p),&inv);
4940: ndv_mul_c_lf(p,inv);
4941: } else if ( mod )
4942: ndv_mul_c(mod,p,invm(HCM(p),mod));
4943: else {
4944: len = p->len;
4945: w = (P *)MALLOC(len*sizeof(P));
4946: c = (Z *)MALLOC(len*sizeof(Q));
4947: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4948: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4949: all_p = all_p && !NUM(w[i]);
4950: }
4951: if ( all_p ) {
4952: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4953: mulp(nd_vc,(P)dvr,g,&cont);
4954: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4955: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4956: }
4957: } else {
4958: sortbynm((Q *)c,len);
4959: qltozl((Q *)c,len,&dvr);
4960: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4961: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4962: }
4963: }
4964: }
4965: }
4966:
4967: /* koko */
4968:
4969: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4970: {
4971: int len,i,max;
4972: NMV m,mr0,mr,t;
4973:
4974: len = p->len;
1.14 noro 4975: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 4976: max = MAX(max,TD(DL(m)));
4977: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4978: m = (NMV)((char *)mr0+(len-1)*oadv);
4979: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4980: t = (NMV)MALLOC(nmv_adv);
4981: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4982: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4983: CZ(mr) = CZ(m);
1.1 noro 4984: ndl_copy(DL(t),DL(mr));
4985: }
4986: NV(p)++;
4987: BDY(p) = mr0;
4988: }
4989:
4990: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4991: {
4992: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4993: int pos;
4994: Q *w;
4995: Q dvr,t;
4996: NMV m,r;
4997:
4998: len = p->len;
4999: newnvar = nd_nvar-1;
5000: newexporigin = nd_get_exporigin(ord);
5001: if ( nd_module ) newmpos = newexporigin-1;
5002: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5003: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5004: ndl_dehomogenize(DL(m));
5005: if ( newwpd != nd_wpd ) {
5006: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5007: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5008: CZ(r) = CZ(m);
1.1 noro 5009: if ( nd_module ) pos = MPOS(DL(m));
5010: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5011: adj = nd_exporigin-newexporigin;
5012: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5013: if ( nd_module ) {
5014: DL(r)[newmpos] = pos;
5015: }
5016: }
5017: }
5018: NV(p)--;
5019: }
5020:
5021: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5022: {
5023: int i;
5024: P *tpl,*tpl1;
5025: NODE l;
5026: P h,gcd,t;
5027:
5028: tpl = (P *)MALLOC(m*sizeof(P));
5029: tpl1 = (P *)MALLOC(m*sizeof(P));
5030: bcopy(pl,tpl,m*sizeof(P));
5031: gcd = (P)ONE;
5032: for ( l = nd_hcf; l; l = NEXT(l) ) {
5033: h = (P)BDY(l);
5034: while ( 1 ) {
5035: for ( i = 0; i < m; i++ )
5036: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5037: break;
5038: if ( i == m ) {
5039: bcopy(tpl1,tpl,m*sizeof(P));
5040: mulp(vl,gcd,h,&t); gcd = t;
5041: } else
5042: break;
5043: }
5044: }
5045: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5046: if ( full ) {
5047: heu_nezgcdnpz(vl,tpl,m,&t);
5048: mulp(vl,gcd,t,pr);
5049: } else
5050: *pr = gcd;
5051: }
5052:
5053: void removecont_array(P *p,int n,int full)
5054: {
5055: int all_p,all_q,i;
5056: Z *c;
5057: P *w;
5058: P t,s;
5059:
5060: for ( all_q = 1, i = 0; i < n; i++ )
5061: all_q = all_q && NUM(p[i]);
5062: if ( all_q ) {
5063: removecont_array_q((Z *)p,n);
5064: } else {
5065: c = (Z *)MALLOC(n*sizeof(Z));
5066: w = (P *)MALLOC(n*sizeof(P));
5067: for ( i = 0; i < n; i++ ) {
5068: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5069: }
5070: removecont_array_q(c,n);
5071: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5072: for ( i = 0; i < n; i++ ) {
5073: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5074: }
5075: }
5076: }
5077:
5078: /* c is an int array */
5079:
5080: void removecont_array_q(Z *c,int n)
5081: {
5082: struct oVECT v;
5083: Z d0,d1,a,u,u1,gcd;
5084: int i,j;
5085: Z *q,*r;
5086:
5087: q = (Z *)MALLOC(n*sizeof(Z));
5088: r = (Z *)MALLOC(n*sizeof(Z));
5089: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5090: gcdvz_estimate(&v,&d0);
5091: for ( i = 0; i < n; i++ ) {
5092: divqrz(c[i],d0,&q[i],&r[i]);
5093: }
5094: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5095: if ( i < n ) {
5096: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5097: gcdvz(&v,&d1);
5098: gcdz(d0,d1,&gcd);
1.6 noro 5099: /* exact division */
5100: divsz(d0,gcd,&a);
1.1 noro 5101: for ( i = 0; i < n; i++ ) {
5102: mulz(a,q[i],&u);
5103: if ( r[i] ) {
1.6 noro 5104: /* exact division */
5105: divsz(r[i],gcd,&u1);
1.1 noro 5106: addz(u,u1,&q[i]);
5107: } else
5108: q[i] = u;
5109: }
5110: }
5111: for ( i = 0; i < n; i++ ) c[i] = q[i];
5112: }
5113:
1.4 noro 5114: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5115:
5116: void mpz_removecont_array(mpz_t *c,int n)
5117: {
5118: mpz_t d0,a,u,u1,gcd;
5119: int i,j;
1.13 noro 5120: static mpz_t *q,*r;
5121: static int c_len = 0;
1.4 noro 5122:
5123: for ( i = 0; i < n; i++ )
5124: if ( mpz_sgn(c[i]) ) break;
5125: if ( i == n ) return;
5126: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5127: if ( n > c_len ) {
5128: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5129: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5130: c_len = n;
5131: }
1.4 noro 5132: for ( i = 0; i < n; i++ ) {
5133: mpz_init(q[i]); mpz_init(r[i]);
5134: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5135: }
5136: for ( i = 0; i < n; i++ )
5137: if ( mpz_sgn(r[i]) ) break;
5138: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5139: if ( i < n ) {
5140: mpz_gcd(gcd,d0,r[i]);
5141: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5142: mpz_div(a,d0,gcd);
5143: for ( i = 0; i < n; i++ ) {
5144: mpz_mul(u,a,q[i]);
5145: if ( mpz_sgn(r[i]) ) {
5146: mpz_div(u1,r[i],gcd);
5147: mpz_add(q[i],u,u1);
5148: } else
5149: mpz_set(q[i],u);
5150: }
5151: }
5152: for ( i = 0; i < n; i++ )
5153: mpz_set(c[i],q[i]);
5154: }
5155:
1.1 noro 5156: void nd_mul_c(int mod,ND p,int mul)
5157: {
5158: NM m;
5159: int c,c1;
5160:
5161: if ( !p ) return;
5162: if ( mul == 1 ) return;
5163: if ( mod == -1 )
5164: for ( m = BDY(p); m; m = NEXT(m) )
5165: CM(m) = _mulsf(CM(m),mul);
5166: else
5167: for ( m = BDY(p); m; m = NEXT(m) ) {
5168: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5169: }
5170: }
5171:
5172: void nd_mul_c_lf(ND p,Z mul)
5173: {
5174: NM m;
5175: Z c;
5176:
5177: if ( !p ) return;
5178: if ( UNIZ(mul) ) return;
5179: for ( m = BDY(p); m; m = NEXT(m) ) {
5180: mullf(CZ(m),mul,&c); CZ(m) = c;
5181: }
5182: }
5183:
5184: void nd_mul_c_q(ND p,P mul)
5185: {
5186: NM m;
5187: P c;
5188:
5189: if ( !p ) return;
5190: if ( UNIQ(mul) ) return;
5191: for ( m = BDY(p); m; m = NEXT(m) ) {
5192: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5193: }
5194: }
5195:
5196: void nd_mul_c_p(VL vl,ND p,P mul)
5197: {
5198: NM m;
5199: P c;
5200:
5201: if ( !p ) return;
5202: for ( m = BDY(p); m; m = NEXT(m) ) {
5203: mulp(vl,CP(m),mul,&c); CP(m) = c;
5204: }
5205: }
5206:
5207: void nd_free(ND p)
5208: {
5209: NM t,s;
5210:
5211: if ( !p ) return;
5212: t = BDY(p);
5213: while ( t ) {
5214: s = NEXT(t);
5215: FREENM(t);
5216: t = s;
5217: }
5218: FREEND(p);
5219: }
5220:
5221: void ndv_free(NDV p)
5222: {
5223: GCFREE(BDY(p));
5224: }
5225:
5226: void nd_append_red(UINT *d,int i)
5227: {
5228: RHist m,m0;
5229: int h;
5230:
5231: NEWRHist(m);
5232: h = ndl_hash_value(d);
5233: m->index = i;
5234: ndl_copy(d,DL(m));
5235: NEXT(m) = nd_red[h];
5236: nd_red[h] = m;
5237: }
5238:
5239: UINT *ndv_compute_bound(NDV p)
5240: {
5241: UINT *d1,*d2,*t;
5242: UINT u;
5243: int i,j,k,l,len,ind;
5244: NMV m;
5245:
5246: if ( !p )
5247: return 0;
5248: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5249: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5250: len = LEN(p);
5251: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5252: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5253: ndl_max(DL(m),d1,d2);
5254: t = d1; d1 = d2; d2 = t;
5255: }
5256: l = nd_nvar+31;
5257: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5258: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5259: u = d1[i];
5260: k = (nd_epw-1)*nd_bpe;
5261: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5262: t[ind] = (u>>k)&nd_mask0;
5263: }
5264: for ( ; ind < l; ind++ ) t[ind] = 0;
5265: return t;
5266: }
5267:
5268: UINT *nd_compute_bound(ND p)
5269: {
5270: UINT *d1,*d2,*t;
5271: UINT u;
5272: int i,j,k,l,len,ind;
5273: NM m;
5274:
5275: if ( !p )
5276: return 0;
5277: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5278: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5279: len = LEN(p);
5280: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5281: for ( m = NEXT(m); m; m = NEXT(m) ) {
5282: ndl_lcm(DL(m),d1,d2);
5283: t = d1; d1 = d2; d2 = t;
5284: }
5285: l = nd_nvar+31;
5286: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5287: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5288: u = d1[i];
5289: k = (nd_epw-1)*nd_bpe;
5290: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5291: t[ind] = (u>>k)&nd_mask0;
5292: }
5293: for ( ; ind < l; ind++ ) t[ind] = 0;
5294: return t;
5295: }
5296:
5297: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5298: /* of a term. In this case we need additional 1 word. */
5299:
5300: int nd_get_exporigin(struct order_spec *ord)
5301: {
5302: switch ( ord->id ) {
1.21 noro 5303: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5304: return 1+nd_module;
5305: case 1: case 257:
5306: /* block order */
5307: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5308: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5309: return ord->ord.block.length+1+nd_module;
5310: case 3: case 259:
5311: #if 0
5312: error("nd_get_exporigin : composite order is not supported yet.");
5313: #else
5314: return 1+nd_module;
5315: #endif
5316: default:
5317: error("nd_get_exporigin : ivalid argument.");
5318: return 0;
5319: }
5320: }
5321:
5322: void nd_setup_parameters(int nvar,int max) {
5323: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5324: struct order_pair *op;
5325:
5326: nd_nvar = nvar;
5327: if ( max ) {
5328: /* XXX */
5329: if ( do_weyl ) nd_bpe = 32;
5330: else if ( max < 2 ) nd_bpe = 1;
5331: else if ( max < 4 ) nd_bpe = 2;
5332: else if ( max < 8 ) nd_bpe = 3;
5333: else if ( max < 16 ) nd_bpe = 4;
5334: else if ( max < 32 ) nd_bpe = 5;
5335: else if ( max < 64 ) nd_bpe = 6;
5336: else if ( max < 256 ) nd_bpe = 8;
5337: else if ( max < 1024 ) nd_bpe = 10;
5338: else if ( max < 65536 ) nd_bpe = 16;
5339: else nd_bpe = 32;
5340: }
5341: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5342: UINT t;
5343: int st;
5344: int *v;
5345: /* t = max(weights) */
5346: t = 0;
5347: if ( current_dl_weight_vector )
5348: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5349: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5350: if ( t < st ) t = st;
5351: }
5352: if ( nd_matrix )
5353: for ( i = 0; i < nd_matrix_len; i++ )
5354: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5355: if ( (st=v[j]) < 0 ) st = -st;
5356: if ( t < st ) t = st;
5357: }
5358: /* i = bitsize of t */
5359: for ( i = 0; t; t >>=1, i++ );
5360: /* i += bitsize of nd_nvar */
5361: for ( t = nd_nvar; t; t >>=1, i++);
5362: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5363: if ( (nd_bpe+i) >= 31 )
5364: error("nd_setup_parameters : too large weight");
5365: }
5366: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5367: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5368: nd_exporigin = nd_get_exporigin(nd_ord);
5369: wpd = nd_exporigin+elen;
5370: if ( nd_module )
5371: nd_mpos = nd_exporigin-1;
5372: else
5373: nd_mpos = -1;
5374: if ( wpd != nd_wpd ) {
5375: nd_free_private_storage();
5376: nd_wpd = wpd;
5377: }
5378: if ( nd_bpe < 32 ) {
5379: nd_mask0 = (1<<nd_bpe)-1;
5380: } else {
5381: nd_mask0 = 0xffffffff;
5382: }
5383: bzero(nd_mask,sizeof(nd_mask));
5384: nd_mask1 = 0;
5385: for ( i = 0; i < nd_epw; i++ ) {
5386: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5387: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5388: }
5389: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5390: nd_epos = nd_create_epos(nd_ord);
5391: nd_blockmask = nd_create_blockmask(nd_ord);
5392: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5393: }
5394:
5395: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5396: {
5397: int i,obpe,oadv,h;
5398: static NM prev_nm_free_list;
5399: static ND_pairs prev_ndp_free_list;
5400: RHist mr0,mr;
5401: RHist r;
5402: RHist *old_red;
5403: ND_pairs s0,s,t;
5404: EPOS oepos;
5405:
5406: obpe = nd_bpe;
5407: oadv = nmv_adv;
5408: oepos = nd_epos;
5409: if ( obpe < 2 ) nd_bpe = 2;
5410: else if ( obpe < 3 ) nd_bpe = 3;
5411: else if ( obpe < 4 ) nd_bpe = 4;
5412: else if ( obpe < 5 ) nd_bpe = 5;
5413: else if ( obpe < 6 ) nd_bpe = 6;
5414: else if ( obpe < 8 ) nd_bpe = 8;
5415: else if ( obpe < 10 ) nd_bpe = 10;
5416: else if ( obpe < 16 ) nd_bpe = 16;
5417: else if ( obpe < 32 ) nd_bpe = 32;
5418: else error("nd_reconstruct : exponent too large");
5419:
5420: nd_setup_parameters(nd_nvar,0);
5421: prev_nm_free_list = _nm_free_list;
5422: prev_ndp_free_list = _ndp_free_list;
5423: _nm_free_list = 0;
5424: _ndp_free_list = 0;
5425: for ( i = nd_psn-1; i >= 0; i-- ) {
5426: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5427: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5428: }
5429: if ( trace )
5430: for ( i = nd_psn-1; i >= 0; i-- ) {
5431: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5432: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5433: }
5434: s0 = 0;
5435: for ( t = d; t; t = NEXT(t) ) {
5436: NEXTND_pairs(s0,s);
5437: s->i1 = t->i1;
5438: s->i2 = t->i2;
1.24 noro 5439: s->sig = t->sig;
1.1 noro 5440: SG(s) = SG(t);
5441: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5442: }
5443:
5444: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5445: for ( i = 0; i < REDTAB_LEN; i++ ) {
5446: old_red[i] = nd_red[i];
5447: nd_red[i] = 0;
5448: }
5449: for ( i = 0; i < REDTAB_LEN; i++ )
5450: for ( r = old_red[i]; r; r = NEXT(r) ) {
5451: NEWRHist(mr);
5452: mr->index = r->index;
5453: SG(mr) = SG(r);
5454: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5455: h = ndl_hash_value(DL(mr));
5456: NEXT(mr) = nd_red[h];
5457: nd_red[h] = mr;
1.24 noro 5458: mr->sig = r->sig;
1.1 noro 5459: }
5460: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5461: old_red = 0;
5462: for ( i = 0; i < nd_psn; i++ ) {
5463: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5464: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5465: r->sig = nd_psh[i]->sig;
1.1 noro 5466: nd_psh[i] = r;
5467: }
5468: if ( s0 ) NEXT(s) = 0;
5469: prev_nm_free_list = 0;
5470: prev_ndp_free_list = 0;
5471: #if 0
5472: GC_gcollect();
5473: #endif
5474: return s0;
5475: }
5476:
5477: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5478: {
5479: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5480: struct order_pair *op;
5481:
5482: n = nd_nvar;
5483: oepw = (sizeof(UINT)*8)/obpe;
5484: omask0 = (1<<obpe)-1;
5485: TD(r) = TD(d);
5486: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5487: if ( nd_blockmask ) {
5488: l = nd_blockmask->n;
5489: op = nd_blockmask->order_pair;
5490: for ( i = 1; i < nd_exporigin; i++ )
5491: r[i] = d[i];
5492: for ( j = 0, s = 0; j < l; j++ ) {
5493: ord_l = op[j].length;
5494: for ( i = 0; i < ord_l; i++, s++ ) {
5495: ei = GET_EXP_OLD(d,s);
5496: PUT_EXP(r,s,ei);
5497: }
5498: }
5499: } else {
5500: for ( i = 0; i < n; i++ ) {
5501: ei = GET_EXP_OLD(d,i);
5502: PUT_EXP(r,i,ei);
5503: }
5504: }
5505: if ( nd_module ) MPOS(r) = MPOS(d);
5506: }
5507:
5508: ND nd_copy(ND p)
5509: {
5510: NM m,mr,mr0;
5511: int c,n;
5512: ND r;
5513:
5514: if ( !p )
5515: return 0;
5516: else {
5517: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5518: NEXTNM(mr0,mr);
5519: CM(mr) = CM(m);
5520: ndl_copy(DL(m),DL(mr));
5521: }
5522: NEXT(mr) = 0;
5523: MKND(NV(p),mr0,LEN(p),r);
5524: SG(r) = SG(p);
5525: return r;
5526: }
5527: }
5528:
5529: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5530: {
5531: NM m1,m2;
5532: NDV p1,p2;
5533: ND t1,t2;
5534: UINT *lcm;
5535: P gp,tp;
5536: Z g,t;
5537: Z iq;
5538: int td;
5539: LIST hist;
5540: NODE node;
5541: DP d;
5542:
5543: if ( !mod && nd_demand ) {
5544: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5545: } else {
5546: if ( trace ) {
5547: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5548: } else {
5549: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5550: }
5551: }
5552: lcm = LCM(p);
5553: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5554: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5555: FREENM(m1); return 0;
5556: }
5557: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5558: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5559: FREENM(m1); FREENM(m2); return 0;
5560: }
5561:
5562: if ( mod == -1 ) {
5563: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5564: } else if ( mod > 0 ) {
5565: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5566: } else if ( mod == -2 ) {
5567: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5568: } else if ( nd_vc ) {
5569: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5570: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5571: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5572: } else {
1.6 noro 5573: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5574: }
5575: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5576: *rp = nd_add(mod,t1,t2);
5577: if ( nd_gentrace ) {
5578: /* nd_tracelist is initialized */
1.6 noro 5579: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5580: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5581: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5582: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5583: nd_tracelist = node;
5584: }
1.24 noro 5585: if ( *rp )
5586: (*rp)->sig = p->sig;
1.1 noro 5587: FREENM(m1); FREENM(m2);
5588: return 1;
5589: }
5590:
5591: void ndv_mul_c(int mod,NDV p,int mul)
5592: {
5593: NMV m;
5594: int c,c1,len,i;
5595:
5596: if ( !p ) return;
5597: len = LEN(p);
5598: if ( mod == -1 )
5599: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5600: CM(m) = _mulsf(CM(m),mul);
5601: else
5602: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5603: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5604: }
5605: }
5606:
5607: void ndv_mul_c_lf(NDV p,Z mul)
5608: {
5609: NMV m;
5610: Z c;
5611: int len,i;
5612:
5613: if ( !p ) return;
5614: len = LEN(p);
5615: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5616: mullf(CZ(m),mul,&c); CZ(m) = c;
5617: }
5618: }
5619:
5620: /* for nd_det */
5621: void ndv_mul_c_q(NDV p,Z mul)
5622: {
5623: NMV m;
5624: Z c;
5625: int len,i;
5626:
5627: if ( !p ) return;
5628: len = LEN(p);
5629: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 5630: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 5631: }
5632: }
5633:
5634: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
5635: int n2,i,j,l,n,tlen;
5636: UINT *d0;
5637: NM *tab,*psum;
5638: ND s,r;
5639: NM t;
5640: NMV m1;
5641:
5642: if ( !p ) return 0;
5643: n = NV(p); n2 = n>>1;
5644: d0 = DL(m0);
5645: l = LEN(p);
5646: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
5647: tab = (NM *)MALLOC(tlen*sizeof(NM));
5648: psum = (NM *)MALLOC(tlen*sizeof(NM));
5649: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
5650: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
5651: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
5652: /* m0(NM) * m1(NMV) => tab(NM) */
5653: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
5654: for ( j = 0; j < tlen; j++ ) {
5655: if ( tab[j] ) {
5656: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
5657: }
5658: }
5659: }
5660: for ( i = tlen-1, r = 0; i >= 0; i-- )
5661: if ( psum[i] ) {
5662: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
5663: MKND(n,psum[i],j,s);
5664: r = nd_add(mod,r,s);
5665: }
5666: if ( r ) SG(r) = SG(p)+TD(d0);
5667: return r;
5668: }
5669:
5670: /* product of monomials */
5671: /* XXX block order is not handled correctly */
5672:
5673: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
5674: {
5675: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
5676: UINT *d0,*d1,*d,*dt,*ctab;
5677: Z *ctab_q;
5678: Z q,q1;
5679: UINT c0,c1,c;
5680: NM *p;
5681: NM m,t;
5682: int mpos;
5683:
5684: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
5685: if ( !m0 || !m1 ) return;
5686: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
5687: if ( nd_module )
5688: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
5689:
5690: NEWNM(m); d = DL(m);
5691: if ( mod ) {
5692: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
5693: } else if ( nd_vc )
5694: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
5695: else
1.6 noro 5696: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 5697: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
5698: homo = n&1 ? 1 : 0;
5699: if ( homo ) {
5700: /* offset of h-degree */
5701: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
5702: PUT_EXP(DL(m),n-1,h);
5703: TD(DL(m)) = h;
5704: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5705: }
5706: tab[0] = m;
5707: NEWNM(m); d = DL(m);
5708: for ( i = 0, curlen = 1; i < n2; i++ ) {
5709: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
5710: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
5711: /* xi^a*(Di^k*xi^l)*Di^b */
5712: a += l; b += k;
5713: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
5714: if ( !k || !l ) {
5715: for ( j = 0; j < curlen; j++ )
5716: if ( (t = tab[j]) != 0 ) {
5717: dt = DL(t);
5718: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
5719: if ( nd_blockmask ) ndl_weight_mask(dt);
5720: }
5721: curlen *= k+1;
5722: continue;
5723: }
5724: min = MIN(k,l);
5725: if ( mod ) {
5726: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
5727: mkwcm(k,l,mod,(int *)ctab);
5728: } else {
5729: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
5730: mkwc(k,l,ctab_q);
5731: }
5732: for ( j = min; j >= 0; j-- ) {
5733: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
5734: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
5735: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
5736: if ( homo ) {
5737: TD(d) = s;
5738: PUT_EXP(d,n-1,s-h);
5739: } else TD(d) = h;
5740: if ( nd_blockmask ) ndl_weight_mask(d);
5741: if ( mod ) c = ctab[j];
5742: else q = ctab_q[j];
5743: p = tab+curlen*j;
5744: if ( j == 0 ) {
5745: for ( u = 0; u < curlen; u++, p++ ) {
5746: if ( tab[u] ) {
5747: ndl_addto(DL(tab[u]),d);
5748: if ( mod ) {
5749: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
5750: } else if ( nd_vc )
5751: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
5752: else {
1.6 noro 5753: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 5754: }
5755: }
5756: }
5757: } else {
5758: for ( u = 0; u < curlen; u++, p++ ) {
5759: if ( tab[u] ) {
5760: NEWNM(t);
5761: ndl_add(DL(tab[u]),d,DL(t));
5762: if ( mod ) {
5763: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
5764: } else if ( nd_vc )
5765: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
5766: else
1.6 noro 5767: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 5768: *p = t;
5769: }
5770: }
5771: }
5772: }
5773: curlen *= k+1;
5774: }
5775: FREENM(m);
5776: if ( nd_module ) {
5777: mpos = MPOS(d1);
5778: for ( i = 0; i < tlen; i++ )
5779: if ( tab[i] ) {
5780: d = DL(tab[i]);
5781: MPOS(d) = mpos;
5782: TD(d) = ndl_weight(d);
5783: }
5784: }
5785: }
5786:
5787: ND ndv_mul_nm_symbolic(NM m0,NDV p)
5788: {
5789: NM mr,mr0;
5790: NMV m;
5791: UINT *d,*dt,*dm;
5792: int c,n,td,i,c1,c2,len;
5793: Q q;
5794: ND r;
5795:
5796: if ( !p ) return 0;
5797: else {
5798: n = NV(p); m = BDY(p);
5799: d = DL(m0);
5800: len = LEN(p);
5801: mr0 = 0;
5802: td = TD(d);
5803: c = CM(m0);
5804: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5805: NEXTNM(mr0,mr);
5806: CM(mr) = 1;
5807: ndl_add(DL(m),d,DL(mr));
5808: }
5809: NEXT(mr) = 0;
5810: MKND(NV(p),mr0,len,r);
5811: SG(r) = SG(p) + TD(d);
5812: return r;
5813: }
5814: }
5815:
5816: ND ndv_mul_nm(int mod,NM m0,NDV p)
5817: {
5818: NM mr,mr0;
5819: NMV m;
5820: UINT *d,*dt,*dm;
5821: int c,n,td,i,c1,c2,len;
5822: P q;
5823: ND r;
5824:
5825: if ( !p ) return 0;
5826: else if ( do_weyl ) {
5827: if ( mod < 0 ) {
5828: error("ndv_mul_nm : not implemented (weyl)");
5829: return 0;
5830: } else
5831: return weyl_ndv_mul_nm(mod,m0,p);
5832: } else {
5833: n = NV(p); m = BDY(p);
5834: d = DL(m0);
5835: len = LEN(p);
5836: mr0 = 0;
5837: td = TD(d);
5838: if ( mod == -1 ) {
5839: c = CM(m0);
5840: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5841: NEXTNM(mr0,mr);
5842: CM(mr) = _mulsf(CM(m),c);
5843: ndl_add(DL(m),d,DL(mr));
5844: }
5845: } else if ( mod == -2 ) {
5846: Z cl;
5847: cl = CZ(m0);
5848: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5849: NEXTNM(mr0,mr);
5850: mullf(CZ(m),cl,&CZ(mr));
5851: ndl_add(DL(m),d,DL(mr));
5852: }
5853: } else if ( mod ) {
5854: c = CM(m0);
5855: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5856: NEXTNM(mr0,mr);
5857: c1 = CM(m);
5858: DMAR(c1,c,0,mod,c2);
5859: CM(mr) = c2;
5860: ndl_add(DL(m),d,DL(mr));
5861: }
5862: } else {
5863: q = CP(m0);
5864: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5865: NEXTNM(mr0,mr);
5866: mulp(nd_vc,CP(m),q,&CP(mr));
5867: ndl_add(DL(m),d,DL(mr));
5868: }
5869: }
5870: NEXT(mr) = 0;
5871: MKND(NV(p),mr0,len,r);
5872: SG(r) = SG(p) + TD(d);
5873: return r;
5874: }
5875: }
5876:
5877: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
5878: {
5879: NM mq0,mq;
5880: NMV tm;
5881: Q q;
5882: int i,nv,sg,c,c1,c2,hindex;
5883: ND p,t,r;
5884:
5885: if ( bucket->m < 0 ) return 0;
5886: else {
5887: nv = NV(d);
5888: mq0 = 0;
5889: tm = (NMV)MALLOC(nmv_adv);
5890: while ( 1 ) {
5891: if ( mod > 0 || mod == -1 )
5892: hindex = head_pbucket(mod,bucket);
5893: else if ( mod == -2 )
5894: hindex = head_pbucket_lf(bucket);
5895: else
5896: hindex = head_pbucket_q(bucket);
5897: if ( hindex < 0 ) break;
5898: p = bucket->body[hindex];
5899: NEXTNM(mq0,mq);
5900: ndl_sub(HDL(p),HDL(d),DL(mq));
5901: ndl_copy(DL(mq),DL(tm));
5902: if ( mod ) {
5903: c1 = invm(HCM(d),mod); c2 = HCM(p);
5904: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5905: CM(tm) = mod-c;
5906: } else {
1.6 noro 5907: divsz(HCZ(p),HCZ(d),&CZ(mq));
5908: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5909: }
5910: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5911: bucket->body[hindex] = nd_remove_head(p);
5912: t = nd_remove_head(t);
5913: add_pbucket(mod,bucket,t);
5914: }
5915: if ( !mq0 )
5916: r = 0;
5917: else {
5918: NEXT(mq) = 0;
5919: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5920: MKND(nv,mq0,i,r);
5921: /* XXX */
5922: SG(r) = HTD(r);
5923: }
5924: return r;
5925: }
5926: }
5927:
5928: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5929: {
5930: NMV m,mr,mr0,t;
5931: int len,i,k;
5932:
5933: if ( !p ) return;
5934: m = BDY(p); len = LEN(p);
5935: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5936: m = (NMV)((char *)mr0+(len-1)*oadv);
5937: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5938: t = (NMV)MALLOC(nmv_adv);
5939: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5940: CZ(t) = CZ(m);
1.1 noro 5941: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5942: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5943: CZ(mr) = CZ(t);
1.1 noro 5944: ndl_copy(DL(t),DL(mr));
5945: }
5946: BDY(p) = mr0;
5947: }
5948:
5949: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5950: {
5951: NMV m,mr,mr0;
5952: int len,i;
5953: NDV r;
5954:
5955: if ( !p ) return 0;
5956: m = BDY(p); len = LEN(p);
5957: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5958: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5959: ndl_zero(DL(mr));
5960: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5961: CZ(mr) = CZ(m);
1.1 noro 5962: }
5963: MKNDV(NV(p),mr0,len,r);
5964: SG(r) = SG(p);
1.24 noro 5965: r->sig = p->sig;
1.1 noro 5966: return r;
5967: }
5968:
5969: /* duplicate p */
5970:
5971: NDV ndv_dup(int mod,NDV p)
5972: {
5973: NDV d;
5974: NMV t,m,m0;
5975: int i,len;
5976:
5977: if ( !p ) return 0;
5978: len = LEN(p);
5979: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5980: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5981: ndl_copy(DL(t),DL(m));
1.6 noro 5982: CZ(m) = CZ(t);
1.1 noro 5983: }
5984: MKNDV(NV(p),m0,len,d);
5985: SG(d) = SG(p);
5986: return d;
5987: }
5988:
5989: NDV ndv_symbolic(int mod,NDV p)
5990: {
5991: NDV d;
5992: NMV t,m,m0;
5993: int i,len;
5994:
5995: if ( !p ) return 0;
5996: len = LEN(p);
5997: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5998: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5999: ndl_copy(DL(t),DL(m));
1.6 noro 6000: CZ(m) = ONE;
1.1 noro 6001: }
6002: MKNDV(NV(p),m0,len,d);
6003: SG(d) = SG(p);
6004: return d;
6005: }
6006:
6007: ND nd_dup(ND p)
6008: {
6009: ND d;
6010: NM t,m,m0;
6011:
6012: if ( !p ) return 0;
6013: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6014: NEXTNM(m0,m);
6015: ndl_copy(DL(t),DL(m));
1.6 noro 6016: CZ(m) = CZ(t);
1.1 noro 6017: }
6018: if ( m0 ) NEXT(m) = 0;
6019: MKND(NV(p),m0,LEN(p),d);
6020: SG(d) = SG(p);
6021: return d;
6022: }
6023:
6024: /* XXX if p->len == 0 then it represents 0 */
6025:
6026: void ndv_mod(int mod,NDV p)
6027: {
6028: NMV t,d;
6029: int r,s,u;
6030: int i,len,dlen;
6031: P cp;
6032: Z c;
6033: Obj gfs;
6034:
6035: if ( !p ) return;
6036: len = LEN(p);
6037: dlen = 0;
6038: if ( mod == -1 )
6039: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6040: simp_ff((Obj)CP(t),&gfs);
6041: if ( gfs ) {
6042: r = FTOIF(CONT((GFS)gfs));
6043: CM(d) = r;
6044: ndl_copy(DL(t),DL(d));
6045: NMV_ADV(d);
6046: dlen++;
6047: }
6048: }
6049: else if ( mod == -2 )
6050: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6051: simp_ff((Obj)CP(t),&gfs);
6052: if ( gfs ) {
6053: lmtolf((LM)gfs,&CZ(d));
6054: ndl_copy(DL(t),DL(d));
6055: NMV_ADV(d);
6056: dlen++;
6057: }
6058: }
6059: else
6060: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6061: if ( nd_vc ) {
6062: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6063: c = (Z)cp;
6064: } else
1.6 noro 6065: c = CZ(t);
1.1 noro 6066: r = remqi((Q)c,mod);
6067: if ( r ) {
6068: CM(d) = r;
6069: ndl_copy(DL(t),DL(d));
6070: NMV_ADV(d);
6071: dlen++;
6072: }
6073: }
6074: LEN(p) = dlen;
6075: }
6076:
6077: NDV ptondv(VL vl,VL dvl,P p)
6078: {
6079: ND nd;
6080:
6081: nd = ptond(vl,dvl,p);
6082: return ndtondv(0,nd);
6083: }
6084:
6085: void pltozpl(LIST l,Q *cont,LIST *pp)
6086: {
1.16 noro 6087: NODE nd,nd1;
6088: int n;
6089: P *pl;
6090: Q *cl;
6091: int i;
6092: P dmy;
6093: Z dvr,inv;
6094: LIST r;
6095:
6096: nd = BDY(l); n = length(nd);
6097: pl = (P *)MALLOC(n*sizeof(P));
6098: cl = (Q *)MALLOC(n*sizeof(Q));
6099: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6100: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6101: }
6102: qltozl(cl,n,&dvr);
6103: divz(ONE,dvr,&inv);
6104: nd = BDY(l);
6105: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6106: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6107: nd = 0;
6108: for ( i = n-1; i >= 0; i-- ) {
6109: MKNODE(nd1,pl[i],nd); nd = nd1;
6110: }
6111: MKLIST(r,nd);
6112: *pp = r;
1.1 noro 6113: }
6114:
6115: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6116:
6117: NDV pltondv(VL vl,VL dvl,LIST p)
6118: {
6119: int i;
6120: NODE t;
6121: ND r,ri;
6122: NM m;
6123:
6124: if ( !nd_module ) error("pltond : module order must be set");
6125: r = 0;
6126: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6127: ri = ptond(vl,dvl,(P)BDY(t));
6128: if ( ri )
6129: for ( m = BDY(ri); m; m = NEXT(m) ) {
6130: MPOS(DL(m)) = i;
6131: TD(DL(m)) = ndl_weight(DL(m));
6132: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6133: }
6134: r = nd_add(0,r,ri);
6135: }
6136: return ndtondv(0,r);
6137: }
6138:
6139: ND ptond(VL vl,VL dvl,P p)
6140: {
6141: int n,i,j,k,e;
6142: VL tvl;
6143: V v;
6144: DCP dc;
6145: DCP *w;
6146: ND r,s,t,u;
6147: P x;
6148: int c;
6149: UINT *d;
6150: NM m,m0;
6151:
6152: if ( !p )
6153: return 0;
6154: else if ( NUM(p) ) {
6155: NEWNM(m);
6156: ndl_zero(DL(m));
6157: if ( !INT((Q)p) )
6158: error("ptond : input must be integer-coefficient");
1.6 noro 6159: CZ(m) = (Z)p;
1.1 noro 6160: NEXT(m) = 0;
6161: MKND(nd_nvar,m,1,r);
6162: SG(r) = 0;
6163: return r;
6164: } else {
6165: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6166: w = (DCP *)MALLOC(k*sizeof(DCP));
6167: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6168: for ( i = 0, tvl = dvl, v = VR(p);
6169: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6170: if ( !tvl ) {
6171: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6172: t = ptond(vl,dvl,COEF(w[j]));
6173: pwrp(vl,x,DEG(w[j]),&p);
6174: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6175: }
6176: return s;
6177: } else {
6178: NEWNM(m0); d = DL(m0);
6179: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6180: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6181: TD(d) = MUL_WEIGHT(e,i);
6182: if ( nd_blockmask) ndl_weight_mask(d);
6183: if ( nd_module ) MPOS(d) = 0;
6184: t = ptond(vl,dvl,COEF(w[j]));
6185: for ( m = BDY(t); m; m = NEXT(m) )
6186: ndl_addto(DL(m),d);
6187: SG(t) += TD(d);
6188: s = nd_add(0,s,t);
6189: }
6190: FREENM(m0);
6191: return s;
6192: }
6193: }
6194: }
6195:
6196: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6197: {
6198: VL tvl;
6199: int len,n,j,i,e;
6200: NMV m;
6201: Z q;
6202: P c;
6203: UINT *d;
6204: P s,r,u,t,w;
6205: GFS gfs;
6206:
6207: if ( !p ) return 0;
6208: else {
6209: len = LEN(p);
6210: n = NV(p);
6211: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6212: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6213: if ( mod == -1 ) {
6214: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6215: } else if ( mod == -2 ) {
6216: c = (P)CZ(m);
6217: } else if ( mod > 0 ) {
1.6 noro 6218: STOZ(CM(m),q); c = (P)q;
1.1 noro 6219: } else
6220: c = CP(m);
6221: d = DL(m);
6222: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6223: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6224: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6225: }
6226: addp(vl,s,t,&u); s = u;
6227: }
6228: return s;
6229: }
6230: }
6231:
6232: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6233: {
6234: VL tvl;
6235: int len,n,j,i,e;
6236: NMV m;
6237: Z q;
6238: P c;
6239: UINT *d;
6240: P s,r,u,t,w;
6241: GFS gfs;
6242: P *a;
6243: LIST l;
6244: NODE nd,nd1;
6245:
6246: if ( !p ) return 0;
6247: else {
6248: a = (P *)MALLOC((rank+1)*sizeof(P));
6249: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6250: len = LEN(p);
6251: n = NV(p);
6252: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6253: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6254: if ( mod == -1 ) {
6255: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6256: } else if ( mod ) {
1.6 noro 6257: STOZ(CM(m),q); c = (P)q;
1.1 noro 6258: } else
6259: c = CP(m);
6260: d = DL(m);
6261: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6262: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6263: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6264: }
6265: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6266: }
6267: nd = 0;
6268: for ( i = rank; i > 0; i-- ) {
6269: MKNODE(nd1,a[i],nd); nd = nd1;
6270: }
6271: MKLIST(l,nd);
6272: return l;
6273: }
6274: }
6275:
6276: NDV ndtondv(int mod,ND p)
6277: {
6278: NDV d;
6279: NMV m,m0;
6280: NM t;
6281: int i,len;
6282:
6283: if ( !p ) return 0;
6284: len = LEN(p);
6285: if ( mod > 0 || mod == -1 )
6286: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6287: else
6288: m0 = m = MALLOC(len*nmv_adv);
6289: #if 0
6290: ndv_alloc += nmv_adv*len;
6291: #endif
6292: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6293: ndl_copy(DL(t),DL(m));
1.6 noro 6294: CZ(m) = CZ(t);
1.1 noro 6295: }
6296: MKNDV(NV(p),m0,len,d);
6297: SG(d) = SG(p);
1.24 noro 6298: d->sig = p->sig;
1.1 noro 6299: return d;
6300: }
6301:
1.16 noro 6302: static int dmm_comp_nv;
6303:
6304: int dmm_comp(DMM *a,DMM *b)
6305: {
6306: return -compdmm(dmm_comp_nv,*a,*b);
6307: }
6308:
6309: void dmm_sort_by_ord(DMM *a,int len,int nv)
6310: {
6311: dmm_comp_nv = nv;
6312: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6313: }
6314:
6315: void dpm_sort(DPM p,DPM *rp)
6316: {
6317: DMM t,t1;
6318: int len,i,n;
6319: DMM *a;
6320: DPM d;
6321:
6322: if ( !p ) *rp = 0;
6323: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6324: a = (DMM *)MALLOC(len*sizeof(DMM));
6325: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6326: n = p->nv;
6327: dmm_sort_by_ord(a,len,n);
6328: t = 0;
6329: for ( i = len-1; i >= 0; i-- ) {
6330: NEWDMM(t1);
6331: t1->c = a[i]->c;
6332: t1->dl = a[i]->dl;
6333: t1->pos = a[i]->pos;
6334: t1->next = t;
6335: t = t1;
6336: }
6337: MKDPM(n,t,d);
6338: SG(d) = SG(p);
6339: *rp = d;
6340: }
6341:
1.18 noro 6342: int dpm_comp(DPM *a,DPM *b)
6343: {
1.22 noro 6344: return -compdpm(CO,*a,*b);
1.18 noro 6345: }
6346:
6347: NODE dpm_sort_list(NODE l)
6348: {
6349: int i,len;
6350: NODE t,t1;
6351: DPM *a;
6352:
6353: len = length(l);
6354: a = (DPM *)MALLOC(len*sizeof(DPM));
6355: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6356: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6357: t = 0;
6358: for ( i = len-1; i >= 0; i-- ) {
6359: MKNODE(t1,(pointer)a[i],t); t = t1;
6360: }
6361: return t;
6362: }
6363:
1.20 noro 6364: int nmv_comp(NMV a,NMV b)
6365: {
1.21 noro 6366: int t;
6367: t = DL_COMPARE(a->dl,b->dl);
6368: return -t;
1.20 noro 6369: }
6370:
1.16 noro 6371: NDV dpmtondv(int mod,DPM p)
6372: {
6373: NDV d;
6374: NMV m,m0;
6375: DMM t;
6376: DMM *a;
6377: int i,len,n;
6378:
6379: if ( !p ) return 0;
6380: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6381: a = (DMM *)MALLOC(len*sizeof(DMM));
6382: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6383: n = p->nv;
6384: dmm_sort_by_ord(a,len,n);
6385: if ( mod > 0 || mod == -1 )
6386: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6387: else
6388: m0 = m = MALLOC(len*nmv_adv);
6389: #if 0
6390: ndv_alloc += nmv_adv*len;
6391: #endif
6392: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6393: dltondl(n,a[i]->dl,DL(m));
6394: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6395: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6396: CZ(m) = (Z)a[i]->c;
6397: }
1.20 noro 6398: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6399: MKNDV(NV(p),m0,len,d);
6400: SG(d) = SG(p);
6401: return d;
6402: }
6403:
1.1 noro 6404: ND ndvtond(int mod,NDV p)
6405: {
6406: ND d;
6407: NM m,m0;
6408: NMV t;
6409: int i,len;
6410:
6411: if ( !p ) return 0;
6412: m0 = 0;
6413: len = p->len;
6414: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6415: NEXTNM(m0,m);
6416: ndl_copy(DL(t),DL(m));
1.6 noro 6417: CZ(m) = CZ(t);
1.1 noro 6418: }
6419: NEXT(m) = 0;
6420: MKND(NV(p),m0,len,d);
6421: SG(d) = SG(p);
6422: return d;
6423: }
6424:
6425: DP ndvtodp(int mod,NDV p)
6426: {
6427: MP m,m0;
6428: DP d;
6429: NMV t;
6430: int i,len;
6431:
6432: if ( !p ) return 0;
6433: m0 = 0;
6434: len = p->len;
6435: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6436: NEXTMP(m0,m);
6437: m->dl = ndltodl(nd_nvar,DL(t));
6438: m->c = (Obj)ndctop(mod,t->c);
6439: }
6440: NEXT(m) = 0;
6441: MKDP(nd_nvar,m0,d);
6442: SG(d) = SG(p);
6443: return d;
6444: }
6445:
1.16 noro 6446: DPM ndvtodpm(int mod,NDV p)
6447: {
6448: DMM m,m0;
6449: DPM d;
6450: NMV t;
6451: int i,len;
6452:
6453: if ( !p ) return 0;
6454: m0 = 0;
6455: len = p->len;
6456: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6457: NEXTDMM(m0,m);
6458: m->dl = ndltodl(nd_nvar,DL(t));
6459: m->c = (Obj)ndctop(mod,t->c);
6460: m->pos = MPOS(DL(t));
6461: }
6462: NEXT(m) = 0;
6463: MKDPM(nd_nvar,m0,d);
6464: SG(d) = SG(p);
6465: return d;
6466: }
6467:
6468:
1.1 noro 6469: DP ndtodp(int mod,ND p)
6470: {
6471: MP m,m0;
6472: DP d;
6473: NM t;
6474: int i,len;
6475:
6476: if ( !p ) return 0;
6477: m0 = 0;
6478: len = p->len;
6479: for ( t = BDY(p); t; t = NEXT(t) ) {
6480: NEXTMP(m0,m);
6481: m->dl = ndltodl(nd_nvar,DL(t));
6482: m->c = (Obj)ndctop(mod,t->c);
6483: }
6484: NEXT(m) = 0;
6485: MKDP(nd_nvar,m0,d);
6486: SG(d) = SG(p);
6487: return d;
6488: }
6489:
6490: void ndv_print(NDV p)
6491: {
6492: NMV m;
6493: int i,len;
6494:
6495: if ( !p ) printf("0\n");
6496: else {
6497: len = LEN(p);
6498: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6499: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
6500: else printf("+%d*",CM(m));
6501: ndl_print(DL(m));
6502: }
6503: printf("\n");
6504: }
6505: }
6506:
6507: void ndv_print_q(NDV p)
6508: {
6509: NMV m;
6510: int i,len;
6511:
6512: if ( !p ) printf("0\n");
6513: else {
6514: len = LEN(p);
6515: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6516: printf("+");
1.6 noro 6517: printexpr(CO,(Obj)CZ(m));
1.1 noro 6518: printf("*");
6519: ndl_print(DL(m));
6520: }
6521: printf("\n");
6522: }
6523: }
6524:
6525: NODE ndv_reducebase(NODE x,int *perm)
6526: {
6527: int len,i,j;
6528: NDVI w;
6529: NODE t,t0;
6530:
6531: len = length(x);
6532: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6533: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6534: w[i].p = BDY(t); w[i].i = perm[i];
6535: }
6536: for ( i = 0; i < len; i++ ) {
6537: for ( j = 0; j < i; j++ ) {
6538: if ( w[i].p && w[j].p ) {
6539: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6540: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6541: }
6542: }
6543: }
6544: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6545: if ( w[i].p ) {
6546: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6547: perm[j++] = w[i].i;
6548: }
6549: }
6550: NEXT(t) = 0; x = t0;
6551: return x;
6552: }
6553:
6554: /* XXX incomplete */
6555:
1.21 noro 6556: extern DMMstack dmm_stack;
6557: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6558:
1.1 noro 6559: void nd_init_ord(struct order_spec *ord)
6560: {
6561: nd_module = (ord->id >= 256);
6562: if ( nd_module ) {
6563: nd_dcomp = -1;
1.21 noro 6564: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6565: nd_pot_nelim = ord->pot_nelim;
6566: nd_poly_weight_len = ord->nv;
6567: nd_poly_weight = ord->top_weight;
6568: nd_module_rank = ord->module_rank;
6569: nd_module_weight = ord->module_top_weight;
6570: }
6571: nd_matrix = 0;
6572: nd_matrix_len = 0;
6573: switch ( ord->id ) {
6574: case 0:
6575: switch ( ord->ord.simple ) {
6576: case 0:
6577: nd_dcomp = 1;
6578: nd_isrlex = 1;
6579: break;
6580: case 1:
6581: nd_dcomp = 1;
6582: nd_isrlex = 0;
6583: break;
6584: case 2:
6585: nd_dcomp = 0;
6586: nd_isrlex = 0;
6587: ndl_compare_function = ndl_lex_compare;
6588: break;
6589: case 11:
6590: /* XXX */
6591: nd_dcomp = 0;
6592: nd_isrlex = 1;
6593: ndl_compare_function = ndl_ww_lex_compare;
6594: break;
6595: default:
6596: error("nd_gr : unsupported order");
6597: }
6598: break;
6599: case 1:
6600: /* block order */
6601: /* XXX */
6602: nd_dcomp = -1;
6603: nd_isrlex = 0;
6604: ndl_compare_function = ndl_block_compare;
6605: break;
6606: case 2:
6607: /* matrix order */
6608: /* XXX */
6609: nd_dcomp = -1;
6610: nd_isrlex = 0;
6611: nd_matrix_len = ord->ord.matrix.row;
6612: nd_matrix = ord->ord.matrix.matrix;
6613: ndl_compare_function = ndl_matrix_compare;
6614: break;
6615: case 3:
6616: /* composite order */
6617: nd_dcomp = -1;
6618: nd_isrlex = 0;
6619: nd_worb_len = ord->ord.composite.length;
6620: nd_worb = ord->ord.composite.w_or_b;
6621: ndl_compare_function = ndl_composite_compare;
6622: break;
6623:
6624: /* module order */
6625: case 256:
6626: switch ( ord->ord.simple ) {
6627: case 0:
1.21 noro 6628: nd_dcomp = 0;
1.1 noro 6629: nd_isrlex = 1;
1.21 noro 6630: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 6631: break;
6632: case 1:
1.21 noro 6633: nd_dcomp = 0;
1.1 noro 6634: nd_isrlex = 0;
6635: ndl_compare_function = ndl_module_glex_compare;
6636: break;
6637: case 2:
1.21 noro 6638: nd_dcomp = 0;
1.1 noro 6639: nd_isrlex = 0;
1.21 noro 6640: ndl_compare_function = ndl_module_compare;
6641: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 6642: break;
6643: default:
1.21 noro 6644: error("nd_init_ord : unsupported order");
1.1 noro 6645: }
6646: break;
6647: case 257:
6648: /* block order */
6649: nd_isrlex = 0;
1.21 noro 6650: ndl_compare_function = ndl_module_compare;
6651: ndl_base_compare_function = ndl_block_compare;
1.1 noro 6652: break;
6653: case 258:
6654: /* matrix order */
6655: nd_isrlex = 0;
6656: nd_matrix_len = ord->ord.matrix.row;
6657: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 6658: ndl_compare_function = ndl_module_compare;
6659: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 6660: break;
6661: case 259:
6662: /* composite order */
6663: nd_isrlex = 0;
6664: nd_worb_len = ord->ord.composite.length;
6665: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 6666: ndl_compare_function = ndl_module_compare;
6667: ndl_base_compare_function = ndl_composite_compare;
6668: break;
6669: case 300:
6670: /* schreyer order */
6671: if ( ord->base->id != 256 )
6672: error("nd_init_ord : unsupported base order");
6673: ndl_compare_function = ndl_module_schreyer_compare;
6674: dmm_stack = ord->dmmstack;
6675: switch ( ord->base->ord.simple ) {
6676: case 0:
6677: nd_isrlex = 1;
6678: ndl_base_compare_function = ndl_glex_compare;
6679: dl_base_compare_function = cmpdl_revgradlex;
6680: break;
6681: case 1:
6682: nd_isrlex = 0;
6683: ndl_base_compare_function = ndl_glex_compare;
6684: dl_base_compare_function = cmpdl_gradlex;
6685: break;
6686: case 2:
6687: nd_isrlex = 0;
6688: ndl_base_compare_function = ndl_lex_compare;
6689: dl_base_compare_function = cmpdl_lex;
6690: break;
6691: default:
6692: error("nd_init_ord : unsupported order");
6693: }
1.1 noro 6694: break;
6695: }
6696: nd_ord = ord;
6697: }
6698:
6699: BlockMask nd_create_blockmask(struct order_spec *ord)
6700: {
6701: int n,i,j,s,l;
6702: UINT *t;
6703: BlockMask bm;
6704:
6705: /* we only create mask table for block order */
6706: if ( ord->id != 1 && ord->id != 257 )
6707: return 0;
6708: n = ord->ord.block.length;
6709: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
6710: bm->n = n;
6711: bm->order_pair = ord->ord.block.order_pair;
6712: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
6713: for ( i = 0, s = 0; i < n; i++ ) {
6714: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
6715: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
6716: l = bm->order_pair[i].length;
6717: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
6718: }
6719: return bm;
6720: }
6721:
6722: EPOS nd_create_epos(struct order_spec *ord)
6723: {
6724: int i,j,l,s,ord_l,ord_o;
6725: EPOS epos;
6726: struct order_pair *op;
6727:
6728: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
6729: switch ( ord->id ) {
1.21 noro 6730: case 0: case 256: case 300:
1.1 noro 6731: if ( nd_isrlex ) {
6732: for ( i = 0; i < nd_nvar; i++ ) {
6733: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
6734: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
6735: }
6736: } else {
6737: for ( i = 0; i < nd_nvar; i++ ) {
6738: epos[i].i = nd_exporigin + i/nd_epw;
6739: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6740: }
6741: }
6742: break;
6743: case 1: case 257:
6744: /* block order */
6745: l = ord->ord.block.length;
6746: op = ord->ord.block.order_pair;
6747: for ( j = 0, s = 0; j < l; j++ ) {
6748: ord_o = op[j].order;
6749: ord_l = op[j].length;
6750: if ( !ord_o )
6751: for ( i = 0; i < ord_l; i++ ) {
6752: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
6753: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
6754: }
6755: else
6756: for ( i = 0; i < ord_l; i++ ) {
6757: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
6758: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
6759: }
6760: s += ord_l;
6761: }
6762: break;
6763: case 2:
6764: /* matrix order */
6765: case 3:
6766: /* composite order */
6767: default:
6768: for ( i = 0; i < nd_nvar; i++ ) {
6769: epos[i].i = nd_exporigin + i/nd_epw;
6770: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6771: }
6772: break;
6773: }
6774: return epos;
6775: }
6776:
6777: /* external interface */
6778:
6779: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
6780: {
6781: NODE t,in0,in;
6782: ND ndf,nf;
6783: NDV ndvf;
6784: VL vv,tv;
6785: int stat,nvar,max,mrank;
6786: union oNDC dn;
6787: Q cont;
6788: P pp;
6789: LIST ppl;
6790:
6791: if ( !f ) {
6792: *rp = 0;
6793: return;
6794: }
6795: pltovl(v,&vv);
6796: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
6797:
6798: /* max=65536 implies nd_bpe=32 */
6799: max = 65536;
6800:
6801: nd_module = 0;
6802: /* nd_module will be set if ord is a module ordering */
6803: nd_init_ord(ord);
6804: nd_setup_parameters(nvar,max);
6805: if ( nd_module && OID(f) != O_LIST )
6806: error("nd_nf_p : the first argument must be a list");
6807: if ( nd_module ) mrank = length(BDY((LIST)f));
6808: /* conversion to ndv */
6809: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
6810: NEXTNODE(in0,in);
6811: if ( nd_module ) {
6812: if ( !BDY(t) || OID(BDY(t)) != O_LIST
6813: || length(BDY((LIST)BDY(t))) != mrank )
6814: error("nd_nf_p : inconsistent basis element");
6815: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
6816: else ppl = (LIST)BDY(t);
6817: BDY(in) = (pointer)pltondv(CO,vv,ppl);
6818: } else {
6819: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
6820: else pp = (P)BDY(t);
6821: BDY(in) = (pointer)ptondv(CO,vv,pp);
6822: }
6823: if ( m ) ndv_mod(m,(NDV)BDY(in));
6824: }
6825: if ( in0 ) NEXT(in) = 0;
6826:
6827: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
6828: else ndvf = ptondv(CO,vv,(P)f);
6829: if ( m ) ndv_mod(m,ndvf);
6830: ndf = (pointer)ndvtond(m,ndvf);
6831:
6832: /* dont sort, dont removecont */
1.24 noro 6833: ndv_setup(m,0,in0,1,1,0);
1.1 noro 6834: nd_scale=2;
1.6 noro 6835: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 6836: if ( !stat )
6837: error("nd_nf_p : exponent too large");
6838: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
6839: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
6840: }
6841:
6842: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
6843: {
6844: NM m;
6845: UINT *t,*s;
6846: int i;
6847:
6848: for ( i = 0; i < n; i++ ) r[i] = 0;
6849: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6850: t = DL(m);
6851: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6852: r[i] = CM(m);
6853: }
6854: for ( i = 0; !r[i]; i++ );
6855: return i;
6856: }
6857:
6858: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
6859: {
6860: NM m;
6861: UINT *t,*s;
6862: int i;
6863:
6864: for ( i = 0; i < n; i++ ) r[i] = 0;
6865: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6866: t = DL(m);
6867: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6868: r[i] = CZ(m);
1.1 noro 6869: }
6870: for ( i = 0; !r[i]; i++ );
6871: return i;
6872: }
6873:
6874: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
6875: {
6876: NM m;
6877: UINT *t,*s;
6878: int i;
6879:
6880: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
6881: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6882: t = DL(m);
6883: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6884: mpz_set(r[i],BDY(CZ(m)));
6885: }
6886: for ( i = 0; !mpz_sgn(r[i]); i++ );
6887: return i;
6888: }
6889:
6890: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
6891: {
6892: NM m;
6893: unsigned long *v;
6894: int i,j,h,size;
6895: UINT *s,*t;
6896:
6897: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
6898: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
6899: bzero(v,size);
6900: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
6901: t = DL(m);
6902: h = ndl_hash_value(t);
6903: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6904: v[i/BLEN] |= 1L <<(i%BLEN);
6905: }
6906: return v;
6907: }
6908:
6909: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
6910: {
6911: NMV mr;
6912: UINT *d,*t,*s;
6913: int i,j,len,h,head;
6914:
6915: d = DL(m);
6916: len = LEN(p);
6917: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6918: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6919: ndl_add(d,DL(mr),t);
6920: h = ndl_hash_value(t);
6921: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6922: if ( j == 0 ) head = i;
6923: v[i/BLEN] |= 1L <<(i%BLEN);
6924: }
6925: return head;
6926: }
6927:
6928: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
6929: {
6930: NM m;
6931: NMV mr;
6932: UINT *d,*t,*s;
6933: NDV p;
6934: int i,j,len;
6935: Z *r;
6936:
6937: m = pair->mul;
6938: d = DL(m);
6939: p = nd_ps[pair->index];
6940: len = LEN(p);
6941: r = (Z *)CALLOC(n,sizeof(Q));
6942: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6943: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6944: ndl_add(d,DL(mr),t);
6945: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6946: r[i] = CZ(mr);
1.1 noro 6947: }
6948: return r;
6949: }
6950:
1.11 noro 6951: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 6952: {
6953: NM m;
6954: NMV mr;
1.11 noro 6955: UINT *d,*t,*s,*u;
1.1 noro 6956: NDV p;
6957: unsigned char *ivc;
6958: unsigned short *ivs;
6959: UINT *v,*ivi,*s0v;
1.11 noro 6960: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 6961: IndArray r;
6962:
6963: m = pair->mul;
6964: d = DL(m);
6965: if ( trace )
6966: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
6967: else
6968: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
6969:
6970: len = LEN(p);
6971: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6972: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 6973: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
6974: ndl_add(d,DL(mr),t);
6975: st = prev;
6976: ed = n;
6977: while ( ed > st ) {
6978: md = (st+ed)/2;
6979: u = s0+md*nd_wpd;
6980: c = DL_COMPARE(u,t);
6981: if ( c == 0 ) break;
6982: else if ( c > 0 ) st = md;
6983: else ed = md;
6984: }
6985: prev = v[j] = md;
1.1 noro 6986: }
6987: r = (IndArray)MALLOC(sizeof(struct oIndArray));
6988: r->head = v[0];
6989: diff = 0;
6990: for ( i = 1; i < len; i++ ) {
6991: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
6992: }
6993: if ( diff < 256 ) {
6994: r->width = 1;
6995: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
6996: r->index.c = ivc;
6997: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
6998: } else if ( diff < 65536 ) {
6999: r->width = 2;
7000: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7001: r->index.s = ivs;
7002: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7003: } else {
7004: r->width = 4;
7005: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7006: r->index.i = ivi;
7007: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7008: }
7009: return r;
7010: }
7011:
7012: int compress_array(Z *svect,Z *cvect,int n)
7013: {
7014: int i,j;
7015:
7016: for ( i = j = 0; i < n; i++ )
7017: if ( svect[i] ) cvect[j++] = svect[i];
7018: return j;
7019: }
7020:
7021: void expand_array(Z *svect,Z *cvect,int n)
7022: {
7023: int i,j;
7024:
7025: for ( i = j = 0; j < n; i++ )
7026: if ( svect[i] ) svect[i] = cvect[j++];
7027: }
7028:
1.8 noro 7029: #if 0
1.1 noro 7030: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7031: {
7032: int i,j,k,len,pos,prev,nz;
7033: Z cs,mcs,c1,c2,cr,gcd,t;
7034: IndArray ivect;
7035: unsigned char *ivc;
7036: unsigned short *ivs;
7037: unsigned int *ivi;
7038: NDV redv;
7039: NMV mr;
7040: NODE rp;
7041: int maxrs;
7042: double hmag;
7043: Z *cvect;
1.3 noro 7044: int l;
1.1 noro 7045:
7046: maxrs = 0;
7047: for ( i = 0; i < col && !svect[i]; i++ );
7048: if ( i == col ) return maxrs;
7049: hmag = p_mag((P)svect[i])*nd_scale;
7050: cvect = (Z *)MALLOC(col*sizeof(Q));
7051: for ( i = 0; i < nred; i++ ) {
7052: ivect = imat[i];
7053: k = ivect->head;
7054: if ( svect[k] ) {
7055: maxrs = MAX(maxrs,rp0[i]->sugar);
7056: redv = nd_demand?ndv_load(rp0[i]->index)
7057: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7058: len = LEN(redv); mr = BDY(redv);
1.6 noro 7059: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7060: chsgnz(cs,&mcs);
7061: if ( !UNIQ(cr) ) {
7062: for ( j = 0; j < col; j++ ) {
7063: mulz(svect[j],cr,&c1); svect[j] = c1;
7064: }
7065: }
7066: svect[k] = 0; prev = k;
7067: switch ( ivect->width ) {
7068: case 1:
7069: ivc = ivect->index.c;
7070: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7071: pos = prev+ivc[j]; prev = pos;
1.6 noro 7072: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7073: }
7074: break;
7075: case 2:
7076: ivs = ivect->index.s;
7077: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7078: pos = prev+ivs[j]; prev = pos;
1.6 noro 7079: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7080: }
7081: break;
7082: case 4:
7083: ivi = ivect->index.i;
7084: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7085: pos = prev+ivi[j]; prev = pos;
1.6 noro 7086: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7087: }
7088: break;
7089: }
7090: for ( j = k+1; j < col && !svect[j]; j++ );
7091: if ( j == col ) break;
7092: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7093: nz = compress_array(svect,cvect,col);
7094: removecont_array((P *)cvect,nz,1);
7095: expand_array(svect,cvect,nz);
7096: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7097: }
7098: }
7099: }
7100: nz = compress_array(svect,cvect,col);
7101: removecont_array((P *)cvect,nz,1);
7102: expand_array(svect,cvect,nz);
7103: if ( DP_Print ) {
7104: fprintf(asir_out,"-"); fflush(asir_out);
7105: }
7106: return maxrs;
7107: }
1.4 noro 7108: #else
1.9 noro 7109:
1.4 noro 7110: /* direct mpz version */
7111: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7112: {
7113: int i,j,k,len,pos,prev;
7114: mpz_t cs,cr,gcd;
7115: IndArray ivect;
7116: unsigned char *ivc;
7117: unsigned short *ivs;
7118: unsigned int *ivi;
7119: NDV redv;
7120: NMV mr;
7121: NODE rp;
7122: int maxrs;
7123: double hmag;
7124: int l;
1.13 noro 7125: static mpz_t *svect;
7126: static int svect_len=0;
1.4 noro 7127:
7128: maxrs = 0;
7129: for ( i = 0; i < col && !svect0[i]; i++ );
7130: if ( i == col ) return maxrs;
7131: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7132: if ( col > svect_len ) {
7133: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7134: svect_len = col;
7135: }
1.4 noro 7136: for ( i = 0; i < col; i++ ) {
7137: mpz_init(svect[i]);
7138: if ( svect0[i] )
7139: mpz_set(svect[i],BDY(svect0[i]));
7140: else
7141: mpz_set_ui(svect[i],0);
7142: }
7143: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7144: for ( i = 0; i < nred; i++ ) {
7145: ivect = imat[i];
7146: k = ivect->head;
7147: if ( mpz_sgn(svect[k]) ) {
7148: maxrs = MAX(maxrs,rp0[i]->sugar);
7149: redv = nd_demand?ndv_load(rp0[i]->index)
7150: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7151: len = LEN(redv); mr = BDY(redv);
1.6 noro 7152: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7153: mpz_div(cs,svect[k],gcd);
1.6 noro 7154: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7155: mpz_neg(cs,cs);
1.9 noro 7156: if ( MUNIMPZ(cr) )
7157: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7158: else if ( !UNIMPZ(cr) )
7159: for ( j = 0; j < col; j++ ) {
7160: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7161: }
1.4 noro 7162: mpz_set_ui(svect[k],0);
7163: prev = k;
7164: switch ( ivect->width ) {
7165: case 1:
7166: ivc = ivect->index.c;
7167: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7168: pos = prev+ivc[j]; prev = pos;
1.6 noro 7169: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7170: }
7171: break;
7172: case 2:
7173: ivs = ivect->index.s;
7174: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7175: pos = prev+ivs[j]; prev = pos;
1.6 noro 7176: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7177: }
7178: break;
7179: case 4:
7180: ivi = ivect->index.i;
7181: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7182: pos = prev+ivi[j]; prev = pos;
1.6 noro 7183: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7184: }
7185: break;
7186: }
7187: for ( j = k+1; j < col && !svect[j]; j++ );
7188: if ( j == col ) break;
7189: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7190: mpz_removecont_array(svect,col);
7191: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7192: }
7193: }
7194: }
7195: mpz_removecont_array(svect,col);
7196: if ( DP_Print ) {
7197: fprintf(asir_out,"-"); fflush(asir_out);
7198: }
7199: for ( i = 0; i < col; i++ )
7200: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7201: else svect0[i] = 0;
7202: return maxrs;
7203: }
7204: #endif
1.1 noro 7205:
7206: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7207: {
7208: int i,j,k,len,pos,prev;
7209: UINT c,c1,c2,c3,up,lo,dmy;
7210: IndArray ivect;
7211: unsigned char *ivc;
7212: unsigned short *ivs;
7213: unsigned int *ivi;
7214: NDV redv;
7215: NMV mr;
7216: NODE rp;
7217: int maxrs;
7218:
7219: maxrs = 0;
7220: for ( i = 0; i < nred; i++ ) {
7221: ivect = imat[i];
7222: k = ivect->head; svect[k] %= m;
7223: if ( (c = svect[k]) != 0 ) {
7224: maxrs = MAX(maxrs,rp0[i]->sugar);
7225: c = m-c; redv = nd_ps[rp0[i]->index];
7226: len = LEN(redv); mr = BDY(redv);
7227: svect[k] = 0; prev = k;
7228: switch ( ivect->width ) {
7229: case 1:
7230: ivc = ivect->index.c;
7231: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7232: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
7233: if ( c1 ) {
7234: c2 = svect[pos];
7235: DMA(c1,c,c2,up,lo);
7236: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7237: } else svect[pos] = lo;
7238: }
7239: }
7240: break;
7241: case 2:
7242: ivs = ivect->index.s;
7243: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7244: pos = prev+ivs[j]; c1 = CM(mr);
7245: prev = pos;
7246: if ( c1 ) {
7247: c2 = svect[pos];
7248: DMA(c1,c,c2,up,lo);
7249: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7250: } else svect[pos] = lo;
7251: }
7252: }
7253: break;
7254: case 4:
7255: ivi = ivect->index.i;
7256: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7257: pos = prev+ivi[j]; c1 = CM(mr);
7258: prev = pos;
7259: if ( c1 ) {
7260: c2 = svect[pos];
7261: DMA(c1,c,c2,up,lo);
7262: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7263: } else svect[pos] = lo;
7264: }
7265: }
7266: break;
7267: }
7268: }
7269: }
7270: for ( i = 0; i < col; i++ )
7271: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7272: return maxrs;
7273: }
7274:
7275: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7276: {
7277: int i,j,k,len,pos,prev;
7278: UINT c,c1,c2,c3,up,lo,dmy;
7279: IndArray ivect;
7280: unsigned char *ivc;
7281: unsigned short *ivs;
7282: unsigned int *ivi;
7283: NDV redv;
7284: NMV mr;
7285: NODE rp;
7286: int maxrs;
7287:
7288: maxrs = 0;
7289: for ( i = 0; i < nred; i++ ) {
7290: ivect = imat[i];
7291: k = ivect->head;
7292: if ( (c = svect[k]) != 0 ) {
7293: maxrs = MAX(maxrs,rp0[i]->sugar);
7294: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7295: len = LEN(redv); mr = BDY(redv);
7296: svect[k] = 0; prev = k;
7297: switch ( ivect->width ) {
7298: case 1:
7299: ivc = ivect->index.c;
7300: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7301: pos = prev+ivc[j]; prev = pos;
7302: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7303: }
7304: break;
7305: case 2:
7306: ivs = ivect->index.s;
7307: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7308: pos = prev+ivs[j]; prev = pos;
7309: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7310: }
7311: break;
7312: case 4:
7313: ivi = ivect->index.i;
7314: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7315: pos = prev+ivi[j]; prev = pos;
7316: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7317: }
7318: break;
7319: }
7320: }
7321: }
7322: return maxrs;
7323: }
7324:
7325: ND nd_add_lf(ND p1,ND p2)
7326: {
7327: int n,c,can;
7328: ND r;
7329: NM m1,m2,mr0,mr,s;
7330: Z t;
7331:
7332: if ( !p1 ) return p2;
7333: else if ( !p2 ) return p1;
7334: else {
7335: can = 0;
7336: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7337: c = DL_COMPARE(DL(m1),DL(m2));
7338: switch ( c ) {
7339: case 0:
7340: addlf(CZ(m1),CZ(m2),&t);
7341: s = m1; m1 = NEXT(m1);
7342: if ( t ) {
7343: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7344: } else {
7345: can += 2; FREENM(s);
7346: }
7347: s = m2; m2 = NEXT(m2); FREENM(s);
7348: break;
7349: case 1:
7350: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7351: break;
7352: case -1:
7353: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7354: break;
7355: }
7356: }
7357: if ( !mr0 )
7358: if ( m1 ) mr0 = m1;
7359: else if ( m2 ) mr0 = m2;
7360: else return 0;
7361: else if ( m1 ) NEXT(mr) = m1;
7362: else if ( m2 ) NEXT(mr) = m2;
7363: else NEXT(mr) = 0;
7364: BDY(p1) = mr0;
7365: SG(p1) = MAX(SG(p1),SG(p2));
7366: LEN(p1) = LEN(p1)+LEN(p2)-can;
7367: FREEND(p2);
7368: return p1;
7369: }
7370: }
7371:
7372: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7373: {
7374: int i,j,k,len,pos,prev;
7375: mpz_t c,mc,c1;
7376: IndArray ivect;
7377: unsigned char *ivc;
7378: unsigned short *ivs;
7379: unsigned int *ivi;
7380: NDV redv;
7381: NMV mr;
7382: NODE rp;
7383: int maxrs;
7384:
7385: maxrs = 0;
7386: lf_lazy = 1;
7387: for ( i = 0; i < nred; i++ ) {
7388: ivect = imat[i];
7389: k = ivect->head;
7390: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7391: if ( mpz_sgn(svect[k]) ) {
7392: maxrs = MAX(maxrs,rp0[i]->sugar);
7393: mpz_neg(svect[k],svect[k]);
7394: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7395: len = LEN(redv); mr = BDY(redv);
7396: prev = k;
7397: switch ( ivect->width ) {
7398: case 1:
7399: ivc = ivect->index.c;
7400: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7401: pos = prev+ivc[j]; prev = pos;
7402: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7403: }
7404: break;
7405: case 2:
7406: ivs = ivect->index.s;
7407: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7408: pos = prev+ivs[j]; prev = pos;
7409: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7410: }
7411: break;
7412: case 4:
7413: ivi = ivect->index.i;
7414: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7415: pos = prev+ivi[j]; prev = pos;
7416: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7417: }
7418: break;
7419: }
7420: mpz_set_ui(svect[k],0);
7421: }
7422: }
7423: lf_lazy=0;
7424: for ( i = 0; i < col; i++ ) {
7425: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7426: }
7427: return maxrs;
7428: }
7429:
7430: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7431: {
7432: int i,j,k,l,rank,s;
7433: mpz_t a,a1,inv;
7434: mpz_t *t,*pivot,*pk;
7435: mpz_t **mat;
7436: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7437: int size,size1;
7438:
7439: mpz_init(inv);
7440: mpz_init(a);
7441: mat = (mpz_t **)mat0;
7442: size = 0;
7443: for ( rank = 0, j = 0; j < col; j++ ) {
7444: for ( i = rank; i < row; i++ ) {
7445: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7446: }
7447: for ( i = rank; i < row; i++ )
7448: if ( mpz_sgn(mat[i][j]) )
7449: break;
7450: if ( i == row ) {
7451: colstat[j] = 0;
7452: continue;
7453: } else
7454: colstat[j] = 1;
7455: if ( i != rank ) {
7456: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7457: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7458: }
7459: pivot = mat[rank];
7460: s = sugar[rank];
7461: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7462: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7463: if ( mpz_sgn(*pk) ) {
7464: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7465: }
7466: for ( i = rank+1; i < row; i++ ) {
7467: t = mat[i];
7468: if ( mpz_sgn(t[j]) ) {
7469: sugar[i] = MAX(sugar[i],s);
7470: mpz_neg(a,t[j]);
7471: red_by_vect_lf(t+j,pivot+j,a,col-j);
7472: }
7473: }
7474: rank++;
7475: }
7476: for ( j = col-1, l = rank-1; j >= 0; j-- )
7477: if ( colstat[j] ) {
7478: pivot = mat[l];
7479: s = sugar[l];
7480: for ( k = j; k < col; k++ )
7481: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7482: for ( i = 0; i < l; i++ ) {
7483: t = mat[i];
7484: if ( mpz_sgn(t[j]) ) {
7485: sugar[i] = MAX(sugar[i],s);
7486: mpz_neg(a,t[j]);
7487: red_by_vect_lf(t+j,pivot+j,a,col-j);
7488: }
7489: }
7490: l--;
7491: }
7492: for ( j = 0, l = 0; l < rank; j++ )
7493: if ( colstat[j] ) {
7494: t = mat[l];
7495: for ( k = j; k < col; k++ ) {
7496: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7497: }
7498: l++;
7499: }
7500: return rank;
7501: }
7502:
7503:
7504: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7505: {
7506: int j,k,len;
7507: UINT *p;
7508: UINT c;
7509: NDV r;
7510: NMV mr0,mr;
7511:
7512: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7513: if ( !len ) return 0;
7514: else {
7515: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7516: #if 0
7517: ndv_alloc += nmv_adv*len;
7518: #endif
7519: mr = mr0;
7520: p = s0vect;
7521: for ( j = k = 0; j < col; j++, p += nd_wpd )
7522: if ( !rhead[j] ) {
7523: if ( (c = vect[k++]) != 0 ) {
7524: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7525: }
7526: }
7527: MKNDV(nd_nvar,mr0,len,r);
7528: return r;
7529: }
7530: }
7531:
7532: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7533: {
7534: int j,k,len;
7535: UINT *p;
7536: NDV r;
7537: NMV mr0,mr;
7538:
7539: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7540: if ( !len ) return 0;
7541: else {
7542: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7543: mr = mr0;
7544: p = s0vect;
7545: for ( j = 0; j < col; j++, p += nd_wpd )
7546: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7547: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7548: }
7549: MKNDV(nd_nvar,mr0,len,r);
7550: return r;
7551: }
7552: }
7553:
7554: /* for preprocessed vector */
7555:
7556: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7557: {
1.6 noro 7558: int j,k,len;
7559: UINT *p;
7560: Z c;
7561: NDV r;
7562: NMV mr0,mr;
1.1 noro 7563:
1.6 noro 7564: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7565: if ( !len ) return 0;
7566: else {
7567: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7568: #if 0
1.6 noro 7569: ndv_alloc += nmv_adv*len;
1.1 noro 7570: #endif
1.6 noro 7571: mr = mr0;
7572: p = s0vect;
7573: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7574: if ( !rhead[j] ) {
7575: if ( (c = vect[k++]) != 0 ) {
7576: if ( !INT(c) )
7577: error("vect_to_ndv_q : components must be integers");
7578: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7579: }
7580: }
1.1 noro 7581: }
1.6 noro 7582: MKNDV(nd_nvar,mr0,len,r);
7583: return r;
7584: }
1.1 noro 7585: }
7586:
7587: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
7588: {
7589: int j,k,len;
7590: UINT *p;
7591: mpz_t c;
7592: NDV r;
7593: NMV mr0,mr;
7594:
7595: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
7596: if ( !len ) return 0;
7597: else {
7598: mr0 = (NMV)MALLOC(nmv_adv*len);
7599: #if 0
7600: ndv_alloc += nmv_adv*len;
7601: #endif
7602: mr = mr0;
7603: p = s0vect;
7604: for ( j = k = 0; j < col; j++, p += nd_wpd )
7605: if ( !rhead[j] ) {
7606: c[0] = vect[k++][0];
7607: if ( mpz_sgn(c) ) {
7608: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
7609: }
7610: }
7611: MKNDV(nd_nvar,mr0,len,r);
7612: return r;
7613: }
7614: }
7615:
7616: /* for plain vector */
7617:
7618: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
7619: {
7620: int j,k,len;
7621: UINT *p;
7622: Z c;
7623: NDV r;
7624: NMV mr0,mr;
7625:
7626: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7627: if ( !len ) return 0;
7628: else {
7629: mr0 = (NMV)MALLOC(nmv_adv*len);
7630: #if 0
7631: ndv_alloc += nmv_adv*len;
7632: #endif
7633: mr = mr0;
7634: p = s0vect;
7635: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
7636: if ( (c = vect[k]) != 0 ) {
7637: if ( !INT(c) )
1.6 noro 7638: error("plain_vect_to_ndv_q : components must be integers");
7639: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 7640: }
7641: MKNDV(nd_nvar,mr0,len,r);
7642: return r;
7643: }
7644: }
7645:
7646: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
7647: {
7648: ND_pairs t;
7649: NODE sp0,sp;
7650: int stat;
7651: ND spol;
7652:
7653: for ( t = l; t; t = NEXT(t) ) {
7654: stat = nd_sp(m,trace,t,&spol);
7655: if ( !stat ) return 0;
7656: if ( spol ) {
7657: add_pbucket_symbolic(bucket,spol);
7658: }
7659: }
7660: return 1;
7661: }
7662:
7663: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
7664: {
7665: NODE rp0,rp;
7666: NM mul,head,s0,s;
7667: int index,col,i,sugar;
7668: RHist h;
7669: UINT *s0v,*p;
7670: NM_ind_pair pair;
7671: ND red;
7672: NDV *ps;
7673:
7674: s0 = 0; rp0 = 0; col = 0;
7675: if ( nd_demand )
7676: ps = trace?nd_ps_trace_sym:nd_ps_sym;
7677: else
7678: ps = trace?nd_ps_trace:nd_ps;
7679: while ( 1 ) {
7680: head = remove_head_pbucket_symbolic(bucket);
7681: if ( !head ) break;
7682: if ( !s0 ) s0 = head;
7683: else NEXT(s) = head;
7684: s = head;
7685: index = ndl_find_reducer(DL(head));
7686: if ( index >= 0 ) {
7687: h = nd_psh[index];
7688: NEWNM(mul);
7689: ndl_sub(DL(head),DL(h),DL(mul));
7690: if ( ndl_check_bound2(index,DL(mul)) )
7691: return 0;
7692: sugar = TD(DL(mul))+SG(ps[index]);
7693: MKNM_ind_pair(pair,mul,index,sugar);
7694: red = ndv_mul_nm_symbolic(mul,ps[index]);
7695: add_pbucket_symbolic(bucket,nd_remove_head(red));
7696: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
7697: }
7698: col++;
7699: }
7700: if ( rp0 ) NEXT(rp) = 0;
7701: NEXT(s) = 0;
7702: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
7703: for ( i = 0, p = s0v, s = s0; i < col;
7704: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
7705: *s0vect = s0v;
7706: *r = rp0;
7707: return col;
7708: }
7709:
7710: void print_ndp(ND_pairs l)
7711: {
7712: ND_pairs t;
7713:
7714: for ( t = l; t; t = NEXT(t) )
7715: printf("[%d,%d] ",t->i1,t->i2);
7716: printf("\n");
7717: }
7718:
7719: NODE nd_f4(int m,int checkonly,int **indp)
7720: {
7721: int i,nh,stat,index,f4red;
7722: NODE r,g,tn0,tn,node;
7723: ND_pairs d,l,t,ll0,ll,lh;
7724: LIST l0,l1;
7725: ND spol,red;
7726: NDV nf,redv;
7727: NM s0,s;
7728: NODE rp0,srp0,nflist,nzlist,nzlist_t;
7729: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
7730: UINT c;
7731: UINT **spmat;
7732: UINT *s0vect,*svect,*p,*v;
7733: int *colstat;
7734: IndArray *imat;
7735: int *rhead;
7736: int spcol,sprow;
7737: int sugar,sugarh;
7738: PGeoBucket bucket;
7739: struct oEGT eg0,eg1,eg_f4;
7740: Z i1,i2,sugarq;
1.12 noro 7741:
7742: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 7743: #if 0
7744: ndv_alloc = 0;
7745: #endif
1.11 noro 7746: Nf4_red=0;
1.1 noro 7747: g = 0; d = 0;
7748: for ( i = 0; i < nd_psn; i++ ) {
7749: d = update_pairs(d,g,i,0);
7750: g = update_base(g,i);
7751: }
7752: nzlist = 0;
7753: nzlist_t = nd_nzlist;
7754: f4red = 1;
7755: nd_last_nonzero = 0;
7756: while ( d ) {
7757: get_eg(&eg0);
7758: l = nd_minsugarp(d,&d);
7759: sugar = nd_sugarweight?l->sugar2:SG(l);
7760: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7761: if ( nzlist_t ) {
7762: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 7763: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 7764: tn = BDY((LIST)ARG1(node));
7765: if ( !tn ) {
7766: nzlist_t = NEXT(nzlist_t);
7767: continue;
7768: }
7769: /* tn = [[i1,i2],...] */
7770: lh = nd_ipairtospair(tn);
7771: }
7772: bucket = create_pbucket();
7773: stat = nd_sp_f4(m,0,l,bucket);
7774: if ( !stat ) {
7775: for ( t = l; NEXT(t); t = NEXT(t) );
7776: NEXT(t) = d; d = l;
7777: d = nd_reconstruct(0,d);
7778: continue;
7779: }
7780: if ( bucket->m < 0 ) continue;
7781: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7782: if ( !col ) {
7783: for ( t = l; NEXT(t); t = NEXT(t) );
7784: NEXT(t) = d; d = l;
7785: d = nd_reconstruct(0,d);
7786: continue;
7787: }
1.12 noro 7788: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 7789: if ( DP_Print )
1.6 noro 7790: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 7791: sugar,eg_f4.exectime);
1.1 noro 7792: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
7793: if ( checkonly && nflist ) return 0;
7794: /* adding new bases */
7795: if ( nflist ) nd_last_nonzero = f4red;
7796: for ( r = nflist; r; r = NEXT(r) ) {
7797: nf = (NDV)BDY(r);
7798: ndv_removecont(m,nf);
7799: if ( !m && nd_nalg ) {
7800: ND nf1;
7801:
7802: nf1 = ndvtond(m,nf);
7803: nd_monic(0,&nf1);
7804: nd_removecont(m,nf1);
7805: nf = ndtondv(m,nf1);
7806: }
1.24 noro 7807: nh = ndv_newps(m,nf,0);
1.1 noro 7808: d = update_pairs(d,g,nh,0);
7809: g = update_base(g,nh);
7810: }
7811: if ( DP_Print ) {
7812: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
7813: }
7814: if ( nd_gentrace ) {
7815: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
7816: NEXTNODE(tn0,tn);
1.6 noro 7817: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 7818: node = mknode(2,i1,i2); MKLIST(l0,node);
7819: BDY(tn) = l0;
7820: }
7821: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 7822: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 7823: MKNODE(node,l1,nzlist); nzlist = node;
7824: }
7825: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
7826: f4red++;
7827: if ( nd_f4red && f4red > nd_f4red ) break;
7828: if ( nd_rank0 && !nflist ) break;
7829: }
7830: if ( nd_gentrace ) {
7831: MKLIST(l0,reverse_node(nzlist));
7832: MKNODE(nd_alltracelist,l0,0);
7833: }
7834: #if 0
7835: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7836: #endif
1.12 noro 7837: if ( DP_Print ) {
7838: fprintf(asir_out,"number of red=%d,",Nf4_red);
7839: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
7840: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
7841: }
1.1 noro 7842: conv_ilist(nd_demand,0,g,indp);
7843: return g;
7844: }
7845:
7846: NODE nd_f4_trace(int m,int **indp)
7847: {
7848: int i,nh,stat,index;
7849: NODE r,g;
7850: ND_pairs d,l,l0,t;
7851: ND spol,red;
7852: NDV nf,redv,nfqv,nfv;
7853: NM s0,s;
7854: NODE rp0,srp0,nflist;
7855: int nsp,nred,col,rank,len,k,j,a;
7856: UINT c;
7857: UINT **spmat;
7858: UINT *s0vect,*svect,*p,*v;
7859: int *colstat;
7860: IndArray *imat;
7861: int *rhead;
7862: int spcol,sprow;
7863: int sugar;
7864: PGeoBucket bucket;
7865: struct oEGT eg0,eg1,eg_f4;
7866:
7867: g = 0; d = 0;
7868: for ( i = 0; i < nd_psn; i++ ) {
7869: d = update_pairs(d,g,i,0);
7870: g = update_base(g,i);
7871: }
7872: while ( d ) {
7873: get_eg(&eg0);
7874: l = nd_minsugarp(d,&d);
7875: sugar = SG(l);
7876: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7877: bucket = create_pbucket();
7878: stat = nd_sp_f4(m,0,l,bucket);
7879: if ( !stat ) {
7880: for ( t = l; NEXT(t); t = NEXT(t) );
7881: NEXT(t) = d; d = l;
7882: d = nd_reconstruct(1,d);
7883: continue;
7884: }
7885: if ( bucket->m < 0 ) continue;
7886: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7887: if ( !col ) {
7888: for ( t = l; NEXT(t); t = NEXT(t) );
7889: NEXT(t) = d; d = l;
7890: d = nd_reconstruct(1,d);
7891: continue;
7892: }
7893: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
7894: if ( DP_Print )
1.3 noro 7895: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 7896: sugar,eg_f4.exectime);
1.1 noro 7897: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
7898: if ( !l0 ) continue;
7899: l = l0;
7900:
7901: /* over Q */
7902: bucket = create_pbucket();
7903: stat = nd_sp_f4(0,1,l,bucket);
7904: if ( !stat ) {
7905: for ( t = l; NEXT(t); t = NEXT(t) );
7906: NEXT(t) = d; d = l;
7907: d = nd_reconstruct(1,d);
7908: continue;
7909: }
7910: if ( bucket->m < 0 ) continue;
7911: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7912: if ( !col ) {
7913: for ( t = l; NEXT(t); t = NEXT(t) );
7914: NEXT(t) = d; d = l;
7915: d = nd_reconstruct(1,d);
7916: continue;
7917: }
7918: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7919: /* adding new bases */
7920: for ( r = nflist; r; r = NEXT(r) ) {
7921: nfqv = (NDV)BDY(r);
7922: ndv_removecont(0,nfqv);
1.6 noro 7923: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 7924: if ( nd_nalg ) {
7925: ND nf1;
7926:
7927: nf1 = ndvtond(m,nfqv);
7928: nd_monic(0,&nf1);
7929: nd_removecont(0,nf1);
7930: nfqv = ndtondv(0,nf1); nd_free(nf1);
7931: }
7932: nfv = ndv_dup(0,nfqv);
7933: ndv_mod(m,nfv);
7934: ndv_removecont(m,nfv);
1.24 noro 7935: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 7936: d = update_pairs(d,g,nh,0);
7937: g = update_base(g,nh);
7938: }
7939: }
7940: #if 0
7941: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7942: #endif
7943: conv_ilist(nd_demand,1,g,indp);
7944: return g;
7945: }
7946:
7947: int rref(matrix mat,int *sugar)
7948: {
7949: int row,col,i,j,k,l,s,wcol,wj;
7950: unsigned long bj;
7951: unsigned long **a;
7952: unsigned long *ai,*ak,*as,*t;
7953: int *pivot;
7954:
7955: row = mat->row;
7956: col = mat->col;
7957: a = mat->a;
7958: wcol = (col+BLEN-1)/BLEN;
7959: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7960: i = 0;
7961: for ( j = 0; j < col; j++ ) {
7962: wj = j/BLEN; bj = 1L<<(j%BLEN);
7963: for ( k = i; k < row; k++ )
7964: if ( a[k][wj] & bj ) break;
7965: if ( k == row ) continue;
7966: pivot[i] = j;
7967: if ( k != i ) {
7968: t = a[i]; a[i] = a[k]; a[k] = t;
7969: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7970: }
7971: ai = a[i];
7972: for ( k = i+1; k < row; k++ ) {
7973: ak = a[k];
7974: if ( ak[wj] & bj ) {
7975: for ( l = wj; l < wcol; l++ )
7976: ak[l] ^= ai[l];
7977: sugar[k] = MAX(sugar[k],sugar[i]);
7978: }
7979: }
7980: i++;
7981: }
7982: for ( k = i-1; k >= 0; k-- ) {
7983: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
7984: ak = a[k];
7985: for ( s = 0; s < k; s++ ) {
7986: as = a[s];
7987: if ( as[wj] & bj ) {
7988: for ( l = wj; l < wcol; l++ )
7989: as[l] ^= ak[l];
7990: sugar[s] = MAX(sugar[s],sugar[k]);
7991: }
7992: }
7993: }
7994: return i;
7995: }
7996:
7997: void print_matrix(matrix mat)
7998: {
7999: int row,col,i,j;
8000: unsigned long *ai;
8001:
8002: row = mat->row;
8003: col = mat->col;
8004: printf("%d x %d\n",row,col);
8005: for ( i = 0; i < row; i++ ) {
8006: ai = mat->a[i];
8007: for ( j = 0; j < col; j++ ) {
8008: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8009: else putchar('0');
8010: }
8011: putchar('\n');
8012: }
8013: }
8014:
8015: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8016:
8017: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8018: {
8019: int row,col,wcol,wj,i,j;
8020: unsigned long bj;
8021: unsigned long *ai;
8022: unsigned long **a;
8023: int len;
8024: int *pos;
8025:
8026: row = mat->row;
8027: col = mat->col;
8028: wcol = (col+BLEN-1)/BLEN;
8029: pos = (int *)MALLOC(wcol*sizeof(int));
8030: bzero(pos,wcol*sizeof(int));
8031: for ( i = j = 0; i < wcol; i++ )
8032: if ( v[i] ) pos[j++] = i;;
8033: len = j;
8034: wj = rhead/BLEN;
8035: bj = 1L<<rhead%BLEN;
8036: a = mat->a;
8037: for ( i = 0; i < row; i++ ) {
8038: ai = a[i];
8039: if ( ai[wj]&bj ) {
8040: for ( j = 0; j < len; j++ )
8041: ai[pos[j]] ^= v[pos[j]];
8042: sugar[i] = MAX(sugar[i],rsugar);
8043: }
8044: }
8045: }
8046:
8047: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8048: {
8049: int nsp,nred,i,i0,k,rank,row;
8050: NODE r0,rp;
8051: ND_pairs sp;
8052: ND spol;
8053: NM_ind_pair rt;
8054: int *s0hash;
8055: UINT *s;
8056: int *pivot,*sugar,*head;
8057: matrix mat;
8058: NM m;
8059: NODE r;
8060: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8061: int rhead,rsugar,size;
8062: unsigned long *v;
8063:
8064: get_eg(&eg0);
8065: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8066: nred = length(rp0);
8067: mat = alloc_matrix(nsp,col);
8068: s0hash = (int *)MALLOC(col*sizeof(int));
8069: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8070: s0hash[i] = ndl_hash_value(s);
8071:
8072: sugar = (int *)MALLOC(nsp*sizeof(int));
8073: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8074: nd_sp(2,0,sp,&spol);
8075: if ( spol ) {
8076: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8077: sugar[i] = SG(spol);
8078: i++;
8079: }
8080: }
8081: mat->row = i;
8082: if ( DP_Print ) {
8083: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8084: }
8085: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8086: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8087: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8088: rt = (NM_ind_pair)BDY(rp);
8089: bzero(v,size);
8090: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8091: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8092: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8093: }
8094:
8095: get_eg(&eg1);
8096: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8097: rank = rref(mat,sugar);
8098:
8099: for ( i = 0, r0 = 0; i < rank; i++ ) {
8100: NEXTNODE(r0,r);
8101: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8102: SG((NDV)BDY(r)) = sugar[i];
8103: }
8104: if ( r0 ) NEXT(r) = 0;
8105: get_eg(&eg2);
8106: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8107: if ( DP_Print ) {
8108: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8109: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8110: fflush(asir_out);
8111: }
8112: return r0;
8113: }
8114:
8115:
8116: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8117: {
8118: IndArray *imat;
1.11 noro 8119: int nsp,nred,i,start;
1.1 noro 8120: int *rhead;
8121: NODE r0,rp;
8122: ND_pairs sp;
8123: NM_ind_pair *rvect;
8124: UINT *s;
8125: int *s0hash;
1.11 noro 8126: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8127:
8128: if ( m == 2 && nd_rref2 )
8129: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8130:
8131: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8132: nred = length(rp0);
8133: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8134: rhead = (int *)MALLOC(col*sizeof(int));
8135: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8136:
8137: /* construction of index arrays */
1.11 noro 8138: get_eg(&eg0);
1.1 noro 8139: if ( DP_Print ) {
1.11 noro 8140: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8141: fflush(asir_out);
1.1 noro 8142: }
8143: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8144: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8145: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8146: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8147: rhead[imat[i]->head] = 1;
1.11 noro 8148: start = imat[i]->head;
8149: }
1.12 noro 8150: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8151: if ( DP_Print ) {
8152: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8153: fflush(asir_out);
1.1 noro 8154: }
8155: if ( m > 0 )
1.7 noro 8156: #if SIZEOF_LONG==8
1.1 noro 8157: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8158: #else
8159: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8160: #endif
8161: else if ( m == -1 )
8162: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8163: else if ( m == -2 )
8164: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8165: else
8166: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8167: return r0;
8168: }
8169:
8170: /* for Fp, 2<=p<2^16 */
8171:
8172: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8173: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8174: {
8175: int spcol,sprow,a;
8176: int i,j,k,l,rank;
8177: NODE r0,r;
8178: ND_pairs sp;
8179: ND spol;
8180: UINT **spmat;
8181: UINT *svect,*v;
8182: int *colstat;
8183: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8184: int maxrs;
8185: int *spsugar;
8186: ND_pairs *spactive;
8187:
8188: spcol = col-nred;
8189: get_eg(&eg0);
8190: /* elimination (1st step) */
8191: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8192: svect = (UINT *)MALLOC(col*sizeof(UINT));
8193: spsugar = (int *)MALLOC(nsp*sizeof(int));
8194: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8195: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8196: nd_sp(m,0,sp,&spol);
8197: if ( !spol ) continue;
8198: nd_to_vect(m,s0vect,col,spol,svect);
8199: if ( m == -1 )
8200: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8201: else
8202: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
8203: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8204: if ( i < col ) {
8205: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8206: for ( j = k = 0; j < col; j++ )
8207: if ( !rhead[j] ) v[k++] = svect[j];
8208: spsugar[sprow] = MAX(maxrs,SG(spol));
8209: if ( nz )
8210: spactive[sprow] = sp;
8211: sprow++;
8212: }
8213: nd_free(spol);
8214: }
8215: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8216: if ( DP_Print ) {
1.5 noro 8217: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8218: fflush(asir_out);
8219: }
8220: /* free index arrays */
8221: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8222:
8223: /* elimination (2nd step) */
8224: colstat = (int *)MALLOC(spcol*sizeof(int));
8225: if ( m == -1 )
8226: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8227: else
8228: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8229: r0 = 0;
8230: for ( i = 0; i < rank; i++ ) {
8231: NEXTNODE(r0,r); BDY(r) =
8232: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8233: SG((NDV)BDY(r)) = spsugar[i];
8234: GCFREE(spmat[i]);
8235: }
8236: if ( r0 ) NEXT(r) = 0;
8237:
8238: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8239: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8240: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8241: if ( DP_Print ) {
1.5 noro 8242: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8243: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8244: nsp,nred,sprow,spcol,rank);
1.5 noro 8245: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8246: }
8247: if ( nz ) {
8248: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8249: if ( rank > 0 ) {
8250: NEXT(spactive[rank-1]) = 0;
8251: *nz = spactive[0];
8252: } else
8253: *nz = 0;
8254: }
8255: return r0;
8256: }
8257:
8258:
8259: /* for small finite fields */
8260:
8261: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8262: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8263: {
8264: int spcol,sprow,a;
8265: int i,j,k,l,rank;
8266: NODE r0,r;
8267: ND_pairs sp;
8268: ND spol;
8269: UINT **spmat;
8270: UINT *svect,*v;
8271: int *colstat;
8272: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8273: int maxrs;
8274: int *spsugar;
8275: ND_pairs *spactive;
8276:
8277: spcol = col-nred;
8278: get_eg(&eg0);
8279: /* elimination (1st step) */
8280: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8281: svect = (UINT *)MALLOC(col*sizeof(UINT));
8282: spsugar = (int *)MALLOC(nsp*sizeof(int));
8283: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8284: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8285: nd_sp(m,0,sp,&spol);
8286: if ( !spol ) continue;
8287: nd_to_vect(m,s0vect,col,spol,svect);
8288: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8289: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8290: if ( i < col ) {
8291: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8292: for ( j = k = 0; j < col; j++ )
8293: if ( !rhead[j] ) v[k++] = svect[j];
8294: spsugar[sprow] = MAX(maxrs,SG(spol));
8295: if ( nz )
8296: spactive[sprow] = sp;
8297: sprow++;
8298: }
8299: nd_free(spol);
8300: }
8301: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8302: if ( DP_Print ) {
1.5 noro 8303: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8304: fflush(asir_out);
8305: }
8306: /* free index arrays */
8307: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8308:
8309: /* elimination (2nd step) */
8310: colstat = (int *)MALLOC(spcol*sizeof(int));
8311: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8312: r0 = 0;
8313: for ( i = 0; i < rank; i++ ) {
8314: NEXTNODE(r0,r); BDY(r) =
8315: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8316: SG((NDV)BDY(r)) = spsugar[i];
8317: GCFREE(spmat[i]);
8318: }
8319: if ( r0 ) NEXT(r) = 0;
8320:
8321: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8322: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8323: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8324: if ( DP_Print ) {
1.5 noro 8325: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8326: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8327: nsp,nred,sprow,spcol,rank);
1.5 noro 8328: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8329: }
8330: if ( nz ) {
8331: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8332: if ( rank > 0 ) {
8333: NEXT(spactive[rank-1]) = 0;
8334: *nz = spactive[0];
8335: } else
8336: *nz = 0;
8337: }
8338: return r0;
8339: }
8340:
8341: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8342: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8343: {
8344: int spcol,sprow,a;
8345: int i,j,k,l,rank;
8346: NODE r0,r;
8347: ND_pairs sp;
8348: ND spol;
8349: mpz_t **spmat;
8350: mpz_t *svect,*v;
8351: int *colstat;
8352: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8353: int maxrs;
8354: int *spsugar;
8355: pointer *w;
8356:
8357: spcol = col-nred;
8358: get_eg(&eg0);
8359: /* elimination (1st step) */
8360: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8361: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8362: spsugar = (int *)MALLOC(nsp*sizeof(int));
8363: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8364: nd_sp(m,trace,sp,&spol);
8365: if ( !spol ) continue;
8366: nd_to_vect_lf(s0vect,col,spol,svect);
8367: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8368: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8369: if ( i < col ) {
8370: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8371: for ( j = k = 0; j < col; j++ )
8372: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8373: spsugar[sprow] = MAX(maxrs,SG(spol));
8374: sprow++;
8375: }
8376: /* nd_free(spol); */
8377: }
8378: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8379: if ( DP_Print ) {
1.5 noro 8380: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8381: fflush(asir_out);
8382: }
8383: /* free index arrays */
8384: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8385:
8386: /* elimination (2nd step) */
8387: colstat = (int *)MALLOC(spcol*sizeof(int));
8388: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8389: w = (pointer *)MALLOC(rank*sizeof(pointer));
8390: for ( i = 0; i < rank; i++ ) {
8391: #if 0
8392: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8393: SG((NDV)w[rank-i-1]) = spsugar[i];
8394: #else
8395: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8396: SG((NDV)w[i]) = spsugar[i];
8397: #endif
8398: /* GCFREE(spmat[i]); */
8399:
8400: }
8401: #if 0
8402: qsort(w,rank,sizeof(NDV),
8403: (int (*)(const void *,const void *))ndv_compare);
8404: #endif
8405: r0 = 0;
8406: for ( i = 0; i < rank; i++ ) {
8407: NEXTNODE(r0,r); BDY(r) = w[i];
8408: }
8409: if ( r0 ) NEXT(r) = 0;
8410:
8411: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8412: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8413: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8414: if ( DP_Print ) {
1.5 noro 8415: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8416: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8417: nsp,nred,sprow,spcol,rank);
1.5 noro 8418: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8419: }
8420: return r0;
8421: }
8422:
8423: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8424: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8425: {
8426: int spcol,sprow,a;
8427: int i,j,k,l,rank;
8428: NODE r0,r;
8429: ND_pairs sp;
8430: ND spol;
8431: Z **spmat;
8432: Z *svect,*v;
8433: int *colstat;
8434: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8435: int maxrs;
8436: int *spsugar;
8437: pointer *w;
8438:
8439: spcol = col-nred;
8440: get_eg(&eg0);
8441: /* elimination (1st step) */
8442: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8443: svect = (Z *)MALLOC(col*sizeof(Q));
8444: spsugar = (int *)MALLOC(nsp*sizeof(int));
8445: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8446: nd_sp(0,trace,sp,&spol);
8447: if ( !spol ) continue;
8448: nd_to_vect_q(s0vect,col,spol,svect);
8449: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8450: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8451: if ( i < col ) {
8452: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8453: for ( j = k = 0; j < col; j++ )
8454: if ( !rhead[j] ) v[k++] = svect[j];
8455: spsugar[sprow] = MAX(maxrs,SG(spol));
8456: sprow++;
8457: }
8458: /* nd_free(spol); */
8459: }
8460: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8461: if ( DP_Print ) {
1.5 noro 8462: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8463: fflush(asir_out);
8464: }
8465: /* free index arrays */
8466: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8467:
8468: /* elimination (2nd step) */
8469: colstat = (int *)MALLOC(spcol*sizeof(int));
8470: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8471: w = (pointer *)MALLOC(rank*sizeof(pointer));
8472: for ( i = 0; i < rank; i++ ) {
8473: #if 0
8474: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8475: SG((NDV)w[rank-i-1]) = spsugar[i];
8476: #else
8477: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8478: SG((NDV)w[i]) = spsugar[i];
8479: #endif
8480: /* GCFREE(spmat[i]); */
8481: }
8482: #if 0
8483: qsort(w,rank,sizeof(NDV),
8484: (int (*)(const void *,const void *))ndv_compare);
8485: #endif
8486: r0 = 0;
8487: for ( i = 0; i < rank; i++ ) {
8488: NEXTNODE(r0,r); BDY(r) = w[i];
8489: }
8490: if ( r0 ) NEXT(r) = 0;
8491:
8492: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8493: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8494: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8495: if ( DP_Print ) {
1.5 noro 8496: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8497: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8498: nsp,nred,sprow,spcol,rank);
1.5 noro 8499: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8500: }
8501: return r0;
8502: }
8503:
8504: FILE *nd_write,*nd_read;
8505:
8506: void nd_send_int(int a) {
8507: write_int(nd_write,(unsigned int *)&a);
8508: }
8509:
8510: void nd_send_intarray(int *p,int len) {
8511: write_intarray(nd_write,(unsigned int *)p,len);
8512: }
8513:
8514: int nd_recv_int() {
8515: int a;
8516:
8517: read_int(nd_read,(unsigned int *)&a);
8518: return a;
8519: }
8520:
8521: void nd_recv_intarray(int *p,int len) {
8522: read_intarray(nd_read,(unsigned int *)p,len);
8523: }
8524:
8525: void nd_send_ndv(NDV p) {
8526: int len,i;
8527: NMV m;
8528:
8529: if ( !p ) nd_send_int(0);
8530: else {
8531: len = LEN(p);
8532: nd_send_int(len);
8533: m = BDY(p);
8534: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8535: nd_send_int(CM(m));
8536: nd_send_intarray((int *)DL(m),nd_wpd);
8537: }
8538: }
8539: }
8540:
8541: void nd_send_nd(ND p) {
8542: int len,i;
8543: NM m;
8544:
8545: if ( !p ) nd_send_int(0);
8546: else {
8547: len = LEN(p);
8548: nd_send_int(len);
8549: m = BDY(p);
8550: for ( i = 0; i < len; i++, m = NEXT(m) ) {
8551: nd_send_int(CM(m));
8552: nd_send_intarray((int *)DL(m),nd_wpd);
8553: }
8554: }
8555: }
8556:
8557: NDV nd_recv_ndv()
8558: {
8559: int len,i;
8560: NMV m,m0;
8561: NDV r;
8562:
8563: len = nd_recv_int();
8564: if ( !len ) return 0;
8565: else {
8566: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8567: #if 0
8568: ndv_alloc += len*nmv_adv;
8569: #endif
8570: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8571: CM(m) = nd_recv_int();
8572: nd_recv_intarray((int *)DL(m),nd_wpd);
8573: }
8574: MKNDV(nd_nvar,m0,len,r);
8575: return r;
8576: }
8577: }
8578:
8579: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
8580: {
8581: int i,j,t,c,rank,inv;
8582: int *ci,*ri;
8583: Z dn;
8584: MAT m,nm;
8585:
8586: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
8587: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
8588: for ( i = 0; i < row; i++ )
8589: for ( j = 0; j < col; j++ )
8590: mat0[i][j] = 0;
8591: c = col-rank;
8592: for ( i = 0; i < rank; i++ ) {
8593: mat0[i][ri[i]] = dn;
8594: for ( j = 0; j < c; j++ )
8595: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
8596: }
8597: return rank;
8598: }
8599:
8600: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
8601: {
8602: int i,j,k,l,inv,a,rank,s;
8603: unsigned int *t,*pivot,*pk;
8604: unsigned int **mat;
8605: ND_pairs pair;
8606:
8607: mat = (unsigned int **)mat0;
8608: for ( rank = 0, j = 0; j < col; j++ ) {
8609: for ( i = rank; i < row; i++ )
8610: mat[i][j] %= md;
8611: for ( i = rank; i < row; i++ )
8612: if ( mat[i][j] )
8613: break;
8614: if ( i == row ) {
8615: colstat[j] = 0;
8616: continue;
8617: } else
8618: colstat[j] = 1;
8619: if ( i != rank ) {
8620: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8621: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8622: if ( spactive ) {
8623: pair = spactive[i]; spactive[i] = spactive[rank];
8624: spactive[rank] = pair;
8625: }
8626: }
8627: pivot = mat[rank];
8628: s = sugar[rank];
8629: inv = invm(pivot[j],md);
8630: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8631: if ( *pk ) {
8632: if ( *pk >= (unsigned int)md )
8633: *pk %= md;
8634: DMAR(*pk,inv,0,md,*pk)
8635: }
8636: for ( i = rank+1; i < row; i++ ) {
8637: t = mat[i];
8638: if ( (a = t[j]) != 0 ) {
8639: sugar[i] = MAX(sugar[i],s);
8640: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8641: }
8642: }
8643: rank++;
8644: }
8645: for ( j = col-1, l = rank-1; j >= 0; j-- )
8646: if ( colstat[j] ) {
8647: pivot = mat[l];
8648: s = sugar[l];
8649: for ( i = 0; i < l; i++ ) {
8650: t = mat[i];
8651: t[j] %= md;
8652: if ( (a = t[j]) != 0 ) {
8653: sugar[i] = MAX(sugar[i],s);
8654: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8655: }
8656: }
8657: l--;
8658: }
8659: for ( j = 0, l = 0; l < rank; j++ )
8660: if ( colstat[j] ) {
8661: t = mat[l];
8662: for ( k = j; k < col; k++ )
8663: if ( t[k] >= (unsigned int)md )
8664: t[k] %= md;
8665: l++;
8666: }
8667: return rank;
8668: }
8669:
8670:
1.7 noro 8671: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 8672: {
1.7 noro 8673: int i,j,k,l,inv,a,rank,s;
8674: unsigned int *t,*pivot,*pk;
8675: unsigned int **mat;
8676:
8677: mat = (unsigned int **)mat0;
8678: for ( rank = 0, j = 0; j < col; j++ ) {
8679: for ( i = rank; i < row; i++ )
8680: if ( mat[i][j] )
8681: break;
8682: if ( i == row ) {
8683: colstat[j] = 0;
8684: continue;
8685: } else
8686: colstat[j] = 1;
8687: if ( i != rank ) {
8688: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8689: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8690: }
8691: pivot = mat[rank];
8692: s = sugar[rank];
8693: inv = _invsf(pivot[j]);
8694: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8695: if ( *pk )
8696: *pk = _mulsf(*pk,inv);
8697: for ( i = rank+1; i < row; i++ ) {
8698: t = mat[i];
8699: if ( (a = t[j]) != 0 ) {
8700: sugar[i] = MAX(sugar[i],s);
8701: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8702: }
8703: }
8704: rank++;
8705: }
8706: for ( j = col-1, l = rank-1; j >= 0; j-- )
8707: if ( colstat[j] ) {
8708: pivot = mat[l];
8709: s = sugar[l];
8710: for ( i = 0; i < l; i++ ) {
8711: t = mat[i];
8712: if ( (a = t[j]) != 0 ) {
8713: sugar[i] = MAX(sugar[i],s);
8714: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8715: }
8716: }
8717: l--;
8718: }
8719: return rank;
8720: }
1.1 noro 8721:
1.7 noro 8722: int ndv_ishomo(NDV p)
8723: {
8724: NMV m;
8725: int len,h;
1.1 noro 8726:
8727: if ( !p ) return 1;
8728: len = LEN(p);
8729: m = BDY(p);
8730: h = TD(DL(m));
8731: NMV_ADV(m);
8732: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 8733: if ( TD(DL(m)) != h ) {
8734: return 0;
8735: }
1.1 noro 8736: return 1;
8737: }
8738:
8739: void ndv_save(NDV p,int index)
8740: {
8741: FILE *s;
8742: char name[BUFSIZ];
8743: short id;
8744: int nv,sugar,len,n,i,td,e,j;
8745: NMV m;
8746: unsigned int *dl;
8747: int mpos;
8748:
8749: sprintf(name,"%s/%d",Demand,index);
8750: s = fopen(name,"w");
8751: savevl(s,0);
8752: if ( !p ) {
8753: saveobj(s,0);
8754: return;
8755: }
8756: id = O_DP;
8757: nv = NV(p);
8758: sugar = SG(p);
8759: len = LEN(p);
8760: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
8761: write_int(s,(unsigned int *)&len);
8762:
8763: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8764: saveobj(s,(Obj)CZ(m));
1.1 noro 8765: dl = DL(m);
8766: td = TD(dl);
8767: write_int(s,(unsigned int *)&td);
8768: for ( j = 0; j < nv; j++ ) {
8769: e = GET_EXP(dl,j);
8770: write_int(s,(unsigned int *)&e);
8771: }
8772: if ( nd_module ) {
8773: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
8774: }
8775: }
8776: fclose(s);
8777: }
8778:
8779: void nd_save_mod(ND p,int index)
8780: {
8781: FILE *s;
8782: char name[BUFSIZ];
8783: int nv,sugar,len,c;
8784: NM m;
8785:
8786: sprintf(name,"%s/%d",Demand,index);
8787: s = fopen(name,"w");
8788: if ( !p ) {
8789: len = 0;
8790: write_int(s,(unsigned int *)&len);
8791: fclose(s);
8792: return;
8793: }
8794: nv = NV(p);
8795: sugar = SG(p);
8796: len = LEN(p);
8797: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
8798: for ( m = BDY(p); m; m = NEXT(m) ) {
8799: c = CM(m); write_int(s,(unsigned int *)&c);
8800: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
8801: }
8802: fclose(s);
8803: }
8804:
8805: NDV ndv_load(int index)
8806: {
8807: FILE *s;
8808: char name[BUFSIZ];
8809: short id;
8810: int nv,sugar,len,n,i,td,e,j;
8811: NDV d;
8812: NMV m0,m;
8813: unsigned int *dl;
8814: Obj obj;
8815: int mpos;
8816:
8817: sprintf(name,"%s/%d",Demand,index);
8818: s = fopen(name,"r");
8819: if ( !s ) return 0;
8820:
8821: skipvl(s);
8822: read_short(s,(unsigned short *)&id);
8823: if ( !id ) return 0;
8824: read_int(s,(unsigned int *)&nv);
8825: read_int(s,(unsigned int *)&sugar);
8826: read_int(s,(unsigned int *)&len);
8827:
8828: m0 = m = MALLOC(len*nmv_adv);
8829: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8830: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 8831: dl = DL(m);
8832: ndl_zero(dl);
8833: read_int(s,(unsigned int *)&td); TD(dl) = td;
8834: for ( j = 0; j < nv; j++ ) {
8835: read_int(s,(unsigned int *)&e);
8836: PUT_EXP(dl,j,e);
8837: }
8838: if ( nd_module ) {
8839: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
8840: }
8841: if ( nd_blockmask ) ndl_weight_mask(dl);
8842: }
8843: fclose(s);
8844: MKNDV(nv,m0,len,d);
8845: SG(d) = sugar;
8846: return d;
8847: }
8848:
8849: ND nd_load_mod(int index)
8850: {
8851: FILE *s;
8852: char name[BUFSIZ];
8853: int nv,sugar,len,i,c;
8854: ND d;
8855: NM m0,m;
8856:
8857: sprintf(name,"%s/%d",Demand,index);
8858: s = fopen(name,"r");
8859: /* if the file does not exist, it means p[index]=0 */
8860: if ( !s ) return 0;
8861:
8862: read_int(s,(unsigned int *)&nv);
8863: if ( !nv ) { fclose(s); return 0; }
8864:
8865: read_int(s,(unsigned int *)&sugar);
8866: read_int(s,(unsigned int *)&len);
8867: for ( m0 = 0, i = 0; i < len; i++ ) {
8868: NEXTNM(m0,m);
8869: read_int(s,(unsigned int *)&c); CM(m) = c;
8870: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
8871: }
8872: NEXT(m) = 0;
8873: MKND(nv,m0,len,d);
8874: SG(d) = sugar;
8875: fclose(s);
8876: return d;
8877: }
8878:
8879: void nd_det(int mod,MAT f,P *rp)
8880: {
8881: VL fv,tv;
8882: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
8883: pointer **m;
8884: P **w;
8885: P mp,r;
8886: NDV **dm;
8887: NDV *t,*mi,*mj;
8888: NDV d,s,mij,mjj;
8889: ND u;
8890: NMV nmv;
8891: UINT *bound;
8892: PGeoBucket bucket;
8893: struct order_spec *ord;
8894: Z dq,dt,ds;
8895: Z mone;
8896: Z gn,qn,dn0,nm,dn;
8897:
8898: create_order_spec(0,0,&ord);
8899: nd_init_ord(ord);
8900: get_vars((Obj)f,&fv);
8901: if ( f->row != f->col )
8902: error("nd_det : non-square matrix");
8903: n = f->row;
8904: m = f->body;
8905: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8906:
8907: if ( !nvar ) {
8908: if ( !mod )
8909: detp(CO,(P **)m,n,rp);
8910: else {
8911: w = (P **)almat_pointer(n,n);
8912: for ( i = 0; i < n; i++ )
8913: for ( j = 0; j < n; j++ )
8914: ptomp(mod,(P)m[i][j],&w[i][j]);
8915: detmp(CO,mod,w,n,&mp);
8916: mptop(mp,rp);
8917: }
8918: return;
8919: }
8920:
8921: if ( !mod ) {
8922: w = (P **)almat_pointer(n,n);
8923: dq = ONE;
8924: for ( i = 0; i < n; i++ ) {
8925: dn0 = ONE;
8926: for ( j = 0; j < n; j++ ) {
8927: if ( !m[i][j] ) continue;
8928: lgp(m[i][j],&nm,&dn);
1.6 noro 8929: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 8930: }
8931: if ( !UNIZ(dn0) ) {
8932: ds = dn0;
8933: for ( j = 0; j < n; j++ )
8934: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8935: mulz(dq,ds,&dt); dq = dt;
8936: } else
8937: for ( j = 0; j < n; j++ )
8938: w[i][j] = (P)m[i][j];
8939: }
8940: m = (pointer **)w;
8941: }
8942:
8943: for ( i = 0, max = 1; i < n; i++ )
8944: for ( j = 0; j < n; j++ )
8945: for ( tv = fv; tv; tv = NEXT(tv) ) {
8946: e = getdeg(tv->v,(P)m[i][j]);
8947: max = MAX(e,max);
8948: }
8949: nd_setup_parameters(nvar,max);
8950: dm = (NDV **)almat_pointer(n,n);
8951: for ( i = 0, max = 1; i < n; i++ )
8952: for ( j = 0; j < n; j++ ) {
8953: dm[i][j] = ptondv(CO,fv,m[i][j]);
8954: if ( mod ) ndv_mod(mod,dm[i][j]);
8955: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8956: }
8957: d = ptondv(CO,fv,(P)ONE);
8958: if ( mod ) ndv_mod(mod,d);
8959: chsgnz(ONE,&mone);
8960: for ( j = 0, sgn = 1; j < n; j++ ) {
8961: if ( DP_Print ) {
8962: fprintf(asir_out,".");
8963: }
8964: for ( i = j; i < n && !dm[i][j]; i++ );
8965: if ( i == n ) {
8966: *rp = 0;
8967: return;
8968: }
8969: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8970: for ( k = j; k < n; k++ )
8971: for ( l = j; l < n; l++ )
8972: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8973: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8974: }
8975: if ( k0 != j ) {
8976: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8977: sgn = -sgn;
8978: }
8979: if ( l0 != j ) {
8980: for ( k = j; k < n; k++ ) {
8981: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8982: }
8983: sgn = -sgn;
8984: }
8985: bound = nd_det_compute_bound(dm,n,j);
8986: for ( k = 0; k < nd_nvar; k++ )
8987: if ( bound[k]*2 > nd_mask0 ) break;
8988: if ( k < nd_nvar )
8989: nd_det_reconstruct(dm,n,j,d);
8990:
8991: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8992: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8993: mi = dm[i]; mij = mi[j];
8994: if ( mod )
8995: ndv_mul_c(mod,mij,mod-1);
8996: else
8997: ndv_mul_c_q(mij,mone);
8998: for ( k = j+1; k < n; k++ ) {
8999: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9000: bucket = create_pbucket();
9001: if ( mi[k] ) {
9002: nmv = BDY(mjj); len = LEN(mjj);
9003: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9004: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9005: add_pbucket(mod,bucket,u);
9006: }
9007: }
9008: if ( mj[k] && mij ) {
9009: nmv = BDY(mij); len = LEN(mij);
9010: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9011: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9012: add_pbucket(mod,bucket,u);
9013: }
9014: }
9015: u = nd_quo(mod,bucket,d);
9016: mi[k] = ndtondv(mod,u);
9017: }
9018: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9019: }
9020: d = mjj;
9021: }
9022: if ( DP_Print ) {
9023: fprintf(asir_out,"\n");
9024: }
9025: if ( sgn < 0 ) {
9026: if ( mod )
9027: ndv_mul_c(mod,d,mod-1);
9028: else
9029: ndv_mul_c_q(d,mone);
9030: }
9031: r = ndvtop(mod,CO,fv,d);
9032: if ( !mod && !UNIQ(dq) )
9033: divsp(CO,r,(P)dq,rp);
9034: else
9035: *rp = r;
9036: }
9037:
9038: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9039: {
9040: NM mr,mr0;
9041: NM tnm;
9042: NMV m;
9043: UINT *d0,*dt,*dm;
9044: int c,n,td,i,c1,c2,len;
9045: Z q;
9046: ND r;
9047:
9048: if ( !p ) return 0;
9049: else {
9050: n = NV(p); m = BDY(p); len = LEN(p);
9051: d0 = DL(m0);
9052: td = TD(d);
9053: mr0 = 0;
9054: NEWNM(tnm);
9055: if ( mod ) {
9056: c = CM(m0);
9057: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9058: ndl_add(DL(m),d0,DL(tnm));
9059: if ( ndl_reducible(DL(tnm),d) ) {
9060: NEXTNM(mr0,mr);
9061: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9062: ndl_copy(DL(tnm),DL(mr));
9063: }
9064: }
9065: } else {
1.6 noro 9066: q = CZ(m0);
1.1 noro 9067: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9068: ndl_add(DL(m),d0,DL(tnm));
9069: if ( ndl_reducible(DL(tnm),d) ) {
9070: NEXTNM(mr0,mr);
1.6 noro 9071: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9072: ndl_copy(DL(tnm),DL(mr));
9073: }
9074: }
9075: }
9076: if ( !mr0 )
9077: return 0;
9078: else {
9079: NEXT(mr) = 0;
9080: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9081: MKND(NV(p),mr0,len,r);
9082: SG(r) = SG(p) + TD(d0);
9083: return r;
9084: }
9085: }
9086: }
9087:
9088: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9089: {
9090: int i,obpe,oadv,h,k,l;
9091: static NM prev_nm_free_list;
9092: EPOS oepos;
9093:
9094: obpe = nd_bpe;
9095: oadv = nmv_adv;
9096: oepos = nd_epos;
9097: if ( obpe < 2 ) nd_bpe = 2;
9098: else if ( obpe < 3 ) nd_bpe = 3;
9099: else if ( obpe < 4 ) nd_bpe = 4;
9100: else if ( obpe < 5 ) nd_bpe = 5;
9101: else if ( obpe < 6 ) nd_bpe = 6;
9102: else if ( obpe < 8 ) nd_bpe = 8;
9103: else if ( obpe < 10 ) nd_bpe = 10;
9104: else if ( obpe < 16 ) nd_bpe = 16;
9105: else if ( obpe < 32 ) nd_bpe = 32;
9106: else error("nd_det_reconstruct : exponent too large");
9107:
9108: nd_setup_parameters(nd_nvar,0);
9109: prev_nm_free_list = _nm_free_list;
9110: _nm_free_list = 0;
9111: for ( k = j; k < n; k++ )
9112: for (l = j; l < n; l++ )
9113: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9114: ndv_realloc(d,obpe,oadv,oepos);
9115: prev_nm_free_list = 0;
9116: #if 0
9117: GC_gcollect();
9118: #endif
9119: }
9120:
9121: /* returns a UINT array containing degree bounds */
9122:
9123: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9124: {
9125: UINT *d0,*d1,*d,*t,*r;
9126: int k,l,i;
9127:
9128: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9129: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9130: for ( k = j; k < n; k++ )
9131: for ( l = j; l < n; l++ )
9132: if ( dm[k][l] ) {
9133: d = ndv_compute_bound(dm[k][l]);
9134: for ( i = 0; i < nd_nvar; i++ )
9135: d0[i] = MAX(d0[i],d[i]);
9136: }
9137: return d0;
9138: }
9139:
9140: DL nd_separate_d(UINT *d,UINT *trans)
9141: {
9142: int n,td,i,e,j;
9143: DL a;
9144:
9145: ndl_zero(trans);
9146: td = 0;
9147: for ( i = 0; i < nd_ntrans; i++ ) {
9148: e = GET_EXP(d,i);
9149: PUT_EXP(trans,i,e);
9150: td += MUL_WEIGHT(e,i);
9151: }
9152: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9153: /* homogenized */
9154: i = nd_nvar-1;
9155: e = GET_EXP(d,i);
9156: PUT_EXP(trans,i,e);
9157: td += MUL_WEIGHT(e,i);
9158: }
9159: TD(trans) = td;
9160: if ( nd_blockmask) ndl_weight_mask(trans);
9161: NEWDL(a,nd_nalg);
9162: td = 0;
9163: for ( i = 0; i < nd_nalg; i++ ) {
9164: j = nd_ntrans+i;
9165: e = GET_EXP(d,j);
9166: a->d[i] = e;
9167: td += e;
9168: }
9169: a->td = td;
9170: return a;
9171: }
9172:
9173: int nd_monic(int mod,ND *p)
9174: {
9175: UINT *trans,*t;
9176: DL alg;
9177: MP mp0,mp;
9178: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9179: ND r;
9180: DL dl;
9181: DP nm;
9182: NDV ndv;
9183: DAlg inv,cd;
9184: ND s,c;
9185: Z l,mul;
9186: Z ln;
9187: int n,ntrans,i,e,td,is_lc,len;
9188: NumberField nf;
9189: struct oEGT eg0,eg1;
9190:
9191: if ( !(nf = get_numberfield()) )
9192: error("nd_monic : current_numberfield is not set");
9193:
9194: /* Q coef -> DAlg coef */
9195: NEWNM(ma0); ma = ma0;
9196: m = BDY(*p);
9197: is_lc = 1;
9198: while ( 1 ) {
9199: NEWMP(mp0); mp = mp0;
1.6 noro 9200: mp->c = (Obj)CZ(m);
1.1 noro 9201: mp->dl = nd_separate_d(DL(m),DL(ma));
9202: NEWNM(mb);
9203: for ( m = NEXT(m); m; m = NEXT(m) ) {
9204: alg = nd_separate_d(DL(m),DL(mb));
9205: if ( !ndl_equal(DL(ma),DL(mb)) )
9206: break;
1.6 noro 9207: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9208: }
9209: NEXT(mp) = 0;
9210: MKDP(nd_nalg,mp0,nm);
9211: MKDAlg(nm,ONE,cd);
9212: if ( is_lc == 1 ) {
9213: /* if the lc is a rational number, we have nothing to do */
9214: if ( !mp0->dl->td )
9215: return 1;
9216:
9217: get_eg(&eg0);
9218: invdalg(cd,&inv);
9219: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9220: /* check the validity of inv */
9221: if ( mod && !remqi((Q)inv->dn,mod) )
9222: return 0;
9223: CA(ma) = nf->one;
9224: is_lc = 0;
9225: ln = ONE;
9226: } else {
9227: muldalg(cd,inv,&CA(ma));
9228: lcmz(ln,CA(ma)->dn,&ln);
9229: }
9230: if ( m ) {
9231: NEXT(ma) = mb; ma = mb;
9232: } else {
9233: NEXT(ma) = 0;
9234: break;
9235: }
9236: }
9237: /* l = lcm(denoms) */
9238: l = ln;
9239: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9240: divsz(l,CA(m)->dn,&mul);
1.1 noro 9241: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9242: NEXTNM(mr0,mr);
1.6 noro 9243: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9244: dl = mp->dl;
9245: td = TD(DL(m));
9246: ndl_copy(DL(m),DL(mr));
9247: for ( i = 0; i < nd_nalg; i++ ) {
9248: e = dl->d[i];
9249: PUT_EXP(DL(mr),i+nd_ntrans,e);
9250: td += MUL_WEIGHT(e,i+nd_ntrans);
9251: }
9252: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9253: TD(DL(mr)) = td;
9254: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9255: }
9256: }
9257: NEXT(mr) = 0;
9258: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9259: MKND(NV(*p),mr0,len,r);
9260: /* XXX */
9261: SG(r) = SG(*p);
9262: nd_free(*p);
9263: *p = r;
9264: return 1;
9265: }
9266:
9267: NODE reverse_node(NODE n)
9268: {
9269: NODE t,t1;
9270:
9271: for ( t = 0; n; n = NEXT(n) ) {
9272: MKNODE(t1,BDY(n),t); t = t1;
9273: }
9274: return t;
9275: }
9276:
9277: P ndc_div(int mod,union oNDC a,union oNDC b)
9278: {
9279: union oNDC c;
9280: int inv,t;
9281:
9282: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9283: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9284: else if ( mod ) {
9285: inv = invm(b.m,mod);
9286: DMAR(a.m,inv,0,mod,t); c.m = t;
9287: } else if ( nd_vc )
9288: divsp(nd_vc,a.p,b.p,&c.p);
9289: else
9290: divsz(a.z,b.z,&c.z);
9291: return ndctop(mod,c);
9292: }
9293:
9294: P ndctop(int mod,union oNDC c)
9295: {
9296: Z q;
9297: int e;
9298: GFS gfs;
9299:
9300: if ( mod == -1 ) {
9301: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9302: } else if ( mod == -2 ) {
1.10 noro 9303: q = c.z; return (P)q;
1.1 noro 9304: } else if ( mod > 0 ) {
1.6 noro 9305: STOZ(c.m,q); return (P)q;
1.1 noro 9306: } else
9307: return (P)c.p;
9308: }
9309:
9310: /* [0,0,0,cont] = p -> p/cont */
9311:
9312: void finalize_tracelist(int i,P cont)
9313: {
9314: LIST l;
9315: NODE node;
9316: Z iq;
9317:
9318: if ( !UNIQ(cont) ) {
9319: node = mknode(4,NULLP,NULLP,NULLP,cont);
9320: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9321: nd_tracelist = node;
9322: }
1.6 noro 9323: STOZ(i,iq);
1.1 noro 9324: nd_tracelist = reverse_node(nd_tracelist);
9325: MKLIST(l,nd_tracelist);
9326: node = mknode(2,iq,l); MKLIST(l,node);
9327: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9328: nd_alltracelist = node; nd_tracelist = 0;
9329: }
9330:
9331: void conv_ilist(int demand,int trace,NODE g,int **indp)
9332: {
9333: int n,i,j;
9334: int *ind;
9335: NODE t;
9336:
9337: n = length(g);
9338: ind = (int *)MALLOC(n*sizeof(int));
9339: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9340: j = (long)BDY(t); ind[i] = j;
9341: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9342: }
9343: if ( indp ) *indp = ind;
9344: }
9345:
9346: void parse_nd_option(NODE opt)
9347: {
9348: NODE t,p,u;
9349: int i,s,n;
9350: char *key;
9351: Obj value;
9352:
9353: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
9354: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9355: nd_splist = 0; nd_check_splist = 0;
9356: nd_sugarweight = 0;
9357: nd_f4red =0;
9358: nd_rank0 = 0;
9359: for ( t = opt; t; t = NEXT(t) ) {
9360: p = BDY((LIST)BDY(t));
9361: key = BDY((STRING)BDY(p));
9362: value = (Obj)BDY(NEXT(p));
9363: if ( !strcmp(key,"gentrace") )
9364: nd_gentrace = value?1:0;
9365: else if ( !strcmp(key,"gensyz") )
9366: nd_gensyz = value?1:0;
9367: else if ( !strcmp(key,"nora") )
9368: nd_nora = value?1:0;
9369: else if ( !strcmp(key,"gbblock") ) {
9370: if ( value && OID(value) == O_LIST ) {
9371: u = BDY((LIST)value);
9372: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
9373: for ( i = 0; u; u = NEXT(u) ) {
9374: p = BDY((LIST)BDY(u));
1.6 noro 9375: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9376: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9377: }
9378: nd_gbblock[i] = -1;
9379: } else
9380: nd_gbblock = 0;
9381: } else if ( !strcmp(key,"newelim") )
9382: nd_newelim = value?1:0;
9383: else if ( !strcmp(key,"intersect") )
9384: nd_intersect = value?1:0;
1.17 noro 9385: else if ( !strcmp(key,"syzgen") )
9386: nd_intersect = ZTOS((Q)value);
1.1 noro 9387: else if ( !strcmp(key,"lf") )
9388: nd_lf = value?1:0;
9389: else if ( !strcmp(key,"trace") ) {
9390: if ( value ) {
9391: u = BDY((LIST)value);
9392: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 9393: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 9394: }
9395: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 9396: nd_f4red = ZTOS((Q)value);
1.1 noro 9397: } else if ( !strcmp(key,"rank0") ) {
9398: nd_rank0 = value?1:0;
9399: } else if ( !strcmp(key,"splist") ) {
9400: nd_splist = value?1:0;
9401: } else if ( !strcmp(key,"check_splist") ) {
9402: nd_check_splist = BDY((LIST)value);
9403: } else if ( !strcmp(key,"sugarweight") ) {
9404: u = BDY((LIST)value);
9405: n = length(u);
9406: nd_sugarweight = MALLOC(n*sizeof(int));
9407: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 9408: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 9409: }
9410: }
9411: }
9412:
9413: ND mdptond(DP d);
9414: ND nd_mul_nm(int mod,NM m0,ND p);
9415: ND nd_mul_nm_lf(NM m0,ND p);
9416: ND *btog(NODE ti,ND **p,int nb,int mod);
9417: ND btog_one(NODE ti,ND *p,int nb,int mod);
9418: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
9419: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
9420:
9421: /* d:monomial */
9422: ND mdptond(DP d)
9423: {
9424: NM m;
9425: ND r;
9426:
9427: if ( OID(d) == 1 )
9428: r = ptond(CO,CO,(P)d);
9429: else {
9430: NEWNM(m);
9431: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 9432: CZ(m) = (Z)BDY(d)->c;
1.1 noro 9433: NEXT(m) = 0;
9434: MKND(NV(d),m,1,r);
9435: }
9436: return r;
9437: }
9438:
9439: ND nd_mul_nm(int mod,NM m0,ND p)
9440: {
9441: UINT *d0;
9442: int c0,c1,c;
9443: NM tm,mr,mr0;
9444: ND r;
9445:
9446: if ( !p ) return 0;
9447: d0 = DL(m0);
9448: c0 = CM(m0);
9449: mr0 = 0;
9450: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9451: NEXTNM(mr0,mr);
9452: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
9453: ndl_add(d0,DL(tm),DL(mr));
9454: }
9455: NEXT(mr) = 0;
9456: MKND(NV(p),mr0,LEN(p),r);
9457: return r;
9458: }
9459:
9460: ND nd_mul_nm_lf(NM m0,ND p)
9461: {
9462: UINT *d0;
9463: Z c0,c1,c;
9464: NM tm,mr,mr0;
9465: ND r;
9466:
9467: if ( !p ) return 0;
9468: d0 = DL(m0);
9469: c0 = CZ(m0);
9470: mr0 = 0;
9471: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9472: NEXTNM(mr0,mr);
9473: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
9474: ndl_add(d0,DL(tm),DL(mr));
9475: }
9476: NEXT(mr) = 0;
9477: MKND(NV(p),mr0,LEN(p),r);
9478: return r;
9479: }
9480:
9481: ND *btog(NODE ti,ND **p,int nb,int mod)
9482: {
9483: PGeoBucket *r;
9484: int i,ci;
9485: NODE t,s;
9486: ND m,tp;
9487: ND *pi,*rd;
9488: P c;
9489:
9490: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9491: for ( i = 0; i < nb; i++ )
9492: r[i] = create_pbucket();
9493: for ( t = ti; t; t = NEXT(t) ) {
9494: s = BDY((LIST)BDY(t));
9495: if ( ARG0(s) ) {
9496: m = mdptond((DP)ARG2(s));
1.6 noro 9497: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9498: if ( (ci = ((MQ)c)->cont) != 0 ) {
9499: HCM(m) = ci;
1.6 noro 9500: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9501: for ( i = 0; i < nb; i++ ) {
9502: tp = nd_mul_nm(mod,BDY(m),pi[i]);
9503: add_pbucket(mod,r[i],tp);
9504: }
9505: }
9506: ci = 1;
9507: } else {
9508: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9509: ci = invm(ci,mod);
9510: }
9511: }
9512: rd = (ND *)MALLOC(nb*sizeof(ND));
9513: for ( i = 0; i < nb; i++ )
9514: rd[i] = normalize_pbucket(mod,r[i]);
9515: if ( ci != 1 )
9516: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
9517: return rd;
9518: }
9519:
9520: /* YYY */
9521: ND *btog_lf(NODE ti,ND **p,int nb)
9522: {
9523: PGeoBucket *r;
9524: int i;
9525: NODE t,s;
9526: ND m,tp;
9527: ND *pi,*rd;
9528: LM lm;
9529: Z lf,c;
9530:
9531: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9532: for ( i = 0; i < nb; i++ )
9533: r[i] = create_pbucket();
9534: for ( t = ti; t; t = NEXT(t) ) {
9535: s = BDY((LIST)BDY(t));
9536: if ( ARG0(s) ) {
9537: m = mdptond((DP)ARG2(s));
1.6 noro 9538: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 9539: if ( lm ) {
9540: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 9541: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9542: for ( i = 0; i < nb; i++ ) {
9543: tp = nd_mul_nm_lf(BDY(m),pi[i]);
9544: add_pbucket(-2,r[i],tp);
9545: }
9546: }
9547: c = ONE;
9548: } else {
9549: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
9550: }
9551: }
9552: rd = (ND *)MALLOC(nb*sizeof(ND));
9553: for ( i = 0; i < nb; i++ )
9554: rd[i] = normalize_pbucket(-2,r[i]);
9555: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
9556: return rd;
9557: }
9558:
9559: ND btog_one(NODE ti,ND *p,int nb,int mod)
9560: {
9561: PGeoBucket r;
9562: int i,ci,j;
9563: NODE t,s;
9564: ND m,tp;
9565: ND pi,rd;
9566: P c;
9567:
9568: r = create_pbucket();
9569: for ( t = ti; t; t = NEXT(t) ) {
9570: s = BDY((LIST)BDY(t));
9571: if ( ARG0(s) ) {
9572: m = mdptond((DP)ARG2(s));
1.6 noro 9573: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9574: if ( (ci = ((MQ)c)->cont) != 0 ) {
9575: HCM(m) = ci;
1.6 noro 9576: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 9577: if ( !pi ) {
9578: pi = nd_load_mod(j);
9579: tp = nd_mul_nm(mod,BDY(m),pi);
9580: nd_free(pi);
9581: add_pbucket(mod,r,tp);
9582: } else {
9583: tp = nd_mul_nm(mod,BDY(m),pi);
9584: add_pbucket(mod,r,tp);
9585: }
9586: }
9587: ci = 1;
9588: } else {
9589: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9590: ci = invm(ci,mod);
9591: }
9592: }
9593: rd = normalize_pbucket(mod,r);
9594: free_pbucket(r);
9595: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
9596: return rd;
9597: }
9598:
9599: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
9600:
9601: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
9602: {
9603: int i,j,n,m,nb,pi0,pi1,nvar;
9604: VL fv,tv,vv;
9605: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9606: ND **p;
9607: ND *c;
9608: ND u;
9609: P inv;
9610: MAT mat;
9611:
9612: if ( mod == -2 )
9613: return nd_btog_lf(f,v,ord,tlist,rp);
9614:
9615: parse_nd_option(current_option);
9616: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9617: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9618: switch ( ord->id ) {
9619: case 1:
9620: if ( ord->nv != nvar )
9621: error("nd_check : invalid order specification");
9622: break;
9623: default:
9624: break;
9625: }
9626: nd_init_ord(ord);
9627: #if 0
1.6 noro 9628: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9629: #else
9630: nd_bpe = 32;
9631: #endif
9632: nd_setup_parameters(nvar,0);
9633: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9634: intred = BDY((LIST)ARG3(BDY(tlist)));
9635: ind = BDY((LIST)ARG4(BDY(tlist)));
9636: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9637: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9638: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9639: if ( j > i ) i = j;
9640: }
9641: n = i+1;
9642: nb = length(BDY(f));
9643: p = (ND **)MALLOC(n*sizeof(ND *));
9644: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9645: pi = BDY((LIST)BDY(t));
1.6 noro 9646: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9647: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9648: ptomp(mod,(P)ARG2(pi),&inv);
9649: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9650: u = ptond(CO,vv,(P)ONE);
9651: HCM(u) = ((MQ)inv)->cont;
9652: c[pi1] = u;
9653: }
9654: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9655: printf("%d ",i); fflush(stdout);
9656: ti = BDY((LIST)BDY(t));
1.6 noro 9657: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9658: }
9659: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9660: printf("%d ",i); fflush(stdout);
9661: ti = BDY((LIST)BDY(t));
1.6 noro 9662: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9663: }
9664: m = length(ind);
9665: MKMAT(mat,nb,m);
9666: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9667: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9668: BDY(mat)[i][j] = ndtodp(mod,c[i]);
9669: return mat;
9670: }
9671:
9672: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
9673: {
9674: int i,j,n,m,nb,pi0,pi1,nvar;
9675: VL fv,tv,vv;
9676: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9677: ND **p;
9678: ND *c;
9679: ND u;
9680: MAT mat;
9681: LM lm;
9682: Z lf,inv;
9683:
9684: parse_nd_option(current_option);
9685: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9686: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9687: switch ( ord->id ) {
9688: case 1:
9689: if ( ord->nv != nvar )
9690: error("nd_check : invalid order specification");
9691: break;
9692: default:
9693: break;
9694: }
9695: nd_init_ord(ord);
9696: #if 0
1.6 noro 9697: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9698: #else
9699: nd_bpe = 32;
9700: #endif
9701: nd_setup_parameters(nvar,0);
9702: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9703: intred = BDY((LIST)ARG3(BDY(tlist)));
9704: ind = BDY((LIST)ARG4(BDY(tlist)));
9705: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9706: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9707: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9708: if ( j > i ) i = j;
9709: }
9710: n = i+1;
9711: nb = length(BDY(f));
9712: p = (ND **)MALLOC(n*sizeof(ND *));
9713: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9714: pi = BDY((LIST)BDY(t));
1.6 noro 9715: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9716: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9717: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
9718: u = ptond(CO,vv,(P)ONE);
9719: HCZ(u) = inv;
9720: c[pi1] = u;
9721: }
9722: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9723: printf("%d ",i); fflush(stdout);
9724: ti = BDY((LIST)BDY(t));
1.6 noro 9725: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9726: }
9727: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9728: printf("%d ",i); fflush(stdout);
9729: ti = BDY((LIST)BDY(t));
1.6 noro 9730: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9731: }
9732: m = length(ind);
9733: MKMAT(mat,nb,m);
9734: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9735: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9736: BDY(mat)[i][j] = ndtodp(-2,c[i]);
9737: return mat;
9738: }
9739:
9740: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
9741: LIST tlist,int pos,MAT *rp)
9742: {
9743: int i,j,n,m,nb,pi0,pi1,nvar;
9744: VL fv,tv,vv;
9745: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9746: ND *p;
9747: ND *c;
9748: ND u;
9749: P inv;
9750: VECT vect;
9751:
9752: if ( mod == -2 )
9753: error("nd_btog_one : not implemented yet for a large finite field");
9754:
9755: parse_nd_option(current_option);
9756: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9757: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9758: switch ( ord->id ) {
9759: case 1:
9760: if ( ord->nv != nvar )
9761: error("nd_check : invalid order specification");
9762: break;
9763: default:
9764: break;
9765: }
9766: nd_init_ord(ord);
9767: #if 0
1.6 noro 9768: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9769: #else
9770: nd_bpe = 32;
9771: #endif
9772: nd_setup_parameters(nvar,0);
9773: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9774: intred = BDY((LIST)ARG3(BDY(tlist)));
9775: ind = BDY((LIST)ARG4(BDY(tlist)));
9776: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9777: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9778: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9779: if ( j > i ) i = j;
9780: }
9781: n = i+1;
9782: nb = length(BDY(f));
9783: p = (ND *)MALLOC(n*sizeof(ND *));
9784: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9785: pi = BDY((LIST)BDY(t));
1.6 noro 9786: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9787: if ( pi1 == pos ) {
9788: ptomp(mod,(P)ARG2(pi),&inv);
9789: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9790: u = ptond(CO,vv,(P)ONE);
9791: HCM(u) = ((MQ)inv)->cont;
9792: p[pi0] = u;
9793: }
9794: }
9795: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9796: printf("%d ",i); fflush(stdout);
9797: ti = BDY((LIST)BDY(t));
1.6 noro 9798: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9799: if ( Demand ) {
9800: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
9801: }
9802: }
9803: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9804: printf("%d ",i); fflush(stdout);
9805: ti = BDY((LIST)BDY(t));
1.6 noro 9806: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9807: if ( Demand ) {
9808: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
9809: }
9810: }
9811: m = length(ind);
9812: MKVECT(vect,m);
9813: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 9814: u = p[ZTOS((Q)BDY(t))];
1.1 noro 9815: if ( !u ) {
1.6 noro 9816: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 9817: BDY(vect)[j] = ndtodp(mod,u);
9818: nd_free(u);
9819: } else
9820: BDY(vect)[j] = ndtodp(mod,u);
9821: }
9822: return vect;
9823: }
9824:
9825: void ndv_print_lf(NDV p)
9826: {
9827: NMV m;
9828: int i,len;
9829:
9830: if ( !p ) printf("0\n");
9831: else {
9832: len = LEN(p);
9833: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
9834: printf("+");
9835: mpz_out_str(asir_out,10,BDY(CZ(m)));
9836: printf("*");
9837: ndl_print(DL(m));
9838: }
9839: printf("\n");
9840: }
9841: }
9842:
9843: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
9844: {
9845: VL tv,fv,vv,vc,av;
9846: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
9847: int m,nocheck,nvar,mindex,e,max;
9848: NDV c;
9849: NMV a;
9850: P p,zp;
9851: Q dmy;
9852: EPOS oepos;
9853: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
9854: Alg alpha,dp;
9855: P poly;
9856: LIST f1,f2,zpl;
9857: Obj obj;
9858: NumberField nf;
9859: struct order_spec *ord1;
9860: struct oEGT eg_check,eg0,eg1;
9861: NODE tr,tl1,tl2,tl3,tl4;
9862: LIST l1,l2,l3,l4,l5;
9863: int *perm;
9864: int j,ret;
9865: NODE retn;
9866: Q jq,bpe;
9867:
9868: nd_module = 0;
9869: parse_nd_option(current_option);
9870: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9871: if ( nd_vc )
9872: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
9873: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9874: switch ( ord->id ) {
9875: case 1:
9876: if ( ord->nv != nvar )
9877: error("nd_f4_lf_trace : invalid order specification");
9878: break;
9879: default:
9880: break;
9881: }
9882:
9883: nd_ntrans = nvar;
9884: nd_nalg = 0;
9885:
9886: nocheck = 0;
9887: mindex = 0;
9888:
9889: /* do not use on-demand load/save */
9890: nd_demand = 0;
9891: m = trace > 1 ? trace : get_lprime(mindex);
9892: nd_init_ord(ord);
9893: mrank = 0;
9894: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9895: for ( tv = vv; tv; tv = NEXT(tv) ) {
9896: if ( nd_module ) {
9897: s = BDY((LIST)BDY(t));
9898: trank = length(s);
9899: mrank = MAX(mrank,trank);
9900: for ( ; s; s = NEXT(s) ) {
9901: e = getdeg(tv->v,(P)BDY(s));
9902: max = MAX(e,max);
9903: }
9904: } else {
9905: e = getdeg(tv->v,(P)BDY(t));
9906: max = MAX(e,max);
9907: }
9908: }
9909: nd_setup_parameters(nvar,max);
9910: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9911: ishomo = 1;
9912: /* XXX */
9913: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9914: if ( nd_module ) {
9915: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9916: } else {
9917: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9918: }
9919: if ( ishomo )
9920: ishomo = ishomo && ndv_ishomo(c);
9921: if ( c ) {
9922: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9923: ndv_mod(-2,c);
9924: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9925: }
9926: }
9927: if ( in0 ) NEXT(in) = 0;
9928: if ( fd0 ) NEXT(fd) = 0;
9929: if ( !ishomo && homo ) {
9930: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9931: c = (NDV)BDY(t); len = LEN(c);
9932: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9933: wmax = MAX(TD(DL(a)),wmax);
9934: }
9935: homogenize_order(ord,nvar,&ord1);
9936: nd_init_ord(ord1);
9937: nd_setup_parameters(nvar+1,wmax);
9938: for ( t = fd0; t; t = NEXT(t) )
9939: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9940: }
9941: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 9942: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 9943: if ( ret )
9944: cand = nd_f4_lf_trace_main(m,&perm);
9945: if ( !ret || !cand ) {
9946: *rp = 0; return;
9947: }
9948: if ( !ishomo && homo ) {
9949: /* dehomogenization */
9950: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9951: nd_init_ord(ord);
9952: nd_setup_parameters(nvar,0);
9953: }
9954: cand = ndv_reducebase(cand,perm);
9955: cand = ndv_reduceall(-2,cand);
9956: cbpe = nd_bpe;
9957: get_eg(&eg0);
9958: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9959: /* gbcheck : cand is a GB of Id(cand) ? */
9960: retn = nd_f4(-2,0,0);
9961: }
9962: if ( !retn ) {
9963: /* failure */
9964: *rp = 0; return;
9965: }
9966: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9967: if ( DP_Print )
1.5 noro 9968: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 9969: /* dp->p */
9970: nd_bpe = cbpe;
9971: nd_setup_parameters(nd_nvar,0);
9972: for ( r = cand; r; r = NEXT(r) ) {
9973: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9974: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9975: }
9976: MKLIST(*rp,cand);
9977: }
9978:
9979: NODE nd_f4_lf_trace_main(int m,int **indp)
9980: {
9981: int i,nh,stat,index;
9982: NODE r,rm,g;
9983: ND_pairs d,l,l0,t;
9984: ND spol,red;
9985: NDV nf,redv,nfqv,nfv;
9986: NM s0,s;
9987: NODE rp0,srp0,nflist,nflist_lf;
9988: int nsp,nred,col,rank,len,k,j,a;
9989: UINT c;
9990: UINT **spmat;
9991: UINT *s0vect,*svect,*p,*v;
9992: int *colstat;
9993: IndArray *imat;
9994: int *rhead;
9995: int spcol,sprow;
9996: int sugar;
9997: PGeoBucket bucket;
9998: struct oEGT eg0,eg1,eg_f4;
9999:
10000: g = 0; d = 0;
10001: for ( i = 0; i < nd_psn; i++ ) {
10002: d = update_pairs(d,g,i,0);
10003: g = update_base(g,i);
10004: }
10005: while ( d ) {
10006: get_eg(&eg0);
10007: l = nd_minsugarp(d,&d);
10008: sugar = SG(l);
10009: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10010: bucket = create_pbucket();
10011: stat = nd_sp_f4(m,0,l,bucket);
10012: if ( !stat ) {
10013: for ( t = l; NEXT(t); t = NEXT(t) );
10014: NEXT(t) = d; d = l;
10015: d = nd_reconstruct(1,d);
10016: continue;
10017: }
10018: if ( bucket->m < 0 ) continue;
10019: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10020: if ( !col ) {
10021: for ( t = l; NEXT(t); t = NEXT(t) );
10022: NEXT(t) = d; d = l;
10023: d = nd_reconstruct(1,d);
10024: continue;
10025: }
10026: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10027: if ( DP_Print )
1.5 noro 10028: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10029: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10030: if ( !l0 ) continue;
10031: l = l0;
10032:
10033: /* over LF */
10034: bucket = create_pbucket();
10035: stat = nd_sp_f4(-2,1,l,bucket);
10036: if ( !stat ) {
10037: for ( t = l; NEXT(t); t = NEXT(t) );
10038: NEXT(t) = d; d = l;
10039: d = nd_reconstruct(1,d);
10040: continue;
10041: }
10042: if ( bucket->m < 0 ) continue;
10043: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10044: if ( !col ) {
10045: for ( t = l; NEXT(t); t = NEXT(t) );
10046: NEXT(t) = d; d = l;
10047: d = nd_reconstruct(1,d);
10048: continue;
10049: }
10050: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10051: /* adding new bases */
10052: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10053: nfv = (NDV)BDY(rm);
10054: nfqv = (NDV)BDY(r);
10055: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10056: ndv_removecont(m,nfv);
10057: ndv_removecont(-2,nfqv);
1.24 noro 10058: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10059: d = update_pairs(d,g,nh,0);
10060: g = update_base(g,nh);
10061: }
10062: if ( r || rm ) return 0;
10063: }
10064: conv_ilist(nd_demand,1,g,indp);
10065: return g;
10066: }
10067:
1.7 noro 10068: #if SIZEOF_LONG==8
10069:
10070: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10071: {
10072: int j,k,len;
10073: UINT *p;
10074: UINT c;
10075: NDV r;
10076: NMV mr0,mr;
10077:
10078: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10079: if ( !len ) return 0;
10080: else {
10081: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10082: #if 0
10083: ndv_alloc += nmv_adv*len;
10084: #endif
10085: mr = mr0;
10086: p = s0vect;
10087: for ( j = k = 0; j < col; j++, p += nd_wpd )
10088: if ( !rhead[j] ) {
10089: if ( (c = (UINT)vect[k++]) != 0 ) {
10090: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10091: }
10092: }
10093: MKNDV(nd_nvar,mr0,len,r);
10094: return r;
10095: }
10096: }
10097:
10098: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10099: {
10100: NM m;
1.11 noro 10101: UINT *t,*s,*u;
10102: int i,st,ed,md,prev,c;
1.7 noro 10103:
10104: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10105: prev = 0;
10106: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10107: t = DL(m);
10108: st = prev;
10109: ed = n;
10110: while ( ed > st ) {
10111: md = (st+ed)/2;
10112: u = s0+md*nd_wpd;
10113: c = DL_COMPARE(u,t);
10114: if ( c == 0 ) break;
10115: else if ( c > 0 ) st = md;
10116: else ed = md;
10117: }
10118: r[md] = (mp_limb_t)CM(m);
10119: prev = md;
1.7 noro 10120: }
10121: for ( i = 0; !r[i]; i++ );
10122: return i;
10123: }
10124:
10125: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10126:
10127: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
10128: {
10129: int i,j,k,len,pos,prev;
10130: mp_limb_t a,c,c1,c2;
10131: IndArray ivect;
10132: unsigned char *ivc;
10133: unsigned short *ivs;
10134: unsigned int *ivi;
10135: NDV redv;
10136: NMV mr;
10137: NODE rp;
10138: int maxrs;
10139:
10140: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10141: maxrs = 0;
10142: for ( i = 0; i < nred; i++ ) {
10143: ivect = imat[i];
10144: k = ivect->head;
10145: a = svect[k]; c = cvect[k];
10146: MOD128(a,c,m);
10147: svect[k] = a; cvect[k] = 0;
10148: if ( (c = svect[k]) != 0 ) {
1.11 noro 10149: Nf4_red++;
1.7 noro 10150: maxrs = MAX(maxrs,rp0[i]->sugar);
10151: c = m-c; redv = nd_ps[rp0[i]->index];
10152: len = LEN(redv); mr = BDY(redv);
10153: svect[k] = 0; prev = k;
10154: switch ( ivect->width ) {
10155: case 1:
10156: ivc = ivect->index.c;
10157: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10158: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10159: c2 = svect[pos]+c1*c;
10160: if ( c2 < svect[pos] ) cvect[pos]++;
10161: svect[pos] = c2;
1.7 noro 10162: }
10163: break;
10164: case 2:
10165: ivs = ivect->index.s;
10166: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10167: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10168: c2 = svect[pos]+c1*c;
10169: if ( c2 < svect[pos] ) cvect[pos]++;
10170: svect[pos] = c2;
1.7 noro 10171: }
10172: break;
10173: case 4:
10174: ivi = ivect->index.i;
10175: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10176: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10177: c2 = svect[pos]+c1*c;
10178: if ( c2 < svect[pos] ) cvect[pos]++;
10179: svect[pos] = c2;
1.7 noro 10180: }
10181: break;
10182: }
10183: }
10184: }
10185: for ( i = 0; i < col; i++ ) {
10186: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10187: }
10188: return maxrs;
10189: }
10190:
10191: /* for Fp, 2^15=<p<2^29 */
10192:
10193: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10194: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10195: {
10196: int spcol,sprow,a;
10197: int i,j,k,l,rank;
10198: NODE r0,r;
10199: ND_pairs sp;
10200: ND spol;
10201: mp_limb_t **spmat;
10202: mp_limb_t *svect,*cvect;
10203: mp_limb_t *v;
10204: int *colstat;
10205: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10206: int maxrs;
10207: int *spsugar;
10208: ND_pairs *spactive;
10209:
10210: spcol = col-nred;
10211: get_eg(&eg0);
10212: /* elimination (1st step) */
10213: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10214: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10215: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10216: spsugar = (int *)MALLOC(nsp*sizeof(int));
10217: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10218: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10219: nd_sp(m,0,sp,&spol);
10220: if ( !spol ) continue;
10221: nd_to_vect64(m,s0vect,col,spol,svect);
10222: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
10223: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10224: if ( i < col ) {
10225: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10226: for ( j = k = 0; j < col; j++ )
10227: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10228: spsugar[sprow] = MAX(maxrs,SG(spol));
10229: if ( nz )
10230: spactive[sprow] = sp;
10231: sprow++;
10232: }
10233: nd_free(spol);
10234: }
1.12 noro 10235: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10236: if ( DP_Print ) {
10237: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10238: fflush(asir_out);
10239: }
10240: /* free index arrays */
10241: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10242:
10243: /* elimination (2nd step) */
10244: colstat = (int *)MALLOC(spcol*sizeof(int));
10245: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10246: r0 = 0;
10247: for ( i = 0; i < rank; i++ ) {
10248: NEXTNODE(r0,r); BDY(r) =
10249: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10250: SG((NDV)BDY(r)) = spsugar[i];
10251: GCFREE(spmat[i]);
10252: }
10253: if ( r0 ) NEXT(r) = 0;
10254:
10255: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10256: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10257: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10258: if ( DP_Print ) {
10259: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10260: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10261: nsp,nred,sprow,spcol,rank);
10262: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10263: }
10264: if ( nz ) {
10265: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10266: if ( rank > 0 ) {
10267: NEXT(spactive[rank-1]) = 0;
10268: *nz = spactive[0];
10269: } else
10270: *nz = 0;
10271: }
10272: return r0;
10273: }
10274:
10275: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10276: {
10277: int i,j,k,l,rank,s;
10278: mp_limb_t inv;
10279: mp_limb_t a;
10280: UINT c;
10281: mp_limb_t *t,*pivot,*pk;
10282: UINT *ck;
10283: UINT **cmat;
10284: UINT *ct;
10285: ND_pairs pair;
10286:
10287: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10288: for ( i = 0; i < row; i++ ) {
10289: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10290: bzero(cmat[i],col*sizeof(UINT));
10291: }
10292:
10293: for ( rank = 0, j = 0; j < col; j++ ) {
10294: for ( i = rank; i < row; i++ ) {
10295: a = mat[i][j]; c = cmat[i][j];
10296: MOD128(a,c,md);
10297: mat[i][j] = a; cmat[i][j] = 0;
10298: }
10299: for ( i = rank; i < row; i++ )
10300: if ( mat[i][j] )
10301: break;
10302: if ( i == row ) {
10303: colstat[j] = 0;
10304: continue;
10305: } else
10306: colstat[j] = 1;
10307: if ( i != rank ) {
10308: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10309: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10310: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10311: if ( spactive ) {
10312: pair = spactive[i]; spactive[i] = spactive[rank];
10313: spactive[rank] = pair;
10314: }
10315: }
10316: /* column j is normalized */
10317: s = sugar[rank];
10318: inv = invm((UINT)mat[rank][j],md);
10319: /* normalize pivot row */
10320: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10321: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10322: }
10323: for ( i = rank+1; i < row; i++ ) {
10324: if ( (a = mat[i][j]) != 0 ) {
10325: sugar[i] = MAX(sugar[i],s);
10326: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10327: Nf4_red++;
1.7 noro 10328: }
10329: }
10330: rank++;
10331: }
10332: for ( j = col-1, l = rank-1; j >= 0; j-- )
10333: if ( colstat[j] ) {
10334: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10335: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10336: }
10337: s = sugar[l];
10338: for ( i = 0; i < l; i++ ) {
10339: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10340: if ( a ) {
10341: sugar[i] = MAX(sugar[i],s);
10342: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10343: Nf4_red++;
1.7 noro 10344: }
10345: }
10346: l--;
10347: }
10348: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10349: GCFREE(cmat);
10350: return rank;
10351: }
10352: #endif
10353:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>