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