Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.56
1.56 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.55 2021/11/29 09:19:33 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.45 noro 5: void print_siglist(NODE l);
6:
1.49 noro 7: NODE nd_hpdata;
1.47 noro 8: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs,Nnewpair;
1.12 noro 9: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 10:
11: int diag_period = 6;
12: int weight_check = 1;
13: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 14: /* for general module order */
1.19 noro 15: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 16: int (*dl_base_compare_function)(int nv,DL a,DL b);
17: int nd_base_ordtype;
1.1 noro 18: int nd_dcomp;
19: int nd_rref2;
20: NM _nm_free_list;
21: ND _nd_free_list;
22: ND_pairs _ndp_free_list;
23: NODE nd_hcf;
1.35 noro 24: int Nsyz,Nsamesig;
1.1 noro 25:
26: Obj nd_top_weight;
27:
28: static NODE nd_subst;
29: static VL nd_vc;
30: static int nd_ntrans;
31: static int nd_nalg;
32: #if 0
33: static int ndv_alloc;
34: #endif
35: #if 1
36: static int nd_f4_nsp=0x7fffffff;
37: #else
38: static int nd_f4_nsp=50;
39: #endif
40: static double nd_scale=2;
41: static UINT **nd_bound;
42: static struct order_spec *nd_ord;
43: static EPOS nd_epos;
44: static BlockMask nd_blockmask;
45: static int nd_nvar;
46: static int nd_isrlex;
47: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
48: static UINT nd_mask[32];
49: static UINT nd_mask0,nd_mask1;
50:
51: static NDV *nd_ps;
52: static NDV *nd_ps_trace;
53: static NDV *nd_ps_sym;
54: static NDV *nd_ps_trace_sym;
55: static RHist *nd_psh;
1.30 noro 56: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 57: static RHist *nd_red;
58: static int *nd_work_vector;
59: static int **nd_matrix;
60: static int nd_matrix_len;
61: static struct weight_or_block *nd_worb;
62: static int nd_worb_len;
63: static int nd_found,nd_create,nd_notfirst;
64: static int nmv_adv;
65: static int nd_demand;
1.21 noro 66: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 67: static int nd_module_rank,nd_poly_weight_len;
68: static int *nd_poly_weight,*nd_module_weight;
69: static NODE nd_tracelist;
70: static NODE nd_alltracelist;
1.41 noro 71: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
72: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.46 noro 73: static int nd_top,nd_sba_syz,nd_sba_inputisgb;
1.1 noro 74: static int *nd_gbblock;
75: static NODE nd_nzlist,nd_check_splist;
76: static int nd_splist;
77: static int *nd_sugarweight;
78: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 79: static DL *nd_sba_hm;
1.29 noro 80: static NODE *nd_sba_pos;
1.1 noro 81:
1.46 noro 82: struct comp_sig_spec {
83: int n;
84: // current_i <-> oldv[i]
85: int *oldv;
86: int *weight;
87: struct order_pair *order_pair;
88: int block_length;
89: int **matrix;
90: int row;
91: int (*cmpdl)(int n,DL d1,DL d2);
92: };
93:
94: struct comp_sig_spec *nd_sba_modord;
95:
1.49 noro 96: DL ndltodl(int n,UINT *ndl);
1.1 noro 97: NumberField get_numberfield();
98: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
99: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
100: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
101: int nd_monic(int m,ND *p);
102: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
103: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
104: NDV pltondv(VL vl,VL dvl,LIST p);
105: void pltozpl(LIST l,Q *cont,LIST *pp);
106: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
107: void nmtodp(int mod,NM m,DP *r);
1.15 noro 108: void ndltodp(UINT *d,DP *r);
1.1 noro 109: NODE reverse_node(NODE n);
110: P ndc_div(int mod,union oNDC a,union oNDC b);
111: P ndctop(int mod,union oNDC c);
112: void finalize_tracelist(int i,P cont);
113: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.46 noro 114: void parse_nd_option(VL vl,NODE opt);
1.1 noro 115: void dltondl(int n,DL dl,UINT *r);
116: DP ndvtodp(int mod,NDV p);
117: DP ndtodp(int mod,ND p);
1.16 noro 118: DPM ndvtodpm(int mod,NDV p);
1.43 noro 119: NDV dptondv(int mod,DP p);
1.16 noro 120: NDV dpmtondv(int mod,DPM p);
1.43 noro 121: int dp_getdeg(DP p);
1.16 noro 122: int dpm_getdeg(DPM p,int *rank);
123: void dpm_ptozp(DPM p,Z *cont,DPM *r);
124: int compdmm(int nv,DMM a,DMM b);
1.44 noro 125: DPM sigtodpm(SIG s);
126: SIG dup_sig(SIG sig);
1.1 noro 127:
128: void Pdp_set_weight(NODE,VECT *);
129: void Pox_cmo_rpc(NODE,Obj *);
130:
131: ND nd_add_lf(ND p1,ND p2);
132: void nd_mul_c_lf(ND p,Z mul);
133: void ndv_mul_c_lf(NDV p,Z mul);
134: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
135: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
136: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
137: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
138: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
139: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
140: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 141: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig);
1.1 noro 142: NODE nd_f4_lf_trace_main(int m,int **indp);
143: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
144:
145: extern int lf_lazy;
146: extern Z current_mod_lf;
147:
148: extern int Denominator,DP_Multiple,MaxDeg;
149:
150: #define BLEN (8*sizeof(unsigned long))
151:
152: typedef struct matrix {
153: int row,col;
154: unsigned long **a;
155: } *matrix;
156:
157:
158: void nd_free_private_storage()
159: {
160: _nm_free_list = 0;
161: _ndp_free_list = 0;
162: #if 0
163: GC_gcollect();
164: #endif
165: }
166:
167: void _NM_alloc()
168: {
169: NM p;
170: int i;
171:
172: for ( i = 0; i < 1024; i++ ) {
173: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
174: p->next = _nm_free_list; _nm_free_list = p;
175: }
176: }
177:
178: matrix alloc_matrix(int row,int col)
179: {
180: unsigned long **a;
181: int i,len,blen;
182: matrix mat;
183:
184: mat = (matrix)MALLOC(sizeof(struct matrix));
185: mat->row = row;
186: mat->col = col;
187: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
188: return mat;
189: }
190:
191:
192: void _ND_alloc()
193: {
194: ND p;
195: int i;
196:
197: for ( i = 0; i < 1024; i++ ) {
198: p = (ND)MALLOC(sizeof(struct oND));
199: p->body = (NM)_nd_free_list; _nd_free_list = p;
200: }
201: }
202:
203: void _NDP_alloc()
204: {
205: ND_pairs p;
206: int i;
207:
208: for ( i = 0; i < 1024; i++ ) {
209: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
210: +(nd_wpd-1)*sizeof(UINT));
211: p->next = _ndp_free_list; _ndp_free_list = p;
212: }
213: }
214:
215: INLINE int nd_length(ND p)
216: {
217: NM m;
218: int i;
219:
220: if ( !p )
221: return 0;
222: else {
223: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
224: return i;
225: }
226: }
227:
228: extern int dp_negative_weight;
229:
230: INLINE int ndl_reducible(UINT *d1,UINT *d2)
231: {
232: UINT u1,u2;
233: int i,j;
234:
235: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
236:
237: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
238: #if USE_UNROLL
239: switch ( nd_bpe ) {
240: case 3:
241: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
242: u1 = d1[i]; u2 = d2[i];
243: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
244: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
245: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
246: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
247: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
248: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
249: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
250: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
251: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
252: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
253: }
254: return 1;
255: break;
256: case 4:
257: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
258: u1 = d1[i]; u2 = d2[i];
259: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
260: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
261: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
262: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
263: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
264: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
265: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
266: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
267: }
268: return 1;
269: break;
270: case 6:
271: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
272: u1 = d1[i]; u2 = d2[i];
273: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
274: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
275: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
276: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
277: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
278: }
279: return 1;
280: break;
281: case 8:
282: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
283: u1 = d1[i]; u2 = d2[i];
284: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
285: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
286: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
287: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
288: }
289: return 1;
290: break;
291: case 16:
292: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
293: u1 = d1[i]; u2 = d2[i];
294: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
295: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
296: }
297: return 1;
298: break;
299: case 32:
300: for ( i = nd_exporigin; i < nd_wpd; i++ )
301: if ( d1[i] < d2[i] ) return 0;
302: return 1;
303: break;
304: default:
305: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
306: u1 = d1[i]; u2 = d2[i];
307: for ( j = 0; j < nd_epw; j++ )
308: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
309: }
310: return 1;
311: }
312: #else
313: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
314: u1 = d1[i]; u2 = d2[i];
315: for ( j = 0; j < nd_epw; j++ )
316: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
317: }
318: return 1;
319: #endif
320: }
321:
322: /*
323: * If the current order is a block order,
324: * then the last block is length 1 and contains
325: * the homo variable. Otherwise, the original
326: * order is either 0 or 2.
327: */
328:
329: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
330: {
331: int w,i,e,n,omask0;
332:
333: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
334: n = nd_nvar-1;
335: ndl_zero(r);
336: for ( i = 0; i < n; i++ ) {
337: e = GET_EXP_OLD(d,i);
338: PUT_EXP(r,i,e);
339: }
340: w = TD(d);
341: PUT_EXP(r,nd_nvar-1,weight-w);
342: if ( nd_module ) MPOS(r) = d[ompos];
343: TD(r) = weight;
344: if ( nd_blockmask ) ndl_weight_mask(r);
345: }
346:
347: void ndl_dehomogenize(UINT *d)
348: {
349: UINT mask;
350: UINT h;
351: int i,bits;
352:
353: if ( nd_blockmask ) {
354: h = GET_EXP(d,nd_nvar-1);
355: XOR_EXP(d,nd_nvar-1,h);
356: TD(d) -= h;
357: ndl_weight_mask(d);
358: } else {
359: if ( nd_isrlex ) {
360: if ( nd_bpe == 32 ) {
361: h = d[nd_exporigin];
362: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
363: d[i-1] = d[i];
364: d[i-1] = 0;
365: TD(d) -= h;
366: } else {
367: bits = nd_epw*nd_bpe;
368: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
369: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
370: for ( i = nd_exporigin; i < nd_wpd; i++ )
371: d[i] = ((d[i]<<nd_bpe)&mask)
372: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
373: TD(d) -= h;
374: }
375: } else {
376: h = GET_EXP(d,nd_nvar-1);
377: XOR_EXP(d,nd_nvar-1,h);
378: TD(d) -= h;
379: }
380: }
381: }
382:
383: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
384: {
385: UINT t1,t2,u,u1,u2;
386: int i,j,l;
387:
388: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
389: error("ndl_lcm : inconsistent monomials");
390: #if USE_UNROLL
391: switch ( nd_bpe ) {
392: case 3:
393: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
394: u1 = d1[i]; u2 = d2[i];
395: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
396: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
397: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
398: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
400: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
401: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
403: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
404: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
405: d[i] = u;
406: }
407: break;
408: case 4:
409: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
410: u1 = d1[i]; u2 = d2[i];
411: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
412: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
414: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
415: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
416: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
417: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
418: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
419: d[i] = u;
420: }
421: break;
422: case 6:
423: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
424: u1 = d1[i]; u2 = d2[i];
425: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
426: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
427: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
428: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
429: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
430: d[i] = u;
431: }
432: break;
433: case 8:
434: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
435: u1 = d1[i]; u2 = d2[i];
436: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
437: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
438: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
439: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
440: d[i] = u;
441: }
442: break;
443: case 16:
444: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
445: u1 = d1[i]; u2 = d2[i];
446: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
447: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
448: d[i] = u;
449: }
450: break;
451: case 32:
452: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
453: u1 = d1[i]; u2 = d2[i];
454: d[i] = u1>u2?u1:u2;
455: }
456: break;
457: default:
458: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
459: u1 = d1[i]; u2 = d2[i];
460: for ( j = 0, u = 0; j < nd_epw; j++ ) {
461: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
462: }
463: d[i] = u;
464: }
465: break;
466: }
467: #else
468: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
469: u1 = d1[i]; u2 = d2[i];
470: for ( j = 0, u = 0; j < nd_epw; j++ ) {
471: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
472: }
473: d[i] = u;
474: }
475: #endif
476: if ( nd_module ) MPOS(d) = MPOS(d1);
477: TD(d) = ndl_weight(d);
478: if ( nd_blockmask ) ndl_weight_mask(d);
479: }
480:
481: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
482: {
483: UINT t1,t2,u,u1,u2;
484: int i,j,l;
485:
486: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
487: u1 = d1[i]; u2 = d2[i];
488: for ( j = 0, u = 0; j < nd_epw; j++ ) {
489: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
490: }
491: d[i] = u;
492: }
493: }
494:
495: int ndl_weight(UINT *d)
496: {
497: UINT t,u;
498: int i,j;
499:
500: if ( current_dl_weight_vector )
501: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
502: u = GET_EXP(d,i);
503: t += MUL_WEIGHT(u,i);
504: }
505: else
506: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
507: u = d[i];
508: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
509: t += (u&nd_mask0);
510: }
1.20 noro 511: if ( nd_module && nd_module_rank && MPOS(d) )
512: t += nd_module_weight[MPOS(d)-1];
513: for ( i = nd_exporigin; i < nd_wpd; i++ )
514: if ( d[i] && !t )
515: printf("afo\n");
1.1 noro 516: return t;
517: }
518:
519: /* for sugarweight */
520:
521: int ndl_weight2(UINT *d)
522: {
523: int t,u;
524: int i,j;
525:
526: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
527: u = GET_EXP(d,i);
528: t += nd_sugarweight[i]*u;
529: }
1.20 noro 530: if ( nd_module && nd_module_rank && MPOS(d) )
531: t += nd_module_weight[MPOS(d)-1];
1.1 noro 532: return t;
533: }
534:
535: void ndl_weight_mask(UINT *d)
536: {
537: UINT t,u;
538: UINT *mask;
539: int i,j,k,l;
540:
541: l = nd_blockmask->n;
542: for ( k = 0; k < l; k++ ) {
543: mask = nd_blockmask->mask[k];
544: if ( current_dl_weight_vector )
545: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
546: u = GET_EXP_MASK(d,i,mask);
547: t += MUL_WEIGHT(u,i);
548: }
549: else
550: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
551: u = d[i]&mask[i];
552: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
553: t += (u&nd_mask0);
554: }
555: d[k+1] = t;
556: }
557: }
558:
1.21 noro 559: int ndl_glex_compare(UINT *d1,UINT *d2)
560: {
561: if ( TD(d1) > TD(d2) ) return 1;
562: else if ( TD(d1) < TD(d2) ) return -1;
563: else return ndl_lex_compare(d1,d2);
564: }
565:
1.1 noro 566: int ndl_lex_compare(UINT *d1,UINT *d2)
567: {
568: int i;
569:
570: d1 += nd_exporigin;
571: d2 += nd_exporigin;
572: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
573: if ( *d1 > *d2 )
574: return nd_isrlex ? -1 : 1;
575: else if ( *d1 < *d2 )
576: return nd_isrlex ? 1 : -1;
577: return 0;
578: }
579:
580: int ndl_block_compare(UINT *d1,UINT *d2)
581: {
582: int i,l,j,ord_o,ord_l;
583: struct order_pair *op;
584: UINT t1,t2,m;
585: UINT *mask;
586:
587: l = nd_blockmask->n;
588: op = nd_blockmask->order_pair;
589: for ( j = 0; j < l; j++ ) {
590: mask = nd_blockmask->mask[j];
591: ord_o = op[j].order;
592: if ( ord_o < 2 ) {
593: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
594: else if ( t1 < t2 ) return -1;
595: }
596: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
597: m = mask[i];
598: t1 = d1[i]&m;
599: t2 = d2[i]&m;
600: if ( t1 > t2 )
601: return !ord_o ? -1 : 1;
602: else if ( t1 < t2 )
603: return !ord_o ? 1 : -1;
604: }
605: }
606: return 0;
607: }
608:
609: int ndl_matrix_compare(UINT *d1,UINT *d2)
610: {
611: int i,j,s,row;
612: int *v;
613: Z **mat;
614: Z *w;
615: Z t1;
616: Z t,t2;
617:
1.6 noro 618: for ( j = 0; j < nd_nvar; j++ )
619: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 620: if ( nd_top_weight ) {
621: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 622: mat = (Z **)&BDY((VECT)nd_top_weight);
623: row = 1;
1.1 noro 624: } else {
625: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 626: row = ((MAT)nd_top_weight)->row;
1.1 noro 627: }
628: for ( i = 0; i < row; i++ ) {
1.6 noro 629: w = mat[i];
1.1 noro 630: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 631: STOZ(nd_work_vector[j],t1);
1.1 noro 632: mulz(w[j],t1,&t2);
633: addz(t,t2,&t1);
634: t = t1;
635: }
1.6 noro 636: if ( t ) {
637: s = sgnz(t);
1.1 noro 638: if ( s > 0 ) return 1;
639: else if ( s < 0 ) return -1;
1.6 noro 640: }
1.1 noro 641: }
1.6 noro 642: }
643: for ( i = 0; i < nd_matrix_len; i++ ) {
644: v = nd_matrix[i];
645: for ( j = 0, s = 0; j < nd_nvar; j++ )
646: s += v[j]*nd_work_vector[j];
647: if ( s > 0 ) return 1;
648: else if ( s < 0 ) return -1;
649: }
1.1 noro 650: if ( !ndl_equal(d1,d2) )
1.6 noro 651: error("ndl_matrix_compare : invalid matrix");
652: return 0;
1.1 noro 653: }
654:
655: int ndl_composite_compare(UINT *d1,UINT *d2)
656: {
657: int i,j,s,start,end,len,o;
658: int *v;
659: struct sparse_weight *sw;
660:
661: for ( j = 0; j < nd_nvar; j++ )
662: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
663: for ( i = 0; i < nd_worb_len; i++ ) {
664: len = nd_worb[i].length;
665: switch ( nd_worb[i].type ) {
666: case IS_DENSE_WEIGHT:
667: v = nd_worb[i].body.dense_weight;
668: for ( j = 0, s = 0; j < len; j++ )
669: s += v[j]*nd_work_vector[j];
670: if ( s > 0 ) return 1;
671: else if ( s < 0 ) return -1;
672: break;
673: case IS_SPARSE_WEIGHT:
674: sw = nd_worb[i].body.sparse_weight;
675: for ( j = 0, s = 0; j < len; j++ )
676: s += sw[j].value*nd_work_vector[sw[j].pos];
677: if ( s > 0 ) return 1;
678: else if ( s < 0 ) return -1;
679: break;
680: case IS_BLOCK:
681: o = nd_worb[i].body.block.order;
682: start = nd_worb[i].body.block.start;
683: switch ( o ) {
684: case 0:
685: end = start+len;
686: for ( j = start, s = 0; j < end; j++ )
687: s += MUL_WEIGHT(nd_work_vector[j],j);
688: if ( s > 0 ) return 1;
689: else if ( s < 0 ) return -1;
690: for ( j = end-1; j >= start; j-- )
691: if ( nd_work_vector[j] < 0 ) return 1;
692: else if ( nd_work_vector[j] > 0 ) return -1;
693: break;
694: case 1:
695: end = start+len;
696: for ( j = start, s = 0; j < end; j++ )
697: s += MUL_WEIGHT(nd_work_vector[j],j);
698: if ( s > 0 ) return 1;
699: else if ( s < 0 ) return -1;
700: for ( j = start; j < end; j++ )
701: if ( nd_work_vector[j] > 0 ) return 1;
702: else if ( nd_work_vector[j] < 0 ) return -1;
703: break;
704: case 2:
705: end = start+len;
706: for ( j = start; j < end; j++ )
707: if ( nd_work_vector[j] > 0 ) return 1;
708: else if ( nd_work_vector[j] < 0 ) return -1;
709: break;
710: }
711: break;
712: }
713: }
714: return 0;
715: }
716:
717: /* TDH -> WW -> TD-> RL */
718:
719: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
720: {
721: int i,m,e1,e2;
722:
723: if ( TD(d1) > TD(d2) ) return 1;
724: else if ( TD(d1) < TD(d2) ) return -1;
725: m = nd_nvar>>1;
726: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
727: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
728: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
729: }
730: if ( e1 > e2 ) return 1;
731: else if ( e1 < e2 ) return -1;
732: return ndl_lex_compare(d1,d2);
733: }
734:
1.21 noro 735: // common function for module glex and grlex comparison
736: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 737: {
1.21 noro 738: int c;
1.1 noro 739:
1.21 noro 740: switch ( nd_module_ordtype ) {
741: case 0:
742: if ( TD(d1) > TD(d2) ) return 1;
743: else if ( TD(d1) < TD(d2) ) return -1;
744: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
745: else if ( MPOS(d1) < MPOS(d2) ) return 1;
746: else if ( MPOS(d1) > MPOS(d2) ) return -1;
747: else return 0;
748: break;
1.1 noro 749:
1.21 noro 750: case 1:
1.19 noro 751: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
752: if ( TD(d1) > TD(d2) ) return 1;
753: else if ( TD(d1) < TD(d2) ) return -1;
754: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
755: if ( MPOS(d1) < MPOS(d2) ) return 1;
756: else if ( MPOS(d1) > MPOS(d2) ) return -1;
757: }
758: if ( MPOS(d1) < MPOS(d2) ) return 1;
759: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 760: else if ( TD(d1) > TD(d2) ) return 1;
761: else if ( TD(d1) < TD(d2) ) return -1;
762: else return ndl_lex_compare(d1,d2);
763: break;
1.1 noro 764:
1.21 noro 765: case 2: // weight -> POT
766: if ( TD(d1) > TD(d2) ) return 1;
767: else if ( TD(d1) < TD(d2) ) return -1;
768: else if ( MPOS(d1) < MPOS(d2) ) return 1;
769: else if ( MPOS(d1) > MPOS(d2) ) return -1;
770: else return ndl_lex_compare(d1,d2);
771: break;
1.1 noro 772:
1.21 noro 773: default:
774: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 775: return 0;
1.21 noro 776: }
1.1 noro 777: }
778:
1.21 noro 779: // common for module comparison
780: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 781: {
1.21 noro 782: int c;
1.1 noro 783:
1.21 noro 784: switch ( nd_module_ordtype ) {
785: case 0:
1.23 noro 786: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 787: else if ( MPOS(d1) > MPOS(d2) ) return -1;
788: else if ( MPOS(d1) < MPOS(d2) ) return 1;
789: else return 0;
790: break;
1.1 noro 791:
1.21 noro 792: case 1:
793: if ( MPOS(d1) < MPOS(d2) ) return 1;
794: else if ( MPOS(d1) > MPOS(d2) ) return -1;
795: else return (*ndl_base_compare_function)(d1,d2);
796: break;
1.1 noro 797:
1.21 noro 798: case 2: // weight -> POT
799: if ( TD(d1) > TD(d2) ) return 1;
800: else if ( TD(d1) < TD(d2) ) return -1;
801: else if ( MPOS(d1) < MPOS(d2) ) return 1;
802: else if ( MPOS(d1) > MPOS(d2) ) return -1;
803: else return (*ndl_base_compare_function)(d1,d2);
804: break;
1.1 noro 805:
1.21 noro 806: default:
807: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 808: return 0;
1.21 noro 809: }
1.1 noro 810: }
811:
1.21 noro 812: extern DMMstack dmm_stack;
813: void _addtodl(int n,DL d1,DL d2);
1.31 noro 814: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 815: int _eqdl(int n,DL d1,DL d2);
816:
817: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
818: {
1.44 noro 819: int pos1,pos2,t,j,retpot;
1.21 noro 820: DMM *in;
821: DMMstack s;
822: static DL d1=0;
823: static DL d2=0;
824: static int dlen=0;
1.44 noro 825: extern int ReversePOT;
1.21 noro 826:
1.44 noro 827: if ( ReversePOT ) retpot = -1;
828: else retpot = 1;
1.21 noro 829: pos1 = MPOS(m1); pos2 = MPOS(m2);
830: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
831: if ( nd_nvar > dlen ) {
832: NEWDL(d1,nd_nvar);
833: NEWDL(d2,nd_nvar);
834: dlen = nd_nvar;
835: }
836: d1->td = TD(m1);
837: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
838: d2->td = TD(m2);
839: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
840: for ( s = dmm_stack; s; s = NEXT(s) ) {
841: in = s->in;
842: _addtodl(nd_nvar,in[pos1]->dl,d1);
843: _addtodl(nd_nvar,in[pos2]->dl,d2);
844: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44 noro 845: if ( pos1 < pos2 ) return retpot;
846: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 847: else return 0;
848: }
849: pos1 = in[pos1]->pos;
850: pos2 = in[pos2]->pos;
851: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
852: }
853: // comparison by the bottom order
854: LAST:
855: switch ( nd_base_ordtype ) {
856: case 0:
857: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
858: if ( t ) return t;
1.44 noro 859: else if ( pos1 < pos2 ) return retpot;
860: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 861: else return 0;
862: break;
863: case 1:
1.44 noro 864: if ( pos1 < pos2 ) return retpot;
865: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 866: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
867: break;
868: case 2:
869: if ( d1->td > d2->td ) return 1;
870: else if ( d1->td < d2->td ) return -1;
1.44 noro 871: else if ( pos1 < pos2 ) return retpot;
872: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 873: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
874: break;
875: default:
876: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 877: return 0;
1.21 noro 878: }
1.1 noro 879: }
880:
881: INLINE int ndl_equal(UINT *d1,UINT *d2)
882: {
883: int i;
884:
885: switch ( nd_wpd ) {
886: case 2:
887: if ( TD(d2) != TD(d1) ) return 0;
888: if ( d2[1] != d1[1] ) return 0;
889: return 1;
890: break;
891: case 3:
892: if ( TD(d2) != TD(d1) ) return 0;
893: if ( d2[1] != d1[1] ) return 0;
894: if ( d2[2] != d1[2] ) return 0;
895: return 1;
896: break;
897: default:
898: for ( i = 0; i < nd_wpd; i++ )
899: if ( *d1++ != *d2++ ) return 0;
900: return 1;
901: break;
902: }
903: }
904:
905: INLINE void ndl_copy(UINT *d1,UINT *d2)
906: {
907: int i;
908:
909: switch ( nd_wpd ) {
910: case 2:
911: TD(d2) = TD(d1);
912: d2[1] = d1[1];
913: break;
914: case 3:
915: TD(d2) = TD(d1);
916: d2[1] = d1[1];
917: d2[2] = d1[2];
918: break;
919: default:
920: for ( i = 0; i < nd_wpd; i++ )
921: d2[i] = d1[i];
922: break;
923: }
924: }
925:
926: INLINE void ndl_zero(UINT *d)
927: {
928: int i;
929: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
930: }
931:
932: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
933: {
934: int i;
935:
936: if ( nd_module ) {
937: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
938: error("ndl_add : invalid operation");
939: }
940: #if 1
941: switch ( nd_wpd ) {
942: case 2:
943: TD(d) = TD(d1)+TD(d2);
944: d[1] = d1[1]+d2[1];
945: break;
946: case 3:
947: TD(d) = TD(d1)+TD(d2);
948: d[1] = d1[1]+d2[1];
949: d[2] = d1[2]+d2[2];
950: break;
951: default:
952: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
953: break;
954: }
955: #else
956: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
957: #endif
958: }
959:
960: /* d1 += d2 */
961: INLINE void ndl_addto(UINT *d1,UINT *d2)
962: {
963: int i;
964:
965: if ( nd_module ) {
966: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
967: error("ndl_addto : invalid operation");
968: }
969: #if 1
970: switch ( nd_wpd ) {
971: case 2:
972: TD(d1) += TD(d2);
973: d1[1] += d2[1];
974: break;
975: case 3:
976: TD(d1) += TD(d2);
977: d1[1] += d2[1];
978: d1[2] += d2[2];
979: break;
980: default:
981: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
982: break;
983: }
984: #else
985: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
986: #endif
987: }
988:
989: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
990: {
991: int i;
992:
993: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
994: }
995:
996: int ndl_disjoint(UINT *d1,UINT *d2)
997: {
998: UINT t1,t2,u,u1,u2;
999: int i,j;
1000:
1001: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1002: #if USE_UNROLL
1003: switch ( nd_bpe ) {
1004: case 3:
1005: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1006: u1 = d1[i]; u2 = d2[i];
1007: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
1015: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
1016: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
1017: }
1018: return 1;
1019: break;
1020: case 4:
1021: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1022: u1 = d1[i]; u2 = d2[i];
1023: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1027: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1028: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1029: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1030: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1031: }
1032: return 1;
1033: break;
1034: case 6:
1035: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1036: u1 = d1[i]; u2 = d2[i];
1037: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1038: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1039: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1040: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1041: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1042: }
1043: return 1;
1044: break;
1045: case 8:
1046: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1047: u1 = d1[i]; u2 = d2[i];
1048: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1049: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1050: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1051: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1052: }
1053: return 1;
1054: break;
1055: case 16:
1056: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1057: u1 = d1[i]; u2 = d2[i];
1058: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1059: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1060: }
1061: return 1;
1062: break;
1063: case 32:
1064: for ( i = nd_exporigin; i < nd_wpd; i++ )
1065: if ( d1[i] && d2[i] ) return 0;
1066: return 1;
1067: break;
1068: default:
1069: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1070: u1 = d1[i]; u2 = d2[i];
1071: for ( j = 0; j < nd_epw; j++ ) {
1072: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1073: u1 >>= nd_bpe; u2 >>= nd_bpe;
1074: }
1075: }
1076: return 1;
1077: break;
1078: }
1079: #else
1080: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1081: u1 = d1[i]; u2 = d2[i];
1082: for ( j = 0; j < nd_epw; j++ ) {
1083: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1084: u1 >>= nd_bpe; u2 >>= nd_bpe;
1085: }
1086: }
1087: return 1;
1088: #endif
1089: }
1090:
1091: int ndl_check_bound(UINT *d1,UINT *d2)
1092: {
1093: UINT u2;
1094: int i,j,ind,k;
1095:
1096: ind = 0;
1097: #if USE_UNROLL
1098: switch ( nd_bpe ) {
1099: case 3:
1100: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1101: u2 = d2[i];
1102: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1103: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1104: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1105: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1106: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1107: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1108: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1109: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1110: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1111: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1112: }
1113: return 0;
1114: break;
1115: case 4:
1116: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1117: u2 = d2[i];
1118: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1119: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1120: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1121: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1122: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1123: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1124: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1125: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1126: }
1127: return 0;
1128: break;
1129: case 6:
1130: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1131: u2 = d2[i];
1132: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1133: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1134: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1135: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1136: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1137: }
1138: return 0;
1139: break;
1140: case 8:
1141: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1142: u2 = d2[i];
1143: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1144: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1145: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1146: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1147: }
1148: return 0;
1149: break;
1150: case 16:
1151: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1152: u2 = d2[i];
1153: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1154: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1155: }
1156: return 0;
1157: break;
1158: case 32:
1159: for ( i = nd_exporigin; i < nd_wpd; i++ )
1160: if ( d1[i]+d2[i]<d1[i] ) return 1;
1161: return 0;
1162: break;
1163: default:
1164: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1165: u2 = d2[i];
1166: k = (nd_epw-1)*nd_bpe;
1167: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1168: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1169: }
1170: return 0;
1171: break;
1172: }
1173: #else
1174: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1175: u2 = d2[i];
1176: k = (nd_epw-1)*nd_bpe;
1177: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1178: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1179: }
1180: return 0;
1181: #endif
1182: }
1183:
1184: int ndl_check_bound2(int index,UINT *d2)
1185: {
1186: return ndl_check_bound(nd_bound[index],d2);
1187: }
1188:
1189: INLINE int ndl_hash_value(UINT *d)
1190: {
1191: int i;
1.11 noro 1192: UINT r;
1.1 noro 1193:
1194: r = 0;
1195: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1196: r = (r*1511+d[i]);
1.11 noro 1197: r %= REDTAB_LEN;
1.1 noro 1198: return r;
1199: }
1200:
1201: INLINE int ndl_find_reducer(UINT *dg)
1202: {
1203: RHist r;
1204: int d,k,i;
1205:
1206: d = ndl_hash_value(dg);
1207: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1208: if ( ndl_equal(dg,DL(r)) ) {
1209: if ( k > 0 ) nd_notfirst++;
1210: nd_found++;
1211: return r->index;
1212: }
1213: }
1214: if ( Reverse )
1215: for ( i = nd_psn-1; i >= 0; i-- ) {
1216: r = nd_psh[i];
1217: if ( ndl_reducible(dg,DL(r)) ) {
1218: nd_create++;
1219: nd_append_red(dg,i);
1220: return i;
1221: }
1222: }
1223: else
1224: for ( i = 0; i < nd_psn; i++ ) {
1225: r = nd_psh[i];
1226: if ( ndl_reducible(dg,DL(r)) ) {
1227: nd_create++;
1228: nd_append_red(dg,i);
1229: return i;
1230: }
1231: }
1232: return -1;
1233: }
1234:
1.41 noro 1235: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1236: {
1237: RHist r;
1238: int i;
1239:
1240: for ( i = 0; i < nd_psn; i++ ) {
1241: r = nd_psh[i];
1242: if ( ndl_reducible(dg,DL(r)) ) return i;
1243: }
1244: return -1;
1245: }
1246:
1.24 noro 1247: // ret=0,...,nd_psn-1 => reducer found
1248: // ret=nd_psn => reducer not found
1249: // ret=-1 => singular top reducible
1250:
1251: int comp_sig(SIG s1,SIG s2);
1252: void _ndltodl(UINT *ndl,DL dl);
1253:
1254: void print_sig(SIG s)
1255: {
1256: int i;
1257:
1258: fprintf(asir_out,"<<");
1259: for ( i = 0; i < nd_nvar; i++ ) {
1260: fprintf(asir_out,"%d",s->dl->d[i]);
1261: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1262: }
1263: fprintf(asir_out,">>*e%d",s->pos);
1264: }
1265:
1.45 noro 1266: void print_siglist(NODE l)
1267: {
1268: for ( ; l; l = NEXT(l) )
1269: print_sig((SIG)l->body);
1270: }
1271:
1272:
1.35 noro 1273: // assuming increasing order wrt signature
1274:
1.24 noro 1275: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1276: {
1277: RHist r;
1.27 noro 1278: int i,singular,ret,d,k;
1.26 noro 1279: static int wpd,nvar;
1.24 noro 1280: static SIG quo;
1281: static UINT *tmp;
1282:
1.26 noro 1283: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1284: if ( wpd != nd_wpd ) {
1.24 noro 1285: wpd = nd_wpd;
1286: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1287: }
1.27 noro 1288: d = ndl_hash_value(dg);
1.35 noro 1289: #if 1
1.27 noro 1290: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1291: if ( ndl_equal(dg,DL(r)) ) {
1292: return r->index;
1293: }
1294: }
1.35 noro 1295: #endif
1.24 noro 1296: singular = 0;
1297: for ( i = 0; i < nd_psn; i++ ) {
1298: r = nd_psh[i];
1299: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1300: ndl_sub(dg,DL(r),tmp);
1.24 noro 1301: _ndltodl(tmp,DL(quo));
1302: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1303: quo->pos = nd_psh[i]->sig->pos;
1.55 noro 1304: _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.24 noro 1305: ret = comp_sig(sig,quo);
1306: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1307: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1308: }
1309: }
1310: if ( singular ) return -1;
1.27 noro 1311: else if ( i < nd_psn )
1312: nd_append_red(dg,i);
1313: return i;
1.24 noro 1314: }
1315:
1.1 noro 1316: ND nd_merge(ND p1,ND p2)
1317: {
1318: int n,c;
1319: int t,can,td1,td2;
1320: ND r;
1321: NM m1,m2,mr0,mr,s;
1322:
1323: if ( !p1 ) return p2;
1324: else if ( !p2 ) return p1;
1325: else {
1326: can = 0;
1327: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1328: c = DL_COMPARE(DL(m1),DL(m2));
1329: switch ( c ) {
1330: case 0:
1331: s = m1; m1 = NEXT(m1);
1332: can++; NEXTNM2(mr0,mr,s);
1333: s = m2; m2 = NEXT(m2); FREENM(s);
1334: break;
1335: case 1:
1336: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1337: break;
1338: case -1:
1339: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1340: break;
1341: }
1342: }
1343: if ( !mr0 )
1344: if ( m1 ) mr0 = m1;
1345: else if ( m2 ) mr0 = m2;
1346: else return 0;
1347: else if ( m1 ) NEXT(mr) = m1;
1348: else if ( m2 ) NEXT(mr) = m2;
1349: else NEXT(mr) = 0;
1350: BDY(p1) = mr0;
1351: SG(p1) = MAX(SG(p1),SG(p2));
1352: LEN(p1) = LEN(p1)+LEN(p2)-can;
1353: FREEND(p2);
1354: return p1;
1355: }
1356: }
1357:
1358: ND nd_add(int mod,ND p1,ND p2)
1359: {
1360: int n,c;
1361: int t,can,td1,td2;
1362: ND r;
1363: NM m1,m2,mr0,mr,s;
1364:
1.11 noro 1365: Nnd_add++;
1.1 noro 1366: if ( !p1 ) return p2;
1367: else if ( !p2 ) return p1;
1368: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1369: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1370: else if ( !mod ) return nd_add_q(p1,p2);
1371: else {
1372: can = 0;
1373: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1374: c = DL_COMPARE(DL(m1),DL(m2));
1375: switch ( c ) {
1376: case 0:
1377: t = ((CM(m1))+(CM(m2))) - mod;
1378: if ( t < 0 ) t += mod;
1379: s = m1; m1 = NEXT(m1);
1380: if ( t ) {
1381: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1382: } else {
1383: can += 2; FREENM(s);
1384: }
1385: s = m2; m2 = NEXT(m2); FREENM(s);
1386: break;
1387: case 1:
1388: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1389: break;
1390: case -1:
1391: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1392: break;
1393: }
1394: }
1395: if ( !mr0 )
1396: if ( m1 ) mr0 = m1;
1397: else if ( m2 ) mr0 = m2;
1398: else return 0;
1399: else if ( m1 ) NEXT(mr) = m1;
1400: else if ( m2 ) NEXT(mr) = m2;
1401: else NEXT(mr) = 0;
1402: BDY(p1) = mr0;
1403: SG(p1) = MAX(SG(p1),SG(p2));
1404: LEN(p1) = LEN(p1)+LEN(p2)-can;
1405: FREEND(p2);
1406: return p1;
1407: }
1408: }
1409:
1410: /* XXX on opteron, the inlined manipulation of destructive additon of
1411: * two NM seems to make gcc optimizer get confused, so the part is
1412: * done in a function.
1413: */
1414:
1415: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1416: {
1417: NM s;
1418: P t;
1419: int can;
1420:
1421: addp(nd_vc,CP(*m1),CP(*m2),&t);
1422: s = *m1; *m1 = NEXT(*m1);
1423: if ( t ) {
1424: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1425: } else {
1426: can = 2; FREENM(s);
1427: }
1428: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1429: return can;
1430: }
1431:
1432: ND nd_add_q(ND p1,ND p2)
1433: {
1434: int n,c,can;
1435: ND r;
1436: NM m1,m2,mr0,mr,s;
1437: P t;
1438:
1439: if ( !p1 ) return p2;
1440: else if ( !p2 ) return p1;
1441: else {
1442: can = 0;
1443: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1444: c = DL_COMPARE(DL(m1),DL(m2));
1445: switch ( c ) {
1446: case 0:
1447: #if defined(__x86_64__)
1448: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1449: #else
1450: addp(nd_vc,CP(m1),CP(m2),&t);
1451: s = m1; m1 = NEXT(m1);
1452: if ( t ) {
1453: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1454: } else {
1455: can += 2; FREENM(s);
1456: }
1457: s = m2; m2 = NEXT(m2); FREENM(s);
1458: #endif
1459: break;
1460: case 1:
1461: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1462: break;
1463: case -1:
1464: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1465: break;
1466: }
1467: }
1468: if ( !mr0 )
1469: if ( m1 ) mr0 = m1;
1470: else if ( m2 ) mr0 = m2;
1471: else return 0;
1472: else if ( m1 ) NEXT(mr) = m1;
1473: else if ( m2 ) NEXT(mr) = m2;
1474: else NEXT(mr) = 0;
1475: BDY(p1) = mr0;
1476: SG(p1) = MAX(SG(p1),SG(p2));
1477: LEN(p1) = LEN(p1)+LEN(p2)-can;
1478: FREEND(p2);
1479: return p1;
1480: }
1481: }
1482:
1483: ND nd_add_sf(ND p1,ND p2)
1484: {
1485: int n,c,can;
1486: ND r;
1487: NM m1,m2,mr0,mr,s;
1488: int t;
1489:
1490: if ( !p1 ) return p2;
1491: else if ( !p2 ) return p1;
1492: else {
1493: can = 0;
1494: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1495: c = DL_COMPARE(DL(m1),DL(m2));
1496: switch ( c ) {
1497: case 0:
1498: t = _addsf(CM(m1),CM(m2));
1499: s = m1; m1 = NEXT(m1);
1500: if ( t ) {
1501: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1502: } else {
1503: can += 2; FREENM(s);
1504: }
1505: s = m2; m2 = NEXT(m2); FREENM(s);
1506: break;
1507: case 1:
1508: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1509: break;
1510: case -1:
1511: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1512: break;
1513: }
1514: }
1515: if ( !mr0 )
1516: if ( m1 ) mr0 = m1;
1517: else if ( m2 ) mr0 = m2;
1518: else return 0;
1519: else if ( m1 ) NEXT(mr) = m1;
1520: else if ( m2 ) NEXT(mr) = m2;
1521: else NEXT(mr) = 0;
1522: BDY(p1) = mr0;
1523: SG(p1) = MAX(SG(p1),SG(p2));
1524: LEN(p1) = LEN(p1)+LEN(p2)-can;
1525: FREEND(p2);
1526: return p1;
1527: }
1528: }
1529:
1530:
1531: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1532: {
1533: int c,c1,c2;
1534: Z cg,cred,gcd,tq;
1535: P cgp,credp,gcdp;
1536: Obj tr,tr1;
1537:
1538: if ( mod == -1 ) {
1539: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1540: *divp = (Obj)ONE;
1541: } else if ( mod == -2 ) {
1542: Z inv,t;
1543: divlf(ONE,HCZ(p),&inv);
1544: chsgnlf(HCZ(g),&t);
1545: mullf(inv,t,&CZ(mul));
1546: *divp = (Obj)ONE;
1547: } else if ( mod ) {
1548: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1549: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1550: *divp = (Obj)ONE;
1551: } else if ( nd_vc ) {
1552: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1553: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1554: chsgnp(cgp,&CP(mul));
1555: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1556: if ( dn ) {
1557: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1558: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1559: }
1560: *divp = (Obj)credp;
1561: } else {
1.6 noro 1562: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1563: chsgnz(cg,&CZ(mul));
1.1 noro 1564: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1565: if ( dn ) {
1566: mulz(dn->z,cred,&tq); dn->z = tq;
1567: }
1568: *divp = (Obj)cred;
1569: }
1570: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1571: }
1572:
1573: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1574: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1575: {
1576: NM m,mrd,tail;
1577: NM mul;
1578: int n,sugar,psugar,sugar0,stat,index;
1579: int c,c1,c2,dummy;
1580: RHist h;
1581: NDV p,red;
1582: Q cg,cred,gcd,tq,qq;
1583: Z iq;
1584: DP dmul;
1585: NODE node;
1586: LIST hist;
1587: double hmag;
1588: P tp,tp1;
1589: Obj tr,tr1,div;
1590: union oNDC hg;
1591: P cont;
1592:
1593: if ( !g ) {
1594: *rp = d;
1595: return 1;
1596: }
1597: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1598:
1599: sugar0 = sugar = SG(g);
1600: n = NV(g);
1601: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1602: if ( d )
1603: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1604: for ( ; g; ) {
1605: index = ndl_find_reducer(HDL(g));
1606: if ( index >= 0 ) {
1607: h = nd_psh[index];
1608: ndl_sub(HDL(g),DL(h),DL(mul));
1609: if ( ndl_check_bound2(index,DL(mul)) ) {
1610: nd_free(g); nd_free(d);
1611: return 0;
1612: }
1613: p = nd_demand ? ndv_load(index) : ps[index];
1614: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1615: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1616: if ( nd_gentrace ) {
1617: /* Trace=[div,index,mul,ONE] */
1.6 noro 1618: STOZ(index,iq);
1.1 noro 1619: nmtodp(mod,mul,&dmul);
1620: node = mknode(4,div,iq,dmul,ONE);
1621: }
1622: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1623: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1624: hg = HCU(g);
1625: nd_removecont2(d,g);
1.6 noro 1626: if ( nd_gentrace ) {
1.1 noro 1627: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1628: /* exact division */
1.1 noro 1629: cont = ndc_div(mod,hg,HCU(g));
1630: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1631: }
1632: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1633: }
1634: MKLIST(hist,node);
1635: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1636: } else if ( !full ) {
1637: *rp = g;
1638: return 1;
1639: } else {
1640: m = BDY(g);
1641: if ( NEXT(m) ) {
1642: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1643: } else {
1644: FREEND(g); g = 0;
1645: }
1646: if ( d ) {
1647: NEXT(tail)=m; tail=m; LEN(d)++;
1648: } else {
1649: MKND(n,m,1,d); tail = BDY(d);
1650: }
1651: }
1652: }
1653: if ( d ) SG(d) = sugar;
1654: *rp = d;
1655: return 1;
1656: }
1657:
1.24 noro 1658: // ret=1 => success
1659: // ret=0 => overflow
1660: // ret=-1 => singular top reducible
1661:
1662: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1663: {
1664: NM m,mrd,tail;
1665: NM mul;
1666: int n,sugar,psugar,sugar0,stat,index;
1667: int c,c1,c2,dummy;
1668: RHist h;
1669: NDV p,red;
1670: Q cg,cred,gcd,tq,qq;
1671: Z iq;
1672: DP dmul;
1673: NODE node;
1674: LIST hist;
1675: double hmag;
1676: P tp,tp1;
1677: Obj tr,tr1,div;
1678: union oNDC hg;
1679: P cont;
1680: SIG sig;
1681:
1682: if ( !g ) {
1683: *rp = d;
1684: return 1;
1685: }
1686: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1687:
1688: sugar0 = sugar = SG(g);
1689: n = NV(g);
1690: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1691: if ( d )
1692: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1693: sig = g->sig;
1694: for ( ; g; ) {
1695: index = ndl_find_reducer_s(HDL(g),sig);
1696: if ( index >= 0 && index < nd_psn ) {
1697: // reducer found
1698: h = nd_psh[index];
1699: ndl_sub(HDL(g),DL(h),DL(mul));
1700: if ( ndl_check_bound2(index,DL(mul)) ) {
1701: nd_free(g); nd_free(d);
1702: return 0;
1703: }
1704: p = ps[index];
1705: /* d+g -> div*(d+g)+mul*p */
1706: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1707: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1708: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1709: hg = HCU(g);
1710: nd_removecont2(d,g);
1711: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1712: }
1.55 noro 1713: if ( nd_gentrace ) {
1714: /* Trace=[div,index,mul,ONE] */
1715: STOZ(index,iq);
1716: nmtodp(mod,mul,&dmul);
1717: node = mknode(4,div,iq,dmul,ONE);
1718: }
1719: MKLIST(hist,node);
1720: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.24 noro 1721: } else if ( index == -1 ) {
1722: // singular top reducible
1723: return -1;
1724: } else if ( !full ) {
1725: *rp = g;
1726: g->sig = sig;
1727: return 1;
1728: } else {
1729: m = BDY(g);
1730: if ( NEXT(m) ) {
1731: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1732: } else {
1733: FREEND(g); g = 0;
1734: }
1735: if ( d ) {
1736: NEXT(tail)=m; tail=m; LEN(d)++;
1737: } else {
1738: MKND(n,m,1,d); tail = BDY(d);
1739: }
1740: }
1741: }
1742: if ( d ) {
1743: SG(d) = sugar;
1744: d->sig = sig;
1745: }
1746: *rp = d;
1747: return 1;
1748: }
1749:
1.1 noro 1750: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1751: {
1752: int hindex,index;
1753: NDV p;
1754: ND u,d,red;
1755: NODE l;
1756: NM mul,m,mrd,tail;
1757: int sugar,psugar,n,h_reducible;
1758: PGeoBucket bucket;
1759: int c,c1,c2;
1760: Z cg,cred,gcd,zzz;
1761: RHist h;
1762: double hmag,gmag;
1763: int count = 0;
1764: int hcount = 0;
1765:
1766: if ( !g ) {
1767: *rp = 0;
1768: return 1;
1769: }
1770: sugar = SG(g);
1771: n = NV(g);
1.6 noro 1772: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1773: bucket = create_pbucket();
1774: add_pbucket(mod,bucket,g);
1775: d = 0;
1776: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1777: while ( 1 ) {
1778: if ( mod > 0 || mod == -1 )
1779: hindex = head_pbucket(mod,bucket);
1780: else if ( mod == -2 )
1781: hindex = head_pbucket_lf(bucket);
1782: else
1783: hindex = head_pbucket_q(bucket);
1784: if ( hindex < 0 ) {
1785: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1786: if ( d ) SG(d) = sugar;
1787: *rp = d;
1788: return 1;
1789: }
1790: g = bucket->body[hindex];
1791: index = ndl_find_reducer(HDL(g));
1792: if ( index >= 0 ) {
1793: count++;
1794: if ( !d ) hcount++;
1795: h = nd_psh[index];
1796: ndl_sub(HDL(g),DL(h),DL(mul));
1797: if ( ndl_check_bound2(index,DL(mul)) ) {
1798: nd_free(d);
1799: free_pbucket(bucket);
1800: *rp = 0;
1801: return 0;
1802: }
1803: p = ps[index];
1804: if ( mod == -1 )
1805: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1806: else if ( mod == -2 ) {
1807: Z inv,t;
1808: divlf(ONE,HCZ(p),&inv);
1809: chsgnlf(HCZ(g),&t);
1810: mullf(inv,t,&CZ(mul));
1811: } else if ( mod ) {
1812: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1813: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1814: } else {
1.6 noro 1815: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1816: chsgnz(cg,&CZ(mul));
1.1 noro 1817: nd_mul_c_q(d,(P)cred);
1818: mulq_pbucket(bucket,cred);
1819: g = bucket->body[hindex];
1.6 noro 1820: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1821: }
1822: red = ndv_mul_nm(mod,mul,p);
1823: bucket->body[hindex] = nd_remove_head(g);
1824: red = nd_remove_head(red);
1825: add_pbucket(mod,bucket,red);
1826: psugar = SG(p)+TD(DL(mul));
1827: sugar = MAX(sugar,psugar);
1828: if ( !mod && hmag && (gmag > hmag) ) {
1829: g = normalize_pbucket(mod,bucket);
1830: if ( !g ) {
1831: if ( d ) SG(d) = sugar;
1832: *rp = d;
1833: return 1;
1834: }
1835: nd_removecont2(d,g);
1.6 noro 1836: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1837: add_pbucket(mod,bucket,g);
1838: }
1839: } else if ( !full ) {
1840: g = normalize_pbucket(mod,bucket);
1841: if ( g ) SG(g) = sugar;
1842: *rp = g;
1843: return 1;
1844: } else {
1845: m = BDY(g);
1846: if ( NEXT(m) ) {
1847: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1848: } else {
1849: FREEND(g); g = 0;
1850: }
1851: bucket->body[hindex] = g;
1852: NEXT(m) = 0;
1853: if ( d ) {
1854: NEXT(tail)=m; tail=m; LEN(d)++;
1855: } else {
1856: MKND(n,m,1,d); tail = BDY(d);
1857: }
1858: }
1859: }
1860: }
1861:
1.25 noro 1862: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1863: {
1864: int hindex,index;
1865: NDV p;
1866: ND u,d,red;
1867: NODE l;
1868: NM mul,m,mrd,tail;
1869: int sugar,psugar,n,h_reducible;
1870: PGeoBucket bucket;
1871: int c,c1,c2;
1872: Z cg,cred,gcd,zzz;
1873: RHist h;
1874: double hmag,gmag;
1875: int count = 0;
1876: int hcount = 0;
1877: SIG sig;
1878:
1879: if ( !g ) {
1880: *rp = 0;
1881: return 1;
1882: }
1883: sugar = SG(g);
1884: n = NV(g);
1885: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1886: bucket = create_pbucket();
1887: add_pbucket(mod,bucket,g);
1888: d = 0;
1889: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1890: sig = g->sig;
1891: while ( 1 ) {
1892: if ( mod > 0 || mod == -1 )
1893: hindex = head_pbucket(mod,bucket);
1894: else if ( mod == -2 )
1895: hindex = head_pbucket_lf(bucket);
1896: else
1897: hindex = head_pbucket_q(bucket);
1898: if ( hindex < 0 ) {
1899: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1900: if ( d ) {
1901: SG(d) = sugar;
1902: d->sig = sig;
1903: }
1904: *rp = d;
1905: return 1;
1906: }
1907: g = bucket->body[hindex];
1908: index = ndl_find_reducer_s(HDL(g),sig);
1909: if ( index >= 0 && index < nd_psn ) {
1910: count++;
1911: if ( !d ) hcount++;
1912: h = nd_psh[index];
1913: ndl_sub(HDL(g),DL(h),DL(mul));
1914: if ( ndl_check_bound2(index,DL(mul)) ) {
1915: nd_free(d);
1916: free_pbucket(bucket);
1917: *rp = 0;
1918: return 0;
1919: }
1920: p = ps[index];
1921: if ( mod == -1 )
1922: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1923: else if ( mod == -2 ) {
1924: Z inv,t;
1925: divlf(ONE,HCZ(p),&inv);
1926: chsgnlf(HCZ(g),&t);
1927: mullf(inv,t,&CZ(mul));
1928: } else if ( mod ) {
1929: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1930: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1931: } else {
1932: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1933: chsgnz(cg,&CZ(mul));
1934: nd_mul_c_q(d,(P)cred);
1935: mulq_pbucket(bucket,cred);
1936: g = bucket->body[hindex];
1937: gmag = (double)p_mag((P)HCZ(g));
1938: }
1939: red = ndv_mul_nm(mod,mul,p);
1940: bucket->body[hindex] = nd_remove_head(g);
1941: red = nd_remove_head(red);
1942: add_pbucket(mod,bucket,red);
1943: psugar = SG(p)+TD(DL(mul));
1944: sugar = MAX(sugar,psugar);
1945: if ( !mod && hmag && (gmag > hmag) ) {
1946: g = normalize_pbucket(mod,bucket);
1947: if ( !g ) {
1948: if ( d ) {
1949: SG(d) = sugar;
1950: d->sig = sig;
1951: }
1952: *rp = d;
1953: return 1;
1954: }
1955: nd_removecont2(d,g);
1956: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1957: add_pbucket(mod,bucket,g);
1958: }
1959: } else if ( index == -1 ) {
1960: // singular top reducible
1961: return -1;
1962: } else if ( !full ) {
1963: g = normalize_pbucket(mod,bucket);
1964: if ( g ) {
1965: SG(g) = sugar;
1966: g->sig = sig;
1967: }
1968: *rp = g;
1969: return 1;
1970: } else {
1971: m = BDY(g);
1972: if ( NEXT(m) ) {
1973: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1974: } else {
1975: FREEND(g); g = 0;
1976: }
1977: bucket->body[hindex] = g;
1978: NEXT(m) = 0;
1979: if ( d ) {
1980: NEXT(tail)=m; tail=m; LEN(d)++;
1981: } else {
1982: MKND(n,m,1,d); tail = BDY(d);
1983: }
1984: }
1985: }
1986: }
1987:
1.1 noro 1988: /* input : list of NDV, cand : list of NDV */
1989:
1990: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1991: {
1992: int n,i,stat;
1993: ND nf,d;
1994: NDV r;
1995: NODE t,s;
1996: union oNDC dn;
1997: Z q;
1998: LIST list;
1999:
1.24 noro 2000: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 2001: n = length(cand);
2002:
2003: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
2004: /* membercheck : list is a subset of Id(cand) ? */
2005: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
2006: again:
2007: nd_tracelist = 0;
2008: if ( nd_bpe > obpe )
2009: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
2010: else
2011: r = (NDV)BDY(t);
2012: #if 0
2013: // moved to nd_f4_lf_trace()
2014: if ( m == -2 ) ndv_mod(m,r);
2015: #endif
2016: d = ndvtond(m,r);
1.6 noro 2017: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 2018: if ( !stat ) {
2019: nd_reconstruct(0,0);
2020: goto again;
2021: } else if ( nf ) return 0;
2022: if ( nd_gentrace ) {
2023: nd_tracelist = reverse_node(nd_tracelist);
2024: MKLIST(list,nd_tracelist);
1.6 noro 2025: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 2026: MKNODE(s,list,nd_alltracelist);
2027: nd_alltracelist = s; nd_tracelist = 0;
2028: }
2029: if ( DP_Print ) { printf("."); fflush(stdout); }
2030: }
2031: if ( DP_Print ) { printf("\n"); }
2032: return 1;
2033: }
2034:
2035: ND nd_remove_head(ND p)
2036: {
2037: NM m;
2038:
2039: m = BDY(p);
2040: if ( !NEXT(m) ) {
2041: FREEND(p); p = 0;
2042: } else {
2043: BDY(p) = NEXT(m); LEN(p)--;
2044: }
2045: FREENM(m);
2046: return p;
2047: }
2048:
2049: ND nd_separate_head(ND p,ND *head)
2050: {
2051: NM m,m0;
2052: ND r;
2053:
2054: m = BDY(p);
2055: if ( !NEXT(m) ) {
2056: *head = p; p = 0;
2057: } else {
2058: m0 = m;
2059: BDY(p) = NEXT(m); LEN(p)--;
2060: NEXT(m0) = 0;
2061: MKND(NV(p),m0,1,r);
2062: *head = r;
2063: }
2064: return p;
2065: }
2066:
2067: PGeoBucket create_pbucket()
2068: {
2069: PGeoBucket g;
2070:
2071: g = CALLOC(1,sizeof(struct oPGeoBucket));
2072: g->m = -1;
2073: return g;
2074: }
2075:
2076: void free_pbucket(PGeoBucket b) {
2077: int i;
2078:
2079: for ( i = 0; i <= b->m; i++ )
2080: if ( b->body[i] ) {
2081: nd_free(b->body[i]);
2082: b->body[i] = 0;
2083: }
2084: GCFREE(b);
2085: }
2086:
1.39 noro 2087: #if 0
1.1 noro 2088: void add_pbucket_symbolic(PGeoBucket g,ND d)
2089: {
2090: int l,i,k,m;
2091:
2092: if ( !d )
2093: return;
2094: l = LEN(d);
2095: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2096: /* 2^(k-1) < l <= 2^k (=m) */
2097: d = nd_merge(g->body[k],d);
2098: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2099: g->body[k] = 0;
2100: d = nd_merge(g->body[k+1],d);
2101: }
2102: g->body[k] = d;
2103: g->m = MAX(g->m,k);
2104: }
1.39 noro 2105: #else
2106: void add_pbucket_symbolic(PGeoBucket g,ND d)
2107: {
2108: int l,i,k,m,m0;
2109:
2110: if ( !d )
2111: return;
2112: m0 = g->m;
2113: while ( 1 ) {
2114: l = LEN(d);
2115: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2116: /* 2^(k-1) < l <= 2^k (=m) */
2117: if ( g->body[k] == 0 ) {
2118: g->body[k] = d;
2119: m0 = MAX(k,m0);
2120: break;
2121: } else {
2122: d = nd_merge(g->body[k],d);
2123: g->body[k] = 0;
2124: }
2125: }
2126: g->m = m0;
2127: }
2128: #endif
1.1 noro 2129:
1.39 noro 2130: #if 0
1.1 noro 2131: void add_pbucket(int mod,PGeoBucket g,ND d)
2132: {
2133: int l,i,k,m;
2134:
2135: if ( !d )
2136: return;
2137: l = LEN(d);
2138: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2139: /* 2^(k-1) < l <= 2^k (=m) */
2140: d = nd_add(mod,g->body[k],d);
2141: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2142: g->body[k] = 0;
2143: d = nd_add(mod,g->body[k+1],d);
2144: }
2145: g->body[k] = d;
2146: g->m = MAX(g->m,k);
2147: }
1.39 noro 2148: #else
2149: void add_pbucket(int mod,PGeoBucket g,ND d)
2150: {
2151: int l,i,k,m,m0;
2152:
2153: m0 = g->m;
2154: while ( d != 0 ) {
2155: l = LEN(d);
2156: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2157: /* 2^(k-1) < l <= 2^k (=m) */
2158: if ( g->body[k] == 0 ) {
2159: g->body[k] = d;
2160: m0 = MAX(k,m0);
2161: break;
2162: } else {
2163: d = nd_add(mod,g->body[k],d);
2164: g->body[k] = 0;
2165: }
2166: }
2167: g->m = m0;
2168: }
2169: #endif
1.1 noro 2170:
2171: void mulq_pbucket(PGeoBucket g,Z c)
2172: {
2173: int k;
2174:
2175: for ( k = 0; k <= g->m; k++ )
2176: nd_mul_c_q(g->body[k],(P)c);
2177: }
2178:
2179: NM remove_head_pbucket_symbolic(PGeoBucket g)
2180: {
2181: int j,i,k,c;
2182: NM head;
2183:
2184: k = g->m;
2185: j = -1;
2186: for ( i = 0; i <= k; i++ ) {
2187: if ( !g->body[i] ) continue;
2188: if ( j < 0 ) j = i;
2189: else {
2190: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2191: if ( c > 0 )
2192: j = i;
2193: else if ( c == 0 )
2194: g->body[i] = nd_remove_head(g->body[i]);
2195: }
2196: }
2197: if ( j < 0 ) return 0;
2198: else {
2199: head = BDY(g->body[j]);
2200: if ( !NEXT(head) ) {
2201: FREEND(g->body[j]);
2202: g->body[j] = 0;
2203: } else {
2204: BDY(g->body[j]) = NEXT(head);
2205: LEN(g->body[j])--;
2206: }
2207: return head;
2208: }
2209: }
2210:
2211: int head_pbucket(int mod,PGeoBucket g)
2212: {
2213: int j,i,c,k,nv,sum;
2214: UINT *di,*dj;
2215: ND gi,gj;
2216:
2217: k = g->m;
2218: while ( 1 ) {
2219: j = -1;
2220: for ( i = 0; i <= k; i++ ) {
2221: if ( !(gi = g->body[i]) )
2222: continue;
2223: if ( j < 0 ) {
2224: j = i;
2225: gj = g->body[j];
2226: dj = HDL(gj);
2227: sum = HCM(gj);
2228: } else {
2229: c = DL_COMPARE(HDL(gi),dj);
2230: if ( c > 0 ) {
2231: if ( sum ) HCM(gj) = sum;
2232: else g->body[j] = nd_remove_head(gj);
2233: j = i;
2234: gj = g->body[j];
2235: dj = HDL(gj);
2236: sum = HCM(gj);
2237: } else if ( c == 0 ) {
2238: if ( mod == -1 )
2239: sum = _addsf(sum,HCM(gi));
2240: else {
2241: sum = sum+HCM(gi)-mod;
2242: if ( sum < 0 ) sum += mod;
2243: }
2244: g->body[i] = nd_remove_head(gi);
2245: }
2246: }
2247: }
2248: if ( j < 0 ) return -1;
2249: else if ( sum ) {
2250: HCM(gj) = sum;
2251: return j;
2252: } else
2253: g->body[j] = nd_remove_head(gj);
2254: }
2255: }
2256:
2257: int head_pbucket_q(PGeoBucket g)
2258: {
2259: int j,i,c,k,nv;
2260: Z sum,t;
2261: ND gi,gj;
2262:
2263: k = g->m;
2264: while ( 1 ) {
2265: j = -1;
2266: for ( i = 0; i <= k; i++ ) {
2267: if ( !(gi = g->body[i]) ) continue;
2268: if ( j < 0 ) {
2269: j = i;
2270: gj = g->body[j];
1.6 noro 2271: sum = HCZ(gj);
1.1 noro 2272: } else {
2273: nv = NV(gi);
2274: c = DL_COMPARE(HDL(gi),HDL(gj));
2275: if ( c > 0 ) {
1.6 noro 2276: if ( sum ) HCZ(gj) = sum;
1.1 noro 2277: else g->body[j] = nd_remove_head(gj);
2278: j = i;
2279: gj = g->body[j];
1.6 noro 2280: sum = HCZ(gj);
1.1 noro 2281: } else if ( c == 0 ) {
1.6 noro 2282: addz(sum,HCZ(gi),&t);
1.1 noro 2283: sum = t;
2284: g->body[i] = nd_remove_head(gi);
2285: }
2286: }
2287: }
2288: if ( j < 0 ) return -1;
2289: else if ( sum ) {
1.6 noro 2290: HCZ(gj) = sum;
1.1 noro 2291: return j;
2292: } else
2293: g->body[j] = nd_remove_head(gj);
2294: }
2295: }
2296:
2297: int head_pbucket_lf(PGeoBucket g)
2298: {
2299: int j,i,c,k,nv;
2300: Z sum,t;
2301: ND gi,gj;
2302:
2303: k = g->m;
2304: while ( 1 ) {
2305: j = -1;
2306: for ( i = 0; i <= k; i++ ) {
2307: if ( !(gi = g->body[i]) ) continue;
2308: if ( j < 0 ) {
2309: j = i;
2310: gj = g->body[j];
2311: sum = HCZ(gj);
2312: } else {
2313: nv = NV(gi);
2314: c = DL_COMPARE(HDL(gi),HDL(gj));
2315: if ( c > 0 ) {
2316: if ( sum ) HCZ(gj) = sum;
2317: else g->body[j] = nd_remove_head(gj);
2318: j = i;
2319: gj = g->body[j];
2320: sum = HCZ(gj);
2321: } else if ( c == 0 ) {
2322: addlf(sum,HCZ(gi),&t);
2323: sum = t;
2324: g->body[i] = nd_remove_head(gi);
2325: }
2326: }
2327: }
2328: if ( j < 0 ) return -1;
2329: else if ( sum ) {
2330: HCZ(gj) = sum;
2331: return j;
2332: } else
2333: g->body[j] = nd_remove_head(gj);
2334: }
2335: }
2336:
2337: ND normalize_pbucket(int mod,PGeoBucket g)
2338: {
2339: int i;
2340: ND r,t;
2341:
2342: r = 0;
2343: for ( i = 0; i <= g->m; i++ ) {
2344: r = nd_add(mod,r,g->body[i]);
2345: g->body[i] = 0;
2346: }
2347: g->m = -1;
2348: return r;
2349: }
2350:
2351: #if 0
2352: void register_hcf(NDV p)
2353: {
2354: DCP dc,t;
2355: P hc,h;
2356: int c;
2357: NODE l,l1,prev;
2358:
2359: hc = p->body->c.p;
2360: if ( !nd_vc || NUM(hc) ) return;
2361: fctrp(nd_vc,hc,&dc);
2362: for ( t = dc; t; t = NEXT(t) ) {
2363: h = t->c;
2364: if ( NUM(h) ) continue;
2365: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2366: c = compp(nd_vc,h,(P)BDY(l));
2367: if ( c >= 0 ) break;
2368: }
2369: if ( !l || c > 0 ) {
2370: MKNODE(l1,h,l);
2371: if ( !prev )
2372: nd_hcf = l1;
2373: else
2374: NEXT(prev) = l1;
2375: }
2376: }
2377: }
2378: #else
2379: void register_hcf(NDV p)
2380: {
2381: DCP dc,t;
2382: P hc,h,q;
2383: Q dmy;
2384: int c;
2385: NODE l,l1,prev;
2386:
2387: hc = p->body->c.p;
2388: if ( NUM(hc) ) return;
2389: ptozp(hc,1,&dmy,&h);
2390: #if 1
2391: for ( l = nd_hcf; l; l = NEXT(l) ) {
2392: while ( 1 ) {
2393: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2394: else break;
2395: }
2396: }
2397: if ( NUM(h) ) return;
2398: #endif
2399: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2400: c = compp(nd_vc,h,(P)BDY(l));
2401: if ( c >= 0 ) break;
2402: }
2403: if ( !l || c > 0 ) {
2404: MKNODE(l1,h,l);
2405: if ( !prev )
2406: nd_hcf = l1;
2407: else
2408: NEXT(prev) = l1;
2409: }
2410: }
2411: #endif
2412:
2413: int do_diagonalize(int sugar,int m)
2414: {
1.6 noro 2415: int i,nh,stat;
2416: NODE r,g,t;
2417: ND h,nf,s,head;
2418: NDV nfv;
2419: Q q;
2420: P nm,nmp,dn,mnp,dnp,cont,cont1;
2421: union oNDC hc;
2422: NODE node;
2423: LIST l;
2424: Z iq;
1.1 noro 2425:
1.6 noro 2426: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2427: if ( nd_gentrace ) {
2428: /* Trace = [1,index,1,1] */
2429: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2430: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2431: }
2432: if ( nd_demand )
2433: nfv = ndv_load(i);
2434: else
2435: nfv = nd_ps[i];
2436: s = ndvtond(m,nfv);
2437: s = nd_separate_head(s,&head);
2438: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2439: if ( !stat ) return 0;
2440: ndv_free(nfv);
2441: hc = HCU(nf); nd_removecont(m,nf);
2442: /* exact division */
2443: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2444: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2445: nfv = ndtondv(m,nf);
2446: nd_free(nf);
2447: nd_bound[i] = ndv_compute_bound(nfv);
2448: if ( !m ) register_hcf(nfv);
2449: if ( nd_demand ) {
2450: ndv_save(nfv,i);
2451: ndv_free(nfv);
2452: } else
2453: nd_ps[i] = nfv;
2454: }
2455: return 1;
1.1 noro 2456: }
2457:
2458: LIST compute_splist()
2459: {
2460: NODE g,tn0,tn,node;
2461: LIST l0;
2462: ND_pairs d,t;
2463: int i;
2464: Z i1,i2;
2465:
2466: g = 0; d = 0;
2467: for ( i = 0; i < nd_psn; i++ ) {
2468: d = update_pairs(d,g,i,0);
2469: g = update_base(g,i);
2470: }
2471: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2472: NEXTNODE(tn0,tn);
1.6 noro 2473: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2474: node = mknode(2,i1,i2); MKLIST(l0,node);
2475: BDY(tn) = l0;
2476: }
2477: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2478: return l0;
2479: }
2480:
1.49 noro 2481: typedef struct oHPDATA {
1.56 ! noro 2482: int n;
! 2483: P hn; // HP(t)=hn(t)/((1-t^w0)*...*(1-t^w(n-1)))
1.49 noro 2484: VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
1.56 ! noro 2485: int *w;
1.49 noro 2486: } *HPDATA;
2487:
2488: void make_reduced(VECT b,int nv);
2489: void mhp_rec(VECT b,VECT x,P t,P *r);
2490: P mhp_ctop(P *r,P *plist,int n);
2491: DL monomial_colon(DL a,DL b,int n);
1.56 ! noro 2492: LIST dp_monomial_hilbert_poincare(VECT b,VECT x);
! 2493: LIST dp_monomial_hilbert_poincare_weight(VECT b,VECT x,int *w);
1.49 noro 2494:
2495: void setup_hpdata(HPDATA final,HPDATA current)
2496: {
1.56 ! noro 2497: int n,i,wlen;
1.49 noro 2498: DL *p;
1.56 ! noro 2499: VECT b,x;
1.49 noro 2500: DL dl;
1.56 ! noro 2501: LIST weight;
! 2502: LIST ret;
! 2503: int *w;
! 2504: NODE nd;
! 2505:
! 2506: final->n = n = nd_nvar;
! 2507: final->hn = (P)BDY(nd_hpdata);
! 2508: if ( NEXT(nd_hpdata) != 0 && (weight=(LIST)BDY(NEXT(nd_hpdata))) != 0 ) {
! 2509: wlen = length(BDY(weight));
! 2510: if ( n != wlen )
! 2511: error("setup_hpdata : inconsistent weight length");
! 2512: w = (int *)MALLOC(n*sizeof(int));
! 2513: for ( i = 0, nd = BDY((LIST)weight); i < n; i++, nd = NEXT(nd) )
! 2514: w[i] = ZTOS((Z)BDY(nd));
! 2515: } else
! 2516: w = 0;
1.49 noro 2517: MKVECT(x,n);
2518: for ( i = 0; i < n; i++ ) {
2519: NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
2520: }
2521: final->x = x;
1.56 ! noro 2522: final->w = w;
1.49 noro 2523:
2524: MKVECT(b,nd_psn); p = (DL *)BDY(b);
2525: for ( i = 0; i < nd_psn; i++ ) {
2526: p[i] = ndltodl(n,nd_psh[i]->dl);
2527: }
1.56 ! noro 2528: if ( w ) {
! 2529: ret = dp_monomial_hilbert_poincare_weight(b,x,w);
! 2530: } else
! 2531: ret = dp_monomial_hilbert_poincare(b,x);
! 2532: current->n = n;
! 2533: current->hn = (P)BDY(BDY(ret));
1.49 noro 2534: current->x = x;
1.56 ! noro 2535: current->w = w;
! 2536: }
! 2537:
! 2538: int comp_hn(P a, P b)
! 2539: {
! 2540: P s;
! 2541: DCP dc;
! 2542:
! 2543: subp(CO,a,b,&s);
! 2544: if ( !s ) return 99999999; /* XXX */
! 2545: else if ( OID(s) == 1 ) return 0;
! 2546: else {
! 2547: for ( dc = DC(s); NEXT(dc); dc = NEXT(dc) );
! 2548: return (int)ZTOS((Z)dc->d);
! 2549: }
1.49 noro 2550: }
2551:
1.56 ! noro 2552: void update_hpdata(HPDATA current,int nh)
1.49 noro 2553: {
2554: NODE data1,nd,t;
2555: DL new,dl;
2556: int len,i,n;
2557: Z dz;
2558: DL *p;
2559: VECT b,head;
2560: P tv,td,s,hn,hpoly;
2561: LIST list1;
2562:
2563: n = nd_nvar;
2564: new = ndltodl(n,nd_psh[nh]->dl);
2565: MKVECT(b,nh); p = (DL *)BDY(b);
2566: for ( i = 0; i < nh; i++ ) {
2567: p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
2568: }
2569: // compute HP(I:new)
1.56 ! noro 2570: if ( current->w )
! 2571: list1 = dp_monomial_hilbert_poincare_weight(b,current->x,current->w);
! 2572: else
! 2573: list1 = dp_monomial_hilbert_poincare(b,current->x);
1.49 noro 2574: data1 = BDY((LIST)list1);
2575: // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
2576: makevar("t",&tv); UTOZ(new->td,dz);
2577: pwrp(CO,tv,dz,&td);
2578: mulp(CO,(P)ARG0(data1),td,&s);
2579: subp(CO,current->hn,s,&hn);
2580: current->hn = hn;
2581: }
2582:
2583: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
2584: {
2585: struct oND_pairs root;
2586: ND_pairs prev,cur;
2587:
2588: root.next = d;
2589: prev = &root; cur = d;
2590: while ( cur ) {
2591: if ( SG(cur) == sugar )
2592: prev->next = cur->next;
2593: else
2594: prev = cur;
2595: cur = cur->next;
2596: }
2597: return root.next;
2598: }
2599:
1.1 noro 2600: /* return value = 0 => input is not a GB */
2601:
2602: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2603: {
1.6 noro 2604: int i,nh,sugar,stat;
2605: NODE r,g,t;
2606: ND_pairs d;
2607: ND_pairs l;
2608: ND h,nf,s,head,nf1;
2609: NDV nfv;
2610: Z q;
2611: union oNDC dn,hc;
2612: int diag_count = 0;
1.41 noro 2613: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2614: P cont;
2615: LIST list;
1.49 noro 2616: struct oHPDATA current_hpdata,final_hpdata;
2617: int final_hpvalue;
2618:
1.27 noro 2619: struct oEGT eg1,eg2,eg_update;
1.6 noro 2620:
1.27 noro 2621: init_eg(&eg_update);
1.11 noro 2622: Nnd_add = 0;
1.6 noro 2623: g = 0; d = 0;
2624: for ( i = 0; i < nd_psn; i++ ) {
2625: d = update_pairs(d,g,i,gensyz);
2626: g = update_base(g,i);
2627: }
2628: sugar = 0;
1.49 noro 2629: if ( nd_hpdata ) {
1.52 noro 2630: if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49 noro 2631: setup_hpdata(&final_hpdata,¤t_hpdata);
2632: }
1.6 noro 2633: while ( d ) {
1.1 noro 2634: again:
1.6 noro 2635: l = nd_minp(d,&d);
2636: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2637: if ( SG(l) != sugar ) {
2638: if ( ishomo ) {
2639: diag_count = 0;
2640: stat = do_diagonalize(sugar,m);
1.1 noro 2641: if ( !stat ) {
1.6 noro 2642: NEXT(l) = d; d = l;
2643: d = nd_reconstruct(0,d);
2644: goto again;
1.1 noro 2645: }
1.6 noro 2646: }
2647: sugar = SG(l);
2648: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2649: }
2650: stat = nd_sp(m,0,l,&h);
2651: if ( !stat ) {
2652: NEXT(l) = d; d = l;
2653: d = nd_reconstruct(0,d);
2654: goto again;
2655: }
1.1 noro 2656: #if USE_GEOBUCKET
1.39 noro 2657: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2658: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2659: #else
1.39 noro 2660: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2661: #endif
1.6 noro 2662: if ( !stat ) {
2663: NEXT(l) = d; d = l;
2664: d = nd_reconstruct(0,d);
2665: goto again;
2666: } else if ( nf ) {
1.41 noro 2667: Nnfnz++;
1.6 noro 2668: if ( checkonly || gensyz ) return 0;
1.1 noro 2669: if ( nd_newelim ) {
2670: if ( nd_module ) {
2671: if ( MPOS(HDL(nf)) > 1 ) return 0;
2672: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2673: }
1.6 noro 2674: if ( DP_Print ) { printf("+"); fflush(stdout); }
2675: hc = HCU(nf);
2676: nd_removecont(m,nf);
2677: if ( !m && nd_nalg ) {
2678: nd_monic(0,&nf);
2679: nd_removecont(m,nf);
2680: }
2681: if ( nd_gentrace ) {
2682: /* exact division */
1.1 noro 2683: cont = ndc_div(m,hc,HCU(nf));
2684: if ( m || !UNIQ(cont) ) {
1.6 noro 2685: t = mknode(4,NULLP,NULLP,NULLP,cont);
2686: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2687: nd_tracelist = t;
2688: }
2689: }
1.6 noro 2690: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2691: nh = ndv_newps(m,nfv,0);
1.6 noro 2692: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2693: diag_count = 0;
2694: stat = do_diagonalize(sugar,m);
2695: if ( !stat ) {
2696: NEXT(l) = d; d = l;
2697: d = nd_reconstruct(1,d);
2698: goto again;
1.1 noro 2699: }
1.6 noro 2700: }
1.27 noro 2701: get_eg(&eg1);
1.6 noro 2702: d = update_pairs(d,g,nh,0);
1.27 noro 2703: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2704: g = update_base(g,nh);
2705: FREENDP(l);
1.49 noro 2706: if ( nd_hpdata ) {
1.56 ! noro 2707: int dg;
! 2708:
! 2709: update_hpdata(¤t_hpdata,nh);
! 2710: dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
! 2711: if ( dg > sugar ) {
! 2712: printexpr(CO,(Obj)current_hpdata.hn); printf("\n");
! 2713: }
! 2714: while ( d && dg > sugar ) {
! 2715: if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49 noro 2716: d = nd_remove_same_sugar(d,sugar);
1.56 ! noro 2717: sugar++;
1.49 noro 2718: }
2719: }
1.41 noro 2720: } else {
2721: Nnfz++;
2722: if ( nd_gentrace && gensyz ) {
2723: nd_tracelist = reverse_node(nd_tracelist);
2724: MKLIST(list,nd_tracelist);
2725: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2726: MKNODE(t,list,nd_alltracelist);
2727: nd_alltracelist = t; nd_tracelist = 0;
2728: }
2729: if ( DP_Print ) { printf("."); fflush(stdout); }
2730: FREENDP(l);
2731: }
1.36 noro 2732: }
2733: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2734: if ( !checkonly && DP_Print ) {
2735: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2736: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2737: fflush(asir_out);
2738: }
2739: if ( DP_Print ) {
2740: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2741: }
1.36 noro 2742: return g;
1.1 noro 2743: }
2744:
1.30 noro 2745: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47 noro 2746: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30 noro 2747: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47 noro 2748: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24 noro 2749:
2750: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2751: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2752:
2753: void _copydl(int n,DL d1,DL d2);
2754: void _subfromdl(int n,DL d1,DL d2);
2755: extern int (*cmpdl)(int n,DL d1,DL d2);
1.53 noro 2756: int _dl_redble_ext(DL,DL,DL,int);
2757:
2758: int primitive_irred(ND p,SIG sig)
2759: {
2760: static int wpd=0,dlen=0;
2761: static DL dquo,squo;
2762: static UINT *quo;
2763: int i;
2764:
2765: if ( dlen < nd_nvar ) {
2766: NEWDL(dquo,nd_nvar);
2767: NEWDL(squo,nd_nvar);
2768: dlen = nd_nvar;
2769: }
2770: if ( wpd != nd_wpd ) {
2771: wpd = nd_wpd;
2772: quo = (UINT *)MALLOC(wpd*sizeof(UINT));
2773: }
2774: for ( i = 0; i < nd_psn; i++ ) {
2775: if ( sig->pos == nd_psh[i]->sig->pos &&
2776: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),squo,nd_nvar) )
2777: if ( ndl_reducible(HDL(p),DL(nd_psh[i])) ) {
2778: if ( DP_Print ) fprintf(asir_out,"D");
2779: ndl_sub(HDL(p),DL(nd_psh[i]),quo);
2780: _ndltodl(quo,dquo);
2781: if ( _eqdl(nd_nvar,squo,dquo) )
2782: return 0;
2783: }
2784: }
2785: return 1;
2786: }
1.24 noro 2787:
2788: NODE insert_sig(NODE l,SIG s)
2789: {
2790: int pos;
2791: DL sig;
2792: struct oNODE root;
2793: NODE p,prev,r;
2794: SIG t;
2795:
2796: pos = s->pos; sig = DL(s);
2797: root.next = l; prev = &root;
2798: for ( p = l; p; p = p->next ) {
2799: t = (SIG)p->body;
2800: if ( t->pos == pos ) {
2801: if ( _dl_redble(DL(t),sig,nd_nvar) )
2802: return root.next;
2803: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2804: // remove p
2805: prev->next = p->next;
1.45 noro 2806: else
2807: prev = p;
1.24 noro 2808: } else
2809: prev = p;
2810: }
1.27 noro 2811: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2812: for ( p = &root; p->next; p = p->next );
2813: p->next = r;
2814: // r->next = root.next;
2815: // return r;
2816: return root.next;
1.24 noro 2817: }
2818:
2819: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2820: {
2821: struct oND_pairs root;
2822: ND_pairs prev,p;
2823: SIG spsig;
2824:
2825: root.next = d;
2826: prev = &root; p = d;
2827: while ( p ) {
2828: spsig = p->sig;
1.26 noro 2829: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2830: // remove p
2831: prev->next = p->next;
1.26 noro 2832: Nsyz++;
2833: } else
1.24 noro 2834: prev = p;
2835: p = p->next;
2836: }
2837: return (ND_pairs)root.next;
2838: }
2839:
1.28 noro 2840: int small_lcm(ND_pairs l)
2841: {
2842: SIG sig;
2843: int i;
1.29 noro 2844: NODE t;
1.28 noro 2845: static DL lcm,mul,quo;
1.44 noro 2846: static int nvar = 0;
1.28 noro 2847:
1.38 noro 2848: if ( nd_sba_largelcm ) return 0;
1.28 noro 2849: if ( nvar < nd_nvar ) {
2850: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2851: }
2852: sig = l->sig;
2853: _ndltodl(l->lcm,lcm);
1.29 noro 2854: #if 0
1.28 noro 2855: for ( i = 0; i < nd_psn; i++ ) {
2856: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2857: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2858: _ndltodl(DL(nd_psh[i]),mul);
2859: _addtodl(nd_nvar,quo,mul);
2860: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2861: break;
2862: }
2863: }
2864: if ( i < nd_psn ) return 1;
2865: else return 0;
1.29 noro 2866: #else
2867: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2868: i = (long)BDY(t);
2869: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2870: _ndltodl(DL(nd_psh[i]),mul);
2871: _addtodl(nd_nvar,quo,mul);
2872: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2873: break;
2874: }
2875: }
2876: if ( t ) return 1;
2877: else return 0;
2878: #endif
1.28 noro 2879: }
2880:
1.41 noro 2881: ND_pairs find_smallest_lcm(ND_pairs l)
2882: {
2883: SIG sig;
2884: int i,minindex;
2885: NODE t;
2886: ND_pairs r;
2887: struct oSIG sig1;
1.55 noro 2888: static DL mul,quo,quo2,minlm;
1.44 noro 2889: static int nvar = 0;
1.41 noro 2890:
2891: if ( nvar < nd_nvar ) {
2892: nvar = nd_nvar;
1.55 noro 2893: NEWDL(quo,nvar); NEWDL(quo2,nvar); NEWDL(mul,nvar);
1.41 noro 2894: NEWDL(minlm,nvar);
2895: }
2896: sig = l->sig;
2897: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2898: _ndltodl(l->lcm,minlm); minindex = -1;
2899: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2900: i = (long)BDY(t);
2901: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2902: _ndltodl(DL(nd_psh[i]),mul);
2903: _addtodl(nd_nvar,quo,mul);
2904: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2905: minindex = i;
1.47 noro 2906: break;
1.41 noro 2907: _copydl(nd_nvar,mul,minlm);
2908: }
2909: }
2910: }
2911: // l->lcm is minimal; return l itself
2912: if ( minindex < 0 ) return l;
1.47 noro 2913: else return 0;
1.41 noro 2914: for ( i = 0; i < nd_psn; i++ ) {
2915: if ( i == minindex ) continue;
2916: _ndltodl(DL(nd_psh[i]),mul);
2917: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2918: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2919: sig1.pos = nd_ps[i]->sig->pos;
2920: sig1.dl = quo;
1.55 noro 2921: sig1.dl2 = quo2;
2922: _adddl(nd_nvar,sig1.dl,nd_sba_hm[sig1.pos],sig1.dl2);
1.41 noro 2923: if ( comp_sig(sig,&sig1) > 0 ) {
2924: // printf("X");
2925: NEWND_pairs(r);
2926: r->sig = sig;
2927: r->i1 = minindex;
2928: r->i2 = i;
2929: dltondl(nd_nvar,minlm,r->lcm);
2930: r->next = 0;
2931: return r;
2932: }
2933: }
2934: }
2935: // there is no suitable spair
2936: return 0;
2937: }
2938:
1.28 noro 2939: ND_pairs remove_large_lcm(ND_pairs d)
2940: {
2941: struct oND_pairs root;
2942: ND_pairs prev,p;
2943:
2944: root.next = d;
2945: prev = &root; p = d;
2946: while ( p ) {
1.41 noro 2947: #if 0
1.28 noro 2948: if ( small_lcm(p) ) {
2949: // remove p
2950: prev->next = p->next;
2951: } else
1.41 noro 2952: #else
2953: if ( find_smallest_lcm(p) == 0 ) {
2954: // remove p
2955: prev->next = p->next;
2956: } else
2957: #endif
1.28 noro 2958: prev = p;
2959: p = p->next;
2960: }
2961: return (ND_pairs)root.next;
2962: }
2963:
1.26 noro 2964: struct oEGT eg_create,eg_newpairs,eg_merge;
2965:
1.30 noro 2966: NODE conv_ilist_s(int demand,int trace,int **indp);
2967:
1.44 noro 2968: // S(fj*ei-fi*ej)
2969:
1.45 noro 2970: void _subdl(int,DL,DL,DL);
2971:
1.44 noro 2972: SIG trivial_sig(int i,int j)
2973: {
1.45 noro 2974: static DL lcm;
1.44 noro 2975: static struct oSIG sigi,sigj;
2976: static int nvar = 0;
2977: SIG sig;
2978:
2979: if ( nvar != nd_nvar ) {
1.45 noro 2980: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
1.55 noro 2981: NEWDL(sigi.dl2,nvar); NEWDL(sigj.dl2,nvar);
1.45 noro 2982: }
1.46 noro 2983: if ( nd_sba_inputisgb != 0 ) {
1.45 noro 2984: lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
2985: sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
1.55 noro 2986: _copydl(nd_nvar,lcm,sigi.dl2);
1.45 noro 2987: sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
1.55 noro 2988: _copydl(nd_nvar,lcm,sigj.dl2);
1.45 noro 2989: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2990: else sig = dup_sig(&sigj);
2991: } else {
2992: sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
1.55 noro 2993: _adddl(nd_nvar,sigi.dl,nd_sba_hm[i],sigi.dl2);
1.45 noro 2994: sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
1.55 noro 2995: _adddl(nd_nvar,sigj.dl,nd_sba_hm[j],sigj.dl2);
1.45 noro 2996: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2997: else sig = dup_sig(&sigj);
1.44 noro 2998: }
2999: return sig;
3000: }
3001:
1.47 noro 3002: int nd_minsig(ND_pairs *d)
3003: {
3004: int min,i,ret;
3005:
3006: min = -1;
3007: for ( i = 0; i < nd_nbase; i++ ) {
3008: if ( d[i] != 0 ) {
3009: if ( min < 0 ) min = i;
3010: else {
3011: ret = comp_sig(d[i]->sig,d[min]->sig);
3012: if ( ret < 0 ) min = i;
3013: }
3014: }
3015: }
3016: return min;
3017: }
3018:
3019: int dlength(ND_pairs d)
3020: {
3021: int i;
3022: for ( i = 0; d; d = d->next, i++ );
3023: return i;
3024: }
3025:
1.44 noro 3026: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24 noro 3027: {
1.39 noro 3028: int i,j,nh,sugar,stat,pos;
1.30 noro 3029: NODE r,t,g;
1.47 noro 3030: ND_pairs *d;
1.41 noro 3031: ND_pairs l,l1;
1.24 noro 3032: ND h,nf,s,head,nf1;
3033: NDV nfv;
3034: Z q;
3035: union oNDC dn,hc;
3036: P cont;
3037: LIST list;
3038: SIG sig;
1.29 noro 3039: NODE *syzlist;
1.47 noro 3040: int ngen,ind;
1.41 noro 3041: int Nnominimal,Nredundant;
1.28 noro 3042: DL lcm,quo,mul;
1.49 noro 3043: struct oHPDATA final_hpdata,current_hpdata;
1.50 noro 3044: struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
3045: struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47 noro 3046: int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24 noro 3047:
1.27 noro 3048: init_eg(&eg_remove);
1.29 noro 3049: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47 noro 3050: d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
3051: nd_nbase = nd_psn;
1.26 noro 3052: Nsyz = 0;
1.24 noro 3053: Nnd_add = 0;
1.41 noro 3054: Nnominimal = 0;
1.26 noro 3055: Nredundant = 0;
1.44 noro 3056: ngen = nd_psn;
1.56 ! noro 3057: if ( !do_weyl || nd_sba_inputisgb ) {
1.48 noro 3058: for ( i = 0; i < nd_psn; i++ )
3059: for ( j = i+1; j < nd_psn; j++ ) {
3060: sig = trivial_sig(i,j);
3061: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
3062: }
1.29 noro 3063: }
1.47 noro 3064: dlen = 0;
1.41 noro 3065: for ( i = 0; i < nd_psn; i++ ) {
1.47 noro 3066: dlen += update_pairs_array_s(d,i,syzlist);
1.41 noro 3067: }
1.24 noro 3068: sugar = 0;
1.39 noro 3069: pos = 0;
1.49 noro 3070: if ( nd_hpdata ) {
3071: setup_hpdata(&final_hpdata,¤t_hpdata);
3072: }
1.26 noro 3073: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50 noro 3074: init_eg(&eg_sp);
1.26 noro 3075: init_eg(&eg_create);
3076: init_eg(&eg_merge);
1.47 noro 3077: init_eg(&eg_minsig);
3078: init_eg(&eg_smallest);
1.29 noro 3079: init_eg(&eg_large);
1.31 noro 3080: init_eg(&eg_nf);
3081: init_eg(&eg_nfzero);
1.50 noro 3082: init_eg(&eg_removecont);
3083: init_eg(&eg_updatepairs);
3084: init_eg(&eg_hpdata);
3085: init_eg(&eg_sbabuch);
3086: get_eg(&eg3);
1.47 noro 3087: while ( 1 ) {
1.53 noro 3088: if ( DP_Print && !nd_hpdata && dlen%1000 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47 noro 3089: again :
3090: get_eg(&eg1);
3091: ind = nd_minsig(d);
3092: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
3093: if ( ind < 0 ) break;
3094: l = d[ind];
3095: // printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
3096: get_eg(&eg1);
1.41 noro 3097: l1 = find_smallest_lcm(l);
1.47 noro 3098: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41 noro 3099: if ( l1 == 0 ) {
1.47 noro 3100: d[ind] = d[ind]->next; dlen--;
1.53 noro 3101: // if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41 noro 3102: Nnominimal++;
3103: continue;
3104: }
3105: if ( SG(l1) != sugar ) {
3106: sugar = SG(l1);
3107: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3108: }
3109: sig = l1->sig;
3110: if ( DP_Print && nd_sba_pot ) {
3111: if ( sig->pos != pos ) {
3112: fprintf(asir_out,"[%d]",sig->pos);
3113: pos = sig->pos;
3114: }
3115: }
1.50 noro 3116: get_eg(&eg1);
1.41 noro 3117: stat = nd_sp(m,0,l1,&h);
1.50 noro 3118: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24 noro 3119: if ( !stat ) {
1.47 noro 3120: nd_reconstruct_s(0,d);
1.24 noro 3121: goto again;
3122: }
1.31 noro 3123: get_eg(&eg1);
1.25 noro 3124: #if USE_GEOBUCKET
1.55 noro 3125: stat = (m&&!nd_gentrace)?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 3126: #else
1.39 noro 3127: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 3128: #endif
1.31 noro 3129: get_eg(&eg2);
1.24 noro 3130: if ( !stat ) {
1.47 noro 3131: nd_reconstruct_s(0,d);
1.24 noro 3132: goto again;
3133: } else if ( stat == -1 ) {
1.47 noro 3134: d[ind] = d[ind]->next; dlen--;
1.41 noro 3135: Nnfs++;
1.24 noro 3136: if ( DP_Print ) { printf("S"); fflush(stdout); }
3137: } else if ( nf ) {
1.47 noro 3138: d[ind] = d[ind]->next; dlen--;
1.41 noro 3139: Nnfnz++;
3140: if ( DP_Print ) {
3141: if ( nd_sba_redundant_check ) {
3142: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
3143: Nredundant++;
3144: printf("R");
3145: } else
3146: printf("+");
3147: } else
3148: printf("+");
3149: fflush(stdout);
3150: }
1.31 noro 3151: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 3152: hc = HCU(nf);
1.55 noro 3153: get_eg(&eg1);
3154: nd_removecont(m,nf);
3155: get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
3156: nfv = ndtondv(m,nf); nd_free(nf);
3157: nh = ndv_newps(m,nfv,0);
3158:
3159: get_eg(&eg1);
3160: dlen += update_pairs_array_s(d,nh,syzlist);
3161: get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
3162: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
3163: if ( nd_hpdata ) {
3164: get_eg(&eg1);
1.56 ! noro 3165: update_hpdata(¤t_hpdata,nh);
1.55 noro 3166: get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
3167: if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) ) {
3168: if ( DP_Print ) { printf("\nWe found a gb.\n"); }
3169: break;
3170: }
1.49 noro 3171: }
1.24 noro 3172: } else {
1.47 noro 3173: d[ind] = d[ind]->next; dlen--;
1.41 noro 3174: Nnfz++;
1.31 noro 3175: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 3176: // syzygy
1.27 noro 3177: get_eg(&eg1);
1.47 noro 3178: nsyz = Nsyz;
3179: d[sig->pos] = remove_spair_s(d[sig->pos],sig);
3180: dlen -= Nsyz-nsyz;
1.27 noro 3181: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 3182: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 3183: if ( DP_Print ) { printf("."); fflush(stdout); }
3184: }
3185: }
1.50 noro 3186: get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30 noro 3187: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 3188: if ( DP_Print ) {
1.47 noro 3189: 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 3190: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
1.50 noro 3191: fflush(stdout);
1.41 noro 3192: if ( nd_sba_redundant_check )
3193: printf("Nredundant=%d\n",Nredundant);
1.50 noro 3194: fflush(stdout);
3195: print_eg("sp",&eg_sp);
1.26 noro 3196: print_eg("create",&eg_create);
3197: print_eg("merge",&eg_merge);
1.47 noro 3198: print_eg("minsig",&eg_minsig);
3199: print_eg("smallest",&eg_smallest);
3200: print_eg("remove",&eg_remove);
1.50 noro 3201: printf("\n");
1.47 noro 3202: print_eg("nf",&eg_nf);
3203: print_eg("nfzero",&eg_nfzero);
1.50 noro 3204: print_eg("removecont",&eg_removecont);
3205: print_eg("updatepairs",&eg_updatepairs);
3206: print_eg("hpdata",&eg_hpdata);
3207: print_eg("total",&eg_sbabuch);
1.47 noro 3208: printf("\n");
3209: }
3210: if ( nd_sba_syz ) {
1.27 noro 3211: print_eg("remove",&eg_remove);
1.31 noro 3212: print_eg("nf",&eg_nf);
3213: print_eg("nfzero",&eg_nfzero);
1.27 noro 3214: printf("\n");
1.26 noro 3215: }
1.44 noro 3216: if ( nd_sba_syz ) {
3217: NODE hsyz,tsyz,prev;
3218:
3219: hsyz = 0;
3220: for ( i = 0; i < ngen; i++ ) {
3221: tsyz = syzlist[i];
3222: for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
3223: BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
3224: if ( prev != 0 ) {
3225: prev->next = hsyz; hsyz = syzlist[i];
3226: }
3227: }
3228: *syzp = hsyz;
3229: } else *syzp = 0;
1.24 noro 3230: return g;
3231: }
3232:
1.1 noro 3233: /* splist = [[i1,i2],...] */
3234:
3235: int check_splist(int m,NODE splist)
3236: {
3237: NODE t,p;
3238: ND_pairs d,r,l;
3239: int stat;
3240: ND h,nf;
3241:
3242: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3243: p = BDY((LIST)BDY(t));
1.6 noro 3244: NEXTND_pairs(d,r);
3245: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3246: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3247: SG(r) = TD(LCM(r)); /* XXX */
3248: }
3249: if ( d ) NEXT(r) = 0;
3250:
1.6 noro 3251: while ( d ) {
1.1 noro 3252: again:
1.6 noro 3253: l = nd_minp(d,&d);
3254: stat = nd_sp(m,0,l,&h);
3255: if ( !stat ) {
3256: NEXT(l) = d; d = l;
3257: d = nd_reconstruct(0,d);
3258: goto again;
3259: }
1.39 noro 3260: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 3261: if ( !stat ) {
3262: NEXT(l) = d; d = l;
3263: d = nd_reconstruct(0,d);
3264: goto again;
3265: } else if ( nf ) return 0;
1.1 noro 3266: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 3267: }
1.1 noro 3268: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
3269: return 1;
3270: }
3271:
3272: int check_splist_f4(int m,NODE splist)
3273: {
3274: UINT *s0vect;
1.6 noro 3275: PGeoBucket bucket;
1.1 noro 3276: NODE p,rp0,t;
3277: ND_pairs d,r,l,ll;
3278: int col,stat;
3279:
3280: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3281: p = BDY((LIST)BDY(t));
1.6 noro 3282: NEXTND_pairs(d,r);
3283: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3284: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3285: SG(r) = TD(LCM(r)); /* XXX */
3286: }
3287: if ( d ) NEXT(r) = 0;
3288:
1.6 noro 3289: while ( d ) {
3290: l = nd_minsugarp(d,&d);
3291: bucket = create_pbucket();
3292: stat = nd_sp_f4(m,0,l,bucket);
3293: if ( !stat ) {
3294: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3295: NEXT(ll) = d; d = l;
3296: d = nd_reconstruct(0,d);
3297: continue;
3298: }
3299: if ( bucket->m < 0 ) continue;
3300: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
3301: if ( !col ) {
3302: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3303: NEXT(ll) = d; d = l;
3304: d = nd_reconstruct(0,d);
3305: continue;
1.1 noro 3306: }
1.6 noro 3307: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
3308: }
3309: return 1;
1.1 noro 3310: }
3311:
3312: int do_diagonalize_trace(int sugar,int m)
3313: {
1.6 noro 3314: int i,nh,stat;
3315: NODE r,g,t;
3316: ND h,nf,nfq,s,head;
3317: NDV nfv,nfqv;
3318: Q q,den,num;
3319: union oNDC hc;
3320: NODE node;
3321: LIST l;
3322: Z iq;
3323: P cont,cont1;
1.1 noro 3324:
1.6 noro 3325: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3326: if ( nd_gentrace ) {
3327: /* Trace = [1,index,1,1] */
3328: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3329: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3330: }
3331: /* for nd_ps */
3332: s = ndvtond(m,nd_ps[i]);
3333: s = nd_separate_head(s,&head);
3334: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3335: if ( !stat ) return 0;
3336: nf = nd_add(m,head,nf);
3337: ndv_free(nd_ps[i]);
3338: nd_ps[i] = ndtondv(m,nf);
3339: nd_free(nf);
3340:
3341: /* for nd_ps_trace */
3342: if ( nd_demand )
3343: nfv = ndv_load(i);
3344: else
3345: nfv = nd_ps_trace[i];
3346: s = ndvtond(0,nfv);
3347: s = nd_separate_head(s,&head);
3348: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3349: if ( !stat ) return 0;
3350: ndv_free(nfv);
3351: hc = HCU(nf); nd_removecont(0,nf);
3352: /* exact division */
1.1 noro 3353: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3354: if ( nd_gentrace ) finalize_tracelist(i,cont);
3355: nfv = ndtondv(0,nf);
3356: nd_free(nf);
3357: nd_bound[i] = ndv_compute_bound(nfv);
3358: register_hcf(nfv);
3359: if ( nd_demand ) {
3360: ndv_save(nfv,i);
3361: ndv_free(nfv);
3362: } else
3363: nd_ps_trace[i] = nfv;
3364: }
3365: return 1;
1.1 noro 3366: }
3367:
3368: static struct oEGT eg_invdalg;
3369: struct oEGT eg_le;
3370:
3371: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3372: {
3373: NODE tn;
3374: P p1;
3375:
3376: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3377: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3378: }
3379: *r = p;
3380: }
3381:
3382: NODE nd_gb_trace(int m,int ishomo,int **indp)
3383: {
1.6 noro 3384: int i,nh,sugar,stat;
3385: NODE r,g,t;
3386: ND_pairs d;
3387: ND_pairs l;
3388: ND h,nf,nfq,s,head;
3389: NDV nfv,nfqv;
3390: Z q,den,num;
3391: P hc;
3392: union oNDC dn,hnfq;
3393: struct oEGT eg_monic,egm0,egm1;
3394: int diag_count = 0;
3395: P cont;
3396: LIST list;
1.49 noro 3397: struct oHPDATA current_hpdata,final_hpdata;
3398: int final_hpvalue;
1.6 noro 3399:
3400: init_eg(&eg_monic);
3401: init_eg(&eg_invdalg);
3402: init_eg(&eg_le);
3403: g = 0; d = 0;
3404: for ( i = 0; i < nd_psn; i++ ) {
3405: d = update_pairs(d,g,i,0);
3406: g = update_base(g,i);
3407: }
3408: sugar = 0;
1.49 noro 3409: if ( nd_hpdata ) {
1.52 noro 3410: if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49 noro 3411: setup_hpdata(&final_hpdata,¤t_hpdata);
3412: }
3413:
1.6 noro 3414: while ( d ) {
1.1 noro 3415: again:
1.6 noro 3416: l = nd_minp(d,&d);
3417: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3418: if ( SG(l) != sugar ) {
1.1 noro 3419: #if 1
1.6 noro 3420: if ( ishomo ) {
3421: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3422: stat = do_diagonalize_trace(sugar,m);
3423: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3424: diag_count = 0;
1.1 noro 3425: if ( !stat ) {
1.6 noro 3426: NEXT(l) = d; d = l;
3427: d = nd_reconstruct(1,d);
3428: goto again;
1.1 noro 3429: }
1.6 noro 3430: }
3431: #endif
3432: sugar = SG(l);
3433: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3434: }
3435: stat = nd_sp(m,0,l,&h);
3436: if ( !stat ) {
3437: NEXT(l) = d; d = l;
3438: d = nd_reconstruct(1,d);
3439: goto again;
3440: }
1.1 noro 3441: #if USE_GEOBUCKET
1.39 noro 3442: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3443: #else
1.39 noro 3444: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3445: #endif
1.6 noro 3446: if ( !stat ) {
3447: NEXT(l) = d; d = l;
3448: d = nd_reconstruct(1,d);
3449: goto again;
3450: } else if ( nf ) {
3451: if ( nd_demand ) {
3452: nfqv = ndv_load(nd_psn);
3453: nfq = ndvtond(0,nfqv);
3454: } else
3455: nfq = 0;
3456: if ( !nfq ) {
1.39 noro 3457: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3458: NEXT(l) = d; d = l;
3459: d = nd_reconstruct(1,d);
3460: goto again;
3461: }
3462: }
3463: if ( nfq ) {
3464: /* m|HC(nfq) => failure */
3465: if ( nd_vc ) {
3466: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3467: } else
3468: q = HCZ(nfq);
3469: if ( !remqi((Q)q,m) ) return 0;
3470:
3471: if ( DP_Print ) { printf("+"); fflush(stdout); }
3472: hnfq = HCU(nfq);
3473: if ( nd_nalg ) {
3474: /* m|DN(HC(nf)^(-1)) => failure */
3475: get_eg(&egm0);
3476: if ( !nd_monic(m,&nfq) ) return 0;
3477: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3478: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3479: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3480: } else {
3481: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3482: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3483: }
3484: if ( nd_gentrace ) {
3485: /* exact division */
3486: cont = ndc_div(0,hnfq,HCU(nfqv));
3487: if ( !UNIQ(cont) ) {
3488: t = mknode(4,NULLP,NULLP,NULLP,cont);
3489: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3490: nd_tracelist = t;
3491: }
3492: }
1.24 noro 3493: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3494: if ( ishomo && ++diag_count == diag_period ) {
3495: diag_count = 0;
3496: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3497: stat = do_diagonalize_trace(sugar,m);
3498: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3499: if ( !stat ) {
1.1 noro 3500: NEXT(l) = d; d = l;
3501: d = nd_reconstruct(1,d);
3502: goto again;
1.6 noro 3503: }
1.1 noro 3504: }
1.6 noro 3505: d = update_pairs(d,g,nh,0);
3506: g = update_base(g,nh);
1.49 noro 3507: if ( nd_hpdata ) {
1.56 ! noro 3508: int dg;
! 3509:
! 3510: update_hpdata(¤t_hpdata,nh);
! 3511: dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
! 3512: if ( dg > sugar ) {
! 3513: printexpr(CO,(Obj)current_hpdata.hn); printf("\n");
! 3514: }
! 3515: while ( d && dg > sugar ) {
! 3516: if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49 noro 3517: d = nd_remove_same_sugar(d,sugar);
1.56 ! noro 3518: sugar++;
1.49 noro 3519: }
3520: }
1.6 noro 3521: } else {
3522: if ( DP_Print ) { printf("*"); fflush(stdout); }
3523: }
3524: } else {
3525: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3526: }
1.6 noro 3527: FREENDP(l);
3528: }
3529: if ( nd_nalg ) {
3530: if ( DP_Print ) {
3531: print_eg("monic",&eg_monic);
3532: print_eg("invdalg",&eg_invdalg);
3533: print_eg("le",&eg_le);
1.1 noro 3534: }
1.6 noro 3535: }
1.1 noro 3536: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3537: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3538: return g;
1.1 noro 3539: }
3540:
3541: int ndv_compare(NDV *p1,NDV *p2)
3542: {
3543: return DL_COMPARE(HDL(*p1),HDL(*p2));
3544: }
3545:
3546: int ndv_compare_rev(NDV *p1,NDV *p2)
3547: {
3548: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3549: }
3550:
3551: int ndvi_compare(NDVI p1,NDVI p2)
3552: {
3553: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3554: }
3555:
3556: int ndvi_compare_rev(NDVI p1,NDVI p2)
3557: {
3558: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3559: }
3560:
3561: NODE ndv_reduceall(int m,NODE f)
3562: {
3563: int i,j,n,stat;
3564: ND nf,g,head;
3565: NODE t,a0,a;
3566: union oNDC dn;
3567: Q q,num,den;
3568: NODE node;
3569: LIST l;
3570: Z iq,jq;
3571: int *perm;
3572: union oNDC hc;
3573: P cont,cont1;
3574:
3575: if ( nd_nora ) return f;
3576: n = length(f);
1.24 noro 3577: ndv_setup(m,0,f,0,1,0);
1.1 noro 3578: perm = (int *)MALLOC(n*sizeof(int));
3579: if ( nd_gentrace ) {
3580: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3581: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3582: }
3583: for ( i = 0; i < n; ) {
3584: if ( nd_gentrace ) {
3585: /* Trace = [1,index,1,1] */
1.6 noro 3586: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3587: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3588: }
3589: g = ndvtond(m,nd_ps[i]);
3590: g = nd_separate_head(g,&head);
1.6 noro 3591: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3592: if ( !stat )
3593: nd_reconstruct(0,0);
3594: else {
3595: if ( DP_Print ) { printf("."); fflush(stdout); }
3596: ndv_free(nd_ps[i]);
3597: hc = HCU(nf); nd_removecont(m,nf);
3598: if ( nd_gentrace ) {
3599: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3600: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3601: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3602: }
1.6 noro 3603: /* exact division */
1.1 noro 3604: cont = ndc_div(m,hc,HCU(nf));
3605: finalize_tracelist(perm[i],cont);
3606: }
3607: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3608: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3609: i++;
3610: }
3611: }
3612: if ( DP_Print ) { printf("\n"); }
3613: for ( a0 = 0, i = 0; i < n; i++ ) {
3614: NEXTNODE(a0,a);
3615: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3616: else {
3617: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3618: BDY(a) = (pointer)nd_ps[j];
3619: }
3620: }
3621: NEXT(a) = 0;
3622: return a0;
3623: }
3624:
1.41 noro 3625: int ndplength(ND_pairs d)
3626: {
3627: int i;
3628: for ( i = 0; d; i++ ) d = NEXT(d);
3629: return i;
3630: }
3631:
1.1 noro 3632: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3633: {
3634: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3635: int len0;
1.1 noro 3636:
3637: if ( !g ) return d;
3638: /* for testing */
3639: if ( gensyz && nd_gensyz == 2 ) {
3640: d1 = nd_newpairs(g,t);
3641: if ( !d )
3642: return d1;
3643: else {
3644: nd = d;
3645: while ( NEXT(nd) ) nd = NEXT(nd);
3646: NEXT(nd) = d1;
3647: return d;
3648: }
3649: }
3650: d = crit_B(d,t);
3651: d1 = nd_newpairs(g,t);
1.41 noro 3652: len0 = ndplength(d1);
1.1 noro 3653: d1 = crit_M(d1);
3654: d1 = crit_F(d1);
1.41 noro 3655: NcriMF += len0-ndplength(d1);
1.1 noro 3656: if ( gensyz || do_weyl )
3657: head = d1;
3658: else {
3659: prev = 0; cur = head = d1;
3660: while ( cur ) {
3661: if ( crit_2( cur->i1,cur->i2 ) ) {
3662: remove = cur;
3663: if ( !prev ) head = cur = NEXT(cur);
3664: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3665: FREENDP(remove); Ncri2++;
1.1 noro 3666: } else {
3667: prev = cur; cur = NEXT(cur);
3668: }
3669: }
3670: }
3671: if ( !d )
3672: return head;
3673: else {
3674: nd = d;
3675: while ( NEXT(nd) ) nd = NEXT(nd);
3676: NEXT(nd) = head;
3677: return d;
3678: }
3679: }
3680:
1.24 noro 3681: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3682:
1.30 noro 3683: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3684: {
3685: ND_pairs d1;
1.26 noro 3686: struct oEGT eg1,eg2,eg3;
1.24 noro 3687:
1.30 noro 3688: if ( !t ) return d;
1.26 noro 3689: get_eg(&eg1);
1.30 noro 3690: d1 = nd_newpairs_s(t,syz);
1.26 noro 3691: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3692: d = merge_pairs_s(d,d1);
1.26 noro 3693: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3694: return d;
3695: }
1.1 noro 3696:
1.47 noro 3697: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
3698: {
3699: ND_pairs *d1;
3700: struct oEGT eg1,eg2,eg3;
3701: int i;
3702:
3703: if ( !t ) return 0;
3704: get_eg(&eg1);
3705: Nnewpair = 0;
3706: d1 = nd_newpairs_array_s(t,syz);
3707: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
3708: for ( i = 0; i < nd_nbase; i++ )
3709: d[i] = merge_pairs_s(d[i],d1[i]);
3710: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
3711: return Nnewpair;
3712: }
3713:
1.1 noro 3714: ND_pairs nd_newpairs( NODE g, int t )
3715: {
3716: NODE h;
3717: UINT *dl;
3718: int ts,s,i,t0,min,max;
3719: ND_pairs r,r0;
3720:
3721: dl = DL(nd_psh[t]);
3722: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3723: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3724: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3725: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3726: continue;
3727: if ( nd_gbblock ) {
3728: t0 = (long)BDY(h);
3729: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3730: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3731: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3732: break;
3733: }
3734: if ( nd_gbblock[i] >= 0 )
3735: continue;
3736: }
1.41 noro 3737: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3738: r->i1 = (long)BDY(h);
3739: r->i2 = t;
3740: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3741: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3742: SG(r) = MAX(s,ts) + TD(LCM(r));
3743: /* experimental */
3744: if ( nd_sugarweight )
3745: r->sugar2 = ndl_weight2(r->lcm);
3746: }
3747: if ( r0 ) NEXT(r) = 0;
3748: return r0;
3749: }
3750:
1.46 noro 3751: int sig_cmpdl_op(int n,DL d1,DL d2)
3752: {
3753: int e1,e2,i,j,l;
3754: int *t1,*t2;
3755: int len,head;
3756: struct order_pair *pair;
3757:
3758: len = nd_sba_modord->block_length;
3759: pair = nd_sba_modord->order_pair;
3760:
3761: head = 0;
3762: for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
3763: l = pair[i].length;
3764: switch ( pair[i].order ) {
3765: case 0:
3766: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3767: e1 += t1[j];
3768: e2 += t2[j];
3769: }
3770: if ( e1 > e2 )
3771: return 1;
3772: else if ( e1 < e2 )
3773: return -1;
3774: else {
3775: for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
3776: if ( j >= 0 )
3777: return t1[j] < t2[j] ? 1 : -1;
3778: }
3779: break;
3780: case 1:
3781: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
3782: e1 += t1[j];
3783: e2 += t2[j];
3784: }
3785: if ( e1 > e2 )
3786: return 1;
3787: else if ( e1 < e2 )
3788: return -1;
3789: else {
3790: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3791: if ( j < l )
3792: return t1[j] > t2[j] ? 1 : -1;
3793: }
3794: break;
3795: case 2:
3796: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
3797: if ( j < l )
3798: return t1[j] > t2[j] ? 1 : -1;
3799: break;
3800: default:
3801: error("sig_cmpdl_op : invalid order"); break;
3802: }
3803: t1 += l; t2 += l; head += l;
3804: }
3805: return 0;
3806: }
3807:
3808: int sig_cmpdl_mat(int n,DL d1,DL d2)
3809: {
3810: int *v,*t1,*t2;
3811: int s,i,j,len;
3812: int **matrix;
3813: static int *w;
3814: static int nvar = 0;
3815:
3816: if ( nvar != n ) {
3817: nvar = n; w = (int *)MALLOC(n*sizeof(int));
3818: }
3819: for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
3820: w[i] = t1[i]-t2[i];
3821: len = nd_sba_modord->row;
3822: matrix = nd_sba_modord->matrix;
3823: for ( j = 0; j < len; j++ ) {
3824: v = matrix[j];
3825: for ( i = 0, s = 0; i < n; i++ )
3826: s += v[i]*w[i];
3827: if ( s > 0 )
3828: return 1;
3829: else if ( s < 0 )
3830: return -1;
3831: }
3832: return 0;
3833: }
3834:
3835: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
3836: {
3837: struct comp_sig_spec *spec;
3838: VL ovl,vl;
3839: V ov;
3840: int i,j,n,nvar,s;
3841: NODE node,t,tn;
3842: struct order_pair *l;
3843: MAT m;
3844: Obj **b;
3845: int **w;
3846: int *a;
3847:
3848: spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
3849: for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
3850: spec->n = nvar = i;
3851: if ( old_vl != 0 ) {
3852: spec->oldv = (int *)MALLOC(nvar*sizeof(int));
3853: for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
3854: ov = ovl->v;
3855: for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
3856: if ( ov == vl->v ) break;
3857: spec->oldv[i] = j;
3858: }
3859: } else
3860: spec->oldv = 0;
3861: if ( !ord || NUM(ord) ) {
3862: switch ( ZTOS((Z)ord) ) {
3863: case 0:
3864: spec->cmpdl = cmpdl_revgradlex; break;
3865: case 1:
3866: spec->cmpdl = cmpdl_gradlex; break;
3867: case 2:
3868: spec->cmpdl = cmpdl_lex; break;
3869: default:
3870: error("create_comp_sig_spec : invalid spec"); break;
3871: }
3872: } else if ( OID(ord) == O_LIST ) {
3873: node = BDY((LIST)ord);
3874: for ( n = 0, t = node; t; t = NEXT(t), n++ );
3875: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
3876: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
3877: tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
3878: tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
3879: s += l[i].length;
3880: }
3881: if ( s != nvar )
3882: error("create_comp_sig_spec : invalid spec");
3883: spec->order_pair = l;
3884: spec->block_length = n;
3885: spec->cmpdl = sig_cmpdl_op;
3886: } else if ( OID(ord) == O_MAT ) {
3887: m = (MAT)ord; b = (Obj **)BDY(m);
3888: if ( m->col != nvar )
3889: error("create_comp_sig_spec : invalid spec");
3890: w = almat(m->row,m->col);
3891: for ( i = 0; i < m->row; i++ )
3892: for ( j = 0; j < m->col; j++ )
3893: w[i][j] = ZTOS((Q)b[i][j]);
3894: spec->row = m->row;
3895: spec->matrix = w;
3896: spec->cmpdl = sig_cmpdl_mat;
3897: } else
3898: error("create_comp_sig_spec : invalid spec");
3899: if ( weight != 0 ) {
3900: node = BDY((LIST)weight);
3901: a = (int *)MALLOC(nvar*sizeof(int));
3902: for ( i = 0; i < nvar; i++, node = NEXT(node) )
3903: a[i] = ZTOS((Z)BDY(node));
3904: spec->weight = a;
3905: }
3906: return spec;
3907: }
3908:
3909: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
3910:
3911: int comp_sig_monomial(int n,DL d1,DL d2)
3912: {
3913: static DL m1,m2;
3914: static int nvar = 0;
3915: int *oldv,*weight;
3916: int i,w1,w2;
3917:
3918: if ( nvar != n ) {
3919: nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
3920: }
3921: if ( !nd_sba_modord )
3922: return (*cmpdl)(n,d1,d2);
1.55 noro 3923: else if ( !nd_sba_modord->weight && !nd_sba_modord->oldv ) {
3924: return (*nd_sba_modord->cmpdl)(n,d1,d2);
3925: } else {
1.46 noro 3926: weight = nd_sba_modord->weight;
3927: oldv = nd_sba_modord->oldv;
3928: if ( oldv ) {
3929: for ( i = 0; i < n; i++ ) {
3930: m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
3931: }
3932: } else {
3933: for ( i = 0; i < n; i++ ) {
3934: m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
3935: }
3936: }
3937: for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
3938: w1 += SIG_MUL_WEIGHT(m1->d[i],i);
3939: w2 += SIG_MUL_WEIGHT(m2->d[i],i);
3940: }
3941: m1->td = w1; m2->td = w2;
3942: return (*nd_sba_modord->cmpdl)(n,m1,m2);
3943: }
3944: }
3945:
1.24 noro 3946: int comp_sig(SIG s1,SIG s2)
3947: {
1.38 noro 3948: if ( nd_sba_pot ) {
3949: if ( s1->pos > s2->pos ) return 1;
3950: else if ( s1->pos < s2->pos ) return -1;
1.55 noro 3951: else return comp_sig_monomial(nd_nvar,DL(s1),DL(s2));
1.38 noro 3952: } else {
3953: static DL m1,m2;
1.44 noro 3954: static int nvar = 0;
1.38 noro 3955: int ret;
3956:
3957: if ( nvar != nd_nvar ) {
3958: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3959: }
1.47 noro 3960: if ( !nd_sba_modord )
1.55 noro 3961: ret = (*cmpdl)(nd_nvar,DL2(s1),DL2(s2));
1.47 noro 3962: else
1.55 noro 3963: ret = comp_sig_monomial(nd_nvar,DL2(s1),DL2(s2));
1.38 noro 3964: if ( ret != 0 ) return ret;
3965: else if ( s1->pos > s2->pos ) return 1;
3966: else if ( s1->pos < s2->pos ) return -1;
3967: else return 0;
1.24 noro 3968: }
3969: }
3970:
3971: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3972: {
3973: int ret,s1,s2;
3974: RHist p1,p2;
3975: static int wpd;
3976: static UINT *lcm;
3977:
3978: sp->i1 = i1;
3979: sp->i2 = i2;
3980: p1 = nd_psh[i1];
3981: p2 = nd_psh[i2];
3982: ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49 noro 3983: #if 0
1.24 noro 3984: s1 = SG(p1)-TD(DL(p1));
3985: s2 = SG(p2)-TD(DL(p2));
3986: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49 noro 3987: #endif
1.24 noro 3988:
1.26 noro 3989: if ( wpd != nd_wpd ) {
1.24 noro 3990: wpd = nd_wpd;
3991: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3992: }
3993: // DL(sig1) <- sp->lcm
3994: // DL(sig1) -= DL(p1)
3995: // DL(sig1) += DL(p1->sig)
1.27 noro 3996: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3997: _ndltodl(lcm,DL(sig1));
3998: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3999: sig1->pos = p1->sig->pos;
1.55 noro 4000: _adddl(nd_nvar,DL(sig1),nd_sba_hm[sig1->pos],DL2(sig1));
1.24 noro 4001:
4002: // DL(sig2) <- sp->lcm
4003: // DL(sig2) -= DL(p2)
4004: // DL(sig2) += DL(p2->sig)
1.27 noro 4005: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 4006: _ndltodl(lcm,DL(sig2));
4007: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
4008: sig2->pos = p2->sig->pos;
1.55 noro 4009: _adddl(nd_nvar,DL(sig2),nd_sba_hm[sig2->pos],DL2(sig2));
1.24 noro 4010:
4011: ret = comp_sig(sig1,sig2);
4012: if ( ret == 0 ) return 0;
4013: else if ( ret > 0 ) sp->sig = sig1;
4014: else sp->sig = sig2;
1.49 noro 4015:
4016: s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
4017: s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
4018: SG(sp) = MAX(s1,s2);
4019:
1.24 noro 4020: return 1;
4021: }
4022:
4023: SIG dup_sig(SIG sig)
4024: {
4025: SIG r;
4026:
4027: if ( !sig ) return 0;
4028: else {
4029: NEWSIG(r);
4030: _copydl(nd_nvar,DL(sig),DL(r));
1.55 noro 4031: _copydl(nd_nvar,DL2(sig),DL2(r));
1.24 noro 4032: r->pos = sig->pos;
4033: return r;
4034: }
4035: }
4036:
4037: void dup_ND_pairs(ND_pairs to,ND_pairs from)
4038: {
4039: to->i1 = from->i1;
4040: to->i2 = from->i2;
4041: to->sugar = from->sugar;
4042: to->sugar2 = from->sugar2;
4043: ndl_copy(from->lcm,to->lcm);
4044: to->sig = dup_sig(from->sig);
4045: }
4046:
4047: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
4048: {
4049: struct oND_pairs root;
4050: ND_pairs q1,q2,r0,r;
4051: int ret;
4052:
4053: r = &root;
4054: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
4055: ret = comp_sig(q1->sig,q2->sig);
4056: if ( ret < 0 ) {
4057: r->next = q1; r = q1; q1 = q1->next;
4058: } else if ( ret > 0 ) {
4059: r->next = q2; r = q2; q2 = q2->next;
4060: } else {
1.47 noro 4061: Nnewpair--;
1.24 noro 4062: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 4063: Nsamesig++;
1.24 noro 4064: if ( ret < 0 ) {
4065: r->next = q1; r = q1; q1 = q1->next;
4066: q2 = q2->next;
4067: } else {
4068: r->next = q2; r = q2; q2 = q2->next;
4069: q1 = q1->next;
4070: }
4071: }
4072: }
4073: if ( q1 ) {
4074: r->next = q1;
4075: } else {
4076: r->next = q2;
4077: }
4078: return root.next;
4079: }
4080:
4081: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
4082: {
4083: ND_pairs p,prev;
1.47 noro 4084: int ret=1;
1.24 noro 4085:
4086: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
4087: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
4088: break;
4089: }
4090: if ( ret == 0 ) {
4091: ret = DL_COMPARE(s->lcm,p->lcm);
4092: if ( ret < 0 ) {
4093: // replace p with s
4094: s->next = p->next;
4095: if ( prev == 0 ) {
4096: return s;
4097: } else {
4098: prev->next = s;
4099: return l;
4100: }
4101: } else
4102: return l;
4103: } else {
4104: // insert s between prev and p
1.47 noro 4105: Nnewpair++;
1.24 noro 4106: s->next = p;
4107: if ( prev == 0 ) {
4108: return s;
4109: } else {
4110: prev->next = s;
4111: return l;
4112: }
4113: }
4114: }
4115:
1.29 noro 4116: INLINE int __dl_redble(DL d1,DL d2,int nvar)
4117: {
4118: int i;
4119:
4120: if ( d1->td > d2->td )
4121: return 0;
4122: for ( i = nvar-1; i >= 0; i-- )
4123: if ( d1->d[i] > d2->d[i] )
4124: break;
4125: if ( i >= 0 )
4126: return 0;
4127: else
4128: return 1;
4129: }
4130:
1.30 noro 4131: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 4132: {
4133: NODE h,s;
4134: UINT *dl;
1.27 noro 4135: int ts,ret,i;
1.24 noro 4136: ND_pairs r,r0,_sp,sp;
1.29 noro 4137: SIG spsig,tsig;
1.44 noro 4138: static int nvar = 0;
1.29 noro 4139: static SIG _sig1,_sig2;
1.26 noro 4140: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 4141:
4142: NEWND_pairs(_sp);
1.29 noro 4143: if ( !_sig1 || nvar != nd_nvar ) {
4144: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
4145: }
1.24 noro 4146: r0 = 0;
1.27 noro 4147: for ( i = 0; i < t; i++ ) {
4148: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 4149: if ( ret ) {
4150: spsig = _sp->sig;
1.29 noro 4151: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 4152: tsig = (SIG)s->body;
1.29 noro 4153: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 4154: break;
4155: }
4156: if ( s == 0 ) {
4157: NEWND_pairs(sp);
4158: dup_ND_pairs(sp,_sp);
4159: r0 = insert_pair_s(r0,sp);
1.26 noro 4160: } else
4161: Nsyz++;
1.24 noro 4162: }
4163: }
4164: return r0;
4165: }
4166:
1.47 noro 4167: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
4168: {
4169: NODE h,s;
4170: UINT *dl;
4171: int ts,ret,i;
4172: ND_pairs r,r0,_sp,sp;
4173: ND_pairs *d;
4174: SIG spsig,tsig;
4175: static int nvar = 0;
4176: static SIG _sig1,_sig2;
4177: struct oEGT eg1,eg2,eg3,eg4;
4178:
4179: NEWND_pairs(_sp);
4180: if ( !_sig1 || nvar != nd_nvar ) {
4181: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
4182: }
4183: d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
4184: Nnewpair = 0;
4185: for ( i = 0; i < t; i++ ) {
4186: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
4187: if ( ret ) {
4188: spsig = _sp->sig;
4189: for ( s = syz[spsig->pos]; s; s = s->next ) {
4190: tsig = (SIG)s->body;
4191: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
4192: break;
4193: }
4194: if ( s == 0 ) {
4195: NEWND_pairs(sp);
4196: dup_ND_pairs(sp,_sp);
4197: d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
4198: } else
4199: Nsyz++;
4200: }
4201: }
4202: return d;
4203: }
4204:
1.1 noro 4205: /* ipair = [i1,i2],[i1,i2],... */
4206: ND_pairs nd_ipairtospair(NODE ipair)
4207: {
4208: int s1,s2;
4209: NODE tn,t;
4210: ND_pairs r,r0;
4211:
4212: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
4213: NEXTND_pairs(r0,r);
4214: tn = BDY((LIST)BDY(t));
1.6 noro 4215: r->i1 = ZTOS((Q)ARG0(tn));
4216: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 4217: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
4218: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
4219: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
4220: SG(r) = MAX(s1,s2) + TD(LCM(r));
4221: /* experimental */
4222: if ( nd_sugarweight )
4223: r->sugar2 = ndl_weight2(r->lcm);
4224: }
4225: if ( r0 ) NEXT(r) = 0;
4226: return r0;
4227: }
4228:
4229: /* kokokara */
4230:
4231: ND_pairs crit_B( ND_pairs d, int s )
4232: {
4233: ND_pairs cur,head,prev,remove;
4234: UINT *t,*tl,*lcm;
4235: int td,tdl;
4236:
4237: if ( !d ) return 0;
4238: t = DL(nd_psh[s]);
4239: prev = 0;
4240: head = cur = d;
4241: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4242: while ( cur ) {
4243: tl = cur->lcm;
4244: if ( ndl_reducible(tl,t) ) {
4245: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
4246: if ( !ndl_equal(lcm,tl) ) {
4247: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
4248: if (!ndl_equal(lcm,tl)) {
4249: remove = cur;
4250: if ( !prev ) {
4251: head = cur = NEXT(cur);
4252: } else {
4253: cur = NEXT(prev) = NEXT(cur);
4254: }
1.41 noro 4255: FREENDP(remove); NcriB++;
1.1 noro 4256: } else {
4257: prev = cur; cur = NEXT(cur);
4258: }
4259: } else {
4260: prev = cur; cur = NEXT(cur);
4261: }
4262: } else {
4263: prev = cur; cur = NEXT(cur);
4264: }
4265: }
4266: return head;
4267: }
4268:
4269: ND_pairs crit_M( ND_pairs d1 )
4270: {
4271: ND_pairs e,d2,d3,dd,p;
4272: UINT *id,*jd;
4273:
4274: if ( !d1 ) return d1;
4275: for ( dd = 0, e = d1; e; e = d3 ) {
4276: if ( !(d2 = NEXT(e)) ) {
4277: NEXT(e) = dd;
4278: return e;
4279: }
4280: id = LCM(e);
4281: for ( d3 = 0; d2; d2 = p ) {
4282: p = NEXT(d2);
4283: jd = LCM(d2);
4284: if ( ndl_equal(jd,id) )
4285: ;
4286: else if ( TD(jd) > TD(id) )
4287: if ( ndl_reducible(jd,id) ) continue;
4288: else ;
4289: else if ( ndl_reducible(id,jd) ) goto delit;
4290: NEXT(d2) = d3;
4291: d3 = d2;
4292: }
4293: NEXT(e) = dd;
4294: dd = e;
4295: continue;
4296: /**/
4297: delit: NEXT(d2) = d3;
4298: d3 = d2;
4299: for ( ; p; p = d2 ) {
4300: d2 = NEXT(p);
4301: NEXT(p) = d3;
4302: d3 = p;
4303: }
4304: FREENDP(e);
4305: }
4306: return dd;
4307: }
4308:
4309: ND_pairs crit_F( ND_pairs d1 )
4310: {
4311: ND_pairs rest, head,remove;
4312: ND_pairs last, p, r, w;
4313: int s;
4314:
4315: if ( !d1 ) return d1;
4316: for ( head = last = 0, p = d1; NEXT(p); ) {
4317: r = w = equivalent_pairs(p,&rest);
4318: s = SG(r);
4319: w = NEXT(w);
4320: while ( w ) {
4321: if ( crit_2(w->i1,w->i2) ) {
4322: r = w;
4323: w = NEXT(w);
4324: while ( w ) {
4325: remove = w;
4326: w = NEXT(w);
4327: FREENDP(remove);
4328: }
4329: break;
4330: } else if ( SG(w) < s ) {
4331: FREENDP(r);
4332: r = w;
4333: s = SG(r);
4334: w = NEXT(w);
4335: } else {
4336: remove = w;
4337: w = NEXT(w);
4338: FREENDP(remove);
4339: }
4340: }
4341: if ( last ) NEXT(last) = r;
4342: else head = r;
4343: NEXT(last = r) = 0;
4344: p = rest;
4345: if ( !p ) return head;
4346: }
4347: if ( !last ) return p;
4348: NEXT(last) = p;
4349: return head;
4350: }
4351:
4352: int crit_2( int dp1, int dp2 )
4353: {
4354: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
4355: }
4356:
4357: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
4358: {
4359: ND_pairs w,p,r,s;
4360: UINT *d;
4361:
4362: w = d1;
4363: d = LCM(w);
4364: s = NEXT(w);
4365: NEXT(w) = 0;
4366: for ( r = 0; s; s = p ) {
4367: p = NEXT(s);
4368: if ( ndl_equal(d,LCM(s)) ) {
4369: NEXT(s) = w; w = s;
4370: } else {
4371: NEXT(s) = r; r = s;
4372: }
4373: }
4374: *prest = r;
4375: return w;
4376: }
4377:
4378: NODE update_base(NODE nd,int ndp)
4379: {
4380: UINT *dl, *dln;
4381: NODE last, p, head;
4382:
4383: dl = DL(nd_psh[ndp]);
4384: for ( head = last = 0, p = nd; p; ) {
4385: dln = DL(nd_psh[(long)BDY(p)]);
4386: if ( ndl_reducible( dln, dl ) ) {
4387: p = NEXT(p);
4388: if ( last ) NEXT(last) = p;
4389: } else {
4390: if ( !last ) head = p;
4391: p = NEXT(last = p);
4392: }
4393: }
4394: head = append_one(head,ndp);
4395: return head;
4396: }
4397:
4398: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
4399: {
4400: ND_pairs m,ml,p,l;
4401: UINT *lcm;
4402: int s,td,len,tlen,c,c1;
4403:
4404: if ( !(p = NEXT(m = d)) ) {
4405: *prest = p;
4406: NEXT(m) = 0;
4407: return m;
4408: }
4409: if ( !NoSugar ) {
4410: if ( nd_sugarweight ) {
4411: s = m->sugar2;
4412: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4413: if ( (p->sugar2 < s)
4414: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4415: ml = l; m = p; s = m->sugar2;
4416: }
4417: } else {
4418: s = SG(m);
4419: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4420: if ( (SG(p) < s)
4421: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4422: ml = l; m = p; s = SG(m);
4423: }
4424: }
4425: } else {
4426: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4427: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
4428: ml = l; m = p; s = SG(m);
4429: }
4430: }
4431: if ( !ml ) *prest = NEXT(m);
4432: else {
4433: NEXT(ml) = NEXT(m);
4434: *prest = d;
4435: }
4436: NEXT(m) = 0;
4437: return m;
4438: }
4439:
4440: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
4441: {
4442: int msugar,i;
4443: ND_pairs t,dm0,dm,dr0,dr;
4444:
4445: if ( nd_sugarweight ) {
4446: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
4447: if ( t->sugar2 < msugar ) msugar = t->sugar2;
4448: dm0 = 0; dr0 = 0;
4449: for ( i = 0, t = d; t; t = NEXT(t) )
4450: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
4451: if ( dm0 ) NEXT(dm) = t;
4452: else dm0 = t;
4453: dm = t;
4454: i++;
4455: } else {
4456: if ( dr0 ) NEXT(dr) = t;
4457: else dr0 = t;
4458: dr = t;
4459: }
4460: } else {
4461: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
4462: if ( SG(t) < msugar ) msugar = SG(t);
4463: dm0 = 0; dr0 = 0;
4464: for ( i = 0, t = d; t; t = NEXT(t) )
4465: if ( i < nd_f4_nsp && SG(t) == msugar ) {
4466: if ( dm0 ) NEXT(dm) = t;
4467: else dm0 = t;
4468: dm = t;
4469: i++;
4470: } else {
4471: if ( dr0 ) NEXT(dr) = t;
4472: else dr0 = t;
4473: dr = t;
4474: }
4475: }
4476: NEXT(dm) = 0;
4477: if ( dr0 ) NEXT(dr) = 0;
4478: *prest = dr0;
4479: return dm0;
4480: }
4481:
1.28 noro 4482: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
4483: {
4484: int msugar;
4485: ND_pairs t,last;
4486:
4487: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
4488: if ( SG(t) == msugar ) last = t;
4489: *prest = last->next;
4490: last->next = 0;
4491: return d;
4492: }
4493:
1.1 noro 4494: int nd_tdeg(NDV c)
4495: {
4496: int wmax = 0;
4497: int i,len;
4498: NMV a;
4499:
4500: len = LEN(c);
4501: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4502: wmax = MAX(TD(DL(a)),wmax);
4503: return wmax;
4504: }
4505:
1.24 noro 4506: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 4507: {
4508: int len;
4509: RHist r;
4510: NDV b;
4511: NODE tn;
4512: LIST l;
4513: Z iq;
4514:
4515: if ( nd_psn == nd_pslen ) {
4516: nd_pslen *= 2;
4517: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
4518: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
4519: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
4520: nd_bound = (UINT **)
4521: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
4522: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
4523: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
4524: }
4525: NEWRHist(r); nd_psh[nd_psn] = r;
4526: nd_ps[nd_psn] = a;
4527: if ( aq ) {
4528: nd_ps_trace[nd_psn] = aq;
4529: if ( !m ) {
4530: register_hcf(aq);
4531: } else if ( m == -2 ) {
4532: /* do nothing */
4533: } else
4534: error("ndv_newps : invalud modulus");
4535: nd_bound[nd_psn] = ndv_compute_bound(aq);
4536: #if 1
4537: SG(r) = SG(aq);
4538: #else
4539: SG(r) = nd_tdeg(aq);
4540: #endif
4541: ndl_copy(HDL(aq),DL(r));
1.24 noro 4542: r->sig = dup_sig(aq->sig);
1.1 noro 4543: } else {
4544: if ( !m ) register_hcf(a);
4545: nd_bound[nd_psn] = ndv_compute_bound(a);
4546: #if 1
4547: SG(r) = SG(a);
4548: #else
4549: SG(r) = nd_tdeg(a);
4550: #endif
4551: ndl_copy(HDL(a),DL(r));
1.24 noro 4552: r->sig = dup_sig(a->sig);
1.1 noro 4553: }
4554: if ( nd_demand ) {
4555: if ( aq ) {
4556: ndv_save(nd_ps_trace[nd_psn],nd_psn);
4557: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4558: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4559: nd_ps_trace[nd_psn] = 0;
4560: } else {
4561: ndv_save(nd_ps[nd_psn],nd_psn);
4562: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
4563: nd_ps[nd_psn] = 0;
4564: }
4565: }
4566: if ( nd_gentrace ) {
4567: /* reverse the tracelist and append it to alltracelist */
4568: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 4569: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 4570: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
4571: }
4572: return nd_psn++;
4573: }
4574:
1.46 noro 4575: // find LM wrt the specified modord
4576: void ndv_lm_modord(NDV p,DL d)
1.45 noro 4577: {
4578: NMV m;
4579: DL tmp;
4580: int len,i,ret;
4581:
4582: NEWDL(tmp,nd_nvar);
4583: m = BDY(p); len = LEN(p);
1.46 noro 4584: _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45 noro 4585: for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
4586: _ndltodl(DL(m),tmp);
1.46 noro 4587: ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45 noro 4588: if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
4589: }
1.46 noro 4590: // printdl(d); printf("\n");
1.45 noro 4591: }
4592:
1.1 noro 4593: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
4594: /* return 1 if success, 0 if failure (HC(a mod p)) */
4595:
1.24 noro 4596: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 4597: {
1.6 noro 4598: int i,j,td,len,max;
4599: NODE s,s0,f0,tn;
4600: UINT *d;
4601: RHist r;
4602: NDVI w;
4603: NDV a,am;
4604: union oNDC hc;
4605: NODE node;
4606: P hcp;
4607: Z iq,jq;
4608: LIST l;
4609:
4610: nd_found = 0; nd_notfirst = 0; nd_create = 0;
4611: /* initialize the tracelist */
4612: nd_tracelist = 0;
4613:
4614: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
4615: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
4616: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
4617: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
4618: if ( !dont_sort ) {
4619: /* XXX heuristic */
1.26 noro 4620: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 4621: qsort(w,nd_psn,sizeof(struct oNDVI),
4622: (int (*)(const void *,const void *))ndvi_compare_rev);
4623: else
4624: qsort(w,nd_psn,sizeof(struct oNDVI),
4625: (int (*)(const void *,const void *))ndvi_compare);
4626: }
4627: nd_pslen = 2*nd_psn;
4628: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4629: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4630: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4631: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4632: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4633: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4634: nd_hcf = 0;
1.1 noro 4635:
1.6 noro 4636: if ( trace && nd_vc )
4637: makesubst(nd_vc,&nd_subst);
4638: else
4639: nd_subst = 0;
1.1 noro 4640:
1.6 noro 4641: if ( !nd_red )
4642: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4643: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4644: for ( i = 0; i < nd_psn; i++ ) {
4645: hc = HCU(w[i].p);
4646: if ( trace ) {
4647: if ( mod == -2 ) {
4648: /* over a large finite field */
4649: /* trace = small modulus */
4650: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4651: ndv_mod(-2,a);
4652: if ( !dont_removecont) ndv_removecont(-2,a);
4653: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4654: ndv_mod(trace,am);
4655: if ( DL_COMPARE(HDL(am),HDL(a)) )
4656: return 0;
4657: ndv_removecont(trace,am);
4658: } else {
4659: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4660: if ( !dont_removecont) ndv_removecont(0,a);
4661: register_hcf(a);
4662: am = nd_ps[i] = ndv_dup(mod,a);
4663: ndv_mod(mod,am);
4664: if ( DL_COMPARE(HDL(am),HDL(a)) )
4665: return 0;
4666: ndv_removecont(mod,am);
4667: }
4668: } else {
4669: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4670: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4671: if ( !mod ) register_hcf(a);
1.1 noro 4672: }
1.6 noro 4673: if ( nd_gentrace ) {
4674: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4675: /* exact division */
1.1 noro 4676: if ( !dont_removecont )
1.6 noro 4677: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4678: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4679: }
4680: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4681: nd_bound[i] = ndv_compute_bound(a);
4682: nd_psh[i] = r;
4683: if ( nd_demand ) {
4684: if ( trace ) {
4685: ndv_save(nd_ps_trace[i],i);
4686: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4687: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4688: nd_ps_trace[i] = 0;
4689: } else {
4690: ndv_save(nd_ps[i],i);
4691: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4692: nd_ps[i] = 0;
4693: }
1.1 noro 4694: }
1.6 noro 4695: }
1.24 noro 4696: if ( sba ) {
1.27 noro 4697: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4698: // setup signatures
1.27 noro 4699: for ( i = 0; i < nd_psn; i++ ) {
4700: SIG sig;
1.24 noro 4701:
1.27 noro 4702: NEWSIG(sig); sig->pos = i;
4703: nd_ps[i]->sig = sig;
4704: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4705: nd_psh[i]->sig = sig;
4706: if ( trace ) {
4707: nd_ps_trace[i]->sig = sig;
4708: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4709: }
4710: NEWDL(nd_sba_hm[i],nd_nvar);
1.46 noro 4711: if ( nd_sba_modord )
4712: ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45 noro 4713: else
4714: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.55 noro 4715: _adddl(nd_nvar,DL(sig),nd_sba_hm[i],DL2(sig));
1.27 noro 4716: }
1.29 noro 4717: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4718: for ( i = 0; i < nd_psn; i++ ) {
4719: j = nd_psh[i]->sig->pos;
4720: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4721: }
1.24 noro 4722: }
1.6 noro 4723: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4724: return 1;
1.1 noro 4725: }
4726:
4727: struct order_spec *append_block(struct order_spec *spec,
4728: int nv,int nalg,int ord);
4729:
4730: extern VECT current_dl_weight_vector_obj;
4731: static VECT prev_weight_vector_obj;
4732:
4733: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4734: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4735: {
4736: NODE alist,t,s,r0,r,arg;
4737: VL tv;
4738: P poly;
4739: DP d;
4740: Alg alpha,dp;
4741: DAlg inv,da,hc;
4742: MP m;
4743: int i,nvar,nalg,n;
4744: NumberField nf;
4745: LIST f1,f2;
4746: struct order_spec *current_spec;
4747: VECT obj,obj0;
4748: VECT tmp;
4749:
4750: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4751: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4752:
4753: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4754: NEXTNODE(alist,t); MKV(tv->v,poly);
4755: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4756: tv->v = tv->v->priv;
4757: }
4758: NEXT(t) = 0;
4759:
4760: /* simplification, making polynomials monic */
4761: setfield_dalg(alist);
4762: obj_algtodalg((Obj)f,(Obj *)&f1);
4763: for ( t = BDY(f); t; t = NEXT(t) ) {
4764: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4765: hc = (DAlg)BDY(d)->c;
4766: if ( NID(hc) == N_DA ) {
4767: invdalg(hc,&inv);
4768: for ( m = BDY(d); m; m = NEXT(m) ) {
4769: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4770: }
4771: }
4772: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4773: }
4774: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4775:
4776: /* append alg vars to the var list */
4777: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4778: NEXT(tv) = av;
4779:
4780: /* append a block to ord */
4781: *ord1p = append_block(ord,nvar,nalg,2);
4782:
4783: /* create generator list */
4784: nf = get_numberfield();
4785: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4786: MKAlg(nf->defpoly[i],dp);
4787: MKNODE(s,dp,t); t = s;
4788: }
4789: MKLIST(f1,t);
4790: *alistp = alist;
4791: algobjtorat((Obj)f1,(Obj *)f1p);
4792:
4793: /* creating a new weight vector */
4794: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4795: n = nvar+nalg+1;
4796: MKVECT(obj,n);
4797: if ( obj0 && obj0->len == nvar )
4798: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4799: else
4800: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4801: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4802: BDY(obj)[n-1] = (pointer)ONE;
4803: arg = mknode(1,obj);
4804: Pdp_set_weight(arg,&tmp);
4805: }
4806:
4807: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4808: {
4809: NODE s,t,u0,u;
4810: P p;
4811: VL tv;
4812: Obj obj;
4813: VECT tmp;
4814: NODE arg;
4815:
4816: u0 = 0;
4817: for ( t = r; t; t = NEXT(t) ) {
4818: p = (P)BDY(t);
4819: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4820: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4821: }
4822: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4823: NEXTNODE(u0,u);
4824: BDY(u) = (pointer)p;
4825: }
4826: }
4827: arg = mknode(1,prev_weight_vector_obj);
4828: Pdp_set_weight(arg,&tmp);
4829:
4830: return u0;
4831: }
4832:
4833: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4834: {
4835: VL tv,fv,vv,vc,av;
4836: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4837: int e,max,nvar,i;
4838: NDV b;
4839: int ishomo,nalg,mrank,trank,wmax,len;
4840: NMV a;
4841: Alg alpha,dp;
4842: P p,zp;
4843: Q dmy;
4844: LIST f1,f2,zpl;
4845: Obj obj;
4846: NumberField nf;
4847: struct order_spec *ord1;
4848: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4849: LIST l1,l2,l3,l4,l5;
4850: int j;
4851: Z jq,bpe,last_nonzero;
4852: int *perm;
4853: EPOS oepos;
4854: int obpe,oadv,ompos,cbpe;
1.15 noro 4855: VECT hvect;
1.1 noro 4856:
1.41 noro 4857: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4858: nd_module = 0;
4859: if ( !m && Demand ) nd_demand = 1;
4860: else nd_demand = 0;
4861:
4862: if ( DP_Multiple )
4863: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4864: #if 0
4865: ndv_alloc = 0;
4866: #endif
4867: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 4868: parse_nd_option(vv,current_option);
1.1 noro 4869: if ( m && nd_vc )
4870: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4871: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4872: switch ( ord->id ) {
4873: case 1:
4874: if ( ord->nv != nvar )
4875: error("nd_{gr,f4} : invalid order specification");
4876: break;
4877: default:
4878: break;
4879: }
4880: nd_nalg = 0;
4881: av = 0;
4882: if ( !m ) {
4883: get_algtree((Obj)f,&av);
4884: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4885: nd_ntrans = nvar;
4886: nd_nalg = nalg;
4887: /* #i -> t#i */
4888: if ( nalg ) {
4889: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4890: ord = ord1;
4891: f = f1;
4892: }
4893: nvar += nalg;
4894: }
4895: nd_init_ord(ord);
4896: mrank = 0;
4897: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4898: for ( tv = vv; tv; tv = NEXT(tv) ) {
4899: if ( nd_module ) {
1.16 noro 4900: if ( OID(BDY(t)) == O_DPM ) {
4901: e = dpm_getdeg((DPM)BDY(t),&trank);
4902: max = MAX(e,max);
4903: mrank = MAX(mrank,trank);
4904: } else {
4905: s = BDY((LIST)BDY(t));
4906: trank = length(s);
4907: mrank = MAX(mrank,trank);
4908: for ( ; s; s = NEXT(s) ) {
4909: e = getdeg(tv->v,(P)BDY(s));
4910: max = MAX(e,max);
4911: }
1.1 noro 4912: }
4913: } else {
1.43 noro 4914: if ( OID(BDY(t)) == O_DP ) {
4915: e = dp_getdeg((DP)BDY(t));
4916: max = MAX(e,max);
4917: } else {
4918: e = getdeg(tv->v,(P)BDY(t));
4919: max = MAX(e,max);
4920: }
1.1 noro 4921: }
4922: }
4923: nd_setup_parameters(nvar,nd_nzlist?0:max);
4924: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4925: ishomo = 1;
4926: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4927: if ( nd_module ) {
1.16 noro 4928: if ( OID(BDY(t)) == O_DPM ) {
4929: Z cont;
4930: DPM zdpm;
4931:
4932: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4933: else zdpm = (DPM)BDY(t);
4934: b = (pointer)dpmtondv(m,zdpm);
4935: } else {
4936: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4937: else zpl = (LIST)BDY(t);
1.1 noro 4938: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4939: }
1.1 noro 4940: } else {
1.43 noro 4941: if ( OID(BDY(t)) == O_DP ) {
4942: DP zdp;
4943:
4944: if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
4945: else zdp = (DP)BDY(t);
4946: b = (pointer)dptondv(m,zdp);
4947: } else {
4948: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4949: else zp = (P)BDY(t);
4950: b = (pointer)ptondv(CO,vv,zp);
4951: }
1.1 noro 4952: }
4953: if ( ishomo )
4954: ishomo = ishomo && ndv_ishomo(b);
4955: if ( m ) ndv_mod(m,b);
4956: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4957: }
4958: if ( fd0 ) NEXT(fd) = 0;
4959:
4960: if ( !ishomo && homo ) {
4961: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4962: b = (NDV)BDY(t); len = LEN(b);
4963: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4964: wmax = MAX(TD(DL(a)),wmax);
4965: }
4966: homogenize_order(ord,nvar,&ord1);
4967: nd_init_ord(ord1);
4968: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4969: for ( t = fd0; t; t = NEXT(t) )
4970: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4971: }
4972:
1.24 noro 4973: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4974: if ( nd_gentrace ) {
4975: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4976: }
4977: if ( nd_splist ) {
4978: *rp = compute_splist();
4979: return;
4980: }
4981: if ( nd_check_splist ) {
4982: if ( f4 ) {
4983: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4984: else *rp = 0;
4985: } else {
4986: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4987: else *rp = 0;
4988: }
4989: return;
4990: }
4991: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4992: if ( !x ) {
4993: *rp = 0; return;
4994: }
1.15 noro 4995: if ( nd_gentrace ) {
4996: MKVECT(hvect,nd_psn);
4997: for ( i = 0; i < nd_psn; i++ )
4998: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4999: }
1.1 noro 5000: if ( !ishomo && homo ) {
5001: /* dehomogenization */
5002: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5003: nd_init_ord(ord);
5004: nd_setup_parameters(nvar,0);
5005: }
5006: nd_demand = 0;
5007: if ( nd_module && nd_intersect ) {
5008: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 5009: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 5010: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
5011: }
5012: conv_ilist(nd_demand,0,x,0);
5013: goto FINAL;
5014: }
5015: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
5016: x = ndv_reducebase(x,perm);
5017: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
5018: x = ndv_reduceall(m,x);
5019: cbpe = nd_bpe;
5020: if ( nd_gentrace && !f4 ) {
5021: tl2 = nd_alltracelist; nd_alltracelist = 0;
5022: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
5023: tl3 = nd_alltracelist; nd_alltracelist = 0;
5024: if ( nd_gensyz ) {
5025: nd_gb(m,0,1,1,0);
5026: tl4 = nd_alltracelist; nd_alltracelist = 0;
5027: } else tl4 = 0;
5028: }
5029: nd_bpe = cbpe;
5030: nd_setup_parameters(nd_nvar,0);
5031: FINAL:
5032: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 5033: NEXTNODE(r0,r);
5034: if ( nd_module ) {
5035: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
5036: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
5037: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
5038: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 5039: }
5040: if ( r0 ) NEXT(r) = 0;
5041: if ( !m && nd_nalg )
5042: r0 = postprocess_algcoef(av,alist,r0);
5043: MKLIST(*rp,r0);
5044: if ( nd_gentrace ) {
5045: if ( f4 ) {
1.6 noro 5046: STOZ(16,bpe);
5047: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 5048: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 5049: } else {
5050: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5051: tl3 = reverse_node(tl3);
5052: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5053: for ( t = tl2; t; t = NEXT(t) ) {
5054: /* s = [i,[*,j,*,*],...] */
5055: s = BDY((LIST)BDY(t));
1.6 noro 5056: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5057: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5058: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5059: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5060: }
5061: }
5062: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5063: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5064: }
5065: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5066: MKLIST(l5,tl4);
1.6 noro 5067: STOZ(nd_bpe,bpe);
1.15 noro 5068: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5069: }
5070: }
5071: #if 0
5072: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
5073: #endif
5074: }
5075:
1.28 noro 5076: NODE nd_sba_f4(int m,int **indp);
5077:
5078: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 5079: {
5080: VL tv,fv,vv,vc,av;
1.55 noro 5081: NODE fd,fd0,r,r0,t,x,s,xx,nd,nd1,syz;
1.24 noro 5082: int e,max,nvar,i;
5083: NDV b;
5084: int ishomo,nalg,wmax,len;
5085: NMV a;
5086: P p,zp;
5087: Q dmy;
5088: struct order_spec *ord1;
5089: int j;
5090: int *perm;
5091: EPOS oepos;
5092: int obpe,oadv,ompos,cbpe;
1.51 noro 5093: struct oEGT eg0,eg1,egconv,egintred;
1.55 noro 5094: LIST l1,redind;
5095: Z z;
1.24 noro 5096:
5097: nd_module = 0;
5098: nd_demand = 0;
1.35 noro 5099: Nsamesig = 0;
1.24 noro 5100: if ( DP_Multiple )
5101: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5102: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5103: parse_nd_option(vv,current_option);
1.24 noro 5104: if ( m && nd_vc )
5105: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
5106: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5107: switch ( ord->id ) {
5108: case 1:
5109: if ( ord->nv != nvar )
5110: error("nd_sba : invalid order specification");
5111: break;
5112: default:
5113: break;
5114: }
5115: nd_nalg = 0;
5116: nd_init_ord(ord);
5117: // for SIG comparison
5118: initd(ord);
5119: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
5120: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43 noro 5121: if ( OID(BDY(t)) == O_DP ) {
5122: e = dp_getdeg((DP)BDY(t));
5123: max = MAX(e,max);
5124: } else {
5125: e = getdeg(tv->v,(P)BDY(t));
5126: max = MAX(e,max);
5127: }
1.24 noro 5128: }
5129: }
1.25 noro 5130: nd_setup_parameters(nvar,max);
1.24 noro 5131: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5132: ishomo = 1;
5133: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43 noro 5134: if ( OID(BDY(t)) == O_DP ) {
5135: DP zdp;
5136:
5137: if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
5138: else zdp = (DP)BDY(t);
5139: b = (pointer)dptondv(m,zdp);
5140: } else {
5141: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
5142: else zp = (P)BDY(t);
5143: b = (pointer)ptondv(CO,vv,zp);
5144: }
1.24 noro 5145: if ( ishomo )
5146: ishomo = ishomo && ndv_ishomo(b);
5147: if ( m ) ndv_mod(m,b);
5148: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
5149: }
5150: if ( fd0 ) NEXT(fd) = 0;
5151:
5152: if ( !ishomo && homo ) {
5153: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
5154: b = (NDV)BDY(t); len = LEN(b);
5155: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
5156: wmax = MAX(TD(DL(a)),wmax);
5157: }
5158: homogenize_order(ord,nvar,&ord1);
5159: nd_init_ord(ord1);
1.42 noro 5160: // for SIG comparison
5161: initd(ord1);
1.24 noro 5162: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
5163: for ( t = fd0; t; t = NEXT(t) )
5164: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5165: }
5166:
1.39 noro 5167: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.55 noro 5168: if ( nd_gentrace ) {
5169: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
5170: }
1.44 noro 5171: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24 noro 5172: if ( !x ) {
5173: *rp = 0; return;
5174: }
5175: if ( !ishomo && homo ) {
5176: /* dehomogenization */
5177: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5178: nd_init_ord(ord);
1.42 noro 5179: // for SIG comparison
5180: initd(ord);
1.24 noro 5181: nd_setup_parameters(nvar,0);
5182: }
5183: nd_demand = 0;
1.51 noro 5184: get_eg(&eg0);
1.24 noro 5185: x = ndv_reducebase(x,perm);
1.55 noro 5186: for ( nd = 0, i = length(x)-1; i >= 0; i-- ) {
5187: STOZ(perm[i],z); MKNODE(nd1,z,nd); nd = nd1;
5188: }
5189: MKLIST(redind,nd);
1.24 noro 5190: x = ndv_reduceall(m,x);
1.51 noro 5191: get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24 noro 5192: nd_setup_parameters(nd_nvar,0);
1.29 noro 5193: get_eg(&eg0);
1.24 noro 5194: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
5195: NEXTNODE(r0,r);
5196: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 5197: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 5198: }
5199: if ( r0 ) NEXT(r) = 0;
1.44 noro 5200: if ( nd_sba_syz ) {
5201: LIST gb,hsyz;
5202:
5203: MKLIST(gb,r0);
5204: MKLIST(hsyz,syz);
5205: nd = mknode(2,gb,hsyz);
5206: MKLIST(*rp,nd);
1.55 noro 5207: } else if ( nd_gentrace ) {
5208: LIST gb,trace;
5209:
5210: MKLIST(trace,nd_alltracelist);
5211: MKLIST(gb,r0);
5212: nd = mknode(3,gb,redind,trace);
5213: MKLIST(*rp,nd);
1.44 noro 5214: } else
5215: MKLIST(*rp,r0);
1.29 noro 5216: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51 noro 5217: print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29 noro 5218: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 5219: }
5220:
1.1 noro 5221: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
5222: {
5223: VL tv,fv,vv,vc,av;
5224: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5225: int e,max,nvar,i;
5226: NDV b;
5227: int ishomo,nalg;
5228: Alg alpha,dp;
5229: P p,zp;
5230: Q dmy;
5231: LIST f1,f2;
5232: Obj obj;
5233: NumberField nf;
5234: struct order_spec *ord1;
5235: int *perm;
5236:
5237: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5238: parse_nd_option(vv,current_option);
1.1 noro 5239: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5240: switch ( ord->id ) {
5241: case 1:
5242: if ( ord->nv != nvar )
5243: error("nd_check : invalid order specification");
5244: break;
5245: default:
5246: break;
5247: }
5248: nd_nalg = 0;
5249: av = 0;
5250: if ( !m ) {
5251: get_algtree((Obj)f,&av);
5252: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5253: nd_ntrans = nvar;
5254: nd_nalg = nalg;
5255: /* #i -> t#i */
5256: if ( nalg ) {
5257: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5258: ord = ord1;
5259: f = f1;
5260: }
5261: nvar += nalg;
5262: }
5263: nd_init_ord(ord);
5264: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5265: for ( tv = vv; tv; tv = NEXT(tv) ) {
5266: e = getdeg(tv->v,(P)BDY(t));
5267: max = MAX(e,max);
5268: }
5269: nd_setup_parameters(nvar,max);
5270: ishomo = 1;
5271: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5272: ptozp((P)BDY(t),1,&dmy,&zp);
5273: b = (pointer)ptondv(CO,vv,zp);
5274: if ( ishomo )
5275: ishomo = ishomo && ndv_ishomo(b);
5276: if ( m ) ndv_mod(m,b);
5277: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
5278: }
5279: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 5280: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 5281: for ( x = 0, i = 0; i < nd_psn; i++ )
5282: x = update_base(x,i);
5283: if ( do_check ) {
5284: x = nd_gb(m,ishomo,1,0,&perm);
5285: if ( !x ) {
5286: *rp = 0;
5287: return;
5288: }
5289: } else {
5290: #if 0
5291: /* bug ? */
5292: for ( t = x; t; t = NEXT(t) )
5293: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
5294: #else
5295: conv_ilist(0,0,x,&perm);
5296: #endif
5297: }
5298: x = ndv_reducebase(x,perm);
5299: x = ndv_reduceall(m,x);
5300: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
5301: NEXTNODE(r0,r);
5302: BDY(r) = ndvtop(m,CO,vv,BDY(t));
5303: }
5304: if ( r0 ) NEXT(r) = 0;
5305: if ( !m && nd_nalg )
5306: r0 = postprocess_algcoef(av,alist,r0);
5307: MKLIST(*rp,r0);
5308: }
5309:
5310: NDV recompute_trace(NODE trace,NDV *p,int m);
5311: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
5312:
5313: NDV recompute_trace(NODE ti,NDV *p,int mod)
5314: {
5315: int c,c1,c2,i;
5316: NM mul,m,tail;
5317: ND d,r,rm;
5318: NODE sj;
5319: NDV red;
5320: Obj mj;
5321:
5322: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
5323: CM(mul) = 1;
5324: tail = 0;
5325: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
5326: sj = BDY((LIST)BDY(ti));
5327: if ( ARG0(sj) ) {
1.6 noro 5328: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 5329: mj = (Obj)ARG2(sj);
5330: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
5331: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
5332: rm = ndv_mul_nm(mod,mul,red);
5333: if ( !r ) r = rm;
5334: else {
5335: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
5336: if ( d ) {
5337: NEXT(tail) = m; tail = m; LEN(d)++;
5338: } else {
5339: MKND(nd_nvar,m,1,d); tail = BDY(d);
5340: }
5341: }
5342: if ( !m ) return 0; /* failure */
5343: else {
5344: BDY(r) = m;
5345: if ( mod > 0 || mod == -1 ) {
5346: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
5347: DMAR(c1,c2,0,mod,c);
5348: nd_mul_c(mod,rm,c);
5349: } else {
5350: Z t,u;
5351:
5352: chsgnlf(HCZ(r),&t);
5353: divlf(t,HCZ(rm),&u);
5354: nd_mul_c_lf(rm,u);
5355: }
5356: r = nd_add(mod,r,rm);
5357: }
5358: }
5359: }
5360: }
5361: if ( tail ) NEXT(tail) = 0;
5362: d = nd_add(mod,d,r);
5363: nd_mul_c(mod,d,invm(HCM(d),mod));
5364: return ndtondv(mod,d);
5365: }
5366:
5367: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
5368: {
5369: VL tv,fv,vv,vc,av;
5370: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5371: int e,max,nvar,i;
5372: NDV b;
5373: int ishomo,nalg;
5374: Alg alpha,dp;
5375: P p,zp;
5376: Q dmy;
5377: LIST f1,f2;
5378: Obj obj;
5379: NumberField nf;
5380: struct order_spec *ord1;
5381: NODE permtrace,intred,ind,perm,trace,ti;
5382: int len,n,j;
5383: NDV *db,*pb;
5384:
5385: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 5386: parse_nd_option(vv,current_option);
1.1 noro 5387: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5388: switch ( ord->id ) {
5389: case 1:
5390: if ( ord->nv != nvar )
5391: error("nd_check : invalid order specification");
5392: break;
5393: default:
5394: break;
5395: }
5396: nd_init_ord(ord);
1.6 noro 5397: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 5398: nd_setup_parameters(nvar,0);
5399:
5400: len = length(BDY(f));
5401: db = (NDV *)MALLOC(len*sizeof(NDV *));
5402: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
5403: ptozp((P)BDY(t),1,&dmy,&zp);
5404: b = ptondv(CO,vv,zp);
5405: ndv_mod(m,b);
5406: ndv_mul_c(m,b,invm(HCM(b),m));
5407: db[i] = b;
5408: }
5409:
5410: permtrace = BDY((LIST)ARG2(BDY(tlist)));
5411: intred = BDY((LIST)ARG3(BDY(tlist)));
5412: ind = BDY((LIST)ARG4(BDY(tlist)));
5413: perm = BDY((LIST)ARG0(permtrace));
5414: trace = NEXT(permtrace);
5415:
5416: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 5417: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 5418: if ( j > i ) i = j;
5419: }
5420: n = i+1;
5421: pb = (NDV *)MALLOC(n*sizeof(NDV *));
5422: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
5423: ti = BDY((LIST)BDY(t));
1.6 noro 5424: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 5425: }
5426: for ( t = trace; t; t = NEXT(t) ) {
5427: ti = BDY((LIST)BDY(t));
1.6 noro 5428: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5429: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5430: if ( DP_Print ) {
5431: fprintf(asir_out,"."); fflush(asir_out);
5432: }
5433: }
5434: for ( t = intred; t; t = NEXT(t) ) {
5435: ti = BDY((LIST)BDY(t));
1.6 noro 5436: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5437: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5438: if ( DP_Print ) {
5439: fprintf(asir_out,"*"); fflush(asir_out);
5440: }
5441: }
5442: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
5443: NEXTNODE(r0,r);
1.6 noro 5444: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 5445: ndv_mul_c(m,b,invm(HCM(b),m));
5446: #if 0
1.6 noro 5447: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5448: #else
1.6 noro 5449: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5450: #endif
5451: }
5452: if ( r0 ) NEXT(r) = 0;
5453: MKLIST(*rp,r0);
5454: if ( DP_Print ) fprintf(asir_out,"\n");
5455: }
5456:
1.16 noro 5457: 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 5458: {
5459: VL tv,fv,vv,vc,av;
5460: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
5461: int m,nocheck,nvar,mindex,e,max;
5462: NDV c;
5463: NMV a;
5464: P p,zp;
5465: Q dmy;
5466: EPOS oepos;
5467: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
5468: Alg alpha,dp;
5469: P poly;
5470: LIST f1,f2,zpl;
5471: Obj obj;
5472: NumberField nf;
5473: struct order_spec *ord1;
5474: struct oEGT eg_check,eg0,eg1;
5475: NODE tr,tl1,tl2,tl3,tl4;
5476: LIST l1,l2,l3,l4,l5;
5477: int *perm;
5478: int j,ret;
5479: Z jq,bpe;
1.15 noro 5480: VECT hvect;
1.1 noro 5481:
1.41 noro 5482: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 5483: nd_module = 0;
5484: nd_lf = 0;
1.46 noro 5485: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
5486: parse_nd_option(vv,current_option);
1.1 noro 5487: if ( nd_lf ) {
5488: if ( f4 )
5489: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
5490: else
5491: error("nd_gr_trace is not implemented yet over a large finite field");
5492: return;
5493: }
5494: if ( DP_Multiple )
5495: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5496:
5497: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5498: switch ( ord->id ) {
5499: case 1:
5500: if ( ord->nv != nvar )
5501: error("nd_gr_trace : invalid order specification");
5502: break;
5503: default:
5504: break;
5505: }
5506:
5507: get_algtree((Obj)f,&av);
5508: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5509: nd_ntrans = nvar;
5510: nd_nalg = nalg;
5511: /* #i -> t#i */
5512: if ( nalg ) {
5513: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5514: ord = ord1;
5515: f = f1;
5516: }
5517: nvar += nalg;
5518:
5519: nocheck = 0;
5520: mindex = 0;
5521:
5522: if ( Demand ) nd_demand = 1;
5523: else nd_demand = 0;
5524:
5525: /* setup modulus */
5526: if ( trace < 0 ) {
5527: trace = -trace;
5528: nocheck = 1;
5529: }
5530: m = trace > 1 ? trace : get_lprime(mindex);
5531: nd_init_ord(ord);
5532: mrank = 0;
5533: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5534: for ( tv = vv; tv; tv = NEXT(tv) ) {
5535: if ( nd_module ) {
1.16 noro 5536: if ( OID(BDY(t)) == O_DPM ) {
5537: e = dpm_getdeg((DPM)BDY(t),&trank);
5538: max = MAX(e,max);
5539: mrank = MAX(mrank,trank);
5540: } else {
1.1 noro 5541: s = BDY((LIST)BDY(t));
5542: trank = length(s);
5543: mrank = MAX(mrank,trank);
5544: for ( ; s; s = NEXT(s) ) {
5545: e = getdeg(tv->v,(P)BDY(s));
5546: max = MAX(e,max);
5547: }
1.16 noro 5548: }
1.1 noro 5549: } else {
1.43 noro 5550: if ( OID(BDY(t)) == O_DP ) {
5551: e = dp_getdeg((DP)BDY(t));
5552: max = MAX(e,max);
5553: } else {
1.1 noro 5554: e = getdeg(tv->v,(P)BDY(t));
5555: max = MAX(e,max);
1.43 noro 5556: }
1.1 noro 5557: }
5558: }
5559: nd_setup_parameters(nvar,max);
5560: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5561: ishomo = 1;
5562: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5563: if ( nd_module ) {
1.16 noro 5564: if ( OID(BDY(t)) == O_DPM ) {
5565: Z cont;
5566: DPM zdpm;
5567:
1.17 noro 5568: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 5569: else zdpm = (DPM)BDY(t);
5570: c = (pointer)dpmtondv(m,zdpm);
5571: } else {
5572: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
5573: else zpl = (LIST)BDY(t);
1.1 noro 5574: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 5575: }
1.1 noro 5576: } else {
1.43 noro 5577: if ( OID(BDY(t)) == O_DP ) {
5578: DP zdp;
5579:
5580: if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
5581: else zdp = (DP)BDY(t);
5582: c = (pointer)dptondv(m,zdp);
5583: } else {
5584: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
5585: else zp = (P)BDY(t);
5586: c = (pointer)ptondv(CO,vv,zp);
5587: }
1.1 noro 5588: }
5589: if ( ishomo )
5590: ishomo = ishomo && ndv_ishomo(c);
5591: if ( c ) {
5592: NEXTNODE(in0,in); BDY(in) = (pointer)c;
5593: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
5594: }
5595: }
5596: if ( in0 ) NEXT(in) = 0;
5597: if ( fd0 ) NEXT(fd) = 0;
5598: if ( !ishomo && homo ) {
5599: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
5600: c = (NDV)BDY(t); len = LEN(c);
5601: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
5602: wmax = MAX(TD(DL(a)),wmax);
5603: }
5604: homogenize_order(ord,nvar,&ord1);
5605: nd_init_ord(ord1);
5606: nd_setup_parameters(nvar+1,wmax);
5607: for ( t = fd0; t; t = NEXT(t) )
5608: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5609: }
5610: if ( MaxDeg > 0 ) nocheck = 1;
5611: while ( 1 ) {
5612: tl1 = tl2 = tl3 = tl4 = 0;
5613: if ( Demand )
5614: nd_demand = 1;
1.24 noro 5615: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 5616: if ( nd_gentrace ) {
5617: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
5618: }
5619: if ( ret )
5620: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
5621: if ( !ret || !cand ) {
5622: /* failure */
5623: if ( trace > 1 ) { *rp = 0; return; }
5624: else m = get_lprime(++mindex);
5625: continue;
5626: }
1.15 noro 5627: if ( nd_gentrace ) {
5628: MKVECT(hvect,nd_psn);
5629: for ( i = 0; i < nd_psn; i++ )
5630: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
5631: }
1.1 noro 5632: if ( !ishomo && homo ) {
5633: /* dehomogenization */
5634: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5635: nd_init_ord(ord);
5636: nd_setup_parameters(nvar,0);
5637: }
5638: nd_demand = 0;
5639: cand = ndv_reducebase(cand,perm);
5640: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
5641: cand = ndv_reduceall(0,cand);
5642: cbpe = nd_bpe;
5643: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
5644: get_eg(&eg0);
5645: if ( nocheck )
5646: break;
5647: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
5648: if ( nd_gentrace ) {
5649: tl3 = nd_alltracelist; nd_alltracelist = 0;
5650: } else tl3 = 0;
5651: /* gbcheck : cand is a GB of Id(cand) ? */
1.54 noro 5652: if ( nd_vc || nd_gentrace || nd_gensyz || do_weyl )
1.1 noro 5653: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
5654: else
5655: ret = nd_f4(0,1,0)!=0;
5656: if ( nd_gentrace && nd_gensyz ) {
5657: tl4 = nd_alltracelist; nd_alltracelist = 0;
5658: } else tl4 = 0;
5659: }
5660: if ( ret ) break;
5661: else if ( trace > 1 ) {
5662: /* failure */
5663: *rp = 0; return;
5664: } else {
5665: /* try the next modulus */
5666: m = get_lprime(++mindex);
5667: /* reset the parameters */
5668: if ( !ishomo && homo ) {
5669: nd_init_ord(ord1);
5670: nd_setup_parameters(nvar+1,wmax);
5671: } else {
5672: nd_init_ord(ord);
5673: nd_setup_parameters(nvar,max);
5674: }
5675: }
5676: }
5677: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
5678: if ( DP_Print )
1.6 noro 5679: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 5680: /* dp->p */
5681: nd_bpe = cbpe;
5682: nd_setup_parameters(nd_nvar,0);
5683: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 5684: if ( nd_module ) {
1.17 noro 5685: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 5686: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 5687: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
5688: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 5689: }
5690: if ( nd_nalg )
5691: cand = postprocess_algcoef(av,alist,cand);
5692: MKLIST(*rp,cand);
5693: if ( nd_gentrace ) {
5694: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5695: tl3 = reverse_node(tl3);
5696: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5697: for ( t = tl2; t; t = NEXT(t) ) {
5698: /* s = [i,[*,j,*,*],...] */
5699: s = BDY((LIST)BDY(t));
1.6 noro 5700: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5701: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5702: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5703: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5704: }
5705: }
5706: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5707: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5708: }
5709: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5710: MKLIST(l5,tl4);
1.6 noro 5711: STOZ(nd_bpe,bpe);
1.15 noro 5712: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5713: }
5714: }
5715:
5716: /* XXX : module element is not considered */
5717:
5718: void dltondl(int n,DL dl,UINT *r)
5719: {
5720: UINT *d;
5721: int i,j,l,s,ord_l;
5722: struct order_pair *op;
5723:
5724: d = (unsigned int *)dl->d;
5725: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5726: if ( nd_blockmask ) {
5727: l = nd_blockmask->n;
5728: op = nd_blockmask->order_pair;
5729: for ( j = 0, s = 0; j < l; j++ ) {
5730: ord_l = op[j].length;
5731: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5732: }
5733: TD(r) = ndl_weight(r);
5734: ndl_weight_mask(r);
5735: } else {
5736: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5737: TD(r) = ndl_weight(r);
5738: }
5739: }
5740:
5741: DL ndltodl(int n,UINT *ndl)
5742: {
5743: DL dl;
5744: int *d;
5745: int i,j,l,s,ord_l;
5746: struct order_pair *op;
5747:
1.33 noro 5748: NEWDL_NOINIT(dl,n);
1.1 noro 5749: dl->td = TD(ndl);
5750: d = dl->d;
5751: if ( nd_blockmask ) {
5752: l = nd_blockmask->n;
5753: op = nd_blockmask->order_pair;
5754: for ( j = 0, s = 0; j < l; j++ ) {
5755: ord_l = op[j].length;
5756: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5757: }
5758: } else {
5759: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5760: }
5761: return dl;
5762: }
5763:
1.24 noro 5764: void _ndltodl(UINT *ndl,DL dl)
5765: {
5766: int *d;
5767: int i,j,l,s,ord_l,n;
5768: struct order_pair *op;
5769:
5770: n = nd_nvar;
5771: dl->td = TD(ndl);
5772: d = dl->d;
5773: if ( nd_blockmask ) {
5774: l = nd_blockmask->n;
5775: op = nd_blockmask->order_pair;
5776: for ( j = 0, s = 0; j < l; j++ ) {
5777: ord_l = op[j].length;
5778: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5779: }
5780: } else {
5781: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5782: }
5783: }
5784:
1.1 noro 5785: void nmtodp(int mod,NM m,DP *r)
5786: {
5787: DP dp;
5788: MP mr;
5789:
5790: NEWMP(mr);
5791: mr->dl = ndltodl(nd_nvar,DL(m));
5792: mr->c = (Obj)ndctop(mod,m->c);
5793: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5794: *r = dp;
5795: }
5796:
1.15 noro 5797: void ndltodp(UINT *d,DP *r)
5798: {
5799: DP dp;
5800: MP mr;
5801:
5802: NEWMP(mr);
5803: mr->dl = ndltodl(nd_nvar,d);
5804: mr->c = (Obj)ONE;
5805: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5806: *r = dp;
5807: }
5808:
1.1 noro 5809: void ndl_print(UINT *dl)
5810: {
5811: int n;
5812: int i,j,l,ord_l,s,s0;
5813: struct order_pair *op;
5814:
5815: n = nd_nvar;
5816: printf("<<");
5817: if ( nd_blockmask ) {
5818: l = nd_blockmask->n;
5819: op = nd_blockmask->order_pair;
5820: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5821: ord_l = op[j].length;
5822: for ( i = 0; i < ord_l; i++, s++ )
5823: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5824: }
5825: } else {
5826: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5827: }
5828: printf(">>");
5829: if ( nd_module && MPOS(dl) )
5830: printf("*e%d",MPOS(dl));
5831: }
5832:
5833: void nd_print(ND p)
5834: {
5835: NM m;
5836:
5837: if ( !p )
5838: printf("0\n");
5839: else {
5840: for ( m = BDY(p); m; m = NEXT(m) ) {
5841: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5842: else printf("+%ld*",CM(m));
1.1 noro 5843: ndl_print(DL(m));
5844: }
5845: printf("\n");
5846: }
5847: }
5848:
5849: void nd_print_q(ND p)
5850: {
5851: NM m;
5852:
5853: if ( !p )
5854: printf("0\n");
5855: else {
5856: for ( m = BDY(p); m; m = NEXT(m) ) {
5857: printf("+");
1.6 noro 5858: printexpr(CO,(Obj)CZ(m));
1.1 noro 5859: printf("*");
5860: ndl_print(DL(m));
5861: }
5862: printf("\n");
5863: }
5864: }
5865:
5866: void ndp_print(ND_pairs d)
5867: {
5868: ND_pairs t;
5869:
5870: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5871: printf("\n");
5872: }
5873:
5874: void nd_removecont(int mod,ND p)
5875: {
5876: int i,n;
5877: Z *w;
5878: NM m;
5879: struct oVECT v;
5880:
5881: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5882: else if ( mod == -2 ) {
5883: Z inv;
5884: divlf(ONE,HCZ(p),&inv);
5885: nd_mul_c_lf(p,inv);
5886: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5887: else {
5888: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5889: w = (Z *)MALLOC(n*sizeof(Q));
5890: v.len = n;
5891: v.body = (pointer *)w;
1.6 noro 5892: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5893: removecont_array((P *)w,n,1);
1.6 noro 5894: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5895: }
5896: }
5897:
5898: void nd_removecont2(ND p1,ND p2)
5899: {
5900: int i,n1,n2,n;
5901: Z *w;
5902: NM m;
5903: struct oVECT v;
5904:
5905: n1 = nd_length(p1);
5906: n2 = nd_length(p2);
5907: n = n1+n2;
5908: w = (Z *)MALLOC(n*sizeof(Q));
5909: v.len = n;
5910: v.body = (pointer *)w;
5911: i = 0;
5912: if ( p1 )
1.6 noro 5913: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5914: if ( p2 )
1.6 noro 5915: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5916: removecont_array((P *)w,n,1);
5917: i = 0;
5918: if ( p1 )
1.6 noro 5919: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5920: if ( p2 )
1.6 noro 5921: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5922: }
5923:
5924: void ndv_removecont(int mod,NDV p)
5925: {
5926: int i,len,all_p;
5927: Z *c;
5928: P *w;
5929: Z dvr,t;
5930: P g,cont,tp;
5931: NMV m;
5932:
5933: if ( mod == -1 )
5934: ndv_mul_c(mod,p,_invsf(HCM(p)));
5935: else if ( mod == -2 ) {
5936: Z inv;
5937: divlf(ONE,HCZ(p),&inv);
5938: ndv_mul_c_lf(p,inv);
5939: } else if ( mod )
5940: ndv_mul_c(mod,p,invm(HCM(p),mod));
5941: else {
5942: len = p->len;
5943: w = (P *)MALLOC(len*sizeof(P));
5944: c = (Z *)MALLOC(len*sizeof(Q));
5945: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5946: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5947: all_p = all_p && !NUM(w[i]);
5948: }
5949: if ( all_p ) {
5950: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5951: mulp(nd_vc,(P)dvr,g,&cont);
5952: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5953: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5954: }
5955: } else {
5956: sortbynm((Q *)c,len);
5957: qltozl((Q *)c,len,&dvr);
5958: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5959: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5960: }
5961: }
5962: }
5963: }
5964:
5965: /* koko */
5966:
5967: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5968: {
5969: int len,i,max;
5970: NMV m,mr0,mr,t;
5971:
5972: len = p->len;
1.14 noro 5973: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5974: max = MAX(max,TD(DL(m)));
5975: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5976: m = (NMV)((char *)mr0+(len-1)*oadv);
5977: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5978: t = (NMV)MALLOC(nmv_adv);
5979: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5980: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5981: CZ(mr) = CZ(m);
1.1 noro 5982: ndl_copy(DL(t),DL(mr));
5983: }
5984: NV(p)++;
5985: BDY(p) = mr0;
5986: }
5987:
5988: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5989: {
5990: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5991: int pos;
5992: Q *w;
5993: Q dvr,t;
5994: NMV m,r;
5995:
5996: len = p->len;
5997: newnvar = nd_nvar-1;
5998: newexporigin = nd_get_exporigin(ord);
5999: if ( nd_module ) newmpos = newexporigin-1;
6000: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
6001: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
6002: ndl_dehomogenize(DL(m));
6003: if ( newwpd != nd_wpd ) {
6004: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
6005: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 6006: CZ(r) = CZ(m);
1.1 noro 6007: if ( nd_module ) pos = MPOS(DL(m));
6008: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
6009: adj = nd_exporigin-newexporigin;
6010: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
6011: if ( nd_module ) {
6012: DL(r)[newmpos] = pos;
6013: }
6014: }
6015: }
6016: NV(p)--;
6017: }
6018:
6019: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
6020: {
6021: int i;
6022: P *tpl,*tpl1;
6023: NODE l;
6024: P h,gcd,t;
6025:
6026: tpl = (P *)MALLOC(m*sizeof(P));
6027: tpl1 = (P *)MALLOC(m*sizeof(P));
6028: bcopy(pl,tpl,m*sizeof(P));
6029: gcd = (P)ONE;
6030: for ( l = nd_hcf; l; l = NEXT(l) ) {
6031: h = (P)BDY(l);
6032: while ( 1 ) {
6033: for ( i = 0; i < m; i++ )
6034: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
6035: break;
6036: if ( i == m ) {
6037: bcopy(tpl1,tpl,m*sizeof(P));
6038: mulp(vl,gcd,h,&t); gcd = t;
6039: } else
6040: break;
6041: }
6042: }
6043: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
6044: if ( full ) {
6045: heu_nezgcdnpz(vl,tpl,m,&t);
6046: mulp(vl,gcd,t,pr);
6047: } else
6048: *pr = gcd;
6049: }
6050:
6051: void removecont_array(P *p,int n,int full)
6052: {
6053: int all_p,all_q,i;
6054: Z *c;
6055: P *w;
6056: P t,s;
6057:
6058: for ( all_q = 1, i = 0; i < n; i++ )
6059: all_q = all_q && NUM(p[i]);
6060: if ( all_q ) {
6061: removecont_array_q((Z *)p,n);
6062: } else {
6063: c = (Z *)MALLOC(n*sizeof(Z));
6064: w = (P *)MALLOC(n*sizeof(P));
6065: for ( i = 0; i < n; i++ ) {
6066: ptozp(p[i],1,(Q *)&c[i],&w[i]);
6067: }
6068: removecont_array_q(c,n);
6069: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
6070: for ( i = 0; i < n; i++ ) {
6071: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
6072: }
6073: }
6074: }
6075:
6076: /* c is an int array */
6077:
6078: void removecont_array_q(Z *c,int n)
6079: {
6080: struct oVECT v;
6081: Z d0,d1,a,u,u1,gcd;
6082: int i,j;
6083: Z *q,*r;
6084:
6085: q = (Z *)MALLOC(n*sizeof(Z));
6086: r = (Z *)MALLOC(n*sizeof(Z));
6087: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
6088: gcdvz_estimate(&v,&d0);
6089: for ( i = 0; i < n; i++ ) {
6090: divqrz(c[i],d0,&q[i],&r[i]);
6091: }
6092: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
6093: if ( i < n ) {
6094: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
6095: gcdvz(&v,&d1);
6096: gcdz(d0,d1,&gcd);
1.6 noro 6097: /* exact division */
6098: divsz(d0,gcd,&a);
1.1 noro 6099: for ( i = 0; i < n; i++ ) {
6100: mulz(a,q[i],&u);
6101: if ( r[i] ) {
1.6 noro 6102: /* exact division */
6103: divsz(r[i],gcd,&u1);
1.1 noro 6104: addz(u,u1,&q[i]);
6105: } else
6106: q[i] = u;
6107: }
6108: }
6109: for ( i = 0; i < n; i++ ) c[i] = q[i];
6110: }
6111:
1.4 noro 6112: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
6113:
6114: void mpz_removecont_array(mpz_t *c,int n)
6115: {
6116: mpz_t d0,a,u,u1,gcd;
6117: int i,j;
1.13 noro 6118: static mpz_t *q,*r;
6119: static int c_len = 0;
1.4 noro 6120:
6121: for ( i = 0; i < n; i++ )
6122: if ( mpz_sgn(c[i]) ) break;
6123: if ( i == n ) return;
6124: gcdv_mpz_estimate(d0,c,n);
1.13 noro 6125: if ( n > c_len ) {
6126: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
6127: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
6128: c_len = n;
6129: }
1.4 noro 6130: for ( i = 0; i < n; i++ ) {
6131: mpz_init(q[i]); mpz_init(r[i]);
6132: mpz_fdiv_qr(q[i],r[i],c[i],d0);
6133: }
6134: for ( i = 0; i < n; i++ )
6135: if ( mpz_sgn(r[i]) ) break;
6136: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
6137: if ( i < n ) {
6138: mpz_gcd(gcd,d0,r[i]);
6139: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
6140: mpz_div(a,d0,gcd);
6141: for ( i = 0; i < n; i++ ) {
6142: mpz_mul(u,a,q[i]);
6143: if ( mpz_sgn(r[i]) ) {
6144: mpz_div(u1,r[i],gcd);
6145: mpz_add(q[i],u,u1);
6146: } else
6147: mpz_set(q[i],u);
6148: }
6149: }
6150: for ( i = 0; i < n; i++ )
6151: mpz_set(c[i],q[i]);
6152: }
6153:
1.1 noro 6154: void nd_mul_c(int mod,ND p,int mul)
6155: {
6156: NM m;
6157: int c,c1;
6158:
6159: if ( !p ) return;
6160: if ( mul == 1 ) return;
6161: if ( mod == -1 )
6162: for ( m = BDY(p); m; m = NEXT(m) )
6163: CM(m) = _mulsf(CM(m),mul);
6164: else
6165: for ( m = BDY(p); m; m = NEXT(m) ) {
6166: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6167: }
6168: }
6169:
6170: void nd_mul_c_lf(ND p,Z mul)
6171: {
6172: NM m;
6173: Z c;
6174:
6175: if ( !p ) return;
6176: if ( UNIZ(mul) ) return;
6177: for ( m = BDY(p); m; m = NEXT(m) ) {
6178: mullf(CZ(m),mul,&c); CZ(m) = c;
6179: }
6180: }
6181:
6182: void nd_mul_c_q(ND p,P mul)
6183: {
6184: NM m;
6185: P c;
6186:
6187: if ( !p ) return;
6188: if ( UNIQ(mul) ) return;
6189: for ( m = BDY(p); m; m = NEXT(m) ) {
6190: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
6191: }
6192: }
6193:
6194: void nd_mul_c_p(VL vl,ND p,P mul)
6195: {
6196: NM m;
6197: P c;
6198:
6199: if ( !p ) return;
6200: for ( m = BDY(p); m; m = NEXT(m) ) {
6201: mulp(vl,CP(m),mul,&c); CP(m) = c;
6202: }
6203: }
6204:
6205: void nd_free(ND p)
6206: {
6207: NM t,s;
6208:
6209: if ( !p ) return;
6210: t = BDY(p);
6211: while ( t ) {
6212: s = NEXT(t);
6213: FREENM(t);
6214: t = s;
6215: }
6216: FREEND(p);
6217: }
6218:
6219: void ndv_free(NDV p)
6220: {
6221: GCFREE(BDY(p));
6222: }
6223:
6224: void nd_append_red(UINT *d,int i)
6225: {
6226: RHist m,m0;
6227: int h;
6228:
6229: NEWRHist(m);
6230: h = ndl_hash_value(d);
6231: m->index = i;
6232: ndl_copy(d,DL(m));
6233: NEXT(m) = nd_red[h];
6234: nd_red[h] = m;
6235: }
6236:
6237: UINT *ndv_compute_bound(NDV p)
6238: {
6239: UINT *d1,*d2,*t;
6240: UINT u;
6241: int i,j,k,l,len,ind;
6242: NMV m;
6243:
6244: if ( !p )
6245: return 0;
6246: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6247: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6248: len = LEN(p);
6249: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
6250: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
6251: ndl_max(DL(m),d1,d2);
6252: t = d1; d1 = d2; d2 = t;
6253: }
6254: l = nd_nvar+31;
6255: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
6256: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
6257: u = d1[i];
6258: k = (nd_epw-1)*nd_bpe;
6259: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
6260: t[ind] = (u>>k)&nd_mask0;
6261: }
6262: for ( ; ind < l; ind++ ) t[ind] = 0;
6263: return t;
6264: }
6265:
6266: UINT *nd_compute_bound(ND p)
6267: {
6268: UINT *d1,*d2,*t;
6269: UINT u;
6270: int i,j,k,l,len,ind;
6271: NM m;
6272:
6273: if ( !p )
6274: return 0;
6275: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6276: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6277: len = LEN(p);
6278: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
6279: for ( m = NEXT(m); m; m = NEXT(m) ) {
6280: ndl_lcm(DL(m),d1,d2);
6281: t = d1; d1 = d2; d2 = t;
6282: }
6283: l = nd_nvar+31;
6284: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
6285: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
6286: u = d1[i];
6287: k = (nd_epw-1)*nd_bpe;
6288: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
6289: t[ind] = (u>>k)&nd_mask0;
6290: }
6291: for ( ; ind < l; ind++ ) t[ind] = 0;
6292: return t;
6293: }
6294:
6295: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
6296: /* of a term. In this case we need additional 1 word. */
6297:
6298: int nd_get_exporigin(struct order_spec *ord)
6299: {
6300: switch ( ord->id ) {
1.21 noro 6301: case 0: case 2: case 256: case 258: case 300:
1.1 noro 6302: return 1+nd_module;
6303: case 1: case 257:
6304: /* block order */
6305: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
6306: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
6307: return ord->ord.block.length+1+nd_module;
6308: case 3: case 259:
6309: #if 0
6310: error("nd_get_exporigin : composite order is not supported yet.");
6311: #else
6312: return 1+nd_module;
6313: #endif
6314: default:
6315: error("nd_get_exporigin : ivalid argument.");
6316: return 0;
6317: }
6318: }
6319:
6320: void nd_setup_parameters(int nvar,int max) {
6321: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
6322: struct order_pair *op;
1.45 noro 6323: extern int CNVars;
1.1 noro 6324:
6325: nd_nvar = nvar;
1.45 noro 6326: CNVars = nvar;
1.1 noro 6327: if ( max ) {
6328: /* XXX */
6329: if ( do_weyl ) nd_bpe = 32;
6330: else if ( max < 2 ) nd_bpe = 1;
6331: else if ( max < 4 ) nd_bpe = 2;
6332: else if ( max < 8 ) nd_bpe = 3;
6333: else if ( max < 16 ) nd_bpe = 4;
6334: else if ( max < 32 ) nd_bpe = 5;
6335: else if ( max < 64 ) nd_bpe = 6;
6336: else if ( max < 256 ) nd_bpe = 8;
6337: else if ( max < 1024 ) nd_bpe = 10;
6338: else if ( max < 65536 ) nd_bpe = 16;
6339: else nd_bpe = 32;
6340: }
6341: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
6342: UINT t;
6343: int st;
6344: int *v;
6345: /* t = max(weights) */
6346: t = 0;
6347: if ( current_dl_weight_vector )
6348: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
6349: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
6350: if ( t < st ) t = st;
6351: }
6352: if ( nd_matrix )
6353: for ( i = 0; i < nd_matrix_len; i++ )
6354: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
6355: if ( (st=v[j]) < 0 ) st = -st;
6356: if ( t < st ) t = st;
6357: }
6358: /* i = bitsize of t */
6359: for ( i = 0; t; t >>=1, i++ );
6360: /* i += bitsize of nd_nvar */
6361: for ( t = nd_nvar; t; t >>=1, i++);
6362: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
6363: if ( (nd_bpe+i) >= 31 )
6364: error("nd_setup_parameters : too large weight");
6365: }
6366: nd_epw = (sizeof(UINT)*8)/nd_bpe;
6367: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
6368: nd_exporigin = nd_get_exporigin(nd_ord);
6369: wpd = nd_exporigin+elen;
6370: if ( nd_module )
6371: nd_mpos = nd_exporigin-1;
6372: else
6373: nd_mpos = -1;
6374: if ( wpd != nd_wpd ) {
6375: nd_free_private_storage();
6376: nd_wpd = wpd;
6377: }
6378: if ( nd_bpe < 32 ) {
6379: nd_mask0 = (1<<nd_bpe)-1;
6380: } else {
6381: nd_mask0 = 0xffffffff;
6382: }
6383: bzero(nd_mask,sizeof(nd_mask));
6384: nd_mask1 = 0;
6385: for ( i = 0; i < nd_epw; i++ ) {
6386: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
6387: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
6388: }
6389: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
6390: nd_epos = nd_create_epos(nd_ord);
6391: nd_blockmask = nd_create_blockmask(nd_ord);
6392: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
6393: }
6394:
6395: ND_pairs nd_reconstruct(int trace,ND_pairs d)
6396: {
6397: int i,obpe,oadv,h;
6398: static NM prev_nm_free_list;
6399: static ND_pairs prev_ndp_free_list;
6400: RHist mr0,mr;
6401: RHist r;
6402: RHist *old_red;
6403: ND_pairs s0,s,t;
6404: EPOS oepos;
6405:
6406: obpe = nd_bpe;
6407: oadv = nmv_adv;
6408: oepos = nd_epos;
6409: if ( obpe < 2 ) nd_bpe = 2;
6410: else if ( obpe < 3 ) nd_bpe = 3;
6411: else if ( obpe < 4 ) nd_bpe = 4;
6412: else if ( obpe < 5 ) nd_bpe = 5;
6413: else if ( obpe < 6 ) nd_bpe = 6;
6414: else if ( obpe < 8 ) nd_bpe = 8;
6415: else if ( obpe < 10 ) nd_bpe = 10;
6416: else if ( obpe < 16 ) nd_bpe = 16;
6417: else if ( obpe < 32 ) nd_bpe = 32;
6418: else error("nd_reconstruct : exponent too large");
6419:
6420: nd_setup_parameters(nd_nvar,0);
6421: prev_nm_free_list = _nm_free_list;
6422: prev_ndp_free_list = _ndp_free_list;
6423: _nm_free_list = 0;
6424: _ndp_free_list = 0;
6425: for ( i = nd_psn-1; i >= 0; i-- ) {
6426: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6427: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6428: }
6429: if ( trace )
6430: for ( i = nd_psn-1; i >= 0; i-- ) {
6431: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6432: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6433: }
6434: s0 = 0;
6435: for ( t = d; t; t = NEXT(t) ) {
6436: NEXTND_pairs(s0,s);
6437: s->i1 = t->i1;
6438: s->i2 = t->i2;
1.24 noro 6439: s->sig = t->sig;
1.1 noro 6440: SG(s) = SG(t);
6441: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6442: }
6443:
6444: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6445: for ( i = 0; i < REDTAB_LEN; i++ ) {
6446: old_red[i] = nd_red[i];
6447: nd_red[i] = 0;
6448: }
6449: for ( i = 0; i < REDTAB_LEN; i++ )
6450: for ( r = old_red[i]; r; r = NEXT(r) ) {
6451: NEWRHist(mr);
6452: mr->index = r->index;
6453: SG(mr) = SG(r);
6454: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6455: h = ndl_hash_value(DL(mr));
6456: NEXT(mr) = nd_red[h];
6457: nd_red[h] = mr;
1.24 noro 6458: mr->sig = r->sig;
1.1 noro 6459: }
6460: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6461: old_red = 0;
6462: for ( i = 0; i < nd_psn; i++ ) {
6463: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6464: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 6465: r->sig = nd_psh[i]->sig;
1.1 noro 6466: nd_psh[i] = r;
6467: }
6468: if ( s0 ) NEXT(s) = 0;
6469: prev_nm_free_list = 0;
6470: prev_ndp_free_list = 0;
6471: #if 0
6472: GC_gcollect();
6473: #endif
6474: return s0;
6475: }
6476:
1.30 noro 6477: void nd_reconstruct_s(int trace,ND_pairs *d)
6478: {
6479: int i,obpe,oadv,h;
6480: static NM prev_nm_free_list;
6481: static ND_pairs prev_ndp_free_list;
6482: RHist mr0,mr;
6483: RHist r;
6484: RHist *old_red;
6485: ND_pairs s0,s,t;
6486: EPOS oepos;
6487:
6488: obpe = nd_bpe;
6489: oadv = nmv_adv;
6490: oepos = nd_epos;
6491: if ( obpe < 2 ) nd_bpe = 2;
6492: else if ( obpe < 3 ) nd_bpe = 3;
6493: else if ( obpe < 4 ) nd_bpe = 4;
6494: else if ( obpe < 5 ) nd_bpe = 5;
6495: else if ( obpe < 6 ) nd_bpe = 6;
6496: else if ( obpe < 8 ) nd_bpe = 8;
6497: else if ( obpe < 10 ) nd_bpe = 10;
6498: else if ( obpe < 16 ) nd_bpe = 16;
6499: else if ( obpe < 32 ) nd_bpe = 32;
6500: else error("nd_reconstruct_s : exponent too large");
6501:
6502: nd_setup_parameters(nd_nvar,0);
6503: prev_nm_free_list = _nm_free_list;
6504: prev_ndp_free_list = _ndp_free_list;
6505: _nm_free_list = 0;
6506: _ndp_free_list = 0;
6507: for ( i = nd_psn-1; i >= 0; i-- ) {
6508: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6509: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6510: }
6511: if ( trace )
6512: for ( i = nd_psn-1; i >= 0; i-- ) {
6513: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6514: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6515: }
6516:
6517: for ( i = 0; i < nd_nbase; i++ ) {
6518: s0 = 0;
6519: for ( t = d[i]; t; t = NEXT(t) ) {
6520: NEXTND_pairs(s0,s);
6521: s->i1 = t->i1;
6522: s->i2 = t->i2;
6523: s->sig = t->sig;
6524: SG(s) = SG(t);
6525: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6526: }
1.47 noro 6527: if ( s0 ) NEXT(s) = 0;
1.30 noro 6528: d[i] = s0;
6529: }
6530:
6531: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6532: for ( i = 0; i < REDTAB_LEN; i++ ) {
6533: old_red[i] = nd_red[i];
6534: nd_red[i] = 0;
6535: }
6536: for ( i = 0; i < REDTAB_LEN; i++ )
6537: for ( r = old_red[i]; r; r = NEXT(r) ) {
6538: NEWRHist(mr);
6539: mr->index = r->index;
6540: SG(mr) = SG(r);
6541: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6542: h = ndl_hash_value(DL(mr));
6543: NEXT(mr) = nd_red[h];
6544: nd_red[h] = mr;
6545: mr->sig = r->sig;
6546: }
6547: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6548: old_red = 0;
6549: for ( i = 0; i < nd_psn; i++ ) {
6550: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6551: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
6552: r->sig = nd_psh[i]->sig;
6553: nd_psh[i] = r;
6554: }
6555: if ( s0 ) NEXT(s) = 0;
6556: prev_nm_free_list = 0;
6557: prev_ndp_free_list = 0;
6558: #if 0
6559: GC_gcollect();
6560: #endif
6561: }
6562:
1.1 noro 6563: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
6564: {
6565: int n,i,ei,oepw,omask0,j,s,ord_l,l;
6566: struct order_pair *op;
6567:
6568: n = nd_nvar;
6569: oepw = (sizeof(UINT)*8)/obpe;
6570: omask0 = (1<<obpe)-1;
6571: TD(r) = TD(d);
6572: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
6573: if ( nd_blockmask ) {
6574: l = nd_blockmask->n;
6575: op = nd_blockmask->order_pair;
6576: for ( i = 1; i < nd_exporigin; i++ )
6577: r[i] = d[i];
6578: for ( j = 0, s = 0; j < l; j++ ) {
6579: ord_l = op[j].length;
6580: for ( i = 0; i < ord_l; i++, s++ ) {
6581: ei = GET_EXP_OLD(d,s);
6582: PUT_EXP(r,s,ei);
6583: }
6584: }
6585: } else {
6586: for ( i = 0; i < n; i++ ) {
6587: ei = GET_EXP_OLD(d,i);
6588: PUT_EXP(r,i,ei);
6589: }
6590: }
6591: if ( nd_module ) MPOS(r) = MPOS(d);
6592: }
6593:
6594: ND nd_copy(ND p)
6595: {
6596: NM m,mr,mr0;
6597: int c,n;
6598: ND r;
6599:
6600: if ( !p )
6601: return 0;
6602: else {
6603: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
6604: NEXTNM(mr0,mr);
6605: CM(mr) = CM(m);
6606: ndl_copy(DL(m),DL(mr));
6607: }
6608: NEXT(mr) = 0;
6609: MKND(NV(p),mr0,LEN(p),r);
6610: SG(r) = SG(p);
6611: return r;
6612: }
6613: }
6614:
6615: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
6616: {
6617: NM m1,m2;
6618: NDV p1,p2;
6619: ND t1,t2;
6620: UINT *lcm;
6621: P gp,tp;
6622: Z g,t;
6623: Z iq;
6624: int td;
6625: LIST hist;
6626: NODE node;
6627: DP d;
6628:
6629: if ( !mod && nd_demand ) {
6630: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
6631: } else {
6632: if ( trace ) {
6633: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
6634: } else {
6635: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
6636: }
6637: }
6638: lcm = LCM(p);
6639: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
6640: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
6641: FREENM(m1); return 0;
6642: }
6643: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
6644: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
6645: FREENM(m1); FREENM(m2); return 0;
6646: }
6647:
6648: if ( mod == -1 ) {
6649: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
6650: } else if ( mod > 0 ) {
6651: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
6652: } else if ( mod == -2 ) {
6653: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
6654: } else if ( nd_vc ) {
6655: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
6656: divsp(nd_vc,HCP(p2),gp,&CP(m1));
6657: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
6658: } else {
1.6 noro 6659: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 6660: }
6661: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
6662: *rp = nd_add(mod,t1,t2);
6663: if ( nd_gentrace ) {
6664: /* nd_tracelist is initialized */
1.6 noro 6665: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6666: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 6667: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6668: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
6669: nd_tracelist = node;
6670: }
1.24 noro 6671: if ( *rp )
6672: (*rp)->sig = p->sig;
1.1 noro 6673: FREENM(m1); FREENM(m2);
6674: return 1;
6675: }
6676:
6677: void ndv_mul_c(int mod,NDV p,int mul)
6678: {
6679: NMV m;
6680: int c,c1,len,i;
6681:
6682: if ( !p ) return;
6683: len = LEN(p);
6684: if ( mod == -1 )
6685: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
6686: CM(m) = _mulsf(CM(m),mul);
6687: else
6688: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6689: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6690: }
6691: }
6692:
6693: void ndv_mul_c_lf(NDV p,Z mul)
6694: {
6695: NMV m;
6696: Z c;
6697: int len,i;
6698:
6699: if ( !p ) return;
6700: len = LEN(p);
6701: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6702: mullf(CZ(m),mul,&c); CZ(m) = c;
6703: }
6704: }
6705:
6706: /* for nd_det */
6707: void ndv_mul_c_q(NDV p,Z mul)
6708: {
6709: NMV m;
6710: Z c;
6711: int len,i;
6712:
6713: if ( !p ) return;
6714: len = LEN(p);
6715: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6716: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6717: }
6718: }
6719:
6720: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6721: int n2,i,j,l,n,tlen;
6722: UINT *d0;
6723: NM *tab,*psum;
6724: ND s,r;
6725: NM t;
6726: NMV m1;
6727:
6728: if ( !p ) return 0;
6729: n = NV(p); n2 = n>>1;
6730: d0 = DL(m0);
6731: l = LEN(p);
6732: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6733: tab = (NM *)MALLOC(tlen*sizeof(NM));
6734: psum = (NM *)MALLOC(tlen*sizeof(NM));
6735: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6736: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6737: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6738: /* m0(NM) * m1(NMV) => tab(NM) */
6739: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6740: for ( j = 0; j < tlen; j++ ) {
6741: if ( tab[j] ) {
6742: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6743: }
6744: }
6745: }
6746: for ( i = tlen-1, r = 0; i >= 0; i-- )
6747: if ( psum[i] ) {
6748: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6749: MKND(n,psum[i],j,s);
6750: r = nd_add(mod,r,s);
6751: }
6752: if ( r ) SG(r) = SG(p)+TD(d0);
6753: return r;
6754: }
6755:
6756: /* product of monomials */
6757: /* XXX block order is not handled correctly */
6758:
6759: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6760: {
6761: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6762: UINT *d0,*d1,*d,*dt,*ctab;
6763: Z *ctab_q;
6764: Z q,q1;
6765: UINT c0,c1,c;
6766: NM *p;
6767: NM m,t;
6768: int mpos;
6769:
6770: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6771: if ( !m0 || !m1 ) return;
6772: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6773: if ( nd_module )
6774: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6775:
6776: NEWNM(m); d = DL(m);
6777: if ( mod ) {
6778: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6779: } else if ( nd_vc )
6780: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6781: else
1.6 noro 6782: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6783: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6784: homo = n&1 ? 1 : 0;
6785: if ( homo ) {
6786: /* offset of h-degree */
6787: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6788: PUT_EXP(DL(m),n-1,h);
6789: TD(DL(m)) = h;
6790: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6791: }
6792: tab[0] = m;
6793: NEWNM(m); d = DL(m);
6794: for ( i = 0, curlen = 1; i < n2; i++ ) {
6795: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6796: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6797: /* xi^a*(Di^k*xi^l)*Di^b */
6798: a += l; b += k;
6799: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6800: if ( !k || !l ) {
6801: for ( j = 0; j < curlen; j++ )
6802: if ( (t = tab[j]) != 0 ) {
6803: dt = DL(t);
6804: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6805: if ( nd_blockmask ) ndl_weight_mask(dt);
6806: }
6807: curlen *= k+1;
6808: continue;
6809: }
6810: min = MIN(k,l);
6811: if ( mod ) {
6812: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6813: mkwcm(k,l,mod,(int *)ctab);
6814: } else {
6815: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6816: mkwc(k,l,ctab_q);
6817: }
6818: for ( j = min; j >= 0; j-- ) {
6819: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6820: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6821: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6822: if ( homo ) {
6823: TD(d) = s;
6824: PUT_EXP(d,n-1,s-h);
6825: } else TD(d) = h;
6826: if ( nd_blockmask ) ndl_weight_mask(d);
6827: if ( mod ) c = ctab[j];
6828: else q = ctab_q[j];
6829: p = tab+curlen*j;
6830: if ( j == 0 ) {
6831: for ( u = 0; u < curlen; u++, p++ ) {
6832: if ( tab[u] ) {
6833: ndl_addto(DL(tab[u]),d);
6834: if ( mod ) {
6835: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6836: } else if ( nd_vc )
6837: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6838: else {
1.6 noro 6839: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6840: }
6841: }
6842: }
6843: } else {
6844: for ( u = 0; u < curlen; u++, p++ ) {
6845: if ( tab[u] ) {
6846: NEWNM(t);
6847: ndl_add(DL(tab[u]),d,DL(t));
6848: if ( mod ) {
6849: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6850: } else if ( nd_vc )
6851: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6852: else
1.6 noro 6853: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6854: *p = t;
6855: }
6856: }
6857: }
6858: }
6859: curlen *= k+1;
6860: }
6861: FREENM(m);
6862: if ( nd_module ) {
6863: mpos = MPOS(d1);
6864: for ( i = 0; i < tlen; i++ )
6865: if ( tab[i] ) {
6866: d = DL(tab[i]);
6867: MPOS(d) = mpos;
6868: TD(d) = ndl_weight(d);
6869: }
6870: }
6871: }
6872:
6873: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6874: {
6875: NM mr,mr0;
6876: NMV m;
6877: UINT *d,*dt,*dm;
6878: int c,n,td,i,c1,c2,len;
6879: Q q;
6880: ND r;
6881:
6882: if ( !p ) return 0;
6883: else {
6884: n = NV(p); m = BDY(p);
6885: d = DL(m0);
6886: len = LEN(p);
6887: mr0 = 0;
6888: td = TD(d);
6889: c = CM(m0);
6890: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6891: NEXTNM(mr0,mr);
6892: CM(mr) = 1;
6893: ndl_add(DL(m),d,DL(mr));
6894: }
6895: NEXT(mr) = 0;
6896: MKND(NV(p),mr0,len,r);
6897: SG(r) = SG(p) + TD(d);
6898: return r;
6899: }
6900: }
6901:
6902: ND ndv_mul_nm(int mod,NM m0,NDV p)
6903: {
6904: NM mr,mr0;
6905: NMV m;
6906: UINT *d,*dt,*dm;
6907: int c,n,td,i,c1,c2,len;
6908: P q;
6909: ND r;
6910:
6911: if ( !p ) return 0;
6912: else if ( do_weyl ) {
6913: if ( mod < 0 ) {
6914: error("ndv_mul_nm : not implemented (weyl)");
6915: return 0;
6916: } else
6917: return weyl_ndv_mul_nm(mod,m0,p);
6918: } else {
6919: n = NV(p); m = BDY(p);
6920: d = DL(m0);
6921: len = LEN(p);
6922: mr0 = 0;
6923: td = TD(d);
6924: if ( mod == -1 ) {
6925: c = CM(m0);
6926: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6927: NEXTNM(mr0,mr);
6928: CM(mr) = _mulsf(CM(m),c);
6929: ndl_add(DL(m),d,DL(mr));
6930: }
6931: } else if ( mod == -2 ) {
6932: Z cl;
6933: cl = CZ(m0);
6934: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6935: NEXTNM(mr0,mr);
6936: mullf(CZ(m),cl,&CZ(mr));
6937: ndl_add(DL(m),d,DL(mr));
6938: }
6939: } else if ( mod ) {
6940: c = CM(m0);
6941: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6942: NEXTNM(mr0,mr);
6943: c1 = CM(m);
6944: DMAR(c1,c,0,mod,c2);
6945: CM(mr) = c2;
6946: ndl_add(DL(m),d,DL(mr));
6947: }
6948: } else {
6949: q = CP(m0);
6950: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6951: NEXTNM(mr0,mr);
6952: mulp(nd_vc,CP(m),q,&CP(mr));
6953: ndl_add(DL(m),d,DL(mr));
6954: }
6955: }
6956: NEXT(mr) = 0;
6957: MKND(NV(p),mr0,len,r);
6958: SG(r) = SG(p) + TD(d);
6959: return r;
6960: }
6961: }
6962:
6963: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6964: {
6965: NM mq0,mq;
6966: NMV tm;
6967: Q q;
6968: int i,nv,sg,c,c1,c2,hindex;
6969: ND p,t,r;
6970:
6971: if ( bucket->m < 0 ) return 0;
6972: else {
6973: nv = NV(d);
6974: mq0 = 0;
6975: tm = (NMV)MALLOC(nmv_adv);
6976: while ( 1 ) {
6977: if ( mod > 0 || mod == -1 )
6978: hindex = head_pbucket(mod,bucket);
6979: else if ( mod == -2 )
6980: hindex = head_pbucket_lf(bucket);
6981: else
6982: hindex = head_pbucket_q(bucket);
6983: if ( hindex < 0 ) break;
6984: p = bucket->body[hindex];
6985: NEXTNM(mq0,mq);
6986: ndl_sub(HDL(p),HDL(d),DL(mq));
6987: ndl_copy(DL(mq),DL(tm));
6988: if ( mod ) {
6989: c1 = invm(HCM(d),mod); c2 = HCM(p);
6990: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6991: CM(tm) = mod-c;
6992: } else {
1.6 noro 6993: divsz(HCZ(p),HCZ(d),&CZ(mq));
6994: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6995: }
6996: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6997: bucket->body[hindex] = nd_remove_head(p);
6998: t = nd_remove_head(t);
6999: add_pbucket(mod,bucket,t);
7000: }
7001: if ( !mq0 )
7002: r = 0;
7003: else {
7004: NEXT(mq) = 0;
7005: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
7006: MKND(nv,mq0,i,r);
7007: /* XXX */
7008: SG(r) = HTD(r);
7009: }
7010: return r;
7011: }
7012: }
7013:
7014: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
7015: {
7016: NMV m,mr,mr0,t;
7017: int len,i,k;
7018:
7019: if ( !p ) return;
7020: m = BDY(p); len = LEN(p);
7021: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
7022: m = (NMV)((char *)mr0+(len-1)*oadv);
7023: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
7024: t = (NMV)MALLOC(nmv_adv);
7025: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 7026: CZ(t) = CZ(m);
1.1 noro 7027: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
7028: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 7029: CZ(mr) = CZ(t);
1.1 noro 7030: ndl_copy(DL(t),DL(mr));
7031: }
7032: BDY(p) = mr0;
7033: }
7034:
7035: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
7036: {
7037: NMV m,mr,mr0;
7038: int len,i;
7039: NDV r;
7040:
7041: if ( !p ) return 0;
7042: m = BDY(p); len = LEN(p);
7043: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
7044: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
7045: ndl_zero(DL(mr));
7046: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 7047: CZ(mr) = CZ(m);
1.1 noro 7048: }
7049: MKNDV(NV(p),mr0,len,r);
7050: SG(r) = SG(p);
1.24 noro 7051: r->sig = p->sig;
1.1 noro 7052: return r;
7053: }
7054:
7055: /* duplicate p */
7056:
7057: NDV ndv_dup(int mod,NDV p)
7058: {
7059: NDV d;
7060: NMV t,m,m0;
7061: int i,len;
7062:
7063: if ( !p ) return 0;
7064: len = LEN(p);
7065: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
7066: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
7067: ndl_copy(DL(t),DL(m));
1.6 noro 7068: CZ(m) = CZ(t);
1.1 noro 7069: }
7070: MKNDV(NV(p),m0,len,d);
7071: SG(d) = SG(p);
7072: return d;
7073: }
7074:
7075: NDV ndv_symbolic(int mod,NDV p)
7076: {
7077: NDV d;
7078: NMV t,m,m0;
7079: int i,len;
7080:
7081: if ( !p ) return 0;
7082: len = LEN(p);
7083: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
7084: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
7085: ndl_copy(DL(t),DL(m));
1.6 noro 7086: CZ(m) = ONE;
1.1 noro 7087: }
7088: MKNDV(NV(p),m0,len,d);
7089: SG(d) = SG(p);
7090: return d;
7091: }
7092:
7093: ND nd_dup(ND p)
7094: {
7095: ND d;
7096: NM t,m,m0;
7097:
7098: if ( !p ) return 0;
7099: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
7100: NEXTNM(m0,m);
7101: ndl_copy(DL(t),DL(m));
1.6 noro 7102: CZ(m) = CZ(t);
1.1 noro 7103: }
7104: if ( m0 ) NEXT(m) = 0;
7105: MKND(NV(p),m0,LEN(p),d);
7106: SG(d) = SG(p);
7107: return d;
7108: }
7109:
7110: /* XXX if p->len == 0 then it represents 0 */
7111:
7112: void ndv_mod(int mod,NDV p)
7113: {
7114: NMV t,d;
7115: int r,s,u;
7116: int i,len,dlen;
7117: P cp;
7118: Z c;
7119: Obj gfs;
7120:
7121: if ( !p ) return;
7122: len = LEN(p);
7123: dlen = 0;
7124: if ( mod == -1 )
7125: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7126: simp_ff((Obj)CP(t),&gfs);
7127: if ( gfs ) {
7128: r = FTOIF(CONT((GFS)gfs));
7129: CM(d) = r;
7130: ndl_copy(DL(t),DL(d));
7131: NMV_ADV(d);
7132: dlen++;
7133: }
7134: }
7135: else if ( mod == -2 )
7136: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7137: simp_ff((Obj)CP(t),&gfs);
7138: if ( gfs ) {
7139: lmtolf((LM)gfs,&CZ(d));
7140: ndl_copy(DL(t),DL(d));
7141: NMV_ADV(d);
7142: dlen++;
7143: }
7144: }
7145: else
7146: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
7147: if ( nd_vc ) {
7148: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
7149: c = (Z)cp;
7150: } else
1.6 noro 7151: c = CZ(t);
1.1 noro 7152: r = remqi((Q)c,mod);
7153: if ( r ) {
7154: CM(d) = r;
7155: ndl_copy(DL(t),DL(d));
7156: NMV_ADV(d);
7157: dlen++;
7158: }
7159: }
7160: LEN(p) = dlen;
7161: }
7162:
7163: NDV ptondv(VL vl,VL dvl,P p)
7164: {
7165: ND nd;
7166:
7167: nd = ptond(vl,dvl,p);
7168: return ndtondv(0,nd);
7169: }
7170:
7171: void pltozpl(LIST l,Q *cont,LIST *pp)
7172: {
1.16 noro 7173: NODE nd,nd1;
7174: int n;
7175: P *pl;
7176: Q *cl;
7177: int i;
7178: P dmy;
7179: Z dvr,inv;
7180: LIST r;
7181:
7182: nd = BDY(l); n = length(nd);
7183: pl = (P *)MALLOC(n*sizeof(P));
7184: cl = (Q *)MALLOC(n*sizeof(Q));
7185: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
7186: ptozp((P)BDY(nd),1,&cl[i],&dmy);
7187: }
7188: qltozl(cl,n,&dvr);
7189: divz(ONE,dvr,&inv);
7190: nd = BDY(l);
7191: for ( i = 0; i < n; i++, nd = NEXT(nd) )
7192: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
7193: nd = 0;
7194: for ( i = n-1; i >= 0; i-- ) {
7195: MKNODE(nd1,pl[i],nd); nd = nd1;
7196: }
7197: MKLIST(r,nd);
7198: *pp = r;
1.1 noro 7199: }
7200:
7201: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
7202:
7203: NDV pltondv(VL vl,VL dvl,LIST p)
7204: {
7205: int i;
7206: NODE t;
7207: ND r,ri;
7208: NM m;
7209:
7210: if ( !nd_module ) error("pltond : module order must be set");
7211: r = 0;
7212: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
7213: ri = ptond(vl,dvl,(P)BDY(t));
7214: if ( ri )
7215: for ( m = BDY(ri); m; m = NEXT(m) ) {
7216: MPOS(DL(m)) = i;
7217: TD(DL(m)) = ndl_weight(DL(m));
7218: if ( nd_blockmask ) ndl_weight_mask(DL(m));
7219: }
7220: r = nd_add(0,r,ri);
7221: }
7222: return ndtondv(0,r);
7223: }
7224:
7225: ND ptond(VL vl,VL dvl,P p)
7226: {
7227: int n,i,j,k,e;
7228: VL tvl;
7229: V v;
7230: DCP dc;
7231: DCP *w;
7232: ND r,s,t,u;
7233: P x;
7234: int c;
7235: UINT *d;
7236: NM m,m0;
7237:
7238: if ( !p )
7239: return 0;
7240: else if ( NUM(p) ) {
7241: NEWNM(m);
7242: ndl_zero(DL(m));
1.48 noro 7243: if ( RATN(p) && !INT((Q)p) )
1.1 noro 7244: error("ptond : input must be integer-coefficient");
1.6 noro 7245: CZ(m) = (Z)p;
1.1 noro 7246: NEXT(m) = 0;
7247: MKND(nd_nvar,m,1,r);
7248: SG(r) = 0;
7249: return r;
7250: } else {
7251: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
7252: w = (DCP *)MALLOC(k*sizeof(DCP));
7253: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
7254: for ( i = 0, tvl = dvl, v = VR(p);
7255: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
7256: if ( !tvl ) {
7257: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
7258: t = ptond(vl,dvl,COEF(w[j]));
7259: pwrp(vl,x,DEG(w[j]),&p);
7260: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
7261: }
7262: return s;
7263: } else {
7264: NEWNM(m0); d = DL(m0);
7265: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 7266: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 7267: TD(d) = MUL_WEIGHT(e,i);
7268: if ( nd_blockmask) ndl_weight_mask(d);
7269: if ( nd_module ) MPOS(d) = 0;
7270: t = ptond(vl,dvl,COEF(w[j]));
7271: for ( m = BDY(t); m; m = NEXT(m) )
7272: ndl_addto(DL(m),d);
7273: SG(t) += TD(d);
7274: s = nd_add(0,s,t);
7275: }
7276: FREENM(m0);
7277: return s;
7278: }
7279: }
7280: }
7281:
7282: P ndvtop(int mod,VL vl,VL dvl,NDV p)
7283: {
7284: VL tvl;
7285: int len,n,j,i,e;
7286: NMV m;
7287: Z q;
7288: P c;
7289: UINT *d;
7290: P s,r,u,t,w;
7291: GFS gfs;
7292:
7293: if ( !p ) return 0;
7294: else {
7295: len = LEN(p);
7296: n = NV(p);
7297: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7298: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
7299: if ( mod == -1 ) {
7300: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7301: } else if ( mod == -2 ) {
7302: c = (P)CZ(m);
7303: } else if ( mod > 0 ) {
1.6 noro 7304: STOZ(CM(m),q); c = (P)q;
1.1 noro 7305: } else
7306: c = CP(m);
7307: d = DL(m);
7308: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7309: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7310: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7311: }
7312: addp(vl,s,t,&u); s = u;
7313: }
7314: return s;
7315: }
7316: }
7317:
7318: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
7319: {
7320: VL tvl;
7321: int len,n,j,i,e;
7322: NMV m;
7323: Z q;
7324: P c;
7325: UINT *d;
7326: P s,r,u,t,w;
7327: GFS gfs;
7328: P *a;
7329: LIST l;
7330: NODE nd,nd1;
7331:
7332: if ( !p ) return 0;
7333: else {
7334: a = (P *)MALLOC((rank+1)*sizeof(P));
7335: for ( i = 0; i <= rank; i++ ) a[i] = 0;
7336: len = LEN(p);
7337: n = NV(p);
7338: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
7339: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
7340: if ( mod == -1 ) {
7341: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
7342: } else if ( mod ) {
1.6 noro 7343: STOZ(CM(m),q); c = (P)q;
1.1 noro 7344: } else
7345: c = CP(m);
7346: d = DL(m);
7347: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 7348: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 7349: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
7350: }
7351: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
7352: }
7353: nd = 0;
7354: for ( i = rank; i > 0; i-- ) {
7355: MKNODE(nd1,a[i],nd); nd = nd1;
7356: }
7357: MKLIST(l,nd);
7358: return l;
7359: }
7360: }
7361:
7362: NDV ndtondv(int mod,ND p)
7363: {
7364: NDV d;
7365: NMV m,m0;
7366: NM t;
7367: int i,len;
7368:
7369: if ( !p ) return 0;
7370: len = LEN(p);
7371: if ( mod > 0 || mod == -1 )
7372: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7373: else
7374: m0 = m = MALLOC(len*nmv_adv);
7375: #if 0
7376: ndv_alloc += nmv_adv*len;
7377: #endif
7378: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
7379: ndl_copy(DL(t),DL(m));
1.6 noro 7380: CZ(m) = CZ(t);
1.1 noro 7381: }
7382: MKNDV(NV(p),m0,len,d);
7383: SG(d) = SG(p);
1.24 noro 7384: d->sig = p->sig;
1.1 noro 7385: return d;
7386: }
7387:
1.16 noro 7388: static int dmm_comp_nv;
7389:
7390: int dmm_comp(DMM *a,DMM *b)
7391: {
7392: return -compdmm(dmm_comp_nv,*a,*b);
7393: }
7394:
7395: void dmm_sort_by_ord(DMM *a,int len,int nv)
7396: {
7397: dmm_comp_nv = nv;
7398: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
7399: }
7400:
7401: void dpm_sort(DPM p,DPM *rp)
7402: {
7403: DMM t,t1;
7404: int len,i,n;
7405: DMM *a;
7406: DPM d;
7407:
7408: if ( !p ) *rp = 0;
7409: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7410: a = (DMM *)MALLOC(len*sizeof(DMM));
7411: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7412: n = p->nv;
7413: dmm_sort_by_ord(a,len,n);
7414: t = 0;
7415: for ( i = len-1; i >= 0; i-- ) {
7416: NEWDMM(t1);
7417: t1->c = a[i]->c;
7418: t1->dl = a[i]->dl;
7419: t1->pos = a[i]->pos;
7420: t1->next = t;
7421: t = t1;
7422: }
7423: MKDPM(n,t,d);
7424: SG(d) = SG(p);
7425: *rp = d;
7426: }
7427:
1.18 noro 7428: int dpm_comp(DPM *a,DPM *b)
7429: {
1.22 noro 7430: return -compdpm(CO,*a,*b);
1.18 noro 7431: }
7432:
7433: NODE dpm_sort_list(NODE l)
7434: {
7435: int i,len;
7436: NODE t,t1;
7437: DPM *a;
7438:
7439: len = length(l);
7440: a = (DPM *)MALLOC(len*sizeof(DPM));
7441: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
7442: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
7443: t = 0;
7444: for ( i = len-1; i >= 0; i-- ) {
7445: MKNODE(t1,(pointer)a[i],t); t = t1;
7446: }
7447: return t;
7448: }
7449:
1.20 noro 7450: int nmv_comp(NMV a,NMV b)
7451: {
1.21 noro 7452: int t;
7453: t = DL_COMPARE(a->dl,b->dl);
7454: return -t;
1.20 noro 7455: }
7456:
1.43 noro 7457: NDV dptondv(int mod,DP p)
7458: {
7459: NDV d;
7460: NMV m,m0;
7461: MP t;
7462: MP *a;
7463: int i,len,n;
7464:
7465: if ( !p ) return 0;
7466: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7467: n = p->nv;
7468: if ( mod > 0 || mod == -1 )
7469: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7470: else
7471: m0 = m = MALLOC(len*nmv_adv);
7472: for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
7473: dltondl(n,t->dl,DL(m));
7474: TD(DL(m)) = ndl_weight(DL(m));
7475: CZ(m) = (Z)t->c;
7476: }
7477: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
7478: MKNDV(NV(p),m0,len,d);
7479: SG(d) = SG(p);
7480: return d;
7481: }
7482:
1.16 noro 7483: NDV dpmtondv(int mod,DPM p)
7484: {
7485: NDV d;
7486: NMV m,m0;
7487: DMM t;
7488: DMM *a;
7489: int i,len,n;
7490:
7491: if ( !p ) return 0;
7492: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7493: a = (DMM *)MALLOC(len*sizeof(DMM));
7494: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7495: n = p->nv;
7496: dmm_sort_by_ord(a,len,n);
7497: if ( mod > 0 || mod == -1 )
7498: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7499: else
7500: m0 = m = MALLOC(len*nmv_adv);
7501: #if 0
7502: ndv_alloc += nmv_adv*len;
7503: #endif
7504: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7505: dltondl(n,a[i]->dl,DL(m));
7506: MPOS(DL(m)) = a[i]->pos;
1.20 noro 7507: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 7508: CZ(m) = (Z)a[i]->c;
7509: }
1.20 noro 7510: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 7511: MKNDV(NV(p),m0,len,d);
7512: SG(d) = SG(p);
7513: return d;
7514: }
7515:
1.1 noro 7516: ND ndvtond(int mod,NDV p)
7517: {
7518: ND d;
7519: NM m,m0;
7520: NMV t;
7521: int i,len;
7522:
7523: if ( !p ) return 0;
7524: m0 = 0;
7525: len = p->len;
7526: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7527: NEXTNM(m0,m);
7528: ndl_copy(DL(t),DL(m));
1.6 noro 7529: CZ(m) = CZ(t);
1.1 noro 7530: }
7531: NEXT(m) = 0;
7532: MKND(NV(p),m0,len,d);
7533: SG(d) = SG(p);
1.28 noro 7534: d->sig = p->sig;
1.1 noro 7535: return d;
7536: }
7537:
7538: DP ndvtodp(int mod,NDV p)
7539: {
7540: MP m,m0;
7541: DP d;
7542: NMV t;
7543: int i,len;
7544:
7545: if ( !p ) return 0;
7546: m0 = 0;
7547: len = p->len;
7548: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7549: NEXTMP(m0,m);
7550: m->dl = ndltodl(nd_nvar,DL(t));
7551: m->c = (Obj)ndctop(mod,t->c);
7552: }
7553: NEXT(m) = 0;
7554: MKDP(nd_nvar,m0,d);
7555: SG(d) = SG(p);
7556: return d;
7557: }
7558:
1.44 noro 7559: DPM sigtodpm(SIG s)
7560: {
7561: DMM m;
7562: DPM d;
7563:
7564: NEWDMM(m);
7565: m->c = (Obj)ONE;
7566: m->dl = s->dl;
7567: m->pos = s->pos+1;
7568: m->next = 0;
7569: MKDPM(nd_nvar,m,d);
7570: SG(d) = s->dl->td;
7571: return d;
7572: }
7573:
1.16 noro 7574: DPM ndvtodpm(int mod,NDV p)
7575: {
7576: DMM m,m0;
7577: DPM d;
7578: NMV t;
7579: int i,len;
7580:
7581: if ( !p ) return 0;
7582: m0 = 0;
7583: len = p->len;
7584: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7585: NEXTDMM(m0,m);
7586: m->dl = ndltodl(nd_nvar,DL(t));
7587: m->c = (Obj)ndctop(mod,t->c);
7588: m->pos = MPOS(DL(t));
7589: }
7590: NEXT(m) = 0;
7591: MKDPM(nd_nvar,m0,d);
7592: SG(d) = SG(p);
7593: return d;
7594: }
7595:
7596:
1.1 noro 7597: DP ndtodp(int mod,ND p)
7598: {
7599: MP m,m0;
7600: DP d;
7601: NM t;
7602: int i,len;
7603:
7604: if ( !p ) return 0;
7605: m0 = 0;
7606: len = p->len;
7607: for ( t = BDY(p); t; t = NEXT(t) ) {
7608: NEXTMP(m0,m);
7609: m->dl = ndltodl(nd_nvar,DL(t));
7610: m->c = (Obj)ndctop(mod,t->c);
7611: }
7612: NEXT(m) = 0;
7613: MKDP(nd_nvar,m0,d);
7614: SG(d) = SG(p);
7615: return d;
7616: }
7617:
7618: void ndv_print(NDV p)
7619: {
7620: NMV m;
7621: int i,len;
7622:
7623: if ( !p ) printf("0\n");
7624: else {
7625: len = LEN(p);
7626: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7627: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 7628: else printf("+%ld*",CM(m));
1.1 noro 7629: ndl_print(DL(m));
7630: }
7631: printf("\n");
7632: }
7633: }
7634:
7635: void ndv_print_q(NDV p)
7636: {
7637: NMV m;
7638: int i,len;
7639:
7640: if ( !p ) printf("0\n");
7641: else {
7642: len = LEN(p);
7643: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7644: printf("+");
1.6 noro 7645: printexpr(CO,(Obj)CZ(m));
1.1 noro 7646: printf("*");
7647: ndl_print(DL(m));
7648: }
7649: printf("\n");
7650: }
7651: }
7652:
7653: NODE ndv_reducebase(NODE x,int *perm)
7654: {
7655: int len,i,j;
7656: NDVI w;
7657: NODE t,t0;
7658:
1.41 noro 7659: if ( nd_norb ) return x;
1.1 noro 7660: len = length(x);
7661: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
7662: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
7663: w[i].p = BDY(t); w[i].i = perm[i];
7664: }
7665: for ( i = 0; i < len; i++ ) {
7666: for ( j = 0; j < i; j++ ) {
7667: if ( w[i].p && w[j].p ) {
7668: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
7669: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
7670: }
7671: }
7672: }
7673: for ( i = j = 0, t0 = 0; i < len; i++ ) {
7674: if ( w[i].p ) {
7675: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
7676: perm[j++] = w[i].i;
7677: }
7678: }
7679: NEXT(t) = 0; x = t0;
7680: return x;
7681: }
7682:
7683: /* XXX incomplete */
7684:
1.21 noro 7685: extern DMMstack dmm_stack;
7686: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 7687:
1.1 noro 7688: void nd_init_ord(struct order_spec *ord)
7689: {
7690: nd_module = (ord->id >= 256);
7691: if ( nd_module ) {
7692: nd_dcomp = -1;
1.21 noro 7693: nd_module_ordtype = ord->module_ordtype;
1.1 noro 7694: nd_pot_nelim = ord->pot_nelim;
7695: nd_poly_weight_len = ord->nv;
7696: nd_poly_weight = ord->top_weight;
7697: nd_module_rank = ord->module_rank;
7698: nd_module_weight = ord->module_top_weight;
7699: }
7700: nd_matrix = 0;
7701: nd_matrix_len = 0;
7702: switch ( ord->id ) {
7703: case 0:
7704: switch ( ord->ord.simple ) {
7705: case 0:
7706: nd_dcomp = 1;
7707: nd_isrlex = 1;
7708: break;
7709: case 1:
7710: nd_dcomp = 1;
7711: nd_isrlex = 0;
7712: break;
7713: case 2:
7714: nd_dcomp = 0;
7715: nd_isrlex = 0;
7716: ndl_compare_function = ndl_lex_compare;
7717: break;
7718: case 11:
7719: /* XXX */
7720: nd_dcomp = 0;
7721: nd_isrlex = 1;
7722: ndl_compare_function = ndl_ww_lex_compare;
7723: break;
7724: default:
7725: error("nd_gr : unsupported order");
7726: }
7727: break;
7728: case 1:
7729: /* block order */
7730: /* XXX */
7731: nd_dcomp = -1;
7732: nd_isrlex = 0;
7733: ndl_compare_function = ndl_block_compare;
7734: break;
7735: case 2:
7736: /* matrix order */
7737: /* XXX */
7738: nd_dcomp = -1;
7739: nd_isrlex = 0;
7740: nd_matrix_len = ord->ord.matrix.row;
7741: nd_matrix = ord->ord.matrix.matrix;
7742: ndl_compare_function = ndl_matrix_compare;
7743: break;
7744: case 3:
7745: /* composite order */
7746: nd_dcomp = -1;
7747: nd_isrlex = 0;
7748: nd_worb_len = ord->ord.composite.length;
7749: nd_worb = ord->ord.composite.w_or_b;
7750: ndl_compare_function = ndl_composite_compare;
7751: break;
7752:
7753: /* module order */
7754: case 256:
7755: switch ( ord->ord.simple ) {
7756: case 0:
1.21 noro 7757: nd_dcomp = 0;
1.1 noro 7758: nd_isrlex = 1;
1.21 noro 7759: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7760: break;
7761: case 1:
1.21 noro 7762: nd_dcomp = 0;
1.1 noro 7763: nd_isrlex = 0;
7764: ndl_compare_function = ndl_module_glex_compare;
7765: break;
7766: case 2:
1.21 noro 7767: nd_dcomp = 0;
1.1 noro 7768: nd_isrlex = 0;
1.21 noro 7769: ndl_compare_function = ndl_module_compare;
7770: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7771: break;
7772: default:
1.21 noro 7773: error("nd_init_ord : unsupported order");
1.1 noro 7774: }
7775: break;
7776: case 257:
7777: /* block order */
7778: nd_isrlex = 0;
1.21 noro 7779: ndl_compare_function = ndl_module_compare;
7780: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7781: break;
7782: case 258:
7783: /* matrix order */
7784: nd_isrlex = 0;
7785: nd_matrix_len = ord->ord.matrix.row;
7786: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7787: ndl_compare_function = ndl_module_compare;
7788: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7789: break;
7790: case 259:
7791: /* composite order */
7792: nd_isrlex = 0;
7793: nd_worb_len = ord->ord.composite.length;
7794: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7795: ndl_compare_function = ndl_module_compare;
7796: ndl_base_compare_function = ndl_composite_compare;
7797: break;
7798: case 300:
7799: /* schreyer order */
7800: if ( ord->base->id != 256 )
7801: error("nd_init_ord : unsupported base order");
7802: ndl_compare_function = ndl_module_schreyer_compare;
7803: dmm_stack = ord->dmmstack;
7804: switch ( ord->base->ord.simple ) {
7805: case 0:
7806: nd_isrlex = 1;
7807: ndl_base_compare_function = ndl_glex_compare;
7808: dl_base_compare_function = cmpdl_revgradlex;
7809: break;
7810: case 1:
7811: nd_isrlex = 0;
7812: ndl_base_compare_function = ndl_glex_compare;
7813: dl_base_compare_function = cmpdl_gradlex;
7814: break;
7815: case 2:
7816: nd_isrlex = 0;
7817: ndl_base_compare_function = ndl_lex_compare;
7818: dl_base_compare_function = cmpdl_lex;
7819: break;
7820: default:
7821: error("nd_init_ord : unsupported order");
7822: }
1.1 noro 7823: break;
7824: }
7825: nd_ord = ord;
7826: }
7827:
7828: BlockMask nd_create_blockmask(struct order_spec *ord)
7829: {
7830: int n,i,j,s,l;
7831: UINT *t;
7832: BlockMask bm;
7833:
7834: /* we only create mask table for block order */
7835: if ( ord->id != 1 && ord->id != 257 )
7836: return 0;
7837: n = ord->ord.block.length;
7838: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7839: bm->n = n;
7840: bm->order_pair = ord->ord.block.order_pair;
7841: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7842: for ( i = 0, s = 0; i < n; i++ ) {
7843: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7844: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7845: l = bm->order_pair[i].length;
7846: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7847: }
7848: return bm;
7849: }
7850:
7851: EPOS nd_create_epos(struct order_spec *ord)
7852: {
7853: int i,j,l,s,ord_l,ord_o;
7854: EPOS epos;
7855: struct order_pair *op;
7856:
7857: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7858: switch ( ord->id ) {
1.21 noro 7859: case 0: case 256: case 300:
1.1 noro 7860: if ( nd_isrlex ) {
7861: for ( i = 0; i < nd_nvar; i++ ) {
7862: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7863: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7864: }
7865: } else {
7866: for ( i = 0; i < nd_nvar; i++ ) {
7867: epos[i].i = nd_exporigin + i/nd_epw;
7868: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7869: }
7870: }
7871: break;
7872: case 1: case 257:
7873: /* block order */
7874: l = ord->ord.block.length;
7875: op = ord->ord.block.order_pair;
7876: for ( j = 0, s = 0; j < l; j++ ) {
7877: ord_o = op[j].order;
7878: ord_l = op[j].length;
7879: if ( !ord_o )
7880: for ( i = 0; i < ord_l; i++ ) {
7881: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7882: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7883: }
7884: else
7885: for ( i = 0; i < ord_l; i++ ) {
7886: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7887: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7888: }
7889: s += ord_l;
7890: }
7891: break;
7892: case 2:
7893: /* matrix order */
7894: case 3:
7895: /* composite order */
7896: default:
7897: for ( i = 0; i < nd_nvar; i++ ) {
7898: epos[i].i = nd_exporigin + i/nd_epw;
7899: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7900: }
7901: break;
7902: }
7903: return epos;
7904: }
7905:
7906: /* external interface */
7907:
7908: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7909: {
7910: NODE t,in0,in;
7911: ND ndf,nf;
7912: NDV ndvf;
7913: VL vv,tv;
7914: int stat,nvar,max,mrank;
7915: union oNDC dn;
7916: Q cont;
7917: P pp;
7918: LIST ppl;
7919:
7920: if ( !f ) {
7921: *rp = 0;
7922: return;
7923: }
7924: pltovl(v,&vv);
7925: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7926:
7927: /* max=65536 implies nd_bpe=32 */
7928: max = 65536;
7929:
7930: nd_module = 0;
7931: /* nd_module will be set if ord is a module ordering */
7932: nd_init_ord(ord);
7933: nd_setup_parameters(nvar,max);
7934: if ( nd_module && OID(f) != O_LIST )
7935: error("nd_nf_p : the first argument must be a list");
7936: if ( nd_module ) mrank = length(BDY((LIST)f));
7937: /* conversion to ndv */
7938: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7939: NEXTNODE(in0,in);
7940: if ( nd_module ) {
7941: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7942: || length(BDY((LIST)BDY(t))) != mrank )
7943: error("nd_nf_p : inconsistent basis element");
7944: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7945: else ppl = (LIST)BDY(t);
7946: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7947: } else {
7948: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7949: else pp = (P)BDY(t);
7950: BDY(in) = (pointer)ptondv(CO,vv,pp);
7951: }
7952: if ( m ) ndv_mod(m,(NDV)BDY(in));
7953: }
7954: if ( in0 ) NEXT(in) = 0;
7955:
7956: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7957: else ndvf = ptondv(CO,vv,(P)f);
7958: if ( m ) ndv_mod(m,ndvf);
7959: ndf = (pointer)ndvtond(m,ndvf);
7960:
7961: /* dont sort, dont removecont */
1.24 noro 7962: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7963: nd_scale=2;
1.6 noro 7964: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7965: if ( !stat )
7966: error("nd_nf_p : exponent too large");
7967: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7968: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7969: }
7970:
7971: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7972: {
7973: NM m;
7974: UINT *t,*s;
7975: int i;
7976:
7977: for ( i = 0; i < n; i++ ) r[i] = 0;
7978: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7979: t = DL(m);
7980: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7981: r[i] = CM(m);
7982: }
7983: for ( i = 0; !r[i]; i++ );
7984: return i;
7985: }
7986:
7987: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7988: {
7989: NM m;
7990: UINT *t,*s;
7991: int i;
7992:
7993: for ( i = 0; i < n; i++ ) r[i] = 0;
7994: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7995: t = DL(m);
7996: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7997: r[i] = CZ(m);
1.1 noro 7998: }
7999: for ( i = 0; !r[i]; i++ );
8000: return i;
8001: }
8002:
8003: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
8004: {
8005: NM m;
8006: UINT *t,*s;
8007: int i;
8008:
8009: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
8010: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
8011: t = DL(m);
8012: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
8013: mpz_set(r[i],BDY(CZ(m)));
8014: }
8015: for ( i = 0; !mpz_sgn(r[i]); i++ );
8016: return i;
8017: }
8018:
8019: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
8020: {
8021: NM m;
8022: unsigned long *v;
8023: int i,j,h,size;
8024: UINT *s,*t;
8025:
8026: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
8027: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
8028: bzero(v,size);
8029: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
8030: t = DL(m);
8031: h = ndl_hash_value(t);
8032: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
8033: v[i/BLEN] |= 1L <<(i%BLEN);
8034: }
8035: return v;
8036: }
8037:
8038: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
8039: {
8040: NMV mr;
8041: UINT *d,*t,*s;
8042: int i,j,len,h,head;
8043:
8044: d = DL(m);
8045: len = LEN(p);
8046: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
8047: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
8048: ndl_add(d,DL(mr),t);
8049: h = ndl_hash_value(t);
8050: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
8051: if ( j == 0 ) head = i;
8052: v[i/BLEN] |= 1L <<(i%BLEN);
8053: }
8054: return head;
8055: }
8056:
8057: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
8058: {
8059: NM m;
8060: NMV mr;
8061: UINT *d,*t,*s;
8062: NDV p;
8063: int i,j,len;
8064: Z *r;
8065:
8066: m = pair->mul;
8067: d = DL(m);
8068: p = nd_ps[pair->index];
8069: len = LEN(p);
8070: r = (Z *)CALLOC(n,sizeof(Q));
8071: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
8072: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
8073: ndl_add(d,DL(mr),t);
8074: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 8075: r[i] = CZ(mr);
1.1 noro 8076: }
8077: return r;
8078: }
8079:
1.11 noro 8080: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 8081: {
8082: NM m;
8083: NMV mr;
1.11 noro 8084: UINT *d,*t,*s,*u;
1.1 noro 8085: NDV p;
8086: unsigned char *ivc;
8087: unsigned short *ivs;
8088: UINT *v,*ivi,*s0v;
1.11 noro 8089: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 8090: IndArray r;
8091:
8092: m = pair->mul;
8093: d = DL(m);
8094: if ( trace )
8095: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
8096: else
8097: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
8098:
8099: len = LEN(p);
8100: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
8101: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 8102: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
8103: ndl_add(d,DL(mr),t);
8104: st = prev;
8105: ed = n;
8106: while ( ed > st ) {
8107: md = (st+ed)/2;
8108: u = s0+md*nd_wpd;
8109: c = DL_COMPARE(u,t);
8110: if ( c == 0 ) break;
8111: else if ( c > 0 ) st = md;
8112: else ed = md;
8113: }
8114: prev = v[j] = md;
1.1 noro 8115: }
8116: r = (IndArray)MALLOC(sizeof(struct oIndArray));
8117: r->head = v[0];
8118: diff = 0;
8119: for ( i = 1; i < len; i++ ) {
8120: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
8121: }
8122: if ( diff < 256 ) {
8123: r->width = 1;
8124: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
8125: r->index.c = ivc;
8126: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
8127: } else if ( diff < 65536 ) {
8128: r->width = 2;
8129: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
8130: r->index.s = ivs;
8131: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
8132: } else {
8133: r->width = 4;
8134: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
8135: r->index.i = ivi;
8136: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
8137: }
8138: return r;
8139: }
8140:
8141: int compress_array(Z *svect,Z *cvect,int n)
8142: {
8143: int i,j;
8144:
8145: for ( i = j = 0; i < n; i++ )
8146: if ( svect[i] ) cvect[j++] = svect[i];
8147: return j;
8148: }
8149:
8150: void expand_array(Z *svect,Z *cvect,int n)
8151: {
8152: int i,j;
8153:
8154: for ( i = j = 0; j < n; i++ )
8155: if ( svect[i] ) svect[i] = cvect[j++];
8156: }
8157:
1.8 noro 8158: #if 0
1.1 noro 8159: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8160: {
8161: int i,j,k,len,pos,prev,nz;
8162: Z cs,mcs,c1,c2,cr,gcd,t;
8163: IndArray ivect;
8164: unsigned char *ivc;
8165: unsigned short *ivs;
8166: unsigned int *ivi;
8167: NDV redv;
8168: NMV mr;
8169: NODE rp;
8170: int maxrs;
8171: double hmag;
8172: Z *cvect;
1.3 noro 8173: int l;
1.1 noro 8174:
8175: maxrs = 0;
8176: for ( i = 0; i < col && !svect[i]; i++ );
8177: if ( i == col ) return maxrs;
8178: hmag = p_mag((P)svect[i])*nd_scale;
8179: cvect = (Z *)MALLOC(col*sizeof(Q));
8180: for ( i = 0; i < nred; i++ ) {
8181: ivect = imat[i];
8182: k = ivect->head;
8183: if ( svect[k] ) {
8184: maxrs = MAX(maxrs,rp0[i]->sugar);
8185: redv = nd_demand?ndv_load(rp0[i]->index)
8186: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
8187: len = LEN(redv); mr = BDY(redv);
1.6 noro 8188: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 8189: chsgnz(cs,&mcs);
8190: if ( !UNIQ(cr) ) {
8191: for ( j = 0; j < col; j++ ) {
8192: mulz(svect[j],cr,&c1); svect[j] = c1;
8193: }
8194: }
8195: svect[k] = 0; prev = k;
8196: switch ( ivect->width ) {
8197: case 1:
8198: ivc = ivect->index.c;
8199: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8200: pos = prev+ivc[j]; prev = pos;
1.6 noro 8201: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8202: }
8203: break;
8204: case 2:
8205: ivs = ivect->index.s;
8206: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8207: pos = prev+ivs[j]; prev = pos;
1.6 noro 8208: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8209: }
8210: break;
8211: case 4:
8212: ivi = ivect->index.i;
8213: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8214: pos = prev+ivi[j]; prev = pos;
1.6 noro 8215: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 8216: }
8217: break;
8218: }
8219: for ( j = k+1; j < col && !svect[j]; j++ );
8220: if ( j == col ) break;
8221: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
8222: nz = compress_array(svect,cvect,col);
8223: removecont_array((P *)cvect,nz,1);
8224: expand_array(svect,cvect,nz);
8225: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
8226: }
8227: }
8228: }
8229: nz = compress_array(svect,cvect,col);
8230: removecont_array((P *)cvect,nz,1);
8231: expand_array(svect,cvect,nz);
8232: if ( DP_Print ) {
8233: fprintf(asir_out,"-"); fflush(asir_out);
8234: }
8235: return maxrs;
8236: }
1.4 noro 8237: #else
1.9 noro 8238:
1.4 noro 8239: /* direct mpz version */
8240: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8241: {
8242: int i,j,k,len,pos,prev;
8243: mpz_t cs,cr,gcd;
8244: IndArray ivect;
8245: unsigned char *ivc;
8246: unsigned short *ivs;
8247: unsigned int *ivi;
8248: NDV redv;
8249: NMV mr;
8250: NODE rp;
8251: int maxrs;
8252: double hmag;
8253: int l;
1.13 noro 8254: static mpz_t *svect;
8255: static int svect_len=0;
1.4 noro 8256:
8257: maxrs = 0;
8258: for ( i = 0; i < col && !svect0[i]; i++ );
8259: if ( i == col ) return maxrs;
8260: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 8261: if ( col > svect_len ) {
8262: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8263: svect_len = col;
8264: }
1.4 noro 8265: for ( i = 0; i < col; i++ ) {
8266: mpz_init(svect[i]);
8267: if ( svect0[i] )
8268: mpz_set(svect[i],BDY(svect0[i]));
8269: else
8270: mpz_set_ui(svect[i],0);
8271: }
8272: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
8273: for ( i = 0; i < nred; i++ ) {
8274: ivect = imat[i];
8275: k = ivect->head;
8276: if ( mpz_sgn(svect[k]) ) {
8277: maxrs = MAX(maxrs,rp0[i]->sugar);
8278: redv = nd_demand?ndv_load(rp0[i]->index)
8279: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
8280: len = LEN(redv); mr = BDY(redv);
1.6 noro 8281: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 8282: mpz_div(cs,svect[k],gcd);
1.6 noro 8283: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 8284: mpz_neg(cs,cs);
1.9 noro 8285: if ( MUNIMPZ(cr) )
8286: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
8287: else if ( !UNIMPZ(cr) )
8288: for ( j = 0; j < col; j++ ) {
8289: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
8290: }
1.4 noro 8291: mpz_set_ui(svect[k],0);
8292: prev = k;
8293: switch ( ivect->width ) {
8294: case 1:
8295: ivc = ivect->index.c;
8296: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8297: pos = prev+ivc[j]; prev = pos;
1.6 noro 8298: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8299: }
8300: break;
8301: case 2:
8302: ivs = ivect->index.s;
8303: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8304: pos = prev+ivs[j]; prev = pos;
1.6 noro 8305: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8306: }
8307: break;
8308: case 4:
8309: ivi = ivect->index.i;
8310: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8311: pos = prev+ivi[j]; prev = pos;
1.6 noro 8312: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 8313: }
8314: break;
8315: }
8316: for ( j = k+1; j < col && !svect[j]; j++ );
8317: if ( j == col ) break;
8318: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
8319: mpz_removecont_array(svect,col);
8320: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
8321: }
8322: }
8323: }
8324: mpz_removecont_array(svect,col);
8325: if ( DP_Print ) {
8326: fprintf(asir_out,"-"); fflush(asir_out);
8327: }
8328: for ( i = 0; i < col; i++ )
8329: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
8330: else svect0[i] = 0;
8331: return maxrs;
8332: }
8333: #endif
1.1 noro 8334:
1.40 noro 8335: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 8336: {
8337: int i,j,k,len,pos,prev;
8338: UINT c,c1,c2,c3,up,lo,dmy;
8339: IndArray ivect;
8340: unsigned char *ivc;
8341: unsigned short *ivs;
8342: unsigned int *ivi;
8343: NDV redv;
8344: NMV mr;
8345: NODE rp;
8346: int maxrs;
8347:
8348: maxrs = 0;
8349: for ( i = 0; i < nred; i++ ) {
8350: ivect = imat[i];
8351: k = ivect->head; svect[k] %= m;
1.40 noro 8352: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 8353: maxrs = MAX(maxrs,rp0[i]->sugar);
8354: c = m-c; redv = nd_ps[rp0[i]->index];
8355: len = LEN(redv); mr = BDY(redv);
8356: svect[k] = 0; prev = k;
8357: switch ( ivect->width ) {
8358: case 1:
8359: ivc = ivect->index.c;
8360: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8361: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 8362: if ( c1 ) {
8363: c2 = svect[pos];
1.1 noro 8364: DMA(c1,c,c2,up,lo);
8365: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8366: } else svect[pos] = lo;
1.40 noro 8367: }
1.1 noro 8368: }
8369: break;
8370: case 2:
8371: ivs = ivect->index.s;
8372: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8373: pos = prev+ivs[j]; c1 = CM(mr);
8374: prev = pos;
1.40 noro 8375: if ( c1 ) {
8376: c2 = svect[pos];
1.1 noro 8377: DMA(c1,c,c2,up,lo);
8378: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8379: } else svect[pos] = lo;
1.40 noro 8380: }
1.1 noro 8381: }
8382: break;
8383: case 4:
8384: ivi = ivect->index.i;
8385: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8386: pos = prev+ivi[j]; c1 = CM(mr);
8387: prev = pos;
1.40 noro 8388: if ( c1 ) {
8389: c2 = svect[pos];
1.1 noro 8390: DMA(c1,c,c2,up,lo);
8391: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8392: } else svect[pos] = lo;
1.40 noro 8393: }
1.1 noro 8394: }
8395: break;
8396: }
8397: }
8398: }
8399: for ( i = 0; i < col; i++ )
8400: if ( svect[i] >= (UINT)m ) svect[i] %= m;
8401: return maxrs;
8402: }
8403:
8404: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8405: {
8406: int i,j,k,len,pos,prev;
8407: UINT c,c1,c2,c3,up,lo,dmy;
8408: IndArray ivect;
8409: unsigned char *ivc;
8410: unsigned short *ivs;
8411: unsigned int *ivi;
8412: NDV redv;
8413: NMV mr;
8414: NODE rp;
8415: int maxrs;
8416:
8417: maxrs = 0;
8418: for ( i = 0; i < nred; i++ ) {
8419: ivect = imat[i];
8420: k = ivect->head;
8421: if ( (c = svect[k]) != 0 ) {
8422: maxrs = MAX(maxrs,rp0[i]->sugar);
8423: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
8424: len = LEN(redv); mr = BDY(redv);
8425: svect[k] = 0; prev = k;
8426: switch ( ivect->width ) {
8427: case 1:
8428: ivc = ivect->index.c;
8429: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8430: pos = prev+ivc[j]; prev = pos;
8431: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8432: }
8433: break;
8434: case 2:
8435: ivs = ivect->index.s;
8436: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8437: pos = prev+ivs[j]; prev = pos;
8438: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8439: }
8440: break;
8441: case 4:
8442: ivi = ivect->index.i;
8443: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8444: pos = prev+ivi[j]; prev = pos;
8445: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8446: }
8447: break;
8448: }
8449: }
8450: }
8451: return maxrs;
8452: }
8453:
8454: ND nd_add_lf(ND p1,ND p2)
8455: {
8456: int n,c,can;
8457: ND r;
8458: NM m1,m2,mr0,mr,s;
8459: Z t;
8460:
8461: if ( !p1 ) return p2;
8462: else if ( !p2 ) return p1;
8463: else {
8464: can = 0;
8465: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
8466: c = DL_COMPARE(DL(m1),DL(m2));
8467: switch ( c ) {
8468: case 0:
8469: addlf(CZ(m1),CZ(m2),&t);
8470: s = m1; m1 = NEXT(m1);
8471: if ( t ) {
8472: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
8473: } else {
8474: can += 2; FREENM(s);
8475: }
8476: s = m2; m2 = NEXT(m2); FREENM(s);
8477: break;
8478: case 1:
8479: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
8480: break;
8481: case -1:
8482: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
8483: break;
8484: }
8485: }
8486: if ( !mr0 )
8487: if ( m1 ) mr0 = m1;
8488: else if ( m2 ) mr0 = m2;
8489: else return 0;
8490: else if ( m1 ) NEXT(mr) = m1;
8491: else if ( m2 ) NEXT(mr) = m2;
8492: else NEXT(mr) = 0;
8493: BDY(p1) = mr0;
8494: SG(p1) = MAX(SG(p1),SG(p2));
8495: LEN(p1) = LEN(p1)+LEN(p2)-can;
8496: FREEND(p2);
8497: return p1;
8498: }
8499: }
8500:
8501: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8502: {
8503: int i,j,k,len,pos,prev;
8504: mpz_t c,mc,c1;
8505: IndArray ivect;
8506: unsigned char *ivc;
8507: unsigned short *ivs;
8508: unsigned int *ivi;
8509: NDV redv;
8510: NMV mr;
8511: NODE rp;
8512: int maxrs;
8513:
8514: maxrs = 0;
8515: lf_lazy = 1;
8516: for ( i = 0; i < nred; i++ ) {
8517: ivect = imat[i];
8518: k = ivect->head;
8519: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
8520: if ( mpz_sgn(svect[k]) ) {
8521: maxrs = MAX(maxrs,rp0[i]->sugar);
8522: mpz_neg(svect[k],svect[k]);
8523: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
8524: len = LEN(redv); mr = BDY(redv);
8525: prev = k;
8526: switch ( ivect->width ) {
8527: case 1:
8528: ivc = ivect->index.c;
8529: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8530: pos = prev+ivc[j]; prev = pos;
8531: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8532: }
8533: break;
8534: case 2:
8535: ivs = ivect->index.s;
8536: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8537: pos = prev+ivs[j]; prev = pos;
8538: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8539: }
8540: break;
8541: case 4:
8542: ivi = ivect->index.i;
8543: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8544: pos = prev+ivi[j]; prev = pos;
8545: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8546: }
8547: break;
8548: }
8549: mpz_set_ui(svect[k],0);
8550: }
8551: }
8552: lf_lazy=0;
8553: for ( i = 0; i < col; i++ ) {
8554: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
8555: }
8556: return maxrs;
8557: }
8558:
8559: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
8560: {
8561: int i,j,k,l,rank,s;
8562: mpz_t a,a1,inv;
8563: mpz_t *t,*pivot,*pk;
8564: mpz_t **mat;
8565: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
8566: int size,size1;
8567:
8568: mpz_init(inv);
8569: mpz_init(a);
8570: mat = (mpz_t **)mat0;
8571: size = 0;
8572: for ( rank = 0, j = 0; j < col; j++ ) {
8573: for ( i = rank; i < row; i++ ) {
8574: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
8575: }
8576: for ( i = rank; i < row; i++ )
8577: if ( mpz_sgn(mat[i][j]) )
8578: break;
8579: if ( i == row ) {
8580: colstat[j] = 0;
8581: continue;
8582: } else
8583: colstat[j] = 1;
8584: if ( i != rank ) {
8585: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8586: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8587: }
8588: pivot = mat[rank];
8589: s = sugar[rank];
8590: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
8591: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8592: if ( mpz_sgn(*pk) ) {
8593: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
8594: }
8595: for ( i = rank+1; i < row; i++ ) {
8596: t = mat[i];
8597: if ( mpz_sgn(t[j]) ) {
8598: sugar[i] = MAX(sugar[i],s);
8599: mpz_neg(a,t[j]);
8600: red_by_vect_lf(t+j,pivot+j,a,col-j);
8601: }
8602: }
8603: rank++;
8604: }
8605: for ( j = col-1, l = rank-1; j >= 0; j-- )
8606: if ( colstat[j] ) {
8607: pivot = mat[l];
8608: s = sugar[l];
8609: for ( k = j; k < col; k++ )
8610: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
8611: for ( i = 0; i < l; i++ ) {
8612: t = mat[i];
8613: if ( mpz_sgn(t[j]) ) {
8614: sugar[i] = MAX(sugar[i],s);
8615: mpz_neg(a,t[j]);
8616: red_by_vect_lf(t+j,pivot+j,a,col-j);
8617: }
8618: }
8619: l--;
8620: }
8621: for ( j = 0, l = 0; l < rank; j++ )
8622: if ( colstat[j] ) {
8623: t = mat[l];
8624: for ( k = j; k < col; k++ ) {
8625: mpz_mod(t[k],t[k],BDY(current_mod_lf));
8626: }
8627: l++;
8628: }
8629: return rank;
8630: }
8631:
8632:
8633: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
8634: {
8635: int j,k,len;
8636: UINT *p;
8637: UINT c;
8638: NDV r;
8639: NMV mr0,mr;
8640:
8641: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8642: if ( !len ) return 0;
8643: else {
8644: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8645: #if 0
8646: ndv_alloc += nmv_adv*len;
8647: #endif
8648: mr = mr0;
8649: p = s0vect;
8650: for ( j = k = 0; j < col; j++, p += nd_wpd )
8651: if ( !rhead[j] ) {
8652: if ( (c = vect[k++]) != 0 ) {
8653: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8654: }
8655: }
8656: MKNDV(nd_nvar,mr0,len,r);
8657: return r;
8658: }
8659: }
8660:
1.40 noro 8661: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
8662: {
8663: int j,k,len;
8664: UINT *p;
8665: UINT c;
8666: NDV r;
8667: NMV mr0,mr;
8668:
8669: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8670: if ( !len ) return 0;
8671: else {
8672: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8673: mr = mr0;
8674: p = s0vect;
8675: for ( j = k = 0; j < col; j++, p += nd_wpd )
8676: if ( (c = vect[k++]) != 0 ) {
8677: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8678: }
8679: MKNDV(nd_nvar,mr0,len,r);
8680: return r;
8681: }
8682: }
8683:
1.1 noro 8684: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
8685: {
8686: int j,k,len;
8687: UINT *p;
8688: NDV r;
8689: NMV mr0,mr;
8690:
8691: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
8692: if ( !len ) return 0;
8693: else {
8694: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8695: mr = mr0;
8696: p = s0vect;
8697: for ( j = 0; j < col; j++, p += nd_wpd )
8698: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
8699: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
8700: }
8701: MKNDV(nd_nvar,mr0,len,r);
8702: return r;
8703: }
8704: }
8705:
8706: /* for preprocessed vector */
8707:
8708: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
8709: {
1.6 noro 8710: int j,k,len;
8711: UINT *p;
8712: Z c;
8713: NDV r;
8714: NMV mr0,mr;
1.1 noro 8715:
1.6 noro 8716: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8717: if ( !len ) return 0;
8718: else {
8719: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 8720: #if 0
1.6 noro 8721: ndv_alloc += nmv_adv*len;
1.1 noro 8722: #endif
1.6 noro 8723: mr = mr0;
8724: p = s0vect;
8725: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
8726: if ( !rhead[j] ) {
8727: if ( (c = vect[k++]) != 0 ) {
8728: if ( !INT(c) )
8729: error("vect_to_ndv_q : components must be integers");
8730: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
8731: }
8732: }
1.1 noro 8733: }
1.6 noro 8734: MKNDV(nd_nvar,mr0,len,r);
8735: return r;
8736: }
1.1 noro 8737: }
8738:
8739: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8740: {
8741: int j,k,len;
8742: UINT *p;
8743: mpz_t c;
8744: NDV r;
8745: NMV mr0,mr;
8746:
8747: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8748: if ( !len ) return 0;
8749: else {
8750: mr0 = (NMV)MALLOC(nmv_adv*len);
8751: #if 0
8752: ndv_alloc += nmv_adv*len;
8753: #endif
8754: mr = mr0;
8755: p = s0vect;
8756: for ( j = k = 0; j < col; j++, p += nd_wpd )
8757: if ( !rhead[j] ) {
8758: c[0] = vect[k++][0];
8759: if ( mpz_sgn(c) ) {
8760: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8761: }
8762: }
8763: MKNDV(nd_nvar,mr0,len,r);
8764: return r;
8765: }
8766: }
8767:
8768: /* for plain vector */
8769:
8770: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8771: {
8772: int j,k,len;
8773: UINT *p;
8774: Z c;
8775: NDV r;
8776: NMV mr0,mr;
8777:
8778: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8779: if ( !len ) return 0;
8780: else {
8781: mr0 = (NMV)MALLOC(nmv_adv*len);
8782: #if 0
8783: ndv_alloc += nmv_adv*len;
8784: #endif
8785: mr = mr0;
8786: p = s0vect;
8787: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8788: if ( (c = vect[k]) != 0 ) {
8789: if ( !INT(c) )
1.6 noro 8790: error("plain_vect_to_ndv_q : components must be integers");
8791: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8792: }
8793: MKNDV(nd_nvar,mr0,len,r);
8794: return r;
8795: }
8796: }
8797:
8798: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8799: {
8800: ND_pairs t;
8801: NODE sp0,sp;
8802: int stat;
8803: ND spol;
8804:
8805: for ( t = l; t; t = NEXT(t) ) {
8806: stat = nd_sp(m,trace,t,&spol);
8807: if ( !stat ) return 0;
8808: if ( spol ) {
8809: add_pbucket_symbolic(bucket,spol);
8810: }
8811: }
8812: return 1;
8813: }
8814:
8815: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8816: {
8817: NODE rp0,rp;
8818: NM mul,head,s0,s;
8819: int index,col,i,sugar;
8820: RHist h;
8821: UINT *s0v,*p;
8822: NM_ind_pair pair;
8823: ND red;
8824: NDV *ps;
8825:
8826: s0 = 0; rp0 = 0; col = 0;
8827: if ( nd_demand )
8828: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8829: else
8830: ps = trace?nd_ps_trace:nd_ps;
8831: while ( 1 ) {
8832: head = remove_head_pbucket_symbolic(bucket);
8833: if ( !head ) break;
8834: if ( !s0 ) s0 = head;
8835: else NEXT(s) = head;
8836: s = head;
8837: index = ndl_find_reducer(DL(head));
8838: if ( index >= 0 ) {
8839: h = nd_psh[index];
8840: NEWNM(mul);
8841: ndl_sub(DL(head),DL(h),DL(mul));
8842: if ( ndl_check_bound2(index,DL(mul)) )
8843: return 0;
8844: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8845: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8846: red = ndv_mul_nm_symbolic(mul,ps[index]);
8847: add_pbucket_symbolic(bucket,nd_remove_head(red));
8848: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8849: }
8850: col++;
8851: }
8852: if ( rp0 ) NEXT(rp) = 0;
8853: NEXT(s) = 0;
8854: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8855: for ( i = 0, p = s0v, s = s0; i < col;
8856: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8857: *s0vect = s0v;
8858: *r = rp0;
8859: return col;
8860: }
8861:
8862: void print_ndp(ND_pairs l)
8863: {
8864: ND_pairs t;
8865:
8866: for ( t = l; t; t = NEXT(t) )
8867: printf("[%d,%d] ",t->i1,t->i2);
8868: printf("\n");
8869: }
8870:
8871: NODE nd_f4(int m,int checkonly,int **indp)
8872: {
8873: int i,nh,stat,index,f4red;
8874: NODE r,g,tn0,tn,node;
8875: ND_pairs d,l,t,ll0,ll,lh;
8876: LIST l0,l1;
8877: ND spol,red;
8878: NDV nf,redv;
8879: NM s0,s;
8880: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8881: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8882: UINT c;
8883: UINT **spmat;
8884: UINT *s0vect,*svect,*p,*v;
8885: int *colstat;
8886: IndArray *imat;
8887: int *rhead;
8888: int spcol,sprow;
8889: int sugar,sugarh;
8890: PGeoBucket bucket;
8891: struct oEGT eg0,eg1,eg_f4;
8892: Z i1,i2,sugarq;
1.12 noro 8893:
8894: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8895: #if 0
8896: ndv_alloc = 0;
8897: #endif
1.11 noro 8898: Nf4_red=0;
1.1 noro 8899: g = 0; d = 0;
8900: for ( i = 0; i < nd_psn; i++ ) {
8901: d = update_pairs(d,g,i,0);
8902: g = update_base(g,i);
8903: }
8904: nzlist = 0;
8905: nzlist_t = nd_nzlist;
8906: f4red = 1;
8907: nd_last_nonzero = 0;
8908: while ( d ) {
8909: get_eg(&eg0);
8910: l = nd_minsugarp(d,&d);
8911: sugar = nd_sugarweight?l->sugar2:SG(l);
8912: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8913: if ( nzlist_t ) {
8914: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8915: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8916: tn = BDY((LIST)ARG1(node));
8917: if ( !tn ) {
8918: nzlist_t = NEXT(nzlist_t);
8919: continue;
8920: }
8921: /* tn = [[i1,i2],...] */
8922: lh = nd_ipairtospair(tn);
8923: }
8924: bucket = create_pbucket();
8925: stat = nd_sp_f4(m,0,l,bucket);
8926: if ( !stat ) {
8927: for ( t = l; NEXT(t); t = NEXT(t) );
8928: NEXT(t) = d; d = l;
8929: d = nd_reconstruct(0,d);
8930: continue;
8931: }
8932: if ( bucket->m < 0 ) continue;
8933: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8934: if ( !col ) {
8935: for ( t = l; NEXT(t); t = NEXT(t) );
8936: NEXT(t) = d; d = l;
8937: d = nd_reconstruct(0,d);
8938: continue;
8939: }
1.12 noro 8940: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8941: if ( DP_Print )
1.6 noro 8942: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8943: sugar,eg_f4.exectime);
1.1 noro 8944: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8945: if ( checkonly && nflist ) return 0;
8946: /* adding new bases */
8947: if ( nflist ) nd_last_nonzero = f4red;
8948: for ( r = nflist; r; r = NEXT(r) ) {
8949: nf = (NDV)BDY(r);
1.38 noro 8950: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8951: ndv_removecont(m,nf);
8952: if ( !m && nd_nalg ) {
8953: ND nf1;
8954:
8955: nf1 = ndvtond(m,nf);
8956: nd_monic(0,&nf1);
8957: nd_removecont(m,nf1);
8958: nf = ndtondv(m,nf1);
8959: }
1.24 noro 8960: nh = ndv_newps(m,nf,0);
1.1 noro 8961: d = update_pairs(d,g,nh,0);
8962: g = update_base(g,nh);
8963: }
8964: if ( DP_Print ) {
8965: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8966: }
8967: if ( nd_gentrace ) {
8968: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8969: NEXTNODE(tn0,tn);
1.6 noro 8970: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8971: node = mknode(2,i1,i2); MKLIST(l0,node);
8972: BDY(tn) = l0;
8973: }
8974: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8975: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8976: MKNODE(node,l1,nzlist); nzlist = node;
8977: }
8978: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8979: f4red++;
8980: if ( nd_f4red && f4red > nd_f4red ) break;
8981: if ( nd_rank0 && !nflist ) break;
8982: }
8983: if ( nd_gentrace ) {
8984: MKLIST(l0,reverse_node(nzlist));
8985: MKNODE(nd_alltracelist,l0,0);
8986: }
8987: #if 0
8988: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8989: #endif
1.12 noro 8990: if ( DP_Print ) {
8991: fprintf(asir_out,"number of red=%d,",Nf4_red);
8992: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8993: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8994: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8995: }
1.1 noro 8996: conv_ilist(nd_demand,0,g,indp);
8997: return g;
8998: }
8999:
9000: NODE nd_f4_trace(int m,int **indp)
9001: {
9002: int i,nh,stat,index;
9003: NODE r,g;
9004: ND_pairs d,l,l0,t;
9005: ND spol,red;
9006: NDV nf,redv,nfqv,nfv;
9007: NM s0,s;
9008: NODE rp0,srp0,nflist;
9009: int nsp,nred,col,rank,len,k,j,a;
9010: UINT c;
9011: UINT **spmat;
9012: UINT *s0vect,*svect,*p,*v;
9013: int *colstat;
9014: IndArray *imat;
9015: int *rhead;
9016: int spcol,sprow;
9017: int sugar;
9018: PGeoBucket bucket;
9019: struct oEGT eg0,eg1,eg_f4;
9020:
9021: g = 0; d = 0;
9022: for ( i = 0; i < nd_psn; i++ ) {
9023: d = update_pairs(d,g,i,0);
9024: g = update_base(g,i);
9025: }
9026: while ( d ) {
9027: get_eg(&eg0);
9028: l = nd_minsugarp(d,&d);
9029: sugar = SG(l);
9030: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9031: bucket = create_pbucket();
9032: stat = nd_sp_f4(m,0,l,bucket);
9033: if ( !stat ) {
9034: for ( t = l; NEXT(t); t = NEXT(t) );
9035: NEXT(t) = d; d = l;
9036: d = nd_reconstruct(1,d);
9037: continue;
9038: }
9039: if ( bucket->m < 0 ) continue;
9040: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9041: if ( !col ) {
9042: for ( t = l; NEXT(t); t = NEXT(t) );
9043: NEXT(t) = d; d = l;
9044: d = nd_reconstruct(1,d);
9045: continue;
9046: }
9047: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9048: if ( DP_Print )
1.3 noro 9049: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 9050: sugar,eg_f4.exectime);
1.1 noro 9051: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9052: if ( !l0 ) continue;
9053: l = l0;
9054:
9055: /* over Q */
9056: bucket = create_pbucket();
9057: stat = nd_sp_f4(0,1,l,bucket);
9058: if ( !stat ) {
9059: for ( t = l; NEXT(t); t = NEXT(t) );
9060: NEXT(t) = d; d = l;
9061: d = nd_reconstruct(1,d);
9062: continue;
9063: }
9064: if ( bucket->m < 0 ) continue;
9065: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9066: if ( !col ) {
9067: for ( t = l; NEXT(t); t = NEXT(t) );
9068: NEXT(t) = d; d = l;
9069: d = nd_reconstruct(1,d);
9070: continue;
9071: }
9072: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
9073: /* adding new bases */
9074: for ( r = nflist; r; r = NEXT(r) ) {
9075: nfqv = (NDV)BDY(r);
9076: ndv_removecont(0,nfqv);
1.6 noro 9077: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 9078: if ( nd_nalg ) {
9079: ND nf1;
9080:
9081: nf1 = ndvtond(m,nfqv);
9082: nd_monic(0,&nf1);
9083: nd_removecont(0,nf1);
9084: nfqv = ndtondv(0,nf1); nd_free(nf1);
9085: }
9086: nfv = ndv_dup(0,nfqv);
9087: ndv_mod(m,nfv);
9088: ndv_removecont(m,nfv);
1.24 noro 9089: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 9090: d = update_pairs(d,g,nh,0);
9091: g = update_base(g,nh);
9092: }
9093: }
9094: #if 0
9095: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
9096: #endif
9097: conv_ilist(nd_demand,1,g,indp);
9098: return g;
9099: }
9100:
9101: int rref(matrix mat,int *sugar)
9102: {
9103: int row,col,i,j,k,l,s,wcol,wj;
9104: unsigned long bj;
9105: unsigned long **a;
9106: unsigned long *ai,*ak,*as,*t;
9107: int *pivot;
9108:
9109: row = mat->row;
9110: col = mat->col;
9111: a = mat->a;
9112: wcol = (col+BLEN-1)/BLEN;
9113: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
9114: i = 0;
9115: for ( j = 0; j < col; j++ ) {
9116: wj = j/BLEN; bj = 1L<<(j%BLEN);
9117: for ( k = i; k < row; k++ )
9118: if ( a[k][wj] & bj ) break;
9119: if ( k == row ) continue;
9120: pivot[i] = j;
9121: if ( k != i ) {
9122: t = a[i]; a[i] = a[k]; a[k] = t;
9123: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
9124: }
9125: ai = a[i];
9126: for ( k = i+1; k < row; k++ ) {
9127: ak = a[k];
9128: if ( ak[wj] & bj ) {
9129: for ( l = wj; l < wcol; l++ )
9130: ak[l] ^= ai[l];
9131: sugar[k] = MAX(sugar[k],sugar[i]);
9132: }
9133: }
9134: i++;
9135: }
9136: for ( k = i-1; k >= 0; k-- ) {
9137: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
9138: ak = a[k];
9139: for ( s = 0; s < k; s++ ) {
9140: as = a[s];
9141: if ( as[wj] & bj ) {
9142: for ( l = wj; l < wcol; l++ )
9143: as[l] ^= ak[l];
9144: sugar[s] = MAX(sugar[s],sugar[k]);
9145: }
9146: }
9147: }
9148: return i;
9149: }
9150:
9151: void print_matrix(matrix mat)
9152: {
9153: int row,col,i,j;
9154: unsigned long *ai;
9155:
9156: row = mat->row;
9157: col = mat->col;
9158: printf("%d x %d\n",row,col);
9159: for ( i = 0; i < row; i++ ) {
9160: ai = mat->a[i];
9161: for ( j = 0; j < col; j++ ) {
9162: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
9163: else putchar('0');
9164: }
9165: putchar('\n');
9166: }
9167: }
9168:
9169: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
9170:
9171: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
9172: {
9173: int row,col,wcol,wj,i,j;
9174: unsigned long bj;
9175: unsigned long *ai;
9176: unsigned long **a;
9177: int len;
9178: int *pos;
9179:
9180: row = mat->row;
9181: col = mat->col;
9182: wcol = (col+BLEN-1)/BLEN;
9183: pos = (int *)MALLOC(wcol*sizeof(int));
9184: bzero(pos,wcol*sizeof(int));
9185: for ( i = j = 0; i < wcol; i++ )
9186: if ( v[i] ) pos[j++] = i;;
9187: len = j;
9188: wj = rhead/BLEN;
9189: bj = 1L<<rhead%BLEN;
9190: a = mat->a;
9191: for ( i = 0; i < row; i++ ) {
9192: ai = a[i];
9193: if ( ai[wj]&bj ) {
9194: for ( j = 0; j < len; j++ )
9195: ai[pos[j]] ^= v[pos[j]];
9196: sugar[i] = MAX(sugar[i],rsugar);
9197: }
9198: }
9199: }
9200:
9201: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
9202: {
9203: int nsp,nred,i,i0,k,rank,row;
9204: NODE r0,rp;
9205: ND_pairs sp;
9206: ND spol;
9207: NM_ind_pair rt;
9208: int *s0hash;
9209: UINT *s;
9210: int *pivot,*sugar,*head;
9211: matrix mat;
9212: NM m;
9213: NODE r;
9214: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
9215: int rhead,rsugar,size;
9216: unsigned long *v;
9217:
9218: get_eg(&eg0);
9219: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
9220: nred = length(rp0);
9221: mat = alloc_matrix(nsp,col);
9222: s0hash = (int *)MALLOC(col*sizeof(int));
9223: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
9224: s0hash[i] = ndl_hash_value(s);
9225:
9226: sugar = (int *)MALLOC(nsp*sizeof(int));
9227: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
9228: nd_sp(2,0,sp,&spol);
9229: if ( spol ) {
9230: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
9231: sugar[i] = SG(spol);
9232: i++;
9233: }
9234: }
9235: mat->row = i;
9236: if ( DP_Print ) {
9237: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
9238: }
9239: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
9240: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
9241: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
9242: rt = (NM_ind_pair)BDY(rp);
9243: bzero(v,size);
9244: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
9245: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
9246: red_by_vect_2(mat,sugar,v,rhead,rsugar);
9247: }
9248:
9249: get_eg(&eg1);
9250: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
9251: rank = rref(mat,sugar);
9252:
9253: for ( i = 0, r0 = 0; i < rank; i++ ) {
9254: NEXTNODE(r0,r);
9255: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
9256: SG((NDV)BDY(r)) = sugar[i];
9257: }
9258: if ( r0 ) NEXT(r) = 0;
9259: get_eg(&eg2);
9260: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
9261: if ( DP_Print ) {
9262: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 9263: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 9264: fflush(asir_out);
9265: }
9266: return r0;
9267: }
9268:
9269:
9270: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
9271: {
9272: IndArray *imat;
1.11 noro 9273: int nsp,nred,i,start;
1.1 noro 9274: int *rhead;
9275: NODE r0,rp;
9276: ND_pairs sp;
9277: NM_ind_pair *rvect;
9278: UINT *s;
9279: int *s0hash;
1.11 noro 9280: struct oEGT eg0,eg1,eg_conv;
1.1 noro 9281:
9282: if ( m == 2 && nd_rref2 )
9283: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
9284:
9285: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
9286: nred = length(rp0);
9287: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
9288: rhead = (int *)MALLOC(col*sizeof(int));
9289: for ( i = 0; i < col; i++ ) rhead[i] = 0;
9290:
9291: /* construction of index arrays */
1.11 noro 9292: get_eg(&eg0);
1.1 noro 9293: if ( DP_Print ) {
1.11 noro 9294: fprintf(asir_out,"%dx%d,",nsp+nred,col);
9295: fflush(asir_out);
1.1 noro 9296: }
9297: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 9298: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 9299: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 9300: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 9301: rhead[imat[i]->head] = 1;
1.11 noro 9302: start = imat[i]->head;
9303: }
1.12 noro 9304: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 9305: if ( DP_Print ) {
9306: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
9307: fflush(asir_out);
1.1 noro 9308: }
9309: if ( m > 0 )
1.7 noro 9310: #if SIZEOF_LONG==8
1.1 noro 9311: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9312: #else
9313: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9314: #endif
9315: else if ( m == -1 )
9316: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
9317: else if ( m == -2 )
9318: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9319: else
9320: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
9321: return r0;
9322: }
9323:
9324: /* for Fp, 2<=p<2^16 */
9325:
9326: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9327: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9328: {
9329: int spcol,sprow,a;
9330: int i,j,k,l,rank;
9331: NODE r0,r;
9332: ND_pairs sp;
9333: ND spol;
9334: UINT **spmat;
9335: UINT *svect,*v;
9336: int *colstat;
9337: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9338: int maxrs;
9339: int *spsugar;
9340: ND_pairs *spactive;
9341:
9342: spcol = col-nred;
9343: get_eg(&eg0);
9344: /* elimination (1st step) */
9345: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9346: svect = (UINT *)MALLOC(col*sizeof(UINT));
9347: spsugar = (int *)MALLOC(nsp*sizeof(int));
9348: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9349: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9350: nd_sp(m,0,sp,&spol);
9351: if ( !spol ) continue;
9352: nd_to_vect(m,s0vect,col,spol,svect);
9353: if ( m == -1 )
9354: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9355: else
1.40 noro 9356: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 9357: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9358: if ( i < col ) {
9359: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9360: for ( j = k = 0; j < col; j++ )
9361: if ( !rhead[j] ) v[k++] = svect[j];
9362: spsugar[sprow] = MAX(maxrs,SG(spol));
9363: if ( nz )
9364: spactive[sprow] = sp;
9365: sprow++;
9366: }
9367: nd_free(spol);
9368: }
9369: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9370: if ( DP_Print ) {
1.5 noro 9371: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9372: fflush(asir_out);
9373: }
9374: /* free index arrays */
9375: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9376:
9377: /* elimination (2nd step) */
9378: colstat = (int *)MALLOC(spcol*sizeof(int));
9379: if ( m == -1 )
9380: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9381: else
9382: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9383: r0 = 0;
9384: for ( i = 0; i < rank; i++ ) {
9385: NEXTNODE(r0,r); BDY(r) =
9386: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9387: SG((NDV)BDY(r)) = spsugar[i];
9388: GCFREE(spmat[i]);
9389: }
9390: if ( r0 ) NEXT(r) = 0;
9391:
9392: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9393: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9394: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9395: if ( DP_Print ) {
1.5 noro 9396: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9397: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9398: nsp,nred,sprow,spcol,rank);
1.5 noro 9399: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9400: }
9401: if ( nz ) {
9402: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9403: if ( rank > 0 ) {
9404: NEXT(spactive[rank-1]) = 0;
9405: *nz = spactive[0];
9406: } else
9407: *nz = 0;
9408: }
9409: return r0;
9410: }
9411:
1.40 noro 9412: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9413: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
9414: {
9415: int spcol,sprow,a;
9416: int i,j,k,l,rank;
9417: NODE r0,r;
9418: ND_pairs sp;
9419: ND spol;
9420: UINT **spmat;
9421: UINT *svect,*cvect;
9422: UINT *v;
9423: int *colstat;
9424: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9425: int maxrs;
9426: int *spsugar;
9427: ND_pairs *spactive;
9428: SIG *spsig;
9429:
9430: get_eg(&eg0);
9431: /* elimination (1st step) */
9432: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9433: spsugar = (int *)MALLOC(nsp*sizeof(int));
9434: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
9435: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9436: nd_sp(m,0,sp,&spol);
9437: if ( !spol ) {
9438: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9439: continue;
9440: }
9441: svect = (UINT *)MALLOC(col*sizeof(UINT));
9442: nd_to_vect(m,s0vect,col,spol,svect);
9443: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
9444: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9445: if ( i < col ) {
9446: spmat[sprow] = svect;
9447: spsugar[sprow] = MAX(maxrs,SG(spol));
9448: spsig[sprow] = sp->sig;
9449: sprow++;
9450: } else {
9451: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9452: }
9453: nd_free(spol);
9454: }
9455: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
9456: if ( DP_Print ) {
9457: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9458: fflush(asir_out);
9459: }
9460: /* free index arrays */
9461: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9462:
9463: /* elimination (2nd step) */
9464: colstat = (int *)MALLOC(col*sizeof(int));
9465: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
9466: r0 = 0;
9467: for ( i = 0; i < sprow; i++ ) {
9468: if ( spsugar[i] >= 0 ) {
9469: NEXTNODE(r0,r);
9470: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
9471: SG((NDV)BDY(r)) = spsugar[i];
9472: ((NDV)BDY(r))->sig = spsig[i];
9473: } else
9474: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
9475: GCFREE(spmat[i]);
9476: }
9477: if ( r0 ) NEXT(r) = 0;
9478: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
9479: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9480: if ( DP_Print ) {
9481: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9482: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9483: nsp,nred,sprow,col,rank);
9484: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9485: }
9486: return r0;
9487: }
9488:
1.1 noro 9489:
9490: /* for small finite fields */
9491:
9492: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9493: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9494: {
9495: int spcol,sprow,a;
9496: int i,j,k,l,rank;
9497: NODE r0,r;
9498: ND_pairs sp;
9499: ND spol;
9500: UINT **spmat;
9501: UINT *svect,*v;
9502: int *colstat;
9503: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9504: int maxrs;
9505: int *spsugar;
9506: ND_pairs *spactive;
9507:
9508: spcol = col-nred;
9509: get_eg(&eg0);
9510: /* elimination (1st step) */
9511: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9512: svect = (UINT *)MALLOC(col*sizeof(UINT));
9513: spsugar = (int *)MALLOC(nsp*sizeof(int));
9514: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9515: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9516: nd_sp(m,0,sp,&spol);
9517: if ( !spol ) continue;
9518: nd_to_vect(m,s0vect,col,spol,svect);
9519: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9520: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9521: if ( i < col ) {
9522: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9523: for ( j = k = 0; j < col; j++ )
9524: if ( !rhead[j] ) v[k++] = svect[j];
9525: spsugar[sprow] = MAX(maxrs,SG(spol));
9526: if ( nz )
9527: spactive[sprow] = sp;
9528: sprow++;
9529: }
9530: nd_free(spol);
9531: }
9532: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9533: if ( DP_Print ) {
1.5 noro 9534: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9535: fflush(asir_out);
9536: }
9537: /* free index arrays */
9538: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9539:
9540: /* elimination (2nd step) */
9541: colstat = (int *)MALLOC(spcol*sizeof(int));
9542: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9543: r0 = 0;
9544: for ( i = 0; i < rank; i++ ) {
9545: NEXTNODE(r0,r); BDY(r) =
9546: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9547: SG((NDV)BDY(r)) = spsugar[i];
9548: GCFREE(spmat[i]);
9549: }
9550: if ( r0 ) NEXT(r) = 0;
9551:
9552: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9553: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9554: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9555: if ( DP_Print ) {
1.5 noro 9556: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9557: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9558: nsp,nred,sprow,spcol,rank);
1.5 noro 9559: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9560: }
9561: if ( nz ) {
9562: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9563: if ( rank > 0 ) {
9564: NEXT(spactive[rank-1]) = 0;
9565: *nz = spactive[0];
9566: } else
9567: *nz = 0;
9568: }
9569: return r0;
9570: }
9571:
9572: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9573: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9574: {
9575: int spcol,sprow,a;
9576: int i,j,k,l,rank;
9577: NODE r0,r;
9578: ND_pairs sp;
9579: ND spol;
9580: mpz_t **spmat;
9581: mpz_t *svect,*v;
9582: int *colstat;
9583: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9584: int maxrs;
9585: int *spsugar;
9586: pointer *w;
9587:
9588: spcol = col-nred;
9589: get_eg(&eg0);
9590: /* elimination (1st step) */
9591: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
9592: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
9593: spsugar = (int *)MALLOC(nsp*sizeof(int));
9594: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9595: nd_sp(m,trace,sp,&spol);
9596: if ( !spol ) continue;
9597: nd_to_vect_lf(s0vect,col,spol,svect);
9598: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
9599: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
9600: if ( i < col ) {
9601: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
9602: for ( j = k = 0; j < col; j++ )
9603: if ( !rhead[j] ) v[k++][0] = svect[j][0];
9604: spsugar[sprow] = MAX(maxrs,SG(spol));
9605: sprow++;
9606: }
9607: /* nd_free(spol); */
9608: }
9609: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9610: if ( DP_Print ) {
1.5 noro 9611: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9612: fflush(asir_out);
9613: }
9614: /* free index arrays */
9615: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9616:
9617: /* elimination (2nd step) */
9618: colstat = (int *)MALLOC(spcol*sizeof(int));
9619: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
9620: w = (pointer *)MALLOC(rank*sizeof(pointer));
9621: for ( i = 0; i < rank; i++ ) {
9622: #if 0
9623: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9624: SG((NDV)w[rank-i-1]) = spsugar[i];
9625: #else
9626: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9627: SG((NDV)w[i]) = spsugar[i];
9628: #endif
9629: /* GCFREE(spmat[i]); */
9630:
9631: }
9632: #if 0
9633: qsort(w,rank,sizeof(NDV),
9634: (int (*)(const void *,const void *))ndv_compare);
9635: #endif
9636: r0 = 0;
9637: for ( i = 0; i < rank; i++ ) {
9638: NEXTNODE(r0,r); BDY(r) = w[i];
9639: }
9640: if ( r0 ) NEXT(r) = 0;
9641:
9642: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9643: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9644: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9645: if ( DP_Print ) {
1.5 noro 9646: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9647: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9648: nsp,nred,sprow,spcol,rank);
1.5 noro 9649: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9650: }
9651: return r0;
9652: }
9653:
9654: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9655: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9656: {
9657: int spcol,sprow,a;
9658: int i,j,k,l,rank;
9659: NODE r0,r;
9660: ND_pairs sp;
9661: ND spol;
9662: Z **spmat;
9663: Z *svect,*v;
9664: int *colstat;
9665: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9666: int maxrs;
9667: int *spsugar;
9668: pointer *w;
9669:
9670: spcol = col-nred;
9671: get_eg(&eg0);
9672: /* elimination (1st step) */
9673: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
9674: svect = (Z *)MALLOC(col*sizeof(Q));
9675: spsugar = (int *)MALLOC(nsp*sizeof(int));
9676: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9677: nd_sp(0,trace,sp,&spol);
9678: if ( !spol ) continue;
9679: nd_to_vect_q(s0vect,col,spol,svect);
9680: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
9681: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9682: if ( i < col ) {
9683: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
9684: for ( j = k = 0; j < col; j++ )
9685: if ( !rhead[j] ) v[k++] = svect[j];
9686: spsugar[sprow] = MAX(maxrs,SG(spol));
9687: sprow++;
9688: }
9689: /* nd_free(spol); */
9690: }
9691: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9692: if ( DP_Print ) {
1.5 noro 9693: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9694: fflush(asir_out);
9695: }
9696: /* free index arrays */
9697: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9698:
9699: /* elimination (2nd step) */
9700: colstat = (int *)MALLOC(spcol*sizeof(int));
9701: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
9702: w = (pointer *)MALLOC(rank*sizeof(pointer));
9703: for ( i = 0; i < rank; i++ ) {
9704: #if 0
9705: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9706: SG((NDV)w[rank-i-1]) = spsugar[i];
9707: #else
9708: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9709: SG((NDV)w[i]) = spsugar[i];
9710: #endif
9711: /* GCFREE(spmat[i]); */
9712: }
9713: #if 0
9714: qsort(w,rank,sizeof(NDV),
9715: (int (*)(const void *,const void *))ndv_compare);
9716: #endif
9717: r0 = 0;
9718: for ( i = 0; i < rank; i++ ) {
9719: NEXTNODE(r0,r); BDY(r) = w[i];
9720: }
9721: if ( r0 ) NEXT(r) = 0;
9722:
9723: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9724: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9725: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9726: if ( DP_Print ) {
1.5 noro 9727: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9728: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9729: nsp,nred,sprow,spcol,rank);
1.5 noro 9730: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9731: }
9732: return r0;
9733: }
9734:
9735: FILE *nd_write,*nd_read;
9736:
9737: void nd_send_int(int a) {
9738: write_int(nd_write,(unsigned int *)&a);
9739: }
9740:
9741: void nd_send_intarray(int *p,int len) {
9742: write_intarray(nd_write,(unsigned int *)p,len);
9743: }
9744:
9745: int nd_recv_int() {
9746: int a;
9747:
9748: read_int(nd_read,(unsigned int *)&a);
9749: return a;
9750: }
9751:
9752: void nd_recv_intarray(int *p,int len) {
9753: read_intarray(nd_read,(unsigned int *)p,len);
9754: }
9755:
9756: void nd_send_ndv(NDV p) {
9757: int len,i;
9758: NMV m;
9759:
9760: if ( !p ) nd_send_int(0);
9761: else {
9762: len = LEN(p);
9763: nd_send_int(len);
9764: m = BDY(p);
9765: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9766: nd_send_int(CM(m));
9767: nd_send_intarray((int *)DL(m),nd_wpd);
9768: }
9769: }
9770: }
9771:
9772: void nd_send_nd(ND p) {
9773: int len,i;
9774: NM m;
9775:
9776: if ( !p ) nd_send_int(0);
9777: else {
9778: len = LEN(p);
9779: nd_send_int(len);
9780: m = BDY(p);
9781: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9782: nd_send_int(CM(m));
9783: nd_send_intarray((int *)DL(m),nd_wpd);
9784: }
9785: }
9786: }
9787:
9788: NDV nd_recv_ndv()
9789: {
9790: int len,i;
9791: NMV m,m0;
9792: NDV r;
9793:
9794: len = nd_recv_int();
9795: if ( !len ) return 0;
9796: else {
9797: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9798: #if 0
9799: ndv_alloc += len*nmv_adv;
9800: #endif
9801: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9802: CM(m) = nd_recv_int();
9803: nd_recv_intarray((int *)DL(m),nd_wpd);
9804: }
9805: MKNDV(nd_nvar,m0,len,r);
9806: return r;
9807: }
9808: }
9809:
9810: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9811: {
9812: int i,j,t,c,rank,inv;
9813: int *ci,*ri;
9814: Z dn;
9815: MAT m,nm;
9816:
9817: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9818: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9819: for ( i = 0; i < row; i++ )
9820: for ( j = 0; j < col; j++ )
9821: mat0[i][j] = 0;
9822: c = col-rank;
9823: for ( i = 0; i < rank; i++ ) {
9824: mat0[i][ri[i]] = dn;
9825: for ( j = 0; j < c; j++ )
9826: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9827: }
9828: return rank;
9829: }
9830:
9831: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9832: {
9833: int i,j,k,l,inv,a,rank,s;
9834: unsigned int *t,*pivot,*pk;
9835: unsigned int **mat;
9836: ND_pairs pair;
9837:
9838: mat = (unsigned int **)mat0;
9839: for ( rank = 0, j = 0; j < col; j++ ) {
9840: for ( i = rank; i < row; i++ )
9841: mat[i][j] %= md;
9842: for ( i = rank; i < row; i++ )
9843: if ( mat[i][j] )
9844: break;
9845: if ( i == row ) {
9846: colstat[j] = 0;
9847: continue;
9848: } else
9849: colstat[j] = 1;
9850: if ( i != rank ) {
9851: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9852: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9853: if ( spactive ) {
9854: pair = spactive[i]; spactive[i] = spactive[rank];
9855: spactive[rank] = pair;
9856: }
9857: }
9858: pivot = mat[rank];
9859: s = sugar[rank];
9860: inv = invm(pivot[j],md);
9861: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9862: if ( *pk ) {
9863: if ( *pk >= (unsigned int)md )
9864: *pk %= md;
9865: DMAR(*pk,inv,0,md,*pk)
9866: }
9867: for ( i = rank+1; i < row; i++ ) {
9868: t = mat[i];
9869: if ( (a = t[j]) != 0 ) {
9870: sugar[i] = MAX(sugar[i],s);
9871: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9872: }
9873: }
9874: rank++;
9875: }
9876: for ( j = col-1, l = rank-1; j >= 0; j-- )
9877: if ( colstat[j] ) {
9878: pivot = mat[l];
9879: s = sugar[l];
9880: for ( i = 0; i < l; i++ ) {
9881: t = mat[i];
9882: t[j] %= md;
9883: if ( (a = t[j]) != 0 ) {
9884: sugar[i] = MAX(sugar[i],s);
9885: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9886: }
9887: }
9888: l--;
9889: }
9890: for ( j = 0, l = 0; l < rank; j++ )
9891: if ( colstat[j] ) {
9892: t = mat[l];
9893: for ( k = j; k < col; k++ )
9894: if ( t[k] >= (unsigned int)md )
9895: t[k] %= md;
9896: l++;
9897: }
9898: return rank;
9899: }
9900:
1.40 noro 9901: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9902: {
9903: int i,j,k,l,rank,s,imin;
9904: UINT inv;
9905: UINT a;
9906: UINT *t,*pivot,*pk;
9907: UINT *ck;
9908: UINT *ct;
9909: ND_pairs pair;
9910: SIG sg;
9911: int *used;
9912:
9913: used = (int *)MALLOC(row*sizeof(int));
9914: for ( j = 0; j < col; j++ ) {
9915: for ( i = 0; i < row; i++ )
9916: a = mat[i][j] %= md;
9917: for ( i = 0; i < row; i++ )
9918: if ( !used[i] && mat[i][j] ) break;
9919: if ( i == row ) {
9920: colstat[j] = 0;
9921: continue;
9922: } else {
9923: colstat[j] = 1;
9924: used[i] = 1;
9925: }
9926: /* column j is normalized */
9927: s = sugar[i];
9928: inv = invm(mat[i][j],md);
9929: /* normalize pivot row */
9930: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9931: DMAR(*pk,inv,0,md,*pk);
9932: }
9933: for ( k = i+1; k < row; k++ ) {
9934: if ( (a = mat[k][j]) != 0 ) {
9935: sugar[k] = MAX(sugar[k],s);
9936: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9937: Nf4_red++;
9938: }
9939: }
9940: }
9941: rank = 0;
9942: for ( i = 0; i < row; i++ ) {
9943: for ( j = 0; j < col; j++ )
9944: if ( mat[i][j] ) break;
9945: if ( j == col ) sugar[i] = -1;
9946: else rank++;
9947: }
9948: return rank;
9949: }
9950:
1.1 noro 9951:
1.7 noro 9952: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9953: {
1.7 noro 9954: int i,j,k,l,inv,a,rank,s;
9955: unsigned int *t,*pivot,*pk;
9956: unsigned int **mat;
9957:
9958: mat = (unsigned int **)mat0;
9959: for ( rank = 0, j = 0; j < col; j++ ) {
9960: for ( i = rank; i < row; i++ )
9961: if ( mat[i][j] )
9962: break;
9963: if ( i == row ) {
9964: colstat[j] = 0;
9965: continue;
9966: } else
9967: colstat[j] = 1;
9968: if ( i != rank ) {
9969: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9970: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9971: }
9972: pivot = mat[rank];
9973: s = sugar[rank];
9974: inv = _invsf(pivot[j]);
9975: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9976: if ( *pk )
9977: *pk = _mulsf(*pk,inv);
9978: for ( i = rank+1; i < row; i++ ) {
9979: t = mat[i];
9980: if ( (a = t[j]) != 0 ) {
9981: sugar[i] = MAX(sugar[i],s);
9982: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9983: }
9984: }
9985: rank++;
9986: }
9987: for ( j = col-1, l = rank-1; j >= 0; j-- )
9988: if ( colstat[j] ) {
9989: pivot = mat[l];
9990: s = sugar[l];
9991: for ( i = 0; i < l; i++ ) {
9992: t = mat[i];
9993: if ( (a = t[j]) != 0 ) {
9994: sugar[i] = MAX(sugar[i],s);
9995: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9996: }
9997: }
9998: l--;
9999: }
10000: return rank;
10001: }
1.1 noro 10002:
1.7 noro 10003: int ndv_ishomo(NDV p)
10004: {
10005: NMV m;
10006: int len,h;
1.1 noro 10007:
10008: if ( !p ) return 1;
10009: len = LEN(p);
10010: m = BDY(p);
10011: h = TD(DL(m));
10012: NMV_ADV(m);
10013: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 10014: if ( TD(DL(m)) != h ) {
10015: return 0;
10016: }
1.1 noro 10017: return 1;
10018: }
10019:
10020: void ndv_save(NDV p,int index)
10021: {
10022: FILE *s;
10023: char name[BUFSIZ];
10024: short id;
10025: int nv,sugar,len,n,i,td,e,j;
10026: NMV m;
10027: unsigned int *dl;
10028: int mpos;
10029:
10030: sprintf(name,"%s/%d",Demand,index);
10031: s = fopen(name,"w");
10032: savevl(s,0);
10033: if ( !p ) {
10034: saveobj(s,0);
10035: return;
10036: }
10037: id = O_DP;
10038: nv = NV(p);
10039: sugar = SG(p);
10040: len = LEN(p);
10041: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
10042: write_int(s,(unsigned int *)&len);
10043:
10044: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 10045: saveobj(s,(Obj)CZ(m));
1.1 noro 10046: dl = DL(m);
10047: td = TD(dl);
10048: write_int(s,(unsigned int *)&td);
10049: for ( j = 0; j < nv; j++ ) {
10050: e = GET_EXP(dl,j);
10051: write_int(s,(unsigned int *)&e);
10052: }
10053: if ( nd_module ) {
10054: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
10055: }
10056: }
10057: fclose(s);
10058: }
10059:
10060: void nd_save_mod(ND p,int index)
10061: {
10062: FILE *s;
10063: char name[BUFSIZ];
10064: int nv,sugar,len,c;
10065: NM m;
10066:
10067: sprintf(name,"%s/%d",Demand,index);
10068: s = fopen(name,"w");
10069: if ( !p ) {
10070: len = 0;
10071: write_int(s,(unsigned int *)&len);
10072: fclose(s);
10073: return;
10074: }
10075: nv = NV(p);
10076: sugar = SG(p);
10077: len = LEN(p);
10078: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
10079: for ( m = BDY(p); m; m = NEXT(m) ) {
10080: c = CM(m); write_int(s,(unsigned int *)&c);
10081: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
10082: }
10083: fclose(s);
10084: }
10085:
10086: NDV ndv_load(int index)
10087: {
10088: FILE *s;
10089: char name[BUFSIZ];
10090: short id;
10091: int nv,sugar,len,n,i,td,e,j;
10092: NDV d;
10093: NMV m0,m;
10094: unsigned int *dl;
10095: Obj obj;
10096: int mpos;
10097:
10098: sprintf(name,"%s/%d",Demand,index);
10099: s = fopen(name,"r");
10100: if ( !s ) return 0;
10101:
10102: skipvl(s);
10103: read_short(s,(unsigned short *)&id);
10104: if ( !id ) return 0;
10105: read_int(s,(unsigned int *)&nv);
10106: read_int(s,(unsigned int *)&sugar);
10107: read_int(s,(unsigned int *)&len);
10108:
10109: m0 = m = MALLOC(len*nmv_adv);
10110: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 10111: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 10112: dl = DL(m);
10113: ndl_zero(dl);
10114: read_int(s,(unsigned int *)&td); TD(dl) = td;
10115: for ( j = 0; j < nv; j++ ) {
10116: read_int(s,(unsigned int *)&e);
10117: PUT_EXP(dl,j,e);
10118: }
10119: if ( nd_module ) {
10120: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
10121: }
10122: if ( nd_blockmask ) ndl_weight_mask(dl);
10123: }
10124: fclose(s);
10125: MKNDV(nv,m0,len,d);
10126: SG(d) = sugar;
10127: return d;
10128: }
10129:
10130: ND nd_load_mod(int index)
10131: {
10132: FILE *s;
10133: char name[BUFSIZ];
10134: int nv,sugar,len,i,c;
10135: ND d;
10136: NM m0,m;
10137:
10138: sprintf(name,"%s/%d",Demand,index);
10139: s = fopen(name,"r");
10140: /* if the file does not exist, it means p[index]=0 */
10141: if ( !s ) return 0;
10142:
10143: read_int(s,(unsigned int *)&nv);
10144: if ( !nv ) { fclose(s); return 0; }
10145:
10146: read_int(s,(unsigned int *)&sugar);
10147: read_int(s,(unsigned int *)&len);
10148: for ( m0 = 0, i = 0; i < len; i++ ) {
10149: NEXTNM(m0,m);
10150: read_int(s,(unsigned int *)&c); CM(m) = c;
10151: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
10152: }
10153: NEXT(m) = 0;
10154: MKND(nv,m0,len,d);
10155: SG(d) = sugar;
10156: fclose(s);
10157: return d;
10158: }
10159:
10160: void nd_det(int mod,MAT f,P *rp)
10161: {
10162: VL fv,tv;
10163: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
10164: pointer **m;
10165: P **w;
10166: P mp,r;
10167: NDV **dm;
10168: NDV *t,*mi,*mj;
10169: NDV d,s,mij,mjj;
10170: ND u;
10171: NMV nmv;
10172: UINT *bound;
10173: PGeoBucket bucket;
10174: struct order_spec *ord;
10175: Z dq,dt,ds;
10176: Z mone;
10177: Z gn,qn,dn0,nm,dn;
10178:
10179: create_order_spec(0,0,&ord);
10180: nd_init_ord(ord);
10181: get_vars((Obj)f,&fv);
10182: if ( f->row != f->col )
10183: error("nd_det : non-square matrix");
10184: n = f->row;
10185: m = f->body;
10186: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
10187:
10188: if ( !nvar ) {
10189: if ( !mod )
10190: detp(CO,(P **)m,n,rp);
10191: else {
10192: w = (P **)almat_pointer(n,n);
10193: for ( i = 0; i < n; i++ )
10194: for ( j = 0; j < n; j++ )
10195: ptomp(mod,(P)m[i][j],&w[i][j]);
10196: detmp(CO,mod,w,n,&mp);
10197: mptop(mp,rp);
10198: }
10199: return;
10200: }
10201:
10202: if ( !mod ) {
10203: w = (P **)almat_pointer(n,n);
10204: dq = ONE;
10205: for ( i = 0; i < n; i++ ) {
10206: dn0 = ONE;
10207: for ( j = 0; j < n; j++ ) {
10208: if ( !m[i][j] ) continue;
10209: lgp(m[i][j],&nm,&dn);
1.6 noro 10210: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 10211: }
10212: if ( !UNIZ(dn0) ) {
10213: ds = dn0;
10214: for ( j = 0; j < n; j++ )
10215: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
10216: mulz(dq,ds,&dt); dq = dt;
10217: } else
10218: for ( j = 0; j < n; j++ )
10219: w[i][j] = (P)m[i][j];
10220: }
10221: m = (pointer **)w;
10222: }
10223:
10224: for ( i = 0, max = 1; i < n; i++ )
10225: for ( j = 0; j < n; j++ )
10226: for ( tv = fv; tv; tv = NEXT(tv) ) {
10227: e = getdeg(tv->v,(P)m[i][j]);
10228: max = MAX(e,max);
10229: }
10230: nd_setup_parameters(nvar,max);
10231: dm = (NDV **)almat_pointer(n,n);
10232: for ( i = 0, max = 1; i < n; i++ )
10233: for ( j = 0; j < n; j++ ) {
10234: dm[i][j] = ptondv(CO,fv,m[i][j]);
10235: if ( mod ) ndv_mod(mod,dm[i][j]);
10236: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
10237: }
10238: d = ptondv(CO,fv,(P)ONE);
10239: if ( mod ) ndv_mod(mod,d);
10240: chsgnz(ONE,&mone);
10241: for ( j = 0, sgn = 1; j < n; j++ ) {
10242: if ( DP_Print ) {
10243: fprintf(asir_out,".");
10244: }
10245: for ( i = j; i < n && !dm[i][j]; i++ );
10246: if ( i == n ) {
10247: *rp = 0;
10248: return;
10249: }
10250: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
10251: for ( k = j; k < n; k++ )
10252: for ( l = j; l < n; l++ )
10253: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
10254: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
10255: }
10256: if ( k0 != j ) {
10257: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
10258: sgn = -sgn;
10259: }
10260: if ( l0 != j ) {
10261: for ( k = j; k < n; k++ ) {
10262: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
10263: }
10264: sgn = -sgn;
10265: }
10266: bound = nd_det_compute_bound(dm,n,j);
10267: for ( k = 0; k < nd_nvar; k++ )
10268: if ( bound[k]*2 > nd_mask0 ) break;
10269: if ( k < nd_nvar )
10270: nd_det_reconstruct(dm,n,j,d);
10271:
10272: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
10273: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
10274: mi = dm[i]; mij = mi[j];
10275: if ( mod )
10276: ndv_mul_c(mod,mij,mod-1);
10277: else
10278: ndv_mul_c_q(mij,mone);
10279: for ( k = j+1; k < n; k++ ) {
10280: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
10281: bucket = create_pbucket();
10282: if ( mi[k] ) {
10283: nmv = BDY(mjj); len = LEN(mjj);
10284: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10285: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
10286: add_pbucket(mod,bucket,u);
10287: }
10288: }
10289: if ( mj[k] && mij ) {
10290: nmv = BDY(mij); len = LEN(mij);
10291: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
10292: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
10293: add_pbucket(mod,bucket,u);
10294: }
10295: }
10296: u = nd_quo(mod,bucket,d);
10297: mi[k] = ndtondv(mod,u);
10298: }
10299: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
10300: }
10301: d = mjj;
10302: }
10303: if ( DP_Print ) {
10304: fprintf(asir_out,"\n");
10305: }
10306: if ( sgn < 0 ) {
10307: if ( mod )
10308: ndv_mul_c(mod,d,mod-1);
10309: else
10310: ndv_mul_c_q(d,mone);
10311: }
10312: r = ndvtop(mod,CO,fv,d);
10313: if ( !mod && !UNIQ(dq) )
10314: divsp(CO,r,(P)dq,rp);
10315: else
10316: *rp = r;
10317: }
10318:
10319: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
10320: {
10321: NM mr,mr0;
10322: NM tnm;
10323: NMV m;
10324: UINT *d0,*dt,*dm;
10325: int c,n,td,i,c1,c2,len;
10326: Z q;
10327: ND r;
10328:
10329: if ( !p ) return 0;
10330: else {
10331: n = NV(p); m = BDY(p); len = LEN(p);
10332: d0 = DL(m0);
10333: td = TD(d);
10334: mr0 = 0;
10335: NEWNM(tnm);
10336: if ( mod ) {
10337: c = CM(m0);
10338: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10339: ndl_add(DL(m),d0,DL(tnm));
10340: if ( ndl_reducible(DL(tnm),d) ) {
10341: NEXTNM(mr0,mr);
10342: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
10343: ndl_copy(DL(tnm),DL(mr));
10344: }
10345: }
10346: } else {
1.6 noro 10347: q = CZ(m0);
1.1 noro 10348: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
10349: ndl_add(DL(m),d0,DL(tnm));
10350: if ( ndl_reducible(DL(tnm),d) ) {
10351: NEXTNM(mr0,mr);
1.6 noro 10352: mulz(CZ(m),q,&CZ(mr));
1.1 noro 10353: ndl_copy(DL(tnm),DL(mr));
10354: }
10355: }
10356: }
10357: if ( !mr0 )
10358: return 0;
10359: else {
10360: NEXT(mr) = 0;
10361: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10362: MKND(NV(p),mr0,len,r);
10363: SG(r) = SG(p) + TD(d0);
10364: return r;
10365: }
10366: }
10367: }
10368:
10369: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
10370: {
10371: int i,obpe,oadv,h,k,l;
10372: static NM prev_nm_free_list;
10373: EPOS oepos;
10374:
10375: obpe = nd_bpe;
10376: oadv = nmv_adv;
10377: oepos = nd_epos;
10378: if ( obpe < 2 ) nd_bpe = 2;
10379: else if ( obpe < 3 ) nd_bpe = 3;
10380: else if ( obpe < 4 ) nd_bpe = 4;
10381: else if ( obpe < 5 ) nd_bpe = 5;
10382: else if ( obpe < 6 ) nd_bpe = 6;
10383: else if ( obpe < 8 ) nd_bpe = 8;
10384: else if ( obpe < 10 ) nd_bpe = 10;
10385: else if ( obpe < 16 ) nd_bpe = 16;
10386: else if ( obpe < 32 ) nd_bpe = 32;
10387: else error("nd_det_reconstruct : exponent too large");
10388:
10389: nd_setup_parameters(nd_nvar,0);
10390: prev_nm_free_list = _nm_free_list;
10391: _nm_free_list = 0;
10392: for ( k = j; k < n; k++ )
10393: for (l = j; l < n; l++ )
10394: ndv_realloc(dm[k][l],obpe,oadv,oepos);
10395: ndv_realloc(d,obpe,oadv,oepos);
10396: prev_nm_free_list = 0;
10397: #if 0
10398: GC_gcollect();
10399: #endif
10400: }
10401:
10402: /* returns a UINT array containing degree bounds */
10403:
10404: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
10405: {
10406: UINT *d0,*d1,*d,*t,*r;
10407: int k,l,i;
10408:
10409: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
10410: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
10411: for ( k = j; k < n; k++ )
10412: for ( l = j; l < n; l++ )
10413: if ( dm[k][l] ) {
10414: d = ndv_compute_bound(dm[k][l]);
10415: for ( i = 0; i < nd_nvar; i++ )
10416: d0[i] = MAX(d0[i],d[i]);
10417: }
10418: return d0;
10419: }
10420:
10421: DL nd_separate_d(UINT *d,UINT *trans)
10422: {
10423: int n,td,i,e,j;
10424: DL a;
10425:
10426: ndl_zero(trans);
10427: td = 0;
10428: for ( i = 0; i < nd_ntrans; i++ ) {
10429: e = GET_EXP(d,i);
10430: PUT_EXP(trans,i,e);
10431: td += MUL_WEIGHT(e,i);
10432: }
10433: if ( nd_ntrans+nd_nalg < nd_nvar ) {
10434: /* homogenized */
10435: i = nd_nvar-1;
10436: e = GET_EXP(d,i);
10437: PUT_EXP(trans,i,e);
10438: td += MUL_WEIGHT(e,i);
10439: }
10440: TD(trans) = td;
10441: if ( nd_blockmask) ndl_weight_mask(trans);
10442: NEWDL(a,nd_nalg);
10443: td = 0;
10444: for ( i = 0; i < nd_nalg; i++ ) {
10445: j = nd_ntrans+i;
10446: e = GET_EXP(d,j);
10447: a->d[i] = e;
10448: td += e;
10449: }
10450: a->td = td;
10451: return a;
10452: }
10453:
10454: int nd_monic(int mod,ND *p)
10455: {
10456: UINT *trans,*t;
10457: DL alg;
10458: MP mp0,mp;
10459: NM m,m0,m1,ma0,ma,mb,mr0,mr;
10460: ND r;
10461: DL dl;
10462: DP nm;
10463: NDV ndv;
10464: DAlg inv,cd;
10465: ND s,c;
10466: Z l,mul;
10467: Z ln;
10468: int n,ntrans,i,e,td,is_lc,len;
10469: NumberField nf;
10470: struct oEGT eg0,eg1;
10471:
10472: if ( !(nf = get_numberfield()) )
10473: error("nd_monic : current_numberfield is not set");
10474:
10475: /* Q coef -> DAlg coef */
10476: NEWNM(ma0); ma = ma0;
10477: m = BDY(*p);
10478: is_lc = 1;
10479: while ( 1 ) {
10480: NEWMP(mp0); mp = mp0;
1.6 noro 10481: mp->c = (Obj)CZ(m);
1.1 noro 10482: mp->dl = nd_separate_d(DL(m),DL(ma));
10483: NEWNM(mb);
10484: for ( m = NEXT(m); m; m = NEXT(m) ) {
10485: alg = nd_separate_d(DL(m),DL(mb));
10486: if ( !ndl_equal(DL(ma),DL(mb)) )
10487: break;
1.6 noro 10488: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 10489: }
10490: NEXT(mp) = 0;
10491: MKDP(nd_nalg,mp0,nm);
10492: MKDAlg(nm,ONE,cd);
10493: if ( is_lc == 1 ) {
10494: /* if the lc is a rational number, we have nothing to do */
10495: if ( !mp0->dl->td )
10496: return 1;
10497:
10498: get_eg(&eg0);
10499: invdalg(cd,&inv);
10500: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
10501: /* check the validity of inv */
10502: if ( mod && !remqi((Q)inv->dn,mod) )
10503: return 0;
10504: CA(ma) = nf->one;
10505: is_lc = 0;
10506: ln = ONE;
10507: } else {
10508: muldalg(cd,inv,&CA(ma));
10509: lcmz(ln,CA(ma)->dn,&ln);
10510: }
10511: if ( m ) {
10512: NEXT(ma) = mb; ma = mb;
10513: } else {
10514: NEXT(ma) = 0;
10515: break;
10516: }
10517: }
10518: /* l = lcm(denoms) */
10519: l = ln;
10520: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 10521: divsz(l,CA(m)->dn,&mul);
1.1 noro 10522: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
10523: NEXTNM(mr0,mr);
1.6 noro 10524: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 10525: dl = mp->dl;
10526: td = TD(DL(m));
10527: ndl_copy(DL(m),DL(mr));
10528: for ( i = 0; i < nd_nalg; i++ ) {
10529: e = dl->d[i];
10530: PUT_EXP(DL(mr),i+nd_ntrans,e);
10531: td += MUL_WEIGHT(e,i+nd_ntrans);
10532: }
10533: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
10534: TD(DL(mr)) = td;
10535: if ( nd_blockmask) ndl_weight_mask(DL(mr));
10536: }
10537: }
10538: NEXT(mr) = 0;
10539: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10540: MKND(NV(*p),mr0,len,r);
10541: /* XXX */
10542: SG(r) = SG(*p);
10543: nd_free(*p);
10544: *p = r;
10545: return 1;
10546: }
10547:
10548: NODE reverse_node(NODE n)
10549: {
10550: NODE t,t1;
10551:
10552: for ( t = 0; n; n = NEXT(n) ) {
10553: MKNODE(t1,BDY(n),t); t = t1;
10554: }
10555: return t;
10556: }
10557:
10558: P ndc_div(int mod,union oNDC a,union oNDC b)
10559: {
10560: union oNDC c;
10561: int inv,t;
10562:
10563: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 10564: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 10565: else if ( mod ) {
10566: inv = invm(b.m,mod);
10567: DMAR(a.m,inv,0,mod,t); c.m = t;
10568: } else if ( nd_vc )
10569: divsp(nd_vc,a.p,b.p,&c.p);
10570: else
10571: divsz(a.z,b.z,&c.z);
10572: return ndctop(mod,c);
10573: }
10574:
10575: P ndctop(int mod,union oNDC c)
10576: {
10577: Z q;
10578: int e;
10579: GFS gfs;
10580:
10581: if ( mod == -1 ) {
10582: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
10583: } else if ( mod == -2 ) {
1.10 noro 10584: q = c.z; return (P)q;
1.1 noro 10585: } else if ( mod > 0 ) {
1.6 noro 10586: STOZ(c.m,q); return (P)q;
1.1 noro 10587: } else
10588: return (P)c.p;
10589: }
10590:
10591: /* [0,0,0,cont] = p -> p/cont */
10592:
10593: void finalize_tracelist(int i,P cont)
10594: {
10595: LIST l;
10596: NODE node;
10597: Z iq;
10598:
10599: if ( !UNIQ(cont) ) {
10600: node = mknode(4,NULLP,NULLP,NULLP,cont);
10601: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
10602: nd_tracelist = node;
10603: }
1.6 noro 10604: STOZ(i,iq);
1.1 noro 10605: nd_tracelist = reverse_node(nd_tracelist);
10606: MKLIST(l,nd_tracelist);
10607: node = mknode(2,iq,l); MKLIST(l,node);
10608: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
10609: nd_alltracelist = node; nd_tracelist = 0;
10610: }
10611:
10612: void conv_ilist(int demand,int trace,NODE g,int **indp)
10613: {
10614: int n,i,j;
10615: int *ind;
10616: NODE t;
10617:
10618: n = length(g);
10619: ind = (int *)MALLOC(n*sizeof(int));
10620: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
10621: j = (long)BDY(t); ind[i] = j;
10622: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
10623: }
10624: if ( indp ) *indp = ind;
10625: }
10626:
1.30 noro 10627: NODE conv_ilist_s(int demand,int trace,int **indp)
10628: {
10629: int n,i,j;
10630: int *ind;
10631: NODE g0,g;
10632:
10633: n = nd_psn;
10634: ind = (int *)MALLOC(n*sizeof(int));
10635: g0 = 0;
10636: for ( i = 0; i < n; i++ ) {
10637: ind[i] = i;
10638: NEXTNODE(g0,g);
10639: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
10640: }
10641: if ( g0 ) NEXT(g) = 0;
10642: if ( indp ) *indp = ind;
10643: return g0;
10644: }
10645:
1.46 noro 10646: void parse_nd_option(VL vl,NODE opt)
1.1 noro 10647: {
1.38 noro 10648: NODE t,p,u;
1.1 noro 10649: int i,s,n;
1.38 noro 10650: char *key;
10651: Obj value;
1.46 noro 10652: VL oldvl;
1.1 noro 10653:
1.41 noro 10654: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 10655: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
10656: nd_splist = 0; nd_check_splist = 0;
1.38 noro 10657: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
10658: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 10659: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46 noro 10660: nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.49 noro 10661: nd_hpdata = 0;
1.39 noro 10662:
1.38 noro 10663: for ( t = opt; t; t = NEXT(t) ) {
10664: p = BDY((LIST)BDY(t));
10665: key = BDY((STRING)BDY(p));
10666: value = (Obj)BDY(NEXT(p));
10667: if ( !strcmp(key,"gentrace") )
10668: nd_gentrace = value?1:0;
10669: else if ( !strcmp(key,"gensyz") )
10670: nd_gensyz = value?1:0;
10671: else if ( !strcmp(key,"nora") )
10672: nd_nora = value?1:0;
1.41 noro 10673: else if ( !strcmp(key,"norb") )
10674: nd_norb = value?1:0;
1.38 noro 10675: else if ( !strcmp(key,"gbblock") ) {
10676: if ( value && OID(value) == O_LIST ) {
1.1 noro 10677: u = BDY((LIST)value);
1.38 noro 10678: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 10679: for ( i = 0; u; u = NEXT(u) ) {
10680: p = BDY((LIST)BDY(u));
1.6 noro 10681: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
10682: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 10683: }
10684: nd_gbblock[i] = -1;
1.38 noro 10685: } else
10686: nd_gbblock = 0;
1.1 noro 10687: } else if ( !strcmp(key,"newelim") )
10688: nd_newelim = value?1:0;
10689: else if ( !strcmp(key,"intersect") )
10690: nd_intersect = value?1:0;
1.17 noro 10691: else if ( !strcmp(key,"syzgen") )
10692: nd_intersect = ZTOS((Q)value);
1.1 noro 10693: else if ( !strcmp(key,"lf") )
10694: nd_lf = value?1:0;
10695: else if ( !strcmp(key,"trace") ) {
1.38 noro 10696: if ( value ) {
10697: u = BDY((LIST)value);
10698: nd_nzlist = BDY((LIST)ARG2(u));
10699: nd_bpe = ZTOS((Q)ARG3(u));
10700: }
1.1 noro 10701: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 10702: nd_f4red = ZTOS((Q)value);
1.1 noro 10703: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 10704: nd_rank0 = value?1:0;
1.1 noro 10705: } else if ( !strcmp(key,"splist") ) {
1.38 noro 10706: nd_splist = value?1:0;
1.1 noro 10707: } else if ( !strcmp(key,"check_splist") ) {
10708: nd_check_splist = BDY((LIST)value);
1.49 noro 10709: } else if ( !strcmp(key,"hpdata") ) {
1.50 noro 10710: if ( value )
10711: nd_hpdata = BDY((LIST)value);
1.1 noro 10712: } else if ( !strcmp(key,"sugarweight") ) {
10713: u = BDY((LIST)value);
1.38 noro 10714: n = length(u);
10715: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 10716: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 10717: nd_sugarweight[i] = ZTOS((Q)BDY(u));
10718: } else if ( !strcmp(key,"f4_td") ) {
10719: nd_f4_td = value?1:0;
10720: } else if ( !strcmp(key,"sba_f4step") ) {
10721: nd_sba_f4step = value?ZTOS((Q)value):0;
10722: } else if ( !strcmp(key,"sba_pot") ) {
1.44 noro 10723: nd_sba_pot = ZTOS((Q)value);
1.38 noro 10724: } else if ( !strcmp(key,"sba_largelcm") ) {
10725: nd_sba_largelcm = value?1:0;
1.39 noro 10726: } else if ( !strcmp(key,"sba_dontsort") ) {
10727: nd_sba_dontsort = value?1:0;
1.44 noro 10728: } else if ( !strcmp(key,"sba_syz") ) {
10729: nd_sba_syz = value?1:0;
1.46 noro 10730: } else if ( !strcmp(key,"sba_modord") ) {
10731: // value=[vlist,ordspec,weight]
10732: u = BDY((LIST)value);
10733: pltovl((LIST)ARG0(u),&oldvl);
10734: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10735: } else if ( !strcmp(key,"sba_gbinput") ) {
10736: nd_sba_inputisgb = value?1:0;
10737: if ( nd_sba_inputisgb != 0 ) {
10738: // value=[vlist,ordspec,weight]
10739: u = BDY((LIST)value);
10740: pltovl((LIST)ARG0(u),&oldvl);
10741: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
10742: }
1.41 noro 10743: } else if ( !strcmp(key,"sba_redundant_check") ) {
10744: nd_sba_redundant_check = value?1:0;
1.39 noro 10745: } else if ( !strcmp(key,"top") ) {
10746: nd_top = value?1:0;
1.1 noro 10747: }
1.38 noro 10748: }
1.44 noro 10749: if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1 noro 10750: }
10751:
10752: ND mdptond(DP d);
10753: ND nd_mul_nm(int mod,NM m0,ND p);
10754: ND nd_mul_nm_lf(NM m0,ND p);
10755: ND *btog(NODE ti,ND **p,int nb,int mod);
10756: ND btog_one(NODE ti,ND *p,int nb,int mod);
10757: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10758: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10759:
10760: /* d:monomial */
10761: ND mdptond(DP d)
10762: {
10763: NM m;
10764: ND r;
10765:
10766: if ( OID(d) == 1 )
10767: r = ptond(CO,CO,(P)d);
10768: else {
10769: NEWNM(m);
10770: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10771: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10772: NEXT(m) = 0;
10773: MKND(NV(d),m,1,r);
10774: }
10775: return r;
10776: }
10777:
10778: ND nd_mul_nm(int mod,NM m0,ND p)
10779: {
10780: UINT *d0;
10781: int c0,c1,c;
10782: NM tm,mr,mr0;
10783: ND r;
10784:
10785: if ( !p ) return 0;
10786: d0 = DL(m0);
10787: c0 = CM(m0);
10788: mr0 = 0;
10789: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10790: NEXTNM(mr0,mr);
10791: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10792: ndl_add(d0,DL(tm),DL(mr));
10793: }
10794: NEXT(mr) = 0;
10795: MKND(NV(p),mr0,LEN(p),r);
10796: return r;
10797: }
10798:
10799: ND nd_mul_nm_lf(NM m0,ND p)
10800: {
10801: UINT *d0;
10802: Z c0,c1,c;
10803: NM tm,mr,mr0;
10804: ND r;
10805:
10806: if ( !p ) return 0;
10807: d0 = DL(m0);
10808: c0 = CZ(m0);
10809: mr0 = 0;
10810: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10811: NEXTNM(mr0,mr);
10812: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10813: ndl_add(d0,DL(tm),DL(mr));
10814: }
10815: NEXT(mr) = 0;
10816: MKND(NV(p),mr0,LEN(p),r);
10817: return r;
10818: }
10819:
10820: ND *btog(NODE ti,ND **p,int nb,int mod)
10821: {
10822: PGeoBucket *r;
10823: int i,ci;
10824: NODE t,s;
10825: ND m,tp;
10826: ND *pi,*rd;
10827: P c;
10828:
10829: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10830: for ( i = 0; i < nb; i++ )
10831: r[i] = create_pbucket();
10832: for ( t = ti; t; t = NEXT(t) ) {
10833: s = BDY((LIST)BDY(t));
10834: if ( ARG0(s) ) {
10835: m = mdptond((DP)ARG2(s));
1.6 noro 10836: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10837: if ( (ci = ((MQ)c)->cont) != 0 ) {
10838: HCM(m) = ci;
1.6 noro 10839: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10840: for ( i = 0; i < nb; i++ ) {
10841: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10842: add_pbucket(mod,r[i],tp);
10843: }
10844: }
10845: ci = 1;
10846: } else {
10847: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10848: ci = invm(ci,mod);
10849: }
10850: }
10851: rd = (ND *)MALLOC(nb*sizeof(ND));
10852: for ( i = 0; i < nb; i++ )
10853: rd[i] = normalize_pbucket(mod,r[i]);
10854: if ( ci != 1 )
10855: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10856: return rd;
10857: }
10858:
10859: /* YYY */
10860: ND *btog_lf(NODE ti,ND **p,int nb)
10861: {
10862: PGeoBucket *r;
10863: int i;
10864: NODE t,s;
10865: ND m,tp;
10866: ND *pi,*rd;
10867: LM lm;
10868: Z lf,c;
10869:
10870: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10871: for ( i = 0; i < nb; i++ )
10872: r[i] = create_pbucket();
10873: for ( t = ti; t; t = NEXT(t) ) {
10874: s = BDY((LIST)BDY(t));
10875: if ( ARG0(s) ) {
10876: m = mdptond((DP)ARG2(s));
1.6 noro 10877: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10878: if ( lm ) {
10879: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10880: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10881: for ( i = 0; i < nb; i++ ) {
10882: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10883: add_pbucket(-2,r[i],tp);
10884: }
10885: }
10886: c = ONE;
10887: } else {
10888: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10889: }
10890: }
10891: rd = (ND *)MALLOC(nb*sizeof(ND));
10892: for ( i = 0; i < nb; i++ )
10893: rd[i] = normalize_pbucket(-2,r[i]);
10894: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10895: return rd;
10896: }
10897:
10898: ND btog_one(NODE ti,ND *p,int nb,int mod)
10899: {
10900: PGeoBucket r;
10901: int i,ci,j;
10902: NODE t,s;
10903: ND m,tp;
10904: ND pi,rd;
10905: P c;
10906:
10907: r = create_pbucket();
10908: for ( t = ti; t; t = NEXT(t) ) {
10909: s = BDY((LIST)BDY(t));
10910: if ( ARG0(s) ) {
10911: m = mdptond((DP)ARG2(s));
1.6 noro 10912: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10913: if ( (ci = ((MQ)c)->cont) != 0 ) {
10914: HCM(m) = ci;
1.6 noro 10915: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10916: if ( !pi ) {
10917: pi = nd_load_mod(j);
10918: tp = nd_mul_nm(mod,BDY(m),pi);
10919: nd_free(pi);
10920: add_pbucket(mod,r,tp);
10921: } else {
10922: tp = nd_mul_nm(mod,BDY(m),pi);
10923: add_pbucket(mod,r,tp);
10924: }
10925: }
10926: ci = 1;
10927: } else {
10928: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10929: ci = invm(ci,mod);
10930: }
10931: }
10932: rd = normalize_pbucket(mod,r);
10933: free_pbucket(r);
10934: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10935: return rd;
10936: }
10937:
10938: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10939:
10940: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10941: {
10942: int i,j,n,m,nb,pi0,pi1,nvar;
10943: VL fv,tv,vv;
10944: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10945: ND **p;
10946: ND *c;
10947: ND u;
10948: P inv;
10949: MAT mat;
10950:
10951: if ( mod == -2 )
10952: return nd_btog_lf(f,v,ord,tlist,rp);
10953:
10954: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 10955: parse_nd_option(vv,current_option);
1.1 noro 10956: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10957: switch ( ord->id ) {
10958: case 1:
10959: if ( ord->nv != nvar )
10960: error("nd_check : invalid order specification");
10961: break;
10962: default:
10963: break;
10964: }
10965: nd_init_ord(ord);
10966: #if 0
1.6 noro 10967: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10968: #else
10969: nd_bpe = 32;
10970: #endif
10971: nd_setup_parameters(nvar,0);
10972: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10973: intred = BDY((LIST)ARG3(BDY(tlist)));
10974: ind = BDY((LIST)ARG4(BDY(tlist)));
10975: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10976: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10977: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10978: if ( j > i ) i = j;
10979: }
10980: n = i+1;
10981: nb = length(BDY(f));
10982: p = (ND **)MALLOC(n*sizeof(ND *));
10983: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10984: pi = BDY((LIST)BDY(t));
1.6 noro 10985: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10986: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10987: ptomp(mod,(P)ARG2(pi),&inv);
10988: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10989: u = ptond(CO,vv,(P)ONE);
10990: HCM(u) = ((MQ)inv)->cont;
10991: c[pi1] = u;
10992: }
10993: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10994: printf("%d ",i); fflush(stdout);
10995: ti = BDY((LIST)BDY(t));
1.6 noro 10996: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10997: }
10998: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10999: printf("%d ",i); fflush(stdout);
11000: ti = BDY((LIST)BDY(t));
1.6 noro 11001: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 11002: }
11003: m = length(ind);
11004: MKMAT(mat,nb,m);
11005: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 11006: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 11007: BDY(mat)[i][j] = ndtodp(mod,c[i]);
11008: return mat;
11009: }
11010:
11011: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
11012: {
11013: int i,j,n,m,nb,pi0,pi1,nvar;
11014: VL fv,tv,vv;
11015: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
11016: ND **p;
11017: ND *c;
11018: ND u;
11019: MAT mat;
11020: LM lm;
11021: Z lf,inv;
11022:
11023: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 11024: parse_nd_option(vv,current_option);
1.1 noro 11025: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
11026: switch ( ord->id ) {
11027: case 1:
11028: if ( ord->nv != nvar )
11029: error("nd_check : invalid order specification");
11030: break;
11031: default:
11032: break;
11033: }
11034: nd_init_ord(ord);
11035: #if 0
1.6 noro 11036: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 11037: #else
11038: nd_bpe = 32;
11039: #endif
11040: nd_setup_parameters(nvar,0);
11041: permtrace = BDY((LIST)ARG2(BDY(tlist)));
11042: intred = BDY((LIST)ARG3(BDY(tlist)));
11043: ind = BDY((LIST)ARG4(BDY(tlist)));
11044: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
11045: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 11046: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 11047: if ( j > i ) i = j;
11048: }
11049: n = i+1;
11050: nb = length(BDY(f));
11051: p = (ND **)MALLOC(n*sizeof(ND *));
11052: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
11053: pi = BDY((LIST)BDY(t));
1.6 noro 11054: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 11055: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
11056: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
11057: u = ptond(CO,vv,(P)ONE);
11058: HCZ(u) = inv;
11059: c[pi1] = u;
11060: }
11061: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
11062: printf("%d ",i); fflush(stdout);
11063: ti = BDY((LIST)BDY(t));
1.6 noro 11064: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 11065: }
11066: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
11067: printf("%d ",i); fflush(stdout);
11068: ti = BDY((LIST)BDY(t));
1.6 noro 11069: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 11070: }
11071: m = length(ind);
11072: MKMAT(mat,nb,m);
11073: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 11074: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 11075: BDY(mat)[i][j] = ndtodp(-2,c[i]);
11076: return mat;
11077: }
11078:
11079: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
11080: LIST tlist,int pos,MAT *rp)
11081: {
11082: int i,j,n,m,nb,pi0,pi1,nvar;
11083: VL fv,tv,vv;
11084: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
11085: ND *p;
11086: ND *c;
11087: ND u;
11088: P inv;
11089: VECT vect;
11090:
11091: if ( mod == -2 )
11092: error("nd_btog_one : not implemented yet for a large finite field");
11093:
11094: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 11095: parse_nd_option(vv,current_option);
1.1 noro 11096: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
11097: switch ( ord->id ) {
11098: case 1:
11099: if ( ord->nv != nvar )
11100: error("nd_check : invalid order specification");
11101: break;
11102: default:
11103: break;
11104: }
11105: nd_init_ord(ord);
11106: #if 0
1.6 noro 11107: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 11108: #else
11109: nd_bpe = 32;
11110: #endif
11111: nd_setup_parameters(nvar,0);
11112: permtrace = BDY((LIST)ARG2(BDY(tlist)));
11113: intred = BDY((LIST)ARG3(BDY(tlist)));
11114: ind = BDY((LIST)ARG4(BDY(tlist)));
11115: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
11116: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 11117: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 11118: if ( j > i ) i = j;
11119: }
11120: n = i+1;
11121: nb = length(BDY(f));
11122: p = (ND *)MALLOC(n*sizeof(ND *));
11123: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
11124: pi = BDY((LIST)BDY(t));
1.6 noro 11125: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 11126: if ( pi1 == pos ) {
11127: ptomp(mod,(P)ARG2(pi),&inv);
11128: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
11129: u = ptond(CO,vv,(P)ONE);
11130: HCM(u) = ((MQ)inv)->cont;
11131: p[pi0] = u;
11132: }
11133: }
11134: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
11135: printf("%d ",i); fflush(stdout);
11136: ti = BDY((LIST)BDY(t));
1.6 noro 11137: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 11138: if ( Demand ) {
11139: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
11140: }
11141: }
11142: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
11143: printf("%d ",i); fflush(stdout);
11144: ti = BDY((LIST)BDY(t));
1.6 noro 11145: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 11146: if ( Demand ) {
11147: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
11148: }
11149: }
11150: m = length(ind);
11151: MKVECT(vect,m);
11152: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 11153: u = p[ZTOS((Q)BDY(t))];
1.1 noro 11154: if ( !u ) {
1.6 noro 11155: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 11156: BDY(vect)[j] = ndtodp(mod,u);
11157: nd_free(u);
11158: } else
11159: BDY(vect)[j] = ndtodp(mod,u);
11160: }
11161: return vect;
11162: }
11163:
11164: void ndv_print_lf(NDV p)
11165: {
11166: NMV m;
11167: int i,len;
11168:
11169: if ( !p ) printf("0\n");
11170: else {
11171: len = LEN(p);
11172: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
11173: printf("+");
11174: mpz_out_str(asir_out,10,BDY(CZ(m)));
11175: printf("*");
11176: ndl_print(DL(m));
11177: }
11178: printf("\n");
11179: }
11180: }
11181:
11182: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
11183: {
11184: VL tv,fv,vv,vc,av;
11185: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
11186: int m,nocheck,nvar,mindex,e,max;
11187: NDV c;
11188: NMV a;
11189: P p,zp;
11190: Q dmy;
11191: EPOS oepos;
11192: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
11193: Alg alpha,dp;
11194: P poly;
11195: LIST f1,f2,zpl;
11196: Obj obj;
11197: NumberField nf;
11198: struct order_spec *ord1;
11199: struct oEGT eg_check,eg0,eg1;
11200: NODE tr,tl1,tl2,tl3,tl4;
11201: LIST l1,l2,l3,l4,l5;
11202: int *perm;
11203: int j,ret;
11204: NODE retn;
11205: Q jq,bpe;
11206:
11207: nd_module = 0;
11208: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 noro 11209: parse_nd_option(vv,current_option);
1.1 noro 11210: if ( nd_vc )
11211: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
11212: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
11213: switch ( ord->id ) {
11214: case 1:
11215: if ( ord->nv != nvar )
11216: error("nd_f4_lf_trace : invalid order specification");
11217: break;
11218: default:
11219: break;
11220: }
11221:
11222: nd_ntrans = nvar;
11223: nd_nalg = 0;
11224:
11225: nocheck = 0;
11226: mindex = 0;
11227:
11228: /* do not use on-demand load/save */
11229: nd_demand = 0;
11230: m = trace > 1 ? trace : get_lprime(mindex);
11231: nd_init_ord(ord);
11232: mrank = 0;
11233: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
11234: for ( tv = vv; tv; tv = NEXT(tv) ) {
11235: if ( nd_module ) {
11236: s = BDY((LIST)BDY(t));
11237: trank = length(s);
11238: mrank = MAX(mrank,trank);
11239: for ( ; s; s = NEXT(s) ) {
11240: e = getdeg(tv->v,(P)BDY(s));
11241: max = MAX(e,max);
11242: }
11243: } else {
11244: e = getdeg(tv->v,(P)BDY(t));
11245: max = MAX(e,max);
11246: }
11247: }
11248: nd_setup_parameters(nvar,max);
11249: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
11250: ishomo = 1;
11251: /* XXX */
11252: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
11253: if ( nd_module ) {
11254: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
11255: } else {
11256: c = (pointer)ptondv(CO,vv,(P)BDY(t));
11257: }
11258: if ( ishomo )
11259: ishomo = ishomo && ndv_ishomo(c);
11260: if ( c ) {
11261: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
11262: ndv_mod(-2,c);
11263: NEXTNODE(in0,in); BDY(in) = (pointer)c;
11264: }
11265: }
11266: if ( in0 ) NEXT(in) = 0;
11267: if ( fd0 ) NEXT(fd) = 0;
11268: if ( !ishomo && homo ) {
11269: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
11270: c = (NDV)BDY(t); len = LEN(c);
11271: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
11272: wmax = MAX(TD(DL(a)),wmax);
11273: }
11274: homogenize_order(ord,nvar,&ord1);
11275: nd_init_ord(ord1);
11276: nd_setup_parameters(nvar+1,wmax);
11277: for ( t = fd0; t; t = NEXT(t) )
11278: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
11279: }
11280: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 11281: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 11282: if ( ret )
11283: cand = nd_f4_lf_trace_main(m,&perm);
11284: if ( !ret || !cand ) {
11285: *rp = 0; return;
11286: }
11287: if ( !ishomo && homo ) {
11288: /* dehomogenization */
11289: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
11290: nd_init_ord(ord);
11291: nd_setup_parameters(nvar,0);
11292: }
11293: cand = ndv_reducebase(cand,perm);
11294: cand = ndv_reduceall(-2,cand);
11295: cbpe = nd_bpe;
11296: get_eg(&eg0);
11297: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
11298: /* gbcheck : cand is a GB of Id(cand) ? */
11299: retn = nd_f4(-2,0,0);
11300: }
11301: if ( !retn ) {
11302: /* failure */
11303: *rp = 0; return;
11304: }
11305: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
11306: if ( DP_Print )
1.5 noro 11307: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 11308: /* dp->p */
11309: nd_bpe = cbpe;
11310: nd_setup_parameters(nd_nvar,0);
11311: for ( r = cand; r; r = NEXT(r) ) {
11312: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
11313: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
11314: }
11315: MKLIST(*rp,cand);
11316: }
11317:
11318: NODE nd_f4_lf_trace_main(int m,int **indp)
11319: {
11320: int i,nh,stat,index;
11321: NODE r,rm,g;
11322: ND_pairs d,l,l0,t;
11323: ND spol,red;
11324: NDV nf,redv,nfqv,nfv;
11325: NM s0,s;
11326: NODE rp0,srp0,nflist,nflist_lf;
11327: int nsp,nred,col,rank,len,k,j,a;
11328: UINT c;
11329: UINT **spmat;
11330: UINT *s0vect,*svect,*p,*v;
11331: int *colstat;
11332: IndArray *imat;
11333: int *rhead;
11334: int spcol,sprow;
11335: int sugar;
11336: PGeoBucket bucket;
11337: struct oEGT eg0,eg1,eg_f4;
11338:
11339: g = 0; d = 0;
11340: for ( i = 0; i < nd_psn; i++ ) {
11341: d = update_pairs(d,g,i,0);
11342: g = update_base(g,i);
11343: }
11344: while ( d ) {
11345: get_eg(&eg0);
11346: l = nd_minsugarp(d,&d);
11347: sugar = SG(l);
11348: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
11349: bucket = create_pbucket();
11350: stat = nd_sp_f4(m,0,l,bucket);
11351: if ( !stat ) {
11352: for ( t = l; NEXT(t); t = NEXT(t) );
11353: NEXT(t) = d; d = l;
11354: d = nd_reconstruct(1,d);
11355: continue;
11356: }
11357: if ( bucket->m < 0 ) continue;
11358: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
11359: if ( !col ) {
11360: for ( t = l; NEXT(t); t = NEXT(t) );
11361: NEXT(t) = d; d = l;
11362: d = nd_reconstruct(1,d);
11363: continue;
11364: }
11365: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
11366: if ( DP_Print )
1.5 noro 11367: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 11368: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
11369: if ( !l0 ) continue;
11370: l = l0;
11371:
11372: /* over LF */
11373: bucket = create_pbucket();
11374: stat = nd_sp_f4(-2,1,l,bucket);
11375: if ( !stat ) {
11376: for ( t = l; NEXT(t); t = NEXT(t) );
11377: NEXT(t) = d; d = l;
11378: d = nd_reconstruct(1,d);
11379: continue;
11380: }
11381: if ( bucket->m < 0 ) continue;
11382: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
11383: if ( !col ) {
11384: for ( t = l; NEXT(t); t = NEXT(t) );
11385: NEXT(t) = d; d = l;
11386: d = nd_reconstruct(1,d);
11387: continue;
11388: }
11389: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
11390: /* adding new bases */
11391: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
11392: nfv = (NDV)BDY(rm);
11393: nfqv = (NDV)BDY(r);
11394: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
11395: ndv_removecont(m,nfv);
11396: ndv_removecont(-2,nfqv);
1.24 noro 11397: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 11398: d = update_pairs(d,g,nh,0);
11399: g = update_base(g,nh);
11400: }
11401: if ( r || rm ) return 0;
11402: }
11403: conv_ilist(nd_demand,1,g,indp);
11404: return g;
11405: }
11406:
1.7 noro 11407: #if SIZEOF_LONG==8
11408:
11409: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
11410: {
11411: int j,k,len;
11412: UINT *p;
11413: UINT c;
11414: NDV r;
11415: NMV mr0,mr;
11416:
11417: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
11418: if ( !len ) return 0;
11419: else {
11420: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11421: #if 0
11422: ndv_alloc += nmv_adv*len;
11423: #endif
11424: mr = mr0;
11425: p = s0vect;
11426: for ( j = k = 0; j < col; j++, p += nd_wpd )
11427: if ( !rhead[j] ) {
11428: if ( (c = (UINT)vect[k++]) != 0 ) {
11429: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11430: }
11431: }
11432: MKNDV(nd_nvar,mr0,len,r);
11433: return r;
11434: }
11435: }
11436:
1.28 noro 11437: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
11438: {
11439: int j,k,len;
11440: UINT *p;
11441: UINT c;
11442: NDV r;
11443: NMV mr0,mr;
11444:
11445: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
11446: if ( !len ) return 0;
11447: else {
11448: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11449: mr = mr0;
11450: p = s0vect;
11451: for ( j = k = 0; j < col; j++, p += nd_wpd )
11452: if ( (c = (UINT)vect[k++]) != 0 ) {
11453: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11454: }
11455: MKNDV(nd_nvar,mr0,len,r);
11456: return r;
11457: }
11458: }
11459:
1.7 noro 11460: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
11461: {
11462: NM m;
1.11 noro 11463: UINT *t,*s,*u;
11464: int i,st,ed,md,prev,c;
1.7 noro 11465:
11466: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 11467: prev = 0;
11468: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
11469: t = DL(m);
11470: st = prev;
11471: ed = n;
11472: while ( ed > st ) {
11473: md = (st+ed)/2;
11474: u = s0+md*nd_wpd;
11475: c = DL_COMPARE(u,t);
11476: if ( c == 0 ) break;
11477: else if ( c > 0 ) st = md;
11478: else ed = md;
11479: }
11480: r[md] = (mp_limb_t)CM(m);
11481: prev = md;
1.7 noro 11482: }
11483: for ( i = 0; !r[i]; i++ );
11484: return i;
11485: }
11486:
11487: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
11488:
1.28 noro 11489: 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 11490: {
11491: int i,j,k,len,pos,prev;
11492: mp_limb_t a,c,c1,c2;
11493: IndArray ivect;
11494: unsigned char *ivc;
11495: unsigned short *ivs;
11496: unsigned int *ivi;
11497: NDV redv;
11498: NMV mr;
11499: NODE rp;
11500: int maxrs;
11501:
11502: for ( i = 0; i < col; i++ ) cvect[i] = 0;
11503: maxrs = 0;
11504: for ( i = 0; i < nred; i++ ) {
11505: ivect = imat[i];
11506: k = ivect->head;
11507: a = svect[k]; c = cvect[k];
11508: MOD128(a,c,m);
11509: svect[k] = a; cvect[k] = 0;
1.28 noro 11510: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 11511: Nf4_red++;
1.7 noro 11512: maxrs = MAX(maxrs,rp0[i]->sugar);
11513: c = m-c; redv = nd_ps[rp0[i]->index];
11514: len = LEN(redv); mr = BDY(redv);
11515: svect[k] = 0; prev = k;
11516: switch ( ivect->width ) {
11517: case 1:
11518: ivc = ivect->index.c;
11519: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11520: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 11521: c2 = svect[pos]+c1*c;
11522: if ( c2 < svect[pos] ) cvect[pos]++;
11523: svect[pos] = c2;
1.7 noro 11524: }
11525: break;
11526: case 2:
11527: ivs = ivect->index.s;
11528: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11529: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 11530: c2 = svect[pos]+c1*c;
11531: if ( c2 < svect[pos] ) cvect[pos]++;
11532: svect[pos] = c2;
1.7 noro 11533: }
11534: break;
11535: case 4:
11536: ivi = ivect->index.i;
11537: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11538: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 11539: c2 = svect[pos]+c1*c;
11540: if ( c2 < svect[pos] ) cvect[pos]++;
11541: svect[pos] = c2;
1.7 noro 11542: }
11543: break;
11544: }
11545: }
11546: }
11547: for ( i = 0; i < col; i++ ) {
11548: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
11549: }
11550: return maxrs;
11551: }
11552:
11553: /* for Fp, 2^15=<p<2^29 */
11554:
11555: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11556: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
11557: {
11558: int spcol,sprow,a;
11559: int i,j,k,l,rank;
11560: NODE r0,r;
11561: ND_pairs sp;
11562: ND spol;
11563: mp_limb_t **spmat;
11564: mp_limb_t *svect,*cvect;
11565: mp_limb_t *v;
11566: int *colstat;
11567: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11568: int maxrs;
11569: int *spsugar;
11570: ND_pairs *spactive;
11571:
11572: spcol = col-nred;
11573: get_eg(&eg0);
11574: /* elimination (1st step) */
11575: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11576: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11577: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11578: spsugar = (int *)MALLOC(nsp*sizeof(int));
11579: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
11580: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11581: nd_sp(m,0,sp,&spol);
11582: if ( !spol ) continue;
11583: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 11584: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 11585: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11586: if ( i < col ) {
11587: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
11588: for ( j = k = 0; j < col; j++ )
11589: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
11590: spsugar[sprow] = MAX(maxrs,SG(spol));
11591: if ( nz )
11592: spactive[sprow] = sp;
11593: sprow++;
11594: }
11595: nd_free(spol);
11596: }
1.12 noro 11597: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 11598: if ( DP_Print ) {
11599: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11600: fflush(asir_out);
11601: }
11602: /* free index arrays */
11603: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11604:
11605: /* elimination (2nd step) */
11606: colstat = (int *)MALLOC(spcol*sizeof(int));
11607: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
11608: r0 = 0;
11609: for ( i = 0; i < rank; i++ ) {
11610: NEXTNODE(r0,r); BDY(r) =
11611: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
11612: SG((NDV)BDY(r)) = spsugar[i];
11613: GCFREE(spmat[i]);
11614: }
11615: if ( r0 ) NEXT(r) = 0;
11616:
11617: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 11618: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 11619: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11620: if ( DP_Print ) {
11621: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11622: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11623: nsp,nred,sprow,spcol,rank);
11624: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11625: }
11626: if ( nz ) {
11627: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
11628: if ( rank > 0 ) {
11629: NEXT(spactive[rank-1]) = 0;
11630: *nz = spactive[0];
11631: } else
11632: *nz = 0;
11633: }
11634: return r0;
11635: }
11636:
11637: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
11638: {
11639: int i,j,k,l,rank,s;
11640: mp_limb_t inv;
11641: mp_limb_t a;
11642: UINT c;
11643: mp_limb_t *t,*pivot,*pk;
11644: UINT *ck;
11645: UINT **cmat;
11646: UINT *ct;
11647: ND_pairs pair;
11648:
11649: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11650: for ( i = 0; i < row; i++ ) {
11651: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11652: bzero(cmat[i],col*sizeof(UINT));
11653: }
11654:
11655: for ( rank = 0, j = 0; j < col; j++ ) {
11656: for ( i = rank; i < row; i++ ) {
11657: a = mat[i][j]; c = cmat[i][j];
11658: MOD128(a,c,md);
11659: mat[i][j] = a; cmat[i][j] = 0;
11660: }
11661: for ( i = rank; i < row; i++ )
11662: if ( mat[i][j] )
11663: break;
11664: if ( i == row ) {
11665: colstat[j] = 0;
11666: continue;
11667: } else
11668: colstat[j] = 1;
11669: if ( i != rank ) {
11670: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
11671: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
11672: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
11673: if ( spactive ) {
11674: pair = spactive[i]; spactive[i] = spactive[rank];
11675: spactive[rank] = pair;
11676: }
11677: }
11678: /* column j is normalized */
11679: s = sugar[rank];
11680: inv = invm((UINT)mat[rank][j],md);
11681: /* normalize pivot row */
11682: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
11683: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11684: }
11685: for ( i = rank+1; i < row; i++ ) {
11686: if ( (a = mat[i][j]) != 0 ) {
11687: sugar[i] = MAX(sugar[i],s);
11688: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 11689: Nf4_red++;
1.7 noro 11690: }
11691: }
11692: rank++;
11693: }
11694: for ( j = col-1, l = rank-1; j >= 0; j-- )
11695: if ( colstat[j] ) {
11696: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
11697: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
11698: }
11699: s = sugar[l];
11700: for ( i = 0; i < l; i++ ) {
11701: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
11702: if ( a ) {
11703: sugar[i] = MAX(sugar[i],s);
11704: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 11705: Nf4_red++;
1.7 noro 11706: }
11707: }
11708: l--;
11709: }
11710: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11711: GCFREE(cmat);
11712: return rank;
11713: }
11714:
1.28 noro 11715: 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)
11716: {
11717: int i,j,k,l,rank,s,imin;
11718: mp_limb_t inv;
11719: mp_limb_t a;
11720: UINT c;
11721: mp_limb_t *t,*pivot,*pk;
11722: UINT *ck;
11723: UINT **cmat;
11724: UINT *ct;
11725: ND_pairs pair;
11726: SIG sg;
1.31 noro 11727: int *used;
1.28 noro 11728:
1.31 noro 11729: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 11730: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11731: for ( i = 0; i < row; i++ ) {
11732: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11733: bzero(cmat[i],col*sizeof(UINT));
11734: }
11735:
1.31 noro 11736: for ( j = 0; j < col; j++ ) {
11737: for ( i = 0; i < row; i++ ) {
1.28 noro 11738: a = mat[i][j]; c = cmat[i][j];
11739: MOD128(a,c,md);
11740: mat[i][j] = a; cmat[i][j] = 0;
11741: }
1.31 noro 11742: for ( i = 0; i < row; i++ )
11743: if ( !used[i] && mat[i][j] ) break;
11744: if ( i == row ) {
1.28 noro 11745: colstat[j] = 0;
11746: continue;
1.31 noro 11747: } else {
1.28 noro 11748: colstat[j] = 1;
1.31 noro 11749: used[i] = 1;
1.28 noro 11750: }
11751: /* column j is normalized */
1.31 noro 11752: s = sugar[i];
11753: inv = invm((UINT)mat[i][j],md);
1.28 noro 11754: /* normalize pivot row */
1.31 noro 11755: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 11756: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11757: }
1.31 noro 11758: for ( k = i+1; k < row; k++ ) {
11759: if ( (a = mat[k][j]) != 0 ) {
11760: sugar[k] = MAX(sugar[k],s);
11761: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11762: Nf4_red++;
11763: }
11764: }
11765: }
1.31 noro 11766: rank = 0;
11767: for ( i = 0; i < row; i++ ) {
11768: for ( j = 0; j < col; j++ )
11769: if ( mat[i][j] ) break;
11770: if ( j == col ) sugar[i] = -1;
11771: else rank++;
11772: }
1.28 noro 11773: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11774: GCFREE(cmat);
11775: return rank;
11776: }
11777:
11778: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11779: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11780: {
11781: int spcol,sprow,a;
11782: int i,j,k,l,rank;
11783: NODE r0,r;
11784: ND_pairs sp;
11785: ND spol;
11786: mp_limb_t **spmat;
11787: mp_limb_t *svect,*cvect;
11788: mp_limb_t *v;
11789: int *colstat;
11790: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11791: int maxrs;
11792: int *spsugar;
11793: ND_pairs *spactive;
11794: SIG *spsig;
11795:
11796: get_eg(&eg0);
11797: /* elimination (1st step) */
11798: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11799: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11800: spsugar = (int *)MALLOC(nsp*sizeof(int));
11801: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11802: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11803: nd_sp(m,0,sp,&spol);
11804: if ( !spol ) {
1.29 noro 11805: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11806: continue;
11807: }
11808: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11809: nd_to_vect64(m,s0vect,col,spol,svect);
11810: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11811: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11812: if ( i < col ) {
11813: spmat[sprow] = svect;
11814: spsugar[sprow] = MAX(maxrs,SG(spol));
11815: spsig[sprow] = sp->sig;
11816: sprow++;
11817: } else {
1.29 noro 11818: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11819: }
11820: nd_free(spol);
11821: }
11822: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11823: if ( DP_Print ) {
11824: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11825: fflush(asir_out);
11826: }
11827: /* free index arrays */
11828: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11829:
11830: /* elimination (2nd step) */
11831: colstat = (int *)MALLOC(col*sizeof(int));
11832: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11833: r0 = 0;
1.31 noro 11834: for ( i = 0; i < sprow; i++ ) {
11835: if ( spsugar[i] >= 0 ) {
11836: NEXTNODE(r0,r);
11837: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11838: SG((NDV)BDY(r)) = spsugar[i];
11839: ((NDV)BDY(r))->sig = spsig[i];
11840: } else
11841: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11842: GCFREE(spmat[i]);
11843: }
11844: if ( r0 ) NEXT(r) = 0;
11845: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11846: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11847: if ( DP_Print ) {
11848: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11849: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11850: nsp,nred,sprow,col,rank);
11851: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11852: }
11853: return r0;
11854: }
1.40 noro 11855: #endif
1.28 noro 11856:
11857: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11858: {
11859: IndArray *imat;
11860: int nsp,nred,i,start;
11861: int *rhead;
11862: NODE r0,rp;
11863: ND_pairs sp;
11864: NM_ind_pair *rvect;
11865: UINT *s;
11866: int *s0hash;
11867: struct oEGT eg0,eg1,eg_conv;
11868:
11869: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11870: nred = length(rp0);
11871: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11872: rhead = (int *)MALLOC(col*sizeof(int));
11873: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11874:
11875: /* construction of index arrays */
11876: get_eg(&eg0);
11877: if ( DP_Print ) {
11878: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11879: fflush(asir_out);
11880: }
11881: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11882: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11883: rvect[i] = (NM_ind_pair)BDY(rp);
11884: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11885: rhead[imat[i]->head] = 1;
11886: start = imat[i]->head;
11887: }
11888: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11889: if ( DP_Print ) {
11890: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11891: fflush(asir_out);
11892: }
11893: if ( m > 0 )
1.40 noro 11894: #if SIZEOF_LONG==8
1.28 noro 11895: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11896: #else
11897: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11898: #endif
1.28 noro 11899: else
11900: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11901: error("nd_f4_red_q_main_s : not implemented yet");
11902: return r0;
11903: }
11904:
11905: INLINE int ndl_find_reducer_minsig(UINT *dg)
11906: {
11907: RHist r;
11908: int i,singular,ret,d,k,imin;
11909: SIG t;
11910: static int wpd,nvar;
11911: static SIG quo,quomin;
11912: static UINT *tmp;
11913:
11914: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11915: if ( wpd != nd_wpd ) {
11916: wpd = nd_wpd;
11917: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11918: }
11919: #if 0
11920: d = ndl_hash_value(dg);
11921: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11922: if ( ndl_equal(dg,DL(r)) ) {
11923: return r->index;
11924: }
11925: }
11926: #endif
11927: imin = -1;
11928: for ( i = 0; i < nd_psn; i++ ) {
11929: r = nd_psh[i];
11930: if ( ndl_reducible(dg,DL(r)) ) {
11931: ndl_sub(dg,DL(r),tmp);
11932: _ndltodl(tmp,DL(quo));
11933: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11934: quo->pos = nd_psh[i]->sig->pos;
1.55 noro 11935: _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.28 noro 11936: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11937: t = quo; quo = quomin; quomin = t;
11938: imin = i;
11939: }
11940: }
11941: }
11942: if ( imin == -1 ) return nd_psn;
11943: else {
11944: #if 0
11945: nd_append_red(dg,i);
11946: #endif
11947: return imin;
11948: }
11949: }
11950:
11951: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11952: {
11953: NODE rp0,rp;
11954: NM mul,head,s0,s;
11955: int index,col,i,sugar;
11956: RHist h;
11957: UINT *s0v,*p;
11958: NM_ind_pair pair;
11959: ND red;
11960: NDV *ps;
11961: SIG sig;
11962:
11963: s0 = 0; rp0 = 0; col = 0;
11964: if ( nd_demand )
11965: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11966: else
11967: ps = trace?nd_ps_trace:nd_ps;
11968: while ( 1 ) {
11969: head = remove_head_pbucket_symbolic(bucket);
11970: if ( !head ) break;
11971: if ( !s0 ) s0 = head;
11972: else NEXT(s) = head;
11973: s = head;
11974: index = ndl_find_reducer_minsig(DL(head));
11975: if ( index >= 0 && index < nd_psn ) {
11976: h = nd_psh[index];
11977: NEWNM(mul);
11978: ndl_sub(DL(head),DL(h),DL(mul));
11979: if ( ndl_check_bound2(index,DL(mul)) )
11980: return 0;
11981: sugar = TD(DL(mul))+SG(ps[index]);
11982: NEWSIG(sig);
11983: _ndltodl(DL(mul),DL(sig));
11984: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11985: sig->pos = nd_psh[index]->sig->pos;
1.55 noro 11986: _adddl(nd_nvar,DL(sig),nd_sba_hm[sig->pos],DL2(sig));
1.28 noro 11987: MKNM_ind_pair(pair,mul,index,sugar,sig);
11988: red = ndv_mul_nm_symbolic(mul,ps[index]);
11989: add_pbucket_symbolic(bucket,nd_remove_head(red));
11990: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11991: }
11992: col++;
11993: }
11994: if ( rp0 ) NEXT(rp) = 0;
11995: NEXT(s) = 0;
11996: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11997: for ( i = 0, p = s0v, s = s0; i < col;
11998: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11999: *s0vect = s0v;
12000: *r = rp0;
12001:
12002: return col;
12003: }
12004:
12005: NODE nd_sba_f4(int m,int **indp)
12006: {
1.34 noro 12007: int i,nh,stat,index,f4red,f4step;
1.33 noro 12008: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
12009: NODE r,g,rp0,nflist;
1.41 noro 12010: ND_pairs d,l,t,l1;
1.33 noro 12011: ND h,nf;
12012: NDV nfv;
12013: union oNDC hc;
12014: UINT *s0vect;
1.28 noro 12015: UINT c;
12016: PGeoBucket bucket;
1.33 noro 12017: NODE *syzlist;
12018: SIG sig;
1.28 noro 12019: struct oEGT eg0,eg1,eg_f4;
1.33 noro 12020: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 12021:
12022: Nf4_red=0;
1.30 noro 12023: d = 0;
1.29 noro 12024: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 12025: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 12026: d = update_pairs_s(d,i,syzlist);
1.28 noro 12027: }
1.30 noro 12028: nd_nbase = nd_psn;
1.28 noro 12029: f4red = 1;
1.33 noro 12030: psugar = 0;
1.34 noro 12031: f4step = 0;
1.28 noro 12032: while ( d ) {
1.33 noro 12033: for ( t = d, ms = SG(d); t; t = NEXT(t) )
12034: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 12035: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 12036: again:
12037: l = d; d = d->next;
1.41 noro 12038: #if 0
1.33 noro 12039: if ( small_lcm(l) ) {
12040: if ( DP_Print ) fprintf(asir_out,"M");
12041: continue;
12042: }
12043: sig = l->sig;
12044: stat = nd_sp(m,0,l,&h);
1.41 noro 12045: #else
12046: l1 = find_smallest_lcm(l);
12047: if ( l1 == 0 ) {
12048: if ( DP_Print ) fprintf(asir_out,"M");
12049: continue;
12050: }
12051: sig = l1->sig;
12052: stat = nd_sp(m,0,l1,&h);
12053: #endif
1.33 noro 12054: if ( !stat ) {
12055: NEXT(l) = d; d = l;
12056: d = nd_reconstruct(0,d);
12057: goto again;
12058: }
12059: get_eg(&eg1);
12060: #if USE_GEOBUCKET
1.39 noro 12061: 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 12062: #else
1.39 noro 12063: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 12064: #endif
12065: get_eg(&eg2);
12066: if ( !stat ) {
12067: NEXT(l) = d; d = l;
12068: d = nd_reconstruct(0,d);
12069: goto again;
12070: } else if ( stat == -1 ) {
12071: if ( DP_Print ) { printf("S"); fflush(stdout); }
12072: FREENDP(l);
12073: } else if ( nf ) {
12074: if ( DP_Print ) { printf("+"); fflush(stdout); }
12075: add_eg(&eg_nf,&eg1,&eg2);
12076: hc = HCU(nf);
12077: nd_removecont(m,nf);
12078: nfv = ndtondv(m,nf); nd_free(nf);
12079: nh = ndv_newps(m,nfv,0);
12080:
1.30 noro 12081: d = update_pairs_s(d,nh,syzlist);
12082: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 12083: FREENDP(l);
1.28 noro 12084: } else {
1.33 noro 12085: add_eg(&eg_nfzero,&eg1,&eg2);
12086: // syzygy
12087: get_eg(&eg1);
12088: d = remove_spair_s(d,sig);
12089: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 12090: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 12091: if ( DP_Print ) { printf("."); fflush(stdout); }
12092: FREENDP(l);
12093: }
12094: } else {
1.34 noro 12095: if ( ms != psugar ) f4step = 1;
12096: else f4step++;
1.33 noro 12097: again2:
12098: psugar = ms;
12099: l = nd_minsugarp_s(d,&d);
12100: sugar = nd_sugarweight?d->sugar2:SG(d);
12101: bucket = create_pbucket();
12102: stat = nd_sp_f4(m,0,l,bucket);
12103: if ( !stat ) {
12104: for ( t = l; NEXT(t); t = NEXT(t) );
12105: NEXT(t) = d; d = l;
12106: d = nd_reconstruct(0,d);
12107: goto again2;
12108: }
12109: if ( bucket->m < 0 ) continue;
12110: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
12111: if ( !col ) {
1.37 noro 12112: for ( t = l; NEXT(t); t = NEXT(t) )
12113: ;
12114: NEXT(t) = d; d = l;
1.33 noro 12115: d = nd_reconstruct(0,d);
12116: goto again2;
12117: }
1.34 noro 12118: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 12119: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
12120: /* adding new bases */
12121: for ( r = nflist; r; r = NEXT(r) ) {
12122: nfv = (NDV)BDY(r);
1.38 noro 12123: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 12124: ndv_removecont(m,nfv);
12125: nh = ndv_newps(m,nfv,0);
12126: d = update_pairs_s(d,nh,syzlist);
12127: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
12128: }
12129: for ( i = 0; i < nd_nbase; i++ )
12130: for ( r = syzlist[i]; r; r = NEXT(r) )
12131: d = remove_spair_s(d,(SIG)BDY(r));
12132: d = remove_large_lcm(d);
12133: if ( DP_Print ) {
1.34 noro 12134: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 12135: }
12136: f4red++;
1.28 noro 12137: }
12138: }
12139: if ( DP_Print ) {
1.34 noro 12140: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 12141: }
1.30 noro 12142: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 12143: return g;
12144: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>