Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.46
1.46 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.45 2021/01/11 08:37:44 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.45 noro 5: void print_siglist(NODE l);
6:
1.41 noro 7: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs;
1.12 noro 8: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 9:
10: int diag_period = 6;
11: int weight_check = 1;
12: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: /* for general module order */
1.19 noro 14: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 15: int (*dl_base_compare_function)(int nv,DL a,DL b);
16: int nd_base_ordtype;
1.1 noro 17: int nd_dcomp;
18: int nd_rref2;
19: NM _nm_free_list;
20: ND _nd_free_list;
21: ND_pairs _ndp_free_list;
22: NODE nd_hcf;
1.35 noro 23: int Nsyz,Nsamesig;
1.1 noro 24:
25: Obj nd_top_weight;
26:
27: static NODE nd_subst;
28: static VL nd_vc;
29: static int nd_ntrans;
30: static int nd_nalg;
31: #if 0
32: static int ndv_alloc;
33: #endif
34: #if 1
35: static int nd_f4_nsp=0x7fffffff;
36: #else
37: static int nd_f4_nsp=50;
38: #endif
39: static double nd_scale=2;
40: static UINT **nd_bound;
41: static struct order_spec *nd_ord;
42: static EPOS nd_epos;
43: static BlockMask nd_blockmask;
44: static int nd_nvar;
45: static int nd_isrlex;
46: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
47: static UINT nd_mask[32];
48: static UINT nd_mask0,nd_mask1;
49:
50: static NDV *nd_ps;
51: static NDV *nd_ps_trace;
52: static NDV *nd_ps_sym;
53: static NDV *nd_ps_trace_sym;
54: static RHist *nd_psh;
1.30 noro 55: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 56: static RHist *nd_red;
57: static int *nd_work_vector;
58: static int **nd_matrix;
59: static int nd_matrix_len;
60: static struct weight_or_block *nd_worb;
61: static int nd_worb_len;
62: static int nd_found,nd_create,nd_notfirst;
63: static int nmv_adv;
64: static int nd_demand;
1.21 noro 65: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 66: static int nd_module_rank,nd_poly_weight_len;
67: static int *nd_poly_weight,*nd_module_weight;
68: static NODE nd_tracelist;
69: static NODE nd_alltracelist;
1.41 noro 70: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
71: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.46 ! noro 72: static int nd_top,nd_sba_syz,nd_sba_inputisgb;
1.1 noro 73: static int *nd_gbblock;
74: static NODE nd_nzlist,nd_check_splist;
75: static int nd_splist;
76: static int *nd_sugarweight;
77: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 78: static DL *nd_sba_hm;
1.29 noro 79: static NODE *nd_sba_pos;
1.1 noro 80:
1.46 ! noro 81: struct comp_sig_spec {
! 82: int n;
! 83: // current_i <-> oldv[i]
! 84: int *oldv;
! 85: int *weight;
! 86: struct order_pair *order_pair;
! 87: int block_length;
! 88: int **matrix;
! 89: int row;
! 90: int (*cmpdl)(int n,DL d1,DL d2);
! 91: };
! 92:
! 93: struct comp_sig_spec *nd_sba_modord;
! 94:
1.1 noro 95: NumberField get_numberfield();
96: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
97: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
98: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
99: int nd_monic(int m,ND *p);
100: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
101: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
102: NDV pltondv(VL vl,VL dvl,LIST p);
103: void pltozpl(LIST l,Q *cont,LIST *pp);
104: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
105: void nmtodp(int mod,NM m,DP *r);
1.15 noro 106: void ndltodp(UINT *d,DP *r);
1.1 noro 107: NODE reverse_node(NODE n);
108: P ndc_div(int mod,union oNDC a,union oNDC b);
109: P ndctop(int mod,union oNDC c);
110: void finalize_tracelist(int i,P cont);
111: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.46 ! noro 112: void parse_nd_option(VL vl,NODE opt);
1.1 noro 113: void dltondl(int n,DL dl,UINT *r);
114: DP ndvtodp(int mod,NDV p);
115: DP ndtodp(int mod,ND p);
1.16 noro 116: DPM ndvtodpm(int mod,NDV p);
1.43 noro 117: NDV dptondv(int mod,DP p);
1.16 noro 118: NDV dpmtondv(int mod,DPM p);
1.43 noro 119: int dp_getdeg(DP p);
1.16 noro 120: int dpm_getdeg(DPM p,int *rank);
121: void dpm_ptozp(DPM p,Z *cont,DPM *r);
122: int compdmm(int nv,DMM a,DMM b);
1.44 noro 123: DPM sigtodpm(SIG s);
124: SIG dup_sig(SIG sig);
1.1 noro 125:
126: void Pdp_set_weight(NODE,VECT *);
127: void Pox_cmo_rpc(NODE,Obj *);
128:
129: ND nd_add_lf(ND p1,ND p2);
130: void nd_mul_c_lf(ND p,Z mul);
131: void ndv_mul_c_lf(NDV p,Z mul);
132: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
133: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
134: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
135: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
136: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
137: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
138: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40 noro 139: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig);
1.1 noro 140: NODE nd_f4_lf_trace_main(int m,int **indp);
141: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
142:
143: extern int lf_lazy;
144: extern Z current_mod_lf;
145:
146: extern int Denominator,DP_Multiple,MaxDeg;
147:
148: #define BLEN (8*sizeof(unsigned long))
149:
150: typedef struct matrix {
151: int row,col;
152: unsigned long **a;
153: } *matrix;
154:
155:
156: void nd_free_private_storage()
157: {
158: _nm_free_list = 0;
159: _ndp_free_list = 0;
160: #if 0
161: GC_gcollect();
162: #endif
163: }
164:
165: void _NM_alloc()
166: {
167: NM p;
168: int i;
169:
170: for ( i = 0; i < 1024; i++ ) {
171: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
172: p->next = _nm_free_list; _nm_free_list = p;
173: }
174: }
175:
176: matrix alloc_matrix(int row,int col)
177: {
178: unsigned long **a;
179: int i,len,blen;
180: matrix mat;
181:
182: mat = (matrix)MALLOC(sizeof(struct matrix));
183: mat->row = row;
184: mat->col = col;
185: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
186: return mat;
187: }
188:
189:
190: void _ND_alloc()
191: {
192: ND p;
193: int i;
194:
195: for ( i = 0; i < 1024; i++ ) {
196: p = (ND)MALLOC(sizeof(struct oND));
197: p->body = (NM)_nd_free_list; _nd_free_list = p;
198: }
199: }
200:
201: void _NDP_alloc()
202: {
203: ND_pairs p;
204: int i;
205:
206: for ( i = 0; i < 1024; i++ ) {
207: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
208: +(nd_wpd-1)*sizeof(UINT));
209: p->next = _ndp_free_list; _ndp_free_list = p;
210: }
211: }
212:
213: INLINE int nd_length(ND p)
214: {
215: NM m;
216: int i;
217:
218: if ( !p )
219: return 0;
220: else {
221: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
222: return i;
223: }
224: }
225:
226: extern int dp_negative_weight;
227:
228: INLINE int ndl_reducible(UINT *d1,UINT *d2)
229: {
230: UINT u1,u2;
231: int i,j;
232:
233: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
234:
235: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
236: #if USE_UNROLL
237: switch ( nd_bpe ) {
238: case 3:
239: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
240: u1 = d1[i]; u2 = d2[i];
241: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
242: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
243: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
244: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
245: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
246: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
247: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
248: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
249: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
250: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
251: }
252: return 1;
253: break;
254: case 4:
255: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
256: u1 = d1[i]; u2 = d2[i];
257: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
258: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
259: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
260: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
261: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
262: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
263: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
264: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
265: }
266: return 1;
267: break;
268: case 6:
269: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
270: u1 = d1[i]; u2 = d2[i];
271: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
272: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
273: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
274: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
275: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
276: }
277: return 1;
278: break;
279: case 8:
280: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
281: u1 = d1[i]; u2 = d2[i];
282: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
283: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
284: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
285: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
286: }
287: return 1;
288: break;
289: case 16:
290: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
291: u1 = d1[i]; u2 = d2[i];
292: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
293: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
294: }
295: return 1;
296: break;
297: case 32:
298: for ( i = nd_exporigin; i < nd_wpd; i++ )
299: if ( d1[i] < d2[i] ) return 0;
300: return 1;
301: break;
302: default:
303: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
304: u1 = d1[i]; u2 = d2[i];
305: for ( j = 0; j < nd_epw; j++ )
306: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
307: }
308: return 1;
309: }
310: #else
311: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
312: u1 = d1[i]; u2 = d2[i];
313: for ( j = 0; j < nd_epw; j++ )
314: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
315: }
316: return 1;
317: #endif
318: }
319:
320: /*
321: * If the current order is a block order,
322: * then the last block is length 1 and contains
323: * the homo variable. Otherwise, the original
324: * order is either 0 or 2.
325: */
326:
327: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
328: {
329: int w,i,e,n,omask0;
330:
331: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
332: n = nd_nvar-1;
333: ndl_zero(r);
334: for ( i = 0; i < n; i++ ) {
335: e = GET_EXP_OLD(d,i);
336: PUT_EXP(r,i,e);
337: }
338: w = TD(d);
339: PUT_EXP(r,nd_nvar-1,weight-w);
340: if ( nd_module ) MPOS(r) = d[ompos];
341: TD(r) = weight;
342: if ( nd_blockmask ) ndl_weight_mask(r);
343: }
344:
345: void ndl_dehomogenize(UINT *d)
346: {
347: UINT mask;
348: UINT h;
349: int i,bits;
350:
351: if ( nd_blockmask ) {
352: h = GET_EXP(d,nd_nvar-1);
353: XOR_EXP(d,nd_nvar-1,h);
354: TD(d) -= h;
355: ndl_weight_mask(d);
356: } else {
357: if ( nd_isrlex ) {
358: if ( nd_bpe == 32 ) {
359: h = d[nd_exporigin];
360: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
361: d[i-1] = d[i];
362: d[i-1] = 0;
363: TD(d) -= h;
364: } else {
365: bits = nd_epw*nd_bpe;
366: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
367: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
368: for ( i = nd_exporigin; i < nd_wpd; i++ )
369: d[i] = ((d[i]<<nd_bpe)&mask)
370: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
371: TD(d) -= h;
372: }
373: } else {
374: h = GET_EXP(d,nd_nvar-1);
375: XOR_EXP(d,nd_nvar-1,h);
376: TD(d) -= h;
377: }
378: }
379: }
380:
381: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
382: {
383: UINT t1,t2,u,u1,u2;
384: int i,j,l;
385:
386: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
387: error("ndl_lcm : inconsistent monomials");
388: #if USE_UNROLL
389: switch ( nd_bpe ) {
390: case 3:
391: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
392: u1 = d1[i]; u2 = d2[i];
393: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
394: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
395: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
396: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
397: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
398: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
399: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
400: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
401: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
403: d[i] = u;
404: }
405: break;
406: case 4:
407: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
408: u1 = d1[i]; u2 = d2[i];
409: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
410: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
411: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
412: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
414: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
415: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
416: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
417: d[i] = u;
418: }
419: break;
420: case 6:
421: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
422: u1 = d1[i]; u2 = d2[i];
423: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
424: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
425: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
426: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
427: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
428: d[i] = u;
429: }
430: break;
431: case 8:
432: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
433: u1 = d1[i]; u2 = d2[i];
434: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
435: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
436: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
437: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
438: d[i] = u;
439: }
440: break;
441: case 16:
442: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
443: u1 = d1[i]; u2 = d2[i];
444: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
445: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
446: d[i] = u;
447: }
448: break;
449: case 32:
450: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
451: u1 = d1[i]; u2 = d2[i];
452: d[i] = u1>u2?u1:u2;
453: }
454: break;
455: default:
456: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
457: u1 = d1[i]; u2 = d2[i];
458: for ( j = 0, u = 0; j < nd_epw; j++ ) {
459: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
460: }
461: d[i] = u;
462: }
463: break;
464: }
465: #else
466: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
467: u1 = d1[i]; u2 = d2[i];
468: for ( j = 0, u = 0; j < nd_epw; j++ ) {
469: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
470: }
471: d[i] = u;
472: }
473: #endif
474: if ( nd_module ) MPOS(d) = MPOS(d1);
475: TD(d) = ndl_weight(d);
476: if ( nd_blockmask ) ndl_weight_mask(d);
477: }
478:
479: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
480: {
481: UINT t1,t2,u,u1,u2;
482: int i,j,l;
483:
484: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
485: u1 = d1[i]; u2 = d2[i];
486: for ( j = 0, u = 0; j < nd_epw; j++ ) {
487: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
488: }
489: d[i] = u;
490: }
491: }
492:
493: int ndl_weight(UINT *d)
494: {
495: UINT t,u;
496: int i,j;
497:
498: if ( current_dl_weight_vector )
499: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
500: u = GET_EXP(d,i);
501: t += MUL_WEIGHT(u,i);
502: }
503: else
504: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
505: u = d[i];
506: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
507: t += (u&nd_mask0);
508: }
1.20 noro 509: if ( nd_module && nd_module_rank && MPOS(d) )
510: t += nd_module_weight[MPOS(d)-1];
511: for ( i = nd_exporigin; i < nd_wpd; i++ )
512: if ( d[i] && !t )
513: printf("afo\n");
1.1 noro 514: return t;
515: }
516:
517: /* for sugarweight */
518:
519: int ndl_weight2(UINT *d)
520: {
521: int t,u;
522: int i,j;
523:
524: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
525: u = GET_EXP(d,i);
526: t += nd_sugarweight[i]*u;
527: }
1.20 noro 528: if ( nd_module && nd_module_rank && MPOS(d) )
529: t += nd_module_weight[MPOS(d)-1];
1.1 noro 530: return t;
531: }
532:
533: void ndl_weight_mask(UINT *d)
534: {
535: UINT t,u;
536: UINT *mask;
537: int i,j,k,l;
538:
539: l = nd_blockmask->n;
540: for ( k = 0; k < l; k++ ) {
541: mask = nd_blockmask->mask[k];
542: if ( current_dl_weight_vector )
543: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
544: u = GET_EXP_MASK(d,i,mask);
545: t += MUL_WEIGHT(u,i);
546: }
547: else
548: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
549: u = d[i]&mask[i];
550: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
551: t += (u&nd_mask0);
552: }
553: d[k+1] = t;
554: }
555: }
556:
1.21 noro 557: int ndl_glex_compare(UINT *d1,UINT *d2)
558: {
559: if ( TD(d1) > TD(d2) ) return 1;
560: else if ( TD(d1) < TD(d2) ) return -1;
561: else return ndl_lex_compare(d1,d2);
562: }
563:
1.1 noro 564: int ndl_lex_compare(UINT *d1,UINT *d2)
565: {
566: int i;
567:
568: d1 += nd_exporigin;
569: d2 += nd_exporigin;
570: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
571: if ( *d1 > *d2 )
572: return nd_isrlex ? -1 : 1;
573: else if ( *d1 < *d2 )
574: return nd_isrlex ? 1 : -1;
575: return 0;
576: }
577:
578: int ndl_block_compare(UINT *d1,UINT *d2)
579: {
580: int i,l,j,ord_o,ord_l;
581: struct order_pair *op;
582: UINT t1,t2,m;
583: UINT *mask;
584:
585: l = nd_blockmask->n;
586: op = nd_blockmask->order_pair;
587: for ( j = 0; j < l; j++ ) {
588: mask = nd_blockmask->mask[j];
589: ord_o = op[j].order;
590: if ( ord_o < 2 ) {
591: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
592: else if ( t1 < t2 ) return -1;
593: }
594: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
595: m = mask[i];
596: t1 = d1[i]&m;
597: t2 = d2[i]&m;
598: if ( t1 > t2 )
599: return !ord_o ? -1 : 1;
600: else if ( t1 < t2 )
601: return !ord_o ? 1 : -1;
602: }
603: }
604: return 0;
605: }
606:
607: int ndl_matrix_compare(UINT *d1,UINT *d2)
608: {
609: int i,j,s,row;
610: int *v;
611: Z **mat;
612: Z *w;
613: Z t1;
614: Z t,t2;
615:
1.6 noro 616: for ( j = 0; j < nd_nvar; j++ )
617: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 618: if ( nd_top_weight ) {
619: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 620: mat = (Z **)&BDY((VECT)nd_top_weight);
621: row = 1;
1.1 noro 622: } else {
623: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 624: row = ((MAT)nd_top_weight)->row;
1.1 noro 625: }
626: for ( i = 0; i < row; i++ ) {
1.6 noro 627: w = mat[i];
1.1 noro 628: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 629: STOZ(nd_work_vector[j],t1);
1.1 noro 630: mulz(w[j],t1,&t2);
631: addz(t,t2,&t1);
632: t = t1;
633: }
1.6 noro 634: if ( t ) {
635: s = sgnz(t);
1.1 noro 636: if ( s > 0 ) return 1;
637: else if ( s < 0 ) return -1;
1.6 noro 638: }
1.1 noro 639: }
1.6 noro 640: }
641: for ( i = 0; i < nd_matrix_len; i++ ) {
642: v = nd_matrix[i];
643: for ( j = 0, s = 0; j < nd_nvar; j++ )
644: s += v[j]*nd_work_vector[j];
645: if ( s > 0 ) return 1;
646: else if ( s < 0 ) return -1;
647: }
1.1 noro 648: if ( !ndl_equal(d1,d2) )
1.6 noro 649: error("ndl_matrix_compare : invalid matrix");
650: return 0;
1.1 noro 651: }
652:
653: int ndl_composite_compare(UINT *d1,UINT *d2)
654: {
655: int i,j,s,start,end,len,o;
656: int *v;
657: struct sparse_weight *sw;
658:
659: for ( j = 0; j < nd_nvar; j++ )
660: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
661: for ( i = 0; i < nd_worb_len; i++ ) {
662: len = nd_worb[i].length;
663: switch ( nd_worb[i].type ) {
664: case IS_DENSE_WEIGHT:
665: v = nd_worb[i].body.dense_weight;
666: for ( j = 0, s = 0; j < len; j++ )
667: s += v[j]*nd_work_vector[j];
668: if ( s > 0 ) return 1;
669: else if ( s < 0 ) return -1;
670: break;
671: case IS_SPARSE_WEIGHT:
672: sw = nd_worb[i].body.sparse_weight;
673: for ( j = 0, s = 0; j < len; j++ )
674: s += sw[j].value*nd_work_vector[sw[j].pos];
675: if ( s > 0 ) return 1;
676: else if ( s < 0 ) return -1;
677: break;
678: case IS_BLOCK:
679: o = nd_worb[i].body.block.order;
680: start = nd_worb[i].body.block.start;
681: switch ( o ) {
682: case 0:
683: end = start+len;
684: for ( j = start, s = 0; j < end; j++ )
685: s += MUL_WEIGHT(nd_work_vector[j],j);
686: if ( s > 0 ) return 1;
687: else if ( s < 0 ) return -1;
688: for ( j = end-1; j >= start; j-- )
689: if ( nd_work_vector[j] < 0 ) return 1;
690: else if ( nd_work_vector[j] > 0 ) return -1;
691: break;
692: case 1:
693: end = start+len;
694: for ( j = start, s = 0; j < end; j++ )
695: s += MUL_WEIGHT(nd_work_vector[j],j);
696: if ( s > 0 ) return 1;
697: else if ( s < 0 ) return -1;
698: for ( j = start; j < end; j++ )
699: if ( nd_work_vector[j] > 0 ) return 1;
700: else if ( nd_work_vector[j] < 0 ) return -1;
701: break;
702: case 2:
703: end = start+len;
704: for ( j = start; j < end; j++ )
705: if ( nd_work_vector[j] > 0 ) return 1;
706: else if ( nd_work_vector[j] < 0 ) return -1;
707: break;
708: }
709: break;
710: }
711: }
712: return 0;
713: }
714:
715: /* TDH -> WW -> TD-> RL */
716:
717: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
718: {
719: int i,m,e1,e2;
720:
721: if ( TD(d1) > TD(d2) ) return 1;
722: else if ( TD(d1) < TD(d2) ) return -1;
723: m = nd_nvar>>1;
724: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
725: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
726: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
727: }
728: if ( e1 > e2 ) return 1;
729: else if ( e1 < e2 ) return -1;
730: return ndl_lex_compare(d1,d2);
731: }
732:
1.21 noro 733: // common function for module glex and grlex comparison
734: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 735: {
1.21 noro 736: int c;
1.1 noro 737:
1.21 noro 738: switch ( nd_module_ordtype ) {
739: case 0:
740: if ( TD(d1) > TD(d2) ) return 1;
741: else if ( TD(d1) < TD(d2) ) return -1;
742: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
743: else if ( MPOS(d1) < MPOS(d2) ) return 1;
744: else if ( MPOS(d1) > MPOS(d2) ) return -1;
745: else return 0;
746: break;
1.1 noro 747:
1.21 noro 748: case 1:
1.19 noro 749: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
750: if ( TD(d1) > TD(d2) ) return 1;
751: else if ( TD(d1) < TD(d2) ) return -1;
752: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
753: if ( MPOS(d1) < MPOS(d2) ) return 1;
754: else if ( MPOS(d1) > MPOS(d2) ) return -1;
755: }
756: if ( MPOS(d1) < MPOS(d2) ) return 1;
757: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 758: else if ( TD(d1) > TD(d2) ) return 1;
759: else if ( TD(d1) < TD(d2) ) return -1;
760: else return ndl_lex_compare(d1,d2);
761: break;
1.1 noro 762:
1.21 noro 763: case 2: // weight -> POT
764: if ( TD(d1) > TD(d2) ) return 1;
765: else if ( TD(d1) < TD(d2) ) return -1;
766: else if ( MPOS(d1) < MPOS(d2) ) return 1;
767: else if ( MPOS(d1) > MPOS(d2) ) return -1;
768: else return ndl_lex_compare(d1,d2);
769: break;
1.1 noro 770:
1.21 noro 771: default:
772: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 773: return 0;
1.21 noro 774: }
1.1 noro 775: }
776:
1.21 noro 777: // common for module comparison
778: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 779: {
1.21 noro 780: int c;
1.1 noro 781:
1.21 noro 782: switch ( nd_module_ordtype ) {
783: case 0:
1.23 noro 784: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 785: else if ( MPOS(d1) > MPOS(d2) ) return -1;
786: else if ( MPOS(d1) < MPOS(d2) ) return 1;
787: else return 0;
788: break;
1.1 noro 789:
1.21 noro 790: case 1:
791: if ( MPOS(d1) < MPOS(d2) ) return 1;
792: else if ( MPOS(d1) > MPOS(d2) ) return -1;
793: else return (*ndl_base_compare_function)(d1,d2);
794: break;
1.1 noro 795:
1.21 noro 796: case 2: // weight -> POT
797: if ( TD(d1) > TD(d2) ) return 1;
798: else if ( TD(d1) < TD(d2) ) return -1;
799: else if ( MPOS(d1) < MPOS(d2) ) return 1;
800: else if ( MPOS(d1) > MPOS(d2) ) return -1;
801: else return (*ndl_base_compare_function)(d1,d2);
802: break;
1.1 noro 803:
1.21 noro 804: default:
805: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 806: return 0;
1.21 noro 807: }
1.1 noro 808: }
809:
1.21 noro 810: extern DMMstack dmm_stack;
811: void _addtodl(int n,DL d1,DL d2);
1.31 noro 812: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 813: int _eqdl(int n,DL d1,DL d2);
814:
815: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
816: {
1.44 noro 817: int pos1,pos2,t,j,retpot;
1.21 noro 818: DMM *in;
819: DMMstack s;
820: static DL d1=0;
821: static DL d2=0;
822: static int dlen=0;
1.44 noro 823: extern int ReversePOT;
1.21 noro 824:
1.44 noro 825: if ( ReversePOT ) retpot = -1;
826: else retpot = 1;
1.21 noro 827: pos1 = MPOS(m1); pos2 = MPOS(m2);
828: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
829: if ( nd_nvar > dlen ) {
830: NEWDL(d1,nd_nvar);
831: NEWDL(d2,nd_nvar);
832: dlen = nd_nvar;
833: }
834: d1->td = TD(m1);
835: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
836: d2->td = TD(m2);
837: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
838: for ( s = dmm_stack; s; s = NEXT(s) ) {
839: in = s->in;
840: _addtodl(nd_nvar,in[pos1]->dl,d1);
841: _addtodl(nd_nvar,in[pos2]->dl,d2);
842: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44 noro 843: if ( pos1 < pos2 ) return retpot;
844: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 845: else return 0;
846: }
847: pos1 = in[pos1]->pos;
848: pos2 = in[pos2]->pos;
849: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
850: }
851: // comparison by the bottom order
852: LAST:
853: switch ( nd_base_ordtype ) {
854: case 0:
855: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
856: if ( t ) return t;
1.44 noro 857: else if ( pos1 < pos2 ) return retpot;
858: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 859: else return 0;
860: break;
861: case 1:
1.44 noro 862: if ( pos1 < pos2 ) return retpot;
863: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 864: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
865: break;
866: case 2:
867: if ( d1->td > d2->td ) return 1;
868: else if ( d1->td < d2->td ) return -1;
1.44 noro 869: else if ( pos1 < pos2 ) return retpot;
870: else if ( pos1 > pos2 ) return -retpot;
1.21 noro 871: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
872: break;
873: default:
874: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 875: return 0;
1.21 noro 876: }
1.1 noro 877: }
878:
879: INLINE int ndl_equal(UINT *d1,UINT *d2)
880: {
881: int i;
882:
883: switch ( nd_wpd ) {
884: case 2:
885: if ( TD(d2) != TD(d1) ) return 0;
886: if ( d2[1] != d1[1] ) return 0;
887: return 1;
888: break;
889: case 3:
890: if ( TD(d2) != TD(d1) ) return 0;
891: if ( d2[1] != d1[1] ) return 0;
892: if ( d2[2] != d1[2] ) return 0;
893: return 1;
894: break;
895: default:
896: for ( i = 0; i < nd_wpd; i++ )
897: if ( *d1++ != *d2++ ) return 0;
898: return 1;
899: break;
900: }
901: }
902:
903: INLINE void ndl_copy(UINT *d1,UINT *d2)
904: {
905: int i;
906:
907: switch ( nd_wpd ) {
908: case 2:
909: TD(d2) = TD(d1);
910: d2[1] = d1[1];
911: break;
912: case 3:
913: TD(d2) = TD(d1);
914: d2[1] = d1[1];
915: d2[2] = d1[2];
916: break;
917: default:
918: for ( i = 0; i < nd_wpd; i++ )
919: d2[i] = d1[i];
920: break;
921: }
922: }
923:
924: INLINE void ndl_zero(UINT *d)
925: {
926: int i;
927: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
928: }
929:
930: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
931: {
932: int i;
933:
934: if ( nd_module ) {
935: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
936: error("ndl_add : invalid operation");
937: }
938: #if 1
939: switch ( nd_wpd ) {
940: case 2:
941: TD(d) = TD(d1)+TD(d2);
942: d[1] = d1[1]+d2[1];
943: break;
944: case 3:
945: TD(d) = TD(d1)+TD(d2);
946: d[1] = d1[1]+d2[1];
947: d[2] = d1[2]+d2[2];
948: break;
949: default:
950: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
951: break;
952: }
953: #else
954: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
955: #endif
956: }
957:
958: /* d1 += d2 */
959: INLINE void ndl_addto(UINT *d1,UINT *d2)
960: {
961: int i;
962:
963: if ( nd_module ) {
964: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
965: error("ndl_addto : invalid operation");
966: }
967: #if 1
968: switch ( nd_wpd ) {
969: case 2:
970: TD(d1) += TD(d2);
971: d1[1] += d2[1];
972: break;
973: case 3:
974: TD(d1) += TD(d2);
975: d1[1] += d2[1];
976: d1[2] += d2[2];
977: break;
978: default:
979: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
980: break;
981: }
982: #else
983: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
984: #endif
985: }
986:
987: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
988: {
989: int i;
990:
991: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
992: }
993:
994: int ndl_disjoint(UINT *d1,UINT *d2)
995: {
996: UINT t1,t2,u,u1,u2;
997: int i,j;
998:
999: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1000: #if USE_UNROLL
1001: switch ( nd_bpe ) {
1002: case 3:
1003: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1004: u1 = d1[i]; u2 = d2[i];
1005: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
1006: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
1007: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
1008: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
1014: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
1015: }
1016: return 1;
1017: break;
1018: case 4:
1019: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1020: u1 = d1[i]; u2 = d2[i];
1021: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1022: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
1023: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
1024: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
1025: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1026: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1027: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1028: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1029: }
1030: return 1;
1031: break;
1032: case 6:
1033: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1034: u1 = d1[i]; u2 = d2[i];
1035: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1036: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1037: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1038: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1039: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1040: }
1041: return 1;
1042: break;
1043: case 8:
1044: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1045: u1 = d1[i]; u2 = d2[i];
1046: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1047: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1048: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1049: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1050: }
1051: return 1;
1052: break;
1053: case 16:
1054: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1055: u1 = d1[i]; u2 = d2[i];
1056: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1057: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1058: }
1059: return 1;
1060: break;
1061: case 32:
1062: for ( i = nd_exporigin; i < nd_wpd; i++ )
1063: if ( d1[i] && d2[i] ) return 0;
1064: return 1;
1065: break;
1066: default:
1067: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1068: u1 = d1[i]; u2 = d2[i];
1069: for ( j = 0; j < nd_epw; j++ ) {
1070: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1071: u1 >>= nd_bpe; u2 >>= nd_bpe;
1072: }
1073: }
1074: return 1;
1075: break;
1076: }
1077: #else
1078: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1079: u1 = d1[i]; u2 = d2[i];
1080: for ( j = 0; j < nd_epw; j++ ) {
1081: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1082: u1 >>= nd_bpe; u2 >>= nd_bpe;
1083: }
1084: }
1085: return 1;
1086: #endif
1087: }
1088:
1089: int ndl_check_bound(UINT *d1,UINT *d2)
1090: {
1091: UINT u2;
1092: int i,j,ind,k;
1093:
1094: ind = 0;
1095: #if USE_UNROLL
1096: switch ( nd_bpe ) {
1097: case 3:
1098: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1099: u2 = d2[i];
1100: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1101: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1102: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1103: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1104: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1105: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1106: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1107: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1108: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1109: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1110: }
1111: return 0;
1112: break;
1113: case 4:
1114: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1115: u2 = d2[i];
1116: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1117: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1118: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1119: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1120: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1121: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1122: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1123: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1124: }
1125: return 0;
1126: break;
1127: case 6:
1128: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1129: u2 = d2[i];
1130: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1131: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1132: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1133: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1134: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1135: }
1136: return 0;
1137: break;
1138: case 8:
1139: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1140: u2 = d2[i];
1141: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1142: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1143: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1144: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1145: }
1146: return 0;
1147: break;
1148: case 16:
1149: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1150: u2 = d2[i];
1151: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1152: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1153: }
1154: return 0;
1155: break;
1156: case 32:
1157: for ( i = nd_exporigin; i < nd_wpd; i++ )
1158: if ( d1[i]+d2[i]<d1[i] ) return 1;
1159: return 0;
1160: break;
1161: default:
1162: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1163: u2 = d2[i];
1164: k = (nd_epw-1)*nd_bpe;
1165: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1166: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1167: }
1168: return 0;
1169: break;
1170: }
1171: #else
1172: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1173: u2 = d2[i];
1174: k = (nd_epw-1)*nd_bpe;
1175: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1176: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1177: }
1178: return 0;
1179: #endif
1180: }
1181:
1182: int ndl_check_bound2(int index,UINT *d2)
1183: {
1184: return ndl_check_bound(nd_bound[index],d2);
1185: }
1186:
1187: INLINE int ndl_hash_value(UINT *d)
1188: {
1189: int i;
1.11 noro 1190: UINT r;
1.1 noro 1191:
1192: r = 0;
1193: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1194: r = (r*1511+d[i]);
1.11 noro 1195: r %= REDTAB_LEN;
1.1 noro 1196: return r;
1197: }
1198:
1199: INLINE int ndl_find_reducer(UINT *dg)
1200: {
1201: RHist r;
1202: int d,k,i;
1203:
1204: d = ndl_hash_value(dg);
1205: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1206: if ( ndl_equal(dg,DL(r)) ) {
1207: if ( k > 0 ) nd_notfirst++;
1208: nd_found++;
1209: return r->index;
1210: }
1211: }
1212: if ( Reverse )
1213: for ( i = nd_psn-1; i >= 0; i-- ) {
1214: r = nd_psh[i];
1215: if ( ndl_reducible(dg,DL(r)) ) {
1216: nd_create++;
1217: nd_append_red(dg,i);
1218: return i;
1219: }
1220: }
1221: else
1222: for ( i = 0; i < nd_psn; i++ ) {
1223: r = nd_psh[i];
1224: if ( ndl_reducible(dg,DL(r)) ) {
1225: nd_create++;
1226: nd_append_red(dg,i);
1227: return i;
1228: }
1229: }
1230: return -1;
1231: }
1232:
1.41 noro 1233: INLINE int ndl_find_reducer_nonsig(UINT *dg)
1234: {
1235: RHist r;
1236: int i;
1237:
1238: for ( i = 0; i < nd_psn; i++ ) {
1239: r = nd_psh[i];
1240: if ( ndl_reducible(dg,DL(r)) ) return i;
1241: }
1242: return -1;
1243: }
1244:
1.24 noro 1245: // ret=0,...,nd_psn-1 => reducer found
1246: // ret=nd_psn => reducer not found
1247: // ret=-1 => singular top reducible
1248:
1249: int comp_sig(SIG s1,SIG s2);
1250: void _ndltodl(UINT *ndl,DL dl);
1251:
1252: void print_sig(SIG s)
1253: {
1254: int i;
1255:
1256: fprintf(asir_out,"<<");
1257: for ( i = 0; i < nd_nvar; i++ ) {
1258: fprintf(asir_out,"%d",s->dl->d[i]);
1259: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1260: }
1261: fprintf(asir_out,">>*e%d",s->pos);
1262: }
1263:
1.45 noro 1264: void print_siglist(NODE l)
1265: {
1266: for ( ; l; l = NEXT(l) )
1267: print_sig((SIG)l->body);
1268: }
1269:
1270:
1.35 noro 1271: // assuming increasing order wrt signature
1272:
1.24 noro 1273: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1274: {
1275: RHist r;
1.27 noro 1276: int i,singular,ret,d,k;
1.26 noro 1277: static int wpd,nvar;
1.24 noro 1278: static SIG quo;
1279: static UINT *tmp;
1280:
1.26 noro 1281: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1282: if ( wpd != nd_wpd ) {
1.24 noro 1283: wpd = nd_wpd;
1284: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1285: }
1.27 noro 1286: d = ndl_hash_value(dg);
1.35 noro 1287: #if 1
1.27 noro 1288: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1289: if ( ndl_equal(dg,DL(r)) ) {
1290: return r->index;
1291: }
1292: }
1.35 noro 1293: #endif
1.24 noro 1294: singular = 0;
1295: for ( i = 0; i < nd_psn; i++ ) {
1296: r = nd_psh[i];
1297: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1298: ndl_sub(dg,DL(r),tmp);
1.24 noro 1299: _ndltodl(tmp,DL(quo));
1300: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1301: quo->pos = nd_psh[i]->sig->pos;
1302: ret = comp_sig(sig,quo);
1303: if ( ret > 0 ) { singular = 0; break; }
1.38 noro 1304: if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24 noro 1305: }
1306: }
1307: if ( singular ) return -1;
1.27 noro 1308: else if ( i < nd_psn )
1309: nd_append_red(dg,i);
1310: return i;
1.24 noro 1311: }
1312:
1.1 noro 1313: ND nd_merge(ND p1,ND p2)
1314: {
1315: int n,c;
1316: int t,can,td1,td2;
1317: ND r;
1318: NM m1,m2,mr0,mr,s;
1319:
1320: if ( !p1 ) return p2;
1321: else if ( !p2 ) return p1;
1322: else {
1323: can = 0;
1324: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1325: c = DL_COMPARE(DL(m1),DL(m2));
1326: switch ( c ) {
1327: case 0:
1328: s = m1; m1 = NEXT(m1);
1329: can++; NEXTNM2(mr0,mr,s);
1330: s = m2; m2 = NEXT(m2); FREENM(s);
1331: break;
1332: case 1:
1333: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1334: break;
1335: case -1:
1336: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1337: break;
1338: }
1339: }
1340: if ( !mr0 )
1341: if ( m1 ) mr0 = m1;
1342: else if ( m2 ) mr0 = m2;
1343: else return 0;
1344: else if ( m1 ) NEXT(mr) = m1;
1345: else if ( m2 ) NEXT(mr) = m2;
1346: else NEXT(mr) = 0;
1347: BDY(p1) = mr0;
1348: SG(p1) = MAX(SG(p1),SG(p2));
1349: LEN(p1) = LEN(p1)+LEN(p2)-can;
1350: FREEND(p2);
1351: return p1;
1352: }
1353: }
1354:
1355: ND nd_add(int mod,ND p1,ND p2)
1356: {
1357: int n,c;
1358: int t,can,td1,td2;
1359: ND r;
1360: NM m1,m2,mr0,mr,s;
1361:
1.11 noro 1362: Nnd_add++;
1.1 noro 1363: if ( !p1 ) return p2;
1364: else if ( !p2 ) return p1;
1365: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1366: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1367: else if ( !mod ) return nd_add_q(p1,p2);
1368: else {
1369: can = 0;
1370: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1371: c = DL_COMPARE(DL(m1),DL(m2));
1372: switch ( c ) {
1373: case 0:
1374: t = ((CM(m1))+(CM(m2))) - mod;
1375: if ( t < 0 ) t += mod;
1376: s = m1; m1 = NEXT(m1);
1377: if ( t ) {
1378: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1379: } else {
1380: can += 2; FREENM(s);
1381: }
1382: s = m2; m2 = NEXT(m2); FREENM(s);
1383: break;
1384: case 1:
1385: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1386: break;
1387: case -1:
1388: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1389: break;
1390: }
1391: }
1392: if ( !mr0 )
1393: if ( m1 ) mr0 = m1;
1394: else if ( m2 ) mr0 = m2;
1395: else return 0;
1396: else if ( m1 ) NEXT(mr) = m1;
1397: else if ( m2 ) NEXT(mr) = m2;
1398: else NEXT(mr) = 0;
1399: BDY(p1) = mr0;
1400: SG(p1) = MAX(SG(p1),SG(p2));
1401: LEN(p1) = LEN(p1)+LEN(p2)-can;
1402: FREEND(p2);
1403: return p1;
1404: }
1405: }
1406:
1407: /* XXX on opteron, the inlined manipulation of destructive additon of
1408: * two NM seems to make gcc optimizer get confused, so the part is
1409: * done in a function.
1410: */
1411:
1412: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1413: {
1414: NM s;
1415: P t;
1416: int can;
1417:
1418: addp(nd_vc,CP(*m1),CP(*m2),&t);
1419: s = *m1; *m1 = NEXT(*m1);
1420: if ( t ) {
1421: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1422: } else {
1423: can = 2; FREENM(s);
1424: }
1425: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1426: return can;
1427: }
1428:
1429: ND nd_add_q(ND p1,ND p2)
1430: {
1431: int n,c,can;
1432: ND r;
1433: NM m1,m2,mr0,mr,s;
1434: P t;
1435:
1436: if ( !p1 ) return p2;
1437: else if ( !p2 ) return p1;
1438: else {
1439: can = 0;
1440: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1441: c = DL_COMPARE(DL(m1),DL(m2));
1442: switch ( c ) {
1443: case 0:
1444: #if defined(__x86_64__)
1445: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1446: #else
1447: addp(nd_vc,CP(m1),CP(m2),&t);
1448: s = m1; m1 = NEXT(m1);
1449: if ( t ) {
1450: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1451: } else {
1452: can += 2; FREENM(s);
1453: }
1454: s = m2; m2 = NEXT(m2); FREENM(s);
1455: #endif
1456: break;
1457: case 1:
1458: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1459: break;
1460: case -1:
1461: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1462: break;
1463: }
1464: }
1465: if ( !mr0 )
1466: if ( m1 ) mr0 = m1;
1467: else if ( m2 ) mr0 = m2;
1468: else return 0;
1469: else if ( m1 ) NEXT(mr) = m1;
1470: else if ( m2 ) NEXT(mr) = m2;
1471: else NEXT(mr) = 0;
1472: BDY(p1) = mr0;
1473: SG(p1) = MAX(SG(p1),SG(p2));
1474: LEN(p1) = LEN(p1)+LEN(p2)-can;
1475: FREEND(p2);
1476: return p1;
1477: }
1478: }
1479:
1480: ND nd_add_sf(ND p1,ND p2)
1481: {
1482: int n,c,can;
1483: ND r;
1484: NM m1,m2,mr0,mr,s;
1485: int t;
1486:
1487: if ( !p1 ) return p2;
1488: else if ( !p2 ) return p1;
1489: else {
1490: can = 0;
1491: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1492: c = DL_COMPARE(DL(m1),DL(m2));
1493: switch ( c ) {
1494: case 0:
1495: t = _addsf(CM(m1),CM(m2));
1496: s = m1; m1 = NEXT(m1);
1497: if ( t ) {
1498: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1499: } else {
1500: can += 2; FREENM(s);
1501: }
1502: s = m2; m2 = NEXT(m2); FREENM(s);
1503: break;
1504: case 1:
1505: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1506: break;
1507: case -1:
1508: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1509: break;
1510: }
1511: }
1512: if ( !mr0 )
1513: if ( m1 ) mr0 = m1;
1514: else if ( m2 ) mr0 = m2;
1515: else return 0;
1516: else if ( m1 ) NEXT(mr) = m1;
1517: else if ( m2 ) NEXT(mr) = m2;
1518: else NEXT(mr) = 0;
1519: BDY(p1) = mr0;
1520: SG(p1) = MAX(SG(p1),SG(p2));
1521: LEN(p1) = LEN(p1)+LEN(p2)-can;
1522: FREEND(p2);
1523: return p1;
1524: }
1525: }
1526:
1527:
1528: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1529: {
1530: int c,c1,c2;
1531: Z cg,cred,gcd,tq;
1532: P cgp,credp,gcdp;
1533: Obj tr,tr1;
1534:
1535: if ( mod == -1 ) {
1536: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1537: *divp = (Obj)ONE;
1538: } else if ( mod == -2 ) {
1539: Z inv,t;
1540: divlf(ONE,HCZ(p),&inv);
1541: chsgnlf(HCZ(g),&t);
1542: mullf(inv,t,&CZ(mul));
1543: *divp = (Obj)ONE;
1544: } else if ( mod ) {
1545: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1546: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1547: *divp = (Obj)ONE;
1548: } else if ( nd_vc ) {
1549: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1550: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1551: chsgnp(cgp,&CP(mul));
1552: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1553: if ( dn ) {
1554: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1555: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1556: }
1557: *divp = (Obj)credp;
1558: } else {
1.6 noro 1559: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1560: chsgnz(cg,&CZ(mul));
1.1 noro 1561: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1562: if ( dn ) {
1563: mulz(dn->z,cred,&tq); dn->z = tq;
1564: }
1565: *divp = (Obj)cred;
1566: }
1567: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1568: }
1569:
1570: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1571: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1572: {
1573: NM m,mrd,tail;
1574: NM mul;
1575: int n,sugar,psugar,sugar0,stat,index;
1576: int c,c1,c2,dummy;
1577: RHist h;
1578: NDV p,red;
1579: Q cg,cred,gcd,tq,qq;
1580: Z iq;
1581: DP dmul;
1582: NODE node;
1583: LIST hist;
1584: double hmag;
1585: P tp,tp1;
1586: Obj tr,tr1,div;
1587: union oNDC hg;
1588: P cont;
1589:
1590: if ( !g ) {
1591: *rp = d;
1592: return 1;
1593: }
1594: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1595:
1596: sugar0 = sugar = SG(g);
1597: n = NV(g);
1598: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1599: if ( d )
1600: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1601: for ( ; g; ) {
1602: index = ndl_find_reducer(HDL(g));
1603: if ( index >= 0 ) {
1604: h = nd_psh[index];
1605: ndl_sub(HDL(g),DL(h),DL(mul));
1606: if ( ndl_check_bound2(index,DL(mul)) ) {
1607: nd_free(g); nd_free(d);
1608: return 0;
1609: }
1610: p = nd_demand ? ndv_load(index) : ps[index];
1611: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1612: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1613: if ( nd_gentrace ) {
1614: /* Trace=[div,index,mul,ONE] */
1.6 noro 1615: STOZ(index,iq);
1.1 noro 1616: nmtodp(mod,mul,&dmul);
1617: node = mknode(4,div,iq,dmul,ONE);
1618: }
1619: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1620: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1621: hg = HCU(g);
1622: nd_removecont2(d,g);
1.6 noro 1623: if ( nd_gentrace ) {
1.1 noro 1624: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1625: /* exact division */
1.1 noro 1626: cont = ndc_div(mod,hg,HCU(g));
1627: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1628: }
1629: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1630: }
1631: MKLIST(hist,node);
1632: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1633: } else if ( !full ) {
1634: *rp = g;
1635: return 1;
1636: } else {
1637: m = BDY(g);
1638: if ( NEXT(m) ) {
1639: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1640: } else {
1641: FREEND(g); g = 0;
1642: }
1643: if ( d ) {
1644: NEXT(tail)=m; tail=m; LEN(d)++;
1645: } else {
1646: MKND(n,m,1,d); tail = BDY(d);
1647: }
1648: }
1649: }
1650: if ( d ) SG(d) = sugar;
1651: *rp = d;
1652: return 1;
1653: }
1654:
1.24 noro 1655: // ret=1 => success
1656: // ret=0 => overflow
1657: // ret=-1 => singular top reducible
1658:
1659: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1660: {
1661: NM m,mrd,tail;
1662: NM mul;
1663: int n,sugar,psugar,sugar0,stat,index;
1664: int c,c1,c2,dummy;
1665: RHist h;
1666: NDV p,red;
1667: Q cg,cred,gcd,tq,qq;
1668: Z iq;
1669: DP dmul;
1670: NODE node;
1671: LIST hist;
1672: double hmag;
1673: P tp,tp1;
1674: Obj tr,tr1,div;
1675: union oNDC hg;
1676: P cont;
1677: SIG sig;
1678:
1679: if ( !g ) {
1680: *rp = d;
1681: return 1;
1682: }
1683: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1684:
1685: sugar0 = sugar = SG(g);
1686: n = NV(g);
1687: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1688: if ( d )
1689: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1690: sig = g->sig;
1691: for ( ; g; ) {
1692: index = ndl_find_reducer_s(HDL(g),sig);
1693: if ( index >= 0 && index < nd_psn ) {
1694: // reducer found
1695: h = nd_psh[index];
1696: ndl_sub(HDL(g),DL(h),DL(mul));
1697: if ( ndl_check_bound2(index,DL(mul)) ) {
1698: nd_free(g); nd_free(d);
1699: return 0;
1700: }
1701: p = ps[index];
1702: /* d+g -> div*(d+g)+mul*p */
1703: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1704: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1705: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1706: hg = HCU(g);
1707: nd_removecont2(d,g);
1708: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1709: }
1710: } else if ( index == -1 ) {
1711: // singular top reducible
1712: return -1;
1713: } else if ( !full ) {
1714: *rp = g;
1715: g->sig = sig;
1716: return 1;
1717: } else {
1718: m = BDY(g);
1719: if ( NEXT(m) ) {
1720: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1721: } else {
1722: FREEND(g); g = 0;
1723: }
1724: if ( d ) {
1725: NEXT(tail)=m; tail=m; LEN(d)++;
1726: } else {
1727: MKND(n,m,1,d); tail = BDY(d);
1728: }
1729: }
1730: }
1731: if ( d ) {
1732: SG(d) = sugar;
1733: d->sig = sig;
1734: }
1735: *rp = d;
1736: return 1;
1737: }
1738:
1.1 noro 1739: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1740: {
1741: int hindex,index;
1742: NDV p;
1743: ND u,d,red;
1744: NODE l;
1745: NM mul,m,mrd,tail;
1746: int sugar,psugar,n,h_reducible;
1747: PGeoBucket bucket;
1748: int c,c1,c2;
1749: Z cg,cred,gcd,zzz;
1750: RHist h;
1751: double hmag,gmag;
1752: int count = 0;
1753: int hcount = 0;
1754:
1755: if ( !g ) {
1756: *rp = 0;
1757: return 1;
1758: }
1759: sugar = SG(g);
1760: n = NV(g);
1.6 noro 1761: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1762: bucket = create_pbucket();
1763: add_pbucket(mod,bucket,g);
1764: d = 0;
1765: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1766: while ( 1 ) {
1767: if ( mod > 0 || mod == -1 )
1768: hindex = head_pbucket(mod,bucket);
1769: else if ( mod == -2 )
1770: hindex = head_pbucket_lf(bucket);
1771: else
1772: hindex = head_pbucket_q(bucket);
1773: if ( hindex < 0 ) {
1774: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1775: if ( d ) SG(d) = sugar;
1776: *rp = d;
1777: return 1;
1778: }
1779: g = bucket->body[hindex];
1780: index = ndl_find_reducer(HDL(g));
1781: if ( index >= 0 ) {
1782: count++;
1783: if ( !d ) hcount++;
1784: h = nd_psh[index];
1785: ndl_sub(HDL(g),DL(h),DL(mul));
1786: if ( ndl_check_bound2(index,DL(mul)) ) {
1787: nd_free(d);
1788: free_pbucket(bucket);
1789: *rp = 0;
1790: return 0;
1791: }
1792: p = ps[index];
1793: if ( mod == -1 )
1794: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1795: else if ( mod == -2 ) {
1796: Z inv,t;
1797: divlf(ONE,HCZ(p),&inv);
1798: chsgnlf(HCZ(g),&t);
1799: mullf(inv,t,&CZ(mul));
1800: } else if ( mod ) {
1801: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1802: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1803: } else {
1.6 noro 1804: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1805: chsgnz(cg,&CZ(mul));
1.1 noro 1806: nd_mul_c_q(d,(P)cred);
1807: mulq_pbucket(bucket,cred);
1808: g = bucket->body[hindex];
1.6 noro 1809: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1810: }
1811: red = ndv_mul_nm(mod,mul,p);
1812: bucket->body[hindex] = nd_remove_head(g);
1813: red = nd_remove_head(red);
1814: add_pbucket(mod,bucket,red);
1815: psugar = SG(p)+TD(DL(mul));
1816: sugar = MAX(sugar,psugar);
1817: if ( !mod && hmag && (gmag > hmag) ) {
1818: g = normalize_pbucket(mod,bucket);
1819: if ( !g ) {
1820: if ( d ) SG(d) = sugar;
1821: *rp = d;
1822: return 1;
1823: }
1824: nd_removecont2(d,g);
1.6 noro 1825: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1826: add_pbucket(mod,bucket,g);
1827: }
1828: } else if ( !full ) {
1829: g = normalize_pbucket(mod,bucket);
1830: if ( g ) SG(g) = sugar;
1831: *rp = g;
1832: return 1;
1833: } else {
1834: m = BDY(g);
1835: if ( NEXT(m) ) {
1836: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1837: } else {
1838: FREEND(g); g = 0;
1839: }
1840: bucket->body[hindex] = g;
1841: NEXT(m) = 0;
1842: if ( d ) {
1843: NEXT(tail)=m; tail=m; LEN(d)++;
1844: } else {
1845: MKND(n,m,1,d); tail = BDY(d);
1846: }
1847: }
1848: }
1849: }
1850:
1.25 noro 1851: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1852: {
1853: int hindex,index;
1854: NDV p;
1855: ND u,d,red;
1856: NODE l;
1857: NM mul,m,mrd,tail;
1858: int sugar,psugar,n,h_reducible;
1859: PGeoBucket bucket;
1860: int c,c1,c2;
1861: Z cg,cred,gcd,zzz;
1862: RHist h;
1863: double hmag,gmag;
1864: int count = 0;
1865: int hcount = 0;
1866: SIG sig;
1867:
1868: if ( !g ) {
1869: *rp = 0;
1870: return 1;
1871: }
1872: sugar = SG(g);
1873: n = NV(g);
1874: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1875: bucket = create_pbucket();
1876: add_pbucket(mod,bucket,g);
1877: d = 0;
1878: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1879: sig = g->sig;
1880: while ( 1 ) {
1881: if ( mod > 0 || mod == -1 )
1882: hindex = head_pbucket(mod,bucket);
1883: else if ( mod == -2 )
1884: hindex = head_pbucket_lf(bucket);
1885: else
1886: hindex = head_pbucket_q(bucket);
1887: if ( hindex < 0 ) {
1888: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1889: if ( d ) {
1890: SG(d) = sugar;
1891: d->sig = sig;
1892: }
1893: *rp = d;
1894: return 1;
1895: }
1896: g = bucket->body[hindex];
1897: index = ndl_find_reducer_s(HDL(g),sig);
1898: if ( index >= 0 && index < nd_psn ) {
1899: count++;
1900: if ( !d ) hcount++;
1901: h = nd_psh[index];
1902: ndl_sub(HDL(g),DL(h),DL(mul));
1903: if ( ndl_check_bound2(index,DL(mul)) ) {
1904: nd_free(d);
1905: free_pbucket(bucket);
1906: *rp = 0;
1907: return 0;
1908: }
1909: p = ps[index];
1910: if ( mod == -1 )
1911: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1912: else if ( mod == -2 ) {
1913: Z inv,t;
1914: divlf(ONE,HCZ(p),&inv);
1915: chsgnlf(HCZ(g),&t);
1916: mullf(inv,t,&CZ(mul));
1917: } else if ( mod ) {
1918: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1919: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1920: } else {
1921: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1922: chsgnz(cg,&CZ(mul));
1923: nd_mul_c_q(d,(P)cred);
1924: mulq_pbucket(bucket,cred);
1925: g = bucket->body[hindex];
1926: gmag = (double)p_mag((P)HCZ(g));
1927: }
1928: red = ndv_mul_nm(mod,mul,p);
1929: bucket->body[hindex] = nd_remove_head(g);
1930: red = nd_remove_head(red);
1931: add_pbucket(mod,bucket,red);
1932: psugar = SG(p)+TD(DL(mul));
1933: sugar = MAX(sugar,psugar);
1934: if ( !mod && hmag && (gmag > hmag) ) {
1935: g = normalize_pbucket(mod,bucket);
1936: if ( !g ) {
1937: if ( d ) {
1938: SG(d) = sugar;
1939: d->sig = sig;
1940: }
1941: *rp = d;
1942: return 1;
1943: }
1944: nd_removecont2(d,g);
1945: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1946: add_pbucket(mod,bucket,g);
1947: }
1948: } else if ( index == -1 ) {
1949: // singular top reducible
1950: return -1;
1951: } else if ( !full ) {
1952: g = normalize_pbucket(mod,bucket);
1953: if ( g ) {
1954: SG(g) = sugar;
1955: g->sig = sig;
1956: }
1957: *rp = g;
1958: return 1;
1959: } else {
1960: m = BDY(g);
1961: if ( NEXT(m) ) {
1962: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1963: } else {
1964: FREEND(g); g = 0;
1965: }
1966: bucket->body[hindex] = g;
1967: NEXT(m) = 0;
1968: if ( d ) {
1969: NEXT(tail)=m; tail=m; LEN(d)++;
1970: } else {
1971: MKND(n,m,1,d); tail = BDY(d);
1972: }
1973: }
1974: }
1975: }
1976:
1.1 noro 1977: /* input : list of NDV, cand : list of NDV */
1978:
1979: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1980: {
1981: int n,i,stat;
1982: ND nf,d;
1983: NDV r;
1984: NODE t,s;
1985: union oNDC dn;
1986: Z q;
1987: LIST list;
1988:
1.24 noro 1989: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1990: n = length(cand);
1991:
1992: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1993: /* membercheck : list is a subset of Id(cand) ? */
1994: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1995: again:
1996: nd_tracelist = 0;
1997: if ( nd_bpe > obpe )
1998: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1999: else
2000: r = (NDV)BDY(t);
2001: #if 0
2002: // moved to nd_f4_lf_trace()
2003: if ( m == -2 ) ndv_mod(m,r);
2004: #endif
2005: d = ndvtond(m,r);
1.6 noro 2006: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 2007: if ( !stat ) {
2008: nd_reconstruct(0,0);
2009: goto again;
2010: } else if ( nf ) return 0;
2011: if ( nd_gentrace ) {
2012: nd_tracelist = reverse_node(nd_tracelist);
2013: MKLIST(list,nd_tracelist);
1.6 noro 2014: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 2015: MKNODE(s,list,nd_alltracelist);
2016: nd_alltracelist = s; nd_tracelist = 0;
2017: }
2018: if ( DP_Print ) { printf("."); fflush(stdout); }
2019: }
2020: if ( DP_Print ) { printf("\n"); }
2021: return 1;
2022: }
2023:
2024: ND nd_remove_head(ND p)
2025: {
2026: NM m;
2027:
2028: m = BDY(p);
2029: if ( !NEXT(m) ) {
2030: FREEND(p); p = 0;
2031: } else {
2032: BDY(p) = NEXT(m); LEN(p)--;
2033: }
2034: FREENM(m);
2035: return p;
2036: }
2037:
2038: ND nd_separate_head(ND p,ND *head)
2039: {
2040: NM m,m0;
2041: ND r;
2042:
2043: m = BDY(p);
2044: if ( !NEXT(m) ) {
2045: *head = p; p = 0;
2046: } else {
2047: m0 = m;
2048: BDY(p) = NEXT(m); LEN(p)--;
2049: NEXT(m0) = 0;
2050: MKND(NV(p),m0,1,r);
2051: *head = r;
2052: }
2053: return p;
2054: }
2055:
2056: PGeoBucket create_pbucket()
2057: {
2058: PGeoBucket g;
2059:
2060: g = CALLOC(1,sizeof(struct oPGeoBucket));
2061: g->m = -1;
2062: return g;
2063: }
2064:
2065: void free_pbucket(PGeoBucket b) {
2066: int i;
2067:
2068: for ( i = 0; i <= b->m; i++ )
2069: if ( b->body[i] ) {
2070: nd_free(b->body[i]);
2071: b->body[i] = 0;
2072: }
2073: GCFREE(b);
2074: }
2075:
1.39 noro 2076: #if 0
1.1 noro 2077: void add_pbucket_symbolic(PGeoBucket g,ND d)
2078: {
2079: int l,i,k,m;
2080:
2081: if ( !d )
2082: return;
2083: l = LEN(d);
2084: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2085: /* 2^(k-1) < l <= 2^k (=m) */
2086: d = nd_merge(g->body[k],d);
2087: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2088: g->body[k] = 0;
2089: d = nd_merge(g->body[k+1],d);
2090: }
2091: g->body[k] = d;
2092: g->m = MAX(g->m,k);
2093: }
1.39 noro 2094: #else
2095: void add_pbucket_symbolic(PGeoBucket g,ND d)
2096: {
2097: int l,i,k,m,m0;
2098:
2099: if ( !d )
2100: return;
2101: m0 = g->m;
2102: while ( 1 ) {
2103: l = LEN(d);
2104: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2105: /* 2^(k-1) < l <= 2^k (=m) */
2106: if ( g->body[k] == 0 ) {
2107: g->body[k] = d;
2108: m0 = MAX(k,m0);
2109: break;
2110: } else {
2111: d = nd_merge(g->body[k],d);
2112: g->body[k] = 0;
2113: }
2114: }
2115: g->m = m0;
2116: }
2117: #endif
1.1 noro 2118:
1.39 noro 2119: #if 0
1.1 noro 2120: void add_pbucket(int mod,PGeoBucket g,ND d)
2121: {
2122: int l,i,k,m;
2123:
2124: if ( !d )
2125: return;
2126: l = LEN(d);
2127: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2128: /* 2^(k-1) < l <= 2^k (=m) */
2129: d = nd_add(mod,g->body[k],d);
2130: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2131: g->body[k] = 0;
2132: d = nd_add(mod,g->body[k+1],d);
2133: }
2134: g->body[k] = d;
2135: g->m = MAX(g->m,k);
2136: }
1.39 noro 2137: #else
2138: void add_pbucket(int mod,PGeoBucket g,ND d)
2139: {
2140: int l,i,k,m,m0;
2141:
2142: m0 = g->m;
2143: while ( d != 0 ) {
2144: l = LEN(d);
2145: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2146: /* 2^(k-1) < l <= 2^k (=m) */
2147: if ( g->body[k] == 0 ) {
2148: g->body[k] = d;
2149: m0 = MAX(k,m0);
2150: break;
2151: } else {
2152: d = nd_add(mod,g->body[k],d);
2153: g->body[k] = 0;
2154: }
2155: }
2156: g->m = m0;
2157: }
2158: #endif
1.1 noro 2159:
2160: void mulq_pbucket(PGeoBucket g,Z c)
2161: {
2162: int k;
2163:
2164: for ( k = 0; k <= g->m; k++ )
2165: nd_mul_c_q(g->body[k],(P)c);
2166: }
2167:
2168: NM remove_head_pbucket_symbolic(PGeoBucket g)
2169: {
2170: int j,i,k,c;
2171: NM head;
2172:
2173: k = g->m;
2174: j = -1;
2175: for ( i = 0; i <= k; i++ ) {
2176: if ( !g->body[i] ) continue;
2177: if ( j < 0 ) j = i;
2178: else {
2179: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2180: if ( c > 0 )
2181: j = i;
2182: else if ( c == 0 )
2183: g->body[i] = nd_remove_head(g->body[i]);
2184: }
2185: }
2186: if ( j < 0 ) return 0;
2187: else {
2188: head = BDY(g->body[j]);
2189: if ( !NEXT(head) ) {
2190: FREEND(g->body[j]);
2191: g->body[j] = 0;
2192: } else {
2193: BDY(g->body[j]) = NEXT(head);
2194: LEN(g->body[j])--;
2195: }
2196: return head;
2197: }
2198: }
2199:
2200: int head_pbucket(int mod,PGeoBucket g)
2201: {
2202: int j,i,c,k,nv,sum;
2203: UINT *di,*dj;
2204: ND gi,gj;
2205:
2206: k = g->m;
2207: while ( 1 ) {
2208: j = -1;
2209: for ( i = 0; i <= k; i++ ) {
2210: if ( !(gi = g->body[i]) )
2211: continue;
2212: if ( j < 0 ) {
2213: j = i;
2214: gj = g->body[j];
2215: dj = HDL(gj);
2216: sum = HCM(gj);
2217: } else {
2218: c = DL_COMPARE(HDL(gi),dj);
2219: if ( c > 0 ) {
2220: if ( sum ) HCM(gj) = sum;
2221: else g->body[j] = nd_remove_head(gj);
2222: j = i;
2223: gj = g->body[j];
2224: dj = HDL(gj);
2225: sum = HCM(gj);
2226: } else if ( c == 0 ) {
2227: if ( mod == -1 )
2228: sum = _addsf(sum,HCM(gi));
2229: else {
2230: sum = sum+HCM(gi)-mod;
2231: if ( sum < 0 ) sum += mod;
2232: }
2233: g->body[i] = nd_remove_head(gi);
2234: }
2235: }
2236: }
2237: if ( j < 0 ) return -1;
2238: else if ( sum ) {
2239: HCM(gj) = sum;
2240: return j;
2241: } else
2242: g->body[j] = nd_remove_head(gj);
2243: }
2244: }
2245:
2246: int head_pbucket_q(PGeoBucket g)
2247: {
2248: int j,i,c,k,nv;
2249: Z sum,t;
2250: ND gi,gj;
2251:
2252: k = g->m;
2253: while ( 1 ) {
2254: j = -1;
2255: for ( i = 0; i <= k; i++ ) {
2256: if ( !(gi = g->body[i]) ) continue;
2257: if ( j < 0 ) {
2258: j = i;
2259: gj = g->body[j];
1.6 noro 2260: sum = HCZ(gj);
1.1 noro 2261: } else {
2262: nv = NV(gi);
2263: c = DL_COMPARE(HDL(gi),HDL(gj));
2264: if ( c > 0 ) {
1.6 noro 2265: if ( sum ) HCZ(gj) = sum;
1.1 noro 2266: else g->body[j] = nd_remove_head(gj);
2267: j = i;
2268: gj = g->body[j];
1.6 noro 2269: sum = HCZ(gj);
1.1 noro 2270: } else if ( c == 0 ) {
1.6 noro 2271: addz(sum,HCZ(gi),&t);
1.1 noro 2272: sum = t;
2273: g->body[i] = nd_remove_head(gi);
2274: }
2275: }
2276: }
2277: if ( j < 0 ) return -1;
2278: else if ( sum ) {
1.6 noro 2279: HCZ(gj) = sum;
1.1 noro 2280: return j;
2281: } else
2282: g->body[j] = nd_remove_head(gj);
2283: }
2284: }
2285:
2286: int head_pbucket_lf(PGeoBucket g)
2287: {
2288: int j,i,c,k,nv;
2289: Z sum,t;
2290: ND gi,gj;
2291:
2292: k = g->m;
2293: while ( 1 ) {
2294: j = -1;
2295: for ( i = 0; i <= k; i++ ) {
2296: if ( !(gi = g->body[i]) ) continue;
2297: if ( j < 0 ) {
2298: j = i;
2299: gj = g->body[j];
2300: sum = HCZ(gj);
2301: } else {
2302: nv = NV(gi);
2303: c = DL_COMPARE(HDL(gi),HDL(gj));
2304: if ( c > 0 ) {
2305: if ( sum ) HCZ(gj) = sum;
2306: else g->body[j] = nd_remove_head(gj);
2307: j = i;
2308: gj = g->body[j];
2309: sum = HCZ(gj);
2310: } else if ( c == 0 ) {
2311: addlf(sum,HCZ(gi),&t);
2312: sum = t;
2313: g->body[i] = nd_remove_head(gi);
2314: }
2315: }
2316: }
2317: if ( j < 0 ) return -1;
2318: else if ( sum ) {
2319: HCZ(gj) = sum;
2320: return j;
2321: } else
2322: g->body[j] = nd_remove_head(gj);
2323: }
2324: }
2325:
2326: ND normalize_pbucket(int mod,PGeoBucket g)
2327: {
2328: int i;
2329: ND r,t;
2330:
2331: r = 0;
2332: for ( i = 0; i <= g->m; i++ ) {
2333: r = nd_add(mod,r,g->body[i]);
2334: g->body[i] = 0;
2335: }
2336: g->m = -1;
2337: return r;
2338: }
2339:
2340: #if 0
2341: void register_hcf(NDV p)
2342: {
2343: DCP dc,t;
2344: P hc,h;
2345: int c;
2346: NODE l,l1,prev;
2347:
2348: hc = p->body->c.p;
2349: if ( !nd_vc || NUM(hc) ) return;
2350: fctrp(nd_vc,hc,&dc);
2351: for ( t = dc; t; t = NEXT(t) ) {
2352: h = t->c;
2353: if ( NUM(h) ) continue;
2354: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2355: c = compp(nd_vc,h,(P)BDY(l));
2356: if ( c >= 0 ) break;
2357: }
2358: if ( !l || c > 0 ) {
2359: MKNODE(l1,h,l);
2360: if ( !prev )
2361: nd_hcf = l1;
2362: else
2363: NEXT(prev) = l1;
2364: }
2365: }
2366: }
2367: #else
2368: void register_hcf(NDV p)
2369: {
2370: DCP dc,t;
2371: P hc,h,q;
2372: Q dmy;
2373: int c;
2374: NODE l,l1,prev;
2375:
2376: hc = p->body->c.p;
2377: if ( NUM(hc) ) return;
2378: ptozp(hc,1,&dmy,&h);
2379: #if 1
2380: for ( l = nd_hcf; l; l = NEXT(l) ) {
2381: while ( 1 ) {
2382: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2383: else break;
2384: }
2385: }
2386: if ( NUM(h) ) return;
2387: #endif
2388: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2389: c = compp(nd_vc,h,(P)BDY(l));
2390: if ( c >= 0 ) break;
2391: }
2392: if ( !l || c > 0 ) {
2393: MKNODE(l1,h,l);
2394: if ( !prev )
2395: nd_hcf = l1;
2396: else
2397: NEXT(prev) = l1;
2398: }
2399: }
2400: #endif
2401:
2402: int do_diagonalize(int sugar,int m)
2403: {
1.6 noro 2404: int i,nh,stat;
2405: NODE r,g,t;
2406: ND h,nf,s,head;
2407: NDV nfv;
2408: Q q;
2409: P nm,nmp,dn,mnp,dnp,cont,cont1;
2410: union oNDC hc;
2411: NODE node;
2412: LIST l;
2413: Z iq;
1.1 noro 2414:
1.6 noro 2415: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2416: if ( nd_gentrace ) {
2417: /* Trace = [1,index,1,1] */
2418: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2419: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2420: }
2421: if ( nd_demand )
2422: nfv = ndv_load(i);
2423: else
2424: nfv = nd_ps[i];
2425: s = ndvtond(m,nfv);
2426: s = nd_separate_head(s,&head);
2427: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2428: if ( !stat ) return 0;
2429: ndv_free(nfv);
2430: hc = HCU(nf); nd_removecont(m,nf);
2431: /* exact division */
2432: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2433: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2434: nfv = ndtondv(m,nf);
2435: nd_free(nf);
2436: nd_bound[i] = ndv_compute_bound(nfv);
2437: if ( !m ) register_hcf(nfv);
2438: if ( nd_demand ) {
2439: ndv_save(nfv,i);
2440: ndv_free(nfv);
2441: } else
2442: nd_ps[i] = nfv;
2443: }
2444: return 1;
1.1 noro 2445: }
2446:
2447: LIST compute_splist()
2448: {
2449: NODE g,tn0,tn,node;
2450: LIST l0;
2451: ND_pairs d,t;
2452: int i;
2453: Z i1,i2;
2454:
2455: g = 0; d = 0;
2456: for ( i = 0; i < nd_psn; i++ ) {
2457: d = update_pairs(d,g,i,0);
2458: g = update_base(g,i);
2459: }
2460: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2461: NEXTNODE(tn0,tn);
1.6 noro 2462: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2463: node = mknode(2,i1,i2); MKLIST(l0,node);
2464: BDY(tn) = l0;
2465: }
2466: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2467: return l0;
2468: }
2469:
2470: /* return value = 0 => input is not a GB */
2471:
2472: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2473: {
1.6 noro 2474: int i,nh,sugar,stat;
2475: NODE r,g,t;
2476: ND_pairs d;
2477: ND_pairs l;
2478: ND h,nf,s,head,nf1;
2479: NDV nfv;
2480: Z q;
2481: union oNDC dn,hc;
2482: int diag_count = 0;
1.41 noro 2483: int Nnfnz = 0,Nnfz = 0;
1.6 noro 2484: P cont;
2485: LIST list;
1.27 noro 2486: struct oEGT eg1,eg2,eg_update;
1.6 noro 2487:
1.27 noro 2488: init_eg(&eg_update);
1.11 noro 2489: Nnd_add = 0;
1.6 noro 2490: g = 0; d = 0;
2491: for ( i = 0; i < nd_psn; i++ ) {
2492: d = update_pairs(d,g,i,gensyz);
2493: g = update_base(g,i);
2494: }
2495: sugar = 0;
2496: while ( d ) {
1.1 noro 2497: again:
1.6 noro 2498: l = nd_minp(d,&d);
2499: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2500: if ( SG(l) != sugar ) {
2501: if ( ishomo ) {
2502: diag_count = 0;
2503: stat = do_diagonalize(sugar,m);
1.1 noro 2504: if ( !stat ) {
1.6 noro 2505: NEXT(l) = d; d = l;
2506: d = nd_reconstruct(0,d);
2507: goto again;
1.1 noro 2508: }
1.6 noro 2509: }
2510: sugar = SG(l);
2511: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2512: }
2513: stat = nd_sp(m,0,l,&h);
2514: if ( !stat ) {
2515: NEXT(l) = d; d = l;
2516: d = nd_reconstruct(0,d);
2517: goto again;
2518: }
1.1 noro 2519: #if USE_GEOBUCKET
1.39 noro 2520: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
2521: :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2522: #else
1.39 noro 2523: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 2524: #endif
1.6 noro 2525: if ( !stat ) {
2526: NEXT(l) = d; d = l;
2527: d = nd_reconstruct(0,d);
2528: goto again;
2529: } else if ( nf ) {
1.41 noro 2530: Nnfnz++;
1.6 noro 2531: if ( checkonly || gensyz ) return 0;
1.1 noro 2532: if ( nd_newelim ) {
2533: if ( nd_module ) {
2534: if ( MPOS(HDL(nf)) > 1 ) return 0;
2535: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2536: }
1.6 noro 2537: if ( DP_Print ) { printf("+"); fflush(stdout); }
2538: hc = HCU(nf);
2539: nd_removecont(m,nf);
2540: if ( !m && nd_nalg ) {
2541: nd_monic(0,&nf);
2542: nd_removecont(m,nf);
2543: }
2544: if ( nd_gentrace ) {
2545: /* exact division */
1.1 noro 2546: cont = ndc_div(m,hc,HCU(nf));
2547: if ( m || !UNIQ(cont) ) {
1.6 noro 2548: t = mknode(4,NULLP,NULLP,NULLP,cont);
2549: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2550: nd_tracelist = t;
2551: }
2552: }
1.6 noro 2553: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2554: nh = ndv_newps(m,nfv,0);
1.6 noro 2555: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2556: diag_count = 0;
2557: stat = do_diagonalize(sugar,m);
2558: if ( !stat ) {
2559: NEXT(l) = d; d = l;
2560: d = nd_reconstruct(1,d);
2561: goto again;
1.1 noro 2562: }
1.6 noro 2563: }
1.27 noro 2564: get_eg(&eg1);
1.6 noro 2565: d = update_pairs(d,g,nh,0);
1.27 noro 2566: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2567: g = update_base(g,nh);
2568: FREENDP(l);
1.41 noro 2569: } else {
2570: Nnfz++;
2571: if ( nd_gentrace && gensyz ) {
2572: nd_tracelist = reverse_node(nd_tracelist);
2573: MKLIST(list,nd_tracelist);
2574: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2575: MKNODE(t,list,nd_alltracelist);
2576: nd_alltracelist = t; nd_tracelist = 0;
2577: }
2578: if ( DP_Print ) { printf("."); fflush(stdout); }
2579: FREENDP(l);
2580: }
1.36 noro 2581: }
2582: conv_ilist(nd_demand,0,g,indp);
1.41 noro 2583: if ( !checkonly && DP_Print ) {
2584: printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
2585: printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
2586: fflush(asir_out);
2587: }
2588: if ( DP_Print ) {
2589: print_eg("update",&eg_update); fprintf(asir_out,"\n");
2590: }
1.36 noro 2591: return g;
1.1 noro 2592: }
2593:
1.30 noro 2594: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
2595: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24 noro 2596:
2597: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2598: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2599:
2600: void _copydl(int n,DL d1,DL d2);
2601: void _subfromdl(int n,DL d1,DL d2);
2602: extern int (*cmpdl)(int n,DL d1,DL d2);
2603:
2604: NODE insert_sig(NODE l,SIG s)
2605: {
2606: int pos;
2607: DL sig;
2608: struct oNODE root;
2609: NODE p,prev,r;
2610: SIG t;
2611:
2612: pos = s->pos; sig = DL(s);
2613: root.next = l; prev = &root;
2614: for ( p = l; p; p = p->next ) {
2615: t = (SIG)p->body;
2616: if ( t->pos == pos ) {
2617: if ( _dl_redble(DL(t),sig,nd_nvar) )
2618: return root.next;
2619: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2620: // remove p
2621: prev->next = p->next;
1.45 noro 2622: else
2623: prev = p;
1.24 noro 2624: } else
2625: prev = p;
2626: }
1.27 noro 2627: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2628: for ( p = &root; p->next; p = p->next );
2629: p->next = r;
2630: // r->next = root.next;
2631: // return r;
2632: return root.next;
1.24 noro 2633: }
2634:
2635: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2636: {
2637: struct oND_pairs root;
2638: ND_pairs prev,p;
2639: SIG spsig;
2640:
2641: root.next = d;
2642: prev = &root; p = d;
2643: while ( p ) {
2644: spsig = p->sig;
1.26 noro 2645: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2646: // remove p
2647: prev->next = p->next;
1.26 noro 2648: Nsyz++;
2649: } else
1.24 noro 2650: prev = p;
2651: p = p->next;
2652: }
2653: return (ND_pairs)root.next;
2654: }
2655:
1.29 noro 2656: int _dl_redble_ext(DL,DL,DL,int);
2657:
1.28 noro 2658: int small_lcm(ND_pairs l)
2659: {
2660: SIG sig;
2661: int i;
1.29 noro 2662: NODE t;
1.28 noro 2663: static DL lcm,mul,quo;
1.44 noro 2664: static int nvar = 0;
1.28 noro 2665:
1.38 noro 2666: if ( nd_sba_largelcm ) return 0;
1.28 noro 2667: if ( nvar < nd_nvar ) {
2668: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2669: }
2670: sig = l->sig;
2671: _ndltodl(l->lcm,lcm);
1.29 noro 2672: #if 0
1.28 noro 2673: for ( i = 0; i < nd_psn; i++ ) {
2674: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2675: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2676: _ndltodl(DL(nd_psh[i]),mul);
2677: _addtodl(nd_nvar,quo,mul);
2678: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2679: break;
2680: }
2681: }
2682: if ( i < nd_psn ) return 1;
2683: else return 0;
1.29 noro 2684: #else
2685: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2686: i = (long)BDY(t);
2687: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2688: _ndltodl(DL(nd_psh[i]),mul);
2689: _addtodl(nd_nvar,quo,mul);
2690: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2691: break;
2692: }
2693: }
2694: if ( t ) return 1;
2695: else return 0;
2696: #endif
1.28 noro 2697: }
2698:
1.41 noro 2699: ND_pairs find_smallest_lcm(ND_pairs l)
2700: {
2701: SIG sig;
2702: int i,minindex;
2703: NODE t;
2704: ND_pairs r;
2705: struct oSIG sig1;
2706: static DL mul,quo,minlm;
1.44 noro 2707: static int nvar = 0;
1.41 noro 2708:
2709: if ( nvar < nd_nvar ) {
2710: nvar = nd_nvar;
2711: NEWDL(quo,nvar); NEWDL(mul,nvar);
2712: NEWDL(minlm,nvar);
2713: }
2714: sig = l->sig;
2715: // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
2716: _ndltodl(l->lcm,minlm); minindex = -1;
2717: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2718: i = (long)BDY(t);
2719: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2720: _ndltodl(DL(nd_psh[i]),mul);
2721: _addtodl(nd_nvar,quo,mul);
2722: if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
2723: minindex = i;
2724: _copydl(nd_nvar,mul,minlm);
2725: }
2726: }
2727: }
2728: // l->lcm is minimal; return l itself
2729: if ( minindex < 0 ) return l;
2730: for ( i = 0; i < nd_psn; i++ ) {
2731: if ( i == minindex ) continue;
2732: _ndltodl(DL(nd_psh[i]),mul);
2733: if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
2734: _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
2735: sig1.pos = nd_ps[i]->sig->pos;
2736: sig1.dl = quo;
2737: if ( comp_sig(sig,&sig1) > 0 ) {
2738: // printf("X");
2739: NEWND_pairs(r);
2740: r->sig = sig;
2741: r->i1 = minindex;
2742: r->i2 = i;
2743: dltondl(nd_nvar,minlm,r->lcm);
2744: r->next = 0;
2745: return r;
2746: }
2747: }
2748: }
2749: // there is no suitable spair
2750: return 0;
2751: }
2752:
1.28 noro 2753: ND_pairs remove_large_lcm(ND_pairs d)
2754: {
2755: struct oND_pairs root;
2756: ND_pairs prev,p;
2757:
2758: root.next = d;
2759: prev = &root; p = d;
2760: while ( p ) {
1.41 noro 2761: #if 0
1.28 noro 2762: if ( small_lcm(p) ) {
2763: // remove p
2764: prev->next = p->next;
2765: } else
1.41 noro 2766: #else
2767: if ( find_smallest_lcm(p) == 0 ) {
2768: // remove p
2769: prev->next = p->next;
2770: } else
2771: #endif
1.28 noro 2772: prev = p;
2773: p = p->next;
2774: }
2775: return (ND_pairs)root.next;
2776: }
2777:
1.26 noro 2778: struct oEGT eg_create,eg_newpairs,eg_merge;
2779:
1.30 noro 2780: NODE conv_ilist_s(int demand,int trace,int **indp);
2781:
1.44 noro 2782: // S(fj*ei-fi*ej)
2783:
1.45 noro 2784: void _subdl(int,DL,DL,DL);
2785:
1.44 noro 2786: SIG trivial_sig(int i,int j)
2787: {
1.45 noro 2788: static DL lcm;
1.44 noro 2789: static struct oSIG sigi,sigj;
2790: static int nvar = 0;
2791: SIG sig;
2792:
2793: if ( nvar != nd_nvar ) {
1.45 noro 2794: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
2795: }
1.46 ! noro 2796: if ( nd_sba_inputisgb != 0 ) {
1.45 noro 2797: lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
2798: sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
2799: sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
2800: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2801: else sig = dup_sig(&sigj);
2802: } else {
2803: sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
2804: sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
2805: if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
2806: else sig = dup_sig(&sigj);
1.44 noro 2807: }
2808: return sig;
2809: }
2810:
2811: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24 noro 2812: {
1.39 noro 2813: int i,j,nh,sugar,stat,pos;
1.30 noro 2814: NODE r,t,g;
1.24 noro 2815: ND_pairs d;
1.41 noro 2816: ND_pairs l,l1;
1.24 noro 2817: ND h,nf,s,head,nf1;
2818: NDV nfv;
2819: Z q;
2820: union oNDC dn,hc;
2821: P cont;
2822: LIST list;
2823: SIG sig;
1.29 noro 2824: NODE *syzlist;
1.44 noro 2825: int ngen;
1.41 noro 2826: int Nnominimal,Nredundant;
1.28 noro 2827: DL lcm,quo,mul;
1.31 noro 2828: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.41 noro 2829: int Nnfs=0,Nnfz=0,Nnfnz=0;
1.24 noro 2830:
1.27 noro 2831: init_eg(&eg_remove);
1.29 noro 2832: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26 noro 2833: Nsyz = 0;
1.24 noro 2834: Nnd_add = 0;
1.41 noro 2835: Nnominimal = 0;
1.26 noro 2836: Nredundant = 0;
1.30 noro 2837: d = 0;
1.44 noro 2838: ngen = nd_psn;
1.29 noro 2839: for ( i = 0; i < nd_psn; i++ )
2840: for ( j = i+1; j < nd_psn; j++ ) {
1.44 noro 2841: sig = trivial_sig(i,j);
1.29 noro 2842: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
2843: }
1.41 noro 2844: for ( i = 0; i < nd_psn; i++ ) {
2845: d = update_pairs_s(d,i,syzlist);
2846: }
1.24 noro 2847: sugar = 0;
1.39 noro 2848: pos = 0;
1.26 noro 2849: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2850: init_eg(&eg_create);
2851: init_eg(&eg_merge);
1.29 noro 2852: init_eg(&eg_large);
1.31 noro 2853: init_eg(&eg_nf);
2854: init_eg(&eg_nfzero);
1.24 noro 2855: while ( d ) {
2856: again:
1.26 noro 2857: if ( DP_Print ) {
2858: int len;
2859: ND_pairs td;
1.28 noro 2860: for ( td = d, len=0; td; td = td->next, len++)
2861: ;
1.26 noro 2862: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2863: }
1.24 noro 2864: l = d; d = d->next;
1.41 noro 2865: #if 0
1.28 noro 2866: if ( small_lcm(l) ) {
1.24 noro 2867: if ( DP_Print ) fprintf(asir_out,"M");
1.41 noro 2868: Nnominimal++;
1.24 noro 2869: continue;
2870: }
2871: if ( SG(l) != sugar ) {
2872: sugar = SG(l);
2873: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2874: }
1.28 noro 2875: sig = l->sig;
1.39 noro 2876: if ( DP_Print && nd_sba_pot ) {
2877: if ( sig->pos != pos ) {
2878: fprintf(asir_out,"[%d]",sig->pos);
2879: pos = sig->pos;
2880: }
2881: }
1.24 noro 2882: stat = nd_sp(m,0,l,&h);
1.41 noro 2883: #else
1.46 ! noro 2884: // if ( l->sig->dl->td == 0 )
! 2885: // if ( DP_Print ) print_sig(l->sig);
1.41 noro 2886: l1 = find_smallest_lcm(l);
2887: if ( l1 == 0 ) {
2888: if ( DP_Print ) fprintf(asir_out,"M");
2889: Nnominimal++;
2890: continue;
2891: }
2892: if ( SG(l1) != sugar ) {
2893: sugar = SG(l1);
2894: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2895: }
2896: sig = l1->sig;
2897: if ( DP_Print && nd_sba_pot ) {
2898: if ( sig->pos != pos ) {
2899: fprintf(asir_out,"[%d]",sig->pos);
2900: pos = sig->pos;
2901: }
2902: }
2903: stat = nd_sp(m,0,l1,&h);
2904: #endif
1.24 noro 2905: if ( !stat ) {
2906: NEXT(l) = d; d = l;
2907: d = nd_reconstruct(0,d);
2908: goto again;
2909: }
1.31 noro 2910: get_eg(&eg1);
1.25 noro 2911: #if USE_GEOBUCKET
1.39 noro 2912: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2913: #else
1.39 noro 2914: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24 noro 2915: #endif
1.31 noro 2916: get_eg(&eg2);
1.24 noro 2917: if ( !stat ) {
2918: NEXT(l) = d; d = l;
2919: d = nd_reconstruct(0,d);
2920: goto again;
2921: } else if ( stat == -1 ) {
1.41 noro 2922: Nnfs++;
1.24 noro 2923: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 noro 2924: FREENDP(l);
1.24 noro 2925: } else if ( nf ) {
1.41 noro 2926: Nnfnz++;
2927: if ( DP_Print ) {
2928: if ( nd_sba_redundant_check ) {
2929: if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
2930: Nredundant++;
2931: printf("R");
2932: } else
2933: printf("+");
2934: } else
2935: printf("+");
2936: fflush(stdout);
2937: }
1.31 noro 2938: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 2939: hc = HCU(nf);
2940: nd_removecont(m,nf);
2941: nfv = ndtondv(m,nf); nd_free(nf);
2942: nh = ndv_newps(m,nfv,0);
1.26 noro 2943:
1.30 noro 2944: d = update_pairs_s(d,nh,syzlist);
1.29 noro 2945: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2946: FREENDP(l);
2947: } else {
1.41 noro 2948: Nnfz++;
1.31 noro 2949: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 2950: // syzygy
1.27 noro 2951: get_eg(&eg1);
1.24 noro 2952: d = remove_spair_s(d,sig);
1.27 noro 2953: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2954: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2955: if ( DP_Print ) { printf("."); fflush(stdout); }
2956: FREENDP(l);
2957: }
2958: }
1.30 noro 2959: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2960: if ( DP_Print ) {
1.41 noro 2961: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",Nnd_add,Nsyz,Nsamesig,Nnominimal);
2962: printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
2963: fflush(stdout);
2964: if ( nd_sba_redundant_check )
2965: printf("Nredundant=%d\n",Nredundant);
1.26 noro 2966: fflush(stdout);
2967: print_eg("create",&eg_create);
2968: print_eg("merge",&eg_merge);
1.27 noro 2969: print_eg("remove",&eg_remove);
1.31 noro 2970: print_eg("nf",&eg_nf);
2971: print_eg("nfzero",&eg_nfzero);
1.27 noro 2972: printf("\n");
1.26 noro 2973: }
1.44 noro 2974: if ( nd_sba_syz ) {
2975: NODE hsyz,tsyz,prev;
2976:
2977: hsyz = 0;
2978: for ( i = 0; i < ngen; i++ ) {
2979: tsyz = syzlist[i];
2980: for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
2981: BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
2982: if ( prev != 0 ) {
2983: prev->next = hsyz; hsyz = syzlist[i];
2984: }
2985: }
2986: *syzp = hsyz;
2987: } else *syzp = 0;
1.24 noro 2988: return g;
2989: }
2990:
1.1 noro 2991: /* splist = [[i1,i2],...] */
2992:
2993: int check_splist(int m,NODE splist)
2994: {
2995: NODE t,p;
2996: ND_pairs d,r,l;
2997: int stat;
2998: ND h,nf;
2999:
3000: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3001: p = BDY((LIST)BDY(t));
1.6 noro 3002: NEXTND_pairs(d,r);
3003: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3004: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3005: SG(r) = TD(LCM(r)); /* XXX */
3006: }
3007: if ( d ) NEXT(r) = 0;
3008:
1.6 noro 3009: while ( d ) {
1.1 noro 3010: again:
1.6 noro 3011: l = nd_minp(d,&d);
3012: stat = nd_sp(m,0,l,&h);
3013: if ( !stat ) {
3014: NEXT(l) = d; d = l;
3015: d = nd_reconstruct(0,d);
3016: goto again;
3017: }
1.39 noro 3018: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6 noro 3019: if ( !stat ) {
3020: NEXT(l) = d; d = l;
3021: d = nd_reconstruct(0,d);
3022: goto again;
3023: } else if ( nf ) return 0;
1.1 noro 3024: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 3025: }
1.1 noro 3026: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
3027: return 1;
3028: }
3029:
3030: int check_splist_f4(int m,NODE splist)
3031: {
3032: UINT *s0vect;
1.6 noro 3033: PGeoBucket bucket;
1.1 noro 3034: NODE p,rp0,t;
3035: ND_pairs d,r,l,ll;
3036: int col,stat;
3037:
3038: for ( d = 0, t = splist; t; t = NEXT(t) ) {
3039: p = BDY((LIST)BDY(t));
1.6 noro 3040: NEXTND_pairs(d,r);
3041: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
3042: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 3043: SG(r) = TD(LCM(r)); /* XXX */
3044: }
3045: if ( d ) NEXT(r) = 0;
3046:
1.6 noro 3047: while ( d ) {
3048: l = nd_minsugarp(d,&d);
3049: bucket = create_pbucket();
3050: stat = nd_sp_f4(m,0,l,bucket);
3051: if ( !stat ) {
3052: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3053: NEXT(ll) = d; d = l;
3054: d = nd_reconstruct(0,d);
3055: continue;
3056: }
3057: if ( bucket->m < 0 ) continue;
3058: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
3059: if ( !col ) {
3060: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
3061: NEXT(ll) = d; d = l;
3062: d = nd_reconstruct(0,d);
3063: continue;
1.1 noro 3064: }
1.6 noro 3065: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
3066: }
3067: return 1;
1.1 noro 3068: }
3069:
3070: int do_diagonalize_trace(int sugar,int m)
3071: {
1.6 noro 3072: int i,nh,stat;
3073: NODE r,g,t;
3074: ND h,nf,nfq,s,head;
3075: NDV nfv,nfqv;
3076: Q q,den,num;
3077: union oNDC hc;
3078: NODE node;
3079: LIST l;
3080: Z iq;
3081: P cont,cont1;
1.1 noro 3082:
1.6 noro 3083: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
3084: if ( nd_gentrace ) {
3085: /* Trace = [1,index,1,1] */
3086: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
3087: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3088: }
3089: /* for nd_ps */
3090: s = ndvtond(m,nd_ps[i]);
3091: s = nd_separate_head(s,&head);
3092: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
3093: if ( !stat ) return 0;
3094: nf = nd_add(m,head,nf);
3095: ndv_free(nd_ps[i]);
3096: nd_ps[i] = ndtondv(m,nf);
3097: nd_free(nf);
3098:
3099: /* for nd_ps_trace */
3100: if ( nd_demand )
3101: nfv = ndv_load(i);
3102: else
3103: nfv = nd_ps_trace[i];
3104: s = ndvtond(0,nfv);
3105: s = nd_separate_head(s,&head);
3106: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
3107: if ( !stat ) return 0;
3108: ndv_free(nfv);
3109: hc = HCU(nf); nd_removecont(0,nf);
3110: /* exact division */
1.1 noro 3111: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 3112: if ( nd_gentrace ) finalize_tracelist(i,cont);
3113: nfv = ndtondv(0,nf);
3114: nd_free(nf);
3115: nd_bound[i] = ndv_compute_bound(nfv);
3116: register_hcf(nfv);
3117: if ( nd_demand ) {
3118: ndv_save(nfv,i);
3119: ndv_free(nfv);
3120: } else
3121: nd_ps_trace[i] = nfv;
3122: }
3123: return 1;
1.1 noro 3124: }
3125:
3126: static struct oEGT eg_invdalg;
3127: struct oEGT eg_le;
3128:
3129: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
3130: {
3131: NODE tn;
3132: P p1;
3133:
3134: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
3135: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
3136: }
3137: *r = p;
3138: }
3139:
3140: NODE nd_gb_trace(int m,int ishomo,int **indp)
3141: {
1.6 noro 3142: int i,nh,sugar,stat;
3143: NODE r,g,t;
3144: ND_pairs d;
3145: ND_pairs l;
3146: ND h,nf,nfq,s,head;
3147: NDV nfv,nfqv;
3148: Z q,den,num;
3149: P hc;
3150: union oNDC dn,hnfq;
3151: struct oEGT eg_monic,egm0,egm1;
3152: int diag_count = 0;
3153: P cont;
3154: LIST list;
3155:
3156: init_eg(&eg_monic);
3157: init_eg(&eg_invdalg);
3158: init_eg(&eg_le);
3159: g = 0; d = 0;
3160: for ( i = 0; i < nd_psn; i++ ) {
3161: d = update_pairs(d,g,i,0);
3162: g = update_base(g,i);
3163: }
3164: sugar = 0;
3165: while ( d ) {
1.1 noro 3166: again:
1.6 noro 3167: l = nd_minp(d,&d);
3168: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
3169: if ( SG(l) != sugar ) {
1.1 noro 3170: #if 1
1.6 noro 3171: if ( ishomo ) {
3172: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3173: stat = do_diagonalize_trace(sugar,m);
3174: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3175: diag_count = 0;
1.1 noro 3176: if ( !stat ) {
1.6 noro 3177: NEXT(l) = d; d = l;
3178: d = nd_reconstruct(1,d);
3179: goto again;
1.1 noro 3180: }
1.6 noro 3181: }
3182: #endif
3183: sugar = SG(l);
3184: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
3185: }
3186: stat = nd_sp(m,0,l,&h);
3187: if ( !stat ) {
3188: NEXT(l) = d; d = l;
3189: d = nd_reconstruct(1,d);
3190: goto again;
3191: }
1.1 noro 3192: #if USE_GEOBUCKET
1.39 noro 3193: stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3194: #else
1.39 noro 3195: stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1 noro 3196: #endif
1.6 noro 3197: if ( !stat ) {
3198: NEXT(l) = d; d = l;
3199: d = nd_reconstruct(1,d);
3200: goto again;
3201: } else if ( nf ) {
3202: if ( nd_demand ) {
3203: nfqv = ndv_load(nd_psn);
3204: nfq = ndvtond(0,nfqv);
3205: } else
3206: nfq = 0;
3207: if ( !nfq ) {
1.39 noro 3208: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6 noro 3209: NEXT(l) = d; d = l;
3210: d = nd_reconstruct(1,d);
3211: goto again;
3212: }
3213: }
3214: if ( nfq ) {
3215: /* m|HC(nfq) => failure */
3216: if ( nd_vc ) {
3217: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
3218: } else
3219: q = HCZ(nfq);
3220: if ( !remqi((Q)q,m) ) return 0;
3221:
3222: if ( DP_Print ) { printf("+"); fflush(stdout); }
3223: hnfq = HCU(nfq);
3224: if ( nd_nalg ) {
3225: /* m|DN(HC(nf)^(-1)) => failure */
3226: get_eg(&egm0);
3227: if ( !nd_monic(m,&nfq) ) return 0;
3228: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
3229: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3230: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
3231: } else {
3232: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
3233: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
3234: }
3235: if ( nd_gentrace ) {
3236: /* exact division */
3237: cont = ndc_div(0,hnfq,HCU(nfqv));
3238: if ( !UNIQ(cont) ) {
3239: t = mknode(4,NULLP,NULLP,NULLP,cont);
3240: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
3241: nd_tracelist = t;
3242: }
3243: }
1.24 noro 3244: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 3245: if ( ishomo && ++diag_count == diag_period ) {
3246: diag_count = 0;
3247: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3248: stat = do_diagonalize_trace(sugar,m);
3249: if ( DP_Print > 2 ) fprintf(asir_out,"|");
3250: if ( !stat ) {
1.1 noro 3251: NEXT(l) = d; d = l;
3252: d = nd_reconstruct(1,d);
3253: goto again;
1.6 noro 3254: }
1.1 noro 3255: }
1.6 noro 3256: d = update_pairs(d,g,nh,0);
3257: g = update_base(g,nh);
3258: } else {
3259: if ( DP_Print ) { printf("*"); fflush(stdout); }
3260: }
3261: } else {
3262: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3263: }
1.6 noro 3264: FREENDP(l);
3265: }
3266: if ( nd_nalg ) {
3267: if ( DP_Print ) {
3268: print_eg("monic",&eg_monic);
3269: print_eg("invdalg",&eg_invdalg);
3270: print_eg("le",&eg_le);
1.1 noro 3271: }
1.6 noro 3272: }
1.1 noro 3273: conv_ilist(nd_demand,1,g,indp);
1.41 noro 3274: if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6 noro 3275: return g;
1.1 noro 3276: }
3277:
3278: int ndv_compare(NDV *p1,NDV *p2)
3279: {
3280: return DL_COMPARE(HDL(*p1),HDL(*p2));
3281: }
3282:
3283: int ndv_compare_rev(NDV *p1,NDV *p2)
3284: {
3285: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3286: }
3287:
3288: int ndvi_compare(NDVI p1,NDVI p2)
3289: {
3290: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3291: }
3292:
3293: int ndvi_compare_rev(NDVI p1,NDVI p2)
3294: {
3295: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3296: }
3297:
3298: NODE ndv_reduceall(int m,NODE f)
3299: {
3300: int i,j,n,stat;
3301: ND nf,g,head;
3302: NODE t,a0,a;
3303: union oNDC dn;
3304: Q q,num,den;
3305: NODE node;
3306: LIST l;
3307: Z iq,jq;
3308: int *perm;
3309: union oNDC hc;
3310: P cont,cont1;
3311:
3312: if ( nd_nora ) return f;
3313: n = length(f);
1.24 noro 3314: ndv_setup(m,0,f,0,1,0);
1.1 noro 3315: perm = (int *)MALLOC(n*sizeof(int));
3316: if ( nd_gentrace ) {
3317: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3318: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3319: }
3320: for ( i = 0; i < n; ) {
3321: if ( nd_gentrace ) {
3322: /* Trace = [1,index,1,1] */
1.6 noro 3323: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3324: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3325: }
3326: g = ndvtond(m,nd_ps[i]);
3327: g = nd_separate_head(g,&head);
1.6 noro 3328: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3329: if ( !stat )
3330: nd_reconstruct(0,0);
3331: else {
3332: if ( DP_Print ) { printf("."); fflush(stdout); }
3333: ndv_free(nd_ps[i]);
3334: hc = HCU(nf); nd_removecont(m,nf);
3335: if ( nd_gentrace ) {
3336: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3337: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3338: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3339: }
1.6 noro 3340: /* exact division */
1.1 noro 3341: cont = ndc_div(m,hc,HCU(nf));
3342: finalize_tracelist(perm[i],cont);
3343: }
3344: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3345: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3346: i++;
3347: }
3348: }
3349: if ( DP_Print ) { printf("\n"); }
3350: for ( a0 = 0, i = 0; i < n; i++ ) {
3351: NEXTNODE(a0,a);
3352: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3353: else {
3354: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3355: BDY(a) = (pointer)nd_ps[j];
3356: }
3357: }
3358: NEXT(a) = 0;
3359: return a0;
3360: }
3361:
1.41 noro 3362: int ndplength(ND_pairs d)
3363: {
3364: int i;
3365: for ( i = 0; d; i++ ) d = NEXT(d);
3366: return i;
3367: }
3368:
1.1 noro 3369: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3370: {
3371: ND_pairs d1,nd,cur,head,prev,remove;
1.41 noro 3372: int len0;
1.1 noro 3373:
3374: if ( !g ) return d;
3375: /* for testing */
3376: if ( gensyz && nd_gensyz == 2 ) {
3377: d1 = nd_newpairs(g,t);
3378: if ( !d )
3379: return d1;
3380: else {
3381: nd = d;
3382: while ( NEXT(nd) ) nd = NEXT(nd);
3383: NEXT(nd) = d1;
3384: return d;
3385: }
3386: }
3387: d = crit_B(d,t);
3388: d1 = nd_newpairs(g,t);
1.41 noro 3389: len0 = ndplength(d1);
1.1 noro 3390: d1 = crit_M(d1);
3391: d1 = crit_F(d1);
1.41 noro 3392: NcriMF += len0-ndplength(d1);
1.1 noro 3393: if ( gensyz || do_weyl )
3394: head = d1;
3395: else {
3396: prev = 0; cur = head = d1;
3397: while ( cur ) {
3398: if ( crit_2( cur->i1,cur->i2 ) ) {
3399: remove = cur;
3400: if ( !prev ) head = cur = NEXT(cur);
3401: else cur = NEXT(prev) = NEXT(cur);
1.41 noro 3402: FREENDP(remove); Ncri2++;
1.1 noro 3403: } else {
3404: prev = cur; cur = NEXT(cur);
3405: }
3406: }
3407: }
3408: if ( !d )
3409: return head;
3410: else {
3411: nd = d;
3412: while ( NEXT(nd) ) nd = NEXT(nd);
3413: NEXT(nd) = head;
3414: return d;
3415: }
3416: }
3417:
1.24 noro 3418: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3419:
1.30 noro 3420: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3421: {
3422: ND_pairs d1;
1.26 noro 3423: struct oEGT eg1,eg2,eg3;
1.24 noro 3424:
1.30 noro 3425: if ( !t ) return d;
1.26 noro 3426: get_eg(&eg1);
1.30 noro 3427: d1 = nd_newpairs_s(t,syz);
1.26 noro 3428: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3429: d = merge_pairs_s(d,d1);
1.26 noro 3430: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3431: return d;
3432: }
1.1 noro 3433:
3434: ND_pairs nd_newpairs( NODE g, int t )
3435: {
3436: NODE h;
3437: UINT *dl;
3438: int ts,s,i,t0,min,max;
3439: ND_pairs r,r0;
3440:
3441: dl = DL(nd_psh[t]);
3442: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3443: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3444: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3445: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3446: continue;
3447: if ( nd_gbblock ) {
3448: t0 = (long)BDY(h);
3449: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3450: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3451: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3452: break;
3453: }
3454: if ( nd_gbblock[i] >= 0 )
3455: continue;
3456: }
1.41 noro 3457: NEXTND_pairs(r0,r); Npairs++;
1.1 noro 3458: r->i1 = (long)BDY(h);
3459: r->i2 = t;
3460: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3461: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3462: SG(r) = MAX(s,ts) + TD(LCM(r));
3463: /* experimental */
3464: if ( nd_sugarweight )
3465: r->sugar2 = ndl_weight2(r->lcm);
3466: }
3467: if ( r0 ) NEXT(r) = 0;
3468: return r0;
3469: }
3470:
1.46 ! noro 3471: int sig_cmpdl_op(int n,DL d1,DL d2)
! 3472: {
! 3473: int e1,e2,i,j,l;
! 3474: int *t1,*t2;
! 3475: int len,head;
! 3476: struct order_pair *pair;
! 3477:
! 3478: len = nd_sba_modord->block_length;
! 3479: pair = nd_sba_modord->order_pair;
! 3480:
! 3481: head = 0;
! 3482: for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
! 3483: l = pair[i].length;
! 3484: switch ( pair[i].order ) {
! 3485: case 0:
! 3486: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
! 3487: e1 += t1[j];
! 3488: e2 += t2[j];
! 3489: }
! 3490: if ( e1 > e2 )
! 3491: return 1;
! 3492: else if ( e1 < e2 )
! 3493: return -1;
! 3494: else {
! 3495: for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
! 3496: if ( j >= 0 )
! 3497: return t1[j] < t2[j] ? 1 : -1;
! 3498: }
! 3499: break;
! 3500: case 1:
! 3501: for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
! 3502: e1 += t1[j];
! 3503: e2 += t2[j];
! 3504: }
! 3505: if ( e1 > e2 )
! 3506: return 1;
! 3507: else if ( e1 < e2 )
! 3508: return -1;
! 3509: else {
! 3510: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
! 3511: if ( j < l )
! 3512: return t1[j] > t2[j] ? 1 : -1;
! 3513: }
! 3514: break;
! 3515: case 2:
! 3516: for ( j = 0; j < l && t1[j] == t2[j]; j++ );
! 3517: if ( j < l )
! 3518: return t1[j] > t2[j] ? 1 : -1;
! 3519: break;
! 3520: default:
! 3521: error("sig_cmpdl_op : invalid order"); break;
! 3522: }
! 3523: t1 += l; t2 += l; head += l;
! 3524: }
! 3525: return 0;
! 3526: }
! 3527:
! 3528: int sig_cmpdl_mat(int n,DL d1,DL d2)
! 3529: {
! 3530: int *v,*t1,*t2;
! 3531: int s,i,j,len;
! 3532: int **matrix;
! 3533: static int *w;
! 3534: static int nvar = 0;
! 3535:
! 3536: if ( nvar != n ) {
! 3537: nvar = n; w = (int *)MALLOC(n*sizeof(int));
! 3538: }
! 3539: for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
! 3540: w[i] = t1[i]-t2[i];
! 3541: len = nd_sba_modord->row;
! 3542: matrix = nd_sba_modord->matrix;
! 3543: for ( j = 0; j < len; j++ ) {
! 3544: v = matrix[j];
! 3545: for ( i = 0, s = 0; i < n; i++ )
! 3546: s += v[i]*w[i];
! 3547: if ( s > 0 )
! 3548: return 1;
! 3549: else if ( s < 0 )
! 3550: return -1;
! 3551: }
! 3552: return 0;
! 3553: }
! 3554:
! 3555: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
! 3556: {
! 3557: struct comp_sig_spec *spec;
! 3558: VL ovl,vl;
! 3559: V ov;
! 3560: int i,j,n,nvar,s;
! 3561: NODE node,t,tn;
! 3562: struct order_pair *l;
! 3563: MAT m;
! 3564: Obj **b;
! 3565: int **w;
! 3566: int *a;
! 3567:
! 3568: spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
! 3569: for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
! 3570: spec->n = nvar = i;
! 3571: if ( old_vl != 0 ) {
! 3572: spec->oldv = (int *)MALLOC(nvar*sizeof(int));
! 3573: for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
! 3574: ov = ovl->v;
! 3575: for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
! 3576: if ( ov == vl->v ) break;
! 3577: spec->oldv[i] = j;
! 3578: }
! 3579: } else
! 3580: spec->oldv = 0;
! 3581: if ( !ord || NUM(ord) ) {
! 3582: switch ( ZTOS((Z)ord) ) {
! 3583: case 0:
! 3584: spec->cmpdl = cmpdl_revgradlex; break;
! 3585: case 1:
! 3586: spec->cmpdl = cmpdl_gradlex; break;
! 3587: case 2:
! 3588: spec->cmpdl = cmpdl_lex; break;
! 3589: default:
! 3590: error("create_comp_sig_spec : invalid spec"); break;
! 3591: }
! 3592: } else if ( OID(ord) == O_LIST ) {
! 3593: node = BDY((LIST)ord);
! 3594: for ( n = 0, t = node; t; t = NEXT(t), n++ );
! 3595: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
! 3596: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
! 3597: tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
! 3598: tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
! 3599: s += l[i].length;
! 3600: }
! 3601: if ( s != nvar )
! 3602: error("create_comp_sig_spec : invalid spec");
! 3603: spec->order_pair = l;
! 3604: spec->block_length = n;
! 3605: spec->cmpdl = sig_cmpdl_op;
! 3606: } else if ( OID(ord) == O_MAT ) {
! 3607: m = (MAT)ord; b = (Obj **)BDY(m);
! 3608: if ( m->col != nvar )
! 3609: error("create_comp_sig_spec : invalid spec");
! 3610: w = almat(m->row,m->col);
! 3611: for ( i = 0; i < m->row; i++ )
! 3612: for ( j = 0; j < m->col; j++ )
! 3613: w[i][j] = ZTOS((Q)b[i][j]);
! 3614: spec->row = m->row;
! 3615: spec->matrix = w;
! 3616: spec->cmpdl = sig_cmpdl_mat;
! 3617: } else
! 3618: error("create_comp_sig_spec : invalid spec");
! 3619: if ( weight != 0 ) {
! 3620: node = BDY((LIST)weight);
! 3621: a = (int *)MALLOC(nvar*sizeof(int));
! 3622: for ( i = 0; i < nvar; i++, node = NEXT(node) )
! 3623: a[i] = ZTOS((Z)BDY(node));
! 3624: spec->weight = a;
! 3625: }
! 3626: return spec;
! 3627: }
! 3628:
! 3629: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
! 3630:
! 3631: int comp_sig_monomial(int n,DL d1,DL d2)
! 3632: {
! 3633: static DL m1,m2;
! 3634: static int nvar = 0;
! 3635: int *oldv,*weight;
! 3636: int i,w1,w2;
! 3637:
! 3638: if ( nvar != n ) {
! 3639: nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
! 3640: }
! 3641: if ( !nd_sba_modord )
! 3642: return (*cmpdl)(n,d1,d2);
! 3643: else {
! 3644: weight = nd_sba_modord->weight;
! 3645: oldv = nd_sba_modord->oldv;
! 3646: if ( oldv ) {
! 3647: for ( i = 0; i < n; i++ ) {
! 3648: m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
! 3649: }
! 3650: } else {
! 3651: for ( i = 0; i < n; i++ ) {
! 3652: m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
! 3653: }
! 3654: }
! 3655: for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
! 3656: w1 += SIG_MUL_WEIGHT(m1->d[i],i);
! 3657: w2 += SIG_MUL_WEIGHT(m2->d[i],i);
! 3658: }
! 3659: m1->td = w1; m2->td = w2;
! 3660: return (*nd_sba_modord->cmpdl)(n,m1,m2);
! 3661: }
! 3662: }
! 3663:
1.24 noro 3664: int comp_sig(SIG s1,SIG s2)
3665: {
1.38 noro 3666: if ( nd_sba_pot ) {
3667: if ( s1->pos > s2->pos ) return 1;
3668: else if ( s1->pos < s2->pos ) return -1;
1.46 ! noro 3669: else return comp_sig_monomial(nd_nvar,s1->dl,s2->dl);
1.38 noro 3670: } else {
3671: static DL m1,m2;
1.44 noro 3672: static int nvar = 0;
1.38 noro 3673: int ret;
3674:
3675: if ( nvar != nd_nvar ) {
3676: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
3677: }
3678: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3679: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.46 ! noro 3680: ret = comp_sig_monomial(nd_nvar,m1,m2);
1.38 noro 3681: if ( ret != 0 ) return ret;
3682: else if ( s1->pos > s2->pos ) return 1;
3683: else if ( s1->pos < s2->pos ) return -1;
3684: else return 0;
1.24 noro 3685: }
3686: }
3687:
3688: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3689: {
3690: int ret,s1,s2;
3691: RHist p1,p2;
3692: static int wpd;
3693: static UINT *lcm;
3694:
3695: sp->i1 = i1;
3696: sp->i2 = i2;
3697: p1 = nd_psh[i1];
3698: p2 = nd_psh[i2];
3699: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3700: s1 = SG(p1)-TD(DL(p1));
3701: s2 = SG(p2)-TD(DL(p2));
3702: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3703:
1.26 noro 3704: if ( wpd != nd_wpd ) {
1.24 noro 3705: wpd = nd_wpd;
3706: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3707: }
3708: // DL(sig1) <- sp->lcm
3709: // DL(sig1) -= DL(p1)
3710: // DL(sig1) += DL(p1->sig)
1.27 noro 3711: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3712: _ndltodl(lcm,DL(sig1));
3713: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3714: sig1->pos = p1->sig->pos;
3715:
3716: // DL(sig2) <- sp->lcm
3717: // DL(sig2) -= DL(p2)
3718: // DL(sig2) += DL(p2->sig)
1.27 noro 3719: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3720: _ndltodl(lcm,DL(sig2));
3721: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3722: sig2->pos = p2->sig->pos;
3723:
3724: ret = comp_sig(sig1,sig2);
3725: if ( ret == 0 ) return 0;
3726: else if ( ret > 0 ) sp->sig = sig1;
3727: else sp->sig = sig2;
3728: return 1;
3729: }
3730:
3731: SIG dup_sig(SIG sig)
3732: {
3733: SIG r;
3734:
3735: if ( !sig ) return 0;
3736: else {
3737: NEWSIG(r);
3738: _copydl(nd_nvar,DL(sig),DL(r));
3739: r->pos = sig->pos;
3740: return r;
3741: }
3742: }
3743:
3744: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3745: {
3746: to->i1 = from->i1;
3747: to->i2 = from->i2;
3748: to->sugar = from->sugar;
3749: to->sugar2 = from->sugar2;
3750: ndl_copy(from->lcm,to->lcm);
3751: to->sig = dup_sig(from->sig);
3752: }
3753:
3754: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3755: {
3756: struct oND_pairs root;
3757: ND_pairs q1,q2,r0,r;
3758: int ret;
3759:
3760: r = &root;
3761: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3762: ret = comp_sig(q1->sig,q2->sig);
3763: if ( ret < 0 ) {
3764: r->next = q1; r = q1; q1 = q1->next;
3765: } else if ( ret > 0 ) {
3766: r->next = q2; r = q2; q2 = q2->next;
3767: } else {
3768: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 3769: Nsamesig++;
1.24 noro 3770: if ( ret < 0 ) {
3771: r->next = q1; r = q1; q1 = q1->next;
3772: q2 = q2->next;
3773: } else {
3774: r->next = q2; r = q2; q2 = q2->next;
3775: q1 = q1->next;
3776: }
3777: }
3778: }
3779: if ( q1 ) {
3780: r->next = q1;
3781: } else {
3782: r->next = q2;
3783: }
3784: return root.next;
3785: }
3786:
3787: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3788: {
3789: ND_pairs p,prev;
3790: int ret;
3791:
3792: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3793: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3794: break;
3795: }
3796: if ( ret == 0 ) {
3797: ret = DL_COMPARE(s->lcm,p->lcm);
3798: if ( ret < 0 ) {
3799: // replace p with s
3800: s->next = p->next;
3801: if ( prev == 0 ) {
3802: return s;
3803: } else {
3804: prev->next = s;
3805: return l;
3806: }
3807: } else
3808: return l;
3809: } else {
3810: // insert s between prev and p
3811: s->next = p;
3812: if ( prev == 0 ) {
3813: return s;
3814: } else {
3815: prev->next = s;
3816: return l;
3817: }
3818: }
3819: }
3820:
1.29 noro 3821: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3822: {
3823: int i;
3824:
3825: if ( d1->td > d2->td )
3826: return 0;
3827: for ( i = nvar-1; i >= 0; i-- )
3828: if ( d1->d[i] > d2->d[i] )
3829: break;
3830: if ( i >= 0 )
3831: return 0;
3832: else
3833: return 1;
3834: }
3835:
1.30 noro 3836: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3837: {
3838: NODE h,s;
3839: UINT *dl;
1.27 noro 3840: int ts,ret,i;
1.24 noro 3841: ND_pairs r,r0,_sp,sp;
1.29 noro 3842: SIG spsig,tsig;
1.44 noro 3843: static int nvar = 0;
1.29 noro 3844: static SIG _sig1,_sig2;
1.26 noro 3845: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3846:
3847: NEWND_pairs(_sp);
1.29 noro 3848: if ( !_sig1 || nvar != nd_nvar ) {
3849: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3850: }
1.24 noro 3851: r0 = 0;
1.27 noro 3852: for ( i = 0; i < t; i++ ) {
3853: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3854: if ( ret ) {
3855: spsig = _sp->sig;
1.29 noro 3856: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3857: tsig = (SIG)s->body;
1.29 noro 3858: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3859: break;
3860: }
3861: if ( s == 0 ) {
3862: NEWND_pairs(sp);
3863: dup_ND_pairs(sp,_sp);
3864: r0 = insert_pair_s(r0,sp);
1.26 noro 3865: } else
3866: Nsyz++;
1.24 noro 3867: }
3868: }
3869: return r0;
3870: }
3871:
1.1 noro 3872: /* ipair = [i1,i2],[i1,i2],... */
3873: ND_pairs nd_ipairtospair(NODE ipair)
3874: {
3875: int s1,s2;
3876: NODE tn,t;
3877: ND_pairs r,r0;
3878:
3879: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3880: NEXTND_pairs(r0,r);
3881: tn = BDY((LIST)BDY(t));
1.6 noro 3882: r->i1 = ZTOS((Q)ARG0(tn));
3883: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3884: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3885: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3886: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3887: SG(r) = MAX(s1,s2) + TD(LCM(r));
3888: /* experimental */
3889: if ( nd_sugarweight )
3890: r->sugar2 = ndl_weight2(r->lcm);
3891: }
3892: if ( r0 ) NEXT(r) = 0;
3893: return r0;
3894: }
3895:
3896: /* kokokara */
3897:
3898: ND_pairs crit_B( ND_pairs d, int s )
3899: {
3900: ND_pairs cur,head,prev,remove;
3901: UINT *t,*tl,*lcm;
3902: int td,tdl;
3903:
3904: if ( !d ) return 0;
3905: t = DL(nd_psh[s]);
3906: prev = 0;
3907: head = cur = d;
3908: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3909: while ( cur ) {
3910: tl = cur->lcm;
3911: if ( ndl_reducible(tl,t) ) {
3912: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3913: if ( !ndl_equal(lcm,tl) ) {
3914: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3915: if (!ndl_equal(lcm,tl)) {
3916: remove = cur;
3917: if ( !prev ) {
3918: head = cur = NEXT(cur);
3919: } else {
3920: cur = NEXT(prev) = NEXT(cur);
3921: }
1.41 noro 3922: FREENDP(remove); NcriB++;
1.1 noro 3923: } else {
3924: prev = cur; cur = NEXT(cur);
3925: }
3926: } else {
3927: prev = cur; cur = NEXT(cur);
3928: }
3929: } else {
3930: prev = cur; cur = NEXT(cur);
3931: }
3932: }
3933: return head;
3934: }
3935:
3936: ND_pairs crit_M( ND_pairs d1 )
3937: {
3938: ND_pairs e,d2,d3,dd,p;
3939: UINT *id,*jd;
3940:
3941: if ( !d1 ) return d1;
3942: for ( dd = 0, e = d1; e; e = d3 ) {
3943: if ( !(d2 = NEXT(e)) ) {
3944: NEXT(e) = dd;
3945: return e;
3946: }
3947: id = LCM(e);
3948: for ( d3 = 0; d2; d2 = p ) {
3949: p = NEXT(d2);
3950: jd = LCM(d2);
3951: if ( ndl_equal(jd,id) )
3952: ;
3953: else if ( TD(jd) > TD(id) )
3954: if ( ndl_reducible(jd,id) ) continue;
3955: else ;
3956: else if ( ndl_reducible(id,jd) ) goto delit;
3957: NEXT(d2) = d3;
3958: d3 = d2;
3959: }
3960: NEXT(e) = dd;
3961: dd = e;
3962: continue;
3963: /**/
3964: delit: NEXT(d2) = d3;
3965: d3 = d2;
3966: for ( ; p; p = d2 ) {
3967: d2 = NEXT(p);
3968: NEXT(p) = d3;
3969: d3 = p;
3970: }
3971: FREENDP(e);
3972: }
3973: return dd;
3974: }
3975:
3976: ND_pairs crit_F( ND_pairs d1 )
3977: {
3978: ND_pairs rest, head,remove;
3979: ND_pairs last, p, r, w;
3980: int s;
3981:
3982: if ( !d1 ) return d1;
3983: for ( head = last = 0, p = d1; NEXT(p); ) {
3984: r = w = equivalent_pairs(p,&rest);
3985: s = SG(r);
3986: w = NEXT(w);
3987: while ( w ) {
3988: if ( crit_2(w->i1,w->i2) ) {
3989: r = w;
3990: w = NEXT(w);
3991: while ( w ) {
3992: remove = w;
3993: w = NEXT(w);
3994: FREENDP(remove);
3995: }
3996: break;
3997: } else if ( SG(w) < s ) {
3998: FREENDP(r);
3999: r = w;
4000: s = SG(r);
4001: w = NEXT(w);
4002: } else {
4003: remove = w;
4004: w = NEXT(w);
4005: FREENDP(remove);
4006: }
4007: }
4008: if ( last ) NEXT(last) = r;
4009: else head = r;
4010: NEXT(last = r) = 0;
4011: p = rest;
4012: if ( !p ) return head;
4013: }
4014: if ( !last ) return p;
4015: NEXT(last) = p;
4016: return head;
4017: }
4018:
4019: int crit_2( int dp1, int dp2 )
4020: {
4021: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
4022: }
4023:
4024: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
4025: {
4026: ND_pairs w,p,r,s;
4027: UINT *d;
4028:
4029: w = d1;
4030: d = LCM(w);
4031: s = NEXT(w);
4032: NEXT(w) = 0;
4033: for ( r = 0; s; s = p ) {
4034: p = NEXT(s);
4035: if ( ndl_equal(d,LCM(s)) ) {
4036: NEXT(s) = w; w = s;
4037: } else {
4038: NEXT(s) = r; r = s;
4039: }
4040: }
4041: *prest = r;
4042: return w;
4043: }
4044:
4045: NODE update_base(NODE nd,int ndp)
4046: {
4047: UINT *dl, *dln;
4048: NODE last, p, head;
4049:
4050: dl = DL(nd_psh[ndp]);
4051: for ( head = last = 0, p = nd; p; ) {
4052: dln = DL(nd_psh[(long)BDY(p)]);
4053: if ( ndl_reducible( dln, dl ) ) {
4054: p = NEXT(p);
4055: if ( last ) NEXT(last) = p;
4056: } else {
4057: if ( !last ) head = p;
4058: p = NEXT(last = p);
4059: }
4060: }
4061: head = append_one(head,ndp);
4062: return head;
4063: }
4064:
4065: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
4066: {
4067: ND_pairs m,ml,p,l;
4068: UINT *lcm;
4069: int s,td,len,tlen,c,c1;
4070:
4071: if ( !(p = NEXT(m = d)) ) {
4072: *prest = p;
4073: NEXT(m) = 0;
4074: return m;
4075: }
4076: if ( !NoSugar ) {
4077: if ( nd_sugarweight ) {
4078: s = m->sugar2;
4079: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4080: if ( (p->sugar2 < s)
4081: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4082: ml = l; m = p; s = m->sugar2;
4083: }
4084: } else {
4085: s = SG(m);
4086: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4087: if ( (SG(p) < s)
4088: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
4089: ml = l; m = p; s = SG(m);
4090: }
4091: }
4092: } else {
4093: for ( ml = 0, l = m; p; p = NEXT(l = p) )
4094: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
4095: ml = l; m = p; s = SG(m);
4096: }
4097: }
4098: if ( !ml ) *prest = NEXT(m);
4099: else {
4100: NEXT(ml) = NEXT(m);
4101: *prest = d;
4102: }
4103: NEXT(m) = 0;
4104: return m;
4105: }
4106:
4107: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
4108: {
4109: int msugar,i;
4110: ND_pairs t,dm0,dm,dr0,dr;
4111:
4112: if ( nd_sugarweight ) {
4113: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
4114: if ( t->sugar2 < msugar ) msugar = t->sugar2;
4115: dm0 = 0; dr0 = 0;
4116: for ( i = 0, t = d; t; t = NEXT(t) )
4117: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
4118: if ( dm0 ) NEXT(dm) = t;
4119: else dm0 = t;
4120: dm = t;
4121: i++;
4122: } else {
4123: if ( dr0 ) NEXT(dr) = t;
4124: else dr0 = t;
4125: dr = t;
4126: }
4127: } else {
4128: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
4129: if ( SG(t) < msugar ) msugar = SG(t);
4130: dm0 = 0; dr0 = 0;
4131: for ( i = 0, t = d; t; t = NEXT(t) )
4132: if ( i < nd_f4_nsp && SG(t) == msugar ) {
4133: if ( dm0 ) NEXT(dm) = t;
4134: else dm0 = t;
4135: dm = t;
4136: i++;
4137: } else {
4138: if ( dr0 ) NEXT(dr) = t;
4139: else dr0 = t;
4140: dr = t;
4141: }
4142: }
4143: NEXT(dm) = 0;
4144: if ( dr0 ) NEXT(dr) = 0;
4145: *prest = dr0;
4146: return dm0;
4147: }
4148:
1.28 noro 4149: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
4150: {
4151: int msugar;
4152: ND_pairs t,last;
4153:
4154: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
4155: if ( SG(t) == msugar ) last = t;
4156: *prest = last->next;
4157: last->next = 0;
4158: return d;
4159: }
4160:
1.1 noro 4161: int nd_tdeg(NDV c)
4162: {
4163: int wmax = 0;
4164: int i,len;
4165: NMV a;
4166:
4167: len = LEN(c);
4168: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4169: wmax = MAX(TD(DL(a)),wmax);
4170: return wmax;
4171: }
4172:
1.24 noro 4173: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 4174: {
4175: int len;
4176: RHist r;
4177: NDV b;
4178: NODE tn;
4179: LIST l;
4180: Z iq;
4181:
4182: if ( nd_psn == nd_pslen ) {
4183: nd_pslen *= 2;
4184: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
4185: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
4186: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
4187: nd_bound = (UINT **)
4188: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
4189: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
4190: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
4191: }
4192: NEWRHist(r); nd_psh[nd_psn] = r;
4193: nd_ps[nd_psn] = a;
4194: if ( aq ) {
4195: nd_ps_trace[nd_psn] = aq;
4196: if ( !m ) {
4197: register_hcf(aq);
4198: } else if ( m == -2 ) {
4199: /* do nothing */
4200: } else
4201: error("ndv_newps : invalud modulus");
4202: nd_bound[nd_psn] = ndv_compute_bound(aq);
4203: #if 1
4204: SG(r) = SG(aq);
4205: #else
4206: SG(r) = nd_tdeg(aq);
4207: #endif
4208: ndl_copy(HDL(aq),DL(r));
1.24 noro 4209: r->sig = dup_sig(aq->sig);
1.1 noro 4210: } else {
4211: if ( !m ) register_hcf(a);
4212: nd_bound[nd_psn] = ndv_compute_bound(a);
4213: #if 1
4214: SG(r) = SG(a);
4215: #else
4216: SG(r) = nd_tdeg(a);
4217: #endif
4218: ndl_copy(HDL(a),DL(r));
1.24 noro 4219: r->sig = dup_sig(a->sig);
1.1 noro 4220: }
4221: if ( nd_demand ) {
4222: if ( aq ) {
4223: ndv_save(nd_ps_trace[nd_psn],nd_psn);
4224: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4225: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
4226: nd_ps_trace[nd_psn] = 0;
4227: } else {
4228: ndv_save(nd_ps[nd_psn],nd_psn);
4229: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
4230: nd_ps[nd_psn] = 0;
4231: }
4232: }
4233: if ( nd_gentrace ) {
4234: /* reverse the tracelist and append it to alltracelist */
4235: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 4236: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 4237: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
4238: }
4239: return nd_psn++;
4240: }
4241:
1.46 ! noro 4242: // find LM wrt the specified modord
! 4243: void ndv_lm_modord(NDV p,DL d)
1.45 noro 4244: {
4245: NMV m;
4246: DL tmp;
4247: int len,i,ret;
4248:
4249: NEWDL(tmp,nd_nvar);
4250: m = BDY(p); len = LEN(p);
1.46 ! noro 4251: _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45 noro 4252: for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
4253: _ndltodl(DL(m),tmp);
1.46 ! noro 4254: ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45 noro 4255: if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
4256: }
1.46 ! noro 4257: // printdl(d); printf("\n");
1.45 noro 4258: }
4259:
1.1 noro 4260: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
4261: /* return 1 if success, 0 if failure (HC(a mod p)) */
4262:
1.24 noro 4263: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 4264: {
1.6 noro 4265: int i,j,td,len,max;
4266: NODE s,s0,f0,tn;
4267: UINT *d;
4268: RHist r;
4269: NDVI w;
4270: NDV a,am;
4271: union oNDC hc;
4272: NODE node;
4273: P hcp;
4274: Z iq,jq;
4275: LIST l;
4276:
4277: nd_found = 0; nd_notfirst = 0; nd_create = 0;
4278: /* initialize the tracelist */
4279: nd_tracelist = 0;
4280:
4281: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
4282: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
4283: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
4284: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
4285: if ( !dont_sort ) {
4286: /* XXX heuristic */
1.26 noro 4287: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 4288: qsort(w,nd_psn,sizeof(struct oNDVI),
4289: (int (*)(const void *,const void *))ndvi_compare_rev);
4290: else
4291: qsort(w,nd_psn,sizeof(struct oNDVI),
4292: (int (*)(const void *,const void *))ndvi_compare);
4293: }
4294: nd_pslen = 2*nd_psn;
4295: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4296: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4297: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4298: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
4299: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
4300: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
4301: nd_hcf = 0;
1.1 noro 4302:
1.6 noro 4303: if ( trace && nd_vc )
4304: makesubst(nd_vc,&nd_subst);
4305: else
4306: nd_subst = 0;
1.1 noro 4307:
1.6 noro 4308: if ( !nd_red )
4309: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4310: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
4311: for ( i = 0; i < nd_psn; i++ ) {
4312: hc = HCU(w[i].p);
4313: if ( trace ) {
4314: if ( mod == -2 ) {
4315: /* over a large finite field */
4316: /* trace = small modulus */
4317: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
4318: ndv_mod(-2,a);
4319: if ( !dont_removecont) ndv_removecont(-2,a);
4320: am = nd_ps[i] = ndv_dup(trace,w[i].p);
4321: ndv_mod(trace,am);
4322: if ( DL_COMPARE(HDL(am),HDL(a)) )
4323: return 0;
4324: ndv_removecont(trace,am);
4325: } else {
4326: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
4327: if ( !dont_removecont) ndv_removecont(0,a);
4328: register_hcf(a);
4329: am = nd_ps[i] = ndv_dup(mod,a);
4330: ndv_mod(mod,am);
4331: if ( DL_COMPARE(HDL(am),HDL(a)) )
4332: return 0;
4333: ndv_removecont(mod,am);
4334: }
4335: } else {
4336: a = nd_ps[i] = ndv_dup(mod,w[i].p);
4337: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
4338: if ( !mod ) register_hcf(a);
1.1 noro 4339: }
1.6 noro 4340: if ( nd_gentrace ) {
4341: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
4342: /* exact division */
1.1 noro 4343: if ( !dont_removecont )
1.6 noro 4344: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
4345: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
4346: }
4347: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
4348: nd_bound[i] = ndv_compute_bound(a);
4349: nd_psh[i] = r;
4350: if ( nd_demand ) {
4351: if ( trace ) {
4352: ndv_save(nd_ps_trace[i],i);
4353: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4354: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
4355: nd_ps_trace[i] = 0;
4356: } else {
4357: ndv_save(nd_ps[i],i);
4358: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
4359: nd_ps[i] = 0;
4360: }
1.1 noro 4361: }
1.6 noro 4362: }
1.24 noro 4363: if ( sba ) {
1.27 noro 4364: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 4365: // setup signatures
1.27 noro 4366: for ( i = 0; i < nd_psn; i++ ) {
4367: SIG sig;
1.24 noro 4368:
1.27 noro 4369: NEWSIG(sig); sig->pos = i;
4370: nd_ps[i]->sig = sig;
4371: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
4372: nd_psh[i]->sig = sig;
4373: if ( trace ) {
4374: nd_ps_trace[i]->sig = sig;
4375: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
4376: }
4377: NEWDL(nd_sba_hm[i],nd_nvar);
1.46 ! noro 4378: if ( nd_sba_modord )
! 4379: ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45 noro 4380: else
4381: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27 noro 4382: }
1.29 noro 4383: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
4384: for ( i = 0; i < nd_psn; i++ ) {
4385: j = nd_psh[i]->sig->pos;
4386: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
4387: }
1.24 noro 4388: }
1.6 noro 4389: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
4390: return 1;
1.1 noro 4391: }
4392:
4393: struct order_spec *append_block(struct order_spec *spec,
4394: int nv,int nalg,int ord);
4395:
4396: extern VECT current_dl_weight_vector_obj;
4397: static VECT prev_weight_vector_obj;
4398:
4399: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
4400: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
4401: {
4402: NODE alist,t,s,r0,r,arg;
4403: VL tv;
4404: P poly;
4405: DP d;
4406: Alg alpha,dp;
4407: DAlg inv,da,hc;
4408: MP m;
4409: int i,nvar,nalg,n;
4410: NumberField nf;
4411: LIST f1,f2;
4412: struct order_spec *current_spec;
4413: VECT obj,obj0;
4414: VECT tmp;
4415:
4416: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
4417: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
4418:
4419: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
4420: NEXTNODE(alist,t); MKV(tv->v,poly);
4421: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
4422: tv->v = tv->v->priv;
4423: }
4424: NEXT(t) = 0;
4425:
4426: /* simplification, making polynomials monic */
4427: setfield_dalg(alist);
4428: obj_algtodalg((Obj)f,(Obj *)&f1);
4429: for ( t = BDY(f); t; t = NEXT(t) ) {
4430: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
4431: hc = (DAlg)BDY(d)->c;
4432: if ( NID(hc) == N_DA ) {
4433: invdalg(hc,&inv);
4434: for ( m = BDY(d); m; m = NEXT(m) ) {
4435: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
4436: }
4437: }
4438: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
4439: }
4440: obj_dalgtoalg((Obj)f1,(Obj *)&f);
4441:
4442: /* append alg vars to the var list */
4443: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
4444: NEXT(tv) = av;
4445:
4446: /* append a block to ord */
4447: *ord1p = append_block(ord,nvar,nalg,2);
4448:
4449: /* create generator list */
4450: nf = get_numberfield();
4451: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
4452: MKAlg(nf->defpoly[i],dp);
4453: MKNODE(s,dp,t); t = s;
4454: }
4455: MKLIST(f1,t);
4456: *alistp = alist;
4457: algobjtorat((Obj)f1,(Obj *)f1p);
4458:
4459: /* creating a new weight vector */
4460: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
4461: n = nvar+nalg+1;
4462: MKVECT(obj,n);
4463: if ( obj0 && obj0->len == nvar )
4464: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
4465: else
4466: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
4467: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
4468: BDY(obj)[n-1] = (pointer)ONE;
4469: arg = mknode(1,obj);
4470: Pdp_set_weight(arg,&tmp);
4471: }
4472:
4473: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4474: {
4475: NODE s,t,u0,u;
4476: P p;
4477: VL tv;
4478: Obj obj;
4479: VECT tmp;
4480: NODE arg;
4481:
4482: u0 = 0;
4483: for ( t = r; t; t = NEXT(t) ) {
4484: p = (P)BDY(t);
4485: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4486: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4487: }
4488: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4489: NEXTNODE(u0,u);
4490: BDY(u) = (pointer)p;
4491: }
4492: }
4493: arg = mknode(1,prev_weight_vector_obj);
4494: Pdp_set_weight(arg,&tmp);
4495:
4496: return u0;
4497: }
4498:
4499: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4500: {
4501: VL tv,fv,vv,vc,av;
4502: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4503: int e,max,nvar,i;
4504: NDV b;
4505: int ishomo,nalg,mrank,trank,wmax,len;
4506: NMV a;
4507: Alg alpha,dp;
4508: P p,zp;
4509: Q dmy;
4510: LIST f1,f2,zpl;
4511: Obj obj;
4512: NumberField nf;
4513: struct order_spec *ord1;
4514: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4515: LIST l1,l2,l3,l4,l5;
4516: int j;
4517: Z jq,bpe,last_nonzero;
4518: int *perm;
4519: EPOS oepos;
4520: int obpe,oadv,ompos,cbpe;
1.15 noro 4521: VECT hvect;
1.1 noro 4522:
1.41 noro 4523: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 4524: nd_module = 0;
4525: if ( !m && Demand ) nd_demand = 1;
4526: else nd_demand = 0;
4527:
4528: if ( DP_Multiple )
4529: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4530: #if 0
4531: ndv_alloc = 0;
4532: #endif
4533: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 4534: parse_nd_option(vv,current_option);
1.1 noro 4535: if ( m && nd_vc )
4536: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4537: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4538: switch ( ord->id ) {
4539: case 1:
4540: if ( ord->nv != nvar )
4541: error("nd_{gr,f4} : invalid order specification");
4542: break;
4543: default:
4544: break;
4545: }
4546: nd_nalg = 0;
4547: av = 0;
4548: if ( !m ) {
4549: get_algtree((Obj)f,&av);
4550: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4551: nd_ntrans = nvar;
4552: nd_nalg = nalg;
4553: /* #i -> t#i */
4554: if ( nalg ) {
4555: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4556: ord = ord1;
4557: f = f1;
4558: }
4559: nvar += nalg;
4560: }
4561: nd_init_ord(ord);
4562: mrank = 0;
4563: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4564: for ( tv = vv; tv; tv = NEXT(tv) ) {
4565: if ( nd_module ) {
1.16 noro 4566: if ( OID(BDY(t)) == O_DPM ) {
4567: e = dpm_getdeg((DPM)BDY(t),&trank);
4568: max = MAX(e,max);
4569: mrank = MAX(mrank,trank);
4570: } else {
4571: s = BDY((LIST)BDY(t));
4572: trank = length(s);
4573: mrank = MAX(mrank,trank);
4574: for ( ; s; s = NEXT(s) ) {
4575: e = getdeg(tv->v,(P)BDY(s));
4576: max = MAX(e,max);
4577: }
1.1 noro 4578: }
4579: } else {
1.43 noro 4580: if ( OID(BDY(t)) == O_DP ) {
4581: e = dp_getdeg((DP)BDY(t));
4582: max = MAX(e,max);
4583: } else {
4584: e = getdeg(tv->v,(P)BDY(t));
4585: max = MAX(e,max);
4586: }
1.1 noro 4587: }
4588: }
4589: nd_setup_parameters(nvar,nd_nzlist?0:max);
4590: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4591: ishomo = 1;
4592: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4593: if ( nd_module ) {
1.16 noro 4594: if ( OID(BDY(t)) == O_DPM ) {
4595: Z cont;
4596: DPM zdpm;
4597:
4598: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4599: else zdpm = (DPM)BDY(t);
4600: b = (pointer)dpmtondv(m,zdpm);
4601: } else {
4602: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4603: else zpl = (LIST)BDY(t);
1.1 noro 4604: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4605: }
1.1 noro 4606: } else {
1.43 noro 4607: if ( OID(BDY(t)) == O_DP ) {
4608: DP zdp;
4609:
4610: if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
4611: else zdp = (DP)BDY(t);
4612: b = (pointer)dptondv(m,zdp);
4613: } else {
4614: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4615: else zp = (P)BDY(t);
4616: b = (pointer)ptondv(CO,vv,zp);
4617: }
1.1 noro 4618: }
4619: if ( ishomo )
4620: ishomo = ishomo && ndv_ishomo(b);
4621: if ( m ) ndv_mod(m,b);
4622: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4623: }
4624: if ( fd0 ) NEXT(fd) = 0;
4625:
4626: if ( !ishomo && homo ) {
4627: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4628: b = (NDV)BDY(t); len = LEN(b);
4629: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4630: wmax = MAX(TD(DL(a)),wmax);
4631: }
4632: homogenize_order(ord,nvar,&ord1);
4633: nd_init_ord(ord1);
4634: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4635: for ( t = fd0; t; t = NEXT(t) )
4636: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4637: }
4638:
1.24 noro 4639: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4640: if ( nd_gentrace ) {
4641: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4642: }
4643: if ( nd_splist ) {
4644: *rp = compute_splist();
4645: return;
4646: }
4647: if ( nd_check_splist ) {
4648: if ( f4 ) {
4649: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4650: else *rp = 0;
4651: } else {
4652: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4653: else *rp = 0;
4654: }
4655: return;
4656: }
4657: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4658: if ( !x ) {
4659: *rp = 0; return;
4660: }
1.15 noro 4661: if ( nd_gentrace ) {
4662: MKVECT(hvect,nd_psn);
4663: for ( i = 0; i < nd_psn; i++ )
4664: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4665: }
1.1 noro 4666: if ( !ishomo && homo ) {
4667: /* dehomogenization */
4668: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4669: nd_init_ord(ord);
4670: nd_setup_parameters(nvar,0);
4671: }
4672: nd_demand = 0;
4673: if ( nd_module && nd_intersect ) {
4674: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4675: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4676: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4677: }
4678: conv_ilist(nd_demand,0,x,0);
4679: goto FINAL;
4680: }
4681: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4682: x = ndv_reducebase(x,perm);
4683: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4684: x = ndv_reduceall(m,x);
4685: cbpe = nd_bpe;
4686: if ( nd_gentrace && !f4 ) {
4687: tl2 = nd_alltracelist; nd_alltracelist = 0;
4688: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4689: tl3 = nd_alltracelist; nd_alltracelist = 0;
4690: if ( nd_gensyz ) {
4691: nd_gb(m,0,1,1,0);
4692: tl4 = nd_alltracelist; nd_alltracelist = 0;
4693: } else tl4 = 0;
4694: }
4695: nd_bpe = cbpe;
4696: nd_setup_parameters(nd_nvar,0);
4697: FINAL:
4698: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4699: NEXTNODE(r0,r);
4700: if ( nd_module ) {
4701: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4702: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4703: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4704: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4705: }
4706: if ( r0 ) NEXT(r) = 0;
4707: if ( !m && nd_nalg )
4708: r0 = postprocess_algcoef(av,alist,r0);
4709: MKLIST(*rp,r0);
4710: if ( nd_gentrace ) {
4711: if ( f4 ) {
1.6 noro 4712: STOZ(16,bpe);
4713: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4714: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4715: } else {
4716: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4717: tl3 = reverse_node(tl3);
4718: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4719: for ( t = tl2; t; t = NEXT(t) ) {
4720: /* s = [i,[*,j,*,*],...] */
4721: s = BDY((LIST)BDY(t));
1.6 noro 4722: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4723: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4724: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4725: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4726: }
4727: }
4728: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4729: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4730: }
4731: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4732: MKLIST(l5,tl4);
1.6 noro 4733: STOZ(nd_bpe,bpe);
1.15 noro 4734: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4735: }
4736: }
4737: #if 0
4738: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4739: #endif
4740: }
4741:
1.28 noro 4742: NODE nd_sba_f4(int m,int **indp);
4743:
4744: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4745: {
4746: VL tv,fv,vv,vc,av;
1.44 noro 4747: NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24 noro 4748: int e,max,nvar,i;
4749: NDV b;
4750: int ishomo,nalg,wmax,len;
4751: NMV a;
4752: P p,zp;
4753: Q dmy;
4754: struct order_spec *ord1;
4755: int j;
4756: int *perm;
4757: EPOS oepos;
4758: int obpe,oadv,ompos,cbpe;
1.29 noro 4759: struct oEGT eg0,eg1,egconv;
1.24 noro 4760:
4761: nd_module = 0;
4762: nd_demand = 0;
1.35 noro 4763: Nsamesig = 0;
1.24 noro 4764: if ( DP_Multiple )
4765: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4766: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 4767: parse_nd_option(vv,current_option);
1.24 noro 4768: if ( m && nd_vc )
4769: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4770: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4771: switch ( ord->id ) {
4772: case 1:
4773: if ( ord->nv != nvar )
4774: error("nd_sba : invalid order specification");
4775: break;
4776: default:
4777: break;
4778: }
4779: nd_nalg = 0;
4780: nd_init_ord(ord);
4781: // for SIG comparison
4782: initd(ord);
4783: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4784: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43 noro 4785: if ( OID(BDY(t)) == O_DP ) {
4786: e = dp_getdeg((DP)BDY(t));
4787: max = MAX(e,max);
4788: } else {
4789: e = getdeg(tv->v,(P)BDY(t));
4790: max = MAX(e,max);
4791: }
1.24 noro 4792: }
4793: }
1.25 noro 4794: nd_setup_parameters(nvar,max);
1.24 noro 4795: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4796: ishomo = 1;
4797: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43 noro 4798: if ( OID(BDY(t)) == O_DP ) {
4799: DP zdp;
4800:
4801: if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
4802: else zdp = (DP)BDY(t);
4803: b = (pointer)dptondv(m,zdp);
4804: } else {
4805: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4806: else zp = (P)BDY(t);
4807: b = (pointer)ptondv(CO,vv,zp);
4808: }
1.24 noro 4809: if ( ishomo )
4810: ishomo = ishomo && ndv_ishomo(b);
4811: if ( m ) ndv_mod(m,b);
4812: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4813: }
4814: if ( fd0 ) NEXT(fd) = 0;
4815:
4816: if ( !ishomo && homo ) {
4817: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4818: b = (NDV)BDY(t); len = LEN(b);
4819: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4820: wmax = MAX(TD(DL(a)),wmax);
4821: }
4822: homogenize_order(ord,nvar,&ord1);
4823: nd_init_ord(ord1);
1.42 noro 4824: // for SIG comparison
4825: initd(ord1);
1.24 noro 4826: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4827: for ( t = fd0; t; t = NEXT(t) )
4828: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4829: }
4830:
1.39 noro 4831: ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44 noro 4832: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24 noro 4833: if ( !x ) {
4834: *rp = 0; return;
4835: }
4836: if ( !ishomo && homo ) {
4837: /* dehomogenization */
4838: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4839: nd_init_ord(ord);
1.42 noro 4840: // for SIG comparison
4841: initd(ord);
1.24 noro 4842: nd_setup_parameters(nvar,0);
4843: }
4844: nd_demand = 0;
4845: x = ndv_reducebase(x,perm);
4846: x = ndv_reduceall(m,x);
4847: nd_setup_parameters(nd_nvar,0);
1.29 noro 4848: get_eg(&eg0);
1.24 noro 4849: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4850: NEXTNODE(r0,r);
4851: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4852: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4853: }
4854: if ( r0 ) NEXT(r) = 0;
1.44 noro 4855: if ( nd_sba_syz ) {
4856: LIST gb,hsyz;
4857: NODE nd;
4858:
4859: MKLIST(gb,r0);
4860: MKLIST(hsyz,syz);
4861: nd = mknode(2,gb,hsyz);
4862: MKLIST(*rp,nd);
4863: } else
4864: MKLIST(*rp,r0);
1.29 noro 4865: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4866: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4867: }
4868:
1.1 noro 4869: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4870: {
4871: VL tv,fv,vv,vc,av;
4872: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4873: int e,max,nvar,i;
4874: NDV b;
4875: int ishomo,nalg;
4876: Alg alpha,dp;
4877: P p,zp;
4878: Q dmy;
4879: LIST f1,f2;
4880: Obj obj;
4881: NumberField nf;
4882: struct order_spec *ord1;
4883: int *perm;
4884:
4885: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 4886: parse_nd_option(vv,current_option);
1.1 noro 4887: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4888: switch ( ord->id ) {
4889: case 1:
4890: if ( ord->nv != nvar )
4891: error("nd_check : invalid order specification");
4892: break;
4893: default:
4894: break;
4895: }
4896: nd_nalg = 0;
4897: av = 0;
4898: if ( !m ) {
4899: get_algtree((Obj)f,&av);
4900: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4901: nd_ntrans = nvar;
4902: nd_nalg = nalg;
4903: /* #i -> t#i */
4904: if ( nalg ) {
4905: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4906: ord = ord1;
4907: f = f1;
4908: }
4909: nvar += nalg;
4910: }
4911: nd_init_ord(ord);
4912: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4913: for ( tv = vv; tv; tv = NEXT(tv) ) {
4914: e = getdeg(tv->v,(P)BDY(t));
4915: max = MAX(e,max);
4916: }
4917: nd_setup_parameters(nvar,max);
4918: ishomo = 1;
4919: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4920: ptozp((P)BDY(t),1,&dmy,&zp);
4921: b = (pointer)ptondv(CO,vv,zp);
4922: if ( ishomo )
4923: ishomo = ishomo && ndv_ishomo(b);
4924: if ( m ) ndv_mod(m,b);
4925: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4926: }
4927: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4928: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4929: for ( x = 0, i = 0; i < nd_psn; i++ )
4930: x = update_base(x,i);
4931: if ( do_check ) {
4932: x = nd_gb(m,ishomo,1,0,&perm);
4933: if ( !x ) {
4934: *rp = 0;
4935: return;
4936: }
4937: } else {
4938: #if 0
4939: /* bug ? */
4940: for ( t = x; t; t = NEXT(t) )
4941: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4942: #else
4943: conv_ilist(0,0,x,&perm);
4944: #endif
4945: }
4946: x = ndv_reducebase(x,perm);
4947: x = ndv_reduceall(m,x);
4948: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4949: NEXTNODE(r0,r);
4950: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4951: }
4952: if ( r0 ) NEXT(r) = 0;
4953: if ( !m && nd_nalg )
4954: r0 = postprocess_algcoef(av,alist,r0);
4955: MKLIST(*rp,r0);
4956: }
4957:
4958: NDV recompute_trace(NODE trace,NDV *p,int m);
4959: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4960:
4961: NDV recompute_trace(NODE ti,NDV *p,int mod)
4962: {
4963: int c,c1,c2,i;
4964: NM mul,m,tail;
4965: ND d,r,rm;
4966: NODE sj;
4967: NDV red;
4968: Obj mj;
4969:
4970: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4971: CM(mul) = 1;
4972: tail = 0;
4973: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4974: sj = BDY((LIST)BDY(ti));
4975: if ( ARG0(sj) ) {
1.6 noro 4976: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4977: mj = (Obj)ARG2(sj);
4978: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4979: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4980: rm = ndv_mul_nm(mod,mul,red);
4981: if ( !r ) r = rm;
4982: else {
4983: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4984: if ( d ) {
4985: NEXT(tail) = m; tail = m; LEN(d)++;
4986: } else {
4987: MKND(nd_nvar,m,1,d); tail = BDY(d);
4988: }
4989: }
4990: if ( !m ) return 0; /* failure */
4991: else {
4992: BDY(r) = m;
4993: if ( mod > 0 || mod == -1 ) {
4994: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4995: DMAR(c1,c2,0,mod,c);
4996: nd_mul_c(mod,rm,c);
4997: } else {
4998: Z t,u;
4999:
5000: chsgnlf(HCZ(r),&t);
5001: divlf(t,HCZ(rm),&u);
5002: nd_mul_c_lf(rm,u);
5003: }
5004: r = nd_add(mod,r,rm);
5005: }
5006: }
5007: }
5008: }
5009: if ( tail ) NEXT(tail) = 0;
5010: d = nd_add(mod,d,r);
5011: nd_mul_c(mod,d,invm(HCM(d),mod));
5012: return ndtondv(mod,d);
5013: }
5014:
5015: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
5016: {
5017: VL tv,fv,vv,vc,av;
5018: NODE fd,fd0,r,r0,t,x,s,xx,alist;
5019: int e,max,nvar,i;
5020: NDV b;
5021: int ishomo,nalg;
5022: Alg alpha,dp;
5023: P p,zp;
5024: Q dmy;
5025: LIST f1,f2;
5026: Obj obj;
5027: NumberField nf;
5028: struct order_spec *ord1;
5029: NODE permtrace,intred,ind,perm,trace,ti;
5030: int len,n,j;
5031: NDV *db,*pb;
5032:
5033: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 5034: parse_nd_option(vv,current_option);
1.1 noro 5035: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5036: switch ( ord->id ) {
5037: case 1:
5038: if ( ord->nv != nvar )
5039: error("nd_check : invalid order specification");
5040: break;
5041: default:
5042: break;
5043: }
5044: nd_init_ord(ord);
1.6 noro 5045: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 5046: nd_setup_parameters(nvar,0);
5047:
5048: len = length(BDY(f));
5049: db = (NDV *)MALLOC(len*sizeof(NDV *));
5050: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
5051: ptozp((P)BDY(t),1,&dmy,&zp);
5052: b = ptondv(CO,vv,zp);
5053: ndv_mod(m,b);
5054: ndv_mul_c(m,b,invm(HCM(b),m));
5055: db[i] = b;
5056: }
5057:
5058: permtrace = BDY((LIST)ARG2(BDY(tlist)));
5059: intred = BDY((LIST)ARG3(BDY(tlist)));
5060: ind = BDY((LIST)ARG4(BDY(tlist)));
5061: perm = BDY((LIST)ARG0(permtrace));
5062: trace = NEXT(permtrace);
5063:
5064: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 5065: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 5066: if ( j > i ) i = j;
5067: }
5068: n = i+1;
5069: pb = (NDV *)MALLOC(n*sizeof(NDV *));
5070: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
5071: ti = BDY((LIST)BDY(t));
1.6 noro 5072: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 5073: }
5074: for ( t = trace; t; t = NEXT(t) ) {
5075: ti = BDY((LIST)BDY(t));
1.6 noro 5076: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5077: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5078: if ( DP_Print ) {
5079: fprintf(asir_out,"."); fflush(asir_out);
5080: }
5081: }
5082: for ( t = intred; t; t = NEXT(t) ) {
5083: ti = BDY((LIST)BDY(t));
1.6 noro 5084: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
5085: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 5086: if ( DP_Print ) {
5087: fprintf(asir_out,"*"); fflush(asir_out);
5088: }
5089: }
5090: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
5091: NEXTNODE(r0,r);
1.6 noro 5092: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 5093: ndv_mul_c(m,b,invm(HCM(b),m));
5094: #if 0
1.6 noro 5095: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5096: #else
1.6 noro 5097: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 5098: #endif
5099: }
5100: if ( r0 ) NEXT(r) = 0;
5101: MKLIST(*rp,r0);
5102: if ( DP_Print ) fprintf(asir_out,"\n");
5103: }
5104:
1.16 noro 5105: 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 5106: {
5107: VL tv,fv,vv,vc,av;
5108: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
5109: int m,nocheck,nvar,mindex,e,max;
5110: NDV c;
5111: NMV a;
5112: P p,zp;
5113: Q dmy;
5114: EPOS oepos;
5115: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
5116: Alg alpha,dp;
5117: P poly;
5118: LIST f1,f2,zpl;
5119: Obj obj;
5120: NumberField nf;
5121: struct order_spec *ord1;
5122: struct oEGT eg_check,eg0,eg1;
5123: NODE tr,tl1,tl2,tl3,tl4;
5124: LIST l1,l2,l3,l4,l5;
5125: int *perm;
5126: int j,ret;
5127: Z jq,bpe;
1.15 noro 5128: VECT hvect;
1.1 noro 5129:
1.41 noro 5130: NcriB = NcriMF = Ncri2 = 0;
1.1 noro 5131: nd_module = 0;
5132: nd_lf = 0;
1.46 ! noro 5133: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
! 5134: parse_nd_option(vv,current_option);
1.1 noro 5135: if ( nd_lf ) {
5136: if ( f4 )
5137: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
5138: else
5139: error("nd_gr_trace is not implemented yet over a large finite field");
5140: return;
5141: }
5142: if ( DP_Multiple )
5143: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
5144:
5145: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5146: switch ( ord->id ) {
5147: case 1:
5148: if ( ord->nv != nvar )
5149: error("nd_gr_trace : invalid order specification");
5150: break;
5151: default:
5152: break;
5153: }
5154:
5155: get_algtree((Obj)f,&av);
5156: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
5157: nd_ntrans = nvar;
5158: nd_nalg = nalg;
5159: /* #i -> t#i */
5160: if ( nalg ) {
5161: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
5162: ord = ord1;
5163: f = f1;
5164: }
5165: nvar += nalg;
5166:
5167: nocheck = 0;
5168: mindex = 0;
5169:
5170: if ( Demand ) nd_demand = 1;
5171: else nd_demand = 0;
5172:
5173: /* setup modulus */
5174: if ( trace < 0 ) {
5175: trace = -trace;
5176: nocheck = 1;
5177: }
5178: m = trace > 1 ? trace : get_lprime(mindex);
5179: nd_init_ord(ord);
5180: mrank = 0;
5181: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
5182: for ( tv = vv; tv; tv = NEXT(tv) ) {
5183: if ( nd_module ) {
1.16 noro 5184: if ( OID(BDY(t)) == O_DPM ) {
5185: e = dpm_getdeg((DPM)BDY(t),&trank);
5186: max = MAX(e,max);
5187: mrank = MAX(mrank,trank);
5188: } else {
1.1 noro 5189: s = BDY((LIST)BDY(t));
5190: trank = length(s);
5191: mrank = MAX(mrank,trank);
5192: for ( ; s; s = NEXT(s) ) {
5193: e = getdeg(tv->v,(P)BDY(s));
5194: max = MAX(e,max);
5195: }
1.16 noro 5196: }
1.1 noro 5197: } else {
1.43 noro 5198: if ( OID(BDY(t)) == O_DP ) {
5199: e = dp_getdeg((DP)BDY(t));
5200: max = MAX(e,max);
5201: } else {
1.1 noro 5202: e = getdeg(tv->v,(P)BDY(t));
5203: max = MAX(e,max);
1.43 noro 5204: }
1.1 noro 5205: }
5206: }
5207: nd_setup_parameters(nvar,max);
5208: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
5209: ishomo = 1;
5210: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
5211: if ( nd_module ) {
1.16 noro 5212: if ( OID(BDY(t)) == O_DPM ) {
5213: Z cont;
5214: DPM zdpm;
5215:
1.17 noro 5216: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 5217: else zdpm = (DPM)BDY(t);
5218: c = (pointer)dpmtondv(m,zdpm);
5219: } else {
5220: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
5221: else zpl = (LIST)BDY(t);
1.1 noro 5222: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 5223: }
1.1 noro 5224: } else {
1.43 noro 5225: if ( OID(BDY(t)) == O_DP ) {
5226: DP zdp;
5227:
5228: if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
5229: else zdp = (DP)BDY(t);
5230: c = (pointer)dptondv(m,zdp);
5231: } else {
5232: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
5233: else zp = (P)BDY(t);
5234: c = (pointer)ptondv(CO,vv,zp);
5235: }
1.1 noro 5236: }
5237: if ( ishomo )
5238: ishomo = ishomo && ndv_ishomo(c);
5239: if ( c ) {
5240: NEXTNODE(in0,in); BDY(in) = (pointer)c;
5241: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
5242: }
5243: }
5244: if ( in0 ) NEXT(in) = 0;
5245: if ( fd0 ) NEXT(fd) = 0;
5246: if ( !ishomo && homo ) {
5247: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
5248: c = (NDV)BDY(t); len = LEN(c);
5249: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
5250: wmax = MAX(TD(DL(a)),wmax);
5251: }
5252: homogenize_order(ord,nvar,&ord1);
5253: nd_init_ord(ord1);
5254: nd_setup_parameters(nvar+1,wmax);
5255: for ( t = fd0; t; t = NEXT(t) )
5256: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
5257: }
5258: if ( MaxDeg > 0 ) nocheck = 1;
5259: while ( 1 ) {
5260: tl1 = tl2 = tl3 = tl4 = 0;
5261: if ( Demand )
5262: nd_demand = 1;
1.24 noro 5263: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 5264: if ( nd_gentrace ) {
5265: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
5266: }
5267: if ( ret )
5268: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
5269: if ( !ret || !cand ) {
5270: /* failure */
5271: if ( trace > 1 ) { *rp = 0; return; }
5272: else m = get_lprime(++mindex);
5273: continue;
5274: }
1.15 noro 5275: if ( nd_gentrace ) {
5276: MKVECT(hvect,nd_psn);
5277: for ( i = 0; i < nd_psn; i++ )
5278: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
5279: }
1.1 noro 5280: if ( !ishomo && homo ) {
5281: /* dehomogenization */
5282: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
5283: nd_init_ord(ord);
5284: nd_setup_parameters(nvar,0);
5285: }
5286: nd_demand = 0;
5287: cand = ndv_reducebase(cand,perm);
5288: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
5289: cand = ndv_reduceall(0,cand);
5290: cbpe = nd_bpe;
5291: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
5292: get_eg(&eg0);
5293: if ( nocheck )
5294: break;
5295: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
5296: if ( nd_gentrace ) {
5297: tl3 = nd_alltracelist; nd_alltracelist = 0;
5298: } else tl3 = 0;
5299: /* gbcheck : cand is a GB of Id(cand) ? */
5300: if ( nd_vc || nd_gentrace || nd_gensyz )
5301: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
5302: else
5303: ret = nd_f4(0,1,0)!=0;
5304: if ( nd_gentrace && nd_gensyz ) {
5305: tl4 = nd_alltracelist; nd_alltracelist = 0;
5306: } else tl4 = 0;
5307: }
5308: if ( ret ) break;
5309: else if ( trace > 1 ) {
5310: /* failure */
5311: *rp = 0; return;
5312: } else {
5313: /* try the next modulus */
5314: m = get_lprime(++mindex);
5315: /* reset the parameters */
5316: if ( !ishomo && homo ) {
5317: nd_init_ord(ord1);
5318: nd_setup_parameters(nvar+1,wmax);
5319: } else {
5320: nd_init_ord(ord);
5321: nd_setup_parameters(nvar,max);
5322: }
5323: }
5324: }
5325: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
5326: if ( DP_Print )
1.6 noro 5327: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 5328: /* dp->p */
5329: nd_bpe = cbpe;
5330: nd_setup_parameters(nd_nvar,0);
5331: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 5332: if ( nd_module ) {
1.17 noro 5333: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 5334: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 5335: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
5336: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 5337: }
5338: if ( nd_nalg )
5339: cand = postprocess_algcoef(av,alist,cand);
5340: MKLIST(*rp,cand);
5341: if ( nd_gentrace ) {
5342: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
5343: tl3 = reverse_node(tl3);
5344: /* tl2 = [[i,[[*,j,*,*],...]],...] */
5345: for ( t = tl2; t; t = NEXT(t) ) {
5346: /* s = [i,[*,j,*,*],...] */
5347: s = BDY((LIST)BDY(t));
1.6 noro 5348: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 5349: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 5350: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 5351: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
5352: }
5353: }
5354: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 5355: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 5356: }
5357: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
5358: MKLIST(l5,tl4);
1.6 noro 5359: STOZ(nd_bpe,bpe);
1.15 noro 5360: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 5361: }
5362: }
5363:
5364: /* XXX : module element is not considered */
5365:
5366: void dltondl(int n,DL dl,UINT *r)
5367: {
5368: UINT *d;
5369: int i,j,l,s,ord_l;
5370: struct order_pair *op;
5371:
5372: d = (unsigned int *)dl->d;
5373: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
5374: if ( nd_blockmask ) {
5375: l = nd_blockmask->n;
5376: op = nd_blockmask->order_pair;
5377: for ( j = 0, s = 0; j < l; j++ ) {
5378: ord_l = op[j].length;
5379: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
5380: }
5381: TD(r) = ndl_weight(r);
5382: ndl_weight_mask(r);
5383: } else {
5384: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
5385: TD(r) = ndl_weight(r);
5386: }
5387: }
5388:
5389: DL ndltodl(int n,UINT *ndl)
5390: {
5391: DL dl;
5392: int *d;
5393: int i,j,l,s,ord_l;
5394: struct order_pair *op;
5395:
1.33 noro 5396: NEWDL_NOINIT(dl,n);
1.1 noro 5397: dl->td = TD(ndl);
5398: d = dl->d;
5399: if ( nd_blockmask ) {
5400: l = nd_blockmask->n;
5401: op = nd_blockmask->order_pair;
5402: for ( j = 0, s = 0; j < l; j++ ) {
5403: ord_l = op[j].length;
5404: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5405: }
5406: } else {
5407: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5408: }
5409: return dl;
5410: }
5411:
1.24 noro 5412: void _ndltodl(UINT *ndl,DL dl)
5413: {
5414: int *d;
5415: int i,j,l,s,ord_l,n;
5416: struct order_pair *op;
5417:
5418: n = nd_nvar;
5419: dl->td = TD(ndl);
5420: d = dl->d;
5421: if ( nd_blockmask ) {
5422: l = nd_blockmask->n;
5423: op = nd_blockmask->order_pair;
5424: for ( j = 0, s = 0; j < l; j++ ) {
5425: ord_l = op[j].length;
5426: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
5427: }
5428: } else {
5429: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
5430: }
5431: }
5432:
1.1 noro 5433: void nmtodp(int mod,NM m,DP *r)
5434: {
5435: DP dp;
5436: MP mr;
5437:
5438: NEWMP(mr);
5439: mr->dl = ndltodl(nd_nvar,DL(m));
5440: mr->c = (Obj)ndctop(mod,m->c);
5441: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5442: *r = dp;
5443: }
5444:
1.15 noro 5445: void ndltodp(UINT *d,DP *r)
5446: {
5447: DP dp;
5448: MP mr;
5449:
5450: NEWMP(mr);
5451: mr->dl = ndltodl(nd_nvar,d);
5452: mr->c = (Obj)ONE;
5453: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
5454: *r = dp;
5455: }
5456:
1.1 noro 5457: void ndl_print(UINT *dl)
5458: {
5459: int n;
5460: int i,j,l,ord_l,s,s0;
5461: struct order_pair *op;
5462:
5463: n = nd_nvar;
5464: printf("<<");
5465: if ( nd_blockmask ) {
5466: l = nd_blockmask->n;
5467: op = nd_blockmask->order_pair;
5468: for ( j = 0, s = s0 = 0; j < l; j++ ) {
5469: ord_l = op[j].length;
5470: for ( i = 0; i < ord_l; i++, s++ )
5471: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
5472: }
5473: } else {
5474: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
5475: }
5476: printf(">>");
5477: if ( nd_module && MPOS(dl) )
5478: printf("*e%d",MPOS(dl));
5479: }
5480:
5481: void nd_print(ND p)
5482: {
5483: NM m;
5484:
5485: if ( !p )
5486: printf("0\n");
5487: else {
5488: for ( m = BDY(p); m; m = NEXT(m) ) {
5489: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 5490: else printf("+%ld*",CM(m));
1.1 noro 5491: ndl_print(DL(m));
5492: }
5493: printf("\n");
5494: }
5495: }
5496:
5497: void nd_print_q(ND p)
5498: {
5499: NM m;
5500:
5501: if ( !p )
5502: printf("0\n");
5503: else {
5504: for ( m = BDY(p); m; m = NEXT(m) ) {
5505: printf("+");
1.6 noro 5506: printexpr(CO,(Obj)CZ(m));
1.1 noro 5507: printf("*");
5508: ndl_print(DL(m));
5509: }
5510: printf("\n");
5511: }
5512: }
5513:
5514: void ndp_print(ND_pairs d)
5515: {
5516: ND_pairs t;
5517:
5518: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
5519: printf("\n");
5520: }
5521:
5522: void nd_removecont(int mod,ND p)
5523: {
5524: int i,n;
5525: Z *w;
5526: NM m;
5527: struct oVECT v;
5528:
5529: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5530: else if ( mod == -2 ) {
5531: Z inv;
5532: divlf(ONE,HCZ(p),&inv);
5533: nd_mul_c_lf(p,inv);
5534: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5535: else {
5536: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5537: w = (Z *)MALLOC(n*sizeof(Q));
5538: v.len = n;
5539: v.body = (pointer *)w;
1.6 noro 5540: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5541: removecont_array((P *)w,n,1);
1.6 noro 5542: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5543: }
5544: }
5545:
5546: void nd_removecont2(ND p1,ND p2)
5547: {
5548: int i,n1,n2,n;
5549: Z *w;
5550: NM m;
5551: struct oVECT v;
5552:
5553: n1 = nd_length(p1);
5554: n2 = nd_length(p2);
5555: n = n1+n2;
5556: w = (Z *)MALLOC(n*sizeof(Q));
5557: v.len = n;
5558: v.body = (pointer *)w;
5559: i = 0;
5560: if ( p1 )
1.6 noro 5561: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5562: if ( p2 )
1.6 noro 5563: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5564: removecont_array((P *)w,n,1);
5565: i = 0;
5566: if ( p1 )
1.6 noro 5567: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5568: if ( p2 )
1.6 noro 5569: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5570: }
5571:
5572: void ndv_removecont(int mod,NDV p)
5573: {
5574: int i,len,all_p;
5575: Z *c;
5576: P *w;
5577: Z dvr,t;
5578: P g,cont,tp;
5579: NMV m;
5580:
5581: if ( mod == -1 )
5582: ndv_mul_c(mod,p,_invsf(HCM(p)));
5583: else if ( mod == -2 ) {
5584: Z inv;
5585: divlf(ONE,HCZ(p),&inv);
5586: ndv_mul_c_lf(p,inv);
5587: } else if ( mod )
5588: ndv_mul_c(mod,p,invm(HCM(p),mod));
5589: else {
5590: len = p->len;
5591: w = (P *)MALLOC(len*sizeof(P));
5592: c = (Z *)MALLOC(len*sizeof(Q));
5593: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5594: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5595: all_p = all_p && !NUM(w[i]);
5596: }
5597: if ( all_p ) {
5598: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5599: mulp(nd_vc,(P)dvr,g,&cont);
5600: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5601: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5602: }
5603: } else {
5604: sortbynm((Q *)c,len);
5605: qltozl((Q *)c,len,&dvr);
5606: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5607: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5608: }
5609: }
5610: }
5611: }
5612:
5613: /* koko */
5614:
5615: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5616: {
5617: int len,i,max;
5618: NMV m,mr0,mr,t;
5619:
5620: len = p->len;
1.14 noro 5621: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5622: max = MAX(max,TD(DL(m)));
5623: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5624: m = (NMV)((char *)mr0+(len-1)*oadv);
5625: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5626: t = (NMV)MALLOC(nmv_adv);
5627: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5628: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5629: CZ(mr) = CZ(m);
1.1 noro 5630: ndl_copy(DL(t),DL(mr));
5631: }
5632: NV(p)++;
5633: BDY(p) = mr0;
5634: }
5635:
5636: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5637: {
5638: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5639: int pos;
5640: Q *w;
5641: Q dvr,t;
5642: NMV m,r;
5643:
5644: len = p->len;
5645: newnvar = nd_nvar-1;
5646: newexporigin = nd_get_exporigin(ord);
5647: if ( nd_module ) newmpos = newexporigin-1;
5648: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5649: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5650: ndl_dehomogenize(DL(m));
5651: if ( newwpd != nd_wpd ) {
5652: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5653: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5654: CZ(r) = CZ(m);
1.1 noro 5655: if ( nd_module ) pos = MPOS(DL(m));
5656: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5657: adj = nd_exporigin-newexporigin;
5658: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5659: if ( nd_module ) {
5660: DL(r)[newmpos] = pos;
5661: }
5662: }
5663: }
5664: NV(p)--;
5665: }
5666:
5667: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5668: {
5669: int i;
5670: P *tpl,*tpl1;
5671: NODE l;
5672: P h,gcd,t;
5673:
5674: tpl = (P *)MALLOC(m*sizeof(P));
5675: tpl1 = (P *)MALLOC(m*sizeof(P));
5676: bcopy(pl,tpl,m*sizeof(P));
5677: gcd = (P)ONE;
5678: for ( l = nd_hcf; l; l = NEXT(l) ) {
5679: h = (P)BDY(l);
5680: while ( 1 ) {
5681: for ( i = 0; i < m; i++ )
5682: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5683: break;
5684: if ( i == m ) {
5685: bcopy(tpl1,tpl,m*sizeof(P));
5686: mulp(vl,gcd,h,&t); gcd = t;
5687: } else
5688: break;
5689: }
5690: }
5691: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5692: if ( full ) {
5693: heu_nezgcdnpz(vl,tpl,m,&t);
5694: mulp(vl,gcd,t,pr);
5695: } else
5696: *pr = gcd;
5697: }
5698:
5699: void removecont_array(P *p,int n,int full)
5700: {
5701: int all_p,all_q,i;
5702: Z *c;
5703: P *w;
5704: P t,s;
5705:
5706: for ( all_q = 1, i = 0; i < n; i++ )
5707: all_q = all_q && NUM(p[i]);
5708: if ( all_q ) {
5709: removecont_array_q((Z *)p,n);
5710: } else {
5711: c = (Z *)MALLOC(n*sizeof(Z));
5712: w = (P *)MALLOC(n*sizeof(P));
5713: for ( i = 0; i < n; i++ ) {
5714: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5715: }
5716: removecont_array_q(c,n);
5717: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5718: for ( i = 0; i < n; i++ ) {
5719: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5720: }
5721: }
5722: }
5723:
5724: /* c is an int array */
5725:
5726: void removecont_array_q(Z *c,int n)
5727: {
5728: struct oVECT v;
5729: Z d0,d1,a,u,u1,gcd;
5730: int i,j;
5731: Z *q,*r;
5732:
5733: q = (Z *)MALLOC(n*sizeof(Z));
5734: r = (Z *)MALLOC(n*sizeof(Z));
5735: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5736: gcdvz_estimate(&v,&d0);
5737: for ( i = 0; i < n; i++ ) {
5738: divqrz(c[i],d0,&q[i],&r[i]);
5739: }
5740: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5741: if ( i < n ) {
5742: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5743: gcdvz(&v,&d1);
5744: gcdz(d0,d1,&gcd);
1.6 noro 5745: /* exact division */
5746: divsz(d0,gcd,&a);
1.1 noro 5747: for ( i = 0; i < n; i++ ) {
5748: mulz(a,q[i],&u);
5749: if ( r[i] ) {
1.6 noro 5750: /* exact division */
5751: divsz(r[i],gcd,&u1);
1.1 noro 5752: addz(u,u1,&q[i]);
5753: } else
5754: q[i] = u;
5755: }
5756: }
5757: for ( i = 0; i < n; i++ ) c[i] = q[i];
5758: }
5759:
1.4 noro 5760: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5761:
5762: void mpz_removecont_array(mpz_t *c,int n)
5763: {
5764: mpz_t d0,a,u,u1,gcd;
5765: int i,j;
1.13 noro 5766: static mpz_t *q,*r;
5767: static int c_len = 0;
1.4 noro 5768:
5769: for ( i = 0; i < n; i++ )
5770: if ( mpz_sgn(c[i]) ) break;
5771: if ( i == n ) return;
5772: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5773: if ( n > c_len ) {
5774: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5775: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5776: c_len = n;
5777: }
1.4 noro 5778: for ( i = 0; i < n; i++ ) {
5779: mpz_init(q[i]); mpz_init(r[i]);
5780: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5781: }
5782: for ( i = 0; i < n; i++ )
5783: if ( mpz_sgn(r[i]) ) break;
5784: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5785: if ( i < n ) {
5786: mpz_gcd(gcd,d0,r[i]);
5787: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5788: mpz_div(a,d0,gcd);
5789: for ( i = 0; i < n; i++ ) {
5790: mpz_mul(u,a,q[i]);
5791: if ( mpz_sgn(r[i]) ) {
5792: mpz_div(u1,r[i],gcd);
5793: mpz_add(q[i],u,u1);
5794: } else
5795: mpz_set(q[i],u);
5796: }
5797: }
5798: for ( i = 0; i < n; i++ )
5799: mpz_set(c[i],q[i]);
5800: }
5801:
1.1 noro 5802: void nd_mul_c(int mod,ND p,int mul)
5803: {
5804: NM m;
5805: int c,c1;
5806:
5807: if ( !p ) return;
5808: if ( mul == 1 ) return;
5809: if ( mod == -1 )
5810: for ( m = BDY(p); m; m = NEXT(m) )
5811: CM(m) = _mulsf(CM(m),mul);
5812: else
5813: for ( m = BDY(p); m; m = NEXT(m) ) {
5814: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5815: }
5816: }
5817:
5818: void nd_mul_c_lf(ND p,Z mul)
5819: {
5820: NM m;
5821: Z c;
5822:
5823: if ( !p ) return;
5824: if ( UNIZ(mul) ) return;
5825: for ( m = BDY(p); m; m = NEXT(m) ) {
5826: mullf(CZ(m),mul,&c); CZ(m) = c;
5827: }
5828: }
5829:
5830: void nd_mul_c_q(ND p,P mul)
5831: {
5832: NM m;
5833: P c;
5834:
5835: if ( !p ) return;
5836: if ( UNIQ(mul) ) return;
5837: for ( m = BDY(p); m; m = NEXT(m) ) {
5838: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5839: }
5840: }
5841:
5842: void nd_mul_c_p(VL vl,ND p,P mul)
5843: {
5844: NM m;
5845: P c;
5846:
5847: if ( !p ) return;
5848: for ( m = BDY(p); m; m = NEXT(m) ) {
5849: mulp(vl,CP(m),mul,&c); CP(m) = c;
5850: }
5851: }
5852:
5853: void nd_free(ND p)
5854: {
5855: NM t,s;
5856:
5857: if ( !p ) return;
5858: t = BDY(p);
5859: while ( t ) {
5860: s = NEXT(t);
5861: FREENM(t);
5862: t = s;
5863: }
5864: FREEND(p);
5865: }
5866:
5867: void ndv_free(NDV p)
5868: {
5869: GCFREE(BDY(p));
5870: }
5871:
5872: void nd_append_red(UINT *d,int i)
5873: {
5874: RHist m,m0;
5875: int h;
5876:
5877: NEWRHist(m);
5878: h = ndl_hash_value(d);
5879: m->index = i;
5880: ndl_copy(d,DL(m));
5881: NEXT(m) = nd_red[h];
5882: nd_red[h] = m;
5883: }
5884:
5885: UINT *ndv_compute_bound(NDV p)
5886: {
5887: UINT *d1,*d2,*t;
5888: UINT u;
5889: int i,j,k,l,len,ind;
5890: NMV m;
5891:
5892: if ( !p )
5893: return 0;
5894: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5895: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5896: len = LEN(p);
5897: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5898: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5899: ndl_max(DL(m),d1,d2);
5900: t = d1; d1 = d2; d2 = t;
5901: }
5902: l = nd_nvar+31;
5903: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5904: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5905: u = d1[i];
5906: k = (nd_epw-1)*nd_bpe;
5907: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5908: t[ind] = (u>>k)&nd_mask0;
5909: }
5910: for ( ; ind < l; ind++ ) t[ind] = 0;
5911: return t;
5912: }
5913:
5914: UINT *nd_compute_bound(ND p)
5915: {
5916: UINT *d1,*d2,*t;
5917: UINT u;
5918: int i,j,k,l,len,ind;
5919: NM m;
5920:
5921: if ( !p )
5922: return 0;
5923: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5924: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5925: len = LEN(p);
5926: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5927: for ( m = NEXT(m); m; m = NEXT(m) ) {
5928: ndl_lcm(DL(m),d1,d2);
5929: t = d1; d1 = d2; d2 = t;
5930: }
5931: l = nd_nvar+31;
5932: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5933: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5934: u = d1[i];
5935: k = (nd_epw-1)*nd_bpe;
5936: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5937: t[ind] = (u>>k)&nd_mask0;
5938: }
5939: for ( ; ind < l; ind++ ) t[ind] = 0;
5940: return t;
5941: }
5942:
5943: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5944: /* of a term. In this case we need additional 1 word. */
5945:
5946: int nd_get_exporigin(struct order_spec *ord)
5947: {
5948: switch ( ord->id ) {
1.21 noro 5949: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5950: return 1+nd_module;
5951: case 1: case 257:
5952: /* block order */
5953: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5954: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5955: return ord->ord.block.length+1+nd_module;
5956: case 3: case 259:
5957: #if 0
5958: error("nd_get_exporigin : composite order is not supported yet.");
5959: #else
5960: return 1+nd_module;
5961: #endif
5962: default:
5963: error("nd_get_exporigin : ivalid argument.");
5964: return 0;
5965: }
5966: }
5967:
5968: void nd_setup_parameters(int nvar,int max) {
5969: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5970: struct order_pair *op;
1.45 noro 5971: extern int CNVars;
1.1 noro 5972:
5973: nd_nvar = nvar;
1.45 noro 5974: CNVars = nvar;
1.1 noro 5975: if ( max ) {
5976: /* XXX */
5977: if ( do_weyl ) nd_bpe = 32;
5978: else if ( max < 2 ) nd_bpe = 1;
5979: else if ( max < 4 ) nd_bpe = 2;
5980: else if ( max < 8 ) nd_bpe = 3;
5981: else if ( max < 16 ) nd_bpe = 4;
5982: else if ( max < 32 ) nd_bpe = 5;
5983: else if ( max < 64 ) nd_bpe = 6;
5984: else if ( max < 256 ) nd_bpe = 8;
5985: else if ( max < 1024 ) nd_bpe = 10;
5986: else if ( max < 65536 ) nd_bpe = 16;
5987: else nd_bpe = 32;
5988: }
5989: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5990: UINT t;
5991: int st;
5992: int *v;
5993: /* t = max(weights) */
5994: t = 0;
5995: if ( current_dl_weight_vector )
5996: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5997: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5998: if ( t < st ) t = st;
5999: }
6000: if ( nd_matrix )
6001: for ( i = 0; i < nd_matrix_len; i++ )
6002: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
6003: if ( (st=v[j]) < 0 ) st = -st;
6004: if ( t < st ) t = st;
6005: }
6006: /* i = bitsize of t */
6007: for ( i = 0; t; t >>=1, i++ );
6008: /* i += bitsize of nd_nvar */
6009: for ( t = nd_nvar; t; t >>=1, i++);
6010: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
6011: if ( (nd_bpe+i) >= 31 )
6012: error("nd_setup_parameters : too large weight");
6013: }
6014: nd_epw = (sizeof(UINT)*8)/nd_bpe;
6015: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
6016: nd_exporigin = nd_get_exporigin(nd_ord);
6017: wpd = nd_exporigin+elen;
6018: if ( nd_module )
6019: nd_mpos = nd_exporigin-1;
6020: else
6021: nd_mpos = -1;
6022: if ( wpd != nd_wpd ) {
6023: nd_free_private_storage();
6024: nd_wpd = wpd;
6025: }
6026: if ( nd_bpe < 32 ) {
6027: nd_mask0 = (1<<nd_bpe)-1;
6028: } else {
6029: nd_mask0 = 0xffffffff;
6030: }
6031: bzero(nd_mask,sizeof(nd_mask));
6032: nd_mask1 = 0;
6033: for ( i = 0; i < nd_epw; i++ ) {
6034: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
6035: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
6036: }
6037: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
6038: nd_epos = nd_create_epos(nd_ord);
6039: nd_blockmask = nd_create_blockmask(nd_ord);
6040: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
6041: }
6042:
6043: ND_pairs nd_reconstruct(int trace,ND_pairs d)
6044: {
6045: int i,obpe,oadv,h;
6046: static NM prev_nm_free_list;
6047: static ND_pairs prev_ndp_free_list;
6048: RHist mr0,mr;
6049: RHist r;
6050: RHist *old_red;
6051: ND_pairs s0,s,t;
6052: EPOS oepos;
6053:
6054: obpe = nd_bpe;
6055: oadv = nmv_adv;
6056: oepos = nd_epos;
6057: if ( obpe < 2 ) nd_bpe = 2;
6058: else if ( obpe < 3 ) nd_bpe = 3;
6059: else if ( obpe < 4 ) nd_bpe = 4;
6060: else if ( obpe < 5 ) nd_bpe = 5;
6061: else if ( obpe < 6 ) nd_bpe = 6;
6062: else if ( obpe < 8 ) nd_bpe = 8;
6063: else if ( obpe < 10 ) nd_bpe = 10;
6064: else if ( obpe < 16 ) nd_bpe = 16;
6065: else if ( obpe < 32 ) nd_bpe = 32;
6066: else error("nd_reconstruct : exponent too large");
6067:
6068: nd_setup_parameters(nd_nvar,0);
6069: prev_nm_free_list = _nm_free_list;
6070: prev_ndp_free_list = _ndp_free_list;
6071: _nm_free_list = 0;
6072: _ndp_free_list = 0;
6073: for ( i = nd_psn-1; i >= 0; i-- ) {
6074: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6075: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6076: }
6077: if ( trace )
6078: for ( i = nd_psn-1; i >= 0; i-- ) {
6079: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6080: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6081: }
6082: s0 = 0;
6083: for ( t = d; t; t = NEXT(t) ) {
6084: NEXTND_pairs(s0,s);
6085: s->i1 = t->i1;
6086: s->i2 = t->i2;
1.24 noro 6087: s->sig = t->sig;
1.1 noro 6088: SG(s) = SG(t);
6089: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6090: }
6091:
6092: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6093: for ( i = 0; i < REDTAB_LEN; i++ ) {
6094: old_red[i] = nd_red[i];
6095: nd_red[i] = 0;
6096: }
6097: for ( i = 0; i < REDTAB_LEN; i++ )
6098: for ( r = old_red[i]; r; r = NEXT(r) ) {
6099: NEWRHist(mr);
6100: mr->index = r->index;
6101: SG(mr) = SG(r);
6102: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6103: h = ndl_hash_value(DL(mr));
6104: NEXT(mr) = nd_red[h];
6105: nd_red[h] = mr;
1.24 noro 6106: mr->sig = r->sig;
1.1 noro 6107: }
6108: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6109: old_red = 0;
6110: for ( i = 0; i < nd_psn; i++ ) {
6111: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6112: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 6113: r->sig = nd_psh[i]->sig;
1.1 noro 6114: nd_psh[i] = r;
6115: }
6116: if ( s0 ) NEXT(s) = 0;
6117: prev_nm_free_list = 0;
6118: prev_ndp_free_list = 0;
6119: #if 0
6120: GC_gcollect();
6121: #endif
6122: return s0;
6123: }
6124:
1.30 noro 6125: void nd_reconstruct_s(int trace,ND_pairs *d)
6126: {
6127: int i,obpe,oadv,h;
6128: static NM prev_nm_free_list;
6129: static ND_pairs prev_ndp_free_list;
6130: RHist mr0,mr;
6131: RHist r;
6132: RHist *old_red;
6133: ND_pairs s0,s,t;
6134: EPOS oepos;
6135:
6136: obpe = nd_bpe;
6137: oadv = nmv_adv;
6138: oepos = nd_epos;
6139: if ( obpe < 2 ) nd_bpe = 2;
6140: else if ( obpe < 3 ) nd_bpe = 3;
6141: else if ( obpe < 4 ) nd_bpe = 4;
6142: else if ( obpe < 5 ) nd_bpe = 5;
6143: else if ( obpe < 6 ) nd_bpe = 6;
6144: else if ( obpe < 8 ) nd_bpe = 8;
6145: else if ( obpe < 10 ) nd_bpe = 10;
6146: else if ( obpe < 16 ) nd_bpe = 16;
6147: else if ( obpe < 32 ) nd_bpe = 32;
6148: else error("nd_reconstruct_s : exponent too large");
6149:
6150: nd_setup_parameters(nd_nvar,0);
6151: prev_nm_free_list = _nm_free_list;
6152: prev_ndp_free_list = _ndp_free_list;
6153: _nm_free_list = 0;
6154: _ndp_free_list = 0;
6155: for ( i = nd_psn-1; i >= 0; i-- ) {
6156: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
6157: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
6158: }
6159: if ( trace )
6160: for ( i = nd_psn-1; i >= 0; i-- ) {
6161: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
6162: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
6163: }
6164:
6165: for ( i = 0; i < nd_nbase; i++ ) {
6166: s0 = 0;
6167: for ( t = d[i]; t; t = NEXT(t) ) {
6168: NEXTND_pairs(s0,s);
6169: s->i1 = t->i1;
6170: s->i2 = t->i2;
6171: s->sig = t->sig;
6172: SG(s) = SG(t);
6173: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
6174: }
6175: d[i] = s0;
6176: }
6177:
6178: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
6179: for ( i = 0; i < REDTAB_LEN; i++ ) {
6180: old_red[i] = nd_red[i];
6181: nd_red[i] = 0;
6182: }
6183: for ( i = 0; i < REDTAB_LEN; i++ )
6184: for ( r = old_red[i]; r; r = NEXT(r) ) {
6185: NEWRHist(mr);
6186: mr->index = r->index;
6187: SG(mr) = SG(r);
6188: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
6189: h = ndl_hash_value(DL(mr));
6190: NEXT(mr) = nd_red[h];
6191: nd_red[h] = mr;
6192: mr->sig = r->sig;
6193: }
6194: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
6195: old_red = 0;
6196: for ( i = 0; i < nd_psn; i++ ) {
6197: NEWRHist(r); SG(r) = SG(nd_psh[i]);
6198: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
6199: r->sig = nd_psh[i]->sig;
6200: nd_psh[i] = r;
6201: }
6202: if ( s0 ) NEXT(s) = 0;
6203: prev_nm_free_list = 0;
6204: prev_ndp_free_list = 0;
6205: #if 0
6206: GC_gcollect();
6207: #endif
6208: }
6209:
1.1 noro 6210: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
6211: {
6212: int n,i,ei,oepw,omask0,j,s,ord_l,l;
6213: struct order_pair *op;
6214:
6215: n = nd_nvar;
6216: oepw = (sizeof(UINT)*8)/obpe;
6217: omask0 = (1<<obpe)-1;
6218: TD(r) = TD(d);
6219: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
6220: if ( nd_blockmask ) {
6221: l = nd_blockmask->n;
6222: op = nd_blockmask->order_pair;
6223: for ( i = 1; i < nd_exporigin; i++ )
6224: r[i] = d[i];
6225: for ( j = 0, s = 0; j < l; j++ ) {
6226: ord_l = op[j].length;
6227: for ( i = 0; i < ord_l; i++, s++ ) {
6228: ei = GET_EXP_OLD(d,s);
6229: PUT_EXP(r,s,ei);
6230: }
6231: }
6232: } else {
6233: for ( i = 0; i < n; i++ ) {
6234: ei = GET_EXP_OLD(d,i);
6235: PUT_EXP(r,i,ei);
6236: }
6237: }
6238: if ( nd_module ) MPOS(r) = MPOS(d);
6239: }
6240:
6241: ND nd_copy(ND p)
6242: {
6243: NM m,mr,mr0;
6244: int c,n;
6245: ND r;
6246:
6247: if ( !p )
6248: return 0;
6249: else {
6250: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
6251: NEXTNM(mr0,mr);
6252: CM(mr) = CM(m);
6253: ndl_copy(DL(m),DL(mr));
6254: }
6255: NEXT(mr) = 0;
6256: MKND(NV(p),mr0,LEN(p),r);
6257: SG(r) = SG(p);
6258: return r;
6259: }
6260: }
6261:
6262: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
6263: {
6264: NM m1,m2;
6265: NDV p1,p2;
6266: ND t1,t2;
6267: UINT *lcm;
6268: P gp,tp;
6269: Z g,t;
6270: Z iq;
6271: int td;
6272: LIST hist;
6273: NODE node;
6274: DP d;
6275:
6276: if ( !mod && nd_demand ) {
6277: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
6278: } else {
6279: if ( trace ) {
6280: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
6281: } else {
6282: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
6283: }
6284: }
6285: lcm = LCM(p);
6286: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
6287: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
6288: FREENM(m1); return 0;
6289: }
6290: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
6291: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
6292: FREENM(m1); FREENM(m2); return 0;
6293: }
6294:
6295: if ( mod == -1 ) {
6296: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
6297: } else if ( mod > 0 ) {
6298: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
6299: } else if ( mod == -2 ) {
6300: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
6301: } else if ( nd_vc ) {
6302: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
6303: divsp(nd_vc,HCP(p2),gp,&CP(m1));
6304: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
6305: } else {
1.6 noro 6306: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 6307: }
6308: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
6309: *rp = nd_add(mod,t1,t2);
6310: if ( nd_gentrace ) {
6311: /* nd_tracelist is initialized */
1.6 noro 6312: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6313: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 6314: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 6315: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
6316: nd_tracelist = node;
6317: }
1.24 noro 6318: if ( *rp )
6319: (*rp)->sig = p->sig;
1.1 noro 6320: FREENM(m1); FREENM(m2);
6321: return 1;
6322: }
6323:
6324: void ndv_mul_c(int mod,NDV p,int mul)
6325: {
6326: NMV m;
6327: int c,c1,len,i;
6328:
6329: if ( !p ) return;
6330: len = LEN(p);
6331: if ( mod == -1 )
6332: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
6333: CM(m) = _mulsf(CM(m),mul);
6334: else
6335: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6336: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
6337: }
6338: }
6339:
6340: void ndv_mul_c_lf(NDV p,Z mul)
6341: {
6342: NMV m;
6343: Z c;
6344: int len,i;
6345:
6346: if ( !p ) return;
6347: len = LEN(p);
6348: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6349: mullf(CZ(m),mul,&c); CZ(m) = c;
6350: }
6351: }
6352:
6353: /* for nd_det */
6354: void ndv_mul_c_q(NDV p,Z mul)
6355: {
6356: NMV m;
6357: Z c;
6358: int len,i;
6359:
6360: if ( !p ) return;
6361: len = LEN(p);
6362: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 6363: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 6364: }
6365: }
6366:
6367: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
6368: int n2,i,j,l,n,tlen;
6369: UINT *d0;
6370: NM *tab,*psum;
6371: ND s,r;
6372: NM t;
6373: NMV m1;
6374:
6375: if ( !p ) return 0;
6376: n = NV(p); n2 = n>>1;
6377: d0 = DL(m0);
6378: l = LEN(p);
6379: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
6380: tab = (NM *)MALLOC(tlen*sizeof(NM));
6381: psum = (NM *)MALLOC(tlen*sizeof(NM));
6382: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
6383: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
6384: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
6385: /* m0(NM) * m1(NMV) => tab(NM) */
6386: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
6387: for ( j = 0; j < tlen; j++ ) {
6388: if ( tab[j] ) {
6389: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
6390: }
6391: }
6392: }
6393: for ( i = tlen-1, r = 0; i >= 0; i-- )
6394: if ( psum[i] ) {
6395: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
6396: MKND(n,psum[i],j,s);
6397: r = nd_add(mod,r,s);
6398: }
6399: if ( r ) SG(r) = SG(p)+TD(d0);
6400: return r;
6401: }
6402:
6403: /* product of monomials */
6404: /* XXX block order is not handled correctly */
6405:
6406: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
6407: {
6408: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
6409: UINT *d0,*d1,*d,*dt,*ctab;
6410: Z *ctab_q;
6411: Z q,q1;
6412: UINT c0,c1,c;
6413: NM *p;
6414: NM m,t;
6415: int mpos;
6416:
6417: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
6418: if ( !m0 || !m1 ) return;
6419: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
6420: if ( nd_module )
6421: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
6422:
6423: NEWNM(m); d = DL(m);
6424: if ( mod ) {
6425: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
6426: } else if ( nd_vc )
6427: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
6428: else
1.6 noro 6429: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 6430: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
6431: homo = n&1 ? 1 : 0;
6432: if ( homo ) {
6433: /* offset of h-degree */
6434: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
6435: PUT_EXP(DL(m),n-1,h);
6436: TD(DL(m)) = h;
6437: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6438: }
6439: tab[0] = m;
6440: NEWNM(m); d = DL(m);
6441: for ( i = 0, curlen = 1; i < n2; i++ ) {
6442: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
6443: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
6444: /* xi^a*(Di^k*xi^l)*Di^b */
6445: a += l; b += k;
6446: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
6447: if ( !k || !l ) {
6448: for ( j = 0; j < curlen; j++ )
6449: if ( (t = tab[j]) != 0 ) {
6450: dt = DL(t);
6451: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
6452: if ( nd_blockmask ) ndl_weight_mask(dt);
6453: }
6454: curlen *= k+1;
6455: continue;
6456: }
6457: min = MIN(k,l);
6458: if ( mod ) {
6459: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
6460: mkwcm(k,l,mod,(int *)ctab);
6461: } else {
6462: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
6463: mkwc(k,l,ctab_q);
6464: }
6465: for ( j = min; j >= 0; j-- ) {
6466: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
6467: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
6468: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
6469: if ( homo ) {
6470: TD(d) = s;
6471: PUT_EXP(d,n-1,s-h);
6472: } else TD(d) = h;
6473: if ( nd_blockmask ) ndl_weight_mask(d);
6474: if ( mod ) c = ctab[j];
6475: else q = ctab_q[j];
6476: p = tab+curlen*j;
6477: if ( j == 0 ) {
6478: for ( u = 0; u < curlen; u++, p++ ) {
6479: if ( tab[u] ) {
6480: ndl_addto(DL(tab[u]),d);
6481: if ( mod ) {
6482: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
6483: } else if ( nd_vc )
6484: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
6485: else {
1.6 noro 6486: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 6487: }
6488: }
6489: }
6490: } else {
6491: for ( u = 0; u < curlen; u++, p++ ) {
6492: if ( tab[u] ) {
6493: NEWNM(t);
6494: ndl_add(DL(tab[u]),d,DL(t));
6495: if ( mod ) {
6496: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
6497: } else if ( nd_vc )
6498: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
6499: else
1.6 noro 6500: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 6501: *p = t;
6502: }
6503: }
6504: }
6505: }
6506: curlen *= k+1;
6507: }
6508: FREENM(m);
6509: if ( nd_module ) {
6510: mpos = MPOS(d1);
6511: for ( i = 0; i < tlen; i++ )
6512: if ( tab[i] ) {
6513: d = DL(tab[i]);
6514: MPOS(d) = mpos;
6515: TD(d) = ndl_weight(d);
6516: }
6517: }
6518: }
6519:
6520: ND ndv_mul_nm_symbolic(NM m0,NDV p)
6521: {
6522: NM mr,mr0;
6523: NMV m;
6524: UINT *d,*dt,*dm;
6525: int c,n,td,i,c1,c2,len;
6526: Q q;
6527: ND r;
6528:
6529: if ( !p ) return 0;
6530: else {
6531: n = NV(p); m = BDY(p);
6532: d = DL(m0);
6533: len = LEN(p);
6534: mr0 = 0;
6535: td = TD(d);
6536: c = CM(m0);
6537: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6538: NEXTNM(mr0,mr);
6539: CM(mr) = 1;
6540: ndl_add(DL(m),d,DL(mr));
6541: }
6542: NEXT(mr) = 0;
6543: MKND(NV(p),mr0,len,r);
6544: SG(r) = SG(p) + TD(d);
6545: return r;
6546: }
6547: }
6548:
6549: ND ndv_mul_nm(int mod,NM m0,NDV p)
6550: {
6551: NM mr,mr0;
6552: NMV m;
6553: UINT *d,*dt,*dm;
6554: int c,n,td,i,c1,c2,len;
6555: P q;
6556: ND r;
6557:
6558: if ( !p ) return 0;
6559: else if ( do_weyl ) {
6560: if ( mod < 0 ) {
6561: error("ndv_mul_nm : not implemented (weyl)");
6562: return 0;
6563: } else
6564: return weyl_ndv_mul_nm(mod,m0,p);
6565: } else {
6566: n = NV(p); m = BDY(p);
6567: d = DL(m0);
6568: len = LEN(p);
6569: mr0 = 0;
6570: td = TD(d);
6571: if ( mod == -1 ) {
6572: c = CM(m0);
6573: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6574: NEXTNM(mr0,mr);
6575: CM(mr) = _mulsf(CM(m),c);
6576: ndl_add(DL(m),d,DL(mr));
6577: }
6578: } else if ( mod == -2 ) {
6579: Z cl;
6580: cl = CZ(m0);
6581: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6582: NEXTNM(mr0,mr);
6583: mullf(CZ(m),cl,&CZ(mr));
6584: ndl_add(DL(m),d,DL(mr));
6585: }
6586: } else if ( mod ) {
6587: c = CM(m0);
6588: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6589: NEXTNM(mr0,mr);
6590: c1 = CM(m);
6591: DMAR(c1,c,0,mod,c2);
6592: CM(mr) = c2;
6593: ndl_add(DL(m),d,DL(mr));
6594: }
6595: } else {
6596: q = CP(m0);
6597: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6598: NEXTNM(mr0,mr);
6599: mulp(nd_vc,CP(m),q,&CP(mr));
6600: ndl_add(DL(m),d,DL(mr));
6601: }
6602: }
6603: NEXT(mr) = 0;
6604: MKND(NV(p),mr0,len,r);
6605: SG(r) = SG(p) + TD(d);
6606: return r;
6607: }
6608: }
6609:
6610: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6611: {
6612: NM mq0,mq;
6613: NMV tm;
6614: Q q;
6615: int i,nv,sg,c,c1,c2,hindex;
6616: ND p,t,r;
6617:
6618: if ( bucket->m < 0 ) return 0;
6619: else {
6620: nv = NV(d);
6621: mq0 = 0;
6622: tm = (NMV)MALLOC(nmv_adv);
6623: while ( 1 ) {
6624: if ( mod > 0 || mod == -1 )
6625: hindex = head_pbucket(mod,bucket);
6626: else if ( mod == -2 )
6627: hindex = head_pbucket_lf(bucket);
6628: else
6629: hindex = head_pbucket_q(bucket);
6630: if ( hindex < 0 ) break;
6631: p = bucket->body[hindex];
6632: NEXTNM(mq0,mq);
6633: ndl_sub(HDL(p),HDL(d),DL(mq));
6634: ndl_copy(DL(mq),DL(tm));
6635: if ( mod ) {
6636: c1 = invm(HCM(d),mod); c2 = HCM(p);
6637: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6638: CM(tm) = mod-c;
6639: } else {
1.6 noro 6640: divsz(HCZ(p),HCZ(d),&CZ(mq));
6641: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6642: }
6643: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6644: bucket->body[hindex] = nd_remove_head(p);
6645: t = nd_remove_head(t);
6646: add_pbucket(mod,bucket,t);
6647: }
6648: if ( !mq0 )
6649: r = 0;
6650: else {
6651: NEXT(mq) = 0;
6652: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6653: MKND(nv,mq0,i,r);
6654: /* XXX */
6655: SG(r) = HTD(r);
6656: }
6657: return r;
6658: }
6659: }
6660:
6661: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6662: {
6663: NMV m,mr,mr0,t;
6664: int len,i,k;
6665:
6666: if ( !p ) return;
6667: m = BDY(p); len = LEN(p);
6668: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6669: m = (NMV)((char *)mr0+(len-1)*oadv);
6670: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6671: t = (NMV)MALLOC(nmv_adv);
6672: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6673: CZ(t) = CZ(m);
1.1 noro 6674: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6675: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6676: CZ(mr) = CZ(t);
1.1 noro 6677: ndl_copy(DL(t),DL(mr));
6678: }
6679: BDY(p) = mr0;
6680: }
6681:
6682: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6683: {
6684: NMV m,mr,mr0;
6685: int len,i;
6686: NDV r;
6687:
6688: if ( !p ) return 0;
6689: m = BDY(p); len = LEN(p);
6690: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6691: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6692: ndl_zero(DL(mr));
6693: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6694: CZ(mr) = CZ(m);
1.1 noro 6695: }
6696: MKNDV(NV(p),mr0,len,r);
6697: SG(r) = SG(p);
1.24 noro 6698: r->sig = p->sig;
1.1 noro 6699: return r;
6700: }
6701:
6702: /* duplicate p */
6703:
6704: NDV ndv_dup(int mod,NDV p)
6705: {
6706: NDV d;
6707: NMV t,m,m0;
6708: int i,len;
6709:
6710: if ( !p ) return 0;
6711: len = LEN(p);
6712: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6713: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6714: ndl_copy(DL(t),DL(m));
1.6 noro 6715: CZ(m) = CZ(t);
1.1 noro 6716: }
6717: MKNDV(NV(p),m0,len,d);
6718: SG(d) = SG(p);
6719: return d;
6720: }
6721:
6722: NDV ndv_symbolic(int mod,NDV p)
6723: {
6724: NDV d;
6725: NMV t,m,m0;
6726: int i,len;
6727:
6728: if ( !p ) return 0;
6729: len = LEN(p);
6730: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6731: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6732: ndl_copy(DL(t),DL(m));
1.6 noro 6733: CZ(m) = ONE;
1.1 noro 6734: }
6735: MKNDV(NV(p),m0,len,d);
6736: SG(d) = SG(p);
6737: return d;
6738: }
6739:
6740: ND nd_dup(ND p)
6741: {
6742: ND d;
6743: NM t,m,m0;
6744:
6745: if ( !p ) return 0;
6746: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6747: NEXTNM(m0,m);
6748: ndl_copy(DL(t),DL(m));
1.6 noro 6749: CZ(m) = CZ(t);
1.1 noro 6750: }
6751: if ( m0 ) NEXT(m) = 0;
6752: MKND(NV(p),m0,LEN(p),d);
6753: SG(d) = SG(p);
6754: return d;
6755: }
6756:
6757: /* XXX if p->len == 0 then it represents 0 */
6758:
6759: void ndv_mod(int mod,NDV p)
6760: {
6761: NMV t,d;
6762: int r,s,u;
6763: int i,len,dlen;
6764: P cp;
6765: Z c;
6766: Obj gfs;
6767:
6768: if ( !p ) return;
6769: len = LEN(p);
6770: dlen = 0;
6771: if ( mod == -1 )
6772: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6773: simp_ff((Obj)CP(t),&gfs);
6774: if ( gfs ) {
6775: r = FTOIF(CONT((GFS)gfs));
6776: CM(d) = r;
6777: ndl_copy(DL(t),DL(d));
6778: NMV_ADV(d);
6779: dlen++;
6780: }
6781: }
6782: else if ( mod == -2 )
6783: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6784: simp_ff((Obj)CP(t),&gfs);
6785: if ( gfs ) {
6786: lmtolf((LM)gfs,&CZ(d));
6787: ndl_copy(DL(t),DL(d));
6788: NMV_ADV(d);
6789: dlen++;
6790: }
6791: }
6792: else
6793: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6794: if ( nd_vc ) {
6795: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6796: c = (Z)cp;
6797: } else
1.6 noro 6798: c = CZ(t);
1.1 noro 6799: r = remqi((Q)c,mod);
6800: if ( r ) {
6801: CM(d) = r;
6802: ndl_copy(DL(t),DL(d));
6803: NMV_ADV(d);
6804: dlen++;
6805: }
6806: }
6807: LEN(p) = dlen;
6808: }
6809:
6810: NDV ptondv(VL vl,VL dvl,P p)
6811: {
6812: ND nd;
6813:
6814: nd = ptond(vl,dvl,p);
6815: return ndtondv(0,nd);
6816: }
6817:
6818: void pltozpl(LIST l,Q *cont,LIST *pp)
6819: {
1.16 noro 6820: NODE nd,nd1;
6821: int n;
6822: P *pl;
6823: Q *cl;
6824: int i;
6825: P dmy;
6826: Z dvr,inv;
6827: LIST r;
6828:
6829: nd = BDY(l); n = length(nd);
6830: pl = (P *)MALLOC(n*sizeof(P));
6831: cl = (Q *)MALLOC(n*sizeof(Q));
6832: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6833: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6834: }
6835: qltozl(cl,n,&dvr);
6836: divz(ONE,dvr,&inv);
6837: nd = BDY(l);
6838: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6839: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6840: nd = 0;
6841: for ( i = n-1; i >= 0; i-- ) {
6842: MKNODE(nd1,pl[i],nd); nd = nd1;
6843: }
6844: MKLIST(r,nd);
6845: *pp = r;
1.1 noro 6846: }
6847:
6848: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6849:
6850: NDV pltondv(VL vl,VL dvl,LIST p)
6851: {
6852: int i;
6853: NODE t;
6854: ND r,ri;
6855: NM m;
6856:
6857: if ( !nd_module ) error("pltond : module order must be set");
6858: r = 0;
6859: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6860: ri = ptond(vl,dvl,(P)BDY(t));
6861: if ( ri )
6862: for ( m = BDY(ri); m; m = NEXT(m) ) {
6863: MPOS(DL(m)) = i;
6864: TD(DL(m)) = ndl_weight(DL(m));
6865: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6866: }
6867: r = nd_add(0,r,ri);
6868: }
6869: return ndtondv(0,r);
6870: }
6871:
6872: ND ptond(VL vl,VL dvl,P p)
6873: {
6874: int n,i,j,k,e;
6875: VL tvl;
6876: V v;
6877: DCP dc;
6878: DCP *w;
6879: ND r,s,t,u;
6880: P x;
6881: int c;
6882: UINT *d;
6883: NM m,m0;
6884:
6885: if ( !p )
6886: return 0;
6887: else if ( NUM(p) ) {
6888: NEWNM(m);
6889: ndl_zero(DL(m));
6890: if ( !INT((Q)p) )
6891: error("ptond : input must be integer-coefficient");
1.6 noro 6892: CZ(m) = (Z)p;
1.1 noro 6893: NEXT(m) = 0;
6894: MKND(nd_nvar,m,1,r);
6895: SG(r) = 0;
6896: return r;
6897: } else {
6898: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6899: w = (DCP *)MALLOC(k*sizeof(DCP));
6900: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6901: for ( i = 0, tvl = dvl, v = VR(p);
6902: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6903: if ( !tvl ) {
6904: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6905: t = ptond(vl,dvl,COEF(w[j]));
6906: pwrp(vl,x,DEG(w[j]),&p);
6907: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6908: }
6909: return s;
6910: } else {
6911: NEWNM(m0); d = DL(m0);
6912: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6913: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6914: TD(d) = MUL_WEIGHT(e,i);
6915: if ( nd_blockmask) ndl_weight_mask(d);
6916: if ( nd_module ) MPOS(d) = 0;
6917: t = ptond(vl,dvl,COEF(w[j]));
6918: for ( m = BDY(t); m; m = NEXT(m) )
6919: ndl_addto(DL(m),d);
6920: SG(t) += TD(d);
6921: s = nd_add(0,s,t);
6922: }
6923: FREENM(m0);
6924: return s;
6925: }
6926: }
6927: }
6928:
6929: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6930: {
6931: VL tvl;
6932: int len,n,j,i,e;
6933: NMV m;
6934: Z q;
6935: P c;
6936: UINT *d;
6937: P s,r,u,t,w;
6938: GFS gfs;
6939:
6940: if ( !p ) return 0;
6941: else {
6942: len = LEN(p);
6943: n = NV(p);
6944: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6945: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6946: if ( mod == -1 ) {
6947: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6948: } else if ( mod == -2 ) {
6949: c = (P)CZ(m);
6950: } else if ( mod > 0 ) {
1.6 noro 6951: STOZ(CM(m),q); c = (P)q;
1.1 noro 6952: } else
6953: c = CP(m);
6954: d = DL(m);
6955: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6956: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6957: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6958: }
6959: addp(vl,s,t,&u); s = u;
6960: }
6961: return s;
6962: }
6963: }
6964:
6965: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6966: {
6967: VL tvl;
6968: int len,n,j,i,e;
6969: NMV m;
6970: Z q;
6971: P c;
6972: UINT *d;
6973: P s,r,u,t,w;
6974: GFS gfs;
6975: P *a;
6976: LIST l;
6977: NODE nd,nd1;
6978:
6979: if ( !p ) return 0;
6980: else {
6981: a = (P *)MALLOC((rank+1)*sizeof(P));
6982: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6983: len = LEN(p);
6984: n = NV(p);
6985: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6986: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6987: if ( mod == -1 ) {
6988: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6989: } else if ( mod ) {
1.6 noro 6990: STOZ(CM(m),q); c = (P)q;
1.1 noro 6991: } else
6992: c = CP(m);
6993: d = DL(m);
6994: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6995: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6996: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6997: }
6998: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6999: }
7000: nd = 0;
7001: for ( i = rank; i > 0; i-- ) {
7002: MKNODE(nd1,a[i],nd); nd = nd1;
7003: }
7004: MKLIST(l,nd);
7005: return l;
7006: }
7007: }
7008:
7009: NDV ndtondv(int mod,ND p)
7010: {
7011: NDV d;
7012: NMV m,m0;
7013: NM t;
7014: int i,len;
7015:
7016: if ( !p ) return 0;
7017: len = LEN(p);
7018: if ( mod > 0 || mod == -1 )
7019: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7020: else
7021: m0 = m = MALLOC(len*nmv_adv);
7022: #if 0
7023: ndv_alloc += nmv_adv*len;
7024: #endif
7025: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
7026: ndl_copy(DL(t),DL(m));
1.6 noro 7027: CZ(m) = CZ(t);
1.1 noro 7028: }
7029: MKNDV(NV(p),m0,len,d);
7030: SG(d) = SG(p);
1.24 noro 7031: d->sig = p->sig;
1.1 noro 7032: return d;
7033: }
7034:
1.16 noro 7035: static int dmm_comp_nv;
7036:
7037: int dmm_comp(DMM *a,DMM *b)
7038: {
7039: return -compdmm(dmm_comp_nv,*a,*b);
7040: }
7041:
7042: void dmm_sort_by_ord(DMM *a,int len,int nv)
7043: {
7044: dmm_comp_nv = nv;
7045: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
7046: }
7047:
7048: void dpm_sort(DPM p,DPM *rp)
7049: {
7050: DMM t,t1;
7051: int len,i,n;
7052: DMM *a;
7053: DPM d;
7054:
7055: if ( !p ) *rp = 0;
7056: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7057: a = (DMM *)MALLOC(len*sizeof(DMM));
7058: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7059: n = p->nv;
7060: dmm_sort_by_ord(a,len,n);
7061: t = 0;
7062: for ( i = len-1; i >= 0; i-- ) {
7063: NEWDMM(t1);
7064: t1->c = a[i]->c;
7065: t1->dl = a[i]->dl;
7066: t1->pos = a[i]->pos;
7067: t1->next = t;
7068: t = t1;
7069: }
7070: MKDPM(n,t,d);
7071: SG(d) = SG(p);
7072: *rp = d;
7073: }
7074:
1.18 noro 7075: int dpm_comp(DPM *a,DPM *b)
7076: {
1.22 noro 7077: return -compdpm(CO,*a,*b);
1.18 noro 7078: }
7079:
7080: NODE dpm_sort_list(NODE l)
7081: {
7082: int i,len;
7083: NODE t,t1;
7084: DPM *a;
7085:
7086: len = length(l);
7087: a = (DPM *)MALLOC(len*sizeof(DPM));
7088: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
7089: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
7090: t = 0;
7091: for ( i = len-1; i >= 0; i-- ) {
7092: MKNODE(t1,(pointer)a[i],t); t = t1;
7093: }
7094: return t;
7095: }
7096:
1.20 noro 7097: int nmv_comp(NMV a,NMV b)
7098: {
1.21 noro 7099: int t;
7100: t = DL_COMPARE(a->dl,b->dl);
7101: return -t;
1.20 noro 7102: }
7103:
1.43 noro 7104: NDV dptondv(int mod,DP p)
7105: {
7106: NDV d;
7107: NMV m,m0;
7108: MP t;
7109: MP *a;
7110: int i,len,n;
7111:
7112: if ( !p ) return 0;
7113: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7114: n = p->nv;
7115: if ( mod > 0 || mod == -1 )
7116: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7117: else
7118: m0 = m = MALLOC(len*nmv_adv);
7119: for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
7120: dltondl(n,t->dl,DL(m));
7121: TD(DL(m)) = ndl_weight(DL(m));
7122: CZ(m) = (Z)t->c;
7123: }
7124: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
7125: MKNDV(NV(p),m0,len,d);
7126: SG(d) = SG(p);
7127: return d;
7128: }
7129:
1.16 noro 7130: NDV dpmtondv(int mod,DPM p)
7131: {
7132: NDV d;
7133: NMV m,m0;
7134: DMM t;
7135: DMM *a;
7136: int i,len,n;
7137:
7138: if ( !p ) return 0;
7139: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
7140: a = (DMM *)MALLOC(len*sizeof(DMM));
7141: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
7142: n = p->nv;
7143: dmm_sort_by_ord(a,len,n);
7144: if ( mod > 0 || mod == -1 )
7145: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
7146: else
7147: m0 = m = MALLOC(len*nmv_adv);
7148: #if 0
7149: ndv_alloc += nmv_adv*len;
7150: #endif
7151: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7152: dltondl(n,a[i]->dl,DL(m));
7153: MPOS(DL(m)) = a[i]->pos;
1.20 noro 7154: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 7155: CZ(m) = (Z)a[i]->c;
7156: }
1.20 noro 7157: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 7158: MKNDV(NV(p),m0,len,d);
7159: SG(d) = SG(p);
7160: return d;
7161: }
7162:
1.1 noro 7163: ND ndvtond(int mod,NDV p)
7164: {
7165: ND d;
7166: NM m,m0;
7167: NMV t;
7168: int i,len;
7169:
7170: if ( !p ) return 0;
7171: m0 = 0;
7172: len = p->len;
7173: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7174: NEXTNM(m0,m);
7175: ndl_copy(DL(t),DL(m));
1.6 noro 7176: CZ(m) = CZ(t);
1.1 noro 7177: }
7178: NEXT(m) = 0;
7179: MKND(NV(p),m0,len,d);
7180: SG(d) = SG(p);
1.28 noro 7181: d->sig = p->sig;
1.1 noro 7182: return d;
7183: }
7184:
7185: DP ndvtodp(int mod,NDV p)
7186: {
7187: MP m,m0;
7188: DP d;
7189: NMV t;
7190: int i,len;
7191:
7192: if ( !p ) return 0;
7193: m0 = 0;
7194: len = p->len;
7195: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7196: NEXTMP(m0,m);
7197: m->dl = ndltodl(nd_nvar,DL(t));
7198: m->c = (Obj)ndctop(mod,t->c);
7199: }
7200: NEXT(m) = 0;
7201: MKDP(nd_nvar,m0,d);
7202: SG(d) = SG(p);
7203: return d;
7204: }
7205:
1.44 noro 7206: DPM sigtodpm(SIG s)
7207: {
7208: DMM m;
7209: DPM d;
7210:
7211: NEWDMM(m);
7212: m->c = (Obj)ONE;
7213: m->dl = s->dl;
7214: m->pos = s->pos+1;
7215: m->next = 0;
7216: MKDPM(nd_nvar,m,d);
7217: SG(d) = s->dl->td;
7218: return d;
7219: }
7220:
1.16 noro 7221: DPM ndvtodpm(int mod,NDV p)
7222: {
7223: DMM m,m0;
7224: DPM d;
7225: NMV t;
7226: int i,len;
7227:
7228: if ( !p ) return 0;
7229: m0 = 0;
7230: len = p->len;
7231: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
7232: NEXTDMM(m0,m);
7233: m->dl = ndltodl(nd_nvar,DL(t));
7234: m->c = (Obj)ndctop(mod,t->c);
7235: m->pos = MPOS(DL(t));
7236: }
7237: NEXT(m) = 0;
7238: MKDPM(nd_nvar,m0,d);
7239: SG(d) = SG(p);
7240: return d;
7241: }
7242:
7243:
1.1 noro 7244: DP ndtodp(int mod,ND p)
7245: {
7246: MP m,m0;
7247: DP d;
7248: NM t;
7249: int i,len;
7250:
7251: if ( !p ) return 0;
7252: m0 = 0;
7253: len = p->len;
7254: for ( t = BDY(p); t; t = NEXT(t) ) {
7255: NEXTMP(m0,m);
7256: m->dl = ndltodl(nd_nvar,DL(t));
7257: m->c = (Obj)ndctop(mod,t->c);
7258: }
7259: NEXT(m) = 0;
7260: MKDP(nd_nvar,m0,d);
7261: SG(d) = SG(p);
7262: return d;
7263: }
7264:
7265: void ndv_print(NDV p)
7266: {
7267: NMV m;
7268: int i,len;
7269:
7270: if ( !p ) printf("0\n");
7271: else {
7272: len = LEN(p);
7273: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7274: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 7275: else printf("+%ld*",CM(m));
1.1 noro 7276: ndl_print(DL(m));
7277: }
7278: printf("\n");
7279: }
7280: }
7281:
7282: void ndv_print_q(NDV p)
7283: {
7284: NMV m;
7285: int i,len;
7286:
7287: if ( !p ) printf("0\n");
7288: else {
7289: len = LEN(p);
7290: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
7291: printf("+");
1.6 noro 7292: printexpr(CO,(Obj)CZ(m));
1.1 noro 7293: printf("*");
7294: ndl_print(DL(m));
7295: }
7296: printf("\n");
7297: }
7298: }
7299:
7300: NODE ndv_reducebase(NODE x,int *perm)
7301: {
7302: int len,i,j;
7303: NDVI w;
7304: NODE t,t0;
7305:
1.41 noro 7306: if ( nd_norb ) return x;
1.1 noro 7307: len = length(x);
7308: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
7309: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
7310: w[i].p = BDY(t); w[i].i = perm[i];
7311: }
7312: for ( i = 0; i < len; i++ ) {
7313: for ( j = 0; j < i; j++ ) {
7314: if ( w[i].p && w[j].p ) {
7315: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
7316: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
7317: }
7318: }
7319: }
7320: for ( i = j = 0, t0 = 0; i < len; i++ ) {
7321: if ( w[i].p ) {
7322: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
7323: perm[j++] = w[i].i;
7324: }
7325: }
7326: NEXT(t) = 0; x = t0;
7327: return x;
7328: }
7329:
7330: /* XXX incomplete */
7331:
1.21 noro 7332: extern DMMstack dmm_stack;
7333: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 7334:
1.1 noro 7335: void nd_init_ord(struct order_spec *ord)
7336: {
7337: nd_module = (ord->id >= 256);
7338: if ( nd_module ) {
7339: nd_dcomp = -1;
1.21 noro 7340: nd_module_ordtype = ord->module_ordtype;
1.1 noro 7341: nd_pot_nelim = ord->pot_nelim;
7342: nd_poly_weight_len = ord->nv;
7343: nd_poly_weight = ord->top_weight;
7344: nd_module_rank = ord->module_rank;
7345: nd_module_weight = ord->module_top_weight;
7346: }
7347: nd_matrix = 0;
7348: nd_matrix_len = 0;
7349: switch ( ord->id ) {
7350: case 0:
7351: switch ( ord->ord.simple ) {
7352: case 0:
7353: nd_dcomp = 1;
7354: nd_isrlex = 1;
7355: break;
7356: case 1:
7357: nd_dcomp = 1;
7358: nd_isrlex = 0;
7359: break;
7360: case 2:
7361: nd_dcomp = 0;
7362: nd_isrlex = 0;
7363: ndl_compare_function = ndl_lex_compare;
7364: break;
7365: case 11:
7366: /* XXX */
7367: nd_dcomp = 0;
7368: nd_isrlex = 1;
7369: ndl_compare_function = ndl_ww_lex_compare;
7370: break;
7371: default:
7372: error("nd_gr : unsupported order");
7373: }
7374: break;
7375: case 1:
7376: /* block order */
7377: /* XXX */
7378: nd_dcomp = -1;
7379: nd_isrlex = 0;
7380: ndl_compare_function = ndl_block_compare;
7381: break;
7382: case 2:
7383: /* matrix order */
7384: /* XXX */
7385: nd_dcomp = -1;
7386: nd_isrlex = 0;
7387: nd_matrix_len = ord->ord.matrix.row;
7388: nd_matrix = ord->ord.matrix.matrix;
7389: ndl_compare_function = ndl_matrix_compare;
7390: break;
7391: case 3:
7392: /* composite order */
7393: nd_dcomp = -1;
7394: nd_isrlex = 0;
7395: nd_worb_len = ord->ord.composite.length;
7396: nd_worb = ord->ord.composite.w_or_b;
7397: ndl_compare_function = ndl_composite_compare;
7398: break;
7399:
7400: /* module order */
7401: case 256:
7402: switch ( ord->ord.simple ) {
7403: case 0:
1.21 noro 7404: nd_dcomp = 0;
1.1 noro 7405: nd_isrlex = 1;
1.21 noro 7406: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 7407: break;
7408: case 1:
1.21 noro 7409: nd_dcomp = 0;
1.1 noro 7410: nd_isrlex = 0;
7411: ndl_compare_function = ndl_module_glex_compare;
7412: break;
7413: case 2:
1.21 noro 7414: nd_dcomp = 0;
1.1 noro 7415: nd_isrlex = 0;
1.21 noro 7416: ndl_compare_function = ndl_module_compare;
7417: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 7418: break;
7419: default:
1.21 noro 7420: error("nd_init_ord : unsupported order");
1.1 noro 7421: }
7422: break;
7423: case 257:
7424: /* block order */
7425: nd_isrlex = 0;
1.21 noro 7426: ndl_compare_function = ndl_module_compare;
7427: ndl_base_compare_function = ndl_block_compare;
1.1 noro 7428: break;
7429: case 258:
7430: /* matrix order */
7431: nd_isrlex = 0;
7432: nd_matrix_len = ord->ord.matrix.row;
7433: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 7434: ndl_compare_function = ndl_module_compare;
7435: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 7436: break;
7437: case 259:
7438: /* composite order */
7439: nd_isrlex = 0;
7440: nd_worb_len = ord->ord.composite.length;
7441: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 7442: ndl_compare_function = ndl_module_compare;
7443: ndl_base_compare_function = ndl_composite_compare;
7444: break;
7445: case 300:
7446: /* schreyer order */
7447: if ( ord->base->id != 256 )
7448: error("nd_init_ord : unsupported base order");
7449: ndl_compare_function = ndl_module_schreyer_compare;
7450: dmm_stack = ord->dmmstack;
7451: switch ( ord->base->ord.simple ) {
7452: case 0:
7453: nd_isrlex = 1;
7454: ndl_base_compare_function = ndl_glex_compare;
7455: dl_base_compare_function = cmpdl_revgradlex;
7456: break;
7457: case 1:
7458: nd_isrlex = 0;
7459: ndl_base_compare_function = ndl_glex_compare;
7460: dl_base_compare_function = cmpdl_gradlex;
7461: break;
7462: case 2:
7463: nd_isrlex = 0;
7464: ndl_base_compare_function = ndl_lex_compare;
7465: dl_base_compare_function = cmpdl_lex;
7466: break;
7467: default:
7468: error("nd_init_ord : unsupported order");
7469: }
1.1 noro 7470: break;
7471: }
7472: nd_ord = ord;
7473: }
7474:
7475: BlockMask nd_create_blockmask(struct order_spec *ord)
7476: {
7477: int n,i,j,s,l;
7478: UINT *t;
7479: BlockMask bm;
7480:
7481: /* we only create mask table for block order */
7482: if ( ord->id != 1 && ord->id != 257 )
7483: return 0;
7484: n = ord->ord.block.length;
7485: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
7486: bm->n = n;
7487: bm->order_pair = ord->ord.block.order_pair;
7488: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
7489: for ( i = 0, s = 0; i < n; i++ ) {
7490: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
7491: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
7492: l = bm->order_pair[i].length;
7493: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
7494: }
7495: return bm;
7496: }
7497:
7498: EPOS nd_create_epos(struct order_spec *ord)
7499: {
7500: int i,j,l,s,ord_l,ord_o;
7501: EPOS epos;
7502: struct order_pair *op;
7503:
7504: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
7505: switch ( ord->id ) {
1.21 noro 7506: case 0: case 256: case 300:
1.1 noro 7507: if ( nd_isrlex ) {
7508: for ( i = 0; i < nd_nvar; i++ ) {
7509: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
7510: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
7511: }
7512: } else {
7513: for ( i = 0; i < nd_nvar; i++ ) {
7514: epos[i].i = nd_exporigin + i/nd_epw;
7515: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7516: }
7517: }
7518: break;
7519: case 1: case 257:
7520: /* block order */
7521: l = ord->ord.block.length;
7522: op = ord->ord.block.order_pair;
7523: for ( j = 0, s = 0; j < l; j++ ) {
7524: ord_o = op[j].order;
7525: ord_l = op[j].length;
7526: if ( !ord_o )
7527: for ( i = 0; i < ord_l; i++ ) {
7528: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
7529: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
7530: }
7531: else
7532: for ( i = 0; i < ord_l; i++ ) {
7533: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
7534: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
7535: }
7536: s += ord_l;
7537: }
7538: break;
7539: case 2:
7540: /* matrix order */
7541: case 3:
7542: /* composite order */
7543: default:
7544: for ( i = 0; i < nd_nvar; i++ ) {
7545: epos[i].i = nd_exporigin + i/nd_epw;
7546: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
7547: }
7548: break;
7549: }
7550: return epos;
7551: }
7552:
7553: /* external interface */
7554:
7555: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
7556: {
7557: NODE t,in0,in;
7558: ND ndf,nf;
7559: NDV ndvf;
7560: VL vv,tv;
7561: int stat,nvar,max,mrank;
7562: union oNDC dn;
7563: Q cont;
7564: P pp;
7565: LIST ppl;
7566:
7567: if ( !f ) {
7568: *rp = 0;
7569: return;
7570: }
7571: pltovl(v,&vv);
7572: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7573:
7574: /* max=65536 implies nd_bpe=32 */
7575: max = 65536;
7576:
7577: nd_module = 0;
7578: /* nd_module will be set if ord is a module ordering */
7579: nd_init_ord(ord);
7580: nd_setup_parameters(nvar,max);
7581: if ( nd_module && OID(f) != O_LIST )
7582: error("nd_nf_p : the first argument must be a list");
7583: if ( nd_module ) mrank = length(BDY((LIST)f));
7584: /* conversion to ndv */
7585: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7586: NEXTNODE(in0,in);
7587: if ( nd_module ) {
7588: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7589: || length(BDY((LIST)BDY(t))) != mrank )
7590: error("nd_nf_p : inconsistent basis element");
7591: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7592: else ppl = (LIST)BDY(t);
7593: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7594: } else {
7595: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7596: else pp = (P)BDY(t);
7597: BDY(in) = (pointer)ptondv(CO,vv,pp);
7598: }
7599: if ( m ) ndv_mod(m,(NDV)BDY(in));
7600: }
7601: if ( in0 ) NEXT(in) = 0;
7602:
7603: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7604: else ndvf = ptondv(CO,vv,(P)f);
7605: if ( m ) ndv_mod(m,ndvf);
7606: ndf = (pointer)ndvtond(m,ndvf);
7607:
7608: /* dont sort, dont removecont */
1.24 noro 7609: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7610: nd_scale=2;
1.6 noro 7611: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7612: if ( !stat )
7613: error("nd_nf_p : exponent too large");
7614: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7615: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7616: }
7617:
7618: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7619: {
7620: NM m;
7621: UINT *t,*s;
7622: int i;
7623:
7624: for ( i = 0; i < n; i++ ) r[i] = 0;
7625: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7626: t = DL(m);
7627: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7628: r[i] = CM(m);
7629: }
7630: for ( i = 0; !r[i]; i++ );
7631: return i;
7632: }
7633:
7634: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7635: {
7636: NM m;
7637: UINT *t,*s;
7638: int i;
7639:
7640: for ( i = 0; i < n; i++ ) r[i] = 0;
7641: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7642: t = DL(m);
7643: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7644: r[i] = CZ(m);
1.1 noro 7645: }
7646: for ( i = 0; !r[i]; i++ );
7647: return i;
7648: }
7649:
7650: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7651: {
7652: NM m;
7653: UINT *t,*s;
7654: int i;
7655:
7656: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7657: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7658: t = DL(m);
7659: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7660: mpz_set(r[i],BDY(CZ(m)));
7661: }
7662: for ( i = 0; !mpz_sgn(r[i]); i++ );
7663: return i;
7664: }
7665:
7666: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7667: {
7668: NM m;
7669: unsigned long *v;
7670: int i,j,h,size;
7671: UINT *s,*t;
7672:
7673: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7674: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7675: bzero(v,size);
7676: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7677: t = DL(m);
7678: h = ndl_hash_value(t);
7679: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7680: v[i/BLEN] |= 1L <<(i%BLEN);
7681: }
7682: return v;
7683: }
7684:
7685: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7686: {
7687: NMV mr;
7688: UINT *d,*t,*s;
7689: int i,j,len,h,head;
7690:
7691: d = DL(m);
7692: len = LEN(p);
7693: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7694: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7695: ndl_add(d,DL(mr),t);
7696: h = ndl_hash_value(t);
7697: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7698: if ( j == 0 ) head = i;
7699: v[i/BLEN] |= 1L <<(i%BLEN);
7700: }
7701: return head;
7702: }
7703:
7704: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7705: {
7706: NM m;
7707: NMV mr;
7708: UINT *d,*t,*s;
7709: NDV p;
7710: int i,j,len;
7711: Z *r;
7712:
7713: m = pair->mul;
7714: d = DL(m);
7715: p = nd_ps[pair->index];
7716: len = LEN(p);
7717: r = (Z *)CALLOC(n,sizeof(Q));
7718: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7719: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7720: ndl_add(d,DL(mr),t);
7721: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7722: r[i] = CZ(mr);
1.1 noro 7723: }
7724: return r;
7725: }
7726:
1.11 noro 7727: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7728: {
7729: NM m;
7730: NMV mr;
1.11 noro 7731: UINT *d,*t,*s,*u;
1.1 noro 7732: NDV p;
7733: unsigned char *ivc;
7734: unsigned short *ivs;
7735: UINT *v,*ivi,*s0v;
1.11 noro 7736: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7737: IndArray r;
7738:
7739: m = pair->mul;
7740: d = DL(m);
7741: if ( trace )
7742: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7743: else
7744: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7745:
7746: len = LEN(p);
7747: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7748: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7749: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7750: ndl_add(d,DL(mr),t);
7751: st = prev;
7752: ed = n;
7753: while ( ed > st ) {
7754: md = (st+ed)/2;
7755: u = s0+md*nd_wpd;
7756: c = DL_COMPARE(u,t);
7757: if ( c == 0 ) break;
7758: else if ( c > 0 ) st = md;
7759: else ed = md;
7760: }
7761: prev = v[j] = md;
1.1 noro 7762: }
7763: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7764: r->head = v[0];
7765: diff = 0;
7766: for ( i = 1; i < len; i++ ) {
7767: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7768: }
7769: if ( diff < 256 ) {
7770: r->width = 1;
7771: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7772: r->index.c = ivc;
7773: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7774: } else if ( diff < 65536 ) {
7775: r->width = 2;
7776: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7777: r->index.s = ivs;
7778: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7779: } else {
7780: r->width = 4;
7781: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7782: r->index.i = ivi;
7783: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7784: }
7785: return r;
7786: }
7787:
7788: int compress_array(Z *svect,Z *cvect,int n)
7789: {
7790: int i,j;
7791:
7792: for ( i = j = 0; i < n; i++ )
7793: if ( svect[i] ) cvect[j++] = svect[i];
7794: return j;
7795: }
7796:
7797: void expand_array(Z *svect,Z *cvect,int n)
7798: {
7799: int i,j;
7800:
7801: for ( i = j = 0; j < n; i++ )
7802: if ( svect[i] ) svect[i] = cvect[j++];
7803: }
7804:
1.8 noro 7805: #if 0
1.1 noro 7806: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7807: {
7808: int i,j,k,len,pos,prev,nz;
7809: Z cs,mcs,c1,c2,cr,gcd,t;
7810: IndArray ivect;
7811: unsigned char *ivc;
7812: unsigned short *ivs;
7813: unsigned int *ivi;
7814: NDV redv;
7815: NMV mr;
7816: NODE rp;
7817: int maxrs;
7818: double hmag;
7819: Z *cvect;
1.3 noro 7820: int l;
1.1 noro 7821:
7822: maxrs = 0;
7823: for ( i = 0; i < col && !svect[i]; i++ );
7824: if ( i == col ) return maxrs;
7825: hmag = p_mag((P)svect[i])*nd_scale;
7826: cvect = (Z *)MALLOC(col*sizeof(Q));
7827: for ( i = 0; i < nred; i++ ) {
7828: ivect = imat[i];
7829: k = ivect->head;
7830: if ( svect[k] ) {
7831: maxrs = MAX(maxrs,rp0[i]->sugar);
7832: redv = nd_demand?ndv_load(rp0[i]->index)
7833: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7834: len = LEN(redv); mr = BDY(redv);
1.6 noro 7835: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7836: chsgnz(cs,&mcs);
7837: if ( !UNIQ(cr) ) {
7838: for ( j = 0; j < col; j++ ) {
7839: mulz(svect[j],cr,&c1); svect[j] = c1;
7840: }
7841: }
7842: svect[k] = 0; prev = k;
7843: switch ( ivect->width ) {
7844: case 1:
7845: ivc = ivect->index.c;
7846: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7847: pos = prev+ivc[j]; prev = pos;
1.6 noro 7848: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7849: }
7850: break;
7851: case 2:
7852: ivs = ivect->index.s;
7853: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7854: pos = prev+ivs[j]; prev = pos;
1.6 noro 7855: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7856: }
7857: break;
7858: case 4:
7859: ivi = ivect->index.i;
7860: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7861: pos = prev+ivi[j]; prev = pos;
1.6 noro 7862: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7863: }
7864: break;
7865: }
7866: for ( j = k+1; j < col && !svect[j]; j++ );
7867: if ( j == col ) break;
7868: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7869: nz = compress_array(svect,cvect,col);
7870: removecont_array((P *)cvect,nz,1);
7871: expand_array(svect,cvect,nz);
7872: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7873: }
7874: }
7875: }
7876: nz = compress_array(svect,cvect,col);
7877: removecont_array((P *)cvect,nz,1);
7878: expand_array(svect,cvect,nz);
7879: if ( DP_Print ) {
7880: fprintf(asir_out,"-"); fflush(asir_out);
7881: }
7882: return maxrs;
7883: }
1.4 noro 7884: #else
1.9 noro 7885:
1.4 noro 7886: /* direct mpz version */
7887: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7888: {
7889: int i,j,k,len,pos,prev;
7890: mpz_t cs,cr,gcd;
7891: IndArray ivect;
7892: unsigned char *ivc;
7893: unsigned short *ivs;
7894: unsigned int *ivi;
7895: NDV redv;
7896: NMV mr;
7897: NODE rp;
7898: int maxrs;
7899: double hmag;
7900: int l;
1.13 noro 7901: static mpz_t *svect;
7902: static int svect_len=0;
1.4 noro 7903:
7904: maxrs = 0;
7905: for ( i = 0; i < col && !svect0[i]; i++ );
7906: if ( i == col ) return maxrs;
7907: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7908: if ( col > svect_len ) {
7909: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7910: svect_len = col;
7911: }
1.4 noro 7912: for ( i = 0; i < col; i++ ) {
7913: mpz_init(svect[i]);
7914: if ( svect0[i] )
7915: mpz_set(svect[i],BDY(svect0[i]));
7916: else
7917: mpz_set_ui(svect[i],0);
7918: }
7919: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7920: for ( i = 0; i < nred; i++ ) {
7921: ivect = imat[i];
7922: k = ivect->head;
7923: if ( mpz_sgn(svect[k]) ) {
7924: maxrs = MAX(maxrs,rp0[i]->sugar);
7925: redv = nd_demand?ndv_load(rp0[i]->index)
7926: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7927: len = LEN(redv); mr = BDY(redv);
1.6 noro 7928: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7929: mpz_div(cs,svect[k],gcd);
1.6 noro 7930: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7931: mpz_neg(cs,cs);
1.9 noro 7932: if ( MUNIMPZ(cr) )
7933: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7934: else if ( !UNIMPZ(cr) )
7935: for ( j = 0; j < col; j++ ) {
7936: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7937: }
1.4 noro 7938: mpz_set_ui(svect[k],0);
7939: prev = k;
7940: switch ( ivect->width ) {
7941: case 1:
7942: ivc = ivect->index.c;
7943: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7944: pos = prev+ivc[j]; prev = pos;
1.6 noro 7945: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7946: }
7947: break;
7948: case 2:
7949: ivs = ivect->index.s;
7950: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7951: pos = prev+ivs[j]; prev = pos;
1.6 noro 7952: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7953: }
7954: break;
7955: case 4:
7956: ivi = ivect->index.i;
7957: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7958: pos = prev+ivi[j]; prev = pos;
1.6 noro 7959: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7960: }
7961: break;
7962: }
7963: for ( j = k+1; j < col && !svect[j]; j++ );
7964: if ( j == col ) break;
7965: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7966: mpz_removecont_array(svect,col);
7967: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7968: }
7969: }
7970: }
7971: mpz_removecont_array(svect,col);
7972: if ( DP_Print ) {
7973: fprintf(asir_out,"-"); fflush(asir_out);
7974: }
7975: for ( i = 0; i < col; i++ )
7976: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7977: else svect0[i] = 0;
7978: return maxrs;
7979: }
7980: #endif
1.1 noro 7981:
1.40 noro 7982: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1 noro 7983: {
7984: int i,j,k,len,pos,prev;
7985: UINT c,c1,c2,c3,up,lo,dmy;
7986: IndArray ivect;
7987: unsigned char *ivc;
7988: unsigned short *ivs;
7989: unsigned int *ivi;
7990: NDV redv;
7991: NMV mr;
7992: NODE rp;
7993: int maxrs;
7994:
7995: maxrs = 0;
7996: for ( i = 0; i < nred; i++ ) {
7997: ivect = imat[i];
7998: k = ivect->head; svect[k] %= m;
1.40 noro 7999: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1 noro 8000: maxrs = MAX(maxrs,rp0[i]->sugar);
8001: c = m-c; redv = nd_ps[rp0[i]->index];
8002: len = LEN(redv); mr = BDY(redv);
8003: svect[k] = 0; prev = k;
8004: switch ( ivect->width ) {
8005: case 1:
8006: ivc = ivect->index.c;
8007: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8008: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40 noro 8009: if ( c1 ) {
8010: c2 = svect[pos];
1.1 noro 8011: DMA(c1,c,c2,up,lo);
8012: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8013: } else svect[pos] = lo;
1.40 noro 8014: }
1.1 noro 8015: }
8016: break;
8017: case 2:
8018: ivs = ivect->index.s;
8019: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8020: pos = prev+ivs[j]; c1 = CM(mr);
8021: prev = pos;
1.40 noro 8022: if ( c1 ) {
8023: c2 = svect[pos];
1.1 noro 8024: DMA(c1,c,c2,up,lo);
8025: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8026: } else svect[pos] = lo;
1.40 noro 8027: }
1.1 noro 8028: }
8029: break;
8030: case 4:
8031: ivi = ivect->index.i;
8032: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8033: pos = prev+ivi[j]; c1 = CM(mr);
8034: prev = pos;
1.40 noro 8035: if ( c1 ) {
8036: c2 = svect[pos];
1.1 noro 8037: DMA(c1,c,c2,up,lo);
8038: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
8039: } else svect[pos] = lo;
1.40 noro 8040: }
1.1 noro 8041: }
8042: break;
8043: }
8044: }
8045: }
8046: for ( i = 0; i < col; i++ )
8047: if ( svect[i] >= (UINT)m ) svect[i] %= m;
8048: return maxrs;
8049: }
8050:
8051: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8052: {
8053: int i,j,k,len,pos,prev;
8054: UINT c,c1,c2,c3,up,lo,dmy;
8055: IndArray ivect;
8056: unsigned char *ivc;
8057: unsigned short *ivs;
8058: unsigned int *ivi;
8059: NDV redv;
8060: NMV mr;
8061: NODE rp;
8062: int maxrs;
8063:
8064: maxrs = 0;
8065: for ( i = 0; i < nred; i++ ) {
8066: ivect = imat[i];
8067: k = ivect->head;
8068: if ( (c = svect[k]) != 0 ) {
8069: maxrs = MAX(maxrs,rp0[i]->sugar);
8070: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
8071: len = LEN(redv); mr = BDY(redv);
8072: svect[k] = 0; prev = k;
8073: switch ( ivect->width ) {
8074: case 1:
8075: ivc = ivect->index.c;
8076: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8077: pos = prev+ivc[j]; prev = pos;
8078: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8079: }
8080: break;
8081: case 2:
8082: ivs = ivect->index.s;
8083: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8084: pos = prev+ivs[j]; prev = pos;
8085: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8086: }
8087: break;
8088: case 4:
8089: ivi = ivect->index.i;
8090: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8091: pos = prev+ivi[j]; prev = pos;
8092: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
8093: }
8094: break;
8095: }
8096: }
8097: }
8098: return maxrs;
8099: }
8100:
8101: ND nd_add_lf(ND p1,ND p2)
8102: {
8103: int n,c,can;
8104: ND r;
8105: NM m1,m2,mr0,mr,s;
8106: Z t;
8107:
8108: if ( !p1 ) return p2;
8109: else if ( !p2 ) return p1;
8110: else {
8111: can = 0;
8112: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
8113: c = DL_COMPARE(DL(m1),DL(m2));
8114: switch ( c ) {
8115: case 0:
8116: addlf(CZ(m1),CZ(m2),&t);
8117: s = m1; m1 = NEXT(m1);
8118: if ( t ) {
8119: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
8120: } else {
8121: can += 2; FREENM(s);
8122: }
8123: s = m2; m2 = NEXT(m2); FREENM(s);
8124: break;
8125: case 1:
8126: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
8127: break;
8128: case -1:
8129: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
8130: break;
8131: }
8132: }
8133: if ( !mr0 )
8134: if ( m1 ) mr0 = m1;
8135: else if ( m2 ) mr0 = m2;
8136: else return 0;
8137: else if ( m1 ) NEXT(mr) = m1;
8138: else if ( m2 ) NEXT(mr) = m2;
8139: else NEXT(mr) = 0;
8140: BDY(p1) = mr0;
8141: SG(p1) = MAX(SG(p1),SG(p2));
8142: LEN(p1) = LEN(p1)+LEN(p2)-can;
8143: FREEND(p2);
8144: return p1;
8145: }
8146: }
8147:
8148: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
8149: {
8150: int i,j,k,len,pos,prev;
8151: mpz_t c,mc,c1;
8152: IndArray ivect;
8153: unsigned char *ivc;
8154: unsigned short *ivs;
8155: unsigned int *ivi;
8156: NDV redv;
8157: NMV mr;
8158: NODE rp;
8159: int maxrs;
8160:
8161: maxrs = 0;
8162: lf_lazy = 1;
8163: for ( i = 0; i < nred; i++ ) {
8164: ivect = imat[i];
8165: k = ivect->head;
8166: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
8167: if ( mpz_sgn(svect[k]) ) {
8168: maxrs = MAX(maxrs,rp0[i]->sugar);
8169: mpz_neg(svect[k],svect[k]);
8170: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
8171: len = LEN(redv); mr = BDY(redv);
8172: prev = k;
8173: switch ( ivect->width ) {
8174: case 1:
8175: ivc = ivect->index.c;
8176: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8177: pos = prev+ivc[j]; prev = pos;
8178: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8179: }
8180: break;
8181: case 2:
8182: ivs = ivect->index.s;
8183: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8184: pos = prev+ivs[j]; prev = pos;
8185: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8186: }
8187: break;
8188: case 4:
8189: ivi = ivect->index.i;
8190: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
8191: pos = prev+ivi[j]; prev = pos;
8192: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
8193: }
8194: break;
8195: }
8196: mpz_set_ui(svect[k],0);
8197: }
8198: }
8199: lf_lazy=0;
8200: for ( i = 0; i < col; i++ ) {
8201: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
8202: }
8203: return maxrs;
8204: }
8205:
8206: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
8207: {
8208: int i,j,k,l,rank,s;
8209: mpz_t a,a1,inv;
8210: mpz_t *t,*pivot,*pk;
8211: mpz_t **mat;
8212: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
8213: int size,size1;
8214:
8215: mpz_init(inv);
8216: mpz_init(a);
8217: mat = (mpz_t **)mat0;
8218: size = 0;
8219: for ( rank = 0, j = 0; j < col; j++ ) {
8220: for ( i = rank; i < row; i++ ) {
8221: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
8222: }
8223: for ( i = rank; i < row; i++ )
8224: if ( mpz_sgn(mat[i][j]) )
8225: break;
8226: if ( i == row ) {
8227: colstat[j] = 0;
8228: continue;
8229: } else
8230: colstat[j] = 1;
8231: if ( i != rank ) {
8232: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8233: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8234: }
8235: pivot = mat[rank];
8236: s = sugar[rank];
8237: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
8238: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8239: if ( mpz_sgn(*pk) ) {
8240: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
8241: }
8242: for ( i = rank+1; i < row; i++ ) {
8243: t = mat[i];
8244: if ( mpz_sgn(t[j]) ) {
8245: sugar[i] = MAX(sugar[i],s);
8246: mpz_neg(a,t[j]);
8247: red_by_vect_lf(t+j,pivot+j,a,col-j);
8248: }
8249: }
8250: rank++;
8251: }
8252: for ( j = col-1, l = rank-1; j >= 0; j-- )
8253: if ( colstat[j] ) {
8254: pivot = mat[l];
8255: s = sugar[l];
8256: for ( k = j; k < col; k++ )
8257: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
8258: for ( i = 0; i < l; i++ ) {
8259: t = mat[i];
8260: if ( mpz_sgn(t[j]) ) {
8261: sugar[i] = MAX(sugar[i],s);
8262: mpz_neg(a,t[j]);
8263: red_by_vect_lf(t+j,pivot+j,a,col-j);
8264: }
8265: }
8266: l--;
8267: }
8268: for ( j = 0, l = 0; l < rank; j++ )
8269: if ( colstat[j] ) {
8270: t = mat[l];
8271: for ( k = j; k < col; k++ ) {
8272: mpz_mod(t[k],t[k],BDY(current_mod_lf));
8273: }
8274: l++;
8275: }
8276: return rank;
8277: }
8278:
8279:
8280: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
8281: {
8282: int j,k,len;
8283: UINT *p;
8284: UINT c;
8285: NDV r;
8286: NMV mr0,mr;
8287:
8288: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8289: if ( !len ) return 0;
8290: else {
8291: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8292: #if 0
8293: ndv_alloc += nmv_adv*len;
8294: #endif
8295: mr = mr0;
8296: p = s0vect;
8297: for ( j = k = 0; j < col; j++, p += nd_wpd )
8298: if ( !rhead[j] ) {
8299: if ( (c = vect[k++]) != 0 ) {
8300: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8301: }
8302: }
8303: MKNDV(nd_nvar,mr0,len,r);
8304: return r;
8305: }
8306: }
8307:
1.40 noro 8308: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
8309: {
8310: int j,k,len;
8311: UINT *p;
8312: UINT c;
8313: NDV r;
8314: NMV mr0,mr;
8315:
8316: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8317: if ( !len ) return 0;
8318: else {
8319: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8320: mr = mr0;
8321: p = s0vect;
8322: for ( j = k = 0; j < col; j++, p += nd_wpd )
8323: if ( (c = vect[k++]) != 0 ) {
8324: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
8325: }
8326: MKNDV(nd_nvar,mr0,len,r);
8327: return r;
8328: }
8329: }
8330:
1.1 noro 8331: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
8332: {
8333: int j,k,len;
8334: UINT *p;
8335: NDV r;
8336: NMV mr0,mr;
8337:
8338: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
8339: if ( !len ) return 0;
8340: else {
8341: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8342: mr = mr0;
8343: p = s0vect;
8344: for ( j = 0; j < col; j++, p += nd_wpd )
8345: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
8346: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
8347: }
8348: MKNDV(nd_nvar,mr0,len,r);
8349: return r;
8350: }
8351: }
8352:
8353: /* for preprocessed vector */
8354:
8355: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
8356: {
1.6 noro 8357: int j,k,len;
8358: UINT *p;
8359: Z c;
8360: NDV r;
8361: NMV mr0,mr;
1.1 noro 8362:
1.6 noro 8363: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
8364: if ( !len ) return 0;
8365: else {
8366: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 8367: #if 0
1.6 noro 8368: ndv_alloc += nmv_adv*len;
1.1 noro 8369: #endif
1.6 noro 8370: mr = mr0;
8371: p = s0vect;
8372: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
8373: if ( !rhead[j] ) {
8374: if ( (c = vect[k++]) != 0 ) {
8375: if ( !INT(c) )
8376: error("vect_to_ndv_q : components must be integers");
8377: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
8378: }
8379: }
1.1 noro 8380: }
1.6 noro 8381: MKNDV(nd_nvar,mr0,len,r);
8382: return r;
8383: }
1.1 noro 8384: }
8385:
8386: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
8387: {
8388: int j,k,len;
8389: UINT *p;
8390: mpz_t c;
8391: NDV r;
8392: NMV mr0,mr;
8393:
8394: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
8395: if ( !len ) return 0;
8396: else {
8397: mr0 = (NMV)MALLOC(nmv_adv*len);
8398: #if 0
8399: ndv_alloc += nmv_adv*len;
8400: #endif
8401: mr = mr0;
8402: p = s0vect;
8403: for ( j = k = 0; j < col; j++, p += nd_wpd )
8404: if ( !rhead[j] ) {
8405: c[0] = vect[k++][0];
8406: if ( mpz_sgn(c) ) {
8407: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
8408: }
8409: }
8410: MKNDV(nd_nvar,mr0,len,r);
8411: return r;
8412: }
8413: }
8414:
8415: /* for plain vector */
8416:
8417: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
8418: {
8419: int j,k,len;
8420: UINT *p;
8421: Z c;
8422: NDV r;
8423: NMV mr0,mr;
8424:
8425: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
8426: if ( !len ) return 0;
8427: else {
8428: mr0 = (NMV)MALLOC(nmv_adv*len);
8429: #if 0
8430: ndv_alloc += nmv_adv*len;
8431: #endif
8432: mr = mr0;
8433: p = s0vect;
8434: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
8435: if ( (c = vect[k]) != 0 ) {
8436: if ( !INT(c) )
1.6 noro 8437: error("plain_vect_to_ndv_q : components must be integers");
8438: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 8439: }
8440: MKNDV(nd_nvar,mr0,len,r);
8441: return r;
8442: }
8443: }
8444:
8445: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
8446: {
8447: ND_pairs t;
8448: NODE sp0,sp;
8449: int stat;
8450: ND spol;
8451:
8452: for ( t = l; t; t = NEXT(t) ) {
8453: stat = nd_sp(m,trace,t,&spol);
8454: if ( !stat ) return 0;
8455: if ( spol ) {
8456: add_pbucket_symbolic(bucket,spol);
8457: }
8458: }
8459: return 1;
8460: }
8461:
8462: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
8463: {
8464: NODE rp0,rp;
8465: NM mul,head,s0,s;
8466: int index,col,i,sugar;
8467: RHist h;
8468: UINT *s0v,*p;
8469: NM_ind_pair pair;
8470: ND red;
8471: NDV *ps;
8472:
8473: s0 = 0; rp0 = 0; col = 0;
8474: if ( nd_demand )
8475: ps = trace?nd_ps_trace_sym:nd_ps_sym;
8476: else
8477: ps = trace?nd_ps_trace:nd_ps;
8478: while ( 1 ) {
8479: head = remove_head_pbucket_symbolic(bucket);
8480: if ( !head ) break;
8481: if ( !s0 ) s0 = head;
8482: else NEXT(s) = head;
8483: s = head;
8484: index = ndl_find_reducer(DL(head));
8485: if ( index >= 0 ) {
8486: h = nd_psh[index];
8487: NEWNM(mul);
8488: ndl_sub(DL(head),DL(h),DL(mul));
8489: if ( ndl_check_bound2(index,DL(mul)) )
8490: return 0;
8491: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 8492: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 8493: red = ndv_mul_nm_symbolic(mul,ps[index]);
8494: add_pbucket_symbolic(bucket,nd_remove_head(red));
8495: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
8496: }
8497: col++;
8498: }
8499: if ( rp0 ) NEXT(rp) = 0;
8500: NEXT(s) = 0;
8501: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
8502: for ( i = 0, p = s0v, s = s0; i < col;
8503: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
8504: *s0vect = s0v;
8505: *r = rp0;
8506: return col;
8507: }
8508:
8509: void print_ndp(ND_pairs l)
8510: {
8511: ND_pairs t;
8512:
8513: for ( t = l; t; t = NEXT(t) )
8514: printf("[%d,%d] ",t->i1,t->i2);
8515: printf("\n");
8516: }
8517:
8518: NODE nd_f4(int m,int checkonly,int **indp)
8519: {
8520: int i,nh,stat,index,f4red;
8521: NODE r,g,tn0,tn,node;
8522: ND_pairs d,l,t,ll0,ll,lh;
8523: LIST l0,l1;
8524: ND spol,red;
8525: NDV nf,redv;
8526: NM s0,s;
8527: NODE rp0,srp0,nflist,nzlist,nzlist_t;
8528: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
8529: UINT c;
8530: UINT **spmat;
8531: UINT *s0vect,*svect,*p,*v;
8532: int *colstat;
8533: IndArray *imat;
8534: int *rhead;
8535: int spcol,sprow;
8536: int sugar,sugarh;
8537: PGeoBucket bucket;
8538: struct oEGT eg0,eg1,eg_f4;
8539: Z i1,i2,sugarq;
1.12 noro 8540:
8541: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 8542: #if 0
8543: ndv_alloc = 0;
8544: #endif
1.11 noro 8545: Nf4_red=0;
1.1 noro 8546: g = 0; d = 0;
8547: for ( i = 0; i < nd_psn; i++ ) {
8548: d = update_pairs(d,g,i,0);
8549: g = update_base(g,i);
8550: }
8551: nzlist = 0;
8552: nzlist_t = nd_nzlist;
8553: f4red = 1;
8554: nd_last_nonzero = 0;
8555: while ( d ) {
8556: get_eg(&eg0);
8557: l = nd_minsugarp(d,&d);
8558: sugar = nd_sugarweight?l->sugar2:SG(l);
8559: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8560: if ( nzlist_t ) {
8561: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 8562: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 8563: tn = BDY((LIST)ARG1(node));
8564: if ( !tn ) {
8565: nzlist_t = NEXT(nzlist_t);
8566: continue;
8567: }
8568: /* tn = [[i1,i2],...] */
8569: lh = nd_ipairtospair(tn);
8570: }
8571: bucket = create_pbucket();
8572: stat = nd_sp_f4(m,0,l,bucket);
8573: if ( !stat ) {
8574: for ( t = l; NEXT(t); t = NEXT(t) );
8575: NEXT(t) = d; d = l;
8576: d = nd_reconstruct(0,d);
8577: continue;
8578: }
8579: if ( bucket->m < 0 ) continue;
8580: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8581: if ( !col ) {
8582: for ( t = l; NEXT(t); t = NEXT(t) );
8583: NEXT(t) = d; d = l;
8584: d = nd_reconstruct(0,d);
8585: continue;
8586: }
1.12 noro 8587: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 8588: if ( DP_Print )
1.6 noro 8589: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 8590: sugar,eg_f4.exectime);
1.1 noro 8591: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
8592: if ( checkonly && nflist ) return 0;
8593: /* adding new bases */
8594: if ( nflist ) nd_last_nonzero = f4red;
8595: for ( r = nflist; r; r = NEXT(r) ) {
8596: nf = (NDV)BDY(r);
1.38 noro 8597: if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1 noro 8598: ndv_removecont(m,nf);
8599: if ( !m && nd_nalg ) {
8600: ND nf1;
8601:
8602: nf1 = ndvtond(m,nf);
8603: nd_monic(0,&nf1);
8604: nd_removecont(m,nf1);
8605: nf = ndtondv(m,nf1);
8606: }
1.24 noro 8607: nh = ndv_newps(m,nf,0);
1.1 noro 8608: d = update_pairs(d,g,nh,0);
8609: g = update_base(g,nh);
8610: }
8611: if ( DP_Print ) {
8612: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8613: }
8614: if ( nd_gentrace ) {
8615: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8616: NEXTNODE(tn0,tn);
1.6 noro 8617: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8618: node = mknode(2,i1,i2); MKLIST(l0,node);
8619: BDY(tn) = l0;
8620: }
8621: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8622: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8623: MKNODE(node,l1,nzlist); nzlist = node;
8624: }
8625: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8626: f4red++;
8627: if ( nd_f4red && f4red > nd_f4red ) break;
8628: if ( nd_rank0 && !nflist ) break;
8629: }
8630: if ( nd_gentrace ) {
8631: MKLIST(l0,reverse_node(nzlist));
8632: MKNODE(nd_alltracelist,l0,0);
8633: }
8634: #if 0
8635: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8636: #endif
1.12 noro 8637: if ( DP_Print ) {
8638: fprintf(asir_out,"number of red=%d,",Nf4_red);
8639: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8640: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41 noro 8641: fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12 noro 8642: }
1.1 noro 8643: conv_ilist(nd_demand,0,g,indp);
8644: return g;
8645: }
8646:
8647: NODE nd_f4_trace(int m,int **indp)
8648: {
8649: int i,nh,stat,index;
8650: NODE r,g;
8651: ND_pairs d,l,l0,t;
8652: ND spol,red;
8653: NDV nf,redv,nfqv,nfv;
8654: NM s0,s;
8655: NODE rp0,srp0,nflist;
8656: int nsp,nred,col,rank,len,k,j,a;
8657: UINT c;
8658: UINT **spmat;
8659: UINT *s0vect,*svect,*p,*v;
8660: int *colstat;
8661: IndArray *imat;
8662: int *rhead;
8663: int spcol,sprow;
8664: int sugar;
8665: PGeoBucket bucket;
8666: struct oEGT eg0,eg1,eg_f4;
8667:
8668: g = 0; d = 0;
8669: for ( i = 0; i < nd_psn; i++ ) {
8670: d = update_pairs(d,g,i,0);
8671: g = update_base(g,i);
8672: }
8673: while ( d ) {
8674: get_eg(&eg0);
8675: l = nd_minsugarp(d,&d);
8676: sugar = SG(l);
8677: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8678: bucket = create_pbucket();
8679: stat = nd_sp_f4(m,0,l,bucket);
8680: if ( !stat ) {
8681: for ( t = l; NEXT(t); t = NEXT(t) );
8682: NEXT(t) = d; d = l;
8683: d = nd_reconstruct(1,d);
8684: continue;
8685: }
8686: if ( bucket->m < 0 ) continue;
8687: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8688: if ( !col ) {
8689: for ( t = l; NEXT(t); t = NEXT(t) );
8690: NEXT(t) = d; d = l;
8691: d = nd_reconstruct(1,d);
8692: continue;
8693: }
8694: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8695: if ( DP_Print )
1.3 noro 8696: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8697: sugar,eg_f4.exectime);
1.1 noro 8698: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8699: if ( !l0 ) continue;
8700: l = l0;
8701:
8702: /* over Q */
8703: bucket = create_pbucket();
8704: stat = nd_sp_f4(0,1,l,bucket);
8705: if ( !stat ) {
8706: for ( t = l; NEXT(t); t = NEXT(t) );
8707: NEXT(t) = d; d = l;
8708: d = nd_reconstruct(1,d);
8709: continue;
8710: }
8711: if ( bucket->m < 0 ) continue;
8712: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8713: if ( !col ) {
8714: for ( t = l; NEXT(t); t = NEXT(t) );
8715: NEXT(t) = d; d = l;
8716: d = nd_reconstruct(1,d);
8717: continue;
8718: }
8719: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8720: /* adding new bases */
8721: for ( r = nflist; r; r = NEXT(r) ) {
8722: nfqv = (NDV)BDY(r);
8723: ndv_removecont(0,nfqv);
1.6 noro 8724: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8725: if ( nd_nalg ) {
8726: ND nf1;
8727:
8728: nf1 = ndvtond(m,nfqv);
8729: nd_monic(0,&nf1);
8730: nd_removecont(0,nf1);
8731: nfqv = ndtondv(0,nf1); nd_free(nf1);
8732: }
8733: nfv = ndv_dup(0,nfqv);
8734: ndv_mod(m,nfv);
8735: ndv_removecont(m,nfv);
1.24 noro 8736: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8737: d = update_pairs(d,g,nh,0);
8738: g = update_base(g,nh);
8739: }
8740: }
8741: #if 0
8742: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8743: #endif
8744: conv_ilist(nd_demand,1,g,indp);
8745: return g;
8746: }
8747:
8748: int rref(matrix mat,int *sugar)
8749: {
8750: int row,col,i,j,k,l,s,wcol,wj;
8751: unsigned long bj;
8752: unsigned long **a;
8753: unsigned long *ai,*ak,*as,*t;
8754: int *pivot;
8755:
8756: row = mat->row;
8757: col = mat->col;
8758: a = mat->a;
8759: wcol = (col+BLEN-1)/BLEN;
8760: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8761: i = 0;
8762: for ( j = 0; j < col; j++ ) {
8763: wj = j/BLEN; bj = 1L<<(j%BLEN);
8764: for ( k = i; k < row; k++ )
8765: if ( a[k][wj] & bj ) break;
8766: if ( k == row ) continue;
8767: pivot[i] = j;
8768: if ( k != i ) {
8769: t = a[i]; a[i] = a[k]; a[k] = t;
8770: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8771: }
8772: ai = a[i];
8773: for ( k = i+1; k < row; k++ ) {
8774: ak = a[k];
8775: if ( ak[wj] & bj ) {
8776: for ( l = wj; l < wcol; l++ )
8777: ak[l] ^= ai[l];
8778: sugar[k] = MAX(sugar[k],sugar[i]);
8779: }
8780: }
8781: i++;
8782: }
8783: for ( k = i-1; k >= 0; k-- ) {
8784: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8785: ak = a[k];
8786: for ( s = 0; s < k; s++ ) {
8787: as = a[s];
8788: if ( as[wj] & bj ) {
8789: for ( l = wj; l < wcol; l++ )
8790: as[l] ^= ak[l];
8791: sugar[s] = MAX(sugar[s],sugar[k]);
8792: }
8793: }
8794: }
8795: return i;
8796: }
8797:
8798: void print_matrix(matrix mat)
8799: {
8800: int row,col,i,j;
8801: unsigned long *ai;
8802:
8803: row = mat->row;
8804: col = mat->col;
8805: printf("%d x %d\n",row,col);
8806: for ( i = 0; i < row; i++ ) {
8807: ai = mat->a[i];
8808: for ( j = 0; j < col; j++ ) {
8809: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8810: else putchar('0');
8811: }
8812: putchar('\n');
8813: }
8814: }
8815:
8816: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8817:
8818: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8819: {
8820: int row,col,wcol,wj,i,j;
8821: unsigned long bj;
8822: unsigned long *ai;
8823: unsigned long **a;
8824: int len;
8825: int *pos;
8826:
8827: row = mat->row;
8828: col = mat->col;
8829: wcol = (col+BLEN-1)/BLEN;
8830: pos = (int *)MALLOC(wcol*sizeof(int));
8831: bzero(pos,wcol*sizeof(int));
8832: for ( i = j = 0; i < wcol; i++ )
8833: if ( v[i] ) pos[j++] = i;;
8834: len = j;
8835: wj = rhead/BLEN;
8836: bj = 1L<<rhead%BLEN;
8837: a = mat->a;
8838: for ( i = 0; i < row; i++ ) {
8839: ai = a[i];
8840: if ( ai[wj]&bj ) {
8841: for ( j = 0; j < len; j++ )
8842: ai[pos[j]] ^= v[pos[j]];
8843: sugar[i] = MAX(sugar[i],rsugar);
8844: }
8845: }
8846: }
8847:
8848: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8849: {
8850: int nsp,nred,i,i0,k,rank,row;
8851: NODE r0,rp;
8852: ND_pairs sp;
8853: ND spol;
8854: NM_ind_pair rt;
8855: int *s0hash;
8856: UINT *s;
8857: int *pivot,*sugar,*head;
8858: matrix mat;
8859: NM m;
8860: NODE r;
8861: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8862: int rhead,rsugar,size;
8863: unsigned long *v;
8864:
8865: get_eg(&eg0);
8866: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8867: nred = length(rp0);
8868: mat = alloc_matrix(nsp,col);
8869: s0hash = (int *)MALLOC(col*sizeof(int));
8870: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8871: s0hash[i] = ndl_hash_value(s);
8872:
8873: sugar = (int *)MALLOC(nsp*sizeof(int));
8874: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8875: nd_sp(2,0,sp,&spol);
8876: if ( spol ) {
8877: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8878: sugar[i] = SG(spol);
8879: i++;
8880: }
8881: }
8882: mat->row = i;
8883: if ( DP_Print ) {
8884: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8885: }
8886: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8887: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8888: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8889: rt = (NM_ind_pair)BDY(rp);
8890: bzero(v,size);
8891: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8892: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8893: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8894: }
8895:
8896: get_eg(&eg1);
8897: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8898: rank = rref(mat,sugar);
8899:
8900: for ( i = 0, r0 = 0; i < rank; i++ ) {
8901: NEXTNODE(r0,r);
8902: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8903: SG((NDV)BDY(r)) = sugar[i];
8904: }
8905: if ( r0 ) NEXT(r) = 0;
8906: get_eg(&eg2);
8907: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8908: if ( DP_Print ) {
8909: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8910: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8911: fflush(asir_out);
8912: }
8913: return r0;
8914: }
8915:
8916:
8917: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8918: {
8919: IndArray *imat;
1.11 noro 8920: int nsp,nred,i,start;
1.1 noro 8921: int *rhead;
8922: NODE r0,rp;
8923: ND_pairs sp;
8924: NM_ind_pair *rvect;
8925: UINT *s;
8926: int *s0hash;
1.11 noro 8927: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8928:
8929: if ( m == 2 && nd_rref2 )
8930: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8931:
8932: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8933: nred = length(rp0);
8934: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8935: rhead = (int *)MALLOC(col*sizeof(int));
8936: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8937:
8938: /* construction of index arrays */
1.11 noro 8939: get_eg(&eg0);
1.1 noro 8940: if ( DP_Print ) {
1.11 noro 8941: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8942: fflush(asir_out);
1.1 noro 8943: }
8944: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8945: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8946: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8947: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8948: rhead[imat[i]->head] = 1;
1.11 noro 8949: start = imat[i]->head;
8950: }
1.12 noro 8951: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8952: if ( DP_Print ) {
8953: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8954: fflush(asir_out);
1.1 noro 8955: }
8956: if ( m > 0 )
1.7 noro 8957: #if SIZEOF_LONG==8
1.1 noro 8958: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8959: #else
8960: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8961: #endif
8962: else if ( m == -1 )
8963: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8964: else if ( m == -2 )
8965: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8966: else
8967: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8968: return r0;
8969: }
8970:
8971: /* for Fp, 2<=p<2^16 */
8972:
8973: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8974: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8975: {
8976: int spcol,sprow,a;
8977: int i,j,k,l,rank;
8978: NODE r0,r;
8979: ND_pairs sp;
8980: ND spol;
8981: UINT **spmat;
8982: UINT *svect,*v;
8983: int *colstat;
8984: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8985: int maxrs;
8986: int *spsugar;
8987: ND_pairs *spactive;
8988:
8989: spcol = col-nred;
8990: get_eg(&eg0);
8991: /* elimination (1st step) */
8992: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8993: svect = (UINT *)MALLOC(col*sizeof(UINT));
8994: spsugar = (int *)MALLOC(nsp*sizeof(int));
8995: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8996: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8997: nd_sp(m,0,sp,&spol);
8998: if ( !spol ) continue;
8999: nd_to_vect(m,s0vect,col,spol,svect);
9000: if ( m == -1 )
9001: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9002: else
1.40 noro 9003: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1 noro 9004: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9005: if ( i < col ) {
9006: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9007: for ( j = k = 0; j < col; j++ )
9008: if ( !rhead[j] ) v[k++] = svect[j];
9009: spsugar[sprow] = MAX(maxrs,SG(spol));
9010: if ( nz )
9011: spactive[sprow] = sp;
9012: sprow++;
9013: }
9014: nd_free(spol);
9015: }
9016: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9017: if ( DP_Print ) {
1.5 noro 9018: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9019: fflush(asir_out);
9020: }
9021: /* free index arrays */
9022: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9023:
9024: /* elimination (2nd step) */
9025: colstat = (int *)MALLOC(spcol*sizeof(int));
9026: if ( m == -1 )
9027: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9028: else
9029: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9030: r0 = 0;
9031: for ( i = 0; i < rank; i++ ) {
9032: NEXTNODE(r0,r); BDY(r) =
9033: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9034: SG((NDV)BDY(r)) = spsugar[i];
9035: GCFREE(spmat[i]);
9036: }
9037: if ( r0 ) NEXT(r) = 0;
9038:
9039: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9040: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9041: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9042: if ( DP_Print ) {
1.5 noro 9043: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9044: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9045: nsp,nred,sprow,spcol,rank);
1.5 noro 9046: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9047: }
9048: if ( nz ) {
9049: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9050: if ( rank > 0 ) {
9051: NEXT(spactive[rank-1]) = 0;
9052: *nz = spactive[0];
9053: } else
9054: *nz = 0;
9055: }
9056: return r0;
9057: }
9058:
1.40 noro 9059: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9060: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
9061: {
9062: int spcol,sprow,a;
9063: int i,j,k,l,rank;
9064: NODE r0,r;
9065: ND_pairs sp;
9066: ND spol;
9067: UINT **spmat;
9068: UINT *svect,*cvect;
9069: UINT *v;
9070: int *colstat;
9071: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9072: int maxrs;
9073: int *spsugar;
9074: ND_pairs *spactive;
9075: SIG *spsig;
9076:
9077: get_eg(&eg0);
9078: /* elimination (1st step) */
9079: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9080: spsugar = (int *)MALLOC(nsp*sizeof(int));
9081: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
9082: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9083: nd_sp(m,0,sp,&spol);
9084: if ( !spol ) {
9085: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9086: continue;
9087: }
9088: svect = (UINT *)MALLOC(col*sizeof(UINT));
9089: nd_to_vect(m,s0vect,col,spol,svect);
9090: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
9091: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9092: if ( i < col ) {
9093: spmat[sprow] = svect;
9094: spsugar[sprow] = MAX(maxrs,SG(spol));
9095: spsig[sprow] = sp->sig;
9096: sprow++;
9097: } else {
9098: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
9099: }
9100: nd_free(spol);
9101: }
9102: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
9103: if ( DP_Print ) {
9104: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9105: fflush(asir_out);
9106: }
9107: /* free index arrays */
9108: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9109:
9110: /* elimination (2nd step) */
9111: colstat = (int *)MALLOC(col*sizeof(int));
9112: rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
9113: r0 = 0;
9114: for ( i = 0; i < sprow; i++ ) {
9115: if ( spsugar[i] >= 0 ) {
9116: NEXTNODE(r0,r);
9117: BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
9118: SG((NDV)BDY(r)) = spsugar[i];
9119: ((NDV)BDY(r))->sig = spsig[i];
9120: } else
9121: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
9122: GCFREE(spmat[i]);
9123: }
9124: if ( r0 ) NEXT(r) = 0;
9125: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
9126: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9127: if ( DP_Print ) {
9128: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9129: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9130: nsp,nred,sprow,col,rank);
9131: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9132: }
9133: return r0;
9134: }
9135:
1.1 noro 9136:
9137: /* for small finite fields */
9138:
9139: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9140: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9141: {
9142: int spcol,sprow,a;
9143: int i,j,k,l,rank;
9144: NODE r0,r;
9145: ND_pairs sp;
9146: ND spol;
9147: UINT **spmat;
9148: UINT *svect,*v;
9149: int *colstat;
9150: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9151: int maxrs;
9152: int *spsugar;
9153: ND_pairs *spactive;
9154:
9155: spcol = col-nred;
9156: get_eg(&eg0);
9157: /* elimination (1st step) */
9158: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
9159: svect = (UINT *)MALLOC(col*sizeof(UINT));
9160: spsugar = (int *)MALLOC(nsp*sizeof(int));
9161: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9162: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9163: nd_sp(m,0,sp,&spol);
9164: if ( !spol ) continue;
9165: nd_to_vect(m,s0vect,col,spol,svect);
9166: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
9167: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9168: if ( i < col ) {
9169: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
9170: for ( j = k = 0; j < col; j++ )
9171: if ( !rhead[j] ) v[k++] = svect[j];
9172: spsugar[sprow] = MAX(maxrs,SG(spol));
9173: if ( nz )
9174: spactive[sprow] = sp;
9175: sprow++;
9176: }
9177: nd_free(spol);
9178: }
9179: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9180: if ( DP_Print ) {
1.5 noro 9181: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9182: fflush(asir_out);
9183: }
9184: /* free index arrays */
9185: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9186:
9187: /* elimination (2nd step) */
9188: colstat = (int *)MALLOC(spcol*sizeof(int));
9189: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
9190: r0 = 0;
9191: for ( i = 0; i < rank; i++ ) {
9192: NEXTNODE(r0,r); BDY(r) =
9193: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9194: SG((NDV)BDY(r)) = spsugar[i];
9195: GCFREE(spmat[i]);
9196: }
9197: if ( r0 ) NEXT(r) = 0;
9198:
9199: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
9200: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9201: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9202: if ( DP_Print ) {
1.5 noro 9203: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9204: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9205: nsp,nred,sprow,spcol,rank);
1.5 noro 9206: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9207: }
9208: if ( nz ) {
9209: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9210: if ( rank > 0 ) {
9211: NEXT(spactive[rank-1]) = 0;
9212: *nz = spactive[0];
9213: } else
9214: *nz = 0;
9215: }
9216: return r0;
9217: }
9218:
9219: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9220: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9221: {
9222: int spcol,sprow,a;
9223: int i,j,k,l,rank;
9224: NODE r0,r;
9225: ND_pairs sp;
9226: ND spol;
9227: mpz_t **spmat;
9228: mpz_t *svect,*v;
9229: int *colstat;
9230: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9231: int maxrs;
9232: int *spsugar;
9233: pointer *w;
9234:
9235: spcol = col-nred;
9236: get_eg(&eg0);
9237: /* elimination (1st step) */
9238: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
9239: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
9240: spsugar = (int *)MALLOC(nsp*sizeof(int));
9241: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9242: nd_sp(m,trace,sp,&spol);
9243: if ( !spol ) continue;
9244: nd_to_vect_lf(s0vect,col,spol,svect);
9245: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
9246: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
9247: if ( i < col ) {
9248: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
9249: for ( j = k = 0; j < col; j++ )
9250: if ( !rhead[j] ) v[k++][0] = svect[j][0];
9251: spsugar[sprow] = MAX(maxrs,SG(spol));
9252: sprow++;
9253: }
9254: /* nd_free(spol); */
9255: }
9256: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9257: if ( DP_Print ) {
1.5 noro 9258: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9259: fflush(asir_out);
9260: }
9261: /* free index arrays */
9262: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9263:
9264: /* elimination (2nd step) */
9265: colstat = (int *)MALLOC(spcol*sizeof(int));
9266: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
9267: w = (pointer *)MALLOC(rank*sizeof(pointer));
9268: for ( i = 0; i < rank; i++ ) {
9269: #if 0
9270: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9271: SG((NDV)w[rank-i-1]) = spsugar[i];
9272: #else
9273: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
9274: SG((NDV)w[i]) = spsugar[i];
9275: #endif
9276: /* GCFREE(spmat[i]); */
9277:
9278: }
9279: #if 0
9280: qsort(w,rank,sizeof(NDV),
9281: (int (*)(const void *,const void *))ndv_compare);
9282: #endif
9283: r0 = 0;
9284: for ( i = 0; i < rank; i++ ) {
9285: NEXTNODE(r0,r); BDY(r) = w[i];
9286: }
9287: if ( r0 ) NEXT(r) = 0;
9288:
9289: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9290: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9291: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9292: if ( DP_Print ) {
1.5 noro 9293: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9294: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9295: nsp,nred,sprow,spcol,rank);
1.5 noro 9296: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9297: }
9298: return r0;
9299: }
9300:
9301: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
9302: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
9303: {
9304: int spcol,sprow,a;
9305: int i,j,k,l,rank;
9306: NODE r0,r;
9307: ND_pairs sp;
9308: ND spol;
9309: Z **spmat;
9310: Z *svect,*v;
9311: int *colstat;
9312: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9313: int maxrs;
9314: int *spsugar;
9315: pointer *w;
9316:
9317: spcol = col-nred;
9318: get_eg(&eg0);
9319: /* elimination (1st step) */
9320: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
9321: svect = (Z *)MALLOC(col*sizeof(Q));
9322: spsugar = (int *)MALLOC(nsp*sizeof(int));
9323: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9324: nd_sp(0,trace,sp,&spol);
9325: if ( !spol ) continue;
9326: nd_to_vect_q(s0vect,col,spol,svect);
9327: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
9328: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9329: if ( i < col ) {
9330: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
9331: for ( j = k = 0; j < col; j++ )
9332: if ( !rhead[j] ) v[k++] = svect[j];
9333: spsugar[sprow] = MAX(maxrs,SG(spol));
9334: sprow++;
9335: }
9336: /* nd_free(spol); */
9337: }
9338: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
9339: if ( DP_Print ) {
1.5 noro 9340: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 9341: fflush(asir_out);
9342: }
9343: /* free index arrays */
9344: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
9345:
9346: /* elimination (2nd step) */
9347: colstat = (int *)MALLOC(spcol*sizeof(int));
9348: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
9349: w = (pointer *)MALLOC(rank*sizeof(pointer));
9350: for ( i = 0; i < rank; i++ ) {
9351: #if 0
9352: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9353: SG((NDV)w[rank-i-1]) = spsugar[i];
9354: #else
9355: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
9356: SG((NDV)w[i]) = spsugar[i];
9357: #endif
9358: /* GCFREE(spmat[i]); */
9359: }
9360: #if 0
9361: qsort(w,rank,sizeof(NDV),
9362: (int (*)(const void *,const void *))ndv_compare);
9363: #endif
9364: r0 = 0;
9365: for ( i = 0; i < rank; i++ ) {
9366: NEXTNODE(r0,r); BDY(r) = w[i];
9367: }
9368: if ( r0 ) NEXT(r) = 0;
9369:
9370: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
9371: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
9372: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9373: if ( DP_Print ) {
1.5 noro 9374: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 9375: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9376: nsp,nred,sprow,spcol,rank);
1.5 noro 9377: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 9378: }
9379: return r0;
9380: }
9381:
9382: FILE *nd_write,*nd_read;
9383:
9384: void nd_send_int(int a) {
9385: write_int(nd_write,(unsigned int *)&a);
9386: }
9387:
9388: void nd_send_intarray(int *p,int len) {
9389: write_intarray(nd_write,(unsigned int *)p,len);
9390: }
9391:
9392: int nd_recv_int() {
9393: int a;
9394:
9395: read_int(nd_read,(unsigned int *)&a);
9396: return a;
9397: }
9398:
9399: void nd_recv_intarray(int *p,int len) {
9400: read_intarray(nd_read,(unsigned int *)p,len);
9401: }
9402:
9403: void nd_send_ndv(NDV p) {
9404: int len,i;
9405: NMV m;
9406:
9407: if ( !p ) nd_send_int(0);
9408: else {
9409: len = LEN(p);
9410: nd_send_int(len);
9411: m = BDY(p);
9412: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9413: nd_send_int(CM(m));
9414: nd_send_intarray((int *)DL(m),nd_wpd);
9415: }
9416: }
9417: }
9418:
9419: void nd_send_nd(ND p) {
9420: int len,i;
9421: NM m;
9422:
9423: if ( !p ) nd_send_int(0);
9424: else {
9425: len = LEN(p);
9426: nd_send_int(len);
9427: m = BDY(p);
9428: for ( i = 0; i < len; i++, m = NEXT(m) ) {
9429: nd_send_int(CM(m));
9430: nd_send_intarray((int *)DL(m),nd_wpd);
9431: }
9432: }
9433: }
9434:
9435: NDV nd_recv_ndv()
9436: {
9437: int len,i;
9438: NMV m,m0;
9439: NDV r;
9440:
9441: len = nd_recv_int();
9442: if ( !len ) return 0;
9443: else {
9444: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9445: #if 0
9446: ndv_alloc += len*nmv_adv;
9447: #endif
9448: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9449: CM(m) = nd_recv_int();
9450: nd_recv_intarray((int *)DL(m),nd_wpd);
9451: }
9452: MKNDV(nd_nvar,m0,len,r);
9453: return r;
9454: }
9455: }
9456:
9457: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
9458: {
9459: int i,j,t,c,rank,inv;
9460: int *ci,*ri;
9461: Z dn;
9462: MAT m,nm;
9463:
9464: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
9465: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
9466: for ( i = 0; i < row; i++ )
9467: for ( j = 0; j < col; j++ )
9468: mat0[i][j] = 0;
9469: c = col-rank;
9470: for ( i = 0; i < rank; i++ ) {
9471: mat0[i][ri[i]] = dn;
9472: for ( j = 0; j < c; j++ )
9473: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
9474: }
9475: return rank;
9476: }
9477:
9478: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9479: {
9480: int i,j,k,l,inv,a,rank,s;
9481: unsigned int *t,*pivot,*pk;
9482: unsigned int **mat;
9483: ND_pairs pair;
9484:
9485: mat = (unsigned int **)mat0;
9486: for ( rank = 0, j = 0; j < col; j++ ) {
9487: for ( i = rank; i < row; i++ )
9488: mat[i][j] %= md;
9489: for ( i = rank; i < row; i++ )
9490: if ( mat[i][j] )
9491: break;
9492: if ( i == row ) {
9493: colstat[j] = 0;
9494: continue;
9495: } else
9496: colstat[j] = 1;
9497: if ( i != rank ) {
9498: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9499: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9500: if ( spactive ) {
9501: pair = spactive[i]; spactive[i] = spactive[rank];
9502: spactive[rank] = pair;
9503: }
9504: }
9505: pivot = mat[rank];
9506: s = sugar[rank];
9507: inv = invm(pivot[j],md);
9508: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9509: if ( *pk ) {
9510: if ( *pk >= (unsigned int)md )
9511: *pk %= md;
9512: DMAR(*pk,inv,0,md,*pk)
9513: }
9514: for ( i = rank+1; i < row; i++ ) {
9515: t = mat[i];
9516: if ( (a = t[j]) != 0 ) {
9517: sugar[i] = MAX(sugar[i],s);
9518: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9519: }
9520: }
9521: rank++;
9522: }
9523: for ( j = col-1, l = rank-1; j >= 0; j-- )
9524: if ( colstat[j] ) {
9525: pivot = mat[l];
9526: s = sugar[l];
9527: for ( i = 0; i < l; i++ ) {
9528: t = mat[i];
9529: t[j] %= md;
9530: if ( (a = t[j]) != 0 ) {
9531: sugar[i] = MAX(sugar[i],s);
9532: red_by_vect(md,t+j,pivot+j,md-a,col-j);
9533: }
9534: }
9535: l--;
9536: }
9537: for ( j = 0, l = 0; l < rank; j++ )
9538: if ( colstat[j] ) {
9539: t = mat[l];
9540: for ( k = j; k < col; k++ )
9541: if ( t[k] >= (unsigned int)md )
9542: t[k] %= md;
9543: l++;
9544: }
9545: return rank;
9546: }
9547:
1.40 noro 9548: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
9549: {
9550: int i,j,k,l,rank,s,imin;
9551: UINT inv;
9552: UINT a;
9553: UINT *t,*pivot,*pk;
9554: UINT *ck;
9555: UINT *ct;
9556: ND_pairs pair;
9557: SIG sg;
9558: int *used;
9559:
9560: used = (int *)MALLOC(row*sizeof(int));
9561: for ( j = 0; j < col; j++ ) {
9562: for ( i = 0; i < row; i++ )
9563: a = mat[i][j] %= md;
9564: for ( i = 0; i < row; i++ )
9565: if ( !used[i] && mat[i][j] ) break;
9566: if ( i == row ) {
9567: colstat[j] = 0;
9568: continue;
9569: } else {
9570: colstat[j] = 1;
9571: used[i] = 1;
9572: }
9573: /* column j is normalized */
9574: s = sugar[i];
9575: inv = invm(mat[i][j],md);
9576: /* normalize pivot row */
9577: for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
9578: DMAR(*pk,inv,0,md,*pk);
9579: }
9580: for ( k = i+1; k < row; k++ ) {
9581: if ( (a = mat[k][j]) != 0 ) {
9582: sugar[k] = MAX(sugar[k],s);
9583: red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
9584: Nf4_red++;
9585: }
9586: }
9587: }
9588: rank = 0;
9589: for ( i = 0; i < row; i++ ) {
9590: for ( j = 0; j < col; j++ )
9591: if ( mat[i][j] ) break;
9592: if ( j == col ) sugar[i] = -1;
9593: else rank++;
9594: }
9595: return rank;
9596: }
9597:
1.1 noro 9598:
1.7 noro 9599: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 9600: {
1.7 noro 9601: int i,j,k,l,inv,a,rank,s;
9602: unsigned int *t,*pivot,*pk;
9603: unsigned int **mat;
9604:
9605: mat = (unsigned int **)mat0;
9606: for ( rank = 0, j = 0; j < col; j++ ) {
9607: for ( i = rank; i < row; i++ )
9608: if ( mat[i][j] )
9609: break;
9610: if ( i == row ) {
9611: colstat[j] = 0;
9612: continue;
9613: } else
9614: colstat[j] = 1;
9615: if ( i != rank ) {
9616: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9617: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9618: }
9619: pivot = mat[rank];
9620: s = sugar[rank];
9621: inv = _invsf(pivot[j]);
9622: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
9623: if ( *pk )
9624: *pk = _mulsf(*pk,inv);
9625: for ( i = rank+1; i < row; i++ ) {
9626: t = mat[i];
9627: if ( (a = t[j]) != 0 ) {
9628: sugar[i] = MAX(sugar[i],s);
9629: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9630: }
9631: }
9632: rank++;
9633: }
9634: for ( j = col-1, l = rank-1; j >= 0; j-- )
9635: if ( colstat[j] ) {
9636: pivot = mat[l];
9637: s = sugar[l];
9638: for ( i = 0; i < l; i++ ) {
9639: t = mat[i];
9640: if ( (a = t[j]) != 0 ) {
9641: sugar[i] = MAX(sugar[i],s);
9642: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
9643: }
9644: }
9645: l--;
9646: }
9647: return rank;
9648: }
1.1 noro 9649:
1.7 noro 9650: int ndv_ishomo(NDV p)
9651: {
9652: NMV m;
9653: int len,h;
1.1 noro 9654:
9655: if ( !p ) return 1;
9656: len = LEN(p);
9657: m = BDY(p);
9658: h = TD(DL(m));
9659: NMV_ADV(m);
9660: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 9661: if ( TD(DL(m)) != h ) {
9662: return 0;
9663: }
1.1 noro 9664: return 1;
9665: }
9666:
9667: void ndv_save(NDV p,int index)
9668: {
9669: FILE *s;
9670: char name[BUFSIZ];
9671: short id;
9672: int nv,sugar,len,n,i,td,e,j;
9673: NMV m;
9674: unsigned int *dl;
9675: int mpos;
9676:
9677: sprintf(name,"%s/%d",Demand,index);
9678: s = fopen(name,"w");
9679: savevl(s,0);
9680: if ( !p ) {
9681: saveobj(s,0);
9682: return;
9683: }
9684: id = O_DP;
9685: nv = NV(p);
9686: sugar = SG(p);
9687: len = LEN(p);
9688: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
9689: write_int(s,(unsigned int *)&len);
9690:
9691: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9692: saveobj(s,(Obj)CZ(m));
1.1 noro 9693: dl = DL(m);
9694: td = TD(dl);
9695: write_int(s,(unsigned int *)&td);
9696: for ( j = 0; j < nv; j++ ) {
9697: e = GET_EXP(dl,j);
9698: write_int(s,(unsigned int *)&e);
9699: }
9700: if ( nd_module ) {
9701: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
9702: }
9703: }
9704: fclose(s);
9705: }
9706:
9707: void nd_save_mod(ND p,int index)
9708: {
9709: FILE *s;
9710: char name[BUFSIZ];
9711: int nv,sugar,len,c;
9712: NM m;
9713:
9714: sprintf(name,"%s/%d",Demand,index);
9715: s = fopen(name,"w");
9716: if ( !p ) {
9717: len = 0;
9718: write_int(s,(unsigned int *)&len);
9719: fclose(s);
9720: return;
9721: }
9722: nv = NV(p);
9723: sugar = SG(p);
9724: len = LEN(p);
9725: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9726: for ( m = BDY(p); m; m = NEXT(m) ) {
9727: c = CM(m); write_int(s,(unsigned int *)&c);
9728: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9729: }
9730: fclose(s);
9731: }
9732:
9733: NDV ndv_load(int index)
9734: {
9735: FILE *s;
9736: char name[BUFSIZ];
9737: short id;
9738: int nv,sugar,len,n,i,td,e,j;
9739: NDV d;
9740: NMV m0,m;
9741: unsigned int *dl;
9742: Obj obj;
9743: int mpos;
9744:
9745: sprintf(name,"%s/%d",Demand,index);
9746: s = fopen(name,"r");
9747: if ( !s ) return 0;
9748:
9749: skipvl(s);
9750: read_short(s,(unsigned short *)&id);
9751: if ( !id ) return 0;
9752: read_int(s,(unsigned int *)&nv);
9753: read_int(s,(unsigned int *)&sugar);
9754: read_int(s,(unsigned int *)&len);
9755:
9756: m0 = m = MALLOC(len*nmv_adv);
9757: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9758: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9759: dl = DL(m);
9760: ndl_zero(dl);
9761: read_int(s,(unsigned int *)&td); TD(dl) = td;
9762: for ( j = 0; j < nv; j++ ) {
9763: read_int(s,(unsigned int *)&e);
9764: PUT_EXP(dl,j,e);
9765: }
9766: if ( nd_module ) {
9767: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9768: }
9769: if ( nd_blockmask ) ndl_weight_mask(dl);
9770: }
9771: fclose(s);
9772: MKNDV(nv,m0,len,d);
9773: SG(d) = sugar;
9774: return d;
9775: }
9776:
9777: ND nd_load_mod(int index)
9778: {
9779: FILE *s;
9780: char name[BUFSIZ];
9781: int nv,sugar,len,i,c;
9782: ND d;
9783: NM m0,m;
9784:
9785: sprintf(name,"%s/%d",Demand,index);
9786: s = fopen(name,"r");
9787: /* if the file does not exist, it means p[index]=0 */
9788: if ( !s ) return 0;
9789:
9790: read_int(s,(unsigned int *)&nv);
9791: if ( !nv ) { fclose(s); return 0; }
9792:
9793: read_int(s,(unsigned int *)&sugar);
9794: read_int(s,(unsigned int *)&len);
9795: for ( m0 = 0, i = 0; i < len; i++ ) {
9796: NEXTNM(m0,m);
9797: read_int(s,(unsigned int *)&c); CM(m) = c;
9798: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9799: }
9800: NEXT(m) = 0;
9801: MKND(nv,m0,len,d);
9802: SG(d) = sugar;
9803: fclose(s);
9804: return d;
9805: }
9806:
9807: void nd_det(int mod,MAT f,P *rp)
9808: {
9809: VL fv,tv;
9810: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9811: pointer **m;
9812: P **w;
9813: P mp,r;
9814: NDV **dm;
9815: NDV *t,*mi,*mj;
9816: NDV d,s,mij,mjj;
9817: ND u;
9818: NMV nmv;
9819: UINT *bound;
9820: PGeoBucket bucket;
9821: struct order_spec *ord;
9822: Z dq,dt,ds;
9823: Z mone;
9824: Z gn,qn,dn0,nm,dn;
9825:
9826: create_order_spec(0,0,&ord);
9827: nd_init_ord(ord);
9828: get_vars((Obj)f,&fv);
9829: if ( f->row != f->col )
9830: error("nd_det : non-square matrix");
9831: n = f->row;
9832: m = f->body;
9833: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9834:
9835: if ( !nvar ) {
9836: if ( !mod )
9837: detp(CO,(P **)m,n,rp);
9838: else {
9839: w = (P **)almat_pointer(n,n);
9840: for ( i = 0; i < n; i++ )
9841: for ( j = 0; j < n; j++ )
9842: ptomp(mod,(P)m[i][j],&w[i][j]);
9843: detmp(CO,mod,w,n,&mp);
9844: mptop(mp,rp);
9845: }
9846: return;
9847: }
9848:
9849: if ( !mod ) {
9850: w = (P **)almat_pointer(n,n);
9851: dq = ONE;
9852: for ( i = 0; i < n; i++ ) {
9853: dn0 = ONE;
9854: for ( j = 0; j < n; j++ ) {
9855: if ( !m[i][j] ) continue;
9856: lgp(m[i][j],&nm,&dn);
1.6 noro 9857: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9858: }
9859: if ( !UNIZ(dn0) ) {
9860: ds = dn0;
9861: for ( j = 0; j < n; j++ )
9862: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9863: mulz(dq,ds,&dt); dq = dt;
9864: } else
9865: for ( j = 0; j < n; j++ )
9866: w[i][j] = (P)m[i][j];
9867: }
9868: m = (pointer **)w;
9869: }
9870:
9871: for ( i = 0, max = 1; i < n; i++ )
9872: for ( j = 0; j < n; j++ )
9873: for ( tv = fv; tv; tv = NEXT(tv) ) {
9874: e = getdeg(tv->v,(P)m[i][j]);
9875: max = MAX(e,max);
9876: }
9877: nd_setup_parameters(nvar,max);
9878: dm = (NDV **)almat_pointer(n,n);
9879: for ( i = 0, max = 1; i < n; i++ )
9880: for ( j = 0; j < n; j++ ) {
9881: dm[i][j] = ptondv(CO,fv,m[i][j]);
9882: if ( mod ) ndv_mod(mod,dm[i][j]);
9883: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9884: }
9885: d = ptondv(CO,fv,(P)ONE);
9886: if ( mod ) ndv_mod(mod,d);
9887: chsgnz(ONE,&mone);
9888: for ( j = 0, sgn = 1; j < n; j++ ) {
9889: if ( DP_Print ) {
9890: fprintf(asir_out,".");
9891: }
9892: for ( i = j; i < n && !dm[i][j]; i++ );
9893: if ( i == n ) {
9894: *rp = 0;
9895: return;
9896: }
9897: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9898: for ( k = j; k < n; k++ )
9899: for ( l = j; l < n; l++ )
9900: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9901: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9902: }
9903: if ( k0 != j ) {
9904: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9905: sgn = -sgn;
9906: }
9907: if ( l0 != j ) {
9908: for ( k = j; k < n; k++ ) {
9909: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9910: }
9911: sgn = -sgn;
9912: }
9913: bound = nd_det_compute_bound(dm,n,j);
9914: for ( k = 0; k < nd_nvar; k++ )
9915: if ( bound[k]*2 > nd_mask0 ) break;
9916: if ( k < nd_nvar )
9917: nd_det_reconstruct(dm,n,j,d);
9918:
9919: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9920: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9921: mi = dm[i]; mij = mi[j];
9922: if ( mod )
9923: ndv_mul_c(mod,mij,mod-1);
9924: else
9925: ndv_mul_c_q(mij,mone);
9926: for ( k = j+1; k < n; k++ ) {
9927: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9928: bucket = create_pbucket();
9929: if ( mi[k] ) {
9930: nmv = BDY(mjj); len = LEN(mjj);
9931: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9932: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9933: add_pbucket(mod,bucket,u);
9934: }
9935: }
9936: if ( mj[k] && mij ) {
9937: nmv = BDY(mij); len = LEN(mij);
9938: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9939: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9940: add_pbucket(mod,bucket,u);
9941: }
9942: }
9943: u = nd_quo(mod,bucket,d);
9944: mi[k] = ndtondv(mod,u);
9945: }
9946: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9947: }
9948: d = mjj;
9949: }
9950: if ( DP_Print ) {
9951: fprintf(asir_out,"\n");
9952: }
9953: if ( sgn < 0 ) {
9954: if ( mod )
9955: ndv_mul_c(mod,d,mod-1);
9956: else
9957: ndv_mul_c_q(d,mone);
9958: }
9959: r = ndvtop(mod,CO,fv,d);
9960: if ( !mod && !UNIQ(dq) )
9961: divsp(CO,r,(P)dq,rp);
9962: else
9963: *rp = r;
9964: }
9965:
9966: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9967: {
9968: NM mr,mr0;
9969: NM tnm;
9970: NMV m;
9971: UINT *d0,*dt,*dm;
9972: int c,n,td,i,c1,c2,len;
9973: Z q;
9974: ND r;
9975:
9976: if ( !p ) return 0;
9977: else {
9978: n = NV(p); m = BDY(p); len = LEN(p);
9979: d0 = DL(m0);
9980: td = TD(d);
9981: mr0 = 0;
9982: NEWNM(tnm);
9983: if ( mod ) {
9984: c = CM(m0);
9985: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9986: ndl_add(DL(m),d0,DL(tnm));
9987: if ( ndl_reducible(DL(tnm),d) ) {
9988: NEXTNM(mr0,mr);
9989: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9990: ndl_copy(DL(tnm),DL(mr));
9991: }
9992: }
9993: } else {
1.6 noro 9994: q = CZ(m0);
1.1 noro 9995: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9996: ndl_add(DL(m),d0,DL(tnm));
9997: if ( ndl_reducible(DL(tnm),d) ) {
9998: NEXTNM(mr0,mr);
1.6 noro 9999: mulz(CZ(m),q,&CZ(mr));
1.1 noro 10000: ndl_copy(DL(tnm),DL(mr));
10001: }
10002: }
10003: }
10004: if ( !mr0 )
10005: return 0;
10006: else {
10007: NEXT(mr) = 0;
10008: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10009: MKND(NV(p),mr0,len,r);
10010: SG(r) = SG(p) + TD(d0);
10011: return r;
10012: }
10013: }
10014: }
10015:
10016: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
10017: {
10018: int i,obpe,oadv,h,k,l;
10019: static NM prev_nm_free_list;
10020: EPOS oepos;
10021:
10022: obpe = nd_bpe;
10023: oadv = nmv_adv;
10024: oepos = nd_epos;
10025: if ( obpe < 2 ) nd_bpe = 2;
10026: else if ( obpe < 3 ) nd_bpe = 3;
10027: else if ( obpe < 4 ) nd_bpe = 4;
10028: else if ( obpe < 5 ) nd_bpe = 5;
10029: else if ( obpe < 6 ) nd_bpe = 6;
10030: else if ( obpe < 8 ) nd_bpe = 8;
10031: else if ( obpe < 10 ) nd_bpe = 10;
10032: else if ( obpe < 16 ) nd_bpe = 16;
10033: else if ( obpe < 32 ) nd_bpe = 32;
10034: else error("nd_det_reconstruct : exponent too large");
10035:
10036: nd_setup_parameters(nd_nvar,0);
10037: prev_nm_free_list = _nm_free_list;
10038: _nm_free_list = 0;
10039: for ( k = j; k < n; k++ )
10040: for (l = j; l < n; l++ )
10041: ndv_realloc(dm[k][l],obpe,oadv,oepos);
10042: ndv_realloc(d,obpe,oadv,oepos);
10043: prev_nm_free_list = 0;
10044: #if 0
10045: GC_gcollect();
10046: #endif
10047: }
10048:
10049: /* returns a UINT array containing degree bounds */
10050:
10051: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
10052: {
10053: UINT *d0,*d1,*d,*t,*r;
10054: int k,l,i;
10055:
10056: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
10057: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
10058: for ( k = j; k < n; k++ )
10059: for ( l = j; l < n; l++ )
10060: if ( dm[k][l] ) {
10061: d = ndv_compute_bound(dm[k][l]);
10062: for ( i = 0; i < nd_nvar; i++ )
10063: d0[i] = MAX(d0[i],d[i]);
10064: }
10065: return d0;
10066: }
10067:
10068: DL nd_separate_d(UINT *d,UINT *trans)
10069: {
10070: int n,td,i,e,j;
10071: DL a;
10072:
10073: ndl_zero(trans);
10074: td = 0;
10075: for ( i = 0; i < nd_ntrans; i++ ) {
10076: e = GET_EXP(d,i);
10077: PUT_EXP(trans,i,e);
10078: td += MUL_WEIGHT(e,i);
10079: }
10080: if ( nd_ntrans+nd_nalg < nd_nvar ) {
10081: /* homogenized */
10082: i = nd_nvar-1;
10083: e = GET_EXP(d,i);
10084: PUT_EXP(trans,i,e);
10085: td += MUL_WEIGHT(e,i);
10086: }
10087: TD(trans) = td;
10088: if ( nd_blockmask) ndl_weight_mask(trans);
10089: NEWDL(a,nd_nalg);
10090: td = 0;
10091: for ( i = 0; i < nd_nalg; i++ ) {
10092: j = nd_ntrans+i;
10093: e = GET_EXP(d,j);
10094: a->d[i] = e;
10095: td += e;
10096: }
10097: a->td = td;
10098: return a;
10099: }
10100:
10101: int nd_monic(int mod,ND *p)
10102: {
10103: UINT *trans,*t;
10104: DL alg;
10105: MP mp0,mp;
10106: NM m,m0,m1,ma0,ma,mb,mr0,mr;
10107: ND r;
10108: DL dl;
10109: DP nm;
10110: NDV ndv;
10111: DAlg inv,cd;
10112: ND s,c;
10113: Z l,mul;
10114: Z ln;
10115: int n,ntrans,i,e,td,is_lc,len;
10116: NumberField nf;
10117: struct oEGT eg0,eg1;
10118:
10119: if ( !(nf = get_numberfield()) )
10120: error("nd_monic : current_numberfield is not set");
10121:
10122: /* Q coef -> DAlg coef */
10123: NEWNM(ma0); ma = ma0;
10124: m = BDY(*p);
10125: is_lc = 1;
10126: while ( 1 ) {
10127: NEWMP(mp0); mp = mp0;
1.6 noro 10128: mp->c = (Obj)CZ(m);
1.1 noro 10129: mp->dl = nd_separate_d(DL(m),DL(ma));
10130: NEWNM(mb);
10131: for ( m = NEXT(m); m; m = NEXT(m) ) {
10132: alg = nd_separate_d(DL(m),DL(mb));
10133: if ( !ndl_equal(DL(ma),DL(mb)) )
10134: break;
1.6 noro 10135: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 10136: }
10137: NEXT(mp) = 0;
10138: MKDP(nd_nalg,mp0,nm);
10139: MKDAlg(nm,ONE,cd);
10140: if ( is_lc == 1 ) {
10141: /* if the lc is a rational number, we have nothing to do */
10142: if ( !mp0->dl->td )
10143: return 1;
10144:
10145: get_eg(&eg0);
10146: invdalg(cd,&inv);
10147: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
10148: /* check the validity of inv */
10149: if ( mod && !remqi((Q)inv->dn,mod) )
10150: return 0;
10151: CA(ma) = nf->one;
10152: is_lc = 0;
10153: ln = ONE;
10154: } else {
10155: muldalg(cd,inv,&CA(ma));
10156: lcmz(ln,CA(ma)->dn,&ln);
10157: }
10158: if ( m ) {
10159: NEXT(ma) = mb; ma = mb;
10160: } else {
10161: NEXT(ma) = 0;
10162: break;
10163: }
10164: }
10165: /* l = lcm(denoms) */
10166: l = ln;
10167: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 10168: divsz(l,CA(m)->dn,&mul);
1.1 noro 10169: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
10170: NEXTNM(mr0,mr);
1.6 noro 10171: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 10172: dl = mp->dl;
10173: td = TD(DL(m));
10174: ndl_copy(DL(m),DL(mr));
10175: for ( i = 0; i < nd_nalg; i++ ) {
10176: e = dl->d[i];
10177: PUT_EXP(DL(mr),i+nd_ntrans,e);
10178: td += MUL_WEIGHT(e,i+nd_ntrans);
10179: }
10180: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
10181: TD(DL(mr)) = td;
10182: if ( nd_blockmask) ndl_weight_mask(DL(mr));
10183: }
10184: }
10185: NEXT(mr) = 0;
10186: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
10187: MKND(NV(*p),mr0,len,r);
10188: /* XXX */
10189: SG(r) = SG(*p);
10190: nd_free(*p);
10191: *p = r;
10192: return 1;
10193: }
10194:
10195: NODE reverse_node(NODE n)
10196: {
10197: NODE t,t1;
10198:
10199: for ( t = 0; n; n = NEXT(n) ) {
10200: MKNODE(t1,BDY(n),t); t = t1;
10201: }
10202: return t;
10203: }
10204:
10205: P ndc_div(int mod,union oNDC a,union oNDC b)
10206: {
10207: union oNDC c;
10208: int inv,t;
10209:
10210: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 10211: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 10212: else if ( mod ) {
10213: inv = invm(b.m,mod);
10214: DMAR(a.m,inv,0,mod,t); c.m = t;
10215: } else if ( nd_vc )
10216: divsp(nd_vc,a.p,b.p,&c.p);
10217: else
10218: divsz(a.z,b.z,&c.z);
10219: return ndctop(mod,c);
10220: }
10221:
10222: P ndctop(int mod,union oNDC c)
10223: {
10224: Z q;
10225: int e;
10226: GFS gfs;
10227:
10228: if ( mod == -1 ) {
10229: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
10230: } else if ( mod == -2 ) {
1.10 noro 10231: q = c.z; return (P)q;
1.1 noro 10232: } else if ( mod > 0 ) {
1.6 noro 10233: STOZ(c.m,q); return (P)q;
1.1 noro 10234: } else
10235: return (P)c.p;
10236: }
10237:
10238: /* [0,0,0,cont] = p -> p/cont */
10239:
10240: void finalize_tracelist(int i,P cont)
10241: {
10242: LIST l;
10243: NODE node;
10244: Z iq;
10245:
10246: if ( !UNIQ(cont) ) {
10247: node = mknode(4,NULLP,NULLP,NULLP,cont);
10248: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
10249: nd_tracelist = node;
10250: }
1.6 noro 10251: STOZ(i,iq);
1.1 noro 10252: nd_tracelist = reverse_node(nd_tracelist);
10253: MKLIST(l,nd_tracelist);
10254: node = mknode(2,iq,l); MKLIST(l,node);
10255: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
10256: nd_alltracelist = node; nd_tracelist = 0;
10257: }
10258:
10259: void conv_ilist(int demand,int trace,NODE g,int **indp)
10260: {
10261: int n,i,j;
10262: int *ind;
10263: NODE t;
10264:
10265: n = length(g);
10266: ind = (int *)MALLOC(n*sizeof(int));
10267: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
10268: j = (long)BDY(t); ind[i] = j;
10269: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
10270: }
10271: if ( indp ) *indp = ind;
10272: }
10273:
1.30 noro 10274: NODE conv_ilist_s(int demand,int trace,int **indp)
10275: {
10276: int n,i,j;
10277: int *ind;
10278: NODE g0,g;
10279:
10280: n = nd_psn;
10281: ind = (int *)MALLOC(n*sizeof(int));
10282: g0 = 0;
10283: for ( i = 0; i < n; i++ ) {
10284: ind[i] = i;
10285: NEXTNODE(g0,g);
10286: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
10287: }
10288: if ( g0 ) NEXT(g) = 0;
10289: if ( indp ) *indp = ind;
10290: return g0;
10291: }
10292:
1.46 ! noro 10293: void parse_nd_option(VL vl,NODE opt)
1.1 noro 10294: {
1.38 noro 10295: NODE t,p,u;
1.1 noro 10296: int i,s,n;
1.38 noro 10297: char *key;
10298: Obj value;
1.46 ! noro 10299: VL oldvl;
1.1 noro 10300:
1.41 noro 10301: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1 noro 10302: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
10303: nd_splist = 0; nd_check_splist = 0;
1.38 noro 10304: nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
10305: nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41 noro 10306: nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46 ! noro 10307: nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.39 noro 10308:
1.38 noro 10309: for ( t = opt; t; t = NEXT(t) ) {
10310: p = BDY((LIST)BDY(t));
10311: key = BDY((STRING)BDY(p));
10312: value = (Obj)BDY(NEXT(p));
10313: if ( !strcmp(key,"gentrace") )
10314: nd_gentrace = value?1:0;
10315: else if ( !strcmp(key,"gensyz") )
10316: nd_gensyz = value?1:0;
10317: else if ( !strcmp(key,"nora") )
10318: nd_nora = value?1:0;
1.41 noro 10319: else if ( !strcmp(key,"norb") )
10320: nd_norb = value?1:0;
1.38 noro 10321: else if ( !strcmp(key,"gbblock") ) {
10322: if ( value && OID(value) == O_LIST ) {
1.1 noro 10323: u = BDY((LIST)value);
1.38 noro 10324: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1 noro 10325: for ( i = 0; u; u = NEXT(u) ) {
10326: p = BDY((LIST)BDY(u));
1.6 noro 10327: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
10328: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 10329: }
10330: nd_gbblock[i] = -1;
1.38 noro 10331: } else
10332: nd_gbblock = 0;
1.1 noro 10333: } else if ( !strcmp(key,"newelim") )
10334: nd_newelim = value?1:0;
10335: else if ( !strcmp(key,"intersect") )
10336: nd_intersect = value?1:0;
1.17 noro 10337: else if ( !strcmp(key,"syzgen") )
10338: nd_intersect = ZTOS((Q)value);
1.1 noro 10339: else if ( !strcmp(key,"lf") )
10340: nd_lf = value?1:0;
10341: else if ( !strcmp(key,"trace") ) {
1.38 noro 10342: if ( value ) {
10343: u = BDY((LIST)value);
10344: nd_nzlist = BDY((LIST)ARG2(u));
10345: nd_bpe = ZTOS((Q)ARG3(u));
10346: }
1.1 noro 10347: } else if ( !strcmp(key,"f4red") ) {
1.38 noro 10348: nd_f4red = ZTOS((Q)value);
1.1 noro 10349: } else if ( !strcmp(key,"rank0") ) {
1.38 noro 10350: nd_rank0 = value?1:0;
1.1 noro 10351: } else if ( !strcmp(key,"splist") ) {
1.38 noro 10352: nd_splist = value?1:0;
1.1 noro 10353: } else if ( !strcmp(key,"check_splist") ) {
10354: nd_check_splist = BDY((LIST)value);
10355: } else if ( !strcmp(key,"sugarweight") ) {
10356: u = BDY((LIST)value);
1.38 noro 10357: n = length(u);
10358: nd_sugarweight = MALLOC(n*sizeof(int));
1.1 noro 10359: for ( i = 0; i < n; i++, u = NEXT(u) )
1.38 noro 10360: nd_sugarweight[i] = ZTOS((Q)BDY(u));
10361: } else if ( !strcmp(key,"f4_td") ) {
10362: nd_f4_td = value?1:0;
10363: } else if ( !strcmp(key,"sba_f4step") ) {
10364: nd_sba_f4step = value?ZTOS((Q)value):0;
10365: } else if ( !strcmp(key,"sba_pot") ) {
1.44 noro 10366: nd_sba_pot = ZTOS((Q)value);
1.38 noro 10367: } else if ( !strcmp(key,"sba_largelcm") ) {
10368: nd_sba_largelcm = value?1:0;
1.39 noro 10369: } else if ( !strcmp(key,"sba_dontsort") ) {
10370: nd_sba_dontsort = value?1:0;
1.44 noro 10371: } else if ( !strcmp(key,"sba_syz") ) {
10372: nd_sba_syz = value?1:0;
1.46 ! noro 10373: } else if ( !strcmp(key,"sba_modord") ) {
! 10374: // value=[vlist,ordspec,weight]
! 10375: u = BDY((LIST)value);
! 10376: pltovl((LIST)ARG0(u),&oldvl);
! 10377: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
! 10378: } else if ( !strcmp(key,"sba_gbinput") ) {
! 10379: nd_sba_inputisgb = value?1:0;
! 10380: if ( nd_sba_inputisgb != 0 ) {
! 10381: // value=[vlist,ordspec,weight]
! 10382: u = BDY((LIST)value);
! 10383: pltovl((LIST)ARG0(u),&oldvl);
! 10384: nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
! 10385: }
1.41 noro 10386: } else if ( !strcmp(key,"sba_redundant_check") ) {
10387: nd_sba_redundant_check = value?1:0;
1.39 noro 10388: } else if ( !strcmp(key,"top") ) {
10389: nd_top = value?1:0;
1.1 noro 10390: }
1.38 noro 10391: }
1.44 noro 10392: if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1 noro 10393: }
10394:
10395: ND mdptond(DP d);
10396: ND nd_mul_nm(int mod,NM m0,ND p);
10397: ND nd_mul_nm_lf(NM m0,ND p);
10398: ND *btog(NODE ti,ND **p,int nb,int mod);
10399: ND btog_one(NODE ti,ND *p,int nb,int mod);
10400: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
10401: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
10402:
10403: /* d:monomial */
10404: ND mdptond(DP d)
10405: {
10406: NM m;
10407: ND r;
10408:
10409: if ( OID(d) == 1 )
10410: r = ptond(CO,CO,(P)d);
10411: else {
10412: NEWNM(m);
10413: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 10414: CZ(m) = (Z)BDY(d)->c;
1.1 noro 10415: NEXT(m) = 0;
10416: MKND(NV(d),m,1,r);
10417: }
10418: return r;
10419: }
10420:
10421: ND nd_mul_nm(int mod,NM m0,ND p)
10422: {
10423: UINT *d0;
10424: int c0,c1,c;
10425: NM tm,mr,mr0;
10426: ND r;
10427:
10428: if ( !p ) return 0;
10429: d0 = DL(m0);
10430: c0 = CM(m0);
10431: mr0 = 0;
10432: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10433: NEXTNM(mr0,mr);
10434: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
10435: ndl_add(d0,DL(tm),DL(mr));
10436: }
10437: NEXT(mr) = 0;
10438: MKND(NV(p),mr0,LEN(p),r);
10439: return r;
10440: }
10441:
10442: ND nd_mul_nm_lf(NM m0,ND p)
10443: {
10444: UINT *d0;
10445: Z c0,c1,c;
10446: NM tm,mr,mr0;
10447: ND r;
10448:
10449: if ( !p ) return 0;
10450: d0 = DL(m0);
10451: c0 = CZ(m0);
10452: mr0 = 0;
10453: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
10454: NEXTNM(mr0,mr);
10455: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
10456: ndl_add(d0,DL(tm),DL(mr));
10457: }
10458: NEXT(mr) = 0;
10459: MKND(NV(p),mr0,LEN(p),r);
10460: return r;
10461: }
10462:
10463: ND *btog(NODE ti,ND **p,int nb,int mod)
10464: {
10465: PGeoBucket *r;
10466: int i,ci;
10467: NODE t,s;
10468: ND m,tp;
10469: ND *pi,*rd;
10470: P c;
10471:
10472: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10473: for ( i = 0; i < nb; i++ )
10474: r[i] = create_pbucket();
10475: for ( t = ti; t; t = NEXT(t) ) {
10476: s = BDY((LIST)BDY(t));
10477: if ( ARG0(s) ) {
10478: m = mdptond((DP)ARG2(s));
1.6 noro 10479: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10480: if ( (ci = ((MQ)c)->cont) != 0 ) {
10481: HCM(m) = ci;
1.6 noro 10482: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10483: for ( i = 0; i < nb; i++ ) {
10484: tp = nd_mul_nm(mod,BDY(m),pi[i]);
10485: add_pbucket(mod,r[i],tp);
10486: }
10487: }
10488: ci = 1;
10489: } else {
10490: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10491: ci = invm(ci,mod);
10492: }
10493: }
10494: rd = (ND *)MALLOC(nb*sizeof(ND));
10495: for ( i = 0; i < nb; i++ )
10496: rd[i] = normalize_pbucket(mod,r[i]);
10497: if ( ci != 1 )
10498: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
10499: return rd;
10500: }
10501:
10502: /* YYY */
10503: ND *btog_lf(NODE ti,ND **p,int nb)
10504: {
10505: PGeoBucket *r;
10506: int i;
10507: NODE t,s;
10508: ND m,tp;
10509: ND *pi,*rd;
10510: LM lm;
10511: Z lf,c;
10512:
10513: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
10514: for ( i = 0; i < nb; i++ )
10515: r[i] = create_pbucket();
10516: for ( t = ti; t; t = NEXT(t) ) {
10517: s = BDY((LIST)BDY(t));
10518: if ( ARG0(s) ) {
10519: m = mdptond((DP)ARG2(s));
1.6 noro 10520: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 10521: if ( lm ) {
10522: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 10523: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 10524: for ( i = 0; i < nb; i++ ) {
10525: tp = nd_mul_nm_lf(BDY(m),pi[i]);
10526: add_pbucket(-2,r[i],tp);
10527: }
10528: }
10529: c = ONE;
10530: } else {
10531: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
10532: }
10533: }
10534: rd = (ND *)MALLOC(nb*sizeof(ND));
10535: for ( i = 0; i < nb; i++ )
10536: rd[i] = normalize_pbucket(-2,r[i]);
10537: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
10538: return rd;
10539: }
10540:
10541: ND btog_one(NODE ti,ND *p,int nb,int mod)
10542: {
10543: PGeoBucket r;
10544: int i,ci,j;
10545: NODE t,s;
10546: ND m,tp;
10547: ND pi,rd;
10548: P c;
10549:
10550: r = create_pbucket();
10551: for ( t = ti; t; t = NEXT(t) ) {
10552: s = BDY((LIST)BDY(t));
10553: if ( ARG0(s) ) {
10554: m = mdptond((DP)ARG2(s));
1.6 noro 10555: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 10556: if ( (ci = ((MQ)c)->cont) != 0 ) {
10557: HCM(m) = ci;
1.6 noro 10558: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 10559: if ( !pi ) {
10560: pi = nd_load_mod(j);
10561: tp = nd_mul_nm(mod,BDY(m),pi);
10562: nd_free(pi);
10563: add_pbucket(mod,r,tp);
10564: } else {
10565: tp = nd_mul_nm(mod,BDY(m),pi);
10566: add_pbucket(mod,r,tp);
10567: }
10568: }
10569: ci = 1;
10570: } else {
10571: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
10572: ci = invm(ci,mod);
10573: }
10574: }
10575: rd = normalize_pbucket(mod,r);
10576: free_pbucket(r);
10577: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
10578: return rd;
10579: }
10580:
10581: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
10582:
10583: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
10584: {
10585: int i,j,n,m,nb,pi0,pi1,nvar;
10586: VL fv,tv,vv;
10587: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10588: ND **p;
10589: ND *c;
10590: ND u;
10591: P inv;
10592: MAT mat;
10593:
10594: if ( mod == -2 )
10595: return nd_btog_lf(f,v,ord,tlist,rp);
10596:
10597: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 10598: parse_nd_option(vv,current_option);
1.1 noro 10599: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10600: switch ( ord->id ) {
10601: case 1:
10602: if ( ord->nv != nvar )
10603: error("nd_check : invalid order specification");
10604: break;
10605: default:
10606: break;
10607: }
10608: nd_init_ord(ord);
10609: #if 0
1.6 noro 10610: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10611: #else
10612: nd_bpe = 32;
10613: #endif
10614: nd_setup_parameters(nvar,0);
10615: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10616: intred = BDY((LIST)ARG3(BDY(tlist)));
10617: ind = BDY((LIST)ARG4(BDY(tlist)));
10618: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10619: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10620: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10621: if ( j > i ) i = j;
10622: }
10623: n = i+1;
10624: nb = length(BDY(f));
10625: p = (ND **)MALLOC(n*sizeof(ND *));
10626: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10627: pi = BDY((LIST)BDY(t));
1.6 noro 10628: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10629: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10630: ptomp(mod,(P)ARG2(pi),&inv);
10631: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10632: u = ptond(CO,vv,(P)ONE);
10633: HCM(u) = ((MQ)inv)->cont;
10634: c[pi1] = u;
10635: }
10636: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10637: printf("%d ",i); fflush(stdout);
10638: ti = BDY((LIST)BDY(t));
1.6 noro 10639: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10640: }
10641: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10642: printf("%d ",i); fflush(stdout);
10643: ti = BDY((LIST)BDY(t));
1.6 noro 10644: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10645: }
10646: m = length(ind);
10647: MKMAT(mat,nb,m);
10648: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10649: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10650: BDY(mat)[i][j] = ndtodp(mod,c[i]);
10651: return mat;
10652: }
10653:
10654: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
10655: {
10656: int i,j,n,m,nb,pi0,pi1,nvar;
10657: VL fv,tv,vv;
10658: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10659: ND **p;
10660: ND *c;
10661: ND u;
10662: MAT mat;
10663: LM lm;
10664: Z lf,inv;
10665:
10666: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 10667: parse_nd_option(vv,current_option);
1.1 noro 10668: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10669: switch ( ord->id ) {
10670: case 1:
10671: if ( ord->nv != nvar )
10672: error("nd_check : invalid order specification");
10673: break;
10674: default:
10675: break;
10676: }
10677: nd_init_ord(ord);
10678: #if 0
1.6 noro 10679: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10680: #else
10681: nd_bpe = 32;
10682: #endif
10683: nd_setup_parameters(nvar,0);
10684: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10685: intred = BDY((LIST)ARG3(BDY(tlist)));
10686: ind = BDY((LIST)ARG4(BDY(tlist)));
10687: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10688: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10689: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10690: if ( j > i ) i = j;
10691: }
10692: n = i+1;
10693: nb = length(BDY(f));
10694: p = (ND **)MALLOC(n*sizeof(ND *));
10695: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10696: pi = BDY((LIST)BDY(t));
1.6 noro 10697: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10698: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
10699: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
10700: u = ptond(CO,vv,(P)ONE);
10701: HCZ(u) = inv;
10702: c[pi1] = u;
10703: }
10704: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10705: printf("%d ",i); fflush(stdout);
10706: ti = BDY((LIST)BDY(t));
1.6 noro 10707: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10708: }
10709: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10710: printf("%d ",i); fflush(stdout);
10711: ti = BDY((LIST)BDY(t));
1.6 noro 10712: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 10713: }
10714: m = length(ind);
10715: MKMAT(mat,nb,m);
10716: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 10717: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 10718: BDY(mat)[i][j] = ndtodp(-2,c[i]);
10719: return mat;
10720: }
10721:
10722: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
10723: LIST tlist,int pos,MAT *rp)
10724: {
10725: int i,j,n,m,nb,pi0,pi1,nvar;
10726: VL fv,tv,vv;
10727: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
10728: ND *p;
10729: ND *c;
10730: ND u;
10731: P inv;
10732: VECT vect;
10733:
10734: if ( mod == -2 )
10735: error("nd_btog_one : not implemented yet for a large finite field");
10736:
10737: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 10738: parse_nd_option(vv,current_option);
1.1 noro 10739: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10740: switch ( ord->id ) {
10741: case 1:
10742: if ( ord->nv != nvar )
10743: error("nd_check : invalid order specification");
10744: break;
10745: default:
10746: break;
10747: }
10748: nd_init_ord(ord);
10749: #if 0
1.6 noro 10750: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 10751: #else
10752: nd_bpe = 32;
10753: #endif
10754: nd_setup_parameters(nvar,0);
10755: permtrace = BDY((LIST)ARG2(BDY(tlist)));
10756: intred = BDY((LIST)ARG3(BDY(tlist)));
10757: ind = BDY((LIST)ARG4(BDY(tlist)));
10758: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10759: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10760: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10761: if ( j > i ) i = j;
10762: }
10763: n = i+1;
10764: nb = length(BDY(f));
10765: p = (ND *)MALLOC(n*sizeof(ND *));
10766: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10767: pi = BDY((LIST)BDY(t));
1.6 noro 10768: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10769: if ( pi1 == pos ) {
10770: ptomp(mod,(P)ARG2(pi),&inv);
10771: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10772: u = ptond(CO,vv,(P)ONE);
10773: HCM(u) = ((MQ)inv)->cont;
10774: p[pi0] = u;
10775: }
10776: }
10777: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10778: printf("%d ",i); fflush(stdout);
10779: ti = BDY((LIST)BDY(t));
1.6 noro 10780: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10781: if ( Demand ) {
10782: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10783: }
10784: }
10785: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10786: printf("%d ",i); fflush(stdout);
10787: ti = BDY((LIST)BDY(t));
1.6 noro 10788: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10789: if ( Demand ) {
10790: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10791: }
10792: }
10793: m = length(ind);
10794: MKVECT(vect,m);
10795: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10796: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10797: if ( !u ) {
1.6 noro 10798: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10799: BDY(vect)[j] = ndtodp(mod,u);
10800: nd_free(u);
10801: } else
10802: BDY(vect)[j] = ndtodp(mod,u);
10803: }
10804: return vect;
10805: }
10806:
10807: void ndv_print_lf(NDV p)
10808: {
10809: NMV m;
10810: int i,len;
10811:
10812: if ( !p ) printf("0\n");
10813: else {
10814: len = LEN(p);
10815: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10816: printf("+");
10817: mpz_out_str(asir_out,10,BDY(CZ(m)));
10818: printf("*");
10819: ndl_print(DL(m));
10820: }
10821: printf("\n");
10822: }
10823: }
10824:
10825: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10826: {
10827: VL tv,fv,vv,vc,av;
10828: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10829: int m,nocheck,nvar,mindex,e,max;
10830: NDV c;
10831: NMV a;
10832: P p,zp;
10833: Q dmy;
10834: EPOS oepos;
10835: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10836: Alg alpha,dp;
10837: P poly;
10838: LIST f1,f2,zpl;
10839: Obj obj;
10840: NumberField nf;
10841: struct order_spec *ord1;
10842: struct oEGT eg_check,eg0,eg1;
10843: NODE tr,tl1,tl2,tl3,tl4;
10844: LIST l1,l2,l3,l4,l5;
10845: int *perm;
10846: int j,ret;
10847: NODE retn;
10848: Q jq,bpe;
10849:
10850: nd_module = 0;
10851: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46 ! noro 10852: parse_nd_option(vv,current_option);
1.1 noro 10853: if ( nd_vc )
10854: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10855: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10856: switch ( ord->id ) {
10857: case 1:
10858: if ( ord->nv != nvar )
10859: error("nd_f4_lf_trace : invalid order specification");
10860: break;
10861: default:
10862: break;
10863: }
10864:
10865: nd_ntrans = nvar;
10866: nd_nalg = 0;
10867:
10868: nocheck = 0;
10869: mindex = 0;
10870:
10871: /* do not use on-demand load/save */
10872: nd_demand = 0;
10873: m = trace > 1 ? trace : get_lprime(mindex);
10874: nd_init_ord(ord);
10875: mrank = 0;
10876: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10877: for ( tv = vv; tv; tv = NEXT(tv) ) {
10878: if ( nd_module ) {
10879: s = BDY((LIST)BDY(t));
10880: trank = length(s);
10881: mrank = MAX(mrank,trank);
10882: for ( ; s; s = NEXT(s) ) {
10883: e = getdeg(tv->v,(P)BDY(s));
10884: max = MAX(e,max);
10885: }
10886: } else {
10887: e = getdeg(tv->v,(P)BDY(t));
10888: max = MAX(e,max);
10889: }
10890: }
10891: nd_setup_parameters(nvar,max);
10892: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10893: ishomo = 1;
10894: /* XXX */
10895: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10896: if ( nd_module ) {
10897: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10898: } else {
10899: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10900: }
10901: if ( ishomo )
10902: ishomo = ishomo && ndv_ishomo(c);
10903: if ( c ) {
10904: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10905: ndv_mod(-2,c);
10906: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10907: }
10908: }
10909: if ( in0 ) NEXT(in) = 0;
10910: if ( fd0 ) NEXT(fd) = 0;
10911: if ( !ishomo && homo ) {
10912: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10913: c = (NDV)BDY(t); len = LEN(c);
10914: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10915: wmax = MAX(TD(DL(a)),wmax);
10916: }
10917: homogenize_order(ord,nvar,&ord1);
10918: nd_init_ord(ord1);
10919: nd_setup_parameters(nvar+1,wmax);
10920: for ( t = fd0; t; t = NEXT(t) )
10921: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10922: }
10923: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10924: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10925: if ( ret )
10926: cand = nd_f4_lf_trace_main(m,&perm);
10927: if ( !ret || !cand ) {
10928: *rp = 0; return;
10929: }
10930: if ( !ishomo && homo ) {
10931: /* dehomogenization */
10932: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10933: nd_init_ord(ord);
10934: nd_setup_parameters(nvar,0);
10935: }
10936: cand = ndv_reducebase(cand,perm);
10937: cand = ndv_reduceall(-2,cand);
10938: cbpe = nd_bpe;
10939: get_eg(&eg0);
10940: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10941: /* gbcheck : cand is a GB of Id(cand) ? */
10942: retn = nd_f4(-2,0,0);
10943: }
10944: if ( !retn ) {
10945: /* failure */
10946: *rp = 0; return;
10947: }
10948: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10949: if ( DP_Print )
1.5 noro 10950: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10951: /* dp->p */
10952: nd_bpe = cbpe;
10953: nd_setup_parameters(nd_nvar,0);
10954: for ( r = cand; r; r = NEXT(r) ) {
10955: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10956: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10957: }
10958: MKLIST(*rp,cand);
10959: }
10960:
10961: NODE nd_f4_lf_trace_main(int m,int **indp)
10962: {
10963: int i,nh,stat,index;
10964: NODE r,rm,g;
10965: ND_pairs d,l,l0,t;
10966: ND spol,red;
10967: NDV nf,redv,nfqv,nfv;
10968: NM s0,s;
10969: NODE rp0,srp0,nflist,nflist_lf;
10970: int nsp,nred,col,rank,len,k,j,a;
10971: UINT c;
10972: UINT **spmat;
10973: UINT *s0vect,*svect,*p,*v;
10974: int *colstat;
10975: IndArray *imat;
10976: int *rhead;
10977: int spcol,sprow;
10978: int sugar;
10979: PGeoBucket bucket;
10980: struct oEGT eg0,eg1,eg_f4;
10981:
10982: g = 0; d = 0;
10983: for ( i = 0; i < nd_psn; i++ ) {
10984: d = update_pairs(d,g,i,0);
10985: g = update_base(g,i);
10986: }
10987: while ( d ) {
10988: get_eg(&eg0);
10989: l = nd_minsugarp(d,&d);
10990: sugar = SG(l);
10991: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10992: bucket = create_pbucket();
10993: stat = nd_sp_f4(m,0,l,bucket);
10994: if ( !stat ) {
10995: for ( t = l; NEXT(t); t = NEXT(t) );
10996: NEXT(t) = d; d = l;
10997: d = nd_reconstruct(1,d);
10998: continue;
10999: }
11000: if ( bucket->m < 0 ) continue;
11001: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
11002: if ( !col ) {
11003: for ( t = l; NEXT(t); t = NEXT(t) );
11004: NEXT(t) = d; d = l;
11005: d = nd_reconstruct(1,d);
11006: continue;
11007: }
11008: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
11009: if ( DP_Print )
1.5 noro 11010: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 11011: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
11012: if ( !l0 ) continue;
11013: l = l0;
11014:
11015: /* over LF */
11016: bucket = create_pbucket();
11017: stat = nd_sp_f4(-2,1,l,bucket);
11018: if ( !stat ) {
11019: for ( t = l; NEXT(t); t = NEXT(t) );
11020: NEXT(t) = d; d = l;
11021: d = nd_reconstruct(1,d);
11022: continue;
11023: }
11024: if ( bucket->m < 0 ) continue;
11025: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
11026: if ( !col ) {
11027: for ( t = l; NEXT(t); t = NEXT(t) );
11028: NEXT(t) = d; d = l;
11029: d = nd_reconstruct(1,d);
11030: continue;
11031: }
11032: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
11033: /* adding new bases */
11034: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
11035: nfv = (NDV)BDY(rm);
11036: nfqv = (NDV)BDY(r);
11037: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
11038: ndv_removecont(m,nfv);
11039: ndv_removecont(-2,nfqv);
1.24 noro 11040: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 11041: d = update_pairs(d,g,nh,0);
11042: g = update_base(g,nh);
11043: }
11044: if ( r || rm ) return 0;
11045: }
11046: conv_ilist(nd_demand,1,g,indp);
11047: return g;
11048: }
11049:
1.7 noro 11050: #if SIZEOF_LONG==8
11051:
11052: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
11053: {
11054: int j,k,len;
11055: UINT *p;
11056: UINT c;
11057: NDV r;
11058: NMV mr0,mr;
11059:
11060: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
11061: if ( !len ) return 0;
11062: else {
11063: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11064: #if 0
11065: ndv_alloc += nmv_adv*len;
11066: #endif
11067: mr = mr0;
11068: p = s0vect;
11069: for ( j = k = 0; j < col; j++, p += nd_wpd )
11070: if ( !rhead[j] ) {
11071: if ( (c = (UINT)vect[k++]) != 0 ) {
11072: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11073: }
11074: }
11075: MKNDV(nd_nvar,mr0,len,r);
11076: return r;
11077: }
11078: }
11079:
1.28 noro 11080: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
11081: {
11082: int j,k,len;
11083: UINT *p;
11084: UINT c;
11085: NDV r;
11086: NMV mr0,mr;
11087:
11088: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
11089: if ( !len ) return 0;
11090: else {
11091: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
11092: mr = mr0;
11093: p = s0vect;
11094: for ( j = k = 0; j < col; j++, p += nd_wpd )
11095: if ( (c = (UINT)vect[k++]) != 0 ) {
11096: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
11097: }
11098: MKNDV(nd_nvar,mr0,len,r);
11099: return r;
11100: }
11101: }
11102:
1.7 noro 11103: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
11104: {
11105: NM m;
1.11 noro 11106: UINT *t,*s,*u;
11107: int i,st,ed,md,prev,c;
1.7 noro 11108:
11109: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 11110: prev = 0;
11111: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
11112: t = DL(m);
11113: st = prev;
11114: ed = n;
11115: while ( ed > st ) {
11116: md = (st+ed)/2;
11117: u = s0+md*nd_wpd;
11118: c = DL_COMPARE(u,t);
11119: if ( c == 0 ) break;
11120: else if ( c > 0 ) st = md;
11121: else ed = md;
11122: }
11123: r[md] = (mp_limb_t)CM(m);
11124: prev = md;
1.7 noro 11125: }
11126: for ( i = 0; !r[i]; i++ );
11127: return i;
11128: }
11129:
11130: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
11131:
1.28 noro 11132: 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 11133: {
11134: int i,j,k,len,pos,prev;
11135: mp_limb_t a,c,c1,c2;
11136: IndArray ivect;
11137: unsigned char *ivc;
11138: unsigned short *ivs;
11139: unsigned int *ivi;
11140: NDV redv;
11141: NMV mr;
11142: NODE rp;
11143: int maxrs;
11144:
11145: for ( i = 0; i < col; i++ ) cvect[i] = 0;
11146: maxrs = 0;
11147: for ( i = 0; i < nred; i++ ) {
11148: ivect = imat[i];
11149: k = ivect->head;
11150: a = svect[k]; c = cvect[k];
11151: MOD128(a,c,m);
11152: svect[k] = a; cvect[k] = 0;
1.28 noro 11153: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 11154: Nf4_red++;
1.7 noro 11155: maxrs = MAX(maxrs,rp0[i]->sugar);
11156: c = m-c; redv = nd_ps[rp0[i]->index];
11157: len = LEN(redv); mr = BDY(redv);
11158: svect[k] = 0; prev = k;
11159: switch ( ivect->width ) {
11160: case 1:
11161: ivc = ivect->index.c;
11162: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11163: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 11164: c2 = svect[pos]+c1*c;
11165: if ( c2 < svect[pos] ) cvect[pos]++;
11166: svect[pos] = c2;
1.7 noro 11167: }
11168: break;
11169: case 2:
11170: ivs = ivect->index.s;
11171: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11172: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 11173: c2 = svect[pos]+c1*c;
11174: if ( c2 < svect[pos] ) cvect[pos]++;
11175: svect[pos] = c2;
1.7 noro 11176: }
11177: break;
11178: case 4:
11179: ivi = ivect->index.i;
11180: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
11181: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 11182: c2 = svect[pos]+c1*c;
11183: if ( c2 < svect[pos] ) cvect[pos]++;
11184: svect[pos] = c2;
1.7 noro 11185: }
11186: break;
11187: }
11188: }
11189: }
11190: for ( i = 0; i < col; i++ ) {
11191: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
11192: }
11193: return maxrs;
11194: }
11195:
11196: /* for Fp, 2^15=<p<2^29 */
11197:
11198: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11199: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
11200: {
11201: int spcol,sprow,a;
11202: int i,j,k,l,rank;
11203: NODE r0,r;
11204: ND_pairs sp;
11205: ND spol;
11206: mp_limb_t **spmat;
11207: mp_limb_t *svect,*cvect;
11208: mp_limb_t *v;
11209: int *colstat;
11210: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11211: int maxrs;
11212: int *spsugar;
11213: ND_pairs *spactive;
11214:
11215: spcol = col-nred;
11216: get_eg(&eg0);
11217: /* elimination (1st step) */
11218: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11219: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11220: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11221: spsugar = (int *)MALLOC(nsp*sizeof(int));
11222: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
11223: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11224: nd_sp(m,0,sp,&spol);
11225: if ( !spol ) continue;
11226: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 11227: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 11228: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11229: if ( i < col ) {
11230: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
11231: for ( j = k = 0; j < col; j++ )
11232: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
11233: spsugar[sprow] = MAX(maxrs,SG(spol));
11234: if ( nz )
11235: spactive[sprow] = sp;
11236: sprow++;
11237: }
11238: nd_free(spol);
11239: }
1.12 noro 11240: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 11241: if ( DP_Print ) {
11242: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11243: fflush(asir_out);
11244: }
11245: /* free index arrays */
11246: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11247:
11248: /* elimination (2nd step) */
11249: colstat = (int *)MALLOC(spcol*sizeof(int));
11250: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
11251: r0 = 0;
11252: for ( i = 0; i < rank; i++ ) {
11253: NEXTNODE(r0,r); BDY(r) =
11254: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
11255: SG((NDV)BDY(r)) = spsugar[i];
11256: GCFREE(spmat[i]);
11257: }
11258: if ( r0 ) NEXT(r) = 0;
11259:
11260: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 11261: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 11262: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11263: if ( DP_Print ) {
11264: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11265: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11266: nsp,nred,sprow,spcol,rank);
11267: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11268: }
11269: if ( nz ) {
11270: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
11271: if ( rank > 0 ) {
11272: NEXT(spactive[rank-1]) = 0;
11273: *nz = spactive[0];
11274: } else
11275: *nz = 0;
11276: }
11277: return r0;
11278: }
11279:
11280: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
11281: {
11282: int i,j,k,l,rank,s;
11283: mp_limb_t inv;
11284: mp_limb_t a;
11285: UINT c;
11286: mp_limb_t *t,*pivot,*pk;
11287: UINT *ck;
11288: UINT **cmat;
11289: UINT *ct;
11290: ND_pairs pair;
11291:
11292: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11293: for ( i = 0; i < row; i++ ) {
11294: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11295: bzero(cmat[i],col*sizeof(UINT));
11296: }
11297:
11298: for ( rank = 0, j = 0; j < col; j++ ) {
11299: for ( i = rank; i < row; i++ ) {
11300: a = mat[i][j]; c = cmat[i][j];
11301: MOD128(a,c,md);
11302: mat[i][j] = a; cmat[i][j] = 0;
11303: }
11304: for ( i = rank; i < row; i++ )
11305: if ( mat[i][j] )
11306: break;
11307: if ( i == row ) {
11308: colstat[j] = 0;
11309: continue;
11310: } else
11311: colstat[j] = 1;
11312: if ( i != rank ) {
11313: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
11314: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
11315: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
11316: if ( spactive ) {
11317: pair = spactive[i]; spactive[i] = spactive[rank];
11318: spactive[rank] = pair;
11319: }
11320: }
11321: /* column j is normalized */
11322: s = sugar[rank];
11323: inv = invm((UINT)mat[rank][j],md);
11324: /* normalize pivot row */
11325: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
11326: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11327: }
11328: for ( i = rank+1; i < row; i++ ) {
11329: if ( (a = mat[i][j]) != 0 ) {
11330: sugar[i] = MAX(sugar[i],s);
11331: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 11332: Nf4_red++;
1.7 noro 11333: }
11334: }
11335: rank++;
11336: }
11337: for ( j = col-1, l = rank-1; j >= 0; j-- )
11338: if ( colstat[j] ) {
11339: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
11340: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
11341: }
11342: s = sugar[l];
11343: for ( i = 0; i < l; i++ ) {
11344: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
11345: if ( a ) {
11346: sugar[i] = MAX(sugar[i],s);
11347: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 11348: Nf4_red++;
1.7 noro 11349: }
11350: }
11351: l--;
11352: }
11353: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11354: GCFREE(cmat);
11355: return rank;
11356: }
11357:
1.28 noro 11358: 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)
11359: {
11360: int i,j,k,l,rank,s,imin;
11361: mp_limb_t inv;
11362: mp_limb_t a;
11363: UINT c;
11364: mp_limb_t *t,*pivot,*pk;
11365: UINT *ck;
11366: UINT **cmat;
11367: UINT *ct;
11368: ND_pairs pair;
11369: SIG sg;
1.31 noro 11370: int *used;
1.28 noro 11371:
1.31 noro 11372: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 11373: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
11374: for ( i = 0; i < row; i++ ) {
11375: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
11376: bzero(cmat[i],col*sizeof(UINT));
11377: }
11378:
1.31 noro 11379: for ( j = 0; j < col; j++ ) {
11380: for ( i = 0; i < row; i++ ) {
1.28 noro 11381: a = mat[i][j]; c = cmat[i][j];
11382: MOD128(a,c,md);
11383: mat[i][j] = a; cmat[i][j] = 0;
11384: }
1.31 noro 11385: for ( i = 0; i < row; i++ )
11386: if ( !used[i] && mat[i][j] ) break;
11387: if ( i == row ) {
1.28 noro 11388: colstat[j] = 0;
11389: continue;
1.31 noro 11390: } else {
1.28 noro 11391: colstat[j] = 1;
1.31 noro 11392: used[i] = 1;
1.28 noro 11393: }
11394: /* column j is normalized */
1.31 noro 11395: s = sugar[i];
11396: inv = invm((UINT)mat[i][j],md);
1.28 noro 11397: /* normalize pivot row */
1.31 noro 11398: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 11399: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
11400: }
1.31 noro 11401: for ( k = i+1; k < row; k++ ) {
11402: if ( (a = mat[k][j]) != 0 ) {
11403: sugar[k] = MAX(sugar[k],s);
11404: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 11405: Nf4_red++;
11406: }
11407: }
11408: }
1.31 noro 11409: rank = 0;
11410: for ( i = 0; i < row; i++ ) {
11411: for ( j = 0; j < col; j++ )
11412: if ( mat[i][j] ) break;
11413: if ( j == col ) sugar[i] = -1;
11414: else rank++;
11415: }
1.28 noro 11416: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
11417: GCFREE(cmat);
11418: return rank;
11419: }
11420:
11421: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
11422: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
11423: {
11424: int spcol,sprow,a;
11425: int i,j,k,l,rank;
11426: NODE r0,r;
11427: ND_pairs sp;
11428: ND spol;
11429: mp_limb_t **spmat;
11430: mp_limb_t *svect,*cvect;
11431: mp_limb_t *v;
11432: int *colstat;
11433: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
11434: int maxrs;
11435: int *spsugar;
11436: ND_pairs *spactive;
11437: SIG *spsig;
11438:
11439: get_eg(&eg0);
11440: /* elimination (1st step) */
11441: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
11442: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11443: spsugar = (int *)MALLOC(nsp*sizeof(int));
11444: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
11445: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
11446: nd_sp(m,0,sp,&spol);
11447: if ( !spol ) {
1.29 noro 11448: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11449: continue;
11450: }
11451: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
11452: nd_to_vect64(m,s0vect,col,spol,svect);
11453: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
11454: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
11455: if ( i < col ) {
11456: spmat[sprow] = svect;
11457: spsugar[sprow] = MAX(maxrs,SG(spol));
11458: spsig[sprow] = sp->sig;
11459: sprow++;
11460: } else {
1.29 noro 11461: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 11462: }
11463: nd_free(spol);
11464: }
11465: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
11466: if ( DP_Print ) {
11467: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
11468: fflush(asir_out);
11469: }
11470: /* free index arrays */
11471: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
11472:
11473: /* elimination (2nd step) */
11474: colstat = (int *)MALLOC(col*sizeof(int));
11475: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
11476: r0 = 0;
1.31 noro 11477: for ( i = 0; i < sprow; i++ ) {
11478: if ( spsugar[i] >= 0 ) {
11479: NEXTNODE(r0,r);
11480: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
11481: SG((NDV)BDY(r)) = spsugar[i];
11482: ((NDV)BDY(r))->sig = spsig[i];
11483: } else
11484: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 11485: GCFREE(spmat[i]);
11486: }
11487: if ( r0 ) NEXT(r) = 0;
11488: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
11489: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
11490: if ( DP_Print ) {
11491: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
11492: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
11493: nsp,nred,sprow,col,rank);
11494: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
11495: }
11496: return r0;
11497: }
1.40 noro 11498: #endif
1.28 noro 11499:
11500: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
11501: {
11502: IndArray *imat;
11503: int nsp,nred,i,start;
11504: int *rhead;
11505: NODE r0,rp;
11506: ND_pairs sp;
11507: NM_ind_pair *rvect;
11508: UINT *s;
11509: int *s0hash;
11510: struct oEGT eg0,eg1,eg_conv;
11511:
11512: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
11513: nred = length(rp0);
11514: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
11515: rhead = (int *)MALLOC(col*sizeof(int));
11516: for ( i = 0; i < col; i++ ) rhead[i] = 0;
11517:
11518: /* construction of index arrays */
11519: get_eg(&eg0);
11520: if ( DP_Print ) {
11521: fprintf(asir_out,"%dx%d,",nsp+nred,col);
11522: fflush(asir_out);
11523: }
11524: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
11525: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
11526: rvect[i] = (NM_ind_pair)BDY(rp);
11527: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
11528: rhead[imat[i]->head] = 1;
11529: start = imat[i]->head;
11530: }
11531: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
11532: if ( DP_Print ) {
11533: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
11534: fflush(asir_out);
11535: }
11536: if ( m > 0 )
1.40 noro 11537: #if SIZEOF_LONG==8
1.28 noro 11538: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40 noro 11539: #else
11540: r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
11541: #endif
1.28 noro 11542: else
11543: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
11544: error("nd_f4_red_q_main_s : not implemented yet");
11545: return r0;
11546: }
11547:
11548: INLINE int ndl_find_reducer_minsig(UINT *dg)
11549: {
11550: RHist r;
11551: int i,singular,ret,d,k,imin;
11552: SIG t;
11553: static int wpd,nvar;
11554: static SIG quo,quomin;
11555: static UINT *tmp;
11556:
11557: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
11558: if ( wpd != nd_wpd ) {
11559: wpd = nd_wpd;
11560: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
11561: }
11562: #if 0
11563: d = ndl_hash_value(dg);
11564: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
11565: if ( ndl_equal(dg,DL(r)) ) {
11566: return r->index;
11567: }
11568: }
11569: #endif
11570: imin = -1;
11571: for ( i = 0; i < nd_psn; i++ ) {
11572: r = nd_psh[i];
11573: if ( ndl_reducible(dg,DL(r)) ) {
11574: ndl_sub(dg,DL(r),tmp);
11575: _ndltodl(tmp,DL(quo));
11576: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
11577: quo->pos = nd_psh[i]->sig->pos;
11578: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
11579: t = quo; quo = quomin; quomin = t;
11580: imin = i;
11581: }
11582: }
11583: }
11584: if ( imin == -1 ) return nd_psn;
11585: else {
11586: #if 0
11587: nd_append_red(dg,i);
11588: #endif
11589: return imin;
11590: }
11591: }
11592:
11593: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
11594: {
11595: NODE rp0,rp;
11596: NM mul,head,s0,s;
11597: int index,col,i,sugar;
11598: RHist h;
11599: UINT *s0v,*p;
11600: NM_ind_pair pair;
11601: ND red;
11602: NDV *ps;
11603: SIG sig;
11604:
11605: s0 = 0; rp0 = 0; col = 0;
11606: if ( nd_demand )
11607: ps = trace?nd_ps_trace_sym:nd_ps_sym;
11608: else
11609: ps = trace?nd_ps_trace:nd_ps;
11610: while ( 1 ) {
11611: head = remove_head_pbucket_symbolic(bucket);
11612: if ( !head ) break;
11613: if ( !s0 ) s0 = head;
11614: else NEXT(s) = head;
11615: s = head;
11616: index = ndl_find_reducer_minsig(DL(head));
11617: if ( index >= 0 && index < nd_psn ) {
11618: h = nd_psh[index];
11619: NEWNM(mul);
11620: ndl_sub(DL(head),DL(h),DL(mul));
11621: if ( ndl_check_bound2(index,DL(mul)) )
11622: return 0;
11623: sugar = TD(DL(mul))+SG(ps[index]);
11624: NEWSIG(sig);
11625: _ndltodl(DL(mul),DL(sig));
11626: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
11627: sig->pos = nd_psh[index]->sig->pos;
11628: MKNM_ind_pair(pair,mul,index,sugar,sig);
11629: red = ndv_mul_nm_symbolic(mul,ps[index]);
11630: add_pbucket_symbolic(bucket,nd_remove_head(red));
11631: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
11632: }
11633: col++;
11634: }
11635: if ( rp0 ) NEXT(rp) = 0;
11636: NEXT(s) = 0;
11637: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
11638: for ( i = 0, p = s0v, s = s0; i < col;
11639: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
11640: *s0vect = s0v;
11641: *r = rp0;
11642:
11643: return col;
11644: }
11645:
11646: NODE nd_sba_f4(int m,int **indp)
11647: {
1.34 noro 11648: int i,nh,stat,index,f4red,f4step;
1.33 noro 11649: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
11650: NODE r,g,rp0,nflist;
1.41 noro 11651: ND_pairs d,l,t,l1;
1.33 noro 11652: ND h,nf;
11653: NDV nfv;
11654: union oNDC hc;
11655: UINT *s0vect;
1.28 noro 11656: UINT c;
11657: PGeoBucket bucket;
1.33 noro 11658: NODE *syzlist;
11659: SIG sig;
1.28 noro 11660: struct oEGT eg0,eg1,eg_f4;
1.33 noro 11661: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 11662:
11663: Nf4_red=0;
1.30 noro 11664: d = 0;
1.29 noro 11665: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 11666: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 11667: d = update_pairs_s(d,i,syzlist);
1.28 noro 11668: }
1.30 noro 11669: nd_nbase = nd_psn;
1.28 noro 11670: f4red = 1;
1.33 noro 11671: psugar = 0;
1.34 noro 11672: f4step = 0;
1.28 noro 11673: while ( d ) {
1.33 noro 11674: for ( t = d, ms = SG(d); t; t = NEXT(t) )
11675: if ( SG(t) < ms ) ms = SG(t);
1.38 noro 11676: if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33 noro 11677: again:
11678: l = d; d = d->next;
1.41 noro 11679: #if 0
1.33 noro 11680: if ( small_lcm(l) ) {
11681: if ( DP_Print ) fprintf(asir_out,"M");
11682: continue;
11683: }
11684: sig = l->sig;
11685: stat = nd_sp(m,0,l,&h);
1.41 noro 11686: #else
11687: l1 = find_smallest_lcm(l);
11688: if ( l1 == 0 ) {
11689: if ( DP_Print ) fprintf(asir_out,"M");
11690: continue;
11691: }
11692: sig = l1->sig;
11693: stat = nd_sp(m,0,l1,&h);
11694: #endif
1.33 noro 11695: if ( !stat ) {
11696: NEXT(l) = d; d = l;
11697: d = nd_reconstruct(0,d);
11698: goto again;
11699: }
11700: get_eg(&eg1);
11701: #if USE_GEOBUCKET
1.39 noro 11702: 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 11703: #else
1.39 noro 11704: stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33 noro 11705: #endif
11706: get_eg(&eg2);
11707: if ( !stat ) {
11708: NEXT(l) = d; d = l;
11709: d = nd_reconstruct(0,d);
11710: goto again;
11711: } else if ( stat == -1 ) {
11712: if ( DP_Print ) { printf("S"); fflush(stdout); }
11713: FREENDP(l);
11714: } else if ( nf ) {
11715: if ( DP_Print ) { printf("+"); fflush(stdout); }
11716: add_eg(&eg_nf,&eg1,&eg2);
11717: hc = HCU(nf);
11718: nd_removecont(m,nf);
11719: nfv = ndtondv(m,nf); nd_free(nf);
11720: nh = ndv_newps(m,nfv,0);
11721:
1.30 noro 11722: d = update_pairs_s(d,nh,syzlist);
11723: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 11724: FREENDP(l);
1.28 noro 11725: } else {
1.33 noro 11726: add_eg(&eg_nfzero,&eg1,&eg2);
11727: // syzygy
11728: get_eg(&eg1);
11729: d = remove_spair_s(d,sig);
11730: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 11731: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 11732: if ( DP_Print ) { printf("."); fflush(stdout); }
11733: FREENDP(l);
11734: }
11735: } else {
1.34 noro 11736: if ( ms != psugar ) f4step = 1;
11737: else f4step++;
1.33 noro 11738: again2:
11739: psugar = ms;
11740: l = nd_minsugarp_s(d,&d);
11741: sugar = nd_sugarweight?d->sugar2:SG(d);
11742: bucket = create_pbucket();
11743: stat = nd_sp_f4(m,0,l,bucket);
11744: if ( !stat ) {
11745: for ( t = l; NEXT(t); t = NEXT(t) );
11746: NEXT(t) = d; d = l;
11747: d = nd_reconstruct(0,d);
11748: goto again2;
11749: }
11750: if ( bucket->m < 0 ) continue;
11751: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
11752: if ( !col ) {
1.37 noro 11753: for ( t = l; NEXT(t); t = NEXT(t) )
11754: ;
11755: NEXT(t) = d; d = l;
1.33 noro 11756: d = nd_reconstruct(0,d);
11757: goto again2;
11758: }
1.34 noro 11759: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 11760: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
11761: /* adding new bases */
11762: for ( r = nflist; r; r = NEXT(r) ) {
11763: nfv = (NDV)BDY(r);
1.38 noro 11764: if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33 noro 11765: ndv_removecont(m,nfv);
11766: nh = ndv_newps(m,nfv,0);
11767: d = update_pairs_s(d,nh,syzlist);
11768: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
11769: }
11770: for ( i = 0; i < nd_nbase; i++ )
11771: for ( r = syzlist[i]; r; r = NEXT(r) )
11772: d = remove_spair_s(d,(SIG)BDY(r));
11773: d = remove_large_lcm(d);
11774: if ( DP_Print ) {
1.34 noro 11775: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11776: }
11777: f4red++;
1.28 noro 11778: }
11779: }
11780: if ( DP_Print ) {
1.34 noro 11781: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11782: }
1.30 noro 11783: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11784: return g;
11785: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>