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