Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.48
1.48 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.47 2021/02/01 08:06:33 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.45 noro 5: void print_siglist(NODE l);
6:
1.47 noro 7: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs,Nnewpair;
1.12 noro 8: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 9:
10: int diag_period = 6;
11: int weight_check = 1;
12: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: /* for general module order */
1.19 noro 14: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 15: int (*dl_base_compare_function)(int nv,DL a,DL b);
16: int nd_base_ordtype;
1.1 noro 17: int nd_dcomp;
18: int nd_rref2;
19: NM _nm_free_list;
20: ND _nd_free_list;
21: ND_pairs _ndp_free_list;
22: NODE nd_hcf;
1.35 noro 23: int Nsyz,Nsamesig;
1.1 noro 24:
25: Obj nd_top_weight;
26:
27: static NODE nd_subst;
28: static VL nd_vc;
29: static int nd_ntrans;
30: static int nd_nalg;
31: #if 0
32: static int ndv_alloc;
33: #endif
34: #if 1
35: static int nd_f4_nsp=0x7fffffff;
36: #else
37: static int nd_f4_nsp=50;
38: #endif
39: static double nd_scale=2;
40: static UINT **nd_bound;
41: static struct order_spec *nd_ord;
42: static EPOS nd_epos;
43: static BlockMask nd_blockmask;
44: static int nd_nvar;
45: static int nd_isrlex;
46: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
47: static UINT nd_mask[32];
48: static UINT nd_mask0,nd_mask1;
49:
50: static NDV *nd_ps;
51: static NDV *nd_ps_trace;
52: static NDV *nd_ps_sym;
53: static NDV *nd_ps_trace_sym;
54: static RHist *nd_psh;
1.30 noro 55: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 56: static RHist *nd_red;
57: static int *nd_work_vector;
58: static int **nd_matrix;
59: static int nd_matrix_len;
60: static struct weight_or_block *nd_worb;
61: static int nd_worb_len;
62: static int nd_found,nd_create,nd_notfirst;
63: static int nmv_adv;
64: static int nd_demand;
1.21 noro 65: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 66: static int nd_module_rank,nd_poly_weight_len;
67: static int *nd_poly_weight,*nd_module_weight;
68: static NODE nd_tracelist;
69: static NODE nd_alltracelist;
1.41 noro 70: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
71: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.46 noro 72: static int nd_top,nd_sba_syz,nd_sba_inputisgb;
1.1 noro 73: static int *nd_gbblock;
74: static NODE nd_nzlist,nd_check_splist;
75: static int nd_splist;
76: static int *nd_sugarweight;
77: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 78: static DL *nd_sba_hm;
1.29 noro 79: static NODE *nd_sba_pos;
1.1 noro 80:
1.46 noro 81: struct comp_sig_spec {
82: int n;
83: // current_i <-> oldv[i]
84: int *oldv;
85: int *weight;
86: struct order_pair *order_pair;
87: int block_length;
88: int **matrix;
89: int row;
90: int (*cmpdl)(int n,DL d1,DL d2);
91: };
92:
93: struct comp_sig_spec *nd_sba_modord;
94:
1.1 noro 95: NumberField get_numberfield();
96: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
97: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
98: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
99: int nd_monic(int m,ND *p);
100: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
101: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
102: NDV pltondv(VL vl,VL dvl,LIST p);
103: void pltozpl(LIST l,Q *cont,LIST *pp);
104: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
105: void nmtodp(int mod,NM m,DP *r);
1.15 noro 106: void ndltodp(UINT *d,DP *r);
1.1 noro 107: NODE reverse_node(NODE n);
108: P ndc_div(int mod,union oNDC a,union oNDC b);
109: P ndctop(int mod,union oNDC c);
110: void finalize_tracelist(int i,P cont);
111: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.46 noro 112: void parse_nd_option(VL vl,NODE opt);
1.1 noro 113: void dltondl(int n,DL dl,UINT *r);
114: DP ndvtodp(int mod,NDV p);
115: DP ndtodp(int mod,ND p);
1.16 noro 116: DPM ndvtodpm(int mod,NDV p);
1.43 noro 117: NDV dptondv(int mod,DP p);
1.16 noro 118: NDV dpmtondv(int mod,DPM p);
1.43 noro 119: int dp_getdeg(DP p);
1.16 noro 120: int dpm_getdeg(DPM p,int *rank);
121: void dpm_ptozp(DPM p,Z *cont,DPM *r);
122: int compdmm(int nv,DMM a,DMM b);
1.44 noro 123: DPM sigtodpm(SIG s);
124: SIG dup_sig(SIG sig);
1.1 noro 125:
126: void Pdp_set_weight(NODE,VECT *);
127: void Pox_cmo_rpc(NODE,Obj *);
128:
129: ND nd_add_lf(ND p1,ND p2);
130: void nd_mul_c_lf(ND p,Z mul);
131: void ndv_mul_c_lf(NDV p,Z mul);
132: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
133: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
134: NODE nd_f4_red_mod64_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_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
137: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
138: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 139: 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 140: NODE nd_f4_lf_trace_main(int m,int **indp);
141: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
142:
143: extern int lf_lazy;
144: extern Z current_mod_lf;
145:
146: extern int Denominator,DP_Multiple,MaxDeg;
147:
148: #define BLEN (8*sizeof(unsigned long))
149:
150: typedef struct matrix {
151: int row,col;
152: unsigned long **a;
153: } *matrix;
154:
155:
156: void nd_free_private_storage()
157: {
158: _nm_free_list = 0;
159: _ndp_free_list = 0;
160: #if 0
161: GC_gcollect();
162: #endif
163: }
164:
165: void _NM_alloc()
166: {
167: NM p;
168: int i;
169:
170: for ( i = 0; i < 1024; i++ ) {
171: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
172: p->next = _nm_free_list; _nm_free_list = p;
173: }
174: }
175:
176: matrix alloc_matrix(int row,int col)
177: {
178: unsigned long **a;
179: int i,len,blen;
180: matrix mat;
181:
182: mat = (matrix)MALLOC(sizeof(struct matrix));
183: mat->row = row;
184: mat->col = col;
185: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
186: return mat;
187: }
188:
189:
190: void _ND_alloc()
191: {
192: ND p;
193: int i;
194:
195: for ( i = 0; i < 1024; i++ ) {
196: p = (ND)MALLOC(sizeof(struct oND));
197: p->body = (NM)_nd_free_list; _nd_free_list = p;
198: }
199: }
200:
201: void _NDP_alloc()
202: {
203: ND_pairs p;
204: int i;
205:
206: for ( i = 0; i < 1024; i++ ) {
207: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
208: +(nd_wpd-1)*sizeof(UINT));
209: p->next = _ndp_free_list; _ndp_free_list = p;
210: }
211: }
212:
213: INLINE int nd_length(ND p)
214: {
215: NM m;
216: int i;
217:
218: if ( !p )
219: return 0;
220: else {
221: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
222: return i;
223: }
224: }
225:
226: extern int dp_negative_weight;
227:
228: INLINE int ndl_reducible(UINT *d1,UINT *d2)
229: {
230: UINT u1,u2;
231: int i,j;
232:
233: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
234:
235: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
236: #if USE_UNROLL
237: switch ( nd_bpe ) {
238: case 3:
239: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
240: u1 = d1[i]; u2 = d2[i];
241: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
242: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
243: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
244: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
245: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
246: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
247: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
248: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
249: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
250: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
251: }
252: return 1;
253: break;
254: case 4:
255: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
256: u1 = d1[i]; u2 = d2[i];
257: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
258: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
259: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
260: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
261: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
262: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
263: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
264: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
265: }
266: return 1;
267: break;
268: case 6:
269: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
270: u1 = d1[i]; u2 = d2[i];
271: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
272: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
273: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
274: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
275: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
276: }
277: return 1;
278: break;
279: case 8:
280: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
281: u1 = d1[i]; u2 = d2[i];
282: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
283: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
284: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
285: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
286: }
287: return 1;
288: break;
289: case 16:
290: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
291: u1 = d1[i]; u2 = d2[i];
292: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
293: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
294: }
295: return 1;
296: break;
297: case 32:
298: for ( i = nd_exporigin; i < nd_wpd; i++ )
299: if ( d1[i] < d2[i] ) return 0;
300: return 1;
301: break;
302: default:
303: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
304: u1 = d1[i]; u2 = d2[i];
305: for ( j = 0; j < nd_epw; j++ )
306: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
307: }
308: return 1;
309: }
310: #else
311: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
312: u1 = d1[i]; u2 = d2[i];
313: for ( j = 0; j < nd_epw; j++ )
314: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
315: }
316: return 1;
317: #endif
318: }
319:
320: /*
321: * If the current order is a block order,
322: * then the last block is length 1 and contains
323: * the homo variable. Otherwise, the original
324: * order is either 0 or 2.
325: */
326:
327: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
328: {
329: int w,i,e,n,omask0;
330:
331: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
332: n = nd_nvar-1;
333: ndl_zero(r);
334: for ( i = 0; i < n; i++ ) {
335: e = GET_EXP_OLD(d,i);
336: PUT_EXP(r,i,e);
337: }
338: w = TD(d);
339: PUT_EXP(r,nd_nvar-1,weight-w);
340: if ( nd_module ) MPOS(r) = d[ompos];
341: TD(r) = weight;
342: if ( nd_blockmask ) ndl_weight_mask(r);
343: }
344:
345: void ndl_dehomogenize(UINT *d)
346: {
347: UINT mask;
348: UINT h;
349: int i,bits;
350:
351: if ( nd_blockmask ) {
352: h = GET_EXP(d,nd_nvar-1);
353: XOR_EXP(d,nd_nvar-1,h);
354: TD(d) -= h;
355: ndl_weight_mask(d);
356: } else {
357: if ( nd_isrlex ) {
358: if ( nd_bpe == 32 ) {
359: h = d[nd_exporigin];
360: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
361: d[i-1] = d[i];
362: d[i-1] = 0;
363: TD(d) -= h;
364: } else {
365: bits = nd_epw*nd_bpe;
366: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
367: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
368: for ( i = nd_exporigin; i < nd_wpd; i++ )
369: d[i] = ((d[i]<<nd_bpe)&mask)
370: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
371: TD(d) -= h;
372: }
373: } else {
374: h = GET_EXP(d,nd_nvar-1);
375: XOR_EXP(d,nd_nvar-1,h);
376: TD(d) -= h;
377: }
378: }
379: }
380:
381: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
382: {
383: UINT t1,t2,u,u1,u2;
384: int i,j,l;
385:
386: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
387: error("ndl_lcm : inconsistent monomials");
388: #if USE_UNROLL
389: switch ( nd_bpe ) {
390: case 3:
391: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
392: u1 = d1[i]; u2 = d2[i];
393: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
394: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
395: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
396: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
397: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
398: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
400: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
401: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
403: d[i] = u;
404: }
405: break;
406: case 4:
407: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
408: u1 = d1[i]; u2 = d2[i];
409: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
410: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
411: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
412: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
414: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
415: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
416: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
417: d[i] = u;
418: }
419: break;
420: case 6:
421: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
422: u1 = d1[i]; u2 = d2[i];
423: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
424: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
425: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
426: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
427: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
428: d[i] = u;
429: }
430: break;
431: case 8:
432: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
433: u1 = d1[i]; u2 = d2[i];
434: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
435: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
436: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
437: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
438: d[i] = u;
439: }
440: break;
441: case 16:
442: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
443: u1 = d1[i]; u2 = d2[i];
444: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
445: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
446: d[i] = u;
447: }
448: break;
449: case 32:
450: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
451: u1 = d1[i]; u2 = d2[i];
452: d[i] = u1>u2?u1:u2;
453: }
454: break;
455: default:
456: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
457: u1 = d1[i]; u2 = d2[i];
458: for ( j = 0, u = 0; j < nd_epw; j++ ) {
459: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
460: }
461: d[i] = u;
462: }
463: break;
464: }
465: #else
466: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
467: u1 = d1[i]; u2 = d2[i];
468: for ( j = 0, u = 0; j < nd_epw; j++ ) {
469: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
470: }
471: d[i] = u;
472: }
473: #endif
474: if ( nd_module ) MPOS(d) = MPOS(d1);
475: TD(d) = ndl_weight(d);
476: if ( nd_blockmask ) ndl_weight_mask(d);
477: }
478:
479: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
480: {
481: UINT t1,t2,u,u1,u2;
482: int i,j,l;
483:
484: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
485: u1 = d1[i]; u2 = d2[i];
486: for ( j = 0, u = 0; j < nd_epw; j++ ) {
487: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
488: }
489: d[i] = u;
490: }
491: }
492:
493: int ndl_weight(UINT *d)
494: {
495: UINT t,u;
496: int i,j;
497:
498: if ( current_dl_weight_vector )
499: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
500: u = GET_EXP(d,i);
501: t += MUL_WEIGHT(u,i);
502: }
503: else
504: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
505: u = d[i];
506: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
507: t += (u&nd_mask0);
508: }
1.20 noro 509: if ( nd_module && nd_module_rank && MPOS(d) )
510: t += nd_module_weight[MPOS(d)-1];
511: for ( i = nd_exporigin; i < nd_wpd; i++ )
512: if ( d[i] && !t )
513: printf("afo\n");
1.1 noro 514: return t;
515: }
516:
517: /* for sugarweight */
518:
519: int ndl_weight2(UINT *d)
520: {
521: int t,u;
522: int i,j;
523:
524: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
525: u = GET_EXP(d,i);
526: t += nd_sugarweight[i]*u;
527: }
1.20 noro 528: if ( nd_module && nd_module_rank && MPOS(d) )
529: t += nd_module_weight[MPOS(d)-1];
1.1 noro 530: return t;
531: }
532:
533: void ndl_weight_mask(UINT *d)
534: {
535: UINT t,u;
536: UINT *mask;
537: int i,j,k,l;
538:
539: l = nd_blockmask->n;
540: for ( k = 0; k < l; k++ ) {
541: mask = nd_blockmask->mask[k];
542: if ( current_dl_weight_vector )
543: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
544: u = GET_EXP_MASK(d,i,mask);
545: t += MUL_WEIGHT(u,i);
546: }
547: else
548: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
549: u = d[i]&mask[i];
550: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
551: t += (u&nd_mask0);
552: }
553: d[k+1] = t;
554: }
555: }
556:
1.21 noro 557: int ndl_glex_compare(UINT *d1,UINT *d2)
558: {
559: if ( TD(d1) > TD(d2) ) return 1;
560: else if ( TD(d1) < TD(d2) ) return -1;
561: else return ndl_lex_compare(d1,d2);
562: }
563:
1.1 noro 564: int ndl_lex_compare(UINT *d1,UINT *d2)
565: {
566: int i;
567:
568: d1 += nd_exporigin;
569: d2 += nd_exporigin;
570: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
571: if ( *d1 > *d2 )
572: return nd_isrlex ? -1 : 1;
573: else if ( *d1 < *d2 )
574: return nd_isrlex ? 1 : -1;
575: return 0;
576: }
577:
578: int ndl_block_compare(UINT *d1,UINT *d2)
579: {
580: int i,l,j,ord_o,ord_l;
581: struct order_pair *op;
582: UINT t1,t2,m;
583: UINT *mask;
584:
585: l = nd_blockmask->n;
586: op = nd_blockmask->order_pair;
587: for ( j = 0; j < l; j++ ) {
588: mask = nd_blockmask->mask[j];
589: ord_o = op[j].order;
590: if ( ord_o < 2 ) {
591: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
592: else if ( t1 < t2 ) return -1;
593: }
594: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
595: m = mask[i];
596: t1 = d1[i]&m;
597: t2 = d2[i]&m;
598: if ( t1 > t2 )
599: return !ord_o ? -1 : 1;
600: else if ( t1 < t2 )
601: return !ord_o ? 1 : -1;
602: }
603: }
604: return 0;
605: }
606:
607: int ndl_matrix_compare(UINT *d1,UINT *d2)
608: {
609: int i,j,s,row;
610: int *v;
611: Z **mat;
612: Z *w;
613: Z t1;
614: Z t,t2;
615:
1.6 noro 616: for ( j = 0; j < nd_nvar; j++ )
617: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 618: if ( nd_top_weight ) {
619: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 620: mat = (Z **)&BDY((VECT)nd_top_weight);
621: row = 1;
1.1 noro 622: } else {
623: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 624: row = ((MAT)nd_top_weight)->row;
1.1 noro 625: }
626: for ( i = 0; i < row; i++ ) {
1.6 noro 627: w = mat[i];
1.1 noro 628: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 629: STOZ(nd_work_vector[j],t1);
1.1 noro 630: mulz(w[j],t1,&t2);
631: addz(t,t2,&t1);
632: t = t1;
633: }
1.6 noro 634: if ( t ) {
635: s = sgnz(t);
1.1 noro 636: if ( s > 0 ) return 1;
637: else if ( s < 0 ) return -1;
1.6 noro 638: }
1.1 noro 639: }
1.6 noro 640: }
641: for ( i = 0; i < nd_matrix_len; i++ ) {
642: v = nd_matrix[i];
643: for ( j = 0, s = 0; j < nd_nvar; j++ )
644: s += v[j]*nd_work_vector[j];
645: if ( s > 0 ) return 1;
646: else if ( s < 0 ) return -1;
647: }
1.1 noro 648: if ( !ndl_equal(d1,d2) )
1.6 noro 649: error("ndl_matrix_compare : invalid matrix");
650: return 0;
1.1 noro 651: }
652:
653: int ndl_composite_compare(UINT *d1,UINT *d2)
654: {
655: int i,j,s,start,end,len,o;
656: int *v;
657: struct sparse_weight *sw;
658:
659: for ( j = 0; j < nd_nvar; j++ )
660: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
661: for ( i = 0; i < nd_worb_len; i++ ) {
662: len = nd_worb[i].length;
663: switch ( nd_worb[i].type ) {
664: case IS_DENSE_WEIGHT:
665: v = nd_worb[i].body.dense_weight;
666: for ( j = 0, s = 0; j < len; j++ )
667: s += v[j]*nd_work_vector[j];
668: if ( s > 0 ) return 1;
669: else if ( s < 0 ) return -1;
670: break;
671: case IS_SPARSE_WEIGHT:
672: sw = nd_worb[i].body.sparse_weight;
673: for ( j = 0, s = 0; j < len; j++ )
674: s += sw[j].value*nd_work_vector[sw[j].pos];
675: if ( s > 0 ) return 1;
676: else if ( s < 0 ) return -1;
677: break;
678: case IS_BLOCK:
679: o = nd_worb[i].body.block.order;
680: start = nd_worb[i].body.block.start;
681: switch ( o ) {
682: case 0:
683: end = start+len;
684: for ( j = start, s = 0; j < end; j++ )
685: s += MUL_WEIGHT(nd_work_vector[j],j);
686: if ( s > 0 ) return 1;
687: else if ( s < 0 ) return -1;
688: for ( j = end-1; j >= start; j-- )
689: if ( nd_work_vector[j] < 0 ) return 1;
690: else if ( nd_work_vector[j] > 0 ) return -1;
691: break;
692: case 1:
693: end = start+len;
694: for ( j = start, s = 0; j < end; j++ )
695: s += MUL_WEIGHT(nd_work_vector[j],j);
696: if ( s > 0 ) return 1;
697: else if ( s < 0 ) return -1;
698: for ( j = start; j < end; j++ )
699: if ( nd_work_vector[j] > 0 ) return 1;
700: else if ( nd_work_vector[j] < 0 ) return -1;
701: break;
702: case 2:
703: end = start+len;
704: for ( j = start; j < end; j++ )
705: if ( nd_work_vector[j] > 0 ) return 1;
706: else if ( nd_work_vector[j] < 0 ) return -1;
707: break;
708: }
709: break;
710: }
711: }
712: return 0;
713: }
714:
715: /* TDH -> WW -> TD-> RL */
716:
717: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
718: {
719: int i,m,e1,e2;
720:
721: if ( TD(d1) > TD(d2) ) return 1;
722: else if ( TD(d1) < TD(d2) ) return -1;
723: m = nd_nvar>>1;
724: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
725: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
726: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
727: }
728: if ( e1 > e2 ) return 1;
729: else if ( e1 < e2 ) return -1;
730: return ndl_lex_compare(d1,d2);
731: }
732:
1.21 noro 733: // common function for module glex and grlex comparison
734: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 735: {
1.21 noro 736: int c;
1.1 noro 737:
1.21 noro 738: switch ( nd_module_ordtype ) {
739: case 0:
740: if ( TD(d1) > TD(d2) ) return 1;
741: else if ( TD(d1) < TD(d2) ) return -1;
742: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
743: else if ( MPOS(d1) < MPOS(d2) ) return 1;
744: else if ( MPOS(d1) > MPOS(d2) ) return -1;
745: else return 0;
746: break;
1.1 noro 747:
1.21 noro 748: case 1:
1.19 noro 749: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
750: if ( TD(d1) > TD(d2) ) return 1;
751: else if ( TD(d1) < TD(d2) ) return -1;
752: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
753: if ( MPOS(d1) < MPOS(d2) ) return 1;
754: else if ( MPOS(d1) > MPOS(d2) ) return -1;
755: }
756: if ( MPOS(d1) < MPOS(d2) ) return 1;
757: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 758: else if ( TD(d1) > TD(d2) ) return 1;
759: else if ( TD(d1) < TD(d2) ) return -1;
760: else return ndl_lex_compare(d1,d2);
761: break;
1.1 noro 762:
1.21 noro 763: case 2: // weight -> POT
764: if ( TD(d1) > TD(d2) ) return 1;
765: else if ( TD(d1) < TD(d2) ) return -1;
766: else if ( MPOS(d1) < MPOS(d2) ) return 1;
767: else if ( MPOS(d1) > MPOS(d2) ) return -1;
768: else return ndl_lex_compare(d1,d2);
769: break;
1.1 noro 770:
1.21 noro 771: default:
772: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 773: return 0;
1.21 noro 774: }
1.1 noro 775: }
776:
1.21 noro 777: // common for module comparison
778: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 779: {
1.21 noro 780: int c;
1.1 noro 781:
1.21 noro 782: switch ( nd_module_ordtype ) {
783: case 0:
1.23 noro 784: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 785: else if ( MPOS(d1) > MPOS(d2) ) return -1;
786: else if ( MPOS(d1) < MPOS(d2) ) return 1;
787: else return 0;
788: break;
1.1 noro 789:
1.21 noro 790: case 1:
791: if ( MPOS(d1) < MPOS(d2) ) return 1;
792: else if ( MPOS(d1) > MPOS(d2) ) return -1;
793: else return (*ndl_base_compare_function)(d1,d2);
794: break;
1.1 noro 795:
1.21 noro 796: case 2: // weight -> POT
797: if ( TD(d1) > TD(d2) ) return 1;
798: else if ( TD(d1) < TD(d2) ) return -1;
799: else if ( MPOS(d1) < MPOS(d2) ) return 1;
800: else if ( MPOS(d1) > MPOS(d2) ) return -1;
801: else return (*ndl_base_compare_function)(d1,d2);
802: break;
1.1 noro 803:
1.21 noro 804: default:
805: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 806: return 0;
1.21 noro 807: }
1.1 noro 808: }
809:
1.21 noro 810: extern DMMstack dmm_stack;
811: void _addtodl(int n,DL d1,DL d2);
1.31 noro 812: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 813: int _eqdl(int n,DL d1,DL d2);
814:
815: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
816: {
1.44 noro 817: int pos1,pos2,t,j,retpot;
1.21 noro 818: DMM *in;
819: DMMstack s;
820: static DL d1=0;
821: static DL d2=0;
822: static int dlen=0;
1.44 noro 823: extern int ReversePOT;
1.21 noro 824:
1.44 noro 825: if ( ReversePOT ) retpot = -1;
826: else retpot = 1;
1.21 noro 827: pos1 = MPOS(m1); pos2 = MPOS(m2);
828: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
829: if ( nd_nvar > dlen ) {
830: NEWDL(d1,nd_nvar);
831: NEWDL(d2,nd_nvar);
832: dlen = nd_nvar;
833: }
834: d1->td = TD(m1);
835: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
836: d2->td = TD(m2);
837: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
838: for ( s = dmm_stack; s; s = NEXT(s) ) {
839: in = s->in;
840: _addtodl(nd_nvar,in[pos1]->dl,d1);
841: _addtodl(nd_nvar,in[pos2]->dl,d2);
842: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44 noro 843: if ( pos1 < pos2 ) return retpot;
844: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 845: else return 0;
846: }
847: pos1 = in[pos1]->pos;
848: pos2 = in[pos2]->pos;
849: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
850: }
851: // comparison by the bottom order
852: LAST:
853: switch ( nd_base_ordtype ) {
854: case 0:
855: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
856: if ( t ) return t;
1.44 noro 857: else if ( pos1 < pos2 ) return retpot;
858: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 859: else return 0;
860: break;
861: case 1:
1.44 noro 862: if ( pos1 < pos2 ) return retpot;
863: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 864: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
865: break;
866: case 2:
867: if ( d1->td > d2->td ) return 1;
868: else if ( d1->td < d2->td ) return -1;
1.44 noro 869: else if ( pos1 < pos2 ) return retpot;
870: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 871: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
872: break;
873: default:
874: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 875: return 0;
1.21 noro 876: }
1.1 noro 877: }
878:
879: INLINE int ndl_equal(UINT *d1,UINT *d2)
880: {
881: int i;
882:
883: switch ( nd_wpd ) {
884: case 2:
885: if ( TD(d2) != TD(d1) ) return 0;
886: if ( d2[1] != d1[1] ) return 0;
887: return 1;
888: break;
889: case 3:
890: if ( TD(d2) != TD(d1) ) return 0;
891: if ( d2[1] != d1[1] ) return 0;
892: if ( d2[2] != d1[2] ) return 0;
893: return 1;
894: break;
895: default:
896: for ( i = 0; i < nd_wpd; i++ )
897: if ( *d1++ != *d2++ ) return 0;
898: return 1;
899: break;
900: }
901: }
902:
903: INLINE void ndl_copy(UINT *d1,UINT *d2)
904: {
905: int i;
906:
907: switch ( nd_wpd ) {
908: case 2:
909: TD(d2) = TD(d1);
910: d2[1] = d1[1];
911: break;
912: case 3:
913: TD(d2) = TD(d1);
914: d2[1] = d1[1];
915: d2[2] = d1[2];
916: break;
917: default:
918: for ( i = 0; i < nd_wpd; i++ )
919: d2[i] = d1[i];
920: break;
921: }
922: }
923:
924: INLINE void ndl_zero(UINT *d)
925: {
926: int i;
927: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
928: }
929:
930: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
931: {
932: int i;
933:
934: if ( nd_module ) {
935: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
936: error("ndl_add : invalid operation");
937: }
938: #if 1
939: switch ( nd_wpd ) {
940: case 2:
941: TD(d) = TD(d1)+TD(d2);
942: d[1] = d1[1]+d2[1];
943: break;
944: case 3:
945: TD(d) = TD(d1)+TD(d2);
946: d[1] = d1[1]+d2[1];
947: d[2] = d1[2]+d2[2];
948: break;
949: default:
950: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
951: break;
952: }
953: #else
954: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
955: #endif
956: }
957:
958: /* d1 += d2 */
959: INLINE void ndl_addto(UINT *d1,UINT *d2)
960: {
961: int i;
962:
963: if ( nd_module ) {
964: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
965: error("ndl_addto : invalid operation");
966: }
967: #if 1
968: switch ( nd_wpd ) {
969: case 2:
970: TD(d1) += TD(d2);
971: d1[1] += d2[1];
972: break;
973: case 3:
974: TD(d1) += TD(d2);
975: d1[1] += d2[1];
976: d1[2] += d2[2];
977: break;
978: default:
979: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
980: break;
981: }
982: #else
983: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
984: #endif
985: }
986:
987: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
988: {
989: int i;
990:
991: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
992: }
993:
994: int ndl_disjoint(UINT *d1,UINT *d2)
995: {
996: UINT t1,t2,u,u1,u2;
997: int i,j;
998:
999: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1000: #if USE_UNROLL
1001: switch ( nd_bpe ) {
1002: case 3:
1003: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1004: u1 = d1[i]; u2 = d2[i];
1005: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
1007: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
1015: }
1016: return 1;
1017: break;
1018: case 4:
1019: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1020: u1 = d1[i]; u2 = d2[i];
1021: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1022: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1023: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1027: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1028: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1029: }
1030: return 1;
1031: break;
1032: case 6:
1033: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1034: u1 = d1[i]; u2 = d2[i];
1035: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1036: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1037: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1038: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1039: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1040: }
1041: return 1;
1042: break;
1043: case 8:
1044: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1045: u1 = d1[i]; u2 = d2[i];
1046: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1047: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1048: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1049: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1050: }
1051: return 1;
1052: break;
1053: case 16:
1054: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1055: u1 = d1[i]; u2 = d2[i];
1056: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1057: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1058: }
1059: return 1;
1060: break;
1061: case 32:
1062: for ( i = nd_exporigin; i < nd_wpd; i++ )
1063: if ( d1[i] && d2[i] ) return 0;
1064: return 1;
1065: break;
1066: default:
1067: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1068: u1 = d1[i]; u2 = d2[i];
1069: for ( j = 0; j < nd_epw; j++ ) {
1070: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1071: u1 >>= nd_bpe; u2 >>= nd_bpe;
1072: }
1073: }
1074: return 1;
1075: break;
1076: }
1077: #else
1078: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1079: u1 = d1[i]; u2 = d2[i];
1080: for ( j = 0; j < nd_epw; j++ ) {
1081: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1082: u1 >>= nd_bpe; u2 >>= nd_bpe;
1083: }
1084: }
1085: return 1;
1086: #endif
1087: }
1088:
1089: int ndl_check_bound(UINT *d1,UINT *d2)
1090: {
1091: UINT u2;
1092: int i,j,ind,k;
1093:
1094: ind = 0;
1095: #if USE_UNROLL
1096: switch ( nd_bpe ) {
1097: case 3:
1098: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1099: u2 = d2[i];
1100: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1101: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1102: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1103: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1104: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1105: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1106: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1107: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1108: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1109: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1110: }
1111: return 0;
1112: break;
1113: case 4:
1114: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1115: u2 = d2[i];
1116: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1117: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1118: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1119: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1120: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1121: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1122: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1123: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1124: }
1125: return 0;
1126: break;
1127: case 6:
1128: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1129: u2 = d2[i];
1130: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1131: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1132: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1133: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1134: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1135: }
1136: return 0;
1137: break;
1138: case 8:
1139: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1140: u2 = d2[i];
1141: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1142: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1143: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1144: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1145: }
1146: return 0;
1147: break;
1148: case 16:
1149: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1150: u2 = d2[i];
1151: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1152: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1153: }
1154: return 0;
1155: break;
1156: case 32:
1157: for ( i = nd_exporigin; i < nd_wpd; i++ )
1158: if ( d1[i]+d2[i]<d1[i] ) return 1;
1159: return 0;
1160: break;
1161: default:
1162: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1163: u2 = d2[i];
1164: k = (nd_epw-1)*nd_bpe;
1165: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1166: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1167: }
1168: return 0;
1169: break;
1170: }
1171: #else
1172: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1173: u2 = d2[i];
1174: k = (nd_epw-1)*nd_bpe;
1175: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1176: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1177: }
1178: return 0;
1179: #endif
1180: }
1181:
1182: int ndl_check_bound2(int index,UINT *d2)
1183: {
1184: return ndl_check_bound(nd_bound[index],d2);
1185: }
1186:
1187: INLINE int ndl_hash_value(UINT *d)
1188: {
1189: int i;
1.11 noro 1190: UINT r;
1.1 noro 1191:
1192: r = 0;
1193: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1194: r = (r*1511+d[i]);
1.11 noro 1195: r %= REDTAB_LEN;
1.1 noro 1196: return r;
1197: }
1198:
1199: INLINE int ndl_find_reducer(UINT *dg)
1200: {
1201: RHist r;
1202: int d,k,i;
1203:
1204: d = ndl_hash_value(dg);
1205: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1206: if ( ndl_equal(dg,DL(r)) ) {
1207: if ( k > 0 ) nd_notfirst++;
1208: nd_found++;
1209: return r->index;
1210: }
1211: }
1212: if ( Reverse )
1213: for ( i = nd_psn-1; i >= 0; i-- ) {
1214: r = nd_psh[i];
1215: if ( ndl_reducible(dg,DL(r)) ) {
1216: nd_create++;
1217: nd_append_red(dg,i);
1218: return i;
1219: }
1220: }
1221: else
1222: for ( i = 0; i < nd_psn; i++ ) {
1223: r = nd_psh[i];
1224: if ( ndl_reducible(dg,DL(r)) ) {
1225: nd_create++;
1226: nd_append_red(dg,i);
1227: return i;
1228: }
1229: }
1230: return -1;
1231: }
1232:
1.41 noro 1233: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1234: {
1235: RHist r;
1236: int i;
1237:
1238: for ( i = 0; i < nd_psn; i++ ) {
1239: r = nd_psh[i];
1240: if ( ndl_reducible(dg,DL(r)) ) return i;
1241: }
1242: return -1;
1243: }
1244:
1.24 noro 1245: // ret=0,...,nd_psn-1 => reducer found
1246: // ret=nd_psn => reducer not found
1247: // ret=-1 => singular top reducible
1248:
1249: int comp_sig(SIG s1,SIG s2);
1250: void _ndltodl(UINT *ndl,DL dl);
1251:
1252: void print_sig(SIG s)
1253: {
1254: int i;
1255:
1256: fprintf(asir_out,"<<");
1257: for ( i = 0; i < nd_nvar; i++ ) {
1258: fprintf(asir_out,"%d",s->dl->d[i]);
1259: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1260: }
1261: fprintf(asir_out,">>*e%d",s->pos);
1262: }
1263:
1.45 noro 1264: void print_siglist(NODE l)
1265: {
1266: for ( ; l; l = NEXT(l) )
1267: print_sig((SIG)l->body);
1268: }
1269:
1270:
1.35 noro 1271: // assuming increasing order wrt signature
1272:
1.24 noro 1273: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1274: {
1275: RHist r;
1.27 noro 1276: int i,singular,ret,d,k;
1.26 noro 1277: static int wpd,nvar;
1.24 noro 1278: static SIG quo;
1279: static UINT *tmp;
1280:
1.26 noro 1281: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1282: if ( wpd != nd_wpd ) {
1.24 noro 1283: wpd = nd_wpd;
1284: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1285: }
1.27 noro 1286: d = ndl_hash_value(dg);
1.35 noro 1287: #if 1
1.27 noro 1288: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1289: if ( ndl_equal(dg,DL(r)) ) {
1290: return r->index;
1291: }
1292: }
1.35 noro 1293: #endif
1.24 noro 1294: singular = 0;
1295: for ( i = 0; i < nd_psn; i++ ) {
1296: r = nd_psh[i];
1297: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1298: ndl_sub(dg,DL(r),tmp);
1.24 noro 1299: _ndltodl(tmp,DL(quo));
1300: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1301: quo->pos = nd_psh[i]->sig->pos;
1302: ret = comp_sig(sig,quo);
1303: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1304: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1305: }
1306: }
1307: if ( singular ) return -1;
1.27 noro 1308: else if ( i < nd_psn )
1309: nd_append_red(dg,i);
1310: return i;
1.24 noro 1311: }
1312:
1.1 noro 1313: ND nd_merge(ND p1,ND p2)
1314: {
1315: int n,c;
1316: int t,can,td1,td2;
1317: ND r;
1318: NM m1,m2,mr0,mr,s;
1319:
1320: if ( !p1 ) return p2;
1321: else if ( !p2 ) return p1;
1322: else {
1323: can = 0;
1324: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1325: c = DL_COMPARE(DL(m1),DL(m2));
1326: switch ( c ) {
1327: case 0:
1328: s = m1; m1 = NEXT(m1);
1329: can++; NEXTNM2(mr0,mr,s);
1330: s = m2; m2 = NEXT(m2); FREENM(s);
1331: break;
1332: case 1:
1333: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1334: break;
1335: case -1:
1336: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1337: break;
1338: }
1339: }
1340: if ( !mr0 )
1341: if ( m1 ) mr0 = m1;
1342: else if ( m2 ) mr0 = m2;
1343: else return 0;
1344: else if ( m1 ) NEXT(mr) = m1;
1345: else if ( m2 ) NEXT(mr) = m2;
1346: else NEXT(mr) = 0;
1347: BDY(p1) = mr0;
1348: SG(p1) = MAX(SG(p1),SG(p2));
1349: LEN(p1) = LEN(p1)+LEN(p2)-can;
1350: FREEND(p2);
1351: return p1;
1352: }
1353: }
1354:
1355: ND nd_add(int mod,ND p1,ND p2)
1356: {
1357: int n,c;
1358: int t,can,td1,td2;
1359: ND r;
1360: NM m1,m2,mr0,mr,s;
1361:
1.11 noro 1362: Nnd_add++;
1.1 noro 1363: if ( !p1 ) return p2;
1364: else if ( !p2 ) return p1;
1365: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1366: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1367: else if ( !mod ) return nd_add_q(p1,p2);
1368: else {
1369: can = 0;
1370: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1371: c = DL_COMPARE(DL(m1),DL(m2));
1372: switch ( c ) {
1373: case 0:
1374: t = ((CM(m1))+(CM(m2))) - mod;
1375: if ( t < 0 ) t += mod;
1376: s = m1; m1 = NEXT(m1);
1377: if ( t ) {
1378: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1379: } else {
1380: can += 2; FREENM(s);
1381: }
1382: s = m2; m2 = NEXT(m2); FREENM(s);
1383: break;
1384: case 1:
1385: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1386: break;
1387: case -1:
1388: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1389: break;
1390: }
1391: }
1392: if ( !mr0 )
1393: if ( m1 ) mr0 = m1;
1394: else if ( m2 ) mr0 = m2;
1395: else return 0;
1396: else if ( m1 ) NEXT(mr) = m1;
1397: else if ( m2 ) NEXT(mr) = m2;
1398: else NEXT(mr) = 0;
1399: BDY(p1) = mr0;
1400: SG(p1) = MAX(SG(p1),SG(p2));
1401: LEN(p1) = LEN(p1)+LEN(p2)-can;
1402: FREEND(p2);
1403: return p1;
1404: }
1405: }
1406:
1407: /* XXX on opteron, the inlined manipulation of destructive additon of
1408: * two NM seems to make gcc optimizer get confused, so the part is
1409: * done in a function.
1410: */
1411:
1412: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1413: {
1414: NM s;
1415: P t;
1416: int can;
1417:
1418: addp(nd_vc,CP(*m1),CP(*m2),&t);
1419: s = *m1; *m1 = NEXT(*m1);
1420: if ( t ) {
1421: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1422: } else {
1423: can = 2; FREENM(s);
1424: }
1425: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1426: return can;
1427: }
1428:
1429: ND nd_add_q(ND p1,ND p2)
1430: {
1431: int n,c,can;
1432: ND r;
1433: NM m1,m2,mr0,mr,s;
1434: P t;
1435:
1436: if ( !p1 ) return p2;
1437: else if ( !p2 ) return p1;
1438: else {
1439: can = 0;
1440: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1441: c = DL_COMPARE(DL(m1),DL(m2));
1442: switch ( c ) {
1443: case 0:
1444: #if defined(__x86_64__)
1445: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1446: #else
1447: addp(nd_vc,CP(m1),CP(m2),&t);
1448: s = m1; m1 = NEXT(m1);
1449: if ( t ) {
1450: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1451: } else {
1452: can += 2; FREENM(s);
1453: }
1454: s = m2; m2 = NEXT(m2); FREENM(s);
1455: #endif
1456: break;
1457: case 1:
1458: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1459: break;
1460: case -1:
1461: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1462: break;
1463: }
1464: }
1465: if ( !mr0 )
1466: if ( m1 ) mr0 = m1;
1467: else if ( m2 ) mr0 = m2;
1468: else return 0;
1469: else if ( m1 ) NEXT(mr) = m1;
1470: else if ( m2 ) NEXT(mr) = m2;
1471: else NEXT(mr) = 0;
1472: BDY(p1) = mr0;
1473: SG(p1) = MAX(SG(p1),SG(p2));
1474: LEN(p1) = LEN(p1)+LEN(p2)-can;
1475: FREEND(p2);
1476: return p1;
1477: }
1478: }
1479:
1480: ND nd_add_sf(ND p1,ND p2)
1481: {
1482: int n,c,can;
1483: ND r;
1484: NM m1,m2,mr0,mr,s;
1485: int t;
1486:
1487: if ( !p1 ) return p2;
1488: else if ( !p2 ) return p1;
1489: else {
1490: can = 0;
1491: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1492: c = DL_COMPARE(DL(m1),DL(m2));
1493: switch ( c ) {
1494: case 0:
1495: t = _addsf(CM(m1),CM(m2));
1496: s = m1; m1 = NEXT(m1);
1497: if ( t ) {
1498: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1499: } else {
1500: can += 2; FREENM(s);
1501: }
1502: s = m2; m2 = NEXT(m2); FREENM(s);
1503: break;
1504: case 1:
1505: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1506: break;
1507: case -1:
1508: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1509: break;
1510: }
1511: }
1512: if ( !mr0 )
1513: if ( m1 ) mr0 = m1;
1514: else if ( m2 ) mr0 = m2;
1515: else return 0;
1516: else if ( m1 ) NEXT(mr) = m1;
1517: else if ( m2 ) NEXT(mr) = m2;
1518: else NEXT(mr) = 0;
1519: BDY(p1) = mr0;
1520: SG(p1) = MAX(SG(p1),SG(p2));
1521: LEN(p1) = LEN(p1)+LEN(p2)-can;
1522: FREEND(p2);
1523: return p1;
1524: }
1525: }
1526:
1527:
1528: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1529: {
1530: int c,c1,c2;
1531: Z cg,cred,gcd,tq;
1532: P cgp,credp,gcdp;
1533: Obj tr,tr1;
1534:
1535: if ( mod == -1 ) {
1536: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1537: *divp = (Obj)ONE;
1538: } else if ( mod == -2 ) {
1539: Z inv,t;
1540: divlf(ONE,HCZ(p),&inv);
1541: chsgnlf(HCZ(g),&t);
1542: mullf(inv,t,&CZ(mul));
1543: *divp = (Obj)ONE;
1544: } else if ( mod ) {
1545: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1546: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1547: *divp = (Obj)ONE;
1548: } else if ( nd_vc ) {
1549: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1550: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1551: chsgnp(cgp,&CP(mul));
1552: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1553: if ( dn ) {
1554: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1555: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1556: }
1557: *divp = (Obj)credp;
1558: } else {
1.6 noro 1559: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1560: chsgnz(cg,&CZ(mul));
1.1 noro 1561: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1562: if ( dn ) {
1563: mulz(dn->z,cred,&tq); dn->z = tq;
1564: }
1565: *divp = (Obj)cred;
1566: }
1567: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1568: }
1569:
1570: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1571: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1572: {
1573: NM m,mrd,tail;
1574: NM mul;
1575: int n,sugar,psugar,sugar0,stat,index;
1576: int c,c1,c2,dummy;
1577: RHist h;
1578: NDV p,red;
1579: Q cg,cred,gcd,tq,qq;
1580: Z iq;
1581: DP dmul;
1582: NODE node;
1583: LIST hist;
1584: double hmag;
1585: P tp,tp1;
1586: Obj tr,tr1,div;
1587: union oNDC hg;
1588: P cont;
1589:
1590: if ( !g ) {
1591: *rp = d;
1592: return 1;
1593: }
1594: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1595:
1596: sugar0 = sugar = SG(g);
1597: n = NV(g);
1598: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1599: if ( d )
1600: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1601: for ( ; g; ) {
1602: index = ndl_find_reducer(HDL(g));
1603: if ( index >= 0 ) {
1604: h = nd_psh[index];
1605: ndl_sub(HDL(g),DL(h),DL(mul));
1606: if ( ndl_check_bound2(index,DL(mul)) ) {
1607: nd_free(g); nd_free(d);
1608: return 0;
1609: }
1610: p = nd_demand ? ndv_load(index) : ps[index];
1611: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1612: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1613: if ( nd_gentrace ) {
1614: /* Trace=[div,index,mul,ONE] */
1.6 noro 1615: STOZ(index,iq);
1.1 noro 1616: nmtodp(mod,mul,&dmul);
1617: node = mknode(4,div,iq,dmul,ONE);
1618: }
1619: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1620: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1621: hg = HCU(g);
1622: nd_removecont2(d,g);
1.6 noro 1623: if ( nd_gentrace ) {
1.1 noro 1624: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1625: /* exact division */
1.1 noro 1626: cont = ndc_div(mod,hg,HCU(g));
1627: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1628: }
1629: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1630: }
1631: MKLIST(hist,node);
1632: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1633: } else if ( !full ) {
1634: *rp = g;
1635: return 1;
1636: } else {
1637: m = BDY(g);
1638: if ( NEXT(m) ) {
1639: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1640: } else {
1641: FREEND(g); g = 0;
1642: }
1643: if ( d ) {
1644: NEXT(tail)=m; tail=m; LEN(d)++;
1645: } else {
1646: MKND(n,m,1,d); tail = BDY(d);
1647: }
1648: }
1649: }
1650: if ( d ) SG(d) = sugar;
1651: *rp = d;
1652: return 1;
1653: }
1654:
1.24 noro 1655: // ret=1 => success
1656: // ret=0 => overflow
1657: // ret=-1 => singular top reducible
1658:
1659: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1660: {
1661: NM m,mrd,tail;
1662: NM mul;
1663: int n,sugar,psugar,sugar0,stat,index;
1664: int c,c1,c2,dummy;
1665: RHist h;
1666: NDV p,red;
1667: Q cg,cred,gcd,tq,qq;
1668: Z iq;
1669: DP dmul;
1670: NODE node;
1671: LIST hist;
1672: double hmag;
1673: P tp,tp1;
1674: Obj tr,tr1,div;
1675: union oNDC hg;
1676: P cont;
1677: SIG sig;
1678:
1679: if ( !g ) {
1680: *rp = d;
1681: return 1;
1682: }
1683: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1684:
1685: sugar0 = sugar = SG(g);
1686: n = NV(g);
1687: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1688: if ( d )
1689: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1690: sig = g->sig;
1691: for ( ; g; ) {
1692: index = ndl_find_reducer_s(HDL(g),sig);
1693: if ( index >= 0 && index < nd_psn ) {
1694: // reducer found
1695: h = nd_psh[index];
1696: ndl_sub(HDL(g),DL(h),DL(mul));
1697: if ( ndl_check_bound2(index,DL(mul)) ) {
1698: nd_free(g); nd_free(d);
1699: return 0;
1700: }
1701: p = ps[index];
1702: /* d+g -> div*(d+g)+mul*p */
1703: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1704: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1705: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1706: hg = HCU(g);
1707: nd_removecont2(d,g);
1708: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1709: }
1710: } else if ( index == -1 ) {
1711: // singular top reducible
1712: return -1;
1713: } else if ( !full ) {
1714: *rp = g;
1715: g->sig = sig;
1716: return 1;
1717: } else {
1718: m = BDY(g);
1719: if ( NEXT(m) ) {
1720: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1721: } else {
1722: FREEND(g); g = 0;
1723: }
1724: if ( d ) {
1725: NEXT(tail)=m; tail=m; LEN(d)++;
1726: } else {
1727: MKND(n,m,1,d); tail = BDY(d);
1728: }
1729: }
1730: }
1731: if ( d ) {
1732: SG(d) = sugar;
1733: d->sig = sig;
1734: }
1735: *rp = d;
1736: return 1;
1737: }
1738:
1.1 noro 1739: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1740: {
1741: int hindex,index;
1742: NDV p;
1743: ND u,d,red;
1744: NODE l;
1745: NM mul,m,mrd,tail;
1746: int sugar,psugar,n,h_reducible;
1747: PGeoBucket bucket;
1748: int c,c1,c2;
1749: Z cg,cred,gcd,zzz;
1750: RHist h;
1751: double hmag,gmag;
1752: int count = 0;
1753: int hcount = 0;
1754:
1755: if ( !g ) {
1756: *rp = 0;
1757: return 1;
1758: }
1759: sugar = SG(g);
1760: n = NV(g);
1.6 noro 1761: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1762: bucket = create_pbucket();
1763: add_pbucket(mod,bucket,g);
1764: d = 0;
1765: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1766: while ( 1 ) {
1767: if ( mod > 0 || mod == -1 )
1768: hindex = head_pbucket(mod,bucket);
1769: else if ( mod == -2 )
1770: hindex = head_pbucket_lf(bucket);
1771: else
1772: hindex = head_pbucket_q(bucket);
1773: if ( hindex < 0 ) {
1774: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1775: if ( d ) SG(d) = sugar;
1776: *rp = d;
1777: return 1;
1778: }
1779: g = bucket->body[hindex];
1780: index = ndl_find_reducer(HDL(g));
1781: if ( index >= 0 ) {
1782: count++;
1783: if ( !d ) hcount++;
1784: h = nd_psh[index];
1785: ndl_sub(HDL(g),DL(h),DL(mul));
1786: if ( ndl_check_bound2(index,DL(mul)) ) {
1787: nd_free(d);
1788: free_pbucket(bucket);
1789: *rp = 0;
1790: return 0;
1791: }
1792: p = ps[index];
1793: if ( mod == -1 )
1794: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1795: else if ( mod == -2 ) {
1796: Z inv,t;
1797: divlf(ONE,HCZ(p),&inv);
1798: chsgnlf(HCZ(g),&t);
1799: mullf(inv,t,&CZ(mul));
1800: } else if ( mod ) {
1801: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1802: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1803: } else {
1.6 noro 1804: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1805: chsgnz(cg,&CZ(mul));
1.1 noro 1806: nd_mul_c_q(d,(P)cred);
1807: mulq_pbucket(bucket,cred);
1808: g = bucket->body[hindex];
1.6 noro 1809: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1810: }
1811: red = ndv_mul_nm(mod,mul,p);
1812: bucket->body[hindex] = nd_remove_head(g);
1813: red = nd_remove_head(red);
1814: add_pbucket(mod,bucket,red);
1815: psugar = SG(p)+TD(DL(mul));
1816: sugar = MAX(sugar,psugar);
1817: if ( !mod && hmag && (gmag > hmag) ) {
1818: g = normalize_pbucket(mod,bucket);
1819: if ( !g ) {
1820: if ( d ) SG(d) = sugar;
1821: *rp = d;
1822: return 1;
1823: }
1824: nd_removecont2(d,g);
1.6 noro 1825: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1826: add_pbucket(mod,bucket,g);
1827: }
1828: } else if ( !full ) {
1829: g = normalize_pbucket(mod,bucket);
1830: if ( g ) SG(g) = sugar;
1831: *rp = g;
1832: return 1;
1833: } else {
1834: m = BDY(g);
1835: if ( NEXT(m) ) {
1836: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1837: } else {
1838: FREEND(g); g = 0;
1839: }
1840: bucket->body[hindex] = g;
1841: NEXT(m) = 0;
1842: if ( d ) {
1843: NEXT(tail)=m; tail=m; LEN(d)++;
1844: } else {
1845: MKND(n,m,1,d); tail = BDY(d);
1846: }
1847: }
1848: }
1849: }
1850:
1.25 noro 1851: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1852: {
1853: int hindex,index;
1854: NDV p;
1855: ND u,d,red;
1856: NODE l;
1857: NM mul,m,mrd,tail;
1858: int sugar,psugar,n,h_reducible;
1859: PGeoBucket bucket;
1860: int c,c1,c2;
1861: Z cg,cred,gcd,zzz;
1862: RHist h;
1863: double hmag,gmag;
1864: int count = 0;
1865: int hcount = 0;
1866: SIG sig;
1867:
1868: if ( !g ) {
1869: *rp = 0;
1870: return 1;
1871: }
1872: sugar = SG(g);
1873: n = NV(g);
1874: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1875: bucket = create_pbucket();
1876: add_pbucket(mod,bucket,g);
1877: d = 0;
1878: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1879: sig = g->sig;
1880: while ( 1 ) {
1881: if ( mod > 0 || mod == -1 )
1882: hindex = head_pbucket(mod,bucket);
1883: else if ( mod == -2 )
1884: hindex = head_pbucket_lf(bucket);
1885: else
1886: hindex = head_pbucket_q(bucket);
1887: if ( hindex < 0 ) {
1888: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1889: if ( d ) {
1890: SG(d) = sugar;
1891: d->sig = sig;
1892: }
1893: *rp = d;
1894: return 1;
1895: }
1896: g = bucket->body[hindex];
1897: index = ndl_find_reducer_s(HDL(g),sig);
1898: if ( index >= 0 && index < nd_psn ) {
1899: count++;
1900: if ( !d ) hcount++;
1901: h = nd_psh[index];
1902: ndl_sub(HDL(g),DL(h),DL(mul));
1903: if ( ndl_check_bound2(index,DL(mul)) ) {
1904: nd_free(d);
1905: free_pbucket(bucket);
1906: *rp = 0;
1907: return 0;
1908: }
1909: p = ps[index];
1910: if ( mod == -1 )
1911: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1912: else if ( mod == -2 ) {
1913: Z inv,t;
1914: divlf(ONE,HCZ(p),&inv);
1915: chsgnlf(HCZ(g),&t);
1916: mullf(inv,t,&CZ(mul));
1917: } else if ( mod ) {
1918: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1919: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1920: } else {
1921: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1922: chsgnz(cg,&CZ(mul));
1923: nd_mul_c_q(d,(P)cred);
1924: mulq_pbucket(bucket,cred);
1925: g = bucket->body[hindex];
1926: gmag = (double)p_mag((P)HCZ(g));
1927: }
1928: red = ndv_mul_nm(mod,mul,p);
1929: bucket->body[hindex] = nd_remove_head(g);
1930: red = nd_remove_head(red);
1931: add_pbucket(mod,bucket,red);
1932: psugar = SG(p)+TD(DL(mul));
1933: sugar = MAX(sugar,psugar);
1934: if ( !mod && hmag && (gmag > hmag) ) {
1935: g = normalize_pbucket(mod,bucket);
1936: if ( !g ) {
1937: if ( d ) {
1938: SG(d) = sugar;
1939: d->sig = sig;
1940: }
1941: *rp = d;
1942: return 1;
1943: }
1944: nd_removecont2(d,g);
1945: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1946: add_pbucket(mod,bucket,g);
1947: }
1948: } else if ( index == -1 ) {
1949: // singular top reducible
1950: return -1;
1951: } else if ( !full ) {
1952: g = normalize_pbucket(mod,bucket);
1953: if ( g ) {
1954: SG(g) = sugar;
1955: g->sig = sig;
1956: }
1957: *rp = g;
1958: return 1;
1959: } else {
1960: m = BDY(g);
1961: if ( NEXT(m) ) {
1962: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1963: } else {
1964: FREEND(g); g = 0;
1965: }
1966: bucket->body[hindex] = g;
1967: NEXT(m) = 0;
1968: if ( d ) {
1969: NEXT(tail)=m; tail=m; LEN(d)++;
1970: } else {
1971: MKND(n,m,1,d); tail = BDY(d);
1972: }
1973: }
1974: }
1975: }
1976:
1.1 noro 1977: /* input : list of NDV, cand : list of NDV */
1978:
1979: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1980: {
1981: int n,i,stat;
1982: ND nf,d;
1983: NDV r;
1984: NODE t,s;
1985: union oNDC dn;
1986: Z q;
1987: LIST list;
1988:
1.24 noro 1989: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1990: n = length(cand);
1991:
1992: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1993: /* membercheck : list is a subset of Id(cand) ? */
1994: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1995: again:
1996: nd_tracelist = 0;
1997: if ( nd_bpe > obpe )
1998: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1999: else
2000: r = (NDV)BDY(t);
2001: #if 0
2002: // moved to nd_f4_lf_trace()
2003: if ( m == -2 ) ndv_mod(m,r);
2004: #endif
2005: d = ndvtond(m,r);
1.6 noro 2006: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 2007: if ( !stat ) {
2008: nd_reconstruct(0,0);
2009: goto again;
2010: } else if ( nf ) return 0;
2011: if ( nd_gentrace ) {
2012: nd_tracelist = reverse_node(nd_tracelist);
2013: MKLIST(list,nd_tracelist);
1.6 noro 2014: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 2015: MKNODE(s,list,nd_alltracelist);
2016: nd_alltracelist = s; nd_tracelist = 0;
2017: }
2018: if ( DP_Print ) { printf("."); fflush(stdout); }
2019: }
2020: if ( DP_Print ) { printf("\n"); }
2021: return 1;
2022: }
2023:
2024: ND nd_remove_head(ND p)
2025: {
2026: NM m;
2027:
2028: m = BDY(p);
2029: if ( !NEXT(m) ) {
2030: FREEND(p); p = 0;
2031: } else {
2032: BDY(p) = NEXT(m); LEN(p)--;
2033: }
2034: FREENM(m);
2035: return p;
2036: }
2037:
2038: ND nd_separate_head(ND p,ND *head)
2039: {
2040: NM m,m0;
2041: ND r;
2042:
2043: m = BDY(p);
2044: if ( !NEXT(m) ) {
2045: *head = p; p = 0;
2046: } else {
2047: m0 = m;
2048: BDY(p) = NEXT(m); LEN(p)--;
2049: NEXT(m0) = 0;
2050: MKND(NV(p),m0,1,r);
2051: *head = r;
2052: }
2053: return p;
2054: }
2055:
2056: PGeoBucket create_pbucket()
2057: {
2058: PGeoBucket g;
2059:
2060: g = CALLOC(1,sizeof(struct oPGeoBucket));
2061: g->m = -1;
2062: return g;
2063: }
2064:
2065: void free_pbucket(PGeoBucket b) {
2066: int i;
2067:
2068: for ( i = 0; i <= b->m; i++ )
2069: if ( b->body[i] ) {
2070: nd_free(b->body[i]);
2071: b->body[i] = 0;
2072: }
2073: GCFREE(b);
2074: }
2075:
1.39 noro 2076: #if 0
1.1 noro 2077: void add_pbucket_symbolic(PGeoBucket g,ND d)
2078: {
2079: int l,i,k,m;
2080:
2081: if ( !d )
2082: return;
2083: l = LEN(d);
2084: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2085: /* 2^(k-1) < l <= 2^k (=m) */
2086: d = nd_merge(g->body[k],d);
2087: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2088: g->body[k] = 0;
2089: d = nd_merge(g->body[k+1],d);
2090: }
2091: g->body[k] = d;
2092: g->m = MAX(g->m,k);
2093: }
1.39 noro 2094: #else
2095: void add_pbucket_symbolic(PGeoBucket g,ND d)
2096: {
2097: int l,i,k,m,m0;
2098:
2099: if ( !d )
2100: return;
2101: m0 = g->m;
2102: while ( 1 ) {
2103: l = LEN(d);
2104: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2105: /* 2^(k-1) < l <= 2^k (=m) */
2106: if ( g->body[k] == 0 ) {
2107: g->body[k] = d;
2108: m0 = MAX(k,m0);
2109: break;
2110: } else {
2111: d = nd_merge(g->body[k],d);
2112: g->body[k] = 0;
2113: }
2114: }
2115: g->m = m0;
2116: }
2117: #endif
1.1 noro 2118:
1.39 noro 2119: #if 0
1.1 noro 2120: void add_pbucket(int mod,PGeoBucket g,ND d)
2121: {
2122: int l,i,k,m;
2123:
2124: if ( !d )
2125: return;
2126: l = LEN(d);
2127: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2128: /* 2^(k-1) < l <= 2^k (=m) */
2129: d = nd_add(mod,g->body[k],d);
2130: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2131: g->body[k] = 0;
2132: d = nd_add(mod,g->body[k+1],d);
2133: }
2134: g->body[k] = d;
2135: g->m = MAX(g->m,k);
2136: }
1.39 noro 2137: #else
2138: void add_pbucket(int mod,PGeoBucket g,ND d)
2139: {
2140: int l,i,k,m,m0;
2141:
2142: m0 = g->m;
2143: while ( d != 0 ) {
2144: l = LEN(d);
2145: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2146: /* 2^(k-1) < l <= 2^k (=m) */
2147: if ( g->body[k] == 0 ) {
2148: g->body[k] = d;
2149: m0 = MAX(k,m0);
2150: break;
2151: } else {
2152: d = nd_add(mod,g->body[k],d);
2153: g->body[k] = 0;
2154: }
2155: }
2156: g->m = m0;
2157: }
2158: #endif
1.1 noro 2159:
2160: void mulq_pbucket(PGeoBucket g,Z c)
2161: {
2162: int k;
2163:
2164: for ( k = 0; k <= g->m; k++ )
2165: nd_mul_c_q(g->body[k],(P)c);
2166: }
2167:
2168: NM remove_head_pbucket_symbolic(PGeoBucket g)
2169: {
2170: int j,i,k,c;
2171: NM head;
2172:
2173: k = g->m;
2174: j = -1;
2175: for ( i = 0; i <= k; i++ ) {
2176: if ( !g->body[i] ) continue;
2177: if ( j < 0 ) j = i;
2178: else {
2179: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2180: if ( c > 0 )
2181: j = i;
2182: else if ( c == 0 )
2183: g->body[i] = nd_remove_head(g->body[i]);
2184: }
2185: }
2186: if ( j < 0 ) return 0;
2187: else {
2188: head = BDY(g->body[j]);
2189: if ( !NEXT(head) ) {
2190: FREEND(g->body[j]);
2191: g->body[j] = 0;
2192: } else {
2193: BDY(g->body[j]) = NEXT(head);
2194: LEN(g->body[j])--;
2195: }
2196: return head;
2197: }
2198: }
2199:
2200: int head_pbucket(int mod,PGeoBucket g)
2201: {
2202: int j,i,c,k,nv,sum;
2203: UINT *di,*dj;
2204: ND gi,gj;
2205:
2206: k = g->m;
2207: while ( 1 ) {
2208: j = -1;
2209: for ( i = 0; i <= k; i++ ) {
2210: if ( !(gi = g->body[i]) )
2211: continue;
2212: if ( j < 0 ) {
2213: j = i;
2214: gj = g->body[j];
2215: dj = HDL(gj);
2216: sum = HCM(gj);
2217: } else {
2218: c = DL_COMPARE(HDL(gi),dj);
2219: if ( c > 0 ) {
2220: if ( sum ) HCM(gj) = sum;
2221: else g->body[j] = nd_remove_head(gj);
2222: j = i;
2223: gj = g->body[j];
2224: dj = HDL(gj);
2225: sum = HCM(gj);
2226: } else if ( c == 0 ) {
2227: if ( mod == -1 )
2228: sum = _addsf(sum,HCM(gi));
2229: else {
2230: sum = sum+HCM(gi)-mod;
2231: if ( sum < 0 ) sum += mod;
2232: }
2233: g->body[i] = nd_remove_head(gi);
2234: }
2235: }
2236: }
2237: if ( j < 0 ) return -1;
2238: else if ( sum ) {
2239: HCM(gj) = sum;
2240: return j;
2241: } else
2242: g->body[j] = nd_remove_head(gj);
2243: }
2244: }
2245:
2246: int head_pbucket_q(PGeoBucket g)
2247: {
2248: int j,i,c,k,nv;
2249: Z sum,t;
2250: ND gi,gj;
2251:
2252: k = g->m;
2253: while ( 1 ) {
2254: j = -1;
2255: for ( i = 0; i <= k; i++ ) {
2256: if ( !(gi = g->body[i]) ) continue;
2257: if ( j < 0 ) {
2258: j = i;
2259: gj = g->body[j];
1.6 noro 2260: sum = HCZ(gj);
1.1 noro 2261: } else {
2262: nv = NV(gi);
2263: c = DL_COMPARE(HDL(gi),HDL(gj));
2264: if ( c > 0 ) {
1.6 noro 2265: if ( sum ) HCZ(gj) = sum;
1.1 noro 2266: else g->body[j] = nd_remove_head(gj);
2267: j = i;
2268: gj = g->body[j];
1.6 noro 2269: sum = HCZ(gj);
1.1 noro 2270: } else if ( c == 0 ) {
1.6 noro 2271: addz(sum,HCZ(gi),&t);
1.1 noro 2272: sum = t;
2273: g->body[i] = nd_remove_head(gi);
2274: }
2275: }
2276: }
2277: if ( j < 0 ) return -1;
2278: else if ( sum ) {
1.6 noro 2279: HCZ(gj) = sum;
1.1 noro 2280: return j;
2281: } else
2282: g->body[j] = nd_remove_head(gj);
2283: }
2284: }
2285:
2286: int head_pbucket_lf(PGeoBucket g)
2287: {
2288: int j,i,c,k,nv;
2289: Z sum,t;
2290: ND gi,gj;
2291:
2292: k = g->m;
2293: while ( 1 ) {
2294: j = -1;
2295: for ( i = 0; i <= k; i++ ) {
2296: if ( !(gi = g->body[i]) ) continue;
2297: if ( j < 0 ) {
2298: j = i;
2299: gj = g->body[j];
2300: sum = HCZ(gj);
2301: } else {
2302: nv = NV(gi);
2303: c = DL_COMPARE(HDL(gi),HDL(gj));
2304: if ( c > 0 ) {
2305: if ( sum ) HCZ(gj) = sum;
2306: else g->body[j] = nd_remove_head(gj);
2307: j = i;
2308: gj = g->body[j];
2309: sum = HCZ(gj);
2310: } else if ( c == 0 ) {
2311: addlf(sum,HCZ(gi),&t);
2312: sum = t;
2313: g->body[i] = nd_remove_head(gi);
2314: }
2315: }
2316: }
2317: if ( j < 0 ) return -1;
2318: else if ( sum ) {
2319: HCZ(gj) = sum;
2320: return j;
2321: } else
2322: g->body[j] = nd_remove_head(gj);
2323: }
2324: }
2325:
2326: ND normalize_pbucket(int mod,PGeoBucket g)
2327: {
2328: int i;
2329: ND r,t;
2330:
2331: r = 0;
2332: for ( i = 0; i <= g->m; i++ ) {
2333: r = nd_add(mod,r,g->body[i]);
2334: g->body[i] = 0;
2335: }
2336: g->m = -1;
2337: return r;
2338: }
2339:
2340: #if 0
2341: void register_hcf(NDV p)
2342: {
2343: DCP dc,t;
2344: P hc,h;
2345: int c;
2346: NODE l,l1,prev;
2347:
2348: hc = p->body->c.p;
2349: if ( !nd_vc || NUM(hc) ) return;
2350: fctrp(nd_vc,hc,&dc);
2351: for ( t = dc; t; t = NEXT(t) ) {
2352: h = t->c;
2353: if ( NUM(h) ) continue;
2354: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2355: c = compp(nd_vc,h,(P)BDY(l));
2356: if ( c >= 0 ) break;
2357: }
2358: if ( !l || c > 0 ) {
2359: MKNODE(l1,h,l);
2360: if ( !prev )
2361: nd_hcf = l1;
2362: else
2363: NEXT(prev) = l1;
2364: }
2365: }
2366: }
2367: #else
2368: void register_hcf(NDV p)
2369: {
2370: DCP dc,t;
2371: P hc,h,q;
2372: Q dmy;
2373: int c;
2374: NODE l,l1,prev;
2375:
2376: hc = p->body->c.p;
2377: if ( NUM(hc) ) return;
2378: ptozp(hc,1,&dmy,&h);
2379: #if 1
2380: for ( l = nd_hcf; l; l = NEXT(l) ) {
2381: while ( 1 ) {
2382: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2383: else break;
2384: }
2385: }
2386: if ( NUM(h) ) return;
2387: #endif
2388: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2389: c = compp(nd_vc,h,(P)BDY(l));
2390: if ( c >= 0 ) break;
2391: }
2392: if ( !l || c > 0 ) {
2393: MKNODE(l1,h,l);
2394: if ( !prev )
2395: nd_hcf = l1;
2396: else
2397: NEXT(prev) = l1;
2398: }
2399: }
2400: #endif
2401:
2402: int do_diagonalize(int sugar,int m)
2403: {
1.6 noro 2404: int i,nh,stat;
2405: NODE r,g,t;
2406: ND h,nf,s,head;
2407: NDV nfv;
2408: Q q;
2409: P nm,nmp,dn,mnp,dnp,cont,cont1;
2410: union oNDC hc;
2411: NODE node;
2412: LIST l;
2413: Z iq;
1.1 noro 2414:
1.6 noro 2415: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2416: if ( nd_gentrace ) {
2417: /* Trace = [1,index,1,1] */
2418: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2419: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2420: }
2421: if ( nd_demand )
2422: nfv = ndv_load(i);
2423: else
2424: nfv = nd_ps[i];
2425: s = ndvtond(m,nfv);
2426: s = nd_separate_head(s,&head);
2427: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2428: if ( !stat ) return 0;
2429: ndv_free(nfv);
2430: hc = HCU(nf); nd_removecont(m,nf);
2431: /* exact division */
2432: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2433: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2434: nfv = ndtondv(m,nf);
2435: nd_free(nf);
2436: nd_bound[i] = ndv_compute_bound(nfv);
2437: if ( !m ) register_hcf(nfv);
2438: if ( nd_demand ) {
2439: ndv_save(nfv,i);
2440: ndv_free(nfv);
2441: } else
2442: nd_ps[i] = nfv;
2443: }
2444: return 1;
1.1 noro 2445: }
2446:
2447: LIST compute_splist()
2448: {
2449: NODE g,tn0,tn,node;
2450: LIST l0;
2451: ND_pairs d,t;
2452: int i;
2453: Z i1,i2;
2454:
2455: g = 0; d = 0;
2456: for ( i = 0; i < nd_psn; i++ ) {
2457: d = update_pairs(d,g,i,0);
2458: g = update_base(g,i);
2459: }
2460: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2461: NEXTNODE(tn0,tn);
1.6 noro 2462: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2463: node = mknode(2,i1,i2); MKLIST(l0,node);
2464: BDY(tn) = l0;
2465: }
2466: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2467: return l0;
2468: }
2469:
2470: /* return value = 0 => input is not a GB */
2471:
2472: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2473: {
1.6 noro 2474: int i,nh,sugar,stat;
2475: NODE r,g,t;
2476: ND_pairs d;
2477: ND_pairs l;
2478: ND h,nf,s,head,nf1;
2479: NDV nfv;
2480: Z q;
2481: union oNDC dn,hc;
2482: int diag_count = 0;
1.41 noro 2483: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2484: P cont;
2485: LIST list;
1.27 noro 2486: struct oEGT eg1,eg2,eg_update;
1.6 noro 2487:
1.27 noro 2488: init_eg(&eg_update);
1.11 noro 2489: Nnd_add = 0;
1.6 noro 2490: g = 0; d = 0;
2491: for ( i = 0; i < nd_psn; i++ ) {
2492: d = update_pairs(d,g,i,gensyz);
2493: g = update_base(g,i);
2494: }
2495: sugar = 0;
2496: while ( d ) {
1.1 noro 2497: again:
1.6 noro 2498: l = nd_minp(d,&d);
2499: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2500: if ( SG(l) != sugar ) {
2501: if ( ishomo ) {
2502: diag_count = 0;
2503: stat = do_diagonalize(sugar,m);
1.1 noro 2504: if ( !stat ) {
1.6 noro 2505: NEXT(l) = d; d = l;
2506: d = nd_reconstruct(0,d);
2507: goto again;
1.1 noro 2508: }
1.6 noro 2509: }
2510: sugar = SG(l);
2511: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2512: }
2513: stat = nd_sp(m,0,l,&h);
2514: if ( !stat ) {
2515: NEXT(l) = d; d = l;
2516: d = nd_reconstruct(0,d);
2517: goto again;
2518: }
1.1 noro 2519: #if USE_GEOBUCKET
1.39 noro 2520: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2521: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2522: #else
1.39 noro 2523: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2524: #endif
1.6 noro 2525: if ( !stat ) {
2526: NEXT(l) = d; d = l;
2527: d = nd_reconstruct(0,d);
2528: goto again;
2529: } else if ( nf ) {
1.41 noro 2530: Nnfnz++;
1.6 noro 2531: if ( checkonly || gensyz ) return 0;
1.1 noro 2532: if ( nd_newelim ) {
2533: if ( nd_module ) {
2534: if ( MPOS(HDL(nf)) > 1 ) return 0;
2535: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2536: }
1.6 noro 2537: if ( DP_Print ) { printf("+"); fflush(stdout); }
2538: hc = HCU(nf);
2539: nd_removecont(m,nf);
2540: if ( !m && nd_nalg ) {
2541: nd_monic(0,&nf);
2542: nd_removecont(m,nf);
2543: }
2544: if ( nd_gentrace ) {
2545: /* exact division */
1.1 noro 2546: cont = ndc_div(m,hc,HCU(nf));
2547: if ( m || !UNIQ(cont) ) {
1.6 noro 2548: t = mknode(4,NULLP,NULLP,NULLP,cont);
2549: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2550: nd_tracelist = t;
2551: }
2552: }
1.6 noro 2553: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2554: nh = ndv_newps(m,nfv,0);
1.6 noro 2555: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2556: diag_count = 0;
2557: stat = do_diagonalize(sugar,m);
2558: if ( !stat ) {
2559: NEXT(l) = d; d = l;
2560: d = nd_reconstruct(1,d);
2561: goto again;
1.1 noro 2562: }
1.6 noro 2563: }
1.27 noro 2564: get_eg(&eg1);
1.6 noro 2565: d = update_pairs(d,g,nh,0);
1.27 noro 2566: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2567: g = update_base(g,nh);
2568: FREENDP(l);
1.41 noro 2569: } else {
2570: Nnfz++;
2571: if ( nd_gentrace && gensyz ) {
2572: nd_tracelist = reverse_node(nd_tracelist);
2573: MKLIST(list,nd_tracelist);
2574: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2575: MKNODE(t,list,nd_alltracelist);
2576: nd_alltracelist = t; nd_tracelist = 0;
2577: }
2578: if ( DP_Print ) { printf("."); fflush(stdout); }
2579: FREENDP(l);
2580: }
1.36 noro 2581: }
2582: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2583: if ( !checkonly && DP_Print ) {
2584: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2585: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2586: fflush(asir_out);
2587: }
2588: if ( DP_Print ) {
2589: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2590: }
1.36 noro 2591: return g;
1.1 noro 2592: }
2593:
1.30 noro 2594: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47 noro 2595: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30 noro 2596: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47 noro 2597: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24 noro 2598:
2599: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2600: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2601:
2602: void _copydl(int n,DL d1,DL d2);
2603: void _subfromdl(int n,DL d1,DL d2);
2604: extern int (*cmpdl)(int n,DL d1,DL d2);
2605:
2606: NODE insert_sig(NODE l,SIG s)
2607: {
2608: int pos;
2609: DL sig;
2610: struct oNODE root;
2611: NODE p,prev,r;
2612: SIG t;
2613:
2614: pos = s->pos; sig = DL(s);
2615: root.next = l; prev = &root;
2616: for ( p = l; p; p = p->next ) {
2617: t = (SIG)p->body;
2618: if ( t->pos == pos ) {
2619: if ( _dl_redble(DL(t),sig,nd_nvar) )
2620: return root.next;
2621: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2622: // remove p
2623: prev->next = p->next;
1.45 noro 2624: else
2625: prev = p;
1.24 noro 2626: } else
2627: prev = p;
2628: }
1.27 noro 2629: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2630: for ( p = &root; p->next; p = p->next );
2631: p->next = r;
2632: // r->next = root.next;
2633: // return r;
2634: return root.next;
1.24 noro 2635: }
2636:
2637: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2638: {
2639: struct oND_pairs root;
2640: ND_pairs prev,p;
2641: SIG spsig;
2642:
2643: root.next = d;
2644: prev = &root; p = d;
2645: while ( p ) {
2646: spsig = p->sig;
1.26 noro 2647: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2648: // remove p
2649: prev->next = p->next;
1.26 noro 2650: Nsyz++;
2651: } else
1.24 noro 2652: prev = p;
2653: p = p->next;
2654: }
2655: return (ND_pairs)root.next;
2656: }
2657:
1.29 noro 2658: int _dl_redble_ext(DL,DL,DL,int);
2659:
1.28 noro 2660: int small_lcm(ND_pairs l)
2661: {
2662: SIG sig;
2663: int i;
1.29 noro 2664: NODE t;
1.28 noro 2665: static DL lcm,mul,quo;
1.44 noro 2666: static int nvar = 0;
1.28 noro 2667:
1.38 noro 2668: if ( nd_sba_largelcm ) return 0;
1.28 noro 2669: if ( nvar < nd_nvar ) {
2670: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2671: }
2672: sig = l->sig;
2673: _ndltodl(l->lcm,lcm);
1.29 noro 2674: #if 0
1.28 noro 2675: for ( i = 0; i < nd_psn; i++ ) {
2676: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2677: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2678: _ndltodl(DL(nd_psh[i]),mul);
2679: _addtodl(nd_nvar,quo,mul);
2680: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2681: break;
2682: }
2683: }
2684: if ( i < nd_psn ) return 1;
2685: else return 0;
1.29 noro 2686: #else
2687: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2688: i = (long)BDY(t);
2689: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2690: _ndltodl(DL(nd_psh[i]),mul);
2691: _addtodl(nd_nvar,quo,mul);
2692: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2693: break;
2694: }
2695: }
2696: if ( t ) return 1;
2697: else return 0;
2698: #endif
1.28 noro 2699: }
2700:
1.41 noro 2701: ND_pairs find_smallest_lcm(ND_pairs l)
2702: {
2703: SIG sig;
2704: int i,minindex;
2705: NODE t;
2706: ND_pairs r;
2707: struct oSIG sig1;
2708: static DL mul,quo,minlm;
1.44 noro 2709: static int nvar = 0;
1.41 noro 2710:
2711: if ( nvar < nd_nvar ) {
2712: nvar = nd_nvar;
2713: NEWDL(quo,nvar); NEWDL(mul,nvar);
2714: NEWDL(minlm,nvar);
2715: }
2716: sig = l->sig;
2717: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2718: _ndltodl(l->lcm,minlm); minindex = -1;
2719: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2720: i = (long)BDY(t);
2721: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2722: _ndltodl(DL(nd_psh[i]),mul);
2723: _addtodl(nd_nvar,quo,mul);
2724: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2725: minindex = i;
1.47 noro 2726: break;
1.41 noro 2727: _copydl(nd_nvar,mul,minlm);
2728: }
2729: }
2730: }
2731: // l->lcm is minimal; return l itself
2732: if ( minindex < 0 ) return l;
1.47 noro 2733: else return 0;
1.41 noro 2734: for ( i = 0; i < nd_psn; i++ ) {
2735: if ( i == minindex ) continue;
2736: _ndltodl(DL(nd_psh[i]),mul);
2737: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2738: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2739: sig1.pos = nd_ps[i]->sig->pos;
2740: sig1.dl = quo;
2741: if ( comp_sig(sig,&sig1) > 0 ) {
2742: // printf("X");
2743: NEWND_pairs(r);
2744: r->sig = sig;
2745: r->i1 = minindex;
2746: r->i2 = i;
2747: dltondl(nd_nvar,minlm,r->lcm);
2748: r->next = 0;
2749: return r;
2750: }
2751: }
2752: }
2753: // there is no suitable spair
2754: return 0;
2755: }
2756:
1.28 noro 2757: ND_pairs remove_large_lcm(ND_pairs d)
2758: {
2759: struct oND_pairs root;
2760: ND_pairs prev,p;
2761:
2762: root.next = d;
2763: prev = &root; p = d;
2764: while ( p ) {
1.41 noro 2765: #if 0
1.28 noro 2766: if ( small_lcm(p) ) {
2767: // remove p
2768: prev->next = p->next;
2769: } else
1.41 noro 2770: #else
2771: if ( find_smallest_lcm(p) == 0 ) {
2772: // remove p
2773: prev->next = p->next;
2774: } else
2775: #endif
1.28 noro 2776: prev = p;
2777: p = p->next;
2778: }
2779: return (ND_pairs)root.next;
2780: }
2781:
1.26 noro 2782: struct oEGT eg_create,eg_newpairs,eg_merge;
2783:
1.30 noro 2784: NODE conv_ilist_s(int demand,int trace,int **indp);
2785:
1.44 noro 2786: // S(fj*ei-fi*ej)
2787:
1.45 noro 2788: void _subdl(int,DL,DL,DL);
2789:
1.44 noro 2790: SIG trivial_sig(int i,int j)
2791: {
1.45 noro 2792: static DL lcm;
1.44 noro 2793: static struct oSIG sigi,sigj;
2794: static int nvar = 0;
2795: SIG sig;
2796:
2797: if ( nvar != nd_nvar ) {
1.45 noro 2798: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
2799: }
1.46 noro 2800: if ( nd_sba_inputisgb != 0 ) {
1.45 noro 2801: lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
2802: sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
2803: sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
2804: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2805: else sig = dup_sig(&sigj);
2806: } else {
2807: sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
2808: sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
2809: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2810: else sig = dup_sig(&sigj);
1.44 noro 2811: }
2812: return sig;
2813: }
2814:
1.47 noro 2815: int nd_minsig(ND_pairs *d)
2816: {
2817: int min,i,ret;
2818:
2819: min = -1;
2820: for ( i = 0; i < nd_nbase; i++ ) {
2821: if ( d[i] != 0 ) {
2822: if ( min < 0 ) min = i;
2823: else {
2824: ret = comp_sig(d[i]->sig,d[min]->sig);
2825: if ( ret < 0 ) min = i;
2826: }
2827: }
2828: }
2829: return min;
2830: }
2831:
2832: int dlength(ND_pairs d)
2833: {
2834: int i;
2835: for ( i = 0; d; d = d->next, i++ );
2836: return i;
2837: }
2838:
1.44 noro 2839: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24 noro 2840: {
1.39 noro 2841: int i,j,nh,sugar,stat,pos;
1.30 noro 2842: NODE r,t,g;
1.47 noro 2843: ND_pairs *d;
1.41 noro 2844: ND_pairs l,l1;
1.24 noro 2845: ND h,nf,s,head,nf1;
2846: NDV nfv;
2847: Z q;
2848: union oNDC dn,hc;
2849: P cont;
2850: LIST list;
2851: SIG sig;
1.29 noro 2852: NODE *syzlist;
1.47 noro 2853: int ngen,ind;
1.41 noro 2854: int Nnominimal,Nredundant;
1.28 noro 2855: DL lcm,quo,mul;
1.47 noro 2856: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero,eg_minsig,eg_smallest;
2857: int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24 noro 2858:
1.27 noro 2859: init_eg(&eg_remove);
1.29 noro 2860: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47 noro 2861: d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
2862: nd_nbase = nd_psn;
1.26 noro 2863: Nsyz = 0;
1.24 noro 2864: Nnd_add = 0;
1.41 noro 2865: Nnominimal = 0;
1.26 noro 2866: Nredundant = 0;
1.44 noro 2867: ngen = nd_psn;
1.48 ! noro 2868: if ( !do_weyl ) {
! 2869: for ( i = 0; i < nd_psn; i++ )
! 2870: for ( j = i+1; j < nd_psn; j++ ) {
! 2871: sig = trivial_sig(i,j);
! 2872: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
! 2873: }
1.29 noro 2874: }
1.47 noro 2875: dlen = 0;
1.41 noro 2876: for ( i = 0; i < nd_psn; i++ ) {
1.47 noro 2877: dlen += update_pairs_array_s(d,i,syzlist);
1.41 noro 2878: }
1.24 noro 2879: sugar = 0;
1.39 noro 2880: pos = 0;
1.26 noro 2881: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2882: init_eg(&eg_create);
2883: init_eg(&eg_merge);
1.47 noro 2884: init_eg(&eg_minsig);
2885: init_eg(&eg_smallest);
1.29 noro 2886: init_eg(&eg_large);
1.31 noro 2887: init_eg(&eg_nf);
2888: init_eg(&eg_nfzero);
1.47 noro 2889: while ( 1 ) {
2890: if ( DP_Print && dlen%100 == 0 ) fprintf(asir_out,"(%d)",dlen);
2891: again :
2892: get_eg(&eg1);
2893: ind = nd_minsig(d);
2894: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
2895: if ( ind < 0 ) break;
2896: l = d[ind];
2897: // printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
2898: get_eg(&eg1);
1.41 noro 2899: l1 = find_smallest_lcm(l);
1.47 noro 2900: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41 noro 2901: if ( l1 == 0 ) {
1.47 noro 2902: d[ind] = d[ind]->next; dlen--;
1.41 noro 2903: if ( DP_Print ) fprintf(asir_out,"M");
2904: Nnominimal++;
2905: continue;
2906: }
2907: if ( SG(l1) != sugar ) {
2908: sugar = SG(l1);
2909: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2910: }
2911: sig = l1->sig;
2912: if ( DP_Print && nd_sba_pot ) {
2913: if ( sig->pos != pos ) {
2914: fprintf(asir_out,"[%d]",sig->pos);
2915: pos = sig->pos;
2916: }
2917: }
2918: stat = nd_sp(m,0,l1,&h);
1.24 noro 2919: if ( !stat ) {
1.47 noro 2920: nd_reconstruct_s(0,d);
1.24 noro 2921: goto again;
2922: }
1.31 noro 2923: get_eg(&eg1);
1.25 noro 2924: #if USE_GEOBUCKET
1.39 noro 2925: 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 2926: #else
1.39 noro 2927: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2928: #endif
1.31 noro 2929: get_eg(&eg2);
1.24 noro 2930: if ( !stat ) {
1.47 noro 2931: nd_reconstruct_s(0,d);
1.24 noro 2932: goto again;
2933: } else if ( stat == -1 ) {
1.47 noro 2934: d[ind] = d[ind]->next; dlen--;
1.41 noro 2935: Nnfs++;
1.24 noro 2936: if ( DP_Print ) { printf("S"); fflush(stdout); }
2937: } else if ( nf ) {
1.47 noro 2938: d[ind] = d[ind]->next; dlen--;
1.41 noro 2939: Nnfnz++;
2940: if ( DP_Print ) {
2941: if ( nd_sba_redundant_check ) {
2942: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
2943: Nredundant++;
2944: printf("R");
2945: } else
2946: printf("+");
2947: } else
2948: printf("+");
2949: fflush(stdout);
2950: }
1.31 noro 2951: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 2952: hc = HCU(nf);
2953: nd_removecont(m,nf);
2954: nfv = ndtondv(m,nf); nd_free(nf);
2955: nh = ndv_newps(m,nfv,0);
1.26 noro 2956:
1.47 noro 2957: dlen += update_pairs_array_s(d,nh,syzlist);
1.29 noro 2958: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2959: } else {
1.47 noro 2960: d[ind] = d[ind]->next; dlen--;
1.41 noro 2961: Nnfz++;
1.31 noro 2962: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 2963: // syzygy
1.27 noro 2964: get_eg(&eg1);
1.47 noro 2965: nsyz = Nsyz;
2966: d[sig->pos] = remove_spair_s(d[sig->pos],sig);
2967: dlen -= Nsyz-nsyz;
1.27 noro 2968: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2969: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2970: if ( DP_Print ) { printf("."); fflush(stdout); }
2971: }
2972: }
1.30 noro 2973: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2974: if ( DP_Print ) {
1.47 noro 2975: 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 2976: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
2977: fflush(stdout);
2978: if ( nd_sba_redundant_check )
2979: printf("Nredundant=%d\n",Nredundant);
1.26 noro 2980: fflush(stdout);
2981: print_eg("create",&eg_create);
2982: print_eg("merge",&eg_merge);
1.47 noro 2983: print_eg("minsig",&eg_minsig);
2984: print_eg("smallest",&eg_smallest);
2985: print_eg("remove",&eg_remove);
2986: print_eg("nf",&eg_nf);
2987: print_eg("nfzero",&eg_nfzero);
2988: printf("\n");
2989: }
2990: if ( nd_sba_syz ) {
1.27 noro 2991: print_eg("remove",&eg_remove);
1.31 noro 2992: print_eg("nf",&eg_nf);
2993: print_eg("nfzero",&eg_nfzero);
1.27 noro 2994: printf("\n");
1.26 noro 2995: }
1.44 noro 2996: if ( nd_sba_syz ) {
2997: NODE hsyz,tsyz,prev;
2998:
2999: hsyz = 0;
3000: for ( i = 0; i < ngen; i++ ) {
3001: tsyz = syzlist[i];
3002: for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
3003: BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
3004: if ( prev != 0 ) {
3005: prev->next = hsyz; hsyz = syzlist[i];
3006: }
3007: }
3008: *syzp = hsyz;
3009: } else *syzp = 0;
1.24 noro 3010: return g;
3011: }
3012:
1.1 noro 3013: /* splist = [[i1,i2],...] */
3014:
3015: int check_splist(int m,NODE splist)
3016: {
3017: NODE t,p;
3018: ND_pairs d,r,l;
3019: int stat;
3020: ND h,nf;
3021:
3022: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3023: p = BDY((LIST)BDY(t));
1.6 noro 3024: NEXTND_pairs(d,r);
3025: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3026: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3027: SG(r) = TD(LCM(r)); /* XXX */
3028: }
3029: if ( d ) NEXT(r) = 0;
3030:
1.6 noro 3031: while ( d ) {
1.1 noro 3032: again:
1.6 noro 3033: l = nd_minp(d,&d);
3034: stat = nd_sp(m,0,l,&h);
3035: if ( !stat ) {
3036: NEXT(l) = d; d = l;
3037: d = nd_reconstruct(0,d);
3038: goto again;
3039: }
1.39 noro 3040: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 3041: if ( !stat ) {
3042: NEXT(l) = d; d = l;
3043: d = nd_reconstruct(0,d);
3044: goto again;
3045: } else if ( nf ) return 0;
1.1 noro 3046: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 3047: }
1.1 noro 3048: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
3049: return 1;
3050: }
3051:
3052: int check_splist_f4(int m,NODE splist)
3053: {
3054: UINT *s0vect;
1.6 noro 3055: PGeoBucket bucket;
1.1 noro 3056: NODE p,rp0,t;
3057: ND_pairs d,r,l,ll;
3058: int col,stat;
3059:
3060: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3061: p = BDY((LIST)BDY(t));
1.6 noro 3062: NEXTND_pairs(d,r);
3063: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3064: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3065: SG(r) = TD(LCM(r)); /* XXX */
3066: }
3067: if ( d ) NEXT(r) = 0;
3068:
1.6 noro 3069: while ( d ) {
3070: l = nd_minsugarp(d,&d);
3071: bucket = create_pbucket();
3072: stat = nd_sp_f4(m,0,l,bucket);
3073: if ( !stat ) {
3074: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3075: NEXT(ll) = d; d = l;
3076: d = nd_reconstruct(0,d);
3077: continue;
3078: }
3079: if ( bucket->m < 0 ) continue;
3080: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
3081: if ( !col ) {
3082: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3083: NEXT(ll) = d; d = l;
3084: d = nd_reconstruct(0,d);
3085: continue;
1.1 noro 3086: }
1.6 noro 3087: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
3088: }
3089: return 1;
1.1 noro 3090: }
3091:
3092: int do_diagonalize_trace(int sugar,int m)
3093: {
1.6 noro 3094: int i,nh,stat;
3095: NODE r,g,t;
3096: ND h,nf,nfq,s,head;
3097: NDV nfv,nfqv;
3098: Q q,den,num;
3099: union oNDC hc;
3100: NODE node;
3101: LIST l;
3102: Z iq;
3103: P cont,cont1;
1.1 noro 3104:
1.6 noro 3105: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3106: if ( nd_gentrace ) {
3107: /* Trace = [1,index,1,1] */
3108: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3109: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3110: }
3111: /* for nd_ps */
3112: s = ndvtond(m,nd_ps[i]);
3113: s = nd_separate_head(s,&head);
3114: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3115: if ( !stat ) return 0;
3116: nf = nd_add(m,head,nf);
3117: ndv_free(nd_ps[i]);
3118: nd_ps[i] = ndtondv(m,nf);
3119: nd_free(nf);
3120:
3121: /* for nd_ps_trace */
3122: if ( nd_demand )
3123: nfv = ndv_load(i);
3124: else
3125: nfv = nd_ps_trace[i];
3126: s = ndvtond(0,nfv);
3127: s = nd_separate_head(s,&head);
3128: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3129: if ( !stat ) return 0;
3130: ndv_free(nfv);
3131: hc = HCU(nf); nd_removecont(0,nf);
3132: /* exact division */
1.1 noro 3133: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3134: if ( nd_gentrace ) finalize_tracelist(i,cont);
3135: nfv = ndtondv(0,nf);
3136: nd_free(nf);
3137: nd_bound[i] = ndv_compute_bound(nfv);
3138: register_hcf(nfv);
3139: if ( nd_demand ) {
3140: ndv_save(nfv,i);
3141: ndv_free(nfv);
3142: } else
3143: nd_ps_trace[i] = nfv;
3144: }
3145: return 1;
1.1 noro 3146: }
3147:
3148: static struct oEGT eg_invdalg;
3149: struct oEGT eg_le;
3150:
3151: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3152: {
3153: NODE tn;
3154: P p1;
3155:
3156: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3157: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3158: }
3159: *r = p;
3160: }
3161:
3162: NODE nd_gb_trace(int m,int ishomo,int **indp)
3163: {
1.6 noro 3164: int i,nh,sugar,stat;
3165: NODE r,g,t;
3166: ND_pairs d;
3167: ND_pairs l;
3168: ND h,nf,nfq,s,head;
3169: NDV nfv,nfqv;
3170: Z q,den,num;
3171: P hc;
3172: union oNDC dn,hnfq;
3173: struct oEGT eg_monic,egm0,egm1;
3174: int diag_count = 0;
3175: P cont;
3176: LIST list;
3177:
3178: init_eg(&eg_monic);
3179: init_eg(&eg_invdalg);
3180: init_eg(&eg_le);
3181: g = 0; d = 0;
3182: for ( i = 0; i < nd_psn; i++ ) {
3183: d = update_pairs(d,g,i,0);
3184: g = update_base(g,i);
3185: }
3186: sugar = 0;
3187: while ( d ) {
1.1 noro 3188: again:
1.6 noro 3189: l = nd_minp(d,&d);
3190: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3191: if ( SG(l) != sugar ) {
1.1 noro 3192: #if 1
1.6 noro 3193: if ( ishomo ) {
3194: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3195: stat = do_diagonalize_trace(sugar,m);
3196: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3197: diag_count = 0;
1.1 noro 3198: if ( !stat ) {
1.6 noro 3199: NEXT(l) = d; d = l;
3200: d = nd_reconstruct(1,d);
3201: goto again;
1.1 noro 3202: }
1.6 noro 3203: }
3204: #endif
3205: sugar = SG(l);
3206: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3207: }
3208: stat = nd_sp(m,0,l,&h);
3209: if ( !stat ) {
3210: NEXT(l) = d; d = l;
3211: d = nd_reconstruct(1,d);
3212: goto again;
3213: }
1.1 noro 3214: #if USE_GEOBUCKET
1.39 noro 3215: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3216: #else
1.39 noro 3217: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3218: #endif
1.6 noro 3219: if ( !stat ) {
3220: NEXT(l) = d; d = l;
3221: d = nd_reconstruct(1,d);
3222: goto again;
3223: } else if ( nf ) {
3224: if ( nd_demand ) {
3225: nfqv = ndv_load(nd_psn);
3226: nfq = ndvtond(0,nfqv);
3227: } else
3228: nfq = 0;
3229: if ( !nfq ) {
1.39 noro 3230: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3231: NEXT(l) = d; d = l;
3232: d = nd_reconstruct(1,d);
3233: goto again;
3234: }
3235: }
3236: if ( nfq ) {
3237: /* m|HC(nfq) => failure */
3238: if ( nd_vc ) {
3239: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3240: } else
3241: q = HCZ(nfq);
3242: if ( !remqi((Q)q,m) ) return 0;
3243:
3244: if ( DP_Print ) { printf("+"); fflush(stdout); }
3245: hnfq = HCU(nfq);
3246: if ( nd_nalg ) {
3247: /* m|DN(HC(nf)^(-1)) => failure */
3248: get_eg(&egm0);
3249: if ( !nd_monic(m,&nfq) ) return 0;
3250: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3251: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3252: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3253: } else {
3254: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3255: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3256: }
3257: if ( nd_gentrace ) {
3258: /* exact division */
3259: cont = ndc_div(0,hnfq,HCU(nfqv));
3260: if ( !UNIQ(cont) ) {
3261: t = mknode(4,NULLP,NULLP,NULLP,cont);
3262: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3263: nd_tracelist = t;
3264: }
3265: }
1.24 noro 3266: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3267: if ( ishomo && ++diag_count == diag_period ) {
3268: diag_count = 0;
3269: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3270: stat = do_diagonalize_trace(sugar,m);
3271: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3272: if ( !stat ) {
1.1 noro 3273: NEXT(l) = d; d = l;
3274: d = nd_reconstruct(1,d);
3275: goto again;
1.6 noro 3276: }
1.1 noro 3277: }
1.6 noro 3278: d = update_pairs(d,g,nh,0);
3279: g = update_base(g,nh);
3280: } else {
3281: if ( DP_Print ) { printf("*"); fflush(stdout); }
3282: }
3283: } else {
3284: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3285: }
1.6 noro 3286: FREENDP(l);
3287: }
3288: if ( nd_nalg ) {
3289: if ( DP_Print ) {
3290: print_eg("monic",&eg_monic);
3291: print_eg("invdalg",&eg_invdalg);
3292: print_eg("le",&eg_le);
1.1 noro 3293: }
1.6 noro 3294: }
1.1 noro 3295: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3296: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3297: return g;
1.1 noro 3298: }
3299:
3300: int ndv_compare(NDV *p1,NDV *p2)
3301: {
3302: return DL_COMPARE(HDL(*p1),HDL(*p2));
3303: }
3304:
3305: int ndv_compare_rev(NDV *p1,NDV *p2)
3306: {
3307: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3308: }
3309:
3310: int ndvi_compare(NDVI p1,NDVI p2)
3311: {
3312: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3313: }
3314:
3315: int ndvi_compare_rev(NDVI p1,NDVI p2)
3316: {
3317: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3318: }
3319:
3320: NODE ndv_reduceall(int m,NODE f)
3321: {
3322: int i,j,n,stat;
3323: ND nf,g,head;
3324: NODE t,a0,a;
3325: union oNDC dn;
3326: Q q,num,den;
3327: NODE node;
3328: LIST l;
3329: Z iq,jq;
3330: int *perm;
3331: union oNDC hc;
3332: P cont,cont1;
3333:
3334: if ( nd_nora ) return f;
3335: n = length(f);
1.24 noro 3336: ndv_setup(m,0,f,0,1,0);
1.1 noro 3337: perm = (int *)MALLOC(n*sizeof(int));
3338: if ( nd_gentrace ) {
3339: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3340: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3341: }
3342: for ( i = 0; i < n; ) {
3343: if ( nd_gentrace ) {
3344: /* Trace = [1,index,1,1] */
1.6 noro 3345: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3346: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3347: }
3348: g = ndvtond(m,nd_ps[i]);
3349: g = nd_separate_head(g,&head);
1.6 noro 3350: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3351: if ( !stat )
3352: nd_reconstruct(0,0);
3353: else {
3354: if ( DP_Print ) { printf("."); fflush(stdout); }
3355: ndv_free(nd_ps[i]);
3356: hc = HCU(nf); nd_removecont(m,nf);
3357: if ( nd_gentrace ) {
3358: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3359: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3360: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3361: }
1.6 noro 3362: /* exact division */
1.1 noro 3363: cont = ndc_div(m,hc,HCU(nf));
3364: finalize_tracelist(perm[i],cont);
3365: }
3366: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3367: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3368: i++;
3369: }
3370: }
3371: if ( DP_Print ) { printf("\n"); }
3372: for ( a0 = 0, i = 0; i < n; i++ ) {
3373: NEXTNODE(a0,a);
3374: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3375: else {
3376: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3377: BDY(a) = (pointer)nd_ps[j];
3378: }
3379: }
3380: NEXT(a) = 0;
3381: return a0;
3382: }
3383:
1.41 noro 3384: int ndplength(ND_pairs d)
3385: {
3386: int i;
3387: for ( i = 0; d; i++ ) d = NEXT(d);
3388: return i;
3389: }
3390:
1.1 noro 3391: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3392: {
3393: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3394: int len0;
1.1 noro 3395:
3396: if ( !g ) return d;
3397: /* for testing */
3398: if ( gensyz && nd_gensyz == 2 ) {
3399: d1 = nd_newpairs(g,t);
3400: if ( !d )
3401: return d1;
3402: else {
3403: nd = d;
3404: while ( NEXT(nd) ) nd = NEXT(nd);
3405: NEXT(nd) = d1;
3406: return d;
3407: }
3408: }
3409: d = crit_B(d,t);
3410: d1 = nd_newpairs(g,t);
1.41 noro 3411: len0 = ndplength(d1);
1.1 noro 3412: d1 = crit_M(d1);
3413: d1 = crit_F(d1);
1.41 noro 3414: NcriMF += len0-ndplength(d1);
1.1 noro 3415: if ( gensyz || do_weyl )
3416: head = d1;
3417: else {
3418: prev = 0; cur = head = d1;
3419: while ( cur ) {
3420: if ( crit_2( cur->i1,cur->i2 ) ) {
3421: remove = cur;
3422: if ( !prev ) head = cur = NEXT(cur);
3423: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3424: FREENDP(remove); Ncri2++;
1.1 noro 3425: } else {
3426: prev = cur; cur = NEXT(cur);
3427: }
3428: }
3429: }
3430: if ( !d )
3431: return head;
3432: else {
3433: nd = d;
3434: while ( NEXT(nd) ) nd = NEXT(nd);
3435: NEXT(nd) = head;
3436: return d;
3437: }
3438: }
3439:
1.24 noro 3440: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3441:
1.30 noro 3442: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3443: {
3444: ND_pairs d1;
1.26 noro 3445: struct oEGT eg1,eg2,eg3;
1.24 noro 3446:
1.30 noro 3447: if ( !t ) return d;
1.26 noro 3448: get_eg(&eg1);
1.30 noro 3449: d1 = nd_newpairs_s(t,syz);
1.26 noro 3450: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3451: d = merge_pairs_s(d,d1);
1.26 noro 3452: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3453: return d;
3454: }
1.1 noro 3455:
1.47 noro 3456: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
3457: {
3458: ND_pairs *d1;
3459: struct oEGT eg1,eg2,eg3;
3460: int i;
3461:
3462: if ( !t ) return 0;
3463: get_eg(&eg1);
3464: Nnewpair = 0;
3465: d1 = nd_newpairs_array_s(t,syz);
3466: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
3467: for ( i = 0; i < nd_nbase; i++ )
3468: d[i] = merge_pairs_s(d[i],d1[i]);
3469: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
3470: return Nnewpair;
3471: }
3472:
1.1 noro 3473: ND_pairs nd_newpairs( NODE g, int t )
3474: {
3475: NODE h;
3476: UINT *dl;
3477: int ts,s,i,t0,min,max;
3478: ND_pairs r,r0;
3479:
3480: dl = DL(nd_psh[t]);
3481: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3482: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3483: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3484: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3485: continue;
3486: if ( nd_gbblock ) {
3487: t0 = (long)BDY(h);
3488: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3489: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3490: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3491: break;
3492: }
3493: if ( nd_gbblock[i] >= 0 )
3494: continue;
3495: }
1.41 noro 3496: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3497: r->i1 = (long)BDY(h);
3498: r->i2 = t;
3499: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3500: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3501: SG(r) = MAX(s,ts) + TD(LCM(r));
3502: /* experimental */
3503: if ( nd_sugarweight )
3504: r->sugar2 = ndl_weight2(r->lcm);
3505: }
3506: if ( r0 ) NEXT(r) = 0;
3507: return r0;
3508: }
3509:
1.46 noro 3510: int sig_cmpdl_op(int n,DL d1,DL d2)
3511: {
3512: int e1,e2,i,j,l;
3513: int *t1,*t2;
3514: int len,head;
3515: struct order_pair *pair;
3516:
3517: len = nd_sba_modord->block_length;
3518: pair = nd_sba_modord->order_pair;
3519:
3520: head = 0;
3521: for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
3522: l = pair[i].length;
3523: switch ( pair[i].order ) {
3524: case 0:
3525: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3526: e1 += t1[j];
3527: e2 += t2[j];
3528: }
3529: if ( e1 > e2 )
3530: return 1;
3531: else if ( e1 < e2 )
3532: return -1;
3533: else {
3534: for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
3535: if ( j >= 0 )
3536: return t1[j] < t2[j] ? 1 : -1;
3537: }
3538: break;
3539: case 1:
3540: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3541: e1 += t1[j];
3542: e2 += t2[j];
3543: }
3544: if ( e1 > e2 )
3545: return 1;
3546: else if ( e1 < e2 )
3547: return -1;
3548: else {
3549: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3550: if ( j < l )
3551: return t1[j] > t2[j] ? 1 : -1;
3552: }
3553: break;
3554: case 2:
3555: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3556: if ( j < l )
3557: return t1[j] > t2[j] ? 1 : -1;
3558: break;
3559: default:
3560: error("sig_cmpdl_op : invalid order"); break;
3561: }
3562: t1 += l; t2 += l; head += l;
3563: }
3564: return 0;
3565: }
3566:
3567: int sig_cmpdl_mat(int n,DL d1,DL d2)
3568: {
3569: int *v,*t1,*t2;
3570: int s,i,j,len;
3571: int **matrix;
3572: static int *w;
3573: static int nvar = 0;
3574:
3575: if ( nvar != n ) {
3576: nvar = n; w = (int *)MALLOC(n*sizeof(int));
3577: }
3578: for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
3579: w[i] = t1[i]-t2[i];
3580: len = nd_sba_modord->row;
3581: matrix = nd_sba_modord->matrix;
3582: for ( j = 0; j < len; j++ ) {
3583: v = matrix[j];
3584: for ( i = 0, s = 0; i < n; i++ )
3585: s += v[i]*w[i];
3586: if ( s > 0 )
3587: return 1;
3588: else if ( s < 0 )
3589: return -1;
3590: }
3591: return 0;
3592: }
3593:
3594: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
3595: {
3596: struct comp_sig_spec *spec;
3597: VL ovl,vl;
3598: V ov;
3599: int i,j,n,nvar,s;
3600: NODE node,t,tn;
3601: struct order_pair *l;
3602: MAT m;
3603: Obj **b;
3604: int **w;
3605: int *a;
3606:
3607: spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
3608: for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
3609: spec->n = nvar = i;
3610: if ( old_vl != 0 ) {
3611: spec->oldv = (int *)MALLOC(nvar*sizeof(int));
3612: for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
3613: ov = ovl->v;
3614: for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
3615: if ( ov == vl->v ) break;
3616: spec->oldv[i] = j;
3617: }
3618: } else
3619: spec->oldv = 0;
3620: if ( !ord || NUM(ord) ) {
3621: switch ( ZTOS((Z)ord) ) {
3622: case 0:
3623: spec->cmpdl = cmpdl_revgradlex; break;
3624: case 1:
3625: spec->cmpdl = cmpdl_gradlex; break;
3626: case 2:
3627: spec->cmpdl = cmpdl_lex; break;
3628: default:
3629: error("create_comp_sig_spec : invalid spec"); break;
3630: }
3631: } else if ( OID(ord) == O_LIST ) {
3632: node = BDY((LIST)ord);
3633: for ( n = 0, t = node; t; t = NEXT(t), n++ );
3634: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
3635: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
3636: tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
3637: tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
3638: s += l[i].length;
3639: }
3640: if ( s != nvar )
3641: error("create_comp_sig_spec : invalid spec");
3642: spec->order_pair = l;
3643: spec->block_length = n;
3644: spec->cmpdl = sig_cmpdl_op;
3645: } else if ( OID(ord) == O_MAT ) {
3646: m = (MAT)ord; b = (Obj **)BDY(m);
3647: if ( m->col != nvar )
3648: error("create_comp_sig_spec : invalid spec");
3649: w = almat(m->row,m->col);
3650: for ( i = 0; i < m->row; i++ )
3651: for ( j = 0; j < m->col; j++ )
3652: w[i][j] = ZTOS((Q)b[i][j]);
3653: spec->row = m->row;
3654: spec->matrix = w;
3655: spec->cmpdl = sig_cmpdl_mat;
3656: } else
3657: error("create_comp_sig_spec : invalid spec");
3658: if ( weight != 0 ) {
3659: node = BDY((LIST)weight);
3660: a = (int *)MALLOC(nvar*sizeof(int));
3661: for ( i = 0; i < nvar; i++, node = NEXT(node) )
3662: a[i] = ZTOS((Z)BDY(node));
3663: spec->weight = a;
3664: }
3665: return spec;
3666: }
3667:
3668: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
3669:
3670: int comp_sig_monomial(int n,DL d1,DL d2)
3671: {
3672: static DL m1,m2;
3673: static int nvar = 0;
3674: int *oldv,*weight;
3675: int i,w1,w2;
3676:
3677: if ( nvar != n ) {
3678: nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
3679: }
3680: if ( !nd_sba_modord )
3681: return (*cmpdl)(n,d1,d2);
3682: else {
3683: weight = nd_sba_modord->weight;
3684: oldv = nd_sba_modord->oldv;
3685: if ( oldv ) {
3686: for ( i = 0; i < n; i++ ) {
3687: m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
3688: }
3689: } else {
3690: for ( i = 0; i < n; i++ ) {
3691: m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
3692: }
3693: }
3694: for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
3695: w1 += SIG_MUL_WEIGHT(m1->d[i],i);
3696: w2 += SIG_MUL_WEIGHT(m2->d[i],i);
3697: }
3698: m1->td = w1; m2->td = w2;
3699: return (*nd_sba_modord->cmpdl)(n,m1,m2);
3700: }
3701: }
3702:
1.24 noro 3703: int comp_sig(SIG s1,SIG s2)
3704: {
1.38 noro 3705: if ( nd_sba_pot ) {
3706: if ( s1->pos > s2->pos ) return 1;
3707: else if ( s1->pos < s2->pos ) return -1;
1.46 noro 3708: else return comp_sig_monomial(nd_nvar,s1->dl,s2->dl);
1.38 noro 3709: } else {
3710: static DL m1,m2;
1.44 noro 3711: static int nvar = 0;
1.38 noro 3712: int ret;
3713:
3714: if ( nvar != nd_nvar ) {
3715: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3716: }
3717: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3718: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.47 noro 3719: if ( !nd_sba_modord )
3720: ret = (*cmpdl)(nd_nvar,m1,m2);
3721: else
3722: ret = comp_sig_monomial(nd_nvar,m1,m2);
1.38 noro 3723: if ( ret != 0 ) return ret;
3724: else if ( s1->pos > s2->pos ) return 1;
3725: else if ( s1->pos < s2->pos ) return -1;
3726: else return 0;
1.24 noro 3727: }
3728: }
3729:
3730: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3731: {
3732: int ret,s1,s2;
3733: RHist p1,p2;
3734: static int wpd;
3735: static UINT *lcm;
3736:
3737: sp->i1 = i1;
3738: sp->i2 = i2;
3739: p1 = nd_psh[i1];
3740: p2 = nd_psh[i2];
3741: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3742: s1 = SG(p1)-TD(DL(p1));
3743: s2 = SG(p2)-TD(DL(p2));
3744: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3745:
1.26 noro 3746: if ( wpd != nd_wpd ) {
1.24 noro 3747: wpd = nd_wpd;
3748: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3749: }
3750: // DL(sig1) <- sp->lcm
3751: // DL(sig1) -= DL(p1)
3752: // DL(sig1) += DL(p1->sig)
1.27 noro 3753: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3754: _ndltodl(lcm,DL(sig1));
3755: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3756: sig1->pos = p1->sig->pos;
3757:
3758: // DL(sig2) <- sp->lcm
3759: // DL(sig2) -= DL(p2)
3760: // DL(sig2) += DL(p2->sig)
1.27 noro 3761: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3762: _ndltodl(lcm,DL(sig2));
3763: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3764: sig2->pos = p2->sig->pos;
3765:
3766: ret = comp_sig(sig1,sig2);
3767: if ( ret == 0 ) return 0;
3768: else if ( ret > 0 ) sp->sig = sig1;
3769: else sp->sig = sig2;
3770: return 1;
3771: }
3772:
3773: SIG dup_sig(SIG sig)
3774: {
3775: SIG r;
3776:
3777: if ( !sig ) return 0;
3778: else {
3779: NEWSIG(r);
3780: _copydl(nd_nvar,DL(sig),DL(r));
3781: r->pos = sig->pos;
3782: return r;
3783: }
3784: }
3785:
3786: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3787: {
3788: to->i1 = from->i1;
3789: to->i2 = from->i2;
3790: to->sugar = from->sugar;
3791: to->sugar2 = from->sugar2;
3792: ndl_copy(from->lcm,to->lcm);
3793: to->sig = dup_sig(from->sig);
3794: }
3795:
3796: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3797: {
3798: struct oND_pairs root;
3799: ND_pairs q1,q2,r0,r;
3800: int ret;
3801:
3802: r = &root;
3803: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3804: ret = comp_sig(q1->sig,q2->sig);
3805: if ( ret < 0 ) {
3806: r->next = q1; r = q1; q1 = q1->next;
3807: } else if ( ret > 0 ) {
3808: r->next = q2; r = q2; q2 = q2->next;
3809: } else {
1.47 noro 3810: Nnewpair--;
1.24 noro 3811: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 3812: Nsamesig++;
1.24 noro 3813: if ( ret < 0 ) {
3814: r->next = q1; r = q1; q1 = q1->next;
3815: q2 = q2->next;
3816: } else {
3817: r->next = q2; r = q2; q2 = q2->next;
3818: q1 = q1->next;
3819: }
3820: }
3821: }
3822: if ( q1 ) {
3823: r->next = q1;
3824: } else {
3825: r->next = q2;
3826: }
3827: return root.next;
3828: }
3829:
3830: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3831: {
3832: ND_pairs p,prev;
1.47 noro 3833: int ret=1;
1.24 noro 3834:
3835: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3836: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3837: break;
3838: }
3839: if ( ret == 0 ) {
3840: ret = DL_COMPARE(s->lcm,p->lcm);
3841: if ( ret < 0 ) {
3842: // replace p with s
3843: s->next = p->next;
3844: if ( prev == 0 ) {
3845: return s;
3846: } else {
3847: prev->next = s;
3848: return l;
3849: }
3850: } else
3851: return l;
3852: } else {
3853: // insert s between prev and p
1.47 noro 3854: Nnewpair++;
1.24 noro 3855: s->next = p;
3856: if ( prev == 0 ) {
3857: return s;
3858: } else {
3859: prev->next = s;
3860: return l;
3861: }
3862: }
3863: }
3864:
1.29 noro 3865: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3866: {
3867: int i;
3868:
3869: if ( d1->td > d2->td )
3870: return 0;
3871: for ( i = nvar-1; i >= 0; i-- )
3872: if ( d1->d[i] > d2->d[i] )
3873: break;
3874: if ( i >= 0 )
3875: return 0;
3876: else
3877: return 1;
3878: }
3879:
1.30 noro 3880: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3881: {
3882: NODE h,s;
3883: UINT *dl;
1.27 noro 3884: int ts,ret,i;
1.24 noro 3885: ND_pairs r,r0,_sp,sp;
1.29 noro 3886: SIG spsig,tsig;
1.44 noro 3887: static int nvar = 0;
1.29 noro 3888: static SIG _sig1,_sig2;
1.26 noro 3889: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3890:
3891: NEWND_pairs(_sp);
1.29 noro 3892: if ( !_sig1 || nvar != nd_nvar ) {
3893: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3894: }
1.24 noro 3895: r0 = 0;
1.27 noro 3896: for ( i = 0; i < t; i++ ) {
3897: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3898: if ( ret ) {
3899: spsig = _sp->sig;
1.29 noro 3900: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3901: tsig = (SIG)s->body;
1.29 noro 3902: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3903: break;
3904: }
3905: if ( s == 0 ) {
3906: NEWND_pairs(sp);
3907: dup_ND_pairs(sp,_sp);
3908: r0 = insert_pair_s(r0,sp);
1.26 noro 3909: } else
3910: Nsyz++;
1.24 noro 3911: }
3912: }
3913: return r0;
3914: }
3915:
1.47 noro 3916: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
3917: {
3918: NODE h,s;
3919: UINT *dl;
3920: int ts,ret,i;
3921: ND_pairs r,r0,_sp,sp;
3922: ND_pairs *d;
3923: SIG spsig,tsig;
3924: static int nvar = 0;
3925: static SIG _sig1,_sig2;
3926: struct oEGT eg1,eg2,eg3,eg4;
3927:
3928: NEWND_pairs(_sp);
3929: if ( !_sig1 || nvar != nd_nvar ) {
3930: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3931: }
3932: d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
3933: Nnewpair = 0;
3934: for ( i = 0; i < t; i++ ) {
3935: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
3936: if ( ret ) {
3937: spsig = _sp->sig;
3938: for ( s = syz[spsig->pos]; s; s = s->next ) {
3939: tsig = (SIG)s->body;
3940: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
3941: break;
3942: }
3943: if ( s == 0 ) {
3944: NEWND_pairs(sp);
3945: dup_ND_pairs(sp,_sp);
3946: d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
3947: } else
3948: Nsyz++;
3949: }
3950: }
3951: return d;
3952: }
3953:
1.1 noro 3954: /* ipair = [i1,i2],[i1,i2],... */
3955: ND_pairs nd_ipairtospair(NODE ipair)
3956: {
3957: int s1,s2;
3958: NODE tn,t;
3959: ND_pairs r,r0;
3960:
3961: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3962: NEXTND_pairs(r0,r);
3963: tn = BDY((LIST)BDY(t));
1.6 noro 3964: r->i1 = ZTOS((Q)ARG0(tn));
3965: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3966: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3967: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3968: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3969: SG(r) = MAX(s1,s2) + TD(LCM(r));
3970: /* experimental */
3971: if ( nd_sugarweight )
3972: r->sugar2 = ndl_weight2(r->lcm);
3973: }
3974: if ( r0 ) NEXT(r) = 0;
3975: return r0;
3976: }
3977:
3978: /* kokokara */
3979:
3980: ND_pairs crit_B( ND_pairs d, int s )
3981: {
3982: ND_pairs cur,head,prev,remove;
3983: UINT *t,*tl,*lcm;
3984: int td,tdl;
3985:
3986: if ( !d ) return 0;
3987: t = DL(nd_psh[s]);
3988: prev = 0;
3989: head = cur = d;
3990: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3991: while ( cur ) {
3992: tl = cur->lcm;
3993: if ( ndl_reducible(tl,t) ) {
3994: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3995: if ( !ndl_equal(lcm,tl) ) {
3996: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3997: if (!ndl_equal(lcm,tl)) {
3998: remove = cur;
3999: if ( !prev ) {
4000: head = cur = NEXT(cur);
4001: } else {
4002: cur = NEXT(prev) = NEXT(cur);
4003: }
1.41 noro 4004: FREENDP(remove); NcriB++;
1.1 noro 4005: } else {
4006: prev = cur; cur = NEXT(cur);
4007: }
4008: } else {
4009: prev = cur; cur = NEXT(cur);
4010: }
4011: } else {
4012: prev = cur; cur = NEXT(cur);
4013: }
4014: }
4015: return head;
4016: }
4017:
4018: ND_pairs crit_M( ND_pairs d1 )
4019: {
4020: ND_pairs e,d2,d3,dd,p;
4021: UINT *id,*jd;
4022:
4023: if ( !d1 ) return d1;
4024: for ( dd = 0, e = d1; e; e = d3 ) {
4025: if ( !(d2 = NEXT(e)) ) {
4026: NEXT(e) = dd;
4027: return e;
4028: }
4029: id = LCM(e);
4030: for ( d3 = 0; d2; d2 = p ) {
4031: p = NEXT(d2);
4032: jd = LCM(d2);
4033: if ( ndl_equal(jd,id) )
4034: ;
4035: else if ( TD(jd) > TD(id) )
4036: if ( ndl_reducible(jd,id) ) continue;
4037: else ;
4038: else if ( ndl_reducible(id,jd) ) goto delit;
4039: NEXT(d2) = d3;
4040: d3 = d2;
4041: }
4042: NEXT(e) = dd;
4043: dd = e;
4044: continue;
4045: /**/
4046: delit: NEXT(d2) = d3;
4047: d3 = d2;
4048: for ( ; p; p = d2 ) {
4049: d2 = NEXT(p);
4050: NEXT(p) = d3;
4051: d3 = p;
4052: }
4053: FREENDP(e);
4054: }
4055: return dd;
4056: }
4057:
4058: ND_pairs crit_F( ND_pairs d1 )
4059: {
4060: ND_pairs rest, head,remove;
4061: ND_pairs last, p, r, w;
4062: int s;
4063:
4064: if ( !d1 ) return d1;
4065: for ( head = last = 0, p = d1; NEXT(p); ) {
4066: r = w = equivalent_pairs(p,&rest);
4067: s = SG(r);
4068: w = NEXT(w);
4069: while ( w ) {
4070: if ( crit_2(w->i1,w->i2) ) {
4071: r = w;
4072: w = NEXT(w);
4073: while ( w ) {
4074: remove = w;
4075: w = NEXT(w);
4076: FREENDP(remove);
4077: }
4078: break;
4079: } else if ( SG(w) < s ) {
4080: FREENDP(r);
4081: r = w;
4082: s = SG(r);
4083: w = NEXT(w);
4084: } else {
4085: remove = w;
4086: w = NEXT(w);
4087: FREENDP(remove);
4088: }
4089: }
4090: if ( last ) NEXT(last) = r;
4091: else head = r;
4092: NEXT(last = r) = 0;
4093: p = rest;
4094: if ( !p ) return head;
4095: }
4096: if ( !last ) return p;
4097: NEXT(last) = p;
4098: return head;
4099: }
4100:
4101: int crit_2( int dp1, int dp2 )
4102: {
4103: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
4104: }
4105:
4106: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
4107: {
4108: ND_pairs w,p,r,s;
4109: UINT *d;
4110:
4111: w = d1;
4112: d = LCM(w);
4113: s = NEXT(w);
4114: NEXT(w) = 0;
4115: for ( r = 0; s; s = p ) {
4116: p = NEXT(s);
4117: if ( ndl_equal(d,LCM(s)) ) {
4118: NEXT(s) = w; w = s;
4119: } else {
4120: NEXT(s) = r; r = s;
4121: }
4122: }
4123: *prest = r;
4124: return w;
4125: }
4126:
4127: NODE update_base(NODE nd,int ndp)
4128: {
4129: UINT *dl, *dln;
4130: NODE last, p, head;
4131:
4132: dl = DL(nd_psh[ndp]);
4133: for ( head = last = 0, p = nd; p; ) {
4134: dln = DL(nd_psh[(long)BDY(p)]);
4135: if ( ndl_reducible( dln, dl ) ) {
4136: p = NEXT(p);
4137: if ( last ) NEXT(last) = p;
4138: } else {
4139: if ( !last ) head = p;
4140: p = NEXT(last = p);
4141: }
4142: }
4143: head = append_one(head,ndp);
4144: return head;
4145: }
4146:
4147: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
4148: {
4149: ND_pairs m,ml,p,l;
4150: UINT *lcm;
4151: int s,td,len,tlen,c,c1;
4152:
4153: if ( !(p = NEXT(m = d)) ) {
4154: *prest = p;
4155: NEXT(m) = 0;
4156: return m;
4157: }
4158: if ( !NoSugar ) {
4159: if ( nd_sugarweight ) {
4160: s = m->sugar2;
4161: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4162: if ( (p->sugar2 < s)
4163: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4164: ml = l; m = p; s = m->sugar2;
4165: }
4166: } else {
4167: s = SG(m);
4168: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4169: if ( (SG(p) < s)
4170: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4171: ml = l; m = p; s = SG(m);
4172: }
4173: }
4174: } else {
4175: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4176: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
4177: ml = l; m = p; s = SG(m);
4178: }
4179: }
4180: if ( !ml ) *prest = NEXT(m);
4181: else {
4182: NEXT(ml) = NEXT(m);
4183: *prest = d;
4184: }
4185: NEXT(m) = 0;
4186: return m;
4187: }
4188:
4189: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
4190: {
4191: int msugar,i;
4192: ND_pairs t,dm0,dm,dr0,dr;
4193:
4194: if ( nd_sugarweight ) {
4195: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
4196: if ( t->sugar2 < msugar ) msugar = t->sugar2;
4197: dm0 = 0; dr0 = 0;
4198: for ( i = 0, t = d; t; t = NEXT(t) )
4199: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
4200: if ( dm0 ) NEXT(dm) = t;
4201: else dm0 = t;
4202: dm = t;
4203: i++;
4204: } else {
4205: if ( dr0 ) NEXT(dr) = t;
4206: else dr0 = t;
4207: dr = t;
4208: }
4209: } else {
4210: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
4211: if ( SG(t) < msugar ) msugar = SG(t);
4212: dm0 = 0; dr0 = 0;
4213: for ( i = 0, t = d; t; t = NEXT(t) )
4214: if ( i < nd_f4_nsp && SG(t) == msugar ) {
4215: if ( dm0 ) NEXT(dm) = t;
4216: else dm0 = t;
4217: dm = t;
4218: i++;
4219: } else {
4220: if ( dr0 ) NEXT(dr) = t;
4221: else dr0 = t;
4222: dr = t;
4223: }
4224: }
4225: NEXT(dm) = 0;
4226: if ( dr0 ) NEXT(dr) = 0;
4227: *prest = dr0;
4228: return dm0;
4229: }
4230:
1.28 noro 4231: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
4232: {
4233: int msugar;
4234: ND_pairs t,last;
4235:
4236: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
4237: if ( SG(t) == msugar ) last = t;
4238: *prest = last->next;
4239: last->next = 0;
4240: return d;
4241: }
4242:
1.1 noro 4243: int nd_tdeg(NDV c)
4244: {
4245: int wmax = 0;
4246: int i,len;
4247: NMV a;
4248:
4249: len = LEN(c);
4250: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4251: wmax = MAX(TD(DL(a)),wmax);
4252: return wmax;
4253: }
4254:
1.24 noro 4255: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 4256: {
4257: int len;
4258: RHist r;
4259: NDV b;
4260: NODE tn;
4261: LIST l;
4262: Z iq;
4263:
4264: if ( nd_psn == nd_pslen ) {
4265: nd_pslen *= 2;
4266: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
4267: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
4268: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
4269: nd_bound = (UINT **)
4270: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
4271: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
4272: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
4273: }
4274: NEWRHist(r); nd_psh[nd_psn] = r;
4275: nd_ps[nd_psn] = a;
4276: if ( aq ) {
4277: nd_ps_trace[nd_psn] = aq;
4278: if ( !m ) {
4279: register_hcf(aq);
4280: } else if ( m == -2 ) {
4281: /* do nothing */
4282: } else
4283: error("ndv_newps : invalud modulus");
4284: nd_bound[nd_psn] = ndv_compute_bound(aq);
4285: #if 1
4286: SG(r) = SG(aq);
4287: #else
4288: SG(r) = nd_tdeg(aq);
4289: #endif
4290: ndl_copy(HDL(aq),DL(r));
1.24 noro 4291: r->sig = dup_sig(aq->sig);
1.1 noro 4292: } else {
4293: if ( !m ) register_hcf(a);
4294: nd_bound[nd_psn] = ndv_compute_bound(a);
4295: #if 1
4296: SG(r) = SG(a);
4297: #else
4298: SG(r) = nd_tdeg(a);
4299: #endif
4300: ndl_copy(HDL(a),DL(r));
1.24 noro 4301: r->sig = dup_sig(a->sig);
1.1 noro 4302: }
4303: if ( nd_demand ) {
4304: if ( aq ) {
4305: ndv_save(nd_ps_trace[nd_psn],nd_psn);
4306: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4307: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4308: nd_ps_trace[nd_psn] = 0;
4309: } else {
4310: ndv_save(nd_ps[nd_psn],nd_psn);
4311: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
4312: nd_ps[nd_psn] = 0;
4313: }
4314: }
4315: if ( nd_gentrace ) {
4316: /* reverse the tracelist and append it to alltracelist */
4317: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 4318: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 4319: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
4320: }
4321: return nd_psn++;
4322: }
4323:
1.46 noro 4324: // find LM wrt the specified modord
4325: void ndv_lm_modord(NDV p,DL d)
1.45 noro 4326: {
4327: NMV m;
4328: DL tmp;
4329: int len,i,ret;
4330:
4331: NEWDL(tmp,nd_nvar);
4332: m = BDY(p); len = LEN(p);
1.46 noro 4333: _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45 noro 4334: for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
4335: _ndltodl(DL(m),tmp);
1.46 noro 4336: ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45 noro 4337: if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
4338: }
1.46 noro 4339: // printdl(d); printf("\n");
1.45 noro 4340: }
4341:
1.1 noro 4342: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
4343: /* return 1 if success, 0 if failure (HC(a mod p)) */
4344:
1.24 noro 4345: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 4346: {
1.6 noro 4347: int i,j,td,len,max;
4348: NODE s,s0,f0,tn;
4349: UINT *d;
4350: RHist r;
4351: NDVI w;
4352: NDV a,am;
4353: union oNDC hc;
4354: NODE node;
4355: P hcp;
4356: Z iq,jq;
4357: LIST l;
4358:
4359: nd_found = 0; nd_notfirst = 0; nd_create = 0;
4360: /* initialize the tracelist */
4361: nd_tracelist = 0;
4362:
4363: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
4364: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
4365: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
4366: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
4367: if ( !dont_sort ) {
4368: /* XXX heuristic */
1.26 noro 4369: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 4370: qsort(w,nd_psn,sizeof(struct oNDVI),
4371: (int (*)(const void *,const void *))ndvi_compare_rev);
4372: else
4373: qsort(w,nd_psn,sizeof(struct oNDVI),
4374: (int (*)(const void *,const void *))ndvi_compare);
4375: }
4376: nd_pslen = 2*nd_psn;
4377: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4378: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4379: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4380: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4381: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4382: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4383: nd_hcf = 0;
1.1 noro 4384:
1.6 noro 4385: if ( trace && nd_vc )
4386: makesubst(nd_vc,&nd_subst);
4387: else
4388: nd_subst = 0;
1.1 noro 4389:
1.6 noro 4390: if ( !nd_red )
4391: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4392: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4393: for ( i = 0; i < nd_psn; i++ ) {
4394: hc = HCU(w[i].p);
4395: if ( trace ) {
4396: if ( mod == -2 ) {
4397: /* over a large finite field */
4398: /* trace = small modulus */
4399: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4400: ndv_mod(-2,a);
4401: if ( !dont_removecont) ndv_removecont(-2,a);
4402: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4403: ndv_mod(trace,am);
4404: if ( DL_COMPARE(HDL(am),HDL(a)) )
4405: return 0;
4406: ndv_removecont(trace,am);
4407: } else {
4408: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4409: if ( !dont_removecont) ndv_removecont(0,a);
4410: register_hcf(a);
4411: am = nd_ps[i] = ndv_dup(mod,a);
4412: ndv_mod(mod,am);
4413: if ( DL_COMPARE(HDL(am),HDL(a)) )
4414: return 0;
4415: ndv_removecont(mod,am);
4416: }
4417: } else {
4418: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4419: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4420: if ( !mod ) register_hcf(a);
1.1 noro 4421: }
1.6 noro 4422: if ( nd_gentrace ) {
4423: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4424: /* exact division */
1.1 noro 4425: if ( !dont_removecont )
1.6 noro 4426: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4427: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4428: }
4429: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4430: nd_bound[i] = ndv_compute_bound(a);
4431: nd_psh[i] = r;
4432: if ( nd_demand ) {
4433: if ( trace ) {
4434: ndv_save(nd_ps_trace[i],i);
4435: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4436: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4437: nd_ps_trace[i] = 0;
4438: } else {
4439: ndv_save(nd_ps[i],i);
4440: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4441: nd_ps[i] = 0;
4442: }
1.1 noro 4443: }
1.6 noro 4444: }
1.24 noro 4445: if ( sba ) {
1.27 noro 4446: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4447: // setup signatures
1.27 noro 4448: for ( i = 0; i < nd_psn; i++ ) {
4449: SIG sig;
1.24 noro 4450:
1.27 noro 4451: NEWSIG(sig); sig->pos = i;
4452: nd_ps[i]->sig = sig;
4453: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4454: nd_psh[i]->sig = sig;
4455: if ( trace ) {
4456: nd_ps_trace[i]->sig = sig;
4457: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4458: }
4459: NEWDL(nd_sba_hm[i],nd_nvar);
1.46 noro 4460: if ( nd_sba_modord )
4461: ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45 noro 4462: else
4463: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27 noro 4464: }
1.29 noro 4465: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4466: for ( i = 0; i < nd_psn; i++ ) {
4467: j = nd_psh[i]->sig->pos;
4468: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4469: }
1.24 noro 4470: }
1.6 noro 4471: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4472: return 1;
1.1 noro 4473: }
4474:
4475: struct order_spec *append_block(struct order_spec *spec,
4476: int nv,int nalg,int ord);
4477:
4478: extern VECT current_dl_weight_vector_obj;
4479: static VECT prev_weight_vector_obj;
4480:
4481: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4482: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4483: {
4484: NODE alist,t,s,r0,r,arg;
4485: VL tv;
4486: P poly;
4487: DP d;
4488: Alg alpha,dp;
4489: DAlg inv,da,hc;
4490: MP m;
4491: int i,nvar,nalg,n;
4492: NumberField nf;
4493: LIST f1,f2;
4494: struct order_spec *current_spec;
4495: VECT obj,obj0;
4496: VECT tmp;
4497:
4498: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4499: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4500:
4501: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4502: NEXTNODE(alist,t); MKV(tv->v,poly);
4503: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4504: tv->v = tv->v->priv;
4505: }
4506: NEXT(t) = 0;
4507:
4508: /* simplification, making polynomials monic */
4509: setfield_dalg(alist);
4510: obj_algtodalg((Obj)f,(Obj *)&f1);
4511: for ( t = BDY(f); t; t = NEXT(t) ) {
4512: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4513: hc = (DAlg)BDY(d)->c;
4514: if ( NID(hc) == N_DA ) {
4515: invdalg(hc,&inv);
4516: for ( m = BDY(d); m; m = NEXT(m) ) {
4517: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4518: }
4519: }
4520: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4521: }
4522: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4523:
4524: /* append alg vars to the var list */
4525: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4526: NEXT(tv) = av;
4527:
4528: /* append a block to ord */
4529: *ord1p = append_block(ord,nvar,nalg,2);
4530:
4531: /* create generator list */
4532: nf = get_numberfield();
4533: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4534: MKAlg(nf->defpoly[i],dp);
4535: MKNODE(s,dp,t); t = s;
4536: }
4537: MKLIST(f1,t);
4538: *alistp = alist;
4539: algobjtorat((Obj)f1,(Obj *)f1p);
4540:
4541: /* creating a new weight vector */
4542: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4543: n = nvar+nalg+1;
4544: MKVECT(obj,n);
4545: if ( obj0 && obj0->len == nvar )
4546: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4547: else
4548: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4549: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4550: BDY(obj)[n-1] = (pointer)ONE;
4551: arg = mknode(1,obj);
4552: Pdp_set_weight(arg,&tmp);
4553: }
4554:
4555: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4556: {
4557: NODE s,t,u0,u;
4558: P p;
4559: VL tv;
4560: Obj obj;
4561: VECT tmp;
4562: NODE arg;
4563:
4564: u0 = 0;
4565: for ( t = r; t; t = NEXT(t) ) {
4566: p = (P)BDY(t);
4567: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4568: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4569: }
4570: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4571: NEXTNODE(u0,u);
4572: BDY(u) = (pointer)p;
4573: }
4574: }
4575: arg = mknode(1,prev_weight_vector_obj);
4576: Pdp_set_weight(arg,&tmp);
4577:
4578: return u0;
4579: }
4580:
4581: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4582: {
4583: VL tv,fv,vv,vc,av;
4584: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4585: int e,max,nvar,i;
4586: NDV b;
4587: int ishomo,nalg,mrank,trank,wmax,len;
4588: NMV a;
4589: Alg alpha,dp;
4590: P p,zp;
4591: Q dmy;
4592: LIST f1,f2,zpl;
4593: Obj obj;
4594: NumberField nf;
4595: struct order_spec *ord1;
4596: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4597: LIST l1,l2,l3,l4,l5;
4598: int j;
4599: Z jq,bpe,last_nonzero;
4600: int *perm;
4601: EPOS oepos;
4602: int obpe,oadv,ompos,cbpe;
1.15 noro 4603: VECT hvect;
1.1 noro 4604:
1.41 noro 4605: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4606: nd_module = 0;
4607: if ( !m && Demand ) nd_demand = 1;
4608: else nd_demand = 0;
4609:
4610: if ( DP_Multiple )
4611: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4612: #if 0
4613: ndv_alloc = 0;
4614: #endif
4615: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 4616: parse_nd_option(vv,current_option);
1.1 noro 4617: if ( m && nd_vc )
4618: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4619: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4620: switch ( ord->id ) {
4621: case 1:
4622: if ( ord->nv != nvar )
4623: error("nd_{gr,f4} : invalid order specification");
4624: break;
4625: default:
4626: break;
4627: }
4628: nd_nalg = 0;
4629: av = 0;
4630: if ( !m ) {
4631: get_algtree((Obj)f,&av);
4632: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4633: nd_ntrans = nvar;
4634: nd_nalg = nalg;
4635: /* #i -> t#i */
4636: if ( nalg ) {
4637: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4638: ord = ord1;
4639: f = f1;
4640: }
4641: nvar += nalg;
4642: }
4643: nd_init_ord(ord);
4644: mrank = 0;
4645: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4646: for ( tv = vv; tv; tv = NEXT(tv) ) {
4647: if ( nd_module ) {
1.16 noro 4648: if ( OID(BDY(t)) == O_DPM ) {
4649: e = dpm_getdeg((DPM)BDY(t),&trank);
4650: max = MAX(e,max);
4651: mrank = MAX(mrank,trank);
4652: } else {
4653: s = BDY((LIST)BDY(t));
4654: trank = length(s);
4655: mrank = MAX(mrank,trank);
4656: for ( ; s; s = NEXT(s) ) {
4657: e = getdeg(tv->v,(P)BDY(s));
4658: max = MAX(e,max);
4659: }
1.1 noro 4660: }
4661: } else {
1.43 noro 4662: if ( OID(BDY(t)) == O_DP ) {
4663: e = dp_getdeg((DP)BDY(t));
4664: max = MAX(e,max);
4665: } else {
4666: e = getdeg(tv->v,(P)BDY(t));
4667: max = MAX(e,max);
4668: }
1.1 noro 4669: }
4670: }
4671: nd_setup_parameters(nvar,nd_nzlist?0:max);
4672: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4673: ishomo = 1;
4674: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4675: if ( nd_module ) {
1.16 noro 4676: if ( OID(BDY(t)) == O_DPM ) {
4677: Z cont;
4678: DPM zdpm;
4679:
4680: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4681: else zdpm = (DPM)BDY(t);
4682: b = (pointer)dpmtondv(m,zdpm);
4683: } else {
4684: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4685: else zpl = (LIST)BDY(t);
1.1 noro 4686: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4687: }
1.1 noro 4688: } else {
1.43 noro 4689: if ( OID(BDY(t)) == O_DP ) {
4690: DP zdp;
4691:
4692: if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
4693: else zdp = (DP)BDY(t);
4694: b = (pointer)dptondv(m,zdp);
4695: } else {
4696: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4697: else zp = (P)BDY(t);
4698: b = (pointer)ptondv(CO,vv,zp);
4699: }
1.1 noro 4700: }
4701: if ( ishomo )
4702: ishomo = ishomo && ndv_ishomo(b);
4703: if ( m ) ndv_mod(m,b);
4704: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4705: }
4706: if ( fd0 ) NEXT(fd) = 0;
4707:
4708: if ( !ishomo && homo ) {
4709: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4710: b = (NDV)BDY(t); len = LEN(b);
4711: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4712: wmax = MAX(TD(DL(a)),wmax);
4713: }
4714: homogenize_order(ord,nvar,&ord1);
4715: nd_init_ord(ord1);
4716: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4717: for ( t = fd0; t; t = NEXT(t) )
4718: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4719: }
4720:
1.24 noro 4721: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4722: if ( nd_gentrace ) {
4723: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4724: }
4725: if ( nd_splist ) {
4726: *rp = compute_splist();
4727: return;
4728: }
4729: if ( nd_check_splist ) {
4730: if ( f4 ) {
4731: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4732: else *rp = 0;
4733: } else {
4734: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4735: else *rp = 0;
4736: }
4737: return;
4738: }
4739: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4740: if ( !x ) {
4741: *rp = 0; return;
4742: }
1.15 noro 4743: if ( nd_gentrace ) {
4744: MKVECT(hvect,nd_psn);
4745: for ( i = 0; i < nd_psn; i++ )
4746: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4747: }
1.1 noro 4748: if ( !ishomo && homo ) {
4749: /* dehomogenization */
4750: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4751: nd_init_ord(ord);
4752: nd_setup_parameters(nvar,0);
4753: }
4754: nd_demand = 0;
4755: if ( nd_module && nd_intersect ) {
4756: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4757: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4758: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4759: }
4760: conv_ilist(nd_demand,0,x,0);
4761: goto FINAL;
4762: }
4763: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4764: x = ndv_reducebase(x,perm);
4765: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4766: x = ndv_reduceall(m,x);
4767: cbpe = nd_bpe;
4768: if ( nd_gentrace && !f4 ) {
4769: tl2 = nd_alltracelist; nd_alltracelist = 0;
4770: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4771: tl3 = nd_alltracelist; nd_alltracelist = 0;
4772: if ( nd_gensyz ) {
4773: nd_gb(m,0,1,1,0);
4774: tl4 = nd_alltracelist; nd_alltracelist = 0;
4775: } else tl4 = 0;
4776: }
4777: nd_bpe = cbpe;
4778: nd_setup_parameters(nd_nvar,0);
4779: FINAL:
4780: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4781: NEXTNODE(r0,r);
4782: if ( nd_module ) {
4783: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4784: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4785: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4786: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4787: }
4788: if ( r0 ) NEXT(r) = 0;
4789: if ( !m && nd_nalg )
4790: r0 = postprocess_algcoef(av,alist,r0);
4791: MKLIST(*rp,r0);
4792: if ( nd_gentrace ) {
4793: if ( f4 ) {
1.6 noro 4794: STOZ(16,bpe);
4795: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4796: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4797: } else {
4798: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4799: tl3 = reverse_node(tl3);
4800: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4801: for ( t = tl2; t; t = NEXT(t) ) {
4802: /* s = [i,[*,j,*,*],...] */
4803: s = BDY((LIST)BDY(t));
1.6 noro 4804: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4805: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4806: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4807: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4808: }
4809: }
4810: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4811: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4812: }
4813: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4814: MKLIST(l5,tl4);
1.6 noro 4815: STOZ(nd_bpe,bpe);
1.15 noro 4816: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4817: }
4818: }
4819: #if 0
4820: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4821: #endif
4822: }
4823:
1.28 noro 4824: NODE nd_sba_f4(int m,int **indp);
4825:
4826: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4827: {
4828: VL tv,fv,vv,vc,av;
1.44 noro 4829: NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24 noro 4830: int e,max,nvar,i;
4831: NDV b;
4832: int ishomo,nalg,wmax,len;
4833: NMV a;
4834: P p,zp;
4835: Q dmy;
4836: struct order_spec *ord1;
4837: int j;
4838: int *perm;
4839: EPOS oepos;
4840: int obpe,oadv,ompos,cbpe;
1.29 noro 4841: struct oEGT eg0,eg1,egconv;
1.24 noro 4842:
4843: nd_module = 0;
4844: nd_demand = 0;
1.35 noro 4845: Nsamesig = 0;
1.24 noro 4846: if ( DP_Multiple )
4847: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4848: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 4849: parse_nd_option(vv,current_option);
1.24 noro 4850: if ( m && nd_vc )
4851: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4852: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4853: switch ( ord->id ) {
4854: case 1:
4855: if ( ord->nv != nvar )
4856: error("nd_sba : invalid order specification");
4857: break;
4858: default:
4859: break;
4860: }
4861: nd_nalg = 0;
4862: nd_init_ord(ord);
4863: // for SIG comparison
4864: initd(ord);
4865: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4866: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43 noro 4867: if ( OID(BDY(t)) == O_DP ) {
4868: e = dp_getdeg((DP)BDY(t));
4869: max = MAX(e,max);
4870: } else {
4871: e = getdeg(tv->v,(P)BDY(t));
4872: max = MAX(e,max);
4873: }
1.24 noro 4874: }
4875: }
1.25 noro 4876: nd_setup_parameters(nvar,max);
1.24 noro 4877: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4878: ishomo = 1;
4879: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43 noro 4880: if ( OID(BDY(t)) == O_DP ) {
4881: DP zdp;
4882:
4883: if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
4884: else zdp = (DP)BDY(t);
4885: b = (pointer)dptondv(m,zdp);
4886: } else {
4887: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4888: else zp = (P)BDY(t);
4889: b = (pointer)ptondv(CO,vv,zp);
4890: }
1.24 noro 4891: if ( ishomo )
4892: ishomo = ishomo && ndv_ishomo(b);
4893: if ( m ) ndv_mod(m,b);
4894: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4895: }
4896: if ( fd0 ) NEXT(fd) = 0;
4897:
4898: if ( !ishomo && homo ) {
4899: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4900: b = (NDV)BDY(t); len = LEN(b);
4901: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4902: wmax = MAX(TD(DL(a)),wmax);
4903: }
4904: homogenize_order(ord,nvar,&ord1);
4905: nd_init_ord(ord1);
1.42 noro 4906: // for SIG comparison
4907: initd(ord1);
1.24 noro 4908: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4909: for ( t = fd0; t; t = NEXT(t) )
4910: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4911: }
4912:
1.39 noro 4913: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44 noro 4914: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24 noro 4915: if ( !x ) {
4916: *rp = 0; return;
4917: }
4918: if ( !ishomo && homo ) {
4919: /* dehomogenization */
4920: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4921: nd_init_ord(ord);
1.42 noro 4922: // for SIG comparison
4923: initd(ord);
1.24 noro 4924: nd_setup_parameters(nvar,0);
4925: }
4926: nd_demand = 0;
4927: x = ndv_reducebase(x,perm);
4928: x = ndv_reduceall(m,x);
4929: nd_setup_parameters(nd_nvar,0);
1.29 noro 4930: get_eg(&eg0);
1.24 noro 4931: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4932: NEXTNODE(r0,r);
4933: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4934: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4935: }
4936: if ( r0 ) NEXT(r) = 0;
1.44 noro 4937: if ( nd_sba_syz ) {
4938: LIST gb,hsyz;
4939: NODE nd;
4940:
4941: MKLIST(gb,r0);
4942: MKLIST(hsyz,syz);
4943: nd = mknode(2,gb,hsyz);
4944: MKLIST(*rp,nd);
4945: } else
4946: MKLIST(*rp,r0);
1.29 noro 4947: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4948: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4949: }
4950:
1.1 noro 4951: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4952: {
4953: VL tv,fv,vv,vc,av;
4954: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4955: int e,max,nvar,i;
4956: NDV b;
4957: int ishomo,nalg;
4958: Alg alpha,dp;
4959: P p,zp;
4960: Q dmy;
4961: LIST f1,f2;
4962: Obj obj;
4963: NumberField nf;
4964: struct order_spec *ord1;
4965: int *perm;
4966:
4967: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 4968: parse_nd_option(vv,current_option);
1.1 noro 4969: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4970: switch ( ord->id ) {
4971: case 1:
4972: if ( ord->nv != nvar )
4973: error("nd_check : invalid order specification");
4974: break;
4975: default:
4976: break;
4977: }
4978: nd_nalg = 0;
4979: av = 0;
4980: if ( !m ) {
4981: get_algtree((Obj)f,&av);
4982: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4983: nd_ntrans = nvar;
4984: nd_nalg = nalg;
4985: /* #i -> t#i */
4986: if ( nalg ) {
4987: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4988: ord = ord1;
4989: f = f1;
4990: }
4991: nvar += nalg;
4992: }
4993: nd_init_ord(ord);
4994: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4995: for ( tv = vv; tv; tv = NEXT(tv) ) {
4996: e = getdeg(tv->v,(P)BDY(t));
4997: max = MAX(e,max);
4998: }
4999: nd_setup_parameters(nvar,max);
5000: ishomo = 1;
5001: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5002: ptozp((P)BDY(t),1,&dmy,&zp);
5003: b = (pointer)ptondv(CO,vv,zp);
5004: if ( ishomo )
5005: ishomo = ishomo && ndv_ishomo(b);
5006: if ( m ) ndv_mod(m,b);
5007: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
5008: }
5009: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 5010: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 5011: for ( x = 0, i = 0; i < nd_psn; i++ )
5012: x = update_base(x,i);
5013: if ( do_check ) {
5014: x = nd_gb(m,ishomo,1,0,&perm);
5015: if ( !x ) {
5016: *rp = 0;
5017: return;
5018: }
5019: } else {
5020: #if 0
5021: /* bug ? */
5022: for ( t = x; t; t = NEXT(t) )
5023: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
5024: #else
5025: conv_ilist(0,0,x,&perm);
5026: #endif
5027: }
5028: x = ndv_reducebase(x,perm);
5029: x = ndv_reduceall(m,x);
5030: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
5031: NEXTNODE(r0,r);
5032: BDY(r) = ndvtop(m,CO,vv,BDY(t));
5033: }
5034: if ( r0 ) NEXT(r) = 0;
5035: if ( !m && nd_nalg )
5036: r0 = postprocess_algcoef(av,alist,r0);
5037: MKLIST(*rp,r0);
5038: }
5039:
5040: NDV recompute_trace(NODE trace,NDV *p,int m);
5041: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
5042:
5043: NDV recompute_trace(NODE ti,NDV *p,int mod)
5044: {
5045: int c,c1,c2,i;
5046: NM mul,m,tail;
5047: ND d,r,rm;
5048: NODE sj;
5049: NDV red;
5050: Obj mj;
5051:
5052: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
5053: CM(mul) = 1;
5054: tail = 0;
5055: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
5056: sj = BDY((LIST)BDY(ti));
5057: if ( ARG0(sj) ) {
1.6 noro 5058: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 5059: mj = (Obj)ARG2(sj);
5060: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
5061: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
5062: rm = ndv_mul_nm(mod,mul,red);
5063: if ( !r ) r = rm;
5064: else {
5065: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
5066: if ( d ) {
5067: NEXT(tail) = m; tail = m; LEN(d)++;
5068: } else {
5069: MKND(nd_nvar,m,1,d); tail = BDY(d);
5070: }
5071: }
5072: if ( !m ) return 0; /* failure */
5073: else {
5074: BDY(r) = m;
5075: if ( mod > 0 || mod == -1 ) {
5076: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
5077: DMAR(c1,c2,0,mod,c);
5078: nd_mul_c(mod,rm,c);
5079: } else {
5080: Z t,u;
5081:
5082: chsgnlf(HCZ(r),&t);
5083: divlf(t,HCZ(rm),&u);
5084: nd_mul_c_lf(rm,u);
5085: }
5086: r = nd_add(mod,r,rm);
5087: }
5088: }
5089: }
5090: }
5091: if ( tail ) NEXT(tail) = 0;
5092: d = nd_add(mod,d,r);
5093: nd_mul_c(mod,d,invm(HCM(d),mod));
5094: return ndtondv(mod,d);
5095: }
5096:
5097: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
5098: {
5099: VL tv,fv,vv,vc,av;
5100: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5101: int e,max,nvar,i;
5102: NDV b;
5103: int ishomo,nalg;
5104: Alg alpha,dp;
5105: P p,zp;
5106: Q dmy;
5107: LIST f1,f2;
5108: Obj obj;
5109: NumberField nf;
5110: struct order_spec *ord1;
5111: NODE permtrace,intred,ind,perm,trace,ti;
5112: int len,n,j;
5113: NDV *db,*pb;
5114:
5115: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5116: parse_nd_option(vv,current_option);
1.1 noro 5117: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5118: switch ( ord->id ) {
5119: case 1:
5120: if ( ord->nv != nvar )
5121: error("nd_check : invalid order specification");
5122: break;
5123: default:
5124: break;
5125: }
5126: nd_init_ord(ord);
1.6 noro 5127: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 5128: nd_setup_parameters(nvar,0);
5129:
5130: len = length(BDY(f));
5131: db = (NDV *)MALLOC(len*sizeof(NDV *));
5132: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
5133: ptozp((P)BDY(t),1,&dmy,&zp);
5134: b = ptondv(CO,vv,zp);
5135: ndv_mod(m,b);
5136: ndv_mul_c(m,b,invm(HCM(b),m));
5137: db[i] = b;
5138: }
5139:
5140: permtrace = BDY((LIST)ARG2(BDY(tlist)));
5141: intred = BDY((LIST)ARG3(BDY(tlist)));
5142: ind = BDY((LIST)ARG4(BDY(tlist)));
5143: perm = BDY((LIST)ARG0(permtrace));
5144: trace = NEXT(permtrace);
5145:
5146: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 5147: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 5148: if ( j > i ) i = j;
5149: }
5150: n = i+1;
5151: pb = (NDV *)MALLOC(n*sizeof(NDV *));
5152: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
5153: ti = BDY((LIST)BDY(t));
1.6 noro 5154: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 5155: }
5156: for ( t = trace; t; t = NEXT(t) ) {
5157: ti = BDY((LIST)BDY(t));
1.6 noro 5158: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5159: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5160: if ( DP_Print ) {
5161: fprintf(asir_out,"."); fflush(asir_out);
5162: }
5163: }
5164: for ( t = intred; t; t = NEXT(t) ) {
5165: ti = BDY((LIST)BDY(t));
1.6 noro 5166: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5167: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5168: if ( DP_Print ) {
5169: fprintf(asir_out,"*"); fflush(asir_out);
5170: }
5171: }
5172: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
5173: NEXTNODE(r0,r);
1.6 noro 5174: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 5175: ndv_mul_c(m,b,invm(HCM(b),m));
5176: #if 0
1.6 noro 5177: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5178: #else
1.6 noro 5179: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5180: #endif
5181: }
5182: if ( r0 ) NEXT(r) = 0;
5183: MKLIST(*rp,r0);
5184: if ( DP_Print ) fprintf(asir_out,"\n");
5185: }
5186:
1.16 noro 5187: 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 5188: {
5189: VL tv,fv,vv,vc,av;
5190: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
5191: int m,nocheck,nvar,mindex,e,max;
5192: NDV c;
5193: NMV a;
5194: P p,zp;
5195: Q dmy;
5196: EPOS oepos;
5197: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
5198: Alg alpha,dp;
5199: P poly;
5200: LIST f1,f2,zpl;
5201: Obj obj;
5202: NumberField nf;
5203: struct order_spec *ord1;
5204: struct oEGT eg_check,eg0,eg1;
5205: NODE tr,tl1,tl2,tl3,tl4;
5206: LIST l1,l2,l3,l4,l5;
5207: int *perm;
5208: int j,ret;
5209: Z jq,bpe;
1.15 noro 5210: VECT hvect;
1.1 noro 5211:
1.41 noro 5212: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 5213: nd_module = 0;
5214: nd_lf = 0;
1.46 noro 5215: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
5216: parse_nd_option(vv,current_option);
1.1 noro 5217: if ( nd_lf ) {
5218: if ( f4 )
5219: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
5220: else
5221: error("nd_gr_trace is not implemented yet over a large finite field");
5222: return;
5223: }
5224: if ( DP_Multiple )
5225: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5226:
5227: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5228: switch ( ord->id ) {
5229: case 1:
5230: if ( ord->nv != nvar )
5231: error("nd_gr_trace : invalid order specification");
5232: break;
5233: default:
5234: break;
5235: }
5236:
5237: get_algtree((Obj)f,&av);
5238: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5239: nd_ntrans = nvar;
5240: nd_nalg = nalg;
5241: /* #i -> t#i */
5242: if ( nalg ) {
5243: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5244: ord = ord1;
5245: f = f1;
5246: }
5247: nvar += nalg;
5248:
5249: nocheck = 0;
5250: mindex = 0;
5251:
5252: if ( Demand ) nd_demand = 1;
5253: else nd_demand = 0;
5254:
5255: /* setup modulus */
5256: if ( trace < 0 ) {
5257: trace = -trace;
5258: nocheck = 1;
5259: }
5260: m = trace > 1 ? trace : get_lprime(mindex);
5261: nd_init_ord(ord);
5262: mrank = 0;
5263: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5264: for ( tv = vv; tv; tv = NEXT(tv) ) {
5265: if ( nd_module ) {
1.16 noro 5266: if ( OID(BDY(t)) == O_DPM ) {
5267: e = dpm_getdeg((DPM)BDY(t),&trank);
5268: max = MAX(e,max);
5269: mrank = MAX(mrank,trank);
5270: } else {
1.1 noro 5271: s = BDY((LIST)BDY(t));
5272: trank = length(s);
5273: mrank = MAX(mrank,trank);
5274: for ( ; s; s = NEXT(s) ) {
5275: e = getdeg(tv->v,(P)BDY(s));
5276: max = MAX(e,max);
5277: }
1.16 noro 5278: }
1.1 noro 5279: } else {
1.43 noro 5280: if ( OID(BDY(t)) == O_DP ) {
5281: e = dp_getdeg((DP)BDY(t));
5282: max = MAX(e,max);
5283: } else {
1.1 noro 5284: e = getdeg(tv->v,(P)BDY(t));
5285: max = MAX(e,max);
1.43 noro 5286: }
1.1 noro 5287: }
5288: }
5289: nd_setup_parameters(nvar,max);
5290: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5291: ishomo = 1;
5292: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5293: if ( nd_module ) {
1.16 noro 5294: if ( OID(BDY(t)) == O_DPM ) {
5295: Z cont;
5296: DPM zdpm;
5297:
1.17 noro 5298: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 5299: else zdpm = (DPM)BDY(t);
5300: c = (pointer)dpmtondv(m,zdpm);
5301: } else {
5302: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
5303: else zpl = (LIST)BDY(t);
1.1 noro 5304: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 5305: }
1.1 noro 5306: } else {
1.43 noro 5307: if ( OID(BDY(t)) == O_DP ) {
5308: DP zdp;
5309:
5310: if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
5311: else zdp = (DP)BDY(t);
5312: c = (pointer)dptondv(m,zdp);
5313: } else {
5314: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
5315: else zp = (P)BDY(t);
5316: c = (pointer)ptondv(CO,vv,zp);
5317: }
1.1 noro 5318: }
5319: if ( ishomo )
5320: ishomo = ishomo && ndv_ishomo(c);
5321: if ( c ) {
5322: NEXTNODE(in0,in); BDY(in) = (pointer)c;
5323: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
5324: }
5325: }
5326: if ( in0 ) NEXT(in) = 0;
5327: if ( fd0 ) NEXT(fd) = 0;
5328: if ( !ishomo && homo ) {
5329: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
5330: c = (NDV)BDY(t); len = LEN(c);
5331: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
5332: wmax = MAX(TD(DL(a)),wmax);
5333: }
5334: homogenize_order(ord,nvar,&ord1);
5335: nd_init_ord(ord1);
5336: nd_setup_parameters(nvar+1,wmax);
5337: for ( t = fd0; t; t = NEXT(t) )
5338: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5339: }
5340: if ( MaxDeg > 0 ) nocheck = 1;
5341: while ( 1 ) {
5342: tl1 = tl2 = tl3 = tl4 = 0;
5343: if ( Demand )
5344: nd_demand = 1;
1.24 noro 5345: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 5346: if ( nd_gentrace ) {
5347: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
5348: }
5349: if ( ret )
5350: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
5351: if ( !ret || !cand ) {
5352: /* failure */
5353: if ( trace > 1 ) { *rp = 0; return; }
5354: else m = get_lprime(++mindex);
5355: continue;
5356: }
1.15 noro 5357: if ( nd_gentrace ) {
5358: MKVECT(hvect,nd_psn);
5359: for ( i = 0; i < nd_psn; i++ )
5360: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
5361: }
1.1 noro 5362: if ( !ishomo && homo ) {
5363: /* dehomogenization */
5364: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5365: nd_init_ord(ord);
5366: nd_setup_parameters(nvar,0);
5367: }
5368: nd_demand = 0;
5369: cand = ndv_reducebase(cand,perm);
5370: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
5371: cand = ndv_reduceall(0,cand);
5372: cbpe = nd_bpe;
5373: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
5374: get_eg(&eg0);
5375: if ( nocheck )
5376: break;
5377: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
5378: if ( nd_gentrace ) {
5379: tl3 = nd_alltracelist; nd_alltracelist = 0;
5380: } else tl3 = 0;
5381: /* gbcheck : cand is a GB of Id(cand) ? */
5382: if ( nd_vc || nd_gentrace || nd_gensyz )
5383: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
5384: else
5385: ret = nd_f4(0,1,0)!=0;
5386: if ( nd_gentrace && nd_gensyz ) {
5387: tl4 = nd_alltracelist; nd_alltracelist = 0;
5388: } else tl4 = 0;
5389: }
5390: if ( ret ) break;
5391: else if ( trace > 1 ) {
5392: /* failure */
5393: *rp = 0; return;
5394: } else {
5395: /* try the next modulus */
5396: m = get_lprime(++mindex);
5397: /* reset the parameters */
5398: if ( !ishomo && homo ) {
5399: nd_init_ord(ord1);
5400: nd_setup_parameters(nvar+1,wmax);
5401: } else {
5402: nd_init_ord(ord);
5403: nd_setup_parameters(nvar,max);
5404: }
5405: }
5406: }
5407: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
5408: if ( DP_Print )
1.6 noro 5409: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 5410: /* dp->p */
5411: nd_bpe = cbpe;
5412: nd_setup_parameters(nd_nvar,0);
5413: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 5414: if ( nd_module ) {
1.17 noro 5415: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 5416: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 5417: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
5418: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 5419: }
5420: if ( nd_nalg )
5421: cand = postprocess_algcoef(av,alist,cand);
5422: MKLIST(*rp,cand);
5423: if ( nd_gentrace ) {
5424: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5425: tl3 = reverse_node(tl3);
5426: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5427: for ( t = tl2; t; t = NEXT(t) ) {
5428: /* s = [i,[*,j,*,*],...] */
5429: s = BDY((LIST)BDY(t));
1.6 noro 5430: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5431: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5432: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5433: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5434: }
5435: }
5436: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5437: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5438: }
5439: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5440: MKLIST(l5,tl4);
1.6 noro 5441: STOZ(nd_bpe,bpe);
1.15 noro 5442: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5443: }
5444: }
5445:
5446: /* XXX : module element is not considered */
5447:
5448: void dltondl(int n,DL dl,UINT *r)
5449: {
5450: UINT *d;
5451: int i,j,l,s,ord_l;
5452: struct order_pair *op;
5453:
5454: d = (unsigned int *)dl->d;
5455: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5456: if ( nd_blockmask ) {
5457: l = nd_blockmask->n;
5458: op = nd_blockmask->order_pair;
5459: for ( j = 0, s = 0; j < l; j++ ) {
5460: ord_l = op[j].length;
5461: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5462: }
5463: TD(r) = ndl_weight(r);
5464: ndl_weight_mask(r);
5465: } else {
5466: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5467: TD(r) = ndl_weight(r);
5468: }
5469: }
5470:
5471: DL ndltodl(int n,UINT *ndl)
5472: {
5473: DL dl;
5474: int *d;
5475: int i,j,l,s,ord_l;
5476: struct order_pair *op;
5477:
1.33 noro 5478: NEWDL_NOINIT(dl,n);
1.1 noro 5479: dl->td = TD(ndl);
5480: d = dl->d;
5481: if ( nd_blockmask ) {
5482: l = nd_blockmask->n;
5483: op = nd_blockmask->order_pair;
5484: for ( j = 0, s = 0; j < l; j++ ) {
5485: ord_l = op[j].length;
5486: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5487: }
5488: } else {
5489: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5490: }
5491: return dl;
5492: }
5493:
1.24 noro 5494: void _ndltodl(UINT *ndl,DL dl)
5495: {
5496: int *d;
5497: int i,j,l,s,ord_l,n;
5498: struct order_pair *op;
5499:
5500: n = nd_nvar;
5501: dl->td = TD(ndl);
5502: d = dl->d;
5503: if ( nd_blockmask ) {
5504: l = nd_blockmask->n;
5505: op = nd_blockmask->order_pair;
5506: for ( j = 0, s = 0; j < l; j++ ) {
5507: ord_l = op[j].length;
5508: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5509: }
5510: } else {
5511: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5512: }
5513: }
5514:
1.1 noro 5515: void nmtodp(int mod,NM m,DP *r)
5516: {
5517: DP dp;
5518: MP mr;
5519:
5520: NEWMP(mr);
5521: mr->dl = ndltodl(nd_nvar,DL(m));
5522: mr->c = (Obj)ndctop(mod,m->c);
5523: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5524: *r = dp;
5525: }
5526:
1.15 noro 5527: void ndltodp(UINT *d,DP *r)
5528: {
5529: DP dp;
5530: MP mr;
5531:
5532: NEWMP(mr);
5533: mr->dl = ndltodl(nd_nvar,d);
5534: mr->c = (Obj)ONE;
5535: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5536: *r = dp;
5537: }
5538:
1.1 noro 5539: void ndl_print(UINT *dl)
5540: {
5541: int n;
5542: int i,j,l,ord_l,s,s0;
5543: struct order_pair *op;
5544:
5545: n = nd_nvar;
5546: printf("<<");
5547: if ( nd_blockmask ) {
5548: l = nd_blockmask->n;
5549: op = nd_blockmask->order_pair;
5550: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5551: ord_l = op[j].length;
5552: for ( i = 0; i < ord_l; i++, s++ )
5553: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5554: }
5555: } else {
5556: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5557: }
5558: printf(">>");
5559: if ( nd_module && MPOS(dl) )
5560: printf("*e%d",MPOS(dl));
5561: }
5562:
5563: void nd_print(ND p)
5564: {
5565: NM m;
5566:
5567: if ( !p )
5568: printf("0\n");
5569: else {
5570: for ( m = BDY(p); m; m = NEXT(m) ) {
5571: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5572: else printf("+%ld*",CM(m));
1.1 noro 5573: ndl_print(DL(m));
5574: }
5575: printf("\n");
5576: }
5577: }
5578:
5579: void nd_print_q(ND p)
5580: {
5581: NM m;
5582:
5583: if ( !p )
5584: printf("0\n");
5585: else {
5586: for ( m = BDY(p); m; m = NEXT(m) ) {
5587: printf("+");
1.6 noro 5588: printexpr(CO,(Obj)CZ(m));
1.1 noro 5589: printf("*");
5590: ndl_print(DL(m));
5591: }
5592: printf("\n");
5593: }
5594: }
5595:
5596: void ndp_print(ND_pairs d)
5597: {
5598: ND_pairs t;
5599:
5600: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5601: printf("\n");
5602: }
5603:
5604: void nd_removecont(int mod,ND p)
5605: {
5606: int i,n;
5607: Z *w;
5608: NM m;
5609: struct oVECT v;
5610:
5611: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5612: else if ( mod == -2 ) {
5613: Z inv;
5614: divlf(ONE,HCZ(p),&inv);
5615: nd_mul_c_lf(p,inv);
5616: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5617: else {
5618: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5619: w = (Z *)MALLOC(n*sizeof(Q));
5620: v.len = n;
5621: v.body = (pointer *)w;
1.6 noro 5622: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5623: removecont_array((P *)w,n,1);
1.6 noro 5624: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5625: }
5626: }
5627:
5628: void nd_removecont2(ND p1,ND p2)
5629: {
5630: int i,n1,n2,n;
5631: Z *w;
5632: NM m;
5633: struct oVECT v;
5634:
5635: n1 = nd_length(p1);
5636: n2 = nd_length(p2);
5637: n = n1+n2;
5638: w = (Z *)MALLOC(n*sizeof(Q));
5639: v.len = n;
5640: v.body = (pointer *)w;
5641: i = 0;
5642: if ( p1 )
1.6 noro 5643: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5644: if ( p2 )
1.6 noro 5645: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5646: removecont_array((P *)w,n,1);
5647: i = 0;
5648: if ( p1 )
1.6 noro 5649: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5650: if ( p2 )
1.6 noro 5651: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5652: }
5653:
5654: void ndv_removecont(int mod,NDV p)
5655: {
5656: int i,len,all_p;
5657: Z *c;
5658: P *w;
5659: Z dvr,t;
5660: P g,cont,tp;
5661: NMV m;
5662:
5663: if ( mod == -1 )
5664: ndv_mul_c(mod,p,_invsf(HCM(p)));
5665: else if ( mod == -2 ) {
5666: Z inv;
5667: divlf(ONE,HCZ(p),&inv);
5668: ndv_mul_c_lf(p,inv);
5669: } else if ( mod )
5670: ndv_mul_c(mod,p,invm(HCM(p),mod));
5671: else {
5672: len = p->len;
5673: w = (P *)MALLOC(len*sizeof(P));
5674: c = (Z *)MALLOC(len*sizeof(Q));
5675: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5676: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5677: all_p = all_p && !NUM(w[i]);
5678: }
5679: if ( all_p ) {
5680: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5681: mulp(nd_vc,(P)dvr,g,&cont);
5682: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5683: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5684: }
5685: } else {
5686: sortbynm((Q *)c,len);
5687: qltozl((Q *)c,len,&dvr);
5688: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5689: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5690: }
5691: }
5692: }
5693: }
5694:
5695: /* koko */
5696:
5697: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5698: {
5699: int len,i,max;
5700: NMV m,mr0,mr,t;
5701:
5702: len = p->len;
1.14 noro 5703: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5704: max = MAX(max,TD(DL(m)));
5705: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5706: m = (NMV)((char *)mr0+(len-1)*oadv);
5707: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5708: t = (NMV)MALLOC(nmv_adv);
5709: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5710: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5711: CZ(mr) = CZ(m);
1.1 noro 5712: ndl_copy(DL(t),DL(mr));
5713: }
5714: NV(p)++;
5715: BDY(p) = mr0;
5716: }
5717:
5718: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5719: {
5720: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5721: int pos;
5722: Q *w;
5723: Q dvr,t;
5724: NMV m,r;
5725:
5726: len = p->len;
5727: newnvar = nd_nvar-1;
5728: newexporigin = nd_get_exporigin(ord);
5729: if ( nd_module ) newmpos = newexporigin-1;
5730: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5731: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5732: ndl_dehomogenize(DL(m));
5733: if ( newwpd != nd_wpd ) {
5734: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5735: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5736: CZ(r) = CZ(m);
1.1 noro 5737: if ( nd_module ) pos = MPOS(DL(m));
5738: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5739: adj = nd_exporigin-newexporigin;
5740: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5741: if ( nd_module ) {
5742: DL(r)[newmpos] = pos;
5743: }
5744: }
5745: }
5746: NV(p)--;
5747: }
5748:
5749: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5750: {
5751: int i;
5752: P *tpl,*tpl1;
5753: NODE l;
5754: P h,gcd,t;
5755:
5756: tpl = (P *)MALLOC(m*sizeof(P));
5757: tpl1 = (P *)MALLOC(m*sizeof(P));
5758: bcopy(pl,tpl,m*sizeof(P));
5759: gcd = (P)ONE;
5760: for ( l = nd_hcf; l; l = NEXT(l) ) {
5761: h = (P)BDY(l);
5762: while ( 1 ) {
5763: for ( i = 0; i < m; i++ )
5764: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5765: break;
5766: if ( i == m ) {
5767: bcopy(tpl1,tpl,m*sizeof(P));
5768: mulp(vl,gcd,h,&t); gcd = t;
5769: } else
5770: break;
5771: }
5772: }
5773: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5774: if ( full ) {
5775: heu_nezgcdnpz(vl,tpl,m,&t);
5776: mulp(vl,gcd,t,pr);
5777: } else
5778: *pr = gcd;
5779: }
5780:
5781: void removecont_array(P *p,int n,int full)
5782: {
5783: int all_p,all_q,i;
5784: Z *c;
5785: P *w;
5786: P t,s;
5787:
5788: for ( all_q = 1, i = 0; i < n; i++ )
5789: all_q = all_q && NUM(p[i]);
5790: if ( all_q ) {
5791: removecont_array_q((Z *)p,n);
5792: } else {
5793: c = (Z *)MALLOC(n*sizeof(Z));
5794: w = (P *)MALLOC(n*sizeof(P));
5795: for ( i = 0; i < n; i++ ) {
5796: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5797: }
5798: removecont_array_q(c,n);
5799: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5800: for ( i = 0; i < n; i++ ) {
5801: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5802: }
5803: }
5804: }
5805:
5806: /* c is an int array */
5807:
5808: void removecont_array_q(Z *c,int n)
5809: {
5810: struct oVECT v;
5811: Z d0,d1,a,u,u1,gcd;
5812: int i,j;
5813: Z *q,*r;
5814:
5815: q = (Z *)MALLOC(n*sizeof(Z));
5816: r = (Z *)MALLOC(n*sizeof(Z));
5817: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5818: gcdvz_estimate(&v,&d0);
5819: for ( i = 0; i < n; i++ ) {
5820: divqrz(c[i],d0,&q[i],&r[i]);
5821: }
5822: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5823: if ( i < n ) {
5824: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5825: gcdvz(&v,&d1);
5826: gcdz(d0,d1,&gcd);
1.6 noro 5827: /* exact division */
5828: divsz(d0,gcd,&a);
1.1 noro 5829: for ( i = 0; i < n; i++ ) {
5830: mulz(a,q[i],&u);
5831: if ( r[i] ) {
1.6 noro 5832: /* exact division */
5833: divsz(r[i],gcd,&u1);
1.1 noro 5834: addz(u,u1,&q[i]);
5835: } else
5836: q[i] = u;
5837: }
5838: }
5839: for ( i = 0; i < n; i++ ) c[i] = q[i];
5840: }
5841:
1.4 noro 5842: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5843:
5844: void mpz_removecont_array(mpz_t *c,int n)
5845: {
5846: mpz_t d0,a,u,u1,gcd;
5847: int i,j;
1.13 noro 5848: static mpz_t *q,*r;
5849: static int c_len = 0;
1.4 noro 5850:
5851: for ( i = 0; i < n; i++ )
5852: if ( mpz_sgn(c[i]) ) break;
5853: if ( i == n ) return;
5854: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5855: if ( n > c_len ) {
5856: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5857: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5858: c_len = n;
5859: }
1.4 noro 5860: for ( i = 0; i < n; i++ ) {
5861: mpz_init(q[i]); mpz_init(r[i]);
5862: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5863: }
5864: for ( i = 0; i < n; i++ )
5865: if ( mpz_sgn(r[i]) ) break;
5866: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5867: if ( i < n ) {
5868: mpz_gcd(gcd,d0,r[i]);
5869: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5870: mpz_div(a,d0,gcd);
5871: for ( i = 0; i < n; i++ ) {
5872: mpz_mul(u,a,q[i]);
5873: if ( mpz_sgn(r[i]) ) {
5874: mpz_div(u1,r[i],gcd);
5875: mpz_add(q[i],u,u1);
5876: } else
5877: mpz_set(q[i],u);
5878: }
5879: }
5880: for ( i = 0; i < n; i++ )
5881: mpz_set(c[i],q[i]);
5882: }
5883:
1.1 noro 5884: void nd_mul_c(int mod,ND p,int mul)
5885: {
5886: NM m;
5887: int c,c1;
5888:
5889: if ( !p ) return;
5890: if ( mul == 1 ) return;
5891: if ( mod == -1 )
5892: for ( m = BDY(p); m; m = NEXT(m) )
5893: CM(m) = _mulsf(CM(m),mul);
5894: else
5895: for ( m = BDY(p); m; m = NEXT(m) ) {
5896: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5897: }
5898: }
5899:
5900: void nd_mul_c_lf(ND p,Z mul)
5901: {
5902: NM m;
5903: Z c;
5904:
5905: if ( !p ) return;
5906: if ( UNIZ(mul) ) return;
5907: for ( m = BDY(p); m; m = NEXT(m) ) {
5908: mullf(CZ(m),mul,&c); CZ(m) = c;
5909: }
5910: }
5911:
5912: void nd_mul_c_q(ND p,P mul)
5913: {
5914: NM m;
5915: P c;
5916:
5917: if ( !p ) return;
5918: if ( UNIQ(mul) ) return;
5919: for ( m = BDY(p); m; m = NEXT(m) ) {
5920: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5921: }
5922: }
5923:
5924: void nd_mul_c_p(VL vl,ND p,P mul)
5925: {
5926: NM m;
5927: P c;
5928:
5929: if ( !p ) return;
5930: for ( m = BDY(p); m; m = NEXT(m) ) {
5931: mulp(vl,CP(m),mul,&c); CP(m) = c;
5932: }
5933: }
5934:
5935: void nd_free(ND p)
5936: {
5937: NM t,s;
5938:
5939: if ( !p ) return;
5940: t = BDY(p);
5941: while ( t ) {
5942: s = NEXT(t);
5943: FREENM(t);
5944: t = s;
5945: }
5946: FREEND(p);
5947: }
5948:
5949: void ndv_free(NDV p)
5950: {
5951: GCFREE(BDY(p));
5952: }
5953:
5954: void nd_append_red(UINT *d,int i)
5955: {
5956: RHist m,m0;
5957: int h;
5958:
5959: NEWRHist(m);
5960: h = ndl_hash_value(d);
5961: m->index = i;
5962: ndl_copy(d,DL(m));
5963: NEXT(m) = nd_red[h];
5964: nd_red[h] = m;
5965: }
5966:
5967: UINT *ndv_compute_bound(NDV p)
5968: {
5969: UINT *d1,*d2,*t;
5970: UINT u;
5971: int i,j,k,l,len,ind;
5972: NMV m;
5973:
5974: if ( !p )
5975: return 0;
5976: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5977: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5978: len = LEN(p);
5979: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5980: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5981: ndl_max(DL(m),d1,d2);
5982: t = d1; d1 = d2; d2 = t;
5983: }
5984: l = nd_nvar+31;
5985: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5986: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5987: u = d1[i];
5988: k = (nd_epw-1)*nd_bpe;
5989: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5990: t[ind] = (u>>k)&nd_mask0;
5991: }
5992: for ( ; ind < l; ind++ ) t[ind] = 0;
5993: return t;
5994: }
5995:
5996: UINT *nd_compute_bound(ND p)
5997: {
5998: UINT *d1,*d2,*t;
5999: UINT u;
6000: int i,j,k,l,len,ind;
6001: NM m;
6002:
6003: if ( !p )
6004: return 0;
6005: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6006: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6007: len = LEN(p);
6008: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
6009: for ( m = NEXT(m); m; m = NEXT(m) ) {
6010: ndl_lcm(DL(m),d1,d2);
6011: t = d1; d1 = d2; d2 = t;
6012: }
6013: l = nd_nvar+31;
6014: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
6015: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
6016: u = d1[i];
6017: k = (nd_epw-1)*nd_bpe;
6018: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
6019: t[ind] = (u>>k)&nd_mask0;
6020: }
6021: for ( ; ind < l; ind++ ) t[ind] = 0;
6022: return t;
6023: }
6024:
6025: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
6026: /* of a term. In this case we need additional 1 word. */
6027:
6028: int nd_get_exporigin(struct order_spec *ord)
6029: {
6030: switch ( ord->id ) {
1.21 noro 6031: case 0: case 2: case 256: case 258: case 300:
1.1 noro 6032: return 1+nd_module;
6033: case 1: case 257:
6034: /* block order */
6035: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
6036: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
6037: return ord->ord.block.length+1+nd_module;
6038: case 3: case 259:
6039: #if 0
6040: error("nd_get_exporigin : composite order is not supported yet.");
6041: #else
6042: return 1+nd_module;
6043: #endif
6044: default:
6045: error("nd_get_exporigin : ivalid argument.");
6046: return 0;
6047: }
6048: }
6049:
6050: void nd_setup_parameters(int nvar,int max) {
6051: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
6052: struct order_pair *op;
1.45 noro 6053: extern int CNVars;
1.1 noro 6054:
6055: nd_nvar = nvar;
1.45 noro 6056: CNVars = nvar;
1.1 noro 6057: if ( max ) {
6058: /* XXX */
6059: if ( do_weyl ) nd_bpe = 32;
6060: else if ( max < 2 ) nd_bpe = 1;
6061: else if ( max < 4 ) nd_bpe = 2;
6062: else if ( max < 8 ) nd_bpe = 3;
6063: else if ( max < 16 ) nd_bpe = 4;
6064: else if ( max < 32 ) nd_bpe = 5;
6065: else if ( max < 64 ) nd_bpe = 6;
6066: else if ( max < 256 ) nd_bpe = 8;
6067: else if ( max < 1024 ) nd_bpe = 10;
6068: else if ( max < 65536 ) nd_bpe = 16;
6069: else nd_bpe = 32;
6070: }
6071: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
6072: UINT t;
6073: int st;
6074: int *v;
6075: /* t = max(weights) */
6076: t = 0;
6077: if ( current_dl_weight_vector )
6078: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
6079: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
6080: if ( t < st ) t = st;
6081: }
6082: if ( nd_matrix )
6083: for ( i = 0; i < nd_matrix_len; i++ )
6084: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
6085: if ( (st=v[j]) < 0 ) st = -st;
6086: if ( t < st ) t = st;
6087: }
6088: /* i = bitsize of t */
6089: for ( i = 0; t; t >>=1, i++ );
6090: /* i += bitsize of nd_nvar */
6091: for ( t = nd_nvar; t; t >>=1, i++);
6092: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
6093: if ( (nd_bpe+i) >= 31 )
6094: error("nd_setup_parameters : too large weight");
6095: }
6096: nd_epw = (sizeof(UINT)*8)/nd_bpe;
6097: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
6098: nd_exporigin = nd_get_exporigin(nd_ord);
6099: wpd = nd_exporigin+elen;
6100: if ( nd_module )
6101: nd_mpos = nd_exporigin-1;
6102: else
6103: nd_mpos = -1;
6104: if ( wpd != nd_wpd ) {
6105: nd_free_private_storage();
6106: nd_wpd = wpd;
6107: }
6108: if ( nd_bpe < 32 ) {
6109: nd_mask0 = (1<<nd_bpe)-1;
6110: } else {
6111: nd_mask0 = 0xffffffff;
6112: }
6113: bzero(nd_mask,sizeof(nd_mask));
6114: nd_mask1 = 0;
6115: for ( i = 0; i < nd_epw; i++ ) {
6116: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
6117: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
6118: }
6119: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
6120: nd_epos = nd_create_epos(nd_ord);
6121: nd_blockmask = nd_create_blockmask(nd_ord);
6122: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
6123: }
6124:
6125: ND_pairs nd_reconstruct(int trace,ND_pairs d)
6126: {
6127: int i,obpe,oadv,h;
6128: static NM prev_nm_free_list;
6129: static ND_pairs prev_ndp_free_list;
6130: RHist mr0,mr;
6131: RHist r;
6132: RHist *old_red;
6133: ND_pairs s0,s,t;
6134: EPOS oepos;
6135:
6136: obpe = nd_bpe;
6137: oadv = nmv_adv;
6138: oepos = nd_epos;
6139: if ( obpe < 2 ) nd_bpe = 2;
6140: else if ( obpe < 3 ) nd_bpe = 3;
6141: else if ( obpe < 4 ) nd_bpe = 4;
6142: else if ( obpe < 5 ) nd_bpe = 5;
6143: else if ( obpe < 6 ) nd_bpe = 6;
6144: else if ( obpe < 8 ) nd_bpe = 8;
6145: else if ( obpe < 10 ) nd_bpe = 10;
6146: else if ( obpe < 16 ) nd_bpe = 16;
6147: else if ( obpe < 32 ) nd_bpe = 32;
6148: else error("nd_reconstruct : exponent too large");
6149:
6150: nd_setup_parameters(nd_nvar,0);
6151: prev_nm_free_list = _nm_free_list;
6152: prev_ndp_free_list = _ndp_free_list;
6153: _nm_free_list = 0;
6154: _ndp_free_list = 0;
6155: for ( i = nd_psn-1; i >= 0; i-- ) {
6156: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6157: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6158: }
6159: if ( trace )
6160: for ( i = nd_psn-1; i >= 0; i-- ) {
6161: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6162: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6163: }
6164: s0 = 0;
6165: for ( t = d; t; t = NEXT(t) ) {
6166: NEXTND_pairs(s0,s);
6167: s->i1 = t->i1;
6168: s->i2 = t->i2;
1.24 noro 6169: s->sig = t->sig;
1.1 noro 6170: SG(s) = SG(t);
6171: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6172: }
6173:
6174: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6175: for ( i = 0; i < REDTAB_LEN; i++ ) {
6176: old_red[i] = nd_red[i];
6177: nd_red[i] = 0;
6178: }
6179: for ( i = 0; i < REDTAB_LEN; i++ )
6180: for ( r = old_red[i]; r; r = NEXT(r) ) {
6181: NEWRHist(mr);
6182: mr->index = r->index;
6183: SG(mr) = SG(r);
6184: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6185: h = ndl_hash_value(DL(mr));
6186: NEXT(mr) = nd_red[h];
6187: nd_red[h] = mr;
1.24 noro 6188: mr->sig = r->sig;
1.1 noro 6189: }
6190: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6191: old_red = 0;
6192: for ( i = 0; i < nd_psn; i++ ) {
6193: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6194: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 6195: r->sig = nd_psh[i]->sig;
1.1 noro 6196: nd_psh[i] = r;
6197: }
6198: if ( s0 ) NEXT(s) = 0;
6199: prev_nm_free_list = 0;
6200: prev_ndp_free_list = 0;
6201: #if 0
6202: GC_gcollect();
6203: #endif
6204: return s0;
6205: }
6206:
1.30 noro 6207: void nd_reconstruct_s(int trace,ND_pairs *d)
6208: {
6209: int i,obpe,oadv,h;
6210: static NM prev_nm_free_list;
6211: static ND_pairs prev_ndp_free_list;
6212: RHist mr0,mr;
6213: RHist r;
6214: RHist *old_red;
6215: ND_pairs s0,s,t;
6216: EPOS oepos;
6217:
6218: obpe = nd_bpe;
6219: oadv = nmv_adv;
6220: oepos = nd_epos;
6221: if ( obpe < 2 ) nd_bpe = 2;
6222: else if ( obpe < 3 ) nd_bpe = 3;
6223: else if ( obpe < 4 ) nd_bpe = 4;
6224: else if ( obpe < 5 ) nd_bpe = 5;
6225: else if ( obpe < 6 ) nd_bpe = 6;
6226: else if ( obpe < 8 ) nd_bpe = 8;
6227: else if ( obpe < 10 ) nd_bpe = 10;
6228: else if ( obpe < 16 ) nd_bpe = 16;
6229: else if ( obpe < 32 ) nd_bpe = 32;
6230: else error("nd_reconstruct_s : exponent too large");
6231:
6232: nd_setup_parameters(nd_nvar,0);
6233: prev_nm_free_list = _nm_free_list;
6234: prev_ndp_free_list = _ndp_free_list;
6235: _nm_free_list = 0;
6236: _ndp_free_list = 0;
6237: for ( i = nd_psn-1; i >= 0; i-- ) {
6238: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6239: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6240: }
6241: if ( trace )
6242: for ( i = nd_psn-1; i >= 0; i-- ) {
6243: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6244: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6245: }
6246:
6247: for ( i = 0; i < nd_nbase; i++ ) {
6248: s0 = 0;
6249: for ( t = d[i]; t; t = NEXT(t) ) {
6250: NEXTND_pairs(s0,s);
6251: s->i1 = t->i1;
6252: s->i2 = t->i2;
6253: s->sig = t->sig;
6254: SG(s) = SG(t);
6255: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6256: }
1.47 noro 6257: if ( s0 ) NEXT(s) = 0;
1.30 noro 6258: d[i] = s0;
6259: }
6260:
6261: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6262: for ( i = 0; i < REDTAB_LEN; i++ ) {
6263: old_red[i] = nd_red[i];
6264: nd_red[i] = 0;
6265: }
6266: for ( i = 0; i < REDTAB_LEN; i++ )
6267: for ( r = old_red[i]; r; r = NEXT(r) ) {
6268: NEWRHist(mr);
6269: mr->index = r->index;
6270: SG(mr) = SG(r);
6271: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6272: h = ndl_hash_value(DL(mr));
6273: NEXT(mr) = nd_red[h];
6274: nd_red[h] = mr;
6275: mr->sig = r->sig;
6276: }
6277: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6278: old_red = 0;
6279: for ( i = 0; i < nd_psn; i++ ) {
6280: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6281: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
6282: r->sig = nd_psh[i]->sig;
6283: nd_psh[i] = r;
6284: }
6285: if ( s0 ) NEXT(s) = 0;
6286: prev_nm_free_list = 0;
6287: prev_ndp_free_list = 0;
6288: #if 0
6289: GC_gcollect();
6290: #endif
6291: }
6292:
1.1 noro 6293: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
6294: {
6295: int n,i,ei,oepw,omask0,j,s,ord_l,l;
6296: struct order_pair *op;
6297:
6298: n = nd_nvar;
6299: oepw = (sizeof(UINT)*8)/obpe;
6300: omask0 = (1<<obpe)-1;
6301: TD(r) = TD(d);
6302: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
6303: if ( nd_blockmask ) {
6304: l = nd_blockmask->n;
6305: op = nd_blockmask->order_pair;
6306: for ( i = 1; i < nd_exporigin; i++ )
6307: r[i] = d[i];
6308: for ( j = 0, s = 0; j < l; j++ ) {
6309: ord_l = op[j].length;
6310: for ( i = 0; i < ord_l; i++, s++ ) {
6311: ei = GET_EXP_OLD(d,s);
6312: PUT_EXP(r,s,ei);
6313: }
6314: }
6315: } else {
6316: for ( i = 0; i < n; i++ ) {
6317: ei = GET_EXP_OLD(d,i);
6318: PUT_EXP(r,i,ei);
6319: }
6320: }
6321: if ( nd_module ) MPOS(r) = MPOS(d);
6322: }
6323:
6324: ND nd_copy(ND p)
6325: {
6326: NM m,mr,mr0;
6327: int c,n;
6328: ND r;
6329:
6330: if ( !p )
6331: return 0;
6332: else {
6333: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
6334: NEXTNM(mr0,mr);
6335: CM(mr) = CM(m);
6336: ndl_copy(DL(m),DL(mr));
6337: }
6338: NEXT(mr) = 0;
6339: MKND(NV(p),mr0,LEN(p),r);
6340: SG(r) = SG(p);
6341: return r;
6342: }
6343: }
6344:
6345: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
6346: {
6347: NM m1,m2;
6348: NDV p1,p2;
6349: ND t1,t2;
6350: UINT *lcm;
6351: P gp,tp;
6352: Z g,t;
6353: Z iq;
6354: int td;
6355: LIST hist;
6356: NODE node;
6357: DP d;
6358:
6359: if ( !mod && nd_demand ) {
6360: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
6361: } else {
6362: if ( trace ) {
6363: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
6364: } else {
6365: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
6366: }
6367: }
6368: lcm = LCM(p);
6369: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
6370: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
6371: FREENM(m1); return 0;
6372: }
6373: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
6374: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
6375: FREENM(m1); FREENM(m2); return 0;
6376: }
6377:
6378: if ( mod == -1 ) {
6379: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
6380: } else if ( mod > 0 ) {
6381: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
6382: } else if ( mod == -2 ) {
6383: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
6384: } else if ( nd_vc ) {
6385: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
6386: divsp(nd_vc,HCP(p2),gp,&CP(m1));
6387: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
6388: } else {
1.6 noro 6389: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 6390: }
6391: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
6392: *rp = nd_add(mod,t1,t2);
6393: if ( nd_gentrace ) {
6394: /* nd_tracelist is initialized */
1.6 noro 6395: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6396: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 6397: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6398: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
6399: nd_tracelist = node;
6400: }
1.24 noro 6401: if ( *rp )
6402: (*rp)->sig = p->sig;
1.1 noro 6403: FREENM(m1); FREENM(m2);
6404: return 1;
6405: }
6406:
6407: void ndv_mul_c(int mod,NDV p,int mul)
6408: {
6409: NMV m;
6410: int c,c1,len,i;
6411:
6412: if ( !p ) return;
6413: len = LEN(p);
6414: if ( mod == -1 )
6415: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
6416: CM(m) = _mulsf(CM(m),mul);
6417: else
6418: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6419: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6420: }
6421: }
6422:
6423: void ndv_mul_c_lf(NDV p,Z mul)
6424: {
6425: NMV m;
6426: Z c;
6427: int len,i;
6428:
6429: if ( !p ) return;
6430: len = LEN(p);
6431: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6432: mullf(CZ(m),mul,&c); CZ(m) = c;
6433: }
6434: }
6435:
6436: /* for nd_det */
6437: void ndv_mul_c_q(NDV p,Z mul)
6438: {
6439: NMV m;
6440: Z c;
6441: int len,i;
6442:
6443: if ( !p ) return;
6444: len = LEN(p);
6445: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6446: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6447: }
6448: }
6449:
6450: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6451: int n2,i,j,l,n,tlen;
6452: UINT *d0;
6453: NM *tab,*psum;
6454: ND s,r;
6455: NM t;
6456: NMV m1;
6457:
6458: if ( !p ) return 0;
6459: n = NV(p); n2 = n>>1;
6460: d0 = DL(m0);
6461: l = LEN(p);
6462: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6463: tab = (NM *)MALLOC(tlen*sizeof(NM));
6464: psum = (NM *)MALLOC(tlen*sizeof(NM));
6465: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6466: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6467: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6468: /* m0(NM) * m1(NMV) => tab(NM) */
6469: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6470: for ( j = 0; j < tlen; j++ ) {
6471: if ( tab[j] ) {
6472: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6473: }
6474: }
6475: }
6476: for ( i = tlen-1, r = 0; i >= 0; i-- )
6477: if ( psum[i] ) {
6478: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6479: MKND(n,psum[i],j,s);
6480: r = nd_add(mod,r,s);
6481: }
6482: if ( r ) SG(r) = SG(p)+TD(d0);
6483: return r;
6484: }
6485:
6486: /* product of monomials */
6487: /* XXX block order is not handled correctly */
6488:
6489: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6490: {
6491: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6492: UINT *d0,*d1,*d,*dt,*ctab;
6493: Z *ctab_q;
6494: Z q,q1;
6495: UINT c0,c1,c;
6496: NM *p;
6497: NM m,t;
6498: int mpos;
6499:
6500: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6501: if ( !m0 || !m1 ) return;
6502: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6503: if ( nd_module )
6504: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6505:
6506: NEWNM(m); d = DL(m);
6507: if ( mod ) {
6508: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6509: } else if ( nd_vc )
6510: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6511: else
1.6 noro 6512: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6513: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6514: homo = n&1 ? 1 : 0;
6515: if ( homo ) {
6516: /* offset of h-degree */
6517: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6518: PUT_EXP(DL(m),n-1,h);
6519: TD(DL(m)) = h;
6520: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6521: }
6522: tab[0] = m;
6523: NEWNM(m); d = DL(m);
6524: for ( i = 0, curlen = 1; i < n2; i++ ) {
6525: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6526: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6527: /* xi^a*(Di^k*xi^l)*Di^b */
6528: a += l; b += k;
6529: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6530: if ( !k || !l ) {
6531: for ( j = 0; j < curlen; j++ )
6532: if ( (t = tab[j]) != 0 ) {
6533: dt = DL(t);
6534: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6535: if ( nd_blockmask ) ndl_weight_mask(dt);
6536: }
6537: curlen *= k+1;
6538: continue;
6539: }
6540: min = MIN(k,l);
6541: if ( mod ) {
6542: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6543: mkwcm(k,l,mod,(int *)ctab);
6544: } else {
6545: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6546: mkwc(k,l,ctab_q);
6547: }
6548: for ( j = min; j >= 0; j-- ) {
6549: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6550: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6551: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6552: if ( homo ) {
6553: TD(d) = s;
6554: PUT_EXP(d,n-1,s-h);
6555: } else TD(d) = h;
6556: if ( nd_blockmask ) ndl_weight_mask(d);
6557: if ( mod ) c = ctab[j];
6558: else q = ctab_q[j];
6559: p = tab+curlen*j;
6560: if ( j == 0 ) {
6561: for ( u = 0; u < curlen; u++, p++ ) {
6562: if ( tab[u] ) {
6563: ndl_addto(DL(tab[u]),d);
6564: if ( mod ) {
6565: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6566: } else if ( nd_vc )
6567: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6568: else {
1.6 noro 6569: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6570: }
6571: }
6572: }
6573: } else {
6574: for ( u = 0; u < curlen; u++, p++ ) {
6575: if ( tab[u] ) {
6576: NEWNM(t);
6577: ndl_add(DL(tab[u]),d,DL(t));
6578: if ( mod ) {
6579: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6580: } else if ( nd_vc )
6581: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6582: else
1.6 noro 6583: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6584: *p = t;
6585: }
6586: }
6587: }
6588: }
6589: curlen *= k+1;
6590: }
6591: FREENM(m);
6592: if ( nd_module ) {
6593: mpos = MPOS(d1);
6594: for ( i = 0; i < tlen; i++ )
6595: if ( tab[i] ) {
6596: d = DL(tab[i]);
6597: MPOS(d) = mpos;
6598: TD(d) = ndl_weight(d);
6599: }
6600: }
6601: }
6602:
6603: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6604: {
6605: NM mr,mr0;
6606: NMV m;
6607: UINT *d,*dt,*dm;
6608: int c,n,td,i,c1,c2,len;
6609: Q q;
6610: ND r;
6611:
6612: if ( !p ) return 0;
6613: else {
6614: n = NV(p); m = BDY(p);
6615: d = DL(m0);
6616: len = LEN(p);
6617: mr0 = 0;
6618: td = TD(d);
6619: c = CM(m0);
6620: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6621: NEXTNM(mr0,mr);
6622: CM(mr) = 1;
6623: ndl_add(DL(m),d,DL(mr));
6624: }
6625: NEXT(mr) = 0;
6626: MKND(NV(p),mr0,len,r);
6627: SG(r) = SG(p) + TD(d);
6628: return r;
6629: }
6630: }
6631:
6632: ND ndv_mul_nm(int mod,NM m0,NDV p)
6633: {
6634: NM mr,mr0;
6635: NMV m;
6636: UINT *d,*dt,*dm;
6637: int c,n,td,i,c1,c2,len;
6638: P q;
6639: ND r;
6640:
6641: if ( !p ) return 0;
6642: else if ( do_weyl ) {
6643: if ( mod < 0 ) {
6644: error("ndv_mul_nm : not implemented (weyl)");
6645: return 0;
6646: } else
6647: return weyl_ndv_mul_nm(mod,m0,p);
6648: } else {
6649: n = NV(p); m = BDY(p);
6650: d = DL(m0);
6651: len = LEN(p);
6652: mr0 = 0;
6653: td = TD(d);
6654: if ( mod == -1 ) {
6655: c = CM(m0);
6656: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6657: NEXTNM(mr0,mr);
6658: CM(mr) = _mulsf(CM(m),c);
6659: ndl_add(DL(m),d,DL(mr));
6660: }
6661: } else if ( mod == -2 ) {
6662: Z cl;
6663: cl = CZ(m0);
6664: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6665: NEXTNM(mr0,mr);
6666: mullf(CZ(m),cl,&CZ(mr));
6667: ndl_add(DL(m),d,DL(mr));
6668: }
6669: } else if ( mod ) {
6670: c = CM(m0);
6671: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6672: NEXTNM(mr0,mr);
6673: c1 = CM(m);
6674: DMAR(c1,c,0,mod,c2);
6675: CM(mr) = c2;
6676: ndl_add(DL(m),d,DL(mr));
6677: }
6678: } else {
6679: q = CP(m0);
6680: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6681: NEXTNM(mr0,mr);
6682: mulp(nd_vc,CP(m),q,&CP(mr));
6683: ndl_add(DL(m),d,DL(mr));
6684: }
6685: }
6686: NEXT(mr) = 0;
6687: MKND(NV(p),mr0,len,r);
6688: SG(r) = SG(p) + TD(d);
6689: return r;
6690: }
6691: }
6692:
6693: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6694: {
6695: NM mq0,mq;
6696: NMV tm;
6697: Q q;
6698: int i,nv,sg,c,c1,c2,hindex;
6699: ND p,t,r;
6700:
6701: if ( bucket->m < 0 ) return 0;
6702: else {
6703: nv = NV(d);
6704: mq0 = 0;
6705: tm = (NMV)MALLOC(nmv_adv);
6706: while ( 1 ) {
6707: if ( mod > 0 || mod == -1 )
6708: hindex = head_pbucket(mod,bucket);
6709: else if ( mod == -2 )
6710: hindex = head_pbucket_lf(bucket);
6711: else
6712: hindex = head_pbucket_q(bucket);
6713: if ( hindex < 0 ) break;
6714: p = bucket->body[hindex];
6715: NEXTNM(mq0,mq);
6716: ndl_sub(HDL(p),HDL(d),DL(mq));
6717: ndl_copy(DL(mq),DL(tm));
6718: if ( mod ) {
6719: c1 = invm(HCM(d),mod); c2 = HCM(p);
6720: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6721: CM(tm) = mod-c;
6722: } else {
1.6 noro 6723: divsz(HCZ(p),HCZ(d),&CZ(mq));
6724: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6725: }
6726: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6727: bucket->body[hindex] = nd_remove_head(p);
6728: t = nd_remove_head(t);
6729: add_pbucket(mod,bucket,t);
6730: }
6731: if ( !mq0 )
6732: r = 0;
6733: else {
6734: NEXT(mq) = 0;
6735: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6736: MKND(nv,mq0,i,r);
6737: /* XXX */
6738: SG(r) = HTD(r);
6739: }
6740: return r;
6741: }
6742: }
6743:
6744: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6745: {
6746: NMV m,mr,mr0,t;
6747: int len,i,k;
6748:
6749: if ( !p ) return;
6750: m = BDY(p); len = LEN(p);
6751: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6752: m = (NMV)((char *)mr0+(len-1)*oadv);
6753: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6754: t = (NMV)MALLOC(nmv_adv);
6755: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6756: CZ(t) = CZ(m);
1.1 noro 6757: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6758: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6759: CZ(mr) = CZ(t);
1.1 noro 6760: ndl_copy(DL(t),DL(mr));
6761: }
6762: BDY(p) = mr0;
6763: }
6764:
6765: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6766: {
6767: NMV m,mr,mr0;
6768: int len,i;
6769: NDV r;
6770:
6771: if ( !p ) return 0;
6772: m = BDY(p); len = LEN(p);
6773: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6774: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6775: ndl_zero(DL(mr));
6776: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6777: CZ(mr) = CZ(m);
1.1 noro 6778: }
6779: MKNDV(NV(p),mr0,len,r);
6780: SG(r) = SG(p);
1.24 noro 6781: r->sig = p->sig;
1.1 noro 6782: return r;
6783: }
6784:
6785: /* duplicate p */
6786:
6787: NDV ndv_dup(int mod,NDV p)
6788: {
6789: NDV d;
6790: NMV t,m,m0;
6791: int i,len;
6792:
6793: if ( !p ) return 0;
6794: len = LEN(p);
6795: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6796: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6797: ndl_copy(DL(t),DL(m));
1.6 noro 6798: CZ(m) = CZ(t);
1.1 noro 6799: }
6800: MKNDV(NV(p),m0,len,d);
6801: SG(d) = SG(p);
6802: return d;
6803: }
6804:
6805: NDV ndv_symbolic(int mod,NDV p)
6806: {
6807: NDV d;
6808: NMV t,m,m0;
6809: int i,len;
6810:
6811: if ( !p ) return 0;
6812: len = LEN(p);
6813: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6814: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6815: ndl_copy(DL(t),DL(m));
1.6 noro 6816: CZ(m) = ONE;
1.1 noro 6817: }
6818: MKNDV(NV(p),m0,len,d);
6819: SG(d) = SG(p);
6820: return d;
6821: }
6822:
6823: ND nd_dup(ND p)
6824: {
6825: ND d;
6826: NM t,m,m0;
6827:
6828: if ( !p ) return 0;
6829: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6830: NEXTNM(m0,m);
6831: ndl_copy(DL(t),DL(m));
1.6 noro 6832: CZ(m) = CZ(t);
1.1 noro 6833: }
6834: if ( m0 ) NEXT(m) = 0;
6835: MKND(NV(p),m0,LEN(p),d);
6836: SG(d) = SG(p);
6837: return d;
6838: }
6839:
6840: /* XXX if p->len == 0 then it represents 0 */
6841:
6842: void ndv_mod(int mod,NDV p)
6843: {
6844: NMV t,d;
6845: int r,s,u;
6846: int i,len,dlen;
6847: P cp;
6848: Z c;
6849: Obj gfs;
6850:
6851: if ( !p ) return;
6852: len = LEN(p);
6853: dlen = 0;
6854: if ( mod == -1 )
6855: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6856: simp_ff((Obj)CP(t),&gfs);
6857: if ( gfs ) {
6858: r = FTOIF(CONT((GFS)gfs));
6859: CM(d) = r;
6860: ndl_copy(DL(t),DL(d));
6861: NMV_ADV(d);
6862: dlen++;
6863: }
6864: }
6865: else if ( mod == -2 )
6866: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6867: simp_ff((Obj)CP(t),&gfs);
6868: if ( gfs ) {
6869: lmtolf((LM)gfs,&CZ(d));
6870: ndl_copy(DL(t),DL(d));
6871: NMV_ADV(d);
6872: dlen++;
6873: }
6874: }
6875: else
6876: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6877: if ( nd_vc ) {
6878: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6879: c = (Z)cp;
6880: } else
1.6 noro 6881: c = CZ(t);
1.1 noro 6882: r = remqi((Q)c,mod);
6883: if ( r ) {
6884: CM(d) = r;
6885: ndl_copy(DL(t),DL(d));
6886: NMV_ADV(d);
6887: dlen++;
6888: }
6889: }
6890: LEN(p) = dlen;
6891: }
6892:
6893: NDV ptondv(VL vl,VL dvl,P p)
6894: {
6895: ND nd;
6896:
6897: nd = ptond(vl,dvl,p);
6898: return ndtondv(0,nd);
6899: }
6900:
6901: void pltozpl(LIST l,Q *cont,LIST *pp)
6902: {
1.16 noro 6903: NODE nd,nd1;
6904: int n;
6905: P *pl;
6906: Q *cl;
6907: int i;
6908: P dmy;
6909: Z dvr,inv;
6910: LIST r;
6911:
6912: nd = BDY(l); n = length(nd);
6913: pl = (P *)MALLOC(n*sizeof(P));
6914: cl = (Q *)MALLOC(n*sizeof(Q));
6915: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6916: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6917: }
6918: qltozl(cl,n,&dvr);
6919: divz(ONE,dvr,&inv);
6920: nd = BDY(l);
6921: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6922: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6923: nd = 0;
6924: for ( i = n-1; i >= 0; i-- ) {
6925: MKNODE(nd1,pl[i],nd); nd = nd1;
6926: }
6927: MKLIST(r,nd);
6928: *pp = r;
1.1 noro 6929: }
6930:
6931: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6932:
6933: NDV pltondv(VL vl,VL dvl,LIST p)
6934: {
6935: int i;
6936: NODE t;
6937: ND r,ri;
6938: NM m;
6939:
6940: if ( !nd_module ) error("pltond : module order must be set");
6941: r = 0;
6942: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6943: ri = ptond(vl,dvl,(P)BDY(t));
6944: if ( ri )
6945: for ( m = BDY(ri); m; m = NEXT(m) ) {
6946: MPOS(DL(m)) = i;
6947: TD(DL(m)) = ndl_weight(DL(m));
6948: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6949: }
6950: r = nd_add(0,r,ri);
6951: }
6952: return ndtondv(0,r);
6953: }
6954:
6955: ND ptond(VL vl,VL dvl,P p)
6956: {
6957: int n,i,j,k,e;
6958: VL tvl;
6959: V v;
6960: DCP dc;
6961: DCP *w;
6962: ND r,s,t,u;
6963: P x;
6964: int c;
6965: UINT *d;
6966: NM m,m0;
6967:
6968: if ( !p )
6969: return 0;
6970: else if ( NUM(p) ) {
6971: NEWNM(m);
6972: ndl_zero(DL(m));
1.48 ! noro 6973: if ( RATN(p) && !INT((Q)p) )
1.1 noro 6974: error("ptond : input must be integer-coefficient");
1.6 noro 6975: CZ(m) = (Z)p;
1.1 noro 6976: NEXT(m) = 0;
6977: MKND(nd_nvar,m,1,r);
6978: SG(r) = 0;
6979: return r;
6980: } else {
6981: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6982: w = (DCP *)MALLOC(k*sizeof(DCP));
6983: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6984: for ( i = 0, tvl = dvl, v = VR(p);
6985: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6986: if ( !tvl ) {
6987: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6988: t = ptond(vl,dvl,COEF(w[j]));
6989: pwrp(vl,x,DEG(w[j]),&p);
6990: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6991: }
6992: return s;
6993: } else {
6994: NEWNM(m0); d = DL(m0);
6995: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6996: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6997: TD(d) = MUL_WEIGHT(e,i);
6998: if ( nd_blockmask) ndl_weight_mask(d);
6999: if ( nd_module ) MPOS(d) = 0;
7000: t = ptond(vl,dvl,COEF(w[j]));
7001: for ( m = BDY(t); m; m = NEXT(m) )
7002: ndl_addto(DL(m),d);
7003: SG(t) += TD(d);
7004: s = nd_add(0,s,t);
7005: }
7006: FREENM(m0);
7007: return s;
7008: }
7009: }
7010: }
7011:
7012: P ndvtop(int mod,VL vl,VL dvl,NDV p)
7013: {
7014: VL tvl;
7015: int len,n,j,i,e;
7016: NMV m;
7017: Z q;
7018: P c;
7019: UINT *d;
7020: P s,r,u,t,w;
7021: GFS gfs;
7022:
7023: if ( !p ) return 0;
7024: else {
7025: len = LEN(p);
7026: n = NV(p);
7027: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7028: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
7029: if ( mod == -1 ) {
7030: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7031: } else if ( mod == -2 ) {
7032: c = (P)CZ(m);
7033: } else if ( mod > 0 ) {
1.6 noro 7034: STOZ(CM(m),q); c = (P)q;
1.1 noro 7035: } else
7036: c = CP(m);
7037: d = DL(m);
7038: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7039: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7040: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7041: }
7042: addp(vl,s,t,&u); s = u;
7043: }
7044: return s;
7045: }
7046: }
7047:
7048: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
7049: {
7050: VL tvl;
7051: int len,n,j,i,e;
7052: NMV m;
7053: Z q;
7054: P c;
7055: UINT *d;
7056: P s,r,u,t,w;
7057: GFS gfs;
7058: P *a;
7059: LIST l;
7060: NODE nd,nd1;
7061:
7062: if ( !p ) return 0;
7063: else {
7064: a = (P *)MALLOC((rank+1)*sizeof(P));
7065: for ( i = 0; i <= rank; i++ ) a[i] = 0;
7066: len = LEN(p);
7067: n = NV(p);
7068: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7069: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
7070: if ( mod == -1 ) {
7071: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7072: } else if ( mod ) {
1.6 noro 7073: STOZ(CM(m),q); c = (P)q;
1.1 noro 7074: } else
7075: c = CP(m);
7076: d = DL(m);
7077: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7078: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7079: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7080: }
7081: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
7082: }
7083: nd = 0;
7084: for ( i = rank; i > 0; i-- ) {
7085: MKNODE(nd1,a[i],nd); nd = nd1;
7086: }
7087: MKLIST(l,nd);
7088: return l;
7089: }
7090: }
7091:
7092: NDV ndtondv(int mod,ND p)
7093: {
7094: NDV d;
7095: NMV m,m0;
7096: NM t;
7097: int i,len;
7098:
7099: if ( !p ) return 0;
7100: len = LEN(p);
7101: if ( mod > 0 || mod == -1 )
7102: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7103: else
7104: m0 = m = MALLOC(len*nmv_adv);
7105: #if 0
7106: ndv_alloc += nmv_adv*len;
7107: #endif
7108: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
7109: ndl_copy(DL(t),DL(m));
1.6 noro 7110: CZ(m) = CZ(t);
1.1 noro 7111: }
7112: MKNDV(NV(p),m0,len,d);
7113: SG(d) = SG(p);
1.24 noro 7114: d->sig = p->sig;
1.1 noro 7115: return d;
7116: }
7117:
1.16 noro 7118: static int dmm_comp_nv;
7119:
7120: int dmm_comp(DMM *a,DMM *b)
7121: {
7122: return -compdmm(dmm_comp_nv,*a,*b);
7123: }
7124:
7125: void dmm_sort_by_ord(DMM *a,int len,int nv)
7126: {
7127: dmm_comp_nv = nv;
7128: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
7129: }
7130:
7131: void dpm_sort(DPM p,DPM *rp)
7132: {
7133: DMM t,t1;
7134: int len,i,n;
7135: DMM *a;
7136: DPM d;
7137:
7138: if ( !p ) *rp = 0;
7139: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7140: a = (DMM *)MALLOC(len*sizeof(DMM));
7141: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7142: n = p->nv;
7143: dmm_sort_by_ord(a,len,n);
7144: t = 0;
7145: for ( i = len-1; i >= 0; i-- ) {
7146: NEWDMM(t1);
7147: t1->c = a[i]->c;
7148: t1->dl = a[i]->dl;
7149: t1->pos = a[i]->pos;
7150: t1->next = t;
7151: t = t1;
7152: }
7153: MKDPM(n,t,d);
7154: SG(d) = SG(p);
7155: *rp = d;
7156: }
7157:
1.18 noro 7158: int dpm_comp(DPM *a,DPM *b)
7159: {
1.22 noro 7160: return -compdpm(CO,*a,*b);
1.18 noro 7161: }
7162:
7163: NODE dpm_sort_list(NODE l)
7164: {
7165: int i,len;
7166: NODE t,t1;
7167: DPM *a;
7168:
7169: len = length(l);
7170: a = (DPM *)MALLOC(len*sizeof(DPM));
7171: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
7172: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
7173: t = 0;
7174: for ( i = len-1; i >= 0; i-- ) {
7175: MKNODE(t1,(pointer)a[i],t); t = t1;
7176: }
7177: return t;
7178: }
7179:
1.20 noro 7180: int nmv_comp(NMV a,NMV b)
7181: {
1.21 noro 7182: int t;
7183: t = DL_COMPARE(a->dl,b->dl);
7184: return -t;
1.20 noro 7185: }
7186:
1.43 noro 7187: NDV dptondv(int mod,DP p)
7188: {
7189: NDV d;
7190: NMV m,m0;
7191: MP t;
7192: MP *a;
7193: int i,len,n;
7194:
7195: if ( !p ) return 0;
7196: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7197: n = p->nv;
7198: if ( mod > 0 || mod == -1 )
7199: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7200: else
7201: m0 = m = MALLOC(len*nmv_adv);
7202: for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
7203: dltondl(n,t->dl,DL(m));
7204: TD(DL(m)) = ndl_weight(DL(m));
7205: CZ(m) = (Z)t->c;
7206: }
7207: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
7208: MKNDV(NV(p),m0,len,d);
7209: SG(d) = SG(p);
7210: return d;
7211: }
7212:
1.16 noro 7213: NDV dpmtondv(int mod,DPM p)
7214: {
7215: NDV d;
7216: NMV m,m0;
7217: DMM t;
7218: DMM *a;
7219: int i,len,n;
7220:
7221: if ( !p ) return 0;
7222: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7223: a = (DMM *)MALLOC(len*sizeof(DMM));
7224: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7225: n = p->nv;
7226: dmm_sort_by_ord(a,len,n);
7227: if ( mod > 0 || mod == -1 )
7228: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7229: else
7230: m0 = m = MALLOC(len*nmv_adv);
7231: #if 0
7232: ndv_alloc += nmv_adv*len;
7233: #endif
7234: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7235: dltondl(n,a[i]->dl,DL(m));
7236: MPOS(DL(m)) = a[i]->pos;
1.20 noro 7237: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 7238: CZ(m) = (Z)a[i]->c;
7239: }
1.20 noro 7240: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 7241: MKNDV(NV(p),m0,len,d);
7242: SG(d) = SG(p);
7243: return d;
7244: }
7245:
1.1 noro 7246: ND ndvtond(int mod,NDV p)
7247: {
7248: ND d;
7249: NM m,m0;
7250: NMV t;
7251: int i,len;
7252:
7253: if ( !p ) return 0;
7254: m0 = 0;
7255: len = p->len;
7256: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7257: NEXTNM(m0,m);
7258: ndl_copy(DL(t),DL(m));
1.6 noro 7259: CZ(m) = CZ(t);
1.1 noro 7260: }
7261: NEXT(m) = 0;
7262: MKND(NV(p),m0,len,d);
7263: SG(d) = SG(p);
1.28 noro 7264: d->sig = p->sig;
1.1 noro 7265: return d;
7266: }
7267:
7268: DP ndvtodp(int mod,NDV p)
7269: {
7270: MP m,m0;
7271: DP d;
7272: NMV t;
7273: int i,len;
7274:
7275: if ( !p ) return 0;
7276: m0 = 0;
7277: len = p->len;
7278: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7279: NEXTMP(m0,m);
7280: m->dl = ndltodl(nd_nvar,DL(t));
7281: m->c = (Obj)ndctop(mod,t->c);
7282: }
7283: NEXT(m) = 0;
7284: MKDP(nd_nvar,m0,d);
7285: SG(d) = SG(p);
7286: return d;
7287: }
7288:
1.44 noro 7289: DPM sigtodpm(SIG s)
7290: {
7291: DMM m;
7292: DPM d;
7293:
7294: NEWDMM(m);
7295: m->c = (Obj)ONE;
7296: m->dl = s->dl;
7297: m->pos = s->pos+1;
7298: m->next = 0;
7299: MKDPM(nd_nvar,m,d);
7300: SG(d) = s->dl->td;
7301: return d;
7302: }
7303:
1.16 noro 7304: DPM ndvtodpm(int mod,NDV p)
7305: {
7306: DMM m,m0;
7307: DPM d;
7308: NMV t;
7309: int i,len;
7310:
7311: if ( !p ) return 0;
7312: m0 = 0;
7313: len = p->len;
7314: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7315: NEXTDMM(m0,m);
7316: m->dl = ndltodl(nd_nvar,DL(t));
7317: m->c = (Obj)ndctop(mod,t->c);
7318: m->pos = MPOS(DL(t));
7319: }
7320: NEXT(m) = 0;
7321: MKDPM(nd_nvar,m0,d);
7322: SG(d) = SG(p);
7323: return d;
7324: }
7325:
7326:
1.1 noro 7327: DP ndtodp(int mod,ND p)
7328: {
7329: MP m,m0;
7330: DP d;
7331: NM t;
7332: int i,len;
7333:
7334: if ( !p ) return 0;
7335: m0 = 0;
7336: len = p->len;
7337: for ( t = BDY(p); t; t = NEXT(t) ) {
7338: NEXTMP(m0,m);
7339: m->dl = ndltodl(nd_nvar,DL(t));
7340: m->c = (Obj)ndctop(mod,t->c);
7341: }
7342: NEXT(m) = 0;
7343: MKDP(nd_nvar,m0,d);
7344: SG(d) = SG(p);
7345: return d;
7346: }
7347:
7348: void ndv_print(NDV p)
7349: {
7350: NMV m;
7351: int i,len;
7352:
7353: if ( !p ) printf("0\n");
7354: else {
7355: len = LEN(p);
7356: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7357: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 7358: else printf("+%ld*",CM(m));
1.1 noro 7359: ndl_print(DL(m));
7360: }
7361: printf("\n");
7362: }
7363: }
7364:
7365: void ndv_print_q(NDV p)
7366: {
7367: NMV m;
7368: int i,len;
7369:
7370: if ( !p ) printf("0\n");
7371: else {
7372: len = LEN(p);
7373: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7374: printf("+");
1.6 noro 7375: printexpr(CO,(Obj)CZ(m));
1.1 noro 7376: printf("*");
7377: ndl_print(DL(m));
7378: }
7379: printf("\n");
7380: }
7381: }
7382:
7383: NODE ndv_reducebase(NODE x,int *perm)
7384: {
7385: int len,i,j;
7386: NDVI w;
7387: NODE t,t0;
7388:
1.41 noro 7389: if ( nd_norb ) return x;
1.1 noro 7390: len = length(x);
7391: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
7392: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
7393: w[i].p = BDY(t); w[i].i = perm[i];
7394: }
7395: for ( i = 0; i < len; i++ ) {
7396: for ( j = 0; j < i; j++ ) {
7397: if ( w[i].p && w[j].p ) {
7398: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
7399: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
7400: }
7401: }
7402: }
7403: for ( i = j = 0, t0 = 0; i < len; i++ ) {
7404: if ( w[i].p ) {
7405: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
7406: perm[j++] = w[i].i;
7407: }
7408: }
7409: NEXT(t) = 0; x = t0;
7410: return x;
7411: }
7412:
7413: /* XXX incomplete */
7414:
1.21 noro 7415: extern DMMstack dmm_stack;
7416: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 7417:
1.1 noro 7418: void nd_init_ord(struct order_spec *ord)
7419: {
7420: nd_module = (ord->id >= 256);
7421: if ( nd_module ) {
7422: nd_dcomp = -1;
1.21 noro 7423: nd_module_ordtype = ord->module_ordtype;
1.1 noro 7424: nd_pot_nelim = ord->pot_nelim;
7425: nd_poly_weight_len = ord->nv;
7426: nd_poly_weight = ord->top_weight;
7427: nd_module_rank = ord->module_rank;
7428: nd_module_weight = ord->module_top_weight;
7429: }
7430: nd_matrix = 0;
7431: nd_matrix_len = 0;
7432: switch ( ord->id ) {
7433: case 0:
7434: switch ( ord->ord.simple ) {
7435: case 0:
7436: nd_dcomp = 1;
7437: nd_isrlex = 1;
7438: break;
7439: case 1:
7440: nd_dcomp = 1;
7441: nd_isrlex = 0;
7442: break;
7443: case 2:
7444: nd_dcomp = 0;
7445: nd_isrlex = 0;
7446: ndl_compare_function = ndl_lex_compare;
7447: break;
7448: case 11:
7449: /* XXX */
7450: nd_dcomp = 0;
7451: nd_isrlex = 1;
7452: ndl_compare_function = ndl_ww_lex_compare;
7453: break;
7454: default:
7455: error("nd_gr : unsupported order");
7456: }
7457: break;
7458: case 1:
7459: /* block order */
7460: /* XXX */
7461: nd_dcomp = -1;
7462: nd_isrlex = 0;
7463: ndl_compare_function = ndl_block_compare;
7464: break;
7465: case 2:
7466: /* matrix order */
7467: /* XXX */
7468: nd_dcomp = -1;
7469: nd_isrlex = 0;
7470: nd_matrix_len = ord->ord.matrix.row;
7471: nd_matrix = ord->ord.matrix.matrix;
7472: ndl_compare_function = ndl_matrix_compare;
7473: break;
7474: case 3:
7475: /* composite order */
7476: nd_dcomp = -1;
7477: nd_isrlex = 0;
7478: nd_worb_len = ord->ord.composite.length;
7479: nd_worb = ord->ord.composite.w_or_b;
7480: ndl_compare_function = ndl_composite_compare;
7481: break;
7482:
7483: /* module order */
7484: case 256:
7485: switch ( ord->ord.simple ) {
7486: case 0:
1.21 noro 7487: nd_dcomp = 0;
1.1 noro 7488: nd_isrlex = 1;
1.21 noro 7489: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7490: break;
7491: case 1:
1.21 noro 7492: nd_dcomp = 0;
1.1 noro 7493: nd_isrlex = 0;
7494: ndl_compare_function = ndl_module_glex_compare;
7495: break;
7496: case 2:
1.21 noro 7497: nd_dcomp = 0;
1.1 noro 7498: nd_isrlex = 0;
1.21 noro 7499: ndl_compare_function = ndl_module_compare;
7500: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7501: break;
7502: default:
1.21 noro 7503: error("nd_init_ord : unsupported order");
1.1 noro 7504: }
7505: break;
7506: case 257:
7507: /* block order */
7508: nd_isrlex = 0;
1.21 noro 7509: ndl_compare_function = ndl_module_compare;
7510: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7511: break;
7512: case 258:
7513: /* matrix order */
7514: nd_isrlex = 0;
7515: nd_matrix_len = ord->ord.matrix.row;
7516: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7517: ndl_compare_function = ndl_module_compare;
7518: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7519: break;
7520: case 259:
7521: /* composite order */
7522: nd_isrlex = 0;
7523: nd_worb_len = ord->ord.composite.length;
7524: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7525: ndl_compare_function = ndl_module_compare;
7526: ndl_base_compare_function = ndl_composite_compare;
7527: break;
7528: case 300:
7529: /* schreyer order */
7530: if ( ord->base->id != 256 )
7531: error("nd_init_ord : unsupported base order");
7532: ndl_compare_function = ndl_module_schreyer_compare;
7533: dmm_stack = ord->dmmstack;
7534: switch ( ord->base->ord.simple ) {
7535: case 0:
7536: nd_isrlex = 1;
7537: ndl_base_compare_function = ndl_glex_compare;
7538: dl_base_compare_function = cmpdl_revgradlex;
7539: break;
7540: case 1:
7541: nd_isrlex = 0;
7542: ndl_base_compare_function = ndl_glex_compare;
7543: dl_base_compare_function = cmpdl_gradlex;
7544: break;
7545: case 2:
7546: nd_isrlex = 0;
7547: ndl_base_compare_function = ndl_lex_compare;
7548: dl_base_compare_function = cmpdl_lex;
7549: break;
7550: default:
7551: error("nd_init_ord : unsupported order");
7552: }
1.1 noro 7553: break;
7554: }
7555: nd_ord = ord;
7556: }
7557:
7558: BlockMask nd_create_blockmask(struct order_spec *ord)
7559: {
7560: int n,i,j,s,l;
7561: UINT *t;
7562: BlockMask bm;
7563:
7564: /* we only create mask table for block order */
7565: if ( ord->id != 1 && ord->id != 257 )
7566: return 0;
7567: n = ord->ord.block.length;
7568: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7569: bm->n = n;
7570: bm->order_pair = ord->ord.block.order_pair;
7571: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7572: for ( i = 0, s = 0; i < n; i++ ) {
7573: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7574: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7575: l = bm->order_pair[i].length;
7576: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7577: }
7578: return bm;
7579: }
7580:
7581: EPOS nd_create_epos(struct order_spec *ord)
7582: {
7583: int i,j,l,s,ord_l,ord_o;
7584: EPOS epos;
7585: struct order_pair *op;
7586:
7587: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7588: switch ( ord->id ) {
1.21 noro 7589: case 0: case 256: case 300:
1.1 noro 7590: if ( nd_isrlex ) {
7591: for ( i = 0; i < nd_nvar; i++ ) {
7592: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7593: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7594: }
7595: } else {
7596: for ( i = 0; i < nd_nvar; i++ ) {
7597: epos[i].i = nd_exporigin + i/nd_epw;
7598: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7599: }
7600: }
7601: break;
7602: case 1: case 257:
7603: /* block order */
7604: l = ord->ord.block.length;
7605: op = ord->ord.block.order_pair;
7606: for ( j = 0, s = 0; j < l; j++ ) {
7607: ord_o = op[j].order;
7608: ord_l = op[j].length;
7609: if ( !ord_o )
7610: for ( i = 0; i < ord_l; i++ ) {
7611: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7612: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7613: }
7614: else
7615: for ( i = 0; i < ord_l; i++ ) {
7616: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7617: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7618: }
7619: s += ord_l;
7620: }
7621: break;
7622: case 2:
7623: /* matrix order */
7624: case 3:
7625: /* composite order */
7626: default:
7627: for ( i = 0; i < nd_nvar; i++ ) {
7628: epos[i].i = nd_exporigin + i/nd_epw;
7629: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7630: }
7631: break;
7632: }
7633: return epos;
7634: }
7635:
7636: /* external interface */
7637:
7638: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7639: {
7640: NODE t,in0,in;
7641: ND ndf,nf;
7642: NDV ndvf;
7643: VL vv,tv;
7644: int stat,nvar,max,mrank;
7645: union oNDC dn;
7646: Q cont;
7647: P pp;
7648: LIST ppl;
7649:
7650: if ( !f ) {
7651: *rp = 0;
7652: return;
7653: }
7654: pltovl(v,&vv);
7655: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7656:
7657: /* max=65536 implies nd_bpe=32 */
7658: max = 65536;
7659:
7660: nd_module = 0;
7661: /* nd_module will be set if ord is a module ordering */
7662: nd_init_ord(ord);
7663: nd_setup_parameters(nvar,max);
7664: if ( nd_module && OID(f) != O_LIST )
7665: error("nd_nf_p : the first argument must be a list");
7666: if ( nd_module ) mrank = length(BDY((LIST)f));
7667: /* conversion to ndv */
7668: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7669: NEXTNODE(in0,in);
7670: if ( nd_module ) {
7671: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7672: || length(BDY((LIST)BDY(t))) != mrank )
7673: error("nd_nf_p : inconsistent basis element");
7674: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7675: else ppl = (LIST)BDY(t);
7676: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7677: } else {
7678: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7679: else pp = (P)BDY(t);
7680: BDY(in) = (pointer)ptondv(CO,vv,pp);
7681: }
7682: if ( m ) ndv_mod(m,(NDV)BDY(in));
7683: }
7684: if ( in0 ) NEXT(in) = 0;
7685:
7686: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7687: else ndvf = ptondv(CO,vv,(P)f);
7688: if ( m ) ndv_mod(m,ndvf);
7689: ndf = (pointer)ndvtond(m,ndvf);
7690:
7691: /* dont sort, dont removecont */
1.24 noro 7692: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7693: nd_scale=2;
1.6 noro 7694: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7695: if ( !stat )
7696: error("nd_nf_p : exponent too large");
7697: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7698: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7699: }
7700:
7701: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7702: {
7703: NM m;
7704: UINT *t,*s;
7705: int i;
7706:
7707: for ( i = 0; i < n; i++ ) r[i] = 0;
7708: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7709: t = DL(m);
7710: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7711: r[i] = CM(m);
7712: }
7713: for ( i = 0; !r[i]; i++ );
7714: return i;
7715: }
7716:
7717: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7718: {
7719: NM m;
7720: UINT *t,*s;
7721: int i;
7722:
7723: for ( i = 0; i < n; i++ ) r[i] = 0;
7724: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7725: t = DL(m);
7726: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7727: r[i] = CZ(m);
1.1 noro 7728: }
7729: for ( i = 0; !r[i]; i++ );
7730: return i;
7731: }
7732:
7733: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7734: {
7735: NM m;
7736: UINT *t,*s;
7737: int i;
7738:
7739: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7740: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7741: t = DL(m);
7742: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7743: mpz_set(r[i],BDY(CZ(m)));
7744: }
7745: for ( i = 0; !mpz_sgn(r[i]); i++ );
7746: return i;
7747: }
7748:
7749: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7750: {
7751: NM m;
7752: unsigned long *v;
7753: int i,j,h,size;
7754: UINT *s,*t;
7755:
7756: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7757: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7758: bzero(v,size);
7759: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7760: t = DL(m);
7761: h = ndl_hash_value(t);
7762: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7763: v[i/BLEN] |= 1L <<(i%BLEN);
7764: }
7765: return v;
7766: }
7767:
7768: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7769: {
7770: NMV mr;
7771: UINT *d,*t,*s;
7772: int i,j,len,h,head;
7773:
7774: d = DL(m);
7775: len = LEN(p);
7776: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7777: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7778: ndl_add(d,DL(mr),t);
7779: h = ndl_hash_value(t);
7780: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7781: if ( j == 0 ) head = i;
7782: v[i/BLEN] |= 1L <<(i%BLEN);
7783: }
7784: return head;
7785: }
7786:
7787: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7788: {
7789: NM m;
7790: NMV mr;
7791: UINT *d,*t,*s;
7792: NDV p;
7793: int i,j,len;
7794: Z *r;
7795:
7796: m = pair->mul;
7797: d = DL(m);
7798: p = nd_ps[pair->index];
7799: len = LEN(p);
7800: r = (Z *)CALLOC(n,sizeof(Q));
7801: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7802: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7803: ndl_add(d,DL(mr),t);
7804: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7805: r[i] = CZ(mr);
1.1 noro 7806: }
7807: return r;
7808: }
7809:
1.11 noro 7810: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7811: {
7812: NM m;
7813: NMV mr;
1.11 noro 7814: UINT *d,*t,*s,*u;
1.1 noro 7815: NDV p;
7816: unsigned char *ivc;
7817: unsigned short *ivs;
7818: UINT *v,*ivi,*s0v;
1.11 noro 7819: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7820: IndArray r;
7821:
7822: m = pair->mul;
7823: d = DL(m);
7824: if ( trace )
7825: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7826: else
7827: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7828:
7829: len = LEN(p);
7830: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7831: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7832: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7833: ndl_add(d,DL(mr),t);
7834: st = prev;
7835: ed = n;
7836: while ( ed > st ) {
7837: md = (st+ed)/2;
7838: u = s0+md*nd_wpd;
7839: c = DL_COMPARE(u,t);
7840: if ( c == 0 ) break;
7841: else if ( c > 0 ) st = md;
7842: else ed = md;
7843: }
7844: prev = v[j] = md;
1.1 noro 7845: }
7846: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7847: r->head = v[0];
7848: diff = 0;
7849: for ( i = 1; i < len; i++ ) {
7850: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7851: }
7852: if ( diff < 256 ) {
7853: r->width = 1;
7854: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7855: r->index.c = ivc;
7856: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7857: } else if ( diff < 65536 ) {
7858: r->width = 2;
7859: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7860: r->index.s = ivs;
7861: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7862: } else {
7863: r->width = 4;
7864: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7865: r->index.i = ivi;
7866: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7867: }
7868: return r;
7869: }
7870:
7871: int compress_array(Z *svect,Z *cvect,int n)
7872: {
7873: int i,j;
7874:
7875: for ( i = j = 0; i < n; i++ )
7876: if ( svect[i] ) cvect[j++] = svect[i];
7877: return j;
7878: }
7879:
7880: void expand_array(Z *svect,Z *cvect,int n)
7881: {
7882: int i,j;
7883:
7884: for ( i = j = 0; j < n; i++ )
7885: if ( svect[i] ) svect[i] = cvect[j++];
7886: }
7887:
1.8 noro 7888: #if 0
1.1 noro 7889: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7890: {
7891: int i,j,k,len,pos,prev,nz;
7892: Z cs,mcs,c1,c2,cr,gcd,t;
7893: IndArray ivect;
7894: unsigned char *ivc;
7895: unsigned short *ivs;
7896: unsigned int *ivi;
7897: NDV redv;
7898: NMV mr;
7899: NODE rp;
7900: int maxrs;
7901: double hmag;
7902: Z *cvect;
1.3 noro 7903: int l;
1.1 noro 7904:
7905: maxrs = 0;
7906: for ( i = 0; i < col && !svect[i]; i++ );
7907: if ( i == col ) return maxrs;
7908: hmag = p_mag((P)svect[i])*nd_scale;
7909: cvect = (Z *)MALLOC(col*sizeof(Q));
7910: for ( i = 0; i < nred; i++ ) {
7911: ivect = imat[i];
7912: k = ivect->head;
7913: if ( svect[k] ) {
7914: maxrs = MAX(maxrs,rp0[i]->sugar);
7915: redv = nd_demand?ndv_load(rp0[i]->index)
7916: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7917: len = LEN(redv); mr = BDY(redv);
1.6 noro 7918: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7919: chsgnz(cs,&mcs);
7920: if ( !UNIQ(cr) ) {
7921: for ( j = 0; j < col; j++ ) {
7922: mulz(svect[j],cr,&c1); svect[j] = c1;
7923: }
7924: }
7925: svect[k] = 0; prev = k;
7926: switch ( ivect->width ) {
7927: case 1:
7928: ivc = ivect->index.c;
7929: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7930: pos = prev+ivc[j]; prev = pos;
1.6 noro 7931: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7932: }
7933: break;
7934: case 2:
7935: ivs = ivect->index.s;
7936: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7937: pos = prev+ivs[j]; prev = pos;
1.6 noro 7938: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7939: }
7940: break;
7941: case 4:
7942: ivi = ivect->index.i;
7943: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7944: pos = prev+ivi[j]; prev = pos;
1.6 noro 7945: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7946: }
7947: break;
7948: }
7949: for ( j = k+1; j < col && !svect[j]; j++ );
7950: if ( j == col ) break;
7951: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7952: nz = compress_array(svect,cvect,col);
7953: removecont_array((P *)cvect,nz,1);
7954: expand_array(svect,cvect,nz);
7955: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7956: }
7957: }
7958: }
7959: nz = compress_array(svect,cvect,col);
7960: removecont_array((P *)cvect,nz,1);
7961: expand_array(svect,cvect,nz);
7962: if ( DP_Print ) {
7963: fprintf(asir_out,"-"); fflush(asir_out);
7964: }
7965: return maxrs;
7966: }
1.4 noro 7967: #else
1.9 noro 7968:
1.4 noro 7969: /* direct mpz version */
7970: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7971: {
7972: int i,j,k,len,pos,prev;
7973: mpz_t cs,cr,gcd;
7974: IndArray ivect;
7975: unsigned char *ivc;
7976: unsigned short *ivs;
7977: unsigned int *ivi;
7978: NDV redv;
7979: NMV mr;
7980: NODE rp;
7981: int maxrs;
7982: double hmag;
7983: int l;
1.13 noro 7984: static mpz_t *svect;
7985: static int svect_len=0;
1.4 noro 7986:
7987: maxrs = 0;
7988: for ( i = 0; i < col && !svect0[i]; i++ );
7989: if ( i == col ) return maxrs;
7990: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7991: if ( col > svect_len ) {
7992: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7993: svect_len = col;
7994: }
1.4 noro 7995: for ( i = 0; i < col; i++ ) {
7996: mpz_init(svect[i]);
7997: if ( svect0[i] )
7998: mpz_set(svect[i],BDY(svect0[i]));
7999: else
8000: mpz_set_ui(svect[i],0);
8001: }
8002: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
8003: for ( i = 0; i < nred; i++ ) {
8004: ivect = imat[i];
8005: k = ivect->head;
8006: if ( mpz_sgn(svect[k]) ) {
8007: maxrs = MAX(maxrs,rp0[i]->sugar);
8008: redv = nd_demand?ndv_load(rp0[i]->index)
8009: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
8010: len = LEN(redv); mr = BDY(redv);
1.6 noro 8011: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 8012: mpz_div(cs,svect[k],gcd);
1.6 noro 8013: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 8014: mpz_neg(cs,cs);
1.9 noro 8015: if ( MUNIMPZ(cr) )
8016: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
8017: else if ( !UNIMPZ(cr) )
8018: for ( j = 0; j < col; j++ ) {
8019: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
8020: }
1.4 noro 8021: mpz_set_ui(svect[k],0);
8022: prev = k;
8023: switch ( ivect->width ) {
8024: case 1:
8025: ivc = ivect->index.c;
8026: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8027: pos = prev+ivc[j]; prev = pos;
1.6 noro 8028: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8029: }
8030: break;
8031: case 2:
8032: ivs = ivect->index.s;
8033: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8034: pos = prev+ivs[j]; prev = pos;
1.6 noro 8035: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8036: }
8037: break;
8038: case 4:
8039: ivi = ivect->index.i;
8040: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8041: pos = prev+ivi[j]; prev = pos;
1.6 noro 8042: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8043: }
8044: break;
8045: }
8046: for ( j = k+1; j < col && !svect[j]; j++ );
8047: if ( j == col ) break;
8048: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
8049: mpz_removecont_array(svect,col);
8050: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
8051: }
8052: }
8053: }
8054: mpz_removecont_array(svect,col);
8055: if ( DP_Print ) {
8056: fprintf(asir_out,"-"); fflush(asir_out);
8057: }
8058: for ( i = 0; i < col; i++ )
8059: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
8060: else svect0[i] = 0;
8061: return maxrs;
8062: }
8063: #endif
1.1 noro 8064:
1.40 noro 8065: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 8066: {
8067: int i,j,k,len,pos,prev;
8068: UINT c,c1,c2,c3,up,lo,dmy;
8069: IndArray ivect;
8070: unsigned char *ivc;
8071: unsigned short *ivs;
8072: unsigned int *ivi;
8073: NDV redv;
8074: NMV mr;
8075: NODE rp;
8076: int maxrs;
8077:
8078: maxrs = 0;
8079: for ( i = 0; i < nred; i++ ) {
8080: ivect = imat[i];
8081: k = ivect->head; svect[k] %= m;
1.40 noro 8082: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 8083: maxrs = MAX(maxrs,rp0[i]->sugar);
8084: c = m-c; redv = nd_ps[rp0[i]->index];
8085: len = LEN(redv); mr = BDY(redv);
8086: svect[k] = 0; prev = k;
8087: switch ( ivect->width ) {
8088: case 1:
8089: ivc = ivect->index.c;
8090: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8091: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 8092: if ( c1 ) {
8093: c2 = svect[pos];
1.1 noro 8094: DMA(c1,c,c2,up,lo);
8095: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8096: } else svect[pos] = lo;
1.40 noro 8097: }
1.1 noro 8098: }
8099: break;
8100: case 2:
8101: ivs = ivect->index.s;
8102: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8103: pos = prev+ivs[j]; c1 = CM(mr);
8104: prev = pos;
1.40 noro 8105: if ( c1 ) {
8106: c2 = svect[pos];
1.1 noro 8107: DMA(c1,c,c2,up,lo);
8108: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8109: } else svect[pos] = lo;
1.40 noro 8110: }
1.1 noro 8111: }
8112: break;
8113: case 4:
8114: ivi = ivect->index.i;
8115: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8116: pos = prev+ivi[j]; c1 = CM(mr);
8117: prev = pos;
1.40 noro 8118: if ( c1 ) {
8119: c2 = svect[pos];
1.1 noro 8120: DMA(c1,c,c2,up,lo);
8121: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8122: } else svect[pos] = lo;
1.40 noro 8123: }
1.1 noro 8124: }
8125: break;
8126: }
8127: }
8128: }
8129: for ( i = 0; i < col; i++ )
8130: if ( svect[i] >= (UINT)m ) svect[i] %= m;
8131: return maxrs;
8132: }
8133:
8134: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8135: {
8136: int i,j,k,len,pos,prev;
8137: UINT c,c1,c2,c3,up,lo,dmy;
8138: IndArray ivect;
8139: unsigned char *ivc;
8140: unsigned short *ivs;
8141: unsigned int *ivi;
8142: NDV redv;
8143: NMV mr;
8144: NODE rp;
8145: int maxrs;
8146:
8147: maxrs = 0;
8148: for ( i = 0; i < nred; i++ ) {
8149: ivect = imat[i];
8150: k = ivect->head;
8151: if ( (c = svect[k]) != 0 ) {
8152: maxrs = MAX(maxrs,rp0[i]->sugar);
8153: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
8154: len = LEN(redv); mr = BDY(redv);
8155: svect[k] = 0; prev = k;
8156: switch ( ivect->width ) {
8157: case 1:
8158: ivc = ivect->index.c;
8159: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8160: pos = prev+ivc[j]; prev = pos;
8161: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8162: }
8163: break;
8164: case 2:
8165: ivs = ivect->index.s;
8166: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8167: pos = prev+ivs[j]; prev = pos;
8168: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8169: }
8170: break;
8171: case 4:
8172: ivi = ivect->index.i;
8173: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8174: pos = prev+ivi[j]; prev = pos;
8175: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8176: }
8177: break;
8178: }
8179: }
8180: }
8181: return maxrs;
8182: }
8183:
8184: ND nd_add_lf(ND p1,ND p2)
8185: {
8186: int n,c,can;
8187: ND r;
8188: NM m1,m2,mr0,mr,s;
8189: Z t;
8190:
8191: if ( !p1 ) return p2;
8192: else if ( !p2 ) return p1;
8193: else {
8194: can = 0;
8195: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
8196: c = DL_COMPARE(DL(m1),DL(m2));
8197: switch ( c ) {
8198: case 0:
8199: addlf(CZ(m1),CZ(m2),&t);
8200: s = m1; m1 = NEXT(m1);
8201: if ( t ) {
8202: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
8203: } else {
8204: can += 2; FREENM(s);
8205: }
8206: s = m2; m2 = NEXT(m2); FREENM(s);
8207: break;
8208: case 1:
8209: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
8210: break;
8211: case -1:
8212: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
8213: break;
8214: }
8215: }
8216: if ( !mr0 )
8217: if ( m1 ) mr0 = m1;
8218: else if ( m2 ) mr0 = m2;
8219: else return 0;
8220: else if ( m1 ) NEXT(mr) = m1;
8221: else if ( m2 ) NEXT(mr) = m2;
8222: else NEXT(mr) = 0;
8223: BDY(p1) = mr0;
8224: SG(p1) = MAX(SG(p1),SG(p2));
8225: LEN(p1) = LEN(p1)+LEN(p2)-can;
8226: FREEND(p2);
8227: return p1;
8228: }
8229: }
8230:
8231: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8232: {
8233: int i,j,k,len,pos,prev;
8234: mpz_t c,mc,c1;
8235: IndArray ivect;
8236: unsigned char *ivc;
8237: unsigned short *ivs;
8238: unsigned int *ivi;
8239: NDV redv;
8240: NMV mr;
8241: NODE rp;
8242: int maxrs;
8243:
8244: maxrs = 0;
8245: lf_lazy = 1;
8246: for ( i = 0; i < nred; i++ ) {
8247: ivect = imat[i];
8248: k = ivect->head;
8249: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
8250: if ( mpz_sgn(svect[k]) ) {
8251: maxrs = MAX(maxrs,rp0[i]->sugar);
8252: mpz_neg(svect[k],svect[k]);
8253: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
8254: len = LEN(redv); mr = BDY(redv);
8255: prev = k;
8256: switch ( ivect->width ) {
8257: case 1:
8258: ivc = ivect->index.c;
8259: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8260: pos = prev+ivc[j]; prev = pos;
8261: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8262: }
8263: break;
8264: case 2:
8265: ivs = ivect->index.s;
8266: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8267: pos = prev+ivs[j]; prev = pos;
8268: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8269: }
8270: break;
8271: case 4:
8272: ivi = ivect->index.i;
8273: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8274: pos = prev+ivi[j]; prev = pos;
8275: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8276: }
8277: break;
8278: }
8279: mpz_set_ui(svect[k],0);
8280: }
8281: }
8282: lf_lazy=0;
8283: for ( i = 0; i < col; i++ ) {
8284: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
8285: }
8286: return maxrs;
8287: }
8288:
8289: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
8290: {
8291: int i,j,k,l,rank,s;
8292: mpz_t a,a1,inv;
8293: mpz_t *t,*pivot,*pk;
8294: mpz_t **mat;
8295: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
8296: int size,size1;
8297:
8298: mpz_init(inv);
8299: mpz_init(a);
8300: mat = (mpz_t **)mat0;
8301: size = 0;
8302: for ( rank = 0, j = 0; j < col; j++ ) {
8303: for ( i = rank; i < row; i++ ) {
8304: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
8305: }
8306: for ( i = rank; i < row; i++ )
8307: if ( mpz_sgn(mat[i][j]) )
8308: break;
8309: if ( i == row ) {
8310: colstat[j] = 0;
8311: continue;
8312: } else
8313: colstat[j] = 1;
8314: if ( i != rank ) {
8315: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8316: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8317: }
8318: pivot = mat[rank];
8319: s = sugar[rank];
8320: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
8321: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8322: if ( mpz_sgn(*pk) ) {
8323: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
8324: }
8325: for ( i = rank+1; i < row; i++ ) {
8326: t = mat[i];
8327: if ( mpz_sgn(t[j]) ) {
8328: sugar[i] = MAX(sugar[i],s);
8329: mpz_neg(a,t[j]);
8330: red_by_vect_lf(t+j,pivot+j,a,col-j);
8331: }
8332: }
8333: rank++;
8334: }
8335: for ( j = col-1, l = rank-1; j >= 0; j-- )
8336: if ( colstat[j] ) {
8337: pivot = mat[l];
8338: s = sugar[l];
8339: for ( k = j; k < col; k++ )
8340: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
8341: for ( i = 0; i < l; i++ ) {
8342: t = mat[i];
8343: if ( mpz_sgn(t[j]) ) {
8344: sugar[i] = MAX(sugar[i],s);
8345: mpz_neg(a,t[j]);
8346: red_by_vect_lf(t+j,pivot+j,a,col-j);
8347: }
8348: }
8349: l--;
8350: }
8351: for ( j = 0, l = 0; l < rank; j++ )
8352: if ( colstat[j] ) {
8353: t = mat[l];
8354: for ( k = j; k < col; k++ ) {
8355: mpz_mod(t[k],t[k],BDY(current_mod_lf));
8356: }
8357: l++;
8358: }
8359: return rank;
8360: }
8361:
8362:
8363: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
8364: {
8365: int j,k,len;
8366: UINT *p;
8367: UINT c;
8368: NDV r;
8369: NMV mr0,mr;
8370:
8371: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8372: if ( !len ) return 0;
8373: else {
8374: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8375: #if 0
8376: ndv_alloc += nmv_adv*len;
8377: #endif
8378: mr = mr0;
8379: p = s0vect;
8380: for ( j = k = 0; j < col; j++, p += nd_wpd )
8381: if ( !rhead[j] ) {
8382: if ( (c = vect[k++]) != 0 ) {
8383: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8384: }
8385: }
8386: MKNDV(nd_nvar,mr0,len,r);
8387: return r;
8388: }
8389: }
8390:
1.40 noro 8391: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
8392: {
8393: int j,k,len;
8394: UINT *p;
8395: UINT c;
8396: NDV r;
8397: NMV mr0,mr;
8398:
8399: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8400: if ( !len ) return 0;
8401: else {
8402: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8403: mr = mr0;
8404: p = s0vect;
8405: for ( j = k = 0; j < col; j++, p += nd_wpd )
8406: if ( (c = vect[k++]) != 0 ) {
8407: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8408: }
8409: MKNDV(nd_nvar,mr0,len,r);
8410: return r;
8411: }
8412: }
8413:
1.1 noro 8414: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
8415: {
8416: int j,k,len;
8417: UINT *p;
8418: NDV r;
8419: NMV mr0,mr;
8420:
8421: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
8422: if ( !len ) return 0;
8423: else {
8424: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8425: mr = mr0;
8426: p = s0vect;
8427: for ( j = 0; j < col; j++, p += nd_wpd )
8428: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
8429: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
8430: }
8431: MKNDV(nd_nvar,mr0,len,r);
8432: return r;
8433: }
8434: }
8435:
8436: /* for preprocessed vector */
8437:
8438: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
8439: {
1.6 noro 8440: int j,k,len;
8441: UINT *p;
8442: Z c;
8443: NDV r;
8444: NMV mr0,mr;
1.1 noro 8445:
1.6 noro 8446: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8447: if ( !len ) return 0;
8448: else {
8449: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 8450: #if 0
1.6 noro 8451: ndv_alloc += nmv_adv*len;
1.1 noro 8452: #endif
1.6 noro 8453: mr = mr0;
8454: p = s0vect;
8455: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
8456: if ( !rhead[j] ) {
8457: if ( (c = vect[k++]) != 0 ) {
8458: if ( !INT(c) )
8459: error("vect_to_ndv_q : components must be integers");
8460: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
8461: }
8462: }
1.1 noro 8463: }
1.6 noro 8464: MKNDV(nd_nvar,mr0,len,r);
8465: return r;
8466: }
1.1 noro 8467: }
8468:
8469: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8470: {
8471: int j,k,len;
8472: UINT *p;
8473: mpz_t c;
8474: NDV r;
8475: NMV mr0,mr;
8476:
8477: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8478: if ( !len ) return 0;
8479: else {
8480: mr0 = (NMV)MALLOC(nmv_adv*len);
8481: #if 0
8482: ndv_alloc += nmv_adv*len;
8483: #endif
8484: mr = mr0;
8485: p = s0vect;
8486: for ( j = k = 0; j < col; j++, p += nd_wpd )
8487: if ( !rhead[j] ) {
8488: c[0] = vect[k++][0];
8489: if ( mpz_sgn(c) ) {
8490: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8491: }
8492: }
8493: MKNDV(nd_nvar,mr0,len,r);
8494: return r;
8495: }
8496: }
8497:
8498: /* for plain vector */
8499:
8500: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8501: {
8502: int j,k,len;
8503: UINT *p;
8504: Z c;
8505: NDV r;
8506: NMV mr0,mr;
8507:
8508: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8509: if ( !len ) return 0;
8510: else {
8511: mr0 = (NMV)MALLOC(nmv_adv*len);
8512: #if 0
8513: ndv_alloc += nmv_adv*len;
8514: #endif
8515: mr = mr0;
8516: p = s0vect;
8517: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8518: if ( (c = vect[k]) != 0 ) {
8519: if ( !INT(c) )
1.6 noro 8520: error("plain_vect_to_ndv_q : components must be integers");
8521: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8522: }
8523: MKNDV(nd_nvar,mr0,len,r);
8524: return r;
8525: }
8526: }
8527:
8528: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8529: {
8530: ND_pairs t;
8531: NODE sp0,sp;
8532: int stat;
8533: ND spol;
8534:
8535: for ( t = l; t; t = NEXT(t) ) {
8536: stat = nd_sp(m,trace,t,&spol);
8537: if ( !stat ) return 0;
8538: if ( spol ) {
8539: add_pbucket_symbolic(bucket,spol);
8540: }
8541: }
8542: return 1;
8543: }
8544:
8545: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8546: {
8547: NODE rp0,rp;
8548: NM mul,head,s0,s;
8549: int index,col,i,sugar;
8550: RHist h;
8551: UINT *s0v,*p;
8552: NM_ind_pair pair;
8553: ND red;
8554: NDV *ps;
8555:
8556: s0 = 0; rp0 = 0; col = 0;
8557: if ( nd_demand )
8558: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8559: else
8560: ps = trace?nd_ps_trace:nd_ps;
8561: while ( 1 ) {
8562: head = remove_head_pbucket_symbolic(bucket);
8563: if ( !head ) break;
8564: if ( !s0 ) s0 = head;
8565: else NEXT(s) = head;
8566: s = head;
8567: index = ndl_find_reducer(DL(head));
8568: if ( index >= 0 ) {
8569: h = nd_psh[index];
8570: NEWNM(mul);
8571: ndl_sub(DL(head),DL(h),DL(mul));
8572: if ( ndl_check_bound2(index,DL(mul)) )
8573: return 0;
8574: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8575: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8576: red = ndv_mul_nm_symbolic(mul,ps[index]);
8577: add_pbucket_symbolic(bucket,nd_remove_head(red));
8578: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8579: }
8580: col++;
8581: }
8582: if ( rp0 ) NEXT(rp) = 0;
8583: NEXT(s) = 0;
8584: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8585: for ( i = 0, p = s0v, s = s0; i < col;
8586: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8587: *s0vect = s0v;
8588: *r = rp0;
8589: return col;
8590: }
8591:
8592: void print_ndp(ND_pairs l)
8593: {
8594: ND_pairs t;
8595:
8596: for ( t = l; t; t = NEXT(t) )
8597: printf("[%d,%d] ",t->i1,t->i2);
8598: printf("\n");
8599: }
8600:
8601: NODE nd_f4(int m,int checkonly,int **indp)
8602: {
8603: int i,nh,stat,index,f4red;
8604: NODE r,g,tn0,tn,node;
8605: ND_pairs d,l,t,ll0,ll,lh;
8606: LIST l0,l1;
8607: ND spol,red;
8608: NDV nf,redv;
8609: NM s0,s;
8610: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8611: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8612: UINT c;
8613: UINT **spmat;
8614: UINT *s0vect,*svect,*p,*v;
8615: int *colstat;
8616: IndArray *imat;
8617: int *rhead;
8618: int spcol,sprow;
8619: int sugar,sugarh;
8620: PGeoBucket bucket;
8621: struct oEGT eg0,eg1,eg_f4;
8622: Z i1,i2,sugarq;
1.12 noro 8623:
8624: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8625: #if 0
8626: ndv_alloc = 0;
8627: #endif
1.11 noro 8628: Nf4_red=0;
1.1 noro 8629: g = 0; d = 0;
8630: for ( i = 0; i < nd_psn; i++ ) {
8631: d = update_pairs(d,g,i,0);
8632: g = update_base(g,i);
8633: }
8634: nzlist = 0;
8635: nzlist_t = nd_nzlist;
8636: f4red = 1;
8637: nd_last_nonzero = 0;
8638: while ( d ) {
8639: get_eg(&eg0);
8640: l = nd_minsugarp(d,&d);
8641: sugar = nd_sugarweight?l->sugar2:SG(l);
8642: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8643: if ( nzlist_t ) {
8644: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8645: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8646: tn = BDY((LIST)ARG1(node));
8647: if ( !tn ) {
8648: nzlist_t = NEXT(nzlist_t);
8649: continue;
8650: }
8651: /* tn = [[i1,i2],...] */
8652: lh = nd_ipairtospair(tn);
8653: }
8654: bucket = create_pbucket();
8655: stat = nd_sp_f4(m,0,l,bucket);
8656: if ( !stat ) {
8657: for ( t = l; NEXT(t); t = NEXT(t) );
8658: NEXT(t) = d; d = l;
8659: d = nd_reconstruct(0,d);
8660: continue;
8661: }
8662: if ( bucket->m < 0 ) continue;
8663: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8664: if ( !col ) {
8665: for ( t = l; NEXT(t); t = NEXT(t) );
8666: NEXT(t) = d; d = l;
8667: d = nd_reconstruct(0,d);
8668: continue;
8669: }
1.12 noro 8670: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8671: if ( DP_Print )
1.6 noro 8672: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8673: sugar,eg_f4.exectime);
1.1 noro 8674: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8675: if ( checkonly && nflist ) return 0;
8676: /* adding new bases */
8677: if ( nflist ) nd_last_nonzero = f4red;
8678: for ( r = nflist; r; r = NEXT(r) ) {
8679: nf = (NDV)BDY(r);
1.38 noro 8680: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8681: ndv_removecont(m,nf);
8682: if ( !m && nd_nalg ) {
8683: ND nf1;
8684:
8685: nf1 = ndvtond(m,nf);
8686: nd_monic(0,&nf1);
8687: nd_removecont(m,nf1);
8688: nf = ndtondv(m,nf1);
8689: }
1.24 noro 8690: nh = ndv_newps(m,nf,0);
1.1 noro 8691: d = update_pairs(d,g,nh,0);
8692: g = update_base(g,nh);
8693: }
8694: if ( DP_Print ) {
8695: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8696: }
8697: if ( nd_gentrace ) {
8698: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8699: NEXTNODE(tn0,tn);
1.6 noro 8700: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8701: node = mknode(2,i1,i2); MKLIST(l0,node);
8702: BDY(tn) = l0;
8703: }
8704: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8705: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8706: MKNODE(node,l1,nzlist); nzlist = node;
8707: }
8708: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8709: f4red++;
8710: if ( nd_f4red && f4red > nd_f4red ) break;
8711: if ( nd_rank0 && !nflist ) break;
8712: }
8713: if ( nd_gentrace ) {
8714: MKLIST(l0,reverse_node(nzlist));
8715: MKNODE(nd_alltracelist,l0,0);
8716: }
8717: #if 0
8718: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8719: #endif
1.12 noro 8720: if ( DP_Print ) {
8721: fprintf(asir_out,"number of red=%d,",Nf4_red);
8722: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8723: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8724: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8725: }
1.1 noro 8726: conv_ilist(nd_demand,0,g,indp);
8727: return g;
8728: }
8729:
8730: NODE nd_f4_trace(int m,int **indp)
8731: {
8732: int i,nh,stat,index;
8733: NODE r,g;
8734: ND_pairs d,l,l0,t;
8735: ND spol,red;
8736: NDV nf,redv,nfqv,nfv;
8737: NM s0,s;
8738: NODE rp0,srp0,nflist;
8739: int nsp,nred,col,rank,len,k,j,a;
8740: UINT c;
8741: UINT **spmat;
8742: UINT *s0vect,*svect,*p,*v;
8743: int *colstat;
8744: IndArray *imat;
8745: int *rhead;
8746: int spcol,sprow;
8747: int sugar;
8748: PGeoBucket bucket;
8749: struct oEGT eg0,eg1,eg_f4;
8750:
8751: g = 0; d = 0;
8752: for ( i = 0; i < nd_psn; i++ ) {
8753: d = update_pairs(d,g,i,0);
8754: g = update_base(g,i);
8755: }
8756: while ( d ) {
8757: get_eg(&eg0);
8758: l = nd_minsugarp(d,&d);
8759: sugar = SG(l);
8760: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8761: bucket = create_pbucket();
8762: stat = nd_sp_f4(m,0,l,bucket);
8763: if ( !stat ) {
8764: for ( t = l; NEXT(t); t = NEXT(t) );
8765: NEXT(t) = d; d = l;
8766: d = nd_reconstruct(1,d);
8767: continue;
8768: }
8769: if ( bucket->m < 0 ) continue;
8770: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8771: if ( !col ) {
8772: for ( t = l; NEXT(t); t = NEXT(t) );
8773: NEXT(t) = d; d = l;
8774: d = nd_reconstruct(1,d);
8775: continue;
8776: }
8777: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8778: if ( DP_Print )
1.3 noro 8779: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8780: sugar,eg_f4.exectime);
1.1 noro 8781: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8782: if ( !l0 ) continue;
8783: l = l0;
8784:
8785: /* over Q */
8786: bucket = create_pbucket();
8787: stat = nd_sp_f4(0,1,l,bucket);
8788: if ( !stat ) {
8789: for ( t = l; NEXT(t); t = NEXT(t) );
8790: NEXT(t) = d; d = l;
8791: d = nd_reconstruct(1,d);
8792: continue;
8793: }
8794: if ( bucket->m < 0 ) continue;
8795: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8796: if ( !col ) {
8797: for ( t = l; NEXT(t); t = NEXT(t) );
8798: NEXT(t) = d; d = l;
8799: d = nd_reconstruct(1,d);
8800: continue;
8801: }
8802: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8803: /* adding new bases */
8804: for ( r = nflist; r; r = NEXT(r) ) {
8805: nfqv = (NDV)BDY(r);
8806: ndv_removecont(0,nfqv);
1.6 noro 8807: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8808: if ( nd_nalg ) {
8809: ND nf1;
8810:
8811: nf1 = ndvtond(m,nfqv);
8812: nd_monic(0,&nf1);
8813: nd_removecont(0,nf1);
8814: nfqv = ndtondv(0,nf1); nd_free(nf1);
8815: }
8816: nfv = ndv_dup(0,nfqv);
8817: ndv_mod(m,nfv);
8818: ndv_removecont(m,nfv);
1.24 noro 8819: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8820: d = update_pairs(d,g,nh,0);
8821: g = update_base(g,nh);
8822: }
8823: }
8824: #if 0
8825: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8826: #endif
8827: conv_ilist(nd_demand,1,g,indp);
8828: return g;
8829: }
8830:
8831: int rref(matrix mat,int *sugar)
8832: {
8833: int row,col,i,j,k,l,s,wcol,wj;
8834: unsigned long bj;
8835: unsigned long **a;
8836: unsigned long *ai,*ak,*as,*t;
8837: int *pivot;
8838:
8839: row = mat->row;
8840: col = mat->col;
8841: a = mat->a;
8842: wcol = (col+BLEN-1)/BLEN;
8843: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8844: i = 0;
8845: for ( j = 0; j < col; j++ ) {
8846: wj = j/BLEN; bj = 1L<<(j%BLEN);
8847: for ( k = i; k < row; k++ )
8848: if ( a[k][wj] & bj ) break;
8849: if ( k == row ) continue;
8850: pivot[i] = j;
8851: if ( k != i ) {
8852: t = a[i]; a[i] = a[k]; a[k] = t;
8853: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8854: }
8855: ai = a[i];
8856: for ( k = i+1; k < row; k++ ) {
8857: ak = a[k];
8858: if ( ak[wj] & bj ) {
8859: for ( l = wj; l < wcol; l++ )
8860: ak[l] ^= ai[l];
8861: sugar[k] = MAX(sugar[k],sugar[i]);
8862: }
8863: }
8864: i++;
8865: }
8866: for ( k = i-1; k >= 0; k-- ) {
8867: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8868: ak = a[k];
8869: for ( s = 0; s < k; s++ ) {
8870: as = a[s];
8871: if ( as[wj] & bj ) {
8872: for ( l = wj; l < wcol; l++ )
8873: as[l] ^= ak[l];
8874: sugar[s] = MAX(sugar[s],sugar[k]);
8875: }
8876: }
8877: }
8878: return i;
8879: }
8880:
8881: void print_matrix(matrix mat)
8882: {
8883: int row,col,i,j;
8884: unsigned long *ai;
8885:
8886: row = mat->row;
8887: col = mat->col;
8888: printf("%d x %d\n",row,col);
8889: for ( i = 0; i < row; i++ ) {
8890: ai = mat->a[i];
8891: for ( j = 0; j < col; j++ ) {
8892: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8893: else putchar('0');
8894: }
8895: putchar('\n');
8896: }
8897: }
8898:
8899: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8900:
8901: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8902: {
8903: int row,col,wcol,wj,i,j;
8904: unsigned long bj;
8905: unsigned long *ai;
8906: unsigned long **a;
8907: int len;
8908: int *pos;
8909:
8910: row = mat->row;
8911: col = mat->col;
8912: wcol = (col+BLEN-1)/BLEN;
8913: pos = (int *)MALLOC(wcol*sizeof(int));
8914: bzero(pos,wcol*sizeof(int));
8915: for ( i = j = 0; i < wcol; i++ )
8916: if ( v[i] ) pos[j++] = i;;
8917: len = j;
8918: wj = rhead/BLEN;
8919: bj = 1L<<rhead%BLEN;
8920: a = mat->a;
8921: for ( i = 0; i < row; i++ ) {
8922: ai = a[i];
8923: if ( ai[wj]&bj ) {
8924: for ( j = 0; j < len; j++ )
8925: ai[pos[j]] ^= v[pos[j]];
8926: sugar[i] = MAX(sugar[i],rsugar);
8927: }
8928: }
8929: }
8930:
8931: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8932: {
8933: int nsp,nred,i,i0,k,rank,row;
8934: NODE r0,rp;
8935: ND_pairs sp;
8936: ND spol;
8937: NM_ind_pair rt;
8938: int *s0hash;
8939: UINT *s;
8940: int *pivot,*sugar,*head;
8941: matrix mat;
8942: NM m;
8943: NODE r;
8944: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8945: int rhead,rsugar,size;
8946: unsigned long *v;
8947:
8948: get_eg(&eg0);
8949: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8950: nred = length(rp0);
8951: mat = alloc_matrix(nsp,col);
8952: s0hash = (int *)MALLOC(col*sizeof(int));
8953: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8954: s0hash[i] = ndl_hash_value(s);
8955:
8956: sugar = (int *)MALLOC(nsp*sizeof(int));
8957: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8958: nd_sp(2,0,sp,&spol);
8959: if ( spol ) {
8960: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8961: sugar[i] = SG(spol);
8962: i++;
8963: }
8964: }
8965: mat->row = i;
8966: if ( DP_Print ) {
8967: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8968: }
8969: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8970: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8971: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8972: rt = (NM_ind_pair)BDY(rp);
8973: bzero(v,size);
8974: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8975: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8976: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8977: }
8978:
8979: get_eg(&eg1);
8980: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8981: rank = rref(mat,sugar);
8982:
8983: for ( i = 0, r0 = 0; i < rank; i++ ) {
8984: NEXTNODE(r0,r);
8985: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8986: SG((NDV)BDY(r)) = sugar[i];
8987: }
8988: if ( r0 ) NEXT(r) = 0;
8989: get_eg(&eg2);
8990: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8991: if ( DP_Print ) {
8992: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8993: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8994: fflush(asir_out);
8995: }
8996: return r0;
8997: }
8998:
8999:
9000: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
9001: {
9002: IndArray *imat;
1.11 noro 9003: int nsp,nred,i,start;
1.1 noro 9004: int *rhead;
9005: NODE r0,rp;
9006: ND_pairs sp;
9007: NM_ind_pair *rvect;
9008: UINT *s;
9009: int *s0hash;
1.11 noro 9010: struct oEGT eg0,eg1,eg_conv;
1.1 noro 9011:
9012: if ( m == 2 && nd_rref2 )
9013: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
9014:
9015: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
9016: nred = length(rp0);
9017: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
9018: rhead = (int *)MALLOC(col*sizeof(int));
9019: for ( i = 0; i < col; i++ ) rhead[i] = 0;
9020:
9021: /* construction of index arrays */
1.11 noro 9022: get_eg(&eg0);
1.1 noro 9023: if ( DP_Print ) {
1.11 noro 9024: fprintf(asir_out,"%dx%d,",nsp+nred,col);
9025: fflush(asir_out);
1.1 noro 9026: }
9027: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 9028: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 9029: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 9030: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 9031: rhead[imat[i]->head] = 1;
1.11 noro 9032: start = imat[i]->head;
9033: }
1.12 noro 9034: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 9035: if ( DP_Print ) {
9036: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
9037: fflush(asir_out);
1.1 noro 9038: }
9039: if ( m > 0 )
1.7 noro 9040: #if SIZEOF_LONG==8
1.1 noro 9041: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9042: #else
9043: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9044: #endif
9045: else if ( m == -1 )
9046: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9047: else if ( m == -2 )
9048: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9049: else
9050: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9051: return r0;
9052: }
9053:
9054: /* for Fp, 2<=p<2^16 */
9055:
9056: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9057: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9058: {
9059: int spcol,sprow,a;
9060: int i,j,k,l,rank;
9061: NODE r0,r;
9062: ND_pairs sp;
9063: ND spol;
9064: UINT **spmat;
9065: UINT *svect,*v;
9066: int *colstat;
9067: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9068: int maxrs;
9069: int *spsugar;
9070: ND_pairs *spactive;
9071:
9072: spcol = col-nred;
9073: get_eg(&eg0);
9074: /* elimination (1st step) */
9075: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9076: svect = (UINT *)MALLOC(col*sizeof(UINT));
9077: spsugar = (int *)MALLOC(nsp*sizeof(int));
9078: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9079: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9080: nd_sp(m,0,sp,&spol);
9081: if ( !spol ) continue;
9082: nd_to_vect(m,s0vect,col,spol,svect);
9083: if ( m == -1 )
9084: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9085: else
1.40 noro 9086: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 9087: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9088: if ( i < col ) {
9089: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9090: for ( j = k = 0; j < col; j++ )
9091: if ( !rhead[j] ) v[k++] = svect[j];
9092: spsugar[sprow] = MAX(maxrs,SG(spol));
9093: if ( nz )
9094: spactive[sprow] = sp;
9095: sprow++;
9096: }
9097: nd_free(spol);
9098: }
9099: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9100: if ( DP_Print ) {
1.5 noro 9101: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9102: fflush(asir_out);
9103: }
9104: /* free index arrays */
9105: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9106:
9107: /* elimination (2nd step) */
9108: colstat = (int *)MALLOC(spcol*sizeof(int));
9109: if ( m == -1 )
9110: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9111: else
9112: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9113: r0 = 0;
9114: for ( i = 0; i < rank; i++ ) {
9115: NEXTNODE(r0,r); BDY(r) =
9116: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9117: SG((NDV)BDY(r)) = spsugar[i];
9118: GCFREE(spmat[i]);
9119: }
9120: if ( r0 ) NEXT(r) = 0;
9121:
9122: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9123: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9124: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9125: if ( DP_Print ) {
1.5 noro 9126: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9127: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9128: nsp,nred,sprow,spcol,rank);
1.5 noro 9129: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9130: }
9131: if ( nz ) {
9132: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9133: if ( rank > 0 ) {
9134: NEXT(spactive[rank-1]) = 0;
9135: *nz = spactive[0];
9136: } else
9137: *nz = 0;
9138: }
9139: return r0;
9140: }
9141:
1.40 noro 9142: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9143: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
9144: {
9145: int spcol,sprow,a;
9146: int i,j,k,l,rank;
9147: NODE r0,r;
9148: ND_pairs sp;
9149: ND spol;
9150: UINT **spmat;
9151: UINT *svect,*cvect;
9152: UINT *v;
9153: int *colstat;
9154: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9155: int maxrs;
9156: int *spsugar;
9157: ND_pairs *spactive;
9158: SIG *spsig;
9159:
9160: get_eg(&eg0);
9161: /* elimination (1st step) */
9162: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9163: spsugar = (int *)MALLOC(nsp*sizeof(int));
9164: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
9165: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9166: nd_sp(m,0,sp,&spol);
9167: if ( !spol ) {
9168: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9169: continue;
9170: }
9171: svect = (UINT *)MALLOC(col*sizeof(UINT));
9172: nd_to_vect(m,s0vect,col,spol,svect);
9173: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
9174: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9175: if ( i < col ) {
9176: spmat[sprow] = svect;
9177: spsugar[sprow] = MAX(maxrs,SG(spol));
9178: spsig[sprow] = sp->sig;
9179: sprow++;
9180: } else {
9181: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9182: }
9183: nd_free(spol);
9184: }
9185: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
9186: if ( DP_Print ) {
9187: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9188: fflush(asir_out);
9189: }
9190: /* free index arrays */
9191: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9192:
9193: /* elimination (2nd step) */
9194: colstat = (int *)MALLOC(col*sizeof(int));
9195: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
9196: r0 = 0;
9197: for ( i = 0; i < sprow; i++ ) {
9198: if ( spsugar[i] >= 0 ) {
9199: NEXTNODE(r0,r);
9200: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
9201: SG((NDV)BDY(r)) = spsugar[i];
9202: ((NDV)BDY(r))->sig = spsig[i];
9203: } else
9204: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
9205: GCFREE(spmat[i]);
9206: }
9207: if ( r0 ) NEXT(r) = 0;
9208: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
9209: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9210: if ( DP_Print ) {
9211: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9212: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9213: nsp,nred,sprow,col,rank);
9214: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9215: }
9216: return r0;
9217: }
9218:
1.1 noro 9219:
9220: /* for small finite fields */
9221:
9222: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9223: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9224: {
9225: int spcol,sprow,a;
9226: int i,j,k,l,rank;
9227: NODE r0,r;
9228: ND_pairs sp;
9229: ND spol;
9230: UINT **spmat;
9231: UINT *svect,*v;
9232: int *colstat;
9233: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9234: int maxrs;
9235: int *spsugar;
9236: ND_pairs *spactive;
9237:
9238: spcol = col-nred;
9239: get_eg(&eg0);
9240: /* elimination (1st step) */
9241: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9242: svect = (UINT *)MALLOC(col*sizeof(UINT));
9243: spsugar = (int *)MALLOC(nsp*sizeof(int));
9244: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9245: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9246: nd_sp(m,0,sp,&spol);
9247: if ( !spol ) continue;
9248: nd_to_vect(m,s0vect,col,spol,svect);
9249: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9250: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9251: if ( i < col ) {
9252: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9253: for ( j = k = 0; j < col; j++ )
9254: if ( !rhead[j] ) v[k++] = svect[j];
9255: spsugar[sprow] = MAX(maxrs,SG(spol));
9256: if ( nz )
9257: spactive[sprow] = sp;
9258: sprow++;
9259: }
9260: nd_free(spol);
9261: }
9262: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9263: if ( DP_Print ) {
1.5 noro 9264: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9265: fflush(asir_out);
9266: }
9267: /* free index arrays */
9268: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9269:
9270: /* elimination (2nd step) */
9271: colstat = (int *)MALLOC(spcol*sizeof(int));
9272: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9273: r0 = 0;
9274: for ( i = 0; i < rank; i++ ) {
9275: NEXTNODE(r0,r); BDY(r) =
9276: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9277: SG((NDV)BDY(r)) = spsugar[i];
9278: GCFREE(spmat[i]);
9279: }
9280: if ( r0 ) NEXT(r) = 0;
9281:
9282: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9283: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9284: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9285: if ( DP_Print ) {
1.5 noro 9286: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9287: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9288: nsp,nred,sprow,spcol,rank);
1.5 noro 9289: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9290: }
9291: if ( nz ) {
9292: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9293: if ( rank > 0 ) {
9294: NEXT(spactive[rank-1]) = 0;
9295: *nz = spactive[0];
9296: } else
9297: *nz = 0;
9298: }
9299: return r0;
9300: }
9301:
9302: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9303: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9304: {
9305: int spcol,sprow,a;
9306: int i,j,k,l,rank;
9307: NODE r0,r;
9308: ND_pairs sp;
9309: ND spol;
9310: mpz_t **spmat;
9311: mpz_t *svect,*v;
9312: int *colstat;
9313: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9314: int maxrs;
9315: int *spsugar;
9316: pointer *w;
9317:
9318: spcol = col-nred;
9319: get_eg(&eg0);
9320: /* elimination (1st step) */
9321: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
9322: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
9323: spsugar = (int *)MALLOC(nsp*sizeof(int));
9324: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9325: nd_sp(m,trace,sp,&spol);
9326: if ( !spol ) continue;
9327: nd_to_vect_lf(s0vect,col,spol,svect);
9328: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
9329: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
9330: if ( i < col ) {
9331: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
9332: for ( j = k = 0; j < col; j++ )
9333: if ( !rhead[j] ) v[k++][0] = svect[j][0];
9334: spsugar[sprow] = MAX(maxrs,SG(spol));
9335: sprow++;
9336: }
9337: /* nd_free(spol); */
9338: }
9339: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9340: if ( DP_Print ) {
1.5 noro 9341: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9342: fflush(asir_out);
9343: }
9344: /* free index arrays */
9345: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9346:
9347: /* elimination (2nd step) */
9348: colstat = (int *)MALLOC(spcol*sizeof(int));
9349: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
9350: w = (pointer *)MALLOC(rank*sizeof(pointer));
9351: for ( i = 0; i < rank; i++ ) {
9352: #if 0
9353: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9354: SG((NDV)w[rank-i-1]) = spsugar[i];
9355: #else
9356: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9357: SG((NDV)w[i]) = spsugar[i];
9358: #endif
9359: /* GCFREE(spmat[i]); */
9360:
9361: }
9362: #if 0
9363: qsort(w,rank,sizeof(NDV),
9364: (int (*)(const void *,const void *))ndv_compare);
9365: #endif
9366: r0 = 0;
9367: for ( i = 0; i < rank; i++ ) {
9368: NEXTNODE(r0,r); BDY(r) = w[i];
9369: }
9370: if ( r0 ) NEXT(r) = 0;
9371:
9372: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9373: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9374: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9375: if ( DP_Print ) {
1.5 noro 9376: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9377: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9378: nsp,nred,sprow,spcol,rank);
1.5 noro 9379: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9380: }
9381: return r0;
9382: }
9383:
9384: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9385: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9386: {
9387: int spcol,sprow,a;
9388: int i,j,k,l,rank;
9389: NODE r0,r;
9390: ND_pairs sp;
9391: ND spol;
9392: Z **spmat;
9393: Z *svect,*v;
9394: int *colstat;
9395: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9396: int maxrs;
9397: int *spsugar;
9398: pointer *w;
9399:
9400: spcol = col-nred;
9401: get_eg(&eg0);
9402: /* elimination (1st step) */
9403: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
9404: svect = (Z *)MALLOC(col*sizeof(Q));
9405: spsugar = (int *)MALLOC(nsp*sizeof(int));
9406: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9407: nd_sp(0,trace,sp,&spol);
9408: if ( !spol ) continue;
9409: nd_to_vect_q(s0vect,col,spol,svect);
9410: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
9411: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9412: if ( i < col ) {
9413: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
9414: for ( j = k = 0; j < col; j++ )
9415: if ( !rhead[j] ) v[k++] = svect[j];
9416: spsugar[sprow] = MAX(maxrs,SG(spol));
9417: sprow++;
9418: }
9419: /* nd_free(spol); */
9420: }
9421: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9422: if ( DP_Print ) {
1.5 noro 9423: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9424: fflush(asir_out);
9425: }
9426: /* free index arrays */
9427: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9428:
9429: /* elimination (2nd step) */
9430: colstat = (int *)MALLOC(spcol*sizeof(int));
9431: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
9432: w = (pointer *)MALLOC(rank*sizeof(pointer));
9433: for ( i = 0; i < rank; i++ ) {
9434: #if 0
9435: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9436: SG((NDV)w[rank-i-1]) = spsugar[i];
9437: #else
9438: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9439: SG((NDV)w[i]) = spsugar[i];
9440: #endif
9441: /* GCFREE(spmat[i]); */
9442: }
9443: #if 0
9444: qsort(w,rank,sizeof(NDV),
9445: (int (*)(const void *,const void *))ndv_compare);
9446: #endif
9447: r0 = 0;
9448: for ( i = 0; i < rank; i++ ) {
9449: NEXTNODE(r0,r); BDY(r) = w[i];
9450: }
9451: if ( r0 ) NEXT(r) = 0;
9452:
9453: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9454: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9455: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9456: if ( DP_Print ) {
1.5 noro 9457: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9458: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9459: nsp,nred,sprow,spcol,rank);
1.5 noro 9460: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9461: }
9462: return r0;
9463: }
9464:
9465: FILE *nd_write,*nd_read;
9466:
9467: void nd_send_int(int a) {
9468: write_int(nd_write,(unsigned int *)&a);
9469: }
9470:
9471: void nd_send_intarray(int *p,int len) {
9472: write_intarray(nd_write,(unsigned int *)p,len);
9473: }
9474:
9475: int nd_recv_int() {
9476: int a;
9477:
9478: read_int(nd_read,(unsigned int *)&a);
9479: return a;
9480: }
9481:
9482: void nd_recv_intarray(int *p,int len) {
9483: read_intarray(nd_read,(unsigned int *)p,len);
9484: }
9485:
9486: void nd_send_ndv(NDV p) {
9487: int len,i;
9488: NMV m;
9489:
9490: if ( !p ) nd_send_int(0);
9491: else {
9492: len = LEN(p);
9493: nd_send_int(len);
9494: m = BDY(p);
9495: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9496: nd_send_int(CM(m));
9497: nd_send_intarray((int *)DL(m),nd_wpd);
9498: }
9499: }
9500: }
9501:
9502: void nd_send_nd(ND p) {
9503: int len,i;
9504: NM m;
9505:
9506: if ( !p ) nd_send_int(0);
9507: else {
9508: len = LEN(p);
9509: nd_send_int(len);
9510: m = BDY(p);
9511: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9512: nd_send_int(CM(m));
9513: nd_send_intarray((int *)DL(m),nd_wpd);
9514: }
9515: }
9516: }
9517:
9518: NDV nd_recv_ndv()
9519: {
9520: int len,i;
9521: NMV m,m0;
9522: NDV r;
9523:
9524: len = nd_recv_int();
9525: if ( !len ) return 0;
9526: else {
9527: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9528: #if 0
9529: ndv_alloc += len*nmv_adv;
9530: #endif
9531: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9532: CM(m) = nd_recv_int();
9533: nd_recv_intarray((int *)DL(m),nd_wpd);
9534: }
9535: MKNDV(nd_nvar,m0,len,r);
9536: return r;
9537: }
9538: }
9539:
9540: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9541: {
9542: int i,j,t,c,rank,inv;
9543: int *ci,*ri;
9544: Z dn;
9545: MAT m,nm;
9546:
9547: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9548: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9549: for ( i = 0; i < row; i++ )
9550: for ( j = 0; j < col; j++ )
9551: mat0[i][j] = 0;
9552: c = col-rank;
9553: for ( i = 0; i < rank; i++ ) {
9554: mat0[i][ri[i]] = dn;
9555: for ( j = 0; j < c; j++ )
9556: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9557: }
9558: return rank;
9559: }
9560:
9561: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9562: {
9563: int i,j,k,l,inv,a,rank,s;
9564: unsigned int *t,*pivot,*pk;
9565: unsigned int **mat;
9566: ND_pairs pair;
9567:
9568: mat = (unsigned int **)mat0;
9569: for ( rank = 0, j = 0; j < col; j++ ) {
9570: for ( i = rank; i < row; i++ )
9571: mat[i][j] %= md;
9572: for ( i = rank; i < row; i++ )
9573: if ( mat[i][j] )
9574: break;
9575: if ( i == row ) {
9576: colstat[j] = 0;
9577: continue;
9578: } else
9579: colstat[j] = 1;
9580: if ( i != rank ) {
9581: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9582: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9583: if ( spactive ) {
9584: pair = spactive[i]; spactive[i] = spactive[rank];
9585: spactive[rank] = pair;
9586: }
9587: }
9588: pivot = mat[rank];
9589: s = sugar[rank];
9590: inv = invm(pivot[j],md);
9591: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9592: if ( *pk ) {
9593: if ( *pk >= (unsigned int)md )
9594: *pk %= md;
9595: DMAR(*pk,inv,0,md,*pk)
9596: }
9597: for ( i = rank+1; i < row; i++ ) {
9598: t = mat[i];
9599: if ( (a = t[j]) != 0 ) {
9600: sugar[i] = MAX(sugar[i],s);
9601: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9602: }
9603: }
9604: rank++;
9605: }
9606: for ( j = col-1, l = rank-1; j >= 0; j-- )
9607: if ( colstat[j] ) {
9608: pivot = mat[l];
9609: s = sugar[l];
9610: for ( i = 0; i < l; i++ ) {
9611: t = mat[i];
9612: t[j] %= md;
9613: if ( (a = t[j]) != 0 ) {
9614: sugar[i] = MAX(sugar[i],s);
9615: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9616: }
9617: }
9618: l--;
9619: }
9620: for ( j = 0, l = 0; l < rank; j++ )
9621: if ( colstat[j] ) {
9622: t = mat[l];
9623: for ( k = j; k < col; k++ )
9624: if ( t[k] >= (unsigned int)md )
9625: t[k] %= md;
9626: l++;
9627: }
9628: return rank;
9629: }
9630:
1.40 noro 9631: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9632: {
9633: int i,j,k,l,rank,s,imin;
9634: UINT inv;
9635: UINT a;
9636: UINT *t,*pivot,*pk;
9637: UINT *ck;
9638: UINT *ct;
9639: ND_pairs pair;
9640: SIG sg;
9641: int *used;
9642:
9643: used = (int *)MALLOC(row*sizeof(int));
9644: for ( j = 0; j < col; j++ ) {
9645: for ( i = 0; i < row; i++ )
9646: a = mat[i][j] %= md;
9647: for ( i = 0; i < row; i++ )
9648: if ( !used[i] && mat[i][j] ) break;
9649: if ( i == row ) {
9650: colstat[j] = 0;
9651: continue;
9652: } else {
9653: colstat[j] = 1;
9654: used[i] = 1;
9655: }
9656: /* column j is normalized */
9657: s = sugar[i];
9658: inv = invm(mat[i][j],md);
9659: /* normalize pivot row */
9660: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9661: DMAR(*pk,inv,0,md,*pk);
9662: }
9663: for ( k = i+1; k < row; k++ ) {
9664: if ( (a = mat[k][j]) != 0 ) {
9665: sugar[k] = MAX(sugar[k],s);
9666: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9667: Nf4_red++;
9668: }
9669: }
9670: }
9671: rank = 0;
9672: for ( i = 0; i < row; i++ ) {
9673: for ( j = 0; j < col; j++ )
9674: if ( mat[i][j] ) break;
9675: if ( j == col ) sugar[i] = -1;
9676: else rank++;
9677: }
9678: return rank;
9679: }
9680:
1.1 noro 9681:
1.7 noro 9682: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9683: {
1.7 noro 9684: int i,j,k,l,inv,a,rank,s;
9685: unsigned int *t,*pivot,*pk;
9686: unsigned int **mat;
9687:
9688: mat = (unsigned int **)mat0;
9689: for ( rank = 0, j = 0; j < col; j++ ) {
9690: for ( i = rank; i < row; i++ )
9691: if ( mat[i][j] )
9692: break;
9693: if ( i == row ) {
9694: colstat[j] = 0;
9695: continue;
9696: } else
9697: colstat[j] = 1;
9698: if ( i != rank ) {
9699: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9700: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9701: }
9702: pivot = mat[rank];
9703: s = sugar[rank];
9704: inv = _invsf(pivot[j]);
9705: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9706: if ( *pk )
9707: *pk = _mulsf(*pk,inv);
9708: for ( i = rank+1; i < row; i++ ) {
9709: t = mat[i];
9710: if ( (a = t[j]) != 0 ) {
9711: sugar[i] = MAX(sugar[i],s);
9712: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9713: }
9714: }
9715: rank++;
9716: }
9717: for ( j = col-1, l = rank-1; j >= 0; j-- )
9718: if ( colstat[j] ) {
9719: pivot = mat[l];
9720: s = sugar[l];
9721: for ( i = 0; i < l; i++ ) {
9722: t = mat[i];
9723: if ( (a = t[j]) != 0 ) {
9724: sugar[i] = MAX(sugar[i],s);
9725: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9726: }
9727: }
9728: l--;
9729: }
9730: return rank;
9731: }
1.1 noro 9732:
1.7 noro 9733: int ndv_ishomo(NDV p)
9734: {
9735: NMV m;
9736: int len,h;
1.1 noro 9737:
9738: if ( !p ) return 1;
9739: len = LEN(p);
9740: m = BDY(p);
9741: h = TD(DL(m));
9742: NMV_ADV(m);
9743: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9744: if ( TD(DL(m)) != h ) {
9745: return 0;
9746: }
1.1 noro 9747: return 1;
9748: }
9749:
9750: void ndv_save(NDV p,int index)
9751: {
9752: FILE *s;
9753: char name[BUFSIZ];
9754: short id;
9755: int nv,sugar,len,n,i,td,e,j;
9756: NMV m;
9757: unsigned int *dl;
9758: int mpos;
9759:
9760: sprintf(name,"%s/%d",Demand,index);
9761: s = fopen(name,"w");
9762: savevl(s,0);
9763: if ( !p ) {
9764: saveobj(s,0);
9765: return;
9766: }
9767: id = O_DP;
9768: nv = NV(p);
9769: sugar = SG(p);
9770: len = LEN(p);
9771: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9772: write_int(s,(unsigned int *)&len);
9773:
9774: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9775: saveobj(s,(Obj)CZ(m));
1.1 noro 9776: dl = DL(m);
9777: td = TD(dl);
9778: write_int(s,(unsigned int *)&td);
9779: for ( j = 0; j < nv; j++ ) {
9780: e = GET_EXP(dl,j);
9781: write_int(s,(unsigned int *)&e);
9782: }
9783: if ( nd_module ) {
9784: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
9785: }
9786: }
9787: fclose(s);
9788: }
9789:
9790: void nd_save_mod(ND p,int index)
9791: {
9792: FILE *s;
9793: char name[BUFSIZ];
9794: int nv,sugar,len,c;
9795: NM m;
9796:
9797: sprintf(name,"%s/%d",Demand,index);
9798: s = fopen(name,"w");
9799: if ( !p ) {
9800: len = 0;
9801: write_int(s,(unsigned int *)&len);
9802: fclose(s);
9803: return;
9804: }
9805: nv = NV(p);
9806: sugar = SG(p);
9807: len = LEN(p);
9808: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9809: for ( m = BDY(p); m; m = NEXT(m) ) {
9810: c = CM(m); write_int(s,(unsigned int *)&c);
9811: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9812: }
9813: fclose(s);
9814: }
9815:
9816: NDV ndv_load(int index)
9817: {
9818: FILE *s;
9819: char name[BUFSIZ];
9820: short id;
9821: int nv,sugar,len,n,i,td,e,j;
9822: NDV d;
9823: NMV m0,m;
9824: unsigned int *dl;
9825: Obj obj;
9826: int mpos;
9827:
9828: sprintf(name,"%s/%d",Demand,index);
9829: s = fopen(name,"r");
9830: if ( !s ) return 0;
9831:
9832: skipvl(s);
9833: read_short(s,(unsigned short *)&id);
9834: if ( !id ) return 0;
9835: read_int(s,(unsigned int *)&nv);
9836: read_int(s,(unsigned int *)&sugar);
9837: read_int(s,(unsigned int *)&len);
9838:
9839: m0 = m = MALLOC(len*nmv_adv);
9840: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9841: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9842: dl = DL(m);
9843: ndl_zero(dl);
9844: read_int(s,(unsigned int *)&td); TD(dl) = td;
9845: for ( j = 0; j < nv; j++ ) {
9846: read_int(s,(unsigned int *)&e);
9847: PUT_EXP(dl,j,e);
9848: }
9849: if ( nd_module ) {
9850: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9851: }
9852: if ( nd_blockmask ) ndl_weight_mask(dl);
9853: }
9854: fclose(s);
9855: MKNDV(nv,m0,len,d);
9856: SG(d) = sugar;
9857: return d;
9858: }
9859:
9860: ND nd_load_mod(int index)
9861: {
9862: FILE *s;
9863: char name[BUFSIZ];
9864: int nv,sugar,len,i,c;
9865: ND d;
9866: NM m0,m;
9867:
9868: sprintf(name,"%s/%d",Demand,index);
9869: s = fopen(name,"r");
9870: /* if the file does not exist, it means p[index]=0 */
9871: if ( !s ) return 0;
9872:
9873: read_int(s,(unsigned int *)&nv);
9874: if ( !nv ) { fclose(s); return 0; }
9875:
9876: read_int(s,(unsigned int *)&sugar);
9877: read_int(s,(unsigned int *)&len);
9878: for ( m0 = 0, i = 0; i < len; i++ ) {
9879: NEXTNM(m0,m);
9880: read_int(s,(unsigned int *)&c); CM(m) = c;
9881: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9882: }
9883: NEXT(m) = 0;
9884: MKND(nv,m0,len,d);
9885: SG(d) = sugar;
9886: fclose(s);
9887: return d;
9888: }
9889:
9890: void nd_det(int mod,MAT f,P *rp)
9891: {
9892: VL fv,tv;
9893: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9894: pointer **m;
9895: P **w;
9896: P mp,r;
9897: NDV **dm;
9898: NDV *t,*mi,*mj;
9899: NDV d,s,mij,mjj;
9900: ND u;
9901: NMV nmv;
9902: UINT *bound;
9903: PGeoBucket bucket;
9904: struct order_spec *ord;
9905: Z dq,dt,ds;
9906: Z mone;
9907: Z gn,qn,dn0,nm,dn;
9908:
9909: create_order_spec(0,0,&ord);
9910: nd_init_ord(ord);
9911: get_vars((Obj)f,&fv);
9912: if ( f->row != f->col )
9913: error("nd_det : non-square matrix");
9914: n = f->row;
9915: m = f->body;
9916: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9917:
9918: if ( !nvar ) {
9919: if ( !mod )
9920: detp(CO,(P **)m,n,rp);
9921: else {
9922: w = (P **)almat_pointer(n,n);
9923: for ( i = 0; i < n; i++ )
9924: for ( j = 0; j < n; j++ )
9925: ptomp(mod,(P)m[i][j],&w[i][j]);
9926: detmp(CO,mod,w,n,&mp);
9927: mptop(mp,rp);
9928: }
9929: return;
9930: }
9931:
9932: if ( !mod ) {
9933: w = (P **)almat_pointer(n,n);
9934: dq = ONE;
9935: for ( i = 0; i < n; i++ ) {
9936: dn0 = ONE;
9937: for ( j = 0; j < n; j++ ) {
9938: if ( !m[i][j] ) continue;
9939: lgp(m[i][j],&nm,&dn);
1.6 noro 9940: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9941: }
9942: if ( !UNIZ(dn0) ) {
9943: ds = dn0;
9944: for ( j = 0; j < n; j++ )
9945: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9946: mulz(dq,ds,&dt); dq = dt;
9947: } else
9948: for ( j = 0; j < n; j++ )
9949: w[i][j] = (P)m[i][j];
9950: }
9951: m = (pointer **)w;
9952: }
9953:
9954: for ( i = 0, max = 1; i < n; i++ )
9955: for ( j = 0; j < n; j++ )
9956: for ( tv = fv; tv; tv = NEXT(tv) ) {
9957: e = getdeg(tv->v,(P)m[i][j]);
9958: max = MAX(e,max);
9959: }
9960: nd_setup_parameters(nvar,max);
9961: dm = (NDV **)almat_pointer(n,n);
9962: for ( i = 0, max = 1; i < n; i++ )
9963: for ( j = 0; j < n; j++ ) {
9964: dm[i][j] = ptondv(CO,fv,m[i][j]);
9965: if ( mod ) ndv_mod(mod,dm[i][j]);
9966: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9967: }
9968: d = ptondv(CO,fv,(P)ONE);
9969: if ( mod ) ndv_mod(mod,d);
9970: chsgnz(ONE,&mone);
9971: for ( j = 0, sgn = 1; j < n; j++ ) {
9972: if ( DP_Print ) {
9973: fprintf(asir_out,".");
9974: }
9975: for ( i = j; i < n && !dm[i][j]; i++ );
9976: if ( i == n ) {
9977: *rp = 0;
9978: return;
9979: }
9980: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9981: for ( k = j; k < n; k++ )
9982: for ( l = j; l < n; l++ )
9983: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9984: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9985: }
9986: if ( k0 != j ) {
9987: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9988: sgn = -sgn;
9989: }
9990: if ( l0 != j ) {
9991: for ( k = j; k < n; k++ ) {
9992: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9993: }
9994: sgn = -sgn;
9995: }
9996: bound = nd_det_compute_bound(dm,n,j);
9997: for ( k = 0; k < nd_nvar; k++ )
9998: if ( bound[k]*2 > nd_mask0 ) break;
9999: if ( k < nd_nvar )
10000: nd_det_reconstruct(dm,n,j,d);
10001:
10002: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
10003: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
10004: mi = dm[i]; mij = mi[j];
10005: if ( mod )
10006: ndv_mul_c(mod,mij,mod-1);
10007: else
10008: ndv_mul_c_q(mij,mone);
10009: for ( k = j+1; k < n; k++ ) {
10010: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
10011: bucket = create_pbucket();
10012: if ( mi[k] ) {
10013: nmv = BDY(mjj); len = LEN(mjj);
10014: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10015: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
10016: add_pbucket(mod,bucket,u);
10017: }
10018: }
10019: if ( mj[k] && mij ) {
10020: nmv = BDY(mij); len = LEN(mij);
10021: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10022: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
10023: add_pbucket(mod,bucket,u);
10024: }
10025: }
10026: u = nd_quo(mod,bucket,d);
10027: mi[k] = ndtondv(mod,u);
10028: }
10029: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
10030: }
10031: d = mjj;
10032: }
10033: if ( DP_Print ) {
10034: fprintf(asir_out,"\n");
10035: }
10036: if ( sgn < 0 ) {
10037: if ( mod )
10038: ndv_mul_c(mod,d,mod-1);
10039: else
10040: ndv_mul_c_q(d,mone);
10041: }
10042: r = ndvtop(mod,CO,fv,d);
10043: if ( !mod && !UNIQ(dq) )
10044: divsp(CO,r,(P)dq,rp);
10045: else
10046: *rp = r;
10047: }
10048:
10049: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
10050: {
10051: NM mr,mr0;
10052: NM tnm;
10053: NMV m;
10054: UINT *d0,*dt,*dm;
10055: int c,n,td,i,c1,c2,len;
10056: Z q;
10057: ND r;
10058:
10059: if ( !p ) return 0;
10060: else {
10061: n = NV(p); m = BDY(p); len = LEN(p);
10062: d0 = DL(m0);
10063: td = TD(d);
10064: mr0 = 0;
10065: NEWNM(tnm);
10066: if ( mod ) {
10067: c = CM(m0);
10068: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10069: ndl_add(DL(m),d0,DL(tnm));
10070: if ( ndl_reducible(DL(tnm),d) ) {
10071: NEXTNM(mr0,mr);
10072: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
10073: ndl_copy(DL(tnm),DL(mr));
10074: }
10075: }
10076: } else {
1.6 noro 10077: q = CZ(m0);
1.1 noro 10078: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10079: ndl_add(DL(m),d0,DL(tnm));
10080: if ( ndl_reducible(DL(tnm),d) ) {
10081: NEXTNM(mr0,mr);
1.6 noro 10082: mulz(CZ(m),q,&CZ(mr));
1.1 noro 10083: ndl_copy(DL(tnm),DL(mr));
10084: }
10085: }
10086: }
10087: if ( !mr0 )
10088: return 0;
10089: else {
10090: NEXT(mr) = 0;
10091: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10092: MKND(NV(p),mr0,len,r);
10093: SG(r) = SG(p) + TD(d0);
10094: return r;
10095: }
10096: }
10097: }
10098:
10099: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
10100: {
10101: int i,obpe,oadv,h,k,l;
10102: static NM prev_nm_free_list;
10103: EPOS oepos;
10104:
10105: obpe = nd_bpe;
10106: oadv = nmv_adv;
10107: oepos = nd_epos;
10108: if ( obpe < 2 ) nd_bpe = 2;
10109: else if ( obpe < 3 ) nd_bpe = 3;
10110: else if ( obpe < 4 ) nd_bpe = 4;
10111: else if ( obpe < 5 ) nd_bpe = 5;
10112: else if ( obpe < 6 ) nd_bpe = 6;
10113: else if ( obpe < 8 ) nd_bpe = 8;
10114: else if ( obpe < 10 ) nd_bpe = 10;
10115: else if ( obpe < 16 ) nd_bpe = 16;
10116: else if ( obpe < 32 ) nd_bpe = 32;
10117: else error("nd_det_reconstruct : exponent too large");
10118:
10119: nd_setup_parameters(nd_nvar,0);
10120: prev_nm_free_list = _nm_free_list;
10121: _nm_free_list = 0;
10122: for ( k = j; k < n; k++ )
10123: for (l = j; l < n; l++ )
10124: ndv_realloc(dm[k][l],obpe,oadv,oepos);
10125: ndv_realloc(d,obpe,oadv,oepos);
10126: prev_nm_free_list = 0;
10127: #if 0
10128: GC_gcollect();
10129: #endif
10130: }
10131:
10132: /* returns a UINT array containing degree bounds */
10133:
10134: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
10135: {
10136: UINT *d0,*d1,*d,*t,*r;
10137: int k,l,i;
10138:
10139: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
10140: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
10141: for ( k = j; k < n; k++ )
10142: for ( l = j; l < n; l++ )
10143: if ( dm[k][l] ) {
10144: d = ndv_compute_bound(dm[k][l]);
10145: for ( i = 0; i < nd_nvar; i++ )
10146: d0[i] = MAX(d0[i],d[i]);
10147: }
10148: return d0;
10149: }
10150:
10151: DL nd_separate_d(UINT *d,UINT *trans)
10152: {
10153: int n,td,i,e,j;
10154: DL a;
10155:
10156: ndl_zero(trans);
10157: td = 0;
10158: for ( i = 0; i < nd_ntrans; i++ ) {
10159: e = GET_EXP(d,i);
10160: PUT_EXP(trans,i,e);
10161: td += MUL_WEIGHT(e,i);
10162: }
10163: if ( nd_ntrans+nd_nalg < nd_nvar ) {
10164: /* homogenized */
10165: i = nd_nvar-1;
10166: e = GET_EXP(d,i);
10167: PUT_EXP(trans,i,e);
10168: td += MUL_WEIGHT(e,i);
10169: }
10170: TD(trans) = td;
10171: if ( nd_blockmask) ndl_weight_mask(trans);
10172: NEWDL(a,nd_nalg);
10173: td = 0;
10174: for ( i = 0; i < nd_nalg; i++ ) {
10175: j = nd_ntrans+i;
10176: e = GET_EXP(d,j);
10177: a->d[i] = e;
10178: td += e;
10179: }
10180: a->td = td;
10181: return a;
10182: }
10183:
10184: int nd_monic(int mod,ND *p)
10185: {
10186: UINT *trans,*t;
10187: DL alg;
10188: MP mp0,mp;
10189: NM m,m0,m1,ma0,ma,mb,mr0,mr;
10190: ND r;
10191: DL dl;
10192: DP nm;
10193: NDV ndv;
10194: DAlg inv,cd;
10195: ND s,c;
10196: Z l,mul;
10197: Z ln;
10198: int n,ntrans,i,e,td,is_lc,len;
10199: NumberField nf;
10200: struct oEGT eg0,eg1;
10201:
10202: if ( !(nf = get_numberfield()) )
10203: error("nd_monic : current_numberfield is not set");
10204:
10205: /* Q coef -> DAlg coef */
10206: NEWNM(ma0); ma = ma0;
10207: m = BDY(*p);
10208: is_lc = 1;
10209: while ( 1 ) {
10210: NEWMP(mp0); mp = mp0;
1.6 noro 10211: mp->c = (Obj)CZ(m);
1.1 noro 10212: mp->dl = nd_separate_d(DL(m),DL(ma));
10213: NEWNM(mb);
10214: for ( m = NEXT(m); m; m = NEXT(m) ) {
10215: alg = nd_separate_d(DL(m),DL(mb));
10216: if ( !ndl_equal(DL(ma),DL(mb)) )
10217: break;
1.6 noro 10218: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 10219: }
10220: NEXT(mp) = 0;
10221: MKDP(nd_nalg,mp0,nm);
10222: MKDAlg(nm,ONE,cd);
10223: if ( is_lc == 1 ) {
10224: /* if the lc is a rational number, we have nothing to do */
10225: if ( !mp0->dl->td )
10226: return 1;
10227:
10228: get_eg(&eg0);
10229: invdalg(cd,&inv);
10230: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
10231: /* check the validity of inv */
10232: if ( mod && !remqi((Q)inv->dn,mod) )
10233: return 0;
10234: CA(ma) = nf->one;
10235: is_lc = 0;
10236: ln = ONE;
10237: } else {
10238: muldalg(cd,inv,&CA(ma));
10239: lcmz(ln,CA(ma)->dn,&ln);
10240: }
10241: if ( m ) {
10242: NEXT(ma) = mb; ma = mb;
10243: } else {
10244: NEXT(ma) = 0;
10245: break;
10246: }
10247: }
10248: /* l = lcm(denoms) */
10249: l = ln;
10250: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 10251: divsz(l,CA(m)->dn,&mul);
1.1 noro 10252: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
10253: NEXTNM(mr0,mr);
1.6 noro 10254: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 10255: dl = mp->dl;
10256: td = TD(DL(m));
10257: ndl_copy(DL(m),DL(mr));
10258: for ( i = 0; i < nd_nalg; i++ ) {
10259: e = dl->d[i];
10260: PUT_EXP(DL(mr),i+nd_ntrans,e);
10261: td += MUL_WEIGHT(e,i+nd_ntrans);
10262: }
10263: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
10264: TD(DL(mr)) = td;
10265: if ( nd_blockmask) ndl_weight_mask(DL(mr));
10266: }
10267: }
10268: NEXT(mr) = 0;
10269: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10270: MKND(NV(*p),mr0,len,r);
10271: /* XXX */
10272: SG(r) = SG(*p);
10273: nd_free(*p);
10274: *p = r;
10275: return 1;
10276: }
10277:
10278: NODE reverse_node(NODE n)
10279: {
10280: NODE t,t1;
10281:
10282: for ( t = 0; n; n = NEXT(n) ) {
10283: MKNODE(t1,BDY(n),t); t = t1;
10284: }
10285: return t;
10286: }
10287:
10288: P ndc_div(int mod,union oNDC a,union oNDC b)
10289: {
10290: union oNDC c;
10291: int inv,t;
10292:
10293: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 10294: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 10295: else if ( mod ) {
10296: inv = invm(b.m,mod);
10297: DMAR(a.m,inv,0,mod,t); c.m = t;
10298: } else if ( nd_vc )
10299: divsp(nd_vc,a.p,b.p,&c.p);
10300: else
10301: divsz(a.z,b.z,&c.z);
10302: return ndctop(mod,c);
10303: }
10304:
10305: P ndctop(int mod,union oNDC c)
10306: {
10307: Z q;
10308: int e;
10309: GFS gfs;
10310:
10311: if ( mod == -1 ) {
10312: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
10313: } else if ( mod == -2 ) {
1.10 noro 10314: q = c.z; return (P)q;
1.1 noro 10315: } else if ( mod > 0 ) {
1.6 noro 10316: STOZ(c.m,q); return (P)q;
1.1 noro 10317: } else
10318: return (P)c.p;
10319: }
10320:
10321: /* [0,0,0,cont] = p -> p/cont */
10322:
10323: void finalize_tracelist(int i,P cont)
10324: {
10325: LIST l;
10326: NODE node;
10327: Z iq;
10328:
10329: if ( !UNIQ(cont) ) {
10330: node = mknode(4,NULLP,NULLP,NULLP,cont);
10331: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
10332: nd_tracelist = node;
10333: }
1.6 noro 10334: STOZ(i,iq);
1.1 noro 10335: nd_tracelist = reverse_node(nd_tracelist);
10336: MKLIST(l,nd_tracelist);
10337: node = mknode(2,iq,l); MKLIST(l,node);
10338: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
10339: nd_alltracelist = node; nd_tracelist = 0;
10340: }
10341:
10342: void conv_ilist(int demand,int trace,NODE g,int **indp)
10343: {
10344: int n,i,j;
10345: int *ind;
10346: NODE t;
10347:
10348: n = length(g);
10349: ind = (int *)MALLOC(n*sizeof(int));
10350: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
10351: j = (long)BDY(t); ind[i] = j;
10352: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
10353: }
10354: if ( indp ) *indp = ind;
10355: }
10356:
1.30 noro 10357: NODE conv_ilist_s(int demand,int trace,int **indp)
10358: {
10359: int n,i,j;
10360: int *ind;
10361: NODE g0,g;
10362:
10363: n = nd_psn;
10364: ind = (int *)MALLOC(n*sizeof(int));
10365: g0 = 0;
10366: for ( i = 0; i < n; i++ ) {
10367: ind[i] = i;
10368: NEXTNODE(g0,g);
10369: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
10370: }
10371: if ( g0 ) NEXT(g) = 0;
10372: if ( indp ) *indp = ind;
10373: return g0;
10374: }
10375:
1.46 noro 10376: void parse_nd_option(VL vl,NODE opt)
1.1 noro 10377: {
1.38 noro 10378: NODE t,p,u;
1.1 noro 10379: int i,s,n;
1.38 noro 10380: char *key;
10381: Obj value;
1.46 noro 10382: VL oldvl;
1.1 noro 10383:
1.41 noro 10384: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 10385: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
10386: nd_splist = 0; nd_check_splist = 0;
1.38 noro 10387: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
10388: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 10389: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46 noro 10390: nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.39 noro 10391:
1.38 noro 10392: for ( t = opt; t; t = NEXT(t) ) {
10393: p = BDY((LIST)BDY(t));
10394: key = BDY((STRING)BDY(p));
10395: value = (Obj)BDY(NEXT(p));
10396: if ( !strcmp(key,"gentrace") )
10397: nd_gentrace = value?1:0;
10398: else if ( !strcmp(key,"gensyz") )
10399: nd_gensyz = value?1:0;
10400: else if ( !strcmp(key,"nora") )
10401: nd_nora = value?1:0;
1.41 noro 10402: else if ( !strcmp(key,"norb") )
10403: nd_norb = value?1:0;
1.38 noro 10404: else if ( !strcmp(key,"gbblock") ) {
10405: if ( value && OID(value) == O_LIST ) {
1.1 noro 10406: u = BDY((LIST)value);
1.38 noro 10407: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 10408: for ( i = 0; u; u = NEXT(u) ) {
10409: p = BDY((LIST)BDY(u));
1.6 noro 10410: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
10411: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 10412: }
10413: nd_gbblock[i] = -1;
1.38 noro 10414: } else
10415: nd_gbblock = 0;
1.1 noro 10416: } else if ( !strcmp(key,"newelim") )
10417: nd_newelim = value?1:0;
10418: else if ( !strcmp(key,"intersect") )
10419: nd_intersect = value?1:0;
1.17 noro 10420: else if ( !strcmp(key,"syzgen") )
10421: nd_intersect = ZTOS((Q)value);
1.1 noro 10422: else if ( !strcmp(key,"lf") )
10423: nd_lf = value?1:0;
10424: else if ( !strcmp(key,"trace") ) {
1.38 noro 10425: if ( value ) {
10426: u = BDY((LIST)value);
10427: nd_nzlist = BDY((LIST)ARG2(u));
10428: nd_bpe = ZTOS((Q)ARG3(u));
10429: }
1.1 noro 10430: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 10431: nd_f4red = ZTOS((Q)value);
1.1 noro 10432: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 10433: nd_rank0 = value?1:0;
1.1 noro 10434: } else if ( !strcmp(key,"splist") ) {
1.38 noro 10435: nd_splist = value?1:0;
1.1 noro 10436: } else if ( !strcmp(key,"check_splist") ) {
10437: nd_check_splist = BDY((LIST)value);
10438: } else if ( !strcmp(key,"sugarweight") ) {
10439: u = BDY((LIST)value);
1.38 noro 10440: n = length(u);
10441: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 10442: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 10443: nd_sugarweight[i] = ZTOS((Q)BDY(u));
10444: } else if ( !strcmp(key,"f4_td") ) {
10445: nd_f4_td = value?1:0;
10446: } else if ( !strcmp(key,"sba_f4step") ) {
10447: nd_sba_f4step = value?ZTOS((Q)value):0;
10448: } else if ( !strcmp(key,"sba_pot") ) {
1.44 noro 10449: nd_sba_pot = ZTOS((Q)value);
1.38 noro 10450: } else if ( !strcmp(key,"sba_largelcm") ) {
10451: nd_sba_largelcm = value?1:0;
1.39 noro 10452: } else if ( !strcmp(key,"sba_dontsort") ) {
10453: nd_sba_dontsort = value?1:0;
1.44 noro 10454: } else if ( !strcmp(key,"sba_syz") ) {
10455: nd_sba_syz = value?1:0;
1.46 noro 10456: } else if ( !strcmp(key,"sba_modord") ) {
10457: // value=[vlist,ordspec,weight]
10458: u = BDY((LIST)value);
10459: pltovl((LIST)ARG0(u),&oldvl);
10460: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10461: } else if ( !strcmp(key,"sba_gbinput") ) {
10462: nd_sba_inputisgb = value?1:0;
10463: if ( nd_sba_inputisgb != 0 ) {
10464: // value=[vlist,ordspec,weight]
10465: u = BDY((LIST)value);
10466: pltovl((LIST)ARG0(u),&oldvl);
10467: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10468: }
1.41 noro 10469: } else if ( !strcmp(key,"sba_redundant_check") ) {
10470: nd_sba_redundant_check = value?1:0;
1.39 noro 10471: } else if ( !strcmp(key,"top") ) {
10472: nd_top = value?1:0;
1.1 noro 10473: }
1.38 noro 10474: }
1.44 noro 10475: if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1 noro 10476: }
10477:
10478: ND mdptond(DP d);
10479: ND nd_mul_nm(int mod,NM m0,ND p);
10480: ND nd_mul_nm_lf(NM m0,ND p);
10481: ND *btog(NODE ti,ND **p,int nb,int mod);
10482: ND btog_one(NODE ti,ND *p,int nb,int mod);
10483: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10484: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10485:
10486: /* d:monomial */
10487: ND mdptond(DP d)
10488: {
10489: NM m;
10490: ND r;
10491:
10492: if ( OID(d) == 1 )
10493: r = ptond(CO,CO,(P)d);
10494: else {
10495: NEWNM(m);
10496: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10497: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10498: NEXT(m) = 0;
10499: MKND(NV(d),m,1,r);
10500: }
10501: return r;
10502: }
10503:
10504: ND nd_mul_nm(int mod,NM m0,ND p)
10505: {
10506: UINT *d0;
10507: int c0,c1,c;
10508: NM tm,mr,mr0;
10509: ND r;
10510:
10511: if ( !p ) return 0;
10512: d0 = DL(m0);
10513: c0 = CM(m0);
10514: mr0 = 0;
10515: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10516: NEXTNM(mr0,mr);
10517: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10518: ndl_add(d0,DL(tm),DL(mr));
10519: }
10520: NEXT(mr) = 0;
10521: MKND(NV(p),mr0,LEN(p),r);
10522: return r;
10523: }
10524:
10525: ND nd_mul_nm_lf(NM m0,ND p)
10526: {
10527: UINT *d0;
10528: Z c0,c1,c;
10529: NM tm,mr,mr0;
10530: ND r;
10531:
10532: if ( !p ) return 0;
10533: d0 = DL(m0);
10534: c0 = CZ(m0);
10535: mr0 = 0;
10536: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10537: NEXTNM(mr0,mr);
10538: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10539: ndl_add(d0,DL(tm),DL(mr));
10540: }
10541: NEXT(mr) = 0;
10542: MKND(NV(p),mr0,LEN(p),r);
10543: return r;
10544: }
10545:
10546: ND *btog(NODE ti,ND **p,int nb,int mod)
10547: {
10548: PGeoBucket *r;
10549: int i,ci;
10550: NODE t,s;
10551: ND m,tp;
10552: ND *pi,*rd;
10553: P c;
10554:
10555: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10556: for ( i = 0; i < nb; i++ )
10557: r[i] = create_pbucket();
10558: for ( t = ti; t; t = NEXT(t) ) {
10559: s = BDY((LIST)BDY(t));
10560: if ( ARG0(s) ) {
10561: m = mdptond((DP)ARG2(s));
1.6 noro 10562: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10563: if ( (ci = ((MQ)c)->cont) != 0 ) {
10564: HCM(m) = ci;
1.6 noro 10565: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10566: for ( i = 0; i < nb; i++ ) {
10567: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10568: add_pbucket(mod,r[i],tp);
10569: }
10570: }
10571: ci = 1;
10572: } else {
10573: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10574: ci = invm(ci,mod);
10575: }
10576: }
10577: rd = (ND *)MALLOC(nb*sizeof(ND));
10578: for ( i = 0; i < nb; i++ )
10579: rd[i] = normalize_pbucket(mod,r[i]);
10580: if ( ci != 1 )
10581: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10582: return rd;
10583: }
10584:
10585: /* YYY */
10586: ND *btog_lf(NODE ti,ND **p,int nb)
10587: {
10588: PGeoBucket *r;
10589: int i;
10590: NODE t,s;
10591: ND m,tp;
10592: ND *pi,*rd;
10593: LM lm;
10594: Z lf,c;
10595:
10596: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10597: for ( i = 0; i < nb; i++ )
10598: r[i] = create_pbucket();
10599: for ( t = ti; t; t = NEXT(t) ) {
10600: s = BDY((LIST)BDY(t));
10601: if ( ARG0(s) ) {
10602: m = mdptond((DP)ARG2(s));
1.6 noro 10603: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10604: if ( lm ) {
10605: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10606: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10607: for ( i = 0; i < nb; i++ ) {
10608: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10609: add_pbucket(-2,r[i],tp);
10610: }
10611: }
10612: c = ONE;
10613: } else {
10614: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10615: }
10616: }
10617: rd = (ND *)MALLOC(nb*sizeof(ND));
10618: for ( i = 0; i < nb; i++ )
10619: rd[i] = normalize_pbucket(-2,r[i]);
10620: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10621: return rd;
10622: }
10623:
10624: ND btog_one(NODE ti,ND *p,int nb,int mod)
10625: {
10626: PGeoBucket r;
10627: int i,ci,j;
10628: NODE t,s;
10629: ND m,tp;
10630: ND pi,rd;
10631: P c;
10632:
10633: r = create_pbucket();
10634: for ( t = ti; t; t = NEXT(t) ) {
10635: s = BDY((LIST)BDY(t));
10636: if ( ARG0(s) ) {
10637: m = mdptond((DP)ARG2(s));
1.6 noro 10638: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10639: if ( (ci = ((MQ)c)->cont) != 0 ) {
10640: HCM(m) = ci;
1.6 noro 10641: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10642: if ( !pi ) {
10643: pi = nd_load_mod(j);
10644: tp = nd_mul_nm(mod,BDY(m),pi);
10645: nd_free(pi);
10646: add_pbucket(mod,r,tp);
10647: } else {
10648: tp = nd_mul_nm(mod,BDY(m),pi);
10649: add_pbucket(mod,r,tp);
10650: }
10651: }
10652: ci = 1;
10653: } else {
10654: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10655: ci = invm(ci,mod);
10656: }
10657: }
10658: rd = normalize_pbucket(mod,r);
10659: free_pbucket(r);
10660: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10661: return rd;
10662: }
10663:
10664: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10665:
10666: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10667: {
10668: int i,j,n,m,nb,pi0,pi1,nvar;
10669: VL fv,tv,vv;
10670: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10671: ND **p;
10672: ND *c;
10673: ND u;
10674: P inv;
10675: MAT mat;
10676:
10677: if ( mod == -2 )
10678: return nd_btog_lf(f,v,ord,tlist,rp);
10679:
10680: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10681: parse_nd_option(vv,current_option);
1.1 noro 10682: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10683: switch ( ord->id ) {
10684: case 1:
10685: if ( ord->nv != nvar )
10686: error("nd_check : invalid order specification");
10687: break;
10688: default:
10689: break;
10690: }
10691: nd_init_ord(ord);
10692: #if 0
1.6 noro 10693: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10694: #else
10695: nd_bpe = 32;
10696: #endif
10697: nd_setup_parameters(nvar,0);
10698: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10699: intred = BDY((LIST)ARG3(BDY(tlist)));
10700: ind = BDY((LIST)ARG4(BDY(tlist)));
10701: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10702: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10703: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10704: if ( j > i ) i = j;
10705: }
10706: n = i+1;
10707: nb = length(BDY(f));
10708: p = (ND **)MALLOC(n*sizeof(ND *));
10709: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10710: pi = BDY((LIST)BDY(t));
1.6 noro 10711: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10712: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10713: ptomp(mod,(P)ARG2(pi),&inv);
10714: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10715: u = ptond(CO,vv,(P)ONE);
10716: HCM(u) = ((MQ)inv)->cont;
10717: c[pi1] = u;
10718: }
10719: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10720: printf("%d ",i); fflush(stdout);
10721: ti = BDY((LIST)BDY(t));
1.6 noro 10722: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10723: }
10724: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10725: printf("%d ",i); fflush(stdout);
10726: ti = BDY((LIST)BDY(t));
1.6 noro 10727: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10728: }
10729: m = length(ind);
10730: MKMAT(mat,nb,m);
10731: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10732: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10733: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10734: return mat;
10735: }
10736:
10737: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10738: {
10739: int i,j,n,m,nb,pi0,pi1,nvar;
10740: VL fv,tv,vv;
10741: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10742: ND **p;
10743: ND *c;
10744: ND u;
10745: MAT mat;
10746: LM lm;
10747: Z lf,inv;
10748:
10749: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10750: parse_nd_option(vv,current_option);
1.1 noro 10751: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10752: switch ( ord->id ) {
10753: case 1:
10754: if ( ord->nv != nvar )
10755: error("nd_check : invalid order specification");
10756: break;
10757: default:
10758: break;
10759: }
10760: nd_init_ord(ord);
10761: #if 0
1.6 noro 10762: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10763: #else
10764: nd_bpe = 32;
10765: #endif
10766: nd_setup_parameters(nvar,0);
10767: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10768: intred = BDY((LIST)ARG3(BDY(tlist)));
10769: ind = BDY((LIST)ARG4(BDY(tlist)));
10770: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10771: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10772: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10773: if ( j > i ) i = j;
10774: }
10775: n = i+1;
10776: nb = length(BDY(f));
10777: p = (ND **)MALLOC(n*sizeof(ND *));
10778: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10779: pi = BDY((LIST)BDY(t));
1.6 noro 10780: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10781: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10782: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
10783: u = ptond(CO,vv,(P)ONE);
10784: HCZ(u) = inv;
10785: c[pi1] = u;
10786: }
10787: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10788: printf("%d ",i); fflush(stdout);
10789: ti = BDY((LIST)BDY(t));
1.6 noro 10790: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10791: }
10792: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10793: printf("%d ",i); fflush(stdout);
10794: ti = BDY((LIST)BDY(t));
1.6 noro 10795: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10796: }
10797: m = length(ind);
10798: MKMAT(mat,nb,m);
10799: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10800: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10801: BDY(mat)[i][j] = ndtodp(-2,c[i]);
10802: return mat;
10803: }
10804:
10805: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
10806: LIST tlist,int pos,MAT *rp)
10807: {
10808: int i,j,n,m,nb,pi0,pi1,nvar;
10809: VL fv,tv,vv;
10810: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10811: ND *p;
10812: ND *c;
10813: ND u;
10814: P inv;
10815: VECT vect;
10816:
10817: if ( mod == -2 )
10818: error("nd_btog_one : not implemented yet for a large finite field");
10819:
10820: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10821: parse_nd_option(vv,current_option);
1.1 noro 10822: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10823: switch ( ord->id ) {
10824: case 1:
10825: if ( ord->nv != nvar )
10826: error("nd_check : invalid order specification");
10827: break;
10828: default:
10829: break;
10830: }
10831: nd_init_ord(ord);
10832: #if 0
1.6 noro 10833: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10834: #else
10835: nd_bpe = 32;
10836: #endif
10837: nd_setup_parameters(nvar,0);
10838: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10839: intred = BDY((LIST)ARG3(BDY(tlist)));
10840: ind = BDY((LIST)ARG4(BDY(tlist)));
10841: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10842: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10843: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10844: if ( j > i ) i = j;
10845: }
10846: n = i+1;
10847: nb = length(BDY(f));
10848: p = (ND *)MALLOC(n*sizeof(ND *));
10849: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10850: pi = BDY((LIST)BDY(t));
1.6 noro 10851: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10852: if ( pi1 == pos ) {
10853: ptomp(mod,(P)ARG2(pi),&inv);
10854: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10855: u = ptond(CO,vv,(P)ONE);
10856: HCM(u) = ((MQ)inv)->cont;
10857: p[pi0] = u;
10858: }
10859: }
10860: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10861: printf("%d ",i); fflush(stdout);
10862: ti = BDY((LIST)BDY(t));
1.6 noro 10863: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10864: if ( Demand ) {
10865: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10866: }
10867: }
10868: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10869: printf("%d ",i); fflush(stdout);
10870: ti = BDY((LIST)BDY(t));
1.6 noro 10871: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10872: if ( Demand ) {
10873: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10874: }
10875: }
10876: m = length(ind);
10877: MKVECT(vect,m);
10878: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10879: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10880: if ( !u ) {
1.6 noro 10881: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10882: BDY(vect)[j] = ndtodp(mod,u);
10883: nd_free(u);
10884: } else
10885: BDY(vect)[j] = ndtodp(mod,u);
10886: }
10887: return vect;
10888: }
10889:
10890: void ndv_print_lf(NDV p)
10891: {
10892: NMV m;
10893: int i,len;
10894:
10895: if ( !p ) printf("0\n");
10896: else {
10897: len = LEN(p);
10898: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10899: printf("+");
10900: mpz_out_str(asir_out,10,BDY(CZ(m)));
10901: printf("*");
10902: ndl_print(DL(m));
10903: }
10904: printf("\n");
10905: }
10906: }
10907:
10908: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10909: {
10910: VL tv,fv,vv,vc,av;
10911: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10912: int m,nocheck,nvar,mindex,e,max;
10913: NDV c;
10914: NMV a;
10915: P p,zp;
10916: Q dmy;
10917: EPOS oepos;
10918: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10919: Alg alpha,dp;
10920: P poly;
10921: LIST f1,f2,zpl;
10922: Obj obj;
10923: NumberField nf;
10924: struct order_spec *ord1;
10925: struct oEGT eg_check,eg0,eg1;
10926: NODE tr,tl1,tl2,tl3,tl4;
10927: LIST l1,l2,l3,l4,l5;
10928: int *perm;
10929: int j,ret;
10930: NODE retn;
10931: Q jq,bpe;
10932:
10933: nd_module = 0;
10934: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10935: parse_nd_option(vv,current_option);
1.1 noro 10936: if ( nd_vc )
10937: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10938: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10939: switch ( ord->id ) {
10940: case 1:
10941: if ( ord->nv != nvar )
10942: error("nd_f4_lf_trace : invalid order specification");
10943: break;
10944: default:
10945: break;
10946: }
10947:
10948: nd_ntrans = nvar;
10949: nd_nalg = 0;
10950:
10951: nocheck = 0;
10952: mindex = 0;
10953:
10954: /* do not use on-demand load/save */
10955: nd_demand = 0;
10956: m = trace > 1 ? trace : get_lprime(mindex);
10957: nd_init_ord(ord);
10958: mrank = 0;
10959: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10960: for ( tv = vv; tv; tv = NEXT(tv) ) {
10961: if ( nd_module ) {
10962: s = BDY((LIST)BDY(t));
10963: trank = length(s);
10964: mrank = MAX(mrank,trank);
10965: for ( ; s; s = NEXT(s) ) {
10966: e = getdeg(tv->v,(P)BDY(s));
10967: max = MAX(e,max);
10968: }
10969: } else {
10970: e = getdeg(tv->v,(P)BDY(t));
10971: max = MAX(e,max);
10972: }
10973: }
10974: nd_setup_parameters(nvar,max);
10975: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10976: ishomo = 1;
10977: /* XXX */
10978: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10979: if ( nd_module ) {
10980: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10981: } else {
10982: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10983: }
10984: if ( ishomo )
10985: ishomo = ishomo && ndv_ishomo(c);
10986: if ( c ) {
10987: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10988: ndv_mod(-2,c);
10989: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10990: }
10991: }
10992: if ( in0 ) NEXT(in) = 0;
10993: if ( fd0 ) NEXT(fd) = 0;
10994: if ( !ishomo && homo ) {
10995: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10996: c = (NDV)BDY(t); len = LEN(c);
10997: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10998: wmax = MAX(TD(DL(a)),wmax);
10999: }
11000: homogenize_order(ord,nvar,&ord1);
11001: nd_init_ord(ord1);
11002: nd_setup_parameters(nvar+1,wmax);
11003: for ( t = fd0; t; t = NEXT(t) )
11004: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
11005: }
11006: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 11007: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 11008: if ( ret )
11009: cand = nd_f4_lf_trace_main(m,&perm);
11010: if ( !ret || !cand ) {
11011: *rp = 0; return;
11012: }
11013: if ( !ishomo && homo ) {
11014: /* dehomogenization */
11015: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
11016: nd_init_ord(ord);
11017: nd_setup_parameters(nvar,0);
11018: }
11019: cand = ndv_reducebase(cand,perm);
11020: cand = ndv_reduceall(-2,cand);
11021: cbpe = nd_bpe;
11022: get_eg(&eg0);
11023: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
11024: /* gbcheck : cand is a GB of Id(cand) ? */
11025: retn = nd_f4(-2,0,0);
11026: }
11027: if ( !retn ) {
11028: /* failure */
11029: *rp = 0; return;
11030: }
11031: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
11032: if ( DP_Print )
1.5 noro 11033: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 11034: /* dp->p */
11035: nd_bpe = cbpe;
11036: nd_setup_parameters(nd_nvar,0);
11037: for ( r = cand; r; r = NEXT(r) ) {
11038: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
11039: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
11040: }
11041: MKLIST(*rp,cand);
11042: }
11043:
11044: NODE nd_f4_lf_trace_main(int m,int **indp)
11045: {
11046: int i,nh,stat,index;
11047: NODE r,rm,g;
11048: ND_pairs d,l,l0,t;
11049: ND spol,red;
11050: NDV nf,redv,nfqv,nfv;
11051: NM s0,s;
11052: NODE rp0,srp0,nflist,nflist_lf;
11053: int nsp,nred,col,rank,len,k,j,a;
11054: UINT c;
11055: UINT **spmat;
11056: UINT *s0vect,*svect,*p,*v;
11057: int *colstat;
11058: IndArray *imat;
11059: int *rhead;
11060: int spcol,sprow;
11061: int sugar;
11062: PGeoBucket bucket;
11063: struct oEGT eg0,eg1,eg_f4;
11064:
11065: g = 0; d = 0;
11066: for ( i = 0; i < nd_psn; i++ ) {
11067: d = update_pairs(d,g,i,0);
11068: g = update_base(g,i);
11069: }
11070: while ( d ) {
11071: get_eg(&eg0);
11072: l = nd_minsugarp(d,&d);
11073: sugar = SG(l);
11074: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
11075: bucket = create_pbucket();
11076: stat = nd_sp_f4(m,0,l,bucket);
11077: if ( !stat ) {
11078: for ( t = l; NEXT(t); t = NEXT(t) );
11079: NEXT(t) = d; d = l;
11080: d = nd_reconstruct(1,d);
11081: continue;
11082: }
11083: if ( bucket->m < 0 ) continue;
11084: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
11085: if ( !col ) {
11086: for ( t = l; NEXT(t); t = NEXT(t) );
11087: NEXT(t) = d; d = l;
11088: d = nd_reconstruct(1,d);
11089: continue;
11090: }
11091: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
11092: if ( DP_Print )
1.5 noro 11093: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 11094: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
11095: if ( !l0 ) continue;
11096: l = l0;
11097:
11098: /* over LF */
11099: bucket = create_pbucket();
11100: stat = nd_sp_f4(-2,1,l,bucket);
11101: if ( !stat ) {
11102: for ( t = l; NEXT(t); t = NEXT(t) );
11103: NEXT(t) = d; d = l;
11104: d = nd_reconstruct(1,d);
11105: continue;
11106: }
11107: if ( bucket->m < 0 ) continue;
11108: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
11109: if ( !col ) {
11110: for ( t = l; NEXT(t); t = NEXT(t) );
11111: NEXT(t) = d; d = l;
11112: d = nd_reconstruct(1,d);
11113: continue;
11114: }
11115: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
11116: /* adding new bases */
11117: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
11118: nfv = (NDV)BDY(rm);
11119: nfqv = (NDV)BDY(r);
11120: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
11121: ndv_removecont(m,nfv);
11122: ndv_removecont(-2,nfqv);
1.24 noro 11123: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 11124: d = update_pairs(d,g,nh,0);
11125: g = update_base(g,nh);
11126: }
11127: if ( r || rm ) return 0;
11128: }
11129: conv_ilist(nd_demand,1,g,indp);
11130: return g;
11131: }
11132:
1.7 noro 11133: #if SIZEOF_LONG==8
11134:
11135: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
11136: {
11137: int j,k,len;
11138: UINT *p;
11139: UINT c;
11140: NDV r;
11141: NMV mr0,mr;
11142:
11143: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
11144: if ( !len ) return 0;
11145: else {
11146: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11147: #if 0
11148: ndv_alloc += nmv_adv*len;
11149: #endif
11150: mr = mr0;
11151: p = s0vect;
11152: for ( j = k = 0; j < col; j++, p += nd_wpd )
11153: if ( !rhead[j] ) {
11154: if ( (c = (UINT)vect[k++]) != 0 ) {
11155: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11156: }
11157: }
11158: MKNDV(nd_nvar,mr0,len,r);
11159: return r;
11160: }
11161: }
11162:
1.28 noro 11163: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
11164: {
11165: int j,k,len;
11166: UINT *p;
11167: UINT c;
11168: NDV r;
11169: NMV mr0,mr;
11170:
11171: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
11172: if ( !len ) return 0;
11173: else {
11174: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11175: mr = mr0;
11176: p = s0vect;
11177: for ( j = k = 0; j < col; j++, p += nd_wpd )
11178: if ( (c = (UINT)vect[k++]) != 0 ) {
11179: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11180: }
11181: MKNDV(nd_nvar,mr0,len,r);
11182: return r;
11183: }
11184: }
11185:
1.7 noro 11186: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
11187: {
11188: NM m;
1.11 noro 11189: UINT *t,*s,*u;
11190: int i,st,ed,md,prev,c;
1.7 noro 11191:
11192: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 11193: prev = 0;
11194: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
11195: t = DL(m);
11196: st = prev;
11197: ed = n;
11198: while ( ed > st ) {
11199: md = (st+ed)/2;
11200: u = s0+md*nd_wpd;
11201: c = DL_COMPARE(u,t);
11202: if ( c == 0 ) break;
11203: else if ( c > 0 ) st = md;
11204: else ed = md;
11205: }
11206: r[md] = (mp_limb_t)CM(m);
11207: prev = md;
1.7 noro 11208: }
11209: for ( i = 0; !r[i]; i++ );
11210: return i;
11211: }
11212:
11213: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
11214:
1.28 noro 11215: 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 11216: {
11217: int i,j,k,len,pos,prev;
11218: mp_limb_t a,c,c1,c2;
11219: IndArray ivect;
11220: unsigned char *ivc;
11221: unsigned short *ivs;
11222: unsigned int *ivi;
11223: NDV redv;
11224: NMV mr;
11225: NODE rp;
11226: int maxrs;
11227:
11228: for ( i = 0; i < col; i++ ) cvect[i] = 0;
11229: maxrs = 0;
11230: for ( i = 0; i < nred; i++ ) {
11231: ivect = imat[i];
11232: k = ivect->head;
11233: a = svect[k]; c = cvect[k];
11234: MOD128(a,c,m);
11235: svect[k] = a; cvect[k] = 0;
1.28 noro 11236: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 11237: Nf4_red++;
1.7 noro 11238: maxrs = MAX(maxrs,rp0[i]->sugar);
11239: c = m-c; redv = nd_ps[rp0[i]->index];
11240: len = LEN(redv); mr = BDY(redv);
11241: svect[k] = 0; prev = k;
11242: switch ( ivect->width ) {
11243: case 1:
11244: ivc = ivect->index.c;
11245: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11246: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 11247: c2 = svect[pos]+c1*c;
11248: if ( c2 < svect[pos] ) cvect[pos]++;
11249: svect[pos] = c2;
1.7 noro 11250: }
11251: break;
11252: case 2:
11253: ivs = ivect->index.s;
11254: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11255: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 11256: c2 = svect[pos]+c1*c;
11257: if ( c2 < svect[pos] ) cvect[pos]++;
11258: svect[pos] = c2;
1.7 noro 11259: }
11260: break;
11261: case 4:
11262: ivi = ivect->index.i;
11263: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11264: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 11265: c2 = svect[pos]+c1*c;
11266: if ( c2 < svect[pos] ) cvect[pos]++;
11267: svect[pos] = c2;
1.7 noro 11268: }
11269: break;
11270: }
11271: }
11272: }
11273: for ( i = 0; i < col; i++ ) {
11274: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
11275: }
11276: return maxrs;
11277: }
11278:
11279: /* for Fp, 2^15=<p<2^29 */
11280:
11281: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11282: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
11283: {
11284: int spcol,sprow,a;
11285: int i,j,k,l,rank;
11286: NODE r0,r;
11287: ND_pairs sp;
11288: ND spol;
11289: mp_limb_t **spmat;
11290: mp_limb_t *svect,*cvect;
11291: mp_limb_t *v;
11292: int *colstat;
11293: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11294: int maxrs;
11295: int *spsugar;
11296: ND_pairs *spactive;
11297:
11298: spcol = col-nred;
11299: get_eg(&eg0);
11300: /* elimination (1st step) */
11301: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11302: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11303: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11304: spsugar = (int *)MALLOC(nsp*sizeof(int));
11305: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
11306: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11307: nd_sp(m,0,sp,&spol);
11308: if ( !spol ) continue;
11309: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 11310: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 11311: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11312: if ( i < col ) {
11313: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
11314: for ( j = k = 0; j < col; j++ )
11315: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
11316: spsugar[sprow] = MAX(maxrs,SG(spol));
11317: if ( nz )
11318: spactive[sprow] = sp;
11319: sprow++;
11320: }
11321: nd_free(spol);
11322: }
1.12 noro 11323: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 11324: if ( DP_Print ) {
11325: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11326: fflush(asir_out);
11327: }
11328: /* free index arrays */
11329: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11330:
11331: /* elimination (2nd step) */
11332: colstat = (int *)MALLOC(spcol*sizeof(int));
11333: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
11334: r0 = 0;
11335: for ( i = 0; i < rank; i++ ) {
11336: NEXTNODE(r0,r); BDY(r) =
11337: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
11338: SG((NDV)BDY(r)) = spsugar[i];
11339: GCFREE(spmat[i]);
11340: }
11341: if ( r0 ) NEXT(r) = 0;
11342:
11343: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 11344: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 11345: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11346: if ( DP_Print ) {
11347: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11348: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11349: nsp,nred,sprow,spcol,rank);
11350: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11351: }
11352: if ( nz ) {
11353: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
11354: if ( rank > 0 ) {
11355: NEXT(spactive[rank-1]) = 0;
11356: *nz = spactive[0];
11357: } else
11358: *nz = 0;
11359: }
11360: return r0;
11361: }
11362:
11363: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
11364: {
11365: int i,j,k,l,rank,s;
11366: mp_limb_t inv;
11367: mp_limb_t a;
11368: UINT c;
11369: mp_limb_t *t,*pivot,*pk;
11370: UINT *ck;
11371: UINT **cmat;
11372: UINT *ct;
11373: ND_pairs pair;
11374:
11375: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11376: for ( i = 0; i < row; i++ ) {
11377: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11378: bzero(cmat[i],col*sizeof(UINT));
11379: }
11380:
11381: for ( rank = 0, j = 0; j < col; j++ ) {
11382: for ( i = rank; i < row; i++ ) {
11383: a = mat[i][j]; c = cmat[i][j];
11384: MOD128(a,c,md);
11385: mat[i][j] = a; cmat[i][j] = 0;
11386: }
11387: for ( i = rank; i < row; i++ )
11388: if ( mat[i][j] )
11389: break;
11390: if ( i == row ) {
11391: colstat[j] = 0;
11392: continue;
11393: } else
11394: colstat[j] = 1;
11395: if ( i != rank ) {
11396: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
11397: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
11398: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
11399: if ( spactive ) {
11400: pair = spactive[i]; spactive[i] = spactive[rank];
11401: spactive[rank] = pair;
11402: }
11403: }
11404: /* column j is normalized */
11405: s = sugar[rank];
11406: inv = invm((UINT)mat[rank][j],md);
11407: /* normalize pivot row */
11408: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
11409: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11410: }
11411: for ( i = rank+1; i < row; i++ ) {
11412: if ( (a = mat[i][j]) != 0 ) {
11413: sugar[i] = MAX(sugar[i],s);
11414: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 11415: Nf4_red++;
1.7 noro 11416: }
11417: }
11418: rank++;
11419: }
11420: for ( j = col-1, l = rank-1; j >= 0; j-- )
11421: if ( colstat[j] ) {
11422: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
11423: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
11424: }
11425: s = sugar[l];
11426: for ( i = 0; i < l; i++ ) {
11427: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
11428: if ( a ) {
11429: sugar[i] = MAX(sugar[i],s);
11430: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 11431: Nf4_red++;
1.7 noro 11432: }
11433: }
11434: l--;
11435: }
11436: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11437: GCFREE(cmat);
11438: return rank;
11439: }
11440:
1.28 noro 11441: 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)
11442: {
11443: int i,j,k,l,rank,s,imin;
11444: mp_limb_t inv;
11445: mp_limb_t a;
11446: UINT c;
11447: mp_limb_t *t,*pivot,*pk;
11448: UINT *ck;
11449: UINT **cmat;
11450: UINT *ct;
11451: ND_pairs pair;
11452: SIG sg;
1.31 noro 11453: int *used;
1.28 noro 11454:
1.31 noro 11455: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 11456: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11457: for ( i = 0; i < row; i++ ) {
11458: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11459: bzero(cmat[i],col*sizeof(UINT));
11460: }
11461:
1.31 noro 11462: for ( j = 0; j < col; j++ ) {
11463: for ( i = 0; i < row; i++ ) {
1.28 noro 11464: a = mat[i][j]; c = cmat[i][j];
11465: MOD128(a,c,md);
11466: mat[i][j] = a; cmat[i][j] = 0;
11467: }
1.31 noro 11468: for ( i = 0; i < row; i++ )
11469: if ( !used[i] && mat[i][j] ) break;
11470: if ( i == row ) {
1.28 noro 11471: colstat[j] = 0;
11472: continue;
1.31 noro 11473: } else {
1.28 noro 11474: colstat[j] = 1;
1.31 noro 11475: used[i] = 1;
1.28 noro 11476: }
11477: /* column j is normalized */
1.31 noro 11478: s = sugar[i];
11479: inv = invm((UINT)mat[i][j],md);
1.28 noro 11480: /* normalize pivot row */
1.31 noro 11481: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 11482: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11483: }
1.31 noro 11484: for ( k = i+1; k < row; k++ ) {
11485: if ( (a = mat[k][j]) != 0 ) {
11486: sugar[k] = MAX(sugar[k],s);
11487: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11488: Nf4_red++;
11489: }
11490: }
11491: }
1.31 noro 11492: rank = 0;
11493: for ( i = 0; i < row; i++ ) {
11494: for ( j = 0; j < col; j++ )
11495: if ( mat[i][j] ) break;
11496: if ( j == col ) sugar[i] = -1;
11497: else rank++;
11498: }
1.28 noro 11499: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11500: GCFREE(cmat);
11501: return rank;
11502: }
11503:
11504: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11505: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11506: {
11507: int spcol,sprow,a;
11508: int i,j,k,l,rank;
11509: NODE r0,r;
11510: ND_pairs sp;
11511: ND spol;
11512: mp_limb_t **spmat;
11513: mp_limb_t *svect,*cvect;
11514: mp_limb_t *v;
11515: int *colstat;
11516: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11517: int maxrs;
11518: int *spsugar;
11519: ND_pairs *spactive;
11520: SIG *spsig;
11521:
11522: get_eg(&eg0);
11523: /* elimination (1st step) */
11524: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11525: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11526: spsugar = (int *)MALLOC(nsp*sizeof(int));
11527: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11528: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11529: nd_sp(m,0,sp,&spol);
11530: if ( !spol ) {
1.29 noro 11531: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11532: continue;
11533: }
11534: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11535: nd_to_vect64(m,s0vect,col,spol,svect);
11536: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11537: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11538: if ( i < col ) {
11539: spmat[sprow] = svect;
11540: spsugar[sprow] = MAX(maxrs,SG(spol));
11541: spsig[sprow] = sp->sig;
11542: sprow++;
11543: } else {
1.29 noro 11544: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11545: }
11546: nd_free(spol);
11547: }
11548: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
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(col*sizeof(int));
11558: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11559: r0 = 0;
1.31 noro 11560: for ( i = 0; i < sprow; i++ ) {
11561: if ( spsugar[i] >= 0 ) {
11562: NEXTNODE(r0,r);
11563: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11564: SG((NDV)BDY(r)) = spsugar[i];
11565: ((NDV)BDY(r))->sig = spsig[i];
11566: } else
11567: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11568: GCFREE(spmat[i]);
11569: }
11570: if ( r0 ) NEXT(r) = 0;
11571: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11572: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11573: if ( DP_Print ) {
11574: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11575: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11576: nsp,nred,sprow,col,rank);
11577: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11578: }
11579: return r0;
11580: }
1.40 noro 11581: #endif
1.28 noro 11582:
11583: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11584: {
11585: IndArray *imat;
11586: int nsp,nred,i,start;
11587: int *rhead;
11588: NODE r0,rp;
11589: ND_pairs sp;
11590: NM_ind_pair *rvect;
11591: UINT *s;
11592: int *s0hash;
11593: struct oEGT eg0,eg1,eg_conv;
11594:
11595: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11596: nred = length(rp0);
11597: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11598: rhead = (int *)MALLOC(col*sizeof(int));
11599: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11600:
11601: /* construction of index arrays */
11602: get_eg(&eg0);
11603: if ( DP_Print ) {
11604: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11605: fflush(asir_out);
11606: }
11607: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11608: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11609: rvect[i] = (NM_ind_pair)BDY(rp);
11610: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11611: rhead[imat[i]->head] = 1;
11612: start = imat[i]->head;
11613: }
11614: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11615: if ( DP_Print ) {
11616: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11617: fflush(asir_out);
11618: }
11619: if ( m > 0 )
1.40 noro 11620: #if SIZEOF_LONG==8
1.28 noro 11621: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11622: #else
11623: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11624: #endif
1.28 noro 11625: else
11626: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11627: error("nd_f4_red_q_main_s : not implemented yet");
11628: return r0;
11629: }
11630:
11631: INLINE int ndl_find_reducer_minsig(UINT *dg)
11632: {
11633: RHist r;
11634: int i,singular,ret,d,k,imin;
11635: SIG t;
11636: static int wpd,nvar;
11637: static SIG quo,quomin;
11638: static UINT *tmp;
11639:
11640: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11641: if ( wpd != nd_wpd ) {
11642: wpd = nd_wpd;
11643: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11644: }
11645: #if 0
11646: d = ndl_hash_value(dg);
11647: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11648: if ( ndl_equal(dg,DL(r)) ) {
11649: return r->index;
11650: }
11651: }
11652: #endif
11653: imin = -1;
11654: for ( i = 0; i < nd_psn; i++ ) {
11655: r = nd_psh[i];
11656: if ( ndl_reducible(dg,DL(r)) ) {
11657: ndl_sub(dg,DL(r),tmp);
11658: _ndltodl(tmp,DL(quo));
11659: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11660: quo->pos = nd_psh[i]->sig->pos;
11661: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11662: t = quo; quo = quomin; quomin = t;
11663: imin = i;
11664: }
11665: }
11666: }
11667: if ( imin == -1 ) return nd_psn;
11668: else {
11669: #if 0
11670: nd_append_red(dg,i);
11671: #endif
11672: return imin;
11673: }
11674: }
11675:
11676: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11677: {
11678: NODE rp0,rp;
11679: NM mul,head,s0,s;
11680: int index,col,i,sugar;
11681: RHist h;
11682: UINT *s0v,*p;
11683: NM_ind_pair pair;
11684: ND red;
11685: NDV *ps;
11686: SIG sig;
11687:
11688: s0 = 0; rp0 = 0; col = 0;
11689: if ( nd_demand )
11690: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11691: else
11692: ps = trace?nd_ps_trace:nd_ps;
11693: while ( 1 ) {
11694: head = remove_head_pbucket_symbolic(bucket);
11695: if ( !head ) break;
11696: if ( !s0 ) s0 = head;
11697: else NEXT(s) = head;
11698: s = head;
11699: index = ndl_find_reducer_minsig(DL(head));
11700: if ( index >= 0 && index < nd_psn ) {
11701: h = nd_psh[index];
11702: NEWNM(mul);
11703: ndl_sub(DL(head),DL(h),DL(mul));
11704: if ( ndl_check_bound2(index,DL(mul)) )
11705: return 0;
11706: sugar = TD(DL(mul))+SG(ps[index]);
11707: NEWSIG(sig);
11708: _ndltodl(DL(mul),DL(sig));
11709: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11710: sig->pos = nd_psh[index]->sig->pos;
11711: MKNM_ind_pair(pair,mul,index,sugar,sig);
11712: red = ndv_mul_nm_symbolic(mul,ps[index]);
11713: add_pbucket_symbolic(bucket,nd_remove_head(red));
11714: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11715: }
11716: col++;
11717: }
11718: if ( rp0 ) NEXT(rp) = 0;
11719: NEXT(s) = 0;
11720: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11721: for ( i = 0, p = s0v, s = s0; i < col;
11722: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11723: *s0vect = s0v;
11724: *r = rp0;
11725:
11726: return col;
11727: }
11728:
11729: NODE nd_sba_f4(int m,int **indp)
11730: {
1.34 noro 11731: int i,nh,stat,index,f4red,f4step;
1.33 noro 11732: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11733: NODE r,g,rp0,nflist;
1.41 noro 11734: ND_pairs d,l,t,l1;
1.33 noro 11735: ND h,nf;
11736: NDV nfv;
11737: union oNDC hc;
11738: UINT *s0vect;
1.28 noro 11739: UINT c;
11740: PGeoBucket bucket;
1.33 noro 11741: NODE *syzlist;
11742: SIG sig;
1.28 noro 11743: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11744: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11745:
11746: Nf4_red=0;
1.30 noro 11747: d = 0;
1.29 noro 11748: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11749: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11750: d = update_pairs_s(d,i,syzlist);
1.28 noro 11751: }
1.30 noro 11752: nd_nbase = nd_psn;
1.28 noro 11753: f4red = 1;
1.33 noro 11754: psugar = 0;
1.34 noro 11755: f4step = 0;
1.28 noro 11756: while ( d ) {
1.33 noro 11757: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11758: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11759: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11760: again:
11761: l = d; d = d->next;
1.41 noro 11762: #if 0
1.33 noro 11763: if ( small_lcm(l) ) {
11764: if ( DP_Print ) fprintf(asir_out,"M");
11765: continue;
11766: }
11767: sig = l->sig;
11768: stat = nd_sp(m,0,l,&h);
1.41 noro 11769: #else
11770: l1 = find_smallest_lcm(l);
11771: if ( l1 == 0 ) {
11772: if ( DP_Print ) fprintf(asir_out,"M");
11773: continue;
11774: }
11775: sig = l1->sig;
11776: stat = nd_sp(m,0,l1,&h);
11777: #endif
1.33 noro 11778: if ( !stat ) {
11779: NEXT(l) = d; d = l;
11780: d = nd_reconstruct(0,d);
11781: goto again;
11782: }
11783: get_eg(&eg1);
11784: #if USE_GEOBUCKET
1.39 noro 11785: 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 11786: #else
1.39 noro 11787: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11788: #endif
11789: get_eg(&eg2);
11790: if ( !stat ) {
11791: NEXT(l) = d; d = l;
11792: d = nd_reconstruct(0,d);
11793: goto again;
11794: } else if ( stat == -1 ) {
11795: if ( DP_Print ) { printf("S"); fflush(stdout); }
11796: FREENDP(l);
11797: } else if ( nf ) {
11798: if ( DP_Print ) { printf("+"); fflush(stdout); }
11799: add_eg(&eg_nf,&eg1,&eg2);
11800: hc = HCU(nf);
11801: nd_removecont(m,nf);
11802: nfv = ndtondv(m,nf); nd_free(nf);
11803: nh = ndv_newps(m,nfv,0);
11804:
1.30 noro 11805: d = update_pairs_s(d,nh,syzlist);
11806: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 11807: FREENDP(l);
1.28 noro 11808: } else {
1.33 noro 11809: add_eg(&eg_nfzero,&eg1,&eg2);
11810: // syzygy
11811: get_eg(&eg1);
11812: d = remove_spair_s(d,sig);
11813: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 11814: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 11815: if ( DP_Print ) { printf("."); fflush(stdout); }
11816: FREENDP(l);
11817: }
11818: } else {
1.34 noro 11819: if ( ms != psugar ) f4step = 1;
11820: else f4step++;
1.33 noro 11821: again2:
11822: psugar = ms;
11823: l = nd_minsugarp_s(d,&d);
11824: sugar = nd_sugarweight?d->sugar2:SG(d);
11825: bucket = create_pbucket();
11826: stat = nd_sp_f4(m,0,l,bucket);
11827: if ( !stat ) {
11828: for ( t = l; NEXT(t); t = NEXT(t) );
11829: NEXT(t) = d; d = l;
11830: d = nd_reconstruct(0,d);
11831: goto again2;
11832: }
11833: if ( bucket->m < 0 ) continue;
11834: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
11835: if ( !col ) {
1.37 noro 11836: for ( t = l; NEXT(t); t = NEXT(t) )
11837: ;
11838: NEXT(t) = d; d = l;
1.33 noro 11839: d = nd_reconstruct(0,d);
11840: goto again2;
11841: }
1.34 noro 11842: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 11843: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
11844: /* adding new bases */
11845: for ( r = nflist; r; r = NEXT(r) ) {
11846: nfv = (NDV)BDY(r);
1.38 noro 11847: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 11848: ndv_removecont(m,nfv);
11849: nh = ndv_newps(m,nfv,0);
11850: d = update_pairs_s(d,nh,syzlist);
11851: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
11852: }
11853: for ( i = 0; i < nd_nbase; i++ )
11854: for ( r = syzlist[i]; r; r = NEXT(r) )
11855: d = remove_spair_s(d,(SIG)BDY(r));
11856: d = remove_large_lcm(d);
11857: if ( DP_Print ) {
1.34 noro 11858: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11859: }
11860: f4red++;
1.28 noro 11861: }
11862: }
11863: if ( DP_Print ) {
1.34 noro 11864: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11865: }
1.30 noro 11866: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11867: return g;
11868: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>