Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.42
1.42 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.41 2020/11/26 03:55:23 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.41 noro 5: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs;
1.12 noro 6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 7:
8: int diag_period = 6;
9: int weight_check = 1;
10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 11: /* for general module order */
1.19 noro 12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: int (*dl_base_compare_function)(int nv,DL a,DL b);
14: int nd_base_ordtype;
1.1 noro 15: int nd_dcomp;
16: int nd_rref2;
17: NM _nm_free_list;
18: ND _nd_free_list;
19: ND_pairs _ndp_free_list;
20: NODE nd_hcf;
1.35 noro 21: int Nsyz,Nsamesig;
1.1 noro 22:
23: Obj nd_top_weight;
24:
25: static NODE nd_subst;
26: static VL nd_vc;
27: static int nd_ntrans;
28: static int nd_nalg;
29: #if 0
30: static int ndv_alloc;
31: #endif
32: #if 1
33: static int nd_f4_nsp=0x7fffffff;
34: #else
35: static int nd_f4_nsp=50;
36: #endif
37: static double nd_scale=2;
38: static UINT **nd_bound;
39: static struct order_spec *nd_ord;
40: static EPOS nd_epos;
41: static BlockMask nd_blockmask;
42: static int nd_nvar;
43: static int nd_isrlex;
44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
45: static UINT nd_mask[32];
46: static UINT nd_mask0,nd_mask1;
47:
48: static NDV *nd_ps;
49: static NDV *nd_ps_trace;
50: static NDV *nd_ps_sym;
51: static NDV *nd_ps_trace_sym;
52: static RHist *nd_psh;
1.30 noro 53: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 54: static RHist *nd_red;
55: static int *nd_work_vector;
56: static int **nd_matrix;
57: static int nd_matrix_len;
58: static struct weight_or_block *nd_worb;
59: static int nd_worb_len;
60: static int nd_found,nd_create,nd_notfirst;
61: static int nmv_adv;
62: static int nd_demand;
1.21 noro 63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 64: static int nd_module_rank,nd_poly_weight_len;
65: static int *nd_poly_weight,*nd_module_weight;
66: static NODE nd_tracelist;
67: static NODE nd_alltracelist;
1.41 noro 68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
69: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.39 noro 70: static int nd_top;
1.1 noro 71: static int *nd_gbblock;
72: static NODE nd_nzlist,nd_check_splist;
73: static int nd_splist;
74: static int *nd_sugarweight;
75: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 76: static DL *nd_sba_hm;
1.29 noro 77: static NODE *nd_sba_pos;
1.1 noro 78:
79: NumberField get_numberfield();
80: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
81: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
82: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
83: int nd_monic(int m,ND *p);
84: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
85: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
86: NDV pltondv(VL vl,VL dvl,LIST p);
87: void pltozpl(LIST l,Q *cont,LIST *pp);
88: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
89: void nmtodp(int mod,NM m,DP *r);
1.15 noro 90: void ndltodp(UINT *d,DP *r);
1.1 noro 91: NODE reverse_node(NODE n);
92: P ndc_div(int mod,union oNDC a,union oNDC b);
93: P ndctop(int mod,union oNDC c);
94: void finalize_tracelist(int i,P cont);
95: void conv_ilist(int demand,int trace,NODE g,int **indp);
96: void parse_nd_option(NODE opt);
97: void dltondl(int n,DL dl,UINT *r);
98: DP ndvtodp(int mod,NDV p);
99: DP ndtodp(int mod,ND p);
1.16 noro 100: DPM ndvtodpm(int mod,NDV p);
101: NDV dpmtondv(int mod,DPM p);
102: int dpm_getdeg(DPM p,int *rank);
103: void dpm_ptozp(DPM p,Z *cont,DPM *r);
104: int compdmm(int nv,DMM a,DMM b);
1.1 noro 105:
106: void Pdp_set_weight(NODE,VECT *);
107: void Pox_cmo_rpc(NODE,Obj *);
108:
109: ND nd_add_lf(ND p1,ND p2);
110: void nd_mul_c_lf(ND p,Z mul);
111: void ndv_mul_c_lf(NDV p,Z mul);
112: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
113: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
114: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
115: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
116: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
117: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
118: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 119: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig);
1.1 noro 120: NODE nd_f4_lf_trace_main(int m,int **indp);
121: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
122:
123: extern int lf_lazy;
124: extern Z current_mod_lf;
125:
126: extern int Denominator,DP_Multiple,MaxDeg;
127:
128: #define BLEN (8*sizeof(unsigned long))
129:
130: typedef struct matrix {
131: int row,col;
132: unsigned long **a;
133: } *matrix;
134:
135:
136: void nd_free_private_storage()
137: {
138: _nm_free_list = 0;
139: _ndp_free_list = 0;
140: #if 0
141: GC_gcollect();
142: #endif
143: }
144:
145: void _NM_alloc()
146: {
147: NM p;
148: int i;
149:
150: for ( i = 0; i < 1024; i++ ) {
151: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
152: p->next = _nm_free_list; _nm_free_list = p;
153: }
154: }
155:
156: matrix alloc_matrix(int row,int col)
157: {
158: unsigned long **a;
159: int i,len,blen;
160: matrix mat;
161:
162: mat = (matrix)MALLOC(sizeof(struct matrix));
163: mat->row = row;
164: mat->col = col;
165: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
166: return mat;
167: }
168:
169:
170: void _ND_alloc()
171: {
172: ND p;
173: int i;
174:
175: for ( i = 0; i < 1024; i++ ) {
176: p = (ND)MALLOC(sizeof(struct oND));
177: p->body = (NM)_nd_free_list; _nd_free_list = p;
178: }
179: }
180:
181: void _NDP_alloc()
182: {
183: ND_pairs p;
184: int i;
185:
186: for ( i = 0; i < 1024; i++ ) {
187: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
188: +(nd_wpd-1)*sizeof(UINT));
189: p->next = _ndp_free_list; _ndp_free_list = p;
190: }
191: }
192:
193: INLINE int nd_length(ND p)
194: {
195: NM m;
196: int i;
197:
198: if ( !p )
199: return 0;
200: else {
201: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
202: return i;
203: }
204: }
205:
206: extern int dp_negative_weight;
207:
208: INLINE int ndl_reducible(UINT *d1,UINT *d2)
209: {
210: UINT u1,u2;
211: int i,j;
212:
213: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
214:
215: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
216: #if USE_UNROLL
217: switch ( nd_bpe ) {
218: case 3:
219: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
220: u1 = d1[i]; u2 = d2[i];
221: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
222: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
223: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
224: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
225: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
226: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
227: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
228: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
229: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
230: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
231: }
232: return 1;
233: break;
234: case 4:
235: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
236: u1 = d1[i]; u2 = d2[i];
237: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
238: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
239: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
240: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
241: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
242: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
243: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
244: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
245: }
246: return 1;
247: break;
248: case 6:
249: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
250: u1 = d1[i]; u2 = d2[i];
251: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
252: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
253: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
254: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
255: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
256: }
257: return 1;
258: break;
259: case 8:
260: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
261: u1 = d1[i]; u2 = d2[i];
262: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
263: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
264: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
265: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
266: }
267: return 1;
268: break;
269: case 16:
270: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
271: u1 = d1[i]; u2 = d2[i];
272: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
273: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
274: }
275: return 1;
276: break;
277: case 32:
278: for ( i = nd_exporigin; i < nd_wpd; i++ )
279: if ( d1[i] < d2[i] ) return 0;
280: return 1;
281: break;
282: default:
283: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
284: u1 = d1[i]; u2 = d2[i];
285: for ( j = 0; j < nd_epw; j++ )
286: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
287: }
288: return 1;
289: }
290: #else
291: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
292: u1 = d1[i]; u2 = d2[i];
293: for ( j = 0; j < nd_epw; j++ )
294: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
295: }
296: return 1;
297: #endif
298: }
299:
300: /*
301: * If the current order is a block order,
302: * then the last block is length 1 and contains
303: * the homo variable. Otherwise, the original
304: * order is either 0 or 2.
305: */
306:
307: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
308: {
309: int w,i,e,n,omask0;
310:
311: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
312: n = nd_nvar-1;
313: ndl_zero(r);
314: for ( i = 0; i < n; i++ ) {
315: e = GET_EXP_OLD(d,i);
316: PUT_EXP(r,i,e);
317: }
318: w = TD(d);
319: PUT_EXP(r,nd_nvar-1,weight-w);
320: if ( nd_module ) MPOS(r) = d[ompos];
321: TD(r) = weight;
322: if ( nd_blockmask ) ndl_weight_mask(r);
323: }
324:
325: void ndl_dehomogenize(UINT *d)
326: {
327: UINT mask;
328: UINT h;
329: int i,bits;
330:
331: if ( nd_blockmask ) {
332: h = GET_EXP(d,nd_nvar-1);
333: XOR_EXP(d,nd_nvar-1,h);
334: TD(d) -= h;
335: ndl_weight_mask(d);
336: } else {
337: if ( nd_isrlex ) {
338: if ( nd_bpe == 32 ) {
339: h = d[nd_exporigin];
340: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
341: d[i-1] = d[i];
342: d[i-1] = 0;
343: TD(d) -= h;
344: } else {
345: bits = nd_epw*nd_bpe;
346: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
347: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
348: for ( i = nd_exporigin; i < nd_wpd; i++ )
349: d[i] = ((d[i]<<nd_bpe)&mask)
350: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
351: TD(d) -= h;
352: }
353: } else {
354: h = GET_EXP(d,nd_nvar-1);
355: XOR_EXP(d,nd_nvar-1,h);
356: TD(d) -= h;
357: }
358: }
359: }
360:
361: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
362: {
363: UINT t1,t2,u,u1,u2;
364: int i,j,l;
365:
366: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
367: error("ndl_lcm : inconsistent monomials");
368: #if USE_UNROLL
369: switch ( nd_bpe ) {
370: case 3:
371: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
372: u1 = d1[i]; u2 = d2[i];
373: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
374: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
375: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
376: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
377: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
378: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
379: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
380: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
381: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
382: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
383: d[i] = u;
384: }
385: break;
386: case 4:
387: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
388: u1 = d1[i]; u2 = d2[i];
389: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
390: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
391: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
392: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
393: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
394: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
395: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
396: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
397: d[i] = u;
398: }
399: break;
400: case 6:
401: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
402: u1 = d1[i]; u2 = d2[i];
403: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
404: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
405: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
406: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
407: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
408: d[i] = u;
409: }
410: break;
411: case 8:
412: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
413: u1 = d1[i]; u2 = d2[i];
414: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
415: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
416: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
417: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
418: d[i] = u;
419: }
420: break;
421: case 16:
422: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
423: u1 = d1[i]; u2 = d2[i];
424: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
425: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
426: d[i] = u;
427: }
428: break;
429: case 32:
430: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
431: u1 = d1[i]; u2 = d2[i];
432: d[i] = u1>u2?u1:u2;
433: }
434: break;
435: default:
436: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
437: u1 = d1[i]; u2 = d2[i];
438: for ( j = 0, u = 0; j < nd_epw; j++ ) {
439: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
440: }
441: d[i] = u;
442: }
443: break;
444: }
445: #else
446: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
447: u1 = d1[i]; u2 = d2[i];
448: for ( j = 0, u = 0; j < nd_epw; j++ ) {
449: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
450: }
451: d[i] = u;
452: }
453: #endif
454: if ( nd_module ) MPOS(d) = MPOS(d1);
455: TD(d) = ndl_weight(d);
456: if ( nd_blockmask ) ndl_weight_mask(d);
457: }
458:
459: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
460: {
461: UINT t1,t2,u,u1,u2;
462: int i,j,l;
463:
464: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
465: u1 = d1[i]; u2 = d2[i];
466: for ( j = 0, u = 0; j < nd_epw; j++ ) {
467: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
468: }
469: d[i] = u;
470: }
471: }
472:
473: int ndl_weight(UINT *d)
474: {
475: UINT t,u;
476: int i,j;
477:
478: if ( current_dl_weight_vector )
479: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
480: u = GET_EXP(d,i);
481: t += MUL_WEIGHT(u,i);
482: }
483: else
484: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
485: u = d[i];
486: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
487: t += (u&nd_mask0);
488: }
1.20 noro 489: if ( nd_module && nd_module_rank && MPOS(d) )
490: t += nd_module_weight[MPOS(d)-1];
491: for ( i = nd_exporigin; i < nd_wpd; i++ )
492: if ( d[i] && !t )
493: printf("afo\n");
1.1 noro 494: return t;
495: }
496:
497: /* for sugarweight */
498:
499: int ndl_weight2(UINT *d)
500: {
501: int t,u;
502: int i,j;
503:
504: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
505: u = GET_EXP(d,i);
506: t += nd_sugarweight[i]*u;
507: }
1.20 noro 508: if ( nd_module && nd_module_rank && MPOS(d) )
509: t += nd_module_weight[MPOS(d)-1];
1.1 noro 510: return t;
511: }
512:
513: void ndl_weight_mask(UINT *d)
514: {
515: UINT t,u;
516: UINT *mask;
517: int i,j,k,l;
518:
519: l = nd_blockmask->n;
520: for ( k = 0; k < l; k++ ) {
521: mask = nd_blockmask->mask[k];
522: if ( current_dl_weight_vector )
523: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
524: u = GET_EXP_MASK(d,i,mask);
525: t += MUL_WEIGHT(u,i);
526: }
527: else
528: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
529: u = d[i]&mask[i];
530: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
531: t += (u&nd_mask0);
532: }
533: d[k+1] = t;
534: }
535: }
536:
1.21 noro 537: int ndl_glex_compare(UINT *d1,UINT *d2)
538: {
539: if ( TD(d1) > TD(d2) ) return 1;
540: else if ( TD(d1) < TD(d2) ) return -1;
541: else return ndl_lex_compare(d1,d2);
542: }
543:
1.1 noro 544: int ndl_lex_compare(UINT *d1,UINT *d2)
545: {
546: int i;
547:
548: d1 += nd_exporigin;
549: d2 += nd_exporigin;
550: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
551: if ( *d1 > *d2 )
552: return nd_isrlex ? -1 : 1;
553: else if ( *d1 < *d2 )
554: return nd_isrlex ? 1 : -1;
555: return 0;
556: }
557:
558: int ndl_block_compare(UINT *d1,UINT *d2)
559: {
560: int i,l,j,ord_o,ord_l;
561: struct order_pair *op;
562: UINT t1,t2,m;
563: UINT *mask;
564:
565: l = nd_blockmask->n;
566: op = nd_blockmask->order_pair;
567: for ( j = 0; j < l; j++ ) {
568: mask = nd_blockmask->mask[j];
569: ord_o = op[j].order;
570: if ( ord_o < 2 ) {
571: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
572: else if ( t1 < t2 ) return -1;
573: }
574: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
575: m = mask[i];
576: t1 = d1[i]&m;
577: t2 = d2[i]&m;
578: if ( t1 > t2 )
579: return !ord_o ? -1 : 1;
580: else if ( t1 < t2 )
581: return !ord_o ? 1 : -1;
582: }
583: }
584: return 0;
585: }
586:
587: int ndl_matrix_compare(UINT *d1,UINT *d2)
588: {
589: int i,j,s,row;
590: int *v;
591: Z **mat;
592: Z *w;
593: Z t1;
594: Z t,t2;
595:
1.6 noro 596: for ( j = 0; j < nd_nvar; j++ )
597: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 598: if ( nd_top_weight ) {
599: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 600: mat = (Z **)&BDY((VECT)nd_top_weight);
601: row = 1;
1.1 noro 602: } else {
603: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 604: row = ((MAT)nd_top_weight)->row;
1.1 noro 605: }
606: for ( i = 0; i < row; i++ ) {
1.6 noro 607: w = mat[i];
1.1 noro 608: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 609: STOZ(nd_work_vector[j],t1);
1.1 noro 610: mulz(w[j],t1,&t2);
611: addz(t,t2,&t1);
612: t = t1;
613: }
1.6 noro 614: if ( t ) {
615: s = sgnz(t);
1.1 noro 616: if ( s > 0 ) return 1;
617: else if ( s < 0 ) return -1;
1.6 noro 618: }
1.1 noro 619: }
1.6 noro 620: }
621: for ( i = 0; i < nd_matrix_len; i++ ) {
622: v = nd_matrix[i];
623: for ( j = 0, s = 0; j < nd_nvar; j++ )
624: s += v[j]*nd_work_vector[j];
625: if ( s > 0 ) return 1;
626: else if ( s < 0 ) return -1;
627: }
1.1 noro 628: if ( !ndl_equal(d1,d2) )
1.6 noro 629: error("ndl_matrix_compare : invalid matrix");
630: return 0;
1.1 noro 631: }
632:
633: int ndl_composite_compare(UINT *d1,UINT *d2)
634: {
635: int i,j,s,start,end,len,o;
636: int *v;
637: struct sparse_weight *sw;
638:
639: for ( j = 0; j < nd_nvar; j++ )
640: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
641: for ( i = 0; i < nd_worb_len; i++ ) {
642: len = nd_worb[i].length;
643: switch ( nd_worb[i].type ) {
644: case IS_DENSE_WEIGHT:
645: v = nd_worb[i].body.dense_weight;
646: for ( j = 0, s = 0; j < len; j++ )
647: s += v[j]*nd_work_vector[j];
648: if ( s > 0 ) return 1;
649: else if ( s < 0 ) return -1;
650: break;
651: case IS_SPARSE_WEIGHT:
652: sw = nd_worb[i].body.sparse_weight;
653: for ( j = 0, s = 0; j < len; j++ )
654: s += sw[j].value*nd_work_vector[sw[j].pos];
655: if ( s > 0 ) return 1;
656: else if ( s < 0 ) return -1;
657: break;
658: case IS_BLOCK:
659: o = nd_worb[i].body.block.order;
660: start = nd_worb[i].body.block.start;
661: switch ( o ) {
662: case 0:
663: end = start+len;
664: for ( j = start, s = 0; j < end; j++ )
665: s += MUL_WEIGHT(nd_work_vector[j],j);
666: if ( s > 0 ) return 1;
667: else if ( s < 0 ) return -1;
668: for ( j = end-1; j >= start; j-- )
669: if ( nd_work_vector[j] < 0 ) return 1;
670: else if ( nd_work_vector[j] > 0 ) return -1;
671: break;
672: case 1:
673: end = start+len;
674: for ( j = start, s = 0; j < end; j++ )
675: s += MUL_WEIGHT(nd_work_vector[j],j);
676: if ( s > 0 ) return 1;
677: else if ( s < 0 ) return -1;
678: for ( j = start; j < end; j++ )
679: if ( nd_work_vector[j] > 0 ) return 1;
680: else if ( nd_work_vector[j] < 0 ) return -1;
681: break;
682: case 2:
683: end = start+len;
684: for ( j = start; j < end; j++ )
685: if ( nd_work_vector[j] > 0 ) return 1;
686: else if ( nd_work_vector[j] < 0 ) return -1;
687: break;
688: }
689: break;
690: }
691: }
692: return 0;
693: }
694:
695: /* TDH -> WW -> TD-> RL */
696:
697: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
698: {
699: int i,m,e1,e2;
700:
701: if ( TD(d1) > TD(d2) ) return 1;
702: else if ( TD(d1) < TD(d2) ) return -1;
703: m = nd_nvar>>1;
704: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
705: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
706: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
707: }
708: if ( e1 > e2 ) return 1;
709: else if ( e1 < e2 ) return -1;
710: return ndl_lex_compare(d1,d2);
711: }
712:
1.21 noro 713: // common function for module glex and grlex comparison
714: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 715: {
1.21 noro 716: int c;
1.1 noro 717:
1.21 noro 718: switch ( nd_module_ordtype ) {
719: case 0:
720: if ( TD(d1) > TD(d2) ) return 1;
721: else if ( TD(d1) < TD(d2) ) return -1;
722: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
723: else if ( MPOS(d1) < MPOS(d2) ) return 1;
724: else if ( MPOS(d1) > MPOS(d2) ) return -1;
725: else return 0;
726: break;
1.1 noro 727:
1.21 noro 728: case 1:
1.19 noro 729: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
730: if ( TD(d1) > TD(d2) ) return 1;
731: else if ( TD(d1) < TD(d2) ) return -1;
732: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
733: if ( MPOS(d1) < MPOS(d2) ) return 1;
734: else if ( MPOS(d1) > MPOS(d2) ) return -1;
735: }
736: if ( MPOS(d1) < MPOS(d2) ) return 1;
737: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 738: else if ( TD(d1) > TD(d2) ) return 1;
739: else if ( TD(d1) < TD(d2) ) return -1;
740: else return ndl_lex_compare(d1,d2);
741: break;
1.1 noro 742:
1.21 noro 743: case 2: // weight -> POT
744: if ( TD(d1) > TD(d2) ) return 1;
745: else if ( TD(d1) < TD(d2) ) return -1;
746: else if ( MPOS(d1) < MPOS(d2) ) return 1;
747: else if ( MPOS(d1) > MPOS(d2) ) return -1;
748: else return ndl_lex_compare(d1,d2);
749: break;
1.1 noro 750:
1.21 noro 751: default:
752: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 753: return 0;
1.21 noro 754: }
1.1 noro 755: }
756:
1.21 noro 757: // common for module comparison
758: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 759: {
1.21 noro 760: int c;
1.1 noro 761:
1.21 noro 762: switch ( nd_module_ordtype ) {
763: case 0:
1.23 noro 764: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 765: else if ( MPOS(d1) > MPOS(d2) ) return -1;
766: else if ( MPOS(d1) < MPOS(d2) ) return 1;
767: else return 0;
768: break;
1.1 noro 769:
1.21 noro 770: case 1:
771: if ( MPOS(d1) < MPOS(d2) ) return 1;
772: else if ( MPOS(d1) > MPOS(d2) ) return -1;
773: else return (*ndl_base_compare_function)(d1,d2);
774: break;
1.1 noro 775:
1.21 noro 776: case 2: // weight -> POT
777: if ( TD(d1) > TD(d2) ) return 1;
778: else if ( TD(d1) < TD(d2) ) return -1;
779: else if ( MPOS(d1) < MPOS(d2) ) return 1;
780: else if ( MPOS(d1) > MPOS(d2) ) return -1;
781: else return (*ndl_base_compare_function)(d1,d2);
782: break;
1.1 noro 783:
1.21 noro 784: default:
785: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 786: return 0;
1.21 noro 787: }
1.1 noro 788: }
789:
1.21 noro 790: extern DMMstack dmm_stack;
791: void _addtodl(int n,DL d1,DL d2);
1.31 noro 792: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 793: int _eqdl(int n,DL d1,DL d2);
794:
795: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
796: {
797: int pos1,pos2,t,j;
798: DMM *in;
799: DMMstack s;
800: static DL d1=0;
801: static DL d2=0;
802: static int dlen=0;
803:
804: pos1 = MPOS(m1); pos2 = MPOS(m2);
805: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
806: if ( nd_nvar > dlen ) {
807: NEWDL(d1,nd_nvar);
808: NEWDL(d2,nd_nvar);
809: dlen = nd_nvar;
810: }
811: d1->td = TD(m1);
812: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
813: d2->td = TD(m2);
814: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
815: for ( s = dmm_stack; s; s = NEXT(s) ) {
816: in = s->in;
817: _addtodl(nd_nvar,in[pos1]->dl,d1);
818: _addtodl(nd_nvar,in[pos2]->dl,d2);
819: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
820: if ( pos1 < pos2 ) return 1;
821: else if ( pos1 > pos2 ) return -1;
822: else return 0;
823: }
824: pos1 = in[pos1]->pos;
825: pos2 = in[pos2]->pos;
826: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
827: }
828: // comparison by the bottom order
829: LAST:
830: switch ( nd_base_ordtype ) {
831: case 0:
832: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
833: if ( t ) return t;
834: else if ( pos1 < pos2 ) return 1;
835: else if ( pos1 > pos2 ) return -1;
836: else return 0;
837: break;
838: case 1:
839: if ( pos1 < pos2 ) return 1;
840: else if ( pos1 > pos2 ) return -1;
841: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
842: break;
843: case 2:
844: if ( d1->td > d2->td ) return 1;
845: else if ( d1->td < d2->td ) return -1;
846: else if ( pos1 < pos2 ) return 1;
847: else if ( pos1 > pos2 ) return -1;
848: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
849: break;
850: default:
851: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 852: return 0;
1.21 noro 853: }
1.1 noro 854: }
855:
856: INLINE int ndl_equal(UINT *d1,UINT *d2)
857: {
858: int i;
859:
860: switch ( nd_wpd ) {
861: case 2:
862: if ( TD(d2) != TD(d1) ) return 0;
863: if ( d2[1] != d1[1] ) return 0;
864: return 1;
865: break;
866: case 3:
867: if ( TD(d2) != TD(d1) ) return 0;
868: if ( d2[1] != d1[1] ) return 0;
869: if ( d2[2] != d1[2] ) return 0;
870: return 1;
871: break;
872: default:
873: for ( i = 0; i < nd_wpd; i++ )
874: if ( *d1++ != *d2++ ) return 0;
875: return 1;
876: break;
877: }
878: }
879:
880: INLINE void ndl_copy(UINT *d1,UINT *d2)
881: {
882: int i;
883:
884: switch ( nd_wpd ) {
885: case 2:
886: TD(d2) = TD(d1);
887: d2[1] = d1[1];
888: break;
889: case 3:
890: TD(d2) = TD(d1);
891: d2[1] = d1[1];
892: d2[2] = d1[2];
893: break;
894: default:
895: for ( i = 0; i < nd_wpd; i++ )
896: d2[i] = d1[i];
897: break;
898: }
899: }
900:
901: INLINE void ndl_zero(UINT *d)
902: {
903: int i;
904: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
905: }
906:
907: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
908: {
909: int i;
910:
911: if ( nd_module ) {
912: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
913: error("ndl_add : invalid operation");
914: }
915: #if 1
916: switch ( nd_wpd ) {
917: case 2:
918: TD(d) = TD(d1)+TD(d2);
919: d[1] = d1[1]+d2[1];
920: break;
921: case 3:
922: TD(d) = TD(d1)+TD(d2);
923: d[1] = d1[1]+d2[1];
924: d[2] = d1[2]+d2[2];
925: break;
926: default:
927: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
928: break;
929: }
930: #else
931: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
932: #endif
933: }
934:
935: /* d1 += d2 */
936: INLINE void ndl_addto(UINT *d1,UINT *d2)
937: {
938: int i;
939:
940: if ( nd_module ) {
941: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
942: error("ndl_addto : invalid operation");
943: }
944: #if 1
945: switch ( nd_wpd ) {
946: case 2:
947: TD(d1) += TD(d2);
948: d1[1] += d2[1];
949: break;
950: case 3:
951: TD(d1) += TD(d2);
952: d1[1] += d2[1];
953: d1[2] += d2[2];
954: break;
955: default:
956: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
957: break;
958: }
959: #else
960: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
961: #endif
962: }
963:
964: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
965: {
966: int i;
967:
968: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
969: }
970:
971: int ndl_disjoint(UINT *d1,UINT *d2)
972: {
973: UINT t1,t2,u,u1,u2;
974: int i,j;
975:
976: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
977: #if USE_UNROLL
978: switch ( nd_bpe ) {
979: case 3:
980: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
981: u1 = d1[i]; u2 = d2[i];
982: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
983: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
984: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
985: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
986: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
987: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
988: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
989: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
990: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
991: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
992: }
993: return 1;
994: break;
995: case 4:
996: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
997: u1 = d1[i]; u2 = d2[i];
998: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
999: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1000: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1001: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1002: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1003: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1004: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1005: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1006: }
1007: return 1;
1008: break;
1009: case 6:
1010: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1011: u1 = d1[i]; u2 = d2[i];
1012: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1015: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1017: }
1018: return 1;
1019: break;
1020: case 8:
1021: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1022: u1 = d1[i]; u2 = d2[i];
1023: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1027: }
1028: return 1;
1029: break;
1030: case 16:
1031: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1032: u1 = d1[i]; u2 = d2[i];
1033: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1034: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1035: }
1036: return 1;
1037: break;
1038: case 32:
1039: for ( i = nd_exporigin; i < nd_wpd; i++ )
1040: if ( d1[i] && d2[i] ) return 0;
1041: return 1;
1042: break;
1043: default:
1044: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1045: u1 = d1[i]; u2 = d2[i];
1046: for ( j = 0; j < nd_epw; j++ ) {
1047: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1048: u1 >>= nd_bpe; u2 >>= nd_bpe;
1049: }
1050: }
1051: return 1;
1052: break;
1053: }
1054: #else
1055: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1056: u1 = d1[i]; u2 = d2[i];
1057: for ( j = 0; j < nd_epw; j++ ) {
1058: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1059: u1 >>= nd_bpe; u2 >>= nd_bpe;
1060: }
1061: }
1062: return 1;
1063: #endif
1064: }
1065:
1066: int ndl_check_bound(UINT *d1,UINT *d2)
1067: {
1068: UINT u2;
1069: int i,j,ind,k;
1070:
1071: ind = 0;
1072: #if USE_UNROLL
1073: switch ( nd_bpe ) {
1074: case 3:
1075: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1076: u2 = d2[i];
1077: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1078: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1079: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1080: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1081: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1082: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1083: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1084: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1085: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1086: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1087: }
1088: return 0;
1089: break;
1090: case 4:
1091: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1092: u2 = d2[i];
1093: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1094: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1095: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1096: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1097: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1098: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1099: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1100: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1101: }
1102: return 0;
1103: break;
1104: case 6:
1105: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1106: u2 = d2[i];
1107: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1108: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1109: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1110: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1111: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1112: }
1113: return 0;
1114: break;
1115: case 8:
1116: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1117: u2 = d2[i];
1118: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1119: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1120: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1121: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1122: }
1123: return 0;
1124: break;
1125: case 16:
1126: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1127: u2 = d2[i];
1128: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1129: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1130: }
1131: return 0;
1132: break;
1133: case 32:
1134: for ( i = nd_exporigin; i < nd_wpd; i++ )
1135: if ( d1[i]+d2[i]<d1[i] ) return 1;
1136: return 0;
1137: break;
1138: default:
1139: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1140: u2 = d2[i];
1141: k = (nd_epw-1)*nd_bpe;
1142: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1143: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1144: }
1145: return 0;
1146: break;
1147: }
1148: #else
1149: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1150: u2 = d2[i];
1151: k = (nd_epw-1)*nd_bpe;
1152: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1153: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1154: }
1155: return 0;
1156: #endif
1157: }
1158:
1159: int ndl_check_bound2(int index,UINT *d2)
1160: {
1161: return ndl_check_bound(nd_bound[index],d2);
1162: }
1163:
1164: INLINE int ndl_hash_value(UINT *d)
1165: {
1166: int i;
1.11 noro 1167: UINT r;
1.1 noro 1168:
1169: r = 0;
1170: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1171: r = (r*1511+d[i]);
1.11 noro 1172: r %= REDTAB_LEN;
1.1 noro 1173: return r;
1174: }
1175:
1176: INLINE int ndl_find_reducer(UINT *dg)
1177: {
1178: RHist r;
1179: int d,k,i;
1180:
1181: d = ndl_hash_value(dg);
1182: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1183: if ( ndl_equal(dg,DL(r)) ) {
1184: if ( k > 0 ) nd_notfirst++;
1185: nd_found++;
1186: return r->index;
1187: }
1188: }
1189: if ( Reverse )
1190: for ( i = nd_psn-1; i >= 0; i-- ) {
1191: r = nd_psh[i];
1192: if ( ndl_reducible(dg,DL(r)) ) {
1193: nd_create++;
1194: nd_append_red(dg,i);
1195: return i;
1196: }
1197: }
1198: else
1199: for ( i = 0; i < nd_psn; i++ ) {
1200: r = nd_psh[i];
1201: if ( ndl_reducible(dg,DL(r)) ) {
1202: nd_create++;
1203: nd_append_red(dg,i);
1204: return i;
1205: }
1206: }
1207: return -1;
1208: }
1209:
1.41 noro 1210: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1211: {
1212: RHist r;
1213: int i;
1214:
1215: for ( i = 0; i < nd_psn; i++ ) {
1216: r = nd_psh[i];
1217: if ( ndl_reducible(dg,DL(r)) ) return i;
1218: }
1219: return -1;
1220: }
1221:
1.24 noro 1222: // ret=0,...,nd_psn-1 => reducer found
1223: // ret=nd_psn => reducer not found
1224: // ret=-1 => singular top reducible
1225:
1226: int comp_sig(SIG s1,SIG s2);
1227: void _ndltodl(UINT *ndl,DL dl);
1228:
1229: void print_sig(SIG s)
1230: {
1231: int i;
1232:
1233: fprintf(asir_out,"<<");
1234: for ( i = 0; i < nd_nvar; i++ ) {
1235: fprintf(asir_out,"%d",s->dl->d[i]);
1236: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1237: }
1238: fprintf(asir_out,">>*e%d",s->pos);
1239: }
1240:
1.35 noro 1241: // assuming increasing order wrt signature
1242:
1.24 noro 1243: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1244: {
1245: RHist r;
1.27 noro 1246: int i,singular,ret,d,k;
1.26 noro 1247: static int wpd,nvar;
1.24 noro 1248: static SIG quo;
1249: static UINT *tmp;
1250:
1.26 noro 1251: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1252: if ( wpd != nd_wpd ) {
1.24 noro 1253: wpd = nd_wpd;
1254: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1255: }
1.27 noro 1256: d = ndl_hash_value(dg);
1.35 noro 1257: #if 1
1.27 noro 1258: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1259: if ( ndl_equal(dg,DL(r)) ) {
1260: return r->index;
1261: }
1262: }
1.35 noro 1263: #endif
1.24 noro 1264: singular = 0;
1265: for ( i = 0; i < nd_psn; i++ ) {
1266: r = nd_psh[i];
1267: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1268: ndl_sub(dg,DL(r),tmp);
1.24 noro 1269: _ndltodl(tmp,DL(quo));
1270: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1271: quo->pos = nd_psh[i]->sig->pos;
1272: ret = comp_sig(sig,quo);
1273: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1274: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1275: }
1276: }
1277: if ( singular ) return -1;
1.27 noro 1278: else if ( i < nd_psn )
1279: nd_append_red(dg,i);
1280: return i;
1.24 noro 1281: }
1282:
1.1 noro 1283: ND nd_merge(ND p1,ND p2)
1284: {
1285: int n,c;
1286: int t,can,td1,td2;
1287: ND r;
1288: NM m1,m2,mr0,mr,s;
1289:
1290: if ( !p1 ) return p2;
1291: else if ( !p2 ) return p1;
1292: else {
1293: can = 0;
1294: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1295: c = DL_COMPARE(DL(m1),DL(m2));
1296: switch ( c ) {
1297: case 0:
1298: s = m1; m1 = NEXT(m1);
1299: can++; NEXTNM2(mr0,mr,s);
1300: s = m2; m2 = NEXT(m2); FREENM(s);
1301: break;
1302: case 1:
1303: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1304: break;
1305: case -1:
1306: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1307: break;
1308: }
1309: }
1310: if ( !mr0 )
1311: if ( m1 ) mr0 = m1;
1312: else if ( m2 ) mr0 = m2;
1313: else return 0;
1314: else if ( m1 ) NEXT(mr) = m1;
1315: else if ( m2 ) NEXT(mr) = m2;
1316: else NEXT(mr) = 0;
1317: BDY(p1) = mr0;
1318: SG(p1) = MAX(SG(p1),SG(p2));
1319: LEN(p1) = LEN(p1)+LEN(p2)-can;
1320: FREEND(p2);
1321: return p1;
1322: }
1323: }
1324:
1325: ND nd_add(int mod,ND p1,ND p2)
1326: {
1327: int n,c;
1328: int t,can,td1,td2;
1329: ND r;
1330: NM m1,m2,mr0,mr,s;
1331:
1.11 noro 1332: Nnd_add++;
1.1 noro 1333: if ( !p1 ) return p2;
1334: else if ( !p2 ) return p1;
1335: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1336: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1337: else if ( !mod ) return nd_add_q(p1,p2);
1338: else {
1339: can = 0;
1340: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1341: c = DL_COMPARE(DL(m1),DL(m2));
1342: switch ( c ) {
1343: case 0:
1344: t = ((CM(m1))+(CM(m2))) - mod;
1345: if ( t < 0 ) t += mod;
1346: s = m1; m1 = NEXT(m1);
1347: if ( t ) {
1348: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1349: } else {
1350: can += 2; FREENM(s);
1351: }
1352: s = m2; m2 = NEXT(m2); FREENM(s);
1353: break;
1354: case 1:
1355: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1356: break;
1357: case -1:
1358: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1359: break;
1360: }
1361: }
1362: if ( !mr0 )
1363: if ( m1 ) mr0 = m1;
1364: else if ( m2 ) mr0 = m2;
1365: else return 0;
1366: else if ( m1 ) NEXT(mr) = m1;
1367: else if ( m2 ) NEXT(mr) = m2;
1368: else NEXT(mr) = 0;
1369: BDY(p1) = mr0;
1370: SG(p1) = MAX(SG(p1),SG(p2));
1371: LEN(p1) = LEN(p1)+LEN(p2)-can;
1372: FREEND(p2);
1373: return p1;
1374: }
1375: }
1376:
1377: /* XXX on opteron, the inlined manipulation of destructive additon of
1378: * two NM seems to make gcc optimizer get confused, so the part is
1379: * done in a function.
1380: */
1381:
1382: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1383: {
1384: NM s;
1385: P t;
1386: int can;
1387:
1388: addp(nd_vc,CP(*m1),CP(*m2),&t);
1389: s = *m1; *m1 = NEXT(*m1);
1390: if ( t ) {
1391: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1392: } else {
1393: can = 2; FREENM(s);
1394: }
1395: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1396: return can;
1397: }
1398:
1399: ND nd_add_q(ND p1,ND p2)
1400: {
1401: int n,c,can;
1402: ND r;
1403: NM m1,m2,mr0,mr,s;
1404: P t;
1405:
1406: if ( !p1 ) return p2;
1407: else if ( !p2 ) return p1;
1408: else {
1409: can = 0;
1410: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1411: c = DL_COMPARE(DL(m1),DL(m2));
1412: switch ( c ) {
1413: case 0:
1414: #if defined(__x86_64__)
1415: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1416: #else
1417: addp(nd_vc,CP(m1),CP(m2),&t);
1418: s = m1; m1 = NEXT(m1);
1419: if ( t ) {
1420: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1421: } else {
1422: can += 2; FREENM(s);
1423: }
1424: s = m2; m2 = NEXT(m2); FREENM(s);
1425: #endif
1426: break;
1427: case 1:
1428: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1429: break;
1430: case -1:
1431: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1432: break;
1433: }
1434: }
1435: if ( !mr0 )
1436: if ( m1 ) mr0 = m1;
1437: else if ( m2 ) mr0 = m2;
1438: else return 0;
1439: else if ( m1 ) NEXT(mr) = m1;
1440: else if ( m2 ) NEXT(mr) = m2;
1441: else NEXT(mr) = 0;
1442: BDY(p1) = mr0;
1443: SG(p1) = MAX(SG(p1),SG(p2));
1444: LEN(p1) = LEN(p1)+LEN(p2)-can;
1445: FREEND(p2);
1446: return p1;
1447: }
1448: }
1449:
1450: ND nd_add_sf(ND p1,ND p2)
1451: {
1452: int n,c,can;
1453: ND r;
1454: NM m1,m2,mr0,mr,s;
1455: int t;
1456:
1457: if ( !p1 ) return p2;
1458: else if ( !p2 ) return p1;
1459: else {
1460: can = 0;
1461: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1462: c = DL_COMPARE(DL(m1),DL(m2));
1463: switch ( c ) {
1464: case 0:
1465: t = _addsf(CM(m1),CM(m2));
1466: s = m1; m1 = NEXT(m1);
1467: if ( t ) {
1468: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1469: } else {
1470: can += 2; FREENM(s);
1471: }
1472: s = m2; m2 = NEXT(m2); FREENM(s);
1473: break;
1474: case 1:
1475: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1476: break;
1477: case -1:
1478: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1479: break;
1480: }
1481: }
1482: if ( !mr0 )
1483: if ( m1 ) mr0 = m1;
1484: else if ( m2 ) mr0 = m2;
1485: else return 0;
1486: else if ( m1 ) NEXT(mr) = m1;
1487: else if ( m2 ) NEXT(mr) = m2;
1488: else NEXT(mr) = 0;
1489: BDY(p1) = mr0;
1490: SG(p1) = MAX(SG(p1),SG(p2));
1491: LEN(p1) = LEN(p1)+LEN(p2)-can;
1492: FREEND(p2);
1493: return p1;
1494: }
1495: }
1496:
1497:
1498: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1499: {
1500: int c,c1,c2;
1501: Z cg,cred,gcd,tq;
1502: P cgp,credp,gcdp;
1503: Obj tr,tr1;
1504:
1505: if ( mod == -1 ) {
1506: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1507: *divp = (Obj)ONE;
1508: } else if ( mod == -2 ) {
1509: Z inv,t;
1510: divlf(ONE,HCZ(p),&inv);
1511: chsgnlf(HCZ(g),&t);
1512: mullf(inv,t,&CZ(mul));
1513: *divp = (Obj)ONE;
1514: } else if ( mod ) {
1515: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1516: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1517: *divp = (Obj)ONE;
1518: } else if ( nd_vc ) {
1519: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1520: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1521: chsgnp(cgp,&CP(mul));
1522: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1523: if ( dn ) {
1524: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1525: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1526: }
1527: *divp = (Obj)credp;
1528: } else {
1.6 noro 1529: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1530: chsgnz(cg,&CZ(mul));
1.1 noro 1531: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1532: if ( dn ) {
1533: mulz(dn->z,cred,&tq); dn->z = tq;
1534: }
1535: *divp = (Obj)cred;
1536: }
1537: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1538: }
1539:
1540: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1541: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1542: {
1543: NM m,mrd,tail;
1544: NM mul;
1545: int n,sugar,psugar,sugar0,stat,index;
1546: int c,c1,c2,dummy;
1547: RHist h;
1548: NDV p,red;
1549: Q cg,cred,gcd,tq,qq;
1550: Z iq;
1551: DP dmul;
1552: NODE node;
1553: LIST hist;
1554: double hmag;
1555: P tp,tp1;
1556: Obj tr,tr1,div;
1557: union oNDC hg;
1558: P cont;
1559:
1560: if ( !g ) {
1561: *rp = d;
1562: return 1;
1563: }
1564: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1565:
1566: sugar0 = sugar = SG(g);
1567: n = NV(g);
1568: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1569: if ( d )
1570: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1571: for ( ; g; ) {
1572: index = ndl_find_reducer(HDL(g));
1573: if ( index >= 0 ) {
1574: h = nd_psh[index];
1575: ndl_sub(HDL(g),DL(h),DL(mul));
1576: if ( ndl_check_bound2(index,DL(mul)) ) {
1577: nd_free(g); nd_free(d);
1578: return 0;
1579: }
1580: p = nd_demand ? ndv_load(index) : ps[index];
1581: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1582: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1583: if ( nd_gentrace ) {
1584: /* Trace=[div,index,mul,ONE] */
1.6 noro 1585: STOZ(index,iq);
1.1 noro 1586: nmtodp(mod,mul,&dmul);
1587: node = mknode(4,div,iq,dmul,ONE);
1588: }
1589: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1590: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1591: hg = HCU(g);
1592: nd_removecont2(d,g);
1.6 noro 1593: if ( nd_gentrace ) {
1.1 noro 1594: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1595: /* exact division */
1.1 noro 1596: cont = ndc_div(mod,hg,HCU(g));
1597: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1598: }
1599: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1600: }
1601: MKLIST(hist,node);
1602: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1603: } else if ( !full ) {
1604: *rp = g;
1605: return 1;
1606: } else {
1607: m = BDY(g);
1608: if ( NEXT(m) ) {
1609: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1610: } else {
1611: FREEND(g); g = 0;
1612: }
1613: if ( d ) {
1614: NEXT(tail)=m; tail=m; LEN(d)++;
1615: } else {
1616: MKND(n,m,1,d); tail = BDY(d);
1617: }
1618: }
1619: }
1620: if ( d ) SG(d) = sugar;
1621: *rp = d;
1622: return 1;
1623: }
1624:
1.24 noro 1625: // ret=1 => success
1626: // ret=0 => overflow
1627: // ret=-1 => singular top reducible
1628:
1629: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1630: {
1631: NM m,mrd,tail;
1632: NM mul;
1633: int n,sugar,psugar,sugar0,stat,index;
1634: int c,c1,c2,dummy;
1635: RHist h;
1636: NDV p,red;
1637: Q cg,cred,gcd,tq,qq;
1638: Z iq;
1639: DP dmul;
1640: NODE node;
1641: LIST hist;
1642: double hmag;
1643: P tp,tp1;
1644: Obj tr,tr1,div;
1645: union oNDC hg;
1646: P cont;
1647: SIG sig;
1648:
1649: if ( !g ) {
1650: *rp = d;
1651: return 1;
1652: }
1653: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1654:
1655: sugar0 = sugar = SG(g);
1656: n = NV(g);
1657: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1658: if ( d )
1659: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1660: sig = g->sig;
1661: for ( ; g; ) {
1662: index = ndl_find_reducer_s(HDL(g),sig);
1663: if ( index >= 0 && index < nd_psn ) {
1664: // reducer found
1665: h = nd_psh[index];
1666: ndl_sub(HDL(g),DL(h),DL(mul));
1667: if ( ndl_check_bound2(index,DL(mul)) ) {
1668: nd_free(g); nd_free(d);
1669: return 0;
1670: }
1671: p = ps[index];
1672: /* d+g -> div*(d+g)+mul*p */
1673: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1674: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1675: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1676: hg = HCU(g);
1677: nd_removecont2(d,g);
1678: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1679: }
1680: } else if ( index == -1 ) {
1681: // singular top reducible
1682: return -1;
1683: } else if ( !full ) {
1684: *rp = g;
1685: g->sig = sig;
1686: return 1;
1687: } else {
1688: m = BDY(g);
1689: if ( NEXT(m) ) {
1690: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1691: } else {
1692: FREEND(g); g = 0;
1693: }
1694: if ( d ) {
1695: NEXT(tail)=m; tail=m; LEN(d)++;
1696: } else {
1697: MKND(n,m,1,d); tail = BDY(d);
1698: }
1699: }
1700: }
1701: if ( d ) {
1702: SG(d) = sugar;
1703: d->sig = sig;
1704: }
1705: *rp = d;
1706: return 1;
1707: }
1708:
1.1 noro 1709: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1710: {
1711: int hindex,index;
1712: NDV p;
1713: ND u,d,red;
1714: NODE l;
1715: NM mul,m,mrd,tail;
1716: int sugar,psugar,n,h_reducible;
1717: PGeoBucket bucket;
1718: int c,c1,c2;
1719: Z cg,cred,gcd,zzz;
1720: RHist h;
1721: double hmag,gmag;
1722: int count = 0;
1723: int hcount = 0;
1724:
1725: if ( !g ) {
1726: *rp = 0;
1727: return 1;
1728: }
1729: sugar = SG(g);
1730: n = NV(g);
1.6 noro 1731: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1732: bucket = create_pbucket();
1733: add_pbucket(mod,bucket,g);
1734: d = 0;
1735: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1736: while ( 1 ) {
1737: if ( mod > 0 || mod == -1 )
1738: hindex = head_pbucket(mod,bucket);
1739: else if ( mod == -2 )
1740: hindex = head_pbucket_lf(bucket);
1741: else
1742: hindex = head_pbucket_q(bucket);
1743: if ( hindex < 0 ) {
1744: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1745: if ( d ) SG(d) = sugar;
1746: *rp = d;
1747: return 1;
1748: }
1749: g = bucket->body[hindex];
1750: index = ndl_find_reducer(HDL(g));
1751: if ( index >= 0 ) {
1752: count++;
1753: if ( !d ) hcount++;
1754: h = nd_psh[index];
1755: ndl_sub(HDL(g),DL(h),DL(mul));
1756: if ( ndl_check_bound2(index,DL(mul)) ) {
1757: nd_free(d);
1758: free_pbucket(bucket);
1759: *rp = 0;
1760: return 0;
1761: }
1762: p = ps[index];
1763: if ( mod == -1 )
1764: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1765: else if ( mod == -2 ) {
1766: Z inv,t;
1767: divlf(ONE,HCZ(p),&inv);
1768: chsgnlf(HCZ(g),&t);
1769: mullf(inv,t,&CZ(mul));
1770: } else if ( mod ) {
1771: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1772: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1773: } else {
1.6 noro 1774: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1775: chsgnz(cg,&CZ(mul));
1.1 noro 1776: nd_mul_c_q(d,(P)cred);
1777: mulq_pbucket(bucket,cred);
1778: g = bucket->body[hindex];
1.6 noro 1779: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1780: }
1781: red = ndv_mul_nm(mod,mul,p);
1782: bucket->body[hindex] = nd_remove_head(g);
1783: red = nd_remove_head(red);
1784: add_pbucket(mod,bucket,red);
1785: psugar = SG(p)+TD(DL(mul));
1786: sugar = MAX(sugar,psugar);
1787: if ( !mod && hmag && (gmag > hmag) ) {
1788: g = normalize_pbucket(mod,bucket);
1789: if ( !g ) {
1790: if ( d ) SG(d) = sugar;
1791: *rp = d;
1792: return 1;
1793: }
1794: nd_removecont2(d,g);
1.6 noro 1795: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1796: add_pbucket(mod,bucket,g);
1797: }
1798: } else if ( !full ) {
1799: g = normalize_pbucket(mod,bucket);
1800: if ( g ) SG(g) = sugar;
1801: *rp = g;
1802: return 1;
1803: } else {
1804: m = BDY(g);
1805: if ( NEXT(m) ) {
1806: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1807: } else {
1808: FREEND(g); g = 0;
1809: }
1810: bucket->body[hindex] = g;
1811: NEXT(m) = 0;
1812: if ( d ) {
1813: NEXT(tail)=m; tail=m; LEN(d)++;
1814: } else {
1815: MKND(n,m,1,d); tail = BDY(d);
1816: }
1817: }
1818: }
1819: }
1820:
1.25 noro 1821: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1822: {
1823: int hindex,index;
1824: NDV p;
1825: ND u,d,red;
1826: NODE l;
1827: NM mul,m,mrd,tail;
1828: int sugar,psugar,n,h_reducible;
1829: PGeoBucket bucket;
1830: int c,c1,c2;
1831: Z cg,cred,gcd,zzz;
1832: RHist h;
1833: double hmag,gmag;
1834: int count = 0;
1835: int hcount = 0;
1836: SIG sig;
1837:
1838: if ( !g ) {
1839: *rp = 0;
1840: return 1;
1841: }
1842: sugar = SG(g);
1843: n = NV(g);
1844: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1845: bucket = create_pbucket();
1846: add_pbucket(mod,bucket,g);
1847: d = 0;
1848: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1849: sig = g->sig;
1850: while ( 1 ) {
1851: if ( mod > 0 || mod == -1 )
1852: hindex = head_pbucket(mod,bucket);
1853: else if ( mod == -2 )
1854: hindex = head_pbucket_lf(bucket);
1855: else
1856: hindex = head_pbucket_q(bucket);
1857: if ( hindex < 0 ) {
1858: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1859: if ( d ) {
1860: SG(d) = sugar;
1861: d->sig = sig;
1862: }
1863: *rp = d;
1864: return 1;
1865: }
1866: g = bucket->body[hindex];
1867: index = ndl_find_reducer_s(HDL(g),sig);
1868: if ( index >= 0 && index < nd_psn ) {
1869: count++;
1870: if ( !d ) hcount++;
1871: h = nd_psh[index];
1872: ndl_sub(HDL(g),DL(h),DL(mul));
1873: if ( ndl_check_bound2(index,DL(mul)) ) {
1874: nd_free(d);
1875: free_pbucket(bucket);
1876: *rp = 0;
1877: return 0;
1878: }
1879: p = ps[index];
1880: if ( mod == -1 )
1881: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1882: else if ( mod == -2 ) {
1883: Z inv,t;
1884: divlf(ONE,HCZ(p),&inv);
1885: chsgnlf(HCZ(g),&t);
1886: mullf(inv,t,&CZ(mul));
1887: } else if ( mod ) {
1888: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1889: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1890: } else {
1891: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1892: chsgnz(cg,&CZ(mul));
1893: nd_mul_c_q(d,(P)cred);
1894: mulq_pbucket(bucket,cred);
1895: g = bucket->body[hindex];
1896: gmag = (double)p_mag((P)HCZ(g));
1897: }
1898: red = ndv_mul_nm(mod,mul,p);
1899: bucket->body[hindex] = nd_remove_head(g);
1900: red = nd_remove_head(red);
1901: add_pbucket(mod,bucket,red);
1902: psugar = SG(p)+TD(DL(mul));
1903: sugar = MAX(sugar,psugar);
1904: if ( !mod && hmag && (gmag > hmag) ) {
1905: g = normalize_pbucket(mod,bucket);
1906: if ( !g ) {
1907: if ( d ) {
1908: SG(d) = sugar;
1909: d->sig = sig;
1910: }
1911: *rp = d;
1912: return 1;
1913: }
1914: nd_removecont2(d,g);
1915: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1916: add_pbucket(mod,bucket,g);
1917: }
1918: } else if ( index == -1 ) {
1919: // singular top reducible
1920: return -1;
1921: } else if ( !full ) {
1922: g = normalize_pbucket(mod,bucket);
1923: if ( g ) {
1924: SG(g) = sugar;
1925: g->sig = sig;
1926: }
1927: *rp = g;
1928: return 1;
1929: } else {
1930: m = BDY(g);
1931: if ( NEXT(m) ) {
1932: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1933: } else {
1934: FREEND(g); g = 0;
1935: }
1936: bucket->body[hindex] = g;
1937: NEXT(m) = 0;
1938: if ( d ) {
1939: NEXT(tail)=m; tail=m; LEN(d)++;
1940: } else {
1941: MKND(n,m,1,d); tail = BDY(d);
1942: }
1943: }
1944: }
1945: }
1946:
1.1 noro 1947: /* input : list of NDV, cand : list of NDV */
1948:
1949: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1950: {
1951: int n,i,stat;
1952: ND nf,d;
1953: NDV r;
1954: NODE t,s;
1955: union oNDC dn;
1956: Z q;
1957: LIST list;
1958:
1.24 noro 1959: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1960: n = length(cand);
1961:
1962: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1963: /* membercheck : list is a subset of Id(cand) ? */
1964: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1965: again:
1966: nd_tracelist = 0;
1967: if ( nd_bpe > obpe )
1968: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1969: else
1970: r = (NDV)BDY(t);
1971: #if 0
1972: // moved to nd_f4_lf_trace()
1973: if ( m == -2 ) ndv_mod(m,r);
1974: #endif
1975: d = ndvtond(m,r);
1.6 noro 1976: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1977: if ( !stat ) {
1978: nd_reconstruct(0,0);
1979: goto again;
1980: } else if ( nf ) return 0;
1981: if ( nd_gentrace ) {
1982: nd_tracelist = reverse_node(nd_tracelist);
1983: MKLIST(list,nd_tracelist);
1.6 noro 1984: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1985: MKNODE(s,list,nd_alltracelist);
1986: nd_alltracelist = s; nd_tracelist = 0;
1987: }
1988: if ( DP_Print ) { printf("."); fflush(stdout); }
1989: }
1990: if ( DP_Print ) { printf("\n"); }
1991: return 1;
1992: }
1993:
1994: ND nd_remove_head(ND p)
1995: {
1996: NM m;
1997:
1998: m = BDY(p);
1999: if ( !NEXT(m) ) {
2000: FREEND(p); p = 0;
2001: } else {
2002: BDY(p) = NEXT(m); LEN(p)--;
2003: }
2004: FREENM(m);
2005: return p;
2006: }
2007:
2008: ND nd_separate_head(ND p,ND *head)
2009: {
2010: NM m,m0;
2011: ND r;
2012:
2013: m = BDY(p);
2014: if ( !NEXT(m) ) {
2015: *head = p; p = 0;
2016: } else {
2017: m0 = m;
2018: BDY(p) = NEXT(m); LEN(p)--;
2019: NEXT(m0) = 0;
2020: MKND(NV(p),m0,1,r);
2021: *head = r;
2022: }
2023: return p;
2024: }
2025:
2026: PGeoBucket create_pbucket()
2027: {
2028: PGeoBucket g;
2029:
2030: g = CALLOC(1,sizeof(struct oPGeoBucket));
2031: g->m = -1;
2032: return g;
2033: }
2034:
2035: void free_pbucket(PGeoBucket b) {
2036: int i;
2037:
2038: for ( i = 0; i <= b->m; i++ )
2039: if ( b->body[i] ) {
2040: nd_free(b->body[i]);
2041: b->body[i] = 0;
2042: }
2043: GCFREE(b);
2044: }
2045:
1.39 noro 2046: #if 0
1.1 noro 2047: void add_pbucket_symbolic(PGeoBucket g,ND d)
2048: {
2049: int l,i,k,m;
2050:
2051: if ( !d )
2052: return;
2053: l = LEN(d);
2054: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2055: /* 2^(k-1) < l <= 2^k (=m) */
2056: d = nd_merge(g->body[k],d);
2057: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2058: g->body[k] = 0;
2059: d = nd_merge(g->body[k+1],d);
2060: }
2061: g->body[k] = d;
2062: g->m = MAX(g->m,k);
2063: }
1.39 noro 2064: #else
2065: void add_pbucket_symbolic(PGeoBucket g,ND d)
2066: {
2067: int l,i,k,m,m0;
2068:
2069: if ( !d )
2070: return;
2071: m0 = g->m;
2072: while ( 1 ) {
2073: l = LEN(d);
2074: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2075: /* 2^(k-1) < l <= 2^k (=m) */
2076: if ( g->body[k] == 0 ) {
2077: g->body[k] = d;
2078: m0 = MAX(k,m0);
2079: break;
2080: } else {
2081: d = nd_merge(g->body[k],d);
2082: g->body[k] = 0;
2083: }
2084: }
2085: g->m = m0;
2086: }
2087: #endif
1.1 noro 2088:
1.39 noro 2089: #if 0
1.1 noro 2090: void add_pbucket(int mod,PGeoBucket g,ND d)
2091: {
2092: int l,i,k,m;
2093:
2094: if ( !d )
2095: return;
2096: l = LEN(d);
2097: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2098: /* 2^(k-1) < l <= 2^k (=m) */
2099: d = nd_add(mod,g->body[k],d);
2100: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2101: g->body[k] = 0;
2102: d = nd_add(mod,g->body[k+1],d);
2103: }
2104: g->body[k] = d;
2105: g->m = MAX(g->m,k);
2106: }
1.39 noro 2107: #else
2108: void add_pbucket(int mod,PGeoBucket g,ND d)
2109: {
2110: int l,i,k,m,m0;
2111:
2112: m0 = g->m;
2113: while ( d != 0 ) {
2114: l = LEN(d);
2115: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2116: /* 2^(k-1) < l <= 2^k (=m) */
2117: if ( g->body[k] == 0 ) {
2118: g->body[k] = d;
2119: m0 = MAX(k,m0);
2120: break;
2121: } else {
2122: d = nd_add(mod,g->body[k],d);
2123: g->body[k] = 0;
2124: }
2125: }
2126: g->m = m0;
2127: }
2128: #endif
1.1 noro 2129:
2130: void mulq_pbucket(PGeoBucket g,Z c)
2131: {
2132: int k;
2133:
2134: for ( k = 0; k <= g->m; k++ )
2135: nd_mul_c_q(g->body[k],(P)c);
2136: }
2137:
2138: NM remove_head_pbucket_symbolic(PGeoBucket g)
2139: {
2140: int j,i,k,c;
2141: NM head;
2142:
2143: k = g->m;
2144: j = -1;
2145: for ( i = 0; i <= k; i++ ) {
2146: if ( !g->body[i] ) continue;
2147: if ( j < 0 ) j = i;
2148: else {
2149: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2150: if ( c > 0 )
2151: j = i;
2152: else if ( c == 0 )
2153: g->body[i] = nd_remove_head(g->body[i]);
2154: }
2155: }
2156: if ( j < 0 ) return 0;
2157: else {
2158: head = BDY(g->body[j]);
2159: if ( !NEXT(head) ) {
2160: FREEND(g->body[j]);
2161: g->body[j] = 0;
2162: } else {
2163: BDY(g->body[j]) = NEXT(head);
2164: LEN(g->body[j])--;
2165: }
2166: return head;
2167: }
2168: }
2169:
2170: int head_pbucket(int mod,PGeoBucket g)
2171: {
2172: int j,i,c,k,nv,sum;
2173: UINT *di,*dj;
2174: ND gi,gj;
2175:
2176: k = g->m;
2177: while ( 1 ) {
2178: j = -1;
2179: for ( i = 0; i <= k; i++ ) {
2180: if ( !(gi = g->body[i]) )
2181: continue;
2182: if ( j < 0 ) {
2183: j = i;
2184: gj = g->body[j];
2185: dj = HDL(gj);
2186: sum = HCM(gj);
2187: } else {
2188: c = DL_COMPARE(HDL(gi),dj);
2189: if ( c > 0 ) {
2190: if ( sum ) HCM(gj) = sum;
2191: else g->body[j] = nd_remove_head(gj);
2192: j = i;
2193: gj = g->body[j];
2194: dj = HDL(gj);
2195: sum = HCM(gj);
2196: } else if ( c == 0 ) {
2197: if ( mod == -1 )
2198: sum = _addsf(sum,HCM(gi));
2199: else {
2200: sum = sum+HCM(gi)-mod;
2201: if ( sum < 0 ) sum += mod;
2202: }
2203: g->body[i] = nd_remove_head(gi);
2204: }
2205: }
2206: }
2207: if ( j < 0 ) return -1;
2208: else if ( sum ) {
2209: HCM(gj) = sum;
2210: return j;
2211: } else
2212: g->body[j] = nd_remove_head(gj);
2213: }
2214: }
2215:
2216: int head_pbucket_q(PGeoBucket g)
2217: {
2218: int j,i,c,k,nv;
2219: Z sum,t;
2220: ND gi,gj;
2221:
2222: k = g->m;
2223: while ( 1 ) {
2224: j = -1;
2225: for ( i = 0; i <= k; i++ ) {
2226: if ( !(gi = g->body[i]) ) continue;
2227: if ( j < 0 ) {
2228: j = i;
2229: gj = g->body[j];
1.6 noro 2230: sum = HCZ(gj);
1.1 noro 2231: } else {
2232: nv = NV(gi);
2233: c = DL_COMPARE(HDL(gi),HDL(gj));
2234: if ( c > 0 ) {
1.6 noro 2235: if ( sum ) HCZ(gj) = sum;
1.1 noro 2236: else g->body[j] = nd_remove_head(gj);
2237: j = i;
2238: gj = g->body[j];
1.6 noro 2239: sum = HCZ(gj);
1.1 noro 2240: } else if ( c == 0 ) {
1.6 noro 2241: addz(sum,HCZ(gi),&t);
1.1 noro 2242: sum = t;
2243: g->body[i] = nd_remove_head(gi);
2244: }
2245: }
2246: }
2247: if ( j < 0 ) return -1;
2248: else if ( sum ) {
1.6 noro 2249: HCZ(gj) = sum;
1.1 noro 2250: return j;
2251: } else
2252: g->body[j] = nd_remove_head(gj);
2253: }
2254: }
2255:
2256: int head_pbucket_lf(PGeoBucket g)
2257: {
2258: int j,i,c,k,nv;
2259: Z sum,t;
2260: ND gi,gj;
2261:
2262: k = g->m;
2263: while ( 1 ) {
2264: j = -1;
2265: for ( i = 0; i <= k; i++ ) {
2266: if ( !(gi = g->body[i]) ) continue;
2267: if ( j < 0 ) {
2268: j = i;
2269: gj = g->body[j];
2270: sum = HCZ(gj);
2271: } else {
2272: nv = NV(gi);
2273: c = DL_COMPARE(HDL(gi),HDL(gj));
2274: if ( c > 0 ) {
2275: if ( sum ) HCZ(gj) = sum;
2276: else g->body[j] = nd_remove_head(gj);
2277: j = i;
2278: gj = g->body[j];
2279: sum = HCZ(gj);
2280: } else if ( c == 0 ) {
2281: addlf(sum,HCZ(gi),&t);
2282: sum = t;
2283: g->body[i] = nd_remove_head(gi);
2284: }
2285: }
2286: }
2287: if ( j < 0 ) return -1;
2288: else if ( sum ) {
2289: HCZ(gj) = sum;
2290: return j;
2291: } else
2292: g->body[j] = nd_remove_head(gj);
2293: }
2294: }
2295:
2296: ND normalize_pbucket(int mod,PGeoBucket g)
2297: {
2298: int i;
2299: ND r,t;
2300:
2301: r = 0;
2302: for ( i = 0; i <= g->m; i++ ) {
2303: r = nd_add(mod,r,g->body[i]);
2304: g->body[i] = 0;
2305: }
2306: g->m = -1;
2307: return r;
2308: }
2309:
2310: #if 0
2311: void register_hcf(NDV p)
2312: {
2313: DCP dc,t;
2314: P hc,h;
2315: int c;
2316: NODE l,l1,prev;
2317:
2318: hc = p->body->c.p;
2319: if ( !nd_vc || NUM(hc) ) return;
2320: fctrp(nd_vc,hc,&dc);
2321: for ( t = dc; t; t = NEXT(t) ) {
2322: h = t->c;
2323: if ( NUM(h) ) continue;
2324: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2325: c = compp(nd_vc,h,(P)BDY(l));
2326: if ( c >= 0 ) break;
2327: }
2328: if ( !l || c > 0 ) {
2329: MKNODE(l1,h,l);
2330: if ( !prev )
2331: nd_hcf = l1;
2332: else
2333: NEXT(prev) = l1;
2334: }
2335: }
2336: }
2337: #else
2338: void register_hcf(NDV p)
2339: {
2340: DCP dc,t;
2341: P hc,h,q;
2342: Q dmy;
2343: int c;
2344: NODE l,l1,prev;
2345:
2346: hc = p->body->c.p;
2347: if ( NUM(hc) ) return;
2348: ptozp(hc,1,&dmy,&h);
2349: #if 1
2350: for ( l = nd_hcf; l; l = NEXT(l) ) {
2351: while ( 1 ) {
2352: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2353: else break;
2354: }
2355: }
2356: if ( NUM(h) ) return;
2357: #endif
2358: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2359: c = compp(nd_vc,h,(P)BDY(l));
2360: if ( c >= 0 ) break;
2361: }
2362: if ( !l || c > 0 ) {
2363: MKNODE(l1,h,l);
2364: if ( !prev )
2365: nd_hcf = l1;
2366: else
2367: NEXT(prev) = l1;
2368: }
2369: }
2370: #endif
2371:
2372: int do_diagonalize(int sugar,int m)
2373: {
1.6 noro 2374: int i,nh,stat;
2375: NODE r,g,t;
2376: ND h,nf,s,head;
2377: NDV nfv;
2378: Q q;
2379: P nm,nmp,dn,mnp,dnp,cont,cont1;
2380: union oNDC hc;
2381: NODE node;
2382: LIST l;
2383: Z iq;
1.1 noro 2384:
1.6 noro 2385: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2386: if ( nd_gentrace ) {
2387: /* Trace = [1,index,1,1] */
2388: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2389: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2390: }
2391: if ( nd_demand )
2392: nfv = ndv_load(i);
2393: else
2394: nfv = nd_ps[i];
2395: s = ndvtond(m,nfv);
2396: s = nd_separate_head(s,&head);
2397: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2398: if ( !stat ) return 0;
2399: ndv_free(nfv);
2400: hc = HCU(nf); nd_removecont(m,nf);
2401: /* exact division */
2402: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2403: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2404: nfv = ndtondv(m,nf);
2405: nd_free(nf);
2406: nd_bound[i] = ndv_compute_bound(nfv);
2407: if ( !m ) register_hcf(nfv);
2408: if ( nd_demand ) {
2409: ndv_save(nfv,i);
2410: ndv_free(nfv);
2411: } else
2412: nd_ps[i] = nfv;
2413: }
2414: return 1;
1.1 noro 2415: }
2416:
2417: LIST compute_splist()
2418: {
2419: NODE g,tn0,tn,node;
2420: LIST l0;
2421: ND_pairs d,t;
2422: int i;
2423: Z i1,i2;
2424:
2425: g = 0; d = 0;
2426: for ( i = 0; i < nd_psn; i++ ) {
2427: d = update_pairs(d,g,i,0);
2428: g = update_base(g,i);
2429: }
2430: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2431: NEXTNODE(tn0,tn);
1.6 noro 2432: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2433: node = mknode(2,i1,i2); MKLIST(l0,node);
2434: BDY(tn) = l0;
2435: }
2436: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2437: return l0;
2438: }
2439:
2440: /* return value = 0 => input is not a GB */
2441:
2442: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2443: {
1.6 noro 2444: int i,nh,sugar,stat;
2445: NODE r,g,t;
2446: ND_pairs d;
2447: ND_pairs l;
2448: ND h,nf,s,head,nf1;
2449: NDV nfv;
2450: Z q;
2451: union oNDC dn,hc;
2452: int diag_count = 0;
1.41 noro 2453: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2454: P cont;
2455: LIST list;
1.27 noro 2456: struct oEGT eg1,eg2,eg_update;
1.6 noro 2457:
1.27 noro 2458: init_eg(&eg_update);
1.11 noro 2459: Nnd_add = 0;
1.6 noro 2460: g = 0; d = 0;
2461: for ( i = 0; i < nd_psn; i++ ) {
2462: d = update_pairs(d,g,i,gensyz);
2463: g = update_base(g,i);
2464: }
2465: sugar = 0;
2466: while ( d ) {
1.1 noro 2467: again:
1.6 noro 2468: l = nd_minp(d,&d);
2469: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2470: if ( SG(l) != sugar ) {
2471: if ( ishomo ) {
2472: diag_count = 0;
2473: stat = do_diagonalize(sugar,m);
1.1 noro 2474: if ( !stat ) {
1.6 noro 2475: NEXT(l) = d; d = l;
2476: d = nd_reconstruct(0,d);
2477: goto again;
1.1 noro 2478: }
1.6 noro 2479: }
2480: sugar = SG(l);
2481: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2482: }
2483: stat = nd_sp(m,0,l,&h);
2484: if ( !stat ) {
2485: NEXT(l) = d; d = l;
2486: d = nd_reconstruct(0,d);
2487: goto again;
2488: }
1.1 noro 2489: #if USE_GEOBUCKET
1.39 noro 2490: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2491: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2492: #else
1.39 noro 2493: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2494: #endif
1.6 noro 2495: if ( !stat ) {
2496: NEXT(l) = d; d = l;
2497: d = nd_reconstruct(0,d);
2498: goto again;
2499: } else if ( nf ) {
1.41 noro 2500: Nnfnz++;
1.6 noro 2501: if ( checkonly || gensyz ) return 0;
1.1 noro 2502: if ( nd_newelim ) {
2503: if ( nd_module ) {
2504: if ( MPOS(HDL(nf)) > 1 ) return 0;
2505: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2506: }
1.6 noro 2507: if ( DP_Print ) { printf("+"); fflush(stdout); }
2508: hc = HCU(nf);
2509: nd_removecont(m,nf);
2510: if ( !m && nd_nalg ) {
2511: nd_monic(0,&nf);
2512: nd_removecont(m,nf);
2513: }
2514: if ( nd_gentrace ) {
2515: /* exact division */
1.1 noro 2516: cont = ndc_div(m,hc,HCU(nf));
2517: if ( m || !UNIQ(cont) ) {
1.6 noro 2518: t = mknode(4,NULLP,NULLP,NULLP,cont);
2519: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2520: nd_tracelist = t;
2521: }
2522: }
1.6 noro 2523: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2524: nh = ndv_newps(m,nfv,0);
1.6 noro 2525: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2526: diag_count = 0;
2527: stat = do_diagonalize(sugar,m);
2528: if ( !stat ) {
2529: NEXT(l) = d; d = l;
2530: d = nd_reconstruct(1,d);
2531: goto again;
1.1 noro 2532: }
1.6 noro 2533: }
1.27 noro 2534: get_eg(&eg1);
1.6 noro 2535: d = update_pairs(d,g,nh,0);
1.27 noro 2536: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2537: g = update_base(g,nh);
2538: FREENDP(l);
1.41 noro 2539: } else {
2540: Nnfz++;
2541: if ( nd_gentrace && gensyz ) {
2542: nd_tracelist = reverse_node(nd_tracelist);
2543: MKLIST(list,nd_tracelist);
2544: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2545: MKNODE(t,list,nd_alltracelist);
2546: nd_alltracelist = t; nd_tracelist = 0;
2547: }
2548: if ( DP_Print ) { printf("."); fflush(stdout); }
2549: FREENDP(l);
2550: }
1.36 noro 2551: }
2552: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2553: if ( !checkonly && DP_Print ) {
2554: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2555: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2556: fflush(asir_out);
2557: }
2558: if ( DP_Print ) {
2559: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2560: }
1.36 noro 2561: return g;
1.1 noro 2562: }
2563:
1.30 noro 2564: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
2565: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24 noro 2566:
2567: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2568: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2569:
2570: void _copydl(int n,DL d1,DL d2);
2571: void _subfromdl(int n,DL d1,DL d2);
2572: extern int (*cmpdl)(int n,DL d1,DL d2);
2573:
2574: NODE insert_sig(NODE l,SIG s)
2575: {
2576: int pos;
2577: DL sig;
2578: struct oNODE root;
2579: NODE p,prev,r;
2580: SIG t;
2581:
2582: pos = s->pos; sig = DL(s);
2583: root.next = l; prev = &root;
2584: for ( p = l; p; p = p->next ) {
2585: t = (SIG)p->body;
2586: if ( t->pos == pos ) {
2587: if ( _dl_redble(DL(t),sig,nd_nvar) )
2588: return root.next;
2589: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2590: // remove p
2591: prev->next = p->next;
2592: } else
2593: prev = p;
2594: }
1.27 noro 2595: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2596: for ( p = &root; p->next; p = p->next );
2597: p->next = r;
2598: // r->next = root.next;
2599: // return r;
2600: return root.next;
1.24 noro 2601: }
2602:
2603: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2604: {
2605: struct oND_pairs root;
2606: ND_pairs prev,p;
2607: SIG spsig;
2608:
2609: root.next = d;
2610: prev = &root; p = d;
2611: while ( p ) {
2612: spsig = p->sig;
1.26 noro 2613: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2614: // remove p
2615: prev->next = p->next;
1.26 noro 2616: Nsyz++;
2617: } else
1.24 noro 2618: prev = p;
2619: p = p->next;
2620: }
2621: return (ND_pairs)root.next;
2622: }
2623:
1.29 noro 2624: int _dl_redble_ext(DL,DL,DL,int);
2625:
1.28 noro 2626: int small_lcm(ND_pairs l)
2627: {
2628: SIG sig;
2629: int i;
1.29 noro 2630: NODE t;
1.28 noro 2631: static DL lcm,mul,quo;
2632: static int nvar;
2633:
1.38 noro 2634: if ( nd_sba_largelcm ) return 0;
1.28 noro 2635: if ( nvar < nd_nvar ) {
2636: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2637: }
2638: sig = l->sig;
2639: _ndltodl(l->lcm,lcm);
1.29 noro 2640: #if 0
1.28 noro 2641: for ( i = 0; i < nd_psn; i++ ) {
2642: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2643: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2644: _ndltodl(DL(nd_psh[i]),mul);
2645: _addtodl(nd_nvar,quo,mul);
2646: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2647: break;
2648: }
2649: }
2650: if ( i < nd_psn ) return 1;
2651: else return 0;
1.29 noro 2652: #else
2653: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2654: i = (long)BDY(t);
2655: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2656: _ndltodl(DL(nd_psh[i]),mul);
2657: _addtodl(nd_nvar,quo,mul);
2658: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2659: break;
2660: }
2661: }
2662: if ( t ) return 1;
2663: else return 0;
2664: #endif
1.28 noro 2665: }
2666:
1.41 noro 2667: ND_pairs find_smallest_lcm(ND_pairs l)
2668: {
2669: SIG sig;
2670: int i,minindex;
2671: NODE t;
2672: ND_pairs r;
2673: struct oSIG sig1;
2674: static DL mul,quo,minlm;
2675: static int nvar;
2676:
2677: if ( nvar < nd_nvar ) {
2678: nvar = nd_nvar;
2679: NEWDL(quo,nvar); NEWDL(mul,nvar);
2680: NEWDL(minlm,nvar);
2681: }
2682: sig = l->sig;
2683: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2684: _ndltodl(l->lcm,minlm); minindex = -1;
2685: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2686: i = (long)BDY(t);
2687: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2688: _ndltodl(DL(nd_psh[i]),mul);
2689: _addtodl(nd_nvar,quo,mul);
2690: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2691: minindex = i;
2692: _copydl(nd_nvar,mul,minlm);
2693: }
2694: }
2695: }
2696: // l->lcm is minimal; return l itself
2697: if ( minindex < 0 ) return l;
2698: for ( i = 0; i < nd_psn; i++ ) {
2699: if ( i == minindex ) continue;
2700: _ndltodl(DL(nd_psh[i]),mul);
2701: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2702: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2703: sig1.pos = nd_ps[i]->sig->pos;
2704: sig1.dl = quo;
2705: if ( comp_sig(sig,&sig1) > 0 ) {
2706: // printf("X");
2707: NEWND_pairs(r);
2708: r->sig = sig;
2709: r->i1 = minindex;
2710: r->i2 = i;
2711: dltondl(nd_nvar,minlm,r->lcm);
2712: r->next = 0;
2713: return r;
2714: }
2715: }
2716: }
2717: // there is no suitable spair
2718: return 0;
2719: }
2720:
1.28 noro 2721: ND_pairs remove_large_lcm(ND_pairs d)
2722: {
2723: struct oND_pairs root;
2724: ND_pairs prev,p;
2725:
2726: root.next = d;
2727: prev = &root; p = d;
2728: while ( p ) {
1.41 noro 2729: #if 0
1.28 noro 2730: if ( small_lcm(p) ) {
2731: // remove p
2732: prev->next = p->next;
2733: } else
1.41 noro 2734: #else
2735: if ( find_smallest_lcm(p) == 0 ) {
2736: // remove p
2737: prev->next = p->next;
2738: } else
2739: #endif
1.28 noro 2740: prev = p;
2741: p = p->next;
2742: }
2743: return (ND_pairs)root.next;
2744: }
2745:
1.26 noro 2746: struct oEGT eg_create,eg_newpairs,eg_merge;
2747:
1.30 noro 2748: NODE conv_ilist_s(int demand,int trace,int **indp);
2749:
1.24 noro 2750: NODE nd_sba_buch(int m,int ishomo,int **indp)
2751: {
1.39 noro 2752: int i,j,nh,sugar,stat,pos;
1.30 noro 2753: NODE r,t,g;
1.24 noro 2754: ND_pairs d;
1.41 noro 2755: ND_pairs l,l1;
1.24 noro 2756: ND h,nf,s,head,nf1;
2757: NDV nfv;
2758: Z q;
2759: union oNDC dn,hc;
2760: P cont;
2761: LIST list;
2762: SIG sig;
1.29 noro 2763: NODE *syzlist;
1.41 noro 2764: int Nnominimal,Nredundant;
1.28 noro 2765: DL lcm,quo,mul;
1.31 noro 2766: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.41 noro 2767: int Nnfs=0,Nnfz=0,Nnfnz=0;
1.24 noro 2768:
1.27 noro 2769: init_eg(&eg_remove);
1.29 noro 2770: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26 noro 2771: Nsyz = 0;
1.24 noro 2772: Nnd_add = 0;
1.41 noro 2773: Nnominimal = 0;
1.26 noro 2774: Nredundant = 0;
1.30 noro 2775: d = 0;
1.29 noro 2776: for ( i = 0; i < nd_psn; i++ )
2777: for ( j = i+1; j < nd_psn; j++ ) {
2778: NEWSIG(sig); sig->pos = j;
2779: _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
2780: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
2781: }
1.41 noro 2782: for ( i = 0; i < nd_psn; i++ ) {
2783: d = update_pairs_s(d,i,syzlist);
2784: }
1.24 noro 2785: sugar = 0;
1.39 noro 2786: pos = 0;
1.26 noro 2787: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2788: init_eg(&eg_create);
2789: init_eg(&eg_merge);
1.29 noro 2790: init_eg(&eg_large);
1.31 noro 2791: init_eg(&eg_nf);
2792: init_eg(&eg_nfzero);
1.24 noro 2793: while ( d ) {
2794: again:
1.26 noro 2795: if ( DP_Print ) {
2796: int len;
2797: ND_pairs td;
1.28 noro 2798: for ( td = d, len=0; td; td = td->next, len++)
2799: ;
1.26 noro 2800: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2801: }
1.24 noro 2802: l = d; d = d->next;
1.41 noro 2803: #if 0
1.28 noro 2804: if ( small_lcm(l) ) {
1.24 noro 2805: if ( DP_Print ) fprintf(asir_out,"M");
1.41 noro 2806: Nnominimal++;
1.24 noro 2807: continue;
2808: }
2809: if ( SG(l) != sugar ) {
2810: sugar = SG(l);
2811: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2812: }
1.28 noro 2813: sig = l->sig;
1.39 noro 2814: if ( DP_Print && nd_sba_pot ) {
2815: if ( sig->pos != pos ) {
2816: fprintf(asir_out,"[%d]",sig->pos);
2817: pos = sig->pos;
2818: }
2819: }
1.24 noro 2820: stat = nd_sp(m,0,l,&h);
1.41 noro 2821: #else
2822: l1 = find_smallest_lcm(l);
2823: if ( l1 == 0 ) {
2824: if ( DP_Print ) fprintf(asir_out,"M");
2825: Nnominimal++;
2826: continue;
2827: }
2828: if ( SG(l1) != sugar ) {
2829: sugar = SG(l1);
2830: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2831: }
2832: sig = l1->sig;
2833: if ( DP_Print && nd_sba_pot ) {
2834: if ( sig->pos != pos ) {
2835: fprintf(asir_out,"[%d]",sig->pos);
2836: pos = sig->pos;
2837: }
2838: }
2839: stat = nd_sp(m,0,l1,&h);
2840: #endif
1.24 noro 2841: if ( !stat ) {
2842: NEXT(l) = d; d = l;
2843: d = nd_reconstruct(0,d);
2844: goto again;
2845: }
1.31 noro 2846: get_eg(&eg1);
1.25 noro 2847: #if USE_GEOBUCKET
1.39 noro 2848: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2849: #else
1.39 noro 2850: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2851: #endif
1.31 noro 2852: get_eg(&eg2);
1.24 noro 2853: if ( !stat ) {
2854: NEXT(l) = d; d = l;
2855: d = nd_reconstruct(0,d);
2856: goto again;
2857: } else if ( stat == -1 ) {
1.41 noro 2858: Nnfs++;
1.24 noro 2859: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 noro 2860: FREENDP(l);
1.24 noro 2861: } else if ( nf ) {
1.41 noro 2862: Nnfnz++;
2863: if ( DP_Print ) {
2864: if ( nd_sba_redundant_check ) {
2865: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
2866: Nredundant++;
2867: printf("R");
2868: } else
2869: printf("+");
2870: } else
2871: printf("+");
2872: fflush(stdout);
2873: }
1.31 noro 2874: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 2875: hc = HCU(nf);
2876: nd_removecont(m,nf);
2877: nfv = ndtondv(m,nf); nd_free(nf);
2878: nh = ndv_newps(m,nfv,0);
1.26 noro 2879:
1.30 noro 2880: d = update_pairs_s(d,nh,syzlist);
1.29 noro 2881: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2882: FREENDP(l);
2883: } else {
1.41 noro 2884: Nnfz++;
1.31 noro 2885: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 2886: // syzygy
1.27 noro 2887: get_eg(&eg1);
1.24 noro 2888: d = remove_spair_s(d,sig);
1.27 noro 2889: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2890: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2891: if ( DP_Print ) { printf("."); fflush(stdout); }
2892: FREENDP(l);
2893: }
2894: }
1.30 noro 2895: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2896: if ( DP_Print ) {
1.41 noro 2897: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",Nnd_add,Nsyz,Nsamesig,Nnominimal);
2898: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
2899: fflush(stdout);
2900: if ( nd_sba_redundant_check )
2901: printf("Nredundant=%d\n",Nredundant);
1.26 noro 2902: fflush(stdout);
2903: print_eg("create",&eg_create);
2904: print_eg("merge",&eg_merge);
1.27 noro 2905: print_eg("remove",&eg_remove);
1.31 noro 2906: print_eg("nf",&eg_nf);
2907: print_eg("nfzero",&eg_nfzero);
1.27 noro 2908: printf("\n");
1.26 noro 2909: }
1.24 noro 2910: return g;
2911: }
2912:
1.1 noro 2913: /* splist = [[i1,i2],...] */
2914:
2915: int check_splist(int m,NODE splist)
2916: {
2917: NODE t,p;
2918: ND_pairs d,r,l;
2919: int stat;
2920: ND h,nf;
2921:
2922: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2923: p = BDY((LIST)BDY(t));
1.6 noro 2924: NEXTND_pairs(d,r);
2925: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2926: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2927: SG(r) = TD(LCM(r)); /* XXX */
2928: }
2929: if ( d ) NEXT(r) = 0;
2930:
1.6 noro 2931: while ( d ) {
1.1 noro 2932: again:
1.6 noro 2933: l = nd_minp(d,&d);
2934: stat = nd_sp(m,0,l,&h);
2935: if ( !stat ) {
2936: NEXT(l) = d; d = l;
2937: d = nd_reconstruct(0,d);
2938: goto again;
2939: }
1.39 noro 2940: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 2941: if ( !stat ) {
2942: NEXT(l) = d; d = l;
2943: d = nd_reconstruct(0,d);
2944: goto again;
2945: } else if ( nf ) return 0;
1.1 noro 2946: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2947: }
1.1 noro 2948: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2949: return 1;
2950: }
2951:
2952: int check_splist_f4(int m,NODE splist)
2953: {
2954: UINT *s0vect;
1.6 noro 2955: PGeoBucket bucket;
1.1 noro 2956: NODE p,rp0,t;
2957: ND_pairs d,r,l,ll;
2958: int col,stat;
2959:
2960: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2961: p = BDY((LIST)BDY(t));
1.6 noro 2962: NEXTND_pairs(d,r);
2963: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2964: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2965: SG(r) = TD(LCM(r)); /* XXX */
2966: }
2967: if ( d ) NEXT(r) = 0;
2968:
1.6 noro 2969: while ( d ) {
2970: l = nd_minsugarp(d,&d);
2971: bucket = create_pbucket();
2972: stat = nd_sp_f4(m,0,l,bucket);
2973: if ( !stat ) {
2974: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2975: NEXT(ll) = d; d = l;
2976: d = nd_reconstruct(0,d);
2977: continue;
2978: }
2979: if ( bucket->m < 0 ) continue;
2980: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2981: if ( !col ) {
2982: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2983: NEXT(ll) = d; d = l;
2984: d = nd_reconstruct(0,d);
2985: continue;
1.1 noro 2986: }
1.6 noro 2987: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2988: }
2989: return 1;
1.1 noro 2990: }
2991:
2992: int do_diagonalize_trace(int sugar,int m)
2993: {
1.6 noro 2994: int i,nh,stat;
2995: NODE r,g,t;
2996: ND h,nf,nfq,s,head;
2997: NDV nfv,nfqv;
2998: Q q,den,num;
2999: union oNDC hc;
3000: NODE node;
3001: LIST l;
3002: Z iq;
3003: P cont,cont1;
1.1 noro 3004:
1.6 noro 3005: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3006: if ( nd_gentrace ) {
3007: /* Trace = [1,index,1,1] */
3008: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3009: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3010: }
3011: /* for nd_ps */
3012: s = ndvtond(m,nd_ps[i]);
3013: s = nd_separate_head(s,&head);
3014: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3015: if ( !stat ) return 0;
3016: nf = nd_add(m,head,nf);
3017: ndv_free(nd_ps[i]);
3018: nd_ps[i] = ndtondv(m,nf);
3019: nd_free(nf);
3020:
3021: /* for nd_ps_trace */
3022: if ( nd_demand )
3023: nfv = ndv_load(i);
3024: else
3025: nfv = nd_ps_trace[i];
3026: s = ndvtond(0,nfv);
3027: s = nd_separate_head(s,&head);
3028: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3029: if ( !stat ) return 0;
3030: ndv_free(nfv);
3031: hc = HCU(nf); nd_removecont(0,nf);
3032: /* exact division */
1.1 noro 3033: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3034: if ( nd_gentrace ) finalize_tracelist(i,cont);
3035: nfv = ndtondv(0,nf);
3036: nd_free(nf);
3037: nd_bound[i] = ndv_compute_bound(nfv);
3038: register_hcf(nfv);
3039: if ( nd_demand ) {
3040: ndv_save(nfv,i);
3041: ndv_free(nfv);
3042: } else
3043: nd_ps_trace[i] = nfv;
3044: }
3045: return 1;
1.1 noro 3046: }
3047:
3048: static struct oEGT eg_invdalg;
3049: struct oEGT eg_le;
3050:
3051: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3052: {
3053: NODE tn;
3054: P p1;
3055:
3056: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3057: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3058: }
3059: *r = p;
3060: }
3061:
3062: NODE nd_gb_trace(int m,int ishomo,int **indp)
3063: {
1.6 noro 3064: int i,nh,sugar,stat;
3065: NODE r,g,t;
3066: ND_pairs d;
3067: ND_pairs l;
3068: ND h,nf,nfq,s,head;
3069: NDV nfv,nfqv;
3070: Z q,den,num;
3071: P hc;
3072: union oNDC dn,hnfq;
3073: struct oEGT eg_monic,egm0,egm1;
3074: int diag_count = 0;
3075: P cont;
3076: LIST list;
3077:
3078: init_eg(&eg_monic);
3079: init_eg(&eg_invdalg);
3080: init_eg(&eg_le);
3081: g = 0; d = 0;
3082: for ( i = 0; i < nd_psn; i++ ) {
3083: d = update_pairs(d,g,i,0);
3084: g = update_base(g,i);
3085: }
3086: sugar = 0;
3087: while ( d ) {
1.1 noro 3088: again:
1.6 noro 3089: l = nd_minp(d,&d);
3090: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3091: if ( SG(l) != sugar ) {
1.1 noro 3092: #if 1
1.6 noro 3093: if ( ishomo ) {
3094: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3095: stat = do_diagonalize_trace(sugar,m);
3096: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3097: diag_count = 0;
1.1 noro 3098: if ( !stat ) {
1.6 noro 3099: NEXT(l) = d; d = l;
3100: d = nd_reconstruct(1,d);
3101: goto again;
1.1 noro 3102: }
1.6 noro 3103: }
3104: #endif
3105: sugar = SG(l);
3106: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3107: }
3108: stat = nd_sp(m,0,l,&h);
3109: if ( !stat ) {
3110: NEXT(l) = d; d = l;
3111: d = nd_reconstruct(1,d);
3112: goto again;
3113: }
1.1 noro 3114: #if USE_GEOBUCKET
1.39 noro 3115: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3116: #else
1.39 noro 3117: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3118: #endif
1.6 noro 3119: if ( !stat ) {
3120: NEXT(l) = d; d = l;
3121: d = nd_reconstruct(1,d);
3122: goto again;
3123: } else if ( nf ) {
3124: if ( nd_demand ) {
3125: nfqv = ndv_load(nd_psn);
3126: nfq = ndvtond(0,nfqv);
3127: } else
3128: nfq = 0;
3129: if ( !nfq ) {
1.39 noro 3130: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3131: NEXT(l) = d; d = l;
3132: d = nd_reconstruct(1,d);
3133: goto again;
3134: }
3135: }
3136: if ( nfq ) {
3137: /* m|HC(nfq) => failure */
3138: if ( nd_vc ) {
3139: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3140: } else
3141: q = HCZ(nfq);
3142: if ( !remqi((Q)q,m) ) return 0;
3143:
3144: if ( DP_Print ) { printf("+"); fflush(stdout); }
3145: hnfq = HCU(nfq);
3146: if ( nd_nalg ) {
3147: /* m|DN(HC(nf)^(-1)) => failure */
3148: get_eg(&egm0);
3149: if ( !nd_monic(m,&nfq) ) return 0;
3150: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3151: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3152: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3153: } else {
3154: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3155: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3156: }
3157: if ( nd_gentrace ) {
3158: /* exact division */
3159: cont = ndc_div(0,hnfq,HCU(nfqv));
3160: if ( !UNIQ(cont) ) {
3161: t = mknode(4,NULLP,NULLP,NULLP,cont);
3162: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3163: nd_tracelist = t;
3164: }
3165: }
1.24 noro 3166: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3167: if ( ishomo && ++diag_count == diag_period ) {
3168: diag_count = 0;
3169: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3170: stat = do_diagonalize_trace(sugar,m);
3171: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3172: if ( !stat ) {
1.1 noro 3173: NEXT(l) = d; d = l;
3174: d = nd_reconstruct(1,d);
3175: goto again;
1.6 noro 3176: }
1.1 noro 3177: }
1.6 noro 3178: d = update_pairs(d,g,nh,0);
3179: g = update_base(g,nh);
3180: } else {
3181: if ( DP_Print ) { printf("*"); fflush(stdout); }
3182: }
3183: } else {
3184: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3185: }
1.6 noro 3186: FREENDP(l);
3187: }
3188: if ( nd_nalg ) {
3189: if ( DP_Print ) {
3190: print_eg("monic",&eg_monic);
3191: print_eg("invdalg",&eg_invdalg);
3192: print_eg("le",&eg_le);
1.1 noro 3193: }
1.6 noro 3194: }
1.1 noro 3195: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3196: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3197: return g;
1.1 noro 3198: }
3199:
3200: int ndv_compare(NDV *p1,NDV *p2)
3201: {
3202: return DL_COMPARE(HDL(*p1),HDL(*p2));
3203: }
3204:
3205: int ndv_compare_rev(NDV *p1,NDV *p2)
3206: {
3207: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3208: }
3209:
3210: int ndvi_compare(NDVI p1,NDVI p2)
3211: {
3212: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3213: }
3214:
3215: int ndvi_compare_rev(NDVI p1,NDVI p2)
3216: {
3217: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3218: }
3219:
3220: NODE ndv_reduceall(int m,NODE f)
3221: {
3222: int i,j,n,stat;
3223: ND nf,g,head;
3224: NODE t,a0,a;
3225: union oNDC dn;
3226: Q q,num,den;
3227: NODE node;
3228: LIST l;
3229: Z iq,jq;
3230: int *perm;
3231: union oNDC hc;
3232: P cont,cont1;
3233:
3234: if ( nd_nora ) return f;
3235: n = length(f);
1.24 noro 3236: ndv_setup(m,0,f,0,1,0);
1.1 noro 3237: perm = (int *)MALLOC(n*sizeof(int));
3238: if ( nd_gentrace ) {
3239: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3240: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3241: }
3242: for ( i = 0; i < n; ) {
3243: if ( nd_gentrace ) {
3244: /* Trace = [1,index,1,1] */
1.6 noro 3245: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3246: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3247: }
3248: g = ndvtond(m,nd_ps[i]);
3249: g = nd_separate_head(g,&head);
1.6 noro 3250: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3251: if ( !stat )
3252: nd_reconstruct(0,0);
3253: else {
3254: if ( DP_Print ) { printf("."); fflush(stdout); }
3255: ndv_free(nd_ps[i]);
3256: hc = HCU(nf); nd_removecont(m,nf);
3257: if ( nd_gentrace ) {
3258: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3259: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3260: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3261: }
1.6 noro 3262: /* exact division */
1.1 noro 3263: cont = ndc_div(m,hc,HCU(nf));
3264: finalize_tracelist(perm[i],cont);
3265: }
3266: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3267: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3268: i++;
3269: }
3270: }
3271: if ( DP_Print ) { printf("\n"); }
3272: for ( a0 = 0, i = 0; i < n; i++ ) {
3273: NEXTNODE(a0,a);
3274: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3275: else {
3276: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3277: BDY(a) = (pointer)nd_ps[j];
3278: }
3279: }
3280: NEXT(a) = 0;
3281: return a0;
3282: }
3283:
1.41 noro 3284: int ndplength(ND_pairs d)
3285: {
3286: int i;
3287: for ( i = 0; d; i++ ) d = NEXT(d);
3288: return i;
3289: }
3290:
1.1 noro 3291: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3292: {
3293: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3294: int len0;
1.1 noro 3295:
3296: if ( !g ) return d;
3297: /* for testing */
3298: if ( gensyz && nd_gensyz == 2 ) {
3299: d1 = nd_newpairs(g,t);
3300: if ( !d )
3301: return d1;
3302: else {
3303: nd = d;
3304: while ( NEXT(nd) ) nd = NEXT(nd);
3305: NEXT(nd) = d1;
3306: return d;
3307: }
3308: }
3309: d = crit_B(d,t);
3310: d1 = nd_newpairs(g,t);
1.41 noro 3311: len0 = ndplength(d1);
1.1 noro 3312: d1 = crit_M(d1);
3313: d1 = crit_F(d1);
1.41 noro 3314: NcriMF += len0-ndplength(d1);
1.1 noro 3315: if ( gensyz || do_weyl )
3316: head = d1;
3317: else {
3318: prev = 0; cur = head = d1;
3319: while ( cur ) {
3320: if ( crit_2( cur->i1,cur->i2 ) ) {
3321: remove = cur;
3322: if ( !prev ) head = cur = NEXT(cur);
3323: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3324: FREENDP(remove); Ncri2++;
1.1 noro 3325: } else {
3326: prev = cur; cur = NEXT(cur);
3327: }
3328: }
3329: }
3330: if ( !d )
3331: return head;
3332: else {
3333: nd = d;
3334: while ( NEXT(nd) ) nd = NEXT(nd);
3335: NEXT(nd) = head;
3336: return d;
3337: }
3338: }
3339:
1.24 noro 3340: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3341:
1.30 noro 3342: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3343: {
3344: ND_pairs d1;
1.26 noro 3345: struct oEGT eg1,eg2,eg3;
1.24 noro 3346:
1.30 noro 3347: if ( !t ) return d;
1.26 noro 3348: get_eg(&eg1);
1.30 noro 3349: d1 = nd_newpairs_s(t,syz);
1.26 noro 3350: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3351: d = merge_pairs_s(d,d1);
1.26 noro 3352: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3353: return d;
3354: }
1.1 noro 3355:
3356: ND_pairs nd_newpairs( NODE g, int t )
3357: {
3358: NODE h;
3359: UINT *dl;
3360: int ts,s,i,t0,min,max;
3361: ND_pairs r,r0;
3362:
3363: dl = DL(nd_psh[t]);
3364: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3365: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3366: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3367: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3368: continue;
3369: if ( nd_gbblock ) {
3370: t0 = (long)BDY(h);
3371: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3372: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3373: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3374: break;
3375: }
3376: if ( nd_gbblock[i] >= 0 )
3377: continue;
3378: }
1.41 noro 3379: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3380: r->i1 = (long)BDY(h);
3381: r->i2 = t;
3382: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3383: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3384: SG(r) = MAX(s,ts) + TD(LCM(r));
3385: /* experimental */
3386: if ( nd_sugarweight )
3387: r->sugar2 = ndl_weight2(r->lcm);
3388: }
3389: if ( r0 ) NEXT(r) = 0;
3390: return r0;
3391: }
3392:
1.24 noro 3393: int comp_sig(SIG s1,SIG s2)
3394: {
1.38 noro 3395: if ( nd_sba_pot ) {
3396: if ( s1->pos > s2->pos ) return 1;
3397: else if ( s1->pos < s2->pos ) return -1;
3398: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3399: } else {
3400: static DL m1,m2;
3401: static int nvar;
3402: int ret;
3403:
3404: if ( nvar != nd_nvar ) {
3405: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3406: }
3407: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3408: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
3409: ret = (*cmpdl)(nd_nvar,m1,m2);
3410: if ( ret != 0 ) return ret;
3411: else if ( s1->pos > s2->pos ) return 1;
3412: else if ( s1->pos < s2->pos ) return -1;
3413: else return 0;
1.24 noro 3414: }
3415: }
3416:
3417: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3418: {
3419: int ret,s1,s2;
3420: RHist p1,p2;
3421: static int wpd;
3422: static UINT *lcm;
3423:
3424: sp->i1 = i1;
3425: sp->i2 = i2;
3426: p1 = nd_psh[i1];
3427: p2 = nd_psh[i2];
3428: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3429: s1 = SG(p1)-TD(DL(p1));
3430: s2 = SG(p2)-TD(DL(p2));
3431: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3432:
1.26 noro 3433: if ( wpd != nd_wpd ) {
1.24 noro 3434: wpd = nd_wpd;
3435: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3436: }
3437: // DL(sig1) <- sp->lcm
3438: // DL(sig1) -= DL(p1)
3439: // DL(sig1) += DL(p1->sig)
1.27 noro 3440: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3441: _ndltodl(lcm,DL(sig1));
3442: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3443: sig1->pos = p1->sig->pos;
3444:
3445: // DL(sig2) <- sp->lcm
3446: // DL(sig2) -= DL(p2)
3447: // DL(sig2) += DL(p2->sig)
1.27 noro 3448: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3449: _ndltodl(lcm,DL(sig2));
3450: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3451: sig2->pos = p2->sig->pos;
3452:
3453: ret = comp_sig(sig1,sig2);
3454: if ( ret == 0 ) return 0;
3455: else if ( ret > 0 ) sp->sig = sig1;
3456: else sp->sig = sig2;
3457: return 1;
3458: }
3459:
3460: SIG dup_sig(SIG sig)
3461: {
3462: SIG r;
3463:
3464: if ( !sig ) return 0;
3465: else {
3466: NEWSIG(r);
3467: _copydl(nd_nvar,DL(sig),DL(r));
3468: r->pos = sig->pos;
3469: return r;
3470: }
3471: }
3472:
3473: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3474: {
3475: to->i1 = from->i1;
3476: to->i2 = from->i2;
3477: to->sugar = from->sugar;
3478: to->sugar2 = from->sugar2;
3479: ndl_copy(from->lcm,to->lcm);
3480: to->sig = dup_sig(from->sig);
3481: }
3482:
3483: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3484: {
3485: struct oND_pairs root;
3486: ND_pairs q1,q2,r0,r;
3487: int ret;
3488:
3489: r = &root;
3490: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3491: ret = comp_sig(q1->sig,q2->sig);
3492: if ( ret < 0 ) {
3493: r->next = q1; r = q1; q1 = q1->next;
3494: } else if ( ret > 0 ) {
3495: r->next = q2; r = q2; q2 = q2->next;
3496: } else {
3497: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 3498: Nsamesig++;
1.24 noro 3499: if ( ret < 0 ) {
3500: r->next = q1; r = q1; q1 = q1->next;
3501: q2 = q2->next;
3502: } else {
3503: r->next = q2; r = q2; q2 = q2->next;
3504: q1 = q1->next;
3505: }
3506: }
3507: }
3508: if ( q1 ) {
3509: r->next = q1;
3510: } else {
3511: r->next = q2;
3512: }
3513: return root.next;
3514: }
3515:
3516: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3517: {
3518: ND_pairs p,prev;
3519: int ret;
3520:
3521: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3522: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3523: break;
3524: }
3525: if ( ret == 0 ) {
3526: ret = DL_COMPARE(s->lcm,p->lcm);
3527: if ( ret < 0 ) {
3528: // replace p with s
3529: s->next = p->next;
3530: if ( prev == 0 ) {
3531: return s;
3532: } else {
3533: prev->next = s;
3534: return l;
3535: }
3536: } else
3537: return l;
3538: } else {
3539: // insert s between prev and p
3540: s->next = p;
3541: if ( prev == 0 ) {
3542: return s;
3543: } else {
3544: prev->next = s;
3545: return l;
3546: }
3547: }
3548: }
3549:
1.29 noro 3550: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3551: {
3552: int i;
3553:
3554: if ( d1->td > d2->td )
3555: return 0;
3556: for ( i = nvar-1; i >= 0; i-- )
3557: if ( d1->d[i] > d2->d[i] )
3558: break;
3559: if ( i >= 0 )
3560: return 0;
3561: else
3562: return 1;
3563: }
3564:
1.30 noro 3565: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3566: {
3567: NODE h,s;
3568: UINT *dl;
1.27 noro 3569: int ts,ret,i;
1.24 noro 3570: ND_pairs r,r0,_sp,sp;
1.29 noro 3571: SIG spsig,tsig;
3572: static int nvar;
3573: static SIG _sig1,_sig2;
1.26 noro 3574: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3575:
3576: NEWND_pairs(_sp);
1.29 noro 3577: if ( !_sig1 || nvar != nd_nvar ) {
3578: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3579: }
1.24 noro 3580: r0 = 0;
1.27 noro 3581: for ( i = 0; i < t; i++ ) {
3582: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3583: if ( ret ) {
3584: spsig = _sp->sig;
1.29 noro 3585: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3586: tsig = (SIG)s->body;
1.29 noro 3587: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3588: break;
3589: }
3590: if ( s == 0 ) {
3591: NEWND_pairs(sp);
3592: dup_ND_pairs(sp,_sp);
3593: r0 = insert_pair_s(r0,sp);
1.26 noro 3594: } else
3595: Nsyz++;
1.24 noro 3596: }
3597: }
3598: return r0;
3599: }
3600:
1.1 noro 3601: /* ipair = [i1,i2],[i1,i2],... */
3602: ND_pairs nd_ipairtospair(NODE ipair)
3603: {
3604: int s1,s2;
3605: NODE tn,t;
3606: ND_pairs r,r0;
3607:
3608: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3609: NEXTND_pairs(r0,r);
3610: tn = BDY((LIST)BDY(t));
1.6 noro 3611: r->i1 = ZTOS((Q)ARG0(tn));
3612: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3613: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3614: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3615: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3616: SG(r) = MAX(s1,s2) + TD(LCM(r));
3617: /* experimental */
3618: if ( nd_sugarweight )
3619: r->sugar2 = ndl_weight2(r->lcm);
3620: }
3621: if ( r0 ) NEXT(r) = 0;
3622: return r0;
3623: }
3624:
3625: /* kokokara */
3626:
3627: ND_pairs crit_B( ND_pairs d, int s )
3628: {
3629: ND_pairs cur,head,prev,remove;
3630: UINT *t,*tl,*lcm;
3631: int td,tdl;
3632:
3633: if ( !d ) return 0;
3634: t = DL(nd_psh[s]);
3635: prev = 0;
3636: head = cur = d;
3637: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3638: while ( cur ) {
3639: tl = cur->lcm;
3640: if ( ndl_reducible(tl,t) ) {
3641: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3642: if ( !ndl_equal(lcm,tl) ) {
3643: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3644: if (!ndl_equal(lcm,tl)) {
3645: remove = cur;
3646: if ( !prev ) {
3647: head = cur = NEXT(cur);
3648: } else {
3649: cur = NEXT(prev) = NEXT(cur);
3650: }
1.41 noro 3651: FREENDP(remove); NcriB++;
1.1 noro 3652: } else {
3653: prev = cur; cur = NEXT(cur);
3654: }
3655: } else {
3656: prev = cur; cur = NEXT(cur);
3657: }
3658: } else {
3659: prev = cur; cur = NEXT(cur);
3660: }
3661: }
3662: return head;
3663: }
3664:
3665: ND_pairs crit_M( ND_pairs d1 )
3666: {
3667: ND_pairs e,d2,d3,dd,p;
3668: UINT *id,*jd;
3669:
3670: if ( !d1 ) return d1;
3671: for ( dd = 0, e = d1; e; e = d3 ) {
3672: if ( !(d2 = NEXT(e)) ) {
3673: NEXT(e) = dd;
3674: return e;
3675: }
3676: id = LCM(e);
3677: for ( d3 = 0; d2; d2 = p ) {
3678: p = NEXT(d2);
3679: jd = LCM(d2);
3680: if ( ndl_equal(jd,id) )
3681: ;
3682: else if ( TD(jd) > TD(id) )
3683: if ( ndl_reducible(jd,id) ) continue;
3684: else ;
3685: else if ( ndl_reducible(id,jd) ) goto delit;
3686: NEXT(d2) = d3;
3687: d3 = d2;
3688: }
3689: NEXT(e) = dd;
3690: dd = e;
3691: continue;
3692: /**/
3693: delit: NEXT(d2) = d3;
3694: d3 = d2;
3695: for ( ; p; p = d2 ) {
3696: d2 = NEXT(p);
3697: NEXT(p) = d3;
3698: d3 = p;
3699: }
3700: FREENDP(e);
3701: }
3702: return dd;
3703: }
3704:
3705: ND_pairs crit_F( ND_pairs d1 )
3706: {
3707: ND_pairs rest, head,remove;
3708: ND_pairs last, p, r, w;
3709: int s;
3710:
3711: if ( !d1 ) return d1;
3712: for ( head = last = 0, p = d1; NEXT(p); ) {
3713: r = w = equivalent_pairs(p,&rest);
3714: s = SG(r);
3715: w = NEXT(w);
3716: while ( w ) {
3717: if ( crit_2(w->i1,w->i2) ) {
3718: r = w;
3719: w = NEXT(w);
3720: while ( w ) {
3721: remove = w;
3722: w = NEXT(w);
3723: FREENDP(remove);
3724: }
3725: break;
3726: } else if ( SG(w) < s ) {
3727: FREENDP(r);
3728: r = w;
3729: s = SG(r);
3730: w = NEXT(w);
3731: } else {
3732: remove = w;
3733: w = NEXT(w);
3734: FREENDP(remove);
3735: }
3736: }
3737: if ( last ) NEXT(last) = r;
3738: else head = r;
3739: NEXT(last = r) = 0;
3740: p = rest;
3741: if ( !p ) return head;
3742: }
3743: if ( !last ) return p;
3744: NEXT(last) = p;
3745: return head;
3746: }
3747:
3748: int crit_2( int dp1, int dp2 )
3749: {
3750: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3751: }
3752:
3753: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3754: {
3755: ND_pairs w,p,r,s;
3756: UINT *d;
3757:
3758: w = d1;
3759: d = LCM(w);
3760: s = NEXT(w);
3761: NEXT(w) = 0;
3762: for ( r = 0; s; s = p ) {
3763: p = NEXT(s);
3764: if ( ndl_equal(d,LCM(s)) ) {
3765: NEXT(s) = w; w = s;
3766: } else {
3767: NEXT(s) = r; r = s;
3768: }
3769: }
3770: *prest = r;
3771: return w;
3772: }
3773:
3774: NODE update_base(NODE nd,int ndp)
3775: {
3776: UINT *dl, *dln;
3777: NODE last, p, head;
3778:
3779: dl = DL(nd_psh[ndp]);
3780: for ( head = last = 0, p = nd; p; ) {
3781: dln = DL(nd_psh[(long)BDY(p)]);
3782: if ( ndl_reducible( dln, dl ) ) {
3783: p = NEXT(p);
3784: if ( last ) NEXT(last) = p;
3785: } else {
3786: if ( !last ) head = p;
3787: p = NEXT(last = p);
3788: }
3789: }
3790: head = append_one(head,ndp);
3791: return head;
3792: }
3793:
3794: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3795: {
3796: ND_pairs m,ml,p,l;
3797: UINT *lcm;
3798: int s,td,len,tlen,c,c1;
3799:
3800: if ( !(p = NEXT(m = d)) ) {
3801: *prest = p;
3802: NEXT(m) = 0;
3803: return m;
3804: }
3805: if ( !NoSugar ) {
3806: if ( nd_sugarweight ) {
3807: s = m->sugar2;
3808: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3809: if ( (p->sugar2 < s)
3810: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3811: ml = l; m = p; s = m->sugar2;
3812: }
3813: } else {
3814: s = SG(m);
3815: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3816: if ( (SG(p) < s)
3817: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3818: ml = l; m = p; s = SG(m);
3819: }
3820: }
3821: } else {
3822: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3823: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3824: ml = l; m = p; s = SG(m);
3825: }
3826: }
3827: if ( !ml ) *prest = NEXT(m);
3828: else {
3829: NEXT(ml) = NEXT(m);
3830: *prest = d;
3831: }
3832: NEXT(m) = 0;
3833: return m;
3834: }
3835:
3836: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3837: {
3838: int msugar,i;
3839: ND_pairs t,dm0,dm,dr0,dr;
3840:
3841: if ( nd_sugarweight ) {
3842: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3843: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3844: dm0 = 0; dr0 = 0;
3845: for ( i = 0, t = d; t; t = NEXT(t) )
3846: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3847: if ( dm0 ) NEXT(dm) = t;
3848: else dm0 = t;
3849: dm = t;
3850: i++;
3851: } else {
3852: if ( dr0 ) NEXT(dr) = t;
3853: else dr0 = t;
3854: dr = t;
3855: }
3856: } else {
3857: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3858: if ( SG(t) < msugar ) msugar = SG(t);
3859: dm0 = 0; dr0 = 0;
3860: for ( i = 0, t = d; t; t = NEXT(t) )
3861: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3862: if ( dm0 ) NEXT(dm) = t;
3863: else dm0 = t;
3864: dm = t;
3865: i++;
3866: } else {
3867: if ( dr0 ) NEXT(dr) = t;
3868: else dr0 = t;
3869: dr = t;
3870: }
3871: }
3872: NEXT(dm) = 0;
3873: if ( dr0 ) NEXT(dr) = 0;
3874: *prest = dr0;
3875: return dm0;
3876: }
3877:
1.28 noro 3878: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
3879: {
3880: int msugar;
3881: ND_pairs t,last;
3882:
3883: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
3884: if ( SG(t) == msugar ) last = t;
3885: *prest = last->next;
3886: last->next = 0;
3887: return d;
3888: }
3889:
1.1 noro 3890: int nd_tdeg(NDV c)
3891: {
3892: int wmax = 0;
3893: int i,len;
3894: NMV a;
3895:
3896: len = LEN(c);
3897: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3898: wmax = MAX(TD(DL(a)),wmax);
3899: return wmax;
3900: }
3901:
1.24 noro 3902: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3903: {
3904: int len;
3905: RHist r;
3906: NDV b;
3907: NODE tn;
3908: LIST l;
3909: Z iq;
3910:
3911: if ( nd_psn == nd_pslen ) {
3912: nd_pslen *= 2;
3913: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3914: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3915: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3916: nd_bound = (UINT **)
3917: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3918: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3919: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3920: }
3921: NEWRHist(r); nd_psh[nd_psn] = r;
3922: nd_ps[nd_psn] = a;
3923: if ( aq ) {
3924: nd_ps_trace[nd_psn] = aq;
3925: if ( !m ) {
3926: register_hcf(aq);
3927: } else if ( m == -2 ) {
3928: /* do nothing */
3929: } else
3930: error("ndv_newps : invalud modulus");
3931: nd_bound[nd_psn] = ndv_compute_bound(aq);
3932: #if 1
3933: SG(r) = SG(aq);
3934: #else
3935: SG(r) = nd_tdeg(aq);
3936: #endif
3937: ndl_copy(HDL(aq),DL(r));
1.24 noro 3938: r->sig = dup_sig(aq->sig);
1.1 noro 3939: } else {
3940: if ( !m ) register_hcf(a);
3941: nd_bound[nd_psn] = ndv_compute_bound(a);
3942: #if 1
3943: SG(r) = SG(a);
3944: #else
3945: SG(r) = nd_tdeg(a);
3946: #endif
3947: ndl_copy(HDL(a),DL(r));
1.24 noro 3948: r->sig = dup_sig(a->sig);
1.1 noro 3949: }
3950: if ( nd_demand ) {
3951: if ( aq ) {
3952: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3953: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3954: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3955: nd_ps_trace[nd_psn] = 0;
3956: } else {
3957: ndv_save(nd_ps[nd_psn],nd_psn);
3958: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3959: nd_ps[nd_psn] = 0;
3960: }
3961: }
3962: if ( nd_gentrace ) {
3963: /* reverse the tracelist and append it to alltracelist */
3964: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3965: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3966: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3967: }
3968: return nd_psn++;
3969: }
3970:
3971: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3972: /* return 1 if success, 0 if failure (HC(a mod p)) */
3973:
1.24 noro 3974: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3975: {
1.6 noro 3976: int i,j,td,len,max;
3977: NODE s,s0,f0,tn;
3978: UINT *d;
3979: RHist r;
3980: NDVI w;
3981: NDV a,am;
3982: union oNDC hc;
3983: NODE node;
3984: P hcp;
3985: Z iq,jq;
3986: LIST l;
3987:
3988: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3989: /* initialize the tracelist */
3990: nd_tracelist = 0;
3991:
3992: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3993: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3994: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3995: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3996: if ( !dont_sort ) {
3997: /* XXX heuristic */
1.26 noro 3998: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 3999: qsort(w,nd_psn,sizeof(struct oNDVI),
4000: (int (*)(const void *,const void *))ndvi_compare_rev);
4001: else
4002: qsort(w,nd_psn,sizeof(struct oNDVI),
4003: (int (*)(const void *,const void *))ndvi_compare);
4004: }
4005: nd_pslen = 2*nd_psn;
4006: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4007: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4008: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4009: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4010: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4011: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4012: nd_hcf = 0;
1.1 noro 4013:
1.6 noro 4014: if ( trace && nd_vc )
4015: makesubst(nd_vc,&nd_subst);
4016: else
4017: nd_subst = 0;
1.1 noro 4018:
1.6 noro 4019: if ( !nd_red )
4020: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4021: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4022: for ( i = 0; i < nd_psn; i++ ) {
4023: hc = HCU(w[i].p);
4024: if ( trace ) {
4025: if ( mod == -2 ) {
4026: /* over a large finite field */
4027: /* trace = small modulus */
4028: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4029: ndv_mod(-2,a);
4030: if ( !dont_removecont) ndv_removecont(-2,a);
4031: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4032: ndv_mod(trace,am);
4033: if ( DL_COMPARE(HDL(am),HDL(a)) )
4034: return 0;
4035: ndv_removecont(trace,am);
4036: } else {
4037: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4038: if ( !dont_removecont) ndv_removecont(0,a);
4039: register_hcf(a);
4040: am = nd_ps[i] = ndv_dup(mod,a);
4041: ndv_mod(mod,am);
4042: if ( DL_COMPARE(HDL(am),HDL(a)) )
4043: return 0;
4044: ndv_removecont(mod,am);
4045: }
4046: } else {
4047: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4048: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4049: if ( !mod ) register_hcf(a);
1.1 noro 4050: }
1.6 noro 4051: if ( nd_gentrace ) {
4052: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4053: /* exact division */
1.1 noro 4054: if ( !dont_removecont )
1.6 noro 4055: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4056: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4057: }
4058: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4059: nd_bound[i] = ndv_compute_bound(a);
4060: nd_psh[i] = r;
4061: if ( nd_demand ) {
4062: if ( trace ) {
4063: ndv_save(nd_ps_trace[i],i);
4064: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4065: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4066: nd_ps_trace[i] = 0;
4067: } else {
4068: ndv_save(nd_ps[i],i);
4069: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4070: nd_ps[i] = 0;
4071: }
1.1 noro 4072: }
1.6 noro 4073: }
1.24 noro 4074: if ( sba ) {
1.27 noro 4075: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4076: // setup signatures
1.27 noro 4077: for ( i = 0; i < nd_psn; i++ ) {
4078: SIG sig;
1.24 noro 4079:
1.27 noro 4080: NEWSIG(sig); sig->pos = i;
4081: nd_ps[i]->sig = sig;
4082: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4083: nd_psh[i]->sig = sig;
4084: if ( trace ) {
4085: nd_ps_trace[i]->sig = sig;
4086: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4087: }
4088: NEWDL(nd_sba_hm[i],nd_nvar);
4089: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
4090: }
1.29 noro 4091: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4092: for ( i = 0; i < nd_psn; i++ ) {
4093: j = nd_psh[i]->sig->pos;
4094: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4095: }
1.24 noro 4096: }
1.6 noro 4097: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4098: return 1;
1.1 noro 4099: }
4100:
4101: struct order_spec *append_block(struct order_spec *spec,
4102: int nv,int nalg,int ord);
4103:
4104: extern VECT current_dl_weight_vector_obj;
4105: static VECT prev_weight_vector_obj;
4106:
4107: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4108: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4109: {
4110: NODE alist,t,s,r0,r,arg;
4111: VL tv;
4112: P poly;
4113: DP d;
4114: Alg alpha,dp;
4115: DAlg inv,da,hc;
4116: MP m;
4117: int i,nvar,nalg,n;
4118: NumberField nf;
4119: LIST f1,f2;
4120: struct order_spec *current_spec;
4121: VECT obj,obj0;
4122: VECT tmp;
4123:
4124: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4125: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4126:
4127: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4128: NEXTNODE(alist,t); MKV(tv->v,poly);
4129: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4130: tv->v = tv->v->priv;
4131: }
4132: NEXT(t) = 0;
4133:
4134: /* simplification, making polynomials monic */
4135: setfield_dalg(alist);
4136: obj_algtodalg((Obj)f,(Obj *)&f1);
4137: for ( t = BDY(f); t; t = NEXT(t) ) {
4138: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4139: hc = (DAlg)BDY(d)->c;
4140: if ( NID(hc) == N_DA ) {
4141: invdalg(hc,&inv);
4142: for ( m = BDY(d); m; m = NEXT(m) ) {
4143: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4144: }
4145: }
4146: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4147: }
4148: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4149:
4150: /* append alg vars to the var list */
4151: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4152: NEXT(tv) = av;
4153:
4154: /* append a block to ord */
4155: *ord1p = append_block(ord,nvar,nalg,2);
4156:
4157: /* create generator list */
4158: nf = get_numberfield();
4159: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4160: MKAlg(nf->defpoly[i],dp);
4161: MKNODE(s,dp,t); t = s;
4162: }
4163: MKLIST(f1,t);
4164: *alistp = alist;
4165: algobjtorat((Obj)f1,(Obj *)f1p);
4166:
4167: /* creating a new weight vector */
4168: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4169: n = nvar+nalg+1;
4170: MKVECT(obj,n);
4171: if ( obj0 && obj0->len == nvar )
4172: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4173: else
4174: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4175: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4176: BDY(obj)[n-1] = (pointer)ONE;
4177: arg = mknode(1,obj);
4178: Pdp_set_weight(arg,&tmp);
4179: }
4180:
4181: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4182: {
4183: NODE s,t,u0,u;
4184: P p;
4185: VL tv;
4186: Obj obj;
4187: VECT tmp;
4188: NODE arg;
4189:
4190: u0 = 0;
4191: for ( t = r; t; t = NEXT(t) ) {
4192: p = (P)BDY(t);
4193: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4194: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4195: }
4196: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4197: NEXTNODE(u0,u);
4198: BDY(u) = (pointer)p;
4199: }
4200: }
4201: arg = mknode(1,prev_weight_vector_obj);
4202: Pdp_set_weight(arg,&tmp);
4203:
4204: return u0;
4205: }
4206:
4207: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4208: {
4209: VL tv,fv,vv,vc,av;
4210: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4211: int e,max,nvar,i;
4212: NDV b;
4213: int ishomo,nalg,mrank,trank,wmax,len;
4214: NMV a;
4215: Alg alpha,dp;
4216: P p,zp;
4217: Q dmy;
4218: LIST f1,f2,zpl;
4219: Obj obj;
4220: NumberField nf;
4221: struct order_spec *ord1;
4222: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4223: LIST l1,l2,l3,l4,l5;
4224: int j;
4225: Z jq,bpe,last_nonzero;
4226: int *perm;
4227: EPOS oepos;
4228: int obpe,oadv,ompos,cbpe;
1.15 noro 4229: VECT hvect;
1.1 noro 4230:
1.41 noro 4231: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4232: nd_module = 0;
4233: if ( !m && Demand ) nd_demand = 1;
4234: else nd_demand = 0;
4235: parse_nd_option(current_option);
4236:
4237: if ( DP_Multiple )
4238: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4239: #if 0
4240: ndv_alloc = 0;
4241: #endif
4242: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4243: if ( m && nd_vc )
4244: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4245: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4246: switch ( ord->id ) {
4247: case 1:
4248: if ( ord->nv != nvar )
4249: error("nd_{gr,f4} : invalid order specification");
4250: break;
4251: default:
4252: break;
4253: }
4254: nd_nalg = 0;
4255: av = 0;
4256: if ( !m ) {
4257: get_algtree((Obj)f,&av);
4258: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4259: nd_ntrans = nvar;
4260: nd_nalg = nalg;
4261: /* #i -> t#i */
4262: if ( nalg ) {
4263: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4264: ord = ord1;
4265: f = f1;
4266: }
4267: nvar += nalg;
4268: }
4269: nd_init_ord(ord);
4270: mrank = 0;
4271: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4272: for ( tv = vv; tv; tv = NEXT(tv) ) {
4273: if ( nd_module ) {
1.16 noro 4274: if ( OID(BDY(t)) == O_DPM ) {
4275: e = dpm_getdeg((DPM)BDY(t),&trank);
4276: max = MAX(e,max);
4277: mrank = MAX(mrank,trank);
4278: } else {
4279: s = BDY((LIST)BDY(t));
4280: trank = length(s);
4281: mrank = MAX(mrank,trank);
4282: for ( ; s; s = NEXT(s) ) {
4283: e = getdeg(tv->v,(P)BDY(s));
4284: max = MAX(e,max);
4285: }
1.1 noro 4286: }
4287: } else {
4288: e = getdeg(tv->v,(P)BDY(t));
4289: max = MAX(e,max);
4290: }
4291: }
4292: nd_setup_parameters(nvar,nd_nzlist?0:max);
4293: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4294: ishomo = 1;
4295: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4296: if ( nd_module ) {
1.16 noro 4297: if ( OID(BDY(t)) == O_DPM ) {
4298: Z cont;
4299: DPM zdpm;
4300:
4301: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4302: else zdpm = (DPM)BDY(t);
4303: b = (pointer)dpmtondv(m,zdpm);
4304: } else {
4305: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4306: else zpl = (LIST)BDY(t);
1.1 noro 4307: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4308: }
1.1 noro 4309: } else {
4310: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4311: else zp = (P)BDY(t);
4312: b = (pointer)ptondv(CO,vv,zp);
4313: }
4314: if ( ishomo )
4315: ishomo = ishomo && ndv_ishomo(b);
4316: if ( m ) ndv_mod(m,b);
4317: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4318: }
4319: if ( fd0 ) NEXT(fd) = 0;
4320:
4321: if ( !ishomo && homo ) {
4322: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4323: b = (NDV)BDY(t); len = LEN(b);
4324: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4325: wmax = MAX(TD(DL(a)),wmax);
4326: }
4327: homogenize_order(ord,nvar,&ord1);
4328: nd_init_ord(ord1);
4329: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4330: for ( t = fd0; t; t = NEXT(t) )
4331: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4332: }
4333:
1.24 noro 4334: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4335: if ( nd_gentrace ) {
4336: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4337: }
4338: if ( nd_splist ) {
4339: *rp = compute_splist();
4340: return;
4341: }
4342: if ( nd_check_splist ) {
4343: if ( f4 ) {
4344: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4345: else *rp = 0;
4346: } else {
4347: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4348: else *rp = 0;
4349: }
4350: return;
4351: }
4352: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4353: if ( !x ) {
4354: *rp = 0; return;
4355: }
1.15 noro 4356: if ( nd_gentrace ) {
4357: MKVECT(hvect,nd_psn);
4358: for ( i = 0; i < nd_psn; i++ )
4359: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4360: }
1.1 noro 4361: if ( !ishomo && homo ) {
4362: /* dehomogenization */
4363: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4364: nd_init_ord(ord);
4365: nd_setup_parameters(nvar,0);
4366: }
4367: nd_demand = 0;
4368: if ( nd_module && nd_intersect ) {
4369: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4370: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4371: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4372: }
4373: conv_ilist(nd_demand,0,x,0);
4374: goto FINAL;
4375: }
4376: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4377: x = ndv_reducebase(x,perm);
4378: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4379: x = ndv_reduceall(m,x);
4380: cbpe = nd_bpe;
4381: if ( nd_gentrace && !f4 ) {
4382: tl2 = nd_alltracelist; nd_alltracelist = 0;
4383: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4384: tl3 = nd_alltracelist; nd_alltracelist = 0;
4385: if ( nd_gensyz ) {
4386: nd_gb(m,0,1,1,0);
4387: tl4 = nd_alltracelist; nd_alltracelist = 0;
4388: } else tl4 = 0;
4389: }
4390: nd_bpe = cbpe;
4391: nd_setup_parameters(nd_nvar,0);
4392: FINAL:
4393: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4394: NEXTNODE(r0,r);
4395: if ( nd_module ) {
4396: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4397: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4398: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4399: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4400: }
4401: if ( r0 ) NEXT(r) = 0;
4402: if ( !m && nd_nalg )
4403: r0 = postprocess_algcoef(av,alist,r0);
4404: MKLIST(*rp,r0);
4405: if ( nd_gentrace ) {
4406: if ( f4 ) {
1.6 noro 4407: STOZ(16,bpe);
4408: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4409: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4410: } else {
4411: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4412: tl3 = reverse_node(tl3);
4413: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4414: for ( t = tl2; t; t = NEXT(t) ) {
4415: /* s = [i,[*,j,*,*],...] */
4416: s = BDY((LIST)BDY(t));
1.6 noro 4417: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4418: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4419: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4420: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4421: }
4422: }
4423: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4424: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4425: }
4426: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4427: MKLIST(l5,tl4);
1.6 noro 4428: STOZ(nd_bpe,bpe);
1.15 noro 4429: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4430: }
4431: }
4432: #if 0
4433: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4434: #endif
4435: }
4436:
1.28 noro 4437: NODE nd_sba_f4(int m,int **indp);
4438:
4439: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4440: {
4441: VL tv,fv,vv,vc,av;
4442: NODE fd,fd0,r,r0,t,x,s,xx;
4443: int e,max,nvar,i;
4444: NDV b;
4445: int ishomo,nalg,wmax,len;
4446: NMV a;
4447: P p,zp;
4448: Q dmy;
4449: struct order_spec *ord1;
4450: int j;
4451: int *perm;
4452: EPOS oepos;
4453: int obpe,oadv,ompos,cbpe;
1.29 noro 4454: struct oEGT eg0,eg1,egconv;
1.24 noro 4455:
4456: nd_module = 0;
4457: nd_demand = 0;
4458: parse_nd_option(current_option);
1.35 noro 4459: Nsamesig = 0;
1.24 noro 4460: if ( DP_Multiple )
4461: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4462: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4463: if ( m && nd_vc )
4464: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4465: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4466: switch ( ord->id ) {
4467: case 1:
4468: if ( ord->nv != nvar )
4469: error("nd_sba : invalid order specification");
4470: break;
4471: default:
4472: break;
4473: }
4474: nd_nalg = 0;
4475: nd_init_ord(ord);
4476: // for SIG comparison
4477: initd(ord);
4478: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4479: for ( tv = vv; tv; tv = NEXT(tv) ) {
4480: e = getdeg(tv->v,(P)BDY(t));
4481: max = MAX(e,max);
4482: }
4483: }
1.25 noro 4484: nd_setup_parameters(nvar,max);
1.24 noro 4485: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4486: ishomo = 1;
4487: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4488: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4489: else zp = (P)BDY(t);
4490: b = (pointer)ptondv(CO,vv,zp);
4491: if ( ishomo )
4492: ishomo = ishomo && ndv_ishomo(b);
4493: if ( m ) ndv_mod(m,b);
4494: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4495: }
4496: if ( fd0 ) NEXT(fd) = 0;
4497:
4498: if ( !ishomo && homo ) {
4499: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4500: b = (NDV)BDY(t); len = LEN(b);
4501: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4502: wmax = MAX(TD(DL(a)),wmax);
4503: }
4504: homogenize_order(ord,nvar,&ord1);
4505: nd_init_ord(ord1);
1.42 ! noro 4506: // for SIG comparison
! 4507: initd(ord1);
1.24 noro 4508: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4509: for ( t = fd0; t; t = NEXT(t) )
4510: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4511: }
4512:
1.39 noro 4513: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.28 noro 4514: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24 noro 4515: if ( !x ) {
4516: *rp = 0; return;
4517: }
4518: if ( !ishomo && homo ) {
4519: /* dehomogenization */
4520: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4521: nd_init_ord(ord);
1.42 ! noro 4522: // for SIG comparison
! 4523: initd(ord);
1.24 noro 4524: nd_setup_parameters(nvar,0);
4525: }
4526: nd_demand = 0;
4527: x = ndv_reducebase(x,perm);
4528: x = ndv_reduceall(m,x);
4529: nd_setup_parameters(nd_nvar,0);
1.29 noro 4530: get_eg(&eg0);
1.24 noro 4531: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4532: NEXTNODE(r0,r);
4533: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4534: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4535: }
4536: if ( r0 ) NEXT(r) = 0;
4537: MKLIST(*rp,r0);
1.29 noro 4538: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4539: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4540: }
4541:
1.1 noro 4542: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4543: {
4544: VL tv,fv,vv,vc,av;
4545: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4546: int e,max,nvar,i;
4547: NDV b;
4548: int ishomo,nalg;
4549: Alg alpha,dp;
4550: P p,zp;
4551: Q dmy;
4552: LIST f1,f2;
4553: Obj obj;
4554: NumberField nf;
4555: struct order_spec *ord1;
4556: int *perm;
4557:
4558: parse_nd_option(current_option);
4559: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4560: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4561: switch ( ord->id ) {
4562: case 1:
4563: if ( ord->nv != nvar )
4564: error("nd_check : invalid order specification");
4565: break;
4566: default:
4567: break;
4568: }
4569: nd_nalg = 0;
4570: av = 0;
4571: if ( !m ) {
4572: get_algtree((Obj)f,&av);
4573: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4574: nd_ntrans = nvar;
4575: nd_nalg = nalg;
4576: /* #i -> t#i */
4577: if ( nalg ) {
4578: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4579: ord = ord1;
4580: f = f1;
4581: }
4582: nvar += nalg;
4583: }
4584: nd_init_ord(ord);
4585: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4586: for ( tv = vv; tv; tv = NEXT(tv) ) {
4587: e = getdeg(tv->v,(P)BDY(t));
4588: max = MAX(e,max);
4589: }
4590: nd_setup_parameters(nvar,max);
4591: ishomo = 1;
4592: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4593: ptozp((P)BDY(t),1,&dmy,&zp);
4594: b = (pointer)ptondv(CO,vv,zp);
4595: if ( ishomo )
4596: ishomo = ishomo && ndv_ishomo(b);
4597: if ( m ) ndv_mod(m,b);
4598: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4599: }
4600: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4601: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4602: for ( x = 0, i = 0; i < nd_psn; i++ )
4603: x = update_base(x,i);
4604: if ( do_check ) {
4605: x = nd_gb(m,ishomo,1,0,&perm);
4606: if ( !x ) {
4607: *rp = 0;
4608: return;
4609: }
4610: } else {
4611: #if 0
4612: /* bug ? */
4613: for ( t = x; t; t = NEXT(t) )
4614: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4615: #else
4616: conv_ilist(0,0,x,&perm);
4617: #endif
4618: }
4619: x = ndv_reducebase(x,perm);
4620: x = ndv_reduceall(m,x);
4621: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4622: NEXTNODE(r0,r);
4623: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4624: }
4625: if ( r0 ) NEXT(r) = 0;
4626: if ( !m && nd_nalg )
4627: r0 = postprocess_algcoef(av,alist,r0);
4628: MKLIST(*rp,r0);
4629: }
4630:
4631: NDV recompute_trace(NODE trace,NDV *p,int m);
4632: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4633:
4634: NDV recompute_trace(NODE ti,NDV *p,int mod)
4635: {
4636: int c,c1,c2,i;
4637: NM mul,m,tail;
4638: ND d,r,rm;
4639: NODE sj;
4640: NDV red;
4641: Obj mj;
4642:
4643: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4644: CM(mul) = 1;
4645: tail = 0;
4646: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4647: sj = BDY((LIST)BDY(ti));
4648: if ( ARG0(sj) ) {
1.6 noro 4649: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4650: mj = (Obj)ARG2(sj);
4651: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4652: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4653: rm = ndv_mul_nm(mod,mul,red);
4654: if ( !r ) r = rm;
4655: else {
4656: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4657: if ( d ) {
4658: NEXT(tail) = m; tail = m; LEN(d)++;
4659: } else {
4660: MKND(nd_nvar,m,1,d); tail = BDY(d);
4661: }
4662: }
4663: if ( !m ) return 0; /* failure */
4664: else {
4665: BDY(r) = m;
4666: if ( mod > 0 || mod == -1 ) {
4667: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4668: DMAR(c1,c2,0,mod,c);
4669: nd_mul_c(mod,rm,c);
4670: } else {
4671: Z t,u;
4672:
4673: chsgnlf(HCZ(r),&t);
4674: divlf(t,HCZ(rm),&u);
4675: nd_mul_c_lf(rm,u);
4676: }
4677: r = nd_add(mod,r,rm);
4678: }
4679: }
4680: }
4681: }
4682: if ( tail ) NEXT(tail) = 0;
4683: d = nd_add(mod,d,r);
4684: nd_mul_c(mod,d,invm(HCM(d),mod));
4685: return ndtondv(mod,d);
4686: }
4687:
4688: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4689: {
4690: VL tv,fv,vv,vc,av;
4691: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4692: int e,max,nvar,i;
4693: NDV b;
4694: int ishomo,nalg;
4695: Alg alpha,dp;
4696: P p,zp;
4697: Q dmy;
4698: LIST f1,f2;
4699: Obj obj;
4700: NumberField nf;
4701: struct order_spec *ord1;
4702: NODE permtrace,intred,ind,perm,trace,ti;
4703: int len,n,j;
4704: NDV *db,*pb;
4705:
4706: parse_nd_option(current_option);
4707: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4708: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4709: switch ( ord->id ) {
4710: case 1:
4711: if ( ord->nv != nvar )
4712: error("nd_check : invalid order specification");
4713: break;
4714: default:
4715: break;
4716: }
4717: nd_init_ord(ord);
1.6 noro 4718: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4719: nd_setup_parameters(nvar,0);
4720:
4721: len = length(BDY(f));
4722: db = (NDV *)MALLOC(len*sizeof(NDV *));
4723: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4724: ptozp((P)BDY(t),1,&dmy,&zp);
4725: b = ptondv(CO,vv,zp);
4726: ndv_mod(m,b);
4727: ndv_mul_c(m,b,invm(HCM(b),m));
4728: db[i] = b;
4729: }
4730:
4731: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4732: intred = BDY((LIST)ARG3(BDY(tlist)));
4733: ind = BDY((LIST)ARG4(BDY(tlist)));
4734: perm = BDY((LIST)ARG0(permtrace));
4735: trace = NEXT(permtrace);
4736:
4737: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4738: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4739: if ( j > i ) i = j;
4740: }
4741: n = i+1;
4742: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4743: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4744: ti = BDY((LIST)BDY(t));
1.6 noro 4745: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4746: }
4747: for ( t = trace; t; t = NEXT(t) ) {
4748: ti = BDY((LIST)BDY(t));
1.6 noro 4749: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4750: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4751: if ( DP_Print ) {
4752: fprintf(asir_out,"."); fflush(asir_out);
4753: }
4754: }
4755: for ( t = intred; t; t = NEXT(t) ) {
4756: ti = BDY((LIST)BDY(t));
1.6 noro 4757: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4758: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4759: if ( DP_Print ) {
4760: fprintf(asir_out,"*"); fflush(asir_out);
4761: }
4762: }
4763: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4764: NEXTNODE(r0,r);
1.6 noro 4765: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4766: ndv_mul_c(m,b,invm(HCM(b),m));
4767: #if 0
1.6 noro 4768: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4769: #else
1.6 noro 4770: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4771: #endif
4772: }
4773: if ( r0 ) NEXT(r) = 0;
4774: MKLIST(*rp,r0);
4775: if ( DP_Print ) fprintf(asir_out,"\n");
4776: }
4777:
1.16 noro 4778: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1 noro 4779: {
4780: VL tv,fv,vv,vc,av;
4781: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4782: int m,nocheck,nvar,mindex,e,max;
4783: NDV c;
4784: NMV a;
4785: P p,zp;
4786: Q dmy;
4787: EPOS oepos;
4788: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4789: Alg alpha,dp;
4790: P poly;
4791: LIST f1,f2,zpl;
4792: Obj obj;
4793: NumberField nf;
4794: struct order_spec *ord1;
4795: struct oEGT eg_check,eg0,eg1;
4796: NODE tr,tl1,tl2,tl3,tl4;
4797: LIST l1,l2,l3,l4,l5;
4798: int *perm;
4799: int j,ret;
4800: Z jq,bpe;
1.15 noro 4801: VECT hvect;
1.1 noro 4802:
1.41 noro 4803: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4804: nd_module = 0;
4805: nd_lf = 0;
4806: parse_nd_option(current_option);
4807: if ( nd_lf ) {
4808: if ( f4 )
4809: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4810: else
4811: error("nd_gr_trace is not implemented yet over a large finite field");
4812: return;
4813: }
4814: if ( DP_Multiple )
4815: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4816:
4817: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4818: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4819: switch ( ord->id ) {
4820: case 1:
4821: if ( ord->nv != nvar )
4822: error("nd_gr_trace : invalid order specification");
4823: break;
4824: default:
4825: break;
4826: }
4827:
4828: get_algtree((Obj)f,&av);
4829: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4830: nd_ntrans = nvar;
4831: nd_nalg = nalg;
4832: /* #i -> t#i */
4833: if ( nalg ) {
4834: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4835: ord = ord1;
4836: f = f1;
4837: }
4838: nvar += nalg;
4839:
4840: nocheck = 0;
4841: mindex = 0;
4842:
4843: if ( Demand ) nd_demand = 1;
4844: else nd_demand = 0;
4845:
4846: /* setup modulus */
4847: if ( trace < 0 ) {
4848: trace = -trace;
4849: nocheck = 1;
4850: }
4851: m = trace > 1 ? trace : get_lprime(mindex);
4852: nd_init_ord(ord);
4853: mrank = 0;
4854: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4855: for ( tv = vv; tv; tv = NEXT(tv) ) {
4856: if ( nd_module ) {
1.16 noro 4857: if ( OID(BDY(t)) == O_DPM ) {
4858: e = dpm_getdeg((DPM)BDY(t),&trank);
4859: max = MAX(e,max);
4860: mrank = MAX(mrank,trank);
4861: } else {
1.1 noro 4862: s = BDY((LIST)BDY(t));
4863: trank = length(s);
4864: mrank = MAX(mrank,trank);
4865: for ( ; s; s = NEXT(s) ) {
4866: e = getdeg(tv->v,(P)BDY(s));
4867: max = MAX(e,max);
4868: }
1.16 noro 4869: }
1.1 noro 4870: } else {
4871: e = getdeg(tv->v,(P)BDY(t));
4872: max = MAX(e,max);
4873: }
4874: }
4875: nd_setup_parameters(nvar,max);
4876: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4877: ishomo = 1;
4878: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4879: if ( nd_module ) {
1.16 noro 4880: if ( OID(BDY(t)) == O_DPM ) {
4881: Z cont;
4882: DPM zdpm;
4883:
1.17 noro 4884: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4885: else zdpm = (DPM)BDY(t);
4886: c = (pointer)dpmtondv(m,zdpm);
4887: } else {
4888: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4889: else zpl = (LIST)BDY(t);
1.1 noro 4890: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4891: }
1.1 noro 4892: } else {
1.16 noro 4893: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4894: else zp = (P)BDY(t);
4895: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4896: }
4897: if ( ishomo )
4898: ishomo = ishomo && ndv_ishomo(c);
4899: if ( c ) {
4900: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4901: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4902: }
4903: }
4904: if ( in0 ) NEXT(in) = 0;
4905: if ( fd0 ) NEXT(fd) = 0;
4906: if ( !ishomo && homo ) {
4907: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4908: c = (NDV)BDY(t); len = LEN(c);
4909: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4910: wmax = MAX(TD(DL(a)),wmax);
4911: }
4912: homogenize_order(ord,nvar,&ord1);
4913: nd_init_ord(ord1);
4914: nd_setup_parameters(nvar+1,wmax);
4915: for ( t = fd0; t; t = NEXT(t) )
4916: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4917: }
4918: if ( MaxDeg > 0 ) nocheck = 1;
4919: while ( 1 ) {
4920: tl1 = tl2 = tl3 = tl4 = 0;
4921: if ( Demand )
4922: nd_demand = 1;
1.24 noro 4923: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4924: if ( nd_gentrace ) {
4925: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4926: }
4927: if ( ret )
4928: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4929: if ( !ret || !cand ) {
4930: /* failure */
4931: if ( trace > 1 ) { *rp = 0; return; }
4932: else m = get_lprime(++mindex);
4933: continue;
4934: }
1.15 noro 4935: if ( nd_gentrace ) {
4936: MKVECT(hvect,nd_psn);
4937: for ( i = 0; i < nd_psn; i++ )
4938: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4939: }
1.1 noro 4940: if ( !ishomo && homo ) {
4941: /* dehomogenization */
4942: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4943: nd_init_ord(ord);
4944: nd_setup_parameters(nvar,0);
4945: }
4946: nd_demand = 0;
4947: cand = ndv_reducebase(cand,perm);
4948: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4949: cand = ndv_reduceall(0,cand);
4950: cbpe = nd_bpe;
4951: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4952: get_eg(&eg0);
4953: if ( nocheck )
4954: break;
4955: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4956: if ( nd_gentrace ) {
4957: tl3 = nd_alltracelist; nd_alltracelist = 0;
4958: } else tl3 = 0;
4959: /* gbcheck : cand is a GB of Id(cand) ? */
4960: if ( nd_vc || nd_gentrace || nd_gensyz )
4961: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4962: else
4963: ret = nd_f4(0,1,0)!=0;
4964: if ( nd_gentrace && nd_gensyz ) {
4965: tl4 = nd_alltracelist; nd_alltracelist = 0;
4966: } else tl4 = 0;
4967: }
4968: if ( ret ) break;
4969: else if ( trace > 1 ) {
4970: /* failure */
4971: *rp = 0; return;
4972: } else {
4973: /* try the next modulus */
4974: m = get_lprime(++mindex);
4975: /* reset the parameters */
4976: if ( !ishomo && homo ) {
4977: nd_init_ord(ord1);
4978: nd_setup_parameters(nvar+1,wmax);
4979: } else {
4980: nd_init_ord(ord);
4981: nd_setup_parameters(nvar,max);
4982: }
4983: }
4984: }
4985: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4986: if ( DP_Print )
1.6 noro 4987: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4988: /* dp->p */
4989: nd_bpe = cbpe;
4990: nd_setup_parameters(nd_nvar,0);
4991: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4992: if ( nd_module ) {
1.17 noro 4993: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4994: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4995: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4996: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4997: }
4998: if ( nd_nalg )
4999: cand = postprocess_algcoef(av,alist,cand);
5000: MKLIST(*rp,cand);
5001: if ( nd_gentrace ) {
5002: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5003: tl3 = reverse_node(tl3);
5004: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5005: for ( t = tl2; t; t = NEXT(t) ) {
5006: /* s = [i,[*,j,*,*],...] */
5007: s = BDY((LIST)BDY(t));
1.6 noro 5008: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5009: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5010: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5011: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5012: }
5013: }
5014: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5015: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5016: }
5017: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5018: MKLIST(l5,tl4);
1.6 noro 5019: STOZ(nd_bpe,bpe);
1.15 noro 5020: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5021: }
5022: }
5023:
5024: /* XXX : module element is not considered */
5025:
5026: void dltondl(int n,DL dl,UINT *r)
5027: {
5028: UINT *d;
5029: int i,j,l,s,ord_l;
5030: struct order_pair *op;
5031:
5032: d = (unsigned int *)dl->d;
5033: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5034: if ( nd_blockmask ) {
5035: l = nd_blockmask->n;
5036: op = nd_blockmask->order_pair;
5037: for ( j = 0, s = 0; j < l; j++ ) {
5038: ord_l = op[j].length;
5039: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5040: }
5041: TD(r) = ndl_weight(r);
5042: ndl_weight_mask(r);
5043: } else {
5044: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5045: TD(r) = ndl_weight(r);
5046: }
5047: }
5048:
5049: DL ndltodl(int n,UINT *ndl)
5050: {
5051: DL dl;
5052: int *d;
5053: int i,j,l,s,ord_l;
5054: struct order_pair *op;
5055:
1.33 noro 5056: NEWDL_NOINIT(dl,n);
1.1 noro 5057: dl->td = TD(ndl);
5058: d = dl->d;
5059: if ( nd_blockmask ) {
5060: l = nd_blockmask->n;
5061: op = nd_blockmask->order_pair;
5062: for ( j = 0, s = 0; j < l; j++ ) {
5063: ord_l = op[j].length;
5064: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5065: }
5066: } else {
5067: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5068: }
5069: return dl;
5070: }
5071:
1.24 noro 5072: void _ndltodl(UINT *ndl,DL dl)
5073: {
5074: int *d;
5075: int i,j,l,s,ord_l,n;
5076: struct order_pair *op;
5077:
5078: n = nd_nvar;
5079: dl->td = TD(ndl);
5080: d = dl->d;
5081: if ( nd_blockmask ) {
5082: l = nd_blockmask->n;
5083: op = nd_blockmask->order_pair;
5084: for ( j = 0, s = 0; j < l; j++ ) {
5085: ord_l = op[j].length;
5086: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5087: }
5088: } else {
5089: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5090: }
5091: }
5092:
1.1 noro 5093: void nmtodp(int mod,NM m,DP *r)
5094: {
5095: DP dp;
5096: MP mr;
5097:
5098: NEWMP(mr);
5099: mr->dl = ndltodl(nd_nvar,DL(m));
5100: mr->c = (Obj)ndctop(mod,m->c);
5101: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5102: *r = dp;
5103: }
5104:
1.15 noro 5105: void ndltodp(UINT *d,DP *r)
5106: {
5107: DP dp;
5108: MP mr;
5109:
5110: NEWMP(mr);
5111: mr->dl = ndltodl(nd_nvar,d);
5112: mr->c = (Obj)ONE;
5113: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5114: *r = dp;
5115: }
5116:
1.1 noro 5117: void ndl_print(UINT *dl)
5118: {
5119: int n;
5120: int i,j,l,ord_l,s,s0;
5121: struct order_pair *op;
5122:
5123: n = nd_nvar;
5124: printf("<<");
5125: if ( nd_blockmask ) {
5126: l = nd_blockmask->n;
5127: op = nd_blockmask->order_pair;
5128: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5129: ord_l = op[j].length;
5130: for ( i = 0; i < ord_l; i++, s++ )
5131: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5132: }
5133: } else {
5134: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5135: }
5136: printf(">>");
5137: if ( nd_module && MPOS(dl) )
5138: printf("*e%d",MPOS(dl));
5139: }
5140:
5141: void nd_print(ND p)
5142: {
5143: NM m;
5144:
5145: if ( !p )
5146: printf("0\n");
5147: else {
5148: for ( m = BDY(p); m; m = NEXT(m) ) {
5149: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5150: else printf("+%ld*",CM(m));
1.1 noro 5151: ndl_print(DL(m));
5152: }
5153: printf("\n");
5154: }
5155: }
5156:
5157: void nd_print_q(ND p)
5158: {
5159: NM m;
5160:
5161: if ( !p )
5162: printf("0\n");
5163: else {
5164: for ( m = BDY(p); m; m = NEXT(m) ) {
5165: printf("+");
1.6 noro 5166: printexpr(CO,(Obj)CZ(m));
1.1 noro 5167: printf("*");
5168: ndl_print(DL(m));
5169: }
5170: printf("\n");
5171: }
5172: }
5173:
5174: void ndp_print(ND_pairs d)
5175: {
5176: ND_pairs t;
5177:
5178: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5179: printf("\n");
5180: }
5181:
5182: void nd_removecont(int mod,ND p)
5183: {
5184: int i,n;
5185: Z *w;
5186: NM m;
5187: struct oVECT v;
5188:
5189: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5190: else if ( mod == -2 ) {
5191: Z inv;
5192: divlf(ONE,HCZ(p),&inv);
5193: nd_mul_c_lf(p,inv);
5194: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5195: else {
5196: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5197: w = (Z *)MALLOC(n*sizeof(Q));
5198: v.len = n;
5199: v.body = (pointer *)w;
1.6 noro 5200: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5201: removecont_array((P *)w,n,1);
1.6 noro 5202: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5203: }
5204: }
5205:
5206: void nd_removecont2(ND p1,ND p2)
5207: {
5208: int i,n1,n2,n;
5209: Z *w;
5210: NM m;
5211: struct oVECT v;
5212:
5213: n1 = nd_length(p1);
5214: n2 = nd_length(p2);
5215: n = n1+n2;
5216: w = (Z *)MALLOC(n*sizeof(Q));
5217: v.len = n;
5218: v.body = (pointer *)w;
5219: i = 0;
5220: if ( p1 )
1.6 noro 5221: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5222: if ( p2 )
1.6 noro 5223: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5224: removecont_array((P *)w,n,1);
5225: i = 0;
5226: if ( p1 )
1.6 noro 5227: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5228: if ( p2 )
1.6 noro 5229: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5230: }
5231:
5232: void ndv_removecont(int mod,NDV p)
5233: {
5234: int i,len,all_p;
5235: Z *c;
5236: P *w;
5237: Z dvr,t;
5238: P g,cont,tp;
5239: NMV m;
5240:
5241: if ( mod == -1 )
5242: ndv_mul_c(mod,p,_invsf(HCM(p)));
5243: else if ( mod == -2 ) {
5244: Z inv;
5245: divlf(ONE,HCZ(p),&inv);
5246: ndv_mul_c_lf(p,inv);
5247: } else if ( mod )
5248: ndv_mul_c(mod,p,invm(HCM(p),mod));
5249: else {
5250: len = p->len;
5251: w = (P *)MALLOC(len*sizeof(P));
5252: c = (Z *)MALLOC(len*sizeof(Q));
5253: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5254: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5255: all_p = all_p && !NUM(w[i]);
5256: }
5257: if ( all_p ) {
5258: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5259: mulp(nd_vc,(P)dvr,g,&cont);
5260: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5261: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5262: }
5263: } else {
5264: sortbynm((Q *)c,len);
5265: qltozl((Q *)c,len,&dvr);
5266: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5267: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5268: }
5269: }
5270: }
5271: }
5272:
5273: /* koko */
5274:
5275: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5276: {
5277: int len,i,max;
5278: NMV m,mr0,mr,t;
5279:
5280: len = p->len;
1.14 noro 5281: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5282: max = MAX(max,TD(DL(m)));
5283: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5284: m = (NMV)((char *)mr0+(len-1)*oadv);
5285: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5286: t = (NMV)MALLOC(nmv_adv);
5287: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5288: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5289: CZ(mr) = CZ(m);
1.1 noro 5290: ndl_copy(DL(t),DL(mr));
5291: }
5292: NV(p)++;
5293: BDY(p) = mr0;
5294: }
5295:
5296: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5297: {
5298: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5299: int pos;
5300: Q *w;
5301: Q dvr,t;
5302: NMV m,r;
5303:
5304: len = p->len;
5305: newnvar = nd_nvar-1;
5306: newexporigin = nd_get_exporigin(ord);
5307: if ( nd_module ) newmpos = newexporigin-1;
5308: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5309: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5310: ndl_dehomogenize(DL(m));
5311: if ( newwpd != nd_wpd ) {
5312: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5313: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5314: CZ(r) = CZ(m);
1.1 noro 5315: if ( nd_module ) pos = MPOS(DL(m));
5316: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5317: adj = nd_exporigin-newexporigin;
5318: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5319: if ( nd_module ) {
5320: DL(r)[newmpos] = pos;
5321: }
5322: }
5323: }
5324: NV(p)--;
5325: }
5326:
5327: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5328: {
5329: int i;
5330: P *tpl,*tpl1;
5331: NODE l;
5332: P h,gcd,t;
5333:
5334: tpl = (P *)MALLOC(m*sizeof(P));
5335: tpl1 = (P *)MALLOC(m*sizeof(P));
5336: bcopy(pl,tpl,m*sizeof(P));
5337: gcd = (P)ONE;
5338: for ( l = nd_hcf; l; l = NEXT(l) ) {
5339: h = (P)BDY(l);
5340: while ( 1 ) {
5341: for ( i = 0; i < m; i++ )
5342: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5343: break;
5344: if ( i == m ) {
5345: bcopy(tpl1,tpl,m*sizeof(P));
5346: mulp(vl,gcd,h,&t); gcd = t;
5347: } else
5348: break;
5349: }
5350: }
5351: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5352: if ( full ) {
5353: heu_nezgcdnpz(vl,tpl,m,&t);
5354: mulp(vl,gcd,t,pr);
5355: } else
5356: *pr = gcd;
5357: }
5358:
5359: void removecont_array(P *p,int n,int full)
5360: {
5361: int all_p,all_q,i;
5362: Z *c;
5363: P *w;
5364: P t,s;
5365:
5366: for ( all_q = 1, i = 0; i < n; i++ )
5367: all_q = all_q && NUM(p[i]);
5368: if ( all_q ) {
5369: removecont_array_q((Z *)p,n);
5370: } else {
5371: c = (Z *)MALLOC(n*sizeof(Z));
5372: w = (P *)MALLOC(n*sizeof(P));
5373: for ( i = 0; i < n; i++ ) {
5374: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5375: }
5376: removecont_array_q(c,n);
5377: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5378: for ( i = 0; i < n; i++ ) {
5379: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5380: }
5381: }
5382: }
5383:
5384: /* c is an int array */
5385:
5386: void removecont_array_q(Z *c,int n)
5387: {
5388: struct oVECT v;
5389: Z d0,d1,a,u,u1,gcd;
5390: int i,j;
5391: Z *q,*r;
5392:
5393: q = (Z *)MALLOC(n*sizeof(Z));
5394: r = (Z *)MALLOC(n*sizeof(Z));
5395: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5396: gcdvz_estimate(&v,&d0);
5397: for ( i = 0; i < n; i++ ) {
5398: divqrz(c[i],d0,&q[i],&r[i]);
5399: }
5400: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5401: if ( i < n ) {
5402: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5403: gcdvz(&v,&d1);
5404: gcdz(d0,d1,&gcd);
1.6 noro 5405: /* exact division */
5406: divsz(d0,gcd,&a);
1.1 noro 5407: for ( i = 0; i < n; i++ ) {
5408: mulz(a,q[i],&u);
5409: if ( r[i] ) {
1.6 noro 5410: /* exact division */
5411: divsz(r[i],gcd,&u1);
1.1 noro 5412: addz(u,u1,&q[i]);
5413: } else
5414: q[i] = u;
5415: }
5416: }
5417: for ( i = 0; i < n; i++ ) c[i] = q[i];
5418: }
5419:
1.4 noro 5420: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5421:
5422: void mpz_removecont_array(mpz_t *c,int n)
5423: {
5424: mpz_t d0,a,u,u1,gcd;
5425: int i,j;
1.13 noro 5426: static mpz_t *q,*r;
5427: static int c_len = 0;
1.4 noro 5428:
5429: for ( i = 0; i < n; i++ )
5430: if ( mpz_sgn(c[i]) ) break;
5431: if ( i == n ) return;
5432: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5433: if ( n > c_len ) {
5434: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5435: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5436: c_len = n;
5437: }
1.4 noro 5438: for ( i = 0; i < n; i++ ) {
5439: mpz_init(q[i]); mpz_init(r[i]);
5440: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5441: }
5442: for ( i = 0; i < n; i++ )
5443: if ( mpz_sgn(r[i]) ) break;
5444: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5445: if ( i < n ) {
5446: mpz_gcd(gcd,d0,r[i]);
5447: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5448: mpz_div(a,d0,gcd);
5449: for ( i = 0; i < n; i++ ) {
5450: mpz_mul(u,a,q[i]);
5451: if ( mpz_sgn(r[i]) ) {
5452: mpz_div(u1,r[i],gcd);
5453: mpz_add(q[i],u,u1);
5454: } else
5455: mpz_set(q[i],u);
5456: }
5457: }
5458: for ( i = 0; i < n; i++ )
5459: mpz_set(c[i],q[i]);
5460: }
5461:
1.1 noro 5462: void nd_mul_c(int mod,ND p,int mul)
5463: {
5464: NM m;
5465: int c,c1;
5466:
5467: if ( !p ) return;
5468: if ( mul == 1 ) return;
5469: if ( mod == -1 )
5470: for ( m = BDY(p); m; m = NEXT(m) )
5471: CM(m) = _mulsf(CM(m),mul);
5472: else
5473: for ( m = BDY(p); m; m = NEXT(m) ) {
5474: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5475: }
5476: }
5477:
5478: void nd_mul_c_lf(ND p,Z mul)
5479: {
5480: NM m;
5481: Z c;
5482:
5483: if ( !p ) return;
5484: if ( UNIZ(mul) ) return;
5485: for ( m = BDY(p); m; m = NEXT(m) ) {
5486: mullf(CZ(m),mul,&c); CZ(m) = c;
5487: }
5488: }
5489:
5490: void nd_mul_c_q(ND p,P mul)
5491: {
5492: NM m;
5493: P c;
5494:
5495: if ( !p ) return;
5496: if ( UNIQ(mul) ) return;
5497: for ( m = BDY(p); m; m = NEXT(m) ) {
5498: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5499: }
5500: }
5501:
5502: void nd_mul_c_p(VL vl,ND p,P mul)
5503: {
5504: NM m;
5505: P c;
5506:
5507: if ( !p ) return;
5508: for ( m = BDY(p); m; m = NEXT(m) ) {
5509: mulp(vl,CP(m),mul,&c); CP(m) = c;
5510: }
5511: }
5512:
5513: void nd_free(ND p)
5514: {
5515: NM t,s;
5516:
5517: if ( !p ) return;
5518: t = BDY(p);
5519: while ( t ) {
5520: s = NEXT(t);
5521: FREENM(t);
5522: t = s;
5523: }
5524: FREEND(p);
5525: }
5526:
5527: void ndv_free(NDV p)
5528: {
5529: GCFREE(BDY(p));
5530: }
5531:
5532: void nd_append_red(UINT *d,int i)
5533: {
5534: RHist m,m0;
5535: int h;
5536:
5537: NEWRHist(m);
5538: h = ndl_hash_value(d);
5539: m->index = i;
5540: ndl_copy(d,DL(m));
5541: NEXT(m) = nd_red[h];
5542: nd_red[h] = m;
5543: }
5544:
5545: UINT *ndv_compute_bound(NDV p)
5546: {
5547: UINT *d1,*d2,*t;
5548: UINT u;
5549: int i,j,k,l,len,ind;
5550: NMV m;
5551:
5552: if ( !p )
5553: return 0;
5554: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5555: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5556: len = LEN(p);
5557: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5558: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5559: ndl_max(DL(m),d1,d2);
5560: t = d1; d1 = d2; d2 = t;
5561: }
5562: l = nd_nvar+31;
5563: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5564: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5565: u = d1[i];
5566: k = (nd_epw-1)*nd_bpe;
5567: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5568: t[ind] = (u>>k)&nd_mask0;
5569: }
5570: for ( ; ind < l; ind++ ) t[ind] = 0;
5571: return t;
5572: }
5573:
5574: UINT *nd_compute_bound(ND p)
5575: {
5576: UINT *d1,*d2,*t;
5577: UINT u;
5578: int i,j,k,l,len,ind;
5579: NM m;
5580:
5581: if ( !p )
5582: return 0;
5583: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5584: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5585: len = LEN(p);
5586: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5587: for ( m = NEXT(m); m; m = NEXT(m) ) {
5588: ndl_lcm(DL(m),d1,d2);
5589: t = d1; d1 = d2; d2 = t;
5590: }
5591: l = nd_nvar+31;
5592: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5593: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5594: u = d1[i];
5595: k = (nd_epw-1)*nd_bpe;
5596: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5597: t[ind] = (u>>k)&nd_mask0;
5598: }
5599: for ( ; ind < l; ind++ ) t[ind] = 0;
5600: return t;
5601: }
5602:
5603: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5604: /* of a term. In this case we need additional 1 word. */
5605:
5606: int nd_get_exporigin(struct order_spec *ord)
5607: {
5608: switch ( ord->id ) {
1.21 noro 5609: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5610: return 1+nd_module;
5611: case 1: case 257:
5612: /* block order */
5613: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5614: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5615: return ord->ord.block.length+1+nd_module;
5616: case 3: case 259:
5617: #if 0
5618: error("nd_get_exporigin : composite order is not supported yet.");
5619: #else
5620: return 1+nd_module;
5621: #endif
5622: default:
5623: error("nd_get_exporigin : ivalid argument.");
5624: return 0;
5625: }
5626: }
5627:
5628: void nd_setup_parameters(int nvar,int max) {
5629: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5630: struct order_pair *op;
5631:
5632: nd_nvar = nvar;
5633: if ( max ) {
5634: /* XXX */
5635: if ( do_weyl ) nd_bpe = 32;
5636: else if ( max < 2 ) nd_bpe = 1;
5637: else if ( max < 4 ) nd_bpe = 2;
5638: else if ( max < 8 ) nd_bpe = 3;
5639: else if ( max < 16 ) nd_bpe = 4;
5640: else if ( max < 32 ) nd_bpe = 5;
5641: else if ( max < 64 ) nd_bpe = 6;
5642: else if ( max < 256 ) nd_bpe = 8;
5643: else if ( max < 1024 ) nd_bpe = 10;
5644: else if ( max < 65536 ) nd_bpe = 16;
5645: else nd_bpe = 32;
5646: }
5647: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5648: UINT t;
5649: int st;
5650: int *v;
5651: /* t = max(weights) */
5652: t = 0;
5653: if ( current_dl_weight_vector )
5654: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5655: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5656: if ( t < st ) t = st;
5657: }
5658: if ( nd_matrix )
5659: for ( i = 0; i < nd_matrix_len; i++ )
5660: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5661: if ( (st=v[j]) < 0 ) st = -st;
5662: if ( t < st ) t = st;
5663: }
5664: /* i = bitsize of t */
5665: for ( i = 0; t; t >>=1, i++ );
5666: /* i += bitsize of nd_nvar */
5667: for ( t = nd_nvar; t; t >>=1, i++);
5668: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5669: if ( (nd_bpe+i) >= 31 )
5670: error("nd_setup_parameters : too large weight");
5671: }
5672: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5673: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5674: nd_exporigin = nd_get_exporigin(nd_ord);
5675: wpd = nd_exporigin+elen;
5676: if ( nd_module )
5677: nd_mpos = nd_exporigin-1;
5678: else
5679: nd_mpos = -1;
5680: if ( wpd != nd_wpd ) {
5681: nd_free_private_storage();
5682: nd_wpd = wpd;
5683: }
5684: if ( nd_bpe < 32 ) {
5685: nd_mask0 = (1<<nd_bpe)-1;
5686: } else {
5687: nd_mask0 = 0xffffffff;
5688: }
5689: bzero(nd_mask,sizeof(nd_mask));
5690: nd_mask1 = 0;
5691: for ( i = 0; i < nd_epw; i++ ) {
5692: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5693: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5694: }
5695: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5696: nd_epos = nd_create_epos(nd_ord);
5697: nd_blockmask = nd_create_blockmask(nd_ord);
5698: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5699: }
5700:
5701: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5702: {
5703: int i,obpe,oadv,h;
5704: static NM prev_nm_free_list;
5705: static ND_pairs prev_ndp_free_list;
5706: RHist mr0,mr;
5707: RHist r;
5708: RHist *old_red;
5709: ND_pairs s0,s,t;
5710: EPOS oepos;
5711:
5712: obpe = nd_bpe;
5713: oadv = nmv_adv;
5714: oepos = nd_epos;
5715: if ( obpe < 2 ) nd_bpe = 2;
5716: else if ( obpe < 3 ) nd_bpe = 3;
5717: else if ( obpe < 4 ) nd_bpe = 4;
5718: else if ( obpe < 5 ) nd_bpe = 5;
5719: else if ( obpe < 6 ) nd_bpe = 6;
5720: else if ( obpe < 8 ) nd_bpe = 8;
5721: else if ( obpe < 10 ) nd_bpe = 10;
5722: else if ( obpe < 16 ) nd_bpe = 16;
5723: else if ( obpe < 32 ) nd_bpe = 32;
5724: else error("nd_reconstruct : exponent too large");
5725:
5726: nd_setup_parameters(nd_nvar,0);
5727: prev_nm_free_list = _nm_free_list;
5728: prev_ndp_free_list = _ndp_free_list;
5729: _nm_free_list = 0;
5730: _ndp_free_list = 0;
5731: for ( i = nd_psn-1; i >= 0; i-- ) {
5732: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5733: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5734: }
5735: if ( trace )
5736: for ( i = nd_psn-1; i >= 0; i-- ) {
5737: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5738: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5739: }
5740: s0 = 0;
5741: for ( t = d; t; t = NEXT(t) ) {
5742: NEXTND_pairs(s0,s);
5743: s->i1 = t->i1;
5744: s->i2 = t->i2;
1.24 noro 5745: s->sig = t->sig;
1.1 noro 5746: SG(s) = SG(t);
5747: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5748: }
5749:
5750: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5751: for ( i = 0; i < REDTAB_LEN; i++ ) {
5752: old_red[i] = nd_red[i];
5753: nd_red[i] = 0;
5754: }
5755: for ( i = 0; i < REDTAB_LEN; i++ )
5756: for ( r = old_red[i]; r; r = NEXT(r) ) {
5757: NEWRHist(mr);
5758: mr->index = r->index;
5759: SG(mr) = SG(r);
5760: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5761: h = ndl_hash_value(DL(mr));
5762: NEXT(mr) = nd_red[h];
5763: nd_red[h] = mr;
1.24 noro 5764: mr->sig = r->sig;
1.1 noro 5765: }
5766: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5767: old_red = 0;
5768: for ( i = 0; i < nd_psn; i++ ) {
5769: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5770: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5771: r->sig = nd_psh[i]->sig;
1.1 noro 5772: nd_psh[i] = r;
5773: }
5774: if ( s0 ) NEXT(s) = 0;
5775: prev_nm_free_list = 0;
5776: prev_ndp_free_list = 0;
5777: #if 0
5778: GC_gcollect();
5779: #endif
5780: return s0;
5781: }
5782:
1.30 noro 5783: void nd_reconstruct_s(int trace,ND_pairs *d)
5784: {
5785: int i,obpe,oadv,h;
5786: static NM prev_nm_free_list;
5787: static ND_pairs prev_ndp_free_list;
5788: RHist mr0,mr;
5789: RHist r;
5790: RHist *old_red;
5791: ND_pairs s0,s,t;
5792: EPOS oepos;
5793:
5794: obpe = nd_bpe;
5795: oadv = nmv_adv;
5796: oepos = nd_epos;
5797: if ( obpe < 2 ) nd_bpe = 2;
5798: else if ( obpe < 3 ) nd_bpe = 3;
5799: else if ( obpe < 4 ) nd_bpe = 4;
5800: else if ( obpe < 5 ) nd_bpe = 5;
5801: else if ( obpe < 6 ) nd_bpe = 6;
5802: else if ( obpe < 8 ) nd_bpe = 8;
5803: else if ( obpe < 10 ) nd_bpe = 10;
5804: else if ( obpe < 16 ) nd_bpe = 16;
5805: else if ( obpe < 32 ) nd_bpe = 32;
5806: else error("nd_reconstruct_s : exponent too large");
5807:
5808: nd_setup_parameters(nd_nvar,0);
5809: prev_nm_free_list = _nm_free_list;
5810: prev_ndp_free_list = _ndp_free_list;
5811: _nm_free_list = 0;
5812: _ndp_free_list = 0;
5813: for ( i = nd_psn-1; i >= 0; i-- ) {
5814: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5815: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5816: }
5817: if ( trace )
5818: for ( i = nd_psn-1; i >= 0; i-- ) {
5819: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5820: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5821: }
5822:
5823: for ( i = 0; i < nd_nbase; i++ ) {
5824: s0 = 0;
5825: for ( t = d[i]; t; t = NEXT(t) ) {
5826: NEXTND_pairs(s0,s);
5827: s->i1 = t->i1;
5828: s->i2 = t->i2;
5829: s->sig = t->sig;
5830: SG(s) = SG(t);
5831: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5832: }
5833: d[i] = s0;
5834: }
5835:
5836: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5837: for ( i = 0; i < REDTAB_LEN; i++ ) {
5838: old_red[i] = nd_red[i];
5839: nd_red[i] = 0;
5840: }
5841: for ( i = 0; i < REDTAB_LEN; i++ )
5842: for ( r = old_red[i]; r; r = NEXT(r) ) {
5843: NEWRHist(mr);
5844: mr->index = r->index;
5845: SG(mr) = SG(r);
5846: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5847: h = ndl_hash_value(DL(mr));
5848: NEXT(mr) = nd_red[h];
5849: nd_red[h] = mr;
5850: mr->sig = r->sig;
5851: }
5852: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5853: old_red = 0;
5854: for ( i = 0; i < nd_psn; i++ ) {
5855: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5856: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
5857: r->sig = nd_psh[i]->sig;
5858: nd_psh[i] = r;
5859: }
5860: if ( s0 ) NEXT(s) = 0;
5861: prev_nm_free_list = 0;
5862: prev_ndp_free_list = 0;
5863: #if 0
5864: GC_gcollect();
5865: #endif
5866: }
5867:
1.1 noro 5868: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5869: {
5870: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5871: struct order_pair *op;
5872:
5873: n = nd_nvar;
5874: oepw = (sizeof(UINT)*8)/obpe;
5875: omask0 = (1<<obpe)-1;
5876: TD(r) = TD(d);
5877: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5878: if ( nd_blockmask ) {
5879: l = nd_blockmask->n;
5880: op = nd_blockmask->order_pair;
5881: for ( i = 1; i < nd_exporigin; i++ )
5882: r[i] = d[i];
5883: for ( j = 0, s = 0; j < l; j++ ) {
5884: ord_l = op[j].length;
5885: for ( i = 0; i < ord_l; i++, s++ ) {
5886: ei = GET_EXP_OLD(d,s);
5887: PUT_EXP(r,s,ei);
5888: }
5889: }
5890: } else {
5891: for ( i = 0; i < n; i++ ) {
5892: ei = GET_EXP_OLD(d,i);
5893: PUT_EXP(r,i,ei);
5894: }
5895: }
5896: if ( nd_module ) MPOS(r) = MPOS(d);
5897: }
5898:
5899: ND nd_copy(ND p)
5900: {
5901: NM m,mr,mr0;
5902: int c,n;
5903: ND r;
5904:
5905: if ( !p )
5906: return 0;
5907: else {
5908: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5909: NEXTNM(mr0,mr);
5910: CM(mr) = CM(m);
5911: ndl_copy(DL(m),DL(mr));
5912: }
5913: NEXT(mr) = 0;
5914: MKND(NV(p),mr0,LEN(p),r);
5915: SG(r) = SG(p);
5916: return r;
5917: }
5918: }
5919:
5920: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5921: {
5922: NM m1,m2;
5923: NDV p1,p2;
5924: ND t1,t2;
5925: UINT *lcm;
5926: P gp,tp;
5927: Z g,t;
5928: Z iq;
5929: int td;
5930: LIST hist;
5931: NODE node;
5932: DP d;
5933:
5934: if ( !mod && nd_demand ) {
5935: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5936: } else {
5937: if ( trace ) {
5938: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5939: } else {
5940: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5941: }
5942: }
5943: lcm = LCM(p);
5944: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5945: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5946: FREENM(m1); return 0;
5947: }
5948: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5949: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5950: FREENM(m1); FREENM(m2); return 0;
5951: }
5952:
5953: if ( mod == -1 ) {
5954: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5955: } else if ( mod > 0 ) {
5956: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5957: } else if ( mod == -2 ) {
5958: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5959: } else if ( nd_vc ) {
5960: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5961: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5962: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5963: } else {
1.6 noro 5964: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5965: }
5966: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5967: *rp = nd_add(mod,t1,t2);
5968: if ( nd_gentrace ) {
5969: /* nd_tracelist is initialized */
1.6 noro 5970: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5971: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5972: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5973: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5974: nd_tracelist = node;
5975: }
1.24 noro 5976: if ( *rp )
5977: (*rp)->sig = p->sig;
1.1 noro 5978: FREENM(m1); FREENM(m2);
5979: return 1;
5980: }
5981:
5982: void ndv_mul_c(int mod,NDV p,int mul)
5983: {
5984: NMV m;
5985: int c,c1,len,i;
5986:
5987: if ( !p ) return;
5988: len = LEN(p);
5989: if ( mod == -1 )
5990: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5991: CM(m) = _mulsf(CM(m),mul);
5992: else
5993: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5994: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5995: }
5996: }
5997:
5998: void ndv_mul_c_lf(NDV p,Z mul)
5999: {
6000: NMV m;
6001: Z c;
6002: int len,i;
6003:
6004: if ( !p ) return;
6005: len = LEN(p);
6006: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6007: mullf(CZ(m),mul,&c); CZ(m) = c;
6008: }
6009: }
6010:
6011: /* for nd_det */
6012: void ndv_mul_c_q(NDV p,Z mul)
6013: {
6014: NMV m;
6015: Z c;
6016: int len,i;
6017:
6018: if ( !p ) return;
6019: len = LEN(p);
6020: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6021: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6022: }
6023: }
6024:
6025: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6026: int n2,i,j,l,n,tlen;
6027: UINT *d0;
6028: NM *tab,*psum;
6029: ND s,r;
6030: NM t;
6031: NMV m1;
6032:
6033: if ( !p ) return 0;
6034: n = NV(p); n2 = n>>1;
6035: d0 = DL(m0);
6036: l = LEN(p);
6037: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6038: tab = (NM *)MALLOC(tlen*sizeof(NM));
6039: psum = (NM *)MALLOC(tlen*sizeof(NM));
6040: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6041: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6042: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6043: /* m0(NM) * m1(NMV) => tab(NM) */
6044: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6045: for ( j = 0; j < tlen; j++ ) {
6046: if ( tab[j] ) {
6047: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6048: }
6049: }
6050: }
6051: for ( i = tlen-1, r = 0; i >= 0; i-- )
6052: if ( psum[i] ) {
6053: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6054: MKND(n,psum[i],j,s);
6055: r = nd_add(mod,r,s);
6056: }
6057: if ( r ) SG(r) = SG(p)+TD(d0);
6058: return r;
6059: }
6060:
6061: /* product of monomials */
6062: /* XXX block order is not handled correctly */
6063:
6064: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6065: {
6066: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6067: UINT *d0,*d1,*d,*dt,*ctab;
6068: Z *ctab_q;
6069: Z q,q1;
6070: UINT c0,c1,c;
6071: NM *p;
6072: NM m,t;
6073: int mpos;
6074:
6075: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6076: if ( !m0 || !m1 ) return;
6077: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6078: if ( nd_module )
6079: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6080:
6081: NEWNM(m); d = DL(m);
6082: if ( mod ) {
6083: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6084: } else if ( nd_vc )
6085: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6086: else
1.6 noro 6087: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6088: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6089: homo = n&1 ? 1 : 0;
6090: if ( homo ) {
6091: /* offset of h-degree */
6092: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6093: PUT_EXP(DL(m),n-1,h);
6094: TD(DL(m)) = h;
6095: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6096: }
6097: tab[0] = m;
6098: NEWNM(m); d = DL(m);
6099: for ( i = 0, curlen = 1; i < n2; i++ ) {
6100: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6101: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6102: /* xi^a*(Di^k*xi^l)*Di^b */
6103: a += l; b += k;
6104: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6105: if ( !k || !l ) {
6106: for ( j = 0; j < curlen; j++ )
6107: if ( (t = tab[j]) != 0 ) {
6108: dt = DL(t);
6109: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6110: if ( nd_blockmask ) ndl_weight_mask(dt);
6111: }
6112: curlen *= k+1;
6113: continue;
6114: }
6115: min = MIN(k,l);
6116: if ( mod ) {
6117: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6118: mkwcm(k,l,mod,(int *)ctab);
6119: } else {
6120: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6121: mkwc(k,l,ctab_q);
6122: }
6123: for ( j = min; j >= 0; j-- ) {
6124: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6125: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6126: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6127: if ( homo ) {
6128: TD(d) = s;
6129: PUT_EXP(d,n-1,s-h);
6130: } else TD(d) = h;
6131: if ( nd_blockmask ) ndl_weight_mask(d);
6132: if ( mod ) c = ctab[j];
6133: else q = ctab_q[j];
6134: p = tab+curlen*j;
6135: if ( j == 0 ) {
6136: for ( u = 0; u < curlen; u++, p++ ) {
6137: if ( tab[u] ) {
6138: ndl_addto(DL(tab[u]),d);
6139: if ( mod ) {
6140: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6141: } else if ( nd_vc )
6142: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6143: else {
1.6 noro 6144: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6145: }
6146: }
6147: }
6148: } else {
6149: for ( u = 0; u < curlen; u++, p++ ) {
6150: if ( tab[u] ) {
6151: NEWNM(t);
6152: ndl_add(DL(tab[u]),d,DL(t));
6153: if ( mod ) {
6154: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6155: } else if ( nd_vc )
6156: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6157: else
1.6 noro 6158: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6159: *p = t;
6160: }
6161: }
6162: }
6163: }
6164: curlen *= k+1;
6165: }
6166: FREENM(m);
6167: if ( nd_module ) {
6168: mpos = MPOS(d1);
6169: for ( i = 0; i < tlen; i++ )
6170: if ( tab[i] ) {
6171: d = DL(tab[i]);
6172: MPOS(d) = mpos;
6173: TD(d) = ndl_weight(d);
6174: }
6175: }
6176: }
6177:
6178: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6179: {
6180: NM mr,mr0;
6181: NMV m;
6182: UINT *d,*dt,*dm;
6183: int c,n,td,i,c1,c2,len;
6184: Q q;
6185: ND r;
6186:
6187: if ( !p ) return 0;
6188: else {
6189: n = NV(p); m = BDY(p);
6190: d = DL(m0);
6191: len = LEN(p);
6192: mr0 = 0;
6193: td = TD(d);
6194: c = CM(m0);
6195: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6196: NEXTNM(mr0,mr);
6197: CM(mr) = 1;
6198: ndl_add(DL(m),d,DL(mr));
6199: }
6200: NEXT(mr) = 0;
6201: MKND(NV(p),mr0,len,r);
6202: SG(r) = SG(p) + TD(d);
6203: return r;
6204: }
6205: }
6206:
6207: ND ndv_mul_nm(int mod,NM m0,NDV p)
6208: {
6209: NM mr,mr0;
6210: NMV m;
6211: UINT *d,*dt,*dm;
6212: int c,n,td,i,c1,c2,len;
6213: P q;
6214: ND r;
6215:
6216: if ( !p ) return 0;
6217: else if ( do_weyl ) {
6218: if ( mod < 0 ) {
6219: error("ndv_mul_nm : not implemented (weyl)");
6220: return 0;
6221: } else
6222: return weyl_ndv_mul_nm(mod,m0,p);
6223: } else {
6224: n = NV(p); m = BDY(p);
6225: d = DL(m0);
6226: len = LEN(p);
6227: mr0 = 0;
6228: td = TD(d);
6229: if ( mod == -1 ) {
6230: c = CM(m0);
6231: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6232: NEXTNM(mr0,mr);
6233: CM(mr) = _mulsf(CM(m),c);
6234: ndl_add(DL(m),d,DL(mr));
6235: }
6236: } else if ( mod == -2 ) {
6237: Z cl;
6238: cl = CZ(m0);
6239: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6240: NEXTNM(mr0,mr);
6241: mullf(CZ(m),cl,&CZ(mr));
6242: ndl_add(DL(m),d,DL(mr));
6243: }
6244: } else if ( mod ) {
6245: c = CM(m0);
6246: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6247: NEXTNM(mr0,mr);
6248: c1 = CM(m);
6249: DMAR(c1,c,0,mod,c2);
6250: CM(mr) = c2;
6251: ndl_add(DL(m),d,DL(mr));
6252: }
6253: } else {
6254: q = CP(m0);
6255: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6256: NEXTNM(mr0,mr);
6257: mulp(nd_vc,CP(m),q,&CP(mr));
6258: ndl_add(DL(m),d,DL(mr));
6259: }
6260: }
6261: NEXT(mr) = 0;
6262: MKND(NV(p),mr0,len,r);
6263: SG(r) = SG(p) + TD(d);
6264: return r;
6265: }
6266: }
6267:
6268: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6269: {
6270: NM mq0,mq;
6271: NMV tm;
6272: Q q;
6273: int i,nv,sg,c,c1,c2,hindex;
6274: ND p,t,r;
6275:
6276: if ( bucket->m < 0 ) return 0;
6277: else {
6278: nv = NV(d);
6279: mq0 = 0;
6280: tm = (NMV)MALLOC(nmv_adv);
6281: while ( 1 ) {
6282: if ( mod > 0 || mod == -1 )
6283: hindex = head_pbucket(mod,bucket);
6284: else if ( mod == -2 )
6285: hindex = head_pbucket_lf(bucket);
6286: else
6287: hindex = head_pbucket_q(bucket);
6288: if ( hindex < 0 ) break;
6289: p = bucket->body[hindex];
6290: NEXTNM(mq0,mq);
6291: ndl_sub(HDL(p),HDL(d),DL(mq));
6292: ndl_copy(DL(mq),DL(tm));
6293: if ( mod ) {
6294: c1 = invm(HCM(d),mod); c2 = HCM(p);
6295: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6296: CM(tm) = mod-c;
6297: } else {
1.6 noro 6298: divsz(HCZ(p),HCZ(d),&CZ(mq));
6299: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6300: }
6301: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6302: bucket->body[hindex] = nd_remove_head(p);
6303: t = nd_remove_head(t);
6304: add_pbucket(mod,bucket,t);
6305: }
6306: if ( !mq0 )
6307: r = 0;
6308: else {
6309: NEXT(mq) = 0;
6310: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6311: MKND(nv,mq0,i,r);
6312: /* XXX */
6313: SG(r) = HTD(r);
6314: }
6315: return r;
6316: }
6317: }
6318:
6319: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6320: {
6321: NMV m,mr,mr0,t;
6322: int len,i,k;
6323:
6324: if ( !p ) return;
6325: m = BDY(p); len = LEN(p);
6326: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6327: m = (NMV)((char *)mr0+(len-1)*oadv);
6328: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6329: t = (NMV)MALLOC(nmv_adv);
6330: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6331: CZ(t) = CZ(m);
1.1 noro 6332: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6333: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6334: CZ(mr) = CZ(t);
1.1 noro 6335: ndl_copy(DL(t),DL(mr));
6336: }
6337: BDY(p) = mr0;
6338: }
6339:
6340: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6341: {
6342: NMV m,mr,mr0;
6343: int len,i;
6344: NDV r;
6345:
6346: if ( !p ) return 0;
6347: m = BDY(p); len = LEN(p);
6348: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6349: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6350: ndl_zero(DL(mr));
6351: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6352: CZ(mr) = CZ(m);
1.1 noro 6353: }
6354: MKNDV(NV(p),mr0,len,r);
6355: SG(r) = SG(p);
1.24 noro 6356: r->sig = p->sig;
1.1 noro 6357: return r;
6358: }
6359:
6360: /* duplicate p */
6361:
6362: NDV ndv_dup(int mod,NDV p)
6363: {
6364: NDV d;
6365: NMV t,m,m0;
6366: int i,len;
6367:
6368: if ( !p ) return 0;
6369: len = LEN(p);
6370: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6371: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6372: ndl_copy(DL(t),DL(m));
1.6 noro 6373: CZ(m) = CZ(t);
1.1 noro 6374: }
6375: MKNDV(NV(p),m0,len,d);
6376: SG(d) = SG(p);
6377: return d;
6378: }
6379:
6380: NDV ndv_symbolic(int mod,NDV p)
6381: {
6382: NDV d;
6383: NMV t,m,m0;
6384: int i,len;
6385:
6386: if ( !p ) return 0;
6387: len = LEN(p);
6388: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6389: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6390: ndl_copy(DL(t),DL(m));
1.6 noro 6391: CZ(m) = ONE;
1.1 noro 6392: }
6393: MKNDV(NV(p),m0,len,d);
6394: SG(d) = SG(p);
6395: return d;
6396: }
6397:
6398: ND nd_dup(ND p)
6399: {
6400: ND d;
6401: NM t,m,m0;
6402:
6403: if ( !p ) return 0;
6404: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6405: NEXTNM(m0,m);
6406: ndl_copy(DL(t),DL(m));
1.6 noro 6407: CZ(m) = CZ(t);
1.1 noro 6408: }
6409: if ( m0 ) NEXT(m) = 0;
6410: MKND(NV(p),m0,LEN(p),d);
6411: SG(d) = SG(p);
6412: return d;
6413: }
6414:
6415: /* XXX if p->len == 0 then it represents 0 */
6416:
6417: void ndv_mod(int mod,NDV p)
6418: {
6419: NMV t,d;
6420: int r,s,u;
6421: int i,len,dlen;
6422: P cp;
6423: Z c;
6424: Obj gfs;
6425:
6426: if ( !p ) return;
6427: len = LEN(p);
6428: dlen = 0;
6429: if ( mod == -1 )
6430: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6431: simp_ff((Obj)CP(t),&gfs);
6432: if ( gfs ) {
6433: r = FTOIF(CONT((GFS)gfs));
6434: CM(d) = r;
6435: ndl_copy(DL(t),DL(d));
6436: NMV_ADV(d);
6437: dlen++;
6438: }
6439: }
6440: else if ( mod == -2 )
6441: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6442: simp_ff((Obj)CP(t),&gfs);
6443: if ( gfs ) {
6444: lmtolf((LM)gfs,&CZ(d));
6445: ndl_copy(DL(t),DL(d));
6446: NMV_ADV(d);
6447: dlen++;
6448: }
6449: }
6450: else
6451: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6452: if ( nd_vc ) {
6453: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6454: c = (Z)cp;
6455: } else
1.6 noro 6456: c = CZ(t);
1.1 noro 6457: r = remqi((Q)c,mod);
6458: if ( r ) {
6459: CM(d) = r;
6460: ndl_copy(DL(t),DL(d));
6461: NMV_ADV(d);
6462: dlen++;
6463: }
6464: }
6465: LEN(p) = dlen;
6466: }
6467:
6468: NDV ptondv(VL vl,VL dvl,P p)
6469: {
6470: ND nd;
6471:
6472: nd = ptond(vl,dvl,p);
6473: return ndtondv(0,nd);
6474: }
6475:
6476: void pltozpl(LIST l,Q *cont,LIST *pp)
6477: {
1.16 noro 6478: NODE nd,nd1;
6479: int n;
6480: P *pl;
6481: Q *cl;
6482: int i;
6483: P dmy;
6484: Z dvr,inv;
6485: LIST r;
6486:
6487: nd = BDY(l); n = length(nd);
6488: pl = (P *)MALLOC(n*sizeof(P));
6489: cl = (Q *)MALLOC(n*sizeof(Q));
6490: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6491: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6492: }
6493: qltozl(cl,n,&dvr);
6494: divz(ONE,dvr,&inv);
6495: nd = BDY(l);
6496: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6497: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6498: nd = 0;
6499: for ( i = n-1; i >= 0; i-- ) {
6500: MKNODE(nd1,pl[i],nd); nd = nd1;
6501: }
6502: MKLIST(r,nd);
6503: *pp = r;
1.1 noro 6504: }
6505:
6506: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6507:
6508: NDV pltondv(VL vl,VL dvl,LIST p)
6509: {
6510: int i;
6511: NODE t;
6512: ND r,ri;
6513: NM m;
6514:
6515: if ( !nd_module ) error("pltond : module order must be set");
6516: r = 0;
6517: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6518: ri = ptond(vl,dvl,(P)BDY(t));
6519: if ( ri )
6520: for ( m = BDY(ri); m; m = NEXT(m) ) {
6521: MPOS(DL(m)) = i;
6522: TD(DL(m)) = ndl_weight(DL(m));
6523: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6524: }
6525: r = nd_add(0,r,ri);
6526: }
6527: return ndtondv(0,r);
6528: }
6529:
6530: ND ptond(VL vl,VL dvl,P p)
6531: {
6532: int n,i,j,k,e;
6533: VL tvl;
6534: V v;
6535: DCP dc;
6536: DCP *w;
6537: ND r,s,t,u;
6538: P x;
6539: int c;
6540: UINT *d;
6541: NM m,m0;
6542:
6543: if ( !p )
6544: return 0;
6545: else if ( NUM(p) ) {
6546: NEWNM(m);
6547: ndl_zero(DL(m));
6548: if ( !INT((Q)p) )
6549: error("ptond : input must be integer-coefficient");
1.6 noro 6550: CZ(m) = (Z)p;
1.1 noro 6551: NEXT(m) = 0;
6552: MKND(nd_nvar,m,1,r);
6553: SG(r) = 0;
6554: return r;
6555: } else {
6556: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6557: w = (DCP *)MALLOC(k*sizeof(DCP));
6558: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6559: for ( i = 0, tvl = dvl, v = VR(p);
6560: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6561: if ( !tvl ) {
6562: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6563: t = ptond(vl,dvl,COEF(w[j]));
6564: pwrp(vl,x,DEG(w[j]),&p);
6565: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6566: }
6567: return s;
6568: } else {
6569: NEWNM(m0); d = DL(m0);
6570: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6571: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6572: TD(d) = MUL_WEIGHT(e,i);
6573: if ( nd_blockmask) ndl_weight_mask(d);
6574: if ( nd_module ) MPOS(d) = 0;
6575: t = ptond(vl,dvl,COEF(w[j]));
6576: for ( m = BDY(t); m; m = NEXT(m) )
6577: ndl_addto(DL(m),d);
6578: SG(t) += TD(d);
6579: s = nd_add(0,s,t);
6580: }
6581: FREENM(m0);
6582: return s;
6583: }
6584: }
6585: }
6586:
6587: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6588: {
6589: VL tvl;
6590: int len,n,j,i,e;
6591: NMV m;
6592: Z q;
6593: P c;
6594: UINT *d;
6595: P s,r,u,t,w;
6596: GFS gfs;
6597:
6598: if ( !p ) return 0;
6599: else {
6600: len = LEN(p);
6601: n = NV(p);
6602: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6603: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6604: if ( mod == -1 ) {
6605: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6606: } else if ( mod == -2 ) {
6607: c = (P)CZ(m);
6608: } else if ( mod > 0 ) {
1.6 noro 6609: STOZ(CM(m),q); c = (P)q;
1.1 noro 6610: } else
6611: c = CP(m);
6612: d = DL(m);
6613: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6614: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6615: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6616: }
6617: addp(vl,s,t,&u); s = u;
6618: }
6619: return s;
6620: }
6621: }
6622:
6623: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6624: {
6625: VL tvl;
6626: int len,n,j,i,e;
6627: NMV m;
6628: Z q;
6629: P c;
6630: UINT *d;
6631: P s,r,u,t,w;
6632: GFS gfs;
6633: P *a;
6634: LIST l;
6635: NODE nd,nd1;
6636:
6637: if ( !p ) return 0;
6638: else {
6639: a = (P *)MALLOC((rank+1)*sizeof(P));
6640: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6641: len = LEN(p);
6642: n = NV(p);
6643: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6644: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6645: if ( mod == -1 ) {
6646: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6647: } else if ( mod ) {
1.6 noro 6648: STOZ(CM(m),q); c = (P)q;
1.1 noro 6649: } else
6650: c = CP(m);
6651: d = DL(m);
6652: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6653: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6654: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6655: }
6656: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6657: }
6658: nd = 0;
6659: for ( i = rank; i > 0; i-- ) {
6660: MKNODE(nd1,a[i],nd); nd = nd1;
6661: }
6662: MKLIST(l,nd);
6663: return l;
6664: }
6665: }
6666:
6667: NDV ndtondv(int mod,ND p)
6668: {
6669: NDV d;
6670: NMV m,m0;
6671: NM t;
6672: int i,len;
6673:
6674: if ( !p ) return 0;
6675: len = LEN(p);
6676: if ( mod > 0 || mod == -1 )
6677: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6678: else
6679: m0 = m = MALLOC(len*nmv_adv);
6680: #if 0
6681: ndv_alloc += nmv_adv*len;
6682: #endif
6683: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6684: ndl_copy(DL(t),DL(m));
1.6 noro 6685: CZ(m) = CZ(t);
1.1 noro 6686: }
6687: MKNDV(NV(p),m0,len,d);
6688: SG(d) = SG(p);
1.24 noro 6689: d->sig = p->sig;
1.1 noro 6690: return d;
6691: }
6692:
1.16 noro 6693: static int dmm_comp_nv;
6694:
6695: int dmm_comp(DMM *a,DMM *b)
6696: {
6697: return -compdmm(dmm_comp_nv,*a,*b);
6698: }
6699:
6700: void dmm_sort_by_ord(DMM *a,int len,int nv)
6701: {
6702: dmm_comp_nv = nv;
6703: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6704: }
6705:
6706: void dpm_sort(DPM p,DPM *rp)
6707: {
6708: DMM t,t1;
6709: int len,i,n;
6710: DMM *a;
6711: DPM d;
6712:
6713: if ( !p ) *rp = 0;
6714: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6715: a = (DMM *)MALLOC(len*sizeof(DMM));
6716: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6717: n = p->nv;
6718: dmm_sort_by_ord(a,len,n);
6719: t = 0;
6720: for ( i = len-1; i >= 0; i-- ) {
6721: NEWDMM(t1);
6722: t1->c = a[i]->c;
6723: t1->dl = a[i]->dl;
6724: t1->pos = a[i]->pos;
6725: t1->next = t;
6726: t = t1;
6727: }
6728: MKDPM(n,t,d);
6729: SG(d) = SG(p);
6730: *rp = d;
6731: }
6732:
1.18 noro 6733: int dpm_comp(DPM *a,DPM *b)
6734: {
1.22 noro 6735: return -compdpm(CO,*a,*b);
1.18 noro 6736: }
6737:
6738: NODE dpm_sort_list(NODE l)
6739: {
6740: int i,len;
6741: NODE t,t1;
6742: DPM *a;
6743:
6744: len = length(l);
6745: a = (DPM *)MALLOC(len*sizeof(DPM));
6746: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6747: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6748: t = 0;
6749: for ( i = len-1; i >= 0; i-- ) {
6750: MKNODE(t1,(pointer)a[i],t); t = t1;
6751: }
6752: return t;
6753: }
6754:
1.20 noro 6755: int nmv_comp(NMV a,NMV b)
6756: {
1.21 noro 6757: int t;
6758: t = DL_COMPARE(a->dl,b->dl);
6759: return -t;
1.20 noro 6760: }
6761:
1.16 noro 6762: NDV dpmtondv(int mod,DPM p)
6763: {
6764: NDV d;
6765: NMV m,m0;
6766: DMM t;
6767: DMM *a;
6768: int i,len,n;
6769:
6770: if ( !p ) return 0;
6771: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6772: a = (DMM *)MALLOC(len*sizeof(DMM));
6773: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6774: n = p->nv;
6775: dmm_sort_by_ord(a,len,n);
6776: if ( mod > 0 || mod == -1 )
6777: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6778: else
6779: m0 = m = MALLOC(len*nmv_adv);
6780: #if 0
6781: ndv_alloc += nmv_adv*len;
6782: #endif
6783: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6784: dltondl(n,a[i]->dl,DL(m));
6785: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6786: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6787: CZ(m) = (Z)a[i]->c;
6788: }
1.20 noro 6789: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6790: MKNDV(NV(p),m0,len,d);
6791: SG(d) = SG(p);
6792: return d;
6793: }
6794:
1.1 noro 6795: ND ndvtond(int mod,NDV p)
6796: {
6797: ND d;
6798: NM m,m0;
6799: NMV t;
6800: int i,len;
6801:
6802: if ( !p ) return 0;
6803: m0 = 0;
6804: len = p->len;
6805: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6806: NEXTNM(m0,m);
6807: ndl_copy(DL(t),DL(m));
1.6 noro 6808: CZ(m) = CZ(t);
1.1 noro 6809: }
6810: NEXT(m) = 0;
6811: MKND(NV(p),m0,len,d);
6812: SG(d) = SG(p);
1.28 noro 6813: d->sig = p->sig;
1.1 noro 6814: return d;
6815: }
6816:
6817: DP ndvtodp(int mod,NDV p)
6818: {
6819: MP m,m0;
6820: DP d;
6821: NMV t;
6822: int i,len;
6823:
6824: if ( !p ) return 0;
6825: m0 = 0;
6826: len = p->len;
6827: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6828: NEXTMP(m0,m);
6829: m->dl = ndltodl(nd_nvar,DL(t));
6830: m->c = (Obj)ndctop(mod,t->c);
6831: }
6832: NEXT(m) = 0;
6833: MKDP(nd_nvar,m0,d);
6834: SG(d) = SG(p);
6835: return d;
6836: }
6837:
1.16 noro 6838: DPM ndvtodpm(int mod,NDV p)
6839: {
6840: DMM m,m0;
6841: DPM d;
6842: NMV t;
6843: int i,len;
6844:
6845: if ( !p ) return 0;
6846: m0 = 0;
6847: len = p->len;
6848: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6849: NEXTDMM(m0,m);
6850: m->dl = ndltodl(nd_nvar,DL(t));
6851: m->c = (Obj)ndctop(mod,t->c);
6852: m->pos = MPOS(DL(t));
6853: }
6854: NEXT(m) = 0;
6855: MKDPM(nd_nvar,m0,d);
6856: SG(d) = SG(p);
6857: return d;
6858: }
6859:
6860:
1.1 noro 6861: DP ndtodp(int mod,ND p)
6862: {
6863: MP m,m0;
6864: DP d;
6865: NM t;
6866: int i,len;
6867:
6868: if ( !p ) return 0;
6869: m0 = 0;
6870: len = p->len;
6871: for ( t = BDY(p); t; t = NEXT(t) ) {
6872: NEXTMP(m0,m);
6873: m->dl = ndltodl(nd_nvar,DL(t));
6874: m->c = (Obj)ndctop(mod,t->c);
6875: }
6876: NEXT(m) = 0;
6877: MKDP(nd_nvar,m0,d);
6878: SG(d) = SG(p);
6879: return d;
6880: }
6881:
6882: void ndv_print(NDV p)
6883: {
6884: NMV m;
6885: int i,len;
6886:
6887: if ( !p ) printf("0\n");
6888: else {
6889: len = LEN(p);
6890: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6891: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 6892: else printf("+%ld*",CM(m));
1.1 noro 6893: ndl_print(DL(m));
6894: }
6895: printf("\n");
6896: }
6897: }
6898:
6899: void ndv_print_q(NDV p)
6900: {
6901: NMV m;
6902: int i,len;
6903:
6904: if ( !p ) printf("0\n");
6905: else {
6906: len = LEN(p);
6907: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6908: printf("+");
1.6 noro 6909: printexpr(CO,(Obj)CZ(m));
1.1 noro 6910: printf("*");
6911: ndl_print(DL(m));
6912: }
6913: printf("\n");
6914: }
6915: }
6916:
6917: NODE ndv_reducebase(NODE x,int *perm)
6918: {
6919: int len,i,j;
6920: NDVI w;
6921: NODE t,t0;
6922:
1.41 noro 6923: if ( nd_norb ) return x;
1.1 noro 6924: len = length(x);
6925: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6926: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6927: w[i].p = BDY(t); w[i].i = perm[i];
6928: }
6929: for ( i = 0; i < len; i++ ) {
6930: for ( j = 0; j < i; j++ ) {
6931: if ( w[i].p && w[j].p ) {
6932: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6933: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6934: }
6935: }
6936: }
6937: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6938: if ( w[i].p ) {
6939: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6940: perm[j++] = w[i].i;
6941: }
6942: }
6943: NEXT(t) = 0; x = t0;
6944: return x;
6945: }
6946:
6947: /* XXX incomplete */
6948:
1.21 noro 6949: extern DMMstack dmm_stack;
6950: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6951:
1.1 noro 6952: void nd_init_ord(struct order_spec *ord)
6953: {
6954: nd_module = (ord->id >= 256);
6955: if ( nd_module ) {
6956: nd_dcomp = -1;
1.21 noro 6957: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6958: nd_pot_nelim = ord->pot_nelim;
6959: nd_poly_weight_len = ord->nv;
6960: nd_poly_weight = ord->top_weight;
6961: nd_module_rank = ord->module_rank;
6962: nd_module_weight = ord->module_top_weight;
6963: }
6964: nd_matrix = 0;
6965: nd_matrix_len = 0;
6966: switch ( ord->id ) {
6967: case 0:
6968: switch ( ord->ord.simple ) {
6969: case 0:
6970: nd_dcomp = 1;
6971: nd_isrlex = 1;
6972: break;
6973: case 1:
6974: nd_dcomp = 1;
6975: nd_isrlex = 0;
6976: break;
6977: case 2:
6978: nd_dcomp = 0;
6979: nd_isrlex = 0;
6980: ndl_compare_function = ndl_lex_compare;
6981: break;
6982: case 11:
6983: /* XXX */
6984: nd_dcomp = 0;
6985: nd_isrlex = 1;
6986: ndl_compare_function = ndl_ww_lex_compare;
6987: break;
6988: default:
6989: error("nd_gr : unsupported order");
6990: }
6991: break;
6992: case 1:
6993: /* block order */
6994: /* XXX */
6995: nd_dcomp = -1;
6996: nd_isrlex = 0;
6997: ndl_compare_function = ndl_block_compare;
6998: break;
6999: case 2:
7000: /* matrix order */
7001: /* XXX */
7002: nd_dcomp = -1;
7003: nd_isrlex = 0;
7004: nd_matrix_len = ord->ord.matrix.row;
7005: nd_matrix = ord->ord.matrix.matrix;
7006: ndl_compare_function = ndl_matrix_compare;
7007: break;
7008: case 3:
7009: /* composite order */
7010: nd_dcomp = -1;
7011: nd_isrlex = 0;
7012: nd_worb_len = ord->ord.composite.length;
7013: nd_worb = ord->ord.composite.w_or_b;
7014: ndl_compare_function = ndl_composite_compare;
7015: break;
7016:
7017: /* module order */
7018: case 256:
7019: switch ( ord->ord.simple ) {
7020: case 0:
1.21 noro 7021: nd_dcomp = 0;
1.1 noro 7022: nd_isrlex = 1;
1.21 noro 7023: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7024: break;
7025: case 1:
1.21 noro 7026: nd_dcomp = 0;
1.1 noro 7027: nd_isrlex = 0;
7028: ndl_compare_function = ndl_module_glex_compare;
7029: break;
7030: case 2:
1.21 noro 7031: nd_dcomp = 0;
1.1 noro 7032: nd_isrlex = 0;
1.21 noro 7033: ndl_compare_function = ndl_module_compare;
7034: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7035: break;
7036: default:
1.21 noro 7037: error("nd_init_ord : unsupported order");
1.1 noro 7038: }
7039: break;
7040: case 257:
7041: /* block order */
7042: nd_isrlex = 0;
1.21 noro 7043: ndl_compare_function = ndl_module_compare;
7044: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7045: break;
7046: case 258:
7047: /* matrix order */
7048: nd_isrlex = 0;
7049: nd_matrix_len = ord->ord.matrix.row;
7050: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7051: ndl_compare_function = ndl_module_compare;
7052: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7053: break;
7054: case 259:
7055: /* composite order */
7056: nd_isrlex = 0;
7057: nd_worb_len = ord->ord.composite.length;
7058: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7059: ndl_compare_function = ndl_module_compare;
7060: ndl_base_compare_function = ndl_composite_compare;
7061: break;
7062: case 300:
7063: /* schreyer order */
7064: if ( ord->base->id != 256 )
7065: error("nd_init_ord : unsupported base order");
7066: ndl_compare_function = ndl_module_schreyer_compare;
7067: dmm_stack = ord->dmmstack;
7068: switch ( ord->base->ord.simple ) {
7069: case 0:
7070: nd_isrlex = 1;
7071: ndl_base_compare_function = ndl_glex_compare;
7072: dl_base_compare_function = cmpdl_revgradlex;
7073: break;
7074: case 1:
7075: nd_isrlex = 0;
7076: ndl_base_compare_function = ndl_glex_compare;
7077: dl_base_compare_function = cmpdl_gradlex;
7078: break;
7079: case 2:
7080: nd_isrlex = 0;
7081: ndl_base_compare_function = ndl_lex_compare;
7082: dl_base_compare_function = cmpdl_lex;
7083: break;
7084: default:
7085: error("nd_init_ord : unsupported order");
7086: }
1.1 noro 7087: break;
7088: }
7089: nd_ord = ord;
7090: }
7091:
7092: BlockMask nd_create_blockmask(struct order_spec *ord)
7093: {
7094: int n,i,j,s,l;
7095: UINT *t;
7096: BlockMask bm;
7097:
7098: /* we only create mask table for block order */
7099: if ( ord->id != 1 && ord->id != 257 )
7100: return 0;
7101: n = ord->ord.block.length;
7102: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7103: bm->n = n;
7104: bm->order_pair = ord->ord.block.order_pair;
7105: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7106: for ( i = 0, s = 0; i < n; i++ ) {
7107: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7108: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7109: l = bm->order_pair[i].length;
7110: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7111: }
7112: return bm;
7113: }
7114:
7115: EPOS nd_create_epos(struct order_spec *ord)
7116: {
7117: int i,j,l,s,ord_l,ord_o;
7118: EPOS epos;
7119: struct order_pair *op;
7120:
7121: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7122: switch ( ord->id ) {
1.21 noro 7123: case 0: case 256: case 300:
1.1 noro 7124: if ( nd_isrlex ) {
7125: for ( i = 0; i < nd_nvar; i++ ) {
7126: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7127: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7128: }
7129: } else {
7130: for ( i = 0; i < nd_nvar; i++ ) {
7131: epos[i].i = nd_exporigin + i/nd_epw;
7132: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7133: }
7134: }
7135: break;
7136: case 1: case 257:
7137: /* block order */
7138: l = ord->ord.block.length;
7139: op = ord->ord.block.order_pair;
7140: for ( j = 0, s = 0; j < l; j++ ) {
7141: ord_o = op[j].order;
7142: ord_l = op[j].length;
7143: if ( !ord_o )
7144: for ( i = 0; i < ord_l; i++ ) {
7145: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7146: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7147: }
7148: else
7149: for ( i = 0; i < ord_l; i++ ) {
7150: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7151: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7152: }
7153: s += ord_l;
7154: }
7155: break;
7156: case 2:
7157: /* matrix order */
7158: case 3:
7159: /* composite order */
7160: default:
7161: for ( i = 0; i < nd_nvar; i++ ) {
7162: epos[i].i = nd_exporigin + i/nd_epw;
7163: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7164: }
7165: break;
7166: }
7167: return epos;
7168: }
7169:
7170: /* external interface */
7171:
7172: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7173: {
7174: NODE t,in0,in;
7175: ND ndf,nf;
7176: NDV ndvf;
7177: VL vv,tv;
7178: int stat,nvar,max,mrank;
7179: union oNDC dn;
7180: Q cont;
7181: P pp;
7182: LIST ppl;
7183:
7184: if ( !f ) {
7185: *rp = 0;
7186: return;
7187: }
7188: pltovl(v,&vv);
7189: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7190:
7191: /* max=65536 implies nd_bpe=32 */
7192: max = 65536;
7193:
7194: nd_module = 0;
7195: /* nd_module will be set if ord is a module ordering */
7196: nd_init_ord(ord);
7197: nd_setup_parameters(nvar,max);
7198: if ( nd_module && OID(f) != O_LIST )
7199: error("nd_nf_p : the first argument must be a list");
7200: if ( nd_module ) mrank = length(BDY((LIST)f));
7201: /* conversion to ndv */
7202: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7203: NEXTNODE(in0,in);
7204: if ( nd_module ) {
7205: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7206: || length(BDY((LIST)BDY(t))) != mrank )
7207: error("nd_nf_p : inconsistent basis element");
7208: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7209: else ppl = (LIST)BDY(t);
7210: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7211: } else {
7212: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7213: else pp = (P)BDY(t);
7214: BDY(in) = (pointer)ptondv(CO,vv,pp);
7215: }
7216: if ( m ) ndv_mod(m,(NDV)BDY(in));
7217: }
7218: if ( in0 ) NEXT(in) = 0;
7219:
7220: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7221: else ndvf = ptondv(CO,vv,(P)f);
7222: if ( m ) ndv_mod(m,ndvf);
7223: ndf = (pointer)ndvtond(m,ndvf);
7224:
7225: /* dont sort, dont removecont */
1.24 noro 7226: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7227: nd_scale=2;
1.6 noro 7228: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7229: if ( !stat )
7230: error("nd_nf_p : exponent too large");
7231: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7232: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7233: }
7234:
7235: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7236: {
7237: NM m;
7238: UINT *t,*s;
7239: int i;
7240:
7241: for ( i = 0; i < n; i++ ) r[i] = 0;
7242: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7243: t = DL(m);
7244: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7245: r[i] = CM(m);
7246: }
7247: for ( i = 0; !r[i]; i++ );
7248: return i;
7249: }
7250:
7251: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7252: {
7253: NM m;
7254: UINT *t,*s;
7255: int i;
7256:
7257: for ( i = 0; i < n; i++ ) r[i] = 0;
7258: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7259: t = DL(m);
7260: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7261: r[i] = CZ(m);
1.1 noro 7262: }
7263: for ( i = 0; !r[i]; i++ );
7264: return i;
7265: }
7266:
7267: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7268: {
7269: NM m;
7270: UINT *t,*s;
7271: int i;
7272:
7273: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7274: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7275: t = DL(m);
7276: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7277: mpz_set(r[i],BDY(CZ(m)));
7278: }
7279: for ( i = 0; !mpz_sgn(r[i]); i++ );
7280: return i;
7281: }
7282:
7283: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7284: {
7285: NM m;
7286: unsigned long *v;
7287: int i,j,h,size;
7288: UINT *s,*t;
7289:
7290: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7291: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7292: bzero(v,size);
7293: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7294: t = DL(m);
7295: h = ndl_hash_value(t);
7296: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7297: v[i/BLEN] |= 1L <<(i%BLEN);
7298: }
7299: return v;
7300: }
7301:
7302: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7303: {
7304: NMV mr;
7305: UINT *d,*t,*s;
7306: int i,j,len,h,head;
7307:
7308: d = DL(m);
7309: len = LEN(p);
7310: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7311: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7312: ndl_add(d,DL(mr),t);
7313: h = ndl_hash_value(t);
7314: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7315: if ( j == 0 ) head = i;
7316: v[i/BLEN] |= 1L <<(i%BLEN);
7317: }
7318: return head;
7319: }
7320:
7321: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7322: {
7323: NM m;
7324: NMV mr;
7325: UINT *d,*t,*s;
7326: NDV p;
7327: int i,j,len;
7328: Z *r;
7329:
7330: m = pair->mul;
7331: d = DL(m);
7332: p = nd_ps[pair->index];
7333: len = LEN(p);
7334: r = (Z *)CALLOC(n,sizeof(Q));
7335: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7336: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7337: ndl_add(d,DL(mr),t);
7338: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7339: r[i] = CZ(mr);
1.1 noro 7340: }
7341: return r;
7342: }
7343:
1.11 noro 7344: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7345: {
7346: NM m;
7347: NMV mr;
1.11 noro 7348: UINT *d,*t,*s,*u;
1.1 noro 7349: NDV p;
7350: unsigned char *ivc;
7351: unsigned short *ivs;
7352: UINT *v,*ivi,*s0v;
1.11 noro 7353: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7354: IndArray r;
7355:
7356: m = pair->mul;
7357: d = DL(m);
7358: if ( trace )
7359: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7360: else
7361: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7362:
7363: len = LEN(p);
7364: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7365: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7366: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7367: ndl_add(d,DL(mr),t);
7368: st = prev;
7369: ed = n;
7370: while ( ed > st ) {
7371: md = (st+ed)/2;
7372: u = s0+md*nd_wpd;
7373: c = DL_COMPARE(u,t);
7374: if ( c == 0 ) break;
7375: else if ( c > 0 ) st = md;
7376: else ed = md;
7377: }
7378: prev = v[j] = md;
1.1 noro 7379: }
7380: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7381: r->head = v[0];
7382: diff = 0;
7383: for ( i = 1; i < len; i++ ) {
7384: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7385: }
7386: if ( diff < 256 ) {
7387: r->width = 1;
7388: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7389: r->index.c = ivc;
7390: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7391: } else if ( diff < 65536 ) {
7392: r->width = 2;
7393: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7394: r->index.s = ivs;
7395: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7396: } else {
7397: r->width = 4;
7398: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7399: r->index.i = ivi;
7400: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7401: }
7402: return r;
7403: }
7404:
7405: int compress_array(Z *svect,Z *cvect,int n)
7406: {
7407: int i,j;
7408:
7409: for ( i = j = 0; i < n; i++ )
7410: if ( svect[i] ) cvect[j++] = svect[i];
7411: return j;
7412: }
7413:
7414: void expand_array(Z *svect,Z *cvect,int n)
7415: {
7416: int i,j;
7417:
7418: for ( i = j = 0; j < n; i++ )
7419: if ( svect[i] ) svect[i] = cvect[j++];
7420: }
7421:
1.8 noro 7422: #if 0
1.1 noro 7423: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7424: {
7425: int i,j,k,len,pos,prev,nz;
7426: Z cs,mcs,c1,c2,cr,gcd,t;
7427: IndArray ivect;
7428: unsigned char *ivc;
7429: unsigned short *ivs;
7430: unsigned int *ivi;
7431: NDV redv;
7432: NMV mr;
7433: NODE rp;
7434: int maxrs;
7435: double hmag;
7436: Z *cvect;
1.3 noro 7437: int l;
1.1 noro 7438:
7439: maxrs = 0;
7440: for ( i = 0; i < col && !svect[i]; i++ );
7441: if ( i == col ) return maxrs;
7442: hmag = p_mag((P)svect[i])*nd_scale;
7443: cvect = (Z *)MALLOC(col*sizeof(Q));
7444: for ( i = 0; i < nred; i++ ) {
7445: ivect = imat[i];
7446: k = ivect->head;
7447: if ( svect[k] ) {
7448: maxrs = MAX(maxrs,rp0[i]->sugar);
7449: redv = nd_demand?ndv_load(rp0[i]->index)
7450: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7451: len = LEN(redv); mr = BDY(redv);
1.6 noro 7452: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7453: chsgnz(cs,&mcs);
7454: if ( !UNIQ(cr) ) {
7455: for ( j = 0; j < col; j++ ) {
7456: mulz(svect[j],cr,&c1); svect[j] = c1;
7457: }
7458: }
7459: svect[k] = 0; prev = k;
7460: switch ( ivect->width ) {
7461: case 1:
7462: ivc = ivect->index.c;
7463: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7464: pos = prev+ivc[j]; prev = pos;
1.6 noro 7465: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7466: }
7467: break;
7468: case 2:
7469: ivs = ivect->index.s;
7470: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7471: pos = prev+ivs[j]; prev = pos;
1.6 noro 7472: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7473: }
7474: break;
7475: case 4:
7476: ivi = ivect->index.i;
7477: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7478: pos = prev+ivi[j]; prev = pos;
1.6 noro 7479: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7480: }
7481: break;
7482: }
7483: for ( j = k+1; j < col && !svect[j]; j++ );
7484: if ( j == col ) break;
7485: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7486: nz = compress_array(svect,cvect,col);
7487: removecont_array((P *)cvect,nz,1);
7488: expand_array(svect,cvect,nz);
7489: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7490: }
7491: }
7492: }
7493: nz = compress_array(svect,cvect,col);
7494: removecont_array((P *)cvect,nz,1);
7495: expand_array(svect,cvect,nz);
7496: if ( DP_Print ) {
7497: fprintf(asir_out,"-"); fflush(asir_out);
7498: }
7499: return maxrs;
7500: }
1.4 noro 7501: #else
1.9 noro 7502:
1.4 noro 7503: /* direct mpz version */
7504: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7505: {
7506: int i,j,k,len,pos,prev;
7507: mpz_t cs,cr,gcd;
7508: IndArray ivect;
7509: unsigned char *ivc;
7510: unsigned short *ivs;
7511: unsigned int *ivi;
7512: NDV redv;
7513: NMV mr;
7514: NODE rp;
7515: int maxrs;
7516: double hmag;
7517: int l;
1.13 noro 7518: static mpz_t *svect;
7519: static int svect_len=0;
1.4 noro 7520:
7521: maxrs = 0;
7522: for ( i = 0; i < col && !svect0[i]; i++ );
7523: if ( i == col ) return maxrs;
7524: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7525: if ( col > svect_len ) {
7526: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7527: svect_len = col;
7528: }
1.4 noro 7529: for ( i = 0; i < col; i++ ) {
7530: mpz_init(svect[i]);
7531: if ( svect0[i] )
7532: mpz_set(svect[i],BDY(svect0[i]));
7533: else
7534: mpz_set_ui(svect[i],0);
7535: }
7536: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7537: for ( i = 0; i < nred; i++ ) {
7538: ivect = imat[i];
7539: k = ivect->head;
7540: if ( mpz_sgn(svect[k]) ) {
7541: maxrs = MAX(maxrs,rp0[i]->sugar);
7542: redv = nd_demand?ndv_load(rp0[i]->index)
7543: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7544: len = LEN(redv); mr = BDY(redv);
1.6 noro 7545: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7546: mpz_div(cs,svect[k],gcd);
1.6 noro 7547: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7548: mpz_neg(cs,cs);
1.9 noro 7549: if ( MUNIMPZ(cr) )
7550: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7551: else if ( !UNIMPZ(cr) )
7552: for ( j = 0; j < col; j++ ) {
7553: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7554: }
1.4 noro 7555: mpz_set_ui(svect[k],0);
7556: prev = k;
7557: switch ( ivect->width ) {
7558: case 1:
7559: ivc = ivect->index.c;
7560: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7561: pos = prev+ivc[j]; prev = pos;
1.6 noro 7562: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7563: }
7564: break;
7565: case 2:
7566: ivs = ivect->index.s;
7567: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7568: pos = prev+ivs[j]; prev = pos;
1.6 noro 7569: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7570: }
7571: break;
7572: case 4:
7573: ivi = ivect->index.i;
7574: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7575: pos = prev+ivi[j]; prev = pos;
1.6 noro 7576: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7577: }
7578: break;
7579: }
7580: for ( j = k+1; j < col && !svect[j]; j++ );
7581: if ( j == col ) break;
7582: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7583: mpz_removecont_array(svect,col);
7584: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7585: }
7586: }
7587: }
7588: mpz_removecont_array(svect,col);
7589: if ( DP_Print ) {
7590: fprintf(asir_out,"-"); fflush(asir_out);
7591: }
7592: for ( i = 0; i < col; i++ )
7593: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7594: else svect0[i] = 0;
7595: return maxrs;
7596: }
7597: #endif
1.1 noro 7598:
1.40 noro 7599: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 7600: {
7601: int i,j,k,len,pos,prev;
7602: UINT c,c1,c2,c3,up,lo,dmy;
7603: IndArray ivect;
7604: unsigned char *ivc;
7605: unsigned short *ivs;
7606: unsigned int *ivi;
7607: NDV redv;
7608: NMV mr;
7609: NODE rp;
7610: int maxrs;
7611:
7612: maxrs = 0;
7613: for ( i = 0; i < nred; i++ ) {
7614: ivect = imat[i];
7615: k = ivect->head; svect[k] %= m;
1.40 noro 7616: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 7617: maxrs = MAX(maxrs,rp0[i]->sugar);
7618: c = m-c; redv = nd_ps[rp0[i]->index];
7619: len = LEN(redv); mr = BDY(redv);
7620: svect[k] = 0; prev = k;
7621: switch ( ivect->width ) {
7622: case 1:
7623: ivc = ivect->index.c;
7624: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7625: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 7626: if ( c1 ) {
7627: c2 = svect[pos];
1.1 noro 7628: DMA(c1,c,c2,up,lo);
7629: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7630: } else svect[pos] = lo;
1.40 noro 7631: }
1.1 noro 7632: }
7633: break;
7634: case 2:
7635: ivs = ivect->index.s;
7636: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7637: pos = prev+ivs[j]; c1 = CM(mr);
7638: prev = pos;
1.40 noro 7639: if ( c1 ) {
7640: c2 = svect[pos];
1.1 noro 7641: DMA(c1,c,c2,up,lo);
7642: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7643: } else svect[pos] = lo;
1.40 noro 7644: }
1.1 noro 7645: }
7646: break;
7647: case 4:
7648: ivi = ivect->index.i;
7649: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7650: pos = prev+ivi[j]; c1 = CM(mr);
7651: prev = pos;
1.40 noro 7652: if ( c1 ) {
7653: c2 = svect[pos];
1.1 noro 7654: DMA(c1,c,c2,up,lo);
7655: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7656: } else svect[pos] = lo;
1.40 noro 7657: }
1.1 noro 7658: }
7659: break;
7660: }
7661: }
7662: }
7663: for ( i = 0; i < col; i++ )
7664: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7665: return maxrs;
7666: }
7667:
7668: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7669: {
7670: int i,j,k,len,pos,prev;
7671: UINT c,c1,c2,c3,up,lo,dmy;
7672: IndArray ivect;
7673: unsigned char *ivc;
7674: unsigned short *ivs;
7675: unsigned int *ivi;
7676: NDV redv;
7677: NMV mr;
7678: NODE rp;
7679: int maxrs;
7680:
7681: maxrs = 0;
7682: for ( i = 0; i < nred; i++ ) {
7683: ivect = imat[i];
7684: k = ivect->head;
7685: if ( (c = svect[k]) != 0 ) {
7686: maxrs = MAX(maxrs,rp0[i]->sugar);
7687: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7688: len = LEN(redv); mr = BDY(redv);
7689: svect[k] = 0; prev = k;
7690: switch ( ivect->width ) {
7691: case 1:
7692: ivc = ivect->index.c;
7693: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7694: pos = prev+ivc[j]; prev = pos;
7695: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7696: }
7697: break;
7698: case 2:
7699: ivs = ivect->index.s;
7700: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7701: pos = prev+ivs[j]; prev = pos;
7702: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7703: }
7704: break;
7705: case 4:
7706: ivi = ivect->index.i;
7707: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7708: pos = prev+ivi[j]; prev = pos;
7709: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7710: }
7711: break;
7712: }
7713: }
7714: }
7715: return maxrs;
7716: }
7717:
7718: ND nd_add_lf(ND p1,ND p2)
7719: {
7720: int n,c,can;
7721: ND r;
7722: NM m1,m2,mr0,mr,s;
7723: Z t;
7724:
7725: if ( !p1 ) return p2;
7726: else if ( !p2 ) return p1;
7727: else {
7728: can = 0;
7729: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7730: c = DL_COMPARE(DL(m1),DL(m2));
7731: switch ( c ) {
7732: case 0:
7733: addlf(CZ(m1),CZ(m2),&t);
7734: s = m1; m1 = NEXT(m1);
7735: if ( t ) {
7736: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7737: } else {
7738: can += 2; FREENM(s);
7739: }
7740: s = m2; m2 = NEXT(m2); FREENM(s);
7741: break;
7742: case 1:
7743: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7744: break;
7745: case -1:
7746: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7747: break;
7748: }
7749: }
7750: if ( !mr0 )
7751: if ( m1 ) mr0 = m1;
7752: else if ( m2 ) mr0 = m2;
7753: else return 0;
7754: else if ( m1 ) NEXT(mr) = m1;
7755: else if ( m2 ) NEXT(mr) = m2;
7756: else NEXT(mr) = 0;
7757: BDY(p1) = mr0;
7758: SG(p1) = MAX(SG(p1),SG(p2));
7759: LEN(p1) = LEN(p1)+LEN(p2)-can;
7760: FREEND(p2);
7761: return p1;
7762: }
7763: }
7764:
7765: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7766: {
7767: int i,j,k,len,pos,prev;
7768: mpz_t c,mc,c1;
7769: IndArray ivect;
7770: unsigned char *ivc;
7771: unsigned short *ivs;
7772: unsigned int *ivi;
7773: NDV redv;
7774: NMV mr;
7775: NODE rp;
7776: int maxrs;
7777:
7778: maxrs = 0;
7779: lf_lazy = 1;
7780: for ( i = 0; i < nred; i++ ) {
7781: ivect = imat[i];
7782: k = ivect->head;
7783: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7784: if ( mpz_sgn(svect[k]) ) {
7785: maxrs = MAX(maxrs,rp0[i]->sugar);
7786: mpz_neg(svect[k],svect[k]);
7787: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7788: len = LEN(redv); mr = BDY(redv);
7789: prev = k;
7790: switch ( ivect->width ) {
7791: case 1:
7792: ivc = ivect->index.c;
7793: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7794: pos = prev+ivc[j]; prev = pos;
7795: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7796: }
7797: break;
7798: case 2:
7799: ivs = ivect->index.s;
7800: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7801: pos = prev+ivs[j]; prev = pos;
7802: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7803: }
7804: break;
7805: case 4:
7806: ivi = ivect->index.i;
7807: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7808: pos = prev+ivi[j]; prev = pos;
7809: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7810: }
7811: break;
7812: }
7813: mpz_set_ui(svect[k],0);
7814: }
7815: }
7816: lf_lazy=0;
7817: for ( i = 0; i < col; i++ ) {
7818: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7819: }
7820: return maxrs;
7821: }
7822:
7823: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7824: {
7825: int i,j,k,l,rank,s;
7826: mpz_t a,a1,inv;
7827: mpz_t *t,*pivot,*pk;
7828: mpz_t **mat;
7829: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7830: int size,size1;
7831:
7832: mpz_init(inv);
7833: mpz_init(a);
7834: mat = (mpz_t **)mat0;
7835: size = 0;
7836: for ( rank = 0, j = 0; j < col; j++ ) {
7837: for ( i = rank; i < row; i++ ) {
7838: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7839: }
7840: for ( i = rank; i < row; i++ )
7841: if ( mpz_sgn(mat[i][j]) )
7842: break;
7843: if ( i == row ) {
7844: colstat[j] = 0;
7845: continue;
7846: } else
7847: colstat[j] = 1;
7848: if ( i != rank ) {
7849: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7850: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7851: }
7852: pivot = mat[rank];
7853: s = sugar[rank];
7854: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7855: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7856: if ( mpz_sgn(*pk) ) {
7857: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7858: }
7859: for ( i = rank+1; i < row; i++ ) {
7860: t = mat[i];
7861: if ( mpz_sgn(t[j]) ) {
7862: sugar[i] = MAX(sugar[i],s);
7863: mpz_neg(a,t[j]);
7864: red_by_vect_lf(t+j,pivot+j,a,col-j);
7865: }
7866: }
7867: rank++;
7868: }
7869: for ( j = col-1, l = rank-1; j >= 0; j-- )
7870: if ( colstat[j] ) {
7871: pivot = mat[l];
7872: s = sugar[l];
7873: for ( k = j; k < col; k++ )
7874: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7875: for ( i = 0; i < l; i++ ) {
7876: t = mat[i];
7877: if ( mpz_sgn(t[j]) ) {
7878: sugar[i] = MAX(sugar[i],s);
7879: mpz_neg(a,t[j]);
7880: red_by_vect_lf(t+j,pivot+j,a,col-j);
7881: }
7882: }
7883: l--;
7884: }
7885: for ( j = 0, l = 0; l < rank; j++ )
7886: if ( colstat[j] ) {
7887: t = mat[l];
7888: for ( k = j; k < col; k++ ) {
7889: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7890: }
7891: l++;
7892: }
7893: return rank;
7894: }
7895:
7896:
7897: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7898: {
7899: int j,k,len;
7900: UINT *p;
7901: UINT c;
7902: NDV r;
7903: NMV mr0,mr;
7904:
7905: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7906: if ( !len ) return 0;
7907: else {
7908: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7909: #if 0
7910: ndv_alloc += nmv_adv*len;
7911: #endif
7912: mr = mr0;
7913: p = s0vect;
7914: for ( j = k = 0; j < col; j++, p += nd_wpd )
7915: if ( !rhead[j] ) {
7916: if ( (c = vect[k++]) != 0 ) {
7917: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7918: }
7919: }
7920: MKNDV(nd_nvar,mr0,len,r);
7921: return r;
7922: }
7923: }
7924:
1.40 noro 7925: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
7926: {
7927: int j,k,len;
7928: UINT *p;
7929: UINT c;
7930: NDV r;
7931: NMV mr0,mr;
7932:
7933: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7934: if ( !len ) return 0;
7935: else {
7936: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7937: mr = mr0;
7938: p = s0vect;
7939: for ( j = k = 0; j < col; j++, p += nd_wpd )
7940: if ( (c = vect[k++]) != 0 ) {
7941: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7942: }
7943: MKNDV(nd_nvar,mr0,len,r);
7944: return r;
7945: }
7946: }
7947:
1.1 noro 7948: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7949: {
7950: int j,k,len;
7951: UINT *p;
7952: NDV r;
7953: NMV mr0,mr;
7954:
7955: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7956: if ( !len ) return 0;
7957: else {
7958: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7959: mr = mr0;
7960: p = s0vect;
7961: for ( j = 0; j < col; j++, p += nd_wpd )
7962: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7963: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7964: }
7965: MKNDV(nd_nvar,mr0,len,r);
7966: return r;
7967: }
7968: }
7969:
7970: /* for preprocessed vector */
7971:
7972: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7973: {
1.6 noro 7974: int j,k,len;
7975: UINT *p;
7976: Z c;
7977: NDV r;
7978: NMV mr0,mr;
1.1 noro 7979:
1.6 noro 7980: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7981: if ( !len ) return 0;
7982: else {
7983: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7984: #if 0
1.6 noro 7985: ndv_alloc += nmv_adv*len;
1.1 noro 7986: #endif
1.6 noro 7987: mr = mr0;
7988: p = s0vect;
7989: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7990: if ( !rhead[j] ) {
7991: if ( (c = vect[k++]) != 0 ) {
7992: if ( !INT(c) )
7993: error("vect_to_ndv_q : components must be integers");
7994: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7995: }
7996: }
1.1 noro 7997: }
1.6 noro 7998: MKNDV(nd_nvar,mr0,len,r);
7999: return r;
8000: }
1.1 noro 8001: }
8002:
8003: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8004: {
8005: int j,k,len;
8006: UINT *p;
8007: mpz_t c;
8008: NDV r;
8009: NMV mr0,mr;
8010:
8011: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8012: if ( !len ) return 0;
8013: else {
8014: mr0 = (NMV)MALLOC(nmv_adv*len);
8015: #if 0
8016: ndv_alloc += nmv_adv*len;
8017: #endif
8018: mr = mr0;
8019: p = s0vect;
8020: for ( j = k = 0; j < col; j++, p += nd_wpd )
8021: if ( !rhead[j] ) {
8022: c[0] = vect[k++][0];
8023: if ( mpz_sgn(c) ) {
8024: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8025: }
8026: }
8027: MKNDV(nd_nvar,mr0,len,r);
8028: return r;
8029: }
8030: }
8031:
8032: /* for plain vector */
8033:
8034: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8035: {
8036: int j,k,len;
8037: UINT *p;
8038: Z c;
8039: NDV r;
8040: NMV mr0,mr;
8041:
8042: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8043: if ( !len ) return 0;
8044: else {
8045: mr0 = (NMV)MALLOC(nmv_adv*len);
8046: #if 0
8047: ndv_alloc += nmv_adv*len;
8048: #endif
8049: mr = mr0;
8050: p = s0vect;
8051: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8052: if ( (c = vect[k]) != 0 ) {
8053: if ( !INT(c) )
1.6 noro 8054: error("plain_vect_to_ndv_q : components must be integers");
8055: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8056: }
8057: MKNDV(nd_nvar,mr0,len,r);
8058: return r;
8059: }
8060: }
8061:
8062: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8063: {
8064: ND_pairs t;
8065: NODE sp0,sp;
8066: int stat;
8067: ND spol;
8068:
8069: for ( t = l; t; t = NEXT(t) ) {
8070: stat = nd_sp(m,trace,t,&spol);
8071: if ( !stat ) return 0;
8072: if ( spol ) {
8073: add_pbucket_symbolic(bucket,spol);
8074: }
8075: }
8076: return 1;
8077: }
8078:
8079: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8080: {
8081: NODE rp0,rp;
8082: NM mul,head,s0,s;
8083: int index,col,i,sugar;
8084: RHist h;
8085: UINT *s0v,*p;
8086: NM_ind_pair pair;
8087: ND red;
8088: NDV *ps;
8089:
8090: s0 = 0; rp0 = 0; col = 0;
8091: if ( nd_demand )
8092: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8093: else
8094: ps = trace?nd_ps_trace:nd_ps;
8095: while ( 1 ) {
8096: head = remove_head_pbucket_symbolic(bucket);
8097: if ( !head ) break;
8098: if ( !s0 ) s0 = head;
8099: else NEXT(s) = head;
8100: s = head;
8101: index = ndl_find_reducer(DL(head));
8102: if ( index >= 0 ) {
8103: h = nd_psh[index];
8104: NEWNM(mul);
8105: ndl_sub(DL(head),DL(h),DL(mul));
8106: if ( ndl_check_bound2(index,DL(mul)) )
8107: return 0;
8108: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8109: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8110: red = ndv_mul_nm_symbolic(mul,ps[index]);
8111: add_pbucket_symbolic(bucket,nd_remove_head(red));
8112: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8113: }
8114: col++;
8115: }
8116: if ( rp0 ) NEXT(rp) = 0;
8117: NEXT(s) = 0;
8118: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8119: for ( i = 0, p = s0v, s = s0; i < col;
8120: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8121: *s0vect = s0v;
8122: *r = rp0;
8123: return col;
8124: }
8125:
8126: void print_ndp(ND_pairs l)
8127: {
8128: ND_pairs t;
8129:
8130: for ( t = l; t; t = NEXT(t) )
8131: printf("[%d,%d] ",t->i1,t->i2);
8132: printf("\n");
8133: }
8134:
8135: NODE nd_f4(int m,int checkonly,int **indp)
8136: {
8137: int i,nh,stat,index,f4red;
8138: NODE r,g,tn0,tn,node;
8139: ND_pairs d,l,t,ll0,ll,lh;
8140: LIST l0,l1;
8141: ND spol,red;
8142: NDV nf,redv;
8143: NM s0,s;
8144: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8145: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8146: UINT c;
8147: UINT **spmat;
8148: UINT *s0vect,*svect,*p,*v;
8149: int *colstat;
8150: IndArray *imat;
8151: int *rhead;
8152: int spcol,sprow;
8153: int sugar,sugarh;
8154: PGeoBucket bucket;
8155: struct oEGT eg0,eg1,eg_f4;
8156: Z i1,i2,sugarq;
1.12 noro 8157:
8158: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8159: #if 0
8160: ndv_alloc = 0;
8161: #endif
1.11 noro 8162: Nf4_red=0;
1.1 noro 8163: g = 0; d = 0;
8164: for ( i = 0; i < nd_psn; i++ ) {
8165: d = update_pairs(d,g,i,0);
8166: g = update_base(g,i);
8167: }
8168: nzlist = 0;
8169: nzlist_t = nd_nzlist;
8170: f4red = 1;
8171: nd_last_nonzero = 0;
8172: while ( d ) {
8173: get_eg(&eg0);
8174: l = nd_minsugarp(d,&d);
8175: sugar = nd_sugarweight?l->sugar2:SG(l);
8176: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8177: if ( nzlist_t ) {
8178: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8179: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8180: tn = BDY((LIST)ARG1(node));
8181: if ( !tn ) {
8182: nzlist_t = NEXT(nzlist_t);
8183: continue;
8184: }
8185: /* tn = [[i1,i2],...] */
8186: lh = nd_ipairtospair(tn);
8187: }
8188: bucket = create_pbucket();
8189: stat = nd_sp_f4(m,0,l,bucket);
8190: if ( !stat ) {
8191: for ( t = l; NEXT(t); t = NEXT(t) );
8192: NEXT(t) = d; d = l;
8193: d = nd_reconstruct(0,d);
8194: continue;
8195: }
8196: if ( bucket->m < 0 ) continue;
8197: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8198: if ( !col ) {
8199: for ( t = l; NEXT(t); t = NEXT(t) );
8200: NEXT(t) = d; d = l;
8201: d = nd_reconstruct(0,d);
8202: continue;
8203: }
1.12 noro 8204: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8205: if ( DP_Print )
1.6 noro 8206: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8207: sugar,eg_f4.exectime);
1.1 noro 8208: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8209: if ( checkonly && nflist ) return 0;
8210: /* adding new bases */
8211: if ( nflist ) nd_last_nonzero = f4red;
8212: for ( r = nflist; r; r = NEXT(r) ) {
8213: nf = (NDV)BDY(r);
1.38 noro 8214: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8215: ndv_removecont(m,nf);
8216: if ( !m && nd_nalg ) {
8217: ND nf1;
8218:
8219: nf1 = ndvtond(m,nf);
8220: nd_monic(0,&nf1);
8221: nd_removecont(m,nf1);
8222: nf = ndtondv(m,nf1);
8223: }
1.24 noro 8224: nh = ndv_newps(m,nf,0);
1.1 noro 8225: d = update_pairs(d,g,nh,0);
8226: g = update_base(g,nh);
8227: }
8228: if ( DP_Print ) {
8229: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8230: }
8231: if ( nd_gentrace ) {
8232: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8233: NEXTNODE(tn0,tn);
1.6 noro 8234: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8235: node = mknode(2,i1,i2); MKLIST(l0,node);
8236: BDY(tn) = l0;
8237: }
8238: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8239: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8240: MKNODE(node,l1,nzlist); nzlist = node;
8241: }
8242: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8243: f4red++;
8244: if ( nd_f4red && f4red > nd_f4red ) break;
8245: if ( nd_rank0 && !nflist ) break;
8246: }
8247: if ( nd_gentrace ) {
8248: MKLIST(l0,reverse_node(nzlist));
8249: MKNODE(nd_alltracelist,l0,0);
8250: }
8251: #if 0
8252: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8253: #endif
1.12 noro 8254: if ( DP_Print ) {
8255: fprintf(asir_out,"number of red=%d,",Nf4_red);
8256: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8257: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8258: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8259: }
1.1 noro 8260: conv_ilist(nd_demand,0,g,indp);
8261: return g;
8262: }
8263:
8264: NODE nd_f4_trace(int m,int **indp)
8265: {
8266: int i,nh,stat,index;
8267: NODE r,g;
8268: ND_pairs d,l,l0,t;
8269: ND spol,red;
8270: NDV nf,redv,nfqv,nfv;
8271: NM s0,s;
8272: NODE rp0,srp0,nflist;
8273: int nsp,nred,col,rank,len,k,j,a;
8274: UINT c;
8275: UINT **spmat;
8276: UINT *s0vect,*svect,*p,*v;
8277: int *colstat;
8278: IndArray *imat;
8279: int *rhead;
8280: int spcol,sprow;
8281: int sugar;
8282: PGeoBucket bucket;
8283: struct oEGT eg0,eg1,eg_f4;
8284:
8285: g = 0; d = 0;
8286: for ( i = 0; i < nd_psn; i++ ) {
8287: d = update_pairs(d,g,i,0);
8288: g = update_base(g,i);
8289: }
8290: while ( d ) {
8291: get_eg(&eg0);
8292: l = nd_minsugarp(d,&d);
8293: sugar = SG(l);
8294: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8295: bucket = create_pbucket();
8296: stat = nd_sp_f4(m,0,l,bucket);
8297: if ( !stat ) {
8298: for ( t = l; NEXT(t); t = NEXT(t) );
8299: NEXT(t) = d; d = l;
8300: d = nd_reconstruct(1,d);
8301: continue;
8302: }
8303: if ( bucket->m < 0 ) continue;
8304: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8305: if ( !col ) {
8306: for ( t = l; NEXT(t); t = NEXT(t) );
8307: NEXT(t) = d; d = l;
8308: d = nd_reconstruct(1,d);
8309: continue;
8310: }
8311: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8312: if ( DP_Print )
1.3 noro 8313: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8314: sugar,eg_f4.exectime);
1.1 noro 8315: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8316: if ( !l0 ) continue;
8317: l = l0;
8318:
8319: /* over Q */
8320: bucket = create_pbucket();
8321: stat = nd_sp_f4(0,1,l,bucket);
8322: if ( !stat ) {
8323: for ( t = l; NEXT(t); t = NEXT(t) );
8324: NEXT(t) = d; d = l;
8325: d = nd_reconstruct(1,d);
8326: continue;
8327: }
8328: if ( bucket->m < 0 ) continue;
8329: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8330: if ( !col ) {
8331: for ( t = l; NEXT(t); t = NEXT(t) );
8332: NEXT(t) = d; d = l;
8333: d = nd_reconstruct(1,d);
8334: continue;
8335: }
8336: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8337: /* adding new bases */
8338: for ( r = nflist; r; r = NEXT(r) ) {
8339: nfqv = (NDV)BDY(r);
8340: ndv_removecont(0,nfqv);
1.6 noro 8341: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8342: if ( nd_nalg ) {
8343: ND nf1;
8344:
8345: nf1 = ndvtond(m,nfqv);
8346: nd_monic(0,&nf1);
8347: nd_removecont(0,nf1);
8348: nfqv = ndtondv(0,nf1); nd_free(nf1);
8349: }
8350: nfv = ndv_dup(0,nfqv);
8351: ndv_mod(m,nfv);
8352: ndv_removecont(m,nfv);
1.24 noro 8353: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8354: d = update_pairs(d,g,nh,0);
8355: g = update_base(g,nh);
8356: }
8357: }
8358: #if 0
8359: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8360: #endif
8361: conv_ilist(nd_demand,1,g,indp);
8362: return g;
8363: }
8364:
8365: int rref(matrix mat,int *sugar)
8366: {
8367: int row,col,i,j,k,l,s,wcol,wj;
8368: unsigned long bj;
8369: unsigned long **a;
8370: unsigned long *ai,*ak,*as,*t;
8371: int *pivot;
8372:
8373: row = mat->row;
8374: col = mat->col;
8375: a = mat->a;
8376: wcol = (col+BLEN-1)/BLEN;
8377: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8378: i = 0;
8379: for ( j = 0; j < col; j++ ) {
8380: wj = j/BLEN; bj = 1L<<(j%BLEN);
8381: for ( k = i; k < row; k++ )
8382: if ( a[k][wj] & bj ) break;
8383: if ( k == row ) continue;
8384: pivot[i] = j;
8385: if ( k != i ) {
8386: t = a[i]; a[i] = a[k]; a[k] = t;
8387: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8388: }
8389: ai = a[i];
8390: for ( k = i+1; k < row; k++ ) {
8391: ak = a[k];
8392: if ( ak[wj] & bj ) {
8393: for ( l = wj; l < wcol; l++ )
8394: ak[l] ^= ai[l];
8395: sugar[k] = MAX(sugar[k],sugar[i]);
8396: }
8397: }
8398: i++;
8399: }
8400: for ( k = i-1; k >= 0; k-- ) {
8401: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8402: ak = a[k];
8403: for ( s = 0; s < k; s++ ) {
8404: as = a[s];
8405: if ( as[wj] & bj ) {
8406: for ( l = wj; l < wcol; l++ )
8407: as[l] ^= ak[l];
8408: sugar[s] = MAX(sugar[s],sugar[k]);
8409: }
8410: }
8411: }
8412: return i;
8413: }
8414:
8415: void print_matrix(matrix mat)
8416: {
8417: int row,col,i,j;
8418: unsigned long *ai;
8419:
8420: row = mat->row;
8421: col = mat->col;
8422: printf("%d x %d\n",row,col);
8423: for ( i = 0; i < row; i++ ) {
8424: ai = mat->a[i];
8425: for ( j = 0; j < col; j++ ) {
8426: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8427: else putchar('0');
8428: }
8429: putchar('\n');
8430: }
8431: }
8432:
8433: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8434:
8435: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8436: {
8437: int row,col,wcol,wj,i,j;
8438: unsigned long bj;
8439: unsigned long *ai;
8440: unsigned long **a;
8441: int len;
8442: int *pos;
8443:
8444: row = mat->row;
8445: col = mat->col;
8446: wcol = (col+BLEN-1)/BLEN;
8447: pos = (int *)MALLOC(wcol*sizeof(int));
8448: bzero(pos,wcol*sizeof(int));
8449: for ( i = j = 0; i < wcol; i++ )
8450: if ( v[i] ) pos[j++] = i;;
8451: len = j;
8452: wj = rhead/BLEN;
8453: bj = 1L<<rhead%BLEN;
8454: a = mat->a;
8455: for ( i = 0; i < row; i++ ) {
8456: ai = a[i];
8457: if ( ai[wj]&bj ) {
8458: for ( j = 0; j < len; j++ )
8459: ai[pos[j]] ^= v[pos[j]];
8460: sugar[i] = MAX(sugar[i],rsugar);
8461: }
8462: }
8463: }
8464:
8465: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8466: {
8467: int nsp,nred,i,i0,k,rank,row;
8468: NODE r0,rp;
8469: ND_pairs sp;
8470: ND spol;
8471: NM_ind_pair rt;
8472: int *s0hash;
8473: UINT *s;
8474: int *pivot,*sugar,*head;
8475: matrix mat;
8476: NM m;
8477: NODE r;
8478: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8479: int rhead,rsugar,size;
8480: unsigned long *v;
8481:
8482: get_eg(&eg0);
8483: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8484: nred = length(rp0);
8485: mat = alloc_matrix(nsp,col);
8486: s0hash = (int *)MALLOC(col*sizeof(int));
8487: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8488: s0hash[i] = ndl_hash_value(s);
8489:
8490: sugar = (int *)MALLOC(nsp*sizeof(int));
8491: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8492: nd_sp(2,0,sp,&spol);
8493: if ( spol ) {
8494: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8495: sugar[i] = SG(spol);
8496: i++;
8497: }
8498: }
8499: mat->row = i;
8500: if ( DP_Print ) {
8501: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8502: }
8503: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8504: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8505: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8506: rt = (NM_ind_pair)BDY(rp);
8507: bzero(v,size);
8508: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8509: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8510: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8511: }
8512:
8513: get_eg(&eg1);
8514: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8515: rank = rref(mat,sugar);
8516:
8517: for ( i = 0, r0 = 0; i < rank; i++ ) {
8518: NEXTNODE(r0,r);
8519: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8520: SG((NDV)BDY(r)) = sugar[i];
8521: }
8522: if ( r0 ) NEXT(r) = 0;
8523: get_eg(&eg2);
8524: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8525: if ( DP_Print ) {
8526: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8527: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8528: fflush(asir_out);
8529: }
8530: return r0;
8531: }
8532:
8533:
8534: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8535: {
8536: IndArray *imat;
1.11 noro 8537: int nsp,nred,i,start;
1.1 noro 8538: int *rhead;
8539: NODE r0,rp;
8540: ND_pairs sp;
8541: NM_ind_pair *rvect;
8542: UINT *s;
8543: int *s0hash;
1.11 noro 8544: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8545:
8546: if ( m == 2 && nd_rref2 )
8547: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8548:
8549: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8550: nred = length(rp0);
8551: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8552: rhead = (int *)MALLOC(col*sizeof(int));
8553: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8554:
8555: /* construction of index arrays */
1.11 noro 8556: get_eg(&eg0);
1.1 noro 8557: if ( DP_Print ) {
1.11 noro 8558: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8559: fflush(asir_out);
1.1 noro 8560: }
8561: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8562: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8563: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8564: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8565: rhead[imat[i]->head] = 1;
1.11 noro 8566: start = imat[i]->head;
8567: }
1.12 noro 8568: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8569: if ( DP_Print ) {
8570: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8571: fflush(asir_out);
1.1 noro 8572: }
8573: if ( m > 0 )
1.7 noro 8574: #if SIZEOF_LONG==8
1.1 noro 8575: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8576: #else
8577: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8578: #endif
8579: else if ( m == -1 )
8580: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8581: else if ( m == -2 )
8582: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8583: else
8584: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8585: return r0;
8586: }
8587:
8588: /* for Fp, 2<=p<2^16 */
8589:
8590: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8591: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8592: {
8593: int spcol,sprow,a;
8594: int i,j,k,l,rank;
8595: NODE r0,r;
8596: ND_pairs sp;
8597: ND spol;
8598: UINT **spmat;
8599: UINT *svect,*v;
8600: int *colstat;
8601: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8602: int maxrs;
8603: int *spsugar;
8604: ND_pairs *spactive;
8605:
8606: spcol = col-nred;
8607: get_eg(&eg0);
8608: /* elimination (1st step) */
8609: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8610: svect = (UINT *)MALLOC(col*sizeof(UINT));
8611: spsugar = (int *)MALLOC(nsp*sizeof(int));
8612: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8613: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8614: nd_sp(m,0,sp,&spol);
8615: if ( !spol ) continue;
8616: nd_to_vect(m,s0vect,col,spol,svect);
8617: if ( m == -1 )
8618: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8619: else
1.40 noro 8620: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 8621: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8622: if ( i < col ) {
8623: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8624: for ( j = k = 0; j < col; j++ )
8625: if ( !rhead[j] ) v[k++] = svect[j];
8626: spsugar[sprow] = MAX(maxrs,SG(spol));
8627: if ( nz )
8628: spactive[sprow] = sp;
8629: sprow++;
8630: }
8631: nd_free(spol);
8632: }
8633: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8634: if ( DP_Print ) {
1.5 noro 8635: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8636: fflush(asir_out);
8637: }
8638: /* free index arrays */
8639: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8640:
8641: /* elimination (2nd step) */
8642: colstat = (int *)MALLOC(spcol*sizeof(int));
8643: if ( m == -1 )
8644: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8645: else
8646: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8647: r0 = 0;
8648: for ( i = 0; i < rank; i++ ) {
8649: NEXTNODE(r0,r); BDY(r) =
8650: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8651: SG((NDV)BDY(r)) = spsugar[i];
8652: GCFREE(spmat[i]);
8653: }
8654: if ( r0 ) NEXT(r) = 0;
8655:
8656: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8657: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8658: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8659: if ( DP_Print ) {
1.5 noro 8660: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8661: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8662: nsp,nred,sprow,spcol,rank);
1.5 noro 8663: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8664: }
8665: if ( nz ) {
8666: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8667: if ( rank > 0 ) {
8668: NEXT(spactive[rank-1]) = 0;
8669: *nz = spactive[0];
8670: } else
8671: *nz = 0;
8672: }
8673: return r0;
8674: }
8675:
1.40 noro 8676: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8677: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
8678: {
8679: int spcol,sprow,a;
8680: int i,j,k,l,rank;
8681: NODE r0,r;
8682: ND_pairs sp;
8683: ND spol;
8684: UINT **spmat;
8685: UINT *svect,*cvect;
8686: UINT *v;
8687: int *colstat;
8688: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8689: int maxrs;
8690: int *spsugar;
8691: ND_pairs *spactive;
8692: SIG *spsig;
8693:
8694: get_eg(&eg0);
8695: /* elimination (1st step) */
8696: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8697: spsugar = (int *)MALLOC(nsp*sizeof(int));
8698: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
8699: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8700: nd_sp(m,0,sp,&spol);
8701: if ( !spol ) {
8702: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8703: continue;
8704: }
8705: svect = (UINT *)MALLOC(col*sizeof(UINT));
8706: nd_to_vect(m,s0vect,col,spol,svect);
8707: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
8708: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8709: if ( i < col ) {
8710: spmat[sprow] = svect;
8711: spsugar[sprow] = MAX(maxrs,SG(spol));
8712: spsig[sprow] = sp->sig;
8713: sprow++;
8714: } else {
8715: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8716: }
8717: nd_free(spol);
8718: }
8719: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
8720: if ( DP_Print ) {
8721: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
8722: fflush(asir_out);
8723: }
8724: /* free index arrays */
8725: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8726:
8727: /* elimination (2nd step) */
8728: colstat = (int *)MALLOC(col*sizeof(int));
8729: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
8730: r0 = 0;
8731: for ( i = 0; i < sprow; i++ ) {
8732: if ( spsugar[i] >= 0 ) {
8733: NEXTNODE(r0,r);
8734: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
8735: SG((NDV)BDY(r)) = spsugar[i];
8736: ((NDV)BDY(r))->sig = spsig[i];
8737: } else
8738: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
8739: GCFREE(spmat[i]);
8740: }
8741: if ( r0 ) NEXT(r) = 0;
8742: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
8743: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8744: if ( DP_Print ) {
8745: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
8746: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8747: nsp,nred,sprow,col,rank);
8748: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
8749: }
8750: return r0;
8751: }
8752:
1.1 noro 8753:
8754: /* for small finite fields */
8755:
8756: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8757: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8758: {
8759: int spcol,sprow,a;
8760: int i,j,k,l,rank;
8761: NODE r0,r;
8762: ND_pairs sp;
8763: ND spol;
8764: UINT **spmat;
8765: UINT *svect,*v;
8766: int *colstat;
8767: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8768: int maxrs;
8769: int *spsugar;
8770: ND_pairs *spactive;
8771:
8772: spcol = col-nred;
8773: get_eg(&eg0);
8774: /* elimination (1st step) */
8775: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8776: svect = (UINT *)MALLOC(col*sizeof(UINT));
8777: spsugar = (int *)MALLOC(nsp*sizeof(int));
8778: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8779: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8780: nd_sp(m,0,sp,&spol);
8781: if ( !spol ) continue;
8782: nd_to_vect(m,s0vect,col,spol,svect);
8783: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8784: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8785: if ( i < col ) {
8786: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8787: for ( j = k = 0; j < col; j++ )
8788: if ( !rhead[j] ) v[k++] = svect[j];
8789: spsugar[sprow] = MAX(maxrs,SG(spol));
8790: if ( nz )
8791: spactive[sprow] = sp;
8792: sprow++;
8793: }
8794: nd_free(spol);
8795: }
8796: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8797: if ( DP_Print ) {
1.5 noro 8798: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8799: fflush(asir_out);
8800: }
8801: /* free index arrays */
8802: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8803:
8804: /* elimination (2nd step) */
8805: colstat = (int *)MALLOC(spcol*sizeof(int));
8806: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8807: r0 = 0;
8808: for ( i = 0; i < rank; i++ ) {
8809: NEXTNODE(r0,r); BDY(r) =
8810: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8811: SG((NDV)BDY(r)) = spsugar[i];
8812: GCFREE(spmat[i]);
8813: }
8814: if ( r0 ) NEXT(r) = 0;
8815:
8816: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8817: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8818: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8819: if ( DP_Print ) {
1.5 noro 8820: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8821: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8822: nsp,nred,sprow,spcol,rank);
1.5 noro 8823: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8824: }
8825: if ( nz ) {
8826: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8827: if ( rank > 0 ) {
8828: NEXT(spactive[rank-1]) = 0;
8829: *nz = spactive[0];
8830: } else
8831: *nz = 0;
8832: }
8833: return r0;
8834: }
8835:
8836: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8837: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8838: {
8839: int spcol,sprow,a;
8840: int i,j,k,l,rank;
8841: NODE r0,r;
8842: ND_pairs sp;
8843: ND spol;
8844: mpz_t **spmat;
8845: mpz_t *svect,*v;
8846: int *colstat;
8847: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8848: int maxrs;
8849: int *spsugar;
8850: pointer *w;
8851:
8852: spcol = col-nred;
8853: get_eg(&eg0);
8854: /* elimination (1st step) */
8855: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8856: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8857: spsugar = (int *)MALLOC(nsp*sizeof(int));
8858: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8859: nd_sp(m,trace,sp,&spol);
8860: if ( !spol ) continue;
8861: nd_to_vect_lf(s0vect,col,spol,svect);
8862: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8863: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8864: if ( i < col ) {
8865: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8866: for ( j = k = 0; j < col; j++ )
8867: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8868: spsugar[sprow] = MAX(maxrs,SG(spol));
8869: sprow++;
8870: }
8871: /* nd_free(spol); */
8872: }
8873: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8874: if ( DP_Print ) {
1.5 noro 8875: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8876: fflush(asir_out);
8877: }
8878: /* free index arrays */
8879: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8880:
8881: /* elimination (2nd step) */
8882: colstat = (int *)MALLOC(spcol*sizeof(int));
8883: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8884: w = (pointer *)MALLOC(rank*sizeof(pointer));
8885: for ( i = 0; i < rank; i++ ) {
8886: #if 0
8887: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8888: SG((NDV)w[rank-i-1]) = spsugar[i];
8889: #else
8890: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8891: SG((NDV)w[i]) = spsugar[i];
8892: #endif
8893: /* GCFREE(spmat[i]); */
8894:
8895: }
8896: #if 0
8897: qsort(w,rank,sizeof(NDV),
8898: (int (*)(const void *,const void *))ndv_compare);
8899: #endif
8900: r0 = 0;
8901: for ( i = 0; i < rank; i++ ) {
8902: NEXTNODE(r0,r); BDY(r) = w[i];
8903: }
8904: if ( r0 ) NEXT(r) = 0;
8905:
8906: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8907: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8908: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8909: if ( DP_Print ) {
1.5 noro 8910: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8911: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8912: nsp,nred,sprow,spcol,rank);
1.5 noro 8913: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8914: }
8915: return r0;
8916: }
8917:
8918: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8919: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8920: {
8921: int spcol,sprow,a;
8922: int i,j,k,l,rank;
8923: NODE r0,r;
8924: ND_pairs sp;
8925: ND spol;
8926: Z **spmat;
8927: Z *svect,*v;
8928: int *colstat;
8929: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8930: int maxrs;
8931: int *spsugar;
8932: pointer *w;
8933:
8934: spcol = col-nred;
8935: get_eg(&eg0);
8936: /* elimination (1st step) */
8937: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8938: svect = (Z *)MALLOC(col*sizeof(Q));
8939: spsugar = (int *)MALLOC(nsp*sizeof(int));
8940: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8941: nd_sp(0,trace,sp,&spol);
8942: if ( !spol ) continue;
8943: nd_to_vect_q(s0vect,col,spol,svect);
8944: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8945: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8946: if ( i < col ) {
8947: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8948: for ( j = k = 0; j < col; j++ )
8949: if ( !rhead[j] ) v[k++] = svect[j];
8950: spsugar[sprow] = MAX(maxrs,SG(spol));
8951: sprow++;
8952: }
8953: /* nd_free(spol); */
8954: }
8955: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8956: if ( DP_Print ) {
1.5 noro 8957: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8958: fflush(asir_out);
8959: }
8960: /* free index arrays */
8961: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8962:
8963: /* elimination (2nd step) */
8964: colstat = (int *)MALLOC(spcol*sizeof(int));
8965: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8966: w = (pointer *)MALLOC(rank*sizeof(pointer));
8967: for ( i = 0; i < rank; i++ ) {
8968: #if 0
8969: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8970: SG((NDV)w[rank-i-1]) = spsugar[i];
8971: #else
8972: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8973: SG((NDV)w[i]) = spsugar[i];
8974: #endif
8975: /* GCFREE(spmat[i]); */
8976: }
8977: #if 0
8978: qsort(w,rank,sizeof(NDV),
8979: (int (*)(const void *,const void *))ndv_compare);
8980: #endif
8981: r0 = 0;
8982: for ( i = 0; i < rank; i++ ) {
8983: NEXTNODE(r0,r); BDY(r) = w[i];
8984: }
8985: if ( r0 ) NEXT(r) = 0;
8986:
8987: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8988: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8989: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8990: if ( DP_Print ) {
1.5 noro 8991: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8992: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8993: nsp,nred,sprow,spcol,rank);
1.5 noro 8994: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8995: }
8996: return r0;
8997: }
8998:
8999: FILE *nd_write,*nd_read;
9000:
9001: void nd_send_int(int a) {
9002: write_int(nd_write,(unsigned int *)&a);
9003: }
9004:
9005: void nd_send_intarray(int *p,int len) {
9006: write_intarray(nd_write,(unsigned int *)p,len);
9007: }
9008:
9009: int nd_recv_int() {
9010: int a;
9011:
9012: read_int(nd_read,(unsigned int *)&a);
9013: return a;
9014: }
9015:
9016: void nd_recv_intarray(int *p,int len) {
9017: read_intarray(nd_read,(unsigned int *)p,len);
9018: }
9019:
9020: void nd_send_ndv(NDV p) {
9021: int len,i;
9022: NMV m;
9023:
9024: if ( !p ) nd_send_int(0);
9025: else {
9026: len = LEN(p);
9027: nd_send_int(len);
9028: m = BDY(p);
9029: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9030: nd_send_int(CM(m));
9031: nd_send_intarray((int *)DL(m),nd_wpd);
9032: }
9033: }
9034: }
9035:
9036: void nd_send_nd(ND p) {
9037: int len,i;
9038: NM m;
9039:
9040: if ( !p ) nd_send_int(0);
9041: else {
9042: len = LEN(p);
9043: nd_send_int(len);
9044: m = BDY(p);
9045: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9046: nd_send_int(CM(m));
9047: nd_send_intarray((int *)DL(m),nd_wpd);
9048: }
9049: }
9050: }
9051:
9052: NDV nd_recv_ndv()
9053: {
9054: int len,i;
9055: NMV m,m0;
9056: NDV r;
9057:
9058: len = nd_recv_int();
9059: if ( !len ) return 0;
9060: else {
9061: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9062: #if 0
9063: ndv_alloc += len*nmv_adv;
9064: #endif
9065: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9066: CM(m) = nd_recv_int();
9067: nd_recv_intarray((int *)DL(m),nd_wpd);
9068: }
9069: MKNDV(nd_nvar,m0,len,r);
9070: return r;
9071: }
9072: }
9073:
9074: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9075: {
9076: int i,j,t,c,rank,inv;
9077: int *ci,*ri;
9078: Z dn;
9079: MAT m,nm;
9080:
9081: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9082: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9083: for ( i = 0; i < row; i++ )
9084: for ( j = 0; j < col; j++ )
9085: mat0[i][j] = 0;
9086: c = col-rank;
9087: for ( i = 0; i < rank; i++ ) {
9088: mat0[i][ri[i]] = dn;
9089: for ( j = 0; j < c; j++ )
9090: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9091: }
9092: return rank;
9093: }
9094:
9095: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9096: {
9097: int i,j,k,l,inv,a,rank,s;
9098: unsigned int *t,*pivot,*pk;
9099: unsigned int **mat;
9100: ND_pairs pair;
9101:
9102: mat = (unsigned int **)mat0;
9103: for ( rank = 0, j = 0; j < col; j++ ) {
9104: for ( i = rank; i < row; i++ )
9105: mat[i][j] %= md;
9106: for ( i = rank; i < row; i++ )
9107: if ( mat[i][j] )
9108: break;
9109: if ( i == row ) {
9110: colstat[j] = 0;
9111: continue;
9112: } else
9113: colstat[j] = 1;
9114: if ( i != rank ) {
9115: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9116: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9117: if ( spactive ) {
9118: pair = spactive[i]; spactive[i] = spactive[rank];
9119: spactive[rank] = pair;
9120: }
9121: }
9122: pivot = mat[rank];
9123: s = sugar[rank];
9124: inv = invm(pivot[j],md);
9125: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9126: if ( *pk ) {
9127: if ( *pk >= (unsigned int)md )
9128: *pk %= md;
9129: DMAR(*pk,inv,0,md,*pk)
9130: }
9131: for ( i = rank+1; i < row; i++ ) {
9132: t = mat[i];
9133: if ( (a = t[j]) != 0 ) {
9134: sugar[i] = MAX(sugar[i],s);
9135: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9136: }
9137: }
9138: rank++;
9139: }
9140: for ( j = col-1, l = rank-1; j >= 0; j-- )
9141: if ( colstat[j] ) {
9142: pivot = mat[l];
9143: s = sugar[l];
9144: for ( i = 0; i < l; i++ ) {
9145: t = mat[i];
9146: t[j] %= md;
9147: if ( (a = t[j]) != 0 ) {
9148: sugar[i] = MAX(sugar[i],s);
9149: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9150: }
9151: }
9152: l--;
9153: }
9154: for ( j = 0, l = 0; l < rank; j++ )
9155: if ( colstat[j] ) {
9156: t = mat[l];
9157: for ( k = j; k < col; k++ )
9158: if ( t[k] >= (unsigned int)md )
9159: t[k] %= md;
9160: l++;
9161: }
9162: return rank;
9163: }
9164:
1.40 noro 9165: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9166: {
9167: int i,j,k,l,rank,s,imin;
9168: UINT inv;
9169: UINT a;
9170: UINT *t,*pivot,*pk;
9171: UINT *ck;
9172: UINT *ct;
9173: ND_pairs pair;
9174: SIG sg;
9175: int *used;
9176:
9177: used = (int *)MALLOC(row*sizeof(int));
9178: for ( j = 0; j < col; j++ ) {
9179: for ( i = 0; i < row; i++ )
9180: a = mat[i][j] %= md;
9181: for ( i = 0; i < row; i++ )
9182: if ( !used[i] && mat[i][j] ) break;
9183: if ( i == row ) {
9184: colstat[j] = 0;
9185: continue;
9186: } else {
9187: colstat[j] = 1;
9188: used[i] = 1;
9189: }
9190: /* column j is normalized */
9191: s = sugar[i];
9192: inv = invm(mat[i][j],md);
9193: /* normalize pivot row */
9194: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9195: DMAR(*pk,inv,0,md,*pk);
9196: }
9197: for ( k = i+1; k < row; k++ ) {
9198: if ( (a = mat[k][j]) != 0 ) {
9199: sugar[k] = MAX(sugar[k],s);
9200: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9201: Nf4_red++;
9202: }
9203: }
9204: }
9205: rank = 0;
9206: for ( i = 0; i < row; i++ ) {
9207: for ( j = 0; j < col; j++ )
9208: if ( mat[i][j] ) break;
9209: if ( j == col ) sugar[i] = -1;
9210: else rank++;
9211: }
9212: return rank;
9213: }
9214:
1.1 noro 9215:
1.7 noro 9216: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9217: {
1.7 noro 9218: int i,j,k,l,inv,a,rank,s;
9219: unsigned int *t,*pivot,*pk;
9220: unsigned int **mat;
9221:
9222: mat = (unsigned int **)mat0;
9223: for ( rank = 0, j = 0; j < col; j++ ) {
9224: for ( i = rank; i < row; i++ )
9225: if ( mat[i][j] )
9226: break;
9227: if ( i == row ) {
9228: colstat[j] = 0;
9229: continue;
9230: } else
9231: colstat[j] = 1;
9232: if ( i != rank ) {
9233: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9234: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9235: }
9236: pivot = mat[rank];
9237: s = sugar[rank];
9238: inv = _invsf(pivot[j]);
9239: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9240: if ( *pk )
9241: *pk = _mulsf(*pk,inv);
9242: for ( i = rank+1; i < row; i++ ) {
9243: t = mat[i];
9244: if ( (a = t[j]) != 0 ) {
9245: sugar[i] = MAX(sugar[i],s);
9246: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9247: }
9248: }
9249: rank++;
9250: }
9251: for ( j = col-1, l = rank-1; j >= 0; j-- )
9252: if ( colstat[j] ) {
9253: pivot = mat[l];
9254: s = sugar[l];
9255: for ( i = 0; i < l; i++ ) {
9256: t = mat[i];
9257: if ( (a = t[j]) != 0 ) {
9258: sugar[i] = MAX(sugar[i],s);
9259: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9260: }
9261: }
9262: l--;
9263: }
9264: return rank;
9265: }
1.1 noro 9266:
1.7 noro 9267: int ndv_ishomo(NDV p)
9268: {
9269: NMV m;
9270: int len,h;
1.1 noro 9271:
9272: if ( !p ) return 1;
9273: len = LEN(p);
9274: m = BDY(p);
9275: h = TD(DL(m));
9276: NMV_ADV(m);
9277: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9278: if ( TD(DL(m)) != h ) {
9279: return 0;
9280: }
1.1 noro 9281: return 1;
9282: }
9283:
9284: void ndv_save(NDV p,int index)
9285: {
9286: FILE *s;
9287: char name[BUFSIZ];
9288: short id;
9289: int nv,sugar,len,n,i,td,e,j;
9290: NMV m;
9291: unsigned int *dl;
9292: int mpos;
9293:
9294: sprintf(name,"%s/%d",Demand,index);
9295: s = fopen(name,"w");
9296: savevl(s,0);
9297: if ( !p ) {
9298: saveobj(s,0);
9299: return;
9300: }
9301: id = O_DP;
9302: nv = NV(p);
9303: sugar = SG(p);
9304: len = LEN(p);
9305: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9306: write_int(s,(unsigned int *)&len);
9307:
9308: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9309: saveobj(s,(Obj)CZ(m));
1.1 noro 9310: dl = DL(m);
9311: td = TD(dl);
9312: write_int(s,(unsigned int *)&td);
9313: for ( j = 0; j < nv; j++ ) {
9314: e = GET_EXP(dl,j);
9315: write_int(s,(unsigned int *)&e);
9316: }
9317: if ( nd_module ) {
9318: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
9319: }
9320: }
9321: fclose(s);
9322: }
9323:
9324: void nd_save_mod(ND p,int index)
9325: {
9326: FILE *s;
9327: char name[BUFSIZ];
9328: int nv,sugar,len,c;
9329: NM m;
9330:
9331: sprintf(name,"%s/%d",Demand,index);
9332: s = fopen(name,"w");
9333: if ( !p ) {
9334: len = 0;
9335: write_int(s,(unsigned int *)&len);
9336: fclose(s);
9337: return;
9338: }
9339: nv = NV(p);
9340: sugar = SG(p);
9341: len = LEN(p);
9342: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9343: for ( m = BDY(p); m; m = NEXT(m) ) {
9344: c = CM(m); write_int(s,(unsigned int *)&c);
9345: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9346: }
9347: fclose(s);
9348: }
9349:
9350: NDV ndv_load(int index)
9351: {
9352: FILE *s;
9353: char name[BUFSIZ];
9354: short id;
9355: int nv,sugar,len,n,i,td,e,j;
9356: NDV d;
9357: NMV m0,m;
9358: unsigned int *dl;
9359: Obj obj;
9360: int mpos;
9361:
9362: sprintf(name,"%s/%d",Demand,index);
9363: s = fopen(name,"r");
9364: if ( !s ) return 0;
9365:
9366: skipvl(s);
9367: read_short(s,(unsigned short *)&id);
9368: if ( !id ) return 0;
9369: read_int(s,(unsigned int *)&nv);
9370: read_int(s,(unsigned int *)&sugar);
9371: read_int(s,(unsigned int *)&len);
9372:
9373: m0 = m = MALLOC(len*nmv_adv);
9374: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9375: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9376: dl = DL(m);
9377: ndl_zero(dl);
9378: read_int(s,(unsigned int *)&td); TD(dl) = td;
9379: for ( j = 0; j < nv; j++ ) {
9380: read_int(s,(unsigned int *)&e);
9381: PUT_EXP(dl,j,e);
9382: }
9383: if ( nd_module ) {
9384: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9385: }
9386: if ( nd_blockmask ) ndl_weight_mask(dl);
9387: }
9388: fclose(s);
9389: MKNDV(nv,m0,len,d);
9390: SG(d) = sugar;
9391: return d;
9392: }
9393:
9394: ND nd_load_mod(int index)
9395: {
9396: FILE *s;
9397: char name[BUFSIZ];
9398: int nv,sugar,len,i,c;
9399: ND d;
9400: NM m0,m;
9401:
9402: sprintf(name,"%s/%d",Demand,index);
9403: s = fopen(name,"r");
9404: /* if the file does not exist, it means p[index]=0 */
9405: if ( !s ) return 0;
9406:
9407: read_int(s,(unsigned int *)&nv);
9408: if ( !nv ) { fclose(s); return 0; }
9409:
9410: read_int(s,(unsigned int *)&sugar);
9411: read_int(s,(unsigned int *)&len);
9412: for ( m0 = 0, i = 0; i < len; i++ ) {
9413: NEXTNM(m0,m);
9414: read_int(s,(unsigned int *)&c); CM(m) = c;
9415: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9416: }
9417: NEXT(m) = 0;
9418: MKND(nv,m0,len,d);
9419: SG(d) = sugar;
9420: fclose(s);
9421: return d;
9422: }
9423:
9424: void nd_det(int mod,MAT f,P *rp)
9425: {
9426: VL fv,tv;
9427: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9428: pointer **m;
9429: P **w;
9430: P mp,r;
9431: NDV **dm;
9432: NDV *t,*mi,*mj;
9433: NDV d,s,mij,mjj;
9434: ND u;
9435: NMV nmv;
9436: UINT *bound;
9437: PGeoBucket bucket;
9438: struct order_spec *ord;
9439: Z dq,dt,ds;
9440: Z mone;
9441: Z gn,qn,dn0,nm,dn;
9442:
9443: create_order_spec(0,0,&ord);
9444: nd_init_ord(ord);
9445: get_vars((Obj)f,&fv);
9446: if ( f->row != f->col )
9447: error("nd_det : non-square matrix");
9448: n = f->row;
9449: m = f->body;
9450: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9451:
9452: if ( !nvar ) {
9453: if ( !mod )
9454: detp(CO,(P **)m,n,rp);
9455: else {
9456: w = (P **)almat_pointer(n,n);
9457: for ( i = 0; i < n; i++ )
9458: for ( j = 0; j < n; j++ )
9459: ptomp(mod,(P)m[i][j],&w[i][j]);
9460: detmp(CO,mod,w,n,&mp);
9461: mptop(mp,rp);
9462: }
9463: return;
9464: }
9465:
9466: if ( !mod ) {
9467: w = (P **)almat_pointer(n,n);
9468: dq = ONE;
9469: for ( i = 0; i < n; i++ ) {
9470: dn0 = ONE;
9471: for ( j = 0; j < n; j++ ) {
9472: if ( !m[i][j] ) continue;
9473: lgp(m[i][j],&nm,&dn);
1.6 noro 9474: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9475: }
9476: if ( !UNIZ(dn0) ) {
9477: ds = dn0;
9478: for ( j = 0; j < n; j++ )
9479: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9480: mulz(dq,ds,&dt); dq = dt;
9481: } else
9482: for ( j = 0; j < n; j++ )
9483: w[i][j] = (P)m[i][j];
9484: }
9485: m = (pointer **)w;
9486: }
9487:
9488: for ( i = 0, max = 1; i < n; i++ )
9489: for ( j = 0; j < n; j++ )
9490: for ( tv = fv; tv; tv = NEXT(tv) ) {
9491: e = getdeg(tv->v,(P)m[i][j]);
9492: max = MAX(e,max);
9493: }
9494: nd_setup_parameters(nvar,max);
9495: dm = (NDV **)almat_pointer(n,n);
9496: for ( i = 0, max = 1; i < n; i++ )
9497: for ( j = 0; j < n; j++ ) {
9498: dm[i][j] = ptondv(CO,fv,m[i][j]);
9499: if ( mod ) ndv_mod(mod,dm[i][j]);
9500: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9501: }
9502: d = ptondv(CO,fv,(P)ONE);
9503: if ( mod ) ndv_mod(mod,d);
9504: chsgnz(ONE,&mone);
9505: for ( j = 0, sgn = 1; j < n; j++ ) {
9506: if ( DP_Print ) {
9507: fprintf(asir_out,".");
9508: }
9509: for ( i = j; i < n && !dm[i][j]; i++ );
9510: if ( i == n ) {
9511: *rp = 0;
9512: return;
9513: }
9514: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9515: for ( k = j; k < n; k++ )
9516: for ( l = j; l < n; l++ )
9517: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9518: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9519: }
9520: if ( k0 != j ) {
9521: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9522: sgn = -sgn;
9523: }
9524: if ( l0 != j ) {
9525: for ( k = j; k < n; k++ ) {
9526: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9527: }
9528: sgn = -sgn;
9529: }
9530: bound = nd_det_compute_bound(dm,n,j);
9531: for ( k = 0; k < nd_nvar; k++ )
9532: if ( bound[k]*2 > nd_mask0 ) break;
9533: if ( k < nd_nvar )
9534: nd_det_reconstruct(dm,n,j,d);
9535:
9536: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9537: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9538: mi = dm[i]; mij = mi[j];
9539: if ( mod )
9540: ndv_mul_c(mod,mij,mod-1);
9541: else
9542: ndv_mul_c_q(mij,mone);
9543: for ( k = j+1; k < n; k++ ) {
9544: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9545: bucket = create_pbucket();
9546: if ( mi[k] ) {
9547: nmv = BDY(mjj); len = LEN(mjj);
9548: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9549: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9550: add_pbucket(mod,bucket,u);
9551: }
9552: }
9553: if ( mj[k] && mij ) {
9554: nmv = BDY(mij); len = LEN(mij);
9555: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9556: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9557: add_pbucket(mod,bucket,u);
9558: }
9559: }
9560: u = nd_quo(mod,bucket,d);
9561: mi[k] = ndtondv(mod,u);
9562: }
9563: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9564: }
9565: d = mjj;
9566: }
9567: if ( DP_Print ) {
9568: fprintf(asir_out,"\n");
9569: }
9570: if ( sgn < 0 ) {
9571: if ( mod )
9572: ndv_mul_c(mod,d,mod-1);
9573: else
9574: ndv_mul_c_q(d,mone);
9575: }
9576: r = ndvtop(mod,CO,fv,d);
9577: if ( !mod && !UNIQ(dq) )
9578: divsp(CO,r,(P)dq,rp);
9579: else
9580: *rp = r;
9581: }
9582:
9583: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9584: {
9585: NM mr,mr0;
9586: NM tnm;
9587: NMV m;
9588: UINT *d0,*dt,*dm;
9589: int c,n,td,i,c1,c2,len;
9590: Z q;
9591: ND r;
9592:
9593: if ( !p ) return 0;
9594: else {
9595: n = NV(p); m = BDY(p); len = LEN(p);
9596: d0 = DL(m0);
9597: td = TD(d);
9598: mr0 = 0;
9599: NEWNM(tnm);
9600: if ( mod ) {
9601: c = CM(m0);
9602: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9603: ndl_add(DL(m),d0,DL(tnm));
9604: if ( ndl_reducible(DL(tnm),d) ) {
9605: NEXTNM(mr0,mr);
9606: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9607: ndl_copy(DL(tnm),DL(mr));
9608: }
9609: }
9610: } else {
1.6 noro 9611: q = CZ(m0);
1.1 noro 9612: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9613: ndl_add(DL(m),d0,DL(tnm));
9614: if ( ndl_reducible(DL(tnm),d) ) {
9615: NEXTNM(mr0,mr);
1.6 noro 9616: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9617: ndl_copy(DL(tnm),DL(mr));
9618: }
9619: }
9620: }
9621: if ( !mr0 )
9622: return 0;
9623: else {
9624: NEXT(mr) = 0;
9625: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9626: MKND(NV(p),mr0,len,r);
9627: SG(r) = SG(p) + TD(d0);
9628: return r;
9629: }
9630: }
9631: }
9632:
9633: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9634: {
9635: int i,obpe,oadv,h,k,l;
9636: static NM prev_nm_free_list;
9637: EPOS oepos;
9638:
9639: obpe = nd_bpe;
9640: oadv = nmv_adv;
9641: oepos = nd_epos;
9642: if ( obpe < 2 ) nd_bpe = 2;
9643: else if ( obpe < 3 ) nd_bpe = 3;
9644: else if ( obpe < 4 ) nd_bpe = 4;
9645: else if ( obpe < 5 ) nd_bpe = 5;
9646: else if ( obpe < 6 ) nd_bpe = 6;
9647: else if ( obpe < 8 ) nd_bpe = 8;
9648: else if ( obpe < 10 ) nd_bpe = 10;
9649: else if ( obpe < 16 ) nd_bpe = 16;
9650: else if ( obpe < 32 ) nd_bpe = 32;
9651: else error("nd_det_reconstruct : exponent too large");
9652:
9653: nd_setup_parameters(nd_nvar,0);
9654: prev_nm_free_list = _nm_free_list;
9655: _nm_free_list = 0;
9656: for ( k = j; k < n; k++ )
9657: for (l = j; l < n; l++ )
9658: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9659: ndv_realloc(d,obpe,oadv,oepos);
9660: prev_nm_free_list = 0;
9661: #if 0
9662: GC_gcollect();
9663: #endif
9664: }
9665:
9666: /* returns a UINT array containing degree bounds */
9667:
9668: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9669: {
9670: UINT *d0,*d1,*d,*t,*r;
9671: int k,l,i;
9672:
9673: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9674: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9675: for ( k = j; k < n; k++ )
9676: for ( l = j; l < n; l++ )
9677: if ( dm[k][l] ) {
9678: d = ndv_compute_bound(dm[k][l]);
9679: for ( i = 0; i < nd_nvar; i++ )
9680: d0[i] = MAX(d0[i],d[i]);
9681: }
9682: return d0;
9683: }
9684:
9685: DL nd_separate_d(UINT *d,UINT *trans)
9686: {
9687: int n,td,i,e,j;
9688: DL a;
9689:
9690: ndl_zero(trans);
9691: td = 0;
9692: for ( i = 0; i < nd_ntrans; i++ ) {
9693: e = GET_EXP(d,i);
9694: PUT_EXP(trans,i,e);
9695: td += MUL_WEIGHT(e,i);
9696: }
9697: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9698: /* homogenized */
9699: i = nd_nvar-1;
9700: e = GET_EXP(d,i);
9701: PUT_EXP(trans,i,e);
9702: td += MUL_WEIGHT(e,i);
9703: }
9704: TD(trans) = td;
9705: if ( nd_blockmask) ndl_weight_mask(trans);
9706: NEWDL(a,nd_nalg);
9707: td = 0;
9708: for ( i = 0; i < nd_nalg; i++ ) {
9709: j = nd_ntrans+i;
9710: e = GET_EXP(d,j);
9711: a->d[i] = e;
9712: td += e;
9713: }
9714: a->td = td;
9715: return a;
9716: }
9717:
9718: int nd_monic(int mod,ND *p)
9719: {
9720: UINT *trans,*t;
9721: DL alg;
9722: MP mp0,mp;
9723: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9724: ND r;
9725: DL dl;
9726: DP nm;
9727: NDV ndv;
9728: DAlg inv,cd;
9729: ND s,c;
9730: Z l,mul;
9731: Z ln;
9732: int n,ntrans,i,e,td,is_lc,len;
9733: NumberField nf;
9734: struct oEGT eg0,eg1;
9735:
9736: if ( !(nf = get_numberfield()) )
9737: error("nd_monic : current_numberfield is not set");
9738:
9739: /* Q coef -> DAlg coef */
9740: NEWNM(ma0); ma = ma0;
9741: m = BDY(*p);
9742: is_lc = 1;
9743: while ( 1 ) {
9744: NEWMP(mp0); mp = mp0;
1.6 noro 9745: mp->c = (Obj)CZ(m);
1.1 noro 9746: mp->dl = nd_separate_d(DL(m),DL(ma));
9747: NEWNM(mb);
9748: for ( m = NEXT(m); m; m = NEXT(m) ) {
9749: alg = nd_separate_d(DL(m),DL(mb));
9750: if ( !ndl_equal(DL(ma),DL(mb)) )
9751: break;
1.6 noro 9752: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9753: }
9754: NEXT(mp) = 0;
9755: MKDP(nd_nalg,mp0,nm);
9756: MKDAlg(nm,ONE,cd);
9757: if ( is_lc == 1 ) {
9758: /* if the lc is a rational number, we have nothing to do */
9759: if ( !mp0->dl->td )
9760: return 1;
9761:
9762: get_eg(&eg0);
9763: invdalg(cd,&inv);
9764: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9765: /* check the validity of inv */
9766: if ( mod && !remqi((Q)inv->dn,mod) )
9767: return 0;
9768: CA(ma) = nf->one;
9769: is_lc = 0;
9770: ln = ONE;
9771: } else {
9772: muldalg(cd,inv,&CA(ma));
9773: lcmz(ln,CA(ma)->dn,&ln);
9774: }
9775: if ( m ) {
9776: NEXT(ma) = mb; ma = mb;
9777: } else {
9778: NEXT(ma) = 0;
9779: break;
9780: }
9781: }
9782: /* l = lcm(denoms) */
9783: l = ln;
9784: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9785: divsz(l,CA(m)->dn,&mul);
1.1 noro 9786: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9787: NEXTNM(mr0,mr);
1.6 noro 9788: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9789: dl = mp->dl;
9790: td = TD(DL(m));
9791: ndl_copy(DL(m),DL(mr));
9792: for ( i = 0; i < nd_nalg; i++ ) {
9793: e = dl->d[i];
9794: PUT_EXP(DL(mr),i+nd_ntrans,e);
9795: td += MUL_WEIGHT(e,i+nd_ntrans);
9796: }
9797: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9798: TD(DL(mr)) = td;
9799: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9800: }
9801: }
9802: NEXT(mr) = 0;
9803: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9804: MKND(NV(*p),mr0,len,r);
9805: /* XXX */
9806: SG(r) = SG(*p);
9807: nd_free(*p);
9808: *p = r;
9809: return 1;
9810: }
9811:
9812: NODE reverse_node(NODE n)
9813: {
9814: NODE t,t1;
9815:
9816: for ( t = 0; n; n = NEXT(n) ) {
9817: MKNODE(t1,BDY(n),t); t = t1;
9818: }
9819: return t;
9820: }
9821:
9822: P ndc_div(int mod,union oNDC a,union oNDC b)
9823: {
9824: union oNDC c;
9825: int inv,t;
9826:
9827: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9828: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9829: else if ( mod ) {
9830: inv = invm(b.m,mod);
9831: DMAR(a.m,inv,0,mod,t); c.m = t;
9832: } else if ( nd_vc )
9833: divsp(nd_vc,a.p,b.p,&c.p);
9834: else
9835: divsz(a.z,b.z,&c.z);
9836: return ndctop(mod,c);
9837: }
9838:
9839: P ndctop(int mod,union oNDC c)
9840: {
9841: Z q;
9842: int e;
9843: GFS gfs;
9844:
9845: if ( mod == -1 ) {
9846: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9847: } else if ( mod == -2 ) {
1.10 noro 9848: q = c.z; return (P)q;
1.1 noro 9849: } else if ( mod > 0 ) {
1.6 noro 9850: STOZ(c.m,q); return (P)q;
1.1 noro 9851: } else
9852: return (P)c.p;
9853: }
9854:
9855: /* [0,0,0,cont] = p -> p/cont */
9856:
9857: void finalize_tracelist(int i,P cont)
9858: {
9859: LIST l;
9860: NODE node;
9861: Z iq;
9862:
9863: if ( !UNIQ(cont) ) {
9864: node = mknode(4,NULLP,NULLP,NULLP,cont);
9865: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9866: nd_tracelist = node;
9867: }
1.6 noro 9868: STOZ(i,iq);
1.1 noro 9869: nd_tracelist = reverse_node(nd_tracelist);
9870: MKLIST(l,nd_tracelist);
9871: node = mknode(2,iq,l); MKLIST(l,node);
9872: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9873: nd_alltracelist = node; nd_tracelist = 0;
9874: }
9875:
9876: void conv_ilist(int demand,int trace,NODE g,int **indp)
9877: {
9878: int n,i,j;
9879: int *ind;
9880: NODE t;
9881:
9882: n = length(g);
9883: ind = (int *)MALLOC(n*sizeof(int));
9884: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9885: j = (long)BDY(t); ind[i] = j;
9886: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9887: }
9888: if ( indp ) *indp = ind;
9889: }
9890:
1.30 noro 9891: NODE conv_ilist_s(int demand,int trace,int **indp)
9892: {
9893: int n,i,j;
9894: int *ind;
9895: NODE g0,g;
9896:
9897: n = nd_psn;
9898: ind = (int *)MALLOC(n*sizeof(int));
9899: g0 = 0;
9900: for ( i = 0; i < n; i++ ) {
9901: ind[i] = i;
9902: NEXTNODE(g0,g);
9903: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
9904: }
9905: if ( g0 ) NEXT(g) = 0;
9906: if ( indp ) *indp = ind;
9907: return g0;
9908: }
9909:
1.1 noro 9910: void parse_nd_option(NODE opt)
9911: {
1.38 noro 9912: NODE t,p,u;
1.1 noro 9913: int i,s,n;
1.38 noro 9914: char *key;
9915: Obj value;
1.1 noro 9916:
1.41 noro 9917: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 9918: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9919: nd_splist = 0; nd_check_splist = 0;
1.38 noro 9920: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
9921: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 9922: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.39 noro 9923:
1.38 noro 9924: for ( t = opt; t; t = NEXT(t) ) {
9925: p = BDY((LIST)BDY(t));
9926: key = BDY((STRING)BDY(p));
9927: value = (Obj)BDY(NEXT(p));
9928: if ( !strcmp(key,"gentrace") )
9929: nd_gentrace = value?1:0;
9930: else if ( !strcmp(key,"gensyz") )
9931: nd_gensyz = value?1:0;
9932: else if ( !strcmp(key,"nora") )
9933: nd_nora = value?1:0;
1.41 noro 9934: else if ( !strcmp(key,"norb") )
9935: nd_norb = value?1:0;
1.38 noro 9936: else if ( !strcmp(key,"gbblock") ) {
9937: if ( value && OID(value) == O_LIST ) {
1.1 noro 9938: u = BDY((LIST)value);
1.38 noro 9939: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 9940: for ( i = 0; u; u = NEXT(u) ) {
9941: p = BDY((LIST)BDY(u));
1.6 noro 9942: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9943: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9944: }
9945: nd_gbblock[i] = -1;
1.38 noro 9946: } else
9947: nd_gbblock = 0;
1.1 noro 9948: } else if ( !strcmp(key,"newelim") )
9949: nd_newelim = value?1:0;
9950: else if ( !strcmp(key,"intersect") )
9951: nd_intersect = value?1:0;
1.17 noro 9952: else if ( !strcmp(key,"syzgen") )
9953: nd_intersect = ZTOS((Q)value);
1.1 noro 9954: else if ( !strcmp(key,"lf") )
9955: nd_lf = value?1:0;
9956: else if ( !strcmp(key,"trace") ) {
1.38 noro 9957: if ( value ) {
9958: u = BDY((LIST)value);
9959: nd_nzlist = BDY((LIST)ARG2(u));
9960: nd_bpe = ZTOS((Q)ARG3(u));
9961: }
1.1 noro 9962: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 9963: nd_f4red = ZTOS((Q)value);
1.1 noro 9964: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 9965: nd_rank0 = value?1:0;
1.1 noro 9966: } else if ( !strcmp(key,"splist") ) {
1.38 noro 9967: nd_splist = value?1:0;
1.1 noro 9968: } else if ( !strcmp(key,"check_splist") ) {
9969: nd_check_splist = BDY((LIST)value);
9970: } else if ( !strcmp(key,"sugarweight") ) {
9971: u = BDY((LIST)value);
1.38 noro 9972: n = length(u);
9973: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 9974: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 9975: nd_sugarweight[i] = ZTOS((Q)BDY(u));
9976: } else if ( !strcmp(key,"f4_td") ) {
9977: nd_f4_td = value?1:0;
9978: } else if ( !strcmp(key,"sba_f4step") ) {
9979: nd_sba_f4step = value?ZTOS((Q)value):0;
9980: } else if ( !strcmp(key,"sba_pot") ) {
9981: nd_sba_pot = value?1:0;
9982: } else if ( !strcmp(key,"sba_largelcm") ) {
9983: nd_sba_largelcm = value?1:0;
1.39 noro 9984: } else if ( !strcmp(key,"sba_dontsort") ) {
9985: nd_sba_dontsort = value?1:0;
1.41 noro 9986: } else if ( !strcmp(key,"sba_redundant_check") ) {
9987: nd_sba_redundant_check = value?1:0;
1.39 noro 9988: } else if ( !strcmp(key,"top") ) {
9989: nd_top = value?1:0;
1.1 noro 9990: }
1.38 noro 9991: }
1.1 noro 9992: }
9993:
9994: ND mdptond(DP d);
9995: ND nd_mul_nm(int mod,NM m0,ND p);
9996: ND nd_mul_nm_lf(NM m0,ND p);
9997: ND *btog(NODE ti,ND **p,int nb,int mod);
9998: ND btog_one(NODE ti,ND *p,int nb,int mod);
9999: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10000: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10001:
10002: /* d:monomial */
10003: ND mdptond(DP d)
10004: {
10005: NM m;
10006: ND r;
10007:
10008: if ( OID(d) == 1 )
10009: r = ptond(CO,CO,(P)d);
10010: else {
10011: NEWNM(m);
10012: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10013: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10014: NEXT(m) = 0;
10015: MKND(NV(d),m,1,r);
10016: }
10017: return r;
10018: }
10019:
10020: ND nd_mul_nm(int mod,NM m0,ND p)
10021: {
10022: UINT *d0;
10023: int c0,c1,c;
10024: NM tm,mr,mr0;
10025: ND r;
10026:
10027: if ( !p ) return 0;
10028: d0 = DL(m0);
10029: c0 = CM(m0);
10030: mr0 = 0;
10031: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10032: NEXTNM(mr0,mr);
10033: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10034: ndl_add(d0,DL(tm),DL(mr));
10035: }
10036: NEXT(mr) = 0;
10037: MKND(NV(p),mr0,LEN(p),r);
10038: return r;
10039: }
10040:
10041: ND nd_mul_nm_lf(NM m0,ND p)
10042: {
10043: UINT *d0;
10044: Z c0,c1,c;
10045: NM tm,mr,mr0;
10046: ND r;
10047:
10048: if ( !p ) return 0;
10049: d0 = DL(m0);
10050: c0 = CZ(m0);
10051: mr0 = 0;
10052: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10053: NEXTNM(mr0,mr);
10054: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10055: ndl_add(d0,DL(tm),DL(mr));
10056: }
10057: NEXT(mr) = 0;
10058: MKND(NV(p),mr0,LEN(p),r);
10059: return r;
10060: }
10061:
10062: ND *btog(NODE ti,ND **p,int nb,int mod)
10063: {
10064: PGeoBucket *r;
10065: int i,ci;
10066: NODE t,s;
10067: ND m,tp;
10068: ND *pi,*rd;
10069: P c;
10070:
10071: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10072: for ( i = 0; i < nb; i++ )
10073: r[i] = create_pbucket();
10074: for ( t = ti; t; t = NEXT(t) ) {
10075: s = BDY((LIST)BDY(t));
10076: if ( ARG0(s) ) {
10077: m = mdptond((DP)ARG2(s));
1.6 noro 10078: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10079: if ( (ci = ((MQ)c)->cont) != 0 ) {
10080: HCM(m) = ci;
1.6 noro 10081: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10082: for ( i = 0; i < nb; i++ ) {
10083: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10084: add_pbucket(mod,r[i],tp);
10085: }
10086: }
10087: ci = 1;
10088: } else {
10089: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10090: ci = invm(ci,mod);
10091: }
10092: }
10093: rd = (ND *)MALLOC(nb*sizeof(ND));
10094: for ( i = 0; i < nb; i++ )
10095: rd[i] = normalize_pbucket(mod,r[i]);
10096: if ( ci != 1 )
10097: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10098: return rd;
10099: }
10100:
10101: /* YYY */
10102: ND *btog_lf(NODE ti,ND **p,int nb)
10103: {
10104: PGeoBucket *r;
10105: int i;
10106: NODE t,s;
10107: ND m,tp;
10108: ND *pi,*rd;
10109: LM lm;
10110: Z lf,c;
10111:
10112: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10113: for ( i = 0; i < nb; i++ )
10114: r[i] = create_pbucket();
10115: for ( t = ti; t; t = NEXT(t) ) {
10116: s = BDY((LIST)BDY(t));
10117: if ( ARG0(s) ) {
10118: m = mdptond((DP)ARG2(s));
1.6 noro 10119: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10120: if ( lm ) {
10121: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10122: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10123: for ( i = 0; i < nb; i++ ) {
10124: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10125: add_pbucket(-2,r[i],tp);
10126: }
10127: }
10128: c = ONE;
10129: } else {
10130: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10131: }
10132: }
10133: rd = (ND *)MALLOC(nb*sizeof(ND));
10134: for ( i = 0; i < nb; i++ )
10135: rd[i] = normalize_pbucket(-2,r[i]);
10136: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10137: return rd;
10138: }
10139:
10140: ND btog_one(NODE ti,ND *p,int nb,int mod)
10141: {
10142: PGeoBucket r;
10143: int i,ci,j;
10144: NODE t,s;
10145: ND m,tp;
10146: ND pi,rd;
10147: P c;
10148:
10149: r = create_pbucket();
10150: for ( t = ti; t; t = NEXT(t) ) {
10151: s = BDY((LIST)BDY(t));
10152: if ( ARG0(s) ) {
10153: m = mdptond((DP)ARG2(s));
1.6 noro 10154: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10155: if ( (ci = ((MQ)c)->cont) != 0 ) {
10156: HCM(m) = ci;
1.6 noro 10157: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10158: if ( !pi ) {
10159: pi = nd_load_mod(j);
10160: tp = nd_mul_nm(mod,BDY(m),pi);
10161: nd_free(pi);
10162: add_pbucket(mod,r,tp);
10163: } else {
10164: tp = nd_mul_nm(mod,BDY(m),pi);
10165: add_pbucket(mod,r,tp);
10166: }
10167: }
10168: ci = 1;
10169: } else {
10170: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10171: ci = invm(ci,mod);
10172: }
10173: }
10174: rd = normalize_pbucket(mod,r);
10175: free_pbucket(r);
10176: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10177: return rd;
10178: }
10179:
10180: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10181:
10182: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10183: {
10184: int i,j,n,m,nb,pi0,pi1,nvar;
10185: VL fv,tv,vv;
10186: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10187: ND **p;
10188: ND *c;
10189: ND u;
10190: P inv;
10191: MAT mat;
10192:
10193: if ( mod == -2 )
10194: return nd_btog_lf(f,v,ord,tlist,rp);
10195:
10196: parse_nd_option(current_option);
10197: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10198: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10199: switch ( ord->id ) {
10200: case 1:
10201: if ( ord->nv != nvar )
10202: error("nd_check : invalid order specification");
10203: break;
10204: default:
10205: break;
10206: }
10207: nd_init_ord(ord);
10208: #if 0
1.6 noro 10209: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10210: #else
10211: nd_bpe = 32;
10212: #endif
10213: nd_setup_parameters(nvar,0);
10214: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10215: intred = BDY((LIST)ARG3(BDY(tlist)));
10216: ind = BDY((LIST)ARG4(BDY(tlist)));
10217: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10218: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10219: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10220: if ( j > i ) i = j;
10221: }
10222: n = i+1;
10223: nb = length(BDY(f));
10224: p = (ND **)MALLOC(n*sizeof(ND *));
10225: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10226: pi = BDY((LIST)BDY(t));
1.6 noro 10227: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10228: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10229: ptomp(mod,(P)ARG2(pi),&inv);
10230: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10231: u = ptond(CO,vv,(P)ONE);
10232: HCM(u) = ((MQ)inv)->cont;
10233: c[pi1] = u;
10234: }
10235: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10236: printf("%d ",i); fflush(stdout);
10237: ti = BDY((LIST)BDY(t));
1.6 noro 10238: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10239: }
10240: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10241: printf("%d ",i); fflush(stdout);
10242: ti = BDY((LIST)BDY(t));
1.6 noro 10243: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10244: }
10245: m = length(ind);
10246: MKMAT(mat,nb,m);
10247: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10248: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10249: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10250: return mat;
10251: }
10252:
10253: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10254: {
10255: int i,j,n,m,nb,pi0,pi1,nvar;
10256: VL fv,tv,vv;
10257: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10258: ND **p;
10259: ND *c;
10260: ND u;
10261: MAT mat;
10262: LM lm;
10263: Z lf,inv;
10264:
10265: parse_nd_option(current_option);
10266: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10267: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10268: switch ( ord->id ) {
10269: case 1:
10270: if ( ord->nv != nvar )
10271: error("nd_check : invalid order specification");
10272: break;
10273: default:
10274: break;
10275: }
10276: nd_init_ord(ord);
10277: #if 0
1.6 noro 10278: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10279: #else
10280: nd_bpe = 32;
10281: #endif
10282: nd_setup_parameters(nvar,0);
10283: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10284: intred = BDY((LIST)ARG3(BDY(tlist)));
10285: ind = BDY((LIST)ARG4(BDY(tlist)));
10286: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10287: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10288: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10289: if ( j > i ) i = j;
10290: }
10291: n = i+1;
10292: nb = length(BDY(f));
10293: p = (ND **)MALLOC(n*sizeof(ND *));
10294: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10295: pi = BDY((LIST)BDY(t));
1.6 noro 10296: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10297: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10298: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
10299: u = ptond(CO,vv,(P)ONE);
10300: HCZ(u) = inv;
10301: c[pi1] = u;
10302: }
10303: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10304: printf("%d ",i); fflush(stdout);
10305: ti = BDY((LIST)BDY(t));
1.6 noro 10306: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10307: }
10308: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10309: printf("%d ",i); fflush(stdout);
10310: ti = BDY((LIST)BDY(t));
1.6 noro 10311: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10312: }
10313: m = length(ind);
10314: MKMAT(mat,nb,m);
10315: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10316: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10317: BDY(mat)[i][j] = ndtodp(-2,c[i]);
10318: return mat;
10319: }
10320:
10321: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
10322: LIST tlist,int pos,MAT *rp)
10323: {
10324: int i,j,n,m,nb,pi0,pi1,nvar;
10325: VL fv,tv,vv;
10326: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10327: ND *p;
10328: ND *c;
10329: ND u;
10330: P inv;
10331: VECT vect;
10332:
10333: if ( mod == -2 )
10334: error("nd_btog_one : not implemented yet for a large finite field");
10335:
10336: parse_nd_option(current_option);
10337: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10338: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10339: switch ( ord->id ) {
10340: case 1:
10341: if ( ord->nv != nvar )
10342: error("nd_check : invalid order specification");
10343: break;
10344: default:
10345: break;
10346: }
10347: nd_init_ord(ord);
10348: #if 0
1.6 noro 10349: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10350: #else
10351: nd_bpe = 32;
10352: #endif
10353: nd_setup_parameters(nvar,0);
10354: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10355: intred = BDY((LIST)ARG3(BDY(tlist)));
10356: ind = BDY((LIST)ARG4(BDY(tlist)));
10357: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10358: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10359: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10360: if ( j > i ) i = j;
10361: }
10362: n = i+1;
10363: nb = length(BDY(f));
10364: p = (ND *)MALLOC(n*sizeof(ND *));
10365: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10366: pi = BDY((LIST)BDY(t));
1.6 noro 10367: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10368: if ( pi1 == pos ) {
10369: ptomp(mod,(P)ARG2(pi),&inv);
10370: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10371: u = ptond(CO,vv,(P)ONE);
10372: HCM(u) = ((MQ)inv)->cont;
10373: p[pi0] = u;
10374: }
10375: }
10376: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10377: printf("%d ",i); fflush(stdout);
10378: ti = BDY((LIST)BDY(t));
1.6 noro 10379: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10380: if ( Demand ) {
10381: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10382: }
10383: }
10384: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10385: printf("%d ",i); fflush(stdout);
10386: ti = BDY((LIST)BDY(t));
1.6 noro 10387: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10388: if ( Demand ) {
10389: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10390: }
10391: }
10392: m = length(ind);
10393: MKVECT(vect,m);
10394: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10395: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10396: if ( !u ) {
1.6 noro 10397: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10398: BDY(vect)[j] = ndtodp(mod,u);
10399: nd_free(u);
10400: } else
10401: BDY(vect)[j] = ndtodp(mod,u);
10402: }
10403: return vect;
10404: }
10405:
10406: void ndv_print_lf(NDV p)
10407: {
10408: NMV m;
10409: int i,len;
10410:
10411: if ( !p ) printf("0\n");
10412: else {
10413: len = LEN(p);
10414: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10415: printf("+");
10416: mpz_out_str(asir_out,10,BDY(CZ(m)));
10417: printf("*");
10418: ndl_print(DL(m));
10419: }
10420: printf("\n");
10421: }
10422: }
10423:
10424: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10425: {
10426: VL tv,fv,vv,vc,av;
10427: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10428: int m,nocheck,nvar,mindex,e,max;
10429: NDV c;
10430: NMV a;
10431: P p,zp;
10432: Q dmy;
10433: EPOS oepos;
10434: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10435: Alg alpha,dp;
10436: P poly;
10437: LIST f1,f2,zpl;
10438: Obj obj;
10439: NumberField nf;
10440: struct order_spec *ord1;
10441: struct oEGT eg_check,eg0,eg1;
10442: NODE tr,tl1,tl2,tl3,tl4;
10443: LIST l1,l2,l3,l4,l5;
10444: int *perm;
10445: int j,ret;
10446: NODE retn;
10447: Q jq,bpe;
10448:
10449: nd_module = 0;
10450: parse_nd_option(current_option);
10451: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10452: if ( nd_vc )
10453: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10454: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10455: switch ( ord->id ) {
10456: case 1:
10457: if ( ord->nv != nvar )
10458: error("nd_f4_lf_trace : invalid order specification");
10459: break;
10460: default:
10461: break;
10462: }
10463:
10464: nd_ntrans = nvar;
10465: nd_nalg = 0;
10466:
10467: nocheck = 0;
10468: mindex = 0;
10469:
10470: /* do not use on-demand load/save */
10471: nd_demand = 0;
10472: m = trace > 1 ? trace : get_lprime(mindex);
10473: nd_init_ord(ord);
10474: mrank = 0;
10475: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10476: for ( tv = vv; tv; tv = NEXT(tv) ) {
10477: if ( nd_module ) {
10478: s = BDY((LIST)BDY(t));
10479: trank = length(s);
10480: mrank = MAX(mrank,trank);
10481: for ( ; s; s = NEXT(s) ) {
10482: e = getdeg(tv->v,(P)BDY(s));
10483: max = MAX(e,max);
10484: }
10485: } else {
10486: e = getdeg(tv->v,(P)BDY(t));
10487: max = MAX(e,max);
10488: }
10489: }
10490: nd_setup_parameters(nvar,max);
10491: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10492: ishomo = 1;
10493: /* XXX */
10494: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10495: if ( nd_module ) {
10496: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10497: } else {
10498: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10499: }
10500: if ( ishomo )
10501: ishomo = ishomo && ndv_ishomo(c);
10502: if ( c ) {
10503: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10504: ndv_mod(-2,c);
10505: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10506: }
10507: }
10508: if ( in0 ) NEXT(in) = 0;
10509: if ( fd0 ) NEXT(fd) = 0;
10510: if ( !ishomo && homo ) {
10511: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10512: c = (NDV)BDY(t); len = LEN(c);
10513: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10514: wmax = MAX(TD(DL(a)),wmax);
10515: }
10516: homogenize_order(ord,nvar,&ord1);
10517: nd_init_ord(ord1);
10518: nd_setup_parameters(nvar+1,wmax);
10519: for ( t = fd0; t; t = NEXT(t) )
10520: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10521: }
10522: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10523: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10524: if ( ret )
10525: cand = nd_f4_lf_trace_main(m,&perm);
10526: if ( !ret || !cand ) {
10527: *rp = 0; return;
10528: }
10529: if ( !ishomo && homo ) {
10530: /* dehomogenization */
10531: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10532: nd_init_ord(ord);
10533: nd_setup_parameters(nvar,0);
10534: }
10535: cand = ndv_reducebase(cand,perm);
10536: cand = ndv_reduceall(-2,cand);
10537: cbpe = nd_bpe;
10538: get_eg(&eg0);
10539: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10540: /* gbcheck : cand is a GB of Id(cand) ? */
10541: retn = nd_f4(-2,0,0);
10542: }
10543: if ( !retn ) {
10544: /* failure */
10545: *rp = 0; return;
10546: }
10547: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10548: if ( DP_Print )
1.5 noro 10549: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10550: /* dp->p */
10551: nd_bpe = cbpe;
10552: nd_setup_parameters(nd_nvar,0);
10553: for ( r = cand; r; r = NEXT(r) ) {
10554: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10555: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10556: }
10557: MKLIST(*rp,cand);
10558: }
10559:
10560: NODE nd_f4_lf_trace_main(int m,int **indp)
10561: {
10562: int i,nh,stat,index;
10563: NODE r,rm,g;
10564: ND_pairs d,l,l0,t;
10565: ND spol,red;
10566: NDV nf,redv,nfqv,nfv;
10567: NM s0,s;
10568: NODE rp0,srp0,nflist,nflist_lf;
10569: int nsp,nred,col,rank,len,k,j,a;
10570: UINT c;
10571: UINT **spmat;
10572: UINT *s0vect,*svect,*p,*v;
10573: int *colstat;
10574: IndArray *imat;
10575: int *rhead;
10576: int spcol,sprow;
10577: int sugar;
10578: PGeoBucket bucket;
10579: struct oEGT eg0,eg1,eg_f4;
10580:
10581: g = 0; d = 0;
10582: for ( i = 0; i < nd_psn; i++ ) {
10583: d = update_pairs(d,g,i,0);
10584: g = update_base(g,i);
10585: }
10586: while ( d ) {
10587: get_eg(&eg0);
10588: l = nd_minsugarp(d,&d);
10589: sugar = SG(l);
10590: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10591: bucket = create_pbucket();
10592: stat = nd_sp_f4(m,0,l,bucket);
10593: if ( !stat ) {
10594: for ( t = l; NEXT(t); t = NEXT(t) );
10595: NEXT(t) = d; d = l;
10596: d = nd_reconstruct(1,d);
10597: continue;
10598: }
10599: if ( bucket->m < 0 ) continue;
10600: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10601: if ( !col ) {
10602: for ( t = l; NEXT(t); t = NEXT(t) );
10603: NEXT(t) = d; d = l;
10604: d = nd_reconstruct(1,d);
10605: continue;
10606: }
10607: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10608: if ( DP_Print )
1.5 noro 10609: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10610: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10611: if ( !l0 ) continue;
10612: l = l0;
10613:
10614: /* over LF */
10615: bucket = create_pbucket();
10616: stat = nd_sp_f4(-2,1,l,bucket);
10617: if ( !stat ) {
10618: for ( t = l; NEXT(t); t = NEXT(t) );
10619: NEXT(t) = d; d = l;
10620: d = nd_reconstruct(1,d);
10621: continue;
10622: }
10623: if ( bucket->m < 0 ) continue;
10624: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10625: if ( !col ) {
10626: for ( t = l; NEXT(t); t = NEXT(t) );
10627: NEXT(t) = d; d = l;
10628: d = nd_reconstruct(1,d);
10629: continue;
10630: }
10631: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10632: /* adding new bases */
10633: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10634: nfv = (NDV)BDY(rm);
10635: nfqv = (NDV)BDY(r);
10636: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10637: ndv_removecont(m,nfv);
10638: ndv_removecont(-2,nfqv);
1.24 noro 10639: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10640: d = update_pairs(d,g,nh,0);
10641: g = update_base(g,nh);
10642: }
10643: if ( r || rm ) return 0;
10644: }
10645: conv_ilist(nd_demand,1,g,indp);
10646: return g;
10647: }
10648:
1.7 noro 10649: #if SIZEOF_LONG==8
10650:
10651: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10652: {
10653: int j,k,len;
10654: UINT *p;
10655: UINT c;
10656: NDV r;
10657: NMV mr0,mr;
10658:
10659: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10660: if ( !len ) return 0;
10661: else {
10662: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10663: #if 0
10664: ndv_alloc += nmv_adv*len;
10665: #endif
10666: mr = mr0;
10667: p = s0vect;
10668: for ( j = k = 0; j < col; j++, p += nd_wpd )
10669: if ( !rhead[j] ) {
10670: if ( (c = (UINT)vect[k++]) != 0 ) {
10671: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10672: }
10673: }
10674: MKNDV(nd_nvar,mr0,len,r);
10675: return r;
10676: }
10677: }
10678:
1.28 noro 10679: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
10680: {
10681: int j,k,len;
10682: UINT *p;
10683: UINT c;
10684: NDV r;
10685: NMV mr0,mr;
10686:
10687: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
10688: if ( !len ) return 0;
10689: else {
10690: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10691: mr = mr0;
10692: p = s0vect;
10693: for ( j = k = 0; j < col; j++, p += nd_wpd )
10694: if ( (c = (UINT)vect[k++]) != 0 ) {
10695: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10696: }
10697: MKNDV(nd_nvar,mr0,len,r);
10698: return r;
10699: }
10700: }
10701:
1.7 noro 10702: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10703: {
10704: NM m;
1.11 noro 10705: UINT *t,*s,*u;
10706: int i,st,ed,md,prev,c;
1.7 noro 10707:
10708: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10709: prev = 0;
10710: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10711: t = DL(m);
10712: st = prev;
10713: ed = n;
10714: while ( ed > st ) {
10715: md = (st+ed)/2;
10716: u = s0+md*nd_wpd;
10717: c = DL_COMPARE(u,t);
10718: if ( c == 0 ) break;
10719: else if ( c > 0 ) st = md;
10720: else ed = md;
10721: }
10722: r[md] = (mp_limb_t)CM(m);
10723: prev = md;
1.7 noro 10724: }
10725: for ( i = 0; !r[i]; i++ );
10726: return i;
10727: }
10728:
10729: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10730:
1.28 noro 10731: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.7 noro 10732: {
10733: int i,j,k,len,pos,prev;
10734: mp_limb_t a,c,c1,c2;
10735: IndArray ivect;
10736: unsigned char *ivc;
10737: unsigned short *ivs;
10738: unsigned int *ivi;
10739: NDV redv;
10740: NMV mr;
10741: NODE rp;
10742: int maxrs;
10743:
10744: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10745: maxrs = 0;
10746: for ( i = 0; i < nred; i++ ) {
10747: ivect = imat[i];
10748: k = ivect->head;
10749: a = svect[k]; c = cvect[k];
10750: MOD128(a,c,m);
10751: svect[k] = a; cvect[k] = 0;
1.28 noro 10752: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 10753: Nf4_red++;
1.7 noro 10754: maxrs = MAX(maxrs,rp0[i]->sugar);
10755: c = m-c; redv = nd_ps[rp0[i]->index];
10756: len = LEN(redv); mr = BDY(redv);
10757: svect[k] = 0; prev = k;
10758: switch ( ivect->width ) {
10759: case 1:
10760: ivc = ivect->index.c;
10761: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10762: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10763: c2 = svect[pos]+c1*c;
10764: if ( c2 < svect[pos] ) cvect[pos]++;
10765: svect[pos] = c2;
1.7 noro 10766: }
10767: break;
10768: case 2:
10769: ivs = ivect->index.s;
10770: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10771: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10772: c2 = svect[pos]+c1*c;
10773: if ( c2 < svect[pos] ) cvect[pos]++;
10774: svect[pos] = c2;
1.7 noro 10775: }
10776: break;
10777: case 4:
10778: ivi = ivect->index.i;
10779: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10780: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10781: c2 = svect[pos]+c1*c;
10782: if ( c2 < svect[pos] ) cvect[pos]++;
10783: svect[pos] = c2;
1.7 noro 10784: }
10785: break;
10786: }
10787: }
10788: }
10789: for ( i = 0; i < col; i++ ) {
10790: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10791: }
10792: return maxrs;
10793: }
10794:
10795: /* for Fp, 2^15=<p<2^29 */
10796:
10797: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10798: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10799: {
10800: int spcol,sprow,a;
10801: int i,j,k,l,rank;
10802: NODE r0,r;
10803: ND_pairs sp;
10804: ND spol;
10805: mp_limb_t **spmat;
10806: mp_limb_t *svect,*cvect;
10807: mp_limb_t *v;
10808: int *colstat;
10809: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10810: int maxrs;
10811: int *spsugar;
10812: ND_pairs *spactive;
10813:
10814: spcol = col-nred;
10815: get_eg(&eg0);
10816: /* elimination (1st step) */
10817: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10818: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10819: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10820: spsugar = (int *)MALLOC(nsp*sizeof(int));
10821: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10822: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10823: nd_sp(m,0,sp,&spol);
10824: if ( !spol ) continue;
10825: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 10826: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 10827: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10828: if ( i < col ) {
10829: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10830: for ( j = k = 0; j < col; j++ )
10831: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10832: spsugar[sprow] = MAX(maxrs,SG(spol));
10833: if ( nz )
10834: spactive[sprow] = sp;
10835: sprow++;
10836: }
10837: nd_free(spol);
10838: }
1.12 noro 10839: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10840: if ( DP_Print ) {
10841: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10842: fflush(asir_out);
10843: }
10844: /* free index arrays */
10845: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10846:
10847: /* elimination (2nd step) */
10848: colstat = (int *)MALLOC(spcol*sizeof(int));
10849: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10850: r0 = 0;
10851: for ( i = 0; i < rank; i++ ) {
10852: NEXTNODE(r0,r); BDY(r) =
10853: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10854: SG((NDV)BDY(r)) = spsugar[i];
10855: GCFREE(spmat[i]);
10856: }
10857: if ( r0 ) NEXT(r) = 0;
10858:
10859: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10860: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10861: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10862: if ( DP_Print ) {
10863: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10864: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10865: nsp,nred,sprow,spcol,rank);
10866: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10867: }
10868: if ( nz ) {
10869: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10870: if ( rank > 0 ) {
10871: NEXT(spactive[rank-1]) = 0;
10872: *nz = spactive[0];
10873: } else
10874: *nz = 0;
10875: }
10876: return r0;
10877: }
10878:
10879: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10880: {
10881: int i,j,k,l,rank,s;
10882: mp_limb_t inv;
10883: mp_limb_t a;
10884: UINT c;
10885: mp_limb_t *t,*pivot,*pk;
10886: UINT *ck;
10887: UINT **cmat;
10888: UINT *ct;
10889: ND_pairs pair;
10890:
10891: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10892: for ( i = 0; i < row; i++ ) {
10893: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10894: bzero(cmat[i],col*sizeof(UINT));
10895: }
10896:
10897: for ( rank = 0, j = 0; j < col; j++ ) {
10898: for ( i = rank; i < row; i++ ) {
10899: a = mat[i][j]; c = cmat[i][j];
10900: MOD128(a,c,md);
10901: mat[i][j] = a; cmat[i][j] = 0;
10902: }
10903: for ( i = rank; i < row; i++ )
10904: if ( mat[i][j] )
10905: break;
10906: if ( i == row ) {
10907: colstat[j] = 0;
10908: continue;
10909: } else
10910: colstat[j] = 1;
10911: if ( i != rank ) {
10912: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10913: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10914: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10915: if ( spactive ) {
10916: pair = spactive[i]; spactive[i] = spactive[rank];
10917: spactive[rank] = pair;
10918: }
10919: }
10920: /* column j is normalized */
10921: s = sugar[rank];
10922: inv = invm((UINT)mat[rank][j],md);
10923: /* normalize pivot row */
10924: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10925: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10926: }
10927: for ( i = rank+1; i < row; i++ ) {
10928: if ( (a = mat[i][j]) != 0 ) {
10929: sugar[i] = MAX(sugar[i],s);
10930: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10931: Nf4_red++;
1.7 noro 10932: }
10933: }
10934: rank++;
10935: }
10936: for ( j = col-1, l = rank-1; j >= 0; j-- )
10937: if ( colstat[j] ) {
10938: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10939: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10940: }
10941: s = sugar[l];
10942: for ( i = 0; i < l; i++ ) {
10943: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10944: if ( a ) {
10945: sugar[i] = MAX(sugar[i],s);
10946: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10947: Nf4_red++;
1.7 noro 10948: }
10949: }
10950: l--;
10951: }
10952: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10953: GCFREE(cmat);
10954: return rank;
10955: }
10956:
1.28 noro 10957: int nd_gauss_elim_mod64_s(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
10958: {
10959: int i,j,k,l,rank,s,imin;
10960: mp_limb_t inv;
10961: mp_limb_t a;
10962: UINT c;
10963: mp_limb_t *t,*pivot,*pk;
10964: UINT *ck;
10965: UINT **cmat;
10966: UINT *ct;
10967: ND_pairs pair;
10968: SIG sg;
1.31 noro 10969: int *used;
1.28 noro 10970:
1.31 noro 10971: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 10972: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10973: for ( i = 0; i < row; i++ ) {
10974: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10975: bzero(cmat[i],col*sizeof(UINT));
10976: }
10977:
1.31 noro 10978: for ( j = 0; j < col; j++ ) {
10979: for ( i = 0; i < row; i++ ) {
1.28 noro 10980: a = mat[i][j]; c = cmat[i][j];
10981: MOD128(a,c,md);
10982: mat[i][j] = a; cmat[i][j] = 0;
10983: }
1.31 noro 10984: for ( i = 0; i < row; i++ )
10985: if ( !used[i] && mat[i][j] ) break;
10986: if ( i == row ) {
1.28 noro 10987: colstat[j] = 0;
10988: continue;
1.31 noro 10989: } else {
1.28 noro 10990: colstat[j] = 1;
1.31 noro 10991: used[i] = 1;
1.28 noro 10992: }
10993: /* column j is normalized */
1.31 noro 10994: s = sugar[i];
10995: inv = invm((UINT)mat[i][j],md);
1.28 noro 10996: /* normalize pivot row */
1.31 noro 10997: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 10998: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10999: }
1.31 noro 11000: for ( k = i+1; k < row; k++ ) {
11001: if ( (a = mat[k][j]) != 0 ) {
11002: sugar[k] = MAX(sugar[k],s);
11003: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11004: Nf4_red++;
11005: }
11006: }
11007: }
1.31 noro 11008: rank = 0;
11009: for ( i = 0; i < row; i++ ) {
11010: for ( j = 0; j < col; j++ )
11011: if ( mat[i][j] ) break;
11012: if ( j == col ) sugar[i] = -1;
11013: else rank++;
11014: }
1.28 noro 11015: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11016: GCFREE(cmat);
11017: return rank;
11018: }
11019:
11020: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11021: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11022: {
11023: int spcol,sprow,a;
11024: int i,j,k,l,rank;
11025: NODE r0,r;
11026: ND_pairs sp;
11027: ND spol;
11028: mp_limb_t **spmat;
11029: mp_limb_t *svect,*cvect;
11030: mp_limb_t *v;
11031: int *colstat;
11032: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11033: int maxrs;
11034: int *spsugar;
11035: ND_pairs *spactive;
11036: SIG *spsig;
11037:
11038: get_eg(&eg0);
11039: /* elimination (1st step) */
11040: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11041: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11042: spsugar = (int *)MALLOC(nsp*sizeof(int));
11043: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11044: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11045: nd_sp(m,0,sp,&spol);
11046: if ( !spol ) {
1.29 noro 11047: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11048: continue;
11049: }
11050: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11051: nd_to_vect64(m,s0vect,col,spol,svect);
11052: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11053: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11054: if ( i < col ) {
11055: spmat[sprow] = svect;
11056: spsugar[sprow] = MAX(maxrs,SG(spol));
11057: spsig[sprow] = sp->sig;
11058: sprow++;
11059: } else {
1.29 noro 11060: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11061: }
11062: nd_free(spol);
11063: }
11064: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11065: if ( DP_Print ) {
11066: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11067: fflush(asir_out);
11068: }
11069: /* free index arrays */
11070: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11071:
11072: /* elimination (2nd step) */
11073: colstat = (int *)MALLOC(col*sizeof(int));
11074: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11075: r0 = 0;
1.31 noro 11076: for ( i = 0; i < sprow; i++ ) {
11077: if ( spsugar[i] >= 0 ) {
11078: NEXTNODE(r0,r);
11079: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11080: SG((NDV)BDY(r)) = spsugar[i];
11081: ((NDV)BDY(r))->sig = spsig[i];
11082: } else
11083: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11084: GCFREE(spmat[i]);
11085: }
11086: if ( r0 ) NEXT(r) = 0;
11087: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11088: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11089: if ( DP_Print ) {
11090: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11091: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11092: nsp,nred,sprow,col,rank);
11093: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11094: }
11095: return r0;
11096: }
1.40 noro 11097: #endif
1.28 noro 11098:
11099: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11100: {
11101: IndArray *imat;
11102: int nsp,nred,i,start;
11103: int *rhead;
11104: NODE r0,rp;
11105: ND_pairs sp;
11106: NM_ind_pair *rvect;
11107: UINT *s;
11108: int *s0hash;
11109: struct oEGT eg0,eg1,eg_conv;
11110:
11111: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11112: nred = length(rp0);
11113: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11114: rhead = (int *)MALLOC(col*sizeof(int));
11115: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11116:
11117: /* construction of index arrays */
11118: get_eg(&eg0);
11119: if ( DP_Print ) {
11120: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11121: fflush(asir_out);
11122: }
11123: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11124: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11125: rvect[i] = (NM_ind_pair)BDY(rp);
11126: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11127: rhead[imat[i]->head] = 1;
11128: start = imat[i]->head;
11129: }
11130: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11131: if ( DP_Print ) {
11132: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11133: fflush(asir_out);
11134: }
11135: if ( m > 0 )
1.40 noro 11136: #if SIZEOF_LONG==8
1.28 noro 11137: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11138: #else
11139: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11140: #endif
1.28 noro 11141: else
11142: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11143: error("nd_f4_red_q_main_s : not implemented yet");
11144: return r0;
11145: }
11146:
11147: INLINE int ndl_find_reducer_minsig(UINT *dg)
11148: {
11149: RHist r;
11150: int i,singular,ret,d,k,imin;
11151: SIG t;
11152: static int wpd,nvar;
11153: static SIG quo,quomin;
11154: static UINT *tmp;
11155:
11156: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11157: if ( wpd != nd_wpd ) {
11158: wpd = nd_wpd;
11159: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11160: }
11161: #if 0
11162: d = ndl_hash_value(dg);
11163: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11164: if ( ndl_equal(dg,DL(r)) ) {
11165: return r->index;
11166: }
11167: }
11168: #endif
11169: imin = -1;
11170: for ( i = 0; i < nd_psn; i++ ) {
11171: r = nd_psh[i];
11172: if ( ndl_reducible(dg,DL(r)) ) {
11173: ndl_sub(dg,DL(r),tmp);
11174: _ndltodl(tmp,DL(quo));
11175: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11176: quo->pos = nd_psh[i]->sig->pos;
11177: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11178: t = quo; quo = quomin; quomin = t;
11179: imin = i;
11180: }
11181: }
11182: }
11183: if ( imin == -1 ) return nd_psn;
11184: else {
11185: #if 0
11186: nd_append_red(dg,i);
11187: #endif
11188: return imin;
11189: }
11190: }
11191:
11192: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11193: {
11194: NODE rp0,rp;
11195: NM mul,head,s0,s;
11196: int index,col,i,sugar;
11197: RHist h;
11198: UINT *s0v,*p;
11199: NM_ind_pair pair;
11200: ND red;
11201: NDV *ps;
11202: SIG sig;
11203:
11204: s0 = 0; rp0 = 0; col = 0;
11205: if ( nd_demand )
11206: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11207: else
11208: ps = trace?nd_ps_trace:nd_ps;
11209: while ( 1 ) {
11210: head = remove_head_pbucket_symbolic(bucket);
11211: if ( !head ) break;
11212: if ( !s0 ) s0 = head;
11213: else NEXT(s) = head;
11214: s = head;
11215: index = ndl_find_reducer_minsig(DL(head));
11216: if ( index >= 0 && index < nd_psn ) {
11217: h = nd_psh[index];
11218: NEWNM(mul);
11219: ndl_sub(DL(head),DL(h),DL(mul));
11220: if ( ndl_check_bound2(index,DL(mul)) )
11221: return 0;
11222: sugar = TD(DL(mul))+SG(ps[index]);
11223: NEWSIG(sig);
11224: _ndltodl(DL(mul),DL(sig));
11225: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11226: sig->pos = nd_psh[index]->sig->pos;
11227: MKNM_ind_pair(pair,mul,index,sugar,sig);
11228: red = ndv_mul_nm_symbolic(mul,ps[index]);
11229: add_pbucket_symbolic(bucket,nd_remove_head(red));
11230: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11231: }
11232: col++;
11233: }
11234: if ( rp0 ) NEXT(rp) = 0;
11235: NEXT(s) = 0;
11236: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11237: for ( i = 0, p = s0v, s = s0; i < col;
11238: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11239: *s0vect = s0v;
11240: *r = rp0;
11241:
11242: return col;
11243: }
11244:
11245: NODE nd_sba_f4(int m,int **indp)
11246: {
1.34 noro 11247: int i,nh,stat,index,f4red,f4step;
1.33 noro 11248: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11249: NODE r,g,rp0,nflist;
1.41 noro 11250: ND_pairs d,l,t,l1;
1.33 noro 11251: ND h,nf;
11252: NDV nfv;
11253: union oNDC hc;
11254: UINT *s0vect;
1.28 noro 11255: UINT c;
11256: PGeoBucket bucket;
1.33 noro 11257: NODE *syzlist;
11258: SIG sig;
1.28 noro 11259: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11260: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11261:
11262: Nf4_red=0;
1.30 noro 11263: d = 0;
1.29 noro 11264: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11265: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11266: d = update_pairs_s(d,i,syzlist);
1.28 noro 11267: }
1.30 noro 11268: nd_nbase = nd_psn;
1.28 noro 11269: f4red = 1;
1.33 noro 11270: psugar = 0;
1.34 noro 11271: f4step = 0;
1.28 noro 11272: while ( d ) {
1.33 noro 11273: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11274: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11275: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11276: again:
11277: l = d; d = d->next;
1.41 noro 11278: #if 0
1.33 noro 11279: if ( small_lcm(l) ) {
11280: if ( DP_Print ) fprintf(asir_out,"M");
11281: continue;
11282: }
11283: sig = l->sig;
11284: stat = nd_sp(m,0,l,&h);
1.41 noro 11285: #else
11286: l1 = find_smallest_lcm(l);
11287: if ( l1 == 0 ) {
11288: if ( DP_Print ) fprintf(asir_out,"M");
11289: continue;
11290: }
11291: sig = l1->sig;
11292: stat = nd_sp(m,0,l1,&h);
11293: #endif
1.33 noro 11294: if ( !stat ) {
11295: NEXT(l) = d; d = l;
11296: d = nd_reconstruct(0,d);
11297: goto again;
11298: }
11299: get_eg(&eg1);
11300: #if USE_GEOBUCKET
1.39 noro 11301: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11302: #else
1.39 noro 11303: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11304: #endif
11305: get_eg(&eg2);
11306: if ( !stat ) {
11307: NEXT(l) = d; d = l;
11308: d = nd_reconstruct(0,d);
11309: goto again;
11310: } else if ( stat == -1 ) {
11311: if ( DP_Print ) { printf("S"); fflush(stdout); }
11312: FREENDP(l);
11313: } else if ( nf ) {
11314: if ( DP_Print ) { printf("+"); fflush(stdout); }
11315: add_eg(&eg_nf,&eg1,&eg2);
11316: hc = HCU(nf);
11317: nd_removecont(m,nf);
11318: nfv = ndtondv(m,nf); nd_free(nf);
11319: nh = ndv_newps(m,nfv,0);
11320:
1.30 noro 11321: d = update_pairs_s(d,nh,syzlist);
11322: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 11323: FREENDP(l);
1.28 noro 11324: } else {
1.33 noro 11325: add_eg(&eg_nfzero,&eg1,&eg2);
11326: // syzygy
11327: get_eg(&eg1);
11328: d = remove_spair_s(d,sig);
11329: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 11330: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 11331: if ( DP_Print ) { printf("."); fflush(stdout); }
11332: FREENDP(l);
11333: }
11334: } else {
1.34 noro 11335: if ( ms != psugar ) f4step = 1;
11336: else f4step++;
1.33 noro 11337: again2:
11338: psugar = ms;
11339: l = nd_minsugarp_s(d,&d);
11340: sugar = nd_sugarweight?d->sugar2:SG(d);
11341: bucket = create_pbucket();
11342: stat = nd_sp_f4(m,0,l,bucket);
11343: if ( !stat ) {
11344: for ( t = l; NEXT(t); t = NEXT(t) );
11345: NEXT(t) = d; d = l;
11346: d = nd_reconstruct(0,d);
11347: goto again2;
11348: }
11349: if ( bucket->m < 0 ) continue;
11350: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
11351: if ( !col ) {
1.37 noro 11352: for ( t = l; NEXT(t); t = NEXT(t) )
11353: ;
11354: NEXT(t) = d; d = l;
1.33 noro 11355: d = nd_reconstruct(0,d);
11356: goto again2;
11357: }
1.34 noro 11358: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 11359: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
11360: /* adding new bases */
11361: for ( r = nflist; r; r = NEXT(r) ) {
11362: nfv = (NDV)BDY(r);
1.38 noro 11363: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 11364: ndv_removecont(m,nfv);
11365: nh = ndv_newps(m,nfv,0);
11366: d = update_pairs_s(d,nh,syzlist);
11367: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
11368: }
11369: for ( i = 0; i < nd_nbase; i++ )
11370: for ( r = syzlist[i]; r; r = NEXT(r) )
11371: d = remove_spair_s(d,(SIG)BDY(r));
11372: d = remove_large_lcm(d);
11373: if ( DP_Print ) {
1.34 noro 11374: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11375: }
11376: f4red++;
1.28 noro 11377: }
11378: }
11379: if ( DP_Print ) {
1.34 noro 11380: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11381: }
1.30 noro 11382: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11383: return g;
11384: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>