Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.13
1.13 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.12 2018/11/12 04:25:13 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);
11: int nd_dcomp;
12: int nd_rref2;
13: NM _nm_free_list;
14: ND _nd_free_list;
15: ND_pairs _ndp_free_list;
16: NODE nd_hcf;
17:
18: Obj nd_top_weight;
19:
20: static NODE nd_subst;
21: static VL nd_vc;
22: static int nd_ntrans;
23: static int nd_nalg;
24: #if 0
25: static int ndv_alloc;
26: #endif
27: #if 1
28: static int nd_f4_nsp=0x7fffffff;
29: #else
30: static int nd_f4_nsp=50;
31: #endif
32: static double nd_scale=2;
33: static UINT **nd_bound;
34: static struct order_spec *nd_ord;
35: static EPOS nd_epos;
36: static BlockMask nd_blockmask;
37: static int nd_nvar;
38: static int nd_isrlex;
39: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
40: static UINT nd_mask[32];
41: static UINT nd_mask0,nd_mask1;
42:
43: static NDV *nd_ps;
44: static NDV *nd_ps_trace;
45: static NDV *nd_ps_sym;
46: static NDV *nd_ps_trace_sym;
47: static RHist *nd_psh;
48: static int nd_psn,nd_pslen;
49: static RHist *nd_red;
50: static int *nd_work_vector;
51: static int **nd_matrix;
52: static int nd_matrix_len;
53: static struct weight_or_block *nd_worb;
54: static int nd_worb_len;
55: static int nd_found,nd_create,nd_notfirst;
56: static int nmv_adv;
57: static int nd_demand;
58: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
59: static int nd_module_rank,nd_poly_weight_len;
60: static int *nd_poly_weight,*nd_module_weight;
61: static NODE nd_tracelist;
62: static NODE nd_alltracelist;
63: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
64: static int *nd_gbblock;
65: static NODE nd_nzlist,nd_check_splist;
66: static int nd_splist;
67: static int *nd_sugarweight;
68: static int nd_f4red,nd_rank0,nd_last_nonzero;
69:
70: NumberField get_numberfield();
71: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
72: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
73: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
74: int nd_monic(int m,ND *p);
75: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
76: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
77: NDV pltondv(VL vl,VL dvl,LIST p);
78: void pltozpl(LIST l,Q *cont,LIST *pp);
79: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
80: void nmtodp(int mod,NM m,DP *r);
81: NODE reverse_node(NODE n);
82: P ndc_div(int mod,union oNDC a,union oNDC b);
83: P ndctop(int mod,union oNDC c);
84: void finalize_tracelist(int i,P cont);
85: void conv_ilist(int demand,int trace,NODE g,int **indp);
86: void parse_nd_option(NODE opt);
87: void dltondl(int n,DL dl,UINT *r);
88: DP ndvtodp(int mod,NDV p);
89: DP ndtodp(int mod,ND p);
90:
91: void Pdp_set_weight(NODE,VECT *);
92: void Pox_cmo_rpc(NODE,Obj *);
93:
94: ND nd_add_lf(ND p1,ND p2);
95: void nd_mul_c_lf(ND p,Z mul);
96: void ndv_mul_c_lf(NDV p,Z mul);
97: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
98: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
99: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
100: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
101: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
102: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
103: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
104: NODE nd_f4_lf_trace_main(int m,int **indp);
105: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
106:
107: extern int lf_lazy;
108: extern Z current_mod_lf;
109:
110: extern int Denominator,DP_Multiple,MaxDeg;
111:
112: #define BLEN (8*sizeof(unsigned long))
113:
114: typedef struct matrix {
115: int row,col;
116: unsigned long **a;
117: } *matrix;
118:
119:
120: void nd_free_private_storage()
121: {
122: _nm_free_list = 0;
123: _ndp_free_list = 0;
124: #if 0
125: GC_gcollect();
126: #endif
127: }
128:
129: void _NM_alloc()
130: {
131: NM p;
132: int i;
133:
134: for ( i = 0; i < 1024; i++ ) {
135: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
136: p->next = _nm_free_list; _nm_free_list = p;
137: }
138: }
139:
140: matrix alloc_matrix(int row,int col)
141: {
142: unsigned long **a;
143: int i,len,blen;
144: matrix mat;
145:
146: mat = (matrix)MALLOC(sizeof(struct matrix));
147: mat->row = row;
148: mat->col = col;
149: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
150: return mat;
151: }
152:
153:
154: void _ND_alloc()
155: {
156: ND p;
157: int i;
158:
159: for ( i = 0; i < 1024; i++ ) {
160: p = (ND)MALLOC(sizeof(struct oND));
161: p->body = (NM)_nd_free_list; _nd_free_list = p;
162: }
163: }
164:
165: void _NDP_alloc()
166: {
167: ND_pairs p;
168: int i;
169:
170: for ( i = 0; i < 1024; i++ ) {
171: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
172: +(nd_wpd-1)*sizeof(UINT));
173: p->next = _ndp_free_list; _ndp_free_list = p;
174: }
175: }
176:
177: INLINE int nd_length(ND p)
178: {
179: NM m;
180: int i;
181:
182: if ( !p )
183: return 0;
184: else {
185: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
186: return i;
187: }
188: }
189:
190: extern int dp_negative_weight;
191:
192: INLINE int ndl_reducible(UINT *d1,UINT *d2)
193: {
194: UINT u1,u2;
195: int i,j;
196:
197: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
198:
199: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
200: #if USE_UNROLL
201: switch ( nd_bpe ) {
202: case 3:
203: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
204: u1 = d1[i]; u2 = d2[i];
205: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
206: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
207: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
208: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
209: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
210: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
211: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
212: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
213: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
214: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
215: }
216: return 1;
217: break;
218: case 4:
219: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
220: u1 = d1[i]; u2 = d2[i];
221: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
222: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
223: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
224: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
225: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
226: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
227: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
228: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
229: }
230: return 1;
231: break;
232: case 6:
233: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
234: u1 = d1[i]; u2 = d2[i];
235: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
236: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
237: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
238: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
239: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
240: }
241: return 1;
242: break;
243: case 8:
244: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
245: u1 = d1[i]; u2 = d2[i];
246: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
247: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
248: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
249: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
250: }
251: return 1;
252: break;
253: case 16:
254: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
255: u1 = d1[i]; u2 = d2[i];
256: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
257: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
258: }
259: return 1;
260: break;
261: case 32:
262: for ( i = nd_exporigin; i < nd_wpd; i++ )
263: if ( d1[i] < d2[i] ) return 0;
264: return 1;
265: break;
266: default:
267: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
268: u1 = d1[i]; u2 = d2[i];
269: for ( j = 0; j < nd_epw; j++ )
270: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
271: }
272: return 1;
273: }
274: #else
275: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
276: u1 = d1[i]; u2 = d2[i];
277: for ( j = 0; j < nd_epw; j++ )
278: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
279: }
280: return 1;
281: #endif
282: }
283:
284: /*
285: * If the current order is a block order,
286: * then the last block is length 1 and contains
287: * the homo variable. Otherwise, the original
288: * order is either 0 or 2.
289: */
290:
291: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
292: {
293: int w,i,e,n,omask0;
294:
295: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
296: n = nd_nvar-1;
297: ndl_zero(r);
298: for ( i = 0; i < n; i++ ) {
299: e = GET_EXP_OLD(d,i);
300: PUT_EXP(r,i,e);
301: }
302: w = TD(d);
303: PUT_EXP(r,nd_nvar-1,weight-w);
304: if ( nd_module ) MPOS(r) = d[ompos];
305: TD(r) = weight;
306: if ( nd_blockmask ) ndl_weight_mask(r);
307: }
308:
309: void ndl_dehomogenize(UINT *d)
310: {
311: UINT mask;
312: UINT h;
313: int i,bits;
314:
315: if ( nd_blockmask ) {
316: h = GET_EXP(d,nd_nvar-1);
317: XOR_EXP(d,nd_nvar-1,h);
318: TD(d) -= h;
319: ndl_weight_mask(d);
320: } else {
321: if ( nd_isrlex ) {
322: if ( nd_bpe == 32 ) {
323: h = d[nd_exporigin];
324: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
325: d[i-1] = d[i];
326: d[i-1] = 0;
327: TD(d) -= h;
328: } else {
329: bits = nd_epw*nd_bpe;
330: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
331: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
332: for ( i = nd_exporigin; i < nd_wpd; i++ )
333: d[i] = ((d[i]<<nd_bpe)&mask)
334: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
335: TD(d) -= h;
336: }
337: } else {
338: h = GET_EXP(d,nd_nvar-1);
339: XOR_EXP(d,nd_nvar-1,h);
340: TD(d) -= h;
341: }
342: }
343: }
344:
345: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
346: {
347: UINT t1,t2,u,u1,u2;
348: int i,j,l;
349:
350: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
351: error("ndl_lcm : inconsistent monomials");
352: #if USE_UNROLL
353: switch ( nd_bpe ) {
354: case 3:
355: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
356: u1 = d1[i]; u2 = d2[i];
357: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
358: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
359: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
360: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
361: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
362: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
363: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
364: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
365: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
366: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
367: d[i] = u;
368: }
369: break;
370: case 4:
371: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
372: u1 = d1[i]; u2 = d2[i];
373: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
374: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
375: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
376: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
377: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
378: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
379: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
380: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
381: d[i] = u;
382: }
383: break;
384: case 6:
385: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
386: u1 = d1[i]; u2 = d2[i];
387: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
388: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
389: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
390: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
391: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
392: d[i] = u;
393: }
394: break;
395: case 8:
396: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
397: u1 = d1[i]; u2 = d2[i];
398: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
399: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
400: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
401: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
402: d[i] = u;
403: }
404: break;
405: case 16:
406: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
407: u1 = d1[i]; u2 = d2[i];
408: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
409: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
410: d[i] = u;
411: }
412: break;
413: case 32:
414: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
415: u1 = d1[i]; u2 = d2[i];
416: d[i] = u1>u2?u1:u2;
417: }
418: break;
419: default:
420: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
421: u1 = d1[i]; u2 = d2[i];
422: for ( j = 0, u = 0; j < nd_epw; j++ ) {
423: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
424: }
425: d[i] = u;
426: }
427: break;
428: }
429: #else
430: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
431: u1 = d1[i]; u2 = d2[i];
432: for ( j = 0, u = 0; j < nd_epw; j++ ) {
433: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
434: }
435: d[i] = u;
436: }
437: #endif
438: if ( nd_module ) MPOS(d) = MPOS(d1);
439: TD(d) = ndl_weight(d);
440: if ( nd_blockmask ) ndl_weight_mask(d);
441: }
442:
443: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
444: {
445: UINT t1,t2,u,u1,u2;
446: int i,j,l;
447:
448: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
449: u1 = d1[i]; u2 = d2[i];
450: for ( j = 0, u = 0; j < nd_epw; j++ ) {
451: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
452: }
453: d[i] = u;
454: }
455: }
456:
457: int ndl_weight(UINT *d)
458: {
459: UINT t,u;
460: int i,j;
461:
462: if ( current_dl_weight_vector )
463: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
464: u = GET_EXP(d,i);
465: t += MUL_WEIGHT(u,i);
466: }
467: else
468: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
469: u = d[i];
470: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
471: t += (u&nd_mask0);
472: }
473: if ( nd_module && current_module_weight_vector && MPOS(d) )
474: t += current_module_weight_vector[MPOS(d)];
475: return t;
476: }
477:
478: /* for sugarweight */
479:
480: int ndl_weight2(UINT *d)
481: {
482: int t,u;
483: int i,j;
484:
485: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
486: u = GET_EXP(d,i);
487: t += nd_sugarweight[i]*u;
488: }
489: if ( nd_module && current_module_weight_vector && MPOS(d) )
490: t += current_module_weight_vector[MPOS(d)];
491: return t;
492: }
493:
494: void ndl_weight_mask(UINT *d)
495: {
496: UINT t,u;
497: UINT *mask;
498: int i,j,k,l;
499:
500: l = nd_blockmask->n;
501: for ( k = 0; k < l; k++ ) {
502: mask = nd_blockmask->mask[k];
503: if ( current_dl_weight_vector )
504: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
505: u = GET_EXP_MASK(d,i,mask);
506: t += MUL_WEIGHT(u,i);
507: }
508: else
509: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
510: u = d[i]&mask[i];
511: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
512: t += (u&nd_mask0);
513: }
514: d[k+1] = t;
515: }
516: }
517:
518: int ndl_lex_compare(UINT *d1,UINT *d2)
519: {
520: int i;
521:
522: d1 += nd_exporigin;
523: d2 += nd_exporigin;
524: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
525: if ( *d1 > *d2 )
526: return nd_isrlex ? -1 : 1;
527: else if ( *d1 < *d2 )
528: return nd_isrlex ? 1 : -1;
529: return 0;
530: }
531:
532: int ndl_block_compare(UINT *d1,UINT *d2)
533: {
534: int i,l,j,ord_o,ord_l;
535: struct order_pair *op;
536: UINT t1,t2,m;
537: UINT *mask;
538:
539: l = nd_blockmask->n;
540: op = nd_blockmask->order_pair;
541: for ( j = 0; j < l; j++ ) {
542: mask = nd_blockmask->mask[j];
543: ord_o = op[j].order;
544: if ( ord_o < 2 ) {
545: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
546: else if ( t1 < t2 ) return -1;
547: }
548: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
549: m = mask[i];
550: t1 = d1[i]&m;
551: t2 = d2[i]&m;
552: if ( t1 > t2 )
553: return !ord_o ? -1 : 1;
554: else if ( t1 < t2 )
555: return !ord_o ? 1 : -1;
556: }
557: }
558: return 0;
559: }
560:
561: int ndl_matrix_compare(UINT *d1,UINT *d2)
562: {
563: int i,j,s,row;
564: int *v;
565: Z **mat;
566: Z *w;
567: Z t1;
568: Z t,t2;
569:
1.6 noro 570: for ( j = 0; j < nd_nvar; j++ )
571: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 572: if ( nd_top_weight ) {
573: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 574: mat = (Z **)&BDY((VECT)nd_top_weight);
575: row = 1;
1.1 noro 576: } else {
577: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 578: row = ((MAT)nd_top_weight)->row;
1.1 noro 579: }
580: for ( i = 0; i < row; i++ ) {
1.6 noro 581: w = mat[i];
1.1 noro 582: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 583: STOZ(nd_work_vector[j],t1);
1.1 noro 584: mulz(w[j],t1,&t2);
585: addz(t,t2,&t1);
586: t = t1;
587: }
1.6 noro 588: if ( t ) {
589: s = sgnz(t);
1.1 noro 590: if ( s > 0 ) return 1;
591: else if ( s < 0 ) return -1;
1.6 noro 592: }
1.1 noro 593: }
1.6 noro 594: }
595: for ( i = 0; i < nd_matrix_len; i++ ) {
596: v = nd_matrix[i];
597: for ( j = 0, s = 0; j < nd_nvar; j++ )
598: s += v[j]*nd_work_vector[j];
599: if ( s > 0 ) return 1;
600: else if ( s < 0 ) return -1;
601: }
1.1 noro 602: if ( !ndl_equal(d1,d2) )
1.6 noro 603: error("ndl_matrix_compare : invalid matrix");
604: return 0;
1.1 noro 605: }
606:
607: int ndl_composite_compare(UINT *d1,UINT *d2)
608: {
609: int i,j,s,start,end,len,o;
610: int *v;
611: struct sparse_weight *sw;
612:
613: for ( j = 0; j < nd_nvar; j++ )
614: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
615: for ( i = 0; i < nd_worb_len; i++ ) {
616: len = nd_worb[i].length;
617: switch ( nd_worb[i].type ) {
618: case IS_DENSE_WEIGHT:
619: v = nd_worb[i].body.dense_weight;
620: for ( j = 0, s = 0; j < len; j++ )
621: s += v[j]*nd_work_vector[j];
622: if ( s > 0 ) return 1;
623: else if ( s < 0 ) return -1;
624: break;
625: case IS_SPARSE_WEIGHT:
626: sw = nd_worb[i].body.sparse_weight;
627: for ( j = 0, s = 0; j < len; j++ )
628: s += sw[j].value*nd_work_vector[sw[j].pos];
629: if ( s > 0 ) return 1;
630: else if ( s < 0 ) return -1;
631: break;
632: case IS_BLOCK:
633: o = nd_worb[i].body.block.order;
634: start = nd_worb[i].body.block.start;
635: switch ( o ) {
636: case 0:
637: end = start+len;
638: for ( j = start, s = 0; j < end; j++ )
639: s += MUL_WEIGHT(nd_work_vector[j],j);
640: if ( s > 0 ) return 1;
641: else if ( s < 0 ) return -1;
642: for ( j = end-1; j >= start; j-- )
643: if ( nd_work_vector[j] < 0 ) return 1;
644: else if ( nd_work_vector[j] > 0 ) return -1;
645: break;
646: case 1:
647: end = start+len;
648: for ( j = start, s = 0; j < end; j++ )
649: s += MUL_WEIGHT(nd_work_vector[j],j);
650: if ( s > 0 ) return 1;
651: else if ( s < 0 ) return -1;
652: for ( j = start; j < end; j++ )
653: if ( nd_work_vector[j] > 0 ) return 1;
654: else if ( nd_work_vector[j] < 0 ) return -1;
655: break;
656: case 2:
657: end = start+len;
658: for ( j = start; j < end; j++ )
659: if ( nd_work_vector[j] > 0 ) return 1;
660: else if ( nd_work_vector[j] < 0 ) return -1;
661: break;
662: }
663: break;
664: }
665: }
666: return 0;
667: }
668:
669: /* TDH -> WW -> TD-> RL */
670:
671: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
672: {
673: int i,m,e1,e2;
674:
675: if ( TD(d1) > TD(d2) ) return 1;
676: else if ( TD(d1) < TD(d2) ) return -1;
677: m = nd_nvar>>1;
678: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
679: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
680: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
681: }
682: if ( e1 > e2 ) return 1;
683: else if ( e1 < e2 ) return -1;
684: return ndl_lex_compare(d1,d2);
685: }
686:
687: int ndl_module_weight_compare(UINT *d1,UINT *d2)
688: {
689: int s,j;
690:
691: if ( nd_nvar != nd_poly_weight_len )
692: error("invalid module weight : the length of polynomial weight != the number of variables");
693: s = 0;
694: for ( j = 0; j < nd_nvar; j++ )
695: s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
696: if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
697: s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
698: }
699: if ( s > 0 ) return 1;
700: else if ( s < 0 ) return -1;
701: else return 0;
702: }
703:
704: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
705: {
706: int i,c;
707:
708: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
709: if ( nd_ispot ) {
710: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
711: if ( TD(d1) > TD(d2) ) return 1;
712: else if ( TD(d1) < TD(d2) ) return -1;
713: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
714: if ( MPOS(d1) < MPOS(d2) ) return 1;
715: else if ( MPOS(d1) > MPOS(d2) ) return -1;
716: return 0;
717: }
718: if ( MPOS(d1) < MPOS(d2) ) return 1;
719: else if ( MPOS(d1) > MPOS(d2) ) return -1;
720: }
721: if ( TD(d1) > TD(d2) ) return 1;
722: else if ( TD(d1) < TD(d2) ) return -1;
723: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
724: if ( !nd_ispot ) {
725: if ( MPOS(d1) < MPOS(d2) ) return 1;
726: else if ( MPOS(d1) > MPOS(d2) ) return -1;
727: }
728: return 0;
729: }
730:
731: int ndl_module_glex_compare(UINT *d1,UINT *d2)
732: {
733: int i,c;
734:
735: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
736: if ( nd_ispot ) {
737: if ( MPOS(d1) < MPOS(d2) ) return 1;
738: else if ( MPOS(d1) > MPOS(d2) ) return -1;
739: }
740: if ( TD(d1) > TD(d2) ) return 1;
741: else if ( TD(d1) < TD(d2) ) return -1;
742: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
743: if ( !nd_ispot ) {
744: if ( MPOS(d1) < MPOS(d2) ) return 1;
745: else if ( MPOS(d1) > MPOS(d2) ) return -1;
746: }
747: return 0;
748: }
749:
750: int ndl_module_lex_compare(UINT *d1,UINT *d2)
751: {
752: int i,c;
753:
754: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
755: if ( nd_ispot ) {
756: if ( MPOS(d1) < MPOS(d2) ) return 1;
757: else if ( MPOS(d1) > MPOS(d2) ) return -1;
758: }
759: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
760: if ( !nd_ispot ) {
761: if ( MPOS(d1) < MPOS(d2) ) return 1;
762: else if ( MPOS(d1) > MPOS(d2) ) return -1;
763: }
764: return 0;
765: }
766:
767: int ndl_module_block_compare(UINT *d1,UINT *d2)
768: {
769: int i,c;
770:
771: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
772: if ( nd_ispot ) {
773: if ( MPOS(d1) < MPOS(d2) ) return 1;
774: else if ( MPOS(d1) > MPOS(d2) ) return -1;
775: }
776: if ( (c = ndl_block_compare(d1,d2)) != 0 ) return c;
777: if ( !nd_ispot ) {
778: if ( MPOS(d1) < MPOS(d2) ) return 1;
779: else if ( MPOS(d1) > MPOS(d2) ) return -1;
780: }
781: return 0;
782: }
783:
784: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
785: {
786: int i,c;
787:
788: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
789: if ( nd_ispot ) {
790: if ( MPOS(d1) < MPOS(d2) ) return 1;
791: else if ( MPOS(d1) > MPOS(d2) ) return -1;
792: }
793: if ( (c = ndl_matrix_compare(d1,d2)) != 0 ) return c;
794: if ( !nd_ispot ) {
795: if ( MPOS(d1) < MPOS(d2) ) return 1;
796: else if ( MPOS(d1) > MPOS(d2) ) return -1;
797: }
798: return 0;
799: }
800:
801: int ndl_module_composite_compare(UINT *d1,UINT *d2)
802: {
803: int i,c;
804:
805: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
806: if ( nd_ispot ) {
807: if ( MPOS(d1) > MPOS(d2) ) return 1;
808: else if ( MPOS(d1) < MPOS(d2) ) return -1;
809: }
810: if ( (c = ndl_composite_compare(d1,d2)) != 0 ) return c;
811: if ( !nd_ispot ) {
812: if ( MPOS(d1) > MPOS(d2) ) return 1;
813: else if ( MPOS(d1) < MPOS(d2) ) return -1;
814: }
815: return 0;
816: }
817:
818: INLINE int ndl_equal(UINT *d1,UINT *d2)
819: {
820: int i;
821:
822: switch ( nd_wpd ) {
823: case 2:
824: if ( TD(d2) != TD(d1) ) return 0;
825: if ( d2[1] != d1[1] ) return 0;
826: return 1;
827: break;
828: case 3:
829: if ( TD(d2) != TD(d1) ) return 0;
830: if ( d2[1] != d1[1] ) return 0;
831: if ( d2[2] != d1[2] ) return 0;
832: return 1;
833: break;
834: default:
835: for ( i = 0; i < nd_wpd; i++ )
836: if ( *d1++ != *d2++ ) return 0;
837: return 1;
838: break;
839: }
840: }
841:
842: INLINE void ndl_copy(UINT *d1,UINT *d2)
843: {
844: int i;
845:
846: switch ( nd_wpd ) {
847: case 2:
848: TD(d2) = TD(d1);
849: d2[1] = d1[1];
850: break;
851: case 3:
852: TD(d2) = TD(d1);
853: d2[1] = d1[1];
854: d2[2] = d1[2];
855: break;
856: default:
857: for ( i = 0; i < nd_wpd; i++ )
858: d2[i] = d1[i];
859: break;
860: }
861: }
862:
863: INLINE void ndl_zero(UINT *d)
864: {
865: int i;
866: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
867: }
868:
869: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
870: {
871: int i;
872:
873: if ( nd_module ) {
874: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
875: error("ndl_add : invalid operation");
876: }
877: #if 1
878: switch ( nd_wpd ) {
879: case 2:
880: TD(d) = TD(d1)+TD(d2);
881: d[1] = d1[1]+d2[1];
882: break;
883: case 3:
884: TD(d) = TD(d1)+TD(d2);
885: d[1] = d1[1]+d2[1];
886: d[2] = d1[2]+d2[2];
887: break;
888: default:
889: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
890: break;
891: }
892: #else
893: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
894: #endif
895: }
896:
897: /* d1 += d2 */
898: INLINE void ndl_addto(UINT *d1,UINT *d2)
899: {
900: int i;
901:
902: if ( nd_module ) {
903: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
904: error("ndl_addto : invalid operation");
905: }
906: #if 1
907: switch ( nd_wpd ) {
908: case 2:
909: TD(d1) += TD(d2);
910: d1[1] += d2[1];
911: break;
912: case 3:
913: TD(d1) += TD(d2);
914: d1[1] += d2[1];
915: d1[2] += d2[2];
916: break;
917: default:
918: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
919: break;
920: }
921: #else
922: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
923: #endif
924: }
925:
926: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
927: {
928: int i;
929:
930: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
931: }
932:
933: int ndl_disjoint(UINT *d1,UINT *d2)
934: {
935: UINT t1,t2,u,u1,u2;
936: int i,j;
937:
938: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
939: #if USE_UNROLL
940: switch ( nd_bpe ) {
941: case 3:
942: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
943: u1 = d1[i]; u2 = d2[i];
944: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
945: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
946: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
947: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
948: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
949: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
950: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
951: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
952: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
953: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
954: }
955: return 1;
956: break;
957: case 4:
958: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
959: u1 = d1[i]; u2 = d2[i];
960: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
961: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
962: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
963: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
964: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
965: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
966: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
967: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
968: }
969: return 1;
970: break;
971: case 6:
972: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
973: u1 = d1[i]; u2 = d2[i];
974: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
975: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
976: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
977: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
978: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
979: }
980: return 1;
981: break;
982: case 8:
983: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
984: u1 = d1[i]; u2 = d2[i];
985: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
986: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
987: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
988: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
989: }
990: return 1;
991: break;
992: case 16:
993: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
994: u1 = d1[i]; u2 = d2[i];
995: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
996: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
997: }
998: return 1;
999: break;
1000: case 32:
1001: for ( i = nd_exporigin; i < nd_wpd; i++ )
1002: if ( d1[i] && d2[i] ) return 0;
1003: return 1;
1004: break;
1005: default:
1006: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1007: u1 = d1[i]; u2 = d2[i];
1008: for ( j = 0; j < nd_epw; j++ ) {
1009: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1010: u1 >>= nd_bpe; u2 >>= nd_bpe;
1011: }
1012: }
1013: return 1;
1014: break;
1015: }
1016: #else
1017: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1018: u1 = d1[i]; u2 = d2[i];
1019: for ( j = 0; j < nd_epw; j++ ) {
1020: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1021: u1 >>= nd_bpe; u2 >>= nd_bpe;
1022: }
1023: }
1024: return 1;
1025: #endif
1026: }
1027:
1028: int ndl_check_bound(UINT *d1,UINT *d2)
1029: {
1030: UINT u2;
1031: int i,j,ind,k;
1032:
1033: ind = 0;
1034: #if USE_UNROLL
1035: switch ( nd_bpe ) {
1036: case 3:
1037: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1038: u2 = d2[i];
1039: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1040: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1041: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1042: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1043: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1044: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1045: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1046: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1047: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1048: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1049: }
1050: return 0;
1051: break;
1052: case 4:
1053: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1054: u2 = d2[i];
1055: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1056: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1057: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1058: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1059: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1060: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1061: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1062: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1063: }
1064: return 0;
1065: break;
1066: case 6:
1067: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1068: u2 = d2[i];
1069: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1070: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1071: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1072: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1073: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1074: }
1075: return 0;
1076: break;
1077: case 8:
1078: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1079: u2 = d2[i];
1080: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1081: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1082: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1083: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1084: }
1085: return 0;
1086: break;
1087: case 16:
1088: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1089: u2 = d2[i];
1090: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1091: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1092: }
1093: return 0;
1094: break;
1095: case 32:
1096: for ( i = nd_exporigin; i < nd_wpd; i++ )
1097: if ( d1[i]+d2[i]<d1[i] ) return 1;
1098: return 0;
1099: break;
1100: default:
1101: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1102: u2 = d2[i];
1103: k = (nd_epw-1)*nd_bpe;
1104: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1105: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1106: }
1107: return 0;
1108: break;
1109: }
1110: #else
1111: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1112: u2 = d2[i];
1113: k = (nd_epw-1)*nd_bpe;
1114: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1115: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1116: }
1117: return 0;
1118: #endif
1119: }
1120:
1121: int ndl_check_bound2(int index,UINT *d2)
1122: {
1123: return ndl_check_bound(nd_bound[index],d2);
1124: }
1125:
1126: INLINE int ndl_hash_value(UINT *d)
1127: {
1128: int i;
1.11 noro 1129: UINT r;
1.1 noro 1130:
1131: r = 0;
1132: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1133: r = (r*1511+d[i]);
1.11 noro 1134: r %= REDTAB_LEN;
1.1 noro 1135: return r;
1136: }
1137:
1138: INLINE int ndl_find_reducer(UINT *dg)
1139: {
1140: RHist r;
1141: int d,k,i;
1142:
1143: d = ndl_hash_value(dg);
1144: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1145: if ( ndl_equal(dg,DL(r)) ) {
1146: if ( k > 0 ) nd_notfirst++;
1147: nd_found++;
1148: return r->index;
1149: }
1150: }
1151: if ( Reverse )
1152: for ( i = nd_psn-1; i >= 0; i-- ) {
1153: r = nd_psh[i];
1154: if ( ndl_reducible(dg,DL(r)) ) {
1155: nd_create++;
1156: nd_append_red(dg,i);
1157: return i;
1158: }
1159: }
1160: else
1161: for ( i = 0; i < nd_psn; i++ ) {
1162: r = nd_psh[i];
1163: if ( ndl_reducible(dg,DL(r)) ) {
1164: nd_create++;
1165: nd_append_red(dg,i);
1166: return i;
1167: }
1168: }
1169: return -1;
1170: }
1171:
1172: ND nd_merge(ND p1,ND p2)
1173: {
1174: int n,c;
1175: int t,can,td1,td2;
1176: ND r;
1177: NM m1,m2,mr0,mr,s;
1178:
1179: if ( !p1 ) return p2;
1180: else if ( !p2 ) return p1;
1181: else {
1182: can = 0;
1183: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1184: c = DL_COMPARE(DL(m1),DL(m2));
1185: switch ( c ) {
1186: case 0:
1187: s = m1; m1 = NEXT(m1);
1188: can++; NEXTNM2(mr0,mr,s);
1189: s = m2; m2 = NEXT(m2); FREENM(s);
1190: break;
1191: case 1:
1192: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1193: break;
1194: case -1:
1195: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1196: break;
1197: }
1198: }
1199: if ( !mr0 )
1200: if ( m1 ) mr0 = m1;
1201: else if ( m2 ) mr0 = m2;
1202: else return 0;
1203: else if ( m1 ) NEXT(mr) = m1;
1204: else if ( m2 ) NEXT(mr) = m2;
1205: else NEXT(mr) = 0;
1206: BDY(p1) = mr0;
1207: SG(p1) = MAX(SG(p1),SG(p2));
1208: LEN(p1) = LEN(p1)+LEN(p2)-can;
1209: FREEND(p2);
1210: return p1;
1211: }
1212: }
1213:
1214: ND nd_add(int mod,ND p1,ND p2)
1215: {
1216: int n,c;
1217: int t,can,td1,td2;
1218: ND r;
1219: NM m1,m2,mr0,mr,s;
1220:
1.11 noro 1221: Nnd_add++;
1.1 noro 1222: if ( !p1 ) return p2;
1223: else if ( !p2 ) return p1;
1224: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1225: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1226: else if ( !mod ) return nd_add_q(p1,p2);
1227: else {
1228: can = 0;
1229: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1230: c = DL_COMPARE(DL(m1),DL(m2));
1231: switch ( c ) {
1232: case 0:
1233: t = ((CM(m1))+(CM(m2))) - mod;
1234: if ( t < 0 ) t += mod;
1235: s = m1; m1 = NEXT(m1);
1236: if ( t ) {
1237: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1238: } else {
1239: can += 2; FREENM(s);
1240: }
1241: s = m2; m2 = NEXT(m2); FREENM(s);
1242: break;
1243: case 1:
1244: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1245: break;
1246: case -1:
1247: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1248: break;
1249: }
1250: }
1251: if ( !mr0 )
1252: if ( m1 ) mr0 = m1;
1253: else if ( m2 ) mr0 = m2;
1254: else return 0;
1255: else if ( m1 ) NEXT(mr) = m1;
1256: else if ( m2 ) NEXT(mr) = m2;
1257: else NEXT(mr) = 0;
1258: BDY(p1) = mr0;
1259: SG(p1) = MAX(SG(p1),SG(p2));
1260: LEN(p1) = LEN(p1)+LEN(p2)-can;
1261: FREEND(p2);
1262: return p1;
1263: }
1264: }
1265:
1266: /* XXX on opteron, the inlined manipulation of destructive additon of
1267: * two NM seems to make gcc optimizer get confused, so the part is
1268: * done in a function.
1269: */
1270:
1271: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1272: {
1273: NM s;
1274: P t;
1275: int can;
1276:
1277: addp(nd_vc,CP(*m1),CP(*m2),&t);
1278: s = *m1; *m1 = NEXT(*m1);
1279: if ( t ) {
1280: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1281: } else {
1282: can = 2; FREENM(s);
1283: }
1284: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1285: return can;
1286: }
1287:
1288: ND nd_add_q(ND p1,ND p2)
1289: {
1290: int n,c,can;
1291: ND r;
1292: NM m1,m2,mr0,mr,s;
1293: P t;
1294:
1295: if ( !p1 ) return p2;
1296: else if ( !p2 ) return p1;
1297: else {
1298: can = 0;
1299: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1300: c = DL_COMPARE(DL(m1),DL(m2));
1301: switch ( c ) {
1302: case 0:
1303: #if defined(__x86_64__)
1304: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1305: #else
1306: addp(nd_vc,CP(m1),CP(m2),&t);
1307: s = m1; m1 = NEXT(m1);
1308: if ( t ) {
1309: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1310: } else {
1311: can += 2; FREENM(s);
1312: }
1313: s = m2; m2 = NEXT(m2); FREENM(s);
1314: #endif
1315: break;
1316: case 1:
1317: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1318: break;
1319: case -1:
1320: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1321: break;
1322: }
1323: }
1324: if ( !mr0 )
1325: if ( m1 ) mr0 = m1;
1326: else if ( m2 ) mr0 = m2;
1327: else return 0;
1328: else if ( m1 ) NEXT(mr) = m1;
1329: else if ( m2 ) NEXT(mr) = m2;
1330: else NEXT(mr) = 0;
1331: BDY(p1) = mr0;
1332: SG(p1) = MAX(SG(p1),SG(p2));
1333: LEN(p1) = LEN(p1)+LEN(p2)-can;
1334: FREEND(p2);
1335: return p1;
1336: }
1337: }
1338:
1339: ND nd_add_sf(ND p1,ND p2)
1340: {
1341: int n,c,can;
1342: ND r;
1343: NM m1,m2,mr0,mr,s;
1344: int t;
1345:
1346: if ( !p1 ) return p2;
1347: else if ( !p2 ) return p1;
1348: else {
1349: can = 0;
1350: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1351: c = DL_COMPARE(DL(m1),DL(m2));
1352: switch ( c ) {
1353: case 0:
1354: t = _addsf(CM(m1),CM(m2));
1355: s = m1; m1 = NEXT(m1);
1356: if ( t ) {
1357: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1358: } else {
1359: can += 2; FREENM(s);
1360: }
1361: s = m2; m2 = NEXT(m2); FREENM(s);
1362: break;
1363: case 1:
1364: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1365: break;
1366: case -1:
1367: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1368: break;
1369: }
1370: }
1371: if ( !mr0 )
1372: if ( m1 ) mr0 = m1;
1373: else if ( m2 ) mr0 = m2;
1374: else return 0;
1375: else if ( m1 ) NEXT(mr) = m1;
1376: else if ( m2 ) NEXT(mr) = m2;
1377: else NEXT(mr) = 0;
1378: BDY(p1) = mr0;
1379: SG(p1) = MAX(SG(p1),SG(p2));
1380: LEN(p1) = LEN(p1)+LEN(p2)-can;
1381: FREEND(p2);
1382: return p1;
1383: }
1384: }
1385:
1386:
1387: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1388: {
1389: int c,c1,c2;
1390: Z cg,cred,gcd,tq;
1391: P cgp,credp,gcdp;
1392: Obj tr,tr1;
1393:
1394: if ( mod == -1 ) {
1395: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1396: *divp = (Obj)ONE;
1397: } else if ( mod == -2 ) {
1398: Z inv,t;
1399: divlf(ONE,HCZ(p),&inv);
1400: chsgnlf(HCZ(g),&t);
1401: mullf(inv,t,&CZ(mul));
1402: *divp = (Obj)ONE;
1403: } else if ( mod ) {
1404: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1405: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1406: *divp = (Obj)ONE;
1407: } else if ( nd_vc ) {
1408: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1409: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1410: chsgnp(cgp,&CP(mul));
1411: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1412: if ( dn ) {
1413: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1414: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1415: }
1416: *divp = (Obj)credp;
1417: } else {
1.6 noro 1418: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1419: chsgnz(cg,&CZ(mul));
1.1 noro 1420: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1421: if ( dn ) {
1422: mulz(dn->z,cred,&tq); dn->z = tq;
1423: }
1424: *divp = (Obj)cred;
1425: }
1426: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1427: }
1428:
1429: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1430: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1431: {
1432: NM m,mrd,tail;
1433: NM mul;
1434: int n,sugar,psugar,sugar0,stat,index;
1435: int c,c1,c2,dummy;
1436: RHist h;
1437: NDV p,red;
1438: Q cg,cred,gcd,tq,qq;
1439: Z iq;
1440: DP dmul;
1441: NODE node;
1442: LIST hist;
1443: double hmag;
1444: P tp,tp1;
1445: Obj tr,tr1,div;
1446: union oNDC hg;
1447: P cont;
1448:
1449: if ( !g ) {
1450: *rp = d;
1451: return 1;
1452: }
1453: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1454:
1455: sugar0 = sugar = SG(g);
1456: n = NV(g);
1457: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1458: if ( d )
1459: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1460: for ( ; g; ) {
1461: index = ndl_find_reducer(HDL(g));
1462: if ( index >= 0 ) {
1463: h = nd_psh[index];
1464: ndl_sub(HDL(g),DL(h),DL(mul));
1465: if ( ndl_check_bound2(index,DL(mul)) ) {
1466: nd_free(g); nd_free(d);
1467: return 0;
1468: }
1469: p = nd_demand ? ndv_load(index) : ps[index];
1470: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1471: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1472: if ( nd_gentrace ) {
1473: /* Trace=[div,index,mul,ONE] */
1.6 noro 1474: STOZ(index,iq);
1.1 noro 1475: nmtodp(mod,mul,&dmul);
1476: node = mknode(4,div,iq,dmul,ONE);
1477: }
1478: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1479: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1480: hg = HCU(g);
1481: nd_removecont2(d,g);
1.6 noro 1482: if ( nd_gentrace ) {
1.1 noro 1483: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1484: /* exact division */
1.1 noro 1485: cont = ndc_div(mod,hg,HCU(g));
1486: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1487: }
1488: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1489: }
1490: MKLIST(hist,node);
1491: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1492: } else if ( !full ) {
1493: *rp = g;
1494: return 1;
1495: } else {
1496: m = BDY(g);
1497: if ( NEXT(m) ) {
1498: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1499: } else {
1500: FREEND(g); g = 0;
1501: }
1502: if ( d ) {
1503: NEXT(tail)=m; tail=m; LEN(d)++;
1504: } else {
1505: MKND(n,m,1,d); tail = BDY(d);
1506: }
1507: }
1508: }
1509: if ( d ) SG(d) = sugar;
1510: *rp = d;
1511: return 1;
1512: }
1513:
1514: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1515: {
1516: int hindex,index;
1517: NDV p;
1518: ND u,d,red;
1519: NODE l;
1520: NM mul,m,mrd,tail;
1521: int sugar,psugar,n,h_reducible;
1522: PGeoBucket bucket;
1523: int c,c1,c2;
1524: Z cg,cred,gcd,zzz;
1525: RHist h;
1526: double hmag,gmag;
1527: int count = 0;
1528: int hcount = 0;
1529:
1530: if ( !g ) {
1531: *rp = 0;
1532: return 1;
1533: }
1534: sugar = SG(g);
1535: n = NV(g);
1.6 noro 1536: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1537: bucket = create_pbucket();
1538: add_pbucket(mod,bucket,g);
1539: d = 0;
1540: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1541: while ( 1 ) {
1542: if ( mod > 0 || mod == -1 )
1543: hindex = head_pbucket(mod,bucket);
1544: else if ( mod == -2 )
1545: hindex = head_pbucket_lf(bucket);
1546: else
1547: hindex = head_pbucket_q(bucket);
1548: if ( hindex < 0 ) {
1549: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1550: if ( d ) SG(d) = sugar;
1551: *rp = d;
1552: return 1;
1553: }
1554: g = bucket->body[hindex];
1555: index = ndl_find_reducer(HDL(g));
1556: if ( index >= 0 ) {
1557: count++;
1558: if ( !d ) hcount++;
1559: h = nd_psh[index];
1560: ndl_sub(HDL(g),DL(h),DL(mul));
1561: if ( ndl_check_bound2(index,DL(mul)) ) {
1562: nd_free(d);
1563: free_pbucket(bucket);
1564: *rp = 0;
1565: return 0;
1566: }
1567: p = ps[index];
1568: if ( mod == -1 )
1569: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1570: else if ( mod == -2 ) {
1571: Z inv,t;
1572: divlf(ONE,HCZ(p),&inv);
1573: chsgnlf(HCZ(g),&t);
1574: mullf(inv,t,&CZ(mul));
1575: } else if ( mod ) {
1576: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1577: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1578: } else {
1.6 noro 1579: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1580: chsgnz(cg,&CZ(mul));
1.1 noro 1581: nd_mul_c_q(d,(P)cred);
1582: mulq_pbucket(bucket,cred);
1583: g = bucket->body[hindex];
1.6 noro 1584: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1585: }
1586: red = ndv_mul_nm(mod,mul,p);
1587: bucket->body[hindex] = nd_remove_head(g);
1588: red = nd_remove_head(red);
1589: add_pbucket(mod,bucket,red);
1590: psugar = SG(p)+TD(DL(mul));
1591: sugar = MAX(sugar,psugar);
1592: if ( !mod && hmag && (gmag > hmag) ) {
1593: g = normalize_pbucket(mod,bucket);
1594: if ( !g ) {
1595: if ( d ) SG(d) = sugar;
1596: *rp = d;
1597: return 1;
1598: }
1599: nd_removecont2(d,g);
1.6 noro 1600: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1601: add_pbucket(mod,bucket,g);
1602: }
1603: } else if ( !full ) {
1604: g = normalize_pbucket(mod,bucket);
1605: if ( g ) SG(g) = sugar;
1606: *rp = g;
1607: return 1;
1608: } else {
1609: m = BDY(g);
1610: if ( NEXT(m) ) {
1611: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1612: } else {
1613: FREEND(g); g = 0;
1614: }
1615: bucket->body[hindex] = g;
1616: NEXT(m) = 0;
1617: if ( d ) {
1618: NEXT(tail)=m; tail=m; LEN(d)++;
1619: } else {
1620: MKND(n,m,1,d); tail = BDY(d);
1621: }
1622: }
1623: }
1624: }
1625:
1626: /* input : list of NDV, cand : list of NDV */
1627:
1628: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1629: {
1630: int n,i,stat;
1631: ND nf,d;
1632: NDV r;
1633: NODE t,s;
1634: union oNDC dn;
1635: Z q;
1636: LIST list;
1637:
1638: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1639: n = length(cand);
1640:
1641: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1642: /* membercheck : list is a subset of Id(cand) ? */
1643: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1644: again:
1645: nd_tracelist = 0;
1646: if ( nd_bpe > obpe )
1647: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1648: else
1649: r = (NDV)BDY(t);
1650: #if 0
1651: // moved to nd_f4_lf_trace()
1652: if ( m == -2 ) ndv_mod(m,r);
1653: #endif
1654: d = ndvtond(m,r);
1.6 noro 1655: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1656: if ( !stat ) {
1657: nd_reconstruct(0,0);
1658: goto again;
1659: } else if ( nf ) return 0;
1660: if ( nd_gentrace ) {
1661: nd_tracelist = reverse_node(nd_tracelist);
1662: MKLIST(list,nd_tracelist);
1.6 noro 1663: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1664: MKNODE(s,list,nd_alltracelist);
1665: nd_alltracelist = s; nd_tracelist = 0;
1666: }
1667: if ( DP_Print ) { printf("."); fflush(stdout); }
1668: }
1669: if ( DP_Print ) { printf("\n"); }
1670: return 1;
1671: }
1672:
1673: ND nd_remove_head(ND p)
1674: {
1675: NM m;
1676:
1677: m = BDY(p);
1678: if ( !NEXT(m) ) {
1679: FREEND(p); p = 0;
1680: } else {
1681: BDY(p) = NEXT(m); LEN(p)--;
1682: }
1683: FREENM(m);
1684: return p;
1685: }
1686:
1687: ND nd_separate_head(ND p,ND *head)
1688: {
1689: NM m,m0;
1690: ND r;
1691:
1692: m = BDY(p);
1693: if ( !NEXT(m) ) {
1694: *head = p; p = 0;
1695: } else {
1696: m0 = m;
1697: BDY(p) = NEXT(m); LEN(p)--;
1698: NEXT(m0) = 0;
1699: MKND(NV(p),m0,1,r);
1700: *head = r;
1701: }
1702: return p;
1703: }
1704:
1705: PGeoBucket create_pbucket()
1706: {
1707: PGeoBucket g;
1708:
1709: g = CALLOC(1,sizeof(struct oPGeoBucket));
1710: g->m = -1;
1711: return g;
1712: }
1713:
1714: void free_pbucket(PGeoBucket b) {
1715: int i;
1716:
1717: for ( i = 0; i <= b->m; i++ )
1718: if ( b->body[i] ) {
1719: nd_free(b->body[i]);
1720: b->body[i] = 0;
1721: }
1722: GCFREE(b);
1723: }
1724:
1725: void add_pbucket_symbolic(PGeoBucket g,ND d)
1726: {
1727: int l,i,k,m;
1728:
1729: if ( !d )
1730: return;
1731: l = LEN(d);
1732: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1733: /* 2^(k-1) < l <= 2^k (=m) */
1734: d = nd_merge(g->body[k],d);
1735: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1736: g->body[k] = 0;
1737: d = nd_merge(g->body[k+1],d);
1738: }
1739: g->body[k] = d;
1740: g->m = MAX(g->m,k);
1741: }
1742:
1743: void add_pbucket(int mod,PGeoBucket g,ND d)
1744: {
1745: int l,i,k,m;
1746:
1747: if ( !d )
1748: return;
1749: l = LEN(d);
1750: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1751: /* 2^(k-1) < l <= 2^k (=m) */
1752: d = nd_add(mod,g->body[k],d);
1753: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1754: g->body[k] = 0;
1755: d = nd_add(mod,g->body[k+1],d);
1756: }
1757: g->body[k] = d;
1758: g->m = MAX(g->m,k);
1759: }
1760:
1761: void mulq_pbucket(PGeoBucket g,Z c)
1762: {
1763: int k;
1764:
1765: for ( k = 0; k <= g->m; k++ )
1766: nd_mul_c_q(g->body[k],(P)c);
1767: }
1768:
1769: NM remove_head_pbucket_symbolic(PGeoBucket g)
1770: {
1771: int j,i,k,c;
1772: NM head;
1773:
1774: k = g->m;
1775: j = -1;
1776: for ( i = 0; i <= k; i++ ) {
1777: if ( !g->body[i] ) continue;
1778: if ( j < 0 ) j = i;
1779: else {
1780: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1781: if ( c > 0 )
1782: j = i;
1783: else if ( c == 0 )
1784: g->body[i] = nd_remove_head(g->body[i]);
1785: }
1786: }
1787: if ( j < 0 ) return 0;
1788: else {
1789: head = BDY(g->body[j]);
1790: if ( !NEXT(head) ) {
1791: FREEND(g->body[j]);
1792: g->body[j] = 0;
1793: } else {
1794: BDY(g->body[j]) = NEXT(head);
1795: LEN(g->body[j])--;
1796: }
1797: return head;
1798: }
1799: }
1800:
1801: int head_pbucket(int mod,PGeoBucket g)
1802: {
1803: int j,i,c,k,nv,sum;
1804: UINT *di,*dj;
1805: ND gi,gj;
1806:
1807: k = g->m;
1808: while ( 1 ) {
1809: j = -1;
1810: for ( i = 0; i <= k; i++ ) {
1811: if ( !(gi = g->body[i]) )
1812: continue;
1813: if ( j < 0 ) {
1814: j = i;
1815: gj = g->body[j];
1816: dj = HDL(gj);
1817: sum = HCM(gj);
1818: } else {
1819: c = DL_COMPARE(HDL(gi),dj);
1820: if ( c > 0 ) {
1821: if ( sum ) HCM(gj) = sum;
1822: else g->body[j] = nd_remove_head(gj);
1823: j = i;
1824: gj = g->body[j];
1825: dj = HDL(gj);
1826: sum = HCM(gj);
1827: } else if ( c == 0 ) {
1828: if ( mod == -1 )
1829: sum = _addsf(sum,HCM(gi));
1830: else {
1831: sum = sum+HCM(gi)-mod;
1832: if ( sum < 0 ) sum += mod;
1833: }
1834: g->body[i] = nd_remove_head(gi);
1835: }
1836: }
1837: }
1838: if ( j < 0 ) return -1;
1839: else if ( sum ) {
1840: HCM(gj) = sum;
1841: return j;
1842: } else
1843: g->body[j] = nd_remove_head(gj);
1844: }
1845: }
1846:
1847: int head_pbucket_q(PGeoBucket g)
1848: {
1849: int j,i,c,k,nv;
1850: Z sum,t;
1851: ND gi,gj;
1852:
1853: k = g->m;
1854: while ( 1 ) {
1855: j = -1;
1856: for ( i = 0; i <= k; i++ ) {
1857: if ( !(gi = g->body[i]) ) continue;
1858: if ( j < 0 ) {
1859: j = i;
1860: gj = g->body[j];
1.6 noro 1861: sum = HCZ(gj);
1.1 noro 1862: } else {
1863: nv = NV(gi);
1864: c = DL_COMPARE(HDL(gi),HDL(gj));
1865: if ( c > 0 ) {
1.6 noro 1866: if ( sum ) HCZ(gj) = sum;
1.1 noro 1867: else g->body[j] = nd_remove_head(gj);
1868: j = i;
1869: gj = g->body[j];
1.6 noro 1870: sum = HCZ(gj);
1.1 noro 1871: } else if ( c == 0 ) {
1.6 noro 1872: addz(sum,HCZ(gi),&t);
1.1 noro 1873: sum = t;
1874: g->body[i] = nd_remove_head(gi);
1875: }
1876: }
1877: }
1878: if ( j < 0 ) return -1;
1879: else if ( sum ) {
1.6 noro 1880: HCZ(gj) = sum;
1.1 noro 1881: return j;
1882: } else
1883: g->body[j] = nd_remove_head(gj);
1884: }
1885: }
1886:
1887: int head_pbucket_lf(PGeoBucket g)
1888: {
1889: int j,i,c,k,nv;
1890: Z sum,t;
1891: ND gi,gj;
1892:
1893: k = g->m;
1894: while ( 1 ) {
1895: j = -1;
1896: for ( i = 0; i <= k; i++ ) {
1897: if ( !(gi = g->body[i]) ) continue;
1898: if ( j < 0 ) {
1899: j = i;
1900: gj = g->body[j];
1901: sum = HCZ(gj);
1902: } else {
1903: nv = NV(gi);
1904: c = DL_COMPARE(HDL(gi),HDL(gj));
1905: if ( c > 0 ) {
1906: if ( sum ) HCZ(gj) = sum;
1907: else g->body[j] = nd_remove_head(gj);
1908: j = i;
1909: gj = g->body[j];
1910: sum = HCZ(gj);
1911: } else if ( c == 0 ) {
1912: addlf(sum,HCZ(gi),&t);
1913: sum = t;
1914: g->body[i] = nd_remove_head(gi);
1915: }
1916: }
1917: }
1918: if ( j < 0 ) return -1;
1919: else if ( sum ) {
1920: HCZ(gj) = sum;
1921: return j;
1922: } else
1923: g->body[j] = nd_remove_head(gj);
1924: }
1925: }
1926:
1927: ND normalize_pbucket(int mod,PGeoBucket g)
1928: {
1929: int i;
1930: ND r,t;
1931:
1932: r = 0;
1933: for ( i = 0; i <= g->m; i++ ) {
1934: r = nd_add(mod,r,g->body[i]);
1935: g->body[i] = 0;
1936: }
1937: g->m = -1;
1938: return r;
1939: }
1940:
1941: #if 0
1942: void register_hcf(NDV p)
1943: {
1944: DCP dc,t;
1945: P hc,h;
1946: int c;
1947: NODE l,l1,prev;
1948:
1949: hc = p->body->c.p;
1950: if ( !nd_vc || NUM(hc) ) return;
1951: fctrp(nd_vc,hc,&dc);
1952: for ( t = dc; t; t = NEXT(t) ) {
1953: h = t->c;
1954: if ( NUM(h) ) continue;
1955: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1956: c = compp(nd_vc,h,(P)BDY(l));
1957: if ( c >= 0 ) break;
1958: }
1959: if ( !l || c > 0 ) {
1960: MKNODE(l1,h,l);
1961: if ( !prev )
1962: nd_hcf = l1;
1963: else
1964: NEXT(prev) = l1;
1965: }
1966: }
1967: }
1968: #else
1969: void register_hcf(NDV p)
1970: {
1971: DCP dc,t;
1972: P hc,h,q;
1973: Q dmy;
1974: int c;
1975: NODE l,l1,prev;
1976:
1977: hc = p->body->c.p;
1978: if ( NUM(hc) ) return;
1979: ptozp(hc,1,&dmy,&h);
1980: #if 1
1981: for ( l = nd_hcf; l; l = NEXT(l) ) {
1982: while ( 1 ) {
1983: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
1984: else break;
1985: }
1986: }
1987: if ( NUM(h) ) return;
1988: #endif
1989: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1990: c = compp(nd_vc,h,(P)BDY(l));
1991: if ( c >= 0 ) break;
1992: }
1993: if ( !l || c > 0 ) {
1994: MKNODE(l1,h,l);
1995: if ( !prev )
1996: nd_hcf = l1;
1997: else
1998: NEXT(prev) = l1;
1999: }
2000: }
2001: #endif
2002:
2003: int do_diagonalize(int sugar,int m)
2004: {
1.6 noro 2005: int i,nh,stat;
2006: NODE r,g,t;
2007: ND h,nf,s,head;
2008: NDV nfv;
2009: Q q;
2010: P nm,nmp,dn,mnp,dnp,cont,cont1;
2011: union oNDC hc;
2012: NODE node;
2013: LIST l;
2014: Z iq;
1.1 noro 2015:
1.6 noro 2016: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2017: if ( nd_gentrace ) {
2018: /* Trace = [1,index,1,1] */
2019: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2020: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2021: }
2022: if ( nd_demand )
2023: nfv = ndv_load(i);
2024: else
2025: nfv = nd_ps[i];
2026: s = ndvtond(m,nfv);
2027: s = nd_separate_head(s,&head);
2028: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2029: if ( !stat ) return 0;
2030: ndv_free(nfv);
2031: hc = HCU(nf); nd_removecont(m,nf);
2032: /* exact division */
2033: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2034: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2035: nfv = ndtondv(m,nf);
2036: nd_free(nf);
2037: nd_bound[i] = ndv_compute_bound(nfv);
2038: if ( !m ) register_hcf(nfv);
2039: if ( nd_demand ) {
2040: ndv_save(nfv,i);
2041: ndv_free(nfv);
2042: } else
2043: nd_ps[i] = nfv;
2044: }
2045: return 1;
1.1 noro 2046: }
2047:
2048: LIST compute_splist()
2049: {
2050: NODE g,tn0,tn,node;
2051: LIST l0;
2052: ND_pairs d,t;
2053: int i;
2054: Z i1,i2;
2055:
2056: g = 0; d = 0;
2057: for ( i = 0; i < nd_psn; i++ ) {
2058: d = update_pairs(d,g,i,0);
2059: g = update_base(g,i);
2060: }
2061: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2062: NEXTNODE(tn0,tn);
1.6 noro 2063: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2064: node = mknode(2,i1,i2); MKLIST(l0,node);
2065: BDY(tn) = l0;
2066: }
2067: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2068: return l0;
2069: }
2070:
2071: /* return value = 0 => input is not a GB */
2072:
2073: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2074: {
1.6 noro 2075: int i,nh,sugar,stat;
2076: NODE r,g,t;
2077: ND_pairs d;
2078: ND_pairs l;
2079: ND h,nf,s,head,nf1;
2080: NDV nfv;
2081: Z q;
2082: union oNDC dn,hc;
2083: int diag_count = 0;
2084: P cont;
2085: LIST list;
2086:
1.11 noro 2087: Nnd_add = 0;
1.6 noro 2088: g = 0; d = 0;
2089: for ( i = 0; i < nd_psn; i++ ) {
2090: d = update_pairs(d,g,i,gensyz);
2091: g = update_base(g,i);
2092: }
2093: sugar = 0;
2094: while ( d ) {
1.1 noro 2095: again:
1.6 noro 2096: l = nd_minp(d,&d);
2097: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2098: if ( SG(l) != sugar ) {
2099: if ( ishomo ) {
2100: diag_count = 0;
2101: stat = do_diagonalize(sugar,m);
1.1 noro 2102: if ( !stat ) {
1.6 noro 2103: NEXT(l) = d; d = l;
2104: d = nd_reconstruct(0,d);
2105: goto again;
1.1 noro 2106: }
1.6 noro 2107: }
2108: sugar = SG(l);
2109: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2110: }
2111: stat = nd_sp(m,0,l,&h);
2112: if ( !stat ) {
2113: NEXT(l) = d; d = l;
2114: d = nd_reconstruct(0,d);
2115: goto again;
2116: }
1.1 noro 2117: #if USE_GEOBUCKET
1.6 noro 2118: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2119: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2120: #else
1.6 noro 2121: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2122: #endif
1.6 noro 2123: if ( !stat ) {
2124: NEXT(l) = d; d = l;
2125: d = nd_reconstruct(0,d);
2126: goto again;
2127: } else if ( nf ) {
2128: if ( checkonly || gensyz ) return 0;
1.1 noro 2129: if ( nd_newelim ) {
2130: if ( nd_module ) {
2131: if ( MPOS(HDL(nf)) > 1 ) return 0;
2132: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2133: }
1.6 noro 2134: if ( DP_Print ) { printf("+"); fflush(stdout); }
2135: hc = HCU(nf);
2136: nd_removecont(m,nf);
2137: if ( !m && nd_nalg ) {
2138: nd_monic(0,&nf);
2139: nd_removecont(m,nf);
2140: }
2141: if ( nd_gentrace ) {
2142: /* exact division */
1.1 noro 2143: cont = ndc_div(m,hc,HCU(nf));
2144: if ( m || !UNIQ(cont) ) {
1.6 noro 2145: t = mknode(4,NULLP,NULLP,NULLP,cont);
2146: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2147: nd_tracelist = t;
2148: }
2149: }
1.6 noro 2150: nfv = ndtondv(m,nf); nd_free(nf);
2151: nh = ndv_newps(m,nfv,0,0);
2152: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2153: diag_count = 0;
2154: stat = do_diagonalize(sugar,m);
2155: if ( !stat ) {
2156: NEXT(l) = d; d = l;
2157: d = nd_reconstruct(1,d);
2158: goto again;
1.1 noro 2159: }
1.6 noro 2160: }
2161: d = update_pairs(d,g,nh,0);
2162: g = update_base(g,nh);
2163: FREENDP(l);
2164: } else {
2165: if ( nd_gentrace && gensyz ) {
2166: nd_tracelist = reverse_node(nd_tracelist);
2167: MKLIST(list,nd_tracelist);
2168: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2169: MKNODE(t,list,nd_alltracelist);
2170: nd_alltracelist = t; nd_tracelist = 0;
2171: }
2172: if ( DP_Print ) { printf("."); fflush(stdout); }
2173: FREENDP(l);
2174: }
2175: }
2176: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2177: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.1 noro 2178: return g;
2179: }
2180:
2181: /* splist = [[i1,i2],...] */
2182:
2183: int check_splist(int m,NODE splist)
2184: {
2185: NODE t,p;
2186: ND_pairs d,r,l;
2187: int stat;
2188: ND h,nf;
2189:
2190: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2191: p = BDY((LIST)BDY(t));
1.6 noro 2192: NEXTND_pairs(d,r);
2193: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2194: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2195: SG(r) = TD(LCM(r)); /* XXX */
2196: }
2197: if ( d ) NEXT(r) = 0;
2198:
1.6 noro 2199: while ( d ) {
1.1 noro 2200: again:
1.6 noro 2201: l = nd_minp(d,&d);
2202: stat = nd_sp(m,0,l,&h);
2203: if ( !stat ) {
2204: NEXT(l) = d; d = l;
2205: d = nd_reconstruct(0,d);
2206: goto again;
2207: }
2208: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2209: if ( !stat ) {
2210: NEXT(l) = d; d = l;
2211: d = nd_reconstruct(0,d);
2212: goto again;
2213: } else if ( nf ) return 0;
1.1 noro 2214: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2215: }
1.1 noro 2216: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2217: return 1;
2218: }
2219:
2220: int check_splist_f4(int m,NODE splist)
2221: {
2222: UINT *s0vect;
1.6 noro 2223: PGeoBucket bucket;
1.1 noro 2224: NODE p,rp0,t;
2225: ND_pairs d,r,l,ll;
2226: int col,stat;
2227:
2228: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2229: p = BDY((LIST)BDY(t));
1.6 noro 2230: NEXTND_pairs(d,r);
2231: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2232: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2233: SG(r) = TD(LCM(r)); /* XXX */
2234: }
2235: if ( d ) NEXT(r) = 0;
2236:
1.6 noro 2237: while ( d ) {
2238: l = nd_minsugarp(d,&d);
2239: bucket = create_pbucket();
2240: stat = nd_sp_f4(m,0,l,bucket);
2241: if ( !stat ) {
2242: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2243: NEXT(ll) = d; d = l;
2244: d = nd_reconstruct(0,d);
2245: continue;
2246: }
2247: if ( bucket->m < 0 ) continue;
2248: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2249: if ( !col ) {
2250: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2251: NEXT(ll) = d; d = l;
2252: d = nd_reconstruct(0,d);
2253: continue;
1.1 noro 2254: }
1.6 noro 2255: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2256: }
2257: return 1;
1.1 noro 2258: }
2259:
2260: int do_diagonalize_trace(int sugar,int m)
2261: {
1.6 noro 2262: int i,nh,stat;
2263: NODE r,g,t;
2264: ND h,nf,nfq,s,head;
2265: NDV nfv,nfqv;
2266: Q q,den,num;
2267: union oNDC hc;
2268: NODE node;
2269: LIST l;
2270: Z iq;
2271: P cont,cont1;
1.1 noro 2272:
1.6 noro 2273: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2274: if ( nd_gentrace ) {
2275: /* Trace = [1,index,1,1] */
2276: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2277: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2278: }
2279: /* for nd_ps */
2280: s = ndvtond(m,nd_ps[i]);
2281: s = nd_separate_head(s,&head);
2282: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2283: if ( !stat ) return 0;
2284: nf = nd_add(m,head,nf);
2285: ndv_free(nd_ps[i]);
2286: nd_ps[i] = ndtondv(m,nf);
2287: nd_free(nf);
2288:
2289: /* for nd_ps_trace */
2290: if ( nd_demand )
2291: nfv = ndv_load(i);
2292: else
2293: nfv = nd_ps_trace[i];
2294: s = ndvtond(0,nfv);
2295: s = nd_separate_head(s,&head);
2296: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2297: if ( !stat ) return 0;
2298: ndv_free(nfv);
2299: hc = HCU(nf); nd_removecont(0,nf);
2300: /* exact division */
1.1 noro 2301: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2302: if ( nd_gentrace ) finalize_tracelist(i,cont);
2303: nfv = ndtondv(0,nf);
2304: nd_free(nf);
2305: nd_bound[i] = ndv_compute_bound(nfv);
2306: register_hcf(nfv);
2307: if ( nd_demand ) {
2308: ndv_save(nfv,i);
2309: ndv_free(nfv);
2310: } else
2311: nd_ps_trace[i] = nfv;
2312: }
2313: return 1;
1.1 noro 2314: }
2315:
2316: static struct oEGT eg_invdalg;
2317: struct oEGT eg_le;
2318:
2319: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2320: {
2321: NODE tn;
2322: P p1;
2323:
2324: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2325: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2326: }
2327: *r = p;
2328: }
2329:
2330: NODE nd_gb_trace(int m,int ishomo,int **indp)
2331: {
1.6 noro 2332: int i,nh,sugar,stat;
2333: NODE r,g,t;
2334: ND_pairs d;
2335: ND_pairs l;
2336: ND h,nf,nfq,s,head;
2337: NDV nfv,nfqv;
2338: Z q,den,num;
2339: P hc;
2340: union oNDC dn,hnfq;
2341: struct oEGT eg_monic,egm0,egm1;
2342: int diag_count = 0;
2343: P cont;
2344: LIST list;
2345:
2346: init_eg(&eg_monic);
2347: init_eg(&eg_invdalg);
2348: init_eg(&eg_le);
2349: g = 0; d = 0;
2350: for ( i = 0; i < nd_psn; i++ ) {
2351: d = update_pairs(d,g,i,0);
2352: g = update_base(g,i);
2353: }
2354: sugar = 0;
2355: while ( d ) {
1.1 noro 2356: again:
1.6 noro 2357: l = nd_minp(d,&d);
2358: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2359: if ( SG(l) != sugar ) {
1.1 noro 2360: #if 1
1.6 noro 2361: if ( ishomo ) {
2362: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2363: stat = do_diagonalize_trace(sugar,m);
2364: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2365: diag_count = 0;
1.1 noro 2366: if ( !stat ) {
1.6 noro 2367: NEXT(l) = d; d = l;
2368: d = nd_reconstruct(1,d);
2369: goto again;
1.1 noro 2370: }
1.6 noro 2371: }
2372: #endif
2373: sugar = SG(l);
2374: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2375: }
2376: stat = nd_sp(m,0,l,&h);
2377: if ( !stat ) {
2378: NEXT(l) = d; d = l;
2379: d = nd_reconstruct(1,d);
2380: goto again;
2381: }
1.1 noro 2382: #if USE_GEOBUCKET
1.6 noro 2383: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2384: #else
1.6 noro 2385: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2386: #endif
1.6 noro 2387: if ( !stat ) {
2388: NEXT(l) = d; d = l;
2389: d = nd_reconstruct(1,d);
2390: goto again;
2391: } else if ( nf ) {
2392: if ( nd_demand ) {
2393: nfqv = ndv_load(nd_psn);
2394: nfq = ndvtond(0,nfqv);
2395: } else
2396: nfq = 0;
2397: if ( !nfq ) {
2398: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2399: NEXT(l) = d; d = l;
2400: d = nd_reconstruct(1,d);
2401: goto again;
2402: }
2403: }
2404: if ( nfq ) {
2405: /* m|HC(nfq) => failure */
2406: if ( nd_vc ) {
2407: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2408: } else
2409: q = HCZ(nfq);
2410: if ( !remqi((Q)q,m) ) return 0;
2411:
2412: if ( DP_Print ) { printf("+"); fflush(stdout); }
2413: hnfq = HCU(nfq);
2414: if ( nd_nalg ) {
2415: /* m|DN(HC(nf)^(-1)) => failure */
2416: get_eg(&egm0);
2417: if ( !nd_monic(m,&nfq) ) return 0;
2418: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2419: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2420: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2421: } else {
2422: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2423: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2424: }
2425: if ( nd_gentrace ) {
2426: /* exact division */
2427: cont = ndc_div(0,hnfq,HCU(nfqv));
2428: if ( !UNIQ(cont) ) {
2429: t = mknode(4,NULLP,NULLP,NULLP,cont);
2430: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2431: nd_tracelist = t;
2432: }
2433: }
2434: nh = ndv_newps(0,nfv,nfqv,0);
2435: if ( ishomo && ++diag_count == diag_period ) {
2436: diag_count = 0;
2437: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2438: stat = do_diagonalize_trace(sugar,m);
2439: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2440: if ( !stat ) {
1.1 noro 2441: NEXT(l) = d; d = l;
2442: d = nd_reconstruct(1,d);
2443: goto again;
1.6 noro 2444: }
1.1 noro 2445: }
1.6 noro 2446: d = update_pairs(d,g,nh,0);
2447: g = update_base(g,nh);
2448: } else {
2449: if ( DP_Print ) { printf("*"); fflush(stdout); }
2450: }
2451: } else {
2452: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2453: }
1.6 noro 2454: FREENDP(l);
2455: }
2456: if ( nd_nalg ) {
2457: if ( DP_Print ) {
2458: print_eg("monic",&eg_monic);
2459: print_eg("invdalg",&eg_invdalg);
2460: print_eg("le",&eg_le);
1.1 noro 2461: }
1.6 noro 2462: }
1.1 noro 2463: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2464: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2465: return g;
1.1 noro 2466: }
2467:
2468: int ndv_compare(NDV *p1,NDV *p2)
2469: {
2470: return DL_COMPARE(HDL(*p1),HDL(*p2));
2471: }
2472:
2473: int ndv_compare_rev(NDV *p1,NDV *p2)
2474: {
2475: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2476: }
2477:
2478: int ndvi_compare(NDVI p1,NDVI p2)
2479: {
2480: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2481: }
2482:
2483: int ndvi_compare_rev(NDVI p1,NDVI p2)
2484: {
2485: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2486: }
2487:
2488: NODE ndv_reduceall(int m,NODE f)
2489: {
2490: int i,j,n,stat;
2491: ND nf,g,head;
2492: NODE t,a0,a;
2493: union oNDC dn;
2494: Q q,num,den;
2495: NODE node;
2496: LIST l;
2497: Z iq,jq;
2498: int *perm;
2499: union oNDC hc;
2500: P cont,cont1;
2501:
2502: if ( nd_nora ) return f;
2503: n = length(f);
2504: ndv_setup(m,0,f,0,1);
2505: perm = (int *)MALLOC(n*sizeof(int));
2506: if ( nd_gentrace ) {
2507: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2508: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2509: }
2510: for ( i = 0; i < n; ) {
2511: if ( nd_gentrace ) {
2512: /* Trace = [1,index,1,1] */
1.6 noro 2513: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2514: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2515: }
2516: g = ndvtond(m,nd_ps[i]);
2517: g = nd_separate_head(g,&head);
1.6 noro 2518: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 2519: if ( !stat )
2520: nd_reconstruct(0,0);
2521: else {
2522: if ( DP_Print ) { printf("."); fflush(stdout); }
2523: ndv_free(nd_ps[i]);
2524: hc = HCU(nf); nd_removecont(m,nf);
2525: if ( nd_gentrace ) {
2526: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 2527: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
2528: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 2529: }
1.6 noro 2530: /* exact division */
1.1 noro 2531: cont = ndc_div(m,hc,HCU(nf));
2532: finalize_tracelist(perm[i],cont);
2533: }
2534: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2535: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2536: i++;
2537: }
2538: }
2539: if ( DP_Print ) { printf("\n"); }
2540: for ( a0 = 0, i = 0; i < n; i++ ) {
2541: NEXTNODE(a0,a);
2542: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2543: else {
2544: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2545: BDY(a) = (pointer)nd_ps[j];
2546: }
2547: }
2548: NEXT(a) = 0;
2549: return a0;
2550: }
2551:
2552: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
2553: {
2554: ND_pairs d1,nd,cur,head,prev,remove;
2555:
2556: if ( !g ) return d;
2557: /* for testing */
2558: if ( gensyz && nd_gensyz == 2 ) {
2559: d1 = nd_newpairs(g,t);
2560: if ( !d )
2561: return d1;
2562: else {
2563: nd = d;
2564: while ( NEXT(nd) ) nd = NEXT(nd);
2565: NEXT(nd) = d1;
2566: return d;
2567: }
2568: }
2569: d = crit_B(d,t);
2570: d1 = nd_newpairs(g,t);
2571: d1 = crit_M(d1);
2572: d1 = crit_F(d1);
2573: if ( gensyz || do_weyl )
2574: head = d1;
2575: else {
2576: prev = 0; cur = head = d1;
2577: while ( cur ) {
2578: if ( crit_2( cur->i1,cur->i2 ) ) {
2579: remove = cur;
2580: if ( !prev ) head = cur = NEXT(cur);
2581: else cur = NEXT(prev) = NEXT(cur);
2582: FREENDP(remove);
2583: } else {
2584: prev = cur; cur = NEXT(cur);
2585: }
2586: }
2587: }
2588: if ( !d )
2589: return head;
2590: else {
2591: nd = d;
2592: while ( NEXT(nd) ) nd = NEXT(nd);
2593: NEXT(nd) = head;
2594: return d;
2595: }
2596: }
2597:
2598:
2599: ND_pairs nd_newpairs( NODE g, int t )
2600: {
2601: NODE h;
2602: UINT *dl;
2603: int ts,s,i,t0,min,max;
2604: ND_pairs r,r0;
2605:
2606: dl = DL(nd_psh[t]);
2607: ts = SG(nd_psh[t]) - TD(dl);
2608: if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
2609: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
2610: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
2611: continue;
2612: if ( nd_gbblock ) {
2613: t0 = (long)BDY(h);
2614: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2615: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2616: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2617: break;
2618: }
2619: if ( nd_gbblock[i] >= 0 )
2620: continue;
2621: }
2622: NEXTND_pairs(r0,r);
2623: r->i1 = (long)BDY(h);
2624: r->i2 = t;
2625: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2626: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2627: SG(r) = MAX(s,ts) + TD(LCM(r));
2628: /* experimental */
2629: if ( nd_sugarweight )
2630: r->sugar2 = ndl_weight2(r->lcm);
2631: }
2632: if ( r0 ) NEXT(r) = 0;
2633: return r0;
2634: }
2635:
2636: /* ipair = [i1,i2],[i1,i2],... */
2637: ND_pairs nd_ipairtospair(NODE ipair)
2638: {
2639: int s1,s2;
2640: NODE tn,t;
2641: ND_pairs r,r0;
2642:
2643: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2644: NEXTND_pairs(r0,r);
2645: tn = BDY((LIST)BDY(t));
1.6 noro 2646: r->i1 = ZTOS((Q)ARG0(tn));
2647: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 2648: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2649: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2650: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2651: SG(r) = MAX(s1,s2) + TD(LCM(r));
2652: /* experimental */
2653: if ( nd_sugarweight )
2654: r->sugar2 = ndl_weight2(r->lcm);
2655: }
2656: if ( r0 ) NEXT(r) = 0;
2657: return r0;
2658: }
2659:
2660: /* kokokara */
2661:
2662: ND_pairs crit_B( ND_pairs d, int s )
2663: {
2664: ND_pairs cur,head,prev,remove;
2665: UINT *t,*tl,*lcm;
2666: int td,tdl;
2667:
2668: if ( !d ) return 0;
2669: t = DL(nd_psh[s]);
2670: prev = 0;
2671: head = cur = d;
2672: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
2673: while ( cur ) {
2674: tl = cur->lcm;
2675: if ( ndl_reducible(tl,t) ) {
2676: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
2677: if ( !ndl_equal(lcm,tl) ) {
2678: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2679: if (!ndl_equal(lcm,tl)) {
2680: remove = cur;
2681: if ( !prev ) {
2682: head = cur = NEXT(cur);
2683: } else {
2684: cur = NEXT(prev) = NEXT(cur);
2685: }
2686: FREENDP(remove);
2687: } else {
2688: prev = cur; cur = NEXT(cur);
2689: }
2690: } else {
2691: prev = cur; cur = NEXT(cur);
2692: }
2693: } else {
2694: prev = cur; cur = NEXT(cur);
2695: }
2696: }
2697: return head;
2698: }
2699:
2700: ND_pairs crit_M( ND_pairs d1 )
2701: {
2702: ND_pairs e,d2,d3,dd,p;
2703: UINT *id,*jd;
2704:
2705: if ( !d1 ) return d1;
2706: for ( dd = 0, e = d1; e; e = d3 ) {
2707: if ( !(d2 = NEXT(e)) ) {
2708: NEXT(e) = dd;
2709: return e;
2710: }
2711: id = LCM(e);
2712: for ( d3 = 0; d2; d2 = p ) {
2713: p = NEXT(d2);
2714: jd = LCM(d2);
2715: if ( ndl_equal(jd,id) )
2716: ;
2717: else if ( TD(jd) > TD(id) )
2718: if ( ndl_reducible(jd,id) ) continue;
2719: else ;
2720: else if ( ndl_reducible(id,jd) ) goto delit;
2721: NEXT(d2) = d3;
2722: d3 = d2;
2723: }
2724: NEXT(e) = dd;
2725: dd = e;
2726: continue;
2727: /**/
2728: delit: NEXT(d2) = d3;
2729: d3 = d2;
2730: for ( ; p; p = d2 ) {
2731: d2 = NEXT(p);
2732: NEXT(p) = d3;
2733: d3 = p;
2734: }
2735: FREENDP(e);
2736: }
2737: return dd;
2738: }
2739:
2740: ND_pairs crit_F( ND_pairs d1 )
2741: {
2742: ND_pairs rest, head,remove;
2743: ND_pairs last, p, r, w;
2744: int s;
2745:
2746: if ( !d1 ) return d1;
2747: for ( head = last = 0, p = d1; NEXT(p); ) {
2748: r = w = equivalent_pairs(p,&rest);
2749: s = SG(r);
2750: w = NEXT(w);
2751: while ( w ) {
2752: if ( crit_2(w->i1,w->i2) ) {
2753: r = w;
2754: w = NEXT(w);
2755: while ( w ) {
2756: remove = w;
2757: w = NEXT(w);
2758: FREENDP(remove);
2759: }
2760: break;
2761: } else if ( SG(w) < s ) {
2762: FREENDP(r);
2763: r = w;
2764: s = SG(r);
2765: w = NEXT(w);
2766: } else {
2767: remove = w;
2768: w = NEXT(w);
2769: FREENDP(remove);
2770: }
2771: }
2772: if ( last ) NEXT(last) = r;
2773: else head = r;
2774: NEXT(last = r) = 0;
2775: p = rest;
2776: if ( !p ) return head;
2777: }
2778: if ( !last ) return p;
2779: NEXT(last) = p;
2780: return head;
2781: }
2782:
2783: int crit_2( int dp1, int dp2 )
2784: {
2785: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
2786: }
2787:
2788: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
2789: {
2790: ND_pairs w,p,r,s;
2791: UINT *d;
2792:
2793: w = d1;
2794: d = LCM(w);
2795: s = NEXT(w);
2796: NEXT(w) = 0;
2797: for ( r = 0; s; s = p ) {
2798: p = NEXT(s);
2799: if ( ndl_equal(d,LCM(s)) ) {
2800: NEXT(s) = w; w = s;
2801: } else {
2802: NEXT(s) = r; r = s;
2803: }
2804: }
2805: *prest = r;
2806: return w;
2807: }
2808:
2809: NODE update_base(NODE nd,int ndp)
2810: {
2811: UINT *dl, *dln;
2812: NODE last, p, head;
2813:
2814: dl = DL(nd_psh[ndp]);
2815: for ( head = last = 0, p = nd; p; ) {
2816: dln = DL(nd_psh[(long)BDY(p)]);
2817: if ( ndl_reducible( dln, dl ) ) {
2818: p = NEXT(p);
2819: if ( last ) NEXT(last) = p;
2820: } else {
2821: if ( !last ) head = p;
2822: p = NEXT(last = p);
2823: }
2824: }
2825: head = append_one(head,ndp);
2826: return head;
2827: }
2828:
2829: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2830: {
2831: ND_pairs m,ml,p,l;
2832: UINT *lcm;
2833: int s,td,len,tlen,c,c1;
2834:
2835: if ( !(p = NEXT(m = d)) ) {
2836: *prest = p;
2837: NEXT(m) = 0;
2838: return m;
2839: }
2840: if ( !NoSugar ) {
2841: if ( nd_sugarweight ) {
2842: s = m->sugar2;
2843: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2844: if ( (p->sugar2 < s)
2845: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2846: ml = l; m = p; s = m->sugar2;
2847: }
2848: } else {
2849: s = SG(m);
2850: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2851: if ( (SG(p) < s)
2852: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2853: ml = l; m = p; s = SG(m);
2854: }
2855: }
2856: } else {
2857: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2858: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2859: ml = l; m = p; s = SG(m);
2860: }
2861: }
2862: if ( !ml ) *prest = NEXT(m);
2863: else {
2864: NEXT(ml) = NEXT(m);
2865: *prest = d;
2866: }
2867: NEXT(m) = 0;
2868: return m;
2869: }
2870:
2871: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2872: {
2873: int msugar,i;
2874: ND_pairs t,dm0,dm,dr0,dr;
2875:
2876: if ( nd_sugarweight ) {
2877: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2878: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2879: dm0 = 0; dr0 = 0;
2880: for ( i = 0, t = d; t; t = NEXT(t) )
2881: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2882: if ( dm0 ) NEXT(dm) = t;
2883: else dm0 = t;
2884: dm = t;
2885: i++;
2886: } else {
2887: if ( dr0 ) NEXT(dr) = t;
2888: else dr0 = t;
2889: dr = t;
2890: }
2891: } else {
2892: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2893: if ( SG(t) < msugar ) msugar = SG(t);
2894: dm0 = 0; dr0 = 0;
2895: for ( i = 0, t = d; t; t = NEXT(t) )
2896: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2897: if ( dm0 ) NEXT(dm) = t;
2898: else dm0 = t;
2899: dm = t;
2900: i++;
2901: } else {
2902: if ( dr0 ) NEXT(dr) = t;
2903: else dr0 = t;
2904: dr = t;
2905: }
2906: }
2907: NEXT(dm) = 0;
2908: if ( dr0 ) NEXT(dr) = 0;
2909: *prest = dr0;
2910: return dm0;
2911: }
2912:
2913: int nd_tdeg(NDV c)
2914: {
2915: int wmax = 0;
2916: int i,len;
2917: NMV a;
2918:
2919: len = LEN(c);
2920: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2921: wmax = MAX(TD(DL(a)),wmax);
2922: return wmax;
2923: }
2924:
2925: int ndv_newps(int m,NDV a,NDV aq,int f4)
2926: {
2927: int len;
2928: RHist r;
2929: NDV b;
2930: NODE tn;
2931: LIST l;
2932: Z iq;
2933:
2934: if ( nd_psn == nd_pslen ) {
2935: nd_pslen *= 2;
2936: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
2937: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2938: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2939: nd_bound = (UINT **)
2940: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
2941: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2942: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
2943: }
2944: NEWRHist(r); nd_psh[nd_psn] = r;
2945: nd_ps[nd_psn] = a;
2946: if ( aq ) {
2947: nd_ps_trace[nd_psn] = aq;
2948: if ( !m ) {
2949: register_hcf(aq);
2950: } else if ( m == -2 ) {
2951: /* do nothing */
2952: } else
2953: error("ndv_newps : invalud modulus");
2954: nd_bound[nd_psn] = ndv_compute_bound(aq);
2955: #if 1
2956: SG(r) = SG(aq);
2957: #else
2958: SG(r) = nd_tdeg(aq);
2959: #endif
2960: ndl_copy(HDL(aq),DL(r));
2961: } else {
2962: if ( !m ) register_hcf(a);
2963: nd_bound[nd_psn] = ndv_compute_bound(a);
2964: #if 1
2965: SG(r) = SG(a);
2966: #else
2967: SG(r) = nd_tdeg(a);
2968: #endif
2969: ndl_copy(HDL(a),DL(r));
2970: }
2971: if ( nd_demand ) {
2972: if ( aq ) {
2973: ndv_save(nd_ps_trace[nd_psn],nd_psn);
2974: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2975: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2976: nd_ps_trace[nd_psn] = 0;
2977: } else {
2978: ndv_save(nd_ps[nd_psn],nd_psn);
2979: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
2980: nd_ps[nd_psn] = 0;
2981: }
2982: }
2983: if ( nd_gentrace ) {
2984: /* reverse the tracelist and append it to alltracelist */
2985: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 2986: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 2987: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
2988: }
2989: return nd_psn++;
2990: }
2991:
2992: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
2993: /* return 1 if success, 0 if failure (HC(a mod p)) */
2994:
2995: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
2996: {
1.6 noro 2997: int i,j,td,len,max;
2998: NODE s,s0,f0,tn;
2999: UINT *d;
3000: RHist r;
3001: NDVI w;
3002: NDV a,am;
3003: union oNDC hc;
3004: NODE node;
3005: P hcp;
3006: Z iq,jq;
3007: LIST l;
3008:
3009: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3010: /* initialize the tracelist */
3011: nd_tracelist = 0;
3012:
3013: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3014: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3015: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3016: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3017: if ( !dont_sort ) {
3018: /* XXX heuristic */
3019: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
3020: qsort(w,nd_psn,sizeof(struct oNDVI),
3021: (int (*)(const void *,const void *))ndvi_compare_rev);
3022: else
3023: qsort(w,nd_psn,sizeof(struct oNDVI),
3024: (int (*)(const void *,const void *))ndvi_compare);
3025: }
3026: nd_pslen = 2*nd_psn;
3027: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3028: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3029: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3030: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3031: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3032: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3033: nd_hcf = 0;
1.1 noro 3034:
1.6 noro 3035: if ( trace && nd_vc )
3036: makesubst(nd_vc,&nd_subst);
3037: else
3038: nd_subst = 0;
1.1 noro 3039:
1.6 noro 3040: if ( !nd_red )
3041: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3042: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3043: for ( i = 0; i < nd_psn; i++ ) {
3044: hc = HCU(w[i].p);
3045: if ( trace ) {
3046: if ( mod == -2 ) {
3047: /* over a large finite field */
3048: /* trace = small modulus */
3049: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3050: ndv_mod(-2,a);
3051: if ( !dont_removecont) ndv_removecont(-2,a);
3052: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3053: ndv_mod(trace,am);
3054: if ( DL_COMPARE(HDL(am),HDL(a)) )
3055: return 0;
3056: ndv_removecont(trace,am);
3057: } else {
3058: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3059: if ( !dont_removecont) ndv_removecont(0,a);
3060: register_hcf(a);
3061: am = nd_ps[i] = ndv_dup(mod,a);
3062: ndv_mod(mod,am);
3063: if ( DL_COMPARE(HDL(am),HDL(a)) )
3064: return 0;
3065: ndv_removecont(mod,am);
3066: }
3067: } else {
3068: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3069: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3070: if ( !mod ) register_hcf(a);
1.1 noro 3071: }
1.6 noro 3072: if ( nd_gentrace ) {
3073: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3074: /* exact division */
1.1 noro 3075: if ( !dont_removecont )
1.6 noro 3076: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3077: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3078: }
3079: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3080: nd_bound[i] = ndv_compute_bound(a);
3081: nd_psh[i] = r;
3082: if ( nd_demand ) {
3083: if ( trace ) {
3084: ndv_save(nd_ps_trace[i],i);
3085: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3086: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3087: nd_ps_trace[i] = 0;
3088: } else {
3089: ndv_save(nd_ps[i],i);
3090: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3091: nd_ps[i] = 0;
3092: }
1.1 noro 3093: }
1.6 noro 3094: }
3095: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3096: return 1;
1.1 noro 3097: }
3098:
3099: struct order_spec *append_block(struct order_spec *spec,
3100: int nv,int nalg,int ord);
3101:
3102: extern VECT current_dl_weight_vector_obj;
3103: static VECT prev_weight_vector_obj;
3104:
3105: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3106: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3107: {
3108: NODE alist,t,s,r0,r,arg;
3109: VL tv;
3110: P poly;
3111: DP d;
3112: Alg alpha,dp;
3113: DAlg inv,da,hc;
3114: MP m;
3115: int i,nvar,nalg,n;
3116: NumberField nf;
3117: LIST f1,f2;
3118: struct order_spec *current_spec;
3119: VECT obj,obj0;
3120: VECT tmp;
3121:
3122: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3123: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3124:
3125: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3126: NEXTNODE(alist,t); MKV(tv->v,poly);
3127: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3128: tv->v = tv->v->priv;
3129: }
3130: NEXT(t) = 0;
3131:
3132: /* simplification, making polynomials monic */
3133: setfield_dalg(alist);
3134: obj_algtodalg((Obj)f,(Obj *)&f1);
3135: for ( t = BDY(f); t; t = NEXT(t) ) {
3136: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3137: hc = (DAlg)BDY(d)->c;
3138: if ( NID(hc) == N_DA ) {
3139: invdalg(hc,&inv);
3140: for ( m = BDY(d); m; m = NEXT(m) ) {
3141: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3142: }
3143: }
3144: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3145: }
3146: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3147:
3148: /* append alg vars to the var list */
3149: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3150: NEXT(tv) = av;
3151:
3152: /* append a block to ord */
3153: *ord1p = append_block(ord,nvar,nalg,2);
3154:
3155: /* create generator list */
3156: nf = get_numberfield();
3157: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3158: MKAlg(nf->defpoly[i],dp);
3159: MKNODE(s,dp,t); t = s;
3160: }
3161: MKLIST(f1,t);
3162: *alistp = alist;
3163: algobjtorat((Obj)f1,(Obj *)f1p);
3164:
3165: /* creating a new weight vector */
3166: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3167: n = nvar+nalg+1;
3168: MKVECT(obj,n);
3169: if ( obj0 && obj0->len == nvar )
3170: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3171: else
3172: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3173: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3174: BDY(obj)[n-1] = (pointer)ONE;
3175: arg = mknode(1,obj);
3176: Pdp_set_weight(arg,&tmp);
3177: }
3178:
3179: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3180: {
3181: NODE s,t,u0,u;
3182: P p;
3183: VL tv;
3184: Obj obj;
3185: VECT tmp;
3186: NODE arg;
3187:
3188: u0 = 0;
3189: for ( t = r; t; t = NEXT(t) ) {
3190: p = (P)BDY(t);
3191: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3192: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3193: }
3194: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3195: NEXTNODE(u0,u);
3196: BDY(u) = (pointer)p;
3197: }
3198: }
3199: arg = mknode(1,prev_weight_vector_obj);
3200: Pdp_set_weight(arg,&tmp);
3201:
3202: return u0;
3203: }
3204:
3205: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3206: {
3207: VL tv,fv,vv,vc,av;
3208: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3209: int e,max,nvar,i;
3210: NDV b;
3211: int ishomo,nalg,mrank,trank,wmax,len;
3212: NMV a;
3213: Alg alpha,dp;
3214: P p,zp;
3215: Q dmy;
3216: LIST f1,f2,zpl;
3217: Obj obj;
3218: NumberField nf;
3219: struct order_spec *ord1;
3220: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3221: LIST l1,l2,l3,l4,l5;
3222: int j;
3223: Z jq,bpe,last_nonzero;
3224: int *perm;
3225: EPOS oepos;
3226: int obpe,oadv,ompos,cbpe;
3227:
3228: nd_module = 0;
3229: if ( !m && Demand ) nd_demand = 1;
3230: else nd_demand = 0;
3231: parse_nd_option(current_option);
3232:
3233: if ( DP_Multiple )
3234: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3235: #if 0
3236: ndv_alloc = 0;
3237: #endif
3238: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3239: if ( m && nd_vc )
3240: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3241: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3242: switch ( ord->id ) {
3243: case 1:
3244: if ( ord->nv != nvar )
3245: error("nd_{gr,f4} : invalid order specification");
3246: break;
3247: default:
3248: break;
3249: }
3250: nd_nalg = 0;
3251: av = 0;
3252: if ( !m ) {
3253: get_algtree((Obj)f,&av);
3254: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3255: nd_ntrans = nvar;
3256: nd_nalg = nalg;
3257: /* #i -> t#i */
3258: if ( nalg ) {
3259: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3260: ord = ord1;
3261: f = f1;
3262: }
3263: nvar += nalg;
3264: }
3265: nd_init_ord(ord);
3266: mrank = 0;
3267: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3268: for ( tv = vv; tv; tv = NEXT(tv) ) {
3269: if ( nd_module ) {
3270: s = BDY((LIST)BDY(t));
3271: trank = length(s);
3272: mrank = MAX(mrank,trank);
3273: for ( ; s; s = NEXT(s) ) {
3274: e = getdeg(tv->v,(P)BDY(s));
3275: max = MAX(e,max);
3276: }
3277: } else {
3278: e = getdeg(tv->v,(P)BDY(t));
3279: max = MAX(e,max);
3280: }
3281: }
3282: nd_setup_parameters(nvar,nd_nzlist?0:max);
3283: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3284: ishomo = 1;
3285: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3286: if ( nd_module ) {
3287: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3288: else zpl = (LIST)BDY(t);
3289: b = (pointer)pltondv(CO,vv,zpl);
3290: } else {
3291: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3292: else zp = (P)BDY(t);
3293: b = (pointer)ptondv(CO,vv,zp);
3294: }
3295: if ( ishomo )
3296: ishomo = ishomo && ndv_ishomo(b);
3297: if ( m ) ndv_mod(m,b);
3298: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3299: }
3300: if ( fd0 ) NEXT(fd) = 0;
3301:
3302: if ( !ishomo && homo ) {
3303: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3304: b = (NDV)BDY(t); len = LEN(b);
3305: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3306: wmax = MAX(TD(DL(a)),wmax);
3307: }
3308: homogenize_order(ord,nvar,&ord1);
3309: nd_init_ord(ord1);
3310: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
3311: for ( t = fd0; t; t = NEXT(t) )
3312: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3313: }
3314:
3315: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
3316: if ( nd_gentrace ) {
3317: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3318: }
3319: if ( nd_splist ) {
3320: *rp = compute_splist();
3321: return;
3322: }
3323: if ( nd_check_splist ) {
3324: if ( f4 ) {
3325: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3326: else *rp = 0;
3327: } else {
3328: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3329: else *rp = 0;
3330: }
3331: return;
3332: }
3333: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
3334: if ( !x ) {
3335: *rp = 0; return;
3336: }
3337: if ( !ishomo && homo ) {
3338: /* dehomogenization */
3339: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3340: nd_init_ord(ord);
3341: nd_setup_parameters(nvar,0);
3342: }
3343: nd_demand = 0;
3344: if ( nd_module && nd_intersect ) {
3345: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
3346: if ( MPOS(DL(nd_psh[j])) > 1 ) {
3347: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
3348: }
3349: conv_ilist(nd_demand,0,x,0);
3350: goto FINAL;
3351: }
3352: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
3353: x = ndv_reducebase(x,perm);
3354: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3355: x = ndv_reduceall(m,x);
3356: cbpe = nd_bpe;
3357: if ( nd_gentrace && !f4 ) {
3358: tl2 = nd_alltracelist; nd_alltracelist = 0;
3359: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
3360: tl3 = nd_alltracelist; nd_alltracelist = 0;
3361: if ( nd_gensyz ) {
3362: nd_gb(m,0,1,1,0);
3363: tl4 = nd_alltracelist; nd_alltracelist = 0;
3364: } else tl4 = 0;
3365: }
3366: nd_bpe = cbpe;
3367: nd_setup_parameters(nd_nvar,0);
3368: FINAL:
3369: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3370: NEXTNODE(r0,r);
3371: if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
3372: else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
3373: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
3374: }
3375: if ( r0 ) NEXT(r) = 0;
3376: if ( !m && nd_nalg )
3377: r0 = postprocess_algcoef(av,alist,r0);
3378: MKLIST(*rp,r0);
3379: if ( nd_gentrace ) {
3380: if ( f4 ) {
1.6 noro 3381: STOZ(16,bpe);
3382: STOZ(nd_last_nonzero,last_nonzero);
1.1 noro 3383: tr = mknode(5,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero); MKLIST(*rp,tr);
3384:
3385: } else {
3386: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3387: tl3 = reverse_node(tl3);
3388: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3389: for ( t = tl2; t; t = NEXT(t) ) {
3390: /* s = [i,[*,j,*,*],...] */
3391: s = BDY((LIST)BDY(t));
1.6 noro 3392: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3393: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3394: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3395: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3396: }
3397: }
3398: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3399: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3400: }
3401: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3402: MKLIST(l5,tl4);
1.6 noro 3403: STOZ(nd_bpe,bpe);
1.1 noro 3404: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
3405: }
3406: }
3407: #if 0
3408: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
3409: #endif
3410: }
3411:
3412: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3413: {
3414: VL tv,fv,vv,vc,av;
3415: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3416: int e,max,nvar,i;
3417: NDV b;
3418: int ishomo,nalg;
3419: Alg alpha,dp;
3420: P p,zp;
3421: Q dmy;
3422: LIST f1,f2;
3423: Obj obj;
3424: NumberField nf;
3425: struct order_spec *ord1;
3426: int *perm;
3427:
3428: parse_nd_option(current_option);
3429: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3430: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3431: switch ( ord->id ) {
3432: case 1:
3433: if ( ord->nv != nvar )
3434: error("nd_check : invalid order specification");
3435: break;
3436: default:
3437: break;
3438: }
3439: nd_nalg = 0;
3440: av = 0;
3441: if ( !m ) {
3442: get_algtree((Obj)f,&av);
3443: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3444: nd_ntrans = nvar;
3445: nd_nalg = nalg;
3446: /* #i -> t#i */
3447: if ( nalg ) {
3448: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3449: ord = ord1;
3450: f = f1;
3451: }
3452: nvar += nalg;
3453: }
3454: nd_init_ord(ord);
3455: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3456: for ( tv = vv; tv; tv = NEXT(tv) ) {
3457: e = getdeg(tv->v,(P)BDY(t));
3458: max = MAX(e,max);
3459: }
3460: nd_setup_parameters(nvar,max);
3461: ishomo = 1;
3462: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3463: ptozp((P)BDY(t),1,&dmy,&zp);
3464: b = (pointer)ptondv(CO,vv,zp);
3465: if ( ishomo )
3466: ishomo = ishomo && ndv_ishomo(b);
3467: if ( m ) ndv_mod(m,b);
3468: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3469: }
3470: if ( fd0 ) NEXT(fd) = 0;
3471: ndv_setup(m,0,fd0,0,1);
3472: for ( x = 0, i = 0; i < nd_psn; i++ )
3473: x = update_base(x,i);
3474: if ( do_check ) {
3475: x = nd_gb(m,ishomo,1,0,&perm);
3476: if ( !x ) {
3477: *rp = 0;
3478: return;
3479: }
3480: } else {
3481: #if 0
3482: /* bug ? */
3483: for ( t = x; t; t = NEXT(t) )
3484: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
3485: #else
3486: conv_ilist(0,0,x,&perm);
3487: #endif
3488: }
3489: x = ndv_reducebase(x,perm);
3490: x = ndv_reduceall(m,x);
3491: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3492: NEXTNODE(r0,r);
3493: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3494: }
3495: if ( r0 ) NEXT(r) = 0;
3496: if ( !m && nd_nalg )
3497: r0 = postprocess_algcoef(av,alist,r0);
3498: MKLIST(*rp,r0);
3499: }
3500:
3501: NDV recompute_trace(NODE trace,NDV *p,int m);
3502: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3503:
3504: NDV recompute_trace(NODE ti,NDV *p,int mod)
3505: {
3506: int c,c1,c2,i;
3507: NM mul,m,tail;
3508: ND d,r,rm;
3509: NODE sj;
3510: NDV red;
3511: Obj mj;
3512:
3513: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3514: CM(mul) = 1;
3515: tail = 0;
3516: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3517: sj = BDY((LIST)BDY(ti));
3518: if ( ARG0(sj) ) {
1.6 noro 3519: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 3520: mj = (Obj)ARG2(sj);
3521: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3522: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3523: rm = ndv_mul_nm(mod,mul,red);
3524: if ( !r ) r = rm;
3525: else {
3526: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3527: if ( d ) {
3528: NEXT(tail) = m; tail = m; LEN(d)++;
3529: } else {
3530: MKND(nd_nvar,m,1,d); tail = BDY(d);
3531: }
3532: }
3533: if ( !m ) return 0; /* failure */
3534: else {
3535: BDY(r) = m;
3536: if ( mod > 0 || mod == -1 ) {
3537: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3538: DMAR(c1,c2,0,mod,c);
3539: nd_mul_c(mod,rm,c);
3540: } else {
3541: Z t,u;
3542:
3543: chsgnlf(HCZ(r),&t);
3544: divlf(t,HCZ(rm),&u);
3545: nd_mul_c_lf(rm,u);
3546: }
3547: r = nd_add(mod,r,rm);
3548: }
3549: }
3550: }
3551: }
3552: if ( tail ) NEXT(tail) = 0;
3553: d = nd_add(mod,d,r);
3554: nd_mul_c(mod,d,invm(HCM(d),mod));
3555: return ndtondv(mod,d);
3556: }
3557:
3558: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3559: {
3560: VL tv,fv,vv,vc,av;
3561: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3562: int e,max,nvar,i;
3563: NDV b;
3564: int ishomo,nalg;
3565: Alg alpha,dp;
3566: P p,zp;
3567: Q dmy;
3568: LIST f1,f2;
3569: Obj obj;
3570: NumberField nf;
3571: struct order_spec *ord1;
3572: NODE permtrace,intred,ind,perm,trace,ti;
3573: int len,n,j;
3574: NDV *db,*pb;
3575:
3576: parse_nd_option(current_option);
3577: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3578: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3579: switch ( ord->id ) {
3580: case 1:
3581: if ( ord->nv != nvar )
3582: error("nd_check : invalid order specification");
3583: break;
3584: default:
3585: break;
3586: }
3587: nd_init_ord(ord);
1.6 noro 3588: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 3589: nd_setup_parameters(nvar,0);
3590:
3591: len = length(BDY(f));
3592: db = (NDV *)MALLOC(len*sizeof(NDV *));
3593: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3594: ptozp((P)BDY(t),1,&dmy,&zp);
3595: b = ptondv(CO,vv,zp);
3596: ndv_mod(m,b);
3597: ndv_mul_c(m,b,invm(HCM(b),m));
3598: db[i] = b;
3599: }
3600:
3601: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3602: intred = BDY((LIST)ARG3(BDY(tlist)));
3603: ind = BDY((LIST)ARG4(BDY(tlist)));
3604: perm = BDY((LIST)ARG0(permtrace));
3605: trace = NEXT(permtrace);
3606:
3607: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 3608: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 3609: if ( j > i ) i = j;
3610: }
3611: n = i+1;
3612: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3613: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3614: ti = BDY((LIST)BDY(t));
1.6 noro 3615: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 3616: }
3617: for ( t = trace; t; t = NEXT(t) ) {
3618: ti = BDY((LIST)BDY(t));
1.6 noro 3619: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3620: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3621: if ( DP_Print ) {
3622: fprintf(asir_out,"."); fflush(asir_out);
3623: }
3624: }
3625: for ( t = intred; t; t = NEXT(t) ) {
3626: ti = BDY((LIST)BDY(t));
1.6 noro 3627: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3628: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3629: if ( DP_Print ) {
3630: fprintf(asir_out,"*"); fflush(asir_out);
3631: }
3632: }
3633: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3634: NEXTNODE(r0,r);
1.6 noro 3635: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 3636: ndv_mul_c(m,b,invm(HCM(b),m));
3637: #if 0
1.6 noro 3638: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3639: #else
1.6 noro 3640: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3641: #endif
3642: }
3643: if ( r0 ) NEXT(r) = 0;
3644: MKLIST(*rp,r0);
3645: if ( DP_Print ) fprintf(asir_out,"\n");
3646: }
3647:
3648: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
3649: {
3650: VL tv,fv,vv,vc,av;
3651: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3652: int m,nocheck,nvar,mindex,e,max;
3653: NDV c;
3654: NMV a;
3655: P p,zp;
3656: Q dmy;
3657: EPOS oepos;
3658: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
3659: Alg alpha,dp;
3660: P poly;
3661: LIST f1,f2,zpl;
3662: Obj obj;
3663: NumberField nf;
3664: struct order_spec *ord1;
3665: struct oEGT eg_check,eg0,eg1;
3666: NODE tr,tl1,tl2,tl3,tl4;
3667: LIST l1,l2,l3,l4,l5;
3668: int *perm;
3669: int j,ret;
3670: Z jq,bpe;
3671:
3672: nd_module = 0;
3673: nd_lf = 0;
3674: parse_nd_option(current_option);
3675: if ( nd_lf ) {
3676: if ( f4 )
3677: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3678: else
3679: error("nd_gr_trace is not implemented yet over a large finite field");
3680: return;
3681: }
3682: if ( DP_Multiple )
3683: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3684:
3685: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3686: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3687: switch ( ord->id ) {
3688: case 1:
3689: if ( ord->nv != nvar )
3690: error("nd_gr_trace : invalid order specification");
3691: break;
3692: default:
3693: break;
3694: }
3695:
3696: get_algtree((Obj)f,&av);
3697: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3698: nd_ntrans = nvar;
3699: nd_nalg = nalg;
3700: /* #i -> t#i */
3701: if ( nalg ) {
3702: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3703: ord = ord1;
3704: f = f1;
3705: }
3706: nvar += nalg;
3707:
3708: nocheck = 0;
3709: mindex = 0;
3710:
3711: if ( Demand ) nd_demand = 1;
3712: else nd_demand = 0;
3713:
3714: /* setup modulus */
3715: if ( trace < 0 ) {
3716: trace = -trace;
3717: nocheck = 1;
3718: }
3719: m = trace > 1 ? trace : get_lprime(mindex);
3720: nd_init_ord(ord);
3721: mrank = 0;
3722: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3723: for ( tv = vv; tv; tv = NEXT(tv) ) {
3724: if ( nd_module ) {
3725: s = BDY((LIST)BDY(t));
3726: trank = length(s);
3727: mrank = MAX(mrank,trank);
3728: for ( ; s; s = NEXT(s) ) {
3729: e = getdeg(tv->v,(P)BDY(s));
3730: max = MAX(e,max);
3731: }
3732: } else {
3733: e = getdeg(tv->v,(P)BDY(t));
3734: max = MAX(e,max);
3735: }
3736: }
3737: nd_setup_parameters(nvar,max);
3738: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3739: ishomo = 1;
3740: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3741: if ( nd_module ) {
3742: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3743: else zpl = (LIST)BDY(t);
3744: c = (pointer)pltondv(CO,vv,zpl);
3745: } else {
3746: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3747: else zp = (P)BDY(t);
3748: c = (pointer)ptondv(CO,vv,zp);
3749: }
3750: if ( ishomo )
3751: ishomo = ishomo && ndv_ishomo(c);
3752: if ( c ) {
3753: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3754: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3755: }
3756: }
3757: if ( in0 ) NEXT(in) = 0;
3758: if ( fd0 ) NEXT(fd) = 0;
3759: if ( !ishomo && homo ) {
3760: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3761: c = (NDV)BDY(t); len = LEN(c);
3762: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3763: wmax = MAX(TD(DL(a)),wmax);
3764: }
3765: homogenize_order(ord,nvar,&ord1);
3766: nd_init_ord(ord1);
3767: nd_setup_parameters(nvar+1,wmax);
3768: for ( t = fd0; t; t = NEXT(t) )
3769: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3770: }
3771: if ( MaxDeg > 0 ) nocheck = 1;
3772: while ( 1 ) {
3773: tl1 = tl2 = tl3 = tl4 = 0;
3774: if ( Demand )
3775: nd_demand = 1;
3776: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
3777: if ( nd_gentrace ) {
3778: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3779: }
3780: if ( ret )
3781: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3782: if ( !ret || !cand ) {
3783: /* failure */
3784: if ( trace > 1 ) { *rp = 0; return; }
3785: else m = get_lprime(++mindex);
3786: continue;
3787: }
3788: if ( !ishomo && homo ) {
3789: /* dehomogenization */
3790: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3791: nd_init_ord(ord);
3792: nd_setup_parameters(nvar,0);
3793: }
3794: nd_demand = 0;
3795: cand = ndv_reducebase(cand,perm);
3796: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3797: cand = ndv_reduceall(0,cand);
3798: cbpe = nd_bpe;
3799: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
3800: get_eg(&eg0);
3801: if ( nocheck )
3802: break;
3803: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
3804: if ( nd_gentrace ) {
3805: tl3 = nd_alltracelist; nd_alltracelist = 0;
3806: } else tl3 = 0;
3807: /* gbcheck : cand is a GB of Id(cand) ? */
3808: if ( nd_vc || nd_gentrace || nd_gensyz )
3809: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3810: else
3811: ret = nd_f4(0,1,0)!=0;
3812: if ( nd_gentrace && nd_gensyz ) {
3813: tl4 = nd_alltracelist; nd_alltracelist = 0;
3814: } else tl4 = 0;
3815: }
3816: if ( ret ) break;
3817: else if ( trace > 1 ) {
3818: /* failure */
3819: *rp = 0; return;
3820: } else {
3821: /* try the next modulus */
3822: m = get_lprime(++mindex);
3823: /* reset the parameters */
3824: if ( !ishomo && homo ) {
3825: nd_init_ord(ord1);
3826: nd_setup_parameters(nvar+1,wmax);
3827: } else {
3828: nd_init_ord(ord);
3829: nd_setup_parameters(nvar,max);
3830: }
3831: }
3832: }
3833: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3834: if ( DP_Print )
1.6 noro 3835: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 3836: /* dp->p */
3837: nd_bpe = cbpe;
3838: nd_setup_parameters(nd_nvar,0);
3839: for ( r = cand; r; r = NEXT(r) ) {
3840: if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
3841: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
3842: }
3843: if ( nd_nalg )
3844: cand = postprocess_algcoef(av,alist,cand);
3845: MKLIST(*rp,cand);
3846: if ( nd_gentrace ) {
3847: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3848: tl3 = reverse_node(tl3);
3849: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3850: for ( t = tl2; t; t = NEXT(t) ) {
3851: /* s = [i,[*,j,*,*],...] */
3852: s = BDY((LIST)BDY(t));
1.6 noro 3853: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3854: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3855: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3856: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3857: }
3858: }
3859: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3860: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3861: }
3862: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3863: MKLIST(l5,tl4);
1.6 noro 3864: STOZ(nd_bpe,bpe);
1.1 noro 3865: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
3866: }
3867: }
3868:
3869: /* XXX : module element is not considered */
3870:
3871: void dltondl(int n,DL dl,UINT *r)
3872: {
3873: UINT *d;
3874: int i,j,l,s,ord_l;
3875: struct order_pair *op;
3876:
3877: d = (unsigned int *)dl->d;
3878: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3879: if ( nd_blockmask ) {
3880: l = nd_blockmask->n;
3881: op = nd_blockmask->order_pair;
3882: for ( j = 0, s = 0; j < l; j++ ) {
3883: ord_l = op[j].length;
3884: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3885: }
3886: TD(r) = ndl_weight(r);
3887: ndl_weight_mask(r);
3888: } else {
3889: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3890: TD(r) = ndl_weight(r);
3891: }
3892: }
3893:
3894: DL ndltodl(int n,UINT *ndl)
3895: {
3896: DL dl;
3897: int *d;
3898: int i,j,l,s,ord_l;
3899: struct order_pair *op;
3900:
3901: NEWDL(dl,n);
3902: dl->td = TD(ndl);
3903: d = dl->d;
3904: if ( nd_blockmask ) {
3905: l = nd_blockmask->n;
3906: op = nd_blockmask->order_pair;
3907: for ( j = 0, s = 0; j < l; j++ ) {
3908: ord_l = op[j].length;
3909: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3910: }
3911: } else {
3912: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3913: }
3914: return dl;
3915: }
3916:
3917: void nmtodp(int mod,NM m,DP *r)
3918: {
3919: DP dp;
3920: MP mr;
3921:
3922: NEWMP(mr);
3923: mr->dl = ndltodl(nd_nvar,DL(m));
3924: mr->c = (Obj)ndctop(mod,m->c);
3925: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3926: *r = dp;
3927: }
3928:
3929: void ndl_print(UINT *dl)
3930: {
3931: int n;
3932: int i,j,l,ord_l,s,s0;
3933: struct order_pair *op;
3934:
3935: n = nd_nvar;
3936: printf("<<");
3937: if ( nd_blockmask ) {
3938: l = nd_blockmask->n;
3939: op = nd_blockmask->order_pair;
3940: for ( j = 0, s = s0 = 0; j < l; j++ ) {
3941: ord_l = op[j].length;
3942: for ( i = 0; i < ord_l; i++, s++ )
3943: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
3944: }
3945: } else {
3946: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
3947: }
3948: printf(">>");
3949: if ( nd_module && MPOS(dl) )
3950: printf("*e%d",MPOS(dl));
3951: }
3952:
3953: void nd_print(ND p)
3954: {
3955: NM m;
3956:
3957: if ( !p )
3958: printf("0\n");
3959: else {
3960: for ( m = BDY(p); m; m = NEXT(m) ) {
3961: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3962: else printf("+%d*",CM(m));
3963: ndl_print(DL(m));
3964: }
3965: printf("\n");
3966: }
3967: }
3968:
3969: void nd_print_q(ND p)
3970: {
3971: NM m;
3972:
3973: if ( !p )
3974: printf("0\n");
3975: else {
3976: for ( m = BDY(p); m; m = NEXT(m) ) {
3977: printf("+");
1.6 noro 3978: printexpr(CO,(Obj)CZ(m));
1.1 noro 3979: printf("*");
3980: ndl_print(DL(m));
3981: }
3982: printf("\n");
3983: }
3984: }
3985:
3986: void ndp_print(ND_pairs d)
3987: {
3988: ND_pairs t;
3989:
3990: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
3991: printf("\n");
3992: }
3993:
3994: void nd_removecont(int mod,ND p)
3995: {
3996: int i,n;
3997: Z *w;
3998: NM m;
3999: struct oVECT v;
4000:
4001: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4002: else if ( mod == -2 ) {
4003: Z inv;
4004: divlf(ONE,HCZ(p),&inv);
4005: nd_mul_c_lf(p,inv);
4006: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4007: else {
4008: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4009: w = (Z *)MALLOC(n*sizeof(Q));
4010: v.len = n;
4011: v.body = (pointer *)w;
1.6 noro 4012: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4013: removecont_array((P *)w,n,1);
1.6 noro 4014: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4015: }
4016: }
4017:
4018: void nd_removecont2(ND p1,ND p2)
4019: {
4020: int i,n1,n2,n;
4021: Z *w;
4022: NM m;
4023: struct oVECT v;
4024:
4025: n1 = nd_length(p1);
4026: n2 = nd_length(p2);
4027: n = n1+n2;
4028: w = (Z *)MALLOC(n*sizeof(Q));
4029: v.len = n;
4030: v.body = (pointer *)w;
4031: i = 0;
4032: if ( p1 )
1.6 noro 4033: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4034: if ( p2 )
1.6 noro 4035: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4036: removecont_array((P *)w,n,1);
4037: i = 0;
4038: if ( p1 )
1.6 noro 4039: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4040: if ( p2 )
1.6 noro 4041: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4042: }
4043:
4044: void ndv_removecont(int mod,NDV p)
4045: {
4046: int i,len,all_p;
4047: Z *c;
4048: P *w;
4049: Z dvr,t;
4050: P g,cont,tp;
4051: NMV m;
4052:
4053: if ( mod == -1 )
4054: ndv_mul_c(mod,p,_invsf(HCM(p)));
4055: else if ( mod == -2 ) {
4056: Z inv;
4057: divlf(ONE,HCZ(p),&inv);
4058: ndv_mul_c_lf(p,inv);
4059: } else if ( mod )
4060: ndv_mul_c(mod,p,invm(HCM(p),mod));
4061: else {
4062: len = p->len;
4063: w = (P *)MALLOC(len*sizeof(P));
4064: c = (Z *)MALLOC(len*sizeof(Q));
4065: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4066: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4067: all_p = all_p && !NUM(w[i]);
4068: }
4069: if ( all_p ) {
4070: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4071: mulp(nd_vc,(P)dvr,g,&cont);
4072: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4073: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4074: }
4075: } else {
4076: sortbynm((Q *)c,len);
4077: qltozl((Q *)c,len,&dvr);
4078: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4079: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4080: }
4081: }
4082: }
4083: }
4084:
4085: /* koko */
4086:
4087: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4088: {
4089: int len,i,max;
4090: NMV m,mr0,mr,t;
4091:
4092: len = p->len;
4093: for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
4094: max = MAX(max,TD(DL(m)));
4095: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4096: m = (NMV)((char *)mr0+(len-1)*oadv);
4097: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4098: t = (NMV)MALLOC(nmv_adv);
4099: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4100: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4101: CZ(mr) = CZ(m);
1.1 noro 4102: ndl_copy(DL(t),DL(mr));
4103: }
4104: NV(p)++;
4105: BDY(p) = mr0;
4106: }
4107:
4108: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4109: {
4110: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4111: int pos;
4112: Q *w;
4113: Q dvr,t;
4114: NMV m,r;
4115:
4116: len = p->len;
4117: newnvar = nd_nvar-1;
4118: newexporigin = nd_get_exporigin(ord);
4119: if ( nd_module ) newmpos = newexporigin-1;
4120: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4121: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4122: ndl_dehomogenize(DL(m));
4123: if ( newwpd != nd_wpd ) {
4124: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4125: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 4126: CZ(r) = CZ(m);
1.1 noro 4127: if ( nd_module ) pos = MPOS(DL(m));
4128: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4129: adj = nd_exporigin-newexporigin;
4130: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
4131: if ( nd_module ) {
4132: DL(r)[newmpos] = pos;
4133: }
4134: }
4135: }
4136: NV(p)--;
4137: }
4138:
4139: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4140: {
4141: int i;
4142: P *tpl,*tpl1;
4143: NODE l;
4144: P h,gcd,t;
4145:
4146: tpl = (P *)MALLOC(m*sizeof(P));
4147: tpl1 = (P *)MALLOC(m*sizeof(P));
4148: bcopy(pl,tpl,m*sizeof(P));
4149: gcd = (P)ONE;
4150: for ( l = nd_hcf; l; l = NEXT(l) ) {
4151: h = (P)BDY(l);
4152: while ( 1 ) {
4153: for ( i = 0; i < m; i++ )
4154: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4155: break;
4156: if ( i == m ) {
4157: bcopy(tpl1,tpl,m*sizeof(P));
4158: mulp(vl,gcd,h,&t); gcd = t;
4159: } else
4160: break;
4161: }
4162: }
4163: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4164: if ( full ) {
4165: heu_nezgcdnpz(vl,tpl,m,&t);
4166: mulp(vl,gcd,t,pr);
4167: } else
4168: *pr = gcd;
4169: }
4170:
4171: void removecont_array(P *p,int n,int full)
4172: {
4173: int all_p,all_q,i;
4174: Z *c;
4175: P *w;
4176: P t,s;
4177:
4178: for ( all_q = 1, i = 0; i < n; i++ )
4179: all_q = all_q && NUM(p[i]);
4180: if ( all_q ) {
4181: removecont_array_q((Z *)p,n);
4182: } else {
4183: c = (Z *)MALLOC(n*sizeof(Z));
4184: w = (P *)MALLOC(n*sizeof(P));
4185: for ( i = 0; i < n; i++ ) {
4186: ptozp(p[i],1,(Q *)&c[i],&w[i]);
4187: }
4188: removecont_array_q(c,n);
4189: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4190: for ( i = 0; i < n; i++ ) {
4191: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4192: }
4193: }
4194: }
4195:
4196: /* c is an int array */
4197:
4198: void removecont_array_q(Z *c,int n)
4199: {
4200: struct oVECT v;
4201: Z d0,d1,a,u,u1,gcd;
4202: int i,j;
4203: Z *q,*r;
4204:
4205: q = (Z *)MALLOC(n*sizeof(Z));
4206: r = (Z *)MALLOC(n*sizeof(Z));
4207: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4208: gcdvz_estimate(&v,&d0);
4209: for ( i = 0; i < n; i++ ) {
4210: divqrz(c[i],d0,&q[i],&r[i]);
4211: }
4212: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4213: if ( i < n ) {
4214: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4215: gcdvz(&v,&d1);
4216: gcdz(d0,d1,&gcd);
1.6 noro 4217: /* exact division */
4218: divsz(d0,gcd,&a);
1.1 noro 4219: for ( i = 0; i < n; i++ ) {
4220: mulz(a,q[i],&u);
4221: if ( r[i] ) {
1.6 noro 4222: /* exact division */
4223: divsz(r[i],gcd,&u1);
1.1 noro 4224: addz(u,u1,&q[i]);
4225: } else
4226: q[i] = u;
4227: }
4228: }
4229: for ( i = 0; i < n; i++ ) c[i] = q[i];
4230: }
4231:
1.4 noro 4232: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
4233:
4234: void mpz_removecont_array(mpz_t *c,int n)
4235: {
4236: mpz_t d0,a,u,u1,gcd;
4237: int i,j;
1.13 ! noro 4238: static mpz_t *q,*r;
! 4239: static int c_len = 0;
1.4 noro 4240:
4241: for ( i = 0; i < n; i++ )
4242: if ( mpz_sgn(c[i]) ) break;
4243: if ( i == n ) return;
4244: gcdv_mpz_estimate(d0,c,n);
1.13 ! noro 4245: if ( n > c_len ) {
! 4246: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
! 4247: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
! 4248: c_len = n;
! 4249: }
1.4 noro 4250: for ( i = 0; i < n; i++ ) {
4251: mpz_init(q[i]); mpz_init(r[i]);
4252: mpz_fdiv_qr(q[i],r[i],c[i],d0);
4253: }
4254: for ( i = 0; i < n; i++ )
4255: if ( mpz_sgn(r[i]) ) break;
4256: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
4257: if ( i < n ) {
4258: mpz_gcd(gcd,d0,r[i]);
4259: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
4260: mpz_div(a,d0,gcd);
4261: for ( i = 0; i < n; i++ ) {
4262: mpz_mul(u,a,q[i]);
4263: if ( mpz_sgn(r[i]) ) {
4264: mpz_div(u1,r[i],gcd);
4265: mpz_add(q[i],u,u1);
4266: } else
4267: mpz_set(q[i],u);
4268: }
4269: }
4270: for ( i = 0; i < n; i++ )
4271: mpz_set(c[i],q[i]);
4272: }
4273:
1.1 noro 4274: void nd_mul_c(int mod,ND p,int mul)
4275: {
4276: NM m;
4277: int c,c1;
4278:
4279: if ( !p ) return;
4280: if ( mul == 1 ) return;
4281: if ( mod == -1 )
4282: for ( m = BDY(p); m; m = NEXT(m) )
4283: CM(m) = _mulsf(CM(m),mul);
4284: else
4285: for ( m = BDY(p); m; m = NEXT(m) ) {
4286: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4287: }
4288: }
4289:
4290: void nd_mul_c_lf(ND p,Z mul)
4291: {
4292: NM m;
4293: Z c;
4294:
4295: if ( !p ) return;
4296: if ( UNIZ(mul) ) return;
4297: for ( m = BDY(p); m; m = NEXT(m) ) {
4298: mullf(CZ(m),mul,&c); CZ(m) = c;
4299: }
4300: }
4301:
4302: void nd_mul_c_q(ND p,P mul)
4303: {
4304: NM m;
4305: P c;
4306:
4307: if ( !p ) return;
4308: if ( UNIQ(mul) ) return;
4309: for ( m = BDY(p); m; m = NEXT(m) ) {
4310: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4311: }
4312: }
4313:
4314: void nd_mul_c_p(VL vl,ND p,P mul)
4315: {
4316: NM m;
4317: P c;
4318:
4319: if ( !p ) return;
4320: for ( m = BDY(p); m; m = NEXT(m) ) {
4321: mulp(vl,CP(m),mul,&c); CP(m) = c;
4322: }
4323: }
4324:
4325: void nd_free(ND p)
4326: {
4327: NM t,s;
4328:
4329: if ( !p ) return;
4330: t = BDY(p);
4331: while ( t ) {
4332: s = NEXT(t);
4333: FREENM(t);
4334: t = s;
4335: }
4336: FREEND(p);
4337: }
4338:
4339: void ndv_free(NDV p)
4340: {
4341: GCFREE(BDY(p));
4342: }
4343:
4344: void nd_append_red(UINT *d,int i)
4345: {
4346: RHist m,m0;
4347: int h;
4348:
4349: NEWRHist(m);
4350: h = ndl_hash_value(d);
4351: m->index = i;
4352: ndl_copy(d,DL(m));
4353: NEXT(m) = nd_red[h];
4354: nd_red[h] = m;
4355: }
4356:
4357: UINT *ndv_compute_bound(NDV p)
4358: {
4359: UINT *d1,*d2,*t;
4360: UINT u;
4361: int i,j,k,l,len,ind;
4362: NMV m;
4363:
4364: if ( !p )
4365: return 0;
4366: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4367: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4368: len = LEN(p);
4369: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4370: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4371: ndl_max(DL(m),d1,d2);
4372: t = d1; d1 = d2; d2 = t;
4373: }
4374: l = nd_nvar+31;
4375: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4376: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4377: u = d1[i];
4378: k = (nd_epw-1)*nd_bpe;
4379: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4380: t[ind] = (u>>k)&nd_mask0;
4381: }
4382: for ( ; ind < l; ind++ ) t[ind] = 0;
4383: return t;
4384: }
4385:
4386: UINT *nd_compute_bound(ND p)
4387: {
4388: UINT *d1,*d2,*t;
4389: UINT u;
4390: int i,j,k,l,len,ind;
4391: NM m;
4392:
4393: if ( !p )
4394: return 0;
4395: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4396: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4397: len = LEN(p);
4398: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4399: for ( m = NEXT(m); m; m = NEXT(m) ) {
4400: ndl_lcm(DL(m),d1,d2);
4401: t = d1; d1 = d2; d2 = t;
4402: }
4403: l = nd_nvar+31;
4404: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4405: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4406: u = d1[i];
4407: k = (nd_epw-1)*nd_bpe;
4408: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4409: t[ind] = (u>>k)&nd_mask0;
4410: }
4411: for ( ; ind < l; ind++ ) t[ind] = 0;
4412: return t;
4413: }
4414:
4415: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4416: /* of a term. In this case we need additional 1 word. */
4417:
4418: int nd_get_exporigin(struct order_spec *ord)
4419: {
4420: switch ( ord->id ) {
4421: case 0: case 2: case 256: case 258:
4422: return 1+nd_module;
4423: case 1: case 257:
4424: /* block order */
4425: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4426: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4427: return ord->ord.block.length+1+nd_module;
4428: case 3: case 259:
4429: #if 0
4430: error("nd_get_exporigin : composite order is not supported yet.");
4431: #else
4432: return 1+nd_module;
4433: #endif
4434: default:
4435: error("nd_get_exporigin : ivalid argument.");
4436: return 0;
4437: }
4438: }
4439:
4440: void nd_setup_parameters(int nvar,int max) {
4441: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4442: struct order_pair *op;
4443:
4444: nd_nvar = nvar;
4445: if ( max ) {
4446: /* XXX */
4447: if ( do_weyl ) nd_bpe = 32;
4448: else if ( max < 2 ) nd_bpe = 1;
4449: else if ( max < 4 ) nd_bpe = 2;
4450: else if ( max < 8 ) nd_bpe = 3;
4451: else if ( max < 16 ) nd_bpe = 4;
4452: else if ( max < 32 ) nd_bpe = 5;
4453: else if ( max < 64 ) nd_bpe = 6;
4454: else if ( max < 256 ) nd_bpe = 8;
4455: else if ( max < 1024 ) nd_bpe = 10;
4456: else if ( max < 65536 ) nd_bpe = 16;
4457: else nd_bpe = 32;
4458: }
4459: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4460: UINT t;
4461: int st;
4462: int *v;
4463: /* t = max(weights) */
4464: t = 0;
4465: if ( current_dl_weight_vector )
4466: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4467: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4468: if ( t < st ) t = st;
4469: }
4470: if ( nd_matrix )
4471: for ( i = 0; i < nd_matrix_len; i++ )
4472: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4473: if ( (st=v[j]) < 0 ) st = -st;
4474: if ( t < st ) t = st;
4475: }
4476: /* i = bitsize of t */
4477: for ( i = 0; t; t >>=1, i++ );
4478: /* i += bitsize of nd_nvar */
4479: for ( t = nd_nvar; t; t >>=1, i++);
4480: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4481: if ( (nd_bpe+i) >= 31 )
4482: error("nd_setup_parameters : too large weight");
4483: }
4484: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4485: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4486: nd_exporigin = nd_get_exporigin(nd_ord);
4487: wpd = nd_exporigin+elen;
4488: if ( nd_module )
4489: nd_mpos = nd_exporigin-1;
4490: else
4491: nd_mpos = -1;
4492: if ( wpd != nd_wpd ) {
4493: nd_free_private_storage();
4494: nd_wpd = wpd;
4495: }
4496: if ( nd_bpe < 32 ) {
4497: nd_mask0 = (1<<nd_bpe)-1;
4498: } else {
4499: nd_mask0 = 0xffffffff;
4500: }
4501: bzero(nd_mask,sizeof(nd_mask));
4502: nd_mask1 = 0;
4503: for ( i = 0; i < nd_epw; i++ ) {
4504: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4505: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4506: }
4507: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4508: nd_epos = nd_create_epos(nd_ord);
4509: nd_blockmask = nd_create_blockmask(nd_ord);
4510: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4511: }
4512:
4513: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4514: {
4515: int i,obpe,oadv,h;
4516: static NM prev_nm_free_list;
4517: static ND_pairs prev_ndp_free_list;
4518: RHist mr0,mr;
4519: RHist r;
4520: RHist *old_red;
4521: ND_pairs s0,s,t;
4522: EPOS oepos;
4523:
4524: obpe = nd_bpe;
4525: oadv = nmv_adv;
4526: oepos = nd_epos;
4527: if ( obpe < 2 ) nd_bpe = 2;
4528: else if ( obpe < 3 ) nd_bpe = 3;
4529: else if ( obpe < 4 ) nd_bpe = 4;
4530: else if ( obpe < 5 ) nd_bpe = 5;
4531: else if ( obpe < 6 ) nd_bpe = 6;
4532: else if ( obpe < 8 ) nd_bpe = 8;
4533: else if ( obpe < 10 ) nd_bpe = 10;
4534: else if ( obpe < 16 ) nd_bpe = 16;
4535: else if ( obpe < 32 ) nd_bpe = 32;
4536: else error("nd_reconstruct : exponent too large");
4537:
4538: nd_setup_parameters(nd_nvar,0);
4539: prev_nm_free_list = _nm_free_list;
4540: prev_ndp_free_list = _ndp_free_list;
4541: _nm_free_list = 0;
4542: _ndp_free_list = 0;
4543: for ( i = nd_psn-1; i >= 0; i-- ) {
4544: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4545: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4546: }
4547: if ( trace )
4548: for ( i = nd_psn-1; i >= 0; i-- ) {
4549: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4550: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4551: }
4552: s0 = 0;
4553: for ( t = d; t; t = NEXT(t) ) {
4554: NEXTND_pairs(s0,s);
4555: s->i1 = t->i1;
4556: s->i2 = t->i2;
4557: SG(s) = SG(t);
4558: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4559: }
4560:
4561: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4562: for ( i = 0; i < REDTAB_LEN; i++ ) {
4563: old_red[i] = nd_red[i];
4564: nd_red[i] = 0;
4565: }
4566: for ( i = 0; i < REDTAB_LEN; i++ )
4567: for ( r = old_red[i]; r; r = NEXT(r) ) {
4568: NEWRHist(mr);
4569: mr->index = r->index;
4570: SG(mr) = SG(r);
4571: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4572: h = ndl_hash_value(DL(mr));
4573: NEXT(mr) = nd_red[h];
4574: nd_red[h] = mr;
4575: }
4576: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4577: old_red = 0;
4578: for ( i = 0; i < nd_psn; i++ ) {
4579: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4580: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4581: nd_psh[i] = r;
4582: }
4583: if ( s0 ) NEXT(s) = 0;
4584: prev_nm_free_list = 0;
4585: prev_ndp_free_list = 0;
4586: #if 0
4587: GC_gcollect();
4588: #endif
4589: return s0;
4590: }
4591:
4592: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4593: {
4594: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4595: struct order_pair *op;
4596:
4597: n = nd_nvar;
4598: oepw = (sizeof(UINT)*8)/obpe;
4599: omask0 = (1<<obpe)-1;
4600: TD(r) = TD(d);
4601: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4602: if ( nd_blockmask ) {
4603: l = nd_blockmask->n;
4604: op = nd_blockmask->order_pair;
4605: for ( i = 1; i < nd_exporigin; i++ )
4606: r[i] = d[i];
4607: for ( j = 0, s = 0; j < l; j++ ) {
4608: ord_l = op[j].length;
4609: for ( i = 0; i < ord_l; i++, s++ ) {
4610: ei = GET_EXP_OLD(d,s);
4611: PUT_EXP(r,s,ei);
4612: }
4613: }
4614: } else {
4615: for ( i = 0; i < n; i++ ) {
4616: ei = GET_EXP_OLD(d,i);
4617: PUT_EXP(r,i,ei);
4618: }
4619: }
4620: if ( nd_module ) MPOS(r) = MPOS(d);
4621: }
4622:
4623: ND nd_copy(ND p)
4624: {
4625: NM m,mr,mr0;
4626: int c,n;
4627: ND r;
4628:
4629: if ( !p )
4630: return 0;
4631: else {
4632: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4633: NEXTNM(mr0,mr);
4634: CM(mr) = CM(m);
4635: ndl_copy(DL(m),DL(mr));
4636: }
4637: NEXT(mr) = 0;
4638: MKND(NV(p),mr0,LEN(p),r);
4639: SG(r) = SG(p);
4640: return r;
4641: }
4642: }
4643:
4644: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4645: {
4646: NM m1,m2;
4647: NDV p1,p2;
4648: ND t1,t2;
4649: UINT *lcm;
4650: P gp,tp;
4651: Z g,t;
4652: Z iq;
4653: int td;
4654: LIST hist;
4655: NODE node;
4656: DP d;
4657:
4658: if ( !mod && nd_demand ) {
4659: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4660: } else {
4661: if ( trace ) {
4662: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4663: } else {
4664: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4665: }
4666: }
4667: lcm = LCM(p);
4668: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4669: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4670: FREENM(m1); return 0;
4671: }
4672: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4673: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4674: FREENM(m1); FREENM(m2); return 0;
4675: }
4676:
4677: if ( mod == -1 ) {
4678: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4679: } else if ( mod > 0 ) {
4680: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4681: } else if ( mod == -2 ) {
4682: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4683: } else if ( nd_vc ) {
4684: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4685: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4686: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4687: } else {
1.6 noro 4688: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 4689: }
4690: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4691: *rp = nd_add(mod,t1,t2);
4692: if ( nd_gentrace ) {
4693: /* nd_tracelist is initialized */
1.6 noro 4694: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4695: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 4696: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4697: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4698: nd_tracelist = node;
4699: }
4700: FREENM(m1); FREENM(m2);
4701: return 1;
4702: }
4703:
4704: void ndv_mul_c(int mod,NDV p,int mul)
4705: {
4706: NMV m;
4707: int c,c1,len,i;
4708:
4709: if ( !p ) return;
4710: len = LEN(p);
4711: if ( mod == -1 )
4712: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4713: CM(m) = _mulsf(CM(m),mul);
4714: else
4715: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4716: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4717: }
4718: }
4719:
4720: void ndv_mul_c_lf(NDV p,Z mul)
4721: {
4722: NMV m;
4723: Z c;
4724: int len,i;
4725:
4726: if ( !p ) return;
4727: len = LEN(p);
4728: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4729: mullf(CZ(m),mul,&c); CZ(m) = c;
4730: }
4731: }
4732:
4733: /* for nd_det */
4734: void ndv_mul_c_q(NDV p,Z mul)
4735: {
4736: NMV m;
4737: Z c;
4738: int len,i;
4739:
4740: if ( !p ) return;
4741: len = LEN(p);
4742: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 4743: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 4744: }
4745: }
4746:
4747: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4748: int n2,i,j,l,n,tlen;
4749: UINT *d0;
4750: NM *tab,*psum;
4751: ND s,r;
4752: NM t;
4753: NMV m1;
4754:
4755: if ( !p ) return 0;
4756: n = NV(p); n2 = n>>1;
4757: d0 = DL(m0);
4758: l = LEN(p);
4759: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4760: tab = (NM *)MALLOC(tlen*sizeof(NM));
4761: psum = (NM *)MALLOC(tlen*sizeof(NM));
4762: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4763: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4764: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4765: /* m0(NM) * m1(NMV) => tab(NM) */
4766: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4767: for ( j = 0; j < tlen; j++ ) {
4768: if ( tab[j] ) {
4769: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4770: }
4771: }
4772: }
4773: for ( i = tlen-1, r = 0; i >= 0; i-- )
4774: if ( psum[i] ) {
4775: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4776: MKND(n,psum[i],j,s);
4777: r = nd_add(mod,r,s);
4778: }
4779: if ( r ) SG(r) = SG(p)+TD(d0);
4780: return r;
4781: }
4782:
4783: /* product of monomials */
4784: /* XXX block order is not handled correctly */
4785:
4786: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4787: {
4788: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4789: UINT *d0,*d1,*d,*dt,*ctab;
4790: Z *ctab_q;
4791: Z q,q1;
4792: UINT c0,c1,c;
4793: NM *p;
4794: NM m,t;
4795: int mpos;
4796:
4797: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4798: if ( !m0 || !m1 ) return;
4799: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4800: if ( nd_module )
4801: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4802:
4803: NEWNM(m); d = DL(m);
4804: if ( mod ) {
4805: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4806: } else if ( nd_vc )
4807: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4808: else
1.6 noro 4809: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 4810: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4811: homo = n&1 ? 1 : 0;
4812: if ( homo ) {
4813: /* offset of h-degree */
4814: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4815: PUT_EXP(DL(m),n-1,h);
4816: TD(DL(m)) = h;
4817: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4818: }
4819: tab[0] = m;
4820: NEWNM(m); d = DL(m);
4821: for ( i = 0, curlen = 1; i < n2; i++ ) {
4822: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4823: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4824: /* xi^a*(Di^k*xi^l)*Di^b */
4825: a += l; b += k;
4826: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4827: if ( !k || !l ) {
4828: for ( j = 0; j < curlen; j++ )
4829: if ( (t = tab[j]) != 0 ) {
4830: dt = DL(t);
4831: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4832: if ( nd_blockmask ) ndl_weight_mask(dt);
4833: }
4834: curlen *= k+1;
4835: continue;
4836: }
4837: min = MIN(k,l);
4838: if ( mod ) {
4839: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4840: mkwcm(k,l,mod,(int *)ctab);
4841: } else {
4842: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4843: mkwc(k,l,ctab_q);
4844: }
4845: for ( j = min; j >= 0; j-- ) {
4846: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4847: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4848: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4849: if ( homo ) {
4850: TD(d) = s;
4851: PUT_EXP(d,n-1,s-h);
4852: } else TD(d) = h;
4853: if ( nd_blockmask ) ndl_weight_mask(d);
4854: if ( mod ) c = ctab[j];
4855: else q = ctab_q[j];
4856: p = tab+curlen*j;
4857: if ( j == 0 ) {
4858: for ( u = 0; u < curlen; u++, p++ ) {
4859: if ( tab[u] ) {
4860: ndl_addto(DL(tab[u]),d);
4861: if ( mod ) {
4862: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4863: } else if ( nd_vc )
4864: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4865: else {
1.6 noro 4866: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 4867: }
4868: }
4869: }
4870: } else {
4871: for ( u = 0; u < curlen; u++, p++ ) {
4872: if ( tab[u] ) {
4873: NEWNM(t);
4874: ndl_add(DL(tab[u]),d,DL(t));
4875: if ( mod ) {
4876: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4877: } else if ( nd_vc )
4878: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4879: else
1.6 noro 4880: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 4881: *p = t;
4882: }
4883: }
4884: }
4885: }
4886: curlen *= k+1;
4887: }
4888: FREENM(m);
4889: if ( nd_module ) {
4890: mpos = MPOS(d1);
4891: for ( i = 0; i < tlen; i++ )
4892: if ( tab[i] ) {
4893: d = DL(tab[i]);
4894: MPOS(d) = mpos;
4895: TD(d) = ndl_weight(d);
4896: }
4897: }
4898: }
4899:
4900: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4901: {
4902: NM mr,mr0;
4903: NMV m;
4904: UINT *d,*dt,*dm;
4905: int c,n,td,i,c1,c2,len;
4906: Q q;
4907: ND r;
4908:
4909: if ( !p ) return 0;
4910: else {
4911: n = NV(p); m = BDY(p);
4912: d = DL(m0);
4913: len = LEN(p);
4914: mr0 = 0;
4915: td = TD(d);
4916: c = CM(m0);
4917: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4918: NEXTNM(mr0,mr);
4919: CM(mr) = 1;
4920: ndl_add(DL(m),d,DL(mr));
4921: }
4922: NEXT(mr) = 0;
4923: MKND(NV(p),mr0,len,r);
4924: SG(r) = SG(p) + TD(d);
4925: return r;
4926: }
4927: }
4928:
4929: ND ndv_mul_nm(int mod,NM m0,NDV p)
4930: {
4931: NM mr,mr0;
4932: NMV m;
4933: UINT *d,*dt,*dm;
4934: int c,n,td,i,c1,c2,len;
4935: P q;
4936: ND r;
4937:
4938: if ( !p ) return 0;
4939: else if ( do_weyl ) {
4940: if ( mod < 0 ) {
4941: error("ndv_mul_nm : not implemented (weyl)");
4942: return 0;
4943: } else
4944: return weyl_ndv_mul_nm(mod,m0,p);
4945: } else {
4946: n = NV(p); m = BDY(p);
4947: d = DL(m0);
4948: len = LEN(p);
4949: mr0 = 0;
4950: td = TD(d);
4951: if ( mod == -1 ) {
4952: c = CM(m0);
4953: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4954: NEXTNM(mr0,mr);
4955: CM(mr) = _mulsf(CM(m),c);
4956: ndl_add(DL(m),d,DL(mr));
4957: }
4958: } else if ( mod == -2 ) {
4959: Z cl;
4960: cl = CZ(m0);
4961: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4962: NEXTNM(mr0,mr);
4963: mullf(CZ(m),cl,&CZ(mr));
4964: ndl_add(DL(m),d,DL(mr));
4965: }
4966: } else if ( mod ) {
4967: c = CM(m0);
4968: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4969: NEXTNM(mr0,mr);
4970: c1 = CM(m);
4971: DMAR(c1,c,0,mod,c2);
4972: CM(mr) = c2;
4973: ndl_add(DL(m),d,DL(mr));
4974: }
4975: } else {
4976: q = CP(m0);
4977: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4978: NEXTNM(mr0,mr);
4979: mulp(nd_vc,CP(m),q,&CP(mr));
4980: ndl_add(DL(m),d,DL(mr));
4981: }
4982: }
4983: NEXT(mr) = 0;
4984: MKND(NV(p),mr0,len,r);
4985: SG(r) = SG(p) + TD(d);
4986: return r;
4987: }
4988: }
4989:
4990: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
4991: {
4992: NM mq0,mq;
4993: NMV tm;
4994: Q q;
4995: int i,nv,sg,c,c1,c2,hindex;
4996: ND p,t,r;
4997:
4998: if ( bucket->m < 0 ) return 0;
4999: else {
5000: nv = NV(d);
5001: mq0 = 0;
5002: tm = (NMV)MALLOC(nmv_adv);
5003: while ( 1 ) {
5004: if ( mod > 0 || mod == -1 )
5005: hindex = head_pbucket(mod,bucket);
5006: else if ( mod == -2 )
5007: hindex = head_pbucket_lf(bucket);
5008: else
5009: hindex = head_pbucket_q(bucket);
5010: if ( hindex < 0 ) break;
5011: p = bucket->body[hindex];
5012: NEXTNM(mq0,mq);
5013: ndl_sub(HDL(p),HDL(d),DL(mq));
5014: ndl_copy(DL(mq),DL(tm));
5015: if ( mod ) {
5016: c1 = invm(HCM(d),mod); c2 = HCM(p);
5017: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5018: CM(tm) = mod-c;
5019: } else {
1.6 noro 5020: divsz(HCZ(p),HCZ(d),&CZ(mq));
5021: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5022: }
5023: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5024: bucket->body[hindex] = nd_remove_head(p);
5025: t = nd_remove_head(t);
5026: add_pbucket(mod,bucket,t);
5027: }
5028: if ( !mq0 )
5029: r = 0;
5030: else {
5031: NEXT(mq) = 0;
5032: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5033: MKND(nv,mq0,i,r);
5034: /* XXX */
5035: SG(r) = HTD(r);
5036: }
5037: return r;
5038: }
5039: }
5040:
5041: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5042: {
5043: NMV m,mr,mr0,t;
5044: int len,i,k;
5045:
5046: if ( !p ) return;
5047: m = BDY(p); len = LEN(p);
5048: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5049: m = (NMV)((char *)mr0+(len-1)*oadv);
5050: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5051: t = (NMV)MALLOC(nmv_adv);
5052: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5053: CZ(t) = CZ(m);
1.1 noro 5054: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5055: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5056: CZ(mr) = CZ(t);
1.1 noro 5057: ndl_copy(DL(t),DL(mr));
5058: }
5059: BDY(p) = mr0;
5060: }
5061:
5062: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5063: {
5064: NMV m,mr,mr0;
5065: int len,i;
5066: NDV r;
5067:
5068: if ( !p ) return 0;
5069: m = BDY(p); len = LEN(p);
5070: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5071: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5072: ndl_zero(DL(mr));
5073: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5074: CZ(mr) = CZ(m);
1.1 noro 5075: }
5076: MKNDV(NV(p),mr0,len,r);
5077: SG(r) = SG(p);
5078: return r;
5079: }
5080:
5081: /* duplicate p */
5082:
5083: NDV ndv_dup(int mod,NDV p)
5084: {
5085: NDV d;
5086: NMV t,m,m0;
5087: int i,len;
5088:
5089: if ( !p ) return 0;
5090: len = LEN(p);
5091: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5092: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5093: ndl_copy(DL(t),DL(m));
1.6 noro 5094: CZ(m) = CZ(t);
1.1 noro 5095: }
5096: MKNDV(NV(p),m0,len,d);
5097: SG(d) = SG(p);
5098: return d;
5099: }
5100:
5101: NDV ndv_symbolic(int mod,NDV p)
5102: {
5103: NDV d;
5104: NMV t,m,m0;
5105: int i,len;
5106:
5107: if ( !p ) return 0;
5108: len = LEN(p);
5109: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5110: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5111: ndl_copy(DL(t),DL(m));
1.6 noro 5112: CZ(m) = ONE;
1.1 noro 5113: }
5114: MKNDV(NV(p),m0,len,d);
5115: SG(d) = SG(p);
5116: return d;
5117: }
5118:
5119: ND nd_dup(ND p)
5120: {
5121: ND d;
5122: NM t,m,m0;
5123:
5124: if ( !p ) return 0;
5125: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5126: NEXTNM(m0,m);
5127: ndl_copy(DL(t),DL(m));
1.6 noro 5128: CZ(m) = CZ(t);
1.1 noro 5129: }
5130: if ( m0 ) NEXT(m) = 0;
5131: MKND(NV(p),m0,LEN(p),d);
5132: SG(d) = SG(p);
5133: return d;
5134: }
5135:
5136: /* XXX if p->len == 0 then it represents 0 */
5137:
5138: void ndv_mod(int mod,NDV p)
5139: {
5140: NMV t,d;
5141: int r,s,u;
5142: int i,len,dlen;
5143: P cp;
5144: Z c;
5145: Obj gfs;
5146:
5147: if ( !p ) return;
5148: len = LEN(p);
5149: dlen = 0;
5150: if ( mod == -1 )
5151: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5152: simp_ff((Obj)CP(t),&gfs);
5153: if ( gfs ) {
5154: r = FTOIF(CONT((GFS)gfs));
5155: CM(d) = r;
5156: ndl_copy(DL(t),DL(d));
5157: NMV_ADV(d);
5158: dlen++;
5159: }
5160: }
5161: else if ( mod == -2 )
5162: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5163: simp_ff((Obj)CP(t),&gfs);
5164: if ( gfs ) {
5165: lmtolf((LM)gfs,&CZ(d));
5166: ndl_copy(DL(t),DL(d));
5167: NMV_ADV(d);
5168: dlen++;
5169: }
5170: }
5171: else
5172: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5173: if ( nd_vc ) {
5174: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5175: c = (Z)cp;
5176: } else
1.6 noro 5177: c = CZ(t);
1.1 noro 5178: r = remqi((Q)c,mod);
5179: if ( r ) {
5180: CM(d) = r;
5181: ndl_copy(DL(t),DL(d));
5182: NMV_ADV(d);
5183: dlen++;
5184: }
5185: }
5186: LEN(p) = dlen;
5187: }
5188:
5189: NDV ptondv(VL vl,VL dvl,P p)
5190: {
5191: ND nd;
5192:
5193: nd = ptond(vl,dvl,p);
5194: return ndtondv(0,nd);
5195: }
5196:
5197: void pltozpl(LIST l,Q *cont,LIST *pp)
5198: {
5199: NODE nd,nd1;
5200: int n;
5201: P *pl;
5202: Q *cl;
5203: int i;
5204: P dmy;
5205: Z dvr;
5206: LIST r;
5207:
5208: nd = BDY(l); n = length(nd);
5209: pl = (P *)MALLOC(n*sizeof(P));
5210: cl = (Q *)MALLOC(n*sizeof(P));
5211: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5212: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5213: qltozl(cl,n,&dvr);
5214: nd = BDY(l);
5215: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5216: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5217: }
5218: nd = 0;
5219: for ( i = n-1; i >= 0; i-- ) {
5220: MKNODE(nd1,pl[i],nd); nd = nd1;
5221: }
5222: MKLIST(r,nd);
5223: *pp = r;
5224: }
5225:
5226: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5227:
5228: NDV pltondv(VL vl,VL dvl,LIST p)
5229: {
5230: int i;
5231: NODE t;
5232: ND r,ri;
5233: NM m;
5234:
5235: if ( !nd_module ) error("pltond : module order must be set");
5236: r = 0;
5237: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5238: ri = ptond(vl,dvl,(P)BDY(t));
5239: if ( ri )
5240: for ( m = BDY(ri); m; m = NEXT(m) ) {
5241: MPOS(DL(m)) = i;
5242: TD(DL(m)) = ndl_weight(DL(m));
5243: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5244: }
5245: r = nd_add(0,r,ri);
5246: }
5247: return ndtondv(0,r);
5248: }
5249:
5250: ND ptond(VL vl,VL dvl,P p)
5251: {
5252: int n,i,j,k,e;
5253: VL tvl;
5254: V v;
5255: DCP dc;
5256: DCP *w;
5257: ND r,s,t,u;
5258: P x;
5259: int c;
5260: UINT *d;
5261: NM m,m0;
5262:
5263: if ( !p )
5264: return 0;
5265: else if ( NUM(p) ) {
5266: NEWNM(m);
5267: ndl_zero(DL(m));
5268: if ( !INT((Q)p) )
5269: error("ptond : input must be integer-coefficient");
1.6 noro 5270: CZ(m) = (Z)p;
1.1 noro 5271: NEXT(m) = 0;
5272: MKND(nd_nvar,m,1,r);
5273: SG(r) = 0;
5274: return r;
5275: } else {
5276: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5277: w = (DCP *)MALLOC(k*sizeof(DCP));
5278: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5279: for ( i = 0, tvl = dvl, v = VR(p);
5280: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5281: if ( !tvl ) {
5282: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5283: t = ptond(vl,dvl,COEF(w[j]));
5284: pwrp(vl,x,DEG(w[j]),&p);
5285: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5286: }
5287: return s;
5288: } else {
5289: NEWNM(m0); d = DL(m0);
5290: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 5291: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 5292: TD(d) = MUL_WEIGHT(e,i);
5293: if ( nd_blockmask) ndl_weight_mask(d);
5294: if ( nd_module ) MPOS(d) = 0;
5295: t = ptond(vl,dvl,COEF(w[j]));
5296: for ( m = BDY(t); m; m = NEXT(m) )
5297: ndl_addto(DL(m),d);
5298: SG(t) += TD(d);
5299: s = nd_add(0,s,t);
5300: }
5301: FREENM(m0);
5302: return s;
5303: }
5304: }
5305: }
5306:
5307: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5308: {
5309: VL tvl;
5310: int len,n,j,i,e;
5311: NMV m;
5312: Z q;
5313: P c;
5314: UINT *d;
5315: P s,r,u,t,w;
5316: GFS gfs;
5317:
5318: if ( !p ) return 0;
5319: else {
5320: len = LEN(p);
5321: n = NV(p);
5322: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5323: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5324: if ( mod == -1 ) {
5325: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5326: } else if ( mod == -2 ) {
5327: c = (P)CZ(m);
5328: } else if ( mod > 0 ) {
1.6 noro 5329: STOZ(CM(m),q); c = (P)q;
1.1 noro 5330: } else
5331: c = CP(m);
5332: d = DL(m);
5333: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5334: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5335: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5336: }
5337: addp(vl,s,t,&u); s = u;
5338: }
5339: return s;
5340: }
5341: }
5342:
5343: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5344: {
5345: VL tvl;
5346: int len,n,j,i,e;
5347: NMV m;
5348: Z q;
5349: P c;
5350: UINT *d;
5351: P s,r,u,t,w;
5352: GFS gfs;
5353: P *a;
5354: LIST l;
5355: NODE nd,nd1;
5356:
5357: if ( !p ) return 0;
5358: else {
5359: a = (P *)MALLOC((rank+1)*sizeof(P));
5360: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5361: len = LEN(p);
5362: n = NV(p);
5363: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5364: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5365: if ( mod == -1 ) {
5366: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5367: } else if ( mod ) {
1.6 noro 5368: STOZ(CM(m),q); c = (P)q;
1.1 noro 5369: } else
5370: c = CP(m);
5371: d = DL(m);
5372: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5373: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5374: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5375: }
5376: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5377: }
5378: nd = 0;
5379: for ( i = rank; i > 0; i-- ) {
5380: MKNODE(nd1,a[i],nd); nd = nd1;
5381: }
5382: MKLIST(l,nd);
5383: return l;
5384: }
5385: }
5386:
5387: NDV ndtondv(int mod,ND p)
5388: {
5389: NDV d;
5390: NMV m,m0;
5391: NM t;
5392: int i,len;
5393:
5394: if ( !p ) return 0;
5395: len = LEN(p);
5396: if ( mod > 0 || mod == -1 )
5397: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5398: else
5399: m0 = m = MALLOC(len*nmv_adv);
5400: #if 0
5401: ndv_alloc += nmv_adv*len;
5402: #endif
5403: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5404: ndl_copy(DL(t),DL(m));
1.6 noro 5405: CZ(m) = CZ(t);
1.1 noro 5406: }
5407: MKNDV(NV(p),m0,len,d);
5408: SG(d) = SG(p);
5409: return d;
5410: }
5411:
5412: ND ndvtond(int mod,NDV p)
5413: {
5414: ND d;
5415: NM m,m0;
5416: NMV t;
5417: int i,len;
5418:
5419: if ( !p ) return 0;
5420: m0 = 0;
5421: len = p->len;
5422: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5423: NEXTNM(m0,m);
5424: ndl_copy(DL(t),DL(m));
1.6 noro 5425: CZ(m) = CZ(t);
1.1 noro 5426: }
5427: NEXT(m) = 0;
5428: MKND(NV(p),m0,len,d);
5429: SG(d) = SG(p);
5430: return d;
5431: }
5432:
5433: DP ndvtodp(int mod,NDV p)
5434: {
5435: MP m,m0;
5436: DP d;
5437: NMV t;
5438: int i,len;
5439:
5440: if ( !p ) return 0;
5441: m0 = 0;
5442: len = p->len;
5443: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5444: NEXTMP(m0,m);
5445: m->dl = ndltodl(nd_nvar,DL(t));
5446: m->c = (Obj)ndctop(mod,t->c);
5447: }
5448: NEXT(m) = 0;
5449: MKDP(nd_nvar,m0,d);
5450: SG(d) = SG(p);
5451: return d;
5452: }
5453:
5454: DP ndtodp(int mod,ND p)
5455: {
5456: MP m,m0;
5457: DP d;
5458: NM t;
5459: int i,len;
5460:
5461: if ( !p ) return 0;
5462: m0 = 0;
5463: len = p->len;
5464: for ( t = BDY(p); t; t = NEXT(t) ) {
5465: NEXTMP(m0,m);
5466: m->dl = ndltodl(nd_nvar,DL(t));
5467: m->c = (Obj)ndctop(mod,t->c);
5468: }
5469: NEXT(m) = 0;
5470: MKDP(nd_nvar,m0,d);
5471: SG(d) = SG(p);
5472: return d;
5473: }
5474:
5475: void ndv_print(NDV p)
5476: {
5477: NMV m;
5478: int i,len;
5479:
5480: if ( !p ) printf("0\n");
5481: else {
5482: len = LEN(p);
5483: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5484: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5485: else printf("+%d*",CM(m));
5486: ndl_print(DL(m));
5487: }
5488: printf("\n");
5489: }
5490: }
5491:
5492: void ndv_print_q(NDV p)
5493: {
5494: NMV m;
5495: int i,len;
5496:
5497: if ( !p ) printf("0\n");
5498: else {
5499: len = LEN(p);
5500: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5501: printf("+");
1.6 noro 5502: printexpr(CO,(Obj)CZ(m));
1.1 noro 5503: printf("*");
5504: ndl_print(DL(m));
5505: }
5506: printf("\n");
5507: }
5508: }
5509:
5510: NODE ndv_reducebase(NODE x,int *perm)
5511: {
5512: int len,i,j;
5513: NDVI w;
5514: NODE t,t0;
5515:
5516: len = length(x);
5517: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5518: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5519: w[i].p = BDY(t); w[i].i = perm[i];
5520: }
5521: for ( i = 0; i < len; i++ ) {
5522: for ( j = 0; j < i; j++ ) {
5523: if ( w[i].p && w[j].p ) {
5524: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5525: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5526: }
5527: }
5528: }
5529: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5530: if ( w[i].p ) {
5531: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5532: perm[j++] = w[i].i;
5533: }
5534: }
5535: NEXT(t) = 0; x = t0;
5536: return x;
5537: }
5538:
5539: /* XXX incomplete */
5540:
5541: void nd_init_ord(struct order_spec *ord)
5542: {
5543: nd_module = (ord->id >= 256);
5544: if ( nd_module ) {
5545: nd_dcomp = -1;
5546: nd_ispot = ord->ispot;
5547: nd_pot_nelim = ord->pot_nelim;
5548: nd_poly_weight_len = ord->nv;
5549: nd_poly_weight = ord->top_weight;
5550: nd_module_rank = ord->module_rank;
5551: nd_module_weight = ord->module_top_weight;
5552: }
5553: nd_matrix = 0;
5554: nd_matrix_len = 0;
5555: switch ( ord->id ) {
5556: case 0:
5557: switch ( ord->ord.simple ) {
5558: case 0:
5559: nd_dcomp = 1;
5560: nd_isrlex = 1;
5561: break;
5562: case 1:
5563: nd_dcomp = 1;
5564: nd_isrlex = 0;
5565: break;
5566: case 2:
5567: nd_dcomp = 0;
5568: nd_isrlex = 0;
5569: ndl_compare_function = ndl_lex_compare;
5570: break;
5571: case 11:
5572: /* XXX */
5573: nd_dcomp = 0;
5574: nd_isrlex = 1;
5575: ndl_compare_function = ndl_ww_lex_compare;
5576: break;
5577: default:
5578: error("nd_gr : unsupported order");
5579: }
5580: break;
5581: case 1:
5582: /* block order */
5583: /* XXX */
5584: nd_dcomp = -1;
5585: nd_isrlex = 0;
5586: ndl_compare_function = ndl_block_compare;
5587: break;
5588: case 2:
5589: /* matrix order */
5590: /* XXX */
5591: nd_dcomp = -1;
5592: nd_isrlex = 0;
5593: nd_matrix_len = ord->ord.matrix.row;
5594: nd_matrix = ord->ord.matrix.matrix;
5595: ndl_compare_function = ndl_matrix_compare;
5596: break;
5597: case 3:
5598: /* composite order */
5599: nd_dcomp = -1;
5600: nd_isrlex = 0;
5601: nd_worb_len = ord->ord.composite.length;
5602: nd_worb = ord->ord.composite.w_or_b;
5603: ndl_compare_function = ndl_composite_compare;
5604: break;
5605:
5606: /* module order */
5607: case 256:
5608: switch ( ord->ord.simple ) {
5609: case 0:
5610: nd_isrlex = 1;
5611: ndl_compare_function = ndl_module_grlex_compare;
5612: break;
5613: case 1:
5614: nd_isrlex = 0;
5615: ndl_compare_function = ndl_module_glex_compare;
5616: break;
5617: case 2:
5618: nd_isrlex = 0;
5619: ndl_compare_function = ndl_module_lex_compare;
5620: break;
5621: default:
5622: error("nd_gr : unsupported order");
5623: }
5624: break;
5625: case 257:
5626: /* block order */
5627: nd_isrlex = 0;
5628: ndl_compare_function = ndl_module_block_compare;
5629: break;
5630: case 258:
5631: /* matrix order */
5632: nd_isrlex = 0;
5633: nd_matrix_len = ord->ord.matrix.row;
5634: nd_matrix = ord->ord.matrix.matrix;
5635: ndl_compare_function = ndl_module_matrix_compare;
5636: break;
5637: case 259:
5638: /* composite order */
5639: nd_isrlex = 0;
5640: nd_worb_len = ord->ord.composite.length;
5641: nd_worb = ord->ord.composite.w_or_b;
5642: ndl_compare_function = ndl_module_composite_compare;
5643: break;
5644: }
5645: nd_ord = ord;
5646: }
5647:
5648: BlockMask nd_create_blockmask(struct order_spec *ord)
5649: {
5650: int n,i,j,s,l;
5651: UINT *t;
5652: BlockMask bm;
5653:
5654: /* we only create mask table for block order */
5655: if ( ord->id != 1 && ord->id != 257 )
5656: return 0;
5657: n = ord->ord.block.length;
5658: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5659: bm->n = n;
5660: bm->order_pair = ord->ord.block.order_pair;
5661: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5662: for ( i = 0, s = 0; i < n; i++ ) {
5663: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5664: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5665: l = bm->order_pair[i].length;
5666: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5667: }
5668: return bm;
5669: }
5670:
5671: EPOS nd_create_epos(struct order_spec *ord)
5672: {
5673: int i,j,l,s,ord_l,ord_o;
5674: EPOS epos;
5675: struct order_pair *op;
5676:
5677: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5678: switch ( ord->id ) {
5679: case 0: case 256:
5680: if ( nd_isrlex ) {
5681: for ( i = 0; i < nd_nvar; i++ ) {
5682: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5683: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5684: }
5685: } else {
5686: for ( i = 0; i < nd_nvar; i++ ) {
5687: epos[i].i = nd_exporigin + i/nd_epw;
5688: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5689: }
5690: }
5691: break;
5692: case 1: case 257:
5693: /* block order */
5694: l = ord->ord.block.length;
5695: op = ord->ord.block.order_pair;
5696: for ( j = 0, s = 0; j < l; j++ ) {
5697: ord_o = op[j].order;
5698: ord_l = op[j].length;
5699: if ( !ord_o )
5700: for ( i = 0; i < ord_l; i++ ) {
5701: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5702: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5703: }
5704: else
5705: for ( i = 0; i < ord_l; i++ ) {
5706: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5707: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5708: }
5709: s += ord_l;
5710: }
5711: break;
5712: case 2:
5713: /* matrix order */
5714: case 3:
5715: /* composite order */
5716: default:
5717: for ( i = 0; i < nd_nvar; i++ ) {
5718: epos[i].i = nd_exporigin + i/nd_epw;
5719: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5720: }
5721: break;
5722: }
5723: return epos;
5724: }
5725:
5726: /* external interface */
5727:
5728: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5729: {
5730: NODE t,in0,in;
5731: ND ndf,nf;
5732: NDV ndvf;
5733: VL vv,tv;
5734: int stat,nvar,max,mrank;
5735: union oNDC dn;
5736: Q cont;
5737: P pp;
5738: LIST ppl;
5739:
5740: if ( !f ) {
5741: *rp = 0;
5742: return;
5743: }
5744: pltovl(v,&vv);
5745: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5746:
5747: /* max=65536 implies nd_bpe=32 */
5748: max = 65536;
5749:
5750: nd_module = 0;
5751: /* nd_module will be set if ord is a module ordering */
5752: nd_init_ord(ord);
5753: nd_setup_parameters(nvar,max);
5754: if ( nd_module && OID(f) != O_LIST )
5755: error("nd_nf_p : the first argument must be a list");
5756: if ( nd_module ) mrank = length(BDY((LIST)f));
5757: /* conversion to ndv */
5758: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5759: NEXTNODE(in0,in);
5760: if ( nd_module ) {
5761: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5762: || length(BDY((LIST)BDY(t))) != mrank )
5763: error("nd_nf_p : inconsistent basis element");
5764: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5765: else ppl = (LIST)BDY(t);
5766: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5767: } else {
5768: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5769: else pp = (P)BDY(t);
5770: BDY(in) = (pointer)ptondv(CO,vv,pp);
5771: }
5772: if ( m ) ndv_mod(m,(NDV)BDY(in));
5773: }
5774: if ( in0 ) NEXT(in) = 0;
5775:
5776: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5777: else ndvf = ptondv(CO,vv,(P)f);
5778: if ( m ) ndv_mod(m,ndvf);
5779: ndf = (pointer)ndvtond(m,ndvf);
5780:
5781: /* dont sort, dont removecont */
5782: ndv_setup(m,0,in0,1,1);
5783: nd_scale=2;
1.6 noro 5784: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 5785: if ( !stat )
5786: error("nd_nf_p : exponent too large");
5787: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5788: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5789: }
5790:
5791: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5792: {
5793: NM m;
5794: UINT *t,*s;
5795: int i;
5796:
5797: for ( i = 0; i < n; i++ ) r[i] = 0;
5798: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5799: t = DL(m);
5800: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5801: r[i] = CM(m);
5802: }
5803: for ( i = 0; !r[i]; i++ );
5804: return i;
5805: }
5806:
5807: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
5808: {
5809: NM m;
5810: UINT *t,*s;
5811: int i;
5812:
5813: for ( i = 0; i < n; i++ ) r[i] = 0;
5814: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5815: t = DL(m);
5816: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5817: r[i] = CZ(m);
1.1 noro 5818: }
5819: for ( i = 0; !r[i]; i++ );
5820: return i;
5821: }
5822:
5823: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5824: {
5825: NM m;
5826: UINT *t,*s;
5827: int i;
5828:
5829: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5830: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5831: t = DL(m);
5832: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5833: mpz_set(r[i],BDY(CZ(m)));
5834: }
5835: for ( i = 0; !mpz_sgn(r[i]); i++ );
5836: return i;
5837: }
5838:
5839: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
5840: {
5841: NM m;
5842: unsigned long *v;
5843: int i,j,h,size;
5844: UINT *s,*t;
5845:
5846: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
5847: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
5848: bzero(v,size);
5849: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
5850: t = DL(m);
5851: h = ndl_hash_value(t);
5852: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5853: v[i/BLEN] |= 1L <<(i%BLEN);
5854: }
5855: return v;
5856: }
5857:
5858: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
5859: {
5860: NMV mr;
5861: UINT *d,*t,*s;
5862: int i,j,len,h,head;
5863:
5864: d = DL(m);
5865: len = LEN(p);
5866: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5867: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5868: ndl_add(d,DL(mr),t);
5869: h = ndl_hash_value(t);
5870: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5871: if ( j == 0 ) head = i;
5872: v[i/BLEN] |= 1L <<(i%BLEN);
5873: }
5874: return head;
5875: }
5876:
5877: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
5878: {
5879: NM m;
5880: NMV mr;
5881: UINT *d,*t,*s;
5882: NDV p;
5883: int i,j,len;
5884: Z *r;
5885:
5886: m = pair->mul;
5887: d = DL(m);
5888: p = nd_ps[pair->index];
5889: len = LEN(p);
5890: r = (Z *)CALLOC(n,sizeof(Q));
5891: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5892: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5893: ndl_add(d,DL(mr),t);
5894: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5895: r[i] = CZ(mr);
1.1 noro 5896: }
5897: return r;
5898: }
5899:
1.11 noro 5900: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 5901: {
5902: NM m;
5903: NMV mr;
1.11 noro 5904: UINT *d,*t,*s,*u;
1.1 noro 5905: NDV p;
5906: unsigned char *ivc;
5907: unsigned short *ivs;
5908: UINT *v,*ivi,*s0v;
1.11 noro 5909: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 5910: IndArray r;
5911:
5912: m = pair->mul;
5913: d = DL(m);
5914: if ( trace )
5915: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
5916: else
5917: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
5918:
5919: len = LEN(p);
5920: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5921: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 5922: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
5923: ndl_add(d,DL(mr),t);
5924: st = prev;
5925: ed = n;
5926: while ( ed > st ) {
5927: md = (st+ed)/2;
5928: u = s0+md*nd_wpd;
5929: c = DL_COMPARE(u,t);
5930: if ( c == 0 ) break;
5931: else if ( c > 0 ) st = md;
5932: else ed = md;
5933: }
5934: prev = v[j] = md;
1.1 noro 5935: }
5936: r = (IndArray)MALLOC(sizeof(struct oIndArray));
5937: r->head = v[0];
5938: diff = 0;
5939: for ( i = 1; i < len; i++ ) {
5940: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
5941: }
5942: if ( diff < 256 ) {
5943: r->width = 1;
5944: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
5945: r->index.c = ivc;
5946: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
5947: } else if ( diff < 65536 ) {
5948: r->width = 2;
5949: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
5950: r->index.s = ivs;
5951: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
5952: } else {
5953: r->width = 4;
5954: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
5955: r->index.i = ivi;
5956: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
5957: }
5958: return r;
5959: }
5960:
5961: int compress_array(Z *svect,Z *cvect,int n)
5962: {
5963: int i,j;
5964:
5965: for ( i = j = 0; i < n; i++ )
5966: if ( svect[i] ) cvect[j++] = svect[i];
5967: return j;
5968: }
5969:
5970: void expand_array(Z *svect,Z *cvect,int n)
5971: {
5972: int i,j;
5973:
5974: for ( i = j = 0; j < n; i++ )
5975: if ( svect[i] ) svect[i] = cvect[j++];
5976: }
5977:
1.8 noro 5978: #if 0
1.1 noro 5979: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
5980: {
5981: int i,j,k,len,pos,prev,nz;
5982: Z cs,mcs,c1,c2,cr,gcd,t;
5983: IndArray ivect;
5984: unsigned char *ivc;
5985: unsigned short *ivs;
5986: unsigned int *ivi;
5987: NDV redv;
5988: NMV mr;
5989: NODE rp;
5990: int maxrs;
5991: double hmag;
5992: Z *cvect;
1.3 noro 5993: int l;
1.1 noro 5994:
5995: maxrs = 0;
5996: for ( i = 0; i < col && !svect[i]; i++ );
5997: if ( i == col ) return maxrs;
5998: hmag = p_mag((P)svect[i])*nd_scale;
5999: cvect = (Z *)MALLOC(col*sizeof(Q));
6000: for ( i = 0; i < nred; i++ ) {
6001: ivect = imat[i];
6002: k = ivect->head;
6003: if ( svect[k] ) {
6004: maxrs = MAX(maxrs,rp0[i]->sugar);
6005: redv = nd_demand?ndv_load(rp0[i]->index)
6006: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6007: len = LEN(redv); mr = BDY(redv);
1.6 noro 6008: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6009: chsgnz(cs,&mcs);
6010: if ( !UNIQ(cr) ) {
6011: for ( j = 0; j < col; j++ ) {
6012: mulz(svect[j],cr,&c1); svect[j] = c1;
6013: }
6014: }
6015: svect[k] = 0; prev = k;
6016: switch ( ivect->width ) {
6017: case 1:
6018: ivc = ivect->index.c;
6019: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6020: pos = prev+ivc[j]; prev = pos;
1.6 noro 6021: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6022: }
6023: break;
6024: case 2:
6025: ivs = ivect->index.s;
6026: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6027: pos = prev+ivs[j]; prev = pos;
1.6 noro 6028: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6029: }
6030: break;
6031: case 4:
6032: ivi = ivect->index.i;
6033: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6034: pos = prev+ivi[j]; prev = pos;
1.6 noro 6035: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6036: }
6037: break;
6038: }
6039: for ( j = k+1; j < col && !svect[j]; j++ );
6040: if ( j == col ) break;
6041: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6042: nz = compress_array(svect,cvect,col);
6043: removecont_array((P *)cvect,nz,1);
6044: expand_array(svect,cvect,nz);
6045: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6046: }
6047: }
6048: }
6049: nz = compress_array(svect,cvect,col);
6050: removecont_array((P *)cvect,nz,1);
6051: expand_array(svect,cvect,nz);
6052: if ( DP_Print ) {
6053: fprintf(asir_out,"-"); fflush(asir_out);
6054: }
6055: return maxrs;
6056: }
1.4 noro 6057: #else
1.9 noro 6058:
1.4 noro 6059: /* direct mpz version */
6060: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6061: {
6062: int i,j,k,len,pos,prev;
6063: mpz_t cs,cr,gcd;
6064: IndArray ivect;
6065: unsigned char *ivc;
6066: unsigned short *ivs;
6067: unsigned int *ivi;
6068: NDV redv;
6069: NMV mr;
6070: NODE rp;
6071: int maxrs;
6072: double hmag;
6073: int l;
1.13 ! noro 6074: static mpz_t *svect;
! 6075: static int svect_len=0;
1.4 noro 6076:
6077: maxrs = 0;
6078: for ( i = 0; i < col && !svect0[i]; i++ );
6079: if ( i == col ) return maxrs;
6080: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 ! noro 6081: if ( col > svect_len ) {
! 6082: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
! 6083: svect_len = col;
! 6084: }
1.4 noro 6085: for ( i = 0; i < col; i++ ) {
6086: mpz_init(svect[i]);
6087: if ( svect0[i] )
6088: mpz_set(svect[i],BDY(svect0[i]));
6089: else
6090: mpz_set_ui(svect[i],0);
6091: }
6092: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6093: for ( i = 0; i < nred; i++ ) {
6094: ivect = imat[i];
6095: k = ivect->head;
6096: if ( mpz_sgn(svect[k]) ) {
6097: maxrs = MAX(maxrs,rp0[i]->sugar);
6098: redv = nd_demand?ndv_load(rp0[i]->index)
6099: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6100: len = LEN(redv); mr = BDY(redv);
1.6 noro 6101: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6102: mpz_div(cs,svect[k],gcd);
1.6 noro 6103: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6104: mpz_neg(cs,cs);
1.9 noro 6105: if ( MUNIMPZ(cr) )
6106: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6107: else if ( !UNIMPZ(cr) )
6108: for ( j = 0; j < col; j++ ) {
6109: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6110: }
1.4 noro 6111: mpz_set_ui(svect[k],0);
6112: prev = k;
6113: switch ( ivect->width ) {
6114: case 1:
6115: ivc = ivect->index.c;
6116: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6117: pos = prev+ivc[j]; prev = pos;
1.6 noro 6118: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6119: }
6120: break;
6121: case 2:
6122: ivs = ivect->index.s;
6123: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6124: pos = prev+ivs[j]; prev = pos;
1.6 noro 6125: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6126: }
6127: break;
6128: case 4:
6129: ivi = ivect->index.i;
6130: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6131: pos = prev+ivi[j]; prev = pos;
1.6 noro 6132: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6133: }
6134: break;
6135: }
6136: for ( j = k+1; j < col && !svect[j]; j++ );
6137: if ( j == col ) break;
6138: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6139: mpz_removecont_array(svect,col);
6140: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6141: }
6142: }
6143: }
6144: mpz_removecont_array(svect,col);
6145: if ( DP_Print ) {
6146: fprintf(asir_out,"-"); fflush(asir_out);
6147: }
6148: for ( i = 0; i < col; i++ )
6149: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6150: else svect0[i] = 0;
6151: return maxrs;
6152: }
6153: #endif
1.1 noro 6154:
6155: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6156: {
6157: int i,j,k,len,pos,prev;
6158: UINT c,c1,c2,c3,up,lo,dmy;
6159: IndArray ivect;
6160: unsigned char *ivc;
6161: unsigned short *ivs;
6162: unsigned int *ivi;
6163: NDV redv;
6164: NMV mr;
6165: NODE rp;
6166: int maxrs;
6167:
6168: maxrs = 0;
6169: for ( i = 0; i < nred; i++ ) {
6170: ivect = imat[i];
6171: k = ivect->head; svect[k] %= m;
6172: if ( (c = svect[k]) != 0 ) {
6173: maxrs = MAX(maxrs,rp0[i]->sugar);
6174: c = m-c; redv = nd_ps[rp0[i]->index];
6175: len = LEN(redv); mr = BDY(redv);
6176: svect[k] = 0; prev = k;
6177: switch ( ivect->width ) {
6178: case 1:
6179: ivc = ivect->index.c;
6180: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6181: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6182: if ( c1 ) {
6183: c2 = svect[pos];
6184: DMA(c1,c,c2,up,lo);
6185: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6186: } else svect[pos] = lo;
6187: }
6188: }
6189: break;
6190: case 2:
6191: ivs = ivect->index.s;
6192: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6193: pos = prev+ivs[j]; c1 = CM(mr);
6194: prev = pos;
6195: if ( c1 ) {
6196: c2 = svect[pos];
6197: DMA(c1,c,c2,up,lo);
6198: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6199: } else svect[pos] = lo;
6200: }
6201: }
6202: break;
6203: case 4:
6204: ivi = ivect->index.i;
6205: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6206: pos = prev+ivi[j]; c1 = CM(mr);
6207: prev = pos;
6208: if ( c1 ) {
6209: c2 = svect[pos];
6210: DMA(c1,c,c2,up,lo);
6211: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6212: } else svect[pos] = lo;
6213: }
6214: }
6215: break;
6216: }
6217: }
6218: }
6219: for ( i = 0; i < col; i++ )
6220: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6221: return maxrs;
6222: }
6223:
6224: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6225: {
6226: int i,j,k,len,pos,prev;
6227: UINT c,c1,c2,c3,up,lo,dmy;
6228: IndArray ivect;
6229: unsigned char *ivc;
6230: unsigned short *ivs;
6231: unsigned int *ivi;
6232: NDV redv;
6233: NMV mr;
6234: NODE rp;
6235: int maxrs;
6236:
6237: maxrs = 0;
6238: for ( i = 0; i < nred; i++ ) {
6239: ivect = imat[i];
6240: k = ivect->head;
6241: if ( (c = svect[k]) != 0 ) {
6242: maxrs = MAX(maxrs,rp0[i]->sugar);
6243: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6244: len = LEN(redv); mr = BDY(redv);
6245: svect[k] = 0; prev = k;
6246: switch ( ivect->width ) {
6247: case 1:
6248: ivc = ivect->index.c;
6249: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6250: pos = prev+ivc[j]; prev = pos;
6251: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6252: }
6253: break;
6254: case 2:
6255: ivs = ivect->index.s;
6256: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6257: pos = prev+ivs[j]; prev = pos;
6258: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6259: }
6260: break;
6261: case 4:
6262: ivi = ivect->index.i;
6263: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6264: pos = prev+ivi[j]; prev = pos;
6265: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6266: }
6267: break;
6268: }
6269: }
6270: }
6271: return maxrs;
6272: }
6273:
6274: ND nd_add_lf(ND p1,ND p2)
6275: {
6276: int n,c,can;
6277: ND r;
6278: NM m1,m2,mr0,mr,s;
6279: Z t;
6280:
6281: if ( !p1 ) return p2;
6282: else if ( !p2 ) return p1;
6283: else {
6284: can = 0;
6285: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6286: c = DL_COMPARE(DL(m1),DL(m2));
6287: switch ( c ) {
6288: case 0:
6289: addlf(CZ(m1),CZ(m2),&t);
6290: s = m1; m1 = NEXT(m1);
6291: if ( t ) {
6292: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6293: } else {
6294: can += 2; FREENM(s);
6295: }
6296: s = m2; m2 = NEXT(m2); FREENM(s);
6297: break;
6298: case 1:
6299: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6300: break;
6301: case -1:
6302: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6303: break;
6304: }
6305: }
6306: if ( !mr0 )
6307: if ( m1 ) mr0 = m1;
6308: else if ( m2 ) mr0 = m2;
6309: else return 0;
6310: else if ( m1 ) NEXT(mr) = m1;
6311: else if ( m2 ) NEXT(mr) = m2;
6312: else NEXT(mr) = 0;
6313: BDY(p1) = mr0;
6314: SG(p1) = MAX(SG(p1),SG(p2));
6315: LEN(p1) = LEN(p1)+LEN(p2)-can;
6316: FREEND(p2);
6317: return p1;
6318: }
6319: }
6320:
6321: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6322: {
6323: int i,j,k,len,pos,prev;
6324: mpz_t c,mc,c1;
6325: IndArray ivect;
6326: unsigned char *ivc;
6327: unsigned short *ivs;
6328: unsigned int *ivi;
6329: NDV redv;
6330: NMV mr;
6331: NODE rp;
6332: int maxrs;
6333:
6334: maxrs = 0;
6335: lf_lazy = 1;
6336: for ( i = 0; i < nred; i++ ) {
6337: ivect = imat[i];
6338: k = ivect->head;
6339: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6340: if ( mpz_sgn(svect[k]) ) {
6341: maxrs = MAX(maxrs,rp0[i]->sugar);
6342: mpz_neg(svect[k],svect[k]);
6343: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6344: len = LEN(redv); mr = BDY(redv);
6345: prev = k;
6346: switch ( ivect->width ) {
6347: case 1:
6348: ivc = ivect->index.c;
6349: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6350: pos = prev+ivc[j]; prev = pos;
6351: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6352: }
6353: break;
6354: case 2:
6355: ivs = ivect->index.s;
6356: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6357: pos = prev+ivs[j]; prev = pos;
6358: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6359: }
6360: break;
6361: case 4:
6362: ivi = ivect->index.i;
6363: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6364: pos = prev+ivi[j]; prev = pos;
6365: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6366: }
6367: break;
6368: }
6369: mpz_set_ui(svect[k],0);
6370: }
6371: }
6372: lf_lazy=0;
6373: for ( i = 0; i < col; i++ ) {
6374: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6375: }
6376: return maxrs;
6377: }
6378:
6379: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6380: {
6381: int i,j,k,l,rank,s;
6382: mpz_t a,a1,inv;
6383: mpz_t *t,*pivot,*pk;
6384: mpz_t **mat;
6385: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6386: int size,size1;
6387:
6388: mpz_init(inv);
6389: mpz_init(a);
6390: mat = (mpz_t **)mat0;
6391: size = 0;
6392: for ( rank = 0, j = 0; j < col; j++ ) {
6393: for ( i = rank; i < row; i++ ) {
6394: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6395: }
6396: for ( i = rank; i < row; i++ )
6397: if ( mpz_sgn(mat[i][j]) )
6398: break;
6399: if ( i == row ) {
6400: colstat[j] = 0;
6401: continue;
6402: } else
6403: colstat[j] = 1;
6404: if ( i != rank ) {
6405: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6406: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6407: }
6408: pivot = mat[rank];
6409: s = sugar[rank];
6410: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6411: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6412: if ( mpz_sgn(*pk) ) {
6413: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6414: }
6415: for ( i = rank+1; i < row; i++ ) {
6416: t = mat[i];
6417: if ( mpz_sgn(t[j]) ) {
6418: sugar[i] = MAX(sugar[i],s);
6419: mpz_neg(a,t[j]);
6420: red_by_vect_lf(t+j,pivot+j,a,col-j);
6421: }
6422: }
6423: rank++;
6424: }
6425: for ( j = col-1, l = rank-1; j >= 0; j-- )
6426: if ( colstat[j] ) {
6427: pivot = mat[l];
6428: s = sugar[l];
6429: for ( k = j; k < col; k++ )
6430: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6431: for ( i = 0; i < l; i++ ) {
6432: t = mat[i];
6433: if ( mpz_sgn(t[j]) ) {
6434: sugar[i] = MAX(sugar[i],s);
6435: mpz_neg(a,t[j]);
6436: red_by_vect_lf(t+j,pivot+j,a,col-j);
6437: }
6438: }
6439: l--;
6440: }
6441: for ( j = 0, l = 0; l < rank; j++ )
6442: if ( colstat[j] ) {
6443: t = mat[l];
6444: for ( k = j; k < col; k++ ) {
6445: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6446: }
6447: l++;
6448: }
6449: return rank;
6450: }
6451:
6452:
6453: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6454: {
6455: int j,k,len;
6456: UINT *p;
6457: UINT c;
6458: NDV r;
6459: NMV mr0,mr;
6460:
6461: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6462: if ( !len ) return 0;
6463: else {
6464: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6465: #if 0
6466: ndv_alloc += nmv_adv*len;
6467: #endif
6468: mr = mr0;
6469: p = s0vect;
6470: for ( j = k = 0; j < col; j++, p += nd_wpd )
6471: if ( !rhead[j] ) {
6472: if ( (c = vect[k++]) != 0 ) {
6473: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6474: }
6475: }
6476: MKNDV(nd_nvar,mr0,len,r);
6477: return r;
6478: }
6479: }
6480:
6481: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6482: {
6483: int j,k,len;
6484: UINT *p;
6485: NDV r;
6486: NMV mr0,mr;
6487:
6488: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6489: if ( !len ) return 0;
6490: else {
6491: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6492: mr = mr0;
6493: p = s0vect;
6494: for ( j = 0; j < col; j++, p += nd_wpd )
6495: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6496: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6497: }
6498: MKNDV(nd_nvar,mr0,len,r);
6499: return r;
6500: }
6501: }
6502:
6503: /* for preprocessed vector */
6504:
6505: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6506: {
1.6 noro 6507: int j,k,len;
6508: UINT *p;
6509: Z c;
6510: NDV r;
6511: NMV mr0,mr;
1.1 noro 6512:
1.6 noro 6513: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6514: if ( !len ) return 0;
6515: else {
6516: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6517: #if 0
1.6 noro 6518: ndv_alloc += nmv_adv*len;
1.1 noro 6519: #endif
1.6 noro 6520: mr = mr0;
6521: p = s0vect;
6522: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6523: if ( !rhead[j] ) {
6524: if ( (c = vect[k++]) != 0 ) {
6525: if ( !INT(c) )
6526: error("vect_to_ndv_q : components must be integers");
6527: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6528: }
6529: }
1.1 noro 6530: }
1.6 noro 6531: MKNDV(nd_nvar,mr0,len,r);
6532: return r;
6533: }
1.1 noro 6534: }
6535:
6536: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6537: {
6538: int j,k,len;
6539: UINT *p;
6540: mpz_t c;
6541: NDV r;
6542: NMV mr0,mr;
6543:
6544: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6545: if ( !len ) return 0;
6546: else {
6547: mr0 = (NMV)MALLOC(nmv_adv*len);
6548: #if 0
6549: ndv_alloc += nmv_adv*len;
6550: #endif
6551: mr = mr0;
6552: p = s0vect;
6553: for ( j = k = 0; j < col; j++, p += nd_wpd )
6554: if ( !rhead[j] ) {
6555: c[0] = vect[k++][0];
6556: if ( mpz_sgn(c) ) {
6557: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6558: }
6559: }
6560: MKNDV(nd_nvar,mr0,len,r);
6561: return r;
6562: }
6563: }
6564:
6565: /* for plain vector */
6566:
6567: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6568: {
6569: int j,k,len;
6570: UINT *p;
6571: Z c;
6572: NDV r;
6573: NMV mr0,mr;
6574:
6575: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6576: if ( !len ) return 0;
6577: else {
6578: mr0 = (NMV)MALLOC(nmv_adv*len);
6579: #if 0
6580: ndv_alloc += nmv_adv*len;
6581: #endif
6582: mr = mr0;
6583: p = s0vect;
6584: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6585: if ( (c = vect[k]) != 0 ) {
6586: if ( !INT(c) )
1.6 noro 6587: error("plain_vect_to_ndv_q : components must be integers");
6588: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6589: }
6590: MKNDV(nd_nvar,mr0,len,r);
6591: return r;
6592: }
6593: }
6594:
6595: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6596: {
6597: ND_pairs t;
6598: NODE sp0,sp;
6599: int stat;
6600: ND spol;
6601:
6602: for ( t = l; t; t = NEXT(t) ) {
6603: stat = nd_sp(m,trace,t,&spol);
6604: if ( !stat ) return 0;
6605: if ( spol ) {
6606: add_pbucket_symbolic(bucket,spol);
6607: }
6608: }
6609: return 1;
6610: }
6611:
6612: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6613: {
6614: NODE rp0,rp;
6615: NM mul,head,s0,s;
6616: int index,col,i,sugar;
6617: RHist h;
6618: UINT *s0v,*p;
6619: NM_ind_pair pair;
6620: ND red;
6621: NDV *ps;
6622:
6623: s0 = 0; rp0 = 0; col = 0;
6624: if ( nd_demand )
6625: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6626: else
6627: ps = trace?nd_ps_trace:nd_ps;
6628: while ( 1 ) {
6629: head = remove_head_pbucket_symbolic(bucket);
6630: if ( !head ) break;
6631: if ( !s0 ) s0 = head;
6632: else NEXT(s) = head;
6633: s = head;
6634: index = ndl_find_reducer(DL(head));
6635: if ( index >= 0 ) {
6636: h = nd_psh[index];
6637: NEWNM(mul);
6638: ndl_sub(DL(head),DL(h),DL(mul));
6639: if ( ndl_check_bound2(index,DL(mul)) )
6640: return 0;
6641: sugar = TD(DL(mul))+SG(ps[index]);
6642: MKNM_ind_pair(pair,mul,index,sugar);
6643: red = ndv_mul_nm_symbolic(mul,ps[index]);
6644: add_pbucket_symbolic(bucket,nd_remove_head(red));
6645: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6646: }
6647: col++;
6648: }
6649: if ( rp0 ) NEXT(rp) = 0;
6650: NEXT(s) = 0;
6651: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6652: for ( i = 0, p = s0v, s = s0; i < col;
6653: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6654: *s0vect = s0v;
6655: *r = rp0;
6656: return col;
6657: }
6658:
6659: void print_ndp(ND_pairs l)
6660: {
6661: ND_pairs t;
6662:
6663: for ( t = l; t; t = NEXT(t) )
6664: printf("[%d,%d] ",t->i1,t->i2);
6665: printf("\n");
6666: }
6667:
6668: NODE nd_f4(int m,int checkonly,int **indp)
6669: {
6670: int i,nh,stat,index,f4red;
6671: NODE r,g,tn0,tn,node;
6672: ND_pairs d,l,t,ll0,ll,lh;
6673: LIST l0,l1;
6674: ND spol,red;
6675: NDV nf,redv;
6676: NM s0,s;
6677: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6678: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6679: UINT c;
6680: UINT **spmat;
6681: UINT *s0vect,*svect,*p,*v;
6682: int *colstat;
6683: IndArray *imat;
6684: int *rhead;
6685: int spcol,sprow;
6686: int sugar,sugarh;
6687: PGeoBucket bucket;
6688: struct oEGT eg0,eg1,eg_f4;
6689: Z i1,i2,sugarq;
1.12 noro 6690:
6691: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6692: #if 0
6693: ndv_alloc = 0;
6694: #endif
1.11 noro 6695: Nf4_red=0;
1.1 noro 6696: g = 0; d = 0;
6697: for ( i = 0; i < nd_psn; i++ ) {
6698: d = update_pairs(d,g,i,0);
6699: g = update_base(g,i);
6700: }
6701: nzlist = 0;
6702: nzlist_t = nd_nzlist;
6703: f4red = 1;
6704: nd_last_nonzero = 0;
6705: while ( d ) {
6706: get_eg(&eg0);
6707: l = nd_minsugarp(d,&d);
6708: sugar = nd_sugarweight?l->sugar2:SG(l);
6709: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6710: if ( nzlist_t ) {
6711: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6712: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6713: tn = BDY((LIST)ARG1(node));
6714: if ( !tn ) {
6715: nzlist_t = NEXT(nzlist_t);
6716: continue;
6717: }
6718: /* tn = [[i1,i2],...] */
6719: lh = nd_ipairtospair(tn);
6720: }
6721: bucket = create_pbucket();
6722: stat = nd_sp_f4(m,0,l,bucket);
6723: if ( !stat ) {
6724: for ( t = l; NEXT(t); t = NEXT(t) );
6725: NEXT(t) = d; d = l;
6726: d = nd_reconstruct(0,d);
6727: continue;
6728: }
6729: if ( bucket->m < 0 ) continue;
6730: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6731: if ( !col ) {
6732: for ( t = l; NEXT(t); t = NEXT(t) );
6733: NEXT(t) = d; d = l;
6734: d = nd_reconstruct(0,d);
6735: continue;
6736: }
1.12 noro 6737: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6738: if ( DP_Print )
1.6 noro 6739: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6740: sugar,eg_f4.exectime);
1.1 noro 6741: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6742: if ( checkonly && nflist ) return 0;
6743: /* adding new bases */
6744: if ( nflist ) nd_last_nonzero = f4red;
6745: for ( r = nflist; r; r = NEXT(r) ) {
6746: nf = (NDV)BDY(r);
6747: ndv_removecont(m,nf);
6748: if ( !m && nd_nalg ) {
6749: ND nf1;
6750:
6751: nf1 = ndvtond(m,nf);
6752: nd_monic(0,&nf1);
6753: nd_removecont(m,nf1);
6754: nf = ndtondv(m,nf1);
6755: }
6756: nh = ndv_newps(m,nf,0,1);
6757: d = update_pairs(d,g,nh,0);
6758: g = update_base(g,nh);
6759: }
6760: if ( DP_Print ) {
6761: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6762: }
6763: if ( nd_gentrace ) {
6764: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6765: NEXTNODE(tn0,tn);
1.6 noro 6766: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6767: node = mknode(2,i1,i2); MKLIST(l0,node);
6768: BDY(tn) = l0;
6769: }
6770: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6771: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6772: MKNODE(node,l1,nzlist); nzlist = node;
6773: }
6774: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6775: f4red++;
6776: if ( nd_f4red && f4red > nd_f4red ) break;
6777: if ( nd_rank0 && !nflist ) break;
6778: }
6779: if ( nd_gentrace ) {
6780: MKLIST(l0,reverse_node(nzlist));
6781: MKNODE(nd_alltracelist,l0,0);
6782: }
6783: #if 0
6784: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6785: #endif
1.12 noro 6786: if ( DP_Print ) {
6787: fprintf(asir_out,"number of red=%d,",Nf4_red);
6788: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
6789: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
6790: }
1.1 noro 6791: conv_ilist(nd_demand,0,g,indp);
6792: return g;
6793: }
6794:
6795: NODE nd_f4_trace(int m,int **indp)
6796: {
6797: int i,nh,stat,index;
6798: NODE r,g;
6799: ND_pairs d,l,l0,t;
6800: ND spol,red;
6801: NDV nf,redv,nfqv,nfv;
6802: NM s0,s;
6803: NODE rp0,srp0,nflist;
6804: int nsp,nred,col,rank,len,k,j,a;
6805: UINT c;
6806: UINT **spmat;
6807: UINT *s0vect,*svect,*p,*v;
6808: int *colstat;
6809: IndArray *imat;
6810: int *rhead;
6811: int spcol,sprow;
6812: int sugar;
6813: PGeoBucket bucket;
6814: struct oEGT eg0,eg1,eg_f4;
6815:
6816: g = 0; d = 0;
6817: for ( i = 0; i < nd_psn; i++ ) {
6818: d = update_pairs(d,g,i,0);
6819: g = update_base(g,i);
6820: }
6821: while ( d ) {
6822: get_eg(&eg0);
6823: l = nd_minsugarp(d,&d);
6824: sugar = SG(l);
6825: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6826: bucket = create_pbucket();
6827: stat = nd_sp_f4(m,0,l,bucket);
6828: if ( !stat ) {
6829: for ( t = l; NEXT(t); t = NEXT(t) );
6830: NEXT(t) = d; d = l;
6831: d = nd_reconstruct(1,d);
6832: continue;
6833: }
6834: if ( bucket->m < 0 ) continue;
6835: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6836: if ( !col ) {
6837: for ( t = l; NEXT(t); t = NEXT(t) );
6838: NEXT(t) = d; d = l;
6839: d = nd_reconstruct(1,d);
6840: continue;
6841: }
6842: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6843: if ( DP_Print )
1.3 noro 6844: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 6845: sugar,eg_f4.exectime);
1.1 noro 6846: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6847: if ( !l0 ) continue;
6848: l = l0;
6849:
6850: /* over Q */
6851: bucket = create_pbucket();
6852: stat = nd_sp_f4(0,1,l,bucket);
6853: if ( !stat ) {
6854: for ( t = l; NEXT(t); t = NEXT(t) );
6855: NEXT(t) = d; d = l;
6856: d = nd_reconstruct(1,d);
6857: continue;
6858: }
6859: if ( bucket->m < 0 ) continue;
6860: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6861: if ( !col ) {
6862: for ( t = l; NEXT(t); t = NEXT(t) );
6863: NEXT(t) = d; d = l;
6864: d = nd_reconstruct(1,d);
6865: continue;
6866: }
6867: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6868: /* adding new bases */
6869: for ( r = nflist; r; r = NEXT(r) ) {
6870: nfqv = (NDV)BDY(r);
6871: ndv_removecont(0,nfqv);
1.6 noro 6872: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 6873: if ( nd_nalg ) {
6874: ND nf1;
6875:
6876: nf1 = ndvtond(m,nfqv);
6877: nd_monic(0,&nf1);
6878: nd_removecont(0,nf1);
6879: nfqv = ndtondv(0,nf1); nd_free(nf1);
6880: }
6881: nfv = ndv_dup(0,nfqv);
6882: ndv_mod(m,nfv);
6883: ndv_removecont(m,nfv);
6884: nh = ndv_newps(0,nfv,nfqv,1);
6885: d = update_pairs(d,g,nh,0);
6886: g = update_base(g,nh);
6887: }
6888: }
6889: #if 0
6890: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6891: #endif
6892: conv_ilist(nd_demand,1,g,indp);
6893: return g;
6894: }
6895:
6896: int rref(matrix mat,int *sugar)
6897: {
6898: int row,col,i,j,k,l,s,wcol,wj;
6899: unsigned long bj;
6900: unsigned long **a;
6901: unsigned long *ai,*ak,*as,*t;
6902: int *pivot;
6903:
6904: row = mat->row;
6905: col = mat->col;
6906: a = mat->a;
6907: wcol = (col+BLEN-1)/BLEN;
6908: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6909: i = 0;
6910: for ( j = 0; j < col; j++ ) {
6911: wj = j/BLEN; bj = 1L<<(j%BLEN);
6912: for ( k = i; k < row; k++ )
6913: if ( a[k][wj] & bj ) break;
6914: if ( k == row ) continue;
6915: pivot[i] = j;
6916: if ( k != i ) {
6917: t = a[i]; a[i] = a[k]; a[k] = t;
6918: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6919: }
6920: ai = a[i];
6921: for ( k = i+1; k < row; k++ ) {
6922: ak = a[k];
6923: if ( ak[wj] & bj ) {
6924: for ( l = wj; l < wcol; l++ )
6925: ak[l] ^= ai[l];
6926: sugar[k] = MAX(sugar[k],sugar[i]);
6927: }
6928: }
6929: i++;
6930: }
6931: for ( k = i-1; k >= 0; k-- ) {
6932: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6933: ak = a[k];
6934: for ( s = 0; s < k; s++ ) {
6935: as = a[s];
6936: if ( as[wj] & bj ) {
6937: for ( l = wj; l < wcol; l++ )
6938: as[l] ^= ak[l];
6939: sugar[s] = MAX(sugar[s],sugar[k]);
6940: }
6941: }
6942: }
6943: return i;
6944: }
6945:
6946: void print_matrix(matrix mat)
6947: {
6948: int row,col,i,j;
6949: unsigned long *ai;
6950:
6951: row = mat->row;
6952: col = mat->col;
6953: printf("%d x %d\n",row,col);
6954: for ( i = 0; i < row; i++ ) {
6955: ai = mat->a[i];
6956: for ( j = 0; j < col; j++ ) {
6957: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6958: else putchar('0');
6959: }
6960: putchar('\n');
6961: }
6962: }
6963:
6964: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6965:
6966: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6967: {
6968: int row,col,wcol,wj,i,j;
6969: unsigned long bj;
6970: unsigned long *ai;
6971: unsigned long **a;
6972: int len;
6973: int *pos;
6974:
6975: row = mat->row;
6976: col = mat->col;
6977: wcol = (col+BLEN-1)/BLEN;
6978: pos = (int *)MALLOC(wcol*sizeof(int));
6979: bzero(pos,wcol*sizeof(int));
6980: for ( i = j = 0; i < wcol; i++ )
6981: if ( v[i] ) pos[j++] = i;;
6982: len = j;
6983: wj = rhead/BLEN;
6984: bj = 1L<<rhead%BLEN;
6985: a = mat->a;
6986: for ( i = 0; i < row; i++ ) {
6987: ai = a[i];
6988: if ( ai[wj]&bj ) {
6989: for ( j = 0; j < len; j++ )
6990: ai[pos[j]] ^= v[pos[j]];
6991: sugar[i] = MAX(sugar[i],rsugar);
6992: }
6993: }
6994: }
6995:
6996: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
6997: {
6998: int nsp,nred,i,i0,k,rank,row;
6999: NODE r0,rp;
7000: ND_pairs sp;
7001: ND spol;
7002: NM_ind_pair rt;
7003: int *s0hash;
7004: UINT *s;
7005: int *pivot,*sugar,*head;
7006: matrix mat;
7007: NM m;
7008: NODE r;
7009: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7010: int rhead,rsugar,size;
7011: unsigned long *v;
7012:
7013: get_eg(&eg0);
7014: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7015: nred = length(rp0);
7016: mat = alloc_matrix(nsp,col);
7017: s0hash = (int *)MALLOC(col*sizeof(int));
7018: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7019: s0hash[i] = ndl_hash_value(s);
7020:
7021: sugar = (int *)MALLOC(nsp*sizeof(int));
7022: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7023: nd_sp(2,0,sp,&spol);
7024: if ( spol ) {
7025: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7026: sugar[i] = SG(spol);
7027: i++;
7028: }
7029: }
7030: mat->row = i;
7031: if ( DP_Print ) {
7032: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7033: }
7034: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7035: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7036: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7037: rt = (NM_ind_pair)BDY(rp);
7038: bzero(v,size);
7039: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7040: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7041: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7042: }
7043:
7044: get_eg(&eg1);
7045: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7046: rank = rref(mat,sugar);
7047:
7048: for ( i = 0, r0 = 0; i < rank; i++ ) {
7049: NEXTNODE(r0,r);
7050: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7051: SG((NDV)BDY(r)) = sugar[i];
7052: }
7053: if ( r0 ) NEXT(r) = 0;
7054: get_eg(&eg2);
7055: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7056: if ( DP_Print ) {
7057: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7058: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7059: fflush(asir_out);
7060: }
7061: return r0;
7062: }
7063:
7064:
7065: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7066: {
7067: IndArray *imat;
1.11 noro 7068: int nsp,nred,i,start;
1.1 noro 7069: int *rhead;
7070: NODE r0,rp;
7071: ND_pairs sp;
7072: NM_ind_pair *rvect;
7073: UINT *s;
7074: int *s0hash;
1.11 noro 7075: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7076:
7077: if ( m == 2 && nd_rref2 )
7078: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7079:
7080: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7081: nred = length(rp0);
7082: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7083: rhead = (int *)MALLOC(col*sizeof(int));
7084: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7085:
7086: /* construction of index arrays */
1.11 noro 7087: get_eg(&eg0);
1.1 noro 7088: if ( DP_Print ) {
1.11 noro 7089: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7090: fflush(asir_out);
1.1 noro 7091: }
7092: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7093: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7094: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7095: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7096: rhead[imat[i]->head] = 1;
1.11 noro 7097: start = imat[i]->head;
7098: }
1.12 noro 7099: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7100: if ( DP_Print ) {
7101: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7102: fflush(asir_out);
1.1 noro 7103: }
7104: if ( m > 0 )
1.7 noro 7105: #if SIZEOF_LONG==8
1.1 noro 7106: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7107: #else
7108: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7109: #endif
7110: else if ( m == -1 )
7111: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7112: else if ( m == -2 )
7113: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7114: else
7115: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7116: return r0;
7117: }
7118:
7119: /* for Fp, 2<=p<2^16 */
7120:
7121: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7122: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7123: {
7124: int spcol,sprow,a;
7125: int i,j,k,l,rank;
7126: NODE r0,r;
7127: ND_pairs sp;
7128: ND spol;
7129: UINT **spmat;
7130: UINT *svect,*v;
7131: int *colstat;
7132: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7133: int maxrs;
7134: int *spsugar;
7135: ND_pairs *spactive;
7136:
7137: spcol = col-nred;
7138: get_eg(&eg0);
7139: /* elimination (1st step) */
7140: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7141: svect = (UINT *)MALLOC(col*sizeof(UINT));
7142: spsugar = (int *)MALLOC(nsp*sizeof(int));
7143: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7144: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7145: nd_sp(m,0,sp,&spol);
7146: if ( !spol ) continue;
7147: nd_to_vect(m,s0vect,col,spol,svect);
7148: if ( m == -1 )
7149: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7150: else
7151: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7152: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7153: if ( i < col ) {
7154: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7155: for ( j = k = 0; j < col; j++ )
7156: if ( !rhead[j] ) v[k++] = svect[j];
7157: spsugar[sprow] = MAX(maxrs,SG(spol));
7158: if ( nz )
7159: spactive[sprow] = sp;
7160: sprow++;
7161: }
7162: nd_free(spol);
7163: }
7164: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7165: if ( DP_Print ) {
1.5 noro 7166: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7167: fflush(asir_out);
7168: }
7169: /* free index arrays */
7170: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7171:
7172: /* elimination (2nd step) */
7173: colstat = (int *)MALLOC(spcol*sizeof(int));
7174: if ( m == -1 )
7175: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7176: else
7177: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7178: r0 = 0;
7179: for ( i = 0; i < rank; i++ ) {
7180: NEXTNODE(r0,r); BDY(r) =
7181: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7182: SG((NDV)BDY(r)) = spsugar[i];
7183: GCFREE(spmat[i]);
7184: }
7185: if ( r0 ) NEXT(r) = 0;
7186:
7187: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7188: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7189: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7190: if ( DP_Print ) {
1.5 noro 7191: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7192: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7193: nsp,nred,sprow,spcol,rank);
1.5 noro 7194: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7195: }
7196: if ( nz ) {
7197: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7198: if ( rank > 0 ) {
7199: NEXT(spactive[rank-1]) = 0;
7200: *nz = spactive[0];
7201: } else
7202: *nz = 0;
7203: }
7204: return r0;
7205: }
7206:
7207:
7208: /* for small finite fields */
7209:
7210: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7211: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7212: {
7213: int spcol,sprow,a;
7214: int i,j,k,l,rank;
7215: NODE r0,r;
7216: ND_pairs sp;
7217: ND spol;
7218: UINT **spmat;
7219: UINT *svect,*v;
7220: int *colstat;
7221: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7222: int maxrs;
7223: int *spsugar;
7224: ND_pairs *spactive;
7225:
7226: spcol = col-nred;
7227: get_eg(&eg0);
7228: /* elimination (1st step) */
7229: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7230: svect = (UINT *)MALLOC(col*sizeof(UINT));
7231: spsugar = (int *)MALLOC(nsp*sizeof(int));
7232: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7233: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7234: nd_sp(m,0,sp,&spol);
7235: if ( !spol ) continue;
7236: nd_to_vect(m,s0vect,col,spol,svect);
7237: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7238: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7239: if ( i < col ) {
7240: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7241: for ( j = k = 0; j < col; j++ )
7242: if ( !rhead[j] ) v[k++] = svect[j];
7243: spsugar[sprow] = MAX(maxrs,SG(spol));
7244: if ( nz )
7245: spactive[sprow] = sp;
7246: sprow++;
7247: }
7248: nd_free(spol);
7249: }
7250: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7251: if ( DP_Print ) {
1.5 noro 7252: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7253: fflush(asir_out);
7254: }
7255: /* free index arrays */
7256: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7257:
7258: /* elimination (2nd step) */
7259: colstat = (int *)MALLOC(spcol*sizeof(int));
7260: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7261: r0 = 0;
7262: for ( i = 0; i < rank; i++ ) {
7263: NEXTNODE(r0,r); BDY(r) =
7264: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7265: SG((NDV)BDY(r)) = spsugar[i];
7266: GCFREE(spmat[i]);
7267: }
7268: if ( r0 ) NEXT(r) = 0;
7269:
7270: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7271: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7272: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7273: if ( DP_Print ) {
1.5 noro 7274: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7275: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7276: nsp,nred,sprow,spcol,rank);
1.5 noro 7277: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7278: }
7279: if ( nz ) {
7280: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7281: if ( rank > 0 ) {
7282: NEXT(spactive[rank-1]) = 0;
7283: *nz = spactive[0];
7284: } else
7285: *nz = 0;
7286: }
7287: return r0;
7288: }
7289:
7290: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7291: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7292: {
7293: int spcol,sprow,a;
7294: int i,j,k,l,rank;
7295: NODE r0,r;
7296: ND_pairs sp;
7297: ND spol;
7298: mpz_t **spmat;
7299: mpz_t *svect,*v;
7300: int *colstat;
7301: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7302: int maxrs;
7303: int *spsugar;
7304: pointer *w;
7305:
7306: spcol = col-nred;
7307: get_eg(&eg0);
7308: /* elimination (1st step) */
7309: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7310: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7311: spsugar = (int *)MALLOC(nsp*sizeof(int));
7312: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7313: nd_sp(m,trace,sp,&spol);
7314: if ( !spol ) continue;
7315: nd_to_vect_lf(s0vect,col,spol,svect);
7316: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7317: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7318: if ( i < col ) {
7319: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7320: for ( j = k = 0; j < col; j++ )
7321: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7322: spsugar[sprow] = MAX(maxrs,SG(spol));
7323: sprow++;
7324: }
7325: /* nd_free(spol); */
7326: }
7327: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7328: if ( DP_Print ) {
1.5 noro 7329: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7330: fflush(asir_out);
7331: }
7332: /* free index arrays */
7333: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7334:
7335: /* elimination (2nd step) */
7336: colstat = (int *)MALLOC(spcol*sizeof(int));
7337: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7338: w = (pointer *)MALLOC(rank*sizeof(pointer));
7339: for ( i = 0; i < rank; i++ ) {
7340: #if 0
7341: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7342: SG((NDV)w[rank-i-1]) = spsugar[i];
7343: #else
7344: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7345: SG((NDV)w[i]) = spsugar[i];
7346: #endif
7347: /* GCFREE(spmat[i]); */
7348:
7349: }
7350: #if 0
7351: qsort(w,rank,sizeof(NDV),
7352: (int (*)(const void *,const void *))ndv_compare);
7353: #endif
7354: r0 = 0;
7355: for ( i = 0; i < rank; i++ ) {
7356: NEXTNODE(r0,r); BDY(r) = w[i];
7357: }
7358: if ( r0 ) NEXT(r) = 0;
7359:
7360: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7361: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7362: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7363: if ( DP_Print ) {
1.5 noro 7364: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7365: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7366: nsp,nred,sprow,spcol,rank);
1.5 noro 7367: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7368: }
7369: return r0;
7370: }
7371:
7372: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7373: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7374: {
7375: int spcol,sprow,a;
7376: int i,j,k,l,rank;
7377: NODE r0,r;
7378: ND_pairs sp;
7379: ND spol;
7380: Z **spmat;
7381: Z *svect,*v;
7382: int *colstat;
7383: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7384: int maxrs;
7385: int *spsugar;
7386: pointer *w;
7387:
7388: spcol = col-nred;
7389: get_eg(&eg0);
7390: /* elimination (1st step) */
7391: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7392: svect = (Z *)MALLOC(col*sizeof(Q));
7393: spsugar = (int *)MALLOC(nsp*sizeof(int));
7394: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7395: nd_sp(0,trace,sp,&spol);
7396: if ( !spol ) continue;
7397: nd_to_vect_q(s0vect,col,spol,svect);
7398: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7399: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7400: if ( i < col ) {
7401: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7402: for ( j = k = 0; j < col; j++ )
7403: if ( !rhead[j] ) v[k++] = svect[j];
7404: spsugar[sprow] = MAX(maxrs,SG(spol));
7405: sprow++;
7406: }
7407: /* nd_free(spol); */
7408: }
7409: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7410: if ( DP_Print ) {
1.5 noro 7411: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7412: fflush(asir_out);
7413: }
7414: /* free index arrays */
7415: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7416:
7417: /* elimination (2nd step) */
7418: colstat = (int *)MALLOC(spcol*sizeof(int));
7419: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7420: w = (pointer *)MALLOC(rank*sizeof(pointer));
7421: for ( i = 0; i < rank; i++ ) {
7422: #if 0
7423: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7424: SG((NDV)w[rank-i-1]) = spsugar[i];
7425: #else
7426: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7427: SG((NDV)w[i]) = spsugar[i];
7428: #endif
7429: /* GCFREE(spmat[i]); */
7430: }
7431: #if 0
7432: qsort(w,rank,sizeof(NDV),
7433: (int (*)(const void *,const void *))ndv_compare);
7434: #endif
7435: r0 = 0;
7436: for ( i = 0; i < rank; i++ ) {
7437: NEXTNODE(r0,r); BDY(r) = w[i];
7438: }
7439: if ( r0 ) NEXT(r) = 0;
7440:
7441: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7442: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7443: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7444: if ( DP_Print ) {
1.5 noro 7445: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7446: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7447: nsp,nred,sprow,spcol,rank);
1.5 noro 7448: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7449: }
7450: return r0;
7451: }
7452:
7453: FILE *nd_write,*nd_read;
7454:
7455: void nd_send_int(int a) {
7456: write_int(nd_write,(unsigned int *)&a);
7457: }
7458:
7459: void nd_send_intarray(int *p,int len) {
7460: write_intarray(nd_write,(unsigned int *)p,len);
7461: }
7462:
7463: int nd_recv_int() {
7464: int a;
7465:
7466: read_int(nd_read,(unsigned int *)&a);
7467: return a;
7468: }
7469:
7470: void nd_recv_intarray(int *p,int len) {
7471: read_intarray(nd_read,(unsigned int *)p,len);
7472: }
7473:
7474: void nd_send_ndv(NDV p) {
7475: int len,i;
7476: NMV m;
7477:
7478: if ( !p ) nd_send_int(0);
7479: else {
7480: len = LEN(p);
7481: nd_send_int(len);
7482: m = BDY(p);
7483: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7484: nd_send_int(CM(m));
7485: nd_send_intarray((int *)DL(m),nd_wpd);
7486: }
7487: }
7488: }
7489:
7490: void nd_send_nd(ND p) {
7491: int len,i;
7492: NM m;
7493:
7494: if ( !p ) nd_send_int(0);
7495: else {
7496: len = LEN(p);
7497: nd_send_int(len);
7498: m = BDY(p);
7499: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7500: nd_send_int(CM(m));
7501: nd_send_intarray((int *)DL(m),nd_wpd);
7502: }
7503: }
7504: }
7505:
7506: NDV nd_recv_ndv()
7507: {
7508: int len,i;
7509: NMV m,m0;
7510: NDV r;
7511:
7512: len = nd_recv_int();
7513: if ( !len ) return 0;
7514: else {
7515: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7516: #if 0
7517: ndv_alloc += len*nmv_adv;
7518: #endif
7519: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7520: CM(m) = nd_recv_int();
7521: nd_recv_intarray((int *)DL(m),nd_wpd);
7522: }
7523: MKNDV(nd_nvar,m0,len,r);
7524: return r;
7525: }
7526: }
7527:
7528: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7529: {
7530: int i,j,t,c,rank,inv;
7531: int *ci,*ri;
7532: Z dn;
7533: MAT m,nm;
7534:
7535: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7536: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7537: for ( i = 0; i < row; i++ )
7538: for ( j = 0; j < col; j++ )
7539: mat0[i][j] = 0;
7540: c = col-rank;
7541: for ( i = 0; i < rank; i++ ) {
7542: mat0[i][ri[i]] = dn;
7543: for ( j = 0; j < c; j++ )
7544: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7545: }
7546: return rank;
7547: }
7548:
7549: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7550: {
7551: int i,j,k,l,inv,a,rank,s;
7552: unsigned int *t,*pivot,*pk;
7553: unsigned int **mat;
7554: ND_pairs pair;
7555:
7556: mat = (unsigned int **)mat0;
7557: for ( rank = 0, j = 0; j < col; j++ ) {
7558: for ( i = rank; i < row; i++ )
7559: mat[i][j] %= md;
7560: for ( i = rank; i < row; i++ )
7561: if ( mat[i][j] )
7562: break;
7563: if ( i == row ) {
7564: colstat[j] = 0;
7565: continue;
7566: } else
7567: colstat[j] = 1;
7568: if ( i != rank ) {
7569: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7570: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7571: if ( spactive ) {
7572: pair = spactive[i]; spactive[i] = spactive[rank];
7573: spactive[rank] = pair;
7574: }
7575: }
7576: pivot = mat[rank];
7577: s = sugar[rank];
7578: inv = invm(pivot[j],md);
7579: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7580: if ( *pk ) {
7581: if ( *pk >= (unsigned int)md )
7582: *pk %= md;
7583: DMAR(*pk,inv,0,md,*pk)
7584: }
7585: for ( i = rank+1; i < row; i++ ) {
7586: t = mat[i];
7587: if ( (a = t[j]) != 0 ) {
7588: sugar[i] = MAX(sugar[i],s);
7589: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7590: }
7591: }
7592: rank++;
7593: }
7594: for ( j = col-1, l = rank-1; j >= 0; j-- )
7595: if ( colstat[j] ) {
7596: pivot = mat[l];
7597: s = sugar[l];
7598: for ( i = 0; i < l; i++ ) {
7599: t = mat[i];
7600: t[j] %= md;
7601: if ( (a = t[j]) != 0 ) {
7602: sugar[i] = MAX(sugar[i],s);
7603: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7604: }
7605: }
7606: l--;
7607: }
7608: for ( j = 0, l = 0; l < rank; j++ )
7609: if ( colstat[j] ) {
7610: t = mat[l];
7611: for ( k = j; k < col; k++ )
7612: if ( t[k] >= (unsigned int)md )
7613: t[k] %= md;
7614: l++;
7615: }
7616: return rank;
7617: }
7618:
7619:
1.7 noro 7620: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7621: {
1.7 noro 7622: int i,j,k,l,inv,a,rank,s;
7623: unsigned int *t,*pivot,*pk;
7624: unsigned int **mat;
7625:
7626: mat = (unsigned int **)mat0;
7627: for ( rank = 0, j = 0; j < col; j++ ) {
7628: for ( i = rank; i < row; i++ )
7629: if ( mat[i][j] )
7630: break;
7631: if ( i == row ) {
7632: colstat[j] = 0;
7633: continue;
7634: } else
7635: colstat[j] = 1;
7636: if ( i != rank ) {
7637: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7638: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7639: }
7640: pivot = mat[rank];
7641: s = sugar[rank];
7642: inv = _invsf(pivot[j]);
7643: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7644: if ( *pk )
7645: *pk = _mulsf(*pk,inv);
7646: for ( i = rank+1; i < row; i++ ) {
7647: t = mat[i];
7648: if ( (a = t[j]) != 0 ) {
7649: sugar[i] = MAX(sugar[i],s);
7650: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7651: }
7652: }
7653: rank++;
7654: }
7655: for ( j = col-1, l = rank-1; j >= 0; j-- )
7656: if ( colstat[j] ) {
7657: pivot = mat[l];
7658: s = sugar[l];
7659: for ( i = 0; i < l; i++ ) {
7660: t = mat[i];
7661: if ( (a = t[j]) != 0 ) {
7662: sugar[i] = MAX(sugar[i],s);
7663: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7664: }
7665: }
7666: l--;
7667: }
7668: return rank;
7669: }
1.1 noro 7670:
1.7 noro 7671: int ndv_ishomo(NDV p)
7672: {
7673: NMV m;
7674: int len,h;
1.1 noro 7675:
7676: if ( !p ) return 1;
7677: len = LEN(p);
7678: m = BDY(p);
7679: h = TD(DL(m));
7680: NMV_ADV(m);
7681: for ( len--; len; len--, NMV_ADV(m) )
7682: if ( TD(DL(m)) != h ) return 0;
7683: return 1;
7684: }
7685:
7686: void ndv_save(NDV p,int index)
7687: {
7688: FILE *s;
7689: char name[BUFSIZ];
7690: short id;
7691: int nv,sugar,len,n,i,td,e,j;
7692: NMV m;
7693: unsigned int *dl;
7694: int mpos;
7695:
7696: sprintf(name,"%s/%d",Demand,index);
7697: s = fopen(name,"w");
7698: savevl(s,0);
7699: if ( !p ) {
7700: saveobj(s,0);
7701: return;
7702: }
7703: id = O_DP;
7704: nv = NV(p);
7705: sugar = SG(p);
7706: len = LEN(p);
7707: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7708: write_int(s,(unsigned int *)&len);
7709:
7710: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7711: saveobj(s,(Obj)CZ(m));
1.1 noro 7712: dl = DL(m);
7713: td = TD(dl);
7714: write_int(s,(unsigned int *)&td);
7715: for ( j = 0; j < nv; j++ ) {
7716: e = GET_EXP(dl,j);
7717: write_int(s,(unsigned int *)&e);
7718: }
7719: if ( nd_module ) {
7720: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7721: }
7722: }
7723: fclose(s);
7724: }
7725:
7726: void nd_save_mod(ND p,int index)
7727: {
7728: FILE *s;
7729: char name[BUFSIZ];
7730: int nv,sugar,len,c;
7731: NM m;
7732:
7733: sprintf(name,"%s/%d",Demand,index);
7734: s = fopen(name,"w");
7735: if ( !p ) {
7736: len = 0;
7737: write_int(s,(unsigned int *)&len);
7738: fclose(s);
7739: return;
7740: }
7741: nv = NV(p);
7742: sugar = SG(p);
7743: len = LEN(p);
7744: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7745: for ( m = BDY(p); m; m = NEXT(m) ) {
7746: c = CM(m); write_int(s,(unsigned int *)&c);
7747: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7748: }
7749: fclose(s);
7750: }
7751:
7752: NDV ndv_load(int index)
7753: {
7754: FILE *s;
7755: char name[BUFSIZ];
7756: short id;
7757: int nv,sugar,len,n,i,td,e,j;
7758: NDV d;
7759: NMV m0,m;
7760: unsigned int *dl;
7761: Obj obj;
7762: int mpos;
7763:
7764: sprintf(name,"%s/%d",Demand,index);
7765: s = fopen(name,"r");
7766: if ( !s ) return 0;
7767:
7768: skipvl(s);
7769: read_short(s,(unsigned short *)&id);
7770: if ( !id ) return 0;
7771: read_int(s,(unsigned int *)&nv);
7772: read_int(s,(unsigned int *)&sugar);
7773: read_int(s,(unsigned int *)&len);
7774:
7775: m0 = m = MALLOC(len*nmv_adv);
7776: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7777: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7778: dl = DL(m);
7779: ndl_zero(dl);
7780: read_int(s,(unsigned int *)&td); TD(dl) = td;
7781: for ( j = 0; j < nv; j++ ) {
7782: read_int(s,(unsigned int *)&e);
7783: PUT_EXP(dl,j,e);
7784: }
7785: if ( nd_module ) {
7786: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7787: }
7788: if ( nd_blockmask ) ndl_weight_mask(dl);
7789: }
7790: fclose(s);
7791: MKNDV(nv,m0,len,d);
7792: SG(d) = sugar;
7793: return d;
7794: }
7795:
7796: ND nd_load_mod(int index)
7797: {
7798: FILE *s;
7799: char name[BUFSIZ];
7800: int nv,sugar,len,i,c;
7801: ND d;
7802: NM m0,m;
7803:
7804: sprintf(name,"%s/%d",Demand,index);
7805: s = fopen(name,"r");
7806: /* if the file does not exist, it means p[index]=0 */
7807: if ( !s ) return 0;
7808:
7809: read_int(s,(unsigned int *)&nv);
7810: if ( !nv ) { fclose(s); return 0; }
7811:
7812: read_int(s,(unsigned int *)&sugar);
7813: read_int(s,(unsigned int *)&len);
7814: for ( m0 = 0, i = 0; i < len; i++ ) {
7815: NEXTNM(m0,m);
7816: read_int(s,(unsigned int *)&c); CM(m) = c;
7817: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7818: }
7819: NEXT(m) = 0;
7820: MKND(nv,m0,len,d);
7821: SG(d) = sugar;
7822: fclose(s);
7823: return d;
7824: }
7825:
7826: void nd_det(int mod,MAT f,P *rp)
7827: {
7828: VL fv,tv;
7829: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7830: pointer **m;
7831: P **w;
7832: P mp,r;
7833: NDV **dm;
7834: NDV *t,*mi,*mj;
7835: NDV d,s,mij,mjj;
7836: ND u;
7837: NMV nmv;
7838: UINT *bound;
7839: PGeoBucket bucket;
7840: struct order_spec *ord;
7841: Z dq,dt,ds;
7842: Z mone;
7843: Z gn,qn,dn0,nm,dn;
7844:
7845: create_order_spec(0,0,&ord);
7846: nd_init_ord(ord);
7847: get_vars((Obj)f,&fv);
7848: if ( f->row != f->col )
7849: error("nd_det : non-square matrix");
7850: n = f->row;
7851: m = f->body;
7852: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7853:
7854: if ( !nvar ) {
7855: if ( !mod )
7856: detp(CO,(P **)m,n,rp);
7857: else {
7858: w = (P **)almat_pointer(n,n);
7859: for ( i = 0; i < n; i++ )
7860: for ( j = 0; j < n; j++ )
7861: ptomp(mod,(P)m[i][j],&w[i][j]);
7862: detmp(CO,mod,w,n,&mp);
7863: mptop(mp,rp);
7864: }
7865: return;
7866: }
7867:
7868: if ( !mod ) {
7869: w = (P **)almat_pointer(n,n);
7870: dq = ONE;
7871: for ( i = 0; i < n; i++ ) {
7872: dn0 = ONE;
7873: for ( j = 0; j < n; j++ ) {
7874: if ( !m[i][j] ) continue;
7875: lgp(m[i][j],&nm,&dn);
1.6 noro 7876: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 7877: }
7878: if ( !UNIZ(dn0) ) {
7879: ds = dn0;
7880: for ( j = 0; j < n; j++ )
7881: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
7882: mulz(dq,ds,&dt); dq = dt;
7883: } else
7884: for ( j = 0; j < n; j++ )
7885: w[i][j] = (P)m[i][j];
7886: }
7887: m = (pointer **)w;
7888: }
7889:
7890: for ( i = 0, max = 1; i < n; i++ )
7891: for ( j = 0; j < n; j++ )
7892: for ( tv = fv; tv; tv = NEXT(tv) ) {
7893: e = getdeg(tv->v,(P)m[i][j]);
7894: max = MAX(e,max);
7895: }
7896: nd_setup_parameters(nvar,max);
7897: dm = (NDV **)almat_pointer(n,n);
7898: for ( i = 0, max = 1; i < n; i++ )
7899: for ( j = 0; j < n; j++ ) {
7900: dm[i][j] = ptondv(CO,fv,m[i][j]);
7901: if ( mod ) ndv_mod(mod,dm[i][j]);
7902: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
7903: }
7904: d = ptondv(CO,fv,(P)ONE);
7905: if ( mod ) ndv_mod(mod,d);
7906: chsgnz(ONE,&mone);
7907: for ( j = 0, sgn = 1; j < n; j++ ) {
7908: if ( DP_Print ) {
7909: fprintf(asir_out,".");
7910: }
7911: for ( i = j; i < n && !dm[i][j]; i++ );
7912: if ( i == n ) {
7913: *rp = 0;
7914: return;
7915: }
7916: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
7917: for ( k = j; k < n; k++ )
7918: for ( l = j; l < n; l++ )
7919: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
7920: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
7921: }
7922: if ( k0 != j ) {
7923: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
7924: sgn = -sgn;
7925: }
7926: if ( l0 != j ) {
7927: for ( k = j; k < n; k++ ) {
7928: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
7929: }
7930: sgn = -sgn;
7931: }
7932: bound = nd_det_compute_bound(dm,n,j);
7933: for ( k = 0; k < nd_nvar; k++ )
7934: if ( bound[k]*2 > nd_mask0 ) break;
7935: if ( k < nd_nvar )
7936: nd_det_reconstruct(dm,n,j,d);
7937:
7938: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
7939: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
7940: mi = dm[i]; mij = mi[j];
7941: if ( mod )
7942: ndv_mul_c(mod,mij,mod-1);
7943: else
7944: ndv_mul_c_q(mij,mone);
7945: for ( k = j+1; k < n; k++ ) {
7946: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
7947: bucket = create_pbucket();
7948: if ( mi[k] ) {
7949: nmv = BDY(mjj); len = LEN(mjj);
7950: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7951: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
7952: add_pbucket(mod,bucket,u);
7953: }
7954: }
7955: if ( mj[k] && mij ) {
7956: nmv = BDY(mij); len = LEN(mij);
7957: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7958: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
7959: add_pbucket(mod,bucket,u);
7960: }
7961: }
7962: u = nd_quo(mod,bucket,d);
7963: mi[k] = ndtondv(mod,u);
7964: }
7965: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
7966: }
7967: d = mjj;
7968: }
7969: if ( DP_Print ) {
7970: fprintf(asir_out,"\n");
7971: }
7972: if ( sgn < 0 ) {
7973: if ( mod )
7974: ndv_mul_c(mod,d,mod-1);
7975: else
7976: ndv_mul_c_q(d,mone);
7977: }
7978: r = ndvtop(mod,CO,fv,d);
7979: if ( !mod && !UNIQ(dq) )
7980: divsp(CO,r,(P)dq,rp);
7981: else
7982: *rp = r;
7983: }
7984:
7985: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
7986: {
7987: NM mr,mr0;
7988: NM tnm;
7989: NMV m;
7990: UINT *d0,*dt,*dm;
7991: int c,n,td,i,c1,c2,len;
7992: Z q;
7993: ND r;
7994:
7995: if ( !p ) return 0;
7996: else {
7997: n = NV(p); m = BDY(p); len = LEN(p);
7998: d0 = DL(m0);
7999: td = TD(d);
8000: mr0 = 0;
8001: NEWNM(tnm);
8002: if ( mod ) {
8003: c = CM(m0);
8004: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8005: ndl_add(DL(m),d0,DL(tnm));
8006: if ( ndl_reducible(DL(tnm),d) ) {
8007: NEXTNM(mr0,mr);
8008: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8009: ndl_copy(DL(tnm),DL(mr));
8010: }
8011: }
8012: } else {
1.6 noro 8013: q = CZ(m0);
1.1 noro 8014: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8015: ndl_add(DL(m),d0,DL(tnm));
8016: if ( ndl_reducible(DL(tnm),d) ) {
8017: NEXTNM(mr0,mr);
1.6 noro 8018: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8019: ndl_copy(DL(tnm),DL(mr));
8020: }
8021: }
8022: }
8023: if ( !mr0 )
8024: return 0;
8025: else {
8026: NEXT(mr) = 0;
8027: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8028: MKND(NV(p),mr0,len,r);
8029: SG(r) = SG(p) + TD(d0);
8030: return r;
8031: }
8032: }
8033: }
8034:
8035: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8036: {
8037: int i,obpe,oadv,h,k,l;
8038: static NM prev_nm_free_list;
8039: EPOS oepos;
8040:
8041: obpe = nd_bpe;
8042: oadv = nmv_adv;
8043: oepos = nd_epos;
8044: if ( obpe < 2 ) nd_bpe = 2;
8045: else if ( obpe < 3 ) nd_bpe = 3;
8046: else if ( obpe < 4 ) nd_bpe = 4;
8047: else if ( obpe < 5 ) nd_bpe = 5;
8048: else if ( obpe < 6 ) nd_bpe = 6;
8049: else if ( obpe < 8 ) nd_bpe = 8;
8050: else if ( obpe < 10 ) nd_bpe = 10;
8051: else if ( obpe < 16 ) nd_bpe = 16;
8052: else if ( obpe < 32 ) nd_bpe = 32;
8053: else error("nd_det_reconstruct : exponent too large");
8054:
8055: nd_setup_parameters(nd_nvar,0);
8056: prev_nm_free_list = _nm_free_list;
8057: _nm_free_list = 0;
8058: for ( k = j; k < n; k++ )
8059: for (l = j; l < n; l++ )
8060: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8061: ndv_realloc(d,obpe,oadv,oepos);
8062: prev_nm_free_list = 0;
8063: #if 0
8064: GC_gcollect();
8065: #endif
8066: }
8067:
8068: /* returns a UINT array containing degree bounds */
8069:
8070: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8071: {
8072: UINT *d0,*d1,*d,*t,*r;
8073: int k,l,i;
8074:
8075: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8076: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8077: for ( k = j; k < n; k++ )
8078: for ( l = j; l < n; l++ )
8079: if ( dm[k][l] ) {
8080: d = ndv_compute_bound(dm[k][l]);
8081: for ( i = 0; i < nd_nvar; i++ )
8082: d0[i] = MAX(d0[i],d[i]);
8083: }
8084: return d0;
8085: }
8086:
8087: DL nd_separate_d(UINT *d,UINT *trans)
8088: {
8089: int n,td,i,e,j;
8090: DL a;
8091:
8092: ndl_zero(trans);
8093: td = 0;
8094: for ( i = 0; i < nd_ntrans; i++ ) {
8095: e = GET_EXP(d,i);
8096: PUT_EXP(trans,i,e);
8097: td += MUL_WEIGHT(e,i);
8098: }
8099: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8100: /* homogenized */
8101: i = nd_nvar-1;
8102: e = GET_EXP(d,i);
8103: PUT_EXP(trans,i,e);
8104: td += MUL_WEIGHT(e,i);
8105: }
8106: TD(trans) = td;
8107: if ( nd_blockmask) ndl_weight_mask(trans);
8108: NEWDL(a,nd_nalg);
8109: td = 0;
8110: for ( i = 0; i < nd_nalg; i++ ) {
8111: j = nd_ntrans+i;
8112: e = GET_EXP(d,j);
8113: a->d[i] = e;
8114: td += e;
8115: }
8116: a->td = td;
8117: return a;
8118: }
8119:
8120: int nd_monic(int mod,ND *p)
8121: {
8122: UINT *trans,*t;
8123: DL alg;
8124: MP mp0,mp;
8125: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8126: ND r;
8127: DL dl;
8128: DP nm;
8129: NDV ndv;
8130: DAlg inv,cd;
8131: ND s,c;
8132: Z l,mul;
8133: Z ln;
8134: int n,ntrans,i,e,td,is_lc,len;
8135: NumberField nf;
8136: struct oEGT eg0,eg1;
8137:
8138: if ( !(nf = get_numberfield()) )
8139: error("nd_monic : current_numberfield is not set");
8140:
8141: /* Q coef -> DAlg coef */
8142: NEWNM(ma0); ma = ma0;
8143: m = BDY(*p);
8144: is_lc = 1;
8145: while ( 1 ) {
8146: NEWMP(mp0); mp = mp0;
1.6 noro 8147: mp->c = (Obj)CZ(m);
1.1 noro 8148: mp->dl = nd_separate_d(DL(m),DL(ma));
8149: NEWNM(mb);
8150: for ( m = NEXT(m); m; m = NEXT(m) ) {
8151: alg = nd_separate_d(DL(m),DL(mb));
8152: if ( !ndl_equal(DL(ma),DL(mb)) )
8153: break;
1.6 noro 8154: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8155: }
8156: NEXT(mp) = 0;
8157: MKDP(nd_nalg,mp0,nm);
8158: MKDAlg(nm,ONE,cd);
8159: if ( is_lc == 1 ) {
8160: /* if the lc is a rational number, we have nothing to do */
8161: if ( !mp0->dl->td )
8162: return 1;
8163:
8164: get_eg(&eg0);
8165: invdalg(cd,&inv);
8166: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8167: /* check the validity of inv */
8168: if ( mod && !remqi((Q)inv->dn,mod) )
8169: return 0;
8170: CA(ma) = nf->one;
8171: is_lc = 0;
8172: ln = ONE;
8173: } else {
8174: muldalg(cd,inv,&CA(ma));
8175: lcmz(ln,CA(ma)->dn,&ln);
8176: }
8177: if ( m ) {
8178: NEXT(ma) = mb; ma = mb;
8179: } else {
8180: NEXT(ma) = 0;
8181: break;
8182: }
8183: }
8184: /* l = lcm(denoms) */
8185: l = ln;
8186: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8187: divsz(l,CA(m)->dn,&mul);
1.1 noro 8188: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8189: NEXTNM(mr0,mr);
1.6 noro 8190: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8191: dl = mp->dl;
8192: td = TD(DL(m));
8193: ndl_copy(DL(m),DL(mr));
8194: for ( i = 0; i < nd_nalg; i++ ) {
8195: e = dl->d[i];
8196: PUT_EXP(DL(mr),i+nd_ntrans,e);
8197: td += MUL_WEIGHT(e,i+nd_ntrans);
8198: }
8199: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8200: TD(DL(mr)) = td;
8201: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8202: }
8203: }
8204: NEXT(mr) = 0;
8205: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8206: MKND(NV(*p),mr0,len,r);
8207: /* XXX */
8208: SG(r) = SG(*p);
8209: nd_free(*p);
8210: *p = r;
8211: return 1;
8212: }
8213:
8214: NODE reverse_node(NODE n)
8215: {
8216: NODE t,t1;
8217:
8218: for ( t = 0; n; n = NEXT(n) ) {
8219: MKNODE(t1,BDY(n),t); t = t1;
8220: }
8221: return t;
8222: }
8223:
8224: P ndc_div(int mod,union oNDC a,union oNDC b)
8225: {
8226: union oNDC c;
8227: int inv,t;
8228:
8229: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8230: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8231: else if ( mod ) {
8232: inv = invm(b.m,mod);
8233: DMAR(a.m,inv,0,mod,t); c.m = t;
8234: } else if ( nd_vc )
8235: divsp(nd_vc,a.p,b.p,&c.p);
8236: else
8237: divsz(a.z,b.z,&c.z);
8238: return ndctop(mod,c);
8239: }
8240:
8241: P ndctop(int mod,union oNDC c)
8242: {
8243: Z q;
8244: int e;
8245: GFS gfs;
8246:
8247: if ( mod == -1 ) {
8248: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8249: } else if ( mod == -2 ) {
1.10 noro 8250: q = c.z; return (P)q;
1.1 noro 8251: } else if ( mod > 0 ) {
1.6 noro 8252: STOZ(c.m,q); return (P)q;
1.1 noro 8253: } else
8254: return (P)c.p;
8255: }
8256:
8257: /* [0,0,0,cont] = p -> p/cont */
8258:
8259: void finalize_tracelist(int i,P cont)
8260: {
8261: LIST l;
8262: NODE node;
8263: Z iq;
8264:
8265: if ( !UNIQ(cont) ) {
8266: node = mknode(4,NULLP,NULLP,NULLP,cont);
8267: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8268: nd_tracelist = node;
8269: }
1.6 noro 8270: STOZ(i,iq);
1.1 noro 8271: nd_tracelist = reverse_node(nd_tracelist);
8272: MKLIST(l,nd_tracelist);
8273: node = mknode(2,iq,l); MKLIST(l,node);
8274: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8275: nd_alltracelist = node; nd_tracelist = 0;
8276: }
8277:
8278: void conv_ilist(int demand,int trace,NODE g,int **indp)
8279: {
8280: int n,i,j;
8281: int *ind;
8282: NODE t;
8283:
8284: n = length(g);
8285: ind = (int *)MALLOC(n*sizeof(int));
8286: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8287: j = (long)BDY(t); ind[i] = j;
8288: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8289: }
8290: if ( indp ) *indp = ind;
8291: }
8292:
8293: void parse_nd_option(NODE opt)
8294: {
8295: NODE t,p,u;
8296: int i,s,n;
8297: char *key;
8298: Obj value;
8299:
8300: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8301: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8302: nd_splist = 0; nd_check_splist = 0;
8303: nd_sugarweight = 0;
8304: nd_f4red =0;
8305: nd_rank0 = 0;
8306: for ( t = opt; t; t = NEXT(t) ) {
8307: p = BDY((LIST)BDY(t));
8308: key = BDY((STRING)BDY(p));
8309: value = (Obj)BDY(NEXT(p));
8310: if ( !strcmp(key,"gentrace") )
8311: nd_gentrace = value?1:0;
8312: else if ( !strcmp(key,"gensyz") )
8313: nd_gensyz = value?1:0;
8314: else if ( !strcmp(key,"nora") )
8315: nd_nora = value?1:0;
8316: else if ( !strcmp(key,"gbblock") ) {
8317: if ( value && OID(value) == O_LIST ) {
8318: u = BDY((LIST)value);
8319: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8320: for ( i = 0; u; u = NEXT(u) ) {
8321: p = BDY((LIST)BDY(u));
1.6 noro 8322: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8323: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8324: }
8325: nd_gbblock[i] = -1;
8326: } else
8327: nd_gbblock = 0;
8328: } else if ( !strcmp(key,"newelim") )
8329: nd_newelim = value?1:0;
8330: else if ( !strcmp(key,"intersect") )
8331: nd_intersect = value?1:0;
8332: else if ( !strcmp(key,"lf") )
8333: nd_lf = value?1:0;
8334: else if ( !strcmp(key,"trace") ) {
8335: if ( value ) {
8336: u = BDY((LIST)value);
8337: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8338: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8339: }
8340: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8341: nd_f4red = ZTOS((Q)value);
1.1 noro 8342: } else if ( !strcmp(key,"rank0") ) {
8343: nd_rank0 = value?1:0;
8344: } else if ( !strcmp(key,"splist") ) {
8345: nd_splist = value?1:0;
8346: } else if ( !strcmp(key,"check_splist") ) {
8347: nd_check_splist = BDY((LIST)value);
8348: } else if ( !strcmp(key,"sugarweight") ) {
8349: u = BDY((LIST)value);
8350: n = length(u);
8351: nd_sugarweight = MALLOC(n*sizeof(int));
8352: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8353: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8354: }
8355: }
8356: }
8357:
8358: ND mdptond(DP d);
8359: ND nd_mul_nm(int mod,NM m0,ND p);
8360: ND nd_mul_nm_lf(NM m0,ND p);
8361: ND *btog(NODE ti,ND **p,int nb,int mod);
8362: ND btog_one(NODE ti,ND *p,int nb,int mod);
8363: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8364: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8365:
8366: /* d:monomial */
8367: ND mdptond(DP d)
8368: {
8369: NM m;
8370: ND r;
8371:
8372: if ( OID(d) == 1 )
8373: r = ptond(CO,CO,(P)d);
8374: else {
8375: NEWNM(m);
8376: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8377: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8378: NEXT(m) = 0;
8379: MKND(NV(d),m,1,r);
8380: }
8381: return r;
8382: }
8383:
8384: ND nd_mul_nm(int mod,NM m0,ND p)
8385: {
8386: UINT *d0;
8387: int c0,c1,c;
8388: NM tm,mr,mr0;
8389: ND r;
8390:
8391: if ( !p ) return 0;
8392: d0 = DL(m0);
8393: c0 = CM(m0);
8394: mr0 = 0;
8395: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8396: NEXTNM(mr0,mr);
8397: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8398: ndl_add(d0,DL(tm),DL(mr));
8399: }
8400: NEXT(mr) = 0;
8401: MKND(NV(p),mr0,LEN(p),r);
8402: return r;
8403: }
8404:
8405: ND nd_mul_nm_lf(NM m0,ND p)
8406: {
8407: UINT *d0;
8408: Z c0,c1,c;
8409: NM tm,mr,mr0;
8410: ND r;
8411:
8412: if ( !p ) return 0;
8413: d0 = DL(m0);
8414: c0 = CZ(m0);
8415: mr0 = 0;
8416: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8417: NEXTNM(mr0,mr);
8418: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8419: ndl_add(d0,DL(tm),DL(mr));
8420: }
8421: NEXT(mr) = 0;
8422: MKND(NV(p),mr0,LEN(p),r);
8423: return r;
8424: }
8425:
8426: ND *btog(NODE ti,ND **p,int nb,int mod)
8427: {
8428: PGeoBucket *r;
8429: int i,ci;
8430: NODE t,s;
8431: ND m,tp;
8432: ND *pi,*rd;
8433: P c;
8434:
8435: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8436: for ( i = 0; i < nb; i++ )
8437: r[i] = create_pbucket();
8438: for ( t = ti; t; t = NEXT(t) ) {
8439: s = BDY((LIST)BDY(t));
8440: if ( ARG0(s) ) {
8441: m = mdptond((DP)ARG2(s));
1.6 noro 8442: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8443: if ( (ci = ((MQ)c)->cont) != 0 ) {
8444: HCM(m) = ci;
1.6 noro 8445: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8446: for ( i = 0; i < nb; i++ ) {
8447: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8448: add_pbucket(mod,r[i],tp);
8449: }
8450: }
8451: ci = 1;
8452: } else {
8453: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8454: ci = invm(ci,mod);
8455: }
8456: }
8457: rd = (ND *)MALLOC(nb*sizeof(ND));
8458: for ( i = 0; i < nb; i++ )
8459: rd[i] = normalize_pbucket(mod,r[i]);
8460: if ( ci != 1 )
8461: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8462: return rd;
8463: }
8464:
8465: /* YYY */
8466: ND *btog_lf(NODE ti,ND **p,int nb)
8467: {
8468: PGeoBucket *r;
8469: int i;
8470: NODE t,s;
8471: ND m,tp;
8472: ND *pi,*rd;
8473: LM lm;
8474: Z lf,c;
8475:
8476: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8477: for ( i = 0; i < nb; i++ )
8478: r[i] = create_pbucket();
8479: for ( t = ti; t; t = NEXT(t) ) {
8480: s = BDY((LIST)BDY(t));
8481: if ( ARG0(s) ) {
8482: m = mdptond((DP)ARG2(s));
1.6 noro 8483: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8484: if ( lm ) {
8485: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8486: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8487: for ( i = 0; i < nb; i++ ) {
8488: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8489: add_pbucket(-2,r[i],tp);
8490: }
8491: }
8492: c = ONE;
8493: } else {
8494: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8495: }
8496: }
8497: rd = (ND *)MALLOC(nb*sizeof(ND));
8498: for ( i = 0; i < nb; i++ )
8499: rd[i] = normalize_pbucket(-2,r[i]);
8500: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8501: return rd;
8502: }
8503:
8504: ND btog_one(NODE ti,ND *p,int nb,int mod)
8505: {
8506: PGeoBucket r;
8507: int i,ci,j;
8508: NODE t,s;
8509: ND m,tp;
8510: ND pi,rd;
8511: P c;
8512:
8513: r = create_pbucket();
8514: for ( t = ti; t; t = NEXT(t) ) {
8515: s = BDY((LIST)BDY(t));
8516: if ( ARG0(s) ) {
8517: m = mdptond((DP)ARG2(s));
1.6 noro 8518: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8519: if ( (ci = ((MQ)c)->cont) != 0 ) {
8520: HCM(m) = ci;
1.6 noro 8521: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8522: if ( !pi ) {
8523: pi = nd_load_mod(j);
8524: tp = nd_mul_nm(mod,BDY(m),pi);
8525: nd_free(pi);
8526: add_pbucket(mod,r,tp);
8527: } else {
8528: tp = nd_mul_nm(mod,BDY(m),pi);
8529: add_pbucket(mod,r,tp);
8530: }
8531: }
8532: ci = 1;
8533: } else {
8534: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8535: ci = invm(ci,mod);
8536: }
8537: }
8538: rd = normalize_pbucket(mod,r);
8539: free_pbucket(r);
8540: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8541: return rd;
8542: }
8543:
8544: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8545:
8546: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8547: {
8548: int i,j,n,m,nb,pi0,pi1,nvar;
8549: VL fv,tv,vv;
8550: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8551: ND **p;
8552: ND *c;
8553: ND u;
8554: P inv;
8555: MAT mat;
8556:
8557: if ( mod == -2 )
8558: return nd_btog_lf(f,v,ord,tlist,rp);
8559:
8560: parse_nd_option(current_option);
8561: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8562: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8563: switch ( ord->id ) {
8564: case 1:
8565: if ( ord->nv != nvar )
8566: error("nd_check : invalid order specification");
8567: break;
8568: default:
8569: break;
8570: }
8571: nd_init_ord(ord);
8572: #if 0
1.6 noro 8573: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8574: #else
8575: nd_bpe = 32;
8576: #endif
8577: nd_setup_parameters(nvar,0);
8578: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8579: intred = BDY((LIST)ARG3(BDY(tlist)));
8580: ind = BDY((LIST)ARG4(BDY(tlist)));
8581: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8582: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8583: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8584: if ( j > i ) i = j;
8585: }
8586: n = i+1;
8587: nb = length(BDY(f));
8588: p = (ND **)MALLOC(n*sizeof(ND *));
8589: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8590: pi = BDY((LIST)BDY(t));
1.6 noro 8591: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8592: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8593: ptomp(mod,(P)ARG2(pi),&inv);
8594: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8595: u = ptond(CO,vv,(P)ONE);
8596: HCM(u) = ((MQ)inv)->cont;
8597: c[pi1] = u;
8598: }
8599: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8600: printf("%d ",i); fflush(stdout);
8601: ti = BDY((LIST)BDY(t));
1.6 noro 8602: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8603: }
8604: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8605: printf("%d ",i); fflush(stdout);
8606: ti = BDY((LIST)BDY(t));
1.6 noro 8607: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8608: }
8609: m = length(ind);
8610: MKMAT(mat,nb,m);
8611: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8612: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8613: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8614: return mat;
8615: }
8616:
8617: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8618: {
8619: int i,j,n,m,nb,pi0,pi1,nvar;
8620: VL fv,tv,vv;
8621: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8622: ND **p;
8623: ND *c;
8624: ND u;
8625: MAT mat;
8626: LM lm;
8627: Z lf,inv;
8628:
8629: parse_nd_option(current_option);
8630: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8631: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8632: switch ( ord->id ) {
8633: case 1:
8634: if ( ord->nv != nvar )
8635: error("nd_check : invalid order specification");
8636: break;
8637: default:
8638: break;
8639: }
8640: nd_init_ord(ord);
8641: #if 0
1.6 noro 8642: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8643: #else
8644: nd_bpe = 32;
8645: #endif
8646: nd_setup_parameters(nvar,0);
8647: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8648: intred = BDY((LIST)ARG3(BDY(tlist)));
8649: ind = BDY((LIST)ARG4(BDY(tlist)));
8650: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8651: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8652: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8653: if ( j > i ) i = j;
8654: }
8655: n = i+1;
8656: nb = length(BDY(f));
8657: p = (ND **)MALLOC(n*sizeof(ND *));
8658: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8659: pi = BDY((LIST)BDY(t));
1.6 noro 8660: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8661: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8662: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8663: u = ptond(CO,vv,(P)ONE);
8664: HCZ(u) = inv;
8665: c[pi1] = u;
8666: }
8667: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8668: printf("%d ",i); fflush(stdout);
8669: ti = BDY((LIST)BDY(t));
1.6 noro 8670: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8671: }
8672: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8673: printf("%d ",i); fflush(stdout);
8674: ti = BDY((LIST)BDY(t));
1.6 noro 8675: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8676: }
8677: m = length(ind);
8678: MKMAT(mat,nb,m);
8679: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8680: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8681: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8682: return mat;
8683: }
8684:
8685: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8686: LIST tlist,int pos,MAT *rp)
8687: {
8688: int i,j,n,m,nb,pi0,pi1,nvar;
8689: VL fv,tv,vv;
8690: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8691: ND *p;
8692: ND *c;
8693: ND u;
8694: P inv;
8695: VECT vect;
8696:
8697: if ( mod == -2 )
8698: error("nd_btog_one : not implemented yet for a large finite field");
8699:
8700: parse_nd_option(current_option);
8701: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8702: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8703: switch ( ord->id ) {
8704: case 1:
8705: if ( ord->nv != nvar )
8706: error("nd_check : invalid order specification");
8707: break;
8708: default:
8709: break;
8710: }
8711: nd_init_ord(ord);
8712: #if 0
1.6 noro 8713: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8714: #else
8715: nd_bpe = 32;
8716: #endif
8717: nd_setup_parameters(nvar,0);
8718: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8719: intred = BDY((LIST)ARG3(BDY(tlist)));
8720: ind = BDY((LIST)ARG4(BDY(tlist)));
8721: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8722: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8723: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8724: if ( j > i ) i = j;
8725: }
8726: n = i+1;
8727: nb = length(BDY(f));
8728: p = (ND *)MALLOC(n*sizeof(ND *));
8729: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8730: pi = BDY((LIST)BDY(t));
1.6 noro 8731: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8732: if ( pi1 == pos ) {
8733: ptomp(mod,(P)ARG2(pi),&inv);
8734: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8735: u = ptond(CO,vv,(P)ONE);
8736: HCM(u) = ((MQ)inv)->cont;
8737: p[pi0] = u;
8738: }
8739: }
8740: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8741: printf("%d ",i); fflush(stdout);
8742: ti = BDY((LIST)BDY(t));
1.6 noro 8743: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8744: if ( Demand ) {
8745: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8746: }
8747: }
8748: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8749: printf("%d ",i); fflush(stdout);
8750: ti = BDY((LIST)BDY(t));
1.6 noro 8751: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8752: if ( Demand ) {
8753: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8754: }
8755: }
8756: m = length(ind);
8757: MKVECT(vect,m);
8758: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8759: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8760: if ( !u ) {
1.6 noro 8761: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8762: BDY(vect)[j] = ndtodp(mod,u);
8763: nd_free(u);
8764: } else
8765: BDY(vect)[j] = ndtodp(mod,u);
8766: }
8767: return vect;
8768: }
8769:
8770: void ndv_print_lf(NDV p)
8771: {
8772: NMV m;
8773: int i,len;
8774:
8775: if ( !p ) printf("0\n");
8776: else {
8777: len = LEN(p);
8778: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8779: printf("+");
8780: mpz_out_str(asir_out,10,BDY(CZ(m)));
8781: printf("*");
8782: ndl_print(DL(m));
8783: }
8784: printf("\n");
8785: }
8786: }
8787:
8788: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8789: {
8790: VL tv,fv,vv,vc,av;
8791: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8792: int m,nocheck,nvar,mindex,e,max;
8793: NDV c;
8794: NMV a;
8795: P p,zp;
8796: Q dmy;
8797: EPOS oepos;
8798: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8799: Alg alpha,dp;
8800: P poly;
8801: LIST f1,f2,zpl;
8802: Obj obj;
8803: NumberField nf;
8804: struct order_spec *ord1;
8805: struct oEGT eg_check,eg0,eg1;
8806: NODE tr,tl1,tl2,tl3,tl4;
8807: LIST l1,l2,l3,l4,l5;
8808: int *perm;
8809: int j,ret;
8810: NODE retn;
8811: Q jq,bpe;
8812:
8813: nd_module = 0;
8814: parse_nd_option(current_option);
8815: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8816: if ( nd_vc )
8817: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8818: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8819: switch ( ord->id ) {
8820: case 1:
8821: if ( ord->nv != nvar )
8822: error("nd_f4_lf_trace : invalid order specification");
8823: break;
8824: default:
8825: break;
8826: }
8827:
8828: nd_ntrans = nvar;
8829: nd_nalg = 0;
8830:
8831: nocheck = 0;
8832: mindex = 0;
8833:
8834: /* do not use on-demand load/save */
8835: nd_demand = 0;
8836: m = trace > 1 ? trace : get_lprime(mindex);
8837: nd_init_ord(ord);
8838: mrank = 0;
8839: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8840: for ( tv = vv; tv; tv = NEXT(tv) ) {
8841: if ( nd_module ) {
8842: s = BDY((LIST)BDY(t));
8843: trank = length(s);
8844: mrank = MAX(mrank,trank);
8845: for ( ; s; s = NEXT(s) ) {
8846: e = getdeg(tv->v,(P)BDY(s));
8847: max = MAX(e,max);
8848: }
8849: } else {
8850: e = getdeg(tv->v,(P)BDY(t));
8851: max = MAX(e,max);
8852: }
8853: }
8854: nd_setup_parameters(nvar,max);
8855: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8856: ishomo = 1;
8857: /* XXX */
8858: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8859: if ( nd_module ) {
8860: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8861: } else {
8862: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8863: }
8864: if ( ishomo )
8865: ishomo = ishomo && ndv_ishomo(c);
8866: if ( c ) {
8867: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
8868: ndv_mod(-2,c);
8869: NEXTNODE(in0,in); BDY(in) = (pointer)c;
8870: }
8871: }
8872: if ( in0 ) NEXT(in) = 0;
8873: if ( fd0 ) NEXT(fd) = 0;
8874: if ( !ishomo && homo ) {
8875: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
8876: c = (NDV)BDY(t); len = LEN(c);
8877: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
8878: wmax = MAX(TD(DL(a)),wmax);
8879: }
8880: homogenize_order(ord,nvar,&ord1);
8881: nd_init_ord(ord1);
8882: nd_setup_parameters(nvar+1,wmax);
8883: for ( t = fd0; t; t = NEXT(t) )
8884: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
8885: }
8886: if ( MaxDeg > 0 ) nocheck = 1;
8887: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
8888: if ( ret )
8889: cand = nd_f4_lf_trace_main(m,&perm);
8890: if ( !ret || !cand ) {
8891: *rp = 0; return;
8892: }
8893: if ( !ishomo && homo ) {
8894: /* dehomogenization */
8895: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
8896: nd_init_ord(ord);
8897: nd_setup_parameters(nvar,0);
8898: }
8899: cand = ndv_reducebase(cand,perm);
8900: cand = ndv_reduceall(-2,cand);
8901: cbpe = nd_bpe;
8902: get_eg(&eg0);
8903: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
8904: /* gbcheck : cand is a GB of Id(cand) ? */
8905: retn = nd_f4(-2,0,0);
8906: }
8907: if ( !retn ) {
8908: /* failure */
8909: *rp = 0; return;
8910: }
8911: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
8912: if ( DP_Print )
1.5 noro 8913: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 8914: /* dp->p */
8915: nd_bpe = cbpe;
8916: nd_setup_parameters(nd_nvar,0);
8917: for ( r = cand; r; r = NEXT(r) ) {
8918: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
8919: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
8920: }
8921: MKLIST(*rp,cand);
8922: }
8923:
8924: NODE nd_f4_lf_trace_main(int m,int **indp)
8925: {
8926: int i,nh,stat,index;
8927: NODE r,rm,g;
8928: ND_pairs d,l,l0,t;
8929: ND spol,red;
8930: NDV nf,redv,nfqv,nfv;
8931: NM s0,s;
8932: NODE rp0,srp0,nflist,nflist_lf;
8933: int nsp,nred,col,rank,len,k,j,a;
8934: UINT c;
8935: UINT **spmat;
8936: UINT *s0vect,*svect,*p,*v;
8937: int *colstat;
8938: IndArray *imat;
8939: int *rhead;
8940: int spcol,sprow;
8941: int sugar;
8942: PGeoBucket bucket;
8943: struct oEGT eg0,eg1,eg_f4;
8944:
8945: g = 0; d = 0;
8946: for ( i = 0; i < nd_psn; i++ ) {
8947: d = update_pairs(d,g,i,0);
8948: g = update_base(g,i);
8949: }
8950: while ( d ) {
8951: get_eg(&eg0);
8952: l = nd_minsugarp(d,&d);
8953: sugar = SG(l);
8954: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8955: bucket = create_pbucket();
8956: stat = nd_sp_f4(m,0,l,bucket);
8957: if ( !stat ) {
8958: for ( t = l; NEXT(t); t = NEXT(t) );
8959: NEXT(t) = d; d = l;
8960: d = nd_reconstruct(1,d);
8961: continue;
8962: }
8963: if ( bucket->m < 0 ) continue;
8964: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8965: if ( !col ) {
8966: for ( t = l; NEXT(t); t = NEXT(t) );
8967: NEXT(t) = d; d = l;
8968: d = nd_reconstruct(1,d);
8969: continue;
8970: }
8971: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8972: if ( DP_Print )
1.5 noro 8973: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 8974: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8975: if ( !l0 ) continue;
8976: l = l0;
8977:
8978: /* over LF */
8979: bucket = create_pbucket();
8980: stat = nd_sp_f4(-2,1,l,bucket);
8981: if ( !stat ) {
8982: for ( t = l; NEXT(t); t = NEXT(t) );
8983: NEXT(t) = d; d = l;
8984: d = nd_reconstruct(1,d);
8985: continue;
8986: }
8987: if ( bucket->m < 0 ) continue;
8988: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8989: if ( !col ) {
8990: for ( t = l; NEXT(t); t = NEXT(t) );
8991: NEXT(t) = d; d = l;
8992: d = nd_reconstruct(1,d);
8993: continue;
8994: }
8995: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
8996: /* adding new bases */
8997: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
8998: nfv = (NDV)BDY(rm);
8999: nfqv = (NDV)BDY(r);
9000: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9001: ndv_removecont(m,nfv);
9002: ndv_removecont(-2,nfqv);
9003: nh = ndv_newps(-2,nfv,nfqv,1);
9004: d = update_pairs(d,g,nh,0);
9005: g = update_base(g,nh);
9006: }
9007: if ( r || rm ) return 0;
9008: }
9009: conv_ilist(nd_demand,1,g,indp);
9010: return g;
9011: }
9012:
1.7 noro 9013: #if SIZEOF_LONG==8
9014:
9015: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9016: {
9017: int j,k,len;
9018: UINT *p;
9019: UINT c;
9020: NDV r;
9021: NMV mr0,mr;
9022:
9023: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9024: if ( !len ) return 0;
9025: else {
9026: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9027: #if 0
9028: ndv_alloc += nmv_adv*len;
9029: #endif
9030: mr = mr0;
9031: p = s0vect;
9032: for ( j = k = 0; j < col; j++, p += nd_wpd )
9033: if ( !rhead[j] ) {
9034: if ( (c = (UINT)vect[k++]) != 0 ) {
9035: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9036: }
9037: }
9038: MKNDV(nd_nvar,mr0,len,r);
9039: return r;
9040: }
9041: }
9042:
9043: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9044: {
9045: NM m;
1.11 noro 9046: UINT *t,*s,*u;
9047: int i,st,ed,md,prev,c;
1.7 noro 9048:
9049: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9050: prev = 0;
9051: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9052: t = DL(m);
9053: st = prev;
9054: ed = n;
9055: while ( ed > st ) {
9056: md = (st+ed)/2;
9057: u = s0+md*nd_wpd;
9058: c = DL_COMPARE(u,t);
9059: if ( c == 0 ) break;
9060: else if ( c > 0 ) st = md;
9061: else ed = md;
9062: }
9063: r[md] = (mp_limb_t)CM(m);
9064: prev = md;
1.7 noro 9065: }
9066: for ( i = 0; !r[i]; i++ );
9067: return i;
9068: }
9069:
9070: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9071:
9072: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9073: {
9074: int i,j,k,len,pos,prev;
9075: mp_limb_t a,c,c1,c2;
9076: IndArray ivect;
9077: unsigned char *ivc;
9078: unsigned short *ivs;
9079: unsigned int *ivi;
9080: NDV redv;
9081: NMV mr;
9082: NODE rp;
9083: int maxrs;
9084:
9085: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9086: maxrs = 0;
9087: for ( i = 0; i < nred; i++ ) {
9088: ivect = imat[i];
9089: k = ivect->head;
9090: a = svect[k]; c = cvect[k];
9091: MOD128(a,c,m);
9092: svect[k] = a; cvect[k] = 0;
9093: if ( (c = svect[k]) != 0 ) {
1.11 noro 9094: Nf4_red++;
1.7 noro 9095: maxrs = MAX(maxrs,rp0[i]->sugar);
9096: c = m-c; redv = nd_ps[rp0[i]->index];
9097: len = LEN(redv); mr = BDY(redv);
9098: svect[k] = 0; prev = k;
9099: switch ( ivect->width ) {
9100: case 1:
9101: ivc = ivect->index.c;
9102: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9103: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 9104: c2 = svect[pos]+c1*c;
9105: if ( c2 < svect[pos] ) cvect[pos]++;
9106: svect[pos] = c2;
1.7 noro 9107: }
9108: break;
9109: case 2:
9110: ivs = ivect->index.s;
9111: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9112: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 9113: c2 = svect[pos]+c1*c;
9114: if ( c2 < svect[pos] ) cvect[pos]++;
9115: svect[pos] = c2;
1.7 noro 9116: }
9117: break;
9118: case 4:
9119: ivi = ivect->index.i;
9120: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9121: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 9122: c2 = svect[pos]+c1*c;
9123: if ( c2 < svect[pos] ) cvect[pos]++;
9124: svect[pos] = c2;
1.7 noro 9125: }
9126: break;
9127: }
9128: }
9129: }
9130: for ( i = 0; i < col; i++ ) {
9131: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9132: }
9133: return maxrs;
9134: }
9135:
9136: /* for Fp, 2^15=<p<2^29 */
9137:
9138: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9139: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9140: {
9141: int spcol,sprow,a;
9142: int i,j,k,l,rank;
9143: NODE r0,r;
9144: ND_pairs sp;
9145: ND spol;
9146: mp_limb_t **spmat;
9147: mp_limb_t *svect,*cvect;
9148: mp_limb_t *v;
9149: int *colstat;
9150: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9151: int maxrs;
9152: int *spsugar;
9153: ND_pairs *spactive;
9154:
9155: spcol = col-nred;
9156: get_eg(&eg0);
9157: /* elimination (1st step) */
9158: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9159: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9160: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9161: spsugar = (int *)MALLOC(nsp*sizeof(int));
9162: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9163: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9164: nd_sp(m,0,sp,&spol);
9165: if ( !spol ) continue;
9166: nd_to_vect64(m,s0vect,col,spol,svect);
9167: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9168: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9169: if ( i < col ) {
9170: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9171: for ( j = k = 0; j < col; j++ )
9172: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9173: spsugar[sprow] = MAX(maxrs,SG(spol));
9174: if ( nz )
9175: spactive[sprow] = sp;
9176: sprow++;
9177: }
9178: nd_free(spol);
9179: }
1.12 noro 9180: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9181: if ( DP_Print ) {
9182: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9183: fflush(asir_out);
9184: }
9185: /* free index arrays */
9186: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9187:
9188: /* elimination (2nd step) */
9189: colstat = (int *)MALLOC(spcol*sizeof(int));
9190: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9191: r0 = 0;
9192: for ( i = 0; i < rank; i++ ) {
9193: NEXTNODE(r0,r); BDY(r) =
9194: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9195: SG((NDV)BDY(r)) = spsugar[i];
9196: GCFREE(spmat[i]);
9197: }
9198: if ( r0 ) NEXT(r) = 0;
9199:
9200: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 9201: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9202: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9203: if ( DP_Print ) {
9204: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9205: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9206: nsp,nred,sprow,spcol,rank);
9207: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9208: }
9209: if ( nz ) {
9210: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9211: if ( rank > 0 ) {
9212: NEXT(spactive[rank-1]) = 0;
9213: *nz = spactive[0];
9214: } else
9215: *nz = 0;
9216: }
9217: return r0;
9218: }
9219:
9220: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9221: {
9222: int i,j,k,l,rank,s;
9223: mp_limb_t inv;
9224: mp_limb_t a;
9225: UINT c;
9226: mp_limb_t *t,*pivot,*pk;
9227: UINT *ck;
9228: UINT **cmat;
9229: UINT *ct;
9230: ND_pairs pair;
9231:
9232: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9233: for ( i = 0; i < row; i++ ) {
9234: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9235: bzero(cmat[i],col*sizeof(UINT));
9236: }
9237:
9238: for ( rank = 0, j = 0; j < col; j++ ) {
9239: for ( i = rank; i < row; i++ ) {
9240: a = mat[i][j]; c = cmat[i][j];
9241: MOD128(a,c,md);
9242: mat[i][j] = a; cmat[i][j] = 0;
9243: }
9244: for ( i = rank; i < row; i++ )
9245: if ( mat[i][j] )
9246: break;
9247: if ( i == row ) {
9248: colstat[j] = 0;
9249: continue;
9250: } else
9251: colstat[j] = 1;
9252: if ( i != rank ) {
9253: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9254: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9255: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9256: if ( spactive ) {
9257: pair = spactive[i]; spactive[i] = spactive[rank];
9258: spactive[rank] = pair;
9259: }
9260: }
9261: /* column j is normalized */
9262: s = sugar[rank];
9263: inv = invm((UINT)mat[rank][j],md);
9264: /* normalize pivot row */
9265: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9266: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9267: }
9268: for ( i = rank+1; i < row; i++ ) {
9269: if ( (a = mat[i][j]) != 0 ) {
9270: sugar[i] = MAX(sugar[i],s);
9271: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9272: Nf4_red++;
1.7 noro 9273: }
9274: }
9275: rank++;
9276: }
9277: for ( j = col-1, l = rank-1; j >= 0; j-- )
9278: if ( colstat[j] ) {
9279: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9280: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9281: }
9282: s = sugar[l];
9283: for ( i = 0; i < l; i++ ) {
9284: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9285: if ( a ) {
9286: sugar[i] = MAX(sugar[i],s);
9287: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9288: Nf4_red++;
1.7 noro 9289: }
9290: }
9291: l--;
9292: }
9293: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9294: GCFREE(cmat);
9295: return rank;
9296: }
9297: #endif
9298:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>