Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.11
1.11 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.10 2018/10/19 23:27:38 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.11 ! noro 5: int Nnd_add,Nf4_red;
1.1 noro 6: struct oEGT eg_search;
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.11 ! noro 1133: r = (r*10007+d[i]);
! 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;
6682: #if 0
6683: ndv_alloc = 0;
6684: #endif
1.11 ! noro 6685: Nf4_red=0;
1.1 noro 6686: g = 0; d = 0;
6687: for ( i = 0; i < nd_psn; i++ ) {
6688: d = update_pairs(d,g,i,0);
6689: g = update_base(g,i);
6690: }
6691: nzlist = 0;
6692: nzlist_t = nd_nzlist;
6693: f4red = 1;
6694: nd_last_nonzero = 0;
6695: while ( d ) {
6696: get_eg(&eg0);
6697: l = nd_minsugarp(d,&d);
6698: sugar = nd_sugarweight?l->sugar2:SG(l);
6699: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6700: if ( nzlist_t ) {
6701: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6702: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6703: tn = BDY((LIST)ARG1(node));
6704: if ( !tn ) {
6705: nzlist_t = NEXT(nzlist_t);
6706: continue;
6707: }
6708: /* tn = [[i1,i2],...] */
6709: lh = nd_ipairtospair(tn);
6710: }
6711: bucket = create_pbucket();
6712: stat = nd_sp_f4(m,0,l,bucket);
6713: if ( !stat ) {
6714: for ( t = l; NEXT(t); t = NEXT(t) );
6715: NEXT(t) = d; d = l;
6716: d = nd_reconstruct(0,d);
6717: continue;
6718: }
6719: if ( bucket->m < 0 ) continue;
6720: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6721: if ( !col ) {
6722: for ( t = l; NEXT(t); t = NEXT(t) );
6723: NEXT(t) = d; d = l;
6724: d = nd_reconstruct(0,d);
6725: continue;
6726: }
6727: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6728: if ( DP_Print )
1.6 noro 6729: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6730: sugar,eg_f4.exectime);
1.1 noro 6731: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6732: if ( checkonly && nflist ) return 0;
6733: /* adding new bases */
6734: if ( nflist ) nd_last_nonzero = f4red;
6735: for ( r = nflist; r; r = NEXT(r) ) {
6736: nf = (NDV)BDY(r);
6737: ndv_removecont(m,nf);
6738: if ( !m && nd_nalg ) {
6739: ND nf1;
6740:
6741: nf1 = ndvtond(m,nf);
6742: nd_monic(0,&nf1);
6743: nd_removecont(m,nf1);
6744: nf = ndtondv(m,nf1);
6745: }
6746: nh = ndv_newps(m,nf,0,1);
6747: d = update_pairs(d,g,nh,0);
6748: g = update_base(g,nh);
6749: }
6750: if ( DP_Print ) {
6751: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6752: }
6753: if ( nd_gentrace ) {
6754: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6755: NEXTNODE(tn0,tn);
1.6 noro 6756: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6757: node = mknode(2,i1,i2); MKLIST(l0,node);
6758: BDY(tn) = l0;
6759: }
6760: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6761: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6762: MKNODE(node,l1,nzlist); nzlist = node;
6763: }
6764: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6765: f4red++;
6766: if ( nd_f4red && f4red > nd_f4red ) break;
6767: if ( nd_rank0 && !nflist ) break;
6768: }
6769: if ( nd_gentrace ) {
6770: MKLIST(l0,reverse_node(nzlist));
6771: MKNODE(nd_alltracelist,l0,0);
6772: }
6773: #if 0
6774: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6775: #endif
1.11 ! noro 6776: if ( DP_Print )
! 6777: fprintf(asir_out,"number of red=%d\n",Nf4_red);
1.1 noro 6778: conv_ilist(nd_demand,0,g,indp);
6779: return g;
6780: }
6781:
6782: NODE nd_f4_trace(int m,int **indp)
6783: {
6784: int i,nh,stat,index;
6785: NODE r,g;
6786: ND_pairs d,l,l0,t;
6787: ND spol,red;
6788: NDV nf,redv,nfqv,nfv;
6789: NM s0,s;
6790: NODE rp0,srp0,nflist;
6791: int nsp,nred,col,rank,len,k,j,a;
6792: UINT c;
6793: UINT **spmat;
6794: UINT *s0vect,*svect,*p,*v;
6795: int *colstat;
6796: IndArray *imat;
6797: int *rhead;
6798: int spcol,sprow;
6799: int sugar;
6800: PGeoBucket bucket;
6801: struct oEGT eg0,eg1,eg_f4;
6802:
6803: g = 0; d = 0;
6804: for ( i = 0; i < nd_psn; i++ ) {
6805: d = update_pairs(d,g,i,0);
6806: g = update_base(g,i);
6807: }
6808: while ( d ) {
6809: get_eg(&eg0);
6810: l = nd_minsugarp(d,&d);
6811: sugar = SG(l);
6812: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6813: bucket = create_pbucket();
6814: stat = nd_sp_f4(m,0,l,bucket);
6815: if ( !stat ) {
6816: for ( t = l; NEXT(t); t = NEXT(t) );
6817: NEXT(t) = d; d = l;
6818: d = nd_reconstruct(1,d);
6819: continue;
6820: }
6821: if ( bucket->m < 0 ) continue;
6822: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6823: if ( !col ) {
6824: for ( t = l; NEXT(t); t = NEXT(t) );
6825: NEXT(t) = d; d = l;
6826: d = nd_reconstruct(1,d);
6827: continue;
6828: }
6829: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6830: if ( DP_Print )
1.3 noro 6831: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 6832: sugar,eg_f4.exectime);
1.1 noro 6833: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6834: if ( !l0 ) continue;
6835: l = l0;
6836:
6837: /* over Q */
6838: bucket = create_pbucket();
6839: stat = nd_sp_f4(0,1,l,bucket);
6840: if ( !stat ) {
6841: for ( t = l; NEXT(t); t = NEXT(t) );
6842: NEXT(t) = d; d = l;
6843: d = nd_reconstruct(1,d);
6844: continue;
6845: }
6846: if ( bucket->m < 0 ) continue;
6847: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6848: if ( !col ) {
6849: for ( t = l; NEXT(t); t = NEXT(t) );
6850: NEXT(t) = d; d = l;
6851: d = nd_reconstruct(1,d);
6852: continue;
6853: }
6854: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6855: /* adding new bases */
6856: for ( r = nflist; r; r = NEXT(r) ) {
6857: nfqv = (NDV)BDY(r);
6858: ndv_removecont(0,nfqv);
1.6 noro 6859: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 6860: if ( nd_nalg ) {
6861: ND nf1;
6862:
6863: nf1 = ndvtond(m,nfqv);
6864: nd_monic(0,&nf1);
6865: nd_removecont(0,nf1);
6866: nfqv = ndtondv(0,nf1); nd_free(nf1);
6867: }
6868: nfv = ndv_dup(0,nfqv);
6869: ndv_mod(m,nfv);
6870: ndv_removecont(m,nfv);
6871: nh = ndv_newps(0,nfv,nfqv,1);
6872: d = update_pairs(d,g,nh,0);
6873: g = update_base(g,nh);
6874: }
6875: }
6876: #if 0
6877: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6878: #endif
6879: conv_ilist(nd_demand,1,g,indp);
6880: return g;
6881: }
6882:
6883: int rref(matrix mat,int *sugar)
6884: {
6885: int row,col,i,j,k,l,s,wcol,wj;
6886: unsigned long bj;
6887: unsigned long **a;
6888: unsigned long *ai,*ak,*as,*t;
6889: int *pivot;
6890:
6891: row = mat->row;
6892: col = mat->col;
6893: a = mat->a;
6894: wcol = (col+BLEN-1)/BLEN;
6895: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6896: i = 0;
6897: for ( j = 0; j < col; j++ ) {
6898: wj = j/BLEN; bj = 1L<<(j%BLEN);
6899: for ( k = i; k < row; k++ )
6900: if ( a[k][wj] & bj ) break;
6901: if ( k == row ) continue;
6902: pivot[i] = j;
6903: if ( k != i ) {
6904: t = a[i]; a[i] = a[k]; a[k] = t;
6905: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6906: }
6907: ai = a[i];
6908: for ( k = i+1; k < row; k++ ) {
6909: ak = a[k];
6910: if ( ak[wj] & bj ) {
6911: for ( l = wj; l < wcol; l++ )
6912: ak[l] ^= ai[l];
6913: sugar[k] = MAX(sugar[k],sugar[i]);
6914: }
6915: }
6916: i++;
6917: }
6918: for ( k = i-1; k >= 0; k-- ) {
6919: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6920: ak = a[k];
6921: for ( s = 0; s < k; s++ ) {
6922: as = a[s];
6923: if ( as[wj] & bj ) {
6924: for ( l = wj; l < wcol; l++ )
6925: as[l] ^= ak[l];
6926: sugar[s] = MAX(sugar[s],sugar[k]);
6927: }
6928: }
6929: }
6930: return i;
6931: }
6932:
6933: void print_matrix(matrix mat)
6934: {
6935: int row,col,i,j;
6936: unsigned long *ai;
6937:
6938: row = mat->row;
6939: col = mat->col;
6940: printf("%d x %d\n",row,col);
6941: for ( i = 0; i < row; i++ ) {
6942: ai = mat->a[i];
6943: for ( j = 0; j < col; j++ ) {
6944: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6945: else putchar('0');
6946: }
6947: putchar('\n');
6948: }
6949: }
6950:
6951: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6952:
6953: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6954: {
6955: int row,col,wcol,wj,i,j;
6956: unsigned long bj;
6957: unsigned long *ai;
6958: unsigned long **a;
6959: int len;
6960: int *pos;
6961:
6962: row = mat->row;
6963: col = mat->col;
6964: wcol = (col+BLEN-1)/BLEN;
6965: pos = (int *)MALLOC(wcol*sizeof(int));
6966: bzero(pos,wcol*sizeof(int));
6967: for ( i = j = 0; i < wcol; i++ )
6968: if ( v[i] ) pos[j++] = i;;
6969: len = j;
6970: wj = rhead/BLEN;
6971: bj = 1L<<rhead%BLEN;
6972: a = mat->a;
6973: for ( i = 0; i < row; i++ ) {
6974: ai = a[i];
6975: if ( ai[wj]&bj ) {
6976: for ( j = 0; j < len; j++ )
6977: ai[pos[j]] ^= v[pos[j]];
6978: sugar[i] = MAX(sugar[i],rsugar);
6979: }
6980: }
6981: }
6982:
6983: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
6984: {
6985: int nsp,nred,i,i0,k,rank,row;
6986: NODE r0,rp;
6987: ND_pairs sp;
6988: ND spol;
6989: NM_ind_pair rt;
6990: int *s0hash;
6991: UINT *s;
6992: int *pivot,*sugar,*head;
6993: matrix mat;
6994: NM m;
6995: NODE r;
6996: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
6997: int rhead,rsugar,size;
6998: unsigned long *v;
6999:
7000: get_eg(&eg0);
7001: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7002: nred = length(rp0);
7003: mat = alloc_matrix(nsp,col);
7004: s0hash = (int *)MALLOC(col*sizeof(int));
7005: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7006: s0hash[i] = ndl_hash_value(s);
7007:
7008: sugar = (int *)MALLOC(nsp*sizeof(int));
7009: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7010: nd_sp(2,0,sp,&spol);
7011: if ( spol ) {
7012: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7013: sugar[i] = SG(spol);
7014: i++;
7015: }
7016: }
7017: mat->row = i;
7018: if ( DP_Print ) {
7019: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7020: }
7021: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7022: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7023: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7024: rt = (NM_ind_pair)BDY(rp);
7025: bzero(v,size);
7026: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7027: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7028: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7029: }
7030:
7031: get_eg(&eg1);
7032: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7033: rank = rref(mat,sugar);
7034:
7035: for ( i = 0, r0 = 0; i < rank; i++ ) {
7036: NEXTNODE(r0,r);
7037: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7038: SG((NDV)BDY(r)) = sugar[i];
7039: }
7040: if ( r0 ) NEXT(r) = 0;
7041: get_eg(&eg2);
7042: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7043: if ( DP_Print ) {
7044: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7045: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7046: fflush(asir_out);
7047: }
7048: return r0;
7049: }
7050:
7051:
7052: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7053: {
7054: IndArray *imat;
1.11 ! noro 7055: int nsp,nred,i,start;
1.1 noro 7056: int *rhead;
7057: NODE r0,rp;
7058: ND_pairs sp;
7059: NM_ind_pair *rvect;
7060: UINT *s;
7061: int *s0hash;
1.11 ! noro 7062: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7063:
7064: if ( m == 2 && nd_rref2 )
7065: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7066:
7067: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7068: nred = length(rp0);
7069: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7070: rhead = (int *)MALLOC(col*sizeof(int));
7071: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7072:
7073: /* construction of index arrays */
1.11 ! noro 7074: get_eg(&eg0);
1.1 noro 7075: if ( DP_Print ) {
1.11 ! noro 7076: fprintf(asir_out,"%dx%d,",nsp+nred,col);
! 7077: fflush(asir_out);
1.1 noro 7078: }
7079: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 ! noro 7080: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7081: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 ! noro 7082: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7083: rhead[imat[i]->head] = 1;
1.11 ! noro 7084: start = imat[i]->head;
! 7085: }
! 7086: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1);
! 7087: if ( DP_Print ) {
! 7088: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
! 7089: fflush(asir_out);
1.1 noro 7090: }
7091: if ( m > 0 )
1.7 noro 7092: #if SIZEOF_LONG==8
1.1 noro 7093: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7094: #else
7095: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7096: #endif
7097: else if ( m == -1 )
7098: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7099: else if ( m == -2 )
7100: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7101: else
7102: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7103: return r0;
7104: }
7105:
7106: /* for Fp, 2<=p<2^16 */
7107:
7108: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7109: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7110: {
7111: int spcol,sprow,a;
7112: int i,j,k,l,rank;
7113: NODE r0,r;
7114: ND_pairs sp;
7115: ND spol;
7116: UINT **spmat;
7117: UINT *svect,*v;
7118: int *colstat;
7119: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7120: int maxrs;
7121: int *spsugar;
7122: ND_pairs *spactive;
7123:
7124: spcol = col-nred;
7125: get_eg(&eg0);
7126: /* elimination (1st step) */
7127: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7128: svect = (UINT *)MALLOC(col*sizeof(UINT));
7129: spsugar = (int *)MALLOC(nsp*sizeof(int));
7130: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7131: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7132: nd_sp(m,0,sp,&spol);
7133: if ( !spol ) continue;
7134: nd_to_vect(m,s0vect,col,spol,svect);
7135: if ( m == -1 )
7136: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7137: else
7138: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7139: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7140: if ( i < col ) {
7141: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7142: for ( j = k = 0; j < col; j++ )
7143: if ( !rhead[j] ) v[k++] = svect[j];
7144: spsugar[sprow] = MAX(maxrs,SG(spol));
7145: if ( nz )
7146: spactive[sprow] = sp;
7147: sprow++;
7148: }
7149: nd_free(spol);
7150: }
7151: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7152: if ( DP_Print ) {
1.5 noro 7153: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7154: fflush(asir_out);
7155: }
7156: /* free index arrays */
7157: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7158:
7159: /* elimination (2nd step) */
7160: colstat = (int *)MALLOC(spcol*sizeof(int));
7161: if ( m == -1 )
7162: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7163: else
7164: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7165: r0 = 0;
7166: for ( i = 0; i < rank; i++ ) {
7167: NEXTNODE(r0,r); BDY(r) =
7168: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7169: SG((NDV)BDY(r)) = spsugar[i];
7170: GCFREE(spmat[i]);
7171: }
7172: if ( r0 ) NEXT(r) = 0;
7173:
7174: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7175: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7176: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7177: if ( DP_Print ) {
1.5 noro 7178: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7179: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7180: nsp,nred,sprow,spcol,rank);
1.5 noro 7181: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7182: }
7183: if ( nz ) {
7184: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7185: if ( rank > 0 ) {
7186: NEXT(spactive[rank-1]) = 0;
7187: *nz = spactive[0];
7188: } else
7189: *nz = 0;
7190: }
7191: return r0;
7192: }
7193:
7194:
7195: /* for small finite fields */
7196:
7197: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7198: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7199: {
7200: int spcol,sprow,a;
7201: int i,j,k,l,rank;
7202: NODE r0,r;
7203: ND_pairs sp;
7204: ND spol;
7205: UINT **spmat;
7206: UINT *svect,*v;
7207: int *colstat;
7208: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7209: int maxrs;
7210: int *spsugar;
7211: ND_pairs *spactive;
7212:
7213: spcol = col-nred;
7214: get_eg(&eg0);
7215: /* elimination (1st step) */
7216: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7217: svect = (UINT *)MALLOC(col*sizeof(UINT));
7218: spsugar = (int *)MALLOC(nsp*sizeof(int));
7219: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7220: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7221: nd_sp(m,0,sp,&spol);
7222: if ( !spol ) continue;
7223: nd_to_vect(m,s0vect,col,spol,svect);
7224: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7225: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7226: if ( i < col ) {
7227: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7228: for ( j = k = 0; j < col; j++ )
7229: if ( !rhead[j] ) v[k++] = svect[j];
7230: spsugar[sprow] = MAX(maxrs,SG(spol));
7231: if ( nz )
7232: spactive[sprow] = sp;
7233: sprow++;
7234: }
7235: nd_free(spol);
7236: }
7237: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7238: if ( DP_Print ) {
1.5 noro 7239: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7240: fflush(asir_out);
7241: }
7242: /* free index arrays */
7243: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7244:
7245: /* elimination (2nd step) */
7246: colstat = (int *)MALLOC(spcol*sizeof(int));
7247: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7248: r0 = 0;
7249: for ( i = 0; i < rank; i++ ) {
7250: NEXTNODE(r0,r); BDY(r) =
7251: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7252: SG((NDV)BDY(r)) = spsugar[i];
7253: GCFREE(spmat[i]);
7254: }
7255: if ( r0 ) NEXT(r) = 0;
7256:
7257: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7258: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7259: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7260: if ( DP_Print ) {
1.5 noro 7261: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7262: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7263: nsp,nred,sprow,spcol,rank);
1.5 noro 7264: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7265: }
7266: if ( nz ) {
7267: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7268: if ( rank > 0 ) {
7269: NEXT(spactive[rank-1]) = 0;
7270: *nz = spactive[0];
7271: } else
7272: *nz = 0;
7273: }
7274: return r0;
7275: }
7276:
7277: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7278: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7279: {
7280: int spcol,sprow,a;
7281: int i,j,k,l,rank;
7282: NODE r0,r;
7283: ND_pairs sp;
7284: ND spol;
7285: mpz_t **spmat;
7286: mpz_t *svect,*v;
7287: int *colstat;
7288: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7289: int maxrs;
7290: int *spsugar;
7291: pointer *w;
7292:
7293: spcol = col-nred;
7294: get_eg(&eg0);
7295: /* elimination (1st step) */
7296: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7297: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7298: spsugar = (int *)MALLOC(nsp*sizeof(int));
7299: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7300: nd_sp(m,trace,sp,&spol);
7301: if ( !spol ) continue;
7302: nd_to_vect_lf(s0vect,col,spol,svect);
7303: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7304: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7305: if ( i < col ) {
7306: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7307: for ( j = k = 0; j < col; j++ )
7308: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7309: spsugar[sprow] = MAX(maxrs,SG(spol));
7310: sprow++;
7311: }
7312: /* nd_free(spol); */
7313: }
7314: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7315: if ( DP_Print ) {
1.5 noro 7316: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7317: fflush(asir_out);
7318: }
7319: /* free index arrays */
7320: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7321:
7322: /* elimination (2nd step) */
7323: colstat = (int *)MALLOC(spcol*sizeof(int));
7324: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7325: w = (pointer *)MALLOC(rank*sizeof(pointer));
7326: for ( i = 0; i < rank; i++ ) {
7327: #if 0
7328: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7329: SG((NDV)w[rank-i-1]) = spsugar[i];
7330: #else
7331: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7332: SG((NDV)w[i]) = spsugar[i];
7333: #endif
7334: /* GCFREE(spmat[i]); */
7335:
7336: }
7337: #if 0
7338: qsort(w,rank,sizeof(NDV),
7339: (int (*)(const void *,const void *))ndv_compare);
7340: #endif
7341: r0 = 0;
7342: for ( i = 0; i < rank; i++ ) {
7343: NEXTNODE(r0,r); BDY(r) = w[i];
7344: }
7345: if ( r0 ) NEXT(r) = 0;
7346:
7347: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7348: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7349: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7350: if ( DP_Print ) {
1.5 noro 7351: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7352: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7353: nsp,nred,sprow,spcol,rank);
1.5 noro 7354: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7355: }
7356: return r0;
7357: }
7358:
7359: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7360: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7361: {
7362: int spcol,sprow,a;
7363: int i,j,k,l,rank;
7364: NODE r0,r;
7365: ND_pairs sp;
7366: ND spol;
7367: Z **spmat;
7368: Z *svect,*v;
7369: int *colstat;
7370: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7371: int maxrs;
7372: int *spsugar;
7373: pointer *w;
7374:
7375: spcol = col-nred;
7376: get_eg(&eg0);
7377: /* elimination (1st step) */
7378: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7379: svect = (Z *)MALLOC(col*sizeof(Q));
7380: spsugar = (int *)MALLOC(nsp*sizeof(int));
7381: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7382: nd_sp(0,trace,sp,&spol);
7383: if ( !spol ) continue;
7384: nd_to_vect_q(s0vect,col,spol,svect);
7385: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7386: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7387: if ( i < col ) {
7388: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7389: for ( j = k = 0; j < col; j++ )
7390: if ( !rhead[j] ) v[k++] = svect[j];
7391: spsugar[sprow] = MAX(maxrs,SG(spol));
7392: sprow++;
7393: }
7394: /* nd_free(spol); */
7395: }
7396: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7397: if ( DP_Print ) {
1.5 noro 7398: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7399: fflush(asir_out);
7400: }
7401: /* free index arrays */
7402: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7403:
7404: /* elimination (2nd step) */
7405: colstat = (int *)MALLOC(spcol*sizeof(int));
7406: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7407: w = (pointer *)MALLOC(rank*sizeof(pointer));
7408: for ( i = 0; i < rank; i++ ) {
7409: #if 0
7410: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7411: SG((NDV)w[rank-i-1]) = spsugar[i];
7412: #else
7413: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7414: SG((NDV)w[i]) = spsugar[i];
7415: #endif
7416: /* GCFREE(spmat[i]); */
7417: }
7418: #if 0
7419: qsort(w,rank,sizeof(NDV),
7420: (int (*)(const void *,const void *))ndv_compare);
7421: #endif
7422: r0 = 0;
7423: for ( i = 0; i < rank; i++ ) {
7424: NEXTNODE(r0,r); BDY(r) = w[i];
7425: }
7426: if ( r0 ) NEXT(r) = 0;
7427:
7428: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7429: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7430: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7431: if ( DP_Print ) {
1.5 noro 7432: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7433: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7434: nsp,nred,sprow,spcol,rank);
1.5 noro 7435: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7436: }
7437: return r0;
7438: }
7439:
7440: FILE *nd_write,*nd_read;
7441:
7442: void nd_send_int(int a) {
7443: write_int(nd_write,(unsigned int *)&a);
7444: }
7445:
7446: void nd_send_intarray(int *p,int len) {
7447: write_intarray(nd_write,(unsigned int *)p,len);
7448: }
7449:
7450: int nd_recv_int() {
7451: int a;
7452:
7453: read_int(nd_read,(unsigned int *)&a);
7454: return a;
7455: }
7456:
7457: void nd_recv_intarray(int *p,int len) {
7458: read_intarray(nd_read,(unsigned int *)p,len);
7459: }
7460:
7461: void nd_send_ndv(NDV p) {
7462: int len,i;
7463: NMV m;
7464:
7465: if ( !p ) nd_send_int(0);
7466: else {
7467: len = LEN(p);
7468: nd_send_int(len);
7469: m = BDY(p);
7470: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7471: nd_send_int(CM(m));
7472: nd_send_intarray((int *)DL(m),nd_wpd);
7473: }
7474: }
7475: }
7476:
7477: void nd_send_nd(ND p) {
7478: int len,i;
7479: NM m;
7480:
7481: if ( !p ) nd_send_int(0);
7482: else {
7483: len = LEN(p);
7484: nd_send_int(len);
7485: m = BDY(p);
7486: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7487: nd_send_int(CM(m));
7488: nd_send_intarray((int *)DL(m),nd_wpd);
7489: }
7490: }
7491: }
7492:
7493: NDV nd_recv_ndv()
7494: {
7495: int len,i;
7496: NMV m,m0;
7497: NDV r;
7498:
7499: len = nd_recv_int();
7500: if ( !len ) return 0;
7501: else {
7502: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7503: #if 0
7504: ndv_alloc += len*nmv_adv;
7505: #endif
7506: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7507: CM(m) = nd_recv_int();
7508: nd_recv_intarray((int *)DL(m),nd_wpd);
7509: }
7510: MKNDV(nd_nvar,m0,len,r);
7511: return r;
7512: }
7513: }
7514:
7515: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7516: {
7517: int i,j,t,c,rank,inv;
7518: int *ci,*ri;
7519: Z dn;
7520: MAT m,nm;
7521:
7522: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7523: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7524: for ( i = 0; i < row; i++ )
7525: for ( j = 0; j < col; j++ )
7526: mat0[i][j] = 0;
7527: c = col-rank;
7528: for ( i = 0; i < rank; i++ ) {
7529: mat0[i][ri[i]] = dn;
7530: for ( j = 0; j < c; j++ )
7531: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7532: }
7533: return rank;
7534: }
7535:
7536: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7537: {
7538: int i,j,k,l,inv,a,rank,s;
7539: unsigned int *t,*pivot,*pk;
7540: unsigned int **mat;
7541: ND_pairs pair;
7542:
7543: mat = (unsigned int **)mat0;
7544: for ( rank = 0, j = 0; j < col; j++ ) {
7545: for ( i = rank; i < row; i++ )
7546: mat[i][j] %= md;
7547: for ( i = rank; i < row; i++ )
7548: if ( mat[i][j] )
7549: break;
7550: if ( i == row ) {
7551: colstat[j] = 0;
7552: continue;
7553: } else
7554: colstat[j] = 1;
7555: if ( i != rank ) {
7556: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7557: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7558: if ( spactive ) {
7559: pair = spactive[i]; spactive[i] = spactive[rank];
7560: spactive[rank] = pair;
7561: }
7562: }
7563: pivot = mat[rank];
7564: s = sugar[rank];
7565: inv = invm(pivot[j],md);
7566: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7567: if ( *pk ) {
7568: if ( *pk >= (unsigned int)md )
7569: *pk %= md;
7570: DMAR(*pk,inv,0,md,*pk)
7571: }
7572: for ( i = rank+1; i < row; i++ ) {
7573: t = mat[i];
7574: if ( (a = t[j]) != 0 ) {
7575: sugar[i] = MAX(sugar[i],s);
7576: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7577: }
7578: }
7579: rank++;
7580: }
7581: for ( j = col-1, l = rank-1; j >= 0; j-- )
7582: if ( colstat[j] ) {
7583: pivot = mat[l];
7584: s = sugar[l];
7585: for ( i = 0; i < l; i++ ) {
7586: t = mat[i];
7587: t[j] %= md;
7588: if ( (a = t[j]) != 0 ) {
7589: sugar[i] = MAX(sugar[i],s);
7590: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7591: }
7592: }
7593: l--;
7594: }
7595: for ( j = 0, l = 0; l < rank; j++ )
7596: if ( colstat[j] ) {
7597: t = mat[l];
7598: for ( k = j; k < col; k++ )
7599: if ( t[k] >= (unsigned int)md )
7600: t[k] %= md;
7601: l++;
7602: }
7603: return rank;
7604: }
7605:
7606:
1.7 noro 7607: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7608: {
1.7 noro 7609: int i,j,k,l,inv,a,rank,s;
7610: unsigned int *t,*pivot,*pk;
7611: unsigned int **mat;
7612:
7613: mat = (unsigned int **)mat0;
7614: for ( rank = 0, j = 0; j < col; j++ ) {
7615: for ( i = rank; i < row; i++ )
7616: if ( mat[i][j] )
7617: break;
7618: if ( i == row ) {
7619: colstat[j] = 0;
7620: continue;
7621: } else
7622: colstat[j] = 1;
7623: if ( i != rank ) {
7624: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7625: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7626: }
7627: pivot = mat[rank];
7628: s = sugar[rank];
7629: inv = _invsf(pivot[j]);
7630: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7631: if ( *pk )
7632: *pk = _mulsf(*pk,inv);
7633: for ( i = rank+1; i < row; i++ ) {
7634: t = mat[i];
7635: if ( (a = t[j]) != 0 ) {
7636: sugar[i] = MAX(sugar[i],s);
7637: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7638: }
7639: }
7640: rank++;
7641: }
7642: for ( j = col-1, l = rank-1; j >= 0; j-- )
7643: if ( colstat[j] ) {
7644: pivot = mat[l];
7645: s = sugar[l];
7646: for ( i = 0; i < l; i++ ) {
7647: t = mat[i];
7648: if ( (a = t[j]) != 0 ) {
7649: sugar[i] = MAX(sugar[i],s);
7650: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7651: }
7652: }
7653: l--;
7654: }
7655: return rank;
7656: }
1.1 noro 7657:
1.7 noro 7658: int ndv_ishomo(NDV p)
7659: {
7660: NMV m;
7661: int len,h;
1.1 noro 7662:
7663: if ( !p ) return 1;
7664: len = LEN(p);
7665: m = BDY(p);
7666: h = TD(DL(m));
7667: NMV_ADV(m);
7668: for ( len--; len; len--, NMV_ADV(m) )
7669: if ( TD(DL(m)) != h ) return 0;
7670: return 1;
7671: }
7672:
7673: void ndv_save(NDV p,int index)
7674: {
7675: FILE *s;
7676: char name[BUFSIZ];
7677: short id;
7678: int nv,sugar,len,n,i,td,e,j;
7679: NMV m;
7680: unsigned int *dl;
7681: int mpos;
7682:
7683: sprintf(name,"%s/%d",Demand,index);
7684: s = fopen(name,"w");
7685: savevl(s,0);
7686: if ( !p ) {
7687: saveobj(s,0);
7688: return;
7689: }
7690: id = O_DP;
7691: nv = NV(p);
7692: sugar = SG(p);
7693: len = LEN(p);
7694: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7695: write_int(s,(unsigned int *)&len);
7696:
7697: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7698: saveobj(s,(Obj)CZ(m));
1.1 noro 7699: dl = DL(m);
7700: td = TD(dl);
7701: write_int(s,(unsigned int *)&td);
7702: for ( j = 0; j < nv; j++ ) {
7703: e = GET_EXP(dl,j);
7704: write_int(s,(unsigned int *)&e);
7705: }
7706: if ( nd_module ) {
7707: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7708: }
7709: }
7710: fclose(s);
7711: }
7712:
7713: void nd_save_mod(ND p,int index)
7714: {
7715: FILE *s;
7716: char name[BUFSIZ];
7717: int nv,sugar,len,c;
7718: NM m;
7719:
7720: sprintf(name,"%s/%d",Demand,index);
7721: s = fopen(name,"w");
7722: if ( !p ) {
7723: len = 0;
7724: write_int(s,(unsigned int *)&len);
7725: fclose(s);
7726: return;
7727: }
7728: nv = NV(p);
7729: sugar = SG(p);
7730: len = LEN(p);
7731: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7732: for ( m = BDY(p); m; m = NEXT(m) ) {
7733: c = CM(m); write_int(s,(unsigned int *)&c);
7734: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7735: }
7736: fclose(s);
7737: }
7738:
7739: NDV ndv_load(int index)
7740: {
7741: FILE *s;
7742: char name[BUFSIZ];
7743: short id;
7744: int nv,sugar,len,n,i,td,e,j;
7745: NDV d;
7746: NMV m0,m;
7747: unsigned int *dl;
7748: Obj obj;
7749: int mpos;
7750:
7751: sprintf(name,"%s/%d",Demand,index);
7752: s = fopen(name,"r");
7753: if ( !s ) return 0;
7754:
7755: skipvl(s);
7756: read_short(s,(unsigned short *)&id);
7757: if ( !id ) return 0;
7758: read_int(s,(unsigned int *)&nv);
7759: read_int(s,(unsigned int *)&sugar);
7760: read_int(s,(unsigned int *)&len);
7761:
7762: m0 = m = MALLOC(len*nmv_adv);
7763: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7764: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7765: dl = DL(m);
7766: ndl_zero(dl);
7767: read_int(s,(unsigned int *)&td); TD(dl) = td;
7768: for ( j = 0; j < nv; j++ ) {
7769: read_int(s,(unsigned int *)&e);
7770: PUT_EXP(dl,j,e);
7771: }
7772: if ( nd_module ) {
7773: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7774: }
7775: if ( nd_blockmask ) ndl_weight_mask(dl);
7776: }
7777: fclose(s);
7778: MKNDV(nv,m0,len,d);
7779: SG(d) = sugar;
7780: return d;
7781: }
7782:
7783: ND nd_load_mod(int index)
7784: {
7785: FILE *s;
7786: char name[BUFSIZ];
7787: int nv,sugar,len,i,c;
7788: ND d;
7789: NM m0,m;
7790:
7791: sprintf(name,"%s/%d",Demand,index);
7792: s = fopen(name,"r");
7793: /* if the file does not exist, it means p[index]=0 */
7794: if ( !s ) return 0;
7795:
7796: read_int(s,(unsigned int *)&nv);
7797: if ( !nv ) { fclose(s); return 0; }
7798:
7799: read_int(s,(unsigned int *)&sugar);
7800: read_int(s,(unsigned int *)&len);
7801: for ( m0 = 0, i = 0; i < len; i++ ) {
7802: NEXTNM(m0,m);
7803: read_int(s,(unsigned int *)&c); CM(m) = c;
7804: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7805: }
7806: NEXT(m) = 0;
7807: MKND(nv,m0,len,d);
7808: SG(d) = sugar;
7809: fclose(s);
7810: return d;
7811: }
7812:
7813: void nd_det(int mod,MAT f,P *rp)
7814: {
7815: VL fv,tv;
7816: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7817: pointer **m;
7818: P **w;
7819: P mp,r;
7820: NDV **dm;
7821: NDV *t,*mi,*mj;
7822: NDV d,s,mij,mjj;
7823: ND u;
7824: NMV nmv;
7825: UINT *bound;
7826: PGeoBucket bucket;
7827: struct order_spec *ord;
7828: Z dq,dt,ds;
7829: Z mone;
7830: Z gn,qn,dn0,nm,dn;
7831:
7832: create_order_spec(0,0,&ord);
7833: nd_init_ord(ord);
7834: get_vars((Obj)f,&fv);
7835: if ( f->row != f->col )
7836: error("nd_det : non-square matrix");
7837: n = f->row;
7838: m = f->body;
7839: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7840:
7841: if ( !nvar ) {
7842: if ( !mod )
7843: detp(CO,(P **)m,n,rp);
7844: else {
7845: w = (P **)almat_pointer(n,n);
7846: for ( i = 0; i < n; i++ )
7847: for ( j = 0; j < n; j++ )
7848: ptomp(mod,(P)m[i][j],&w[i][j]);
7849: detmp(CO,mod,w,n,&mp);
7850: mptop(mp,rp);
7851: }
7852: return;
7853: }
7854:
7855: if ( !mod ) {
7856: w = (P **)almat_pointer(n,n);
7857: dq = ONE;
7858: for ( i = 0; i < n; i++ ) {
7859: dn0 = ONE;
7860: for ( j = 0; j < n; j++ ) {
7861: if ( !m[i][j] ) continue;
7862: lgp(m[i][j],&nm,&dn);
1.6 noro 7863: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 7864: }
7865: if ( !UNIZ(dn0) ) {
7866: ds = dn0;
7867: for ( j = 0; j < n; j++ )
7868: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
7869: mulz(dq,ds,&dt); dq = dt;
7870: } else
7871: for ( j = 0; j < n; j++ )
7872: w[i][j] = (P)m[i][j];
7873: }
7874: m = (pointer **)w;
7875: }
7876:
7877: for ( i = 0, max = 1; i < n; i++ )
7878: for ( j = 0; j < n; j++ )
7879: for ( tv = fv; tv; tv = NEXT(tv) ) {
7880: e = getdeg(tv->v,(P)m[i][j]);
7881: max = MAX(e,max);
7882: }
7883: nd_setup_parameters(nvar,max);
7884: dm = (NDV **)almat_pointer(n,n);
7885: for ( i = 0, max = 1; i < n; i++ )
7886: for ( j = 0; j < n; j++ ) {
7887: dm[i][j] = ptondv(CO,fv,m[i][j]);
7888: if ( mod ) ndv_mod(mod,dm[i][j]);
7889: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
7890: }
7891: d = ptondv(CO,fv,(P)ONE);
7892: if ( mod ) ndv_mod(mod,d);
7893: chsgnz(ONE,&mone);
7894: for ( j = 0, sgn = 1; j < n; j++ ) {
7895: if ( DP_Print ) {
7896: fprintf(asir_out,".");
7897: }
7898: for ( i = j; i < n && !dm[i][j]; i++ );
7899: if ( i == n ) {
7900: *rp = 0;
7901: return;
7902: }
7903: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
7904: for ( k = j; k < n; k++ )
7905: for ( l = j; l < n; l++ )
7906: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
7907: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
7908: }
7909: if ( k0 != j ) {
7910: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
7911: sgn = -sgn;
7912: }
7913: if ( l0 != j ) {
7914: for ( k = j; k < n; k++ ) {
7915: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
7916: }
7917: sgn = -sgn;
7918: }
7919: bound = nd_det_compute_bound(dm,n,j);
7920: for ( k = 0; k < nd_nvar; k++ )
7921: if ( bound[k]*2 > nd_mask0 ) break;
7922: if ( k < nd_nvar )
7923: nd_det_reconstruct(dm,n,j,d);
7924:
7925: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
7926: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
7927: mi = dm[i]; mij = mi[j];
7928: if ( mod )
7929: ndv_mul_c(mod,mij,mod-1);
7930: else
7931: ndv_mul_c_q(mij,mone);
7932: for ( k = j+1; k < n; k++ ) {
7933: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
7934: bucket = create_pbucket();
7935: if ( mi[k] ) {
7936: nmv = BDY(mjj); len = LEN(mjj);
7937: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7938: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
7939: add_pbucket(mod,bucket,u);
7940: }
7941: }
7942: if ( mj[k] && mij ) {
7943: nmv = BDY(mij); len = LEN(mij);
7944: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7945: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
7946: add_pbucket(mod,bucket,u);
7947: }
7948: }
7949: u = nd_quo(mod,bucket,d);
7950: mi[k] = ndtondv(mod,u);
7951: }
7952: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
7953: }
7954: d = mjj;
7955: }
7956: if ( DP_Print ) {
7957: fprintf(asir_out,"\n");
7958: }
7959: if ( sgn < 0 ) {
7960: if ( mod )
7961: ndv_mul_c(mod,d,mod-1);
7962: else
7963: ndv_mul_c_q(d,mone);
7964: }
7965: r = ndvtop(mod,CO,fv,d);
7966: if ( !mod && !UNIQ(dq) )
7967: divsp(CO,r,(P)dq,rp);
7968: else
7969: *rp = r;
7970: }
7971:
7972: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
7973: {
7974: NM mr,mr0;
7975: NM tnm;
7976: NMV m;
7977: UINT *d0,*dt,*dm;
7978: int c,n,td,i,c1,c2,len;
7979: Z q;
7980: ND r;
7981:
7982: if ( !p ) return 0;
7983: else {
7984: n = NV(p); m = BDY(p); len = LEN(p);
7985: d0 = DL(m0);
7986: td = TD(d);
7987: mr0 = 0;
7988: NEWNM(tnm);
7989: if ( mod ) {
7990: c = CM(m0);
7991: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7992: ndl_add(DL(m),d0,DL(tnm));
7993: if ( ndl_reducible(DL(tnm),d) ) {
7994: NEXTNM(mr0,mr);
7995: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
7996: ndl_copy(DL(tnm),DL(mr));
7997: }
7998: }
7999: } else {
1.6 noro 8000: q = CZ(m0);
1.1 noro 8001: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8002: ndl_add(DL(m),d0,DL(tnm));
8003: if ( ndl_reducible(DL(tnm),d) ) {
8004: NEXTNM(mr0,mr);
1.6 noro 8005: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8006: ndl_copy(DL(tnm),DL(mr));
8007: }
8008: }
8009: }
8010: if ( !mr0 )
8011: return 0;
8012: else {
8013: NEXT(mr) = 0;
8014: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8015: MKND(NV(p),mr0,len,r);
8016: SG(r) = SG(p) + TD(d0);
8017: return r;
8018: }
8019: }
8020: }
8021:
8022: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8023: {
8024: int i,obpe,oadv,h,k,l;
8025: static NM prev_nm_free_list;
8026: EPOS oepos;
8027:
8028: obpe = nd_bpe;
8029: oadv = nmv_adv;
8030: oepos = nd_epos;
8031: if ( obpe < 2 ) nd_bpe = 2;
8032: else if ( obpe < 3 ) nd_bpe = 3;
8033: else if ( obpe < 4 ) nd_bpe = 4;
8034: else if ( obpe < 5 ) nd_bpe = 5;
8035: else if ( obpe < 6 ) nd_bpe = 6;
8036: else if ( obpe < 8 ) nd_bpe = 8;
8037: else if ( obpe < 10 ) nd_bpe = 10;
8038: else if ( obpe < 16 ) nd_bpe = 16;
8039: else if ( obpe < 32 ) nd_bpe = 32;
8040: else error("nd_det_reconstruct : exponent too large");
8041:
8042: nd_setup_parameters(nd_nvar,0);
8043: prev_nm_free_list = _nm_free_list;
8044: _nm_free_list = 0;
8045: for ( k = j; k < n; k++ )
8046: for (l = j; l < n; l++ )
8047: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8048: ndv_realloc(d,obpe,oadv,oepos);
8049: prev_nm_free_list = 0;
8050: #if 0
8051: GC_gcollect();
8052: #endif
8053: }
8054:
8055: /* returns a UINT array containing degree bounds */
8056:
8057: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8058: {
8059: UINT *d0,*d1,*d,*t,*r;
8060: int k,l,i;
8061:
8062: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8063: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8064: for ( k = j; k < n; k++ )
8065: for ( l = j; l < n; l++ )
8066: if ( dm[k][l] ) {
8067: d = ndv_compute_bound(dm[k][l]);
8068: for ( i = 0; i < nd_nvar; i++ )
8069: d0[i] = MAX(d0[i],d[i]);
8070: }
8071: return d0;
8072: }
8073:
8074: DL nd_separate_d(UINT *d,UINT *trans)
8075: {
8076: int n,td,i,e,j;
8077: DL a;
8078:
8079: ndl_zero(trans);
8080: td = 0;
8081: for ( i = 0; i < nd_ntrans; i++ ) {
8082: e = GET_EXP(d,i);
8083: PUT_EXP(trans,i,e);
8084: td += MUL_WEIGHT(e,i);
8085: }
8086: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8087: /* homogenized */
8088: i = nd_nvar-1;
8089: e = GET_EXP(d,i);
8090: PUT_EXP(trans,i,e);
8091: td += MUL_WEIGHT(e,i);
8092: }
8093: TD(trans) = td;
8094: if ( nd_blockmask) ndl_weight_mask(trans);
8095: NEWDL(a,nd_nalg);
8096: td = 0;
8097: for ( i = 0; i < nd_nalg; i++ ) {
8098: j = nd_ntrans+i;
8099: e = GET_EXP(d,j);
8100: a->d[i] = e;
8101: td += e;
8102: }
8103: a->td = td;
8104: return a;
8105: }
8106:
8107: int nd_monic(int mod,ND *p)
8108: {
8109: UINT *trans,*t;
8110: DL alg;
8111: MP mp0,mp;
8112: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8113: ND r;
8114: DL dl;
8115: DP nm;
8116: NDV ndv;
8117: DAlg inv,cd;
8118: ND s,c;
8119: Z l,mul;
8120: Z ln;
8121: int n,ntrans,i,e,td,is_lc,len;
8122: NumberField nf;
8123: struct oEGT eg0,eg1;
8124:
8125: if ( !(nf = get_numberfield()) )
8126: error("nd_monic : current_numberfield is not set");
8127:
8128: /* Q coef -> DAlg coef */
8129: NEWNM(ma0); ma = ma0;
8130: m = BDY(*p);
8131: is_lc = 1;
8132: while ( 1 ) {
8133: NEWMP(mp0); mp = mp0;
1.6 noro 8134: mp->c = (Obj)CZ(m);
1.1 noro 8135: mp->dl = nd_separate_d(DL(m),DL(ma));
8136: NEWNM(mb);
8137: for ( m = NEXT(m); m; m = NEXT(m) ) {
8138: alg = nd_separate_d(DL(m),DL(mb));
8139: if ( !ndl_equal(DL(ma),DL(mb)) )
8140: break;
1.6 noro 8141: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8142: }
8143: NEXT(mp) = 0;
8144: MKDP(nd_nalg,mp0,nm);
8145: MKDAlg(nm,ONE,cd);
8146: if ( is_lc == 1 ) {
8147: /* if the lc is a rational number, we have nothing to do */
8148: if ( !mp0->dl->td )
8149: return 1;
8150:
8151: get_eg(&eg0);
8152: invdalg(cd,&inv);
8153: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8154: /* check the validity of inv */
8155: if ( mod && !remqi((Q)inv->dn,mod) )
8156: return 0;
8157: CA(ma) = nf->one;
8158: is_lc = 0;
8159: ln = ONE;
8160: } else {
8161: muldalg(cd,inv,&CA(ma));
8162: lcmz(ln,CA(ma)->dn,&ln);
8163: }
8164: if ( m ) {
8165: NEXT(ma) = mb; ma = mb;
8166: } else {
8167: NEXT(ma) = 0;
8168: break;
8169: }
8170: }
8171: /* l = lcm(denoms) */
8172: l = ln;
8173: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8174: divsz(l,CA(m)->dn,&mul);
1.1 noro 8175: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8176: NEXTNM(mr0,mr);
1.6 noro 8177: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8178: dl = mp->dl;
8179: td = TD(DL(m));
8180: ndl_copy(DL(m),DL(mr));
8181: for ( i = 0; i < nd_nalg; i++ ) {
8182: e = dl->d[i];
8183: PUT_EXP(DL(mr),i+nd_ntrans,e);
8184: td += MUL_WEIGHT(e,i+nd_ntrans);
8185: }
8186: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8187: TD(DL(mr)) = td;
8188: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8189: }
8190: }
8191: NEXT(mr) = 0;
8192: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8193: MKND(NV(*p),mr0,len,r);
8194: /* XXX */
8195: SG(r) = SG(*p);
8196: nd_free(*p);
8197: *p = r;
8198: return 1;
8199: }
8200:
8201: NODE reverse_node(NODE n)
8202: {
8203: NODE t,t1;
8204:
8205: for ( t = 0; n; n = NEXT(n) ) {
8206: MKNODE(t1,BDY(n),t); t = t1;
8207: }
8208: return t;
8209: }
8210:
8211: P ndc_div(int mod,union oNDC a,union oNDC b)
8212: {
8213: union oNDC c;
8214: int inv,t;
8215:
8216: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8217: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8218: else if ( mod ) {
8219: inv = invm(b.m,mod);
8220: DMAR(a.m,inv,0,mod,t); c.m = t;
8221: } else if ( nd_vc )
8222: divsp(nd_vc,a.p,b.p,&c.p);
8223: else
8224: divsz(a.z,b.z,&c.z);
8225: return ndctop(mod,c);
8226: }
8227:
8228: P ndctop(int mod,union oNDC c)
8229: {
8230: Z q;
8231: int e;
8232: GFS gfs;
8233:
8234: if ( mod == -1 ) {
8235: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8236: } else if ( mod == -2 ) {
1.10 noro 8237: q = c.z; return (P)q;
1.1 noro 8238: } else if ( mod > 0 ) {
1.6 noro 8239: STOZ(c.m,q); return (P)q;
1.1 noro 8240: } else
8241: return (P)c.p;
8242: }
8243:
8244: /* [0,0,0,cont] = p -> p/cont */
8245:
8246: void finalize_tracelist(int i,P cont)
8247: {
8248: LIST l;
8249: NODE node;
8250: Z iq;
8251:
8252: if ( !UNIQ(cont) ) {
8253: node = mknode(4,NULLP,NULLP,NULLP,cont);
8254: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8255: nd_tracelist = node;
8256: }
1.6 noro 8257: STOZ(i,iq);
1.1 noro 8258: nd_tracelist = reverse_node(nd_tracelist);
8259: MKLIST(l,nd_tracelist);
8260: node = mknode(2,iq,l); MKLIST(l,node);
8261: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8262: nd_alltracelist = node; nd_tracelist = 0;
8263: }
8264:
8265: void conv_ilist(int demand,int trace,NODE g,int **indp)
8266: {
8267: int n,i,j;
8268: int *ind;
8269: NODE t;
8270:
8271: n = length(g);
8272: ind = (int *)MALLOC(n*sizeof(int));
8273: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8274: j = (long)BDY(t); ind[i] = j;
8275: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8276: }
8277: if ( indp ) *indp = ind;
8278: }
8279:
8280: void parse_nd_option(NODE opt)
8281: {
8282: NODE t,p,u;
8283: int i,s,n;
8284: char *key;
8285: Obj value;
8286:
8287: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8288: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8289: nd_splist = 0; nd_check_splist = 0;
8290: nd_sugarweight = 0;
8291: nd_f4red =0;
8292: nd_rank0 = 0;
8293: for ( t = opt; t; t = NEXT(t) ) {
8294: p = BDY((LIST)BDY(t));
8295: key = BDY((STRING)BDY(p));
8296: value = (Obj)BDY(NEXT(p));
8297: if ( !strcmp(key,"gentrace") )
8298: nd_gentrace = value?1:0;
8299: else if ( !strcmp(key,"gensyz") )
8300: nd_gensyz = value?1:0;
8301: else if ( !strcmp(key,"nora") )
8302: nd_nora = value?1:0;
8303: else if ( !strcmp(key,"gbblock") ) {
8304: if ( value && OID(value) == O_LIST ) {
8305: u = BDY((LIST)value);
8306: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8307: for ( i = 0; u; u = NEXT(u) ) {
8308: p = BDY((LIST)BDY(u));
1.6 noro 8309: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8310: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8311: }
8312: nd_gbblock[i] = -1;
8313: } else
8314: nd_gbblock = 0;
8315: } else if ( !strcmp(key,"newelim") )
8316: nd_newelim = value?1:0;
8317: else if ( !strcmp(key,"intersect") )
8318: nd_intersect = value?1:0;
8319: else if ( !strcmp(key,"lf") )
8320: nd_lf = value?1:0;
8321: else if ( !strcmp(key,"trace") ) {
8322: if ( value ) {
8323: u = BDY((LIST)value);
8324: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8325: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8326: }
8327: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8328: nd_f4red = ZTOS((Q)value);
1.1 noro 8329: } else if ( !strcmp(key,"rank0") ) {
8330: nd_rank0 = value?1:0;
8331: } else if ( !strcmp(key,"splist") ) {
8332: nd_splist = value?1:0;
8333: } else if ( !strcmp(key,"check_splist") ) {
8334: nd_check_splist = BDY((LIST)value);
8335: } else if ( !strcmp(key,"sugarweight") ) {
8336: u = BDY((LIST)value);
8337: n = length(u);
8338: nd_sugarweight = MALLOC(n*sizeof(int));
8339: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8340: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8341: }
8342: }
8343: }
8344:
8345: ND mdptond(DP d);
8346: ND nd_mul_nm(int mod,NM m0,ND p);
8347: ND nd_mul_nm_lf(NM m0,ND p);
8348: ND *btog(NODE ti,ND **p,int nb,int mod);
8349: ND btog_one(NODE ti,ND *p,int nb,int mod);
8350: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8351: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8352:
8353: /* d:monomial */
8354: ND mdptond(DP d)
8355: {
8356: NM m;
8357: ND r;
8358:
8359: if ( OID(d) == 1 )
8360: r = ptond(CO,CO,(P)d);
8361: else {
8362: NEWNM(m);
8363: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8364: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8365: NEXT(m) = 0;
8366: MKND(NV(d),m,1,r);
8367: }
8368: return r;
8369: }
8370:
8371: ND nd_mul_nm(int mod,NM m0,ND p)
8372: {
8373: UINT *d0;
8374: int c0,c1,c;
8375: NM tm,mr,mr0;
8376: ND r;
8377:
8378: if ( !p ) return 0;
8379: d0 = DL(m0);
8380: c0 = CM(m0);
8381: mr0 = 0;
8382: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8383: NEXTNM(mr0,mr);
8384: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8385: ndl_add(d0,DL(tm),DL(mr));
8386: }
8387: NEXT(mr) = 0;
8388: MKND(NV(p),mr0,LEN(p),r);
8389: return r;
8390: }
8391:
8392: ND nd_mul_nm_lf(NM m0,ND p)
8393: {
8394: UINT *d0;
8395: Z c0,c1,c;
8396: NM tm,mr,mr0;
8397: ND r;
8398:
8399: if ( !p ) return 0;
8400: d0 = DL(m0);
8401: c0 = CZ(m0);
8402: mr0 = 0;
8403: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8404: NEXTNM(mr0,mr);
8405: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8406: ndl_add(d0,DL(tm),DL(mr));
8407: }
8408: NEXT(mr) = 0;
8409: MKND(NV(p),mr0,LEN(p),r);
8410: return r;
8411: }
8412:
8413: ND *btog(NODE ti,ND **p,int nb,int mod)
8414: {
8415: PGeoBucket *r;
8416: int i,ci;
8417: NODE t,s;
8418: ND m,tp;
8419: ND *pi,*rd;
8420: P c;
8421:
8422: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8423: for ( i = 0; i < nb; i++ )
8424: r[i] = create_pbucket();
8425: for ( t = ti; t; t = NEXT(t) ) {
8426: s = BDY((LIST)BDY(t));
8427: if ( ARG0(s) ) {
8428: m = mdptond((DP)ARG2(s));
1.6 noro 8429: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8430: if ( (ci = ((MQ)c)->cont) != 0 ) {
8431: HCM(m) = ci;
1.6 noro 8432: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8433: for ( i = 0; i < nb; i++ ) {
8434: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8435: add_pbucket(mod,r[i],tp);
8436: }
8437: }
8438: ci = 1;
8439: } else {
8440: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8441: ci = invm(ci,mod);
8442: }
8443: }
8444: rd = (ND *)MALLOC(nb*sizeof(ND));
8445: for ( i = 0; i < nb; i++ )
8446: rd[i] = normalize_pbucket(mod,r[i]);
8447: if ( ci != 1 )
8448: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8449: return rd;
8450: }
8451:
8452: /* YYY */
8453: ND *btog_lf(NODE ti,ND **p,int nb)
8454: {
8455: PGeoBucket *r;
8456: int i;
8457: NODE t,s;
8458: ND m,tp;
8459: ND *pi,*rd;
8460: LM lm;
8461: Z lf,c;
8462:
8463: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8464: for ( i = 0; i < nb; i++ )
8465: r[i] = create_pbucket();
8466: for ( t = ti; t; t = NEXT(t) ) {
8467: s = BDY((LIST)BDY(t));
8468: if ( ARG0(s) ) {
8469: m = mdptond((DP)ARG2(s));
1.6 noro 8470: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8471: if ( lm ) {
8472: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8473: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8474: for ( i = 0; i < nb; i++ ) {
8475: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8476: add_pbucket(-2,r[i],tp);
8477: }
8478: }
8479: c = ONE;
8480: } else {
8481: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8482: }
8483: }
8484: rd = (ND *)MALLOC(nb*sizeof(ND));
8485: for ( i = 0; i < nb; i++ )
8486: rd[i] = normalize_pbucket(-2,r[i]);
8487: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8488: return rd;
8489: }
8490:
8491: ND btog_one(NODE ti,ND *p,int nb,int mod)
8492: {
8493: PGeoBucket r;
8494: int i,ci,j;
8495: NODE t,s;
8496: ND m,tp;
8497: ND pi,rd;
8498: P c;
8499:
8500: r = create_pbucket();
8501: for ( t = ti; t; t = NEXT(t) ) {
8502: s = BDY((LIST)BDY(t));
8503: if ( ARG0(s) ) {
8504: m = mdptond((DP)ARG2(s));
1.6 noro 8505: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8506: if ( (ci = ((MQ)c)->cont) != 0 ) {
8507: HCM(m) = ci;
1.6 noro 8508: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8509: if ( !pi ) {
8510: pi = nd_load_mod(j);
8511: tp = nd_mul_nm(mod,BDY(m),pi);
8512: nd_free(pi);
8513: add_pbucket(mod,r,tp);
8514: } else {
8515: tp = nd_mul_nm(mod,BDY(m),pi);
8516: add_pbucket(mod,r,tp);
8517: }
8518: }
8519: ci = 1;
8520: } else {
8521: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8522: ci = invm(ci,mod);
8523: }
8524: }
8525: rd = normalize_pbucket(mod,r);
8526: free_pbucket(r);
8527: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8528: return rd;
8529: }
8530:
8531: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8532:
8533: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8534: {
8535: int i,j,n,m,nb,pi0,pi1,nvar;
8536: VL fv,tv,vv;
8537: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8538: ND **p;
8539: ND *c;
8540: ND u;
8541: P inv;
8542: MAT mat;
8543:
8544: if ( mod == -2 )
8545: return nd_btog_lf(f,v,ord,tlist,rp);
8546:
8547: parse_nd_option(current_option);
8548: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8549: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8550: switch ( ord->id ) {
8551: case 1:
8552: if ( ord->nv != nvar )
8553: error("nd_check : invalid order specification");
8554: break;
8555: default:
8556: break;
8557: }
8558: nd_init_ord(ord);
8559: #if 0
1.6 noro 8560: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8561: #else
8562: nd_bpe = 32;
8563: #endif
8564: nd_setup_parameters(nvar,0);
8565: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8566: intred = BDY((LIST)ARG3(BDY(tlist)));
8567: ind = BDY((LIST)ARG4(BDY(tlist)));
8568: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8569: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8570: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8571: if ( j > i ) i = j;
8572: }
8573: n = i+1;
8574: nb = length(BDY(f));
8575: p = (ND **)MALLOC(n*sizeof(ND *));
8576: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8577: pi = BDY((LIST)BDY(t));
1.6 noro 8578: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8579: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8580: ptomp(mod,(P)ARG2(pi),&inv);
8581: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8582: u = ptond(CO,vv,(P)ONE);
8583: HCM(u) = ((MQ)inv)->cont;
8584: c[pi1] = u;
8585: }
8586: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8587: printf("%d ",i); fflush(stdout);
8588: ti = BDY((LIST)BDY(t));
1.6 noro 8589: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8590: }
8591: for ( t = intred, 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: m = length(ind);
8597: MKMAT(mat,nb,m);
8598: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8599: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8600: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8601: return mat;
8602: }
8603:
8604: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8605: {
8606: int i,j,n,m,nb,pi0,pi1,nvar;
8607: VL fv,tv,vv;
8608: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8609: ND **p;
8610: ND *c;
8611: ND u;
8612: MAT mat;
8613: LM lm;
8614: Z lf,inv;
8615:
8616: parse_nd_option(current_option);
8617: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8618: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8619: switch ( ord->id ) {
8620: case 1:
8621: if ( ord->nv != nvar )
8622: error("nd_check : invalid order specification");
8623: break;
8624: default:
8625: break;
8626: }
8627: nd_init_ord(ord);
8628: #if 0
1.6 noro 8629: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8630: #else
8631: nd_bpe = 32;
8632: #endif
8633: nd_setup_parameters(nvar,0);
8634: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8635: intred = BDY((LIST)ARG3(BDY(tlist)));
8636: ind = BDY((LIST)ARG4(BDY(tlist)));
8637: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8638: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8639: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8640: if ( j > i ) i = j;
8641: }
8642: n = i+1;
8643: nb = length(BDY(f));
8644: p = (ND **)MALLOC(n*sizeof(ND *));
8645: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8646: pi = BDY((LIST)BDY(t));
1.6 noro 8647: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8648: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8649: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8650: u = ptond(CO,vv,(P)ONE);
8651: HCZ(u) = inv;
8652: c[pi1] = u;
8653: }
8654: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8655: printf("%d ",i); fflush(stdout);
8656: ti = BDY((LIST)BDY(t));
1.6 noro 8657: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8658: }
8659: for ( t = intred, 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: m = length(ind);
8665: MKMAT(mat,nb,m);
8666: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8667: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8668: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8669: return mat;
8670: }
8671:
8672: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8673: LIST tlist,int pos,MAT *rp)
8674: {
8675: int i,j,n,m,nb,pi0,pi1,nvar;
8676: VL fv,tv,vv;
8677: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8678: ND *p;
8679: ND *c;
8680: ND u;
8681: P inv;
8682: VECT vect;
8683:
8684: if ( mod == -2 )
8685: error("nd_btog_one : not implemented yet for a large finite field");
8686:
8687: parse_nd_option(current_option);
8688: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8689: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8690: switch ( ord->id ) {
8691: case 1:
8692: if ( ord->nv != nvar )
8693: error("nd_check : invalid order specification");
8694: break;
8695: default:
8696: break;
8697: }
8698: nd_init_ord(ord);
8699: #if 0
1.6 noro 8700: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8701: #else
8702: nd_bpe = 32;
8703: #endif
8704: nd_setup_parameters(nvar,0);
8705: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8706: intred = BDY((LIST)ARG3(BDY(tlist)));
8707: ind = BDY((LIST)ARG4(BDY(tlist)));
8708: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8709: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8710: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8711: if ( j > i ) i = j;
8712: }
8713: n = i+1;
8714: nb = length(BDY(f));
8715: p = (ND *)MALLOC(n*sizeof(ND *));
8716: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8717: pi = BDY((LIST)BDY(t));
1.6 noro 8718: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8719: if ( pi1 == pos ) {
8720: ptomp(mod,(P)ARG2(pi),&inv);
8721: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8722: u = ptond(CO,vv,(P)ONE);
8723: HCM(u) = ((MQ)inv)->cont;
8724: p[pi0] = u;
8725: }
8726: }
8727: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8728: printf("%d ",i); fflush(stdout);
8729: ti = BDY((LIST)BDY(t));
1.6 noro 8730: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8731: if ( Demand ) {
8732: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8733: }
8734: }
8735: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8736: printf("%d ",i); fflush(stdout);
8737: ti = BDY((LIST)BDY(t));
1.6 noro 8738: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8739: if ( Demand ) {
8740: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8741: }
8742: }
8743: m = length(ind);
8744: MKVECT(vect,m);
8745: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8746: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8747: if ( !u ) {
1.6 noro 8748: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8749: BDY(vect)[j] = ndtodp(mod,u);
8750: nd_free(u);
8751: } else
8752: BDY(vect)[j] = ndtodp(mod,u);
8753: }
8754: return vect;
8755: }
8756:
8757: void ndv_print_lf(NDV p)
8758: {
8759: NMV m;
8760: int i,len;
8761:
8762: if ( !p ) printf("0\n");
8763: else {
8764: len = LEN(p);
8765: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8766: printf("+");
8767: mpz_out_str(asir_out,10,BDY(CZ(m)));
8768: printf("*");
8769: ndl_print(DL(m));
8770: }
8771: printf("\n");
8772: }
8773: }
8774:
8775: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8776: {
8777: VL tv,fv,vv,vc,av;
8778: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8779: int m,nocheck,nvar,mindex,e,max;
8780: NDV c;
8781: NMV a;
8782: P p,zp;
8783: Q dmy;
8784: EPOS oepos;
8785: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8786: Alg alpha,dp;
8787: P poly;
8788: LIST f1,f2,zpl;
8789: Obj obj;
8790: NumberField nf;
8791: struct order_spec *ord1;
8792: struct oEGT eg_check,eg0,eg1;
8793: NODE tr,tl1,tl2,tl3,tl4;
8794: LIST l1,l2,l3,l4,l5;
8795: int *perm;
8796: int j,ret;
8797: NODE retn;
8798: Q jq,bpe;
8799:
8800: nd_module = 0;
8801: parse_nd_option(current_option);
8802: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8803: if ( nd_vc )
8804: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8805: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8806: switch ( ord->id ) {
8807: case 1:
8808: if ( ord->nv != nvar )
8809: error("nd_f4_lf_trace : invalid order specification");
8810: break;
8811: default:
8812: break;
8813: }
8814:
8815: nd_ntrans = nvar;
8816: nd_nalg = 0;
8817:
8818: nocheck = 0;
8819: mindex = 0;
8820:
8821: /* do not use on-demand load/save */
8822: nd_demand = 0;
8823: m = trace > 1 ? trace : get_lprime(mindex);
8824: nd_init_ord(ord);
8825: mrank = 0;
8826: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8827: for ( tv = vv; tv; tv = NEXT(tv) ) {
8828: if ( nd_module ) {
8829: s = BDY((LIST)BDY(t));
8830: trank = length(s);
8831: mrank = MAX(mrank,trank);
8832: for ( ; s; s = NEXT(s) ) {
8833: e = getdeg(tv->v,(P)BDY(s));
8834: max = MAX(e,max);
8835: }
8836: } else {
8837: e = getdeg(tv->v,(P)BDY(t));
8838: max = MAX(e,max);
8839: }
8840: }
8841: nd_setup_parameters(nvar,max);
8842: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8843: ishomo = 1;
8844: /* XXX */
8845: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8846: if ( nd_module ) {
8847: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8848: } else {
8849: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8850: }
8851: if ( ishomo )
8852: ishomo = ishomo && ndv_ishomo(c);
8853: if ( c ) {
8854: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
8855: ndv_mod(-2,c);
8856: NEXTNODE(in0,in); BDY(in) = (pointer)c;
8857: }
8858: }
8859: if ( in0 ) NEXT(in) = 0;
8860: if ( fd0 ) NEXT(fd) = 0;
8861: if ( !ishomo && homo ) {
8862: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
8863: c = (NDV)BDY(t); len = LEN(c);
8864: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
8865: wmax = MAX(TD(DL(a)),wmax);
8866: }
8867: homogenize_order(ord,nvar,&ord1);
8868: nd_init_ord(ord1);
8869: nd_setup_parameters(nvar+1,wmax);
8870: for ( t = fd0; t; t = NEXT(t) )
8871: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
8872: }
8873: if ( MaxDeg > 0 ) nocheck = 1;
8874: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
8875: if ( ret )
8876: cand = nd_f4_lf_trace_main(m,&perm);
8877: if ( !ret || !cand ) {
8878: *rp = 0; return;
8879: }
8880: if ( !ishomo && homo ) {
8881: /* dehomogenization */
8882: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
8883: nd_init_ord(ord);
8884: nd_setup_parameters(nvar,0);
8885: }
8886: cand = ndv_reducebase(cand,perm);
8887: cand = ndv_reduceall(-2,cand);
8888: cbpe = nd_bpe;
8889: get_eg(&eg0);
8890: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
8891: /* gbcheck : cand is a GB of Id(cand) ? */
8892: retn = nd_f4(-2,0,0);
8893: }
8894: if ( !retn ) {
8895: /* failure */
8896: *rp = 0; return;
8897: }
8898: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
8899: if ( DP_Print )
1.5 noro 8900: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 8901: /* dp->p */
8902: nd_bpe = cbpe;
8903: nd_setup_parameters(nd_nvar,0);
8904: for ( r = cand; r; r = NEXT(r) ) {
8905: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
8906: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
8907: }
8908: MKLIST(*rp,cand);
8909: }
8910:
8911: NODE nd_f4_lf_trace_main(int m,int **indp)
8912: {
8913: int i,nh,stat,index;
8914: NODE r,rm,g;
8915: ND_pairs d,l,l0,t;
8916: ND spol,red;
8917: NDV nf,redv,nfqv,nfv;
8918: NM s0,s;
8919: NODE rp0,srp0,nflist,nflist_lf;
8920: int nsp,nred,col,rank,len,k,j,a;
8921: UINT c;
8922: UINT **spmat;
8923: UINT *s0vect,*svect,*p,*v;
8924: int *colstat;
8925: IndArray *imat;
8926: int *rhead;
8927: int spcol,sprow;
8928: int sugar;
8929: PGeoBucket bucket;
8930: struct oEGT eg0,eg1,eg_f4;
8931:
8932: g = 0; d = 0;
8933: for ( i = 0; i < nd_psn; i++ ) {
8934: d = update_pairs(d,g,i,0);
8935: g = update_base(g,i);
8936: }
8937: while ( d ) {
8938: get_eg(&eg0);
8939: l = nd_minsugarp(d,&d);
8940: sugar = SG(l);
8941: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8942: bucket = create_pbucket();
8943: stat = nd_sp_f4(m,0,l,bucket);
8944: if ( !stat ) {
8945: for ( t = l; NEXT(t); t = NEXT(t) );
8946: NEXT(t) = d; d = l;
8947: d = nd_reconstruct(1,d);
8948: continue;
8949: }
8950: if ( bucket->m < 0 ) continue;
8951: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8952: if ( !col ) {
8953: for ( t = l; NEXT(t); t = NEXT(t) );
8954: NEXT(t) = d; d = l;
8955: d = nd_reconstruct(1,d);
8956: continue;
8957: }
8958: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8959: if ( DP_Print )
1.5 noro 8960: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 8961: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8962: if ( !l0 ) continue;
8963: l = l0;
8964:
8965: /* over LF */
8966: bucket = create_pbucket();
8967: stat = nd_sp_f4(-2,1,l,bucket);
8968: if ( !stat ) {
8969: for ( t = l; NEXT(t); t = NEXT(t) );
8970: NEXT(t) = d; d = l;
8971: d = nd_reconstruct(1,d);
8972: continue;
8973: }
8974: if ( bucket->m < 0 ) continue;
8975: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8976: if ( !col ) {
8977: for ( t = l; NEXT(t); t = NEXT(t) );
8978: NEXT(t) = d; d = l;
8979: d = nd_reconstruct(1,d);
8980: continue;
8981: }
8982: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
8983: /* adding new bases */
8984: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
8985: nfv = (NDV)BDY(rm);
8986: nfqv = (NDV)BDY(r);
8987: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
8988: ndv_removecont(m,nfv);
8989: ndv_removecont(-2,nfqv);
8990: nh = ndv_newps(-2,nfv,nfqv,1);
8991: d = update_pairs(d,g,nh,0);
8992: g = update_base(g,nh);
8993: }
8994: if ( r || rm ) return 0;
8995: }
8996: conv_ilist(nd_demand,1,g,indp);
8997: return g;
8998: }
8999:
1.7 noro 9000: #if SIZEOF_LONG==8
9001:
9002: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9003: {
9004: int j,k,len;
9005: UINT *p;
9006: UINT c;
9007: NDV r;
9008: NMV mr0,mr;
9009:
9010: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9011: if ( !len ) return 0;
9012: else {
9013: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9014: #if 0
9015: ndv_alloc += nmv_adv*len;
9016: #endif
9017: mr = mr0;
9018: p = s0vect;
9019: for ( j = k = 0; j < col; j++, p += nd_wpd )
9020: if ( !rhead[j] ) {
9021: if ( (c = (UINT)vect[k++]) != 0 ) {
9022: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9023: }
9024: }
9025: MKNDV(nd_nvar,mr0,len,r);
9026: return r;
9027: }
9028: }
9029:
9030: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9031: {
9032: NM m;
1.11 ! noro 9033: UINT *t,*s,*u;
! 9034: int i,st,ed,md,prev,c;
1.7 noro 9035:
9036: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 ! noro 9037: prev = 0;
! 9038: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
! 9039: t = DL(m);
! 9040: st = prev;
! 9041: ed = n;
! 9042: while ( ed > st ) {
! 9043: md = (st+ed)/2;
! 9044: u = s0+md*nd_wpd;
! 9045: c = DL_COMPARE(u,t);
! 9046: if ( c == 0 ) break;
! 9047: else if ( c > 0 ) st = md;
! 9048: else ed = md;
! 9049: }
! 9050: r[md] = (mp_limb_t)CM(m);
! 9051: prev = md;
1.7 noro 9052: }
9053: for ( i = 0; !r[i]; i++ );
9054: return i;
9055: }
9056:
9057: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9058:
9059: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9060: {
9061: int i,j,k,len,pos,prev;
9062: mp_limb_t a,c,c1,c2;
9063: IndArray ivect;
9064: unsigned char *ivc;
9065: unsigned short *ivs;
9066: unsigned int *ivi;
9067: NDV redv;
9068: NMV mr;
9069: NODE rp;
9070: int maxrs;
9071:
9072: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9073: maxrs = 0;
9074: for ( i = 0; i < nred; i++ ) {
9075: ivect = imat[i];
9076: k = ivect->head;
9077: a = svect[k]; c = cvect[k];
9078: MOD128(a,c,m);
9079: svect[k] = a; cvect[k] = 0;
9080: if ( (c = svect[k]) != 0 ) {
1.11 ! noro 9081: Nf4_red++;
1.7 noro 9082: maxrs = MAX(maxrs,rp0[i]->sugar);
9083: c = m-c; redv = nd_ps[rp0[i]->index];
9084: len = LEN(redv); mr = BDY(redv);
9085: svect[k] = 0; prev = k;
9086: switch ( ivect->width ) {
9087: case 1:
9088: ivc = ivect->index.c;
9089: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9090: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
9091: if ( c1 ) {
9092: c2 = svect[pos]+c1*c;
9093: if ( c2 < svect[pos] ) cvect[pos]++;
9094: svect[pos] = c2;
9095: }
9096: }
9097: break;
9098: case 2:
9099: ivs = ivect->index.s;
9100: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9101: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
9102: if ( c1 ) {
9103: c2 = svect[pos]+c1*c;
9104: if ( c2 < svect[pos] ) cvect[pos]++;
9105: svect[pos] = c2;
9106: }
9107: }
9108: break;
9109: case 4:
9110: ivi = ivect->index.i;
9111: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9112: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
9113: if ( c1 ) {
9114: c2 = svect[pos]+c1*c;
9115: if ( c2 < svect[pos] ) cvect[pos]++;
9116: svect[pos] = c2;
9117: }
9118: }
9119: break;
9120: }
9121: }
9122: }
9123: for ( i = 0; i < col; i++ ) {
9124: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9125: }
9126: return maxrs;
9127: }
9128:
9129: /* for Fp, 2^15=<p<2^29 */
9130:
9131: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9132: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9133: {
9134: int spcol,sprow,a;
9135: int i,j,k,l,rank;
9136: NODE r0,r;
9137: ND_pairs sp;
9138: ND spol;
9139: mp_limb_t **spmat;
9140: mp_limb_t *svect,*cvect;
9141: mp_limb_t *v;
9142: int *colstat;
9143: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9144: int maxrs;
9145: int *spsugar;
9146: ND_pairs *spactive;
9147:
9148: spcol = col-nred;
9149: get_eg(&eg0);
9150: /* elimination (1st step) */
9151: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9152: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9153: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9154: spsugar = (int *)MALLOC(nsp*sizeof(int));
9155: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9156: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9157: nd_sp(m,0,sp,&spol);
9158: if ( !spol ) continue;
9159: nd_to_vect64(m,s0vect,col,spol,svect);
9160: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9161: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9162: if ( i < col ) {
9163: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9164: for ( j = k = 0; j < col; j++ )
9165: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9166: spsugar[sprow] = MAX(maxrs,SG(spol));
9167: if ( nz )
9168: spactive[sprow] = sp;
9169: sprow++;
9170: }
9171: nd_free(spol);
9172: }
9173: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9174: if ( DP_Print ) {
9175: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9176: fflush(asir_out);
9177: }
9178: /* free index arrays */
9179: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9180:
9181: /* elimination (2nd step) */
9182: colstat = (int *)MALLOC(spcol*sizeof(int));
9183: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9184: r0 = 0;
9185: for ( i = 0; i < rank; i++ ) {
9186: NEXTNODE(r0,r); BDY(r) =
9187: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9188: SG((NDV)BDY(r)) = spsugar[i];
9189: GCFREE(spmat[i]);
9190: }
9191: if ( r0 ) NEXT(r) = 0;
9192:
9193: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9194: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9195: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9196: if ( DP_Print ) {
9197: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9198: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9199: nsp,nred,sprow,spcol,rank);
9200: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9201: }
9202: if ( nz ) {
9203: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9204: if ( rank > 0 ) {
9205: NEXT(spactive[rank-1]) = 0;
9206: *nz = spactive[0];
9207: } else
9208: *nz = 0;
9209: }
9210: return r0;
9211: }
9212:
9213: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9214: {
9215: int i,j,k,l,rank,s;
9216: mp_limb_t inv;
9217: mp_limb_t a;
9218: UINT c;
9219: mp_limb_t *t,*pivot,*pk;
9220: UINT *ck;
9221: UINT **cmat;
9222: UINT *ct;
9223: ND_pairs pair;
9224:
9225: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9226: for ( i = 0; i < row; i++ ) {
9227: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9228: bzero(cmat[i],col*sizeof(UINT));
9229: }
9230:
9231: for ( rank = 0, j = 0; j < col; j++ ) {
9232: for ( i = rank; i < row; i++ ) {
9233: a = mat[i][j]; c = cmat[i][j];
9234: MOD128(a,c,md);
9235: mat[i][j] = a; cmat[i][j] = 0;
9236: }
9237: for ( i = rank; i < row; i++ )
9238: if ( mat[i][j] )
9239: break;
9240: if ( i == row ) {
9241: colstat[j] = 0;
9242: continue;
9243: } else
9244: colstat[j] = 1;
9245: if ( i != rank ) {
9246: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9247: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9248: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9249: if ( spactive ) {
9250: pair = spactive[i]; spactive[i] = spactive[rank];
9251: spactive[rank] = pair;
9252: }
9253: }
9254: /* column j is normalized */
9255: s = sugar[rank];
9256: inv = invm((UINT)mat[rank][j],md);
9257: /* normalize pivot row */
9258: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9259: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9260: }
9261: for ( i = rank+1; i < row; i++ ) {
9262: if ( (a = mat[i][j]) != 0 ) {
9263: sugar[i] = MAX(sugar[i],s);
9264: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 ! noro 9265: Nf4_red++;
1.7 noro 9266: }
9267: }
9268: rank++;
9269: }
9270: for ( j = col-1, l = rank-1; j >= 0; j-- )
9271: if ( colstat[j] ) {
9272: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9273: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9274: }
9275: s = sugar[l];
9276: for ( i = 0; i < l; i++ ) {
9277: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9278: if ( a ) {
9279: sugar[i] = MAX(sugar[i],s);
9280: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 ! noro 9281: Nf4_red++;
1.7 noro 9282: }
9283: }
9284: l--;
9285: }
9286: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9287: GCFREE(cmat);
9288: return rank;
9289: }
9290: #endif
9291:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>