Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.37
1.37 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.36 2020/09/27 04:35:04 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.11 noro 5: int Nnd_add,Nf4_red;
1.12 noro 6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 7:
8: int diag_period = 6;
9: int weight_check = 1;
10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21 noro 11: /* for general module order */
1.19 noro 12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21 noro 13: int (*dl_base_compare_function)(int nv,DL a,DL b);
14: int nd_base_ordtype;
1.1 noro 15: int nd_dcomp;
16: int nd_rref2;
17: NM _nm_free_list;
18: ND _nd_free_list;
19: ND_pairs _ndp_free_list;
20: NODE nd_hcf;
1.35 noro 21: int Nsyz,Nsamesig;
1.1 noro 22:
23: Obj nd_top_weight;
24:
25: static NODE nd_subst;
26: static VL nd_vc;
27: static int nd_ntrans;
28: static int nd_nalg;
29: #if 0
30: static int ndv_alloc;
31: #endif
32: #if 1
33: static int nd_f4_nsp=0x7fffffff;
34: #else
35: static int nd_f4_nsp=50;
36: #endif
37: static double nd_scale=2;
38: static UINT **nd_bound;
39: static struct order_spec *nd_ord;
40: static EPOS nd_epos;
41: static BlockMask nd_blockmask;
42: static int nd_nvar;
43: static int nd_isrlex;
44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
45: static UINT nd_mask[32];
46: static UINT nd_mask0,nd_mask1;
47:
48: static NDV *nd_ps;
49: static NDV *nd_ps_trace;
50: static NDV *nd_ps_sym;
51: static NDV *nd_ps_trace_sym;
52: static RHist *nd_psh;
1.30 noro 53: static int nd_psn,nd_pslen,nd_nbase;
1.1 noro 54: static RHist *nd_red;
55: static int *nd_work_vector;
56: static int **nd_matrix;
57: static int nd_matrix_len;
58: static struct weight_or_block *nd_worb;
59: static int nd_worb_len;
60: static int nd_found,nd_create,nd_notfirst;
61: static int nmv_adv;
62: static int nd_demand;
1.21 noro 63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1 noro 64: static int nd_module_rank,nd_poly_weight_len;
65: static int *nd_poly_weight,*nd_module_weight;
66: static NODE nd_tracelist;
67: static NODE nd_alltracelist;
68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
69: static int *nd_gbblock;
70: static NODE nd_nzlist,nd_check_splist;
71: static int nd_splist;
72: static int *nd_sugarweight;
73: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27 noro 74: static DL *nd_sba_hm;
1.29 noro 75: static NODE *nd_sba_pos;
1.1 noro 76:
77: NumberField get_numberfield();
78: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
79: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
80: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
81: int nd_monic(int m,ND *p);
82: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
83: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
84: NDV pltondv(VL vl,VL dvl,LIST p);
85: void pltozpl(LIST l,Q *cont,LIST *pp);
86: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
87: void nmtodp(int mod,NM m,DP *r);
1.15 noro 88: void ndltodp(UINT *d,DP *r);
1.1 noro 89: NODE reverse_node(NODE n);
90: P ndc_div(int mod,union oNDC a,union oNDC b);
91: P ndctop(int mod,union oNDC c);
92: void finalize_tracelist(int i,P cont);
93: void conv_ilist(int demand,int trace,NODE g,int **indp);
94: void parse_nd_option(NODE opt);
95: void dltondl(int n,DL dl,UINT *r);
96: DP ndvtodp(int mod,NDV p);
97: DP ndtodp(int mod,ND p);
1.16 noro 98: DPM ndvtodpm(int mod,NDV p);
99: NDV dpmtondv(int mod,DPM p);
100: int dpm_getdeg(DPM p,int *rank);
101: void dpm_ptozp(DPM p,Z *cont,DPM *r);
102: int compdmm(int nv,DMM a,DMM b);
1.1 noro 103:
104: void Pdp_set_weight(NODE,VECT *);
105: void Pox_cmo_rpc(NODE,Obj *);
106:
107: ND nd_add_lf(ND p1,ND p2);
108: void nd_mul_c_lf(ND p,Z mul);
109: void ndv_mul_c_lf(NDV p,Z mul);
110: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
111: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
112: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
113: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
114: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
115: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
116: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
117: NODE nd_f4_lf_trace_main(int m,int **indp);
118: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
119:
120: extern int lf_lazy;
121: extern Z current_mod_lf;
122:
123: extern int Denominator,DP_Multiple,MaxDeg;
124:
125: #define BLEN (8*sizeof(unsigned long))
126:
127: typedef struct matrix {
128: int row,col;
129: unsigned long **a;
130: } *matrix;
131:
132:
133: void nd_free_private_storage()
134: {
135: _nm_free_list = 0;
136: _ndp_free_list = 0;
137: #if 0
138: GC_gcollect();
139: #endif
140: }
141:
142: void _NM_alloc()
143: {
144: NM p;
145: int i;
146:
147: for ( i = 0; i < 1024; i++ ) {
148: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
149: p->next = _nm_free_list; _nm_free_list = p;
150: }
151: }
152:
153: matrix alloc_matrix(int row,int col)
154: {
155: unsigned long **a;
156: int i,len,blen;
157: matrix mat;
158:
159: mat = (matrix)MALLOC(sizeof(struct matrix));
160: mat->row = row;
161: mat->col = col;
162: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
163: return mat;
164: }
165:
166:
167: void _ND_alloc()
168: {
169: ND p;
170: int i;
171:
172: for ( i = 0; i < 1024; i++ ) {
173: p = (ND)MALLOC(sizeof(struct oND));
174: p->body = (NM)_nd_free_list; _nd_free_list = p;
175: }
176: }
177:
178: void _NDP_alloc()
179: {
180: ND_pairs p;
181: int i;
182:
183: for ( i = 0; i < 1024; i++ ) {
184: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
185: +(nd_wpd-1)*sizeof(UINT));
186: p->next = _ndp_free_list; _ndp_free_list = p;
187: }
188: }
189:
190: INLINE int nd_length(ND p)
191: {
192: NM m;
193: int i;
194:
195: if ( !p )
196: return 0;
197: else {
198: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
199: return i;
200: }
201: }
202:
203: extern int dp_negative_weight;
204:
205: INLINE int ndl_reducible(UINT *d1,UINT *d2)
206: {
207: UINT u1,u2;
208: int i,j;
209:
210: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
211:
212: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
213: #if USE_UNROLL
214: switch ( nd_bpe ) {
215: case 3:
216: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
217: u1 = d1[i]; u2 = d2[i];
218: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
219: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
220: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
221: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
222: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
223: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
224: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
225: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
226: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
227: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
228: }
229: return 1;
230: break;
231: case 4:
232: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
233: u1 = d1[i]; u2 = d2[i];
234: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
235: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
236: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
237: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
238: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
239: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
240: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
241: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
242: }
243: return 1;
244: break;
245: case 6:
246: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
247: u1 = d1[i]; u2 = d2[i];
248: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
249: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
250: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
251: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
252: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
253: }
254: return 1;
255: break;
256: case 8:
257: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
258: u1 = d1[i]; u2 = d2[i];
259: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
260: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
261: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
262: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
263: }
264: return 1;
265: break;
266: case 16:
267: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
268: u1 = d1[i]; u2 = d2[i];
269: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
270: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
271: }
272: return 1;
273: break;
274: case 32:
275: for ( i = nd_exporigin; i < nd_wpd; i++ )
276: if ( d1[i] < d2[i] ) return 0;
277: return 1;
278: break;
279: default:
280: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
281: u1 = d1[i]; u2 = d2[i];
282: for ( j = 0; j < nd_epw; j++ )
283: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
284: }
285: return 1;
286: }
287: #else
288: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
289: u1 = d1[i]; u2 = d2[i];
290: for ( j = 0; j < nd_epw; j++ )
291: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
292: }
293: return 1;
294: #endif
295: }
296:
297: /*
298: * If the current order is a block order,
299: * then the last block is length 1 and contains
300: * the homo variable. Otherwise, the original
301: * order is either 0 or 2.
302: */
303:
304: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
305: {
306: int w,i,e,n,omask0;
307:
308: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
309: n = nd_nvar-1;
310: ndl_zero(r);
311: for ( i = 0; i < n; i++ ) {
312: e = GET_EXP_OLD(d,i);
313: PUT_EXP(r,i,e);
314: }
315: w = TD(d);
316: PUT_EXP(r,nd_nvar-1,weight-w);
317: if ( nd_module ) MPOS(r) = d[ompos];
318: TD(r) = weight;
319: if ( nd_blockmask ) ndl_weight_mask(r);
320: }
321:
322: void ndl_dehomogenize(UINT *d)
323: {
324: UINT mask;
325: UINT h;
326: int i,bits;
327:
328: if ( nd_blockmask ) {
329: h = GET_EXP(d,nd_nvar-1);
330: XOR_EXP(d,nd_nvar-1,h);
331: TD(d) -= h;
332: ndl_weight_mask(d);
333: } else {
334: if ( nd_isrlex ) {
335: if ( nd_bpe == 32 ) {
336: h = d[nd_exporigin];
337: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
338: d[i-1] = d[i];
339: d[i-1] = 0;
340: TD(d) -= h;
341: } else {
342: bits = nd_epw*nd_bpe;
343: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
344: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
345: for ( i = nd_exporigin; i < nd_wpd; i++ )
346: d[i] = ((d[i]<<nd_bpe)&mask)
347: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
348: TD(d) -= h;
349: }
350: } else {
351: h = GET_EXP(d,nd_nvar-1);
352: XOR_EXP(d,nd_nvar-1,h);
353: TD(d) -= h;
354: }
355: }
356: }
357:
358: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
359: {
360: UINT t1,t2,u,u1,u2;
361: int i,j,l;
362:
363: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
364: error("ndl_lcm : inconsistent monomials");
365: #if USE_UNROLL
366: switch ( nd_bpe ) {
367: case 3:
368: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
369: u1 = d1[i]; u2 = d2[i];
370: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
371: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
372: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
373: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
374: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
375: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
376: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
377: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
378: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
379: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
380: d[i] = u;
381: }
382: break;
383: case 4:
384: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
385: u1 = d1[i]; u2 = d2[i];
386: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
387: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
388: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
389: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
390: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
391: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
392: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
393: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
394: d[i] = u;
395: }
396: break;
397: case 6:
398: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
399: u1 = d1[i]; u2 = d2[i];
400: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
401: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
402: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
403: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
404: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
405: d[i] = u;
406: }
407: break;
408: case 8:
409: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
410: u1 = d1[i]; u2 = d2[i];
411: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
412: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
413: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
414: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
415: d[i] = u;
416: }
417: break;
418: case 16:
419: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
420: u1 = d1[i]; u2 = d2[i];
421: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
422: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
423: d[i] = u;
424: }
425: break;
426: case 32:
427: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
428: u1 = d1[i]; u2 = d2[i];
429: d[i] = u1>u2?u1:u2;
430: }
431: break;
432: default:
433: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
434: u1 = d1[i]; u2 = d2[i];
435: for ( j = 0, u = 0; j < nd_epw; j++ ) {
436: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
437: }
438: d[i] = u;
439: }
440: break;
441: }
442: #else
443: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
444: u1 = d1[i]; u2 = d2[i];
445: for ( j = 0, u = 0; j < nd_epw; j++ ) {
446: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
447: }
448: d[i] = u;
449: }
450: #endif
451: if ( nd_module ) MPOS(d) = MPOS(d1);
452: TD(d) = ndl_weight(d);
453: if ( nd_blockmask ) ndl_weight_mask(d);
454: }
455:
456: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
457: {
458: UINT t1,t2,u,u1,u2;
459: int i,j,l;
460:
461: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
462: u1 = d1[i]; u2 = d2[i];
463: for ( j = 0, u = 0; j < nd_epw; j++ ) {
464: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
465: }
466: d[i] = u;
467: }
468: }
469:
470: int ndl_weight(UINT *d)
471: {
472: UINT t,u;
473: int i,j;
474:
475: if ( current_dl_weight_vector )
476: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
477: u = GET_EXP(d,i);
478: t += MUL_WEIGHT(u,i);
479: }
480: else
481: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
482: u = d[i];
483: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
484: t += (u&nd_mask0);
485: }
1.20 noro 486: if ( nd_module && nd_module_rank && MPOS(d) )
487: t += nd_module_weight[MPOS(d)-1];
488: for ( i = nd_exporigin; i < nd_wpd; i++ )
489: if ( d[i] && !t )
490: printf("afo\n");
1.1 noro 491: return t;
492: }
493:
494: /* for sugarweight */
495:
496: int ndl_weight2(UINT *d)
497: {
498: int t,u;
499: int i,j;
500:
501: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
502: u = GET_EXP(d,i);
503: t += nd_sugarweight[i]*u;
504: }
1.20 noro 505: if ( nd_module && nd_module_rank && MPOS(d) )
506: t += nd_module_weight[MPOS(d)-1];
1.1 noro 507: return t;
508: }
509:
510: void ndl_weight_mask(UINT *d)
511: {
512: UINT t,u;
513: UINT *mask;
514: int i,j,k,l;
515:
516: l = nd_blockmask->n;
517: for ( k = 0; k < l; k++ ) {
518: mask = nd_blockmask->mask[k];
519: if ( current_dl_weight_vector )
520: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
521: u = GET_EXP_MASK(d,i,mask);
522: t += MUL_WEIGHT(u,i);
523: }
524: else
525: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
526: u = d[i]&mask[i];
527: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
528: t += (u&nd_mask0);
529: }
530: d[k+1] = t;
531: }
532: }
533:
1.21 noro 534: int ndl_glex_compare(UINT *d1,UINT *d2)
535: {
536: if ( TD(d1) > TD(d2) ) return 1;
537: else if ( TD(d1) < TD(d2) ) return -1;
538: else return ndl_lex_compare(d1,d2);
539: }
540:
1.1 noro 541: int ndl_lex_compare(UINT *d1,UINT *d2)
542: {
543: int i;
544:
545: d1 += nd_exporigin;
546: d2 += nd_exporigin;
547: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
548: if ( *d1 > *d2 )
549: return nd_isrlex ? -1 : 1;
550: else if ( *d1 < *d2 )
551: return nd_isrlex ? 1 : -1;
552: return 0;
553: }
554:
555: int ndl_block_compare(UINT *d1,UINT *d2)
556: {
557: int i,l,j,ord_o,ord_l;
558: struct order_pair *op;
559: UINT t1,t2,m;
560: UINT *mask;
561:
562: l = nd_blockmask->n;
563: op = nd_blockmask->order_pair;
564: for ( j = 0; j < l; j++ ) {
565: mask = nd_blockmask->mask[j];
566: ord_o = op[j].order;
567: if ( ord_o < 2 ) {
568: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
569: else if ( t1 < t2 ) return -1;
570: }
571: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
572: m = mask[i];
573: t1 = d1[i]&m;
574: t2 = d2[i]&m;
575: if ( t1 > t2 )
576: return !ord_o ? -1 : 1;
577: else if ( t1 < t2 )
578: return !ord_o ? 1 : -1;
579: }
580: }
581: return 0;
582: }
583:
584: int ndl_matrix_compare(UINT *d1,UINT *d2)
585: {
586: int i,j,s,row;
587: int *v;
588: Z **mat;
589: Z *w;
590: Z t1;
591: Z t,t2;
592:
1.6 noro 593: for ( j = 0; j < nd_nvar; j++ )
594: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 595: if ( nd_top_weight ) {
596: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 597: mat = (Z **)&BDY((VECT)nd_top_weight);
598: row = 1;
1.1 noro 599: } else {
600: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 601: row = ((MAT)nd_top_weight)->row;
1.1 noro 602: }
603: for ( i = 0; i < row; i++ ) {
1.6 noro 604: w = mat[i];
1.1 noro 605: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 606: STOZ(nd_work_vector[j],t1);
1.1 noro 607: mulz(w[j],t1,&t2);
608: addz(t,t2,&t1);
609: t = t1;
610: }
1.6 noro 611: if ( t ) {
612: s = sgnz(t);
1.1 noro 613: if ( s > 0 ) return 1;
614: else if ( s < 0 ) return -1;
1.6 noro 615: }
1.1 noro 616: }
1.6 noro 617: }
618: for ( i = 0; i < nd_matrix_len; i++ ) {
619: v = nd_matrix[i];
620: for ( j = 0, s = 0; j < nd_nvar; j++ )
621: s += v[j]*nd_work_vector[j];
622: if ( s > 0 ) return 1;
623: else if ( s < 0 ) return -1;
624: }
1.1 noro 625: if ( !ndl_equal(d1,d2) )
1.6 noro 626: error("ndl_matrix_compare : invalid matrix");
627: return 0;
1.1 noro 628: }
629:
630: int ndl_composite_compare(UINT *d1,UINT *d2)
631: {
632: int i,j,s,start,end,len,o;
633: int *v;
634: struct sparse_weight *sw;
635:
636: for ( j = 0; j < nd_nvar; j++ )
637: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
638: for ( i = 0; i < nd_worb_len; i++ ) {
639: len = nd_worb[i].length;
640: switch ( nd_worb[i].type ) {
641: case IS_DENSE_WEIGHT:
642: v = nd_worb[i].body.dense_weight;
643: for ( j = 0, s = 0; j < len; j++ )
644: s += v[j]*nd_work_vector[j];
645: if ( s > 0 ) return 1;
646: else if ( s < 0 ) return -1;
647: break;
648: case IS_SPARSE_WEIGHT:
649: sw = nd_worb[i].body.sparse_weight;
650: for ( j = 0, s = 0; j < len; j++ )
651: s += sw[j].value*nd_work_vector[sw[j].pos];
652: if ( s > 0 ) return 1;
653: else if ( s < 0 ) return -1;
654: break;
655: case IS_BLOCK:
656: o = nd_worb[i].body.block.order;
657: start = nd_worb[i].body.block.start;
658: switch ( o ) {
659: case 0:
660: end = start+len;
661: for ( j = start, s = 0; j < end; j++ )
662: s += MUL_WEIGHT(nd_work_vector[j],j);
663: if ( s > 0 ) return 1;
664: else if ( s < 0 ) return -1;
665: for ( j = end-1; j >= start; j-- )
666: if ( nd_work_vector[j] < 0 ) return 1;
667: else if ( nd_work_vector[j] > 0 ) return -1;
668: break;
669: case 1:
670: end = start+len;
671: for ( j = start, s = 0; j < end; j++ )
672: s += MUL_WEIGHT(nd_work_vector[j],j);
673: if ( s > 0 ) return 1;
674: else if ( s < 0 ) return -1;
675: for ( j = start; j < end; j++ )
676: if ( nd_work_vector[j] > 0 ) return 1;
677: else if ( nd_work_vector[j] < 0 ) return -1;
678: break;
679: case 2:
680: end = start+len;
681: for ( j = start; j < end; j++ )
682: if ( nd_work_vector[j] > 0 ) return 1;
683: else if ( nd_work_vector[j] < 0 ) return -1;
684: break;
685: }
686: break;
687: }
688: }
689: return 0;
690: }
691:
692: /* TDH -> WW -> TD-> RL */
693:
694: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
695: {
696: int i,m,e1,e2;
697:
698: if ( TD(d1) > TD(d2) ) return 1;
699: else if ( TD(d1) < TD(d2) ) return -1;
700: m = nd_nvar>>1;
701: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
702: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
703: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
704: }
705: if ( e1 > e2 ) return 1;
706: else if ( e1 < e2 ) return -1;
707: return ndl_lex_compare(d1,d2);
708: }
709:
1.21 noro 710: // common function for module glex and grlex comparison
711: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1 noro 712: {
1.21 noro 713: int c;
1.1 noro 714:
1.21 noro 715: switch ( nd_module_ordtype ) {
716: case 0:
717: if ( TD(d1) > TD(d2) ) return 1;
718: else if ( TD(d1) < TD(d2) ) return -1;
719: else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
720: else if ( MPOS(d1) < MPOS(d2) ) return 1;
721: else if ( MPOS(d1) > MPOS(d2) ) return -1;
722: else return 0;
723: break;
1.1 noro 724:
1.21 noro 725: case 1:
1.19 noro 726: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
727: if ( TD(d1) > TD(d2) ) return 1;
728: else if ( TD(d1) < TD(d2) ) return -1;
729: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
730: if ( MPOS(d1) < MPOS(d2) ) return 1;
731: else if ( MPOS(d1) > MPOS(d2) ) return -1;
732: }
733: if ( MPOS(d1) < MPOS(d2) ) return 1;
734: else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21 noro 735: else if ( TD(d1) > TD(d2) ) return 1;
736: else if ( TD(d1) < TD(d2) ) return -1;
737: else return ndl_lex_compare(d1,d2);
738: break;
1.1 noro 739:
1.21 noro 740: case 2: // weight -> POT
741: if ( TD(d1) > TD(d2) ) return 1;
742: else if ( TD(d1) < TD(d2) ) return -1;
743: else if ( MPOS(d1) < MPOS(d2) ) return 1;
744: else if ( MPOS(d1) > MPOS(d2) ) return -1;
745: else return ndl_lex_compare(d1,d2);
746: break;
1.1 noro 747:
1.21 noro 748: default:
749: error("ndl_module_glex_compare : invalid module_ordtype");
1.28 noro 750: return 0;
1.21 noro 751: }
1.1 noro 752: }
753:
1.21 noro 754: // common for module comparison
755: int ndl_module_compare(UINT *d1,UINT *d2)
1.1 noro 756: {
1.21 noro 757: int c;
1.1 noro 758:
1.21 noro 759: switch ( nd_module_ordtype ) {
760: case 0:
1.23 noro 761: if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21 noro 762: else if ( MPOS(d1) > MPOS(d2) ) return -1;
763: else if ( MPOS(d1) < MPOS(d2) ) return 1;
764: else return 0;
765: break;
1.1 noro 766:
1.21 noro 767: case 1:
768: if ( MPOS(d1) < MPOS(d2) ) return 1;
769: else if ( MPOS(d1) > MPOS(d2) ) return -1;
770: else return (*ndl_base_compare_function)(d1,d2);
771: break;
1.1 noro 772:
1.21 noro 773: case 2: // weight -> POT
774: if ( TD(d1) > TD(d2) ) return 1;
775: else if ( TD(d1) < TD(d2) ) return -1;
776: else if ( MPOS(d1) < MPOS(d2) ) return 1;
777: else if ( MPOS(d1) > MPOS(d2) ) return -1;
778: else return (*ndl_base_compare_function)(d1,d2);
779: break;
1.1 noro 780:
1.21 noro 781: default:
782: error("ndl_module_compare : invalid module_ordtype");
1.28 noro 783: return 0;
1.21 noro 784: }
1.1 noro 785: }
786:
1.21 noro 787: extern DMMstack dmm_stack;
788: void _addtodl(int n,DL d1,DL d2);
1.31 noro 789: void _adddl(int n,DL d1,DL d2,DL d3);
1.21 noro 790: int _eqdl(int n,DL d1,DL d2);
791:
792: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
793: {
794: int pos1,pos2,t,j;
795: DMM *in;
796: DMMstack s;
797: static DL d1=0;
798: static DL d2=0;
799: static int dlen=0;
800:
801: pos1 = MPOS(m1); pos2 = MPOS(m2);
802: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
803: if ( nd_nvar > dlen ) {
804: NEWDL(d1,nd_nvar);
805: NEWDL(d2,nd_nvar);
806: dlen = nd_nvar;
807: }
808: d1->td = TD(m1);
809: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
810: d2->td = TD(m2);
811: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
812: for ( s = dmm_stack; s; s = NEXT(s) ) {
813: in = s->in;
814: _addtodl(nd_nvar,in[pos1]->dl,d1);
815: _addtodl(nd_nvar,in[pos2]->dl,d2);
816: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
817: if ( pos1 < pos2 ) return 1;
818: else if ( pos1 > pos2 ) return -1;
819: else return 0;
820: }
821: pos1 = in[pos1]->pos;
822: pos2 = in[pos2]->pos;
823: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
824: }
825: // comparison by the bottom order
826: LAST:
827: switch ( nd_base_ordtype ) {
828: case 0:
829: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
830: if ( t ) return t;
831: else if ( pos1 < pos2 ) return 1;
832: else if ( pos1 > pos2 ) return -1;
833: else return 0;
834: break;
835: case 1:
836: if ( pos1 < pos2 ) return 1;
837: else if ( pos1 > pos2 ) return -1;
838: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
839: break;
840: case 2:
841: if ( d1->td > d2->td ) return 1;
842: else if ( d1->td < d2->td ) return -1;
843: else if ( pos1 < pos2 ) return 1;
844: else if ( pos1 > pos2 ) return -1;
845: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
846: break;
847: default:
848: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 849: return 0;
1.21 noro 850: }
1.1 noro 851: }
852:
853: INLINE int ndl_equal(UINT *d1,UINT *d2)
854: {
855: int i;
856:
857: switch ( nd_wpd ) {
858: case 2:
859: if ( TD(d2) != TD(d1) ) return 0;
860: if ( d2[1] != d1[1] ) return 0;
861: return 1;
862: break;
863: case 3:
864: if ( TD(d2) != TD(d1) ) return 0;
865: if ( d2[1] != d1[1] ) return 0;
866: if ( d2[2] != d1[2] ) return 0;
867: return 1;
868: break;
869: default:
870: for ( i = 0; i < nd_wpd; i++ )
871: if ( *d1++ != *d2++ ) return 0;
872: return 1;
873: break;
874: }
875: }
876:
877: INLINE void ndl_copy(UINT *d1,UINT *d2)
878: {
879: int i;
880:
881: switch ( nd_wpd ) {
882: case 2:
883: TD(d2) = TD(d1);
884: d2[1] = d1[1];
885: break;
886: case 3:
887: TD(d2) = TD(d1);
888: d2[1] = d1[1];
889: d2[2] = d1[2];
890: break;
891: default:
892: for ( i = 0; i < nd_wpd; i++ )
893: d2[i] = d1[i];
894: break;
895: }
896: }
897:
898: INLINE void ndl_zero(UINT *d)
899: {
900: int i;
901: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
902: }
903:
904: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
905: {
906: int i;
907:
908: if ( nd_module ) {
909: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
910: error("ndl_add : invalid operation");
911: }
912: #if 1
913: switch ( nd_wpd ) {
914: case 2:
915: TD(d) = TD(d1)+TD(d2);
916: d[1] = d1[1]+d2[1];
917: break;
918: case 3:
919: TD(d) = TD(d1)+TD(d2);
920: d[1] = d1[1]+d2[1];
921: d[2] = d1[2]+d2[2];
922: break;
923: default:
924: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
925: break;
926: }
927: #else
928: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
929: #endif
930: }
931:
932: /* d1 += d2 */
933: INLINE void ndl_addto(UINT *d1,UINT *d2)
934: {
935: int i;
936:
937: if ( nd_module ) {
938: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
939: error("ndl_addto : invalid operation");
940: }
941: #if 1
942: switch ( nd_wpd ) {
943: case 2:
944: TD(d1) += TD(d2);
945: d1[1] += d2[1];
946: break;
947: case 3:
948: TD(d1) += TD(d2);
949: d1[1] += d2[1];
950: d1[2] += d2[2];
951: break;
952: default:
953: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
954: break;
955: }
956: #else
957: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
958: #endif
959: }
960:
961: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
962: {
963: int i;
964:
965: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
966: }
967:
968: int ndl_disjoint(UINT *d1,UINT *d2)
969: {
970: UINT t1,t2,u,u1,u2;
971: int i,j;
972:
973: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
974: #if USE_UNROLL
975: switch ( nd_bpe ) {
976: case 3:
977: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
978: u1 = d1[i]; u2 = d2[i];
979: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
980: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
981: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
982: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
983: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
984: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
985: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
986: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
987: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
988: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
989: }
990: return 1;
991: break;
992: case 4:
993: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
994: u1 = d1[i]; u2 = d2[i];
995: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
996: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
997: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
998: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
999: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
1000: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1001: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1002: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1003: }
1004: return 1;
1005: break;
1006: case 6:
1007: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1008: u1 = d1[i]; u2 = d2[i];
1009: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1013: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1014: }
1015: return 1;
1016: break;
1017: case 8:
1018: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1019: u1 = d1[i]; u2 = d2[i];
1020: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1021: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1022: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1023: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1024: }
1025: return 1;
1026: break;
1027: case 16:
1028: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1029: u1 = d1[i]; u2 = d2[i];
1030: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1031: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1032: }
1033: return 1;
1034: break;
1035: case 32:
1036: for ( i = nd_exporigin; i < nd_wpd; i++ )
1037: if ( d1[i] && d2[i] ) return 0;
1038: return 1;
1039: break;
1040: default:
1041: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1042: u1 = d1[i]; u2 = d2[i];
1043: for ( j = 0; j < nd_epw; j++ ) {
1044: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1045: u1 >>= nd_bpe; u2 >>= nd_bpe;
1046: }
1047: }
1048: return 1;
1049: break;
1050: }
1051: #else
1052: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1053: u1 = d1[i]; u2 = d2[i];
1054: for ( j = 0; j < nd_epw; j++ ) {
1055: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1056: u1 >>= nd_bpe; u2 >>= nd_bpe;
1057: }
1058: }
1059: return 1;
1060: #endif
1061: }
1062:
1063: int ndl_check_bound(UINT *d1,UINT *d2)
1064: {
1065: UINT u2;
1066: int i,j,ind,k;
1067:
1068: ind = 0;
1069: #if USE_UNROLL
1070: switch ( nd_bpe ) {
1071: case 3:
1072: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1073: u2 = d2[i];
1074: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1075: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1076: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1077: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1078: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1079: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1080: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1081: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1082: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1083: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1084: }
1085: return 0;
1086: break;
1087: case 4:
1088: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1089: u2 = d2[i];
1090: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1091: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1092: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1093: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1094: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1095: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1096: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1097: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1098: }
1099: return 0;
1100: break;
1101: case 6:
1102: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1103: u2 = d2[i];
1104: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1105: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1106: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1107: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1108: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1109: }
1110: return 0;
1111: break;
1112: case 8:
1113: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1114: u2 = d2[i];
1115: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1116: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1117: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1118: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1119: }
1120: return 0;
1121: break;
1122: case 16:
1123: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1124: u2 = d2[i];
1125: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1126: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1127: }
1128: return 0;
1129: break;
1130: case 32:
1131: for ( i = nd_exporigin; i < nd_wpd; i++ )
1132: if ( d1[i]+d2[i]<d1[i] ) return 1;
1133: return 0;
1134: break;
1135: default:
1136: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1137: u2 = d2[i];
1138: k = (nd_epw-1)*nd_bpe;
1139: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1140: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1141: }
1142: return 0;
1143: break;
1144: }
1145: #else
1146: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1147: u2 = d2[i];
1148: k = (nd_epw-1)*nd_bpe;
1149: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1150: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1151: }
1152: return 0;
1153: #endif
1154: }
1155:
1156: int ndl_check_bound2(int index,UINT *d2)
1157: {
1158: return ndl_check_bound(nd_bound[index],d2);
1159: }
1160:
1161: INLINE int ndl_hash_value(UINT *d)
1162: {
1163: int i;
1.11 noro 1164: UINT r;
1.1 noro 1165:
1166: r = 0;
1167: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1168: r = (r*1511+d[i]);
1.11 noro 1169: r %= REDTAB_LEN;
1.1 noro 1170: return r;
1171: }
1172:
1173: INLINE int ndl_find_reducer(UINT *dg)
1174: {
1175: RHist r;
1176: int d,k,i;
1177:
1178: d = ndl_hash_value(dg);
1179: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1180: if ( ndl_equal(dg,DL(r)) ) {
1181: if ( k > 0 ) nd_notfirst++;
1182: nd_found++;
1183: return r->index;
1184: }
1185: }
1186: if ( Reverse )
1187: for ( i = nd_psn-1; i >= 0; i-- ) {
1188: r = nd_psh[i];
1189: if ( ndl_reducible(dg,DL(r)) ) {
1190: nd_create++;
1191: nd_append_red(dg,i);
1192: return i;
1193: }
1194: }
1195: else
1196: for ( i = 0; i < nd_psn; i++ ) {
1197: r = nd_psh[i];
1198: if ( ndl_reducible(dg,DL(r)) ) {
1199: nd_create++;
1200: nd_append_red(dg,i);
1201: return i;
1202: }
1203: }
1204: return -1;
1205: }
1206:
1.24 noro 1207: // ret=0,...,nd_psn-1 => reducer found
1208: // ret=nd_psn => reducer not found
1209: // ret=-1 => singular top reducible
1210:
1211: int comp_sig(SIG s1,SIG s2);
1212: void _ndltodl(UINT *ndl,DL dl);
1213:
1214: void print_sig(SIG s)
1215: {
1216: int i;
1217:
1218: fprintf(asir_out,"<<");
1219: for ( i = 0; i < nd_nvar; i++ ) {
1220: fprintf(asir_out,"%d",s->dl->d[i]);
1221: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1222: }
1223: fprintf(asir_out,">>*e%d",s->pos);
1224: }
1225:
1.35 noro 1226: // assuming increasing order wrt signature
1227:
1.24 noro 1228: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1229: {
1230: RHist r;
1.27 noro 1231: int i,singular,ret,d,k;
1.26 noro 1232: static int wpd,nvar;
1.24 noro 1233: static SIG quo;
1234: static UINT *tmp;
1235:
1.26 noro 1236: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1237: if ( wpd != nd_wpd ) {
1.24 noro 1238: wpd = nd_wpd;
1239: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1240: }
1.27 noro 1241: d = ndl_hash_value(dg);
1.35 noro 1242: #if 1
1.27 noro 1243: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1244: if ( ndl_equal(dg,DL(r)) ) {
1245: return r->index;
1246: }
1247: }
1.35 noro 1248: #endif
1.24 noro 1249: singular = 0;
1250: for ( i = 0; i < nd_psn; i++ ) {
1251: r = nd_psh[i];
1252: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1253: ndl_sub(dg,DL(r),tmp);
1.24 noro 1254: _ndltodl(tmp,DL(quo));
1255: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1256: quo->pos = nd_psh[i]->sig->pos;
1257: ret = comp_sig(sig,quo);
1258: if ( ret > 0 ) { singular = 0; break; }
1.29 noro 1259: if ( ret == 0 ) { fprintf(asir_out,"s"); fflush(asir_out); singular = 1; }
1.24 noro 1260: }
1261: }
1262: if ( singular ) return -1;
1.27 noro 1263: else if ( i < nd_psn )
1264: nd_append_red(dg,i);
1265: return i;
1.24 noro 1266: }
1267:
1.1 noro 1268: ND nd_merge(ND p1,ND p2)
1269: {
1270: int n,c;
1271: int t,can,td1,td2;
1272: ND r;
1273: NM m1,m2,mr0,mr,s;
1274:
1275: if ( !p1 ) return p2;
1276: else if ( !p2 ) return p1;
1277: else {
1278: can = 0;
1279: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1280: c = DL_COMPARE(DL(m1),DL(m2));
1281: switch ( c ) {
1282: case 0:
1283: s = m1; m1 = NEXT(m1);
1284: can++; NEXTNM2(mr0,mr,s);
1285: s = m2; m2 = NEXT(m2); FREENM(s);
1286: break;
1287: case 1:
1288: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1289: break;
1290: case -1:
1291: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1292: break;
1293: }
1294: }
1295: if ( !mr0 )
1296: if ( m1 ) mr0 = m1;
1297: else if ( m2 ) mr0 = m2;
1298: else return 0;
1299: else if ( m1 ) NEXT(mr) = m1;
1300: else if ( m2 ) NEXT(mr) = m2;
1301: else NEXT(mr) = 0;
1302: BDY(p1) = mr0;
1303: SG(p1) = MAX(SG(p1),SG(p2));
1304: LEN(p1) = LEN(p1)+LEN(p2)-can;
1305: FREEND(p2);
1306: return p1;
1307: }
1308: }
1309:
1310: ND nd_add(int mod,ND p1,ND p2)
1311: {
1312: int n,c;
1313: int t,can,td1,td2;
1314: ND r;
1315: NM m1,m2,mr0,mr,s;
1316:
1.11 noro 1317: Nnd_add++;
1.1 noro 1318: if ( !p1 ) return p2;
1319: else if ( !p2 ) return p1;
1320: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1321: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1322: else if ( !mod ) return nd_add_q(p1,p2);
1323: else {
1324: can = 0;
1325: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1326: c = DL_COMPARE(DL(m1),DL(m2));
1327: switch ( c ) {
1328: case 0:
1329: t = ((CM(m1))+(CM(m2))) - mod;
1330: if ( t < 0 ) t += mod;
1331: s = m1; m1 = NEXT(m1);
1332: if ( t ) {
1333: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1334: } else {
1335: can += 2; FREENM(s);
1336: }
1337: s = m2; m2 = NEXT(m2); FREENM(s);
1338: break;
1339: case 1:
1340: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1341: break;
1342: case -1:
1343: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1344: break;
1345: }
1346: }
1347: if ( !mr0 )
1348: if ( m1 ) mr0 = m1;
1349: else if ( m2 ) mr0 = m2;
1350: else return 0;
1351: else if ( m1 ) NEXT(mr) = m1;
1352: else if ( m2 ) NEXT(mr) = m2;
1353: else NEXT(mr) = 0;
1354: BDY(p1) = mr0;
1355: SG(p1) = MAX(SG(p1),SG(p2));
1356: LEN(p1) = LEN(p1)+LEN(p2)-can;
1357: FREEND(p2);
1358: return p1;
1359: }
1360: }
1361:
1362: /* XXX on opteron, the inlined manipulation of destructive additon of
1363: * two NM seems to make gcc optimizer get confused, so the part is
1364: * done in a function.
1365: */
1366:
1367: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1368: {
1369: NM s;
1370: P t;
1371: int can;
1372:
1373: addp(nd_vc,CP(*m1),CP(*m2),&t);
1374: s = *m1; *m1 = NEXT(*m1);
1375: if ( t ) {
1376: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1377: } else {
1378: can = 2; FREENM(s);
1379: }
1380: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1381: return can;
1382: }
1383:
1384: ND nd_add_q(ND p1,ND p2)
1385: {
1386: int n,c,can;
1387: ND r;
1388: NM m1,m2,mr0,mr,s;
1389: P t;
1390:
1391: if ( !p1 ) return p2;
1392: else if ( !p2 ) return p1;
1393: else {
1394: can = 0;
1395: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1396: c = DL_COMPARE(DL(m1),DL(m2));
1397: switch ( c ) {
1398: case 0:
1399: #if defined(__x86_64__)
1400: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1401: #else
1402: addp(nd_vc,CP(m1),CP(m2),&t);
1403: s = m1; m1 = NEXT(m1);
1404: if ( t ) {
1405: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1406: } else {
1407: can += 2; FREENM(s);
1408: }
1409: s = m2; m2 = NEXT(m2); FREENM(s);
1410: #endif
1411: break;
1412: case 1:
1413: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1414: break;
1415: case -1:
1416: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1417: break;
1418: }
1419: }
1420: if ( !mr0 )
1421: if ( m1 ) mr0 = m1;
1422: else if ( m2 ) mr0 = m2;
1423: else return 0;
1424: else if ( m1 ) NEXT(mr) = m1;
1425: else if ( m2 ) NEXT(mr) = m2;
1426: else NEXT(mr) = 0;
1427: BDY(p1) = mr0;
1428: SG(p1) = MAX(SG(p1),SG(p2));
1429: LEN(p1) = LEN(p1)+LEN(p2)-can;
1430: FREEND(p2);
1431: return p1;
1432: }
1433: }
1434:
1435: ND nd_add_sf(ND p1,ND p2)
1436: {
1437: int n,c,can;
1438: ND r;
1439: NM m1,m2,mr0,mr,s;
1440: int t;
1441:
1442: if ( !p1 ) return p2;
1443: else if ( !p2 ) return p1;
1444: else {
1445: can = 0;
1446: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1447: c = DL_COMPARE(DL(m1),DL(m2));
1448: switch ( c ) {
1449: case 0:
1450: t = _addsf(CM(m1),CM(m2));
1451: s = m1; m1 = NEXT(m1);
1452: if ( t ) {
1453: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1454: } else {
1455: can += 2; FREENM(s);
1456: }
1457: s = m2; m2 = NEXT(m2); FREENM(s);
1458: break;
1459: case 1:
1460: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1461: break;
1462: case -1:
1463: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1464: break;
1465: }
1466: }
1467: if ( !mr0 )
1468: if ( m1 ) mr0 = m1;
1469: else if ( m2 ) mr0 = m2;
1470: else return 0;
1471: else if ( m1 ) NEXT(mr) = m1;
1472: else if ( m2 ) NEXT(mr) = m2;
1473: else NEXT(mr) = 0;
1474: BDY(p1) = mr0;
1475: SG(p1) = MAX(SG(p1),SG(p2));
1476: LEN(p1) = LEN(p1)+LEN(p2)-can;
1477: FREEND(p2);
1478: return p1;
1479: }
1480: }
1481:
1482:
1483: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1484: {
1485: int c,c1,c2;
1486: Z cg,cred,gcd,tq;
1487: P cgp,credp,gcdp;
1488: Obj tr,tr1;
1489:
1490: if ( mod == -1 ) {
1491: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1492: *divp = (Obj)ONE;
1493: } else if ( mod == -2 ) {
1494: Z inv,t;
1495: divlf(ONE,HCZ(p),&inv);
1496: chsgnlf(HCZ(g),&t);
1497: mullf(inv,t,&CZ(mul));
1498: *divp = (Obj)ONE;
1499: } else if ( mod ) {
1500: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1501: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1502: *divp = (Obj)ONE;
1503: } else if ( nd_vc ) {
1504: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1505: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1506: chsgnp(cgp,&CP(mul));
1507: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1508: if ( dn ) {
1509: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1510: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1511: }
1512: *divp = (Obj)credp;
1513: } else {
1.6 noro 1514: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1515: chsgnz(cg,&CZ(mul));
1.1 noro 1516: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1517: if ( dn ) {
1518: mulz(dn->z,cred,&tq); dn->z = tq;
1519: }
1520: *divp = (Obj)cred;
1521: }
1522: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1523: }
1524:
1525: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1526: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1527: {
1528: NM m,mrd,tail;
1529: NM mul;
1530: int n,sugar,psugar,sugar0,stat,index;
1531: int c,c1,c2,dummy;
1532: RHist h;
1533: NDV p,red;
1534: Q cg,cred,gcd,tq,qq;
1535: Z iq;
1536: DP dmul;
1537: NODE node;
1538: LIST hist;
1539: double hmag;
1540: P tp,tp1;
1541: Obj tr,tr1,div;
1542: union oNDC hg;
1543: P cont;
1544:
1545: if ( !g ) {
1546: *rp = d;
1547: return 1;
1548: }
1549: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1550:
1551: sugar0 = sugar = SG(g);
1552: n = NV(g);
1553: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1554: if ( d )
1555: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1556: for ( ; g; ) {
1557: index = ndl_find_reducer(HDL(g));
1558: if ( index >= 0 ) {
1559: h = nd_psh[index];
1560: ndl_sub(HDL(g),DL(h),DL(mul));
1561: if ( ndl_check_bound2(index,DL(mul)) ) {
1562: nd_free(g); nd_free(d);
1563: return 0;
1564: }
1565: p = nd_demand ? ndv_load(index) : ps[index];
1566: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1567: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1568: if ( nd_gentrace ) {
1569: /* Trace=[div,index,mul,ONE] */
1.6 noro 1570: STOZ(index,iq);
1.1 noro 1571: nmtodp(mod,mul,&dmul);
1572: node = mknode(4,div,iq,dmul,ONE);
1573: }
1574: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1575: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1576: hg = HCU(g);
1577: nd_removecont2(d,g);
1.6 noro 1578: if ( nd_gentrace ) {
1.1 noro 1579: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1580: /* exact division */
1.1 noro 1581: cont = ndc_div(mod,hg,HCU(g));
1582: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1583: }
1584: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1585: }
1586: MKLIST(hist,node);
1587: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1588: } else if ( !full ) {
1589: *rp = g;
1590: return 1;
1591: } else {
1592: m = BDY(g);
1593: if ( NEXT(m) ) {
1594: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1595: } else {
1596: FREEND(g); g = 0;
1597: }
1598: if ( d ) {
1599: NEXT(tail)=m; tail=m; LEN(d)++;
1600: } else {
1601: MKND(n,m,1,d); tail = BDY(d);
1602: }
1603: }
1604: }
1605: if ( d ) SG(d) = sugar;
1606: *rp = d;
1607: return 1;
1608: }
1609:
1.24 noro 1610: // ret=1 => success
1611: // ret=0 => overflow
1612: // ret=-1 => singular top reducible
1613:
1614: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1615: {
1616: NM m,mrd,tail;
1617: NM mul;
1618: int n,sugar,psugar,sugar0,stat,index;
1619: int c,c1,c2,dummy;
1620: RHist h;
1621: NDV p,red;
1622: Q cg,cred,gcd,tq,qq;
1623: Z iq;
1624: DP dmul;
1625: NODE node;
1626: LIST hist;
1627: double hmag;
1628: P tp,tp1;
1629: Obj tr,tr1,div;
1630: union oNDC hg;
1631: P cont;
1632: SIG sig;
1633:
1634: if ( !g ) {
1635: *rp = d;
1636: return 1;
1637: }
1638: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1639:
1640: sugar0 = sugar = SG(g);
1641: n = NV(g);
1642: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1643: if ( d )
1644: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1645: sig = g->sig;
1646: for ( ; g; ) {
1647: index = ndl_find_reducer_s(HDL(g),sig);
1648: if ( index >= 0 && index < nd_psn ) {
1649: // reducer found
1650: h = nd_psh[index];
1651: ndl_sub(HDL(g),DL(h),DL(mul));
1652: if ( ndl_check_bound2(index,DL(mul)) ) {
1653: nd_free(g); nd_free(d);
1654: return 0;
1655: }
1656: p = ps[index];
1657: /* d+g -> div*(d+g)+mul*p */
1658: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1659: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1660: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1661: hg = HCU(g);
1662: nd_removecont2(d,g);
1663: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1664: }
1665: } else if ( index == -1 ) {
1666: // singular top reducible
1667: return -1;
1668: } else if ( !full ) {
1669: *rp = g;
1670: g->sig = sig;
1671: return 1;
1672: } else {
1673: m = BDY(g);
1674: if ( NEXT(m) ) {
1675: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1676: } else {
1677: FREEND(g); g = 0;
1678: }
1679: if ( d ) {
1680: NEXT(tail)=m; tail=m; LEN(d)++;
1681: } else {
1682: MKND(n,m,1,d); tail = BDY(d);
1683: }
1684: }
1685: }
1686: if ( d ) {
1687: SG(d) = sugar;
1688: d->sig = sig;
1689: }
1690: *rp = d;
1691: return 1;
1692: }
1693:
1.1 noro 1694: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1695: {
1696: int hindex,index;
1697: NDV p;
1698: ND u,d,red;
1699: NODE l;
1700: NM mul,m,mrd,tail;
1701: int sugar,psugar,n,h_reducible;
1702: PGeoBucket bucket;
1703: int c,c1,c2;
1704: Z cg,cred,gcd,zzz;
1705: RHist h;
1706: double hmag,gmag;
1707: int count = 0;
1708: int hcount = 0;
1709:
1710: if ( !g ) {
1711: *rp = 0;
1712: return 1;
1713: }
1714: sugar = SG(g);
1715: n = NV(g);
1.6 noro 1716: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1717: bucket = create_pbucket();
1718: add_pbucket(mod,bucket,g);
1719: d = 0;
1720: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1721: while ( 1 ) {
1722: if ( mod > 0 || mod == -1 )
1723: hindex = head_pbucket(mod,bucket);
1724: else if ( mod == -2 )
1725: hindex = head_pbucket_lf(bucket);
1726: else
1727: hindex = head_pbucket_q(bucket);
1728: if ( hindex < 0 ) {
1729: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1730: if ( d ) SG(d) = sugar;
1731: *rp = d;
1732: return 1;
1733: }
1734: g = bucket->body[hindex];
1735: index = ndl_find_reducer(HDL(g));
1736: if ( index >= 0 ) {
1737: count++;
1738: if ( !d ) hcount++;
1739: h = nd_psh[index];
1740: ndl_sub(HDL(g),DL(h),DL(mul));
1741: if ( ndl_check_bound2(index,DL(mul)) ) {
1742: nd_free(d);
1743: free_pbucket(bucket);
1744: *rp = 0;
1745: return 0;
1746: }
1747: p = ps[index];
1748: if ( mod == -1 )
1749: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1750: else if ( mod == -2 ) {
1751: Z inv,t;
1752: divlf(ONE,HCZ(p),&inv);
1753: chsgnlf(HCZ(g),&t);
1754: mullf(inv,t,&CZ(mul));
1755: } else if ( mod ) {
1756: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1757: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1758: } else {
1.6 noro 1759: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1760: chsgnz(cg,&CZ(mul));
1.1 noro 1761: nd_mul_c_q(d,(P)cred);
1762: mulq_pbucket(bucket,cred);
1763: g = bucket->body[hindex];
1.6 noro 1764: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1765: }
1766: red = ndv_mul_nm(mod,mul,p);
1767: bucket->body[hindex] = nd_remove_head(g);
1768: red = nd_remove_head(red);
1769: add_pbucket(mod,bucket,red);
1770: psugar = SG(p)+TD(DL(mul));
1771: sugar = MAX(sugar,psugar);
1772: if ( !mod && hmag && (gmag > hmag) ) {
1773: g = normalize_pbucket(mod,bucket);
1774: if ( !g ) {
1775: if ( d ) SG(d) = sugar;
1776: *rp = d;
1777: return 1;
1778: }
1779: nd_removecont2(d,g);
1.6 noro 1780: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1781: add_pbucket(mod,bucket,g);
1782: }
1783: } else if ( !full ) {
1784: g = normalize_pbucket(mod,bucket);
1785: if ( g ) SG(g) = sugar;
1786: *rp = g;
1787: return 1;
1788: } else {
1789: m = BDY(g);
1790: if ( NEXT(m) ) {
1791: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1792: } else {
1793: FREEND(g); g = 0;
1794: }
1795: bucket->body[hindex] = g;
1796: NEXT(m) = 0;
1797: if ( d ) {
1798: NEXT(tail)=m; tail=m; LEN(d)++;
1799: } else {
1800: MKND(n,m,1,d); tail = BDY(d);
1801: }
1802: }
1803: }
1804: }
1805:
1.25 noro 1806: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1807: {
1808: int hindex,index;
1809: NDV p;
1810: ND u,d,red;
1811: NODE l;
1812: NM mul,m,mrd,tail;
1813: int sugar,psugar,n,h_reducible;
1814: PGeoBucket bucket;
1815: int c,c1,c2;
1816: Z cg,cred,gcd,zzz;
1817: RHist h;
1818: double hmag,gmag;
1819: int count = 0;
1820: int hcount = 0;
1821: SIG sig;
1822:
1823: if ( !g ) {
1824: *rp = 0;
1825: return 1;
1826: }
1827: sugar = SG(g);
1828: n = NV(g);
1829: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1830: bucket = create_pbucket();
1831: add_pbucket(mod,bucket,g);
1832: d = 0;
1833: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1834: sig = g->sig;
1835: while ( 1 ) {
1836: if ( mod > 0 || mod == -1 )
1837: hindex = head_pbucket(mod,bucket);
1838: else if ( mod == -2 )
1839: hindex = head_pbucket_lf(bucket);
1840: else
1841: hindex = head_pbucket_q(bucket);
1842: if ( hindex < 0 ) {
1843: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1844: if ( d ) {
1845: SG(d) = sugar;
1846: d->sig = sig;
1847: }
1848: *rp = d;
1849: return 1;
1850: }
1851: g = bucket->body[hindex];
1852: index = ndl_find_reducer_s(HDL(g),sig);
1853: if ( index >= 0 && index < nd_psn ) {
1854: count++;
1855: if ( !d ) hcount++;
1856: h = nd_psh[index];
1857: ndl_sub(HDL(g),DL(h),DL(mul));
1858: if ( ndl_check_bound2(index,DL(mul)) ) {
1859: nd_free(d);
1860: free_pbucket(bucket);
1861: *rp = 0;
1862: return 0;
1863: }
1864: p = ps[index];
1865: if ( mod == -1 )
1866: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1867: else if ( mod == -2 ) {
1868: Z inv,t;
1869: divlf(ONE,HCZ(p),&inv);
1870: chsgnlf(HCZ(g),&t);
1871: mullf(inv,t,&CZ(mul));
1872: } else if ( mod ) {
1873: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1874: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1875: } else {
1876: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1877: chsgnz(cg,&CZ(mul));
1878: nd_mul_c_q(d,(P)cred);
1879: mulq_pbucket(bucket,cred);
1880: g = bucket->body[hindex];
1881: gmag = (double)p_mag((P)HCZ(g));
1882: }
1883: red = ndv_mul_nm(mod,mul,p);
1884: bucket->body[hindex] = nd_remove_head(g);
1885: red = nd_remove_head(red);
1886: add_pbucket(mod,bucket,red);
1887: psugar = SG(p)+TD(DL(mul));
1888: sugar = MAX(sugar,psugar);
1889: if ( !mod && hmag && (gmag > hmag) ) {
1890: g = normalize_pbucket(mod,bucket);
1891: if ( !g ) {
1892: if ( d ) {
1893: SG(d) = sugar;
1894: d->sig = sig;
1895: }
1896: *rp = d;
1897: return 1;
1898: }
1899: nd_removecont2(d,g);
1900: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1901: add_pbucket(mod,bucket,g);
1902: }
1903: } else if ( index == -1 ) {
1904: // singular top reducible
1905: return -1;
1906: } else if ( !full ) {
1907: g = normalize_pbucket(mod,bucket);
1908: if ( g ) {
1909: SG(g) = sugar;
1910: g->sig = sig;
1911: }
1912: *rp = g;
1913: return 1;
1914: } else {
1915: m = BDY(g);
1916: if ( NEXT(m) ) {
1917: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1918: } else {
1919: FREEND(g); g = 0;
1920: }
1921: bucket->body[hindex] = g;
1922: NEXT(m) = 0;
1923: if ( d ) {
1924: NEXT(tail)=m; tail=m; LEN(d)++;
1925: } else {
1926: MKND(n,m,1,d); tail = BDY(d);
1927: }
1928: }
1929: }
1930: }
1931:
1.1 noro 1932: /* input : list of NDV, cand : list of NDV */
1933:
1934: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1935: {
1936: int n,i,stat;
1937: ND nf,d;
1938: NDV r;
1939: NODE t,s;
1940: union oNDC dn;
1941: Z q;
1942: LIST list;
1943:
1.24 noro 1944: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1945: n = length(cand);
1946:
1947: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1948: /* membercheck : list is a subset of Id(cand) ? */
1949: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1950: again:
1951: nd_tracelist = 0;
1952: if ( nd_bpe > obpe )
1953: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1954: else
1955: r = (NDV)BDY(t);
1956: #if 0
1957: // moved to nd_f4_lf_trace()
1958: if ( m == -2 ) ndv_mod(m,r);
1959: #endif
1960: d = ndvtond(m,r);
1.6 noro 1961: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1962: if ( !stat ) {
1963: nd_reconstruct(0,0);
1964: goto again;
1965: } else if ( nf ) return 0;
1966: if ( nd_gentrace ) {
1967: nd_tracelist = reverse_node(nd_tracelist);
1968: MKLIST(list,nd_tracelist);
1.6 noro 1969: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1970: MKNODE(s,list,nd_alltracelist);
1971: nd_alltracelist = s; nd_tracelist = 0;
1972: }
1973: if ( DP_Print ) { printf("."); fflush(stdout); }
1974: }
1975: if ( DP_Print ) { printf("\n"); }
1976: return 1;
1977: }
1978:
1979: ND nd_remove_head(ND p)
1980: {
1981: NM m;
1982:
1983: m = BDY(p);
1984: if ( !NEXT(m) ) {
1985: FREEND(p); p = 0;
1986: } else {
1987: BDY(p) = NEXT(m); LEN(p)--;
1988: }
1989: FREENM(m);
1990: return p;
1991: }
1992:
1993: ND nd_separate_head(ND p,ND *head)
1994: {
1995: NM m,m0;
1996: ND r;
1997:
1998: m = BDY(p);
1999: if ( !NEXT(m) ) {
2000: *head = p; p = 0;
2001: } else {
2002: m0 = m;
2003: BDY(p) = NEXT(m); LEN(p)--;
2004: NEXT(m0) = 0;
2005: MKND(NV(p),m0,1,r);
2006: *head = r;
2007: }
2008: return p;
2009: }
2010:
2011: PGeoBucket create_pbucket()
2012: {
2013: PGeoBucket g;
2014:
2015: g = CALLOC(1,sizeof(struct oPGeoBucket));
2016: g->m = -1;
2017: return g;
2018: }
2019:
2020: void free_pbucket(PGeoBucket b) {
2021: int i;
2022:
2023: for ( i = 0; i <= b->m; i++ )
2024: if ( b->body[i] ) {
2025: nd_free(b->body[i]);
2026: b->body[i] = 0;
2027: }
2028: GCFREE(b);
2029: }
2030:
2031: void add_pbucket_symbolic(PGeoBucket g,ND d)
2032: {
2033: int l,i,k,m;
2034:
2035: if ( !d )
2036: return;
2037: l = LEN(d);
2038: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2039: /* 2^(k-1) < l <= 2^k (=m) */
2040: d = nd_merge(g->body[k],d);
2041: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2042: g->body[k] = 0;
2043: d = nd_merge(g->body[k+1],d);
2044: }
2045: g->body[k] = d;
2046: g->m = MAX(g->m,k);
2047: }
2048:
2049: void add_pbucket(int mod,PGeoBucket g,ND d)
2050: {
2051: int l,i,k,m;
2052:
2053: if ( !d )
2054: return;
2055: l = LEN(d);
2056: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2057: /* 2^(k-1) < l <= 2^k (=m) */
2058: d = nd_add(mod,g->body[k],d);
2059: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2060: g->body[k] = 0;
2061: d = nd_add(mod,g->body[k+1],d);
2062: }
2063: g->body[k] = d;
2064: g->m = MAX(g->m,k);
2065: }
2066:
2067: void mulq_pbucket(PGeoBucket g,Z c)
2068: {
2069: int k;
2070:
2071: for ( k = 0; k <= g->m; k++ )
2072: nd_mul_c_q(g->body[k],(P)c);
2073: }
2074:
2075: NM remove_head_pbucket_symbolic(PGeoBucket g)
2076: {
2077: int j,i,k,c;
2078: NM head;
2079:
2080: k = g->m;
2081: j = -1;
2082: for ( i = 0; i <= k; i++ ) {
2083: if ( !g->body[i] ) continue;
2084: if ( j < 0 ) j = i;
2085: else {
2086: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2087: if ( c > 0 )
2088: j = i;
2089: else if ( c == 0 )
2090: g->body[i] = nd_remove_head(g->body[i]);
2091: }
2092: }
2093: if ( j < 0 ) return 0;
2094: else {
2095: head = BDY(g->body[j]);
2096: if ( !NEXT(head) ) {
2097: FREEND(g->body[j]);
2098: g->body[j] = 0;
2099: } else {
2100: BDY(g->body[j]) = NEXT(head);
2101: LEN(g->body[j])--;
2102: }
2103: return head;
2104: }
2105: }
2106:
2107: int head_pbucket(int mod,PGeoBucket g)
2108: {
2109: int j,i,c,k,nv,sum;
2110: UINT *di,*dj;
2111: ND gi,gj;
2112:
2113: k = g->m;
2114: while ( 1 ) {
2115: j = -1;
2116: for ( i = 0; i <= k; i++ ) {
2117: if ( !(gi = g->body[i]) )
2118: continue;
2119: if ( j < 0 ) {
2120: j = i;
2121: gj = g->body[j];
2122: dj = HDL(gj);
2123: sum = HCM(gj);
2124: } else {
2125: c = DL_COMPARE(HDL(gi),dj);
2126: if ( c > 0 ) {
2127: if ( sum ) HCM(gj) = sum;
2128: else g->body[j] = nd_remove_head(gj);
2129: j = i;
2130: gj = g->body[j];
2131: dj = HDL(gj);
2132: sum = HCM(gj);
2133: } else if ( c == 0 ) {
2134: if ( mod == -1 )
2135: sum = _addsf(sum,HCM(gi));
2136: else {
2137: sum = sum+HCM(gi)-mod;
2138: if ( sum < 0 ) sum += mod;
2139: }
2140: g->body[i] = nd_remove_head(gi);
2141: }
2142: }
2143: }
2144: if ( j < 0 ) return -1;
2145: else if ( sum ) {
2146: HCM(gj) = sum;
2147: return j;
2148: } else
2149: g->body[j] = nd_remove_head(gj);
2150: }
2151: }
2152:
2153: int head_pbucket_q(PGeoBucket g)
2154: {
2155: int j,i,c,k,nv;
2156: Z sum,t;
2157: ND gi,gj;
2158:
2159: k = g->m;
2160: while ( 1 ) {
2161: j = -1;
2162: for ( i = 0; i <= k; i++ ) {
2163: if ( !(gi = g->body[i]) ) continue;
2164: if ( j < 0 ) {
2165: j = i;
2166: gj = g->body[j];
1.6 noro 2167: sum = HCZ(gj);
1.1 noro 2168: } else {
2169: nv = NV(gi);
2170: c = DL_COMPARE(HDL(gi),HDL(gj));
2171: if ( c > 0 ) {
1.6 noro 2172: if ( sum ) HCZ(gj) = sum;
1.1 noro 2173: else g->body[j] = nd_remove_head(gj);
2174: j = i;
2175: gj = g->body[j];
1.6 noro 2176: sum = HCZ(gj);
1.1 noro 2177: } else if ( c == 0 ) {
1.6 noro 2178: addz(sum,HCZ(gi),&t);
1.1 noro 2179: sum = t;
2180: g->body[i] = nd_remove_head(gi);
2181: }
2182: }
2183: }
2184: if ( j < 0 ) return -1;
2185: else if ( sum ) {
1.6 noro 2186: HCZ(gj) = sum;
1.1 noro 2187: return j;
2188: } else
2189: g->body[j] = nd_remove_head(gj);
2190: }
2191: }
2192:
2193: int head_pbucket_lf(PGeoBucket g)
2194: {
2195: int j,i,c,k,nv;
2196: Z sum,t;
2197: ND gi,gj;
2198:
2199: k = g->m;
2200: while ( 1 ) {
2201: j = -1;
2202: for ( i = 0; i <= k; i++ ) {
2203: if ( !(gi = g->body[i]) ) continue;
2204: if ( j < 0 ) {
2205: j = i;
2206: gj = g->body[j];
2207: sum = HCZ(gj);
2208: } else {
2209: nv = NV(gi);
2210: c = DL_COMPARE(HDL(gi),HDL(gj));
2211: if ( c > 0 ) {
2212: if ( sum ) HCZ(gj) = sum;
2213: else g->body[j] = nd_remove_head(gj);
2214: j = i;
2215: gj = g->body[j];
2216: sum = HCZ(gj);
2217: } else if ( c == 0 ) {
2218: addlf(sum,HCZ(gi),&t);
2219: sum = t;
2220: g->body[i] = nd_remove_head(gi);
2221: }
2222: }
2223: }
2224: if ( j < 0 ) return -1;
2225: else if ( sum ) {
2226: HCZ(gj) = sum;
2227: return j;
2228: } else
2229: g->body[j] = nd_remove_head(gj);
2230: }
2231: }
2232:
2233: ND normalize_pbucket(int mod,PGeoBucket g)
2234: {
2235: int i;
2236: ND r,t;
2237:
2238: r = 0;
2239: for ( i = 0; i <= g->m; i++ ) {
2240: r = nd_add(mod,r,g->body[i]);
2241: g->body[i] = 0;
2242: }
2243: g->m = -1;
2244: return r;
2245: }
2246:
2247: #if 0
2248: void register_hcf(NDV p)
2249: {
2250: DCP dc,t;
2251: P hc,h;
2252: int c;
2253: NODE l,l1,prev;
2254:
2255: hc = p->body->c.p;
2256: if ( !nd_vc || NUM(hc) ) return;
2257: fctrp(nd_vc,hc,&dc);
2258: for ( t = dc; t; t = NEXT(t) ) {
2259: h = t->c;
2260: if ( NUM(h) ) continue;
2261: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2262: c = compp(nd_vc,h,(P)BDY(l));
2263: if ( c >= 0 ) break;
2264: }
2265: if ( !l || c > 0 ) {
2266: MKNODE(l1,h,l);
2267: if ( !prev )
2268: nd_hcf = l1;
2269: else
2270: NEXT(prev) = l1;
2271: }
2272: }
2273: }
2274: #else
2275: void register_hcf(NDV p)
2276: {
2277: DCP dc,t;
2278: P hc,h,q;
2279: Q dmy;
2280: int c;
2281: NODE l,l1,prev;
2282:
2283: hc = p->body->c.p;
2284: if ( NUM(hc) ) return;
2285: ptozp(hc,1,&dmy,&h);
2286: #if 1
2287: for ( l = nd_hcf; l; l = NEXT(l) ) {
2288: while ( 1 ) {
2289: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2290: else break;
2291: }
2292: }
2293: if ( NUM(h) ) return;
2294: #endif
2295: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2296: c = compp(nd_vc,h,(P)BDY(l));
2297: if ( c >= 0 ) break;
2298: }
2299: if ( !l || c > 0 ) {
2300: MKNODE(l1,h,l);
2301: if ( !prev )
2302: nd_hcf = l1;
2303: else
2304: NEXT(prev) = l1;
2305: }
2306: }
2307: #endif
2308:
2309: int do_diagonalize(int sugar,int m)
2310: {
1.6 noro 2311: int i,nh,stat;
2312: NODE r,g,t;
2313: ND h,nf,s,head;
2314: NDV nfv;
2315: Q q;
2316: P nm,nmp,dn,mnp,dnp,cont,cont1;
2317: union oNDC hc;
2318: NODE node;
2319: LIST l;
2320: Z iq;
1.1 noro 2321:
1.6 noro 2322: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2323: if ( nd_gentrace ) {
2324: /* Trace = [1,index,1,1] */
2325: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2326: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2327: }
2328: if ( nd_demand )
2329: nfv = ndv_load(i);
2330: else
2331: nfv = nd_ps[i];
2332: s = ndvtond(m,nfv);
2333: s = nd_separate_head(s,&head);
2334: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2335: if ( !stat ) return 0;
2336: ndv_free(nfv);
2337: hc = HCU(nf); nd_removecont(m,nf);
2338: /* exact division */
2339: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2340: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2341: nfv = ndtondv(m,nf);
2342: nd_free(nf);
2343: nd_bound[i] = ndv_compute_bound(nfv);
2344: if ( !m ) register_hcf(nfv);
2345: if ( nd_demand ) {
2346: ndv_save(nfv,i);
2347: ndv_free(nfv);
2348: } else
2349: nd_ps[i] = nfv;
2350: }
2351: return 1;
1.1 noro 2352: }
2353:
2354: LIST compute_splist()
2355: {
2356: NODE g,tn0,tn,node;
2357: LIST l0;
2358: ND_pairs d,t;
2359: int i;
2360: Z i1,i2;
2361:
2362: g = 0; d = 0;
2363: for ( i = 0; i < nd_psn; i++ ) {
2364: d = update_pairs(d,g,i,0);
2365: g = update_base(g,i);
2366: }
2367: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2368: NEXTNODE(tn0,tn);
1.6 noro 2369: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2370: node = mknode(2,i1,i2); MKLIST(l0,node);
2371: BDY(tn) = l0;
2372: }
2373: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2374: return l0;
2375: }
2376:
2377: /* return value = 0 => input is not a GB */
2378:
2379: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2380: {
1.6 noro 2381: int i,nh,sugar,stat;
2382: NODE r,g,t;
2383: ND_pairs d;
2384: ND_pairs l;
2385: ND h,nf,s,head,nf1;
2386: NDV nfv;
2387: Z q;
2388: union oNDC dn,hc;
2389: int diag_count = 0;
2390: P cont;
2391: LIST list;
1.27 noro 2392: struct oEGT eg1,eg2,eg_update;
1.6 noro 2393:
1.27 noro 2394: init_eg(&eg_update);
1.11 noro 2395: Nnd_add = 0;
1.6 noro 2396: g = 0; d = 0;
2397: for ( i = 0; i < nd_psn; i++ ) {
2398: d = update_pairs(d,g,i,gensyz);
2399: g = update_base(g,i);
2400: }
2401: sugar = 0;
2402: while ( d ) {
1.1 noro 2403: again:
1.6 noro 2404: l = nd_minp(d,&d);
2405: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2406: if ( SG(l) != sugar ) {
2407: if ( ishomo ) {
2408: diag_count = 0;
2409: stat = do_diagonalize(sugar,m);
1.1 noro 2410: if ( !stat ) {
1.6 noro 2411: NEXT(l) = d; d = l;
2412: d = nd_reconstruct(0,d);
2413: goto again;
1.1 noro 2414: }
1.6 noro 2415: }
2416: sugar = SG(l);
2417: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2418: }
2419: stat = nd_sp(m,0,l,&h);
2420: if ( !stat ) {
2421: NEXT(l) = d; d = l;
2422: d = nd_reconstruct(0,d);
2423: goto again;
2424: }
1.1 noro 2425: #if USE_GEOBUCKET
1.6 noro 2426: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2427: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2428: #else
1.6 noro 2429: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2430: #endif
1.6 noro 2431: if ( !stat ) {
2432: NEXT(l) = d; d = l;
2433: d = nd_reconstruct(0,d);
2434: goto again;
2435: } else if ( nf ) {
2436: if ( checkonly || gensyz ) return 0;
1.1 noro 2437: if ( nd_newelim ) {
2438: if ( nd_module ) {
2439: if ( MPOS(HDL(nf)) > 1 ) return 0;
2440: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2441: }
1.6 noro 2442: if ( DP_Print ) { printf("+"); fflush(stdout); }
2443: hc = HCU(nf);
2444: nd_removecont(m,nf);
2445: if ( !m && nd_nalg ) {
2446: nd_monic(0,&nf);
2447: nd_removecont(m,nf);
2448: }
2449: if ( nd_gentrace ) {
2450: /* exact division */
1.1 noro 2451: cont = ndc_div(m,hc,HCU(nf));
2452: if ( m || !UNIQ(cont) ) {
1.6 noro 2453: t = mknode(4,NULLP,NULLP,NULLP,cont);
2454: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2455: nd_tracelist = t;
2456: }
2457: }
1.6 noro 2458: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2459: nh = ndv_newps(m,nfv,0);
1.6 noro 2460: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2461: diag_count = 0;
2462: stat = do_diagonalize(sugar,m);
2463: if ( !stat ) {
2464: NEXT(l) = d; d = l;
2465: d = nd_reconstruct(1,d);
2466: goto again;
1.1 noro 2467: }
1.6 noro 2468: }
1.27 noro 2469: get_eg(&eg1);
1.6 noro 2470: d = update_pairs(d,g,nh,0);
1.27 noro 2471: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2472: g = update_base(g,nh);
2473: FREENDP(l);
2474: } else {
2475: if ( nd_gentrace && gensyz ) {
2476: nd_tracelist = reverse_node(nd_tracelist);
2477: MKLIST(list,nd_tracelist);
2478: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2479: MKNODE(t,list,nd_alltracelist);
2480: nd_alltracelist = t; nd_tracelist = 0;
2481: }
2482: if ( DP_Print ) { printf("."); fflush(stdout); }
2483: FREENDP(l);
1.36 noro 2484: }
2485: }
2486: conv_ilist(nd_demand,0,g,indp);
2487: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
2488:
2489: if ( DP_Print )
2490: print_eg("update",&eg_update);
2491: return g;
1.1 noro 2492: }
2493:
1.30 noro 2494: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
2495: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24 noro 2496:
2497: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2498: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2499:
2500: void _copydl(int n,DL d1,DL d2);
2501: void _subfromdl(int n,DL d1,DL d2);
2502: extern int (*cmpdl)(int n,DL d1,DL d2);
2503:
2504: NODE insert_sig(NODE l,SIG s)
2505: {
2506: int pos;
2507: DL sig;
2508: struct oNODE root;
2509: NODE p,prev,r;
2510: SIG t;
2511:
2512: pos = s->pos; sig = DL(s);
2513: root.next = l; prev = &root;
2514: for ( p = l; p; p = p->next ) {
2515: t = (SIG)p->body;
2516: if ( t->pos == pos ) {
2517: if ( _dl_redble(DL(t),sig,nd_nvar) )
2518: return root.next;
2519: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2520: // remove p
2521: prev->next = p->next;
2522: } else
2523: prev = p;
2524: }
1.27 noro 2525: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2526: for ( p = &root; p->next; p = p->next );
2527: p->next = r;
2528: // r->next = root.next;
2529: // return r;
2530: return root.next;
1.24 noro 2531: }
2532:
2533: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2534: {
2535: struct oND_pairs root;
2536: ND_pairs prev,p;
2537: SIG spsig;
2538:
2539: root.next = d;
2540: prev = &root; p = d;
2541: while ( p ) {
2542: spsig = p->sig;
1.26 noro 2543: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2544: // remove p
2545: prev->next = p->next;
1.26 noro 2546: Nsyz++;
2547: } else
1.24 noro 2548: prev = p;
2549: p = p->next;
2550: }
2551: return (ND_pairs)root.next;
2552: }
2553:
1.29 noro 2554: int _dl_redble_ext(DL,DL,DL,int);
2555:
1.28 noro 2556: int small_lcm(ND_pairs l)
2557: {
2558: SIG sig;
2559: int i;
1.29 noro 2560: NODE t;
1.28 noro 2561: static DL lcm,mul,quo;
2562: static int nvar;
2563:
2564: if ( nvar < nd_nvar ) {
2565: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2566: }
2567: sig = l->sig;
2568: _ndltodl(l->lcm,lcm);
1.29 noro 2569: #if 0
1.28 noro 2570: for ( i = 0; i < nd_psn; i++ ) {
2571: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2572: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2573: _ndltodl(DL(nd_psh[i]),mul);
2574: _addtodl(nd_nvar,quo,mul);
2575: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2576: break;
2577: }
2578: }
2579: if ( i < nd_psn ) return 1;
2580: else return 0;
1.29 noro 2581: #else
2582: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2583: i = (long)BDY(t);
2584: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2585: _ndltodl(DL(nd_psh[i]),mul);
2586: _addtodl(nd_nvar,quo,mul);
2587: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2588: break;
2589: }
2590: }
2591: if ( t ) return 1;
2592: else return 0;
2593: #endif
1.28 noro 2594: }
2595:
2596: ND_pairs remove_large_lcm(ND_pairs d)
2597: {
2598: struct oND_pairs root;
2599: ND_pairs prev,p;
2600:
2601: root.next = d;
2602: prev = &root; p = d;
2603: while ( p ) {
2604: if ( small_lcm(p) ) {
2605: // remove p
2606: prev->next = p->next;
2607: } else
2608: prev = p;
2609: p = p->next;
2610: }
2611: return (ND_pairs)root.next;
2612: }
2613:
1.26 noro 2614: struct oEGT eg_create,eg_newpairs,eg_merge;
2615:
1.30 noro 2616: NODE conv_ilist_s(int demand,int trace,int **indp);
2617:
1.24 noro 2618: NODE nd_sba_buch(int m,int ishomo,int **indp)
2619: {
1.29 noro 2620: int i,j,nh,sugar,stat;
1.30 noro 2621: NODE r,t,g;
1.24 noro 2622: ND_pairs d;
2623: ND_pairs l;
2624: ND h,nf,s,head,nf1;
2625: NDV nfv;
2626: Z q;
2627: union oNDC dn,hc;
2628: P cont;
2629: LIST list;
2630: SIG sig;
1.29 noro 2631: NODE *syzlist;
1.26 noro 2632: int Nredundant;
1.28 noro 2633: DL lcm,quo,mul;
1.31 noro 2634: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24 noro 2635:
1.27 noro 2636: init_eg(&eg_remove);
1.29 noro 2637: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26 noro 2638: Nsyz = 0;
1.24 noro 2639: Nnd_add = 0;
1.26 noro 2640: Nredundant = 0;
1.30 noro 2641: d = 0;
1.24 noro 2642: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 2643: d = update_pairs_s(d,i,syzlist);
1.24 noro 2644: }
1.29 noro 2645: for ( i = 0; i < nd_psn; i++ )
2646: for ( j = i+1; j < nd_psn; j++ ) {
2647: NEWSIG(sig); sig->pos = j;
2648: _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
2649: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
2650: }
1.24 noro 2651: sugar = 0;
1.26 noro 2652: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2653: init_eg(&eg_create);
2654: init_eg(&eg_merge);
1.29 noro 2655: init_eg(&eg_large);
1.31 noro 2656: init_eg(&eg_nf);
2657: init_eg(&eg_nfzero);
1.24 noro 2658: while ( d ) {
2659: again:
1.26 noro 2660: if ( DP_Print ) {
2661: int len;
2662: ND_pairs td;
1.28 noro 2663: for ( td = d, len=0; td; td = td->next, len++)
2664: ;
1.26 noro 2665: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2666: }
1.24 noro 2667: l = d; d = d->next;
1.28 noro 2668: if ( small_lcm(l) ) {
1.24 noro 2669: if ( DP_Print ) fprintf(asir_out,"M");
1.26 noro 2670: Nredundant++;
1.24 noro 2671: continue;
2672: }
2673: if ( SG(l) != sugar ) {
2674: sugar = SG(l);
2675: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2676: }
1.28 noro 2677: sig = l->sig;
1.24 noro 2678: stat = nd_sp(m,0,l,&h);
2679: if ( !stat ) {
2680: NEXT(l) = d; d = l;
2681: d = nd_reconstruct(0,d);
2682: goto again;
2683: }
1.31 noro 2684: get_eg(&eg1);
1.25 noro 2685: #if USE_GEOBUCKET
1.24 noro 2686: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2687: #else
2688: stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2689: #endif
1.31 noro 2690: get_eg(&eg2);
1.24 noro 2691: if ( !stat ) {
2692: NEXT(l) = d; d = l;
2693: d = nd_reconstruct(0,d);
2694: goto again;
2695: } else if ( stat == -1 ) {
2696: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 noro 2697: FREENDP(l);
1.24 noro 2698: } else if ( nf ) {
2699: if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31 noro 2700: add_eg(&eg_nf,&eg1,&eg2);
1.24 noro 2701: hc = HCU(nf);
2702: nd_removecont(m,nf);
2703: nfv = ndtondv(m,nf); nd_free(nf);
2704: nh = ndv_newps(m,nfv,0);
1.26 noro 2705:
1.30 noro 2706: d = update_pairs_s(d,nh,syzlist);
1.29 noro 2707: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2708: FREENDP(l);
2709: } else {
1.31 noro 2710: add_eg(&eg_nfzero,&eg1,&eg2);
1.24 noro 2711: // syzygy
1.27 noro 2712: get_eg(&eg1);
1.24 noro 2713: d = remove_spair_s(d,sig);
1.27 noro 2714: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2715: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2716: if ( DP_Print ) { printf("."); fflush(stdout); }
2717: FREENDP(l);
2718: }
2719: }
1.30 noro 2720: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2721: if ( DP_Print ) {
1.35 noro 2722: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nsamesig,Nredundant);
1.26 noro 2723: fflush(stdout);
2724: print_eg("create",&eg_create);
2725: print_eg("merge",&eg_merge);
1.27 noro 2726: print_eg("remove",&eg_remove);
1.31 noro 2727: print_eg("nf",&eg_nf);
2728: print_eg("nfzero",&eg_nfzero);
1.27 noro 2729: printf("\n");
1.26 noro 2730: }
1.24 noro 2731: return g;
2732: }
2733:
1.1 noro 2734: /* splist = [[i1,i2],...] */
2735:
2736: int check_splist(int m,NODE splist)
2737: {
2738: NODE t,p;
2739: ND_pairs d,r,l;
2740: int stat;
2741: ND h,nf;
2742:
2743: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2744: p = BDY((LIST)BDY(t));
1.6 noro 2745: NEXTND_pairs(d,r);
2746: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2747: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2748: SG(r) = TD(LCM(r)); /* XXX */
2749: }
2750: if ( d ) NEXT(r) = 0;
2751:
1.6 noro 2752: while ( d ) {
1.1 noro 2753: again:
1.6 noro 2754: l = nd_minp(d,&d);
2755: stat = nd_sp(m,0,l,&h);
2756: if ( !stat ) {
2757: NEXT(l) = d; d = l;
2758: d = nd_reconstruct(0,d);
2759: goto again;
2760: }
2761: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2762: if ( !stat ) {
2763: NEXT(l) = d; d = l;
2764: d = nd_reconstruct(0,d);
2765: goto again;
2766: } else if ( nf ) return 0;
1.1 noro 2767: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2768: }
1.1 noro 2769: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2770: return 1;
2771: }
2772:
2773: int check_splist_f4(int m,NODE splist)
2774: {
2775: UINT *s0vect;
1.6 noro 2776: PGeoBucket bucket;
1.1 noro 2777: NODE p,rp0,t;
2778: ND_pairs d,r,l,ll;
2779: int col,stat;
2780:
2781: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2782: p = BDY((LIST)BDY(t));
1.6 noro 2783: NEXTND_pairs(d,r);
2784: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2785: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2786: SG(r) = TD(LCM(r)); /* XXX */
2787: }
2788: if ( d ) NEXT(r) = 0;
2789:
1.6 noro 2790: while ( d ) {
2791: l = nd_minsugarp(d,&d);
2792: bucket = create_pbucket();
2793: stat = nd_sp_f4(m,0,l,bucket);
2794: if ( !stat ) {
2795: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2796: NEXT(ll) = d; d = l;
2797: d = nd_reconstruct(0,d);
2798: continue;
2799: }
2800: if ( bucket->m < 0 ) continue;
2801: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2802: if ( !col ) {
2803: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2804: NEXT(ll) = d; d = l;
2805: d = nd_reconstruct(0,d);
2806: continue;
1.1 noro 2807: }
1.6 noro 2808: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2809: }
2810: return 1;
1.1 noro 2811: }
2812:
2813: int do_diagonalize_trace(int sugar,int m)
2814: {
1.6 noro 2815: int i,nh,stat;
2816: NODE r,g,t;
2817: ND h,nf,nfq,s,head;
2818: NDV nfv,nfqv;
2819: Q q,den,num;
2820: union oNDC hc;
2821: NODE node;
2822: LIST l;
2823: Z iq;
2824: P cont,cont1;
1.1 noro 2825:
1.6 noro 2826: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2827: if ( nd_gentrace ) {
2828: /* Trace = [1,index,1,1] */
2829: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2830: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2831: }
2832: /* for nd_ps */
2833: s = ndvtond(m,nd_ps[i]);
2834: s = nd_separate_head(s,&head);
2835: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2836: if ( !stat ) return 0;
2837: nf = nd_add(m,head,nf);
2838: ndv_free(nd_ps[i]);
2839: nd_ps[i] = ndtondv(m,nf);
2840: nd_free(nf);
2841:
2842: /* for nd_ps_trace */
2843: if ( nd_demand )
2844: nfv = ndv_load(i);
2845: else
2846: nfv = nd_ps_trace[i];
2847: s = ndvtond(0,nfv);
2848: s = nd_separate_head(s,&head);
2849: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2850: if ( !stat ) return 0;
2851: ndv_free(nfv);
2852: hc = HCU(nf); nd_removecont(0,nf);
2853: /* exact division */
1.1 noro 2854: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2855: if ( nd_gentrace ) finalize_tracelist(i,cont);
2856: nfv = ndtondv(0,nf);
2857: nd_free(nf);
2858: nd_bound[i] = ndv_compute_bound(nfv);
2859: register_hcf(nfv);
2860: if ( nd_demand ) {
2861: ndv_save(nfv,i);
2862: ndv_free(nfv);
2863: } else
2864: nd_ps_trace[i] = nfv;
2865: }
2866: return 1;
1.1 noro 2867: }
2868:
2869: static struct oEGT eg_invdalg;
2870: struct oEGT eg_le;
2871:
2872: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2873: {
2874: NODE tn;
2875: P p1;
2876:
2877: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2878: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2879: }
2880: *r = p;
2881: }
2882:
2883: NODE nd_gb_trace(int m,int ishomo,int **indp)
2884: {
1.6 noro 2885: int i,nh,sugar,stat;
2886: NODE r,g,t;
2887: ND_pairs d;
2888: ND_pairs l;
2889: ND h,nf,nfq,s,head;
2890: NDV nfv,nfqv;
2891: Z q,den,num;
2892: P hc;
2893: union oNDC dn,hnfq;
2894: struct oEGT eg_monic,egm0,egm1;
2895: int diag_count = 0;
2896: P cont;
2897: LIST list;
2898:
2899: init_eg(&eg_monic);
2900: init_eg(&eg_invdalg);
2901: init_eg(&eg_le);
2902: g = 0; d = 0;
2903: for ( i = 0; i < nd_psn; i++ ) {
2904: d = update_pairs(d,g,i,0);
2905: g = update_base(g,i);
2906: }
2907: sugar = 0;
2908: while ( d ) {
1.1 noro 2909: again:
1.6 noro 2910: l = nd_minp(d,&d);
2911: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2912: if ( SG(l) != sugar ) {
1.1 noro 2913: #if 1
1.6 noro 2914: if ( ishomo ) {
2915: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2916: stat = do_diagonalize_trace(sugar,m);
2917: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2918: diag_count = 0;
1.1 noro 2919: if ( !stat ) {
1.6 noro 2920: NEXT(l) = d; d = l;
2921: d = nd_reconstruct(1,d);
2922: goto again;
1.1 noro 2923: }
1.6 noro 2924: }
2925: #endif
2926: sugar = SG(l);
2927: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2928: }
2929: stat = nd_sp(m,0,l,&h);
2930: if ( !stat ) {
2931: NEXT(l) = d; d = l;
2932: d = nd_reconstruct(1,d);
2933: goto again;
2934: }
1.1 noro 2935: #if USE_GEOBUCKET
1.6 noro 2936: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2937: #else
1.6 noro 2938: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2939: #endif
1.6 noro 2940: if ( !stat ) {
2941: NEXT(l) = d; d = l;
2942: d = nd_reconstruct(1,d);
2943: goto again;
2944: } else if ( nf ) {
2945: if ( nd_demand ) {
2946: nfqv = ndv_load(nd_psn);
2947: nfq = ndvtond(0,nfqv);
2948: } else
2949: nfq = 0;
2950: if ( !nfq ) {
2951: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2952: NEXT(l) = d; d = l;
2953: d = nd_reconstruct(1,d);
2954: goto again;
2955: }
2956: }
2957: if ( nfq ) {
2958: /* m|HC(nfq) => failure */
2959: if ( nd_vc ) {
2960: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2961: } else
2962: q = HCZ(nfq);
2963: if ( !remqi((Q)q,m) ) return 0;
2964:
2965: if ( DP_Print ) { printf("+"); fflush(stdout); }
2966: hnfq = HCU(nfq);
2967: if ( nd_nalg ) {
2968: /* m|DN(HC(nf)^(-1)) => failure */
2969: get_eg(&egm0);
2970: if ( !nd_monic(m,&nfq) ) return 0;
2971: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2972: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2973: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2974: } else {
2975: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2976: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2977: }
2978: if ( nd_gentrace ) {
2979: /* exact division */
2980: cont = ndc_div(0,hnfq,HCU(nfqv));
2981: if ( !UNIQ(cont) ) {
2982: t = mknode(4,NULLP,NULLP,NULLP,cont);
2983: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2984: nd_tracelist = t;
2985: }
2986: }
1.24 noro 2987: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 2988: if ( ishomo && ++diag_count == diag_period ) {
2989: diag_count = 0;
2990: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2991: stat = do_diagonalize_trace(sugar,m);
2992: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2993: if ( !stat ) {
1.1 noro 2994: NEXT(l) = d; d = l;
2995: d = nd_reconstruct(1,d);
2996: goto again;
1.6 noro 2997: }
1.1 noro 2998: }
1.6 noro 2999: d = update_pairs(d,g,nh,0);
3000: g = update_base(g,nh);
3001: } else {
3002: if ( DP_Print ) { printf("*"); fflush(stdout); }
3003: }
3004: } else {
3005: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 3006: }
1.6 noro 3007: FREENDP(l);
3008: }
3009: if ( nd_nalg ) {
3010: if ( DP_Print ) {
3011: print_eg("monic",&eg_monic);
3012: print_eg("invdalg",&eg_invdalg);
3013: print_eg("le",&eg_le);
1.1 noro 3014: }
1.6 noro 3015: }
1.1 noro 3016: conv_ilist(nd_demand,1,g,indp);
1.6 noro 3017: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
3018: return g;
1.1 noro 3019: }
3020:
3021: int ndv_compare(NDV *p1,NDV *p2)
3022: {
3023: return DL_COMPARE(HDL(*p1),HDL(*p2));
3024: }
3025:
3026: int ndv_compare_rev(NDV *p1,NDV *p2)
3027: {
3028: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3029: }
3030:
3031: int ndvi_compare(NDVI p1,NDVI p2)
3032: {
3033: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3034: }
3035:
3036: int ndvi_compare_rev(NDVI p1,NDVI p2)
3037: {
3038: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3039: }
3040:
3041: NODE ndv_reduceall(int m,NODE f)
3042: {
3043: int i,j,n,stat;
3044: ND nf,g,head;
3045: NODE t,a0,a;
3046: union oNDC dn;
3047: Q q,num,den;
3048: NODE node;
3049: LIST l;
3050: Z iq,jq;
3051: int *perm;
3052: union oNDC hc;
3053: P cont,cont1;
3054:
3055: if ( nd_nora ) return f;
3056: n = length(f);
1.24 noro 3057: ndv_setup(m,0,f,0,1,0);
1.1 noro 3058: perm = (int *)MALLOC(n*sizeof(int));
3059: if ( nd_gentrace ) {
3060: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3061: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3062: }
3063: for ( i = 0; i < n; ) {
3064: if ( nd_gentrace ) {
3065: /* Trace = [1,index,1,1] */
1.6 noro 3066: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3067: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3068: }
3069: g = ndvtond(m,nd_ps[i]);
3070: g = nd_separate_head(g,&head);
1.6 noro 3071: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3072: if ( !stat )
3073: nd_reconstruct(0,0);
3074: else {
3075: if ( DP_Print ) { printf("."); fflush(stdout); }
3076: ndv_free(nd_ps[i]);
3077: hc = HCU(nf); nd_removecont(m,nf);
3078: if ( nd_gentrace ) {
3079: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3080: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3081: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3082: }
1.6 noro 3083: /* exact division */
1.1 noro 3084: cont = ndc_div(m,hc,HCU(nf));
3085: finalize_tracelist(perm[i],cont);
3086: }
3087: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3088: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3089: i++;
3090: }
3091: }
3092: if ( DP_Print ) { printf("\n"); }
3093: for ( a0 = 0, i = 0; i < n; i++ ) {
3094: NEXTNODE(a0,a);
3095: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3096: else {
3097: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3098: BDY(a) = (pointer)nd_ps[j];
3099: }
3100: }
3101: NEXT(a) = 0;
3102: return a0;
3103: }
3104:
3105: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3106: {
3107: ND_pairs d1,nd,cur,head,prev,remove;
3108:
3109: if ( !g ) return d;
3110: /* for testing */
3111: if ( gensyz && nd_gensyz == 2 ) {
3112: d1 = nd_newpairs(g,t);
3113: if ( !d )
3114: return d1;
3115: else {
3116: nd = d;
3117: while ( NEXT(nd) ) nd = NEXT(nd);
3118: NEXT(nd) = d1;
3119: return d;
3120: }
3121: }
3122: d = crit_B(d,t);
3123: d1 = nd_newpairs(g,t);
3124: d1 = crit_M(d1);
3125: d1 = crit_F(d1);
3126: if ( gensyz || do_weyl )
3127: head = d1;
3128: else {
3129: prev = 0; cur = head = d1;
3130: while ( cur ) {
3131: if ( crit_2( cur->i1,cur->i2 ) ) {
3132: remove = cur;
3133: if ( !prev ) head = cur = NEXT(cur);
3134: else cur = NEXT(prev) = NEXT(cur);
3135: FREENDP(remove);
3136: } else {
3137: prev = cur; cur = NEXT(cur);
3138: }
3139: }
3140: }
3141: if ( !d )
3142: return head;
3143: else {
3144: nd = d;
3145: while ( NEXT(nd) ) nd = NEXT(nd);
3146: NEXT(nd) = head;
3147: return d;
3148: }
3149: }
3150:
1.24 noro 3151: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3152:
1.30 noro 3153: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3154: {
3155: ND_pairs d1;
1.26 noro 3156: struct oEGT eg1,eg2,eg3;
1.24 noro 3157:
1.30 noro 3158: if ( !t ) return d;
1.26 noro 3159: get_eg(&eg1);
1.30 noro 3160: d1 = nd_newpairs_s(t,syz);
1.26 noro 3161: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3162: d = merge_pairs_s(d,d1);
1.26 noro 3163: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3164: return d;
3165: }
1.1 noro 3166:
3167: ND_pairs nd_newpairs( NODE g, int t )
3168: {
3169: NODE h;
3170: UINT *dl;
3171: int ts,s,i,t0,min,max;
3172: ND_pairs r,r0;
3173:
3174: dl = DL(nd_psh[t]);
3175: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3176: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3177: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3178: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3179: continue;
3180: if ( nd_gbblock ) {
3181: t0 = (long)BDY(h);
3182: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3183: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3184: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3185: break;
3186: }
3187: if ( nd_gbblock[i] >= 0 )
3188: continue;
3189: }
3190: NEXTND_pairs(r0,r);
3191: r->i1 = (long)BDY(h);
3192: r->i2 = t;
3193: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3194: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3195: SG(r) = MAX(s,ts) + TD(LCM(r));
3196: /* experimental */
3197: if ( nd_sugarweight )
3198: r->sugar2 = ndl_weight2(r->lcm);
3199: }
3200: if ( r0 ) NEXT(r) = 0;
3201: return r0;
3202: }
3203:
1.24 noro 3204: int comp_sig(SIG s1,SIG s2)
3205: {
3206: #if 0
3207: if ( s1->pos > s2->pos ) return 1;
3208: else if ( s1->pos < s2->pos ) return -1;
3209: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3210: #else
3211: static DL m1,m2;
3212: static int nvar;
3213: int ret;
3214:
1.26 noro 3215: if ( nvar != nd_nvar ) {
3216: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24 noro 3217: }
1.31 noro 3218: #if 0
1.27 noro 3219: _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
3220: _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24 noro 3221: _addtodl(nd_nvar,s1->dl,m1);
3222: _addtodl(nd_nvar,s2->dl,m2);
1.31 noro 3223: #else
3224: _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
3225: _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
3226: #endif
1.24 noro 3227: ret = (*cmpdl)(nd_nvar,m1,m2);
3228: if ( ret != 0 ) return ret;
3229: else if ( s1->pos > s2->pos ) return 1;
3230: else if ( s1->pos < s2->pos ) return -1;
3231: else return 0;
3232: #endif
3233: }
3234:
3235: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3236: {
3237: int ret,s1,s2;
3238: RHist p1,p2;
3239: static int wpd;
3240: static UINT *lcm;
3241:
3242: sp->i1 = i1;
3243: sp->i2 = i2;
3244: p1 = nd_psh[i1];
3245: p2 = nd_psh[i2];
3246: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3247: s1 = SG(p1)-TD(DL(p1));
3248: s2 = SG(p2)-TD(DL(p2));
3249: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3250:
1.26 noro 3251: if ( wpd != nd_wpd ) {
1.24 noro 3252: wpd = nd_wpd;
3253: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3254: }
3255: // DL(sig1) <- sp->lcm
3256: // DL(sig1) -= DL(p1)
3257: // DL(sig1) += DL(p1->sig)
1.27 noro 3258: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3259: _ndltodl(lcm,DL(sig1));
3260: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3261: sig1->pos = p1->sig->pos;
3262:
3263: // DL(sig2) <- sp->lcm
3264: // DL(sig2) -= DL(p2)
3265: // DL(sig2) += DL(p2->sig)
1.27 noro 3266: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3267: _ndltodl(lcm,DL(sig2));
3268: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3269: sig2->pos = p2->sig->pos;
3270:
3271: ret = comp_sig(sig1,sig2);
3272: if ( ret == 0 ) return 0;
3273: else if ( ret > 0 ) sp->sig = sig1;
3274: else sp->sig = sig2;
3275: return 1;
3276: }
3277:
3278: SIG dup_sig(SIG sig)
3279: {
3280: SIG r;
3281:
3282: if ( !sig ) return 0;
3283: else {
3284: NEWSIG(r);
3285: _copydl(nd_nvar,DL(sig),DL(r));
3286: r->pos = sig->pos;
3287: return r;
3288: }
3289: }
3290:
3291: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3292: {
3293: to->i1 = from->i1;
3294: to->i2 = from->i2;
3295: to->sugar = from->sugar;
3296: to->sugar2 = from->sugar2;
3297: ndl_copy(from->lcm,to->lcm);
3298: to->sig = dup_sig(from->sig);
3299: }
3300:
3301: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3302: {
3303: struct oND_pairs root;
3304: ND_pairs q1,q2,r0,r;
3305: int ret;
3306:
3307: r = &root;
3308: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3309: ret = comp_sig(q1->sig,q2->sig);
3310: if ( ret < 0 ) {
3311: r->next = q1; r = q1; q1 = q1->next;
3312: } else if ( ret > 0 ) {
3313: r->next = q2; r = q2; q2 = q2->next;
3314: } else {
3315: ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35 noro 3316: Nsamesig++;
1.24 noro 3317: if ( ret < 0 ) {
3318: r->next = q1; r = q1; q1 = q1->next;
3319: q2 = q2->next;
3320: } else {
3321: r->next = q2; r = q2; q2 = q2->next;
3322: q1 = q1->next;
3323: }
3324: }
3325: }
3326: if ( q1 ) {
3327: r->next = q1;
3328: } else {
3329: r->next = q2;
3330: }
3331: return root.next;
3332: }
3333:
3334: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3335: {
3336: ND_pairs p,prev;
3337: int ret;
3338:
3339: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3340: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3341: break;
3342: }
3343: if ( ret == 0 ) {
3344: ret = DL_COMPARE(s->lcm,p->lcm);
3345: if ( ret < 0 ) {
3346: // replace p with s
3347: s->next = p->next;
3348: if ( prev == 0 ) {
3349: return s;
3350: } else {
3351: prev->next = s;
3352: return l;
3353: }
3354: } else
3355: return l;
3356: } else {
3357: // insert s between prev and p
3358: s->next = p;
3359: if ( prev == 0 ) {
3360: return s;
3361: } else {
3362: prev->next = s;
3363: return l;
3364: }
3365: }
3366: }
3367:
1.29 noro 3368: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3369: {
3370: int i;
3371:
3372: if ( d1->td > d2->td )
3373: return 0;
3374: for ( i = nvar-1; i >= 0; i-- )
3375: if ( d1->d[i] > d2->d[i] )
3376: break;
3377: if ( i >= 0 )
3378: return 0;
3379: else
3380: return 1;
3381: }
3382:
1.30 noro 3383: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3384: {
3385: NODE h,s;
3386: UINT *dl;
1.27 noro 3387: int ts,ret,i;
1.24 noro 3388: ND_pairs r,r0,_sp,sp;
1.29 noro 3389: SIG spsig,tsig;
3390: static int nvar;
3391: static SIG _sig1,_sig2;
1.26 noro 3392: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3393:
3394: NEWND_pairs(_sp);
1.29 noro 3395: if ( !_sig1 || nvar != nd_nvar ) {
3396: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3397: }
1.24 noro 3398: r0 = 0;
1.27 noro 3399: for ( i = 0; i < t; i++ ) {
3400: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3401: if ( ret ) {
3402: spsig = _sp->sig;
1.29 noro 3403: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3404: tsig = (SIG)s->body;
1.29 noro 3405: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3406: break;
3407: }
3408: if ( s == 0 ) {
3409: NEWND_pairs(sp);
3410: dup_ND_pairs(sp,_sp);
3411: r0 = insert_pair_s(r0,sp);
1.26 noro 3412: } else
3413: Nsyz++;
1.24 noro 3414: }
3415: }
3416: return r0;
3417: }
3418:
1.1 noro 3419: /* ipair = [i1,i2],[i1,i2],... */
3420: ND_pairs nd_ipairtospair(NODE ipair)
3421: {
3422: int s1,s2;
3423: NODE tn,t;
3424: ND_pairs r,r0;
3425:
3426: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3427: NEXTND_pairs(r0,r);
3428: tn = BDY((LIST)BDY(t));
1.6 noro 3429: r->i1 = ZTOS((Q)ARG0(tn));
3430: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3431: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3432: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3433: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3434: SG(r) = MAX(s1,s2) + TD(LCM(r));
3435: /* experimental */
3436: if ( nd_sugarweight )
3437: r->sugar2 = ndl_weight2(r->lcm);
3438: }
3439: if ( r0 ) NEXT(r) = 0;
3440: return r0;
3441: }
3442:
3443: /* kokokara */
3444:
3445: ND_pairs crit_B( ND_pairs d, int s )
3446: {
3447: ND_pairs cur,head,prev,remove;
3448: UINT *t,*tl,*lcm;
3449: int td,tdl;
3450:
3451: if ( !d ) return 0;
3452: t = DL(nd_psh[s]);
3453: prev = 0;
3454: head = cur = d;
3455: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3456: while ( cur ) {
3457: tl = cur->lcm;
3458: if ( ndl_reducible(tl,t) ) {
3459: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3460: if ( !ndl_equal(lcm,tl) ) {
3461: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3462: if (!ndl_equal(lcm,tl)) {
3463: remove = cur;
3464: if ( !prev ) {
3465: head = cur = NEXT(cur);
3466: } else {
3467: cur = NEXT(prev) = NEXT(cur);
3468: }
3469: FREENDP(remove);
3470: } else {
3471: prev = cur; cur = NEXT(cur);
3472: }
3473: } else {
3474: prev = cur; cur = NEXT(cur);
3475: }
3476: } else {
3477: prev = cur; cur = NEXT(cur);
3478: }
3479: }
3480: return head;
3481: }
3482:
3483: ND_pairs crit_M( ND_pairs d1 )
3484: {
3485: ND_pairs e,d2,d3,dd,p;
3486: UINT *id,*jd;
3487:
3488: if ( !d1 ) return d1;
3489: for ( dd = 0, e = d1; e; e = d3 ) {
3490: if ( !(d2 = NEXT(e)) ) {
3491: NEXT(e) = dd;
3492: return e;
3493: }
3494: id = LCM(e);
3495: for ( d3 = 0; d2; d2 = p ) {
3496: p = NEXT(d2);
3497: jd = LCM(d2);
3498: if ( ndl_equal(jd,id) )
3499: ;
3500: else if ( TD(jd) > TD(id) )
3501: if ( ndl_reducible(jd,id) ) continue;
3502: else ;
3503: else if ( ndl_reducible(id,jd) ) goto delit;
3504: NEXT(d2) = d3;
3505: d3 = d2;
3506: }
3507: NEXT(e) = dd;
3508: dd = e;
3509: continue;
3510: /**/
3511: delit: NEXT(d2) = d3;
3512: d3 = d2;
3513: for ( ; p; p = d2 ) {
3514: d2 = NEXT(p);
3515: NEXT(p) = d3;
3516: d3 = p;
3517: }
3518: FREENDP(e);
3519: }
3520: return dd;
3521: }
3522:
3523: ND_pairs crit_F( ND_pairs d1 )
3524: {
3525: ND_pairs rest, head,remove;
3526: ND_pairs last, p, r, w;
3527: int s;
3528:
3529: if ( !d1 ) return d1;
3530: for ( head = last = 0, p = d1; NEXT(p); ) {
3531: r = w = equivalent_pairs(p,&rest);
3532: s = SG(r);
3533: w = NEXT(w);
3534: while ( w ) {
3535: if ( crit_2(w->i1,w->i2) ) {
3536: r = w;
3537: w = NEXT(w);
3538: while ( w ) {
3539: remove = w;
3540: w = NEXT(w);
3541: FREENDP(remove);
3542: }
3543: break;
3544: } else if ( SG(w) < s ) {
3545: FREENDP(r);
3546: r = w;
3547: s = SG(r);
3548: w = NEXT(w);
3549: } else {
3550: remove = w;
3551: w = NEXT(w);
3552: FREENDP(remove);
3553: }
3554: }
3555: if ( last ) NEXT(last) = r;
3556: else head = r;
3557: NEXT(last = r) = 0;
3558: p = rest;
3559: if ( !p ) return head;
3560: }
3561: if ( !last ) return p;
3562: NEXT(last) = p;
3563: return head;
3564: }
3565:
3566: int crit_2( int dp1, int dp2 )
3567: {
3568: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3569: }
3570:
3571: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3572: {
3573: ND_pairs w,p,r,s;
3574: UINT *d;
3575:
3576: w = d1;
3577: d = LCM(w);
3578: s = NEXT(w);
3579: NEXT(w) = 0;
3580: for ( r = 0; s; s = p ) {
3581: p = NEXT(s);
3582: if ( ndl_equal(d,LCM(s)) ) {
3583: NEXT(s) = w; w = s;
3584: } else {
3585: NEXT(s) = r; r = s;
3586: }
3587: }
3588: *prest = r;
3589: return w;
3590: }
3591:
3592: NODE update_base(NODE nd,int ndp)
3593: {
3594: UINT *dl, *dln;
3595: NODE last, p, head;
3596:
3597: dl = DL(nd_psh[ndp]);
3598: for ( head = last = 0, p = nd; p; ) {
3599: dln = DL(nd_psh[(long)BDY(p)]);
3600: if ( ndl_reducible( dln, dl ) ) {
3601: p = NEXT(p);
3602: if ( last ) NEXT(last) = p;
3603: } else {
3604: if ( !last ) head = p;
3605: p = NEXT(last = p);
3606: }
3607: }
3608: head = append_one(head,ndp);
3609: return head;
3610: }
3611:
3612: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3613: {
3614: ND_pairs m,ml,p,l;
3615: UINT *lcm;
3616: int s,td,len,tlen,c,c1;
3617:
3618: if ( !(p = NEXT(m = d)) ) {
3619: *prest = p;
3620: NEXT(m) = 0;
3621: return m;
3622: }
3623: if ( !NoSugar ) {
3624: if ( nd_sugarweight ) {
3625: s = m->sugar2;
3626: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3627: if ( (p->sugar2 < s)
3628: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3629: ml = l; m = p; s = m->sugar2;
3630: }
3631: } else {
3632: s = SG(m);
3633: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3634: if ( (SG(p) < s)
3635: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3636: ml = l; m = p; s = SG(m);
3637: }
3638: }
3639: } else {
3640: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3641: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3642: ml = l; m = p; s = SG(m);
3643: }
3644: }
3645: if ( !ml ) *prest = NEXT(m);
3646: else {
3647: NEXT(ml) = NEXT(m);
3648: *prest = d;
3649: }
3650: NEXT(m) = 0;
3651: return m;
3652: }
3653:
3654: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3655: {
3656: int msugar,i;
3657: ND_pairs t,dm0,dm,dr0,dr;
3658:
3659: if ( nd_sugarweight ) {
3660: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3661: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3662: dm0 = 0; dr0 = 0;
3663: for ( i = 0, t = d; t; t = NEXT(t) )
3664: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3665: if ( dm0 ) NEXT(dm) = t;
3666: else dm0 = t;
3667: dm = t;
3668: i++;
3669: } else {
3670: if ( dr0 ) NEXT(dr) = t;
3671: else dr0 = t;
3672: dr = t;
3673: }
3674: } else {
3675: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3676: if ( SG(t) < msugar ) msugar = SG(t);
3677: dm0 = 0; dr0 = 0;
3678: for ( i = 0, t = d; t; t = NEXT(t) )
3679: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3680: if ( dm0 ) NEXT(dm) = t;
3681: else dm0 = t;
3682: dm = t;
3683: i++;
3684: } else {
3685: if ( dr0 ) NEXT(dr) = t;
3686: else dr0 = t;
3687: dr = t;
3688: }
3689: }
3690: NEXT(dm) = 0;
3691: if ( dr0 ) NEXT(dr) = 0;
3692: *prest = dr0;
3693: return dm0;
3694: }
3695:
1.28 noro 3696: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
3697: {
3698: int msugar;
3699: ND_pairs t,last;
3700:
3701: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
3702: if ( SG(t) == msugar ) last = t;
3703: *prest = last->next;
3704: last->next = 0;
3705: return d;
3706: }
3707:
1.1 noro 3708: int nd_tdeg(NDV c)
3709: {
3710: int wmax = 0;
3711: int i,len;
3712: NMV a;
3713:
3714: len = LEN(c);
3715: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3716: wmax = MAX(TD(DL(a)),wmax);
3717: return wmax;
3718: }
3719:
1.24 noro 3720: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3721: {
3722: int len;
3723: RHist r;
3724: NDV b;
3725: NODE tn;
3726: LIST l;
3727: Z iq;
3728:
3729: if ( nd_psn == nd_pslen ) {
3730: nd_pslen *= 2;
3731: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3732: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3733: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3734: nd_bound = (UINT **)
3735: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3736: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3737: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3738: }
3739: NEWRHist(r); nd_psh[nd_psn] = r;
3740: nd_ps[nd_psn] = a;
3741: if ( aq ) {
3742: nd_ps_trace[nd_psn] = aq;
3743: if ( !m ) {
3744: register_hcf(aq);
3745: } else if ( m == -2 ) {
3746: /* do nothing */
3747: } else
3748: error("ndv_newps : invalud modulus");
3749: nd_bound[nd_psn] = ndv_compute_bound(aq);
3750: #if 1
3751: SG(r) = SG(aq);
3752: #else
3753: SG(r) = nd_tdeg(aq);
3754: #endif
3755: ndl_copy(HDL(aq),DL(r));
1.24 noro 3756: r->sig = dup_sig(aq->sig);
1.1 noro 3757: } else {
3758: if ( !m ) register_hcf(a);
3759: nd_bound[nd_psn] = ndv_compute_bound(a);
3760: #if 1
3761: SG(r) = SG(a);
3762: #else
3763: SG(r) = nd_tdeg(a);
3764: #endif
3765: ndl_copy(HDL(a),DL(r));
1.24 noro 3766: r->sig = dup_sig(a->sig);
1.1 noro 3767: }
3768: if ( nd_demand ) {
3769: if ( aq ) {
3770: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3771: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3772: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3773: nd_ps_trace[nd_psn] = 0;
3774: } else {
3775: ndv_save(nd_ps[nd_psn],nd_psn);
3776: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3777: nd_ps[nd_psn] = 0;
3778: }
3779: }
3780: if ( nd_gentrace ) {
3781: /* reverse the tracelist and append it to alltracelist */
3782: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3783: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3784: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3785: }
3786: return nd_psn++;
3787: }
3788:
3789: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3790: /* return 1 if success, 0 if failure (HC(a mod p)) */
3791:
1.24 noro 3792: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3793: {
1.6 noro 3794: int i,j,td,len,max;
3795: NODE s,s0,f0,tn;
3796: UINT *d;
3797: RHist r;
3798: NDVI w;
3799: NDV a,am;
3800: union oNDC hc;
3801: NODE node;
3802: P hcp;
3803: Z iq,jq;
3804: LIST l;
3805:
3806: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3807: /* initialize the tracelist */
3808: nd_tracelist = 0;
3809:
3810: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3811: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3812: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3813: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3814: if ( !dont_sort ) {
3815: /* XXX heuristic */
1.26 noro 3816: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 3817: qsort(w,nd_psn,sizeof(struct oNDVI),
3818: (int (*)(const void *,const void *))ndvi_compare_rev);
3819: else
3820: qsort(w,nd_psn,sizeof(struct oNDVI),
3821: (int (*)(const void *,const void *))ndvi_compare);
3822: }
3823: nd_pslen = 2*nd_psn;
3824: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3825: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3826: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3827: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3828: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3829: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3830: nd_hcf = 0;
1.1 noro 3831:
1.6 noro 3832: if ( trace && nd_vc )
3833: makesubst(nd_vc,&nd_subst);
3834: else
3835: nd_subst = 0;
1.1 noro 3836:
1.6 noro 3837: if ( !nd_red )
3838: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3839: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3840: for ( i = 0; i < nd_psn; i++ ) {
3841: hc = HCU(w[i].p);
3842: if ( trace ) {
3843: if ( mod == -2 ) {
3844: /* over a large finite field */
3845: /* trace = small modulus */
3846: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3847: ndv_mod(-2,a);
3848: if ( !dont_removecont) ndv_removecont(-2,a);
3849: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3850: ndv_mod(trace,am);
3851: if ( DL_COMPARE(HDL(am),HDL(a)) )
3852: return 0;
3853: ndv_removecont(trace,am);
3854: } else {
3855: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3856: if ( !dont_removecont) ndv_removecont(0,a);
3857: register_hcf(a);
3858: am = nd_ps[i] = ndv_dup(mod,a);
3859: ndv_mod(mod,am);
3860: if ( DL_COMPARE(HDL(am),HDL(a)) )
3861: return 0;
3862: ndv_removecont(mod,am);
3863: }
3864: } else {
3865: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3866: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3867: if ( !mod ) register_hcf(a);
1.1 noro 3868: }
1.6 noro 3869: if ( nd_gentrace ) {
3870: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3871: /* exact division */
1.1 noro 3872: if ( !dont_removecont )
1.6 noro 3873: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3874: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3875: }
3876: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3877: nd_bound[i] = ndv_compute_bound(a);
3878: nd_psh[i] = r;
3879: if ( nd_demand ) {
3880: if ( trace ) {
3881: ndv_save(nd_ps_trace[i],i);
3882: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3883: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3884: nd_ps_trace[i] = 0;
3885: } else {
3886: ndv_save(nd_ps[i],i);
3887: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3888: nd_ps[i] = 0;
3889: }
1.1 noro 3890: }
1.6 noro 3891: }
1.24 noro 3892: if ( sba ) {
1.27 noro 3893: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 3894: // setup signatures
1.27 noro 3895: for ( i = 0; i < nd_psn; i++ ) {
3896: SIG sig;
1.24 noro 3897:
1.27 noro 3898: NEWSIG(sig); sig->pos = i;
3899: nd_ps[i]->sig = sig;
3900: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
3901: nd_psh[i]->sig = sig;
3902: if ( trace ) {
3903: nd_ps_trace[i]->sig = sig;
3904: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
3905: }
3906: NEWDL(nd_sba_hm[i],nd_nvar);
3907: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
3908: }
1.29 noro 3909: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
3910: for ( i = 0; i < nd_psn; i++ ) {
3911: j = nd_psh[i]->sig->pos;
3912: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
3913: }
1.24 noro 3914: }
1.6 noro 3915: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3916: return 1;
1.1 noro 3917: }
3918:
3919: struct order_spec *append_block(struct order_spec *spec,
3920: int nv,int nalg,int ord);
3921:
3922: extern VECT current_dl_weight_vector_obj;
3923: static VECT prev_weight_vector_obj;
3924:
3925: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3926: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3927: {
3928: NODE alist,t,s,r0,r,arg;
3929: VL tv;
3930: P poly;
3931: DP d;
3932: Alg alpha,dp;
3933: DAlg inv,da,hc;
3934: MP m;
3935: int i,nvar,nalg,n;
3936: NumberField nf;
3937: LIST f1,f2;
3938: struct order_spec *current_spec;
3939: VECT obj,obj0;
3940: VECT tmp;
3941:
3942: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3943: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3944:
3945: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3946: NEXTNODE(alist,t); MKV(tv->v,poly);
3947: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3948: tv->v = tv->v->priv;
3949: }
3950: NEXT(t) = 0;
3951:
3952: /* simplification, making polynomials monic */
3953: setfield_dalg(alist);
3954: obj_algtodalg((Obj)f,(Obj *)&f1);
3955: for ( t = BDY(f); t; t = NEXT(t) ) {
3956: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3957: hc = (DAlg)BDY(d)->c;
3958: if ( NID(hc) == N_DA ) {
3959: invdalg(hc,&inv);
3960: for ( m = BDY(d); m; m = NEXT(m) ) {
3961: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3962: }
3963: }
3964: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3965: }
3966: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3967:
3968: /* append alg vars to the var list */
3969: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3970: NEXT(tv) = av;
3971:
3972: /* append a block to ord */
3973: *ord1p = append_block(ord,nvar,nalg,2);
3974:
3975: /* create generator list */
3976: nf = get_numberfield();
3977: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3978: MKAlg(nf->defpoly[i],dp);
3979: MKNODE(s,dp,t); t = s;
3980: }
3981: MKLIST(f1,t);
3982: *alistp = alist;
3983: algobjtorat((Obj)f1,(Obj *)f1p);
3984:
3985: /* creating a new weight vector */
3986: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3987: n = nvar+nalg+1;
3988: MKVECT(obj,n);
3989: if ( obj0 && obj0->len == nvar )
3990: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3991: else
3992: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3993: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3994: BDY(obj)[n-1] = (pointer)ONE;
3995: arg = mknode(1,obj);
3996: Pdp_set_weight(arg,&tmp);
3997: }
3998:
3999: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
4000: {
4001: NODE s,t,u0,u;
4002: P p;
4003: VL tv;
4004: Obj obj;
4005: VECT tmp;
4006: NODE arg;
4007:
4008: u0 = 0;
4009: for ( t = r; t; t = NEXT(t) ) {
4010: p = (P)BDY(t);
4011: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
4012: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4013: }
4014: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4015: NEXTNODE(u0,u);
4016: BDY(u) = (pointer)p;
4017: }
4018: }
4019: arg = mknode(1,prev_weight_vector_obj);
4020: Pdp_set_weight(arg,&tmp);
4021:
4022: return u0;
4023: }
4024:
4025: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4026: {
4027: VL tv,fv,vv,vc,av;
4028: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4029: int e,max,nvar,i;
4030: NDV b;
4031: int ishomo,nalg,mrank,trank,wmax,len;
4032: NMV a;
4033: Alg alpha,dp;
4034: P p,zp;
4035: Q dmy;
4036: LIST f1,f2,zpl;
4037: Obj obj;
4038: NumberField nf;
4039: struct order_spec *ord1;
4040: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4041: LIST l1,l2,l3,l4,l5;
4042: int j;
4043: Z jq,bpe,last_nonzero;
4044: int *perm;
4045: EPOS oepos;
4046: int obpe,oadv,ompos,cbpe;
1.15 noro 4047: VECT hvect;
1.1 noro 4048:
4049: nd_module = 0;
4050: if ( !m && Demand ) nd_demand = 1;
4051: else nd_demand = 0;
4052: parse_nd_option(current_option);
4053:
4054: if ( DP_Multiple )
4055: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4056: #if 0
4057: ndv_alloc = 0;
4058: #endif
4059: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4060: if ( m && nd_vc )
4061: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4062: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4063: switch ( ord->id ) {
4064: case 1:
4065: if ( ord->nv != nvar )
4066: error("nd_{gr,f4} : invalid order specification");
4067: break;
4068: default:
4069: break;
4070: }
4071: nd_nalg = 0;
4072: av = 0;
4073: if ( !m ) {
4074: get_algtree((Obj)f,&av);
4075: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4076: nd_ntrans = nvar;
4077: nd_nalg = nalg;
4078: /* #i -> t#i */
4079: if ( nalg ) {
4080: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4081: ord = ord1;
4082: f = f1;
4083: }
4084: nvar += nalg;
4085: }
4086: nd_init_ord(ord);
4087: mrank = 0;
4088: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4089: for ( tv = vv; tv; tv = NEXT(tv) ) {
4090: if ( nd_module ) {
1.16 noro 4091: if ( OID(BDY(t)) == O_DPM ) {
4092: e = dpm_getdeg((DPM)BDY(t),&trank);
4093: max = MAX(e,max);
4094: mrank = MAX(mrank,trank);
4095: } else {
4096: s = BDY((LIST)BDY(t));
4097: trank = length(s);
4098: mrank = MAX(mrank,trank);
4099: for ( ; s; s = NEXT(s) ) {
4100: e = getdeg(tv->v,(P)BDY(s));
4101: max = MAX(e,max);
4102: }
1.1 noro 4103: }
4104: } else {
4105: e = getdeg(tv->v,(P)BDY(t));
4106: max = MAX(e,max);
4107: }
4108: }
4109: nd_setup_parameters(nvar,nd_nzlist?0:max);
4110: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4111: ishomo = 1;
4112: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4113: if ( nd_module ) {
1.16 noro 4114: if ( OID(BDY(t)) == O_DPM ) {
4115: Z cont;
4116: DPM zdpm;
4117:
4118: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4119: else zdpm = (DPM)BDY(t);
4120: b = (pointer)dpmtondv(m,zdpm);
4121: } else {
4122: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4123: else zpl = (LIST)BDY(t);
1.1 noro 4124: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4125: }
1.1 noro 4126: } else {
4127: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4128: else zp = (P)BDY(t);
4129: b = (pointer)ptondv(CO,vv,zp);
4130: }
4131: if ( ishomo )
4132: ishomo = ishomo && ndv_ishomo(b);
4133: if ( m ) ndv_mod(m,b);
4134: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4135: }
4136: if ( fd0 ) NEXT(fd) = 0;
4137:
4138: if ( !ishomo && homo ) {
4139: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4140: b = (NDV)BDY(t); len = LEN(b);
4141: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4142: wmax = MAX(TD(DL(a)),wmax);
4143: }
4144: homogenize_order(ord,nvar,&ord1);
4145: nd_init_ord(ord1);
4146: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4147: for ( t = fd0; t; t = NEXT(t) )
4148: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4149: }
4150:
1.24 noro 4151: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4152: if ( nd_gentrace ) {
4153: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4154: }
4155: if ( nd_splist ) {
4156: *rp = compute_splist();
4157: return;
4158: }
4159: if ( nd_check_splist ) {
4160: if ( f4 ) {
4161: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4162: else *rp = 0;
4163: } else {
4164: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4165: else *rp = 0;
4166: }
4167: return;
4168: }
4169: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4170: if ( !x ) {
4171: *rp = 0; return;
4172: }
1.15 noro 4173: if ( nd_gentrace ) {
4174: MKVECT(hvect,nd_psn);
4175: for ( i = 0; i < nd_psn; i++ )
4176: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4177: }
1.1 noro 4178: if ( !ishomo && homo ) {
4179: /* dehomogenization */
4180: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4181: nd_init_ord(ord);
4182: nd_setup_parameters(nvar,0);
4183: }
4184: nd_demand = 0;
4185: if ( nd_module && nd_intersect ) {
4186: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4187: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4188: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4189: }
4190: conv_ilist(nd_demand,0,x,0);
4191: goto FINAL;
4192: }
4193: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4194: x = ndv_reducebase(x,perm);
4195: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4196: x = ndv_reduceall(m,x);
4197: cbpe = nd_bpe;
4198: if ( nd_gentrace && !f4 ) {
4199: tl2 = nd_alltracelist; nd_alltracelist = 0;
4200: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4201: tl3 = nd_alltracelist; nd_alltracelist = 0;
4202: if ( nd_gensyz ) {
4203: nd_gb(m,0,1,1,0);
4204: tl4 = nd_alltracelist; nd_alltracelist = 0;
4205: } else tl4 = 0;
4206: }
4207: nd_bpe = cbpe;
4208: nd_setup_parameters(nd_nvar,0);
4209: FINAL:
4210: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4211: NEXTNODE(r0,r);
4212: if ( nd_module ) {
4213: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4214: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4215: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4216: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4217: }
4218: if ( r0 ) NEXT(r) = 0;
4219: if ( !m && nd_nalg )
4220: r0 = postprocess_algcoef(av,alist,r0);
4221: MKLIST(*rp,r0);
4222: if ( nd_gentrace ) {
4223: if ( f4 ) {
1.6 noro 4224: STOZ(16,bpe);
4225: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4226: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4227: } else {
4228: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4229: tl3 = reverse_node(tl3);
4230: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4231: for ( t = tl2; t; t = NEXT(t) ) {
4232: /* s = [i,[*,j,*,*],...] */
4233: s = BDY((LIST)BDY(t));
1.6 noro 4234: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4235: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4236: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4237: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4238: }
4239: }
4240: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4241: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4242: }
4243: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4244: MKLIST(l5,tl4);
1.6 noro 4245: STOZ(nd_bpe,bpe);
1.15 noro 4246: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4247: }
4248: }
4249: #if 0
4250: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4251: #endif
4252: }
4253:
1.28 noro 4254: NODE nd_sba_f4(int m,int **indp);
4255:
4256: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4257: {
4258: VL tv,fv,vv,vc,av;
4259: NODE fd,fd0,r,r0,t,x,s,xx;
4260: int e,max,nvar,i;
4261: NDV b;
4262: int ishomo,nalg,wmax,len;
4263: NMV a;
4264: P p,zp;
4265: Q dmy;
4266: struct order_spec *ord1;
4267: int j;
4268: int *perm;
4269: EPOS oepos;
4270: int obpe,oadv,ompos,cbpe;
1.29 noro 4271: struct oEGT eg0,eg1,egconv;
1.24 noro 4272:
4273: nd_module = 0;
4274: nd_demand = 0;
4275: parse_nd_option(current_option);
1.35 noro 4276: Nsamesig = 0;
1.24 noro 4277: if ( DP_Multiple )
4278: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4279: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4280: if ( m && nd_vc )
4281: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4282: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4283: switch ( ord->id ) {
4284: case 1:
4285: if ( ord->nv != nvar )
4286: error("nd_sba : invalid order specification");
4287: break;
4288: default:
4289: break;
4290: }
4291: nd_nalg = 0;
4292: nd_init_ord(ord);
4293: // for SIG comparison
4294: initd(ord);
4295: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4296: for ( tv = vv; tv; tv = NEXT(tv) ) {
4297: e = getdeg(tv->v,(P)BDY(t));
4298: max = MAX(e,max);
4299: }
4300: }
1.25 noro 4301: nd_setup_parameters(nvar,max);
1.24 noro 4302: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4303: ishomo = 1;
4304: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4305: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4306: else zp = (P)BDY(t);
4307: b = (pointer)ptondv(CO,vv,zp);
4308: if ( ishomo )
4309: ishomo = ishomo && ndv_ishomo(b);
4310: if ( m ) ndv_mod(m,b);
4311: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4312: }
4313: if ( fd0 ) NEXT(fd) = 0;
4314:
4315: if ( !ishomo && homo ) {
4316: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4317: b = (NDV)BDY(t); len = LEN(b);
4318: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4319: wmax = MAX(TD(DL(a)),wmax);
4320: }
4321: homogenize_order(ord,nvar,&ord1);
4322: nd_init_ord(ord1);
4323: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4324: for ( t = fd0; t; t = NEXT(t) )
4325: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4326: }
4327:
4328: ndv_setup(m,0,fd0,0,0,1);
1.28 noro 4329: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24 noro 4330: if ( !x ) {
4331: *rp = 0; return;
4332: }
4333: if ( !ishomo && homo ) {
4334: /* dehomogenization */
4335: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4336: nd_init_ord(ord);
4337: nd_setup_parameters(nvar,0);
4338: }
4339: nd_demand = 0;
4340: x = ndv_reducebase(x,perm);
4341: x = ndv_reduceall(m,x);
4342: nd_setup_parameters(nd_nvar,0);
1.29 noro 4343: get_eg(&eg0);
1.24 noro 4344: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4345: NEXTNODE(r0,r);
4346: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34 noro 4347: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24 noro 4348: }
4349: if ( r0 ) NEXT(r) = 0;
4350: MKLIST(*rp,r0);
1.29 noro 4351: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4352: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4353: }
4354:
1.1 noro 4355: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4356: {
4357: VL tv,fv,vv,vc,av;
4358: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4359: int e,max,nvar,i;
4360: NDV b;
4361: int ishomo,nalg;
4362: Alg alpha,dp;
4363: P p,zp;
4364: Q dmy;
4365: LIST f1,f2;
4366: Obj obj;
4367: NumberField nf;
4368: struct order_spec *ord1;
4369: int *perm;
4370:
4371: parse_nd_option(current_option);
4372: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4373: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4374: switch ( ord->id ) {
4375: case 1:
4376: if ( ord->nv != nvar )
4377: error("nd_check : invalid order specification");
4378: break;
4379: default:
4380: break;
4381: }
4382: nd_nalg = 0;
4383: av = 0;
4384: if ( !m ) {
4385: get_algtree((Obj)f,&av);
4386: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4387: nd_ntrans = nvar;
4388: nd_nalg = nalg;
4389: /* #i -> t#i */
4390: if ( nalg ) {
4391: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4392: ord = ord1;
4393: f = f1;
4394: }
4395: nvar += nalg;
4396: }
4397: nd_init_ord(ord);
4398: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4399: for ( tv = vv; tv; tv = NEXT(tv) ) {
4400: e = getdeg(tv->v,(P)BDY(t));
4401: max = MAX(e,max);
4402: }
4403: nd_setup_parameters(nvar,max);
4404: ishomo = 1;
4405: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4406: ptozp((P)BDY(t),1,&dmy,&zp);
4407: b = (pointer)ptondv(CO,vv,zp);
4408: if ( ishomo )
4409: ishomo = ishomo && ndv_ishomo(b);
4410: if ( m ) ndv_mod(m,b);
4411: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4412: }
4413: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4414: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4415: for ( x = 0, i = 0; i < nd_psn; i++ )
4416: x = update_base(x,i);
4417: if ( do_check ) {
4418: x = nd_gb(m,ishomo,1,0,&perm);
4419: if ( !x ) {
4420: *rp = 0;
4421: return;
4422: }
4423: } else {
4424: #if 0
4425: /* bug ? */
4426: for ( t = x; t; t = NEXT(t) )
4427: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4428: #else
4429: conv_ilist(0,0,x,&perm);
4430: #endif
4431: }
4432: x = ndv_reducebase(x,perm);
4433: x = ndv_reduceall(m,x);
4434: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4435: NEXTNODE(r0,r);
4436: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4437: }
4438: if ( r0 ) NEXT(r) = 0;
4439: if ( !m && nd_nalg )
4440: r0 = postprocess_algcoef(av,alist,r0);
4441: MKLIST(*rp,r0);
4442: }
4443:
4444: NDV recompute_trace(NODE trace,NDV *p,int m);
4445: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4446:
4447: NDV recompute_trace(NODE ti,NDV *p,int mod)
4448: {
4449: int c,c1,c2,i;
4450: NM mul,m,tail;
4451: ND d,r,rm;
4452: NODE sj;
4453: NDV red;
4454: Obj mj;
4455:
4456: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4457: CM(mul) = 1;
4458: tail = 0;
4459: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4460: sj = BDY((LIST)BDY(ti));
4461: if ( ARG0(sj) ) {
1.6 noro 4462: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4463: mj = (Obj)ARG2(sj);
4464: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4465: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4466: rm = ndv_mul_nm(mod,mul,red);
4467: if ( !r ) r = rm;
4468: else {
4469: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4470: if ( d ) {
4471: NEXT(tail) = m; tail = m; LEN(d)++;
4472: } else {
4473: MKND(nd_nvar,m,1,d); tail = BDY(d);
4474: }
4475: }
4476: if ( !m ) return 0; /* failure */
4477: else {
4478: BDY(r) = m;
4479: if ( mod > 0 || mod == -1 ) {
4480: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4481: DMAR(c1,c2,0,mod,c);
4482: nd_mul_c(mod,rm,c);
4483: } else {
4484: Z t,u;
4485:
4486: chsgnlf(HCZ(r),&t);
4487: divlf(t,HCZ(rm),&u);
4488: nd_mul_c_lf(rm,u);
4489: }
4490: r = nd_add(mod,r,rm);
4491: }
4492: }
4493: }
4494: }
4495: if ( tail ) NEXT(tail) = 0;
4496: d = nd_add(mod,d,r);
4497: nd_mul_c(mod,d,invm(HCM(d),mod));
4498: return ndtondv(mod,d);
4499: }
4500:
4501: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4502: {
4503: VL tv,fv,vv,vc,av;
4504: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4505: int e,max,nvar,i;
4506: NDV b;
4507: int ishomo,nalg;
4508: Alg alpha,dp;
4509: P p,zp;
4510: Q dmy;
4511: LIST f1,f2;
4512: Obj obj;
4513: NumberField nf;
4514: struct order_spec *ord1;
4515: NODE permtrace,intred,ind,perm,trace,ti;
4516: int len,n,j;
4517: NDV *db,*pb;
4518:
4519: parse_nd_option(current_option);
4520: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4521: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4522: switch ( ord->id ) {
4523: case 1:
4524: if ( ord->nv != nvar )
4525: error("nd_check : invalid order specification");
4526: break;
4527: default:
4528: break;
4529: }
4530: nd_init_ord(ord);
1.6 noro 4531: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4532: nd_setup_parameters(nvar,0);
4533:
4534: len = length(BDY(f));
4535: db = (NDV *)MALLOC(len*sizeof(NDV *));
4536: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4537: ptozp((P)BDY(t),1,&dmy,&zp);
4538: b = ptondv(CO,vv,zp);
4539: ndv_mod(m,b);
4540: ndv_mul_c(m,b,invm(HCM(b),m));
4541: db[i] = b;
4542: }
4543:
4544: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4545: intred = BDY((LIST)ARG3(BDY(tlist)));
4546: ind = BDY((LIST)ARG4(BDY(tlist)));
4547: perm = BDY((LIST)ARG0(permtrace));
4548: trace = NEXT(permtrace);
4549:
4550: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4551: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4552: if ( j > i ) i = j;
4553: }
4554: n = i+1;
4555: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4556: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4557: ti = BDY((LIST)BDY(t));
1.6 noro 4558: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4559: }
4560: for ( t = trace; t; t = NEXT(t) ) {
4561: ti = BDY((LIST)BDY(t));
1.6 noro 4562: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4563: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4564: if ( DP_Print ) {
4565: fprintf(asir_out,"."); fflush(asir_out);
4566: }
4567: }
4568: for ( t = intred; t; t = NEXT(t) ) {
4569: ti = BDY((LIST)BDY(t));
1.6 noro 4570: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4571: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4572: if ( DP_Print ) {
4573: fprintf(asir_out,"*"); fflush(asir_out);
4574: }
4575: }
4576: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4577: NEXTNODE(r0,r);
1.6 noro 4578: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4579: ndv_mul_c(m,b,invm(HCM(b),m));
4580: #if 0
1.6 noro 4581: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4582: #else
1.6 noro 4583: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4584: #endif
4585: }
4586: if ( r0 ) NEXT(r) = 0;
4587: MKLIST(*rp,r0);
4588: if ( DP_Print ) fprintf(asir_out,"\n");
4589: }
4590:
1.16 noro 4591: 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 4592: {
4593: VL tv,fv,vv,vc,av;
4594: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4595: int m,nocheck,nvar,mindex,e,max;
4596: NDV c;
4597: NMV a;
4598: P p,zp;
4599: Q dmy;
4600: EPOS oepos;
4601: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4602: Alg alpha,dp;
4603: P poly;
4604: LIST f1,f2,zpl;
4605: Obj obj;
4606: NumberField nf;
4607: struct order_spec *ord1;
4608: struct oEGT eg_check,eg0,eg1;
4609: NODE tr,tl1,tl2,tl3,tl4;
4610: LIST l1,l2,l3,l4,l5;
4611: int *perm;
4612: int j,ret;
4613: Z jq,bpe;
1.15 noro 4614: VECT hvect;
1.1 noro 4615:
4616: nd_module = 0;
4617: nd_lf = 0;
4618: parse_nd_option(current_option);
4619: if ( nd_lf ) {
4620: if ( f4 )
4621: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4622: else
4623: error("nd_gr_trace is not implemented yet over a large finite field");
4624: return;
4625: }
4626: if ( DP_Multiple )
4627: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4628:
4629: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4630: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4631: switch ( ord->id ) {
4632: case 1:
4633: if ( ord->nv != nvar )
4634: error("nd_gr_trace : invalid order specification");
4635: break;
4636: default:
4637: break;
4638: }
4639:
4640: get_algtree((Obj)f,&av);
4641: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4642: nd_ntrans = nvar;
4643: nd_nalg = nalg;
4644: /* #i -> t#i */
4645: if ( nalg ) {
4646: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4647: ord = ord1;
4648: f = f1;
4649: }
4650: nvar += nalg;
4651:
4652: nocheck = 0;
4653: mindex = 0;
4654:
4655: if ( Demand ) nd_demand = 1;
4656: else nd_demand = 0;
4657:
4658: /* setup modulus */
4659: if ( trace < 0 ) {
4660: trace = -trace;
4661: nocheck = 1;
4662: }
4663: m = trace > 1 ? trace : get_lprime(mindex);
4664: nd_init_ord(ord);
4665: mrank = 0;
4666: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4667: for ( tv = vv; tv; tv = NEXT(tv) ) {
4668: if ( nd_module ) {
1.16 noro 4669: if ( OID(BDY(t)) == O_DPM ) {
4670: e = dpm_getdeg((DPM)BDY(t),&trank);
4671: max = MAX(e,max);
4672: mrank = MAX(mrank,trank);
4673: } else {
1.1 noro 4674: s = BDY((LIST)BDY(t));
4675: trank = length(s);
4676: mrank = MAX(mrank,trank);
4677: for ( ; s; s = NEXT(s) ) {
4678: e = getdeg(tv->v,(P)BDY(s));
4679: max = MAX(e,max);
4680: }
1.16 noro 4681: }
1.1 noro 4682: } else {
4683: e = getdeg(tv->v,(P)BDY(t));
4684: max = MAX(e,max);
4685: }
4686: }
4687: nd_setup_parameters(nvar,max);
4688: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4689: ishomo = 1;
4690: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4691: if ( nd_module ) {
1.16 noro 4692: if ( OID(BDY(t)) == O_DPM ) {
4693: Z cont;
4694: DPM zdpm;
4695:
1.17 noro 4696: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4697: else zdpm = (DPM)BDY(t);
4698: c = (pointer)dpmtondv(m,zdpm);
4699: } else {
4700: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4701: else zpl = (LIST)BDY(t);
1.1 noro 4702: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4703: }
1.1 noro 4704: } else {
1.16 noro 4705: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4706: else zp = (P)BDY(t);
4707: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4708: }
4709: if ( ishomo )
4710: ishomo = ishomo && ndv_ishomo(c);
4711: if ( c ) {
4712: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4713: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4714: }
4715: }
4716: if ( in0 ) NEXT(in) = 0;
4717: if ( fd0 ) NEXT(fd) = 0;
4718: if ( !ishomo && homo ) {
4719: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4720: c = (NDV)BDY(t); len = LEN(c);
4721: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4722: wmax = MAX(TD(DL(a)),wmax);
4723: }
4724: homogenize_order(ord,nvar,&ord1);
4725: nd_init_ord(ord1);
4726: nd_setup_parameters(nvar+1,wmax);
4727: for ( t = fd0; t; t = NEXT(t) )
4728: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4729: }
4730: if ( MaxDeg > 0 ) nocheck = 1;
4731: while ( 1 ) {
4732: tl1 = tl2 = tl3 = tl4 = 0;
4733: if ( Demand )
4734: nd_demand = 1;
1.24 noro 4735: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4736: if ( nd_gentrace ) {
4737: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4738: }
4739: if ( ret )
4740: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4741: if ( !ret || !cand ) {
4742: /* failure */
4743: if ( trace > 1 ) { *rp = 0; return; }
4744: else m = get_lprime(++mindex);
4745: continue;
4746: }
1.15 noro 4747: if ( nd_gentrace ) {
4748: MKVECT(hvect,nd_psn);
4749: for ( i = 0; i < nd_psn; i++ )
4750: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4751: }
1.1 noro 4752: if ( !ishomo && homo ) {
4753: /* dehomogenization */
4754: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4755: nd_init_ord(ord);
4756: nd_setup_parameters(nvar,0);
4757: }
4758: nd_demand = 0;
4759: cand = ndv_reducebase(cand,perm);
4760: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4761: cand = ndv_reduceall(0,cand);
4762: cbpe = nd_bpe;
4763: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4764: get_eg(&eg0);
4765: if ( nocheck )
4766: break;
4767: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4768: if ( nd_gentrace ) {
4769: tl3 = nd_alltracelist; nd_alltracelist = 0;
4770: } else tl3 = 0;
4771: /* gbcheck : cand is a GB of Id(cand) ? */
4772: if ( nd_vc || nd_gentrace || nd_gensyz )
4773: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4774: else
4775: ret = nd_f4(0,1,0)!=0;
4776: if ( nd_gentrace && nd_gensyz ) {
4777: tl4 = nd_alltracelist; nd_alltracelist = 0;
4778: } else tl4 = 0;
4779: }
4780: if ( ret ) break;
4781: else if ( trace > 1 ) {
4782: /* failure */
4783: *rp = 0; return;
4784: } else {
4785: /* try the next modulus */
4786: m = get_lprime(++mindex);
4787: /* reset the parameters */
4788: if ( !ishomo && homo ) {
4789: nd_init_ord(ord1);
4790: nd_setup_parameters(nvar+1,wmax);
4791: } else {
4792: nd_init_ord(ord);
4793: nd_setup_parameters(nvar,max);
4794: }
4795: }
4796: }
4797: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4798: if ( DP_Print )
1.6 noro 4799: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4800: /* dp->p */
4801: nd_bpe = cbpe;
4802: nd_setup_parameters(nd_nvar,0);
4803: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4804: if ( nd_module ) {
1.17 noro 4805: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4806: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4807: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4808: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4809: }
4810: if ( nd_nalg )
4811: cand = postprocess_algcoef(av,alist,cand);
4812: MKLIST(*rp,cand);
4813: if ( nd_gentrace ) {
4814: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4815: tl3 = reverse_node(tl3);
4816: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4817: for ( t = tl2; t; t = NEXT(t) ) {
4818: /* s = [i,[*,j,*,*],...] */
4819: s = BDY((LIST)BDY(t));
1.6 noro 4820: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4821: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4822: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4823: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4824: }
4825: }
4826: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4827: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4828: }
4829: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4830: MKLIST(l5,tl4);
1.6 noro 4831: STOZ(nd_bpe,bpe);
1.15 noro 4832: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4833: }
4834: }
4835:
4836: /* XXX : module element is not considered */
4837:
4838: void dltondl(int n,DL dl,UINT *r)
4839: {
4840: UINT *d;
4841: int i,j,l,s,ord_l;
4842: struct order_pair *op;
4843:
4844: d = (unsigned int *)dl->d;
4845: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
4846: if ( nd_blockmask ) {
4847: l = nd_blockmask->n;
4848: op = nd_blockmask->order_pair;
4849: for ( j = 0, s = 0; j < l; j++ ) {
4850: ord_l = op[j].length;
4851: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
4852: }
4853: TD(r) = ndl_weight(r);
4854: ndl_weight_mask(r);
4855: } else {
4856: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
4857: TD(r) = ndl_weight(r);
4858: }
4859: }
4860:
4861: DL ndltodl(int n,UINT *ndl)
4862: {
4863: DL dl;
4864: int *d;
4865: int i,j,l,s,ord_l;
4866: struct order_pair *op;
4867:
1.33 noro 4868: NEWDL_NOINIT(dl,n);
1.1 noro 4869: dl->td = TD(ndl);
4870: d = dl->d;
4871: if ( nd_blockmask ) {
4872: l = nd_blockmask->n;
4873: op = nd_blockmask->order_pair;
4874: for ( j = 0, s = 0; j < l; j++ ) {
4875: ord_l = op[j].length;
4876: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4877: }
4878: } else {
4879: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4880: }
4881: return dl;
4882: }
4883:
1.24 noro 4884: void _ndltodl(UINT *ndl,DL dl)
4885: {
4886: int *d;
4887: int i,j,l,s,ord_l,n;
4888: struct order_pair *op;
4889:
4890: n = nd_nvar;
4891: dl->td = TD(ndl);
4892: d = dl->d;
4893: if ( nd_blockmask ) {
4894: l = nd_blockmask->n;
4895: op = nd_blockmask->order_pair;
4896: for ( j = 0, s = 0; j < l; j++ ) {
4897: ord_l = op[j].length;
4898: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4899: }
4900: } else {
4901: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4902: }
4903: }
4904:
1.1 noro 4905: void nmtodp(int mod,NM m,DP *r)
4906: {
4907: DP dp;
4908: MP mr;
4909:
4910: NEWMP(mr);
4911: mr->dl = ndltodl(nd_nvar,DL(m));
4912: mr->c = (Obj)ndctop(mod,m->c);
4913: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4914: *r = dp;
4915: }
4916:
1.15 noro 4917: void ndltodp(UINT *d,DP *r)
4918: {
4919: DP dp;
4920: MP mr;
4921:
4922: NEWMP(mr);
4923: mr->dl = ndltodl(nd_nvar,d);
4924: mr->c = (Obj)ONE;
4925: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4926: *r = dp;
4927: }
4928:
1.1 noro 4929: void ndl_print(UINT *dl)
4930: {
4931: int n;
4932: int i,j,l,ord_l,s,s0;
4933: struct order_pair *op;
4934:
4935: n = nd_nvar;
4936: printf("<<");
4937: if ( nd_blockmask ) {
4938: l = nd_blockmask->n;
4939: op = nd_blockmask->order_pair;
4940: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4941: ord_l = op[j].length;
4942: for ( i = 0; i < ord_l; i++, s++ )
4943: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4944: }
4945: } else {
4946: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4947: }
4948: printf(">>");
4949: if ( nd_module && MPOS(dl) )
4950: printf("*e%d",MPOS(dl));
4951: }
4952:
4953: void nd_print(ND p)
4954: {
4955: NM m;
4956:
4957: if ( !p )
4958: printf("0\n");
4959: else {
4960: for ( m = BDY(p); m; m = NEXT(m) ) {
4961: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 4962: else printf("+%ld*",CM(m));
1.1 noro 4963: ndl_print(DL(m));
4964: }
4965: printf("\n");
4966: }
4967: }
4968:
4969: void nd_print_q(ND p)
4970: {
4971: NM m;
4972:
4973: if ( !p )
4974: printf("0\n");
4975: else {
4976: for ( m = BDY(p); m; m = NEXT(m) ) {
4977: printf("+");
1.6 noro 4978: printexpr(CO,(Obj)CZ(m));
1.1 noro 4979: printf("*");
4980: ndl_print(DL(m));
4981: }
4982: printf("\n");
4983: }
4984: }
4985:
4986: void ndp_print(ND_pairs d)
4987: {
4988: ND_pairs t;
4989:
4990: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4991: printf("\n");
4992: }
4993:
4994: void nd_removecont(int mod,ND p)
4995: {
4996: int i,n;
4997: Z *w;
4998: NM m;
4999: struct oVECT v;
5000:
5001: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
5002: else if ( mod == -2 ) {
5003: Z inv;
5004: divlf(ONE,HCZ(p),&inv);
5005: nd_mul_c_lf(p,inv);
5006: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
5007: else {
5008: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
5009: w = (Z *)MALLOC(n*sizeof(Q));
5010: v.len = n;
5011: v.body = (pointer *)w;
1.6 noro 5012: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5013: removecont_array((P *)w,n,1);
1.6 noro 5014: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5015: }
5016: }
5017:
5018: void nd_removecont2(ND p1,ND p2)
5019: {
5020: int i,n1,n2,n;
5021: Z *w;
5022: NM m;
5023: struct oVECT v;
5024:
5025: n1 = nd_length(p1);
5026: n2 = nd_length(p2);
5027: n = n1+n2;
5028: w = (Z *)MALLOC(n*sizeof(Q));
5029: v.len = n;
5030: v.body = (pointer *)w;
5031: i = 0;
5032: if ( p1 )
1.6 noro 5033: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5034: if ( p2 )
1.6 noro 5035: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5036: removecont_array((P *)w,n,1);
5037: i = 0;
5038: if ( p1 )
1.6 noro 5039: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5040: if ( p2 )
1.6 noro 5041: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5042: }
5043:
5044: void ndv_removecont(int mod,NDV p)
5045: {
5046: int i,len,all_p;
5047: Z *c;
5048: P *w;
5049: Z dvr,t;
5050: P g,cont,tp;
5051: NMV m;
5052:
5053: if ( mod == -1 )
5054: ndv_mul_c(mod,p,_invsf(HCM(p)));
5055: else if ( mod == -2 ) {
5056: Z inv;
5057: divlf(ONE,HCZ(p),&inv);
5058: ndv_mul_c_lf(p,inv);
5059: } else if ( mod )
5060: ndv_mul_c(mod,p,invm(HCM(p),mod));
5061: else {
5062: len = p->len;
5063: w = (P *)MALLOC(len*sizeof(P));
5064: c = (Z *)MALLOC(len*sizeof(Q));
5065: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5066: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5067: all_p = all_p && !NUM(w[i]);
5068: }
5069: if ( all_p ) {
5070: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5071: mulp(nd_vc,(P)dvr,g,&cont);
5072: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5073: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5074: }
5075: } else {
5076: sortbynm((Q *)c,len);
5077: qltozl((Q *)c,len,&dvr);
5078: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5079: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5080: }
5081: }
5082: }
5083: }
5084:
5085: /* koko */
5086:
5087: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5088: {
5089: int len,i,max;
5090: NMV m,mr0,mr,t;
5091:
5092: len = p->len;
1.14 noro 5093: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5094: max = MAX(max,TD(DL(m)));
5095: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5096: m = (NMV)((char *)mr0+(len-1)*oadv);
5097: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5098: t = (NMV)MALLOC(nmv_adv);
5099: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5100: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5101: CZ(mr) = CZ(m);
1.1 noro 5102: ndl_copy(DL(t),DL(mr));
5103: }
5104: NV(p)++;
5105: BDY(p) = mr0;
5106: }
5107:
5108: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5109: {
5110: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5111: int pos;
5112: Q *w;
5113: Q dvr,t;
5114: NMV m,r;
5115:
5116: len = p->len;
5117: newnvar = nd_nvar-1;
5118: newexporigin = nd_get_exporigin(ord);
5119: if ( nd_module ) newmpos = newexporigin-1;
5120: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5121: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5122: ndl_dehomogenize(DL(m));
5123: if ( newwpd != nd_wpd ) {
5124: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5125: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5126: CZ(r) = CZ(m);
1.1 noro 5127: if ( nd_module ) pos = MPOS(DL(m));
5128: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5129: adj = nd_exporigin-newexporigin;
5130: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5131: if ( nd_module ) {
5132: DL(r)[newmpos] = pos;
5133: }
5134: }
5135: }
5136: NV(p)--;
5137: }
5138:
5139: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5140: {
5141: int i;
5142: P *tpl,*tpl1;
5143: NODE l;
5144: P h,gcd,t;
5145:
5146: tpl = (P *)MALLOC(m*sizeof(P));
5147: tpl1 = (P *)MALLOC(m*sizeof(P));
5148: bcopy(pl,tpl,m*sizeof(P));
5149: gcd = (P)ONE;
5150: for ( l = nd_hcf; l; l = NEXT(l) ) {
5151: h = (P)BDY(l);
5152: while ( 1 ) {
5153: for ( i = 0; i < m; i++ )
5154: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5155: break;
5156: if ( i == m ) {
5157: bcopy(tpl1,tpl,m*sizeof(P));
5158: mulp(vl,gcd,h,&t); gcd = t;
5159: } else
5160: break;
5161: }
5162: }
5163: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5164: if ( full ) {
5165: heu_nezgcdnpz(vl,tpl,m,&t);
5166: mulp(vl,gcd,t,pr);
5167: } else
5168: *pr = gcd;
5169: }
5170:
5171: void removecont_array(P *p,int n,int full)
5172: {
5173: int all_p,all_q,i;
5174: Z *c;
5175: P *w;
5176: P t,s;
5177:
5178: for ( all_q = 1, i = 0; i < n; i++ )
5179: all_q = all_q && NUM(p[i]);
5180: if ( all_q ) {
5181: removecont_array_q((Z *)p,n);
5182: } else {
5183: c = (Z *)MALLOC(n*sizeof(Z));
5184: w = (P *)MALLOC(n*sizeof(P));
5185: for ( i = 0; i < n; i++ ) {
5186: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5187: }
5188: removecont_array_q(c,n);
5189: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5190: for ( i = 0; i < n; i++ ) {
5191: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5192: }
5193: }
5194: }
5195:
5196: /* c is an int array */
5197:
5198: void removecont_array_q(Z *c,int n)
5199: {
5200: struct oVECT v;
5201: Z d0,d1,a,u,u1,gcd;
5202: int i,j;
5203: Z *q,*r;
5204:
5205: q = (Z *)MALLOC(n*sizeof(Z));
5206: r = (Z *)MALLOC(n*sizeof(Z));
5207: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5208: gcdvz_estimate(&v,&d0);
5209: for ( i = 0; i < n; i++ ) {
5210: divqrz(c[i],d0,&q[i],&r[i]);
5211: }
5212: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5213: if ( i < n ) {
5214: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5215: gcdvz(&v,&d1);
5216: gcdz(d0,d1,&gcd);
1.6 noro 5217: /* exact division */
5218: divsz(d0,gcd,&a);
1.1 noro 5219: for ( i = 0; i < n; i++ ) {
5220: mulz(a,q[i],&u);
5221: if ( r[i] ) {
1.6 noro 5222: /* exact division */
5223: divsz(r[i],gcd,&u1);
1.1 noro 5224: addz(u,u1,&q[i]);
5225: } else
5226: q[i] = u;
5227: }
5228: }
5229: for ( i = 0; i < n; i++ ) c[i] = q[i];
5230: }
5231:
1.4 noro 5232: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5233:
5234: void mpz_removecont_array(mpz_t *c,int n)
5235: {
5236: mpz_t d0,a,u,u1,gcd;
5237: int i,j;
1.13 noro 5238: static mpz_t *q,*r;
5239: static int c_len = 0;
1.4 noro 5240:
5241: for ( i = 0; i < n; i++ )
5242: if ( mpz_sgn(c[i]) ) break;
5243: if ( i == n ) return;
5244: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5245: if ( n > c_len ) {
5246: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5247: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5248: c_len = n;
5249: }
1.4 noro 5250: for ( i = 0; i < n; i++ ) {
5251: mpz_init(q[i]); mpz_init(r[i]);
5252: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5253: }
5254: for ( i = 0; i < n; i++ )
5255: if ( mpz_sgn(r[i]) ) break;
5256: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5257: if ( i < n ) {
5258: mpz_gcd(gcd,d0,r[i]);
5259: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5260: mpz_div(a,d0,gcd);
5261: for ( i = 0; i < n; i++ ) {
5262: mpz_mul(u,a,q[i]);
5263: if ( mpz_sgn(r[i]) ) {
5264: mpz_div(u1,r[i],gcd);
5265: mpz_add(q[i],u,u1);
5266: } else
5267: mpz_set(q[i],u);
5268: }
5269: }
5270: for ( i = 0; i < n; i++ )
5271: mpz_set(c[i],q[i]);
5272: }
5273:
1.1 noro 5274: void nd_mul_c(int mod,ND p,int mul)
5275: {
5276: NM m;
5277: int c,c1;
5278:
5279: if ( !p ) return;
5280: if ( mul == 1 ) return;
5281: if ( mod == -1 )
5282: for ( m = BDY(p); m; m = NEXT(m) )
5283: CM(m) = _mulsf(CM(m),mul);
5284: else
5285: for ( m = BDY(p); m; m = NEXT(m) ) {
5286: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5287: }
5288: }
5289:
5290: void nd_mul_c_lf(ND p,Z mul)
5291: {
5292: NM m;
5293: Z c;
5294:
5295: if ( !p ) return;
5296: if ( UNIZ(mul) ) return;
5297: for ( m = BDY(p); m; m = NEXT(m) ) {
5298: mullf(CZ(m),mul,&c); CZ(m) = c;
5299: }
5300: }
5301:
5302: void nd_mul_c_q(ND p,P mul)
5303: {
5304: NM m;
5305: P c;
5306:
5307: if ( !p ) return;
5308: if ( UNIQ(mul) ) return;
5309: for ( m = BDY(p); m; m = NEXT(m) ) {
5310: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5311: }
5312: }
5313:
5314: void nd_mul_c_p(VL vl,ND p,P mul)
5315: {
5316: NM m;
5317: P c;
5318:
5319: if ( !p ) return;
5320: for ( m = BDY(p); m; m = NEXT(m) ) {
5321: mulp(vl,CP(m),mul,&c); CP(m) = c;
5322: }
5323: }
5324:
5325: void nd_free(ND p)
5326: {
5327: NM t,s;
5328:
5329: if ( !p ) return;
5330: t = BDY(p);
5331: while ( t ) {
5332: s = NEXT(t);
5333: FREENM(t);
5334: t = s;
5335: }
5336: FREEND(p);
5337: }
5338:
5339: void ndv_free(NDV p)
5340: {
5341: GCFREE(BDY(p));
5342: }
5343:
5344: void nd_append_red(UINT *d,int i)
5345: {
5346: RHist m,m0;
5347: int h;
5348:
5349: NEWRHist(m);
5350: h = ndl_hash_value(d);
5351: m->index = i;
5352: ndl_copy(d,DL(m));
5353: NEXT(m) = nd_red[h];
5354: nd_red[h] = m;
5355: }
5356:
5357: UINT *ndv_compute_bound(NDV p)
5358: {
5359: UINT *d1,*d2,*t;
5360: UINT u;
5361: int i,j,k,l,len,ind;
5362: NMV m;
5363:
5364: if ( !p )
5365: return 0;
5366: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5367: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5368: len = LEN(p);
5369: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5370: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5371: ndl_max(DL(m),d1,d2);
5372: t = d1; d1 = d2; d2 = t;
5373: }
5374: l = nd_nvar+31;
5375: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5376: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5377: u = d1[i];
5378: k = (nd_epw-1)*nd_bpe;
5379: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5380: t[ind] = (u>>k)&nd_mask0;
5381: }
5382: for ( ; ind < l; ind++ ) t[ind] = 0;
5383: return t;
5384: }
5385:
5386: UINT *nd_compute_bound(ND p)
5387: {
5388: UINT *d1,*d2,*t;
5389: UINT u;
5390: int i,j,k,l,len,ind;
5391: NM m;
5392:
5393: if ( !p )
5394: return 0;
5395: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5396: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5397: len = LEN(p);
5398: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5399: for ( m = NEXT(m); m; m = NEXT(m) ) {
5400: ndl_lcm(DL(m),d1,d2);
5401: t = d1; d1 = d2; d2 = t;
5402: }
5403: l = nd_nvar+31;
5404: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5405: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5406: u = d1[i];
5407: k = (nd_epw-1)*nd_bpe;
5408: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5409: t[ind] = (u>>k)&nd_mask0;
5410: }
5411: for ( ; ind < l; ind++ ) t[ind] = 0;
5412: return t;
5413: }
5414:
5415: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5416: /* of a term. In this case we need additional 1 word. */
5417:
5418: int nd_get_exporigin(struct order_spec *ord)
5419: {
5420: switch ( ord->id ) {
1.21 noro 5421: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5422: return 1+nd_module;
5423: case 1: case 257:
5424: /* block order */
5425: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5426: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5427: return ord->ord.block.length+1+nd_module;
5428: case 3: case 259:
5429: #if 0
5430: error("nd_get_exporigin : composite order is not supported yet.");
5431: #else
5432: return 1+nd_module;
5433: #endif
5434: default:
5435: error("nd_get_exporigin : ivalid argument.");
5436: return 0;
5437: }
5438: }
5439:
5440: void nd_setup_parameters(int nvar,int max) {
5441: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5442: struct order_pair *op;
5443:
5444: nd_nvar = nvar;
5445: if ( max ) {
5446: /* XXX */
5447: if ( do_weyl ) nd_bpe = 32;
5448: else if ( max < 2 ) nd_bpe = 1;
5449: else if ( max < 4 ) nd_bpe = 2;
5450: else if ( max < 8 ) nd_bpe = 3;
5451: else if ( max < 16 ) nd_bpe = 4;
5452: else if ( max < 32 ) nd_bpe = 5;
5453: else if ( max < 64 ) nd_bpe = 6;
5454: else if ( max < 256 ) nd_bpe = 8;
5455: else if ( max < 1024 ) nd_bpe = 10;
5456: else if ( max < 65536 ) nd_bpe = 16;
5457: else nd_bpe = 32;
5458: }
5459: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5460: UINT t;
5461: int st;
5462: int *v;
5463: /* t = max(weights) */
5464: t = 0;
5465: if ( current_dl_weight_vector )
5466: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5467: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5468: if ( t < st ) t = st;
5469: }
5470: if ( nd_matrix )
5471: for ( i = 0; i < nd_matrix_len; i++ )
5472: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5473: if ( (st=v[j]) < 0 ) st = -st;
5474: if ( t < st ) t = st;
5475: }
5476: /* i = bitsize of t */
5477: for ( i = 0; t; t >>=1, i++ );
5478: /* i += bitsize of nd_nvar */
5479: for ( t = nd_nvar; t; t >>=1, i++);
5480: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5481: if ( (nd_bpe+i) >= 31 )
5482: error("nd_setup_parameters : too large weight");
5483: }
5484: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5485: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5486: nd_exporigin = nd_get_exporigin(nd_ord);
5487: wpd = nd_exporigin+elen;
5488: if ( nd_module )
5489: nd_mpos = nd_exporigin-1;
5490: else
5491: nd_mpos = -1;
5492: if ( wpd != nd_wpd ) {
5493: nd_free_private_storage();
5494: nd_wpd = wpd;
5495: }
5496: if ( nd_bpe < 32 ) {
5497: nd_mask0 = (1<<nd_bpe)-1;
5498: } else {
5499: nd_mask0 = 0xffffffff;
5500: }
5501: bzero(nd_mask,sizeof(nd_mask));
5502: nd_mask1 = 0;
5503: for ( i = 0; i < nd_epw; i++ ) {
5504: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5505: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5506: }
5507: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5508: nd_epos = nd_create_epos(nd_ord);
5509: nd_blockmask = nd_create_blockmask(nd_ord);
5510: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5511: }
5512:
5513: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5514: {
5515: int i,obpe,oadv,h;
5516: static NM prev_nm_free_list;
5517: static ND_pairs prev_ndp_free_list;
5518: RHist mr0,mr;
5519: RHist r;
5520: RHist *old_red;
5521: ND_pairs s0,s,t;
5522: EPOS oepos;
5523:
5524: obpe = nd_bpe;
5525: oadv = nmv_adv;
5526: oepos = nd_epos;
5527: if ( obpe < 2 ) nd_bpe = 2;
5528: else if ( obpe < 3 ) nd_bpe = 3;
5529: else if ( obpe < 4 ) nd_bpe = 4;
5530: else if ( obpe < 5 ) nd_bpe = 5;
5531: else if ( obpe < 6 ) nd_bpe = 6;
5532: else if ( obpe < 8 ) nd_bpe = 8;
5533: else if ( obpe < 10 ) nd_bpe = 10;
5534: else if ( obpe < 16 ) nd_bpe = 16;
5535: else if ( obpe < 32 ) nd_bpe = 32;
5536: else error("nd_reconstruct : exponent too large");
5537:
5538: nd_setup_parameters(nd_nvar,0);
5539: prev_nm_free_list = _nm_free_list;
5540: prev_ndp_free_list = _ndp_free_list;
5541: _nm_free_list = 0;
5542: _ndp_free_list = 0;
5543: for ( i = nd_psn-1; i >= 0; i-- ) {
5544: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5545: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5546: }
5547: if ( trace )
5548: for ( i = nd_psn-1; i >= 0; i-- ) {
5549: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5550: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5551: }
5552: s0 = 0;
5553: for ( t = d; t; t = NEXT(t) ) {
5554: NEXTND_pairs(s0,s);
5555: s->i1 = t->i1;
5556: s->i2 = t->i2;
1.24 noro 5557: s->sig = t->sig;
1.1 noro 5558: SG(s) = SG(t);
5559: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5560: }
5561:
5562: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5563: for ( i = 0; i < REDTAB_LEN; i++ ) {
5564: old_red[i] = nd_red[i];
5565: nd_red[i] = 0;
5566: }
5567: for ( i = 0; i < REDTAB_LEN; i++ )
5568: for ( r = old_red[i]; r; r = NEXT(r) ) {
5569: NEWRHist(mr);
5570: mr->index = r->index;
5571: SG(mr) = SG(r);
5572: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5573: h = ndl_hash_value(DL(mr));
5574: NEXT(mr) = nd_red[h];
5575: nd_red[h] = mr;
1.24 noro 5576: mr->sig = r->sig;
1.1 noro 5577: }
5578: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5579: old_red = 0;
5580: for ( i = 0; i < nd_psn; i++ ) {
5581: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5582: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5583: r->sig = nd_psh[i]->sig;
1.1 noro 5584: nd_psh[i] = r;
5585: }
5586: if ( s0 ) NEXT(s) = 0;
5587: prev_nm_free_list = 0;
5588: prev_ndp_free_list = 0;
5589: #if 0
5590: GC_gcollect();
5591: #endif
5592: return s0;
5593: }
5594:
1.30 noro 5595: void nd_reconstruct_s(int trace,ND_pairs *d)
5596: {
5597: int i,obpe,oadv,h;
5598: static NM prev_nm_free_list;
5599: static ND_pairs prev_ndp_free_list;
5600: RHist mr0,mr;
5601: RHist r;
5602: RHist *old_red;
5603: ND_pairs s0,s,t;
5604: EPOS oepos;
5605:
5606: obpe = nd_bpe;
5607: oadv = nmv_adv;
5608: oepos = nd_epos;
5609: if ( obpe < 2 ) nd_bpe = 2;
5610: else if ( obpe < 3 ) nd_bpe = 3;
5611: else if ( obpe < 4 ) nd_bpe = 4;
5612: else if ( obpe < 5 ) nd_bpe = 5;
5613: else if ( obpe < 6 ) nd_bpe = 6;
5614: else if ( obpe < 8 ) nd_bpe = 8;
5615: else if ( obpe < 10 ) nd_bpe = 10;
5616: else if ( obpe < 16 ) nd_bpe = 16;
5617: else if ( obpe < 32 ) nd_bpe = 32;
5618: else error("nd_reconstruct_s : exponent too large");
5619:
5620: nd_setup_parameters(nd_nvar,0);
5621: prev_nm_free_list = _nm_free_list;
5622: prev_ndp_free_list = _ndp_free_list;
5623: _nm_free_list = 0;
5624: _ndp_free_list = 0;
5625: for ( i = nd_psn-1; i >= 0; i-- ) {
5626: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5627: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5628: }
5629: if ( trace )
5630: for ( i = nd_psn-1; i >= 0; i-- ) {
5631: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5632: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5633: }
5634:
5635: for ( i = 0; i < nd_nbase; i++ ) {
5636: s0 = 0;
5637: for ( t = d[i]; t; t = NEXT(t) ) {
5638: NEXTND_pairs(s0,s);
5639: s->i1 = t->i1;
5640: s->i2 = t->i2;
5641: s->sig = t->sig;
5642: SG(s) = SG(t);
5643: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5644: }
5645: d[i] = s0;
5646: }
5647:
5648: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5649: for ( i = 0; i < REDTAB_LEN; i++ ) {
5650: old_red[i] = nd_red[i];
5651: nd_red[i] = 0;
5652: }
5653: for ( i = 0; i < REDTAB_LEN; i++ )
5654: for ( r = old_red[i]; r; r = NEXT(r) ) {
5655: NEWRHist(mr);
5656: mr->index = r->index;
5657: SG(mr) = SG(r);
5658: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5659: h = ndl_hash_value(DL(mr));
5660: NEXT(mr) = nd_red[h];
5661: nd_red[h] = mr;
5662: mr->sig = r->sig;
5663: }
5664: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5665: old_red = 0;
5666: for ( i = 0; i < nd_psn; i++ ) {
5667: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5668: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
5669: r->sig = nd_psh[i]->sig;
5670: nd_psh[i] = r;
5671: }
5672: if ( s0 ) NEXT(s) = 0;
5673: prev_nm_free_list = 0;
5674: prev_ndp_free_list = 0;
5675: #if 0
5676: GC_gcollect();
5677: #endif
5678: }
5679:
1.1 noro 5680: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5681: {
5682: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5683: struct order_pair *op;
5684:
5685: n = nd_nvar;
5686: oepw = (sizeof(UINT)*8)/obpe;
5687: omask0 = (1<<obpe)-1;
5688: TD(r) = TD(d);
5689: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5690: if ( nd_blockmask ) {
5691: l = nd_blockmask->n;
5692: op = nd_blockmask->order_pair;
5693: for ( i = 1; i < nd_exporigin; i++ )
5694: r[i] = d[i];
5695: for ( j = 0, s = 0; j < l; j++ ) {
5696: ord_l = op[j].length;
5697: for ( i = 0; i < ord_l; i++, s++ ) {
5698: ei = GET_EXP_OLD(d,s);
5699: PUT_EXP(r,s,ei);
5700: }
5701: }
5702: } else {
5703: for ( i = 0; i < n; i++ ) {
5704: ei = GET_EXP_OLD(d,i);
5705: PUT_EXP(r,i,ei);
5706: }
5707: }
5708: if ( nd_module ) MPOS(r) = MPOS(d);
5709: }
5710:
5711: ND nd_copy(ND p)
5712: {
5713: NM m,mr,mr0;
5714: int c,n;
5715: ND r;
5716:
5717: if ( !p )
5718: return 0;
5719: else {
5720: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5721: NEXTNM(mr0,mr);
5722: CM(mr) = CM(m);
5723: ndl_copy(DL(m),DL(mr));
5724: }
5725: NEXT(mr) = 0;
5726: MKND(NV(p),mr0,LEN(p),r);
5727: SG(r) = SG(p);
5728: return r;
5729: }
5730: }
5731:
5732: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5733: {
5734: NM m1,m2;
5735: NDV p1,p2;
5736: ND t1,t2;
5737: UINT *lcm;
5738: P gp,tp;
5739: Z g,t;
5740: Z iq;
5741: int td;
5742: LIST hist;
5743: NODE node;
5744: DP d;
5745:
5746: if ( !mod && nd_demand ) {
5747: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5748: } else {
5749: if ( trace ) {
5750: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5751: } else {
5752: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5753: }
5754: }
5755: lcm = LCM(p);
5756: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5757: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5758: FREENM(m1); return 0;
5759: }
5760: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5761: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5762: FREENM(m1); FREENM(m2); return 0;
5763: }
5764:
5765: if ( mod == -1 ) {
5766: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5767: } else if ( mod > 0 ) {
5768: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5769: } else if ( mod == -2 ) {
5770: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5771: } else if ( nd_vc ) {
5772: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5773: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5774: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5775: } else {
1.6 noro 5776: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5777: }
5778: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5779: *rp = nd_add(mod,t1,t2);
5780: if ( nd_gentrace ) {
5781: /* nd_tracelist is initialized */
1.6 noro 5782: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5783: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5784: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5785: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5786: nd_tracelist = node;
5787: }
1.24 noro 5788: if ( *rp )
5789: (*rp)->sig = p->sig;
1.1 noro 5790: FREENM(m1); FREENM(m2);
5791: return 1;
5792: }
5793:
5794: void ndv_mul_c(int mod,NDV p,int mul)
5795: {
5796: NMV m;
5797: int c,c1,len,i;
5798:
5799: if ( !p ) return;
5800: len = LEN(p);
5801: if ( mod == -1 )
5802: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5803: CM(m) = _mulsf(CM(m),mul);
5804: else
5805: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5806: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5807: }
5808: }
5809:
5810: void ndv_mul_c_lf(NDV p,Z mul)
5811: {
5812: NMV m;
5813: Z c;
5814: int len,i;
5815:
5816: if ( !p ) return;
5817: len = LEN(p);
5818: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5819: mullf(CZ(m),mul,&c); CZ(m) = c;
5820: }
5821: }
5822:
5823: /* for nd_det */
5824: void ndv_mul_c_q(NDV p,Z mul)
5825: {
5826: NMV m;
5827: Z c;
5828: int len,i;
5829:
5830: if ( !p ) return;
5831: len = LEN(p);
5832: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 5833: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 5834: }
5835: }
5836:
5837: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
5838: int n2,i,j,l,n,tlen;
5839: UINT *d0;
5840: NM *tab,*psum;
5841: ND s,r;
5842: NM t;
5843: NMV m1;
5844:
5845: if ( !p ) return 0;
5846: n = NV(p); n2 = n>>1;
5847: d0 = DL(m0);
5848: l = LEN(p);
5849: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
5850: tab = (NM *)MALLOC(tlen*sizeof(NM));
5851: psum = (NM *)MALLOC(tlen*sizeof(NM));
5852: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
5853: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
5854: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
5855: /* m0(NM) * m1(NMV) => tab(NM) */
5856: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
5857: for ( j = 0; j < tlen; j++ ) {
5858: if ( tab[j] ) {
5859: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
5860: }
5861: }
5862: }
5863: for ( i = tlen-1, r = 0; i >= 0; i-- )
5864: if ( psum[i] ) {
5865: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
5866: MKND(n,psum[i],j,s);
5867: r = nd_add(mod,r,s);
5868: }
5869: if ( r ) SG(r) = SG(p)+TD(d0);
5870: return r;
5871: }
5872:
5873: /* product of monomials */
5874: /* XXX block order is not handled correctly */
5875:
5876: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
5877: {
5878: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
5879: UINT *d0,*d1,*d,*dt,*ctab;
5880: Z *ctab_q;
5881: Z q,q1;
5882: UINT c0,c1,c;
5883: NM *p;
5884: NM m,t;
5885: int mpos;
5886:
5887: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
5888: if ( !m0 || !m1 ) return;
5889: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
5890: if ( nd_module )
5891: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
5892:
5893: NEWNM(m); d = DL(m);
5894: if ( mod ) {
5895: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
5896: } else if ( nd_vc )
5897: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
5898: else
1.6 noro 5899: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 5900: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
5901: homo = n&1 ? 1 : 0;
5902: if ( homo ) {
5903: /* offset of h-degree */
5904: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
5905: PUT_EXP(DL(m),n-1,h);
5906: TD(DL(m)) = h;
5907: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5908: }
5909: tab[0] = m;
5910: NEWNM(m); d = DL(m);
5911: for ( i = 0, curlen = 1; i < n2; i++ ) {
5912: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
5913: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
5914: /* xi^a*(Di^k*xi^l)*Di^b */
5915: a += l; b += k;
5916: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
5917: if ( !k || !l ) {
5918: for ( j = 0; j < curlen; j++ )
5919: if ( (t = tab[j]) != 0 ) {
5920: dt = DL(t);
5921: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
5922: if ( nd_blockmask ) ndl_weight_mask(dt);
5923: }
5924: curlen *= k+1;
5925: continue;
5926: }
5927: min = MIN(k,l);
5928: if ( mod ) {
5929: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
5930: mkwcm(k,l,mod,(int *)ctab);
5931: } else {
5932: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
5933: mkwc(k,l,ctab_q);
5934: }
5935: for ( j = min; j >= 0; j-- ) {
5936: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
5937: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
5938: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
5939: if ( homo ) {
5940: TD(d) = s;
5941: PUT_EXP(d,n-1,s-h);
5942: } else TD(d) = h;
5943: if ( nd_blockmask ) ndl_weight_mask(d);
5944: if ( mod ) c = ctab[j];
5945: else q = ctab_q[j];
5946: p = tab+curlen*j;
5947: if ( j == 0 ) {
5948: for ( u = 0; u < curlen; u++, p++ ) {
5949: if ( tab[u] ) {
5950: ndl_addto(DL(tab[u]),d);
5951: if ( mod ) {
5952: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
5953: } else if ( nd_vc )
5954: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
5955: else {
1.6 noro 5956: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 5957: }
5958: }
5959: }
5960: } else {
5961: for ( u = 0; u < curlen; u++, p++ ) {
5962: if ( tab[u] ) {
5963: NEWNM(t);
5964: ndl_add(DL(tab[u]),d,DL(t));
5965: if ( mod ) {
5966: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
5967: } else if ( nd_vc )
5968: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
5969: else
1.6 noro 5970: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 5971: *p = t;
5972: }
5973: }
5974: }
5975: }
5976: curlen *= k+1;
5977: }
5978: FREENM(m);
5979: if ( nd_module ) {
5980: mpos = MPOS(d1);
5981: for ( i = 0; i < tlen; i++ )
5982: if ( tab[i] ) {
5983: d = DL(tab[i]);
5984: MPOS(d) = mpos;
5985: TD(d) = ndl_weight(d);
5986: }
5987: }
5988: }
5989:
5990: ND ndv_mul_nm_symbolic(NM m0,NDV p)
5991: {
5992: NM mr,mr0;
5993: NMV m;
5994: UINT *d,*dt,*dm;
5995: int c,n,td,i,c1,c2,len;
5996: Q q;
5997: ND r;
5998:
5999: if ( !p ) return 0;
6000: else {
6001: n = NV(p); m = BDY(p);
6002: d = DL(m0);
6003: len = LEN(p);
6004: mr0 = 0;
6005: td = TD(d);
6006: c = CM(m0);
6007: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6008: NEXTNM(mr0,mr);
6009: CM(mr) = 1;
6010: ndl_add(DL(m),d,DL(mr));
6011: }
6012: NEXT(mr) = 0;
6013: MKND(NV(p),mr0,len,r);
6014: SG(r) = SG(p) + TD(d);
6015: return r;
6016: }
6017: }
6018:
6019: ND ndv_mul_nm(int mod,NM m0,NDV p)
6020: {
6021: NM mr,mr0;
6022: NMV m;
6023: UINT *d,*dt,*dm;
6024: int c,n,td,i,c1,c2,len;
6025: P q;
6026: ND r;
6027:
6028: if ( !p ) return 0;
6029: else if ( do_weyl ) {
6030: if ( mod < 0 ) {
6031: error("ndv_mul_nm : not implemented (weyl)");
6032: return 0;
6033: } else
6034: return weyl_ndv_mul_nm(mod,m0,p);
6035: } else {
6036: n = NV(p); m = BDY(p);
6037: d = DL(m0);
6038: len = LEN(p);
6039: mr0 = 0;
6040: td = TD(d);
6041: if ( mod == -1 ) {
6042: c = CM(m0);
6043: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6044: NEXTNM(mr0,mr);
6045: CM(mr) = _mulsf(CM(m),c);
6046: ndl_add(DL(m),d,DL(mr));
6047: }
6048: } else if ( mod == -2 ) {
6049: Z cl;
6050: cl = CZ(m0);
6051: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6052: NEXTNM(mr0,mr);
6053: mullf(CZ(m),cl,&CZ(mr));
6054: ndl_add(DL(m),d,DL(mr));
6055: }
6056: } else if ( mod ) {
6057: c = CM(m0);
6058: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6059: NEXTNM(mr0,mr);
6060: c1 = CM(m);
6061: DMAR(c1,c,0,mod,c2);
6062: CM(mr) = c2;
6063: ndl_add(DL(m),d,DL(mr));
6064: }
6065: } else {
6066: q = CP(m0);
6067: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6068: NEXTNM(mr0,mr);
6069: mulp(nd_vc,CP(m),q,&CP(mr));
6070: ndl_add(DL(m),d,DL(mr));
6071: }
6072: }
6073: NEXT(mr) = 0;
6074: MKND(NV(p),mr0,len,r);
6075: SG(r) = SG(p) + TD(d);
6076: return r;
6077: }
6078: }
6079:
6080: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6081: {
6082: NM mq0,mq;
6083: NMV tm;
6084: Q q;
6085: int i,nv,sg,c,c1,c2,hindex;
6086: ND p,t,r;
6087:
6088: if ( bucket->m < 0 ) return 0;
6089: else {
6090: nv = NV(d);
6091: mq0 = 0;
6092: tm = (NMV)MALLOC(nmv_adv);
6093: while ( 1 ) {
6094: if ( mod > 0 || mod == -1 )
6095: hindex = head_pbucket(mod,bucket);
6096: else if ( mod == -2 )
6097: hindex = head_pbucket_lf(bucket);
6098: else
6099: hindex = head_pbucket_q(bucket);
6100: if ( hindex < 0 ) break;
6101: p = bucket->body[hindex];
6102: NEXTNM(mq0,mq);
6103: ndl_sub(HDL(p),HDL(d),DL(mq));
6104: ndl_copy(DL(mq),DL(tm));
6105: if ( mod ) {
6106: c1 = invm(HCM(d),mod); c2 = HCM(p);
6107: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6108: CM(tm) = mod-c;
6109: } else {
1.6 noro 6110: divsz(HCZ(p),HCZ(d),&CZ(mq));
6111: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6112: }
6113: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6114: bucket->body[hindex] = nd_remove_head(p);
6115: t = nd_remove_head(t);
6116: add_pbucket(mod,bucket,t);
6117: }
6118: if ( !mq0 )
6119: r = 0;
6120: else {
6121: NEXT(mq) = 0;
6122: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6123: MKND(nv,mq0,i,r);
6124: /* XXX */
6125: SG(r) = HTD(r);
6126: }
6127: return r;
6128: }
6129: }
6130:
6131: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6132: {
6133: NMV m,mr,mr0,t;
6134: int len,i,k;
6135:
6136: if ( !p ) return;
6137: m = BDY(p); len = LEN(p);
6138: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6139: m = (NMV)((char *)mr0+(len-1)*oadv);
6140: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6141: t = (NMV)MALLOC(nmv_adv);
6142: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6143: CZ(t) = CZ(m);
1.1 noro 6144: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6145: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6146: CZ(mr) = CZ(t);
1.1 noro 6147: ndl_copy(DL(t),DL(mr));
6148: }
6149: BDY(p) = mr0;
6150: }
6151:
6152: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6153: {
6154: NMV m,mr,mr0;
6155: int len,i;
6156: NDV r;
6157:
6158: if ( !p ) return 0;
6159: m = BDY(p); len = LEN(p);
6160: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6161: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6162: ndl_zero(DL(mr));
6163: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6164: CZ(mr) = CZ(m);
1.1 noro 6165: }
6166: MKNDV(NV(p),mr0,len,r);
6167: SG(r) = SG(p);
1.24 noro 6168: r->sig = p->sig;
1.1 noro 6169: return r;
6170: }
6171:
6172: /* duplicate p */
6173:
6174: NDV ndv_dup(int mod,NDV p)
6175: {
6176: NDV d;
6177: NMV t,m,m0;
6178: int i,len;
6179:
6180: if ( !p ) return 0;
6181: len = LEN(p);
6182: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6183: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6184: ndl_copy(DL(t),DL(m));
1.6 noro 6185: CZ(m) = CZ(t);
1.1 noro 6186: }
6187: MKNDV(NV(p),m0,len,d);
6188: SG(d) = SG(p);
6189: return d;
6190: }
6191:
6192: NDV ndv_symbolic(int mod,NDV p)
6193: {
6194: NDV d;
6195: NMV t,m,m0;
6196: int i,len;
6197:
6198: if ( !p ) return 0;
6199: len = LEN(p);
6200: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6201: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6202: ndl_copy(DL(t),DL(m));
1.6 noro 6203: CZ(m) = ONE;
1.1 noro 6204: }
6205: MKNDV(NV(p),m0,len,d);
6206: SG(d) = SG(p);
6207: return d;
6208: }
6209:
6210: ND nd_dup(ND p)
6211: {
6212: ND d;
6213: NM t,m,m0;
6214:
6215: if ( !p ) return 0;
6216: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6217: NEXTNM(m0,m);
6218: ndl_copy(DL(t),DL(m));
1.6 noro 6219: CZ(m) = CZ(t);
1.1 noro 6220: }
6221: if ( m0 ) NEXT(m) = 0;
6222: MKND(NV(p),m0,LEN(p),d);
6223: SG(d) = SG(p);
6224: return d;
6225: }
6226:
6227: /* XXX if p->len == 0 then it represents 0 */
6228:
6229: void ndv_mod(int mod,NDV p)
6230: {
6231: NMV t,d;
6232: int r,s,u;
6233: int i,len,dlen;
6234: P cp;
6235: Z c;
6236: Obj gfs;
6237:
6238: if ( !p ) return;
6239: len = LEN(p);
6240: dlen = 0;
6241: if ( mod == -1 )
6242: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6243: simp_ff((Obj)CP(t),&gfs);
6244: if ( gfs ) {
6245: r = FTOIF(CONT((GFS)gfs));
6246: CM(d) = r;
6247: ndl_copy(DL(t),DL(d));
6248: NMV_ADV(d);
6249: dlen++;
6250: }
6251: }
6252: else if ( mod == -2 )
6253: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6254: simp_ff((Obj)CP(t),&gfs);
6255: if ( gfs ) {
6256: lmtolf((LM)gfs,&CZ(d));
6257: ndl_copy(DL(t),DL(d));
6258: NMV_ADV(d);
6259: dlen++;
6260: }
6261: }
6262: else
6263: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6264: if ( nd_vc ) {
6265: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6266: c = (Z)cp;
6267: } else
1.6 noro 6268: c = CZ(t);
1.1 noro 6269: r = remqi((Q)c,mod);
6270: if ( r ) {
6271: CM(d) = r;
6272: ndl_copy(DL(t),DL(d));
6273: NMV_ADV(d);
6274: dlen++;
6275: }
6276: }
6277: LEN(p) = dlen;
6278: }
6279:
6280: NDV ptondv(VL vl,VL dvl,P p)
6281: {
6282: ND nd;
6283:
6284: nd = ptond(vl,dvl,p);
6285: return ndtondv(0,nd);
6286: }
6287:
6288: void pltozpl(LIST l,Q *cont,LIST *pp)
6289: {
1.16 noro 6290: NODE nd,nd1;
6291: int n;
6292: P *pl;
6293: Q *cl;
6294: int i;
6295: P dmy;
6296: Z dvr,inv;
6297: LIST r;
6298:
6299: nd = BDY(l); n = length(nd);
6300: pl = (P *)MALLOC(n*sizeof(P));
6301: cl = (Q *)MALLOC(n*sizeof(Q));
6302: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6303: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6304: }
6305: qltozl(cl,n,&dvr);
6306: divz(ONE,dvr,&inv);
6307: nd = BDY(l);
6308: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6309: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6310: nd = 0;
6311: for ( i = n-1; i >= 0; i-- ) {
6312: MKNODE(nd1,pl[i],nd); nd = nd1;
6313: }
6314: MKLIST(r,nd);
6315: *pp = r;
1.1 noro 6316: }
6317:
6318: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6319:
6320: NDV pltondv(VL vl,VL dvl,LIST p)
6321: {
6322: int i;
6323: NODE t;
6324: ND r,ri;
6325: NM m;
6326:
6327: if ( !nd_module ) error("pltond : module order must be set");
6328: r = 0;
6329: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6330: ri = ptond(vl,dvl,(P)BDY(t));
6331: if ( ri )
6332: for ( m = BDY(ri); m; m = NEXT(m) ) {
6333: MPOS(DL(m)) = i;
6334: TD(DL(m)) = ndl_weight(DL(m));
6335: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6336: }
6337: r = nd_add(0,r,ri);
6338: }
6339: return ndtondv(0,r);
6340: }
6341:
6342: ND ptond(VL vl,VL dvl,P p)
6343: {
6344: int n,i,j,k,e;
6345: VL tvl;
6346: V v;
6347: DCP dc;
6348: DCP *w;
6349: ND r,s,t,u;
6350: P x;
6351: int c;
6352: UINT *d;
6353: NM m,m0;
6354:
6355: if ( !p )
6356: return 0;
6357: else if ( NUM(p) ) {
6358: NEWNM(m);
6359: ndl_zero(DL(m));
6360: if ( !INT((Q)p) )
6361: error("ptond : input must be integer-coefficient");
1.6 noro 6362: CZ(m) = (Z)p;
1.1 noro 6363: NEXT(m) = 0;
6364: MKND(nd_nvar,m,1,r);
6365: SG(r) = 0;
6366: return r;
6367: } else {
6368: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6369: w = (DCP *)MALLOC(k*sizeof(DCP));
6370: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6371: for ( i = 0, tvl = dvl, v = VR(p);
6372: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6373: if ( !tvl ) {
6374: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6375: t = ptond(vl,dvl,COEF(w[j]));
6376: pwrp(vl,x,DEG(w[j]),&p);
6377: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6378: }
6379: return s;
6380: } else {
6381: NEWNM(m0); d = DL(m0);
6382: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6383: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6384: TD(d) = MUL_WEIGHT(e,i);
6385: if ( nd_blockmask) ndl_weight_mask(d);
6386: if ( nd_module ) MPOS(d) = 0;
6387: t = ptond(vl,dvl,COEF(w[j]));
6388: for ( m = BDY(t); m; m = NEXT(m) )
6389: ndl_addto(DL(m),d);
6390: SG(t) += TD(d);
6391: s = nd_add(0,s,t);
6392: }
6393: FREENM(m0);
6394: return s;
6395: }
6396: }
6397: }
6398:
6399: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6400: {
6401: VL tvl;
6402: int len,n,j,i,e;
6403: NMV m;
6404: Z q;
6405: P c;
6406: UINT *d;
6407: P s,r,u,t,w;
6408: GFS gfs;
6409:
6410: if ( !p ) return 0;
6411: else {
6412: len = LEN(p);
6413: n = NV(p);
6414: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6415: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6416: if ( mod == -1 ) {
6417: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6418: } else if ( mod == -2 ) {
6419: c = (P)CZ(m);
6420: } else if ( mod > 0 ) {
1.6 noro 6421: STOZ(CM(m),q); c = (P)q;
1.1 noro 6422: } else
6423: c = CP(m);
6424: d = DL(m);
6425: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6426: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6427: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6428: }
6429: addp(vl,s,t,&u); s = u;
6430: }
6431: return s;
6432: }
6433: }
6434:
6435: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6436: {
6437: VL tvl;
6438: int len,n,j,i,e;
6439: NMV m;
6440: Z q;
6441: P c;
6442: UINT *d;
6443: P s,r,u,t,w;
6444: GFS gfs;
6445: P *a;
6446: LIST l;
6447: NODE nd,nd1;
6448:
6449: if ( !p ) return 0;
6450: else {
6451: a = (P *)MALLOC((rank+1)*sizeof(P));
6452: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6453: len = LEN(p);
6454: n = NV(p);
6455: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6456: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6457: if ( mod == -1 ) {
6458: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6459: } else if ( mod ) {
1.6 noro 6460: STOZ(CM(m),q); c = (P)q;
1.1 noro 6461: } else
6462: c = CP(m);
6463: d = DL(m);
6464: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6465: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6466: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6467: }
6468: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6469: }
6470: nd = 0;
6471: for ( i = rank; i > 0; i-- ) {
6472: MKNODE(nd1,a[i],nd); nd = nd1;
6473: }
6474: MKLIST(l,nd);
6475: return l;
6476: }
6477: }
6478:
6479: NDV ndtondv(int mod,ND p)
6480: {
6481: NDV d;
6482: NMV m,m0;
6483: NM t;
6484: int i,len;
6485:
6486: if ( !p ) return 0;
6487: len = LEN(p);
6488: if ( mod > 0 || mod == -1 )
6489: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6490: else
6491: m0 = m = MALLOC(len*nmv_adv);
6492: #if 0
6493: ndv_alloc += nmv_adv*len;
6494: #endif
6495: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6496: ndl_copy(DL(t),DL(m));
1.6 noro 6497: CZ(m) = CZ(t);
1.1 noro 6498: }
6499: MKNDV(NV(p),m0,len,d);
6500: SG(d) = SG(p);
1.24 noro 6501: d->sig = p->sig;
1.1 noro 6502: return d;
6503: }
6504:
1.16 noro 6505: static int dmm_comp_nv;
6506:
6507: int dmm_comp(DMM *a,DMM *b)
6508: {
6509: return -compdmm(dmm_comp_nv,*a,*b);
6510: }
6511:
6512: void dmm_sort_by_ord(DMM *a,int len,int nv)
6513: {
6514: dmm_comp_nv = nv;
6515: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6516: }
6517:
6518: void dpm_sort(DPM p,DPM *rp)
6519: {
6520: DMM t,t1;
6521: int len,i,n;
6522: DMM *a;
6523: DPM d;
6524:
6525: if ( !p ) *rp = 0;
6526: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6527: a = (DMM *)MALLOC(len*sizeof(DMM));
6528: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6529: n = p->nv;
6530: dmm_sort_by_ord(a,len,n);
6531: t = 0;
6532: for ( i = len-1; i >= 0; i-- ) {
6533: NEWDMM(t1);
6534: t1->c = a[i]->c;
6535: t1->dl = a[i]->dl;
6536: t1->pos = a[i]->pos;
6537: t1->next = t;
6538: t = t1;
6539: }
6540: MKDPM(n,t,d);
6541: SG(d) = SG(p);
6542: *rp = d;
6543: }
6544:
1.18 noro 6545: int dpm_comp(DPM *a,DPM *b)
6546: {
1.22 noro 6547: return -compdpm(CO,*a,*b);
1.18 noro 6548: }
6549:
6550: NODE dpm_sort_list(NODE l)
6551: {
6552: int i,len;
6553: NODE t,t1;
6554: DPM *a;
6555:
6556: len = length(l);
6557: a = (DPM *)MALLOC(len*sizeof(DPM));
6558: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6559: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6560: t = 0;
6561: for ( i = len-1; i >= 0; i-- ) {
6562: MKNODE(t1,(pointer)a[i],t); t = t1;
6563: }
6564: return t;
6565: }
6566:
1.20 noro 6567: int nmv_comp(NMV a,NMV b)
6568: {
1.21 noro 6569: int t;
6570: t = DL_COMPARE(a->dl,b->dl);
6571: return -t;
1.20 noro 6572: }
6573:
1.16 noro 6574: NDV dpmtondv(int mod,DPM p)
6575: {
6576: NDV d;
6577: NMV m,m0;
6578: DMM t;
6579: DMM *a;
6580: int i,len,n;
6581:
6582: if ( !p ) return 0;
6583: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6584: a = (DMM *)MALLOC(len*sizeof(DMM));
6585: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6586: n = p->nv;
6587: dmm_sort_by_ord(a,len,n);
6588: if ( mod > 0 || mod == -1 )
6589: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6590: else
6591: m0 = m = MALLOC(len*nmv_adv);
6592: #if 0
6593: ndv_alloc += nmv_adv*len;
6594: #endif
6595: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6596: dltondl(n,a[i]->dl,DL(m));
6597: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6598: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6599: CZ(m) = (Z)a[i]->c;
6600: }
1.20 noro 6601: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6602: MKNDV(NV(p),m0,len,d);
6603: SG(d) = SG(p);
6604: return d;
6605: }
6606:
1.1 noro 6607: ND ndvtond(int mod,NDV p)
6608: {
6609: ND d;
6610: NM m,m0;
6611: NMV t;
6612: int i,len;
6613:
6614: if ( !p ) return 0;
6615: m0 = 0;
6616: len = p->len;
6617: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6618: NEXTNM(m0,m);
6619: ndl_copy(DL(t),DL(m));
1.6 noro 6620: CZ(m) = CZ(t);
1.1 noro 6621: }
6622: NEXT(m) = 0;
6623: MKND(NV(p),m0,len,d);
6624: SG(d) = SG(p);
1.28 noro 6625: d->sig = p->sig;
1.1 noro 6626: return d;
6627: }
6628:
6629: DP ndvtodp(int mod,NDV p)
6630: {
6631: MP m,m0;
6632: DP d;
6633: NMV t;
6634: int i,len;
6635:
6636: if ( !p ) return 0;
6637: m0 = 0;
6638: len = p->len;
6639: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6640: NEXTMP(m0,m);
6641: m->dl = ndltodl(nd_nvar,DL(t));
6642: m->c = (Obj)ndctop(mod,t->c);
6643: }
6644: NEXT(m) = 0;
6645: MKDP(nd_nvar,m0,d);
6646: SG(d) = SG(p);
6647: return d;
6648: }
6649:
1.16 noro 6650: DPM ndvtodpm(int mod,NDV p)
6651: {
6652: DMM m,m0;
6653: DPM d;
6654: NMV t;
6655: int i,len;
6656:
6657: if ( !p ) return 0;
6658: m0 = 0;
6659: len = p->len;
6660: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6661: NEXTDMM(m0,m);
6662: m->dl = ndltodl(nd_nvar,DL(t));
6663: m->c = (Obj)ndctop(mod,t->c);
6664: m->pos = MPOS(DL(t));
6665: }
6666: NEXT(m) = 0;
6667: MKDPM(nd_nvar,m0,d);
6668: SG(d) = SG(p);
6669: return d;
6670: }
6671:
6672:
1.1 noro 6673: DP ndtodp(int mod,ND p)
6674: {
6675: MP m,m0;
6676: DP d;
6677: NM t;
6678: int i,len;
6679:
6680: if ( !p ) return 0;
6681: m0 = 0;
6682: len = p->len;
6683: for ( t = BDY(p); t; t = NEXT(t) ) {
6684: NEXTMP(m0,m);
6685: m->dl = ndltodl(nd_nvar,DL(t));
6686: m->c = (Obj)ndctop(mod,t->c);
6687: }
6688: NEXT(m) = 0;
6689: MKDP(nd_nvar,m0,d);
6690: SG(d) = SG(p);
6691: return d;
6692: }
6693:
6694: void ndv_print(NDV p)
6695: {
6696: NMV m;
6697: int i,len;
6698:
6699: if ( !p ) printf("0\n");
6700: else {
6701: len = LEN(p);
6702: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6703: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 6704: else printf("+%ld*",CM(m));
1.1 noro 6705: ndl_print(DL(m));
6706: }
6707: printf("\n");
6708: }
6709: }
6710:
6711: void ndv_print_q(NDV p)
6712: {
6713: NMV m;
6714: int i,len;
6715:
6716: if ( !p ) printf("0\n");
6717: else {
6718: len = LEN(p);
6719: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6720: printf("+");
1.6 noro 6721: printexpr(CO,(Obj)CZ(m));
1.1 noro 6722: printf("*");
6723: ndl_print(DL(m));
6724: }
6725: printf("\n");
6726: }
6727: }
6728:
6729: NODE ndv_reducebase(NODE x,int *perm)
6730: {
6731: int len,i,j;
6732: NDVI w;
6733: NODE t,t0;
6734:
6735: len = length(x);
6736: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6737: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6738: w[i].p = BDY(t); w[i].i = perm[i];
6739: }
6740: for ( i = 0; i < len; i++ ) {
6741: for ( j = 0; j < i; j++ ) {
6742: if ( w[i].p && w[j].p ) {
6743: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6744: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6745: }
6746: }
6747: }
6748: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6749: if ( w[i].p ) {
6750: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6751: perm[j++] = w[i].i;
6752: }
6753: }
6754: NEXT(t) = 0; x = t0;
6755: return x;
6756: }
6757:
6758: /* XXX incomplete */
6759:
1.21 noro 6760: extern DMMstack dmm_stack;
6761: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6762:
1.1 noro 6763: void nd_init_ord(struct order_spec *ord)
6764: {
6765: nd_module = (ord->id >= 256);
6766: if ( nd_module ) {
6767: nd_dcomp = -1;
1.21 noro 6768: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6769: nd_pot_nelim = ord->pot_nelim;
6770: nd_poly_weight_len = ord->nv;
6771: nd_poly_weight = ord->top_weight;
6772: nd_module_rank = ord->module_rank;
6773: nd_module_weight = ord->module_top_weight;
6774: }
6775: nd_matrix = 0;
6776: nd_matrix_len = 0;
6777: switch ( ord->id ) {
6778: case 0:
6779: switch ( ord->ord.simple ) {
6780: case 0:
6781: nd_dcomp = 1;
6782: nd_isrlex = 1;
6783: break;
6784: case 1:
6785: nd_dcomp = 1;
6786: nd_isrlex = 0;
6787: break;
6788: case 2:
6789: nd_dcomp = 0;
6790: nd_isrlex = 0;
6791: ndl_compare_function = ndl_lex_compare;
6792: break;
6793: case 11:
6794: /* XXX */
6795: nd_dcomp = 0;
6796: nd_isrlex = 1;
6797: ndl_compare_function = ndl_ww_lex_compare;
6798: break;
6799: default:
6800: error("nd_gr : unsupported order");
6801: }
6802: break;
6803: case 1:
6804: /* block order */
6805: /* XXX */
6806: nd_dcomp = -1;
6807: nd_isrlex = 0;
6808: ndl_compare_function = ndl_block_compare;
6809: break;
6810: case 2:
6811: /* matrix order */
6812: /* XXX */
6813: nd_dcomp = -1;
6814: nd_isrlex = 0;
6815: nd_matrix_len = ord->ord.matrix.row;
6816: nd_matrix = ord->ord.matrix.matrix;
6817: ndl_compare_function = ndl_matrix_compare;
6818: break;
6819: case 3:
6820: /* composite order */
6821: nd_dcomp = -1;
6822: nd_isrlex = 0;
6823: nd_worb_len = ord->ord.composite.length;
6824: nd_worb = ord->ord.composite.w_or_b;
6825: ndl_compare_function = ndl_composite_compare;
6826: break;
6827:
6828: /* module order */
6829: case 256:
6830: switch ( ord->ord.simple ) {
6831: case 0:
1.21 noro 6832: nd_dcomp = 0;
1.1 noro 6833: nd_isrlex = 1;
1.21 noro 6834: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 6835: break;
6836: case 1:
1.21 noro 6837: nd_dcomp = 0;
1.1 noro 6838: nd_isrlex = 0;
6839: ndl_compare_function = ndl_module_glex_compare;
6840: break;
6841: case 2:
1.21 noro 6842: nd_dcomp = 0;
1.1 noro 6843: nd_isrlex = 0;
1.21 noro 6844: ndl_compare_function = ndl_module_compare;
6845: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 6846: break;
6847: default:
1.21 noro 6848: error("nd_init_ord : unsupported order");
1.1 noro 6849: }
6850: break;
6851: case 257:
6852: /* block order */
6853: nd_isrlex = 0;
1.21 noro 6854: ndl_compare_function = ndl_module_compare;
6855: ndl_base_compare_function = ndl_block_compare;
1.1 noro 6856: break;
6857: case 258:
6858: /* matrix order */
6859: nd_isrlex = 0;
6860: nd_matrix_len = ord->ord.matrix.row;
6861: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 6862: ndl_compare_function = ndl_module_compare;
6863: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 6864: break;
6865: case 259:
6866: /* composite order */
6867: nd_isrlex = 0;
6868: nd_worb_len = ord->ord.composite.length;
6869: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 6870: ndl_compare_function = ndl_module_compare;
6871: ndl_base_compare_function = ndl_composite_compare;
6872: break;
6873: case 300:
6874: /* schreyer order */
6875: if ( ord->base->id != 256 )
6876: error("nd_init_ord : unsupported base order");
6877: ndl_compare_function = ndl_module_schreyer_compare;
6878: dmm_stack = ord->dmmstack;
6879: switch ( ord->base->ord.simple ) {
6880: case 0:
6881: nd_isrlex = 1;
6882: ndl_base_compare_function = ndl_glex_compare;
6883: dl_base_compare_function = cmpdl_revgradlex;
6884: break;
6885: case 1:
6886: nd_isrlex = 0;
6887: ndl_base_compare_function = ndl_glex_compare;
6888: dl_base_compare_function = cmpdl_gradlex;
6889: break;
6890: case 2:
6891: nd_isrlex = 0;
6892: ndl_base_compare_function = ndl_lex_compare;
6893: dl_base_compare_function = cmpdl_lex;
6894: break;
6895: default:
6896: error("nd_init_ord : unsupported order");
6897: }
1.1 noro 6898: break;
6899: }
6900: nd_ord = ord;
6901: }
6902:
6903: BlockMask nd_create_blockmask(struct order_spec *ord)
6904: {
6905: int n,i,j,s,l;
6906: UINT *t;
6907: BlockMask bm;
6908:
6909: /* we only create mask table for block order */
6910: if ( ord->id != 1 && ord->id != 257 )
6911: return 0;
6912: n = ord->ord.block.length;
6913: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
6914: bm->n = n;
6915: bm->order_pair = ord->ord.block.order_pair;
6916: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
6917: for ( i = 0, s = 0; i < n; i++ ) {
6918: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
6919: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
6920: l = bm->order_pair[i].length;
6921: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
6922: }
6923: return bm;
6924: }
6925:
6926: EPOS nd_create_epos(struct order_spec *ord)
6927: {
6928: int i,j,l,s,ord_l,ord_o;
6929: EPOS epos;
6930: struct order_pair *op;
6931:
6932: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
6933: switch ( ord->id ) {
1.21 noro 6934: case 0: case 256: case 300:
1.1 noro 6935: if ( nd_isrlex ) {
6936: for ( i = 0; i < nd_nvar; i++ ) {
6937: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
6938: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
6939: }
6940: } else {
6941: for ( i = 0; i < nd_nvar; i++ ) {
6942: epos[i].i = nd_exporigin + i/nd_epw;
6943: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6944: }
6945: }
6946: break;
6947: case 1: case 257:
6948: /* block order */
6949: l = ord->ord.block.length;
6950: op = ord->ord.block.order_pair;
6951: for ( j = 0, s = 0; j < l; j++ ) {
6952: ord_o = op[j].order;
6953: ord_l = op[j].length;
6954: if ( !ord_o )
6955: for ( i = 0; i < ord_l; i++ ) {
6956: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
6957: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
6958: }
6959: else
6960: for ( i = 0; i < ord_l; i++ ) {
6961: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
6962: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
6963: }
6964: s += ord_l;
6965: }
6966: break;
6967: case 2:
6968: /* matrix order */
6969: case 3:
6970: /* composite order */
6971: default:
6972: for ( i = 0; i < nd_nvar; i++ ) {
6973: epos[i].i = nd_exporigin + i/nd_epw;
6974: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6975: }
6976: break;
6977: }
6978: return epos;
6979: }
6980:
6981: /* external interface */
6982:
6983: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
6984: {
6985: NODE t,in0,in;
6986: ND ndf,nf;
6987: NDV ndvf;
6988: VL vv,tv;
6989: int stat,nvar,max,mrank;
6990: union oNDC dn;
6991: Q cont;
6992: P pp;
6993: LIST ppl;
6994:
6995: if ( !f ) {
6996: *rp = 0;
6997: return;
6998: }
6999: pltovl(v,&vv);
7000: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
7001:
7002: /* max=65536 implies nd_bpe=32 */
7003: max = 65536;
7004:
7005: nd_module = 0;
7006: /* nd_module will be set if ord is a module ordering */
7007: nd_init_ord(ord);
7008: nd_setup_parameters(nvar,max);
7009: if ( nd_module && OID(f) != O_LIST )
7010: error("nd_nf_p : the first argument must be a list");
7011: if ( nd_module ) mrank = length(BDY((LIST)f));
7012: /* conversion to ndv */
7013: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7014: NEXTNODE(in0,in);
7015: if ( nd_module ) {
7016: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7017: || length(BDY((LIST)BDY(t))) != mrank )
7018: error("nd_nf_p : inconsistent basis element");
7019: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7020: else ppl = (LIST)BDY(t);
7021: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7022: } else {
7023: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7024: else pp = (P)BDY(t);
7025: BDY(in) = (pointer)ptondv(CO,vv,pp);
7026: }
7027: if ( m ) ndv_mod(m,(NDV)BDY(in));
7028: }
7029: if ( in0 ) NEXT(in) = 0;
7030:
7031: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7032: else ndvf = ptondv(CO,vv,(P)f);
7033: if ( m ) ndv_mod(m,ndvf);
7034: ndf = (pointer)ndvtond(m,ndvf);
7035:
7036: /* dont sort, dont removecont */
1.24 noro 7037: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7038: nd_scale=2;
1.6 noro 7039: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7040: if ( !stat )
7041: error("nd_nf_p : exponent too large");
7042: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7043: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7044: }
7045:
7046: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7047: {
7048: NM m;
7049: UINT *t,*s;
7050: int i;
7051:
7052: for ( i = 0; i < n; i++ ) r[i] = 0;
7053: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7054: t = DL(m);
7055: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7056: r[i] = CM(m);
7057: }
7058: for ( i = 0; !r[i]; i++ );
7059: return i;
7060: }
7061:
7062: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7063: {
7064: NM m;
7065: UINT *t,*s;
7066: int i;
7067:
7068: for ( i = 0; i < n; i++ ) r[i] = 0;
7069: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7070: t = DL(m);
7071: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7072: r[i] = CZ(m);
1.1 noro 7073: }
7074: for ( i = 0; !r[i]; i++ );
7075: return i;
7076: }
7077:
7078: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7079: {
7080: NM m;
7081: UINT *t,*s;
7082: int i;
7083:
7084: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7085: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7086: t = DL(m);
7087: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7088: mpz_set(r[i],BDY(CZ(m)));
7089: }
7090: for ( i = 0; !mpz_sgn(r[i]); i++ );
7091: return i;
7092: }
7093:
7094: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7095: {
7096: NM m;
7097: unsigned long *v;
7098: int i,j,h,size;
7099: UINT *s,*t;
7100:
7101: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7102: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7103: bzero(v,size);
7104: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7105: t = DL(m);
7106: h = ndl_hash_value(t);
7107: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7108: v[i/BLEN] |= 1L <<(i%BLEN);
7109: }
7110: return v;
7111: }
7112:
7113: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7114: {
7115: NMV mr;
7116: UINT *d,*t,*s;
7117: int i,j,len,h,head;
7118:
7119: d = DL(m);
7120: len = LEN(p);
7121: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7122: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7123: ndl_add(d,DL(mr),t);
7124: h = ndl_hash_value(t);
7125: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7126: if ( j == 0 ) head = i;
7127: v[i/BLEN] |= 1L <<(i%BLEN);
7128: }
7129: return head;
7130: }
7131:
7132: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7133: {
7134: NM m;
7135: NMV mr;
7136: UINT *d,*t,*s;
7137: NDV p;
7138: int i,j,len;
7139: Z *r;
7140:
7141: m = pair->mul;
7142: d = DL(m);
7143: p = nd_ps[pair->index];
7144: len = LEN(p);
7145: r = (Z *)CALLOC(n,sizeof(Q));
7146: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7147: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7148: ndl_add(d,DL(mr),t);
7149: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7150: r[i] = CZ(mr);
1.1 noro 7151: }
7152: return r;
7153: }
7154:
1.11 noro 7155: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7156: {
7157: NM m;
7158: NMV mr;
1.11 noro 7159: UINT *d,*t,*s,*u;
1.1 noro 7160: NDV p;
7161: unsigned char *ivc;
7162: unsigned short *ivs;
7163: UINT *v,*ivi,*s0v;
1.11 noro 7164: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7165: IndArray r;
7166:
7167: m = pair->mul;
7168: d = DL(m);
7169: if ( trace )
7170: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7171: else
7172: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7173:
7174: len = LEN(p);
7175: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7176: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7177: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7178: ndl_add(d,DL(mr),t);
7179: st = prev;
7180: ed = n;
7181: while ( ed > st ) {
7182: md = (st+ed)/2;
7183: u = s0+md*nd_wpd;
7184: c = DL_COMPARE(u,t);
7185: if ( c == 0 ) break;
7186: else if ( c > 0 ) st = md;
7187: else ed = md;
7188: }
7189: prev = v[j] = md;
1.1 noro 7190: }
7191: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7192: r->head = v[0];
7193: diff = 0;
7194: for ( i = 1; i < len; i++ ) {
7195: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7196: }
7197: if ( diff < 256 ) {
7198: r->width = 1;
7199: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7200: r->index.c = ivc;
7201: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7202: } else if ( diff < 65536 ) {
7203: r->width = 2;
7204: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7205: r->index.s = ivs;
7206: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7207: } else {
7208: r->width = 4;
7209: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7210: r->index.i = ivi;
7211: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7212: }
7213: return r;
7214: }
7215:
7216: int compress_array(Z *svect,Z *cvect,int n)
7217: {
7218: int i,j;
7219:
7220: for ( i = j = 0; i < n; i++ )
7221: if ( svect[i] ) cvect[j++] = svect[i];
7222: return j;
7223: }
7224:
7225: void expand_array(Z *svect,Z *cvect,int n)
7226: {
7227: int i,j;
7228:
7229: for ( i = j = 0; j < n; i++ )
7230: if ( svect[i] ) svect[i] = cvect[j++];
7231: }
7232:
1.8 noro 7233: #if 0
1.1 noro 7234: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7235: {
7236: int i,j,k,len,pos,prev,nz;
7237: Z cs,mcs,c1,c2,cr,gcd,t;
7238: IndArray ivect;
7239: unsigned char *ivc;
7240: unsigned short *ivs;
7241: unsigned int *ivi;
7242: NDV redv;
7243: NMV mr;
7244: NODE rp;
7245: int maxrs;
7246: double hmag;
7247: Z *cvect;
1.3 noro 7248: int l;
1.1 noro 7249:
7250: maxrs = 0;
7251: for ( i = 0; i < col && !svect[i]; i++ );
7252: if ( i == col ) return maxrs;
7253: hmag = p_mag((P)svect[i])*nd_scale;
7254: cvect = (Z *)MALLOC(col*sizeof(Q));
7255: for ( i = 0; i < nred; i++ ) {
7256: ivect = imat[i];
7257: k = ivect->head;
7258: if ( svect[k] ) {
7259: maxrs = MAX(maxrs,rp0[i]->sugar);
7260: redv = nd_demand?ndv_load(rp0[i]->index)
7261: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7262: len = LEN(redv); mr = BDY(redv);
1.6 noro 7263: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7264: chsgnz(cs,&mcs);
7265: if ( !UNIQ(cr) ) {
7266: for ( j = 0; j < col; j++ ) {
7267: mulz(svect[j],cr,&c1); svect[j] = c1;
7268: }
7269: }
7270: svect[k] = 0; prev = k;
7271: switch ( ivect->width ) {
7272: case 1:
7273: ivc = ivect->index.c;
7274: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7275: pos = prev+ivc[j]; prev = pos;
1.6 noro 7276: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7277: }
7278: break;
7279: case 2:
7280: ivs = ivect->index.s;
7281: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7282: pos = prev+ivs[j]; prev = pos;
1.6 noro 7283: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7284: }
7285: break;
7286: case 4:
7287: ivi = ivect->index.i;
7288: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7289: pos = prev+ivi[j]; prev = pos;
1.6 noro 7290: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7291: }
7292: break;
7293: }
7294: for ( j = k+1; j < col && !svect[j]; j++ );
7295: if ( j == col ) break;
7296: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7297: nz = compress_array(svect,cvect,col);
7298: removecont_array((P *)cvect,nz,1);
7299: expand_array(svect,cvect,nz);
7300: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7301: }
7302: }
7303: }
7304: nz = compress_array(svect,cvect,col);
7305: removecont_array((P *)cvect,nz,1);
7306: expand_array(svect,cvect,nz);
7307: if ( DP_Print ) {
7308: fprintf(asir_out,"-"); fflush(asir_out);
7309: }
7310: return maxrs;
7311: }
1.4 noro 7312: #else
1.9 noro 7313:
1.4 noro 7314: /* direct mpz version */
7315: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7316: {
7317: int i,j,k,len,pos,prev;
7318: mpz_t cs,cr,gcd;
7319: IndArray ivect;
7320: unsigned char *ivc;
7321: unsigned short *ivs;
7322: unsigned int *ivi;
7323: NDV redv;
7324: NMV mr;
7325: NODE rp;
7326: int maxrs;
7327: double hmag;
7328: int l;
1.13 noro 7329: static mpz_t *svect;
7330: static int svect_len=0;
1.4 noro 7331:
7332: maxrs = 0;
7333: for ( i = 0; i < col && !svect0[i]; i++ );
7334: if ( i == col ) return maxrs;
7335: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7336: if ( col > svect_len ) {
7337: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7338: svect_len = col;
7339: }
1.4 noro 7340: for ( i = 0; i < col; i++ ) {
7341: mpz_init(svect[i]);
7342: if ( svect0[i] )
7343: mpz_set(svect[i],BDY(svect0[i]));
7344: else
7345: mpz_set_ui(svect[i],0);
7346: }
7347: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7348: for ( i = 0; i < nred; i++ ) {
7349: ivect = imat[i];
7350: k = ivect->head;
7351: if ( mpz_sgn(svect[k]) ) {
7352: maxrs = MAX(maxrs,rp0[i]->sugar);
7353: redv = nd_demand?ndv_load(rp0[i]->index)
7354: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7355: len = LEN(redv); mr = BDY(redv);
1.6 noro 7356: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7357: mpz_div(cs,svect[k],gcd);
1.6 noro 7358: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7359: mpz_neg(cs,cs);
1.9 noro 7360: if ( MUNIMPZ(cr) )
7361: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7362: else if ( !UNIMPZ(cr) )
7363: for ( j = 0; j < col; j++ ) {
7364: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7365: }
1.4 noro 7366: mpz_set_ui(svect[k],0);
7367: prev = k;
7368: switch ( ivect->width ) {
7369: case 1:
7370: ivc = ivect->index.c;
7371: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7372: pos = prev+ivc[j]; prev = pos;
1.6 noro 7373: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7374: }
7375: break;
7376: case 2:
7377: ivs = ivect->index.s;
7378: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7379: pos = prev+ivs[j]; prev = pos;
1.6 noro 7380: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7381: }
7382: break;
7383: case 4:
7384: ivi = ivect->index.i;
7385: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7386: pos = prev+ivi[j]; prev = pos;
1.6 noro 7387: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7388: }
7389: break;
7390: }
7391: for ( j = k+1; j < col && !svect[j]; j++ );
7392: if ( j == col ) break;
7393: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7394: mpz_removecont_array(svect,col);
7395: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7396: }
7397: }
7398: }
7399: mpz_removecont_array(svect,col);
7400: if ( DP_Print ) {
7401: fprintf(asir_out,"-"); fflush(asir_out);
7402: }
7403: for ( i = 0; i < col; i++ )
7404: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7405: else svect0[i] = 0;
7406: return maxrs;
7407: }
7408: #endif
1.1 noro 7409:
7410: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7411: {
7412: int i,j,k,len,pos,prev;
7413: UINT c,c1,c2,c3,up,lo,dmy;
7414: IndArray ivect;
7415: unsigned char *ivc;
7416: unsigned short *ivs;
7417: unsigned int *ivi;
7418: NDV redv;
7419: NMV mr;
7420: NODE rp;
7421: int maxrs;
7422:
7423: maxrs = 0;
7424: for ( i = 0; i < nred; i++ ) {
7425: ivect = imat[i];
7426: k = ivect->head; svect[k] %= m;
7427: if ( (c = svect[k]) != 0 ) {
7428: maxrs = MAX(maxrs,rp0[i]->sugar);
7429: c = m-c; redv = nd_ps[rp0[i]->index];
7430: len = LEN(redv); mr = BDY(redv);
7431: svect[k] = 0; prev = k;
7432: switch ( ivect->width ) {
7433: case 1:
7434: ivc = ivect->index.c;
7435: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7436: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
7437: if ( c1 ) {
7438: c2 = svect[pos];
7439: DMA(c1,c,c2,up,lo);
7440: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7441: } else svect[pos] = lo;
7442: }
7443: }
7444: break;
7445: case 2:
7446: ivs = ivect->index.s;
7447: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7448: pos = prev+ivs[j]; c1 = CM(mr);
7449: prev = pos;
7450: if ( c1 ) {
7451: c2 = svect[pos];
7452: DMA(c1,c,c2,up,lo);
7453: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7454: } else svect[pos] = lo;
7455: }
7456: }
7457: break;
7458: case 4:
7459: ivi = ivect->index.i;
7460: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7461: pos = prev+ivi[j]; c1 = CM(mr);
7462: prev = pos;
7463: if ( c1 ) {
7464: c2 = svect[pos];
7465: DMA(c1,c,c2,up,lo);
7466: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7467: } else svect[pos] = lo;
7468: }
7469: }
7470: break;
7471: }
7472: }
7473: }
7474: for ( i = 0; i < col; i++ )
7475: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7476: return maxrs;
7477: }
7478:
7479: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7480: {
7481: int i,j,k,len,pos,prev;
7482: UINT c,c1,c2,c3,up,lo,dmy;
7483: IndArray ivect;
7484: unsigned char *ivc;
7485: unsigned short *ivs;
7486: unsigned int *ivi;
7487: NDV redv;
7488: NMV mr;
7489: NODE rp;
7490: int maxrs;
7491:
7492: maxrs = 0;
7493: for ( i = 0; i < nred; i++ ) {
7494: ivect = imat[i];
7495: k = ivect->head;
7496: if ( (c = svect[k]) != 0 ) {
7497: maxrs = MAX(maxrs,rp0[i]->sugar);
7498: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7499: len = LEN(redv); mr = BDY(redv);
7500: svect[k] = 0; prev = k;
7501: switch ( ivect->width ) {
7502: case 1:
7503: ivc = ivect->index.c;
7504: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7505: pos = prev+ivc[j]; prev = pos;
7506: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7507: }
7508: break;
7509: case 2:
7510: ivs = ivect->index.s;
7511: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7512: pos = prev+ivs[j]; prev = pos;
7513: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7514: }
7515: break;
7516: case 4:
7517: ivi = ivect->index.i;
7518: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7519: pos = prev+ivi[j]; prev = pos;
7520: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7521: }
7522: break;
7523: }
7524: }
7525: }
7526: return maxrs;
7527: }
7528:
7529: ND nd_add_lf(ND p1,ND p2)
7530: {
7531: int n,c,can;
7532: ND r;
7533: NM m1,m2,mr0,mr,s;
7534: Z t;
7535:
7536: if ( !p1 ) return p2;
7537: else if ( !p2 ) return p1;
7538: else {
7539: can = 0;
7540: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7541: c = DL_COMPARE(DL(m1),DL(m2));
7542: switch ( c ) {
7543: case 0:
7544: addlf(CZ(m1),CZ(m2),&t);
7545: s = m1; m1 = NEXT(m1);
7546: if ( t ) {
7547: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7548: } else {
7549: can += 2; FREENM(s);
7550: }
7551: s = m2; m2 = NEXT(m2); FREENM(s);
7552: break;
7553: case 1:
7554: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7555: break;
7556: case -1:
7557: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7558: break;
7559: }
7560: }
7561: if ( !mr0 )
7562: if ( m1 ) mr0 = m1;
7563: else if ( m2 ) mr0 = m2;
7564: else return 0;
7565: else if ( m1 ) NEXT(mr) = m1;
7566: else if ( m2 ) NEXT(mr) = m2;
7567: else NEXT(mr) = 0;
7568: BDY(p1) = mr0;
7569: SG(p1) = MAX(SG(p1),SG(p2));
7570: LEN(p1) = LEN(p1)+LEN(p2)-can;
7571: FREEND(p2);
7572: return p1;
7573: }
7574: }
7575:
7576: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7577: {
7578: int i,j,k,len,pos,prev;
7579: mpz_t c,mc,c1;
7580: IndArray ivect;
7581: unsigned char *ivc;
7582: unsigned short *ivs;
7583: unsigned int *ivi;
7584: NDV redv;
7585: NMV mr;
7586: NODE rp;
7587: int maxrs;
7588:
7589: maxrs = 0;
7590: lf_lazy = 1;
7591: for ( i = 0; i < nred; i++ ) {
7592: ivect = imat[i];
7593: k = ivect->head;
7594: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7595: if ( mpz_sgn(svect[k]) ) {
7596: maxrs = MAX(maxrs,rp0[i]->sugar);
7597: mpz_neg(svect[k],svect[k]);
7598: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7599: len = LEN(redv); mr = BDY(redv);
7600: prev = k;
7601: switch ( ivect->width ) {
7602: case 1:
7603: ivc = ivect->index.c;
7604: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7605: pos = prev+ivc[j]; prev = pos;
7606: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7607: }
7608: break;
7609: case 2:
7610: ivs = ivect->index.s;
7611: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7612: pos = prev+ivs[j]; prev = pos;
7613: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7614: }
7615: break;
7616: case 4:
7617: ivi = ivect->index.i;
7618: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7619: pos = prev+ivi[j]; prev = pos;
7620: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7621: }
7622: break;
7623: }
7624: mpz_set_ui(svect[k],0);
7625: }
7626: }
7627: lf_lazy=0;
7628: for ( i = 0; i < col; i++ ) {
7629: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7630: }
7631: return maxrs;
7632: }
7633:
7634: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7635: {
7636: int i,j,k,l,rank,s;
7637: mpz_t a,a1,inv;
7638: mpz_t *t,*pivot,*pk;
7639: mpz_t **mat;
7640: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7641: int size,size1;
7642:
7643: mpz_init(inv);
7644: mpz_init(a);
7645: mat = (mpz_t **)mat0;
7646: size = 0;
7647: for ( rank = 0, j = 0; j < col; j++ ) {
7648: for ( i = rank; i < row; i++ ) {
7649: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7650: }
7651: for ( i = rank; i < row; i++ )
7652: if ( mpz_sgn(mat[i][j]) )
7653: break;
7654: if ( i == row ) {
7655: colstat[j] = 0;
7656: continue;
7657: } else
7658: colstat[j] = 1;
7659: if ( i != rank ) {
7660: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7661: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7662: }
7663: pivot = mat[rank];
7664: s = sugar[rank];
7665: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7666: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7667: if ( mpz_sgn(*pk) ) {
7668: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7669: }
7670: for ( i = rank+1; i < row; i++ ) {
7671: t = mat[i];
7672: if ( mpz_sgn(t[j]) ) {
7673: sugar[i] = MAX(sugar[i],s);
7674: mpz_neg(a,t[j]);
7675: red_by_vect_lf(t+j,pivot+j,a,col-j);
7676: }
7677: }
7678: rank++;
7679: }
7680: for ( j = col-1, l = rank-1; j >= 0; j-- )
7681: if ( colstat[j] ) {
7682: pivot = mat[l];
7683: s = sugar[l];
7684: for ( k = j; k < col; k++ )
7685: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7686: for ( i = 0; i < l; i++ ) {
7687: t = mat[i];
7688: if ( mpz_sgn(t[j]) ) {
7689: sugar[i] = MAX(sugar[i],s);
7690: mpz_neg(a,t[j]);
7691: red_by_vect_lf(t+j,pivot+j,a,col-j);
7692: }
7693: }
7694: l--;
7695: }
7696: for ( j = 0, l = 0; l < rank; j++ )
7697: if ( colstat[j] ) {
7698: t = mat[l];
7699: for ( k = j; k < col; k++ ) {
7700: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7701: }
7702: l++;
7703: }
7704: return rank;
7705: }
7706:
7707:
7708: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7709: {
7710: int j,k,len;
7711: UINT *p;
7712: UINT c;
7713: NDV r;
7714: NMV mr0,mr;
7715:
7716: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7717: if ( !len ) return 0;
7718: else {
7719: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7720: #if 0
7721: ndv_alloc += nmv_adv*len;
7722: #endif
7723: mr = mr0;
7724: p = s0vect;
7725: for ( j = k = 0; j < col; j++, p += nd_wpd )
7726: if ( !rhead[j] ) {
7727: if ( (c = vect[k++]) != 0 ) {
7728: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7729: }
7730: }
7731: MKNDV(nd_nvar,mr0,len,r);
7732: return r;
7733: }
7734: }
7735:
7736: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7737: {
7738: int j,k,len;
7739: UINT *p;
7740: NDV r;
7741: NMV mr0,mr;
7742:
7743: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7744: if ( !len ) return 0;
7745: else {
7746: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7747: mr = mr0;
7748: p = s0vect;
7749: for ( j = 0; j < col; j++, p += nd_wpd )
7750: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7751: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7752: }
7753: MKNDV(nd_nvar,mr0,len,r);
7754: return r;
7755: }
7756: }
7757:
7758: /* for preprocessed vector */
7759:
7760: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7761: {
1.6 noro 7762: int j,k,len;
7763: UINT *p;
7764: Z c;
7765: NDV r;
7766: NMV mr0,mr;
1.1 noro 7767:
1.6 noro 7768: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7769: if ( !len ) return 0;
7770: else {
7771: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7772: #if 0
1.6 noro 7773: ndv_alloc += nmv_adv*len;
1.1 noro 7774: #endif
1.6 noro 7775: mr = mr0;
7776: p = s0vect;
7777: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7778: if ( !rhead[j] ) {
7779: if ( (c = vect[k++]) != 0 ) {
7780: if ( !INT(c) )
7781: error("vect_to_ndv_q : components must be integers");
7782: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7783: }
7784: }
1.1 noro 7785: }
1.6 noro 7786: MKNDV(nd_nvar,mr0,len,r);
7787: return r;
7788: }
1.1 noro 7789: }
7790:
7791: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
7792: {
7793: int j,k,len;
7794: UINT *p;
7795: mpz_t c;
7796: NDV r;
7797: NMV mr0,mr;
7798:
7799: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
7800: if ( !len ) return 0;
7801: else {
7802: mr0 = (NMV)MALLOC(nmv_adv*len);
7803: #if 0
7804: ndv_alloc += nmv_adv*len;
7805: #endif
7806: mr = mr0;
7807: p = s0vect;
7808: for ( j = k = 0; j < col; j++, p += nd_wpd )
7809: if ( !rhead[j] ) {
7810: c[0] = vect[k++][0];
7811: if ( mpz_sgn(c) ) {
7812: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
7813: }
7814: }
7815: MKNDV(nd_nvar,mr0,len,r);
7816: return r;
7817: }
7818: }
7819:
7820: /* for plain vector */
7821:
7822: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
7823: {
7824: int j,k,len;
7825: UINT *p;
7826: Z c;
7827: NDV r;
7828: NMV mr0,mr;
7829:
7830: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7831: if ( !len ) return 0;
7832: else {
7833: mr0 = (NMV)MALLOC(nmv_adv*len);
7834: #if 0
7835: ndv_alloc += nmv_adv*len;
7836: #endif
7837: mr = mr0;
7838: p = s0vect;
7839: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
7840: if ( (c = vect[k]) != 0 ) {
7841: if ( !INT(c) )
1.6 noro 7842: error("plain_vect_to_ndv_q : components must be integers");
7843: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 7844: }
7845: MKNDV(nd_nvar,mr0,len,r);
7846: return r;
7847: }
7848: }
7849:
7850: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
7851: {
7852: ND_pairs t;
7853: NODE sp0,sp;
7854: int stat;
7855: ND spol;
7856:
7857: for ( t = l; t; t = NEXT(t) ) {
7858: stat = nd_sp(m,trace,t,&spol);
7859: if ( !stat ) return 0;
7860: if ( spol ) {
7861: add_pbucket_symbolic(bucket,spol);
7862: }
7863: }
7864: return 1;
7865: }
7866:
7867: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
7868: {
7869: NODE rp0,rp;
7870: NM mul,head,s0,s;
7871: int index,col,i,sugar;
7872: RHist h;
7873: UINT *s0v,*p;
7874: NM_ind_pair pair;
7875: ND red;
7876: NDV *ps;
7877:
7878: s0 = 0; rp0 = 0; col = 0;
7879: if ( nd_demand )
7880: ps = trace?nd_ps_trace_sym:nd_ps_sym;
7881: else
7882: ps = trace?nd_ps_trace:nd_ps;
7883: while ( 1 ) {
7884: head = remove_head_pbucket_symbolic(bucket);
7885: if ( !head ) break;
7886: if ( !s0 ) s0 = head;
7887: else NEXT(s) = head;
7888: s = head;
7889: index = ndl_find_reducer(DL(head));
7890: if ( index >= 0 ) {
7891: h = nd_psh[index];
7892: NEWNM(mul);
7893: ndl_sub(DL(head),DL(h),DL(mul));
7894: if ( ndl_check_bound2(index,DL(mul)) )
7895: return 0;
7896: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 7897: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 7898: red = ndv_mul_nm_symbolic(mul,ps[index]);
7899: add_pbucket_symbolic(bucket,nd_remove_head(red));
7900: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
7901: }
7902: col++;
7903: }
7904: if ( rp0 ) NEXT(rp) = 0;
7905: NEXT(s) = 0;
7906: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
7907: for ( i = 0, p = s0v, s = s0; i < col;
7908: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
7909: *s0vect = s0v;
7910: *r = rp0;
7911: return col;
7912: }
7913:
7914: void print_ndp(ND_pairs l)
7915: {
7916: ND_pairs t;
7917:
7918: for ( t = l; t; t = NEXT(t) )
7919: printf("[%d,%d] ",t->i1,t->i2);
7920: printf("\n");
7921: }
7922:
7923: NODE nd_f4(int m,int checkonly,int **indp)
7924: {
7925: int i,nh,stat,index,f4red;
7926: NODE r,g,tn0,tn,node;
7927: ND_pairs d,l,t,ll0,ll,lh;
7928: LIST l0,l1;
7929: ND spol,red;
7930: NDV nf,redv;
7931: NM s0,s;
7932: NODE rp0,srp0,nflist,nzlist,nzlist_t;
7933: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
7934: UINT c;
7935: UINT **spmat;
7936: UINT *s0vect,*svect,*p,*v;
7937: int *colstat;
7938: IndArray *imat;
7939: int *rhead;
7940: int spcol,sprow;
7941: int sugar,sugarh;
7942: PGeoBucket bucket;
7943: struct oEGT eg0,eg1,eg_f4;
7944: Z i1,i2,sugarq;
1.12 noro 7945:
7946: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 7947: #if 0
7948: ndv_alloc = 0;
7949: #endif
1.11 noro 7950: Nf4_red=0;
1.1 noro 7951: g = 0; d = 0;
7952: for ( i = 0; i < nd_psn; i++ ) {
7953: d = update_pairs(d,g,i,0);
7954: g = update_base(g,i);
7955: }
7956: nzlist = 0;
7957: nzlist_t = nd_nzlist;
7958: f4red = 1;
7959: nd_last_nonzero = 0;
7960: while ( d ) {
7961: get_eg(&eg0);
7962: l = nd_minsugarp(d,&d);
7963: sugar = nd_sugarweight?l->sugar2:SG(l);
7964: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7965: if ( nzlist_t ) {
7966: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 7967: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 7968: tn = BDY((LIST)ARG1(node));
7969: if ( !tn ) {
7970: nzlist_t = NEXT(nzlist_t);
7971: continue;
7972: }
7973: /* tn = [[i1,i2],...] */
7974: lh = nd_ipairtospair(tn);
7975: }
7976: bucket = create_pbucket();
7977: stat = nd_sp_f4(m,0,l,bucket);
7978: if ( !stat ) {
7979: for ( t = l; NEXT(t); t = NEXT(t) );
7980: NEXT(t) = d; d = l;
7981: d = nd_reconstruct(0,d);
7982: continue;
7983: }
7984: if ( bucket->m < 0 ) continue;
7985: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7986: if ( !col ) {
7987: for ( t = l; NEXT(t); t = NEXT(t) );
7988: NEXT(t) = d; d = l;
7989: d = nd_reconstruct(0,d);
7990: continue;
7991: }
1.12 noro 7992: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 7993: if ( DP_Print )
1.6 noro 7994: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 7995: sugar,eg_f4.exectime);
1.1 noro 7996: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
7997: if ( checkonly && nflist ) return 0;
7998: /* adding new bases */
7999: if ( nflist ) nd_last_nonzero = f4red;
8000: for ( r = nflist; r; r = NEXT(r) ) {
8001: nf = (NDV)BDY(r);
8002: ndv_removecont(m,nf);
8003: if ( !m && nd_nalg ) {
8004: ND nf1;
8005:
8006: nf1 = ndvtond(m,nf);
8007: nd_monic(0,&nf1);
8008: nd_removecont(m,nf1);
8009: nf = ndtondv(m,nf1);
8010: }
1.24 noro 8011: nh = ndv_newps(m,nf,0);
1.1 noro 8012: d = update_pairs(d,g,nh,0);
8013: g = update_base(g,nh);
8014: }
8015: if ( DP_Print ) {
8016: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8017: }
8018: if ( nd_gentrace ) {
8019: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8020: NEXTNODE(tn0,tn);
1.6 noro 8021: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8022: node = mknode(2,i1,i2); MKLIST(l0,node);
8023: BDY(tn) = l0;
8024: }
8025: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8026: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8027: MKNODE(node,l1,nzlist); nzlist = node;
8028: }
8029: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8030: f4red++;
8031: if ( nd_f4red && f4red > nd_f4red ) break;
8032: if ( nd_rank0 && !nflist ) break;
8033: }
8034: if ( nd_gentrace ) {
8035: MKLIST(l0,reverse_node(nzlist));
8036: MKNODE(nd_alltracelist,l0,0);
8037: }
8038: #if 0
8039: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8040: #endif
1.12 noro 8041: if ( DP_Print ) {
8042: fprintf(asir_out,"number of red=%d,",Nf4_red);
8043: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8044: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
8045: }
1.1 noro 8046: conv_ilist(nd_demand,0,g,indp);
8047: return g;
8048: }
8049:
8050: NODE nd_f4_trace(int m,int **indp)
8051: {
8052: int i,nh,stat,index;
8053: NODE r,g;
8054: ND_pairs d,l,l0,t;
8055: ND spol,red;
8056: NDV nf,redv,nfqv,nfv;
8057: NM s0,s;
8058: NODE rp0,srp0,nflist;
8059: int nsp,nred,col,rank,len,k,j,a;
8060: UINT c;
8061: UINT **spmat;
8062: UINT *s0vect,*svect,*p,*v;
8063: int *colstat;
8064: IndArray *imat;
8065: int *rhead;
8066: int spcol,sprow;
8067: int sugar;
8068: PGeoBucket bucket;
8069: struct oEGT eg0,eg1,eg_f4;
8070:
8071: g = 0; d = 0;
8072: for ( i = 0; i < nd_psn; i++ ) {
8073: d = update_pairs(d,g,i,0);
8074: g = update_base(g,i);
8075: }
8076: while ( d ) {
8077: get_eg(&eg0);
8078: l = nd_minsugarp(d,&d);
8079: sugar = SG(l);
8080: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8081: bucket = create_pbucket();
8082: stat = nd_sp_f4(m,0,l,bucket);
8083: if ( !stat ) {
8084: for ( t = l; NEXT(t); t = NEXT(t) );
8085: NEXT(t) = d; d = l;
8086: d = nd_reconstruct(1,d);
8087: continue;
8088: }
8089: if ( bucket->m < 0 ) continue;
8090: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8091: if ( !col ) {
8092: for ( t = l; NEXT(t); t = NEXT(t) );
8093: NEXT(t) = d; d = l;
8094: d = nd_reconstruct(1,d);
8095: continue;
8096: }
8097: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8098: if ( DP_Print )
1.3 noro 8099: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8100: sugar,eg_f4.exectime);
1.1 noro 8101: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8102: if ( !l0 ) continue;
8103: l = l0;
8104:
8105: /* over Q */
8106: bucket = create_pbucket();
8107: stat = nd_sp_f4(0,1,l,bucket);
8108: if ( !stat ) {
8109: for ( t = l; NEXT(t); t = NEXT(t) );
8110: NEXT(t) = d; d = l;
8111: d = nd_reconstruct(1,d);
8112: continue;
8113: }
8114: if ( bucket->m < 0 ) continue;
8115: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8116: if ( !col ) {
8117: for ( t = l; NEXT(t); t = NEXT(t) );
8118: NEXT(t) = d; d = l;
8119: d = nd_reconstruct(1,d);
8120: continue;
8121: }
8122: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8123: /* adding new bases */
8124: for ( r = nflist; r; r = NEXT(r) ) {
8125: nfqv = (NDV)BDY(r);
8126: ndv_removecont(0,nfqv);
1.6 noro 8127: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8128: if ( nd_nalg ) {
8129: ND nf1;
8130:
8131: nf1 = ndvtond(m,nfqv);
8132: nd_monic(0,&nf1);
8133: nd_removecont(0,nf1);
8134: nfqv = ndtondv(0,nf1); nd_free(nf1);
8135: }
8136: nfv = ndv_dup(0,nfqv);
8137: ndv_mod(m,nfv);
8138: ndv_removecont(m,nfv);
1.24 noro 8139: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8140: d = update_pairs(d,g,nh,0);
8141: g = update_base(g,nh);
8142: }
8143: }
8144: #if 0
8145: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8146: #endif
8147: conv_ilist(nd_demand,1,g,indp);
8148: return g;
8149: }
8150:
8151: int rref(matrix mat,int *sugar)
8152: {
8153: int row,col,i,j,k,l,s,wcol,wj;
8154: unsigned long bj;
8155: unsigned long **a;
8156: unsigned long *ai,*ak,*as,*t;
8157: int *pivot;
8158:
8159: row = mat->row;
8160: col = mat->col;
8161: a = mat->a;
8162: wcol = (col+BLEN-1)/BLEN;
8163: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8164: i = 0;
8165: for ( j = 0; j < col; j++ ) {
8166: wj = j/BLEN; bj = 1L<<(j%BLEN);
8167: for ( k = i; k < row; k++ )
8168: if ( a[k][wj] & bj ) break;
8169: if ( k == row ) continue;
8170: pivot[i] = j;
8171: if ( k != i ) {
8172: t = a[i]; a[i] = a[k]; a[k] = t;
8173: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8174: }
8175: ai = a[i];
8176: for ( k = i+1; k < row; k++ ) {
8177: ak = a[k];
8178: if ( ak[wj] & bj ) {
8179: for ( l = wj; l < wcol; l++ )
8180: ak[l] ^= ai[l];
8181: sugar[k] = MAX(sugar[k],sugar[i]);
8182: }
8183: }
8184: i++;
8185: }
8186: for ( k = i-1; k >= 0; k-- ) {
8187: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8188: ak = a[k];
8189: for ( s = 0; s < k; s++ ) {
8190: as = a[s];
8191: if ( as[wj] & bj ) {
8192: for ( l = wj; l < wcol; l++ )
8193: as[l] ^= ak[l];
8194: sugar[s] = MAX(sugar[s],sugar[k]);
8195: }
8196: }
8197: }
8198: return i;
8199: }
8200:
8201: void print_matrix(matrix mat)
8202: {
8203: int row,col,i,j;
8204: unsigned long *ai;
8205:
8206: row = mat->row;
8207: col = mat->col;
8208: printf("%d x %d\n",row,col);
8209: for ( i = 0; i < row; i++ ) {
8210: ai = mat->a[i];
8211: for ( j = 0; j < col; j++ ) {
8212: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8213: else putchar('0');
8214: }
8215: putchar('\n');
8216: }
8217: }
8218:
8219: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8220:
8221: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8222: {
8223: int row,col,wcol,wj,i,j;
8224: unsigned long bj;
8225: unsigned long *ai;
8226: unsigned long **a;
8227: int len;
8228: int *pos;
8229:
8230: row = mat->row;
8231: col = mat->col;
8232: wcol = (col+BLEN-1)/BLEN;
8233: pos = (int *)MALLOC(wcol*sizeof(int));
8234: bzero(pos,wcol*sizeof(int));
8235: for ( i = j = 0; i < wcol; i++ )
8236: if ( v[i] ) pos[j++] = i;;
8237: len = j;
8238: wj = rhead/BLEN;
8239: bj = 1L<<rhead%BLEN;
8240: a = mat->a;
8241: for ( i = 0; i < row; i++ ) {
8242: ai = a[i];
8243: if ( ai[wj]&bj ) {
8244: for ( j = 0; j < len; j++ )
8245: ai[pos[j]] ^= v[pos[j]];
8246: sugar[i] = MAX(sugar[i],rsugar);
8247: }
8248: }
8249: }
8250:
8251: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8252: {
8253: int nsp,nred,i,i0,k,rank,row;
8254: NODE r0,rp;
8255: ND_pairs sp;
8256: ND spol;
8257: NM_ind_pair rt;
8258: int *s0hash;
8259: UINT *s;
8260: int *pivot,*sugar,*head;
8261: matrix mat;
8262: NM m;
8263: NODE r;
8264: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8265: int rhead,rsugar,size;
8266: unsigned long *v;
8267:
8268: get_eg(&eg0);
8269: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8270: nred = length(rp0);
8271: mat = alloc_matrix(nsp,col);
8272: s0hash = (int *)MALLOC(col*sizeof(int));
8273: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8274: s0hash[i] = ndl_hash_value(s);
8275:
8276: sugar = (int *)MALLOC(nsp*sizeof(int));
8277: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8278: nd_sp(2,0,sp,&spol);
8279: if ( spol ) {
8280: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8281: sugar[i] = SG(spol);
8282: i++;
8283: }
8284: }
8285: mat->row = i;
8286: if ( DP_Print ) {
8287: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8288: }
8289: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8290: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8291: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8292: rt = (NM_ind_pair)BDY(rp);
8293: bzero(v,size);
8294: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8295: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8296: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8297: }
8298:
8299: get_eg(&eg1);
8300: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8301: rank = rref(mat,sugar);
8302:
8303: for ( i = 0, r0 = 0; i < rank; i++ ) {
8304: NEXTNODE(r0,r);
8305: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8306: SG((NDV)BDY(r)) = sugar[i];
8307: }
8308: if ( r0 ) NEXT(r) = 0;
8309: get_eg(&eg2);
8310: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8311: if ( DP_Print ) {
8312: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8313: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8314: fflush(asir_out);
8315: }
8316: return r0;
8317: }
8318:
8319:
8320: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8321: {
8322: IndArray *imat;
1.11 noro 8323: int nsp,nred,i,start;
1.1 noro 8324: int *rhead;
8325: NODE r0,rp;
8326: ND_pairs sp;
8327: NM_ind_pair *rvect;
8328: UINT *s;
8329: int *s0hash;
1.11 noro 8330: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8331:
8332: if ( m == 2 && nd_rref2 )
8333: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8334:
8335: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8336: nred = length(rp0);
8337: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8338: rhead = (int *)MALLOC(col*sizeof(int));
8339: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8340:
8341: /* construction of index arrays */
1.11 noro 8342: get_eg(&eg0);
1.1 noro 8343: if ( DP_Print ) {
1.11 noro 8344: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8345: fflush(asir_out);
1.1 noro 8346: }
8347: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8348: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8349: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8350: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8351: rhead[imat[i]->head] = 1;
1.11 noro 8352: start = imat[i]->head;
8353: }
1.12 noro 8354: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8355: if ( DP_Print ) {
8356: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8357: fflush(asir_out);
1.1 noro 8358: }
8359: if ( m > 0 )
1.7 noro 8360: #if SIZEOF_LONG==8
1.1 noro 8361: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8362: #else
8363: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8364: #endif
8365: else if ( m == -1 )
8366: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8367: else if ( m == -2 )
8368: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8369: else
8370: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8371: return r0;
8372: }
8373:
8374: /* for Fp, 2<=p<2^16 */
8375:
8376: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8377: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8378: {
8379: int spcol,sprow,a;
8380: int i,j,k,l,rank;
8381: NODE r0,r;
8382: ND_pairs sp;
8383: ND spol;
8384: UINT **spmat;
8385: UINT *svect,*v;
8386: int *colstat;
8387: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8388: int maxrs;
8389: int *spsugar;
8390: ND_pairs *spactive;
8391:
8392: spcol = col-nred;
8393: get_eg(&eg0);
8394: /* elimination (1st step) */
8395: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8396: svect = (UINT *)MALLOC(col*sizeof(UINT));
8397: spsugar = (int *)MALLOC(nsp*sizeof(int));
8398: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8399: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8400: nd_sp(m,0,sp,&spol);
8401: if ( !spol ) continue;
8402: nd_to_vect(m,s0vect,col,spol,svect);
8403: if ( m == -1 )
8404: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8405: else
8406: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
8407: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8408: if ( i < col ) {
8409: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8410: for ( j = k = 0; j < col; j++ )
8411: if ( !rhead[j] ) v[k++] = svect[j];
8412: spsugar[sprow] = MAX(maxrs,SG(spol));
8413: if ( nz )
8414: spactive[sprow] = sp;
8415: sprow++;
8416: }
8417: nd_free(spol);
8418: }
8419: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8420: if ( DP_Print ) {
1.5 noro 8421: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8422: fflush(asir_out);
8423: }
8424: /* free index arrays */
8425: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8426:
8427: /* elimination (2nd step) */
8428: colstat = (int *)MALLOC(spcol*sizeof(int));
8429: if ( m == -1 )
8430: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8431: else
8432: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8433: r0 = 0;
8434: for ( i = 0; i < rank; i++ ) {
8435: NEXTNODE(r0,r); BDY(r) =
8436: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8437: SG((NDV)BDY(r)) = spsugar[i];
8438: GCFREE(spmat[i]);
8439: }
8440: if ( r0 ) NEXT(r) = 0;
8441:
8442: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8443: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8444: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8445: if ( DP_Print ) {
1.5 noro 8446: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8447: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8448: nsp,nred,sprow,spcol,rank);
1.5 noro 8449: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8450: }
8451: if ( nz ) {
8452: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8453: if ( rank > 0 ) {
8454: NEXT(spactive[rank-1]) = 0;
8455: *nz = spactive[0];
8456: } else
8457: *nz = 0;
8458: }
8459: return r0;
8460: }
8461:
8462:
8463: /* for small finite fields */
8464:
8465: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8466: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8467: {
8468: int spcol,sprow,a;
8469: int i,j,k,l,rank;
8470: NODE r0,r;
8471: ND_pairs sp;
8472: ND spol;
8473: UINT **spmat;
8474: UINT *svect,*v;
8475: int *colstat;
8476: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8477: int maxrs;
8478: int *spsugar;
8479: ND_pairs *spactive;
8480:
8481: spcol = col-nred;
8482: get_eg(&eg0);
8483: /* elimination (1st step) */
8484: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8485: svect = (UINT *)MALLOC(col*sizeof(UINT));
8486: spsugar = (int *)MALLOC(nsp*sizeof(int));
8487: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8488: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8489: nd_sp(m,0,sp,&spol);
8490: if ( !spol ) continue;
8491: nd_to_vect(m,s0vect,col,spol,svect);
8492: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8493: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8494: if ( i < col ) {
8495: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8496: for ( j = k = 0; j < col; j++ )
8497: if ( !rhead[j] ) v[k++] = svect[j];
8498: spsugar[sprow] = MAX(maxrs,SG(spol));
8499: if ( nz )
8500: spactive[sprow] = sp;
8501: sprow++;
8502: }
8503: nd_free(spol);
8504: }
8505: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8506: if ( DP_Print ) {
1.5 noro 8507: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8508: fflush(asir_out);
8509: }
8510: /* free index arrays */
8511: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8512:
8513: /* elimination (2nd step) */
8514: colstat = (int *)MALLOC(spcol*sizeof(int));
8515: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8516: r0 = 0;
8517: for ( i = 0; i < rank; i++ ) {
8518: NEXTNODE(r0,r); BDY(r) =
8519: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8520: SG((NDV)BDY(r)) = spsugar[i];
8521: GCFREE(spmat[i]);
8522: }
8523: if ( r0 ) NEXT(r) = 0;
8524:
8525: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8526: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8527: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8528: if ( DP_Print ) {
1.5 noro 8529: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8530: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8531: nsp,nred,sprow,spcol,rank);
1.5 noro 8532: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8533: }
8534: if ( nz ) {
8535: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8536: if ( rank > 0 ) {
8537: NEXT(spactive[rank-1]) = 0;
8538: *nz = spactive[0];
8539: } else
8540: *nz = 0;
8541: }
8542: return r0;
8543: }
8544:
8545: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8546: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8547: {
8548: int spcol,sprow,a;
8549: int i,j,k,l,rank;
8550: NODE r0,r;
8551: ND_pairs sp;
8552: ND spol;
8553: mpz_t **spmat;
8554: mpz_t *svect,*v;
8555: int *colstat;
8556: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8557: int maxrs;
8558: int *spsugar;
8559: pointer *w;
8560:
8561: spcol = col-nred;
8562: get_eg(&eg0);
8563: /* elimination (1st step) */
8564: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8565: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8566: spsugar = (int *)MALLOC(nsp*sizeof(int));
8567: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8568: nd_sp(m,trace,sp,&spol);
8569: if ( !spol ) continue;
8570: nd_to_vect_lf(s0vect,col,spol,svect);
8571: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8572: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8573: if ( i < col ) {
8574: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8575: for ( j = k = 0; j < col; j++ )
8576: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8577: spsugar[sprow] = MAX(maxrs,SG(spol));
8578: sprow++;
8579: }
8580: /* nd_free(spol); */
8581: }
8582: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8583: if ( DP_Print ) {
1.5 noro 8584: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8585: fflush(asir_out);
8586: }
8587: /* free index arrays */
8588: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8589:
8590: /* elimination (2nd step) */
8591: colstat = (int *)MALLOC(spcol*sizeof(int));
8592: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8593: w = (pointer *)MALLOC(rank*sizeof(pointer));
8594: for ( i = 0; i < rank; i++ ) {
8595: #if 0
8596: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8597: SG((NDV)w[rank-i-1]) = spsugar[i];
8598: #else
8599: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8600: SG((NDV)w[i]) = spsugar[i];
8601: #endif
8602: /* GCFREE(spmat[i]); */
8603:
8604: }
8605: #if 0
8606: qsort(w,rank,sizeof(NDV),
8607: (int (*)(const void *,const void *))ndv_compare);
8608: #endif
8609: r0 = 0;
8610: for ( i = 0; i < rank; i++ ) {
8611: NEXTNODE(r0,r); BDY(r) = w[i];
8612: }
8613: if ( r0 ) NEXT(r) = 0;
8614:
8615: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8616: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8617: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8618: if ( DP_Print ) {
1.5 noro 8619: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8620: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8621: nsp,nred,sprow,spcol,rank);
1.5 noro 8622: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8623: }
8624: return r0;
8625: }
8626:
8627: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8628: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8629: {
8630: int spcol,sprow,a;
8631: int i,j,k,l,rank;
8632: NODE r0,r;
8633: ND_pairs sp;
8634: ND spol;
8635: Z **spmat;
8636: Z *svect,*v;
8637: int *colstat;
8638: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8639: int maxrs;
8640: int *spsugar;
8641: pointer *w;
8642:
8643: spcol = col-nred;
8644: get_eg(&eg0);
8645: /* elimination (1st step) */
8646: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8647: svect = (Z *)MALLOC(col*sizeof(Q));
8648: spsugar = (int *)MALLOC(nsp*sizeof(int));
8649: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8650: nd_sp(0,trace,sp,&spol);
8651: if ( !spol ) continue;
8652: nd_to_vect_q(s0vect,col,spol,svect);
8653: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8654: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8655: if ( i < col ) {
8656: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8657: for ( j = k = 0; j < col; j++ )
8658: if ( !rhead[j] ) v[k++] = svect[j];
8659: spsugar[sprow] = MAX(maxrs,SG(spol));
8660: sprow++;
8661: }
8662: /* nd_free(spol); */
8663: }
8664: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8665: if ( DP_Print ) {
1.5 noro 8666: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8667: fflush(asir_out);
8668: }
8669: /* free index arrays */
8670: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8671:
8672: /* elimination (2nd step) */
8673: colstat = (int *)MALLOC(spcol*sizeof(int));
8674: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8675: w = (pointer *)MALLOC(rank*sizeof(pointer));
8676: for ( i = 0; i < rank; i++ ) {
8677: #if 0
8678: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8679: SG((NDV)w[rank-i-1]) = spsugar[i];
8680: #else
8681: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8682: SG((NDV)w[i]) = spsugar[i];
8683: #endif
8684: /* GCFREE(spmat[i]); */
8685: }
8686: #if 0
8687: qsort(w,rank,sizeof(NDV),
8688: (int (*)(const void *,const void *))ndv_compare);
8689: #endif
8690: r0 = 0;
8691: for ( i = 0; i < rank; i++ ) {
8692: NEXTNODE(r0,r); BDY(r) = w[i];
8693: }
8694: if ( r0 ) NEXT(r) = 0;
8695:
8696: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8697: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8698: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8699: if ( DP_Print ) {
1.5 noro 8700: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8701: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8702: nsp,nred,sprow,spcol,rank);
1.5 noro 8703: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8704: }
8705: return r0;
8706: }
8707:
8708: FILE *nd_write,*nd_read;
8709:
8710: void nd_send_int(int a) {
8711: write_int(nd_write,(unsigned int *)&a);
8712: }
8713:
8714: void nd_send_intarray(int *p,int len) {
8715: write_intarray(nd_write,(unsigned int *)p,len);
8716: }
8717:
8718: int nd_recv_int() {
8719: int a;
8720:
8721: read_int(nd_read,(unsigned int *)&a);
8722: return a;
8723: }
8724:
8725: void nd_recv_intarray(int *p,int len) {
8726: read_intarray(nd_read,(unsigned int *)p,len);
8727: }
8728:
8729: void nd_send_ndv(NDV p) {
8730: int len,i;
8731: NMV m;
8732:
8733: if ( !p ) nd_send_int(0);
8734: else {
8735: len = LEN(p);
8736: nd_send_int(len);
8737: m = BDY(p);
8738: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8739: nd_send_int(CM(m));
8740: nd_send_intarray((int *)DL(m),nd_wpd);
8741: }
8742: }
8743: }
8744:
8745: void nd_send_nd(ND p) {
8746: int len,i;
8747: NM m;
8748:
8749: if ( !p ) nd_send_int(0);
8750: else {
8751: len = LEN(p);
8752: nd_send_int(len);
8753: m = BDY(p);
8754: for ( i = 0; i < len; i++, m = NEXT(m) ) {
8755: nd_send_int(CM(m));
8756: nd_send_intarray((int *)DL(m),nd_wpd);
8757: }
8758: }
8759: }
8760:
8761: NDV nd_recv_ndv()
8762: {
8763: int len,i;
8764: NMV m,m0;
8765: NDV r;
8766:
8767: len = nd_recv_int();
8768: if ( !len ) return 0;
8769: else {
8770: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8771: #if 0
8772: ndv_alloc += len*nmv_adv;
8773: #endif
8774: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8775: CM(m) = nd_recv_int();
8776: nd_recv_intarray((int *)DL(m),nd_wpd);
8777: }
8778: MKNDV(nd_nvar,m0,len,r);
8779: return r;
8780: }
8781: }
8782:
8783: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
8784: {
8785: int i,j,t,c,rank,inv;
8786: int *ci,*ri;
8787: Z dn;
8788: MAT m,nm;
8789:
8790: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
8791: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
8792: for ( i = 0; i < row; i++ )
8793: for ( j = 0; j < col; j++ )
8794: mat0[i][j] = 0;
8795: c = col-rank;
8796: for ( i = 0; i < rank; i++ ) {
8797: mat0[i][ri[i]] = dn;
8798: for ( j = 0; j < c; j++ )
8799: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
8800: }
8801: return rank;
8802: }
8803:
8804: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
8805: {
8806: int i,j,k,l,inv,a,rank,s;
8807: unsigned int *t,*pivot,*pk;
8808: unsigned int **mat;
8809: ND_pairs pair;
8810:
8811: mat = (unsigned int **)mat0;
8812: for ( rank = 0, j = 0; j < col; j++ ) {
8813: for ( i = rank; i < row; i++ )
8814: mat[i][j] %= md;
8815: for ( i = rank; i < row; i++ )
8816: if ( mat[i][j] )
8817: break;
8818: if ( i == row ) {
8819: colstat[j] = 0;
8820: continue;
8821: } else
8822: colstat[j] = 1;
8823: if ( i != rank ) {
8824: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8825: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8826: if ( spactive ) {
8827: pair = spactive[i]; spactive[i] = spactive[rank];
8828: spactive[rank] = pair;
8829: }
8830: }
8831: pivot = mat[rank];
8832: s = sugar[rank];
8833: inv = invm(pivot[j],md);
8834: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8835: if ( *pk ) {
8836: if ( *pk >= (unsigned int)md )
8837: *pk %= md;
8838: DMAR(*pk,inv,0,md,*pk)
8839: }
8840: for ( i = rank+1; i < row; i++ ) {
8841: t = mat[i];
8842: if ( (a = t[j]) != 0 ) {
8843: sugar[i] = MAX(sugar[i],s);
8844: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8845: }
8846: }
8847: rank++;
8848: }
8849: for ( j = col-1, l = rank-1; j >= 0; j-- )
8850: if ( colstat[j] ) {
8851: pivot = mat[l];
8852: s = sugar[l];
8853: for ( i = 0; i < l; i++ ) {
8854: t = mat[i];
8855: t[j] %= md;
8856: if ( (a = t[j]) != 0 ) {
8857: sugar[i] = MAX(sugar[i],s);
8858: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8859: }
8860: }
8861: l--;
8862: }
8863: for ( j = 0, l = 0; l < rank; j++ )
8864: if ( colstat[j] ) {
8865: t = mat[l];
8866: for ( k = j; k < col; k++ )
8867: if ( t[k] >= (unsigned int)md )
8868: t[k] %= md;
8869: l++;
8870: }
8871: return rank;
8872: }
8873:
8874:
1.7 noro 8875: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 8876: {
1.7 noro 8877: int i,j,k,l,inv,a,rank,s;
8878: unsigned int *t,*pivot,*pk;
8879: unsigned int **mat;
8880:
8881: mat = (unsigned int **)mat0;
8882: for ( rank = 0, j = 0; j < col; j++ ) {
8883: for ( i = rank; i < row; i++ )
8884: if ( mat[i][j] )
8885: break;
8886: if ( i == row ) {
8887: colstat[j] = 0;
8888: continue;
8889: } else
8890: colstat[j] = 1;
8891: if ( i != rank ) {
8892: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8893: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8894: }
8895: pivot = mat[rank];
8896: s = sugar[rank];
8897: inv = _invsf(pivot[j]);
8898: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8899: if ( *pk )
8900: *pk = _mulsf(*pk,inv);
8901: for ( i = rank+1; i < row; i++ ) {
8902: t = mat[i];
8903: if ( (a = t[j]) != 0 ) {
8904: sugar[i] = MAX(sugar[i],s);
8905: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8906: }
8907: }
8908: rank++;
8909: }
8910: for ( j = col-1, l = rank-1; j >= 0; j-- )
8911: if ( colstat[j] ) {
8912: pivot = mat[l];
8913: s = sugar[l];
8914: for ( i = 0; i < l; i++ ) {
8915: t = mat[i];
8916: if ( (a = t[j]) != 0 ) {
8917: sugar[i] = MAX(sugar[i],s);
8918: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8919: }
8920: }
8921: l--;
8922: }
8923: return rank;
8924: }
1.1 noro 8925:
1.7 noro 8926: int ndv_ishomo(NDV p)
8927: {
8928: NMV m;
8929: int len,h;
1.1 noro 8930:
8931: if ( !p ) return 1;
8932: len = LEN(p);
8933: m = BDY(p);
8934: h = TD(DL(m));
8935: NMV_ADV(m);
8936: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 8937: if ( TD(DL(m)) != h ) {
8938: return 0;
8939: }
1.1 noro 8940: return 1;
8941: }
8942:
8943: void ndv_save(NDV p,int index)
8944: {
8945: FILE *s;
8946: char name[BUFSIZ];
8947: short id;
8948: int nv,sugar,len,n,i,td,e,j;
8949: NMV m;
8950: unsigned int *dl;
8951: int mpos;
8952:
8953: sprintf(name,"%s/%d",Demand,index);
8954: s = fopen(name,"w");
8955: savevl(s,0);
8956: if ( !p ) {
8957: saveobj(s,0);
8958: return;
8959: }
8960: id = O_DP;
8961: nv = NV(p);
8962: sugar = SG(p);
8963: len = LEN(p);
8964: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
8965: write_int(s,(unsigned int *)&len);
8966:
8967: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8968: saveobj(s,(Obj)CZ(m));
1.1 noro 8969: dl = DL(m);
8970: td = TD(dl);
8971: write_int(s,(unsigned int *)&td);
8972: for ( j = 0; j < nv; j++ ) {
8973: e = GET_EXP(dl,j);
8974: write_int(s,(unsigned int *)&e);
8975: }
8976: if ( nd_module ) {
8977: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
8978: }
8979: }
8980: fclose(s);
8981: }
8982:
8983: void nd_save_mod(ND p,int index)
8984: {
8985: FILE *s;
8986: char name[BUFSIZ];
8987: int nv,sugar,len,c;
8988: NM m;
8989:
8990: sprintf(name,"%s/%d",Demand,index);
8991: s = fopen(name,"w");
8992: if ( !p ) {
8993: len = 0;
8994: write_int(s,(unsigned int *)&len);
8995: fclose(s);
8996: return;
8997: }
8998: nv = NV(p);
8999: sugar = SG(p);
9000: len = LEN(p);
9001: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
9002: for ( m = BDY(p); m; m = NEXT(m) ) {
9003: c = CM(m); write_int(s,(unsigned int *)&c);
9004: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
9005: }
9006: fclose(s);
9007: }
9008:
9009: NDV ndv_load(int index)
9010: {
9011: FILE *s;
9012: char name[BUFSIZ];
9013: short id;
9014: int nv,sugar,len,n,i,td,e,j;
9015: NDV d;
9016: NMV m0,m;
9017: unsigned int *dl;
9018: Obj obj;
9019: int mpos;
9020:
9021: sprintf(name,"%s/%d",Demand,index);
9022: s = fopen(name,"r");
9023: if ( !s ) return 0;
9024:
9025: skipvl(s);
9026: read_short(s,(unsigned short *)&id);
9027: if ( !id ) return 0;
9028: read_int(s,(unsigned int *)&nv);
9029: read_int(s,(unsigned int *)&sugar);
9030: read_int(s,(unsigned int *)&len);
9031:
9032: m0 = m = MALLOC(len*nmv_adv);
9033: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9034: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9035: dl = DL(m);
9036: ndl_zero(dl);
9037: read_int(s,(unsigned int *)&td); TD(dl) = td;
9038: for ( j = 0; j < nv; j++ ) {
9039: read_int(s,(unsigned int *)&e);
9040: PUT_EXP(dl,j,e);
9041: }
9042: if ( nd_module ) {
9043: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9044: }
9045: if ( nd_blockmask ) ndl_weight_mask(dl);
9046: }
9047: fclose(s);
9048: MKNDV(nv,m0,len,d);
9049: SG(d) = sugar;
9050: return d;
9051: }
9052:
9053: ND nd_load_mod(int index)
9054: {
9055: FILE *s;
9056: char name[BUFSIZ];
9057: int nv,sugar,len,i,c;
9058: ND d;
9059: NM m0,m;
9060:
9061: sprintf(name,"%s/%d",Demand,index);
9062: s = fopen(name,"r");
9063: /* if the file does not exist, it means p[index]=0 */
9064: if ( !s ) return 0;
9065:
9066: read_int(s,(unsigned int *)&nv);
9067: if ( !nv ) { fclose(s); return 0; }
9068:
9069: read_int(s,(unsigned int *)&sugar);
9070: read_int(s,(unsigned int *)&len);
9071: for ( m0 = 0, i = 0; i < len; i++ ) {
9072: NEXTNM(m0,m);
9073: read_int(s,(unsigned int *)&c); CM(m) = c;
9074: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9075: }
9076: NEXT(m) = 0;
9077: MKND(nv,m0,len,d);
9078: SG(d) = sugar;
9079: fclose(s);
9080: return d;
9081: }
9082:
9083: void nd_det(int mod,MAT f,P *rp)
9084: {
9085: VL fv,tv;
9086: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9087: pointer **m;
9088: P **w;
9089: P mp,r;
9090: NDV **dm;
9091: NDV *t,*mi,*mj;
9092: NDV d,s,mij,mjj;
9093: ND u;
9094: NMV nmv;
9095: UINT *bound;
9096: PGeoBucket bucket;
9097: struct order_spec *ord;
9098: Z dq,dt,ds;
9099: Z mone;
9100: Z gn,qn,dn0,nm,dn;
9101:
9102: create_order_spec(0,0,&ord);
9103: nd_init_ord(ord);
9104: get_vars((Obj)f,&fv);
9105: if ( f->row != f->col )
9106: error("nd_det : non-square matrix");
9107: n = f->row;
9108: m = f->body;
9109: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9110:
9111: if ( !nvar ) {
9112: if ( !mod )
9113: detp(CO,(P **)m,n,rp);
9114: else {
9115: w = (P **)almat_pointer(n,n);
9116: for ( i = 0; i < n; i++ )
9117: for ( j = 0; j < n; j++ )
9118: ptomp(mod,(P)m[i][j],&w[i][j]);
9119: detmp(CO,mod,w,n,&mp);
9120: mptop(mp,rp);
9121: }
9122: return;
9123: }
9124:
9125: if ( !mod ) {
9126: w = (P **)almat_pointer(n,n);
9127: dq = ONE;
9128: for ( i = 0; i < n; i++ ) {
9129: dn0 = ONE;
9130: for ( j = 0; j < n; j++ ) {
9131: if ( !m[i][j] ) continue;
9132: lgp(m[i][j],&nm,&dn);
1.6 noro 9133: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9134: }
9135: if ( !UNIZ(dn0) ) {
9136: ds = dn0;
9137: for ( j = 0; j < n; j++ )
9138: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9139: mulz(dq,ds,&dt); dq = dt;
9140: } else
9141: for ( j = 0; j < n; j++ )
9142: w[i][j] = (P)m[i][j];
9143: }
9144: m = (pointer **)w;
9145: }
9146:
9147: for ( i = 0, max = 1; i < n; i++ )
9148: for ( j = 0; j < n; j++ )
9149: for ( tv = fv; tv; tv = NEXT(tv) ) {
9150: e = getdeg(tv->v,(P)m[i][j]);
9151: max = MAX(e,max);
9152: }
9153: nd_setup_parameters(nvar,max);
9154: dm = (NDV **)almat_pointer(n,n);
9155: for ( i = 0, max = 1; i < n; i++ )
9156: for ( j = 0; j < n; j++ ) {
9157: dm[i][j] = ptondv(CO,fv,m[i][j]);
9158: if ( mod ) ndv_mod(mod,dm[i][j]);
9159: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9160: }
9161: d = ptondv(CO,fv,(P)ONE);
9162: if ( mod ) ndv_mod(mod,d);
9163: chsgnz(ONE,&mone);
9164: for ( j = 0, sgn = 1; j < n; j++ ) {
9165: if ( DP_Print ) {
9166: fprintf(asir_out,".");
9167: }
9168: for ( i = j; i < n && !dm[i][j]; i++ );
9169: if ( i == n ) {
9170: *rp = 0;
9171: return;
9172: }
9173: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9174: for ( k = j; k < n; k++ )
9175: for ( l = j; l < n; l++ )
9176: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9177: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9178: }
9179: if ( k0 != j ) {
9180: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9181: sgn = -sgn;
9182: }
9183: if ( l0 != j ) {
9184: for ( k = j; k < n; k++ ) {
9185: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9186: }
9187: sgn = -sgn;
9188: }
9189: bound = nd_det_compute_bound(dm,n,j);
9190: for ( k = 0; k < nd_nvar; k++ )
9191: if ( bound[k]*2 > nd_mask0 ) break;
9192: if ( k < nd_nvar )
9193: nd_det_reconstruct(dm,n,j,d);
9194:
9195: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9196: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9197: mi = dm[i]; mij = mi[j];
9198: if ( mod )
9199: ndv_mul_c(mod,mij,mod-1);
9200: else
9201: ndv_mul_c_q(mij,mone);
9202: for ( k = j+1; k < n; k++ ) {
9203: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9204: bucket = create_pbucket();
9205: if ( mi[k] ) {
9206: nmv = BDY(mjj); len = LEN(mjj);
9207: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9208: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9209: add_pbucket(mod,bucket,u);
9210: }
9211: }
9212: if ( mj[k] && mij ) {
9213: nmv = BDY(mij); len = LEN(mij);
9214: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9215: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9216: add_pbucket(mod,bucket,u);
9217: }
9218: }
9219: u = nd_quo(mod,bucket,d);
9220: mi[k] = ndtondv(mod,u);
9221: }
9222: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9223: }
9224: d = mjj;
9225: }
9226: if ( DP_Print ) {
9227: fprintf(asir_out,"\n");
9228: }
9229: if ( sgn < 0 ) {
9230: if ( mod )
9231: ndv_mul_c(mod,d,mod-1);
9232: else
9233: ndv_mul_c_q(d,mone);
9234: }
9235: r = ndvtop(mod,CO,fv,d);
9236: if ( !mod && !UNIQ(dq) )
9237: divsp(CO,r,(P)dq,rp);
9238: else
9239: *rp = r;
9240: }
9241:
9242: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9243: {
9244: NM mr,mr0;
9245: NM tnm;
9246: NMV m;
9247: UINT *d0,*dt,*dm;
9248: int c,n,td,i,c1,c2,len;
9249: Z q;
9250: ND r;
9251:
9252: if ( !p ) return 0;
9253: else {
9254: n = NV(p); m = BDY(p); len = LEN(p);
9255: d0 = DL(m0);
9256: td = TD(d);
9257: mr0 = 0;
9258: NEWNM(tnm);
9259: if ( mod ) {
9260: c = CM(m0);
9261: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9262: ndl_add(DL(m),d0,DL(tnm));
9263: if ( ndl_reducible(DL(tnm),d) ) {
9264: NEXTNM(mr0,mr);
9265: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9266: ndl_copy(DL(tnm),DL(mr));
9267: }
9268: }
9269: } else {
1.6 noro 9270: q = CZ(m0);
1.1 noro 9271: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9272: ndl_add(DL(m),d0,DL(tnm));
9273: if ( ndl_reducible(DL(tnm),d) ) {
9274: NEXTNM(mr0,mr);
1.6 noro 9275: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9276: ndl_copy(DL(tnm),DL(mr));
9277: }
9278: }
9279: }
9280: if ( !mr0 )
9281: return 0;
9282: else {
9283: NEXT(mr) = 0;
9284: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9285: MKND(NV(p),mr0,len,r);
9286: SG(r) = SG(p) + TD(d0);
9287: return r;
9288: }
9289: }
9290: }
9291:
9292: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9293: {
9294: int i,obpe,oadv,h,k,l;
9295: static NM prev_nm_free_list;
9296: EPOS oepos;
9297:
9298: obpe = nd_bpe;
9299: oadv = nmv_adv;
9300: oepos = nd_epos;
9301: if ( obpe < 2 ) nd_bpe = 2;
9302: else if ( obpe < 3 ) nd_bpe = 3;
9303: else if ( obpe < 4 ) nd_bpe = 4;
9304: else if ( obpe < 5 ) nd_bpe = 5;
9305: else if ( obpe < 6 ) nd_bpe = 6;
9306: else if ( obpe < 8 ) nd_bpe = 8;
9307: else if ( obpe < 10 ) nd_bpe = 10;
9308: else if ( obpe < 16 ) nd_bpe = 16;
9309: else if ( obpe < 32 ) nd_bpe = 32;
9310: else error("nd_det_reconstruct : exponent too large");
9311:
9312: nd_setup_parameters(nd_nvar,0);
9313: prev_nm_free_list = _nm_free_list;
9314: _nm_free_list = 0;
9315: for ( k = j; k < n; k++ )
9316: for (l = j; l < n; l++ )
9317: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9318: ndv_realloc(d,obpe,oadv,oepos);
9319: prev_nm_free_list = 0;
9320: #if 0
9321: GC_gcollect();
9322: #endif
9323: }
9324:
9325: /* returns a UINT array containing degree bounds */
9326:
9327: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9328: {
9329: UINT *d0,*d1,*d,*t,*r;
9330: int k,l,i;
9331:
9332: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9333: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9334: for ( k = j; k < n; k++ )
9335: for ( l = j; l < n; l++ )
9336: if ( dm[k][l] ) {
9337: d = ndv_compute_bound(dm[k][l]);
9338: for ( i = 0; i < nd_nvar; i++ )
9339: d0[i] = MAX(d0[i],d[i]);
9340: }
9341: return d0;
9342: }
9343:
9344: DL nd_separate_d(UINT *d,UINT *trans)
9345: {
9346: int n,td,i,e,j;
9347: DL a;
9348:
9349: ndl_zero(trans);
9350: td = 0;
9351: for ( i = 0; i < nd_ntrans; i++ ) {
9352: e = GET_EXP(d,i);
9353: PUT_EXP(trans,i,e);
9354: td += MUL_WEIGHT(e,i);
9355: }
9356: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9357: /* homogenized */
9358: i = nd_nvar-1;
9359: e = GET_EXP(d,i);
9360: PUT_EXP(trans,i,e);
9361: td += MUL_WEIGHT(e,i);
9362: }
9363: TD(trans) = td;
9364: if ( nd_blockmask) ndl_weight_mask(trans);
9365: NEWDL(a,nd_nalg);
9366: td = 0;
9367: for ( i = 0; i < nd_nalg; i++ ) {
9368: j = nd_ntrans+i;
9369: e = GET_EXP(d,j);
9370: a->d[i] = e;
9371: td += e;
9372: }
9373: a->td = td;
9374: return a;
9375: }
9376:
9377: int nd_monic(int mod,ND *p)
9378: {
9379: UINT *trans,*t;
9380: DL alg;
9381: MP mp0,mp;
9382: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9383: ND r;
9384: DL dl;
9385: DP nm;
9386: NDV ndv;
9387: DAlg inv,cd;
9388: ND s,c;
9389: Z l,mul;
9390: Z ln;
9391: int n,ntrans,i,e,td,is_lc,len;
9392: NumberField nf;
9393: struct oEGT eg0,eg1;
9394:
9395: if ( !(nf = get_numberfield()) )
9396: error("nd_monic : current_numberfield is not set");
9397:
9398: /* Q coef -> DAlg coef */
9399: NEWNM(ma0); ma = ma0;
9400: m = BDY(*p);
9401: is_lc = 1;
9402: while ( 1 ) {
9403: NEWMP(mp0); mp = mp0;
1.6 noro 9404: mp->c = (Obj)CZ(m);
1.1 noro 9405: mp->dl = nd_separate_d(DL(m),DL(ma));
9406: NEWNM(mb);
9407: for ( m = NEXT(m); m; m = NEXT(m) ) {
9408: alg = nd_separate_d(DL(m),DL(mb));
9409: if ( !ndl_equal(DL(ma),DL(mb)) )
9410: break;
1.6 noro 9411: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9412: }
9413: NEXT(mp) = 0;
9414: MKDP(nd_nalg,mp0,nm);
9415: MKDAlg(nm,ONE,cd);
9416: if ( is_lc == 1 ) {
9417: /* if the lc is a rational number, we have nothing to do */
9418: if ( !mp0->dl->td )
9419: return 1;
9420:
9421: get_eg(&eg0);
9422: invdalg(cd,&inv);
9423: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9424: /* check the validity of inv */
9425: if ( mod && !remqi((Q)inv->dn,mod) )
9426: return 0;
9427: CA(ma) = nf->one;
9428: is_lc = 0;
9429: ln = ONE;
9430: } else {
9431: muldalg(cd,inv,&CA(ma));
9432: lcmz(ln,CA(ma)->dn,&ln);
9433: }
9434: if ( m ) {
9435: NEXT(ma) = mb; ma = mb;
9436: } else {
9437: NEXT(ma) = 0;
9438: break;
9439: }
9440: }
9441: /* l = lcm(denoms) */
9442: l = ln;
9443: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9444: divsz(l,CA(m)->dn,&mul);
1.1 noro 9445: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9446: NEXTNM(mr0,mr);
1.6 noro 9447: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9448: dl = mp->dl;
9449: td = TD(DL(m));
9450: ndl_copy(DL(m),DL(mr));
9451: for ( i = 0; i < nd_nalg; i++ ) {
9452: e = dl->d[i];
9453: PUT_EXP(DL(mr),i+nd_ntrans,e);
9454: td += MUL_WEIGHT(e,i+nd_ntrans);
9455: }
9456: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9457: TD(DL(mr)) = td;
9458: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9459: }
9460: }
9461: NEXT(mr) = 0;
9462: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9463: MKND(NV(*p),mr0,len,r);
9464: /* XXX */
9465: SG(r) = SG(*p);
9466: nd_free(*p);
9467: *p = r;
9468: return 1;
9469: }
9470:
9471: NODE reverse_node(NODE n)
9472: {
9473: NODE t,t1;
9474:
9475: for ( t = 0; n; n = NEXT(n) ) {
9476: MKNODE(t1,BDY(n),t); t = t1;
9477: }
9478: return t;
9479: }
9480:
9481: P ndc_div(int mod,union oNDC a,union oNDC b)
9482: {
9483: union oNDC c;
9484: int inv,t;
9485:
9486: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9487: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9488: else if ( mod ) {
9489: inv = invm(b.m,mod);
9490: DMAR(a.m,inv,0,mod,t); c.m = t;
9491: } else if ( nd_vc )
9492: divsp(nd_vc,a.p,b.p,&c.p);
9493: else
9494: divsz(a.z,b.z,&c.z);
9495: return ndctop(mod,c);
9496: }
9497:
9498: P ndctop(int mod,union oNDC c)
9499: {
9500: Z q;
9501: int e;
9502: GFS gfs;
9503:
9504: if ( mod == -1 ) {
9505: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9506: } else if ( mod == -2 ) {
1.10 noro 9507: q = c.z; return (P)q;
1.1 noro 9508: } else if ( mod > 0 ) {
1.6 noro 9509: STOZ(c.m,q); return (P)q;
1.1 noro 9510: } else
9511: return (P)c.p;
9512: }
9513:
9514: /* [0,0,0,cont] = p -> p/cont */
9515:
9516: void finalize_tracelist(int i,P cont)
9517: {
9518: LIST l;
9519: NODE node;
9520: Z iq;
9521:
9522: if ( !UNIQ(cont) ) {
9523: node = mknode(4,NULLP,NULLP,NULLP,cont);
9524: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9525: nd_tracelist = node;
9526: }
1.6 noro 9527: STOZ(i,iq);
1.1 noro 9528: nd_tracelist = reverse_node(nd_tracelist);
9529: MKLIST(l,nd_tracelist);
9530: node = mknode(2,iq,l); MKLIST(l,node);
9531: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9532: nd_alltracelist = node; nd_tracelist = 0;
9533: }
9534:
9535: void conv_ilist(int demand,int trace,NODE g,int **indp)
9536: {
9537: int n,i,j;
9538: int *ind;
9539: NODE t;
9540:
9541: n = length(g);
9542: ind = (int *)MALLOC(n*sizeof(int));
9543: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9544: j = (long)BDY(t); ind[i] = j;
9545: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9546: }
9547: if ( indp ) *indp = ind;
9548: }
9549:
1.30 noro 9550: NODE conv_ilist_s(int demand,int trace,int **indp)
9551: {
9552: int n,i,j;
9553: int *ind;
9554: NODE g0,g;
9555:
9556: n = nd_psn;
9557: ind = (int *)MALLOC(n*sizeof(int));
9558: g0 = 0;
9559: for ( i = 0; i < n; i++ ) {
9560: ind[i] = i;
9561: NEXTNODE(g0,g);
9562: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
9563: }
9564: if ( g0 ) NEXT(g) = 0;
9565: if ( indp ) *indp = ind;
9566: return g0;
9567: }
9568:
1.1 noro 9569: void parse_nd_option(NODE opt)
9570: {
9571: NODE t,p,u;
9572: int i,s,n;
9573: char *key;
9574: Obj value;
9575:
9576: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
9577: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9578: nd_splist = 0; nd_check_splist = 0;
9579: nd_sugarweight = 0;
9580: nd_f4red =0;
9581: nd_rank0 = 0;
9582: for ( t = opt; t; t = NEXT(t) ) {
9583: p = BDY((LIST)BDY(t));
9584: key = BDY((STRING)BDY(p));
9585: value = (Obj)BDY(NEXT(p));
9586: if ( !strcmp(key,"gentrace") )
9587: nd_gentrace = value?1:0;
9588: else if ( !strcmp(key,"gensyz") )
9589: nd_gensyz = value?1:0;
9590: else if ( !strcmp(key,"nora") )
9591: nd_nora = value?1:0;
9592: else if ( !strcmp(key,"gbblock") ) {
9593: if ( value && OID(value) == O_LIST ) {
9594: u = BDY((LIST)value);
9595: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
9596: for ( i = 0; u; u = NEXT(u) ) {
9597: p = BDY((LIST)BDY(u));
1.6 noro 9598: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9599: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9600: }
9601: nd_gbblock[i] = -1;
9602: } else
9603: nd_gbblock = 0;
9604: } else if ( !strcmp(key,"newelim") )
9605: nd_newelim = value?1:0;
9606: else if ( !strcmp(key,"intersect") )
9607: nd_intersect = value?1:0;
1.17 noro 9608: else if ( !strcmp(key,"syzgen") )
9609: nd_intersect = ZTOS((Q)value);
1.1 noro 9610: else if ( !strcmp(key,"lf") )
9611: nd_lf = value?1:0;
9612: else if ( !strcmp(key,"trace") ) {
9613: if ( value ) {
9614: u = BDY((LIST)value);
9615: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 9616: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 9617: }
9618: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 9619: nd_f4red = ZTOS((Q)value);
1.1 noro 9620: } else if ( !strcmp(key,"rank0") ) {
9621: nd_rank0 = value?1:0;
9622: } else if ( !strcmp(key,"splist") ) {
9623: nd_splist = value?1:0;
9624: } else if ( !strcmp(key,"check_splist") ) {
9625: nd_check_splist = BDY((LIST)value);
9626: } else if ( !strcmp(key,"sugarweight") ) {
9627: u = BDY((LIST)value);
9628: n = length(u);
9629: nd_sugarweight = MALLOC(n*sizeof(int));
9630: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 9631: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 9632: }
9633: }
9634: }
9635:
9636: ND mdptond(DP d);
9637: ND nd_mul_nm(int mod,NM m0,ND p);
9638: ND nd_mul_nm_lf(NM m0,ND p);
9639: ND *btog(NODE ti,ND **p,int nb,int mod);
9640: ND btog_one(NODE ti,ND *p,int nb,int mod);
9641: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
9642: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
9643:
9644: /* d:monomial */
9645: ND mdptond(DP d)
9646: {
9647: NM m;
9648: ND r;
9649:
9650: if ( OID(d) == 1 )
9651: r = ptond(CO,CO,(P)d);
9652: else {
9653: NEWNM(m);
9654: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 9655: CZ(m) = (Z)BDY(d)->c;
1.1 noro 9656: NEXT(m) = 0;
9657: MKND(NV(d),m,1,r);
9658: }
9659: return r;
9660: }
9661:
9662: ND nd_mul_nm(int mod,NM m0,ND p)
9663: {
9664: UINT *d0;
9665: int c0,c1,c;
9666: NM tm,mr,mr0;
9667: ND r;
9668:
9669: if ( !p ) return 0;
9670: d0 = DL(m0);
9671: c0 = CM(m0);
9672: mr0 = 0;
9673: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9674: NEXTNM(mr0,mr);
9675: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
9676: ndl_add(d0,DL(tm),DL(mr));
9677: }
9678: NEXT(mr) = 0;
9679: MKND(NV(p),mr0,LEN(p),r);
9680: return r;
9681: }
9682:
9683: ND nd_mul_nm_lf(NM m0,ND p)
9684: {
9685: UINT *d0;
9686: Z c0,c1,c;
9687: NM tm,mr,mr0;
9688: ND r;
9689:
9690: if ( !p ) return 0;
9691: d0 = DL(m0);
9692: c0 = CZ(m0);
9693: mr0 = 0;
9694: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9695: NEXTNM(mr0,mr);
9696: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
9697: ndl_add(d0,DL(tm),DL(mr));
9698: }
9699: NEXT(mr) = 0;
9700: MKND(NV(p),mr0,LEN(p),r);
9701: return r;
9702: }
9703:
9704: ND *btog(NODE ti,ND **p,int nb,int mod)
9705: {
9706: PGeoBucket *r;
9707: int i,ci;
9708: NODE t,s;
9709: ND m,tp;
9710: ND *pi,*rd;
9711: P c;
9712:
9713: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9714: for ( i = 0; i < nb; i++ )
9715: r[i] = create_pbucket();
9716: for ( t = ti; t; t = NEXT(t) ) {
9717: s = BDY((LIST)BDY(t));
9718: if ( ARG0(s) ) {
9719: m = mdptond((DP)ARG2(s));
1.6 noro 9720: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9721: if ( (ci = ((MQ)c)->cont) != 0 ) {
9722: HCM(m) = ci;
1.6 noro 9723: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9724: for ( i = 0; i < nb; i++ ) {
9725: tp = nd_mul_nm(mod,BDY(m),pi[i]);
9726: add_pbucket(mod,r[i],tp);
9727: }
9728: }
9729: ci = 1;
9730: } else {
9731: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9732: ci = invm(ci,mod);
9733: }
9734: }
9735: rd = (ND *)MALLOC(nb*sizeof(ND));
9736: for ( i = 0; i < nb; i++ )
9737: rd[i] = normalize_pbucket(mod,r[i]);
9738: if ( ci != 1 )
9739: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
9740: return rd;
9741: }
9742:
9743: /* YYY */
9744: ND *btog_lf(NODE ti,ND **p,int nb)
9745: {
9746: PGeoBucket *r;
9747: int i;
9748: NODE t,s;
9749: ND m,tp;
9750: ND *pi,*rd;
9751: LM lm;
9752: Z lf,c;
9753:
9754: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9755: for ( i = 0; i < nb; i++ )
9756: r[i] = create_pbucket();
9757: for ( t = ti; t; t = NEXT(t) ) {
9758: s = BDY((LIST)BDY(t));
9759: if ( ARG0(s) ) {
9760: m = mdptond((DP)ARG2(s));
1.6 noro 9761: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 9762: if ( lm ) {
9763: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 9764: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9765: for ( i = 0; i < nb; i++ ) {
9766: tp = nd_mul_nm_lf(BDY(m),pi[i]);
9767: add_pbucket(-2,r[i],tp);
9768: }
9769: }
9770: c = ONE;
9771: } else {
9772: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
9773: }
9774: }
9775: rd = (ND *)MALLOC(nb*sizeof(ND));
9776: for ( i = 0; i < nb; i++ )
9777: rd[i] = normalize_pbucket(-2,r[i]);
9778: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
9779: return rd;
9780: }
9781:
9782: ND btog_one(NODE ti,ND *p,int nb,int mod)
9783: {
9784: PGeoBucket r;
9785: int i,ci,j;
9786: NODE t,s;
9787: ND m,tp;
9788: ND pi,rd;
9789: P c;
9790:
9791: r = create_pbucket();
9792: for ( t = ti; t; t = NEXT(t) ) {
9793: s = BDY((LIST)BDY(t));
9794: if ( ARG0(s) ) {
9795: m = mdptond((DP)ARG2(s));
1.6 noro 9796: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9797: if ( (ci = ((MQ)c)->cont) != 0 ) {
9798: HCM(m) = ci;
1.6 noro 9799: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 9800: if ( !pi ) {
9801: pi = nd_load_mod(j);
9802: tp = nd_mul_nm(mod,BDY(m),pi);
9803: nd_free(pi);
9804: add_pbucket(mod,r,tp);
9805: } else {
9806: tp = nd_mul_nm(mod,BDY(m),pi);
9807: add_pbucket(mod,r,tp);
9808: }
9809: }
9810: ci = 1;
9811: } else {
9812: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9813: ci = invm(ci,mod);
9814: }
9815: }
9816: rd = normalize_pbucket(mod,r);
9817: free_pbucket(r);
9818: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
9819: return rd;
9820: }
9821:
9822: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
9823:
9824: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
9825: {
9826: int i,j,n,m,nb,pi0,pi1,nvar;
9827: VL fv,tv,vv;
9828: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9829: ND **p;
9830: ND *c;
9831: ND u;
9832: P inv;
9833: MAT mat;
9834:
9835: if ( mod == -2 )
9836: return nd_btog_lf(f,v,ord,tlist,rp);
9837:
9838: parse_nd_option(current_option);
9839: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9840: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9841: switch ( ord->id ) {
9842: case 1:
9843: if ( ord->nv != nvar )
9844: error("nd_check : invalid order specification");
9845: break;
9846: default:
9847: break;
9848: }
9849: nd_init_ord(ord);
9850: #if 0
1.6 noro 9851: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9852: #else
9853: nd_bpe = 32;
9854: #endif
9855: nd_setup_parameters(nvar,0);
9856: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9857: intred = BDY((LIST)ARG3(BDY(tlist)));
9858: ind = BDY((LIST)ARG4(BDY(tlist)));
9859: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9860: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9861: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9862: if ( j > i ) i = j;
9863: }
9864: n = i+1;
9865: nb = length(BDY(f));
9866: p = (ND **)MALLOC(n*sizeof(ND *));
9867: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9868: pi = BDY((LIST)BDY(t));
1.6 noro 9869: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9870: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9871: ptomp(mod,(P)ARG2(pi),&inv);
9872: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9873: u = ptond(CO,vv,(P)ONE);
9874: HCM(u) = ((MQ)inv)->cont;
9875: c[pi1] = u;
9876: }
9877: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9878: printf("%d ",i); fflush(stdout);
9879: ti = BDY((LIST)BDY(t));
1.6 noro 9880: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9881: }
9882: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9883: printf("%d ",i); fflush(stdout);
9884: ti = BDY((LIST)BDY(t));
1.6 noro 9885: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9886: }
9887: m = length(ind);
9888: MKMAT(mat,nb,m);
9889: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9890: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9891: BDY(mat)[i][j] = ndtodp(mod,c[i]);
9892: return mat;
9893: }
9894:
9895: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
9896: {
9897: int i,j,n,m,nb,pi0,pi1,nvar;
9898: VL fv,tv,vv;
9899: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9900: ND **p;
9901: ND *c;
9902: ND u;
9903: MAT mat;
9904: LM lm;
9905: Z lf,inv;
9906:
9907: parse_nd_option(current_option);
9908: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9909: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9910: switch ( ord->id ) {
9911: case 1:
9912: if ( ord->nv != nvar )
9913: error("nd_check : invalid order specification");
9914: break;
9915: default:
9916: break;
9917: }
9918: nd_init_ord(ord);
9919: #if 0
1.6 noro 9920: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9921: #else
9922: nd_bpe = 32;
9923: #endif
9924: nd_setup_parameters(nvar,0);
9925: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9926: intred = BDY((LIST)ARG3(BDY(tlist)));
9927: ind = BDY((LIST)ARG4(BDY(tlist)));
9928: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9929: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9930: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9931: if ( j > i ) i = j;
9932: }
9933: n = i+1;
9934: nb = length(BDY(f));
9935: p = (ND **)MALLOC(n*sizeof(ND *));
9936: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9937: pi = BDY((LIST)BDY(t));
1.6 noro 9938: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9939: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9940: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
9941: u = ptond(CO,vv,(P)ONE);
9942: HCZ(u) = inv;
9943: c[pi1] = u;
9944: }
9945: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9946: printf("%d ",i); fflush(stdout);
9947: ti = BDY((LIST)BDY(t));
1.6 noro 9948: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9949: }
9950: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9951: printf("%d ",i); fflush(stdout);
9952: ti = BDY((LIST)BDY(t));
1.6 noro 9953: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9954: }
9955: m = length(ind);
9956: MKMAT(mat,nb,m);
9957: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9958: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9959: BDY(mat)[i][j] = ndtodp(-2,c[i]);
9960: return mat;
9961: }
9962:
9963: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
9964: LIST tlist,int pos,MAT *rp)
9965: {
9966: int i,j,n,m,nb,pi0,pi1,nvar;
9967: VL fv,tv,vv;
9968: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9969: ND *p;
9970: ND *c;
9971: ND u;
9972: P inv;
9973: VECT vect;
9974:
9975: if ( mod == -2 )
9976: error("nd_btog_one : not implemented yet for a large finite field");
9977:
9978: parse_nd_option(current_option);
9979: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9980: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9981: switch ( ord->id ) {
9982: case 1:
9983: if ( ord->nv != nvar )
9984: error("nd_check : invalid order specification");
9985: break;
9986: default:
9987: break;
9988: }
9989: nd_init_ord(ord);
9990: #if 0
1.6 noro 9991: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9992: #else
9993: nd_bpe = 32;
9994: #endif
9995: nd_setup_parameters(nvar,0);
9996: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9997: intred = BDY((LIST)ARG3(BDY(tlist)));
9998: ind = BDY((LIST)ARG4(BDY(tlist)));
9999: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
10000: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 10001: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 10002: if ( j > i ) i = j;
10003: }
10004: n = i+1;
10005: nb = length(BDY(f));
10006: p = (ND *)MALLOC(n*sizeof(ND *));
10007: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
10008: pi = BDY((LIST)BDY(t));
1.6 noro 10009: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 10010: if ( pi1 == pos ) {
10011: ptomp(mod,(P)ARG2(pi),&inv);
10012: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10013: u = ptond(CO,vv,(P)ONE);
10014: HCM(u) = ((MQ)inv)->cont;
10015: p[pi0] = u;
10016: }
10017: }
10018: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10019: printf("%d ",i); fflush(stdout);
10020: ti = BDY((LIST)BDY(t));
1.6 noro 10021: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10022: if ( Demand ) {
10023: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10024: }
10025: }
10026: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10027: printf("%d ",i); fflush(stdout);
10028: ti = BDY((LIST)BDY(t));
1.6 noro 10029: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10030: if ( Demand ) {
10031: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10032: }
10033: }
10034: m = length(ind);
10035: MKVECT(vect,m);
10036: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10037: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10038: if ( !u ) {
1.6 noro 10039: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10040: BDY(vect)[j] = ndtodp(mod,u);
10041: nd_free(u);
10042: } else
10043: BDY(vect)[j] = ndtodp(mod,u);
10044: }
10045: return vect;
10046: }
10047:
10048: void ndv_print_lf(NDV p)
10049: {
10050: NMV m;
10051: int i,len;
10052:
10053: if ( !p ) printf("0\n");
10054: else {
10055: len = LEN(p);
10056: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10057: printf("+");
10058: mpz_out_str(asir_out,10,BDY(CZ(m)));
10059: printf("*");
10060: ndl_print(DL(m));
10061: }
10062: printf("\n");
10063: }
10064: }
10065:
10066: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10067: {
10068: VL tv,fv,vv,vc,av;
10069: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10070: int m,nocheck,nvar,mindex,e,max;
10071: NDV c;
10072: NMV a;
10073: P p,zp;
10074: Q dmy;
10075: EPOS oepos;
10076: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10077: Alg alpha,dp;
10078: P poly;
10079: LIST f1,f2,zpl;
10080: Obj obj;
10081: NumberField nf;
10082: struct order_spec *ord1;
10083: struct oEGT eg_check,eg0,eg1;
10084: NODE tr,tl1,tl2,tl3,tl4;
10085: LIST l1,l2,l3,l4,l5;
10086: int *perm;
10087: int j,ret;
10088: NODE retn;
10089: Q jq,bpe;
10090:
10091: nd_module = 0;
10092: parse_nd_option(current_option);
10093: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10094: if ( nd_vc )
10095: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10096: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10097: switch ( ord->id ) {
10098: case 1:
10099: if ( ord->nv != nvar )
10100: error("nd_f4_lf_trace : invalid order specification");
10101: break;
10102: default:
10103: break;
10104: }
10105:
10106: nd_ntrans = nvar;
10107: nd_nalg = 0;
10108:
10109: nocheck = 0;
10110: mindex = 0;
10111:
10112: /* do not use on-demand load/save */
10113: nd_demand = 0;
10114: m = trace > 1 ? trace : get_lprime(mindex);
10115: nd_init_ord(ord);
10116: mrank = 0;
10117: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10118: for ( tv = vv; tv; tv = NEXT(tv) ) {
10119: if ( nd_module ) {
10120: s = BDY((LIST)BDY(t));
10121: trank = length(s);
10122: mrank = MAX(mrank,trank);
10123: for ( ; s; s = NEXT(s) ) {
10124: e = getdeg(tv->v,(P)BDY(s));
10125: max = MAX(e,max);
10126: }
10127: } else {
10128: e = getdeg(tv->v,(P)BDY(t));
10129: max = MAX(e,max);
10130: }
10131: }
10132: nd_setup_parameters(nvar,max);
10133: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10134: ishomo = 1;
10135: /* XXX */
10136: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10137: if ( nd_module ) {
10138: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10139: } else {
10140: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10141: }
10142: if ( ishomo )
10143: ishomo = ishomo && ndv_ishomo(c);
10144: if ( c ) {
10145: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10146: ndv_mod(-2,c);
10147: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10148: }
10149: }
10150: if ( in0 ) NEXT(in) = 0;
10151: if ( fd0 ) NEXT(fd) = 0;
10152: if ( !ishomo && homo ) {
10153: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10154: c = (NDV)BDY(t); len = LEN(c);
10155: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10156: wmax = MAX(TD(DL(a)),wmax);
10157: }
10158: homogenize_order(ord,nvar,&ord1);
10159: nd_init_ord(ord1);
10160: nd_setup_parameters(nvar+1,wmax);
10161: for ( t = fd0; t; t = NEXT(t) )
10162: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10163: }
10164: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10165: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10166: if ( ret )
10167: cand = nd_f4_lf_trace_main(m,&perm);
10168: if ( !ret || !cand ) {
10169: *rp = 0; return;
10170: }
10171: if ( !ishomo && homo ) {
10172: /* dehomogenization */
10173: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10174: nd_init_ord(ord);
10175: nd_setup_parameters(nvar,0);
10176: }
10177: cand = ndv_reducebase(cand,perm);
10178: cand = ndv_reduceall(-2,cand);
10179: cbpe = nd_bpe;
10180: get_eg(&eg0);
10181: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10182: /* gbcheck : cand is a GB of Id(cand) ? */
10183: retn = nd_f4(-2,0,0);
10184: }
10185: if ( !retn ) {
10186: /* failure */
10187: *rp = 0; return;
10188: }
10189: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10190: if ( DP_Print )
1.5 noro 10191: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10192: /* dp->p */
10193: nd_bpe = cbpe;
10194: nd_setup_parameters(nd_nvar,0);
10195: for ( r = cand; r; r = NEXT(r) ) {
10196: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10197: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10198: }
10199: MKLIST(*rp,cand);
10200: }
10201:
10202: NODE nd_f4_lf_trace_main(int m,int **indp)
10203: {
10204: int i,nh,stat,index;
10205: NODE r,rm,g;
10206: ND_pairs d,l,l0,t;
10207: ND spol,red;
10208: NDV nf,redv,nfqv,nfv;
10209: NM s0,s;
10210: NODE rp0,srp0,nflist,nflist_lf;
10211: int nsp,nred,col,rank,len,k,j,a;
10212: UINT c;
10213: UINT **spmat;
10214: UINT *s0vect,*svect,*p,*v;
10215: int *colstat;
10216: IndArray *imat;
10217: int *rhead;
10218: int spcol,sprow;
10219: int sugar;
10220: PGeoBucket bucket;
10221: struct oEGT eg0,eg1,eg_f4;
10222:
10223: g = 0; d = 0;
10224: for ( i = 0; i < nd_psn; i++ ) {
10225: d = update_pairs(d,g,i,0);
10226: g = update_base(g,i);
10227: }
10228: while ( d ) {
10229: get_eg(&eg0);
10230: l = nd_minsugarp(d,&d);
10231: sugar = SG(l);
10232: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10233: bucket = create_pbucket();
10234: stat = nd_sp_f4(m,0,l,bucket);
10235: if ( !stat ) {
10236: for ( t = l; NEXT(t); t = NEXT(t) );
10237: NEXT(t) = d; d = l;
10238: d = nd_reconstruct(1,d);
10239: continue;
10240: }
10241: if ( bucket->m < 0 ) continue;
10242: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10243: if ( !col ) {
10244: for ( t = l; NEXT(t); t = NEXT(t) );
10245: NEXT(t) = d; d = l;
10246: d = nd_reconstruct(1,d);
10247: continue;
10248: }
10249: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10250: if ( DP_Print )
1.5 noro 10251: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10252: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10253: if ( !l0 ) continue;
10254: l = l0;
10255:
10256: /* over LF */
10257: bucket = create_pbucket();
10258: stat = nd_sp_f4(-2,1,l,bucket);
10259: if ( !stat ) {
10260: for ( t = l; NEXT(t); t = NEXT(t) );
10261: NEXT(t) = d; d = l;
10262: d = nd_reconstruct(1,d);
10263: continue;
10264: }
10265: if ( bucket->m < 0 ) continue;
10266: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10267: if ( !col ) {
10268: for ( t = l; NEXT(t); t = NEXT(t) );
10269: NEXT(t) = d; d = l;
10270: d = nd_reconstruct(1,d);
10271: continue;
10272: }
10273: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10274: /* adding new bases */
10275: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10276: nfv = (NDV)BDY(rm);
10277: nfqv = (NDV)BDY(r);
10278: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10279: ndv_removecont(m,nfv);
10280: ndv_removecont(-2,nfqv);
1.24 noro 10281: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10282: d = update_pairs(d,g,nh,0);
10283: g = update_base(g,nh);
10284: }
10285: if ( r || rm ) return 0;
10286: }
10287: conv_ilist(nd_demand,1,g,indp);
10288: return g;
10289: }
10290:
1.7 noro 10291: #if SIZEOF_LONG==8
10292:
10293: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10294: {
10295: int j,k,len;
10296: UINT *p;
10297: UINT c;
10298: NDV r;
10299: NMV mr0,mr;
10300:
10301: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10302: if ( !len ) return 0;
10303: else {
10304: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10305: #if 0
10306: ndv_alloc += nmv_adv*len;
10307: #endif
10308: mr = mr0;
10309: p = s0vect;
10310: for ( j = k = 0; j < col; j++, p += nd_wpd )
10311: if ( !rhead[j] ) {
10312: if ( (c = (UINT)vect[k++]) != 0 ) {
10313: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10314: }
10315: }
10316: MKNDV(nd_nvar,mr0,len,r);
10317: return r;
10318: }
10319: }
10320:
1.28 noro 10321: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
10322: {
10323: int j,k,len;
10324: UINT *p;
10325: UINT c;
10326: NDV r;
10327: NMV mr0,mr;
10328:
10329: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
10330: if ( !len ) return 0;
10331: else {
10332: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10333: mr = mr0;
10334: p = s0vect;
10335: for ( j = k = 0; j < col; j++, p += nd_wpd )
10336: if ( (c = (UINT)vect[k++]) != 0 ) {
10337: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10338: }
10339: MKNDV(nd_nvar,mr0,len,r);
10340: return r;
10341: }
10342: }
10343:
1.7 noro 10344: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10345: {
10346: NM m;
1.11 noro 10347: UINT *t,*s,*u;
10348: int i,st,ed,md,prev,c;
1.7 noro 10349:
10350: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10351: prev = 0;
10352: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10353: t = DL(m);
10354: st = prev;
10355: ed = n;
10356: while ( ed > st ) {
10357: md = (st+ed)/2;
10358: u = s0+md*nd_wpd;
10359: c = DL_COMPARE(u,t);
10360: if ( c == 0 ) break;
10361: else if ( c > 0 ) st = md;
10362: else ed = md;
10363: }
10364: r[md] = (mp_limb_t)CM(m);
10365: prev = md;
1.7 noro 10366: }
10367: for ( i = 0; !r[i]; i++ );
10368: return i;
10369: }
10370:
10371: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10372:
1.28 noro 10373: 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 10374: {
10375: int i,j,k,len,pos,prev;
10376: mp_limb_t a,c,c1,c2;
10377: IndArray ivect;
10378: unsigned char *ivc;
10379: unsigned short *ivs;
10380: unsigned int *ivi;
10381: NDV redv;
10382: NMV mr;
10383: NODE rp;
10384: int maxrs;
10385:
10386: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10387: maxrs = 0;
10388: for ( i = 0; i < nred; i++ ) {
10389: ivect = imat[i];
10390: k = ivect->head;
10391: a = svect[k]; c = cvect[k];
10392: MOD128(a,c,m);
10393: svect[k] = a; cvect[k] = 0;
1.28 noro 10394: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 10395: Nf4_red++;
1.7 noro 10396: maxrs = MAX(maxrs,rp0[i]->sugar);
10397: c = m-c; redv = nd_ps[rp0[i]->index];
10398: len = LEN(redv); mr = BDY(redv);
10399: svect[k] = 0; prev = k;
10400: switch ( ivect->width ) {
10401: case 1:
10402: ivc = ivect->index.c;
10403: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10404: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10405: c2 = svect[pos]+c1*c;
10406: if ( c2 < svect[pos] ) cvect[pos]++;
10407: svect[pos] = c2;
1.7 noro 10408: }
10409: break;
10410: case 2:
10411: ivs = ivect->index.s;
10412: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10413: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10414: c2 = svect[pos]+c1*c;
10415: if ( c2 < svect[pos] ) cvect[pos]++;
10416: svect[pos] = c2;
1.7 noro 10417: }
10418: break;
10419: case 4:
10420: ivi = ivect->index.i;
10421: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10422: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10423: c2 = svect[pos]+c1*c;
10424: if ( c2 < svect[pos] ) cvect[pos]++;
10425: svect[pos] = c2;
1.7 noro 10426: }
10427: break;
10428: }
10429: }
10430: }
10431: for ( i = 0; i < col; i++ ) {
10432: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10433: }
10434: return maxrs;
10435: }
10436:
10437: /* for Fp, 2^15=<p<2^29 */
10438:
10439: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10440: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10441: {
10442: int spcol,sprow,a;
10443: int i,j,k,l,rank;
10444: NODE r0,r;
10445: ND_pairs sp;
10446: ND spol;
10447: mp_limb_t **spmat;
10448: mp_limb_t *svect,*cvect;
10449: mp_limb_t *v;
10450: int *colstat;
10451: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10452: int maxrs;
10453: int *spsugar;
10454: ND_pairs *spactive;
10455:
10456: spcol = col-nred;
10457: get_eg(&eg0);
10458: /* elimination (1st step) */
10459: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10460: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10461: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10462: spsugar = (int *)MALLOC(nsp*sizeof(int));
10463: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10464: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10465: nd_sp(m,0,sp,&spol);
10466: if ( !spol ) continue;
10467: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 10468: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 10469: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10470: if ( i < col ) {
10471: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10472: for ( j = k = 0; j < col; j++ )
10473: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10474: spsugar[sprow] = MAX(maxrs,SG(spol));
10475: if ( nz )
10476: spactive[sprow] = sp;
10477: sprow++;
10478: }
10479: nd_free(spol);
10480: }
1.12 noro 10481: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10482: if ( DP_Print ) {
10483: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10484: fflush(asir_out);
10485: }
10486: /* free index arrays */
10487: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10488:
10489: /* elimination (2nd step) */
10490: colstat = (int *)MALLOC(spcol*sizeof(int));
10491: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10492: r0 = 0;
10493: for ( i = 0; i < rank; i++ ) {
10494: NEXTNODE(r0,r); BDY(r) =
10495: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10496: SG((NDV)BDY(r)) = spsugar[i];
10497: GCFREE(spmat[i]);
10498: }
10499: if ( r0 ) NEXT(r) = 0;
10500:
10501: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10502: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10503: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10504: if ( DP_Print ) {
10505: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10506: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10507: nsp,nred,sprow,spcol,rank);
10508: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10509: }
10510: if ( nz ) {
10511: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10512: if ( rank > 0 ) {
10513: NEXT(spactive[rank-1]) = 0;
10514: *nz = spactive[0];
10515: } else
10516: *nz = 0;
10517: }
10518: return r0;
10519: }
10520:
10521: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10522: {
10523: int i,j,k,l,rank,s;
10524: mp_limb_t inv;
10525: mp_limb_t a;
10526: UINT c;
10527: mp_limb_t *t,*pivot,*pk;
10528: UINT *ck;
10529: UINT **cmat;
10530: UINT *ct;
10531: ND_pairs pair;
10532:
10533: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10534: for ( i = 0; i < row; i++ ) {
10535: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10536: bzero(cmat[i],col*sizeof(UINT));
10537: }
10538:
10539: for ( rank = 0, j = 0; j < col; j++ ) {
10540: for ( i = rank; i < row; i++ ) {
10541: a = mat[i][j]; c = cmat[i][j];
10542: MOD128(a,c,md);
10543: mat[i][j] = a; cmat[i][j] = 0;
10544: }
10545: for ( i = rank; i < row; i++ )
10546: if ( mat[i][j] )
10547: break;
10548: if ( i == row ) {
10549: colstat[j] = 0;
10550: continue;
10551: } else
10552: colstat[j] = 1;
10553: if ( i != rank ) {
10554: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10555: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10556: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10557: if ( spactive ) {
10558: pair = spactive[i]; spactive[i] = spactive[rank];
10559: spactive[rank] = pair;
10560: }
10561: }
10562: /* column j is normalized */
10563: s = sugar[rank];
10564: inv = invm((UINT)mat[rank][j],md);
10565: /* normalize pivot row */
10566: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10567: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10568: }
10569: for ( i = rank+1; i < row; i++ ) {
10570: if ( (a = mat[i][j]) != 0 ) {
10571: sugar[i] = MAX(sugar[i],s);
10572: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10573: Nf4_red++;
1.7 noro 10574: }
10575: }
10576: rank++;
10577: }
10578: for ( j = col-1, l = rank-1; j >= 0; j-- )
10579: if ( colstat[j] ) {
10580: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10581: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10582: }
10583: s = sugar[l];
10584: for ( i = 0; i < l; i++ ) {
10585: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10586: if ( a ) {
10587: sugar[i] = MAX(sugar[i],s);
10588: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10589: Nf4_red++;
1.7 noro 10590: }
10591: }
10592: l--;
10593: }
10594: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10595: GCFREE(cmat);
10596: return rank;
10597: }
10598: #endif
10599:
1.28 noro 10600: 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)
10601: {
10602: int i,j,k,l,rank,s,imin;
10603: mp_limb_t inv;
10604: mp_limb_t a;
10605: UINT c;
10606: mp_limb_t *t,*pivot,*pk;
10607: UINT *ck;
10608: UINT **cmat;
10609: UINT *ct;
10610: ND_pairs pair;
10611: SIG sg;
1.31 noro 10612: int *used;
1.28 noro 10613:
1.31 noro 10614: used = (int *)MALLOC(row*sizeof(int));
1.28 noro 10615: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10616: for ( i = 0; i < row; i++ ) {
10617: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10618: bzero(cmat[i],col*sizeof(UINT));
10619: }
10620:
1.31 noro 10621: for ( j = 0; j < col; j++ ) {
10622: for ( i = 0; i < row; i++ ) {
1.28 noro 10623: a = mat[i][j]; c = cmat[i][j];
10624: MOD128(a,c,md);
10625: mat[i][j] = a; cmat[i][j] = 0;
10626: }
1.31 noro 10627: for ( i = 0; i < row; i++ )
10628: if ( !used[i] && mat[i][j] ) break;
10629: if ( i == row ) {
1.28 noro 10630: colstat[j] = 0;
10631: continue;
1.31 noro 10632: } else {
1.28 noro 10633: colstat[j] = 1;
1.31 noro 10634: used[i] = 1;
1.28 noro 10635: }
10636: /* column j is normalized */
1.31 noro 10637: s = sugar[i];
10638: inv = invm((UINT)mat[i][j],md);
1.28 noro 10639: /* normalize pivot row */
1.31 noro 10640: for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28 noro 10641: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10642: }
1.31 noro 10643: for ( k = i+1; k < row; k++ ) {
10644: if ( (a = mat[k][j]) != 0 ) {
10645: sugar[k] = MAX(sugar[k],s);
10646: red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28 noro 10647: Nf4_red++;
10648: }
10649: }
10650: }
1.31 noro 10651: rank = 0;
10652: for ( i = 0; i < row; i++ ) {
10653: for ( j = 0; j < col; j++ )
10654: if ( mat[i][j] ) break;
10655: if ( j == col ) sugar[i] = -1;
10656: else rank++;
10657: }
1.28 noro 10658: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10659: GCFREE(cmat);
10660: return rank;
10661: }
10662:
10663: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10664: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
10665: {
10666: int spcol,sprow,a;
10667: int i,j,k,l,rank;
10668: NODE r0,r;
10669: ND_pairs sp;
10670: ND spol;
10671: mp_limb_t **spmat;
10672: mp_limb_t *svect,*cvect;
10673: mp_limb_t *v;
10674: int *colstat;
10675: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10676: int maxrs;
10677: int *spsugar;
10678: ND_pairs *spactive;
10679: SIG *spsig;
10680:
10681: get_eg(&eg0);
10682: /* elimination (1st step) */
10683: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10684: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10685: spsugar = (int *)MALLOC(nsp*sizeof(int));
10686: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
10687: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10688: nd_sp(m,0,sp,&spol);
10689: if ( !spol ) {
1.29 noro 10690: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 10691: continue;
10692: }
10693: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10694: nd_to_vect64(m,s0vect,col,spol,svect);
10695: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
10696: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10697: if ( i < col ) {
10698: spmat[sprow] = svect;
10699: spsugar[sprow] = MAX(maxrs,SG(spol));
10700: spsig[sprow] = sp->sig;
10701: sprow++;
10702: } else {
1.29 noro 10703: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 10704: }
10705: nd_free(spol);
10706: }
10707: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
10708: if ( DP_Print ) {
10709: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10710: fflush(asir_out);
10711: }
10712: /* free index arrays */
10713: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10714:
10715: /* elimination (2nd step) */
10716: colstat = (int *)MALLOC(col*sizeof(int));
10717: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
10718: r0 = 0;
1.31 noro 10719: for ( i = 0; i < sprow; i++ ) {
10720: if ( spsugar[i] >= 0 ) {
10721: NEXTNODE(r0,r);
10722: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
10723: SG((NDV)BDY(r)) = spsugar[i];
10724: ((NDV)BDY(r))->sig = spsig[i];
10725: } else
10726: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 10727: GCFREE(spmat[i]);
10728: }
10729: if ( r0 ) NEXT(r) = 0;
10730: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
10731: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10732: if ( DP_Print ) {
10733: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10734: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10735: nsp,nred,sprow,col,rank);
10736: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10737: }
10738: return r0;
10739: }
10740:
10741: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
10742: {
10743: IndArray *imat;
10744: int nsp,nred,i,start;
10745: int *rhead;
10746: NODE r0,rp;
10747: ND_pairs sp;
10748: NM_ind_pair *rvect;
10749: UINT *s;
10750: int *s0hash;
10751: struct oEGT eg0,eg1,eg_conv;
10752:
10753: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
10754: nred = length(rp0);
10755: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
10756: rhead = (int *)MALLOC(col*sizeof(int));
10757: for ( i = 0; i < col; i++ ) rhead[i] = 0;
10758:
10759: /* construction of index arrays */
10760: get_eg(&eg0);
10761: if ( DP_Print ) {
10762: fprintf(asir_out,"%dx%d,",nsp+nred,col);
10763: fflush(asir_out);
10764: }
10765: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
10766: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
10767: rvect[i] = (NM_ind_pair)BDY(rp);
10768: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
10769: rhead[imat[i]->head] = 1;
10770: start = imat[i]->head;
10771: }
10772: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
10773: if ( DP_Print ) {
10774: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
10775: fflush(asir_out);
10776: }
10777: if ( m > 0 )
10778: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
10779: else
10780: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
10781: error("nd_f4_red_q_main_s : not implemented yet");
10782: return r0;
10783: }
10784:
10785: INLINE int ndl_find_reducer_minsig(UINT *dg)
10786: {
10787: RHist r;
10788: int i,singular,ret,d,k,imin;
10789: SIG t;
10790: static int wpd,nvar;
10791: static SIG quo,quomin;
10792: static UINT *tmp;
10793:
10794: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
10795: if ( wpd != nd_wpd ) {
10796: wpd = nd_wpd;
10797: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
10798: }
10799: #if 0
10800: d = ndl_hash_value(dg);
10801: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
10802: if ( ndl_equal(dg,DL(r)) ) {
10803: return r->index;
10804: }
10805: }
10806: #endif
10807: imin = -1;
10808: for ( i = 0; i < nd_psn; i++ ) {
10809: r = nd_psh[i];
10810: if ( ndl_reducible(dg,DL(r)) ) {
10811: ndl_sub(dg,DL(r),tmp);
10812: _ndltodl(tmp,DL(quo));
10813: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
10814: quo->pos = nd_psh[i]->sig->pos;
10815: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
10816: t = quo; quo = quomin; quomin = t;
10817: imin = i;
10818: }
10819: }
10820: }
10821: if ( imin == -1 ) return nd_psn;
10822: else {
10823: #if 0
10824: nd_append_red(dg,i);
10825: #endif
10826: return imin;
10827: }
10828: }
10829:
10830: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
10831: {
10832: NODE rp0,rp;
10833: NM mul,head,s0,s;
10834: int index,col,i,sugar;
10835: RHist h;
10836: UINT *s0v,*p;
10837: NM_ind_pair pair;
10838: ND red;
10839: NDV *ps;
10840: SIG sig;
10841:
10842: s0 = 0; rp0 = 0; col = 0;
10843: if ( nd_demand )
10844: ps = trace?nd_ps_trace_sym:nd_ps_sym;
10845: else
10846: ps = trace?nd_ps_trace:nd_ps;
10847: while ( 1 ) {
10848: head = remove_head_pbucket_symbolic(bucket);
10849: if ( !head ) break;
10850: if ( !s0 ) s0 = head;
10851: else NEXT(s) = head;
10852: s = head;
10853: index = ndl_find_reducer_minsig(DL(head));
10854: if ( index >= 0 && index < nd_psn ) {
10855: h = nd_psh[index];
10856: NEWNM(mul);
10857: ndl_sub(DL(head),DL(h),DL(mul));
10858: if ( ndl_check_bound2(index,DL(mul)) )
10859: return 0;
10860: sugar = TD(DL(mul))+SG(ps[index]);
10861: NEWSIG(sig);
10862: _ndltodl(DL(mul),DL(sig));
10863: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
10864: sig->pos = nd_psh[index]->sig->pos;
10865: MKNM_ind_pair(pair,mul,index,sugar,sig);
10866: red = ndv_mul_nm_symbolic(mul,ps[index]);
10867: add_pbucket_symbolic(bucket,nd_remove_head(red));
10868: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
10869: }
10870: col++;
10871: }
10872: if ( rp0 ) NEXT(rp) = 0;
10873: NEXT(s) = 0;
10874: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
10875: for ( i = 0, p = s0v, s = s0; i < col;
10876: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
10877: *s0vect = s0v;
10878: *r = rp0;
10879:
10880: return col;
10881: }
10882:
10883: NODE nd_sba_f4(int m,int **indp)
10884: {
1.34 noro 10885: int i,nh,stat,index,f4red,f4step;
1.33 noro 10886: int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
10887: NODE r,g,rp0,nflist;
10888: ND_pairs d,l,t;
10889: ND h,nf;
10890: NDV nfv;
10891: union oNDC hc;
10892: UINT *s0vect;
1.28 noro 10893: UINT c;
10894: PGeoBucket bucket;
1.33 noro 10895: NODE *syzlist;
10896: SIG sig;
1.28 noro 10897: struct oEGT eg0,eg1,eg_f4;
1.33 noro 10898: struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28 noro 10899:
10900: Nf4_red=0;
1.30 noro 10901: d = 0;
1.29 noro 10902: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 10903: for ( i = 0; i < nd_psn; i++ ) {
1.30 noro 10904: d = update_pairs_s(d,i,syzlist);
1.28 noro 10905: }
1.30 noro 10906: nd_nbase = nd_psn;
1.28 noro 10907: f4red = 1;
1.33 noro 10908: psugar = 0;
1.34 noro 10909: f4step = 0;
1.28 noro 10910: while ( d ) {
1.33 noro 10911: for ( t = d, ms = SG(d); t; t = NEXT(t) )
10912: if ( SG(t) < ms ) ms = SG(t);
1.34 noro 10913: if ( ms == psugar && f4step >= 2 ) {
1.33 noro 10914: again:
10915: l = d; d = d->next;
10916: if ( small_lcm(l) ) {
10917: if ( DP_Print ) fprintf(asir_out,"M");
10918: continue;
10919: }
10920: sig = l->sig;
10921: stat = nd_sp(m,0,l,&h);
10922: if ( !stat ) {
10923: NEXT(l) = d; d = l;
10924: d = nd_reconstruct(0,d);
10925: goto again;
10926: }
10927: get_eg(&eg1);
10928: #if USE_GEOBUCKET
10929: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
10930: #else
10931: stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
10932: #endif
10933: get_eg(&eg2);
10934: if ( !stat ) {
10935: NEXT(l) = d; d = l;
10936: d = nd_reconstruct(0,d);
10937: goto again;
10938: } else if ( stat == -1 ) {
10939: if ( DP_Print ) { printf("S"); fflush(stdout); }
10940: FREENDP(l);
10941: } else if ( nf ) {
10942: if ( DP_Print ) { printf("+"); fflush(stdout); }
10943: add_eg(&eg_nf,&eg1,&eg2);
10944: hc = HCU(nf);
10945: nd_removecont(m,nf);
10946: nfv = ndtondv(m,nf); nd_free(nf);
10947: nh = ndv_newps(m,nfv,0);
10948:
1.30 noro 10949: d = update_pairs_s(d,nh,syzlist);
10950: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33 noro 10951: FREENDP(l);
1.28 noro 10952: } else {
1.33 noro 10953: add_eg(&eg_nfzero,&eg1,&eg2);
10954: // syzygy
10955: get_eg(&eg1);
10956: d = remove_spair_s(d,sig);
10957: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 10958: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33 noro 10959: if ( DP_Print ) { printf("."); fflush(stdout); }
10960: FREENDP(l);
10961: }
10962: } else {
1.34 noro 10963: if ( ms != psugar ) f4step = 1;
10964: else f4step++;
1.33 noro 10965: again2:
10966: psugar = ms;
10967: l = nd_minsugarp_s(d,&d);
10968: sugar = nd_sugarweight?d->sugar2:SG(d);
10969: bucket = create_pbucket();
10970: stat = nd_sp_f4(m,0,l,bucket);
10971: if ( !stat ) {
10972: for ( t = l; NEXT(t); t = NEXT(t) );
10973: NEXT(t) = d; d = l;
10974: d = nd_reconstruct(0,d);
10975: goto again2;
10976: }
10977: if ( bucket->m < 0 ) continue;
10978: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
10979: if ( !col ) {
1.37 ! noro 10980: for ( t = l; NEXT(t); t = NEXT(t) )
! 10981: ;
! 10982: NEXT(t) = d; d = l;
1.33 noro 10983: d = nd_reconstruct(0,d);
10984: goto again2;
10985: }
1.34 noro 10986: if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33 noro 10987: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
10988: /* adding new bases */
10989: for ( r = nflist; r; r = NEXT(r) ) {
10990: nfv = (NDV)BDY(r);
10991: ndv_removecont(m,nfv);
10992: nh = ndv_newps(m,nfv,0);
10993: d = update_pairs_s(d,nh,syzlist);
10994: nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
10995: }
10996: for ( i = 0; i < nd_nbase; i++ )
10997: for ( r = syzlist[i]; r; r = NEXT(r) )
10998: d = remove_spair_s(d,(SIG)BDY(r));
10999: d = remove_large_lcm(d);
11000: if ( DP_Print ) {
1.34 noro 11001: fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33 noro 11002: }
11003: f4red++;
1.28 noro 11004: }
11005: }
11006: if ( DP_Print ) {
1.34 noro 11007: fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28 noro 11008: }
1.30 noro 11009: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 11010: return g;
11011: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>