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