Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.3
1.3 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.2 2018/09/21 07:06:51 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;
1.3 ! noro 5969: int l;
1.1 noro 5970:
5971: maxrs = 0;
5972: for ( i = 0; i < col && !svect[i]; i++ );
5973: if ( i == col ) return maxrs;
5974: hmag = p_mag((P)svect[i])*nd_scale;
5975: cvect = (Z *)MALLOC(col*sizeof(Q));
5976: for ( i = 0; i < nred; i++ ) {
5977: ivect = imat[i];
5978: k = ivect->head;
5979: if ( svect[k] ) {
5980: maxrs = MAX(maxrs,rp0[i]->sugar);
5981: redv = nd_demand?ndv_load(rp0[i]->index)
5982: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
5983: len = LEN(redv); mr = BDY(redv);
5984: igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
5985: chsgnz(cs,&mcs);
5986: if ( !UNIQ(cr) ) {
5987: for ( j = 0; j < col; j++ ) {
5988: mulz(svect[j],cr,&c1); svect[j] = c1;
5989: }
5990: }
5991: svect[k] = 0; prev = k;
5992: switch ( ivect->width ) {
5993: case 1:
5994: ivc = ivect->index.c;
5995: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
5996: pos = prev+ivc[j]; prev = pos;
1.2 noro 5997: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 5998: }
5999: break;
6000: case 2:
6001: ivs = ivect->index.s;
6002: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6003: pos = prev+ivs[j]; prev = pos;
1.2 noro 6004: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 6005: }
6006: break;
6007: case 4:
6008: ivi = ivect->index.i;
6009: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6010: pos = prev+ivi[j]; prev = pos;
1.2 noro 6011: muladdtoz(CQ(mr),mcs,&svect[pos]);
1.1 noro 6012: }
6013: break;
6014: }
6015: for ( j = k+1; j < col && !svect[j]; j++ );
6016: if ( j == col ) break;
6017: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6018: nz = compress_array(svect,cvect,col);
6019: removecont_array((P *)cvect,nz,1);
6020: expand_array(svect,cvect,nz);
6021: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6022: }
6023: }
6024: }
6025: nz = compress_array(svect,cvect,col);
6026: removecont_array((P *)cvect,nz,1);
6027: expand_array(svect,cvect,nz);
6028: if ( DP_Print ) {
6029: fprintf(asir_out,"-"); fflush(asir_out);
6030: }
6031: return maxrs;
6032: }
6033:
6034: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6035: {
6036: int i,j,k,len,pos,prev;
6037: UINT c,c1,c2,c3,up,lo,dmy;
6038: IndArray ivect;
6039: unsigned char *ivc;
6040: unsigned short *ivs;
6041: unsigned int *ivi;
6042: NDV redv;
6043: NMV mr;
6044: NODE rp;
6045: int maxrs;
6046:
6047: maxrs = 0;
6048: for ( i = 0; i < nred; i++ ) {
6049: ivect = imat[i];
6050: k = ivect->head; svect[k] %= m;
6051: if ( (c = svect[k]) != 0 ) {
6052: maxrs = MAX(maxrs,rp0[i]->sugar);
6053: c = m-c; redv = nd_ps[rp0[i]->index];
6054: len = LEN(redv); mr = BDY(redv);
6055: svect[k] = 0; prev = k;
6056: switch ( ivect->width ) {
6057: case 1:
6058: ivc = ivect->index.c;
6059: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6060: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6061: if ( c1 ) {
6062: c2 = svect[pos];
6063: DMA(c1,c,c2,up,lo);
6064: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6065: } else svect[pos] = lo;
6066: }
6067: }
6068: break;
6069: case 2:
6070: ivs = ivect->index.s;
6071: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6072: pos = prev+ivs[j]; c1 = CM(mr);
6073: prev = pos;
6074: if ( c1 ) {
6075: c2 = svect[pos];
6076: DMA(c1,c,c2,up,lo);
6077: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6078: } else svect[pos] = lo;
6079: }
6080: }
6081: break;
6082: case 4:
6083: ivi = ivect->index.i;
6084: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6085: pos = prev+ivi[j]; c1 = CM(mr);
6086: prev = pos;
6087: if ( c1 ) {
6088: c2 = svect[pos];
6089: DMA(c1,c,c2,up,lo);
6090: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6091: } else svect[pos] = lo;
6092: }
6093: }
6094: break;
6095: }
6096: }
6097: }
6098: for ( i = 0; i < col; i++ )
6099: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6100: return maxrs;
6101: }
6102:
6103: #if defined(__GNUC__) && SIZEOF_LONG==8
6104:
6105: int ndv_reduce_vect64(int m,U64 *svect,U64 *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6106: {
6107: int i,j,k,len,pos,prev;
6108: U64 a,c,c1,c2;
6109: IndArray ivect;
6110: unsigned char *ivc;
6111: unsigned short *ivs;
6112: unsigned int *ivi;
6113: NDV redv;
6114: NMV mr;
6115: NODE rp;
6116: int maxrs;
6117:
6118: for ( i = 0; i < col; i++ ) cvect[i] = 0;
6119: maxrs = 0;
6120: for ( i = 0; i < nred; i++ ) {
6121: ivect = imat[i];
6122: k = ivect->head;
6123: a = svect[k]; c = cvect[k];
6124: MOD128(a,c,m);
6125: svect[k] = a; cvect[k] = 0;
6126: if ( (c = svect[k]) != 0 ) {
6127: maxrs = MAX(maxrs,rp0[i]->sugar);
6128: c = m-c; redv = nd_ps[rp0[i]->index];
6129: len = LEN(redv); mr = BDY(redv);
6130: svect[k] = 0; prev = k;
6131: switch ( ivect->width ) {
6132: case 1:
6133: ivc = ivect->index.c;
6134: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6135: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6136: if ( c1 ) {
6137: c2 = svect[pos]+c1*c;
6138: if ( c2 < svect[pos] ) cvect[pos]++;
6139: svect[pos] = c2;
6140: }
6141: }
6142: break;
6143: case 2:
6144: ivs = ivect->index.s;
6145: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6146: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
6147: if ( c1 ) {
6148: c2 = svect[pos]+c1*c;
6149: if ( c2 < svect[pos] ) cvect[pos]++;
6150: svect[pos] = c2;
6151: }
6152: }
6153: break;
6154: case 4:
6155: ivi = ivect->index.i;
6156: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6157: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
6158: if ( c1 ) {
6159: c2 = svect[pos]+c1*c;
6160: if ( c2 < svect[pos] ) cvect[pos]++;
6161: svect[pos] = c2;
6162: }
6163: }
6164: break;
6165: }
6166: }
6167: }
6168: for ( i = 0; i < col; i++ ) {
6169: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
6170: }
6171: return maxrs;
6172: }
6173: #endif
6174:
6175: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6176: {
6177: int i,j,k,len,pos,prev;
6178: UINT c,c1,c2,c3,up,lo,dmy;
6179: IndArray ivect;
6180: unsigned char *ivc;
6181: unsigned short *ivs;
6182: unsigned int *ivi;
6183: NDV redv;
6184: NMV mr;
6185: NODE rp;
6186: int maxrs;
6187:
6188: maxrs = 0;
6189: for ( i = 0; i < nred; i++ ) {
6190: ivect = imat[i];
6191: k = ivect->head;
6192: if ( (c = svect[k]) != 0 ) {
6193: maxrs = MAX(maxrs,rp0[i]->sugar);
6194: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6195: len = LEN(redv); mr = BDY(redv);
6196: svect[k] = 0; prev = k;
6197: switch ( ivect->width ) {
6198: case 1:
6199: ivc = ivect->index.c;
6200: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6201: pos = prev+ivc[j]; prev = pos;
6202: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6203: }
6204: break;
6205: case 2:
6206: ivs = ivect->index.s;
6207: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6208: pos = prev+ivs[j]; prev = pos;
6209: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6210: }
6211: break;
6212: case 4:
6213: ivi = ivect->index.i;
6214: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6215: pos = prev+ivi[j]; prev = pos;
6216: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6217: }
6218: break;
6219: }
6220: }
6221: }
6222: return maxrs;
6223: }
6224:
6225: ND nd_add_lf(ND p1,ND p2)
6226: {
6227: int n,c,can;
6228: ND r;
6229: NM m1,m2,mr0,mr,s;
6230: Z t;
6231:
6232: if ( !p1 ) return p2;
6233: else if ( !p2 ) return p1;
6234: else {
6235: can = 0;
6236: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6237: c = DL_COMPARE(DL(m1),DL(m2));
6238: switch ( c ) {
6239: case 0:
6240: addlf(CZ(m1),CZ(m2),&t);
6241: s = m1; m1 = NEXT(m1);
6242: if ( t ) {
6243: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6244: } else {
6245: can += 2; FREENM(s);
6246: }
6247: s = m2; m2 = NEXT(m2); FREENM(s);
6248: break;
6249: case 1:
6250: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6251: break;
6252: case -1:
6253: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6254: break;
6255: }
6256: }
6257: if ( !mr0 )
6258: if ( m1 ) mr0 = m1;
6259: else if ( m2 ) mr0 = m2;
6260: else return 0;
6261: else if ( m1 ) NEXT(mr) = m1;
6262: else if ( m2 ) NEXT(mr) = m2;
6263: else NEXT(mr) = 0;
6264: BDY(p1) = mr0;
6265: SG(p1) = MAX(SG(p1),SG(p2));
6266: LEN(p1) = LEN(p1)+LEN(p2)-can;
6267: FREEND(p2);
6268: return p1;
6269: }
6270: }
6271:
6272: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6273: {
6274: int i,j,k,len,pos,prev;
6275: mpz_t c,mc,c1;
6276: IndArray ivect;
6277: unsigned char *ivc;
6278: unsigned short *ivs;
6279: unsigned int *ivi;
6280: NDV redv;
6281: NMV mr;
6282: NODE rp;
6283: int maxrs;
6284:
6285: maxrs = 0;
6286: lf_lazy = 1;
6287: for ( i = 0; i < nred; i++ ) {
6288: ivect = imat[i];
6289: k = ivect->head;
6290: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6291: if ( mpz_sgn(svect[k]) ) {
6292: maxrs = MAX(maxrs,rp0[i]->sugar);
6293: mpz_neg(svect[k],svect[k]);
6294: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6295: len = LEN(redv); mr = BDY(redv);
6296: prev = k;
6297: switch ( ivect->width ) {
6298: case 1:
6299: ivc = ivect->index.c;
6300: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6301: pos = prev+ivc[j]; prev = pos;
6302: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6303: }
6304: break;
6305: case 2:
6306: ivs = ivect->index.s;
6307: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6308: pos = prev+ivs[j]; prev = pos;
6309: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6310: }
6311: break;
6312: case 4:
6313: ivi = ivect->index.i;
6314: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6315: pos = prev+ivi[j]; prev = pos;
6316: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6317: }
6318: break;
6319: }
6320: mpz_set_ui(svect[k],0);
6321: }
6322: }
6323: lf_lazy=0;
6324: for ( i = 0; i < col; i++ ) {
6325: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6326: }
6327: return maxrs;
6328: }
6329:
6330: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6331: {
6332: int i,j,k,l,rank,s;
6333: mpz_t a,a1,inv;
6334: mpz_t *t,*pivot,*pk;
6335: mpz_t **mat;
6336: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6337: int size,size1;
6338:
6339: mpz_init(inv);
6340: mpz_init(a);
6341: mat = (mpz_t **)mat0;
6342: size = 0;
6343: for ( rank = 0, j = 0; j < col; j++ ) {
6344: for ( i = rank; i < row; i++ ) {
6345: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6346: }
6347: for ( i = rank; i < row; i++ )
6348: if ( mpz_sgn(mat[i][j]) )
6349: break;
6350: if ( i == row ) {
6351: colstat[j] = 0;
6352: continue;
6353: } else
6354: colstat[j] = 1;
6355: if ( i != rank ) {
6356: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6357: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6358: }
6359: pivot = mat[rank];
6360: s = sugar[rank];
6361: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6362: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6363: if ( mpz_sgn(*pk) ) {
6364: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6365: }
6366: for ( i = rank+1; i < row; i++ ) {
6367: t = mat[i];
6368: if ( mpz_sgn(t[j]) ) {
6369: sugar[i] = MAX(sugar[i],s);
6370: mpz_neg(a,t[j]);
6371: red_by_vect_lf(t+j,pivot+j,a,col-j);
6372: }
6373: }
6374: rank++;
6375: }
6376: for ( j = col-1, l = rank-1; j >= 0; j-- )
6377: if ( colstat[j] ) {
6378: pivot = mat[l];
6379: s = sugar[l];
6380: for ( k = j; k < col; k++ )
6381: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6382: for ( i = 0; i < l; i++ ) {
6383: t = mat[i];
6384: if ( mpz_sgn(t[j]) ) {
6385: sugar[i] = MAX(sugar[i],s);
6386: mpz_neg(a,t[j]);
6387: red_by_vect_lf(t+j,pivot+j,a,col-j);
6388: }
6389: }
6390: l--;
6391: }
6392: for ( j = 0, l = 0; l < rank; j++ )
6393: if ( colstat[j] ) {
6394: t = mat[l];
6395: for ( k = j; k < col; k++ ) {
6396: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6397: }
6398: l++;
6399: }
6400: return rank;
6401: }
6402:
6403:
6404: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6405: {
6406: int j,k,len;
6407: UINT *p;
6408: UINT c;
6409: NDV r;
6410: NMV mr0,mr;
6411:
6412: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6413: if ( !len ) return 0;
6414: else {
6415: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6416: #if 0
6417: ndv_alloc += nmv_adv*len;
6418: #endif
6419: mr = mr0;
6420: p = s0vect;
6421: for ( j = k = 0; j < col; j++, p += nd_wpd )
6422: if ( !rhead[j] ) {
6423: if ( (c = vect[k++]) != 0 ) {
6424: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6425: }
6426: }
6427: MKNDV(nd_nvar,mr0,len,r);
6428: return r;
6429: }
6430: }
6431:
6432: #if defined(__GNUC__) && SIZEOF_LONG==8
6433: NDV vect64_to_ndv(U64 *vect,int spcol,int col,int *rhead,UINT *s0vect)
6434: {
6435: int j,k,len;
6436: UINT *p;
6437: UINT c;
6438: NDV r;
6439: NMV mr0,mr;
6440:
6441: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6442: if ( !len ) return 0;
6443: else {
6444: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6445: #if 0
6446: ndv_alloc += nmv_adv*len;
6447: #endif
6448: mr = mr0;
6449: p = s0vect;
6450: for ( j = k = 0; j < col; j++, p += nd_wpd )
6451: if ( !rhead[j] ) {
6452: if ( (c = (UINT)vect[k++]) != 0 ) {
6453: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6454: }
6455: }
6456: MKNDV(nd_nvar,mr0,len,r);
6457: return r;
6458: }
6459: }
6460: #endif
6461:
6462: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6463: {
6464: int j,k,len;
6465: UINT *p;
6466: NDV r;
6467: NMV mr0,mr;
6468:
6469: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6470: if ( !len ) return 0;
6471: else {
6472: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6473: mr = mr0;
6474: p = s0vect;
6475: for ( j = 0; j < col; j++, p += nd_wpd )
6476: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6477: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6478: }
6479: MKNDV(nd_nvar,mr0,len,r);
6480: return r;
6481: }
6482: }
6483:
6484: /* for preprocessed vector */
6485:
6486: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6487: {
6488: int j,k,len;
6489: UINT *p;
6490: Z c;
6491: NDV r;
6492: NMV mr0,mr;
6493:
6494: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6495: if ( !len ) return 0;
6496: else {
6497: mr0 = (NMV)MALLOC(nmv_adv*len);
6498: #if 0
6499: ndv_alloc += nmv_adv*len;
6500: #endif
6501: mr = mr0;
6502: p = s0vect;
6503: for ( j = k = 0; j < col; j++, p += nd_wpd )
6504: if ( !rhead[j] ) {
6505: if ( (c = vect[k++]) != 0 ) {
6506: if ( !INT(c) )
6507: error("afo");
6508: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6509: }
6510: }
6511: MKNDV(nd_nvar,mr0,len,r);
6512: return r;
6513: }
6514: }
6515:
6516: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6517: {
6518: int j,k,len;
6519: UINT *p;
6520: mpz_t c;
6521: NDV r;
6522: NMV mr0,mr;
6523:
6524: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6525: if ( !len ) return 0;
6526: else {
6527: mr0 = (NMV)MALLOC(nmv_adv*len);
6528: #if 0
6529: ndv_alloc += nmv_adv*len;
6530: #endif
6531: mr = mr0;
6532: p = s0vect;
6533: for ( j = k = 0; j < col; j++, p += nd_wpd )
6534: if ( !rhead[j] ) {
6535: c[0] = vect[k++][0];
6536: if ( mpz_sgn(c) ) {
6537: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6538: }
6539: }
6540: MKNDV(nd_nvar,mr0,len,r);
6541: return r;
6542: }
6543: }
6544:
6545: /* for plain vector */
6546:
6547: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6548: {
6549: int j,k,len;
6550: UINT *p;
6551: Z c;
6552: NDV r;
6553: NMV mr0,mr;
6554:
6555: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6556: if ( !len ) return 0;
6557: else {
6558: mr0 = (NMV)MALLOC(nmv_adv*len);
6559: #if 0
6560: ndv_alloc += nmv_adv*len;
6561: #endif
6562: mr = mr0;
6563: p = s0vect;
6564: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6565: if ( (c = vect[k]) != 0 ) {
6566: if ( !INT(c) )
6567: error("afo");
6568: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6569: }
6570: MKNDV(nd_nvar,mr0,len,r);
6571: return r;
6572: }
6573: }
6574:
6575: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6576: {
6577: ND_pairs t;
6578: NODE sp0,sp;
6579: int stat;
6580: ND spol;
6581:
6582: for ( t = l; t; t = NEXT(t) ) {
6583: stat = nd_sp(m,trace,t,&spol);
6584: if ( !stat ) return 0;
6585: if ( spol ) {
6586: add_pbucket_symbolic(bucket,spol);
6587: }
6588: }
6589: return 1;
6590: }
6591:
6592: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6593: {
6594: NODE rp0,rp;
6595: NM mul,head,s0,s;
6596: int index,col,i,sugar;
6597: RHist h;
6598: UINT *s0v,*p;
6599: NM_ind_pair pair;
6600: ND red;
6601: NDV *ps;
6602: static int afo;
6603:
6604: s0 = 0; rp0 = 0; col = 0;
6605: if ( nd_demand )
6606: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6607: else
6608: ps = trace?nd_ps_trace:nd_ps;
6609: while ( 1 ) {
6610: head = remove_head_pbucket_symbolic(bucket);
6611: if ( !head ) break;
6612: if ( !s0 ) s0 = head;
6613: else NEXT(s) = head;
6614: s = head;
6615: index = ndl_find_reducer(DL(head));
6616: if ( index >= 0 ) {
6617: h = nd_psh[index];
6618: NEWNM(mul);
6619: ndl_sub(DL(head),DL(h),DL(mul));
6620: if ( ndl_check_bound2(index,DL(mul)) )
6621: return 0;
6622: sugar = TD(DL(mul))+SG(ps[index]);
6623: MKNM_ind_pair(pair,mul,index,sugar);
6624: red = ndv_mul_nm_symbolic(mul,ps[index]);
6625: add_pbucket_symbolic(bucket,nd_remove_head(red));
6626: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6627: }
6628: col++;
6629: }
6630: if ( rp0 ) NEXT(rp) = 0;
6631: NEXT(s) = 0;
6632: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6633: for ( i = 0, p = s0v, s = s0; i < col;
6634: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6635: *s0vect = s0v;
6636: *r = rp0;
6637: return col;
6638: }
6639:
6640: void print_ndp(ND_pairs l)
6641: {
6642: ND_pairs t;
6643:
6644: for ( t = l; t; t = NEXT(t) )
6645: printf("[%d,%d] ",t->i1,t->i2);
6646: printf("\n");
6647: }
6648:
6649: NODE nd_f4(int m,int checkonly,int **indp)
6650: {
6651: int i,nh,stat,index,f4red;
6652: NODE r,g,tn0,tn,node;
6653: ND_pairs d,l,t,ll0,ll,lh;
6654: LIST l0,l1;
6655: ND spol,red;
6656: NDV nf,redv;
6657: NM s0,s;
6658: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6659: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6660: UINT c;
6661: UINT **spmat;
6662: UINT *s0vect,*svect,*p,*v;
6663: int *colstat;
6664: IndArray *imat;
6665: int *rhead;
6666: int spcol,sprow;
6667: int sugar,sugarh;
6668: PGeoBucket bucket;
6669: struct oEGT eg0,eg1,eg_f4;
6670: Z i1,i2,sugarq;
6671: #if 0
6672: ndv_alloc = 0;
6673: #endif
6674: g = 0; d = 0;
6675: for ( i = 0; i < nd_psn; i++ ) {
6676: d = update_pairs(d,g,i,0);
6677: g = update_base(g,i);
6678: }
6679: nzlist = 0;
6680: nzlist_t = nd_nzlist;
6681: f4red = 1;
6682: nd_last_nonzero = 0;
6683: while ( d ) {
6684: get_eg(&eg0);
6685: l = nd_minsugarp(d,&d);
6686: sugar = nd_sugarweight?l->sugar2:SG(l);
6687: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6688: if ( nzlist_t ) {
6689: node = BDY((LIST)BDY(nzlist_t));
6690: sugarh = QTOS((Q)ARG0(node));
6691: tn = BDY((LIST)ARG1(node));
6692: if ( !tn ) {
6693: nzlist_t = NEXT(nzlist_t);
6694: continue;
6695: }
6696: /* tn = [[i1,i2],...] */
6697: lh = nd_ipairtospair(tn);
6698: }
6699: bucket = create_pbucket();
6700: stat = nd_sp_f4(m,0,l,bucket);
6701: if ( !stat ) {
6702: for ( t = l; NEXT(t); t = NEXT(t) );
6703: NEXT(t) = d; d = l;
6704: d = nd_reconstruct(0,d);
6705: continue;
6706: }
6707: if ( bucket->m < 0 ) continue;
6708: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6709: if ( !col ) {
6710: for ( t = l; NEXT(t); t = NEXT(t) );
6711: NEXT(t) = d; d = l;
6712: d = nd_reconstruct(0,d);
6713: continue;
6714: }
6715: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6716: if ( DP_Print )
1.3 ! noro 6717: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.1 noro 6718: sugar,eg_f4.exectime+eg_f4.gctime);
6719: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6720: if ( checkonly && nflist ) return 0;
6721: /* adding new bases */
6722: if ( nflist ) nd_last_nonzero = f4red;
6723: for ( r = nflist; r; r = NEXT(r) ) {
6724: nf = (NDV)BDY(r);
6725: ndv_removecont(m,nf);
6726: if ( !m && nd_nalg ) {
6727: ND nf1;
6728:
6729: nf1 = ndvtond(m,nf);
6730: nd_monic(0,&nf1);
6731: nd_removecont(m,nf1);
6732: nf = ndtondv(m,nf1);
6733: }
6734: nh = ndv_newps(m,nf,0,1);
6735: d = update_pairs(d,g,nh,0);
6736: g = update_base(g,nh);
6737: }
6738: if ( DP_Print ) {
6739: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6740: }
6741: if ( nd_gentrace ) {
6742: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6743: NEXTNODE(tn0,tn);
6744: STOQ(t->i1,i1); STOQ(t->i2,i2);
6745: node = mknode(2,i1,i2); MKLIST(l0,node);
6746: BDY(tn) = l0;
6747: }
6748: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
6749: STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
6750: MKNODE(node,l1,nzlist); nzlist = node;
6751: }
6752: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6753: f4red++;
6754: if ( nd_f4red && f4red > nd_f4red ) break;
6755: if ( nd_rank0 && !nflist ) break;
6756: }
6757: if ( nd_gentrace ) {
6758: MKLIST(l0,reverse_node(nzlist));
6759: MKNODE(nd_alltracelist,l0,0);
6760: }
6761: #if 0
6762: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6763: #endif
6764: conv_ilist(nd_demand,0,g,indp);
6765: return g;
6766: }
6767:
6768: NODE nd_f4_trace(int m,int **indp)
6769: {
6770: int i,nh,stat,index;
6771: NODE r,g;
6772: ND_pairs d,l,l0,t;
6773: ND spol,red;
6774: NDV nf,redv,nfqv,nfv;
6775: NM s0,s;
6776: NODE rp0,srp0,nflist;
6777: int nsp,nred,col,rank,len,k,j,a;
6778: UINT c;
6779: UINT **spmat;
6780: UINT *s0vect,*svect,*p,*v;
6781: int *colstat;
6782: IndArray *imat;
6783: int *rhead;
6784: int spcol,sprow;
6785: int sugar;
6786: PGeoBucket bucket;
6787: struct oEGT eg0,eg1,eg_f4;
6788:
6789: g = 0; d = 0;
6790: for ( i = 0; i < nd_psn; i++ ) {
6791: d = update_pairs(d,g,i,0);
6792: g = update_base(g,i);
6793: }
6794: while ( d ) {
6795: get_eg(&eg0);
6796: l = nd_minsugarp(d,&d);
6797: sugar = SG(l);
6798: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6799: bucket = create_pbucket();
6800: stat = nd_sp_f4(m,0,l,bucket);
6801: if ( !stat ) {
6802: for ( t = l; NEXT(t); t = NEXT(t) );
6803: NEXT(t) = d; d = l;
6804: d = nd_reconstruct(1,d);
6805: continue;
6806: }
6807: if ( bucket->m < 0 ) continue;
6808: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6809: if ( !col ) {
6810: for ( t = l; NEXT(t); t = NEXT(t) );
6811: NEXT(t) = d; d = l;
6812: d = nd_reconstruct(1,d);
6813: continue;
6814: }
6815: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6816: if ( DP_Print )
1.3 ! noro 6817: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.1 noro 6818: sugar,eg_f4.exectime+eg_f4.gctime);
6819: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6820: if ( !l0 ) continue;
6821: l = l0;
6822:
6823: /* over Q */
6824: bucket = create_pbucket();
6825: stat = nd_sp_f4(0,1,l,bucket);
6826: if ( !stat ) {
6827: for ( t = l; NEXT(t); t = NEXT(t) );
6828: NEXT(t) = d; d = l;
6829: d = nd_reconstruct(1,d);
6830: continue;
6831: }
6832: if ( bucket->m < 0 ) continue;
6833: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6834: if ( !col ) {
6835: for ( t = l; NEXT(t); t = NEXT(t) );
6836: NEXT(t) = d; d = l;
6837: d = nd_reconstruct(1,d);
6838: continue;
6839: }
6840: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6841: /* adding new bases */
6842: for ( r = nflist; r; r = NEXT(r) ) {
6843: nfqv = (NDV)BDY(r);
6844: ndv_removecont(0,nfqv);
6845: if ( !remqi((Q)HCQ(nfqv),m) ) return 0;
6846: if ( nd_nalg ) {
6847: ND nf1;
6848:
6849: nf1 = ndvtond(m,nfqv);
6850: nd_monic(0,&nf1);
6851: nd_removecont(0,nf1);
6852: nfqv = ndtondv(0,nf1); nd_free(nf1);
6853: }
6854: nfv = ndv_dup(0,nfqv);
6855: ndv_mod(m,nfv);
6856: ndv_removecont(m,nfv);
6857: nh = ndv_newps(0,nfv,nfqv,1);
6858: d = update_pairs(d,g,nh,0);
6859: g = update_base(g,nh);
6860: }
6861: }
6862: #if 0
6863: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6864: #endif
6865: conv_ilist(nd_demand,1,g,indp);
6866: return g;
6867: }
6868:
6869: int rref(matrix mat,int *sugar)
6870: {
6871: int row,col,i,j,k,l,s,wcol,wj;
6872: unsigned long bj;
6873: unsigned long **a;
6874: unsigned long *ai,*ak,*as,*t;
6875: int *pivot;
6876:
6877: row = mat->row;
6878: col = mat->col;
6879: a = mat->a;
6880: wcol = (col+BLEN-1)/BLEN;
6881: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6882: i = 0;
6883: for ( j = 0; j < col; j++ ) {
6884: wj = j/BLEN; bj = 1L<<(j%BLEN);
6885: for ( k = i; k < row; k++ )
6886: if ( a[k][wj] & bj ) break;
6887: if ( k == row ) continue;
6888: pivot[i] = j;
6889: if ( k != i ) {
6890: t = a[i]; a[i] = a[k]; a[k] = t;
6891: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6892: }
6893: ai = a[i];
6894: for ( k = i+1; k < row; k++ ) {
6895: ak = a[k];
6896: if ( ak[wj] & bj ) {
6897: for ( l = wj; l < wcol; l++ )
6898: ak[l] ^= ai[l];
6899: sugar[k] = MAX(sugar[k],sugar[i]);
6900: }
6901: }
6902: i++;
6903: }
6904: for ( k = i-1; k >= 0; k-- ) {
6905: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6906: ak = a[k];
6907: for ( s = 0; s < k; s++ ) {
6908: as = a[s];
6909: if ( as[wj] & bj ) {
6910: for ( l = wj; l < wcol; l++ )
6911: as[l] ^= ak[l];
6912: sugar[s] = MAX(sugar[s],sugar[k]);
6913: }
6914: }
6915: }
6916: return i;
6917: }
6918:
6919: void print_matrix(matrix mat)
6920: {
6921: int row,col,i,j;
6922: unsigned long *ai;
6923:
6924: row = mat->row;
6925: col = mat->col;
6926: printf("%d x %d\n",row,col);
6927: for ( i = 0; i < row; i++ ) {
6928: ai = mat->a[i];
6929: for ( j = 0; j < col; j++ ) {
6930: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6931: else putchar('0');
6932: }
6933: putchar('\n');
6934: }
6935: }
6936:
6937: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6938:
6939: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6940: {
6941: int row,col,wcol,wj,i,j;
6942: unsigned long bj;
6943: unsigned long *ai;
6944: unsigned long **a;
6945: int len;
6946: int *pos;
6947:
6948: row = mat->row;
6949: col = mat->col;
6950: wcol = (col+BLEN-1)/BLEN;
6951: pos = (int *)MALLOC(wcol*sizeof(int));
6952: bzero(pos,wcol*sizeof(int));
6953: for ( i = j = 0; i < wcol; i++ )
6954: if ( v[i] ) pos[j++] = i;;
6955: len = j;
6956: wj = rhead/BLEN;
6957: bj = 1L<<rhead%BLEN;
6958: a = mat->a;
6959: for ( i = 0; i < row; i++ ) {
6960: ai = a[i];
6961: if ( ai[wj]&bj ) {
6962: for ( j = 0; j < len; j++ )
6963: ai[pos[j]] ^= v[pos[j]];
6964: sugar[i] = MAX(sugar[i],rsugar);
6965: }
6966: }
6967: }
6968:
6969: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
6970: {
6971: int nsp,nred,i,i0,k,rank,row;
6972: NODE r0,rp;
6973: ND_pairs sp;
6974: ND spol;
6975: NM_ind_pair rt;
6976: int *s0hash;
6977: UINT *s;
6978: int *pivot,*sugar,*head;
6979: matrix mat;
6980: NM m;
6981: NODE r;
6982: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
6983: int rhead,rsugar,size;
6984: unsigned long *v;
6985:
6986: get_eg(&eg0);
6987: init_eg(&eg_search);
6988: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
6989: nred = length(rp0);
6990: mat = alloc_matrix(nsp,col);
6991: s0hash = (int *)MALLOC(col*sizeof(int));
6992: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
6993: s0hash[i] = ndl_hash_value(s);
6994:
6995: sugar = (int *)MALLOC(nsp*sizeof(int));
6996: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
6997: nd_sp(2,0,sp,&spol);
6998: if ( spol ) {
6999: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7000: sugar[i] = SG(spol);
7001: i++;
7002: }
7003: }
7004: mat->row = i;
7005: if ( DP_Print ) {
7006: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7007: }
7008: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7009: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7010: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7011: rt = (NM_ind_pair)BDY(rp);
7012: bzero(v,size);
7013: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7014: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7015: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7016: }
7017:
7018: get_eg(&eg1);
7019: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7020: rank = rref(mat,sugar);
7021:
7022: for ( i = 0, r0 = 0; i < rank; i++ ) {
7023: NEXTNODE(r0,r);
7024: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7025: SG((NDV)BDY(r)) = sugar[i];
7026: }
7027: if ( r0 ) NEXT(r) = 0;
7028: get_eg(&eg2);
7029: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7030: if ( DP_Print ) {
7031: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
7032: eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
7033: fflush(asir_out);
7034: }
7035: return r0;
7036: }
7037:
7038:
7039: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7040: {
7041: IndArray *imat;
7042: int nsp,nred,i;
7043: int *rhead;
7044: NODE r0,rp;
7045: ND_pairs sp;
7046: NM_ind_pair *rvect;
7047: UINT *s;
7048: int *s0hash;
7049:
7050: if ( m == 2 && nd_rref2 )
7051: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7052:
7053: init_eg(&eg_search);
7054: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7055: nred = length(rp0);
7056: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7057: rhead = (int *)MALLOC(col*sizeof(int));
7058: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7059:
7060: /* construction of index arrays */
7061: if ( DP_Print ) {
7062: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7063: }
7064: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
7065: s0hash = (int *)MALLOC(col*sizeof(int));
7066: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7067: s0hash[i] = ndl_hash_value(s);
7068: for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
7069: rvect[i] = (NM_ind_pair)BDY(rp);
7070: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,s0hash,rvect[i]);
7071: rhead[imat[i]->head] = 1;
7072: }
7073: if ( m > 0 )
7074: #if defined(__GNUC__) && SIZEOF_LONG==8
7075: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7076: #else
7077: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7078: #endif
7079: else if ( m == -1 )
7080: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7081: else if ( m == -2 )
7082: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7083: else
7084: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7085: #if 0
7086: if ( DP_Print ) print_eg("search",&eg_search);
7087: #endif
7088: return r0;
7089: }
7090:
7091: /* for Fp, 2<=p<2^16 */
7092:
7093: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7094: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7095: {
7096: int spcol,sprow,a;
7097: int i,j,k,l,rank;
7098: NODE r0,r;
7099: ND_pairs sp;
7100: ND spol;
7101: UINT **spmat;
7102: UINT *svect,*v;
7103: int *colstat;
7104: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7105: int maxrs;
7106: int *spsugar;
7107: ND_pairs *spactive;
7108:
7109: spcol = col-nred;
7110: get_eg(&eg0);
7111: /* elimination (1st step) */
7112: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7113: svect = (UINT *)MALLOC(col*sizeof(UINT));
7114: spsugar = (int *)MALLOC(nsp*sizeof(int));
7115: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7116: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7117: nd_sp(m,0,sp,&spol);
7118: if ( !spol ) continue;
7119: nd_to_vect(m,s0vect,col,spol,svect);
7120: if ( m == -1 )
7121: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7122: else
7123: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7124: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7125: if ( i < col ) {
7126: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7127: for ( j = k = 0; j < col; j++ )
7128: if ( !rhead[j] ) v[k++] = svect[j];
7129: spsugar[sprow] = MAX(maxrs,SG(spol));
7130: if ( nz )
7131: spactive[sprow] = sp;
7132: sprow++;
7133: }
7134: nd_free(spol);
7135: }
7136: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7137: if ( DP_Print ) {
7138: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7139: fflush(asir_out);
7140: }
7141: /* free index arrays */
7142: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7143:
7144: /* elimination (2nd step) */
7145: colstat = (int *)MALLOC(spcol*sizeof(int));
7146: if ( m == -1 )
7147: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7148: else
7149: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7150: r0 = 0;
7151: for ( i = 0; i < rank; i++ ) {
7152: NEXTNODE(r0,r); BDY(r) =
7153: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7154: SG((NDV)BDY(r)) = spsugar[i];
7155: GCFREE(spmat[i]);
7156: }
7157: if ( r0 ) NEXT(r) = 0;
7158:
7159: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7160: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7161: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7162: if ( DP_Print ) {
7163: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7164: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7165: nsp,nred,sprow,spcol,rank);
7166: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7167: }
7168: if ( nz ) {
7169: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7170: if ( rank > 0 ) {
7171: NEXT(spactive[rank-1]) = 0;
7172: *nz = spactive[0];
7173: } else
7174: *nz = 0;
7175: }
7176: return r0;
7177: }
7178:
7179: #if defined(__GNUC__) && SIZEOF_LONG==8
7180: /* for Fp, 2^15=<p<2^29 */
7181:
7182: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7183: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7184: {
7185: int spcol,sprow,a;
7186: int i,j,k,l,rank;
7187: NODE r0,r;
7188: ND_pairs sp;
7189: ND spol;
7190: U64 **spmat;
7191: U64 *svect,*cvect;
7192: U64 *v;
7193: int *colstat;
7194: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7195: int maxrs;
7196: int *spsugar;
7197: ND_pairs *spactive;
7198:
7199: spcol = col-nred;
7200: get_eg(&eg0);
7201: /* elimination (1st step) */
7202: spmat = (U64 **)MALLOC(nsp*sizeof(U64 *));
7203: svect = (U64 *)MALLOC(col*sizeof(U64));
7204: cvect = (U64 *)MALLOC(col*sizeof(U64));
7205: spsugar = (int *)MALLOC(nsp*sizeof(int));
7206: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7207: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7208: nd_sp(m,0,sp,&spol);
7209: if ( !spol ) continue;
7210: nd_to_vect64(m,s0vect,col,spol,svect);
7211: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
7212: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7213: if ( i < col ) {
7214: spmat[sprow] = v = (U64 *)MALLOC_ATOMIC(spcol*sizeof(U64));
7215: for ( j = k = 0; j < col; j++ )
7216: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
7217: spsugar[sprow] = MAX(maxrs,SG(spol));
7218: if ( nz )
7219: spactive[sprow] = sp;
7220: sprow++;
7221: }
7222: nd_free(spol);
7223: }
7224: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7225: if ( DP_Print ) {
7226: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7227: fflush(asir_out);
7228: }
7229: /* free index arrays */
7230: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7231:
7232: /* elimination (2nd step) */
7233: colstat = (int *)MALLOC(spcol*sizeof(int));
7234: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7235: r0 = 0;
7236: for ( i = 0; i < rank; i++ ) {
7237: NEXTNODE(r0,r); BDY(r) =
7238: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7239: SG((NDV)BDY(r)) = spsugar[i];
7240: GCFREE(spmat[i]);
7241: }
7242: if ( r0 ) NEXT(r) = 0;
7243:
7244: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7245: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7246: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7247: if ( DP_Print ) {
7248: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7249: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7250: nsp,nred,sprow,spcol,rank);
7251: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7252: }
7253: if ( nz ) {
7254: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7255: if ( rank > 0 ) {
7256: NEXT(spactive[rank-1]) = 0;
7257: *nz = spactive[0];
7258: } else
7259: *nz = 0;
7260: }
7261: return r0;
7262: }
7263: #endif
7264:
7265: /* for small finite fields */
7266:
7267: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7268: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7269: {
7270: int spcol,sprow,a;
7271: int i,j,k,l,rank;
7272: NODE r0,r;
7273: ND_pairs sp;
7274: ND spol;
7275: UINT **spmat;
7276: UINT *svect,*v;
7277: int *colstat;
7278: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7279: int maxrs;
7280: int *spsugar;
7281: ND_pairs *spactive;
7282:
7283: spcol = col-nred;
7284: get_eg(&eg0);
7285: /* elimination (1st step) */
7286: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7287: svect = (UINT *)MALLOC(col*sizeof(UINT));
7288: spsugar = (int *)MALLOC(nsp*sizeof(int));
7289: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7290: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7291: nd_sp(m,0,sp,&spol);
7292: if ( !spol ) continue;
7293: nd_to_vect(m,s0vect,col,spol,svect);
7294: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7295: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7296: if ( i < col ) {
7297: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7298: for ( j = k = 0; j < col; j++ )
7299: if ( !rhead[j] ) v[k++] = svect[j];
7300: spsugar[sprow] = MAX(maxrs,SG(spol));
7301: if ( nz )
7302: spactive[sprow] = sp;
7303: sprow++;
7304: }
7305: nd_free(spol);
7306: }
7307: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7308: if ( DP_Print ) {
7309: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7310: fflush(asir_out);
7311: }
7312: /* free index arrays */
7313: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7314:
7315: /* elimination (2nd step) */
7316: colstat = (int *)MALLOC(spcol*sizeof(int));
7317: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7318: r0 = 0;
7319: for ( i = 0; i < rank; i++ ) {
7320: NEXTNODE(r0,r); BDY(r) =
7321: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7322: SG((NDV)BDY(r)) = spsugar[i];
7323: GCFREE(spmat[i]);
7324: }
7325: if ( r0 ) NEXT(r) = 0;
7326:
7327: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7328: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7329: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7330: if ( DP_Print ) {
7331: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7332: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7333: nsp,nred,sprow,spcol,rank);
7334: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7335: }
7336: if ( nz ) {
7337: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7338: if ( rank > 0 ) {
7339: NEXT(spactive[rank-1]) = 0;
7340: *nz = spactive[0];
7341: } else
7342: *nz = 0;
7343: }
7344: return r0;
7345: }
7346:
7347: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7348: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7349: {
7350: int spcol,sprow,a;
7351: int i,j,k,l,rank;
7352: NODE r0,r;
7353: ND_pairs sp;
7354: ND spol;
7355: mpz_t **spmat;
7356: mpz_t *svect,*v;
7357: int *colstat;
7358: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7359: int maxrs;
7360: int *spsugar;
7361: pointer *w;
7362:
7363: spcol = col-nred;
7364: get_eg(&eg0);
7365: /* elimination (1st step) */
7366: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7367: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7368: spsugar = (int *)MALLOC(nsp*sizeof(int));
7369: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7370: nd_sp(m,trace,sp,&spol);
7371: if ( !spol ) continue;
7372: nd_to_vect_lf(s0vect,col,spol,svect);
7373: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7374: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7375: if ( i < col ) {
7376: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7377: for ( j = k = 0; j < col; j++ )
7378: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7379: spsugar[sprow] = MAX(maxrs,SG(spol));
7380: sprow++;
7381: }
7382: /* nd_free(spol); */
7383: }
7384: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7385: if ( DP_Print ) {
7386: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7387: fflush(asir_out);
7388: }
7389: /* free index arrays */
7390: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7391:
7392: /* elimination (2nd step) */
7393: colstat = (int *)MALLOC(spcol*sizeof(int));
7394: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7395: w = (pointer *)MALLOC(rank*sizeof(pointer));
7396: for ( i = 0; i < rank; i++ ) {
7397: #if 0
7398: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7399: SG((NDV)w[rank-i-1]) = spsugar[i];
7400: #else
7401: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7402: SG((NDV)w[i]) = spsugar[i];
7403: #endif
7404: /* GCFREE(spmat[i]); */
7405:
7406: }
7407: #if 0
7408: qsort(w,rank,sizeof(NDV),
7409: (int (*)(const void *,const void *))ndv_compare);
7410: #endif
7411: r0 = 0;
7412: for ( i = 0; i < rank; i++ ) {
7413: NEXTNODE(r0,r); BDY(r) = w[i];
7414: }
7415: if ( r0 ) NEXT(r) = 0;
7416:
7417: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7418: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7419: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7420: if ( DP_Print ) {
7421: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7422: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7423: nsp,nred,sprow,spcol,rank);
7424: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7425: }
7426: return r0;
7427: }
7428:
7429: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7430: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7431: {
7432: int spcol,sprow,a;
7433: int i,j,k,l,rank;
7434: NODE r0,r;
7435: ND_pairs sp;
7436: ND spol;
7437: Z **spmat;
7438: Z *svect,*v;
7439: int *colstat;
7440: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7441: int maxrs;
7442: int *spsugar;
7443: pointer *w;
7444:
7445: spcol = col-nred;
7446: get_eg(&eg0);
7447: /* elimination (1st step) */
7448: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7449: svect = (Z *)MALLOC(col*sizeof(Q));
7450: spsugar = (int *)MALLOC(nsp*sizeof(int));
7451: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7452: nd_sp(0,trace,sp,&spol);
7453: if ( !spol ) continue;
7454: nd_to_vect_q(s0vect,col,spol,svect);
7455: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7456: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7457: if ( i < col ) {
7458: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7459: for ( j = k = 0; j < col; j++ )
7460: if ( !rhead[j] ) v[k++] = svect[j];
7461: spsugar[sprow] = MAX(maxrs,SG(spol));
7462: sprow++;
7463: }
7464: /* nd_free(spol); */
7465: }
7466: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7467: if ( DP_Print ) {
7468: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
7469: fflush(asir_out);
7470: }
7471: /* free index arrays */
7472: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7473:
7474: /* elimination (2nd step) */
7475: colstat = (int *)MALLOC(spcol*sizeof(int));
7476: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7477: w = (pointer *)MALLOC(rank*sizeof(pointer));
7478: for ( i = 0; i < rank; i++ ) {
7479: #if 0
7480: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7481: SG((NDV)w[rank-i-1]) = spsugar[i];
7482: #else
7483: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7484: SG((NDV)w[i]) = spsugar[i];
7485: #endif
7486: /* GCFREE(spmat[i]); */
7487: }
7488: #if 0
7489: qsort(w,rank,sizeof(NDV),
7490: (int (*)(const void *,const void *))ndv_compare);
7491: #endif
7492: r0 = 0;
7493: for ( i = 0; i < rank; i++ ) {
7494: NEXTNODE(r0,r); BDY(r) = w[i];
7495: }
7496: if ( r0 ) NEXT(r) = 0;
7497:
7498: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7499: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7500: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7501: if ( DP_Print ) {
7502: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7503: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7504: nsp,nred,sprow,spcol,rank);
7505: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
7506: }
7507: return r0;
7508: }
7509:
7510: FILE *nd_write,*nd_read;
7511:
7512: void nd_send_int(int a) {
7513: write_int(nd_write,(unsigned int *)&a);
7514: }
7515:
7516: void nd_send_intarray(int *p,int len) {
7517: write_intarray(nd_write,(unsigned int *)p,len);
7518: }
7519:
7520: int nd_recv_int() {
7521: int a;
7522:
7523: read_int(nd_read,(unsigned int *)&a);
7524: return a;
7525: }
7526:
7527: void nd_recv_intarray(int *p,int len) {
7528: read_intarray(nd_read,(unsigned int *)p,len);
7529: }
7530:
7531: void nd_send_ndv(NDV p) {
7532: int len,i;
7533: NMV m;
7534:
7535: if ( !p ) nd_send_int(0);
7536: else {
7537: len = LEN(p);
7538: nd_send_int(len);
7539: m = BDY(p);
7540: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7541: nd_send_int(CM(m));
7542: nd_send_intarray((int *)DL(m),nd_wpd);
7543: }
7544: }
7545: }
7546:
7547: void nd_send_nd(ND p) {
7548: int len,i;
7549: NM m;
7550:
7551: if ( !p ) nd_send_int(0);
7552: else {
7553: len = LEN(p);
7554: nd_send_int(len);
7555: m = BDY(p);
7556: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7557: nd_send_int(CM(m));
7558: nd_send_intarray((int *)DL(m),nd_wpd);
7559: }
7560: }
7561: }
7562:
7563: NDV nd_recv_ndv()
7564: {
7565: int len,i;
7566: NMV m,m0;
7567: NDV r;
7568:
7569: len = nd_recv_int();
7570: if ( !len ) return 0;
7571: else {
7572: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7573: #if 0
7574: ndv_alloc += len*nmv_adv;
7575: #endif
7576: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7577: CM(m) = nd_recv_int();
7578: nd_recv_intarray((int *)DL(m),nd_wpd);
7579: }
7580: MKNDV(nd_nvar,m0,len,r);
7581: return r;
7582: }
7583: }
7584:
7585: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7586: {
7587: int i,j,t,c,rank,inv;
7588: int *ci,*ri;
7589: Z dn;
7590: MAT m,nm;
7591:
7592: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7593: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7594: for ( i = 0; i < row; i++ )
7595: for ( j = 0; j < col; j++ )
7596: mat0[i][j] = 0;
7597: c = col-rank;
7598: for ( i = 0; i < rank; i++ ) {
7599: mat0[i][ri[i]] = dn;
7600: for ( j = 0; j < c; j++ )
7601: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7602: }
7603: return rank;
7604: }
7605:
7606: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7607: {
7608: int i,j,k,l,inv,a,rank,s;
7609: unsigned int *t,*pivot,*pk;
7610: unsigned int **mat;
7611: ND_pairs pair;
7612:
7613: mat = (unsigned int **)mat0;
7614: for ( rank = 0, j = 0; j < col; j++ ) {
7615: for ( i = rank; i < row; i++ )
7616: mat[i][j] %= md;
7617: for ( i = rank; i < row; i++ )
7618: if ( mat[i][j] )
7619: break;
7620: if ( i == row ) {
7621: colstat[j] = 0;
7622: continue;
7623: } else
7624: colstat[j] = 1;
7625: if ( i != rank ) {
7626: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7627: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7628: if ( spactive ) {
7629: pair = spactive[i]; spactive[i] = spactive[rank];
7630: spactive[rank] = pair;
7631: }
7632: }
7633: pivot = mat[rank];
7634: s = sugar[rank];
7635: inv = invm(pivot[j],md);
7636: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7637: if ( *pk ) {
7638: if ( *pk >= (unsigned int)md )
7639: *pk %= md;
7640: DMAR(*pk,inv,0,md,*pk)
7641: }
7642: for ( i = rank+1; i < row; i++ ) {
7643: t = mat[i];
7644: if ( (a = t[j]) != 0 ) {
7645: sugar[i] = MAX(sugar[i],s);
7646: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7647: }
7648: }
7649: rank++;
7650: }
7651: for ( j = col-1, l = rank-1; j >= 0; j-- )
7652: if ( colstat[j] ) {
7653: pivot = mat[l];
7654: s = sugar[l];
7655: for ( i = 0; i < l; i++ ) {
7656: t = mat[i];
7657: t[j] %= md;
7658: if ( (a = t[j]) != 0 ) {
7659: sugar[i] = MAX(sugar[i],s);
7660: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7661: }
7662: }
7663: l--;
7664: }
7665: for ( j = 0, l = 0; l < rank; j++ )
7666: if ( colstat[j] ) {
7667: t = mat[l];
7668: for ( k = j; k < col; k++ )
7669: if ( t[k] >= (unsigned int)md )
7670: t[k] %= md;
7671: l++;
7672: }
7673: return rank;
7674: }
7675:
7676: #if defined(__GNUC__) && SIZEOF_LONG==8
7677:
7678: int nd_gauss_elim_mod64(U64 **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7679: {
7680: int i,j,k,l,rank,s;
7681: U64 inv;
7682: U64 a;
7683: UINT c;
7684: U64 *t,*pivot,*pk;
7685: UINT *ck;
7686: UINT **cmat;
7687: UINT *ct;
7688: ND_pairs pair;
7689:
7690: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
7691: for ( i = 0; i < row; i++ ) {
7692: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
7693: bzero(cmat[i],col*sizeof(UINT));
7694: }
7695:
7696: for ( rank = 0, j = 0; j < col; j++ ) {
7697: for ( i = rank; i < row; i++ ) {
7698: a = mat[i][j]; c = cmat[i][j];
7699: MOD128(a,c,md);
7700: mat[i][j] = a; cmat[i][j] = 0;
7701: }
7702: for ( i = rank; i < row; i++ )
7703: if ( mat[i][j] )
7704: break;
7705: if ( i == row ) {
7706: colstat[j] = 0;
7707: continue;
7708: } else
7709: colstat[j] = 1;
7710: if ( i != rank ) {
7711: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7712: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
7713: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7714: if ( spactive ) {
7715: pair = spactive[i]; spactive[i] = spactive[rank];
7716: spactive[rank] = pair;
7717: }
7718: }
7719: /* column j is normalized */
7720: s = sugar[rank];
7721: inv = invm((UINT)mat[rank][j],md);
7722: /* normalize pivot row */
7723: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
7724: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
7725: }
7726: for ( i = rank+1; i < row; i++ ) {
7727: if ( (a = mat[i][j]) != 0 ) {
7728: sugar[i] = MAX(sugar[i],s);
7729: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
7730: }
7731: }
7732: rank++;
7733: }
7734: for ( j = col-1, l = rank-1; j >= 0; j-- )
7735: if ( colstat[j] ) {
7736: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
7737: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
7738: }
7739: s = sugar[l];
7740: for ( i = 0; i < l; i++ ) {
7741: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
7742: if ( a ) {
7743: sugar[i] = MAX(sugar[i],s);
7744: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
7745: }
7746: }
7747: l--;
7748: }
7749: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
7750: GCFREE(cmat);
7751: return rank;
7752: }
7753: #endif
7754:
7755: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
7756: {
7757: int i,j,k,l,inv,a,rank,s;
7758: unsigned int *t,*pivot,*pk;
7759: unsigned int **mat;
7760:
7761: mat = (unsigned int **)mat0;
7762: for ( rank = 0, j = 0; j < col; j++ ) {
7763: for ( i = rank; i < row; i++ )
7764: if ( mat[i][j] )
7765: break;
7766: if ( i == row ) {
7767: colstat[j] = 0;
7768: continue;
7769: } else
7770: colstat[j] = 1;
7771: if ( i != rank ) {
7772: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7773: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7774: }
7775: pivot = mat[rank];
7776: s = sugar[rank];
7777: inv = _invsf(pivot[j]);
7778: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7779: if ( *pk )
7780: *pk = _mulsf(*pk,inv);
7781: for ( i = rank+1; i < row; i++ ) {
7782: t = mat[i];
7783: if ( (a = t[j]) != 0 ) {
7784: sugar[i] = MAX(sugar[i],s);
7785: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7786: }
7787: }
7788: rank++;
7789: }
7790: for ( j = col-1, l = rank-1; j >= 0; j-- )
7791: if ( colstat[j] ) {
7792: pivot = mat[l];
7793: s = sugar[l];
7794: for ( i = 0; i < l; i++ ) {
7795: t = mat[i];
7796: if ( (a = t[j]) != 0 ) {
7797: sugar[i] = MAX(sugar[i],s);
7798: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7799: }
7800: }
7801: l--;
7802: }
7803: return rank;
7804: }
7805:
7806: int ndv_ishomo(NDV p)
7807: {
7808: NMV m;
7809: int len,h;
7810:
7811: if ( !p ) return 1;
7812: len = LEN(p);
7813: m = BDY(p);
7814: h = TD(DL(m));
7815: NMV_ADV(m);
7816: for ( len--; len; len--, NMV_ADV(m) )
7817: if ( TD(DL(m)) != h ) return 0;
7818: return 1;
7819: }
7820:
7821: void ndv_save(NDV p,int index)
7822: {
7823: FILE *s;
7824: char name[BUFSIZ];
7825: short id;
7826: int nv,sugar,len,n,i,td,e,j;
7827: NMV m;
7828: unsigned int *dl;
7829: int mpos;
7830:
7831: sprintf(name,"%s/%d",Demand,index);
7832: s = fopen(name,"w");
7833: savevl(s,0);
7834: if ( !p ) {
7835: saveobj(s,0);
7836: return;
7837: }
7838: id = O_DP;
7839: nv = NV(p);
7840: sugar = SG(p);
7841: len = LEN(p);
7842: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7843: write_int(s,(unsigned int *)&len);
7844:
7845: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7846: saveobj(s,(Obj)CQ(m));
7847: dl = DL(m);
7848: td = TD(dl);
7849: write_int(s,(unsigned int *)&td);
7850: for ( j = 0; j < nv; j++ ) {
7851: e = GET_EXP(dl,j);
7852: write_int(s,(unsigned int *)&e);
7853: }
7854: if ( nd_module ) {
7855: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7856: }
7857: }
7858: fclose(s);
7859: }
7860:
7861: void nd_save_mod(ND p,int index)
7862: {
7863: FILE *s;
7864: char name[BUFSIZ];
7865: int nv,sugar,len,c;
7866: NM m;
7867:
7868: sprintf(name,"%s/%d",Demand,index);
7869: s = fopen(name,"w");
7870: if ( !p ) {
7871: len = 0;
7872: write_int(s,(unsigned int *)&len);
7873: fclose(s);
7874: return;
7875: }
7876: nv = NV(p);
7877: sugar = SG(p);
7878: len = LEN(p);
7879: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7880: for ( m = BDY(p); m; m = NEXT(m) ) {
7881: c = CM(m); write_int(s,(unsigned int *)&c);
7882: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7883: }
7884: fclose(s);
7885: }
7886:
7887: NDV ndv_load(int index)
7888: {
7889: FILE *s;
7890: char name[BUFSIZ];
7891: short id;
7892: int nv,sugar,len,n,i,td,e,j;
7893: NDV d;
7894: NMV m0,m;
7895: unsigned int *dl;
7896: Obj obj;
7897: int mpos;
7898:
7899: sprintf(name,"%s/%d",Demand,index);
7900: s = fopen(name,"r");
7901: if ( !s ) return 0;
7902:
7903: skipvl(s);
7904: read_short(s,(unsigned short *)&id);
7905: if ( !id ) return 0;
7906: read_int(s,(unsigned int *)&nv);
7907: read_int(s,(unsigned int *)&sugar);
7908: read_int(s,(unsigned int *)&len);
7909:
7910: m0 = m = MALLOC(len*nmv_adv);
7911: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7912: loadobj(s,&obj); CQ(m) = (Z)obj;
7913: dl = DL(m);
7914: ndl_zero(dl);
7915: read_int(s,(unsigned int *)&td); TD(dl) = td;
7916: for ( j = 0; j < nv; j++ ) {
7917: read_int(s,(unsigned int *)&e);
7918: PUT_EXP(dl,j,e);
7919: }
7920: if ( nd_module ) {
7921: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7922: }
7923: if ( nd_blockmask ) ndl_weight_mask(dl);
7924: }
7925: fclose(s);
7926: MKNDV(nv,m0,len,d);
7927: SG(d) = sugar;
7928: return d;
7929: }
7930:
7931: ND nd_load_mod(int index)
7932: {
7933: FILE *s;
7934: char name[BUFSIZ];
7935: int nv,sugar,len,i,c;
7936: ND d;
7937: NM m0,m;
7938:
7939: sprintf(name,"%s/%d",Demand,index);
7940: s = fopen(name,"r");
7941: /* if the file does not exist, it means p[index]=0 */
7942: if ( !s ) return 0;
7943:
7944: read_int(s,(unsigned int *)&nv);
7945: if ( !nv ) { fclose(s); return 0; }
7946:
7947: read_int(s,(unsigned int *)&sugar);
7948: read_int(s,(unsigned int *)&len);
7949: for ( m0 = 0, i = 0; i < len; i++ ) {
7950: NEXTNM(m0,m);
7951: read_int(s,(unsigned int *)&c); CM(m) = c;
7952: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7953: }
7954: NEXT(m) = 0;
7955: MKND(nv,m0,len,d);
7956: SG(d) = sugar;
7957: fclose(s);
7958: return d;
7959: }
7960:
7961: void nd_det(int mod,MAT f,P *rp)
7962: {
7963: VL fv,tv;
7964: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7965: pointer **m;
7966: P **w;
7967: P mp,r;
7968: NDV **dm;
7969: NDV *t,*mi,*mj;
7970: NDV d,s,mij,mjj;
7971: ND u;
7972: NMV nmv;
7973: UINT *bound;
7974: PGeoBucket bucket;
7975: struct order_spec *ord;
7976: Z dq,dt,ds;
7977: Z mone;
7978: Z gn,qn,dn0,nm,dn;
7979:
7980: create_order_spec(0,0,&ord);
7981: nd_init_ord(ord);
7982: get_vars((Obj)f,&fv);
7983: if ( f->row != f->col )
7984: error("nd_det : non-square matrix");
7985: n = f->row;
7986: m = f->body;
7987: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7988:
7989: if ( !nvar ) {
7990: if ( !mod )
7991: detp(CO,(P **)m,n,rp);
7992: else {
7993: w = (P **)almat_pointer(n,n);
7994: for ( i = 0; i < n; i++ )
7995: for ( j = 0; j < n; j++ )
7996: ptomp(mod,(P)m[i][j],&w[i][j]);
7997: detmp(CO,mod,w,n,&mp);
7998: mptop(mp,rp);
7999: }
8000: return;
8001: }
8002:
8003: if ( !mod ) {
8004: w = (P **)almat_pointer(n,n);
8005: dq = ONE;
8006: for ( i = 0; i < n; i++ ) {
8007: dn0 = ONE;
8008: for ( j = 0; j < n; j++ ) {
8009: if ( !m[i][j] ) continue;
8010: lgp(m[i][j],&nm,&dn);
8011: gcdz(dn0,dn,&gn); divz(dn0,gn,&qn); mulz(qn,dn,&dn0);
8012: }
8013: if ( !UNIZ(dn0) ) {
8014: ds = dn0;
8015: for ( j = 0; j < n; j++ )
8016: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8017: mulz(dq,ds,&dt); dq = dt;
8018: } else
8019: for ( j = 0; j < n; j++ )
8020: w[i][j] = (P)m[i][j];
8021: }
8022: m = (pointer **)w;
8023: }
8024:
8025: for ( i = 0, max = 1; i < n; i++ )
8026: for ( j = 0; j < n; j++ )
8027: for ( tv = fv; tv; tv = NEXT(tv) ) {
8028: e = getdeg(tv->v,(P)m[i][j]);
8029: max = MAX(e,max);
8030: }
8031: nd_setup_parameters(nvar,max);
8032: dm = (NDV **)almat_pointer(n,n);
8033: for ( i = 0, max = 1; i < n; i++ )
8034: for ( j = 0; j < n; j++ ) {
8035: dm[i][j] = ptondv(CO,fv,m[i][j]);
8036: if ( mod ) ndv_mod(mod,dm[i][j]);
8037: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8038: }
8039: d = ptondv(CO,fv,(P)ONE);
8040: if ( mod ) ndv_mod(mod,d);
8041: chsgnz(ONE,&mone);
8042: for ( j = 0, sgn = 1; j < n; j++ ) {
8043: if ( DP_Print ) {
8044: fprintf(asir_out,".");
8045: }
8046: for ( i = j; i < n && !dm[i][j]; i++ );
8047: if ( i == n ) {
8048: *rp = 0;
8049: return;
8050: }
8051: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8052: for ( k = j; k < n; k++ )
8053: for ( l = j; l < n; l++ )
8054: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8055: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8056: }
8057: if ( k0 != j ) {
8058: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8059: sgn = -sgn;
8060: }
8061: if ( l0 != j ) {
8062: for ( k = j; k < n; k++ ) {
8063: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8064: }
8065: sgn = -sgn;
8066: }
8067: bound = nd_det_compute_bound(dm,n,j);
8068: for ( k = 0; k < nd_nvar; k++ )
8069: if ( bound[k]*2 > nd_mask0 ) break;
8070: if ( k < nd_nvar )
8071: nd_det_reconstruct(dm,n,j,d);
8072:
8073: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8074: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8075: mi = dm[i]; mij = mi[j];
8076: if ( mod )
8077: ndv_mul_c(mod,mij,mod-1);
8078: else
8079: ndv_mul_c_q(mij,mone);
8080: for ( k = j+1; k < n; k++ ) {
8081: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
8082: bucket = create_pbucket();
8083: if ( mi[k] ) {
8084: nmv = BDY(mjj); len = LEN(mjj);
8085: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8086: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8087: add_pbucket(mod,bucket,u);
8088: }
8089: }
8090: if ( mj[k] && mij ) {
8091: nmv = BDY(mij); len = LEN(mij);
8092: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8093: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8094: add_pbucket(mod,bucket,u);
8095: }
8096: }
8097: u = nd_quo(mod,bucket,d);
8098: mi[k] = ndtondv(mod,u);
8099: }
8100: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
8101: }
8102: d = mjj;
8103: }
8104: if ( DP_Print ) {
8105: fprintf(asir_out,"\n");
8106: }
8107: if ( sgn < 0 ) {
8108: if ( mod )
8109: ndv_mul_c(mod,d,mod-1);
8110: else
8111: ndv_mul_c_q(d,mone);
8112: }
8113: r = ndvtop(mod,CO,fv,d);
8114: if ( !mod && !UNIQ(dq) )
8115: divsp(CO,r,(P)dq,rp);
8116: else
8117: *rp = r;
8118: }
8119:
8120: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8121: {
8122: NM mr,mr0;
8123: NM tnm;
8124: NMV m;
8125: UINT *d0,*dt,*dm;
8126: int c,n,td,i,c1,c2,len;
8127: Z q;
8128: ND r;
8129:
8130: if ( !p ) return 0;
8131: else {
8132: n = NV(p); m = BDY(p); len = LEN(p);
8133: d0 = DL(m0);
8134: td = TD(d);
8135: mr0 = 0;
8136: NEWNM(tnm);
8137: if ( mod ) {
8138: c = CM(m0);
8139: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8140: ndl_add(DL(m),d0,DL(tnm));
8141: if ( ndl_reducible(DL(tnm),d) ) {
8142: NEXTNM(mr0,mr);
8143: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8144: ndl_copy(DL(tnm),DL(mr));
8145: }
8146: }
8147: } else {
8148: q = CQ(m0);
8149: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8150: ndl_add(DL(m),d0,DL(tnm));
8151: if ( ndl_reducible(DL(tnm),d) ) {
8152: NEXTNM(mr0,mr);
8153: mulz(CQ(m),q,&CQ(mr));
8154: ndl_copy(DL(tnm),DL(mr));
8155: }
8156: }
8157: }
8158: if ( !mr0 )
8159: return 0;
8160: else {
8161: NEXT(mr) = 0;
8162: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8163: MKND(NV(p),mr0,len,r);
8164: SG(r) = SG(p) + TD(d0);
8165: return r;
8166: }
8167: }
8168: }
8169:
8170: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8171: {
8172: int i,obpe,oadv,h,k,l;
8173: static NM prev_nm_free_list;
8174: EPOS oepos;
8175:
8176: obpe = nd_bpe;
8177: oadv = nmv_adv;
8178: oepos = nd_epos;
8179: if ( obpe < 2 ) nd_bpe = 2;
8180: else if ( obpe < 3 ) nd_bpe = 3;
8181: else if ( obpe < 4 ) nd_bpe = 4;
8182: else if ( obpe < 5 ) nd_bpe = 5;
8183: else if ( obpe < 6 ) nd_bpe = 6;
8184: else if ( obpe < 8 ) nd_bpe = 8;
8185: else if ( obpe < 10 ) nd_bpe = 10;
8186: else if ( obpe < 16 ) nd_bpe = 16;
8187: else if ( obpe < 32 ) nd_bpe = 32;
8188: else error("nd_det_reconstruct : exponent too large");
8189:
8190: nd_setup_parameters(nd_nvar,0);
8191: prev_nm_free_list = _nm_free_list;
8192: _nm_free_list = 0;
8193: for ( k = j; k < n; k++ )
8194: for (l = j; l < n; l++ )
8195: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8196: ndv_realloc(d,obpe,oadv,oepos);
8197: prev_nm_free_list = 0;
8198: #if 0
8199: GC_gcollect();
8200: #endif
8201: }
8202:
8203: /* returns a UINT array containing degree bounds */
8204:
8205: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8206: {
8207: UINT *d0,*d1,*d,*t,*r;
8208: int k,l,i;
8209:
8210: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8211: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8212: for ( k = j; k < n; k++ )
8213: for ( l = j; l < n; l++ )
8214: if ( dm[k][l] ) {
8215: d = ndv_compute_bound(dm[k][l]);
8216: for ( i = 0; i < nd_nvar; i++ )
8217: d0[i] = MAX(d0[i],d[i]);
8218: }
8219: return d0;
8220: }
8221:
8222: DL nd_separate_d(UINT *d,UINT *trans)
8223: {
8224: int n,td,i,e,j;
8225: DL a;
8226:
8227: ndl_zero(trans);
8228: td = 0;
8229: for ( i = 0; i < nd_ntrans; i++ ) {
8230: e = GET_EXP(d,i);
8231: PUT_EXP(trans,i,e);
8232: td += MUL_WEIGHT(e,i);
8233: }
8234: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8235: /* homogenized */
8236: i = nd_nvar-1;
8237: e = GET_EXP(d,i);
8238: PUT_EXP(trans,i,e);
8239: td += MUL_WEIGHT(e,i);
8240: }
8241: TD(trans) = td;
8242: if ( nd_blockmask) ndl_weight_mask(trans);
8243: NEWDL(a,nd_nalg);
8244: td = 0;
8245: for ( i = 0; i < nd_nalg; i++ ) {
8246: j = nd_ntrans+i;
8247: e = GET_EXP(d,j);
8248: a->d[i] = e;
8249: td += e;
8250: }
8251: a->td = td;
8252: return a;
8253: }
8254:
8255: int nd_monic(int mod,ND *p)
8256: {
8257: UINT *trans,*t;
8258: DL alg;
8259: MP mp0,mp;
8260: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8261: ND r;
8262: DL dl;
8263: DP nm;
8264: NDV ndv;
8265: DAlg inv,cd;
8266: ND s,c;
8267: Z l,mul;
8268: Z ln;
8269: int n,ntrans,i,e,td,is_lc,len;
8270: NumberField nf;
8271: struct oEGT eg0,eg1;
8272:
8273: if ( !(nf = get_numberfield()) )
8274: error("nd_monic : current_numberfield is not set");
8275:
8276: /* Q coef -> DAlg coef */
8277: NEWNM(ma0); ma = ma0;
8278: m = BDY(*p);
8279: is_lc = 1;
8280: while ( 1 ) {
8281: NEWMP(mp0); mp = mp0;
8282: mp->c = (Obj)CQ(m);
8283: mp->dl = nd_separate_d(DL(m),DL(ma));
8284: NEWNM(mb);
8285: for ( m = NEXT(m); m; m = NEXT(m) ) {
8286: alg = nd_separate_d(DL(m),DL(mb));
8287: if ( !ndl_equal(DL(ma),DL(mb)) )
8288: break;
8289: NEXTMP(mp0,mp); mp->c = (Obj)CQ(m); mp->dl = alg;
8290: }
8291: NEXT(mp) = 0;
8292: MKDP(nd_nalg,mp0,nm);
8293: MKDAlg(nm,ONE,cd);
8294: if ( is_lc == 1 ) {
8295: /* if the lc is a rational number, we have nothing to do */
8296: if ( !mp0->dl->td )
8297: return 1;
8298:
8299: get_eg(&eg0);
8300: invdalg(cd,&inv);
8301: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8302: /* check the validity of inv */
8303: if ( mod && !remqi((Q)inv->dn,mod) )
8304: return 0;
8305: CA(ma) = nf->one;
8306: is_lc = 0;
8307: ln = ONE;
8308: } else {
8309: muldalg(cd,inv,&CA(ma));
8310: lcmz(ln,CA(ma)->dn,&ln);
8311: }
8312: if ( m ) {
8313: NEXT(ma) = mb; ma = mb;
8314: } else {
8315: NEXT(ma) = 0;
8316: break;
8317: }
8318: }
8319: /* l = lcm(denoms) */
8320: l = ln;
8321: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
8322: divz(l,CA(m)->dn,&mul);
8323: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8324: NEXTNM(mr0,mr);
8325: mulz((Z)mp->c,mul,&CQ(mr));
8326: dl = mp->dl;
8327: td = TD(DL(m));
8328: ndl_copy(DL(m),DL(mr));
8329: for ( i = 0; i < nd_nalg; i++ ) {
8330: e = dl->d[i];
8331: PUT_EXP(DL(mr),i+nd_ntrans,e);
8332: td += MUL_WEIGHT(e,i+nd_ntrans);
8333: }
8334: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8335: TD(DL(mr)) = td;
8336: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8337: }
8338: }
8339: NEXT(mr) = 0;
8340: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8341: MKND(NV(*p),mr0,len,r);
8342: /* XXX */
8343: SG(r) = SG(*p);
8344: nd_free(*p);
8345: *p = r;
8346: return 1;
8347: }
8348:
8349: NODE reverse_node(NODE n)
8350: {
8351: NODE t,t1;
8352:
8353: for ( t = 0; n; n = NEXT(n) ) {
8354: MKNODE(t1,BDY(n),t); t = t1;
8355: }
8356: return t;
8357: }
8358:
8359: P ndc_div(int mod,union oNDC a,union oNDC b)
8360: {
8361: union oNDC c;
8362: int inv,t;
8363:
8364: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
8365: else if ( mod == -2 ) divlf(a.gz,b.gz,&c.gz);
8366: else if ( mod ) {
8367: inv = invm(b.m,mod);
8368: DMAR(a.m,inv,0,mod,t); c.m = t;
8369: } else if ( nd_vc )
8370: divsp(nd_vc,a.p,b.p,&c.p);
8371: else
8372: divsz(a.z,b.z,&c.z);
8373: return ndctop(mod,c);
8374: }
8375:
8376: P ndctop(int mod,union oNDC c)
8377: {
8378: Z q;
8379: int e;
8380: GFS gfs;
8381:
8382: if ( mod == -1 ) {
8383: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8384: } else if ( mod == -2 ) {
8385: q = c.gz; return (P)q;
8386: } else if ( mod > 0 ) {
8387: STOQ(c.m,q); return (P)q;
8388: } else
8389: return (P)c.p;
8390: }
8391:
8392: /* [0,0,0,cont] = p -> p/cont */
8393:
8394: void finalize_tracelist(int i,P cont)
8395: {
8396: LIST l;
8397: NODE node;
8398: Z iq;
8399:
8400: if ( !UNIQ(cont) ) {
8401: node = mknode(4,NULLP,NULLP,NULLP,cont);
8402: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8403: nd_tracelist = node;
8404: }
8405: STOQ(i,iq);
8406: nd_tracelist = reverse_node(nd_tracelist);
8407: MKLIST(l,nd_tracelist);
8408: node = mknode(2,iq,l); MKLIST(l,node);
8409: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8410: nd_alltracelist = node; nd_tracelist = 0;
8411: }
8412:
8413: void conv_ilist(int demand,int trace,NODE g,int **indp)
8414: {
8415: int n,i,j;
8416: int *ind;
8417: NODE t;
8418:
8419: n = length(g);
8420: ind = (int *)MALLOC(n*sizeof(int));
8421: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8422: j = (long)BDY(t); ind[i] = j;
8423: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8424: }
8425: if ( indp ) *indp = ind;
8426: }
8427:
8428: void parse_nd_option(NODE opt)
8429: {
8430: NODE t,p,u;
8431: int i,s,n;
8432: char *key;
8433: Obj value;
8434:
8435: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8436: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8437: nd_splist = 0; nd_check_splist = 0;
8438: nd_sugarweight = 0;
8439: nd_f4red =0;
8440: nd_rank0 = 0;
8441: for ( t = opt; t; t = NEXT(t) ) {
8442: p = BDY((LIST)BDY(t));
8443: key = BDY((STRING)BDY(p));
8444: value = (Obj)BDY(NEXT(p));
8445: if ( !strcmp(key,"gentrace") )
8446: nd_gentrace = value?1:0;
8447: else if ( !strcmp(key,"gensyz") )
8448: nd_gensyz = value?1:0;
8449: else if ( !strcmp(key,"nora") )
8450: nd_nora = value?1:0;
8451: else if ( !strcmp(key,"gbblock") ) {
8452: if ( value && OID(value) == O_LIST ) {
8453: u = BDY((LIST)value);
8454: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8455: for ( i = 0; u; u = NEXT(u) ) {
8456: p = BDY((LIST)BDY(u));
8457: s = nd_gbblock[i++] = QTOS((Q)BDY(p));
8458: nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
8459: }
8460: nd_gbblock[i] = -1;
8461: } else
8462: nd_gbblock = 0;
8463: } else if ( !strcmp(key,"newelim") )
8464: nd_newelim = value?1:0;
8465: else if ( !strcmp(key,"intersect") )
8466: nd_intersect = value?1:0;
8467: else if ( !strcmp(key,"lf") )
8468: nd_lf = value?1:0;
8469: else if ( !strcmp(key,"trace") ) {
8470: if ( value ) {
8471: u = BDY((LIST)value);
8472: nd_nzlist = BDY((LIST)ARG2(u));
8473: nd_bpe = QTOS((Q)ARG3(u));
8474: }
8475: } else if ( !strcmp(key,"f4red") ) {
8476: nd_f4red = QTOS((Q)value);
8477: } else if ( !strcmp(key,"rank0") ) {
8478: nd_rank0 = value?1:0;
8479: } else if ( !strcmp(key,"splist") ) {
8480: nd_splist = value?1:0;
8481: } else if ( !strcmp(key,"check_splist") ) {
8482: nd_check_splist = BDY((LIST)value);
8483: } else if ( !strcmp(key,"sugarweight") ) {
8484: u = BDY((LIST)value);
8485: n = length(u);
8486: nd_sugarweight = MALLOC(n*sizeof(int));
8487: for ( i = 0; i < n; i++, u = NEXT(u) )
8488: nd_sugarweight[i] = QTOS((Q)BDY(u));
8489: }
8490: }
8491: }
8492:
8493: ND mdptond(DP d);
8494: ND nd_mul_nm(int mod,NM m0,ND p);
8495: ND nd_mul_nm_lf(NM m0,ND p);
8496: ND *btog(NODE ti,ND **p,int nb,int mod);
8497: ND btog_one(NODE ti,ND *p,int nb,int mod);
8498: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8499: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8500:
8501: /* d:monomial */
8502: ND mdptond(DP d)
8503: {
8504: NM m;
8505: ND r;
8506:
8507: if ( OID(d) == 1 )
8508: r = ptond(CO,CO,(P)d);
8509: else {
8510: NEWNM(m);
8511: dltondl(NV(d),BDY(d)->dl,DL(m));
8512: CQ(m) = (Z)BDY(d)->c;
8513: NEXT(m) = 0;
8514: MKND(NV(d),m,1,r);
8515: }
8516: return r;
8517: }
8518:
8519: ND nd_mul_nm(int mod,NM m0,ND p)
8520: {
8521: UINT *d0;
8522: int c0,c1,c;
8523: NM tm,mr,mr0;
8524: ND r;
8525:
8526: if ( !p ) return 0;
8527: d0 = DL(m0);
8528: c0 = CM(m0);
8529: mr0 = 0;
8530: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8531: NEXTNM(mr0,mr);
8532: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8533: ndl_add(d0,DL(tm),DL(mr));
8534: }
8535: NEXT(mr) = 0;
8536: MKND(NV(p),mr0,LEN(p),r);
8537: return r;
8538: }
8539:
8540: ND nd_mul_nm_lf(NM m0,ND p)
8541: {
8542: UINT *d0;
8543: Z c0,c1,c;
8544: NM tm,mr,mr0;
8545: ND r;
8546:
8547: if ( !p ) return 0;
8548: d0 = DL(m0);
8549: c0 = CZ(m0);
8550: mr0 = 0;
8551: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8552: NEXTNM(mr0,mr);
8553: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8554: ndl_add(d0,DL(tm),DL(mr));
8555: }
8556: NEXT(mr) = 0;
8557: MKND(NV(p),mr0,LEN(p),r);
8558: return r;
8559: }
8560:
8561: ND *btog(NODE ti,ND **p,int nb,int mod)
8562: {
8563: PGeoBucket *r;
8564: int i,ci;
8565: NODE t,s;
8566: ND m,tp;
8567: ND *pi,*rd;
8568: P c;
8569:
8570: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8571: for ( i = 0; i < nb; i++ )
8572: r[i] = create_pbucket();
8573: for ( t = ti; t; t = NEXT(t) ) {
8574: s = BDY((LIST)BDY(t));
8575: if ( ARG0(s) ) {
8576: m = mdptond((DP)ARG2(s));
8577: ptomp(mod,(P)HCQ(m),&c);
8578: if ( (ci = ((MQ)c)->cont) != 0 ) {
8579: HCM(m) = ci;
8580: pi = p[QTOS((Q)ARG1(s))];
8581: for ( i = 0; i < nb; i++ ) {
8582: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8583: add_pbucket(mod,r[i],tp);
8584: }
8585: }
8586: ci = 1;
8587: } else {
8588: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8589: ci = invm(ci,mod);
8590: }
8591: }
8592: rd = (ND *)MALLOC(nb*sizeof(ND));
8593: for ( i = 0; i < nb; i++ )
8594: rd[i] = normalize_pbucket(mod,r[i]);
8595: if ( ci != 1 )
8596: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8597: return rd;
8598: }
8599:
8600: /* YYY */
8601: ND *btog_lf(NODE ti,ND **p,int nb)
8602: {
8603: PGeoBucket *r;
8604: int i;
8605: NODE t,s;
8606: ND m,tp;
8607: ND *pi,*rd;
8608: LM lm;
8609: Z lf,c;
8610:
8611: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8612: for ( i = 0; i < nb; i++ )
8613: r[i] = create_pbucket();
8614: for ( t = ti; t; t = NEXT(t) ) {
8615: s = BDY((LIST)BDY(t));
8616: if ( ARG0(s) ) {
8617: m = mdptond((DP)ARG2(s));
8618: simp_ff((Obj)HCQ(m),(Obj *)&lm);
8619: if ( lm ) {
8620: lmtolf(lm,&lf); HCZ(m) = lf;
8621: pi = p[QTOS((Q)ARG1(s))];
8622: for ( i = 0; i < nb; i++ ) {
8623: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8624: add_pbucket(-2,r[i],tp);
8625: }
8626: }
8627: c = ONE;
8628: } else {
8629: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8630: }
8631: }
8632: rd = (ND *)MALLOC(nb*sizeof(ND));
8633: for ( i = 0; i < nb; i++ )
8634: rd[i] = normalize_pbucket(-2,r[i]);
8635: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8636: return rd;
8637: }
8638:
8639: ND btog_one(NODE ti,ND *p,int nb,int mod)
8640: {
8641: PGeoBucket r;
8642: int i,ci,j;
8643: NODE t,s;
8644: ND m,tp;
8645: ND pi,rd;
8646: P c;
8647:
8648: r = create_pbucket();
8649: for ( t = ti; t; t = NEXT(t) ) {
8650: s = BDY((LIST)BDY(t));
8651: if ( ARG0(s) ) {
8652: m = mdptond((DP)ARG2(s));
8653: ptomp(mod,(P)HCQ(m),&c);
8654: if ( (ci = ((MQ)c)->cont) != 0 ) {
8655: HCM(m) = ci;
8656: pi = p[j=QTOS((Q)ARG1(s))];
8657: if ( !pi ) {
8658: pi = nd_load_mod(j);
8659: tp = nd_mul_nm(mod,BDY(m),pi);
8660: nd_free(pi);
8661: add_pbucket(mod,r,tp);
8662: } else {
8663: tp = nd_mul_nm(mod,BDY(m),pi);
8664: add_pbucket(mod,r,tp);
8665: }
8666: }
8667: ci = 1;
8668: } else {
8669: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8670: ci = invm(ci,mod);
8671: }
8672: }
8673: rd = normalize_pbucket(mod,r);
8674: free_pbucket(r);
8675: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8676: return rd;
8677: }
8678:
8679: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8680:
8681: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8682: {
8683: int i,j,n,m,nb,pi0,pi1,nvar;
8684: VL fv,tv,vv;
8685: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8686: ND **p;
8687: ND *c;
8688: ND u;
8689: P inv;
8690: MAT mat;
8691:
8692: if ( mod == -2 )
8693: return nd_btog_lf(f,v,ord,tlist,rp);
8694:
8695: parse_nd_option(current_option);
8696: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8697: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8698: switch ( ord->id ) {
8699: case 1:
8700: if ( ord->nv != nvar )
8701: error("nd_check : invalid order specification");
8702: break;
8703: default:
8704: break;
8705: }
8706: nd_init_ord(ord);
8707: #if 0
8708: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8709: #else
8710: nd_bpe = 32;
8711: #endif
8712: nd_setup_parameters(nvar,0);
8713: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8714: intred = BDY((LIST)ARG3(BDY(tlist)));
8715: ind = BDY((LIST)ARG4(BDY(tlist)));
8716: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8717: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8718: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8719: if ( j > i ) i = j;
8720: }
8721: n = i+1;
8722: nb = length(BDY(f));
8723: p = (ND **)MALLOC(n*sizeof(ND *));
8724: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8725: pi = BDY((LIST)BDY(t));
8726: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8727: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8728: ptomp(mod,(P)ARG2(pi),&inv);
8729: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8730: u = ptond(CO,vv,(P)ONE);
8731: HCM(u) = ((MQ)inv)->cont;
8732: c[pi1] = u;
8733: }
8734: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8735: printf("%d ",i); fflush(stdout);
8736: ti = BDY((LIST)BDY(t));
8737: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
8738: }
8739: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8740: printf("%d ",i); fflush(stdout);
8741: ti = BDY((LIST)BDY(t));
8742: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
8743: }
8744: m = length(ind);
8745: MKMAT(mat,nb,m);
8746: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
8747: for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
8748: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8749: return mat;
8750: }
8751:
8752: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8753: {
8754: int i,j,n,m,nb,pi0,pi1,nvar;
8755: VL fv,tv,vv;
8756: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8757: ND **p;
8758: ND *c;
8759: ND u;
8760: MAT mat;
8761: LM lm;
8762: Z lf,inv;
8763:
8764: parse_nd_option(current_option);
8765: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8766: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8767: switch ( ord->id ) {
8768: case 1:
8769: if ( ord->nv != nvar )
8770: error("nd_check : invalid order specification");
8771: break;
8772: default:
8773: break;
8774: }
8775: nd_init_ord(ord);
8776: #if 0
8777: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8778: #else
8779: nd_bpe = 32;
8780: #endif
8781: nd_setup_parameters(nvar,0);
8782: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8783: intred = BDY((LIST)ARG3(BDY(tlist)));
8784: ind = BDY((LIST)ARG4(BDY(tlist)));
8785: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8786: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8787: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8788: if ( j > i ) i = j;
8789: }
8790: n = i+1;
8791: nb = length(BDY(f));
8792: p = (ND **)MALLOC(n*sizeof(ND *));
8793: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8794: pi = BDY((LIST)BDY(t));
8795: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8796: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8797: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8798: u = ptond(CO,vv,(P)ONE);
8799: HCZ(u) = inv;
8800: c[pi1] = u;
8801: }
8802: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8803: printf("%d ",i); fflush(stdout);
8804: ti = BDY((LIST)BDY(t));
8805: p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
8806: }
8807: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8808: printf("%d ",i); fflush(stdout);
8809: ti = BDY((LIST)BDY(t));
8810: p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
8811: }
8812: m = length(ind);
8813: MKMAT(mat,nb,m);
8814: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
8815: for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
8816: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8817: return mat;
8818: }
8819:
8820: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8821: LIST tlist,int pos,MAT *rp)
8822: {
8823: int i,j,n,m,nb,pi0,pi1,nvar;
8824: VL fv,tv,vv;
8825: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8826: ND *p;
8827: ND *c;
8828: ND u;
8829: P inv;
8830: VECT vect;
8831:
8832: if ( mod == -2 )
8833: error("nd_btog_one : not implemented yet for a large finite field");
8834:
8835: parse_nd_option(current_option);
8836: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8837: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8838: switch ( ord->id ) {
8839: case 1:
8840: if ( ord->nv != nvar )
8841: error("nd_check : invalid order specification");
8842: break;
8843: default:
8844: break;
8845: }
8846: nd_init_ord(ord);
8847: #if 0
8848: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8849: #else
8850: nd_bpe = 32;
8851: #endif
8852: nd_setup_parameters(nvar,0);
8853: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8854: intred = BDY((LIST)ARG3(BDY(tlist)));
8855: ind = BDY((LIST)ARG4(BDY(tlist)));
8856: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8857: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
8858: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8859: if ( j > i ) i = j;
8860: }
8861: n = i+1;
8862: nb = length(BDY(f));
8863: p = (ND *)MALLOC(n*sizeof(ND *));
8864: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8865: pi = BDY((LIST)BDY(t));
8866: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8867: if ( pi1 == pos ) {
8868: ptomp(mod,(P)ARG2(pi),&inv);
8869: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8870: u = ptond(CO,vv,(P)ONE);
8871: HCM(u) = ((MQ)inv)->cont;
8872: p[pi0] = u;
8873: }
8874: }
8875: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8876: printf("%d ",i); fflush(stdout);
8877: ti = BDY((LIST)BDY(t));
8878: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
8879: if ( Demand ) {
8880: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8881: }
8882: }
8883: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8884: printf("%d ",i); fflush(stdout);
8885: ti = BDY((LIST)BDY(t));
8886: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
8887: if ( Demand ) {
8888: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8889: }
8890: }
8891: m = length(ind);
8892: MKVECT(vect,m);
8893: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
8894: u = p[QTOS((Q)BDY(t))];
8895: if ( !u ) {
8896: u = nd_load_mod(QTOS((Q)BDY(t)));
8897: BDY(vect)[j] = ndtodp(mod,u);
8898: nd_free(u);
8899: } else
8900: BDY(vect)[j] = ndtodp(mod,u);
8901: }
8902: return vect;
8903: }
8904:
8905: void ndv_print_lf(NDV p)
8906: {
8907: NMV m;
8908: int i,len;
8909:
8910: if ( !p ) printf("0\n");
8911: else {
8912: len = LEN(p);
8913: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8914: printf("+");
8915: mpz_out_str(asir_out,10,BDY(CZ(m)));
8916: printf("*");
8917: ndl_print(DL(m));
8918: }
8919: printf("\n");
8920: }
8921: }
8922:
8923: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8924: {
8925: VL tv,fv,vv,vc,av;
8926: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8927: int m,nocheck,nvar,mindex,e,max;
8928: NDV c;
8929: NMV a;
8930: P p,zp;
8931: Q dmy;
8932: EPOS oepos;
8933: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8934: Alg alpha,dp;
8935: P poly;
8936: LIST f1,f2,zpl;
8937: Obj obj;
8938: NumberField nf;
8939: struct order_spec *ord1;
8940: struct oEGT eg_check,eg0,eg1;
8941: NODE tr,tl1,tl2,tl3,tl4;
8942: LIST l1,l2,l3,l4,l5;
8943: int *perm;
8944: int j,ret;
8945: NODE retn;
8946: Q jq,bpe;
8947:
8948: nd_module = 0;
8949: parse_nd_option(current_option);
8950: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8951: if ( nd_vc )
8952: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8953: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8954: switch ( ord->id ) {
8955: case 1:
8956: if ( ord->nv != nvar )
8957: error("nd_f4_lf_trace : invalid order specification");
8958: break;
8959: default:
8960: break;
8961: }
8962:
8963: nd_ntrans = nvar;
8964: nd_nalg = 0;
8965:
8966: nocheck = 0;
8967: mindex = 0;
8968:
8969: /* do not use on-demand load/save */
8970: nd_demand = 0;
8971: m = trace > 1 ? trace : get_lprime(mindex);
8972: nd_init_ord(ord);
8973: mrank = 0;
8974: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8975: for ( tv = vv; tv; tv = NEXT(tv) ) {
8976: if ( nd_module ) {
8977: s = BDY((LIST)BDY(t));
8978: trank = length(s);
8979: mrank = MAX(mrank,trank);
8980: for ( ; s; s = NEXT(s) ) {
8981: e = getdeg(tv->v,(P)BDY(s));
8982: max = MAX(e,max);
8983: }
8984: } else {
8985: e = getdeg(tv->v,(P)BDY(t));
8986: max = MAX(e,max);
8987: }
8988: }
8989: nd_setup_parameters(nvar,max);
8990: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8991: ishomo = 1;
8992: /* XXX */
8993: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8994: if ( nd_module ) {
8995: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8996: } else {
8997: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8998: }
8999: if ( ishomo )
9000: ishomo = ishomo && ndv_ishomo(c);
9001: if ( c ) {
9002: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9003: ndv_mod(-2,c);
9004: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9005: }
9006: }
9007: if ( in0 ) NEXT(in) = 0;
9008: if ( fd0 ) NEXT(fd) = 0;
9009: if ( !ishomo && homo ) {
9010: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9011: c = (NDV)BDY(t); len = LEN(c);
9012: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9013: wmax = MAX(TD(DL(a)),wmax);
9014: }
9015: homogenize_order(ord,nvar,&ord1);
9016: nd_init_ord(ord1);
9017: nd_setup_parameters(nvar+1,wmax);
9018: for ( t = fd0; t; t = NEXT(t) )
9019: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9020: }
9021: if ( MaxDeg > 0 ) nocheck = 1;
9022: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9023: if ( ret )
9024: cand = nd_f4_lf_trace_main(m,&perm);
9025: if ( !ret || !cand ) {
9026: *rp = 0; return;
9027: }
9028: if ( !ishomo && homo ) {
9029: /* dehomogenization */
9030: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9031: nd_init_ord(ord);
9032: nd_setup_parameters(nvar,0);
9033: }
9034: cand = ndv_reducebase(cand,perm);
9035: cand = ndv_reduceall(-2,cand);
9036: cbpe = nd_bpe;
9037: get_eg(&eg0);
9038: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9039: /* gbcheck : cand is a GB of Id(cand) ? */
9040: retn = nd_f4(-2,0,0);
9041: }
9042: if ( !retn ) {
9043: /* failure */
9044: *rp = 0; return;
9045: }
9046: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9047: if ( DP_Print )
9048: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime+eg_check.gctime);
9049: /* dp->p */
9050: nd_bpe = cbpe;
9051: nd_setup_parameters(nd_nvar,0);
9052: for ( r = cand; r; r = NEXT(r) ) {
9053: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9054: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9055: }
9056: MKLIST(*rp,cand);
9057: }
9058:
9059: NODE nd_f4_lf_trace_main(int m,int **indp)
9060: {
9061: int i,nh,stat,index;
9062: NODE r,rm,g;
9063: ND_pairs d,l,l0,t;
9064: ND spol,red;
9065: NDV nf,redv,nfqv,nfv;
9066: NM s0,s;
9067: NODE rp0,srp0,nflist,nflist_lf;
9068: int nsp,nred,col,rank,len,k,j,a;
9069: UINT c;
9070: UINT **spmat;
9071: UINT *s0vect,*svect,*p,*v;
9072: int *colstat;
9073: IndArray *imat;
9074: int *rhead;
9075: int spcol,sprow;
9076: int sugar;
9077: PGeoBucket bucket;
9078: struct oEGT eg0,eg1,eg_f4;
9079:
9080: g = 0; d = 0;
9081: for ( i = 0; i < nd_psn; i++ ) {
9082: d = update_pairs(d,g,i,0);
9083: g = update_base(g,i);
9084: }
9085: while ( d ) {
9086: get_eg(&eg0);
9087: l = nd_minsugarp(d,&d);
9088: sugar = SG(l);
9089: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9090: bucket = create_pbucket();
9091: stat = nd_sp_f4(m,0,l,bucket);
9092: if ( !stat ) {
9093: for ( t = l; NEXT(t); t = NEXT(t) );
9094: NEXT(t) = d; d = l;
9095: d = nd_reconstruct(1,d);
9096: continue;
9097: }
9098: if ( bucket->m < 0 ) continue;
9099: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9100: if ( !col ) {
9101: for ( t = l; NEXT(t); t = NEXT(t) );
9102: NEXT(t) = d; d = l;
9103: d = nd_reconstruct(1,d);
9104: continue;
9105: }
9106: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9107: if ( DP_Print )
1.3 ! noro 9108: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.1 noro 9109: sugar,eg_f4.exectime+eg_f4.gctime);
9110: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9111: if ( !l0 ) continue;
9112: l = l0;
9113:
9114: /* over LF */
9115: bucket = create_pbucket();
9116: stat = nd_sp_f4(-2,1,l,bucket);
9117: if ( !stat ) {
9118: for ( t = l; NEXT(t); t = NEXT(t) );
9119: NEXT(t) = d; d = l;
9120: d = nd_reconstruct(1,d);
9121: continue;
9122: }
9123: if ( bucket->m < 0 ) continue;
9124: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9125: if ( !col ) {
9126: for ( t = l; NEXT(t); t = NEXT(t) );
9127: NEXT(t) = d; d = l;
9128: d = nd_reconstruct(1,d);
9129: continue;
9130: }
9131: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9132: /* adding new bases */
9133: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9134: nfv = (NDV)BDY(rm);
9135: nfqv = (NDV)BDY(r);
9136: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9137: ndv_removecont(m,nfv);
9138: ndv_removecont(-2,nfqv);
9139: nh = ndv_newps(-2,nfv,nfqv,1);
9140: d = update_pairs(d,g,nh,0);
9141: g = update_base(g,nh);
9142: }
9143: if ( r || rm ) return 0;
9144: }
9145: conv_ilist(nd_demand,1,g,indp);
9146: return g;
9147: }
9148:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>