Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.52
1.52 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.51 2021/03/09 07:07:02 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.45 noro 5: void print_siglist(NODE l);
6:
1.49 noro 7: NODE nd_hpdata;
1.47 noro 8: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs,Nnewpair;
1.12 noro 9: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 10:
11: int diag_period = 6;
12: int weight_check = 1;
13: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 14: /* for general module order */
1.19 noro 15: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 16: int (*dl_base_compare_function)(int nv,DL a,DL b);
17: int nd_base_ordtype;
1.1 noro 18: int nd_dcomp;
19: int nd_rref2;
20: NM _nm_free_list;
21: ND _nd_free_list;
22: ND_pairs _ndp_free_list;
23: NODE nd_hcf;
1.35 noro 24: int Nsyz,Nsamesig;
1.1 noro 25:
26: Obj nd_top_weight;
27:
28: static NODE nd_subst;
29: static VL nd_vc;
30: static int nd_ntrans;
31: static int nd_nalg;
32: #if 0
33: static int ndv_alloc;
34: #endif
35: #if 1
36: static int nd_f4_nsp=0x7fffffff;
37: #else
38: static int nd_f4_nsp=50;
39: #endif
40: static double nd_scale=2;
41: static UINT **nd_bound;
42: static struct order_spec *nd_ord;
43: static EPOS nd_epos;
44: static BlockMask nd_blockmask;
45: static int nd_nvar;
46: static int nd_isrlex;
47: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
48: static UINT nd_mask[32];
49: static UINT nd_mask0,nd_mask1;
50:
51: static NDV *nd_ps;
52: static NDV *nd_ps_trace;
53: static NDV *nd_ps_sym;
54: static NDV *nd_ps_trace_sym;
55: static RHist *nd_psh;
1.30 noro 56: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 57: static RHist *nd_red;
58: static int *nd_work_vector;
59: static int **nd_matrix;
60: static int nd_matrix_len;
61: static struct weight_or_block *nd_worb;
62: static int nd_worb_len;
63: static int nd_found,nd_create,nd_notfirst;
64: static int nmv_adv;
65: static int nd_demand;
1.21 noro 66: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 67: static int nd_module_rank,nd_poly_weight_len;
68: static int *nd_poly_weight,*nd_module_weight;
69: static NODE nd_tracelist;
70: static NODE nd_alltracelist;
1.41 noro 71: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
72: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.46 noro 73: static int nd_top,nd_sba_syz,nd_sba_inputisgb;
1.1 noro 74: static int *nd_gbblock;
75: static NODE nd_nzlist,nd_check_splist;
76: static int nd_splist;
77: static int *nd_sugarweight;
78: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 79: static DL *nd_sba_hm;
1.29 noro 80: static NODE *nd_sba_pos;
1.1 noro 81:
1.46 noro 82: struct comp_sig_spec {
83: int n;
84: // current_i <-> oldv[i]
85: int *oldv;
86: int *weight;
87: struct order_pair *order_pair;
88: int block_length;
89: int **matrix;
90: int row;
91: int (*cmpdl)(int n,DL d1,DL d2);
92: };
93:
94: struct comp_sig_spec *nd_sba_modord;
95:
1.49 noro 96: DL ndltodl(int n,UINT *ndl);
1.1 noro 97: NumberField get_numberfield();
98: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
99: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
100: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
101: int nd_monic(int m,ND *p);
102: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
103: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
104: NDV pltondv(VL vl,VL dvl,LIST p);
105: void pltozpl(LIST l,Q *cont,LIST *pp);
106: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
107: void nmtodp(int mod,NM m,DP *r);
1.15 noro 108: void ndltodp(UINT *d,DP *r);
1.1 noro 109: NODE reverse_node(NODE n);
110: P ndc_div(int mod,union oNDC a,union oNDC b);
111: P ndctop(int mod,union oNDC c);
112: void finalize_tracelist(int i,P cont);
113: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.46 noro 114: void parse_nd_option(VL vl,NODE opt);
1.1 noro 115: void dltondl(int n,DL dl,UINT *r);
116: DP ndvtodp(int mod,NDV p);
117: DP ndtodp(int mod,ND p);
1.16 noro 118: DPM ndvtodpm(int mod,NDV p);
1.43 noro 119: NDV dptondv(int mod,DP p);
1.16 noro 120: NDV dpmtondv(int mod,DPM p);
1.43 noro 121: int dp_getdeg(DP p);
1.16 noro 122: int dpm_getdeg(DPM p,int *rank);
123: void dpm_ptozp(DPM p,Z *cont,DPM *r);
124: int compdmm(int nv,DMM a,DMM b);
1.44 noro 125: DPM sigtodpm(SIG s);
126: SIG dup_sig(SIG sig);
1.1 noro 127:
128: void Pdp_set_weight(NODE,VECT *);
129: void Pox_cmo_rpc(NODE,Obj *);
130:
131: ND nd_add_lf(ND p1,ND p2);
132: void nd_mul_c_lf(ND p,Z mul);
133: void ndv_mul_c_lf(NDV p,Z mul);
134: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
135: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
136: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
137: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
138: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
139: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
140: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 141: 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 142: NODE nd_f4_lf_trace_main(int m,int **indp);
143: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
144:
145: extern int lf_lazy;
146: extern Z current_mod_lf;
147:
148: extern int Denominator,DP_Multiple,MaxDeg;
149:
150: #define BLEN (8*sizeof(unsigned long))
151:
152: typedef struct matrix {
153: int row,col;
154: unsigned long **a;
155: } *matrix;
156:
157:
158: void nd_free_private_storage()
159: {
160: _nm_free_list = 0;
161: _ndp_free_list = 0;
162: #if 0
163: GC_gcollect();
164: #endif
165: }
166:
167: void _NM_alloc()
168: {
169: NM p;
170: int i;
171:
172: for ( i = 0; i < 1024; i++ ) {
173: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
174: p->next = _nm_free_list; _nm_free_list = p;
175: }
176: }
177:
178: matrix alloc_matrix(int row,int col)
179: {
180: unsigned long **a;
181: int i,len,blen;
182: matrix mat;
183:
184: mat = (matrix)MALLOC(sizeof(struct matrix));
185: mat->row = row;
186: mat->col = col;
187: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
188: return mat;
189: }
190:
191:
192: void _ND_alloc()
193: {
194: ND p;
195: int i;
196:
197: for ( i = 0; i < 1024; i++ ) {
198: p = (ND)MALLOC(sizeof(struct oND));
199: p->body = (NM)_nd_free_list; _nd_free_list = p;
200: }
201: }
202:
203: void _NDP_alloc()
204: {
205: ND_pairs p;
206: int i;
207:
208: for ( i = 0; i < 1024; i++ ) {
209: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
210: +(nd_wpd-1)*sizeof(UINT));
211: p->next = _ndp_free_list; _ndp_free_list = p;
212: }
213: }
214:
215: INLINE int nd_length(ND p)
216: {
217: NM m;
218: int i;
219:
220: if ( !p )
221: return 0;
222: else {
223: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
224: return i;
225: }
226: }
227:
228: extern int dp_negative_weight;
229:
230: INLINE int ndl_reducible(UINT *d1,UINT *d2)
231: {
232: UINT u1,u2;
233: int i,j;
234:
235: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
236:
237: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
238: #if USE_UNROLL
239: switch ( nd_bpe ) {
240: case 3:
241: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
242: u1 = d1[i]; u2 = d2[i];
243: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
244: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
245: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
246: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
247: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
248: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
249: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
250: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
251: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
252: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
253: }
254: return 1;
255: break;
256: case 4:
257: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
258: u1 = d1[i]; u2 = d2[i];
259: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
260: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
261: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
262: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
263: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
264: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
265: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
266: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
267: }
268: return 1;
269: break;
270: case 6:
271: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
272: u1 = d1[i]; u2 = d2[i];
273: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
274: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
275: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
276: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
277: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
278: }
279: return 1;
280: break;
281: case 8:
282: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
283: u1 = d1[i]; u2 = d2[i];
284: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
285: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
286: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
287: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
288: }
289: return 1;
290: break;
291: case 16:
292: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
293: u1 = d1[i]; u2 = d2[i];
294: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
295: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
296: }
297: return 1;
298: break;
299: case 32:
300: for ( i = nd_exporigin; i < nd_wpd; i++ )
301: if ( d1[i] < d2[i] ) return 0;
302: return 1;
303: break;
304: default:
305: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
306: u1 = d1[i]; u2 = d2[i];
307: for ( j = 0; j < nd_epw; j++ )
308: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
309: }
310: return 1;
311: }
312: #else
313: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
314: u1 = d1[i]; u2 = d2[i];
315: for ( j = 0; j < nd_epw; j++ )
316: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
317: }
318: return 1;
319: #endif
320: }
321:
322: /*
323: * If the current order is a block order,
324: * then the last block is length 1 and contains
325: * the homo variable. Otherwise, the original
326: * order is either 0 or 2.
327: */
328:
329: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
330: {
331: int w,i,e,n,omask0;
332:
333: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
334: n = nd_nvar-1;
335: ndl_zero(r);
336: for ( i = 0; i < n; i++ ) {
337: e = GET_EXP_OLD(d,i);
338: PUT_EXP(r,i,e);
339: }
340: w = TD(d);
341: PUT_EXP(r,nd_nvar-1,weight-w);
342: if ( nd_module ) MPOS(r) = d[ompos];
343: TD(r) = weight;
344: if ( nd_blockmask ) ndl_weight_mask(r);
345: }
346:
347: void ndl_dehomogenize(UINT *d)
348: {
349: UINT mask;
350: UINT h;
351: int i,bits;
352:
353: if ( nd_blockmask ) {
354: h = GET_EXP(d,nd_nvar-1);
355: XOR_EXP(d,nd_nvar-1,h);
356: TD(d) -= h;
357: ndl_weight_mask(d);
358: } else {
359: if ( nd_isrlex ) {
360: if ( nd_bpe == 32 ) {
361: h = d[nd_exporigin];
362: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
363: d[i-1] = d[i];
364: d[i-1] = 0;
365: TD(d) -= h;
366: } else {
367: bits = nd_epw*nd_bpe;
368: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
369: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
370: for ( i = nd_exporigin; i < nd_wpd; i++ )
371: d[i] = ((d[i]<<nd_bpe)&mask)
372: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
373: TD(d) -= h;
374: }
375: } else {
376: h = GET_EXP(d,nd_nvar-1);
377: XOR_EXP(d,nd_nvar-1,h);
378: TD(d) -= h;
379: }
380: }
381: }
382:
383: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
384: {
385: UINT t1,t2,u,u1,u2;
386: int i,j,l;
387:
388: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
389: error("ndl_lcm : inconsistent monomials");
390: #if USE_UNROLL
391: switch ( nd_bpe ) {
392: case 3:
393: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
394: u1 = d1[i]; u2 = d2[i];
395: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
396: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
397: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
398: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
400: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
401: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
403: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
404: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
405: d[i] = u;
406: }
407: break;
408: case 4:
409: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
410: u1 = d1[i]; u2 = d2[i];
411: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
412: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
414: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
415: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
416: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
417: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
418: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
419: d[i] = u;
420: }
421: break;
422: case 6:
423: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
424: u1 = d1[i]; u2 = d2[i];
425: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
426: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
427: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
428: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
429: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
430: d[i] = u;
431: }
432: break;
433: case 8:
434: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
435: u1 = d1[i]; u2 = d2[i];
436: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
437: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
438: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
439: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
440: d[i] = u;
441: }
442: break;
443: case 16:
444: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
445: u1 = d1[i]; u2 = d2[i];
446: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
447: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
448: d[i] = u;
449: }
450: break;
451: case 32:
452: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
453: u1 = d1[i]; u2 = d2[i];
454: d[i] = u1>u2?u1:u2;
455: }
456: break;
457: default:
458: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
459: u1 = d1[i]; u2 = d2[i];
460: for ( j = 0, u = 0; j < nd_epw; j++ ) {
461: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
462: }
463: d[i] = u;
464: }
465: break;
466: }
467: #else
468: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
469: u1 = d1[i]; u2 = d2[i];
470: for ( j = 0, u = 0; j < nd_epw; j++ ) {
471: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
472: }
473: d[i] = u;
474: }
475: #endif
476: if ( nd_module ) MPOS(d) = MPOS(d1);
477: TD(d) = ndl_weight(d);
478: if ( nd_blockmask ) ndl_weight_mask(d);
479: }
480:
481: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
482: {
483: UINT t1,t2,u,u1,u2;
484: int i,j,l;
485:
486: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
487: u1 = d1[i]; u2 = d2[i];
488: for ( j = 0, u = 0; j < nd_epw; j++ ) {
489: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
490: }
491: d[i] = u;
492: }
493: }
494:
495: int ndl_weight(UINT *d)
496: {
497: UINT t,u;
498: int i,j;
499:
500: if ( current_dl_weight_vector )
501: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
502: u = GET_EXP(d,i);
503: t += MUL_WEIGHT(u,i);
504: }
505: else
506: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
507: u = d[i];
508: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
509: t += (u&nd_mask0);
510: }
1.20 noro 511: if ( nd_module && nd_module_rank && MPOS(d) )
512: t += nd_module_weight[MPOS(d)-1];
513: for ( i = nd_exporigin; i < nd_wpd; i++ )
514: if ( d[i] && !t )
515: printf("afo\n");
1.1 noro 516: return t;
517: }
518:
519: /* for sugarweight */
520:
521: int ndl_weight2(UINT *d)
522: {
523: int t,u;
524: int i,j;
525:
526: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
527: u = GET_EXP(d,i);
528: t += nd_sugarweight[i]*u;
529: }
1.20 noro 530: if ( nd_module && nd_module_rank && MPOS(d) )
531: t += nd_module_weight[MPOS(d)-1];
1.1 noro 532: return t;
533: }
534:
535: void ndl_weight_mask(UINT *d)
536: {
537: UINT t,u;
538: UINT *mask;
539: int i,j,k,l;
540:
541: l = nd_blockmask->n;
542: for ( k = 0; k < l; k++ ) {
543: mask = nd_blockmask->mask[k];
544: if ( current_dl_weight_vector )
545: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
546: u = GET_EXP_MASK(d,i,mask);
547: t += MUL_WEIGHT(u,i);
548: }
549: else
550: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
551: u = d[i]&mask[i];
552: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
553: t += (u&nd_mask0);
554: }
555: d[k+1] = t;
556: }
557: }
558:
1.21 noro 559: int ndl_glex_compare(UINT *d1,UINT *d2)
560: {
561: if ( TD(d1) > TD(d2) ) return 1;
562: else if ( TD(d1) < TD(d2) ) return -1;
563: else return ndl_lex_compare(d1,d2);
564: }
565:
1.1 noro 566: int ndl_lex_compare(UINT *d1,UINT *d2)
567: {
568: int i;
569:
570: d1 += nd_exporigin;
571: d2 += nd_exporigin;
572: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
573: if ( *d1 > *d2 )
574: return nd_isrlex ? -1 : 1;
575: else if ( *d1 < *d2 )
576: return nd_isrlex ? 1 : -1;
577: return 0;
578: }
579:
580: int ndl_block_compare(UINT *d1,UINT *d2)
581: {
582: int i,l,j,ord_o,ord_l;
583: struct order_pair *op;
584: UINT t1,t2,m;
585: UINT *mask;
586:
587: l = nd_blockmask->n;
588: op = nd_blockmask->order_pair;
589: for ( j = 0; j < l; j++ ) {
590: mask = nd_blockmask->mask[j];
591: ord_o = op[j].order;
592: if ( ord_o < 2 ) {
593: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
594: else if ( t1 < t2 ) return -1;
595: }
596: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
597: m = mask[i];
598: t1 = d1[i]&m;
599: t2 = d2[i]&m;
600: if ( t1 > t2 )
601: return !ord_o ? -1 : 1;
602: else if ( t1 < t2 )
603: return !ord_o ? 1 : -1;
604: }
605: }
606: return 0;
607: }
608:
609: int ndl_matrix_compare(UINT *d1,UINT *d2)
610: {
611: int i,j,s,row;
612: int *v;
613: Z **mat;
614: Z *w;
615: Z t1;
616: Z t,t2;
617:
1.6 noro 618: for ( j = 0; j < nd_nvar; j++ )
619: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 620: if ( nd_top_weight ) {
621: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 622: mat = (Z **)&BDY((VECT)nd_top_weight);
623: row = 1;
1.1 noro 624: } else {
625: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 626: row = ((MAT)nd_top_weight)->row;
1.1 noro 627: }
628: for ( i = 0; i < row; i++ ) {
1.6 noro 629: w = mat[i];
1.1 noro 630: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 631: STOZ(nd_work_vector[j],t1);
1.1 noro 632: mulz(w[j],t1,&t2);
633: addz(t,t2,&t1);
634: t = t1;
635: }
1.6 noro 636: if ( t ) {
637: s = sgnz(t);
1.1 noro 638: if ( s > 0 ) return 1;
639: else if ( s < 0 ) return -1;
1.6 noro 640: }
1.1 noro 641: }
1.6 noro 642: }
643: for ( i = 0; i < nd_matrix_len; i++ ) {
644: v = nd_matrix[i];
645: for ( j = 0, s = 0; j < nd_nvar; j++ )
646: s += v[j]*nd_work_vector[j];
647: if ( s > 0 ) return 1;
648: else if ( s < 0 ) return -1;
649: }
1.1 noro 650: if ( !ndl_equal(d1,d2) )
1.6 noro 651: error("ndl_matrix_compare : invalid matrix");
652: return 0;
1.1 noro 653: }
654:
655: int ndl_composite_compare(UINT *d1,UINT *d2)
656: {
657: int i,j,s,start,end,len,o;
658: int *v;
659: struct sparse_weight *sw;
660:
661: for ( j = 0; j < nd_nvar; j++ )
662: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
663: for ( i = 0; i < nd_worb_len; i++ ) {
664: len = nd_worb[i].length;
665: switch ( nd_worb[i].type ) {
666: case IS_DENSE_WEIGHT:
667: v = nd_worb[i].body.dense_weight;
668: for ( j = 0, s = 0; j < len; j++ )
669: s += v[j]*nd_work_vector[j];
670: if ( s > 0 ) return 1;
671: else if ( s < 0 ) return -1;
672: break;
673: case IS_SPARSE_WEIGHT:
674: sw = nd_worb[i].body.sparse_weight;
675: for ( j = 0, s = 0; j < len; j++ )
676: s += sw[j].value*nd_work_vector[sw[j].pos];
677: if ( s > 0 ) return 1;
678: else if ( s < 0 ) return -1;
679: break;
680: case IS_BLOCK:
681: o = nd_worb[i].body.block.order;
682: start = nd_worb[i].body.block.start;
683: switch ( o ) {
684: case 0:
685: end = start+len;
686: for ( j = start, s = 0; j < end; j++ )
687: s += MUL_WEIGHT(nd_work_vector[j],j);
688: if ( s > 0 ) return 1;
689: else if ( s < 0 ) return -1;
690: for ( j = end-1; j >= start; j-- )
691: if ( nd_work_vector[j] < 0 ) return 1;
692: else if ( nd_work_vector[j] > 0 ) return -1;
693: break;
694: case 1:
695: end = start+len;
696: for ( j = start, s = 0; j < end; j++ )
697: s += MUL_WEIGHT(nd_work_vector[j],j);
698: if ( s > 0 ) return 1;
699: else if ( s < 0 ) return -1;
700: for ( j = start; j < end; j++ )
701: if ( nd_work_vector[j] > 0 ) return 1;
702: else if ( nd_work_vector[j] < 0 ) return -1;
703: break;
704: case 2:
705: end = start+len;
706: for ( j = start; j < end; j++ )
707: if ( nd_work_vector[j] > 0 ) return 1;
708: else if ( nd_work_vector[j] < 0 ) return -1;
709: break;
710: }
711: break;
712: }
713: }
714: return 0;
715: }
716:
717: /* TDH -> WW -> TD-> RL */
718:
719: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
720: {
721: int i,m,e1,e2;
722:
723: if ( TD(d1) > TD(d2) ) return 1;
724: else if ( TD(d1) < TD(d2) ) return -1;
725: m = nd_nvar>>1;
726: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
727: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
728: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
729: }
730: if ( e1 > e2 ) return 1;
731: else if ( e1 < e2 ) return -1;
732: return ndl_lex_compare(d1,d2);
733: }
734:
1.21 noro 735: // common function for module glex and grlex comparison
736: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 737: {
1.21 noro 738: int c;
1.1 noro 739:
1.21 noro 740: switch ( nd_module_ordtype ) {
741: case 0:
742: if ( TD(d1) > TD(d2) ) return 1;
743: else if ( TD(d1) < TD(d2) ) return -1;
744: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
745: else if ( MPOS(d1) < MPOS(d2) ) return 1;
746: else if ( MPOS(d1) > MPOS(d2) ) return -1;
747: else return 0;
748: break;
1.1 noro 749:
1.21 noro 750: case 1:
1.19 noro 751: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
752: if ( TD(d1) > TD(d2) ) return 1;
753: else if ( TD(d1) < TD(d2) ) return -1;
754: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
755: if ( MPOS(d1) < MPOS(d2) ) return 1;
756: else if ( MPOS(d1) > MPOS(d2) ) return -1;
757: }
758: if ( MPOS(d1) < MPOS(d2) ) return 1;
759: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 760: else if ( TD(d1) > TD(d2) ) return 1;
761: else if ( TD(d1) < TD(d2) ) return -1;
762: else return ndl_lex_compare(d1,d2);
763: break;
1.1 noro 764:
1.21 noro 765: case 2: // weight -> POT
766: if ( TD(d1) > TD(d2) ) return 1;
767: else if ( TD(d1) < TD(d2) ) return -1;
768: else if ( MPOS(d1) < MPOS(d2) ) return 1;
769: else if ( MPOS(d1) > MPOS(d2) ) return -1;
770: else return ndl_lex_compare(d1,d2);
771: break;
1.1 noro 772:
1.21 noro 773: default:
774: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 775: return 0;
1.21 noro 776: }
1.1 noro 777: }
778:
1.21 noro 779: // common for module comparison
780: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 781: {
1.21 noro 782: int c;
1.1 noro 783:
1.21 noro 784: switch ( nd_module_ordtype ) {
785: case 0:
1.23 noro 786: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 787: else if ( MPOS(d1) > MPOS(d2) ) return -1;
788: else if ( MPOS(d1) < MPOS(d2) ) return 1;
789: else return 0;
790: break;
1.1 noro 791:
1.21 noro 792: case 1:
793: if ( MPOS(d1) < MPOS(d2) ) return 1;
794: else if ( MPOS(d1) > MPOS(d2) ) return -1;
795: else return (*ndl_base_compare_function)(d1,d2);
796: break;
1.1 noro 797:
1.21 noro 798: case 2: // weight -> POT
799: if ( TD(d1) > TD(d2) ) return 1;
800: else if ( TD(d1) < TD(d2) ) return -1;
801: else if ( MPOS(d1) < MPOS(d2) ) return 1;
802: else if ( MPOS(d1) > MPOS(d2) ) return -1;
803: else return (*ndl_base_compare_function)(d1,d2);
804: break;
1.1 noro 805:
1.21 noro 806: default:
807: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 808: return 0;
1.21 noro 809: }
1.1 noro 810: }
811:
1.21 noro 812: extern DMMstack dmm_stack;
813: void _addtodl(int n,DL d1,DL d2);
1.31 noro 814: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 815: int _eqdl(int n,DL d1,DL d2);
816:
817: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
818: {
1.44 noro 819: int pos1,pos2,t,j,retpot;
1.21 noro 820: DMM *in;
821: DMMstack s;
822: static DL d1=0;
823: static DL d2=0;
824: static int dlen=0;
1.44 noro 825: extern int ReversePOT;
1.21 noro 826:
1.44 noro 827: if ( ReversePOT ) retpot = -1;
828: else retpot = 1;
1.21 noro 829: pos1 = MPOS(m1); pos2 = MPOS(m2);
830: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
831: if ( nd_nvar > dlen ) {
832: NEWDL(d1,nd_nvar);
833: NEWDL(d2,nd_nvar);
834: dlen = nd_nvar;
835: }
836: d1->td = TD(m1);
837: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
838: d2->td = TD(m2);
839: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
840: for ( s = dmm_stack; s; s = NEXT(s) ) {
841: in = s->in;
842: _addtodl(nd_nvar,in[pos1]->dl,d1);
843: _addtodl(nd_nvar,in[pos2]->dl,d2);
844: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44 noro 845: if ( pos1 < pos2 ) return retpot;
846: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 847: else return 0;
848: }
849: pos1 = in[pos1]->pos;
850: pos2 = in[pos2]->pos;
851: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
852: }
853: // comparison by the bottom order
854: LAST:
855: switch ( nd_base_ordtype ) {
856: case 0:
857: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
858: if ( t ) return t;
1.44 noro 859: else if ( pos1 < pos2 ) return retpot;
860: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 861: else return 0;
862: break;
863: case 1:
1.44 noro 864: if ( pos1 < pos2 ) return retpot;
865: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 866: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
867: break;
868: case 2:
869: if ( d1->td > d2->td ) return 1;
870: else if ( d1->td < d2->td ) return -1;
1.44 noro 871: else if ( pos1 < pos2 ) return retpot;
872: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 873: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
874: break;
875: default:
876: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 877: return 0;
1.21 noro 878: }
1.1 noro 879: }
880:
881: INLINE int ndl_equal(UINT *d1,UINT *d2)
882: {
883: int i;
884:
885: switch ( nd_wpd ) {
886: case 2:
887: if ( TD(d2) != TD(d1) ) return 0;
888: if ( d2[1] != d1[1] ) return 0;
889: return 1;
890: break;
891: case 3:
892: if ( TD(d2) != TD(d1) ) return 0;
893: if ( d2[1] != d1[1] ) return 0;
894: if ( d2[2] != d1[2] ) return 0;
895: return 1;
896: break;
897: default:
898: for ( i = 0; i < nd_wpd; i++ )
899: if ( *d1++ != *d2++ ) return 0;
900: return 1;
901: break;
902: }
903: }
904:
905: INLINE void ndl_copy(UINT *d1,UINT *d2)
906: {
907: int i;
908:
909: switch ( nd_wpd ) {
910: case 2:
911: TD(d2) = TD(d1);
912: d2[1] = d1[1];
913: break;
914: case 3:
915: TD(d2) = TD(d1);
916: d2[1] = d1[1];
917: d2[2] = d1[2];
918: break;
919: default:
920: for ( i = 0; i < nd_wpd; i++ )
921: d2[i] = d1[i];
922: break;
923: }
924: }
925:
926: INLINE void ndl_zero(UINT *d)
927: {
928: int i;
929: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
930: }
931:
932: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
933: {
934: int i;
935:
936: if ( nd_module ) {
937: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
938: error("ndl_add : invalid operation");
939: }
940: #if 1
941: switch ( nd_wpd ) {
942: case 2:
943: TD(d) = TD(d1)+TD(d2);
944: d[1] = d1[1]+d2[1];
945: break;
946: case 3:
947: TD(d) = TD(d1)+TD(d2);
948: d[1] = d1[1]+d2[1];
949: d[2] = d1[2]+d2[2];
950: break;
951: default:
952: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
953: break;
954: }
955: #else
956: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
957: #endif
958: }
959:
960: /* d1 += d2 */
961: INLINE void ndl_addto(UINT *d1,UINT *d2)
962: {
963: int i;
964:
965: if ( nd_module ) {
966: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
967: error("ndl_addto : invalid operation");
968: }
969: #if 1
970: switch ( nd_wpd ) {
971: case 2:
972: TD(d1) += TD(d2);
973: d1[1] += d2[1];
974: break;
975: case 3:
976: TD(d1) += TD(d2);
977: d1[1] += d2[1];
978: d1[2] += d2[2];
979: break;
980: default:
981: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
982: break;
983: }
984: #else
985: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
986: #endif
987: }
988:
989: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
990: {
991: int i;
992:
993: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
994: }
995:
996: int ndl_disjoint(UINT *d1,UINT *d2)
997: {
998: UINT t1,t2,u,u1,u2;
999: int i,j;
1000:
1001: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1002: #if USE_UNROLL
1003: switch ( nd_bpe ) {
1004: case 3:
1005: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1006: u1 = d1[i]; u2 = d2[i];
1007: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
1015: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
1017: }
1018: return 1;
1019: break;
1020: case 4:
1021: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1022: u1 = d1[i]; u2 = d2[i];
1023: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1027: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1028: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1029: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1030: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1031: }
1032: return 1;
1033: break;
1034: case 6:
1035: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1036: u1 = d1[i]; u2 = d2[i];
1037: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1038: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1039: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1040: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1041: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1042: }
1043: return 1;
1044: break;
1045: case 8:
1046: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1047: u1 = d1[i]; u2 = d2[i];
1048: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1049: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1050: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1051: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1052: }
1053: return 1;
1054: break;
1055: case 16:
1056: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1057: u1 = d1[i]; u2 = d2[i];
1058: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1059: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1060: }
1061: return 1;
1062: break;
1063: case 32:
1064: for ( i = nd_exporigin; i < nd_wpd; i++ )
1065: if ( d1[i] && d2[i] ) return 0;
1066: return 1;
1067: break;
1068: default:
1069: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1070: u1 = d1[i]; u2 = d2[i];
1071: for ( j = 0; j < nd_epw; j++ ) {
1072: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1073: u1 >>= nd_bpe; u2 >>= nd_bpe;
1074: }
1075: }
1076: return 1;
1077: break;
1078: }
1079: #else
1080: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1081: u1 = d1[i]; u2 = d2[i];
1082: for ( j = 0; j < nd_epw; j++ ) {
1083: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1084: u1 >>= nd_bpe; u2 >>= nd_bpe;
1085: }
1086: }
1087: return 1;
1088: #endif
1089: }
1090:
1091: int ndl_check_bound(UINT *d1,UINT *d2)
1092: {
1093: UINT u2;
1094: int i,j,ind,k;
1095:
1096: ind = 0;
1097: #if USE_UNROLL
1098: switch ( nd_bpe ) {
1099: case 3:
1100: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1101: u2 = d2[i];
1102: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1103: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1104: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1105: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1106: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1107: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1108: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1109: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1110: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1111: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1112: }
1113: return 0;
1114: break;
1115: case 4:
1116: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1117: u2 = d2[i];
1118: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1119: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1120: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1121: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1122: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1123: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1124: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1125: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1126: }
1127: return 0;
1128: break;
1129: case 6:
1130: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1131: u2 = d2[i];
1132: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1133: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1134: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1135: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1136: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1137: }
1138: return 0;
1139: break;
1140: case 8:
1141: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1142: u2 = d2[i];
1143: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1144: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1145: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1146: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1147: }
1148: return 0;
1149: break;
1150: case 16:
1151: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1152: u2 = d2[i];
1153: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1154: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1155: }
1156: return 0;
1157: break;
1158: case 32:
1159: for ( i = nd_exporigin; i < nd_wpd; i++ )
1160: if ( d1[i]+d2[i]<d1[i] ) return 1;
1161: return 0;
1162: break;
1163: default:
1164: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1165: u2 = d2[i];
1166: k = (nd_epw-1)*nd_bpe;
1167: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1168: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1169: }
1170: return 0;
1171: break;
1172: }
1173: #else
1174: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1175: u2 = d2[i];
1176: k = (nd_epw-1)*nd_bpe;
1177: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1178: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1179: }
1180: return 0;
1181: #endif
1182: }
1183:
1184: int ndl_check_bound2(int index,UINT *d2)
1185: {
1186: return ndl_check_bound(nd_bound[index],d2);
1187: }
1188:
1189: INLINE int ndl_hash_value(UINT *d)
1190: {
1191: int i;
1.11 noro 1192: UINT r;
1.1 noro 1193:
1194: r = 0;
1195: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1196: r = (r*1511+d[i]);
1.11 noro 1197: r %= REDTAB_LEN;
1.1 noro 1198: return r;
1199: }
1200:
1201: INLINE int ndl_find_reducer(UINT *dg)
1202: {
1203: RHist r;
1204: int d,k,i;
1205:
1206: d = ndl_hash_value(dg);
1207: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1208: if ( ndl_equal(dg,DL(r)) ) {
1209: if ( k > 0 ) nd_notfirst++;
1210: nd_found++;
1211: return r->index;
1212: }
1213: }
1214: if ( Reverse )
1215: for ( i = nd_psn-1; i >= 0; i-- ) {
1216: r = nd_psh[i];
1217: if ( ndl_reducible(dg,DL(r)) ) {
1218: nd_create++;
1219: nd_append_red(dg,i);
1220: return i;
1221: }
1222: }
1223: else
1224: for ( i = 0; i < nd_psn; i++ ) {
1225: r = nd_psh[i];
1226: if ( ndl_reducible(dg,DL(r)) ) {
1227: nd_create++;
1228: nd_append_red(dg,i);
1229: return i;
1230: }
1231: }
1232: return -1;
1233: }
1234:
1.41 noro 1235: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1236: {
1237: RHist r;
1238: int i;
1239:
1240: for ( i = 0; i < nd_psn; i++ ) {
1241: r = nd_psh[i];
1242: if ( ndl_reducible(dg,DL(r)) ) return i;
1243: }
1244: return -1;
1245: }
1246:
1.24 noro 1247: // ret=0,...,nd_psn-1 => reducer found
1248: // ret=nd_psn => reducer not found
1249: // ret=-1 => singular top reducible
1250:
1251: int comp_sig(SIG s1,SIG s2);
1252: void _ndltodl(UINT *ndl,DL dl);
1253:
1254: void print_sig(SIG s)
1255: {
1256: int i;
1257:
1258: fprintf(asir_out,"<<");
1259: for ( i = 0; i < nd_nvar; i++ ) {
1260: fprintf(asir_out,"%d",s->dl->d[i]);
1261: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1262: }
1263: fprintf(asir_out,">>*e%d",s->pos);
1264: }
1265:
1.45 noro 1266: void print_siglist(NODE l)
1267: {
1268: for ( ; l; l = NEXT(l) )
1269: print_sig((SIG)l->body);
1270: }
1271:
1272:
1.35 noro 1273: // assuming increasing order wrt signature
1274:
1.24 noro 1275: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1276: {
1277: RHist r;
1.27 noro 1278: int i,singular,ret,d,k;
1.26 noro 1279: static int wpd,nvar;
1.24 noro 1280: static SIG quo;
1281: static UINT *tmp;
1282:
1.26 noro 1283: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1284: if ( wpd != nd_wpd ) {
1.24 noro 1285: wpd = nd_wpd;
1286: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1287: }
1.27 noro 1288: d = ndl_hash_value(dg);
1.35 noro 1289: #if 1
1.27 noro 1290: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1291: if ( ndl_equal(dg,DL(r)) ) {
1292: return r->index;
1293: }
1294: }
1.35 noro 1295: #endif
1.24 noro 1296: singular = 0;
1297: for ( i = 0; i < nd_psn; i++ ) {
1298: r = nd_psh[i];
1299: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1300: ndl_sub(dg,DL(r),tmp);
1.24 noro 1301: _ndltodl(tmp,DL(quo));
1302: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1303: quo->pos = nd_psh[i]->sig->pos;
1304: ret = comp_sig(sig,quo);
1305: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1306: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1307: }
1308: }
1309: if ( singular ) return -1;
1.27 noro 1310: else if ( i < nd_psn )
1311: nd_append_red(dg,i);
1312: return i;
1.24 noro 1313: }
1314:
1.1 noro 1315: ND nd_merge(ND p1,ND p2)
1316: {
1317: int n,c;
1318: int t,can,td1,td2;
1319: ND r;
1320: NM m1,m2,mr0,mr,s;
1321:
1322: if ( !p1 ) return p2;
1323: else if ( !p2 ) return p1;
1324: else {
1325: can = 0;
1326: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1327: c = DL_COMPARE(DL(m1),DL(m2));
1328: switch ( c ) {
1329: case 0:
1330: s = m1; m1 = NEXT(m1);
1331: can++; NEXTNM2(mr0,mr,s);
1332: s = m2; m2 = NEXT(m2); FREENM(s);
1333: break;
1334: case 1:
1335: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1336: break;
1337: case -1:
1338: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1339: break;
1340: }
1341: }
1342: if ( !mr0 )
1343: if ( m1 ) mr0 = m1;
1344: else if ( m2 ) mr0 = m2;
1345: else return 0;
1346: else if ( m1 ) NEXT(mr) = m1;
1347: else if ( m2 ) NEXT(mr) = m2;
1348: else NEXT(mr) = 0;
1349: BDY(p1) = mr0;
1350: SG(p1) = MAX(SG(p1),SG(p2));
1351: LEN(p1) = LEN(p1)+LEN(p2)-can;
1352: FREEND(p2);
1353: return p1;
1354: }
1355: }
1356:
1357: ND nd_add(int mod,ND p1,ND p2)
1358: {
1359: int n,c;
1360: int t,can,td1,td2;
1361: ND r;
1362: NM m1,m2,mr0,mr,s;
1363:
1.11 noro 1364: Nnd_add++;
1.1 noro 1365: if ( !p1 ) return p2;
1366: else if ( !p2 ) return p1;
1367: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1368: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1369: else if ( !mod ) return nd_add_q(p1,p2);
1370: else {
1371: can = 0;
1372: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1373: c = DL_COMPARE(DL(m1),DL(m2));
1374: switch ( c ) {
1375: case 0:
1376: t = ((CM(m1))+(CM(m2))) - mod;
1377: if ( t < 0 ) t += mod;
1378: s = m1; m1 = NEXT(m1);
1379: if ( t ) {
1380: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1381: } else {
1382: can += 2; FREENM(s);
1383: }
1384: s = m2; m2 = NEXT(m2); FREENM(s);
1385: break;
1386: case 1:
1387: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1388: break;
1389: case -1:
1390: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1391: break;
1392: }
1393: }
1394: if ( !mr0 )
1395: if ( m1 ) mr0 = m1;
1396: else if ( m2 ) mr0 = m2;
1397: else return 0;
1398: else if ( m1 ) NEXT(mr) = m1;
1399: else if ( m2 ) NEXT(mr) = m2;
1400: else NEXT(mr) = 0;
1401: BDY(p1) = mr0;
1402: SG(p1) = MAX(SG(p1),SG(p2));
1403: LEN(p1) = LEN(p1)+LEN(p2)-can;
1404: FREEND(p2);
1405: return p1;
1406: }
1407: }
1408:
1409: /* XXX on opteron, the inlined manipulation of destructive additon of
1410: * two NM seems to make gcc optimizer get confused, so the part is
1411: * done in a function.
1412: */
1413:
1414: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1415: {
1416: NM s;
1417: P t;
1418: int can;
1419:
1420: addp(nd_vc,CP(*m1),CP(*m2),&t);
1421: s = *m1; *m1 = NEXT(*m1);
1422: if ( t ) {
1423: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1424: } else {
1425: can = 2; FREENM(s);
1426: }
1427: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1428: return can;
1429: }
1430:
1431: ND nd_add_q(ND p1,ND p2)
1432: {
1433: int n,c,can;
1434: ND r;
1435: NM m1,m2,mr0,mr,s;
1436: P t;
1437:
1438: if ( !p1 ) return p2;
1439: else if ( !p2 ) return p1;
1440: else {
1441: can = 0;
1442: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1443: c = DL_COMPARE(DL(m1),DL(m2));
1444: switch ( c ) {
1445: case 0:
1446: #if defined(__x86_64__)
1447: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1448: #else
1449: addp(nd_vc,CP(m1),CP(m2),&t);
1450: s = m1; m1 = NEXT(m1);
1451: if ( t ) {
1452: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1453: } else {
1454: can += 2; FREENM(s);
1455: }
1456: s = m2; m2 = NEXT(m2); FREENM(s);
1457: #endif
1458: break;
1459: case 1:
1460: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1461: break;
1462: case -1:
1463: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1464: break;
1465: }
1466: }
1467: if ( !mr0 )
1468: if ( m1 ) mr0 = m1;
1469: else if ( m2 ) mr0 = m2;
1470: else return 0;
1471: else if ( m1 ) NEXT(mr) = m1;
1472: else if ( m2 ) NEXT(mr) = m2;
1473: else NEXT(mr) = 0;
1474: BDY(p1) = mr0;
1475: SG(p1) = MAX(SG(p1),SG(p2));
1476: LEN(p1) = LEN(p1)+LEN(p2)-can;
1477: FREEND(p2);
1478: return p1;
1479: }
1480: }
1481:
1482: ND nd_add_sf(ND p1,ND p2)
1483: {
1484: int n,c,can;
1485: ND r;
1486: NM m1,m2,mr0,mr,s;
1487: int t;
1488:
1489: if ( !p1 ) return p2;
1490: else if ( !p2 ) return p1;
1491: else {
1492: can = 0;
1493: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1494: c = DL_COMPARE(DL(m1),DL(m2));
1495: switch ( c ) {
1496: case 0:
1497: t = _addsf(CM(m1),CM(m2));
1498: s = m1; m1 = NEXT(m1);
1499: if ( t ) {
1500: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1501: } else {
1502: can += 2; FREENM(s);
1503: }
1504: s = m2; m2 = NEXT(m2); FREENM(s);
1505: break;
1506: case 1:
1507: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1508: break;
1509: case -1:
1510: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1511: break;
1512: }
1513: }
1514: if ( !mr0 )
1515: if ( m1 ) mr0 = m1;
1516: else if ( m2 ) mr0 = m2;
1517: else return 0;
1518: else if ( m1 ) NEXT(mr) = m1;
1519: else if ( m2 ) NEXT(mr) = m2;
1520: else NEXT(mr) = 0;
1521: BDY(p1) = mr0;
1522: SG(p1) = MAX(SG(p1),SG(p2));
1523: LEN(p1) = LEN(p1)+LEN(p2)-can;
1524: FREEND(p2);
1525: return p1;
1526: }
1527: }
1528:
1529:
1530: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1531: {
1532: int c,c1,c2;
1533: Z cg,cred,gcd,tq;
1534: P cgp,credp,gcdp;
1535: Obj tr,tr1;
1536:
1537: if ( mod == -1 ) {
1538: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1539: *divp = (Obj)ONE;
1540: } else if ( mod == -2 ) {
1541: Z inv,t;
1542: divlf(ONE,HCZ(p),&inv);
1543: chsgnlf(HCZ(g),&t);
1544: mullf(inv,t,&CZ(mul));
1545: *divp = (Obj)ONE;
1546: } else if ( mod ) {
1547: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1548: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1549: *divp = (Obj)ONE;
1550: } else if ( nd_vc ) {
1551: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1552: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1553: chsgnp(cgp,&CP(mul));
1554: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1555: if ( dn ) {
1556: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1557: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1558: }
1559: *divp = (Obj)credp;
1560: } else {
1.6 noro 1561: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1562: chsgnz(cg,&CZ(mul));
1.1 noro 1563: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1564: if ( dn ) {
1565: mulz(dn->z,cred,&tq); dn->z = tq;
1566: }
1567: *divp = (Obj)cred;
1568: }
1569: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1570: }
1571:
1572: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1573: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1574: {
1575: NM m,mrd,tail;
1576: NM mul;
1577: int n,sugar,psugar,sugar0,stat,index;
1578: int c,c1,c2,dummy;
1579: RHist h;
1580: NDV p,red;
1581: Q cg,cred,gcd,tq,qq;
1582: Z iq;
1583: DP dmul;
1584: NODE node;
1585: LIST hist;
1586: double hmag;
1587: P tp,tp1;
1588: Obj tr,tr1,div;
1589: union oNDC hg;
1590: P cont;
1591:
1592: if ( !g ) {
1593: *rp = d;
1594: return 1;
1595: }
1596: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1597:
1598: sugar0 = sugar = SG(g);
1599: n = NV(g);
1600: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1601: if ( d )
1602: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1603: for ( ; g; ) {
1604: index = ndl_find_reducer(HDL(g));
1605: if ( index >= 0 ) {
1606: h = nd_psh[index];
1607: ndl_sub(HDL(g),DL(h),DL(mul));
1608: if ( ndl_check_bound2(index,DL(mul)) ) {
1609: nd_free(g); nd_free(d);
1610: return 0;
1611: }
1612: p = nd_demand ? ndv_load(index) : ps[index];
1613: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1614: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1615: if ( nd_gentrace ) {
1616: /* Trace=[div,index,mul,ONE] */
1.6 noro 1617: STOZ(index,iq);
1.1 noro 1618: nmtodp(mod,mul,&dmul);
1619: node = mknode(4,div,iq,dmul,ONE);
1620: }
1621: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1622: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1623: hg = HCU(g);
1624: nd_removecont2(d,g);
1.6 noro 1625: if ( nd_gentrace ) {
1.1 noro 1626: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1627: /* exact division */
1.1 noro 1628: cont = ndc_div(mod,hg,HCU(g));
1629: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1630: }
1631: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1632: }
1633: MKLIST(hist,node);
1634: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1635: } else if ( !full ) {
1636: *rp = g;
1637: return 1;
1638: } else {
1639: m = BDY(g);
1640: if ( NEXT(m) ) {
1641: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1642: } else {
1643: FREEND(g); g = 0;
1644: }
1645: if ( d ) {
1646: NEXT(tail)=m; tail=m; LEN(d)++;
1647: } else {
1648: MKND(n,m,1,d); tail = BDY(d);
1649: }
1650: }
1651: }
1652: if ( d ) SG(d) = sugar;
1653: *rp = d;
1654: return 1;
1655: }
1656:
1.24 noro 1657: // ret=1 => success
1658: // ret=0 => overflow
1659: // ret=-1 => singular top reducible
1660:
1661: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1662: {
1663: NM m,mrd,tail;
1664: NM mul;
1665: int n,sugar,psugar,sugar0,stat,index;
1666: int c,c1,c2,dummy;
1667: RHist h;
1668: NDV p,red;
1669: Q cg,cred,gcd,tq,qq;
1670: Z iq;
1671: DP dmul;
1672: NODE node;
1673: LIST hist;
1674: double hmag;
1675: P tp,tp1;
1676: Obj tr,tr1,div;
1677: union oNDC hg;
1678: P cont;
1679: SIG sig;
1680:
1681: if ( !g ) {
1682: *rp = d;
1683: return 1;
1684: }
1685: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1686:
1687: sugar0 = sugar = SG(g);
1688: n = NV(g);
1689: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1690: if ( d )
1691: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1692: sig = g->sig;
1693: for ( ; g; ) {
1694: index = ndl_find_reducer_s(HDL(g),sig);
1695: if ( index >= 0 && index < nd_psn ) {
1696: // reducer found
1697: h = nd_psh[index];
1698: ndl_sub(HDL(g),DL(h),DL(mul));
1699: if ( ndl_check_bound2(index,DL(mul)) ) {
1700: nd_free(g); nd_free(d);
1701: return 0;
1702: }
1703: p = ps[index];
1704: /* d+g -> div*(d+g)+mul*p */
1705: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1706: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1707: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1708: hg = HCU(g);
1709: nd_removecont2(d,g);
1710: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1711: }
1712: } else if ( index == -1 ) {
1713: // singular top reducible
1714: return -1;
1715: } else if ( !full ) {
1716: *rp = g;
1717: g->sig = sig;
1718: return 1;
1719: } else {
1720: m = BDY(g);
1721: if ( NEXT(m) ) {
1722: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1723: } else {
1724: FREEND(g); g = 0;
1725: }
1726: if ( d ) {
1727: NEXT(tail)=m; tail=m; LEN(d)++;
1728: } else {
1729: MKND(n,m,1,d); tail = BDY(d);
1730: }
1731: }
1732: }
1733: if ( d ) {
1734: SG(d) = sugar;
1735: d->sig = sig;
1736: }
1737: *rp = d;
1738: return 1;
1739: }
1740:
1.1 noro 1741: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1742: {
1743: int hindex,index;
1744: NDV p;
1745: ND u,d,red;
1746: NODE l;
1747: NM mul,m,mrd,tail;
1748: int sugar,psugar,n,h_reducible;
1749: PGeoBucket bucket;
1750: int c,c1,c2;
1751: Z cg,cred,gcd,zzz;
1752: RHist h;
1753: double hmag,gmag;
1754: int count = 0;
1755: int hcount = 0;
1756:
1757: if ( !g ) {
1758: *rp = 0;
1759: return 1;
1760: }
1761: sugar = SG(g);
1762: n = NV(g);
1.6 noro 1763: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1764: bucket = create_pbucket();
1765: add_pbucket(mod,bucket,g);
1766: d = 0;
1767: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1768: while ( 1 ) {
1769: if ( mod > 0 || mod == -1 )
1770: hindex = head_pbucket(mod,bucket);
1771: else if ( mod == -2 )
1772: hindex = head_pbucket_lf(bucket);
1773: else
1774: hindex = head_pbucket_q(bucket);
1775: if ( hindex < 0 ) {
1776: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1777: if ( d ) SG(d) = sugar;
1778: *rp = d;
1779: return 1;
1780: }
1781: g = bucket->body[hindex];
1782: index = ndl_find_reducer(HDL(g));
1783: if ( index >= 0 ) {
1784: count++;
1785: if ( !d ) hcount++;
1786: h = nd_psh[index];
1787: ndl_sub(HDL(g),DL(h),DL(mul));
1788: if ( ndl_check_bound2(index,DL(mul)) ) {
1789: nd_free(d);
1790: free_pbucket(bucket);
1791: *rp = 0;
1792: return 0;
1793: }
1794: p = ps[index];
1795: if ( mod == -1 )
1796: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1797: else if ( mod == -2 ) {
1798: Z inv,t;
1799: divlf(ONE,HCZ(p),&inv);
1800: chsgnlf(HCZ(g),&t);
1801: mullf(inv,t,&CZ(mul));
1802: } else if ( mod ) {
1803: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1804: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1805: } else {
1.6 noro 1806: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1807: chsgnz(cg,&CZ(mul));
1.1 noro 1808: nd_mul_c_q(d,(P)cred);
1809: mulq_pbucket(bucket,cred);
1810: g = bucket->body[hindex];
1.6 noro 1811: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1812: }
1813: red = ndv_mul_nm(mod,mul,p);
1814: bucket->body[hindex] = nd_remove_head(g);
1815: red = nd_remove_head(red);
1816: add_pbucket(mod,bucket,red);
1817: psugar = SG(p)+TD(DL(mul));
1818: sugar = MAX(sugar,psugar);
1819: if ( !mod && hmag && (gmag > hmag) ) {
1820: g = normalize_pbucket(mod,bucket);
1821: if ( !g ) {
1822: if ( d ) SG(d) = sugar;
1823: *rp = d;
1824: return 1;
1825: }
1826: nd_removecont2(d,g);
1.6 noro 1827: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1828: add_pbucket(mod,bucket,g);
1829: }
1830: } else if ( !full ) {
1831: g = normalize_pbucket(mod,bucket);
1832: if ( g ) SG(g) = sugar;
1833: *rp = g;
1834: return 1;
1835: } else {
1836: m = BDY(g);
1837: if ( NEXT(m) ) {
1838: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1839: } else {
1840: FREEND(g); g = 0;
1841: }
1842: bucket->body[hindex] = g;
1843: NEXT(m) = 0;
1844: if ( d ) {
1845: NEXT(tail)=m; tail=m; LEN(d)++;
1846: } else {
1847: MKND(n,m,1,d); tail = BDY(d);
1848: }
1849: }
1850: }
1851: }
1852:
1.25 noro 1853: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1854: {
1855: int hindex,index;
1856: NDV p;
1857: ND u,d,red;
1858: NODE l;
1859: NM mul,m,mrd,tail;
1860: int sugar,psugar,n,h_reducible;
1861: PGeoBucket bucket;
1862: int c,c1,c2;
1863: Z cg,cred,gcd,zzz;
1864: RHist h;
1865: double hmag,gmag;
1866: int count = 0;
1867: int hcount = 0;
1868: SIG sig;
1869:
1870: if ( !g ) {
1871: *rp = 0;
1872: return 1;
1873: }
1874: sugar = SG(g);
1875: n = NV(g);
1876: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1877: bucket = create_pbucket();
1878: add_pbucket(mod,bucket,g);
1879: d = 0;
1880: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1881: sig = g->sig;
1882: while ( 1 ) {
1883: if ( mod > 0 || mod == -1 )
1884: hindex = head_pbucket(mod,bucket);
1885: else if ( mod == -2 )
1886: hindex = head_pbucket_lf(bucket);
1887: else
1888: hindex = head_pbucket_q(bucket);
1889: if ( hindex < 0 ) {
1890: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1891: if ( d ) {
1892: SG(d) = sugar;
1893: d->sig = sig;
1894: }
1895: *rp = d;
1896: return 1;
1897: }
1898: g = bucket->body[hindex];
1899: index = ndl_find_reducer_s(HDL(g),sig);
1900: if ( index >= 0 && index < nd_psn ) {
1901: count++;
1902: if ( !d ) hcount++;
1903: h = nd_psh[index];
1904: ndl_sub(HDL(g),DL(h),DL(mul));
1905: if ( ndl_check_bound2(index,DL(mul)) ) {
1906: nd_free(d);
1907: free_pbucket(bucket);
1908: *rp = 0;
1909: return 0;
1910: }
1911: p = ps[index];
1912: if ( mod == -1 )
1913: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1914: else if ( mod == -2 ) {
1915: Z inv,t;
1916: divlf(ONE,HCZ(p),&inv);
1917: chsgnlf(HCZ(g),&t);
1918: mullf(inv,t,&CZ(mul));
1919: } else if ( mod ) {
1920: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1921: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1922: } else {
1923: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1924: chsgnz(cg,&CZ(mul));
1925: nd_mul_c_q(d,(P)cred);
1926: mulq_pbucket(bucket,cred);
1927: g = bucket->body[hindex];
1928: gmag = (double)p_mag((P)HCZ(g));
1929: }
1930: red = ndv_mul_nm(mod,mul,p);
1931: bucket->body[hindex] = nd_remove_head(g);
1932: red = nd_remove_head(red);
1933: add_pbucket(mod,bucket,red);
1934: psugar = SG(p)+TD(DL(mul));
1935: sugar = MAX(sugar,psugar);
1936: if ( !mod && hmag && (gmag > hmag) ) {
1937: g = normalize_pbucket(mod,bucket);
1938: if ( !g ) {
1939: if ( d ) {
1940: SG(d) = sugar;
1941: d->sig = sig;
1942: }
1943: *rp = d;
1944: return 1;
1945: }
1946: nd_removecont2(d,g);
1947: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1948: add_pbucket(mod,bucket,g);
1949: }
1950: } else if ( index == -1 ) {
1951: // singular top reducible
1952: return -1;
1953: } else if ( !full ) {
1954: g = normalize_pbucket(mod,bucket);
1955: if ( g ) {
1956: SG(g) = sugar;
1957: g->sig = sig;
1958: }
1959: *rp = g;
1960: return 1;
1961: } else {
1962: m = BDY(g);
1963: if ( NEXT(m) ) {
1964: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1965: } else {
1966: FREEND(g); g = 0;
1967: }
1968: bucket->body[hindex] = g;
1969: NEXT(m) = 0;
1970: if ( d ) {
1971: NEXT(tail)=m; tail=m; LEN(d)++;
1972: } else {
1973: MKND(n,m,1,d); tail = BDY(d);
1974: }
1975: }
1976: }
1977: }
1978:
1.1 noro 1979: /* input : list of NDV, cand : list of NDV */
1980:
1981: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1982: {
1983: int n,i,stat;
1984: ND nf,d;
1985: NDV r;
1986: NODE t,s;
1987: union oNDC dn;
1988: Z q;
1989: LIST list;
1990:
1.24 noro 1991: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1992: n = length(cand);
1993:
1994: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1995: /* membercheck : list is a subset of Id(cand) ? */
1996: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1997: again:
1998: nd_tracelist = 0;
1999: if ( nd_bpe > obpe )
2000: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
2001: else
2002: r = (NDV)BDY(t);
2003: #if 0
2004: // moved to nd_f4_lf_trace()
2005: if ( m == -2 ) ndv_mod(m,r);
2006: #endif
2007: d = ndvtond(m,r);
1.6 noro 2008: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 2009: if ( !stat ) {
2010: nd_reconstruct(0,0);
2011: goto again;
2012: } else if ( nf ) return 0;
2013: if ( nd_gentrace ) {
2014: nd_tracelist = reverse_node(nd_tracelist);
2015: MKLIST(list,nd_tracelist);
1.6 noro 2016: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 2017: MKNODE(s,list,nd_alltracelist);
2018: nd_alltracelist = s; nd_tracelist = 0;
2019: }
2020: if ( DP_Print ) { printf("."); fflush(stdout); }
2021: }
2022: if ( DP_Print ) { printf("\n"); }
2023: return 1;
2024: }
2025:
2026: ND nd_remove_head(ND p)
2027: {
2028: NM m;
2029:
2030: m = BDY(p);
2031: if ( !NEXT(m) ) {
2032: FREEND(p); p = 0;
2033: } else {
2034: BDY(p) = NEXT(m); LEN(p)--;
2035: }
2036: FREENM(m);
2037: return p;
2038: }
2039:
2040: ND nd_separate_head(ND p,ND *head)
2041: {
2042: NM m,m0;
2043: ND r;
2044:
2045: m = BDY(p);
2046: if ( !NEXT(m) ) {
2047: *head = p; p = 0;
2048: } else {
2049: m0 = m;
2050: BDY(p) = NEXT(m); LEN(p)--;
2051: NEXT(m0) = 0;
2052: MKND(NV(p),m0,1,r);
2053: *head = r;
2054: }
2055: return p;
2056: }
2057:
2058: PGeoBucket create_pbucket()
2059: {
2060: PGeoBucket g;
2061:
2062: g = CALLOC(1,sizeof(struct oPGeoBucket));
2063: g->m = -1;
2064: return g;
2065: }
2066:
2067: void free_pbucket(PGeoBucket b) {
2068: int i;
2069:
2070: for ( i = 0; i <= b->m; i++ )
2071: if ( b->body[i] ) {
2072: nd_free(b->body[i]);
2073: b->body[i] = 0;
2074: }
2075: GCFREE(b);
2076: }
2077:
1.39 noro 2078: #if 0
1.1 noro 2079: void add_pbucket_symbolic(PGeoBucket g,ND d)
2080: {
2081: int l,i,k,m;
2082:
2083: if ( !d )
2084: return;
2085: l = LEN(d);
2086: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2087: /* 2^(k-1) < l <= 2^k (=m) */
2088: d = nd_merge(g->body[k],d);
2089: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2090: g->body[k] = 0;
2091: d = nd_merge(g->body[k+1],d);
2092: }
2093: g->body[k] = d;
2094: g->m = MAX(g->m,k);
2095: }
1.39 noro 2096: #else
2097: void add_pbucket_symbolic(PGeoBucket g,ND d)
2098: {
2099: int l,i,k,m,m0;
2100:
2101: if ( !d )
2102: return;
2103: m0 = g->m;
2104: while ( 1 ) {
2105: l = LEN(d);
2106: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2107: /* 2^(k-1) < l <= 2^k (=m) */
2108: if ( g->body[k] == 0 ) {
2109: g->body[k] = d;
2110: m0 = MAX(k,m0);
2111: break;
2112: } else {
2113: d = nd_merge(g->body[k],d);
2114: g->body[k] = 0;
2115: }
2116: }
2117: g->m = m0;
2118: }
2119: #endif
1.1 noro 2120:
1.39 noro 2121: #if 0
1.1 noro 2122: void add_pbucket(int mod,PGeoBucket g,ND d)
2123: {
2124: int l,i,k,m;
2125:
2126: if ( !d )
2127: return;
2128: l = LEN(d);
2129: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2130: /* 2^(k-1) < l <= 2^k (=m) */
2131: d = nd_add(mod,g->body[k],d);
2132: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2133: g->body[k] = 0;
2134: d = nd_add(mod,g->body[k+1],d);
2135: }
2136: g->body[k] = d;
2137: g->m = MAX(g->m,k);
2138: }
1.39 noro 2139: #else
2140: void add_pbucket(int mod,PGeoBucket g,ND d)
2141: {
2142: int l,i,k,m,m0;
2143:
2144: m0 = g->m;
2145: while ( d != 0 ) {
2146: l = LEN(d);
2147: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2148: /* 2^(k-1) < l <= 2^k (=m) */
2149: if ( g->body[k] == 0 ) {
2150: g->body[k] = d;
2151: m0 = MAX(k,m0);
2152: break;
2153: } else {
2154: d = nd_add(mod,g->body[k],d);
2155: g->body[k] = 0;
2156: }
2157: }
2158: g->m = m0;
2159: }
2160: #endif
1.1 noro 2161:
2162: void mulq_pbucket(PGeoBucket g,Z c)
2163: {
2164: int k;
2165:
2166: for ( k = 0; k <= g->m; k++ )
2167: nd_mul_c_q(g->body[k],(P)c);
2168: }
2169:
2170: NM remove_head_pbucket_symbolic(PGeoBucket g)
2171: {
2172: int j,i,k,c;
2173: NM head;
2174:
2175: k = g->m;
2176: j = -1;
2177: for ( i = 0; i <= k; i++ ) {
2178: if ( !g->body[i] ) continue;
2179: if ( j < 0 ) j = i;
2180: else {
2181: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2182: if ( c > 0 )
2183: j = i;
2184: else if ( c == 0 )
2185: g->body[i] = nd_remove_head(g->body[i]);
2186: }
2187: }
2188: if ( j < 0 ) return 0;
2189: else {
2190: head = BDY(g->body[j]);
2191: if ( !NEXT(head) ) {
2192: FREEND(g->body[j]);
2193: g->body[j] = 0;
2194: } else {
2195: BDY(g->body[j]) = NEXT(head);
2196: LEN(g->body[j])--;
2197: }
2198: return head;
2199: }
2200: }
2201:
2202: int head_pbucket(int mod,PGeoBucket g)
2203: {
2204: int j,i,c,k,nv,sum;
2205: UINT *di,*dj;
2206: ND gi,gj;
2207:
2208: k = g->m;
2209: while ( 1 ) {
2210: j = -1;
2211: for ( i = 0; i <= k; i++ ) {
2212: if ( !(gi = g->body[i]) )
2213: continue;
2214: if ( j < 0 ) {
2215: j = i;
2216: gj = g->body[j];
2217: dj = HDL(gj);
2218: sum = HCM(gj);
2219: } else {
2220: c = DL_COMPARE(HDL(gi),dj);
2221: if ( c > 0 ) {
2222: if ( sum ) HCM(gj) = sum;
2223: else g->body[j] = nd_remove_head(gj);
2224: j = i;
2225: gj = g->body[j];
2226: dj = HDL(gj);
2227: sum = HCM(gj);
2228: } else if ( c == 0 ) {
2229: if ( mod == -1 )
2230: sum = _addsf(sum,HCM(gi));
2231: else {
2232: sum = sum+HCM(gi)-mod;
2233: if ( sum < 0 ) sum += mod;
2234: }
2235: g->body[i] = nd_remove_head(gi);
2236: }
2237: }
2238: }
2239: if ( j < 0 ) return -1;
2240: else if ( sum ) {
2241: HCM(gj) = sum;
2242: return j;
2243: } else
2244: g->body[j] = nd_remove_head(gj);
2245: }
2246: }
2247:
2248: int head_pbucket_q(PGeoBucket g)
2249: {
2250: int j,i,c,k,nv;
2251: Z sum,t;
2252: ND gi,gj;
2253:
2254: k = g->m;
2255: while ( 1 ) {
2256: j = -1;
2257: for ( i = 0; i <= k; i++ ) {
2258: if ( !(gi = g->body[i]) ) continue;
2259: if ( j < 0 ) {
2260: j = i;
2261: gj = g->body[j];
1.6 noro 2262: sum = HCZ(gj);
1.1 noro 2263: } else {
2264: nv = NV(gi);
2265: c = DL_COMPARE(HDL(gi),HDL(gj));
2266: if ( c > 0 ) {
1.6 noro 2267: if ( sum ) HCZ(gj) = sum;
1.1 noro 2268: else g->body[j] = nd_remove_head(gj);
2269: j = i;
2270: gj = g->body[j];
1.6 noro 2271: sum = HCZ(gj);
1.1 noro 2272: } else if ( c == 0 ) {
1.6 noro 2273: addz(sum,HCZ(gi),&t);
1.1 noro 2274: sum = t;
2275: g->body[i] = nd_remove_head(gi);
2276: }
2277: }
2278: }
2279: if ( j < 0 ) return -1;
2280: else if ( sum ) {
1.6 noro 2281: HCZ(gj) = sum;
1.1 noro 2282: return j;
2283: } else
2284: g->body[j] = nd_remove_head(gj);
2285: }
2286: }
2287:
2288: int head_pbucket_lf(PGeoBucket g)
2289: {
2290: int j,i,c,k,nv;
2291: Z sum,t;
2292: ND gi,gj;
2293:
2294: k = g->m;
2295: while ( 1 ) {
2296: j = -1;
2297: for ( i = 0; i <= k; i++ ) {
2298: if ( !(gi = g->body[i]) ) continue;
2299: if ( j < 0 ) {
2300: j = i;
2301: gj = g->body[j];
2302: sum = HCZ(gj);
2303: } else {
2304: nv = NV(gi);
2305: c = DL_COMPARE(HDL(gi),HDL(gj));
2306: if ( c > 0 ) {
2307: if ( sum ) HCZ(gj) = sum;
2308: else g->body[j] = nd_remove_head(gj);
2309: j = i;
2310: gj = g->body[j];
2311: sum = HCZ(gj);
2312: } else if ( c == 0 ) {
2313: addlf(sum,HCZ(gi),&t);
2314: sum = t;
2315: g->body[i] = nd_remove_head(gi);
2316: }
2317: }
2318: }
2319: if ( j < 0 ) return -1;
2320: else if ( sum ) {
2321: HCZ(gj) = sum;
2322: return j;
2323: } else
2324: g->body[j] = nd_remove_head(gj);
2325: }
2326: }
2327:
2328: ND normalize_pbucket(int mod,PGeoBucket g)
2329: {
2330: int i;
2331: ND r,t;
2332:
2333: r = 0;
2334: for ( i = 0; i <= g->m; i++ ) {
2335: r = nd_add(mod,r,g->body[i]);
2336: g->body[i] = 0;
2337: }
2338: g->m = -1;
2339: return r;
2340: }
2341:
2342: #if 0
2343: void register_hcf(NDV p)
2344: {
2345: DCP dc,t;
2346: P hc,h;
2347: int c;
2348: NODE l,l1,prev;
2349:
2350: hc = p->body->c.p;
2351: if ( !nd_vc || NUM(hc) ) return;
2352: fctrp(nd_vc,hc,&dc);
2353: for ( t = dc; t; t = NEXT(t) ) {
2354: h = t->c;
2355: if ( NUM(h) ) continue;
2356: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2357: c = compp(nd_vc,h,(P)BDY(l));
2358: if ( c >= 0 ) break;
2359: }
2360: if ( !l || c > 0 ) {
2361: MKNODE(l1,h,l);
2362: if ( !prev )
2363: nd_hcf = l1;
2364: else
2365: NEXT(prev) = l1;
2366: }
2367: }
2368: }
2369: #else
2370: void register_hcf(NDV p)
2371: {
2372: DCP dc,t;
2373: P hc,h,q;
2374: Q dmy;
2375: int c;
2376: NODE l,l1,prev;
2377:
2378: hc = p->body->c.p;
2379: if ( NUM(hc) ) return;
2380: ptozp(hc,1,&dmy,&h);
2381: #if 1
2382: for ( l = nd_hcf; l; l = NEXT(l) ) {
2383: while ( 1 ) {
2384: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2385: else break;
2386: }
2387: }
2388: if ( NUM(h) ) return;
2389: #endif
2390: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2391: c = compp(nd_vc,h,(P)BDY(l));
2392: if ( c >= 0 ) break;
2393: }
2394: if ( !l || c > 0 ) {
2395: MKNODE(l1,h,l);
2396: if ( !prev )
2397: nd_hcf = l1;
2398: else
2399: NEXT(prev) = l1;
2400: }
2401: }
2402: #endif
2403:
2404: int do_diagonalize(int sugar,int m)
2405: {
1.6 noro 2406: int i,nh,stat;
2407: NODE r,g,t;
2408: ND h,nf,s,head;
2409: NDV nfv;
2410: Q q;
2411: P nm,nmp,dn,mnp,dnp,cont,cont1;
2412: union oNDC hc;
2413: NODE node;
2414: LIST l;
2415: Z iq;
1.1 noro 2416:
1.6 noro 2417: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2418: if ( nd_gentrace ) {
2419: /* Trace = [1,index,1,1] */
2420: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2421: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2422: }
2423: if ( nd_demand )
2424: nfv = ndv_load(i);
2425: else
2426: nfv = nd_ps[i];
2427: s = ndvtond(m,nfv);
2428: s = nd_separate_head(s,&head);
2429: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2430: if ( !stat ) return 0;
2431: ndv_free(nfv);
2432: hc = HCU(nf); nd_removecont(m,nf);
2433: /* exact division */
2434: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2435: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2436: nfv = ndtondv(m,nf);
2437: nd_free(nf);
2438: nd_bound[i] = ndv_compute_bound(nfv);
2439: if ( !m ) register_hcf(nfv);
2440: if ( nd_demand ) {
2441: ndv_save(nfv,i);
2442: ndv_free(nfv);
2443: } else
2444: nd_ps[i] = nfv;
2445: }
2446: return 1;
1.1 noro 2447: }
2448:
2449: LIST compute_splist()
2450: {
2451: NODE g,tn0,tn,node;
2452: LIST l0;
2453: ND_pairs d,t;
2454: int i;
2455: Z i1,i2;
2456:
2457: g = 0; d = 0;
2458: for ( i = 0; i < nd_psn; i++ ) {
2459: d = update_pairs(d,g,i,0);
2460: g = update_base(g,i);
2461: }
2462: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2463: NEXTNODE(tn0,tn);
1.6 noro 2464: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2465: node = mknode(2,i1,i2); MKLIST(l0,node);
2466: BDY(tn) = l0;
2467: }
2468: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2469: return l0;
2470: }
2471:
1.49 noro 2472: typedef struct oHPDATA {
2473: P hn; // HP(t)=hn(t)/(1-t)^n
2474: int len;
2475: P *head; // hp(i)=head[i] (i=0,...,len-1)
2476: P hp; // dim Hm(i)=hp(i) (i >= len)
2477: VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
2478: P *plist; // plist[i]=(1-t)^i
2479: } *HPDATA;
2480:
2481: void make_reduced(VECT b,int nv);
2482: void mhp_rec(VECT b,VECT x,P t,P *r);
2483: P mhp_ctop(P *r,P *plist,int n);
2484: void mhp_to_hf(VL vl,P hp,int n,P *plist,VECT *head,P *hf);
2485: DL monomial_colon(DL a,DL b,int n);
2486: LIST dp_monomial_hilbert_poincare(VECT b,VECT x,P *plist);
2487:
2488: int hpvalue(HPDATA data,int d)
2489: {
2490: P *head;
2491: int len;
2492: P hp,val;
2493: Z dz;
2494:
2495: head = data->head;
2496: len = data->len;
2497: hp = data->hp;
2498: if ( d < len )
2499: return ZTOS((Z)head[d]);
2500: else {
2501: STOZ(d,dz);
2502: substp(CO,hp,hp->v,(P)dz,&val);
2503: return ZTOS((Z)val);
2504: }
2505: }
2506:
2507: void setup_hpdata(HPDATA final,HPDATA current)
2508: {
2509: int n,i;
2510: P *r;
2511: DL *p;
2512: P tv;
2513: VECT b,x,head;
2514: DL dl;
2515:
2516: n = nd_nvar;
2517: final->hn = (P)ARG0(nd_hpdata);
2518: head = (VECT)ARG2(nd_hpdata);
2519: final->len = head->len;
2520: final->head = (P *)BDY(head);
2521: final->hp = (P)ARG3(nd_hpdata);
2522: final->plist = (P *)BDY((VECT)ARG4(nd_hpdata));
2523: MKVECT(x,n);
2524: for ( i = 0; i < n; i++ ) {
2525: NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
2526: }
2527: final->x = x;
2528:
2529: r = (P *)CALLOC(n+1,sizeof(P));
2530: MKVECT(b,nd_psn); p = (DL *)BDY(b);
2531: for ( i = 0; i < nd_psn; i++ ) {
2532: p[i] = ndltodl(n,nd_psh[i]->dl);
2533: }
2534: make_reduced(b,n);
2535: makevar("t",&tv);
2536: mhp_rec(b,x,tv,r);
2537: current->hn = mhp_ctop(r,final->plist,n);
2538: mhp_to_hf(CO,current->hn,n,final->plist,&head,¤t->hp);
2539: current->head = (P *)BDY(head);
2540: current->len = head->len;
2541: current->x = x;
2542: current->plist = final->plist;
2543: }
2544:
2545: void update_hpdata(HPDATA current,int nh,int do_hf)
2546: {
2547: NODE data1,nd,t;
2548: DL new,dl;
2549: int len,i,n;
2550: Z dz;
2551: DL *p;
2552: VECT b,head;
2553: P tv,td,s,hn,hpoly;
2554: LIST list1;
2555:
2556: n = nd_nvar;
2557: new = ndltodl(n,nd_psh[nh]->dl);
2558: MKVECT(b,nh); p = (DL *)BDY(b);
2559: for ( i = 0; i < nh; i++ ) {
2560: p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
2561: }
2562: // compute HP(I:new)
2563: list1 = dp_monomial_hilbert_poincare(b,current->x,current->plist);
2564: data1 = BDY((LIST)list1);
2565: // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
2566: makevar("t",&tv); UTOZ(new->td,dz);
2567: pwrp(CO,tv,dz,&td);
2568: mulp(CO,(P)ARG0(data1),td,&s);
2569: subp(CO,current->hn,s,&hn);
2570: current->hn = hn;
2571: if ( do_hf ) {
2572: mhp_to_hf(CO,hn,n,current->plist,&head,&hpoly);
2573: current->head = (P *)BDY(head);
2574: current->len = head->len;
2575: current->hp = hpoly;
2576: }
2577: }
2578:
2579: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
2580: {
2581: struct oND_pairs root;
2582: ND_pairs prev,cur;
2583:
2584: root.next = d;
2585: prev = &root; cur = d;
2586: while ( cur ) {
2587: if ( SG(cur) == sugar )
2588: prev->next = cur->next;
2589: else
2590: prev = cur;
2591: cur = cur->next;
2592: }
2593: return root.next;
2594: }
2595:
1.1 noro 2596: /* return value = 0 => input is not a GB */
2597:
2598: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2599: {
1.6 noro 2600: int i,nh,sugar,stat;
2601: NODE r,g,t;
2602: ND_pairs d;
2603: ND_pairs l;
2604: ND h,nf,s,head,nf1;
2605: NDV nfv;
2606: Z q;
2607: union oNDC dn,hc;
2608: int diag_count = 0;
1.41 noro 2609: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2610: P cont;
2611: LIST list;
1.49 noro 2612: struct oHPDATA current_hpdata,final_hpdata;
2613: int final_hpvalue;
2614:
1.27 noro 2615: struct oEGT eg1,eg2,eg_update;
1.6 noro 2616:
1.27 noro 2617: init_eg(&eg_update);
1.11 noro 2618: Nnd_add = 0;
1.6 noro 2619: g = 0; d = 0;
2620: for ( i = 0; i < nd_psn; i++ ) {
2621: d = update_pairs(d,g,i,gensyz);
2622: g = update_base(g,i);
2623: }
2624: sugar = 0;
1.49 noro 2625: if ( nd_hpdata ) {
1.52 ! noro 2626: if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49 noro 2627: setup_hpdata(&final_hpdata,¤t_hpdata);
2628: }
1.6 noro 2629: while ( d ) {
1.1 noro 2630: again:
1.6 noro 2631: l = nd_minp(d,&d);
2632: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2633: if ( SG(l) != sugar ) {
2634: if ( ishomo ) {
2635: diag_count = 0;
2636: stat = do_diagonalize(sugar,m);
1.1 noro 2637: if ( !stat ) {
1.6 noro 2638: NEXT(l) = d; d = l;
2639: d = nd_reconstruct(0,d);
2640: goto again;
1.1 noro 2641: }
1.6 noro 2642: }
2643: sugar = SG(l);
2644: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49 noro 2645: if ( nd_hpdata ) {
2646: if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
2647: break;
2648: else {
2649: final_hpvalue = hpvalue(&final_hpdata,sugar);
2650: if ( final_hpvalue == hpvalue(¤t_hpdata,sugar) ) {
1.52 ! noro 2651: // if ( DP_Print ) fprintf(asir_out,"done.\n",sugar);
1.49 noro 2652: d = nd_remove_same_sugar(d,sugar);
2653: continue;
2654: }
2655: }
2656: }
1.6 noro 2657: }
2658: stat = nd_sp(m,0,l,&h);
2659: if ( !stat ) {
2660: NEXT(l) = d; d = l;
2661: d = nd_reconstruct(0,d);
2662: goto again;
2663: }
1.1 noro 2664: #if USE_GEOBUCKET
1.39 noro 2665: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2666: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2667: #else
1.39 noro 2668: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2669: #endif
1.6 noro 2670: if ( !stat ) {
2671: NEXT(l) = d; d = l;
2672: d = nd_reconstruct(0,d);
2673: goto again;
2674: } else if ( nf ) {
1.41 noro 2675: Nnfnz++;
1.6 noro 2676: if ( checkonly || gensyz ) return 0;
1.1 noro 2677: if ( nd_newelim ) {
2678: if ( nd_module ) {
2679: if ( MPOS(HDL(nf)) > 1 ) return 0;
2680: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2681: }
1.6 noro 2682: if ( DP_Print ) { printf("+"); fflush(stdout); }
2683: hc = HCU(nf);
2684: nd_removecont(m,nf);
2685: if ( !m && nd_nalg ) {
2686: nd_monic(0,&nf);
2687: nd_removecont(m,nf);
2688: }
2689: if ( nd_gentrace ) {
2690: /* exact division */
1.1 noro 2691: cont = ndc_div(m,hc,HCU(nf));
2692: if ( m || !UNIQ(cont) ) {
1.6 noro 2693: t = mknode(4,NULLP,NULLP,NULLP,cont);
2694: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2695: nd_tracelist = t;
2696: }
2697: }
1.6 noro 2698: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2699: nh = ndv_newps(m,nfv,0);
1.6 noro 2700: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2701: diag_count = 0;
2702: stat = do_diagonalize(sugar,m);
2703: if ( !stat ) {
2704: NEXT(l) = d; d = l;
2705: d = nd_reconstruct(1,d);
2706: goto again;
1.1 noro 2707: }
1.6 noro 2708: }
1.27 noro 2709: get_eg(&eg1);
1.6 noro 2710: d = update_pairs(d,g,nh,0);
1.27 noro 2711: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2712: g = update_base(g,nh);
2713: FREENDP(l);
1.49 noro 2714: if ( nd_hpdata ) {
2715: update_hpdata(¤t_hpdata,nh,1);
2716: if ( final_hpvalue == hpvalue(¤t_hpdata,sugar) ) {
1.52 ! noro 2717: // if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49 noro 2718: d = nd_remove_same_sugar(d,sugar);
2719: }
2720: }
1.41 noro 2721: } else {
2722: Nnfz++;
2723: if ( nd_gentrace && gensyz ) {
2724: nd_tracelist = reverse_node(nd_tracelist);
2725: MKLIST(list,nd_tracelist);
2726: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2727: MKNODE(t,list,nd_alltracelist);
2728: nd_alltracelist = t; nd_tracelist = 0;
2729: }
2730: if ( DP_Print ) { printf("."); fflush(stdout); }
2731: FREENDP(l);
2732: }
1.36 noro 2733: }
2734: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2735: if ( !checkonly && DP_Print ) {
2736: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2737: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2738: fflush(asir_out);
2739: }
2740: if ( DP_Print ) {
2741: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2742: }
1.36 noro 2743: return g;
1.1 noro 2744: }
2745:
1.30 noro 2746: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47 noro 2747: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30 noro 2748: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47 noro 2749: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24 noro 2750:
2751: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2752: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2753:
2754: void _copydl(int n,DL d1,DL d2);
2755: void _subfromdl(int n,DL d1,DL d2);
2756: extern int (*cmpdl)(int n,DL d1,DL d2);
2757:
2758: NODE insert_sig(NODE l,SIG s)
2759: {
2760: int pos;
2761: DL sig;
2762: struct oNODE root;
2763: NODE p,prev,r;
2764: SIG t;
2765:
2766: pos = s->pos; sig = DL(s);
2767: root.next = l; prev = &root;
2768: for ( p = l; p; p = p->next ) {
2769: t = (SIG)p->body;
2770: if ( t->pos == pos ) {
2771: if ( _dl_redble(DL(t),sig,nd_nvar) )
2772: return root.next;
2773: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2774: // remove p
2775: prev->next = p->next;
1.45 noro 2776: else
2777: prev = p;
1.24 noro 2778: } else
2779: prev = p;
2780: }
1.27 noro 2781: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2782: for ( p = &root; p->next; p = p->next );
2783: p->next = r;
2784: // r->next = root.next;
2785: // return r;
2786: return root.next;
1.24 noro 2787: }
2788:
2789: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2790: {
2791: struct oND_pairs root;
2792: ND_pairs prev,p;
2793: SIG spsig;
2794:
2795: root.next = d;
2796: prev = &root; p = d;
2797: while ( p ) {
2798: spsig = p->sig;
1.26 noro 2799: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2800: // remove p
2801: prev->next = p->next;
1.26 noro 2802: Nsyz++;
2803: } else
1.24 noro 2804: prev = p;
2805: p = p->next;
2806: }
2807: return (ND_pairs)root.next;
2808: }
2809:
1.29 noro 2810: int _dl_redble_ext(DL,DL,DL,int);
2811:
1.28 noro 2812: int small_lcm(ND_pairs l)
2813: {
2814: SIG sig;
2815: int i;
1.29 noro 2816: NODE t;
1.28 noro 2817: static DL lcm,mul,quo;
1.44 noro 2818: static int nvar = 0;
1.28 noro 2819:
1.38 noro 2820: if ( nd_sba_largelcm ) return 0;
1.28 noro 2821: if ( nvar < nd_nvar ) {
2822: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2823: }
2824: sig = l->sig;
2825: _ndltodl(l->lcm,lcm);
1.29 noro 2826: #if 0
1.28 noro 2827: for ( i = 0; i < nd_psn; i++ ) {
2828: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2829: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2830: _ndltodl(DL(nd_psh[i]),mul);
2831: _addtodl(nd_nvar,quo,mul);
2832: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2833: break;
2834: }
2835: }
2836: if ( i < nd_psn ) return 1;
2837: else return 0;
1.29 noro 2838: #else
2839: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2840: i = (long)BDY(t);
2841: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2842: _ndltodl(DL(nd_psh[i]),mul);
2843: _addtodl(nd_nvar,quo,mul);
2844: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2845: break;
2846: }
2847: }
2848: if ( t ) return 1;
2849: else return 0;
2850: #endif
1.28 noro 2851: }
2852:
1.41 noro 2853: ND_pairs find_smallest_lcm(ND_pairs l)
2854: {
2855: SIG sig;
2856: int i,minindex;
2857: NODE t;
2858: ND_pairs r;
2859: struct oSIG sig1;
2860: static DL mul,quo,minlm;
1.44 noro 2861: static int nvar = 0;
1.41 noro 2862:
2863: if ( nvar < nd_nvar ) {
2864: nvar = nd_nvar;
2865: NEWDL(quo,nvar); NEWDL(mul,nvar);
2866: NEWDL(minlm,nvar);
2867: }
2868: sig = l->sig;
2869: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2870: _ndltodl(l->lcm,minlm); minindex = -1;
2871: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2872: i = (long)BDY(t);
2873: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2874: _ndltodl(DL(nd_psh[i]),mul);
2875: _addtodl(nd_nvar,quo,mul);
2876: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2877: minindex = i;
1.47 noro 2878: break;
1.41 noro 2879: _copydl(nd_nvar,mul,minlm);
2880: }
2881: }
2882: }
2883: // l->lcm is minimal; return l itself
2884: if ( minindex < 0 ) return l;
1.47 noro 2885: else return 0;
1.41 noro 2886: for ( i = 0; i < nd_psn; i++ ) {
2887: if ( i == minindex ) continue;
2888: _ndltodl(DL(nd_psh[i]),mul);
2889: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2890: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2891: sig1.pos = nd_ps[i]->sig->pos;
2892: sig1.dl = quo;
2893: if ( comp_sig(sig,&sig1) > 0 ) {
2894: // printf("X");
2895: NEWND_pairs(r);
2896: r->sig = sig;
2897: r->i1 = minindex;
2898: r->i2 = i;
2899: dltondl(nd_nvar,minlm,r->lcm);
2900: r->next = 0;
2901: return r;
2902: }
2903: }
2904: }
2905: // there is no suitable spair
2906: return 0;
2907: }
2908:
1.28 noro 2909: ND_pairs remove_large_lcm(ND_pairs d)
2910: {
2911: struct oND_pairs root;
2912: ND_pairs prev,p;
2913:
2914: root.next = d;
2915: prev = &root; p = d;
2916: while ( p ) {
1.41 noro 2917: #if 0
1.28 noro 2918: if ( small_lcm(p) ) {
2919: // remove p
2920: prev->next = p->next;
2921: } else
1.41 noro 2922: #else
2923: if ( find_smallest_lcm(p) == 0 ) {
2924: // remove p
2925: prev->next = p->next;
2926: } else
2927: #endif
1.28 noro 2928: prev = p;
2929: p = p->next;
2930: }
2931: return (ND_pairs)root.next;
2932: }
2933:
1.26 noro 2934: struct oEGT eg_create,eg_newpairs,eg_merge;
2935:
1.30 noro 2936: NODE conv_ilist_s(int demand,int trace,int **indp);
2937:
1.44 noro 2938: // S(fj*ei-fi*ej)
2939:
1.45 noro 2940: void _subdl(int,DL,DL,DL);
2941:
1.44 noro 2942: SIG trivial_sig(int i,int j)
2943: {
1.45 noro 2944: static DL lcm;
1.44 noro 2945: static struct oSIG sigi,sigj;
2946: static int nvar = 0;
2947: SIG sig;
2948:
2949: if ( nvar != nd_nvar ) {
1.45 noro 2950: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
2951: }
1.46 noro 2952: if ( nd_sba_inputisgb != 0 ) {
1.45 noro 2953: lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
2954: sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
2955: sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
2956: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2957: else sig = dup_sig(&sigj);
2958: } else {
2959: sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
2960: sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
2961: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2962: else sig = dup_sig(&sigj);
1.44 noro 2963: }
2964: return sig;
2965: }
2966:
1.47 noro 2967: int nd_minsig(ND_pairs *d)
2968: {
2969: int min,i,ret;
2970:
2971: min = -1;
2972: for ( i = 0; i < nd_nbase; i++ ) {
2973: if ( d[i] != 0 ) {
2974: if ( min < 0 ) min = i;
2975: else {
2976: ret = comp_sig(d[i]->sig,d[min]->sig);
2977: if ( ret < 0 ) min = i;
2978: }
2979: }
2980: }
2981: return min;
2982: }
2983:
2984: int dlength(ND_pairs d)
2985: {
2986: int i;
2987: for ( i = 0; d; d = d->next, i++ );
2988: return i;
2989: }
2990:
1.44 noro 2991: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24 noro 2992: {
1.39 noro 2993: int i,j,nh,sugar,stat,pos;
1.30 noro 2994: NODE r,t,g;
1.47 noro 2995: ND_pairs *d;
1.41 noro 2996: ND_pairs l,l1;
1.24 noro 2997: ND h,nf,s,head,nf1;
2998: NDV nfv;
2999: Z q;
3000: union oNDC dn,hc;
3001: P cont;
3002: LIST list;
3003: SIG sig;
1.29 noro 3004: NODE *syzlist;
1.47 noro 3005: int ngen,ind;
1.41 noro 3006: int Nnominimal,Nredundant;
1.28 noro 3007: DL lcm,quo,mul;
1.49 noro 3008: struct oHPDATA final_hpdata,current_hpdata;
1.50 noro 3009: struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
3010: struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47 noro 3011: int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24 noro 3012:
1.27 noro 3013: init_eg(&eg_remove);
1.29 noro 3014: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47 noro 3015: d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
3016: nd_nbase = nd_psn;
1.26 noro 3017: Nsyz = 0;
1.24 noro 3018: Nnd_add = 0;
1.41 noro 3019: Nnominimal = 0;
1.26 noro 3020: Nredundant = 0;
1.44 noro 3021: ngen = nd_psn;
1.48 noro 3022: if ( !do_weyl ) {
3023: for ( i = 0; i < nd_psn; i++ )
3024: for ( j = i+1; j < nd_psn; j++ ) {
3025: sig = trivial_sig(i,j);
3026: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
3027: }
1.29 noro 3028: }
1.47 noro 3029: dlen = 0;
1.41 noro 3030: for ( i = 0; i < nd_psn; i++ ) {
1.47 noro 3031: dlen += update_pairs_array_s(d,i,syzlist);
1.41 noro 3032: }
1.24 noro 3033: sugar = 0;
1.39 noro 3034: pos = 0;
1.49 noro 3035: if ( nd_hpdata ) {
3036: setup_hpdata(&final_hpdata,¤t_hpdata);
3037: }
1.26 noro 3038: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50 noro 3039: init_eg(&eg_sp);
1.26 noro 3040: init_eg(&eg_create);
3041: init_eg(&eg_merge);
1.47 noro 3042: init_eg(&eg_minsig);
3043: init_eg(&eg_smallest);
1.29 noro 3044: init_eg(&eg_large);
1.31 noro 3045: init_eg(&eg_nf);
3046: init_eg(&eg_nfzero);
1.50 noro 3047: init_eg(&eg_removecont);
3048: init_eg(&eg_updatepairs);
3049: init_eg(&eg_hpdata);
3050: init_eg(&eg_sbabuch);
3051: get_eg(&eg3);
1.47 noro 3052: while ( 1 ) {
1.52 ! noro 3053: if ( DP_Print && !nd_hpdata && dlen%100 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47 noro 3054: again :
3055: get_eg(&eg1);
3056: ind = nd_minsig(d);
3057: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
3058: if ( ind < 0 ) break;
3059: l = d[ind];
3060: // printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
3061: get_eg(&eg1);
1.41 noro 3062: l1 = find_smallest_lcm(l);
1.47 noro 3063: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41 noro 3064: if ( l1 == 0 ) {
1.47 noro 3065: d[ind] = d[ind]->next; dlen--;
1.50 noro 3066: if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41 noro 3067: Nnominimal++;
3068: continue;
3069: }
3070: if ( SG(l1) != sugar ) {
3071: sugar = SG(l1);
3072: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3073: }
3074: sig = l1->sig;
3075: if ( DP_Print && nd_sba_pot ) {
3076: if ( sig->pos != pos ) {
3077: fprintf(asir_out,"[%d]",sig->pos);
3078: pos = sig->pos;
3079: }
3080: }
1.50 noro 3081: get_eg(&eg1);
1.41 noro 3082: stat = nd_sp(m,0,l1,&h);
1.50 noro 3083: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24 noro 3084: if ( !stat ) {
1.47 noro 3085: nd_reconstruct_s(0,d);
1.24 noro 3086: goto again;
3087: }
1.31 noro 3088: get_eg(&eg1);
1.25 noro 3089: #if USE_GEOBUCKET
1.39 noro 3090: 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 3091: #else
1.39 noro 3092: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 3093: #endif
1.31 noro 3094: get_eg(&eg2);
1.24 noro 3095: if ( !stat ) {
1.47 noro 3096: nd_reconstruct_s(0,d);
1.24 noro 3097: goto again;
3098: } else if ( stat == -1 ) {
1.47 noro 3099: d[ind] = d[ind]->next; dlen--;
1.41 noro 3100: Nnfs++;
1.24 noro 3101: if ( DP_Print ) { printf("S"); fflush(stdout); }
3102: } else if ( nf ) {
1.47 noro 3103: d[ind] = d[ind]->next; dlen--;
1.41 noro 3104: Nnfnz++;
3105: if ( DP_Print ) {
3106: if ( nd_sba_redundant_check ) {
3107: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
3108: Nredundant++;
3109: printf("R");
3110: } else
3111: printf("+");
3112: } else
3113: printf("+");
3114: fflush(stdout);
3115: }
1.31 noro 3116: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 3117: hc = HCU(nf);
1.50 noro 3118: get_eg(&eg1);
1.24 noro 3119: nd_removecont(m,nf);
1.50 noro 3120: get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
1.24 noro 3121: nfv = ndtondv(m,nf); nd_free(nf);
3122: nh = ndv_newps(m,nfv,0);
1.26 noro 3123:
1.50 noro 3124: get_eg(&eg1);
1.47 noro 3125: dlen += update_pairs_array_s(d,nh,syzlist);
1.50 noro 3126: get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
1.29 noro 3127: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.49 noro 3128: if ( nd_hpdata ) {
1.50 noro 3129: get_eg(&eg1);
1.49 noro 3130: update_hpdata(¤t_hpdata,nh,0);
1.50 noro 3131: get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
1.49 noro 3132: if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) ) {
3133: if ( DP_Print ) { printf("\nWe found a gb.\n"); }
3134: break;
3135: }
3136: }
1.24 noro 3137: } else {
1.47 noro 3138: d[ind] = d[ind]->next; dlen--;
1.41 noro 3139: Nnfz++;
1.31 noro 3140: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 3141: // syzygy
1.27 noro 3142: get_eg(&eg1);
1.47 noro 3143: nsyz = Nsyz;
3144: d[sig->pos] = remove_spair_s(d[sig->pos],sig);
3145: dlen -= Nsyz-nsyz;
1.27 noro 3146: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 3147: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 3148: if ( DP_Print ) { printf("."); fflush(stdout); }
3149: }
3150: }
1.50 noro 3151: get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30 noro 3152: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 3153: if ( DP_Print ) {
1.47 noro 3154: printf("\ndlen=%d,nd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",dlen,Nnd_add,Nsyz,Nsamesig,Nnominimal);
1.41 noro 3155: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
1.50 noro 3156: fflush(stdout);
1.41 noro 3157: if ( nd_sba_redundant_check )
3158: printf("Nredundant=%d\n",Nredundant);
1.50 noro 3159: fflush(stdout);
3160: print_eg("sp",&eg_sp);
1.26 noro 3161: print_eg("create",&eg_create);
3162: print_eg("merge",&eg_merge);
1.47 noro 3163: print_eg("minsig",&eg_minsig);
3164: print_eg("smallest",&eg_smallest);
3165: print_eg("remove",&eg_remove);
1.50 noro 3166: printf("\n");
1.47 noro 3167: print_eg("nf",&eg_nf);
3168: print_eg("nfzero",&eg_nfzero);
1.50 noro 3169: print_eg("removecont",&eg_removecont);
3170: print_eg("updatepairs",&eg_updatepairs);
3171: print_eg("hpdata",&eg_hpdata);
3172: print_eg("total",&eg_sbabuch);
1.47 noro 3173: printf("\n");
3174: }
3175: if ( nd_sba_syz ) {
1.27 noro 3176: print_eg("remove",&eg_remove);
1.31 noro 3177: print_eg("nf",&eg_nf);
3178: print_eg("nfzero",&eg_nfzero);
1.27 noro 3179: printf("\n");
1.26 noro 3180: }
1.44 noro 3181: if ( nd_sba_syz ) {
3182: NODE hsyz,tsyz,prev;
3183:
3184: hsyz = 0;
3185: for ( i = 0; i < ngen; i++ ) {
3186: tsyz = syzlist[i];
3187: for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
3188: BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
3189: if ( prev != 0 ) {
3190: prev->next = hsyz; hsyz = syzlist[i];
3191: }
3192: }
3193: *syzp = hsyz;
3194: } else *syzp = 0;
1.24 noro 3195: return g;
3196: }
3197:
1.1 noro 3198: /* splist = [[i1,i2],...] */
3199:
3200: int check_splist(int m,NODE splist)
3201: {
3202: NODE t,p;
3203: ND_pairs d,r,l;
3204: int stat;
3205: ND h,nf;
3206:
3207: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3208: p = BDY((LIST)BDY(t));
1.6 noro 3209: NEXTND_pairs(d,r);
3210: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3211: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3212: SG(r) = TD(LCM(r)); /* XXX */
3213: }
3214: if ( d ) NEXT(r) = 0;
3215:
1.6 noro 3216: while ( d ) {
1.1 noro 3217: again:
1.6 noro 3218: l = nd_minp(d,&d);
3219: stat = nd_sp(m,0,l,&h);
3220: if ( !stat ) {
3221: NEXT(l) = d; d = l;
3222: d = nd_reconstruct(0,d);
3223: goto again;
3224: }
1.39 noro 3225: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 3226: if ( !stat ) {
3227: NEXT(l) = d; d = l;
3228: d = nd_reconstruct(0,d);
3229: goto again;
3230: } else if ( nf ) return 0;
1.1 noro 3231: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 3232: }
1.1 noro 3233: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
3234: return 1;
3235: }
3236:
3237: int check_splist_f4(int m,NODE splist)
3238: {
3239: UINT *s0vect;
1.6 noro 3240: PGeoBucket bucket;
1.1 noro 3241: NODE p,rp0,t;
3242: ND_pairs d,r,l,ll;
3243: int col,stat;
3244:
3245: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3246: p = BDY((LIST)BDY(t));
1.6 noro 3247: NEXTND_pairs(d,r);
3248: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3249: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3250: SG(r) = TD(LCM(r)); /* XXX */
3251: }
3252: if ( d ) NEXT(r) = 0;
3253:
1.6 noro 3254: while ( d ) {
3255: l = nd_minsugarp(d,&d);
3256: bucket = create_pbucket();
3257: stat = nd_sp_f4(m,0,l,bucket);
3258: if ( !stat ) {
3259: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3260: NEXT(ll) = d; d = l;
3261: d = nd_reconstruct(0,d);
3262: continue;
3263: }
3264: if ( bucket->m < 0 ) continue;
3265: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
3266: if ( !col ) {
3267: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3268: NEXT(ll) = d; d = l;
3269: d = nd_reconstruct(0,d);
3270: continue;
1.1 noro 3271: }
1.6 noro 3272: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
3273: }
3274: return 1;
1.1 noro 3275: }
3276:
3277: int do_diagonalize_trace(int sugar,int m)
3278: {
1.6 noro 3279: int i,nh,stat;
3280: NODE r,g,t;
3281: ND h,nf,nfq,s,head;
3282: NDV nfv,nfqv;
3283: Q q,den,num;
3284: union oNDC hc;
3285: NODE node;
3286: LIST l;
3287: Z iq;
3288: P cont,cont1;
1.1 noro 3289:
1.6 noro 3290: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3291: if ( nd_gentrace ) {
3292: /* Trace = [1,index,1,1] */
3293: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3294: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3295: }
3296: /* for nd_ps */
3297: s = ndvtond(m,nd_ps[i]);
3298: s = nd_separate_head(s,&head);
3299: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3300: if ( !stat ) return 0;
3301: nf = nd_add(m,head,nf);
3302: ndv_free(nd_ps[i]);
3303: nd_ps[i] = ndtondv(m,nf);
3304: nd_free(nf);
3305:
3306: /* for nd_ps_trace */
3307: if ( nd_demand )
3308: nfv = ndv_load(i);
3309: else
3310: nfv = nd_ps_trace[i];
3311: s = ndvtond(0,nfv);
3312: s = nd_separate_head(s,&head);
3313: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3314: if ( !stat ) return 0;
3315: ndv_free(nfv);
3316: hc = HCU(nf); nd_removecont(0,nf);
3317: /* exact division */
1.1 noro 3318: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3319: if ( nd_gentrace ) finalize_tracelist(i,cont);
3320: nfv = ndtondv(0,nf);
3321: nd_free(nf);
3322: nd_bound[i] = ndv_compute_bound(nfv);
3323: register_hcf(nfv);
3324: if ( nd_demand ) {
3325: ndv_save(nfv,i);
3326: ndv_free(nfv);
3327: } else
3328: nd_ps_trace[i] = nfv;
3329: }
3330: return 1;
1.1 noro 3331: }
3332:
3333: static struct oEGT eg_invdalg;
3334: struct oEGT eg_le;
3335:
3336: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3337: {
3338: NODE tn;
3339: P p1;
3340:
3341: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3342: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3343: }
3344: *r = p;
3345: }
3346:
3347: NODE nd_gb_trace(int m,int ishomo,int **indp)
3348: {
1.6 noro 3349: int i,nh,sugar,stat;
3350: NODE r,g,t;
3351: ND_pairs d;
3352: ND_pairs l;
3353: ND h,nf,nfq,s,head;
3354: NDV nfv,nfqv;
3355: Z q,den,num;
3356: P hc;
3357: union oNDC dn,hnfq;
3358: struct oEGT eg_monic,egm0,egm1;
3359: int diag_count = 0;
3360: P cont;
3361: LIST list;
1.49 noro 3362: struct oHPDATA current_hpdata,final_hpdata;
3363: int final_hpvalue;
1.6 noro 3364:
3365: init_eg(&eg_monic);
3366: init_eg(&eg_invdalg);
3367: init_eg(&eg_le);
3368: g = 0; d = 0;
3369: for ( i = 0; i < nd_psn; i++ ) {
3370: d = update_pairs(d,g,i,0);
3371: g = update_base(g,i);
3372: }
3373: sugar = 0;
1.49 noro 3374: if ( nd_hpdata ) {
1.52 ! noro 3375: if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49 noro 3376: setup_hpdata(&final_hpdata,¤t_hpdata);
3377: }
3378:
1.6 noro 3379: while ( d ) {
1.1 noro 3380: again:
1.6 noro 3381: l = nd_minp(d,&d);
3382: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3383: if ( SG(l) != sugar ) {
1.1 noro 3384: #if 1
1.6 noro 3385: if ( ishomo ) {
3386: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3387: stat = do_diagonalize_trace(sugar,m);
3388: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3389: diag_count = 0;
1.1 noro 3390: if ( !stat ) {
1.6 noro 3391: NEXT(l) = d; d = l;
3392: d = nd_reconstruct(1,d);
3393: goto again;
1.1 noro 3394: }
1.6 noro 3395: }
3396: #endif
3397: sugar = SG(l);
3398: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49 noro 3399: if ( nd_hpdata ) {
3400: if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
3401: break;
3402: else {
3403: final_hpvalue = hpvalue(&final_hpdata,sugar);
3404: if ( final_hpvalue == hpvalue(¤t_hpdata,sugar) ) {
1.52 ! noro 3405: // if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49 noro 3406: d = nd_remove_same_sugar(d,sugar);
3407: continue;
3408: }
3409: }
3410: }
1.6 noro 3411: }
3412: stat = nd_sp(m,0,l,&h);
3413: if ( !stat ) {
3414: NEXT(l) = d; d = l;
3415: d = nd_reconstruct(1,d);
3416: goto again;
3417: }
1.1 noro 3418: #if USE_GEOBUCKET
1.39 noro 3419: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3420: #else
1.39 noro 3421: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3422: #endif
1.6 noro 3423: if ( !stat ) {
3424: NEXT(l) = d; d = l;
3425: d = nd_reconstruct(1,d);
3426: goto again;
3427: } else if ( nf ) {
3428: if ( nd_demand ) {
3429: nfqv = ndv_load(nd_psn);
3430: nfq = ndvtond(0,nfqv);
3431: } else
3432: nfq = 0;
3433: if ( !nfq ) {
1.39 noro 3434: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3435: NEXT(l) = d; d = l;
3436: d = nd_reconstruct(1,d);
3437: goto again;
3438: }
3439: }
3440: if ( nfq ) {
3441: /* m|HC(nfq) => failure */
3442: if ( nd_vc ) {
3443: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3444: } else
3445: q = HCZ(nfq);
3446: if ( !remqi((Q)q,m) ) return 0;
3447:
3448: if ( DP_Print ) { printf("+"); fflush(stdout); }
3449: hnfq = HCU(nfq);
3450: if ( nd_nalg ) {
3451: /* m|DN(HC(nf)^(-1)) => failure */
3452: get_eg(&egm0);
3453: if ( !nd_monic(m,&nfq) ) return 0;
3454: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3455: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3456: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3457: } else {
3458: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3459: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3460: }
3461: if ( nd_gentrace ) {
3462: /* exact division */
3463: cont = ndc_div(0,hnfq,HCU(nfqv));
3464: if ( !UNIQ(cont) ) {
3465: t = mknode(4,NULLP,NULLP,NULLP,cont);
3466: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3467: nd_tracelist = t;
3468: }
3469: }
1.24 noro 3470: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3471: if ( ishomo && ++diag_count == diag_period ) {
3472: diag_count = 0;
3473: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3474: stat = do_diagonalize_trace(sugar,m);
3475: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3476: if ( !stat ) {
1.1 noro 3477: NEXT(l) = d; d = l;
3478: d = nd_reconstruct(1,d);
3479: goto again;
1.6 noro 3480: }
1.1 noro 3481: }
1.6 noro 3482: d = update_pairs(d,g,nh,0);
3483: g = update_base(g,nh);
1.49 noro 3484: if ( nd_hpdata ) {
3485: update_hpdata(¤t_hpdata,nh,1);
3486: if ( final_hpvalue == hpvalue(¤t_hpdata,sugar) ) {
1.52 ! noro 3487: // if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49 noro 3488: d = nd_remove_same_sugar(d,sugar);
3489: }
3490: }
1.6 noro 3491: } else {
3492: if ( DP_Print ) { printf("*"); fflush(stdout); }
3493: }
3494: } else {
3495: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3496: }
1.6 noro 3497: FREENDP(l);
3498: }
3499: if ( nd_nalg ) {
3500: if ( DP_Print ) {
3501: print_eg("monic",&eg_monic);
3502: print_eg("invdalg",&eg_invdalg);
3503: print_eg("le",&eg_le);
1.1 noro 3504: }
1.6 noro 3505: }
1.1 noro 3506: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3507: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3508: return g;
1.1 noro 3509: }
3510:
3511: int ndv_compare(NDV *p1,NDV *p2)
3512: {
3513: return DL_COMPARE(HDL(*p1),HDL(*p2));
3514: }
3515:
3516: int ndv_compare_rev(NDV *p1,NDV *p2)
3517: {
3518: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3519: }
3520:
3521: int ndvi_compare(NDVI p1,NDVI p2)
3522: {
3523: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3524: }
3525:
3526: int ndvi_compare_rev(NDVI p1,NDVI p2)
3527: {
3528: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3529: }
3530:
3531: NODE ndv_reduceall(int m,NODE f)
3532: {
3533: int i,j,n,stat;
3534: ND nf,g,head;
3535: NODE t,a0,a;
3536: union oNDC dn;
3537: Q q,num,den;
3538: NODE node;
3539: LIST l;
3540: Z iq,jq;
3541: int *perm;
3542: union oNDC hc;
3543: P cont,cont1;
3544:
3545: if ( nd_nora ) return f;
3546: n = length(f);
1.24 noro 3547: ndv_setup(m,0,f,0,1,0);
1.1 noro 3548: perm = (int *)MALLOC(n*sizeof(int));
3549: if ( nd_gentrace ) {
3550: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3551: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3552: }
3553: for ( i = 0; i < n; ) {
3554: if ( nd_gentrace ) {
3555: /* Trace = [1,index,1,1] */
1.6 noro 3556: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3557: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3558: }
3559: g = ndvtond(m,nd_ps[i]);
3560: g = nd_separate_head(g,&head);
1.6 noro 3561: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3562: if ( !stat )
3563: nd_reconstruct(0,0);
3564: else {
3565: if ( DP_Print ) { printf("."); fflush(stdout); }
3566: ndv_free(nd_ps[i]);
3567: hc = HCU(nf); nd_removecont(m,nf);
3568: if ( nd_gentrace ) {
3569: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3570: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3571: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3572: }
1.6 noro 3573: /* exact division */
1.1 noro 3574: cont = ndc_div(m,hc,HCU(nf));
3575: finalize_tracelist(perm[i],cont);
3576: }
3577: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3578: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3579: i++;
3580: }
3581: }
3582: if ( DP_Print ) { printf("\n"); }
3583: for ( a0 = 0, i = 0; i < n; i++ ) {
3584: NEXTNODE(a0,a);
3585: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3586: else {
3587: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3588: BDY(a) = (pointer)nd_ps[j];
3589: }
3590: }
3591: NEXT(a) = 0;
3592: return a0;
3593: }
3594:
1.41 noro 3595: int ndplength(ND_pairs d)
3596: {
3597: int i;
3598: for ( i = 0; d; i++ ) d = NEXT(d);
3599: return i;
3600: }
3601:
1.1 noro 3602: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3603: {
3604: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3605: int len0;
1.1 noro 3606:
3607: if ( !g ) return d;
3608: /* for testing */
3609: if ( gensyz && nd_gensyz == 2 ) {
3610: d1 = nd_newpairs(g,t);
3611: if ( !d )
3612: return d1;
3613: else {
3614: nd = d;
3615: while ( NEXT(nd) ) nd = NEXT(nd);
3616: NEXT(nd) = d1;
3617: return d;
3618: }
3619: }
3620: d = crit_B(d,t);
3621: d1 = nd_newpairs(g,t);
1.41 noro 3622: len0 = ndplength(d1);
1.1 noro 3623: d1 = crit_M(d1);
3624: d1 = crit_F(d1);
1.41 noro 3625: NcriMF += len0-ndplength(d1);
1.1 noro 3626: if ( gensyz || do_weyl )
3627: head = d1;
3628: else {
3629: prev = 0; cur = head = d1;
3630: while ( cur ) {
3631: if ( crit_2( cur->i1,cur->i2 ) ) {
3632: remove = cur;
3633: if ( !prev ) head = cur = NEXT(cur);
3634: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3635: FREENDP(remove); Ncri2++;
1.1 noro 3636: } else {
3637: prev = cur; cur = NEXT(cur);
3638: }
3639: }
3640: }
3641: if ( !d )
3642: return head;
3643: else {
3644: nd = d;
3645: while ( NEXT(nd) ) nd = NEXT(nd);
3646: NEXT(nd) = head;
3647: return d;
3648: }
3649: }
3650:
1.24 noro 3651: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3652:
1.30 noro 3653: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3654: {
3655: ND_pairs d1;
1.26 noro 3656: struct oEGT eg1,eg2,eg3;
1.24 noro 3657:
1.30 noro 3658: if ( !t ) return d;
1.26 noro 3659: get_eg(&eg1);
1.30 noro 3660: d1 = nd_newpairs_s(t,syz);
1.26 noro 3661: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3662: d = merge_pairs_s(d,d1);
1.26 noro 3663: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3664: return d;
3665: }
1.1 noro 3666:
1.47 noro 3667: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
3668: {
3669: ND_pairs *d1;
3670: struct oEGT eg1,eg2,eg3;
3671: int i;
3672:
3673: if ( !t ) return 0;
3674: get_eg(&eg1);
3675: Nnewpair = 0;
3676: d1 = nd_newpairs_array_s(t,syz);
3677: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
3678: for ( i = 0; i < nd_nbase; i++ )
3679: d[i] = merge_pairs_s(d[i],d1[i]);
3680: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
3681: return Nnewpair;
3682: }
3683:
1.1 noro 3684: ND_pairs nd_newpairs( NODE g, int t )
3685: {
3686: NODE h;
3687: UINT *dl;
3688: int ts,s,i,t0,min,max;
3689: ND_pairs r,r0;
3690:
3691: dl = DL(nd_psh[t]);
3692: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3693: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3694: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3695: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3696: continue;
3697: if ( nd_gbblock ) {
3698: t0 = (long)BDY(h);
3699: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3700: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3701: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3702: break;
3703: }
3704: if ( nd_gbblock[i] >= 0 )
3705: continue;
3706: }
1.41 noro 3707: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3708: r->i1 = (long)BDY(h);
3709: r->i2 = t;
3710: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3711: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3712: SG(r) = MAX(s,ts) + TD(LCM(r));
3713: /* experimental */
3714: if ( nd_sugarweight )
3715: r->sugar2 = ndl_weight2(r->lcm);
3716: }
3717: if ( r0 ) NEXT(r) = 0;
3718: return r0;
3719: }
3720:
1.46 noro 3721: int sig_cmpdl_op(int n,DL d1,DL d2)
3722: {
3723: int e1,e2,i,j,l;
3724: int *t1,*t2;
3725: int len,head;
3726: struct order_pair *pair;
3727:
3728: len = nd_sba_modord->block_length;
3729: pair = nd_sba_modord->order_pair;
3730:
3731: head = 0;
3732: for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
3733: l = pair[i].length;
3734: switch ( pair[i].order ) {
3735: case 0:
3736: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3737: e1 += t1[j];
3738: e2 += t2[j];
3739: }
3740: if ( e1 > e2 )
3741: return 1;
3742: else if ( e1 < e2 )
3743: return -1;
3744: else {
3745: for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
3746: if ( j >= 0 )
3747: return t1[j] < t2[j] ? 1 : -1;
3748: }
3749: break;
3750: case 1:
3751: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3752: e1 += t1[j];
3753: e2 += t2[j];
3754: }
3755: if ( e1 > e2 )
3756: return 1;
3757: else if ( e1 < e2 )
3758: return -1;
3759: else {
3760: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3761: if ( j < l )
3762: return t1[j] > t2[j] ? 1 : -1;
3763: }
3764: break;
3765: case 2:
3766: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3767: if ( j < l )
3768: return t1[j] > t2[j] ? 1 : -1;
3769: break;
3770: default:
3771: error("sig_cmpdl_op : invalid order"); break;
3772: }
3773: t1 += l; t2 += l; head += l;
3774: }
3775: return 0;
3776: }
3777:
3778: int sig_cmpdl_mat(int n,DL d1,DL d2)
3779: {
3780: int *v,*t1,*t2;
3781: int s,i,j,len;
3782: int **matrix;
3783: static int *w;
3784: static int nvar = 0;
3785:
3786: if ( nvar != n ) {
3787: nvar = n; w = (int *)MALLOC(n*sizeof(int));
3788: }
3789: for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
3790: w[i] = t1[i]-t2[i];
3791: len = nd_sba_modord->row;
3792: matrix = nd_sba_modord->matrix;
3793: for ( j = 0; j < len; j++ ) {
3794: v = matrix[j];
3795: for ( i = 0, s = 0; i < n; i++ )
3796: s += v[i]*w[i];
3797: if ( s > 0 )
3798: return 1;
3799: else if ( s < 0 )
3800: return -1;
3801: }
3802: return 0;
3803: }
3804:
3805: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
3806: {
3807: struct comp_sig_spec *spec;
3808: VL ovl,vl;
3809: V ov;
3810: int i,j,n,nvar,s;
3811: NODE node,t,tn;
3812: struct order_pair *l;
3813: MAT m;
3814: Obj **b;
3815: int **w;
3816: int *a;
3817:
3818: spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
3819: for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
3820: spec->n = nvar = i;
3821: if ( old_vl != 0 ) {
3822: spec->oldv = (int *)MALLOC(nvar*sizeof(int));
3823: for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
3824: ov = ovl->v;
3825: for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
3826: if ( ov == vl->v ) break;
3827: spec->oldv[i] = j;
3828: }
3829: } else
3830: spec->oldv = 0;
3831: if ( !ord || NUM(ord) ) {
3832: switch ( ZTOS((Z)ord) ) {
3833: case 0:
3834: spec->cmpdl = cmpdl_revgradlex; break;
3835: case 1:
3836: spec->cmpdl = cmpdl_gradlex; break;
3837: case 2:
3838: spec->cmpdl = cmpdl_lex; break;
3839: default:
3840: error("create_comp_sig_spec : invalid spec"); break;
3841: }
3842: } else if ( OID(ord) == O_LIST ) {
3843: node = BDY((LIST)ord);
3844: for ( n = 0, t = node; t; t = NEXT(t), n++ );
3845: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
3846: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
3847: tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
3848: tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
3849: s += l[i].length;
3850: }
3851: if ( s != nvar )
3852: error("create_comp_sig_spec : invalid spec");
3853: spec->order_pair = l;
3854: spec->block_length = n;
3855: spec->cmpdl = sig_cmpdl_op;
3856: } else if ( OID(ord) == O_MAT ) {
3857: m = (MAT)ord; b = (Obj **)BDY(m);
3858: if ( m->col != nvar )
3859: error("create_comp_sig_spec : invalid spec");
3860: w = almat(m->row,m->col);
3861: for ( i = 0; i < m->row; i++ )
3862: for ( j = 0; j < m->col; j++ )
3863: w[i][j] = ZTOS((Q)b[i][j]);
3864: spec->row = m->row;
3865: spec->matrix = w;
3866: spec->cmpdl = sig_cmpdl_mat;
3867: } else
3868: error("create_comp_sig_spec : invalid spec");
3869: if ( weight != 0 ) {
3870: node = BDY((LIST)weight);
3871: a = (int *)MALLOC(nvar*sizeof(int));
3872: for ( i = 0; i < nvar; i++, node = NEXT(node) )
3873: a[i] = ZTOS((Z)BDY(node));
3874: spec->weight = a;
3875: }
3876: return spec;
3877: }
3878:
3879: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
3880:
3881: int comp_sig_monomial(int n,DL d1,DL d2)
3882: {
3883: static DL m1,m2;
3884: static int nvar = 0;
3885: int *oldv,*weight;
3886: int i,w1,w2;
3887:
3888: if ( nvar != n ) {
3889: nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
3890: }
3891: if ( !nd_sba_modord )
3892: return (*cmpdl)(n,d1,d2);
3893: else {
3894: weight = nd_sba_modord->weight;
3895: oldv = nd_sba_modord->oldv;
3896: if ( oldv ) {
3897: for ( i = 0; i < n; i++ ) {
3898: m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
3899: }
3900: } else {
3901: for ( i = 0; i < n; i++ ) {
3902: m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
3903: }
3904: }
3905: for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
3906: w1 += SIG_MUL_WEIGHT(m1->d[i],i);
3907: w2 += SIG_MUL_WEIGHT(m2->d[i],i);
3908: }
3909: m1->td = w1; m2->td = w2;
3910: return (*nd_sba_modord->cmpdl)(n,m1,m2);
3911: }
3912: }
3913:
1.24 noro 3914: int comp_sig(SIG s1,SIG s2)
3915: {
1.38 noro 3916: if ( nd_sba_pot ) {
3917: if ( s1->pos > s2->pos ) return 1;
3918: else if ( s1->pos < s2->pos ) return -1;
1.46 noro 3919: else return comp_sig_monomial(nd_nvar,s1->dl,s2->dl);
1.38 noro 3920: } else {
3921: static DL m1,m2;
1.44 noro 3922: static int nvar = 0;
1.38 noro 3923: int ret;
3924:
3925: if ( nvar != nd_nvar ) {
3926: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3927: }
3928: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3929: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.47 noro 3930: if ( !nd_sba_modord )
3931: ret = (*cmpdl)(nd_nvar,m1,m2);
3932: else
3933: ret = comp_sig_monomial(nd_nvar,m1,m2);
1.38 noro 3934: if ( ret != 0 ) return ret;
3935: else if ( s1->pos > s2->pos ) return 1;
3936: else if ( s1->pos < s2->pos ) return -1;
3937: else return 0;
1.24 noro 3938: }
3939: }
3940:
3941: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3942: {
3943: int ret,s1,s2;
3944: RHist p1,p2;
3945: static int wpd;
3946: static UINT *lcm;
3947:
3948: sp->i1 = i1;
3949: sp->i2 = i2;
3950: p1 = nd_psh[i1];
3951: p2 = nd_psh[i2];
3952: ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49 noro 3953: #if 0
1.24 noro 3954: s1 = SG(p1)-TD(DL(p1));
3955: s2 = SG(p2)-TD(DL(p2));
3956: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49 noro 3957: #endif
1.24 noro 3958:
1.26 noro 3959: if ( wpd != nd_wpd ) {
1.24 noro 3960: wpd = nd_wpd;
3961: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3962: }
3963: // DL(sig1) <- sp->lcm
3964: // DL(sig1) -= DL(p1)
3965: // DL(sig1) += DL(p1->sig)
1.27 noro 3966: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3967: _ndltodl(lcm,DL(sig1));
3968: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3969: sig1->pos = p1->sig->pos;
3970:
3971: // DL(sig2) <- sp->lcm
3972: // DL(sig2) -= DL(p2)
3973: // DL(sig2) += DL(p2->sig)
1.27 noro 3974: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3975: _ndltodl(lcm,DL(sig2));
3976: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3977: sig2->pos = p2->sig->pos;
3978:
3979: ret = comp_sig(sig1,sig2);
3980: if ( ret == 0 ) return 0;
3981: else if ( ret > 0 ) sp->sig = sig1;
3982: else sp->sig = sig2;
1.49 noro 3983:
3984: s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
3985: s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
3986: SG(sp) = MAX(s1,s2);
3987:
1.24 noro 3988: return 1;
3989: }
3990:
3991: SIG dup_sig(SIG sig)
3992: {
3993: SIG r;
3994:
3995: if ( !sig ) return 0;
3996: else {
3997: NEWSIG(r);
3998: _copydl(nd_nvar,DL(sig),DL(r));
3999: r->pos = sig->pos;
4000: return r;
4001: }
4002: }
4003:
4004: void dup_ND_pairs(ND_pairs to,ND_pairs from)
4005: {
4006: to->i1 = from->i1;
4007: to->i2 = from->i2;
4008: to->sugar = from->sugar;
4009: to->sugar2 = from->sugar2;
4010: ndl_copy(from->lcm,to->lcm);
4011: to->sig = dup_sig(from->sig);
4012: }
4013:
4014: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
4015: {
4016: struct oND_pairs root;
4017: ND_pairs q1,q2,r0,r;
4018: int ret;
4019:
4020: r = &root;
4021: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
4022: ret = comp_sig(q1->sig,q2->sig);
4023: if ( ret < 0 ) {
4024: r->next = q1; r = q1; q1 = q1->next;
4025: } else if ( ret > 0 ) {
4026: r->next = q2; r = q2; q2 = q2->next;
4027: } else {
1.47 noro 4028: Nnewpair--;
1.24 noro 4029: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 4030: Nsamesig++;
1.24 noro 4031: if ( ret < 0 ) {
4032: r->next = q1; r = q1; q1 = q1->next;
4033: q2 = q2->next;
4034: } else {
4035: r->next = q2; r = q2; q2 = q2->next;
4036: q1 = q1->next;
4037: }
4038: }
4039: }
4040: if ( q1 ) {
4041: r->next = q1;
4042: } else {
4043: r->next = q2;
4044: }
4045: return root.next;
4046: }
4047:
4048: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
4049: {
4050: ND_pairs p,prev;
1.47 noro 4051: int ret=1;
1.24 noro 4052:
4053: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
4054: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
4055: break;
4056: }
4057: if ( ret == 0 ) {
4058: ret = DL_COMPARE(s->lcm,p->lcm);
4059: if ( ret < 0 ) {
4060: // replace p with s
4061: s->next = p->next;
4062: if ( prev == 0 ) {
4063: return s;
4064: } else {
4065: prev->next = s;
4066: return l;
4067: }
4068: } else
4069: return l;
4070: } else {
4071: // insert s between prev and p
1.47 noro 4072: Nnewpair++;
1.24 noro 4073: s->next = p;
4074: if ( prev == 0 ) {
4075: return s;
4076: } else {
4077: prev->next = s;
4078: return l;
4079: }
4080: }
4081: }
4082:
1.29 noro 4083: INLINE int __dl_redble(DL d1,DL d2,int nvar)
4084: {
4085: int i;
4086:
4087: if ( d1->td > d2->td )
4088: return 0;
4089: for ( i = nvar-1; i >= 0; i-- )
4090: if ( d1->d[i] > d2->d[i] )
4091: break;
4092: if ( i >= 0 )
4093: return 0;
4094: else
4095: return 1;
4096: }
4097:
1.30 noro 4098: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 4099: {
4100: NODE h,s;
4101: UINT *dl;
1.27 noro 4102: int ts,ret,i;
1.24 noro 4103: ND_pairs r,r0,_sp,sp;
1.29 noro 4104: SIG spsig,tsig;
1.44 noro 4105: static int nvar = 0;
1.29 noro 4106: static SIG _sig1,_sig2;
1.26 noro 4107: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 4108:
4109: NEWND_pairs(_sp);
1.29 noro 4110: if ( !_sig1 || nvar != nd_nvar ) {
4111: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
4112: }
1.24 noro 4113: r0 = 0;
1.27 noro 4114: for ( i = 0; i < t; i++ ) {
4115: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 4116: if ( ret ) {
4117: spsig = _sp->sig;
1.29 noro 4118: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 4119: tsig = (SIG)s->body;
1.29 noro 4120: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 4121: break;
4122: }
4123: if ( s == 0 ) {
4124: NEWND_pairs(sp);
4125: dup_ND_pairs(sp,_sp);
4126: r0 = insert_pair_s(r0,sp);
1.26 noro 4127: } else
4128: Nsyz++;
1.24 noro 4129: }
4130: }
4131: return r0;
4132: }
4133:
1.47 noro 4134: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
4135: {
4136: NODE h,s;
4137: UINT *dl;
4138: int ts,ret,i;
4139: ND_pairs r,r0,_sp,sp;
4140: ND_pairs *d;
4141: SIG spsig,tsig;
4142: static int nvar = 0;
4143: static SIG _sig1,_sig2;
4144: struct oEGT eg1,eg2,eg3,eg4;
4145:
4146: NEWND_pairs(_sp);
4147: if ( !_sig1 || nvar != nd_nvar ) {
4148: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
4149: }
4150: d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
4151: Nnewpair = 0;
4152: for ( i = 0; i < t; i++ ) {
4153: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
4154: if ( ret ) {
4155: spsig = _sp->sig;
4156: for ( s = syz[spsig->pos]; s; s = s->next ) {
4157: tsig = (SIG)s->body;
4158: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
4159: break;
4160: }
4161: if ( s == 0 ) {
4162: NEWND_pairs(sp);
4163: dup_ND_pairs(sp,_sp);
4164: d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
4165: } else
4166: Nsyz++;
4167: }
4168: }
4169: return d;
4170: }
4171:
1.1 noro 4172: /* ipair = [i1,i2],[i1,i2],... */
4173: ND_pairs nd_ipairtospair(NODE ipair)
4174: {
4175: int s1,s2;
4176: NODE tn,t;
4177: ND_pairs r,r0;
4178:
4179: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
4180: NEXTND_pairs(r0,r);
4181: tn = BDY((LIST)BDY(t));
1.6 noro 4182: r->i1 = ZTOS((Q)ARG0(tn));
4183: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 4184: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
4185: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
4186: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
4187: SG(r) = MAX(s1,s2) + TD(LCM(r));
4188: /* experimental */
4189: if ( nd_sugarweight )
4190: r->sugar2 = ndl_weight2(r->lcm);
4191: }
4192: if ( r0 ) NEXT(r) = 0;
4193: return r0;
4194: }
4195:
4196: /* kokokara */
4197:
4198: ND_pairs crit_B( ND_pairs d, int s )
4199: {
4200: ND_pairs cur,head,prev,remove;
4201: UINT *t,*tl,*lcm;
4202: int td,tdl;
4203:
4204: if ( !d ) return 0;
4205: t = DL(nd_psh[s]);
4206: prev = 0;
4207: head = cur = d;
4208: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4209: while ( cur ) {
4210: tl = cur->lcm;
4211: if ( ndl_reducible(tl,t) ) {
4212: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
4213: if ( !ndl_equal(lcm,tl) ) {
4214: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
4215: if (!ndl_equal(lcm,tl)) {
4216: remove = cur;
4217: if ( !prev ) {
4218: head = cur = NEXT(cur);
4219: } else {
4220: cur = NEXT(prev) = NEXT(cur);
4221: }
1.41 noro 4222: FREENDP(remove); NcriB++;
1.1 noro 4223: } else {
4224: prev = cur; cur = NEXT(cur);
4225: }
4226: } else {
4227: prev = cur; cur = NEXT(cur);
4228: }
4229: } else {
4230: prev = cur; cur = NEXT(cur);
4231: }
4232: }
4233: return head;
4234: }
4235:
4236: ND_pairs crit_M( ND_pairs d1 )
4237: {
4238: ND_pairs e,d2,d3,dd,p;
4239: UINT *id,*jd;
4240:
4241: if ( !d1 ) return d1;
4242: for ( dd = 0, e = d1; e; e = d3 ) {
4243: if ( !(d2 = NEXT(e)) ) {
4244: NEXT(e) = dd;
4245: return e;
4246: }
4247: id = LCM(e);
4248: for ( d3 = 0; d2; d2 = p ) {
4249: p = NEXT(d2);
4250: jd = LCM(d2);
4251: if ( ndl_equal(jd,id) )
4252: ;
4253: else if ( TD(jd) > TD(id) )
4254: if ( ndl_reducible(jd,id) ) continue;
4255: else ;
4256: else if ( ndl_reducible(id,jd) ) goto delit;
4257: NEXT(d2) = d3;
4258: d3 = d2;
4259: }
4260: NEXT(e) = dd;
4261: dd = e;
4262: continue;
4263: /**/
4264: delit: NEXT(d2) = d3;
4265: d3 = d2;
4266: for ( ; p; p = d2 ) {
4267: d2 = NEXT(p);
4268: NEXT(p) = d3;
4269: d3 = p;
4270: }
4271: FREENDP(e);
4272: }
4273: return dd;
4274: }
4275:
4276: ND_pairs crit_F( ND_pairs d1 )
4277: {
4278: ND_pairs rest, head,remove;
4279: ND_pairs last, p, r, w;
4280: int s;
4281:
4282: if ( !d1 ) return d1;
4283: for ( head = last = 0, p = d1; NEXT(p); ) {
4284: r = w = equivalent_pairs(p,&rest);
4285: s = SG(r);
4286: w = NEXT(w);
4287: while ( w ) {
4288: if ( crit_2(w->i1,w->i2) ) {
4289: r = w;
4290: w = NEXT(w);
4291: while ( w ) {
4292: remove = w;
4293: w = NEXT(w);
4294: FREENDP(remove);
4295: }
4296: break;
4297: } else if ( SG(w) < s ) {
4298: FREENDP(r);
4299: r = w;
4300: s = SG(r);
4301: w = NEXT(w);
4302: } else {
4303: remove = w;
4304: w = NEXT(w);
4305: FREENDP(remove);
4306: }
4307: }
4308: if ( last ) NEXT(last) = r;
4309: else head = r;
4310: NEXT(last = r) = 0;
4311: p = rest;
4312: if ( !p ) return head;
4313: }
4314: if ( !last ) return p;
4315: NEXT(last) = p;
4316: return head;
4317: }
4318:
4319: int crit_2( int dp1, int dp2 )
4320: {
4321: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
4322: }
4323:
4324: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
4325: {
4326: ND_pairs w,p,r,s;
4327: UINT *d;
4328:
4329: w = d1;
4330: d = LCM(w);
4331: s = NEXT(w);
4332: NEXT(w) = 0;
4333: for ( r = 0; s; s = p ) {
4334: p = NEXT(s);
4335: if ( ndl_equal(d,LCM(s)) ) {
4336: NEXT(s) = w; w = s;
4337: } else {
4338: NEXT(s) = r; r = s;
4339: }
4340: }
4341: *prest = r;
4342: return w;
4343: }
4344:
4345: NODE update_base(NODE nd,int ndp)
4346: {
4347: UINT *dl, *dln;
4348: NODE last, p, head;
4349:
4350: dl = DL(nd_psh[ndp]);
4351: for ( head = last = 0, p = nd; p; ) {
4352: dln = DL(nd_psh[(long)BDY(p)]);
4353: if ( ndl_reducible( dln, dl ) ) {
4354: p = NEXT(p);
4355: if ( last ) NEXT(last) = p;
4356: } else {
4357: if ( !last ) head = p;
4358: p = NEXT(last = p);
4359: }
4360: }
4361: head = append_one(head,ndp);
4362: return head;
4363: }
4364:
4365: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
4366: {
4367: ND_pairs m,ml,p,l;
4368: UINT *lcm;
4369: int s,td,len,tlen,c,c1;
4370:
4371: if ( !(p = NEXT(m = d)) ) {
4372: *prest = p;
4373: NEXT(m) = 0;
4374: return m;
4375: }
4376: if ( !NoSugar ) {
4377: if ( nd_sugarweight ) {
4378: s = m->sugar2;
4379: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4380: if ( (p->sugar2 < s)
4381: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4382: ml = l; m = p; s = m->sugar2;
4383: }
4384: } else {
4385: s = SG(m);
4386: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4387: if ( (SG(p) < s)
4388: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4389: ml = l; m = p; s = SG(m);
4390: }
4391: }
4392: } else {
4393: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4394: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
4395: ml = l; m = p; s = SG(m);
4396: }
4397: }
4398: if ( !ml ) *prest = NEXT(m);
4399: else {
4400: NEXT(ml) = NEXT(m);
4401: *prest = d;
4402: }
4403: NEXT(m) = 0;
4404: return m;
4405: }
4406:
4407: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
4408: {
4409: int msugar,i;
4410: ND_pairs t,dm0,dm,dr0,dr;
4411:
4412: if ( nd_sugarweight ) {
4413: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
4414: if ( t->sugar2 < msugar ) msugar = t->sugar2;
4415: dm0 = 0; dr0 = 0;
4416: for ( i = 0, t = d; t; t = NEXT(t) )
4417: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
4418: if ( dm0 ) NEXT(dm) = t;
4419: else dm0 = t;
4420: dm = t;
4421: i++;
4422: } else {
4423: if ( dr0 ) NEXT(dr) = t;
4424: else dr0 = t;
4425: dr = t;
4426: }
4427: } else {
4428: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
4429: if ( SG(t) < msugar ) msugar = SG(t);
4430: dm0 = 0; dr0 = 0;
4431: for ( i = 0, t = d; t; t = NEXT(t) )
4432: if ( i < nd_f4_nsp && SG(t) == msugar ) {
4433: if ( dm0 ) NEXT(dm) = t;
4434: else dm0 = t;
4435: dm = t;
4436: i++;
4437: } else {
4438: if ( dr0 ) NEXT(dr) = t;
4439: else dr0 = t;
4440: dr = t;
4441: }
4442: }
4443: NEXT(dm) = 0;
4444: if ( dr0 ) NEXT(dr) = 0;
4445: *prest = dr0;
4446: return dm0;
4447: }
4448:
1.28 noro 4449: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
4450: {
4451: int msugar;
4452: ND_pairs t,last;
4453:
4454: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
4455: if ( SG(t) == msugar ) last = t;
4456: *prest = last->next;
4457: last->next = 0;
4458: return d;
4459: }
4460:
1.1 noro 4461: int nd_tdeg(NDV c)
4462: {
4463: int wmax = 0;
4464: int i,len;
4465: NMV a;
4466:
4467: len = LEN(c);
4468: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4469: wmax = MAX(TD(DL(a)),wmax);
4470: return wmax;
4471: }
4472:
1.24 noro 4473: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 4474: {
4475: int len;
4476: RHist r;
4477: NDV b;
4478: NODE tn;
4479: LIST l;
4480: Z iq;
4481:
4482: if ( nd_psn == nd_pslen ) {
4483: nd_pslen *= 2;
4484: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
4485: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
4486: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
4487: nd_bound = (UINT **)
4488: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
4489: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
4490: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
4491: }
4492: NEWRHist(r); nd_psh[nd_psn] = r;
4493: nd_ps[nd_psn] = a;
4494: if ( aq ) {
4495: nd_ps_trace[nd_psn] = aq;
4496: if ( !m ) {
4497: register_hcf(aq);
4498: } else if ( m == -2 ) {
4499: /* do nothing */
4500: } else
4501: error("ndv_newps : invalud modulus");
4502: nd_bound[nd_psn] = ndv_compute_bound(aq);
4503: #if 1
4504: SG(r) = SG(aq);
4505: #else
4506: SG(r) = nd_tdeg(aq);
4507: #endif
4508: ndl_copy(HDL(aq),DL(r));
1.24 noro 4509: r->sig = dup_sig(aq->sig);
1.1 noro 4510: } else {
4511: if ( !m ) register_hcf(a);
4512: nd_bound[nd_psn] = ndv_compute_bound(a);
4513: #if 1
4514: SG(r) = SG(a);
4515: #else
4516: SG(r) = nd_tdeg(a);
4517: #endif
4518: ndl_copy(HDL(a),DL(r));
1.24 noro 4519: r->sig = dup_sig(a->sig);
1.1 noro 4520: }
4521: if ( nd_demand ) {
4522: if ( aq ) {
4523: ndv_save(nd_ps_trace[nd_psn],nd_psn);
4524: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4525: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4526: nd_ps_trace[nd_psn] = 0;
4527: } else {
4528: ndv_save(nd_ps[nd_psn],nd_psn);
4529: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
4530: nd_ps[nd_psn] = 0;
4531: }
4532: }
4533: if ( nd_gentrace ) {
4534: /* reverse the tracelist and append it to alltracelist */
4535: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 4536: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 4537: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
4538: }
4539: return nd_psn++;
4540: }
4541:
1.46 noro 4542: // find LM wrt the specified modord
4543: void ndv_lm_modord(NDV p,DL d)
1.45 noro 4544: {
4545: NMV m;
4546: DL tmp;
4547: int len,i,ret;
4548:
4549: NEWDL(tmp,nd_nvar);
4550: m = BDY(p); len = LEN(p);
1.46 noro 4551: _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45 noro 4552: for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
4553: _ndltodl(DL(m),tmp);
1.46 noro 4554: ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45 noro 4555: if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
4556: }
1.46 noro 4557: // printdl(d); printf("\n");
1.45 noro 4558: }
4559:
1.1 noro 4560: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
4561: /* return 1 if success, 0 if failure (HC(a mod p)) */
4562:
1.24 noro 4563: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 4564: {
1.6 noro 4565: int i,j,td,len,max;
4566: NODE s,s0,f0,tn;
4567: UINT *d;
4568: RHist r;
4569: NDVI w;
4570: NDV a,am;
4571: union oNDC hc;
4572: NODE node;
4573: P hcp;
4574: Z iq,jq;
4575: LIST l;
4576:
4577: nd_found = 0; nd_notfirst = 0; nd_create = 0;
4578: /* initialize the tracelist */
4579: nd_tracelist = 0;
4580:
4581: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
4582: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
4583: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
4584: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
4585: if ( !dont_sort ) {
4586: /* XXX heuristic */
1.26 noro 4587: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 4588: qsort(w,nd_psn,sizeof(struct oNDVI),
4589: (int (*)(const void *,const void *))ndvi_compare_rev);
4590: else
4591: qsort(w,nd_psn,sizeof(struct oNDVI),
4592: (int (*)(const void *,const void *))ndvi_compare);
4593: }
4594: nd_pslen = 2*nd_psn;
4595: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4596: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4597: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4598: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4599: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4600: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4601: nd_hcf = 0;
1.1 noro 4602:
1.6 noro 4603: if ( trace && nd_vc )
4604: makesubst(nd_vc,&nd_subst);
4605: else
4606: nd_subst = 0;
1.1 noro 4607:
1.6 noro 4608: if ( !nd_red )
4609: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4610: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4611: for ( i = 0; i < nd_psn; i++ ) {
4612: hc = HCU(w[i].p);
4613: if ( trace ) {
4614: if ( mod == -2 ) {
4615: /* over a large finite field */
4616: /* trace = small modulus */
4617: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4618: ndv_mod(-2,a);
4619: if ( !dont_removecont) ndv_removecont(-2,a);
4620: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4621: ndv_mod(trace,am);
4622: if ( DL_COMPARE(HDL(am),HDL(a)) )
4623: return 0;
4624: ndv_removecont(trace,am);
4625: } else {
4626: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4627: if ( !dont_removecont) ndv_removecont(0,a);
4628: register_hcf(a);
4629: am = nd_ps[i] = ndv_dup(mod,a);
4630: ndv_mod(mod,am);
4631: if ( DL_COMPARE(HDL(am),HDL(a)) )
4632: return 0;
4633: ndv_removecont(mod,am);
4634: }
4635: } else {
4636: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4637: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4638: if ( !mod ) register_hcf(a);
1.1 noro 4639: }
1.6 noro 4640: if ( nd_gentrace ) {
4641: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4642: /* exact division */
1.1 noro 4643: if ( !dont_removecont )
1.6 noro 4644: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4645: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4646: }
4647: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4648: nd_bound[i] = ndv_compute_bound(a);
4649: nd_psh[i] = r;
4650: if ( nd_demand ) {
4651: if ( trace ) {
4652: ndv_save(nd_ps_trace[i],i);
4653: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4654: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4655: nd_ps_trace[i] = 0;
4656: } else {
4657: ndv_save(nd_ps[i],i);
4658: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4659: nd_ps[i] = 0;
4660: }
1.1 noro 4661: }
1.6 noro 4662: }
1.24 noro 4663: if ( sba ) {
1.27 noro 4664: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4665: // setup signatures
1.27 noro 4666: for ( i = 0; i < nd_psn; i++ ) {
4667: SIG sig;
1.24 noro 4668:
1.27 noro 4669: NEWSIG(sig); sig->pos = i;
4670: nd_ps[i]->sig = sig;
4671: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4672: nd_psh[i]->sig = sig;
4673: if ( trace ) {
4674: nd_ps_trace[i]->sig = sig;
4675: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4676: }
4677: NEWDL(nd_sba_hm[i],nd_nvar);
1.46 noro 4678: if ( nd_sba_modord )
4679: ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45 noro 4680: else
4681: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27 noro 4682: }
1.29 noro 4683: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4684: for ( i = 0; i < nd_psn; i++ ) {
4685: j = nd_psh[i]->sig->pos;
4686: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4687: }
1.24 noro 4688: }
1.6 noro 4689: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4690: return 1;
1.1 noro 4691: }
4692:
4693: struct order_spec *append_block(struct order_spec *spec,
4694: int nv,int nalg,int ord);
4695:
4696: extern VECT current_dl_weight_vector_obj;
4697: static VECT prev_weight_vector_obj;
4698:
4699: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4700: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4701: {
4702: NODE alist,t,s,r0,r,arg;
4703: VL tv;
4704: P poly;
4705: DP d;
4706: Alg alpha,dp;
4707: DAlg inv,da,hc;
4708: MP m;
4709: int i,nvar,nalg,n;
4710: NumberField nf;
4711: LIST f1,f2;
4712: struct order_spec *current_spec;
4713: VECT obj,obj0;
4714: VECT tmp;
4715:
4716: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4717: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4718:
4719: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4720: NEXTNODE(alist,t); MKV(tv->v,poly);
4721: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4722: tv->v = tv->v->priv;
4723: }
4724: NEXT(t) = 0;
4725:
4726: /* simplification, making polynomials monic */
4727: setfield_dalg(alist);
4728: obj_algtodalg((Obj)f,(Obj *)&f1);
4729: for ( t = BDY(f); t; t = NEXT(t) ) {
4730: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4731: hc = (DAlg)BDY(d)->c;
4732: if ( NID(hc) == N_DA ) {
4733: invdalg(hc,&inv);
4734: for ( m = BDY(d); m; m = NEXT(m) ) {
4735: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4736: }
4737: }
4738: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4739: }
4740: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4741:
4742: /* append alg vars to the var list */
4743: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4744: NEXT(tv) = av;
4745:
4746: /* append a block to ord */
4747: *ord1p = append_block(ord,nvar,nalg,2);
4748:
4749: /* create generator list */
4750: nf = get_numberfield();
4751: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4752: MKAlg(nf->defpoly[i],dp);
4753: MKNODE(s,dp,t); t = s;
4754: }
4755: MKLIST(f1,t);
4756: *alistp = alist;
4757: algobjtorat((Obj)f1,(Obj *)f1p);
4758:
4759: /* creating a new weight vector */
4760: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4761: n = nvar+nalg+1;
4762: MKVECT(obj,n);
4763: if ( obj0 && obj0->len == nvar )
4764: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4765: else
4766: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4767: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4768: BDY(obj)[n-1] = (pointer)ONE;
4769: arg = mknode(1,obj);
4770: Pdp_set_weight(arg,&tmp);
4771: }
4772:
4773: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4774: {
4775: NODE s,t,u0,u;
4776: P p;
4777: VL tv;
4778: Obj obj;
4779: VECT tmp;
4780: NODE arg;
4781:
4782: u0 = 0;
4783: for ( t = r; t; t = NEXT(t) ) {
4784: p = (P)BDY(t);
4785: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4786: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4787: }
4788: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4789: NEXTNODE(u0,u);
4790: BDY(u) = (pointer)p;
4791: }
4792: }
4793: arg = mknode(1,prev_weight_vector_obj);
4794: Pdp_set_weight(arg,&tmp);
4795:
4796: return u0;
4797: }
4798:
4799: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4800: {
4801: VL tv,fv,vv,vc,av;
4802: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4803: int e,max,nvar,i;
4804: NDV b;
4805: int ishomo,nalg,mrank,trank,wmax,len;
4806: NMV a;
4807: Alg alpha,dp;
4808: P p,zp;
4809: Q dmy;
4810: LIST f1,f2,zpl;
4811: Obj obj;
4812: NumberField nf;
4813: struct order_spec *ord1;
4814: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4815: LIST l1,l2,l3,l4,l5;
4816: int j;
4817: Z jq,bpe,last_nonzero;
4818: int *perm;
4819: EPOS oepos;
4820: int obpe,oadv,ompos,cbpe;
1.15 noro 4821: VECT hvect;
1.1 noro 4822:
1.41 noro 4823: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4824: nd_module = 0;
4825: if ( !m && Demand ) nd_demand = 1;
4826: else nd_demand = 0;
4827:
4828: if ( DP_Multiple )
4829: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4830: #if 0
4831: ndv_alloc = 0;
4832: #endif
4833: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 4834: parse_nd_option(vv,current_option);
1.1 noro 4835: if ( m && nd_vc )
4836: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4837: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4838: switch ( ord->id ) {
4839: case 1:
4840: if ( ord->nv != nvar )
4841: error("nd_{gr,f4} : invalid order specification");
4842: break;
4843: default:
4844: break;
4845: }
4846: nd_nalg = 0;
4847: av = 0;
4848: if ( !m ) {
4849: get_algtree((Obj)f,&av);
4850: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4851: nd_ntrans = nvar;
4852: nd_nalg = nalg;
4853: /* #i -> t#i */
4854: if ( nalg ) {
4855: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4856: ord = ord1;
4857: f = f1;
4858: }
4859: nvar += nalg;
4860: }
4861: nd_init_ord(ord);
4862: mrank = 0;
4863: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4864: for ( tv = vv; tv; tv = NEXT(tv) ) {
4865: if ( nd_module ) {
1.16 noro 4866: if ( OID(BDY(t)) == O_DPM ) {
4867: e = dpm_getdeg((DPM)BDY(t),&trank);
4868: max = MAX(e,max);
4869: mrank = MAX(mrank,trank);
4870: } else {
4871: s = BDY((LIST)BDY(t));
4872: trank = length(s);
4873: mrank = MAX(mrank,trank);
4874: for ( ; s; s = NEXT(s) ) {
4875: e = getdeg(tv->v,(P)BDY(s));
4876: max = MAX(e,max);
4877: }
1.1 noro 4878: }
4879: } else {
1.43 noro 4880: if ( OID(BDY(t)) == O_DP ) {
4881: e = dp_getdeg((DP)BDY(t));
4882: max = MAX(e,max);
4883: } else {
4884: e = getdeg(tv->v,(P)BDY(t));
4885: max = MAX(e,max);
4886: }
1.1 noro 4887: }
4888: }
4889: nd_setup_parameters(nvar,nd_nzlist?0:max);
4890: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4891: ishomo = 1;
4892: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4893: if ( nd_module ) {
1.16 noro 4894: if ( OID(BDY(t)) == O_DPM ) {
4895: Z cont;
4896: DPM zdpm;
4897:
4898: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4899: else zdpm = (DPM)BDY(t);
4900: b = (pointer)dpmtondv(m,zdpm);
4901: } else {
4902: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4903: else zpl = (LIST)BDY(t);
1.1 noro 4904: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4905: }
1.1 noro 4906: } else {
1.43 noro 4907: if ( OID(BDY(t)) == O_DP ) {
4908: DP zdp;
4909:
4910: if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
4911: else zdp = (DP)BDY(t);
4912: b = (pointer)dptondv(m,zdp);
4913: } else {
4914: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4915: else zp = (P)BDY(t);
4916: b = (pointer)ptondv(CO,vv,zp);
4917: }
1.1 noro 4918: }
4919: if ( ishomo )
4920: ishomo = ishomo && ndv_ishomo(b);
4921: if ( m ) ndv_mod(m,b);
4922: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4923: }
4924: if ( fd0 ) NEXT(fd) = 0;
4925:
4926: if ( !ishomo && homo ) {
4927: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4928: b = (NDV)BDY(t); len = LEN(b);
4929: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4930: wmax = MAX(TD(DL(a)),wmax);
4931: }
4932: homogenize_order(ord,nvar,&ord1);
4933: nd_init_ord(ord1);
4934: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4935: for ( t = fd0; t; t = NEXT(t) )
4936: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4937: }
4938:
1.24 noro 4939: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4940: if ( nd_gentrace ) {
4941: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4942: }
4943: if ( nd_splist ) {
4944: *rp = compute_splist();
4945: return;
4946: }
4947: if ( nd_check_splist ) {
4948: if ( f4 ) {
4949: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4950: else *rp = 0;
4951: } else {
4952: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4953: else *rp = 0;
4954: }
4955: return;
4956: }
4957: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4958: if ( !x ) {
4959: *rp = 0; return;
4960: }
1.15 noro 4961: if ( nd_gentrace ) {
4962: MKVECT(hvect,nd_psn);
4963: for ( i = 0; i < nd_psn; i++ )
4964: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4965: }
1.1 noro 4966: if ( !ishomo && homo ) {
4967: /* dehomogenization */
4968: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4969: nd_init_ord(ord);
4970: nd_setup_parameters(nvar,0);
4971: }
4972: nd_demand = 0;
4973: if ( nd_module && nd_intersect ) {
4974: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4975: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4976: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4977: }
4978: conv_ilist(nd_demand,0,x,0);
4979: goto FINAL;
4980: }
4981: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4982: x = ndv_reducebase(x,perm);
4983: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4984: x = ndv_reduceall(m,x);
4985: cbpe = nd_bpe;
4986: if ( nd_gentrace && !f4 ) {
4987: tl2 = nd_alltracelist; nd_alltracelist = 0;
4988: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4989: tl3 = nd_alltracelist; nd_alltracelist = 0;
4990: if ( nd_gensyz ) {
4991: nd_gb(m,0,1,1,0);
4992: tl4 = nd_alltracelist; nd_alltracelist = 0;
4993: } else tl4 = 0;
4994: }
4995: nd_bpe = cbpe;
4996: nd_setup_parameters(nd_nvar,0);
4997: FINAL:
4998: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4999: NEXTNODE(r0,r);
5000: if ( nd_module ) {
5001: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
5002: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
5003: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
5004: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 5005: }
5006: if ( r0 ) NEXT(r) = 0;
5007: if ( !m && nd_nalg )
5008: r0 = postprocess_algcoef(av,alist,r0);
5009: MKLIST(*rp,r0);
5010: if ( nd_gentrace ) {
5011: if ( f4 ) {
1.6 noro 5012: STOZ(16,bpe);
5013: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 5014: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 5015: } else {
5016: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5017: tl3 = reverse_node(tl3);
5018: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5019: for ( t = tl2; t; t = NEXT(t) ) {
5020: /* s = [i,[*,j,*,*],...] */
5021: s = BDY((LIST)BDY(t));
1.6 noro 5022: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5023: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5024: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5025: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5026: }
5027: }
5028: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5029: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5030: }
5031: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5032: MKLIST(l5,tl4);
1.6 noro 5033: STOZ(nd_bpe,bpe);
1.15 noro 5034: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5035: }
5036: }
5037: #if 0
5038: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
5039: #endif
5040: }
5041:
1.28 noro 5042: NODE nd_sba_f4(int m,int **indp);
5043:
5044: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 5045: {
5046: VL tv,fv,vv,vc,av;
1.44 noro 5047: NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24 noro 5048: int e,max,nvar,i;
5049: NDV b;
5050: int ishomo,nalg,wmax,len;
5051: NMV a;
5052: P p,zp;
5053: Q dmy;
5054: struct order_spec *ord1;
5055: int j;
5056: int *perm;
5057: EPOS oepos;
5058: int obpe,oadv,ompos,cbpe;
1.51 noro 5059: struct oEGT eg0,eg1,egconv,egintred;
1.24 noro 5060:
5061: nd_module = 0;
5062: nd_demand = 0;
1.35 noro 5063: Nsamesig = 0;
1.24 noro 5064: if ( DP_Multiple )
5065: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5066: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5067: parse_nd_option(vv,current_option);
1.24 noro 5068: if ( m && nd_vc )
5069: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
5070: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5071: switch ( ord->id ) {
5072: case 1:
5073: if ( ord->nv != nvar )
5074: error("nd_sba : invalid order specification");
5075: break;
5076: default:
5077: break;
5078: }
5079: nd_nalg = 0;
5080: nd_init_ord(ord);
5081: // for SIG comparison
5082: initd(ord);
5083: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
5084: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43 noro 5085: if ( OID(BDY(t)) == O_DP ) {
5086: e = dp_getdeg((DP)BDY(t));
5087: max = MAX(e,max);
5088: } else {
5089: e = getdeg(tv->v,(P)BDY(t));
5090: max = MAX(e,max);
5091: }
1.24 noro 5092: }
5093: }
1.25 noro 5094: nd_setup_parameters(nvar,max);
1.24 noro 5095: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5096: ishomo = 1;
5097: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43 noro 5098: if ( OID(BDY(t)) == O_DP ) {
5099: DP zdp;
5100:
5101: if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
5102: else zdp = (DP)BDY(t);
5103: b = (pointer)dptondv(m,zdp);
5104: } else {
5105: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
5106: else zp = (P)BDY(t);
5107: b = (pointer)ptondv(CO,vv,zp);
5108: }
1.24 noro 5109: if ( ishomo )
5110: ishomo = ishomo && ndv_ishomo(b);
5111: if ( m ) ndv_mod(m,b);
5112: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
5113: }
5114: if ( fd0 ) NEXT(fd) = 0;
5115:
5116: if ( !ishomo && homo ) {
5117: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
5118: b = (NDV)BDY(t); len = LEN(b);
5119: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
5120: wmax = MAX(TD(DL(a)),wmax);
5121: }
5122: homogenize_order(ord,nvar,&ord1);
5123: nd_init_ord(ord1);
1.42 noro 5124: // for SIG comparison
5125: initd(ord1);
1.24 noro 5126: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
5127: for ( t = fd0; t; t = NEXT(t) )
5128: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5129: }
5130:
1.39 noro 5131: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44 noro 5132: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24 noro 5133: if ( !x ) {
5134: *rp = 0; return;
5135: }
5136: if ( !ishomo && homo ) {
5137: /* dehomogenization */
5138: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5139: nd_init_ord(ord);
1.42 noro 5140: // for SIG comparison
5141: initd(ord);
1.24 noro 5142: nd_setup_parameters(nvar,0);
5143: }
5144: nd_demand = 0;
1.51 noro 5145: get_eg(&eg0);
1.24 noro 5146: x = ndv_reducebase(x,perm);
5147: x = ndv_reduceall(m,x);
1.51 noro 5148: get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24 noro 5149: nd_setup_parameters(nd_nvar,0);
1.29 noro 5150: get_eg(&eg0);
1.24 noro 5151: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
5152: NEXTNODE(r0,r);
5153: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 5154: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 5155: }
5156: if ( r0 ) NEXT(r) = 0;
1.44 noro 5157: if ( nd_sba_syz ) {
5158: LIST gb,hsyz;
5159: NODE nd;
5160:
5161: MKLIST(gb,r0);
5162: MKLIST(hsyz,syz);
5163: nd = mknode(2,gb,hsyz);
5164: MKLIST(*rp,nd);
5165: } else
5166: MKLIST(*rp,r0);
1.29 noro 5167: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51 noro 5168: print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29 noro 5169: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 5170: }
5171:
1.1 noro 5172: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
5173: {
5174: VL tv,fv,vv,vc,av;
5175: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5176: int e,max,nvar,i;
5177: NDV b;
5178: int ishomo,nalg;
5179: Alg alpha,dp;
5180: P p,zp;
5181: Q dmy;
5182: LIST f1,f2;
5183: Obj obj;
5184: NumberField nf;
5185: struct order_spec *ord1;
5186: int *perm;
5187:
5188: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5189: parse_nd_option(vv,current_option);
1.1 noro 5190: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5191: switch ( ord->id ) {
5192: case 1:
5193: if ( ord->nv != nvar )
5194: error("nd_check : invalid order specification");
5195: break;
5196: default:
5197: break;
5198: }
5199: nd_nalg = 0;
5200: av = 0;
5201: if ( !m ) {
5202: get_algtree((Obj)f,&av);
5203: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5204: nd_ntrans = nvar;
5205: nd_nalg = nalg;
5206: /* #i -> t#i */
5207: if ( nalg ) {
5208: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5209: ord = ord1;
5210: f = f1;
5211: }
5212: nvar += nalg;
5213: }
5214: nd_init_ord(ord);
5215: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5216: for ( tv = vv; tv; tv = NEXT(tv) ) {
5217: e = getdeg(tv->v,(P)BDY(t));
5218: max = MAX(e,max);
5219: }
5220: nd_setup_parameters(nvar,max);
5221: ishomo = 1;
5222: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5223: ptozp((P)BDY(t),1,&dmy,&zp);
5224: b = (pointer)ptondv(CO,vv,zp);
5225: if ( ishomo )
5226: ishomo = ishomo && ndv_ishomo(b);
5227: if ( m ) ndv_mod(m,b);
5228: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
5229: }
5230: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 5231: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 5232: for ( x = 0, i = 0; i < nd_psn; i++ )
5233: x = update_base(x,i);
5234: if ( do_check ) {
5235: x = nd_gb(m,ishomo,1,0,&perm);
5236: if ( !x ) {
5237: *rp = 0;
5238: return;
5239: }
5240: } else {
5241: #if 0
5242: /* bug ? */
5243: for ( t = x; t; t = NEXT(t) )
5244: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
5245: #else
5246: conv_ilist(0,0,x,&perm);
5247: #endif
5248: }
5249: x = ndv_reducebase(x,perm);
5250: x = ndv_reduceall(m,x);
5251: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
5252: NEXTNODE(r0,r);
5253: BDY(r) = ndvtop(m,CO,vv,BDY(t));
5254: }
5255: if ( r0 ) NEXT(r) = 0;
5256: if ( !m && nd_nalg )
5257: r0 = postprocess_algcoef(av,alist,r0);
5258: MKLIST(*rp,r0);
5259: }
5260:
5261: NDV recompute_trace(NODE trace,NDV *p,int m);
5262: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
5263:
5264: NDV recompute_trace(NODE ti,NDV *p,int mod)
5265: {
5266: int c,c1,c2,i;
5267: NM mul,m,tail;
5268: ND d,r,rm;
5269: NODE sj;
5270: NDV red;
5271: Obj mj;
5272:
5273: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
5274: CM(mul) = 1;
5275: tail = 0;
5276: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
5277: sj = BDY((LIST)BDY(ti));
5278: if ( ARG0(sj) ) {
1.6 noro 5279: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 5280: mj = (Obj)ARG2(sj);
5281: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
5282: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
5283: rm = ndv_mul_nm(mod,mul,red);
5284: if ( !r ) r = rm;
5285: else {
5286: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
5287: if ( d ) {
5288: NEXT(tail) = m; tail = m; LEN(d)++;
5289: } else {
5290: MKND(nd_nvar,m,1,d); tail = BDY(d);
5291: }
5292: }
5293: if ( !m ) return 0; /* failure */
5294: else {
5295: BDY(r) = m;
5296: if ( mod > 0 || mod == -1 ) {
5297: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
5298: DMAR(c1,c2,0,mod,c);
5299: nd_mul_c(mod,rm,c);
5300: } else {
5301: Z t,u;
5302:
5303: chsgnlf(HCZ(r),&t);
5304: divlf(t,HCZ(rm),&u);
5305: nd_mul_c_lf(rm,u);
5306: }
5307: r = nd_add(mod,r,rm);
5308: }
5309: }
5310: }
5311: }
5312: if ( tail ) NEXT(tail) = 0;
5313: d = nd_add(mod,d,r);
5314: nd_mul_c(mod,d,invm(HCM(d),mod));
5315: return ndtondv(mod,d);
5316: }
5317:
5318: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
5319: {
5320: VL tv,fv,vv,vc,av;
5321: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5322: int e,max,nvar,i;
5323: NDV b;
5324: int ishomo,nalg;
5325: Alg alpha,dp;
5326: P p,zp;
5327: Q dmy;
5328: LIST f1,f2;
5329: Obj obj;
5330: NumberField nf;
5331: struct order_spec *ord1;
5332: NODE permtrace,intred,ind,perm,trace,ti;
5333: int len,n,j;
5334: NDV *db,*pb;
5335:
5336: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5337: parse_nd_option(vv,current_option);
1.1 noro 5338: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5339: switch ( ord->id ) {
5340: case 1:
5341: if ( ord->nv != nvar )
5342: error("nd_check : invalid order specification");
5343: break;
5344: default:
5345: break;
5346: }
5347: nd_init_ord(ord);
1.6 noro 5348: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 5349: nd_setup_parameters(nvar,0);
5350:
5351: len = length(BDY(f));
5352: db = (NDV *)MALLOC(len*sizeof(NDV *));
5353: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
5354: ptozp((P)BDY(t),1,&dmy,&zp);
5355: b = ptondv(CO,vv,zp);
5356: ndv_mod(m,b);
5357: ndv_mul_c(m,b,invm(HCM(b),m));
5358: db[i] = b;
5359: }
5360:
5361: permtrace = BDY((LIST)ARG2(BDY(tlist)));
5362: intred = BDY((LIST)ARG3(BDY(tlist)));
5363: ind = BDY((LIST)ARG4(BDY(tlist)));
5364: perm = BDY((LIST)ARG0(permtrace));
5365: trace = NEXT(permtrace);
5366:
5367: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 5368: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 5369: if ( j > i ) i = j;
5370: }
5371: n = i+1;
5372: pb = (NDV *)MALLOC(n*sizeof(NDV *));
5373: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
5374: ti = BDY((LIST)BDY(t));
1.6 noro 5375: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 5376: }
5377: for ( t = trace; t; t = NEXT(t) ) {
5378: ti = BDY((LIST)BDY(t));
1.6 noro 5379: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5380: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5381: if ( DP_Print ) {
5382: fprintf(asir_out,"."); fflush(asir_out);
5383: }
5384: }
5385: for ( t = intred; t; t = NEXT(t) ) {
5386: ti = BDY((LIST)BDY(t));
1.6 noro 5387: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5388: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5389: if ( DP_Print ) {
5390: fprintf(asir_out,"*"); fflush(asir_out);
5391: }
5392: }
5393: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
5394: NEXTNODE(r0,r);
1.6 noro 5395: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 5396: ndv_mul_c(m,b,invm(HCM(b),m));
5397: #if 0
1.6 noro 5398: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5399: #else
1.6 noro 5400: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5401: #endif
5402: }
5403: if ( r0 ) NEXT(r) = 0;
5404: MKLIST(*rp,r0);
5405: if ( DP_Print ) fprintf(asir_out,"\n");
5406: }
5407:
1.16 noro 5408: 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 5409: {
5410: VL tv,fv,vv,vc,av;
5411: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
5412: int m,nocheck,nvar,mindex,e,max;
5413: NDV c;
5414: NMV a;
5415: P p,zp;
5416: Q dmy;
5417: EPOS oepos;
5418: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
5419: Alg alpha,dp;
5420: P poly;
5421: LIST f1,f2,zpl;
5422: Obj obj;
5423: NumberField nf;
5424: struct order_spec *ord1;
5425: struct oEGT eg_check,eg0,eg1;
5426: NODE tr,tl1,tl2,tl3,tl4;
5427: LIST l1,l2,l3,l4,l5;
5428: int *perm;
5429: int j,ret;
5430: Z jq,bpe;
1.15 noro 5431: VECT hvect;
1.1 noro 5432:
1.41 noro 5433: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 5434: nd_module = 0;
5435: nd_lf = 0;
1.46 noro 5436: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
5437: parse_nd_option(vv,current_option);
1.1 noro 5438: if ( nd_lf ) {
5439: if ( f4 )
5440: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
5441: else
5442: error("nd_gr_trace is not implemented yet over a large finite field");
5443: return;
5444: }
5445: if ( DP_Multiple )
5446: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5447:
5448: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5449: switch ( ord->id ) {
5450: case 1:
5451: if ( ord->nv != nvar )
5452: error("nd_gr_trace : invalid order specification");
5453: break;
5454: default:
5455: break;
5456: }
5457:
5458: get_algtree((Obj)f,&av);
5459: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5460: nd_ntrans = nvar;
5461: nd_nalg = nalg;
5462: /* #i -> t#i */
5463: if ( nalg ) {
5464: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5465: ord = ord1;
5466: f = f1;
5467: }
5468: nvar += nalg;
5469:
5470: nocheck = 0;
5471: mindex = 0;
5472:
5473: if ( Demand ) nd_demand = 1;
5474: else nd_demand = 0;
5475:
5476: /* setup modulus */
5477: if ( trace < 0 ) {
5478: trace = -trace;
5479: nocheck = 1;
5480: }
5481: m = trace > 1 ? trace : get_lprime(mindex);
5482: nd_init_ord(ord);
5483: mrank = 0;
5484: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5485: for ( tv = vv; tv; tv = NEXT(tv) ) {
5486: if ( nd_module ) {
1.16 noro 5487: if ( OID(BDY(t)) == O_DPM ) {
5488: e = dpm_getdeg((DPM)BDY(t),&trank);
5489: max = MAX(e,max);
5490: mrank = MAX(mrank,trank);
5491: } else {
1.1 noro 5492: s = BDY((LIST)BDY(t));
5493: trank = length(s);
5494: mrank = MAX(mrank,trank);
5495: for ( ; s; s = NEXT(s) ) {
5496: e = getdeg(tv->v,(P)BDY(s));
5497: max = MAX(e,max);
5498: }
1.16 noro 5499: }
1.1 noro 5500: } else {
1.43 noro 5501: if ( OID(BDY(t)) == O_DP ) {
5502: e = dp_getdeg((DP)BDY(t));
5503: max = MAX(e,max);
5504: } else {
1.1 noro 5505: e = getdeg(tv->v,(P)BDY(t));
5506: max = MAX(e,max);
1.43 noro 5507: }
1.1 noro 5508: }
5509: }
5510: nd_setup_parameters(nvar,max);
5511: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5512: ishomo = 1;
5513: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5514: if ( nd_module ) {
1.16 noro 5515: if ( OID(BDY(t)) == O_DPM ) {
5516: Z cont;
5517: DPM zdpm;
5518:
1.17 noro 5519: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 5520: else zdpm = (DPM)BDY(t);
5521: c = (pointer)dpmtondv(m,zdpm);
5522: } else {
5523: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
5524: else zpl = (LIST)BDY(t);
1.1 noro 5525: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 5526: }
1.1 noro 5527: } else {
1.43 noro 5528: if ( OID(BDY(t)) == O_DP ) {
5529: DP zdp;
5530:
5531: if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
5532: else zdp = (DP)BDY(t);
5533: c = (pointer)dptondv(m,zdp);
5534: } else {
5535: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
5536: else zp = (P)BDY(t);
5537: c = (pointer)ptondv(CO,vv,zp);
5538: }
1.1 noro 5539: }
5540: if ( ishomo )
5541: ishomo = ishomo && ndv_ishomo(c);
5542: if ( c ) {
5543: NEXTNODE(in0,in); BDY(in) = (pointer)c;
5544: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
5545: }
5546: }
5547: if ( in0 ) NEXT(in) = 0;
5548: if ( fd0 ) NEXT(fd) = 0;
5549: if ( !ishomo && homo ) {
5550: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
5551: c = (NDV)BDY(t); len = LEN(c);
5552: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
5553: wmax = MAX(TD(DL(a)),wmax);
5554: }
5555: homogenize_order(ord,nvar,&ord1);
5556: nd_init_ord(ord1);
5557: nd_setup_parameters(nvar+1,wmax);
5558: for ( t = fd0; t; t = NEXT(t) )
5559: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5560: }
5561: if ( MaxDeg > 0 ) nocheck = 1;
5562: while ( 1 ) {
5563: tl1 = tl2 = tl3 = tl4 = 0;
5564: if ( Demand )
5565: nd_demand = 1;
1.24 noro 5566: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 5567: if ( nd_gentrace ) {
5568: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
5569: }
5570: if ( ret )
5571: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
5572: if ( !ret || !cand ) {
5573: /* failure */
5574: if ( trace > 1 ) { *rp = 0; return; }
5575: else m = get_lprime(++mindex);
5576: continue;
5577: }
1.15 noro 5578: if ( nd_gentrace ) {
5579: MKVECT(hvect,nd_psn);
5580: for ( i = 0; i < nd_psn; i++ )
5581: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
5582: }
1.1 noro 5583: if ( !ishomo && homo ) {
5584: /* dehomogenization */
5585: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5586: nd_init_ord(ord);
5587: nd_setup_parameters(nvar,0);
5588: }
5589: nd_demand = 0;
5590: cand = ndv_reducebase(cand,perm);
5591: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
5592: cand = ndv_reduceall(0,cand);
5593: cbpe = nd_bpe;
5594: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
5595: get_eg(&eg0);
5596: if ( nocheck )
5597: break;
5598: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
5599: if ( nd_gentrace ) {
5600: tl3 = nd_alltracelist; nd_alltracelist = 0;
5601: } else tl3 = 0;
5602: /* gbcheck : cand is a GB of Id(cand) ? */
5603: if ( nd_vc || nd_gentrace || nd_gensyz )
5604: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
5605: else
5606: ret = nd_f4(0,1,0)!=0;
5607: if ( nd_gentrace && nd_gensyz ) {
5608: tl4 = nd_alltracelist; nd_alltracelist = 0;
5609: } else tl4 = 0;
5610: }
5611: if ( ret ) break;
5612: else if ( trace > 1 ) {
5613: /* failure */
5614: *rp = 0; return;
5615: } else {
5616: /* try the next modulus */
5617: m = get_lprime(++mindex);
5618: /* reset the parameters */
5619: if ( !ishomo && homo ) {
5620: nd_init_ord(ord1);
5621: nd_setup_parameters(nvar+1,wmax);
5622: } else {
5623: nd_init_ord(ord);
5624: nd_setup_parameters(nvar,max);
5625: }
5626: }
5627: }
5628: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
5629: if ( DP_Print )
1.6 noro 5630: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 5631: /* dp->p */
5632: nd_bpe = cbpe;
5633: nd_setup_parameters(nd_nvar,0);
5634: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 5635: if ( nd_module ) {
1.17 noro 5636: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 5637: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 5638: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
5639: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 5640: }
5641: if ( nd_nalg )
5642: cand = postprocess_algcoef(av,alist,cand);
5643: MKLIST(*rp,cand);
5644: if ( nd_gentrace ) {
5645: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5646: tl3 = reverse_node(tl3);
5647: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5648: for ( t = tl2; t; t = NEXT(t) ) {
5649: /* s = [i,[*,j,*,*],...] */
5650: s = BDY((LIST)BDY(t));
1.6 noro 5651: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5652: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5653: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5654: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5655: }
5656: }
5657: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5658: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5659: }
5660: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5661: MKLIST(l5,tl4);
1.6 noro 5662: STOZ(nd_bpe,bpe);
1.15 noro 5663: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5664: }
5665: }
5666:
5667: /* XXX : module element is not considered */
5668:
5669: void dltondl(int n,DL dl,UINT *r)
5670: {
5671: UINT *d;
5672: int i,j,l,s,ord_l;
5673: struct order_pair *op;
5674:
5675: d = (unsigned int *)dl->d;
5676: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5677: if ( nd_blockmask ) {
5678: l = nd_blockmask->n;
5679: op = nd_blockmask->order_pair;
5680: for ( j = 0, s = 0; j < l; j++ ) {
5681: ord_l = op[j].length;
5682: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5683: }
5684: TD(r) = ndl_weight(r);
5685: ndl_weight_mask(r);
5686: } else {
5687: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5688: TD(r) = ndl_weight(r);
5689: }
5690: }
5691:
5692: DL ndltodl(int n,UINT *ndl)
5693: {
5694: DL dl;
5695: int *d;
5696: int i,j,l,s,ord_l;
5697: struct order_pair *op;
5698:
1.33 noro 5699: NEWDL_NOINIT(dl,n);
1.1 noro 5700: dl->td = TD(ndl);
5701: d = dl->d;
5702: if ( nd_blockmask ) {
5703: l = nd_blockmask->n;
5704: op = nd_blockmask->order_pair;
5705: for ( j = 0, s = 0; j < l; j++ ) {
5706: ord_l = op[j].length;
5707: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5708: }
5709: } else {
5710: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5711: }
5712: return dl;
5713: }
5714:
1.24 noro 5715: void _ndltodl(UINT *ndl,DL dl)
5716: {
5717: int *d;
5718: int i,j,l,s,ord_l,n;
5719: struct order_pair *op;
5720:
5721: n = nd_nvar;
5722: dl->td = TD(ndl);
5723: d = dl->d;
5724: if ( nd_blockmask ) {
5725: l = nd_blockmask->n;
5726: op = nd_blockmask->order_pair;
5727: for ( j = 0, s = 0; j < l; j++ ) {
5728: ord_l = op[j].length;
5729: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5730: }
5731: } else {
5732: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5733: }
5734: }
5735:
1.1 noro 5736: void nmtodp(int mod,NM m,DP *r)
5737: {
5738: DP dp;
5739: MP mr;
5740:
5741: NEWMP(mr);
5742: mr->dl = ndltodl(nd_nvar,DL(m));
5743: mr->c = (Obj)ndctop(mod,m->c);
5744: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5745: *r = dp;
5746: }
5747:
1.15 noro 5748: void ndltodp(UINT *d,DP *r)
5749: {
5750: DP dp;
5751: MP mr;
5752:
5753: NEWMP(mr);
5754: mr->dl = ndltodl(nd_nvar,d);
5755: mr->c = (Obj)ONE;
5756: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5757: *r = dp;
5758: }
5759:
1.1 noro 5760: void ndl_print(UINT *dl)
5761: {
5762: int n;
5763: int i,j,l,ord_l,s,s0;
5764: struct order_pair *op;
5765:
5766: n = nd_nvar;
5767: printf("<<");
5768: if ( nd_blockmask ) {
5769: l = nd_blockmask->n;
5770: op = nd_blockmask->order_pair;
5771: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5772: ord_l = op[j].length;
5773: for ( i = 0; i < ord_l; i++, s++ )
5774: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5775: }
5776: } else {
5777: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5778: }
5779: printf(">>");
5780: if ( nd_module && MPOS(dl) )
5781: printf("*e%d",MPOS(dl));
5782: }
5783:
5784: void nd_print(ND p)
5785: {
5786: NM m;
5787:
5788: if ( !p )
5789: printf("0\n");
5790: else {
5791: for ( m = BDY(p); m; m = NEXT(m) ) {
5792: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5793: else printf("+%ld*",CM(m));
1.1 noro 5794: ndl_print(DL(m));
5795: }
5796: printf("\n");
5797: }
5798: }
5799:
5800: void nd_print_q(ND p)
5801: {
5802: NM m;
5803:
5804: if ( !p )
5805: printf("0\n");
5806: else {
5807: for ( m = BDY(p); m; m = NEXT(m) ) {
5808: printf("+");
1.6 noro 5809: printexpr(CO,(Obj)CZ(m));
1.1 noro 5810: printf("*");
5811: ndl_print(DL(m));
5812: }
5813: printf("\n");
5814: }
5815: }
5816:
5817: void ndp_print(ND_pairs d)
5818: {
5819: ND_pairs t;
5820:
5821: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5822: printf("\n");
5823: }
5824:
5825: void nd_removecont(int mod,ND p)
5826: {
5827: int i,n;
5828: Z *w;
5829: NM m;
5830: struct oVECT v;
5831:
5832: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5833: else if ( mod == -2 ) {
5834: Z inv;
5835: divlf(ONE,HCZ(p),&inv);
5836: nd_mul_c_lf(p,inv);
5837: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5838: else {
5839: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5840: w = (Z *)MALLOC(n*sizeof(Q));
5841: v.len = n;
5842: v.body = (pointer *)w;
1.6 noro 5843: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5844: removecont_array((P *)w,n,1);
1.6 noro 5845: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5846: }
5847: }
5848:
5849: void nd_removecont2(ND p1,ND p2)
5850: {
5851: int i,n1,n2,n;
5852: Z *w;
5853: NM m;
5854: struct oVECT v;
5855:
5856: n1 = nd_length(p1);
5857: n2 = nd_length(p2);
5858: n = n1+n2;
5859: w = (Z *)MALLOC(n*sizeof(Q));
5860: v.len = n;
5861: v.body = (pointer *)w;
5862: i = 0;
5863: if ( p1 )
1.6 noro 5864: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5865: if ( p2 )
1.6 noro 5866: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5867: removecont_array((P *)w,n,1);
5868: i = 0;
5869: if ( p1 )
1.6 noro 5870: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5871: if ( p2 )
1.6 noro 5872: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5873: }
5874:
5875: void ndv_removecont(int mod,NDV p)
5876: {
5877: int i,len,all_p;
5878: Z *c;
5879: P *w;
5880: Z dvr,t;
5881: P g,cont,tp;
5882: NMV m;
5883:
5884: if ( mod == -1 )
5885: ndv_mul_c(mod,p,_invsf(HCM(p)));
5886: else if ( mod == -2 ) {
5887: Z inv;
5888: divlf(ONE,HCZ(p),&inv);
5889: ndv_mul_c_lf(p,inv);
5890: } else if ( mod )
5891: ndv_mul_c(mod,p,invm(HCM(p),mod));
5892: else {
5893: len = p->len;
5894: w = (P *)MALLOC(len*sizeof(P));
5895: c = (Z *)MALLOC(len*sizeof(Q));
5896: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5897: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5898: all_p = all_p && !NUM(w[i]);
5899: }
5900: if ( all_p ) {
5901: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5902: mulp(nd_vc,(P)dvr,g,&cont);
5903: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5904: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5905: }
5906: } else {
5907: sortbynm((Q *)c,len);
5908: qltozl((Q *)c,len,&dvr);
5909: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5910: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5911: }
5912: }
5913: }
5914: }
5915:
5916: /* koko */
5917:
5918: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5919: {
5920: int len,i,max;
5921: NMV m,mr0,mr,t;
5922:
5923: len = p->len;
1.14 noro 5924: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5925: max = MAX(max,TD(DL(m)));
5926: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5927: m = (NMV)((char *)mr0+(len-1)*oadv);
5928: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5929: t = (NMV)MALLOC(nmv_adv);
5930: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5931: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5932: CZ(mr) = CZ(m);
1.1 noro 5933: ndl_copy(DL(t),DL(mr));
5934: }
5935: NV(p)++;
5936: BDY(p) = mr0;
5937: }
5938:
5939: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5940: {
5941: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5942: int pos;
5943: Q *w;
5944: Q dvr,t;
5945: NMV m,r;
5946:
5947: len = p->len;
5948: newnvar = nd_nvar-1;
5949: newexporigin = nd_get_exporigin(ord);
5950: if ( nd_module ) newmpos = newexporigin-1;
5951: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5952: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5953: ndl_dehomogenize(DL(m));
5954: if ( newwpd != nd_wpd ) {
5955: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5956: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5957: CZ(r) = CZ(m);
1.1 noro 5958: if ( nd_module ) pos = MPOS(DL(m));
5959: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5960: adj = nd_exporigin-newexporigin;
5961: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5962: if ( nd_module ) {
5963: DL(r)[newmpos] = pos;
5964: }
5965: }
5966: }
5967: NV(p)--;
5968: }
5969:
5970: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5971: {
5972: int i;
5973: P *tpl,*tpl1;
5974: NODE l;
5975: P h,gcd,t;
5976:
5977: tpl = (P *)MALLOC(m*sizeof(P));
5978: tpl1 = (P *)MALLOC(m*sizeof(P));
5979: bcopy(pl,tpl,m*sizeof(P));
5980: gcd = (P)ONE;
5981: for ( l = nd_hcf; l; l = NEXT(l) ) {
5982: h = (P)BDY(l);
5983: while ( 1 ) {
5984: for ( i = 0; i < m; i++ )
5985: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5986: break;
5987: if ( i == m ) {
5988: bcopy(tpl1,tpl,m*sizeof(P));
5989: mulp(vl,gcd,h,&t); gcd = t;
5990: } else
5991: break;
5992: }
5993: }
5994: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5995: if ( full ) {
5996: heu_nezgcdnpz(vl,tpl,m,&t);
5997: mulp(vl,gcd,t,pr);
5998: } else
5999: *pr = gcd;
6000: }
6001:
6002: void removecont_array(P *p,int n,int full)
6003: {
6004: int all_p,all_q,i;
6005: Z *c;
6006: P *w;
6007: P t,s;
6008:
6009: for ( all_q = 1, i = 0; i < n; i++ )
6010: all_q = all_q && NUM(p[i]);
6011: if ( all_q ) {
6012: removecont_array_q((Z *)p,n);
6013: } else {
6014: c = (Z *)MALLOC(n*sizeof(Z));
6015: w = (P *)MALLOC(n*sizeof(P));
6016: for ( i = 0; i < n; i++ ) {
6017: ptozp(p[i],1,(Q *)&c[i],&w[i]);
6018: }
6019: removecont_array_q(c,n);
6020: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
6021: for ( i = 0; i < n; i++ ) {
6022: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
6023: }
6024: }
6025: }
6026:
6027: /* c is an int array */
6028:
6029: void removecont_array_q(Z *c,int n)
6030: {
6031: struct oVECT v;
6032: Z d0,d1,a,u,u1,gcd;
6033: int i,j;
6034: Z *q,*r;
6035:
6036: q = (Z *)MALLOC(n*sizeof(Z));
6037: r = (Z *)MALLOC(n*sizeof(Z));
6038: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
6039: gcdvz_estimate(&v,&d0);
6040: for ( i = 0; i < n; i++ ) {
6041: divqrz(c[i],d0,&q[i],&r[i]);
6042: }
6043: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
6044: if ( i < n ) {
6045: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
6046: gcdvz(&v,&d1);
6047: gcdz(d0,d1,&gcd);
1.6 noro 6048: /* exact division */
6049: divsz(d0,gcd,&a);
1.1 noro 6050: for ( i = 0; i < n; i++ ) {
6051: mulz(a,q[i],&u);
6052: if ( r[i] ) {
1.6 noro 6053: /* exact division */
6054: divsz(r[i],gcd,&u1);
1.1 noro 6055: addz(u,u1,&q[i]);
6056: } else
6057: q[i] = u;
6058: }
6059: }
6060: for ( i = 0; i < n; i++ ) c[i] = q[i];
6061: }
6062:
1.4 noro 6063: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
6064:
6065: void mpz_removecont_array(mpz_t *c,int n)
6066: {
6067: mpz_t d0,a,u,u1,gcd;
6068: int i,j;
1.13 noro 6069: static mpz_t *q,*r;
6070: static int c_len = 0;
1.4 noro 6071:
6072: for ( i = 0; i < n; i++ )
6073: if ( mpz_sgn(c[i]) ) break;
6074: if ( i == n ) return;
6075: gcdv_mpz_estimate(d0,c,n);
1.13 noro 6076: if ( n > c_len ) {
6077: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
6078: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
6079: c_len = n;
6080: }
1.4 noro 6081: for ( i = 0; i < n; i++ ) {
6082: mpz_init(q[i]); mpz_init(r[i]);
6083: mpz_fdiv_qr(q[i],r[i],c[i],d0);
6084: }
6085: for ( i = 0; i < n; i++ )
6086: if ( mpz_sgn(r[i]) ) break;
6087: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
6088: if ( i < n ) {
6089: mpz_gcd(gcd,d0,r[i]);
6090: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
6091: mpz_div(a,d0,gcd);
6092: for ( i = 0; i < n; i++ ) {
6093: mpz_mul(u,a,q[i]);
6094: if ( mpz_sgn(r[i]) ) {
6095: mpz_div(u1,r[i],gcd);
6096: mpz_add(q[i],u,u1);
6097: } else
6098: mpz_set(q[i],u);
6099: }
6100: }
6101: for ( i = 0; i < n; i++ )
6102: mpz_set(c[i],q[i]);
6103: }
6104:
1.1 noro 6105: void nd_mul_c(int mod,ND p,int mul)
6106: {
6107: NM m;
6108: int c,c1;
6109:
6110: if ( !p ) return;
6111: if ( mul == 1 ) return;
6112: if ( mod == -1 )
6113: for ( m = BDY(p); m; m = NEXT(m) )
6114: CM(m) = _mulsf(CM(m),mul);
6115: else
6116: for ( m = BDY(p); m; m = NEXT(m) ) {
6117: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6118: }
6119: }
6120:
6121: void nd_mul_c_lf(ND p,Z mul)
6122: {
6123: NM m;
6124: Z c;
6125:
6126: if ( !p ) return;
6127: if ( UNIZ(mul) ) return;
6128: for ( m = BDY(p); m; m = NEXT(m) ) {
6129: mullf(CZ(m),mul,&c); CZ(m) = c;
6130: }
6131: }
6132:
6133: void nd_mul_c_q(ND p,P mul)
6134: {
6135: NM m;
6136: P c;
6137:
6138: if ( !p ) return;
6139: if ( UNIQ(mul) ) return;
6140: for ( m = BDY(p); m; m = NEXT(m) ) {
6141: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
6142: }
6143: }
6144:
6145: void nd_mul_c_p(VL vl,ND p,P mul)
6146: {
6147: NM m;
6148: P c;
6149:
6150: if ( !p ) return;
6151: for ( m = BDY(p); m; m = NEXT(m) ) {
6152: mulp(vl,CP(m),mul,&c); CP(m) = c;
6153: }
6154: }
6155:
6156: void nd_free(ND p)
6157: {
6158: NM t,s;
6159:
6160: if ( !p ) return;
6161: t = BDY(p);
6162: while ( t ) {
6163: s = NEXT(t);
6164: FREENM(t);
6165: t = s;
6166: }
6167: FREEND(p);
6168: }
6169:
6170: void ndv_free(NDV p)
6171: {
6172: GCFREE(BDY(p));
6173: }
6174:
6175: void nd_append_red(UINT *d,int i)
6176: {
6177: RHist m,m0;
6178: int h;
6179:
6180: NEWRHist(m);
6181: h = ndl_hash_value(d);
6182: m->index = i;
6183: ndl_copy(d,DL(m));
6184: NEXT(m) = nd_red[h];
6185: nd_red[h] = m;
6186: }
6187:
6188: UINT *ndv_compute_bound(NDV p)
6189: {
6190: UINT *d1,*d2,*t;
6191: UINT u;
6192: int i,j,k,l,len,ind;
6193: NMV m;
6194:
6195: if ( !p )
6196: return 0;
6197: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6198: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6199: len = LEN(p);
6200: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
6201: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
6202: ndl_max(DL(m),d1,d2);
6203: t = d1; d1 = d2; d2 = t;
6204: }
6205: l = nd_nvar+31;
6206: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
6207: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
6208: u = d1[i];
6209: k = (nd_epw-1)*nd_bpe;
6210: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
6211: t[ind] = (u>>k)&nd_mask0;
6212: }
6213: for ( ; ind < l; ind++ ) t[ind] = 0;
6214: return t;
6215: }
6216:
6217: UINT *nd_compute_bound(ND p)
6218: {
6219: UINT *d1,*d2,*t;
6220: UINT u;
6221: int i,j,k,l,len,ind;
6222: NM m;
6223:
6224: if ( !p )
6225: return 0;
6226: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6227: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6228: len = LEN(p);
6229: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
6230: for ( m = NEXT(m); m; m = NEXT(m) ) {
6231: ndl_lcm(DL(m),d1,d2);
6232: t = d1; d1 = d2; d2 = t;
6233: }
6234: l = nd_nvar+31;
6235: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
6236: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
6237: u = d1[i];
6238: k = (nd_epw-1)*nd_bpe;
6239: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
6240: t[ind] = (u>>k)&nd_mask0;
6241: }
6242: for ( ; ind < l; ind++ ) t[ind] = 0;
6243: return t;
6244: }
6245:
6246: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
6247: /* of a term. In this case we need additional 1 word. */
6248:
6249: int nd_get_exporigin(struct order_spec *ord)
6250: {
6251: switch ( ord->id ) {
1.21 noro 6252: case 0: case 2: case 256: case 258: case 300:
1.1 noro 6253: return 1+nd_module;
6254: case 1: case 257:
6255: /* block order */
6256: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
6257: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
6258: return ord->ord.block.length+1+nd_module;
6259: case 3: case 259:
6260: #if 0
6261: error("nd_get_exporigin : composite order is not supported yet.");
6262: #else
6263: return 1+nd_module;
6264: #endif
6265: default:
6266: error("nd_get_exporigin : ivalid argument.");
6267: return 0;
6268: }
6269: }
6270:
6271: void nd_setup_parameters(int nvar,int max) {
6272: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
6273: struct order_pair *op;
1.45 noro 6274: extern int CNVars;
1.1 noro 6275:
6276: nd_nvar = nvar;
1.45 noro 6277: CNVars = nvar;
1.1 noro 6278: if ( max ) {
6279: /* XXX */
6280: if ( do_weyl ) nd_bpe = 32;
6281: else if ( max < 2 ) nd_bpe = 1;
6282: else if ( max < 4 ) nd_bpe = 2;
6283: else if ( max < 8 ) nd_bpe = 3;
6284: else if ( max < 16 ) nd_bpe = 4;
6285: else if ( max < 32 ) nd_bpe = 5;
6286: else if ( max < 64 ) nd_bpe = 6;
6287: else if ( max < 256 ) nd_bpe = 8;
6288: else if ( max < 1024 ) nd_bpe = 10;
6289: else if ( max < 65536 ) nd_bpe = 16;
6290: else nd_bpe = 32;
6291: }
6292: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
6293: UINT t;
6294: int st;
6295: int *v;
6296: /* t = max(weights) */
6297: t = 0;
6298: if ( current_dl_weight_vector )
6299: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
6300: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
6301: if ( t < st ) t = st;
6302: }
6303: if ( nd_matrix )
6304: for ( i = 0; i < nd_matrix_len; i++ )
6305: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
6306: if ( (st=v[j]) < 0 ) st = -st;
6307: if ( t < st ) t = st;
6308: }
6309: /* i = bitsize of t */
6310: for ( i = 0; t; t >>=1, i++ );
6311: /* i += bitsize of nd_nvar */
6312: for ( t = nd_nvar; t; t >>=1, i++);
6313: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
6314: if ( (nd_bpe+i) >= 31 )
6315: error("nd_setup_parameters : too large weight");
6316: }
6317: nd_epw = (sizeof(UINT)*8)/nd_bpe;
6318: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
6319: nd_exporigin = nd_get_exporigin(nd_ord);
6320: wpd = nd_exporigin+elen;
6321: if ( nd_module )
6322: nd_mpos = nd_exporigin-1;
6323: else
6324: nd_mpos = -1;
6325: if ( wpd != nd_wpd ) {
6326: nd_free_private_storage();
6327: nd_wpd = wpd;
6328: }
6329: if ( nd_bpe < 32 ) {
6330: nd_mask0 = (1<<nd_bpe)-1;
6331: } else {
6332: nd_mask0 = 0xffffffff;
6333: }
6334: bzero(nd_mask,sizeof(nd_mask));
6335: nd_mask1 = 0;
6336: for ( i = 0; i < nd_epw; i++ ) {
6337: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
6338: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
6339: }
6340: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
6341: nd_epos = nd_create_epos(nd_ord);
6342: nd_blockmask = nd_create_blockmask(nd_ord);
6343: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
6344: }
6345:
6346: ND_pairs nd_reconstruct(int trace,ND_pairs d)
6347: {
6348: int i,obpe,oadv,h;
6349: static NM prev_nm_free_list;
6350: static ND_pairs prev_ndp_free_list;
6351: RHist mr0,mr;
6352: RHist r;
6353: RHist *old_red;
6354: ND_pairs s0,s,t;
6355: EPOS oepos;
6356:
6357: obpe = nd_bpe;
6358: oadv = nmv_adv;
6359: oepos = nd_epos;
6360: if ( obpe < 2 ) nd_bpe = 2;
6361: else if ( obpe < 3 ) nd_bpe = 3;
6362: else if ( obpe < 4 ) nd_bpe = 4;
6363: else if ( obpe < 5 ) nd_bpe = 5;
6364: else if ( obpe < 6 ) nd_bpe = 6;
6365: else if ( obpe < 8 ) nd_bpe = 8;
6366: else if ( obpe < 10 ) nd_bpe = 10;
6367: else if ( obpe < 16 ) nd_bpe = 16;
6368: else if ( obpe < 32 ) nd_bpe = 32;
6369: else error("nd_reconstruct : exponent too large");
6370:
6371: nd_setup_parameters(nd_nvar,0);
6372: prev_nm_free_list = _nm_free_list;
6373: prev_ndp_free_list = _ndp_free_list;
6374: _nm_free_list = 0;
6375: _ndp_free_list = 0;
6376: for ( i = nd_psn-1; i >= 0; i-- ) {
6377: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6378: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6379: }
6380: if ( trace )
6381: for ( i = nd_psn-1; i >= 0; i-- ) {
6382: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6383: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6384: }
6385: s0 = 0;
6386: for ( t = d; t; t = NEXT(t) ) {
6387: NEXTND_pairs(s0,s);
6388: s->i1 = t->i1;
6389: s->i2 = t->i2;
1.24 noro 6390: s->sig = t->sig;
1.1 noro 6391: SG(s) = SG(t);
6392: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6393: }
6394:
6395: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6396: for ( i = 0; i < REDTAB_LEN; i++ ) {
6397: old_red[i] = nd_red[i];
6398: nd_red[i] = 0;
6399: }
6400: for ( i = 0; i < REDTAB_LEN; i++ )
6401: for ( r = old_red[i]; r; r = NEXT(r) ) {
6402: NEWRHist(mr);
6403: mr->index = r->index;
6404: SG(mr) = SG(r);
6405: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6406: h = ndl_hash_value(DL(mr));
6407: NEXT(mr) = nd_red[h];
6408: nd_red[h] = mr;
1.24 noro 6409: mr->sig = r->sig;
1.1 noro 6410: }
6411: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6412: old_red = 0;
6413: for ( i = 0; i < nd_psn; i++ ) {
6414: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6415: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 6416: r->sig = nd_psh[i]->sig;
1.1 noro 6417: nd_psh[i] = r;
6418: }
6419: if ( s0 ) NEXT(s) = 0;
6420: prev_nm_free_list = 0;
6421: prev_ndp_free_list = 0;
6422: #if 0
6423: GC_gcollect();
6424: #endif
6425: return s0;
6426: }
6427:
1.30 noro 6428: void nd_reconstruct_s(int trace,ND_pairs *d)
6429: {
6430: int i,obpe,oadv,h;
6431: static NM prev_nm_free_list;
6432: static ND_pairs prev_ndp_free_list;
6433: RHist mr0,mr;
6434: RHist r;
6435: RHist *old_red;
6436: ND_pairs s0,s,t;
6437: EPOS oepos;
6438:
6439: obpe = nd_bpe;
6440: oadv = nmv_adv;
6441: oepos = nd_epos;
6442: if ( obpe < 2 ) nd_bpe = 2;
6443: else if ( obpe < 3 ) nd_bpe = 3;
6444: else if ( obpe < 4 ) nd_bpe = 4;
6445: else if ( obpe < 5 ) nd_bpe = 5;
6446: else if ( obpe < 6 ) nd_bpe = 6;
6447: else if ( obpe < 8 ) nd_bpe = 8;
6448: else if ( obpe < 10 ) nd_bpe = 10;
6449: else if ( obpe < 16 ) nd_bpe = 16;
6450: else if ( obpe < 32 ) nd_bpe = 32;
6451: else error("nd_reconstruct_s : exponent too large");
6452:
6453: nd_setup_parameters(nd_nvar,0);
6454: prev_nm_free_list = _nm_free_list;
6455: prev_ndp_free_list = _ndp_free_list;
6456: _nm_free_list = 0;
6457: _ndp_free_list = 0;
6458: for ( i = nd_psn-1; i >= 0; i-- ) {
6459: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6460: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6461: }
6462: if ( trace )
6463: for ( i = nd_psn-1; i >= 0; i-- ) {
6464: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6465: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6466: }
6467:
6468: for ( i = 0; i < nd_nbase; i++ ) {
6469: s0 = 0;
6470: for ( t = d[i]; t; t = NEXT(t) ) {
6471: NEXTND_pairs(s0,s);
6472: s->i1 = t->i1;
6473: s->i2 = t->i2;
6474: s->sig = t->sig;
6475: SG(s) = SG(t);
6476: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6477: }
1.47 noro 6478: if ( s0 ) NEXT(s) = 0;
1.30 noro 6479: d[i] = s0;
6480: }
6481:
6482: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6483: for ( i = 0; i < REDTAB_LEN; i++ ) {
6484: old_red[i] = nd_red[i];
6485: nd_red[i] = 0;
6486: }
6487: for ( i = 0; i < REDTAB_LEN; i++ )
6488: for ( r = old_red[i]; r; r = NEXT(r) ) {
6489: NEWRHist(mr);
6490: mr->index = r->index;
6491: SG(mr) = SG(r);
6492: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6493: h = ndl_hash_value(DL(mr));
6494: NEXT(mr) = nd_red[h];
6495: nd_red[h] = mr;
6496: mr->sig = r->sig;
6497: }
6498: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6499: old_red = 0;
6500: for ( i = 0; i < nd_psn; i++ ) {
6501: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6502: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
6503: r->sig = nd_psh[i]->sig;
6504: nd_psh[i] = r;
6505: }
6506: if ( s0 ) NEXT(s) = 0;
6507: prev_nm_free_list = 0;
6508: prev_ndp_free_list = 0;
6509: #if 0
6510: GC_gcollect();
6511: #endif
6512: }
6513:
1.1 noro 6514: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
6515: {
6516: int n,i,ei,oepw,omask0,j,s,ord_l,l;
6517: struct order_pair *op;
6518:
6519: n = nd_nvar;
6520: oepw = (sizeof(UINT)*8)/obpe;
6521: omask0 = (1<<obpe)-1;
6522: TD(r) = TD(d);
6523: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
6524: if ( nd_blockmask ) {
6525: l = nd_blockmask->n;
6526: op = nd_blockmask->order_pair;
6527: for ( i = 1; i < nd_exporigin; i++ )
6528: r[i] = d[i];
6529: for ( j = 0, s = 0; j < l; j++ ) {
6530: ord_l = op[j].length;
6531: for ( i = 0; i < ord_l; i++, s++ ) {
6532: ei = GET_EXP_OLD(d,s);
6533: PUT_EXP(r,s,ei);
6534: }
6535: }
6536: } else {
6537: for ( i = 0; i < n; i++ ) {
6538: ei = GET_EXP_OLD(d,i);
6539: PUT_EXP(r,i,ei);
6540: }
6541: }
6542: if ( nd_module ) MPOS(r) = MPOS(d);
6543: }
6544:
6545: ND nd_copy(ND p)
6546: {
6547: NM m,mr,mr0;
6548: int c,n;
6549: ND r;
6550:
6551: if ( !p )
6552: return 0;
6553: else {
6554: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
6555: NEXTNM(mr0,mr);
6556: CM(mr) = CM(m);
6557: ndl_copy(DL(m),DL(mr));
6558: }
6559: NEXT(mr) = 0;
6560: MKND(NV(p),mr0,LEN(p),r);
6561: SG(r) = SG(p);
6562: return r;
6563: }
6564: }
6565:
6566: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
6567: {
6568: NM m1,m2;
6569: NDV p1,p2;
6570: ND t1,t2;
6571: UINT *lcm;
6572: P gp,tp;
6573: Z g,t;
6574: Z iq;
6575: int td;
6576: LIST hist;
6577: NODE node;
6578: DP d;
6579:
6580: if ( !mod && nd_demand ) {
6581: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
6582: } else {
6583: if ( trace ) {
6584: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
6585: } else {
6586: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
6587: }
6588: }
6589: lcm = LCM(p);
6590: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
6591: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
6592: FREENM(m1); return 0;
6593: }
6594: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
6595: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
6596: FREENM(m1); FREENM(m2); return 0;
6597: }
6598:
6599: if ( mod == -1 ) {
6600: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
6601: } else if ( mod > 0 ) {
6602: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
6603: } else if ( mod == -2 ) {
6604: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
6605: } else if ( nd_vc ) {
6606: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
6607: divsp(nd_vc,HCP(p2),gp,&CP(m1));
6608: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
6609: } else {
1.6 noro 6610: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 6611: }
6612: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
6613: *rp = nd_add(mod,t1,t2);
6614: if ( nd_gentrace ) {
6615: /* nd_tracelist is initialized */
1.6 noro 6616: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6617: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 6618: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6619: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
6620: nd_tracelist = node;
6621: }
1.24 noro 6622: if ( *rp )
6623: (*rp)->sig = p->sig;
1.1 noro 6624: FREENM(m1); FREENM(m2);
6625: return 1;
6626: }
6627:
6628: void ndv_mul_c(int mod,NDV p,int mul)
6629: {
6630: NMV m;
6631: int c,c1,len,i;
6632:
6633: if ( !p ) return;
6634: len = LEN(p);
6635: if ( mod == -1 )
6636: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
6637: CM(m) = _mulsf(CM(m),mul);
6638: else
6639: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6640: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6641: }
6642: }
6643:
6644: void ndv_mul_c_lf(NDV p,Z mul)
6645: {
6646: NMV m;
6647: Z c;
6648: int len,i;
6649:
6650: if ( !p ) return;
6651: len = LEN(p);
6652: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6653: mullf(CZ(m),mul,&c); CZ(m) = c;
6654: }
6655: }
6656:
6657: /* for nd_det */
6658: void ndv_mul_c_q(NDV p,Z mul)
6659: {
6660: NMV m;
6661: Z c;
6662: int len,i;
6663:
6664: if ( !p ) return;
6665: len = LEN(p);
6666: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6667: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6668: }
6669: }
6670:
6671: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6672: int n2,i,j,l,n,tlen;
6673: UINT *d0;
6674: NM *tab,*psum;
6675: ND s,r;
6676: NM t;
6677: NMV m1;
6678:
6679: if ( !p ) return 0;
6680: n = NV(p); n2 = n>>1;
6681: d0 = DL(m0);
6682: l = LEN(p);
6683: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6684: tab = (NM *)MALLOC(tlen*sizeof(NM));
6685: psum = (NM *)MALLOC(tlen*sizeof(NM));
6686: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6687: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6688: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6689: /* m0(NM) * m1(NMV) => tab(NM) */
6690: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6691: for ( j = 0; j < tlen; j++ ) {
6692: if ( tab[j] ) {
6693: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6694: }
6695: }
6696: }
6697: for ( i = tlen-1, r = 0; i >= 0; i-- )
6698: if ( psum[i] ) {
6699: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6700: MKND(n,psum[i],j,s);
6701: r = nd_add(mod,r,s);
6702: }
6703: if ( r ) SG(r) = SG(p)+TD(d0);
6704: return r;
6705: }
6706:
6707: /* product of monomials */
6708: /* XXX block order is not handled correctly */
6709:
6710: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6711: {
6712: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6713: UINT *d0,*d1,*d,*dt,*ctab;
6714: Z *ctab_q;
6715: Z q,q1;
6716: UINT c0,c1,c;
6717: NM *p;
6718: NM m,t;
6719: int mpos;
6720:
6721: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6722: if ( !m0 || !m1 ) return;
6723: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6724: if ( nd_module )
6725: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6726:
6727: NEWNM(m); d = DL(m);
6728: if ( mod ) {
6729: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6730: } else if ( nd_vc )
6731: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6732: else
1.6 noro 6733: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6734: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6735: homo = n&1 ? 1 : 0;
6736: if ( homo ) {
6737: /* offset of h-degree */
6738: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6739: PUT_EXP(DL(m),n-1,h);
6740: TD(DL(m)) = h;
6741: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6742: }
6743: tab[0] = m;
6744: NEWNM(m); d = DL(m);
6745: for ( i = 0, curlen = 1; i < n2; i++ ) {
6746: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6747: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6748: /* xi^a*(Di^k*xi^l)*Di^b */
6749: a += l; b += k;
6750: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6751: if ( !k || !l ) {
6752: for ( j = 0; j < curlen; j++ )
6753: if ( (t = tab[j]) != 0 ) {
6754: dt = DL(t);
6755: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6756: if ( nd_blockmask ) ndl_weight_mask(dt);
6757: }
6758: curlen *= k+1;
6759: continue;
6760: }
6761: min = MIN(k,l);
6762: if ( mod ) {
6763: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6764: mkwcm(k,l,mod,(int *)ctab);
6765: } else {
6766: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6767: mkwc(k,l,ctab_q);
6768: }
6769: for ( j = min; j >= 0; j-- ) {
6770: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6771: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6772: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6773: if ( homo ) {
6774: TD(d) = s;
6775: PUT_EXP(d,n-1,s-h);
6776: } else TD(d) = h;
6777: if ( nd_blockmask ) ndl_weight_mask(d);
6778: if ( mod ) c = ctab[j];
6779: else q = ctab_q[j];
6780: p = tab+curlen*j;
6781: if ( j == 0 ) {
6782: for ( u = 0; u < curlen; u++, p++ ) {
6783: if ( tab[u] ) {
6784: ndl_addto(DL(tab[u]),d);
6785: if ( mod ) {
6786: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6787: } else if ( nd_vc )
6788: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6789: else {
1.6 noro 6790: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6791: }
6792: }
6793: }
6794: } else {
6795: for ( u = 0; u < curlen; u++, p++ ) {
6796: if ( tab[u] ) {
6797: NEWNM(t);
6798: ndl_add(DL(tab[u]),d,DL(t));
6799: if ( mod ) {
6800: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6801: } else if ( nd_vc )
6802: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6803: else
1.6 noro 6804: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6805: *p = t;
6806: }
6807: }
6808: }
6809: }
6810: curlen *= k+1;
6811: }
6812: FREENM(m);
6813: if ( nd_module ) {
6814: mpos = MPOS(d1);
6815: for ( i = 0; i < tlen; i++ )
6816: if ( tab[i] ) {
6817: d = DL(tab[i]);
6818: MPOS(d) = mpos;
6819: TD(d) = ndl_weight(d);
6820: }
6821: }
6822: }
6823:
6824: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6825: {
6826: NM mr,mr0;
6827: NMV m;
6828: UINT *d,*dt,*dm;
6829: int c,n,td,i,c1,c2,len;
6830: Q q;
6831: ND r;
6832:
6833: if ( !p ) return 0;
6834: else {
6835: n = NV(p); m = BDY(p);
6836: d = DL(m0);
6837: len = LEN(p);
6838: mr0 = 0;
6839: td = TD(d);
6840: c = CM(m0);
6841: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6842: NEXTNM(mr0,mr);
6843: CM(mr) = 1;
6844: ndl_add(DL(m),d,DL(mr));
6845: }
6846: NEXT(mr) = 0;
6847: MKND(NV(p),mr0,len,r);
6848: SG(r) = SG(p) + TD(d);
6849: return r;
6850: }
6851: }
6852:
6853: ND ndv_mul_nm(int mod,NM m0,NDV p)
6854: {
6855: NM mr,mr0;
6856: NMV m;
6857: UINT *d,*dt,*dm;
6858: int c,n,td,i,c1,c2,len;
6859: P q;
6860: ND r;
6861:
6862: if ( !p ) return 0;
6863: else if ( do_weyl ) {
6864: if ( mod < 0 ) {
6865: error("ndv_mul_nm : not implemented (weyl)");
6866: return 0;
6867: } else
6868: return weyl_ndv_mul_nm(mod,m0,p);
6869: } else {
6870: n = NV(p); m = BDY(p);
6871: d = DL(m0);
6872: len = LEN(p);
6873: mr0 = 0;
6874: td = TD(d);
6875: if ( mod == -1 ) {
6876: c = CM(m0);
6877: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6878: NEXTNM(mr0,mr);
6879: CM(mr) = _mulsf(CM(m),c);
6880: ndl_add(DL(m),d,DL(mr));
6881: }
6882: } else if ( mod == -2 ) {
6883: Z cl;
6884: cl = CZ(m0);
6885: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6886: NEXTNM(mr0,mr);
6887: mullf(CZ(m),cl,&CZ(mr));
6888: ndl_add(DL(m),d,DL(mr));
6889: }
6890: } else if ( mod ) {
6891: c = CM(m0);
6892: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6893: NEXTNM(mr0,mr);
6894: c1 = CM(m);
6895: DMAR(c1,c,0,mod,c2);
6896: CM(mr) = c2;
6897: ndl_add(DL(m),d,DL(mr));
6898: }
6899: } else {
6900: q = CP(m0);
6901: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6902: NEXTNM(mr0,mr);
6903: mulp(nd_vc,CP(m),q,&CP(mr));
6904: ndl_add(DL(m),d,DL(mr));
6905: }
6906: }
6907: NEXT(mr) = 0;
6908: MKND(NV(p),mr0,len,r);
6909: SG(r) = SG(p) + TD(d);
6910: return r;
6911: }
6912: }
6913:
6914: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6915: {
6916: NM mq0,mq;
6917: NMV tm;
6918: Q q;
6919: int i,nv,sg,c,c1,c2,hindex;
6920: ND p,t,r;
6921:
6922: if ( bucket->m < 0 ) return 0;
6923: else {
6924: nv = NV(d);
6925: mq0 = 0;
6926: tm = (NMV)MALLOC(nmv_adv);
6927: while ( 1 ) {
6928: if ( mod > 0 || mod == -1 )
6929: hindex = head_pbucket(mod,bucket);
6930: else if ( mod == -2 )
6931: hindex = head_pbucket_lf(bucket);
6932: else
6933: hindex = head_pbucket_q(bucket);
6934: if ( hindex < 0 ) break;
6935: p = bucket->body[hindex];
6936: NEXTNM(mq0,mq);
6937: ndl_sub(HDL(p),HDL(d),DL(mq));
6938: ndl_copy(DL(mq),DL(tm));
6939: if ( mod ) {
6940: c1 = invm(HCM(d),mod); c2 = HCM(p);
6941: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6942: CM(tm) = mod-c;
6943: } else {
1.6 noro 6944: divsz(HCZ(p),HCZ(d),&CZ(mq));
6945: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6946: }
6947: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6948: bucket->body[hindex] = nd_remove_head(p);
6949: t = nd_remove_head(t);
6950: add_pbucket(mod,bucket,t);
6951: }
6952: if ( !mq0 )
6953: r = 0;
6954: else {
6955: NEXT(mq) = 0;
6956: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6957: MKND(nv,mq0,i,r);
6958: /* XXX */
6959: SG(r) = HTD(r);
6960: }
6961: return r;
6962: }
6963: }
6964:
6965: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6966: {
6967: NMV m,mr,mr0,t;
6968: int len,i,k;
6969:
6970: if ( !p ) return;
6971: m = BDY(p); len = LEN(p);
6972: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6973: m = (NMV)((char *)mr0+(len-1)*oadv);
6974: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6975: t = (NMV)MALLOC(nmv_adv);
6976: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6977: CZ(t) = CZ(m);
1.1 noro 6978: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6979: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6980: CZ(mr) = CZ(t);
1.1 noro 6981: ndl_copy(DL(t),DL(mr));
6982: }
6983: BDY(p) = mr0;
6984: }
6985:
6986: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6987: {
6988: NMV m,mr,mr0;
6989: int len,i;
6990: NDV r;
6991:
6992: if ( !p ) return 0;
6993: m = BDY(p); len = LEN(p);
6994: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6995: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6996: ndl_zero(DL(mr));
6997: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6998: CZ(mr) = CZ(m);
1.1 noro 6999: }
7000: MKNDV(NV(p),mr0,len,r);
7001: SG(r) = SG(p);
1.24 noro 7002: r->sig = p->sig;
1.1 noro 7003: return r;
7004: }
7005:
7006: /* duplicate p */
7007:
7008: NDV ndv_dup(int mod,NDV p)
7009: {
7010: NDV d;
7011: NMV t,m,m0;
7012: int i,len;
7013:
7014: if ( !p ) return 0;
7015: len = LEN(p);
7016: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
7017: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
7018: ndl_copy(DL(t),DL(m));
1.6 noro 7019: CZ(m) = CZ(t);
1.1 noro 7020: }
7021: MKNDV(NV(p),m0,len,d);
7022: SG(d) = SG(p);
7023: return d;
7024: }
7025:
7026: NDV ndv_symbolic(int mod,NDV p)
7027: {
7028: NDV d;
7029: NMV t,m,m0;
7030: int i,len;
7031:
7032: if ( !p ) return 0;
7033: len = LEN(p);
7034: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
7035: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
7036: ndl_copy(DL(t),DL(m));
1.6 noro 7037: CZ(m) = ONE;
1.1 noro 7038: }
7039: MKNDV(NV(p),m0,len,d);
7040: SG(d) = SG(p);
7041: return d;
7042: }
7043:
7044: ND nd_dup(ND p)
7045: {
7046: ND d;
7047: NM t,m,m0;
7048:
7049: if ( !p ) return 0;
7050: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
7051: NEXTNM(m0,m);
7052: ndl_copy(DL(t),DL(m));
1.6 noro 7053: CZ(m) = CZ(t);
1.1 noro 7054: }
7055: if ( m0 ) NEXT(m) = 0;
7056: MKND(NV(p),m0,LEN(p),d);
7057: SG(d) = SG(p);
7058: return d;
7059: }
7060:
7061: /* XXX if p->len == 0 then it represents 0 */
7062:
7063: void ndv_mod(int mod,NDV p)
7064: {
7065: NMV t,d;
7066: int r,s,u;
7067: int i,len,dlen;
7068: P cp;
7069: Z c;
7070: Obj gfs;
7071:
7072: if ( !p ) return;
7073: len = LEN(p);
7074: dlen = 0;
7075: if ( mod == -1 )
7076: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7077: simp_ff((Obj)CP(t),&gfs);
7078: if ( gfs ) {
7079: r = FTOIF(CONT((GFS)gfs));
7080: CM(d) = r;
7081: ndl_copy(DL(t),DL(d));
7082: NMV_ADV(d);
7083: dlen++;
7084: }
7085: }
7086: else if ( mod == -2 )
7087: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7088: simp_ff((Obj)CP(t),&gfs);
7089: if ( gfs ) {
7090: lmtolf((LM)gfs,&CZ(d));
7091: ndl_copy(DL(t),DL(d));
7092: NMV_ADV(d);
7093: dlen++;
7094: }
7095: }
7096: else
7097: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7098: if ( nd_vc ) {
7099: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
7100: c = (Z)cp;
7101: } else
1.6 noro 7102: c = CZ(t);
1.1 noro 7103: r = remqi((Q)c,mod);
7104: if ( r ) {
7105: CM(d) = r;
7106: ndl_copy(DL(t),DL(d));
7107: NMV_ADV(d);
7108: dlen++;
7109: }
7110: }
7111: LEN(p) = dlen;
7112: }
7113:
7114: NDV ptondv(VL vl,VL dvl,P p)
7115: {
7116: ND nd;
7117:
7118: nd = ptond(vl,dvl,p);
7119: return ndtondv(0,nd);
7120: }
7121:
7122: void pltozpl(LIST l,Q *cont,LIST *pp)
7123: {
1.16 noro 7124: NODE nd,nd1;
7125: int n;
7126: P *pl;
7127: Q *cl;
7128: int i;
7129: P dmy;
7130: Z dvr,inv;
7131: LIST r;
7132:
7133: nd = BDY(l); n = length(nd);
7134: pl = (P *)MALLOC(n*sizeof(P));
7135: cl = (Q *)MALLOC(n*sizeof(Q));
7136: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
7137: ptozp((P)BDY(nd),1,&cl[i],&dmy);
7138: }
7139: qltozl(cl,n,&dvr);
7140: divz(ONE,dvr,&inv);
7141: nd = BDY(l);
7142: for ( i = 0; i < n; i++, nd = NEXT(nd) )
7143: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
7144: nd = 0;
7145: for ( i = n-1; i >= 0; i-- ) {
7146: MKNODE(nd1,pl[i],nd); nd = nd1;
7147: }
7148: MKLIST(r,nd);
7149: *pp = r;
1.1 noro 7150: }
7151:
7152: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
7153:
7154: NDV pltondv(VL vl,VL dvl,LIST p)
7155: {
7156: int i;
7157: NODE t;
7158: ND r,ri;
7159: NM m;
7160:
7161: if ( !nd_module ) error("pltond : module order must be set");
7162: r = 0;
7163: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
7164: ri = ptond(vl,dvl,(P)BDY(t));
7165: if ( ri )
7166: for ( m = BDY(ri); m; m = NEXT(m) ) {
7167: MPOS(DL(m)) = i;
7168: TD(DL(m)) = ndl_weight(DL(m));
7169: if ( nd_blockmask ) ndl_weight_mask(DL(m));
7170: }
7171: r = nd_add(0,r,ri);
7172: }
7173: return ndtondv(0,r);
7174: }
7175:
7176: ND ptond(VL vl,VL dvl,P p)
7177: {
7178: int n,i,j,k,e;
7179: VL tvl;
7180: V v;
7181: DCP dc;
7182: DCP *w;
7183: ND r,s,t,u;
7184: P x;
7185: int c;
7186: UINT *d;
7187: NM m,m0;
7188:
7189: if ( !p )
7190: return 0;
7191: else if ( NUM(p) ) {
7192: NEWNM(m);
7193: ndl_zero(DL(m));
1.48 noro 7194: if ( RATN(p) && !INT((Q)p) )
1.1 noro 7195: error("ptond : input must be integer-coefficient");
1.6 noro 7196: CZ(m) = (Z)p;
1.1 noro 7197: NEXT(m) = 0;
7198: MKND(nd_nvar,m,1,r);
7199: SG(r) = 0;
7200: return r;
7201: } else {
7202: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
7203: w = (DCP *)MALLOC(k*sizeof(DCP));
7204: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
7205: for ( i = 0, tvl = dvl, v = VR(p);
7206: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
7207: if ( !tvl ) {
7208: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
7209: t = ptond(vl,dvl,COEF(w[j]));
7210: pwrp(vl,x,DEG(w[j]),&p);
7211: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
7212: }
7213: return s;
7214: } else {
7215: NEWNM(m0); d = DL(m0);
7216: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 7217: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 7218: TD(d) = MUL_WEIGHT(e,i);
7219: if ( nd_blockmask) ndl_weight_mask(d);
7220: if ( nd_module ) MPOS(d) = 0;
7221: t = ptond(vl,dvl,COEF(w[j]));
7222: for ( m = BDY(t); m; m = NEXT(m) )
7223: ndl_addto(DL(m),d);
7224: SG(t) += TD(d);
7225: s = nd_add(0,s,t);
7226: }
7227: FREENM(m0);
7228: return s;
7229: }
7230: }
7231: }
7232:
7233: P ndvtop(int mod,VL vl,VL dvl,NDV p)
7234: {
7235: VL tvl;
7236: int len,n,j,i,e;
7237: NMV m;
7238: Z q;
7239: P c;
7240: UINT *d;
7241: P s,r,u,t,w;
7242: GFS gfs;
7243:
7244: if ( !p ) return 0;
7245: else {
7246: len = LEN(p);
7247: n = NV(p);
7248: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7249: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
7250: if ( mod == -1 ) {
7251: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7252: } else if ( mod == -2 ) {
7253: c = (P)CZ(m);
7254: } else if ( mod > 0 ) {
1.6 noro 7255: STOZ(CM(m),q); c = (P)q;
1.1 noro 7256: } else
7257: c = CP(m);
7258: d = DL(m);
7259: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7260: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7261: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7262: }
7263: addp(vl,s,t,&u); s = u;
7264: }
7265: return s;
7266: }
7267: }
7268:
7269: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
7270: {
7271: VL tvl;
7272: int len,n,j,i,e;
7273: NMV m;
7274: Z q;
7275: P c;
7276: UINT *d;
7277: P s,r,u,t,w;
7278: GFS gfs;
7279: P *a;
7280: LIST l;
7281: NODE nd,nd1;
7282:
7283: if ( !p ) return 0;
7284: else {
7285: a = (P *)MALLOC((rank+1)*sizeof(P));
7286: for ( i = 0; i <= rank; i++ ) a[i] = 0;
7287: len = LEN(p);
7288: n = NV(p);
7289: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7290: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
7291: if ( mod == -1 ) {
7292: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7293: } else if ( mod ) {
1.6 noro 7294: STOZ(CM(m),q); c = (P)q;
1.1 noro 7295: } else
7296: c = CP(m);
7297: d = DL(m);
7298: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7299: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7300: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7301: }
7302: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
7303: }
7304: nd = 0;
7305: for ( i = rank; i > 0; i-- ) {
7306: MKNODE(nd1,a[i],nd); nd = nd1;
7307: }
7308: MKLIST(l,nd);
7309: return l;
7310: }
7311: }
7312:
7313: NDV ndtondv(int mod,ND p)
7314: {
7315: NDV d;
7316: NMV m,m0;
7317: NM t;
7318: int i,len;
7319:
7320: if ( !p ) return 0;
7321: len = LEN(p);
7322: if ( mod > 0 || mod == -1 )
7323: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7324: else
7325: m0 = m = MALLOC(len*nmv_adv);
7326: #if 0
7327: ndv_alloc += nmv_adv*len;
7328: #endif
7329: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
7330: ndl_copy(DL(t),DL(m));
1.6 noro 7331: CZ(m) = CZ(t);
1.1 noro 7332: }
7333: MKNDV(NV(p),m0,len,d);
7334: SG(d) = SG(p);
1.24 noro 7335: d->sig = p->sig;
1.1 noro 7336: return d;
7337: }
7338:
1.16 noro 7339: static int dmm_comp_nv;
7340:
7341: int dmm_comp(DMM *a,DMM *b)
7342: {
7343: return -compdmm(dmm_comp_nv,*a,*b);
7344: }
7345:
7346: void dmm_sort_by_ord(DMM *a,int len,int nv)
7347: {
7348: dmm_comp_nv = nv;
7349: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
7350: }
7351:
7352: void dpm_sort(DPM p,DPM *rp)
7353: {
7354: DMM t,t1;
7355: int len,i,n;
7356: DMM *a;
7357: DPM d;
7358:
7359: if ( !p ) *rp = 0;
7360: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7361: a = (DMM *)MALLOC(len*sizeof(DMM));
7362: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7363: n = p->nv;
7364: dmm_sort_by_ord(a,len,n);
7365: t = 0;
7366: for ( i = len-1; i >= 0; i-- ) {
7367: NEWDMM(t1);
7368: t1->c = a[i]->c;
7369: t1->dl = a[i]->dl;
7370: t1->pos = a[i]->pos;
7371: t1->next = t;
7372: t = t1;
7373: }
7374: MKDPM(n,t,d);
7375: SG(d) = SG(p);
7376: *rp = d;
7377: }
7378:
1.18 noro 7379: int dpm_comp(DPM *a,DPM *b)
7380: {
1.22 noro 7381: return -compdpm(CO,*a,*b);
1.18 noro 7382: }
7383:
7384: NODE dpm_sort_list(NODE l)
7385: {
7386: int i,len;
7387: NODE t,t1;
7388: DPM *a;
7389:
7390: len = length(l);
7391: a = (DPM *)MALLOC(len*sizeof(DPM));
7392: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
7393: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
7394: t = 0;
7395: for ( i = len-1; i >= 0; i-- ) {
7396: MKNODE(t1,(pointer)a[i],t); t = t1;
7397: }
7398: return t;
7399: }
7400:
1.20 noro 7401: int nmv_comp(NMV a,NMV b)
7402: {
1.21 noro 7403: int t;
7404: t = DL_COMPARE(a->dl,b->dl);
7405: return -t;
1.20 noro 7406: }
7407:
1.43 noro 7408: NDV dptondv(int mod,DP p)
7409: {
7410: NDV d;
7411: NMV m,m0;
7412: MP t;
7413: MP *a;
7414: int i,len,n;
7415:
7416: if ( !p ) return 0;
7417: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7418: n = p->nv;
7419: if ( mod > 0 || mod == -1 )
7420: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7421: else
7422: m0 = m = MALLOC(len*nmv_adv);
7423: for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
7424: dltondl(n,t->dl,DL(m));
7425: TD(DL(m)) = ndl_weight(DL(m));
7426: CZ(m) = (Z)t->c;
7427: }
7428: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
7429: MKNDV(NV(p),m0,len,d);
7430: SG(d) = SG(p);
7431: return d;
7432: }
7433:
1.16 noro 7434: NDV dpmtondv(int mod,DPM p)
7435: {
7436: NDV d;
7437: NMV m,m0;
7438: DMM t;
7439: DMM *a;
7440: int i,len,n;
7441:
7442: if ( !p ) return 0;
7443: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7444: a = (DMM *)MALLOC(len*sizeof(DMM));
7445: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7446: n = p->nv;
7447: dmm_sort_by_ord(a,len,n);
7448: if ( mod > 0 || mod == -1 )
7449: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7450: else
7451: m0 = m = MALLOC(len*nmv_adv);
7452: #if 0
7453: ndv_alloc += nmv_adv*len;
7454: #endif
7455: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7456: dltondl(n,a[i]->dl,DL(m));
7457: MPOS(DL(m)) = a[i]->pos;
1.20 noro 7458: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 7459: CZ(m) = (Z)a[i]->c;
7460: }
1.20 noro 7461: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 7462: MKNDV(NV(p),m0,len,d);
7463: SG(d) = SG(p);
7464: return d;
7465: }
7466:
1.1 noro 7467: ND ndvtond(int mod,NDV p)
7468: {
7469: ND d;
7470: NM m,m0;
7471: NMV t;
7472: int i,len;
7473:
7474: if ( !p ) return 0;
7475: m0 = 0;
7476: len = p->len;
7477: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7478: NEXTNM(m0,m);
7479: ndl_copy(DL(t),DL(m));
1.6 noro 7480: CZ(m) = CZ(t);
1.1 noro 7481: }
7482: NEXT(m) = 0;
7483: MKND(NV(p),m0,len,d);
7484: SG(d) = SG(p);
1.28 noro 7485: d->sig = p->sig;
1.1 noro 7486: return d;
7487: }
7488:
7489: DP ndvtodp(int mod,NDV p)
7490: {
7491: MP m,m0;
7492: DP d;
7493: NMV t;
7494: int i,len;
7495:
7496: if ( !p ) return 0;
7497: m0 = 0;
7498: len = p->len;
7499: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7500: NEXTMP(m0,m);
7501: m->dl = ndltodl(nd_nvar,DL(t));
7502: m->c = (Obj)ndctop(mod,t->c);
7503: }
7504: NEXT(m) = 0;
7505: MKDP(nd_nvar,m0,d);
7506: SG(d) = SG(p);
7507: return d;
7508: }
7509:
1.44 noro 7510: DPM sigtodpm(SIG s)
7511: {
7512: DMM m;
7513: DPM d;
7514:
7515: NEWDMM(m);
7516: m->c = (Obj)ONE;
7517: m->dl = s->dl;
7518: m->pos = s->pos+1;
7519: m->next = 0;
7520: MKDPM(nd_nvar,m,d);
7521: SG(d) = s->dl->td;
7522: return d;
7523: }
7524:
1.16 noro 7525: DPM ndvtodpm(int mod,NDV p)
7526: {
7527: DMM m,m0;
7528: DPM d;
7529: NMV t;
7530: int i,len;
7531:
7532: if ( !p ) return 0;
7533: m0 = 0;
7534: len = p->len;
7535: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7536: NEXTDMM(m0,m);
7537: m->dl = ndltodl(nd_nvar,DL(t));
7538: m->c = (Obj)ndctop(mod,t->c);
7539: m->pos = MPOS(DL(t));
7540: }
7541: NEXT(m) = 0;
7542: MKDPM(nd_nvar,m0,d);
7543: SG(d) = SG(p);
7544: return d;
7545: }
7546:
7547:
1.1 noro 7548: DP ndtodp(int mod,ND p)
7549: {
7550: MP m,m0;
7551: DP d;
7552: NM t;
7553: int i,len;
7554:
7555: if ( !p ) return 0;
7556: m0 = 0;
7557: len = p->len;
7558: for ( t = BDY(p); t; t = NEXT(t) ) {
7559: NEXTMP(m0,m);
7560: m->dl = ndltodl(nd_nvar,DL(t));
7561: m->c = (Obj)ndctop(mod,t->c);
7562: }
7563: NEXT(m) = 0;
7564: MKDP(nd_nvar,m0,d);
7565: SG(d) = SG(p);
7566: return d;
7567: }
7568:
7569: void ndv_print(NDV p)
7570: {
7571: NMV m;
7572: int i,len;
7573:
7574: if ( !p ) printf("0\n");
7575: else {
7576: len = LEN(p);
7577: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7578: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 7579: else printf("+%ld*",CM(m));
1.1 noro 7580: ndl_print(DL(m));
7581: }
7582: printf("\n");
7583: }
7584: }
7585:
7586: void ndv_print_q(NDV p)
7587: {
7588: NMV m;
7589: int i,len;
7590:
7591: if ( !p ) printf("0\n");
7592: else {
7593: len = LEN(p);
7594: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7595: printf("+");
1.6 noro 7596: printexpr(CO,(Obj)CZ(m));
1.1 noro 7597: printf("*");
7598: ndl_print(DL(m));
7599: }
7600: printf("\n");
7601: }
7602: }
7603:
7604: NODE ndv_reducebase(NODE x,int *perm)
7605: {
7606: int len,i,j;
7607: NDVI w;
7608: NODE t,t0;
7609:
1.41 noro 7610: if ( nd_norb ) return x;
1.1 noro 7611: len = length(x);
7612: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
7613: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
7614: w[i].p = BDY(t); w[i].i = perm[i];
7615: }
7616: for ( i = 0; i < len; i++ ) {
7617: for ( j = 0; j < i; j++ ) {
7618: if ( w[i].p && w[j].p ) {
7619: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
7620: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
7621: }
7622: }
7623: }
7624: for ( i = j = 0, t0 = 0; i < len; i++ ) {
7625: if ( w[i].p ) {
7626: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
7627: perm[j++] = w[i].i;
7628: }
7629: }
7630: NEXT(t) = 0; x = t0;
7631: return x;
7632: }
7633:
7634: /* XXX incomplete */
7635:
1.21 noro 7636: extern DMMstack dmm_stack;
7637: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 7638:
1.1 noro 7639: void nd_init_ord(struct order_spec *ord)
7640: {
7641: nd_module = (ord->id >= 256);
7642: if ( nd_module ) {
7643: nd_dcomp = -1;
1.21 noro 7644: nd_module_ordtype = ord->module_ordtype;
1.1 noro 7645: nd_pot_nelim = ord->pot_nelim;
7646: nd_poly_weight_len = ord->nv;
7647: nd_poly_weight = ord->top_weight;
7648: nd_module_rank = ord->module_rank;
7649: nd_module_weight = ord->module_top_weight;
7650: }
7651: nd_matrix = 0;
7652: nd_matrix_len = 0;
7653: switch ( ord->id ) {
7654: case 0:
7655: switch ( ord->ord.simple ) {
7656: case 0:
7657: nd_dcomp = 1;
7658: nd_isrlex = 1;
7659: break;
7660: case 1:
7661: nd_dcomp = 1;
7662: nd_isrlex = 0;
7663: break;
7664: case 2:
7665: nd_dcomp = 0;
7666: nd_isrlex = 0;
7667: ndl_compare_function = ndl_lex_compare;
7668: break;
7669: case 11:
7670: /* XXX */
7671: nd_dcomp = 0;
7672: nd_isrlex = 1;
7673: ndl_compare_function = ndl_ww_lex_compare;
7674: break;
7675: default:
7676: error("nd_gr : unsupported order");
7677: }
7678: break;
7679: case 1:
7680: /* block order */
7681: /* XXX */
7682: nd_dcomp = -1;
7683: nd_isrlex = 0;
7684: ndl_compare_function = ndl_block_compare;
7685: break;
7686: case 2:
7687: /* matrix order */
7688: /* XXX */
7689: nd_dcomp = -1;
7690: nd_isrlex = 0;
7691: nd_matrix_len = ord->ord.matrix.row;
7692: nd_matrix = ord->ord.matrix.matrix;
7693: ndl_compare_function = ndl_matrix_compare;
7694: break;
7695: case 3:
7696: /* composite order */
7697: nd_dcomp = -1;
7698: nd_isrlex = 0;
7699: nd_worb_len = ord->ord.composite.length;
7700: nd_worb = ord->ord.composite.w_or_b;
7701: ndl_compare_function = ndl_composite_compare;
7702: break;
7703:
7704: /* module order */
7705: case 256:
7706: switch ( ord->ord.simple ) {
7707: case 0:
1.21 noro 7708: nd_dcomp = 0;
1.1 noro 7709: nd_isrlex = 1;
1.21 noro 7710: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7711: break;
7712: case 1:
1.21 noro 7713: nd_dcomp = 0;
1.1 noro 7714: nd_isrlex = 0;
7715: ndl_compare_function = ndl_module_glex_compare;
7716: break;
7717: case 2:
1.21 noro 7718: nd_dcomp = 0;
1.1 noro 7719: nd_isrlex = 0;
1.21 noro 7720: ndl_compare_function = ndl_module_compare;
7721: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7722: break;
7723: default:
1.21 noro 7724: error("nd_init_ord : unsupported order");
1.1 noro 7725: }
7726: break;
7727: case 257:
7728: /* block order */
7729: nd_isrlex = 0;
1.21 noro 7730: ndl_compare_function = ndl_module_compare;
7731: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7732: break;
7733: case 258:
7734: /* matrix order */
7735: nd_isrlex = 0;
7736: nd_matrix_len = ord->ord.matrix.row;
7737: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7738: ndl_compare_function = ndl_module_compare;
7739: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7740: break;
7741: case 259:
7742: /* composite order */
7743: nd_isrlex = 0;
7744: nd_worb_len = ord->ord.composite.length;
7745: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7746: ndl_compare_function = ndl_module_compare;
7747: ndl_base_compare_function = ndl_composite_compare;
7748: break;
7749: case 300:
7750: /* schreyer order */
7751: if ( ord->base->id != 256 )
7752: error("nd_init_ord : unsupported base order");
7753: ndl_compare_function = ndl_module_schreyer_compare;
7754: dmm_stack = ord->dmmstack;
7755: switch ( ord->base->ord.simple ) {
7756: case 0:
7757: nd_isrlex = 1;
7758: ndl_base_compare_function = ndl_glex_compare;
7759: dl_base_compare_function = cmpdl_revgradlex;
7760: break;
7761: case 1:
7762: nd_isrlex = 0;
7763: ndl_base_compare_function = ndl_glex_compare;
7764: dl_base_compare_function = cmpdl_gradlex;
7765: break;
7766: case 2:
7767: nd_isrlex = 0;
7768: ndl_base_compare_function = ndl_lex_compare;
7769: dl_base_compare_function = cmpdl_lex;
7770: break;
7771: default:
7772: error("nd_init_ord : unsupported order");
7773: }
1.1 noro 7774: break;
7775: }
7776: nd_ord = ord;
7777: }
7778:
7779: BlockMask nd_create_blockmask(struct order_spec *ord)
7780: {
7781: int n,i,j,s,l;
7782: UINT *t;
7783: BlockMask bm;
7784:
7785: /* we only create mask table for block order */
7786: if ( ord->id != 1 && ord->id != 257 )
7787: return 0;
7788: n = ord->ord.block.length;
7789: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7790: bm->n = n;
7791: bm->order_pair = ord->ord.block.order_pair;
7792: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7793: for ( i = 0, s = 0; i < n; i++ ) {
7794: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7795: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7796: l = bm->order_pair[i].length;
7797: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7798: }
7799: return bm;
7800: }
7801:
7802: EPOS nd_create_epos(struct order_spec *ord)
7803: {
7804: int i,j,l,s,ord_l,ord_o;
7805: EPOS epos;
7806: struct order_pair *op;
7807:
7808: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7809: switch ( ord->id ) {
1.21 noro 7810: case 0: case 256: case 300:
1.1 noro 7811: if ( nd_isrlex ) {
7812: for ( i = 0; i < nd_nvar; i++ ) {
7813: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7814: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7815: }
7816: } else {
7817: for ( i = 0; i < nd_nvar; i++ ) {
7818: epos[i].i = nd_exporigin + i/nd_epw;
7819: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7820: }
7821: }
7822: break;
7823: case 1: case 257:
7824: /* block order */
7825: l = ord->ord.block.length;
7826: op = ord->ord.block.order_pair;
7827: for ( j = 0, s = 0; j < l; j++ ) {
7828: ord_o = op[j].order;
7829: ord_l = op[j].length;
7830: if ( !ord_o )
7831: for ( i = 0; i < ord_l; i++ ) {
7832: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7833: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7834: }
7835: else
7836: for ( i = 0; i < ord_l; i++ ) {
7837: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7838: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7839: }
7840: s += ord_l;
7841: }
7842: break;
7843: case 2:
7844: /* matrix order */
7845: case 3:
7846: /* composite order */
7847: default:
7848: for ( i = 0; i < nd_nvar; i++ ) {
7849: epos[i].i = nd_exporigin + i/nd_epw;
7850: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7851: }
7852: break;
7853: }
7854: return epos;
7855: }
7856:
7857: /* external interface */
7858:
7859: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7860: {
7861: NODE t,in0,in;
7862: ND ndf,nf;
7863: NDV ndvf;
7864: VL vv,tv;
7865: int stat,nvar,max,mrank;
7866: union oNDC dn;
7867: Q cont;
7868: P pp;
7869: LIST ppl;
7870:
7871: if ( !f ) {
7872: *rp = 0;
7873: return;
7874: }
7875: pltovl(v,&vv);
7876: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7877:
7878: /* max=65536 implies nd_bpe=32 */
7879: max = 65536;
7880:
7881: nd_module = 0;
7882: /* nd_module will be set if ord is a module ordering */
7883: nd_init_ord(ord);
7884: nd_setup_parameters(nvar,max);
7885: if ( nd_module && OID(f) != O_LIST )
7886: error("nd_nf_p : the first argument must be a list");
7887: if ( nd_module ) mrank = length(BDY((LIST)f));
7888: /* conversion to ndv */
7889: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7890: NEXTNODE(in0,in);
7891: if ( nd_module ) {
7892: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7893: || length(BDY((LIST)BDY(t))) != mrank )
7894: error("nd_nf_p : inconsistent basis element");
7895: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7896: else ppl = (LIST)BDY(t);
7897: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7898: } else {
7899: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7900: else pp = (P)BDY(t);
7901: BDY(in) = (pointer)ptondv(CO,vv,pp);
7902: }
7903: if ( m ) ndv_mod(m,(NDV)BDY(in));
7904: }
7905: if ( in0 ) NEXT(in) = 0;
7906:
7907: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7908: else ndvf = ptondv(CO,vv,(P)f);
7909: if ( m ) ndv_mod(m,ndvf);
7910: ndf = (pointer)ndvtond(m,ndvf);
7911:
7912: /* dont sort, dont removecont */
1.24 noro 7913: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7914: nd_scale=2;
1.6 noro 7915: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7916: if ( !stat )
7917: error("nd_nf_p : exponent too large");
7918: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7919: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7920: }
7921:
7922: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7923: {
7924: NM m;
7925: UINT *t,*s;
7926: int i;
7927:
7928: for ( i = 0; i < n; i++ ) r[i] = 0;
7929: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7930: t = DL(m);
7931: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7932: r[i] = CM(m);
7933: }
7934: for ( i = 0; !r[i]; i++ );
7935: return i;
7936: }
7937:
7938: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7939: {
7940: NM m;
7941: UINT *t,*s;
7942: int i;
7943:
7944: for ( i = 0; i < n; i++ ) r[i] = 0;
7945: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7946: t = DL(m);
7947: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7948: r[i] = CZ(m);
1.1 noro 7949: }
7950: for ( i = 0; !r[i]; i++ );
7951: return i;
7952: }
7953:
7954: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7955: {
7956: NM m;
7957: UINT *t,*s;
7958: int i;
7959:
7960: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7961: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7962: t = DL(m);
7963: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7964: mpz_set(r[i],BDY(CZ(m)));
7965: }
7966: for ( i = 0; !mpz_sgn(r[i]); i++ );
7967: return i;
7968: }
7969:
7970: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7971: {
7972: NM m;
7973: unsigned long *v;
7974: int i,j,h,size;
7975: UINT *s,*t;
7976:
7977: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7978: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7979: bzero(v,size);
7980: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7981: t = DL(m);
7982: h = ndl_hash_value(t);
7983: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7984: v[i/BLEN] |= 1L <<(i%BLEN);
7985: }
7986: return v;
7987: }
7988:
7989: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7990: {
7991: NMV mr;
7992: UINT *d,*t,*s;
7993: int i,j,len,h,head;
7994:
7995: d = DL(m);
7996: len = LEN(p);
7997: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7998: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7999: ndl_add(d,DL(mr),t);
8000: h = ndl_hash_value(t);
8001: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
8002: if ( j == 0 ) head = i;
8003: v[i/BLEN] |= 1L <<(i%BLEN);
8004: }
8005: return head;
8006: }
8007:
8008: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
8009: {
8010: NM m;
8011: NMV mr;
8012: UINT *d,*t,*s;
8013: NDV p;
8014: int i,j,len;
8015: Z *r;
8016:
8017: m = pair->mul;
8018: d = DL(m);
8019: p = nd_ps[pair->index];
8020: len = LEN(p);
8021: r = (Z *)CALLOC(n,sizeof(Q));
8022: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
8023: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
8024: ndl_add(d,DL(mr),t);
8025: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 8026: r[i] = CZ(mr);
1.1 noro 8027: }
8028: return r;
8029: }
8030:
1.11 noro 8031: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 8032: {
8033: NM m;
8034: NMV mr;
1.11 noro 8035: UINT *d,*t,*s,*u;
1.1 noro 8036: NDV p;
8037: unsigned char *ivc;
8038: unsigned short *ivs;
8039: UINT *v,*ivi,*s0v;
1.11 noro 8040: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 8041: IndArray r;
8042:
8043: m = pair->mul;
8044: d = DL(m);
8045: if ( trace )
8046: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
8047: else
8048: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
8049:
8050: len = LEN(p);
8051: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
8052: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 8053: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
8054: ndl_add(d,DL(mr),t);
8055: st = prev;
8056: ed = n;
8057: while ( ed > st ) {
8058: md = (st+ed)/2;
8059: u = s0+md*nd_wpd;
8060: c = DL_COMPARE(u,t);
8061: if ( c == 0 ) break;
8062: else if ( c > 0 ) st = md;
8063: else ed = md;
8064: }
8065: prev = v[j] = md;
1.1 noro 8066: }
8067: r = (IndArray)MALLOC(sizeof(struct oIndArray));
8068: r->head = v[0];
8069: diff = 0;
8070: for ( i = 1; i < len; i++ ) {
8071: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
8072: }
8073: if ( diff < 256 ) {
8074: r->width = 1;
8075: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
8076: r->index.c = ivc;
8077: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
8078: } else if ( diff < 65536 ) {
8079: r->width = 2;
8080: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
8081: r->index.s = ivs;
8082: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
8083: } else {
8084: r->width = 4;
8085: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
8086: r->index.i = ivi;
8087: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
8088: }
8089: return r;
8090: }
8091:
8092: int compress_array(Z *svect,Z *cvect,int n)
8093: {
8094: int i,j;
8095:
8096: for ( i = j = 0; i < n; i++ )
8097: if ( svect[i] ) cvect[j++] = svect[i];
8098: return j;
8099: }
8100:
8101: void expand_array(Z *svect,Z *cvect,int n)
8102: {
8103: int i,j;
8104:
8105: for ( i = j = 0; j < n; i++ )
8106: if ( svect[i] ) svect[i] = cvect[j++];
8107: }
8108:
1.8 noro 8109: #if 0
1.1 noro 8110: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8111: {
8112: int i,j,k,len,pos,prev,nz;
8113: Z cs,mcs,c1,c2,cr,gcd,t;
8114: IndArray ivect;
8115: unsigned char *ivc;
8116: unsigned short *ivs;
8117: unsigned int *ivi;
8118: NDV redv;
8119: NMV mr;
8120: NODE rp;
8121: int maxrs;
8122: double hmag;
8123: Z *cvect;
1.3 noro 8124: int l;
1.1 noro 8125:
8126: maxrs = 0;
8127: for ( i = 0; i < col && !svect[i]; i++ );
8128: if ( i == col ) return maxrs;
8129: hmag = p_mag((P)svect[i])*nd_scale;
8130: cvect = (Z *)MALLOC(col*sizeof(Q));
8131: for ( i = 0; i < nred; i++ ) {
8132: ivect = imat[i];
8133: k = ivect->head;
8134: if ( svect[k] ) {
8135: maxrs = MAX(maxrs,rp0[i]->sugar);
8136: redv = nd_demand?ndv_load(rp0[i]->index)
8137: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
8138: len = LEN(redv); mr = BDY(redv);
1.6 noro 8139: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 8140: chsgnz(cs,&mcs);
8141: if ( !UNIQ(cr) ) {
8142: for ( j = 0; j < col; j++ ) {
8143: mulz(svect[j],cr,&c1); svect[j] = c1;
8144: }
8145: }
8146: svect[k] = 0; prev = k;
8147: switch ( ivect->width ) {
8148: case 1:
8149: ivc = ivect->index.c;
8150: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8151: pos = prev+ivc[j]; prev = pos;
1.6 noro 8152: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8153: }
8154: break;
8155: case 2:
8156: ivs = ivect->index.s;
8157: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8158: pos = prev+ivs[j]; prev = pos;
1.6 noro 8159: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8160: }
8161: break;
8162: case 4:
8163: ivi = ivect->index.i;
8164: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8165: pos = prev+ivi[j]; prev = pos;
1.6 noro 8166: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8167: }
8168: break;
8169: }
8170: for ( j = k+1; j < col && !svect[j]; j++ );
8171: if ( j == col ) break;
8172: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
8173: nz = compress_array(svect,cvect,col);
8174: removecont_array((P *)cvect,nz,1);
8175: expand_array(svect,cvect,nz);
8176: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
8177: }
8178: }
8179: }
8180: nz = compress_array(svect,cvect,col);
8181: removecont_array((P *)cvect,nz,1);
8182: expand_array(svect,cvect,nz);
8183: if ( DP_Print ) {
8184: fprintf(asir_out,"-"); fflush(asir_out);
8185: }
8186: return maxrs;
8187: }
1.4 noro 8188: #else
1.9 noro 8189:
1.4 noro 8190: /* direct mpz version */
8191: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8192: {
8193: int i,j,k,len,pos,prev;
8194: mpz_t cs,cr,gcd;
8195: IndArray ivect;
8196: unsigned char *ivc;
8197: unsigned short *ivs;
8198: unsigned int *ivi;
8199: NDV redv;
8200: NMV mr;
8201: NODE rp;
8202: int maxrs;
8203: double hmag;
8204: int l;
1.13 noro 8205: static mpz_t *svect;
8206: static int svect_len=0;
1.4 noro 8207:
8208: maxrs = 0;
8209: for ( i = 0; i < col && !svect0[i]; i++ );
8210: if ( i == col ) return maxrs;
8211: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 8212: if ( col > svect_len ) {
8213: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8214: svect_len = col;
8215: }
1.4 noro 8216: for ( i = 0; i < col; i++ ) {
8217: mpz_init(svect[i]);
8218: if ( svect0[i] )
8219: mpz_set(svect[i],BDY(svect0[i]));
8220: else
8221: mpz_set_ui(svect[i],0);
8222: }
8223: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
8224: for ( i = 0; i < nred; i++ ) {
8225: ivect = imat[i];
8226: k = ivect->head;
8227: if ( mpz_sgn(svect[k]) ) {
8228: maxrs = MAX(maxrs,rp0[i]->sugar);
8229: redv = nd_demand?ndv_load(rp0[i]->index)
8230: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
8231: len = LEN(redv); mr = BDY(redv);
1.6 noro 8232: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 8233: mpz_div(cs,svect[k],gcd);
1.6 noro 8234: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 8235: mpz_neg(cs,cs);
1.9 noro 8236: if ( MUNIMPZ(cr) )
8237: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
8238: else if ( !UNIMPZ(cr) )
8239: for ( j = 0; j < col; j++ ) {
8240: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
8241: }
1.4 noro 8242: mpz_set_ui(svect[k],0);
8243: prev = k;
8244: switch ( ivect->width ) {
8245: case 1:
8246: ivc = ivect->index.c;
8247: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8248: pos = prev+ivc[j]; prev = pos;
1.6 noro 8249: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8250: }
8251: break;
8252: case 2:
8253: ivs = ivect->index.s;
8254: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8255: pos = prev+ivs[j]; prev = pos;
1.6 noro 8256: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8257: }
8258: break;
8259: case 4:
8260: ivi = ivect->index.i;
8261: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8262: pos = prev+ivi[j]; prev = pos;
1.6 noro 8263: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8264: }
8265: break;
8266: }
8267: for ( j = k+1; j < col && !svect[j]; j++ );
8268: if ( j == col ) break;
8269: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
8270: mpz_removecont_array(svect,col);
8271: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
8272: }
8273: }
8274: }
8275: mpz_removecont_array(svect,col);
8276: if ( DP_Print ) {
8277: fprintf(asir_out,"-"); fflush(asir_out);
8278: }
8279: for ( i = 0; i < col; i++ )
8280: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
8281: else svect0[i] = 0;
8282: return maxrs;
8283: }
8284: #endif
1.1 noro 8285:
1.40 noro 8286: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 8287: {
8288: int i,j,k,len,pos,prev;
8289: UINT c,c1,c2,c3,up,lo,dmy;
8290: IndArray ivect;
8291: unsigned char *ivc;
8292: unsigned short *ivs;
8293: unsigned int *ivi;
8294: NDV redv;
8295: NMV mr;
8296: NODE rp;
8297: int maxrs;
8298:
8299: maxrs = 0;
8300: for ( i = 0; i < nred; i++ ) {
8301: ivect = imat[i];
8302: k = ivect->head; svect[k] %= m;
1.40 noro 8303: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 8304: maxrs = MAX(maxrs,rp0[i]->sugar);
8305: c = m-c; redv = nd_ps[rp0[i]->index];
8306: len = LEN(redv); mr = BDY(redv);
8307: svect[k] = 0; prev = k;
8308: switch ( ivect->width ) {
8309: case 1:
8310: ivc = ivect->index.c;
8311: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8312: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 8313: if ( c1 ) {
8314: c2 = svect[pos];
1.1 noro 8315: DMA(c1,c,c2,up,lo);
8316: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8317: } else svect[pos] = lo;
1.40 noro 8318: }
1.1 noro 8319: }
8320: break;
8321: case 2:
8322: ivs = ivect->index.s;
8323: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8324: pos = prev+ivs[j]; c1 = CM(mr);
8325: prev = pos;
1.40 noro 8326: if ( c1 ) {
8327: c2 = svect[pos];
1.1 noro 8328: DMA(c1,c,c2,up,lo);
8329: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8330: } else svect[pos] = lo;
1.40 noro 8331: }
1.1 noro 8332: }
8333: break;
8334: case 4:
8335: ivi = ivect->index.i;
8336: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8337: pos = prev+ivi[j]; c1 = CM(mr);
8338: prev = pos;
1.40 noro 8339: if ( c1 ) {
8340: c2 = svect[pos];
1.1 noro 8341: DMA(c1,c,c2,up,lo);
8342: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8343: } else svect[pos] = lo;
1.40 noro 8344: }
1.1 noro 8345: }
8346: break;
8347: }
8348: }
8349: }
8350: for ( i = 0; i < col; i++ )
8351: if ( svect[i] >= (UINT)m ) svect[i] %= m;
8352: return maxrs;
8353: }
8354:
8355: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8356: {
8357: int i,j,k,len,pos,prev;
8358: UINT c,c1,c2,c3,up,lo,dmy;
8359: IndArray ivect;
8360: unsigned char *ivc;
8361: unsigned short *ivs;
8362: unsigned int *ivi;
8363: NDV redv;
8364: NMV mr;
8365: NODE rp;
8366: int maxrs;
8367:
8368: maxrs = 0;
8369: for ( i = 0; i < nred; i++ ) {
8370: ivect = imat[i];
8371: k = ivect->head;
8372: if ( (c = svect[k]) != 0 ) {
8373: maxrs = MAX(maxrs,rp0[i]->sugar);
8374: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
8375: len = LEN(redv); mr = BDY(redv);
8376: svect[k] = 0; prev = k;
8377: switch ( ivect->width ) {
8378: case 1:
8379: ivc = ivect->index.c;
8380: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8381: pos = prev+ivc[j]; prev = pos;
8382: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8383: }
8384: break;
8385: case 2:
8386: ivs = ivect->index.s;
8387: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8388: pos = prev+ivs[j]; prev = pos;
8389: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8390: }
8391: break;
8392: case 4:
8393: ivi = ivect->index.i;
8394: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8395: pos = prev+ivi[j]; prev = pos;
8396: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8397: }
8398: break;
8399: }
8400: }
8401: }
8402: return maxrs;
8403: }
8404:
8405: ND nd_add_lf(ND p1,ND p2)
8406: {
8407: int n,c,can;
8408: ND r;
8409: NM m1,m2,mr0,mr,s;
8410: Z t;
8411:
8412: if ( !p1 ) return p2;
8413: else if ( !p2 ) return p1;
8414: else {
8415: can = 0;
8416: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
8417: c = DL_COMPARE(DL(m1),DL(m2));
8418: switch ( c ) {
8419: case 0:
8420: addlf(CZ(m1),CZ(m2),&t);
8421: s = m1; m1 = NEXT(m1);
8422: if ( t ) {
8423: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
8424: } else {
8425: can += 2; FREENM(s);
8426: }
8427: s = m2; m2 = NEXT(m2); FREENM(s);
8428: break;
8429: case 1:
8430: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
8431: break;
8432: case -1:
8433: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
8434: break;
8435: }
8436: }
8437: if ( !mr0 )
8438: if ( m1 ) mr0 = m1;
8439: else if ( m2 ) mr0 = m2;
8440: else return 0;
8441: else if ( m1 ) NEXT(mr) = m1;
8442: else if ( m2 ) NEXT(mr) = m2;
8443: else NEXT(mr) = 0;
8444: BDY(p1) = mr0;
8445: SG(p1) = MAX(SG(p1),SG(p2));
8446: LEN(p1) = LEN(p1)+LEN(p2)-can;
8447: FREEND(p2);
8448: return p1;
8449: }
8450: }
8451:
8452: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8453: {
8454: int i,j,k,len,pos,prev;
8455: mpz_t c,mc,c1;
8456: IndArray ivect;
8457: unsigned char *ivc;
8458: unsigned short *ivs;
8459: unsigned int *ivi;
8460: NDV redv;
8461: NMV mr;
8462: NODE rp;
8463: int maxrs;
8464:
8465: maxrs = 0;
8466: lf_lazy = 1;
8467: for ( i = 0; i < nred; i++ ) {
8468: ivect = imat[i];
8469: k = ivect->head;
8470: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
8471: if ( mpz_sgn(svect[k]) ) {
8472: maxrs = MAX(maxrs,rp0[i]->sugar);
8473: mpz_neg(svect[k],svect[k]);
8474: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
8475: len = LEN(redv); mr = BDY(redv);
8476: prev = k;
8477: switch ( ivect->width ) {
8478: case 1:
8479: ivc = ivect->index.c;
8480: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8481: pos = prev+ivc[j]; prev = pos;
8482: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8483: }
8484: break;
8485: case 2:
8486: ivs = ivect->index.s;
8487: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8488: pos = prev+ivs[j]; prev = pos;
8489: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8490: }
8491: break;
8492: case 4:
8493: ivi = ivect->index.i;
8494: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8495: pos = prev+ivi[j]; prev = pos;
8496: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8497: }
8498: break;
8499: }
8500: mpz_set_ui(svect[k],0);
8501: }
8502: }
8503: lf_lazy=0;
8504: for ( i = 0; i < col; i++ ) {
8505: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
8506: }
8507: return maxrs;
8508: }
8509:
8510: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
8511: {
8512: int i,j,k,l,rank,s;
8513: mpz_t a,a1,inv;
8514: mpz_t *t,*pivot,*pk;
8515: mpz_t **mat;
8516: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
8517: int size,size1;
8518:
8519: mpz_init(inv);
8520: mpz_init(a);
8521: mat = (mpz_t **)mat0;
8522: size = 0;
8523: for ( rank = 0, j = 0; j < col; j++ ) {
8524: for ( i = rank; i < row; i++ ) {
8525: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
8526: }
8527: for ( i = rank; i < row; i++ )
8528: if ( mpz_sgn(mat[i][j]) )
8529: break;
8530: if ( i == row ) {
8531: colstat[j] = 0;
8532: continue;
8533: } else
8534: colstat[j] = 1;
8535: if ( i != rank ) {
8536: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8537: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8538: }
8539: pivot = mat[rank];
8540: s = sugar[rank];
8541: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
8542: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8543: if ( mpz_sgn(*pk) ) {
8544: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
8545: }
8546: for ( i = rank+1; i < row; i++ ) {
8547: t = mat[i];
8548: if ( mpz_sgn(t[j]) ) {
8549: sugar[i] = MAX(sugar[i],s);
8550: mpz_neg(a,t[j]);
8551: red_by_vect_lf(t+j,pivot+j,a,col-j);
8552: }
8553: }
8554: rank++;
8555: }
8556: for ( j = col-1, l = rank-1; j >= 0; j-- )
8557: if ( colstat[j] ) {
8558: pivot = mat[l];
8559: s = sugar[l];
8560: for ( k = j; k < col; k++ )
8561: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
8562: for ( i = 0; i < l; i++ ) {
8563: t = mat[i];
8564: if ( mpz_sgn(t[j]) ) {
8565: sugar[i] = MAX(sugar[i],s);
8566: mpz_neg(a,t[j]);
8567: red_by_vect_lf(t+j,pivot+j,a,col-j);
8568: }
8569: }
8570: l--;
8571: }
8572: for ( j = 0, l = 0; l < rank; j++ )
8573: if ( colstat[j] ) {
8574: t = mat[l];
8575: for ( k = j; k < col; k++ ) {
8576: mpz_mod(t[k],t[k],BDY(current_mod_lf));
8577: }
8578: l++;
8579: }
8580: return rank;
8581: }
8582:
8583:
8584: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
8585: {
8586: int j,k,len;
8587: UINT *p;
8588: UINT c;
8589: NDV r;
8590: NMV mr0,mr;
8591:
8592: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8593: if ( !len ) return 0;
8594: else {
8595: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8596: #if 0
8597: ndv_alloc += nmv_adv*len;
8598: #endif
8599: mr = mr0;
8600: p = s0vect;
8601: for ( j = k = 0; j < col; j++, p += nd_wpd )
8602: if ( !rhead[j] ) {
8603: if ( (c = vect[k++]) != 0 ) {
8604: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8605: }
8606: }
8607: MKNDV(nd_nvar,mr0,len,r);
8608: return r;
8609: }
8610: }
8611:
1.40 noro 8612: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
8613: {
8614: int j,k,len;
8615: UINT *p;
8616: UINT c;
8617: NDV r;
8618: NMV mr0,mr;
8619:
8620: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8621: if ( !len ) return 0;
8622: else {
8623: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8624: mr = mr0;
8625: p = s0vect;
8626: for ( j = k = 0; j < col; j++, p += nd_wpd )
8627: if ( (c = vect[k++]) != 0 ) {
8628: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8629: }
8630: MKNDV(nd_nvar,mr0,len,r);
8631: return r;
8632: }
8633: }
8634:
1.1 noro 8635: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
8636: {
8637: int j,k,len;
8638: UINT *p;
8639: NDV r;
8640: NMV mr0,mr;
8641:
8642: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
8643: if ( !len ) return 0;
8644: else {
8645: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8646: mr = mr0;
8647: p = s0vect;
8648: for ( j = 0; j < col; j++, p += nd_wpd )
8649: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
8650: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
8651: }
8652: MKNDV(nd_nvar,mr0,len,r);
8653: return r;
8654: }
8655: }
8656:
8657: /* for preprocessed vector */
8658:
8659: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
8660: {
1.6 noro 8661: int j,k,len;
8662: UINT *p;
8663: Z c;
8664: NDV r;
8665: NMV mr0,mr;
1.1 noro 8666:
1.6 noro 8667: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8668: if ( !len ) return 0;
8669: else {
8670: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 8671: #if 0
1.6 noro 8672: ndv_alloc += nmv_adv*len;
1.1 noro 8673: #endif
1.6 noro 8674: mr = mr0;
8675: p = s0vect;
8676: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
8677: if ( !rhead[j] ) {
8678: if ( (c = vect[k++]) != 0 ) {
8679: if ( !INT(c) )
8680: error("vect_to_ndv_q : components must be integers");
8681: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
8682: }
8683: }
1.1 noro 8684: }
1.6 noro 8685: MKNDV(nd_nvar,mr0,len,r);
8686: return r;
8687: }
1.1 noro 8688: }
8689:
8690: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8691: {
8692: int j,k,len;
8693: UINT *p;
8694: mpz_t c;
8695: NDV r;
8696: NMV mr0,mr;
8697:
8698: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8699: if ( !len ) return 0;
8700: else {
8701: mr0 = (NMV)MALLOC(nmv_adv*len);
8702: #if 0
8703: ndv_alloc += nmv_adv*len;
8704: #endif
8705: mr = mr0;
8706: p = s0vect;
8707: for ( j = k = 0; j < col; j++, p += nd_wpd )
8708: if ( !rhead[j] ) {
8709: c[0] = vect[k++][0];
8710: if ( mpz_sgn(c) ) {
8711: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8712: }
8713: }
8714: MKNDV(nd_nvar,mr0,len,r);
8715: return r;
8716: }
8717: }
8718:
8719: /* for plain vector */
8720:
8721: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8722: {
8723: int j,k,len;
8724: UINT *p;
8725: Z c;
8726: NDV r;
8727: NMV mr0,mr;
8728:
8729: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8730: if ( !len ) return 0;
8731: else {
8732: mr0 = (NMV)MALLOC(nmv_adv*len);
8733: #if 0
8734: ndv_alloc += nmv_adv*len;
8735: #endif
8736: mr = mr0;
8737: p = s0vect;
8738: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8739: if ( (c = vect[k]) != 0 ) {
8740: if ( !INT(c) )
1.6 noro 8741: error("plain_vect_to_ndv_q : components must be integers");
8742: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8743: }
8744: MKNDV(nd_nvar,mr0,len,r);
8745: return r;
8746: }
8747: }
8748:
8749: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8750: {
8751: ND_pairs t;
8752: NODE sp0,sp;
8753: int stat;
8754: ND spol;
8755:
8756: for ( t = l; t; t = NEXT(t) ) {
8757: stat = nd_sp(m,trace,t,&spol);
8758: if ( !stat ) return 0;
8759: if ( spol ) {
8760: add_pbucket_symbolic(bucket,spol);
8761: }
8762: }
8763: return 1;
8764: }
8765:
8766: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8767: {
8768: NODE rp0,rp;
8769: NM mul,head,s0,s;
8770: int index,col,i,sugar;
8771: RHist h;
8772: UINT *s0v,*p;
8773: NM_ind_pair pair;
8774: ND red;
8775: NDV *ps;
8776:
8777: s0 = 0; rp0 = 0; col = 0;
8778: if ( nd_demand )
8779: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8780: else
8781: ps = trace?nd_ps_trace:nd_ps;
8782: while ( 1 ) {
8783: head = remove_head_pbucket_symbolic(bucket);
8784: if ( !head ) break;
8785: if ( !s0 ) s0 = head;
8786: else NEXT(s) = head;
8787: s = head;
8788: index = ndl_find_reducer(DL(head));
8789: if ( index >= 0 ) {
8790: h = nd_psh[index];
8791: NEWNM(mul);
8792: ndl_sub(DL(head),DL(h),DL(mul));
8793: if ( ndl_check_bound2(index,DL(mul)) )
8794: return 0;
8795: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8796: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8797: red = ndv_mul_nm_symbolic(mul,ps[index]);
8798: add_pbucket_symbolic(bucket,nd_remove_head(red));
8799: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8800: }
8801: col++;
8802: }
8803: if ( rp0 ) NEXT(rp) = 0;
8804: NEXT(s) = 0;
8805: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8806: for ( i = 0, p = s0v, s = s0; i < col;
8807: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8808: *s0vect = s0v;
8809: *r = rp0;
8810: return col;
8811: }
8812:
8813: void print_ndp(ND_pairs l)
8814: {
8815: ND_pairs t;
8816:
8817: for ( t = l; t; t = NEXT(t) )
8818: printf("[%d,%d] ",t->i1,t->i2);
8819: printf("\n");
8820: }
8821:
8822: NODE nd_f4(int m,int checkonly,int **indp)
8823: {
8824: int i,nh,stat,index,f4red;
8825: NODE r,g,tn0,tn,node;
8826: ND_pairs d,l,t,ll0,ll,lh;
8827: LIST l0,l1;
8828: ND spol,red;
8829: NDV nf,redv;
8830: NM s0,s;
8831: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8832: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8833: UINT c;
8834: UINT **spmat;
8835: UINT *s0vect,*svect,*p,*v;
8836: int *colstat;
8837: IndArray *imat;
8838: int *rhead;
8839: int spcol,sprow;
8840: int sugar,sugarh;
8841: PGeoBucket bucket;
8842: struct oEGT eg0,eg1,eg_f4;
8843: Z i1,i2,sugarq;
1.12 noro 8844:
8845: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8846: #if 0
8847: ndv_alloc = 0;
8848: #endif
1.11 noro 8849: Nf4_red=0;
1.1 noro 8850: g = 0; d = 0;
8851: for ( i = 0; i < nd_psn; i++ ) {
8852: d = update_pairs(d,g,i,0);
8853: g = update_base(g,i);
8854: }
8855: nzlist = 0;
8856: nzlist_t = nd_nzlist;
8857: f4red = 1;
8858: nd_last_nonzero = 0;
8859: while ( d ) {
8860: get_eg(&eg0);
8861: l = nd_minsugarp(d,&d);
8862: sugar = nd_sugarweight?l->sugar2:SG(l);
8863: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8864: if ( nzlist_t ) {
8865: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8866: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8867: tn = BDY((LIST)ARG1(node));
8868: if ( !tn ) {
8869: nzlist_t = NEXT(nzlist_t);
8870: continue;
8871: }
8872: /* tn = [[i1,i2],...] */
8873: lh = nd_ipairtospair(tn);
8874: }
8875: bucket = create_pbucket();
8876: stat = nd_sp_f4(m,0,l,bucket);
8877: if ( !stat ) {
8878: for ( t = l; NEXT(t); t = NEXT(t) );
8879: NEXT(t) = d; d = l;
8880: d = nd_reconstruct(0,d);
8881: continue;
8882: }
8883: if ( bucket->m < 0 ) continue;
8884: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8885: if ( !col ) {
8886: for ( t = l; NEXT(t); t = NEXT(t) );
8887: NEXT(t) = d; d = l;
8888: d = nd_reconstruct(0,d);
8889: continue;
8890: }
1.12 noro 8891: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8892: if ( DP_Print )
1.6 noro 8893: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8894: sugar,eg_f4.exectime);
1.1 noro 8895: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8896: if ( checkonly && nflist ) return 0;
8897: /* adding new bases */
8898: if ( nflist ) nd_last_nonzero = f4red;
8899: for ( r = nflist; r; r = NEXT(r) ) {
8900: nf = (NDV)BDY(r);
1.38 noro 8901: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8902: ndv_removecont(m,nf);
8903: if ( !m && nd_nalg ) {
8904: ND nf1;
8905:
8906: nf1 = ndvtond(m,nf);
8907: nd_monic(0,&nf1);
8908: nd_removecont(m,nf1);
8909: nf = ndtondv(m,nf1);
8910: }
1.24 noro 8911: nh = ndv_newps(m,nf,0);
1.1 noro 8912: d = update_pairs(d,g,nh,0);
8913: g = update_base(g,nh);
8914: }
8915: if ( DP_Print ) {
8916: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8917: }
8918: if ( nd_gentrace ) {
8919: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8920: NEXTNODE(tn0,tn);
1.6 noro 8921: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8922: node = mknode(2,i1,i2); MKLIST(l0,node);
8923: BDY(tn) = l0;
8924: }
8925: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8926: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8927: MKNODE(node,l1,nzlist); nzlist = node;
8928: }
8929: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8930: f4red++;
8931: if ( nd_f4red && f4red > nd_f4red ) break;
8932: if ( nd_rank0 && !nflist ) break;
8933: }
8934: if ( nd_gentrace ) {
8935: MKLIST(l0,reverse_node(nzlist));
8936: MKNODE(nd_alltracelist,l0,0);
8937: }
8938: #if 0
8939: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8940: #endif
1.12 noro 8941: if ( DP_Print ) {
8942: fprintf(asir_out,"number of red=%d,",Nf4_red);
8943: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8944: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8945: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8946: }
1.1 noro 8947: conv_ilist(nd_demand,0,g,indp);
8948: return g;
8949: }
8950:
8951: NODE nd_f4_trace(int m,int **indp)
8952: {
8953: int i,nh,stat,index;
8954: NODE r,g;
8955: ND_pairs d,l,l0,t;
8956: ND spol,red;
8957: NDV nf,redv,nfqv,nfv;
8958: NM s0,s;
8959: NODE rp0,srp0,nflist;
8960: int nsp,nred,col,rank,len,k,j,a;
8961: UINT c;
8962: UINT **spmat;
8963: UINT *s0vect,*svect,*p,*v;
8964: int *colstat;
8965: IndArray *imat;
8966: int *rhead;
8967: int spcol,sprow;
8968: int sugar;
8969: PGeoBucket bucket;
8970: struct oEGT eg0,eg1,eg_f4;
8971:
8972: g = 0; d = 0;
8973: for ( i = 0; i < nd_psn; i++ ) {
8974: d = update_pairs(d,g,i,0);
8975: g = update_base(g,i);
8976: }
8977: while ( d ) {
8978: get_eg(&eg0);
8979: l = nd_minsugarp(d,&d);
8980: sugar = SG(l);
8981: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8982: bucket = create_pbucket();
8983: stat = nd_sp_f4(m,0,l,bucket);
8984: if ( !stat ) {
8985: for ( t = l; NEXT(t); t = NEXT(t) );
8986: NEXT(t) = d; d = l;
8987: d = nd_reconstruct(1,d);
8988: continue;
8989: }
8990: if ( bucket->m < 0 ) continue;
8991: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8992: if ( !col ) {
8993: for ( t = l; NEXT(t); t = NEXT(t) );
8994: NEXT(t) = d; d = l;
8995: d = nd_reconstruct(1,d);
8996: continue;
8997: }
8998: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8999: if ( DP_Print )
1.3 noro 9000: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 9001: sugar,eg_f4.exectime);
1.1 noro 9002: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9003: if ( !l0 ) continue;
9004: l = l0;
9005:
9006: /* over Q */
9007: bucket = create_pbucket();
9008: stat = nd_sp_f4(0,1,l,bucket);
9009: if ( !stat ) {
9010: for ( t = l; NEXT(t); t = NEXT(t) );
9011: NEXT(t) = d; d = l;
9012: d = nd_reconstruct(1,d);
9013: continue;
9014: }
9015: if ( bucket->m < 0 ) continue;
9016: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9017: if ( !col ) {
9018: for ( t = l; NEXT(t); t = NEXT(t) );
9019: NEXT(t) = d; d = l;
9020: d = nd_reconstruct(1,d);
9021: continue;
9022: }
9023: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
9024: /* adding new bases */
9025: for ( r = nflist; r; r = NEXT(r) ) {
9026: nfqv = (NDV)BDY(r);
9027: ndv_removecont(0,nfqv);
1.6 noro 9028: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 9029: if ( nd_nalg ) {
9030: ND nf1;
9031:
9032: nf1 = ndvtond(m,nfqv);
9033: nd_monic(0,&nf1);
9034: nd_removecont(0,nf1);
9035: nfqv = ndtondv(0,nf1); nd_free(nf1);
9036: }
9037: nfv = ndv_dup(0,nfqv);
9038: ndv_mod(m,nfv);
9039: ndv_removecont(m,nfv);
1.24 noro 9040: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 9041: d = update_pairs(d,g,nh,0);
9042: g = update_base(g,nh);
9043: }
9044: }
9045: #if 0
9046: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
9047: #endif
9048: conv_ilist(nd_demand,1,g,indp);
9049: return g;
9050: }
9051:
9052: int rref(matrix mat,int *sugar)
9053: {
9054: int row,col,i,j,k,l,s,wcol,wj;
9055: unsigned long bj;
9056: unsigned long **a;
9057: unsigned long *ai,*ak,*as,*t;
9058: int *pivot;
9059:
9060: row = mat->row;
9061: col = mat->col;
9062: a = mat->a;
9063: wcol = (col+BLEN-1)/BLEN;
9064: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
9065: i = 0;
9066: for ( j = 0; j < col; j++ ) {
9067: wj = j/BLEN; bj = 1L<<(j%BLEN);
9068: for ( k = i; k < row; k++ )
9069: if ( a[k][wj] & bj ) break;
9070: if ( k == row ) continue;
9071: pivot[i] = j;
9072: if ( k != i ) {
9073: t = a[i]; a[i] = a[k]; a[k] = t;
9074: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
9075: }
9076: ai = a[i];
9077: for ( k = i+1; k < row; k++ ) {
9078: ak = a[k];
9079: if ( ak[wj] & bj ) {
9080: for ( l = wj; l < wcol; l++ )
9081: ak[l] ^= ai[l];
9082: sugar[k] = MAX(sugar[k],sugar[i]);
9083: }
9084: }
9085: i++;
9086: }
9087: for ( k = i-1; k >= 0; k-- ) {
9088: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
9089: ak = a[k];
9090: for ( s = 0; s < k; s++ ) {
9091: as = a[s];
9092: if ( as[wj] & bj ) {
9093: for ( l = wj; l < wcol; l++ )
9094: as[l] ^= ak[l];
9095: sugar[s] = MAX(sugar[s],sugar[k]);
9096: }
9097: }
9098: }
9099: return i;
9100: }
9101:
9102: void print_matrix(matrix mat)
9103: {
9104: int row,col,i,j;
9105: unsigned long *ai;
9106:
9107: row = mat->row;
9108: col = mat->col;
9109: printf("%d x %d\n",row,col);
9110: for ( i = 0; i < row; i++ ) {
9111: ai = mat->a[i];
9112: for ( j = 0; j < col; j++ ) {
9113: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
9114: else putchar('0');
9115: }
9116: putchar('\n');
9117: }
9118: }
9119:
9120: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
9121:
9122: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
9123: {
9124: int row,col,wcol,wj,i,j;
9125: unsigned long bj;
9126: unsigned long *ai;
9127: unsigned long **a;
9128: int len;
9129: int *pos;
9130:
9131: row = mat->row;
9132: col = mat->col;
9133: wcol = (col+BLEN-1)/BLEN;
9134: pos = (int *)MALLOC(wcol*sizeof(int));
9135: bzero(pos,wcol*sizeof(int));
9136: for ( i = j = 0; i < wcol; i++ )
9137: if ( v[i] ) pos[j++] = i;;
9138: len = j;
9139: wj = rhead/BLEN;
9140: bj = 1L<<rhead%BLEN;
9141: a = mat->a;
9142: for ( i = 0; i < row; i++ ) {
9143: ai = a[i];
9144: if ( ai[wj]&bj ) {
9145: for ( j = 0; j < len; j++ )
9146: ai[pos[j]] ^= v[pos[j]];
9147: sugar[i] = MAX(sugar[i],rsugar);
9148: }
9149: }
9150: }
9151:
9152: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
9153: {
9154: int nsp,nred,i,i0,k,rank,row;
9155: NODE r0,rp;
9156: ND_pairs sp;
9157: ND spol;
9158: NM_ind_pair rt;
9159: int *s0hash;
9160: UINT *s;
9161: int *pivot,*sugar,*head;
9162: matrix mat;
9163: NM m;
9164: NODE r;
9165: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
9166: int rhead,rsugar,size;
9167: unsigned long *v;
9168:
9169: get_eg(&eg0);
9170: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
9171: nred = length(rp0);
9172: mat = alloc_matrix(nsp,col);
9173: s0hash = (int *)MALLOC(col*sizeof(int));
9174: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
9175: s0hash[i] = ndl_hash_value(s);
9176:
9177: sugar = (int *)MALLOC(nsp*sizeof(int));
9178: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
9179: nd_sp(2,0,sp,&spol);
9180: if ( spol ) {
9181: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
9182: sugar[i] = SG(spol);
9183: i++;
9184: }
9185: }
9186: mat->row = i;
9187: if ( DP_Print ) {
9188: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
9189: }
9190: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
9191: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
9192: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
9193: rt = (NM_ind_pair)BDY(rp);
9194: bzero(v,size);
9195: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
9196: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
9197: red_by_vect_2(mat,sugar,v,rhead,rsugar);
9198: }
9199:
9200: get_eg(&eg1);
9201: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
9202: rank = rref(mat,sugar);
9203:
9204: for ( i = 0, r0 = 0; i < rank; i++ ) {
9205: NEXTNODE(r0,r);
9206: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
9207: SG((NDV)BDY(r)) = sugar[i];
9208: }
9209: if ( r0 ) NEXT(r) = 0;
9210: get_eg(&eg2);
9211: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
9212: if ( DP_Print ) {
9213: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 9214: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 9215: fflush(asir_out);
9216: }
9217: return r0;
9218: }
9219:
9220:
9221: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
9222: {
9223: IndArray *imat;
1.11 noro 9224: int nsp,nred,i,start;
1.1 noro 9225: int *rhead;
9226: NODE r0,rp;
9227: ND_pairs sp;
9228: NM_ind_pair *rvect;
9229: UINT *s;
9230: int *s0hash;
1.11 noro 9231: struct oEGT eg0,eg1,eg_conv;
1.1 noro 9232:
9233: if ( m == 2 && nd_rref2 )
9234: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
9235:
9236: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
9237: nred = length(rp0);
9238: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
9239: rhead = (int *)MALLOC(col*sizeof(int));
9240: for ( i = 0; i < col; i++ ) rhead[i] = 0;
9241:
9242: /* construction of index arrays */
1.11 noro 9243: get_eg(&eg0);
1.1 noro 9244: if ( DP_Print ) {
1.11 noro 9245: fprintf(asir_out,"%dx%d,",nsp+nred,col);
9246: fflush(asir_out);
1.1 noro 9247: }
9248: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 9249: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 9250: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 9251: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 9252: rhead[imat[i]->head] = 1;
1.11 noro 9253: start = imat[i]->head;
9254: }
1.12 noro 9255: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 9256: if ( DP_Print ) {
9257: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
9258: fflush(asir_out);
1.1 noro 9259: }
9260: if ( m > 0 )
1.7 noro 9261: #if SIZEOF_LONG==8
1.1 noro 9262: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9263: #else
9264: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9265: #endif
9266: else if ( m == -1 )
9267: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9268: else if ( m == -2 )
9269: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9270: else
9271: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9272: return r0;
9273: }
9274:
9275: /* for Fp, 2<=p<2^16 */
9276:
9277: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9278: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9279: {
9280: int spcol,sprow,a;
9281: int i,j,k,l,rank;
9282: NODE r0,r;
9283: ND_pairs sp;
9284: ND spol;
9285: UINT **spmat;
9286: UINT *svect,*v;
9287: int *colstat;
9288: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9289: int maxrs;
9290: int *spsugar;
9291: ND_pairs *spactive;
9292:
9293: spcol = col-nred;
9294: get_eg(&eg0);
9295: /* elimination (1st step) */
9296: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9297: svect = (UINT *)MALLOC(col*sizeof(UINT));
9298: spsugar = (int *)MALLOC(nsp*sizeof(int));
9299: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9300: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9301: nd_sp(m,0,sp,&spol);
9302: if ( !spol ) continue;
9303: nd_to_vect(m,s0vect,col,spol,svect);
9304: if ( m == -1 )
9305: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9306: else
1.40 noro 9307: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 9308: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9309: if ( i < col ) {
9310: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9311: for ( j = k = 0; j < col; j++ )
9312: if ( !rhead[j] ) v[k++] = svect[j];
9313: spsugar[sprow] = MAX(maxrs,SG(spol));
9314: if ( nz )
9315: spactive[sprow] = sp;
9316: sprow++;
9317: }
9318: nd_free(spol);
9319: }
9320: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9321: if ( DP_Print ) {
1.5 noro 9322: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9323: fflush(asir_out);
9324: }
9325: /* free index arrays */
9326: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9327:
9328: /* elimination (2nd step) */
9329: colstat = (int *)MALLOC(spcol*sizeof(int));
9330: if ( m == -1 )
9331: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9332: else
9333: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9334: r0 = 0;
9335: for ( i = 0; i < rank; i++ ) {
9336: NEXTNODE(r0,r); BDY(r) =
9337: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9338: SG((NDV)BDY(r)) = spsugar[i];
9339: GCFREE(spmat[i]);
9340: }
9341: if ( r0 ) NEXT(r) = 0;
9342:
9343: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9344: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9345: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9346: if ( DP_Print ) {
1.5 noro 9347: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9348: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9349: nsp,nred,sprow,spcol,rank);
1.5 noro 9350: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9351: }
9352: if ( nz ) {
9353: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9354: if ( rank > 0 ) {
9355: NEXT(spactive[rank-1]) = 0;
9356: *nz = spactive[0];
9357: } else
9358: *nz = 0;
9359: }
9360: return r0;
9361: }
9362:
1.40 noro 9363: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9364: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
9365: {
9366: int spcol,sprow,a;
9367: int i,j,k,l,rank;
9368: NODE r0,r;
9369: ND_pairs sp;
9370: ND spol;
9371: UINT **spmat;
9372: UINT *svect,*cvect;
9373: UINT *v;
9374: int *colstat;
9375: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9376: int maxrs;
9377: int *spsugar;
9378: ND_pairs *spactive;
9379: SIG *spsig;
9380:
9381: get_eg(&eg0);
9382: /* elimination (1st step) */
9383: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9384: spsugar = (int *)MALLOC(nsp*sizeof(int));
9385: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
9386: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9387: nd_sp(m,0,sp,&spol);
9388: if ( !spol ) {
9389: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9390: continue;
9391: }
9392: svect = (UINT *)MALLOC(col*sizeof(UINT));
9393: nd_to_vect(m,s0vect,col,spol,svect);
9394: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
9395: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9396: if ( i < col ) {
9397: spmat[sprow] = svect;
9398: spsugar[sprow] = MAX(maxrs,SG(spol));
9399: spsig[sprow] = sp->sig;
9400: sprow++;
9401: } else {
9402: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9403: }
9404: nd_free(spol);
9405: }
9406: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
9407: if ( DP_Print ) {
9408: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9409: fflush(asir_out);
9410: }
9411: /* free index arrays */
9412: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9413:
9414: /* elimination (2nd step) */
9415: colstat = (int *)MALLOC(col*sizeof(int));
9416: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
9417: r0 = 0;
9418: for ( i = 0; i < sprow; i++ ) {
9419: if ( spsugar[i] >= 0 ) {
9420: NEXTNODE(r0,r);
9421: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
9422: SG((NDV)BDY(r)) = spsugar[i];
9423: ((NDV)BDY(r))->sig = spsig[i];
9424: } else
9425: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
9426: GCFREE(spmat[i]);
9427: }
9428: if ( r0 ) NEXT(r) = 0;
9429: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
9430: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9431: if ( DP_Print ) {
9432: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9433: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9434: nsp,nred,sprow,col,rank);
9435: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9436: }
9437: return r0;
9438: }
9439:
1.1 noro 9440:
9441: /* for small finite fields */
9442:
9443: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9444: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9445: {
9446: int spcol,sprow,a;
9447: int i,j,k,l,rank;
9448: NODE r0,r;
9449: ND_pairs sp;
9450: ND spol;
9451: UINT **spmat;
9452: UINT *svect,*v;
9453: int *colstat;
9454: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9455: int maxrs;
9456: int *spsugar;
9457: ND_pairs *spactive;
9458:
9459: spcol = col-nred;
9460: get_eg(&eg0);
9461: /* elimination (1st step) */
9462: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9463: svect = (UINT *)MALLOC(col*sizeof(UINT));
9464: spsugar = (int *)MALLOC(nsp*sizeof(int));
9465: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9466: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9467: nd_sp(m,0,sp,&spol);
9468: if ( !spol ) continue;
9469: nd_to_vect(m,s0vect,col,spol,svect);
9470: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9471: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9472: if ( i < col ) {
9473: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9474: for ( j = k = 0; j < col; j++ )
9475: if ( !rhead[j] ) v[k++] = svect[j];
9476: spsugar[sprow] = MAX(maxrs,SG(spol));
9477: if ( nz )
9478: spactive[sprow] = sp;
9479: sprow++;
9480: }
9481: nd_free(spol);
9482: }
9483: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9484: if ( DP_Print ) {
1.5 noro 9485: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9486: fflush(asir_out);
9487: }
9488: /* free index arrays */
9489: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9490:
9491: /* elimination (2nd step) */
9492: colstat = (int *)MALLOC(spcol*sizeof(int));
9493: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9494: r0 = 0;
9495: for ( i = 0; i < rank; i++ ) {
9496: NEXTNODE(r0,r); BDY(r) =
9497: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9498: SG((NDV)BDY(r)) = spsugar[i];
9499: GCFREE(spmat[i]);
9500: }
9501: if ( r0 ) NEXT(r) = 0;
9502:
9503: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9504: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9505: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9506: if ( DP_Print ) {
1.5 noro 9507: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9508: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9509: nsp,nred,sprow,spcol,rank);
1.5 noro 9510: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9511: }
9512: if ( nz ) {
9513: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9514: if ( rank > 0 ) {
9515: NEXT(spactive[rank-1]) = 0;
9516: *nz = spactive[0];
9517: } else
9518: *nz = 0;
9519: }
9520: return r0;
9521: }
9522:
9523: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9524: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9525: {
9526: int spcol,sprow,a;
9527: int i,j,k,l,rank;
9528: NODE r0,r;
9529: ND_pairs sp;
9530: ND spol;
9531: mpz_t **spmat;
9532: mpz_t *svect,*v;
9533: int *colstat;
9534: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9535: int maxrs;
9536: int *spsugar;
9537: pointer *w;
9538:
9539: spcol = col-nred;
9540: get_eg(&eg0);
9541: /* elimination (1st step) */
9542: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
9543: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
9544: spsugar = (int *)MALLOC(nsp*sizeof(int));
9545: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9546: nd_sp(m,trace,sp,&spol);
9547: if ( !spol ) continue;
9548: nd_to_vect_lf(s0vect,col,spol,svect);
9549: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
9550: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
9551: if ( i < col ) {
9552: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
9553: for ( j = k = 0; j < col; j++ )
9554: if ( !rhead[j] ) v[k++][0] = svect[j][0];
9555: spsugar[sprow] = MAX(maxrs,SG(spol));
9556: sprow++;
9557: }
9558: /* nd_free(spol); */
9559: }
9560: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9561: if ( DP_Print ) {
1.5 noro 9562: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9563: fflush(asir_out);
9564: }
9565: /* free index arrays */
9566: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9567:
9568: /* elimination (2nd step) */
9569: colstat = (int *)MALLOC(spcol*sizeof(int));
9570: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
9571: w = (pointer *)MALLOC(rank*sizeof(pointer));
9572: for ( i = 0; i < rank; i++ ) {
9573: #if 0
9574: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9575: SG((NDV)w[rank-i-1]) = spsugar[i];
9576: #else
9577: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9578: SG((NDV)w[i]) = spsugar[i];
9579: #endif
9580: /* GCFREE(spmat[i]); */
9581:
9582: }
9583: #if 0
9584: qsort(w,rank,sizeof(NDV),
9585: (int (*)(const void *,const void *))ndv_compare);
9586: #endif
9587: r0 = 0;
9588: for ( i = 0; i < rank; i++ ) {
9589: NEXTNODE(r0,r); BDY(r) = w[i];
9590: }
9591: if ( r0 ) NEXT(r) = 0;
9592:
9593: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9594: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9595: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9596: if ( DP_Print ) {
1.5 noro 9597: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9598: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9599: nsp,nred,sprow,spcol,rank);
1.5 noro 9600: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9601: }
9602: return r0;
9603: }
9604:
9605: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9606: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9607: {
9608: int spcol,sprow,a;
9609: int i,j,k,l,rank;
9610: NODE r0,r;
9611: ND_pairs sp;
9612: ND spol;
9613: Z **spmat;
9614: Z *svect,*v;
9615: int *colstat;
9616: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9617: int maxrs;
9618: int *spsugar;
9619: pointer *w;
9620:
9621: spcol = col-nred;
9622: get_eg(&eg0);
9623: /* elimination (1st step) */
9624: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
9625: svect = (Z *)MALLOC(col*sizeof(Q));
9626: spsugar = (int *)MALLOC(nsp*sizeof(int));
9627: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9628: nd_sp(0,trace,sp,&spol);
9629: if ( !spol ) continue;
9630: nd_to_vect_q(s0vect,col,spol,svect);
9631: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
9632: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9633: if ( i < col ) {
9634: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
9635: for ( j = k = 0; j < col; j++ )
9636: if ( !rhead[j] ) v[k++] = svect[j];
9637: spsugar[sprow] = MAX(maxrs,SG(spol));
9638: sprow++;
9639: }
9640: /* nd_free(spol); */
9641: }
9642: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9643: if ( DP_Print ) {
1.5 noro 9644: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9645: fflush(asir_out);
9646: }
9647: /* free index arrays */
9648: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9649:
9650: /* elimination (2nd step) */
9651: colstat = (int *)MALLOC(spcol*sizeof(int));
9652: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
9653: w = (pointer *)MALLOC(rank*sizeof(pointer));
9654: for ( i = 0; i < rank; i++ ) {
9655: #if 0
9656: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9657: SG((NDV)w[rank-i-1]) = spsugar[i];
9658: #else
9659: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9660: SG((NDV)w[i]) = spsugar[i];
9661: #endif
9662: /* GCFREE(spmat[i]); */
9663: }
9664: #if 0
9665: qsort(w,rank,sizeof(NDV),
9666: (int (*)(const void *,const void *))ndv_compare);
9667: #endif
9668: r0 = 0;
9669: for ( i = 0; i < rank; i++ ) {
9670: NEXTNODE(r0,r); BDY(r) = w[i];
9671: }
9672: if ( r0 ) NEXT(r) = 0;
9673:
9674: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9675: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9676: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9677: if ( DP_Print ) {
1.5 noro 9678: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9679: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9680: nsp,nred,sprow,spcol,rank);
1.5 noro 9681: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9682: }
9683: return r0;
9684: }
9685:
9686: FILE *nd_write,*nd_read;
9687:
9688: void nd_send_int(int a) {
9689: write_int(nd_write,(unsigned int *)&a);
9690: }
9691:
9692: void nd_send_intarray(int *p,int len) {
9693: write_intarray(nd_write,(unsigned int *)p,len);
9694: }
9695:
9696: int nd_recv_int() {
9697: int a;
9698:
9699: read_int(nd_read,(unsigned int *)&a);
9700: return a;
9701: }
9702:
9703: void nd_recv_intarray(int *p,int len) {
9704: read_intarray(nd_read,(unsigned int *)p,len);
9705: }
9706:
9707: void nd_send_ndv(NDV p) {
9708: int len,i;
9709: NMV m;
9710:
9711: if ( !p ) nd_send_int(0);
9712: else {
9713: len = LEN(p);
9714: nd_send_int(len);
9715: m = BDY(p);
9716: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9717: nd_send_int(CM(m));
9718: nd_send_intarray((int *)DL(m),nd_wpd);
9719: }
9720: }
9721: }
9722:
9723: void nd_send_nd(ND p) {
9724: int len,i;
9725: NM m;
9726:
9727: if ( !p ) nd_send_int(0);
9728: else {
9729: len = LEN(p);
9730: nd_send_int(len);
9731: m = BDY(p);
9732: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9733: nd_send_int(CM(m));
9734: nd_send_intarray((int *)DL(m),nd_wpd);
9735: }
9736: }
9737: }
9738:
9739: NDV nd_recv_ndv()
9740: {
9741: int len,i;
9742: NMV m,m0;
9743: NDV r;
9744:
9745: len = nd_recv_int();
9746: if ( !len ) return 0;
9747: else {
9748: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9749: #if 0
9750: ndv_alloc += len*nmv_adv;
9751: #endif
9752: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9753: CM(m) = nd_recv_int();
9754: nd_recv_intarray((int *)DL(m),nd_wpd);
9755: }
9756: MKNDV(nd_nvar,m0,len,r);
9757: return r;
9758: }
9759: }
9760:
9761: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9762: {
9763: int i,j,t,c,rank,inv;
9764: int *ci,*ri;
9765: Z dn;
9766: MAT m,nm;
9767:
9768: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9769: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9770: for ( i = 0; i < row; i++ )
9771: for ( j = 0; j < col; j++ )
9772: mat0[i][j] = 0;
9773: c = col-rank;
9774: for ( i = 0; i < rank; i++ ) {
9775: mat0[i][ri[i]] = dn;
9776: for ( j = 0; j < c; j++ )
9777: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9778: }
9779: return rank;
9780: }
9781:
9782: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9783: {
9784: int i,j,k,l,inv,a,rank,s;
9785: unsigned int *t,*pivot,*pk;
9786: unsigned int **mat;
9787: ND_pairs pair;
9788:
9789: mat = (unsigned int **)mat0;
9790: for ( rank = 0, j = 0; j < col; j++ ) {
9791: for ( i = rank; i < row; i++ )
9792: mat[i][j] %= md;
9793: for ( i = rank; i < row; i++ )
9794: if ( mat[i][j] )
9795: break;
9796: if ( i == row ) {
9797: colstat[j] = 0;
9798: continue;
9799: } else
9800: colstat[j] = 1;
9801: if ( i != rank ) {
9802: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9803: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9804: if ( spactive ) {
9805: pair = spactive[i]; spactive[i] = spactive[rank];
9806: spactive[rank] = pair;
9807: }
9808: }
9809: pivot = mat[rank];
9810: s = sugar[rank];
9811: inv = invm(pivot[j],md);
9812: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9813: if ( *pk ) {
9814: if ( *pk >= (unsigned int)md )
9815: *pk %= md;
9816: DMAR(*pk,inv,0,md,*pk)
9817: }
9818: for ( i = rank+1; i < row; i++ ) {
9819: t = mat[i];
9820: if ( (a = t[j]) != 0 ) {
9821: sugar[i] = MAX(sugar[i],s);
9822: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9823: }
9824: }
9825: rank++;
9826: }
9827: for ( j = col-1, l = rank-1; j >= 0; j-- )
9828: if ( colstat[j] ) {
9829: pivot = mat[l];
9830: s = sugar[l];
9831: for ( i = 0; i < l; i++ ) {
9832: t = mat[i];
9833: t[j] %= md;
9834: if ( (a = t[j]) != 0 ) {
9835: sugar[i] = MAX(sugar[i],s);
9836: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9837: }
9838: }
9839: l--;
9840: }
9841: for ( j = 0, l = 0; l < rank; j++ )
9842: if ( colstat[j] ) {
9843: t = mat[l];
9844: for ( k = j; k < col; k++ )
9845: if ( t[k] >= (unsigned int)md )
9846: t[k] %= md;
9847: l++;
9848: }
9849: return rank;
9850: }
9851:
1.40 noro 9852: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9853: {
9854: int i,j,k,l,rank,s,imin;
9855: UINT inv;
9856: UINT a;
9857: UINT *t,*pivot,*pk;
9858: UINT *ck;
9859: UINT *ct;
9860: ND_pairs pair;
9861: SIG sg;
9862: int *used;
9863:
9864: used = (int *)MALLOC(row*sizeof(int));
9865: for ( j = 0; j < col; j++ ) {
9866: for ( i = 0; i < row; i++ )
9867: a = mat[i][j] %= md;
9868: for ( i = 0; i < row; i++ )
9869: if ( !used[i] && mat[i][j] ) break;
9870: if ( i == row ) {
9871: colstat[j] = 0;
9872: continue;
9873: } else {
9874: colstat[j] = 1;
9875: used[i] = 1;
9876: }
9877: /* column j is normalized */
9878: s = sugar[i];
9879: inv = invm(mat[i][j],md);
9880: /* normalize pivot row */
9881: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9882: DMAR(*pk,inv,0,md,*pk);
9883: }
9884: for ( k = i+1; k < row; k++ ) {
9885: if ( (a = mat[k][j]) != 0 ) {
9886: sugar[k] = MAX(sugar[k],s);
9887: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9888: Nf4_red++;
9889: }
9890: }
9891: }
9892: rank = 0;
9893: for ( i = 0; i < row; i++ ) {
9894: for ( j = 0; j < col; j++ )
9895: if ( mat[i][j] ) break;
9896: if ( j == col ) sugar[i] = -1;
9897: else rank++;
9898: }
9899: return rank;
9900: }
9901:
1.1 noro 9902:
1.7 noro 9903: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9904: {
1.7 noro 9905: int i,j,k,l,inv,a,rank,s;
9906: unsigned int *t,*pivot,*pk;
9907: unsigned int **mat;
9908:
9909: mat = (unsigned int **)mat0;
9910: for ( rank = 0, j = 0; j < col; j++ ) {
9911: for ( i = rank; i < row; i++ )
9912: if ( mat[i][j] )
9913: break;
9914: if ( i == row ) {
9915: colstat[j] = 0;
9916: continue;
9917: } else
9918: colstat[j] = 1;
9919: if ( i != rank ) {
9920: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9921: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9922: }
9923: pivot = mat[rank];
9924: s = sugar[rank];
9925: inv = _invsf(pivot[j]);
9926: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9927: if ( *pk )
9928: *pk = _mulsf(*pk,inv);
9929: for ( i = rank+1; i < row; i++ ) {
9930: t = mat[i];
9931: if ( (a = t[j]) != 0 ) {
9932: sugar[i] = MAX(sugar[i],s);
9933: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9934: }
9935: }
9936: rank++;
9937: }
9938: for ( j = col-1, l = rank-1; j >= 0; j-- )
9939: if ( colstat[j] ) {
9940: pivot = mat[l];
9941: s = sugar[l];
9942: for ( i = 0; i < l; i++ ) {
9943: t = mat[i];
9944: if ( (a = t[j]) != 0 ) {
9945: sugar[i] = MAX(sugar[i],s);
9946: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9947: }
9948: }
9949: l--;
9950: }
9951: return rank;
9952: }
1.1 noro 9953:
1.7 noro 9954: int ndv_ishomo(NDV p)
9955: {
9956: NMV m;
9957: int len,h;
1.1 noro 9958:
9959: if ( !p ) return 1;
9960: len = LEN(p);
9961: m = BDY(p);
9962: h = TD(DL(m));
9963: NMV_ADV(m);
9964: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9965: if ( TD(DL(m)) != h ) {
9966: return 0;
9967: }
1.1 noro 9968: return 1;
9969: }
9970:
9971: void ndv_save(NDV p,int index)
9972: {
9973: FILE *s;
9974: char name[BUFSIZ];
9975: short id;
9976: int nv,sugar,len,n,i,td,e,j;
9977: NMV m;
9978: unsigned int *dl;
9979: int mpos;
9980:
9981: sprintf(name,"%s/%d",Demand,index);
9982: s = fopen(name,"w");
9983: savevl(s,0);
9984: if ( !p ) {
9985: saveobj(s,0);
9986: return;
9987: }
9988: id = O_DP;
9989: nv = NV(p);
9990: sugar = SG(p);
9991: len = LEN(p);
9992: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9993: write_int(s,(unsigned int *)&len);
9994:
9995: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9996: saveobj(s,(Obj)CZ(m));
1.1 noro 9997: dl = DL(m);
9998: td = TD(dl);
9999: write_int(s,(unsigned int *)&td);
10000: for ( j = 0; j < nv; j++ ) {
10001: e = GET_EXP(dl,j);
10002: write_int(s,(unsigned int *)&e);
10003: }
10004: if ( nd_module ) {
10005: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
10006: }
10007: }
10008: fclose(s);
10009: }
10010:
10011: void nd_save_mod(ND p,int index)
10012: {
10013: FILE *s;
10014: char name[BUFSIZ];
10015: int nv,sugar,len,c;
10016: NM m;
10017:
10018: sprintf(name,"%s/%d",Demand,index);
10019: s = fopen(name,"w");
10020: if ( !p ) {
10021: len = 0;
10022: write_int(s,(unsigned int *)&len);
10023: fclose(s);
10024: return;
10025: }
10026: nv = NV(p);
10027: sugar = SG(p);
10028: len = LEN(p);
10029: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
10030: for ( m = BDY(p); m; m = NEXT(m) ) {
10031: c = CM(m); write_int(s,(unsigned int *)&c);
10032: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
10033: }
10034: fclose(s);
10035: }
10036:
10037: NDV ndv_load(int index)
10038: {
10039: FILE *s;
10040: char name[BUFSIZ];
10041: short id;
10042: int nv,sugar,len,n,i,td,e,j;
10043: NDV d;
10044: NMV m0,m;
10045: unsigned int *dl;
10046: Obj obj;
10047: int mpos;
10048:
10049: sprintf(name,"%s/%d",Demand,index);
10050: s = fopen(name,"r");
10051: if ( !s ) return 0;
10052:
10053: skipvl(s);
10054: read_short(s,(unsigned short *)&id);
10055: if ( !id ) return 0;
10056: read_int(s,(unsigned int *)&nv);
10057: read_int(s,(unsigned int *)&sugar);
10058: read_int(s,(unsigned int *)&len);
10059:
10060: m0 = m = MALLOC(len*nmv_adv);
10061: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 10062: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 10063: dl = DL(m);
10064: ndl_zero(dl);
10065: read_int(s,(unsigned int *)&td); TD(dl) = td;
10066: for ( j = 0; j < nv; j++ ) {
10067: read_int(s,(unsigned int *)&e);
10068: PUT_EXP(dl,j,e);
10069: }
10070: if ( nd_module ) {
10071: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
10072: }
10073: if ( nd_blockmask ) ndl_weight_mask(dl);
10074: }
10075: fclose(s);
10076: MKNDV(nv,m0,len,d);
10077: SG(d) = sugar;
10078: return d;
10079: }
10080:
10081: ND nd_load_mod(int index)
10082: {
10083: FILE *s;
10084: char name[BUFSIZ];
10085: int nv,sugar,len,i,c;
10086: ND d;
10087: NM m0,m;
10088:
10089: sprintf(name,"%s/%d",Demand,index);
10090: s = fopen(name,"r");
10091: /* if the file does not exist, it means p[index]=0 */
10092: if ( !s ) return 0;
10093:
10094: read_int(s,(unsigned int *)&nv);
10095: if ( !nv ) { fclose(s); return 0; }
10096:
10097: read_int(s,(unsigned int *)&sugar);
10098: read_int(s,(unsigned int *)&len);
10099: for ( m0 = 0, i = 0; i < len; i++ ) {
10100: NEXTNM(m0,m);
10101: read_int(s,(unsigned int *)&c); CM(m) = c;
10102: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
10103: }
10104: NEXT(m) = 0;
10105: MKND(nv,m0,len,d);
10106: SG(d) = sugar;
10107: fclose(s);
10108: return d;
10109: }
10110:
10111: void nd_det(int mod,MAT f,P *rp)
10112: {
10113: VL fv,tv;
10114: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
10115: pointer **m;
10116: P **w;
10117: P mp,r;
10118: NDV **dm;
10119: NDV *t,*mi,*mj;
10120: NDV d,s,mij,mjj;
10121: ND u;
10122: NMV nmv;
10123: UINT *bound;
10124: PGeoBucket bucket;
10125: struct order_spec *ord;
10126: Z dq,dt,ds;
10127: Z mone;
10128: Z gn,qn,dn0,nm,dn;
10129:
10130: create_order_spec(0,0,&ord);
10131: nd_init_ord(ord);
10132: get_vars((Obj)f,&fv);
10133: if ( f->row != f->col )
10134: error("nd_det : non-square matrix");
10135: n = f->row;
10136: m = f->body;
10137: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
10138:
10139: if ( !nvar ) {
10140: if ( !mod )
10141: detp(CO,(P **)m,n,rp);
10142: else {
10143: w = (P **)almat_pointer(n,n);
10144: for ( i = 0; i < n; i++ )
10145: for ( j = 0; j < n; j++ )
10146: ptomp(mod,(P)m[i][j],&w[i][j]);
10147: detmp(CO,mod,w,n,&mp);
10148: mptop(mp,rp);
10149: }
10150: return;
10151: }
10152:
10153: if ( !mod ) {
10154: w = (P **)almat_pointer(n,n);
10155: dq = ONE;
10156: for ( i = 0; i < n; i++ ) {
10157: dn0 = ONE;
10158: for ( j = 0; j < n; j++ ) {
10159: if ( !m[i][j] ) continue;
10160: lgp(m[i][j],&nm,&dn);
1.6 noro 10161: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 10162: }
10163: if ( !UNIZ(dn0) ) {
10164: ds = dn0;
10165: for ( j = 0; j < n; j++ )
10166: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
10167: mulz(dq,ds,&dt); dq = dt;
10168: } else
10169: for ( j = 0; j < n; j++ )
10170: w[i][j] = (P)m[i][j];
10171: }
10172: m = (pointer **)w;
10173: }
10174:
10175: for ( i = 0, max = 1; i < n; i++ )
10176: for ( j = 0; j < n; j++ )
10177: for ( tv = fv; tv; tv = NEXT(tv) ) {
10178: e = getdeg(tv->v,(P)m[i][j]);
10179: max = MAX(e,max);
10180: }
10181: nd_setup_parameters(nvar,max);
10182: dm = (NDV **)almat_pointer(n,n);
10183: for ( i = 0, max = 1; i < n; i++ )
10184: for ( j = 0; j < n; j++ ) {
10185: dm[i][j] = ptondv(CO,fv,m[i][j]);
10186: if ( mod ) ndv_mod(mod,dm[i][j]);
10187: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
10188: }
10189: d = ptondv(CO,fv,(P)ONE);
10190: if ( mod ) ndv_mod(mod,d);
10191: chsgnz(ONE,&mone);
10192: for ( j = 0, sgn = 1; j < n; j++ ) {
10193: if ( DP_Print ) {
10194: fprintf(asir_out,".");
10195: }
10196: for ( i = j; i < n && !dm[i][j]; i++ );
10197: if ( i == n ) {
10198: *rp = 0;
10199: return;
10200: }
10201: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
10202: for ( k = j; k < n; k++ )
10203: for ( l = j; l < n; l++ )
10204: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
10205: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
10206: }
10207: if ( k0 != j ) {
10208: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
10209: sgn = -sgn;
10210: }
10211: if ( l0 != j ) {
10212: for ( k = j; k < n; k++ ) {
10213: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
10214: }
10215: sgn = -sgn;
10216: }
10217: bound = nd_det_compute_bound(dm,n,j);
10218: for ( k = 0; k < nd_nvar; k++ )
10219: if ( bound[k]*2 > nd_mask0 ) break;
10220: if ( k < nd_nvar )
10221: nd_det_reconstruct(dm,n,j,d);
10222:
10223: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
10224: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
10225: mi = dm[i]; mij = mi[j];
10226: if ( mod )
10227: ndv_mul_c(mod,mij,mod-1);
10228: else
10229: ndv_mul_c_q(mij,mone);
10230: for ( k = j+1; k < n; k++ ) {
10231: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
10232: bucket = create_pbucket();
10233: if ( mi[k] ) {
10234: nmv = BDY(mjj); len = LEN(mjj);
10235: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10236: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
10237: add_pbucket(mod,bucket,u);
10238: }
10239: }
10240: if ( mj[k] && mij ) {
10241: nmv = BDY(mij); len = LEN(mij);
10242: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10243: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
10244: add_pbucket(mod,bucket,u);
10245: }
10246: }
10247: u = nd_quo(mod,bucket,d);
10248: mi[k] = ndtondv(mod,u);
10249: }
10250: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
10251: }
10252: d = mjj;
10253: }
10254: if ( DP_Print ) {
10255: fprintf(asir_out,"\n");
10256: }
10257: if ( sgn < 0 ) {
10258: if ( mod )
10259: ndv_mul_c(mod,d,mod-1);
10260: else
10261: ndv_mul_c_q(d,mone);
10262: }
10263: r = ndvtop(mod,CO,fv,d);
10264: if ( !mod && !UNIQ(dq) )
10265: divsp(CO,r,(P)dq,rp);
10266: else
10267: *rp = r;
10268: }
10269:
10270: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
10271: {
10272: NM mr,mr0;
10273: NM tnm;
10274: NMV m;
10275: UINT *d0,*dt,*dm;
10276: int c,n,td,i,c1,c2,len;
10277: Z q;
10278: ND r;
10279:
10280: if ( !p ) return 0;
10281: else {
10282: n = NV(p); m = BDY(p); len = LEN(p);
10283: d0 = DL(m0);
10284: td = TD(d);
10285: mr0 = 0;
10286: NEWNM(tnm);
10287: if ( mod ) {
10288: c = CM(m0);
10289: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10290: ndl_add(DL(m),d0,DL(tnm));
10291: if ( ndl_reducible(DL(tnm),d) ) {
10292: NEXTNM(mr0,mr);
10293: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
10294: ndl_copy(DL(tnm),DL(mr));
10295: }
10296: }
10297: } else {
1.6 noro 10298: q = CZ(m0);
1.1 noro 10299: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10300: ndl_add(DL(m),d0,DL(tnm));
10301: if ( ndl_reducible(DL(tnm),d) ) {
10302: NEXTNM(mr0,mr);
1.6 noro 10303: mulz(CZ(m),q,&CZ(mr));
1.1 noro 10304: ndl_copy(DL(tnm),DL(mr));
10305: }
10306: }
10307: }
10308: if ( !mr0 )
10309: return 0;
10310: else {
10311: NEXT(mr) = 0;
10312: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10313: MKND(NV(p),mr0,len,r);
10314: SG(r) = SG(p) + TD(d0);
10315: return r;
10316: }
10317: }
10318: }
10319:
10320: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
10321: {
10322: int i,obpe,oadv,h,k,l;
10323: static NM prev_nm_free_list;
10324: EPOS oepos;
10325:
10326: obpe = nd_bpe;
10327: oadv = nmv_adv;
10328: oepos = nd_epos;
10329: if ( obpe < 2 ) nd_bpe = 2;
10330: else if ( obpe < 3 ) nd_bpe = 3;
10331: else if ( obpe < 4 ) nd_bpe = 4;
10332: else if ( obpe < 5 ) nd_bpe = 5;
10333: else if ( obpe < 6 ) nd_bpe = 6;
10334: else if ( obpe < 8 ) nd_bpe = 8;
10335: else if ( obpe < 10 ) nd_bpe = 10;
10336: else if ( obpe < 16 ) nd_bpe = 16;
10337: else if ( obpe < 32 ) nd_bpe = 32;
10338: else error("nd_det_reconstruct : exponent too large");
10339:
10340: nd_setup_parameters(nd_nvar,0);
10341: prev_nm_free_list = _nm_free_list;
10342: _nm_free_list = 0;
10343: for ( k = j; k < n; k++ )
10344: for (l = j; l < n; l++ )
10345: ndv_realloc(dm[k][l],obpe,oadv,oepos);
10346: ndv_realloc(d,obpe,oadv,oepos);
10347: prev_nm_free_list = 0;
10348: #if 0
10349: GC_gcollect();
10350: #endif
10351: }
10352:
10353: /* returns a UINT array containing degree bounds */
10354:
10355: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
10356: {
10357: UINT *d0,*d1,*d,*t,*r;
10358: int k,l,i;
10359:
10360: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
10361: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
10362: for ( k = j; k < n; k++ )
10363: for ( l = j; l < n; l++ )
10364: if ( dm[k][l] ) {
10365: d = ndv_compute_bound(dm[k][l]);
10366: for ( i = 0; i < nd_nvar; i++ )
10367: d0[i] = MAX(d0[i],d[i]);
10368: }
10369: return d0;
10370: }
10371:
10372: DL nd_separate_d(UINT *d,UINT *trans)
10373: {
10374: int n,td,i,e,j;
10375: DL a;
10376:
10377: ndl_zero(trans);
10378: td = 0;
10379: for ( i = 0; i < nd_ntrans; i++ ) {
10380: e = GET_EXP(d,i);
10381: PUT_EXP(trans,i,e);
10382: td += MUL_WEIGHT(e,i);
10383: }
10384: if ( nd_ntrans+nd_nalg < nd_nvar ) {
10385: /* homogenized */
10386: i = nd_nvar-1;
10387: e = GET_EXP(d,i);
10388: PUT_EXP(trans,i,e);
10389: td += MUL_WEIGHT(e,i);
10390: }
10391: TD(trans) = td;
10392: if ( nd_blockmask) ndl_weight_mask(trans);
10393: NEWDL(a,nd_nalg);
10394: td = 0;
10395: for ( i = 0; i < nd_nalg; i++ ) {
10396: j = nd_ntrans+i;
10397: e = GET_EXP(d,j);
10398: a->d[i] = e;
10399: td += e;
10400: }
10401: a->td = td;
10402: return a;
10403: }
10404:
10405: int nd_monic(int mod,ND *p)
10406: {
10407: UINT *trans,*t;
10408: DL alg;
10409: MP mp0,mp;
10410: NM m,m0,m1,ma0,ma,mb,mr0,mr;
10411: ND r;
10412: DL dl;
10413: DP nm;
10414: NDV ndv;
10415: DAlg inv,cd;
10416: ND s,c;
10417: Z l,mul;
10418: Z ln;
10419: int n,ntrans,i,e,td,is_lc,len;
10420: NumberField nf;
10421: struct oEGT eg0,eg1;
10422:
10423: if ( !(nf = get_numberfield()) )
10424: error("nd_monic : current_numberfield is not set");
10425:
10426: /* Q coef -> DAlg coef */
10427: NEWNM(ma0); ma = ma0;
10428: m = BDY(*p);
10429: is_lc = 1;
10430: while ( 1 ) {
10431: NEWMP(mp0); mp = mp0;
1.6 noro 10432: mp->c = (Obj)CZ(m);
1.1 noro 10433: mp->dl = nd_separate_d(DL(m),DL(ma));
10434: NEWNM(mb);
10435: for ( m = NEXT(m); m; m = NEXT(m) ) {
10436: alg = nd_separate_d(DL(m),DL(mb));
10437: if ( !ndl_equal(DL(ma),DL(mb)) )
10438: break;
1.6 noro 10439: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 10440: }
10441: NEXT(mp) = 0;
10442: MKDP(nd_nalg,mp0,nm);
10443: MKDAlg(nm,ONE,cd);
10444: if ( is_lc == 1 ) {
10445: /* if the lc is a rational number, we have nothing to do */
10446: if ( !mp0->dl->td )
10447: return 1;
10448:
10449: get_eg(&eg0);
10450: invdalg(cd,&inv);
10451: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
10452: /* check the validity of inv */
10453: if ( mod && !remqi((Q)inv->dn,mod) )
10454: return 0;
10455: CA(ma) = nf->one;
10456: is_lc = 0;
10457: ln = ONE;
10458: } else {
10459: muldalg(cd,inv,&CA(ma));
10460: lcmz(ln,CA(ma)->dn,&ln);
10461: }
10462: if ( m ) {
10463: NEXT(ma) = mb; ma = mb;
10464: } else {
10465: NEXT(ma) = 0;
10466: break;
10467: }
10468: }
10469: /* l = lcm(denoms) */
10470: l = ln;
10471: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 10472: divsz(l,CA(m)->dn,&mul);
1.1 noro 10473: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
10474: NEXTNM(mr0,mr);
1.6 noro 10475: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 10476: dl = mp->dl;
10477: td = TD(DL(m));
10478: ndl_copy(DL(m),DL(mr));
10479: for ( i = 0; i < nd_nalg; i++ ) {
10480: e = dl->d[i];
10481: PUT_EXP(DL(mr),i+nd_ntrans,e);
10482: td += MUL_WEIGHT(e,i+nd_ntrans);
10483: }
10484: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
10485: TD(DL(mr)) = td;
10486: if ( nd_blockmask) ndl_weight_mask(DL(mr));
10487: }
10488: }
10489: NEXT(mr) = 0;
10490: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10491: MKND(NV(*p),mr0,len,r);
10492: /* XXX */
10493: SG(r) = SG(*p);
10494: nd_free(*p);
10495: *p = r;
10496: return 1;
10497: }
10498:
10499: NODE reverse_node(NODE n)
10500: {
10501: NODE t,t1;
10502:
10503: for ( t = 0; n; n = NEXT(n) ) {
10504: MKNODE(t1,BDY(n),t); t = t1;
10505: }
10506: return t;
10507: }
10508:
10509: P ndc_div(int mod,union oNDC a,union oNDC b)
10510: {
10511: union oNDC c;
10512: int inv,t;
10513:
10514: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 10515: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 10516: else if ( mod ) {
10517: inv = invm(b.m,mod);
10518: DMAR(a.m,inv,0,mod,t); c.m = t;
10519: } else if ( nd_vc )
10520: divsp(nd_vc,a.p,b.p,&c.p);
10521: else
10522: divsz(a.z,b.z,&c.z);
10523: return ndctop(mod,c);
10524: }
10525:
10526: P ndctop(int mod,union oNDC c)
10527: {
10528: Z q;
10529: int e;
10530: GFS gfs;
10531:
10532: if ( mod == -1 ) {
10533: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
10534: } else if ( mod == -2 ) {
1.10 noro 10535: q = c.z; return (P)q;
1.1 noro 10536: } else if ( mod > 0 ) {
1.6 noro 10537: STOZ(c.m,q); return (P)q;
1.1 noro 10538: } else
10539: return (P)c.p;
10540: }
10541:
10542: /* [0,0,0,cont] = p -> p/cont */
10543:
10544: void finalize_tracelist(int i,P cont)
10545: {
10546: LIST l;
10547: NODE node;
10548: Z iq;
10549:
10550: if ( !UNIQ(cont) ) {
10551: node = mknode(4,NULLP,NULLP,NULLP,cont);
10552: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
10553: nd_tracelist = node;
10554: }
1.6 noro 10555: STOZ(i,iq);
1.1 noro 10556: nd_tracelist = reverse_node(nd_tracelist);
10557: MKLIST(l,nd_tracelist);
10558: node = mknode(2,iq,l); MKLIST(l,node);
10559: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
10560: nd_alltracelist = node; nd_tracelist = 0;
10561: }
10562:
10563: void conv_ilist(int demand,int trace,NODE g,int **indp)
10564: {
10565: int n,i,j;
10566: int *ind;
10567: NODE t;
10568:
10569: n = length(g);
10570: ind = (int *)MALLOC(n*sizeof(int));
10571: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
10572: j = (long)BDY(t); ind[i] = j;
10573: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
10574: }
10575: if ( indp ) *indp = ind;
10576: }
10577:
1.30 noro 10578: NODE conv_ilist_s(int demand,int trace,int **indp)
10579: {
10580: int n,i,j;
10581: int *ind;
10582: NODE g0,g;
10583:
10584: n = nd_psn;
10585: ind = (int *)MALLOC(n*sizeof(int));
10586: g0 = 0;
10587: for ( i = 0; i < n; i++ ) {
10588: ind[i] = i;
10589: NEXTNODE(g0,g);
10590: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
10591: }
10592: if ( g0 ) NEXT(g) = 0;
10593: if ( indp ) *indp = ind;
10594: return g0;
10595: }
10596:
1.46 noro 10597: void parse_nd_option(VL vl,NODE opt)
1.1 noro 10598: {
1.38 noro 10599: NODE t,p,u;
1.1 noro 10600: int i,s,n;
1.38 noro 10601: char *key;
10602: Obj value;
1.46 noro 10603: VL oldvl;
1.1 noro 10604:
1.41 noro 10605: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 10606: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
10607: nd_splist = 0; nd_check_splist = 0;
1.38 noro 10608: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
10609: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 10610: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46 noro 10611: nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.49 noro 10612: nd_hpdata = 0;
1.39 noro 10613:
1.38 noro 10614: for ( t = opt; t; t = NEXT(t) ) {
10615: p = BDY((LIST)BDY(t));
10616: key = BDY((STRING)BDY(p));
10617: value = (Obj)BDY(NEXT(p));
10618: if ( !strcmp(key,"gentrace") )
10619: nd_gentrace = value?1:0;
10620: else if ( !strcmp(key,"gensyz") )
10621: nd_gensyz = value?1:0;
10622: else if ( !strcmp(key,"nora") )
10623: nd_nora = value?1:0;
1.41 noro 10624: else if ( !strcmp(key,"norb") )
10625: nd_norb = value?1:0;
1.38 noro 10626: else if ( !strcmp(key,"gbblock") ) {
10627: if ( value && OID(value) == O_LIST ) {
1.1 noro 10628: u = BDY((LIST)value);
1.38 noro 10629: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 10630: for ( i = 0; u; u = NEXT(u) ) {
10631: p = BDY((LIST)BDY(u));
1.6 noro 10632: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
10633: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 10634: }
10635: nd_gbblock[i] = -1;
1.38 noro 10636: } else
10637: nd_gbblock = 0;
1.1 noro 10638: } else if ( !strcmp(key,"newelim") )
10639: nd_newelim = value?1:0;
10640: else if ( !strcmp(key,"intersect") )
10641: nd_intersect = value?1:0;
1.17 noro 10642: else if ( !strcmp(key,"syzgen") )
10643: nd_intersect = ZTOS((Q)value);
1.1 noro 10644: else if ( !strcmp(key,"lf") )
10645: nd_lf = value?1:0;
10646: else if ( !strcmp(key,"trace") ) {
1.38 noro 10647: if ( value ) {
10648: u = BDY((LIST)value);
10649: nd_nzlist = BDY((LIST)ARG2(u));
10650: nd_bpe = ZTOS((Q)ARG3(u));
10651: }
1.1 noro 10652: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 10653: nd_f4red = ZTOS((Q)value);
1.1 noro 10654: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 10655: nd_rank0 = value?1:0;
1.1 noro 10656: } else if ( !strcmp(key,"splist") ) {
1.38 noro 10657: nd_splist = value?1:0;
1.1 noro 10658: } else if ( !strcmp(key,"check_splist") ) {
10659: nd_check_splist = BDY((LIST)value);
1.49 noro 10660: } else if ( !strcmp(key,"hpdata") ) {
1.50 noro 10661: if ( value )
10662: nd_hpdata = BDY((LIST)value);
1.1 noro 10663: } else if ( !strcmp(key,"sugarweight") ) {
10664: u = BDY((LIST)value);
1.38 noro 10665: n = length(u);
10666: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 10667: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 10668: nd_sugarweight[i] = ZTOS((Q)BDY(u));
10669: } else if ( !strcmp(key,"f4_td") ) {
10670: nd_f4_td = value?1:0;
10671: } else if ( !strcmp(key,"sba_f4step") ) {
10672: nd_sba_f4step = value?ZTOS((Q)value):0;
10673: } else if ( !strcmp(key,"sba_pot") ) {
1.44 noro 10674: nd_sba_pot = ZTOS((Q)value);
1.38 noro 10675: } else if ( !strcmp(key,"sba_largelcm") ) {
10676: nd_sba_largelcm = value?1:0;
1.39 noro 10677: } else if ( !strcmp(key,"sba_dontsort") ) {
10678: nd_sba_dontsort = value?1:0;
1.44 noro 10679: } else if ( !strcmp(key,"sba_syz") ) {
10680: nd_sba_syz = value?1:0;
1.46 noro 10681: } else if ( !strcmp(key,"sba_modord") ) {
10682: // value=[vlist,ordspec,weight]
10683: u = BDY((LIST)value);
10684: pltovl((LIST)ARG0(u),&oldvl);
10685: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10686: } else if ( !strcmp(key,"sba_gbinput") ) {
10687: nd_sba_inputisgb = value?1:0;
10688: if ( nd_sba_inputisgb != 0 ) {
10689: // value=[vlist,ordspec,weight]
10690: u = BDY((LIST)value);
10691: pltovl((LIST)ARG0(u),&oldvl);
10692: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10693: }
1.41 noro 10694: } else if ( !strcmp(key,"sba_redundant_check") ) {
10695: nd_sba_redundant_check = value?1:0;
1.39 noro 10696: } else if ( !strcmp(key,"top") ) {
10697: nd_top = value?1:0;
1.1 noro 10698: }
1.38 noro 10699: }
1.44 noro 10700: if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1 noro 10701: }
10702:
10703: ND mdptond(DP d);
10704: ND nd_mul_nm(int mod,NM m0,ND p);
10705: ND nd_mul_nm_lf(NM m0,ND p);
10706: ND *btog(NODE ti,ND **p,int nb,int mod);
10707: ND btog_one(NODE ti,ND *p,int nb,int mod);
10708: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10709: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10710:
10711: /* d:monomial */
10712: ND mdptond(DP d)
10713: {
10714: NM m;
10715: ND r;
10716:
10717: if ( OID(d) == 1 )
10718: r = ptond(CO,CO,(P)d);
10719: else {
10720: NEWNM(m);
10721: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10722: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10723: NEXT(m) = 0;
10724: MKND(NV(d),m,1,r);
10725: }
10726: return r;
10727: }
10728:
10729: ND nd_mul_nm(int mod,NM m0,ND p)
10730: {
10731: UINT *d0;
10732: int c0,c1,c;
10733: NM tm,mr,mr0;
10734: ND r;
10735:
10736: if ( !p ) return 0;
10737: d0 = DL(m0);
10738: c0 = CM(m0);
10739: mr0 = 0;
10740: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10741: NEXTNM(mr0,mr);
10742: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10743: ndl_add(d0,DL(tm),DL(mr));
10744: }
10745: NEXT(mr) = 0;
10746: MKND(NV(p),mr0,LEN(p),r);
10747: return r;
10748: }
10749:
10750: ND nd_mul_nm_lf(NM m0,ND p)
10751: {
10752: UINT *d0;
10753: Z c0,c1,c;
10754: NM tm,mr,mr0;
10755: ND r;
10756:
10757: if ( !p ) return 0;
10758: d0 = DL(m0);
10759: c0 = CZ(m0);
10760: mr0 = 0;
10761: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10762: NEXTNM(mr0,mr);
10763: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10764: ndl_add(d0,DL(tm),DL(mr));
10765: }
10766: NEXT(mr) = 0;
10767: MKND(NV(p),mr0,LEN(p),r);
10768: return r;
10769: }
10770:
10771: ND *btog(NODE ti,ND **p,int nb,int mod)
10772: {
10773: PGeoBucket *r;
10774: int i,ci;
10775: NODE t,s;
10776: ND m,tp;
10777: ND *pi,*rd;
10778: P c;
10779:
10780: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10781: for ( i = 0; i < nb; i++ )
10782: r[i] = create_pbucket();
10783: for ( t = ti; t; t = NEXT(t) ) {
10784: s = BDY((LIST)BDY(t));
10785: if ( ARG0(s) ) {
10786: m = mdptond((DP)ARG2(s));
1.6 noro 10787: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10788: if ( (ci = ((MQ)c)->cont) != 0 ) {
10789: HCM(m) = ci;
1.6 noro 10790: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10791: for ( i = 0; i < nb; i++ ) {
10792: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10793: add_pbucket(mod,r[i],tp);
10794: }
10795: }
10796: ci = 1;
10797: } else {
10798: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10799: ci = invm(ci,mod);
10800: }
10801: }
10802: rd = (ND *)MALLOC(nb*sizeof(ND));
10803: for ( i = 0; i < nb; i++ )
10804: rd[i] = normalize_pbucket(mod,r[i]);
10805: if ( ci != 1 )
10806: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10807: return rd;
10808: }
10809:
10810: /* YYY */
10811: ND *btog_lf(NODE ti,ND **p,int nb)
10812: {
10813: PGeoBucket *r;
10814: int i;
10815: NODE t,s;
10816: ND m,tp;
10817: ND *pi,*rd;
10818: LM lm;
10819: Z lf,c;
10820:
10821: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10822: for ( i = 0; i < nb; i++ )
10823: r[i] = create_pbucket();
10824: for ( t = ti; t; t = NEXT(t) ) {
10825: s = BDY((LIST)BDY(t));
10826: if ( ARG0(s) ) {
10827: m = mdptond((DP)ARG2(s));
1.6 noro 10828: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10829: if ( lm ) {
10830: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10831: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10832: for ( i = 0; i < nb; i++ ) {
10833: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10834: add_pbucket(-2,r[i],tp);
10835: }
10836: }
10837: c = ONE;
10838: } else {
10839: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10840: }
10841: }
10842: rd = (ND *)MALLOC(nb*sizeof(ND));
10843: for ( i = 0; i < nb; i++ )
10844: rd[i] = normalize_pbucket(-2,r[i]);
10845: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10846: return rd;
10847: }
10848:
10849: ND btog_one(NODE ti,ND *p,int nb,int mod)
10850: {
10851: PGeoBucket r;
10852: int i,ci,j;
10853: NODE t,s;
10854: ND m,tp;
10855: ND pi,rd;
10856: P c;
10857:
10858: r = create_pbucket();
10859: for ( t = ti; t; t = NEXT(t) ) {
10860: s = BDY((LIST)BDY(t));
10861: if ( ARG0(s) ) {
10862: m = mdptond((DP)ARG2(s));
1.6 noro 10863: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10864: if ( (ci = ((MQ)c)->cont) != 0 ) {
10865: HCM(m) = ci;
1.6 noro 10866: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10867: if ( !pi ) {
10868: pi = nd_load_mod(j);
10869: tp = nd_mul_nm(mod,BDY(m),pi);
10870: nd_free(pi);
10871: add_pbucket(mod,r,tp);
10872: } else {
10873: tp = nd_mul_nm(mod,BDY(m),pi);
10874: add_pbucket(mod,r,tp);
10875: }
10876: }
10877: ci = 1;
10878: } else {
10879: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10880: ci = invm(ci,mod);
10881: }
10882: }
10883: rd = normalize_pbucket(mod,r);
10884: free_pbucket(r);
10885: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10886: return rd;
10887: }
10888:
10889: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10890:
10891: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10892: {
10893: int i,j,n,m,nb,pi0,pi1,nvar;
10894: VL fv,tv,vv;
10895: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10896: ND **p;
10897: ND *c;
10898: ND u;
10899: P inv;
10900: MAT mat;
10901:
10902: if ( mod == -2 )
10903: return nd_btog_lf(f,v,ord,tlist,rp);
10904:
10905: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10906: parse_nd_option(vv,current_option);
1.1 noro 10907: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10908: switch ( ord->id ) {
10909: case 1:
10910: if ( ord->nv != nvar )
10911: error("nd_check : invalid order specification");
10912: break;
10913: default:
10914: break;
10915: }
10916: nd_init_ord(ord);
10917: #if 0
1.6 noro 10918: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10919: #else
10920: nd_bpe = 32;
10921: #endif
10922: nd_setup_parameters(nvar,0);
10923: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10924: intred = BDY((LIST)ARG3(BDY(tlist)));
10925: ind = BDY((LIST)ARG4(BDY(tlist)));
10926: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10927: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10928: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10929: if ( j > i ) i = j;
10930: }
10931: n = i+1;
10932: nb = length(BDY(f));
10933: p = (ND **)MALLOC(n*sizeof(ND *));
10934: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10935: pi = BDY((LIST)BDY(t));
1.6 noro 10936: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10937: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10938: ptomp(mod,(P)ARG2(pi),&inv);
10939: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10940: u = ptond(CO,vv,(P)ONE);
10941: HCM(u) = ((MQ)inv)->cont;
10942: c[pi1] = u;
10943: }
10944: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10945: printf("%d ",i); fflush(stdout);
10946: ti = BDY((LIST)BDY(t));
1.6 noro 10947: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10948: }
10949: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10950: printf("%d ",i); fflush(stdout);
10951: ti = BDY((LIST)BDY(t));
1.6 noro 10952: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10953: }
10954: m = length(ind);
10955: MKMAT(mat,nb,m);
10956: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10957: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10958: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10959: return mat;
10960: }
10961:
10962: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10963: {
10964: int i,j,n,m,nb,pi0,pi1,nvar;
10965: VL fv,tv,vv;
10966: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10967: ND **p;
10968: ND *c;
10969: ND u;
10970: MAT mat;
10971: LM lm;
10972: Z lf,inv;
10973:
10974: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10975: parse_nd_option(vv,current_option);
1.1 noro 10976: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10977: switch ( ord->id ) {
10978: case 1:
10979: if ( ord->nv != nvar )
10980: error("nd_check : invalid order specification");
10981: break;
10982: default:
10983: break;
10984: }
10985: nd_init_ord(ord);
10986: #if 0
1.6 noro 10987: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10988: #else
10989: nd_bpe = 32;
10990: #endif
10991: nd_setup_parameters(nvar,0);
10992: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10993: intred = BDY((LIST)ARG3(BDY(tlist)));
10994: ind = BDY((LIST)ARG4(BDY(tlist)));
10995: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10996: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10997: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10998: if ( j > i ) i = j;
10999: }
11000: n = i+1;
11001: nb = length(BDY(f));
11002: p = (ND **)MALLOC(n*sizeof(ND *));
11003: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
11004: pi = BDY((LIST)BDY(t));
1.6 noro 11005: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 11006: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
11007: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
11008: u = ptond(CO,vv,(P)ONE);
11009: HCZ(u) = inv;
11010: c[pi1] = u;
11011: }
11012: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
11013: printf("%d ",i); fflush(stdout);
11014: ti = BDY((LIST)BDY(t));
1.6 noro 11015: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 11016: }
11017: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
11018: printf("%d ",i); fflush(stdout);
11019: ti = BDY((LIST)BDY(t));
1.6 noro 11020: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 11021: }
11022: m = length(ind);
11023: MKMAT(mat,nb,m);
11024: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 11025: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 11026: BDY(mat)[i][j] = ndtodp(-2,c[i]);
11027: return mat;
11028: }
11029:
11030: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
11031: LIST tlist,int pos,MAT *rp)
11032: {
11033: int i,j,n,m,nb,pi0,pi1,nvar;
11034: VL fv,tv,vv;
11035: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
11036: ND *p;
11037: ND *c;
11038: ND u;
11039: P inv;
11040: VECT vect;
11041:
11042: if ( mod == -2 )
11043: error("nd_btog_one : not implemented yet for a large finite field");
11044:
11045: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 11046: parse_nd_option(vv,current_option);
1.1 noro 11047: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
11048: switch ( ord->id ) {
11049: case 1:
11050: if ( ord->nv != nvar )
11051: error("nd_check : invalid order specification");
11052: break;
11053: default:
11054: break;
11055: }
11056: nd_init_ord(ord);
11057: #if 0
1.6 noro 11058: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 11059: #else
11060: nd_bpe = 32;
11061: #endif
11062: nd_setup_parameters(nvar,0);
11063: permtrace = BDY((LIST)ARG2(BDY(tlist)));
11064: intred = BDY((LIST)ARG3(BDY(tlist)));
11065: ind = BDY((LIST)ARG4(BDY(tlist)));
11066: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
11067: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 11068: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 11069: if ( j > i ) i = j;
11070: }
11071: n = i+1;
11072: nb = length(BDY(f));
11073: p = (ND *)MALLOC(n*sizeof(ND *));
11074: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
11075: pi = BDY((LIST)BDY(t));
1.6 noro 11076: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 11077: if ( pi1 == pos ) {
11078: ptomp(mod,(P)ARG2(pi),&inv);
11079: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
11080: u = ptond(CO,vv,(P)ONE);
11081: HCM(u) = ((MQ)inv)->cont;
11082: p[pi0] = u;
11083: }
11084: }
11085: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
11086: printf("%d ",i); fflush(stdout);
11087: ti = BDY((LIST)BDY(t));
1.6 noro 11088: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 11089: if ( Demand ) {
11090: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
11091: }
11092: }
11093: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
11094: printf("%d ",i); fflush(stdout);
11095: ti = BDY((LIST)BDY(t));
1.6 noro 11096: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 11097: if ( Demand ) {
11098: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
11099: }
11100: }
11101: m = length(ind);
11102: MKVECT(vect,m);
11103: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 11104: u = p[ZTOS((Q)BDY(t))];
1.1 noro 11105: if ( !u ) {
1.6 noro 11106: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 11107: BDY(vect)[j] = ndtodp(mod,u);
11108: nd_free(u);
11109: } else
11110: BDY(vect)[j] = ndtodp(mod,u);
11111: }
11112: return vect;
11113: }
11114:
11115: void ndv_print_lf(NDV p)
11116: {
11117: NMV m;
11118: int i,len;
11119:
11120: if ( !p ) printf("0\n");
11121: else {
11122: len = LEN(p);
11123: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
11124: printf("+");
11125: mpz_out_str(asir_out,10,BDY(CZ(m)));
11126: printf("*");
11127: ndl_print(DL(m));
11128: }
11129: printf("\n");
11130: }
11131: }
11132:
11133: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
11134: {
11135: VL tv,fv,vv,vc,av;
11136: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
11137: int m,nocheck,nvar,mindex,e,max;
11138: NDV c;
11139: NMV a;
11140: P p,zp;
11141: Q dmy;
11142: EPOS oepos;
11143: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
11144: Alg alpha,dp;
11145: P poly;
11146: LIST f1,f2,zpl;
11147: Obj obj;
11148: NumberField nf;
11149: struct order_spec *ord1;
11150: struct oEGT eg_check,eg0,eg1;
11151: NODE tr,tl1,tl2,tl3,tl4;
11152: LIST l1,l2,l3,l4,l5;
11153: int *perm;
11154: int j,ret;
11155: NODE retn;
11156: Q jq,bpe;
11157:
11158: nd_module = 0;
11159: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 11160: parse_nd_option(vv,current_option);
1.1 noro 11161: if ( nd_vc )
11162: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
11163: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
11164: switch ( ord->id ) {
11165: case 1:
11166: if ( ord->nv != nvar )
11167: error("nd_f4_lf_trace : invalid order specification");
11168: break;
11169: default:
11170: break;
11171: }
11172:
11173: nd_ntrans = nvar;
11174: nd_nalg = 0;
11175:
11176: nocheck = 0;
11177: mindex = 0;
11178:
11179: /* do not use on-demand load/save */
11180: nd_demand = 0;
11181: m = trace > 1 ? trace : get_lprime(mindex);
11182: nd_init_ord(ord);
11183: mrank = 0;
11184: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
11185: for ( tv = vv; tv; tv = NEXT(tv) ) {
11186: if ( nd_module ) {
11187: s = BDY((LIST)BDY(t));
11188: trank = length(s);
11189: mrank = MAX(mrank,trank);
11190: for ( ; s; s = NEXT(s) ) {
11191: e = getdeg(tv->v,(P)BDY(s));
11192: max = MAX(e,max);
11193: }
11194: } else {
11195: e = getdeg(tv->v,(P)BDY(t));
11196: max = MAX(e,max);
11197: }
11198: }
11199: nd_setup_parameters(nvar,max);
11200: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
11201: ishomo = 1;
11202: /* XXX */
11203: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
11204: if ( nd_module ) {
11205: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
11206: } else {
11207: c = (pointer)ptondv(CO,vv,(P)BDY(t));
11208: }
11209: if ( ishomo )
11210: ishomo = ishomo && ndv_ishomo(c);
11211: if ( c ) {
11212: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
11213: ndv_mod(-2,c);
11214: NEXTNODE(in0,in); BDY(in) = (pointer)c;
11215: }
11216: }
11217: if ( in0 ) NEXT(in) = 0;
11218: if ( fd0 ) NEXT(fd) = 0;
11219: if ( !ishomo && homo ) {
11220: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
11221: c = (NDV)BDY(t); len = LEN(c);
11222: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
11223: wmax = MAX(TD(DL(a)),wmax);
11224: }
11225: homogenize_order(ord,nvar,&ord1);
11226: nd_init_ord(ord1);
11227: nd_setup_parameters(nvar+1,wmax);
11228: for ( t = fd0; t; t = NEXT(t) )
11229: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
11230: }
11231: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 11232: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 11233: if ( ret )
11234: cand = nd_f4_lf_trace_main(m,&perm);
11235: if ( !ret || !cand ) {
11236: *rp = 0; return;
11237: }
11238: if ( !ishomo && homo ) {
11239: /* dehomogenization */
11240: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
11241: nd_init_ord(ord);
11242: nd_setup_parameters(nvar,0);
11243: }
11244: cand = ndv_reducebase(cand,perm);
11245: cand = ndv_reduceall(-2,cand);
11246: cbpe = nd_bpe;
11247: get_eg(&eg0);
11248: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
11249: /* gbcheck : cand is a GB of Id(cand) ? */
11250: retn = nd_f4(-2,0,0);
11251: }
11252: if ( !retn ) {
11253: /* failure */
11254: *rp = 0; return;
11255: }
11256: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
11257: if ( DP_Print )
1.5 noro 11258: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 11259: /* dp->p */
11260: nd_bpe = cbpe;
11261: nd_setup_parameters(nd_nvar,0);
11262: for ( r = cand; r; r = NEXT(r) ) {
11263: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
11264: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
11265: }
11266: MKLIST(*rp,cand);
11267: }
11268:
11269: NODE nd_f4_lf_trace_main(int m,int **indp)
11270: {
11271: int i,nh,stat,index;
11272: NODE r,rm,g;
11273: ND_pairs d,l,l0,t;
11274: ND spol,red;
11275: NDV nf,redv,nfqv,nfv;
11276: NM s0,s;
11277: NODE rp0,srp0,nflist,nflist_lf;
11278: int nsp,nred,col,rank,len,k,j,a;
11279: UINT c;
11280: UINT **spmat;
11281: UINT *s0vect,*svect,*p,*v;
11282: int *colstat;
11283: IndArray *imat;
11284: int *rhead;
11285: int spcol,sprow;
11286: int sugar;
11287: PGeoBucket bucket;
11288: struct oEGT eg0,eg1,eg_f4;
11289:
11290: g = 0; d = 0;
11291: for ( i = 0; i < nd_psn; i++ ) {
11292: d = update_pairs(d,g,i,0);
11293: g = update_base(g,i);
11294: }
11295: while ( d ) {
11296: get_eg(&eg0);
11297: l = nd_minsugarp(d,&d);
11298: sugar = SG(l);
11299: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
11300: bucket = create_pbucket();
11301: stat = nd_sp_f4(m,0,l,bucket);
11302: if ( !stat ) {
11303: for ( t = l; NEXT(t); t = NEXT(t) );
11304: NEXT(t) = d; d = l;
11305: d = nd_reconstruct(1,d);
11306: continue;
11307: }
11308: if ( bucket->m < 0 ) continue;
11309: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
11310: if ( !col ) {
11311: for ( t = l; NEXT(t); t = NEXT(t) );
11312: NEXT(t) = d; d = l;
11313: d = nd_reconstruct(1,d);
11314: continue;
11315: }
11316: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
11317: if ( DP_Print )
1.5 noro 11318: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 11319: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
11320: if ( !l0 ) continue;
11321: l = l0;
11322:
11323: /* over LF */
11324: bucket = create_pbucket();
11325: stat = nd_sp_f4(-2,1,l,bucket);
11326: if ( !stat ) {
11327: for ( t = l; NEXT(t); t = NEXT(t) );
11328: NEXT(t) = d; d = l;
11329: d = nd_reconstruct(1,d);
11330: continue;
11331: }
11332: if ( bucket->m < 0 ) continue;
11333: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
11334: if ( !col ) {
11335: for ( t = l; NEXT(t); t = NEXT(t) );
11336: NEXT(t) = d; d = l;
11337: d = nd_reconstruct(1,d);
11338: continue;
11339: }
11340: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
11341: /* adding new bases */
11342: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
11343: nfv = (NDV)BDY(rm);
11344: nfqv = (NDV)BDY(r);
11345: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
11346: ndv_removecont(m,nfv);
11347: ndv_removecont(-2,nfqv);
1.24 noro 11348: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 11349: d = update_pairs(d,g,nh,0);
11350: g = update_base(g,nh);
11351: }
11352: if ( r || rm ) return 0;
11353: }
11354: conv_ilist(nd_demand,1,g,indp);
11355: return g;
11356: }
11357:
1.7 noro 11358: #if SIZEOF_LONG==8
11359:
11360: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
11361: {
11362: int j,k,len;
11363: UINT *p;
11364: UINT c;
11365: NDV r;
11366: NMV mr0,mr;
11367:
11368: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
11369: if ( !len ) return 0;
11370: else {
11371: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11372: #if 0
11373: ndv_alloc += nmv_adv*len;
11374: #endif
11375: mr = mr0;
11376: p = s0vect;
11377: for ( j = k = 0; j < col; j++, p += nd_wpd )
11378: if ( !rhead[j] ) {
11379: if ( (c = (UINT)vect[k++]) != 0 ) {
11380: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11381: }
11382: }
11383: MKNDV(nd_nvar,mr0,len,r);
11384: return r;
11385: }
11386: }
11387:
1.28 noro 11388: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
11389: {
11390: int j,k,len;
11391: UINT *p;
11392: UINT c;
11393: NDV r;
11394: NMV mr0,mr;
11395:
11396: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
11397: if ( !len ) return 0;
11398: else {
11399: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11400: mr = mr0;
11401: p = s0vect;
11402: for ( j = k = 0; j < col; j++, p += nd_wpd )
11403: if ( (c = (UINT)vect[k++]) != 0 ) {
11404: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11405: }
11406: MKNDV(nd_nvar,mr0,len,r);
11407: return r;
11408: }
11409: }
11410:
1.7 noro 11411: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
11412: {
11413: NM m;
1.11 noro 11414: UINT *t,*s,*u;
11415: int i,st,ed,md,prev,c;
1.7 noro 11416:
11417: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 11418: prev = 0;
11419: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
11420: t = DL(m);
11421: st = prev;
11422: ed = n;
11423: while ( ed > st ) {
11424: md = (st+ed)/2;
11425: u = s0+md*nd_wpd;
11426: c = DL_COMPARE(u,t);
11427: if ( c == 0 ) break;
11428: else if ( c > 0 ) st = md;
11429: else ed = md;
11430: }
11431: r[md] = (mp_limb_t)CM(m);
11432: prev = md;
1.7 noro 11433: }
11434: for ( i = 0; !r[i]; i++ );
11435: return i;
11436: }
11437:
11438: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
11439:
1.28 noro 11440: 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 11441: {
11442: int i,j,k,len,pos,prev;
11443: mp_limb_t a,c,c1,c2;
11444: IndArray ivect;
11445: unsigned char *ivc;
11446: unsigned short *ivs;
11447: unsigned int *ivi;
11448: NDV redv;
11449: NMV mr;
11450: NODE rp;
11451: int maxrs;
11452:
11453: for ( i = 0; i < col; i++ ) cvect[i] = 0;
11454: maxrs = 0;
11455: for ( i = 0; i < nred; i++ ) {
11456: ivect = imat[i];
11457: k = ivect->head;
11458: a = svect[k]; c = cvect[k];
11459: MOD128(a,c,m);
11460: svect[k] = a; cvect[k] = 0;
1.28 noro 11461: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 11462: Nf4_red++;
1.7 noro 11463: maxrs = MAX(maxrs,rp0[i]->sugar);
11464: c = m-c; redv = nd_ps[rp0[i]->index];
11465: len = LEN(redv); mr = BDY(redv);
11466: svect[k] = 0; prev = k;
11467: switch ( ivect->width ) {
11468: case 1:
11469: ivc = ivect->index.c;
11470: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11471: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 11472: c2 = svect[pos]+c1*c;
11473: if ( c2 < svect[pos] ) cvect[pos]++;
11474: svect[pos] = c2;
1.7 noro 11475: }
11476: break;
11477: case 2:
11478: ivs = ivect->index.s;
11479: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11480: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 11481: c2 = svect[pos]+c1*c;
11482: if ( c2 < svect[pos] ) cvect[pos]++;
11483: svect[pos] = c2;
1.7 noro 11484: }
11485: break;
11486: case 4:
11487: ivi = ivect->index.i;
11488: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11489: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 11490: c2 = svect[pos]+c1*c;
11491: if ( c2 < svect[pos] ) cvect[pos]++;
11492: svect[pos] = c2;
1.7 noro 11493: }
11494: break;
11495: }
11496: }
11497: }
11498: for ( i = 0; i < col; i++ ) {
11499: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
11500: }
11501: return maxrs;
11502: }
11503:
11504: /* for Fp, 2^15=<p<2^29 */
11505:
11506: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11507: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
11508: {
11509: int spcol,sprow,a;
11510: int i,j,k,l,rank;
11511: NODE r0,r;
11512: ND_pairs sp;
11513: ND spol;
11514: mp_limb_t **spmat;
11515: mp_limb_t *svect,*cvect;
11516: mp_limb_t *v;
11517: int *colstat;
11518: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11519: int maxrs;
11520: int *spsugar;
11521: ND_pairs *spactive;
11522:
11523: spcol = col-nred;
11524: get_eg(&eg0);
11525: /* elimination (1st step) */
11526: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11527: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11528: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11529: spsugar = (int *)MALLOC(nsp*sizeof(int));
11530: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
11531: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11532: nd_sp(m,0,sp,&spol);
11533: if ( !spol ) continue;
11534: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 11535: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 11536: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11537: if ( i < col ) {
11538: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
11539: for ( j = k = 0; j < col; j++ )
11540: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
11541: spsugar[sprow] = MAX(maxrs,SG(spol));
11542: if ( nz )
11543: spactive[sprow] = sp;
11544: sprow++;
11545: }
11546: nd_free(spol);
11547: }
1.12 noro 11548: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 11549: if ( DP_Print ) {
11550: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11551: fflush(asir_out);
11552: }
11553: /* free index arrays */
11554: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11555:
11556: /* elimination (2nd step) */
11557: colstat = (int *)MALLOC(spcol*sizeof(int));
11558: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
11559: r0 = 0;
11560: for ( i = 0; i < rank; i++ ) {
11561: NEXTNODE(r0,r); BDY(r) =
11562: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
11563: SG((NDV)BDY(r)) = spsugar[i];
11564: GCFREE(spmat[i]);
11565: }
11566: if ( r0 ) NEXT(r) = 0;
11567:
11568: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 11569: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 11570: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11571: if ( DP_Print ) {
11572: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11573: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11574: nsp,nred,sprow,spcol,rank);
11575: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11576: }
11577: if ( nz ) {
11578: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
11579: if ( rank > 0 ) {
11580: NEXT(spactive[rank-1]) = 0;
11581: *nz = spactive[0];
11582: } else
11583: *nz = 0;
11584: }
11585: return r0;
11586: }
11587:
11588: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
11589: {
11590: int i,j,k,l,rank,s;
11591: mp_limb_t inv;
11592: mp_limb_t a;
11593: UINT c;
11594: mp_limb_t *t,*pivot,*pk;
11595: UINT *ck;
11596: UINT **cmat;
11597: UINT *ct;
11598: ND_pairs pair;
11599:
11600: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11601: for ( i = 0; i < row; i++ ) {
11602: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11603: bzero(cmat[i],col*sizeof(UINT));
11604: }
11605:
11606: for ( rank = 0, j = 0; j < col; j++ ) {
11607: for ( i = rank; i < row; i++ ) {
11608: a = mat[i][j]; c = cmat[i][j];
11609: MOD128(a,c,md);
11610: mat[i][j] = a; cmat[i][j] = 0;
11611: }
11612: for ( i = rank; i < row; i++ )
11613: if ( mat[i][j] )
11614: break;
11615: if ( i == row ) {
11616: colstat[j] = 0;
11617: continue;
11618: } else
11619: colstat[j] = 1;
11620: if ( i != rank ) {
11621: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
11622: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
11623: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
11624: if ( spactive ) {
11625: pair = spactive[i]; spactive[i] = spactive[rank];
11626: spactive[rank] = pair;
11627: }
11628: }
11629: /* column j is normalized */
11630: s = sugar[rank];
11631: inv = invm((UINT)mat[rank][j],md);
11632: /* normalize pivot row */
11633: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
11634: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11635: }
11636: for ( i = rank+1; i < row; i++ ) {
11637: if ( (a = mat[i][j]) != 0 ) {
11638: sugar[i] = MAX(sugar[i],s);
11639: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 11640: Nf4_red++;
1.7 noro 11641: }
11642: }
11643: rank++;
11644: }
11645: for ( j = col-1, l = rank-1; j >= 0; j-- )
11646: if ( colstat[j] ) {
11647: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
11648: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
11649: }
11650: s = sugar[l];
11651: for ( i = 0; i < l; i++ ) {
11652: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
11653: if ( a ) {
11654: sugar[i] = MAX(sugar[i],s);
11655: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 11656: Nf4_red++;
1.7 noro 11657: }
11658: }
11659: l--;
11660: }
11661: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11662: GCFREE(cmat);
11663: return rank;
11664: }
11665:
1.28 noro 11666: 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)
11667: {
11668: int i,j,k,l,rank,s,imin;
11669: mp_limb_t inv;
11670: mp_limb_t a;
11671: UINT c;
11672: mp_limb_t *t,*pivot,*pk;
11673: UINT *ck;
11674: UINT **cmat;
11675: UINT *ct;
11676: ND_pairs pair;
11677: SIG sg;
1.31 noro 11678: int *used;
1.28 noro 11679:
1.31 noro 11680: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 11681: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11682: for ( i = 0; i < row; i++ ) {
11683: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11684: bzero(cmat[i],col*sizeof(UINT));
11685: }
11686:
1.31 noro 11687: for ( j = 0; j < col; j++ ) {
11688: for ( i = 0; i < row; i++ ) {
1.28 noro 11689: a = mat[i][j]; c = cmat[i][j];
11690: MOD128(a,c,md);
11691: mat[i][j] = a; cmat[i][j] = 0;
11692: }
1.31 noro 11693: for ( i = 0; i < row; i++ )
11694: if ( !used[i] && mat[i][j] ) break;
11695: if ( i == row ) {
1.28 noro 11696: colstat[j] = 0;
11697: continue;
1.31 noro 11698: } else {
1.28 noro 11699: colstat[j] = 1;
1.31 noro 11700: used[i] = 1;
1.28 noro 11701: }
11702: /* column j is normalized */
1.31 noro 11703: s = sugar[i];
11704: inv = invm((UINT)mat[i][j],md);
1.28 noro 11705: /* normalize pivot row */
1.31 noro 11706: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 11707: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11708: }
1.31 noro 11709: for ( k = i+1; k < row; k++ ) {
11710: if ( (a = mat[k][j]) != 0 ) {
11711: sugar[k] = MAX(sugar[k],s);
11712: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11713: Nf4_red++;
11714: }
11715: }
11716: }
1.31 noro 11717: rank = 0;
11718: for ( i = 0; i < row; i++ ) {
11719: for ( j = 0; j < col; j++ )
11720: if ( mat[i][j] ) break;
11721: if ( j == col ) sugar[i] = -1;
11722: else rank++;
11723: }
1.28 noro 11724: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11725: GCFREE(cmat);
11726: return rank;
11727: }
11728:
11729: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11730: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11731: {
11732: int spcol,sprow,a;
11733: int i,j,k,l,rank;
11734: NODE r0,r;
11735: ND_pairs sp;
11736: ND spol;
11737: mp_limb_t **spmat;
11738: mp_limb_t *svect,*cvect;
11739: mp_limb_t *v;
11740: int *colstat;
11741: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11742: int maxrs;
11743: int *spsugar;
11744: ND_pairs *spactive;
11745: SIG *spsig;
11746:
11747: get_eg(&eg0);
11748: /* elimination (1st step) */
11749: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11750: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11751: spsugar = (int *)MALLOC(nsp*sizeof(int));
11752: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11753: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11754: nd_sp(m,0,sp,&spol);
11755: if ( !spol ) {
1.29 noro 11756: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11757: continue;
11758: }
11759: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11760: nd_to_vect64(m,s0vect,col,spol,svect);
11761: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11762: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11763: if ( i < col ) {
11764: spmat[sprow] = svect;
11765: spsugar[sprow] = MAX(maxrs,SG(spol));
11766: spsig[sprow] = sp->sig;
11767: sprow++;
11768: } else {
1.29 noro 11769: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11770: }
11771: nd_free(spol);
11772: }
11773: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11774: if ( DP_Print ) {
11775: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11776: fflush(asir_out);
11777: }
11778: /* free index arrays */
11779: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11780:
11781: /* elimination (2nd step) */
11782: colstat = (int *)MALLOC(col*sizeof(int));
11783: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11784: r0 = 0;
1.31 noro 11785: for ( i = 0; i < sprow; i++ ) {
11786: if ( spsugar[i] >= 0 ) {
11787: NEXTNODE(r0,r);
11788: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11789: SG((NDV)BDY(r)) = spsugar[i];
11790: ((NDV)BDY(r))->sig = spsig[i];
11791: } else
11792: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11793: GCFREE(spmat[i]);
11794: }
11795: if ( r0 ) NEXT(r) = 0;
11796: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11797: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11798: if ( DP_Print ) {
11799: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11800: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11801: nsp,nred,sprow,col,rank);
11802: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11803: }
11804: return r0;
11805: }
1.40 noro 11806: #endif
1.28 noro 11807:
11808: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11809: {
11810: IndArray *imat;
11811: int nsp,nred,i,start;
11812: int *rhead;
11813: NODE r0,rp;
11814: ND_pairs sp;
11815: NM_ind_pair *rvect;
11816: UINT *s;
11817: int *s0hash;
11818: struct oEGT eg0,eg1,eg_conv;
11819:
11820: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11821: nred = length(rp0);
11822: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11823: rhead = (int *)MALLOC(col*sizeof(int));
11824: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11825:
11826: /* construction of index arrays */
11827: get_eg(&eg0);
11828: if ( DP_Print ) {
11829: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11830: fflush(asir_out);
11831: }
11832: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11833: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11834: rvect[i] = (NM_ind_pair)BDY(rp);
11835: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11836: rhead[imat[i]->head] = 1;
11837: start = imat[i]->head;
11838: }
11839: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11840: if ( DP_Print ) {
11841: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11842: fflush(asir_out);
11843: }
11844: if ( m > 0 )
1.40 noro 11845: #if SIZEOF_LONG==8
1.28 noro 11846: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11847: #else
11848: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11849: #endif
1.28 noro 11850: else
11851: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11852: error("nd_f4_red_q_main_s : not implemented yet");
11853: return r0;
11854: }
11855:
11856: INLINE int ndl_find_reducer_minsig(UINT *dg)
11857: {
11858: RHist r;
11859: int i,singular,ret,d,k,imin;
11860: SIG t;
11861: static int wpd,nvar;
11862: static SIG quo,quomin;
11863: static UINT *tmp;
11864:
11865: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11866: if ( wpd != nd_wpd ) {
11867: wpd = nd_wpd;
11868: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11869: }
11870: #if 0
11871: d = ndl_hash_value(dg);
11872: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11873: if ( ndl_equal(dg,DL(r)) ) {
11874: return r->index;
11875: }
11876: }
11877: #endif
11878: imin = -1;
11879: for ( i = 0; i < nd_psn; i++ ) {
11880: r = nd_psh[i];
11881: if ( ndl_reducible(dg,DL(r)) ) {
11882: ndl_sub(dg,DL(r),tmp);
11883: _ndltodl(tmp,DL(quo));
11884: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11885: quo->pos = nd_psh[i]->sig->pos;
11886: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11887: t = quo; quo = quomin; quomin = t;
11888: imin = i;
11889: }
11890: }
11891: }
11892: if ( imin == -1 ) return nd_psn;
11893: else {
11894: #if 0
11895: nd_append_red(dg,i);
11896: #endif
11897: return imin;
11898: }
11899: }
11900:
11901: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11902: {
11903: NODE rp0,rp;
11904: NM mul,head,s0,s;
11905: int index,col,i,sugar;
11906: RHist h;
11907: UINT *s0v,*p;
11908: NM_ind_pair pair;
11909: ND red;
11910: NDV *ps;
11911: SIG sig;
11912:
11913: s0 = 0; rp0 = 0; col = 0;
11914: if ( nd_demand )
11915: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11916: else
11917: ps = trace?nd_ps_trace:nd_ps;
11918: while ( 1 ) {
11919: head = remove_head_pbucket_symbolic(bucket);
11920: if ( !head ) break;
11921: if ( !s0 ) s0 = head;
11922: else NEXT(s) = head;
11923: s = head;
11924: index = ndl_find_reducer_minsig(DL(head));
11925: if ( index >= 0 && index < nd_psn ) {
11926: h = nd_psh[index];
11927: NEWNM(mul);
11928: ndl_sub(DL(head),DL(h),DL(mul));
11929: if ( ndl_check_bound2(index,DL(mul)) )
11930: return 0;
11931: sugar = TD(DL(mul))+SG(ps[index]);
11932: NEWSIG(sig);
11933: _ndltodl(DL(mul),DL(sig));
11934: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11935: sig->pos = nd_psh[index]->sig->pos;
11936: MKNM_ind_pair(pair,mul,index,sugar,sig);
11937: red = ndv_mul_nm_symbolic(mul,ps[index]);
11938: add_pbucket_symbolic(bucket,nd_remove_head(red));
11939: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11940: }
11941: col++;
11942: }
11943: if ( rp0 ) NEXT(rp) = 0;
11944: NEXT(s) = 0;
11945: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11946: for ( i = 0, p = s0v, s = s0; i < col;
11947: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11948: *s0vect = s0v;
11949: *r = rp0;
11950:
11951: return col;
11952: }
11953:
11954: NODE nd_sba_f4(int m,int **indp)
11955: {
1.34 noro 11956: int i,nh,stat,index,f4red,f4step;
1.33 noro 11957: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11958: NODE r,g,rp0,nflist;
1.41 noro 11959: ND_pairs d,l,t,l1;
1.33 noro 11960: ND h,nf;
11961: NDV nfv;
11962: union oNDC hc;
11963: UINT *s0vect;
1.28 noro 11964: UINT c;
11965: PGeoBucket bucket;
1.33 noro 11966: NODE *syzlist;
11967: SIG sig;
1.28 noro 11968: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11969: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11970:
11971: Nf4_red=0;
1.30 noro 11972: d = 0;
1.29 noro 11973: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11974: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11975: d = update_pairs_s(d,i,syzlist);
1.28 noro 11976: }
1.30 noro 11977: nd_nbase = nd_psn;
1.28 noro 11978: f4red = 1;
1.33 noro 11979: psugar = 0;
1.34 noro 11980: f4step = 0;
1.28 noro 11981: while ( d ) {
1.33 noro 11982: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11983: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11984: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11985: again:
11986: l = d; d = d->next;
1.41 noro 11987: #if 0
1.33 noro 11988: if ( small_lcm(l) ) {
11989: if ( DP_Print ) fprintf(asir_out,"M");
11990: continue;
11991: }
11992: sig = l->sig;
11993: stat = nd_sp(m,0,l,&h);
1.41 noro 11994: #else
11995: l1 = find_smallest_lcm(l);
11996: if ( l1 == 0 ) {
11997: if ( DP_Print ) fprintf(asir_out,"M");
11998: continue;
11999: }
12000: sig = l1->sig;
12001: stat = nd_sp(m,0,l1,&h);
12002: #endif
1.33 noro 12003: if ( !stat ) {
12004: NEXT(l) = d; d = l;
12005: d = nd_reconstruct(0,d);
12006: goto again;
12007: }
12008: get_eg(&eg1);
12009: #if USE_GEOBUCKET
1.39 noro 12010: 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 12011: #else
1.39 noro 12012: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 12013: #endif
12014: get_eg(&eg2);
12015: if ( !stat ) {
12016: NEXT(l) = d; d = l;
12017: d = nd_reconstruct(0,d);
12018: goto again;
12019: } else if ( stat == -1 ) {
12020: if ( DP_Print ) { printf("S"); fflush(stdout); }
12021: FREENDP(l);
12022: } else if ( nf ) {
12023: if ( DP_Print ) { printf("+"); fflush(stdout); }
12024: add_eg(&eg_nf,&eg1,&eg2);
12025: hc = HCU(nf);
12026: nd_removecont(m,nf);
12027: nfv = ndtondv(m,nf); nd_free(nf);
12028: nh = ndv_newps(m,nfv,0);
12029:
1.30 noro 12030: d = update_pairs_s(d,nh,syzlist);
12031: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 12032: FREENDP(l);
1.28 noro 12033: } else {
1.33 noro 12034: add_eg(&eg_nfzero,&eg1,&eg2);
12035: // syzygy
12036: get_eg(&eg1);
12037: d = remove_spair_s(d,sig);
12038: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 12039: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 12040: if ( DP_Print ) { printf("."); fflush(stdout); }
12041: FREENDP(l);
12042: }
12043: } else {
1.34 noro 12044: if ( ms != psugar ) f4step = 1;
12045: else f4step++;
1.33 noro 12046: again2:
12047: psugar = ms;
12048: l = nd_minsugarp_s(d,&d);
12049: sugar = nd_sugarweight?d->sugar2:SG(d);
12050: bucket = create_pbucket();
12051: stat = nd_sp_f4(m,0,l,bucket);
12052: if ( !stat ) {
12053: for ( t = l; NEXT(t); t = NEXT(t) );
12054: NEXT(t) = d; d = l;
12055: d = nd_reconstruct(0,d);
12056: goto again2;
12057: }
12058: if ( bucket->m < 0 ) continue;
12059: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
12060: if ( !col ) {
1.37 noro 12061: for ( t = l; NEXT(t); t = NEXT(t) )
12062: ;
12063: NEXT(t) = d; d = l;
1.33 noro 12064: d = nd_reconstruct(0,d);
12065: goto again2;
12066: }
1.34 noro 12067: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 12068: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
12069: /* adding new bases */
12070: for ( r = nflist; r; r = NEXT(r) ) {
12071: nfv = (NDV)BDY(r);
1.38 noro 12072: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 12073: ndv_removecont(m,nfv);
12074: nh = ndv_newps(m,nfv,0);
12075: d = update_pairs_s(d,nh,syzlist);
12076: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
12077: }
12078: for ( i = 0; i < nd_nbase; i++ )
12079: for ( r = syzlist[i]; r; r = NEXT(r) )
12080: d = remove_spair_s(d,(SIG)BDY(r));
12081: d = remove_large_lcm(d);
12082: if ( DP_Print ) {
1.34 noro 12083: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 12084: }
12085: f4red++;
1.28 noro 12086: }
12087: }
12088: if ( DP_Print ) {
1.34 noro 12089: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 12090: }
1.30 noro 12091: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 12092: return g;
12093: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>