Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.12
1.12 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.11 2018/10/23 04:53:37 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;
4238: mpz_t *q,*r;
4239:
4240: for ( i = 0; i < n; i++ )
4241: if ( mpz_sgn(c[i]) ) break;
4242: if ( i == n ) return;
4243: gcdv_mpz_estimate(d0,c,n);
4244: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4245: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4246: for ( i = 0; i < n; i++ ) {
4247: mpz_init(q[i]); mpz_init(r[i]);
4248: mpz_fdiv_qr(q[i],r[i],c[i],d0);
4249: }
4250: for ( i = 0; i < n; i++ )
4251: if ( mpz_sgn(r[i]) ) break;
4252: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
4253: if ( i < n ) {
4254: mpz_gcd(gcd,d0,r[i]);
4255: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
4256: mpz_div(a,d0,gcd);
4257: for ( i = 0; i < n; i++ ) {
4258: mpz_mul(u,a,q[i]);
4259: if ( mpz_sgn(r[i]) ) {
4260: mpz_div(u1,r[i],gcd);
4261: mpz_add(q[i],u,u1);
4262: } else
4263: mpz_set(q[i],u);
4264: }
4265: }
4266: for ( i = 0; i < n; i++ )
4267: mpz_set(c[i],q[i]);
4268: }
4269:
1.1 noro 4270: void nd_mul_c(int mod,ND p,int mul)
4271: {
4272: NM m;
4273: int c,c1;
4274:
4275: if ( !p ) return;
4276: if ( mul == 1 ) return;
4277: if ( mod == -1 )
4278: for ( m = BDY(p); m; m = NEXT(m) )
4279: CM(m) = _mulsf(CM(m),mul);
4280: else
4281: for ( m = BDY(p); m; m = NEXT(m) ) {
4282: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4283: }
4284: }
4285:
4286: void nd_mul_c_lf(ND p,Z mul)
4287: {
4288: NM m;
4289: Z c;
4290:
4291: if ( !p ) return;
4292: if ( UNIZ(mul) ) return;
4293: for ( m = BDY(p); m; m = NEXT(m) ) {
4294: mullf(CZ(m),mul,&c); CZ(m) = c;
4295: }
4296: }
4297:
4298: void nd_mul_c_q(ND p,P mul)
4299: {
4300: NM m;
4301: P c;
4302:
4303: if ( !p ) return;
4304: if ( UNIQ(mul) ) return;
4305: for ( m = BDY(p); m; m = NEXT(m) ) {
4306: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4307: }
4308: }
4309:
4310: void nd_mul_c_p(VL vl,ND p,P mul)
4311: {
4312: NM m;
4313: P c;
4314:
4315: if ( !p ) return;
4316: for ( m = BDY(p); m; m = NEXT(m) ) {
4317: mulp(vl,CP(m),mul,&c); CP(m) = c;
4318: }
4319: }
4320:
4321: void nd_free(ND p)
4322: {
4323: NM t,s;
4324:
4325: if ( !p ) return;
4326: t = BDY(p);
4327: while ( t ) {
4328: s = NEXT(t);
4329: FREENM(t);
4330: t = s;
4331: }
4332: FREEND(p);
4333: }
4334:
4335: void ndv_free(NDV p)
4336: {
4337: GCFREE(BDY(p));
4338: }
4339:
4340: void nd_append_red(UINT *d,int i)
4341: {
4342: RHist m,m0;
4343: int h;
4344:
4345: NEWRHist(m);
4346: h = ndl_hash_value(d);
4347: m->index = i;
4348: ndl_copy(d,DL(m));
4349: NEXT(m) = nd_red[h];
4350: nd_red[h] = m;
4351: }
4352:
4353: UINT *ndv_compute_bound(NDV p)
4354: {
4355: UINT *d1,*d2,*t;
4356: UINT u;
4357: int i,j,k,l,len,ind;
4358: NMV m;
4359:
4360: if ( !p )
4361: return 0;
4362: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4363: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4364: len = LEN(p);
4365: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4366: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4367: ndl_max(DL(m),d1,d2);
4368: t = d1; d1 = d2; d2 = t;
4369: }
4370: l = nd_nvar+31;
4371: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4372: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4373: u = d1[i];
4374: k = (nd_epw-1)*nd_bpe;
4375: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4376: t[ind] = (u>>k)&nd_mask0;
4377: }
4378: for ( ; ind < l; ind++ ) t[ind] = 0;
4379: return t;
4380: }
4381:
4382: UINT *nd_compute_bound(ND p)
4383: {
4384: UINT *d1,*d2,*t;
4385: UINT u;
4386: int i,j,k,l,len,ind;
4387: NM m;
4388:
4389: if ( !p )
4390: return 0;
4391: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4392: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4393: len = LEN(p);
4394: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4395: for ( m = NEXT(m); m; m = NEXT(m) ) {
4396: ndl_lcm(DL(m),d1,d2);
4397: t = d1; d1 = d2; d2 = t;
4398: }
4399: l = nd_nvar+31;
4400: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4401: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4402: u = d1[i];
4403: k = (nd_epw-1)*nd_bpe;
4404: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4405: t[ind] = (u>>k)&nd_mask0;
4406: }
4407: for ( ; ind < l; ind++ ) t[ind] = 0;
4408: return t;
4409: }
4410:
4411: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4412: /* of a term. In this case we need additional 1 word. */
4413:
4414: int nd_get_exporigin(struct order_spec *ord)
4415: {
4416: switch ( ord->id ) {
4417: case 0: case 2: case 256: case 258:
4418: return 1+nd_module;
4419: case 1: case 257:
4420: /* block order */
4421: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4422: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4423: return ord->ord.block.length+1+nd_module;
4424: case 3: case 259:
4425: #if 0
4426: error("nd_get_exporigin : composite order is not supported yet.");
4427: #else
4428: return 1+nd_module;
4429: #endif
4430: default:
4431: error("nd_get_exporigin : ivalid argument.");
4432: return 0;
4433: }
4434: }
4435:
4436: void nd_setup_parameters(int nvar,int max) {
4437: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4438: struct order_pair *op;
4439:
4440: nd_nvar = nvar;
4441: if ( max ) {
4442: /* XXX */
4443: if ( do_weyl ) nd_bpe = 32;
4444: else if ( max < 2 ) nd_bpe = 1;
4445: else if ( max < 4 ) nd_bpe = 2;
4446: else if ( max < 8 ) nd_bpe = 3;
4447: else if ( max < 16 ) nd_bpe = 4;
4448: else if ( max < 32 ) nd_bpe = 5;
4449: else if ( max < 64 ) nd_bpe = 6;
4450: else if ( max < 256 ) nd_bpe = 8;
4451: else if ( max < 1024 ) nd_bpe = 10;
4452: else if ( max < 65536 ) nd_bpe = 16;
4453: else nd_bpe = 32;
4454: }
4455: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4456: UINT t;
4457: int st;
4458: int *v;
4459: /* t = max(weights) */
4460: t = 0;
4461: if ( current_dl_weight_vector )
4462: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4463: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4464: if ( t < st ) t = st;
4465: }
4466: if ( nd_matrix )
4467: for ( i = 0; i < nd_matrix_len; i++ )
4468: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4469: if ( (st=v[j]) < 0 ) st = -st;
4470: if ( t < st ) t = st;
4471: }
4472: /* i = bitsize of t */
4473: for ( i = 0; t; t >>=1, i++ );
4474: /* i += bitsize of nd_nvar */
4475: for ( t = nd_nvar; t; t >>=1, i++);
4476: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4477: if ( (nd_bpe+i) >= 31 )
4478: error("nd_setup_parameters : too large weight");
4479: }
4480: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4481: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4482: nd_exporigin = nd_get_exporigin(nd_ord);
4483: wpd = nd_exporigin+elen;
4484: if ( nd_module )
4485: nd_mpos = nd_exporigin-1;
4486: else
4487: nd_mpos = -1;
4488: if ( wpd != nd_wpd ) {
4489: nd_free_private_storage();
4490: nd_wpd = wpd;
4491: }
4492: if ( nd_bpe < 32 ) {
4493: nd_mask0 = (1<<nd_bpe)-1;
4494: } else {
4495: nd_mask0 = 0xffffffff;
4496: }
4497: bzero(nd_mask,sizeof(nd_mask));
4498: nd_mask1 = 0;
4499: for ( i = 0; i < nd_epw; i++ ) {
4500: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4501: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4502: }
4503: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4504: nd_epos = nd_create_epos(nd_ord);
4505: nd_blockmask = nd_create_blockmask(nd_ord);
4506: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4507: }
4508:
4509: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4510: {
4511: int i,obpe,oadv,h;
4512: static NM prev_nm_free_list;
4513: static ND_pairs prev_ndp_free_list;
4514: RHist mr0,mr;
4515: RHist r;
4516: RHist *old_red;
4517: ND_pairs s0,s,t;
4518: EPOS oepos;
4519:
4520: obpe = nd_bpe;
4521: oadv = nmv_adv;
4522: oepos = nd_epos;
4523: if ( obpe < 2 ) nd_bpe = 2;
4524: else if ( obpe < 3 ) nd_bpe = 3;
4525: else if ( obpe < 4 ) nd_bpe = 4;
4526: else if ( obpe < 5 ) nd_bpe = 5;
4527: else if ( obpe < 6 ) nd_bpe = 6;
4528: else if ( obpe < 8 ) nd_bpe = 8;
4529: else if ( obpe < 10 ) nd_bpe = 10;
4530: else if ( obpe < 16 ) nd_bpe = 16;
4531: else if ( obpe < 32 ) nd_bpe = 32;
4532: else error("nd_reconstruct : exponent too large");
4533:
4534: nd_setup_parameters(nd_nvar,0);
4535: prev_nm_free_list = _nm_free_list;
4536: prev_ndp_free_list = _ndp_free_list;
4537: _nm_free_list = 0;
4538: _ndp_free_list = 0;
4539: for ( i = nd_psn-1; i >= 0; i-- ) {
4540: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4541: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4542: }
4543: if ( trace )
4544: for ( i = nd_psn-1; i >= 0; i-- ) {
4545: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4546: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4547: }
4548: s0 = 0;
4549: for ( t = d; t; t = NEXT(t) ) {
4550: NEXTND_pairs(s0,s);
4551: s->i1 = t->i1;
4552: s->i2 = t->i2;
4553: SG(s) = SG(t);
4554: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4555: }
4556:
4557: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4558: for ( i = 0; i < REDTAB_LEN; i++ ) {
4559: old_red[i] = nd_red[i];
4560: nd_red[i] = 0;
4561: }
4562: for ( i = 0; i < REDTAB_LEN; i++ )
4563: for ( r = old_red[i]; r; r = NEXT(r) ) {
4564: NEWRHist(mr);
4565: mr->index = r->index;
4566: SG(mr) = SG(r);
4567: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4568: h = ndl_hash_value(DL(mr));
4569: NEXT(mr) = nd_red[h];
4570: nd_red[h] = mr;
4571: }
4572: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4573: old_red = 0;
4574: for ( i = 0; i < nd_psn; i++ ) {
4575: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4576: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4577: nd_psh[i] = r;
4578: }
4579: if ( s0 ) NEXT(s) = 0;
4580: prev_nm_free_list = 0;
4581: prev_ndp_free_list = 0;
4582: #if 0
4583: GC_gcollect();
4584: #endif
4585: return s0;
4586: }
4587:
4588: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4589: {
4590: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4591: struct order_pair *op;
4592:
4593: n = nd_nvar;
4594: oepw = (sizeof(UINT)*8)/obpe;
4595: omask0 = (1<<obpe)-1;
4596: TD(r) = TD(d);
4597: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4598: if ( nd_blockmask ) {
4599: l = nd_blockmask->n;
4600: op = nd_blockmask->order_pair;
4601: for ( i = 1; i < nd_exporigin; i++ )
4602: r[i] = d[i];
4603: for ( j = 0, s = 0; j < l; j++ ) {
4604: ord_l = op[j].length;
4605: for ( i = 0; i < ord_l; i++, s++ ) {
4606: ei = GET_EXP_OLD(d,s);
4607: PUT_EXP(r,s,ei);
4608: }
4609: }
4610: } else {
4611: for ( i = 0; i < n; i++ ) {
4612: ei = GET_EXP_OLD(d,i);
4613: PUT_EXP(r,i,ei);
4614: }
4615: }
4616: if ( nd_module ) MPOS(r) = MPOS(d);
4617: }
4618:
4619: ND nd_copy(ND p)
4620: {
4621: NM m,mr,mr0;
4622: int c,n;
4623: ND r;
4624:
4625: if ( !p )
4626: return 0;
4627: else {
4628: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4629: NEXTNM(mr0,mr);
4630: CM(mr) = CM(m);
4631: ndl_copy(DL(m),DL(mr));
4632: }
4633: NEXT(mr) = 0;
4634: MKND(NV(p),mr0,LEN(p),r);
4635: SG(r) = SG(p);
4636: return r;
4637: }
4638: }
4639:
4640: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4641: {
4642: NM m1,m2;
4643: NDV p1,p2;
4644: ND t1,t2;
4645: UINT *lcm;
4646: P gp,tp;
4647: Z g,t;
4648: Z iq;
4649: int td;
4650: LIST hist;
4651: NODE node;
4652: DP d;
4653:
4654: if ( !mod && nd_demand ) {
4655: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4656: } else {
4657: if ( trace ) {
4658: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4659: } else {
4660: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4661: }
4662: }
4663: lcm = LCM(p);
4664: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4665: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4666: FREENM(m1); return 0;
4667: }
4668: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4669: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4670: FREENM(m1); FREENM(m2); return 0;
4671: }
4672:
4673: if ( mod == -1 ) {
4674: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4675: } else if ( mod > 0 ) {
4676: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4677: } else if ( mod == -2 ) {
4678: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4679: } else if ( nd_vc ) {
4680: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4681: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4682: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4683: } else {
1.6 noro 4684: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 4685: }
4686: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4687: *rp = nd_add(mod,t1,t2);
4688: if ( nd_gentrace ) {
4689: /* nd_tracelist is initialized */
1.6 noro 4690: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4691: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 4692: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4693: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4694: nd_tracelist = node;
4695: }
4696: FREENM(m1); FREENM(m2);
4697: return 1;
4698: }
4699:
4700: void ndv_mul_c(int mod,NDV p,int mul)
4701: {
4702: NMV m;
4703: int c,c1,len,i;
4704:
4705: if ( !p ) return;
4706: len = LEN(p);
4707: if ( mod == -1 )
4708: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4709: CM(m) = _mulsf(CM(m),mul);
4710: else
4711: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4712: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4713: }
4714: }
4715:
4716: void ndv_mul_c_lf(NDV p,Z mul)
4717: {
4718: NMV m;
4719: Z c;
4720: int len,i;
4721:
4722: if ( !p ) return;
4723: len = LEN(p);
4724: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4725: mullf(CZ(m),mul,&c); CZ(m) = c;
4726: }
4727: }
4728:
4729: /* for nd_det */
4730: void ndv_mul_c_q(NDV p,Z mul)
4731: {
4732: NMV m;
4733: Z c;
4734: int len,i;
4735:
4736: if ( !p ) return;
4737: len = LEN(p);
4738: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 4739: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 4740: }
4741: }
4742:
4743: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4744: int n2,i,j,l,n,tlen;
4745: UINT *d0;
4746: NM *tab,*psum;
4747: ND s,r;
4748: NM t;
4749: NMV m1;
4750:
4751: if ( !p ) return 0;
4752: n = NV(p); n2 = n>>1;
4753: d0 = DL(m0);
4754: l = LEN(p);
4755: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4756: tab = (NM *)MALLOC(tlen*sizeof(NM));
4757: psum = (NM *)MALLOC(tlen*sizeof(NM));
4758: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4759: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4760: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4761: /* m0(NM) * m1(NMV) => tab(NM) */
4762: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4763: for ( j = 0; j < tlen; j++ ) {
4764: if ( tab[j] ) {
4765: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4766: }
4767: }
4768: }
4769: for ( i = tlen-1, r = 0; i >= 0; i-- )
4770: if ( psum[i] ) {
4771: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4772: MKND(n,psum[i],j,s);
4773: r = nd_add(mod,r,s);
4774: }
4775: if ( r ) SG(r) = SG(p)+TD(d0);
4776: return r;
4777: }
4778:
4779: /* product of monomials */
4780: /* XXX block order is not handled correctly */
4781:
4782: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4783: {
4784: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4785: UINT *d0,*d1,*d,*dt,*ctab;
4786: Z *ctab_q;
4787: Z q,q1;
4788: UINT c0,c1,c;
4789: NM *p;
4790: NM m,t;
4791: int mpos;
4792:
4793: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4794: if ( !m0 || !m1 ) return;
4795: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4796: if ( nd_module )
4797: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4798:
4799: NEWNM(m); d = DL(m);
4800: if ( mod ) {
4801: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4802: } else if ( nd_vc )
4803: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4804: else
1.6 noro 4805: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 4806: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4807: homo = n&1 ? 1 : 0;
4808: if ( homo ) {
4809: /* offset of h-degree */
4810: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4811: PUT_EXP(DL(m),n-1,h);
4812: TD(DL(m)) = h;
4813: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4814: }
4815: tab[0] = m;
4816: NEWNM(m); d = DL(m);
4817: for ( i = 0, curlen = 1; i < n2; i++ ) {
4818: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4819: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4820: /* xi^a*(Di^k*xi^l)*Di^b */
4821: a += l; b += k;
4822: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4823: if ( !k || !l ) {
4824: for ( j = 0; j < curlen; j++ )
4825: if ( (t = tab[j]) != 0 ) {
4826: dt = DL(t);
4827: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4828: if ( nd_blockmask ) ndl_weight_mask(dt);
4829: }
4830: curlen *= k+1;
4831: continue;
4832: }
4833: min = MIN(k,l);
4834: if ( mod ) {
4835: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4836: mkwcm(k,l,mod,(int *)ctab);
4837: } else {
4838: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4839: mkwc(k,l,ctab_q);
4840: }
4841: for ( j = min; j >= 0; j-- ) {
4842: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4843: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4844: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4845: if ( homo ) {
4846: TD(d) = s;
4847: PUT_EXP(d,n-1,s-h);
4848: } else TD(d) = h;
4849: if ( nd_blockmask ) ndl_weight_mask(d);
4850: if ( mod ) c = ctab[j];
4851: else q = ctab_q[j];
4852: p = tab+curlen*j;
4853: if ( j == 0 ) {
4854: for ( u = 0; u < curlen; u++, p++ ) {
4855: if ( tab[u] ) {
4856: ndl_addto(DL(tab[u]),d);
4857: if ( mod ) {
4858: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4859: } else if ( nd_vc )
4860: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4861: else {
1.6 noro 4862: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 4863: }
4864: }
4865: }
4866: } else {
4867: for ( u = 0; u < curlen; u++, p++ ) {
4868: if ( tab[u] ) {
4869: NEWNM(t);
4870: ndl_add(DL(tab[u]),d,DL(t));
4871: if ( mod ) {
4872: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4873: } else if ( nd_vc )
4874: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4875: else
1.6 noro 4876: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 4877: *p = t;
4878: }
4879: }
4880: }
4881: }
4882: curlen *= k+1;
4883: }
4884: FREENM(m);
4885: if ( nd_module ) {
4886: mpos = MPOS(d1);
4887: for ( i = 0; i < tlen; i++ )
4888: if ( tab[i] ) {
4889: d = DL(tab[i]);
4890: MPOS(d) = mpos;
4891: TD(d) = ndl_weight(d);
4892: }
4893: }
4894: }
4895:
4896: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4897: {
4898: NM mr,mr0;
4899: NMV m;
4900: UINT *d,*dt,*dm;
4901: int c,n,td,i,c1,c2,len;
4902: Q q;
4903: ND r;
4904:
4905: if ( !p ) return 0;
4906: else {
4907: n = NV(p); m = BDY(p);
4908: d = DL(m0);
4909: len = LEN(p);
4910: mr0 = 0;
4911: td = TD(d);
4912: c = CM(m0);
4913: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4914: NEXTNM(mr0,mr);
4915: CM(mr) = 1;
4916: ndl_add(DL(m),d,DL(mr));
4917: }
4918: NEXT(mr) = 0;
4919: MKND(NV(p),mr0,len,r);
4920: SG(r) = SG(p) + TD(d);
4921: return r;
4922: }
4923: }
4924:
4925: ND ndv_mul_nm(int mod,NM m0,NDV p)
4926: {
4927: NM mr,mr0;
4928: NMV m;
4929: UINT *d,*dt,*dm;
4930: int c,n,td,i,c1,c2,len;
4931: P q;
4932: ND r;
4933:
4934: if ( !p ) return 0;
4935: else if ( do_weyl ) {
4936: if ( mod < 0 ) {
4937: error("ndv_mul_nm : not implemented (weyl)");
4938: return 0;
4939: } else
4940: return weyl_ndv_mul_nm(mod,m0,p);
4941: } else {
4942: n = NV(p); m = BDY(p);
4943: d = DL(m0);
4944: len = LEN(p);
4945: mr0 = 0;
4946: td = TD(d);
4947: if ( mod == -1 ) {
4948: c = CM(m0);
4949: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4950: NEXTNM(mr0,mr);
4951: CM(mr) = _mulsf(CM(m),c);
4952: ndl_add(DL(m),d,DL(mr));
4953: }
4954: } else if ( mod == -2 ) {
4955: Z cl;
4956: cl = CZ(m0);
4957: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4958: NEXTNM(mr0,mr);
4959: mullf(CZ(m),cl,&CZ(mr));
4960: ndl_add(DL(m),d,DL(mr));
4961: }
4962: } else if ( mod ) {
4963: c = CM(m0);
4964: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4965: NEXTNM(mr0,mr);
4966: c1 = CM(m);
4967: DMAR(c1,c,0,mod,c2);
4968: CM(mr) = c2;
4969: ndl_add(DL(m),d,DL(mr));
4970: }
4971: } else {
4972: q = CP(m0);
4973: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4974: NEXTNM(mr0,mr);
4975: mulp(nd_vc,CP(m),q,&CP(mr));
4976: ndl_add(DL(m),d,DL(mr));
4977: }
4978: }
4979: NEXT(mr) = 0;
4980: MKND(NV(p),mr0,len,r);
4981: SG(r) = SG(p) + TD(d);
4982: return r;
4983: }
4984: }
4985:
4986: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
4987: {
4988: NM mq0,mq;
4989: NMV tm;
4990: Q q;
4991: int i,nv,sg,c,c1,c2,hindex;
4992: ND p,t,r;
4993:
4994: if ( bucket->m < 0 ) return 0;
4995: else {
4996: nv = NV(d);
4997: mq0 = 0;
4998: tm = (NMV)MALLOC(nmv_adv);
4999: while ( 1 ) {
5000: if ( mod > 0 || mod == -1 )
5001: hindex = head_pbucket(mod,bucket);
5002: else if ( mod == -2 )
5003: hindex = head_pbucket_lf(bucket);
5004: else
5005: hindex = head_pbucket_q(bucket);
5006: if ( hindex < 0 ) break;
5007: p = bucket->body[hindex];
5008: NEXTNM(mq0,mq);
5009: ndl_sub(HDL(p),HDL(d),DL(mq));
5010: ndl_copy(DL(mq),DL(tm));
5011: if ( mod ) {
5012: c1 = invm(HCM(d),mod); c2 = HCM(p);
5013: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5014: CM(tm) = mod-c;
5015: } else {
1.6 noro 5016: divsz(HCZ(p),HCZ(d),&CZ(mq));
5017: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5018: }
5019: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5020: bucket->body[hindex] = nd_remove_head(p);
5021: t = nd_remove_head(t);
5022: add_pbucket(mod,bucket,t);
5023: }
5024: if ( !mq0 )
5025: r = 0;
5026: else {
5027: NEXT(mq) = 0;
5028: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5029: MKND(nv,mq0,i,r);
5030: /* XXX */
5031: SG(r) = HTD(r);
5032: }
5033: return r;
5034: }
5035: }
5036:
5037: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5038: {
5039: NMV m,mr,mr0,t;
5040: int len,i,k;
5041:
5042: if ( !p ) return;
5043: m = BDY(p); len = LEN(p);
5044: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5045: m = (NMV)((char *)mr0+(len-1)*oadv);
5046: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5047: t = (NMV)MALLOC(nmv_adv);
5048: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5049: CZ(t) = CZ(m);
1.1 noro 5050: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5051: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5052: CZ(mr) = CZ(t);
1.1 noro 5053: ndl_copy(DL(t),DL(mr));
5054: }
5055: BDY(p) = mr0;
5056: }
5057:
5058: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5059: {
5060: NMV m,mr,mr0;
5061: int len,i;
5062: NDV r;
5063:
5064: if ( !p ) return 0;
5065: m = BDY(p); len = LEN(p);
5066: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5067: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5068: ndl_zero(DL(mr));
5069: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5070: CZ(mr) = CZ(m);
1.1 noro 5071: }
5072: MKNDV(NV(p),mr0,len,r);
5073: SG(r) = SG(p);
5074: return r;
5075: }
5076:
5077: /* duplicate p */
5078:
5079: NDV ndv_dup(int mod,NDV p)
5080: {
5081: NDV d;
5082: NMV t,m,m0;
5083: int i,len;
5084:
5085: if ( !p ) return 0;
5086: len = LEN(p);
5087: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5088: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5089: ndl_copy(DL(t),DL(m));
1.6 noro 5090: CZ(m) = CZ(t);
1.1 noro 5091: }
5092: MKNDV(NV(p),m0,len,d);
5093: SG(d) = SG(p);
5094: return d;
5095: }
5096:
5097: NDV ndv_symbolic(int mod,NDV p)
5098: {
5099: NDV d;
5100: NMV t,m,m0;
5101: int i,len;
5102:
5103: if ( !p ) return 0;
5104: len = LEN(p);
5105: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5106: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5107: ndl_copy(DL(t),DL(m));
1.6 noro 5108: CZ(m) = ONE;
1.1 noro 5109: }
5110: MKNDV(NV(p),m0,len,d);
5111: SG(d) = SG(p);
5112: return d;
5113: }
5114:
5115: ND nd_dup(ND p)
5116: {
5117: ND d;
5118: NM t,m,m0;
5119:
5120: if ( !p ) return 0;
5121: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5122: NEXTNM(m0,m);
5123: ndl_copy(DL(t),DL(m));
1.6 noro 5124: CZ(m) = CZ(t);
1.1 noro 5125: }
5126: if ( m0 ) NEXT(m) = 0;
5127: MKND(NV(p),m0,LEN(p),d);
5128: SG(d) = SG(p);
5129: return d;
5130: }
5131:
5132: /* XXX if p->len == 0 then it represents 0 */
5133:
5134: void ndv_mod(int mod,NDV p)
5135: {
5136: NMV t,d;
5137: int r,s,u;
5138: int i,len,dlen;
5139: P cp;
5140: Z c;
5141: Obj gfs;
5142:
5143: if ( !p ) return;
5144: len = LEN(p);
5145: dlen = 0;
5146: if ( mod == -1 )
5147: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5148: simp_ff((Obj)CP(t),&gfs);
5149: if ( gfs ) {
5150: r = FTOIF(CONT((GFS)gfs));
5151: CM(d) = r;
5152: ndl_copy(DL(t),DL(d));
5153: NMV_ADV(d);
5154: dlen++;
5155: }
5156: }
5157: else if ( mod == -2 )
5158: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5159: simp_ff((Obj)CP(t),&gfs);
5160: if ( gfs ) {
5161: lmtolf((LM)gfs,&CZ(d));
5162: ndl_copy(DL(t),DL(d));
5163: NMV_ADV(d);
5164: dlen++;
5165: }
5166: }
5167: else
5168: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5169: if ( nd_vc ) {
5170: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5171: c = (Z)cp;
5172: } else
1.6 noro 5173: c = CZ(t);
1.1 noro 5174: r = remqi((Q)c,mod);
5175: if ( r ) {
5176: CM(d) = r;
5177: ndl_copy(DL(t),DL(d));
5178: NMV_ADV(d);
5179: dlen++;
5180: }
5181: }
5182: LEN(p) = dlen;
5183: }
5184:
5185: NDV ptondv(VL vl,VL dvl,P p)
5186: {
5187: ND nd;
5188:
5189: nd = ptond(vl,dvl,p);
5190: return ndtondv(0,nd);
5191: }
5192:
5193: void pltozpl(LIST l,Q *cont,LIST *pp)
5194: {
5195: NODE nd,nd1;
5196: int n;
5197: P *pl;
5198: Q *cl;
5199: int i;
5200: P dmy;
5201: Z dvr;
5202: LIST r;
5203:
5204: nd = BDY(l); n = length(nd);
5205: pl = (P *)MALLOC(n*sizeof(P));
5206: cl = (Q *)MALLOC(n*sizeof(P));
5207: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5208: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5209: qltozl(cl,n,&dvr);
5210: nd = BDY(l);
5211: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5212: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5213: }
5214: nd = 0;
5215: for ( i = n-1; i >= 0; i-- ) {
5216: MKNODE(nd1,pl[i],nd); nd = nd1;
5217: }
5218: MKLIST(r,nd);
5219: *pp = r;
5220: }
5221:
5222: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5223:
5224: NDV pltondv(VL vl,VL dvl,LIST p)
5225: {
5226: int i;
5227: NODE t;
5228: ND r,ri;
5229: NM m;
5230:
5231: if ( !nd_module ) error("pltond : module order must be set");
5232: r = 0;
5233: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5234: ri = ptond(vl,dvl,(P)BDY(t));
5235: if ( ri )
5236: for ( m = BDY(ri); m; m = NEXT(m) ) {
5237: MPOS(DL(m)) = i;
5238: TD(DL(m)) = ndl_weight(DL(m));
5239: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5240: }
5241: r = nd_add(0,r,ri);
5242: }
5243: return ndtondv(0,r);
5244: }
5245:
5246: ND ptond(VL vl,VL dvl,P p)
5247: {
5248: int n,i,j,k,e;
5249: VL tvl;
5250: V v;
5251: DCP dc;
5252: DCP *w;
5253: ND r,s,t,u;
5254: P x;
5255: int c;
5256: UINT *d;
5257: NM m,m0;
5258:
5259: if ( !p )
5260: return 0;
5261: else if ( NUM(p) ) {
5262: NEWNM(m);
5263: ndl_zero(DL(m));
5264: if ( !INT((Q)p) )
5265: error("ptond : input must be integer-coefficient");
1.6 noro 5266: CZ(m) = (Z)p;
1.1 noro 5267: NEXT(m) = 0;
5268: MKND(nd_nvar,m,1,r);
5269: SG(r) = 0;
5270: return r;
5271: } else {
5272: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5273: w = (DCP *)MALLOC(k*sizeof(DCP));
5274: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5275: for ( i = 0, tvl = dvl, v = VR(p);
5276: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5277: if ( !tvl ) {
5278: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5279: t = ptond(vl,dvl,COEF(w[j]));
5280: pwrp(vl,x,DEG(w[j]),&p);
5281: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5282: }
5283: return s;
5284: } else {
5285: NEWNM(m0); d = DL(m0);
5286: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 5287: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 5288: TD(d) = MUL_WEIGHT(e,i);
5289: if ( nd_blockmask) ndl_weight_mask(d);
5290: if ( nd_module ) MPOS(d) = 0;
5291: t = ptond(vl,dvl,COEF(w[j]));
5292: for ( m = BDY(t); m; m = NEXT(m) )
5293: ndl_addto(DL(m),d);
5294: SG(t) += TD(d);
5295: s = nd_add(0,s,t);
5296: }
5297: FREENM(m0);
5298: return s;
5299: }
5300: }
5301: }
5302:
5303: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5304: {
5305: VL tvl;
5306: int len,n,j,i,e;
5307: NMV m;
5308: Z q;
5309: P c;
5310: UINT *d;
5311: P s,r,u,t,w;
5312: GFS gfs;
5313:
5314: if ( !p ) return 0;
5315: else {
5316: len = LEN(p);
5317: n = NV(p);
5318: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5319: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5320: if ( mod == -1 ) {
5321: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5322: } else if ( mod == -2 ) {
5323: c = (P)CZ(m);
5324: } else if ( mod > 0 ) {
1.6 noro 5325: STOZ(CM(m),q); c = (P)q;
1.1 noro 5326: } else
5327: c = CP(m);
5328: d = DL(m);
5329: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5330: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5331: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5332: }
5333: addp(vl,s,t,&u); s = u;
5334: }
5335: return s;
5336: }
5337: }
5338:
5339: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5340: {
5341: VL tvl;
5342: int len,n,j,i,e;
5343: NMV m;
5344: Z q;
5345: P c;
5346: UINT *d;
5347: P s,r,u,t,w;
5348: GFS gfs;
5349: P *a;
5350: LIST l;
5351: NODE nd,nd1;
5352:
5353: if ( !p ) return 0;
5354: else {
5355: a = (P *)MALLOC((rank+1)*sizeof(P));
5356: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5357: len = LEN(p);
5358: n = NV(p);
5359: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5360: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5361: if ( mod == -1 ) {
5362: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5363: } else if ( mod ) {
1.6 noro 5364: STOZ(CM(m),q); c = (P)q;
1.1 noro 5365: } else
5366: c = CP(m);
5367: d = DL(m);
5368: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5369: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5370: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5371: }
5372: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5373: }
5374: nd = 0;
5375: for ( i = rank; i > 0; i-- ) {
5376: MKNODE(nd1,a[i],nd); nd = nd1;
5377: }
5378: MKLIST(l,nd);
5379: return l;
5380: }
5381: }
5382:
5383: NDV ndtondv(int mod,ND p)
5384: {
5385: NDV d;
5386: NMV m,m0;
5387: NM t;
5388: int i,len;
5389:
5390: if ( !p ) return 0;
5391: len = LEN(p);
5392: if ( mod > 0 || mod == -1 )
5393: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5394: else
5395: m0 = m = MALLOC(len*nmv_adv);
5396: #if 0
5397: ndv_alloc += nmv_adv*len;
5398: #endif
5399: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5400: ndl_copy(DL(t),DL(m));
1.6 noro 5401: CZ(m) = CZ(t);
1.1 noro 5402: }
5403: MKNDV(NV(p),m0,len,d);
5404: SG(d) = SG(p);
5405: return d;
5406: }
5407:
5408: ND ndvtond(int mod,NDV p)
5409: {
5410: ND d;
5411: NM m,m0;
5412: NMV t;
5413: int i,len;
5414:
5415: if ( !p ) return 0;
5416: m0 = 0;
5417: len = p->len;
5418: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5419: NEXTNM(m0,m);
5420: ndl_copy(DL(t),DL(m));
1.6 noro 5421: CZ(m) = CZ(t);
1.1 noro 5422: }
5423: NEXT(m) = 0;
5424: MKND(NV(p),m0,len,d);
5425: SG(d) = SG(p);
5426: return d;
5427: }
5428:
5429: DP ndvtodp(int mod,NDV p)
5430: {
5431: MP m,m0;
5432: DP d;
5433: NMV t;
5434: int i,len;
5435:
5436: if ( !p ) return 0;
5437: m0 = 0;
5438: len = p->len;
5439: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5440: NEXTMP(m0,m);
5441: m->dl = ndltodl(nd_nvar,DL(t));
5442: m->c = (Obj)ndctop(mod,t->c);
5443: }
5444: NEXT(m) = 0;
5445: MKDP(nd_nvar,m0,d);
5446: SG(d) = SG(p);
5447: return d;
5448: }
5449:
5450: DP ndtodp(int mod,ND p)
5451: {
5452: MP m,m0;
5453: DP d;
5454: NM t;
5455: int i,len;
5456:
5457: if ( !p ) return 0;
5458: m0 = 0;
5459: len = p->len;
5460: for ( t = BDY(p); t; t = NEXT(t) ) {
5461: NEXTMP(m0,m);
5462: m->dl = ndltodl(nd_nvar,DL(t));
5463: m->c = (Obj)ndctop(mod,t->c);
5464: }
5465: NEXT(m) = 0;
5466: MKDP(nd_nvar,m0,d);
5467: SG(d) = SG(p);
5468: return d;
5469: }
5470:
5471: void ndv_print(NDV p)
5472: {
5473: NMV m;
5474: int i,len;
5475:
5476: if ( !p ) printf("0\n");
5477: else {
5478: len = LEN(p);
5479: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5480: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5481: else printf("+%d*",CM(m));
5482: ndl_print(DL(m));
5483: }
5484: printf("\n");
5485: }
5486: }
5487:
5488: void ndv_print_q(NDV p)
5489: {
5490: NMV m;
5491: int i,len;
5492:
5493: if ( !p ) printf("0\n");
5494: else {
5495: len = LEN(p);
5496: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5497: printf("+");
1.6 noro 5498: printexpr(CO,(Obj)CZ(m));
1.1 noro 5499: printf("*");
5500: ndl_print(DL(m));
5501: }
5502: printf("\n");
5503: }
5504: }
5505:
5506: NODE ndv_reducebase(NODE x,int *perm)
5507: {
5508: int len,i,j;
5509: NDVI w;
5510: NODE t,t0;
5511:
5512: len = length(x);
5513: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5514: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5515: w[i].p = BDY(t); w[i].i = perm[i];
5516: }
5517: for ( i = 0; i < len; i++ ) {
5518: for ( j = 0; j < i; j++ ) {
5519: if ( w[i].p && w[j].p ) {
5520: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5521: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5522: }
5523: }
5524: }
5525: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5526: if ( w[i].p ) {
5527: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5528: perm[j++] = w[i].i;
5529: }
5530: }
5531: NEXT(t) = 0; x = t0;
5532: return x;
5533: }
5534:
5535: /* XXX incomplete */
5536:
5537: void nd_init_ord(struct order_spec *ord)
5538: {
5539: nd_module = (ord->id >= 256);
5540: if ( nd_module ) {
5541: nd_dcomp = -1;
5542: nd_ispot = ord->ispot;
5543: nd_pot_nelim = ord->pot_nelim;
5544: nd_poly_weight_len = ord->nv;
5545: nd_poly_weight = ord->top_weight;
5546: nd_module_rank = ord->module_rank;
5547: nd_module_weight = ord->module_top_weight;
5548: }
5549: nd_matrix = 0;
5550: nd_matrix_len = 0;
5551: switch ( ord->id ) {
5552: case 0:
5553: switch ( ord->ord.simple ) {
5554: case 0:
5555: nd_dcomp = 1;
5556: nd_isrlex = 1;
5557: break;
5558: case 1:
5559: nd_dcomp = 1;
5560: nd_isrlex = 0;
5561: break;
5562: case 2:
5563: nd_dcomp = 0;
5564: nd_isrlex = 0;
5565: ndl_compare_function = ndl_lex_compare;
5566: break;
5567: case 11:
5568: /* XXX */
5569: nd_dcomp = 0;
5570: nd_isrlex = 1;
5571: ndl_compare_function = ndl_ww_lex_compare;
5572: break;
5573: default:
5574: error("nd_gr : unsupported order");
5575: }
5576: break;
5577: case 1:
5578: /* block order */
5579: /* XXX */
5580: nd_dcomp = -1;
5581: nd_isrlex = 0;
5582: ndl_compare_function = ndl_block_compare;
5583: break;
5584: case 2:
5585: /* matrix order */
5586: /* XXX */
5587: nd_dcomp = -1;
5588: nd_isrlex = 0;
5589: nd_matrix_len = ord->ord.matrix.row;
5590: nd_matrix = ord->ord.matrix.matrix;
5591: ndl_compare_function = ndl_matrix_compare;
5592: break;
5593: case 3:
5594: /* composite order */
5595: nd_dcomp = -1;
5596: nd_isrlex = 0;
5597: nd_worb_len = ord->ord.composite.length;
5598: nd_worb = ord->ord.composite.w_or_b;
5599: ndl_compare_function = ndl_composite_compare;
5600: break;
5601:
5602: /* module order */
5603: case 256:
5604: switch ( ord->ord.simple ) {
5605: case 0:
5606: nd_isrlex = 1;
5607: ndl_compare_function = ndl_module_grlex_compare;
5608: break;
5609: case 1:
5610: nd_isrlex = 0;
5611: ndl_compare_function = ndl_module_glex_compare;
5612: break;
5613: case 2:
5614: nd_isrlex = 0;
5615: ndl_compare_function = ndl_module_lex_compare;
5616: break;
5617: default:
5618: error("nd_gr : unsupported order");
5619: }
5620: break;
5621: case 257:
5622: /* block order */
5623: nd_isrlex = 0;
5624: ndl_compare_function = ndl_module_block_compare;
5625: break;
5626: case 258:
5627: /* matrix order */
5628: nd_isrlex = 0;
5629: nd_matrix_len = ord->ord.matrix.row;
5630: nd_matrix = ord->ord.matrix.matrix;
5631: ndl_compare_function = ndl_module_matrix_compare;
5632: break;
5633: case 259:
5634: /* composite order */
5635: nd_isrlex = 0;
5636: nd_worb_len = ord->ord.composite.length;
5637: nd_worb = ord->ord.composite.w_or_b;
5638: ndl_compare_function = ndl_module_composite_compare;
5639: break;
5640: }
5641: nd_ord = ord;
5642: }
5643:
5644: BlockMask nd_create_blockmask(struct order_spec *ord)
5645: {
5646: int n,i,j,s,l;
5647: UINT *t;
5648: BlockMask bm;
5649:
5650: /* we only create mask table for block order */
5651: if ( ord->id != 1 && ord->id != 257 )
5652: return 0;
5653: n = ord->ord.block.length;
5654: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5655: bm->n = n;
5656: bm->order_pair = ord->ord.block.order_pair;
5657: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5658: for ( i = 0, s = 0; i < n; i++ ) {
5659: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5660: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5661: l = bm->order_pair[i].length;
5662: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5663: }
5664: return bm;
5665: }
5666:
5667: EPOS nd_create_epos(struct order_spec *ord)
5668: {
5669: int i,j,l,s,ord_l,ord_o;
5670: EPOS epos;
5671: struct order_pair *op;
5672:
5673: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5674: switch ( ord->id ) {
5675: case 0: case 256:
5676: if ( nd_isrlex ) {
5677: for ( i = 0; i < nd_nvar; i++ ) {
5678: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5679: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5680: }
5681: } else {
5682: for ( i = 0; i < nd_nvar; i++ ) {
5683: epos[i].i = nd_exporigin + i/nd_epw;
5684: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5685: }
5686: }
5687: break;
5688: case 1: case 257:
5689: /* block order */
5690: l = ord->ord.block.length;
5691: op = ord->ord.block.order_pair;
5692: for ( j = 0, s = 0; j < l; j++ ) {
5693: ord_o = op[j].order;
5694: ord_l = op[j].length;
5695: if ( !ord_o )
5696: for ( i = 0; i < ord_l; i++ ) {
5697: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5698: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5699: }
5700: else
5701: for ( i = 0; i < ord_l; i++ ) {
5702: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5703: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5704: }
5705: s += ord_l;
5706: }
5707: break;
5708: case 2:
5709: /* matrix order */
5710: case 3:
5711: /* composite order */
5712: default:
5713: for ( i = 0; i < nd_nvar; i++ ) {
5714: epos[i].i = nd_exporigin + i/nd_epw;
5715: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5716: }
5717: break;
5718: }
5719: return epos;
5720: }
5721:
5722: /* external interface */
5723:
5724: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5725: {
5726: NODE t,in0,in;
5727: ND ndf,nf;
5728: NDV ndvf;
5729: VL vv,tv;
5730: int stat,nvar,max,mrank;
5731: union oNDC dn;
5732: Q cont;
5733: P pp;
5734: LIST ppl;
5735:
5736: if ( !f ) {
5737: *rp = 0;
5738: return;
5739: }
5740: pltovl(v,&vv);
5741: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5742:
5743: /* max=65536 implies nd_bpe=32 */
5744: max = 65536;
5745:
5746: nd_module = 0;
5747: /* nd_module will be set if ord is a module ordering */
5748: nd_init_ord(ord);
5749: nd_setup_parameters(nvar,max);
5750: if ( nd_module && OID(f) != O_LIST )
5751: error("nd_nf_p : the first argument must be a list");
5752: if ( nd_module ) mrank = length(BDY((LIST)f));
5753: /* conversion to ndv */
5754: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5755: NEXTNODE(in0,in);
5756: if ( nd_module ) {
5757: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5758: || length(BDY((LIST)BDY(t))) != mrank )
5759: error("nd_nf_p : inconsistent basis element");
5760: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5761: else ppl = (LIST)BDY(t);
5762: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5763: } else {
5764: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5765: else pp = (P)BDY(t);
5766: BDY(in) = (pointer)ptondv(CO,vv,pp);
5767: }
5768: if ( m ) ndv_mod(m,(NDV)BDY(in));
5769: }
5770: if ( in0 ) NEXT(in) = 0;
5771:
5772: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5773: else ndvf = ptondv(CO,vv,(P)f);
5774: if ( m ) ndv_mod(m,ndvf);
5775: ndf = (pointer)ndvtond(m,ndvf);
5776:
5777: /* dont sort, dont removecont */
5778: ndv_setup(m,0,in0,1,1);
5779: nd_scale=2;
1.6 noro 5780: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 5781: if ( !stat )
5782: error("nd_nf_p : exponent too large");
5783: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5784: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5785: }
5786:
5787: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5788: {
5789: NM m;
5790: UINT *t,*s;
5791: int i;
5792:
5793: for ( i = 0; i < n; i++ ) r[i] = 0;
5794: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5795: t = DL(m);
5796: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5797: r[i] = CM(m);
5798: }
5799: for ( i = 0; !r[i]; i++ );
5800: return i;
5801: }
5802:
5803: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
5804: {
5805: NM m;
5806: UINT *t,*s;
5807: int i;
5808:
5809: for ( i = 0; i < n; i++ ) r[i] = 0;
5810: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5811: t = DL(m);
5812: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5813: r[i] = CZ(m);
1.1 noro 5814: }
5815: for ( i = 0; !r[i]; i++ );
5816: return i;
5817: }
5818:
5819: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5820: {
5821: NM m;
5822: UINT *t,*s;
5823: int i;
5824:
5825: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5826: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5827: t = DL(m);
5828: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5829: mpz_set(r[i],BDY(CZ(m)));
5830: }
5831: for ( i = 0; !mpz_sgn(r[i]); i++ );
5832: return i;
5833: }
5834:
5835: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
5836: {
5837: NM m;
5838: unsigned long *v;
5839: int i,j,h,size;
5840: UINT *s,*t;
5841:
5842: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
5843: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
5844: bzero(v,size);
5845: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
5846: t = DL(m);
5847: h = ndl_hash_value(t);
5848: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5849: v[i/BLEN] |= 1L <<(i%BLEN);
5850: }
5851: return v;
5852: }
5853:
5854: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
5855: {
5856: NMV mr;
5857: UINT *d,*t,*s;
5858: int i,j,len,h,head;
5859:
5860: d = DL(m);
5861: len = LEN(p);
5862: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5863: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5864: ndl_add(d,DL(mr),t);
5865: h = ndl_hash_value(t);
5866: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5867: if ( j == 0 ) head = i;
5868: v[i/BLEN] |= 1L <<(i%BLEN);
5869: }
5870: return head;
5871: }
5872:
5873: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
5874: {
5875: NM m;
5876: NMV mr;
5877: UINT *d,*t,*s;
5878: NDV p;
5879: int i,j,len;
5880: Z *r;
5881:
5882: m = pair->mul;
5883: d = DL(m);
5884: p = nd_ps[pair->index];
5885: len = LEN(p);
5886: r = (Z *)CALLOC(n,sizeof(Q));
5887: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5888: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5889: ndl_add(d,DL(mr),t);
5890: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5891: r[i] = CZ(mr);
1.1 noro 5892: }
5893: return r;
5894: }
5895:
1.11 noro 5896: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 5897: {
5898: NM m;
5899: NMV mr;
1.11 noro 5900: UINT *d,*t,*s,*u;
1.1 noro 5901: NDV p;
5902: unsigned char *ivc;
5903: unsigned short *ivs;
5904: UINT *v,*ivi,*s0v;
1.11 noro 5905: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 5906: IndArray r;
5907:
5908: m = pair->mul;
5909: d = DL(m);
5910: if ( trace )
5911: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
5912: else
5913: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
5914:
5915: len = LEN(p);
5916: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5917: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 5918: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
5919: ndl_add(d,DL(mr),t);
5920: st = prev;
5921: ed = n;
5922: while ( ed > st ) {
5923: md = (st+ed)/2;
5924: u = s0+md*nd_wpd;
5925: c = DL_COMPARE(u,t);
5926: if ( c == 0 ) break;
5927: else if ( c > 0 ) st = md;
5928: else ed = md;
5929: }
5930: prev = v[j] = md;
1.1 noro 5931: }
5932: r = (IndArray)MALLOC(sizeof(struct oIndArray));
5933: r->head = v[0];
5934: diff = 0;
5935: for ( i = 1; i < len; i++ ) {
5936: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
5937: }
5938: if ( diff < 256 ) {
5939: r->width = 1;
5940: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
5941: r->index.c = ivc;
5942: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
5943: } else if ( diff < 65536 ) {
5944: r->width = 2;
5945: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
5946: r->index.s = ivs;
5947: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
5948: } else {
5949: r->width = 4;
5950: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
5951: r->index.i = ivi;
5952: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
5953: }
5954: return r;
5955: }
5956:
5957: int compress_array(Z *svect,Z *cvect,int n)
5958: {
5959: int i,j;
5960:
5961: for ( i = j = 0; i < n; i++ )
5962: if ( svect[i] ) cvect[j++] = svect[i];
5963: return j;
5964: }
5965:
5966: void expand_array(Z *svect,Z *cvect,int n)
5967: {
5968: int i,j;
5969:
5970: for ( i = j = 0; j < n; i++ )
5971: if ( svect[i] ) svect[i] = cvect[j++];
5972: }
5973:
1.8 noro 5974: #if 0
1.1 noro 5975: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
5976: {
5977: int i,j,k,len,pos,prev,nz;
5978: Z cs,mcs,c1,c2,cr,gcd,t;
5979: IndArray ivect;
5980: unsigned char *ivc;
5981: unsigned short *ivs;
5982: unsigned int *ivi;
5983: NDV redv;
5984: NMV mr;
5985: NODE rp;
5986: int maxrs;
5987: double hmag;
5988: Z *cvect;
1.3 noro 5989: int l;
1.1 noro 5990:
5991: maxrs = 0;
5992: for ( i = 0; i < col && !svect[i]; i++ );
5993: if ( i == col ) return maxrs;
5994: hmag = p_mag((P)svect[i])*nd_scale;
5995: cvect = (Z *)MALLOC(col*sizeof(Q));
5996: for ( i = 0; i < nred; i++ ) {
5997: ivect = imat[i];
5998: k = ivect->head;
5999: if ( svect[k] ) {
6000: maxrs = MAX(maxrs,rp0[i]->sugar);
6001: redv = nd_demand?ndv_load(rp0[i]->index)
6002: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6003: len = LEN(redv); mr = BDY(redv);
1.6 noro 6004: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6005: chsgnz(cs,&mcs);
6006: if ( !UNIQ(cr) ) {
6007: for ( j = 0; j < col; j++ ) {
6008: mulz(svect[j],cr,&c1); svect[j] = c1;
6009: }
6010: }
6011: svect[k] = 0; prev = k;
6012: switch ( ivect->width ) {
6013: case 1:
6014: ivc = ivect->index.c;
6015: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6016: pos = prev+ivc[j]; prev = pos;
1.6 noro 6017: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6018: }
6019: break;
6020: case 2:
6021: ivs = ivect->index.s;
6022: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6023: pos = prev+ivs[j]; prev = pos;
1.6 noro 6024: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6025: }
6026: break;
6027: case 4:
6028: ivi = ivect->index.i;
6029: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6030: pos = prev+ivi[j]; prev = pos;
1.6 noro 6031: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6032: }
6033: break;
6034: }
6035: for ( j = k+1; j < col && !svect[j]; j++ );
6036: if ( j == col ) break;
6037: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6038: nz = compress_array(svect,cvect,col);
6039: removecont_array((P *)cvect,nz,1);
6040: expand_array(svect,cvect,nz);
6041: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6042: }
6043: }
6044: }
6045: nz = compress_array(svect,cvect,col);
6046: removecont_array((P *)cvect,nz,1);
6047: expand_array(svect,cvect,nz);
6048: if ( DP_Print ) {
6049: fprintf(asir_out,"-"); fflush(asir_out);
6050: }
6051: return maxrs;
6052: }
1.4 noro 6053: #else
1.9 noro 6054:
1.4 noro 6055: /* direct mpz version */
6056: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6057: {
6058: int i,j,k,len,pos,prev;
6059: mpz_t *svect;
6060: mpz_t cs,cr,gcd;
6061: IndArray ivect;
6062: unsigned char *ivc;
6063: unsigned short *ivs;
6064: unsigned int *ivi;
6065: NDV redv;
6066: NMV mr;
6067: NODE rp;
6068: int maxrs;
6069: double hmag;
6070: int l;
6071:
6072: maxrs = 0;
6073: for ( i = 0; i < col && !svect0[i]; i++ );
6074: if ( i == col ) return maxrs;
6075: hmag = p_mag((P)svect0[i])*nd_scale;
6076: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
6077: for ( i = 0; i < col; i++ ) {
6078: mpz_init(svect[i]);
6079: if ( svect0[i] )
6080: mpz_set(svect[i],BDY(svect0[i]));
6081: else
6082: mpz_set_ui(svect[i],0);
6083: }
6084: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6085: for ( i = 0; i < nred; i++ ) {
6086: ivect = imat[i];
6087: k = ivect->head;
6088: if ( mpz_sgn(svect[k]) ) {
6089: maxrs = MAX(maxrs,rp0[i]->sugar);
6090: redv = nd_demand?ndv_load(rp0[i]->index)
6091: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6092: len = LEN(redv); mr = BDY(redv);
1.6 noro 6093: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6094: mpz_div(cs,svect[k],gcd);
1.6 noro 6095: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6096: mpz_neg(cs,cs);
1.9 noro 6097: if ( MUNIMPZ(cr) )
6098: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6099: else if ( !UNIMPZ(cr) )
6100: for ( j = 0; j < col; j++ ) {
6101: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6102: }
1.4 noro 6103: mpz_set_ui(svect[k],0);
6104: prev = k;
6105: switch ( ivect->width ) {
6106: case 1:
6107: ivc = ivect->index.c;
6108: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6109: pos = prev+ivc[j]; prev = pos;
1.6 noro 6110: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6111: }
6112: break;
6113: case 2:
6114: ivs = ivect->index.s;
6115: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6116: pos = prev+ivs[j]; prev = pos;
1.6 noro 6117: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6118: }
6119: break;
6120: case 4:
6121: ivi = ivect->index.i;
6122: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6123: pos = prev+ivi[j]; prev = pos;
1.6 noro 6124: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6125: }
6126: break;
6127: }
6128: for ( j = k+1; j < col && !svect[j]; j++ );
6129: if ( j == col ) break;
6130: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6131: mpz_removecont_array(svect,col);
6132: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6133: }
6134: }
6135: }
6136: mpz_removecont_array(svect,col);
6137: if ( DP_Print ) {
6138: fprintf(asir_out,"-"); fflush(asir_out);
6139: }
6140: for ( i = 0; i < col; i++ )
6141: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6142: else svect0[i] = 0;
6143: return maxrs;
6144: }
6145: #endif
1.1 noro 6146:
6147: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6148: {
6149: int i,j,k,len,pos,prev;
6150: UINT c,c1,c2,c3,up,lo,dmy;
6151: IndArray ivect;
6152: unsigned char *ivc;
6153: unsigned short *ivs;
6154: unsigned int *ivi;
6155: NDV redv;
6156: NMV mr;
6157: NODE rp;
6158: int maxrs;
6159:
6160: maxrs = 0;
6161: for ( i = 0; i < nred; i++ ) {
6162: ivect = imat[i];
6163: k = ivect->head; svect[k] %= m;
6164: if ( (c = svect[k]) != 0 ) {
6165: maxrs = MAX(maxrs,rp0[i]->sugar);
6166: c = m-c; redv = nd_ps[rp0[i]->index];
6167: len = LEN(redv); mr = BDY(redv);
6168: svect[k] = 0; prev = k;
6169: switch ( ivect->width ) {
6170: case 1:
6171: ivc = ivect->index.c;
6172: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6173: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6174: if ( c1 ) {
6175: c2 = svect[pos];
6176: DMA(c1,c,c2,up,lo);
6177: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6178: } else svect[pos] = lo;
6179: }
6180: }
6181: break;
6182: case 2:
6183: ivs = ivect->index.s;
6184: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6185: pos = prev+ivs[j]; c1 = CM(mr);
6186: prev = pos;
6187: if ( c1 ) {
6188: c2 = svect[pos];
6189: DMA(c1,c,c2,up,lo);
6190: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6191: } else svect[pos] = lo;
6192: }
6193: }
6194: break;
6195: case 4:
6196: ivi = ivect->index.i;
6197: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6198: pos = prev+ivi[j]; c1 = CM(mr);
6199: prev = pos;
6200: if ( c1 ) {
6201: c2 = svect[pos];
6202: DMA(c1,c,c2,up,lo);
6203: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6204: } else svect[pos] = lo;
6205: }
6206: }
6207: break;
6208: }
6209: }
6210: }
6211: for ( i = 0; i < col; i++ )
6212: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6213: return maxrs;
6214: }
6215:
6216: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6217: {
6218: int i,j,k,len,pos,prev;
6219: UINT c,c1,c2,c3,up,lo,dmy;
6220: IndArray ivect;
6221: unsigned char *ivc;
6222: unsigned short *ivs;
6223: unsigned int *ivi;
6224: NDV redv;
6225: NMV mr;
6226: NODE rp;
6227: int maxrs;
6228:
6229: maxrs = 0;
6230: for ( i = 0; i < nred; i++ ) {
6231: ivect = imat[i];
6232: k = ivect->head;
6233: if ( (c = svect[k]) != 0 ) {
6234: maxrs = MAX(maxrs,rp0[i]->sugar);
6235: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6236: len = LEN(redv); mr = BDY(redv);
6237: svect[k] = 0; prev = k;
6238: switch ( ivect->width ) {
6239: case 1:
6240: ivc = ivect->index.c;
6241: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6242: pos = prev+ivc[j]; prev = pos;
6243: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6244: }
6245: break;
6246: case 2:
6247: ivs = ivect->index.s;
6248: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6249: pos = prev+ivs[j]; prev = pos;
6250: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6251: }
6252: break;
6253: case 4:
6254: ivi = ivect->index.i;
6255: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6256: pos = prev+ivi[j]; prev = pos;
6257: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6258: }
6259: break;
6260: }
6261: }
6262: }
6263: return maxrs;
6264: }
6265:
6266: ND nd_add_lf(ND p1,ND p2)
6267: {
6268: int n,c,can;
6269: ND r;
6270: NM m1,m2,mr0,mr,s;
6271: Z t;
6272:
6273: if ( !p1 ) return p2;
6274: else if ( !p2 ) return p1;
6275: else {
6276: can = 0;
6277: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6278: c = DL_COMPARE(DL(m1),DL(m2));
6279: switch ( c ) {
6280: case 0:
6281: addlf(CZ(m1),CZ(m2),&t);
6282: s = m1; m1 = NEXT(m1);
6283: if ( t ) {
6284: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6285: } else {
6286: can += 2; FREENM(s);
6287: }
6288: s = m2; m2 = NEXT(m2); FREENM(s);
6289: break;
6290: case 1:
6291: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6292: break;
6293: case -1:
6294: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6295: break;
6296: }
6297: }
6298: if ( !mr0 )
6299: if ( m1 ) mr0 = m1;
6300: else if ( m2 ) mr0 = m2;
6301: else return 0;
6302: else if ( m1 ) NEXT(mr) = m1;
6303: else if ( m2 ) NEXT(mr) = m2;
6304: else NEXT(mr) = 0;
6305: BDY(p1) = mr0;
6306: SG(p1) = MAX(SG(p1),SG(p2));
6307: LEN(p1) = LEN(p1)+LEN(p2)-can;
6308: FREEND(p2);
6309: return p1;
6310: }
6311: }
6312:
6313: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6314: {
6315: int i,j,k,len,pos,prev;
6316: mpz_t c,mc,c1;
6317: IndArray ivect;
6318: unsigned char *ivc;
6319: unsigned short *ivs;
6320: unsigned int *ivi;
6321: NDV redv;
6322: NMV mr;
6323: NODE rp;
6324: int maxrs;
6325:
6326: maxrs = 0;
6327: lf_lazy = 1;
6328: for ( i = 0; i < nred; i++ ) {
6329: ivect = imat[i];
6330: k = ivect->head;
6331: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6332: if ( mpz_sgn(svect[k]) ) {
6333: maxrs = MAX(maxrs,rp0[i]->sugar);
6334: mpz_neg(svect[k],svect[k]);
6335: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6336: len = LEN(redv); mr = BDY(redv);
6337: prev = k;
6338: switch ( ivect->width ) {
6339: case 1:
6340: ivc = ivect->index.c;
6341: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6342: pos = prev+ivc[j]; prev = pos;
6343: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6344: }
6345: break;
6346: case 2:
6347: ivs = ivect->index.s;
6348: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6349: pos = prev+ivs[j]; prev = pos;
6350: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6351: }
6352: break;
6353: case 4:
6354: ivi = ivect->index.i;
6355: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6356: pos = prev+ivi[j]; prev = pos;
6357: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6358: }
6359: break;
6360: }
6361: mpz_set_ui(svect[k],0);
6362: }
6363: }
6364: lf_lazy=0;
6365: for ( i = 0; i < col; i++ ) {
6366: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6367: }
6368: return maxrs;
6369: }
6370:
6371: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6372: {
6373: int i,j,k,l,rank,s;
6374: mpz_t a,a1,inv;
6375: mpz_t *t,*pivot,*pk;
6376: mpz_t **mat;
6377: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6378: int size,size1;
6379:
6380: mpz_init(inv);
6381: mpz_init(a);
6382: mat = (mpz_t **)mat0;
6383: size = 0;
6384: for ( rank = 0, j = 0; j < col; j++ ) {
6385: for ( i = rank; i < row; i++ ) {
6386: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6387: }
6388: for ( i = rank; i < row; i++ )
6389: if ( mpz_sgn(mat[i][j]) )
6390: break;
6391: if ( i == row ) {
6392: colstat[j] = 0;
6393: continue;
6394: } else
6395: colstat[j] = 1;
6396: if ( i != rank ) {
6397: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6398: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6399: }
6400: pivot = mat[rank];
6401: s = sugar[rank];
6402: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6403: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6404: if ( mpz_sgn(*pk) ) {
6405: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6406: }
6407: for ( i = rank+1; i < row; i++ ) {
6408: t = mat[i];
6409: if ( mpz_sgn(t[j]) ) {
6410: sugar[i] = MAX(sugar[i],s);
6411: mpz_neg(a,t[j]);
6412: red_by_vect_lf(t+j,pivot+j,a,col-j);
6413: }
6414: }
6415: rank++;
6416: }
6417: for ( j = col-1, l = rank-1; j >= 0; j-- )
6418: if ( colstat[j] ) {
6419: pivot = mat[l];
6420: s = sugar[l];
6421: for ( k = j; k < col; k++ )
6422: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6423: for ( i = 0; i < l; i++ ) {
6424: t = mat[i];
6425: if ( mpz_sgn(t[j]) ) {
6426: sugar[i] = MAX(sugar[i],s);
6427: mpz_neg(a,t[j]);
6428: red_by_vect_lf(t+j,pivot+j,a,col-j);
6429: }
6430: }
6431: l--;
6432: }
6433: for ( j = 0, l = 0; l < rank; j++ )
6434: if ( colstat[j] ) {
6435: t = mat[l];
6436: for ( k = j; k < col; k++ ) {
6437: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6438: }
6439: l++;
6440: }
6441: return rank;
6442: }
6443:
6444:
6445: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6446: {
6447: int j,k,len;
6448: UINT *p;
6449: UINT c;
6450: NDV r;
6451: NMV mr0,mr;
6452:
6453: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6454: if ( !len ) return 0;
6455: else {
6456: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6457: #if 0
6458: ndv_alloc += nmv_adv*len;
6459: #endif
6460: mr = mr0;
6461: p = s0vect;
6462: for ( j = k = 0; j < col; j++, p += nd_wpd )
6463: if ( !rhead[j] ) {
6464: if ( (c = vect[k++]) != 0 ) {
6465: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6466: }
6467: }
6468: MKNDV(nd_nvar,mr0,len,r);
6469: return r;
6470: }
6471: }
6472:
6473: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6474: {
6475: int j,k,len;
6476: UINT *p;
6477: NDV r;
6478: NMV mr0,mr;
6479:
6480: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6481: if ( !len ) return 0;
6482: else {
6483: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6484: mr = mr0;
6485: p = s0vect;
6486: for ( j = 0; j < col; j++, p += nd_wpd )
6487: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6488: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6489: }
6490: MKNDV(nd_nvar,mr0,len,r);
6491: return r;
6492: }
6493: }
6494:
6495: /* for preprocessed vector */
6496:
6497: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6498: {
1.6 noro 6499: int j,k,len;
6500: UINT *p;
6501: Z c;
6502: NDV r;
6503: NMV mr0,mr;
1.1 noro 6504:
1.6 noro 6505: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6506: if ( !len ) return 0;
6507: else {
6508: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6509: #if 0
1.6 noro 6510: ndv_alloc += nmv_adv*len;
1.1 noro 6511: #endif
1.6 noro 6512: mr = mr0;
6513: p = s0vect;
6514: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6515: if ( !rhead[j] ) {
6516: if ( (c = vect[k++]) != 0 ) {
6517: if ( !INT(c) )
6518: error("vect_to_ndv_q : components must be integers");
6519: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6520: }
6521: }
1.1 noro 6522: }
1.6 noro 6523: MKNDV(nd_nvar,mr0,len,r);
6524: return r;
6525: }
1.1 noro 6526: }
6527:
6528: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6529: {
6530: int j,k,len;
6531: UINT *p;
6532: mpz_t c;
6533: NDV r;
6534: NMV mr0,mr;
6535:
6536: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6537: if ( !len ) return 0;
6538: else {
6539: mr0 = (NMV)MALLOC(nmv_adv*len);
6540: #if 0
6541: ndv_alloc += nmv_adv*len;
6542: #endif
6543: mr = mr0;
6544: p = s0vect;
6545: for ( j = k = 0; j < col; j++, p += nd_wpd )
6546: if ( !rhead[j] ) {
6547: c[0] = vect[k++][0];
6548: if ( mpz_sgn(c) ) {
6549: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6550: }
6551: }
6552: MKNDV(nd_nvar,mr0,len,r);
6553: return r;
6554: }
6555: }
6556:
6557: /* for plain vector */
6558:
6559: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6560: {
6561: int j,k,len;
6562: UINT *p;
6563: Z c;
6564: NDV r;
6565: NMV mr0,mr;
6566:
6567: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6568: if ( !len ) return 0;
6569: else {
6570: mr0 = (NMV)MALLOC(nmv_adv*len);
6571: #if 0
6572: ndv_alloc += nmv_adv*len;
6573: #endif
6574: mr = mr0;
6575: p = s0vect;
6576: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6577: if ( (c = vect[k]) != 0 ) {
6578: if ( !INT(c) )
1.6 noro 6579: error("plain_vect_to_ndv_q : components must be integers");
6580: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6581: }
6582: MKNDV(nd_nvar,mr0,len,r);
6583: return r;
6584: }
6585: }
6586:
6587: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6588: {
6589: ND_pairs t;
6590: NODE sp0,sp;
6591: int stat;
6592: ND spol;
6593:
6594: for ( t = l; t; t = NEXT(t) ) {
6595: stat = nd_sp(m,trace,t,&spol);
6596: if ( !stat ) return 0;
6597: if ( spol ) {
6598: add_pbucket_symbolic(bucket,spol);
6599: }
6600: }
6601: return 1;
6602: }
6603:
6604: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6605: {
6606: NODE rp0,rp;
6607: NM mul,head,s0,s;
6608: int index,col,i,sugar;
6609: RHist h;
6610: UINT *s0v,*p;
6611: NM_ind_pair pair;
6612: ND red;
6613: NDV *ps;
6614:
6615: s0 = 0; rp0 = 0; col = 0;
6616: if ( nd_demand )
6617: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6618: else
6619: ps = trace?nd_ps_trace:nd_ps;
6620: while ( 1 ) {
6621: head = remove_head_pbucket_symbolic(bucket);
6622: if ( !head ) break;
6623: if ( !s0 ) s0 = head;
6624: else NEXT(s) = head;
6625: s = head;
6626: index = ndl_find_reducer(DL(head));
6627: if ( index >= 0 ) {
6628: h = nd_psh[index];
6629: NEWNM(mul);
6630: ndl_sub(DL(head),DL(h),DL(mul));
6631: if ( ndl_check_bound2(index,DL(mul)) )
6632: return 0;
6633: sugar = TD(DL(mul))+SG(ps[index]);
6634: MKNM_ind_pair(pair,mul,index,sugar);
6635: red = ndv_mul_nm_symbolic(mul,ps[index]);
6636: add_pbucket_symbolic(bucket,nd_remove_head(red));
6637: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6638: }
6639: col++;
6640: }
6641: if ( rp0 ) NEXT(rp) = 0;
6642: NEXT(s) = 0;
6643: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6644: for ( i = 0, p = s0v, s = s0; i < col;
6645: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6646: *s0vect = s0v;
6647: *r = rp0;
6648: return col;
6649: }
6650:
6651: void print_ndp(ND_pairs l)
6652: {
6653: ND_pairs t;
6654:
6655: for ( t = l; t; t = NEXT(t) )
6656: printf("[%d,%d] ",t->i1,t->i2);
6657: printf("\n");
6658: }
6659:
6660: NODE nd_f4(int m,int checkonly,int **indp)
6661: {
6662: int i,nh,stat,index,f4red;
6663: NODE r,g,tn0,tn,node;
6664: ND_pairs d,l,t,ll0,ll,lh;
6665: LIST l0,l1;
6666: ND spol,red;
6667: NDV nf,redv;
6668: NM s0,s;
6669: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6670: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6671: UINT c;
6672: UINT **spmat;
6673: UINT *s0vect,*svect,*p,*v;
6674: int *colstat;
6675: IndArray *imat;
6676: int *rhead;
6677: int spcol,sprow;
6678: int sugar,sugarh;
6679: PGeoBucket bucket;
6680: struct oEGT eg0,eg1,eg_f4;
6681: Z i1,i2,sugarq;
1.12 ! noro 6682:
! 6683: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6684: #if 0
6685: ndv_alloc = 0;
6686: #endif
1.11 noro 6687: Nf4_red=0;
1.1 noro 6688: g = 0; d = 0;
6689: for ( i = 0; i < nd_psn; i++ ) {
6690: d = update_pairs(d,g,i,0);
6691: g = update_base(g,i);
6692: }
6693: nzlist = 0;
6694: nzlist_t = nd_nzlist;
6695: f4red = 1;
6696: nd_last_nonzero = 0;
6697: while ( d ) {
6698: get_eg(&eg0);
6699: l = nd_minsugarp(d,&d);
6700: sugar = nd_sugarweight?l->sugar2:SG(l);
6701: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6702: if ( nzlist_t ) {
6703: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6704: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6705: tn = BDY((LIST)ARG1(node));
6706: if ( !tn ) {
6707: nzlist_t = NEXT(nzlist_t);
6708: continue;
6709: }
6710: /* tn = [[i1,i2],...] */
6711: lh = nd_ipairtospair(tn);
6712: }
6713: bucket = create_pbucket();
6714: stat = nd_sp_f4(m,0,l,bucket);
6715: if ( !stat ) {
6716: for ( t = l; NEXT(t); t = NEXT(t) );
6717: NEXT(t) = d; d = l;
6718: d = nd_reconstruct(0,d);
6719: continue;
6720: }
6721: if ( bucket->m < 0 ) continue;
6722: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6723: if ( !col ) {
6724: for ( t = l; NEXT(t); t = NEXT(t) );
6725: NEXT(t) = d; d = l;
6726: d = nd_reconstruct(0,d);
6727: continue;
6728: }
1.12 ! noro 6729: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6730: if ( DP_Print )
1.6 noro 6731: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6732: sugar,eg_f4.exectime);
1.1 noro 6733: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6734: if ( checkonly && nflist ) return 0;
6735: /* adding new bases */
6736: if ( nflist ) nd_last_nonzero = f4red;
6737: for ( r = nflist; r; r = NEXT(r) ) {
6738: nf = (NDV)BDY(r);
6739: ndv_removecont(m,nf);
6740: if ( !m && nd_nalg ) {
6741: ND nf1;
6742:
6743: nf1 = ndvtond(m,nf);
6744: nd_monic(0,&nf1);
6745: nd_removecont(m,nf1);
6746: nf = ndtondv(m,nf1);
6747: }
6748: nh = ndv_newps(m,nf,0,1);
6749: d = update_pairs(d,g,nh,0);
6750: g = update_base(g,nh);
6751: }
6752: if ( DP_Print ) {
6753: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6754: }
6755: if ( nd_gentrace ) {
6756: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6757: NEXTNODE(tn0,tn);
1.6 noro 6758: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6759: node = mknode(2,i1,i2); MKLIST(l0,node);
6760: BDY(tn) = l0;
6761: }
6762: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6763: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6764: MKNODE(node,l1,nzlist); nzlist = node;
6765: }
6766: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6767: f4red++;
6768: if ( nd_f4red && f4red > nd_f4red ) break;
6769: if ( nd_rank0 && !nflist ) break;
6770: }
6771: if ( nd_gentrace ) {
6772: MKLIST(l0,reverse_node(nzlist));
6773: MKNODE(nd_alltracelist,l0,0);
6774: }
6775: #if 0
6776: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6777: #endif
1.12 ! noro 6778: if ( DP_Print ) {
! 6779: fprintf(asir_out,"number of red=%d,",Nf4_red);
! 6780: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
! 6781: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
! 6782: }
1.1 noro 6783: conv_ilist(nd_demand,0,g,indp);
6784: return g;
6785: }
6786:
6787: NODE nd_f4_trace(int m,int **indp)
6788: {
6789: int i,nh,stat,index;
6790: NODE r,g;
6791: ND_pairs d,l,l0,t;
6792: ND spol,red;
6793: NDV nf,redv,nfqv,nfv;
6794: NM s0,s;
6795: NODE rp0,srp0,nflist;
6796: int nsp,nred,col,rank,len,k,j,a;
6797: UINT c;
6798: UINT **spmat;
6799: UINT *s0vect,*svect,*p,*v;
6800: int *colstat;
6801: IndArray *imat;
6802: int *rhead;
6803: int spcol,sprow;
6804: int sugar;
6805: PGeoBucket bucket;
6806: struct oEGT eg0,eg1,eg_f4;
6807:
6808: g = 0; d = 0;
6809: for ( i = 0; i < nd_psn; i++ ) {
6810: d = update_pairs(d,g,i,0);
6811: g = update_base(g,i);
6812: }
6813: while ( d ) {
6814: get_eg(&eg0);
6815: l = nd_minsugarp(d,&d);
6816: sugar = SG(l);
6817: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6818: bucket = create_pbucket();
6819: stat = nd_sp_f4(m,0,l,bucket);
6820: if ( !stat ) {
6821: for ( t = l; NEXT(t); t = NEXT(t) );
6822: NEXT(t) = d; d = l;
6823: d = nd_reconstruct(1,d);
6824: continue;
6825: }
6826: if ( bucket->m < 0 ) continue;
6827: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6828: if ( !col ) {
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: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6835: if ( DP_Print )
1.3 noro 6836: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 6837: sugar,eg_f4.exectime);
1.1 noro 6838: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6839: if ( !l0 ) continue;
6840: l = l0;
6841:
6842: /* over Q */
6843: bucket = create_pbucket();
6844: stat = nd_sp_f4(0,1,l,bucket);
6845: if ( !stat ) {
6846: for ( t = l; NEXT(t); t = NEXT(t) );
6847: NEXT(t) = d; d = l;
6848: d = nd_reconstruct(1,d);
6849: continue;
6850: }
6851: if ( bucket->m < 0 ) continue;
6852: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6853: if ( !col ) {
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: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6860: /* adding new bases */
6861: for ( r = nflist; r; r = NEXT(r) ) {
6862: nfqv = (NDV)BDY(r);
6863: ndv_removecont(0,nfqv);
1.6 noro 6864: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 6865: if ( nd_nalg ) {
6866: ND nf1;
6867:
6868: nf1 = ndvtond(m,nfqv);
6869: nd_monic(0,&nf1);
6870: nd_removecont(0,nf1);
6871: nfqv = ndtondv(0,nf1); nd_free(nf1);
6872: }
6873: nfv = ndv_dup(0,nfqv);
6874: ndv_mod(m,nfv);
6875: ndv_removecont(m,nfv);
6876: nh = ndv_newps(0,nfv,nfqv,1);
6877: d = update_pairs(d,g,nh,0);
6878: g = update_base(g,nh);
6879: }
6880: }
6881: #if 0
6882: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6883: #endif
6884: conv_ilist(nd_demand,1,g,indp);
6885: return g;
6886: }
6887:
6888: int rref(matrix mat,int *sugar)
6889: {
6890: int row,col,i,j,k,l,s,wcol,wj;
6891: unsigned long bj;
6892: unsigned long **a;
6893: unsigned long *ai,*ak,*as,*t;
6894: int *pivot;
6895:
6896: row = mat->row;
6897: col = mat->col;
6898: a = mat->a;
6899: wcol = (col+BLEN-1)/BLEN;
6900: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6901: i = 0;
6902: for ( j = 0; j < col; j++ ) {
6903: wj = j/BLEN; bj = 1L<<(j%BLEN);
6904: for ( k = i; k < row; k++ )
6905: if ( a[k][wj] & bj ) break;
6906: if ( k == row ) continue;
6907: pivot[i] = j;
6908: if ( k != i ) {
6909: t = a[i]; a[i] = a[k]; a[k] = t;
6910: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6911: }
6912: ai = a[i];
6913: for ( k = i+1; k < row; k++ ) {
6914: ak = a[k];
6915: if ( ak[wj] & bj ) {
6916: for ( l = wj; l < wcol; l++ )
6917: ak[l] ^= ai[l];
6918: sugar[k] = MAX(sugar[k],sugar[i]);
6919: }
6920: }
6921: i++;
6922: }
6923: for ( k = i-1; k >= 0; k-- ) {
6924: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6925: ak = a[k];
6926: for ( s = 0; s < k; s++ ) {
6927: as = a[s];
6928: if ( as[wj] & bj ) {
6929: for ( l = wj; l < wcol; l++ )
6930: as[l] ^= ak[l];
6931: sugar[s] = MAX(sugar[s],sugar[k]);
6932: }
6933: }
6934: }
6935: return i;
6936: }
6937:
6938: void print_matrix(matrix mat)
6939: {
6940: int row,col,i,j;
6941: unsigned long *ai;
6942:
6943: row = mat->row;
6944: col = mat->col;
6945: printf("%d x %d\n",row,col);
6946: for ( i = 0; i < row; i++ ) {
6947: ai = mat->a[i];
6948: for ( j = 0; j < col; j++ ) {
6949: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6950: else putchar('0');
6951: }
6952: putchar('\n');
6953: }
6954: }
6955:
6956: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6957:
6958: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6959: {
6960: int row,col,wcol,wj,i,j;
6961: unsigned long bj;
6962: unsigned long *ai;
6963: unsigned long **a;
6964: int len;
6965: int *pos;
6966:
6967: row = mat->row;
6968: col = mat->col;
6969: wcol = (col+BLEN-1)/BLEN;
6970: pos = (int *)MALLOC(wcol*sizeof(int));
6971: bzero(pos,wcol*sizeof(int));
6972: for ( i = j = 0; i < wcol; i++ )
6973: if ( v[i] ) pos[j++] = i;;
6974: len = j;
6975: wj = rhead/BLEN;
6976: bj = 1L<<rhead%BLEN;
6977: a = mat->a;
6978: for ( i = 0; i < row; i++ ) {
6979: ai = a[i];
6980: if ( ai[wj]&bj ) {
6981: for ( j = 0; j < len; j++ )
6982: ai[pos[j]] ^= v[pos[j]];
6983: sugar[i] = MAX(sugar[i],rsugar);
6984: }
6985: }
6986: }
6987:
6988: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
6989: {
6990: int nsp,nred,i,i0,k,rank,row;
6991: NODE r0,rp;
6992: ND_pairs sp;
6993: ND spol;
6994: NM_ind_pair rt;
6995: int *s0hash;
6996: UINT *s;
6997: int *pivot,*sugar,*head;
6998: matrix mat;
6999: NM m;
7000: NODE r;
7001: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7002: int rhead,rsugar,size;
7003: unsigned long *v;
7004:
7005: get_eg(&eg0);
7006: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7007: nred = length(rp0);
7008: mat = alloc_matrix(nsp,col);
7009: s0hash = (int *)MALLOC(col*sizeof(int));
7010: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7011: s0hash[i] = ndl_hash_value(s);
7012:
7013: sugar = (int *)MALLOC(nsp*sizeof(int));
7014: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7015: nd_sp(2,0,sp,&spol);
7016: if ( spol ) {
7017: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7018: sugar[i] = SG(spol);
7019: i++;
7020: }
7021: }
7022: mat->row = i;
7023: if ( DP_Print ) {
7024: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7025: }
7026: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7027: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7028: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7029: rt = (NM_ind_pair)BDY(rp);
7030: bzero(v,size);
7031: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7032: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7033: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7034: }
7035:
7036: get_eg(&eg1);
7037: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7038: rank = rref(mat,sugar);
7039:
7040: for ( i = 0, r0 = 0; i < rank; i++ ) {
7041: NEXTNODE(r0,r);
7042: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7043: SG((NDV)BDY(r)) = sugar[i];
7044: }
7045: if ( r0 ) NEXT(r) = 0;
7046: get_eg(&eg2);
7047: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7048: if ( DP_Print ) {
7049: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7050: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7051: fflush(asir_out);
7052: }
7053: return r0;
7054: }
7055:
7056:
7057: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7058: {
7059: IndArray *imat;
1.11 noro 7060: int nsp,nred,i,start;
1.1 noro 7061: int *rhead;
7062: NODE r0,rp;
7063: ND_pairs sp;
7064: NM_ind_pair *rvect;
7065: UINT *s;
7066: int *s0hash;
1.11 noro 7067: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7068:
7069: if ( m == 2 && nd_rref2 )
7070: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7071:
7072: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7073: nred = length(rp0);
7074: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7075: rhead = (int *)MALLOC(col*sizeof(int));
7076: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7077:
7078: /* construction of index arrays */
1.11 noro 7079: get_eg(&eg0);
1.1 noro 7080: if ( DP_Print ) {
1.11 noro 7081: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7082: fflush(asir_out);
1.1 noro 7083: }
7084: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7085: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7086: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7087: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7088: rhead[imat[i]->head] = 1;
1.11 noro 7089: start = imat[i]->head;
7090: }
1.12 ! noro 7091: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7092: if ( DP_Print ) {
7093: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7094: fflush(asir_out);
1.1 noro 7095: }
7096: if ( m > 0 )
1.7 noro 7097: #if SIZEOF_LONG==8
1.1 noro 7098: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7099: #else
7100: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7101: #endif
7102: else if ( m == -1 )
7103: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7104: else if ( m == -2 )
7105: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7106: else
7107: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7108: return r0;
7109: }
7110:
7111: /* for Fp, 2<=p<2^16 */
7112:
7113: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7114: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7115: {
7116: int spcol,sprow,a;
7117: int i,j,k,l,rank;
7118: NODE r0,r;
7119: ND_pairs sp;
7120: ND spol;
7121: UINT **spmat;
7122: UINT *svect,*v;
7123: int *colstat;
7124: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7125: int maxrs;
7126: int *spsugar;
7127: ND_pairs *spactive;
7128:
7129: spcol = col-nred;
7130: get_eg(&eg0);
7131: /* elimination (1st step) */
7132: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7133: svect = (UINT *)MALLOC(col*sizeof(UINT));
7134: spsugar = (int *)MALLOC(nsp*sizeof(int));
7135: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7136: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7137: nd_sp(m,0,sp,&spol);
7138: if ( !spol ) continue;
7139: nd_to_vect(m,s0vect,col,spol,svect);
7140: if ( m == -1 )
7141: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7142: else
7143: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7144: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7145: if ( i < col ) {
7146: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7147: for ( j = k = 0; j < col; j++ )
7148: if ( !rhead[j] ) v[k++] = svect[j];
7149: spsugar[sprow] = MAX(maxrs,SG(spol));
7150: if ( nz )
7151: spactive[sprow] = sp;
7152: sprow++;
7153: }
7154: nd_free(spol);
7155: }
7156: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7157: if ( DP_Print ) {
1.5 noro 7158: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7159: fflush(asir_out);
7160: }
7161: /* free index arrays */
7162: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7163:
7164: /* elimination (2nd step) */
7165: colstat = (int *)MALLOC(spcol*sizeof(int));
7166: if ( m == -1 )
7167: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7168: else
7169: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7170: r0 = 0;
7171: for ( i = 0; i < rank; i++ ) {
7172: NEXTNODE(r0,r); BDY(r) =
7173: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7174: SG((NDV)BDY(r)) = spsugar[i];
7175: GCFREE(spmat[i]);
7176: }
7177: if ( r0 ) NEXT(r) = 0;
7178:
7179: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7180: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7181: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7182: if ( DP_Print ) {
1.5 noro 7183: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7184: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7185: nsp,nred,sprow,spcol,rank);
1.5 noro 7186: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7187: }
7188: if ( nz ) {
7189: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7190: if ( rank > 0 ) {
7191: NEXT(spactive[rank-1]) = 0;
7192: *nz = spactive[0];
7193: } else
7194: *nz = 0;
7195: }
7196: return r0;
7197: }
7198:
7199:
7200: /* for small finite fields */
7201:
7202: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7203: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7204: {
7205: int spcol,sprow,a;
7206: int i,j,k,l,rank;
7207: NODE r0,r;
7208: ND_pairs sp;
7209: ND spol;
7210: UINT **spmat;
7211: UINT *svect,*v;
7212: int *colstat;
7213: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7214: int maxrs;
7215: int *spsugar;
7216: ND_pairs *spactive;
7217:
7218: spcol = col-nred;
7219: get_eg(&eg0);
7220: /* elimination (1st step) */
7221: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7222: svect = (UINT *)MALLOC(col*sizeof(UINT));
7223: spsugar = (int *)MALLOC(nsp*sizeof(int));
7224: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7225: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7226: nd_sp(m,0,sp,&spol);
7227: if ( !spol ) continue;
7228: nd_to_vect(m,s0vect,col,spol,svect);
7229: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7230: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7231: if ( i < col ) {
7232: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7233: for ( j = k = 0; j < col; j++ )
7234: if ( !rhead[j] ) v[k++] = svect[j];
7235: spsugar[sprow] = MAX(maxrs,SG(spol));
7236: if ( nz )
7237: spactive[sprow] = sp;
7238: sprow++;
7239: }
7240: nd_free(spol);
7241: }
7242: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7243: if ( DP_Print ) {
1.5 noro 7244: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7245: fflush(asir_out);
7246: }
7247: /* free index arrays */
7248: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7249:
7250: /* elimination (2nd step) */
7251: colstat = (int *)MALLOC(spcol*sizeof(int));
7252: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7253: r0 = 0;
7254: for ( i = 0; i < rank; i++ ) {
7255: NEXTNODE(r0,r); BDY(r) =
7256: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7257: SG((NDV)BDY(r)) = spsugar[i];
7258: GCFREE(spmat[i]);
7259: }
7260: if ( r0 ) NEXT(r) = 0;
7261:
7262: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7263: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7264: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7265: if ( DP_Print ) {
1.5 noro 7266: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7267: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7268: nsp,nred,sprow,spcol,rank);
1.5 noro 7269: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7270: }
7271: if ( nz ) {
7272: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7273: if ( rank > 0 ) {
7274: NEXT(spactive[rank-1]) = 0;
7275: *nz = spactive[0];
7276: } else
7277: *nz = 0;
7278: }
7279: return r0;
7280: }
7281:
7282: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7283: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7284: {
7285: int spcol,sprow,a;
7286: int i,j,k,l,rank;
7287: NODE r0,r;
7288: ND_pairs sp;
7289: ND spol;
7290: mpz_t **spmat;
7291: mpz_t *svect,*v;
7292: int *colstat;
7293: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7294: int maxrs;
7295: int *spsugar;
7296: pointer *w;
7297:
7298: spcol = col-nred;
7299: get_eg(&eg0);
7300: /* elimination (1st step) */
7301: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7302: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7303: spsugar = (int *)MALLOC(nsp*sizeof(int));
7304: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7305: nd_sp(m,trace,sp,&spol);
7306: if ( !spol ) continue;
7307: nd_to_vect_lf(s0vect,col,spol,svect);
7308: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7309: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7310: if ( i < col ) {
7311: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7312: for ( j = k = 0; j < col; j++ )
7313: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7314: spsugar[sprow] = MAX(maxrs,SG(spol));
7315: sprow++;
7316: }
7317: /* nd_free(spol); */
7318: }
7319: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7320: if ( DP_Print ) {
1.5 noro 7321: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7322: fflush(asir_out);
7323: }
7324: /* free index arrays */
7325: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7326:
7327: /* elimination (2nd step) */
7328: colstat = (int *)MALLOC(spcol*sizeof(int));
7329: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7330: w = (pointer *)MALLOC(rank*sizeof(pointer));
7331: for ( i = 0; i < rank; i++ ) {
7332: #if 0
7333: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7334: SG((NDV)w[rank-i-1]) = spsugar[i];
7335: #else
7336: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7337: SG((NDV)w[i]) = spsugar[i];
7338: #endif
7339: /* GCFREE(spmat[i]); */
7340:
7341: }
7342: #if 0
7343: qsort(w,rank,sizeof(NDV),
7344: (int (*)(const void *,const void *))ndv_compare);
7345: #endif
7346: r0 = 0;
7347: for ( i = 0; i < rank; i++ ) {
7348: NEXTNODE(r0,r); BDY(r) = w[i];
7349: }
7350: if ( r0 ) NEXT(r) = 0;
7351:
7352: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7353: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7354: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7355: if ( DP_Print ) {
1.5 noro 7356: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7357: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7358: nsp,nred,sprow,spcol,rank);
1.5 noro 7359: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7360: }
7361: return r0;
7362: }
7363:
7364: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7365: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7366: {
7367: int spcol,sprow,a;
7368: int i,j,k,l,rank;
7369: NODE r0,r;
7370: ND_pairs sp;
7371: ND spol;
7372: Z **spmat;
7373: Z *svect,*v;
7374: int *colstat;
7375: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7376: int maxrs;
7377: int *spsugar;
7378: pointer *w;
7379:
7380: spcol = col-nred;
7381: get_eg(&eg0);
7382: /* elimination (1st step) */
7383: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7384: svect = (Z *)MALLOC(col*sizeof(Q));
7385: spsugar = (int *)MALLOC(nsp*sizeof(int));
7386: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7387: nd_sp(0,trace,sp,&spol);
7388: if ( !spol ) continue;
7389: nd_to_vect_q(s0vect,col,spol,svect);
7390: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7391: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7392: if ( i < col ) {
7393: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7394: for ( j = k = 0; j < col; j++ )
7395: if ( !rhead[j] ) v[k++] = svect[j];
7396: spsugar[sprow] = MAX(maxrs,SG(spol));
7397: sprow++;
7398: }
7399: /* nd_free(spol); */
7400: }
7401: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7402: if ( DP_Print ) {
1.5 noro 7403: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7404: fflush(asir_out);
7405: }
7406: /* free index arrays */
7407: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7408:
7409: /* elimination (2nd step) */
7410: colstat = (int *)MALLOC(spcol*sizeof(int));
7411: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7412: w = (pointer *)MALLOC(rank*sizeof(pointer));
7413: for ( i = 0; i < rank; i++ ) {
7414: #if 0
7415: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7416: SG((NDV)w[rank-i-1]) = spsugar[i];
7417: #else
7418: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7419: SG((NDV)w[i]) = spsugar[i];
7420: #endif
7421: /* GCFREE(spmat[i]); */
7422: }
7423: #if 0
7424: qsort(w,rank,sizeof(NDV),
7425: (int (*)(const void *,const void *))ndv_compare);
7426: #endif
7427: r0 = 0;
7428: for ( i = 0; i < rank; i++ ) {
7429: NEXTNODE(r0,r); BDY(r) = w[i];
7430: }
7431: if ( r0 ) NEXT(r) = 0;
7432:
7433: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7434: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7435: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7436: if ( DP_Print ) {
1.5 noro 7437: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7438: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7439: nsp,nred,sprow,spcol,rank);
1.5 noro 7440: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7441: }
7442: return r0;
7443: }
7444:
7445: FILE *nd_write,*nd_read;
7446:
7447: void nd_send_int(int a) {
7448: write_int(nd_write,(unsigned int *)&a);
7449: }
7450:
7451: void nd_send_intarray(int *p,int len) {
7452: write_intarray(nd_write,(unsigned int *)p,len);
7453: }
7454:
7455: int nd_recv_int() {
7456: int a;
7457:
7458: read_int(nd_read,(unsigned int *)&a);
7459: return a;
7460: }
7461:
7462: void nd_recv_intarray(int *p,int len) {
7463: read_intarray(nd_read,(unsigned int *)p,len);
7464: }
7465:
7466: void nd_send_ndv(NDV p) {
7467: int len,i;
7468: NMV m;
7469:
7470: if ( !p ) nd_send_int(0);
7471: else {
7472: len = LEN(p);
7473: nd_send_int(len);
7474: m = BDY(p);
7475: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7476: nd_send_int(CM(m));
7477: nd_send_intarray((int *)DL(m),nd_wpd);
7478: }
7479: }
7480: }
7481:
7482: void nd_send_nd(ND p) {
7483: int len,i;
7484: NM m;
7485:
7486: if ( !p ) nd_send_int(0);
7487: else {
7488: len = LEN(p);
7489: nd_send_int(len);
7490: m = BDY(p);
7491: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7492: nd_send_int(CM(m));
7493: nd_send_intarray((int *)DL(m),nd_wpd);
7494: }
7495: }
7496: }
7497:
7498: NDV nd_recv_ndv()
7499: {
7500: int len,i;
7501: NMV m,m0;
7502: NDV r;
7503:
7504: len = nd_recv_int();
7505: if ( !len ) return 0;
7506: else {
7507: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7508: #if 0
7509: ndv_alloc += len*nmv_adv;
7510: #endif
7511: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7512: CM(m) = nd_recv_int();
7513: nd_recv_intarray((int *)DL(m),nd_wpd);
7514: }
7515: MKNDV(nd_nvar,m0,len,r);
7516: return r;
7517: }
7518: }
7519:
7520: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7521: {
7522: int i,j,t,c,rank,inv;
7523: int *ci,*ri;
7524: Z dn;
7525: MAT m,nm;
7526:
7527: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7528: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7529: for ( i = 0; i < row; i++ )
7530: for ( j = 0; j < col; j++ )
7531: mat0[i][j] = 0;
7532: c = col-rank;
7533: for ( i = 0; i < rank; i++ ) {
7534: mat0[i][ri[i]] = dn;
7535: for ( j = 0; j < c; j++ )
7536: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7537: }
7538: return rank;
7539: }
7540:
7541: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7542: {
7543: int i,j,k,l,inv,a,rank,s;
7544: unsigned int *t,*pivot,*pk;
7545: unsigned int **mat;
7546: ND_pairs pair;
7547:
7548: mat = (unsigned int **)mat0;
7549: for ( rank = 0, j = 0; j < col; j++ ) {
7550: for ( i = rank; i < row; i++ )
7551: mat[i][j] %= md;
7552: for ( i = rank; i < row; i++ )
7553: if ( mat[i][j] )
7554: break;
7555: if ( i == row ) {
7556: colstat[j] = 0;
7557: continue;
7558: } else
7559: colstat[j] = 1;
7560: if ( i != rank ) {
7561: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7562: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7563: if ( spactive ) {
7564: pair = spactive[i]; spactive[i] = spactive[rank];
7565: spactive[rank] = pair;
7566: }
7567: }
7568: pivot = mat[rank];
7569: s = sugar[rank];
7570: inv = invm(pivot[j],md);
7571: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7572: if ( *pk ) {
7573: if ( *pk >= (unsigned int)md )
7574: *pk %= md;
7575: DMAR(*pk,inv,0,md,*pk)
7576: }
7577: for ( i = rank+1; i < row; i++ ) {
7578: t = mat[i];
7579: if ( (a = t[j]) != 0 ) {
7580: sugar[i] = MAX(sugar[i],s);
7581: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7582: }
7583: }
7584: rank++;
7585: }
7586: for ( j = col-1, l = rank-1; j >= 0; j-- )
7587: if ( colstat[j] ) {
7588: pivot = mat[l];
7589: s = sugar[l];
7590: for ( i = 0; i < l; i++ ) {
7591: t = mat[i];
7592: t[j] %= md;
7593: if ( (a = t[j]) != 0 ) {
7594: sugar[i] = MAX(sugar[i],s);
7595: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7596: }
7597: }
7598: l--;
7599: }
7600: for ( j = 0, l = 0; l < rank; j++ )
7601: if ( colstat[j] ) {
7602: t = mat[l];
7603: for ( k = j; k < col; k++ )
7604: if ( t[k] >= (unsigned int)md )
7605: t[k] %= md;
7606: l++;
7607: }
7608: return rank;
7609: }
7610:
7611:
1.7 noro 7612: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7613: {
1.7 noro 7614: int i,j,k,l,inv,a,rank,s;
7615: unsigned int *t,*pivot,*pk;
7616: unsigned int **mat;
7617:
7618: mat = (unsigned int **)mat0;
7619: for ( rank = 0, j = 0; j < col; j++ ) {
7620: for ( i = rank; i < row; i++ )
7621: if ( mat[i][j] )
7622: break;
7623: if ( i == row ) {
7624: colstat[j] = 0;
7625: continue;
7626: } else
7627: colstat[j] = 1;
7628: if ( i != rank ) {
7629: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7630: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7631: }
7632: pivot = mat[rank];
7633: s = sugar[rank];
7634: inv = _invsf(pivot[j]);
7635: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7636: if ( *pk )
7637: *pk = _mulsf(*pk,inv);
7638: for ( i = rank+1; i < row; i++ ) {
7639: t = mat[i];
7640: if ( (a = t[j]) != 0 ) {
7641: sugar[i] = MAX(sugar[i],s);
7642: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7643: }
7644: }
7645: rank++;
7646: }
7647: for ( j = col-1, l = rank-1; j >= 0; j-- )
7648: if ( colstat[j] ) {
7649: pivot = mat[l];
7650: s = sugar[l];
7651: for ( i = 0; i < l; i++ ) {
7652: t = mat[i];
7653: if ( (a = t[j]) != 0 ) {
7654: sugar[i] = MAX(sugar[i],s);
7655: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7656: }
7657: }
7658: l--;
7659: }
7660: return rank;
7661: }
1.1 noro 7662:
1.7 noro 7663: int ndv_ishomo(NDV p)
7664: {
7665: NMV m;
7666: int len,h;
1.1 noro 7667:
7668: if ( !p ) return 1;
7669: len = LEN(p);
7670: m = BDY(p);
7671: h = TD(DL(m));
7672: NMV_ADV(m);
7673: for ( len--; len; len--, NMV_ADV(m) )
7674: if ( TD(DL(m)) != h ) return 0;
7675: return 1;
7676: }
7677:
7678: void ndv_save(NDV p,int index)
7679: {
7680: FILE *s;
7681: char name[BUFSIZ];
7682: short id;
7683: int nv,sugar,len,n,i,td,e,j;
7684: NMV m;
7685: unsigned int *dl;
7686: int mpos;
7687:
7688: sprintf(name,"%s/%d",Demand,index);
7689: s = fopen(name,"w");
7690: savevl(s,0);
7691: if ( !p ) {
7692: saveobj(s,0);
7693: return;
7694: }
7695: id = O_DP;
7696: nv = NV(p);
7697: sugar = SG(p);
7698: len = LEN(p);
7699: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7700: write_int(s,(unsigned int *)&len);
7701:
7702: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7703: saveobj(s,(Obj)CZ(m));
1.1 noro 7704: dl = DL(m);
7705: td = TD(dl);
7706: write_int(s,(unsigned int *)&td);
7707: for ( j = 0; j < nv; j++ ) {
7708: e = GET_EXP(dl,j);
7709: write_int(s,(unsigned int *)&e);
7710: }
7711: if ( nd_module ) {
7712: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7713: }
7714: }
7715: fclose(s);
7716: }
7717:
7718: void nd_save_mod(ND p,int index)
7719: {
7720: FILE *s;
7721: char name[BUFSIZ];
7722: int nv,sugar,len,c;
7723: NM m;
7724:
7725: sprintf(name,"%s/%d",Demand,index);
7726: s = fopen(name,"w");
7727: if ( !p ) {
7728: len = 0;
7729: write_int(s,(unsigned int *)&len);
7730: fclose(s);
7731: return;
7732: }
7733: nv = NV(p);
7734: sugar = SG(p);
7735: len = LEN(p);
7736: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7737: for ( m = BDY(p); m; m = NEXT(m) ) {
7738: c = CM(m); write_int(s,(unsigned int *)&c);
7739: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7740: }
7741: fclose(s);
7742: }
7743:
7744: NDV ndv_load(int index)
7745: {
7746: FILE *s;
7747: char name[BUFSIZ];
7748: short id;
7749: int nv,sugar,len,n,i,td,e,j;
7750: NDV d;
7751: NMV m0,m;
7752: unsigned int *dl;
7753: Obj obj;
7754: int mpos;
7755:
7756: sprintf(name,"%s/%d",Demand,index);
7757: s = fopen(name,"r");
7758: if ( !s ) return 0;
7759:
7760: skipvl(s);
7761: read_short(s,(unsigned short *)&id);
7762: if ( !id ) return 0;
7763: read_int(s,(unsigned int *)&nv);
7764: read_int(s,(unsigned int *)&sugar);
7765: read_int(s,(unsigned int *)&len);
7766:
7767: m0 = m = MALLOC(len*nmv_adv);
7768: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7769: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7770: dl = DL(m);
7771: ndl_zero(dl);
7772: read_int(s,(unsigned int *)&td); TD(dl) = td;
7773: for ( j = 0; j < nv; j++ ) {
7774: read_int(s,(unsigned int *)&e);
7775: PUT_EXP(dl,j,e);
7776: }
7777: if ( nd_module ) {
7778: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7779: }
7780: if ( nd_blockmask ) ndl_weight_mask(dl);
7781: }
7782: fclose(s);
7783: MKNDV(nv,m0,len,d);
7784: SG(d) = sugar;
7785: return d;
7786: }
7787:
7788: ND nd_load_mod(int index)
7789: {
7790: FILE *s;
7791: char name[BUFSIZ];
7792: int nv,sugar,len,i,c;
7793: ND d;
7794: NM m0,m;
7795:
7796: sprintf(name,"%s/%d",Demand,index);
7797: s = fopen(name,"r");
7798: /* if the file does not exist, it means p[index]=0 */
7799: if ( !s ) return 0;
7800:
7801: read_int(s,(unsigned int *)&nv);
7802: if ( !nv ) { fclose(s); return 0; }
7803:
7804: read_int(s,(unsigned int *)&sugar);
7805: read_int(s,(unsigned int *)&len);
7806: for ( m0 = 0, i = 0; i < len; i++ ) {
7807: NEXTNM(m0,m);
7808: read_int(s,(unsigned int *)&c); CM(m) = c;
7809: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7810: }
7811: NEXT(m) = 0;
7812: MKND(nv,m0,len,d);
7813: SG(d) = sugar;
7814: fclose(s);
7815: return d;
7816: }
7817:
7818: void nd_det(int mod,MAT f,P *rp)
7819: {
7820: VL fv,tv;
7821: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7822: pointer **m;
7823: P **w;
7824: P mp,r;
7825: NDV **dm;
7826: NDV *t,*mi,*mj;
7827: NDV d,s,mij,mjj;
7828: ND u;
7829: NMV nmv;
7830: UINT *bound;
7831: PGeoBucket bucket;
7832: struct order_spec *ord;
7833: Z dq,dt,ds;
7834: Z mone;
7835: Z gn,qn,dn0,nm,dn;
7836:
7837: create_order_spec(0,0,&ord);
7838: nd_init_ord(ord);
7839: get_vars((Obj)f,&fv);
7840: if ( f->row != f->col )
7841: error("nd_det : non-square matrix");
7842: n = f->row;
7843: m = f->body;
7844: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7845:
7846: if ( !nvar ) {
7847: if ( !mod )
7848: detp(CO,(P **)m,n,rp);
7849: else {
7850: w = (P **)almat_pointer(n,n);
7851: for ( i = 0; i < n; i++ )
7852: for ( j = 0; j < n; j++ )
7853: ptomp(mod,(P)m[i][j],&w[i][j]);
7854: detmp(CO,mod,w,n,&mp);
7855: mptop(mp,rp);
7856: }
7857: return;
7858: }
7859:
7860: if ( !mod ) {
7861: w = (P **)almat_pointer(n,n);
7862: dq = ONE;
7863: for ( i = 0; i < n; i++ ) {
7864: dn0 = ONE;
7865: for ( j = 0; j < n; j++ ) {
7866: if ( !m[i][j] ) continue;
7867: lgp(m[i][j],&nm,&dn);
1.6 noro 7868: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 7869: }
7870: if ( !UNIZ(dn0) ) {
7871: ds = dn0;
7872: for ( j = 0; j < n; j++ )
7873: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
7874: mulz(dq,ds,&dt); dq = dt;
7875: } else
7876: for ( j = 0; j < n; j++ )
7877: w[i][j] = (P)m[i][j];
7878: }
7879: m = (pointer **)w;
7880: }
7881:
7882: for ( i = 0, max = 1; i < n; i++ )
7883: for ( j = 0; j < n; j++ )
7884: for ( tv = fv; tv; tv = NEXT(tv) ) {
7885: e = getdeg(tv->v,(P)m[i][j]);
7886: max = MAX(e,max);
7887: }
7888: nd_setup_parameters(nvar,max);
7889: dm = (NDV **)almat_pointer(n,n);
7890: for ( i = 0, max = 1; i < n; i++ )
7891: for ( j = 0; j < n; j++ ) {
7892: dm[i][j] = ptondv(CO,fv,m[i][j]);
7893: if ( mod ) ndv_mod(mod,dm[i][j]);
7894: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
7895: }
7896: d = ptondv(CO,fv,(P)ONE);
7897: if ( mod ) ndv_mod(mod,d);
7898: chsgnz(ONE,&mone);
7899: for ( j = 0, sgn = 1; j < n; j++ ) {
7900: if ( DP_Print ) {
7901: fprintf(asir_out,".");
7902: }
7903: for ( i = j; i < n && !dm[i][j]; i++ );
7904: if ( i == n ) {
7905: *rp = 0;
7906: return;
7907: }
7908: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
7909: for ( k = j; k < n; k++ )
7910: for ( l = j; l < n; l++ )
7911: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
7912: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
7913: }
7914: if ( k0 != j ) {
7915: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
7916: sgn = -sgn;
7917: }
7918: if ( l0 != j ) {
7919: for ( k = j; k < n; k++ ) {
7920: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
7921: }
7922: sgn = -sgn;
7923: }
7924: bound = nd_det_compute_bound(dm,n,j);
7925: for ( k = 0; k < nd_nvar; k++ )
7926: if ( bound[k]*2 > nd_mask0 ) break;
7927: if ( k < nd_nvar )
7928: nd_det_reconstruct(dm,n,j,d);
7929:
7930: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
7931: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
7932: mi = dm[i]; mij = mi[j];
7933: if ( mod )
7934: ndv_mul_c(mod,mij,mod-1);
7935: else
7936: ndv_mul_c_q(mij,mone);
7937: for ( k = j+1; k < n; k++ ) {
7938: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
7939: bucket = create_pbucket();
7940: if ( mi[k] ) {
7941: nmv = BDY(mjj); len = LEN(mjj);
7942: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7943: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
7944: add_pbucket(mod,bucket,u);
7945: }
7946: }
7947: if ( mj[k] && mij ) {
7948: nmv = BDY(mij); len = LEN(mij);
7949: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7950: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
7951: add_pbucket(mod,bucket,u);
7952: }
7953: }
7954: u = nd_quo(mod,bucket,d);
7955: mi[k] = ndtondv(mod,u);
7956: }
7957: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
7958: }
7959: d = mjj;
7960: }
7961: if ( DP_Print ) {
7962: fprintf(asir_out,"\n");
7963: }
7964: if ( sgn < 0 ) {
7965: if ( mod )
7966: ndv_mul_c(mod,d,mod-1);
7967: else
7968: ndv_mul_c_q(d,mone);
7969: }
7970: r = ndvtop(mod,CO,fv,d);
7971: if ( !mod && !UNIQ(dq) )
7972: divsp(CO,r,(P)dq,rp);
7973: else
7974: *rp = r;
7975: }
7976:
7977: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
7978: {
7979: NM mr,mr0;
7980: NM tnm;
7981: NMV m;
7982: UINT *d0,*dt,*dm;
7983: int c,n,td,i,c1,c2,len;
7984: Z q;
7985: ND r;
7986:
7987: if ( !p ) return 0;
7988: else {
7989: n = NV(p); m = BDY(p); len = LEN(p);
7990: d0 = DL(m0);
7991: td = TD(d);
7992: mr0 = 0;
7993: NEWNM(tnm);
7994: if ( mod ) {
7995: c = CM(m0);
7996: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7997: ndl_add(DL(m),d0,DL(tnm));
7998: if ( ndl_reducible(DL(tnm),d) ) {
7999: NEXTNM(mr0,mr);
8000: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8001: ndl_copy(DL(tnm),DL(mr));
8002: }
8003: }
8004: } else {
1.6 noro 8005: q = CZ(m0);
1.1 noro 8006: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8007: ndl_add(DL(m),d0,DL(tnm));
8008: if ( ndl_reducible(DL(tnm),d) ) {
8009: NEXTNM(mr0,mr);
1.6 noro 8010: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8011: ndl_copy(DL(tnm),DL(mr));
8012: }
8013: }
8014: }
8015: if ( !mr0 )
8016: return 0;
8017: else {
8018: NEXT(mr) = 0;
8019: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8020: MKND(NV(p),mr0,len,r);
8021: SG(r) = SG(p) + TD(d0);
8022: return r;
8023: }
8024: }
8025: }
8026:
8027: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8028: {
8029: int i,obpe,oadv,h,k,l;
8030: static NM prev_nm_free_list;
8031: EPOS oepos;
8032:
8033: obpe = nd_bpe;
8034: oadv = nmv_adv;
8035: oepos = nd_epos;
8036: if ( obpe < 2 ) nd_bpe = 2;
8037: else if ( obpe < 3 ) nd_bpe = 3;
8038: else if ( obpe < 4 ) nd_bpe = 4;
8039: else if ( obpe < 5 ) nd_bpe = 5;
8040: else if ( obpe < 6 ) nd_bpe = 6;
8041: else if ( obpe < 8 ) nd_bpe = 8;
8042: else if ( obpe < 10 ) nd_bpe = 10;
8043: else if ( obpe < 16 ) nd_bpe = 16;
8044: else if ( obpe < 32 ) nd_bpe = 32;
8045: else error("nd_det_reconstruct : exponent too large");
8046:
8047: nd_setup_parameters(nd_nvar,0);
8048: prev_nm_free_list = _nm_free_list;
8049: _nm_free_list = 0;
8050: for ( k = j; k < n; k++ )
8051: for (l = j; l < n; l++ )
8052: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8053: ndv_realloc(d,obpe,oadv,oepos);
8054: prev_nm_free_list = 0;
8055: #if 0
8056: GC_gcollect();
8057: #endif
8058: }
8059:
8060: /* returns a UINT array containing degree bounds */
8061:
8062: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8063: {
8064: UINT *d0,*d1,*d,*t,*r;
8065: int k,l,i;
8066:
8067: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8068: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8069: for ( k = j; k < n; k++ )
8070: for ( l = j; l < n; l++ )
8071: if ( dm[k][l] ) {
8072: d = ndv_compute_bound(dm[k][l]);
8073: for ( i = 0; i < nd_nvar; i++ )
8074: d0[i] = MAX(d0[i],d[i]);
8075: }
8076: return d0;
8077: }
8078:
8079: DL nd_separate_d(UINT *d,UINT *trans)
8080: {
8081: int n,td,i,e,j;
8082: DL a;
8083:
8084: ndl_zero(trans);
8085: td = 0;
8086: for ( i = 0; i < nd_ntrans; i++ ) {
8087: e = GET_EXP(d,i);
8088: PUT_EXP(trans,i,e);
8089: td += MUL_WEIGHT(e,i);
8090: }
8091: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8092: /* homogenized */
8093: i = nd_nvar-1;
8094: e = GET_EXP(d,i);
8095: PUT_EXP(trans,i,e);
8096: td += MUL_WEIGHT(e,i);
8097: }
8098: TD(trans) = td;
8099: if ( nd_blockmask) ndl_weight_mask(trans);
8100: NEWDL(a,nd_nalg);
8101: td = 0;
8102: for ( i = 0; i < nd_nalg; i++ ) {
8103: j = nd_ntrans+i;
8104: e = GET_EXP(d,j);
8105: a->d[i] = e;
8106: td += e;
8107: }
8108: a->td = td;
8109: return a;
8110: }
8111:
8112: int nd_monic(int mod,ND *p)
8113: {
8114: UINT *trans,*t;
8115: DL alg;
8116: MP mp0,mp;
8117: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8118: ND r;
8119: DL dl;
8120: DP nm;
8121: NDV ndv;
8122: DAlg inv,cd;
8123: ND s,c;
8124: Z l,mul;
8125: Z ln;
8126: int n,ntrans,i,e,td,is_lc,len;
8127: NumberField nf;
8128: struct oEGT eg0,eg1;
8129:
8130: if ( !(nf = get_numberfield()) )
8131: error("nd_monic : current_numberfield is not set");
8132:
8133: /* Q coef -> DAlg coef */
8134: NEWNM(ma0); ma = ma0;
8135: m = BDY(*p);
8136: is_lc = 1;
8137: while ( 1 ) {
8138: NEWMP(mp0); mp = mp0;
1.6 noro 8139: mp->c = (Obj)CZ(m);
1.1 noro 8140: mp->dl = nd_separate_d(DL(m),DL(ma));
8141: NEWNM(mb);
8142: for ( m = NEXT(m); m; m = NEXT(m) ) {
8143: alg = nd_separate_d(DL(m),DL(mb));
8144: if ( !ndl_equal(DL(ma),DL(mb)) )
8145: break;
1.6 noro 8146: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8147: }
8148: NEXT(mp) = 0;
8149: MKDP(nd_nalg,mp0,nm);
8150: MKDAlg(nm,ONE,cd);
8151: if ( is_lc == 1 ) {
8152: /* if the lc is a rational number, we have nothing to do */
8153: if ( !mp0->dl->td )
8154: return 1;
8155:
8156: get_eg(&eg0);
8157: invdalg(cd,&inv);
8158: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8159: /* check the validity of inv */
8160: if ( mod && !remqi((Q)inv->dn,mod) )
8161: return 0;
8162: CA(ma) = nf->one;
8163: is_lc = 0;
8164: ln = ONE;
8165: } else {
8166: muldalg(cd,inv,&CA(ma));
8167: lcmz(ln,CA(ma)->dn,&ln);
8168: }
8169: if ( m ) {
8170: NEXT(ma) = mb; ma = mb;
8171: } else {
8172: NEXT(ma) = 0;
8173: break;
8174: }
8175: }
8176: /* l = lcm(denoms) */
8177: l = ln;
8178: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8179: divsz(l,CA(m)->dn,&mul);
1.1 noro 8180: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8181: NEXTNM(mr0,mr);
1.6 noro 8182: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8183: dl = mp->dl;
8184: td = TD(DL(m));
8185: ndl_copy(DL(m),DL(mr));
8186: for ( i = 0; i < nd_nalg; i++ ) {
8187: e = dl->d[i];
8188: PUT_EXP(DL(mr),i+nd_ntrans,e);
8189: td += MUL_WEIGHT(e,i+nd_ntrans);
8190: }
8191: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8192: TD(DL(mr)) = td;
8193: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8194: }
8195: }
8196: NEXT(mr) = 0;
8197: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8198: MKND(NV(*p),mr0,len,r);
8199: /* XXX */
8200: SG(r) = SG(*p);
8201: nd_free(*p);
8202: *p = r;
8203: return 1;
8204: }
8205:
8206: NODE reverse_node(NODE n)
8207: {
8208: NODE t,t1;
8209:
8210: for ( t = 0; n; n = NEXT(n) ) {
8211: MKNODE(t1,BDY(n),t); t = t1;
8212: }
8213: return t;
8214: }
8215:
8216: P ndc_div(int mod,union oNDC a,union oNDC b)
8217: {
8218: union oNDC c;
8219: int inv,t;
8220:
8221: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8222: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8223: else if ( mod ) {
8224: inv = invm(b.m,mod);
8225: DMAR(a.m,inv,0,mod,t); c.m = t;
8226: } else if ( nd_vc )
8227: divsp(nd_vc,a.p,b.p,&c.p);
8228: else
8229: divsz(a.z,b.z,&c.z);
8230: return ndctop(mod,c);
8231: }
8232:
8233: P ndctop(int mod,union oNDC c)
8234: {
8235: Z q;
8236: int e;
8237: GFS gfs;
8238:
8239: if ( mod == -1 ) {
8240: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8241: } else if ( mod == -2 ) {
1.10 noro 8242: q = c.z; return (P)q;
1.1 noro 8243: } else if ( mod > 0 ) {
1.6 noro 8244: STOZ(c.m,q); return (P)q;
1.1 noro 8245: } else
8246: return (P)c.p;
8247: }
8248:
8249: /* [0,0,0,cont] = p -> p/cont */
8250:
8251: void finalize_tracelist(int i,P cont)
8252: {
8253: LIST l;
8254: NODE node;
8255: Z iq;
8256:
8257: if ( !UNIQ(cont) ) {
8258: node = mknode(4,NULLP,NULLP,NULLP,cont);
8259: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8260: nd_tracelist = node;
8261: }
1.6 noro 8262: STOZ(i,iq);
1.1 noro 8263: nd_tracelist = reverse_node(nd_tracelist);
8264: MKLIST(l,nd_tracelist);
8265: node = mknode(2,iq,l); MKLIST(l,node);
8266: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8267: nd_alltracelist = node; nd_tracelist = 0;
8268: }
8269:
8270: void conv_ilist(int demand,int trace,NODE g,int **indp)
8271: {
8272: int n,i,j;
8273: int *ind;
8274: NODE t;
8275:
8276: n = length(g);
8277: ind = (int *)MALLOC(n*sizeof(int));
8278: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8279: j = (long)BDY(t); ind[i] = j;
8280: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8281: }
8282: if ( indp ) *indp = ind;
8283: }
8284:
8285: void parse_nd_option(NODE opt)
8286: {
8287: NODE t,p,u;
8288: int i,s,n;
8289: char *key;
8290: Obj value;
8291:
8292: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8293: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8294: nd_splist = 0; nd_check_splist = 0;
8295: nd_sugarweight = 0;
8296: nd_f4red =0;
8297: nd_rank0 = 0;
8298: for ( t = opt; t; t = NEXT(t) ) {
8299: p = BDY((LIST)BDY(t));
8300: key = BDY((STRING)BDY(p));
8301: value = (Obj)BDY(NEXT(p));
8302: if ( !strcmp(key,"gentrace") )
8303: nd_gentrace = value?1:0;
8304: else if ( !strcmp(key,"gensyz") )
8305: nd_gensyz = value?1:0;
8306: else if ( !strcmp(key,"nora") )
8307: nd_nora = value?1:0;
8308: else if ( !strcmp(key,"gbblock") ) {
8309: if ( value && OID(value) == O_LIST ) {
8310: u = BDY((LIST)value);
8311: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8312: for ( i = 0; u; u = NEXT(u) ) {
8313: p = BDY((LIST)BDY(u));
1.6 noro 8314: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8315: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8316: }
8317: nd_gbblock[i] = -1;
8318: } else
8319: nd_gbblock = 0;
8320: } else if ( !strcmp(key,"newelim") )
8321: nd_newelim = value?1:0;
8322: else if ( !strcmp(key,"intersect") )
8323: nd_intersect = value?1:0;
8324: else if ( !strcmp(key,"lf") )
8325: nd_lf = value?1:0;
8326: else if ( !strcmp(key,"trace") ) {
8327: if ( value ) {
8328: u = BDY((LIST)value);
8329: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8330: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8331: }
8332: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8333: nd_f4red = ZTOS((Q)value);
1.1 noro 8334: } else if ( !strcmp(key,"rank0") ) {
8335: nd_rank0 = value?1:0;
8336: } else if ( !strcmp(key,"splist") ) {
8337: nd_splist = value?1:0;
8338: } else if ( !strcmp(key,"check_splist") ) {
8339: nd_check_splist = BDY((LIST)value);
8340: } else if ( !strcmp(key,"sugarweight") ) {
8341: u = BDY((LIST)value);
8342: n = length(u);
8343: nd_sugarweight = MALLOC(n*sizeof(int));
8344: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8345: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8346: }
8347: }
8348: }
8349:
8350: ND mdptond(DP d);
8351: ND nd_mul_nm(int mod,NM m0,ND p);
8352: ND nd_mul_nm_lf(NM m0,ND p);
8353: ND *btog(NODE ti,ND **p,int nb,int mod);
8354: ND btog_one(NODE ti,ND *p,int nb,int mod);
8355: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8356: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8357:
8358: /* d:monomial */
8359: ND mdptond(DP d)
8360: {
8361: NM m;
8362: ND r;
8363:
8364: if ( OID(d) == 1 )
8365: r = ptond(CO,CO,(P)d);
8366: else {
8367: NEWNM(m);
8368: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8369: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8370: NEXT(m) = 0;
8371: MKND(NV(d),m,1,r);
8372: }
8373: return r;
8374: }
8375:
8376: ND nd_mul_nm(int mod,NM m0,ND p)
8377: {
8378: UINT *d0;
8379: int c0,c1,c;
8380: NM tm,mr,mr0;
8381: ND r;
8382:
8383: if ( !p ) return 0;
8384: d0 = DL(m0);
8385: c0 = CM(m0);
8386: mr0 = 0;
8387: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8388: NEXTNM(mr0,mr);
8389: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8390: ndl_add(d0,DL(tm),DL(mr));
8391: }
8392: NEXT(mr) = 0;
8393: MKND(NV(p),mr0,LEN(p),r);
8394: return r;
8395: }
8396:
8397: ND nd_mul_nm_lf(NM m0,ND p)
8398: {
8399: UINT *d0;
8400: Z c0,c1,c;
8401: NM tm,mr,mr0;
8402: ND r;
8403:
8404: if ( !p ) return 0;
8405: d0 = DL(m0);
8406: c0 = CZ(m0);
8407: mr0 = 0;
8408: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8409: NEXTNM(mr0,mr);
8410: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8411: ndl_add(d0,DL(tm),DL(mr));
8412: }
8413: NEXT(mr) = 0;
8414: MKND(NV(p),mr0,LEN(p),r);
8415: return r;
8416: }
8417:
8418: ND *btog(NODE ti,ND **p,int nb,int mod)
8419: {
8420: PGeoBucket *r;
8421: int i,ci;
8422: NODE t,s;
8423: ND m,tp;
8424: ND *pi,*rd;
8425: P c;
8426:
8427: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8428: for ( i = 0; i < nb; i++ )
8429: r[i] = create_pbucket();
8430: for ( t = ti; t; t = NEXT(t) ) {
8431: s = BDY((LIST)BDY(t));
8432: if ( ARG0(s) ) {
8433: m = mdptond((DP)ARG2(s));
1.6 noro 8434: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8435: if ( (ci = ((MQ)c)->cont) != 0 ) {
8436: HCM(m) = ci;
1.6 noro 8437: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8438: for ( i = 0; i < nb; i++ ) {
8439: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8440: add_pbucket(mod,r[i],tp);
8441: }
8442: }
8443: ci = 1;
8444: } else {
8445: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8446: ci = invm(ci,mod);
8447: }
8448: }
8449: rd = (ND *)MALLOC(nb*sizeof(ND));
8450: for ( i = 0; i < nb; i++ )
8451: rd[i] = normalize_pbucket(mod,r[i]);
8452: if ( ci != 1 )
8453: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8454: return rd;
8455: }
8456:
8457: /* YYY */
8458: ND *btog_lf(NODE ti,ND **p,int nb)
8459: {
8460: PGeoBucket *r;
8461: int i;
8462: NODE t,s;
8463: ND m,tp;
8464: ND *pi,*rd;
8465: LM lm;
8466: Z lf,c;
8467:
8468: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8469: for ( i = 0; i < nb; i++ )
8470: r[i] = create_pbucket();
8471: for ( t = ti; t; t = NEXT(t) ) {
8472: s = BDY((LIST)BDY(t));
8473: if ( ARG0(s) ) {
8474: m = mdptond((DP)ARG2(s));
1.6 noro 8475: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8476: if ( lm ) {
8477: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8478: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8479: for ( i = 0; i < nb; i++ ) {
8480: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8481: add_pbucket(-2,r[i],tp);
8482: }
8483: }
8484: c = ONE;
8485: } else {
8486: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8487: }
8488: }
8489: rd = (ND *)MALLOC(nb*sizeof(ND));
8490: for ( i = 0; i < nb; i++ )
8491: rd[i] = normalize_pbucket(-2,r[i]);
8492: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8493: return rd;
8494: }
8495:
8496: ND btog_one(NODE ti,ND *p,int nb,int mod)
8497: {
8498: PGeoBucket r;
8499: int i,ci,j;
8500: NODE t,s;
8501: ND m,tp;
8502: ND pi,rd;
8503: P c;
8504:
8505: r = create_pbucket();
8506: for ( t = ti; t; t = NEXT(t) ) {
8507: s = BDY((LIST)BDY(t));
8508: if ( ARG0(s) ) {
8509: m = mdptond((DP)ARG2(s));
1.6 noro 8510: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8511: if ( (ci = ((MQ)c)->cont) != 0 ) {
8512: HCM(m) = ci;
1.6 noro 8513: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8514: if ( !pi ) {
8515: pi = nd_load_mod(j);
8516: tp = nd_mul_nm(mod,BDY(m),pi);
8517: nd_free(pi);
8518: add_pbucket(mod,r,tp);
8519: } else {
8520: tp = nd_mul_nm(mod,BDY(m),pi);
8521: add_pbucket(mod,r,tp);
8522: }
8523: }
8524: ci = 1;
8525: } else {
8526: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8527: ci = invm(ci,mod);
8528: }
8529: }
8530: rd = normalize_pbucket(mod,r);
8531: free_pbucket(r);
8532: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8533: return rd;
8534: }
8535:
8536: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8537:
8538: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8539: {
8540: int i,j,n,m,nb,pi0,pi1,nvar;
8541: VL fv,tv,vv;
8542: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8543: ND **p;
8544: ND *c;
8545: ND u;
8546: P inv;
8547: MAT mat;
8548:
8549: if ( mod == -2 )
8550: return nd_btog_lf(f,v,ord,tlist,rp);
8551:
8552: parse_nd_option(current_option);
8553: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8554: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8555: switch ( ord->id ) {
8556: case 1:
8557: if ( ord->nv != nvar )
8558: error("nd_check : invalid order specification");
8559: break;
8560: default:
8561: break;
8562: }
8563: nd_init_ord(ord);
8564: #if 0
1.6 noro 8565: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8566: #else
8567: nd_bpe = 32;
8568: #endif
8569: nd_setup_parameters(nvar,0);
8570: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8571: intred = BDY((LIST)ARG3(BDY(tlist)));
8572: ind = BDY((LIST)ARG4(BDY(tlist)));
8573: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8574: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8575: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8576: if ( j > i ) i = j;
8577: }
8578: n = i+1;
8579: nb = length(BDY(f));
8580: p = (ND **)MALLOC(n*sizeof(ND *));
8581: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8582: pi = BDY((LIST)BDY(t));
1.6 noro 8583: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8584: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8585: ptomp(mod,(P)ARG2(pi),&inv);
8586: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8587: u = ptond(CO,vv,(P)ONE);
8588: HCM(u) = ((MQ)inv)->cont;
8589: c[pi1] = u;
8590: }
8591: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8592: printf("%d ",i); fflush(stdout);
8593: ti = BDY((LIST)BDY(t));
1.6 noro 8594: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8595: }
8596: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8597: printf("%d ",i); fflush(stdout);
8598: ti = BDY((LIST)BDY(t));
1.6 noro 8599: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8600: }
8601: m = length(ind);
8602: MKMAT(mat,nb,m);
8603: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8604: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8605: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8606: return mat;
8607: }
8608:
8609: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8610: {
8611: int i,j,n,m,nb,pi0,pi1,nvar;
8612: VL fv,tv,vv;
8613: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8614: ND **p;
8615: ND *c;
8616: ND u;
8617: MAT mat;
8618: LM lm;
8619: Z lf,inv;
8620:
8621: parse_nd_option(current_option);
8622: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8623: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8624: switch ( ord->id ) {
8625: case 1:
8626: if ( ord->nv != nvar )
8627: error("nd_check : invalid order specification");
8628: break;
8629: default:
8630: break;
8631: }
8632: nd_init_ord(ord);
8633: #if 0
1.6 noro 8634: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8635: #else
8636: nd_bpe = 32;
8637: #endif
8638: nd_setup_parameters(nvar,0);
8639: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8640: intred = BDY((LIST)ARG3(BDY(tlist)));
8641: ind = BDY((LIST)ARG4(BDY(tlist)));
8642: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8643: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8644: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8645: if ( j > i ) i = j;
8646: }
8647: n = i+1;
8648: nb = length(BDY(f));
8649: p = (ND **)MALLOC(n*sizeof(ND *));
8650: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8651: pi = BDY((LIST)BDY(t));
1.6 noro 8652: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8653: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8654: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8655: u = ptond(CO,vv,(P)ONE);
8656: HCZ(u) = inv;
8657: c[pi1] = u;
8658: }
8659: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8660: printf("%d ",i); fflush(stdout);
8661: ti = BDY((LIST)BDY(t));
1.6 noro 8662: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8663: }
8664: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8665: printf("%d ",i); fflush(stdout);
8666: ti = BDY((LIST)BDY(t));
1.6 noro 8667: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8668: }
8669: m = length(ind);
8670: MKMAT(mat,nb,m);
8671: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8672: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8673: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8674: return mat;
8675: }
8676:
8677: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8678: LIST tlist,int pos,MAT *rp)
8679: {
8680: int i,j,n,m,nb,pi0,pi1,nvar;
8681: VL fv,tv,vv;
8682: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8683: ND *p;
8684: ND *c;
8685: ND u;
8686: P inv;
8687: VECT vect;
8688:
8689: if ( mod == -2 )
8690: error("nd_btog_one : not implemented yet for a large finite field");
8691:
8692: parse_nd_option(current_option);
8693: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8694: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8695: switch ( ord->id ) {
8696: case 1:
8697: if ( ord->nv != nvar )
8698: error("nd_check : invalid order specification");
8699: break;
8700: default:
8701: break;
8702: }
8703: nd_init_ord(ord);
8704: #if 0
1.6 noro 8705: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8706: #else
8707: nd_bpe = 32;
8708: #endif
8709: nd_setup_parameters(nvar,0);
8710: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8711: intred = BDY((LIST)ARG3(BDY(tlist)));
8712: ind = BDY((LIST)ARG4(BDY(tlist)));
8713: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8714: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8715: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8716: if ( j > i ) i = j;
8717: }
8718: n = i+1;
8719: nb = length(BDY(f));
8720: p = (ND *)MALLOC(n*sizeof(ND *));
8721: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8722: pi = BDY((LIST)BDY(t));
1.6 noro 8723: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8724: if ( pi1 == pos ) {
8725: ptomp(mod,(P)ARG2(pi),&inv);
8726: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8727: u = ptond(CO,vv,(P)ONE);
8728: HCM(u) = ((MQ)inv)->cont;
8729: p[pi0] = u;
8730: }
8731: }
8732: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8733: printf("%d ",i); fflush(stdout);
8734: ti = BDY((LIST)BDY(t));
1.6 noro 8735: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8736: if ( Demand ) {
8737: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8738: }
8739: }
8740: for ( t = intred, 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: m = length(ind);
8749: MKVECT(vect,m);
8750: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8751: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8752: if ( !u ) {
1.6 noro 8753: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8754: BDY(vect)[j] = ndtodp(mod,u);
8755: nd_free(u);
8756: } else
8757: BDY(vect)[j] = ndtodp(mod,u);
8758: }
8759: return vect;
8760: }
8761:
8762: void ndv_print_lf(NDV p)
8763: {
8764: NMV m;
8765: int i,len;
8766:
8767: if ( !p ) printf("0\n");
8768: else {
8769: len = LEN(p);
8770: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8771: printf("+");
8772: mpz_out_str(asir_out,10,BDY(CZ(m)));
8773: printf("*");
8774: ndl_print(DL(m));
8775: }
8776: printf("\n");
8777: }
8778: }
8779:
8780: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8781: {
8782: VL tv,fv,vv,vc,av;
8783: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8784: int m,nocheck,nvar,mindex,e,max;
8785: NDV c;
8786: NMV a;
8787: P p,zp;
8788: Q dmy;
8789: EPOS oepos;
8790: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8791: Alg alpha,dp;
8792: P poly;
8793: LIST f1,f2,zpl;
8794: Obj obj;
8795: NumberField nf;
8796: struct order_spec *ord1;
8797: struct oEGT eg_check,eg0,eg1;
8798: NODE tr,tl1,tl2,tl3,tl4;
8799: LIST l1,l2,l3,l4,l5;
8800: int *perm;
8801: int j,ret;
8802: NODE retn;
8803: Q jq,bpe;
8804:
8805: nd_module = 0;
8806: parse_nd_option(current_option);
8807: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8808: if ( nd_vc )
8809: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8810: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8811: switch ( ord->id ) {
8812: case 1:
8813: if ( ord->nv != nvar )
8814: error("nd_f4_lf_trace : invalid order specification");
8815: break;
8816: default:
8817: break;
8818: }
8819:
8820: nd_ntrans = nvar;
8821: nd_nalg = 0;
8822:
8823: nocheck = 0;
8824: mindex = 0;
8825:
8826: /* do not use on-demand load/save */
8827: nd_demand = 0;
8828: m = trace > 1 ? trace : get_lprime(mindex);
8829: nd_init_ord(ord);
8830: mrank = 0;
8831: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8832: for ( tv = vv; tv; tv = NEXT(tv) ) {
8833: if ( nd_module ) {
8834: s = BDY((LIST)BDY(t));
8835: trank = length(s);
8836: mrank = MAX(mrank,trank);
8837: for ( ; s; s = NEXT(s) ) {
8838: e = getdeg(tv->v,(P)BDY(s));
8839: max = MAX(e,max);
8840: }
8841: } else {
8842: e = getdeg(tv->v,(P)BDY(t));
8843: max = MAX(e,max);
8844: }
8845: }
8846: nd_setup_parameters(nvar,max);
8847: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8848: ishomo = 1;
8849: /* XXX */
8850: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8851: if ( nd_module ) {
8852: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8853: } else {
8854: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8855: }
8856: if ( ishomo )
8857: ishomo = ishomo && ndv_ishomo(c);
8858: if ( c ) {
8859: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
8860: ndv_mod(-2,c);
8861: NEXTNODE(in0,in); BDY(in) = (pointer)c;
8862: }
8863: }
8864: if ( in0 ) NEXT(in) = 0;
8865: if ( fd0 ) NEXT(fd) = 0;
8866: if ( !ishomo && homo ) {
8867: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
8868: c = (NDV)BDY(t); len = LEN(c);
8869: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
8870: wmax = MAX(TD(DL(a)),wmax);
8871: }
8872: homogenize_order(ord,nvar,&ord1);
8873: nd_init_ord(ord1);
8874: nd_setup_parameters(nvar+1,wmax);
8875: for ( t = fd0; t; t = NEXT(t) )
8876: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
8877: }
8878: if ( MaxDeg > 0 ) nocheck = 1;
8879: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
8880: if ( ret )
8881: cand = nd_f4_lf_trace_main(m,&perm);
8882: if ( !ret || !cand ) {
8883: *rp = 0; return;
8884: }
8885: if ( !ishomo && homo ) {
8886: /* dehomogenization */
8887: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
8888: nd_init_ord(ord);
8889: nd_setup_parameters(nvar,0);
8890: }
8891: cand = ndv_reducebase(cand,perm);
8892: cand = ndv_reduceall(-2,cand);
8893: cbpe = nd_bpe;
8894: get_eg(&eg0);
8895: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
8896: /* gbcheck : cand is a GB of Id(cand) ? */
8897: retn = nd_f4(-2,0,0);
8898: }
8899: if ( !retn ) {
8900: /* failure */
8901: *rp = 0; return;
8902: }
8903: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
8904: if ( DP_Print )
1.5 noro 8905: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 8906: /* dp->p */
8907: nd_bpe = cbpe;
8908: nd_setup_parameters(nd_nvar,0);
8909: for ( r = cand; r; r = NEXT(r) ) {
8910: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
8911: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
8912: }
8913: MKLIST(*rp,cand);
8914: }
8915:
8916: NODE nd_f4_lf_trace_main(int m,int **indp)
8917: {
8918: int i,nh,stat,index;
8919: NODE r,rm,g;
8920: ND_pairs d,l,l0,t;
8921: ND spol,red;
8922: NDV nf,redv,nfqv,nfv;
8923: NM s0,s;
8924: NODE rp0,srp0,nflist,nflist_lf;
8925: int nsp,nred,col,rank,len,k,j,a;
8926: UINT c;
8927: UINT **spmat;
8928: UINT *s0vect,*svect,*p,*v;
8929: int *colstat;
8930: IndArray *imat;
8931: int *rhead;
8932: int spcol,sprow;
8933: int sugar;
8934: PGeoBucket bucket;
8935: struct oEGT eg0,eg1,eg_f4;
8936:
8937: g = 0; d = 0;
8938: for ( i = 0; i < nd_psn; i++ ) {
8939: d = update_pairs(d,g,i,0);
8940: g = update_base(g,i);
8941: }
8942: while ( d ) {
8943: get_eg(&eg0);
8944: l = nd_minsugarp(d,&d);
8945: sugar = SG(l);
8946: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8947: bucket = create_pbucket();
8948: stat = nd_sp_f4(m,0,l,bucket);
8949: if ( !stat ) {
8950: for ( t = l; NEXT(t); t = NEXT(t) );
8951: NEXT(t) = d; d = l;
8952: d = nd_reconstruct(1,d);
8953: continue;
8954: }
8955: if ( bucket->m < 0 ) continue;
8956: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8957: if ( !col ) {
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: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8964: if ( DP_Print )
1.5 noro 8965: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 8966: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8967: if ( !l0 ) continue;
8968: l = l0;
8969:
8970: /* over LF */
8971: bucket = create_pbucket();
8972: stat = nd_sp_f4(-2,1,l,bucket);
8973: if ( !stat ) {
8974: for ( t = l; NEXT(t); t = NEXT(t) );
8975: NEXT(t) = d; d = l;
8976: d = nd_reconstruct(1,d);
8977: continue;
8978: }
8979: if ( bucket->m < 0 ) continue;
8980: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8981: if ( !col ) {
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: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
8988: /* adding new bases */
8989: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
8990: nfv = (NDV)BDY(rm);
8991: nfqv = (NDV)BDY(r);
8992: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
8993: ndv_removecont(m,nfv);
8994: ndv_removecont(-2,nfqv);
8995: nh = ndv_newps(-2,nfv,nfqv,1);
8996: d = update_pairs(d,g,nh,0);
8997: g = update_base(g,nh);
8998: }
8999: if ( r || rm ) return 0;
9000: }
9001: conv_ilist(nd_demand,1,g,indp);
9002: return g;
9003: }
9004:
1.7 noro 9005: #if SIZEOF_LONG==8
9006:
9007: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9008: {
9009: int j,k,len;
9010: UINT *p;
9011: UINT c;
9012: NDV r;
9013: NMV mr0,mr;
9014:
9015: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9016: if ( !len ) return 0;
9017: else {
9018: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9019: #if 0
9020: ndv_alloc += nmv_adv*len;
9021: #endif
9022: mr = mr0;
9023: p = s0vect;
9024: for ( j = k = 0; j < col; j++, p += nd_wpd )
9025: if ( !rhead[j] ) {
9026: if ( (c = (UINT)vect[k++]) != 0 ) {
9027: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9028: }
9029: }
9030: MKNDV(nd_nvar,mr0,len,r);
9031: return r;
9032: }
9033: }
9034:
9035: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9036: {
9037: NM m;
1.11 noro 9038: UINT *t,*s,*u;
9039: int i,st,ed,md,prev,c;
1.7 noro 9040:
9041: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9042: prev = 0;
9043: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9044: t = DL(m);
9045: st = prev;
9046: ed = n;
9047: while ( ed > st ) {
9048: md = (st+ed)/2;
9049: u = s0+md*nd_wpd;
9050: c = DL_COMPARE(u,t);
9051: if ( c == 0 ) break;
9052: else if ( c > 0 ) st = md;
9053: else ed = md;
9054: }
9055: r[md] = (mp_limb_t)CM(m);
9056: prev = md;
1.7 noro 9057: }
9058: for ( i = 0; !r[i]; i++ );
9059: return i;
9060: }
9061:
9062: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9063:
9064: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9065: {
9066: int i,j,k,len,pos,prev;
9067: mp_limb_t a,c,c1,c2;
9068: IndArray ivect;
9069: unsigned char *ivc;
9070: unsigned short *ivs;
9071: unsigned int *ivi;
9072: NDV redv;
9073: NMV mr;
9074: NODE rp;
9075: int maxrs;
9076:
9077: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9078: maxrs = 0;
9079: for ( i = 0; i < nred; i++ ) {
9080: ivect = imat[i];
9081: k = ivect->head;
9082: a = svect[k]; c = cvect[k];
9083: MOD128(a,c,m);
9084: svect[k] = a; cvect[k] = 0;
9085: if ( (c = svect[k]) != 0 ) {
1.11 noro 9086: Nf4_red++;
1.7 noro 9087: maxrs = MAX(maxrs,rp0[i]->sugar);
9088: c = m-c; redv = nd_ps[rp0[i]->index];
9089: len = LEN(redv); mr = BDY(redv);
9090: svect[k] = 0; prev = k;
9091: switch ( ivect->width ) {
9092: case 1:
9093: ivc = ivect->index.c;
9094: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9095: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 ! noro 9096: c2 = svect[pos]+c1*c;
! 9097: if ( c2 < svect[pos] ) cvect[pos]++;
! 9098: svect[pos] = c2;
1.7 noro 9099: }
9100: break;
9101: case 2:
9102: ivs = ivect->index.s;
9103: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9104: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 ! noro 9105: c2 = svect[pos]+c1*c;
! 9106: if ( c2 < svect[pos] ) cvect[pos]++;
! 9107: svect[pos] = c2;
1.7 noro 9108: }
9109: break;
9110: case 4:
9111: ivi = ivect->index.i;
9112: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9113: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 ! noro 9114: c2 = svect[pos]+c1*c;
! 9115: if ( c2 < svect[pos] ) cvect[pos]++;
! 9116: svect[pos] = c2;
1.7 noro 9117: }
9118: break;
9119: }
9120: }
9121: }
9122: for ( i = 0; i < col; i++ ) {
9123: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9124: }
9125: return maxrs;
9126: }
9127:
9128: /* for Fp, 2^15=<p<2^29 */
9129:
9130: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9131: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9132: {
9133: int spcol,sprow,a;
9134: int i,j,k,l,rank;
9135: NODE r0,r;
9136: ND_pairs sp;
9137: ND spol;
9138: mp_limb_t **spmat;
9139: mp_limb_t *svect,*cvect;
9140: mp_limb_t *v;
9141: int *colstat;
9142: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9143: int maxrs;
9144: int *spsugar;
9145: ND_pairs *spactive;
9146:
9147: spcol = col-nred;
9148: get_eg(&eg0);
9149: /* elimination (1st step) */
9150: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9151: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9152: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9153: spsugar = (int *)MALLOC(nsp*sizeof(int));
9154: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9155: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9156: nd_sp(m,0,sp,&spol);
9157: if ( !spol ) continue;
9158: nd_to_vect64(m,s0vect,col,spol,svect);
9159: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9160: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9161: if ( i < col ) {
9162: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9163: for ( j = k = 0; j < col; j++ )
9164: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9165: spsugar[sprow] = MAX(maxrs,SG(spol));
9166: if ( nz )
9167: spactive[sprow] = sp;
9168: sprow++;
9169: }
9170: nd_free(spol);
9171: }
1.12 ! noro 9172: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9173: if ( DP_Print ) {
9174: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9175: fflush(asir_out);
9176: }
9177: /* free index arrays */
9178: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9179:
9180: /* elimination (2nd step) */
9181: colstat = (int *)MALLOC(spcol*sizeof(int));
9182: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9183: r0 = 0;
9184: for ( i = 0; i < rank; i++ ) {
9185: NEXTNODE(r0,r); BDY(r) =
9186: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9187: SG((NDV)BDY(r)) = spsugar[i];
9188: GCFREE(spmat[i]);
9189: }
9190: if ( r0 ) NEXT(r) = 0;
9191:
9192: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 ! noro 9193: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9194: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9195: if ( DP_Print ) {
9196: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9197: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9198: nsp,nred,sprow,spcol,rank);
9199: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9200: }
9201: if ( nz ) {
9202: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9203: if ( rank > 0 ) {
9204: NEXT(spactive[rank-1]) = 0;
9205: *nz = spactive[0];
9206: } else
9207: *nz = 0;
9208: }
9209: return r0;
9210: }
9211:
9212: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9213: {
9214: int i,j,k,l,rank,s;
9215: mp_limb_t inv;
9216: mp_limb_t a;
9217: UINT c;
9218: mp_limb_t *t,*pivot,*pk;
9219: UINT *ck;
9220: UINT **cmat;
9221: UINT *ct;
9222: ND_pairs pair;
9223:
9224: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9225: for ( i = 0; i < row; i++ ) {
9226: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9227: bzero(cmat[i],col*sizeof(UINT));
9228: }
9229:
9230: for ( rank = 0, j = 0; j < col; j++ ) {
9231: for ( i = rank; i < row; i++ ) {
9232: a = mat[i][j]; c = cmat[i][j];
9233: MOD128(a,c,md);
9234: mat[i][j] = a; cmat[i][j] = 0;
9235: }
9236: for ( i = rank; i < row; i++ )
9237: if ( mat[i][j] )
9238: break;
9239: if ( i == row ) {
9240: colstat[j] = 0;
9241: continue;
9242: } else
9243: colstat[j] = 1;
9244: if ( i != rank ) {
9245: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9246: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9247: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9248: if ( spactive ) {
9249: pair = spactive[i]; spactive[i] = spactive[rank];
9250: spactive[rank] = pair;
9251: }
9252: }
9253: /* column j is normalized */
9254: s = sugar[rank];
9255: inv = invm((UINT)mat[rank][j],md);
9256: /* normalize pivot row */
9257: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9258: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9259: }
9260: for ( i = rank+1; i < row; i++ ) {
9261: if ( (a = mat[i][j]) != 0 ) {
9262: sugar[i] = MAX(sugar[i],s);
9263: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9264: Nf4_red++;
1.7 noro 9265: }
9266: }
9267: rank++;
9268: }
9269: for ( j = col-1, l = rank-1; j >= 0; j-- )
9270: if ( colstat[j] ) {
9271: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9272: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9273: }
9274: s = sugar[l];
9275: for ( i = 0; i < l; i++ ) {
9276: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9277: if ( a ) {
9278: sugar[i] = MAX(sugar[i],s);
9279: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9280: Nf4_red++;
1.7 noro 9281: }
9282: }
9283: l--;
9284: }
9285: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9286: GCFREE(cmat);
9287: return rank;
9288: }
9289: #endif
9290:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>