Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.43
1.43 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.42 2020/12/03 07:58:54 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);
1.43 ! noro 101: NDV dptondv(int mod,DP p);
1.16 noro 102: NDV dpmtondv(int mod,DPM p);
1.43 ! noro 103: int dp_getdeg(DP p);
1.16 noro 104: int dpm_getdeg(DPM p,int *rank);
105: void dpm_ptozp(DPM p,Z *cont,DPM *r);
106: int compdmm(int nv,DMM a,DMM b);
1.1 noro 107:
108: void Pdp_set_weight(NODE,VECT *);
109: void Pox_cmo_rpc(NODE,Obj *);
110:
111: ND nd_add_lf(ND p1,ND p2);
112: void nd_mul_c_lf(ND p,Z mul);
113: void ndv_mul_c_lf(NDV p,Z mul);
114: NODE nd_f4_red_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_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
117: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
118: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
119: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
120: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 121: 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 122: NODE nd_f4_lf_trace_main(int m,int **indp);
123: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
124:
125: extern int lf_lazy;
126: extern Z current_mod_lf;
127:
128: extern int Denominator,DP_Multiple,MaxDeg;
129:
130: #define BLEN (8*sizeof(unsigned long))
131:
132: typedef struct matrix {
133: int row,col;
134: unsigned long **a;
135: } *matrix;
136:
137:
138: void nd_free_private_storage()
139: {
140: _nm_free_list = 0;
141: _ndp_free_list = 0;
142: #if 0
143: GC_gcollect();
144: #endif
145: }
146:
147: void _NM_alloc()
148: {
149: NM p;
150: int i;
151:
152: for ( i = 0; i < 1024; i++ ) {
153: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
154: p->next = _nm_free_list; _nm_free_list = p;
155: }
156: }
157:
158: matrix alloc_matrix(int row,int col)
159: {
160: unsigned long **a;
161: int i,len,blen;
162: matrix mat;
163:
164: mat = (matrix)MALLOC(sizeof(struct matrix));
165: mat->row = row;
166: mat->col = col;
167: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
168: return mat;
169: }
170:
171:
172: void _ND_alloc()
173: {
174: ND p;
175: int i;
176:
177: for ( i = 0; i < 1024; i++ ) {
178: p = (ND)MALLOC(sizeof(struct oND));
179: p->body = (NM)_nd_free_list; _nd_free_list = p;
180: }
181: }
182:
183: void _NDP_alloc()
184: {
185: ND_pairs p;
186: int i;
187:
188: for ( i = 0; i < 1024; i++ ) {
189: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
190: +(nd_wpd-1)*sizeof(UINT));
191: p->next = _ndp_free_list; _ndp_free_list = p;
192: }
193: }
194:
195: INLINE int nd_length(ND p)
196: {
197: NM m;
198: int i;
199:
200: if ( !p )
201: return 0;
202: else {
203: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
204: return i;
205: }
206: }
207:
208: extern int dp_negative_weight;
209:
210: INLINE int ndl_reducible(UINT *d1,UINT *d2)
211: {
212: UINT u1,u2;
213: int i,j;
214:
215: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
216:
217: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
218: #if USE_UNROLL
219: switch ( nd_bpe ) {
220: case 3:
221: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
222: u1 = d1[i]; u2 = d2[i];
223: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
224: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
225: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
226: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
227: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
228: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
229: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
230: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
231: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
232: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
233: }
234: return 1;
235: break;
236: case 4:
237: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
238: u1 = d1[i]; u2 = d2[i];
239: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
240: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
241: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
242: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
243: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
244: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
245: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
246: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
247: }
248: return 1;
249: break;
250: case 6:
251: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
252: u1 = d1[i]; u2 = d2[i];
253: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
254: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
255: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
256: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
257: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
258: }
259: return 1;
260: break;
261: case 8:
262: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
263: u1 = d1[i]; u2 = d2[i];
264: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
265: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
266: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
267: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
268: }
269: return 1;
270: break;
271: case 16:
272: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
273: u1 = d1[i]; u2 = d2[i];
274: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
275: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
276: }
277: return 1;
278: break;
279: case 32:
280: for ( i = nd_exporigin; i < nd_wpd; i++ )
281: if ( d1[i] < d2[i] ) return 0;
282: return 1;
283: break;
284: default:
285: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
286: u1 = d1[i]; u2 = d2[i];
287: for ( j = 0; j < nd_epw; j++ )
288: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
289: }
290: return 1;
291: }
292: #else
293: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
294: u1 = d1[i]; u2 = d2[i];
295: for ( j = 0; j < nd_epw; j++ )
296: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
297: }
298: return 1;
299: #endif
300: }
301:
302: /*
303: * If the current order is a block order,
304: * then the last block is length 1 and contains
305: * the homo variable. Otherwise, the original
306: * order is either 0 or 2.
307: */
308:
309: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
310: {
311: int w,i,e,n,omask0;
312:
313: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
314: n = nd_nvar-1;
315: ndl_zero(r);
316: for ( i = 0; i < n; i++ ) {
317: e = GET_EXP_OLD(d,i);
318: PUT_EXP(r,i,e);
319: }
320: w = TD(d);
321: PUT_EXP(r,nd_nvar-1,weight-w);
322: if ( nd_module ) MPOS(r) = d[ompos];
323: TD(r) = weight;
324: if ( nd_blockmask ) ndl_weight_mask(r);
325: }
326:
327: void ndl_dehomogenize(UINT *d)
328: {
329: UINT mask;
330: UINT h;
331: int i,bits;
332:
333: if ( nd_blockmask ) {
334: h = GET_EXP(d,nd_nvar-1);
335: XOR_EXP(d,nd_nvar-1,h);
336: TD(d) -= h;
337: ndl_weight_mask(d);
338: } else {
339: if ( nd_isrlex ) {
340: if ( nd_bpe == 32 ) {
341: h = d[nd_exporigin];
342: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
343: d[i-1] = d[i];
344: d[i-1] = 0;
345: TD(d) -= h;
346: } else {
347: bits = nd_epw*nd_bpe;
348: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
349: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
350: for ( i = nd_exporigin; i < nd_wpd; i++ )
351: d[i] = ((d[i]<<nd_bpe)&mask)
352: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
353: TD(d) -= h;
354: }
355: } else {
356: h = GET_EXP(d,nd_nvar-1);
357: XOR_EXP(d,nd_nvar-1,h);
358: TD(d) -= h;
359: }
360: }
361: }
362:
363: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
364: {
365: UINT t1,t2,u,u1,u2;
366: int i,j,l;
367:
368: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
369: error("ndl_lcm : inconsistent monomials");
370: #if USE_UNROLL
371: switch ( nd_bpe ) {
372: case 3:
373: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
374: u1 = d1[i]; u2 = d2[i];
375: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
376: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
377: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
378: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
379: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
380: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
381: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
382: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
383: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
384: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
385: d[i] = u;
386: }
387: break;
388: case 4:
389: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
390: u1 = d1[i]; u2 = d2[i];
391: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
392: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
393: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
394: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
395: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
396: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
397: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
398: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
399: d[i] = u;
400: }
401: break;
402: case 6:
403: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
404: u1 = d1[i]; u2 = d2[i];
405: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
406: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
407: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
408: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
409: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
410: d[i] = u;
411: }
412: break;
413: case 8:
414: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
415: u1 = d1[i]; u2 = d2[i];
416: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
417: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
418: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
419: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
420: d[i] = u;
421: }
422: break;
423: case 16:
424: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
425: u1 = d1[i]; u2 = d2[i];
426: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
427: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
428: d[i] = u;
429: }
430: break;
431: case 32:
432: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
433: u1 = d1[i]; u2 = d2[i];
434: d[i] = u1>u2?u1:u2;
435: }
436: break;
437: default:
438: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
439: u1 = d1[i]; u2 = d2[i];
440: for ( j = 0, u = 0; j < nd_epw; j++ ) {
441: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
442: }
443: d[i] = u;
444: }
445: break;
446: }
447: #else
448: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
449: u1 = d1[i]; u2 = d2[i];
450: for ( j = 0, u = 0; j < nd_epw; j++ ) {
451: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
452: }
453: d[i] = u;
454: }
455: #endif
456: if ( nd_module ) MPOS(d) = MPOS(d1);
457: TD(d) = ndl_weight(d);
458: if ( nd_blockmask ) ndl_weight_mask(d);
459: }
460:
461: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
462: {
463: UINT t1,t2,u,u1,u2;
464: int i,j,l;
465:
466: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
467: u1 = d1[i]; u2 = d2[i];
468: for ( j = 0, u = 0; j < nd_epw; j++ ) {
469: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
470: }
471: d[i] = u;
472: }
473: }
474:
475: int ndl_weight(UINT *d)
476: {
477: UINT t,u;
478: int i,j;
479:
480: if ( current_dl_weight_vector )
481: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
482: u = GET_EXP(d,i);
483: t += MUL_WEIGHT(u,i);
484: }
485: else
486: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
487: u = d[i];
488: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
489: t += (u&nd_mask0);
490: }
1.20 noro 491: if ( nd_module && nd_module_rank && MPOS(d) )
492: t += nd_module_weight[MPOS(d)-1];
493: for ( i = nd_exporigin; i < nd_wpd; i++ )
494: if ( d[i] && !t )
495: printf("afo\n");
1.1 noro 496: return t;
497: }
498:
499: /* for sugarweight */
500:
501: int ndl_weight2(UINT *d)
502: {
503: int t,u;
504: int i,j;
505:
506: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
507: u = GET_EXP(d,i);
508: t += nd_sugarweight[i]*u;
509: }
1.20 noro 510: if ( nd_module && nd_module_rank && MPOS(d) )
511: t += nd_module_weight[MPOS(d)-1];
1.1 noro 512: return t;
513: }
514:
515: void ndl_weight_mask(UINT *d)
516: {
517: UINT t,u;
518: UINT *mask;
519: int i,j,k,l;
520:
521: l = nd_blockmask->n;
522: for ( k = 0; k < l; k++ ) {
523: mask = nd_blockmask->mask[k];
524: if ( current_dl_weight_vector )
525: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
526: u = GET_EXP_MASK(d,i,mask);
527: t += MUL_WEIGHT(u,i);
528: }
529: else
530: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
531: u = d[i]&mask[i];
532: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
533: t += (u&nd_mask0);
534: }
535: d[k+1] = t;
536: }
537: }
538:
1.21 noro 539: int ndl_glex_compare(UINT *d1,UINT *d2)
540: {
541: if ( TD(d1) > TD(d2) ) return 1;
542: else if ( TD(d1) < TD(d2) ) return -1;
543: else return ndl_lex_compare(d1,d2);
544: }
545:
1.1 noro 546: int ndl_lex_compare(UINT *d1,UINT *d2)
547: {
548: int i;
549:
550: d1 += nd_exporigin;
551: d2 += nd_exporigin;
552: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
553: if ( *d1 > *d2 )
554: return nd_isrlex ? -1 : 1;
555: else if ( *d1 < *d2 )
556: return nd_isrlex ? 1 : -1;
557: return 0;
558: }
559:
560: int ndl_block_compare(UINT *d1,UINT *d2)
561: {
562: int i,l,j,ord_o,ord_l;
563: struct order_pair *op;
564: UINT t1,t2,m;
565: UINT *mask;
566:
567: l = nd_blockmask->n;
568: op = nd_blockmask->order_pair;
569: for ( j = 0; j < l; j++ ) {
570: mask = nd_blockmask->mask[j];
571: ord_o = op[j].order;
572: if ( ord_o < 2 ) {
573: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
574: else if ( t1 < t2 ) return -1;
575: }
576: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
577: m = mask[i];
578: t1 = d1[i]&m;
579: t2 = d2[i]&m;
580: if ( t1 > t2 )
581: return !ord_o ? -1 : 1;
582: else if ( t1 < t2 )
583: return !ord_o ? 1 : -1;
584: }
585: }
586: return 0;
587: }
588:
589: int ndl_matrix_compare(UINT *d1,UINT *d2)
590: {
591: int i,j,s,row;
592: int *v;
593: Z **mat;
594: Z *w;
595: Z t1;
596: Z t,t2;
597:
1.6 noro 598: for ( j = 0; j < nd_nvar; j++ )
599: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 600: if ( nd_top_weight ) {
601: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 602: mat = (Z **)&BDY((VECT)nd_top_weight);
603: row = 1;
1.1 noro 604: } else {
605: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 606: row = ((MAT)nd_top_weight)->row;
1.1 noro 607: }
608: for ( i = 0; i < row; i++ ) {
1.6 noro 609: w = mat[i];
1.1 noro 610: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 611: STOZ(nd_work_vector[j],t1);
1.1 noro 612: mulz(w[j],t1,&t2);
613: addz(t,t2,&t1);
614: t = t1;
615: }
1.6 noro 616: if ( t ) {
617: s = sgnz(t);
1.1 noro 618: if ( s > 0 ) return 1;
619: else if ( s < 0 ) return -1;
1.6 noro 620: }
1.1 noro 621: }
1.6 noro 622: }
623: for ( i = 0; i < nd_matrix_len; i++ ) {
624: v = nd_matrix[i];
625: for ( j = 0, s = 0; j < nd_nvar; j++ )
626: s += v[j]*nd_work_vector[j];
627: if ( s > 0 ) return 1;
628: else if ( s < 0 ) return -1;
629: }
1.1 noro 630: if ( !ndl_equal(d1,d2) )
1.6 noro 631: error("ndl_matrix_compare : invalid matrix");
632: return 0;
1.1 noro 633: }
634:
635: int ndl_composite_compare(UINT *d1,UINT *d2)
636: {
637: int i,j,s,start,end,len,o;
638: int *v;
639: struct sparse_weight *sw;
640:
641: for ( j = 0; j < nd_nvar; j++ )
642: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
643: for ( i = 0; i < nd_worb_len; i++ ) {
644: len = nd_worb[i].length;
645: switch ( nd_worb[i].type ) {
646: case IS_DENSE_WEIGHT:
647: v = nd_worb[i].body.dense_weight;
648: for ( j = 0, s = 0; j < len; j++ )
649: s += v[j]*nd_work_vector[j];
650: if ( s > 0 ) return 1;
651: else if ( s < 0 ) return -1;
652: break;
653: case IS_SPARSE_WEIGHT:
654: sw = nd_worb[i].body.sparse_weight;
655: for ( j = 0, s = 0; j < len; j++ )
656: s += sw[j].value*nd_work_vector[sw[j].pos];
657: if ( s > 0 ) return 1;
658: else if ( s < 0 ) return -1;
659: break;
660: case IS_BLOCK:
661: o = nd_worb[i].body.block.order;
662: start = nd_worb[i].body.block.start;
663: switch ( o ) {
664: case 0:
665: end = start+len;
666: for ( j = start, s = 0; j < end; j++ )
667: s += MUL_WEIGHT(nd_work_vector[j],j);
668: if ( s > 0 ) return 1;
669: else if ( s < 0 ) return -1;
670: for ( j = end-1; j >= start; j-- )
671: if ( nd_work_vector[j] < 0 ) return 1;
672: else if ( nd_work_vector[j] > 0 ) return -1;
673: break;
674: case 1:
675: end = start+len;
676: for ( j = start, s = 0; j < end; j++ )
677: s += MUL_WEIGHT(nd_work_vector[j],j);
678: if ( s > 0 ) return 1;
679: else if ( s < 0 ) return -1;
680: for ( j = start; j < end; j++ )
681: if ( nd_work_vector[j] > 0 ) return 1;
682: else if ( nd_work_vector[j] < 0 ) return -1;
683: break;
684: case 2:
685: end = start+len;
686: for ( j = start; j < end; j++ )
687: if ( nd_work_vector[j] > 0 ) return 1;
688: else if ( nd_work_vector[j] < 0 ) return -1;
689: break;
690: }
691: break;
692: }
693: }
694: return 0;
695: }
696:
697: /* TDH -> WW -> TD-> RL */
698:
699: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
700: {
701: int i,m,e1,e2;
702:
703: if ( TD(d1) > TD(d2) ) return 1;
704: else if ( TD(d1) < TD(d2) ) return -1;
705: m = nd_nvar>>1;
706: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
707: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
708: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
709: }
710: if ( e1 > e2 ) return 1;
711: else if ( e1 < e2 ) return -1;
712: return ndl_lex_compare(d1,d2);
713: }
714:
1.21 noro 715: // common function for module glex and grlex comparison
716: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 717: {
1.21 noro 718: int c;
1.1 noro 719:
1.21 noro 720: switch ( nd_module_ordtype ) {
721: case 0:
722: if ( TD(d1) > TD(d2) ) return 1;
723: else if ( TD(d1) < TD(d2) ) return -1;
724: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
725: else if ( MPOS(d1) < MPOS(d2) ) return 1;
726: else if ( MPOS(d1) > MPOS(d2) ) return -1;
727: else return 0;
728: break;
1.1 noro 729:
1.21 noro 730: case 1:
1.19 noro 731: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
732: if ( TD(d1) > TD(d2) ) return 1;
733: else if ( TD(d1) < TD(d2) ) return -1;
734: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
735: if ( MPOS(d1) < MPOS(d2) ) return 1;
736: else if ( MPOS(d1) > MPOS(d2) ) return -1;
737: }
738: if ( MPOS(d1) < MPOS(d2) ) return 1;
739: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 740: else if ( TD(d1) > TD(d2) ) return 1;
741: else if ( TD(d1) < TD(d2) ) return -1;
742: else return ndl_lex_compare(d1,d2);
743: break;
1.1 noro 744:
1.21 noro 745: case 2: // weight -> POT
746: if ( TD(d1) > TD(d2) ) return 1;
747: else if ( TD(d1) < TD(d2) ) return -1;
748: else if ( MPOS(d1) < MPOS(d2) ) return 1;
749: else if ( MPOS(d1) > MPOS(d2) ) return -1;
750: else return ndl_lex_compare(d1,d2);
751: break;
1.1 noro 752:
1.21 noro 753: default:
754: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 755: return 0;
1.21 noro 756: }
1.1 noro 757: }
758:
1.21 noro 759: // common for module comparison
760: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 761: {
1.21 noro 762: int c;
1.1 noro 763:
1.21 noro 764: switch ( nd_module_ordtype ) {
765: case 0:
1.23 noro 766: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 767: else if ( MPOS(d1) > MPOS(d2) ) return -1;
768: else if ( MPOS(d1) < MPOS(d2) ) return 1;
769: else return 0;
770: break;
1.1 noro 771:
1.21 noro 772: case 1:
773: if ( MPOS(d1) < MPOS(d2) ) return 1;
774: else if ( MPOS(d1) > MPOS(d2) ) return -1;
775: else return (*ndl_base_compare_function)(d1,d2);
776: break;
1.1 noro 777:
1.21 noro 778: case 2: // weight -> POT
779: if ( TD(d1) > TD(d2) ) return 1;
780: else if ( TD(d1) < TD(d2) ) return -1;
781: else if ( MPOS(d1) < MPOS(d2) ) return 1;
782: else if ( MPOS(d1) > MPOS(d2) ) return -1;
783: else return (*ndl_base_compare_function)(d1,d2);
784: break;
1.1 noro 785:
1.21 noro 786: default:
787: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 788: return 0;
1.21 noro 789: }
1.1 noro 790: }
791:
1.21 noro 792: extern DMMstack dmm_stack;
793: void _addtodl(int n,DL d1,DL d2);
1.31 noro 794: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 795: int _eqdl(int n,DL d1,DL d2);
796:
797: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
798: {
799: int pos1,pos2,t,j;
800: DMM *in;
801: DMMstack s;
802: static DL d1=0;
803: static DL d2=0;
804: static int dlen=0;
805:
806: pos1 = MPOS(m1); pos2 = MPOS(m2);
807: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
808: if ( nd_nvar > dlen ) {
809: NEWDL(d1,nd_nvar);
810: NEWDL(d2,nd_nvar);
811: dlen = nd_nvar;
812: }
813: d1->td = TD(m1);
814: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
815: d2->td = TD(m2);
816: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
817: for ( s = dmm_stack; s; s = NEXT(s) ) {
818: in = s->in;
819: _addtodl(nd_nvar,in[pos1]->dl,d1);
820: _addtodl(nd_nvar,in[pos2]->dl,d2);
821: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
822: if ( pos1 < pos2 ) return 1;
823: else if ( pos1 > pos2 ) return -1;
824: else return 0;
825: }
826: pos1 = in[pos1]->pos;
827: pos2 = in[pos2]->pos;
828: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
829: }
830: // comparison by the bottom order
831: LAST:
832: switch ( nd_base_ordtype ) {
833: case 0:
834: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
835: if ( t ) return t;
836: else if ( pos1 < pos2 ) return 1;
837: else if ( pos1 > pos2 ) return -1;
838: else return 0;
839: break;
840: case 1:
841: if ( pos1 < pos2 ) return 1;
842: else if ( pos1 > pos2 ) return -1;
843: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
844: break;
845: case 2:
846: if ( d1->td > d2->td ) return 1;
847: else if ( d1->td < d2->td ) return -1;
848: else if ( pos1 < pos2 ) return 1;
849: else if ( pos1 > pos2 ) return -1;
850: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
851: break;
852: default:
853: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 854: return 0;
1.21 noro 855: }
1.1 noro 856: }
857:
858: INLINE int ndl_equal(UINT *d1,UINT *d2)
859: {
860: int i;
861:
862: switch ( nd_wpd ) {
863: case 2:
864: if ( TD(d2) != TD(d1) ) return 0;
865: if ( d2[1] != d1[1] ) return 0;
866: return 1;
867: break;
868: case 3:
869: if ( TD(d2) != TD(d1) ) return 0;
870: if ( d2[1] != d1[1] ) return 0;
871: if ( d2[2] != d1[2] ) return 0;
872: return 1;
873: break;
874: default:
875: for ( i = 0; i < nd_wpd; i++ )
876: if ( *d1++ != *d2++ ) return 0;
877: return 1;
878: break;
879: }
880: }
881:
882: INLINE void ndl_copy(UINT *d1,UINT *d2)
883: {
884: int i;
885:
886: switch ( nd_wpd ) {
887: case 2:
888: TD(d2) = TD(d1);
889: d2[1] = d1[1];
890: break;
891: case 3:
892: TD(d2) = TD(d1);
893: d2[1] = d1[1];
894: d2[2] = d1[2];
895: break;
896: default:
897: for ( i = 0; i < nd_wpd; i++ )
898: d2[i] = d1[i];
899: break;
900: }
901: }
902:
903: INLINE void ndl_zero(UINT *d)
904: {
905: int i;
906: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
907: }
908:
909: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
910: {
911: int i;
912:
913: if ( nd_module ) {
914: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
915: error("ndl_add : invalid operation");
916: }
917: #if 1
918: switch ( nd_wpd ) {
919: case 2:
920: TD(d) = TD(d1)+TD(d2);
921: d[1] = d1[1]+d2[1];
922: break;
923: case 3:
924: TD(d) = TD(d1)+TD(d2);
925: d[1] = d1[1]+d2[1];
926: d[2] = d1[2]+d2[2];
927: break;
928: default:
929: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
930: break;
931: }
932: #else
933: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
934: #endif
935: }
936:
937: /* d1 += d2 */
938: INLINE void ndl_addto(UINT *d1,UINT *d2)
939: {
940: int i;
941:
942: if ( nd_module ) {
943: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
944: error("ndl_addto : invalid operation");
945: }
946: #if 1
947: switch ( nd_wpd ) {
948: case 2:
949: TD(d1) += TD(d2);
950: d1[1] += d2[1];
951: break;
952: case 3:
953: TD(d1) += TD(d2);
954: d1[1] += d2[1];
955: d1[2] += d2[2];
956: break;
957: default:
958: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
959: break;
960: }
961: #else
962: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
963: #endif
964: }
965:
966: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
967: {
968: int i;
969:
970: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
971: }
972:
973: int ndl_disjoint(UINT *d1,UINT *d2)
974: {
975: UINT t1,t2,u,u1,u2;
976: int i,j;
977:
978: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
979: #if USE_UNROLL
980: switch ( nd_bpe ) {
981: case 3:
982: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
983: u1 = d1[i]; u2 = d2[i];
984: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
985: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
986: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
987: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
988: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
989: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
990: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
991: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
992: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
993: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
994: }
995: return 1;
996: break;
997: case 4:
998: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
999: u1 = d1[i]; u2 = d2[i];
1000: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1001: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1002: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1003: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1004: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1005: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1007: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1008: }
1009: return 1;
1010: break;
1011: case 6:
1012: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1013: u1 = d1[i]; u2 = d2[i];
1014: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1015: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1017: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1018: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1019: }
1020: return 1;
1021: break;
1022: case 8:
1023: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1024: u1 = d1[i]; u2 = d2[i];
1025: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1027: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1028: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1029: }
1030: return 1;
1031: break;
1032: case 16:
1033: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1034: u1 = d1[i]; u2 = d2[i];
1035: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1036: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1037: }
1038: return 1;
1039: break;
1040: case 32:
1041: for ( i = nd_exporigin; i < nd_wpd; i++ )
1042: if ( d1[i] && d2[i] ) return 0;
1043: return 1;
1044: break;
1045: default:
1046: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1047: u1 = d1[i]; u2 = d2[i];
1048: for ( j = 0; j < nd_epw; j++ ) {
1049: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1050: u1 >>= nd_bpe; u2 >>= nd_bpe;
1051: }
1052: }
1053: return 1;
1054: break;
1055: }
1056: #else
1057: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1058: u1 = d1[i]; u2 = d2[i];
1059: for ( j = 0; j < nd_epw; j++ ) {
1060: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1061: u1 >>= nd_bpe; u2 >>= nd_bpe;
1062: }
1063: }
1064: return 1;
1065: #endif
1066: }
1067:
1068: int ndl_check_bound(UINT *d1,UINT *d2)
1069: {
1070: UINT u2;
1071: int i,j,ind,k;
1072:
1073: ind = 0;
1074: #if USE_UNROLL
1075: switch ( nd_bpe ) {
1076: case 3:
1077: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1078: u2 = d2[i];
1079: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1080: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1081: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1082: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1083: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1084: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1085: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1086: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1087: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1088: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1089: }
1090: return 0;
1091: break;
1092: case 4:
1093: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1094: u2 = d2[i];
1095: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1096: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1097: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1098: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1099: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1100: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1101: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1102: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1103: }
1104: return 0;
1105: break;
1106: case 6:
1107: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1108: u2 = d2[i];
1109: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1110: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1111: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1112: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1113: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1114: }
1115: return 0;
1116: break;
1117: case 8:
1118: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1119: u2 = d2[i];
1120: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1121: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1122: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1123: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1124: }
1125: return 0;
1126: break;
1127: case 16:
1128: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1129: u2 = d2[i];
1130: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1131: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1132: }
1133: return 0;
1134: break;
1135: case 32:
1136: for ( i = nd_exporigin; i < nd_wpd; i++ )
1137: if ( d1[i]+d2[i]<d1[i] ) return 1;
1138: return 0;
1139: break;
1140: default:
1141: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1142: u2 = d2[i];
1143: k = (nd_epw-1)*nd_bpe;
1144: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1145: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1146: }
1147: return 0;
1148: break;
1149: }
1150: #else
1151: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1152: u2 = d2[i];
1153: k = (nd_epw-1)*nd_bpe;
1154: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1155: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1156: }
1157: return 0;
1158: #endif
1159: }
1160:
1161: int ndl_check_bound2(int index,UINT *d2)
1162: {
1163: return ndl_check_bound(nd_bound[index],d2);
1164: }
1165:
1166: INLINE int ndl_hash_value(UINT *d)
1167: {
1168: int i;
1.11 noro 1169: UINT r;
1.1 noro 1170:
1171: r = 0;
1172: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1173: r = (r*1511+d[i]);
1.11 noro 1174: r %= REDTAB_LEN;
1.1 noro 1175: return r;
1176: }
1177:
1178: INLINE int ndl_find_reducer(UINT *dg)
1179: {
1180: RHist r;
1181: int d,k,i;
1182:
1183: d = ndl_hash_value(dg);
1184: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1185: if ( ndl_equal(dg,DL(r)) ) {
1186: if ( k > 0 ) nd_notfirst++;
1187: nd_found++;
1188: return r->index;
1189: }
1190: }
1191: if ( Reverse )
1192: for ( i = nd_psn-1; i >= 0; i-- ) {
1193: r = nd_psh[i];
1194: if ( ndl_reducible(dg,DL(r)) ) {
1195: nd_create++;
1196: nd_append_red(dg,i);
1197: return i;
1198: }
1199: }
1200: else
1201: for ( i = 0; i < nd_psn; i++ ) {
1202: r = nd_psh[i];
1203: if ( ndl_reducible(dg,DL(r)) ) {
1204: nd_create++;
1205: nd_append_red(dg,i);
1206: return i;
1207: }
1208: }
1209: return -1;
1210: }
1211:
1.41 noro 1212: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1213: {
1214: RHist r;
1215: int i;
1216:
1217: for ( i = 0; i < nd_psn; i++ ) {
1218: r = nd_psh[i];
1219: if ( ndl_reducible(dg,DL(r)) ) return i;
1220: }
1221: return -1;
1222: }
1223:
1.24 noro 1224: // ret=0,...,nd_psn-1 => reducer found
1225: // ret=nd_psn => reducer not found
1226: // ret=-1 => singular top reducible
1227:
1228: int comp_sig(SIG s1,SIG s2);
1229: void _ndltodl(UINT *ndl,DL dl);
1230:
1231: void print_sig(SIG s)
1232: {
1233: int i;
1234:
1235: fprintf(asir_out,"<<");
1236: for ( i = 0; i < nd_nvar; i++ ) {
1237: fprintf(asir_out,"%d",s->dl->d[i]);
1238: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1239: }
1240: fprintf(asir_out,">>*e%d",s->pos);
1241: }
1242:
1.35 noro 1243: // assuming increasing order wrt signature
1244:
1.24 noro 1245: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1246: {
1247: RHist r;
1.27 noro 1248: int i,singular,ret,d,k;
1.26 noro 1249: static int wpd,nvar;
1.24 noro 1250: static SIG quo;
1251: static UINT *tmp;
1252:
1.26 noro 1253: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1254: if ( wpd != nd_wpd ) {
1.24 noro 1255: wpd = nd_wpd;
1256: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1257: }
1.27 noro 1258: d = ndl_hash_value(dg);
1.35 noro 1259: #if 1
1.27 noro 1260: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1261: if ( ndl_equal(dg,DL(r)) ) {
1262: return r->index;
1263: }
1264: }
1.35 noro 1265: #endif
1.24 noro 1266: singular = 0;
1267: for ( i = 0; i < nd_psn; i++ ) {
1268: r = nd_psh[i];
1269: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1270: ndl_sub(dg,DL(r),tmp);
1.24 noro 1271: _ndltodl(tmp,DL(quo));
1272: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1273: quo->pos = nd_psh[i]->sig->pos;
1274: ret = comp_sig(sig,quo);
1275: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1276: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1277: }
1278: }
1279: if ( singular ) return -1;
1.27 noro 1280: else if ( i < nd_psn )
1281: nd_append_red(dg,i);
1282: return i;
1.24 noro 1283: }
1284:
1.1 noro 1285: ND nd_merge(ND p1,ND p2)
1286: {
1287: int n,c;
1288: int t,can,td1,td2;
1289: ND r;
1290: NM m1,m2,mr0,mr,s;
1291:
1292: if ( !p1 ) return p2;
1293: else if ( !p2 ) return p1;
1294: else {
1295: can = 0;
1296: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1297: c = DL_COMPARE(DL(m1),DL(m2));
1298: switch ( c ) {
1299: case 0:
1300: s = m1; m1 = NEXT(m1);
1301: can++; NEXTNM2(mr0,mr,s);
1302: s = m2; m2 = NEXT(m2); FREENM(s);
1303: break;
1304: case 1:
1305: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1306: break;
1307: case -1:
1308: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1309: break;
1310: }
1311: }
1312: if ( !mr0 )
1313: if ( m1 ) mr0 = m1;
1314: else if ( m2 ) mr0 = m2;
1315: else return 0;
1316: else if ( m1 ) NEXT(mr) = m1;
1317: else if ( m2 ) NEXT(mr) = m2;
1318: else NEXT(mr) = 0;
1319: BDY(p1) = mr0;
1320: SG(p1) = MAX(SG(p1),SG(p2));
1321: LEN(p1) = LEN(p1)+LEN(p2)-can;
1322: FREEND(p2);
1323: return p1;
1324: }
1325: }
1326:
1327: ND nd_add(int mod,ND p1,ND p2)
1328: {
1329: int n,c;
1330: int t,can,td1,td2;
1331: ND r;
1332: NM m1,m2,mr0,mr,s;
1333:
1.11 noro 1334: Nnd_add++;
1.1 noro 1335: if ( !p1 ) return p2;
1336: else if ( !p2 ) return p1;
1337: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1338: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1339: else if ( !mod ) return nd_add_q(p1,p2);
1340: else {
1341: can = 0;
1342: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1343: c = DL_COMPARE(DL(m1),DL(m2));
1344: switch ( c ) {
1345: case 0:
1346: t = ((CM(m1))+(CM(m2))) - mod;
1347: if ( t < 0 ) t += mod;
1348: s = m1; m1 = NEXT(m1);
1349: if ( t ) {
1350: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1351: } else {
1352: can += 2; FREENM(s);
1353: }
1354: s = m2; m2 = NEXT(m2); FREENM(s);
1355: break;
1356: case 1:
1357: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1358: break;
1359: case -1:
1360: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1361: break;
1362: }
1363: }
1364: if ( !mr0 )
1365: if ( m1 ) mr0 = m1;
1366: else if ( m2 ) mr0 = m2;
1367: else return 0;
1368: else if ( m1 ) NEXT(mr) = m1;
1369: else if ( m2 ) NEXT(mr) = m2;
1370: else NEXT(mr) = 0;
1371: BDY(p1) = mr0;
1372: SG(p1) = MAX(SG(p1),SG(p2));
1373: LEN(p1) = LEN(p1)+LEN(p2)-can;
1374: FREEND(p2);
1375: return p1;
1376: }
1377: }
1378:
1379: /* XXX on opteron, the inlined manipulation of destructive additon of
1380: * two NM seems to make gcc optimizer get confused, so the part is
1381: * done in a function.
1382: */
1383:
1384: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1385: {
1386: NM s;
1387: P t;
1388: int can;
1389:
1390: addp(nd_vc,CP(*m1),CP(*m2),&t);
1391: s = *m1; *m1 = NEXT(*m1);
1392: if ( t ) {
1393: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1394: } else {
1395: can = 2; FREENM(s);
1396: }
1397: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1398: return can;
1399: }
1400:
1401: ND nd_add_q(ND p1,ND p2)
1402: {
1403: int n,c,can;
1404: ND r;
1405: NM m1,m2,mr0,mr,s;
1406: P t;
1407:
1408: if ( !p1 ) return p2;
1409: else if ( !p2 ) return p1;
1410: else {
1411: can = 0;
1412: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1413: c = DL_COMPARE(DL(m1),DL(m2));
1414: switch ( c ) {
1415: case 0:
1416: #if defined(__x86_64__)
1417: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1418: #else
1419: addp(nd_vc,CP(m1),CP(m2),&t);
1420: s = m1; m1 = NEXT(m1);
1421: if ( t ) {
1422: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1423: } else {
1424: can += 2; FREENM(s);
1425: }
1426: s = m2; m2 = NEXT(m2); FREENM(s);
1427: #endif
1428: break;
1429: case 1:
1430: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1431: break;
1432: case -1:
1433: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1434: break;
1435: }
1436: }
1437: if ( !mr0 )
1438: if ( m1 ) mr0 = m1;
1439: else if ( m2 ) mr0 = m2;
1440: else return 0;
1441: else if ( m1 ) NEXT(mr) = m1;
1442: else if ( m2 ) NEXT(mr) = m2;
1443: else NEXT(mr) = 0;
1444: BDY(p1) = mr0;
1445: SG(p1) = MAX(SG(p1),SG(p2));
1446: LEN(p1) = LEN(p1)+LEN(p2)-can;
1447: FREEND(p2);
1448: return p1;
1449: }
1450: }
1451:
1452: ND nd_add_sf(ND p1,ND p2)
1453: {
1454: int n,c,can;
1455: ND r;
1456: NM m1,m2,mr0,mr,s;
1457: int t;
1458:
1459: if ( !p1 ) return p2;
1460: else if ( !p2 ) return p1;
1461: else {
1462: can = 0;
1463: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1464: c = DL_COMPARE(DL(m1),DL(m2));
1465: switch ( c ) {
1466: case 0:
1467: t = _addsf(CM(m1),CM(m2));
1468: s = m1; m1 = NEXT(m1);
1469: if ( t ) {
1470: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1471: } else {
1472: can += 2; FREENM(s);
1473: }
1474: s = m2; m2 = NEXT(m2); FREENM(s);
1475: break;
1476: case 1:
1477: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1478: break;
1479: case -1:
1480: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1481: break;
1482: }
1483: }
1484: if ( !mr0 )
1485: if ( m1 ) mr0 = m1;
1486: else if ( m2 ) mr0 = m2;
1487: else return 0;
1488: else if ( m1 ) NEXT(mr) = m1;
1489: else if ( m2 ) NEXT(mr) = m2;
1490: else NEXT(mr) = 0;
1491: BDY(p1) = mr0;
1492: SG(p1) = MAX(SG(p1),SG(p2));
1493: LEN(p1) = LEN(p1)+LEN(p2)-can;
1494: FREEND(p2);
1495: return p1;
1496: }
1497: }
1498:
1499:
1500: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1501: {
1502: int c,c1,c2;
1503: Z cg,cred,gcd,tq;
1504: P cgp,credp,gcdp;
1505: Obj tr,tr1;
1506:
1507: if ( mod == -1 ) {
1508: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1509: *divp = (Obj)ONE;
1510: } else if ( mod == -2 ) {
1511: Z inv,t;
1512: divlf(ONE,HCZ(p),&inv);
1513: chsgnlf(HCZ(g),&t);
1514: mullf(inv,t,&CZ(mul));
1515: *divp = (Obj)ONE;
1516: } else if ( mod ) {
1517: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1518: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1519: *divp = (Obj)ONE;
1520: } else if ( nd_vc ) {
1521: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1522: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1523: chsgnp(cgp,&CP(mul));
1524: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1525: if ( dn ) {
1526: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1527: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1528: }
1529: *divp = (Obj)credp;
1530: } else {
1.6 noro 1531: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1532: chsgnz(cg,&CZ(mul));
1.1 noro 1533: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1534: if ( dn ) {
1535: mulz(dn->z,cred,&tq); dn->z = tq;
1536: }
1537: *divp = (Obj)cred;
1538: }
1539: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1540: }
1541:
1542: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1543: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1544: {
1545: NM m,mrd,tail;
1546: NM mul;
1547: int n,sugar,psugar,sugar0,stat,index;
1548: int c,c1,c2,dummy;
1549: RHist h;
1550: NDV p,red;
1551: Q cg,cred,gcd,tq,qq;
1552: Z iq;
1553: DP dmul;
1554: NODE node;
1555: LIST hist;
1556: double hmag;
1557: P tp,tp1;
1558: Obj tr,tr1,div;
1559: union oNDC hg;
1560: P cont;
1561:
1562: if ( !g ) {
1563: *rp = d;
1564: return 1;
1565: }
1566: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1567:
1568: sugar0 = sugar = SG(g);
1569: n = NV(g);
1570: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1571: if ( d )
1572: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1573: for ( ; g; ) {
1574: index = ndl_find_reducer(HDL(g));
1575: if ( index >= 0 ) {
1576: h = nd_psh[index];
1577: ndl_sub(HDL(g),DL(h),DL(mul));
1578: if ( ndl_check_bound2(index,DL(mul)) ) {
1579: nd_free(g); nd_free(d);
1580: return 0;
1581: }
1582: p = nd_demand ? ndv_load(index) : ps[index];
1583: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1584: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1585: if ( nd_gentrace ) {
1586: /* Trace=[div,index,mul,ONE] */
1.6 noro 1587: STOZ(index,iq);
1.1 noro 1588: nmtodp(mod,mul,&dmul);
1589: node = mknode(4,div,iq,dmul,ONE);
1590: }
1591: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1592: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1593: hg = HCU(g);
1594: nd_removecont2(d,g);
1.6 noro 1595: if ( nd_gentrace ) {
1.1 noro 1596: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1597: /* exact division */
1.1 noro 1598: cont = ndc_div(mod,hg,HCU(g));
1599: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1600: }
1601: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1602: }
1603: MKLIST(hist,node);
1604: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1605: } else if ( !full ) {
1606: *rp = g;
1607: return 1;
1608: } else {
1609: m = BDY(g);
1610: if ( NEXT(m) ) {
1611: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1612: } else {
1613: FREEND(g); g = 0;
1614: }
1615: if ( d ) {
1616: NEXT(tail)=m; tail=m; LEN(d)++;
1617: } else {
1618: MKND(n,m,1,d); tail = BDY(d);
1619: }
1620: }
1621: }
1622: if ( d ) SG(d) = sugar;
1623: *rp = d;
1624: return 1;
1625: }
1626:
1.24 noro 1627: // ret=1 => success
1628: // ret=0 => overflow
1629: // ret=-1 => singular top reducible
1630:
1631: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1632: {
1633: NM m,mrd,tail;
1634: NM mul;
1635: int n,sugar,psugar,sugar0,stat,index;
1636: int c,c1,c2,dummy;
1637: RHist h;
1638: NDV p,red;
1639: Q cg,cred,gcd,tq,qq;
1640: Z iq;
1641: DP dmul;
1642: NODE node;
1643: LIST hist;
1644: double hmag;
1645: P tp,tp1;
1646: Obj tr,tr1,div;
1647: union oNDC hg;
1648: P cont;
1649: SIG sig;
1650:
1651: if ( !g ) {
1652: *rp = d;
1653: return 1;
1654: }
1655: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1656:
1657: sugar0 = sugar = SG(g);
1658: n = NV(g);
1659: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1660: if ( d )
1661: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1662: sig = g->sig;
1663: for ( ; g; ) {
1664: index = ndl_find_reducer_s(HDL(g),sig);
1665: if ( index >= 0 && index < nd_psn ) {
1666: // reducer found
1667: h = nd_psh[index];
1668: ndl_sub(HDL(g),DL(h),DL(mul));
1669: if ( ndl_check_bound2(index,DL(mul)) ) {
1670: nd_free(g); nd_free(d);
1671: return 0;
1672: }
1673: p = ps[index];
1674: /* d+g -> div*(d+g)+mul*p */
1675: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1676: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1677: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1678: hg = HCU(g);
1679: nd_removecont2(d,g);
1680: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1681: }
1682: } else if ( index == -1 ) {
1683: // singular top reducible
1684: return -1;
1685: } else if ( !full ) {
1686: *rp = g;
1687: g->sig = sig;
1688: return 1;
1689: } else {
1690: m = BDY(g);
1691: if ( NEXT(m) ) {
1692: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1693: } else {
1694: FREEND(g); g = 0;
1695: }
1696: if ( d ) {
1697: NEXT(tail)=m; tail=m; LEN(d)++;
1698: } else {
1699: MKND(n,m,1,d); tail = BDY(d);
1700: }
1701: }
1702: }
1703: if ( d ) {
1704: SG(d) = sugar;
1705: d->sig = sig;
1706: }
1707: *rp = d;
1708: return 1;
1709: }
1710:
1.1 noro 1711: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1712: {
1713: int hindex,index;
1714: NDV p;
1715: ND u,d,red;
1716: NODE l;
1717: NM mul,m,mrd,tail;
1718: int sugar,psugar,n,h_reducible;
1719: PGeoBucket bucket;
1720: int c,c1,c2;
1721: Z cg,cred,gcd,zzz;
1722: RHist h;
1723: double hmag,gmag;
1724: int count = 0;
1725: int hcount = 0;
1726:
1727: if ( !g ) {
1728: *rp = 0;
1729: return 1;
1730: }
1731: sugar = SG(g);
1732: n = NV(g);
1.6 noro 1733: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1734: bucket = create_pbucket();
1735: add_pbucket(mod,bucket,g);
1736: d = 0;
1737: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1738: while ( 1 ) {
1739: if ( mod > 0 || mod == -1 )
1740: hindex = head_pbucket(mod,bucket);
1741: else if ( mod == -2 )
1742: hindex = head_pbucket_lf(bucket);
1743: else
1744: hindex = head_pbucket_q(bucket);
1745: if ( hindex < 0 ) {
1746: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1747: if ( d ) SG(d) = sugar;
1748: *rp = d;
1749: return 1;
1750: }
1751: g = bucket->body[hindex];
1752: index = ndl_find_reducer(HDL(g));
1753: if ( index >= 0 ) {
1754: count++;
1755: if ( !d ) hcount++;
1756: h = nd_psh[index];
1757: ndl_sub(HDL(g),DL(h),DL(mul));
1758: if ( ndl_check_bound2(index,DL(mul)) ) {
1759: nd_free(d);
1760: free_pbucket(bucket);
1761: *rp = 0;
1762: return 0;
1763: }
1764: p = ps[index];
1765: if ( mod == -1 )
1766: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1767: else if ( mod == -2 ) {
1768: Z inv,t;
1769: divlf(ONE,HCZ(p),&inv);
1770: chsgnlf(HCZ(g),&t);
1771: mullf(inv,t,&CZ(mul));
1772: } else if ( mod ) {
1773: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1774: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1775: } else {
1.6 noro 1776: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1777: chsgnz(cg,&CZ(mul));
1.1 noro 1778: nd_mul_c_q(d,(P)cred);
1779: mulq_pbucket(bucket,cred);
1780: g = bucket->body[hindex];
1.6 noro 1781: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1782: }
1783: red = ndv_mul_nm(mod,mul,p);
1784: bucket->body[hindex] = nd_remove_head(g);
1785: red = nd_remove_head(red);
1786: add_pbucket(mod,bucket,red);
1787: psugar = SG(p)+TD(DL(mul));
1788: sugar = MAX(sugar,psugar);
1789: if ( !mod && hmag && (gmag > hmag) ) {
1790: g = normalize_pbucket(mod,bucket);
1791: if ( !g ) {
1792: if ( d ) SG(d) = sugar;
1793: *rp = d;
1794: return 1;
1795: }
1796: nd_removecont2(d,g);
1.6 noro 1797: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1798: add_pbucket(mod,bucket,g);
1799: }
1800: } else if ( !full ) {
1801: g = normalize_pbucket(mod,bucket);
1802: if ( g ) SG(g) = sugar;
1803: *rp = g;
1804: return 1;
1805: } else {
1806: m = BDY(g);
1807: if ( NEXT(m) ) {
1808: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1809: } else {
1810: FREEND(g); g = 0;
1811: }
1812: bucket->body[hindex] = g;
1813: NEXT(m) = 0;
1814: if ( d ) {
1815: NEXT(tail)=m; tail=m; LEN(d)++;
1816: } else {
1817: MKND(n,m,1,d); tail = BDY(d);
1818: }
1819: }
1820: }
1821: }
1822:
1.25 noro 1823: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1824: {
1825: int hindex,index;
1826: NDV p;
1827: ND u,d,red;
1828: NODE l;
1829: NM mul,m,mrd,tail;
1830: int sugar,psugar,n,h_reducible;
1831: PGeoBucket bucket;
1832: int c,c1,c2;
1833: Z cg,cred,gcd,zzz;
1834: RHist h;
1835: double hmag,gmag;
1836: int count = 0;
1837: int hcount = 0;
1838: SIG sig;
1839:
1840: if ( !g ) {
1841: *rp = 0;
1842: return 1;
1843: }
1844: sugar = SG(g);
1845: n = NV(g);
1846: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1847: bucket = create_pbucket();
1848: add_pbucket(mod,bucket,g);
1849: d = 0;
1850: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1851: sig = g->sig;
1852: while ( 1 ) {
1853: if ( mod > 0 || mod == -1 )
1854: hindex = head_pbucket(mod,bucket);
1855: else if ( mod == -2 )
1856: hindex = head_pbucket_lf(bucket);
1857: else
1858: hindex = head_pbucket_q(bucket);
1859: if ( hindex < 0 ) {
1860: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1861: if ( d ) {
1862: SG(d) = sugar;
1863: d->sig = sig;
1864: }
1865: *rp = d;
1866: return 1;
1867: }
1868: g = bucket->body[hindex];
1869: index = ndl_find_reducer_s(HDL(g),sig);
1870: if ( index >= 0 && index < nd_psn ) {
1871: count++;
1872: if ( !d ) hcount++;
1873: h = nd_psh[index];
1874: ndl_sub(HDL(g),DL(h),DL(mul));
1875: if ( ndl_check_bound2(index,DL(mul)) ) {
1876: nd_free(d);
1877: free_pbucket(bucket);
1878: *rp = 0;
1879: return 0;
1880: }
1881: p = ps[index];
1882: if ( mod == -1 )
1883: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1884: else if ( mod == -2 ) {
1885: Z inv,t;
1886: divlf(ONE,HCZ(p),&inv);
1887: chsgnlf(HCZ(g),&t);
1888: mullf(inv,t,&CZ(mul));
1889: } else if ( mod ) {
1890: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1891: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1892: } else {
1893: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1894: chsgnz(cg,&CZ(mul));
1895: nd_mul_c_q(d,(P)cred);
1896: mulq_pbucket(bucket,cred);
1897: g = bucket->body[hindex];
1898: gmag = (double)p_mag((P)HCZ(g));
1899: }
1900: red = ndv_mul_nm(mod,mul,p);
1901: bucket->body[hindex] = nd_remove_head(g);
1902: red = nd_remove_head(red);
1903: add_pbucket(mod,bucket,red);
1904: psugar = SG(p)+TD(DL(mul));
1905: sugar = MAX(sugar,psugar);
1906: if ( !mod && hmag && (gmag > hmag) ) {
1907: g = normalize_pbucket(mod,bucket);
1908: if ( !g ) {
1909: if ( d ) {
1910: SG(d) = sugar;
1911: d->sig = sig;
1912: }
1913: *rp = d;
1914: return 1;
1915: }
1916: nd_removecont2(d,g);
1917: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1918: add_pbucket(mod,bucket,g);
1919: }
1920: } else if ( index == -1 ) {
1921: // singular top reducible
1922: return -1;
1923: } else if ( !full ) {
1924: g = normalize_pbucket(mod,bucket);
1925: if ( g ) {
1926: SG(g) = sugar;
1927: g->sig = sig;
1928: }
1929: *rp = g;
1930: return 1;
1931: } else {
1932: m = BDY(g);
1933: if ( NEXT(m) ) {
1934: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1935: } else {
1936: FREEND(g); g = 0;
1937: }
1938: bucket->body[hindex] = g;
1939: NEXT(m) = 0;
1940: if ( d ) {
1941: NEXT(tail)=m; tail=m; LEN(d)++;
1942: } else {
1943: MKND(n,m,1,d); tail = BDY(d);
1944: }
1945: }
1946: }
1947: }
1948:
1.1 noro 1949: /* input : list of NDV, cand : list of NDV */
1950:
1951: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1952: {
1953: int n,i,stat;
1954: ND nf,d;
1955: NDV r;
1956: NODE t,s;
1957: union oNDC dn;
1958: Z q;
1959: LIST list;
1960:
1.24 noro 1961: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1962: n = length(cand);
1963:
1964: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1965: /* membercheck : list is a subset of Id(cand) ? */
1966: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1967: again:
1968: nd_tracelist = 0;
1969: if ( nd_bpe > obpe )
1970: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1971: else
1972: r = (NDV)BDY(t);
1973: #if 0
1974: // moved to nd_f4_lf_trace()
1975: if ( m == -2 ) ndv_mod(m,r);
1976: #endif
1977: d = ndvtond(m,r);
1.6 noro 1978: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1979: if ( !stat ) {
1980: nd_reconstruct(0,0);
1981: goto again;
1982: } else if ( nf ) return 0;
1983: if ( nd_gentrace ) {
1984: nd_tracelist = reverse_node(nd_tracelist);
1985: MKLIST(list,nd_tracelist);
1.6 noro 1986: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1987: MKNODE(s,list,nd_alltracelist);
1988: nd_alltracelist = s; nd_tracelist = 0;
1989: }
1990: if ( DP_Print ) { printf("."); fflush(stdout); }
1991: }
1992: if ( DP_Print ) { printf("\n"); }
1993: return 1;
1994: }
1995:
1996: ND nd_remove_head(ND p)
1997: {
1998: NM m;
1999:
2000: m = BDY(p);
2001: if ( !NEXT(m) ) {
2002: FREEND(p); p = 0;
2003: } else {
2004: BDY(p) = NEXT(m); LEN(p)--;
2005: }
2006: FREENM(m);
2007: return p;
2008: }
2009:
2010: ND nd_separate_head(ND p,ND *head)
2011: {
2012: NM m,m0;
2013: ND r;
2014:
2015: m = BDY(p);
2016: if ( !NEXT(m) ) {
2017: *head = p; p = 0;
2018: } else {
2019: m0 = m;
2020: BDY(p) = NEXT(m); LEN(p)--;
2021: NEXT(m0) = 0;
2022: MKND(NV(p),m0,1,r);
2023: *head = r;
2024: }
2025: return p;
2026: }
2027:
2028: PGeoBucket create_pbucket()
2029: {
2030: PGeoBucket g;
2031:
2032: g = CALLOC(1,sizeof(struct oPGeoBucket));
2033: g->m = -1;
2034: return g;
2035: }
2036:
2037: void free_pbucket(PGeoBucket b) {
2038: int i;
2039:
2040: for ( i = 0; i <= b->m; i++ )
2041: if ( b->body[i] ) {
2042: nd_free(b->body[i]);
2043: b->body[i] = 0;
2044: }
2045: GCFREE(b);
2046: }
2047:
1.39 noro 2048: #if 0
1.1 noro 2049: void add_pbucket_symbolic(PGeoBucket g,ND d)
2050: {
2051: int l,i,k,m;
2052:
2053: if ( !d )
2054: return;
2055: l = LEN(d);
2056: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2057: /* 2^(k-1) < l <= 2^k (=m) */
2058: d = nd_merge(g->body[k],d);
2059: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2060: g->body[k] = 0;
2061: d = nd_merge(g->body[k+1],d);
2062: }
2063: g->body[k] = d;
2064: g->m = MAX(g->m,k);
2065: }
1.39 noro 2066: #else
2067: void add_pbucket_symbolic(PGeoBucket g,ND d)
2068: {
2069: int l,i,k,m,m0;
2070:
2071: if ( !d )
2072: return;
2073: m0 = g->m;
2074: while ( 1 ) {
2075: l = LEN(d);
2076: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2077: /* 2^(k-1) < l <= 2^k (=m) */
2078: if ( g->body[k] == 0 ) {
2079: g->body[k] = d;
2080: m0 = MAX(k,m0);
2081: break;
2082: } else {
2083: d = nd_merge(g->body[k],d);
2084: g->body[k] = 0;
2085: }
2086: }
2087: g->m = m0;
2088: }
2089: #endif
1.1 noro 2090:
1.39 noro 2091: #if 0
1.1 noro 2092: void add_pbucket(int mod,PGeoBucket g,ND d)
2093: {
2094: int l,i,k,m;
2095:
2096: if ( !d )
2097: return;
2098: l = LEN(d);
2099: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2100: /* 2^(k-1) < l <= 2^k (=m) */
2101: d = nd_add(mod,g->body[k],d);
2102: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2103: g->body[k] = 0;
2104: d = nd_add(mod,g->body[k+1],d);
2105: }
2106: g->body[k] = d;
2107: g->m = MAX(g->m,k);
2108: }
1.39 noro 2109: #else
2110: void add_pbucket(int mod,PGeoBucket g,ND d)
2111: {
2112: int l,i,k,m,m0;
2113:
2114: m0 = g->m;
2115: while ( d != 0 ) {
2116: l = LEN(d);
2117: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2118: /* 2^(k-1) < l <= 2^k (=m) */
2119: if ( g->body[k] == 0 ) {
2120: g->body[k] = d;
2121: m0 = MAX(k,m0);
2122: break;
2123: } else {
2124: d = nd_add(mod,g->body[k],d);
2125: g->body[k] = 0;
2126: }
2127: }
2128: g->m = m0;
2129: }
2130: #endif
1.1 noro 2131:
2132: void mulq_pbucket(PGeoBucket g,Z c)
2133: {
2134: int k;
2135:
2136: for ( k = 0; k <= g->m; k++ )
2137: nd_mul_c_q(g->body[k],(P)c);
2138: }
2139:
2140: NM remove_head_pbucket_symbolic(PGeoBucket g)
2141: {
2142: int j,i,k,c;
2143: NM head;
2144:
2145: k = g->m;
2146: j = -1;
2147: for ( i = 0; i <= k; i++ ) {
2148: if ( !g->body[i] ) continue;
2149: if ( j < 0 ) j = i;
2150: else {
2151: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2152: if ( c > 0 )
2153: j = i;
2154: else if ( c == 0 )
2155: g->body[i] = nd_remove_head(g->body[i]);
2156: }
2157: }
2158: if ( j < 0 ) return 0;
2159: else {
2160: head = BDY(g->body[j]);
2161: if ( !NEXT(head) ) {
2162: FREEND(g->body[j]);
2163: g->body[j] = 0;
2164: } else {
2165: BDY(g->body[j]) = NEXT(head);
2166: LEN(g->body[j])--;
2167: }
2168: return head;
2169: }
2170: }
2171:
2172: int head_pbucket(int mod,PGeoBucket g)
2173: {
2174: int j,i,c,k,nv,sum;
2175: UINT *di,*dj;
2176: ND gi,gj;
2177:
2178: k = g->m;
2179: while ( 1 ) {
2180: j = -1;
2181: for ( i = 0; i <= k; i++ ) {
2182: if ( !(gi = g->body[i]) )
2183: continue;
2184: if ( j < 0 ) {
2185: j = i;
2186: gj = g->body[j];
2187: dj = HDL(gj);
2188: sum = HCM(gj);
2189: } else {
2190: c = DL_COMPARE(HDL(gi),dj);
2191: if ( c > 0 ) {
2192: if ( sum ) HCM(gj) = sum;
2193: else g->body[j] = nd_remove_head(gj);
2194: j = i;
2195: gj = g->body[j];
2196: dj = HDL(gj);
2197: sum = HCM(gj);
2198: } else if ( c == 0 ) {
2199: if ( mod == -1 )
2200: sum = _addsf(sum,HCM(gi));
2201: else {
2202: sum = sum+HCM(gi)-mod;
2203: if ( sum < 0 ) sum += mod;
2204: }
2205: g->body[i] = nd_remove_head(gi);
2206: }
2207: }
2208: }
2209: if ( j < 0 ) return -1;
2210: else if ( sum ) {
2211: HCM(gj) = sum;
2212: return j;
2213: } else
2214: g->body[j] = nd_remove_head(gj);
2215: }
2216: }
2217:
2218: int head_pbucket_q(PGeoBucket g)
2219: {
2220: int j,i,c,k,nv;
2221: Z sum,t;
2222: ND gi,gj;
2223:
2224: k = g->m;
2225: while ( 1 ) {
2226: j = -1;
2227: for ( i = 0; i <= k; i++ ) {
2228: if ( !(gi = g->body[i]) ) continue;
2229: if ( j < 0 ) {
2230: j = i;
2231: gj = g->body[j];
1.6 noro 2232: sum = HCZ(gj);
1.1 noro 2233: } else {
2234: nv = NV(gi);
2235: c = DL_COMPARE(HDL(gi),HDL(gj));
2236: if ( c > 0 ) {
1.6 noro 2237: if ( sum ) HCZ(gj) = sum;
1.1 noro 2238: else g->body[j] = nd_remove_head(gj);
2239: j = i;
2240: gj = g->body[j];
1.6 noro 2241: sum = HCZ(gj);
1.1 noro 2242: } else if ( c == 0 ) {
1.6 noro 2243: addz(sum,HCZ(gi),&t);
1.1 noro 2244: sum = t;
2245: g->body[i] = nd_remove_head(gi);
2246: }
2247: }
2248: }
2249: if ( j < 0 ) return -1;
2250: else if ( sum ) {
1.6 noro 2251: HCZ(gj) = sum;
1.1 noro 2252: return j;
2253: } else
2254: g->body[j] = nd_remove_head(gj);
2255: }
2256: }
2257:
2258: int head_pbucket_lf(PGeoBucket g)
2259: {
2260: int j,i,c,k,nv;
2261: Z sum,t;
2262: ND gi,gj;
2263:
2264: k = g->m;
2265: while ( 1 ) {
2266: j = -1;
2267: for ( i = 0; i <= k; i++ ) {
2268: if ( !(gi = g->body[i]) ) continue;
2269: if ( j < 0 ) {
2270: j = i;
2271: gj = g->body[j];
2272: sum = HCZ(gj);
2273: } else {
2274: nv = NV(gi);
2275: c = DL_COMPARE(HDL(gi),HDL(gj));
2276: if ( c > 0 ) {
2277: if ( sum ) HCZ(gj) = sum;
2278: else g->body[j] = nd_remove_head(gj);
2279: j = i;
2280: gj = g->body[j];
2281: sum = HCZ(gj);
2282: } else if ( c == 0 ) {
2283: addlf(sum,HCZ(gi),&t);
2284: sum = t;
2285: g->body[i] = nd_remove_head(gi);
2286: }
2287: }
2288: }
2289: if ( j < 0 ) return -1;
2290: else if ( sum ) {
2291: HCZ(gj) = sum;
2292: return j;
2293: } else
2294: g->body[j] = nd_remove_head(gj);
2295: }
2296: }
2297:
2298: ND normalize_pbucket(int mod,PGeoBucket g)
2299: {
2300: int i;
2301: ND r,t;
2302:
2303: r = 0;
2304: for ( i = 0; i <= g->m; i++ ) {
2305: r = nd_add(mod,r,g->body[i]);
2306: g->body[i] = 0;
2307: }
2308: g->m = -1;
2309: return r;
2310: }
2311:
2312: #if 0
2313: void register_hcf(NDV p)
2314: {
2315: DCP dc,t;
2316: P hc,h;
2317: int c;
2318: NODE l,l1,prev;
2319:
2320: hc = p->body->c.p;
2321: if ( !nd_vc || NUM(hc) ) return;
2322: fctrp(nd_vc,hc,&dc);
2323: for ( t = dc; t; t = NEXT(t) ) {
2324: h = t->c;
2325: if ( NUM(h) ) continue;
2326: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2327: c = compp(nd_vc,h,(P)BDY(l));
2328: if ( c >= 0 ) break;
2329: }
2330: if ( !l || c > 0 ) {
2331: MKNODE(l1,h,l);
2332: if ( !prev )
2333: nd_hcf = l1;
2334: else
2335: NEXT(prev) = l1;
2336: }
2337: }
2338: }
2339: #else
2340: void register_hcf(NDV p)
2341: {
2342: DCP dc,t;
2343: P hc,h,q;
2344: Q dmy;
2345: int c;
2346: NODE l,l1,prev;
2347:
2348: hc = p->body->c.p;
2349: if ( NUM(hc) ) return;
2350: ptozp(hc,1,&dmy,&h);
2351: #if 1
2352: for ( l = nd_hcf; l; l = NEXT(l) ) {
2353: while ( 1 ) {
2354: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2355: else break;
2356: }
2357: }
2358: if ( NUM(h) ) return;
2359: #endif
2360: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2361: c = compp(nd_vc,h,(P)BDY(l));
2362: if ( c >= 0 ) break;
2363: }
2364: if ( !l || c > 0 ) {
2365: MKNODE(l1,h,l);
2366: if ( !prev )
2367: nd_hcf = l1;
2368: else
2369: NEXT(prev) = l1;
2370: }
2371: }
2372: #endif
2373:
2374: int do_diagonalize(int sugar,int m)
2375: {
1.6 noro 2376: int i,nh,stat;
2377: NODE r,g,t;
2378: ND h,nf,s,head;
2379: NDV nfv;
2380: Q q;
2381: P nm,nmp,dn,mnp,dnp,cont,cont1;
2382: union oNDC hc;
2383: NODE node;
2384: LIST l;
2385: Z iq;
1.1 noro 2386:
1.6 noro 2387: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2388: if ( nd_gentrace ) {
2389: /* Trace = [1,index,1,1] */
2390: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2391: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2392: }
2393: if ( nd_demand )
2394: nfv = ndv_load(i);
2395: else
2396: nfv = nd_ps[i];
2397: s = ndvtond(m,nfv);
2398: s = nd_separate_head(s,&head);
2399: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2400: if ( !stat ) return 0;
2401: ndv_free(nfv);
2402: hc = HCU(nf); nd_removecont(m,nf);
2403: /* exact division */
2404: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2405: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2406: nfv = ndtondv(m,nf);
2407: nd_free(nf);
2408: nd_bound[i] = ndv_compute_bound(nfv);
2409: if ( !m ) register_hcf(nfv);
2410: if ( nd_demand ) {
2411: ndv_save(nfv,i);
2412: ndv_free(nfv);
2413: } else
2414: nd_ps[i] = nfv;
2415: }
2416: return 1;
1.1 noro 2417: }
2418:
2419: LIST compute_splist()
2420: {
2421: NODE g,tn0,tn,node;
2422: LIST l0;
2423: ND_pairs d,t;
2424: int i;
2425: Z i1,i2;
2426:
2427: g = 0; d = 0;
2428: for ( i = 0; i < nd_psn; i++ ) {
2429: d = update_pairs(d,g,i,0);
2430: g = update_base(g,i);
2431: }
2432: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2433: NEXTNODE(tn0,tn);
1.6 noro 2434: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2435: node = mknode(2,i1,i2); MKLIST(l0,node);
2436: BDY(tn) = l0;
2437: }
2438: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2439: return l0;
2440: }
2441:
2442: /* return value = 0 => input is not a GB */
2443:
2444: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2445: {
1.6 noro 2446: int i,nh,sugar,stat;
2447: NODE r,g,t;
2448: ND_pairs d;
2449: ND_pairs l;
2450: ND h,nf,s,head,nf1;
2451: NDV nfv;
2452: Z q;
2453: union oNDC dn,hc;
2454: int diag_count = 0;
1.41 noro 2455: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2456: P cont;
2457: LIST list;
1.27 noro 2458: struct oEGT eg1,eg2,eg_update;
1.6 noro 2459:
1.27 noro 2460: init_eg(&eg_update);
1.11 noro 2461: Nnd_add = 0;
1.6 noro 2462: g = 0; d = 0;
2463: for ( i = 0; i < nd_psn; i++ ) {
2464: d = update_pairs(d,g,i,gensyz);
2465: g = update_base(g,i);
2466: }
2467: sugar = 0;
2468: while ( d ) {
1.1 noro 2469: again:
1.6 noro 2470: l = nd_minp(d,&d);
2471: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2472: if ( SG(l) != sugar ) {
2473: if ( ishomo ) {
2474: diag_count = 0;
2475: stat = do_diagonalize(sugar,m);
1.1 noro 2476: if ( !stat ) {
1.6 noro 2477: NEXT(l) = d; d = l;
2478: d = nd_reconstruct(0,d);
2479: goto again;
1.1 noro 2480: }
1.6 noro 2481: }
2482: sugar = SG(l);
2483: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2484: }
2485: stat = nd_sp(m,0,l,&h);
2486: if ( !stat ) {
2487: NEXT(l) = d; d = l;
2488: d = nd_reconstruct(0,d);
2489: goto again;
2490: }
1.1 noro 2491: #if USE_GEOBUCKET
1.39 noro 2492: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2493: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2494: #else
1.39 noro 2495: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2496: #endif
1.6 noro 2497: if ( !stat ) {
2498: NEXT(l) = d; d = l;
2499: d = nd_reconstruct(0,d);
2500: goto again;
2501: } else if ( nf ) {
1.41 noro 2502: Nnfnz++;
1.6 noro 2503: if ( checkonly || gensyz ) return 0;
1.1 noro 2504: if ( nd_newelim ) {
2505: if ( nd_module ) {
2506: if ( MPOS(HDL(nf)) > 1 ) return 0;
2507: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2508: }
1.6 noro 2509: if ( DP_Print ) { printf("+"); fflush(stdout); }
2510: hc = HCU(nf);
2511: nd_removecont(m,nf);
2512: if ( !m && nd_nalg ) {
2513: nd_monic(0,&nf);
2514: nd_removecont(m,nf);
2515: }
2516: if ( nd_gentrace ) {
2517: /* exact division */
1.1 noro 2518: cont = ndc_div(m,hc,HCU(nf));
2519: if ( m || !UNIQ(cont) ) {
1.6 noro 2520: t = mknode(4,NULLP,NULLP,NULLP,cont);
2521: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2522: nd_tracelist = t;
2523: }
2524: }
1.6 noro 2525: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2526: nh = ndv_newps(m,nfv,0);
1.6 noro 2527: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2528: diag_count = 0;
2529: stat = do_diagonalize(sugar,m);
2530: if ( !stat ) {
2531: NEXT(l) = d; d = l;
2532: d = nd_reconstruct(1,d);
2533: goto again;
1.1 noro 2534: }
1.6 noro 2535: }
1.27 noro 2536: get_eg(&eg1);
1.6 noro 2537: d = update_pairs(d,g,nh,0);
1.27 noro 2538: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2539: g = update_base(g,nh);
2540: FREENDP(l);
1.41 noro 2541: } else {
2542: Nnfz++;
2543: if ( nd_gentrace && gensyz ) {
2544: nd_tracelist = reverse_node(nd_tracelist);
2545: MKLIST(list,nd_tracelist);
2546: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2547: MKNODE(t,list,nd_alltracelist);
2548: nd_alltracelist = t; nd_tracelist = 0;
2549: }
2550: if ( DP_Print ) { printf("."); fflush(stdout); }
2551: FREENDP(l);
2552: }
1.36 noro 2553: }
2554: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2555: if ( !checkonly && DP_Print ) {
2556: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2557: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2558: fflush(asir_out);
2559: }
2560: if ( DP_Print ) {
2561: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2562: }
1.36 noro 2563: return g;
1.1 noro 2564: }
2565:
1.30 noro 2566: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
2567: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24 noro 2568:
2569: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2570: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2571:
2572: void _copydl(int n,DL d1,DL d2);
2573: void _subfromdl(int n,DL d1,DL d2);
2574: extern int (*cmpdl)(int n,DL d1,DL d2);
2575:
2576: NODE insert_sig(NODE l,SIG s)
2577: {
2578: int pos;
2579: DL sig;
2580: struct oNODE root;
2581: NODE p,prev,r;
2582: SIG t;
2583:
2584: pos = s->pos; sig = DL(s);
2585: root.next = l; prev = &root;
2586: for ( p = l; p; p = p->next ) {
2587: t = (SIG)p->body;
2588: if ( t->pos == pos ) {
2589: if ( _dl_redble(DL(t),sig,nd_nvar) )
2590: return root.next;
2591: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2592: // remove p
2593: prev->next = p->next;
2594: } else
2595: prev = p;
2596: }
1.27 noro 2597: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2598: for ( p = &root; p->next; p = p->next );
2599: p->next = r;
2600: // r->next = root.next;
2601: // return r;
2602: return root.next;
1.24 noro 2603: }
2604:
2605: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2606: {
2607: struct oND_pairs root;
2608: ND_pairs prev,p;
2609: SIG spsig;
2610:
2611: root.next = d;
2612: prev = &root; p = d;
2613: while ( p ) {
2614: spsig = p->sig;
1.26 noro 2615: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2616: // remove p
2617: prev->next = p->next;
1.26 noro 2618: Nsyz++;
2619: } else
1.24 noro 2620: prev = p;
2621: p = p->next;
2622: }
2623: return (ND_pairs)root.next;
2624: }
2625:
1.29 noro 2626: int _dl_redble_ext(DL,DL,DL,int);
2627:
1.28 noro 2628: int small_lcm(ND_pairs l)
2629: {
2630: SIG sig;
2631: int i;
1.29 noro 2632: NODE t;
1.28 noro 2633: static DL lcm,mul,quo;
2634: static int nvar;
2635:
1.38 noro 2636: if ( nd_sba_largelcm ) return 0;
1.28 noro 2637: if ( nvar < nd_nvar ) {
2638: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2639: }
2640: sig = l->sig;
2641: _ndltodl(l->lcm,lcm);
1.29 noro 2642: #if 0
1.28 noro 2643: for ( i = 0; i < nd_psn; i++ ) {
2644: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2645: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2646: _ndltodl(DL(nd_psh[i]),mul);
2647: _addtodl(nd_nvar,quo,mul);
2648: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2649: break;
2650: }
2651: }
2652: if ( i < nd_psn ) return 1;
2653: else return 0;
1.29 noro 2654: #else
2655: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2656: i = (long)BDY(t);
2657: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2658: _ndltodl(DL(nd_psh[i]),mul);
2659: _addtodl(nd_nvar,quo,mul);
2660: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2661: break;
2662: }
2663: }
2664: if ( t ) return 1;
2665: else return 0;
2666: #endif
1.28 noro 2667: }
2668:
1.41 noro 2669: ND_pairs find_smallest_lcm(ND_pairs l)
2670: {
2671: SIG sig;
2672: int i,minindex;
2673: NODE t;
2674: ND_pairs r;
2675: struct oSIG sig1;
2676: static DL mul,quo,minlm;
2677: static int nvar;
2678:
2679: if ( nvar < nd_nvar ) {
2680: nvar = nd_nvar;
2681: NEWDL(quo,nvar); NEWDL(mul,nvar);
2682: NEWDL(minlm,nvar);
2683: }
2684: sig = l->sig;
2685: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2686: _ndltodl(l->lcm,minlm); minindex = -1;
2687: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2688: i = (long)BDY(t);
2689: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2690: _ndltodl(DL(nd_psh[i]),mul);
2691: _addtodl(nd_nvar,quo,mul);
2692: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2693: minindex = i;
2694: _copydl(nd_nvar,mul,minlm);
2695: }
2696: }
2697: }
2698: // l->lcm is minimal; return l itself
2699: if ( minindex < 0 ) return l;
2700: for ( i = 0; i < nd_psn; i++ ) {
2701: if ( i == minindex ) continue;
2702: _ndltodl(DL(nd_psh[i]),mul);
2703: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2704: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2705: sig1.pos = nd_ps[i]->sig->pos;
2706: sig1.dl = quo;
2707: if ( comp_sig(sig,&sig1) > 0 ) {
2708: // printf("X");
2709: NEWND_pairs(r);
2710: r->sig = sig;
2711: r->i1 = minindex;
2712: r->i2 = i;
2713: dltondl(nd_nvar,minlm,r->lcm);
2714: r->next = 0;
2715: return r;
2716: }
2717: }
2718: }
2719: // there is no suitable spair
2720: return 0;
2721: }
2722:
1.28 noro 2723: ND_pairs remove_large_lcm(ND_pairs d)
2724: {
2725: struct oND_pairs root;
2726: ND_pairs prev,p;
2727:
2728: root.next = d;
2729: prev = &root; p = d;
2730: while ( p ) {
1.41 noro 2731: #if 0
1.28 noro 2732: if ( small_lcm(p) ) {
2733: // remove p
2734: prev->next = p->next;
2735: } else
1.41 noro 2736: #else
2737: if ( find_smallest_lcm(p) == 0 ) {
2738: // remove p
2739: prev->next = p->next;
2740: } else
2741: #endif
1.28 noro 2742: prev = p;
2743: p = p->next;
2744: }
2745: return (ND_pairs)root.next;
2746: }
2747:
1.26 noro 2748: struct oEGT eg_create,eg_newpairs,eg_merge;
2749:
1.30 noro 2750: NODE conv_ilist_s(int demand,int trace,int **indp);
2751:
1.24 noro 2752: NODE nd_sba_buch(int m,int ishomo,int **indp)
2753: {
1.39 noro 2754: int i,j,nh,sugar,stat,pos;
1.30 noro 2755: NODE r,t,g;
1.24 noro 2756: ND_pairs d;
1.41 noro 2757: ND_pairs l,l1;
1.24 noro 2758: ND h,nf,s,head,nf1;
2759: NDV nfv;
2760: Z q;
2761: union oNDC dn,hc;
2762: P cont;
2763: LIST list;
2764: SIG sig;
1.29 noro 2765: NODE *syzlist;
1.41 noro 2766: int Nnominimal,Nredundant;
1.28 noro 2767: DL lcm,quo,mul;
1.31 noro 2768: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.41 noro 2769: int Nnfs=0,Nnfz=0,Nnfnz=0;
1.24 noro 2770:
1.27 noro 2771: init_eg(&eg_remove);
1.29 noro 2772: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26 noro 2773: Nsyz = 0;
1.24 noro 2774: Nnd_add = 0;
1.41 noro 2775: Nnominimal = 0;
1.26 noro 2776: Nredundant = 0;
1.30 noro 2777: d = 0;
1.29 noro 2778: for ( i = 0; i < nd_psn; i++ )
2779: for ( j = i+1; j < nd_psn; j++ ) {
2780: NEWSIG(sig); sig->pos = j;
2781: _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
2782: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
2783: }
1.41 noro 2784: for ( i = 0; i < nd_psn; i++ ) {
2785: d = update_pairs_s(d,i,syzlist);
2786: }
1.24 noro 2787: sugar = 0;
1.39 noro 2788: pos = 0;
1.26 noro 2789: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2790: init_eg(&eg_create);
2791: init_eg(&eg_merge);
1.29 noro 2792: init_eg(&eg_large);
1.31 noro 2793: init_eg(&eg_nf);
2794: init_eg(&eg_nfzero);
1.24 noro 2795: while ( d ) {
2796: again:
1.26 noro 2797: if ( DP_Print ) {
2798: int len;
2799: ND_pairs td;
1.28 noro 2800: for ( td = d, len=0; td; td = td->next, len++)
2801: ;
1.26 noro 2802: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2803: }
1.24 noro 2804: l = d; d = d->next;
1.41 noro 2805: #if 0
1.28 noro 2806: if ( small_lcm(l) ) {
1.24 noro 2807: if ( DP_Print ) fprintf(asir_out,"M");
1.41 noro 2808: Nnominimal++;
1.24 noro 2809: continue;
2810: }
2811: if ( SG(l) != sugar ) {
2812: sugar = SG(l);
2813: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2814: }
1.28 noro 2815: sig = l->sig;
1.39 noro 2816: if ( DP_Print && nd_sba_pot ) {
2817: if ( sig->pos != pos ) {
2818: fprintf(asir_out,"[%d]",sig->pos);
2819: pos = sig->pos;
2820: }
2821: }
1.24 noro 2822: stat = nd_sp(m,0,l,&h);
1.41 noro 2823: #else
2824: l1 = find_smallest_lcm(l);
2825: if ( l1 == 0 ) {
2826: if ( DP_Print ) fprintf(asir_out,"M");
2827: Nnominimal++;
2828: continue;
2829: }
2830: if ( SG(l1) != sugar ) {
2831: sugar = SG(l1);
2832: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2833: }
2834: sig = l1->sig;
2835: if ( DP_Print && nd_sba_pot ) {
2836: if ( sig->pos != pos ) {
2837: fprintf(asir_out,"[%d]",sig->pos);
2838: pos = sig->pos;
2839: }
2840: }
2841: stat = nd_sp(m,0,l1,&h);
2842: #endif
1.24 noro 2843: if ( !stat ) {
2844: NEXT(l) = d; d = l;
2845: d = nd_reconstruct(0,d);
2846: goto again;
2847: }
1.31 noro 2848: get_eg(&eg1);
1.25 noro 2849: #if USE_GEOBUCKET
1.39 noro 2850: 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 2851: #else
1.39 noro 2852: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2853: #endif
1.31 noro 2854: get_eg(&eg2);
1.24 noro 2855: if ( !stat ) {
2856: NEXT(l) = d; d = l;
2857: d = nd_reconstruct(0,d);
2858: goto again;
2859: } else if ( stat == -1 ) {
1.41 noro 2860: Nnfs++;
1.24 noro 2861: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 noro 2862: FREENDP(l);
1.24 noro 2863: } else if ( nf ) {
1.41 noro 2864: Nnfnz++;
2865: if ( DP_Print ) {
2866: if ( nd_sba_redundant_check ) {
2867: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
2868: Nredundant++;
2869: printf("R");
2870: } else
2871: printf("+");
2872: } else
2873: printf("+");
2874: fflush(stdout);
2875: }
1.31 noro 2876: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 2877: hc = HCU(nf);
2878: nd_removecont(m,nf);
2879: nfv = ndtondv(m,nf); nd_free(nf);
2880: nh = ndv_newps(m,nfv,0);
1.26 noro 2881:
1.30 noro 2882: d = update_pairs_s(d,nh,syzlist);
1.29 noro 2883: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2884: FREENDP(l);
2885: } else {
1.41 noro 2886: Nnfz++;
1.31 noro 2887: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 2888: // syzygy
1.27 noro 2889: get_eg(&eg1);
1.24 noro 2890: d = remove_spair_s(d,sig);
1.27 noro 2891: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2892: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2893: if ( DP_Print ) { printf("."); fflush(stdout); }
2894: FREENDP(l);
2895: }
2896: }
1.30 noro 2897: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2898: if ( DP_Print ) {
1.41 noro 2899: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",Nnd_add,Nsyz,Nsamesig,Nnominimal);
2900: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
2901: fflush(stdout);
2902: if ( nd_sba_redundant_check )
2903: printf("Nredundant=%d\n",Nredundant);
1.26 noro 2904: fflush(stdout);
2905: print_eg("create",&eg_create);
2906: print_eg("merge",&eg_merge);
1.27 noro 2907: print_eg("remove",&eg_remove);
1.31 noro 2908: print_eg("nf",&eg_nf);
2909: print_eg("nfzero",&eg_nfzero);
1.27 noro 2910: printf("\n");
1.26 noro 2911: }
1.24 noro 2912: return g;
2913: }
2914:
1.1 noro 2915: /* splist = [[i1,i2],...] */
2916:
2917: int check_splist(int m,NODE splist)
2918: {
2919: NODE t,p;
2920: ND_pairs d,r,l;
2921: int stat;
2922: ND h,nf;
2923:
2924: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2925: p = BDY((LIST)BDY(t));
1.6 noro 2926: NEXTND_pairs(d,r);
2927: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2928: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2929: SG(r) = TD(LCM(r)); /* XXX */
2930: }
2931: if ( d ) NEXT(r) = 0;
2932:
1.6 noro 2933: while ( d ) {
1.1 noro 2934: again:
1.6 noro 2935: l = nd_minp(d,&d);
2936: stat = nd_sp(m,0,l,&h);
2937: if ( !stat ) {
2938: NEXT(l) = d; d = l;
2939: d = nd_reconstruct(0,d);
2940: goto again;
2941: }
1.39 noro 2942: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 2943: if ( !stat ) {
2944: NEXT(l) = d; d = l;
2945: d = nd_reconstruct(0,d);
2946: goto again;
2947: } else if ( nf ) return 0;
1.1 noro 2948: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2949: }
1.1 noro 2950: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2951: return 1;
2952: }
2953:
2954: int check_splist_f4(int m,NODE splist)
2955: {
2956: UINT *s0vect;
1.6 noro 2957: PGeoBucket bucket;
1.1 noro 2958: NODE p,rp0,t;
2959: ND_pairs d,r,l,ll;
2960: int col,stat;
2961:
2962: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2963: p = BDY((LIST)BDY(t));
1.6 noro 2964: NEXTND_pairs(d,r);
2965: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2966: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2967: SG(r) = TD(LCM(r)); /* XXX */
2968: }
2969: if ( d ) NEXT(r) = 0;
2970:
1.6 noro 2971: while ( d ) {
2972: l = nd_minsugarp(d,&d);
2973: bucket = create_pbucket();
2974: stat = nd_sp_f4(m,0,l,bucket);
2975: if ( !stat ) {
2976: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2977: NEXT(ll) = d; d = l;
2978: d = nd_reconstruct(0,d);
2979: continue;
2980: }
2981: if ( bucket->m < 0 ) continue;
2982: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2983: if ( !col ) {
2984: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2985: NEXT(ll) = d; d = l;
2986: d = nd_reconstruct(0,d);
2987: continue;
1.1 noro 2988: }
1.6 noro 2989: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2990: }
2991: return 1;
1.1 noro 2992: }
2993:
2994: int do_diagonalize_trace(int sugar,int m)
2995: {
1.6 noro 2996: int i,nh,stat;
2997: NODE r,g,t;
2998: ND h,nf,nfq,s,head;
2999: NDV nfv,nfqv;
3000: Q q,den,num;
3001: union oNDC hc;
3002: NODE node;
3003: LIST l;
3004: Z iq;
3005: P cont,cont1;
1.1 noro 3006:
1.6 noro 3007: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3008: if ( nd_gentrace ) {
3009: /* Trace = [1,index,1,1] */
3010: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3011: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3012: }
3013: /* for nd_ps */
3014: s = ndvtond(m,nd_ps[i]);
3015: s = nd_separate_head(s,&head);
3016: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3017: if ( !stat ) return 0;
3018: nf = nd_add(m,head,nf);
3019: ndv_free(nd_ps[i]);
3020: nd_ps[i] = ndtondv(m,nf);
3021: nd_free(nf);
3022:
3023: /* for nd_ps_trace */
3024: if ( nd_demand )
3025: nfv = ndv_load(i);
3026: else
3027: nfv = nd_ps_trace[i];
3028: s = ndvtond(0,nfv);
3029: s = nd_separate_head(s,&head);
3030: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3031: if ( !stat ) return 0;
3032: ndv_free(nfv);
3033: hc = HCU(nf); nd_removecont(0,nf);
3034: /* exact division */
1.1 noro 3035: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3036: if ( nd_gentrace ) finalize_tracelist(i,cont);
3037: nfv = ndtondv(0,nf);
3038: nd_free(nf);
3039: nd_bound[i] = ndv_compute_bound(nfv);
3040: register_hcf(nfv);
3041: if ( nd_demand ) {
3042: ndv_save(nfv,i);
3043: ndv_free(nfv);
3044: } else
3045: nd_ps_trace[i] = nfv;
3046: }
3047: return 1;
1.1 noro 3048: }
3049:
3050: static struct oEGT eg_invdalg;
3051: struct oEGT eg_le;
3052:
3053: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3054: {
3055: NODE tn;
3056: P p1;
3057:
3058: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3059: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3060: }
3061: *r = p;
3062: }
3063:
3064: NODE nd_gb_trace(int m,int ishomo,int **indp)
3065: {
1.6 noro 3066: int i,nh,sugar,stat;
3067: NODE r,g,t;
3068: ND_pairs d;
3069: ND_pairs l;
3070: ND h,nf,nfq,s,head;
3071: NDV nfv,nfqv;
3072: Z q,den,num;
3073: P hc;
3074: union oNDC dn,hnfq;
3075: struct oEGT eg_monic,egm0,egm1;
3076: int diag_count = 0;
3077: P cont;
3078: LIST list;
3079:
3080: init_eg(&eg_monic);
3081: init_eg(&eg_invdalg);
3082: init_eg(&eg_le);
3083: g = 0; d = 0;
3084: for ( i = 0; i < nd_psn; i++ ) {
3085: d = update_pairs(d,g,i,0);
3086: g = update_base(g,i);
3087: }
3088: sugar = 0;
3089: while ( d ) {
1.1 noro 3090: again:
1.6 noro 3091: l = nd_minp(d,&d);
3092: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3093: if ( SG(l) != sugar ) {
1.1 noro 3094: #if 1
1.6 noro 3095: if ( ishomo ) {
3096: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3097: stat = do_diagonalize_trace(sugar,m);
3098: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3099: diag_count = 0;
1.1 noro 3100: if ( !stat ) {
1.6 noro 3101: NEXT(l) = d; d = l;
3102: d = nd_reconstruct(1,d);
3103: goto again;
1.1 noro 3104: }
1.6 noro 3105: }
3106: #endif
3107: sugar = SG(l);
3108: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3109: }
3110: stat = nd_sp(m,0,l,&h);
3111: if ( !stat ) {
3112: NEXT(l) = d; d = l;
3113: d = nd_reconstruct(1,d);
3114: goto again;
3115: }
1.1 noro 3116: #if USE_GEOBUCKET
1.39 noro 3117: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3118: #else
1.39 noro 3119: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3120: #endif
1.6 noro 3121: if ( !stat ) {
3122: NEXT(l) = d; d = l;
3123: d = nd_reconstruct(1,d);
3124: goto again;
3125: } else if ( nf ) {
3126: if ( nd_demand ) {
3127: nfqv = ndv_load(nd_psn);
3128: nfq = ndvtond(0,nfqv);
3129: } else
3130: nfq = 0;
3131: if ( !nfq ) {
1.39 noro 3132: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3133: NEXT(l) = d; d = l;
3134: d = nd_reconstruct(1,d);
3135: goto again;
3136: }
3137: }
3138: if ( nfq ) {
3139: /* m|HC(nfq) => failure */
3140: if ( nd_vc ) {
3141: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3142: } else
3143: q = HCZ(nfq);
3144: if ( !remqi((Q)q,m) ) return 0;
3145:
3146: if ( DP_Print ) { printf("+"); fflush(stdout); }
3147: hnfq = HCU(nfq);
3148: if ( nd_nalg ) {
3149: /* m|DN(HC(nf)^(-1)) => failure */
3150: get_eg(&egm0);
3151: if ( !nd_monic(m,&nfq) ) return 0;
3152: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3153: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3154: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3155: } else {
3156: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3157: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3158: }
3159: if ( nd_gentrace ) {
3160: /* exact division */
3161: cont = ndc_div(0,hnfq,HCU(nfqv));
3162: if ( !UNIQ(cont) ) {
3163: t = mknode(4,NULLP,NULLP,NULLP,cont);
3164: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3165: nd_tracelist = t;
3166: }
3167: }
1.24 noro 3168: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3169: if ( ishomo && ++diag_count == diag_period ) {
3170: diag_count = 0;
3171: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3172: stat = do_diagonalize_trace(sugar,m);
3173: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3174: if ( !stat ) {
1.1 noro 3175: NEXT(l) = d; d = l;
3176: d = nd_reconstruct(1,d);
3177: goto again;
1.6 noro 3178: }
1.1 noro 3179: }
1.6 noro 3180: d = update_pairs(d,g,nh,0);
3181: g = update_base(g,nh);
3182: } else {
3183: if ( DP_Print ) { printf("*"); fflush(stdout); }
3184: }
3185: } else {
3186: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3187: }
1.6 noro 3188: FREENDP(l);
3189: }
3190: if ( nd_nalg ) {
3191: if ( DP_Print ) {
3192: print_eg("monic",&eg_monic);
3193: print_eg("invdalg",&eg_invdalg);
3194: print_eg("le",&eg_le);
1.1 noro 3195: }
1.6 noro 3196: }
1.1 noro 3197: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3198: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3199: return g;
1.1 noro 3200: }
3201:
3202: int ndv_compare(NDV *p1,NDV *p2)
3203: {
3204: return DL_COMPARE(HDL(*p1),HDL(*p2));
3205: }
3206:
3207: int ndv_compare_rev(NDV *p1,NDV *p2)
3208: {
3209: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3210: }
3211:
3212: int ndvi_compare(NDVI p1,NDVI p2)
3213: {
3214: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3215: }
3216:
3217: int ndvi_compare_rev(NDVI p1,NDVI p2)
3218: {
3219: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3220: }
3221:
3222: NODE ndv_reduceall(int m,NODE f)
3223: {
3224: int i,j,n,stat;
3225: ND nf,g,head;
3226: NODE t,a0,a;
3227: union oNDC dn;
3228: Q q,num,den;
3229: NODE node;
3230: LIST l;
3231: Z iq,jq;
3232: int *perm;
3233: union oNDC hc;
3234: P cont,cont1;
3235:
3236: if ( nd_nora ) return f;
3237: n = length(f);
1.24 noro 3238: ndv_setup(m,0,f,0,1,0);
1.1 noro 3239: perm = (int *)MALLOC(n*sizeof(int));
3240: if ( nd_gentrace ) {
3241: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3242: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3243: }
3244: for ( i = 0; i < n; ) {
3245: if ( nd_gentrace ) {
3246: /* Trace = [1,index,1,1] */
1.6 noro 3247: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3248: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3249: }
3250: g = ndvtond(m,nd_ps[i]);
3251: g = nd_separate_head(g,&head);
1.6 noro 3252: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3253: if ( !stat )
3254: nd_reconstruct(0,0);
3255: else {
3256: if ( DP_Print ) { printf("."); fflush(stdout); }
3257: ndv_free(nd_ps[i]);
3258: hc = HCU(nf); nd_removecont(m,nf);
3259: if ( nd_gentrace ) {
3260: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3261: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3262: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3263: }
1.6 noro 3264: /* exact division */
1.1 noro 3265: cont = ndc_div(m,hc,HCU(nf));
3266: finalize_tracelist(perm[i],cont);
3267: }
3268: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3269: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3270: i++;
3271: }
3272: }
3273: if ( DP_Print ) { printf("\n"); }
3274: for ( a0 = 0, i = 0; i < n; i++ ) {
3275: NEXTNODE(a0,a);
3276: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3277: else {
3278: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3279: BDY(a) = (pointer)nd_ps[j];
3280: }
3281: }
3282: NEXT(a) = 0;
3283: return a0;
3284: }
3285:
1.41 noro 3286: int ndplength(ND_pairs d)
3287: {
3288: int i;
3289: for ( i = 0; d; i++ ) d = NEXT(d);
3290: return i;
3291: }
3292:
1.1 noro 3293: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3294: {
3295: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3296: int len0;
1.1 noro 3297:
3298: if ( !g ) return d;
3299: /* for testing */
3300: if ( gensyz && nd_gensyz == 2 ) {
3301: d1 = nd_newpairs(g,t);
3302: if ( !d )
3303: return d1;
3304: else {
3305: nd = d;
3306: while ( NEXT(nd) ) nd = NEXT(nd);
3307: NEXT(nd) = d1;
3308: return d;
3309: }
3310: }
3311: d = crit_B(d,t);
3312: d1 = nd_newpairs(g,t);
1.41 noro 3313: len0 = ndplength(d1);
1.1 noro 3314: d1 = crit_M(d1);
3315: d1 = crit_F(d1);
1.41 noro 3316: NcriMF += len0-ndplength(d1);
1.1 noro 3317: if ( gensyz || do_weyl )
3318: head = d1;
3319: else {
3320: prev = 0; cur = head = d1;
3321: while ( cur ) {
3322: if ( crit_2( cur->i1,cur->i2 ) ) {
3323: remove = cur;
3324: if ( !prev ) head = cur = NEXT(cur);
3325: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3326: FREENDP(remove); Ncri2++;
1.1 noro 3327: } else {
3328: prev = cur; cur = NEXT(cur);
3329: }
3330: }
3331: }
3332: if ( !d )
3333: return head;
3334: else {
3335: nd = d;
3336: while ( NEXT(nd) ) nd = NEXT(nd);
3337: NEXT(nd) = head;
3338: return d;
3339: }
3340: }
3341:
1.24 noro 3342: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3343:
1.30 noro 3344: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3345: {
3346: ND_pairs d1;
1.26 noro 3347: struct oEGT eg1,eg2,eg3;
1.24 noro 3348:
1.30 noro 3349: if ( !t ) return d;
1.26 noro 3350: get_eg(&eg1);
1.30 noro 3351: d1 = nd_newpairs_s(t,syz);
1.26 noro 3352: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3353: d = merge_pairs_s(d,d1);
1.26 noro 3354: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3355: return d;
3356: }
1.1 noro 3357:
3358: ND_pairs nd_newpairs( NODE g, int t )
3359: {
3360: NODE h;
3361: UINT *dl;
3362: int ts,s,i,t0,min,max;
3363: ND_pairs r,r0;
3364:
3365: dl = DL(nd_psh[t]);
3366: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3367: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3368: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3369: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3370: continue;
3371: if ( nd_gbblock ) {
3372: t0 = (long)BDY(h);
3373: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3374: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3375: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3376: break;
3377: }
3378: if ( nd_gbblock[i] >= 0 )
3379: continue;
3380: }
1.41 noro 3381: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3382: r->i1 = (long)BDY(h);
3383: r->i2 = t;
3384: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3385: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3386: SG(r) = MAX(s,ts) + TD(LCM(r));
3387: /* experimental */
3388: if ( nd_sugarweight )
3389: r->sugar2 = ndl_weight2(r->lcm);
3390: }
3391: if ( r0 ) NEXT(r) = 0;
3392: return r0;
3393: }
3394:
1.24 noro 3395: int comp_sig(SIG s1,SIG s2)
3396: {
1.38 noro 3397: if ( nd_sba_pot ) {
3398: if ( s1->pos > s2->pos ) return 1;
3399: else if ( s1->pos < s2->pos ) return -1;
3400: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3401: } else {
3402: static DL m1,m2;
3403: static int nvar;
3404: int ret;
3405:
3406: if ( nvar != nd_nvar ) {
3407: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3408: }
3409: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3410: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
3411: ret = (*cmpdl)(nd_nvar,m1,m2);
3412: if ( ret != 0 ) return ret;
3413: else if ( s1->pos > s2->pos ) return 1;
3414: else if ( s1->pos < s2->pos ) return -1;
3415: else return 0;
1.24 noro 3416: }
3417: }
3418:
3419: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3420: {
3421: int ret,s1,s2;
3422: RHist p1,p2;
3423: static int wpd;
3424: static UINT *lcm;
3425:
3426: sp->i1 = i1;
3427: sp->i2 = i2;
3428: p1 = nd_psh[i1];
3429: p2 = nd_psh[i2];
3430: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3431: s1 = SG(p1)-TD(DL(p1));
3432: s2 = SG(p2)-TD(DL(p2));
3433: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3434:
1.26 noro 3435: if ( wpd != nd_wpd ) {
1.24 noro 3436: wpd = nd_wpd;
3437: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3438: }
3439: // DL(sig1) <- sp->lcm
3440: // DL(sig1) -= DL(p1)
3441: // DL(sig1) += DL(p1->sig)
1.27 noro 3442: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3443: _ndltodl(lcm,DL(sig1));
3444: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3445: sig1->pos = p1->sig->pos;
3446:
3447: // DL(sig2) <- sp->lcm
3448: // DL(sig2) -= DL(p2)
3449: // DL(sig2) += DL(p2->sig)
1.27 noro 3450: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3451: _ndltodl(lcm,DL(sig2));
3452: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3453: sig2->pos = p2->sig->pos;
3454:
3455: ret = comp_sig(sig1,sig2);
3456: if ( ret == 0 ) return 0;
3457: else if ( ret > 0 ) sp->sig = sig1;
3458: else sp->sig = sig2;
3459: return 1;
3460: }
3461:
3462: SIG dup_sig(SIG sig)
3463: {
3464: SIG r;
3465:
3466: if ( !sig ) return 0;
3467: else {
3468: NEWSIG(r);
3469: _copydl(nd_nvar,DL(sig),DL(r));
3470: r->pos = sig->pos;
3471: return r;
3472: }
3473: }
3474:
3475: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3476: {
3477: to->i1 = from->i1;
3478: to->i2 = from->i2;
3479: to->sugar = from->sugar;
3480: to->sugar2 = from->sugar2;
3481: ndl_copy(from->lcm,to->lcm);
3482: to->sig = dup_sig(from->sig);
3483: }
3484:
3485: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3486: {
3487: struct oND_pairs root;
3488: ND_pairs q1,q2,r0,r;
3489: int ret;
3490:
3491: r = &root;
3492: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3493: ret = comp_sig(q1->sig,q2->sig);
3494: if ( ret < 0 ) {
3495: r->next = q1; r = q1; q1 = q1->next;
3496: } else if ( ret > 0 ) {
3497: r->next = q2; r = q2; q2 = q2->next;
3498: } else {
3499: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 3500: Nsamesig++;
1.24 noro 3501: if ( ret < 0 ) {
3502: r->next = q1; r = q1; q1 = q1->next;
3503: q2 = q2->next;
3504: } else {
3505: r->next = q2; r = q2; q2 = q2->next;
3506: q1 = q1->next;
3507: }
3508: }
3509: }
3510: if ( q1 ) {
3511: r->next = q1;
3512: } else {
3513: r->next = q2;
3514: }
3515: return root.next;
3516: }
3517:
3518: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3519: {
3520: ND_pairs p,prev;
3521: int ret;
3522:
3523: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3524: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3525: break;
3526: }
3527: if ( ret == 0 ) {
3528: ret = DL_COMPARE(s->lcm,p->lcm);
3529: if ( ret < 0 ) {
3530: // replace p with s
3531: s->next = p->next;
3532: if ( prev == 0 ) {
3533: return s;
3534: } else {
3535: prev->next = s;
3536: return l;
3537: }
3538: } else
3539: return l;
3540: } else {
3541: // insert s between prev and p
3542: s->next = p;
3543: if ( prev == 0 ) {
3544: return s;
3545: } else {
3546: prev->next = s;
3547: return l;
3548: }
3549: }
3550: }
3551:
1.29 noro 3552: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3553: {
3554: int i;
3555:
3556: if ( d1->td > d2->td )
3557: return 0;
3558: for ( i = nvar-1; i >= 0; i-- )
3559: if ( d1->d[i] > d2->d[i] )
3560: break;
3561: if ( i >= 0 )
3562: return 0;
3563: else
3564: return 1;
3565: }
3566:
1.30 noro 3567: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3568: {
3569: NODE h,s;
3570: UINT *dl;
1.27 noro 3571: int ts,ret,i;
1.24 noro 3572: ND_pairs r,r0,_sp,sp;
1.29 noro 3573: SIG spsig,tsig;
3574: static int nvar;
3575: static SIG _sig1,_sig2;
1.26 noro 3576: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3577:
3578: NEWND_pairs(_sp);
1.29 noro 3579: if ( !_sig1 || nvar != nd_nvar ) {
3580: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3581: }
1.24 noro 3582: r0 = 0;
1.27 noro 3583: for ( i = 0; i < t; i++ ) {
3584: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3585: if ( ret ) {
3586: spsig = _sp->sig;
1.29 noro 3587: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3588: tsig = (SIG)s->body;
1.29 noro 3589: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3590: break;
3591: }
3592: if ( s == 0 ) {
3593: NEWND_pairs(sp);
3594: dup_ND_pairs(sp,_sp);
3595: r0 = insert_pair_s(r0,sp);
1.26 noro 3596: } else
3597: Nsyz++;
1.24 noro 3598: }
3599: }
3600: return r0;
3601: }
3602:
1.1 noro 3603: /* ipair = [i1,i2],[i1,i2],... */
3604: ND_pairs nd_ipairtospair(NODE ipair)
3605: {
3606: int s1,s2;
3607: NODE tn,t;
3608: ND_pairs r,r0;
3609:
3610: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3611: NEXTND_pairs(r0,r);
3612: tn = BDY((LIST)BDY(t));
1.6 noro 3613: r->i1 = ZTOS((Q)ARG0(tn));
3614: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3615: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3616: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3617: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3618: SG(r) = MAX(s1,s2) + TD(LCM(r));
3619: /* experimental */
3620: if ( nd_sugarweight )
3621: r->sugar2 = ndl_weight2(r->lcm);
3622: }
3623: if ( r0 ) NEXT(r) = 0;
3624: return r0;
3625: }
3626:
3627: /* kokokara */
3628:
3629: ND_pairs crit_B( ND_pairs d, int s )
3630: {
3631: ND_pairs cur,head,prev,remove;
3632: UINT *t,*tl,*lcm;
3633: int td,tdl;
3634:
3635: if ( !d ) return 0;
3636: t = DL(nd_psh[s]);
3637: prev = 0;
3638: head = cur = d;
3639: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3640: while ( cur ) {
3641: tl = cur->lcm;
3642: if ( ndl_reducible(tl,t) ) {
3643: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3644: if ( !ndl_equal(lcm,tl) ) {
3645: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3646: if (!ndl_equal(lcm,tl)) {
3647: remove = cur;
3648: if ( !prev ) {
3649: head = cur = NEXT(cur);
3650: } else {
3651: cur = NEXT(prev) = NEXT(cur);
3652: }
1.41 noro 3653: FREENDP(remove); NcriB++;
1.1 noro 3654: } else {
3655: prev = cur; cur = NEXT(cur);
3656: }
3657: } else {
3658: prev = cur; cur = NEXT(cur);
3659: }
3660: } else {
3661: prev = cur; cur = NEXT(cur);
3662: }
3663: }
3664: return head;
3665: }
3666:
3667: ND_pairs crit_M( ND_pairs d1 )
3668: {
3669: ND_pairs e,d2,d3,dd,p;
3670: UINT *id,*jd;
3671:
3672: if ( !d1 ) return d1;
3673: for ( dd = 0, e = d1; e; e = d3 ) {
3674: if ( !(d2 = NEXT(e)) ) {
3675: NEXT(e) = dd;
3676: return e;
3677: }
3678: id = LCM(e);
3679: for ( d3 = 0; d2; d2 = p ) {
3680: p = NEXT(d2);
3681: jd = LCM(d2);
3682: if ( ndl_equal(jd,id) )
3683: ;
3684: else if ( TD(jd) > TD(id) )
3685: if ( ndl_reducible(jd,id) ) continue;
3686: else ;
3687: else if ( ndl_reducible(id,jd) ) goto delit;
3688: NEXT(d2) = d3;
3689: d3 = d2;
3690: }
3691: NEXT(e) = dd;
3692: dd = e;
3693: continue;
3694: /**/
3695: delit: NEXT(d2) = d3;
3696: d3 = d2;
3697: for ( ; p; p = d2 ) {
3698: d2 = NEXT(p);
3699: NEXT(p) = d3;
3700: d3 = p;
3701: }
3702: FREENDP(e);
3703: }
3704: return dd;
3705: }
3706:
3707: ND_pairs crit_F( ND_pairs d1 )
3708: {
3709: ND_pairs rest, head,remove;
3710: ND_pairs last, p, r, w;
3711: int s;
3712:
3713: if ( !d1 ) return d1;
3714: for ( head = last = 0, p = d1; NEXT(p); ) {
3715: r = w = equivalent_pairs(p,&rest);
3716: s = SG(r);
3717: w = NEXT(w);
3718: while ( w ) {
3719: if ( crit_2(w->i1,w->i2) ) {
3720: r = w;
3721: w = NEXT(w);
3722: while ( w ) {
3723: remove = w;
3724: w = NEXT(w);
3725: FREENDP(remove);
3726: }
3727: break;
3728: } else if ( SG(w) < s ) {
3729: FREENDP(r);
3730: r = w;
3731: s = SG(r);
3732: w = NEXT(w);
3733: } else {
3734: remove = w;
3735: w = NEXT(w);
3736: FREENDP(remove);
3737: }
3738: }
3739: if ( last ) NEXT(last) = r;
3740: else head = r;
3741: NEXT(last = r) = 0;
3742: p = rest;
3743: if ( !p ) return head;
3744: }
3745: if ( !last ) return p;
3746: NEXT(last) = p;
3747: return head;
3748: }
3749:
3750: int crit_2( int dp1, int dp2 )
3751: {
3752: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3753: }
3754:
3755: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3756: {
3757: ND_pairs w,p,r,s;
3758: UINT *d;
3759:
3760: w = d1;
3761: d = LCM(w);
3762: s = NEXT(w);
3763: NEXT(w) = 0;
3764: for ( r = 0; s; s = p ) {
3765: p = NEXT(s);
3766: if ( ndl_equal(d,LCM(s)) ) {
3767: NEXT(s) = w; w = s;
3768: } else {
3769: NEXT(s) = r; r = s;
3770: }
3771: }
3772: *prest = r;
3773: return w;
3774: }
3775:
3776: NODE update_base(NODE nd,int ndp)
3777: {
3778: UINT *dl, *dln;
3779: NODE last, p, head;
3780:
3781: dl = DL(nd_psh[ndp]);
3782: for ( head = last = 0, p = nd; p; ) {
3783: dln = DL(nd_psh[(long)BDY(p)]);
3784: if ( ndl_reducible( dln, dl ) ) {
3785: p = NEXT(p);
3786: if ( last ) NEXT(last) = p;
3787: } else {
3788: if ( !last ) head = p;
3789: p = NEXT(last = p);
3790: }
3791: }
3792: head = append_one(head,ndp);
3793: return head;
3794: }
3795:
3796: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3797: {
3798: ND_pairs m,ml,p,l;
3799: UINT *lcm;
3800: int s,td,len,tlen,c,c1;
3801:
3802: if ( !(p = NEXT(m = d)) ) {
3803: *prest = p;
3804: NEXT(m) = 0;
3805: return m;
3806: }
3807: if ( !NoSugar ) {
3808: if ( nd_sugarweight ) {
3809: s = m->sugar2;
3810: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3811: if ( (p->sugar2 < s)
3812: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3813: ml = l; m = p; s = m->sugar2;
3814: }
3815: } else {
3816: s = SG(m);
3817: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3818: if ( (SG(p) < s)
3819: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3820: ml = l; m = p; s = SG(m);
3821: }
3822: }
3823: } else {
3824: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3825: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3826: ml = l; m = p; s = SG(m);
3827: }
3828: }
3829: if ( !ml ) *prest = NEXT(m);
3830: else {
3831: NEXT(ml) = NEXT(m);
3832: *prest = d;
3833: }
3834: NEXT(m) = 0;
3835: return m;
3836: }
3837:
3838: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3839: {
3840: int msugar,i;
3841: ND_pairs t,dm0,dm,dr0,dr;
3842:
3843: if ( nd_sugarweight ) {
3844: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3845: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3846: dm0 = 0; dr0 = 0;
3847: for ( i = 0, t = d; t; t = NEXT(t) )
3848: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3849: if ( dm0 ) NEXT(dm) = t;
3850: else dm0 = t;
3851: dm = t;
3852: i++;
3853: } else {
3854: if ( dr0 ) NEXT(dr) = t;
3855: else dr0 = t;
3856: dr = t;
3857: }
3858: } else {
3859: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3860: if ( SG(t) < msugar ) msugar = SG(t);
3861: dm0 = 0; dr0 = 0;
3862: for ( i = 0, t = d; t; t = NEXT(t) )
3863: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3864: if ( dm0 ) NEXT(dm) = t;
3865: else dm0 = t;
3866: dm = t;
3867: i++;
3868: } else {
3869: if ( dr0 ) NEXT(dr) = t;
3870: else dr0 = t;
3871: dr = t;
3872: }
3873: }
3874: NEXT(dm) = 0;
3875: if ( dr0 ) NEXT(dr) = 0;
3876: *prest = dr0;
3877: return dm0;
3878: }
3879:
1.28 noro 3880: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
3881: {
3882: int msugar;
3883: ND_pairs t,last;
3884:
3885: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
3886: if ( SG(t) == msugar ) last = t;
3887: *prest = last->next;
3888: last->next = 0;
3889: return d;
3890: }
3891:
1.1 noro 3892: int nd_tdeg(NDV c)
3893: {
3894: int wmax = 0;
3895: int i,len;
3896: NMV a;
3897:
3898: len = LEN(c);
3899: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3900: wmax = MAX(TD(DL(a)),wmax);
3901: return wmax;
3902: }
3903:
1.24 noro 3904: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3905: {
3906: int len;
3907: RHist r;
3908: NDV b;
3909: NODE tn;
3910: LIST l;
3911: Z iq;
3912:
3913: if ( nd_psn == nd_pslen ) {
3914: nd_pslen *= 2;
3915: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3916: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3917: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3918: nd_bound = (UINT **)
3919: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3920: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3921: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3922: }
3923: NEWRHist(r); nd_psh[nd_psn] = r;
3924: nd_ps[nd_psn] = a;
3925: if ( aq ) {
3926: nd_ps_trace[nd_psn] = aq;
3927: if ( !m ) {
3928: register_hcf(aq);
3929: } else if ( m == -2 ) {
3930: /* do nothing */
3931: } else
3932: error("ndv_newps : invalud modulus");
3933: nd_bound[nd_psn] = ndv_compute_bound(aq);
3934: #if 1
3935: SG(r) = SG(aq);
3936: #else
3937: SG(r) = nd_tdeg(aq);
3938: #endif
3939: ndl_copy(HDL(aq),DL(r));
1.24 noro 3940: r->sig = dup_sig(aq->sig);
1.1 noro 3941: } else {
3942: if ( !m ) register_hcf(a);
3943: nd_bound[nd_psn] = ndv_compute_bound(a);
3944: #if 1
3945: SG(r) = SG(a);
3946: #else
3947: SG(r) = nd_tdeg(a);
3948: #endif
3949: ndl_copy(HDL(a),DL(r));
1.24 noro 3950: r->sig = dup_sig(a->sig);
1.1 noro 3951: }
3952: if ( nd_demand ) {
3953: if ( aq ) {
3954: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3955: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3956: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3957: nd_ps_trace[nd_psn] = 0;
3958: } else {
3959: ndv_save(nd_ps[nd_psn],nd_psn);
3960: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3961: nd_ps[nd_psn] = 0;
3962: }
3963: }
3964: if ( nd_gentrace ) {
3965: /* reverse the tracelist and append it to alltracelist */
3966: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3967: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3968: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3969: }
3970: return nd_psn++;
3971: }
3972:
3973: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3974: /* return 1 if success, 0 if failure (HC(a mod p)) */
3975:
1.24 noro 3976: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3977: {
1.6 noro 3978: int i,j,td,len,max;
3979: NODE s,s0,f0,tn;
3980: UINT *d;
3981: RHist r;
3982: NDVI w;
3983: NDV a,am;
3984: union oNDC hc;
3985: NODE node;
3986: P hcp;
3987: Z iq,jq;
3988: LIST l;
3989:
3990: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3991: /* initialize the tracelist */
3992: nd_tracelist = 0;
3993:
3994: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3995: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3996: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3997: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3998: if ( !dont_sort ) {
3999: /* XXX heuristic */
1.26 noro 4000: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 4001: qsort(w,nd_psn,sizeof(struct oNDVI),
4002: (int (*)(const void *,const void *))ndvi_compare_rev);
4003: else
4004: qsort(w,nd_psn,sizeof(struct oNDVI),
4005: (int (*)(const void *,const void *))ndvi_compare);
4006: }
4007: nd_pslen = 2*nd_psn;
4008: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4009: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4010: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4011: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4012: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4013: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4014: nd_hcf = 0;
1.1 noro 4015:
1.6 noro 4016: if ( trace && nd_vc )
4017: makesubst(nd_vc,&nd_subst);
4018: else
4019: nd_subst = 0;
1.1 noro 4020:
1.6 noro 4021: if ( !nd_red )
4022: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4023: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4024: for ( i = 0; i < nd_psn; i++ ) {
4025: hc = HCU(w[i].p);
4026: if ( trace ) {
4027: if ( mod == -2 ) {
4028: /* over a large finite field */
4029: /* trace = small modulus */
4030: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4031: ndv_mod(-2,a);
4032: if ( !dont_removecont) ndv_removecont(-2,a);
4033: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4034: ndv_mod(trace,am);
4035: if ( DL_COMPARE(HDL(am),HDL(a)) )
4036: return 0;
4037: ndv_removecont(trace,am);
4038: } else {
4039: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4040: if ( !dont_removecont) ndv_removecont(0,a);
4041: register_hcf(a);
4042: am = nd_ps[i] = ndv_dup(mod,a);
4043: ndv_mod(mod,am);
4044: if ( DL_COMPARE(HDL(am),HDL(a)) )
4045: return 0;
4046: ndv_removecont(mod,am);
4047: }
4048: } else {
4049: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4050: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4051: if ( !mod ) register_hcf(a);
1.1 noro 4052: }
1.6 noro 4053: if ( nd_gentrace ) {
4054: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4055: /* exact division */
1.1 noro 4056: if ( !dont_removecont )
1.6 noro 4057: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4058: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4059: }
4060: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4061: nd_bound[i] = ndv_compute_bound(a);
4062: nd_psh[i] = r;
4063: if ( nd_demand ) {
4064: if ( trace ) {
4065: ndv_save(nd_ps_trace[i],i);
4066: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4067: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4068: nd_ps_trace[i] = 0;
4069: } else {
4070: ndv_save(nd_ps[i],i);
4071: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4072: nd_ps[i] = 0;
4073: }
1.1 noro 4074: }
1.6 noro 4075: }
1.24 noro 4076: if ( sba ) {
1.27 noro 4077: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4078: // setup signatures
1.27 noro 4079: for ( i = 0; i < nd_psn; i++ ) {
4080: SIG sig;
1.24 noro 4081:
1.27 noro 4082: NEWSIG(sig); sig->pos = i;
4083: nd_ps[i]->sig = sig;
4084: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4085: nd_psh[i]->sig = sig;
4086: if ( trace ) {
4087: nd_ps_trace[i]->sig = sig;
4088: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4089: }
4090: NEWDL(nd_sba_hm[i],nd_nvar);
4091: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
4092: }
1.29 noro 4093: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4094: for ( i = 0; i < nd_psn; i++ ) {
4095: j = nd_psh[i]->sig->pos;
4096: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4097: }
1.24 noro 4098: }
1.6 noro 4099: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4100: return 1;
1.1 noro 4101: }
4102:
4103: struct order_spec *append_block(struct order_spec *spec,
4104: int nv,int nalg,int ord);
4105:
4106: extern VECT current_dl_weight_vector_obj;
4107: static VECT prev_weight_vector_obj;
4108:
4109: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4110: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4111: {
4112: NODE alist,t,s,r0,r,arg;
4113: VL tv;
4114: P poly;
4115: DP d;
4116: Alg alpha,dp;
4117: DAlg inv,da,hc;
4118: MP m;
4119: int i,nvar,nalg,n;
4120: NumberField nf;
4121: LIST f1,f2;
4122: struct order_spec *current_spec;
4123: VECT obj,obj0;
4124: VECT tmp;
4125:
4126: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4127: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4128:
4129: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4130: NEXTNODE(alist,t); MKV(tv->v,poly);
4131: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4132: tv->v = tv->v->priv;
4133: }
4134: NEXT(t) = 0;
4135:
4136: /* simplification, making polynomials monic */
4137: setfield_dalg(alist);
4138: obj_algtodalg((Obj)f,(Obj *)&f1);
4139: for ( t = BDY(f); t; t = NEXT(t) ) {
4140: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4141: hc = (DAlg)BDY(d)->c;
4142: if ( NID(hc) == N_DA ) {
4143: invdalg(hc,&inv);
4144: for ( m = BDY(d); m; m = NEXT(m) ) {
4145: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4146: }
4147: }
4148: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4149: }
4150: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4151:
4152: /* append alg vars to the var list */
4153: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4154: NEXT(tv) = av;
4155:
4156: /* append a block to ord */
4157: *ord1p = append_block(ord,nvar,nalg,2);
4158:
4159: /* create generator list */
4160: nf = get_numberfield();
4161: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4162: MKAlg(nf->defpoly[i],dp);
4163: MKNODE(s,dp,t); t = s;
4164: }
4165: MKLIST(f1,t);
4166: *alistp = alist;
4167: algobjtorat((Obj)f1,(Obj *)f1p);
4168:
4169: /* creating a new weight vector */
4170: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4171: n = nvar+nalg+1;
4172: MKVECT(obj,n);
4173: if ( obj0 && obj0->len == nvar )
4174: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4175: else
4176: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4177: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4178: BDY(obj)[n-1] = (pointer)ONE;
4179: arg = mknode(1,obj);
4180: Pdp_set_weight(arg,&tmp);
4181: }
4182:
4183: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4184: {
4185: NODE s,t,u0,u;
4186: P p;
4187: VL tv;
4188: Obj obj;
4189: VECT tmp;
4190: NODE arg;
4191:
4192: u0 = 0;
4193: for ( t = r; t; t = NEXT(t) ) {
4194: p = (P)BDY(t);
4195: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4196: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4197: }
4198: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4199: NEXTNODE(u0,u);
4200: BDY(u) = (pointer)p;
4201: }
4202: }
4203: arg = mknode(1,prev_weight_vector_obj);
4204: Pdp_set_weight(arg,&tmp);
4205:
4206: return u0;
4207: }
4208:
4209: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4210: {
4211: VL tv,fv,vv,vc,av;
4212: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4213: int e,max,nvar,i;
4214: NDV b;
4215: int ishomo,nalg,mrank,trank,wmax,len;
4216: NMV a;
4217: Alg alpha,dp;
4218: P p,zp;
4219: Q dmy;
4220: LIST f1,f2,zpl;
4221: Obj obj;
4222: NumberField nf;
4223: struct order_spec *ord1;
4224: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4225: LIST l1,l2,l3,l4,l5;
4226: int j;
4227: Z jq,bpe,last_nonzero;
4228: int *perm;
4229: EPOS oepos;
4230: int obpe,oadv,ompos,cbpe;
1.15 noro 4231: VECT hvect;
1.1 noro 4232:
1.41 noro 4233: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4234: nd_module = 0;
4235: if ( !m && Demand ) nd_demand = 1;
4236: else nd_demand = 0;
4237: parse_nd_option(current_option);
4238:
4239: if ( DP_Multiple )
4240: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4241: #if 0
4242: ndv_alloc = 0;
4243: #endif
4244: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4245: if ( m && nd_vc )
4246: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4247: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4248: switch ( ord->id ) {
4249: case 1:
4250: if ( ord->nv != nvar )
4251: error("nd_{gr,f4} : invalid order specification");
4252: break;
4253: default:
4254: break;
4255: }
4256: nd_nalg = 0;
4257: av = 0;
4258: if ( !m ) {
4259: get_algtree((Obj)f,&av);
4260: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4261: nd_ntrans = nvar;
4262: nd_nalg = nalg;
4263: /* #i -> t#i */
4264: if ( nalg ) {
4265: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4266: ord = ord1;
4267: f = f1;
4268: }
4269: nvar += nalg;
4270: }
4271: nd_init_ord(ord);
4272: mrank = 0;
4273: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4274: for ( tv = vv; tv; tv = NEXT(tv) ) {
4275: if ( nd_module ) {
1.16 noro 4276: if ( OID(BDY(t)) == O_DPM ) {
4277: e = dpm_getdeg((DPM)BDY(t),&trank);
4278: max = MAX(e,max);
4279: mrank = MAX(mrank,trank);
4280: } else {
4281: s = BDY((LIST)BDY(t));
4282: trank = length(s);
4283: mrank = MAX(mrank,trank);
4284: for ( ; s; s = NEXT(s) ) {
4285: e = getdeg(tv->v,(P)BDY(s));
4286: max = MAX(e,max);
4287: }
1.1 noro 4288: }
4289: } else {
1.43 ! noro 4290: if ( OID(BDY(t)) == O_DP ) {
! 4291: e = dp_getdeg((DP)BDY(t));
! 4292: max = MAX(e,max);
! 4293: } else {
! 4294: e = getdeg(tv->v,(P)BDY(t));
! 4295: max = MAX(e,max);
! 4296: }
1.1 noro 4297: }
4298: }
4299: nd_setup_parameters(nvar,nd_nzlist?0:max);
4300: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4301: ishomo = 1;
4302: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4303: if ( nd_module ) {
1.16 noro 4304: if ( OID(BDY(t)) == O_DPM ) {
4305: Z cont;
4306: DPM zdpm;
4307:
4308: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4309: else zdpm = (DPM)BDY(t);
4310: b = (pointer)dpmtondv(m,zdpm);
4311: } else {
4312: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4313: else zpl = (LIST)BDY(t);
1.1 noro 4314: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4315: }
1.1 noro 4316: } else {
1.43 ! noro 4317: if ( OID(BDY(t)) == O_DP ) {
! 4318: DP zdp;
! 4319:
! 4320: if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
! 4321: else zdp = (DP)BDY(t);
! 4322: b = (pointer)dptondv(m,zdp);
! 4323: } else {
! 4324: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
! 4325: else zp = (P)BDY(t);
! 4326: b = (pointer)ptondv(CO,vv,zp);
! 4327: }
1.1 noro 4328: }
4329: if ( ishomo )
4330: ishomo = ishomo && ndv_ishomo(b);
4331: if ( m ) ndv_mod(m,b);
4332: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4333: }
4334: if ( fd0 ) NEXT(fd) = 0;
4335:
4336: if ( !ishomo && homo ) {
4337: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4338: b = (NDV)BDY(t); len = LEN(b);
4339: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4340: wmax = MAX(TD(DL(a)),wmax);
4341: }
4342: homogenize_order(ord,nvar,&ord1);
4343: nd_init_ord(ord1);
4344: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4345: for ( t = fd0; t; t = NEXT(t) )
4346: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4347: }
4348:
1.24 noro 4349: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4350: if ( nd_gentrace ) {
4351: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4352: }
4353: if ( nd_splist ) {
4354: *rp = compute_splist();
4355: return;
4356: }
4357: if ( nd_check_splist ) {
4358: if ( f4 ) {
4359: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4360: else *rp = 0;
4361: } else {
4362: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4363: else *rp = 0;
4364: }
4365: return;
4366: }
4367: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4368: if ( !x ) {
4369: *rp = 0; return;
4370: }
1.15 noro 4371: if ( nd_gentrace ) {
4372: MKVECT(hvect,nd_psn);
4373: for ( i = 0; i < nd_psn; i++ )
4374: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4375: }
1.1 noro 4376: if ( !ishomo && homo ) {
4377: /* dehomogenization */
4378: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4379: nd_init_ord(ord);
4380: nd_setup_parameters(nvar,0);
4381: }
4382: nd_demand = 0;
4383: if ( nd_module && nd_intersect ) {
4384: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4385: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4386: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4387: }
4388: conv_ilist(nd_demand,0,x,0);
4389: goto FINAL;
4390: }
4391: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4392: x = ndv_reducebase(x,perm);
4393: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4394: x = ndv_reduceall(m,x);
4395: cbpe = nd_bpe;
4396: if ( nd_gentrace && !f4 ) {
4397: tl2 = nd_alltracelist; nd_alltracelist = 0;
4398: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4399: tl3 = nd_alltracelist; nd_alltracelist = 0;
4400: if ( nd_gensyz ) {
4401: nd_gb(m,0,1,1,0);
4402: tl4 = nd_alltracelist; nd_alltracelist = 0;
4403: } else tl4 = 0;
4404: }
4405: nd_bpe = cbpe;
4406: nd_setup_parameters(nd_nvar,0);
4407: FINAL:
4408: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4409: NEXTNODE(r0,r);
4410: if ( nd_module ) {
4411: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4412: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4413: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4414: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4415: }
4416: if ( r0 ) NEXT(r) = 0;
4417: if ( !m && nd_nalg )
4418: r0 = postprocess_algcoef(av,alist,r0);
4419: MKLIST(*rp,r0);
4420: if ( nd_gentrace ) {
4421: if ( f4 ) {
1.6 noro 4422: STOZ(16,bpe);
4423: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4424: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4425: } else {
4426: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4427: tl3 = reverse_node(tl3);
4428: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4429: for ( t = tl2; t; t = NEXT(t) ) {
4430: /* s = [i,[*,j,*,*],...] */
4431: s = BDY((LIST)BDY(t));
1.6 noro 4432: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4433: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4434: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4435: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4436: }
4437: }
4438: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4439: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4440: }
4441: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4442: MKLIST(l5,tl4);
1.6 noro 4443: STOZ(nd_bpe,bpe);
1.15 noro 4444: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4445: }
4446: }
4447: #if 0
4448: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4449: #endif
4450: }
4451:
1.28 noro 4452: NODE nd_sba_f4(int m,int **indp);
4453:
4454: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4455: {
4456: VL tv,fv,vv,vc,av;
4457: NODE fd,fd0,r,r0,t,x,s,xx;
4458: int e,max,nvar,i;
4459: NDV b;
4460: int ishomo,nalg,wmax,len;
4461: NMV a;
4462: P p,zp;
4463: Q dmy;
4464: struct order_spec *ord1;
4465: int j;
4466: int *perm;
4467: EPOS oepos;
4468: int obpe,oadv,ompos,cbpe;
1.29 noro 4469: struct oEGT eg0,eg1,egconv;
1.24 noro 4470:
4471: nd_module = 0;
4472: nd_demand = 0;
4473: parse_nd_option(current_option);
1.35 noro 4474: Nsamesig = 0;
1.24 noro 4475: if ( DP_Multiple )
4476: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4477: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4478: if ( m && nd_vc )
4479: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4480: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4481: switch ( ord->id ) {
4482: case 1:
4483: if ( ord->nv != nvar )
4484: error("nd_sba : invalid order specification");
4485: break;
4486: default:
4487: break;
4488: }
4489: nd_nalg = 0;
4490: nd_init_ord(ord);
4491: // for SIG comparison
4492: initd(ord);
4493: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4494: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43 ! noro 4495: if ( OID(BDY(t)) == O_DP ) {
! 4496: e = dp_getdeg((DP)BDY(t));
! 4497: max = MAX(e,max);
! 4498: } else {
! 4499: e = getdeg(tv->v,(P)BDY(t));
! 4500: max = MAX(e,max);
! 4501: }
1.24 noro 4502: }
4503: }
1.25 noro 4504: nd_setup_parameters(nvar,max);
1.24 noro 4505: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4506: ishomo = 1;
4507: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43 ! noro 4508: if ( OID(BDY(t)) == O_DP ) {
! 4509: DP zdp;
! 4510:
! 4511: if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
! 4512: else zdp = (DP)BDY(t);
! 4513: b = (pointer)dptondv(m,zdp);
! 4514: } else {
! 4515: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
! 4516: else zp = (P)BDY(t);
! 4517: b = (pointer)ptondv(CO,vv,zp);
! 4518: }
1.24 noro 4519: if ( ishomo )
4520: ishomo = ishomo && ndv_ishomo(b);
4521: if ( m ) ndv_mod(m,b);
4522: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4523: }
4524: if ( fd0 ) NEXT(fd) = 0;
4525:
4526: if ( !ishomo && homo ) {
4527: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4528: b = (NDV)BDY(t); len = LEN(b);
4529: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4530: wmax = MAX(TD(DL(a)),wmax);
4531: }
4532: homogenize_order(ord,nvar,&ord1);
4533: nd_init_ord(ord1);
1.42 noro 4534: // for SIG comparison
4535: initd(ord1);
1.24 noro 4536: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4537: for ( t = fd0; t; t = NEXT(t) )
4538: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4539: }
4540:
1.39 noro 4541: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.28 noro 4542: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24 noro 4543: if ( !x ) {
4544: *rp = 0; return;
4545: }
4546: if ( !ishomo && homo ) {
4547: /* dehomogenization */
4548: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4549: nd_init_ord(ord);
1.42 noro 4550: // for SIG comparison
4551: initd(ord);
1.24 noro 4552: nd_setup_parameters(nvar,0);
4553: }
4554: nd_demand = 0;
4555: x = ndv_reducebase(x,perm);
4556: x = ndv_reduceall(m,x);
4557: nd_setup_parameters(nd_nvar,0);
1.29 noro 4558: get_eg(&eg0);
1.24 noro 4559: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4560: NEXTNODE(r0,r);
4561: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4562: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4563: }
4564: if ( r0 ) NEXT(r) = 0;
4565: MKLIST(*rp,r0);
1.29 noro 4566: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4567: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4568: }
4569:
1.1 noro 4570: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4571: {
4572: VL tv,fv,vv,vc,av;
4573: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4574: int e,max,nvar,i;
4575: NDV b;
4576: int ishomo,nalg;
4577: Alg alpha,dp;
4578: P p,zp;
4579: Q dmy;
4580: LIST f1,f2;
4581: Obj obj;
4582: NumberField nf;
4583: struct order_spec *ord1;
4584: int *perm;
4585:
4586: parse_nd_option(current_option);
4587: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4588: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4589: switch ( ord->id ) {
4590: case 1:
4591: if ( ord->nv != nvar )
4592: error("nd_check : invalid order specification");
4593: break;
4594: default:
4595: break;
4596: }
4597: nd_nalg = 0;
4598: av = 0;
4599: if ( !m ) {
4600: get_algtree((Obj)f,&av);
4601: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4602: nd_ntrans = nvar;
4603: nd_nalg = nalg;
4604: /* #i -> t#i */
4605: if ( nalg ) {
4606: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4607: ord = ord1;
4608: f = f1;
4609: }
4610: nvar += nalg;
4611: }
4612: nd_init_ord(ord);
4613: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4614: for ( tv = vv; tv; tv = NEXT(tv) ) {
4615: e = getdeg(tv->v,(P)BDY(t));
4616: max = MAX(e,max);
4617: }
4618: nd_setup_parameters(nvar,max);
4619: ishomo = 1;
4620: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4621: ptozp((P)BDY(t),1,&dmy,&zp);
4622: b = (pointer)ptondv(CO,vv,zp);
4623: if ( ishomo )
4624: ishomo = ishomo && ndv_ishomo(b);
4625: if ( m ) ndv_mod(m,b);
4626: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4627: }
4628: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4629: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4630: for ( x = 0, i = 0; i < nd_psn; i++ )
4631: x = update_base(x,i);
4632: if ( do_check ) {
4633: x = nd_gb(m,ishomo,1,0,&perm);
4634: if ( !x ) {
4635: *rp = 0;
4636: return;
4637: }
4638: } else {
4639: #if 0
4640: /* bug ? */
4641: for ( t = x; t; t = NEXT(t) )
4642: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4643: #else
4644: conv_ilist(0,0,x,&perm);
4645: #endif
4646: }
4647: x = ndv_reducebase(x,perm);
4648: x = ndv_reduceall(m,x);
4649: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4650: NEXTNODE(r0,r);
4651: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4652: }
4653: if ( r0 ) NEXT(r) = 0;
4654: if ( !m && nd_nalg )
4655: r0 = postprocess_algcoef(av,alist,r0);
4656: MKLIST(*rp,r0);
4657: }
4658:
4659: NDV recompute_trace(NODE trace,NDV *p,int m);
4660: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4661:
4662: NDV recompute_trace(NODE ti,NDV *p,int mod)
4663: {
4664: int c,c1,c2,i;
4665: NM mul,m,tail;
4666: ND d,r,rm;
4667: NODE sj;
4668: NDV red;
4669: Obj mj;
4670:
4671: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4672: CM(mul) = 1;
4673: tail = 0;
4674: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4675: sj = BDY((LIST)BDY(ti));
4676: if ( ARG0(sj) ) {
1.6 noro 4677: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4678: mj = (Obj)ARG2(sj);
4679: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4680: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4681: rm = ndv_mul_nm(mod,mul,red);
4682: if ( !r ) r = rm;
4683: else {
4684: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4685: if ( d ) {
4686: NEXT(tail) = m; tail = m; LEN(d)++;
4687: } else {
4688: MKND(nd_nvar,m,1,d); tail = BDY(d);
4689: }
4690: }
4691: if ( !m ) return 0; /* failure */
4692: else {
4693: BDY(r) = m;
4694: if ( mod > 0 || mod == -1 ) {
4695: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4696: DMAR(c1,c2,0,mod,c);
4697: nd_mul_c(mod,rm,c);
4698: } else {
4699: Z t,u;
4700:
4701: chsgnlf(HCZ(r),&t);
4702: divlf(t,HCZ(rm),&u);
4703: nd_mul_c_lf(rm,u);
4704: }
4705: r = nd_add(mod,r,rm);
4706: }
4707: }
4708: }
4709: }
4710: if ( tail ) NEXT(tail) = 0;
4711: d = nd_add(mod,d,r);
4712: nd_mul_c(mod,d,invm(HCM(d),mod));
4713: return ndtondv(mod,d);
4714: }
4715:
4716: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4717: {
4718: VL tv,fv,vv,vc,av;
4719: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4720: int e,max,nvar,i;
4721: NDV b;
4722: int ishomo,nalg;
4723: Alg alpha,dp;
4724: P p,zp;
4725: Q dmy;
4726: LIST f1,f2;
4727: Obj obj;
4728: NumberField nf;
4729: struct order_spec *ord1;
4730: NODE permtrace,intred,ind,perm,trace,ti;
4731: int len,n,j;
4732: NDV *db,*pb;
4733:
4734: parse_nd_option(current_option);
4735: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4736: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4737: switch ( ord->id ) {
4738: case 1:
4739: if ( ord->nv != nvar )
4740: error("nd_check : invalid order specification");
4741: break;
4742: default:
4743: break;
4744: }
4745: nd_init_ord(ord);
1.6 noro 4746: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4747: nd_setup_parameters(nvar,0);
4748:
4749: len = length(BDY(f));
4750: db = (NDV *)MALLOC(len*sizeof(NDV *));
4751: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4752: ptozp((P)BDY(t),1,&dmy,&zp);
4753: b = ptondv(CO,vv,zp);
4754: ndv_mod(m,b);
4755: ndv_mul_c(m,b,invm(HCM(b),m));
4756: db[i] = b;
4757: }
4758:
4759: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4760: intred = BDY((LIST)ARG3(BDY(tlist)));
4761: ind = BDY((LIST)ARG4(BDY(tlist)));
4762: perm = BDY((LIST)ARG0(permtrace));
4763: trace = NEXT(permtrace);
4764:
4765: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4766: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4767: if ( j > i ) i = j;
4768: }
4769: n = i+1;
4770: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4771: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4772: ti = BDY((LIST)BDY(t));
1.6 noro 4773: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4774: }
4775: for ( t = trace; t; t = NEXT(t) ) {
4776: ti = BDY((LIST)BDY(t));
1.6 noro 4777: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4778: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4779: if ( DP_Print ) {
4780: fprintf(asir_out,"."); fflush(asir_out);
4781: }
4782: }
4783: for ( t = intred; t; t = NEXT(t) ) {
4784: ti = BDY((LIST)BDY(t));
1.6 noro 4785: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4786: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4787: if ( DP_Print ) {
4788: fprintf(asir_out,"*"); fflush(asir_out);
4789: }
4790: }
4791: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4792: NEXTNODE(r0,r);
1.6 noro 4793: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4794: ndv_mul_c(m,b,invm(HCM(b),m));
4795: #if 0
1.6 noro 4796: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4797: #else
1.6 noro 4798: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4799: #endif
4800: }
4801: if ( r0 ) NEXT(r) = 0;
4802: MKLIST(*rp,r0);
4803: if ( DP_Print ) fprintf(asir_out,"\n");
4804: }
4805:
1.16 noro 4806: 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 4807: {
4808: VL tv,fv,vv,vc,av;
4809: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4810: int m,nocheck,nvar,mindex,e,max;
4811: NDV c;
4812: NMV a;
4813: P p,zp;
4814: Q dmy;
4815: EPOS oepos;
4816: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4817: Alg alpha,dp;
4818: P poly;
4819: LIST f1,f2,zpl;
4820: Obj obj;
4821: NumberField nf;
4822: struct order_spec *ord1;
4823: struct oEGT eg_check,eg0,eg1;
4824: NODE tr,tl1,tl2,tl3,tl4;
4825: LIST l1,l2,l3,l4,l5;
4826: int *perm;
4827: int j,ret;
4828: Z jq,bpe;
1.15 noro 4829: VECT hvect;
1.1 noro 4830:
1.41 noro 4831: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4832: nd_module = 0;
4833: nd_lf = 0;
4834: parse_nd_option(current_option);
4835: if ( nd_lf ) {
4836: if ( f4 )
4837: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4838: else
4839: error("nd_gr_trace is not implemented yet over a large finite field");
4840: return;
4841: }
4842: if ( DP_Multiple )
4843: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4844:
4845: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4846: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4847: switch ( ord->id ) {
4848: case 1:
4849: if ( ord->nv != nvar )
4850: error("nd_gr_trace : invalid order specification");
4851: break;
4852: default:
4853: break;
4854: }
4855:
4856: get_algtree((Obj)f,&av);
4857: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4858: nd_ntrans = nvar;
4859: nd_nalg = nalg;
4860: /* #i -> t#i */
4861: if ( nalg ) {
4862: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4863: ord = ord1;
4864: f = f1;
4865: }
4866: nvar += nalg;
4867:
4868: nocheck = 0;
4869: mindex = 0;
4870:
4871: if ( Demand ) nd_demand = 1;
4872: else nd_demand = 0;
4873:
4874: /* setup modulus */
4875: if ( trace < 0 ) {
4876: trace = -trace;
4877: nocheck = 1;
4878: }
4879: m = trace > 1 ? trace : get_lprime(mindex);
4880: nd_init_ord(ord);
4881: mrank = 0;
4882: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4883: for ( tv = vv; tv; tv = NEXT(tv) ) {
4884: if ( nd_module ) {
1.16 noro 4885: if ( OID(BDY(t)) == O_DPM ) {
4886: e = dpm_getdeg((DPM)BDY(t),&trank);
4887: max = MAX(e,max);
4888: mrank = MAX(mrank,trank);
4889: } else {
1.1 noro 4890: s = BDY((LIST)BDY(t));
4891: trank = length(s);
4892: mrank = MAX(mrank,trank);
4893: for ( ; s; s = NEXT(s) ) {
4894: e = getdeg(tv->v,(P)BDY(s));
4895: max = MAX(e,max);
4896: }
1.16 noro 4897: }
1.1 noro 4898: } else {
1.43 ! noro 4899: if ( OID(BDY(t)) == O_DP ) {
! 4900: e = dp_getdeg((DP)BDY(t));
! 4901: max = MAX(e,max);
! 4902: } else {
1.1 noro 4903: e = getdeg(tv->v,(P)BDY(t));
4904: max = MAX(e,max);
1.43 ! noro 4905: }
1.1 noro 4906: }
4907: }
4908: nd_setup_parameters(nvar,max);
4909: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4910: ishomo = 1;
4911: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4912: if ( nd_module ) {
1.16 noro 4913: if ( OID(BDY(t)) == O_DPM ) {
4914: Z cont;
4915: DPM zdpm;
4916:
1.17 noro 4917: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4918: else zdpm = (DPM)BDY(t);
4919: c = (pointer)dpmtondv(m,zdpm);
4920: } else {
4921: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4922: else zpl = (LIST)BDY(t);
1.1 noro 4923: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4924: }
1.1 noro 4925: } else {
1.43 ! noro 4926: if ( OID(BDY(t)) == O_DP ) {
! 4927: DP zdp;
! 4928:
! 4929: if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
! 4930: else zdp = (DP)BDY(t);
! 4931: c = (pointer)dptondv(m,zdp);
! 4932: } else {
! 4933: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
! 4934: else zp = (P)BDY(t);
! 4935: c = (pointer)ptondv(CO,vv,zp);
! 4936: }
1.1 noro 4937: }
4938: if ( ishomo )
4939: ishomo = ishomo && ndv_ishomo(c);
4940: if ( c ) {
4941: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4942: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4943: }
4944: }
4945: if ( in0 ) NEXT(in) = 0;
4946: if ( fd0 ) NEXT(fd) = 0;
4947: if ( !ishomo && homo ) {
4948: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4949: c = (NDV)BDY(t); len = LEN(c);
4950: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4951: wmax = MAX(TD(DL(a)),wmax);
4952: }
4953: homogenize_order(ord,nvar,&ord1);
4954: nd_init_ord(ord1);
4955: nd_setup_parameters(nvar+1,wmax);
4956: for ( t = fd0; t; t = NEXT(t) )
4957: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4958: }
4959: if ( MaxDeg > 0 ) nocheck = 1;
4960: while ( 1 ) {
4961: tl1 = tl2 = tl3 = tl4 = 0;
4962: if ( Demand )
4963: nd_demand = 1;
1.24 noro 4964: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4965: if ( nd_gentrace ) {
4966: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4967: }
4968: if ( ret )
4969: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4970: if ( !ret || !cand ) {
4971: /* failure */
4972: if ( trace > 1 ) { *rp = 0; return; }
4973: else m = get_lprime(++mindex);
4974: continue;
4975: }
1.15 noro 4976: if ( nd_gentrace ) {
4977: MKVECT(hvect,nd_psn);
4978: for ( i = 0; i < nd_psn; i++ )
4979: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4980: }
1.1 noro 4981: if ( !ishomo && homo ) {
4982: /* dehomogenization */
4983: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4984: nd_init_ord(ord);
4985: nd_setup_parameters(nvar,0);
4986: }
4987: nd_demand = 0;
4988: cand = ndv_reducebase(cand,perm);
4989: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4990: cand = ndv_reduceall(0,cand);
4991: cbpe = nd_bpe;
4992: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4993: get_eg(&eg0);
4994: if ( nocheck )
4995: break;
4996: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4997: if ( nd_gentrace ) {
4998: tl3 = nd_alltracelist; nd_alltracelist = 0;
4999: } else tl3 = 0;
5000: /* gbcheck : cand is a GB of Id(cand) ? */
5001: if ( nd_vc || nd_gentrace || nd_gensyz )
5002: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
5003: else
5004: ret = nd_f4(0,1,0)!=0;
5005: if ( nd_gentrace && nd_gensyz ) {
5006: tl4 = nd_alltracelist; nd_alltracelist = 0;
5007: } else tl4 = 0;
5008: }
5009: if ( ret ) break;
5010: else if ( trace > 1 ) {
5011: /* failure */
5012: *rp = 0; return;
5013: } else {
5014: /* try the next modulus */
5015: m = get_lprime(++mindex);
5016: /* reset the parameters */
5017: if ( !ishomo && homo ) {
5018: nd_init_ord(ord1);
5019: nd_setup_parameters(nvar+1,wmax);
5020: } else {
5021: nd_init_ord(ord);
5022: nd_setup_parameters(nvar,max);
5023: }
5024: }
5025: }
5026: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
5027: if ( DP_Print )
1.6 noro 5028: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 5029: /* dp->p */
5030: nd_bpe = cbpe;
5031: nd_setup_parameters(nd_nvar,0);
5032: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 5033: if ( nd_module ) {
1.17 noro 5034: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 5035: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 5036: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
5037: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 5038: }
5039: if ( nd_nalg )
5040: cand = postprocess_algcoef(av,alist,cand);
5041: MKLIST(*rp,cand);
5042: if ( nd_gentrace ) {
5043: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5044: tl3 = reverse_node(tl3);
5045: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5046: for ( t = tl2; t; t = NEXT(t) ) {
5047: /* s = [i,[*,j,*,*],...] */
5048: s = BDY((LIST)BDY(t));
1.6 noro 5049: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5050: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5051: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5052: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5053: }
5054: }
5055: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5056: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5057: }
5058: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5059: MKLIST(l5,tl4);
1.6 noro 5060: STOZ(nd_bpe,bpe);
1.15 noro 5061: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5062: }
5063: }
5064:
5065: /* XXX : module element is not considered */
5066:
5067: void dltondl(int n,DL dl,UINT *r)
5068: {
5069: UINT *d;
5070: int i,j,l,s,ord_l;
5071: struct order_pair *op;
5072:
5073: d = (unsigned int *)dl->d;
5074: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5075: if ( nd_blockmask ) {
5076: l = nd_blockmask->n;
5077: op = nd_blockmask->order_pair;
5078: for ( j = 0, s = 0; j < l; j++ ) {
5079: ord_l = op[j].length;
5080: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5081: }
5082: TD(r) = ndl_weight(r);
5083: ndl_weight_mask(r);
5084: } else {
5085: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5086: TD(r) = ndl_weight(r);
5087: }
5088: }
5089:
5090: DL ndltodl(int n,UINT *ndl)
5091: {
5092: DL dl;
5093: int *d;
5094: int i,j,l,s,ord_l;
5095: struct order_pair *op;
5096:
1.33 noro 5097: NEWDL_NOINIT(dl,n);
1.1 noro 5098: dl->td = TD(ndl);
5099: d = dl->d;
5100: if ( nd_blockmask ) {
5101: l = nd_blockmask->n;
5102: op = nd_blockmask->order_pair;
5103: for ( j = 0, s = 0; j < l; j++ ) {
5104: ord_l = op[j].length;
5105: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5106: }
5107: } else {
5108: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5109: }
5110: return dl;
5111: }
5112:
1.24 noro 5113: void _ndltodl(UINT *ndl,DL dl)
5114: {
5115: int *d;
5116: int i,j,l,s,ord_l,n;
5117: struct order_pair *op;
5118:
5119: n = nd_nvar;
5120: dl->td = TD(ndl);
5121: d = dl->d;
5122: if ( nd_blockmask ) {
5123: l = nd_blockmask->n;
5124: op = nd_blockmask->order_pair;
5125: for ( j = 0, s = 0; j < l; j++ ) {
5126: ord_l = op[j].length;
5127: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5128: }
5129: } else {
5130: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5131: }
5132: }
5133:
1.1 noro 5134: void nmtodp(int mod,NM m,DP *r)
5135: {
5136: DP dp;
5137: MP mr;
5138:
5139: NEWMP(mr);
5140: mr->dl = ndltodl(nd_nvar,DL(m));
5141: mr->c = (Obj)ndctop(mod,m->c);
5142: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5143: *r = dp;
5144: }
5145:
1.15 noro 5146: void ndltodp(UINT *d,DP *r)
5147: {
5148: DP dp;
5149: MP mr;
5150:
5151: NEWMP(mr);
5152: mr->dl = ndltodl(nd_nvar,d);
5153: mr->c = (Obj)ONE;
5154: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5155: *r = dp;
5156: }
5157:
1.1 noro 5158: void ndl_print(UINT *dl)
5159: {
5160: int n;
5161: int i,j,l,ord_l,s,s0;
5162: struct order_pair *op;
5163:
5164: n = nd_nvar;
5165: printf("<<");
5166: if ( nd_blockmask ) {
5167: l = nd_blockmask->n;
5168: op = nd_blockmask->order_pair;
5169: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5170: ord_l = op[j].length;
5171: for ( i = 0; i < ord_l; i++, s++ )
5172: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5173: }
5174: } else {
5175: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5176: }
5177: printf(">>");
5178: if ( nd_module && MPOS(dl) )
5179: printf("*e%d",MPOS(dl));
5180: }
5181:
5182: void nd_print(ND p)
5183: {
5184: NM m;
5185:
5186: if ( !p )
5187: printf("0\n");
5188: else {
5189: for ( m = BDY(p); m; m = NEXT(m) ) {
5190: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5191: else printf("+%ld*",CM(m));
1.1 noro 5192: ndl_print(DL(m));
5193: }
5194: printf("\n");
5195: }
5196: }
5197:
5198: void nd_print_q(ND p)
5199: {
5200: NM m;
5201:
5202: if ( !p )
5203: printf("0\n");
5204: else {
5205: for ( m = BDY(p); m; m = NEXT(m) ) {
5206: printf("+");
1.6 noro 5207: printexpr(CO,(Obj)CZ(m));
1.1 noro 5208: printf("*");
5209: ndl_print(DL(m));
5210: }
5211: printf("\n");
5212: }
5213: }
5214:
5215: void ndp_print(ND_pairs d)
5216: {
5217: ND_pairs t;
5218:
5219: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5220: printf("\n");
5221: }
5222:
5223: void nd_removecont(int mod,ND p)
5224: {
5225: int i,n;
5226: Z *w;
5227: NM m;
5228: struct oVECT v;
5229:
5230: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5231: else if ( mod == -2 ) {
5232: Z inv;
5233: divlf(ONE,HCZ(p),&inv);
5234: nd_mul_c_lf(p,inv);
5235: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5236: else {
5237: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5238: w = (Z *)MALLOC(n*sizeof(Q));
5239: v.len = n;
5240: v.body = (pointer *)w;
1.6 noro 5241: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5242: removecont_array((P *)w,n,1);
1.6 noro 5243: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5244: }
5245: }
5246:
5247: void nd_removecont2(ND p1,ND p2)
5248: {
5249: int i,n1,n2,n;
5250: Z *w;
5251: NM m;
5252: struct oVECT v;
5253:
5254: n1 = nd_length(p1);
5255: n2 = nd_length(p2);
5256: n = n1+n2;
5257: w = (Z *)MALLOC(n*sizeof(Q));
5258: v.len = n;
5259: v.body = (pointer *)w;
5260: i = 0;
5261: if ( p1 )
1.6 noro 5262: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5263: if ( p2 )
1.6 noro 5264: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5265: removecont_array((P *)w,n,1);
5266: i = 0;
5267: if ( p1 )
1.6 noro 5268: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5269: if ( p2 )
1.6 noro 5270: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5271: }
5272:
5273: void ndv_removecont(int mod,NDV p)
5274: {
5275: int i,len,all_p;
5276: Z *c;
5277: P *w;
5278: Z dvr,t;
5279: P g,cont,tp;
5280: NMV m;
5281:
5282: if ( mod == -1 )
5283: ndv_mul_c(mod,p,_invsf(HCM(p)));
5284: else if ( mod == -2 ) {
5285: Z inv;
5286: divlf(ONE,HCZ(p),&inv);
5287: ndv_mul_c_lf(p,inv);
5288: } else if ( mod )
5289: ndv_mul_c(mod,p,invm(HCM(p),mod));
5290: else {
5291: len = p->len;
5292: w = (P *)MALLOC(len*sizeof(P));
5293: c = (Z *)MALLOC(len*sizeof(Q));
5294: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5295: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5296: all_p = all_p && !NUM(w[i]);
5297: }
5298: if ( all_p ) {
5299: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5300: mulp(nd_vc,(P)dvr,g,&cont);
5301: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5302: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5303: }
5304: } else {
5305: sortbynm((Q *)c,len);
5306: qltozl((Q *)c,len,&dvr);
5307: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5308: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5309: }
5310: }
5311: }
5312: }
5313:
5314: /* koko */
5315:
5316: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5317: {
5318: int len,i,max;
5319: NMV m,mr0,mr,t;
5320:
5321: len = p->len;
1.14 noro 5322: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5323: max = MAX(max,TD(DL(m)));
5324: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5325: m = (NMV)((char *)mr0+(len-1)*oadv);
5326: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5327: t = (NMV)MALLOC(nmv_adv);
5328: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5329: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5330: CZ(mr) = CZ(m);
1.1 noro 5331: ndl_copy(DL(t),DL(mr));
5332: }
5333: NV(p)++;
5334: BDY(p) = mr0;
5335: }
5336:
5337: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5338: {
5339: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5340: int pos;
5341: Q *w;
5342: Q dvr,t;
5343: NMV m,r;
5344:
5345: len = p->len;
5346: newnvar = nd_nvar-1;
5347: newexporigin = nd_get_exporigin(ord);
5348: if ( nd_module ) newmpos = newexporigin-1;
5349: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5350: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5351: ndl_dehomogenize(DL(m));
5352: if ( newwpd != nd_wpd ) {
5353: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5354: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5355: CZ(r) = CZ(m);
1.1 noro 5356: if ( nd_module ) pos = MPOS(DL(m));
5357: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5358: adj = nd_exporigin-newexporigin;
5359: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5360: if ( nd_module ) {
5361: DL(r)[newmpos] = pos;
5362: }
5363: }
5364: }
5365: NV(p)--;
5366: }
5367:
5368: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5369: {
5370: int i;
5371: P *tpl,*tpl1;
5372: NODE l;
5373: P h,gcd,t;
5374:
5375: tpl = (P *)MALLOC(m*sizeof(P));
5376: tpl1 = (P *)MALLOC(m*sizeof(P));
5377: bcopy(pl,tpl,m*sizeof(P));
5378: gcd = (P)ONE;
5379: for ( l = nd_hcf; l; l = NEXT(l) ) {
5380: h = (P)BDY(l);
5381: while ( 1 ) {
5382: for ( i = 0; i < m; i++ )
5383: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5384: break;
5385: if ( i == m ) {
5386: bcopy(tpl1,tpl,m*sizeof(P));
5387: mulp(vl,gcd,h,&t); gcd = t;
5388: } else
5389: break;
5390: }
5391: }
5392: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5393: if ( full ) {
5394: heu_nezgcdnpz(vl,tpl,m,&t);
5395: mulp(vl,gcd,t,pr);
5396: } else
5397: *pr = gcd;
5398: }
5399:
5400: void removecont_array(P *p,int n,int full)
5401: {
5402: int all_p,all_q,i;
5403: Z *c;
5404: P *w;
5405: P t,s;
5406:
5407: for ( all_q = 1, i = 0; i < n; i++ )
5408: all_q = all_q && NUM(p[i]);
5409: if ( all_q ) {
5410: removecont_array_q((Z *)p,n);
5411: } else {
5412: c = (Z *)MALLOC(n*sizeof(Z));
5413: w = (P *)MALLOC(n*sizeof(P));
5414: for ( i = 0; i < n; i++ ) {
5415: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5416: }
5417: removecont_array_q(c,n);
5418: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5419: for ( i = 0; i < n; i++ ) {
5420: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5421: }
5422: }
5423: }
5424:
5425: /* c is an int array */
5426:
5427: void removecont_array_q(Z *c,int n)
5428: {
5429: struct oVECT v;
5430: Z d0,d1,a,u,u1,gcd;
5431: int i,j;
5432: Z *q,*r;
5433:
5434: q = (Z *)MALLOC(n*sizeof(Z));
5435: r = (Z *)MALLOC(n*sizeof(Z));
5436: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5437: gcdvz_estimate(&v,&d0);
5438: for ( i = 0; i < n; i++ ) {
5439: divqrz(c[i],d0,&q[i],&r[i]);
5440: }
5441: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5442: if ( i < n ) {
5443: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5444: gcdvz(&v,&d1);
5445: gcdz(d0,d1,&gcd);
1.6 noro 5446: /* exact division */
5447: divsz(d0,gcd,&a);
1.1 noro 5448: for ( i = 0; i < n; i++ ) {
5449: mulz(a,q[i],&u);
5450: if ( r[i] ) {
1.6 noro 5451: /* exact division */
5452: divsz(r[i],gcd,&u1);
1.1 noro 5453: addz(u,u1,&q[i]);
5454: } else
5455: q[i] = u;
5456: }
5457: }
5458: for ( i = 0; i < n; i++ ) c[i] = q[i];
5459: }
5460:
1.4 noro 5461: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5462:
5463: void mpz_removecont_array(mpz_t *c,int n)
5464: {
5465: mpz_t d0,a,u,u1,gcd;
5466: int i,j;
1.13 noro 5467: static mpz_t *q,*r;
5468: static int c_len = 0;
1.4 noro 5469:
5470: for ( i = 0; i < n; i++ )
5471: if ( mpz_sgn(c[i]) ) break;
5472: if ( i == n ) return;
5473: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5474: if ( n > c_len ) {
5475: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5476: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5477: c_len = n;
5478: }
1.4 noro 5479: for ( i = 0; i < n; i++ ) {
5480: mpz_init(q[i]); mpz_init(r[i]);
5481: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5482: }
5483: for ( i = 0; i < n; i++ )
5484: if ( mpz_sgn(r[i]) ) break;
5485: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5486: if ( i < n ) {
5487: mpz_gcd(gcd,d0,r[i]);
5488: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5489: mpz_div(a,d0,gcd);
5490: for ( i = 0; i < n; i++ ) {
5491: mpz_mul(u,a,q[i]);
5492: if ( mpz_sgn(r[i]) ) {
5493: mpz_div(u1,r[i],gcd);
5494: mpz_add(q[i],u,u1);
5495: } else
5496: mpz_set(q[i],u);
5497: }
5498: }
5499: for ( i = 0; i < n; i++ )
5500: mpz_set(c[i],q[i]);
5501: }
5502:
1.1 noro 5503: void nd_mul_c(int mod,ND p,int mul)
5504: {
5505: NM m;
5506: int c,c1;
5507:
5508: if ( !p ) return;
5509: if ( mul == 1 ) return;
5510: if ( mod == -1 )
5511: for ( m = BDY(p); m; m = NEXT(m) )
5512: CM(m) = _mulsf(CM(m),mul);
5513: else
5514: for ( m = BDY(p); m; m = NEXT(m) ) {
5515: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5516: }
5517: }
5518:
5519: void nd_mul_c_lf(ND p,Z mul)
5520: {
5521: NM m;
5522: Z c;
5523:
5524: if ( !p ) return;
5525: if ( UNIZ(mul) ) return;
5526: for ( m = BDY(p); m; m = NEXT(m) ) {
5527: mullf(CZ(m),mul,&c); CZ(m) = c;
5528: }
5529: }
5530:
5531: void nd_mul_c_q(ND p,P mul)
5532: {
5533: NM m;
5534: P c;
5535:
5536: if ( !p ) return;
5537: if ( UNIQ(mul) ) return;
5538: for ( m = BDY(p); m; m = NEXT(m) ) {
5539: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5540: }
5541: }
5542:
5543: void nd_mul_c_p(VL vl,ND p,P mul)
5544: {
5545: NM m;
5546: P c;
5547:
5548: if ( !p ) return;
5549: for ( m = BDY(p); m; m = NEXT(m) ) {
5550: mulp(vl,CP(m),mul,&c); CP(m) = c;
5551: }
5552: }
5553:
5554: void nd_free(ND p)
5555: {
5556: NM t,s;
5557:
5558: if ( !p ) return;
5559: t = BDY(p);
5560: while ( t ) {
5561: s = NEXT(t);
5562: FREENM(t);
5563: t = s;
5564: }
5565: FREEND(p);
5566: }
5567:
5568: void ndv_free(NDV p)
5569: {
5570: GCFREE(BDY(p));
5571: }
5572:
5573: void nd_append_red(UINT *d,int i)
5574: {
5575: RHist m,m0;
5576: int h;
5577:
5578: NEWRHist(m);
5579: h = ndl_hash_value(d);
5580: m->index = i;
5581: ndl_copy(d,DL(m));
5582: NEXT(m) = nd_red[h];
5583: nd_red[h] = m;
5584: }
5585:
5586: UINT *ndv_compute_bound(NDV p)
5587: {
5588: UINT *d1,*d2,*t;
5589: UINT u;
5590: int i,j,k,l,len,ind;
5591: NMV m;
5592:
5593: if ( !p )
5594: return 0;
5595: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5596: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5597: len = LEN(p);
5598: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5599: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5600: ndl_max(DL(m),d1,d2);
5601: t = d1; d1 = d2; d2 = t;
5602: }
5603: l = nd_nvar+31;
5604: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5605: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5606: u = d1[i];
5607: k = (nd_epw-1)*nd_bpe;
5608: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5609: t[ind] = (u>>k)&nd_mask0;
5610: }
5611: for ( ; ind < l; ind++ ) t[ind] = 0;
5612: return t;
5613: }
5614:
5615: UINT *nd_compute_bound(ND p)
5616: {
5617: UINT *d1,*d2,*t;
5618: UINT u;
5619: int i,j,k,l,len,ind;
5620: NM m;
5621:
5622: if ( !p )
5623: return 0;
5624: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5625: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5626: len = LEN(p);
5627: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5628: for ( m = NEXT(m); m; m = NEXT(m) ) {
5629: ndl_lcm(DL(m),d1,d2);
5630: t = d1; d1 = d2; d2 = t;
5631: }
5632: l = nd_nvar+31;
5633: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5634: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5635: u = d1[i];
5636: k = (nd_epw-1)*nd_bpe;
5637: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5638: t[ind] = (u>>k)&nd_mask0;
5639: }
5640: for ( ; ind < l; ind++ ) t[ind] = 0;
5641: return t;
5642: }
5643:
5644: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5645: /* of a term. In this case we need additional 1 word. */
5646:
5647: int nd_get_exporigin(struct order_spec *ord)
5648: {
5649: switch ( ord->id ) {
1.21 noro 5650: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5651: return 1+nd_module;
5652: case 1: case 257:
5653: /* block order */
5654: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5655: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5656: return ord->ord.block.length+1+nd_module;
5657: case 3: case 259:
5658: #if 0
5659: error("nd_get_exporigin : composite order is not supported yet.");
5660: #else
5661: return 1+nd_module;
5662: #endif
5663: default:
5664: error("nd_get_exporigin : ivalid argument.");
5665: return 0;
5666: }
5667: }
5668:
5669: void nd_setup_parameters(int nvar,int max) {
5670: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5671: struct order_pair *op;
5672:
5673: nd_nvar = nvar;
5674: if ( max ) {
5675: /* XXX */
5676: if ( do_weyl ) nd_bpe = 32;
5677: else if ( max < 2 ) nd_bpe = 1;
5678: else if ( max < 4 ) nd_bpe = 2;
5679: else if ( max < 8 ) nd_bpe = 3;
5680: else if ( max < 16 ) nd_bpe = 4;
5681: else if ( max < 32 ) nd_bpe = 5;
5682: else if ( max < 64 ) nd_bpe = 6;
5683: else if ( max < 256 ) nd_bpe = 8;
5684: else if ( max < 1024 ) nd_bpe = 10;
5685: else if ( max < 65536 ) nd_bpe = 16;
5686: else nd_bpe = 32;
5687: }
5688: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5689: UINT t;
5690: int st;
5691: int *v;
5692: /* t = max(weights) */
5693: t = 0;
5694: if ( current_dl_weight_vector )
5695: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5696: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5697: if ( t < st ) t = st;
5698: }
5699: if ( nd_matrix )
5700: for ( i = 0; i < nd_matrix_len; i++ )
5701: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5702: if ( (st=v[j]) < 0 ) st = -st;
5703: if ( t < st ) t = st;
5704: }
5705: /* i = bitsize of t */
5706: for ( i = 0; t; t >>=1, i++ );
5707: /* i += bitsize of nd_nvar */
5708: for ( t = nd_nvar; t; t >>=1, i++);
5709: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5710: if ( (nd_bpe+i) >= 31 )
5711: error("nd_setup_parameters : too large weight");
5712: }
5713: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5714: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5715: nd_exporigin = nd_get_exporigin(nd_ord);
5716: wpd = nd_exporigin+elen;
5717: if ( nd_module )
5718: nd_mpos = nd_exporigin-1;
5719: else
5720: nd_mpos = -1;
5721: if ( wpd != nd_wpd ) {
5722: nd_free_private_storage();
5723: nd_wpd = wpd;
5724: }
5725: if ( nd_bpe < 32 ) {
5726: nd_mask0 = (1<<nd_bpe)-1;
5727: } else {
5728: nd_mask0 = 0xffffffff;
5729: }
5730: bzero(nd_mask,sizeof(nd_mask));
5731: nd_mask1 = 0;
5732: for ( i = 0; i < nd_epw; i++ ) {
5733: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5734: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5735: }
5736: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5737: nd_epos = nd_create_epos(nd_ord);
5738: nd_blockmask = nd_create_blockmask(nd_ord);
5739: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5740: }
5741:
5742: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5743: {
5744: int i,obpe,oadv,h;
5745: static NM prev_nm_free_list;
5746: static ND_pairs prev_ndp_free_list;
5747: RHist mr0,mr;
5748: RHist r;
5749: RHist *old_red;
5750: ND_pairs s0,s,t;
5751: EPOS oepos;
5752:
5753: obpe = nd_bpe;
5754: oadv = nmv_adv;
5755: oepos = nd_epos;
5756: if ( obpe < 2 ) nd_bpe = 2;
5757: else if ( obpe < 3 ) nd_bpe = 3;
5758: else if ( obpe < 4 ) nd_bpe = 4;
5759: else if ( obpe < 5 ) nd_bpe = 5;
5760: else if ( obpe < 6 ) nd_bpe = 6;
5761: else if ( obpe < 8 ) nd_bpe = 8;
5762: else if ( obpe < 10 ) nd_bpe = 10;
5763: else if ( obpe < 16 ) nd_bpe = 16;
5764: else if ( obpe < 32 ) nd_bpe = 32;
5765: else error("nd_reconstruct : exponent too large");
5766:
5767: nd_setup_parameters(nd_nvar,0);
5768: prev_nm_free_list = _nm_free_list;
5769: prev_ndp_free_list = _ndp_free_list;
5770: _nm_free_list = 0;
5771: _ndp_free_list = 0;
5772: for ( i = nd_psn-1; i >= 0; i-- ) {
5773: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5774: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5775: }
5776: if ( trace )
5777: for ( i = nd_psn-1; i >= 0; i-- ) {
5778: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5779: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5780: }
5781: s0 = 0;
5782: for ( t = d; t; t = NEXT(t) ) {
5783: NEXTND_pairs(s0,s);
5784: s->i1 = t->i1;
5785: s->i2 = t->i2;
1.24 noro 5786: s->sig = t->sig;
1.1 noro 5787: SG(s) = SG(t);
5788: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5789: }
5790:
5791: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5792: for ( i = 0; i < REDTAB_LEN; i++ ) {
5793: old_red[i] = nd_red[i];
5794: nd_red[i] = 0;
5795: }
5796: for ( i = 0; i < REDTAB_LEN; i++ )
5797: for ( r = old_red[i]; r; r = NEXT(r) ) {
5798: NEWRHist(mr);
5799: mr->index = r->index;
5800: SG(mr) = SG(r);
5801: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5802: h = ndl_hash_value(DL(mr));
5803: NEXT(mr) = nd_red[h];
5804: nd_red[h] = mr;
1.24 noro 5805: mr->sig = r->sig;
1.1 noro 5806: }
5807: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5808: old_red = 0;
5809: for ( i = 0; i < nd_psn; i++ ) {
5810: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5811: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5812: r->sig = nd_psh[i]->sig;
1.1 noro 5813: nd_psh[i] = r;
5814: }
5815: if ( s0 ) NEXT(s) = 0;
5816: prev_nm_free_list = 0;
5817: prev_ndp_free_list = 0;
5818: #if 0
5819: GC_gcollect();
5820: #endif
5821: return s0;
5822: }
5823:
1.30 noro 5824: void nd_reconstruct_s(int trace,ND_pairs *d)
5825: {
5826: int i,obpe,oadv,h;
5827: static NM prev_nm_free_list;
5828: static ND_pairs prev_ndp_free_list;
5829: RHist mr0,mr;
5830: RHist r;
5831: RHist *old_red;
5832: ND_pairs s0,s,t;
5833: EPOS oepos;
5834:
5835: obpe = nd_bpe;
5836: oadv = nmv_adv;
5837: oepos = nd_epos;
5838: if ( obpe < 2 ) nd_bpe = 2;
5839: else if ( obpe < 3 ) nd_bpe = 3;
5840: else if ( obpe < 4 ) nd_bpe = 4;
5841: else if ( obpe < 5 ) nd_bpe = 5;
5842: else if ( obpe < 6 ) nd_bpe = 6;
5843: else if ( obpe < 8 ) nd_bpe = 8;
5844: else if ( obpe < 10 ) nd_bpe = 10;
5845: else if ( obpe < 16 ) nd_bpe = 16;
5846: else if ( obpe < 32 ) nd_bpe = 32;
5847: else error("nd_reconstruct_s : exponent too large");
5848:
5849: nd_setup_parameters(nd_nvar,0);
5850: prev_nm_free_list = _nm_free_list;
5851: prev_ndp_free_list = _ndp_free_list;
5852: _nm_free_list = 0;
5853: _ndp_free_list = 0;
5854: for ( i = nd_psn-1; i >= 0; i-- ) {
5855: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5856: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5857: }
5858: if ( trace )
5859: for ( i = nd_psn-1; i >= 0; i-- ) {
5860: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5861: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5862: }
5863:
5864: for ( i = 0; i < nd_nbase; i++ ) {
5865: s0 = 0;
5866: for ( t = d[i]; t; t = NEXT(t) ) {
5867: NEXTND_pairs(s0,s);
5868: s->i1 = t->i1;
5869: s->i2 = t->i2;
5870: s->sig = t->sig;
5871: SG(s) = SG(t);
5872: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5873: }
5874: d[i] = s0;
5875: }
5876:
5877: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5878: for ( i = 0; i < REDTAB_LEN; i++ ) {
5879: old_red[i] = nd_red[i];
5880: nd_red[i] = 0;
5881: }
5882: for ( i = 0; i < REDTAB_LEN; i++ )
5883: for ( r = old_red[i]; r; r = NEXT(r) ) {
5884: NEWRHist(mr);
5885: mr->index = r->index;
5886: SG(mr) = SG(r);
5887: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5888: h = ndl_hash_value(DL(mr));
5889: NEXT(mr) = nd_red[h];
5890: nd_red[h] = mr;
5891: mr->sig = r->sig;
5892: }
5893: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5894: old_red = 0;
5895: for ( i = 0; i < nd_psn; i++ ) {
5896: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5897: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
5898: r->sig = nd_psh[i]->sig;
5899: nd_psh[i] = r;
5900: }
5901: if ( s0 ) NEXT(s) = 0;
5902: prev_nm_free_list = 0;
5903: prev_ndp_free_list = 0;
5904: #if 0
5905: GC_gcollect();
5906: #endif
5907: }
5908:
1.1 noro 5909: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5910: {
5911: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5912: struct order_pair *op;
5913:
5914: n = nd_nvar;
5915: oepw = (sizeof(UINT)*8)/obpe;
5916: omask0 = (1<<obpe)-1;
5917: TD(r) = TD(d);
5918: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5919: if ( nd_blockmask ) {
5920: l = nd_blockmask->n;
5921: op = nd_blockmask->order_pair;
5922: for ( i = 1; i < nd_exporigin; i++ )
5923: r[i] = d[i];
5924: for ( j = 0, s = 0; j < l; j++ ) {
5925: ord_l = op[j].length;
5926: for ( i = 0; i < ord_l; i++, s++ ) {
5927: ei = GET_EXP_OLD(d,s);
5928: PUT_EXP(r,s,ei);
5929: }
5930: }
5931: } else {
5932: for ( i = 0; i < n; i++ ) {
5933: ei = GET_EXP_OLD(d,i);
5934: PUT_EXP(r,i,ei);
5935: }
5936: }
5937: if ( nd_module ) MPOS(r) = MPOS(d);
5938: }
5939:
5940: ND nd_copy(ND p)
5941: {
5942: NM m,mr,mr0;
5943: int c,n;
5944: ND r;
5945:
5946: if ( !p )
5947: return 0;
5948: else {
5949: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5950: NEXTNM(mr0,mr);
5951: CM(mr) = CM(m);
5952: ndl_copy(DL(m),DL(mr));
5953: }
5954: NEXT(mr) = 0;
5955: MKND(NV(p),mr0,LEN(p),r);
5956: SG(r) = SG(p);
5957: return r;
5958: }
5959: }
5960:
5961: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5962: {
5963: NM m1,m2;
5964: NDV p1,p2;
5965: ND t1,t2;
5966: UINT *lcm;
5967: P gp,tp;
5968: Z g,t;
5969: Z iq;
5970: int td;
5971: LIST hist;
5972: NODE node;
5973: DP d;
5974:
5975: if ( !mod && nd_demand ) {
5976: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5977: } else {
5978: if ( trace ) {
5979: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5980: } else {
5981: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5982: }
5983: }
5984: lcm = LCM(p);
5985: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5986: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5987: FREENM(m1); return 0;
5988: }
5989: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5990: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5991: FREENM(m1); FREENM(m2); return 0;
5992: }
5993:
5994: if ( mod == -1 ) {
5995: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5996: } else if ( mod > 0 ) {
5997: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5998: } else if ( mod == -2 ) {
5999: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
6000: } else if ( nd_vc ) {
6001: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
6002: divsp(nd_vc,HCP(p2),gp,&CP(m1));
6003: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
6004: } else {
1.6 noro 6005: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 6006: }
6007: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
6008: *rp = nd_add(mod,t1,t2);
6009: if ( nd_gentrace ) {
6010: /* nd_tracelist is initialized */
1.6 noro 6011: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6012: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 6013: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6014: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
6015: nd_tracelist = node;
6016: }
1.24 noro 6017: if ( *rp )
6018: (*rp)->sig = p->sig;
1.1 noro 6019: FREENM(m1); FREENM(m2);
6020: return 1;
6021: }
6022:
6023: void ndv_mul_c(int mod,NDV p,int mul)
6024: {
6025: NMV m;
6026: int c,c1,len,i;
6027:
6028: if ( !p ) return;
6029: len = LEN(p);
6030: if ( mod == -1 )
6031: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
6032: CM(m) = _mulsf(CM(m),mul);
6033: else
6034: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6035: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6036: }
6037: }
6038:
6039: void ndv_mul_c_lf(NDV p,Z mul)
6040: {
6041: NMV m;
6042: Z c;
6043: int len,i;
6044:
6045: if ( !p ) return;
6046: len = LEN(p);
6047: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6048: mullf(CZ(m),mul,&c); CZ(m) = c;
6049: }
6050: }
6051:
6052: /* for nd_det */
6053: void ndv_mul_c_q(NDV p,Z mul)
6054: {
6055: NMV m;
6056: Z c;
6057: int len,i;
6058:
6059: if ( !p ) return;
6060: len = LEN(p);
6061: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6062: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6063: }
6064: }
6065:
6066: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6067: int n2,i,j,l,n,tlen;
6068: UINT *d0;
6069: NM *tab,*psum;
6070: ND s,r;
6071: NM t;
6072: NMV m1;
6073:
6074: if ( !p ) return 0;
6075: n = NV(p); n2 = n>>1;
6076: d0 = DL(m0);
6077: l = LEN(p);
6078: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6079: tab = (NM *)MALLOC(tlen*sizeof(NM));
6080: psum = (NM *)MALLOC(tlen*sizeof(NM));
6081: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6082: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6083: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6084: /* m0(NM) * m1(NMV) => tab(NM) */
6085: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6086: for ( j = 0; j < tlen; j++ ) {
6087: if ( tab[j] ) {
6088: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6089: }
6090: }
6091: }
6092: for ( i = tlen-1, r = 0; i >= 0; i-- )
6093: if ( psum[i] ) {
6094: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6095: MKND(n,psum[i],j,s);
6096: r = nd_add(mod,r,s);
6097: }
6098: if ( r ) SG(r) = SG(p)+TD(d0);
6099: return r;
6100: }
6101:
6102: /* product of monomials */
6103: /* XXX block order is not handled correctly */
6104:
6105: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6106: {
6107: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6108: UINT *d0,*d1,*d,*dt,*ctab;
6109: Z *ctab_q;
6110: Z q,q1;
6111: UINT c0,c1,c;
6112: NM *p;
6113: NM m,t;
6114: int mpos;
6115:
6116: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6117: if ( !m0 || !m1 ) return;
6118: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6119: if ( nd_module )
6120: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6121:
6122: NEWNM(m); d = DL(m);
6123: if ( mod ) {
6124: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6125: } else if ( nd_vc )
6126: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6127: else
1.6 noro 6128: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6129: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6130: homo = n&1 ? 1 : 0;
6131: if ( homo ) {
6132: /* offset of h-degree */
6133: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6134: PUT_EXP(DL(m),n-1,h);
6135: TD(DL(m)) = h;
6136: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6137: }
6138: tab[0] = m;
6139: NEWNM(m); d = DL(m);
6140: for ( i = 0, curlen = 1; i < n2; i++ ) {
6141: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6142: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6143: /* xi^a*(Di^k*xi^l)*Di^b */
6144: a += l; b += k;
6145: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6146: if ( !k || !l ) {
6147: for ( j = 0; j < curlen; j++ )
6148: if ( (t = tab[j]) != 0 ) {
6149: dt = DL(t);
6150: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6151: if ( nd_blockmask ) ndl_weight_mask(dt);
6152: }
6153: curlen *= k+1;
6154: continue;
6155: }
6156: min = MIN(k,l);
6157: if ( mod ) {
6158: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6159: mkwcm(k,l,mod,(int *)ctab);
6160: } else {
6161: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6162: mkwc(k,l,ctab_q);
6163: }
6164: for ( j = min; j >= 0; j-- ) {
6165: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6166: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6167: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6168: if ( homo ) {
6169: TD(d) = s;
6170: PUT_EXP(d,n-1,s-h);
6171: } else TD(d) = h;
6172: if ( nd_blockmask ) ndl_weight_mask(d);
6173: if ( mod ) c = ctab[j];
6174: else q = ctab_q[j];
6175: p = tab+curlen*j;
6176: if ( j == 0 ) {
6177: for ( u = 0; u < curlen; u++, p++ ) {
6178: if ( tab[u] ) {
6179: ndl_addto(DL(tab[u]),d);
6180: if ( mod ) {
6181: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6182: } else if ( nd_vc )
6183: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6184: else {
1.6 noro 6185: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6186: }
6187: }
6188: }
6189: } else {
6190: for ( u = 0; u < curlen; u++, p++ ) {
6191: if ( tab[u] ) {
6192: NEWNM(t);
6193: ndl_add(DL(tab[u]),d,DL(t));
6194: if ( mod ) {
6195: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6196: } else if ( nd_vc )
6197: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6198: else
1.6 noro 6199: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6200: *p = t;
6201: }
6202: }
6203: }
6204: }
6205: curlen *= k+1;
6206: }
6207: FREENM(m);
6208: if ( nd_module ) {
6209: mpos = MPOS(d1);
6210: for ( i = 0; i < tlen; i++ )
6211: if ( tab[i] ) {
6212: d = DL(tab[i]);
6213: MPOS(d) = mpos;
6214: TD(d) = ndl_weight(d);
6215: }
6216: }
6217: }
6218:
6219: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6220: {
6221: NM mr,mr0;
6222: NMV m;
6223: UINT *d,*dt,*dm;
6224: int c,n,td,i,c1,c2,len;
6225: Q q;
6226: ND r;
6227:
6228: if ( !p ) return 0;
6229: else {
6230: n = NV(p); m = BDY(p);
6231: d = DL(m0);
6232: len = LEN(p);
6233: mr0 = 0;
6234: td = TD(d);
6235: c = CM(m0);
6236: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6237: NEXTNM(mr0,mr);
6238: CM(mr) = 1;
6239: ndl_add(DL(m),d,DL(mr));
6240: }
6241: NEXT(mr) = 0;
6242: MKND(NV(p),mr0,len,r);
6243: SG(r) = SG(p) + TD(d);
6244: return r;
6245: }
6246: }
6247:
6248: ND ndv_mul_nm(int mod,NM m0,NDV p)
6249: {
6250: NM mr,mr0;
6251: NMV m;
6252: UINT *d,*dt,*dm;
6253: int c,n,td,i,c1,c2,len;
6254: P q;
6255: ND r;
6256:
6257: if ( !p ) return 0;
6258: else if ( do_weyl ) {
6259: if ( mod < 0 ) {
6260: error("ndv_mul_nm : not implemented (weyl)");
6261: return 0;
6262: } else
6263: return weyl_ndv_mul_nm(mod,m0,p);
6264: } else {
6265: n = NV(p); m = BDY(p);
6266: d = DL(m0);
6267: len = LEN(p);
6268: mr0 = 0;
6269: td = TD(d);
6270: if ( mod == -1 ) {
6271: c = CM(m0);
6272: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6273: NEXTNM(mr0,mr);
6274: CM(mr) = _mulsf(CM(m),c);
6275: ndl_add(DL(m),d,DL(mr));
6276: }
6277: } else if ( mod == -2 ) {
6278: Z cl;
6279: cl = CZ(m0);
6280: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6281: NEXTNM(mr0,mr);
6282: mullf(CZ(m),cl,&CZ(mr));
6283: ndl_add(DL(m),d,DL(mr));
6284: }
6285: } else if ( mod ) {
6286: c = CM(m0);
6287: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6288: NEXTNM(mr0,mr);
6289: c1 = CM(m);
6290: DMAR(c1,c,0,mod,c2);
6291: CM(mr) = c2;
6292: ndl_add(DL(m),d,DL(mr));
6293: }
6294: } else {
6295: q = CP(m0);
6296: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6297: NEXTNM(mr0,mr);
6298: mulp(nd_vc,CP(m),q,&CP(mr));
6299: ndl_add(DL(m),d,DL(mr));
6300: }
6301: }
6302: NEXT(mr) = 0;
6303: MKND(NV(p),mr0,len,r);
6304: SG(r) = SG(p) + TD(d);
6305: return r;
6306: }
6307: }
6308:
6309: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6310: {
6311: NM mq0,mq;
6312: NMV tm;
6313: Q q;
6314: int i,nv,sg,c,c1,c2,hindex;
6315: ND p,t,r;
6316:
6317: if ( bucket->m < 0 ) return 0;
6318: else {
6319: nv = NV(d);
6320: mq0 = 0;
6321: tm = (NMV)MALLOC(nmv_adv);
6322: while ( 1 ) {
6323: if ( mod > 0 || mod == -1 )
6324: hindex = head_pbucket(mod,bucket);
6325: else if ( mod == -2 )
6326: hindex = head_pbucket_lf(bucket);
6327: else
6328: hindex = head_pbucket_q(bucket);
6329: if ( hindex < 0 ) break;
6330: p = bucket->body[hindex];
6331: NEXTNM(mq0,mq);
6332: ndl_sub(HDL(p),HDL(d),DL(mq));
6333: ndl_copy(DL(mq),DL(tm));
6334: if ( mod ) {
6335: c1 = invm(HCM(d),mod); c2 = HCM(p);
6336: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6337: CM(tm) = mod-c;
6338: } else {
1.6 noro 6339: divsz(HCZ(p),HCZ(d),&CZ(mq));
6340: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6341: }
6342: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6343: bucket->body[hindex] = nd_remove_head(p);
6344: t = nd_remove_head(t);
6345: add_pbucket(mod,bucket,t);
6346: }
6347: if ( !mq0 )
6348: r = 0;
6349: else {
6350: NEXT(mq) = 0;
6351: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6352: MKND(nv,mq0,i,r);
6353: /* XXX */
6354: SG(r) = HTD(r);
6355: }
6356: return r;
6357: }
6358: }
6359:
6360: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6361: {
6362: NMV m,mr,mr0,t;
6363: int len,i,k;
6364:
6365: if ( !p ) return;
6366: m = BDY(p); len = LEN(p);
6367: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6368: m = (NMV)((char *)mr0+(len-1)*oadv);
6369: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6370: t = (NMV)MALLOC(nmv_adv);
6371: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6372: CZ(t) = CZ(m);
1.1 noro 6373: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6374: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6375: CZ(mr) = CZ(t);
1.1 noro 6376: ndl_copy(DL(t),DL(mr));
6377: }
6378: BDY(p) = mr0;
6379: }
6380:
6381: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6382: {
6383: NMV m,mr,mr0;
6384: int len,i;
6385: NDV r;
6386:
6387: if ( !p ) return 0;
6388: m = BDY(p); len = LEN(p);
6389: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6390: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6391: ndl_zero(DL(mr));
6392: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6393: CZ(mr) = CZ(m);
1.1 noro 6394: }
6395: MKNDV(NV(p),mr0,len,r);
6396: SG(r) = SG(p);
1.24 noro 6397: r->sig = p->sig;
1.1 noro 6398: return r;
6399: }
6400:
6401: /* duplicate p */
6402:
6403: NDV ndv_dup(int mod,NDV p)
6404: {
6405: NDV d;
6406: NMV t,m,m0;
6407: int i,len;
6408:
6409: if ( !p ) return 0;
6410: len = LEN(p);
6411: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6412: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6413: ndl_copy(DL(t),DL(m));
1.6 noro 6414: CZ(m) = CZ(t);
1.1 noro 6415: }
6416: MKNDV(NV(p),m0,len,d);
6417: SG(d) = SG(p);
6418: return d;
6419: }
6420:
6421: NDV ndv_symbolic(int mod,NDV p)
6422: {
6423: NDV d;
6424: NMV t,m,m0;
6425: int i,len;
6426:
6427: if ( !p ) return 0;
6428: len = LEN(p);
6429: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6430: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6431: ndl_copy(DL(t),DL(m));
1.6 noro 6432: CZ(m) = ONE;
1.1 noro 6433: }
6434: MKNDV(NV(p),m0,len,d);
6435: SG(d) = SG(p);
6436: return d;
6437: }
6438:
6439: ND nd_dup(ND p)
6440: {
6441: ND d;
6442: NM t,m,m0;
6443:
6444: if ( !p ) return 0;
6445: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6446: NEXTNM(m0,m);
6447: ndl_copy(DL(t),DL(m));
1.6 noro 6448: CZ(m) = CZ(t);
1.1 noro 6449: }
6450: if ( m0 ) NEXT(m) = 0;
6451: MKND(NV(p),m0,LEN(p),d);
6452: SG(d) = SG(p);
6453: return d;
6454: }
6455:
6456: /* XXX if p->len == 0 then it represents 0 */
6457:
6458: void ndv_mod(int mod,NDV p)
6459: {
6460: NMV t,d;
6461: int r,s,u;
6462: int i,len,dlen;
6463: P cp;
6464: Z c;
6465: Obj gfs;
6466:
6467: if ( !p ) return;
6468: len = LEN(p);
6469: dlen = 0;
6470: if ( mod == -1 )
6471: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6472: simp_ff((Obj)CP(t),&gfs);
6473: if ( gfs ) {
6474: r = FTOIF(CONT((GFS)gfs));
6475: CM(d) = r;
6476: ndl_copy(DL(t),DL(d));
6477: NMV_ADV(d);
6478: dlen++;
6479: }
6480: }
6481: else if ( mod == -2 )
6482: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6483: simp_ff((Obj)CP(t),&gfs);
6484: if ( gfs ) {
6485: lmtolf((LM)gfs,&CZ(d));
6486: ndl_copy(DL(t),DL(d));
6487: NMV_ADV(d);
6488: dlen++;
6489: }
6490: }
6491: else
6492: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6493: if ( nd_vc ) {
6494: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6495: c = (Z)cp;
6496: } else
1.6 noro 6497: c = CZ(t);
1.1 noro 6498: r = remqi((Q)c,mod);
6499: if ( r ) {
6500: CM(d) = r;
6501: ndl_copy(DL(t),DL(d));
6502: NMV_ADV(d);
6503: dlen++;
6504: }
6505: }
6506: LEN(p) = dlen;
6507: }
6508:
6509: NDV ptondv(VL vl,VL dvl,P p)
6510: {
6511: ND nd;
6512:
6513: nd = ptond(vl,dvl,p);
6514: return ndtondv(0,nd);
6515: }
6516:
6517: void pltozpl(LIST l,Q *cont,LIST *pp)
6518: {
1.16 noro 6519: NODE nd,nd1;
6520: int n;
6521: P *pl;
6522: Q *cl;
6523: int i;
6524: P dmy;
6525: Z dvr,inv;
6526: LIST r;
6527:
6528: nd = BDY(l); n = length(nd);
6529: pl = (P *)MALLOC(n*sizeof(P));
6530: cl = (Q *)MALLOC(n*sizeof(Q));
6531: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6532: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6533: }
6534: qltozl(cl,n,&dvr);
6535: divz(ONE,dvr,&inv);
6536: nd = BDY(l);
6537: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6538: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6539: nd = 0;
6540: for ( i = n-1; i >= 0; i-- ) {
6541: MKNODE(nd1,pl[i],nd); nd = nd1;
6542: }
6543: MKLIST(r,nd);
6544: *pp = r;
1.1 noro 6545: }
6546:
6547: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6548:
6549: NDV pltondv(VL vl,VL dvl,LIST p)
6550: {
6551: int i;
6552: NODE t;
6553: ND r,ri;
6554: NM m;
6555:
6556: if ( !nd_module ) error("pltond : module order must be set");
6557: r = 0;
6558: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6559: ri = ptond(vl,dvl,(P)BDY(t));
6560: if ( ri )
6561: for ( m = BDY(ri); m; m = NEXT(m) ) {
6562: MPOS(DL(m)) = i;
6563: TD(DL(m)) = ndl_weight(DL(m));
6564: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6565: }
6566: r = nd_add(0,r,ri);
6567: }
6568: return ndtondv(0,r);
6569: }
6570:
6571: ND ptond(VL vl,VL dvl,P p)
6572: {
6573: int n,i,j,k,e;
6574: VL tvl;
6575: V v;
6576: DCP dc;
6577: DCP *w;
6578: ND r,s,t,u;
6579: P x;
6580: int c;
6581: UINT *d;
6582: NM m,m0;
6583:
6584: if ( !p )
6585: return 0;
6586: else if ( NUM(p) ) {
6587: NEWNM(m);
6588: ndl_zero(DL(m));
6589: if ( !INT((Q)p) )
6590: error("ptond : input must be integer-coefficient");
1.6 noro 6591: CZ(m) = (Z)p;
1.1 noro 6592: NEXT(m) = 0;
6593: MKND(nd_nvar,m,1,r);
6594: SG(r) = 0;
6595: return r;
6596: } else {
6597: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6598: w = (DCP *)MALLOC(k*sizeof(DCP));
6599: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6600: for ( i = 0, tvl = dvl, v = VR(p);
6601: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6602: if ( !tvl ) {
6603: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6604: t = ptond(vl,dvl,COEF(w[j]));
6605: pwrp(vl,x,DEG(w[j]),&p);
6606: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6607: }
6608: return s;
6609: } else {
6610: NEWNM(m0); d = DL(m0);
6611: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6612: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6613: TD(d) = MUL_WEIGHT(e,i);
6614: if ( nd_blockmask) ndl_weight_mask(d);
6615: if ( nd_module ) MPOS(d) = 0;
6616: t = ptond(vl,dvl,COEF(w[j]));
6617: for ( m = BDY(t); m; m = NEXT(m) )
6618: ndl_addto(DL(m),d);
6619: SG(t) += TD(d);
6620: s = nd_add(0,s,t);
6621: }
6622: FREENM(m0);
6623: return s;
6624: }
6625: }
6626: }
6627:
6628: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6629: {
6630: VL tvl;
6631: int len,n,j,i,e;
6632: NMV m;
6633: Z q;
6634: P c;
6635: UINT *d;
6636: P s,r,u,t,w;
6637: GFS gfs;
6638:
6639: if ( !p ) return 0;
6640: else {
6641: len = LEN(p);
6642: n = NV(p);
6643: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6644: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6645: if ( mod == -1 ) {
6646: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6647: } else if ( mod == -2 ) {
6648: c = (P)CZ(m);
6649: } else if ( mod > 0 ) {
1.6 noro 6650: STOZ(CM(m),q); c = (P)q;
1.1 noro 6651: } else
6652: c = CP(m);
6653: d = DL(m);
6654: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6655: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6656: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6657: }
6658: addp(vl,s,t,&u); s = u;
6659: }
6660: return s;
6661: }
6662: }
6663:
6664: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6665: {
6666: VL tvl;
6667: int len,n,j,i,e;
6668: NMV m;
6669: Z q;
6670: P c;
6671: UINT *d;
6672: P s,r,u,t,w;
6673: GFS gfs;
6674: P *a;
6675: LIST l;
6676: NODE nd,nd1;
6677:
6678: if ( !p ) return 0;
6679: else {
6680: a = (P *)MALLOC((rank+1)*sizeof(P));
6681: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6682: len = LEN(p);
6683: n = NV(p);
6684: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6685: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6686: if ( mod == -1 ) {
6687: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6688: } else if ( mod ) {
1.6 noro 6689: STOZ(CM(m),q); c = (P)q;
1.1 noro 6690: } else
6691: c = CP(m);
6692: d = DL(m);
6693: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6694: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6695: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6696: }
6697: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6698: }
6699: nd = 0;
6700: for ( i = rank; i > 0; i-- ) {
6701: MKNODE(nd1,a[i],nd); nd = nd1;
6702: }
6703: MKLIST(l,nd);
6704: return l;
6705: }
6706: }
6707:
6708: NDV ndtondv(int mod,ND p)
6709: {
6710: NDV d;
6711: NMV m,m0;
6712: NM t;
6713: int i,len;
6714:
6715: if ( !p ) return 0;
6716: len = LEN(p);
6717: if ( mod > 0 || mod == -1 )
6718: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6719: else
6720: m0 = m = MALLOC(len*nmv_adv);
6721: #if 0
6722: ndv_alloc += nmv_adv*len;
6723: #endif
6724: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6725: ndl_copy(DL(t),DL(m));
1.6 noro 6726: CZ(m) = CZ(t);
1.1 noro 6727: }
6728: MKNDV(NV(p),m0,len,d);
6729: SG(d) = SG(p);
1.24 noro 6730: d->sig = p->sig;
1.1 noro 6731: return d;
6732: }
6733:
1.16 noro 6734: static int dmm_comp_nv;
6735:
6736: int dmm_comp(DMM *a,DMM *b)
6737: {
6738: return -compdmm(dmm_comp_nv,*a,*b);
6739: }
6740:
6741: void dmm_sort_by_ord(DMM *a,int len,int nv)
6742: {
6743: dmm_comp_nv = nv;
6744: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6745: }
6746:
6747: void dpm_sort(DPM p,DPM *rp)
6748: {
6749: DMM t,t1;
6750: int len,i,n;
6751: DMM *a;
6752: DPM d;
6753:
6754: if ( !p ) *rp = 0;
6755: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6756: a = (DMM *)MALLOC(len*sizeof(DMM));
6757: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6758: n = p->nv;
6759: dmm_sort_by_ord(a,len,n);
6760: t = 0;
6761: for ( i = len-1; i >= 0; i-- ) {
6762: NEWDMM(t1);
6763: t1->c = a[i]->c;
6764: t1->dl = a[i]->dl;
6765: t1->pos = a[i]->pos;
6766: t1->next = t;
6767: t = t1;
6768: }
6769: MKDPM(n,t,d);
6770: SG(d) = SG(p);
6771: *rp = d;
6772: }
6773:
1.18 noro 6774: int dpm_comp(DPM *a,DPM *b)
6775: {
1.22 noro 6776: return -compdpm(CO,*a,*b);
1.18 noro 6777: }
6778:
6779: NODE dpm_sort_list(NODE l)
6780: {
6781: int i,len;
6782: NODE t,t1;
6783: DPM *a;
6784:
6785: len = length(l);
6786: a = (DPM *)MALLOC(len*sizeof(DPM));
6787: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6788: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6789: t = 0;
6790: for ( i = len-1; i >= 0; i-- ) {
6791: MKNODE(t1,(pointer)a[i],t); t = t1;
6792: }
6793: return t;
6794: }
6795:
1.20 noro 6796: int nmv_comp(NMV a,NMV b)
6797: {
1.21 noro 6798: int t;
6799: t = DL_COMPARE(a->dl,b->dl);
6800: return -t;
1.20 noro 6801: }
6802:
1.43 ! noro 6803: NDV dptondv(int mod,DP p)
! 6804: {
! 6805: NDV d;
! 6806: NMV m,m0;
! 6807: MP t;
! 6808: MP *a;
! 6809: int i,len,n;
! 6810:
! 6811: if ( !p ) return 0;
! 6812: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
! 6813: n = p->nv;
! 6814: if ( mod > 0 || mod == -1 )
! 6815: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
! 6816: else
! 6817: m0 = m = MALLOC(len*nmv_adv);
! 6818: for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
! 6819: dltondl(n,t->dl,DL(m));
! 6820: TD(DL(m)) = ndl_weight(DL(m));
! 6821: CZ(m) = (Z)t->c;
! 6822: }
! 6823: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
! 6824: MKNDV(NV(p),m0,len,d);
! 6825: SG(d) = SG(p);
! 6826: return d;
! 6827: }
! 6828:
1.16 noro 6829: NDV dpmtondv(int mod,DPM p)
6830: {
6831: NDV d;
6832: NMV m,m0;
6833: DMM t;
6834: DMM *a;
6835: int i,len,n;
6836:
6837: if ( !p ) return 0;
6838: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6839: a = (DMM *)MALLOC(len*sizeof(DMM));
6840: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6841: n = p->nv;
6842: dmm_sort_by_ord(a,len,n);
6843: if ( mod > 0 || mod == -1 )
6844: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6845: else
6846: m0 = m = MALLOC(len*nmv_adv);
6847: #if 0
6848: ndv_alloc += nmv_adv*len;
6849: #endif
6850: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6851: dltondl(n,a[i]->dl,DL(m));
6852: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6853: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6854: CZ(m) = (Z)a[i]->c;
6855: }
1.20 noro 6856: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6857: MKNDV(NV(p),m0,len,d);
6858: SG(d) = SG(p);
6859: return d;
6860: }
6861:
1.1 noro 6862: ND ndvtond(int mod,NDV p)
6863: {
6864: ND d;
6865: NM m,m0;
6866: NMV t;
6867: int i,len;
6868:
6869: if ( !p ) return 0;
6870: m0 = 0;
6871: len = p->len;
6872: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6873: NEXTNM(m0,m);
6874: ndl_copy(DL(t),DL(m));
1.6 noro 6875: CZ(m) = CZ(t);
1.1 noro 6876: }
6877: NEXT(m) = 0;
6878: MKND(NV(p),m0,len,d);
6879: SG(d) = SG(p);
1.28 noro 6880: d->sig = p->sig;
1.1 noro 6881: return d;
6882: }
6883:
6884: DP ndvtodp(int mod,NDV p)
6885: {
6886: MP m,m0;
6887: DP d;
6888: NMV t;
6889: int i,len;
6890:
6891: if ( !p ) return 0;
6892: m0 = 0;
6893: len = p->len;
6894: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6895: NEXTMP(m0,m);
6896: m->dl = ndltodl(nd_nvar,DL(t));
6897: m->c = (Obj)ndctop(mod,t->c);
6898: }
6899: NEXT(m) = 0;
6900: MKDP(nd_nvar,m0,d);
6901: SG(d) = SG(p);
6902: return d;
6903: }
6904:
1.16 noro 6905: DPM ndvtodpm(int mod,NDV p)
6906: {
6907: DMM m,m0;
6908: DPM d;
6909: NMV t;
6910: int i,len;
6911:
6912: if ( !p ) return 0;
6913: m0 = 0;
6914: len = p->len;
6915: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6916: NEXTDMM(m0,m);
6917: m->dl = ndltodl(nd_nvar,DL(t));
6918: m->c = (Obj)ndctop(mod,t->c);
6919: m->pos = MPOS(DL(t));
6920: }
6921: NEXT(m) = 0;
6922: MKDPM(nd_nvar,m0,d);
6923: SG(d) = SG(p);
6924: return d;
6925: }
6926:
6927:
1.1 noro 6928: DP ndtodp(int mod,ND p)
6929: {
6930: MP m,m0;
6931: DP d;
6932: NM t;
6933: int i,len;
6934:
6935: if ( !p ) return 0;
6936: m0 = 0;
6937: len = p->len;
6938: for ( t = BDY(p); t; t = NEXT(t) ) {
6939: NEXTMP(m0,m);
6940: m->dl = ndltodl(nd_nvar,DL(t));
6941: m->c = (Obj)ndctop(mod,t->c);
6942: }
6943: NEXT(m) = 0;
6944: MKDP(nd_nvar,m0,d);
6945: SG(d) = SG(p);
6946: return d;
6947: }
6948:
6949: void ndv_print(NDV p)
6950: {
6951: NMV m;
6952: int i,len;
6953:
6954: if ( !p ) printf("0\n");
6955: else {
6956: len = LEN(p);
6957: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6958: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 6959: else printf("+%ld*",CM(m));
1.1 noro 6960: ndl_print(DL(m));
6961: }
6962: printf("\n");
6963: }
6964: }
6965:
6966: void ndv_print_q(NDV p)
6967: {
6968: NMV m;
6969: int i,len;
6970:
6971: if ( !p ) printf("0\n");
6972: else {
6973: len = LEN(p);
6974: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6975: printf("+");
1.6 noro 6976: printexpr(CO,(Obj)CZ(m));
1.1 noro 6977: printf("*");
6978: ndl_print(DL(m));
6979: }
6980: printf("\n");
6981: }
6982: }
6983:
6984: NODE ndv_reducebase(NODE x,int *perm)
6985: {
6986: int len,i,j;
6987: NDVI w;
6988: NODE t,t0;
6989:
1.41 noro 6990: if ( nd_norb ) return x;
1.1 noro 6991: len = length(x);
6992: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6993: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6994: w[i].p = BDY(t); w[i].i = perm[i];
6995: }
6996: for ( i = 0; i < len; i++ ) {
6997: for ( j = 0; j < i; j++ ) {
6998: if ( w[i].p && w[j].p ) {
6999: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
7000: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
7001: }
7002: }
7003: }
7004: for ( i = j = 0, t0 = 0; i < len; i++ ) {
7005: if ( w[i].p ) {
7006: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
7007: perm[j++] = w[i].i;
7008: }
7009: }
7010: NEXT(t) = 0; x = t0;
7011: return x;
7012: }
7013:
7014: /* XXX incomplete */
7015:
1.21 noro 7016: extern DMMstack dmm_stack;
7017: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 7018:
1.1 noro 7019: void nd_init_ord(struct order_spec *ord)
7020: {
7021: nd_module = (ord->id >= 256);
7022: if ( nd_module ) {
7023: nd_dcomp = -1;
1.21 noro 7024: nd_module_ordtype = ord->module_ordtype;
1.1 noro 7025: nd_pot_nelim = ord->pot_nelim;
7026: nd_poly_weight_len = ord->nv;
7027: nd_poly_weight = ord->top_weight;
7028: nd_module_rank = ord->module_rank;
7029: nd_module_weight = ord->module_top_weight;
7030: }
7031: nd_matrix = 0;
7032: nd_matrix_len = 0;
7033: switch ( ord->id ) {
7034: case 0:
7035: switch ( ord->ord.simple ) {
7036: case 0:
7037: nd_dcomp = 1;
7038: nd_isrlex = 1;
7039: break;
7040: case 1:
7041: nd_dcomp = 1;
7042: nd_isrlex = 0;
7043: break;
7044: case 2:
7045: nd_dcomp = 0;
7046: nd_isrlex = 0;
7047: ndl_compare_function = ndl_lex_compare;
7048: break;
7049: case 11:
7050: /* XXX */
7051: nd_dcomp = 0;
7052: nd_isrlex = 1;
7053: ndl_compare_function = ndl_ww_lex_compare;
7054: break;
7055: default:
7056: error("nd_gr : unsupported order");
7057: }
7058: break;
7059: case 1:
7060: /* block order */
7061: /* XXX */
7062: nd_dcomp = -1;
7063: nd_isrlex = 0;
7064: ndl_compare_function = ndl_block_compare;
7065: break;
7066: case 2:
7067: /* matrix order */
7068: /* XXX */
7069: nd_dcomp = -1;
7070: nd_isrlex = 0;
7071: nd_matrix_len = ord->ord.matrix.row;
7072: nd_matrix = ord->ord.matrix.matrix;
7073: ndl_compare_function = ndl_matrix_compare;
7074: break;
7075: case 3:
7076: /* composite order */
7077: nd_dcomp = -1;
7078: nd_isrlex = 0;
7079: nd_worb_len = ord->ord.composite.length;
7080: nd_worb = ord->ord.composite.w_or_b;
7081: ndl_compare_function = ndl_composite_compare;
7082: break;
7083:
7084: /* module order */
7085: case 256:
7086: switch ( ord->ord.simple ) {
7087: case 0:
1.21 noro 7088: nd_dcomp = 0;
1.1 noro 7089: nd_isrlex = 1;
1.21 noro 7090: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7091: break;
7092: case 1:
1.21 noro 7093: nd_dcomp = 0;
1.1 noro 7094: nd_isrlex = 0;
7095: ndl_compare_function = ndl_module_glex_compare;
7096: break;
7097: case 2:
1.21 noro 7098: nd_dcomp = 0;
1.1 noro 7099: nd_isrlex = 0;
1.21 noro 7100: ndl_compare_function = ndl_module_compare;
7101: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7102: break;
7103: default:
1.21 noro 7104: error("nd_init_ord : unsupported order");
1.1 noro 7105: }
7106: break;
7107: case 257:
7108: /* block order */
7109: nd_isrlex = 0;
1.21 noro 7110: ndl_compare_function = ndl_module_compare;
7111: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7112: break;
7113: case 258:
7114: /* matrix order */
7115: nd_isrlex = 0;
7116: nd_matrix_len = ord->ord.matrix.row;
7117: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7118: ndl_compare_function = ndl_module_compare;
7119: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7120: break;
7121: case 259:
7122: /* composite order */
7123: nd_isrlex = 0;
7124: nd_worb_len = ord->ord.composite.length;
7125: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7126: ndl_compare_function = ndl_module_compare;
7127: ndl_base_compare_function = ndl_composite_compare;
7128: break;
7129: case 300:
7130: /* schreyer order */
7131: if ( ord->base->id != 256 )
7132: error("nd_init_ord : unsupported base order");
7133: ndl_compare_function = ndl_module_schreyer_compare;
7134: dmm_stack = ord->dmmstack;
7135: switch ( ord->base->ord.simple ) {
7136: case 0:
7137: nd_isrlex = 1;
7138: ndl_base_compare_function = ndl_glex_compare;
7139: dl_base_compare_function = cmpdl_revgradlex;
7140: break;
7141: case 1:
7142: nd_isrlex = 0;
7143: ndl_base_compare_function = ndl_glex_compare;
7144: dl_base_compare_function = cmpdl_gradlex;
7145: break;
7146: case 2:
7147: nd_isrlex = 0;
7148: ndl_base_compare_function = ndl_lex_compare;
7149: dl_base_compare_function = cmpdl_lex;
7150: break;
7151: default:
7152: error("nd_init_ord : unsupported order");
7153: }
1.1 noro 7154: break;
7155: }
7156: nd_ord = ord;
7157: }
7158:
7159: BlockMask nd_create_blockmask(struct order_spec *ord)
7160: {
7161: int n,i,j,s,l;
7162: UINT *t;
7163: BlockMask bm;
7164:
7165: /* we only create mask table for block order */
7166: if ( ord->id != 1 && ord->id != 257 )
7167: return 0;
7168: n = ord->ord.block.length;
7169: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7170: bm->n = n;
7171: bm->order_pair = ord->ord.block.order_pair;
7172: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7173: for ( i = 0, s = 0; i < n; i++ ) {
7174: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7175: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7176: l = bm->order_pair[i].length;
7177: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7178: }
7179: return bm;
7180: }
7181:
7182: EPOS nd_create_epos(struct order_spec *ord)
7183: {
7184: int i,j,l,s,ord_l,ord_o;
7185: EPOS epos;
7186: struct order_pair *op;
7187:
7188: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7189: switch ( ord->id ) {
1.21 noro 7190: case 0: case 256: case 300:
1.1 noro 7191: if ( nd_isrlex ) {
7192: for ( i = 0; i < nd_nvar; i++ ) {
7193: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7194: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7195: }
7196: } else {
7197: for ( i = 0; i < nd_nvar; i++ ) {
7198: epos[i].i = nd_exporigin + i/nd_epw;
7199: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7200: }
7201: }
7202: break;
7203: case 1: case 257:
7204: /* block order */
7205: l = ord->ord.block.length;
7206: op = ord->ord.block.order_pair;
7207: for ( j = 0, s = 0; j < l; j++ ) {
7208: ord_o = op[j].order;
7209: ord_l = op[j].length;
7210: if ( !ord_o )
7211: for ( i = 0; i < ord_l; i++ ) {
7212: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7213: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7214: }
7215: else
7216: for ( i = 0; i < ord_l; i++ ) {
7217: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7218: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7219: }
7220: s += ord_l;
7221: }
7222: break;
7223: case 2:
7224: /* matrix order */
7225: case 3:
7226: /* composite order */
7227: default:
7228: for ( i = 0; i < nd_nvar; i++ ) {
7229: epos[i].i = nd_exporigin + i/nd_epw;
7230: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7231: }
7232: break;
7233: }
7234: return epos;
7235: }
7236:
7237: /* external interface */
7238:
7239: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7240: {
7241: NODE t,in0,in;
7242: ND ndf,nf;
7243: NDV ndvf;
7244: VL vv,tv;
7245: int stat,nvar,max,mrank;
7246: union oNDC dn;
7247: Q cont;
7248: P pp;
7249: LIST ppl;
7250:
7251: if ( !f ) {
7252: *rp = 0;
7253: return;
7254: }
7255: pltovl(v,&vv);
7256: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7257:
7258: /* max=65536 implies nd_bpe=32 */
7259: max = 65536;
7260:
7261: nd_module = 0;
7262: /* nd_module will be set if ord is a module ordering */
7263: nd_init_ord(ord);
7264: nd_setup_parameters(nvar,max);
7265: if ( nd_module && OID(f) != O_LIST )
7266: error("nd_nf_p : the first argument must be a list");
7267: if ( nd_module ) mrank = length(BDY((LIST)f));
7268: /* conversion to ndv */
7269: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7270: NEXTNODE(in0,in);
7271: if ( nd_module ) {
7272: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7273: || length(BDY((LIST)BDY(t))) != mrank )
7274: error("nd_nf_p : inconsistent basis element");
7275: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7276: else ppl = (LIST)BDY(t);
7277: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7278: } else {
7279: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7280: else pp = (P)BDY(t);
7281: BDY(in) = (pointer)ptondv(CO,vv,pp);
7282: }
7283: if ( m ) ndv_mod(m,(NDV)BDY(in));
7284: }
7285: if ( in0 ) NEXT(in) = 0;
7286:
7287: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7288: else ndvf = ptondv(CO,vv,(P)f);
7289: if ( m ) ndv_mod(m,ndvf);
7290: ndf = (pointer)ndvtond(m,ndvf);
7291:
7292: /* dont sort, dont removecont */
1.24 noro 7293: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7294: nd_scale=2;
1.6 noro 7295: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7296: if ( !stat )
7297: error("nd_nf_p : exponent too large");
7298: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7299: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7300: }
7301:
7302: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7303: {
7304: NM m;
7305: UINT *t,*s;
7306: int i;
7307:
7308: for ( i = 0; i < n; i++ ) r[i] = 0;
7309: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7310: t = DL(m);
7311: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7312: r[i] = CM(m);
7313: }
7314: for ( i = 0; !r[i]; i++ );
7315: return i;
7316: }
7317:
7318: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7319: {
7320: NM m;
7321: UINT *t,*s;
7322: int i;
7323:
7324: for ( i = 0; i < n; i++ ) r[i] = 0;
7325: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7326: t = DL(m);
7327: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7328: r[i] = CZ(m);
1.1 noro 7329: }
7330: for ( i = 0; !r[i]; i++ );
7331: return i;
7332: }
7333:
7334: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7335: {
7336: NM m;
7337: UINT *t,*s;
7338: int i;
7339:
7340: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7341: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7342: t = DL(m);
7343: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7344: mpz_set(r[i],BDY(CZ(m)));
7345: }
7346: for ( i = 0; !mpz_sgn(r[i]); i++ );
7347: return i;
7348: }
7349:
7350: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7351: {
7352: NM m;
7353: unsigned long *v;
7354: int i,j,h,size;
7355: UINT *s,*t;
7356:
7357: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7358: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7359: bzero(v,size);
7360: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7361: t = DL(m);
7362: h = ndl_hash_value(t);
7363: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7364: v[i/BLEN] |= 1L <<(i%BLEN);
7365: }
7366: return v;
7367: }
7368:
7369: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7370: {
7371: NMV mr;
7372: UINT *d,*t,*s;
7373: int i,j,len,h,head;
7374:
7375: d = DL(m);
7376: len = LEN(p);
7377: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7378: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7379: ndl_add(d,DL(mr),t);
7380: h = ndl_hash_value(t);
7381: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7382: if ( j == 0 ) head = i;
7383: v[i/BLEN] |= 1L <<(i%BLEN);
7384: }
7385: return head;
7386: }
7387:
7388: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7389: {
7390: NM m;
7391: NMV mr;
7392: UINT *d,*t,*s;
7393: NDV p;
7394: int i,j,len;
7395: Z *r;
7396:
7397: m = pair->mul;
7398: d = DL(m);
7399: p = nd_ps[pair->index];
7400: len = LEN(p);
7401: r = (Z *)CALLOC(n,sizeof(Q));
7402: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7403: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7404: ndl_add(d,DL(mr),t);
7405: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7406: r[i] = CZ(mr);
1.1 noro 7407: }
7408: return r;
7409: }
7410:
1.11 noro 7411: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7412: {
7413: NM m;
7414: NMV mr;
1.11 noro 7415: UINT *d,*t,*s,*u;
1.1 noro 7416: NDV p;
7417: unsigned char *ivc;
7418: unsigned short *ivs;
7419: UINT *v,*ivi,*s0v;
1.11 noro 7420: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7421: IndArray r;
7422:
7423: m = pair->mul;
7424: d = DL(m);
7425: if ( trace )
7426: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7427: else
7428: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7429:
7430: len = LEN(p);
7431: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7432: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7433: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7434: ndl_add(d,DL(mr),t);
7435: st = prev;
7436: ed = n;
7437: while ( ed > st ) {
7438: md = (st+ed)/2;
7439: u = s0+md*nd_wpd;
7440: c = DL_COMPARE(u,t);
7441: if ( c == 0 ) break;
7442: else if ( c > 0 ) st = md;
7443: else ed = md;
7444: }
7445: prev = v[j] = md;
1.1 noro 7446: }
7447: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7448: r->head = v[0];
7449: diff = 0;
7450: for ( i = 1; i < len; i++ ) {
7451: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7452: }
7453: if ( diff < 256 ) {
7454: r->width = 1;
7455: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7456: r->index.c = ivc;
7457: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7458: } else if ( diff < 65536 ) {
7459: r->width = 2;
7460: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7461: r->index.s = ivs;
7462: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7463: } else {
7464: r->width = 4;
7465: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7466: r->index.i = ivi;
7467: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7468: }
7469: return r;
7470: }
7471:
7472: int compress_array(Z *svect,Z *cvect,int n)
7473: {
7474: int i,j;
7475:
7476: for ( i = j = 0; i < n; i++ )
7477: if ( svect[i] ) cvect[j++] = svect[i];
7478: return j;
7479: }
7480:
7481: void expand_array(Z *svect,Z *cvect,int n)
7482: {
7483: int i,j;
7484:
7485: for ( i = j = 0; j < n; i++ )
7486: if ( svect[i] ) svect[i] = cvect[j++];
7487: }
7488:
1.8 noro 7489: #if 0
1.1 noro 7490: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7491: {
7492: int i,j,k,len,pos,prev,nz;
7493: Z cs,mcs,c1,c2,cr,gcd,t;
7494: IndArray ivect;
7495: unsigned char *ivc;
7496: unsigned short *ivs;
7497: unsigned int *ivi;
7498: NDV redv;
7499: NMV mr;
7500: NODE rp;
7501: int maxrs;
7502: double hmag;
7503: Z *cvect;
1.3 noro 7504: int l;
1.1 noro 7505:
7506: maxrs = 0;
7507: for ( i = 0; i < col && !svect[i]; i++ );
7508: if ( i == col ) return maxrs;
7509: hmag = p_mag((P)svect[i])*nd_scale;
7510: cvect = (Z *)MALLOC(col*sizeof(Q));
7511: for ( i = 0; i < nred; i++ ) {
7512: ivect = imat[i];
7513: k = ivect->head;
7514: if ( svect[k] ) {
7515: maxrs = MAX(maxrs,rp0[i]->sugar);
7516: redv = nd_demand?ndv_load(rp0[i]->index)
7517: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7518: len = LEN(redv); mr = BDY(redv);
1.6 noro 7519: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7520: chsgnz(cs,&mcs);
7521: if ( !UNIQ(cr) ) {
7522: for ( j = 0; j < col; j++ ) {
7523: mulz(svect[j],cr,&c1); svect[j] = c1;
7524: }
7525: }
7526: svect[k] = 0; prev = k;
7527: switch ( ivect->width ) {
7528: case 1:
7529: ivc = ivect->index.c;
7530: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7531: pos = prev+ivc[j]; prev = pos;
1.6 noro 7532: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7533: }
7534: break;
7535: case 2:
7536: ivs = ivect->index.s;
7537: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7538: pos = prev+ivs[j]; prev = pos;
1.6 noro 7539: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7540: }
7541: break;
7542: case 4:
7543: ivi = ivect->index.i;
7544: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7545: pos = prev+ivi[j]; prev = pos;
1.6 noro 7546: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7547: }
7548: break;
7549: }
7550: for ( j = k+1; j < col && !svect[j]; j++ );
7551: if ( j == col ) break;
7552: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7553: nz = compress_array(svect,cvect,col);
7554: removecont_array((P *)cvect,nz,1);
7555: expand_array(svect,cvect,nz);
7556: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7557: }
7558: }
7559: }
7560: nz = compress_array(svect,cvect,col);
7561: removecont_array((P *)cvect,nz,1);
7562: expand_array(svect,cvect,nz);
7563: if ( DP_Print ) {
7564: fprintf(asir_out,"-"); fflush(asir_out);
7565: }
7566: return maxrs;
7567: }
1.4 noro 7568: #else
1.9 noro 7569:
1.4 noro 7570: /* direct mpz version */
7571: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7572: {
7573: int i,j,k,len,pos,prev;
7574: mpz_t cs,cr,gcd;
7575: IndArray ivect;
7576: unsigned char *ivc;
7577: unsigned short *ivs;
7578: unsigned int *ivi;
7579: NDV redv;
7580: NMV mr;
7581: NODE rp;
7582: int maxrs;
7583: double hmag;
7584: int l;
1.13 noro 7585: static mpz_t *svect;
7586: static int svect_len=0;
1.4 noro 7587:
7588: maxrs = 0;
7589: for ( i = 0; i < col && !svect0[i]; i++ );
7590: if ( i == col ) return maxrs;
7591: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7592: if ( col > svect_len ) {
7593: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7594: svect_len = col;
7595: }
1.4 noro 7596: for ( i = 0; i < col; i++ ) {
7597: mpz_init(svect[i]);
7598: if ( svect0[i] )
7599: mpz_set(svect[i],BDY(svect0[i]));
7600: else
7601: mpz_set_ui(svect[i],0);
7602: }
7603: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7604: for ( i = 0; i < nred; i++ ) {
7605: ivect = imat[i];
7606: k = ivect->head;
7607: if ( mpz_sgn(svect[k]) ) {
7608: maxrs = MAX(maxrs,rp0[i]->sugar);
7609: redv = nd_demand?ndv_load(rp0[i]->index)
7610: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7611: len = LEN(redv); mr = BDY(redv);
1.6 noro 7612: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7613: mpz_div(cs,svect[k],gcd);
1.6 noro 7614: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7615: mpz_neg(cs,cs);
1.9 noro 7616: if ( MUNIMPZ(cr) )
7617: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7618: else if ( !UNIMPZ(cr) )
7619: for ( j = 0; j < col; j++ ) {
7620: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7621: }
1.4 noro 7622: mpz_set_ui(svect[k],0);
7623: prev = k;
7624: switch ( ivect->width ) {
7625: case 1:
7626: ivc = ivect->index.c;
7627: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7628: pos = prev+ivc[j]; prev = pos;
1.6 noro 7629: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7630: }
7631: break;
7632: case 2:
7633: ivs = ivect->index.s;
7634: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7635: pos = prev+ivs[j]; prev = pos;
1.6 noro 7636: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7637: }
7638: break;
7639: case 4:
7640: ivi = ivect->index.i;
7641: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7642: pos = prev+ivi[j]; prev = pos;
1.6 noro 7643: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7644: }
7645: break;
7646: }
7647: for ( j = k+1; j < col && !svect[j]; j++ );
7648: if ( j == col ) break;
7649: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7650: mpz_removecont_array(svect,col);
7651: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7652: }
7653: }
7654: }
7655: mpz_removecont_array(svect,col);
7656: if ( DP_Print ) {
7657: fprintf(asir_out,"-"); fflush(asir_out);
7658: }
7659: for ( i = 0; i < col; i++ )
7660: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7661: else svect0[i] = 0;
7662: return maxrs;
7663: }
7664: #endif
1.1 noro 7665:
1.40 noro 7666: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 7667: {
7668: int i,j,k,len,pos,prev;
7669: UINT c,c1,c2,c3,up,lo,dmy;
7670: IndArray ivect;
7671: unsigned char *ivc;
7672: unsigned short *ivs;
7673: unsigned int *ivi;
7674: NDV redv;
7675: NMV mr;
7676: NODE rp;
7677: int maxrs;
7678:
7679: maxrs = 0;
7680: for ( i = 0; i < nred; i++ ) {
7681: ivect = imat[i];
7682: k = ivect->head; svect[k] %= m;
1.40 noro 7683: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 7684: maxrs = MAX(maxrs,rp0[i]->sugar);
7685: c = m-c; redv = nd_ps[rp0[i]->index];
7686: len = LEN(redv); mr = BDY(redv);
7687: svect[k] = 0; prev = k;
7688: switch ( ivect->width ) {
7689: case 1:
7690: ivc = ivect->index.c;
7691: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7692: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 7693: if ( c1 ) {
7694: c2 = svect[pos];
1.1 noro 7695: DMA(c1,c,c2,up,lo);
7696: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7697: } else svect[pos] = lo;
1.40 noro 7698: }
1.1 noro 7699: }
7700: break;
7701: case 2:
7702: ivs = ivect->index.s;
7703: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7704: pos = prev+ivs[j]; c1 = CM(mr);
7705: prev = pos;
1.40 noro 7706: if ( c1 ) {
7707: c2 = svect[pos];
1.1 noro 7708: DMA(c1,c,c2,up,lo);
7709: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7710: } else svect[pos] = lo;
1.40 noro 7711: }
1.1 noro 7712: }
7713: break;
7714: case 4:
7715: ivi = ivect->index.i;
7716: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7717: pos = prev+ivi[j]; c1 = CM(mr);
7718: prev = pos;
1.40 noro 7719: if ( c1 ) {
7720: c2 = svect[pos];
1.1 noro 7721: DMA(c1,c,c2,up,lo);
7722: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7723: } else svect[pos] = lo;
1.40 noro 7724: }
1.1 noro 7725: }
7726: break;
7727: }
7728: }
7729: }
7730: for ( i = 0; i < col; i++ )
7731: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7732: return maxrs;
7733: }
7734:
7735: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7736: {
7737: int i,j,k,len,pos,prev;
7738: UINT c,c1,c2,c3,up,lo,dmy;
7739: IndArray ivect;
7740: unsigned char *ivc;
7741: unsigned short *ivs;
7742: unsigned int *ivi;
7743: NDV redv;
7744: NMV mr;
7745: NODE rp;
7746: int maxrs;
7747:
7748: maxrs = 0;
7749: for ( i = 0; i < nred; i++ ) {
7750: ivect = imat[i];
7751: k = ivect->head;
7752: if ( (c = svect[k]) != 0 ) {
7753: maxrs = MAX(maxrs,rp0[i]->sugar);
7754: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7755: len = LEN(redv); mr = BDY(redv);
7756: svect[k] = 0; prev = k;
7757: switch ( ivect->width ) {
7758: case 1:
7759: ivc = ivect->index.c;
7760: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7761: pos = prev+ivc[j]; prev = pos;
7762: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7763: }
7764: break;
7765: case 2:
7766: ivs = ivect->index.s;
7767: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7768: pos = prev+ivs[j]; prev = pos;
7769: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7770: }
7771: break;
7772: case 4:
7773: ivi = ivect->index.i;
7774: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7775: pos = prev+ivi[j]; prev = pos;
7776: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7777: }
7778: break;
7779: }
7780: }
7781: }
7782: return maxrs;
7783: }
7784:
7785: ND nd_add_lf(ND p1,ND p2)
7786: {
7787: int n,c,can;
7788: ND r;
7789: NM m1,m2,mr0,mr,s;
7790: Z t;
7791:
7792: if ( !p1 ) return p2;
7793: else if ( !p2 ) return p1;
7794: else {
7795: can = 0;
7796: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7797: c = DL_COMPARE(DL(m1),DL(m2));
7798: switch ( c ) {
7799: case 0:
7800: addlf(CZ(m1),CZ(m2),&t);
7801: s = m1; m1 = NEXT(m1);
7802: if ( t ) {
7803: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7804: } else {
7805: can += 2; FREENM(s);
7806: }
7807: s = m2; m2 = NEXT(m2); FREENM(s);
7808: break;
7809: case 1:
7810: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7811: break;
7812: case -1:
7813: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7814: break;
7815: }
7816: }
7817: if ( !mr0 )
7818: if ( m1 ) mr0 = m1;
7819: else if ( m2 ) mr0 = m2;
7820: else return 0;
7821: else if ( m1 ) NEXT(mr) = m1;
7822: else if ( m2 ) NEXT(mr) = m2;
7823: else NEXT(mr) = 0;
7824: BDY(p1) = mr0;
7825: SG(p1) = MAX(SG(p1),SG(p2));
7826: LEN(p1) = LEN(p1)+LEN(p2)-can;
7827: FREEND(p2);
7828: return p1;
7829: }
7830: }
7831:
7832: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7833: {
7834: int i,j,k,len,pos,prev;
7835: mpz_t c,mc,c1;
7836: IndArray ivect;
7837: unsigned char *ivc;
7838: unsigned short *ivs;
7839: unsigned int *ivi;
7840: NDV redv;
7841: NMV mr;
7842: NODE rp;
7843: int maxrs;
7844:
7845: maxrs = 0;
7846: lf_lazy = 1;
7847: for ( i = 0; i < nred; i++ ) {
7848: ivect = imat[i];
7849: k = ivect->head;
7850: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7851: if ( mpz_sgn(svect[k]) ) {
7852: maxrs = MAX(maxrs,rp0[i]->sugar);
7853: mpz_neg(svect[k],svect[k]);
7854: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7855: len = LEN(redv); mr = BDY(redv);
7856: prev = k;
7857: switch ( ivect->width ) {
7858: case 1:
7859: ivc = ivect->index.c;
7860: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7861: pos = prev+ivc[j]; prev = pos;
7862: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7863: }
7864: break;
7865: case 2:
7866: ivs = ivect->index.s;
7867: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7868: pos = prev+ivs[j]; prev = pos;
7869: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7870: }
7871: break;
7872: case 4:
7873: ivi = ivect->index.i;
7874: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7875: pos = prev+ivi[j]; prev = pos;
7876: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7877: }
7878: break;
7879: }
7880: mpz_set_ui(svect[k],0);
7881: }
7882: }
7883: lf_lazy=0;
7884: for ( i = 0; i < col; i++ ) {
7885: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7886: }
7887: return maxrs;
7888: }
7889:
7890: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7891: {
7892: int i,j,k,l,rank,s;
7893: mpz_t a,a1,inv;
7894: mpz_t *t,*pivot,*pk;
7895: mpz_t **mat;
7896: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7897: int size,size1;
7898:
7899: mpz_init(inv);
7900: mpz_init(a);
7901: mat = (mpz_t **)mat0;
7902: size = 0;
7903: for ( rank = 0, j = 0; j < col; j++ ) {
7904: for ( i = rank; i < row; i++ ) {
7905: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7906: }
7907: for ( i = rank; i < row; i++ )
7908: if ( mpz_sgn(mat[i][j]) )
7909: break;
7910: if ( i == row ) {
7911: colstat[j] = 0;
7912: continue;
7913: } else
7914: colstat[j] = 1;
7915: if ( i != rank ) {
7916: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7917: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7918: }
7919: pivot = mat[rank];
7920: s = sugar[rank];
7921: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7922: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7923: if ( mpz_sgn(*pk) ) {
7924: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7925: }
7926: for ( i = rank+1; i < row; i++ ) {
7927: t = mat[i];
7928: if ( mpz_sgn(t[j]) ) {
7929: sugar[i] = MAX(sugar[i],s);
7930: mpz_neg(a,t[j]);
7931: red_by_vect_lf(t+j,pivot+j,a,col-j);
7932: }
7933: }
7934: rank++;
7935: }
7936: for ( j = col-1, l = rank-1; j >= 0; j-- )
7937: if ( colstat[j] ) {
7938: pivot = mat[l];
7939: s = sugar[l];
7940: for ( k = j; k < col; k++ )
7941: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7942: for ( i = 0; i < l; i++ ) {
7943: t = mat[i];
7944: if ( mpz_sgn(t[j]) ) {
7945: sugar[i] = MAX(sugar[i],s);
7946: mpz_neg(a,t[j]);
7947: red_by_vect_lf(t+j,pivot+j,a,col-j);
7948: }
7949: }
7950: l--;
7951: }
7952: for ( j = 0, l = 0; l < rank; j++ )
7953: if ( colstat[j] ) {
7954: t = mat[l];
7955: for ( k = j; k < col; k++ ) {
7956: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7957: }
7958: l++;
7959: }
7960: return rank;
7961: }
7962:
7963:
7964: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7965: {
7966: int j,k,len;
7967: UINT *p;
7968: UINT c;
7969: NDV r;
7970: NMV mr0,mr;
7971:
7972: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7973: if ( !len ) return 0;
7974: else {
7975: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7976: #if 0
7977: ndv_alloc += nmv_adv*len;
7978: #endif
7979: mr = mr0;
7980: p = s0vect;
7981: for ( j = k = 0; j < col; j++, p += nd_wpd )
7982: if ( !rhead[j] ) {
7983: if ( (c = vect[k++]) != 0 ) {
7984: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7985: }
7986: }
7987: MKNDV(nd_nvar,mr0,len,r);
7988: return r;
7989: }
7990: }
7991:
1.40 noro 7992: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
7993: {
7994: int j,k,len;
7995: UINT *p;
7996: UINT c;
7997: NDV r;
7998: NMV mr0,mr;
7999:
8000: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8001: if ( !len ) return 0;
8002: else {
8003: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8004: mr = mr0;
8005: p = s0vect;
8006: for ( j = k = 0; j < col; j++, p += nd_wpd )
8007: if ( (c = vect[k++]) != 0 ) {
8008: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8009: }
8010: MKNDV(nd_nvar,mr0,len,r);
8011: return r;
8012: }
8013: }
8014:
1.1 noro 8015: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
8016: {
8017: int j,k,len;
8018: UINT *p;
8019: NDV r;
8020: NMV mr0,mr;
8021:
8022: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
8023: if ( !len ) return 0;
8024: else {
8025: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8026: mr = mr0;
8027: p = s0vect;
8028: for ( j = 0; j < col; j++, p += nd_wpd )
8029: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
8030: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
8031: }
8032: MKNDV(nd_nvar,mr0,len,r);
8033: return r;
8034: }
8035: }
8036:
8037: /* for preprocessed vector */
8038:
8039: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
8040: {
1.6 noro 8041: int j,k,len;
8042: UINT *p;
8043: Z c;
8044: NDV r;
8045: NMV mr0,mr;
1.1 noro 8046:
1.6 noro 8047: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8048: if ( !len ) return 0;
8049: else {
8050: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 8051: #if 0
1.6 noro 8052: ndv_alloc += nmv_adv*len;
1.1 noro 8053: #endif
1.6 noro 8054: mr = mr0;
8055: p = s0vect;
8056: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
8057: if ( !rhead[j] ) {
8058: if ( (c = vect[k++]) != 0 ) {
8059: if ( !INT(c) )
8060: error("vect_to_ndv_q : components must be integers");
8061: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
8062: }
8063: }
1.1 noro 8064: }
1.6 noro 8065: MKNDV(nd_nvar,mr0,len,r);
8066: return r;
8067: }
1.1 noro 8068: }
8069:
8070: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8071: {
8072: int j,k,len;
8073: UINT *p;
8074: mpz_t c;
8075: NDV r;
8076: NMV mr0,mr;
8077:
8078: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8079: if ( !len ) return 0;
8080: else {
8081: mr0 = (NMV)MALLOC(nmv_adv*len);
8082: #if 0
8083: ndv_alloc += nmv_adv*len;
8084: #endif
8085: mr = mr0;
8086: p = s0vect;
8087: for ( j = k = 0; j < col; j++, p += nd_wpd )
8088: if ( !rhead[j] ) {
8089: c[0] = vect[k++][0];
8090: if ( mpz_sgn(c) ) {
8091: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8092: }
8093: }
8094: MKNDV(nd_nvar,mr0,len,r);
8095: return r;
8096: }
8097: }
8098:
8099: /* for plain vector */
8100:
8101: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8102: {
8103: int j,k,len;
8104: UINT *p;
8105: Z c;
8106: NDV r;
8107: NMV mr0,mr;
8108:
8109: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8110: if ( !len ) return 0;
8111: else {
8112: mr0 = (NMV)MALLOC(nmv_adv*len);
8113: #if 0
8114: ndv_alloc += nmv_adv*len;
8115: #endif
8116: mr = mr0;
8117: p = s0vect;
8118: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8119: if ( (c = vect[k]) != 0 ) {
8120: if ( !INT(c) )
1.6 noro 8121: error("plain_vect_to_ndv_q : components must be integers");
8122: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8123: }
8124: MKNDV(nd_nvar,mr0,len,r);
8125: return r;
8126: }
8127: }
8128:
8129: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8130: {
8131: ND_pairs t;
8132: NODE sp0,sp;
8133: int stat;
8134: ND spol;
8135:
8136: for ( t = l; t; t = NEXT(t) ) {
8137: stat = nd_sp(m,trace,t,&spol);
8138: if ( !stat ) return 0;
8139: if ( spol ) {
8140: add_pbucket_symbolic(bucket,spol);
8141: }
8142: }
8143: return 1;
8144: }
8145:
8146: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8147: {
8148: NODE rp0,rp;
8149: NM mul,head,s0,s;
8150: int index,col,i,sugar;
8151: RHist h;
8152: UINT *s0v,*p;
8153: NM_ind_pair pair;
8154: ND red;
8155: NDV *ps;
8156:
8157: s0 = 0; rp0 = 0; col = 0;
8158: if ( nd_demand )
8159: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8160: else
8161: ps = trace?nd_ps_trace:nd_ps;
8162: while ( 1 ) {
8163: head = remove_head_pbucket_symbolic(bucket);
8164: if ( !head ) break;
8165: if ( !s0 ) s0 = head;
8166: else NEXT(s) = head;
8167: s = head;
8168: index = ndl_find_reducer(DL(head));
8169: if ( index >= 0 ) {
8170: h = nd_psh[index];
8171: NEWNM(mul);
8172: ndl_sub(DL(head),DL(h),DL(mul));
8173: if ( ndl_check_bound2(index,DL(mul)) )
8174: return 0;
8175: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8176: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8177: red = ndv_mul_nm_symbolic(mul,ps[index]);
8178: add_pbucket_symbolic(bucket,nd_remove_head(red));
8179: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8180: }
8181: col++;
8182: }
8183: if ( rp0 ) NEXT(rp) = 0;
8184: NEXT(s) = 0;
8185: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8186: for ( i = 0, p = s0v, s = s0; i < col;
8187: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8188: *s0vect = s0v;
8189: *r = rp0;
8190: return col;
8191: }
8192:
8193: void print_ndp(ND_pairs l)
8194: {
8195: ND_pairs t;
8196:
8197: for ( t = l; t; t = NEXT(t) )
8198: printf("[%d,%d] ",t->i1,t->i2);
8199: printf("\n");
8200: }
8201:
8202: NODE nd_f4(int m,int checkonly,int **indp)
8203: {
8204: int i,nh,stat,index,f4red;
8205: NODE r,g,tn0,tn,node;
8206: ND_pairs d,l,t,ll0,ll,lh;
8207: LIST l0,l1;
8208: ND spol,red;
8209: NDV nf,redv;
8210: NM s0,s;
8211: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8212: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8213: UINT c;
8214: UINT **spmat;
8215: UINT *s0vect,*svect,*p,*v;
8216: int *colstat;
8217: IndArray *imat;
8218: int *rhead;
8219: int spcol,sprow;
8220: int sugar,sugarh;
8221: PGeoBucket bucket;
8222: struct oEGT eg0,eg1,eg_f4;
8223: Z i1,i2,sugarq;
1.12 noro 8224:
8225: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8226: #if 0
8227: ndv_alloc = 0;
8228: #endif
1.11 noro 8229: Nf4_red=0;
1.1 noro 8230: g = 0; d = 0;
8231: for ( i = 0; i < nd_psn; i++ ) {
8232: d = update_pairs(d,g,i,0);
8233: g = update_base(g,i);
8234: }
8235: nzlist = 0;
8236: nzlist_t = nd_nzlist;
8237: f4red = 1;
8238: nd_last_nonzero = 0;
8239: while ( d ) {
8240: get_eg(&eg0);
8241: l = nd_minsugarp(d,&d);
8242: sugar = nd_sugarweight?l->sugar2:SG(l);
8243: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8244: if ( nzlist_t ) {
8245: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8246: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8247: tn = BDY((LIST)ARG1(node));
8248: if ( !tn ) {
8249: nzlist_t = NEXT(nzlist_t);
8250: continue;
8251: }
8252: /* tn = [[i1,i2],...] */
8253: lh = nd_ipairtospair(tn);
8254: }
8255: bucket = create_pbucket();
8256: stat = nd_sp_f4(m,0,l,bucket);
8257: if ( !stat ) {
8258: for ( t = l; NEXT(t); t = NEXT(t) );
8259: NEXT(t) = d; d = l;
8260: d = nd_reconstruct(0,d);
8261: continue;
8262: }
8263: if ( bucket->m < 0 ) continue;
8264: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8265: if ( !col ) {
8266: for ( t = l; NEXT(t); t = NEXT(t) );
8267: NEXT(t) = d; d = l;
8268: d = nd_reconstruct(0,d);
8269: continue;
8270: }
1.12 noro 8271: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8272: if ( DP_Print )
1.6 noro 8273: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8274: sugar,eg_f4.exectime);
1.1 noro 8275: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8276: if ( checkonly && nflist ) return 0;
8277: /* adding new bases */
8278: if ( nflist ) nd_last_nonzero = f4red;
8279: for ( r = nflist; r; r = NEXT(r) ) {
8280: nf = (NDV)BDY(r);
1.38 noro 8281: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8282: ndv_removecont(m,nf);
8283: if ( !m && nd_nalg ) {
8284: ND nf1;
8285:
8286: nf1 = ndvtond(m,nf);
8287: nd_monic(0,&nf1);
8288: nd_removecont(m,nf1);
8289: nf = ndtondv(m,nf1);
8290: }
1.24 noro 8291: nh = ndv_newps(m,nf,0);
1.1 noro 8292: d = update_pairs(d,g,nh,0);
8293: g = update_base(g,nh);
8294: }
8295: if ( DP_Print ) {
8296: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8297: }
8298: if ( nd_gentrace ) {
8299: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8300: NEXTNODE(tn0,tn);
1.6 noro 8301: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8302: node = mknode(2,i1,i2); MKLIST(l0,node);
8303: BDY(tn) = l0;
8304: }
8305: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8306: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8307: MKNODE(node,l1,nzlist); nzlist = node;
8308: }
8309: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8310: f4red++;
8311: if ( nd_f4red && f4red > nd_f4red ) break;
8312: if ( nd_rank0 && !nflist ) break;
8313: }
8314: if ( nd_gentrace ) {
8315: MKLIST(l0,reverse_node(nzlist));
8316: MKNODE(nd_alltracelist,l0,0);
8317: }
8318: #if 0
8319: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8320: #endif
1.12 noro 8321: if ( DP_Print ) {
8322: fprintf(asir_out,"number of red=%d,",Nf4_red);
8323: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8324: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8325: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8326: }
1.1 noro 8327: conv_ilist(nd_demand,0,g,indp);
8328: return g;
8329: }
8330:
8331: NODE nd_f4_trace(int m,int **indp)
8332: {
8333: int i,nh,stat,index;
8334: NODE r,g;
8335: ND_pairs d,l,l0,t;
8336: ND spol,red;
8337: NDV nf,redv,nfqv,nfv;
8338: NM s0,s;
8339: NODE rp0,srp0,nflist;
8340: int nsp,nred,col,rank,len,k,j,a;
8341: UINT c;
8342: UINT **spmat;
8343: UINT *s0vect,*svect,*p,*v;
8344: int *colstat;
8345: IndArray *imat;
8346: int *rhead;
8347: int spcol,sprow;
8348: int sugar;
8349: PGeoBucket bucket;
8350: struct oEGT eg0,eg1,eg_f4;
8351:
8352: g = 0; d = 0;
8353: for ( i = 0; i < nd_psn; i++ ) {
8354: d = update_pairs(d,g,i,0);
8355: g = update_base(g,i);
8356: }
8357: while ( d ) {
8358: get_eg(&eg0);
8359: l = nd_minsugarp(d,&d);
8360: sugar = SG(l);
8361: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8362: bucket = create_pbucket();
8363: stat = nd_sp_f4(m,0,l,bucket);
8364: if ( !stat ) {
8365: for ( t = l; NEXT(t); t = NEXT(t) );
8366: NEXT(t) = d; d = l;
8367: d = nd_reconstruct(1,d);
8368: continue;
8369: }
8370: if ( bucket->m < 0 ) continue;
8371: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8372: if ( !col ) {
8373: for ( t = l; NEXT(t); t = NEXT(t) );
8374: NEXT(t) = d; d = l;
8375: d = nd_reconstruct(1,d);
8376: continue;
8377: }
8378: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8379: if ( DP_Print )
1.3 noro 8380: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8381: sugar,eg_f4.exectime);
1.1 noro 8382: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8383: if ( !l0 ) continue;
8384: l = l0;
8385:
8386: /* over Q */
8387: bucket = create_pbucket();
8388: stat = nd_sp_f4(0,1,l,bucket);
8389: if ( !stat ) {
8390: for ( t = l; NEXT(t); t = NEXT(t) );
8391: NEXT(t) = d; d = l;
8392: d = nd_reconstruct(1,d);
8393: continue;
8394: }
8395: if ( bucket->m < 0 ) continue;
8396: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8397: if ( !col ) {
8398: for ( t = l; NEXT(t); t = NEXT(t) );
8399: NEXT(t) = d; d = l;
8400: d = nd_reconstruct(1,d);
8401: continue;
8402: }
8403: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8404: /* adding new bases */
8405: for ( r = nflist; r; r = NEXT(r) ) {
8406: nfqv = (NDV)BDY(r);
8407: ndv_removecont(0,nfqv);
1.6 noro 8408: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8409: if ( nd_nalg ) {
8410: ND nf1;
8411:
8412: nf1 = ndvtond(m,nfqv);
8413: nd_monic(0,&nf1);
8414: nd_removecont(0,nf1);
8415: nfqv = ndtondv(0,nf1); nd_free(nf1);
8416: }
8417: nfv = ndv_dup(0,nfqv);
8418: ndv_mod(m,nfv);
8419: ndv_removecont(m,nfv);
1.24 noro 8420: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8421: d = update_pairs(d,g,nh,0);
8422: g = update_base(g,nh);
8423: }
8424: }
8425: #if 0
8426: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8427: #endif
8428: conv_ilist(nd_demand,1,g,indp);
8429: return g;
8430: }
8431:
8432: int rref(matrix mat,int *sugar)
8433: {
8434: int row,col,i,j,k,l,s,wcol,wj;
8435: unsigned long bj;
8436: unsigned long **a;
8437: unsigned long *ai,*ak,*as,*t;
8438: int *pivot;
8439:
8440: row = mat->row;
8441: col = mat->col;
8442: a = mat->a;
8443: wcol = (col+BLEN-1)/BLEN;
8444: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8445: i = 0;
8446: for ( j = 0; j < col; j++ ) {
8447: wj = j/BLEN; bj = 1L<<(j%BLEN);
8448: for ( k = i; k < row; k++ )
8449: if ( a[k][wj] & bj ) break;
8450: if ( k == row ) continue;
8451: pivot[i] = j;
8452: if ( k != i ) {
8453: t = a[i]; a[i] = a[k]; a[k] = t;
8454: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8455: }
8456: ai = a[i];
8457: for ( k = i+1; k < row; k++ ) {
8458: ak = a[k];
8459: if ( ak[wj] & bj ) {
8460: for ( l = wj; l < wcol; l++ )
8461: ak[l] ^= ai[l];
8462: sugar[k] = MAX(sugar[k],sugar[i]);
8463: }
8464: }
8465: i++;
8466: }
8467: for ( k = i-1; k >= 0; k-- ) {
8468: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8469: ak = a[k];
8470: for ( s = 0; s < k; s++ ) {
8471: as = a[s];
8472: if ( as[wj] & bj ) {
8473: for ( l = wj; l < wcol; l++ )
8474: as[l] ^= ak[l];
8475: sugar[s] = MAX(sugar[s],sugar[k]);
8476: }
8477: }
8478: }
8479: return i;
8480: }
8481:
8482: void print_matrix(matrix mat)
8483: {
8484: int row,col,i,j;
8485: unsigned long *ai;
8486:
8487: row = mat->row;
8488: col = mat->col;
8489: printf("%d x %d\n",row,col);
8490: for ( i = 0; i < row; i++ ) {
8491: ai = mat->a[i];
8492: for ( j = 0; j < col; j++ ) {
8493: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8494: else putchar('0');
8495: }
8496: putchar('\n');
8497: }
8498: }
8499:
8500: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8501:
8502: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8503: {
8504: int row,col,wcol,wj,i,j;
8505: unsigned long bj;
8506: unsigned long *ai;
8507: unsigned long **a;
8508: int len;
8509: int *pos;
8510:
8511: row = mat->row;
8512: col = mat->col;
8513: wcol = (col+BLEN-1)/BLEN;
8514: pos = (int *)MALLOC(wcol*sizeof(int));
8515: bzero(pos,wcol*sizeof(int));
8516: for ( i = j = 0; i < wcol; i++ )
8517: if ( v[i] ) pos[j++] = i;;
8518: len = j;
8519: wj = rhead/BLEN;
8520: bj = 1L<<rhead%BLEN;
8521: a = mat->a;
8522: for ( i = 0; i < row; i++ ) {
8523: ai = a[i];
8524: if ( ai[wj]&bj ) {
8525: for ( j = 0; j < len; j++ )
8526: ai[pos[j]] ^= v[pos[j]];
8527: sugar[i] = MAX(sugar[i],rsugar);
8528: }
8529: }
8530: }
8531:
8532: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8533: {
8534: int nsp,nred,i,i0,k,rank,row;
8535: NODE r0,rp;
8536: ND_pairs sp;
8537: ND spol;
8538: NM_ind_pair rt;
8539: int *s0hash;
8540: UINT *s;
8541: int *pivot,*sugar,*head;
8542: matrix mat;
8543: NM m;
8544: NODE r;
8545: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8546: int rhead,rsugar,size;
8547: unsigned long *v;
8548:
8549: get_eg(&eg0);
8550: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8551: nred = length(rp0);
8552: mat = alloc_matrix(nsp,col);
8553: s0hash = (int *)MALLOC(col*sizeof(int));
8554: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8555: s0hash[i] = ndl_hash_value(s);
8556:
8557: sugar = (int *)MALLOC(nsp*sizeof(int));
8558: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8559: nd_sp(2,0,sp,&spol);
8560: if ( spol ) {
8561: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8562: sugar[i] = SG(spol);
8563: i++;
8564: }
8565: }
8566: mat->row = i;
8567: if ( DP_Print ) {
8568: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8569: }
8570: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8571: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8572: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8573: rt = (NM_ind_pair)BDY(rp);
8574: bzero(v,size);
8575: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8576: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8577: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8578: }
8579:
8580: get_eg(&eg1);
8581: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8582: rank = rref(mat,sugar);
8583:
8584: for ( i = 0, r0 = 0; i < rank; i++ ) {
8585: NEXTNODE(r0,r);
8586: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8587: SG((NDV)BDY(r)) = sugar[i];
8588: }
8589: if ( r0 ) NEXT(r) = 0;
8590: get_eg(&eg2);
8591: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8592: if ( DP_Print ) {
8593: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8594: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8595: fflush(asir_out);
8596: }
8597: return r0;
8598: }
8599:
8600:
8601: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8602: {
8603: IndArray *imat;
1.11 noro 8604: int nsp,nred,i,start;
1.1 noro 8605: int *rhead;
8606: NODE r0,rp;
8607: ND_pairs sp;
8608: NM_ind_pair *rvect;
8609: UINT *s;
8610: int *s0hash;
1.11 noro 8611: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8612:
8613: if ( m == 2 && nd_rref2 )
8614: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8615:
8616: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8617: nred = length(rp0);
8618: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8619: rhead = (int *)MALLOC(col*sizeof(int));
8620: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8621:
8622: /* construction of index arrays */
1.11 noro 8623: get_eg(&eg0);
1.1 noro 8624: if ( DP_Print ) {
1.11 noro 8625: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8626: fflush(asir_out);
1.1 noro 8627: }
8628: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8629: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8630: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8631: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8632: rhead[imat[i]->head] = 1;
1.11 noro 8633: start = imat[i]->head;
8634: }
1.12 noro 8635: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8636: if ( DP_Print ) {
8637: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8638: fflush(asir_out);
1.1 noro 8639: }
8640: if ( m > 0 )
1.7 noro 8641: #if SIZEOF_LONG==8
1.1 noro 8642: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8643: #else
8644: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8645: #endif
8646: else if ( m == -1 )
8647: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8648: else if ( m == -2 )
8649: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8650: else
8651: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8652: return r0;
8653: }
8654:
8655: /* for Fp, 2<=p<2^16 */
8656:
8657: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8658: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8659: {
8660: int spcol,sprow,a;
8661: int i,j,k,l,rank;
8662: NODE r0,r;
8663: ND_pairs sp;
8664: ND spol;
8665: UINT **spmat;
8666: UINT *svect,*v;
8667: int *colstat;
8668: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8669: int maxrs;
8670: int *spsugar;
8671: ND_pairs *spactive;
8672:
8673: spcol = col-nred;
8674: get_eg(&eg0);
8675: /* elimination (1st step) */
8676: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8677: svect = (UINT *)MALLOC(col*sizeof(UINT));
8678: spsugar = (int *)MALLOC(nsp*sizeof(int));
8679: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8680: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8681: nd_sp(m,0,sp,&spol);
8682: if ( !spol ) continue;
8683: nd_to_vect(m,s0vect,col,spol,svect);
8684: if ( m == -1 )
8685: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8686: else
1.40 noro 8687: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 8688: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8689: if ( i < col ) {
8690: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8691: for ( j = k = 0; j < col; j++ )
8692: if ( !rhead[j] ) v[k++] = svect[j];
8693: spsugar[sprow] = MAX(maxrs,SG(spol));
8694: if ( nz )
8695: spactive[sprow] = sp;
8696: sprow++;
8697: }
8698: nd_free(spol);
8699: }
8700: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8701: if ( DP_Print ) {
1.5 noro 8702: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8703: fflush(asir_out);
8704: }
8705: /* free index arrays */
8706: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8707:
8708: /* elimination (2nd step) */
8709: colstat = (int *)MALLOC(spcol*sizeof(int));
8710: if ( m == -1 )
8711: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8712: else
8713: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8714: r0 = 0;
8715: for ( i = 0; i < rank; i++ ) {
8716: NEXTNODE(r0,r); BDY(r) =
8717: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8718: SG((NDV)BDY(r)) = spsugar[i];
8719: GCFREE(spmat[i]);
8720: }
8721: if ( r0 ) NEXT(r) = 0;
8722:
8723: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8724: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8725: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8726: if ( DP_Print ) {
1.5 noro 8727: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8728: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8729: nsp,nred,sprow,spcol,rank);
1.5 noro 8730: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8731: }
8732: if ( nz ) {
8733: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8734: if ( rank > 0 ) {
8735: NEXT(spactive[rank-1]) = 0;
8736: *nz = spactive[0];
8737: } else
8738: *nz = 0;
8739: }
8740: return r0;
8741: }
8742:
1.40 noro 8743: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8744: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
8745: {
8746: int spcol,sprow,a;
8747: int i,j,k,l,rank;
8748: NODE r0,r;
8749: ND_pairs sp;
8750: ND spol;
8751: UINT **spmat;
8752: UINT *svect,*cvect;
8753: UINT *v;
8754: int *colstat;
8755: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8756: int maxrs;
8757: int *spsugar;
8758: ND_pairs *spactive;
8759: SIG *spsig;
8760:
8761: get_eg(&eg0);
8762: /* elimination (1st step) */
8763: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8764: spsugar = (int *)MALLOC(nsp*sizeof(int));
8765: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
8766: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8767: nd_sp(m,0,sp,&spol);
8768: if ( !spol ) {
8769: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8770: continue;
8771: }
8772: svect = (UINT *)MALLOC(col*sizeof(UINT));
8773: nd_to_vect(m,s0vect,col,spol,svect);
8774: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
8775: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8776: if ( i < col ) {
8777: spmat[sprow] = svect;
8778: spsugar[sprow] = MAX(maxrs,SG(spol));
8779: spsig[sprow] = sp->sig;
8780: sprow++;
8781: } else {
8782: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
8783: }
8784: nd_free(spol);
8785: }
8786: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
8787: if ( DP_Print ) {
8788: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
8789: fflush(asir_out);
8790: }
8791: /* free index arrays */
8792: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8793:
8794: /* elimination (2nd step) */
8795: colstat = (int *)MALLOC(col*sizeof(int));
8796: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
8797: r0 = 0;
8798: for ( i = 0; i < sprow; i++ ) {
8799: if ( spsugar[i] >= 0 ) {
8800: NEXTNODE(r0,r);
8801: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
8802: SG((NDV)BDY(r)) = spsugar[i];
8803: ((NDV)BDY(r))->sig = spsig[i];
8804: } else
8805: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
8806: GCFREE(spmat[i]);
8807: }
8808: if ( r0 ) NEXT(r) = 0;
8809: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
8810: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8811: if ( DP_Print ) {
8812: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
8813: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8814: nsp,nred,sprow,col,rank);
8815: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
8816: }
8817: return r0;
8818: }
8819:
1.1 noro 8820:
8821: /* for small finite fields */
8822:
8823: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8824: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8825: {
8826: int spcol,sprow,a;
8827: int i,j,k,l,rank;
8828: NODE r0,r;
8829: ND_pairs sp;
8830: ND spol;
8831: UINT **spmat;
8832: UINT *svect,*v;
8833: int *colstat;
8834: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8835: int maxrs;
8836: int *spsugar;
8837: ND_pairs *spactive;
8838:
8839: spcol = col-nred;
8840: get_eg(&eg0);
8841: /* elimination (1st step) */
8842: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8843: svect = (UINT *)MALLOC(col*sizeof(UINT));
8844: spsugar = (int *)MALLOC(nsp*sizeof(int));
8845: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8846: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8847: nd_sp(m,0,sp,&spol);
8848: if ( !spol ) continue;
8849: nd_to_vect(m,s0vect,col,spol,svect);
8850: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8851: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8852: if ( i < col ) {
8853: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8854: for ( j = k = 0; j < col; j++ )
8855: if ( !rhead[j] ) v[k++] = svect[j];
8856: spsugar[sprow] = MAX(maxrs,SG(spol));
8857: if ( nz )
8858: spactive[sprow] = sp;
8859: sprow++;
8860: }
8861: nd_free(spol);
8862: }
8863: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8864: if ( DP_Print ) {
1.5 noro 8865: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8866: fflush(asir_out);
8867: }
8868: /* free index arrays */
8869: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8870:
8871: /* elimination (2nd step) */
8872: colstat = (int *)MALLOC(spcol*sizeof(int));
8873: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8874: r0 = 0;
8875: for ( i = 0; i < rank; i++ ) {
8876: NEXTNODE(r0,r); BDY(r) =
8877: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8878: SG((NDV)BDY(r)) = spsugar[i];
8879: GCFREE(spmat[i]);
8880: }
8881: if ( r0 ) NEXT(r) = 0;
8882:
8883: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8884: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8885: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8886: if ( DP_Print ) {
1.5 noro 8887: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8888: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8889: nsp,nred,sprow,spcol,rank);
1.5 noro 8890: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8891: }
8892: if ( nz ) {
8893: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8894: if ( rank > 0 ) {
8895: NEXT(spactive[rank-1]) = 0;
8896: *nz = spactive[0];
8897: } else
8898: *nz = 0;
8899: }
8900: return r0;
8901: }
8902:
8903: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8904: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8905: {
8906: int spcol,sprow,a;
8907: int i,j,k,l,rank;
8908: NODE r0,r;
8909: ND_pairs sp;
8910: ND spol;
8911: mpz_t **spmat;
8912: mpz_t *svect,*v;
8913: int *colstat;
8914: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8915: int maxrs;
8916: int *spsugar;
8917: pointer *w;
8918:
8919: spcol = col-nred;
8920: get_eg(&eg0);
8921: /* elimination (1st step) */
8922: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8923: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8924: spsugar = (int *)MALLOC(nsp*sizeof(int));
8925: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8926: nd_sp(m,trace,sp,&spol);
8927: if ( !spol ) continue;
8928: nd_to_vect_lf(s0vect,col,spol,svect);
8929: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8930: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8931: if ( i < col ) {
8932: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8933: for ( j = k = 0; j < col; j++ )
8934: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8935: spsugar[sprow] = MAX(maxrs,SG(spol));
8936: sprow++;
8937: }
8938: /* nd_free(spol); */
8939: }
8940: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8941: if ( DP_Print ) {
1.5 noro 8942: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8943: fflush(asir_out);
8944: }
8945: /* free index arrays */
8946: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8947:
8948: /* elimination (2nd step) */
8949: colstat = (int *)MALLOC(spcol*sizeof(int));
8950: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8951: w = (pointer *)MALLOC(rank*sizeof(pointer));
8952: for ( i = 0; i < rank; i++ ) {
8953: #if 0
8954: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8955: SG((NDV)w[rank-i-1]) = spsugar[i];
8956: #else
8957: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8958: SG((NDV)w[i]) = spsugar[i];
8959: #endif
8960: /* GCFREE(spmat[i]); */
8961:
8962: }
8963: #if 0
8964: qsort(w,rank,sizeof(NDV),
8965: (int (*)(const void *,const void *))ndv_compare);
8966: #endif
8967: r0 = 0;
8968: for ( i = 0; i < rank; i++ ) {
8969: NEXTNODE(r0,r); BDY(r) = w[i];
8970: }
8971: if ( r0 ) NEXT(r) = 0;
8972:
8973: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8974: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8975: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8976: if ( DP_Print ) {
1.5 noro 8977: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8978: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8979: nsp,nred,sprow,spcol,rank);
1.5 noro 8980: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8981: }
8982: return r0;
8983: }
8984:
8985: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8986: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8987: {
8988: int spcol,sprow,a;
8989: int i,j,k,l,rank;
8990: NODE r0,r;
8991: ND_pairs sp;
8992: ND spol;
8993: Z **spmat;
8994: Z *svect,*v;
8995: int *colstat;
8996: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8997: int maxrs;
8998: int *spsugar;
8999: pointer *w;
9000:
9001: spcol = col-nred;
9002: get_eg(&eg0);
9003: /* elimination (1st step) */
9004: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
9005: svect = (Z *)MALLOC(col*sizeof(Q));
9006: spsugar = (int *)MALLOC(nsp*sizeof(int));
9007: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9008: nd_sp(0,trace,sp,&spol);
9009: if ( !spol ) continue;
9010: nd_to_vect_q(s0vect,col,spol,svect);
9011: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
9012: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9013: if ( i < col ) {
9014: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
9015: for ( j = k = 0; j < col; j++ )
9016: if ( !rhead[j] ) v[k++] = svect[j];
9017: spsugar[sprow] = MAX(maxrs,SG(spol));
9018: sprow++;
9019: }
9020: /* nd_free(spol); */
9021: }
9022: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9023: if ( DP_Print ) {
1.5 noro 9024: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9025: fflush(asir_out);
9026: }
9027: /* free index arrays */
9028: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9029:
9030: /* elimination (2nd step) */
9031: colstat = (int *)MALLOC(spcol*sizeof(int));
9032: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
9033: w = (pointer *)MALLOC(rank*sizeof(pointer));
9034: for ( i = 0; i < rank; i++ ) {
9035: #if 0
9036: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9037: SG((NDV)w[rank-i-1]) = spsugar[i];
9038: #else
9039: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9040: SG((NDV)w[i]) = spsugar[i];
9041: #endif
9042: /* GCFREE(spmat[i]); */
9043: }
9044: #if 0
9045: qsort(w,rank,sizeof(NDV),
9046: (int (*)(const void *,const void *))ndv_compare);
9047: #endif
9048: r0 = 0;
9049: for ( i = 0; i < rank; i++ ) {
9050: NEXTNODE(r0,r); BDY(r) = w[i];
9051: }
9052: if ( r0 ) NEXT(r) = 0;
9053:
9054: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9055: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9056: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9057: if ( DP_Print ) {
1.5 noro 9058: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9059: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9060: nsp,nred,sprow,spcol,rank);
1.5 noro 9061: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9062: }
9063: return r0;
9064: }
9065:
9066: FILE *nd_write,*nd_read;
9067:
9068: void nd_send_int(int a) {
9069: write_int(nd_write,(unsigned int *)&a);
9070: }
9071:
9072: void nd_send_intarray(int *p,int len) {
9073: write_intarray(nd_write,(unsigned int *)p,len);
9074: }
9075:
9076: int nd_recv_int() {
9077: int a;
9078:
9079: read_int(nd_read,(unsigned int *)&a);
9080: return a;
9081: }
9082:
9083: void nd_recv_intarray(int *p,int len) {
9084: read_intarray(nd_read,(unsigned int *)p,len);
9085: }
9086:
9087: void nd_send_ndv(NDV p) {
9088: int len,i;
9089: NMV m;
9090:
9091: if ( !p ) nd_send_int(0);
9092: else {
9093: len = LEN(p);
9094: nd_send_int(len);
9095: m = BDY(p);
9096: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9097: nd_send_int(CM(m));
9098: nd_send_intarray((int *)DL(m),nd_wpd);
9099: }
9100: }
9101: }
9102:
9103: void nd_send_nd(ND p) {
9104: int len,i;
9105: NM m;
9106:
9107: if ( !p ) nd_send_int(0);
9108: else {
9109: len = LEN(p);
9110: nd_send_int(len);
9111: m = BDY(p);
9112: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9113: nd_send_int(CM(m));
9114: nd_send_intarray((int *)DL(m),nd_wpd);
9115: }
9116: }
9117: }
9118:
9119: NDV nd_recv_ndv()
9120: {
9121: int len,i;
9122: NMV m,m0;
9123: NDV r;
9124:
9125: len = nd_recv_int();
9126: if ( !len ) return 0;
9127: else {
9128: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9129: #if 0
9130: ndv_alloc += len*nmv_adv;
9131: #endif
9132: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9133: CM(m) = nd_recv_int();
9134: nd_recv_intarray((int *)DL(m),nd_wpd);
9135: }
9136: MKNDV(nd_nvar,m0,len,r);
9137: return r;
9138: }
9139: }
9140:
9141: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9142: {
9143: int i,j,t,c,rank,inv;
9144: int *ci,*ri;
9145: Z dn;
9146: MAT m,nm;
9147:
9148: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9149: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9150: for ( i = 0; i < row; i++ )
9151: for ( j = 0; j < col; j++ )
9152: mat0[i][j] = 0;
9153: c = col-rank;
9154: for ( i = 0; i < rank; i++ ) {
9155: mat0[i][ri[i]] = dn;
9156: for ( j = 0; j < c; j++ )
9157: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9158: }
9159: return rank;
9160: }
9161:
9162: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9163: {
9164: int i,j,k,l,inv,a,rank,s;
9165: unsigned int *t,*pivot,*pk;
9166: unsigned int **mat;
9167: ND_pairs pair;
9168:
9169: mat = (unsigned int **)mat0;
9170: for ( rank = 0, j = 0; j < col; j++ ) {
9171: for ( i = rank; i < row; i++ )
9172: mat[i][j] %= md;
9173: for ( i = rank; i < row; i++ )
9174: if ( mat[i][j] )
9175: break;
9176: if ( i == row ) {
9177: colstat[j] = 0;
9178: continue;
9179: } else
9180: colstat[j] = 1;
9181: if ( i != rank ) {
9182: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9183: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9184: if ( spactive ) {
9185: pair = spactive[i]; spactive[i] = spactive[rank];
9186: spactive[rank] = pair;
9187: }
9188: }
9189: pivot = mat[rank];
9190: s = sugar[rank];
9191: inv = invm(pivot[j],md);
9192: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9193: if ( *pk ) {
9194: if ( *pk >= (unsigned int)md )
9195: *pk %= md;
9196: DMAR(*pk,inv,0,md,*pk)
9197: }
9198: for ( i = rank+1; i < row; i++ ) {
9199: t = mat[i];
9200: if ( (a = t[j]) != 0 ) {
9201: sugar[i] = MAX(sugar[i],s);
9202: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9203: }
9204: }
9205: rank++;
9206: }
9207: for ( j = col-1, l = rank-1; j >= 0; j-- )
9208: if ( colstat[j] ) {
9209: pivot = mat[l];
9210: s = sugar[l];
9211: for ( i = 0; i < l; i++ ) {
9212: t = mat[i];
9213: t[j] %= md;
9214: if ( (a = t[j]) != 0 ) {
9215: sugar[i] = MAX(sugar[i],s);
9216: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9217: }
9218: }
9219: l--;
9220: }
9221: for ( j = 0, l = 0; l < rank; j++ )
9222: if ( colstat[j] ) {
9223: t = mat[l];
9224: for ( k = j; k < col; k++ )
9225: if ( t[k] >= (unsigned int)md )
9226: t[k] %= md;
9227: l++;
9228: }
9229: return rank;
9230: }
9231:
1.40 noro 9232: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9233: {
9234: int i,j,k,l,rank,s,imin;
9235: UINT inv;
9236: UINT a;
9237: UINT *t,*pivot,*pk;
9238: UINT *ck;
9239: UINT *ct;
9240: ND_pairs pair;
9241: SIG sg;
9242: int *used;
9243:
9244: used = (int *)MALLOC(row*sizeof(int));
9245: for ( j = 0; j < col; j++ ) {
9246: for ( i = 0; i < row; i++ )
9247: a = mat[i][j] %= md;
9248: for ( i = 0; i < row; i++ )
9249: if ( !used[i] && mat[i][j] ) break;
9250: if ( i == row ) {
9251: colstat[j] = 0;
9252: continue;
9253: } else {
9254: colstat[j] = 1;
9255: used[i] = 1;
9256: }
9257: /* column j is normalized */
9258: s = sugar[i];
9259: inv = invm(mat[i][j],md);
9260: /* normalize pivot row */
9261: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9262: DMAR(*pk,inv,0,md,*pk);
9263: }
9264: for ( k = i+1; k < row; k++ ) {
9265: if ( (a = mat[k][j]) != 0 ) {
9266: sugar[k] = MAX(sugar[k],s);
9267: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9268: Nf4_red++;
9269: }
9270: }
9271: }
9272: rank = 0;
9273: for ( i = 0; i < row; i++ ) {
9274: for ( j = 0; j < col; j++ )
9275: if ( mat[i][j] ) break;
9276: if ( j == col ) sugar[i] = -1;
9277: else rank++;
9278: }
9279: return rank;
9280: }
9281:
1.1 noro 9282:
1.7 noro 9283: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9284: {
1.7 noro 9285: int i,j,k,l,inv,a,rank,s;
9286: unsigned int *t,*pivot,*pk;
9287: unsigned int **mat;
9288:
9289: mat = (unsigned int **)mat0;
9290: for ( rank = 0, j = 0; j < col; j++ ) {
9291: for ( i = rank; i < row; i++ )
9292: if ( mat[i][j] )
9293: break;
9294: if ( i == row ) {
9295: colstat[j] = 0;
9296: continue;
9297: } else
9298: colstat[j] = 1;
9299: if ( i != rank ) {
9300: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9301: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9302: }
9303: pivot = mat[rank];
9304: s = sugar[rank];
9305: inv = _invsf(pivot[j]);
9306: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9307: if ( *pk )
9308: *pk = _mulsf(*pk,inv);
9309: for ( i = rank+1; i < row; i++ ) {
9310: t = mat[i];
9311: if ( (a = t[j]) != 0 ) {
9312: sugar[i] = MAX(sugar[i],s);
9313: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9314: }
9315: }
9316: rank++;
9317: }
9318: for ( j = col-1, l = rank-1; j >= 0; j-- )
9319: if ( colstat[j] ) {
9320: pivot = mat[l];
9321: s = sugar[l];
9322: for ( i = 0; i < l; i++ ) {
9323: t = mat[i];
9324: if ( (a = t[j]) != 0 ) {
9325: sugar[i] = MAX(sugar[i],s);
9326: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9327: }
9328: }
9329: l--;
9330: }
9331: return rank;
9332: }
1.1 noro 9333:
1.7 noro 9334: int ndv_ishomo(NDV p)
9335: {
9336: NMV m;
9337: int len,h;
1.1 noro 9338:
9339: if ( !p ) return 1;
9340: len = LEN(p);
9341: m = BDY(p);
9342: h = TD(DL(m));
9343: NMV_ADV(m);
9344: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9345: if ( TD(DL(m)) != h ) {
9346: return 0;
9347: }
1.1 noro 9348: return 1;
9349: }
9350:
9351: void ndv_save(NDV p,int index)
9352: {
9353: FILE *s;
9354: char name[BUFSIZ];
9355: short id;
9356: int nv,sugar,len,n,i,td,e,j;
9357: NMV m;
9358: unsigned int *dl;
9359: int mpos;
9360:
9361: sprintf(name,"%s/%d",Demand,index);
9362: s = fopen(name,"w");
9363: savevl(s,0);
9364: if ( !p ) {
9365: saveobj(s,0);
9366: return;
9367: }
9368: id = O_DP;
9369: nv = NV(p);
9370: sugar = SG(p);
9371: len = LEN(p);
9372: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9373: write_int(s,(unsigned int *)&len);
9374:
9375: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9376: saveobj(s,(Obj)CZ(m));
1.1 noro 9377: dl = DL(m);
9378: td = TD(dl);
9379: write_int(s,(unsigned int *)&td);
9380: for ( j = 0; j < nv; j++ ) {
9381: e = GET_EXP(dl,j);
9382: write_int(s,(unsigned int *)&e);
9383: }
9384: if ( nd_module ) {
9385: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
9386: }
9387: }
9388: fclose(s);
9389: }
9390:
9391: void nd_save_mod(ND p,int index)
9392: {
9393: FILE *s;
9394: char name[BUFSIZ];
9395: int nv,sugar,len,c;
9396: NM m;
9397:
9398: sprintf(name,"%s/%d",Demand,index);
9399: s = fopen(name,"w");
9400: if ( !p ) {
9401: len = 0;
9402: write_int(s,(unsigned int *)&len);
9403: fclose(s);
9404: return;
9405: }
9406: nv = NV(p);
9407: sugar = SG(p);
9408: len = LEN(p);
9409: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9410: for ( m = BDY(p); m; m = NEXT(m) ) {
9411: c = CM(m); write_int(s,(unsigned int *)&c);
9412: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9413: }
9414: fclose(s);
9415: }
9416:
9417: NDV ndv_load(int index)
9418: {
9419: FILE *s;
9420: char name[BUFSIZ];
9421: short id;
9422: int nv,sugar,len,n,i,td,e,j;
9423: NDV d;
9424: NMV m0,m;
9425: unsigned int *dl;
9426: Obj obj;
9427: int mpos;
9428:
9429: sprintf(name,"%s/%d",Demand,index);
9430: s = fopen(name,"r");
9431: if ( !s ) return 0;
9432:
9433: skipvl(s);
9434: read_short(s,(unsigned short *)&id);
9435: if ( !id ) return 0;
9436: read_int(s,(unsigned int *)&nv);
9437: read_int(s,(unsigned int *)&sugar);
9438: read_int(s,(unsigned int *)&len);
9439:
9440: m0 = m = MALLOC(len*nmv_adv);
9441: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9442: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9443: dl = DL(m);
9444: ndl_zero(dl);
9445: read_int(s,(unsigned int *)&td); TD(dl) = td;
9446: for ( j = 0; j < nv; j++ ) {
9447: read_int(s,(unsigned int *)&e);
9448: PUT_EXP(dl,j,e);
9449: }
9450: if ( nd_module ) {
9451: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9452: }
9453: if ( nd_blockmask ) ndl_weight_mask(dl);
9454: }
9455: fclose(s);
9456: MKNDV(nv,m0,len,d);
9457: SG(d) = sugar;
9458: return d;
9459: }
9460:
9461: ND nd_load_mod(int index)
9462: {
9463: FILE *s;
9464: char name[BUFSIZ];
9465: int nv,sugar,len,i,c;
9466: ND d;
9467: NM m0,m;
9468:
9469: sprintf(name,"%s/%d",Demand,index);
9470: s = fopen(name,"r");
9471: /* if the file does not exist, it means p[index]=0 */
9472: if ( !s ) return 0;
9473:
9474: read_int(s,(unsigned int *)&nv);
9475: if ( !nv ) { fclose(s); return 0; }
9476:
9477: read_int(s,(unsigned int *)&sugar);
9478: read_int(s,(unsigned int *)&len);
9479: for ( m0 = 0, i = 0; i < len; i++ ) {
9480: NEXTNM(m0,m);
9481: read_int(s,(unsigned int *)&c); CM(m) = c;
9482: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9483: }
9484: NEXT(m) = 0;
9485: MKND(nv,m0,len,d);
9486: SG(d) = sugar;
9487: fclose(s);
9488: return d;
9489: }
9490:
9491: void nd_det(int mod,MAT f,P *rp)
9492: {
9493: VL fv,tv;
9494: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9495: pointer **m;
9496: P **w;
9497: P mp,r;
9498: NDV **dm;
9499: NDV *t,*mi,*mj;
9500: NDV d,s,mij,mjj;
9501: ND u;
9502: NMV nmv;
9503: UINT *bound;
9504: PGeoBucket bucket;
9505: struct order_spec *ord;
9506: Z dq,dt,ds;
9507: Z mone;
9508: Z gn,qn,dn0,nm,dn;
9509:
9510: create_order_spec(0,0,&ord);
9511: nd_init_ord(ord);
9512: get_vars((Obj)f,&fv);
9513: if ( f->row != f->col )
9514: error("nd_det : non-square matrix");
9515: n = f->row;
9516: m = f->body;
9517: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9518:
9519: if ( !nvar ) {
9520: if ( !mod )
9521: detp(CO,(P **)m,n,rp);
9522: else {
9523: w = (P **)almat_pointer(n,n);
9524: for ( i = 0; i < n; i++ )
9525: for ( j = 0; j < n; j++ )
9526: ptomp(mod,(P)m[i][j],&w[i][j]);
9527: detmp(CO,mod,w,n,&mp);
9528: mptop(mp,rp);
9529: }
9530: return;
9531: }
9532:
9533: if ( !mod ) {
9534: w = (P **)almat_pointer(n,n);
9535: dq = ONE;
9536: for ( i = 0; i < n; i++ ) {
9537: dn0 = ONE;
9538: for ( j = 0; j < n; j++ ) {
9539: if ( !m[i][j] ) continue;
9540: lgp(m[i][j],&nm,&dn);
1.6 noro 9541: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9542: }
9543: if ( !UNIZ(dn0) ) {
9544: ds = dn0;
9545: for ( j = 0; j < n; j++ )
9546: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9547: mulz(dq,ds,&dt); dq = dt;
9548: } else
9549: for ( j = 0; j < n; j++ )
9550: w[i][j] = (P)m[i][j];
9551: }
9552: m = (pointer **)w;
9553: }
9554:
9555: for ( i = 0, max = 1; i < n; i++ )
9556: for ( j = 0; j < n; j++ )
9557: for ( tv = fv; tv; tv = NEXT(tv) ) {
9558: e = getdeg(tv->v,(P)m[i][j]);
9559: max = MAX(e,max);
9560: }
9561: nd_setup_parameters(nvar,max);
9562: dm = (NDV **)almat_pointer(n,n);
9563: for ( i = 0, max = 1; i < n; i++ )
9564: for ( j = 0; j < n; j++ ) {
9565: dm[i][j] = ptondv(CO,fv,m[i][j]);
9566: if ( mod ) ndv_mod(mod,dm[i][j]);
9567: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9568: }
9569: d = ptondv(CO,fv,(P)ONE);
9570: if ( mod ) ndv_mod(mod,d);
9571: chsgnz(ONE,&mone);
9572: for ( j = 0, sgn = 1; j < n; j++ ) {
9573: if ( DP_Print ) {
9574: fprintf(asir_out,".");
9575: }
9576: for ( i = j; i < n && !dm[i][j]; i++ );
9577: if ( i == n ) {
9578: *rp = 0;
9579: return;
9580: }
9581: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9582: for ( k = j; k < n; k++ )
9583: for ( l = j; l < n; l++ )
9584: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9585: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9586: }
9587: if ( k0 != j ) {
9588: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9589: sgn = -sgn;
9590: }
9591: if ( l0 != j ) {
9592: for ( k = j; k < n; k++ ) {
9593: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9594: }
9595: sgn = -sgn;
9596: }
9597: bound = nd_det_compute_bound(dm,n,j);
9598: for ( k = 0; k < nd_nvar; k++ )
9599: if ( bound[k]*2 > nd_mask0 ) break;
9600: if ( k < nd_nvar )
9601: nd_det_reconstruct(dm,n,j,d);
9602:
9603: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9604: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9605: mi = dm[i]; mij = mi[j];
9606: if ( mod )
9607: ndv_mul_c(mod,mij,mod-1);
9608: else
9609: ndv_mul_c_q(mij,mone);
9610: for ( k = j+1; k < n; k++ ) {
9611: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9612: bucket = create_pbucket();
9613: if ( mi[k] ) {
9614: nmv = BDY(mjj); len = LEN(mjj);
9615: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9616: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9617: add_pbucket(mod,bucket,u);
9618: }
9619: }
9620: if ( mj[k] && mij ) {
9621: nmv = BDY(mij); len = LEN(mij);
9622: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9623: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9624: add_pbucket(mod,bucket,u);
9625: }
9626: }
9627: u = nd_quo(mod,bucket,d);
9628: mi[k] = ndtondv(mod,u);
9629: }
9630: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9631: }
9632: d = mjj;
9633: }
9634: if ( DP_Print ) {
9635: fprintf(asir_out,"\n");
9636: }
9637: if ( sgn < 0 ) {
9638: if ( mod )
9639: ndv_mul_c(mod,d,mod-1);
9640: else
9641: ndv_mul_c_q(d,mone);
9642: }
9643: r = ndvtop(mod,CO,fv,d);
9644: if ( !mod && !UNIQ(dq) )
9645: divsp(CO,r,(P)dq,rp);
9646: else
9647: *rp = r;
9648: }
9649:
9650: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9651: {
9652: NM mr,mr0;
9653: NM tnm;
9654: NMV m;
9655: UINT *d0,*dt,*dm;
9656: int c,n,td,i,c1,c2,len;
9657: Z q;
9658: ND r;
9659:
9660: if ( !p ) return 0;
9661: else {
9662: n = NV(p); m = BDY(p); len = LEN(p);
9663: d0 = DL(m0);
9664: td = TD(d);
9665: mr0 = 0;
9666: NEWNM(tnm);
9667: if ( mod ) {
9668: c = CM(m0);
9669: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9670: ndl_add(DL(m),d0,DL(tnm));
9671: if ( ndl_reducible(DL(tnm),d) ) {
9672: NEXTNM(mr0,mr);
9673: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9674: ndl_copy(DL(tnm),DL(mr));
9675: }
9676: }
9677: } else {
1.6 noro 9678: q = CZ(m0);
1.1 noro 9679: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9680: ndl_add(DL(m),d0,DL(tnm));
9681: if ( ndl_reducible(DL(tnm),d) ) {
9682: NEXTNM(mr0,mr);
1.6 noro 9683: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9684: ndl_copy(DL(tnm),DL(mr));
9685: }
9686: }
9687: }
9688: if ( !mr0 )
9689: return 0;
9690: else {
9691: NEXT(mr) = 0;
9692: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9693: MKND(NV(p),mr0,len,r);
9694: SG(r) = SG(p) + TD(d0);
9695: return r;
9696: }
9697: }
9698: }
9699:
9700: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9701: {
9702: int i,obpe,oadv,h,k,l;
9703: static NM prev_nm_free_list;
9704: EPOS oepos;
9705:
9706: obpe = nd_bpe;
9707: oadv = nmv_adv;
9708: oepos = nd_epos;
9709: if ( obpe < 2 ) nd_bpe = 2;
9710: else if ( obpe < 3 ) nd_bpe = 3;
9711: else if ( obpe < 4 ) nd_bpe = 4;
9712: else if ( obpe < 5 ) nd_bpe = 5;
9713: else if ( obpe < 6 ) nd_bpe = 6;
9714: else if ( obpe < 8 ) nd_bpe = 8;
9715: else if ( obpe < 10 ) nd_bpe = 10;
9716: else if ( obpe < 16 ) nd_bpe = 16;
9717: else if ( obpe < 32 ) nd_bpe = 32;
9718: else error("nd_det_reconstruct : exponent too large");
9719:
9720: nd_setup_parameters(nd_nvar,0);
9721: prev_nm_free_list = _nm_free_list;
9722: _nm_free_list = 0;
9723: for ( k = j; k < n; k++ )
9724: for (l = j; l < n; l++ )
9725: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9726: ndv_realloc(d,obpe,oadv,oepos);
9727: prev_nm_free_list = 0;
9728: #if 0
9729: GC_gcollect();
9730: #endif
9731: }
9732:
9733: /* returns a UINT array containing degree bounds */
9734:
9735: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9736: {
9737: UINT *d0,*d1,*d,*t,*r;
9738: int k,l,i;
9739:
9740: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9741: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9742: for ( k = j; k < n; k++ )
9743: for ( l = j; l < n; l++ )
9744: if ( dm[k][l] ) {
9745: d = ndv_compute_bound(dm[k][l]);
9746: for ( i = 0; i < nd_nvar; i++ )
9747: d0[i] = MAX(d0[i],d[i]);
9748: }
9749: return d0;
9750: }
9751:
9752: DL nd_separate_d(UINT *d,UINT *trans)
9753: {
9754: int n,td,i,e,j;
9755: DL a;
9756:
9757: ndl_zero(trans);
9758: td = 0;
9759: for ( i = 0; i < nd_ntrans; i++ ) {
9760: e = GET_EXP(d,i);
9761: PUT_EXP(trans,i,e);
9762: td += MUL_WEIGHT(e,i);
9763: }
9764: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9765: /* homogenized */
9766: i = nd_nvar-1;
9767: e = GET_EXP(d,i);
9768: PUT_EXP(trans,i,e);
9769: td += MUL_WEIGHT(e,i);
9770: }
9771: TD(trans) = td;
9772: if ( nd_blockmask) ndl_weight_mask(trans);
9773: NEWDL(a,nd_nalg);
9774: td = 0;
9775: for ( i = 0; i < nd_nalg; i++ ) {
9776: j = nd_ntrans+i;
9777: e = GET_EXP(d,j);
9778: a->d[i] = e;
9779: td += e;
9780: }
9781: a->td = td;
9782: return a;
9783: }
9784:
9785: int nd_monic(int mod,ND *p)
9786: {
9787: UINT *trans,*t;
9788: DL alg;
9789: MP mp0,mp;
9790: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9791: ND r;
9792: DL dl;
9793: DP nm;
9794: NDV ndv;
9795: DAlg inv,cd;
9796: ND s,c;
9797: Z l,mul;
9798: Z ln;
9799: int n,ntrans,i,e,td,is_lc,len;
9800: NumberField nf;
9801: struct oEGT eg0,eg1;
9802:
9803: if ( !(nf = get_numberfield()) )
9804: error("nd_monic : current_numberfield is not set");
9805:
9806: /* Q coef -> DAlg coef */
9807: NEWNM(ma0); ma = ma0;
9808: m = BDY(*p);
9809: is_lc = 1;
9810: while ( 1 ) {
9811: NEWMP(mp0); mp = mp0;
1.6 noro 9812: mp->c = (Obj)CZ(m);
1.1 noro 9813: mp->dl = nd_separate_d(DL(m),DL(ma));
9814: NEWNM(mb);
9815: for ( m = NEXT(m); m; m = NEXT(m) ) {
9816: alg = nd_separate_d(DL(m),DL(mb));
9817: if ( !ndl_equal(DL(ma),DL(mb)) )
9818: break;
1.6 noro 9819: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9820: }
9821: NEXT(mp) = 0;
9822: MKDP(nd_nalg,mp0,nm);
9823: MKDAlg(nm,ONE,cd);
9824: if ( is_lc == 1 ) {
9825: /* if the lc is a rational number, we have nothing to do */
9826: if ( !mp0->dl->td )
9827: return 1;
9828:
9829: get_eg(&eg0);
9830: invdalg(cd,&inv);
9831: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9832: /* check the validity of inv */
9833: if ( mod && !remqi((Q)inv->dn,mod) )
9834: return 0;
9835: CA(ma) = nf->one;
9836: is_lc = 0;
9837: ln = ONE;
9838: } else {
9839: muldalg(cd,inv,&CA(ma));
9840: lcmz(ln,CA(ma)->dn,&ln);
9841: }
9842: if ( m ) {
9843: NEXT(ma) = mb; ma = mb;
9844: } else {
9845: NEXT(ma) = 0;
9846: break;
9847: }
9848: }
9849: /* l = lcm(denoms) */
9850: l = ln;
9851: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9852: divsz(l,CA(m)->dn,&mul);
1.1 noro 9853: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9854: NEXTNM(mr0,mr);
1.6 noro 9855: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9856: dl = mp->dl;
9857: td = TD(DL(m));
9858: ndl_copy(DL(m),DL(mr));
9859: for ( i = 0; i < nd_nalg; i++ ) {
9860: e = dl->d[i];
9861: PUT_EXP(DL(mr),i+nd_ntrans,e);
9862: td += MUL_WEIGHT(e,i+nd_ntrans);
9863: }
9864: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9865: TD(DL(mr)) = td;
9866: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9867: }
9868: }
9869: NEXT(mr) = 0;
9870: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9871: MKND(NV(*p),mr0,len,r);
9872: /* XXX */
9873: SG(r) = SG(*p);
9874: nd_free(*p);
9875: *p = r;
9876: return 1;
9877: }
9878:
9879: NODE reverse_node(NODE n)
9880: {
9881: NODE t,t1;
9882:
9883: for ( t = 0; n; n = NEXT(n) ) {
9884: MKNODE(t1,BDY(n),t); t = t1;
9885: }
9886: return t;
9887: }
9888:
9889: P ndc_div(int mod,union oNDC a,union oNDC b)
9890: {
9891: union oNDC c;
9892: int inv,t;
9893:
9894: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9895: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9896: else if ( mod ) {
9897: inv = invm(b.m,mod);
9898: DMAR(a.m,inv,0,mod,t); c.m = t;
9899: } else if ( nd_vc )
9900: divsp(nd_vc,a.p,b.p,&c.p);
9901: else
9902: divsz(a.z,b.z,&c.z);
9903: return ndctop(mod,c);
9904: }
9905:
9906: P ndctop(int mod,union oNDC c)
9907: {
9908: Z q;
9909: int e;
9910: GFS gfs;
9911:
9912: if ( mod == -1 ) {
9913: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9914: } else if ( mod == -2 ) {
1.10 noro 9915: q = c.z; return (P)q;
1.1 noro 9916: } else if ( mod > 0 ) {
1.6 noro 9917: STOZ(c.m,q); return (P)q;
1.1 noro 9918: } else
9919: return (P)c.p;
9920: }
9921:
9922: /* [0,0,0,cont] = p -> p/cont */
9923:
9924: void finalize_tracelist(int i,P cont)
9925: {
9926: LIST l;
9927: NODE node;
9928: Z iq;
9929:
9930: if ( !UNIQ(cont) ) {
9931: node = mknode(4,NULLP,NULLP,NULLP,cont);
9932: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9933: nd_tracelist = node;
9934: }
1.6 noro 9935: STOZ(i,iq);
1.1 noro 9936: nd_tracelist = reverse_node(nd_tracelist);
9937: MKLIST(l,nd_tracelist);
9938: node = mknode(2,iq,l); MKLIST(l,node);
9939: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9940: nd_alltracelist = node; nd_tracelist = 0;
9941: }
9942:
9943: void conv_ilist(int demand,int trace,NODE g,int **indp)
9944: {
9945: int n,i,j;
9946: int *ind;
9947: NODE t;
9948:
9949: n = length(g);
9950: ind = (int *)MALLOC(n*sizeof(int));
9951: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9952: j = (long)BDY(t); ind[i] = j;
9953: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9954: }
9955: if ( indp ) *indp = ind;
9956: }
9957:
1.30 noro 9958: NODE conv_ilist_s(int demand,int trace,int **indp)
9959: {
9960: int n,i,j;
9961: int *ind;
9962: NODE g0,g;
9963:
9964: n = nd_psn;
9965: ind = (int *)MALLOC(n*sizeof(int));
9966: g0 = 0;
9967: for ( i = 0; i < n; i++ ) {
9968: ind[i] = i;
9969: NEXTNODE(g0,g);
9970: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
9971: }
9972: if ( g0 ) NEXT(g) = 0;
9973: if ( indp ) *indp = ind;
9974: return g0;
9975: }
9976:
1.1 noro 9977: void parse_nd_option(NODE opt)
9978: {
1.38 noro 9979: NODE t,p,u;
1.1 noro 9980: int i,s,n;
1.38 noro 9981: char *key;
9982: Obj value;
1.1 noro 9983:
1.41 noro 9984: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 9985: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9986: nd_splist = 0; nd_check_splist = 0;
1.38 noro 9987: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
9988: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 9989: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.39 noro 9990:
1.38 noro 9991: for ( t = opt; t; t = NEXT(t) ) {
9992: p = BDY((LIST)BDY(t));
9993: key = BDY((STRING)BDY(p));
9994: value = (Obj)BDY(NEXT(p));
9995: if ( !strcmp(key,"gentrace") )
9996: nd_gentrace = value?1:0;
9997: else if ( !strcmp(key,"gensyz") )
9998: nd_gensyz = value?1:0;
9999: else if ( !strcmp(key,"nora") )
10000: nd_nora = value?1:0;
1.41 noro 10001: else if ( !strcmp(key,"norb") )
10002: nd_norb = value?1:0;
1.38 noro 10003: else if ( !strcmp(key,"gbblock") ) {
10004: if ( value && OID(value) == O_LIST ) {
1.1 noro 10005: u = BDY((LIST)value);
1.38 noro 10006: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 10007: for ( i = 0; u; u = NEXT(u) ) {
10008: p = BDY((LIST)BDY(u));
1.6 noro 10009: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
10010: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 10011: }
10012: nd_gbblock[i] = -1;
1.38 noro 10013: } else
10014: nd_gbblock = 0;
1.1 noro 10015: } else if ( !strcmp(key,"newelim") )
10016: nd_newelim = value?1:0;
10017: else if ( !strcmp(key,"intersect") )
10018: nd_intersect = value?1:0;
1.17 noro 10019: else if ( !strcmp(key,"syzgen") )
10020: nd_intersect = ZTOS((Q)value);
1.1 noro 10021: else if ( !strcmp(key,"lf") )
10022: nd_lf = value?1:0;
10023: else if ( !strcmp(key,"trace") ) {
1.38 noro 10024: if ( value ) {
10025: u = BDY((LIST)value);
10026: nd_nzlist = BDY((LIST)ARG2(u));
10027: nd_bpe = ZTOS((Q)ARG3(u));
10028: }
1.1 noro 10029: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 10030: nd_f4red = ZTOS((Q)value);
1.1 noro 10031: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 10032: nd_rank0 = value?1:0;
1.1 noro 10033: } else if ( !strcmp(key,"splist") ) {
1.38 noro 10034: nd_splist = value?1:0;
1.1 noro 10035: } else if ( !strcmp(key,"check_splist") ) {
10036: nd_check_splist = BDY((LIST)value);
10037: } else if ( !strcmp(key,"sugarweight") ) {
10038: u = BDY((LIST)value);
1.38 noro 10039: n = length(u);
10040: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 10041: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 10042: nd_sugarweight[i] = ZTOS((Q)BDY(u));
10043: } else if ( !strcmp(key,"f4_td") ) {
10044: nd_f4_td = value?1:0;
10045: } else if ( !strcmp(key,"sba_f4step") ) {
10046: nd_sba_f4step = value?ZTOS((Q)value):0;
10047: } else if ( !strcmp(key,"sba_pot") ) {
10048: nd_sba_pot = value?1:0;
10049: } else if ( !strcmp(key,"sba_largelcm") ) {
10050: nd_sba_largelcm = value?1:0;
1.39 noro 10051: } else if ( !strcmp(key,"sba_dontsort") ) {
10052: nd_sba_dontsort = value?1:0;
1.41 noro 10053: } else if ( !strcmp(key,"sba_redundant_check") ) {
10054: nd_sba_redundant_check = value?1:0;
1.39 noro 10055: } else if ( !strcmp(key,"top") ) {
10056: nd_top = value?1:0;
1.1 noro 10057: }
1.38 noro 10058: }
1.1 noro 10059: }
10060:
10061: ND mdptond(DP d);
10062: ND nd_mul_nm(int mod,NM m0,ND p);
10063: ND nd_mul_nm_lf(NM m0,ND p);
10064: ND *btog(NODE ti,ND **p,int nb,int mod);
10065: ND btog_one(NODE ti,ND *p,int nb,int mod);
10066: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10067: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10068:
10069: /* d:monomial */
10070: ND mdptond(DP d)
10071: {
10072: NM m;
10073: ND r;
10074:
10075: if ( OID(d) == 1 )
10076: r = ptond(CO,CO,(P)d);
10077: else {
10078: NEWNM(m);
10079: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10080: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10081: NEXT(m) = 0;
10082: MKND(NV(d),m,1,r);
10083: }
10084: return r;
10085: }
10086:
10087: ND nd_mul_nm(int mod,NM m0,ND p)
10088: {
10089: UINT *d0;
10090: int c0,c1,c;
10091: NM tm,mr,mr0;
10092: ND r;
10093:
10094: if ( !p ) return 0;
10095: d0 = DL(m0);
10096: c0 = CM(m0);
10097: mr0 = 0;
10098: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10099: NEXTNM(mr0,mr);
10100: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10101: ndl_add(d0,DL(tm),DL(mr));
10102: }
10103: NEXT(mr) = 0;
10104: MKND(NV(p),mr0,LEN(p),r);
10105: return r;
10106: }
10107:
10108: ND nd_mul_nm_lf(NM m0,ND p)
10109: {
10110: UINT *d0;
10111: Z c0,c1,c;
10112: NM tm,mr,mr0;
10113: ND r;
10114:
10115: if ( !p ) return 0;
10116: d0 = DL(m0);
10117: c0 = CZ(m0);
10118: mr0 = 0;
10119: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10120: NEXTNM(mr0,mr);
10121: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10122: ndl_add(d0,DL(tm),DL(mr));
10123: }
10124: NEXT(mr) = 0;
10125: MKND(NV(p),mr0,LEN(p),r);
10126: return r;
10127: }
10128:
10129: ND *btog(NODE ti,ND **p,int nb,int mod)
10130: {
10131: PGeoBucket *r;
10132: int i,ci;
10133: NODE t,s;
10134: ND m,tp;
10135: ND *pi,*rd;
10136: P c;
10137:
10138: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10139: for ( i = 0; i < nb; i++ )
10140: r[i] = create_pbucket();
10141: for ( t = ti; t; t = NEXT(t) ) {
10142: s = BDY((LIST)BDY(t));
10143: if ( ARG0(s) ) {
10144: m = mdptond((DP)ARG2(s));
1.6 noro 10145: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10146: if ( (ci = ((MQ)c)->cont) != 0 ) {
10147: HCM(m) = ci;
1.6 noro 10148: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10149: for ( i = 0; i < nb; i++ ) {
10150: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10151: add_pbucket(mod,r[i],tp);
10152: }
10153: }
10154: ci = 1;
10155: } else {
10156: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10157: ci = invm(ci,mod);
10158: }
10159: }
10160: rd = (ND *)MALLOC(nb*sizeof(ND));
10161: for ( i = 0; i < nb; i++ )
10162: rd[i] = normalize_pbucket(mod,r[i]);
10163: if ( ci != 1 )
10164: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10165: return rd;
10166: }
10167:
10168: /* YYY */
10169: ND *btog_lf(NODE ti,ND **p,int nb)
10170: {
10171: PGeoBucket *r;
10172: int i;
10173: NODE t,s;
10174: ND m,tp;
10175: ND *pi,*rd;
10176: LM lm;
10177: Z lf,c;
10178:
10179: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10180: for ( i = 0; i < nb; i++ )
10181: r[i] = create_pbucket();
10182: for ( t = ti; t; t = NEXT(t) ) {
10183: s = BDY((LIST)BDY(t));
10184: if ( ARG0(s) ) {
10185: m = mdptond((DP)ARG2(s));
1.6 noro 10186: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10187: if ( lm ) {
10188: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10189: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10190: for ( i = 0; i < nb; i++ ) {
10191: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10192: add_pbucket(-2,r[i],tp);
10193: }
10194: }
10195: c = ONE;
10196: } else {
10197: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10198: }
10199: }
10200: rd = (ND *)MALLOC(nb*sizeof(ND));
10201: for ( i = 0; i < nb; i++ )
10202: rd[i] = normalize_pbucket(-2,r[i]);
10203: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10204: return rd;
10205: }
10206:
10207: ND btog_one(NODE ti,ND *p,int nb,int mod)
10208: {
10209: PGeoBucket r;
10210: int i,ci,j;
10211: NODE t,s;
10212: ND m,tp;
10213: ND pi,rd;
10214: P c;
10215:
10216: r = create_pbucket();
10217: for ( t = ti; t; t = NEXT(t) ) {
10218: s = BDY((LIST)BDY(t));
10219: if ( ARG0(s) ) {
10220: m = mdptond((DP)ARG2(s));
1.6 noro 10221: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10222: if ( (ci = ((MQ)c)->cont) != 0 ) {
10223: HCM(m) = ci;
1.6 noro 10224: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10225: if ( !pi ) {
10226: pi = nd_load_mod(j);
10227: tp = nd_mul_nm(mod,BDY(m),pi);
10228: nd_free(pi);
10229: add_pbucket(mod,r,tp);
10230: } else {
10231: tp = nd_mul_nm(mod,BDY(m),pi);
10232: add_pbucket(mod,r,tp);
10233: }
10234: }
10235: ci = 1;
10236: } else {
10237: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10238: ci = invm(ci,mod);
10239: }
10240: }
10241: rd = normalize_pbucket(mod,r);
10242: free_pbucket(r);
10243: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10244: return rd;
10245: }
10246:
10247: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10248:
10249: MAT nd_btog(LIST f,LIST v,int mod,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: P inv;
10258: MAT mat;
10259:
10260: if ( mod == -2 )
10261: return nd_btog_lf(f,v,ord,tlist,rp);
10262:
10263: parse_nd_option(current_option);
10264: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10265: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10266: switch ( ord->id ) {
10267: case 1:
10268: if ( ord->nv != nvar )
10269: error("nd_check : invalid order specification");
10270: break;
10271: default:
10272: break;
10273: }
10274: nd_init_ord(ord);
10275: #if 0
1.6 noro 10276: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10277: #else
10278: nd_bpe = 32;
10279: #endif
10280: nd_setup_parameters(nvar,0);
10281: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10282: intred = BDY((LIST)ARG3(BDY(tlist)));
10283: ind = BDY((LIST)ARG4(BDY(tlist)));
10284: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10285: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10286: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10287: if ( j > i ) i = j;
10288: }
10289: n = i+1;
10290: nb = length(BDY(f));
10291: p = (ND **)MALLOC(n*sizeof(ND *));
10292: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10293: pi = BDY((LIST)BDY(t));
1.6 noro 10294: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10295: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10296: ptomp(mod,(P)ARG2(pi),&inv);
10297: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10298: u = ptond(CO,vv,(P)ONE);
10299: HCM(u) = ((MQ)inv)->cont;
10300: c[pi1] = u;
10301: }
10302: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10303: printf("%d ",i); fflush(stdout);
10304: ti = BDY((LIST)BDY(t));
1.6 noro 10305: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10306: }
10307: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10308: printf("%d ",i); fflush(stdout);
10309: ti = BDY((LIST)BDY(t));
1.6 noro 10310: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10311: }
10312: m = length(ind);
10313: MKMAT(mat,nb,m);
10314: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10315: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10316: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10317: return mat;
10318: }
10319:
10320: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10321: {
10322: int i,j,n,m,nb,pi0,pi1,nvar;
10323: VL fv,tv,vv;
10324: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10325: ND **p;
10326: ND *c;
10327: ND u;
10328: MAT mat;
10329: LM lm;
10330: Z lf,inv;
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: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10365: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
10366: u = ptond(CO,vv,(P)ONE);
10367: HCZ(u) = inv;
10368: c[pi1] = u;
10369: }
10370: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10371: printf("%d ",i); fflush(stdout);
10372: ti = BDY((LIST)BDY(t));
1.6 noro 10373: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10374: }
10375: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10376: printf("%d ",i); fflush(stdout);
10377: ti = BDY((LIST)BDY(t));
1.6 noro 10378: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10379: }
10380: m = length(ind);
10381: MKMAT(mat,nb,m);
10382: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10383: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10384: BDY(mat)[i][j] = ndtodp(-2,c[i]);
10385: return mat;
10386: }
10387:
10388: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
10389: LIST tlist,int pos,MAT *rp)
10390: {
10391: int i,j,n,m,nb,pi0,pi1,nvar;
10392: VL fv,tv,vv;
10393: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10394: ND *p;
10395: ND *c;
10396: ND u;
10397: P inv;
10398: VECT vect;
10399:
10400: if ( mod == -2 )
10401: error("nd_btog_one : not implemented yet for a large finite field");
10402:
10403: parse_nd_option(current_option);
10404: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10405: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10406: switch ( ord->id ) {
10407: case 1:
10408: if ( ord->nv != nvar )
10409: error("nd_check : invalid order specification");
10410: break;
10411: default:
10412: break;
10413: }
10414: nd_init_ord(ord);
10415: #if 0
1.6 noro 10416: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10417: #else
10418: nd_bpe = 32;
10419: #endif
10420: nd_setup_parameters(nvar,0);
10421: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10422: intred = BDY((LIST)ARG3(BDY(tlist)));
10423: ind = BDY((LIST)ARG4(BDY(tlist)));
10424: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10425: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10426: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10427: if ( j > i ) i = j;
10428: }
10429: n = i+1;
10430: nb = length(BDY(f));
10431: p = (ND *)MALLOC(n*sizeof(ND *));
10432: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10433: pi = BDY((LIST)BDY(t));
1.6 noro 10434: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10435: if ( pi1 == pos ) {
10436: ptomp(mod,(P)ARG2(pi),&inv);
10437: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10438: u = ptond(CO,vv,(P)ONE);
10439: HCM(u) = ((MQ)inv)->cont;
10440: p[pi0] = u;
10441: }
10442: }
10443: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10444: printf("%d ",i); fflush(stdout);
10445: ti = BDY((LIST)BDY(t));
1.6 noro 10446: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10447: if ( Demand ) {
10448: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10449: }
10450: }
10451: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10452: printf("%d ",i); fflush(stdout);
10453: ti = BDY((LIST)BDY(t));
1.6 noro 10454: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10455: if ( Demand ) {
10456: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10457: }
10458: }
10459: m = length(ind);
10460: MKVECT(vect,m);
10461: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10462: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10463: if ( !u ) {
1.6 noro 10464: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10465: BDY(vect)[j] = ndtodp(mod,u);
10466: nd_free(u);
10467: } else
10468: BDY(vect)[j] = ndtodp(mod,u);
10469: }
10470: return vect;
10471: }
10472:
10473: void ndv_print_lf(NDV p)
10474: {
10475: NMV m;
10476: int i,len;
10477:
10478: if ( !p ) printf("0\n");
10479: else {
10480: len = LEN(p);
10481: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10482: printf("+");
10483: mpz_out_str(asir_out,10,BDY(CZ(m)));
10484: printf("*");
10485: ndl_print(DL(m));
10486: }
10487: printf("\n");
10488: }
10489: }
10490:
10491: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10492: {
10493: VL tv,fv,vv,vc,av;
10494: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10495: int m,nocheck,nvar,mindex,e,max;
10496: NDV c;
10497: NMV a;
10498: P p,zp;
10499: Q dmy;
10500: EPOS oepos;
10501: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10502: Alg alpha,dp;
10503: P poly;
10504: LIST f1,f2,zpl;
10505: Obj obj;
10506: NumberField nf;
10507: struct order_spec *ord1;
10508: struct oEGT eg_check,eg0,eg1;
10509: NODE tr,tl1,tl2,tl3,tl4;
10510: LIST l1,l2,l3,l4,l5;
10511: int *perm;
10512: int j,ret;
10513: NODE retn;
10514: Q jq,bpe;
10515:
10516: nd_module = 0;
10517: parse_nd_option(current_option);
10518: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10519: if ( nd_vc )
10520: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10521: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10522: switch ( ord->id ) {
10523: case 1:
10524: if ( ord->nv != nvar )
10525: error("nd_f4_lf_trace : invalid order specification");
10526: break;
10527: default:
10528: break;
10529: }
10530:
10531: nd_ntrans = nvar;
10532: nd_nalg = 0;
10533:
10534: nocheck = 0;
10535: mindex = 0;
10536:
10537: /* do not use on-demand load/save */
10538: nd_demand = 0;
10539: m = trace > 1 ? trace : get_lprime(mindex);
10540: nd_init_ord(ord);
10541: mrank = 0;
10542: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10543: for ( tv = vv; tv; tv = NEXT(tv) ) {
10544: if ( nd_module ) {
10545: s = BDY((LIST)BDY(t));
10546: trank = length(s);
10547: mrank = MAX(mrank,trank);
10548: for ( ; s; s = NEXT(s) ) {
10549: e = getdeg(tv->v,(P)BDY(s));
10550: max = MAX(e,max);
10551: }
10552: } else {
10553: e = getdeg(tv->v,(P)BDY(t));
10554: max = MAX(e,max);
10555: }
10556: }
10557: nd_setup_parameters(nvar,max);
10558: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10559: ishomo = 1;
10560: /* XXX */
10561: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10562: if ( nd_module ) {
10563: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10564: } else {
10565: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10566: }
10567: if ( ishomo )
10568: ishomo = ishomo && ndv_ishomo(c);
10569: if ( c ) {
10570: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10571: ndv_mod(-2,c);
10572: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10573: }
10574: }
10575: if ( in0 ) NEXT(in) = 0;
10576: if ( fd0 ) NEXT(fd) = 0;
10577: if ( !ishomo && homo ) {
10578: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10579: c = (NDV)BDY(t); len = LEN(c);
10580: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10581: wmax = MAX(TD(DL(a)),wmax);
10582: }
10583: homogenize_order(ord,nvar,&ord1);
10584: nd_init_ord(ord1);
10585: nd_setup_parameters(nvar+1,wmax);
10586: for ( t = fd0; t; t = NEXT(t) )
10587: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10588: }
10589: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10590: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10591: if ( ret )
10592: cand = nd_f4_lf_trace_main(m,&perm);
10593: if ( !ret || !cand ) {
10594: *rp = 0; return;
10595: }
10596: if ( !ishomo && homo ) {
10597: /* dehomogenization */
10598: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10599: nd_init_ord(ord);
10600: nd_setup_parameters(nvar,0);
10601: }
10602: cand = ndv_reducebase(cand,perm);
10603: cand = ndv_reduceall(-2,cand);
10604: cbpe = nd_bpe;
10605: get_eg(&eg0);
10606: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10607: /* gbcheck : cand is a GB of Id(cand) ? */
10608: retn = nd_f4(-2,0,0);
10609: }
10610: if ( !retn ) {
10611: /* failure */
10612: *rp = 0; return;
10613: }
10614: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10615: if ( DP_Print )
1.5 noro 10616: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10617: /* dp->p */
10618: nd_bpe = cbpe;
10619: nd_setup_parameters(nd_nvar,0);
10620: for ( r = cand; r; r = NEXT(r) ) {
10621: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10622: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10623: }
10624: MKLIST(*rp,cand);
10625: }
10626:
10627: NODE nd_f4_lf_trace_main(int m,int **indp)
10628: {
10629: int i,nh,stat,index;
10630: NODE r,rm,g;
10631: ND_pairs d,l,l0,t;
10632: ND spol,red;
10633: NDV nf,redv,nfqv,nfv;
10634: NM s0,s;
10635: NODE rp0,srp0,nflist,nflist_lf;
10636: int nsp,nred,col,rank,len,k,j,a;
10637: UINT c;
10638: UINT **spmat;
10639: UINT *s0vect,*svect,*p,*v;
10640: int *colstat;
10641: IndArray *imat;
10642: int *rhead;
10643: int spcol,sprow;
10644: int sugar;
10645: PGeoBucket bucket;
10646: struct oEGT eg0,eg1,eg_f4;
10647:
10648: g = 0; d = 0;
10649: for ( i = 0; i < nd_psn; i++ ) {
10650: d = update_pairs(d,g,i,0);
10651: g = update_base(g,i);
10652: }
10653: while ( d ) {
10654: get_eg(&eg0);
10655: l = nd_minsugarp(d,&d);
10656: sugar = SG(l);
10657: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10658: bucket = create_pbucket();
10659: stat = nd_sp_f4(m,0,l,bucket);
10660: if ( !stat ) {
10661: for ( t = l; NEXT(t); t = NEXT(t) );
10662: NEXT(t) = d; d = l;
10663: d = nd_reconstruct(1,d);
10664: continue;
10665: }
10666: if ( bucket->m < 0 ) continue;
10667: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10668: if ( !col ) {
10669: for ( t = l; NEXT(t); t = NEXT(t) );
10670: NEXT(t) = d; d = l;
10671: d = nd_reconstruct(1,d);
10672: continue;
10673: }
10674: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10675: if ( DP_Print )
1.5 noro 10676: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10677: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10678: if ( !l0 ) continue;
10679: l = l0;
10680:
10681: /* over LF */
10682: bucket = create_pbucket();
10683: stat = nd_sp_f4(-2,1,l,bucket);
10684: if ( !stat ) {
10685: for ( t = l; NEXT(t); t = NEXT(t) );
10686: NEXT(t) = d; d = l;
10687: d = nd_reconstruct(1,d);
10688: continue;
10689: }
10690: if ( bucket->m < 0 ) continue;
10691: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10692: if ( !col ) {
10693: for ( t = l; NEXT(t); t = NEXT(t) );
10694: NEXT(t) = d; d = l;
10695: d = nd_reconstruct(1,d);
10696: continue;
10697: }
10698: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10699: /* adding new bases */
10700: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10701: nfv = (NDV)BDY(rm);
10702: nfqv = (NDV)BDY(r);
10703: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10704: ndv_removecont(m,nfv);
10705: ndv_removecont(-2,nfqv);
1.24 noro 10706: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10707: d = update_pairs(d,g,nh,0);
10708: g = update_base(g,nh);
10709: }
10710: if ( r || rm ) return 0;
10711: }
10712: conv_ilist(nd_demand,1,g,indp);
10713: return g;
10714: }
10715:
1.7 noro 10716: #if SIZEOF_LONG==8
10717:
10718: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10719: {
10720: int j,k,len;
10721: UINT *p;
10722: UINT c;
10723: NDV r;
10724: NMV mr0,mr;
10725:
10726: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10727: if ( !len ) return 0;
10728: else {
10729: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10730: #if 0
10731: ndv_alloc += nmv_adv*len;
10732: #endif
10733: mr = mr0;
10734: p = s0vect;
10735: for ( j = k = 0; j < col; j++, p += nd_wpd )
10736: if ( !rhead[j] ) {
10737: if ( (c = (UINT)vect[k++]) != 0 ) {
10738: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10739: }
10740: }
10741: MKNDV(nd_nvar,mr0,len,r);
10742: return r;
10743: }
10744: }
10745:
1.28 noro 10746: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
10747: {
10748: int j,k,len;
10749: UINT *p;
10750: UINT c;
10751: NDV r;
10752: NMV mr0,mr;
10753:
10754: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
10755: if ( !len ) return 0;
10756: else {
10757: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10758: mr = mr0;
10759: p = s0vect;
10760: for ( j = k = 0; j < col; j++, p += nd_wpd )
10761: if ( (c = (UINT)vect[k++]) != 0 ) {
10762: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10763: }
10764: MKNDV(nd_nvar,mr0,len,r);
10765: return r;
10766: }
10767: }
10768:
1.7 noro 10769: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10770: {
10771: NM m;
1.11 noro 10772: UINT *t,*s,*u;
10773: int i,st,ed,md,prev,c;
1.7 noro 10774:
10775: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10776: prev = 0;
10777: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10778: t = DL(m);
10779: st = prev;
10780: ed = n;
10781: while ( ed > st ) {
10782: md = (st+ed)/2;
10783: u = s0+md*nd_wpd;
10784: c = DL_COMPARE(u,t);
10785: if ( c == 0 ) break;
10786: else if ( c > 0 ) st = md;
10787: else ed = md;
10788: }
10789: r[md] = (mp_limb_t)CM(m);
10790: prev = md;
1.7 noro 10791: }
10792: for ( i = 0; !r[i]; i++ );
10793: return i;
10794: }
10795:
10796: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10797:
1.28 noro 10798: 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 10799: {
10800: int i,j,k,len,pos,prev;
10801: mp_limb_t a,c,c1,c2;
10802: IndArray ivect;
10803: unsigned char *ivc;
10804: unsigned short *ivs;
10805: unsigned int *ivi;
10806: NDV redv;
10807: NMV mr;
10808: NODE rp;
10809: int maxrs;
10810:
10811: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10812: maxrs = 0;
10813: for ( i = 0; i < nred; i++ ) {
10814: ivect = imat[i];
10815: k = ivect->head;
10816: a = svect[k]; c = cvect[k];
10817: MOD128(a,c,m);
10818: svect[k] = a; cvect[k] = 0;
1.28 noro 10819: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 10820: Nf4_red++;
1.7 noro 10821: maxrs = MAX(maxrs,rp0[i]->sugar);
10822: c = m-c; redv = nd_ps[rp0[i]->index];
10823: len = LEN(redv); mr = BDY(redv);
10824: svect[k] = 0; prev = k;
10825: switch ( ivect->width ) {
10826: case 1:
10827: ivc = ivect->index.c;
10828: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10829: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10830: c2 = svect[pos]+c1*c;
10831: if ( c2 < svect[pos] ) cvect[pos]++;
10832: svect[pos] = c2;
1.7 noro 10833: }
10834: break;
10835: case 2:
10836: ivs = ivect->index.s;
10837: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10838: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10839: c2 = svect[pos]+c1*c;
10840: if ( c2 < svect[pos] ) cvect[pos]++;
10841: svect[pos] = c2;
1.7 noro 10842: }
10843: break;
10844: case 4:
10845: ivi = ivect->index.i;
10846: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10847: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10848: c2 = svect[pos]+c1*c;
10849: if ( c2 < svect[pos] ) cvect[pos]++;
10850: svect[pos] = c2;
1.7 noro 10851: }
10852: break;
10853: }
10854: }
10855: }
10856: for ( i = 0; i < col; i++ ) {
10857: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10858: }
10859: return maxrs;
10860: }
10861:
10862: /* for Fp, 2^15=<p<2^29 */
10863:
10864: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10865: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10866: {
10867: int spcol,sprow,a;
10868: int i,j,k,l,rank;
10869: NODE r0,r;
10870: ND_pairs sp;
10871: ND spol;
10872: mp_limb_t **spmat;
10873: mp_limb_t *svect,*cvect;
10874: mp_limb_t *v;
10875: int *colstat;
10876: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10877: int maxrs;
10878: int *spsugar;
10879: ND_pairs *spactive;
10880:
10881: spcol = col-nred;
10882: get_eg(&eg0);
10883: /* elimination (1st step) */
10884: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10885: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10886: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10887: spsugar = (int *)MALLOC(nsp*sizeof(int));
10888: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10889: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10890: nd_sp(m,0,sp,&spol);
10891: if ( !spol ) continue;
10892: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 10893: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 10894: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10895: if ( i < col ) {
10896: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10897: for ( j = k = 0; j < col; j++ )
10898: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10899: spsugar[sprow] = MAX(maxrs,SG(spol));
10900: if ( nz )
10901: spactive[sprow] = sp;
10902: sprow++;
10903: }
10904: nd_free(spol);
10905: }
1.12 noro 10906: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10907: if ( DP_Print ) {
10908: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10909: fflush(asir_out);
10910: }
10911: /* free index arrays */
10912: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10913:
10914: /* elimination (2nd step) */
10915: colstat = (int *)MALLOC(spcol*sizeof(int));
10916: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10917: r0 = 0;
10918: for ( i = 0; i < rank; i++ ) {
10919: NEXTNODE(r0,r); BDY(r) =
10920: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10921: SG((NDV)BDY(r)) = spsugar[i];
10922: GCFREE(spmat[i]);
10923: }
10924: if ( r0 ) NEXT(r) = 0;
10925:
10926: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10927: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10928: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10929: if ( DP_Print ) {
10930: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10931: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10932: nsp,nred,sprow,spcol,rank);
10933: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10934: }
10935: if ( nz ) {
10936: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10937: if ( rank > 0 ) {
10938: NEXT(spactive[rank-1]) = 0;
10939: *nz = spactive[0];
10940: } else
10941: *nz = 0;
10942: }
10943: return r0;
10944: }
10945:
10946: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10947: {
10948: int i,j,k,l,rank,s;
10949: mp_limb_t inv;
10950: mp_limb_t a;
10951: UINT c;
10952: mp_limb_t *t,*pivot,*pk;
10953: UINT *ck;
10954: UINT **cmat;
10955: UINT *ct;
10956: ND_pairs pair;
10957:
10958: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10959: for ( i = 0; i < row; i++ ) {
10960: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10961: bzero(cmat[i],col*sizeof(UINT));
10962: }
10963:
10964: for ( rank = 0, j = 0; j < col; j++ ) {
10965: for ( i = rank; i < row; i++ ) {
10966: a = mat[i][j]; c = cmat[i][j];
10967: MOD128(a,c,md);
10968: mat[i][j] = a; cmat[i][j] = 0;
10969: }
10970: for ( i = rank; i < row; i++ )
10971: if ( mat[i][j] )
10972: break;
10973: if ( i == row ) {
10974: colstat[j] = 0;
10975: continue;
10976: } else
10977: colstat[j] = 1;
10978: if ( i != rank ) {
10979: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10980: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10981: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10982: if ( spactive ) {
10983: pair = spactive[i]; spactive[i] = spactive[rank];
10984: spactive[rank] = pair;
10985: }
10986: }
10987: /* column j is normalized */
10988: s = sugar[rank];
10989: inv = invm((UINT)mat[rank][j],md);
10990: /* normalize pivot row */
10991: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10992: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10993: }
10994: for ( i = rank+1; i < row; i++ ) {
10995: if ( (a = mat[i][j]) != 0 ) {
10996: sugar[i] = MAX(sugar[i],s);
10997: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10998: Nf4_red++;
1.7 noro 10999: }
11000: }
11001: rank++;
11002: }
11003: for ( j = col-1, l = rank-1; j >= 0; j-- )
11004: if ( colstat[j] ) {
11005: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
11006: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
11007: }
11008: s = sugar[l];
11009: for ( i = 0; i < l; i++ ) {
11010: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
11011: if ( a ) {
11012: sugar[i] = MAX(sugar[i],s);
11013: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 11014: Nf4_red++;
1.7 noro 11015: }
11016: }
11017: l--;
11018: }
11019: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11020: GCFREE(cmat);
11021: return rank;
11022: }
11023:
1.28 noro 11024: 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)
11025: {
11026: int i,j,k,l,rank,s,imin;
11027: mp_limb_t inv;
11028: mp_limb_t a;
11029: UINT c;
11030: mp_limb_t *t,*pivot,*pk;
11031: UINT *ck;
11032: UINT **cmat;
11033: UINT *ct;
11034: ND_pairs pair;
11035: SIG sg;
1.31 noro 11036: int *used;
1.28 noro 11037:
1.31 noro 11038: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 11039: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11040: for ( i = 0; i < row; i++ ) {
11041: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11042: bzero(cmat[i],col*sizeof(UINT));
11043: }
11044:
1.31 noro 11045: for ( j = 0; j < col; j++ ) {
11046: for ( i = 0; i < row; i++ ) {
1.28 noro 11047: a = mat[i][j]; c = cmat[i][j];
11048: MOD128(a,c,md);
11049: mat[i][j] = a; cmat[i][j] = 0;
11050: }
1.31 noro 11051: for ( i = 0; i < row; i++ )
11052: if ( !used[i] && mat[i][j] ) break;
11053: if ( i == row ) {
1.28 noro 11054: colstat[j] = 0;
11055: continue;
1.31 noro 11056: } else {
1.28 noro 11057: colstat[j] = 1;
1.31 noro 11058: used[i] = 1;
1.28 noro 11059: }
11060: /* column j is normalized */
1.31 noro 11061: s = sugar[i];
11062: inv = invm((UINT)mat[i][j],md);
1.28 noro 11063: /* normalize pivot row */
1.31 noro 11064: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 11065: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11066: }
1.31 noro 11067: for ( k = i+1; k < row; k++ ) {
11068: if ( (a = mat[k][j]) != 0 ) {
11069: sugar[k] = MAX(sugar[k],s);
11070: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11071: Nf4_red++;
11072: }
11073: }
11074: }
1.31 noro 11075: rank = 0;
11076: for ( i = 0; i < row; i++ ) {
11077: for ( j = 0; j < col; j++ )
11078: if ( mat[i][j] ) break;
11079: if ( j == col ) sugar[i] = -1;
11080: else rank++;
11081: }
1.28 noro 11082: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11083: GCFREE(cmat);
11084: return rank;
11085: }
11086:
11087: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11088: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11089: {
11090: int spcol,sprow,a;
11091: int i,j,k,l,rank;
11092: NODE r0,r;
11093: ND_pairs sp;
11094: ND spol;
11095: mp_limb_t **spmat;
11096: mp_limb_t *svect,*cvect;
11097: mp_limb_t *v;
11098: int *colstat;
11099: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11100: int maxrs;
11101: int *spsugar;
11102: ND_pairs *spactive;
11103: SIG *spsig;
11104:
11105: get_eg(&eg0);
11106: /* elimination (1st step) */
11107: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11108: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11109: spsugar = (int *)MALLOC(nsp*sizeof(int));
11110: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11111: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11112: nd_sp(m,0,sp,&spol);
11113: if ( !spol ) {
1.29 noro 11114: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11115: continue;
11116: }
11117: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11118: nd_to_vect64(m,s0vect,col,spol,svect);
11119: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11120: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11121: if ( i < col ) {
11122: spmat[sprow] = svect;
11123: spsugar[sprow] = MAX(maxrs,SG(spol));
11124: spsig[sprow] = sp->sig;
11125: sprow++;
11126: } else {
1.29 noro 11127: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11128: }
11129: nd_free(spol);
11130: }
11131: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11132: if ( DP_Print ) {
11133: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11134: fflush(asir_out);
11135: }
11136: /* free index arrays */
11137: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11138:
11139: /* elimination (2nd step) */
11140: colstat = (int *)MALLOC(col*sizeof(int));
11141: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11142: r0 = 0;
1.31 noro 11143: for ( i = 0; i < sprow; i++ ) {
11144: if ( spsugar[i] >= 0 ) {
11145: NEXTNODE(r0,r);
11146: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11147: SG((NDV)BDY(r)) = spsugar[i];
11148: ((NDV)BDY(r))->sig = spsig[i];
11149: } else
11150: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11151: GCFREE(spmat[i]);
11152: }
11153: if ( r0 ) NEXT(r) = 0;
11154: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11155: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11156: if ( DP_Print ) {
11157: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11158: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11159: nsp,nred,sprow,col,rank);
11160: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11161: }
11162: return r0;
11163: }
1.40 noro 11164: #endif
1.28 noro 11165:
11166: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11167: {
11168: IndArray *imat;
11169: int nsp,nred,i,start;
11170: int *rhead;
11171: NODE r0,rp;
11172: ND_pairs sp;
11173: NM_ind_pair *rvect;
11174: UINT *s;
11175: int *s0hash;
11176: struct oEGT eg0,eg1,eg_conv;
11177:
11178: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11179: nred = length(rp0);
11180: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11181: rhead = (int *)MALLOC(col*sizeof(int));
11182: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11183:
11184: /* construction of index arrays */
11185: get_eg(&eg0);
11186: if ( DP_Print ) {
11187: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11188: fflush(asir_out);
11189: }
11190: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11191: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11192: rvect[i] = (NM_ind_pair)BDY(rp);
11193: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11194: rhead[imat[i]->head] = 1;
11195: start = imat[i]->head;
11196: }
11197: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11198: if ( DP_Print ) {
11199: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11200: fflush(asir_out);
11201: }
11202: if ( m > 0 )
1.40 noro 11203: #if SIZEOF_LONG==8
1.28 noro 11204: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11205: #else
11206: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11207: #endif
1.28 noro 11208: else
11209: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11210: error("nd_f4_red_q_main_s : not implemented yet");
11211: return r0;
11212: }
11213:
11214: INLINE int ndl_find_reducer_minsig(UINT *dg)
11215: {
11216: RHist r;
11217: int i,singular,ret,d,k,imin;
11218: SIG t;
11219: static int wpd,nvar;
11220: static SIG quo,quomin;
11221: static UINT *tmp;
11222:
11223: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11224: if ( wpd != nd_wpd ) {
11225: wpd = nd_wpd;
11226: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11227: }
11228: #if 0
11229: d = ndl_hash_value(dg);
11230: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11231: if ( ndl_equal(dg,DL(r)) ) {
11232: return r->index;
11233: }
11234: }
11235: #endif
11236: imin = -1;
11237: for ( i = 0; i < nd_psn; i++ ) {
11238: r = nd_psh[i];
11239: if ( ndl_reducible(dg,DL(r)) ) {
11240: ndl_sub(dg,DL(r),tmp);
11241: _ndltodl(tmp,DL(quo));
11242: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11243: quo->pos = nd_psh[i]->sig->pos;
11244: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11245: t = quo; quo = quomin; quomin = t;
11246: imin = i;
11247: }
11248: }
11249: }
11250: if ( imin == -1 ) return nd_psn;
11251: else {
11252: #if 0
11253: nd_append_red(dg,i);
11254: #endif
11255: return imin;
11256: }
11257: }
11258:
11259: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11260: {
11261: NODE rp0,rp;
11262: NM mul,head,s0,s;
11263: int index,col,i,sugar;
11264: RHist h;
11265: UINT *s0v,*p;
11266: NM_ind_pair pair;
11267: ND red;
11268: NDV *ps;
11269: SIG sig;
11270:
11271: s0 = 0; rp0 = 0; col = 0;
11272: if ( nd_demand )
11273: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11274: else
11275: ps = trace?nd_ps_trace:nd_ps;
11276: while ( 1 ) {
11277: head = remove_head_pbucket_symbolic(bucket);
11278: if ( !head ) break;
11279: if ( !s0 ) s0 = head;
11280: else NEXT(s) = head;
11281: s = head;
11282: index = ndl_find_reducer_minsig(DL(head));
11283: if ( index >= 0 && index < nd_psn ) {
11284: h = nd_psh[index];
11285: NEWNM(mul);
11286: ndl_sub(DL(head),DL(h),DL(mul));
11287: if ( ndl_check_bound2(index,DL(mul)) )
11288: return 0;
11289: sugar = TD(DL(mul))+SG(ps[index]);
11290: NEWSIG(sig);
11291: _ndltodl(DL(mul),DL(sig));
11292: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11293: sig->pos = nd_psh[index]->sig->pos;
11294: MKNM_ind_pair(pair,mul,index,sugar,sig);
11295: red = ndv_mul_nm_symbolic(mul,ps[index]);
11296: add_pbucket_symbolic(bucket,nd_remove_head(red));
11297: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11298: }
11299: col++;
11300: }
11301: if ( rp0 ) NEXT(rp) = 0;
11302: NEXT(s) = 0;
11303: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11304: for ( i = 0, p = s0v, s = s0; i < col;
11305: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11306: *s0vect = s0v;
11307: *r = rp0;
11308:
11309: return col;
11310: }
11311:
11312: NODE nd_sba_f4(int m,int **indp)
11313: {
1.34 noro 11314: int i,nh,stat,index,f4red,f4step;
1.33 noro 11315: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11316: NODE r,g,rp0,nflist;
1.41 noro 11317: ND_pairs d,l,t,l1;
1.33 noro 11318: ND h,nf;
11319: NDV nfv;
11320: union oNDC hc;
11321: UINT *s0vect;
1.28 noro 11322: UINT c;
11323: PGeoBucket bucket;
1.33 noro 11324: NODE *syzlist;
11325: SIG sig;
1.28 noro 11326: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11327: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11328:
11329: Nf4_red=0;
1.30 noro 11330: d = 0;
1.29 noro 11331: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11332: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11333: d = update_pairs_s(d,i,syzlist);
1.28 noro 11334: }
1.30 noro 11335: nd_nbase = nd_psn;
1.28 noro 11336: f4red = 1;
1.33 noro 11337: psugar = 0;
1.34 noro 11338: f4step = 0;
1.28 noro 11339: while ( d ) {
1.33 noro 11340: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11341: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11342: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11343: again:
11344: l = d; d = d->next;
1.41 noro 11345: #if 0
1.33 noro 11346: if ( small_lcm(l) ) {
11347: if ( DP_Print ) fprintf(asir_out,"M");
11348: continue;
11349: }
11350: sig = l->sig;
11351: stat = nd_sp(m,0,l,&h);
1.41 noro 11352: #else
11353: l1 = find_smallest_lcm(l);
11354: if ( l1 == 0 ) {
11355: if ( DP_Print ) fprintf(asir_out,"M");
11356: continue;
11357: }
11358: sig = l1->sig;
11359: stat = nd_sp(m,0,l1,&h);
11360: #endif
1.33 noro 11361: if ( !stat ) {
11362: NEXT(l) = d; d = l;
11363: d = nd_reconstruct(0,d);
11364: goto again;
11365: }
11366: get_eg(&eg1);
11367: #if USE_GEOBUCKET
1.39 noro 11368: 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 11369: #else
1.39 noro 11370: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11371: #endif
11372: get_eg(&eg2);
11373: if ( !stat ) {
11374: NEXT(l) = d; d = l;
11375: d = nd_reconstruct(0,d);
11376: goto again;
11377: } else if ( stat == -1 ) {
11378: if ( DP_Print ) { printf("S"); fflush(stdout); }
11379: FREENDP(l);
11380: } else if ( nf ) {
11381: if ( DP_Print ) { printf("+"); fflush(stdout); }
11382: add_eg(&eg_nf,&eg1,&eg2);
11383: hc = HCU(nf);
11384: nd_removecont(m,nf);
11385: nfv = ndtondv(m,nf); nd_free(nf);
11386: nh = ndv_newps(m,nfv,0);
11387:
1.30 noro 11388: d = update_pairs_s(d,nh,syzlist);
11389: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 11390: FREENDP(l);
1.28 noro 11391: } else {
1.33 noro 11392: add_eg(&eg_nfzero,&eg1,&eg2);
11393: // syzygy
11394: get_eg(&eg1);
11395: d = remove_spair_s(d,sig);
11396: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 11397: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 11398: if ( DP_Print ) { printf("."); fflush(stdout); }
11399: FREENDP(l);
11400: }
11401: } else {
1.34 noro 11402: if ( ms != psugar ) f4step = 1;
11403: else f4step++;
1.33 noro 11404: again2:
11405: psugar = ms;
11406: l = nd_minsugarp_s(d,&d);
11407: sugar = nd_sugarweight?d->sugar2:SG(d);
11408: bucket = create_pbucket();
11409: stat = nd_sp_f4(m,0,l,bucket);
11410: if ( !stat ) {
11411: for ( t = l; NEXT(t); t = NEXT(t) );
11412: NEXT(t) = d; d = l;
11413: d = nd_reconstruct(0,d);
11414: goto again2;
11415: }
11416: if ( bucket->m < 0 ) continue;
11417: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
11418: if ( !col ) {
1.37 noro 11419: for ( t = l; NEXT(t); t = NEXT(t) )
11420: ;
11421: NEXT(t) = d; d = l;
1.33 noro 11422: d = nd_reconstruct(0,d);
11423: goto again2;
11424: }
1.34 noro 11425: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 11426: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
11427: /* adding new bases */
11428: for ( r = nflist; r; r = NEXT(r) ) {
11429: nfv = (NDV)BDY(r);
1.38 noro 11430: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 11431: ndv_removecont(m,nfv);
11432: nh = ndv_newps(m,nfv,0);
11433: d = update_pairs_s(d,nh,syzlist);
11434: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
11435: }
11436: for ( i = 0; i < nd_nbase; i++ )
11437: for ( r = syzlist[i]; r; r = NEXT(r) )
11438: d = remove_spair_s(d,(SIG)BDY(r));
11439: d = remove_large_lcm(d);
11440: if ( DP_Print ) {
1.34 noro 11441: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11442: }
11443: f4red++;
1.28 noro 11444: }
11445: }
11446: if ( DP_Print ) {
1.34 noro 11447: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11448: }
1.30 noro 11449: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11450: return g;
11451: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>