Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.1 2018/09/19 05:45:07 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
5: struct oEGT eg_search;
6:
7: int diag_period = 6;
8: int weight_check = 1;
9: int (*ndl_compare_function)(UINT *a1,UINT *a2);
10: int nd_dcomp;
11: int nd_rref2;
12: NM _nm_free_list;
13: ND _nd_free_list;
14: ND_pairs _ndp_free_list;
15: NODE nd_hcf;
16:
17: Obj nd_top_weight;
18:
19: static NODE nd_subst;
20: static VL nd_vc;
21: static int nd_ntrans;
22: static int nd_nalg;
23: #if 0
24: static int ndv_alloc;
25: #endif
26: #if 1
27: static int nd_f4_nsp=0x7fffffff;
28: #else
29: static int nd_f4_nsp=50;
30: #endif
31: static double nd_scale=2;
32: static UINT **nd_bound;
33: static struct order_spec *nd_ord;
34: static EPOS nd_epos;
35: static BlockMask nd_blockmask;
36: static int nd_nvar;
37: static int nd_isrlex;
38: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
39: static UINT nd_mask[32];
40: static UINT nd_mask0,nd_mask1;
41:
42: static NDV *nd_ps;
43: static NDV *nd_ps_trace;
44: static NDV *nd_ps_sym;
45: static NDV *nd_ps_trace_sym;
46: static RHist *nd_psh;
47: static int nd_psn,nd_pslen;
48: static RHist *nd_red;
49: static int *nd_work_vector;
50: static int **nd_matrix;
51: static int nd_matrix_len;
52: static struct weight_or_block *nd_worb;
53: static int nd_worb_len;
54: static int nd_found,nd_create,nd_notfirst;
55: static int nmv_adv;
56: static int nd_demand;
57: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
58: static int nd_module_rank,nd_poly_weight_len;
59: static int *nd_poly_weight,*nd_module_weight;
60: static NODE nd_tracelist;
61: static NODE nd_alltracelist;
62: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
63: static int *nd_gbblock;
64: static NODE nd_nzlist,nd_check_splist;
65: static int nd_splist;
66: static int *nd_sugarweight;
67: static int nd_f4red,nd_rank0,nd_last_nonzero;
68:
69: NumberField get_numberfield();
70: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
71: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
72: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
73: int nd_monic(int m,ND *p);
74: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
75: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
76: NDV pltondv(VL vl,VL dvl,LIST p);
77: void pltozpl(LIST l,Q *cont,LIST *pp);
78: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
79: void nmtodp(int mod,NM m,DP *r);
80: NODE reverse_node(NODE n);
81: P ndc_div(int mod,union oNDC a,union oNDC b);
82: P ndctop(int mod,union oNDC c);
83: void finalize_tracelist(int i,P cont);
84: void conv_ilist(int demand,int trace,NODE g,int **indp);
85: void parse_nd_option(NODE opt);
86: void dltondl(int n,DL dl,UINT *r);
87: DP ndvtodp(int mod,NDV p);
88: DP ndtodp(int mod,ND p);
89:
90: void Pdp_set_weight(NODE,VECT *);
91: void Pox_cmo_rpc(NODE,Obj *);
92:
93: ND nd_add_lf(ND p1,ND p2);
94: void nd_mul_c_lf(ND p,Z mul);
95: void ndv_mul_c_lf(NDV p,Z mul);
96: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
97: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
98: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
99: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
100: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
101: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
102: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
103: NODE nd_f4_lf_trace_main(int m,int **indp);
104: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
105:
106: extern int lf_lazy;
107: extern Z current_mod_lf;
108:
109: extern int Denominator,DP_Multiple,MaxDeg;
110:
111: #define BLEN (8*sizeof(unsigned long))
112:
113: typedef struct matrix {
114: int row,col;
115: unsigned long **a;
116: } *matrix;
117:
118:
119: void nd_free_private_storage()
120: {
121: _nm_free_list = 0;
122: _ndp_free_list = 0;
123: #if 0
124: GC_gcollect();
125: #endif
126: }
127:
128: void _NM_alloc()
129: {
130: NM p;
131: int i;
132:
133: for ( i = 0; i < 1024; i++ ) {
134: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
135: p->next = _nm_free_list; _nm_free_list = p;
136: }
137: }
138:
139: matrix alloc_matrix(int row,int col)
140: {
141: unsigned long **a;
142: int i,len,blen;
143: matrix mat;
144:
145: mat = (matrix)MALLOC(sizeof(struct matrix));
146: mat->row = row;
147: mat->col = col;
148: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
149: return mat;
150: }
151:
152:
153: void _ND_alloc()
154: {
155: ND p;
156: int i;
157:
158: for ( i = 0; i < 1024; i++ ) {
159: p = (ND)MALLOC(sizeof(struct oND));
160: p->body = (NM)_nd_free_list; _nd_free_list = p;
161: }
162: }
163:
164: void _NDP_alloc()
165: {
166: ND_pairs p;
167: int i;
168:
169: for ( i = 0; i < 1024; i++ ) {
170: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
171: +(nd_wpd-1)*sizeof(UINT));
172: p->next = _ndp_free_list; _ndp_free_list = p;
173: }
174: }
175:
176: INLINE int nd_length(ND p)
177: {
178: NM m;
179: int i;
180:
181: if ( !p )
182: return 0;
183: else {
184: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
185: return i;
186: }
187: }
188:
189: extern int dp_negative_weight;
190:
191: INLINE int ndl_reducible(UINT *d1,UINT *d2)
192: {
193: UINT u1,u2;
194: int i,j;
195:
196: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
197:
198: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
199: #if USE_UNROLL
200: switch ( nd_bpe ) {
201: case 3:
202: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
203: u1 = d1[i]; u2 = d2[i];
204: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
205: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
206: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
207: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
208: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
209: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
210: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
211: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
212: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
213: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
214: }
215: return 1;
216: break;
217: case 4:
218: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
219: u1 = d1[i]; u2 = d2[i];
220: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
221: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
222: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
223: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
224: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
225: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
226: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
227: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
228: }
229: return 1;
230: break;
231: case 6:
232: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
233: u1 = d1[i]; u2 = d2[i];
234: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
235: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
236: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
237: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
238: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
239: }
240: return 1;
241: break;
242: case 8:
243: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
244: u1 = d1[i]; u2 = d2[i];
245: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
246: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
247: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
248: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
249: }
250: return 1;
251: break;
252: case 16:
253: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
254: u1 = d1[i]; u2 = d2[i];
255: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
256: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
257: }
258: return 1;
259: break;
260: case 32:
261: for ( i = nd_exporigin; i < nd_wpd; i++ )
262: if ( d1[i] < d2[i] ) return 0;
263: return 1;
264: break;
265: default:
266: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
267: u1 = d1[i]; u2 = d2[i];
268: for ( j = 0; j < nd_epw; j++ )
269: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
270: }
271: return 1;
272: }
273: #else
274: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
275: u1 = d1[i]; u2 = d2[i];
276: for ( j = 0; j < nd_epw; j++ )
277: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
278: }
279: return 1;
280: #endif
281: }
282:
283: /*
284: * If the current order is a block order,
285: * then the last block is length 1 and contains
286: * the homo variable. Otherwise, the original
287: * order is either 0 or 2.
288: */
289:
290: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
291: {
292: int w,i,e,n,omask0;
293:
294: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
295: n = nd_nvar-1;
296: ndl_zero(r);
297: for ( i = 0; i < n; i++ ) {
298: e = GET_EXP_OLD(d,i);
299: PUT_EXP(r,i,e);
300: }
301: w = TD(d);
302: PUT_EXP(r,nd_nvar-1,weight-w);
303: if ( nd_module ) MPOS(r) = d[ompos];
304: TD(r) = weight;
305: if ( nd_blockmask ) ndl_weight_mask(r);
306: }
307:
308: void ndl_dehomogenize(UINT *d)
309: {
310: UINT mask;
311: UINT h;
312: int i,bits;
313:
314: if ( nd_blockmask ) {
315: h = GET_EXP(d,nd_nvar-1);
316: XOR_EXP(d,nd_nvar-1,h);
317: TD(d) -= h;
318: ndl_weight_mask(d);
319: } else {
320: if ( nd_isrlex ) {
321: if ( nd_bpe == 32 ) {
322: h = d[nd_exporigin];
323: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
324: d[i-1] = d[i];
325: d[i-1] = 0;
326: TD(d) -= h;
327: } else {
328: bits = nd_epw*nd_bpe;
329: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
330: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
331: for ( i = nd_exporigin; i < nd_wpd; i++ )
332: d[i] = ((d[i]<<nd_bpe)&mask)
333: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
334: TD(d) -= h;
335: }
336: } else {
337: h = GET_EXP(d,nd_nvar-1);
338: XOR_EXP(d,nd_nvar-1,h);
339: TD(d) -= h;
340: }
341: }
342: }
343:
344: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
345: {
346: UINT t1,t2,u,u1,u2;
347: int i,j,l;
348:
349: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
350: error("ndl_lcm : inconsistent monomials");
351: #if USE_UNROLL
352: switch ( nd_bpe ) {
353: case 3:
354: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
355: u1 = d1[i]; u2 = d2[i];
356: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
357: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
358: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
359: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
360: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
361: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
362: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
363: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
364: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
365: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
366: d[i] = u;
367: }
368: break;
369: case 4:
370: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
371: u1 = d1[i]; u2 = d2[i];
372: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
373: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
374: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
375: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
376: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
377: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
378: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
379: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
380: d[i] = u;
381: }
382: break;
383: case 6:
384: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
385: u1 = d1[i]; u2 = d2[i];
386: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
387: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
388: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
389: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
390: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
391: d[i] = u;
392: }
393: break;
394: case 8:
395: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
396: u1 = d1[i]; u2 = d2[i];
397: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
398: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
400: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
401: d[i] = u;
402: }
403: break;
404: case 16:
405: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
406: u1 = d1[i]; u2 = d2[i];
407: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
408: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
409: d[i] = u;
410: }
411: break;
412: case 32:
413: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
414: u1 = d1[i]; u2 = d2[i];
415: d[i] = u1>u2?u1:u2;
416: }
417: break;
418: default:
419: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
420: u1 = d1[i]; u2 = d2[i];
421: for ( j = 0, u = 0; j < nd_epw; j++ ) {
422: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
423: }
424: d[i] = u;
425: }
426: break;
427: }
428: #else
429: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
430: u1 = d1[i]; u2 = d2[i];
431: for ( j = 0, u = 0; j < nd_epw; j++ ) {
432: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
433: }
434: d[i] = u;
435: }
436: #endif
437: if ( nd_module ) MPOS(d) = MPOS(d1);
438: TD(d) = ndl_weight(d);
439: if ( nd_blockmask ) ndl_weight_mask(d);
440: }
441:
442: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
443: {
444: UINT t1,t2,u,u1,u2;
445: int i,j,l;
446:
447: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
448: u1 = d1[i]; u2 = d2[i];
449: for ( j = 0, u = 0; j < nd_epw; j++ ) {
450: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
451: }
452: d[i] = u;
453: }
454: }
455:
456: int ndl_weight(UINT *d)
457: {
458: UINT t,u;
459: int i,j;
460:
461: if ( current_dl_weight_vector )
462: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
463: u = GET_EXP(d,i);
464: t += MUL_WEIGHT(u,i);
465: }
466: else
467: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
468: u = d[i];
469: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
470: t += (u&nd_mask0);
471: }
472: if ( nd_module && current_module_weight_vector && MPOS(d) )
473: t += current_module_weight_vector[MPOS(d)];
474: return t;
475: }
476:
477: /* for sugarweight */
478:
479: int ndl_weight2(UINT *d)
480: {
481: int t,u;
482: int i,j;
483:
484: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
485: u = GET_EXP(d,i);
486: t += nd_sugarweight[i]*u;
487: }
488: if ( nd_module && current_module_weight_vector && MPOS(d) )
489: t += current_module_weight_vector[MPOS(d)];
490: return t;
491: }
492:
493: void ndl_weight_mask(UINT *d)
494: {
495: UINT t,u;
496: UINT *mask;
497: int i,j,k,l;
498:
499: l = nd_blockmask->n;
500: for ( k = 0; k < l; k++ ) {
501: mask = nd_blockmask->mask[k];
502: if ( current_dl_weight_vector )
503: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
504: u = GET_EXP_MASK(d,i,mask);
505: t += MUL_WEIGHT(u,i);
506: }
507: else
508: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
509: u = d[i]&mask[i];
510: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
511: t += (u&nd_mask0);
512: }
513: d[k+1] = t;
514: }
515: }
516:
517: int ndl_lex_compare(UINT *d1,UINT *d2)
518: {
519: int i;
520:
521: d1 += nd_exporigin;
522: d2 += nd_exporigin;
523: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
524: if ( *d1 > *d2 )
525: return nd_isrlex ? -1 : 1;
526: else if ( *d1 < *d2 )
527: return nd_isrlex ? 1 : -1;
528: return 0;
529: }
530:
531: int ndl_block_compare(UINT *d1,UINT *d2)
532: {
533: int i,l,j,ord_o,ord_l;
534: struct order_pair *op;
535: UINT t1,t2,m;
536: UINT *mask;
537:
538: l = nd_blockmask->n;
539: op = nd_blockmask->order_pair;
540: for ( j = 0; j < l; j++ ) {
541: mask = nd_blockmask->mask[j];
542: ord_o = op[j].order;
543: if ( ord_o < 2 ) {
544: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
545: else if ( t1 < t2 ) return -1;
546: }
547: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
548: m = mask[i];
549: t1 = d1[i]&m;
550: t2 = d2[i]&m;
551: if ( t1 > t2 )
552: return !ord_o ? -1 : 1;
553: else if ( t1 < t2 )
554: return !ord_o ? 1 : -1;
555: }
556: }
557: return 0;
558: }
559:
560: int ndl_matrix_compare(UINT *d1,UINT *d2)
561: {
562: int i,j,s,row;
563: int *v;
564: Z **mat;
565: Z *w;
566: Z t1;
567: Z t,t2;
568:
569: for ( j = 0; j < nd_nvar; j++ )
570: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
571: if ( nd_top_weight ) {
572: if ( OID(nd_top_weight) == O_VECT ) {
573: mat = (Z **)&BDY((VECT)nd_top_weight);
574: row = 1;
575: } else {
576: mat = (Z **)BDY((MAT)nd_top_weight);
577: row = ((MAT)nd_top_weight)->row;
578: }
579: for ( i = 0; i < row; i++ ) {
580: w = mat[i];
581: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
582: STOQ(nd_work_vector[j],t1);
583: mulz(w[j],t1,&t2);
584: addz(t,t2,&t1);
585: t = t1;
586: }
587: if ( t ) {
588: s = sgnz(t);
589: if ( s > 0 ) return 1;
590: else if ( s < 0 ) return -1;
591: }
592: }
593: }
594: for ( i = 0; i < nd_matrix_len; i++ ) {
595: v = nd_matrix[i];
596: for ( j = 0, s = 0; j < nd_nvar; j++ )
597: s += v[j]*nd_work_vector[j];
598: if ( s > 0 ) return 1;
599: else if ( s < 0 ) return -1;
600: }
601: if ( !ndl_equal(d1,d2) )
602: error("afo");
603: return 0;
604: }
605:
606: int ndl_composite_compare(UINT *d1,UINT *d2)
607: {
608: int i,j,s,start,end,len,o;
609: int *v;
610: struct sparse_weight *sw;
611:
612: for ( j = 0; j < nd_nvar; j++ )
613: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
614: for ( i = 0; i < nd_worb_len; i++ ) {
615: len = nd_worb[i].length;
616: switch ( nd_worb[i].type ) {
617: case IS_DENSE_WEIGHT:
618: v = nd_worb[i].body.dense_weight;
619: for ( j = 0, s = 0; j < len; j++ )
620: s += v[j]*nd_work_vector[j];
621: if ( s > 0 ) return 1;
622: else if ( s < 0 ) return -1;
623: break;
624: case IS_SPARSE_WEIGHT:
625: sw = nd_worb[i].body.sparse_weight;
626: for ( j = 0, s = 0; j < len; j++ )
627: s += sw[j].value*nd_work_vector[sw[j].pos];
628: if ( s > 0 ) return 1;
629: else if ( s < 0 ) return -1;
630: break;
631: case IS_BLOCK:
632: o = nd_worb[i].body.block.order;
633: start = nd_worb[i].body.block.start;
634: switch ( o ) {
635: case 0:
636: end = start+len;
637: for ( j = start, s = 0; j < end; j++ )
638: s += MUL_WEIGHT(nd_work_vector[j],j);
639: if ( s > 0 ) return 1;
640: else if ( s < 0 ) return -1;
641: for ( j = end-1; j >= start; j-- )
642: if ( nd_work_vector[j] < 0 ) return 1;
643: else if ( nd_work_vector[j] > 0 ) return -1;
644: break;
645: case 1:
646: end = start+len;
647: for ( j = start, s = 0; j < end; j++ )
648: s += MUL_WEIGHT(nd_work_vector[j],j);
649: if ( s > 0 ) return 1;
650: else if ( s < 0 ) return -1;
651: for ( j = start; j < end; j++ )
652: if ( nd_work_vector[j] > 0 ) return 1;
653: else if ( nd_work_vector[j] < 0 ) return -1;
654: break;
655: case 2:
656: end = start+len;
657: for ( j = start; j < end; j++ )
658: if ( nd_work_vector[j] > 0 ) return 1;
659: else if ( nd_work_vector[j] < 0 ) return -1;
660: break;
661: }
662: break;
663: }
664: }
665: return 0;
666: }
667:
668: /* TDH -> WW -> TD-> RL */
669:
670: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
671: {
672: int i,m,e1,e2;
673:
674: if ( TD(d1) > TD(d2) ) return 1;
675: else if ( TD(d1) < TD(d2) ) return -1;
676: m = nd_nvar>>1;
677: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
678: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
679: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
680: }
681: if ( e1 > e2 ) return 1;
682: else if ( e1 < e2 ) return -1;
683: return ndl_lex_compare(d1,d2);
684: }
685:
686: int ndl_module_weight_compare(UINT *d1,UINT *d2)
687: {
688: int s,j;
689:
690: if ( nd_nvar != nd_poly_weight_len )
691: error("invalid module weight : the length of polynomial weight != the number of variables");
692: s = 0;
693: for ( j = 0; j < nd_nvar; j++ )
694: s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
695: if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
696: s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
697: }
698: if ( s > 0 ) return 1;
699: else if ( s < 0 ) return -1;
700: else return 0;
701: }
702:
703: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
704: {
705: int i,c;
706:
707: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
708: if ( nd_ispot ) {
709: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
710: if ( TD(d1) > TD(d2) ) return 1;
711: else if ( TD(d1) < TD(d2) ) return -1;
712: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
713: if ( MPOS(d1) < MPOS(d2) ) return 1;
714: else if ( MPOS(d1) > MPOS(d2) ) return -1;
715: return 0;
716: }
717: if ( MPOS(d1) < MPOS(d2) ) return 1;
718: else if ( MPOS(d1) > MPOS(d2) ) return -1;
719: }
720: if ( TD(d1) > TD(d2) ) return 1;
721: else if ( TD(d1) < TD(d2) ) return -1;
722: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
723: if ( !nd_ispot ) {
724: if ( MPOS(d1) < MPOS(d2) ) return 1;
725: else if ( MPOS(d1) > MPOS(d2) ) return -1;
726: }
727: return 0;
728: }
729:
730: int ndl_module_glex_compare(UINT *d1,UINT *d2)
731: {
732: int i,c;
733:
734: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
735: if ( nd_ispot ) {
736: if ( MPOS(d1) < MPOS(d2) ) return 1;
737: else if ( MPOS(d1) > MPOS(d2) ) return -1;
738: }
739: if ( TD(d1) > TD(d2) ) return 1;
740: else if ( TD(d1) < TD(d2) ) return -1;
741: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
742: if ( !nd_ispot ) {
743: if ( MPOS(d1) < MPOS(d2) ) return 1;
744: else if ( MPOS(d1) > MPOS(d2) ) return -1;
745: }
746: return 0;
747: }
748:
749: int ndl_module_lex_compare(UINT *d1,UINT *d2)
750: {
751: int i,c;
752:
753: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
754: if ( nd_ispot ) {
755: if ( MPOS(d1) < MPOS(d2) ) return 1;
756: else if ( MPOS(d1) > MPOS(d2) ) return -1;
757: }
758: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
759: if ( !nd_ispot ) {
760: if ( MPOS(d1) < MPOS(d2) ) return 1;
761: else if ( MPOS(d1) > MPOS(d2) ) return -1;
762: }
763: return 0;
764: }
765:
766: int ndl_module_block_compare(UINT *d1,UINT *d2)
767: {
768: int i,c;
769:
770: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
771: if ( nd_ispot ) {
772: if ( MPOS(d1) < MPOS(d2) ) return 1;
773: else if ( MPOS(d1) > MPOS(d2) ) return -1;
774: }
775: if ( (c = ndl_block_compare(d1,d2)) != 0 ) return c;
776: if ( !nd_ispot ) {
777: if ( MPOS(d1) < MPOS(d2) ) return 1;
778: else if ( MPOS(d1) > MPOS(d2) ) return -1;
779: }
780: return 0;
781: }
782:
783: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
784: {
785: int i,c;
786:
787: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
788: if ( nd_ispot ) {
789: if ( MPOS(d1) < MPOS(d2) ) return 1;
790: else if ( MPOS(d1) > MPOS(d2) ) return -1;
791: }
792: if ( (c = ndl_matrix_compare(d1,d2)) != 0 ) return c;
793: if ( !nd_ispot ) {
794: if ( MPOS(d1) < MPOS(d2) ) return 1;
795: else if ( MPOS(d1) > MPOS(d2) ) return -1;
796: }
797: return 0;
798: }
799:
800: int ndl_module_composite_compare(UINT *d1,UINT *d2)
801: {
802: int i,c;
803:
804: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
805: if ( nd_ispot ) {
806: if ( MPOS(d1) > MPOS(d2) ) return 1;
807: else if ( MPOS(d1) < MPOS(d2) ) return -1;
808: }
809: if ( (c = ndl_composite_compare(d1,d2)) != 0 ) return c;
810: if ( !nd_ispot ) {
811: if ( MPOS(d1) > MPOS(d2) ) return 1;
812: else if ( MPOS(d1) < MPOS(d2) ) return -1;
813: }
814: return 0;
815: }
816:
817: INLINE int ndl_equal(UINT *d1,UINT *d2)
818: {
819: int i;
820:
821: switch ( nd_wpd ) {
822: case 2:
823: if ( TD(d2) != TD(d1) ) return 0;
824: if ( d2[1] != d1[1] ) return 0;
825: return 1;
826: break;
827: case 3:
828: if ( TD(d2) != TD(d1) ) return 0;
829: if ( d2[1] != d1[1] ) return 0;
830: if ( d2[2] != d1[2] ) return 0;
831: return 1;
832: break;
833: default:
834: for ( i = 0; i < nd_wpd; i++ )
835: if ( *d1++ != *d2++ ) return 0;
836: return 1;
837: break;
838: }
839: }
840:
841: INLINE void ndl_copy(UINT *d1,UINT *d2)
842: {
843: int i;
844:
845: switch ( nd_wpd ) {
846: case 2:
847: TD(d2) = TD(d1);
848: d2[1] = d1[1];
849: break;
850: case 3:
851: TD(d2) = TD(d1);
852: d2[1] = d1[1];
853: d2[2] = d1[2];
854: break;
855: default:
856: for ( i = 0; i < nd_wpd; i++ )
857: d2[i] = d1[i];
858: break;
859: }
860: }
861:
862: INLINE void ndl_zero(UINT *d)
863: {
864: int i;
865: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
866: }
867:
868: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
869: {
870: int i;
871:
872: if ( nd_module ) {
873: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
874: error("ndl_add : invalid operation");
875: }
876: #if 1
877: switch ( nd_wpd ) {
878: case 2:
879: TD(d) = TD(d1)+TD(d2);
880: d[1] = d1[1]+d2[1];
881: break;
882: case 3:
883: TD(d) = TD(d1)+TD(d2);
884: d[1] = d1[1]+d2[1];
885: d[2] = d1[2]+d2[2];
886: break;
887: default:
888: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
889: break;
890: }
891: #else
892: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
893: #endif
894: }
895:
896: /* d1 += d2 */
897: INLINE void ndl_addto(UINT *d1,UINT *d2)
898: {
899: int i;
900:
901: if ( nd_module ) {
902: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
903: error("ndl_addto : invalid operation");
904: }
905: #if 1
906: switch ( nd_wpd ) {
907: case 2:
908: TD(d1) += TD(d2);
909: d1[1] += d2[1];
910: break;
911: case 3:
912: TD(d1) += TD(d2);
913: d1[1] += d2[1];
914: d1[2] += d2[2];
915: break;
916: default:
917: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
918: break;
919: }
920: #else
921: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
922: #endif
923: }
924:
925: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
926: {
927: int i;
928:
929: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
930: }
931:
932: int ndl_disjoint(UINT *d1,UINT *d2)
933: {
934: UINT t1,t2,u,u1,u2;
935: int i,j;
936:
937: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
938: #if USE_UNROLL
939: switch ( nd_bpe ) {
940: case 3:
941: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
942: u1 = d1[i]; u2 = d2[i];
943: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
944: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
945: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
946: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
947: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
948: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
949: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
950: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
951: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
952: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
953: }
954: return 1;
955: break;
956: case 4:
957: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
958: u1 = d1[i]; u2 = d2[i];
959: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
960: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
961: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
962: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
963: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
964: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
965: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
966: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
967: }
968: return 1;
969: break;
970: case 6:
971: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
972: u1 = d1[i]; u2 = d2[i];
973: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
974: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
975: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
976: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
977: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
978: }
979: return 1;
980: break;
981: case 8:
982: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
983: u1 = d1[i]; u2 = d2[i];
984: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
985: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
986: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
987: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
988: }
989: return 1;
990: break;
991: case 16:
992: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
993: u1 = d1[i]; u2 = d2[i];
994: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
995: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
996: }
997: return 1;
998: break;
999: case 32:
1000: for ( i = nd_exporigin; i < nd_wpd; i++ )
1001: if ( d1[i] && d2[i] ) return 0;
1002: return 1;
1003: break;
1004: default:
1005: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1006: u1 = d1[i]; u2 = d2[i];
1007: for ( j = 0; j < nd_epw; j++ ) {
1008: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1009: u1 >>= nd_bpe; u2 >>= nd_bpe;
1010: }
1011: }
1012: return 1;
1013: break;
1014: }
1015: #else
1016: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1017: u1 = d1[i]; u2 = d2[i];
1018: for ( j = 0; j < nd_epw; j++ ) {
1019: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1020: u1 >>= nd_bpe; u2 >>= nd_bpe;
1021: }
1022: }
1023: return 1;
1024: #endif
1025: }
1026:
1027: int ndl_check_bound(UINT *d1,UINT *d2)
1028: {
1029: UINT u2;
1030: int i,j,ind,k;
1031:
1032: ind = 0;
1033: #if USE_UNROLL
1034: switch ( nd_bpe ) {
1035: case 3:
1036: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1037: u2 = d2[i];
1038: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1039: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1040: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1041: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1042: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1043: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1044: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1045: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1046: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1047: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1048: }
1049: return 0;
1050: break;
1051: case 4:
1052: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1053: u2 = d2[i];
1054: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1055: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1056: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1057: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1058: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1059: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1060: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1061: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1062: }
1063: return 0;
1064: break;
1065: case 6:
1066: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1067: u2 = d2[i];
1068: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1069: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1070: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1071: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1072: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1073: }
1074: return 0;
1075: break;
1076: case 8:
1077: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1078: u2 = d2[i];
1079: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1080: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1081: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1082: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1083: }
1084: return 0;
1085: break;
1086: case 16:
1087: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1088: u2 = d2[i];
1089: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1090: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1091: }
1092: return 0;
1093: break;
1094: case 32:
1095: for ( i = nd_exporigin; i < nd_wpd; i++ )
1096: if ( d1[i]+d2[i]<d1[i] ) return 1;
1097: return 0;
1098: break;
1099: default:
1100: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1101: u2 = d2[i];
1102: k = (nd_epw-1)*nd_bpe;
1103: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1104: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1105: }
1106: return 0;
1107: break;
1108: }
1109: #else
1110: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1111: u2 = d2[i];
1112: k = (nd_epw-1)*nd_bpe;
1113: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1114: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1115: }
1116: return 0;
1117: #endif
1118: }
1119:
1120: int ndl_check_bound2(int index,UINT *d2)
1121: {
1122: return ndl_check_bound(nd_bound[index],d2);
1123: }
1124:
1125: INLINE int ndl_hash_value(UINT *d)
1126: {
1127: int i;
1128: int r;
1129:
1130: r = 0;
1131: for ( i = 0; i < nd_wpd; i++ )
1132: r = ((r<<16)+d[i])%REDTAB_LEN;
1133: return r;
1134: }
1135:
1136: INLINE int ndl_find_reducer(UINT *dg)
1137: {
1138: RHist r;
1139: int d,k,i;
1140:
1141: d = ndl_hash_value(dg);
1142: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1143: if ( ndl_equal(dg,DL(r)) ) {
1144: if ( k > 0 ) nd_notfirst++;
1145: nd_found++;
1146: return r->index;
1147: }
1148: }
1149: if ( Reverse )
1150: for ( i = nd_psn-1; i >= 0; i-- ) {
1151: r = nd_psh[i];
1152: if ( ndl_reducible(dg,DL(r)) ) {
1153: nd_create++;
1154: nd_append_red(dg,i);
1155: return i;
1156: }
1157: }
1158: else
1159: for ( i = 0; i < nd_psn; i++ ) {
1160: r = nd_psh[i];
1161: if ( ndl_reducible(dg,DL(r)) ) {
1162: nd_create++;
1163: nd_append_red(dg,i);
1164: return i;
1165: }
1166: }
1167: return -1;
1168: }
1169:
1170: ND nd_merge(ND p1,ND p2)
1171: {
1172: int n,c;
1173: int t,can,td1,td2;
1174: ND r;
1175: NM m1,m2,mr0,mr,s;
1176:
1177: if ( !p1 ) return p2;
1178: else if ( !p2 ) return p1;
1179: else {
1180: can = 0;
1181: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1182: c = DL_COMPARE(DL(m1),DL(m2));
1183: switch ( c ) {
1184: case 0:
1185: s = m1; m1 = NEXT(m1);
1186: can++; NEXTNM2(mr0,mr,s);
1187: s = m2; m2 = NEXT(m2); FREENM(s);
1188: break;
1189: case 1:
1190: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1191: break;
1192: case -1:
1193: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1194: break;
1195: }
1196: }
1197: if ( !mr0 )
1198: if ( m1 ) mr0 = m1;
1199: else if ( m2 ) mr0 = m2;
1200: else return 0;
1201: else if ( m1 ) NEXT(mr) = m1;
1202: else if ( m2 ) NEXT(mr) = m2;
1203: else NEXT(mr) = 0;
1204: BDY(p1) = mr0;
1205: SG(p1) = MAX(SG(p1),SG(p2));
1206: LEN(p1) = LEN(p1)+LEN(p2)-can;
1207: FREEND(p2);
1208: return p1;
1209: }
1210: }
1211:
1212: ND nd_add(int mod,ND p1,ND p2)
1213: {
1214: int n,c;
1215: int t,can,td1,td2;
1216: ND r;
1217: NM m1,m2,mr0,mr,s;
1218:
1219: if ( !p1 ) return p2;
1220: else if ( !p2 ) return p1;
1221: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1222: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1223: else if ( !mod ) return nd_add_q(p1,p2);
1224: else {
1225: can = 0;
1226: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1227: c = DL_COMPARE(DL(m1),DL(m2));
1228: switch ( c ) {
1229: case 0:
1230: t = ((CM(m1))+(CM(m2))) - mod;
1231: if ( t < 0 ) t += mod;
1232: s = m1; m1 = NEXT(m1);
1233: if ( t ) {
1234: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1235: } else {
1236: can += 2; FREENM(s);
1237: }
1238: s = m2; m2 = NEXT(m2); FREENM(s);
1239: break;
1240: case 1:
1241: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1242: break;
1243: case -1:
1244: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1245: break;
1246: }
1247: }
1248: if ( !mr0 )
1249: if ( m1 ) mr0 = m1;
1250: else if ( m2 ) mr0 = m2;
1251: else return 0;
1252: else if ( m1 ) NEXT(mr) = m1;
1253: else if ( m2 ) NEXT(mr) = m2;
1254: else NEXT(mr) = 0;
1255: BDY(p1) = mr0;
1256: SG(p1) = MAX(SG(p1),SG(p2));
1257: LEN(p1) = LEN(p1)+LEN(p2)-can;
1258: FREEND(p2);
1259: return p1;
1260: }
1261: }
1262:
1263: /* XXX on opteron, the inlined manipulation of destructive additon of
1264: * two NM seems to make gcc optimizer get confused, so the part is
1265: * done in a function.
1266: */
1267:
1268: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1269: {
1270: NM s;
1271: P t;
1272: int can;
1273:
1274: addp(nd_vc,CP(*m1),CP(*m2),&t);
1275: s = *m1; *m1 = NEXT(*m1);
1276: if ( t ) {
1277: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1278: } else {
1279: can = 2; FREENM(s);
1280: }
1281: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1282: return can;
1283: }
1284:
1285: ND nd_add_q(ND p1,ND p2)
1286: {
1287: int n,c,can;
1288: ND r;
1289: NM m1,m2,mr0,mr,s;
1290: P t;
1291:
1292: if ( !p1 ) return p2;
1293: else if ( !p2 ) return p1;
1294: else {
1295: can = 0;
1296: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1297: c = DL_COMPARE(DL(m1),DL(m2));
1298: switch ( c ) {
1299: case 0:
1300: #if defined(__x86_64__)
1301: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1302: #else
1303: addp(nd_vc,CP(m1),CP(m2),&t);
1304: s = m1; m1 = NEXT(m1);
1305: if ( t ) {
1306: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1307: } else {
1308: can += 2; FREENM(s);
1309: }
1310: s = m2; m2 = NEXT(m2); FREENM(s);
1311: #endif
1312: break;
1313: case 1:
1314: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1315: break;
1316: case -1:
1317: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1318: break;
1319: }
1320: }
1321: if ( !mr0 )
1322: if ( m1 ) mr0 = m1;
1323: else if ( m2 ) mr0 = m2;
1324: else return 0;
1325: else if ( m1 ) NEXT(mr) = m1;
1326: else if ( m2 ) NEXT(mr) = m2;
1327: else NEXT(mr) = 0;
1328: BDY(p1) = mr0;
1329: SG(p1) = MAX(SG(p1),SG(p2));
1330: LEN(p1) = LEN(p1)+LEN(p2)-can;
1331: FREEND(p2);
1332: return p1;
1333: }
1334: }
1335:
1336: ND nd_add_sf(ND p1,ND p2)
1337: {
1338: int n,c,can;
1339: ND r;
1340: NM m1,m2,mr0,mr,s;
1341: int t;
1342:
1343: if ( !p1 ) return p2;
1344: else if ( !p2 ) return p1;
1345: else {
1346: can = 0;
1347: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1348: c = DL_COMPARE(DL(m1),DL(m2));
1349: switch ( c ) {
1350: case 0:
1351: t = _addsf(CM(m1),CM(m2));
1352: s = m1; m1 = NEXT(m1);
1353: if ( t ) {
1354: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1355: } else {
1356: can += 2; FREENM(s);
1357: }
1358: s = m2; m2 = NEXT(m2); FREENM(s);
1359: break;
1360: case 1:
1361: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1362: break;
1363: case -1:
1364: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1365: break;
1366: }
1367: }
1368: if ( !mr0 )
1369: if ( m1 ) mr0 = m1;
1370: else if ( m2 ) mr0 = m2;
1371: else return 0;
1372: else if ( m1 ) NEXT(mr) = m1;
1373: else if ( m2 ) NEXT(mr) = m2;
1374: else NEXT(mr) = 0;
1375: BDY(p1) = mr0;
1376: SG(p1) = MAX(SG(p1),SG(p2));
1377: LEN(p1) = LEN(p1)+LEN(p2)-can;
1378: FREEND(p2);
1379: return p1;
1380: }
1381: }
1382:
1383:
1384: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1385: {
1386: int c,c1,c2;
1387: Z cg,cred,gcd,tq;
1388: P cgp,credp,gcdp;
1389: Obj tr,tr1;
1390:
1391: if ( mod == -1 ) {
1392: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1393: *divp = (Obj)ONE;
1394: } else if ( mod == -2 ) {
1395: Z inv,t;
1396: divlf(ONE,HCZ(p),&inv);
1397: chsgnlf(HCZ(g),&t);
1398: mullf(inv,t,&CZ(mul));
1399: *divp = (Obj)ONE;
1400: } else if ( mod ) {
1401: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1402: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1403: *divp = (Obj)ONE;
1404: } else if ( nd_vc ) {
1405: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1406: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1407: chsgnp(cgp,&CP(mul));
1408: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1409: if ( dn ) {
1410: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1411: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1412: }
1413: *divp = (Obj)credp;
1414: } else {
1415: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1416: chsgnz(cg,&CQ(mul));
1417: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1418: if ( dn ) {
1419: mulz(dn->z,cred,&tq); dn->z = tq;
1420: }
1421: *divp = (Obj)cred;
1422: }
1423: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1424: }
1425:
1426: /* ret=1 : success, ret=0 : overflow */
1427: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1428: {
1429: NM m,mrd,tail;
1430: NM mul;
1431: int n,sugar,psugar,sugar0,stat,index;
1432: int c,c1,c2,dummy;
1433: RHist h;
1434: NDV p,red;
1435: Q cg,cred,gcd,tq,qq;
1436: Z iq;
1437: DP dmul;
1438: NODE node;
1439: LIST hist;
1440: double hmag;
1441: P tp,tp1;
1442: Obj tr,tr1,div;
1443: union oNDC hg;
1444: P cont;
1445:
1446: if ( dn ) {
1447: if ( mod )
1448: dn->m = 1;
1449: else if ( nd_vc )
1450: dn->r = (R)ONE;
1451: else
1452: dn->z = ONE;
1453: }
1454: if ( !g ) {
1455: *rp = d;
1456: return 1;
1457: }
1458: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1459:
1460: sugar0 = sugar = SG(g);
1461: n = NV(g);
1462: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1463: if ( d )
1464: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1465: for ( ; g; ) {
1466: index = ndl_find_reducer(HDL(g));
1467: if ( index >= 0 ) {
1468: h = nd_psh[index];
1469: ndl_sub(HDL(g),DL(h),DL(mul));
1470: if ( ndl_check_bound2(index,DL(mul)) ) {
1471: nd_free(g); nd_free(d);
1472: return 0;
1473: }
1474: p = nd_demand ? ndv_load(index) : ps[index];
1475: /* d+g -> div*(d+g)+mul*p */
1476: g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1477: if ( nd_gentrace ) {
1478: /* Trace=[div,index,mul,ONE] */
1479: STOQ(index,iq);
1480: nmtodp(mod,mul,&dmul);
1481: node = mknode(4,div,iq,dmul,ONE);
1482: }
1483: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1484: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1485: hg = HCU(g);
1486: nd_removecont2(d,g);
1487: if ( dn || nd_gentrace ) {
1488: /* overwrite cont : Trace=[div,index,mul,cont] */
1489: cont = ndc_div(mod,hg,HCU(g));
1490: if ( dn ) {
1491: if ( nd_vc ) {
1492: divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
1493: reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
1494: } else divz(dn->z,(Z)cont,&dn->z);
1495: }
1496: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1497: }
1498: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1499: }
1500: MKLIST(hist,node);
1501: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1502: } else if ( !full ) {
1503: *rp = g;
1504: return 1;
1505: } else {
1506: m = BDY(g);
1507: if ( NEXT(m) ) {
1508: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1509: } else {
1510: FREEND(g); g = 0;
1511: }
1512: if ( d ) {
1513: NEXT(tail)=m; tail=m; LEN(d)++;
1514: } else {
1515: MKND(n,m,1,d); tail = BDY(d);
1516: }
1517: }
1518: }
1519: if ( d ) SG(d) = sugar;
1520: *rp = d;
1521: return 1;
1522: }
1523:
1524: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1525: {
1526: int hindex,index;
1527: NDV p;
1528: ND u,d,red;
1529: NODE l;
1530: NM mul,m,mrd,tail;
1531: int sugar,psugar,n,h_reducible;
1532: PGeoBucket bucket;
1533: int c,c1,c2;
1534: Z cg,cred,gcd,zzz;
1535: RHist h;
1536: double hmag,gmag;
1537: int count = 0;
1538: int hcount = 0;
1539:
1540: if ( !g ) {
1541: *rp = 0;
1542: return 1;
1543: }
1544: sugar = SG(g);
1545: n = NV(g);
1546: if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1547: bucket = create_pbucket();
1548: add_pbucket(mod,bucket,g);
1549: d = 0;
1550: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1551: while ( 1 ) {
1552: if ( mod > 0 || mod == -1 )
1553: hindex = head_pbucket(mod,bucket);
1554: else if ( mod == -2 )
1555: hindex = head_pbucket_lf(bucket);
1556: else
1557: hindex = head_pbucket_q(bucket);
1558: if ( hindex < 0 ) {
1559: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1560: if ( d ) SG(d) = sugar;
1561: *rp = d;
1562: return 1;
1563: }
1564: g = bucket->body[hindex];
1565: index = ndl_find_reducer(HDL(g));
1566: if ( index >= 0 ) {
1567: count++;
1568: if ( !d ) hcount++;
1569: h = nd_psh[index];
1570: ndl_sub(HDL(g),DL(h),DL(mul));
1571: if ( ndl_check_bound2(index,DL(mul)) ) {
1572: nd_free(d);
1573: free_pbucket(bucket);
1574: *rp = 0;
1575: return 0;
1576: }
1577: p = ps[index];
1578: if ( mod == -1 )
1579: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1580: else if ( mod == -2 ) {
1581: Z inv,t;
1582: divlf(ONE,HCZ(p),&inv);
1583: chsgnlf(HCZ(g),&t);
1584: mullf(inv,t,&CZ(mul));
1585: } else if ( mod ) {
1586: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1587: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1588: } else {
1589: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1590: chsgnz(cg,&CQ(mul));
1591: nd_mul_c_q(d,(P)cred);
1592: mulq_pbucket(bucket,cred);
1593: g = bucket->body[hindex];
1594: gmag = (double)p_mag((P)HCQ(g));
1595: }
1596: red = ndv_mul_nm(mod,mul,p);
1597: bucket->body[hindex] = nd_remove_head(g);
1598: red = nd_remove_head(red);
1599: add_pbucket(mod,bucket,red);
1600: psugar = SG(p)+TD(DL(mul));
1601: sugar = MAX(sugar,psugar);
1602: if ( !mod && hmag && (gmag > hmag) ) {
1603: g = normalize_pbucket(mod,bucket);
1604: if ( !g ) {
1605: if ( d ) SG(d) = sugar;
1606: *rp = d;
1607: return 1;
1608: }
1609: nd_removecont2(d,g);
1610: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1611: add_pbucket(mod,bucket,g);
1612: }
1613: } else if ( !full ) {
1614: g = normalize_pbucket(mod,bucket);
1615: if ( g ) SG(g) = sugar;
1616: *rp = g;
1617: return 1;
1618: } else {
1619: m = BDY(g);
1620: if ( NEXT(m) ) {
1621: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1622: } else {
1623: FREEND(g); g = 0;
1624: }
1625: bucket->body[hindex] = g;
1626: NEXT(m) = 0;
1627: if ( d ) {
1628: NEXT(tail)=m; tail=m; LEN(d)++;
1629: } else {
1630: MKND(n,m,1,d); tail = BDY(d);
1631: }
1632: }
1633: }
1634: }
1635:
1636: /* input : list of NDV, cand : list of NDV */
1637:
1638: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1639: {
1640: int n,i,stat;
1641: ND nf,d;
1642: NDV r;
1643: NODE t,s;
1644: union oNDC dn;
1645: Z q;
1646: LIST list;
1647:
1648: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1649: n = length(cand);
1650:
1651: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1652: /* membercheck : list is a subset of Id(cand) ? */
1653: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1654: again:
1655: nd_tracelist = 0;
1656: if ( nd_bpe > obpe )
1657: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1658: else
1659: r = (NDV)BDY(t);
1660: #if 0
1661: // moved to nd_f4_lf_trace()
1662: if ( m == -2 ) ndv_mod(m,r);
1663: #endif
1664: d = ndvtond(m,r);
1665: stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1666: if ( !stat ) {
1667: nd_reconstruct(0,0);
1668: goto again;
1669: } else if ( nf ) return 0;
1670: if ( nd_gentrace ) {
1671: nd_tracelist = reverse_node(nd_tracelist);
1672: MKLIST(list,nd_tracelist);
1673: STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1674: MKNODE(s,list,nd_alltracelist);
1675: nd_alltracelist = s; nd_tracelist = 0;
1676: }
1677: if ( DP_Print ) { printf("."); fflush(stdout); }
1678: }
1679: if ( DP_Print ) { printf("\n"); }
1680: return 1;
1681: }
1682:
1683: ND nd_remove_head(ND p)
1684: {
1685: NM m;
1686:
1687: m = BDY(p);
1688: if ( !NEXT(m) ) {
1689: FREEND(p); p = 0;
1690: } else {
1691: BDY(p) = NEXT(m); LEN(p)--;
1692: }
1693: FREENM(m);
1694: return p;
1695: }
1696:
1697: ND nd_separate_head(ND p,ND *head)
1698: {
1699: NM m,m0;
1700: ND r;
1701:
1702: m = BDY(p);
1703: if ( !NEXT(m) ) {
1704: *head = p; p = 0;
1705: } else {
1706: m0 = m;
1707: BDY(p) = NEXT(m); LEN(p)--;
1708: NEXT(m0) = 0;
1709: MKND(NV(p),m0,1,r);
1710: *head = r;
1711: }
1712: return p;
1713: }
1714:
1715: PGeoBucket create_pbucket()
1716: {
1717: PGeoBucket g;
1718:
1719: g = CALLOC(1,sizeof(struct oPGeoBucket));
1720: g->m = -1;
1721: return g;
1722: }
1723:
1724: void free_pbucket(PGeoBucket b) {
1725: int i;
1726:
1727: for ( i = 0; i <= b->m; i++ )
1728: if ( b->body[i] ) {
1729: nd_free(b->body[i]);
1730: b->body[i] = 0;
1731: }
1732: GCFREE(b);
1733: }
1734:
1735: void add_pbucket_symbolic(PGeoBucket g,ND d)
1736: {
1737: int l,i,k,m;
1738:
1739: if ( !d )
1740: return;
1741: l = LEN(d);
1742: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1743: /* 2^(k-1) < l <= 2^k (=m) */
1744: d = nd_merge(g->body[k],d);
1745: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1746: g->body[k] = 0;
1747: d = nd_merge(g->body[k+1],d);
1748: }
1749: g->body[k] = d;
1750: g->m = MAX(g->m,k);
1751: }
1752:
1753: void add_pbucket(int mod,PGeoBucket g,ND d)
1754: {
1755: int l,i,k,m;
1756:
1757: if ( !d )
1758: return;
1759: l = LEN(d);
1760: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1761: /* 2^(k-1) < l <= 2^k (=m) */
1762: d = nd_add(mod,g->body[k],d);
1763: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1764: g->body[k] = 0;
1765: d = nd_add(mod,g->body[k+1],d);
1766: }
1767: g->body[k] = d;
1768: g->m = MAX(g->m,k);
1769: }
1770:
1771: void mulq_pbucket(PGeoBucket g,Z c)
1772: {
1773: int k;
1774:
1775: for ( k = 0; k <= g->m; k++ )
1776: nd_mul_c_q(g->body[k],(P)c);
1777: }
1778:
1779: NM remove_head_pbucket_symbolic(PGeoBucket g)
1780: {
1781: int j,i,k,c;
1782: NM head;
1783:
1784: k = g->m;
1785: j = -1;
1786: for ( i = 0; i <= k; i++ ) {
1787: if ( !g->body[i] ) continue;
1788: if ( j < 0 ) j = i;
1789: else {
1790: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1791: if ( c > 0 )
1792: j = i;
1793: else if ( c == 0 )
1794: g->body[i] = nd_remove_head(g->body[i]);
1795: }
1796: }
1797: if ( j < 0 ) return 0;
1798: else {
1799: head = BDY(g->body[j]);
1800: if ( !NEXT(head) ) {
1801: FREEND(g->body[j]);
1802: g->body[j] = 0;
1803: } else {
1804: BDY(g->body[j]) = NEXT(head);
1805: LEN(g->body[j])--;
1806: }
1807: return head;
1808: }
1809: }
1810:
1811: int head_pbucket(int mod,PGeoBucket g)
1812: {
1813: int j,i,c,k,nv,sum;
1814: UINT *di,*dj;
1815: ND gi,gj;
1816:
1817: k = g->m;
1818: while ( 1 ) {
1819: j = -1;
1820: for ( i = 0; i <= k; i++ ) {
1821: if ( !(gi = g->body[i]) )
1822: continue;
1823: if ( j < 0 ) {
1824: j = i;
1825: gj = g->body[j];
1826: dj = HDL(gj);
1827: sum = HCM(gj);
1828: } else {
1829: c = DL_COMPARE(HDL(gi),dj);
1830: if ( c > 0 ) {
1831: if ( sum ) HCM(gj) = sum;
1832: else g->body[j] = nd_remove_head(gj);
1833: j = i;
1834: gj = g->body[j];
1835: dj = HDL(gj);
1836: sum = HCM(gj);
1837: } else if ( c == 0 ) {
1838: if ( mod == -1 )
1839: sum = _addsf(sum,HCM(gi));
1840: else {
1841: sum = sum+HCM(gi)-mod;
1842: if ( sum < 0 ) sum += mod;
1843: }
1844: g->body[i] = nd_remove_head(gi);
1845: }
1846: }
1847: }
1848: if ( j < 0 ) return -1;
1849: else if ( sum ) {
1850: HCM(gj) = sum;
1851: return j;
1852: } else
1853: g->body[j] = nd_remove_head(gj);
1854: }
1855: }
1856:
1857: int head_pbucket_q(PGeoBucket g)
1858: {
1859: int j,i,c,k,nv;
1860: Z sum,t;
1861: ND gi,gj;
1862:
1863: k = g->m;
1864: while ( 1 ) {
1865: j = -1;
1866: for ( i = 0; i <= k; i++ ) {
1867: if ( !(gi = g->body[i]) ) continue;
1868: if ( j < 0 ) {
1869: j = i;
1870: gj = g->body[j];
1871: sum = HCQ(gj);
1872: } else {
1873: nv = NV(gi);
1874: c = DL_COMPARE(HDL(gi),HDL(gj));
1875: if ( c > 0 ) {
1876: if ( sum ) HCQ(gj) = sum;
1877: else g->body[j] = nd_remove_head(gj);
1878: j = i;
1879: gj = g->body[j];
1880: sum = HCQ(gj);
1881: } else if ( c == 0 ) {
1882: addz(sum,HCQ(gi),&t);
1883: sum = t;
1884: g->body[i] = nd_remove_head(gi);
1885: }
1886: }
1887: }
1888: if ( j < 0 ) return -1;
1889: else if ( sum ) {
1890: HCQ(gj) = sum;
1891: return j;
1892: } else
1893: g->body[j] = nd_remove_head(gj);
1894: }
1895: }
1896:
1897: int head_pbucket_lf(PGeoBucket g)
1898: {
1899: int j,i,c,k,nv;
1900: Z sum,t;
1901: ND gi,gj;
1902:
1903: k = g->m;
1904: while ( 1 ) {
1905: j = -1;
1906: for ( i = 0; i <= k; i++ ) {
1907: if ( !(gi = g->body[i]) ) continue;
1908: if ( j < 0 ) {
1909: j = i;
1910: gj = g->body[j];
1911: sum = HCZ(gj);
1912: } else {
1913: nv = NV(gi);
1914: c = DL_COMPARE(HDL(gi),HDL(gj));
1915: if ( c > 0 ) {
1916: if ( sum ) HCZ(gj) = sum;
1917: else g->body[j] = nd_remove_head(gj);
1918: j = i;
1919: gj = g->body[j];
1920: sum = HCZ(gj);
1921: } else if ( c == 0 ) {
1922: addlf(sum,HCZ(gi),&t);
1923: sum = t;
1924: g->body[i] = nd_remove_head(gi);
1925: }
1926: }
1927: }
1928: if ( j < 0 ) return -1;
1929: else if ( sum ) {
1930: HCZ(gj) = sum;
1931: return j;
1932: } else
1933: g->body[j] = nd_remove_head(gj);
1934: }
1935: }
1936:
1937: ND normalize_pbucket(int mod,PGeoBucket g)
1938: {
1939: int i;
1940: ND r,t;
1941:
1942: r = 0;
1943: for ( i = 0; i <= g->m; i++ ) {
1944: r = nd_add(mod,r,g->body[i]);
1945: g->body[i] = 0;
1946: }
1947: g->m = -1;
1948: return r;
1949: }
1950:
1951: #if 0
1952: void register_hcf(NDV p)
1953: {
1954: DCP dc,t;
1955: P hc,h;
1956: int c;
1957: NODE l,l1,prev;
1958:
1959: hc = p->body->c.p;
1960: if ( !nd_vc || NUM(hc) ) return;
1961: fctrp(nd_vc,hc,&dc);
1962: for ( t = dc; t; t = NEXT(t) ) {
1963: h = t->c;
1964: if ( NUM(h) ) continue;
1965: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1966: c = compp(nd_vc,h,(P)BDY(l));
1967: if ( c >= 0 ) break;
1968: }
1969: if ( !l || c > 0 ) {
1970: MKNODE(l1,h,l);
1971: if ( !prev )
1972: nd_hcf = l1;
1973: else
1974: NEXT(prev) = l1;
1975: }
1976: }
1977: }
1978: #else
1979: void register_hcf(NDV p)
1980: {
1981: DCP dc,t;
1982: P hc,h,q;
1983: Q dmy;
1984: int c;
1985: NODE l,l1,prev;
1986:
1987: hc = p->body->c.p;
1988: if ( NUM(hc) ) return;
1989: ptozp(hc,1,&dmy,&h);
1990: #if 1
1991: for ( l = nd_hcf; l; l = NEXT(l) ) {
1992: while ( 1 ) {
1993: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
1994: else break;
1995: }
1996: }
1997: if ( NUM(h) ) return;
1998: #endif
1999: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2000: c = compp(nd_vc,h,(P)BDY(l));
2001: if ( c >= 0 ) break;
2002: }
2003: if ( !l || c > 0 ) {
2004: MKNODE(l1,h,l);
2005: if ( !prev )
2006: nd_hcf = l1;
2007: else
2008: NEXT(prev) = l1;
2009: }
2010: }
2011: #endif
2012:
2013: int do_diagonalize(int sugar,int m)
2014: {
2015: int i,nh,stat;
2016: NODE r,g,t;
2017: ND h,nf,s,head;
2018: NDV nfv;
2019: Q q;
2020: P nm,nmp,dn,mnp,dnp,cont,cont1;
2021: union oNDC hc;
2022: NODE node;
2023: LIST l;
2024: Z iq;
2025:
2026: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2027: if ( nd_gentrace ) {
2028: /* Trace = [1,index,1,1] */
2029: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2030: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2031: }
2032: if ( nd_demand )
2033: nfv = ndv_load(i);
2034: else
2035: nfv = nd_ps[i];
2036: s = ndvtond(m,nfv);
2037: s = nd_separate_head(s,&head);
2038: stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
2039: if ( !stat ) return 0;
2040: ndv_free(nfv);
2041: hc = HCU(nf); nd_removecont(m,nf);
2042: cont = ndc_div(m,hc,HCU(nf));
2043: if ( nd_gentrace ) finalize_tracelist(i,cont);
2044: nfv = ndtondv(m,nf);
2045: nd_free(nf);
2046: nd_bound[i] = ndv_compute_bound(nfv);
2047: if ( !m ) register_hcf(nfv);
2048: if ( nd_demand ) {
2049: ndv_save(nfv,i);
2050: ndv_free(nfv);
2051: } else
2052: nd_ps[i] = nfv;
2053: }
2054: return 1;
2055: }
2056:
2057: LIST compute_splist()
2058: {
2059: NODE g,tn0,tn,node;
2060: LIST l0;
2061: ND_pairs d,t;
2062: int i;
2063: Z i1,i2;
2064:
2065: g = 0; d = 0;
2066: for ( i = 0; i < nd_psn; i++ ) {
2067: d = update_pairs(d,g,i,0);
2068: g = update_base(g,i);
2069: }
2070: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2071: NEXTNODE(tn0,tn);
2072: STOQ(t->i1,i1); STOQ(t->i2,i2);
2073: node = mknode(2,i1,i2); MKLIST(l0,node);
2074: BDY(tn) = l0;
2075: }
2076: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2077: return l0;
2078: }
2079:
2080: /* return value = 0 => input is not a GB */
2081:
2082: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2083: {
2084: int i,nh,sugar,stat;
2085: NODE r,g,t;
2086: ND_pairs d;
2087: ND_pairs l;
2088: ND h,nf,s,head,nf1;
2089: NDV nfv;
2090: Z q;
2091: union oNDC dn,hc;
2092: int diag_count = 0;
2093: P cont;
2094: LIST list;
2095:
2096: g = 0; d = 0;
2097: for ( i = 0; i < nd_psn; i++ ) {
2098: d = update_pairs(d,g,i,gensyz);
2099: g = update_base(g,i);
2100: }
2101: sugar = 0;
2102: while ( d ) {
2103: again:
2104: l = nd_minp(d,&d);
2105: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2106: if ( SG(l) != sugar ) {
2107: if ( ishomo ) {
2108: diag_count = 0;
2109: stat = do_diagonalize(sugar,m);
2110: if ( !stat ) {
2111: NEXT(l) = d; d = l;
2112: d = nd_reconstruct(0,d);
2113: goto again;
2114: }
2115: }
2116: sugar = SG(l);
2117: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2118: }
2119: stat = nd_sp(m,0,l,&h);
2120: if ( !stat ) {
2121: NEXT(l) = d; d = l;
2122: d = nd_reconstruct(0,d);
2123: goto again;
2124: }
2125: #if USE_GEOBUCKET
2126: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2127: :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
2128: #else
2129: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
2130: #endif
2131: if ( !stat ) {
2132: NEXT(l) = d; d = l;
2133: d = nd_reconstruct(0,d);
2134: goto again;
2135: } else if ( nf ) {
2136: if ( checkonly || gensyz ) return 0;
2137: if ( nd_newelim ) {
2138: if ( nd_module ) {
2139: if ( MPOS(HDL(nf)) > 1 ) return 0;
2140: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2141: }
2142: if ( DP_Print ) { printf("+"); fflush(stdout); }
2143: hc = HCU(nf);
2144: nd_removecont(m,nf);
2145: if ( !m && nd_nalg ) {
2146: nd_monic(0,&nf);
2147: nd_removecont(m,nf);
2148: }
2149: if ( nd_gentrace ) {
2150: cont = ndc_div(m,hc,HCU(nf));
2151: if ( m || !UNIQ(cont) ) {
2152: t = mknode(4,NULLP,NULLP,NULLP,cont);
2153: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2154: nd_tracelist = t;
2155: }
2156: }
2157: nfv = ndtondv(m,nf); nd_free(nf);
2158: nh = ndv_newps(m,nfv,0,0);
2159: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2160: diag_count = 0;
2161: stat = do_diagonalize(sugar,m);
2162: if ( !stat ) {
2163: NEXT(l) = d; d = l;
2164: d = nd_reconstruct(1,d);
2165: goto again;
2166: }
2167: }
2168: d = update_pairs(d,g,nh,0);
2169: g = update_base(g,nh);
2170: FREENDP(l);
2171: } else {
2172: if ( nd_gentrace && gensyz ) {
2173: nd_tracelist = reverse_node(nd_tracelist);
2174: MKLIST(list,nd_tracelist);
2175: STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2176: MKNODE(t,list,nd_alltracelist);
2177: nd_alltracelist = t; nd_tracelist = 0;
2178: }
2179: if ( DP_Print ) { printf("."); fflush(stdout); }
2180: FREENDP(l);
2181: }
2182: }
2183: conv_ilist(nd_demand,0,g,indp);
2184: if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
2185: return g;
2186: }
2187:
2188: /* splist = [[i1,i2],...] */
2189:
2190: int check_splist(int m,NODE splist)
2191: {
2192: NODE t,p;
2193: ND_pairs d,r,l;
2194: int stat;
2195: ND h,nf;
2196:
2197: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2198: p = BDY((LIST)BDY(t));
2199: NEXTND_pairs(d,r);
2200: r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
2201: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2202: SG(r) = TD(LCM(r)); /* XXX */
2203: }
2204: if ( d ) NEXT(r) = 0;
2205:
2206: while ( d ) {
2207: again:
2208: l = nd_minp(d,&d);
2209: stat = nd_sp(m,0,l,&h);
2210: if ( !stat ) {
2211: NEXT(l) = d; d = l;
2212: d = nd_reconstruct(0,d);
2213: goto again;
2214: }
2215: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
2216: if ( !stat ) {
2217: NEXT(l) = d; d = l;
2218: d = nd_reconstruct(0,d);
2219: goto again;
2220: } else if ( nf ) return 0;
2221: if ( DP_Print) { printf("."); fflush(stdout); }
2222: }
2223: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2224: return 1;
2225: }
2226:
2227: int check_splist_f4(int m,NODE splist)
2228: {
2229: UINT *s0vect;
2230: PGeoBucket bucket;
2231: NODE p,rp0,t;
2232: ND_pairs d,r,l,ll;
2233: int col,stat;
2234:
2235: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2236: p = BDY((LIST)BDY(t));
2237: NEXTND_pairs(d,r);
2238: r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
2239: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2240: SG(r) = TD(LCM(r)); /* XXX */
2241: }
2242: if ( d ) NEXT(r) = 0;
2243:
2244: while ( d ) {
2245: l = nd_minsugarp(d,&d);
2246: bucket = create_pbucket();
2247: stat = nd_sp_f4(m,0,l,bucket);
2248: if ( !stat ) {
2249: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2250: NEXT(ll) = d; d = l;
2251: d = nd_reconstruct(0,d);
2252: continue;
2253: }
2254: if ( bucket->m < 0 ) continue;
2255: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2256: if ( !col ) {
2257: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2258: NEXT(ll) = d; d = l;
2259: d = nd_reconstruct(0,d);
2260: continue;
2261: }
2262: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2263: }
2264: return 1;
2265: }
2266:
2267: int do_diagonalize_trace(int sugar,int m)
2268: {
2269: int i,nh,stat;
2270: NODE r,g,t;
2271: ND h,nf,nfq,s,head;
2272: NDV nfv,nfqv;
2273: Q q,den,num;
2274: union oNDC hc;
2275: NODE node;
2276: LIST l;
2277: Z iq;
2278: P cont,cont1;
2279:
2280: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2281: if ( nd_gentrace ) {
2282: /* Trace = [1,index,1,1] */
2283: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2284: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2285: }
2286: /* for nd_ps */
2287: s = ndvtond(m,nd_ps[i]);
2288: s = nd_separate_head(s,&head);
2289: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2290: if ( !stat ) return 0;
2291: nf = nd_add(m,head,nf);
2292: ndv_free(nd_ps[i]);
2293: nd_ps[i] = ndtondv(m,nf);
2294: nd_free(nf);
2295:
2296: /* for nd_ps_trace */
2297: if ( nd_demand )
2298: nfv = ndv_load(i);
2299: else
2300: nfv = nd_ps_trace[i];
2301: s = ndvtond(0,nfv);
2302: s = nd_separate_head(s,&head);
2303: stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
2304: if ( !stat ) return 0;
2305: ndv_free(nfv);
2306: hc = HCU(nf); nd_removecont(0,nf);
2307: cont = ndc_div(0,hc,HCU(nf));
2308: if ( nd_gentrace ) finalize_tracelist(i,cont);
2309: nfv = ndtondv(0,nf);
2310: nd_free(nf);
2311: nd_bound[i] = ndv_compute_bound(nfv);
2312: register_hcf(nfv);
2313: if ( nd_demand ) {
2314: ndv_save(nfv,i);
2315: ndv_free(nfv);
2316: } else
2317: nd_ps_trace[i] = nfv;
2318: }
2319: return 1;
2320: }
2321:
2322: static struct oEGT eg_invdalg;
2323: struct oEGT eg_le;
2324:
2325: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2326: {
2327: NODE tn;
2328: P p1;
2329:
2330: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2331: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2332: }
2333: *r = p;
2334: }
2335:
2336: NODE nd_gb_trace(int m,int ishomo,int **indp)
2337: {
2338: int i,nh,sugar,stat;
2339: NODE r,g,t;
2340: ND_pairs d;
2341: ND_pairs l;
2342: ND h,nf,nfq,s,head;
2343: NDV nfv,nfqv;
2344: Z q,den,num;
2345: P hc;
2346: union oNDC dn,hnfq;
2347: struct oEGT eg_monic,egm0,egm1;
2348: int diag_count = 0;
2349: P cont;
2350: LIST list;
2351:
2352: init_eg(&eg_monic);
2353: init_eg(&eg_invdalg);
2354: init_eg(&eg_le);
2355: g = 0; d = 0;
2356: for ( i = 0; i < nd_psn; i++ ) {
2357: d = update_pairs(d,g,i,0);
2358: g = update_base(g,i);
2359: }
2360: sugar = 0;
2361: while ( d ) {
2362: again:
2363: l = nd_minp(d,&d);
2364: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2365: if ( SG(l) != sugar ) {
2366: #if 1
2367: if ( ishomo ) {
2368: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2369: stat = do_diagonalize_trace(sugar,m);
2370: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2371: diag_count = 0;
2372: if ( !stat ) {
2373: NEXT(l) = d; d = l;
2374: d = nd_reconstruct(1,d);
2375: goto again;
2376: }
2377: }
2378: #endif
2379: sugar = SG(l);
2380: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2381: }
2382: stat = nd_sp(m,0,l,&h);
2383: if ( !stat ) {
2384: NEXT(l) = d; d = l;
2385: d = nd_reconstruct(1,d);
2386: goto again;
2387: }
2388: #if USE_GEOBUCKET
2389: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
2390: #else
2391: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
2392: #endif
2393: if ( !stat ) {
2394: NEXT(l) = d; d = l;
2395: d = nd_reconstruct(1,d);
2396: goto again;
2397: } else if ( nf ) {
2398: if ( nd_demand ) {
2399: nfqv = ndv_load(nd_psn);
2400: nfq = ndvtond(0,nfqv);
2401: } else
2402: nfq = 0;
2403: if ( !nfq ) {
2404: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
2405: NEXT(l) = d; d = l;
2406: d = nd_reconstruct(1,d);
2407: goto again;
2408: }
2409: }
2410: if ( nfq ) {
2411: /* m|HC(nfq) => failure */
2412: if ( nd_vc ) {
2413: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2414: } else
2415: q = HCQ(nfq);
2416: if ( !remqi((Q)q,m) ) return 0;
2417:
2418: if ( DP_Print ) { printf("+"); fflush(stdout); }
2419: hnfq = HCU(nfq);
2420: if ( nd_nalg ) {
2421: /* m|DN(HC(nf)^(-1)) => failure */
2422: get_eg(&egm0);
2423: if ( !nd_monic(m,&nfq) ) return 0;
2424: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2425: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2426: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2427: } else {
2428: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2429: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2430: }
2431: if ( nd_gentrace ) {
2432: cont = ndc_div(0,hnfq,HCU(nfqv));
2433: if ( !UNIQ(cont) ) {
2434: t = mknode(4,NULLP,NULLP,NULLP,cont);
2435: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2436: nd_tracelist = t;
2437: }
2438: }
2439: nh = ndv_newps(0,nfv,nfqv,0);
2440: if ( ishomo && ++diag_count == diag_period ) {
2441: diag_count = 0;
2442: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2443: stat = do_diagonalize_trace(sugar,m);
2444: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2445: if ( !stat ) {
2446: NEXT(l) = d; d = l;
2447: d = nd_reconstruct(1,d);
2448: goto again;
2449: }
2450: }
2451: d = update_pairs(d,g,nh,0);
2452: g = update_base(g,nh);
2453: } else {
2454: if ( DP_Print ) { printf("*"); fflush(stdout); }
2455: }
2456: } else {
2457: if ( DP_Print ) { printf("."); fflush(stdout); }
2458: }
2459: FREENDP(l);
2460: }
2461: if ( nd_nalg ) {
2462: if ( DP_Print ) {
2463: print_eg("monic",&eg_monic);
2464: print_eg("invdalg",&eg_invdalg);
2465: print_eg("le",&eg_le);
2466: }
2467: }
2468: conv_ilist(nd_demand,1,g,indp);
2469: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2470: return g;
2471: }
2472:
2473: int ndv_compare(NDV *p1,NDV *p2)
2474: {
2475: return DL_COMPARE(HDL(*p1),HDL(*p2));
2476: }
2477:
2478: int ndv_compare_rev(NDV *p1,NDV *p2)
2479: {
2480: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2481: }
2482:
2483: int ndvi_compare(NDVI p1,NDVI p2)
2484: {
2485: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2486: }
2487:
2488: int ndvi_compare_rev(NDVI p1,NDVI p2)
2489: {
2490: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2491: }
2492:
2493: NODE ndv_reduceall(int m,NODE f)
2494: {
2495: int i,j,n,stat;
2496: ND nf,g,head;
2497: NODE t,a0,a;
2498: union oNDC dn;
2499: Q q,num,den;
2500: NODE node;
2501: LIST l;
2502: Z iq,jq;
2503: int *perm;
2504: union oNDC hc;
2505: P cont,cont1;
2506:
2507: if ( nd_nora ) return f;
2508: n = length(f);
2509: ndv_setup(m,0,f,0,1);
2510: perm = (int *)MALLOC(n*sizeof(int));
2511: if ( nd_gentrace ) {
2512: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
2513: perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
2514: }
2515: for ( i = 0; i < n; ) {
2516: if ( nd_gentrace ) {
2517: /* Trace = [1,index,1,1] */
2518: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2519: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2520: }
2521: g = ndvtond(m,nd_ps[i]);
2522: g = nd_separate_head(g,&head);
2523: stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
2524: if ( !stat )
2525: nd_reconstruct(0,0);
2526: else {
2527: if ( DP_Print ) { printf("."); fflush(stdout); }
2528: ndv_free(nd_ps[i]);
2529: hc = HCU(nf); nd_removecont(m,nf);
2530: if ( nd_gentrace ) {
2531: for ( t = nd_tracelist; t; t = NEXT(t) ) {
2532: jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
2533: STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
2534: }
2535: cont = ndc_div(m,hc,HCU(nf));
2536: finalize_tracelist(perm[i],cont);
2537: }
2538: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2539: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2540: i++;
2541: }
2542: }
2543: if ( DP_Print ) { printf("\n"); }
2544: for ( a0 = 0, i = 0; i < n; i++ ) {
2545: NEXTNODE(a0,a);
2546: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2547: else {
2548: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2549: BDY(a) = (pointer)nd_ps[j];
2550: }
2551: }
2552: NEXT(a) = 0;
2553: return a0;
2554: }
2555:
2556: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
2557: {
2558: ND_pairs d1,nd,cur,head,prev,remove;
2559:
2560: if ( !g ) return d;
2561: /* for testing */
2562: if ( gensyz && nd_gensyz == 2 ) {
2563: d1 = nd_newpairs(g,t);
2564: if ( !d )
2565: return d1;
2566: else {
2567: nd = d;
2568: while ( NEXT(nd) ) nd = NEXT(nd);
2569: NEXT(nd) = d1;
2570: return d;
2571: }
2572: }
2573: d = crit_B(d,t);
2574: d1 = nd_newpairs(g,t);
2575: d1 = crit_M(d1);
2576: d1 = crit_F(d1);
2577: if ( gensyz || do_weyl )
2578: head = d1;
2579: else {
2580: prev = 0; cur = head = d1;
2581: while ( cur ) {
2582: if ( crit_2( cur->i1,cur->i2 ) ) {
2583: remove = cur;
2584: if ( !prev ) head = cur = NEXT(cur);
2585: else cur = NEXT(prev) = NEXT(cur);
2586: FREENDP(remove);
2587: } else {
2588: prev = cur; cur = NEXT(cur);
2589: }
2590: }
2591: }
2592: if ( !d )
2593: return head;
2594: else {
2595: nd = d;
2596: while ( NEXT(nd) ) nd = NEXT(nd);
2597: NEXT(nd) = head;
2598: return d;
2599: }
2600: }
2601:
2602:
2603: ND_pairs nd_newpairs( NODE g, int t )
2604: {
2605: NODE h;
2606: UINT *dl;
2607: int ts,s,i,t0,min,max;
2608: ND_pairs r,r0;
2609:
2610: dl = DL(nd_psh[t]);
2611: ts = SG(nd_psh[t]) - TD(dl);
2612: if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
2613: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
2614: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
2615: continue;
2616: if ( nd_gbblock ) {
2617: t0 = (long)BDY(h);
2618: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2619: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2620: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2621: break;
2622: }
2623: if ( nd_gbblock[i] >= 0 )
2624: continue;
2625: }
2626: NEXTND_pairs(r0,r);
2627: r->i1 = (long)BDY(h);
2628: r->i2 = t;
2629: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2630: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2631: SG(r) = MAX(s,ts) + TD(LCM(r));
2632: /* experimental */
2633: if ( nd_sugarweight )
2634: r->sugar2 = ndl_weight2(r->lcm);
2635: }
2636: if ( r0 ) NEXT(r) = 0;
2637: return r0;
2638: }
2639:
2640: /* ipair = [i1,i2],[i1,i2],... */
2641: ND_pairs nd_ipairtospair(NODE ipair)
2642: {
2643: int s1,s2;
2644: NODE tn,t;
2645: ND_pairs r,r0;
2646:
2647: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2648: NEXTND_pairs(r0,r);
2649: tn = BDY((LIST)BDY(t));
2650: r->i1 = QTOS((Q)ARG0(tn));
2651: r->i2 = QTOS((Q)ARG1(tn));
2652: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2653: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2654: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2655: SG(r) = MAX(s1,s2) + TD(LCM(r));
2656: /* experimental */
2657: if ( nd_sugarweight )
2658: r->sugar2 = ndl_weight2(r->lcm);
2659: }
2660: if ( r0 ) NEXT(r) = 0;
2661: return r0;
2662: }
2663:
2664: /* kokokara */
2665:
2666: ND_pairs crit_B( ND_pairs d, int s )
2667: {
2668: ND_pairs cur,head,prev,remove;
2669: UINT *t,*tl,*lcm;
2670: int td,tdl;
2671:
2672: if ( !d ) return 0;
2673: t = DL(nd_psh[s]);
2674: prev = 0;
2675: head = cur = d;
2676: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
2677: while ( cur ) {
2678: tl = cur->lcm;
2679: if ( ndl_reducible(tl,t) ) {
2680: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
2681: if ( !ndl_equal(lcm,tl) ) {
2682: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2683: if (!ndl_equal(lcm,tl)) {
2684: remove = cur;
2685: if ( !prev ) {
2686: head = cur = NEXT(cur);
2687: } else {
2688: cur = NEXT(prev) = NEXT(cur);
2689: }
2690: FREENDP(remove);
2691: } else {
2692: prev = cur; cur = NEXT(cur);
2693: }
2694: } else {
2695: prev = cur; cur = NEXT(cur);
2696: }
2697: } else {
2698: prev = cur; cur = NEXT(cur);
2699: }
2700: }
2701: return head;
2702: }
2703:
2704: ND_pairs crit_M( ND_pairs d1 )
2705: {
2706: ND_pairs e,d2,d3,dd,p;
2707: UINT *id,*jd;
2708:
2709: if ( !d1 ) return d1;
2710: for ( dd = 0, e = d1; e; e = d3 ) {
2711: if ( !(d2 = NEXT(e)) ) {
2712: NEXT(e) = dd;
2713: return e;
2714: }
2715: id = LCM(e);
2716: for ( d3 = 0; d2; d2 = p ) {
2717: p = NEXT(d2);
2718: jd = LCM(d2);
2719: if ( ndl_equal(jd,id) )
2720: ;
2721: else if ( TD(jd) > TD(id) )
2722: if ( ndl_reducible(jd,id) ) continue;
2723: else ;
2724: else if ( ndl_reducible(id,jd) ) goto delit;
2725: NEXT(d2) = d3;
2726: d3 = d2;
2727: }
2728: NEXT(e) = dd;
2729: dd = e;
2730: continue;
2731: /**/
2732: delit: NEXT(d2) = d3;
2733: d3 = d2;
2734: for ( ; p; p = d2 ) {
2735: d2 = NEXT(p);
2736: NEXT(p) = d3;
2737: d3 = p;
2738: }
2739: FREENDP(e);
2740: }
2741: return dd;
2742: }
2743:
2744: ND_pairs crit_F( ND_pairs d1 )
2745: {
2746: ND_pairs rest, head,remove;
2747: ND_pairs last, p, r, w;
2748: int s;
2749:
2750: if ( !d1 ) return d1;
2751: for ( head = last = 0, p = d1; NEXT(p); ) {
2752: r = w = equivalent_pairs(p,&rest);
2753: s = SG(r);
2754: w = NEXT(w);
2755: while ( w ) {
2756: if ( crit_2(w->i1,w->i2) ) {
2757: r = w;
2758: w = NEXT(w);
2759: while ( w ) {
2760: remove = w;
2761: w = NEXT(w);
2762: FREENDP(remove);
2763: }
2764: break;
2765: } else if ( SG(w) < s ) {
2766: FREENDP(r);
2767: r = w;
2768: s = SG(r);
2769: w = NEXT(w);
2770: } else {
2771: remove = w;
2772: w = NEXT(w);
2773: FREENDP(remove);
2774: }
2775: }
2776: if ( last ) NEXT(last) = r;
2777: else head = r;
2778: NEXT(last = r) = 0;
2779: p = rest;
2780: if ( !p ) return head;
2781: }
2782: if ( !last ) return p;
2783: NEXT(last) = p;
2784: return head;
2785: }
2786:
2787: int crit_2( int dp1, int dp2 )
2788: {
2789: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
2790: }
2791:
2792: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
2793: {
2794: ND_pairs w,p,r,s;
2795: UINT *d;
2796:
2797: w = d1;
2798: d = LCM(w);
2799: s = NEXT(w);
2800: NEXT(w) = 0;
2801: for ( r = 0; s; s = p ) {
2802: p = NEXT(s);
2803: if ( ndl_equal(d,LCM(s)) ) {
2804: NEXT(s) = w; w = s;
2805: } else {
2806: NEXT(s) = r; r = s;
2807: }
2808: }
2809: *prest = r;
2810: return w;
2811: }
2812:
2813: NODE update_base(NODE nd,int ndp)
2814: {
2815: UINT *dl, *dln;
2816: NODE last, p, head;
2817:
2818: dl = DL(nd_psh[ndp]);
2819: for ( head = last = 0, p = nd; p; ) {
2820: dln = DL(nd_psh[(long)BDY(p)]);
2821: if ( ndl_reducible( dln, dl ) ) {
2822: p = NEXT(p);
2823: if ( last ) NEXT(last) = p;
2824: } else {
2825: if ( !last ) head = p;
2826: p = NEXT(last = p);
2827: }
2828: }
2829: head = append_one(head,ndp);
2830: return head;
2831: }
2832:
2833: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2834: {
2835: ND_pairs m,ml,p,l;
2836: UINT *lcm;
2837: int s,td,len,tlen,c,c1;
2838:
2839: if ( !(p = NEXT(m = d)) ) {
2840: *prest = p;
2841: NEXT(m) = 0;
2842: return m;
2843: }
2844: if ( !NoSugar ) {
2845: if ( nd_sugarweight ) {
2846: s = m->sugar2;
2847: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2848: if ( (p->sugar2 < s)
2849: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2850: ml = l; m = p; s = m->sugar2;
2851: }
2852: } else {
2853: s = SG(m);
2854: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2855: if ( (SG(p) < s)
2856: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2857: ml = l; m = p; s = SG(m);
2858: }
2859: }
2860: } else {
2861: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2862: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2863: ml = l; m = p; s = SG(m);
2864: }
2865: }
2866: if ( !ml ) *prest = NEXT(m);
2867: else {
2868: NEXT(ml) = NEXT(m);
2869: *prest = d;
2870: }
2871: NEXT(m) = 0;
2872: return m;
2873: }
2874:
2875: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2876: {
2877: int msugar,i;
2878: ND_pairs t,dm0,dm,dr0,dr;
2879:
2880: if ( nd_sugarweight ) {
2881: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2882: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2883: dm0 = 0; dr0 = 0;
2884: for ( i = 0, t = d; t; t = NEXT(t) )
2885: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2886: if ( dm0 ) NEXT(dm) = t;
2887: else dm0 = t;
2888: dm = t;
2889: i++;
2890: } else {
2891: if ( dr0 ) NEXT(dr) = t;
2892: else dr0 = t;
2893: dr = t;
2894: }
2895: } else {
2896: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2897: if ( SG(t) < msugar ) msugar = SG(t);
2898: dm0 = 0; dr0 = 0;
2899: for ( i = 0, t = d; t; t = NEXT(t) )
2900: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2901: if ( dm0 ) NEXT(dm) = t;
2902: else dm0 = t;
2903: dm = t;
2904: i++;
2905: } else {
2906: if ( dr0 ) NEXT(dr) = t;
2907: else dr0 = t;
2908: dr = t;
2909: }
2910: }
2911: NEXT(dm) = 0;
2912: if ( dr0 ) NEXT(dr) = 0;
2913: *prest = dr0;
2914: return dm0;
2915: }
2916:
2917: int nd_tdeg(NDV c)
2918: {
2919: int wmax = 0;
2920: int i,len;
2921: NMV a;
2922:
2923: len = LEN(c);
2924: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2925: wmax = MAX(TD(DL(a)),wmax);
2926: return wmax;
2927: }
2928:
2929: int ndv_newps(int m,NDV a,NDV aq,int f4)
2930: {
2931: int len;
2932: RHist r;
2933: NDV b;
2934: NODE tn;
2935: LIST l;
2936: Z iq;
2937:
2938: if ( nd_psn == nd_pslen ) {
2939: nd_pslen *= 2;
2940: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
2941: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2942: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2943: nd_bound = (UINT **)
2944: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
2945: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2946: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
2947: }
2948: NEWRHist(r); nd_psh[nd_psn] = r;
2949: nd_ps[nd_psn] = a;
2950: if ( aq ) {
2951: nd_ps_trace[nd_psn] = aq;
2952: if ( !m ) {
2953: register_hcf(aq);
2954: } else if ( m == -2 ) {
2955: /* do nothing */
2956: } else
2957: error("ndv_newps : invalud modulus");
2958: nd_bound[nd_psn] = ndv_compute_bound(aq);
2959: #if 1
2960: SG(r) = SG(aq);
2961: #else
2962: SG(r) = nd_tdeg(aq);
2963: #endif
2964: ndl_copy(HDL(aq),DL(r));
2965: } else {
2966: if ( !m ) register_hcf(a);
2967: nd_bound[nd_psn] = ndv_compute_bound(a);
2968: #if 1
2969: SG(r) = SG(a);
2970: #else
2971: SG(r) = nd_tdeg(a);
2972: #endif
2973: ndl_copy(HDL(a),DL(r));
2974: }
2975: if ( nd_demand ) {
2976: if ( aq ) {
2977: ndv_save(nd_ps_trace[nd_psn],nd_psn);
2978: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2979: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2980: nd_ps_trace[nd_psn] = 0;
2981: } else {
2982: ndv_save(nd_ps[nd_psn],nd_psn);
2983: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
2984: nd_ps[nd_psn] = 0;
2985: }
2986: }
2987: if ( nd_gentrace ) {
2988: /* reverse the tracelist and append it to alltracelist */
2989: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
2990: STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
2991: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
2992: }
2993: return nd_psn++;
2994: }
2995:
2996: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
2997: /* return 1 if success, 0 if failure (HC(a mod p)) */
2998:
2999: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
3000: {
3001: int i,j,td,len,max;
3002: NODE s,s0,f0,tn;
3003: UINT *d;
3004: RHist r;
3005: NDVI w;
3006: NDV a,am;
3007: union oNDC hc;
3008: NODE node;
3009: P hcp;
3010: Z iq,jq;
3011: LIST l;
3012:
3013: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3014: /* initialize the tracelist */
3015: nd_tracelist = 0;
3016:
3017: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3018: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3019: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3020: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3021: if ( !dont_sort ) {
3022: /* XXX heuristic */
3023: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
3024: qsort(w,nd_psn,sizeof(struct oNDVI),
3025: (int (*)(const void *,const void *))ndvi_compare_rev);
3026: else
3027: qsort(w,nd_psn,sizeof(struct oNDVI),
3028: (int (*)(const void *,const void *))ndvi_compare);
3029: }
3030: nd_pslen = 2*nd_psn;
3031: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3032: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3033: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3034: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3035: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3036: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3037: nd_hcf = 0;
3038:
3039: if ( trace && nd_vc )
3040: makesubst(nd_vc,&nd_subst);
3041: else
3042: nd_subst = 0;
3043:
3044: if ( !nd_red )
3045: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3046: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3047: for ( i = 0; i < nd_psn; i++ ) {
3048: hc = HCU(w[i].p);
3049: if ( trace ) {
3050: if ( mod == -2 ) {
3051: /* over a large finite field */
3052: /* trace = small modulus */
3053: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3054: ndv_mod(-2,a);
3055: if ( !dont_removecont) ndv_removecont(-2,a);
3056: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3057: ndv_mod(trace,am);
3058: if ( DL_COMPARE(HDL(am),HDL(a)) )
3059: return 0;
3060: ndv_removecont(trace,am);
3061: } else {
3062: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3063: if ( !dont_removecont) ndv_removecont(0,a);
3064: register_hcf(a);
3065: am = nd_ps[i] = ndv_dup(mod,a);
3066: ndv_mod(mod,am);
3067: if ( DL_COMPARE(HDL(am),HDL(a)) )
3068: return 0;
3069: ndv_removecont(mod,am);
3070: }
3071: } else {
3072: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3073: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3074: if ( !mod ) register_hcf(a);
3075: }
3076: if ( nd_gentrace ) {
3077: STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3078: if ( !dont_removecont )
3079: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3080: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3081: }
3082: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3083: nd_bound[i] = ndv_compute_bound(a);
3084: nd_psh[i] = r;
3085: if ( nd_demand ) {
3086: if ( trace ) {
3087: ndv_save(nd_ps_trace[i],i);
3088: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3089: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3090: nd_ps_trace[i] = 0;
3091: } else {
3092: ndv_save(nd_ps[i],i);
3093: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3094: nd_ps[i] = 0;
3095: }
3096: }
3097: }
3098: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3099: return 1;
3100: }
3101:
3102: struct order_spec *append_block(struct order_spec *spec,
3103: int nv,int nalg,int ord);
3104:
3105: extern VECT current_dl_weight_vector_obj;
3106: static VECT prev_weight_vector_obj;
3107:
3108: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3109: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3110: {
3111: NODE alist,t,s,r0,r,arg;
3112: VL tv;
3113: P poly;
3114: DP d;
3115: Alg alpha,dp;
3116: DAlg inv,da,hc;
3117: MP m;
3118: int i,nvar,nalg,n;
3119: NumberField nf;
3120: LIST f1,f2;
3121: struct order_spec *current_spec;
3122: VECT obj,obj0;
3123: VECT tmp;
3124:
3125: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3126: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3127:
3128: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3129: NEXTNODE(alist,t); MKV(tv->v,poly);
3130: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3131: tv->v = tv->v->priv;
3132: }
3133: NEXT(t) = 0;
3134:
3135: /* simplification, making polynomials monic */
3136: setfield_dalg(alist);
3137: obj_algtodalg((Obj)f,(Obj *)&f1);
3138: for ( t = BDY(f); t; t = NEXT(t) ) {
3139: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3140: hc = (DAlg)BDY(d)->c;
3141: if ( NID(hc) == N_DA ) {
3142: invdalg(hc,&inv);
3143: for ( m = BDY(d); m; m = NEXT(m) ) {
3144: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3145: }
3146: }
3147: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3148: }
3149: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3150:
3151: /* append alg vars to the var list */
3152: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3153: NEXT(tv) = av;
3154:
3155: /* append a block to ord */
3156: *ord1p = append_block(ord,nvar,nalg,2);
3157:
3158: /* create generator list */
3159: nf = get_numberfield();
3160: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3161: MKAlg(nf->defpoly[i],dp);
3162: MKNODE(s,dp,t); t = s;
3163: }
3164: MKLIST(f1,t);
3165: *alistp = alist;
3166: algobjtorat((Obj)f1,(Obj *)f1p);
3167:
3168: /* creating a new weight vector */
3169: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3170: n = nvar+nalg+1;
3171: MKVECT(obj,n);
3172: if ( obj0 && obj0->len == nvar )
3173: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3174: else
3175: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3176: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3177: BDY(obj)[n-1] = (pointer)ONE;
3178: arg = mknode(1,obj);
3179: Pdp_set_weight(arg,&tmp);
3180: }
3181:
3182: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3183: {
3184: NODE s,t,u0,u;
3185: P p;
3186: VL tv;
3187: Obj obj;
3188: VECT tmp;
3189: NODE arg;
3190:
3191: u0 = 0;
3192: for ( t = r; t; t = NEXT(t) ) {
3193: p = (P)BDY(t);
3194: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3195: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3196: }
3197: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3198: NEXTNODE(u0,u);
3199: BDY(u) = (pointer)p;
3200: }
3201: }
3202: arg = mknode(1,prev_weight_vector_obj);
3203: Pdp_set_weight(arg,&tmp);
3204:
3205: return u0;
3206: }
3207:
3208: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3209: {
3210: VL tv,fv,vv,vc,av;
3211: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3212: int e,max,nvar,i;
3213: NDV b;
3214: int ishomo,nalg,mrank,trank,wmax,len;
3215: NMV a;
3216: Alg alpha,dp;
3217: P p,zp;
3218: Q dmy;
3219: LIST f1,f2,zpl;
3220: Obj obj;
3221: NumberField nf;
3222: struct order_spec *ord1;
3223: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3224: LIST l1,l2,l3,l4,l5;
3225: int j;
3226: Z jq,bpe,last_nonzero;
3227: int *perm;
3228: EPOS oepos;
3229: int obpe,oadv,ompos,cbpe;
3230:
3231: nd_module = 0;
3232: if ( !m && Demand ) nd_demand = 1;
3233: else nd_demand = 0;
3234: parse_nd_option(current_option);
3235:
3236: if ( DP_Multiple )
3237: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3238: #if 0
3239: ndv_alloc = 0;
3240: #endif
3241: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3242: if ( m && nd_vc )
3243: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3244: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3245: switch ( ord->id ) {
3246: case 1:
3247: if ( ord->nv != nvar )
3248: error("nd_{gr,f4} : invalid order specification");
3249: break;
3250: default:
3251: break;
3252: }
3253: nd_nalg = 0;
3254: av = 0;
3255: if ( !m ) {
3256: get_algtree((Obj)f,&av);
3257: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3258: nd_ntrans = nvar;
3259: nd_nalg = nalg;
3260: /* #i -> t#i */
3261: if ( nalg ) {
3262: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3263: ord = ord1;
3264: f = f1;
3265: }
3266: nvar += nalg;
3267: }
3268: nd_init_ord(ord);
3269: mrank = 0;
3270: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3271: for ( tv = vv; tv; tv = NEXT(tv) ) {
3272: if ( nd_module ) {
3273: s = BDY((LIST)BDY(t));
3274: trank = length(s);
3275: mrank = MAX(mrank,trank);
3276: for ( ; s; s = NEXT(s) ) {
3277: e = getdeg(tv->v,(P)BDY(s));
3278: max = MAX(e,max);
3279: }
3280: } else {
3281: e = getdeg(tv->v,(P)BDY(t));
3282: max = MAX(e,max);
3283: }
3284: }
3285: nd_setup_parameters(nvar,nd_nzlist?0:max);
3286: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3287: ishomo = 1;
3288: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3289: if ( nd_module ) {
3290: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3291: else zpl = (LIST)BDY(t);
3292: b = (pointer)pltondv(CO,vv,zpl);
3293: } else {
3294: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3295: else zp = (P)BDY(t);
3296: b = (pointer)ptondv(CO,vv,zp);
3297: }
3298: if ( ishomo )
3299: ishomo = ishomo && ndv_ishomo(b);
3300: if ( m ) ndv_mod(m,b);
3301: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3302: }
3303: if ( fd0 ) NEXT(fd) = 0;
3304:
3305: if ( !ishomo && homo ) {
3306: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3307: b = (NDV)BDY(t); len = LEN(b);
3308: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3309: wmax = MAX(TD(DL(a)),wmax);
3310: }
3311: homogenize_order(ord,nvar,&ord1);
3312: nd_init_ord(ord1);
3313: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
3314: for ( t = fd0; t; t = NEXT(t) )
3315: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3316: }
3317:
3318: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
3319: if ( nd_gentrace ) {
3320: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3321: }
3322: if ( nd_splist ) {
3323: *rp = compute_splist();
3324: return;
3325: }
3326: if ( nd_check_splist ) {
3327: if ( f4 ) {
3328: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3329: else *rp = 0;
3330: } else {
3331: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3332: else *rp = 0;
3333: }
3334: return;
3335: }
3336: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
3337: if ( !x ) {
3338: *rp = 0; return;
3339: }
3340: if ( !ishomo && homo ) {
3341: /* dehomogenization */
3342: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3343: nd_init_ord(ord);
3344: nd_setup_parameters(nvar,0);
3345: }
3346: nd_demand = 0;
3347: if ( nd_module && nd_intersect ) {
3348: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
3349: if ( MPOS(DL(nd_psh[j])) > 1 ) {
3350: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
3351: }
3352: conv_ilist(nd_demand,0,x,0);
3353: goto FINAL;
3354: }
3355: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
3356: x = ndv_reducebase(x,perm);
3357: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3358: x = ndv_reduceall(m,x);
3359: cbpe = nd_bpe;
3360: if ( nd_gentrace && !f4 ) {
3361: tl2 = nd_alltracelist; nd_alltracelist = 0;
3362: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
3363: tl3 = nd_alltracelist; nd_alltracelist = 0;
3364: if ( nd_gensyz ) {
3365: nd_gb(m,0,1,1,0);
3366: tl4 = nd_alltracelist; nd_alltracelist = 0;
3367: } else tl4 = 0;
3368: }
3369: nd_bpe = cbpe;
3370: nd_setup_parameters(nd_nvar,0);
3371: FINAL:
3372: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3373: NEXTNODE(r0,r);
3374: if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
3375: else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
3376: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
3377: }
3378: if ( r0 ) NEXT(r) = 0;
3379: if ( !m && nd_nalg )
3380: r0 = postprocess_algcoef(av,alist,r0);
3381: MKLIST(*rp,r0);
3382: if ( nd_gentrace ) {
3383: if ( f4 ) {
3384: STOQ(16,bpe);
3385: STOQ(nd_last_nonzero,last_nonzero);
3386: tr = mknode(5,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero); MKLIST(*rp,tr);
3387:
3388: } else {
3389: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3390: tl3 = reverse_node(tl3);
3391: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3392: for ( t = tl2; t; t = NEXT(t) ) {
3393: /* s = [i,[*,j,*,*],...] */
3394: s = BDY((LIST)BDY(t));
3395: j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
3396: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
3397: j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
3398: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3399: }
3400: }
3401: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
3402: STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
3403: }
3404: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3405: MKLIST(l5,tl4);
3406: STOQ(nd_bpe,bpe);
3407: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
3408: }
3409: }
3410: #if 0
3411: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
3412: #endif
3413: }
3414:
3415: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3416: {
3417: VL tv,fv,vv,vc,av;
3418: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3419: int e,max,nvar,i;
3420: NDV b;
3421: int ishomo,nalg;
3422: Alg alpha,dp;
3423: P p,zp;
3424: Q dmy;
3425: LIST f1,f2;
3426: Obj obj;
3427: NumberField nf;
3428: struct order_spec *ord1;
3429: int *perm;
3430:
3431: parse_nd_option(current_option);
3432: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3433: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3434: switch ( ord->id ) {
3435: case 1:
3436: if ( ord->nv != nvar )
3437: error("nd_check : invalid order specification");
3438: break;
3439: default:
3440: break;
3441: }
3442: nd_nalg = 0;
3443: av = 0;
3444: if ( !m ) {
3445: get_algtree((Obj)f,&av);
3446: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3447: nd_ntrans = nvar;
3448: nd_nalg = nalg;
3449: /* #i -> t#i */
3450: if ( nalg ) {
3451: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3452: ord = ord1;
3453: f = f1;
3454: }
3455: nvar += nalg;
3456: }
3457: nd_init_ord(ord);
3458: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3459: for ( tv = vv; tv; tv = NEXT(tv) ) {
3460: e = getdeg(tv->v,(P)BDY(t));
3461: max = MAX(e,max);
3462: }
3463: nd_setup_parameters(nvar,max);
3464: ishomo = 1;
3465: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3466: ptozp((P)BDY(t),1,&dmy,&zp);
3467: b = (pointer)ptondv(CO,vv,zp);
3468: if ( ishomo )
3469: ishomo = ishomo && ndv_ishomo(b);
3470: if ( m ) ndv_mod(m,b);
3471: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3472: }
3473: if ( fd0 ) NEXT(fd) = 0;
3474: ndv_setup(m,0,fd0,0,1);
3475: for ( x = 0, i = 0; i < nd_psn; i++ )
3476: x = update_base(x,i);
3477: if ( do_check ) {
3478: x = nd_gb(m,ishomo,1,0,&perm);
3479: if ( !x ) {
3480: *rp = 0;
3481: return;
3482: }
3483: } else {
3484: #if 0
3485: /* bug ? */
3486: for ( t = x; t; t = NEXT(t) )
3487: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
3488: #else
3489: conv_ilist(0,0,x,&perm);
3490: #endif
3491: }
3492: x = ndv_reducebase(x,perm);
3493: x = ndv_reduceall(m,x);
3494: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3495: NEXTNODE(r0,r);
3496: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3497: }
3498: if ( r0 ) NEXT(r) = 0;
3499: if ( !m && nd_nalg )
3500: r0 = postprocess_algcoef(av,alist,r0);
3501: MKLIST(*rp,r0);
3502: }
3503:
3504: NDV recompute_trace(NODE trace,NDV *p,int m);
3505: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3506:
3507: NDV recompute_trace(NODE ti,NDV *p,int mod)
3508: {
3509: int c,c1,c2,i;
3510: NM mul,m,tail;
3511: ND d,r,rm;
3512: NODE sj;
3513: NDV red;
3514: Obj mj;
3515: static int afo=0;
3516:
3517: afo++;
3518: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3519: CM(mul) = 1;
3520: tail = 0;
3521: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3522: sj = BDY((LIST)BDY(ti));
3523: if ( ARG0(sj) ) {
3524: red = p[QTOS((Q)ARG1(sj))];
3525: mj = (Obj)ARG2(sj);
3526: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3527: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3528: rm = ndv_mul_nm(mod,mul,red);
3529: if ( !r ) r = rm;
3530: else {
3531: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3532: if ( d ) {
3533: NEXT(tail) = m; tail = m; LEN(d)++;
3534: } else {
3535: MKND(nd_nvar,m,1,d); tail = BDY(d);
3536: }
3537: }
3538: if ( !m ) return 0; /* failure */
3539: else {
3540: BDY(r) = m;
3541: if ( mod > 0 || mod == -1 ) {
3542: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3543: DMAR(c1,c2,0,mod,c);
3544: nd_mul_c(mod,rm,c);
3545: } else {
3546: Z t,u;
3547:
3548: chsgnlf(HCZ(r),&t);
3549: divlf(t,HCZ(rm),&u);
3550: nd_mul_c_lf(rm,u);
3551: }
3552: r = nd_add(mod,r,rm);
3553: }
3554: }
3555: }
3556: }
3557: if ( tail ) NEXT(tail) = 0;
3558: d = nd_add(mod,d,r);
3559: nd_mul_c(mod,d,invm(HCM(d),mod));
3560: return ndtondv(mod,d);
3561: }
3562:
3563: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3564: {
3565: VL tv,fv,vv,vc,av;
3566: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3567: int e,max,nvar,i;
3568: NDV b;
3569: int ishomo,nalg;
3570: Alg alpha,dp;
3571: P p,zp;
3572: Q dmy;
3573: LIST f1,f2;
3574: Obj obj;
3575: NumberField nf;
3576: struct order_spec *ord1;
3577: NODE permtrace,intred,ind,perm,trace,ti;
3578: int len,n,j;
3579: NDV *db,*pb;
3580:
3581: parse_nd_option(current_option);
3582: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3583: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3584: switch ( ord->id ) {
3585: case 1:
3586: if ( ord->nv != nvar )
3587: error("nd_check : invalid order specification");
3588: break;
3589: default:
3590: break;
3591: }
3592: nd_init_ord(ord);
3593: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
3594: nd_setup_parameters(nvar,0);
3595:
3596: len = length(BDY(f));
3597: db = (NDV *)MALLOC(len*sizeof(NDV *));
3598: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3599: ptozp((P)BDY(t),1,&dmy,&zp);
3600: b = ptondv(CO,vv,zp);
3601: ndv_mod(m,b);
3602: ndv_mul_c(m,b,invm(HCM(b),m));
3603: db[i] = b;
3604: }
3605:
3606: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3607: intred = BDY((LIST)ARG3(BDY(tlist)));
3608: ind = BDY((LIST)ARG4(BDY(tlist)));
3609: perm = BDY((LIST)ARG0(permtrace));
3610: trace = NEXT(permtrace);
3611:
3612: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
3613: j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
3614: if ( j > i ) i = j;
3615: }
3616: n = i+1;
3617: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3618: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3619: ti = BDY((LIST)BDY(t));
3620: pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
3621: }
3622: for ( t = trace; t; t = NEXT(t) ) {
3623: ti = BDY((LIST)BDY(t));
3624: pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3625: if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
3626: if ( DP_Print ) {
3627: fprintf(asir_out,"."); fflush(asir_out);
3628: }
3629: }
3630: for ( t = intred; t; t = NEXT(t) ) {
3631: ti = BDY((LIST)BDY(t));
3632: pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3633: if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
3634: if ( DP_Print ) {
3635: fprintf(asir_out,"*"); fflush(asir_out);
3636: }
3637: }
3638: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3639: NEXTNODE(r0,r);
3640: b = pb[QTOS((Q)BDY(t))];
3641: ndv_mul_c(m,b,invm(HCM(b),m));
3642: #if 0
3643: BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
3644: #else
3645: BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
3646: #endif
3647: }
3648: if ( r0 ) NEXT(r) = 0;
3649: MKLIST(*rp,r0);
3650: if ( DP_Print ) fprintf(asir_out,"\n");
3651: }
3652:
3653: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
3654: {
3655: VL tv,fv,vv,vc,av;
3656: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3657: int m,nocheck,nvar,mindex,e,max;
3658: NDV c;
3659: NMV a;
3660: P p,zp;
3661: Q dmy;
3662: EPOS oepos;
3663: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
3664: Alg alpha,dp;
3665: P poly;
3666: LIST f1,f2,zpl;
3667: Obj obj;
3668: NumberField nf;
3669: struct order_spec *ord1;
3670: struct oEGT eg_check,eg0,eg1;
3671: NODE tr,tl1,tl2,tl3,tl4;
3672: LIST l1,l2,l3,l4,l5;
3673: int *perm;
3674: int j,ret;
3675: Z jq,bpe;
3676:
3677: nd_module = 0;
3678: nd_lf = 0;
3679: parse_nd_option(current_option);
3680: if ( nd_lf ) {
3681: if ( f4 )
3682: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3683: else
3684: error("nd_gr_trace is not implemented yet over a large finite field");
3685: return;
3686: }
3687: if ( DP_Multiple )
3688: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3689:
3690: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3691: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3692: switch ( ord->id ) {
3693: case 1:
3694: if ( ord->nv != nvar )
3695: error("nd_gr_trace : invalid order specification");
3696: break;
3697: default:
3698: break;
3699: }
3700:
3701: get_algtree((Obj)f,&av);
3702: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3703: nd_ntrans = nvar;
3704: nd_nalg = nalg;
3705: /* #i -> t#i */
3706: if ( nalg ) {
3707: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3708: ord = ord1;
3709: f = f1;
3710: }
3711: nvar += nalg;
3712:
3713: nocheck = 0;
3714: mindex = 0;
3715:
3716: if ( Demand ) nd_demand = 1;
3717: else nd_demand = 0;
3718:
3719: /* setup modulus */
3720: if ( trace < 0 ) {
3721: trace = -trace;
3722: nocheck = 1;
3723: }
3724: m = trace > 1 ? trace : get_lprime(mindex);
3725: nd_init_ord(ord);
3726: mrank = 0;
3727: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3728: for ( tv = vv; tv; tv = NEXT(tv) ) {
3729: if ( nd_module ) {
3730: s = BDY((LIST)BDY(t));
3731: trank = length(s);
3732: mrank = MAX(mrank,trank);
3733: for ( ; s; s = NEXT(s) ) {
3734: e = getdeg(tv->v,(P)BDY(s));
3735: max = MAX(e,max);
3736: }
3737: } else {
3738: e = getdeg(tv->v,(P)BDY(t));
3739: max = MAX(e,max);
3740: }
3741: }
3742: nd_setup_parameters(nvar,max);
3743: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3744: ishomo = 1;
3745: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3746: if ( nd_module ) {
3747: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3748: else zpl = (LIST)BDY(t);
3749: c = (pointer)pltondv(CO,vv,zpl);
3750: } else {
3751: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3752: else zp = (P)BDY(t);
3753: c = (pointer)ptondv(CO,vv,zp);
3754: }
3755: if ( ishomo )
3756: ishomo = ishomo && ndv_ishomo(c);
3757: if ( c ) {
3758: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3759: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3760: }
3761: }
3762: if ( in0 ) NEXT(in) = 0;
3763: if ( fd0 ) NEXT(fd) = 0;
3764: if ( !ishomo && homo ) {
3765: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3766: c = (NDV)BDY(t); len = LEN(c);
3767: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3768: wmax = MAX(TD(DL(a)),wmax);
3769: }
3770: homogenize_order(ord,nvar,&ord1);
3771: nd_init_ord(ord1);
3772: nd_setup_parameters(nvar+1,wmax);
3773: for ( t = fd0; t; t = NEXT(t) )
3774: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3775: }
3776: if ( MaxDeg > 0 ) nocheck = 1;
3777: while ( 1 ) {
3778: tl1 = tl2 = tl3 = tl4 = 0;
3779: if ( Demand )
3780: nd_demand = 1;
3781: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
3782: if ( nd_gentrace ) {
3783: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3784: }
3785: if ( ret )
3786: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3787: if ( !ret || !cand ) {
3788: /* failure */
3789: if ( trace > 1 ) { *rp = 0; return; }
3790: else m = get_lprime(++mindex);
3791: continue;
3792: }
3793: if ( !ishomo && homo ) {
3794: /* dehomogenization */
3795: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3796: nd_init_ord(ord);
3797: nd_setup_parameters(nvar,0);
3798: }
3799: nd_demand = 0;
3800: cand = ndv_reducebase(cand,perm);
3801: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3802: cand = ndv_reduceall(0,cand);
3803: cbpe = nd_bpe;
3804: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
3805: get_eg(&eg0);
3806: if ( nocheck )
3807: break;
3808: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
3809: if ( nd_gentrace ) {
3810: tl3 = nd_alltracelist; nd_alltracelist = 0;
3811: } else tl3 = 0;
3812: /* gbcheck : cand is a GB of Id(cand) ? */
3813: if ( nd_vc || nd_gentrace || nd_gensyz )
3814: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3815: else
3816: ret = nd_f4(0,1,0)!=0;
3817: if ( nd_gentrace && nd_gensyz ) {
3818: tl4 = nd_alltracelist; nd_alltracelist = 0;
3819: } else tl4 = 0;
3820: }
3821: if ( ret ) break;
3822: else if ( trace > 1 ) {
3823: /* failure */
3824: *rp = 0; return;
3825: } else {
3826: /* try the next modulus */
3827: m = get_lprime(++mindex);
3828: /* reset the parameters */
3829: if ( !ishomo && homo ) {
3830: nd_init_ord(ord1);
3831: nd_setup_parameters(nvar+1,wmax);
3832: } else {
3833: nd_init_ord(ord);
3834: nd_setup_parameters(nvar,max);
3835: }
3836: }
3837: }
3838: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3839: if ( DP_Print )
3840: fprintf(asir_out,"check=%.3fsec,",eg_check.exectime+eg_check.gctime);
3841: /* dp->p */
3842: nd_bpe = cbpe;
3843: nd_setup_parameters(nd_nvar,0);
3844: for ( r = cand; r; r = NEXT(r) ) {
3845: if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
3846: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
3847: }
3848: if ( nd_nalg )
3849: cand = postprocess_algcoef(av,alist,cand);
3850: MKLIST(*rp,cand);
3851: if ( nd_gentrace ) {
3852: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3853: tl3 = reverse_node(tl3);
3854: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3855: for ( t = tl2; t; t = NEXT(t) ) {
3856: /* s = [i,[*,j,*,*],...] */
3857: s = BDY((LIST)BDY(t));
3858: j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
3859: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
3860: j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
3861: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3862: }
3863: }
3864: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
3865: STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
3866: }
3867: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3868: MKLIST(l5,tl4);
3869: STOQ(nd_bpe,bpe);
3870: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
3871: }
3872: }
3873:
3874: /* XXX : module element is not considered */
3875:
3876: void dltondl(int n,DL dl,UINT *r)
3877: {
3878: UINT *d;
3879: int i,j,l,s,ord_l;
3880: struct order_pair *op;
3881:
3882: d = (unsigned int *)dl->d;
3883: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3884: if ( nd_blockmask ) {
3885: l = nd_blockmask->n;
3886: op = nd_blockmask->order_pair;
3887: for ( j = 0, s = 0; j < l; j++ ) {
3888: ord_l = op[j].length;
3889: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3890: }
3891: TD(r) = ndl_weight(r);
3892: ndl_weight_mask(r);
3893: } else {
3894: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3895: TD(r) = ndl_weight(r);
3896: }
3897: }
3898:
3899: DL ndltodl(int n,UINT *ndl)
3900: {
3901: DL dl;
3902: int *d;
3903: int i,j,l,s,ord_l;
3904: struct order_pair *op;
3905:
3906: NEWDL(dl,n);
3907: dl->td = TD(ndl);
3908: d = dl->d;
3909: if ( nd_blockmask ) {
3910: l = nd_blockmask->n;
3911: op = nd_blockmask->order_pair;
3912: for ( j = 0, s = 0; j < l; j++ ) {
3913: ord_l = op[j].length;
3914: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3915: }
3916: } else {
3917: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3918: }
3919: return dl;
3920: }
3921:
3922: void nmtodp(int mod,NM m,DP *r)
3923: {
3924: DP dp;
3925: MP mr;
3926:
3927: NEWMP(mr);
3928: mr->dl = ndltodl(nd_nvar,DL(m));
3929: mr->c = (Obj)ndctop(mod,m->c);
3930: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3931: *r = dp;
3932: }
3933:
3934: void ndl_print(UINT *dl)
3935: {
3936: int n;
3937: int i,j,l,ord_l,s,s0;
3938: struct order_pair *op;
3939:
3940: n = nd_nvar;
3941: printf("<<");
3942: if ( nd_blockmask ) {
3943: l = nd_blockmask->n;
3944: op = nd_blockmask->order_pair;
3945: for ( j = 0, s = s0 = 0; j < l; j++ ) {
3946: ord_l = op[j].length;
3947: for ( i = 0; i < ord_l; i++, s++ )
3948: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
3949: }
3950: } else {
3951: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
3952: }
3953: printf(">>");
3954: if ( nd_module && MPOS(dl) )
3955: printf("*e%d",MPOS(dl));
3956: }
3957:
3958: void nd_print(ND p)
3959: {
3960: NM m;
3961:
3962: if ( !p )
3963: printf("0\n");
3964: else {
3965: for ( m = BDY(p); m; m = NEXT(m) ) {
3966: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3967: else printf("+%d*",CM(m));
3968: ndl_print(DL(m));
3969: }
3970: printf("\n");
3971: }
3972: }
3973:
3974: void nd_print_q(ND p)
3975: {
3976: NM m;
3977:
3978: if ( !p )
3979: printf("0\n");
3980: else {
3981: for ( m = BDY(p); m; m = NEXT(m) ) {
3982: printf("+");
3983: printexpr(CO,(Obj)CQ(m));
3984: printf("*");
3985: ndl_print(DL(m));
3986: }
3987: printf("\n");
3988: }
3989: }
3990:
3991: void ndp_print(ND_pairs d)
3992: {
3993: ND_pairs t;
3994:
3995: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
3996: printf("\n");
3997: }
3998:
3999: void nd_removecont(int mod,ND p)
4000: {
4001: int i,n;
4002: Z *w;
4003: NM m;
4004: struct oVECT v;
4005:
4006: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4007: else if ( mod == -2 ) {
4008: Z inv;
4009: divlf(ONE,HCZ(p),&inv);
4010: nd_mul_c_lf(p,inv);
4011: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4012: else {
4013: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4014: w = (Z *)MALLOC(n*sizeof(Q));
4015: v.len = n;
4016: v.body = (pointer *)w;
4017: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
4018: removecont_array((P *)w,n,1);
4019: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
4020: }
4021: }
4022:
4023: void nd_removecont2(ND p1,ND p2)
4024: {
4025: int i,n1,n2,n;
4026: Z *w;
4027: NM m;
4028: struct oVECT v;
4029:
4030: n1 = nd_length(p1);
4031: n2 = nd_length(p2);
4032: n = n1+n2;
4033: w = (Z *)MALLOC(n*sizeof(Q));
4034: v.len = n;
4035: v.body = (pointer *)w;
4036: i = 0;
4037: if ( p1 )
4038: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
4039: if ( p2 )
4040: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
4041: removecont_array((P *)w,n,1);
4042: i = 0;
4043: if ( p1 )
4044: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
4045: if ( p2 )
4046: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
4047: }
4048:
4049: void ndv_removecont(int mod,NDV p)
4050: {
4051: int i,len,all_p;
4052: Z *c;
4053: P *w;
4054: Z dvr,t;
4055: P g,cont,tp;
4056: NMV m;
4057:
4058: if ( mod == -1 )
4059: ndv_mul_c(mod,p,_invsf(HCM(p)));
4060: else if ( mod == -2 ) {
4061: Z inv;
4062: divlf(ONE,HCZ(p),&inv);
4063: ndv_mul_c_lf(p,inv);
4064: } else if ( mod )
4065: ndv_mul_c(mod,p,invm(HCM(p),mod));
4066: else {
4067: len = p->len;
4068: w = (P *)MALLOC(len*sizeof(P));
4069: c = (Z *)MALLOC(len*sizeof(Q));
4070: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4071: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4072: all_p = all_p && !NUM(w[i]);
4073: }
4074: if ( all_p ) {
4075: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4076: mulp(nd_vc,(P)dvr,g,&cont);
4077: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4078: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4079: }
4080: } else {
4081: sortbynm((Q *)c,len);
4082: qltozl((Q *)c,len,&dvr);
4083: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4084: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4085: }
4086: }
4087: }
4088: }
4089:
4090: /* koko */
4091:
4092: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4093: {
4094: int len,i,max;
4095: NMV m,mr0,mr,t;
4096:
4097: len = p->len;
4098: for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
4099: max = MAX(max,TD(DL(m)));
4100: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4101: m = (NMV)((char *)mr0+(len-1)*oadv);
4102: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4103: t = (NMV)MALLOC(nmv_adv);
4104: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4105: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
4106: CQ(mr) = CQ(m);
4107: ndl_copy(DL(t),DL(mr));
4108: }
4109: NV(p)++;
4110: BDY(p) = mr0;
4111: }
4112:
4113: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4114: {
4115: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4116: int pos;
4117: Q *w;
4118: Q dvr,t;
4119: NMV m,r;
4120:
4121: len = p->len;
4122: newnvar = nd_nvar-1;
4123: newexporigin = nd_get_exporigin(ord);
4124: if ( nd_module ) newmpos = newexporigin-1;
4125: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4126: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4127: ndl_dehomogenize(DL(m));
4128: if ( newwpd != nd_wpd ) {
4129: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4130: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
4131: CQ(r) = CQ(m);
4132: if ( nd_module ) pos = MPOS(DL(m));
4133: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4134: adj = nd_exporigin-newexporigin;
4135: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
4136: if ( nd_module ) {
4137: DL(r)[newmpos] = pos;
4138: }
4139: }
4140: }
4141: NV(p)--;
4142: }
4143:
4144: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4145: {
4146: int i;
4147: P *tpl,*tpl1;
4148: NODE l;
4149: P h,gcd,t;
4150:
4151: tpl = (P *)MALLOC(m*sizeof(P));
4152: tpl1 = (P *)MALLOC(m*sizeof(P));
4153: bcopy(pl,tpl,m*sizeof(P));
4154: gcd = (P)ONE;
4155: for ( l = nd_hcf; l; l = NEXT(l) ) {
4156: h = (P)BDY(l);
4157: while ( 1 ) {
4158: for ( i = 0; i < m; i++ )
4159: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4160: break;
4161: if ( i == m ) {
4162: bcopy(tpl1,tpl,m*sizeof(P));
4163: mulp(vl,gcd,h,&t); gcd = t;
4164: } else
4165: break;
4166: }
4167: }
4168: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4169: if ( full ) {
4170: heu_nezgcdnpz(vl,tpl,m,&t);
4171: mulp(vl,gcd,t,pr);
4172: } else
4173: *pr = gcd;
4174: }
4175:
4176: void removecont_array(P *p,int n,int full)
4177: {
4178: int all_p,all_q,i;
4179: Z *c;
4180: P *w;
4181: P t,s;
4182:
4183: for ( all_q = 1, i = 0; i < n; i++ )
4184: all_q = all_q && NUM(p[i]);
4185: if ( all_q ) {
4186: removecont_array_q((Z *)p,n);
4187: } else {
4188: c = (Z *)MALLOC(n*sizeof(Z));
4189: w = (P *)MALLOC(n*sizeof(P));
4190: for ( i = 0; i < n; i++ ) {
4191: ptozp(p[i],1,(Q *)&c[i],&w[i]);
4192: }
4193: removecont_array_q(c,n);
4194: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4195: for ( i = 0; i < n; i++ ) {
4196: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4197: }
4198: }
4199: }
4200:
4201: /* c is an int array */
4202:
4203: void removecont_array_q(Z *c,int n)
4204: {
4205: struct oVECT v;
4206: Z d0,d1,a,u,u1,gcd;
4207: int i,j;
4208: Z *q,*r;
4209:
4210: q = (Z *)MALLOC(n*sizeof(Z));
4211: r = (Z *)MALLOC(n*sizeof(Z));
4212: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4213: gcdvz_estimate(&v,&d0);
4214: for ( i = 0; i < n; i++ ) {
4215: divqrz(c[i],d0,&q[i],&r[i]);
4216: }
4217: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4218: if ( i < n ) {
4219: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4220: gcdvz(&v,&d1);
4221: gcdz(d0,d1,&gcd);
4222: divz(d0,gcd,&a);
4223: for ( i = 0; i < n; i++ ) {
4224: mulz(a,q[i],&u);
4225: if ( r[i] ) {
4226: divz(r[i],gcd,&u1);
4227: addz(u,u1,&q[i]);
4228: } else
4229: q[i] = u;
4230: }
4231: }
4232: for ( i = 0; i < n; i++ ) c[i] = q[i];
4233: }
4234:
4235: void nd_mul_c(int mod,ND p,int mul)
4236: {
4237: NM m;
4238: int c,c1;
4239:
4240: if ( !p ) return;
4241: if ( mul == 1 ) return;
4242: if ( mod == -1 )
4243: for ( m = BDY(p); m; m = NEXT(m) )
4244: CM(m) = _mulsf(CM(m),mul);
4245: else
4246: for ( m = BDY(p); m; m = NEXT(m) ) {
4247: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4248: }
4249: }
4250:
4251: void nd_mul_c_lf(ND p,Z mul)
4252: {
4253: NM m;
4254: Z c;
4255:
4256: if ( !p ) return;
4257: if ( UNIZ(mul) ) return;
4258: for ( m = BDY(p); m; m = NEXT(m) ) {
4259: mullf(CZ(m),mul,&c); CZ(m) = c;
4260: }
4261: }
4262:
4263: void nd_mul_c_q(ND p,P mul)
4264: {
4265: NM m;
4266: P c;
4267:
4268: if ( !p ) return;
4269: if ( UNIQ(mul) ) return;
4270: for ( m = BDY(p); m; m = NEXT(m) ) {
4271: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4272: }
4273: }
4274:
4275: void nd_mul_c_p(VL vl,ND p,P mul)
4276: {
4277: NM m;
4278: P c;
4279:
4280: if ( !p ) return;
4281: for ( m = BDY(p); m; m = NEXT(m) ) {
4282: mulp(vl,CP(m),mul,&c); CP(m) = c;
4283: }
4284: }
4285:
4286: void nd_free(ND p)
4287: {
4288: NM t,s;
4289:
4290: if ( !p ) return;
4291: t = BDY(p);
4292: while ( t ) {
4293: s = NEXT(t);
4294: FREENM(t);
4295: t = s;
4296: }
4297: FREEND(p);
4298: }
4299:
4300: void ndv_free(NDV p)
4301: {
4302: GCFREE(BDY(p));
4303: }
4304:
4305: void nd_append_red(UINT *d,int i)
4306: {
4307: RHist m,m0;
4308: int h;
4309:
4310: NEWRHist(m);
4311: h = ndl_hash_value(d);
4312: m->index = i;
4313: ndl_copy(d,DL(m));
4314: NEXT(m) = nd_red[h];
4315: nd_red[h] = m;
4316: }
4317:
4318: UINT *ndv_compute_bound(NDV p)
4319: {
4320: UINT *d1,*d2,*t;
4321: UINT u;
4322: int i,j,k,l,len,ind;
4323: NMV m;
4324:
4325: if ( !p )
4326: return 0;
4327: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4328: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4329: len = LEN(p);
4330: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4331: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4332: ndl_max(DL(m),d1,d2);
4333: t = d1; d1 = d2; d2 = t;
4334: }
4335: l = nd_nvar+31;
4336: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4337: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4338: u = d1[i];
4339: k = (nd_epw-1)*nd_bpe;
4340: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4341: t[ind] = (u>>k)&nd_mask0;
4342: }
4343: for ( ; ind < l; ind++ ) t[ind] = 0;
4344: return t;
4345: }
4346:
4347: UINT *nd_compute_bound(ND p)
4348: {
4349: UINT *d1,*d2,*t;
4350: UINT u;
4351: int i,j,k,l,len,ind;
4352: NM m;
4353:
4354: if ( !p )
4355: return 0;
4356: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4357: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4358: len = LEN(p);
4359: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4360: for ( m = NEXT(m); m; m = NEXT(m) ) {
4361: ndl_lcm(DL(m),d1,d2);
4362: t = d1; d1 = d2; d2 = t;
4363: }
4364: l = nd_nvar+31;
4365: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4366: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4367: u = d1[i];
4368: k = (nd_epw-1)*nd_bpe;
4369: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4370: t[ind] = (u>>k)&nd_mask0;
4371: }
4372: for ( ; ind < l; ind++ ) t[ind] = 0;
4373: return t;
4374: }
4375:
4376: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4377: /* of a term. In this case we need additional 1 word. */
4378:
4379: int nd_get_exporigin(struct order_spec *ord)
4380: {
4381: switch ( ord->id ) {
4382: case 0: case 2: case 256: case 258:
4383: return 1+nd_module;
4384: case 1: case 257:
4385: /* block order */
4386: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4387: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4388: return ord->ord.block.length+1+nd_module;
4389: case 3: case 259:
4390: #if 0
4391: error("nd_get_exporigin : composite order is not supported yet.");
4392: #else
4393: return 1+nd_module;
4394: #endif
4395: default:
4396: error("nd_get_exporigin : ivalid argument.");
4397: return 0;
4398: }
4399: }
4400:
4401: void nd_setup_parameters(int nvar,int max) {
4402: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4403: struct order_pair *op;
4404:
4405: nd_nvar = nvar;
4406: if ( max ) {
4407: /* XXX */
4408: if ( do_weyl ) nd_bpe = 32;
4409: else if ( max < 2 ) nd_bpe = 1;
4410: else if ( max < 4 ) nd_bpe = 2;
4411: else if ( max < 8 ) nd_bpe = 3;
4412: else if ( max < 16 ) nd_bpe = 4;
4413: else if ( max < 32 ) nd_bpe = 5;
4414: else if ( max < 64 ) nd_bpe = 6;
4415: else if ( max < 256 ) nd_bpe = 8;
4416: else if ( max < 1024 ) nd_bpe = 10;
4417: else if ( max < 65536 ) nd_bpe = 16;
4418: else nd_bpe = 32;
4419: }
4420: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4421: UINT t;
4422: int st;
4423: int *v;
4424: /* t = max(weights) */
4425: t = 0;
4426: if ( current_dl_weight_vector )
4427: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4428: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4429: if ( t < st ) t = st;
4430: }
4431: if ( nd_matrix )
4432: for ( i = 0; i < nd_matrix_len; i++ )
4433: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4434: if ( (st=v[j]) < 0 ) st = -st;
4435: if ( t < st ) t = st;
4436: }
4437: /* i = bitsize of t */
4438: for ( i = 0; t; t >>=1, i++ );
4439: /* i += bitsize of nd_nvar */
4440: for ( t = nd_nvar; t; t >>=1, i++);
4441: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4442: if ( (nd_bpe+i) >= 31 )
4443: error("nd_setup_parameters : too large weight");
4444: }
4445: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4446: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4447: nd_exporigin = nd_get_exporigin(nd_ord);
4448: wpd = nd_exporigin+elen;
4449: if ( nd_module )
4450: nd_mpos = nd_exporigin-1;
4451: else
4452: nd_mpos = -1;
4453: if ( wpd != nd_wpd ) {
4454: nd_free_private_storage();
4455: nd_wpd = wpd;
4456: }
4457: if ( nd_bpe < 32 ) {
4458: nd_mask0 = (1<<nd_bpe)-1;
4459: } else {
4460: nd_mask0 = 0xffffffff;
4461: }
4462: bzero(nd_mask,sizeof(nd_mask));
4463: nd_mask1 = 0;
4464: for ( i = 0; i < nd_epw; i++ ) {
4465: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4466: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4467: }
4468: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4469: nd_epos = nd_create_epos(nd_ord);
4470: nd_blockmask = nd_create_blockmask(nd_ord);
4471: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4472: }
4473:
4474: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4475: {
4476: int i,obpe,oadv,h;
4477: static NM prev_nm_free_list;
4478: static ND_pairs prev_ndp_free_list;
4479: RHist mr0,mr;
4480: RHist r;
4481: RHist *old_red;
4482: ND_pairs s0,s,t;
4483: EPOS oepos;
4484:
4485: obpe = nd_bpe;
4486: oadv = nmv_adv;
4487: oepos = nd_epos;
4488: if ( obpe < 2 ) nd_bpe = 2;
4489: else if ( obpe < 3 ) nd_bpe = 3;
4490: else if ( obpe < 4 ) nd_bpe = 4;
4491: else if ( obpe < 5 ) nd_bpe = 5;
4492: else if ( obpe < 6 ) nd_bpe = 6;
4493: else if ( obpe < 8 ) nd_bpe = 8;
4494: else if ( obpe < 10 ) nd_bpe = 10;
4495: else if ( obpe < 16 ) nd_bpe = 16;
4496: else if ( obpe < 32 ) nd_bpe = 32;
4497: else error("nd_reconstruct : exponent too large");
4498:
4499: nd_setup_parameters(nd_nvar,0);
4500: prev_nm_free_list = _nm_free_list;
4501: prev_ndp_free_list = _ndp_free_list;
4502: _nm_free_list = 0;
4503: _ndp_free_list = 0;
4504: for ( i = nd_psn-1; i >= 0; i-- ) {
4505: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4506: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4507: }
4508: if ( trace )
4509: for ( i = nd_psn-1; i >= 0; i-- ) {
4510: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4511: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4512: }
4513: s0 = 0;
4514: for ( t = d; t; t = NEXT(t) ) {
4515: NEXTND_pairs(s0,s);
4516: s->i1 = t->i1;
4517: s->i2 = t->i2;
4518: SG(s) = SG(t);
4519: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4520: }
4521:
4522: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4523: for ( i = 0; i < REDTAB_LEN; i++ ) {
4524: old_red[i] = nd_red[i];
4525: nd_red[i] = 0;
4526: }
4527: for ( i = 0; i < REDTAB_LEN; i++ )
4528: for ( r = old_red[i]; r; r = NEXT(r) ) {
4529: NEWRHist(mr);
4530: mr->index = r->index;
4531: SG(mr) = SG(r);
4532: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4533: h = ndl_hash_value(DL(mr));
4534: NEXT(mr) = nd_red[h];
4535: nd_red[h] = mr;
4536: }
4537: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4538: old_red = 0;
4539: for ( i = 0; i < nd_psn; i++ ) {
4540: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4541: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4542: nd_psh[i] = r;
4543: }
4544: if ( s0 ) NEXT(s) = 0;
4545: prev_nm_free_list = 0;
4546: prev_ndp_free_list = 0;
4547: #if 0
4548: GC_gcollect();
4549: #endif
4550: return s0;
4551: }
4552:
4553: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4554: {
4555: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4556: struct order_pair *op;
4557:
4558: n = nd_nvar;
4559: oepw = (sizeof(UINT)*8)/obpe;
4560: omask0 = (1<<obpe)-1;
4561: TD(r) = TD(d);
4562: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4563: if ( nd_blockmask ) {
4564: l = nd_blockmask->n;
4565: op = nd_blockmask->order_pair;
4566: for ( i = 1; i < nd_exporigin; i++ )
4567: r[i] = d[i];
4568: for ( j = 0, s = 0; j < l; j++ ) {
4569: ord_l = op[j].length;
4570: for ( i = 0; i < ord_l; i++, s++ ) {
4571: ei = GET_EXP_OLD(d,s);
4572: PUT_EXP(r,s,ei);
4573: }
4574: }
4575: } else {
4576: for ( i = 0; i < n; i++ ) {
4577: ei = GET_EXP_OLD(d,i);
4578: PUT_EXP(r,i,ei);
4579: }
4580: }
4581: if ( nd_module ) MPOS(r) = MPOS(d);
4582: }
4583:
4584: ND nd_copy(ND p)
4585: {
4586: NM m,mr,mr0;
4587: int c,n;
4588: ND r;
4589:
4590: if ( !p )
4591: return 0;
4592: else {
4593: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4594: NEXTNM(mr0,mr);
4595: CM(mr) = CM(m);
4596: ndl_copy(DL(m),DL(mr));
4597: }
4598: NEXT(mr) = 0;
4599: MKND(NV(p),mr0,LEN(p),r);
4600: SG(r) = SG(p);
4601: return r;
4602: }
4603: }
4604:
4605: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4606: {
4607: NM m1,m2;
4608: NDV p1,p2;
4609: ND t1,t2;
4610: UINT *lcm;
4611: P gp,tp;
4612: Z g,t;
4613: Z iq;
4614: int td;
4615: LIST hist;
4616: NODE node;
4617: DP d;
4618:
4619: if ( !mod && nd_demand ) {
4620: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4621: } else {
4622: if ( trace ) {
4623: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4624: } else {
4625: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4626: }
4627: }
4628: lcm = LCM(p);
4629: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4630: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4631: FREENM(m1); return 0;
4632: }
4633: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4634: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4635: FREENM(m1); FREENM(m2); return 0;
4636: }
4637:
4638: if ( mod == -1 ) {
4639: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4640: } else if ( mod > 0 ) {
4641: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4642: } else if ( mod == -2 ) {
4643: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4644: } else if ( nd_vc ) {
4645: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4646: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4647: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4648: } else {
4649: igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnz(t,&CQ(m2));
4650: }
4651: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4652: *rp = nd_add(mod,t1,t2);
4653: if ( nd_gentrace ) {
4654: /* nd_tracelist is initialized */
4655: STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
4656: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
4657: STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
4658: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4659: nd_tracelist = node;
4660: }
4661: FREENM(m1); FREENM(m2);
4662: return 1;
4663: }
4664:
4665: void ndv_mul_c(int mod,NDV p,int mul)
4666: {
4667: NMV m;
4668: int c,c1,len,i;
4669:
4670: if ( !p ) return;
4671: len = LEN(p);
4672: if ( mod == -1 )
4673: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4674: CM(m) = _mulsf(CM(m),mul);
4675: else
4676: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4677: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4678: }
4679: }
4680:
4681: void ndv_mul_c_lf(NDV p,Z mul)
4682: {
4683: NMV m;
4684: Z c;
4685: int len,i;
4686:
4687: if ( !p ) return;
4688: len = LEN(p);
4689: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4690: mullf(CZ(m),mul,&c); CZ(m) = c;
4691: }
4692: }
4693:
4694: /* for nd_det */
4695: void ndv_mul_c_q(NDV p,Z mul)
4696: {
4697: NMV m;
4698: Z c;
4699: int len,i;
4700:
4701: if ( !p ) return;
4702: len = LEN(p);
4703: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4704: mulz(CQ(m),mul,&c); CQ(m) = c;
4705: }
4706: }
4707:
4708: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4709: int n2,i,j,l,n,tlen;
4710: UINT *d0;
4711: NM *tab,*psum;
4712: ND s,r;
4713: NM t;
4714: NMV m1;
4715:
4716: if ( !p ) return 0;
4717: n = NV(p); n2 = n>>1;
4718: d0 = DL(m0);
4719: l = LEN(p);
4720: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4721: tab = (NM *)MALLOC(tlen*sizeof(NM));
4722: psum = (NM *)MALLOC(tlen*sizeof(NM));
4723: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4724: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4725: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4726: /* m0(NM) * m1(NMV) => tab(NM) */
4727: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4728: for ( j = 0; j < tlen; j++ ) {
4729: if ( tab[j] ) {
4730: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4731: }
4732: }
4733: }
4734: for ( i = tlen-1, r = 0; i >= 0; i-- )
4735: if ( psum[i] ) {
4736: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4737: MKND(n,psum[i],j,s);
4738: r = nd_add(mod,r,s);
4739: }
4740: if ( r ) SG(r) = SG(p)+TD(d0);
4741: return r;
4742: }
4743:
4744: /* product of monomials */
4745: /* XXX block order is not handled correctly */
4746:
4747: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4748: {
4749: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4750: UINT *d0,*d1,*d,*dt,*ctab;
4751: Z *ctab_q;
4752: Z q,q1;
4753: UINT c0,c1,c;
4754: NM *p;
4755: NM m,t;
4756: int mpos;
4757:
4758: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4759: if ( !m0 || !m1 ) return;
4760: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4761: if ( nd_module )
4762: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4763:
4764: NEWNM(m); d = DL(m);
4765: if ( mod ) {
4766: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4767: } else if ( nd_vc )
4768: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4769: else
4770: mulz(CQ(m0),CQ(m1),&CQ(m));
4771: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4772: homo = n&1 ? 1 : 0;
4773: if ( homo ) {
4774: /* offset of h-degree */
4775: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4776: PUT_EXP(DL(m),n-1,h);
4777: TD(DL(m)) = h;
4778: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4779: }
4780: tab[0] = m;
4781: NEWNM(m); d = DL(m);
4782: for ( i = 0, curlen = 1; i < n2; i++ ) {
4783: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4784: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4785: /* xi^a*(Di^k*xi^l)*Di^b */
4786: a += l; b += k;
4787: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4788: if ( !k || !l ) {
4789: for ( j = 0; j < curlen; j++ )
4790: if ( (t = tab[j]) != 0 ) {
4791: dt = DL(t);
4792: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4793: if ( nd_blockmask ) ndl_weight_mask(dt);
4794: }
4795: curlen *= k+1;
4796: continue;
4797: }
4798: min = MIN(k,l);
4799: if ( mod ) {
4800: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4801: mkwcm(k,l,mod,(int *)ctab);
4802: } else {
4803: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4804: mkwc(k,l,ctab_q);
4805: }
4806: for ( j = min; j >= 0; j-- ) {
4807: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4808: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4809: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4810: if ( homo ) {
4811: TD(d) = s;
4812: PUT_EXP(d,n-1,s-h);
4813: } else TD(d) = h;
4814: if ( nd_blockmask ) ndl_weight_mask(d);
4815: if ( mod ) c = ctab[j];
4816: else q = ctab_q[j];
4817: p = tab+curlen*j;
4818: if ( j == 0 ) {
4819: for ( u = 0; u < curlen; u++, p++ ) {
4820: if ( tab[u] ) {
4821: ndl_addto(DL(tab[u]),d);
4822: if ( mod ) {
4823: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4824: } else if ( nd_vc )
4825: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4826: else {
4827: mulz(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
4828: }
4829: }
4830: }
4831: } else {
4832: for ( u = 0; u < curlen; u++, p++ ) {
4833: if ( tab[u] ) {
4834: NEWNM(t);
4835: ndl_add(DL(tab[u]),d,DL(t));
4836: if ( mod ) {
4837: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4838: } else if ( nd_vc )
4839: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4840: else
4841: mulz(CQ(tab[u]),q,&CQ(t));
4842: *p = t;
4843: }
4844: }
4845: }
4846: }
4847: curlen *= k+1;
4848: }
4849: FREENM(m);
4850: if ( nd_module ) {
4851: mpos = MPOS(d1);
4852: for ( i = 0; i < tlen; i++ )
4853: if ( tab[i] ) {
4854: d = DL(tab[i]);
4855: MPOS(d) = mpos;
4856: TD(d) = ndl_weight(d);
4857: }
4858: }
4859: }
4860:
4861: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4862: {
4863: NM mr,mr0;
4864: NMV m;
4865: UINT *d,*dt,*dm;
4866: int c,n,td,i,c1,c2,len;
4867: Q q;
4868: ND r;
4869:
4870: if ( !p ) return 0;
4871: else {
4872: n = NV(p); m = BDY(p);
4873: d = DL(m0);
4874: len = LEN(p);
4875: mr0 = 0;
4876: td = TD(d);
4877: c = CM(m0);
4878: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4879: NEXTNM(mr0,mr);
4880: CM(mr) = 1;
4881: ndl_add(DL(m),d,DL(mr));
4882: }
4883: NEXT(mr) = 0;
4884: MKND(NV(p),mr0,len,r);
4885: SG(r) = SG(p) + TD(d);
4886: return r;
4887: }
4888: }
4889:
4890: ND ndv_mul_nm(int mod,NM m0,NDV p)
4891: {
4892: NM mr,mr0;
4893: NMV m;
4894: UINT *d,*dt,*dm;
4895: int c,n,td,i,c1,c2,len;
4896: P q;
4897: ND r;
4898:
4899: if ( !p ) return 0;
4900: else if ( do_weyl ) {
4901: if ( mod < 0 ) {
4902: error("ndv_mul_nm : not implemented (weyl)");
4903: return 0;
4904: } else
4905: return weyl_ndv_mul_nm(mod,m0,p);
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: if ( mod == -1 ) {
4913: c = CM(m0);
4914: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4915: NEXTNM(mr0,mr);
4916: CM(mr) = _mulsf(CM(m),c);
4917: ndl_add(DL(m),d,DL(mr));
4918: }
4919: } else if ( mod == -2 ) {
4920: Z cl;
4921: cl = CZ(m0);
4922: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4923: NEXTNM(mr0,mr);
4924: mullf(CZ(m),cl,&CZ(mr));
4925: ndl_add(DL(m),d,DL(mr));
4926: }
4927: } else if ( mod ) {
4928: c = CM(m0);
4929: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4930: NEXTNM(mr0,mr);
4931: c1 = CM(m);
4932: DMAR(c1,c,0,mod,c2);
4933: CM(mr) = c2;
4934: ndl_add(DL(m),d,DL(mr));
4935: }
4936: } else {
4937: q = CP(m0);
4938: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4939: NEXTNM(mr0,mr);
4940: mulp(nd_vc,CP(m),q,&CP(mr));
4941: ndl_add(DL(m),d,DL(mr));
4942: }
4943: }
4944: NEXT(mr) = 0;
4945: MKND(NV(p),mr0,len,r);
4946: SG(r) = SG(p) + TD(d);
4947: return r;
4948: }
4949: }
4950:
4951: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
4952: {
4953: NM mq0,mq;
4954: NMV tm;
4955: Q q;
4956: int i,nv,sg,c,c1,c2,hindex;
4957: ND p,t,r;
4958:
4959: if ( bucket->m < 0 ) return 0;
4960: else {
4961: nv = NV(d);
4962: mq0 = 0;
4963: tm = (NMV)MALLOC(nmv_adv);
4964: while ( 1 ) {
4965: if ( mod > 0 || mod == -1 )
4966: hindex = head_pbucket(mod,bucket);
4967: else if ( mod == -2 )
4968: hindex = head_pbucket_lf(bucket);
4969: else
4970: hindex = head_pbucket_q(bucket);
4971: if ( hindex < 0 ) break;
4972: p = bucket->body[hindex];
4973: NEXTNM(mq0,mq);
4974: ndl_sub(HDL(p),HDL(d),DL(mq));
4975: ndl_copy(DL(mq),DL(tm));
4976: if ( mod ) {
4977: c1 = invm(HCM(d),mod); c2 = HCM(p);
4978: DMAR(c1,c2,0,mod,c); CM(mq) = c;
4979: CM(tm) = mod-c;
4980: } else {
4981: divsz(HCQ(p),HCQ(d),&CQ(mq));
4982: chsgnz(CQ(mq),&CQ(tm));
4983: }
4984: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
4985: bucket->body[hindex] = nd_remove_head(p);
4986: t = nd_remove_head(t);
4987: add_pbucket(mod,bucket,t);
4988: }
4989: if ( !mq0 )
4990: r = 0;
4991: else {
4992: NEXT(mq) = 0;
4993: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
4994: MKND(nv,mq0,i,r);
4995: /* XXX */
4996: SG(r) = HTD(r);
4997: }
4998: return r;
4999: }
5000: }
5001:
5002: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5003: {
5004: NMV m,mr,mr0,t;
5005: int len,i,k;
5006:
5007: if ( !p ) return;
5008: m = BDY(p); len = LEN(p);
5009: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5010: m = (NMV)((char *)mr0+(len-1)*oadv);
5011: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5012: t = (NMV)MALLOC(nmv_adv);
5013: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5014: CQ(t) = CQ(m);
5015: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5016: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
5017: CQ(mr) = CQ(t);
5018: ndl_copy(DL(t),DL(mr));
5019: }
5020: BDY(p) = mr0;
5021: }
5022:
5023: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5024: {
5025: NMV m,mr,mr0;
5026: int len,i;
5027: NDV r;
5028:
5029: if ( !p ) return 0;
5030: m = BDY(p); len = LEN(p);
5031: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5032: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5033: ndl_zero(DL(mr));
5034: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
5035: CQ(mr) = CQ(m);
5036: }
5037: MKNDV(NV(p),mr0,len,r);
5038: SG(r) = SG(p);
5039: return r;
5040: }
5041:
5042: /* duplicate p */
5043:
5044: NDV ndv_dup(int mod,NDV p)
5045: {
5046: NDV d;
5047: NMV t,m,m0;
5048: int i,len;
5049:
5050: if ( !p ) return 0;
5051: len = LEN(p);
5052: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5053: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5054: ndl_copy(DL(t),DL(m));
5055: CQ(m) = CQ(t);
5056: }
5057: MKNDV(NV(p),m0,len,d);
5058: SG(d) = SG(p);
5059: return d;
5060: }
5061:
5062: NDV ndv_symbolic(int mod,NDV p)
5063: {
5064: NDV d;
5065: NMV t,m,m0;
5066: int i,len;
5067:
5068: if ( !p ) return 0;
5069: len = LEN(p);
5070: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5071: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5072: ndl_copy(DL(t),DL(m));
5073: CQ(m) = ONE;
5074: }
5075: MKNDV(NV(p),m0,len,d);
5076: SG(d) = SG(p);
5077: return d;
5078: }
5079:
5080: ND nd_dup(ND p)
5081: {
5082: ND d;
5083: NM t,m,m0;
5084:
5085: if ( !p ) return 0;
5086: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5087: NEXTNM(m0,m);
5088: ndl_copy(DL(t),DL(m));
5089: CQ(m) = CQ(t);
5090: }
5091: if ( m0 ) NEXT(m) = 0;
5092: MKND(NV(p),m0,LEN(p),d);
5093: SG(d) = SG(p);
5094: return d;
5095: }
5096:
5097: /* XXX if p->len == 0 then it represents 0 */
5098:
5099: void ndv_mod(int mod,NDV p)
5100: {
5101: NMV t,d;
5102: int r,s,u;
5103: int i,len,dlen;
5104: P cp;
5105: Z c;
5106: Obj gfs;
5107:
5108: if ( !p ) return;
5109: len = LEN(p);
5110: dlen = 0;
5111: if ( mod == -1 )
5112: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5113: simp_ff((Obj)CP(t),&gfs);
5114: if ( gfs ) {
5115: r = FTOIF(CONT((GFS)gfs));
5116: CM(d) = r;
5117: ndl_copy(DL(t),DL(d));
5118: NMV_ADV(d);
5119: dlen++;
5120: }
5121: }
5122: else if ( mod == -2 )
5123: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5124: simp_ff((Obj)CP(t),&gfs);
5125: if ( gfs ) {
5126: lmtolf((LM)gfs,&CZ(d));
5127: ndl_copy(DL(t),DL(d));
5128: NMV_ADV(d);
5129: dlen++;
5130: }
5131: }
5132: else
5133: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5134: if ( nd_vc ) {
5135: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5136: c = (Z)cp;
5137: } else
5138: c = CQ(t);
5139: r = remqi((Q)c,mod);
5140: if ( r ) {
5141: CM(d) = r;
5142: ndl_copy(DL(t),DL(d));
5143: NMV_ADV(d);
5144: dlen++;
5145: }
5146: }
5147: LEN(p) = dlen;
5148: }
5149:
5150: NDV ptondv(VL vl,VL dvl,P p)
5151: {
5152: ND nd;
5153:
5154: nd = ptond(vl,dvl,p);
5155: return ndtondv(0,nd);
5156: }
5157:
5158: void pltozpl(LIST l,Q *cont,LIST *pp)
5159: {
5160: NODE nd,nd1;
5161: int n;
5162: P *pl;
5163: Q *cl;
5164: int i;
5165: P dmy;
5166: Z dvr;
5167: LIST r;
5168:
5169: nd = BDY(l); n = length(nd);
5170: pl = (P *)MALLOC(n*sizeof(P));
5171: cl = (Q *)MALLOC(n*sizeof(P));
5172: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5173: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5174: qltozl(cl,n,&dvr);
5175: nd = BDY(l);
5176: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5177: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5178: }
5179: nd = 0;
5180: for ( i = n-1; i >= 0; i-- ) {
5181: MKNODE(nd1,pl[i],nd); nd = nd1;
5182: }
5183: MKLIST(r,nd);
5184: *pp = r;
5185: }
5186:
5187: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5188:
5189: NDV pltondv(VL vl,VL dvl,LIST p)
5190: {
5191: int i;
5192: NODE t;
5193: ND r,ri;
5194: NM m;
5195:
5196: if ( !nd_module ) error("pltond : module order must be set");
5197: r = 0;
5198: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5199: ri = ptond(vl,dvl,(P)BDY(t));
5200: if ( ri )
5201: for ( m = BDY(ri); m; m = NEXT(m) ) {
5202: MPOS(DL(m)) = i;
5203: TD(DL(m)) = ndl_weight(DL(m));
5204: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5205: }
5206: r = nd_add(0,r,ri);
5207: }
5208: return ndtondv(0,r);
5209: }
5210:
5211: ND ptond(VL vl,VL dvl,P p)
5212: {
5213: int n,i,j,k,e;
5214: VL tvl;
5215: V v;
5216: DCP dc;
5217: DCP *w;
5218: ND r,s,t,u;
5219: P x;
5220: int c;
5221: UINT *d;
5222: NM m,m0;
5223:
5224: if ( !p )
5225: return 0;
5226: else if ( NUM(p) ) {
5227: NEWNM(m);
5228: ndl_zero(DL(m));
5229: if ( !INT((Q)p) )
5230: error("ptond : input must be integer-coefficient");
5231: CQ(m) = (Z)p;
5232: NEXT(m) = 0;
5233: MKND(nd_nvar,m,1,r);
5234: SG(r) = 0;
5235: return r;
5236: } else {
5237: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5238: w = (DCP *)MALLOC(k*sizeof(DCP));
5239: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5240: for ( i = 0, tvl = dvl, v = VR(p);
5241: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5242: if ( !tvl ) {
5243: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5244: t = ptond(vl,dvl,COEF(w[j]));
5245: pwrp(vl,x,DEG(w[j]),&p);
5246: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5247: }
5248: return s;
5249: } else {
5250: NEWNM(m0); d = DL(m0);
5251: for ( j = k-1, s = 0; j >= 0; j-- ) {
5252: ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
5253: TD(d) = MUL_WEIGHT(e,i);
5254: if ( nd_blockmask) ndl_weight_mask(d);
5255: if ( nd_module ) MPOS(d) = 0;
5256: t = ptond(vl,dvl,COEF(w[j]));
5257: for ( m = BDY(t); m; m = NEXT(m) )
5258: ndl_addto(DL(m),d);
5259: SG(t) += TD(d);
5260: s = nd_add(0,s,t);
5261: }
5262: FREENM(m0);
5263: return s;
5264: }
5265: }
5266: }
5267:
5268: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5269: {
5270: VL tvl;
5271: int len,n,j,i,e;
5272: NMV m;
5273: Z q;
5274: P c;
5275: UINT *d;
5276: P s,r,u,t,w;
5277: GFS gfs;
5278:
5279: if ( !p ) return 0;
5280: else {
5281: len = LEN(p);
5282: n = NV(p);
5283: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5284: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5285: if ( mod == -1 ) {
5286: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5287: } else if ( mod == -2 ) {
5288: c = (P)CZ(m);
5289: } else if ( mod > 0 ) {
5290: STOQ(CM(m),q); c = (P)q;
5291: } else
5292: c = CP(m);
5293: d = DL(m);
5294: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
5295: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
5296: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5297: }
5298: addp(vl,s,t,&u); s = u;
5299: }
5300: return s;
5301: }
5302: }
5303:
5304: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5305: {
5306: VL tvl;
5307: int len,n,j,i,e;
5308: NMV m;
5309: Z q;
5310: P c;
5311: UINT *d;
5312: P s,r,u,t,w;
5313: GFS gfs;
5314: P *a;
5315: LIST l;
5316: NODE nd,nd1;
5317:
5318: if ( !p ) return 0;
5319: else {
5320: a = (P *)MALLOC((rank+1)*sizeof(P));
5321: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5322: len = LEN(p);
5323: n = NV(p);
5324: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5325: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5326: if ( mod == -1 ) {
5327: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5328: } else if ( mod ) {
5329: STOQ(CM(m),q); c = (P)q;
5330: } else
5331: c = CP(m);
5332: d = DL(m);
5333: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
5334: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
5335: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5336: }
5337: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5338: }
5339: nd = 0;
5340: for ( i = rank; i > 0; i-- ) {
5341: MKNODE(nd1,a[i],nd); nd = nd1;
5342: }
5343: MKLIST(l,nd);
5344: return l;
5345: }
5346: }
5347:
5348: NDV ndtondv(int mod,ND p)
5349: {
5350: NDV d;
5351: NMV m,m0;
5352: NM t;
5353: int i,len;
5354:
5355: if ( !p ) return 0;
5356: len = LEN(p);
5357: if ( mod > 0 || mod == -1 )
5358: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5359: else
5360: m0 = m = MALLOC(len*nmv_adv);
5361: #if 0
5362: ndv_alloc += nmv_adv*len;
5363: #endif
5364: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5365: ndl_copy(DL(t),DL(m));
5366: CQ(m) = CQ(t);
5367: }
5368: MKNDV(NV(p),m0,len,d);
5369: SG(d) = SG(p);
5370: return d;
5371: }
5372:
5373: ND ndvtond(int mod,NDV p)
5374: {
5375: ND d;
5376: NM m,m0;
5377: NMV t;
5378: int i,len;
5379:
5380: if ( !p ) return 0;
5381: m0 = 0;
5382: len = p->len;
5383: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5384: NEXTNM(m0,m);
5385: ndl_copy(DL(t),DL(m));
5386: CQ(m) = CQ(t);
5387: }
5388: NEXT(m) = 0;
5389: MKND(NV(p),m0,len,d);
5390: SG(d) = SG(p);
5391: return d;
5392: }
5393:
5394: DP ndvtodp(int mod,NDV p)
5395: {
5396: MP m,m0;
5397: DP d;
5398: NMV t;
5399: int i,len;
5400:
5401: if ( !p ) return 0;
5402: m0 = 0;
5403: len = p->len;
5404: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5405: NEXTMP(m0,m);
5406: m->dl = ndltodl(nd_nvar,DL(t));
5407: m->c = (Obj)ndctop(mod,t->c);
5408: }
5409: NEXT(m) = 0;
5410: MKDP(nd_nvar,m0,d);
5411: SG(d) = SG(p);
5412: return d;
5413: }
5414:
5415: DP ndtodp(int mod,ND p)
5416: {
5417: MP m,m0;
5418: DP d;
5419: NM t;
5420: int i,len;
5421:
5422: if ( !p ) return 0;
5423: m0 = 0;
5424: len = p->len;
5425: for ( t = BDY(p); t; t = NEXT(t) ) {
5426: NEXTMP(m0,m);
5427: m->dl = ndltodl(nd_nvar,DL(t));
5428: m->c = (Obj)ndctop(mod,t->c);
5429: }
5430: NEXT(m) = 0;
5431: MKDP(nd_nvar,m0,d);
5432: SG(d) = SG(p);
5433: return d;
5434: }
5435:
5436: void ndv_print(NDV p)
5437: {
5438: NMV m;
5439: int i,len;
5440:
5441: if ( !p ) printf("0\n");
5442: else {
5443: len = LEN(p);
5444: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5445: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5446: else printf("+%d*",CM(m));
5447: ndl_print(DL(m));
5448: }
5449: printf("\n");
5450: }
5451: }
5452:
5453: void ndv_print_q(NDV p)
5454: {
5455: NMV m;
5456: int i,len;
5457:
5458: if ( !p ) printf("0\n");
5459: else {
5460: len = LEN(p);
5461: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5462: printf("+");
5463: printexpr(CO,(Obj)CQ(m));
5464: printf("*");
5465: ndl_print(DL(m));
5466: }
5467: printf("\n");
5468: }
5469: }
5470:
5471: NODE ndv_reducebase(NODE x,int *perm)
5472: {
5473: int len,i,j;
5474: NDVI w;
5475: NODE t,t0;
5476:
5477: len = length(x);
5478: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5479: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5480: w[i].p = BDY(t); w[i].i = perm[i];
5481: }
5482: for ( i = 0; i < len; i++ ) {
5483: for ( j = 0; j < i; j++ ) {
5484: if ( w[i].p && w[j].p ) {
5485: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5486: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5487: }
5488: }
5489: }
5490: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5491: if ( w[i].p ) {
5492: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5493: perm[j++] = w[i].i;
5494: }
5495: }
5496: NEXT(t) = 0; x = t0;
5497: return x;
5498: }
5499:
5500: /* XXX incomplete */
5501:
5502: void nd_init_ord(struct order_spec *ord)
5503: {
5504: nd_module = (ord->id >= 256);
5505: if ( nd_module ) {
5506: nd_dcomp = -1;
5507: nd_ispot = ord->ispot;
5508: nd_pot_nelim = ord->pot_nelim;
5509: nd_poly_weight_len = ord->nv;
5510: nd_poly_weight = ord->top_weight;
5511: nd_module_rank = ord->module_rank;
5512: nd_module_weight = ord->module_top_weight;
5513: }
5514: nd_matrix = 0;
5515: nd_matrix_len = 0;
5516: switch ( ord->id ) {
5517: case 0:
5518: switch ( ord->ord.simple ) {
5519: case 0:
5520: nd_dcomp = 1;
5521: nd_isrlex = 1;
5522: break;
5523: case 1:
5524: nd_dcomp = 1;
5525: nd_isrlex = 0;
5526: break;
5527: case 2:
5528: nd_dcomp = 0;
5529: nd_isrlex = 0;
5530: ndl_compare_function = ndl_lex_compare;
5531: break;
5532: case 11:
5533: /* XXX */
5534: nd_dcomp = 0;
5535: nd_isrlex = 1;
5536: ndl_compare_function = ndl_ww_lex_compare;
5537: break;
5538: default:
5539: error("nd_gr : unsupported order");
5540: }
5541: break;
5542: case 1:
5543: /* block order */
5544: /* XXX */
5545: nd_dcomp = -1;
5546: nd_isrlex = 0;
5547: ndl_compare_function = ndl_block_compare;
5548: break;
5549: case 2:
5550: /* matrix order */
5551: /* XXX */
5552: nd_dcomp = -1;
5553: nd_isrlex = 0;
5554: nd_matrix_len = ord->ord.matrix.row;
5555: nd_matrix = ord->ord.matrix.matrix;
5556: ndl_compare_function = ndl_matrix_compare;
5557: break;
5558: case 3:
5559: /* composite order */
5560: nd_dcomp = -1;
5561: nd_isrlex = 0;
5562: nd_worb_len = ord->ord.composite.length;
5563: nd_worb = ord->ord.composite.w_or_b;
5564: ndl_compare_function = ndl_composite_compare;
5565: break;
5566:
5567: /* module order */
5568: case 256:
5569: switch ( ord->ord.simple ) {
5570: case 0:
5571: nd_isrlex = 1;
5572: ndl_compare_function = ndl_module_grlex_compare;
5573: break;
5574: case 1:
5575: nd_isrlex = 0;
5576: ndl_compare_function = ndl_module_glex_compare;
5577: break;
5578: case 2:
5579: nd_isrlex = 0;
5580: ndl_compare_function = ndl_module_lex_compare;
5581: break;
5582: default:
5583: error("nd_gr : unsupported order");
5584: }
5585: break;
5586: case 257:
5587: /* block order */
5588: nd_isrlex = 0;
5589: ndl_compare_function = ndl_module_block_compare;
5590: break;
5591: case 258:
5592: /* matrix order */
5593: nd_isrlex = 0;
5594: nd_matrix_len = ord->ord.matrix.row;
5595: nd_matrix = ord->ord.matrix.matrix;
5596: ndl_compare_function = ndl_module_matrix_compare;
5597: break;
5598: case 259:
5599: /* composite order */
5600: nd_isrlex = 0;
5601: nd_worb_len = ord->ord.composite.length;
5602: nd_worb = ord->ord.composite.w_or_b;
5603: ndl_compare_function = ndl_module_composite_compare;
5604: break;
5605: }
5606: nd_ord = ord;
5607: }
5608:
5609: BlockMask nd_create_blockmask(struct order_spec *ord)
5610: {
5611: int n,i,j,s,l;
5612: UINT *t;
5613: BlockMask bm;
5614:
5615: /* we only create mask table for block order */
5616: if ( ord->id != 1 && ord->id != 257 )
5617: return 0;
5618: n = ord->ord.block.length;
5619: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5620: bm->n = n;
5621: bm->order_pair = ord->ord.block.order_pair;
5622: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5623: for ( i = 0, s = 0; i < n; i++ ) {
5624: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5625: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5626: l = bm->order_pair[i].length;
5627: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5628: }
5629: return bm;
5630: }
5631:
5632: EPOS nd_create_epos(struct order_spec *ord)
5633: {
5634: int i,j,l,s,ord_l,ord_o;
5635: EPOS epos;
5636: struct order_pair *op;
5637:
5638: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5639: switch ( ord->id ) {
5640: case 0: case 256:
5641: if ( nd_isrlex ) {
5642: for ( i = 0; i < nd_nvar; i++ ) {
5643: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5644: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5645: }
5646: } else {
5647: for ( i = 0; i < nd_nvar; i++ ) {
5648: epos[i].i = nd_exporigin + i/nd_epw;
5649: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5650: }
5651: }
5652: break;
5653: case 1: case 257:
5654: /* block order */
5655: l = ord->ord.block.length;
5656: op = ord->ord.block.order_pair;
5657: for ( j = 0, s = 0; j < l; j++ ) {
5658: ord_o = op[j].order;
5659: ord_l = op[j].length;
5660: if ( !ord_o )
5661: for ( i = 0; i < ord_l; i++ ) {
5662: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5663: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5664: }
5665: else
5666: for ( i = 0; i < ord_l; i++ ) {
5667: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5668: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5669: }
5670: s += ord_l;
5671: }
5672: break;
5673: case 2:
5674: /* matrix order */
5675: case 3:
5676: /* composite order */
5677: default:
5678: for ( i = 0; i < nd_nvar; i++ ) {
5679: epos[i].i = nd_exporigin + i/nd_epw;
5680: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5681: }
5682: break;
5683: }
5684: return epos;
5685: }
5686:
5687: /* external interface */
5688:
5689: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5690: {
5691: NODE t,in0,in;
5692: ND ndf,nf;
5693: NDV ndvf;
5694: VL vv,tv;
5695: int stat,nvar,max,mrank;
5696: union oNDC dn;
5697: Q cont;
5698: P pp;
5699: LIST ppl;
5700:
5701: if ( !f ) {
5702: *rp = 0;
5703: return;
5704: }
5705: pltovl(v,&vv);
5706: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5707:
5708: /* max=65536 implies nd_bpe=32 */
5709: max = 65536;
5710:
5711: nd_module = 0;
5712: /* nd_module will be set if ord is a module ordering */
5713: nd_init_ord(ord);
5714: nd_setup_parameters(nvar,max);
5715: if ( nd_module && OID(f) != O_LIST )
5716: error("nd_nf_p : the first argument must be a list");
5717: if ( nd_module ) mrank = length(BDY((LIST)f));
5718: /* conversion to ndv */
5719: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5720: NEXTNODE(in0,in);
5721: if ( nd_module ) {
5722: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5723: || length(BDY((LIST)BDY(t))) != mrank )
5724: error("nd_nf_p : inconsistent basis element");
5725: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5726: else ppl = (LIST)BDY(t);
5727: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5728: } else {
5729: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5730: else pp = (P)BDY(t);
5731: BDY(in) = (pointer)ptondv(CO,vv,pp);
5732: }
5733: if ( m ) ndv_mod(m,(NDV)BDY(in));
5734: }
5735: if ( in0 ) NEXT(in) = 0;
5736:
5737: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5738: else ndvf = ptondv(CO,vv,(P)f);
5739: if ( m ) ndv_mod(m,ndvf);
5740: ndf = (pointer)ndvtond(m,ndvf);
5741:
5742: /* dont sort, dont removecont */
5743: ndv_setup(m,0,in0,1,1);
5744: nd_scale=2;
5745: stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
5746: if ( !stat )
5747: error("nd_nf_p : exponent too large");
5748: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5749: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5750: }
5751:
5752: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5753: {
5754: NM m;
5755: UINT *t,*s;
5756: int i;
5757:
5758: for ( i = 0; i < n; i++ ) r[i] = 0;
5759: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5760: t = DL(m);
5761: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5762: r[i] = CM(m);
5763: }
5764: for ( i = 0; !r[i]; i++ );
5765: return i;
5766: }
5767:
5768: #if defined(__GNUC__) && SIZEOF_LONG==8
5769:
5770: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
5771:
5772: int nd_to_vect64(int mod,UINT *s0,int n,ND d,U64 *r)
5773: {
5774: NM m;
5775: UINT *t,*s;
5776: int i;
5777:
5778: for ( i = 0; i < n; i++ ) r[i] = 0;
5779: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5780: t = DL(m);
5781: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5782: r[i] = (U64)CM(m);
5783: }
5784: for ( i = 0; !r[i]; i++ );
5785: return i;
5786: }
5787: #endif
5788:
5789: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
5790: {
5791: NM m;
5792: UINT *t,*s;
5793: int i;
5794:
5795: for ( i = 0; i < n; i++ ) r[i] = 0;
5796: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5797: t = DL(m);
5798: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.2 ! noro 5799: dupz(CQ(m),&r[i]);
1.1 noro 5800: }
5801: for ( i = 0; !r[i]; i++ );
5802: return i;
5803: }
5804:
5805: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5806: {
5807: NM m;
5808: UINT *t,*s;
5809: int i;
5810:
5811: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5812: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5813: t = DL(m);
5814: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5815: mpz_set(r[i],BDY(CZ(m)));
5816: }
5817: for ( i = 0; !mpz_sgn(r[i]); i++ );
5818: return i;
5819: }
5820:
5821: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
5822: {
5823: NM m;
5824: unsigned long *v;
5825: int i,j,h,size;
5826: UINT *s,*t;
5827:
5828: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
5829: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
5830: bzero(v,size);
5831: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
5832: t = DL(m);
5833: h = ndl_hash_value(t);
5834: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5835: v[i/BLEN] |= 1L <<(i%BLEN);
5836: }
5837: return v;
5838: }
5839:
5840: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
5841: {
5842: NMV mr;
5843: UINT *d,*t,*s;
5844: int i,j,len,h,head;
5845:
5846: d = DL(m);
5847: len = LEN(p);
5848: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5849: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5850: ndl_add(d,DL(mr),t);
5851: h = ndl_hash_value(t);
5852: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5853: if ( j == 0 ) head = i;
5854: v[i/BLEN] |= 1L <<(i%BLEN);
5855: }
5856: return head;
5857: }
5858:
5859: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
5860: {
5861: NM m;
5862: NMV mr;
5863: UINT *d,*t,*s;
5864: NDV p;
5865: int i,j,len;
5866: Z *r;
5867:
5868: m = pair->mul;
5869: d = DL(m);
5870: p = nd_ps[pair->index];
5871: len = LEN(p);
5872: r = (Z *)CALLOC(n,sizeof(Q));
5873: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5874: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5875: ndl_add(d,DL(mr),t);
5876: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5877: r[i] = CQ(mr);
5878: }
5879: return r;
5880: }
5881:
5882: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
5883: {
5884: NM m;
5885: NMV mr;
5886: UINT *d,*t,*s;
5887: NDV p;
5888: unsigned char *ivc;
5889: unsigned short *ivs;
5890: UINT *v,*ivi,*s0v;
5891: int i,j,len,prev,diff,cdiff,h;
5892: IndArray r;
5893: struct oEGT eg0,eg1;
5894:
5895: m = pair->mul;
5896: d = DL(m);
5897: if ( trace )
5898: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
5899: else
5900: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
5901:
5902: len = LEN(p);
5903: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5904: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
5905: get_eg(&eg0);
5906: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5907: ndl_add(d,DL(mr),t);
5908: h = ndl_hash_value(t);
5909: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5910: v[j] = i;
5911: }
5912: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
5913: r = (IndArray)MALLOC(sizeof(struct oIndArray));
5914: r->head = v[0];
5915: diff = 0;
5916: for ( i = 1; i < len; i++ ) {
5917: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
5918: }
5919: if ( diff < 256 ) {
5920: r->width = 1;
5921: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
5922: r->index.c = ivc;
5923: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
5924: } else if ( diff < 65536 ) {
5925: r->width = 2;
5926: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
5927: r->index.s = ivs;
5928: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
5929: } else {
5930: r->width = 4;
5931: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
5932: r->index.i = ivi;
5933: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
5934: }
5935: return r;
5936: }
5937:
5938: int compress_array(Z *svect,Z *cvect,int n)
5939: {
5940: int i,j;
5941:
5942: for ( i = j = 0; i < n; i++ )
5943: if ( svect[i] ) cvect[j++] = svect[i];
5944: return j;
5945: }
5946:
5947: void expand_array(Z *svect,Z *cvect,int n)
5948: {
5949: int i,j;
5950:
5951: for ( i = j = 0; j < n; i++ )
5952: if ( svect[i] ) svect[i] = cvect[j++];
5953: }
5954:
5955: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
5956: {
5957: int i,j,k,len,pos,prev,nz;
5958: Z cs,mcs,c1,c2,cr,gcd,t;
5959: IndArray ivect;
5960: unsigned char *ivc;
5961: unsigned short *ivs;
5962: unsigned int *ivi;
5963: NDV redv;
5964: NMV mr;
5965: NODE rp;
5966: int maxrs;
5967: double hmag;
5968: Z *cvect;
5969:
5970: maxrs = 0;
5971: for ( i = 0; i < col && !svect[i]; i++ );
5972: if ( i == col ) return maxrs;
5973: hmag = p_mag((P)svect[i])*nd_scale;
5974: cvect = (Z *)MALLOC(col*sizeof(Q));
5975: for ( i = 0; i < nred; i++ ) {
5976: ivect = imat[i];
5977: k = ivect->head;
5978: if ( svect[k] ) {
5979: maxrs = MAX(maxrs,rp0[i]->sugar);
5980: redv = nd_demand?ndv_load(rp0[i]->index)
5981: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
5982: len = LEN(redv); mr = BDY(redv);
5983: igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
5984: chsgnz(cs,&mcs);
5985: if ( !UNIQ(cr) ) {
5986: for ( j = 0; j < col; j++ ) {
5987: mulz(svect[j],cr,&c1); svect[j] = c1;
5988: }
5989: }
5990: svect[k] = 0; prev = k;
5991: switch ( ivect->width ) {
5992: case 1:
5993: ivc = ivect->index.c;
5994: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
5995: pos = prev+ivc[j]; prev = pos;
1.2 ! noro 5996: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 5997: }
5998: break;
5999: case 2:
6000: ivs = ivect->index.s;
6001: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6002: pos = prev+ivs[j]; prev = pos;
1.2 ! noro 6003: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 6004: }
6005: break;
6006: case 4:
6007: ivi = ivect->index.i;
6008: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6009: pos = prev+ivi[j]; prev = pos;
1.2 ! noro 6010: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 6011: }
6012: break;
6013: }
6014: for ( j = k+1; j < col && !svect[j]; j++ );
6015: if ( j == col ) break;
6016: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6017: nz = compress_array(svect,cvect,col);
6018: removecont_array((P *)cvect,nz,1);
6019: expand_array(svect,cvect,nz);
6020: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6021: }
6022: }
6023: }
6024: nz = compress_array(svect,cvect,col);
6025: removecont_array((P *)cvect,nz,1);
6026: expand_array(svect,cvect,nz);
6027: if ( DP_Print ) {
6028: fprintf(asir_out,"-"); fflush(asir_out);
6029: }
6030: return maxrs;
6031: }
6032:
6033: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6034: {
6035: int i,j,k,len,pos,prev;
6036: UINT c,c1,c2,c3,up,lo,dmy;
6037: IndArray ivect;
6038: unsigned char *ivc;
6039: unsigned short *ivs;
6040: unsigned int *ivi;
6041: NDV redv;
6042: NMV mr;
6043: NODE rp;
6044: int maxrs;
6045:
6046: maxrs = 0;
6047: for ( i = 0; i < nred; i++ ) {
6048: ivect = imat[i];
6049: k = ivect->head; svect[k] %= m;
6050: if ( (c = svect[k]) != 0 ) {
6051: maxrs = MAX(maxrs,rp0[i]->sugar);
6052: c = m-c; redv = nd_ps[rp0[i]->index];
6053: len = LEN(redv); mr = BDY(redv);
6054: svect[k] = 0; prev = k;
6055: switch ( ivect->width ) {
6056: case 1:
6057: ivc = ivect->index.c;
6058: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6059: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6060: if ( c1 ) {
6061: c2 = svect[pos];
6062: DMA(c1,c,c2,up,lo);
6063: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6064: } else svect[pos] = lo;
6065: }
6066: }
6067: break;
6068: case 2:
6069: ivs = ivect->index.s;
6070: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6071: pos = prev+ivs[j]; c1 = CM(mr);
6072: prev = pos;
6073: if ( c1 ) {
6074: c2 = svect[pos];
6075: DMA(c1,c,c2,up,lo);
6076: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6077: } else svect[pos] = lo;
6078: }
6079: }
6080: break;
6081: case 4:
6082: ivi = ivect->index.i;
6083: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6084: pos = prev+ivi[j]; c1 = CM(mr);
6085: prev = pos;
6086: if ( c1 ) {
6087: c2 = svect[pos];
6088: DMA(c1,c,c2,up,lo);
6089: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6090: } else svect[pos] = lo;
6091: }
6092: }
6093: break;
6094: }
6095: }
6096: }
6097: for ( i = 0; i < col; i++ )
6098: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6099: return maxrs;
6100: }
6101:
6102: #if defined(__GNUC__) && SIZEOF_LONG==8
6103:
6104: int ndv_reduce_vect64(int m,U64 *svect,U64 *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6105: {
6106: int i,j,k,len,pos,prev;
6107: U64 a,c,c1,c2;
6108: IndArray ivect;
6109: unsigned char *ivc;
6110: unsigned short *ivs;
6111: unsigned int *ivi;
6112: NDV redv;
6113: NMV mr;
6114: NODE rp;
6115: int maxrs;
6116:
6117: for ( i = 0; i < col; i++ ) cvect[i] = 0;
6118: maxrs = 0;
6119: for ( i = 0; i < nred; i++ ) {
6120: ivect = imat[i];
6121: k = ivect->head;
6122: a = svect[k]; c = cvect[k];
6123: MOD128(a,c,m);
6124: svect[k] = a; cvect[k] = 0;
6125: if ( (c = svect[k]) != 0 ) {
6126: maxrs = MAX(maxrs,rp0[i]->sugar);
6127: c = m-c; redv = nd_ps[rp0[i]->index];
6128: len = LEN(redv); mr = BDY(redv);
6129: svect[k] = 0; prev = k;
6130: switch ( ivect->width ) {
6131: case 1:
6132: ivc = ivect->index.c;
6133: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6134: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6135: if ( c1 ) {
6136: c2 = svect[pos]+c1*c;
6137: if ( c2 < svect[pos] ) cvect[pos]++;
6138: svect[pos] = c2;
6139: }
6140: }
6141: break;
6142: case 2:
6143: ivs = ivect->index.s;
6144: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6145: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
6146: if ( c1 ) {
6147: c2 = svect[pos]+c1*c;
6148: if ( c2 < svect[pos] ) cvect[pos]++;
6149: svect[pos] = c2;
6150: }
6151: }
6152: break;
6153: case 4:
6154: ivi = ivect->index.i;
6155: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6156: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
6157: if ( c1 ) {
6158: c2 = svect[pos]+c1*c;
6159: if ( c2 < svect[pos] ) cvect[pos]++;
6160: svect[pos] = c2;
6161: }
6162: }
6163: break;
6164: }
6165: }
6166: }
6167: for ( i = 0; i < col; i++ ) {
6168: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
6169: }
6170: return maxrs;
6171: }
6172: #endif
6173:
6174: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6175: {
6176: int i,j,k,len,pos,prev;
6177: UINT c,c1,c2,c3,up,lo,dmy;
6178: IndArray ivect;
6179: unsigned char *ivc;
6180: unsigned short *ivs;
6181: unsigned int *ivi;
6182: NDV redv;
6183: NMV mr;
6184: NODE rp;
6185: int maxrs;
6186:
6187: maxrs = 0;
6188: for ( i = 0; i < nred; i++ ) {
6189: ivect = imat[i];
6190: k = ivect->head;
6191: if ( (c = svect[k]) != 0 ) {
6192: maxrs = MAX(maxrs,rp0[i]->sugar);
6193: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6194: len = LEN(redv); mr = BDY(redv);
6195: svect[k] = 0; prev = k;
6196: switch ( ivect->width ) {
6197: case 1:
6198: ivc = ivect->index.c;
6199: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6200: pos = prev+ivc[j]; prev = pos;
6201: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6202: }
6203: break;
6204: case 2:
6205: ivs = ivect->index.s;
6206: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6207: pos = prev+ivs[j]; prev = pos;
6208: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6209: }
6210: break;
6211: case 4:
6212: ivi = ivect->index.i;
6213: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6214: pos = prev+ivi[j]; prev = pos;
6215: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6216: }
6217: break;
6218: }
6219: }
6220: }
6221: return maxrs;
6222: }
6223:
6224: ND nd_add_lf(ND p1,ND p2)
6225: {
6226: int n,c,can;
6227: ND r;
6228: NM m1,m2,mr0,mr,s;
6229: Z t;
6230:
6231: if ( !p1 ) return p2;
6232: else if ( !p2 ) return p1;
6233: else {
6234: can = 0;
6235: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6236: c = DL_COMPARE(DL(m1),DL(m2));
6237: switch ( c ) {
6238: case 0:
6239: addlf(CZ(m1),CZ(m2),&t);
6240: s = m1; m1 = NEXT(m1);
6241: if ( t ) {
6242: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6243: } else {
6244: can += 2; FREENM(s);
6245: }
6246: s = m2; m2 = NEXT(m2); FREENM(s);
6247: break;
6248: case 1:
6249: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6250: break;
6251: case -1:
6252: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6253: break;
6254: }
6255: }
6256: if ( !mr0 )
6257: if ( m1 ) mr0 = m1;
6258: else if ( m2 ) mr0 = m2;
6259: else return 0;
6260: else if ( m1 ) NEXT(mr) = m1;
6261: else if ( m2 ) NEXT(mr) = m2;
6262: else NEXT(mr) = 0;
6263: BDY(p1) = mr0;
6264: SG(p1) = MAX(SG(p1),SG(p2));
6265: LEN(p1) = LEN(p1)+LEN(p2)-can;
6266: FREEND(p2);
6267: return p1;
6268: }
6269: }
6270:
6271: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6272: {
6273: int i,j,k,len,pos,prev;
6274: mpz_t c,mc,c1;
6275: IndArray ivect;
6276: unsigned char *ivc;
6277: unsigned short *ivs;
6278: unsigned int *ivi;
6279: NDV redv;
6280: NMV mr;
6281: NODE rp;
6282: int maxrs;
6283:
6284: maxrs = 0;
6285: lf_lazy = 1;
6286: for ( i = 0; i < nred; i++ ) {
6287: ivect = imat[i];
6288: k = ivect->head;
6289: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6290: if ( mpz_sgn(svect[k]) ) {
6291: maxrs = MAX(maxrs,rp0[i]->sugar);
6292: mpz_neg(svect[k],svect[k]);
6293: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6294: len = LEN(redv); mr = BDY(redv);
6295: prev = k;
6296: switch ( ivect->width ) {
6297: case 1:
6298: ivc = ivect->index.c;
6299: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6300: pos = prev+ivc[j]; prev = pos;
6301: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6302: }
6303: break;
6304: case 2:
6305: ivs = ivect->index.s;
6306: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6307: pos = prev+ivs[j]; prev = pos;
6308: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6309: }
6310: break;
6311: case 4:
6312: ivi = ivect->index.i;
6313: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6314: pos = prev+ivi[j]; prev = pos;
6315: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6316: }
6317: break;
6318: }
6319: mpz_set_ui(svect[k],0);
6320: }
6321: }
6322: lf_lazy=0;
6323: for ( i = 0; i < col; i++ ) {
6324: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6325: }
6326: return maxrs;
6327: }
6328:
6329: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6330: {
6331: int i,j,k,l,rank,s;
6332: mpz_t a,a1,inv;
6333: mpz_t *t,*pivot,*pk;
6334: mpz_t **mat;
6335: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6336: int size,size1;
6337:
6338: mpz_init(inv);
6339: mpz_init(a);
6340: mat = (mpz_t **)mat0;
6341: size = 0;
6342: for ( rank = 0, j = 0; j < col; j++ ) {
6343: for ( i = rank; i < row; i++ ) {
6344: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6345: }
6346: for ( i = rank; i < row; i++ )
6347: if ( mpz_sgn(mat[i][j]) )
6348: break;
6349: if ( i == row ) {
6350: colstat[j] = 0;
6351: continue;
6352: } else
6353: colstat[j] = 1;
6354: if ( i != rank ) {
6355: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6356: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6357: }
6358: pivot = mat[rank];
6359: s = sugar[rank];
6360: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6361: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6362: if ( mpz_sgn(*pk) ) {
6363: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6364: }
6365: for ( i = rank+1; i < row; i++ ) {
6366: t = mat[i];
6367: if ( mpz_sgn(t[j]) ) {
6368: sugar[i] = MAX(sugar[i],s);
6369: mpz_neg(a,t[j]);
6370: red_by_vect_lf(t+j,pivot+j,a,col-j);
6371: }
6372: }
6373: rank++;
6374: }
6375: for ( j = col-1, l = rank-1; j >= 0; j-- )
6376: if ( colstat[j] ) {
6377: pivot = mat[l];
6378: s = sugar[l];
6379: for ( k = j; k < col; k++ )
6380: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6381: for ( i = 0; i < l; i++ ) {
6382: t = mat[i];
6383: if ( mpz_sgn(t[j]) ) {
6384: sugar[i] = MAX(sugar[i],s);
6385: mpz_neg(a,t[j]);
6386: red_by_vect_lf(t+j,pivot+j,a,col-j);
6387: }
6388: }
6389: l--;
6390: }
6391: for ( j = 0, l = 0; l < rank; j++ )
6392: if ( colstat[j] ) {
6393: t = mat[l];
6394: for ( k = j; k < col; k++ ) {
6395: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6396: }
6397: l++;
6398: }
6399: return rank;
6400: }
6401:
6402:
6403: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6404: {
6405: int j,k,len;
6406: UINT *p;
6407: UINT c;
6408: NDV r;
6409: NMV mr0,mr;
6410:
6411: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6412: if ( !len ) return 0;
6413: else {
6414: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6415: #if 0
6416: ndv_alloc += nmv_adv*len;
6417: #endif
6418: mr = mr0;
6419: p = s0vect;
6420: for ( j = k = 0; j < col; j++, p += nd_wpd )
6421: if ( !rhead[j] ) {
6422: if ( (c = vect[k++]) != 0 ) {
6423: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6424: }
6425: }
6426: MKNDV(nd_nvar,mr0,len,r);
6427: return r;
6428: }
6429: }
6430:
6431: #if defined(__GNUC__) && SIZEOF_LONG==8
6432: NDV vect64_to_ndv(U64 *vect,int spcol,int col,int *rhead,UINT *s0vect)
6433: {
6434: int j,k,len;
6435: UINT *p;
6436: UINT c;
6437: NDV r;
6438: NMV mr0,mr;
6439:
6440: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6441: if ( !len ) return 0;
6442: else {
6443: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6444: #if 0
6445: ndv_alloc += nmv_adv*len;
6446: #endif
6447: mr = mr0;
6448: p = s0vect;
6449: for ( j = k = 0; j < col; j++, p += nd_wpd )
6450: if ( !rhead[j] ) {
6451: if ( (c = (UINT)vect[k++]) != 0 ) {
6452: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6453: }
6454: }
6455: MKNDV(nd_nvar,mr0,len,r);
6456: return r;
6457: }
6458: }
6459: #endif
6460:
6461: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6462: {
6463: int j,k,len;
6464: UINT *p;
6465: NDV r;
6466: NMV mr0,mr;
6467:
6468: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6469: if ( !len ) return 0;
6470: else {
6471: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6472: mr = mr0;
6473: p = s0vect;
6474: for ( j = 0; j < col; j++, p += nd_wpd )
6475: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6476: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6477: }
6478: MKNDV(nd_nvar,mr0,len,r);
6479: return r;
6480: }
6481: }
6482:
6483: /* for preprocessed vector */
6484:
6485: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6486: {
6487: int j,k,len;
6488: UINT *p;
6489: Z c;
6490: NDV r;
6491: NMV mr0,mr;
6492:
6493: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6494: if ( !len ) return 0;
6495: else {
6496: mr0 = (NMV)MALLOC(nmv_adv*len);
6497: #if 0
6498: ndv_alloc += nmv_adv*len;
6499: #endif
6500: mr = mr0;
6501: p = s0vect;
6502: for ( j = k = 0; j < col; j++, p += nd_wpd )
6503: if ( !rhead[j] ) {
6504: if ( (c = vect[k++]) != 0 ) {
6505: if ( !INT(c) )
6506: error("afo");
6507: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6508: }
6509: }
6510: MKNDV(nd_nvar,mr0,len,r);
6511: return r;
6512: }
6513: }
6514:
6515: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6516: {
6517: int j,k,len;
6518: UINT *p;
6519: mpz_t c;
6520: NDV r;
6521: NMV mr0,mr;
6522:
6523: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6524: if ( !len ) return 0;
6525: else {
6526: mr0 = (NMV)MALLOC(nmv_adv*len);
6527: #if 0
6528: ndv_alloc += nmv_adv*len;
6529: #endif
6530: mr = mr0;
6531: p = s0vect;
6532: for ( j = k = 0; j < col; j++, p += nd_wpd )
6533: if ( !rhead[j] ) {
6534: c[0] = vect[k++][0];
6535: if ( mpz_sgn(c) ) {
6536: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6537: }
6538: }
6539: MKNDV(nd_nvar,mr0,len,r);
6540: return r;
6541: }
6542: }
6543:
6544: /* for plain vector */
6545:
6546: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6547: {
6548: int j,k,len;
6549: UINT *p;
6550: Z c;
6551: NDV r;
6552: NMV mr0,mr;
6553:
6554: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6555: if ( !len ) return 0;
6556: else {
6557: mr0 = (NMV)MALLOC(nmv_adv*len);
6558: #if 0
6559: ndv_alloc += nmv_adv*len;
6560: #endif
6561: mr = mr0;
6562: p = s0vect;
6563: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6564: if ( (c = vect[k]) != 0 ) {
6565: if ( !INT(c) )
6566: error("afo");
6567: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6568: }
6569: MKNDV(nd_nvar,mr0,len,r);
6570: return r;
6571: }
6572: }
6573:
6574: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6575: {
6576: ND_pairs t;
6577: NODE sp0,sp;
6578: int stat;
6579: ND spol;
6580:
6581: for ( t = l; t; t = NEXT(t) ) {
6582: stat = nd_sp(m,trace,t,&spol);
6583: if ( !stat ) return 0;
6584: if ( spol ) {
6585: add_pbucket_symbolic(bucket,spol);
6586: }
6587: }
6588: return 1;
6589: }
6590:
6591: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6592: {
6593: NODE rp0,rp;
6594: NM mul,head,s0,s;
6595: int index,col,i,sugar;
6596: RHist h;
6597: UINT *s0v,*p;
6598: NM_ind_pair pair;
6599: ND red;
6600: NDV *ps;
6601: static int afo;
6602:
6603: s0 = 0; rp0 = 0; col = 0;
6604: if ( nd_demand )
6605: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6606: else
6607: ps = trace?nd_ps_trace:nd_ps;
6608: while ( 1 ) {
6609: head = remove_head_pbucket_symbolic(bucket);
6610: if ( !head ) break;
6611: if ( !s0 ) s0 = head;
6612: else NEXT(s) = head;
6613: s = head;
6614: index = ndl_find_reducer(DL(head));
6615: if ( index >= 0 ) {
6616: h = nd_psh[index];
6617: NEWNM(mul);
6618: ndl_sub(DL(head),DL(h),DL(mul));
6619: if ( ndl_check_bound2(index,DL(mul)) )
6620: return 0;
6621: sugar = TD(DL(mul))+SG(ps[index]);
6622: MKNM_ind_pair(pair,mul,index,sugar);
6623: red = ndv_mul_nm_symbolic(mul,ps[index]);
6624: add_pbucket_symbolic(bucket,nd_remove_head(red));
6625: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6626: }
6627: col++;
6628: }
6629: if ( rp0 ) NEXT(rp) = 0;
6630: NEXT(s) = 0;
6631: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6632: for ( i = 0, p = s0v, s = s0; i < col;
6633: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6634: *s0vect = s0v;
6635: *r = rp0;
6636: return col;
6637: }
6638:
6639: void print_ndp(ND_pairs l)
6640: {
6641: ND_pairs t;
6642:
6643: for ( t = l; t; t = NEXT(t) )
6644: printf("[%d,%d] ",t->i1,t->i2);
6645: printf("\n");
6646: }
6647:
6648: NODE nd_f4(int m,int checkonly,int **indp)
6649: {
6650: int i,nh,stat,index,f4red;
6651: NODE r,g,tn0,tn,node;
6652: ND_pairs d,l,t,ll0,ll,lh;
6653: LIST l0,l1;
6654: ND spol,red;
6655: NDV nf,redv;
6656: NM s0,s;
6657: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6658: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6659: UINT c;
6660: UINT **spmat;
6661: UINT *s0vect,*svect,*p,*v;
6662: int *colstat;
6663: IndArray *imat;
6664: int *rhead;
6665: int spcol,sprow;
6666: int sugar,sugarh;
6667: PGeoBucket bucket;
6668: struct oEGT eg0,eg1,eg_f4;
6669: Z i1,i2,sugarq;
6670: #if 0
6671: ndv_alloc = 0;
6672: #endif
6673: g = 0; d = 0;
6674: for ( i = 0; i < nd_psn; i++ ) {
6675: d = update_pairs(d,g,i,0);
6676: g = update_base(g,i);
6677: }
6678: nzlist = 0;
6679: nzlist_t = nd_nzlist;
6680: f4red = 1;
6681: nd_last_nonzero = 0;
6682: while ( d ) {
6683: get_eg(&eg0);
6684: l = nd_minsugarp(d,&d);
6685: sugar = nd_sugarweight?l->sugar2:SG(l);
6686: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6687: if ( nzlist_t ) {
6688: node = BDY((LIST)BDY(nzlist_t));
6689: sugarh = QTOS((Q)ARG0(node));
6690: tn = BDY((LIST)ARG1(node));
6691: if ( !tn ) {
6692: nzlist_t = NEXT(nzlist_t);
6693: continue;
6694: }
6695: /* tn = [[i1,i2],...] */
6696: lh = nd_ipairtospair(tn);
6697: }
6698: bucket = create_pbucket();
6699: stat = nd_sp_f4(m,0,l,bucket);
6700: if ( !stat ) {
6701: for ( t = l; NEXT(t); t = NEXT(t) );
6702: NEXT(t) = d; d = l;
6703: d = nd_reconstruct(0,d);
6704: continue;
6705: }
6706: if ( bucket->m < 0 ) continue;
6707: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6708: if ( !col ) {
6709: for ( t = l; NEXT(t); t = NEXT(t) );
6710: NEXT(t) = d; d = l;
6711: d = nd_reconstruct(0,d);
6712: continue;
6713: }
6714: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6715: if ( DP_Print )
6716: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
6717: sugar,eg_f4.exectime+eg_f4.gctime);
6718: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6719: if ( checkonly && nflist ) return 0;
6720: /* adding new bases */
6721: if ( nflist ) nd_last_nonzero = f4red;
6722: for ( r = nflist; r; r = NEXT(r) ) {
6723: nf = (NDV)BDY(r);
6724: ndv_removecont(m,nf);
6725: if ( !m && nd_nalg ) {
6726: ND nf1;
6727:
6728: nf1 = ndvtond(m,nf);
6729: nd_monic(0,&nf1);
6730: nd_removecont(m,nf1);
6731: nf = ndtondv(m,nf1);
6732: }
6733: nh = ndv_newps(m,nf,0,1);
6734: d = update_pairs(d,g,nh,0);
6735: g = update_base(g,nh);
6736: }
6737: if ( DP_Print ) {
6738: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6739: }
6740: if ( nd_gentrace ) {
6741: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6742: NEXTNODE(tn0,tn);
6743: STOQ(t->i1,i1); STOQ(t->i2,i2);
6744: node = mknode(2,i1,i2); MKLIST(l0,node);
6745: BDY(tn) = l0;
6746: }
6747: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
6748: STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
6749: MKNODE(node,l1,nzlist); nzlist = node;
6750: }
6751: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6752: f4red++;
6753: if ( nd_f4red && f4red > nd_f4red ) break;
6754: if ( nd_rank0 && !nflist ) break;
6755: }
6756: if ( nd_gentrace ) {
6757: MKLIST(l0,reverse_node(nzlist));
6758: MKNODE(nd_alltracelist,l0,0);
6759: }
6760: #if 0
6761: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6762: #endif
6763: conv_ilist(nd_demand,0,g,indp);
6764: return g;
6765: }
6766:
6767: NODE nd_f4_trace(int m,int **indp)
6768: {
6769: int i,nh,stat,index;
6770: NODE r,g;
6771: ND_pairs d,l,l0,t;
6772: ND spol,red;
6773: NDV nf,redv,nfqv,nfv;
6774: NM s0,s;
6775: NODE rp0,srp0,nflist;
6776: int nsp,nred,col,rank,len,k,j,a;
6777: UINT c;
6778: UINT **spmat;
6779: UINT *s0vect,*svect,*p,*v;
6780: int *colstat;
6781: IndArray *imat;
6782: int *rhead;
6783: int spcol,sprow;
6784: int sugar;
6785: PGeoBucket bucket;
6786: struct oEGT eg0,eg1,eg_f4;
6787:
6788: g = 0; d = 0;
6789: for ( i = 0; i < nd_psn; i++ ) {
6790: d = update_pairs(d,g,i,0);
6791: g = update_base(g,i);
6792: }
6793: while ( d ) {
6794: get_eg(&eg0);
6795: l = nd_minsugarp(d,&d);
6796: sugar = SG(l);
6797: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6798: bucket = create_pbucket();
6799: stat = nd_sp_f4(m,0,l,bucket);
6800: if ( !stat ) {
6801: for ( t = l; NEXT(t); t = NEXT(t) );
6802: NEXT(t) = d; d = l;
6803: d = nd_reconstruct(1,d);
6804: continue;
6805: }
6806: if ( bucket->m < 0 ) continue;
6807: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6808: if ( !col ) {
6809: for ( t = l; NEXT(t); t = NEXT(t) );
6810: NEXT(t) = d; d = l;
6811: d = nd_reconstruct(1,d);
6812: continue;
6813: }
6814: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6815: if ( DP_Print )
6816: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
6817: sugar,eg_f4.exectime+eg_f4.gctime);
6818: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6819: if ( !l0 ) continue;
6820: l = l0;
6821:
6822: /* over Q */
6823: bucket = create_pbucket();
6824: stat = nd_sp_f4(0,1,l,bucket);
6825: if ( !stat ) {
6826: for ( t = l; NEXT(t); t = NEXT(t) );
6827: NEXT(t) = d; d = l;
6828: d = nd_reconstruct(1,d);
6829: continue;
6830: }
6831: if ( bucket->m < 0 ) continue;
6832: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6833: if ( !col ) {
6834: for ( t = l; NEXT(t); t = NEXT(t) );
6835: NEXT(t) = d; d = l;
6836: d = nd_reconstruct(1,d);
6837: continue;
6838: }
6839: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6840: /* adding new bases */
6841: for ( r = nflist; r; r = NEXT(r) ) {
6842: nfqv = (NDV)BDY(r);
6843: ndv_removecont(0,nfqv);
6844: if ( !remqi((Q)HCQ(nfqv),m) ) return 0;
6845: if ( nd_nalg ) {
6846: ND nf1;
6847:
6848: nf1 = ndvtond(m,nfqv);
6849: nd_monic(0,&nf1);
6850: nd_removecont(0,nf1);
6851: nfqv = ndtondv(0,nf1); nd_free(nf1);
6852: }
6853: nfv = ndv_dup(0,nfqv);
6854: ndv_mod(m,nfv);
6855: ndv_removecont(m,nfv);
6856: nh = ndv_newps(0,nfv,nfqv,1);
6857: d = update_pairs(d,g,nh,0);
6858: g = update_base(g,nh);
6859: }
6860: }
6861: #if 0
6862: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6863: #endif
6864: conv_ilist(nd_demand,1,g,indp);
6865: return g;
6866: }
6867:
6868: int rref(matrix mat,int *sugar)
6869: {
6870: int row,col,i,j,k,l,s,wcol,wj;
6871: unsigned long bj;
6872: unsigned long **a;
6873: unsigned long *ai,*ak,*as,*t;
6874: int *pivot;
6875:
6876: row = mat->row;
6877: col = mat->col;
6878: a = mat->a;
6879: wcol = (col+BLEN-1)/BLEN;
6880: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6881: i = 0;
6882: for ( j = 0; j < col; j++ ) {
6883: wj = j/BLEN; bj = 1L<<(j%BLEN);
6884: for ( k = i; k < row; k++ )
6885: if ( a[k][wj] & bj ) break;
6886: if ( k == row ) continue;
6887: pivot[i] = j;
6888: if ( k != i ) {
6889: t = a[i]; a[i] = a[k]; a[k] = t;
6890: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6891: }
6892: ai = a[i];
6893: for ( k = i+1; k < row; k++ ) {
6894: ak = a[k];
6895: if ( ak[wj] & bj ) {
6896: for ( l = wj; l < wcol; l++ )
6897: ak[l] ^= ai[l];
6898: sugar[k] = MAX(sugar[k],sugar[i]);
6899: }
6900: }
6901: i++;
6902: }
6903: for ( k = i-1; k >= 0; k-- ) {
6904: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6905: ak = a[k];
6906: for ( s = 0; s < k; s++ ) {
6907: as = a[s];
6908: if ( as[wj] & bj ) {
6909: for ( l = wj; l < wcol; l++ )
6910: as[l] ^= ak[l];
6911: sugar[s] = MAX(sugar[s],sugar[k]);
6912: }
6913: }
6914: }
6915: return i;
6916: }
6917:
6918: void print_matrix(matrix mat)
6919: {
6920: int row,col,i,j;
6921: unsigned long *ai;
6922:
6923: row = mat->row;
6924: col = mat->col;
6925: printf("%d x %d\n",row,col);
6926: for ( i = 0; i < row; i++ ) {
6927: ai = mat->a[i];
6928: for ( j = 0; j < col; j++ ) {
6929: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6930: else putchar('0');
6931: }
6932: putchar('\n');
6933: }
6934: }
6935:
6936: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6937:
6938: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6939: {
6940: int row,col,wcol,wj,i,j;
6941: unsigned long bj;
6942: unsigned long *ai;
6943: unsigned long **a;
6944: int len;
6945: int *pos;
6946:
6947: row = mat->row;
6948: col = mat->col;
6949: wcol = (col+BLEN-1)/BLEN;
6950: pos = (int *)MALLOC(wcol*sizeof(int));
6951: bzero(pos,wcol*sizeof(int));
6952: for ( i = j = 0; i < wcol; i++ )
6953: if ( v[i] ) pos[j++] = i;;
6954: len = j;
6955: wj = rhead/BLEN;
6956: bj = 1L<<rhead%BLEN;
6957: a = mat->a;
6958: for ( i = 0; i < row; i++ ) {
6959: ai = a[i];
6960: if ( ai[wj]&bj ) {
6961: for ( j = 0; j < len; j++ )
6962: ai[pos[j]] ^= v[pos[j]];
6963: sugar[i] = MAX(sugar[i],rsugar);
6964: }
6965: }
6966: }
6967:
6968: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
6969: {
6970: int nsp,nred,i,i0,k,rank,row;
6971: NODE r0,rp;
6972: ND_pairs sp;
6973: ND spol;
6974: NM_ind_pair rt;
6975: int *s0hash;
6976: UINT *s;
6977: int *pivot,*sugar,*head;
6978: matrix mat;
6979: NM m;
6980: NODE r;
6981: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
6982: int rhead,rsugar,size;
6983: unsigned long *v;
6984:
6985: get_eg(&eg0);
6986: init_eg(&eg_search);
6987: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
6988: nred = length(rp0);
6989: mat = alloc_matrix(nsp,col);
6990: s0hash = (int *)MALLOC(col*sizeof(int));
6991: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
6992: s0hash[i] = ndl_hash_value(s);
6993:
6994: sugar = (int *)MALLOC(nsp*sizeof(int));
6995: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
6996: nd_sp(2,0,sp,&spol);
6997: if ( spol ) {
6998: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
6999: sugar[i] = SG(spol);
7000: i++;
7001: }
7002: }
7003: mat->row = i;
7004: if ( DP_Print ) {
7005: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7006: }
7007: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7008: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7009: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7010: rt = (NM_ind_pair)BDY(rp);
7011: bzero(v,size);
7012: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7013: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7014: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7015: }
7016:
7017: get_eg(&eg1);
7018: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7019: rank = rref(mat,sugar);
7020:
7021: for ( i = 0, r0 = 0; i < rank; i++ ) {
7022: NEXTNODE(r0,r);
7023: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7024: SG((NDV)BDY(r)) = sugar[i];
7025: }
7026: if ( r0 ) NEXT(r) = 0;
7027: get_eg(&eg2);
7028: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7029: if ( DP_Print ) {
7030: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
7031: eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
7032: fflush(asir_out);
7033: }
7034: return r0;
7035: }
7036:
7037:
7038: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7039: {
7040: IndArray *imat;
7041: int nsp,nred,i;
7042: int *rhead;
7043: NODE r0,rp;
7044: ND_pairs sp;
7045: NM_ind_pair *rvect;
7046: UINT *s;
7047: int *s0hash;
7048:
7049: if ( m == 2 && nd_rref2 )
7050: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7051:
7052: init_eg(&eg_search);
7053: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7054: nred = length(rp0);
7055: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7056: rhead = (int *)MALLOC(col*sizeof(int));
7057: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7058:
7059: /* construction of index arrays */
7060: if ( DP_Print ) {
7061: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7062: }
7063: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
7064: s0hash = (int *)MALLOC(col*sizeof(int));
7065: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7066: s0hash[i] = ndl_hash_value(s);
7067: for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
7068: rvect[i] = (NM_ind_pair)BDY(rp);
7069: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,s0hash,rvect[i]);
7070: rhead[imat[i]->head] = 1;
7071: }
7072: if ( m > 0 )
7073: #if defined(__GNUC__) && SIZEOF_LONG==8
7074: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7075: #else
7076: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7077: #endif
7078: else if ( m == -1 )
7079: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7080: else if ( m == -2 )
7081: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7082: else
7083: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7084: #if 0
7085: if ( DP_Print ) print_eg("search",&eg_search);
7086: #endif
7087: return r0;
7088: }
7089:
7090: /* for Fp, 2<=p<2^16 */
7091:
7092: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7093: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7094: {
7095: int spcol,sprow,a;
7096: int i,j,k,l,rank;
7097: NODE r0,r;
7098: ND_pairs sp;
7099: ND spol;
7100: UINT **spmat;
7101: UINT *svect,*v;
7102: int *colstat;
7103: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7104: int maxrs;
7105: int *spsugar;
7106: ND_pairs *spactive;
7107:
7108: spcol = col-nred;
7109: get_eg(&eg0);
7110: /* elimination (1st step) */
7111: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7112: svect = (UINT *)MALLOC(col*sizeof(UINT));
7113: spsugar = (int *)MALLOC(nsp*sizeof(int));
7114: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7115: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7116: nd_sp(m,0,sp,&spol);
7117: if ( !spol ) continue;
7118: nd_to_vect(m,s0vect,col,spol,svect);
7119: if ( m == -1 )
7120: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7121: else
7122: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7123: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7124: if ( i < col ) {
7125: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7126: for ( j = k = 0; j < col; j++ )
7127: if ( !rhead[j] ) v[k++] = svect[j];
7128: spsugar[sprow] = MAX(maxrs,SG(spol));
7129: if ( nz )
7130: spactive[sprow] = sp;
7131: sprow++;
7132: }
7133: nd_free(spol);
7134: }
7135: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7136: if ( DP_Print ) {
7137: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7138: fflush(asir_out);
7139: }
7140: /* free index arrays */
7141: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7142:
7143: /* elimination (2nd step) */
7144: colstat = (int *)MALLOC(spcol*sizeof(int));
7145: if ( m == -1 )
7146: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7147: else
7148: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7149: r0 = 0;
7150: for ( i = 0; i < rank; i++ ) {
7151: NEXTNODE(r0,r); BDY(r) =
7152: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7153: SG((NDV)BDY(r)) = spsugar[i];
7154: GCFREE(spmat[i]);
7155: }
7156: if ( r0 ) NEXT(r) = 0;
7157:
7158: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7159: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7160: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7161: if ( DP_Print ) {
7162: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7163: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7164: nsp,nred,sprow,spcol,rank);
7165: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7166: }
7167: if ( nz ) {
7168: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7169: if ( rank > 0 ) {
7170: NEXT(spactive[rank-1]) = 0;
7171: *nz = spactive[0];
7172: } else
7173: *nz = 0;
7174: }
7175: return r0;
7176: }
7177:
7178: #if defined(__GNUC__) && SIZEOF_LONG==8
7179: /* for Fp, 2^15=<p<2^29 */
7180:
7181: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7182: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7183: {
7184: int spcol,sprow,a;
7185: int i,j,k,l,rank;
7186: NODE r0,r;
7187: ND_pairs sp;
7188: ND spol;
7189: U64 **spmat;
7190: U64 *svect,*cvect;
7191: U64 *v;
7192: int *colstat;
7193: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7194: int maxrs;
7195: int *spsugar;
7196: ND_pairs *spactive;
7197:
7198: spcol = col-nred;
7199: get_eg(&eg0);
7200: /* elimination (1st step) */
7201: spmat = (U64 **)MALLOC(nsp*sizeof(U64 *));
7202: svect = (U64 *)MALLOC(col*sizeof(U64));
7203: cvect = (U64 *)MALLOC(col*sizeof(U64));
7204: spsugar = (int *)MALLOC(nsp*sizeof(int));
7205: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7206: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7207: nd_sp(m,0,sp,&spol);
7208: if ( !spol ) continue;
7209: nd_to_vect64(m,s0vect,col,spol,svect);
7210: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
7211: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7212: if ( i < col ) {
7213: spmat[sprow] = v = (U64 *)MALLOC_ATOMIC(spcol*sizeof(U64));
7214: for ( j = k = 0; j < col; j++ )
7215: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
7216: spsugar[sprow] = MAX(maxrs,SG(spol));
7217: if ( nz )
7218: spactive[sprow] = sp;
7219: sprow++;
7220: }
7221: nd_free(spol);
7222: }
7223: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7224: if ( DP_Print ) {
7225: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7226: fflush(asir_out);
7227: }
7228: /* free index arrays */
7229: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7230:
7231: /* elimination (2nd step) */
7232: colstat = (int *)MALLOC(spcol*sizeof(int));
7233: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7234: r0 = 0;
7235: for ( i = 0; i < rank; i++ ) {
7236: NEXTNODE(r0,r); BDY(r) =
7237: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7238: SG((NDV)BDY(r)) = spsugar[i];
7239: GCFREE(spmat[i]);
7240: }
7241: if ( r0 ) NEXT(r) = 0;
7242:
7243: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7244: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7245: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7246: if ( DP_Print ) {
7247: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7248: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7249: nsp,nred,sprow,spcol,rank);
7250: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7251: }
7252: if ( nz ) {
7253: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7254: if ( rank > 0 ) {
7255: NEXT(spactive[rank-1]) = 0;
7256: *nz = spactive[0];
7257: } else
7258: *nz = 0;
7259: }
7260: return r0;
7261: }
7262: #endif
7263:
7264: /* for small finite fields */
7265:
7266: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7267: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7268: {
7269: int spcol,sprow,a;
7270: int i,j,k,l,rank;
7271: NODE r0,r;
7272: ND_pairs sp;
7273: ND spol;
7274: UINT **spmat;
7275: UINT *svect,*v;
7276: int *colstat;
7277: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7278: int maxrs;
7279: int *spsugar;
7280: ND_pairs *spactive;
7281:
7282: spcol = col-nred;
7283: get_eg(&eg0);
7284: /* elimination (1st step) */
7285: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7286: svect = (UINT *)MALLOC(col*sizeof(UINT));
7287: spsugar = (int *)MALLOC(nsp*sizeof(int));
7288: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7289: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7290: nd_sp(m,0,sp,&spol);
7291: if ( !spol ) continue;
7292: nd_to_vect(m,s0vect,col,spol,svect);
7293: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7294: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7295: if ( i < col ) {
7296: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7297: for ( j = k = 0; j < col; j++ )
7298: if ( !rhead[j] ) v[k++] = svect[j];
7299: spsugar[sprow] = MAX(maxrs,SG(spol));
7300: if ( nz )
7301: spactive[sprow] = sp;
7302: sprow++;
7303: }
7304: nd_free(spol);
7305: }
7306: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7307: if ( DP_Print ) {
7308: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7309: fflush(asir_out);
7310: }
7311: /* free index arrays */
7312: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7313:
7314: /* elimination (2nd step) */
7315: colstat = (int *)MALLOC(spcol*sizeof(int));
7316: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7317: r0 = 0;
7318: for ( i = 0; i < rank; i++ ) {
7319: NEXTNODE(r0,r); BDY(r) =
7320: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7321: SG((NDV)BDY(r)) = spsugar[i];
7322: GCFREE(spmat[i]);
7323: }
7324: if ( r0 ) NEXT(r) = 0;
7325:
7326: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7327: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7328: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7329: if ( DP_Print ) {
7330: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7331: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7332: nsp,nred,sprow,spcol,rank);
7333: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7334: }
7335: if ( nz ) {
7336: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7337: if ( rank > 0 ) {
7338: NEXT(spactive[rank-1]) = 0;
7339: *nz = spactive[0];
7340: } else
7341: *nz = 0;
7342: }
7343: return r0;
7344: }
7345:
7346: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7347: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7348: {
7349: int spcol,sprow,a;
7350: int i,j,k,l,rank;
7351: NODE r0,r;
7352: ND_pairs sp;
7353: ND spol;
7354: mpz_t **spmat;
7355: mpz_t *svect,*v;
7356: int *colstat;
7357: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7358: int maxrs;
7359: int *spsugar;
7360: pointer *w;
7361:
7362: spcol = col-nred;
7363: get_eg(&eg0);
7364: /* elimination (1st step) */
7365: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7366: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7367: spsugar = (int *)MALLOC(nsp*sizeof(int));
7368: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7369: nd_sp(m,trace,sp,&spol);
7370: if ( !spol ) continue;
7371: nd_to_vect_lf(s0vect,col,spol,svect);
7372: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7373: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7374: if ( i < col ) {
7375: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7376: for ( j = k = 0; j < col; j++ )
7377: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7378: spsugar[sprow] = MAX(maxrs,SG(spol));
7379: sprow++;
7380: }
7381: /* nd_free(spol); */
7382: }
7383: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7384: if ( DP_Print ) {
7385: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7386: fflush(asir_out);
7387: }
7388: /* free index arrays */
7389: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7390:
7391: /* elimination (2nd step) */
7392: colstat = (int *)MALLOC(spcol*sizeof(int));
7393: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7394: w = (pointer *)MALLOC(rank*sizeof(pointer));
7395: for ( i = 0; i < rank; i++ ) {
7396: #if 0
7397: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7398: SG((NDV)w[rank-i-1]) = spsugar[i];
7399: #else
7400: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7401: SG((NDV)w[i]) = spsugar[i];
7402: #endif
7403: /* GCFREE(spmat[i]); */
7404:
7405: }
7406: #if 0
7407: qsort(w,rank,sizeof(NDV),
7408: (int (*)(const void *,const void *))ndv_compare);
7409: #endif
7410: r0 = 0;
7411: for ( i = 0; i < rank; i++ ) {
7412: NEXTNODE(r0,r); BDY(r) = w[i];
7413: }
7414: if ( r0 ) NEXT(r) = 0;
7415:
7416: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7417: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7418: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7419: if ( DP_Print ) {
7420: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7421: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7422: nsp,nred,sprow,spcol,rank);
7423: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7424: }
7425: return r0;
7426: }
7427:
7428: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7429: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7430: {
7431: int spcol,sprow,a;
7432: int i,j,k,l,rank;
7433: NODE r0,r;
7434: ND_pairs sp;
7435: ND spol;
7436: Z **spmat;
7437: Z *svect,*v;
7438: int *colstat;
7439: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7440: int maxrs;
7441: int *spsugar;
7442: pointer *w;
7443:
7444: spcol = col-nred;
7445: get_eg(&eg0);
7446: /* elimination (1st step) */
7447: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7448: svect = (Z *)MALLOC(col*sizeof(Q));
7449: spsugar = (int *)MALLOC(nsp*sizeof(int));
7450: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7451: nd_sp(0,trace,sp,&spol);
7452: if ( !spol ) continue;
7453: nd_to_vect_q(s0vect,col,spol,svect);
7454: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7455: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7456: if ( i < col ) {
7457: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7458: for ( j = k = 0; j < col; j++ )
7459: if ( !rhead[j] ) v[k++] = svect[j];
7460: spsugar[sprow] = MAX(maxrs,SG(spol));
7461: sprow++;
7462: }
7463: /* nd_free(spol); */
7464: }
7465: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7466: if ( DP_Print ) {
7467: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7468: fflush(asir_out);
7469: }
7470: /* free index arrays */
7471: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7472:
7473: /* elimination (2nd step) */
7474: colstat = (int *)MALLOC(spcol*sizeof(int));
7475: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7476: w = (pointer *)MALLOC(rank*sizeof(pointer));
7477: for ( i = 0; i < rank; i++ ) {
7478: #if 0
7479: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7480: SG((NDV)w[rank-i-1]) = spsugar[i];
7481: #else
7482: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7483: SG((NDV)w[i]) = spsugar[i];
7484: #endif
7485: /* GCFREE(spmat[i]); */
7486: }
7487: #if 0
7488: qsort(w,rank,sizeof(NDV),
7489: (int (*)(const void *,const void *))ndv_compare);
7490: #endif
7491: r0 = 0;
7492: for ( i = 0; i < rank; i++ ) {
7493: NEXTNODE(r0,r); BDY(r) = w[i];
7494: }
7495: if ( r0 ) NEXT(r) = 0;
7496:
7497: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7498: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7499: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7500: if ( DP_Print ) {
7501: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7502: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7503: nsp,nred,sprow,spcol,rank);
7504: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7505: }
7506: return r0;
7507: }
7508:
7509: FILE *nd_write,*nd_read;
7510:
7511: void nd_send_int(int a) {
7512: write_int(nd_write,(unsigned int *)&a);
7513: }
7514:
7515: void nd_send_intarray(int *p,int len) {
7516: write_intarray(nd_write,(unsigned int *)p,len);
7517: }
7518:
7519: int nd_recv_int() {
7520: int a;
7521:
7522: read_int(nd_read,(unsigned int *)&a);
7523: return a;
7524: }
7525:
7526: void nd_recv_intarray(int *p,int len) {
7527: read_intarray(nd_read,(unsigned int *)p,len);
7528: }
7529:
7530: void nd_send_ndv(NDV p) {
7531: int len,i;
7532: NMV m;
7533:
7534: if ( !p ) nd_send_int(0);
7535: else {
7536: len = LEN(p);
7537: nd_send_int(len);
7538: m = BDY(p);
7539: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7540: nd_send_int(CM(m));
7541: nd_send_intarray((int *)DL(m),nd_wpd);
7542: }
7543: }
7544: }
7545:
7546: void nd_send_nd(ND p) {
7547: int len,i;
7548: NM m;
7549:
7550: if ( !p ) nd_send_int(0);
7551: else {
7552: len = LEN(p);
7553: nd_send_int(len);
7554: m = BDY(p);
7555: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7556: nd_send_int(CM(m));
7557: nd_send_intarray((int *)DL(m),nd_wpd);
7558: }
7559: }
7560: }
7561:
7562: NDV nd_recv_ndv()
7563: {
7564: int len,i;
7565: NMV m,m0;
7566: NDV r;
7567:
7568: len = nd_recv_int();
7569: if ( !len ) return 0;
7570: else {
7571: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7572: #if 0
7573: ndv_alloc += len*nmv_adv;
7574: #endif
7575: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7576: CM(m) = nd_recv_int();
7577: nd_recv_intarray((int *)DL(m),nd_wpd);
7578: }
7579: MKNDV(nd_nvar,m0,len,r);
7580: return r;
7581: }
7582: }
7583:
7584: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7585: {
7586: int i,j,t,c,rank,inv;
7587: int *ci,*ri;
7588: Z dn;
7589: MAT m,nm;
7590:
7591: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7592: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7593: for ( i = 0; i < row; i++ )
7594: for ( j = 0; j < col; j++ )
7595: mat0[i][j] = 0;
7596: c = col-rank;
7597: for ( i = 0; i < rank; i++ ) {
7598: mat0[i][ri[i]] = dn;
7599: for ( j = 0; j < c; j++ )
7600: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7601: }
7602: return rank;
7603: }
7604:
7605: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7606: {
7607: int i,j,k,l,inv,a,rank,s;
7608: unsigned int *t,*pivot,*pk;
7609: unsigned int **mat;
7610: ND_pairs pair;
7611:
7612: mat = (unsigned int **)mat0;
7613: for ( rank = 0, j = 0; j < col; j++ ) {
7614: for ( i = rank; i < row; i++ )
7615: mat[i][j] %= md;
7616: for ( i = rank; i < row; i++ )
7617: if ( mat[i][j] )
7618: break;
7619: if ( i == row ) {
7620: colstat[j] = 0;
7621: continue;
7622: } else
7623: colstat[j] = 1;
7624: if ( i != rank ) {
7625: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7626: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7627: if ( spactive ) {
7628: pair = spactive[i]; spactive[i] = spactive[rank];
7629: spactive[rank] = pair;
7630: }
7631: }
7632: pivot = mat[rank];
7633: s = sugar[rank];
7634: inv = invm(pivot[j],md);
7635: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7636: if ( *pk ) {
7637: if ( *pk >= (unsigned int)md )
7638: *pk %= md;
7639: DMAR(*pk,inv,0,md,*pk)
7640: }
7641: for ( i = rank+1; i < row; i++ ) {
7642: t = mat[i];
7643: if ( (a = t[j]) != 0 ) {
7644: sugar[i] = MAX(sugar[i],s);
7645: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7646: }
7647: }
7648: rank++;
7649: }
7650: for ( j = col-1, l = rank-1; j >= 0; j-- )
7651: if ( colstat[j] ) {
7652: pivot = mat[l];
7653: s = sugar[l];
7654: for ( i = 0; i < l; i++ ) {
7655: t = mat[i];
7656: t[j] %= md;
7657: if ( (a = t[j]) != 0 ) {
7658: sugar[i] = MAX(sugar[i],s);
7659: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7660: }
7661: }
7662: l--;
7663: }
7664: for ( j = 0, l = 0; l < rank; j++ )
7665: if ( colstat[j] ) {
7666: t = mat[l];
7667: for ( k = j; k < col; k++ )
7668: if ( t[k] >= (unsigned int)md )
7669: t[k] %= md;
7670: l++;
7671: }
7672: return rank;
7673: }
7674:
7675: #if defined(__GNUC__) && SIZEOF_LONG==8
7676:
7677: int nd_gauss_elim_mod64(U64 **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7678: {
7679: int i,j,k,l,rank,s;
7680: U64 inv;
7681: U64 a;
7682: UINT c;
7683: U64 *t,*pivot,*pk;
7684: UINT *ck;
7685: UINT **cmat;
7686: UINT *ct;
7687: ND_pairs pair;
7688:
7689: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
7690: for ( i = 0; i < row; i++ ) {
7691: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
7692: bzero(cmat[i],col*sizeof(UINT));
7693: }
7694:
7695: for ( rank = 0, j = 0; j < col; j++ ) {
7696: for ( i = rank; i < row; i++ ) {
7697: a = mat[i][j]; c = cmat[i][j];
7698: MOD128(a,c,md);
7699: mat[i][j] = a; cmat[i][j] = 0;
7700: }
7701: for ( i = rank; i < row; i++ )
7702: if ( mat[i][j] )
7703: break;
7704: if ( i == row ) {
7705: colstat[j] = 0;
7706: continue;
7707: } else
7708: colstat[j] = 1;
7709: if ( i != rank ) {
7710: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7711: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
7712: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7713: if ( spactive ) {
7714: pair = spactive[i]; spactive[i] = spactive[rank];
7715: spactive[rank] = pair;
7716: }
7717: }
7718: /* column j is normalized */
7719: s = sugar[rank];
7720: inv = invm((UINT)mat[rank][j],md);
7721: /* normalize pivot row */
7722: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
7723: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
7724: }
7725: for ( i = rank+1; i < row; i++ ) {
7726: if ( (a = mat[i][j]) != 0 ) {
7727: sugar[i] = MAX(sugar[i],s);
7728: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
7729: }
7730: }
7731: rank++;
7732: }
7733: for ( j = col-1, l = rank-1; j >= 0; j-- )
7734: if ( colstat[j] ) {
7735: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
7736: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
7737: }
7738: s = sugar[l];
7739: for ( i = 0; i < l; i++ ) {
7740: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
7741: if ( a ) {
7742: sugar[i] = MAX(sugar[i],s);
7743: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
7744: }
7745: }
7746: l--;
7747: }
7748: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
7749: GCFREE(cmat);
7750: return rank;
7751: }
7752: #endif
7753:
7754: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
7755: {
7756: int i,j,k,l,inv,a,rank,s;
7757: unsigned int *t,*pivot,*pk;
7758: unsigned int **mat;
7759:
7760: mat = (unsigned int **)mat0;
7761: for ( rank = 0, j = 0; j < col; j++ ) {
7762: for ( i = rank; i < row; i++ )
7763: if ( mat[i][j] )
7764: break;
7765: if ( i == row ) {
7766: colstat[j] = 0;
7767: continue;
7768: } else
7769: colstat[j] = 1;
7770: if ( i != rank ) {
7771: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7772: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7773: }
7774: pivot = mat[rank];
7775: s = sugar[rank];
7776: inv = _invsf(pivot[j]);
7777: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7778: if ( *pk )
7779: *pk = _mulsf(*pk,inv);
7780: for ( i = rank+1; i < row; i++ ) {
7781: t = mat[i];
7782: if ( (a = t[j]) != 0 ) {
7783: sugar[i] = MAX(sugar[i],s);
7784: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7785: }
7786: }
7787: rank++;
7788: }
7789: for ( j = col-1, l = rank-1; j >= 0; j-- )
7790: if ( colstat[j] ) {
7791: pivot = mat[l];
7792: s = sugar[l];
7793: for ( i = 0; i < l; i++ ) {
7794: t = mat[i];
7795: if ( (a = t[j]) != 0 ) {
7796: sugar[i] = MAX(sugar[i],s);
7797: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7798: }
7799: }
7800: l--;
7801: }
7802: return rank;
7803: }
7804:
7805: int ndv_ishomo(NDV p)
7806: {
7807: NMV m;
7808: int len,h;
7809:
7810: if ( !p ) return 1;
7811: len = LEN(p);
7812: m = BDY(p);
7813: h = TD(DL(m));
7814: NMV_ADV(m);
7815: for ( len--; len; len--, NMV_ADV(m) )
7816: if ( TD(DL(m)) != h ) return 0;
7817: return 1;
7818: }
7819:
7820: void ndv_save(NDV p,int index)
7821: {
7822: FILE *s;
7823: char name[BUFSIZ];
7824: short id;
7825: int nv,sugar,len,n,i,td,e,j;
7826: NMV m;
7827: unsigned int *dl;
7828: int mpos;
7829:
7830: sprintf(name,"%s/%d",Demand,index);
7831: s = fopen(name,"w");
7832: savevl(s,0);
7833: if ( !p ) {
7834: saveobj(s,0);
7835: return;
7836: }
7837: id = O_DP;
7838: nv = NV(p);
7839: sugar = SG(p);
7840: len = LEN(p);
7841: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7842: write_int(s,(unsigned int *)&len);
7843:
7844: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7845: saveobj(s,(Obj)CQ(m));
7846: dl = DL(m);
7847: td = TD(dl);
7848: write_int(s,(unsigned int *)&td);
7849: for ( j = 0; j < nv; j++ ) {
7850: e = GET_EXP(dl,j);
7851: write_int(s,(unsigned int *)&e);
7852: }
7853: if ( nd_module ) {
7854: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7855: }
7856: }
7857: fclose(s);
7858: }
7859:
7860: void nd_save_mod(ND p,int index)
7861: {
7862: FILE *s;
7863: char name[BUFSIZ];
7864: int nv,sugar,len,c;
7865: NM m;
7866:
7867: sprintf(name,"%s/%d",Demand,index);
7868: s = fopen(name,"w");
7869: if ( !p ) {
7870: len = 0;
7871: write_int(s,(unsigned int *)&len);
7872: fclose(s);
7873: return;
7874: }
7875: nv = NV(p);
7876: sugar = SG(p);
7877: len = LEN(p);
7878: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7879: for ( m = BDY(p); m; m = NEXT(m) ) {
7880: c = CM(m); write_int(s,(unsigned int *)&c);
7881: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7882: }
7883: fclose(s);
7884: }
7885:
7886: NDV ndv_load(int index)
7887: {
7888: FILE *s;
7889: char name[BUFSIZ];
7890: short id;
7891: int nv,sugar,len,n,i,td,e,j;
7892: NDV d;
7893: NMV m0,m;
7894: unsigned int *dl;
7895: Obj obj;
7896: int mpos;
7897:
7898: sprintf(name,"%s/%d",Demand,index);
7899: s = fopen(name,"r");
7900: if ( !s ) return 0;
7901:
7902: skipvl(s);
7903: read_short(s,(unsigned short *)&id);
7904: if ( !id ) return 0;
7905: read_int(s,(unsigned int *)&nv);
7906: read_int(s,(unsigned int *)&sugar);
7907: read_int(s,(unsigned int *)&len);
7908:
7909: m0 = m = MALLOC(len*nmv_adv);
7910: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7911: loadobj(s,&obj); CQ(m) = (Z)obj;
7912: dl = DL(m);
7913: ndl_zero(dl);
7914: read_int(s,(unsigned int *)&td); TD(dl) = td;
7915: for ( j = 0; j < nv; j++ ) {
7916: read_int(s,(unsigned int *)&e);
7917: PUT_EXP(dl,j,e);
7918: }
7919: if ( nd_module ) {
7920: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7921: }
7922: if ( nd_blockmask ) ndl_weight_mask(dl);
7923: }
7924: fclose(s);
7925: MKNDV(nv,m0,len,d);
7926: SG(d) = sugar;
7927: return d;
7928: }
7929:
7930: ND nd_load_mod(int index)
7931: {
7932: FILE *s;
7933: char name[BUFSIZ];
7934: int nv,sugar,len,i,c;
7935: ND d;
7936: NM m0,m;
7937:
7938: sprintf(name,"%s/%d",Demand,index);
7939: s = fopen(name,"r");
7940: /* if the file does not exist, it means p[index]=0 */
7941: if ( !s ) return 0;
7942:
7943: read_int(s,(unsigned int *)&nv);
7944: if ( !nv ) { fclose(s); return 0; }
7945:
7946: read_int(s,(unsigned int *)&sugar);
7947: read_int(s,(unsigned int *)&len);
7948: for ( m0 = 0, i = 0; i < len; i++ ) {
7949: NEXTNM(m0,m);
7950: read_int(s,(unsigned int *)&c); CM(m) = c;
7951: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7952: }
7953: NEXT(m) = 0;
7954: MKND(nv,m0,len,d);
7955: SG(d) = sugar;
7956: fclose(s);
7957: return d;
7958: }
7959:
7960: void nd_det(int mod,MAT f,P *rp)
7961: {
7962: VL fv,tv;
7963: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7964: pointer **m;
7965: P **w;
7966: P mp,r;
7967: NDV **dm;
7968: NDV *t,*mi,*mj;
7969: NDV d,s,mij,mjj;
7970: ND u;
7971: NMV nmv;
7972: UINT *bound;
7973: PGeoBucket bucket;
7974: struct order_spec *ord;
7975: Z dq,dt,ds;
7976: Z mone;
7977: Z gn,qn,dn0,nm,dn;
7978:
7979: create_order_spec(0,0,&ord);
7980: nd_init_ord(ord);
7981: get_vars((Obj)f,&fv);
7982: if ( f->row != f->col )
7983: error("nd_det : non-square matrix");
7984: n = f->row;
7985: m = f->body;
7986: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7987:
7988: if ( !nvar ) {
7989: if ( !mod )
7990: detp(CO,(P **)m,n,rp);
7991: else {
7992: w = (P **)almat_pointer(n,n);
7993: for ( i = 0; i < n; i++ )
7994: for ( j = 0; j < n; j++ )
7995: ptomp(mod,(P)m[i][j],&w[i][j]);
7996: detmp(CO,mod,w,n,&mp);
7997: mptop(mp,rp);
7998: }
7999: return;
8000: }
8001:
8002: if ( !mod ) {
8003: w = (P **)almat_pointer(n,n);
8004: dq = ONE;
8005: for ( i = 0; i < n; i++ ) {
8006: dn0 = ONE;
8007: for ( j = 0; j < n; j++ ) {
8008: if ( !m[i][j] ) continue;
8009: lgp(m[i][j],&nm,&dn);
8010: gcdz(dn0,dn,&gn); divz(dn0,gn,&qn); mulz(qn,dn,&dn0);
8011: }
8012: if ( !UNIZ(dn0) ) {
8013: ds = dn0;
8014: for ( j = 0; j < n; j++ )
8015: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8016: mulz(dq,ds,&dt); dq = dt;
8017: } else
8018: for ( j = 0; j < n; j++ )
8019: w[i][j] = (P)m[i][j];
8020: }
8021: m = (pointer **)w;
8022: }
8023:
8024: for ( i = 0, max = 1; i < n; i++ )
8025: for ( j = 0; j < n; j++ )
8026: for ( tv = fv; tv; tv = NEXT(tv) ) {
8027: e = getdeg(tv->v,(P)m[i][j]);
8028: max = MAX(e,max);
8029: }
8030: nd_setup_parameters(nvar,max);
8031: dm = (NDV **)almat_pointer(n,n);
8032: for ( i = 0, max = 1; i < n; i++ )
8033: for ( j = 0; j < n; j++ ) {
8034: dm[i][j] = ptondv(CO,fv,m[i][j]);
8035: if ( mod ) ndv_mod(mod,dm[i][j]);
8036: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8037: }
8038: d = ptondv(CO,fv,(P)ONE);
8039: if ( mod ) ndv_mod(mod,d);
8040: chsgnz(ONE,&mone);
8041: for ( j = 0, sgn = 1; j < n; j++ ) {
8042: if ( DP_Print ) {
8043: fprintf(asir_out,".");
8044: }
8045: for ( i = j; i < n && !dm[i][j]; i++ );
8046: if ( i == n ) {
8047: *rp = 0;
8048: return;
8049: }
8050: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8051: for ( k = j; k < n; k++ )
8052: for ( l = j; l < n; l++ )
8053: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8054: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8055: }
8056: if ( k0 != j ) {
8057: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8058: sgn = -sgn;
8059: }
8060: if ( l0 != j ) {
8061: for ( k = j; k < n; k++ ) {
8062: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8063: }
8064: sgn = -sgn;
8065: }
8066: bound = nd_det_compute_bound(dm,n,j);
8067: for ( k = 0; k < nd_nvar; k++ )
8068: if ( bound[k]*2 > nd_mask0 ) break;
8069: if ( k < nd_nvar )
8070: nd_det_reconstruct(dm,n,j,d);
8071:
8072: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8073: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8074: mi = dm[i]; mij = mi[j];
8075: if ( mod )
8076: ndv_mul_c(mod,mij,mod-1);
8077: else
8078: ndv_mul_c_q(mij,mone);
8079: for ( k = j+1; k < n; k++ ) {
8080: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
8081: bucket = create_pbucket();
8082: if ( mi[k] ) {
8083: nmv = BDY(mjj); len = LEN(mjj);
8084: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8085: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8086: add_pbucket(mod,bucket,u);
8087: }
8088: }
8089: if ( mj[k] && mij ) {
8090: nmv = BDY(mij); len = LEN(mij);
8091: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8092: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8093: add_pbucket(mod,bucket,u);
8094: }
8095: }
8096: u = nd_quo(mod,bucket,d);
8097: mi[k] = ndtondv(mod,u);
8098: }
8099: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
8100: }
8101: d = mjj;
8102: }
8103: if ( DP_Print ) {
8104: fprintf(asir_out,"\n");
8105: }
8106: if ( sgn < 0 ) {
8107: if ( mod )
8108: ndv_mul_c(mod,d,mod-1);
8109: else
8110: ndv_mul_c_q(d,mone);
8111: }
8112: r = ndvtop(mod,CO,fv,d);
8113: if ( !mod && !UNIQ(dq) )
8114: divsp(CO,r,(P)dq,rp);
8115: else
8116: *rp = r;
8117: }
8118:
8119: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8120: {
8121: NM mr,mr0;
8122: NM tnm;
8123: NMV m;
8124: UINT *d0,*dt,*dm;
8125: int c,n,td,i,c1,c2,len;
8126: Z q;
8127: ND r;
8128:
8129: if ( !p ) return 0;
8130: else {
8131: n = NV(p); m = BDY(p); len = LEN(p);
8132: d0 = DL(m0);
8133: td = TD(d);
8134: mr0 = 0;
8135: NEWNM(tnm);
8136: if ( mod ) {
8137: c = CM(m0);
8138: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8139: ndl_add(DL(m),d0,DL(tnm));
8140: if ( ndl_reducible(DL(tnm),d) ) {
8141: NEXTNM(mr0,mr);
8142: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8143: ndl_copy(DL(tnm),DL(mr));
8144: }
8145: }
8146: } else {
8147: q = CQ(m0);
8148: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8149: ndl_add(DL(m),d0,DL(tnm));
8150: if ( ndl_reducible(DL(tnm),d) ) {
8151: NEXTNM(mr0,mr);
8152: mulz(CQ(m),q,&CQ(mr));
8153: ndl_copy(DL(tnm),DL(mr));
8154: }
8155: }
8156: }
8157: if ( !mr0 )
8158: return 0;
8159: else {
8160: NEXT(mr) = 0;
8161: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8162: MKND(NV(p),mr0,len,r);
8163: SG(r) = SG(p) + TD(d0);
8164: return r;
8165: }
8166: }
8167: }
8168:
8169: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8170: {
8171: int i,obpe,oadv,h,k,l;
8172: static NM prev_nm_free_list;
8173: EPOS oepos;
8174:
8175: obpe = nd_bpe;
8176: oadv = nmv_adv;
8177: oepos = nd_epos;
8178: if ( obpe < 2 ) nd_bpe = 2;
8179: else if ( obpe < 3 ) nd_bpe = 3;
8180: else if ( obpe < 4 ) nd_bpe = 4;
8181: else if ( obpe < 5 ) nd_bpe = 5;
8182: else if ( obpe < 6 ) nd_bpe = 6;
8183: else if ( obpe < 8 ) nd_bpe = 8;
8184: else if ( obpe < 10 ) nd_bpe = 10;
8185: else if ( obpe < 16 ) nd_bpe = 16;
8186: else if ( obpe < 32 ) nd_bpe = 32;
8187: else error("nd_det_reconstruct : exponent too large");
8188:
8189: nd_setup_parameters(nd_nvar,0);
8190: prev_nm_free_list = _nm_free_list;
8191: _nm_free_list = 0;
8192: for ( k = j; k < n; k++ )
8193: for (l = j; l < n; l++ )
8194: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8195: ndv_realloc(d,obpe,oadv,oepos);
8196: prev_nm_free_list = 0;
8197: #if 0
8198: GC_gcollect();
8199: #endif
8200: }
8201:
8202: /* returns a UINT array containing degree bounds */
8203:
8204: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8205: {
8206: UINT *d0,*d1,*d,*t,*r;
8207: int k,l,i;
8208:
8209: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8210: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8211: for ( k = j; k < n; k++ )
8212: for ( l = j; l < n; l++ )
8213: if ( dm[k][l] ) {
8214: d = ndv_compute_bound(dm[k][l]);
8215: for ( i = 0; i < nd_nvar; i++ )
8216: d0[i] = MAX(d0[i],d[i]);
8217: }
8218: return d0;
8219: }
8220:
8221: DL nd_separate_d(UINT *d,UINT *trans)
8222: {
8223: int n,td,i,e,j;
8224: DL a;
8225:
8226: ndl_zero(trans);
8227: td = 0;
8228: for ( i = 0; i < nd_ntrans; i++ ) {
8229: e = GET_EXP(d,i);
8230: PUT_EXP(trans,i,e);
8231: td += MUL_WEIGHT(e,i);
8232: }
8233: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8234: /* homogenized */
8235: i = nd_nvar-1;
8236: e = GET_EXP(d,i);
8237: PUT_EXP(trans,i,e);
8238: td += MUL_WEIGHT(e,i);
8239: }
8240: TD(trans) = td;
8241: if ( nd_blockmask) ndl_weight_mask(trans);
8242: NEWDL(a,nd_nalg);
8243: td = 0;
8244: for ( i = 0; i < nd_nalg; i++ ) {
8245: j = nd_ntrans+i;
8246: e = GET_EXP(d,j);
8247: a->d[i] = e;
8248: td += e;
8249: }
8250: a->td = td;
8251: return a;
8252: }
8253:
8254: int nd_monic(int mod,ND *p)
8255: {
8256: UINT *trans,*t;
8257: DL alg;
8258: MP mp0,mp;
8259: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8260: ND r;
8261: DL dl;
8262: DP nm;
8263: NDV ndv;
8264: DAlg inv,cd;
8265: ND s,c;
8266: Z l,mul;
8267: Z ln;
8268: int n,ntrans,i,e,td,is_lc,len;
8269: NumberField nf;
8270: struct oEGT eg0,eg1;
8271:
8272: if ( !(nf = get_numberfield()) )
8273: error("nd_monic : current_numberfield is not set");
8274:
8275: /* Q coef -> DAlg coef */
8276: NEWNM(ma0); ma = ma0;
8277: m = BDY(*p);
8278: is_lc = 1;
8279: while ( 1 ) {
8280: NEWMP(mp0); mp = mp0;
8281: mp->c = (Obj)CQ(m);
8282: mp->dl = nd_separate_d(DL(m),DL(ma));
8283: NEWNM(mb);
8284: for ( m = NEXT(m); m; m = NEXT(m) ) {
8285: alg = nd_separate_d(DL(m),DL(mb));
8286: if ( !ndl_equal(DL(ma),DL(mb)) )
8287: break;
8288: NEXTMP(mp0,mp); mp->c = (Obj)CQ(m); mp->dl = alg;
8289: }
8290: NEXT(mp) = 0;
8291: MKDP(nd_nalg,mp0,nm);
8292: MKDAlg(nm,ONE,cd);
8293: if ( is_lc == 1 ) {
8294: /* if the lc is a rational number, we have nothing to do */
8295: if ( !mp0->dl->td )
8296: return 1;
8297:
8298: get_eg(&eg0);
8299: invdalg(cd,&inv);
8300: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8301: /* check the validity of inv */
8302: if ( mod && !remqi((Q)inv->dn,mod) )
8303: return 0;
8304: CA(ma) = nf->one;
8305: is_lc = 0;
8306: ln = ONE;
8307: } else {
8308: muldalg(cd,inv,&CA(ma));
8309: lcmz(ln,CA(ma)->dn,&ln);
8310: }
8311: if ( m ) {
8312: NEXT(ma) = mb; ma = mb;
8313: } else {
8314: NEXT(ma) = 0;
8315: break;
8316: }
8317: }
8318: /* l = lcm(denoms) */
8319: l = ln;
8320: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
8321: divz(l,CA(m)->dn,&mul);
8322: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8323: NEXTNM(mr0,mr);
8324: mulz((Z)mp->c,mul,&CQ(mr));
8325: dl = mp->dl;
8326: td = TD(DL(m));
8327: ndl_copy(DL(m),DL(mr));
8328: for ( i = 0; i < nd_nalg; i++ ) {
8329: e = dl->d[i];
8330: PUT_EXP(DL(mr),i+nd_ntrans,e);
8331: td += MUL_WEIGHT(e,i+nd_ntrans);
8332: }
8333: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8334: TD(DL(mr)) = td;
8335: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8336: }
8337: }
8338: NEXT(mr) = 0;
8339: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8340: MKND(NV(*p),mr0,len,r);
8341: /* XXX */
8342: SG(r) = SG(*p);
8343: nd_free(*p);
8344: *p = r;
8345: return 1;
8346: }
8347:
8348: NODE reverse_node(NODE n)
8349: {
8350: NODE t,t1;
8351:
8352: for ( t = 0; n; n = NEXT(n) ) {
8353: MKNODE(t1,BDY(n),t); t = t1;
8354: }
8355: return t;
8356: }
8357:
8358: P ndc_div(int mod,union oNDC a,union oNDC b)
8359: {
8360: union oNDC c;
8361: int inv,t;
8362:
8363: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
8364: else if ( mod == -2 ) divlf(a.gz,b.gz,&c.gz);
8365: else if ( mod ) {
8366: inv = invm(b.m,mod);
8367: DMAR(a.m,inv,0,mod,t); c.m = t;
8368: } else if ( nd_vc )
8369: divsp(nd_vc,a.p,b.p,&c.p);
8370: else
8371: divsz(a.z,b.z,&c.z);
8372: return ndctop(mod,c);
8373: }
8374:
8375: P ndctop(int mod,union oNDC c)
8376: {
8377: Z q;
8378: int e;
8379: GFS gfs;
8380:
8381: if ( mod == -1 ) {
8382: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8383: } else if ( mod == -2 ) {
8384: q = c.gz; return (P)q;
8385: } else if ( mod > 0 ) {
8386: STOQ(c.m,q); return (P)q;
8387: } else
8388: return (P)c.p;
8389: }
8390:
8391: /* [0,0,0,cont] = p -> p/cont */
8392:
8393: void finalize_tracelist(int i,P cont)
8394: {
8395: LIST l;
8396: NODE node;
8397: Z iq;
8398:
8399: if ( !UNIQ(cont) ) {
8400: node = mknode(4,NULLP,NULLP,NULLP,cont);
8401: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8402: nd_tracelist = node;
8403: }
8404: STOQ(i,iq);
8405: nd_tracelist = reverse_node(nd_tracelist);
8406: MKLIST(l,nd_tracelist);
8407: node = mknode(2,iq,l); MKLIST(l,node);
8408: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8409: nd_alltracelist = node; nd_tracelist = 0;
8410: }
8411:
8412: void conv_ilist(int demand,int trace,NODE g,int **indp)
8413: {
8414: int n,i,j;
8415: int *ind;
8416: NODE t;
8417:
8418: n = length(g);
8419: ind = (int *)MALLOC(n*sizeof(int));
8420: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8421: j = (long)BDY(t); ind[i] = j;
8422: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8423: }
8424: if ( indp ) *indp = ind;
8425: }
8426:
8427: void parse_nd_option(NODE opt)
8428: {
8429: NODE t,p,u;
8430: int i,s,n;
8431: char *key;
8432: Obj value;
8433:
8434: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8435: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8436: nd_splist = 0; nd_check_splist = 0;
8437: nd_sugarweight = 0;
8438: nd_f4red =0;
8439: nd_rank0 = 0;
8440: for ( t = opt; t; t = NEXT(t) ) {
8441: p = BDY((LIST)BDY(t));
8442: key = BDY((STRING)BDY(p));
8443: value = (Obj)BDY(NEXT(p));
8444: if ( !strcmp(key,"gentrace") )
8445: nd_gentrace = value?1:0;
8446: else if ( !strcmp(key,"gensyz") )
8447: nd_gensyz = value?1:0;
8448: else if ( !strcmp(key,"nora") )
8449: nd_nora = value?1:0;
8450: else if ( !strcmp(key,"gbblock") ) {
8451: if ( value && OID(value) == O_LIST ) {
8452: u = BDY((LIST)value);
8453: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8454: for ( i = 0; u; u = NEXT(u) ) {
8455: p = BDY((LIST)BDY(u));
8456: s = nd_gbblock[i++] = QTOS((Q)BDY(p));
8457: nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
8458: }
8459: nd_gbblock[i] = -1;
8460: } else
8461: nd_gbblock = 0;
8462: } else if ( !strcmp(key,"newelim") )
8463: nd_newelim = value?1:0;
8464: else if ( !strcmp(key,"intersect") )
8465: nd_intersect = value?1:0;
8466: else if ( !strcmp(key,"lf") )
8467: nd_lf = value?1:0;
8468: else if ( !strcmp(key,"trace") ) {
8469: if ( value ) {
8470: u = BDY((LIST)value);
8471: nd_nzlist = BDY((LIST)ARG2(u));
8472: nd_bpe = QTOS((Q)ARG3(u));
8473: }
8474: } else if ( !strcmp(key,"f4red") ) {
8475: nd_f4red = QTOS((Q)value);
8476: } else if ( !strcmp(key,"rank0") ) {
8477: nd_rank0 = value?1:0;
8478: } else if ( !strcmp(key,"splist") ) {
8479: nd_splist = value?1:0;
8480: } else if ( !strcmp(key,"check_splist") ) {
8481: nd_check_splist = BDY((LIST)value);
8482: } else if ( !strcmp(key,"sugarweight") ) {
8483: u = BDY((LIST)value);
8484: n = length(u);
8485: nd_sugarweight = MALLOC(n*sizeof(int));
8486: for ( i = 0; i < n; i++, u = NEXT(u) )
8487: nd_sugarweight[i] = QTOS((Q)BDY(u));
8488: }
8489: }
8490: }
8491:
8492: ND mdptond(DP d);
8493: ND nd_mul_nm(int mod,NM m0,ND p);
8494: ND nd_mul_nm_lf(NM m0,ND p);
8495: ND *btog(NODE ti,ND **p,int nb,int mod);
8496: ND btog_one(NODE ti,ND *p,int nb,int mod);
8497: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8498: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8499:
8500: /* d:monomial */
8501: ND mdptond(DP d)
8502: {
8503: NM m;
8504: ND r;
8505:
8506: if ( OID(d) == 1 )
8507: r = ptond(CO,CO,(P)d);
8508: else {
8509: NEWNM(m);
8510: dltondl(NV(d),BDY(d)->dl,DL(m));
8511: CQ(m) = (Z)BDY(d)->c;
8512: NEXT(m) = 0;
8513: MKND(NV(d),m,1,r);
8514: }
8515: return r;
8516: }
8517:
8518: ND nd_mul_nm(int mod,NM m0,ND p)
8519: {
8520: UINT *d0;
8521: int c0,c1,c;
8522: NM tm,mr,mr0;
8523: ND r;
8524:
8525: if ( !p ) return 0;
8526: d0 = DL(m0);
8527: c0 = CM(m0);
8528: mr0 = 0;
8529: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8530: NEXTNM(mr0,mr);
8531: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8532: ndl_add(d0,DL(tm),DL(mr));
8533: }
8534: NEXT(mr) = 0;
8535: MKND(NV(p),mr0,LEN(p),r);
8536: return r;
8537: }
8538:
8539: ND nd_mul_nm_lf(NM m0,ND p)
8540: {
8541: UINT *d0;
8542: Z c0,c1,c;
8543: NM tm,mr,mr0;
8544: ND r;
8545:
8546: if ( !p ) return 0;
8547: d0 = DL(m0);
8548: c0 = CZ(m0);
8549: mr0 = 0;
8550: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8551: NEXTNM(mr0,mr);
8552: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8553: ndl_add(d0,DL(tm),DL(mr));
8554: }
8555: NEXT(mr) = 0;
8556: MKND(NV(p),mr0,LEN(p),r);
8557: return r;
8558: }
8559:
8560: ND *btog(NODE ti,ND **p,int nb,int mod)
8561: {
8562: PGeoBucket *r;
8563: int i,ci;
8564: NODE t,s;
8565: ND m,tp;
8566: ND *pi,*rd;
8567: P c;
8568:
8569: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8570: for ( i = 0; i < nb; i++ )
8571: r[i] = create_pbucket();
8572: for ( t = ti; t; t = NEXT(t) ) {
8573: s = BDY((LIST)BDY(t));
8574: if ( ARG0(s) ) {
8575: m = mdptond((DP)ARG2(s));
8576: ptomp(mod,(P)HCQ(m),&c);
8577: if ( (ci = ((MQ)c)->cont) != 0 ) {
8578: HCM(m) = ci;
8579: pi = p[QTOS((Q)ARG1(s))];
8580: for ( i = 0; i < nb; i++ ) {
8581: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8582: add_pbucket(mod,r[i],tp);
8583: }
8584: }
8585: ci = 1;
8586: } else {
8587: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8588: ci = invm(ci,mod);
8589: }
8590: }
8591: rd = (ND *)MALLOC(nb*sizeof(ND));
8592: for ( i = 0; i < nb; i++ )
8593: rd[i] = normalize_pbucket(mod,r[i]);
8594: if ( ci != 1 )
8595: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8596: return rd;
8597: }
8598:
8599: /* YYY */
8600: ND *btog_lf(NODE ti,ND **p,int nb)
8601: {
8602: PGeoBucket *r;
8603: int i;
8604: NODE t,s;
8605: ND m,tp;
8606: ND *pi,*rd;
8607: LM lm;
8608: Z lf,c;
8609:
8610: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8611: for ( i = 0; i < nb; i++ )
8612: r[i] = create_pbucket();
8613: for ( t = ti; t; t = NEXT(t) ) {
8614: s = BDY((LIST)BDY(t));
8615: if ( ARG0(s) ) {
8616: m = mdptond((DP)ARG2(s));
8617: simp_ff((Obj)HCQ(m),(Obj *)&lm);
8618: if ( lm ) {
8619: lmtolf(lm,&lf); HCZ(m) = lf;
8620: pi = p[QTOS((Q)ARG1(s))];
8621: for ( i = 0; i < nb; i++ ) {
8622: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8623: add_pbucket(-2,r[i],tp);
8624: }
8625: }
8626: c = ONE;
8627: } else {
8628: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8629: }
8630: }
8631: rd = (ND *)MALLOC(nb*sizeof(ND));
8632: for ( i = 0; i < nb; i++ )
8633: rd[i] = normalize_pbucket(-2,r[i]);
8634: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8635: return rd;
8636: }
8637:
8638: ND btog_one(NODE ti,ND *p,int nb,int mod)
8639: {
8640: PGeoBucket r;
8641: int i,ci,j;
8642: NODE t,s;
8643: ND m,tp;
8644: ND pi,rd;
8645: P c;
8646:
8647: r = create_pbucket();
8648: for ( t = ti; t; t = NEXT(t) ) {
8649: s = BDY((LIST)BDY(t));
8650: if ( ARG0(s) ) {
8651: m = mdptond((DP)ARG2(s));
8652: ptomp(mod,(P)HCQ(m),&c);
8653: if ( (ci = ((MQ)c)->cont) != 0 ) {
8654: HCM(m) = ci;
8655: pi = p[j=QTOS((Q)ARG1(s))];
8656: if ( !pi ) {
8657: pi = nd_load_mod(j);
8658: tp = nd_mul_nm(mod,BDY(m),pi);
8659: nd_free(pi);
8660: add_pbucket(mod,r,tp);
8661: } else {
8662: tp = nd_mul_nm(mod,BDY(m),pi);
8663: add_pbucket(mod,r,tp);
8664: }
8665: }
8666: ci = 1;
8667: } else {
8668: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8669: ci = invm(ci,mod);
8670: }
8671: }
8672: rd = normalize_pbucket(mod,r);
8673: free_pbucket(r);
8674: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8675: return rd;
8676: }
8677:
8678: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8679:
8680: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8681: {
8682: int i,j,n,m,nb,pi0,pi1,nvar;
8683: VL fv,tv,vv;
8684: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8685: ND **p;
8686: ND *c;
8687: ND u;
8688: P inv;
8689: MAT mat;
8690:
8691: if ( mod == -2 )
8692: return nd_btog_lf(f,v,ord,tlist,rp);
8693:
8694: parse_nd_option(current_option);
8695: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8696: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8697: switch ( ord->id ) {
8698: case 1:
8699: if ( ord->nv != nvar )
8700: error("nd_check : invalid order specification");
8701: break;
8702: default:
8703: break;
8704: }
8705: nd_init_ord(ord);
8706: #if 0
8707: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8708: #else
8709: nd_bpe = 32;
8710: #endif
8711: nd_setup_parameters(nvar,0);
8712: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8713: intred = BDY((LIST)ARG3(BDY(tlist)));
8714: ind = BDY((LIST)ARG4(BDY(tlist)));
8715: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8716: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8717: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8718: if ( j > i ) i = j;
8719: }
8720: n = i+1;
8721: nb = length(BDY(f));
8722: p = (ND **)MALLOC(n*sizeof(ND *));
8723: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8724: pi = BDY((LIST)BDY(t));
8725: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8726: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8727: ptomp(mod,(P)ARG2(pi),&inv);
8728: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8729: u = ptond(CO,vv,(P)ONE);
8730: HCM(u) = ((MQ)inv)->cont;
8731: c[pi1] = u;
8732: }
8733: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8734: printf("%d ",i); fflush(stdout);
8735: ti = BDY((LIST)BDY(t));
8736: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
8737: }
8738: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8739: printf("%d ",i); fflush(stdout);
8740: ti = BDY((LIST)BDY(t));
8741: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
8742: }
8743: m = length(ind);
8744: MKMAT(mat,nb,m);
8745: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
8746: for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
8747: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8748: return mat;
8749: }
8750:
8751: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8752: {
8753: int i,j,n,m,nb,pi0,pi1,nvar;
8754: VL fv,tv,vv;
8755: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8756: ND **p;
8757: ND *c;
8758: ND u;
8759: MAT mat;
8760: LM lm;
8761: Z lf,inv;
8762:
8763: parse_nd_option(current_option);
8764: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8765: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8766: switch ( ord->id ) {
8767: case 1:
8768: if ( ord->nv != nvar )
8769: error("nd_check : invalid order specification");
8770: break;
8771: default:
8772: break;
8773: }
8774: nd_init_ord(ord);
8775: #if 0
8776: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8777: #else
8778: nd_bpe = 32;
8779: #endif
8780: nd_setup_parameters(nvar,0);
8781: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8782: intred = BDY((LIST)ARG3(BDY(tlist)));
8783: ind = BDY((LIST)ARG4(BDY(tlist)));
8784: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8785: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8786: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8787: if ( j > i ) i = j;
8788: }
8789: n = i+1;
8790: nb = length(BDY(f));
8791: p = (ND **)MALLOC(n*sizeof(ND *));
8792: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8793: pi = BDY((LIST)BDY(t));
8794: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8795: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8796: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8797: u = ptond(CO,vv,(P)ONE);
8798: HCZ(u) = inv;
8799: c[pi1] = u;
8800: }
8801: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8802: printf("%d ",i); fflush(stdout);
8803: ti = BDY((LIST)BDY(t));
8804: p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
8805: }
8806: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8807: printf("%d ",i); fflush(stdout);
8808: ti = BDY((LIST)BDY(t));
8809: p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
8810: }
8811: m = length(ind);
8812: MKMAT(mat,nb,m);
8813: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
8814: for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
8815: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8816: return mat;
8817: }
8818:
8819: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8820: LIST tlist,int pos,MAT *rp)
8821: {
8822: int i,j,n,m,nb,pi0,pi1,nvar;
8823: VL fv,tv,vv;
8824: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8825: ND *p;
8826: ND *c;
8827: ND u;
8828: P inv;
8829: VECT vect;
8830:
8831: if ( mod == -2 )
8832: error("nd_btog_one : not implemented yet for a large finite field");
8833:
8834: parse_nd_option(current_option);
8835: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8836: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8837: switch ( ord->id ) {
8838: case 1:
8839: if ( ord->nv != nvar )
8840: error("nd_check : invalid order specification");
8841: break;
8842: default:
8843: break;
8844: }
8845: nd_init_ord(ord);
8846: #if 0
8847: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8848: #else
8849: nd_bpe = 32;
8850: #endif
8851: nd_setup_parameters(nvar,0);
8852: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8853: intred = BDY((LIST)ARG3(BDY(tlist)));
8854: ind = BDY((LIST)ARG4(BDY(tlist)));
8855: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8856: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8857: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8858: if ( j > i ) i = j;
8859: }
8860: n = i+1;
8861: nb = length(BDY(f));
8862: p = (ND *)MALLOC(n*sizeof(ND *));
8863: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8864: pi = BDY((LIST)BDY(t));
8865: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8866: if ( pi1 == pos ) {
8867: ptomp(mod,(P)ARG2(pi),&inv);
8868: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8869: u = ptond(CO,vv,(P)ONE);
8870: HCM(u) = ((MQ)inv)->cont;
8871: p[pi0] = u;
8872: }
8873: }
8874: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8875: printf("%d ",i); fflush(stdout);
8876: ti = BDY((LIST)BDY(t));
8877: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
8878: if ( Demand ) {
8879: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8880: }
8881: }
8882: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8883: printf("%d ",i); fflush(stdout);
8884: ti = BDY((LIST)BDY(t));
8885: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
8886: if ( Demand ) {
8887: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8888: }
8889: }
8890: m = length(ind);
8891: MKVECT(vect,m);
8892: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
8893: u = p[QTOS((Q)BDY(t))];
8894: if ( !u ) {
8895: u = nd_load_mod(QTOS((Q)BDY(t)));
8896: BDY(vect)[j] = ndtodp(mod,u);
8897: nd_free(u);
8898: } else
8899: BDY(vect)[j] = ndtodp(mod,u);
8900: }
8901: return vect;
8902: }
8903:
8904: void ndv_print_lf(NDV p)
8905: {
8906: NMV m;
8907: int i,len;
8908:
8909: if ( !p ) printf("0\n");
8910: else {
8911: len = LEN(p);
8912: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8913: printf("+");
8914: mpz_out_str(asir_out,10,BDY(CZ(m)));
8915: printf("*");
8916: ndl_print(DL(m));
8917: }
8918: printf("\n");
8919: }
8920: }
8921:
8922: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8923: {
8924: VL tv,fv,vv,vc,av;
8925: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8926: int m,nocheck,nvar,mindex,e,max;
8927: NDV c;
8928: NMV a;
8929: P p,zp;
8930: Q dmy;
8931: EPOS oepos;
8932: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8933: Alg alpha,dp;
8934: P poly;
8935: LIST f1,f2,zpl;
8936: Obj obj;
8937: NumberField nf;
8938: struct order_spec *ord1;
8939: struct oEGT eg_check,eg0,eg1;
8940: NODE tr,tl1,tl2,tl3,tl4;
8941: LIST l1,l2,l3,l4,l5;
8942: int *perm;
8943: int j,ret;
8944: NODE retn;
8945: Q jq,bpe;
8946:
8947: nd_module = 0;
8948: parse_nd_option(current_option);
8949: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8950: if ( nd_vc )
8951: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8952: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8953: switch ( ord->id ) {
8954: case 1:
8955: if ( ord->nv != nvar )
8956: error("nd_f4_lf_trace : invalid order specification");
8957: break;
8958: default:
8959: break;
8960: }
8961:
8962: nd_ntrans = nvar;
8963: nd_nalg = 0;
8964:
8965: nocheck = 0;
8966: mindex = 0;
8967:
8968: /* do not use on-demand load/save */
8969: nd_demand = 0;
8970: m = trace > 1 ? trace : get_lprime(mindex);
8971: nd_init_ord(ord);
8972: mrank = 0;
8973: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8974: for ( tv = vv; tv; tv = NEXT(tv) ) {
8975: if ( nd_module ) {
8976: s = BDY((LIST)BDY(t));
8977: trank = length(s);
8978: mrank = MAX(mrank,trank);
8979: for ( ; s; s = NEXT(s) ) {
8980: e = getdeg(tv->v,(P)BDY(s));
8981: max = MAX(e,max);
8982: }
8983: } else {
8984: e = getdeg(tv->v,(P)BDY(t));
8985: max = MAX(e,max);
8986: }
8987: }
8988: nd_setup_parameters(nvar,max);
8989: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8990: ishomo = 1;
8991: /* XXX */
8992: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8993: if ( nd_module ) {
8994: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8995: } else {
8996: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8997: }
8998: if ( ishomo )
8999: ishomo = ishomo && ndv_ishomo(c);
9000: if ( c ) {
9001: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9002: ndv_mod(-2,c);
9003: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9004: }
9005: }
9006: if ( in0 ) NEXT(in) = 0;
9007: if ( fd0 ) NEXT(fd) = 0;
9008: if ( !ishomo && homo ) {
9009: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9010: c = (NDV)BDY(t); len = LEN(c);
9011: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9012: wmax = MAX(TD(DL(a)),wmax);
9013: }
9014: homogenize_order(ord,nvar,&ord1);
9015: nd_init_ord(ord1);
9016: nd_setup_parameters(nvar+1,wmax);
9017: for ( t = fd0; t; t = NEXT(t) )
9018: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9019: }
9020: if ( MaxDeg > 0 ) nocheck = 1;
9021: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9022: if ( ret )
9023: cand = nd_f4_lf_trace_main(m,&perm);
9024: if ( !ret || !cand ) {
9025: *rp = 0; return;
9026: }
9027: if ( !ishomo && homo ) {
9028: /* dehomogenization */
9029: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9030: nd_init_ord(ord);
9031: nd_setup_parameters(nvar,0);
9032: }
9033: cand = ndv_reducebase(cand,perm);
9034: cand = ndv_reduceall(-2,cand);
9035: cbpe = nd_bpe;
9036: get_eg(&eg0);
9037: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9038: /* gbcheck : cand is a GB of Id(cand) ? */
9039: retn = nd_f4(-2,0,0);
9040: }
9041: if ( !retn ) {
9042: /* failure */
9043: *rp = 0; return;
9044: }
9045: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9046: if ( DP_Print )
9047: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime+eg_check.gctime);
9048: /* dp->p */
9049: nd_bpe = cbpe;
9050: nd_setup_parameters(nd_nvar,0);
9051: for ( r = cand; r; r = NEXT(r) ) {
9052: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9053: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9054: }
9055: MKLIST(*rp,cand);
9056: }
9057:
9058: NODE nd_f4_lf_trace_main(int m,int **indp)
9059: {
9060: int i,nh,stat,index;
9061: NODE r,rm,g;
9062: ND_pairs d,l,l0,t;
9063: ND spol,red;
9064: NDV nf,redv,nfqv,nfv;
9065: NM s0,s;
9066: NODE rp0,srp0,nflist,nflist_lf;
9067: int nsp,nred,col,rank,len,k,j,a;
9068: UINT c;
9069: UINT **spmat;
9070: UINT *s0vect,*svect,*p,*v;
9071: int *colstat;
9072: IndArray *imat;
9073: int *rhead;
9074: int spcol,sprow;
9075: int sugar;
9076: PGeoBucket bucket;
9077: struct oEGT eg0,eg1,eg_f4;
9078:
9079: g = 0; d = 0;
9080: for ( i = 0; i < nd_psn; i++ ) {
9081: d = update_pairs(d,g,i,0);
9082: g = update_base(g,i);
9083: }
9084: while ( d ) {
9085: get_eg(&eg0);
9086: l = nd_minsugarp(d,&d);
9087: sugar = SG(l);
9088: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9089: bucket = create_pbucket();
9090: stat = nd_sp_f4(m,0,l,bucket);
9091: if ( !stat ) {
9092: for ( t = l; NEXT(t); t = NEXT(t) );
9093: NEXT(t) = d; d = l;
9094: d = nd_reconstruct(1,d);
9095: continue;
9096: }
9097: if ( bucket->m < 0 ) continue;
9098: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9099: if ( !col ) {
9100: for ( t = l; NEXT(t); t = NEXT(t) );
9101: NEXT(t) = d; d = l;
9102: d = nd_reconstruct(1,d);
9103: continue;
9104: }
9105: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9106: if ( DP_Print )
9107: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
9108: sugar,eg_f4.exectime+eg_f4.gctime);
9109: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9110: if ( !l0 ) continue;
9111: l = l0;
9112:
9113: /* over LF */
9114: bucket = create_pbucket();
9115: stat = nd_sp_f4(-2,1,l,bucket);
9116: if ( !stat ) {
9117: for ( t = l; NEXT(t); t = NEXT(t) );
9118: NEXT(t) = d; d = l;
9119: d = nd_reconstruct(1,d);
9120: continue;
9121: }
9122: if ( bucket->m < 0 ) continue;
9123: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9124: if ( !col ) {
9125: for ( t = l; NEXT(t); t = NEXT(t) );
9126: NEXT(t) = d; d = l;
9127: d = nd_reconstruct(1,d);
9128: continue;
9129: }
9130: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9131: /* adding new bases */
9132: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9133: nfv = (NDV)BDY(rm);
9134: nfqv = (NDV)BDY(r);
9135: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9136: ndv_removecont(m,nfv);
9137: ndv_removecont(-2,nfqv);
9138: nh = ndv_newps(-2,nfv,nfqv,1);
9139: d = update_pairs(d,g,nh,0);
9140: g = update_base(g,nh);
9141: }
9142: if ( r || rm ) return 0;
9143: }
9144: conv_ilist(nd_demand,1,g,indp);
9145: return g;
9146: }
9147:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>