Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.41
1.41 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.40 2020/11/02 08:30:55 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);
4506: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4507: for ( t = fd0; t; t = NEXT(t) )
4508: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4509: }
4510:
1.39 noro 4511: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.28 noro 4512: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24 noro 4513: if ( !x ) {
4514: *rp = 0; return;
4515: }
4516: if ( !ishomo && homo ) {
4517: /* dehomogenization */
4518: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4519: nd_init_ord(ord);
4520: nd_setup_parameters(nvar,0);
4521: }
4522: nd_demand = 0;
4523: x = ndv_reducebase(x,perm);
4524: x = ndv_reduceall(m,x);
4525: nd_setup_parameters(nd_nvar,0);
1.29 noro 4526: get_eg(&eg0);
1.24 noro 4527: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4528: NEXTNODE(r0,r);
4529: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4530: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4531: }
4532: if ( r0 ) NEXT(r) = 0;
4533: MKLIST(*rp,r0);
1.29 noro 4534: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4535: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4536: }
4537:
1.1 noro 4538: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4539: {
4540: VL tv,fv,vv,vc,av;
4541: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4542: int e,max,nvar,i;
4543: NDV b;
4544: int ishomo,nalg;
4545: Alg alpha,dp;
4546: P p,zp;
4547: Q dmy;
4548: LIST f1,f2;
4549: Obj obj;
4550: NumberField nf;
4551: struct order_spec *ord1;
4552: int *perm;
4553:
4554: parse_nd_option(current_option);
4555: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4556: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4557: switch ( ord->id ) {
4558: case 1:
4559: if ( ord->nv != nvar )
4560: error("nd_check : invalid order specification");
4561: break;
4562: default:
4563: break;
4564: }
4565: nd_nalg = 0;
4566: av = 0;
4567: if ( !m ) {
4568: get_algtree((Obj)f,&av);
4569: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4570: nd_ntrans = nvar;
4571: nd_nalg = nalg;
4572: /* #i -> t#i */
4573: if ( nalg ) {
4574: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4575: ord = ord1;
4576: f = f1;
4577: }
4578: nvar += nalg;
4579: }
4580: nd_init_ord(ord);
4581: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4582: for ( tv = vv; tv; tv = NEXT(tv) ) {
4583: e = getdeg(tv->v,(P)BDY(t));
4584: max = MAX(e,max);
4585: }
4586: nd_setup_parameters(nvar,max);
4587: ishomo = 1;
4588: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4589: ptozp((P)BDY(t),1,&dmy,&zp);
4590: b = (pointer)ptondv(CO,vv,zp);
4591: if ( ishomo )
4592: ishomo = ishomo && ndv_ishomo(b);
4593: if ( m ) ndv_mod(m,b);
4594: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4595: }
4596: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4597: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4598: for ( x = 0, i = 0; i < nd_psn; i++ )
4599: x = update_base(x,i);
4600: if ( do_check ) {
4601: x = nd_gb(m,ishomo,1,0,&perm);
4602: if ( !x ) {
4603: *rp = 0;
4604: return;
4605: }
4606: } else {
4607: #if 0
4608: /* bug ? */
4609: for ( t = x; t; t = NEXT(t) )
4610: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4611: #else
4612: conv_ilist(0,0,x,&perm);
4613: #endif
4614: }
4615: x = ndv_reducebase(x,perm);
4616: x = ndv_reduceall(m,x);
4617: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4618: NEXTNODE(r0,r);
4619: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4620: }
4621: if ( r0 ) NEXT(r) = 0;
4622: if ( !m && nd_nalg )
4623: r0 = postprocess_algcoef(av,alist,r0);
4624: MKLIST(*rp,r0);
4625: }
4626:
4627: NDV recompute_trace(NODE trace,NDV *p,int m);
4628: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4629:
4630: NDV recompute_trace(NODE ti,NDV *p,int mod)
4631: {
4632: int c,c1,c2,i;
4633: NM mul,m,tail;
4634: ND d,r,rm;
4635: NODE sj;
4636: NDV red;
4637: Obj mj;
4638:
4639: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4640: CM(mul) = 1;
4641: tail = 0;
4642: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4643: sj = BDY((LIST)BDY(ti));
4644: if ( ARG0(sj) ) {
1.6 noro 4645: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4646: mj = (Obj)ARG2(sj);
4647: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4648: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4649: rm = ndv_mul_nm(mod,mul,red);
4650: if ( !r ) r = rm;
4651: else {
4652: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4653: if ( d ) {
4654: NEXT(tail) = m; tail = m; LEN(d)++;
4655: } else {
4656: MKND(nd_nvar,m,1,d); tail = BDY(d);
4657: }
4658: }
4659: if ( !m ) return 0; /* failure */
4660: else {
4661: BDY(r) = m;
4662: if ( mod > 0 || mod == -1 ) {
4663: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4664: DMAR(c1,c2,0,mod,c);
4665: nd_mul_c(mod,rm,c);
4666: } else {
4667: Z t,u;
4668:
4669: chsgnlf(HCZ(r),&t);
4670: divlf(t,HCZ(rm),&u);
4671: nd_mul_c_lf(rm,u);
4672: }
4673: r = nd_add(mod,r,rm);
4674: }
4675: }
4676: }
4677: }
4678: if ( tail ) NEXT(tail) = 0;
4679: d = nd_add(mod,d,r);
4680: nd_mul_c(mod,d,invm(HCM(d),mod));
4681: return ndtondv(mod,d);
4682: }
4683:
4684: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4685: {
4686: VL tv,fv,vv,vc,av;
4687: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4688: int e,max,nvar,i;
4689: NDV b;
4690: int ishomo,nalg;
4691: Alg alpha,dp;
4692: P p,zp;
4693: Q dmy;
4694: LIST f1,f2;
4695: Obj obj;
4696: NumberField nf;
4697: struct order_spec *ord1;
4698: NODE permtrace,intred,ind,perm,trace,ti;
4699: int len,n,j;
4700: NDV *db,*pb;
4701:
4702: parse_nd_option(current_option);
4703: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4704: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4705: switch ( ord->id ) {
4706: case 1:
4707: if ( ord->nv != nvar )
4708: error("nd_check : invalid order specification");
4709: break;
4710: default:
4711: break;
4712: }
4713: nd_init_ord(ord);
1.6 noro 4714: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4715: nd_setup_parameters(nvar,0);
4716:
4717: len = length(BDY(f));
4718: db = (NDV *)MALLOC(len*sizeof(NDV *));
4719: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4720: ptozp((P)BDY(t),1,&dmy,&zp);
4721: b = ptondv(CO,vv,zp);
4722: ndv_mod(m,b);
4723: ndv_mul_c(m,b,invm(HCM(b),m));
4724: db[i] = b;
4725: }
4726:
4727: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4728: intred = BDY((LIST)ARG3(BDY(tlist)));
4729: ind = BDY((LIST)ARG4(BDY(tlist)));
4730: perm = BDY((LIST)ARG0(permtrace));
4731: trace = NEXT(permtrace);
4732:
4733: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4734: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4735: if ( j > i ) i = j;
4736: }
4737: n = i+1;
4738: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4739: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4740: ti = BDY((LIST)BDY(t));
1.6 noro 4741: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4742: }
4743: for ( t = trace; t; t = NEXT(t) ) {
4744: ti = BDY((LIST)BDY(t));
1.6 noro 4745: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4746: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4747: if ( DP_Print ) {
4748: fprintf(asir_out,"."); fflush(asir_out);
4749: }
4750: }
4751: for ( t = intred; t; t = NEXT(t) ) {
4752: ti = BDY((LIST)BDY(t));
1.6 noro 4753: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4754: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4755: if ( DP_Print ) {
4756: fprintf(asir_out,"*"); fflush(asir_out);
4757: }
4758: }
4759: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4760: NEXTNODE(r0,r);
1.6 noro 4761: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4762: ndv_mul_c(m,b,invm(HCM(b),m));
4763: #if 0
1.6 noro 4764: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4765: #else
1.6 noro 4766: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4767: #endif
4768: }
4769: if ( r0 ) NEXT(r) = 0;
4770: MKLIST(*rp,r0);
4771: if ( DP_Print ) fprintf(asir_out,"\n");
4772: }
4773:
1.16 noro 4774: 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 4775: {
4776: VL tv,fv,vv,vc,av;
4777: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4778: int m,nocheck,nvar,mindex,e,max;
4779: NDV c;
4780: NMV a;
4781: P p,zp;
4782: Q dmy;
4783: EPOS oepos;
4784: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4785: Alg alpha,dp;
4786: P poly;
4787: LIST f1,f2,zpl;
4788: Obj obj;
4789: NumberField nf;
4790: struct order_spec *ord1;
4791: struct oEGT eg_check,eg0,eg1;
4792: NODE tr,tl1,tl2,tl3,tl4;
4793: LIST l1,l2,l3,l4,l5;
4794: int *perm;
4795: int j,ret;
4796: Z jq,bpe;
1.15 noro 4797: VECT hvect;
1.1 noro 4798:
1.41 ! noro 4799: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4800: nd_module = 0;
4801: nd_lf = 0;
4802: parse_nd_option(current_option);
4803: if ( nd_lf ) {
4804: if ( f4 )
4805: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4806: else
4807: error("nd_gr_trace is not implemented yet over a large finite field");
4808: return;
4809: }
4810: if ( DP_Multiple )
4811: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4812:
4813: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4814: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4815: switch ( ord->id ) {
4816: case 1:
4817: if ( ord->nv != nvar )
4818: error("nd_gr_trace : invalid order specification");
4819: break;
4820: default:
4821: break;
4822: }
4823:
4824: get_algtree((Obj)f,&av);
4825: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4826: nd_ntrans = nvar;
4827: nd_nalg = nalg;
4828: /* #i -> t#i */
4829: if ( nalg ) {
4830: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4831: ord = ord1;
4832: f = f1;
4833: }
4834: nvar += nalg;
4835:
4836: nocheck = 0;
4837: mindex = 0;
4838:
4839: if ( Demand ) nd_demand = 1;
4840: else nd_demand = 0;
4841:
4842: /* setup modulus */
4843: if ( trace < 0 ) {
4844: trace = -trace;
4845: nocheck = 1;
4846: }
4847: m = trace > 1 ? trace : get_lprime(mindex);
4848: nd_init_ord(ord);
4849: mrank = 0;
4850: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4851: for ( tv = vv; tv; tv = NEXT(tv) ) {
4852: if ( nd_module ) {
1.16 noro 4853: if ( OID(BDY(t)) == O_DPM ) {
4854: e = dpm_getdeg((DPM)BDY(t),&trank);
4855: max = MAX(e,max);
4856: mrank = MAX(mrank,trank);
4857: } else {
1.1 noro 4858: s = BDY((LIST)BDY(t));
4859: trank = length(s);
4860: mrank = MAX(mrank,trank);
4861: for ( ; s; s = NEXT(s) ) {
4862: e = getdeg(tv->v,(P)BDY(s));
4863: max = MAX(e,max);
4864: }
1.16 noro 4865: }
1.1 noro 4866: } else {
4867: e = getdeg(tv->v,(P)BDY(t));
4868: max = MAX(e,max);
4869: }
4870: }
4871: nd_setup_parameters(nvar,max);
4872: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4873: ishomo = 1;
4874: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4875: if ( nd_module ) {
1.16 noro 4876: if ( OID(BDY(t)) == O_DPM ) {
4877: Z cont;
4878: DPM zdpm;
4879:
1.17 noro 4880: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4881: else zdpm = (DPM)BDY(t);
4882: c = (pointer)dpmtondv(m,zdpm);
4883: } else {
4884: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4885: else zpl = (LIST)BDY(t);
1.1 noro 4886: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4887: }
1.1 noro 4888: } else {
1.16 noro 4889: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4890: else zp = (P)BDY(t);
4891: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4892: }
4893: if ( ishomo )
4894: ishomo = ishomo && ndv_ishomo(c);
4895: if ( c ) {
4896: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4897: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4898: }
4899: }
4900: if ( in0 ) NEXT(in) = 0;
4901: if ( fd0 ) NEXT(fd) = 0;
4902: if ( !ishomo && homo ) {
4903: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4904: c = (NDV)BDY(t); len = LEN(c);
4905: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4906: wmax = MAX(TD(DL(a)),wmax);
4907: }
4908: homogenize_order(ord,nvar,&ord1);
4909: nd_init_ord(ord1);
4910: nd_setup_parameters(nvar+1,wmax);
4911: for ( t = fd0; t; t = NEXT(t) )
4912: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4913: }
4914: if ( MaxDeg > 0 ) nocheck = 1;
4915: while ( 1 ) {
4916: tl1 = tl2 = tl3 = tl4 = 0;
4917: if ( Demand )
4918: nd_demand = 1;
1.24 noro 4919: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4920: if ( nd_gentrace ) {
4921: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4922: }
4923: if ( ret )
4924: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4925: if ( !ret || !cand ) {
4926: /* failure */
4927: if ( trace > 1 ) { *rp = 0; return; }
4928: else m = get_lprime(++mindex);
4929: continue;
4930: }
1.15 noro 4931: if ( nd_gentrace ) {
4932: MKVECT(hvect,nd_psn);
4933: for ( i = 0; i < nd_psn; i++ )
4934: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4935: }
1.1 noro 4936: if ( !ishomo && homo ) {
4937: /* dehomogenization */
4938: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4939: nd_init_ord(ord);
4940: nd_setup_parameters(nvar,0);
4941: }
4942: nd_demand = 0;
4943: cand = ndv_reducebase(cand,perm);
4944: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4945: cand = ndv_reduceall(0,cand);
4946: cbpe = nd_bpe;
4947: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4948: get_eg(&eg0);
4949: if ( nocheck )
4950: break;
4951: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4952: if ( nd_gentrace ) {
4953: tl3 = nd_alltracelist; nd_alltracelist = 0;
4954: } else tl3 = 0;
4955: /* gbcheck : cand is a GB of Id(cand) ? */
4956: if ( nd_vc || nd_gentrace || nd_gensyz )
4957: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4958: else
4959: ret = nd_f4(0,1,0)!=0;
4960: if ( nd_gentrace && nd_gensyz ) {
4961: tl4 = nd_alltracelist; nd_alltracelist = 0;
4962: } else tl4 = 0;
4963: }
4964: if ( ret ) break;
4965: else if ( trace > 1 ) {
4966: /* failure */
4967: *rp = 0; return;
4968: } else {
4969: /* try the next modulus */
4970: m = get_lprime(++mindex);
4971: /* reset the parameters */
4972: if ( !ishomo && homo ) {
4973: nd_init_ord(ord1);
4974: nd_setup_parameters(nvar+1,wmax);
4975: } else {
4976: nd_init_ord(ord);
4977: nd_setup_parameters(nvar,max);
4978: }
4979: }
4980: }
4981: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4982: if ( DP_Print )
1.6 noro 4983: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4984: /* dp->p */
4985: nd_bpe = cbpe;
4986: nd_setup_parameters(nd_nvar,0);
4987: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4988: if ( nd_module ) {
1.17 noro 4989: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4990: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4991: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4992: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4993: }
4994: if ( nd_nalg )
4995: cand = postprocess_algcoef(av,alist,cand);
4996: MKLIST(*rp,cand);
4997: if ( nd_gentrace ) {
4998: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4999: tl3 = reverse_node(tl3);
5000: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5001: for ( t = tl2; t; t = NEXT(t) ) {
5002: /* s = [i,[*,j,*,*],...] */
5003: s = BDY((LIST)BDY(t));
1.6 noro 5004: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5005: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5006: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5007: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5008: }
5009: }
5010: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5011: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5012: }
5013: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5014: MKLIST(l5,tl4);
1.6 noro 5015: STOZ(nd_bpe,bpe);
1.15 noro 5016: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5017: }
5018: }
5019:
5020: /* XXX : module element is not considered */
5021:
5022: void dltondl(int n,DL dl,UINT *r)
5023: {
5024: UINT *d;
5025: int i,j,l,s,ord_l;
5026: struct order_pair *op;
5027:
5028: d = (unsigned int *)dl->d;
5029: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5030: if ( nd_blockmask ) {
5031: l = nd_blockmask->n;
5032: op = nd_blockmask->order_pair;
5033: for ( j = 0, s = 0; j < l; j++ ) {
5034: ord_l = op[j].length;
5035: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5036: }
5037: TD(r) = ndl_weight(r);
5038: ndl_weight_mask(r);
5039: } else {
5040: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5041: TD(r) = ndl_weight(r);
5042: }
5043: }
5044:
5045: DL ndltodl(int n,UINT *ndl)
5046: {
5047: DL dl;
5048: int *d;
5049: int i,j,l,s,ord_l;
5050: struct order_pair *op;
5051:
1.33 noro 5052: NEWDL_NOINIT(dl,n);
1.1 noro 5053: dl->td = TD(ndl);
5054: d = dl->d;
5055: if ( nd_blockmask ) {
5056: l = nd_blockmask->n;
5057: op = nd_blockmask->order_pair;
5058: for ( j = 0, s = 0; j < l; j++ ) {
5059: ord_l = op[j].length;
5060: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5061: }
5062: } else {
5063: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5064: }
5065: return dl;
5066: }
5067:
1.24 noro 5068: void _ndltodl(UINT *ndl,DL dl)
5069: {
5070: int *d;
5071: int i,j,l,s,ord_l,n;
5072: struct order_pair *op;
5073:
5074: n = nd_nvar;
5075: dl->td = TD(ndl);
5076: d = dl->d;
5077: if ( nd_blockmask ) {
5078: l = nd_blockmask->n;
5079: op = nd_blockmask->order_pair;
5080: for ( j = 0, s = 0; j < l; j++ ) {
5081: ord_l = op[j].length;
5082: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5083: }
5084: } else {
5085: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5086: }
5087: }
5088:
1.1 noro 5089: void nmtodp(int mod,NM m,DP *r)
5090: {
5091: DP dp;
5092: MP mr;
5093:
5094: NEWMP(mr);
5095: mr->dl = ndltodl(nd_nvar,DL(m));
5096: mr->c = (Obj)ndctop(mod,m->c);
5097: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5098: *r = dp;
5099: }
5100:
1.15 noro 5101: void ndltodp(UINT *d,DP *r)
5102: {
5103: DP dp;
5104: MP mr;
5105:
5106: NEWMP(mr);
5107: mr->dl = ndltodl(nd_nvar,d);
5108: mr->c = (Obj)ONE;
5109: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5110: *r = dp;
5111: }
5112:
1.1 noro 5113: void ndl_print(UINT *dl)
5114: {
5115: int n;
5116: int i,j,l,ord_l,s,s0;
5117: struct order_pair *op;
5118:
5119: n = nd_nvar;
5120: printf("<<");
5121: if ( nd_blockmask ) {
5122: l = nd_blockmask->n;
5123: op = nd_blockmask->order_pair;
5124: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5125: ord_l = op[j].length;
5126: for ( i = 0; i < ord_l; i++, s++ )
5127: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5128: }
5129: } else {
5130: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5131: }
5132: printf(">>");
5133: if ( nd_module && MPOS(dl) )
5134: printf("*e%d",MPOS(dl));
5135: }
5136:
5137: void nd_print(ND p)
5138: {
5139: NM m;
5140:
5141: if ( !p )
5142: printf("0\n");
5143: else {
5144: for ( m = BDY(p); m; m = NEXT(m) ) {
5145: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5146: else printf("+%ld*",CM(m));
1.1 noro 5147: ndl_print(DL(m));
5148: }
5149: printf("\n");
5150: }
5151: }
5152:
5153: void nd_print_q(ND p)
5154: {
5155: NM m;
5156:
5157: if ( !p )
5158: printf("0\n");
5159: else {
5160: for ( m = BDY(p); m; m = NEXT(m) ) {
5161: printf("+");
1.6 noro 5162: printexpr(CO,(Obj)CZ(m));
1.1 noro 5163: printf("*");
5164: ndl_print(DL(m));
5165: }
5166: printf("\n");
5167: }
5168: }
5169:
5170: void ndp_print(ND_pairs d)
5171: {
5172: ND_pairs t;
5173:
5174: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5175: printf("\n");
5176: }
5177:
5178: void nd_removecont(int mod,ND p)
5179: {
5180: int i,n;
5181: Z *w;
5182: NM m;
5183: struct oVECT v;
5184:
5185: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5186: else if ( mod == -2 ) {
5187: Z inv;
5188: divlf(ONE,HCZ(p),&inv);
5189: nd_mul_c_lf(p,inv);
5190: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5191: else {
5192: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5193: w = (Z *)MALLOC(n*sizeof(Q));
5194: v.len = n;
5195: v.body = (pointer *)w;
1.6 noro 5196: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5197: removecont_array((P *)w,n,1);
1.6 noro 5198: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5199: }
5200: }
5201:
5202: void nd_removecont2(ND p1,ND p2)
5203: {
5204: int i,n1,n2,n;
5205: Z *w;
5206: NM m;
5207: struct oVECT v;
5208:
5209: n1 = nd_length(p1);
5210: n2 = nd_length(p2);
5211: n = n1+n2;
5212: w = (Z *)MALLOC(n*sizeof(Q));
5213: v.len = n;
5214: v.body = (pointer *)w;
5215: i = 0;
5216: if ( p1 )
1.6 noro 5217: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5218: if ( p2 )
1.6 noro 5219: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5220: removecont_array((P *)w,n,1);
5221: i = 0;
5222: if ( p1 )
1.6 noro 5223: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5224: if ( p2 )
1.6 noro 5225: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5226: }
5227:
5228: void ndv_removecont(int mod,NDV p)
5229: {
5230: int i,len,all_p;
5231: Z *c;
5232: P *w;
5233: Z dvr,t;
5234: P g,cont,tp;
5235: NMV m;
5236:
5237: if ( mod == -1 )
5238: ndv_mul_c(mod,p,_invsf(HCM(p)));
5239: else if ( mod == -2 ) {
5240: Z inv;
5241: divlf(ONE,HCZ(p),&inv);
5242: ndv_mul_c_lf(p,inv);
5243: } else if ( mod )
5244: ndv_mul_c(mod,p,invm(HCM(p),mod));
5245: else {
5246: len = p->len;
5247: w = (P *)MALLOC(len*sizeof(P));
5248: c = (Z *)MALLOC(len*sizeof(Q));
5249: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5250: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5251: all_p = all_p && !NUM(w[i]);
5252: }
5253: if ( all_p ) {
5254: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5255: mulp(nd_vc,(P)dvr,g,&cont);
5256: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5257: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5258: }
5259: } else {
5260: sortbynm((Q *)c,len);
5261: qltozl((Q *)c,len,&dvr);
5262: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5263: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5264: }
5265: }
5266: }
5267: }
5268:
5269: /* koko */
5270:
5271: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5272: {
5273: int len,i,max;
5274: NMV m,mr0,mr,t;
5275:
5276: len = p->len;
1.14 noro 5277: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5278: max = MAX(max,TD(DL(m)));
5279: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5280: m = (NMV)((char *)mr0+(len-1)*oadv);
5281: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5282: t = (NMV)MALLOC(nmv_adv);
5283: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5284: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5285: CZ(mr) = CZ(m);
1.1 noro 5286: ndl_copy(DL(t),DL(mr));
5287: }
5288: NV(p)++;
5289: BDY(p) = mr0;
5290: }
5291:
5292: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5293: {
5294: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5295: int pos;
5296: Q *w;
5297: Q dvr,t;
5298: NMV m,r;
5299:
5300: len = p->len;
5301: newnvar = nd_nvar-1;
5302: newexporigin = nd_get_exporigin(ord);
5303: if ( nd_module ) newmpos = newexporigin-1;
5304: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5305: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5306: ndl_dehomogenize(DL(m));
5307: if ( newwpd != nd_wpd ) {
5308: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5309: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5310: CZ(r) = CZ(m);
1.1 noro 5311: if ( nd_module ) pos = MPOS(DL(m));
5312: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5313: adj = nd_exporigin-newexporigin;
5314: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5315: if ( nd_module ) {
5316: DL(r)[newmpos] = pos;
5317: }
5318: }
5319: }
5320: NV(p)--;
5321: }
5322:
5323: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5324: {
5325: int i;
5326: P *tpl,*tpl1;
5327: NODE l;
5328: P h,gcd,t;
5329:
5330: tpl = (P *)MALLOC(m*sizeof(P));
5331: tpl1 = (P *)MALLOC(m*sizeof(P));
5332: bcopy(pl,tpl,m*sizeof(P));
5333: gcd = (P)ONE;
5334: for ( l = nd_hcf; l; l = NEXT(l) ) {
5335: h = (P)BDY(l);
5336: while ( 1 ) {
5337: for ( i = 0; i < m; i++ )
5338: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5339: break;
5340: if ( i == m ) {
5341: bcopy(tpl1,tpl,m*sizeof(P));
5342: mulp(vl,gcd,h,&t); gcd = t;
5343: } else
5344: break;
5345: }
5346: }
5347: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5348: if ( full ) {
5349: heu_nezgcdnpz(vl,tpl,m,&t);
5350: mulp(vl,gcd,t,pr);
5351: } else
5352: *pr = gcd;
5353: }
5354:
5355: void removecont_array(P *p,int n,int full)
5356: {
5357: int all_p,all_q,i;
5358: Z *c;
5359: P *w;
5360: P t,s;
5361:
5362: for ( all_q = 1, i = 0; i < n; i++ )
5363: all_q = all_q && NUM(p[i]);
5364: if ( all_q ) {
5365: removecont_array_q((Z *)p,n);
5366: } else {
5367: c = (Z *)MALLOC(n*sizeof(Z));
5368: w = (P *)MALLOC(n*sizeof(P));
5369: for ( i = 0; i < n; i++ ) {
5370: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5371: }
5372: removecont_array_q(c,n);
5373: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5374: for ( i = 0; i < n; i++ ) {
5375: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5376: }
5377: }
5378: }
5379:
5380: /* c is an int array */
5381:
5382: void removecont_array_q(Z *c,int n)
5383: {
5384: struct oVECT v;
5385: Z d0,d1,a,u,u1,gcd;
5386: int i,j;
5387: Z *q,*r;
5388:
5389: q = (Z *)MALLOC(n*sizeof(Z));
5390: r = (Z *)MALLOC(n*sizeof(Z));
5391: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5392: gcdvz_estimate(&v,&d0);
5393: for ( i = 0; i < n; i++ ) {
5394: divqrz(c[i],d0,&q[i],&r[i]);
5395: }
5396: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5397: if ( i < n ) {
5398: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5399: gcdvz(&v,&d1);
5400: gcdz(d0,d1,&gcd);
1.6 noro 5401: /* exact division */
5402: divsz(d0,gcd,&a);
1.1 noro 5403: for ( i = 0; i < n; i++ ) {
5404: mulz(a,q[i],&u);
5405: if ( r[i] ) {
1.6 noro 5406: /* exact division */
5407: divsz(r[i],gcd,&u1);
1.1 noro 5408: addz(u,u1,&q[i]);
5409: } else
5410: q[i] = u;
5411: }
5412: }
5413: for ( i = 0; i < n; i++ ) c[i] = q[i];
5414: }
5415:
1.4 noro 5416: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5417:
5418: void mpz_removecont_array(mpz_t *c,int n)
5419: {
5420: mpz_t d0,a,u,u1,gcd;
5421: int i,j;
1.13 noro 5422: static mpz_t *q,*r;
5423: static int c_len = 0;
1.4 noro 5424:
5425: for ( i = 0; i < n; i++ )
5426: if ( mpz_sgn(c[i]) ) break;
5427: if ( i == n ) return;
5428: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5429: if ( n > c_len ) {
5430: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5431: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5432: c_len = n;
5433: }
1.4 noro 5434: for ( i = 0; i < n; i++ ) {
5435: mpz_init(q[i]); mpz_init(r[i]);
5436: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5437: }
5438: for ( i = 0; i < n; i++ )
5439: if ( mpz_sgn(r[i]) ) break;
5440: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5441: if ( i < n ) {
5442: mpz_gcd(gcd,d0,r[i]);
5443: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5444: mpz_div(a,d0,gcd);
5445: for ( i = 0; i < n; i++ ) {
5446: mpz_mul(u,a,q[i]);
5447: if ( mpz_sgn(r[i]) ) {
5448: mpz_div(u1,r[i],gcd);
5449: mpz_add(q[i],u,u1);
5450: } else
5451: mpz_set(q[i],u);
5452: }
5453: }
5454: for ( i = 0; i < n; i++ )
5455: mpz_set(c[i],q[i]);
5456: }
5457:
1.1 noro 5458: void nd_mul_c(int mod,ND p,int mul)
5459: {
5460: NM m;
5461: int c,c1;
5462:
5463: if ( !p ) return;
5464: if ( mul == 1 ) return;
5465: if ( mod == -1 )
5466: for ( m = BDY(p); m; m = NEXT(m) )
5467: CM(m) = _mulsf(CM(m),mul);
5468: else
5469: for ( m = BDY(p); m; m = NEXT(m) ) {
5470: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5471: }
5472: }
5473:
5474: void nd_mul_c_lf(ND p,Z mul)
5475: {
5476: NM m;
5477: Z c;
5478:
5479: if ( !p ) return;
5480: if ( UNIZ(mul) ) return;
5481: for ( m = BDY(p); m; m = NEXT(m) ) {
5482: mullf(CZ(m),mul,&c); CZ(m) = c;
5483: }
5484: }
5485:
5486: void nd_mul_c_q(ND p,P mul)
5487: {
5488: NM m;
5489: P c;
5490:
5491: if ( !p ) return;
5492: if ( UNIQ(mul) ) return;
5493: for ( m = BDY(p); m; m = NEXT(m) ) {
5494: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5495: }
5496: }
5497:
5498: void nd_mul_c_p(VL vl,ND p,P mul)
5499: {
5500: NM m;
5501: P c;
5502:
5503: if ( !p ) return;
5504: for ( m = BDY(p); m; m = NEXT(m) ) {
5505: mulp(vl,CP(m),mul,&c); CP(m) = c;
5506: }
5507: }
5508:
5509: void nd_free(ND p)
5510: {
5511: NM t,s;
5512:
5513: if ( !p ) return;
5514: t = BDY(p);
5515: while ( t ) {
5516: s = NEXT(t);
5517: FREENM(t);
5518: t = s;
5519: }
5520: FREEND(p);
5521: }
5522:
5523: void ndv_free(NDV p)
5524: {
5525: GCFREE(BDY(p));
5526: }
5527:
5528: void nd_append_red(UINT *d,int i)
5529: {
5530: RHist m,m0;
5531: int h;
5532:
5533: NEWRHist(m);
5534: h = ndl_hash_value(d);
5535: m->index = i;
5536: ndl_copy(d,DL(m));
5537: NEXT(m) = nd_red[h];
5538: nd_red[h] = m;
5539: }
5540:
5541: UINT *ndv_compute_bound(NDV p)
5542: {
5543: UINT *d1,*d2,*t;
5544: UINT u;
5545: int i,j,k,l,len,ind;
5546: NMV m;
5547:
5548: if ( !p )
5549: return 0;
5550: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5551: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5552: len = LEN(p);
5553: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5554: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5555: ndl_max(DL(m),d1,d2);
5556: t = d1; d1 = d2; d2 = t;
5557: }
5558: l = nd_nvar+31;
5559: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5560: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5561: u = d1[i];
5562: k = (nd_epw-1)*nd_bpe;
5563: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5564: t[ind] = (u>>k)&nd_mask0;
5565: }
5566: for ( ; ind < l; ind++ ) t[ind] = 0;
5567: return t;
5568: }
5569:
5570: UINT *nd_compute_bound(ND p)
5571: {
5572: UINT *d1,*d2,*t;
5573: UINT u;
5574: int i,j,k,l,len,ind;
5575: NM m;
5576:
5577: if ( !p )
5578: return 0;
5579: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5580: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5581: len = LEN(p);
5582: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5583: for ( m = NEXT(m); m; m = NEXT(m) ) {
5584: ndl_lcm(DL(m),d1,d2);
5585: t = d1; d1 = d2; d2 = t;
5586: }
5587: l = nd_nvar+31;
5588: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5589: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5590: u = d1[i];
5591: k = (nd_epw-1)*nd_bpe;
5592: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5593: t[ind] = (u>>k)&nd_mask0;
5594: }
5595: for ( ; ind < l; ind++ ) t[ind] = 0;
5596: return t;
5597: }
5598:
5599: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5600: /* of a term. In this case we need additional 1 word. */
5601:
5602: int nd_get_exporigin(struct order_spec *ord)
5603: {
5604: switch ( ord->id ) {
1.21 noro 5605: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5606: return 1+nd_module;
5607: case 1: case 257:
5608: /* block order */
5609: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5610: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5611: return ord->ord.block.length+1+nd_module;
5612: case 3: case 259:
5613: #if 0
5614: error("nd_get_exporigin : composite order is not supported yet.");
5615: #else
5616: return 1+nd_module;
5617: #endif
5618: default:
5619: error("nd_get_exporigin : ivalid argument.");
5620: return 0;
5621: }
5622: }
5623:
5624: void nd_setup_parameters(int nvar,int max) {
5625: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5626: struct order_pair *op;
5627:
5628: nd_nvar = nvar;
5629: if ( max ) {
5630: /* XXX */
5631: if ( do_weyl ) nd_bpe = 32;
5632: else if ( max < 2 ) nd_bpe = 1;
5633: else if ( max < 4 ) nd_bpe = 2;
5634: else if ( max < 8 ) nd_bpe = 3;
5635: else if ( max < 16 ) nd_bpe = 4;
5636: else if ( max < 32 ) nd_bpe = 5;
5637: else if ( max < 64 ) nd_bpe = 6;
5638: else if ( max < 256 ) nd_bpe = 8;
5639: else if ( max < 1024 ) nd_bpe = 10;
5640: else if ( max < 65536 ) nd_bpe = 16;
5641: else nd_bpe = 32;
5642: }
5643: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5644: UINT t;
5645: int st;
5646: int *v;
5647: /* t = max(weights) */
5648: t = 0;
5649: if ( current_dl_weight_vector )
5650: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5651: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5652: if ( t < st ) t = st;
5653: }
5654: if ( nd_matrix )
5655: for ( i = 0; i < nd_matrix_len; i++ )
5656: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5657: if ( (st=v[j]) < 0 ) st = -st;
5658: if ( t < st ) t = st;
5659: }
5660: /* i = bitsize of t */
5661: for ( i = 0; t; t >>=1, i++ );
5662: /* i += bitsize of nd_nvar */
5663: for ( t = nd_nvar; t; t >>=1, i++);
5664: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5665: if ( (nd_bpe+i) >= 31 )
5666: error("nd_setup_parameters : too large weight");
5667: }
5668: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5669: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5670: nd_exporigin = nd_get_exporigin(nd_ord);
5671: wpd = nd_exporigin+elen;
5672: if ( nd_module )
5673: nd_mpos = nd_exporigin-1;
5674: else
5675: nd_mpos = -1;
5676: if ( wpd != nd_wpd ) {
5677: nd_free_private_storage();
5678: nd_wpd = wpd;
5679: }
5680: if ( nd_bpe < 32 ) {
5681: nd_mask0 = (1<<nd_bpe)-1;
5682: } else {
5683: nd_mask0 = 0xffffffff;
5684: }
5685: bzero(nd_mask,sizeof(nd_mask));
5686: nd_mask1 = 0;
5687: for ( i = 0; i < nd_epw; i++ ) {
5688: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5689: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5690: }
5691: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5692: nd_epos = nd_create_epos(nd_ord);
5693: nd_blockmask = nd_create_blockmask(nd_ord);
5694: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5695: }
5696:
5697: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5698: {
5699: int i,obpe,oadv,h;
5700: static NM prev_nm_free_list;
5701: static ND_pairs prev_ndp_free_list;
5702: RHist mr0,mr;
5703: RHist r;
5704: RHist *old_red;
5705: ND_pairs s0,s,t;
5706: EPOS oepos;
5707:
5708: obpe = nd_bpe;
5709: oadv = nmv_adv;
5710: oepos = nd_epos;
5711: if ( obpe < 2 ) nd_bpe = 2;
5712: else if ( obpe < 3 ) nd_bpe = 3;
5713: else if ( obpe < 4 ) nd_bpe = 4;
5714: else if ( obpe < 5 ) nd_bpe = 5;
5715: else if ( obpe < 6 ) nd_bpe = 6;
5716: else if ( obpe < 8 ) nd_bpe = 8;
5717: else if ( obpe < 10 ) nd_bpe = 10;
5718: else if ( obpe < 16 ) nd_bpe = 16;
5719: else if ( obpe < 32 ) nd_bpe = 32;
5720: else error("nd_reconstruct : exponent too large");
5721:
5722: nd_setup_parameters(nd_nvar,0);
5723: prev_nm_free_list = _nm_free_list;
5724: prev_ndp_free_list = _ndp_free_list;
5725: _nm_free_list = 0;
5726: _ndp_free_list = 0;
5727: for ( i = nd_psn-1; i >= 0; i-- ) {
5728: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5729: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5730: }
5731: if ( trace )
5732: for ( i = nd_psn-1; i >= 0; i-- ) {
5733: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5734: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5735: }
5736: s0 = 0;
5737: for ( t = d; t; t = NEXT(t) ) {
5738: NEXTND_pairs(s0,s);
5739: s->i1 = t->i1;
5740: s->i2 = t->i2;
1.24 noro 5741: s->sig = t->sig;
1.1 noro 5742: SG(s) = SG(t);
5743: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5744: }
5745:
5746: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5747: for ( i = 0; i < REDTAB_LEN; i++ ) {
5748: old_red[i] = nd_red[i];
5749: nd_red[i] = 0;
5750: }
5751: for ( i = 0; i < REDTAB_LEN; i++ )
5752: for ( r = old_red[i]; r; r = NEXT(r) ) {
5753: NEWRHist(mr);
5754: mr->index = r->index;
5755: SG(mr) = SG(r);
5756: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5757: h = ndl_hash_value(DL(mr));
5758: NEXT(mr) = nd_red[h];
5759: nd_red[h] = mr;
1.24 noro 5760: mr->sig = r->sig;
1.1 noro 5761: }
5762: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5763: old_red = 0;
5764: for ( i = 0; i < nd_psn; i++ ) {
5765: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5766: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5767: r->sig = nd_psh[i]->sig;
1.1 noro 5768: nd_psh[i] = r;
5769: }
5770: if ( s0 ) NEXT(s) = 0;
5771: prev_nm_free_list = 0;
5772: prev_ndp_free_list = 0;
5773: #if 0
5774: GC_gcollect();
5775: #endif
5776: return s0;
5777: }
5778:
1.30 noro 5779: void nd_reconstruct_s(int trace,ND_pairs *d)
5780: {
5781: int i,obpe,oadv,h;
5782: static NM prev_nm_free_list;
5783: static ND_pairs prev_ndp_free_list;
5784: RHist mr0,mr;
5785: RHist r;
5786: RHist *old_red;
5787: ND_pairs s0,s,t;
5788: EPOS oepos;
5789:
5790: obpe = nd_bpe;
5791: oadv = nmv_adv;
5792: oepos = nd_epos;
5793: if ( obpe < 2 ) nd_bpe = 2;
5794: else if ( obpe < 3 ) nd_bpe = 3;
5795: else if ( obpe < 4 ) nd_bpe = 4;
5796: else if ( obpe < 5 ) nd_bpe = 5;
5797: else if ( obpe < 6 ) nd_bpe = 6;
5798: else if ( obpe < 8 ) nd_bpe = 8;
5799: else if ( obpe < 10 ) nd_bpe = 10;
5800: else if ( obpe < 16 ) nd_bpe = 16;
5801: else if ( obpe < 32 ) nd_bpe = 32;
5802: else error("nd_reconstruct_s : exponent too large");
5803:
5804: nd_setup_parameters(nd_nvar,0);
5805: prev_nm_free_list = _nm_free_list;
5806: prev_ndp_free_list = _ndp_free_list;
5807: _nm_free_list = 0;
5808: _ndp_free_list = 0;
5809: for ( i = nd_psn-1; i >= 0; i-- ) {
5810: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5811: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5812: }
5813: if ( trace )
5814: for ( i = nd_psn-1; i >= 0; i-- ) {
5815: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5816: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5817: }
5818:
5819: for ( i = 0; i < nd_nbase; i++ ) {
5820: s0 = 0;
5821: for ( t = d[i]; t; t = NEXT(t) ) {
5822: NEXTND_pairs(s0,s);
5823: s->i1 = t->i1;
5824: s->i2 = t->i2;
5825: s->sig = t->sig;
5826: SG(s) = SG(t);
5827: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5828: }
5829: d[i] = s0;
5830: }
5831:
5832: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5833: for ( i = 0; i < REDTAB_LEN; i++ ) {
5834: old_red[i] = nd_red[i];
5835: nd_red[i] = 0;
5836: }
5837: for ( i = 0; i < REDTAB_LEN; i++ )
5838: for ( r = old_red[i]; r; r = NEXT(r) ) {
5839: NEWRHist(mr);
5840: mr->index = r->index;
5841: SG(mr) = SG(r);
5842: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5843: h = ndl_hash_value(DL(mr));
5844: NEXT(mr) = nd_red[h];
5845: nd_red[h] = mr;
5846: mr->sig = r->sig;
5847: }
5848: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5849: old_red = 0;
5850: for ( i = 0; i < nd_psn; i++ ) {
5851: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5852: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
5853: r->sig = nd_psh[i]->sig;
5854: nd_psh[i] = r;
5855: }
5856: if ( s0 ) NEXT(s) = 0;
5857: prev_nm_free_list = 0;
5858: prev_ndp_free_list = 0;
5859: #if 0
5860: GC_gcollect();
5861: #endif
5862: }
5863:
1.1 noro 5864: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5865: {
5866: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5867: struct order_pair *op;
5868:
5869: n = nd_nvar;
5870: oepw = (sizeof(UINT)*8)/obpe;
5871: omask0 = (1<<obpe)-1;
5872: TD(r) = TD(d);
5873: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5874: if ( nd_blockmask ) {
5875: l = nd_blockmask->n;
5876: op = nd_blockmask->order_pair;
5877: for ( i = 1; i < nd_exporigin; i++ )
5878: r[i] = d[i];
5879: for ( j = 0, s = 0; j < l; j++ ) {
5880: ord_l = op[j].length;
5881: for ( i = 0; i < ord_l; i++, s++ ) {
5882: ei = GET_EXP_OLD(d,s);
5883: PUT_EXP(r,s,ei);
5884: }
5885: }
5886: } else {
5887: for ( i = 0; i < n; i++ ) {
5888: ei = GET_EXP_OLD(d,i);
5889: PUT_EXP(r,i,ei);
5890: }
5891: }
5892: if ( nd_module ) MPOS(r) = MPOS(d);
5893: }
5894:
5895: ND nd_copy(ND p)
5896: {
5897: NM m,mr,mr0;
5898: int c,n;
5899: ND r;
5900:
5901: if ( !p )
5902: return 0;
5903: else {
5904: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5905: NEXTNM(mr0,mr);
5906: CM(mr) = CM(m);
5907: ndl_copy(DL(m),DL(mr));
5908: }
5909: NEXT(mr) = 0;
5910: MKND(NV(p),mr0,LEN(p),r);
5911: SG(r) = SG(p);
5912: return r;
5913: }
5914: }
5915:
5916: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5917: {
5918: NM m1,m2;
5919: NDV p1,p2;
5920: ND t1,t2;
5921: UINT *lcm;
5922: P gp,tp;
5923: Z g,t;
5924: Z iq;
5925: int td;
5926: LIST hist;
5927: NODE node;
5928: DP d;
5929:
5930: if ( !mod && nd_demand ) {
5931: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5932: } else {
5933: if ( trace ) {
5934: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5935: } else {
5936: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5937: }
5938: }
5939: lcm = LCM(p);
5940: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5941: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5942: FREENM(m1); return 0;
5943: }
5944: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5945: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5946: FREENM(m1); FREENM(m2); return 0;
5947: }
5948:
5949: if ( mod == -1 ) {
5950: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5951: } else if ( mod > 0 ) {
5952: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5953: } else if ( mod == -2 ) {
5954: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5955: } else if ( nd_vc ) {
5956: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5957: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5958: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5959: } else {
1.6 noro 5960: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5961: }
5962: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5963: *rp = nd_add(mod,t1,t2);
5964: if ( nd_gentrace ) {
5965: /* nd_tracelist is initialized */
1.6 noro 5966: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5967: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5968: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5969: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5970: nd_tracelist = node;
5971: }
1.24 noro 5972: if ( *rp )
5973: (*rp)->sig = p->sig;
1.1 noro 5974: FREENM(m1); FREENM(m2);
5975: return 1;
5976: }
5977:
5978: void ndv_mul_c(int mod,NDV p,int mul)
5979: {
5980: NMV m;
5981: int c,c1,len,i;
5982:
5983: if ( !p ) return;
5984: len = LEN(p);
5985: if ( mod == -1 )
5986: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5987: CM(m) = _mulsf(CM(m),mul);
5988: else
5989: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5990: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5991: }
5992: }
5993:
5994: void ndv_mul_c_lf(NDV p,Z mul)
5995: {
5996: NMV m;
5997: Z c;
5998: int len,i;
5999:
6000: if ( !p ) return;
6001: len = LEN(p);
6002: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6003: mullf(CZ(m),mul,&c); CZ(m) = c;
6004: }
6005: }
6006:
6007: /* for nd_det */
6008: void ndv_mul_c_q(NDV p,Z mul)
6009: {
6010: NMV m;
6011: Z c;
6012: int len,i;
6013:
6014: if ( !p ) return;
6015: len = LEN(p);
6016: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6017: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6018: }
6019: }
6020:
6021: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6022: int n2,i,j,l,n,tlen;
6023: UINT *d0;
6024: NM *tab,*psum;
6025: ND s,r;
6026: NM t;
6027: NMV m1;
6028:
6029: if ( !p ) return 0;
6030: n = NV(p); n2 = n>>1;
6031: d0 = DL(m0);
6032: l = LEN(p);
6033: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6034: tab = (NM *)MALLOC(tlen*sizeof(NM));
6035: psum = (NM *)MALLOC(tlen*sizeof(NM));
6036: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6037: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6038: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6039: /* m0(NM) * m1(NMV) => tab(NM) */
6040: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6041: for ( j = 0; j < tlen; j++ ) {
6042: if ( tab[j] ) {
6043: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6044: }
6045: }
6046: }
6047: for ( i = tlen-1, r = 0; i >= 0; i-- )
6048: if ( psum[i] ) {
6049: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6050: MKND(n,psum[i],j,s);
6051: r = nd_add(mod,r,s);
6052: }
6053: if ( r ) SG(r) = SG(p)+TD(d0);
6054: return r;
6055: }
6056:
6057: /* product of monomials */
6058: /* XXX block order is not handled correctly */
6059:
6060: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6061: {
6062: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6063: UINT *d0,*d1,*d,*dt,*ctab;
6064: Z *ctab_q;
6065: Z q,q1;
6066: UINT c0,c1,c;
6067: NM *p;
6068: NM m,t;
6069: int mpos;
6070:
6071: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6072: if ( !m0 || !m1 ) return;
6073: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6074: if ( nd_module )
6075: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6076:
6077: NEWNM(m); d = DL(m);
6078: if ( mod ) {
6079: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6080: } else if ( nd_vc )
6081: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6082: else
1.6 noro 6083: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6084: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6085: homo = n&1 ? 1 : 0;
6086: if ( homo ) {
6087: /* offset of h-degree */
6088: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6089: PUT_EXP(DL(m),n-1,h);
6090: TD(DL(m)) = h;
6091: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6092: }
6093: tab[0] = m;
6094: NEWNM(m); d = DL(m);
6095: for ( i = 0, curlen = 1; i < n2; i++ ) {
6096: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6097: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6098: /* xi^a*(Di^k*xi^l)*Di^b */
6099: a += l; b += k;
6100: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6101: if ( !k || !l ) {
6102: for ( j = 0; j < curlen; j++ )
6103: if ( (t = tab[j]) != 0 ) {
6104: dt = DL(t);
6105: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6106: if ( nd_blockmask ) ndl_weight_mask(dt);
6107: }
6108: curlen *= k+1;
6109: continue;
6110: }
6111: min = MIN(k,l);
6112: if ( mod ) {
6113: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6114: mkwcm(k,l,mod,(int *)ctab);
6115: } else {
6116: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6117: mkwc(k,l,ctab_q);
6118: }
6119: for ( j = min; j >= 0; j-- ) {
6120: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6121: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6122: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6123: if ( homo ) {
6124: TD(d) = s;
6125: PUT_EXP(d,n-1,s-h);
6126: } else TD(d) = h;
6127: if ( nd_blockmask ) ndl_weight_mask(d);
6128: if ( mod ) c = ctab[j];
6129: else q = ctab_q[j];
6130: p = tab+curlen*j;
6131: if ( j == 0 ) {
6132: for ( u = 0; u < curlen; u++, p++ ) {
6133: if ( tab[u] ) {
6134: ndl_addto(DL(tab[u]),d);
6135: if ( mod ) {
6136: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6137: } else if ( nd_vc )
6138: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6139: else {
1.6 noro 6140: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6141: }
6142: }
6143: }
6144: } else {
6145: for ( u = 0; u < curlen; u++, p++ ) {
6146: if ( tab[u] ) {
6147: NEWNM(t);
6148: ndl_add(DL(tab[u]),d,DL(t));
6149: if ( mod ) {
6150: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6151: } else if ( nd_vc )
6152: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6153: else
1.6 noro 6154: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6155: *p = t;
6156: }
6157: }
6158: }
6159: }
6160: curlen *= k+1;
6161: }
6162: FREENM(m);
6163: if ( nd_module ) {
6164: mpos = MPOS(d1);
6165: for ( i = 0; i < tlen; i++ )
6166: if ( tab[i] ) {
6167: d = DL(tab[i]);
6168: MPOS(d) = mpos;
6169: TD(d) = ndl_weight(d);
6170: }
6171: }
6172: }
6173:
6174: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6175: {
6176: NM mr,mr0;
6177: NMV m;
6178: UINT *d,*dt,*dm;
6179: int c,n,td,i,c1,c2,len;
6180: Q q;
6181: ND r;
6182:
6183: if ( !p ) return 0;
6184: else {
6185: n = NV(p); m = BDY(p);
6186: d = DL(m0);
6187: len = LEN(p);
6188: mr0 = 0;
6189: td = TD(d);
6190: c = CM(m0);
6191: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6192: NEXTNM(mr0,mr);
6193: CM(mr) = 1;
6194: ndl_add(DL(m),d,DL(mr));
6195: }
6196: NEXT(mr) = 0;
6197: MKND(NV(p),mr0,len,r);
6198: SG(r) = SG(p) + TD(d);
6199: return r;
6200: }
6201: }
6202:
6203: ND ndv_mul_nm(int mod,NM m0,NDV p)
6204: {
6205: NM mr,mr0;
6206: NMV m;
6207: UINT *d,*dt,*dm;
6208: int c,n,td,i,c1,c2,len;
6209: P q;
6210: ND r;
6211:
6212: if ( !p ) return 0;
6213: else if ( do_weyl ) {
6214: if ( mod < 0 ) {
6215: error("ndv_mul_nm : not implemented (weyl)");
6216: return 0;
6217: } else
6218: return weyl_ndv_mul_nm(mod,m0,p);
6219: } else {
6220: n = NV(p); m = BDY(p);
6221: d = DL(m0);
6222: len = LEN(p);
6223: mr0 = 0;
6224: td = TD(d);
6225: if ( mod == -1 ) {
6226: c = CM(m0);
6227: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6228: NEXTNM(mr0,mr);
6229: CM(mr) = _mulsf(CM(m),c);
6230: ndl_add(DL(m),d,DL(mr));
6231: }
6232: } else if ( mod == -2 ) {
6233: Z cl;
6234: cl = CZ(m0);
6235: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6236: NEXTNM(mr0,mr);
6237: mullf(CZ(m),cl,&CZ(mr));
6238: ndl_add(DL(m),d,DL(mr));
6239: }
6240: } else if ( mod ) {
6241: c = CM(m0);
6242: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6243: NEXTNM(mr0,mr);
6244: c1 = CM(m);
6245: DMAR(c1,c,0,mod,c2);
6246: CM(mr) = c2;
6247: ndl_add(DL(m),d,DL(mr));
6248: }
6249: } else {
6250: q = CP(m0);
6251: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6252: NEXTNM(mr0,mr);
6253: mulp(nd_vc,CP(m),q,&CP(mr));
6254: ndl_add(DL(m),d,DL(mr));
6255: }
6256: }
6257: NEXT(mr) = 0;
6258: MKND(NV(p),mr0,len,r);
6259: SG(r) = SG(p) + TD(d);
6260: return r;
6261: }
6262: }
6263:
6264: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6265: {
6266: NM mq0,mq;
6267: NMV tm;
6268: Q q;
6269: int i,nv,sg,c,c1,c2,hindex;
6270: ND p,t,r;
6271:
6272: if ( bucket->m < 0 ) return 0;
6273: else {
6274: nv = NV(d);
6275: mq0 = 0;
6276: tm = (NMV)MALLOC(nmv_adv);
6277: while ( 1 ) {
6278: if ( mod > 0 || mod == -1 )
6279: hindex = head_pbucket(mod,bucket);
6280: else if ( mod == -2 )
6281: hindex = head_pbucket_lf(bucket);
6282: else
6283: hindex = head_pbucket_q(bucket);
6284: if ( hindex < 0 ) break;
6285: p = bucket->body[hindex];
6286: NEXTNM(mq0,mq);
6287: ndl_sub(HDL(p),HDL(d),DL(mq));
6288: ndl_copy(DL(mq),DL(tm));
6289: if ( mod ) {
6290: c1 = invm(HCM(d),mod); c2 = HCM(p);
6291: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6292: CM(tm) = mod-c;
6293: } else {
1.6 noro 6294: divsz(HCZ(p),HCZ(d),&CZ(mq));
6295: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6296: }
6297: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6298: bucket->body[hindex] = nd_remove_head(p);
6299: t = nd_remove_head(t);
6300: add_pbucket(mod,bucket,t);
6301: }
6302: if ( !mq0 )
6303: r = 0;
6304: else {
6305: NEXT(mq) = 0;
6306: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6307: MKND(nv,mq0,i,r);
6308: /* XXX */
6309: SG(r) = HTD(r);
6310: }
6311: return r;
6312: }
6313: }
6314:
6315: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6316: {
6317: NMV m,mr,mr0,t;
6318: int len,i,k;
6319:
6320: if ( !p ) return;
6321: m = BDY(p); len = LEN(p);
6322: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6323: m = (NMV)((char *)mr0+(len-1)*oadv);
6324: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6325: t = (NMV)MALLOC(nmv_adv);
6326: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6327: CZ(t) = CZ(m);
1.1 noro 6328: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6329: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6330: CZ(mr) = CZ(t);
1.1 noro 6331: ndl_copy(DL(t),DL(mr));
6332: }
6333: BDY(p) = mr0;
6334: }
6335:
6336: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6337: {
6338: NMV m,mr,mr0;
6339: int len,i;
6340: NDV r;
6341:
6342: if ( !p ) return 0;
6343: m = BDY(p); len = LEN(p);
6344: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6345: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6346: ndl_zero(DL(mr));
6347: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6348: CZ(mr) = CZ(m);
1.1 noro 6349: }
6350: MKNDV(NV(p),mr0,len,r);
6351: SG(r) = SG(p);
1.24 noro 6352: r->sig = p->sig;
1.1 noro 6353: return r;
6354: }
6355:
6356: /* duplicate p */
6357:
6358: NDV ndv_dup(int mod,NDV p)
6359: {
6360: NDV d;
6361: NMV t,m,m0;
6362: int i,len;
6363:
6364: if ( !p ) return 0;
6365: len = LEN(p);
6366: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6367: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6368: ndl_copy(DL(t),DL(m));
1.6 noro 6369: CZ(m) = CZ(t);
1.1 noro 6370: }
6371: MKNDV(NV(p),m0,len,d);
6372: SG(d) = SG(p);
6373: return d;
6374: }
6375:
6376: NDV ndv_symbolic(int mod,NDV p)
6377: {
6378: NDV d;
6379: NMV t,m,m0;
6380: int i,len;
6381:
6382: if ( !p ) return 0;
6383: len = LEN(p);
6384: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6385: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6386: ndl_copy(DL(t),DL(m));
1.6 noro 6387: CZ(m) = ONE;
1.1 noro 6388: }
6389: MKNDV(NV(p),m0,len,d);
6390: SG(d) = SG(p);
6391: return d;
6392: }
6393:
6394: ND nd_dup(ND p)
6395: {
6396: ND d;
6397: NM t,m,m0;
6398:
6399: if ( !p ) return 0;
6400: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6401: NEXTNM(m0,m);
6402: ndl_copy(DL(t),DL(m));
1.6 noro 6403: CZ(m) = CZ(t);
1.1 noro 6404: }
6405: if ( m0 ) NEXT(m) = 0;
6406: MKND(NV(p),m0,LEN(p),d);
6407: SG(d) = SG(p);
6408: return d;
6409: }
6410:
6411: /* XXX if p->len == 0 then it represents 0 */
6412:
6413: void ndv_mod(int mod,NDV p)
6414: {
6415: NMV t,d;
6416: int r,s,u;
6417: int i,len,dlen;
6418: P cp;
6419: Z c;
6420: Obj gfs;
6421:
6422: if ( !p ) return;
6423: len = LEN(p);
6424: dlen = 0;
6425: if ( mod == -1 )
6426: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6427: simp_ff((Obj)CP(t),&gfs);
6428: if ( gfs ) {
6429: r = FTOIF(CONT((GFS)gfs));
6430: CM(d) = r;
6431: ndl_copy(DL(t),DL(d));
6432: NMV_ADV(d);
6433: dlen++;
6434: }
6435: }
6436: else if ( mod == -2 )
6437: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6438: simp_ff((Obj)CP(t),&gfs);
6439: if ( gfs ) {
6440: lmtolf((LM)gfs,&CZ(d));
6441: ndl_copy(DL(t),DL(d));
6442: NMV_ADV(d);
6443: dlen++;
6444: }
6445: }
6446: else
6447: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6448: if ( nd_vc ) {
6449: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6450: c = (Z)cp;
6451: } else
1.6 noro 6452: c = CZ(t);
1.1 noro 6453: r = remqi((Q)c,mod);
6454: if ( r ) {
6455: CM(d) = r;
6456: ndl_copy(DL(t),DL(d));
6457: NMV_ADV(d);
6458: dlen++;
6459: }
6460: }
6461: LEN(p) = dlen;
6462: }
6463:
6464: NDV ptondv(VL vl,VL dvl,P p)
6465: {
6466: ND nd;
6467:
6468: nd = ptond(vl,dvl,p);
6469: return ndtondv(0,nd);
6470: }
6471:
6472: void pltozpl(LIST l,Q *cont,LIST *pp)
6473: {
1.16 noro 6474: NODE nd,nd1;
6475: int n;
6476: P *pl;
6477: Q *cl;
6478: int i;
6479: P dmy;
6480: Z dvr,inv;
6481: LIST r;
6482:
6483: nd = BDY(l); n = length(nd);
6484: pl = (P *)MALLOC(n*sizeof(P));
6485: cl = (Q *)MALLOC(n*sizeof(Q));
6486: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6487: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6488: }
6489: qltozl(cl,n,&dvr);
6490: divz(ONE,dvr,&inv);
6491: nd = BDY(l);
6492: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6493: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6494: nd = 0;
6495: for ( i = n-1; i >= 0; i-- ) {
6496: MKNODE(nd1,pl[i],nd); nd = nd1;
6497: }
6498: MKLIST(r,nd);
6499: *pp = r;
1.1 noro 6500: }
6501:
6502: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6503:
6504: NDV pltondv(VL vl,VL dvl,LIST p)
6505: {
6506: int i;
6507: NODE t;
6508: ND r,ri;
6509: NM m;
6510:
6511: if ( !nd_module ) error("pltond : module order must be set");
6512: r = 0;
6513: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6514: ri = ptond(vl,dvl,(P)BDY(t));
6515: if ( ri )
6516: for ( m = BDY(ri); m; m = NEXT(m) ) {
6517: MPOS(DL(m)) = i;
6518: TD(DL(m)) = ndl_weight(DL(m));
6519: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6520: }
6521: r = nd_add(0,r,ri);
6522: }
6523: return ndtondv(0,r);
6524: }
6525:
6526: ND ptond(VL vl,VL dvl,P p)
6527: {
6528: int n,i,j,k,e;
6529: VL tvl;
6530: V v;
6531: DCP dc;
6532: DCP *w;
6533: ND r,s,t,u;
6534: P x;
6535: int c;
6536: UINT *d;
6537: NM m,m0;
6538:
6539: if ( !p )
6540: return 0;
6541: else if ( NUM(p) ) {
6542: NEWNM(m);
6543: ndl_zero(DL(m));
6544: if ( !INT((Q)p) )
6545: error("ptond : input must be integer-coefficient");
1.6 noro 6546: CZ(m) = (Z)p;
1.1 noro 6547: NEXT(m) = 0;
6548: MKND(nd_nvar,m,1,r);
6549: SG(r) = 0;
6550: return r;
6551: } else {
6552: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6553: w = (DCP *)MALLOC(k*sizeof(DCP));
6554: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6555: for ( i = 0, tvl = dvl, v = VR(p);
6556: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6557: if ( !tvl ) {
6558: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6559: t = ptond(vl,dvl,COEF(w[j]));
6560: pwrp(vl,x,DEG(w[j]),&p);
6561: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6562: }
6563: return s;
6564: } else {
6565: NEWNM(m0); d = DL(m0);
6566: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6567: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6568: TD(d) = MUL_WEIGHT(e,i);
6569: if ( nd_blockmask) ndl_weight_mask(d);
6570: if ( nd_module ) MPOS(d) = 0;
6571: t = ptond(vl,dvl,COEF(w[j]));
6572: for ( m = BDY(t); m; m = NEXT(m) )
6573: ndl_addto(DL(m),d);
6574: SG(t) += TD(d);
6575: s = nd_add(0,s,t);
6576: }
6577: FREENM(m0);
6578: return s;
6579: }
6580: }
6581: }
6582:
6583: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6584: {
6585: VL tvl;
6586: int len,n,j,i,e;
6587: NMV m;
6588: Z q;
6589: P c;
6590: UINT *d;
6591: P s,r,u,t,w;
6592: GFS gfs;
6593:
6594: if ( !p ) return 0;
6595: else {
6596: len = LEN(p);
6597: n = NV(p);
6598: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6599: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6600: if ( mod == -1 ) {
6601: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6602: } else if ( mod == -2 ) {
6603: c = (P)CZ(m);
6604: } else if ( mod > 0 ) {
1.6 noro 6605: STOZ(CM(m),q); c = (P)q;
1.1 noro 6606: } else
6607: c = CP(m);
6608: d = DL(m);
6609: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6610: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6611: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6612: }
6613: addp(vl,s,t,&u); s = u;
6614: }
6615: return s;
6616: }
6617: }
6618:
6619: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6620: {
6621: VL tvl;
6622: int len,n,j,i,e;
6623: NMV m;
6624: Z q;
6625: P c;
6626: UINT *d;
6627: P s,r,u,t,w;
6628: GFS gfs;
6629: P *a;
6630: LIST l;
6631: NODE nd,nd1;
6632:
6633: if ( !p ) return 0;
6634: else {
6635: a = (P *)MALLOC((rank+1)*sizeof(P));
6636: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6637: len = LEN(p);
6638: n = NV(p);
6639: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6640: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6641: if ( mod == -1 ) {
6642: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6643: } else if ( mod ) {
1.6 noro 6644: STOZ(CM(m),q); c = (P)q;
1.1 noro 6645: } else
6646: c = CP(m);
6647: d = DL(m);
6648: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6649: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6650: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6651: }
6652: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6653: }
6654: nd = 0;
6655: for ( i = rank; i > 0; i-- ) {
6656: MKNODE(nd1,a[i],nd); nd = nd1;
6657: }
6658: MKLIST(l,nd);
6659: return l;
6660: }
6661: }
6662:
6663: NDV ndtondv(int mod,ND p)
6664: {
6665: NDV d;
6666: NMV m,m0;
6667: NM t;
6668: int i,len;
6669:
6670: if ( !p ) return 0;
6671: len = LEN(p);
6672: if ( mod > 0 || mod == -1 )
6673: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6674: else
6675: m0 = m = MALLOC(len*nmv_adv);
6676: #if 0
6677: ndv_alloc += nmv_adv*len;
6678: #endif
6679: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6680: ndl_copy(DL(t),DL(m));
1.6 noro 6681: CZ(m) = CZ(t);
1.1 noro 6682: }
6683: MKNDV(NV(p),m0,len,d);
6684: SG(d) = SG(p);
1.24 noro 6685: d->sig = p->sig;
1.1 noro 6686: return d;
6687: }
6688:
1.16 noro 6689: static int dmm_comp_nv;
6690:
6691: int dmm_comp(DMM *a,DMM *b)
6692: {
6693: return -compdmm(dmm_comp_nv,*a,*b);
6694: }
6695:
6696: void dmm_sort_by_ord(DMM *a,int len,int nv)
6697: {
6698: dmm_comp_nv = nv;
6699: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6700: }
6701:
6702: void dpm_sort(DPM p,DPM *rp)
6703: {
6704: DMM t,t1;
6705: int len,i,n;
6706: DMM *a;
6707: DPM d;
6708:
6709: if ( !p ) *rp = 0;
6710: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6711: a = (DMM *)MALLOC(len*sizeof(DMM));
6712: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6713: n = p->nv;
6714: dmm_sort_by_ord(a,len,n);
6715: t = 0;
6716: for ( i = len-1; i >= 0; i-- ) {
6717: NEWDMM(t1);
6718: t1->c = a[i]->c;
6719: t1->dl = a[i]->dl;
6720: t1->pos = a[i]->pos;
6721: t1->next = t;
6722: t = t1;
6723: }
6724: MKDPM(n,t,d);
6725: SG(d) = SG(p);
6726: *rp = d;
6727: }
6728:
1.18 noro 6729: int dpm_comp(DPM *a,DPM *b)
6730: {
1.22 noro 6731: return -compdpm(CO,*a,*b);
1.18 noro 6732: }
6733:
6734: NODE dpm_sort_list(NODE l)
6735: {
6736: int i,len;
6737: NODE t,t1;
6738: DPM *a;
6739:
6740: len = length(l);
6741: a = (DPM *)MALLOC(len*sizeof(DPM));
6742: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6743: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6744: t = 0;
6745: for ( i = len-1; i >= 0; i-- ) {
6746: MKNODE(t1,(pointer)a[i],t); t = t1;
6747: }
6748: return t;
6749: }
6750:
1.20 noro 6751: int nmv_comp(NMV a,NMV b)
6752: {
1.21 noro 6753: int t;
6754: t = DL_COMPARE(a->dl,b->dl);
6755: return -t;
1.20 noro 6756: }
6757:
1.16 noro 6758: NDV dpmtondv(int mod,DPM p)
6759: {
6760: NDV d;
6761: NMV m,m0;
6762: DMM t;
6763: DMM *a;
6764: int i,len,n;
6765:
6766: if ( !p ) return 0;
6767: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6768: a = (DMM *)MALLOC(len*sizeof(DMM));
6769: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6770: n = p->nv;
6771: dmm_sort_by_ord(a,len,n);
6772: if ( mod > 0 || mod == -1 )
6773: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6774: else
6775: m0 = m = MALLOC(len*nmv_adv);
6776: #if 0
6777: ndv_alloc += nmv_adv*len;
6778: #endif
6779: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6780: dltondl(n,a[i]->dl,DL(m));
6781: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6782: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6783: CZ(m) = (Z)a[i]->c;
6784: }
1.20 noro 6785: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6786: MKNDV(NV(p),m0,len,d);
6787: SG(d) = SG(p);
6788: return d;
6789: }
6790:
1.1 noro 6791: ND ndvtond(int mod,NDV p)
6792: {
6793: ND d;
6794: NM m,m0;
6795: NMV t;
6796: int i,len;
6797:
6798: if ( !p ) return 0;
6799: m0 = 0;
6800: len = p->len;
6801: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6802: NEXTNM(m0,m);
6803: ndl_copy(DL(t),DL(m));
1.6 noro 6804: CZ(m) = CZ(t);
1.1 noro 6805: }
6806: NEXT(m) = 0;
6807: MKND(NV(p),m0,len,d);
6808: SG(d) = SG(p);
1.28 noro 6809: d->sig = p->sig;
1.1 noro 6810: return d;
6811: }
6812:
6813: DP ndvtodp(int mod,NDV p)
6814: {
6815: MP m,m0;
6816: DP d;
6817: NMV t;
6818: int i,len;
6819:
6820: if ( !p ) return 0;
6821: m0 = 0;
6822: len = p->len;
6823: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6824: NEXTMP(m0,m);
6825: m->dl = ndltodl(nd_nvar,DL(t));
6826: m->c = (Obj)ndctop(mod,t->c);
6827: }
6828: NEXT(m) = 0;
6829: MKDP(nd_nvar,m0,d);
6830: SG(d) = SG(p);
6831: return d;
6832: }
6833:
1.16 noro 6834: DPM ndvtodpm(int mod,NDV p)
6835: {
6836: DMM m,m0;
6837: DPM d;
6838: NMV t;
6839: int i,len;
6840:
6841: if ( !p ) return 0;
6842: m0 = 0;
6843: len = p->len;
6844: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6845: NEXTDMM(m0,m);
6846: m->dl = ndltodl(nd_nvar,DL(t));
6847: m->c = (Obj)ndctop(mod,t->c);
6848: m->pos = MPOS(DL(t));
6849: }
6850: NEXT(m) = 0;
6851: MKDPM(nd_nvar,m0,d);
6852: SG(d) = SG(p);
6853: return d;
6854: }
6855:
6856:
1.1 noro 6857: DP ndtodp(int mod,ND p)
6858: {
6859: MP m,m0;
6860: DP d;
6861: NM t;
6862: int i,len;
6863:
6864: if ( !p ) return 0;
6865: m0 = 0;
6866: len = p->len;
6867: for ( t = BDY(p); t; t = NEXT(t) ) {
6868: NEXTMP(m0,m);
6869: m->dl = ndltodl(nd_nvar,DL(t));
6870: m->c = (Obj)ndctop(mod,t->c);
6871: }
6872: NEXT(m) = 0;
6873: MKDP(nd_nvar,m0,d);
6874: SG(d) = SG(p);
6875: return d;
6876: }
6877:
6878: void ndv_print(NDV p)
6879: {
6880: NMV m;
6881: int i,len;
6882:
6883: if ( !p ) printf("0\n");
6884: else {
6885: len = LEN(p);
6886: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6887: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 6888: else printf("+%ld*",CM(m));
1.1 noro 6889: ndl_print(DL(m));
6890: }
6891: printf("\n");
6892: }
6893: }
6894:
6895: void ndv_print_q(NDV p)
6896: {
6897: NMV m;
6898: int i,len;
6899:
6900: if ( !p ) printf("0\n");
6901: else {
6902: len = LEN(p);
6903: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6904: printf("+");
1.6 noro 6905: printexpr(CO,(Obj)CZ(m));
1.1 noro 6906: printf("*");
6907: ndl_print(DL(m));
6908: }
6909: printf("\n");
6910: }
6911: }
6912:
6913: NODE ndv_reducebase(NODE x,int *perm)
6914: {
6915: int len,i,j;
6916: NDVI w;
6917: NODE t,t0;
6918:
1.41 ! noro 6919: if ( nd_norb ) return x;
1.1 noro 6920: len = length(x);
6921: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6922: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6923: w[i].p = BDY(t); w[i].i = perm[i];
6924: }
6925: for ( i = 0; i < len; i++ ) {
6926: for ( j = 0; j < i; j++ ) {
6927: if ( w[i].p && w[j].p ) {
6928: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6929: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6930: }
6931: }
6932: }
6933: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6934: if ( w[i].p ) {
6935: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6936: perm[j++] = w[i].i;
6937: }
6938: }
6939: NEXT(t) = 0; x = t0;
6940: return x;
6941: }
6942:
6943: /* XXX incomplete */
6944:
1.21 noro 6945: extern DMMstack dmm_stack;
6946: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6947:
1.1 noro 6948: void nd_init_ord(struct order_spec *ord)
6949: {
6950: nd_module = (ord->id >= 256);
6951: if ( nd_module ) {
6952: nd_dcomp = -1;
1.21 noro 6953: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6954: nd_pot_nelim = ord->pot_nelim;
6955: nd_poly_weight_len = ord->nv;
6956: nd_poly_weight = ord->top_weight;
6957: nd_module_rank = ord->module_rank;
6958: nd_module_weight = ord->module_top_weight;
6959: }
6960: nd_matrix = 0;
6961: nd_matrix_len = 0;
6962: switch ( ord->id ) {
6963: case 0:
6964: switch ( ord->ord.simple ) {
6965: case 0:
6966: nd_dcomp = 1;
6967: nd_isrlex = 1;
6968: break;
6969: case 1:
6970: nd_dcomp = 1;
6971: nd_isrlex = 0;
6972: break;
6973: case 2:
6974: nd_dcomp = 0;
6975: nd_isrlex = 0;
6976: ndl_compare_function = ndl_lex_compare;
6977: break;
6978: case 11:
6979: /* XXX */
6980: nd_dcomp = 0;
6981: nd_isrlex = 1;
6982: ndl_compare_function = ndl_ww_lex_compare;
6983: break;
6984: default:
6985: error("nd_gr : unsupported order");
6986: }
6987: break;
6988: case 1:
6989: /* block order */
6990: /* XXX */
6991: nd_dcomp = -1;
6992: nd_isrlex = 0;
6993: ndl_compare_function = ndl_block_compare;
6994: break;
6995: case 2:
6996: /* matrix order */
6997: /* XXX */
6998: nd_dcomp = -1;
6999: nd_isrlex = 0;
7000: nd_matrix_len = ord->ord.matrix.row;
7001: nd_matrix = ord->ord.matrix.matrix;
7002: ndl_compare_function = ndl_matrix_compare;
7003: break;
7004: case 3:
7005: /* composite order */
7006: nd_dcomp = -1;
7007: nd_isrlex = 0;
7008: nd_worb_len = ord->ord.composite.length;
7009: nd_worb = ord->ord.composite.w_or_b;
7010: ndl_compare_function = ndl_composite_compare;
7011: break;
7012:
7013: /* module order */
7014: case 256:
7015: switch ( ord->ord.simple ) {
7016: case 0:
1.21 noro 7017: nd_dcomp = 0;
1.1 noro 7018: nd_isrlex = 1;
1.21 noro 7019: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7020: break;
7021: case 1:
1.21 noro 7022: nd_dcomp = 0;
1.1 noro 7023: nd_isrlex = 0;
7024: ndl_compare_function = ndl_module_glex_compare;
7025: break;
7026: case 2:
1.21 noro 7027: nd_dcomp = 0;
1.1 noro 7028: nd_isrlex = 0;
1.21 noro 7029: ndl_compare_function = ndl_module_compare;
7030: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7031: break;
7032: default:
1.21 noro 7033: error("nd_init_ord : unsupported order");
1.1 noro 7034: }
7035: break;
7036: case 257:
7037: /* block order */
7038: nd_isrlex = 0;
1.21 noro 7039: ndl_compare_function = ndl_module_compare;
7040: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7041: break;
7042: case 258:
7043: /* matrix order */
7044: nd_isrlex = 0;
7045: nd_matrix_len = ord->ord.matrix.row;
7046: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7047: ndl_compare_function = ndl_module_compare;
7048: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7049: break;
7050: case 259:
7051: /* composite order */
7052: nd_isrlex = 0;
7053: nd_worb_len = ord->ord.composite.length;
7054: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7055: ndl_compare_function = ndl_module_compare;
7056: ndl_base_compare_function = ndl_composite_compare;
7057: break;
7058: case 300:
7059: /* schreyer order */
7060: if ( ord->base->id != 256 )
7061: error("nd_init_ord : unsupported base order");
7062: ndl_compare_function = ndl_module_schreyer_compare;
7063: dmm_stack = ord->dmmstack;
7064: switch ( ord->base->ord.simple ) {
7065: case 0:
7066: nd_isrlex = 1;
7067: ndl_base_compare_function = ndl_glex_compare;
7068: dl_base_compare_function = cmpdl_revgradlex;
7069: break;
7070: case 1:
7071: nd_isrlex = 0;
7072: ndl_base_compare_function = ndl_glex_compare;
7073: dl_base_compare_function = cmpdl_gradlex;
7074: break;
7075: case 2:
7076: nd_isrlex = 0;
7077: ndl_base_compare_function = ndl_lex_compare;
7078: dl_base_compare_function = cmpdl_lex;
7079: break;
7080: default:
7081: error("nd_init_ord : unsupported order");
7082: }
1.1 noro 7083: break;
7084: }
7085: nd_ord = ord;
7086: }
7087:
7088: BlockMask nd_create_blockmask(struct order_spec *ord)
7089: {
7090: int n,i,j,s,l;
7091: UINT *t;
7092: BlockMask bm;
7093:
7094: /* we only create mask table for block order */
7095: if ( ord->id != 1 && ord->id != 257 )
7096: return 0;
7097: n = ord->ord.block.length;
7098: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7099: bm->n = n;
7100: bm->order_pair = ord->ord.block.order_pair;
7101: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7102: for ( i = 0, s = 0; i < n; i++ ) {
7103: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7104: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7105: l = bm->order_pair[i].length;
7106: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7107: }
7108: return bm;
7109: }
7110:
7111: EPOS nd_create_epos(struct order_spec *ord)
7112: {
7113: int i,j,l,s,ord_l,ord_o;
7114: EPOS epos;
7115: struct order_pair *op;
7116:
7117: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7118: switch ( ord->id ) {
1.21 noro 7119: case 0: case 256: case 300:
1.1 noro 7120: if ( nd_isrlex ) {
7121: for ( i = 0; i < nd_nvar; i++ ) {
7122: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7123: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7124: }
7125: } else {
7126: for ( i = 0; i < nd_nvar; i++ ) {
7127: epos[i].i = nd_exporigin + i/nd_epw;
7128: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7129: }
7130: }
7131: break;
7132: case 1: case 257:
7133: /* block order */
7134: l = ord->ord.block.length;
7135: op = ord->ord.block.order_pair;
7136: for ( j = 0, s = 0; j < l; j++ ) {
7137: ord_o = op[j].order;
7138: ord_l = op[j].length;
7139: if ( !ord_o )
7140: for ( i = 0; i < ord_l; i++ ) {
7141: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7142: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7143: }
7144: else
7145: for ( i = 0; i < ord_l; i++ ) {
7146: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7147: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7148: }
7149: s += ord_l;
7150: }
7151: break;
7152: case 2:
7153: /* matrix order */
7154: case 3:
7155: /* composite order */
7156: default:
7157: for ( i = 0; i < nd_nvar; i++ ) {
7158: epos[i].i = nd_exporigin + i/nd_epw;
7159: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7160: }
7161: break;
7162: }
7163: return epos;
7164: }
7165:
7166: /* external interface */
7167:
7168: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7169: {
7170: NODE t,in0,in;
7171: ND ndf,nf;
7172: NDV ndvf;
7173: VL vv,tv;
7174: int stat,nvar,max,mrank;
7175: union oNDC dn;
7176: Q cont;
7177: P pp;
7178: LIST ppl;
7179:
7180: if ( !f ) {
7181: *rp = 0;
7182: return;
7183: }
7184: pltovl(v,&vv);
7185: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7186:
7187: /* max=65536 implies nd_bpe=32 */
7188: max = 65536;
7189:
7190: nd_module = 0;
7191: /* nd_module will be set if ord is a module ordering */
7192: nd_init_ord(ord);
7193: nd_setup_parameters(nvar,max);
7194: if ( nd_module && OID(f) != O_LIST )
7195: error("nd_nf_p : the first argument must be a list");
7196: if ( nd_module ) mrank = length(BDY((LIST)f));
7197: /* conversion to ndv */
7198: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7199: NEXTNODE(in0,in);
7200: if ( nd_module ) {
7201: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7202: || length(BDY((LIST)BDY(t))) != mrank )
7203: error("nd_nf_p : inconsistent basis element");
7204: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7205: else ppl = (LIST)BDY(t);
7206: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7207: } else {
7208: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7209: else pp = (P)BDY(t);
7210: BDY(in) = (pointer)ptondv(CO,vv,pp);
7211: }
7212: if ( m ) ndv_mod(m,(NDV)BDY(in));
7213: }
7214: if ( in0 ) NEXT(in) = 0;
7215:
7216: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7217: else ndvf = ptondv(CO,vv,(P)f);
7218: if ( m ) ndv_mod(m,ndvf);
7219: ndf = (pointer)ndvtond(m,ndvf);
7220:
7221: /* dont sort, dont removecont */
1.24 noro 7222: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7223: nd_scale=2;
1.6 noro 7224: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7225: if ( !stat )
7226: error("nd_nf_p : exponent too large");
7227: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7228: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7229: }
7230:
7231: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7232: {
7233: NM m;
7234: UINT *t,*s;
7235: int i;
7236:
7237: for ( i = 0; i < n; i++ ) r[i] = 0;
7238: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7239: t = DL(m);
7240: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7241: r[i] = CM(m);
7242: }
7243: for ( i = 0; !r[i]; i++ );
7244: return i;
7245: }
7246:
7247: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7248: {
7249: NM m;
7250: UINT *t,*s;
7251: int i;
7252:
7253: for ( i = 0; i < n; i++ ) r[i] = 0;
7254: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7255: t = DL(m);
7256: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7257: r[i] = CZ(m);
1.1 noro 7258: }
7259: for ( i = 0; !r[i]; i++ );
7260: return i;
7261: }
7262:
7263: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7264: {
7265: NM m;
7266: UINT *t,*s;
7267: int i;
7268:
7269: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7270: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7271: t = DL(m);
7272: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7273: mpz_set(r[i],BDY(CZ(m)));
7274: }
7275: for ( i = 0; !mpz_sgn(r[i]); i++ );
7276: return i;
7277: }
7278:
7279: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7280: {
7281: NM m;
7282: unsigned long *v;
7283: int i,j,h,size;
7284: UINT *s,*t;
7285:
7286: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7287: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7288: bzero(v,size);
7289: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7290: t = DL(m);
7291: h = ndl_hash_value(t);
7292: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7293: v[i/BLEN] |= 1L <<(i%BLEN);
7294: }
7295: return v;
7296: }
7297:
7298: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7299: {
7300: NMV mr;
7301: UINT *d,*t,*s;
7302: int i,j,len,h,head;
7303:
7304: d = DL(m);
7305: len = LEN(p);
7306: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7307: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7308: ndl_add(d,DL(mr),t);
7309: h = ndl_hash_value(t);
7310: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7311: if ( j == 0 ) head = i;
7312: v[i/BLEN] |= 1L <<(i%BLEN);
7313: }
7314: return head;
7315: }
7316:
7317: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7318: {
7319: NM m;
7320: NMV mr;
7321: UINT *d,*t,*s;
7322: NDV p;
7323: int i,j,len;
7324: Z *r;
7325:
7326: m = pair->mul;
7327: d = DL(m);
7328: p = nd_ps[pair->index];
7329: len = LEN(p);
7330: r = (Z *)CALLOC(n,sizeof(Q));
7331: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7332: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7333: ndl_add(d,DL(mr),t);
7334: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7335: r[i] = CZ(mr);
1.1 noro 7336: }
7337: return r;
7338: }
7339:
1.11 noro 7340: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7341: {
7342: NM m;
7343: NMV mr;
1.11 noro 7344: UINT *d,*t,*s,*u;
1.1 noro 7345: NDV p;
7346: unsigned char *ivc;
7347: unsigned short *ivs;
7348: UINT *v,*ivi,*s0v;
1.11 noro 7349: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7350: IndArray r;
7351:
7352: m = pair->mul;
7353: d = DL(m);
7354: if ( trace )
7355: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7356: else
7357: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7358:
7359: len = LEN(p);
7360: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7361: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7362: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7363: ndl_add(d,DL(mr),t);
7364: st = prev;
7365: ed = n;
7366: while ( ed > st ) {
7367: md = (st+ed)/2;
7368: u = s0+md*nd_wpd;
7369: c = DL_COMPARE(u,t);
7370: if ( c == 0 ) break;
7371: else if ( c > 0 ) st = md;
7372: else ed = md;
7373: }
7374: prev = v[j] = md;
1.1 noro 7375: }
7376: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7377: r->head = v[0];
7378: diff = 0;
7379: for ( i = 1; i < len; i++ ) {
7380: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7381: }
7382: if ( diff < 256 ) {
7383: r->width = 1;
7384: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7385: r->index.c = ivc;
7386: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7387: } else if ( diff < 65536 ) {
7388: r->width = 2;
7389: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7390: r->index.s = ivs;
7391: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7392: } else {
7393: r->width = 4;
7394: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7395: r->index.i = ivi;
7396: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7397: }
7398: return r;
7399: }
7400:
7401: int compress_array(Z *svect,Z *cvect,int n)
7402: {
7403: int i,j;
7404:
7405: for ( i = j = 0; i < n; i++ )
7406: if ( svect[i] ) cvect[j++] = svect[i];
7407: return j;
7408: }
7409:
7410: void expand_array(Z *svect,Z *cvect,int n)
7411: {
7412: int i,j;
7413:
7414: for ( i = j = 0; j < n; i++ )
7415: if ( svect[i] ) svect[i] = cvect[j++];
7416: }
7417:
1.8 noro 7418: #if 0
1.1 noro 7419: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7420: {
7421: int i,j,k,len,pos,prev,nz;
7422: Z cs,mcs,c1,c2,cr,gcd,t;
7423: IndArray ivect;
7424: unsigned char *ivc;
7425: unsigned short *ivs;
7426: unsigned int *ivi;
7427: NDV redv;
7428: NMV mr;
7429: NODE rp;
7430: int maxrs;
7431: double hmag;
7432: Z *cvect;
1.3 noro 7433: int l;
1.1 noro 7434:
7435: maxrs = 0;
7436: for ( i = 0; i < col && !svect[i]; i++ );
7437: if ( i == col ) return maxrs;
7438: hmag = p_mag((P)svect[i])*nd_scale;
7439: cvect = (Z *)MALLOC(col*sizeof(Q));
7440: for ( i = 0; i < nred; i++ ) {
7441: ivect = imat[i];
7442: k = ivect->head;
7443: if ( svect[k] ) {
7444: maxrs = MAX(maxrs,rp0[i]->sugar);
7445: redv = nd_demand?ndv_load(rp0[i]->index)
7446: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7447: len = LEN(redv); mr = BDY(redv);
1.6 noro 7448: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7449: chsgnz(cs,&mcs);
7450: if ( !UNIQ(cr) ) {
7451: for ( j = 0; j < col; j++ ) {
7452: mulz(svect[j],cr,&c1); svect[j] = c1;
7453: }
7454: }
7455: svect[k] = 0; prev = k;
7456: switch ( ivect->width ) {
7457: case 1:
7458: ivc = ivect->index.c;
7459: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7460: pos = prev+ivc[j]; prev = pos;
1.6 noro 7461: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7462: }
7463: break;
7464: case 2:
7465: ivs = ivect->index.s;
7466: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7467: pos = prev+ivs[j]; prev = pos;
1.6 noro 7468: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7469: }
7470: break;
7471: case 4:
7472: ivi = ivect->index.i;
7473: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7474: pos = prev+ivi[j]; prev = pos;
1.6 noro 7475: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7476: }
7477: break;
7478: }
7479: for ( j = k+1; j < col && !svect[j]; j++ );
7480: if ( j == col ) break;
7481: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7482: nz = compress_array(svect,cvect,col);
7483: removecont_array((P *)cvect,nz,1);
7484: expand_array(svect,cvect,nz);
7485: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7486: }
7487: }
7488: }
7489: nz = compress_array(svect,cvect,col);
7490: removecont_array((P *)cvect,nz,1);
7491: expand_array(svect,cvect,nz);
7492: if ( DP_Print ) {
7493: fprintf(asir_out,"-"); fflush(asir_out);
7494: }
7495: return maxrs;
7496: }
1.4 noro 7497: #else
1.9 noro 7498:
1.4 noro 7499: /* direct mpz version */
7500: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7501: {
7502: int i,j,k,len,pos,prev;
7503: mpz_t cs,cr,gcd;
7504: IndArray ivect;
7505: unsigned char *ivc;
7506: unsigned short *ivs;
7507: unsigned int *ivi;
7508: NDV redv;
7509: NMV mr;
7510: NODE rp;
7511: int maxrs;
7512: double hmag;
7513: int l;
1.13 noro 7514: static mpz_t *svect;
7515: static int svect_len=0;
1.4 noro 7516:
7517: maxrs = 0;
7518: for ( i = 0; i < col && !svect0[i]; i++ );
7519: if ( i == col ) return maxrs;
7520: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7521: if ( col > svect_len ) {
7522: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7523: svect_len = col;
7524: }
1.4 noro 7525: for ( i = 0; i < col; i++ ) {
7526: mpz_init(svect[i]);
7527: if ( svect0[i] )
7528: mpz_set(svect[i],BDY(svect0[i]));
7529: else
7530: mpz_set_ui(svect[i],0);
7531: }
7532: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7533: for ( i = 0; i < nred; i++ ) {
7534: ivect = imat[i];
7535: k = ivect->head;
7536: if ( mpz_sgn(svect[k]) ) {
7537: maxrs = MAX(maxrs,rp0[i]->sugar);
7538: redv = nd_demand?ndv_load(rp0[i]->index)
7539: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7540: len = LEN(redv); mr = BDY(redv);
1.6 noro 7541: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7542: mpz_div(cs,svect[k],gcd);
1.6 noro 7543: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7544: mpz_neg(cs,cs);
1.9 noro 7545: if ( MUNIMPZ(cr) )
7546: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7547: else if ( !UNIMPZ(cr) )
7548: for ( j = 0; j < col; j++ ) {
7549: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7550: }
1.4 noro 7551: mpz_set_ui(svect[k],0);
7552: prev = k;
7553: switch ( ivect->width ) {
7554: case 1:
7555: ivc = ivect->index.c;
7556: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7557: pos = prev+ivc[j]; prev = pos;
1.6 noro 7558: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7559: }
7560: break;
7561: case 2:
7562: ivs = ivect->index.s;
7563: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7564: pos = prev+ivs[j]; prev = pos;
1.6 noro 7565: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7566: }
7567: break;
7568: case 4:
7569: ivi = ivect->index.i;
7570: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7571: pos = prev+ivi[j]; prev = pos;
1.6 noro 7572: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7573: }
7574: break;
7575: }
7576: for ( j = k+1; j < col && !svect[j]; j++ );
7577: if ( j == col ) break;
7578: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7579: mpz_removecont_array(svect,col);
7580: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7581: }
7582: }
7583: }
7584: mpz_removecont_array(svect,col);
7585: if ( DP_Print ) {
7586: fprintf(asir_out,"-"); fflush(asir_out);
7587: }
7588: for ( i = 0; i < col; i++ )
7589: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7590: else svect0[i] = 0;
7591: return maxrs;
7592: }
7593: #endif
1.1 noro 7594:
1.40 noro 7595: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 7596: {
7597: int i,j,k,len,pos,prev;
7598: UINT c,c1,c2,c3,up,lo,dmy;
7599: IndArray ivect;
7600: unsigned char *ivc;
7601: unsigned short *ivs;
7602: unsigned int *ivi;
7603: NDV redv;
7604: NMV mr;
7605: NODE rp;
7606: int maxrs;
7607:
7608: maxrs = 0;
7609: for ( i = 0; i < nred; i++ ) {
7610: ivect = imat[i];
7611: k = ivect->head; svect[k] %= m;
1.40 noro 7612: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 7613: maxrs = MAX(maxrs,rp0[i]->sugar);
7614: c = m-c; redv = nd_ps[rp0[i]->index];
7615: len = LEN(redv); mr = BDY(redv);
7616: svect[k] = 0; prev = k;
7617: switch ( ivect->width ) {
7618: case 1:
7619: ivc = ivect->index.c;
7620: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7621: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 7622: if ( c1 ) {
7623: c2 = svect[pos];
1.1 noro 7624: DMA(c1,c,c2,up,lo);
7625: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7626: } else svect[pos] = lo;
1.40 noro 7627: }
1.1 noro 7628: }
7629: break;
7630: case 2:
7631: ivs = ivect->index.s;
7632: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7633: pos = prev+ivs[j]; c1 = CM(mr);
7634: prev = pos;
1.40 noro 7635: if ( c1 ) {
7636: c2 = svect[pos];
1.1 noro 7637: DMA(c1,c,c2,up,lo);
7638: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7639: } else svect[pos] = lo;
1.40 noro 7640: }
1.1 noro 7641: }
7642: break;
7643: case 4:
7644: ivi = ivect->index.i;
7645: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7646: pos = prev+ivi[j]; c1 = CM(mr);
7647: prev = pos;
1.40 noro 7648: if ( c1 ) {
7649: c2 = svect[pos];
1.1 noro 7650: DMA(c1,c,c2,up,lo);
7651: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7652: } else svect[pos] = lo;
1.40 noro 7653: }
1.1 noro 7654: }
7655: break;
7656: }
7657: }
7658: }
7659: for ( i = 0; i < col; i++ )
7660: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7661: return maxrs;
7662: }
7663:
7664: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7665: {
7666: int i,j,k,len,pos,prev;
7667: UINT c,c1,c2,c3,up,lo,dmy;
7668: IndArray ivect;
7669: unsigned char *ivc;
7670: unsigned short *ivs;
7671: unsigned int *ivi;
7672: NDV redv;
7673: NMV mr;
7674: NODE rp;
7675: int maxrs;
7676:
7677: maxrs = 0;
7678: for ( i = 0; i < nred; i++ ) {
7679: ivect = imat[i];
7680: k = ivect->head;
7681: if ( (c = svect[k]) != 0 ) {
7682: maxrs = MAX(maxrs,rp0[i]->sugar);
7683: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7684: len = LEN(redv); mr = BDY(redv);
7685: svect[k] = 0; prev = k;
7686: switch ( ivect->width ) {
7687: case 1:
7688: ivc = ivect->index.c;
7689: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7690: pos = prev+ivc[j]; prev = pos;
7691: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7692: }
7693: break;
7694: case 2:
7695: ivs = ivect->index.s;
7696: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7697: pos = prev+ivs[j]; prev = pos;
7698: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7699: }
7700: break;
7701: case 4:
7702: ivi = ivect->index.i;
7703: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7704: pos = prev+ivi[j]; prev = pos;
7705: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7706: }
7707: break;
7708: }
7709: }
7710: }
7711: return maxrs;
7712: }
7713:
7714: ND nd_add_lf(ND p1,ND p2)
7715: {
7716: int n,c,can;
7717: ND r;
7718: NM m1,m2,mr0,mr,s;
7719: Z t;
7720:
7721: if ( !p1 ) return p2;
7722: else if ( !p2 ) return p1;
7723: else {
7724: can = 0;
7725: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7726: c = DL_COMPARE(DL(m1),DL(m2));
7727: switch ( c ) {
7728: case 0:
7729: addlf(CZ(m1),CZ(m2),&t);
7730: s = m1; m1 = NEXT(m1);
7731: if ( t ) {
7732: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7733: } else {
7734: can += 2; FREENM(s);
7735: }
7736: s = m2; m2 = NEXT(m2); FREENM(s);
7737: break;
7738: case 1:
7739: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7740: break;
7741: case -1:
7742: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7743: break;
7744: }
7745: }
7746: if ( !mr0 )
7747: if ( m1 ) mr0 = m1;
7748: else if ( m2 ) mr0 = m2;
7749: else return 0;
7750: else if ( m1 ) NEXT(mr) = m1;
7751: else if ( m2 ) NEXT(mr) = m2;
7752: else NEXT(mr) = 0;
7753: BDY(p1) = mr0;
7754: SG(p1) = MAX(SG(p1),SG(p2));
7755: LEN(p1) = LEN(p1)+LEN(p2)-can;
7756: FREEND(p2);
7757: return p1;
7758: }
7759: }
7760:
7761: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7762: {
7763: int i,j,k,len,pos,prev;
7764: mpz_t c,mc,c1;
7765: IndArray ivect;
7766: unsigned char *ivc;
7767: unsigned short *ivs;
7768: unsigned int *ivi;
7769: NDV redv;
7770: NMV mr;
7771: NODE rp;
7772: int maxrs;
7773:
7774: maxrs = 0;
7775: lf_lazy = 1;
7776: for ( i = 0; i < nred; i++ ) {
7777: ivect = imat[i];
7778: k = ivect->head;
7779: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7780: if ( mpz_sgn(svect[k]) ) {
7781: maxrs = MAX(maxrs,rp0[i]->sugar);
7782: mpz_neg(svect[k],svect[k]);
7783: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7784: len = LEN(redv); mr = BDY(redv);
7785: prev = k;
7786: switch ( ivect->width ) {
7787: case 1:
7788: ivc = ivect->index.c;
7789: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7790: pos = prev+ivc[j]; prev = pos;
7791: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7792: }
7793: break;
7794: case 2:
7795: ivs = ivect->index.s;
7796: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7797: pos = prev+ivs[j]; prev = pos;
7798: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7799: }
7800: break;
7801: case 4:
7802: ivi = ivect->index.i;
7803: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7804: pos = prev+ivi[j]; prev = pos;
7805: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7806: }
7807: break;
7808: }
7809: mpz_set_ui(svect[k],0);
7810: }
7811: }
7812: lf_lazy=0;
7813: for ( i = 0; i < col; i++ ) {
7814: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7815: }
7816: return maxrs;
7817: }
7818:
7819: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7820: {
7821: int i,j,k,l,rank,s;
7822: mpz_t a,a1,inv;
7823: mpz_t *t,*pivot,*pk;
7824: mpz_t **mat;
7825: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7826: int size,size1;
7827:
7828: mpz_init(inv);
7829: mpz_init(a);
7830: mat = (mpz_t **)mat0;
7831: size = 0;
7832: for ( rank = 0, j = 0; j < col; j++ ) {
7833: for ( i = rank; i < row; i++ ) {
7834: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7835: }
7836: for ( i = rank; i < row; i++ )
7837: if ( mpz_sgn(mat[i][j]) )
7838: break;
7839: if ( i == row ) {
7840: colstat[j] = 0;
7841: continue;
7842: } else
7843: colstat[j] = 1;
7844: if ( i != rank ) {
7845: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7846: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7847: }
7848: pivot = mat[rank];
7849: s = sugar[rank];
7850: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7851: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7852: if ( mpz_sgn(*pk) ) {
7853: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7854: }
7855: for ( i = rank+1; i < row; i++ ) {
7856: t = mat[i];
7857: if ( mpz_sgn(t[j]) ) {
7858: sugar[i] = MAX(sugar[i],s);
7859: mpz_neg(a,t[j]);
7860: red_by_vect_lf(t+j,pivot+j,a,col-j);
7861: }
7862: }
7863: rank++;
7864: }
7865: for ( j = col-1, l = rank-1; j >= 0; j-- )
7866: if ( colstat[j] ) {
7867: pivot = mat[l];
7868: s = sugar[l];
7869: for ( k = j; k < col; k++ )
7870: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7871: for ( i = 0; i < l; i++ ) {
7872: t = mat[i];
7873: if ( mpz_sgn(t[j]) ) {
7874: sugar[i] = MAX(sugar[i],s);
7875: mpz_neg(a,t[j]);
7876: red_by_vect_lf(t+j,pivot+j,a,col-j);
7877: }
7878: }
7879: l--;
7880: }
7881: for ( j = 0, l = 0; l < rank; j++ )
7882: if ( colstat[j] ) {
7883: t = mat[l];
7884: for ( k = j; k < col; k++ ) {
7885: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7886: }
7887: l++;
7888: }
7889: return rank;
7890: }
7891:
7892:
7893: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7894: {
7895: int j,k,len;
7896: UINT *p;
7897: UINT c;
7898: NDV r;
7899: NMV mr0,mr;
7900:
7901: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7902: if ( !len ) return 0;
7903: else {
7904: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7905: #if 0
7906: ndv_alloc += nmv_adv*len;
7907: #endif
7908: mr = mr0;
7909: p = s0vect;
7910: for ( j = k = 0; j < col; j++, p += nd_wpd )
7911: if ( !rhead[j] ) {
7912: if ( (c = vect[k++]) != 0 ) {
7913: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7914: }
7915: }
7916: MKNDV(nd_nvar,mr0,len,r);
7917: return r;
7918: }
7919: }
7920:
1.40 noro 7921: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
7922: {
7923: int j,k,len;
7924: UINT *p;
7925: UINT c;
7926: NDV r;
7927: NMV mr0,mr;
7928:
7929: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7930: if ( !len ) return 0;
7931: else {
7932: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7933: mr = mr0;
7934: p = s0vect;
7935: for ( j = k = 0; j < col; j++, p += nd_wpd )
7936: if ( (c = vect[k++]) != 0 ) {
7937: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7938: }
7939: MKNDV(nd_nvar,mr0,len,r);
7940: return r;
7941: }
7942: }
7943:
1.1 noro 7944: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7945: {
7946: int j,k,len;
7947: UINT *p;
7948: NDV r;
7949: NMV mr0,mr;
7950:
7951: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7952: if ( !len ) return 0;
7953: else {
7954: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7955: mr = mr0;
7956: p = s0vect;
7957: for ( j = 0; j < col; j++, p += nd_wpd )
7958: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7959: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7960: }
7961: MKNDV(nd_nvar,mr0,len,r);
7962: return r;
7963: }
7964: }
7965:
7966: /* for preprocessed vector */
7967:
7968: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7969: {
1.6 noro 7970: int j,k,len;
7971: UINT *p;
7972: Z c;
7973: NDV r;
7974: NMV mr0,mr;
1.1 noro 7975:
1.6 noro 7976: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7977: if ( !len ) return 0;
7978: else {
7979: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7980: #if 0
1.6 noro 7981: ndv_alloc += nmv_adv*len;
1.1 noro 7982: #endif
1.6 noro 7983: mr = mr0;
7984: p = s0vect;
7985: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7986: if ( !rhead[j] ) {
7987: if ( (c = vect[k++]) != 0 ) {
7988: if ( !INT(c) )
7989: error("vect_to_ndv_q : components must be integers");
7990: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7991: }
7992: }
1.1 noro 7993: }
1.6 noro 7994: MKNDV(nd_nvar,mr0,len,r);
7995: return r;
7996: }
1.1 noro 7997: }
7998:
7999: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8000: {
8001: int j,k,len;
8002: UINT *p;
8003: mpz_t c;
8004: NDV r;
8005: NMV mr0,mr;
8006:
8007: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8008: if ( !len ) return 0;
8009: else {
8010: mr0 = (NMV)MALLOC(nmv_adv*len);
8011: #if 0
8012: ndv_alloc += nmv_adv*len;
8013: #endif
8014: mr = mr0;
8015: p = s0vect;
8016: for ( j = k = 0; j < col; j++, p += nd_wpd )
8017: if ( !rhead[j] ) {
8018: c[0] = vect[k++][0];
8019: if ( mpz_sgn(c) ) {
8020: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8021: }
8022: }
8023: MKNDV(nd_nvar,mr0,len,r);
8024: return r;
8025: }
8026: }
8027:
8028: /* for plain vector */
8029:
8030: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8031: {
8032: int j,k,len;
8033: UINT *p;
8034: Z c;
8035: NDV r;
8036: NMV mr0,mr;
8037:
8038: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8039: if ( !len ) return 0;
8040: else {
8041: mr0 = (NMV)MALLOC(nmv_adv*len);
8042: #if 0
8043: ndv_alloc += nmv_adv*len;
8044: #endif
8045: mr = mr0;
8046: p = s0vect;
8047: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8048: if ( (c = vect[k]) != 0 ) {
8049: if ( !INT(c) )
1.6 noro 8050: error("plain_vect_to_ndv_q : components must be integers");
8051: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8052: }
8053: MKNDV(nd_nvar,mr0,len,r);
8054: return r;
8055: }
8056: }
8057:
8058: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8059: {
8060: ND_pairs t;
8061: NODE sp0,sp;
8062: int stat;
8063: ND spol;
8064:
8065: for ( t = l; t; t = NEXT(t) ) {
8066: stat = nd_sp(m,trace,t,&spol);
8067: if ( !stat ) return 0;
8068: if ( spol ) {
8069: add_pbucket_symbolic(bucket,spol);
8070: }
8071: }
8072: return 1;
8073: }
8074:
8075: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8076: {
8077: NODE rp0,rp;
8078: NM mul,head,s0,s;
8079: int index,col,i,sugar;
8080: RHist h;
8081: UINT *s0v,*p;
8082: NM_ind_pair pair;
8083: ND red;
8084: NDV *ps;
8085:
8086: s0 = 0; rp0 = 0; col = 0;
8087: if ( nd_demand )
8088: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8089: else
8090: ps = trace?nd_ps_trace:nd_ps;
8091: while ( 1 ) {
8092: head = remove_head_pbucket_symbolic(bucket);
8093: if ( !head ) break;
8094: if ( !s0 ) s0 = head;
8095: else NEXT(s) = head;
8096: s = head;
8097: index = ndl_find_reducer(DL(head));
8098: if ( index >= 0 ) {
8099: h = nd_psh[index];
8100: NEWNM(mul);
8101: ndl_sub(DL(head),DL(h),DL(mul));
8102: if ( ndl_check_bound2(index,DL(mul)) )
8103: return 0;
8104: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8105: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8106: red = ndv_mul_nm_symbolic(mul,ps[index]);
8107: add_pbucket_symbolic(bucket,nd_remove_head(red));
8108: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8109: }
8110: col++;
8111: }
8112: if ( rp0 ) NEXT(rp) = 0;
8113: NEXT(s) = 0;
8114: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8115: for ( i = 0, p = s0v, s = s0; i < col;
8116: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8117: *s0vect = s0v;
8118: *r = rp0;
8119: return col;
8120: }
8121:
8122: void print_ndp(ND_pairs l)
8123: {
8124: ND_pairs t;
8125:
8126: for ( t = l; t; t = NEXT(t) )
8127: printf("[%d,%d] ",t->i1,t->i2);
8128: printf("\n");
8129: }
8130:
8131: NODE nd_f4(int m,int checkonly,int **indp)
8132: {
8133: int i,nh,stat,index,f4red;
8134: NODE r,g,tn0,tn,node;
8135: ND_pairs d,l,t,ll0,ll,lh;
8136: LIST l0,l1;
8137: ND spol,red;
8138: NDV nf,redv;
8139: NM s0,s;
8140: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8141: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8142: UINT c;
8143: UINT **spmat;
8144: UINT *s0vect,*svect,*p,*v;
8145: int *colstat;
8146: IndArray *imat;
8147: int *rhead;
8148: int spcol,sprow;
8149: int sugar,sugarh;
8150: PGeoBucket bucket;
8151: struct oEGT eg0,eg1,eg_f4;
8152: Z i1,i2,sugarq;
1.12 noro 8153:
8154: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8155: #if 0
8156: ndv_alloc = 0;
8157: #endif
1.11 noro 8158: Nf4_red=0;
1.1 noro 8159: g = 0; d = 0;
8160: for ( i = 0; i < nd_psn; i++ ) {
8161: d = update_pairs(d,g,i,0);
8162: g = update_base(g,i);
8163: }
8164: nzlist = 0;
8165: nzlist_t = nd_nzlist;
8166: f4red = 1;
8167: nd_last_nonzero = 0;
8168: while ( d ) {
8169: get_eg(&eg0);
8170: l = nd_minsugarp(d,&d);
8171: sugar = nd_sugarweight?l->sugar2:SG(l);
8172: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8173: if ( nzlist_t ) {
8174: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8175: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8176: tn = BDY((LIST)ARG1(node));
8177: if ( !tn ) {
8178: nzlist_t = NEXT(nzlist_t);
8179: continue;
8180: }
8181: /* tn = [[i1,i2],...] */
8182: lh = nd_ipairtospair(tn);
8183: }
8184: bucket = create_pbucket();
8185: stat = nd_sp_f4(m,0,l,bucket);
8186: if ( !stat ) {
8187: for ( t = l; NEXT(t); t = NEXT(t) );
8188: NEXT(t) = d; d = l;
8189: d = nd_reconstruct(0,d);
8190: continue;
8191: }
8192: if ( bucket->m < 0 ) continue;
8193: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8194: if ( !col ) {
8195: for ( t = l; NEXT(t); t = NEXT(t) );
8196: NEXT(t) = d; d = l;
8197: d = nd_reconstruct(0,d);
8198: continue;
8199: }
1.12 noro 8200: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8201: if ( DP_Print )
1.6 noro 8202: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8203: sugar,eg_f4.exectime);
1.1 noro 8204: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8205: if ( checkonly && nflist ) return 0;
8206: /* adding new bases */
8207: if ( nflist ) nd_last_nonzero = f4red;
8208: for ( r = nflist; r; r = NEXT(r) ) {
8209: nf = (NDV)BDY(r);
1.38 noro 8210: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8211: ndv_removecont(m,nf);
8212: if ( !m && nd_nalg ) {
8213: ND nf1;
8214:
8215: nf1 = ndvtond(m,nf);
8216: nd_monic(0,&nf1);
8217: nd_removecont(m,nf1);
8218: nf = ndtondv(m,nf1);
8219: }
1.24 noro 8220: nh = ndv_newps(m,nf,0);
1.1 noro 8221: d = update_pairs(d,g,nh,0);
8222: g = update_base(g,nh);
8223: }
8224: if ( DP_Print ) {
8225: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8226: }
8227: if ( nd_gentrace ) {
8228: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8229: NEXTNODE(tn0,tn);
1.6 noro 8230: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8231: node = mknode(2,i1,i2); MKLIST(l0,node);
8232: BDY(tn) = l0;
8233: }
8234: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8235: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8236: MKNODE(node,l1,nzlist); nzlist = node;
8237: }
8238: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8239: f4red++;
8240: if ( nd_f4red && f4red > nd_f4red ) break;
8241: if ( nd_rank0 && !nflist ) break;
8242: }
8243: if ( nd_gentrace ) {
8244: MKLIST(l0,reverse_node(nzlist));
8245: MKNODE(nd_alltracelist,l0,0);
8246: }
8247: #if 0
8248: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8249: #endif
1.12 noro 8250: if ( DP_Print ) {
8251: fprintf(asir_out,"number of red=%d,",Nf4_red);
8252: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8253: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 ! noro 8254: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8255: }
1.1 noro 8256: conv_ilist(nd_demand,0,g,indp);
8257: return g;
8258: }
8259:
8260: NODE nd_f4_trace(int m,int **indp)
8261: {
8262: int i,nh,stat,index;
8263: NODE r,g;
8264: ND_pairs d,l,l0,t;
8265: ND spol,red;
8266: NDV nf,redv,nfqv,nfv;
8267: NM s0,s;
8268: NODE rp0,srp0,nflist;
8269: int nsp,nred,col,rank,len,k,j,a;
8270: UINT c;
8271: UINT **spmat;
8272: UINT *s0vect,*svect,*p,*v;
8273: int *colstat;
8274: IndArray *imat;
8275: int *rhead;
8276: int spcol,sprow;
8277: int sugar;
8278: PGeoBucket bucket;
8279: struct oEGT eg0,eg1,eg_f4;
8280:
8281: g = 0; d = 0;
8282: for ( i = 0; i < nd_psn; i++ ) {
8283: d = update_pairs(d,g,i,0);
8284: g = update_base(g,i);
8285: }
8286: while ( d ) {
8287: get_eg(&eg0);
8288: l = nd_minsugarp(d,&d);
8289: sugar = SG(l);
8290: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8291: bucket = create_pbucket();
8292: stat = nd_sp_f4(m,0,l,bucket);
8293: if ( !stat ) {
8294: for ( t = l; NEXT(t); t = NEXT(t) );
8295: NEXT(t) = d; d = l;
8296: d = nd_reconstruct(1,d);
8297: continue;
8298: }
8299: if ( bucket->m < 0 ) continue;
8300: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8301: if ( !col ) {
8302: for ( t = l; NEXT(t); t = NEXT(t) );
8303: NEXT(t) = d; d = l;
8304: d = nd_reconstruct(1,d);
8305: continue;
8306: }
8307: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8308: if ( DP_Print )
1.3 noro 8309: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8310: sugar,eg_f4.exectime);
1.1 noro 8311: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8312: if ( !l0 ) continue;
8313: l = l0;
8314:
8315: /* over Q */
8316: bucket = create_pbucket();
8317: stat = nd_sp_f4(0,1,l,bucket);
8318: if ( !stat ) {
8319: for ( t = l; NEXT(t); t = NEXT(t) );
8320: NEXT(t) = d; d = l;
8321: d = nd_reconstruct(1,d);
8322: continue;
8323: }
8324: if ( bucket->m < 0 ) continue;
8325: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8326: if ( !col ) {
8327: for ( t = l; NEXT(t); t = NEXT(t) );
8328: NEXT(t) = d; d = l;
8329: d = nd_reconstruct(1,d);
8330: continue;
8331: }
8332: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8333: /* adding new bases */
8334: for ( r = nflist; r; r = NEXT(r) ) {
8335: nfqv = (NDV)BDY(r);
8336: ndv_removecont(0,nfqv);
1.6 noro 8337: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8338: if ( nd_nalg ) {
8339: ND nf1;
8340:
8341: nf1 = ndvtond(m,nfqv);
8342: nd_monic(0,&nf1);
8343: nd_removecont(0,nf1);
8344: nfqv = ndtondv(0,nf1); nd_free(nf1);
8345: }
8346: nfv = ndv_dup(0,nfqv);
8347: ndv_mod(m,nfv);
8348: ndv_removecont(m,nfv);
1.24 noro 8349: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8350: d = update_pairs(d,g,nh,0);
8351: g = update_base(g,nh);
8352: }
8353: }
8354: #if 0
8355: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8356: #endif
8357: conv_ilist(nd_demand,1,g,indp);
8358: return g;
8359: }
8360:
8361: int rref(matrix mat,int *sugar)
8362: {
8363: int row,col,i,j,k,l,s,wcol,wj;
8364: unsigned long bj;
8365: unsigned long **a;
8366: unsigned long *ai,*ak,*as,*t;
8367: int *pivot;
8368:
8369: row = mat->row;
8370: col = mat->col;
8371: a = mat->a;
8372: wcol = (col+BLEN-1)/BLEN;
8373: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8374: i = 0;
8375: for ( j = 0; j < col; j++ ) {
8376: wj = j/BLEN; bj = 1L<<(j%BLEN);
8377: for ( k = i; k < row; k++ )
8378: if ( a[k][wj] & bj ) break;
8379: if ( k == row ) continue;
8380: pivot[i] = j;
8381: if ( k != i ) {
8382: t = a[i]; a[i] = a[k]; a[k] = t;
8383: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8384: }
8385: ai = a[i];
8386: for ( k = i+1; k < row; k++ ) {
8387: ak = a[k];
8388: if ( ak[wj] & bj ) {
8389: for ( l = wj; l < wcol; l++ )
8390: ak[l] ^= ai[l];
8391: sugar[k] = MAX(sugar[k],sugar[i]);
8392: }
8393: }
8394: i++;
8395: }
8396: for ( k = i-1; k >= 0; k-- ) {
8397: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8398: ak = a[k];
8399: for ( s = 0; s < k; s++ ) {
8400: as = a[s];
8401: if ( as[wj] & bj ) {
8402: for ( l = wj; l < wcol; l++ )
8403: as[l] ^= ak[l];
8404: sugar[s] = MAX(sugar[s],sugar[k]);
8405: }
8406: }
8407: }
8408: return i;
8409: }
8410:
8411: void print_matrix(matrix mat)
8412: {
8413: int row,col,i,j;
8414: unsigned long *ai;
8415:
8416: row = mat->row;
8417: col = mat->col;
8418: printf("%d x %d\n",row,col);
8419: for ( i = 0; i < row; i++ ) {
8420: ai = mat->a[i];
8421: for ( j = 0; j < col; j++ ) {
8422: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8423: else putchar('0');
8424: }
8425: putchar('\n');
8426: }
8427: }
8428:
8429: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8430:
8431: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8432: {
8433: int row,col,wcol,wj,i,j;
8434: unsigned long bj;
8435: unsigned long *ai;
8436: unsigned long **a;
8437: int len;
8438: int *pos;
8439:
8440: row = mat->row;
8441: col = mat->col;
8442: wcol = (col+BLEN-1)/BLEN;
8443: pos = (int *)MALLOC(wcol*sizeof(int));
8444: bzero(pos,wcol*sizeof(int));
8445: for ( i = j = 0; i < wcol; i++ )
8446: if ( v[i] ) pos[j++] = i;;
8447: len = j;
8448: wj = rhead/BLEN;
8449: bj = 1L<<rhead%BLEN;
8450: a = mat->a;
8451: for ( i = 0; i < row; i++ ) {
8452: ai = a[i];
8453: if ( ai[wj]&bj ) {
8454: for ( j = 0; j < len; j++ )
8455: ai[pos[j]] ^= v[pos[j]];
8456: sugar[i] = MAX(sugar[i],rsugar);
8457: }
8458: }
8459: }
8460:
8461: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8462: {
8463: int nsp,nred,i,i0,k,rank,row;
8464: NODE r0,rp;
8465: ND_pairs sp;
8466: ND spol;
8467: NM_ind_pair rt;
8468: int *s0hash;
8469: UINT *s;
8470: int *pivot,*sugar,*head;
8471: matrix mat;
8472: NM m;
8473: NODE r;
8474: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8475: int rhead,rsugar,size;
8476: unsigned long *v;
8477:
8478: get_eg(&eg0);
8479: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8480: nred = length(rp0);
8481: mat = alloc_matrix(nsp,col);
8482: s0hash = (int *)MALLOC(col*sizeof(int));
8483: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8484: s0hash[i] = ndl_hash_value(s);
8485:
8486: sugar = (int *)MALLOC(nsp*sizeof(int));
8487: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8488: nd_sp(2,0,sp,&spol);
8489: if ( spol ) {
8490: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8491: sugar[i] = SG(spol);
8492: i++;
8493: }
8494: }
8495: mat->row = i;
8496: if ( DP_Print ) {
8497: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8498: }
8499: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8500: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8501: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8502: rt = (NM_ind_pair)BDY(rp);
8503: bzero(v,size);
8504: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8505: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8506: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8507: }
8508:
8509: get_eg(&eg1);
8510: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8511: rank = rref(mat,sugar);
8512:
8513: for ( i = 0, r0 = 0; i < rank; i++ ) {
8514: NEXTNODE(r0,r);
8515: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8516: SG((NDV)BDY(r)) = sugar[i];
8517: }
8518: if ( r0 ) NEXT(r) = 0;
8519: get_eg(&eg2);
8520: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8521: if ( DP_Print ) {
8522: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8523: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8524: fflush(asir_out);
8525: }
8526: return r0;
8527: }
8528:
8529:
8530: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8531: {
8532: IndArray *imat;
1.11 noro 8533: int nsp,nred,i,start;
1.1 noro 8534: int *rhead;
8535: NODE r0,rp;
8536: ND_pairs sp;
8537: NM_ind_pair *rvect;
8538: UINT *s;
8539: int *s0hash;
1.11 noro 8540: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8541:
8542: if ( m == 2 && nd_rref2 )
8543: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8544:
8545: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8546: nred = length(rp0);
8547: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8548: rhead = (int *)MALLOC(col*sizeof(int));
8549: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8550:
8551: /* construction of index arrays */
1.11 noro 8552: get_eg(&eg0);
1.1 noro 8553: if ( DP_Print ) {
1.11 noro 8554: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8555: fflush(asir_out);
1.1 noro 8556: }
8557: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8558: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8559: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8560: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8561: rhead[imat[i]->head] = 1;
1.11 noro 8562: start = imat[i]->head;
8563: }
1.12 noro 8564: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8565: if ( DP_Print ) {
8566: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8567: fflush(asir_out);
1.1 noro 8568: }
8569: if ( m > 0 )
1.7 noro 8570: #if SIZEOF_LONG==8
1.1 noro 8571: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8572: #else
8573: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8574: #endif
8575: else if ( m == -1 )
8576: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8577: else if ( m == -2 )
8578: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8579: else
8580: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8581: return r0;
8582: }
8583:
8584: /* for Fp, 2<=p<2^16 */
8585:
8586: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8587: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8588: {
8589: int spcol,sprow,a;
8590: int i,j,k,l,rank;
8591: NODE r0,r;
8592: ND_pairs sp;
8593: ND spol;
8594: UINT **spmat;
8595: UINT *svect,*v;
8596: int *colstat;
8597: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8598: int maxrs;
8599: int *spsugar;
8600: ND_pairs *spactive;
8601:
8602: spcol = col-nred;
8603: get_eg(&eg0);
8604: /* elimination (1st step) */
8605: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8606: svect = (UINT *)MALLOC(col*sizeof(UINT));
8607: spsugar = (int *)MALLOC(nsp*sizeof(int));
8608: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8609: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8610: nd_sp(m,0,sp,&spol);
8611: if ( !spol ) continue;
8612: nd_to_vect(m,s0vect,col,spol,svect);
8613: if ( m == -1 )
8614: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8615: else
1.40 noro 8616: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 8617: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8618: if ( i < col ) {
8619: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8620: for ( j = k = 0; j < col; j++ )
8621: if ( !rhead[j] ) v[k++] = svect[j];
8622: spsugar[sprow] = MAX(maxrs,SG(spol));
8623: if ( nz )
8624: spactive[sprow] = sp;
8625: sprow++;
8626: }
8627: nd_free(spol);
8628: }
8629: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8630: if ( DP_Print ) {
1.5 noro 8631: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8632: fflush(asir_out);
8633: }
8634: /* free index arrays */
8635: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8636:
8637: /* elimination (2nd step) */
8638: colstat = (int *)MALLOC(spcol*sizeof(int));
8639: if ( m == -1 )
8640: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8641: else
8642: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8643: r0 = 0;
8644: for ( i = 0; i < rank; i++ ) {
8645: NEXTNODE(r0,r); BDY(r) =
8646: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8647: SG((NDV)BDY(r)) = spsugar[i];
8648: GCFREE(spmat[i]);
8649: }
8650: if ( r0 ) NEXT(r) = 0;
8651:
8652: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8653: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8654: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8655: if ( DP_Print ) {
1.5 noro 8656: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8657: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8658: nsp,nred,sprow,spcol,rank);
1.5 noro 8659: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8660: }
8661: if ( nz ) {
8662: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8663: if ( rank > 0 ) {
8664: NEXT(spactive[rank-1]) = 0;
8665: *nz = spactive[0];
8666: } else
8667: *nz = 0;
8668: }
8669: return r0;
8670: }
8671:
1.40 noro 8672: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8673: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
8674: {
8675: int spcol,sprow,a;
8676: int i,j,k,l,rank;
8677: NODE r0,r;
8678: ND_pairs sp;
8679: ND spol;
8680: UINT **spmat;
8681: UINT *svect,*cvect;
8682: UINT *v;
8683: int *colstat;
8684: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8685: int maxrs;
8686: int *spsugar;
8687: ND_pairs *spactive;
8688: SIG *spsig;
8689:
8690: get_eg(&eg0);
8691: /* elimination (1st step) */
8692: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8693: spsugar = (int *)MALLOC(nsp*sizeof(int));
8694: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
8695: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8696: nd_sp(m,0,sp,&spol);
8697: if ( !spol ) {
8698: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8699: continue;
8700: }
8701: svect = (UINT *)MALLOC(col*sizeof(UINT));
8702: nd_to_vect(m,s0vect,col,spol,svect);
8703: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
8704: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8705: if ( i < col ) {
8706: spmat[sprow] = svect;
8707: spsugar[sprow] = MAX(maxrs,SG(spol));
8708: spsig[sprow] = sp->sig;
8709: sprow++;
8710: } else {
8711: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8712: }
8713: nd_free(spol);
8714: }
8715: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
8716: if ( DP_Print ) {
8717: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
8718: fflush(asir_out);
8719: }
8720: /* free index arrays */
8721: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8722:
8723: /* elimination (2nd step) */
8724: colstat = (int *)MALLOC(col*sizeof(int));
8725: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
8726: r0 = 0;
8727: for ( i = 0; i < sprow; i++ ) {
8728: if ( spsugar[i] >= 0 ) {
8729: NEXTNODE(r0,r);
8730: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
8731: SG((NDV)BDY(r)) = spsugar[i];
8732: ((NDV)BDY(r))->sig = spsig[i];
8733: } else
8734: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
8735: GCFREE(spmat[i]);
8736: }
8737: if ( r0 ) NEXT(r) = 0;
8738: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
8739: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8740: if ( DP_Print ) {
8741: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
8742: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8743: nsp,nred,sprow,col,rank);
8744: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
8745: }
8746: return r0;
8747: }
8748:
1.1 noro 8749:
8750: /* for small finite fields */
8751:
8752: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8753: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8754: {
8755: int spcol,sprow,a;
8756: int i,j,k,l,rank;
8757: NODE r0,r;
8758: ND_pairs sp;
8759: ND spol;
8760: UINT **spmat;
8761: UINT *svect,*v;
8762: int *colstat;
8763: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8764: int maxrs;
8765: int *spsugar;
8766: ND_pairs *spactive;
8767:
8768: spcol = col-nred;
8769: get_eg(&eg0);
8770: /* elimination (1st step) */
8771: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8772: svect = (UINT *)MALLOC(col*sizeof(UINT));
8773: spsugar = (int *)MALLOC(nsp*sizeof(int));
8774: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8775: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8776: nd_sp(m,0,sp,&spol);
8777: if ( !spol ) continue;
8778: nd_to_vect(m,s0vect,col,spol,svect);
8779: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8780: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8781: if ( i < col ) {
8782: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8783: for ( j = k = 0; j < col; j++ )
8784: if ( !rhead[j] ) v[k++] = svect[j];
8785: spsugar[sprow] = MAX(maxrs,SG(spol));
8786: if ( nz )
8787: spactive[sprow] = sp;
8788: sprow++;
8789: }
8790: nd_free(spol);
8791: }
8792: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8793: if ( DP_Print ) {
1.5 noro 8794: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8795: fflush(asir_out);
8796: }
8797: /* free index arrays */
8798: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8799:
8800: /* elimination (2nd step) */
8801: colstat = (int *)MALLOC(spcol*sizeof(int));
8802: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8803: r0 = 0;
8804: for ( i = 0; i < rank; i++ ) {
8805: NEXTNODE(r0,r); BDY(r) =
8806: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8807: SG((NDV)BDY(r)) = spsugar[i];
8808: GCFREE(spmat[i]);
8809: }
8810: if ( r0 ) NEXT(r) = 0;
8811:
8812: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8813: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8814: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8815: if ( DP_Print ) {
1.5 noro 8816: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8817: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8818: nsp,nred,sprow,spcol,rank);
1.5 noro 8819: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8820: }
8821: if ( nz ) {
8822: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8823: if ( rank > 0 ) {
8824: NEXT(spactive[rank-1]) = 0;
8825: *nz = spactive[0];
8826: } else
8827: *nz = 0;
8828: }
8829: return r0;
8830: }
8831:
8832: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8833: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8834: {
8835: int spcol,sprow,a;
8836: int i,j,k,l,rank;
8837: NODE r0,r;
8838: ND_pairs sp;
8839: ND spol;
8840: mpz_t **spmat;
8841: mpz_t *svect,*v;
8842: int *colstat;
8843: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8844: int maxrs;
8845: int *spsugar;
8846: pointer *w;
8847:
8848: spcol = col-nred;
8849: get_eg(&eg0);
8850: /* elimination (1st step) */
8851: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8852: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8853: spsugar = (int *)MALLOC(nsp*sizeof(int));
8854: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8855: nd_sp(m,trace,sp,&spol);
8856: if ( !spol ) continue;
8857: nd_to_vect_lf(s0vect,col,spol,svect);
8858: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8859: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8860: if ( i < col ) {
8861: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8862: for ( j = k = 0; j < col; j++ )
8863: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8864: spsugar[sprow] = MAX(maxrs,SG(spol));
8865: sprow++;
8866: }
8867: /* nd_free(spol); */
8868: }
8869: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8870: if ( DP_Print ) {
1.5 noro 8871: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8872: fflush(asir_out);
8873: }
8874: /* free index arrays */
8875: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8876:
8877: /* elimination (2nd step) */
8878: colstat = (int *)MALLOC(spcol*sizeof(int));
8879: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8880: w = (pointer *)MALLOC(rank*sizeof(pointer));
8881: for ( i = 0; i < rank; i++ ) {
8882: #if 0
8883: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8884: SG((NDV)w[rank-i-1]) = spsugar[i];
8885: #else
8886: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8887: SG((NDV)w[i]) = spsugar[i];
8888: #endif
8889: /* GCFREE(spmat[i]); */
8890:
8891: }
8892: #if 0
8893: qsort(w,rank,sizeof(NDV),
8894: (int (*)(const void *,const void *))ndv_compare);
8895: #endif
8896: r0 = 0;
8897: for ( i = 0; i < rank; i++ ) {
8898: NEXTNODE(r0,r); BDY(r) = w[i];
8899: }
8900: if ( r0 ) NEXT(r) = 0;
8901:
8902: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8903: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8904: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8905: if ( DP_Print ) {
1.5 noro 8906: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8907: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8908: nsp,nred,sprow,spcol,rank);
1.5 noro 8909: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8910: }
8911: return r0;
8912: }
8913:
8914: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8915: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8916: {
8917: int spcol,sprow,a;
8918: int i,j,k,l,rank;
8919: NODE r0,r;
8920: ND_pairs sp;
8921: ND spol;
8922: Z **spmat;
8923: Z *svect,*v;
8924: int *colstat;
8925: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8926: int maxrs;
8927: int *spsugar;
8928: pointer *w;
8929:
8930: spcol = col-nred;
8931: get_eg(&eg0);
8932: /* elimination (1st step) */
8933: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8934: svect = (Z *)MALLOC(col*sizeof(Q));
8935: spsugar = (int *)MALLOC(nsp*sizeof(int));
8936: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8937: nd_sp(0,trace,sp,&spol);
8938: if ( !spol ) continue;
8939: nd_to_vect_q(s0vect,col,spol,svect);
8940: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8941: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8942: if ( i < col ) {
8943: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8944: for ( j = k = 0; j < col; j++ )
8945: if ( !rhead[j] ) v[k++] = svect[j];
8946: spsugar[sprow] = MAX(maxrs,SG(spol));
8947: sprow++;
8948: }
8949: /* nd_free(spol); */
8950: }
8951: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8952: if ( DP_Print ) {
1.5 noro 8953: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8954: fflush(asir_out);
8955: }
8956: /* free index arrays */
8957: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8958:
8959: /* elimination (2nd step) */
8960: colstat = (int *)MALLOC(spcol*sizeof(int));
8961: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8962: w = (pointer *)MALLOC(rank*sizeof(pointer));
8963: for ( i = 0; i < rank; i++ ) {
8964: #if 0
8965: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8966: SG((NDV)w[rank-i-1]) = spsugar[i];
8967: #else
8968: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8969: SG((NDV)w[i]) = spsugar[i];
8970: #endif
8971: /* GCFREE(spmat[i]); */
8972: }
8973: #if 0
8974: qsort(w,rank,sizeof(NDV),
8975: (int (*)(const void *,const void *))ndv_compare);
8976: #endif
8977: r0 = 0;
8978: for ( i = 0; i < rank; i++ ) {
8979: NEXTNODE(r0,r); BDY(r) = w[i];
8980: }
8981: if ( r0 ) NEXT(r) = 0;
8982:
8983: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8984: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8985: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8986: if ( DP_Print ) {
1.5 noro 8987: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8988: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8989: nsp,nred,sprow,spcol,rank);
1.5 noro 8990: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8991: }
8992: return r0;
8993: }
8994:
8995: FILE *nd_write,*nd_read;
8996:
8997: void nd_send_int(int a) {
8998: write_int(nd_write,(unsigned int *)&a);
8999: }
9000:
9001: void nd_send_intarray(int *p,int len) {
9002: write_intarray(nd_write,(unsigned int *)p,len);
9003: }
9004:
9005: int nd_recv_int() {
9006: int a;
9007:
9008: read_int(nd_read,(unsigned int *)&a);
9009: return a;
9010: }
9011:
9012: void nd_recv_intarray(int *p,int len) {
9013: read_intarray(nd_read,(unsigned int *)p,len);
9014: }
9015:
9016: void nd_send_ndv(NDV p) {
9017: int len,i;
9018: NMV m;
9019:
9020: if ( !p ) nd_send_int(0);
9021: else {
9022: len = LEN(p);
9023: nd_send_int(len);
9024: m = BDY(p);
9025: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9026: nd_send_int(CM(m));
9027: nd_send_intarray((int *)DL(m),nd_wpd);
9028: }
9029: }
9030: }
9031:
9032: void nd_send_nd(ND p) {
9033: int len,i;
9034: NM m;
9035:
9036: if ( !p ) nd_send_int(0);
9037: else {
9038: len = LEN(p);
9039: nd_send_int(len);
9040: m = BDY(p);
9041: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9042: nd_send_int(CM(m));
9043: nd_send_intarray((int *)DL(m),nd_wpd);
9044: }
9045: }
9046: }
9047:
9048: NDV nd_recv_ndv()
9049: {
9050: int len,i;
9051: NMV m,m0;
9052: NDV r;
9053:
9054: len = nd_recv_int();
9055: if ( !len ) return 0;
9056: else {
9057: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9058: #if 0
9059: ndv_alloc += len*nmv_adv;
9060: #endif
9061: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9062: CM(m) = nd_recv_int();
9063: nd_recv_intarray((int *)DL(m),nd_wpd);
9064: }
9065: MKNDV(nd_nvar,m0,len,r);
9066: return r;
9067: }
9068: }
9069:
9070: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9071: {
9072: int i,j,t,c,rank,inv;
9073: int *ci,*ri;
9074: Z dn;
9075: MAT m,nm;
9076:
9077: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9078: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9079: for ( i = 0; i < row; i++ )
9080: for ( j = 0; j < col; j++ )
9081: mat0[i][j] = 0;
9082: c = col-rank;
9083: for ( i = 0; i < rank; i++ ) {
9084: mat0[i][ri[i]] = dn;
9085: for ( j = 0; j < c; j++ )
9086: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9087: }
9088: return rank;
9089: }
9090:
9091: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9092: {
9093: int i,j,k,l,inv,a,rank,s;
9094: unsigned int *t,*pivot,*pk;
9095: unsigned int **mat;
9096: ND_pairs pair;
9097:
9098: mat = (unsigned int **)mat0;
9099: for ( rank = 0, j = 0; j < col; j++ ) {
9100: for ( i = rank; i < row; i++ )
9101: mat[i][j] %= md;
9102: for ( i = rank; i < row; i++ )
9103: if ( mat[i][j] )
9104: break;
9105: if ( i == row ) {
9106: colstat[j] = 0;
9107: continue;
9108: } else
9109: colstat[j] = 1;
9110: if ( i != rank ) {
9111: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9112: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9113: if ( spactive ) {
9114: pair = spactive[i]; spactive[i] = spactive[rank];
9115: spactive[rank] = pair;
9116: }
9117: }
9118: pivot = mat[rank];
9119: s = sugar[rank];
9120: inv = invm(pivot[j],md);
9121: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9122: if ( *pk ) {
9123: if ( *pk >= (unsigned int)md )
9124: *pk %= md;
9125: DMAR(*pk,inv,0,md,*pk)
9126: }
9127: for ( i = rank+1; i < row; i++ ) {
9128: t = mat[i];
9129: if ( (a = t[j]) != 0 ) {
9130: sugar[i] = MAX(sugar[i],s);
9131: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9132: }
9133: }
9134: rank++;
9135: }
9136: for ( j = col-1, l = rank-1; j >= 0; j-- )
9137: if ( colstat[j] ) {
9138: pivot = mat[l];
9139: s = sugar[l];
9140: for ( i = 0; i < l; i++ ) {
9141: t = mat[i];
9142: t[j] %= md;
9143: if ( (a = t[j]) != 0 ) {
9144: sugar[i] = MAX(sugar[i],s);
9145: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9146: }
9147: }
9148: l--;
9149: }
9150: for ( j = 0, l = 0; l < rank; j++ )
9151: if ( colstat[j] ) {
9152: t = mat[l];
9153: for ( k = j; k < col; k++ )
9154: if ( t[k] >= (unsigned int)md )
9155: t[k] %= md;
9156: l++;
9157: }
9158: return rank;
9159: }
9160:
1.40 noro 9161: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9162: {
9163: int i,j,k,l,rank,s,imin;
9164: UINT inv;
9165: UINT a;
9166: UINT *t,*pivot,*pk;
9167: UINT *ck;
9168: UINT *ct;
9169: ND_pairs pair;
9170: SIG sg;
9171: int *used;
9172:
9173: used = (int *)MALLOC(row*sizeof(int));
9174: for ( j = 0; j < col; j++ ) {
9175: for ( i = 0; i < row; i++ )
9176: a = mat[i][j] %= md;
9177: for ( i = 0; i < row; i++ )
9178: if ( !used[i] && mat[i][j] ) break;
9179: if ( i == row ) {
9180: colstat[j] = 0;
9181: continue;
9182: } else {
9183: colstat[j] = 1;
9184: used[i] = 1;
9185: }
9186: /* column j is normalized */
9187: s = sugar[i];
9188: inv = invm(mat[i][j],md);
9189: /* normalize pivot row */
9190: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9191: DMAR(*pk,inv,0,md,*pk);
9192: }
9193: for ( k = i+1; k < row; k++ ) {
9194: if ( (a = mat[k][j]) != 0 ) {
9195: sugar[k] = MAX(sugar[k],s);
9196: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9197: Nf4_red++;
9198: }
9199: }
9200: }
9201: rank = 0;
9202: for ( i = 0; i < row; i++ ) {
9203: for ( j = 0; j < col; j++ )
9204: if ( mat[i][j] ) break;
9205: if ( j == col ) sugar[i] = -1;
9206: else rank++;
9207: }
9208: return rank;
9209: }
9210:
1.1 noro 9211:
1.7 noro 9212: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9213: {
1.7 noro 9214: int i,j,k,l,inv,a,rank,s;
9215: unsigned int *t,*pivot,*pk;
9216: unsigned int **mat;
9217:
9218: mat = (unsigned int **)mat0;
9219: for ( rank = 0, j = 0; j < col; j++ ) {
9220: for ( i = rank; i < row; i++ )
9221: if ( mat[i][j] )
9222: break;
9223: if ( i == row ) {
9224: colstat[j] = 0;
9225: continue;
9226: } else
9227: colstat[j] = 1;
9228: if ( i != rank ) {
9229: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9230: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9231: }
9232: pivot = mat[rank];
9233: s = sugar[rank];
9234: inv = _invsf(pivot[j]);
9235: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9236: if ( *pk )
9237: *pk = _mulsf(*pk,inv);
9238: for ( i = rank+1; i < row; i++ ) {
9239: t = mat[i];
9240: if ( (a = t[j]) != 0 ) {
9241: sugar[i] = MAX(sugar[i],s);
9242: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9243: }
9244: }
9245: rank++;
9246: }
9247: for ( j = col-1, l = rank-1; j >= 0; j-- )
9248: if ( colstat[j] ) {
9249: pivot = mat[l];
9250: s = sugar[l];
9251: for ( i = 0; i < l; i++ ) {
9252: t = mat[i];
9253: if ( (a = t[j]) != 0 ) {
9254: sugar[i] = MAX(sugar[i],s);
9255: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9256: }
9257: }
9258: l--;
9259: }
9260: return rank;
9261: }
1.1 noro 9262:
1.7 noro 9263: int ndv_ishomo(NDV p)
9264: {
9265: NMV m;
9266: int len,h;
1.1 noro 9267:
9268: if ( !p ) return 1;
9269: len = LEN(p);
9270: m = BDY(p);
9271: h = TD(DL(m));
9272: NMV_ADV(m);
9273: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9274: if ( TD(DL(m)) != h ) {
9275: return 0;
9276: }
1.1 noro 9277: return 1;
9278: }
9279:
9280: void ndv_save(NDV p,int index)
9281: {
9282: FILE *s;
9283: char name[BUFSIZ];
9284: short id;
9285: int nv,sugar,len,n,i,td,e,j;
9286: NMV m;
9287: unsigned int *dl;
9288: int mpos;
9289:
9290: sprintf(name,"%s/%d",Demand,index);
9291: s = fopen(name,"w");
9292: savevl(s,0);
9293: if ( !p ) {
9294: saveobj(s,0);
9295: return;
9296: }
9297: id = O_DP;
9298: nv = NV(p);
9299: sugar = SG(p);
9300: len = LEN(p);
9301: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9302: write_int(s,(unsigned int *)&len);
9303:
9304: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9305: saveobj(s,(Obj)CZ(m));
1.1 noro 9306: dl = DL(m);
9307: td = TD(dl);
9308: write_int(s,(unsigned int *)&td);
9309: for ( j = 0; j < nv; j++ ) {
9310: e = GET_EXP(dl,j);
9311: write_int(s,(unsigned int *)&e);
9312: }
9313: if ( nd_module ) {
9314: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
9315: }
9316: }
9317: fclose(s);
9318: }
9319:
9320: void nd_save_mod(ND p,int index)
9321: {
9322: FILE *s;
9323: char name[BUFSIZ];
9324: int nv,sugar,len,c;
9325: NM m;
9326:
9327: sprintf(name,"%s/%d",Demand,index);
9328: s = fopen(name,"w");
9329: if ( !p ) {
9330: len = 0;
9331: write_int(s,(unsigned int *)&len);
9332: fclose(s);
9333: return;
9334: }
9335: nv = NV(p);
9336: sugar = SG(p);
9337: len = LEN(p);
9338: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9339: for ( m = BDY(p); m; m = NEXT(m) ) {
9340: c = CM(m); write_int(s,(unsigned int *)&c);
9341: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9342: }
9343: fclose(s);
9344: }
9345:
9346: NDV ndv_load(int index)
9347: {
9348: FILE *s;
9349: char name[BUFSIZ];
9350: short id;
9351: int nv,sugar,len,n,i,td,e,j;
9352: NDV d;
9353: NMV m0,m;
9354: unsigned int *dl;
9355: Obj obj;
9356: int mpos;
9357:
9358: sprintf(name,"%s/%d",Demand,index);
9359: s = fopen(name,"r");
9360: if ( !s ) return 0;
9361:
9362: skipvl(s);
9363: read_short(s,(unsigned short *)&id);
9364: if ( !id ) return 0;
9365: read_int(s,(unsigned int *)&nv);
9366: read_int(s,(unsigned int *)&sugar);
9367: read_int(s,(unsigned int *)&len);
9368:
9369: m0 = m = MALLOC(len*nmv_adv);
9370: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9371: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9372: dl = DL(m);
9373: ndl_zero(dl);
9374: read_int(s,(unsigned int *)&td); TD(dl) = td;
9375: for ( j = 0; j < nv; j++ ) {
9376: read_int(s,(unsigned int *)&e);
9377: PUT_EXP(dl,j,e);
9378: }
9379: if ( nd_module ) {
9380: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9381: }
9382: if ( nd_blockmask ) ndl_weight_mask(dl);
9383: }
9384: fclose(s);
9385: MKNDV(nv,m0,len,d);
9386: SG(d) = sugar;
9387: return d;
9388: }
9389:
9390: ND nd_load_mod(int index)
9391: {
9392: FILE *s;
9393: char name[BUFSIZ];
9394: int nv,sugar,len,i,c;
9395: ND d;
9396: NM m0,m;
9397:
9398: sprintf(name,"%s/%d",Demand,index);
9399: s = fopen(name,"r");
9400: /* if the file does not exist, it means p[index]=0 */
9401: if ( !s ) return 0;
9402:
9403: read_int(s,(unsigned int *)&nv);
9404: if ( !nv ) { fclose(s); return 0; }
9405:
9406: read_int(s,(unsigned int *)&sugar);
9407: read_int(s,(unsigned int *)&len);
9408: for ( m0 = 0, i = 0; i < len; i++ ) {
9409: NEXTNM(m0,m);
9410: read_int(s,(unsigned int *)&c); CM(m) = c;
9411: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9412: }
9413: NEXT(m) = 0;
9414: MKND(nv,m0,len,d);
9415: SG(d) = sugar;
9416: fclose(s);
9417: return d;
9418: }
9419:
9420: void nd_det(int mod,MAT f,P *rp)
9421: {
9422: VL fv,tv;
9423: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9424: pointer **m;
9425: P **w;
9426: P mp,r;
9427: NDV **dm;
9428: NDV *t,*mi,*mj;
9429: NDV d,s,mij,mjj;
9430: ND u;
9431: NMV nmv;
9432: UINT *bound;
9433: PGeoBucket bucket;
9434: struct order_spec *ord;
9435: Z dq,dt,ds;
9436: Z mone;
9437: Z gn,qn,dn0,nm,dn;
9438:
9439: create_order_spec(0,0,&ord);
9440: nd_init_ord(ord);
9441: get_vars((Obj)f,&fv);
9442: if ( f->row != f->col )
9443: error("nd_det : non-square matrix");
9444: n = f->row;
9445: m = f->body;
9446: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9447:
9448: if ( !nvar ) {
9449: if ( !mod )
9450: detp(CO,(P **)m,n,rp);
9451: else {
9452: w = (P **)almat_pointer(n,n);
9453: for ( i = 0; i < n; i++ )
9454: for ( j = 0; j < n; j++ )
9455: ptomp(mod,(P)m[i][j],&w[i][j]);
9456: detmp(CO,mod,w,n,&mp);
9457: mptop(mp,rp);
9458: }
9459: return;
9460: }
9461:
9462: if ( !mod ) {
9463: w = (P **)almat_pointer(n,n);
9464: dq = ONE;
9465: for ( i = 0; i < n; i++ ) {
9466: dn0 = ONE;
9467: for ( j = 0; j < n; j++ ) {
9468: if ( !m[i][j] ) continue;
9469: lgp(m[i][j],&nm,&dn);
1.6 noro 9470: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9471: }
9472: if ( !UNIZ(dn0) ) {
9473: ds = dn0;
9474: for ( j = 0; j < n; j++ )
9475: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9476: mulz(dq,ds,&dt); dq = dt;
9477: } else
9478: for ( j = 0; j < n; j++ )
9479: w[i][j] = (P)m[i][j];
9480: }
9481: m = (pointer **)w;
9482: }
9483:
9484: for ( i = 0, max = 1; i < n; i++ )
9485: for ( j = 0; j < n; j++ )
9486: for ( tv = fv; tv; tv = NEXT(tv) ) {
9487: e = getdeg(tv->v,(P)m[i][j]);
9488: max = MAX(e,max);
9489: }
9490: nd_setup_parameters(nvar,max);
9491: dm = (NDV **)almat_pointer(n,n);
9492: for ( i = 0, max = 1; i < n; i++ )
9493: for ( j = 0; j < n; j++ ) {
9494: dm[i][j] = ptondv(CO,fv,m[i][j]);
9495: if ( mod ) ndv_mod(mod,dm[i][j]);
9496: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9497: }
9498: d = ptondv(CO,fv,(P)ONE);
9499: if ( mod ) ndv_mod(mod,d);
9500: chsgnz(ONE,&mone);
9501: for ( j = 0, sgn = 1; j < n; j++ ) {
9502: if ( DP_Print ) {
9503: fprintf(asir_out,".");
9504: }
9505: for ( i = j; i < n && !dm[i][j]; i++ );
9506: if ( i == n ) {
9507: *rp = 0;
9508: return;
9509: }
9510: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9511: for ( k = j; k < n; k++ )
9512: for ( l = j; l < n; l++ )
9513: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9514: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9515: }
9516: if ( k0 != j ) {
9517: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9518: sgn = -sgn;
9519: }
9520: if ( l0 != j ) {
9521: for ( k = j; k < n; k++ ) {
9522: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9523: }
9524: sgn = -sgn;
9525: }
9526: bound = nd_det_compute_bound(dm,n,j);
9527: for ( k = 0; k < nd_nvar; k++ )
9528: if ( bound[k]*2 > nd_mask0 ) break;
9529: if ( k < nd_nvar )
9530: nd_det_reconstruct(dm,n,j,d);
9531:
9532: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9533: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9534: mi = dm[i]; mij = mi[j];
9535: if ( mod )
9536: ndv_mul_c(mod,mij,mod-1);
9537: else
9538: ndv_mul_c_q(mij,mone);
9539: for ( k = j+1; k < n; k++ ) {
9540: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9541: bucket = create_pbucket();
9542: if ( mi[k] ) {
9543: nmv = BDY(mjj); len = LEN(mjj);
9544: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9545: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9546: add_pbucket(mod,bucket,u);
9547: }
9548: }
9549: if ( mj[k] && mij ) {
9550: nmv = BDY(mij); len = LEN(mij);
9551: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9552: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9553: add_pbucket(mod,bucket,u);
9554: }
9555: }
9556: u = nd_quo(mod,bucket,d);
9557: mi[k] = ndtondv(mod,u);
9558: }
9559: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9560: }
9561: d = mjj;
9562: }
9563: if ( DP_Print ) {
9564: fprintf(asir_out,"\n");
9565: }
9566: if ( sgn < 0 ) {
9567: if ( mod )
9568: ndv_mul_c(mod,d,mod-1);
9569: else
9570: ndv_mul_c_q(d,mone);
9571: }
9572: r = ndvtop(mod,CO,fv,d);
9573: if ( !mod && !UNIQ(dq) )
9574: divsp(CO,r,(P)dq,rp);
9575: else
9576: *rp = r;
9577: }
9578:
9579: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9580: {
9581: NM mr,mr0;
9582: NM tnm;
9583: NMV m;
9584: UINT *d0,*dt,*dm;
9585: int c,n,td,i,c1,c2,len;
9586: Z q;
9587: ND r;
9588:
9589: if ( !p ) return 0;
9590: else {
9591: n = NV(p); m = BDY(p); len = LEN(p);
9592: d0 = DL(m0);
9593: td = TD(d);
9594: mr0 = 0;
9595: NEWNM(tnm);
9596: if ( mod ) {
9597: c = CM(m0);
9598: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9599: ndl_add(DL(m),d0,DL(tnm));
9600: if ( ndl_reducible(DL(tnm),d) ) {
9601: NEXTNM(mr0,mr);
9602: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9603: ndl_copy(DL(tnm),DL(mr));
9604: }
9605: }
9606: } else {
1.6 noro 9607: q = CZ(m0);
1.1 noro 9608: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9609: ndl_add(DL(m),d0,DL(tnm));
9610: if ( ndl_reducible(DL(tnm),d) ) {
9611: NEXTNM(mr0,mr);
1.6 noro 9612: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9613: ndl_copy(DL(tnm),DL(mr));
9614: }
9615: }
9616: }
9617: if ( !mr0 )
9618: return 0;
9619: else {
9620: NEXT(mr) = 0;
9621: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9622: MKND(NV(p),mr0,len,r);
9623: SG(r) = SG(p) + TD(d0);
9624: return r;
9625: }
9626: }
9627: }
9628:
9629: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9630: {
9631: int i,obpe,oadv,h,k,l;
9632: static NM prev_nm_free_list;
9633: EPOS oepos;
9634:
9635: obpe = nd_bpe;
9636: oadv = nmv_adv;
9637: oepos = nd_epos;
9638: if ( obpe < 2 ) nd_bpe = 2;
9639: else if ( obpe < 3 ) nd_bpe = 3;
9640: else if ( obpe < 4 ) nd_bpe = 4;
9641: else if ( obpe < 5 ) nd_bpe = 5;
9642: else if ( obpe < 6 ) nd_bpe = 6;
9643: else if ( obpe < 8 ) nd_bpe = 8;
9644: else if ( obpe < 10 ) nd_bpe = 10;
9645: else if ( obpe < 16 ) nd_bpe = 16;
9646: else if ( obpe < 32 ) nd_bpe = 32;
9647: else error("nd_det_reconstruct : exponent too large");
9648:
9649: nd_setup_parameters(nd_nvar,0);
9650: prev_nm_free_list = _nm_free_list;
9651: _nm_free_list = 0;
9652: for ( k = j; k < n; k++ )
9653: for (l = j; l < n; l++ )
9654: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9655: ndv_realloc(d,obpe,oadv,oepos);
9656: prev_nm_free_list = 0;
9657: #if 0
9658: GC_gcollect();
9659: #endif
9660: }
9661:
9662: /* returns a UINT array containing degree bounds */
9663:
9664: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9665: {
9666: UINT *d0,*d1,*d,*t,*r;
9667: int k,l,i;
9668:
9669: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9670: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9671: for ( k = j; k < n; k++ )
9672: for ( l = j; l < n; l++ )
9673: if ( dm[k][l] ) {
9674: d = ndv_compute_bound(dm[k][l]);
9675: for ( i = 0; i < nd_nvar; i++ )
9676: d0[i] = MAX(d0[i],d[i]);
9677: }
9678: return d0;
9679: }
9680:
9681: DL nd_separate_d(UINT *d,UINT *trans)
9682: {
9683: int n,td,i,e,j;
9684: DL a;
9685:
9686: ndl_zero(trans);
9687: td = 0;
9688: for ( i = 0; i < nd_ntrans; i++ ) {
9689: e = GET_EXP(d,i);
9690: PUT_EXP(trans,i,e);
9691: td += MUL_WEIGHT(e,i);
9692: }
9693: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9694: /* homogenized */
9695: i = nd_nvar-1;
9696: e = GET_EXP(d,i);
9697: PUT_EXP(trans,i,e);
9698: td += MUL_WEIGHT(e,i);
9699: }
9700: TD(trans) = td;
9701: if ( nd_blockmask) ndl_weight_mask(trans);
9702: NEWDL(a,nd_nalg);
9703: td = 0;
9704: for ( i = 0; i < nd_nalg; i++ ) {
9705: j = nd_ntrans+i;
9706: e = GET_EXP(d,j);
9707: a->d[i] = e;
9708: td += e;
9709: }
9710: a->td = td;
9711: return a;
9712: }
9713:
9714: int nd_monic(int mod,ND *p)
9715: {
9716: UINT *trans,*t;
9717: DL alg;
9718: MP mp0,mp;
9719: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9720: ND r;
9721: DL dl;
9722: DP nm;
9723: NDV ndv;
9724: DAlg inv,cd;
9725: ND s,c;
9726: Z l,mul;
9727: Z ln;
9728: int n,ntrans,i,e,td,is_lc,len;
9729: NumberField nf;
9730: struct oEGT eg0,eg1;
9731:
9732: if ( !(nf = get_numberfield()) )
9733: error("nd_monic : current_numberfield is not set");
9734:
9735: /* Q coef -> DAlg coef */
9736: NEWNM(ma0); ma = ma0;
9737: m = BDY(*p);
9738: is_lc = 1;
9739: while ( 1 ) {
9740: NEWMP(mp0); mp = mp0;
1.6 noro 9741: mp->c = (Obj)CZ(m);
1.1 noro 9742: mp->dl = nd_separate_d(DL(m),DL(ma));
9743: NEWNM(mb);
9744: for ( m = NEXT(m); m; m = NEXT(m) ) {
9745: alg = nd_separate_d(DL(m),DL(mb));
9746: if ( !ndl_equal(DL(ma),DL(mb)) )
9747: break;
1.6 noro 9748: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9749: }
9750: NEXT(mp) = 0;
9751: MKDP(nd_nalg,mp0,nm);
9752: MKDAlg(nm,ONE,cd);
9753: if ( is_lc == 1 ) {
9754: /* if the lc is a rational number, we have nothing to do */
9755: if ( !mp0->dl->td )
9756: return 1;
9757:
9758: get_eg(&eg0);
9759: invdalg(cd,&inv);
9760: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9761: /* check the validity of inv */
9762: if ( mod && !remqi((Q)inv->dn,mod) )
9763: return 0;
9764: CA(ma) = nf->one;
9765: is_lc = 0;
9766: ln = ONE;
9767: } else {
9768: muldalg(cd,inv,&CA(ma));
9769: lcmz(ln,CA(ma)->dn,&ln);
9770: }
9771: if ( m ) {
9772: NEXT(ma) = mb; ma = mb;
9773: } else {
9774: NEXT(ma) = 0;
9775: break;
9776: }
9777: }
9778: /* l = lcm(denoms) */
9779: l = ln;
9780: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9781: divsz(l,CA(m)->dn,&mul);
1.1 noro 9782: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9783: NEXTNM(mr0,mr);
1.6 noro 9784: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9785: dl = mp->dl;
9786: td = TD(DL(m));
9787: ndl_copy(DL(m),DL(mr));
9788: for ( i = 0; i < nd_nalg; i++ ) {
9789: e = dl->d[i];
9790: PUT_EXP(DL(mr),i+nd_ntrans,e);
9791: td += MUL_WEIGHT(e,i+nd_ntrans);
9792: }
9793: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9794: TD(DL(mr)) = td;
9795: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9796: }
9797: }
9798: NEXT(mr) = 0;
9799: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9800: MKND(NV(*p),mr0,len,r);
9801: /* XXX */
9802: SG(r) = SG(*p);
9803: nd_free(*p);
9804: *p = r;
9805: return 1;
9806: }
9807:
9808: NODE reverse_node(NODE n)
9809: {
9810: NODE t,t1;
9811:
9812: for ( t = 0; n; n = NEXT(n) ) {
9813: MKNODE(t1,BDY(n),t); t = t1;
9814: }
9815: return t;
9816: }
9817:
9818: P ndc_div(int mod,union oNDC a,union oNDC b)
9819: {
9820: union oNDC c;
9821: int inv,t;
9822:
9823: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9824: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9825: else if ( mod ) {
9826: inv = invm(b.m,mod);
9827: DMAR(a.m,inv,0,mod,t); c.m = t;
9828: } else if ( nd_vc )
9829: divsp(nd_vc,a.p,b.p,&c.p);
9830: else
9831: divsz(a.z,b.z,&c.z);
9832: return ndctop(mod,c);
9833: }
9834:
9835: P ndctop(int mod,union oNDC c)
9836: {
9837: Z q;
9838: int e;
9839: GFS gfs;
9840:
9841: if ( mod == -1 ) {
9842: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9843: } else if ( mod == -2 ) {
1.10 noro 9844: q = c.z; return (P)q;
1.1 noro 9845: } else if ( mod > 0 ) {
1.6 noro 9846: STOZ(c.m,q); return (P)q;
1.1 noro 9847: } else
9848: return (P)c.p;
9849: }
9850:
9851: /* [0,0,0,cont] = p -> p/cont */
9852:
9853: void finalize_tracelist(int i,P cont)
9854: {
9855: LIST l;
9856: NODE node;
9857: Z iq;
9858:
9859: if ( !UNIQ(cont) ) {
9860: node = mknode(4,NULLP,NULLP,NULLP,cont);
9861: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9862: nd_tracelist = node;
9863: }
1.6 noro 9864: STOZ(i,iq);
1.1 noro 9865: nd_tracelist = reverse_node(nd_tracelist);
9866: MKLIST(l,nd_tracelist);
9867: node = mknode(2,iq,l); MKLIST(l,node);
9868: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9869: nd_alltracelist = node; nd_tracelist = 0;
9870: }
9871:
9872: void conv_ilist(int demand,int trace,NODE g,int **indp)
9873: {
9874: int n,i,j;
9875: int *ind;
9876: NODE t;
9877:
9878: n = length(g);
9879: ind = (int *)MALLOC(n*sizeof(int));
9880: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9881: j = (long)BDY(t); ind[i] = j;
9882: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9883: }
9884: if ( indp ) *indp = ind;
9885: }
9886:
1.30 noro 9887: NODE conv_ilist_s(int demand,int trace,int **indp)
9888: {
9889: int n,i,j;
9890: int *ind;
9891: NODE g0,g;
9892:
9893: n = nd_psn;
9894: ind = (int *)MALLOC(n*sizeof(int));
9895: g0 = 0;
9896: for ( i = 0; i < n; i++ ) {
9897: ind[i] = i;
9898: NEXTNODE(g0,g);
9899: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
9900: }
9901: if ( g0 ) NEXT(g) = 0;
9902: if ( indp ) *indp = ind;
9903: return g0;
9904: }
9905:
1.1 noro 9906: void parse_nd_option(NODE opt)
9907: {
1.38 noro 9908: NODE t,p,u;
1.1 noro 9909: int i,s,n;
1.38 noro 9910: char *key;
9911: Obj value;
1.1 noro 9912:
1.41 ! noro 9913: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 9914: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9915: nd_splist = 0; nd_check_splist = 0;
1.38 noro 9916: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
9917: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 ! noro 9918: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.39 noro 9919:
1.38 noro 9920: for ( t = opt; t; t = NEXT(t) ) {
9921: p = BDY((LIST)BDY(t));
9922: key = BDY((STRING)BDY(p));
9923: value = (Obj)BDY(NEXT(p));
9924: if ( !strcmp(key,"gentrace") )
9925: nd_gentrace = value?1:0;
9926: else if ( !strcmp(key,"gensyz") )
9927: nd_gensyz = value?1:0;
9928: else if ( !strcmp(key,"nora") )
9929: nd_nora = value?1:0;
1.41 ! noro 9930: else if ( !strcmp(key,"norb") )
! 9931: nd_norb = value?1:0;
1.38 noro 9932: else if ( !strcmp(key,"gbblock") ) {
9933: if ( value && OID(value) == O_LIST ) {
1.1 noro 9934: u = BDY((LIST)value);
1.38 noro 9935: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 9936: for ( i = 0; u; u = NEXT(u) ) {
9937: p = BDY((LIST)BDY(u));
1.6 noro 9938: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9939: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9940: }
9941: nd_gbblock[i] = -1;
1.38 noro 9942: } else
9943: nd_gbblock = 0;
1.1 noro 9944: } else if ( !strcmp(key,"newelim") )
9945: nd_newelim = value?1:0;
9946: else if ( !strcmp(key,"intersect") )
9947: nd_intersect = value?1:0;
1.17 noro 9948: else if ( !strcmp(key,"syzgen") )
9949: nd_intersect = ZTOS((Q)value);
1.1 noro 9950: else if ( !strcmp(key,"lf") )
9951: nd_lf = value?1:0;
9952: else if ( !strcmp(key,"trace") ) {
1.38 noro 9953: if ( value ) {
9954: u = BDY((LIST)value);
9955: nd_nzlist = BDY((LIST)ARG2(u));
9956: nd_bpe = ZTOS((Q)ARG3(u));
9957: }
1.1 noro 9958: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 9959: nd_f4red = ZTOS((Q)value);
1.1 noro 9960: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 9961: nd_rank0 = value?1:0;
1.1 noro 9962: } else if ( !strcmp(key,"splist") ) {
1.38 noro 9963: nd_splist = value?1:0;
1.1 noro 9964: } else if ( !strcmp(key,"check_splist") ) {
9965: nd_check_splist = BDY((LIST)value);
9966: } else if ( !strcmp(key,"sugarweight") ) {
9967: u = BDY((LIST)value);
1.38 noro 9968: n = length(u);
9969: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 9970: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 9971: nd_sugarweight[i] = ZTOS((Q)BDY(u));
9972: } else if ( !strcmp(key,"f4_td") ) {
9973: nd_f4_td = value?1:0;
9974: } else if ( !strcmp(key,"sba_f4step") ) {
9975: nd_sba_f4step = value?ZTOS((Q)value):0;
9976: } else if ( !strcmp(key,"sba_pot") ) {
9977: nd_sba_pot = value?1:0;
9978: } else if ( !strcmp(key,"sba_largelcm") ) {
9979: nd_sba_largelcm = value?1:0;
1.39 noro 9980: } else if ( !strcmp(key,"sba_dontsort") ) {
9981: nd_sba_dontsort = value?1:0;
1.41 ! noro 9982: } else if ( !strcmp(key,"sba_redundant_check") ) {
! 9983: nd_sba_redundant_check = value?1:0;
1.39 noro 9984: } else if ( !strcmp(key,"top") ) {
9985: nd_top = value?1:0;
1.1 noro 9986: }
1.38 noro 9987: }
1.1 noro 9988: }
9989:
9990: ND mdptond(DP d);
9991: ND nd_mul_nm(int mod,NM m0,ND p);
9992: ND nd_mul_nm_lf(NM m0,ND p);
9993: ND *btog(NODE ti,ND **p,int nb,int mod);
9994: ND btog_one(NODE ti,ND *p,int nb,int mod);
9995: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
9996: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
9997:
9998: /* d:monomial */
9999: ND mdptond(DP d)
10000: {
10001: NM m;
10002: ND r;
10003:
10004: if ( OID(d) == 1 )
10005: r = ptond(CO,CO,(P)d);
10006: else {
10007: NEWNM(m);
10008: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10009: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10010: NEXT(m) = 0;
10011: MKND(NV(d),m,1,r);
10012: }
10013: return r;
10014: }
10015:
10016: ND nd_mul_nm(int mod,NM m0,ND p)
10017: {
10018: UINT *d0;
10019: int c0,c1,c;
10020: NM tm,mr,mr0;
10021: ND r;
10022:
10023: if ( !p ) return 0;
10024: d0 = DL(m0);
10025: c0 = CM(m0);
10026: mr0 = 0;
10027: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10028: NEXTNM(mr0,mr);
10029: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10030: ndl_add(d0,DL(tm),DL(mr));
10031: }
10032: NEXT(mr) = 0;
10033: MKND(NV(p),mr0,LEN(p),r);
10034: return r;
10035: }
10036:
10037: ND nd_mul_nm_lf(NM m0,ND p)
10038: {
10039: UINT *d0;
10040: Z c0,c1,c;
10041: NM tm,mr,mr0;
10042: ND r;
10043:
10044: if ( !p ) return 0;
10045: d0 = DL(m0);
10046: c0 = CZ(m0);
10047: mr0 = 0;
10048: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10049: NEXTNM(mr0,mr);
10050: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10051: ndl_add(d0,DL(tm),DL(mr));
10052: }
10053: NEXT(mr) = 0;
10054: MKND(NV(p),mr0,LEN(p),r);
10055: return r;
10056: }
10057:
10058: ND *btog(NODE ti,ND **p,int nb,int mod)
10059: {
10060: PGeoBucket *r;
10061: int i,ci;
10062: NODE t,s;
10063: ND m,tp;
10064: ND *pi,*rd;
10065: P c;
10066:
10067: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10068: for ( i = 0; i < nb; i++ )
10069: r[i] = create_pbucket();
10070: for ( t = ti; t; t = NEXT(t) ) {
10071: s = BDY((LIST)BDY(t));
10072: if ( ARG0(s) ) {
10073: m = mdptond((DP)ARG2(s));
1.6 noro 10074: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10075: if ( (ci = ((MQ)c)->cont) != 0 ) {
10076: HCM(m) = ci;
1.6 noro 10077: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10078: for ( i = 0; i < nb; i++ ) {
10079: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10080: add_pbucket(mod,r[i],tp);
10081: }
10082: }
10083: ci = 1;
10084: } else {
10085: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10086: ci = invm(ci,mod);
10087: }
10088: }
10089: rd = (ND *)MALLOC(nb*sizeof(ND));
10090: for ( i = 0; i < nb; i++ )
10091: rd[i] = normalize_pbucket(mod,r[i]);
10092: if ( ci != 1 )
10093: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10094: return rd;
10095: }
10096:
10097: /* YYY */
10098: ND *btog_lf(NODE ti,ND **p,int nb)
10099: {
10100: PGeoBucket *r;
10101: int i;
10102: NODE t,s;
10103: ND m,tp;
10104: ND *pi,*rd;
10105: LM lm;
10106: Z lf,c;
10107:
10108: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10109: for ( i = 0; i < nb; i++ )
10110: r[i] = create_pbucket();
10111: for ( t = ti; t; t = NEXT(t) ) {
10112: s = BDY((LIST)BDY(t));
10113: if ( ARG0(s) ) {
10114: m = mdptond((DP)ARG2(s));
1.6 noro 10115: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10116: if ( lm ) {
10117: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10118: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10119: for ( i = 0; i < nb; i++ ) {
10120: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10121: add_pbucket(-2,r[i],tp);
10122: }
10123: }
10124: c = ONE;
10125: } else {
10126: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10127: }
10128: }
10129: rd = (ND *)MALLOC(nb*sizeof(ND));
10130: for ( i = 0; i < nb; i++ )
10131: rd[i] = normalize_pbucket(-2,r[i]);
10132: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10133: return rd;
10134: }
10135:
10136: ND btog_one(NODE ti,ND *p,int nb,int mod)
10137: {
10138: PGeoBucket r;
10139: int i,ci,j;
10140: NODE t,s;
10141: ND m,tp;
10142: ND pi,rd;
10143: P c;
10144:
10145: r = create_pbucket();
10146: for ( t = ti; t; t = NEXT(t) ) {
10147: s = BDY((LIST)BDY(t));
10148: if ( ARG0(s) ) {
10149: m = mdptond((DP)ARG2(s));
1.6 noro 10150: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10151: if ( (ci = ((MQ)c)->cont) != 0 ) {
10152: HCM(m) = ci;
1.6 noro 10153: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10154: if ( !pi ) {
10155: pi = nd_load_mod(j);
10156: tp = nd_mul_nm(mod,BDY(m),pi);
10157: nd_free(pi);
10158: add_pbucket(mod,r,tp);
10159: } else {
10160: tp = nd_mul_nm(mod,BDY(m),pi);
10161: add_pbucket(mod,r,tp);
10162: }
10163: }
10164: ci = 1;
10165: } else {
10166: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10167: ci = invm(ci,mod);
10168: }
10169: }
10170: rd = normalize_pbucket(mod,r);
10171: free_pbucket(r);
10172: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10173: return rd;
10174: }
10175:
10176: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10177:
10178: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10179: {
10180: int i,j,n,m,nb,pi0,pi1,nvar;
10181: VL fv,tv,vv;
10182: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10183: ND **p;
10184: ND *c;
10185: ND u;
10186: P inv;
10187: MAT mat;
10188:
10189: if ( mod == -2 )
10190: return nd_btog_lf(f,v,ord,tlist,rp);
10191:
10192: parse_nd_option(current_option);
10193: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10194: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10195: switch ( ord->id ) {
10196: case 1:
10197: if ( ord->nv != nvar )
10198: error("nd_check : invalid order specification");
10199: break;
10200: default:
10201: break;
10202: }
10203: nd_init_ord(ord);
10204: #if 0
1.6 noro 10205: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10206: #else
10207: nd_bpe = 32;
10208: #endif
10209: nd_setup_parameters(nvar,0);
10210: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10211: intred = BDY((LIST)ARG3(BDY(tlist)));
10212: ind = BDY((LIST)ARG4(BDY(tlist)));
10213: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10214: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10215: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10216: if ( j > i ) i = j;
10217: }
10218: n = i+1;
10219: nb = length(BDY(f));
10220: p = (ND **)MALLOC(n*sizeof(ND *));
10221: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10222: pi = BDY((LIST)BDY(t));
1.6 noro 10223: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10224: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10225: ptomp(mod,(P)ARG2(pi),&inv);
10226: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10227: u = ptond(CO,vv,(P)ONE);
10228: HCM(u) = ((MQ)inv)->cont;
10229: c[pi1] = u;
10230: }
10231: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10232: printf("%d ",i); fflush(stdout);
10233: ti = BDY((LIST)BDY(t));
1.6 noro 10234: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10235: }
10236: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10237: printf("%d ",i); fflush(stdout);
10238: ti = BDY((LIST)BDY(t));
1.6 noro 10239: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10240: }
10241: m = length(ind);
10242: MKMAT(mat,nb,m);
10243: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10244: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10245: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10246: return mat;
10247: }
10248:
10249: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10250: {
10251: int i,j,n,m,nb,pi0,pi1,nvar;
10252: VL fv,tv,vv;
10253: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10254: ND **p;
10255: ND *c;
10256: ND u;
10257: MAT mat;
10258: LM lm;
10259: Z lf,inv;
10260:
10261: parse_nd_option(current_option);
10262: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10263: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10264: switch ( ord->id ) {
10265: case 1:
10266: if ( ord->nv != nvar )
10267: error("nd_check : invalid order specification");
10268: break;
10269: default:
10270: break;
10271: }
10272: nd_init_ord(ord);
10273: #if 0
1.6 noro 10274: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10275: #else
10276: nd_bpe = 32;
10277: #endif
10278: nd_setup_parameters(nvar,0);
10279: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10280: intred = BDY((LIST)ARG3(BDY(tlist)));
10281: ind = BDY((LIST)ARG4(BDY(tlist)));
10282: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10283: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10284: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10285: if ( j > i ) i = j;
10286: }
10287: n = i+1;
10288: nb = length(BDY(f));
10289: p = (ND **)MALLOC(n*sizeof(ND *));
10290: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10291: pi = BDY((LIST)BDY(t));
1.6 noro 10292: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10293: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10294: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
10295: u = ptond(CO,vv,(P)ONE);
10296: HCZ(u) = inv;
10297: c[pi1] = u;
10298: }
10299: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10300: printf("%d ",i); fflush(stdout);
10301: ti = BDY((LIST)BDY(t));
1.6 noro 10302: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10303: }
10304: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10305: printf("%d ",i); fflush(stdout);
10306: ti = BDY((LIST)BDY(t));
1.6 noro 10307: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10308: }
10309: m = length(ind);
10310: MKMAT(mat,nb,m);
10311: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10312: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10313: BDY(mat)[i][j] = ndtodp(-2,c[i]);
10314: return mat;
10315: }
10316:
10317: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
10318: LIST tlist,int pos,MAT *rp)
10319: {
10320: int i,j,n,m,nb,pi0,pi1,nvar;
10321: VL fv,tv,vv;
10322: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10323: ND *p;
10324: ND *c;
10325: ND u;
10326: P inv;
10327: VECT vect;
10328:
10329: if ( mod == -2 )
10330: error("nd_btog_one : not implemented yet for a large finite field");
10331:
10332: parse_nd_option(current_option);
10333: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10334: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10335: switch ( ord->id ) {
10336: case 1:
10337: if ( ord->nv != nvar )
10338: error("nd_check : invalid order specification");
10339: break;
10340: default:
10341: break;
10342: }
10343: nd_init_ord(ord);
10344: #if 0
1.6 noro 10345: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10346: #else
10347: nd_bpe = 32;
10348: #endif
10349: nd_setup_parameters(nvar,0);
10350: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10351: intred = BDY((LIST)ARG3(BDY(tlist)));
10352: ind = BDY((LIST)ARG4(BDY(tlist)));
10353: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10354: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10355: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10356: if ( j > i ) i = j;
10357: }
10358: n = i+1;
10359: nb = length(BDY(f));
10360: p = (ND *)MALLOC(n*sizeof(ND *));
10361: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10362: pi = BDY((LIST)BDY(t));
1.6 noro 10363: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10364: if ( pi1 == pos ) {
10365: ptomp(mod,(P)ARG2(pi),&inv);
10366: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10367: u = ptond(CO,vv,(P)ONE);
10368: HCM(u) = ((MQ)inv)->cont;
10369: p[pi0] = u;
10370: }
10371: }
10372: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10373: printf("%d ",i); fflush(stdout);
10374: ti = BDY((LIST)BDY(t));
1.6 noro 10375: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10376: if ( Demand ) {
10377: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10378: }
10379: }
10380: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10381: printf("%d ",i); fflush(stdout);
10382: ti = BDY((LIST)BDY(t));
1.6 noro 10383: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10384: if ( Demand ) {
10385: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10386: }
10387: }
10388: m = length(ind);
10389: MKVECT(vect,m);
10390: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10391: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10392: if ( !u ) {
1.6 noro 10393: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10394: BDY(vect)[j] = ndtodp(mod,u);
10395: nd_free(u);
10396: } else
10397: BDY(vect)[j] = ndtodp(mod,u);
10398: }
10399: return vect;
10400: }
10401:
10402: void ndv_print_lf(NDV p)
10403: {
10404: NMV m;
10405: int i,len;
10406:
10407: if ( !p ) printf("0\n");
10408: else {
10409: len = LEN(p);
10410: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10411: printf("+");
10412: mpz_out_str(asir_out,10,BDY(CZ(m)));
10413: printf("*");
10414: ndl_print(DL(m));
10415: }
10416: printf("\n");
10417: }
10418: }
10419:
10420: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10421: {
10422: VL tv,fv,vv,vc,av;
10423: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10424: int m,nocheck,nvar,mindex,e,max;
10425: NDV c;
10426: NMV a;
10427: P p,zp;
10428: Q dmy;
10429: EPOS oepos;
10430: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10431: Alg alpha,dp;
10432: P poly;
10433: LIST f1,f2,zpl;
10434: Obj obj;
10435: NumberField nf;
10436: struct order_spec *ord1;
10437: struct oEGT eg_check,eg0,eg1;
10438: NODE tr,tl1,tl2,tl3,tl4;
10439: LIST l1,l2,l3,l4,l5;
10440: int *perm;
10441: int j,ret;
10442: NODE retn;
10443: Q jq,bpe;
10444:
10445: nd_module = 0;
10446: parse_nd_option(current_option);
10447: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10448: if ( nd_vc )
10449: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10450: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10451: switch ( ord->id ) {
10452: case 1:
10453: if ( ord->nv != nvar )
10454: error("nd_f4_lf_trace : invalid order specification");
10455: break;
10456: default:
10457: break;
10458: }
10459:
10460: nd_ntrans = nvar;
10461: nd_nalg = 0;
10462:
10463: nocheck = 0;
10464: mindex = 0;
10465:
10466: /* do not use on-demand load/save */
10467: nd_demand = 0;
10468: m = trace > 1 ? trace : get_lprime(mindex);
10469: nd_init_ord(ord);
10470: mrank = 0;
10471: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10472: for ( tv = vv; tv; tv = NEXT(tv) ) {
10473: if ( nd_module ) {
10474: s = BDY((LIST)BDY(t));
10475: trank = length(s);
10476: mrank = MAX(mrank,trank);
10477: for ( ; s; s = NEXT(s) ) {
10478: e = getdeg(tv->v,(P)BDY(s));
10479: max = MAX(e,max);
10480: }
10481: } else {
10482: e = getdeg(tv->v,(P)BDY(t));
10483: max = MAX(e,max);
10484: }
10485: }
10486: nd_setup_parameters(nvar,max);
10487: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10488: ishomo = 1;
10489: /* XXX */
10490: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10491: if ( nd_module ) {
10492: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10493: } else {
10494: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10495: }
10496: if ( ishomo )
10497: ishomo = ishomo && ndv_ishomo(c);
10498: if ( c ) {
10499: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10500: ndv_mod(-2,c);
10501: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10502: }
10503: }
10504: if ( in0 ) NEXT(in) = 0;
10505: if ( fd0 ) NEXT(fd) = 0;
10506: if ( !ishomo && homo ) {
10507: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10508: c = (NDV)BDY(t); len = LEN(c);
10509: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10510: wmax = MAX(TD(DL(a)),wmax);
10511: }
10512: homogenize_order(ord,nvar,&ord1);
10513: nd_init_ord(ord1);
10514: nd_setup_parameters(nvar+1,wmax);
10515: for ( t = fd0; t; t = NEXT(t) )
10516: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10517: }
10518: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10519: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10520: if ( ret )
10521: cand = nd_f4_lf_trace_main(m,&perm);
10522: if ( !ret || !cand ) {
10523: *rp = 0; return;
10524: }
10525: if ( !ishomo && homo ) {
10526: /* dehomogenization */
10527: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10528: nd_init_ord(ord);
10529: nd_setup_parameters(nvar,0);
10530: }
10531: cand = ndv_reducebase(cand,perm);
10532: cand = ndv_reduceall(-2,cand);
10533: cbpe = nd_bpe;
10534: get_eg(&eg0);
10535: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10536: /* gbcheck : cand is a GB of Id(cand) ? */
10537: retn = nd_f4(-2,0,0);
10538: }
10539: if ( !retn ) {
10540: /* failure */
10541: *rp = 0; return;
10542: }
10543: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10544: if ( DP_Print )
1.5 noro 10545: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10546: /* dp->p */
10547: nd_bpe = cbpe;
10548: nd_setup_parameters(nd_nvar,0);
10549: for ( r = cand; r; r = NEXT(r) ) {
10550: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10551: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10552: }
10553: MKLIST(*rp,cand);
10554: }
10555:
10556: NODE nd_f4_lf_trace_main(int m,int **indp)
10557: {
10558: int i,nh,stat,index;
10559: NODE r,rm,g;
10560: ND_pairs d,l,l0,t;
10561: ND spol,red;
10562: NDV nf,redv,nfqv,nfv;
10563: NM s0,s;
10564: NODE rp0,srp0,nflist,nflist_lf;
10565: int nsp,nred,col,rank,len,k,j,a;
10566: UINT c;
10567: UINT **spmat;
10568: UINT *s0vect,*svect,*p,*v;
10569: int *colstat;
10570: IndArray *imat;
10571: int *rhead;
10572: int spcol,sprow;
10573: int sugar;
10574: PGeoBucket bucket;
10575: struct oEGT eg0,eg1,eg_f4;
10576:
10577: g = 0; d = 0;
10578: for ( i = 0; i < nd_psn; i++ ) {
10579: d = update_pairs(d,g,i,0);
10580: g = update_base(g,i);
10581: }
10582: while ( d ) {
10583: get_eg(&eg0);
10584: l = nd_minsugarp(d,&d);
10585: sugar = SG(l);
10586: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10587: bucket = create_pbucket();
10588: stat = nd_sp_f4(m,0,l,bucket);
10589: if ( !stat ) {
10590: for ( t = l; NEXT(t); t = NEXT(t) );
10591: NEXT(t) = d; d = l;
10592: d = nd_reconstruct(1,d);
10593: continue;
10594: }
10595: if ( bucket->m < 0 ) continue;
10596: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10597: if ( !col ) {
10598: for ( t = l; NEXT(t); t = NEXT(t) );
10599: NEXT(t) = d; d = l;
10600: d = nd_reconstruct(1,d);
10601: continue;
10602: }
10603: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10604: if ( DP_Print )
1.5 noro 10605: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10606: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10607: if ( !l0 ) continue;
10608: l = l0;
10609:
10610: /* over LF */
10611: bucket = create_pbucket();
10612: stat = nd_sp_f4(-2,1,l,bucket);
10613: if ( !stat ) {
10614: for ( t = l; NEXT(t); t = NEXT(t) );
10615: NEXT(t) = d; d = l;
10616: d = nd_reconstruct(1,d);
10617: continue;
10618: }
10619: if ( bucket->m < 0 ) continue;
10620: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10621: if ( !col ) {
10622: for ( t = l; NEXT(t); t = NEXT(t) );
10623: NEXT(t) = d; d = l;
10624: d = nd_reconstruct(1,d);
10625: continue;
10626: }
10627: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10628: /* adding new bases */
10629: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10630: nfv = (NDV)BDY(rm);
10631: nfqv = (NDV)BDY(r);
10632: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10633: ndv_removecont(m,nfv);
10634: ndv_removecont(-2,nfqv);
1.24 noro 10635: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10636: d = update_pairs(d,g,nh,0);
10637: g = update_base(g,nh);
10638: }
10639: if ( r || rm ) return 0;
10640: }
10641: conv_ilist(nd_demand,1,g,indp);
10642: return g;
10643: }
10644:
1.7 noro 10645: #if SIZEOF_LONG==8
10646:
10647: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10648: {
10649: int j,k,len;
10650: UINT *p;
10651: UINT c;
10652: NDV r;
10653: NMV mr0,mr;
10654:
10655: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10656: if ( !len ) return 0;
10657: else {
10658: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10659: #if 0
10660: ndv_alloc += nmv_adv*len;
10661: #endif
10662: mr = mr0;
10663: p = s0vect;
10664: for ( j = k = 0; j < col; j++, p += nd_wpd )
10665: if ( !rhead[j] ) {
10666: if ( (c = (UINT)vect[k++]) != 0 ) {
10667: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10668: }
10669: }
10670: MKNDV(nd_nvar,mr0,len,r);
10671: return r;
10672: }
10673: }
10674:
1.28 noro 10675: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
10676: {
10677: int j,k,len;
10678: UINT *p;
10679: UINT c;
10680: NDV r;
10681: NMV mr0,mr;
10682:
10683: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
10684: if ( !len ) return 0;
10685: else {
10686: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10687: mr = mr0;
10688: p = s0vect;
10689: for ( j = k = 0; j < col; j++, p += nd_wpd )
10690: if ( (c = (UINT)vect[k++]) != 0 ) {
10691: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10692: }
10693: MKNDV(nd_nvar,mr0,len,r);
10694: return r;
10695: }
10696: }
10697:
1.7 noro 10698: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10699: {
10700: NM m;
1.11 noro 10701: UINT *t,*s,*u;
10702: int i,st,ed,md,prev,c;
1.7 noro 10703:
10704: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10705: prev = 0;
10706: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10707: t = DL(m);
10708: st = prev;
10709: ed = n;
10710: while ( ed > st ) {
10711: md = (st+ed)/2;
10712: u = s0+md*nd_wpd;
10713: c = DL_COMPARE(u,t);
10714: if ( c == 0 ) break;
10715: else if ( c > 0 ) st = md;
10716: else ed = md;
10717: }
10718: r[md] = (mp_limb_t)CM(m);
10719: prev = md;
1.7 noro 10720: }
10721: for ( i = 0; !r[i]; i++ );
10722: return i;
10723: }
10724:
10725: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10726:
1.28 noro 10727: 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 10728: {
10729: int i,j,k,len,pos,prev;
10730: mp_limb_t a,c,c1,c2;
10731: IndArray ivect;
10732: unsigned char *ivc;
10733: unsigned short *ivs;
10734: unsigned int *ivi;
10735: NDV redv;
10736: NMV mr;
10737: NODE rp;
10738: int maxrs;
10739:
10740: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10741: maxrs = 0;
10742: for ( i = 0; i < nred; i++ ) {
10743: ivect = imat[i];
10744: k = ivect->head;
10745: a = svect[k]; c = cvect[k];
10746: MOD128(a,c,m);
10747: svect[k] = a; cvect[k] = 0;
1.28 noro 10748: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 10749: Nf4_red++;
1.7 noro 10750: maxrs = MAX(maxrs,rp0[i]->sugar);
10751: c = m-c; redv = nd_ps[rp0[i]->index];
10752: len = LEN(redv); mr = BDY(redv);
10753: svect[k] = 0; prev = k;
10754: switch ( ivect->width ) {
10755: case 1:
10756: ivc = ivect->index.c;
10757: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10758: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10759: c2 = svect[pos]+c1*c;
10760: if ( c2 < svect[pos] ) cvect[pos]++;
10761: svect[pos] = c2;
1.7 noro 10762: }
10763: break;
10764: case 2:
10765: ivs = ivect->index.s;
10766: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10767: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10768: c2 = svect[pos]+c1*c;
10769: if ( c2 < svect[pos] ) cvect[pos]++;
10770: svect[pos] = c2;
1.7 noro 10771: }
10772: break;
10773: case 4:
10774: ivi = ivect->index.i;
10775: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10776: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10777: c2 = svect[pos]+c1*c;
10778: if ( c2 < svect[pos] ) cvect[pos]++;
10779: svect[pos] = c2;
1.7 noro 10780: }
10781: break;
10782: }
10783: }
10784: }
10785: for ( i = 0; i < col; i++ ) {
10786: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10787: }
10788: return maxrs;
10789: }
10790:
10791: /* for Fp, 2^15=<p<2^29 */
10792:
10793: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10794: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10795: {
10796: int spcol,sprow,a;
10797: int i,j,k,l,rank;
10798: NODE r0,r;
10799: ND_pairs sp;
10800: ND spol;
10801: mp_limb_t **spmat;
10802: mp_limb_t *svect,*cvect;
10803: mp_limb_t *v;
10804: int *colstat;
10805: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10806: int maxrs;
10807: int *spsugar;
10808: ND_pairs *spactive;
10809:
10810: spcol = col-nred;
10811: get_eg(&eg0);
10812: /* elimination (1st step) */
10813: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10814: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10815: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10816: spsugar = (int *)MALLOC(nsp*sizeof(int));
10817: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10818: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10819: nd_sp(m,0,sp,&spol);
10820: if ( !spol ) continue;
10821: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 10822: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 10823: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10824: if ( i < col ) {
10825: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10826: for ( j = k = 0; j < col; j++ )
10827: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10828: spsugar[sprow] = MAX(maxrs,SG(spol));
10829: if ( nz )
10830: spactive[sprow] = sp;
10831: sprow++;
10832: }
10833: nd_free(spol);
10834: }
1.12 noro 10835: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10836: if ( DP_Print ) {
10837: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10838: fflush(asir_out);
10839: }
10840: /* free index arrays */
10841: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10842:
10843: /* elimination (2nd step) */
10844: colstat = (int *)MALLOC(spcol*sizeof(int));
10845: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10846: r0 = 0;
10847: for ( i = 0; i < rank; i++ ) {
10848: NEXTNODE(r0,r); BDY(r) =
10849: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10850: SG((NDV)BDY(r)) = spsugar[i];
10851: GCFREE(spmat[i]);
10852: }
10853: if ( r0 ) NEXT(r) = 0;
10854:
10855: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10856: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10857: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10858: if ( DP_Print ) {
10859: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10860: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10861: nsp,nred,sprow,spcol,rank);
10862: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10863: }
10864: if ( nz ) {
10865: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10866: if ( rank > 0 ) {
10867: NEXT(spactive[rank-1]) = 0;
10868: *nz = spactive[0];
10869: } else
10870: *nz = 0;
10871: }
10872: return r0;
10873: }
10874:
10875: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10876: {
10877: int i,j,k,l,rank,s;
10878: mp_limb_t inv;
10879: mp_limb_t a;
10880: UINT c;
10881: mp_limb_t *t,*pivot,*pk;
10882: UINT *ck;
10883: UINT **cmat;
10884: UINT *ct;
10885: ND_pairs pair;
10886:
10887: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10888: for ( i = 0; i < row; i++ ) {
10889: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10890: bzero(cmat[i],col*sizeof(UINT));
10891: }
10892:
10893: for ( rank = 0, j = 0; j < col; j++ ) {
10894: for ( i = rank; i < row; i++ ) {
10895: a = mat[i][j]; c = cmat[i][j];
10896: MOD128(a,c,md);
10897: mat[i][j] = a; cmat[i][j] = 0;
10898: }
10899: for ( i = rank; i < row; i++ )
10900: if ( mat[i][j] )
10901: break;
10902: if ( i == row ) {
10903: colstat[j] = 0;
10904: continue;
10905: } else
10906: colstat[j] = 1;
10907: if ( i != rank ) {
10908: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10909: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10910: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10911: if ( spactive ) {
10912: pair = spactive[i]; spactive[i] = spactive[rank];
10913: spactive[rank] = pair;
10914: }
10915: }
10916: /* column j is normalized */
10917: s = sugar[rank];
10918: inv = invm((UINT)mat[rank][j],md);
10919: /* normalize pivot row */
10920: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10921: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10922: }
10923: for ( i = rank+1; i < row; i++ ) {
10924: if ( (a = mat[i][j]) != 0 ) {
10925: sugar[i] = MAX(sugar[i],s);
10926: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10927: Nf4_red++;
1.7 noro 10928: }
10929: }
10930: rank++;
10931: }
10932: for ( j = col-1, l = rank-1; j >= 0; j-- )
10933: if ( colstat[j] ) {
10934: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10935: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10936: }
10937: s = sugar[l];
10938: for ( i = 0; i < l; i++ ) {
10939: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10940: if ( a ) {
10941: sugar[i] = MAX(sugar[i],s);
10942: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10943: Nf4_red++;
1.7 noro 10944: }
10945: }
10946: l--;
10947: }
10948: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10949: GCFREE(cmat);
10950: return rank;
10951: }
10952:
1.28 noro 10953: 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)
10954: {
10955: int i,j,k,l,rank,s,imin;
10956: mp_limb_t inv;
10957: mp_limb_t a;
10958: UINT c;
10959: mp_limb_t *t,*pivot,*pk;
10960: UINT *ck;
10961: UINT **cmat;
10962: UINT *ct;
10963: ND_pairs pair;
10964: SIG sg;
1.31 noro 10965: int *used;
1.28 noro 10966:
1.31 noro 10967: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 10968: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10969: for ( i = 0; i < row; i++ ) {
10970: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10971: bzero(cmat[i],col*sizeof(UINT));
10972: }
10973:
1.31 noro 10974: for ( j = 0; j < col; j++ ) {
10975: for ( i = 0; i < row; i++ ) {
1.28 noro 10976: a = mat[i][j]; c = cmat[i][j];
10977: MOD128(a,c,md);
10978: mat[i][j] = a; cmat[i][j] = 0;
10979: }
1.31 noro 10980: for ( i = 0; i < row; i++ )
10981: if ( !used[i] && mat[i][j] ) break;
10982: if ( i == row ) {
1.28 noro 10983: colstat[j] = 0;
10984: continue;
1.31 noro 10985: } else {
1.28 noro 10986: colstat[j] = 1;
1.31 noro 10987: used[i] = 1;
1.28 noro 10988: }
10989: /* column j is normalized */
1.31 noro 10990: s = sugar[i];
10991: inv = invm((UINT)mat[i][j],md);
1.28 noro 10992: /* normalize pivot row */
1.31 noro 10993: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 10994: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10995: }
1.31 noro 10996: for ( k = i+1; k < row; k++ ) {
10997: if ( (a = mat[k][j]) != 0 ) {
10998: sugar[k] = MAX(sugar[k],s);
10999: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11000: Nf4_red++;
11001: }
11002: }
11003: }
1.31 noro 11004: rank = 0;
11005: for ( i = 0; i < row; i++ ) {
11006: for ( j = 0; j < col; j++ )
11007: if ( mat[i][j] ) break;
11008: if ( j == col ) sugar[i] = -1;
11009: else rank++;
11010: }
1.28 noro 11011: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11012: GCFREE(cmat);
11013: return rank;
11014: }
11015:
11016: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11017: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11018: {
11019: int spcol,sprow,a;
11020: int i,j,k,l,rank;
11021: NODE r0,r;
11022: ND_pairs sp;
11023: ND spol;
11024: mp_limb_t **spmat;
11025: mp_limb_t *svect,*cvect;
11026: mp_limb_t *v;
11027: int *colstat;
11028: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11029: int maxrs;
11030: int *spsugar;
11031: ND_pairs *spactive;
11032: SIG *spsig;
11033:
11034: get_eg(&eg0);
11035: /* elimination (1st step) */
11036: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11037: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11038: spsugar = (int *)MALLOC(nsp*sizeof(int));
11039: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11040: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11041: nd_sp(m,0,sp,&spol);
11042: if ( !spol ) {
1.29 noro 11043: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11044: continue;
11045: }
11046: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11047: nd_to_vect64(m,s0vect,col,spol,svect);
11048: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11049: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11050: if ( i < col ) {
11051: spmat[sprow] = svect;
11052: spsugar[sprow] = MAX(maxrs,SG(spol));
11053: spsig[sprow] = sp->sig;
11054: sprow++;
11055: } else {
1.29 noro 11056: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11057: }
11058: nd_free(spol);
11059: }
11060: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11061: if ( DP_Print ) {
11062: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11063: fflush(asir_out);
11064: }
11065: /* free index arrays */
11066: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11067:
11068: /* elimination (2nd step) */
11069: colstat = (int *)MALLOC(col*sizeof(int));
11070: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11071: r0 = 0;
1.31 noro 11072: for ( i = 0; i < sprow; i++ ) {
11073: if ( spsugar[i] >= 0 ) {
11074: NEXTNODE(r0,r);
11075: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11076: SG((NDV)BDY(r)) = spsugar[i];
11077: ((NDV)BDY(r))->sig = spsig[i];
11078: } else
11079: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11080: GCFREE(spmat[i]);
11081: }
11082: if ( r0 ) NEXT(r) = 0;
11083: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11084: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11085: if ( DP_Print ) {
11086: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11087: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11088: nsp,nred,sprow,col,rank);
11089: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11090: }
11091: return r0;
11092: }
1.40 noro 11093: #endif
1.28 noro 11094:
11095: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11096: {
11097: IndArray *imat;
11098: int nsp,nred,i,start;
11099: int *rhead;
11100: NODE r0,rp;
11101: ND_pairs sp;
11102: NM_ind_pair *rvect;
11103: UINT *s;
11104: int *s0hash;
11105: struct oEGT eg0,eg1,eg_conv;
11106:
11107: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11108: nred = length(rp0);
11109: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11110: rhead = (int *)MALLOC(col*sizeof(int));
11111: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11112:
11113: /* construction of index arrays */
11114: get_eg(&eg0);
11115: if ( DP_Print ) {
11116: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11117: fflush(asir_out);
11118: }
11119: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11120: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11121: rvect[i] = (NM_ind_pair)BDY(rp);
11122: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11123: rhead[imat[i]->head] = 1;
11124: start = imat[i]->head;
11125: }
11126: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11127: if ( DP_Print ) {
11128: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11129: fflush(asir_out);
11130: }
11131: if ( m > 0 )
1.40 noro 11132: #if SIZEOF_LONG==8
1.28 noro 11133: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11134: #else
11135: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11136: #endif
1.28 noro 11137: else
11138: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11139: error("nd_f4_red_q_main_s : not implemented yet");
11140: return r0;
11141: }
11142:
11143: INLINE int ndl_find_reducer_minsig(UINT *dg)
11144: {
11145: RHist r;
11146: int i,singular,ret,d,k,imin;
11147: SIG t;
11148: static int wpd,nvar;
11149: static SIG quo,quomin;
11150: static UINT *tmp;
11151:
11152: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11153: if ( wpd != nd_wpd ) {
11154: wpd = nd_wpd;
11155: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11156: }
11157: #if 0
11158: d = ndl_hash_value(dg);
11159: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11160: if ( ndl_equal(dg,DL(r)) ) {
11161: return r->index;
11162: }
11163: }
11164: #endif
11165: imin = -1;
11166: for ( i = 0; i < nd_psn; i++ ) {
11167: r = nd_psh[i];
11168: if ( ndl_reducible(dg,DL(r)) ) {
11169: ndl_sub(dg,DL(r),tmp);
11170: _ndltodl(tmp,DL(quo));
11171: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11172: quo->pos = nd_psh[i]->sig->pos;
11173: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11174: t = quo; quo = quomin; quomin = t;
11175: imin = i;
11176: }
11177: }
11178: }
11179: if ( imin == -1 ) return nd_psn;
11180: else {
11181: #if 0
11182: nd_append_red(dg,i);
11183: #endif
11184: return imin;
11185: }
11186: }
11187:
11188: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11189: {
11190: NODE rp0,rp;
11191: NM mul,head,s0,s;
11192: int index,col,i,sugar;
11193: RHist h;
11194: UINT *s0v,*p;
11195: NM_ind_pair pair;
11196: ND red;
11197: NDV *ps;
11198: SIG sig;
11199:
11200: s0 = 0; rp0 = 0; col = 0;
11201: if ( nd_demand )
11202: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11203: else
11204: ps = trace?nd_ps_trace:nd_ps;
11205: while ( 1 ) {
11206: head = remove_head_pbucket_symbolic(bucket);
11207: if ( !head ) break;
11208: if ( !s0 ) s0 = head;
11209: else NEXT(s) = head;
11210: s = head;
11211: index = ndl_find_reducer_minsig(DL(head));
11212: if ( index >= 0 && index < nd_psn ) {
11213: h = nd_psh[index];
11214: NEWNM(mul);
11215: ndl_sub(DL(head),DL(h),DL(mul));
11216: if ( ndl_check_bound2(index,DL(mul)) )
11217: return 0;
11218: sugar = TD(DL(mul))+SG(ps[index]);
11219: NEWSIG(sig);
11220: _ndltodl(DL(mul),DL(sig));
11221: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11222: sig->pos = nd_psh[index]->sig->pos;
11223: MKNM_ind_pair(pair,mul,index,sugar,sig);
11224: red = ndv_mul_nm_symbolic(mul,ps[index]);
11225: add_pbucket_symbolic(bucket,nd_remove_head(red));
11226: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11227: }
11228: col++;
11229: }
11230: if ( rp0 ) NEXT(rp) = 0;
11231: NEXT(s) = 0;
11232: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11233: for ( i = 0, p = s0v, s = s0; i < col;
11234: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11235: *s0vect = s0v;
11236: *r = rp0;
11237:
11238: return col;
11239: }
11240:
11241: NODE nd_sba_f4(int m,int **indp)
11242: {
1.34 noro 11243: int i,nh,stat,index,f4red,f4step;
1.33 noro 11244: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11245: NODE r,g,rp0,nflist;
1.41 ! noro 11246: ND_pairs d,l,t,l1;
1.33 noro 11247: ND h,nf;
11248: NDV nfv;
11249: union oNDC hc;
11250: UINT *s0vect;
1.28 noro 11251: UINT c;
11252: PGeoBucket bucket;
1.33 noro 11253: NODE *syzlist;
11254: SIG sig;
1.28 noro 11255: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11256: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11257:
11258: Nf4_red=0;
1.30 noro 11259: d = 0;
1.29 noro 11260: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11261: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11262: d = update_pairs_s(d,i,syzlist);
1.28 noro 11263: }
1.30 noro 11264: nd_nbase = nd_psn;
1.28 noro 11265: f4red = 1;
1.33 noro 11266: psugar = 0;
1.34 noro 11267: f4step = 0;
1.28 noro 11268: while ( d ) {
1.33 noro 11269: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11270: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11271: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11272: again:
11273: l = d; d = d->next;
1.41 ! noro 11274: #if 0
1.33 noro 11275: if ( small_lcm(l) ) {
11276: if ( DP_Print ) fprintf(asir_out,"M");
11277: continue;
11278: }
11279: sig = l->sig;
11280: stat = nd_sp(m,0,l,&h);
1.41 ! noro 11281: #else
! 11282: l1 = find_smallest_lcm(l);
! 11283: if ( l1 == 0 ) {
! 11284: if ( DP_Print ) fprintf(asir_out,"M");
! 11285: continue;
! 11286: }
! 11287: sig = l1->sig;
! 11288: stat = nd_sp(m,0,l1,&h);
! 11289: #endif
1.33 noro 11290: if ( !stat ) {
11291: NEXT(l) = d; d = l;
11292: d = nd_reconstruct(0,d);
11293: goto again;
11294: }
11295: get_eg(&eg1);
11296: #if USE_GEOBUCKET
1.39 noro 11297: 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 11298: #else
1.39 noro 11299: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11300: #endif
11301: get_eg(&eg2);
11302: if ( !stat ) {
11303: NEXT(l) = d; d = l;
11304: d = nd_reconstruct(0,d);
11305: goto again;
11306: } else if ( stat == -1 ) {
11307: if ( DP_Print ) { printf("S"); fflush(stdout); }
11308: FREENDP(l);
11309: } else if ( nf ) {
11310: if ( DP_Print ) { printf("+"); fflush(stdout); }
11311: add_eg(&eg_nf,&eg1,&eg2);
11312: hc = HCU(nf);
11313: nd_removecont(m,nf);
11314: nfv = ndtondv(m,nf); nd_free(nf);
11315: nh = ndv_newps(m,nfv,0);
11316:
1.30 noro 11317: d = update_pairs_s(d,nh,syzlist);
11318: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 11319: FREENDP(l);
1.28 noro 11320: } else {
1.33 noro 11321: add_eg(&eg_nfzero,&eg1,&eg2);
11322: // syzygy
11323: get_eg(&eg1);
11324: d = remove_spair_s(d,sig);
11325: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 11326: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 11327: if ( DP_Print ) { printf("."); fflush(stdout); }
11328: FREENDP(l);
11329: }
11330: } else {
1.34 noro 11331: if ( ms != psugar ) f4step = 1;
11332: else f4step++;
1.33 noro 11333: again2:
11334: psugar = ms;
11335: l = nd_minsugarp_s(d,&d);
11336: sugar = nd_sugarweight?d->sugar2:SG(d);
11337: bucket = create_pbucket();
11338: stat = nd_sp_f4(m,0,l,bucket);
11339: if ( !stat ) {
11340: for ( t = l; NEXT(t); t = NEXT(t) );
11341: NEXT(t) = d; d = l;
11342: d = nd_reconstruct(0,d);
11343: goto again2;
11344: }
11345: if ( bucket->m < 0 ) continue;
11346: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
11347: if ( !col ) {
1.37 noro 11348: for ( t = l; NEXT(t); t = NEXT(t) )
11349: ;
11350: NEXT(t) = d; d = l;
1.33 noro 11351: d = nd_reconstruct(0,d);
11352: goto again2;
11353: }
1.34 noro 11354: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 11355: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
11356: /* adding new bases */
11357: for ( r = nflist; r; r = NEXT(r) ) {
11358: nfv = (NDV)BDY(r);
1.38 noro 11359: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 11360: ndv_removecont(m,nfv);
11361: nh = ndv_newps(m,nfv,0);
11362: d = update_pairs_s(d,nh,syzlist);
11363: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
11364: }
11365: for ( i = 0; i < nd_nbase; i++ )
11366: for ( r = syzlist[i]; r; r = NEXT(r) )
11367: d = remove_spair_s(d,(SIG)BDY(r));
11368: d = remove_large_lcm(d);
11369: if ( DP_Print ) {
1.34 noro 11370: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11371: }
11372: f4red++;
1.28 noro 11373: }
11374: }
11375: if ( DP_Print ) {
1.34 noro 11376: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11377: }
1.30 noro 11378: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11379: return g;
11380: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>