Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.30
1.30 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.29 2020/07/02 09:24:17 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.26 noro 21: int Nsyz;
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);
789: int _eqdl(int n,DL d1,DL d2);
790:
791: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
792: {
793: int pos1,pos2,t,j;
794: DMM *in;
795: DMMstack s;
796: static DL d1=0;
797: static DL d2=0;
798: static int dlen=0;
799:
800: pos1 = MPOS(m1); pos2 = MPOS(m2);
801: if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
802: if ( nd_nvar > dlen ) {
803: NEWDL(d1,nd_nvar);
804: NEWDL(d2,nd_nvar);
805: dlen = nd_nvar;
806: }
807: d1->td = TD(m1);
808: for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
809: d2->td = TD(m2);
810: for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
811: for ( s = dmm_stack; s; s = NEXT(s) ) {
812: in = s->in;
813: _addtodl(nd_nvar,in[pos1]->dl,d1);
814: _addtodl(nd_nvar,in[pos2]->dl,d2);
815: if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
816: if ( pos1 < pos2 ) return 1;
817: else if ( pos1 > pos2 ) return -1;
818: else return 0;
819: }
820: pos1 = in[pos1]->pos;
821: pos2 = in[pos2]->pos;
822: if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
823: }
824: // comparison by the bottom order
825: LAST:
826: switch ( nd_base_ordtype ) {
827: case 0:
828: t = (*dl_base_compare_function)(nd_nvar,d1,d2);
829: if ( t ) return t;
830: else if ( pos1 < pos2 ) return 1;
831: else if ( pos1 > pos2 ) return -1;
832: else return 0;
833: break;
834: case 1:
835: if ( pos1 < pos2 ) return 1;
836: else if ( pos1 > pos2 ) return -1;
837: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
838: break;
839: case 2:
840: if ( d1->td > d2->td ) return 1;
841: else if ( d1->td < d2->td ) return -1;
842: else if ( pos1 < pos2 ) return 1;
843: else if ( pos1 > pos2 ) return -1;
844: else return (*dl_base_compare_function)(nd_nvar,d1,d2);
845: break;
846: default:
847: error("ndl_schreyer_compare : invalid base ordtype");
1.28 noro 848: return 0;
1.21 noro 849: }
1.1 noro 850: }
851:
852: INLINE int ndl_equal(UINT *d1,UINT *d2)
853: {
854: int i;
855:
856: switch ( nd_wpd ) {
857: case 2:
858: if ( TD(d2) != TD(d1) ) return 0;
859: if ( d2[1] != d1[1] ) return 0;
860: return 1;
861: break;
862: case 3:
863: if ( TD(d2) != TD(d1) ) return 0;
864: if ( d2[1] != d1[1] ) return 0;
865: if ( d2[2] != d1[2] ) return 0;
866: return 1;
867: break;
868: default:
869: for ( i = 0; i < nd_wpd; i++ )
870: if ( *d1++ != *d2++ ) return 0;
871: return 1;
872: break;
873: }
874: }
875:
876: INLINE void ndl_copy(UINT *d1,UINT *d2)
877: {
878: int i;
879:
880: switch ( nd_wpd ) {
881: case 2:
882: TD(d2) = TD(d1);
883: d2[1] = d1[1];
884: break;
885: case 3:
886: TD(d2) = TD(d1);
887: d2[1] = d1[1];
888: d2[2] = d1[2];
889: break;
890: default:
891: for ( i = 0; i < nd_wpd; i++ )
892: d2[i] = d1[i];
893: break;
894: }
895: }
896:
897: INLINE void ndl_zero(UINT *d)
898: {
899: int i;
900: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
901: }
902:
903: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
904: {
905: int i;
906:
907: if ( nd_module ) {
908: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
909: error("ndl_add : invalid operation");
910: }
911: #if 1
912: switch ( nd_wpd ) {
913: case 2:
914: TD(d) = TD(d1)+TD(d2);
915: d[1] = d1[1]+d2[1];
916: break;
917: case 3:
918: TD(d) = TD(d1)+TD(d2);
919: d[1] = d1[1]+d2[1];
920: d[2] = d1[2]+d2[2];
921: break;
922: default:
923: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
924: break;
925: }
926: #else
927: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
928: #endif
929: }
930:
931: /* d1 += d2 */
932: INLINE void ndl_addto(UINT *d1,UINT *d2)
933: {
934: int i;
935:
936: if ( nd_module ) {
937: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
938: error("ndl_addto : invalid operation");
939: }
940: #if 1
941: switch ( nd_wpd ) {
942: case 2:
943: TD(d1) += TD(d2);
944: d1[1] += d2[1];
945: break;
946: case 3:
947: TD(d1) += TD(d2);
948: d1[1] += d2[1];
949: d1[2] += d2[2];
950: break;
951: default:
952: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
953: break;
954: }
955: #else
956: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
957: #endif
958: }
959:
960: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
961: {
962: int i;
963:
964: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
965: }
966:
967: int ndl_disjoint(UINT *d1,UINT *d2)
968: {
969: UINT t1,t2,u,u1,u2;
970: int i,j;
971:
972: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
973: #if USE_UNROLL
974: switch ( nd_bpe ) {
975: case 3:
976: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
977: u1 = d1[i]; u2 = d2[i];
978: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
979: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
980: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
981: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
982: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
983: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
984: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
985: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
986: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
987: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
988: }
989: return 1;
990: break;
991: case 4:
992: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
993: u1 = d1[i]; u2 = d2[i];
994: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
995: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
996: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
997: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
998: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
999: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
1000: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
1001: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
1002: }
1003: return 1;
1004: break;
1005: case 6:
1006: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1007: u1 = d1[i]; u2 = d2[i];
1008: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1009: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
1010: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
1011: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
1012: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
1013: }
1014: return 1;
1015: break;
1016: case 8:
1017: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1018: u1 = d1[i]; u2 = d2[i];
1019: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1020: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
1021: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
1022: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
1023: }
1024: return 1;
1025: break;
1026: case 16:
1027: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1028: u1 = d1[i]; u2 = d2[i];
1029: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1030: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1031: }
1032: return 1;
1033: break;
1034: case 32:
1035: for ( i = nd_exporigin; i < nd_wpd; i++ )
1036: if ( d1[i] && d2[i] ) return 0;
1037: return 1;
1038: break;
1039: default:
1040: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1041: u1 = d1[i]; u2 = d2[i];
1042: for ( j = 0; j < nd_epw; j++ ) {
1043: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1044: u1 >>= nd_bpe; u2 >>= nd_bpe;
1045: }
1046: }
1047: return 1;
1048: break;
1049: }
1050: #else
1051: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1052: u1 = d1[i]; u2 = d2[i];
1053: for ( j = 0; j < nd_epw; j++ ) {
1054: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1055: u1 >>= nd_bpe; u2 >>= nd_bpe;
1056: }
1057: }
1058: return 1;
1059: #endif
1060: }
1061:
1062: int ndl_check_bound(UINT *d1,UINT *d2)
1063: {
1064: UINT u2;
1065: int i,j,ind,k;
1066:
1067: ind = 0;
1068: #if USE_UNROLL
1069: switch ( nd_bpe ) {
1070: case 3:
1071: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1072: u2 = d2[i];
1073: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1074: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1075: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1076: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1077: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1078: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1079: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1080: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1081: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1082: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1083: }
1084: return 0;
1085: break;
1086: case 4:
1087: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1088: u2 = d2[i];
1089: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1090: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1091: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1092: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1093: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1094: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1095: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1096: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1097: }
1098: return 0;
1099: break;
1100: case 6:
1101: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1102: u2 = d2[i];
1103: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1104: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1105: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1106: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1107: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1108: }
1109: return 0;
1110: break;
1111: case 8:
1112: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1113: u2 = d2[i];
1114: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1115: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1116: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1117: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1118: }
1119: return 0;
1120: break;
1121: case 16:
1122: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1123: u2 = d2[i];
1124: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1125: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1126: }
1127: return 0;
1128: break;
1129: case 32:
1130: for ( i = nd_exporigin; i < nd_wpd; i++ )
1131: if ( d1[i]+d2[i]<d1[i] ) return 1;
1132: return 0;
1133: break;
1134: default:
1135: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1136: u2 = d2[i];
1137: k = (nd_epw-1)*nd_bpe;
1138: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1139: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1140: }
1141: return 0;
1142: break;
1143: }
1144: #else
1145: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1146: u2 = d2[i];
1147: k = (nd_epw-1)*nd_bpe;
1148: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1149: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1150: }
1151: return 0;
1152: #endif
1153: }
1154:
1155: int ndl_check_bound2(int index,UINT *d2)
1156: {
1157: return ndl_check_bound(nd_bound[index],d2);
1158: }
1159:
1160: INLINE int ndl_hash_value(UINT *d)
1161: {
1162: int i;
1.11 noro 1163: UINT r;
1.1 noro 1164:
1165: r = 0;
1166: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1167: r = (r*1511+d[i]);
1.11 noro 1168: r %= REDTAB_LEN;
1.1 noro 1169: return r;
1170: }
1171:
1172: INLINE int ndl_find_reducer(UINT *dg)
1173: {
1174: RHist r;
1175: int d,k,i;
1176:
1177: d = ndl_hash_value(dg);
1178: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1179: if ( ndl_equal(dg,DL(r)) ) {
1180: if ( k > 0 ) nd_notfirst++;
1181: nd_found++;
1182: return r->index;
1183: }
1184: }
1185: if ( Reverse )
1186: for ( i = nd_psn-1; i >= 0; i-- ) {
1187: r = nd_psh[i];
1188: if ( ndl_reducible(dg,DL(r)) ) {
1189: nd_create++;
1190: nd_append_red(dg,i);
1191: return i;
1192: }
1193: }
1194: else
1195: for ( i = 0; i < nd_psn; i++ ) {
1196: r = nd_psh[i];
1197: if ( ndl_reducible(dg,DL(r)) ) {
1198: nd_create++;
1199: nd_append_red(dg,i);
1200: return i;
1201: }
1202: }
1203: return -1;
1204: }
1205:
1.24 noro 1206: // ret=0,...,nd_psn-1 => reducer found
1207: // ret=nd_psn => reducer not found
1208: // ret=-1 => singular top reducible
1209:
1210: int comp_sig(SIG s1,SIG s2);
1211: void _ndltodl(UINT *ndl,DL dl);
1212:
1213: void print_sig(SIG s)
1214: {
1215: int i;
1216:
1217: fprintf(asir_out,"<<");
1218: for ( i = 0; i < nd_nvar; i++ ) {
1219: fprintf(asir_out,"%d",s->dl->d[i]);
1220: if ( i != nd_nvar-1 ) fprintf(asir_out,",");
1221: }
1222: fprintf(asir_out,">>*e%d",s->pos);
1223: }
1224:
1225: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
1226: {
1227: RHist r;
1.27 noro 1228: int i,singular,ret,d,k;
1.26 noro 1229: static int wpd,nvar;
1.24 noro 1230: static SIG quo;
1231: static UINT *tmp;
1232:
1.26 noro 1233: if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
1234: if ( wpd != nd_wpd ) {
1.24 noro 1235: wpd = nd_wpd;
1236: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
1237: }
1.27 noro 1238: d = ndl_hash_value(dg);
1239: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1240: if ( ndl_equal(dg,DL(r)) ) {
1241: return r->index;
1242: }
1243: }
1.24 noro 1244: singular = 0;
1245: for ( i = 0; i < nd_psn; i++ ) {
1246: r = nd_psh[i];
1247: if ( ndl_reducible(dg,DL(r)) ) {
1.27 noro 1248: ndl_sub(dg,DL(r),tmp);
1.24 noro 1249: _ndltodl(tmp,DL(quo));
1250: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
1251: quo->pos = nd_psh[i]->sig->pos;
1252: ret = comp_sig(sig,quo);
1253: if ( ret > 0 ) { singular = 0; break; }
1.29 noro 1254: if ( ret == 0 ) { fprintf(asir_out,"s"); fflush(asir_out); singular = 1; }
1.24 noro 1255: }
1256: }
1257: if ( singular ) return -1;
1.27 noro 1258: else if ( i < nd_psn )
1259: nd_append_red(dg,i);
1260: return i;
1.24 noro 1261: }
1262:
1.1 noro 1263: ND nd_merge(ND p1,ND p2)
1264: {
1265: int n,c;
1266: int t,can,td1,td2;
1267: ND r;
1268: NM m1,m2,mr0,mr,s;
1269:
1270: if ( !p1 ) return p2;
1271: else if ( !p2 ) return p1;
1272: else {
1273: can = 0;
1274: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1275: c = DL_COMPARE(DL(m1),DL(m2));
1276: switch ( c ) {
1277: case 0:
1278: s = m1; m1 = NEXT(m1);
1279: can++; NEXTNM2(mr0,mr,s);
1280: s = m2; m2 = NEXT(m2); FREENM(s);
1281: break;
1282: case 1:
1283: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1284: break;
1285: case -1:
1286: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1287: break;
1288: }
1289: }
1290: if ( !mr0 )
1291: if ( m1 ) mr0 = m1;
1292: else if ( m2 ) mr0 = m2;
1293: else return 0;
1294: else if ( m1 ) NEXT(mr) = m1;
1295: else if ( m2 ) NEXT(mr) = m2;
1296: else NEXT(mr) = 0;
1297: BDY(p1) = mr0;
1298: SG(p1) = MAX(SG(p1),SG(p2));
1299: LEN(p1) = LEN(p1)+LEN(p2)-can;
1300: FREEND(p2);
1301: return p1;
1302: }
1303: }
1304:
1305: ND nd_add(int mod,ND p1,ND p2)
1306: {
1307: int n,c;
1308: int t,can,td1,td2;
1309: ND r;
1310: NM m1,m2,mr0,mr,s;
1311:
1.11 noro 1312: Nnd_add++;
1.1 noro 1313: if ( !p1 ) return p2;
1314: else if ( !p2 ) return p1;
1315: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1316: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1317: else if ( !mod ) return nd_add_q(p1,p2);
1318: else {
1319: can = 0;
1320: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1321: c = DL_COMPARE(DL(m1),DL(m2));
1322: switch ( c ) {
1323: case 0:
1324: t = ((CM(m1))+(CM(m2))) - mod;
1325: if ( t < 0 ) t += mod;
1326: s = m1; m1 = NEXT(m1);
1327: if ( t ) {
1328: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1329: } else {
1330: can += 2; FREENM(s);
1331: }
1332: s = m2; m2 = NEXT(m2); FREENM(s);
1333: break;
1334: case 1:
1335: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1336: break;
1337: case -1:
1338: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1339: break;
1340: }
1341: }
1342: if ( !mr0 )
1343: if ( m1 ) mr0 = m1;
1344: else if ( m2 ) mr0 = m2;
1345: else return 0;
1346: else if ( m1 ) NEXT(mr) = m1;
1347: else if ( m2 ) NEXT(mr) = m2;
1348: else NEXT(mr) = 0;
1349: BDY(p1) = mr0;
1350: SG(p1) = MAX(SG(p1),SG(p2));
1351: LEN(p1) = LEN(p1)+LEN(p2)-can;
1352: FREEND(p2);
1353: return p1;
1354: }
1355: }
1356:
1357: /* XXX on opteron, the inlined manipulation of destructive additon of
1358: * two NM seems to make gcc optimizer get confused, so the part is
1359: * done in a function.
1360: */
1361:
1362: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1363: {
1364: NM s;
1365: P t;
1366: int can;
1367:
1368: addp(nd_vc,CP(*m1),CP(*m2),&t);
1369: s = *m1; *m1 = NEXT(*m1);
1370: if ( t ) {
1371: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1372: } else {
1373: can = 2; FREENM(s);
1374: }
1375: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1376: return can;
1377: }
1378:
1379: ND nd_add_q(ND p1,ND p2)
1380: {
1381: int n,c,can;
1382: ND r;
1383: NM m1,m2,mr0,mr,s;
1384: P t;
1385:
1386: if ( !p1 ) return p2;
1387: else if ( !p2 ) return p1;
1388: else {
1389: can = 0;
1390: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1391: c = DL_COMPARE(DL(m1),DL(m2));
1392: switch ( c ) {
1393: case 0:
1394: #if defined(__x86_64__)
1395: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1396: #else
1397: addp(nd_vc,CP(m1),CP(m2),&t);
1398: s = m1; m1 = NEXT(m1);
1399: if ( t ) {
1400: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1401: } else {
1402: can += 2; FREENM(s);
1403: }
1404: s = m2; m2 = NEXT(m2); FREENM(s);
1405: #endif
1406: break;
1407: case 1:
1408: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1409: break;
1410: case -1:
1411: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1412: break;
1413: }
1414: }
1415: if ( !mr0 )
1416: if ( m1 ) mr0 = m1;
1417: else if ( m2 ) mr0 = m2;
1418: else return 0;
1419: else if ( m1 ) NEXT(mr) = m1;
1420: else if ( m2 ) NEXT(mr) = m2;
1421: else NEXT(mr) = 0;
1422: BDY(p1) = mr0;
1423: SG(p1) = MAX(SG(p1),SG(p2));
1424: LEN(p1) = LEN(p1)+LEN(p2)-can;
1425: FREEND(p2);
1426: return p1;
1427: }
1428: }
1429:
1430: ND nd_add_sf(ND p1,ND p2)
1431: {
1432: int n,c,can;
1433: ND r;
1434: NM m1,m2,mr0,mr,s;
1435: int t;
1436:
1437: if ( !p1 ) return p2;
1438: else if ( !p2 ) return p1;
1439: else {
1440: can = 0;
1441: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1442: c = DL_COMPARE(DL(m1),DL(m2));
1443: switch ( c ) {
1444: case 0:
1445: t = _addsf(CM(m1),CM(m2));
1446: s = m1; m1 = NEXT(m1);
1447: if ( t ) {
1448: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1449: } else {
1450: can += 2; FREENM(s);
1451: }
1452: s = m2; m2 = NEXT(m2); FREENM(s);
1453: break;
1454: case 1:
1455: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1456: break;
1457: case -1:
1458: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1459: break;
1460: }
1461: }
1462: if ( !mr0 )
1463: if ( m1 ) mr0 = m1;
1464: else if ( m2 ) mr0 = m2;
1465: else return 0;
1466: else if ( m1 ) NEXT(mr) = m1;
1467: else if ( m2 ) NEXT(mr) = m2;
1468: else NEXT(mr) = 0;
1469: BDY(p1) = mr0;
1470: SG(p1) = MAX(SG(p1),SG(p2));
1471: LEN(p1) = LEN(p1)+LEN(p2)-can;
1472: FREEND(p2);
1473: return p1;
1474: }
1475: }
1476:
1477:
1478: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1479: {
1480: int c,c1,c2;
1481: Z cg,cred,gcd,tq;
1482: P cgp,credp,gcdp;
1483: Obj tr,tr1;
1484:
1485: if ( mod == -1 ) {
1486: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1487: *divp = (Obj)ONE;
1488: } else if ( mod == -2 ) {
1489: Z inv,t;
1490: divlf(ONE,HCZ(p),&inv);
1491: chsgnlf(HCZ(g),&t);
1492: mullf(inv,t,&CZ(mul));
1493: *divp = (Obj)ONE;
1494: } else if ( mod ) {
1495: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1496: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1497: *divp = (Obj)ONE;
1498: } else if ( nd_vc ) {
1499: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1500: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1501: chsgnp(cgp,&CP(mul));
1502: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1503: if ( dn ) {
1504: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1505: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1506: }
1507: *divp = (Obj)credp;
1508: } else {
1.6 noro 1509: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1510: chsgnz(cg,&CZ(mul));
1.1 noro 1511: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1512: if ( dn ) {
1513: mulz(dn->z,cred,&tq); dn->z = tq;
1514: }
1515: *divp = (Obj)cred;
1516: }
1517: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1518: }
1519:
1520: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1521: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1522: {
1523: NM m,mrd,tail;
1524: NM mul;
1525: int n,sugar,psugar,sugar0,stat,index;
1526: int c,c1,c2,dummy;
1527: RHist h;
1528: NDV p,red;
1529: Q cg,cred,gcd,tq,qq;
1530: Z iq;
1531: DP dmul;
1532: NODE node;
1533: LIST hist;
1534: double hmag;
1535: P tp,tp1;
1536: Obj tr,tr1,div;
1537: union oNDC hg;
1538: P cont;
1539:
1540: if ( !g ) {
1541: *rp = d;
1542: return 1;
1543: }
1544: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1545:
1546: sugar0 = sugar = SG(g);
1547: n = NV(g);
1548: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1549: if ( d )
1550: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1551: for ( ; g; ) {
1552: index = ndl_find_reducer(HDL(g));
1553: if ( index >= 0 ) {
1554: h = nd_psh[index];
1555: ndl_sub(HDL(g),DL(h),DL(mul));
1556: if ( ndl_check_bound2(index,DL(mul)) ) {
1557: nd_free(g); nd_free(d);
1558: return 0;
1559: }
1560: p = nd_demand ? ndv_load(index) : ps[index];
1561: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1562: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1563: if ( nd_gentrace ) {
1564: /* Trace=[div,index,mul,ONE] */
1.6 noro 1565: STOZ(index,iq);
1.1 noro 1566: nmtodp(mod,mul,&dmul);
1567: node = mknode(4,div,iq,dmul,ONE);
1568: }
1569: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1570: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1571: hg = HCU(g);
1572: nd_removecont2(d,g);
1.6 noro 1573: if ( nd_gentrace ) {
1.1 noro 1574: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1575: /* exact division */
1.1 noro 1576: cont = ndc_div(mod,hg,HCU(g));
1577: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1578: }
1579: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1580: }
1581: MKLIST(hist,node);
1582: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1583: } else if ( !full ) {
1584: *rp = g;
1585: return 1;
1586: } else {
1587: m = BDY(g);
1588: if ( NEXT(m) ) {
1589: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1590: } else {
1591: FREEND(g); g = 0;
1592: }
1593: if ( d ) {
1594: NEXT(tail)=m; tail=m; LEN(d)++;
1595: } else {
1596: MKND(n,m,1,d); tail = BDY(d);
1597: }
1598: }
1599: }
1600: if ( d ) SG(d) = sugar;
1601: *rp = d;
1602: return 1;
1603: }
1604:
1.24 noro 1605: // ret=1 => success
1606: // ret=0 => overflow
1607: // ret=-1 => singular top reducible
1608:
1609: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1610: {
1611: NM m,mrd,tail;
1612: NM mul;
1613: int n,sugar,psugar,sugar0,stat,index;
1614: int c,c1,c2,dummy;
1615: RHist h;
1616: NDV p,red;
1617: Q cg,cred,gcd,tq,qq;
1618: Z iq;
1619: DP dmul;
1620: NODE node;
1621: LIST hist;
1622: double hmag;
1623: P tp,tp1;
1624: Obj tr,tr1,div;
1625: union oNDC hg;
1626: P cont;
1627: SIG sig;
1628:
1629: if ( !g ) {
1630: *rp = d;
1631: return 1;
1632: }
1633: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1634:
1635: sugar0 = sugar = SG(g);
1636: n = NV(g);
1637: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1638: if ( d )
1639: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1640: sig = g->sig;
1641: for ( ; g; ) {
1642: index = ndl_find_reducer_s(HDL(g),sig);
1643: if ( index >= 0 && index < nd_psn ) {
1644: // reducer found
1645: h = nd_psh[index];
1646: ndl_sub(HDL(g),DL(h),DL(mul));
1647: if ( ndl_check_bound2(index,DL(mul)) ) {
1648: nd_free(g); nd_free(d);
1649: return 0;
1650: }
1651: p = ps[index];
1652: /* d+g -> div*(d+g)+mul*p */
1653: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1654: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1655: if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1656: hg = HCU(g);
1657: nd_removecont2(d,g);
1658: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1659: }
1660: } else if ( index == -1 ) {
1661: // singular top reducible
1662: return -1;
1663: } else if ( !full ) {
1664: *rp = g;
1665: g->sig = sig;
1666: return 1;
1667: } else {
1668: m = BDY(g);
1669: if ( NEXT(m) ) {
1670: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1671: } else {
1672: FREEND(g); g = 0;
1673: }
1674: if ( d ) {
1675: NEXT(tail)=m; tail=m; LEN(d)++;
1676: } else {
1677: MKND(n,m,1,d); tail = BDY(d);
1678: }
1679: }
1680: }
1681: if ( d ) {
1682: SG(d) = sugar;
1683: d->sig = sig;
1684: }
1685: *rp = d;
1686: return 1;
1687: }
1688:
1.1 noro 1689: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1690: {
1691: int hindex,index;
1692: NDV p;
1693: ND u,d,red;
1694: NODE l;
1695: NM mul,m,mrd,tail;
1696: int sugar,psugar,n,h_reducible;
1697: PGeoBucket bucket;
1698: int c,c1,c2;
1699: Z cg,cred,gcd,zzz;
1700: RHist h;
1701: double hmag,gmag;
1702: int count = 0;
1703: int hcount = 0;
1704:
1705: if ( !g ) {
1706: *rp = 0;
1707: return 1;
1708: }
1709: sugar = SG(g);
1710: n = NV(g);
1.6 noro 1711: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1712: bucket = create_pbucket();
1713: add_pbucket(mod,bucket,g);
1714: d = 0;
1715: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1716: while ( 1 ) {
1717: if ( mod > 0 || mod == -1 )
1718: hindex = head_pbucket(mod,bucket);
1719: else if ( mod == -2 )
1720: hindex = head_pbucket_lf(bucket);
1721: else
1722: hindex = head_pbucket_q(bucket);
1723: if ( hindex < 0 ) {
1724: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1725: if ( d ) SG(d) = sugar;
1726: *rp = d;
1727: return 1;
1728: }
1729: g = bucket->body[hindex];
1730: index = ndl_find_reducer(HDL(g));
1731: if ( index >= 0 ) {
1732: count++;
1733: if ( !d ) hcount++;
1734: h = nd_psh[index];
1735: ndl_sub(HDL(g),DL(h),DL(mul));
1736: if ( ndl_check_bound2(index,DL(mul)) ) {
1737: nd_free(d);
1738: free_pbucket(bucket);
1739: *rp = 0;
1740: return 0;
1741: }
1742: p = ps[index];
1743: if ( mod == -1 )
1744: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1745: else if ( mod == -2 ) {
1746: Z inv,t;
1747: divlf(ONE,HCZ(p),&inv);
1748: chsgnlf(HCZ(g),&t);
1749: mullf(inv,t,&CZ(mul));
1750: } else if ( mod ) {
1751: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1752: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1753: } else {
1.6 noro 1754: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1755: chsgnz(cg,&CZ(mul));
1.1 noro 1756: nd_mul_c_q(d,(P)cred);
1757: mulq_pbucket(bucket,cred);
1758: g = bucket->body[hindex];
1.6 noro 1759: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1760: }
1761: red = ndv_mul_nm(mod,mul,p);
1762: bucket->body[hindex] = nd_remove_head(g);
1763: red = nd_remove_head(red);
1764: add_pbucket(mod,bucket,red);
1765: psugar = SG(p)+TD(DL(mul));
1766: sugar = MAX(sugar,psugar);
1767: if ( !mod && hmag && (gmag > hmag) ) {
1768: g = normalize_pbucket(mod,bucket);
1769: if ( !g ) {
1770: if ( d ) SG(d) = sugar;
1771: *rp = d;
1772: return 1;
1773: }
1774: nd_removecont2(d,g);
1.6 noro 1775: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1776: add_pbucket(mod,bucket,g);
1777: }
1778: } else if ( !full ) {
1779: g = normalize_pbucket(mod,bucket);
1780: if ( g ) SG(g) = sugar;
1781: *rp = g;
1782: return 1;
1783: } else {
1784: m = BDY(g);
1785: if ( NEXT(m) ) {
1786: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1787: } else {
1788: FREEND(g); g = 0;
1789: }
1790: bucket->body[hindex] = g;
1791: NEXT(m) = 0;
1792: if ( d ) {
1793: NEXT(tail)=m; tail=m; LEN(d)++;
1794: } else {
1795: MKND(n,m,1,d); tail = BDY(d);
1796: }
1797: }
1798: }
1799: }
1800:
1.25 noro 1801: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
1802: {
1803: int hindex,index;
1804: NDV p;
1805: ND u,d,red;
1806: NODE l;
1807: NM mul,m,mrd,tail;
1808: int sugar,psugar,n,h_reducible;
1809: PGeoBucket bucket;
1810: int c,c1,c2;
1811: Z cg,cred,gcd,zzz;
1812: RHist h;
1813: double hmag,gmag;
1814: int count = 0;
1815: int hcount = 0;
1816: SIG sig;
1817:
1818: if ( !g ) {
1819: *rp = 0;
1820: return 1;
1821: }
1822: sugar = SG(g);
1823: n = NV(g);
1824: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1825: bucket = create_pbucket();
1826: add_pbucket(mod,bucket,g);
1827: d = 0;
1828: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1829: sig = g->sig;
1830: while ( 1 ) {
1831: if ( mod > 0 || mod == -1 )
1832: hindex = head_pbucket(mod,bucket);
1833: else if ( mod == -2 )
1834: hindex = head_pbucket_lf(bucket);
1835: else
1836: hindex = head_pbucket_q(bucket);
1837: if ( hindex < 0 ) {
1838: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1839: if ( d ) {
1840: SG(d) = sugar;
1841: d->sig = sig;
1842: }
1843: *rp = d;
1844: return 1;
1845: }
1846: g = bucket->body[hindex];
1847: index = ndl_find_reducer_s(HDL(g),sig);
1848: if ( index >= 0 && index < nd_psn ) {
1849: count++;
1850: if ( !d ) hcount++;
1851: h = nd_psh[index];
1852: ndl_sub(HDL(g),DL(h),DL(mul));
1853: if ( ndl_check_bound2(index,DL(mul)) ) {
1854: nd_free(d);
1855: free_pbucket(bucket);
1856: *rp = 0;
1857: return 0;
1858: }
1859: p = ps[index];
1860: if ( mod == -1 )
1861: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1862: else if ( mod == -2 ) {
1863: Z inv,t;
1864: divlf(ONE,HCZ(p),&inv);
1865: chsgnlf(HCZ(g),&t);
1866: mullf(inv,t,&CZ(mul));
1867: } else if ( mod ) {
1868: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1869: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1870: } else {
1871: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1872: chsgnz(cg,&CZ(mul));
1873: nd_mul_c_q(d,(P)cred);
1874: mulq_pbucket(bucket,cred);
1875: g = bucket->body[hindex];
1876: gmag = (double)p_mag((P)HCZ(g));
1877: }
1878: red = ndv_mul_nm(mod,mul,p);
1879: bucket->body[hindex] = nd_remove_head(g);
1880: red = nd_remove_head(red);
1881: add_pbucket(mod,bucket,red);
1882: psugar = SG(p)+TD(DL(mul));
1883: sugar = MAX(sugar,psugar);
1884: if ( !mod && hmag && (gmag > hmag) ) {
1885: g = normalize_pbucket(mod,bucket);
1886: if ( !g ) {
1887: if ( d ) {
1888: SG(d) = sugar;
1889: d->sig = sig;
1890: }
1891: *rp = d;
1892: return 1;
1893: }
1894: nd_removecont2(d,g);
1895: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1896: add_pbucket(mod,bucket,g);
1897: }
1898: } else if ( index == -1 ) {
1899: // singular top reducible
1900: return -1;
1901: } else if ( !full ) {
1902: g = normalize_pbucket(mod,bucket);
1903: if ( g ) {
1904: SG(g) = sugar;
1905: g->sig = sig;
1906: }
1907: *rp = g;
1908: return 1;
1909: } else {
1910: m = BDY(g);
1911: if ( NEXT(m) ) {
1912: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1913: } else {
1914: FREEND(g); g = 0;
1915: }
1916: bucket->body[hindex] = g;
1917: NEXT(m) = 0;
1918: if ( d ) {
1919: NEXT(tail)=m; tail=m; LEN(d)++;
1920: } else {
1921: MKND(n,m,1,d); tail = BDY(d);
1922: }
1923: }
1924: }
1925: }
1926:
1.1 noro 1927: /* input : list of NDV, cand : list of NDV */
1928:
1929: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1930: {
1931: int n,i,stat;
1932: ND nf,d;
1933: NDV r;
1934: NODE t,s;
1935: union oNDC dn;
1936: Z q;
1937: LIST list;
1938:
1.24 noro 1939: ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1 noro 1940: n = length(cand);
1941:
1942: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1943: /* membercheck : list is a subset of Id(cand) ? */
1944: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1945: again:
1946: nd_tracelist = 0;
1947: if ( nd_bpe > obpe )
1948: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1949: else
1950: r = (NDV)BDY(t);
1951: #if 0
1952: // moved to nd_f4_lf_trace()
1953: if ( m == -2 ) ndv_mod(m,r);
1954: #endif
1955: d = ndvtond(m,r);
1.6 noro 1956: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1957: if ( !stat ) {
1958: nd_reconstruct(0,0);
1959: goto again;
1960: } else if ( nf ) return 0;
1961: if ( nd_gentrace ) {
1962: nd_tracelist = reverse_node(nd_tracelist);
1963: MKLIST(list,nd_tracelist);
1.6 noro 1964: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1965: MKNODE(s,list,nd_alltracelist);
1966: nd_alltracelist = s; nd_tracelist = 0;
1967: }
1968: if ( DP_Print ) { printf("."); fflush(stdout); }
1969: }
1970: if ( DP_Print ) { printf("\n"); }
1971: return 1;
1972: }
1973:
1974: ND nd_remove_head(ND p)
1975: {
1976: NM m;
1977:
1978: m = BDY(p);
1979: if ( !NEXT(m) ) {
1980: FREEND(p); p = 0;
1981: } else {
1982: BDY(p) = NEXT(m); LEN(p)--;
1983: }
1984: FREENM(m);
1985: return p;
1986: }
1987:
1988: ND nd_separate_head(ND p,ND *head)
1989: {
1990: NM m,m0;
1991: ND r;
1992:
1993: m = BDY(p);
1994: if ( !NEXT(m) ) {
1995: *head = p; p = 0;
1996: } else {
1997: m0 = m;
1998: BDY(p) = NEXT(m); LEN(p)--;
1999: NEXT(m0) = 0;
2000: MKND(NV(p),m0,1,r);
2001: *head = r;
2002: }
2003: return p;
2004: }
2005:
2006: PGeoBucket create_pbucket()
2007: {
2008: PGeoBucket g;
2009:
2010: g = CALLOC(1,sizeof(struct oPGeoBucket));
2011: g->m = -1;
2012: return g;
2013: }
2014:
2015: void free_pbucket(PGeoBucket b) {
2016: int i;
2017:
2018: for ( i = 0; i <= b->m; i++ )
2019: if ( b->body[i] ) {
2020: nd_free(b->body[i]);
2021: b->body[i] = 0;
2022: }
2023: GCFREE(b);
2024: }
2025:
2026: void add_pbucket_symbolic(PGeoBucket g,ND d)
2027: {
2028: int l,i,k,m;
2029:
2030: if ( !d )
2031: return;
2032: l = LEN(d);
2033: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2034: /* 2^(k-1) < l <= 2^k (=m) */
2035: d = nd_merge(g->body[k],d);
2036: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2037: g->body[k] = 0;
2038: d = nd_merge(g->body[k+1],d);
2039: }
2040: g->body[k] = d;
2041: g->m = MAX(g->m,k);
2042: }
2043:
2044: void add_pbucket(int mod,PGeoBucket g,ND d)
2045: {
2046: int l,i,k,m;
2047:
2048: if ( !d )
2049: return;
2050: l = LEN(d);
2051: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
2052: /* 2^(k-1) < l <= 2^k (=m) */
2053: d = nd_add(mod,g->body[k],d);
2054: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
2055: g->body[k] = 0;
2056: d = nd_add(mod,g->body[k+1],d);
2057: }
2058: g->body[k] = d;
2059: g->m = MAX(g->m,k);
2060: }
2061:
2062: void mulq_pbucket(PGeoBucket g,Z c)
2063: {
2064: int k;
2065:
2066: for ( k = 0; k <= g->m; k++ )
2067: nd_mul_c_q(g->body[k],(P)c);
2068: }
2069:
2070: NM remove_head_pbucket_symbolic(PGeoBucket g)
2071: {
2072: int j,i,k,c;
2073: NM head;
2074:
2075: k = g->m;
2076: j = -1;
2077: for ( i = 0; i <= k; i++ ) {
2078: if ( !g->body[i] ) continue;
2079: if ( j < 0 ) j = i;
2080: else {
2081: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
2082: if ( c > 0 )
2083: j = i;
2084: else if ( c == 0 )
2085: g->body[i] = nd_remove_head(g->body[i]);
2086: }
2087: }
2088: if ( j < 0 ) return 0;
2089: else {
2090: head = BDY(g->body[j]);
2091: if ( !NEXT(head) ) {
2092: FREEND(g->body[j]);
2093: g->body[j] = 0;
2094: } else {
2095: BDY(g->body[j]) = NEXT(head);
2096: LEN(g->body[j])--;
2097: }
2098: return head;
2099: }
2100: }
2101:
2102: int head_pbucket(int mod,PGeoBucket g)
2103: {
2104: int j,i,c,k,nv,sum;
2105: UINT *di,*dj;
2106: ND gi,gj;
2107:
2108: k = g->m;
2109: while ( 1 ) {
2110: j = -1;
2111: for ( i = 0; i <= k; i++ ) {
2112: if ( !(gi = g->body[i]) )
2113: continue;
2114: if ( j < 0 ) {
2115: j = i;
2116: gj = g->body[j];
2117: dj = HDL(gj);
2118: sum = HCM(gj);
2119: } else {
2120: c = DL_COMPARE(HDL(gi),dj);
2121: if ( c > 0 ) {
2122: if ( sum ) HCM(gj) = sum;
2123: else g->body[j] = nd_remove_head(gj);
2124: j = i;
2125: gj = g->body[j];
2126: dj = HDL(gj);
2127: sum = HCM(gj);
2128: } else if ( c == 0 ) {
2129: if ( mod == -1 )
2130: sum = _addsf(sum,HCM(gi));
2131: else {
2132: sum = sum+HCM(gi)-mod;
2133: if ( sum < 0 ) sum += mod;
2134: }
2135: g->body[i] = nd_remove_head(gi);
2136: }
2137: }
2138: }
2139: if ( j < 0 ) return -1;
2140: else if ( sum ) {
2141: HCM(gj) = sum;
2142: return j;
2143: } else
2144: g->body[j] = nd_remove_head(gj);
2145: }
2146: }
2147:
2148: int head_pbucket_q(PGeoBucket g)
2149: {
2150: int j,i,c,k,nv;
2151: Z sum,t;
2152: ND gi,gj;
2153:
2154: k = g->m;
2155: while ( 1 ) {
2156: j = -1;
2157: for ( i = 0; i <= k; i++ ) {
2158: if ( !(gi = g->body[i]) ) continue;
2159: if ( j < 0 ) {
2160: j = i;
2161: gj = g->body[j];
1.6 noro 2162: sum = HCZ(gj);
1.1 noro 2163: } else {
2164: nv = NV(gi);
2165: c = DL_COMPARE(HDL(gi),HDL(gj));
2166: if ( c > 0 ) {
1.6 noro 2167: if ( sum ) HCZ(gj) = sum;
1.1 noro 2168: else g->body[j] = nd_remove_head(gj);
2169: j = i;
2170: gj = g->body[j];
1.6 noro 2171: sum = HCZ(gj);
1.1 noro 2172: } else if ( c == 0 ) {
1.6 noro 2173: addz(sum,HCZ(gi),&t);
1.1 noro 2174: sum = t;
2175: g->body[i] = nd_remove_head(gi);
2176: }
2177: }
2178: }
2179: if ( j < 0 ) return -1;
2180: else if ( sum ) {
1.6 noro 2181: HCZ(gj) = sum;
1.1 noro 2182: return j;
2183: } else
2184: g->body[j] = nd_remove_head(gj);
2185: }
2186: }
2187:
2188: int head_pbucket_lf(PGeoBucket g)
2189: {
2190: int j,i,c,k,nv;
2191: Z sum,t;
2192: ND gi,gj;
2193:
2194: k = g->m;
2195: while ( 1 ) {
2196: j = -1;
2197: for ( i = 0; i <= k; i++ ) {
2198: if ( !(gi = g->body[i]) ) continue;
2199: if ( j < 0 ) {
2200: j = i;
2201: gj = g->body[j];
2202: sum = HCZ(gj);
2203: } else {
2204: nv = NV(gi);
2205: c = DL_COMPARE(HDL(gi),HDL(gj));
2206: if ( c > 0 ) {
2207: if ( sum ) HCZ(gj) = sum;
2208: else g->body[j] = nd_remove_head(gj);
2209: j = i;
2210: gj = g->body[j];
2211: sum = HCZ(gj);
2212: } else if ( c == 0 ) {
2213: addlf(sum,HCZ(gi),&t);
2214: sum = t;
2215: g->body[i] = nd_remove_head(gi);
2216: }
2217: }
2218: }
2219: if ( j < 0 ) return -1;
2220: else if ( sum ) {
2221: HCZ(gj) = sum;
2222: return j;
2223: } else
2224: g->body[j] = nd_remove_head(gj);
2225: }
2226: }
2227:
2228: ND normalize_pbucket(int mod,PGeoBucket g)
2229: {
2230: int i;
2231: ND r,t;
2232:
2233: r = 0;
2234: for ( i = 0; i <= g->m; i++ ) {
2235: r = nd_add(mod,r,g->body[i]);
2236: g->body[i] = 0;
2237: }
2238: g->m = -1;
2239: return r;
2240: }
2241:
2242: #if 0
2243: void register_hcf(NDV p)
2244: {
2245: DCP dc,t;
2246: P hc,h;
2247: int c;
2248: NODE l,l1,prev;
2249:
2250: hc = p->body->c.p;
2251: if ( !nd_vc || NUM(hc) ) return;
2252: fctrp(nd_vc,hc,&dc);
2253: for ( t = dc; t; t = NEXT(t) ) {
2254: h = t->c;
2255: if ( NUM(h) ) continue;
2256: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2257: c = compp(nd_vc,h,(P)BDY(l));
2258: if ( c >= 0 ) break;
2259: }
2260: if ( !l || c > 0 ) {
2261: MKNODE(l1,h,l);
2262: if ( !prev )
2263: nd_hcf = l1;
2264: else
2265: NEXT(prev) = l1;
2266: }
2267: }
2268: }
2269: #else
2270: void register_hcf(NDV p)
2271: {
2272: DCP dc,t;
2273: P hc,h,q;
2274: Q dmy;
2275: int c;
2276: NODE l,l1,prev;
2277:
2278: hc = p->body->c.p;
2279: if ( NUM(hc) ) return;
2280: ptozp(hc,1,&dmy,&h);
2281: #if 1
2282: for ( l = nd_hcf; l; l = NEXT(l) ) {
2283: while ( 1 ) {
2284: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
2285: else break;
2286: }
2287: }
2288: if ( NUM(h) ) return;
2289: #endif
2290: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
2291: c = compp(nd_vc,h,(P)BDY(l));
2292: if ( c >= 0 ) break;
2293: }
2294: if ( !l || c > 0 ) {
2295: MKNODE(l1,h,l);
2296: if ( !prev )
2297: nd_hcf = l1;
2298: else
2299: NEXT(prev) = l1;
2300: }
2301: }
2302: #endif
2303:
2304: int do_diagonalize(int sugar,int m)
2305: {
1.6 noro 2306: int i,nh,stat;
2307: NODE r,g,t;
2308: ND h,nf,s,head;
2309: NDV nfv;
2310: Q q;
2311: P nm,nmp,dn,mnp,dnp,cont,cont1;
2312: union oNDC hc;
2313: NODE node;
2314: LIST l;
2315: Z iq;
1.1 noro 2316:
1.6 noro 2317: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2318: if ( nd_gentrace ) {
2319: /* Trace = [1,index,1,1] */
2320: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2321: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2322: }
2323: if ( nd_demand )
2324: nfv = ndv_load(i);
2325: else
2326: nfv = nd_ps[i];
2327: s = ndvtond(m,nfv);
2328: s = nd_separate_head(s,&head);
2329: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2330: if ( !stat ) return 0;
2331: ndv_free(nfv);
2332: hc = HCU(nf); nd_removecont(m,nf);
2333: /* exact division */
2334: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2335: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2336: nfv = ndtondv(m,nf);
2337: nd_free(nf);
2338: nd_bound[i] = ndv_compute_bound(nfv);
2339: if ( !m ) register_hcf(nfv);
2340: if ( nd_demand ) {
2341: ndv_save(nfv,i);
2342: ndv_free(nfv);
2343: } else
2344: nd_ps[i] = nfv;
2345: }
2346: return 1;
1.1 noro 2347: }
2348:
2349: LIST compute_splist()
2350: {
2351: NODE g,tn0,tn,node;
2352: LIST l0;
2353: ND_pairs d,t;
2354: int i;
2355: Z i1,i2;
2356:
2357: g = 0; d = 0;
2358: for ( i = 0; i < nd_psn; i++ ) {
2359: d = update_pairs(d,g,i,0);
2360: g = update_base(g,i);
2361: }
2362: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2363: NEXTNODE(tn0,tn);
1.6 noro 2364: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2365: node = mknode(2,i1,i2); MKLIST(l0,node);
2366: BDY(tn) = l0;
2367: }
2368: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2369: return l0;
2370: }
2371:
2372: /* return value = 0 => input is not a GB */
2373:
2374: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2375: {
1.6 noro 2376: int i,nh,sugar,stat;
2377: NODE r,g,t;
2378: ND_pairs d;
2379: ND_pairs l;
2380: ND h,nf,s,head,nf1;
2381: NDV nfv;
2382: Z q;
2383: union oNDC dn,hc;
2384: int diag_count = 0;
2385: P cont;
2386: LIST list;
1.27 noro 2387: struct oEGT eg1,eg2,eg_update;
1.6 noro 2388:
1.27 noro 2389: init_eg(&eg_update);
1.11 noro 2390: Nnd_add = 0;
1.6 noro 2391: g = 0; d = 0;
2392: for ( i = 0; i < nd_psn; i++ ) {
2393: d = update_pairs(d,g,i,gensyz);
2394: g = update_base(g,i);
2395: }
2396: sugar = 0;
2397: while ( d ) {
1.1 noro 2398: again:
1.6 noro 2399: l = nd_minp(d,&d);
2400: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2401: if ( SG(l) != sugar ) {
2402: if ( ishomo ) {
2403: diag_count = 0;
2404: stat = do_diagonalize(sugar,m);
1.1 noro 2405: if ( !stat ) {
1.6 noro 2406: NEXT(l) = d; d = l;
2407: d = nd_reconstruct(0,d);
2408: goto again;
1.1 noro 2409: }
1.6 noro 2410: }
2411: sugar = SG(l);
2412: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2413: }
2414: stat = nd_sp(m,0,l,&h);
2415: if ( !stat ) {
2416: NEXT(l) = d; d = l;
2417: d = nd_reconstruct(0,d);
2418: goto again;
2419: }
1.1 noro 2420: #if USE_GEOBUCKET
1.6 noro 2421: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2422: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2423: #else
1.6 noro 2424: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2425: #endif
1.6 noro 2426: if ( !stat ) {
2427: NEXT(l) = d; d = l;
2428: d = nd_reconstruct(0,d);
2429: goto again;
2430: } else if ( nf ) {
2431: if ( checkonly || gensyz ) return 0;
1.1 noro 2432: if ( nd_newelim ) {
2433: if ( nd_module ) {
2434: if ( MPOS(HDL(nf)) > 1 ) return 0;
2435: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2436: }
1.6 noro 2437: if ( DP_Print ) { printf("+"); fflush(stdout); }
2438: hc = HCU(nf);
2439: nd_removecont(m,nf);
2440: if ( !m && nd_nalg ) {
2441: nd_monic(0,&nf);
2442: nd_removecont(m,nf);
2443: }
2444: if ( nd_gentrace ) {
2445: /* exact division */
1.1 noro 2446: cont = ndc_div(m,hc,HCU(nf));
2447: if ( m || !UNIQ(cont) ) {
1.6 noro 2448: t = mknode(4,NULLP,NULLP,NULLP,cont);
2449: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2450: nd_tracelist = t;
2451: }
2452: }
1.6 noro 2453: nfv = ndtondv(m,nf); nd_free(nf);
1.24 noro 2454: nh = ndv_newps(m,nfv,0);
1.6 noro 2455: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2456: diag_count = 0;
2457: stat = do_diagonalize(sugar,m);
2458: if ( !stat ) {
2459: NEXT(l) = d; d = l;
2460: d = nd_reconstruct(1,d);
2461: goto again;
1.1 noro 2462: }
1.6 noro 2463: }
1.27 noro 2464: get_eg(&eg1);
1.6 noro 2465: d = update_pairs(d,g,nh,0);
1.27 noro 2466: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6 noro 2467: g = update_base(g,nh);
2468: FREENDP(l);
2469: } else {
2470: if ( nd_gentrace && gensyz ) {
2471: nd_tracelist = reverse_node(nd_tracelist);
2472: MKLIST(list,nd_tracelist);
2473: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2474: MKNODE(t,list,nd_alltracelist);
2475: nd_alltracelist = t; nd_tracelist = 0;
2476: }
2477: if ( DP_Print ) { printf("."); fflush(stdout); }
2478: FREENDP(l);
2479: }
2480: }
2481: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2482: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.27 noro 2483: print_eg("update",&eg_update);
1.1 noro 2484: return g;
2485: }
2486:
1.30 ! noro 2487: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
! 2488: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24 noro 2489:
2490: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
2491: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
2492:
2493: void _copydl(int n,DL d1,DL d2);
2494: void _subfromdl(int n,DL d1,DL d2);
2495: extern int (*cmpdl)(int n,DL d1,DL d2);
2496:
2497: NODE insert_sig(NODE l,SIG s)
2498: {
2499: int pos;
2500: DL sig;
2501: struct oNODE root;
2502: NODE p,prev,r;
2503: SIG t;
2504:
2505: pos = s->pos; sig = DL(s);
2506: root.next = l; prev = &root;
2507: for ( p = l; p; p = p->next ) {
2508: t = (SIG)p->body;
2509: if ( t->pos == pos ) {
2510: if ( _dl_redble(DL(t),sig,nd_nvar) )
2511: return root.next;
2512: else if ( _dl_redble(sig,DL(t),nd_nvar) )
2513: // remove p
2514: prev->next = p->next;
2515: } else
2516: prev = p;
2517: }
1.27 noro 2518: NEWNODE(r); r->body = (pointer)s; r->next = 0;
2519: for ( p = &root; p->next; p = p->next );
2520: p->next = r;
2521: // r->next = root.next;
2522: // return r;
2523: return root.next;
1.24 noro 2524: }
2525:
2526: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
2527: {
2528: struct oND_pairs root;
2529: ND_pairs prev,p;
2530: SIG spsig;
2531:
2532: root.next = d;
2533: prev = &root; p = d;
2534: while ( p ) {
2535: spsig = p->sig;
1.26 noro 2536: if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24 noro 2537: // remove p
2538: prev->next = p->next;
1.26 noro 2539: Nsyz++;
2540: } else
1.24 noro 2541: prev = p;
2542: p = p->next;
2543: }
2544: return (ND_pairs)root.next;
2545: }
2546:
1.29 noro 2547: int _dl_redble_ext(DL,DL,DL,int);
2548:
1.28 noro 2549: int small_lcm(ND_pairs l)
2550: {
2551: SIG sig;
2552: int i;
1.29 noro 2553: NODE t;
1.28 noro 2554: static DL lcm,mul,quo;
2555: static int nvar;
2556:
2557: if ( nvar < nd_nvar ) {
2558: nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
2559: }
2560: sig = l->sig;
2561: _ndltodl(l->lcm,lcm);
1.29 noro 2562: #if 0
1.28 noro 2563: for ( i = 0; i < nd_psn; i++ ) {
2564: if ( sig->pos == nd_psh[i]->sig->pos &&
1.29 noro 2565: _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28 noro 2566: _ndltodl(DL(nd_psh[i]),mul);
2567: _addtodl(nd_nvar,quo,mul);
2568: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2569: break;
2570: }
2571: }
2572: if ( i < nd_psn ) return 1;
2573: else return 0;
1.29 noro 2574: #else
2575: for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
2576: i = (long)BDY(t);
2577: if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
2578: _ndltodl(DL(nd_psh[i]),mul);
2579: _addtodl(nd_nvar,quo,mul);
2580: if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
2581: break;
2582: }
2583: }
2584: if ( t ) return 1;
2585: else return 0;
2586: #endif
1.28 noro 2587: }
2588:
2589: ND_pairs remove_large_lcm(ND_pairs d)
2590: {
2591: struct oND_pairs root;
2592: ND_pairs prev,p;
2593:
2594: root.next = d;
2595: prev = &root; p = d;
2596: while ( p ) {
2597: if ( small_lcm(p) ) {
2598: // remove p
2599: prev->next = p->next;
2600: } else
2601: prev = p;
2602: p = p->next;
2603: }
2604: return (ND_pairs)root.next;
2605: }
2606:
1.26 noro 2607: struct oEGT eg_create,eg_newpairs,eg_merge;
2608:
1.30 ! noro 2609: NODE conv_ilist_s(int demand,int trace,int **indp);
! 2610:
1.24 noro 2611: NODE nd_sba_buch(int m,int ishomo,int **indp)
2612: {
1.29 noro 2613: int i,j,nh,sugar,stat;
1.30 ! noro 2614: NODE r,t,g;
1.24 noro 2615: ND_pairs d;
2616: ND_pairs l;
2617: ND h,nf,s,head,nf1;
2618: NDV nfv;
2619: Z q;
2620: union oNDC dn,hc;
2621: P cont;
2622: LIST list;
2623: SIG sig;
1.29 noro 2624: NODE *syzlist;
1.26 noro 2625: int Nredundant;
1.28 noro 2626: DL lcm,quo,mul;
1.29 noro 2627: struct oEGT eg1,eg2,eg_update,eg_remove,eg_large;
1.24 noro 2628:
1.27 noro 2629: init_eg(&eg_remove);
1.29 noro 2630: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26 noro 2631: Nsyz = 0;
1.24 noro 2632: Nnd_add = 0;
1.26 noro 2633: Nredundant = 0;
1.30 ! noro 2634: d = 0;
1.24 noro 2635: for ( i = 0; i < nd_psn; i++ ) {
1.30 ! noro 2636: d = update_pairs_s(d,i,syzlist);
1.24 noro 2637: }
1.29 noro 2638: for ( i = 0; i < nd_psn; i++ )
2639: for ( j = i+1; j < nd_psn; j++ ) {
2640: NEWSIG(sig); sig->pos = j;
2641: _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
2642: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
2643: }
1.24 noro 2644: sugar = 0;
1.26 noro 2645: NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
2646: init_eg(&eg_create);
2647: init_eg(&eg_merge);
1.29 noro 2648: init_eg(&eg_large);
1.24 noro 2649: while ( d ) {
2650: again:
1.26 noro 2651: if ( DP_Print ) {
2652: int len;
2653: ND_pairs td;
1.28 noro 2654: for ( td = d, len=0; td; td = td->next, len++)
2655: ;
1.26 noro 2656: if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
2657: }
1.24 noro 2658: l = d; d = d->next;
1.28 noro 2659: if ( small_lcm(l) ) {
1.24 noro 2660: if ( DP_Print ) fprintf(asir_out,"M");
1.26 noro 2661: Nredundant++;
1.24 noro 2662: continue;
2663: }
2664: if ( SG(l) != sugar ) {
2665: sugar = SG(l);
2666: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2667: }
1.28 noro 2668: sig = l->sig;
1.24 noro 2669: stat = nd_sp(m,0,l,&h);
2670: if ( !stat ) {
2671: NEXT(l) = d; d = l;
2672: d = nd_reconstruct(0,d);
2673: goto again;
2674: }
1.25 noro 2675: #if USE_GEOBUCKET
1.24 noro 2676: stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2677: #else
2678: stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
2679: #endif
2680: if ( !stat ) {
2681: NEXT(l) = d; d = l;
2682: d = nd_reconstruct(0,d);
2683: goto again;
2684: } else if ( stat == -1 ) {
2685: if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27 noro 2686: FREENDP(l);
1.24 noro 2687: } else if ( nf ) {
2688: if ( DP_Print ) { printf("+"); fflush(stdout); }
2689: hc = HCU(nf);
2690: nd_removecont(m,nf);
2691: nfv = ndtondv(m,nf); nd_free(nf);
2692: nh = ndv_newps(m,nfv,0);
1.26 noro 2693:
1.30 ! noro 2694: d = update_pairs_s(d,nh,syzlist);
1.29 noro 2695: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24 noro 2696: FREENDP(l);
2697: } else {
2698: // syzygy
1.27 noro 2699: get_eg(&eg1);
1.24 noro 2700: d = remove_spair_s(d,sig);
1.27 noro 2701: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29 noro 2702: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24 noro 2703: if ( DP_Print ) { printf("."); fflush(stdout); }
2704: FREENDP(l);
2705: }
2706: }
1.30 ! noro 2707: g = conv_ilist_s(nd_demand,0,indp);
1.26 noro 2708: if ( DP_Print ) {
2709: printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
2710: fflush(stdout);
2711: print_eg("create",&eg_create);
2712: print_eg("merge",&eg_merge);
1.27 noro 2713: print_eg("remove",&eg_remove);
2714: printf("\n");
1.26 noro 2715: }
1.24 noro 2716: return g;
2717: }
2718:
1.1 noro 2719: /* splist = [[i1,i2],...] */
2720:
2721: int check_splist(int m,NODE splist)
2722: {
2723: NODE t,p;
2724: ND_pairs d,r,l;
2725: int stat;
2726: ND h,nf;
2727:
2728: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2729: p = BDY((LIST)BDY(t));
1.6 noro 2730: NEXTND_pairs(d,r);
2731: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2732: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2733: SG(r) = TD(LCM(r)); /* XXX */
2734: }
2735: if ( d ) NEXT(r) = 0;
2736:
1.6 noro 2737: while ( d ) {
1.1 noro 2738: again:
1.6 noro 2739: l = nd_minp(d,&d);
2740: stat = nd_sp(m,0,l,&h);
2741: if ( !stat ) {
2742: NEXT(l) = d; d = l;
2743: d = nd_reconstruct(0,d);
2744: goto again;
2745: }
2746: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2747: if ( !stat ) {
2748: NEXT(l) = d; d = l;
2749: d = nd_reconstruct(0,d);
2750: goto again;
2751: } else if ( nf ) return 0;
1.1 noro 2752: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2753: }
1.1 noro 2754: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2755: return 1;
2756: }
2757:
2758: int check_splist_f4(int m,NODE splist)
2759: {
2760: UINT *s0vect;
1.6 noro 2761: PGeoBucket bucket;
1.1 noro 2762: NODE p,rp0,t;
2763: ND_pairs d,r,l,ll;
2764: int col,stat;
2765:
2766: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2767: p = BDY((LIST)BDY(t));
1.6 noro 2768: NEXTND_pairs(d,r);
2769: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2770: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2771: SG(r) = TD(LCM(r)); /* XXX */
2772: }
2773: if ( d ) NEXT(r) = 0;
2774:
1.6 noro 2775: while ( d ) {
2776: l = nd_minsugarp(d,&d);
2777: bucket = create_pbucket();
2778: stat = nd_sp_f4(m,0,l,bucket);
2779: if ( !stat ) {
2780: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2781: NEXT(ll) = d; d = l;
2782: d = nd_reconstruct(0,d);
2783: continue;
2784: }
2785: if ( bucket->m < 0 ) continue;
2786: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2787: if ( !col ) {
2788: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2789: NEXT(ll) = d; d = l;
2790: d = nd_reconstruct(0,d);
2791: continue;
1.1 noro 2792: }
1.6 noro 2793: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2794: }
2795: return 1;
1.1 noro 2796: }
2797:
2798: int do_diagonalize_trace(int sugar,int m)
2799: {
1.6 noro 2800: int i,nh,stat;
2801: NODE r,g,t;
2802: ND h,nf,nfq,s,head;
2803: NDV nfv,nfqv;
2804: Q q,den,num;
2805: union oNDC hc;
2806: NODE node;
2807: LIST l;
2808: Z iq;
2809: P cont,cont1;
1.1 noro 2810:
1.6 noro 2811: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2812: if ( nd_gentrace ) {
2813: /* Trace = [1,index,1,1] */
2814: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2815: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2816: }
2817: /* for nd_ps */
2818: s = ndvtond(m,nd_ps[i]);
2819: s = nd_separate_head(s,&head);
2820: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2821: if ( !stat ) return 0;
2822: nf = nd_add(m,head,nf);
2823: ndv_free(nd_ps[i]);
2824: nd_ps[i] = ndtondv(m,nf);
2825: nd_free(nf);
2826:
2827: /* for nd_ps_trace */
2828: if ( nd_demand )
2829: nfv = ndv_load(i);
2830: else
2831: nfv = nd_ps_trace[i];
2832: s = ndvtond(0,nfv);
2833: s = nd_separate_head(s,&head);
2834: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2835: if ( !stat ) return 0;
2836: ndv_free(nfv);
2837: hc = HCU(nf); nd_removecont(0,nf);
2838: /* exact division */
1.1 noro 2839: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2840: if ( nd_gentrace ) finalize_tracelist(i,cont);
2841: nfv = ndtondv(0,nf);
2842: nd_free(nf);
2843: nd_bound[i] = ndv_compute_bound(nfv);
2844: register_hcf(nfv);
2845: if ( nd_demand ) {
2846: ndv_save(nfv,i);
2847: ndv_free(nfv);
2848: } else
2849: nd_ps_trace[i] = nfv;
2850: }
2851: return 1;
1.1 noro 2852: }
2853:
2854: static struct oEGT eg_invdalg;
2855: struct oEGT eg_le;
2856:
2857: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2858: {
2859: NODE tn;
2860: P p1;
2861:
2862: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2863: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2864: }
2865: *r = p;
2866: }
2867:
2868: NODE nd_gb_trace(int m,int ishomo,int **indp)
2869: {
1.6 noro 2870: int i,nh,sugar,stat;
2871: NODE r,g,t;
2872: ND_pairs d;
2873: ND_pairs l;
2874: ND h,nf,nfq,s,head;
2875: NDV nfv,nfqv;
2876: Z q,den,num;
2877: P hc;
2878: union oNDC dn,hnfq;
2879: struct oEGT eg_monic,egm0,egm1;
2880: int diag_count = 0;
2881: P cont;
2882: LIST list;
2883:
2884: init_eg(&eg_monic);
2885: init_eg(&eg_invdalg);
2886: init_eg(&eg_le);
2887: g = 0; d = 0;
2888: for ( i = 0; i < nd_psn; i++ ) {
2889: d = update_pairs(d,g,i,0);
2890: g = update_base(g,i);
2891: }
2892: sugar = 0;
2893: while ( d ) {
1.1 noro 2894: again:
1.6 noro 2895: l = nd_minp(d,&d);
2896: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2897: if ( SG(l) != sugar ) {
1.1 noro 2898: #if 1
1.6 noro 2899: if ( ishomo ) {
2900: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2901: stat = do_diagonalize_trace(sugar,m);
2902: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2903: diag_count = 0;
1.1 noro 2904: if ( !stat ) {
1.6 noro 2905: NEXT(l) = d; d = l;
2906: d = nd_reconstruct(1,d);
2907: goto again;
1.1 noro 2908: }
1.6 noro 2909: }
2910: #endif
2911: sugar = SG(l);
2912: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2913: }
2914: stat = nd_sp(m,0,l,&h);
2915: if ( !stat ) {
2916: NEXT(l) = d; d = l;
2917: d = nd_reconstruct(1,d);
2918: goto again;
2919: }
1.1 noro 2920: #if USE_GEOBUCKET
1.6 noro 2921: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2922: #else
1.6 noro 2923: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2924: #endif
1.6 noro 2925: if ( !stat ) {
2926: NEXT(l) = d; d = l;
2927: d = nd_reconstruct(1,d);
2928: goto again;
2929: } else if ( nf ) {
2930: if ( nd_demand ) {
2931: nfqv = ndv_load(nd_psn);
2932: nfq = ndvtond(0,nfqv);
2933: } else
2934: nfq = 0;
2935: if ( !nfq ) {
2936: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2937: NEXT(l) = d; d = l;
2938: d = nd_reconstruct(1,d);
2939: goto again;
2940: }
2941: }
2942: if ( nfq ) {
2943: /* m|HC(nfq) => failure */
2944: if ( nd_vc ) {
2945: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2946: } else
2947: q = HCZ(nfq);
2948: if ( !remqi((Q)q,m) ) return 0;
2949:
2950: if ( DP_Print ) { printf("+"); fflush(stdout); }
2951: hnfq = HCU(nfq);
2952: if ( nd_nalg ) {
2953: /* m|DN(HC(nf)^(-1)) => failure */
2954: get_eg(&egm0);
2955: if ( !nd_monic(m,&nfq) ) return 0;
2956: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2957: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2958: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2959: } else {
2960: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2961: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2962: }
2963: if ( nd_gentrace ) {
2964: /* exact division */
2965: cont = ndc_div(0,hnfq,HCU(nfqv));
2966: if ( !UNIQ(cont) ) {
2967: t = mknode(4,NULLP,NULLP,NULLP,cont);
2968: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2969: nd_tracelist = t;
2970: }
2971: }
1.24 noro 2972: nh = ndv_newps(0,nfv,nfqv);
1.6 noro 2973: if ( ishomo && ++diag_count == diag_period ) {
2974: diag_count = 0;
2975: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2976: stat = do_diagonalize_trace(sugar,m);
2977: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2978: if ( !stat ) {
1.1 noro 2979: NEXT(l) = d; d = l;
2980: d = nd_reconstruct(1,d);
2981: goto again;
1.6 noro 2982: }
1.1 noro 2983: }
1.6 noro 2984: d = update_pairs(d,g,nh,0);
2985: g = update_base(g,nh);
2986: } else {
2987: if ( DP_Print ) { printf("*"); fflush(stdout); }
2988: }
2989: } else {
2990: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2991: }
1.6 noro 2992: FREENDP(l);
2993: }
2994: if ( nd_nalg ) {
2995: if ( DP_Print ) {
2996: print_eg("monic",&eg_monic);
2997: print_eg("invdalg",&eg_invdalg);
2998: print_eg("le",&eg_le);
1.1 noro 2999: }
1.6 noro 3000: }
1.1 noro 3001: conv_ilist(nd_demand,1,g,indp);
1.6 noro 3002: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
3003: return g;
1.1 noro 3004: }
3005:
3006: int ndv_compare(NDV *p1,NDV *p2)
3007: {
3008: return DL_COMPARE(HDL(*p1),HDL(*p2));
3009: }
3010:
3011: int ndv_compare_rev(NDV *p1,NDV *p2)
3012: {
3013: return -DL_COMPARE(HDL(*p1),HDL(*p2));
3014: }
3015:
3016: int ndvi_compare(NDVI p1,NDVI p2)
3017: {
3018: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
3019: }
3020:
3021: int ndvi_compare_rev(NDVI p1,NDVI p2)
3022: {
3023: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
3024: }
3025:
3026: NODE ndv_reduceall(int m,NODE f)
3027: {
3028: int i,j,n,stat;
3029: ND nf,g,head;
3030: NODE t,a0,a;
3031: union oNDC dn;
3032: Q q,num,den;
3033: NODE node;
3034: LIST l;
3035: Z iq,jq;
3036: int *perm;
3037: union oNDC hc;
3038: P cont,cont1;
3039:
3040: if ( nd_nora ) return f;
3041: n = length(f);
1.24 noro 3042: ndv_setup(m,0,f,0,1,0);
1.1 noro 3043: perm = (int *)MALLOC(n*sizeof(int));
3044: if ( nd_gentrace ) {
3045: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 3046: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 3047: }
3048: for ( i = 0; i < n; ) {
3049: if ( nd_gentrace ) {
3050: /* Trace = [1,index,1,1] */
1.6 noro 3051: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 3052: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
3053: }
3054: g = ndvtond(m,nd_ps[i]);
3055: g = nd_separate_head(g,&head);
1.6 noro 3056: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 3057: if ( !stat )
3058: nd_reconstruct(0,0);
3059: else {
3060: if ( DP_Print ) { printf("."); fflush(stdout); }
3061: ndv_free(nd_ps[i]);
3062: hc = HCU(nf); nd_removecont(m,nf);
3063: if ( nd_gentrace ) {
3064: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 3065: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
3066: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 3067: }
1.6 noro 3068: /* exact division */
1.1 noro 3069: cont = ndc_div(m,hc,HCU(nf));
3070: finalize_tracelist(perm[i],cont);
3071: }
3072: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
3073: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
3074: i++;
3075: }
3076: }
3077: if ( DP_Print ) { printf("\n"); }
3078: for ( a0 = 0, i = 0; i < n; i++ ) {
3079: NEXTNODE(a0,a);
3080: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
3081: else {
3082: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
3083: BDY(a) = (pointer)nd_ps[j];
3084: }
3085: }
3086: NEXT(a) = 0;
3087: return a0;
3088: }
3089:
3090: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
3091: {
3092: ND_pairs d1,nd,cur,head,prev,remove;
3093:
3094: if ( !g ) return d;
3095: /* for testing */
3096: if ( gensyz && nd_gensyz == 2 ) {
3097: d1 = nd_newpairs(g,t);
3098: if ( !d )
3099: return d1;
3100: else {
3101: nd = d;
3102: while ( NEXT(nd) ) nd = NEXT(nd);
3103: NEXT(nd) = d1;
3104: return d;
3105: }
3106: }
3107: d = crit_B(d,t);
3108: d1 = nd_newpairs(g,t);
3109: d1 = crit_M(d1);
3110: d1 = crit_F(d1);
3111: if ( gensyz || do_weyl )
3112: head = d1;
3113: else {
3114: prev = 0; cur = head = d1;
3115: while ( cur ) {
3116: if ( crit_2( cur->i1,cur->i2 ) ) {
3117: remove = cur;
3118: if ( !prev ) head = cur = NEXT(cur);
3119: else cur = NEXT(prev) = NEXT(cur);
3120: FREENDP(remove);
3121: } else {
3122: prev = cur; cur = NEXT(cur);
3123: }
3124: }
3125: }
3126: if ( !d )
3127: return head;
3128: else {
3129: nd = d;
3130: while ( NEXT(nd) ) nd = NEXT(nd);
3131: NEXT(nd) = head;
3132: return d;
3133: }
3134: }
3135:
1.24 noro 3136: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
3137:
1.30 ! noro 3138: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24 noro 3139: {
3140: ND_pairs d1;
1.26 noro 3141: struct oEGT eg1,eg2,eg3;
1.24 noro 3142:
1.30 ! noro 3143: if ( !t ) return d;
1.26 noro 3144: get_eg(&eg1);
1.30 ! noro 3145: d1 = nd_newpairs_s(t,syz);
1.26 noro 3146: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24 noro 3147: d = merge_pairs_s(d,d1);
1.26 noro 3148: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24 noro 3149: return d;
3150: }
1.1 noro 3151:
3152: ND_pairs nd_newpairs( NODE g, int t )
3153: {
3154: NODE h;
3155: UINT *dl;
3156: int ts,s,i,t0,min,max;
3157: ND_pairs r,r0;
3158:
3159: dl = DL(nd_psh[t]);
3160: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 3161: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 3162: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
3163: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
3164: continue;
3165: if ( nd_gbblock ) {
3166: t0 = (long)BDY(h);
3167: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
3168: min = nd_gbblock[i]; max = nd_gbblock[i+1];
3169: if ( t0 >= min && t0 <= max && t >= min && t <= max )
3170: break;
3171: }
3172: if ( nd_gbblock[i] >= 0 )
3173: continue;
3174: }
3175: NEXTND_pairs(r0,r);
3176: r->i1 = (long)BDY(h);
3177: r->i2 = t;
3178: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
3179: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3180: SG(r) = MAX(s,ts) + TD(LCM(r));
3181: /* experimental */
3182: if ( nd_sugarweight )
3183: r->sugar2 = ndl_weight2(r->lcm);
3184: }
3185: if ( r0 ) NEXT(r) = 0;
3186: return r0;
3187: }
3188:
1.24 noro 3189: int comp_sig(SIG s1,SIG s2)
3190: {
3191: #if 0
3192: if ( s1->pos > s2->pos ) return 1;
3193: else if ( s1->pos < s2->pos ) return -1;
3194: else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
3195: #else
3196: static DL m1,m2;
3197: static int nvar;
3198: int ret;
3199:
1.26 noro 3200: if ( nvar != nd_nvar ) {
3201: nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24 noro 3202: }
1.27 noro 3203: // _ndltodl(DL(nd_psh[s1->pos]),m1);
3204: // _ndltodl(DL(nd_psh[s2->pos]),m2);
3205: _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
3206: _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24 noro 3207: _addtodl(nd_nvar,s1->dl,m1);
3208: _addtodl(nd_nvar,s2->dl,m2);
3209: ret = (*cmpdl)(nd_nvar,m1,m2);
3210: if ( ret != 0 ) return ret;
3211: else if ( s1->pos > s2->pos ) return 1;
3212: else if ( s1->pos < s2->pos ) return -1;
3213: else return 0;
3214: #endif
3215: }
3216:
3217: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
3218: {
3219: int ret,s1,s2;
3220: RHist p1,p2;
3221: static int wpd;
3222: static UINT *lcm;
3223:
3224: sp->i1 = i1;
3225: sp->i2 = i2;
3226: p1 = nd_psh[i1];
3227: p2 = nd_psh[i2];
3228: ndl_lcm(DL(p1),DL(p2),sp->lcm);
3229: s1 = SG(p1)-TD(DL(p1));
3230: s2 = SG(p2)-TD(DL(p2));
3231: SG(sp) = MAX(s1,s2) + TD(sp->lcm);
3232:
1.26 noro 3233: if ( wpd != nd_wpd ) {
1.24 noro 3234: wpd = nd_wpd;
3235: lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
3236: }
3237: // DL(sig1) <- sp->lcm
3238: // DL(sig1) -= DL(p1)
3239: // DL(sig1) += DL(p1->sig)
1.27 noro 3240: ndl_sub(sp->lcm,DL(p1),lcm);
1.24 noro 3241: _ndltodl(lcm,DL(sig1));
3242: _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
3243: sig1->pos = p1->sig->pos;
3244:
3245: // DL(sig2) <- sp->lcm
3246: // DL(sig2) -= DL(p2)
3247: // DL(sig2) += DL(p2->sig)
1.27 noro 3248: ndl_sub(sp->lcm,DL(p2),lcm);
1.24 noro 3249: _ndltodl(lcm,DL(sig2));
3250: _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
3251: sig2->pos = p2->sig->pos;
3252:
3253: ret = comp_sig(sig1,sig2);
3254: if ( ret == 0 ) return 0;
3255: else if ( ret > 0 ) sp->sig = sig1;
3256: else sp->sig = sig2;
3257: return 1;
3258: }
3259:
3260: SIG dup_sig(SIG sig)
3261: {
3262: SIG r;
3263:
3264: if ( !sig ) return 0;
3265: else {
3266: NEWSIG(r);
3267: _copydl(nd_nvar,DL(sig),DL(r));
3268: r->pos = sig->pos;
3269: return r;
3270: }
3271: }
3272:
3273: void dup_ND_pairs(ND_pairs to,ND_pairs from)
3274: {
3275: to->i1 = from->i1;
3276: to->i2 = from->i2;
3277: to->sugar = from->sugar;
3278: to->sugar2 = from->sugar2;
3279: ndl_copy(from->lcm,to->lcm);
3280: to->sig = dup_sig(from->sig);
3281: }
3282:
3283: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
3284: {
3285: struct oND_pairs root;
3286: ND_pairs q1,q2,r0,r;
3287: int ret;
3288:
3289: r = &root;
3290: for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
3291: ret = comp_sig(q1->sig,q2->sig);
3292: if ( ret < 0 ) {
3293: r->next = q1; r = q1; q1 = q1->next;
3294: } else if ( ret > 0 ) {
3295: r->next = q2; r = q2; q2 = q2->next;
3296: } else {
3297: ret = DL_COMPARE(q1->lcm,q2->lcm);
3298: if ( ret < 0 ) {
3299: r->next = q1; r = q1; q1 = q1->next;
3300: q2 = q2->next;
3301: } else {
3302: r->next = q2; r = q2; q2 = q2->next;
3303: q1 = q1->next;
3304: }
3305: }
3306: }
3307: if ( q1 ) {
3308: r->next = q1;
3309: } else {
3310: r->next = q2;
3311: }
3312: return root.next;
3313: }
3314:
3315: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
3316: {
3317: ND_pairs p,prev;
3318: int ret;
3319:
3320: for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
3321: if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
3322: break;
3323: }
3324: if ( ret == 0 ) {
3325: ret = DL_COMPARE(s->lcm,p->lcm);
3326: if ( ret < 0 ) {
3327: // replace p with s
3328: s->next = p->next;
3329: if ( prev == 0 ) {
3330: return s;
3331: } else {
3332: prev->next = s;
3333: return l;
3334: }
3335: } else
3336: return l;
3337: } else {
3338: // insert s between prev and p
3339: s->next = p;
3340: if ( prev == 0 ) {
3341: return s;
3342: } else {
3343: prev->next = s;
3344: return l;
3345: }
3346: }
3347: }
3348:
1.29 noro 3349: INLINE int __dl_redble(DL d1,DL d2,int nvar)
3350: {
3351: int i;
3352:
3353: if ( d1->td > d2->td )
3354: return 0;
3355: for ( i = nvar-1; i >= 0; i-- )
3356: if ( d1->d[i] > d2->d[i] )
3357: break;
3358: if ( i >= 0 )
3359: return 0;
3360: else
3361: return 1;
3362: }
3363:
1.30 ! noro 3364: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24 noro 3365: {
3366: NODE h,s;
3367: UINT *dl;
1.27 noro 3368: int ts,ret,i;
1.24 noro 3369: ND_pairs r,r0,_sp,sp;
1.29 noro 3370: SIG spsig,tsig;
3371: static int nvar;
3372: static SIG _sig1,_sig2;
1.26 noro 3373: struct oEGT eg1,eg2,eg3,eg4;
1.24 noro 3374:
3375: NEWND_pairs(_sp);
1.29 noro 3376: if ( !_sig1 || nvar != nd_nvar ) {
3377: nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
3378: }
1.24 noro 3379: r0 = 0;
1.27 noro 3380: for ( i = 0; i < t; i++ ) {
3381: ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24 noro 3382: if ( ret ) {
3383: spsig = _sp->sig;
1.29 noro 3384: for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24 noro 3385: tsig = (SIG)s->body;
1.29 noro 3386: if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24 noro 3387: break;
3388: }
3389: if ( s == 0 ) {
3390: NEWND_pairs(sp);
3391: dup_ND_pairs(sp,_sp);
3392: r0 = insert_pair_s(r0,sp);
1.26 noro 3393: } else
3394: Nsyz++;
1.24 noro 3395: }
3396: }
3397: return r0;
3398: }
3399:
1.1 noro 3400: /* ipair = [i1,i2],[i1,i2],... */
3401: ND_pairs nd_ipairtospair(NODE ipair)
3402: {
3403: int s1,s2;
3404: NODE tn,t;
3405: ND_pairs r,r0;
3406:
3407: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
3408: NEXTND_pairs(r0,r);
3409: tn = BDY((LIST)BDY(t));
1.6 noro 3410: r->i1 = ZTOS((Q)ARG0(tn));
3411: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 3412: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
3413: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
3414: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
3415: SG(r) = MAX(s1,s2) + TD(LCM(r));
3416: /* experimental */
3417: if ( nd_sugarweight )
3418: r->sugar2 = ndl_weight2(r->lcm);
3419: }
3420: if ( r0 ) NEXT(r) = 0;
3421: return r0;
3422: }
3423:
3424: /* kokokara */
3425:
3426: ND_pairs crit_B( ND_pairs d, int s )
3427: {
3428: ND_pairs cur,head,prev,remove;
3429: UINT *t,*tl,*lcm;
3430: int td,tdl;
3431:
3432: if ( !d ) return 0;
3433: t = DL(nd_psh[s]);
3434: prev = 0;
3435: head = cur = d;
3436: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
3437: while ( cur ) {
3438: tl = cur->lcm;
3439: if ( ndl_reducible(tl,t) ) {
3440: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
3441: if ( !ndl_equal(lcm,tl) ) {
3442: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
3443: if (!ndl_equal(lcm,tl)) {
3444: remove = cur;
3445: if ( !prev ) {
3446: head = cur = NEXT(cur);
3447: } else {
3448: cur = NEXT(prev) = NEXT(cur);
3449: }
3450: FREENDP(remove);
3451: } else {
3452: prev = cur; cur = NEXT(cur);
3453: }
3454: } else {
3455: prev = cur; cur = NEXT(cur);
3456: }
3457: } else {
3458: prev = cur; cur = NEXT(cur);
3459: }
3460: }
3461: return head;
3462: }
3463:
3464: ND_pairs crit_M( ND_pairs d1 )
3465: {
3466: ND_pairs e,d2,d3,dd,p;
3467: UINT *id,*jd;
3468:
3469: if ( !d1 ) return d1;
3470: for ( dd = 0, e = d1; e; e = d3 ) {
3471: if ( !(d2 = NEXT(e)) ) {
3472: NEXT(e) = dd;
3473: return e;
3474: }
3475: id = LCM(e);
3476: for ( d3 = 0; d2; d2 = p ) {
3477: p = NEXT(d2);
3478: jd = LCM(d2);
3479: if ( ndl_equal(jd,id) )
3480: ;
3481: else if ( TD(jd) > TD(id) )
3482: if ( ndl_reducible(jd,id) ) continue;
3483: else ;
3484: else if ( ndl_reducible(id,jd) ) goto delit;
3485: NEXT(d2) = d3;
3486: d3 = d2;
3487: }
3488: NEXT(e) = dd;
3489: dd = e;
3490: continue;
3491: /**/
3492: delit: NEXT(d2) = d3;
3493: d3 = d2;
3494: for ( ; p; p = d2 ) {
3495: d2 = NEXT(p);
3496: NEXT(p) = d3;
3497: d3 = p;
3498: }
3499: FREENDP(e);
3500: }
3501: return dd;
3502: }
3503:
3504: ND_pairs crit_F( ND_pairs d1 )
3505: {
3506: ND_pairs rest, head,remove;
3507: ND_pairs last, p, r, w;
3508: int s;
3509:
3510: if ( !d1 ) return d1;
3511: for ( head = last = 0, p = d1; NEXT(p); ) {
3512: r = w = equivalent_pairs(p,&rest);
3513: s = SG(r);
3514: w = NEXT(w);
3515: while ( w ) {
3516: if ( crit_2(w->i1,w->i2) ) {
3517: r = w;
3518: w = NEXT(w);
3519: while ( w ) {
3520: remove = w;
3521: w = NEXT(w);
3522: FREENDP(remove);
3523: }
3524: break;
3525: } else if ( SG(w) < s ) {
3526: FREENDP(r);
3527: r = w;
3528: s = SG(r);
3529: w = NEXT(w);
3530: } else {
3531: remove = w;
3532: w = NEXT(w);
3533: FREENDP(remove);
3534: }
3535: }
3536: if ( last ) NEXT(last) = r;
3537: else head = r;
3538: NEXT(last = r) = 0;
3539: p = rest;
3540: if ( !p ) return head;
3541: }
3542: if ( !last ) return p;
3543: NEXT(last) = p;
3544: return head;
3545: }
3546:
3547: int crit_2( int dp1, int dp2 )
3548: {
3549: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
3550: }
3551:
3552: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
3553: {
3554: ND_pairs w,p,r,s;
3555: UINT *d;
3556:
3557: w = d1;
3558: d = LCM(w);
3559: s = NEXT(w);
3560: NEXT(w) = 0;
3561: for ( r = 0; s; s = p ) {
3562: p = NEXT(s);
3563: if ( ndl_equal(d,LCM(s)) ) {
3564: NEXT(s) = w; w = s;
3565: } else {
3566: NEXT(s) = r; r = s;
3567: }
3568: }
3569: *prest = r;
3570: return w;
3571: }
3572:
3573: NODE update_base(NODE nd,int ndp)
3574: {
3575: UINT *dl, *dln;
3576: NODE last, p, head;
3577:
3578: dl = DL(nd_psh[ndp]);
3579: for ( head = last = 0, p = nd; p; ) {
3580: dln = DL(nd_psh[(long)BDY(p)]);
3581: if ( ndl_reducible( dln, dl ) ) {
3582: p = NEXT(p);
3583: if ( last ) NEXT(last) = p;
3584: } else {
3585: if ( !last ) head = p;
3586: p = NEXT(last = p);
3587: }
3588: }
3589: head = append_one(head,ndp);
3590: return head;
3591: }
3592:
3593: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
3594: {
3595: ND_pairs m,ml,p,l;
3596: UINT *lcm;
3597: int s,td,len,tlen,c,c1;
3598:
3599: if ( !(p = NEXT(m = d)) ) {
3600: *prest = p;
3601: NEXT(m) = 0;
3602: return m;
3603: }
3604: if ( !NoSugar ) {
3605: if ( nd_sugarweight ) {
3606: s = m->sugar2;
3607: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3608: if ( (p->sugar2 < s)
3609: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3610: ml = l; m = p; s = m->sugar2;
3611: }
3612: } else {
3613: s = SG(m);
3614: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3615: if ( (SG(p) < s)
3616: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
3617: ml = l; m = p; s = SG(m);
3618: }
3619: }
3620: } else {
3621: for ( ml = 0, l = m; p; p = NEXT(l = p) )
3622: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
3623: ml = l; m = p; s = SG(m);
3624: }
3625: }
3626: if ( !ml ) *prest = NEXT(m);
3627: else {
3628: NEXT(ml) = NEXT(m);
3629: *prest = d;
3630: }
3631: NEXT(m) = 0;
3632: return m;
3633: }
3634:
3635: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
3636: {
3637: int msugar,i;
3638: ND_pairs t,dm0,dm,dr0,dr;
3639:
3640: if ( nd_sugarweight ) {
3641: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
3642: if ( t->sugar2 < msugar ) msugar = t->sugar2;
3643: dm0 = 0; dr0 = 0;
3644: for ( i = 0, t = d; t; t = NEXT(t) )
3645: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
3646: if ( dm0 ) NEXT(dm) = t;
3647: else dm0 = t;
3648: dm = t;
3649: i++;
3650: } else {
3651: if ( dr0 ) NEXT(dr) = t;
3652: else dr0 = t;
3653: dr = t;
3654: }
3655: } else {
3656: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
3657: if ( SG(t) < msugar ) msugar = SG(t);
3658: dm0 = 0; dr0 = 0;
3659: for ( i = 0, t = d; t; t = NEXT(t) )
3660: if ( i < nd_f4_nsp && SG(t) == msugar ) {
3661: if ( dm0 ) NEXT(dm) = t;
3662: else dm0 = t;
3663: dm = t;
3664: i++;
3665: } else {
3666: if ( dr0 ) NEXT(dr) = t;
3667: else dr0 = t;
3668: dr = t;
3669: }
3670: }
3671: NEXT(dm) = 0;
3672: if ( dr0 ) NEXT(dr) = 0;
3673: *prest = dr0;
3674: return dm0;
3675: }
3676:
1.28 noro 3677: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
3678: {
3679: int msugar;
3680: ND_pairs t,last;
3681:
3682: #if 0
3683: for ( msugar = SG(d), t = d; t; t = NEXT(t) )
3684: if ( SG(t) == msugar ) last = t;
3685: #else
3686: msugar = (d->sig->dl->td)+nd_sba_hm[d->sig->pos]->td;
3687: for ( t = d; t; t = NEXT(t) )
3688: if ( ((t->sig->dl->td)+nd_sba_hm[t->sig->pos]->td) == msugar ) last = t;
3689: #endif
3690: *prest = last->next;
3691: last->next = 0;
3692: return d;
3693: }
3694:
1.1 noro 3695: int nd_tdeg(NDV c)
3696: {
3697: int wmax = 0;
3698: int i,len;
3699: NMV a;
3700:
3701: len = LEN(c);
3702: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3703: wmax = MAX(TD(DL(a)),wmax);
3704: return wmax;
3705: }
3706:
1.24 noro 3707: int ndv_newps(int m,NDV a,NDV aq)
1.1 noro 3708: {
3709: int len;
3710: RHist r;
3711: NDV b;
3712: NODE tn;
3713: LIST l;
3714: Z iq;
3715:
3716: if ( nd_psn == nd_pslen ) {
3717: nd_pslen *= 2;
3718: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
3719: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
3720: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
3721: nd_bound = (UINT **)
3722: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
3723: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
3724: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
3725: }
3726: NEWRHist(r); nd_psh[nd_psn] = r;
3727: nd_ps[nd_psn] = a;
3728: if ( aq ) {
3729: nd_ps_trace[nd_psn] = aq;
3730: if ( !m ) {
3731: register_hcf(aq);
3732: } else if ( m == -2 ) {
3733: /* do nothing */
3734: } else
3735: error("ndv_newps : invalud modulus");
3736: nd_bound[nd_psn] = ndv_compute_bound(aq);
3737: #if 1
3738: SG(r) = SG(aq);
3739: #else
3740: SG(r) = nd_tdeg(aq);
3741: #endif
3742: ndl_copy(HDL(aq),DL(r));
1.24 noro 3743: r->sig = dup_sig(aq->sig);
1.1 noro 3744: } else {
3745: if ( !m ) register_hcf(a);
3746: nd_bound[nd_psn] = ndv_compute_bound(a);
3747: #if 1
3748: SG(r) = SG(a);
3749: #else
3750: SG(r) = nd_tdeg(a);
3751: #endif
3752: ndl_copy(HDL(a),DL(r));
1.24 noro 3753: r->sig = dup_sig(a->sig);
1.1 noro 3754: }
3755: if ( nd_demand ) {
3756: if ( aq ) {
3757: ndv_save(nd_ps_trace[nd_psn],nd_psn);
3758: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3759: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
3760: nd_ps_trace[nd_psn] = 0;
3761: } else {
3762: ndv_save(nd_ps[nd_psn],nd_psn);
3763: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
3764: nd_ps[nd_psn] = 0;
3765: }
3766: }
3767: if ( nd_gentrace ) {
3768: /* reverse the tracelist and append it to alltracelist */
3769: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 3770: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 3771: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
3772: }
3773: return nd_psn++;
3774: }
3775:
3776: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
3777: /* return 1 if success, 0 if failure (HC(a mod p)) */
3778:
1.24 noro 3779: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1 noro 3780: {
1.6 noro 3781: int i,j,td,len,max;
3782: NODE s,s0,f0,tn;
3783: UINT *d;
3784: RHist r;
3785: NDVI w;
3786: NDV a,am;
3787: union oNDC hc;
3788: NODE node;
3789: P hcp;
3790: Z iq,jq;
3791: LIST l;
3792:
3793: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3794: /* initialize the tracelist */
3795: nd_tracelist = 0;
3796:
3797: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3798: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3799: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3800: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3801: if ( !dont_sort ) {
3802: /* XXX heuristic */
1.26 noro 3803: if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6 noro 3804: qsort(w,nd_psn,sizeof(struct oNDVI),
3805: (int (*)(const void *,const void *))ndvi_compare_rev);
3806: else
3807: qsort(w,nd_psn,sizeof(struct oNDVI),
3808: (int (*)(const void *,const void *))ndvi_compare);
3809: }
3810: nd_pslen = 2*nd_psn;
3811: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3812: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3813: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3814: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3815: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3816: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3817: nd_hcf = 0;
1.1 noro 3818:
1.6 noro 3819: if ( trace && nd_vc )
3820: makesubst(nd_vc,&nd_subst);
3821: else
3822: nd_subst = 0;
1.1 noro 3823:
1.6 noro 3824: if ( !nd_red )
3825: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3826: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3827: for ( i = 0; i < nd_psn; i++ ) {
3828: hc = HCU(w[i].p);
3829: if ( trace ) {
3830: if ( mod == -2 ) {
3831: /* over a large finite field */
3832: /* trace = small modulus */
3833: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3834: ndv_mod(-2,a);
3835: if ( !dont_removecont) ndv_removecont(-2,a);
3836: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3837: ndv_mod(trace,am);
3838: if ( DL_COMPARE(HDL(am),HDL(a)) )
3839: return 0;
3840: ndv_removecont(trace,am);
3841: } else {
3842: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3843: if ( !dont_removecont) ndv_removecont(0,a);
3844: register_hcf(a);
3845: am = nd_ps[i] = ndv_dup(mod,a);
3846: ndv_mod(mod,am);
3847: if ( DL_COMPARE(HDL(am),HDL(a)) )
3848: return 0;
3849: ndv_removecont(mod,am);
3850: }
3851: } else {
3852: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3853: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3854: if ( !mod ) register_hcf(a);
1.1 noro 3855: }
1.6 noro 3856: if ( nd_gentrace ) {
3857: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3858: /* exact division */
1.1 noro 3859: if ( !dont_removecont )
1.6 noro 3860: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3861: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3862: }
3863: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3864: nd_bound[i] = ndv_compute_bound(a);
3865: nd_psh[i] = r;
3866: if ( nd_demand ) {
3867: if ( trace ) {
3868: ndv_save(nd_ps_trace[i],i);
3869: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3870: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3871: nd_ps_trace[i] = 0;
3872: } else {
3873: ndv_save(nd_ps[i],i);
3874: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3875: nd_ps[i] = 0;
3876: }
1.1 noro 3877: }
1.6 noro 3878: }
1.24 noro 3879: if ( sba ) {
1.27 noro 3880: nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24 noro 3881: // setup signatures
1.27 noro 3882: for ( i = 0; i < nd_psn; i++ ) {
3883: SIG sig;
1.24 noro 3884:
1.27 noro 3885: NEWSIG(sig); sig->pos = i;
3886: nd_ps[i]->sig = sig;
3887: if ( nd_demand ) nd_ps_sym[i]->sig = sig;
3888: nd_psh[i]->sig = sig;
3889: if ( trace ) {
3890: nd_ps_trace[i]->sig = sig;
3891: if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
3892: }
3893: NEWDL(nd_sba_hm[i],nd_nvar);
3894: _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
3895: }
1.29 noro 3896: nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
3897: for ( i = 0; i < nd_psn; i++ ) {
3898: j = nd_psh[i]->sig->pos;
3899: nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
3900: }
1.24 noro 3901: }
1.6 noro 3902: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3903: return 1;
1.1 noro 3904: }
3905:
3906: struct order_spec *append_block(struct order_spec *spec,
3907: int nv,int nalg,int ord);
3908:
3909: extern VECT current_dl_weight_vector_obj;
3910: static VECT prev_weight_vector_obj;
3911:
3912: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3913: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3914: {
3915: NODE alist,t,s,r0,r,arg;
3916: VL tv;
3917: P poly;
3918: DP d;
3919: Alg alpha,dp;
3920: DAlg inv,da,hc;
3921: MP m;
3922: int i,nvar,nalg,n;
3923: NumberField nf;
3924: LIST f1,f2;
3925: struct order_spec *current_spec;
3926: VECT obj,obj0;
3927: VECT tmp;
3928:
3929: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3930: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3931:
3932: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3933: NEXTNODE(alist,t); MKV(tv->v,poly);
3934: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3935: tv->v = tv->v->priv;
3936: }
3937: NEXT(t) = 0;
3938:
3939: /* simplification, making polynomials monic */
3940: setfield_dalg(alist);
3941: obj_algtodalg((Obj)f,(Obj *)&f1);
3942: for ( t = BDY(f); t; t = NEXT(t) ) {
3943: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3944: hc = (DAlg)BDY(d)->c;
3945: if ( NID(hc) == N_DA ) {
3946: invdalg(hc,&inv);
3947: for ( m = BDY(d); m; m = NEXT(m) ) {
3948: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3949: }
3950: }
3951: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3952: }
3953: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3954:
3955: /* append alg vars to the var list */
3956: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3957: NEXT(tv) = av;
3958:
3959: /* append a block to ord */
3960: *ord1p = append_block(ord,nvar,nalg,2);
3961:
3962: /* create generator list */
3963: nf = get_numberfield();
3964: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3965: MKAlg(nf->defpoly[i],dp);
3966: MKNODE(s,dp,t); t = s;
3967: }
3968: MKLIST(f1,t);
3969: *alistp = alist;
3970: algobjtorat((Obj)f1,(Obj *)f1p);
3971:
3972: /* creating a new weight vector */
3973: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3974: n = nvar+nalg+1;
3975: MKVECT(obj,n);
3976: if ( obj0 && obj0->len == nvar )
3977: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3978: else
3979: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3980: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3981: BDY(obj)[n-1] = (pointer)ONE;
3982: arg = mknode(1,obj);
3983: Pdp_set_weight(arg,&tmp);
3984: }
3985:
3986: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3987: {
3988: NODE s,t,u0,u;
3989: P p;
3990: VL tv;
3991: Obj obj;
3992: VECT tmp;
3993: NODE arg;
3994:
3995: u0 = 0;
3996: for ( t = r; t; t = NEXT(t) ) {
3997: p = (P)BDY(t);
3998: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3999: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
4000: }
4001: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
4002: NEXTNODE(u0,u);
4003: BDY(u) = (pointer)p;
4004: }
4005: }
4006: arg = mknode(1,prev_weight_vector_obj);
4007: Pdp_set_weight(arg,&tmp);
4008:
4009: return u0;
4010: }
4011:
4012: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
4013: {
4014: VL tv,fv,vv,vc,av;
4015: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4016: int e,max,nvar,i;
4017: NDV b;
4018: int ishomo,nalg,mrank,trank,wmax,len;
4019: NMV a;
4020: Alg alpha,dp;
4021: P p,zp;
4022: Q dmy;
4023: LIST f1,f2,zpl;
4024: Obj obj;
4025: NumberField nf;
4026: struct order_spec *ord1;
4027: NODE tr,tl1,tl2,tl3,tl4,nzlist;
4028: LIST l1,l2,l3,l4,l5;
4029: int j;
4030: Z jq,bpe,last_nonzero;
4031: int *perm;
4032: EPOS oepos;
4033: int obpe,oadv,ompos,cbpe;
1.15 noro 4034: VECT hvect;
1.1 noro 4035:
4036: nd_module = 0;
4037: if ( !m && Demand ) nd_demand = 1;
4038: else nd_demand = 0;
4039: parse_nd_option(current_option);
4040:
4041: if ( DP_Multiple )
4042: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4043: #if 0
4044: ndv_alloc = 0;
4045: #endif
4046: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4047: if ( m && nd_vc )
4048: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4049: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4050: switch ( ord->id ) {
4051: case 1:
4052: if ( ord->nv != nvar )
4053: error("nd_{gr,f4} : invalid order specification");
4054: break;
4055: default:
4056: break;
4057: }
4058: nd_nalg = 0;
4059: av = 0;
4060: if ( !m ) {
4061: get_algtree((Obj)f,&av);
4062: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4063: nd_ntrans = nvar;
4064: nd_nalg = nalg;
4065: /* #i -> t#i */
4066: if ( nalg ) {
4067: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4068: ord = ord1;
4069: f = f1;
4070: }
4071: nvar += nalg;
4072: }
4073: nd_init_ord(ord);
4074: mrank = 0;
4075: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4076: for ( tv = vv; tv; tv = NEXT(tv) ) {
4077: if ( nd_module ) {
1.16 noro 4078: if ( OID(BDY(t)) == O_DPM ) {
4079: e = dpm_getdeg((DPM)BDY(t),&trank);
4080: max = MAX(e,max);
4081: mrank = MAX(mrank,trank);
4082: } else {
4083: s = BDY((LIST)BDY(t));
4084: trank = length(s);
4085: mrank = MAX(mrank,trank);
4086: for ( ; s; s = NEXT(s) ) {
4087: e = getdeg(tv->v,(P)BDY(s));
4088: max = MAX(e,max);
4089: }
1.1 noro 4090: }
4091: } else {
4092: e = getdeg(tv->v,(P)BDY(t));
4093: max = MAX(e,max);
4094: }
4095: }
4096: nd_setup_parameters(nvar,nd_nzlist?0:max);
4097: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4098: ishomo = 1;
4099: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4100: if ( nd_module ) {
1.16 noro 4101: if ( OID(BDY(t)) == O_DPM ) {
4102: Z cont;
4103: DPM zdpm;
4104:
4105: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
4106: else zdpm = (DPM)BDY(t);
4107: b = (pointer)dpmtondv(m,zdpm);
4108: } else {
4109: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4110: else zpl = (LIST)BDY(t);
1.1 noro 4111: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4112: }
1.1 noro 4113: } else {
4114: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4115: else zp = (P)BDY(t);
4116: b = (pointer)ptondv(CO,vv,zp);
4117: }
4118: if ( ishomo )
4119: ishomo = ishomo && ndv_ishomo(b);
4120: if ( m ) ndv_mod(m,b);
4121: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4122: }
4123: if ( fd0 ) NEXT(fd) = 0;
4124:
4125: if ( !ishomo && homo ) {
4126: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4127: b = (NDV)BDY(t); len = LEN(b);
4128: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4129: wmax = MAX(TD(DL(a)),wmax);
4130: }
4131: homogenize_order(ord,nvar,&ord1);
4132: nd_init_ord(ord1);
4133: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4134: for ( t = fd0; t; t = NEXT(t) )
4135: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4136: }
4137:
1.24 noro 4138: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1 noro 4139: if ( nd_gentrace ) {
4140: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4141: }
4142: if ( nd_splist ) {
4143: *rp = compute_splist();
4144: return;
4145: }
4146: if ( nd_check_splist ) {
4147: if ( f4 ) {
4148: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
4149: else *rp = 0;
4150: } else {
4151: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
4152: else *rp = 0;
4153: }
4154: return;
4155: }
4156: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
4157: if ( !x ) {
4158: *rp = 0; return;
4159: }
1.15 noro 4160: if ( nd_gentrace ) {
4161: MKVECT(hvect,nd_psn);
4162: for ( i = 0; i < nd_psn; i++ )
4163: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4164: }
1.1 noro 4165: if ( !ishomo && homo ) {
4166: /* dehomogenization */
4167: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4168: nd_init_ord(ord);
4169: nd_setup_parameters(nvar,0);
4170: }
4171: nd_demand = 0;
4172: if ( nd_module && nd_intersect ) {
4173: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 4174: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 4175: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
4176: }
4177: conv_ilist(nd_demand,0,x,0);
4178: goto FINAL;
4179: }
4180: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
4181: x = ndv_reducebase(x,perm);
4182: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4183: x = ndv_reduceall(m,x);
4184: cbpe = nd_bpe;
4185: if ( nd_gentrace && !f4 ) {
4186: tl2 = nd_alltracelist; nd_alltracelist = 0;
4187: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
4188: tl3 = nd_alltracelist; nd_alltracelist = 0;
4189: if ( nd_gensyz ) {
4190: nd_gb(m,0,1,1,0);
4191: tl4 = nd_alltracelist; nd_alltracelist = 0;
4192: } else tl4 = 0;
4193: }
4194: nd_bpe = cbpe;
4195: nd_setup_parameters(nd_nvar,0);
4196: FINAL:
4197: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 4198: NEXTNODE(r0,r);
4199: if ( nd_module ) {
4200: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
4201: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
4202: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4203: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 4204: }
4205: if ( r0 ) NEXT(r) = 0;
4206: if ( !m && nd_nalg )
4207: r0 = postprocess_algcoef(av,alist,r0);
4208: MKLIST(*rp,r0);
4209: if ( nd_gentrace ) {
4210: if ( f4 ) {
1.6 noro 4211: STOZ(16,bpe);
4212: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 4213: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 4214: } else {
4215: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4216: tl3 = reverse_node(tl3);
4217: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4218: for ( t = tl2; t; t = NEXT(t) ) {
4219: /* s = [i,[*,j,*,*],...] */
4220: s = BDY((LIST)BDY(t));
1.6 noro 4221: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4222: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4223: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4224: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4225: }
4226: }
4227: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4228: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4229: }
4230: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4231: MKLIST(l5,tl4);
1.6 noro 4232: STOZ(nd_bpe,bpe);
1.15 noro 4233: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4234: }
4235: }
4236: #if 0
4237: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
4238: #endif
4239: }
4240:
1.28 noro 4241: NODE nd_sba_f4(int m,int **indp);
4242:
4243: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24 noro 4244: {
4245: VL tv,fv,vv,vc,av;
4246: NODE fd,fd0,r,r0,t,x,s,xx;
4247: int e,max,nvar,i;
4248: NDV b;
4249: int ishomo,nalg,wmax,len;
4250: NMV a;
4251: P p,zp;
4252: Q dmy;
4253: struct order_spec *ord1;
4254: int j;
4255: int *perm;
4256: EPOS oepos;
4257: int obpe,oadv,ompos,cbpe;
1.29 noro 4258: struct oEGT eg0,eg1,egconv;
1.24 noro 4259:
4260: nd_module = 0;
4261: nd_demand = 0;
4262: parse_nd_option(current_option);
4263:
4264: if ( DP_Multiple )
4265: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4266: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4267: if ( m && nd_vc )
4268: error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
4269: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4270: switch ( ord->id ) {
4271: case 1:
4272: if ( ord->nv != nvar )
4273: error("nd_sba : invalid order specification");
4274: break;
4275: default:
4276: break;
4277: }
4278: nd_nalg = 0;
4279: nd_init_ord(ord);
4280: // for SIG comparison
4281: initd(ord);
4282: for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
4283: for ( tv = vv; tv; tv = NEXT(tv) ) {
4284: e = getdeg(tv->v,(P)BDY(t));
4285: max = MAX(e,max);
4286: }
4287: }
1.25 noro 4288: nd_setup_parameters(nvar,max);
1.24 noro 4289: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4290: ishomo = 1;
4291: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4292: if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
4293: else zp = (P)BDY(t);
4294: b = (pointer)ptondv(CO,vv,zp);
4295: if ( ishomo )
4296: ishomo = ishomo && ndv_ishomo(b);
4297: if ( m ) ndv_mod(m,b);
4298: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4299: }
4300: if ( fd0 ) NEXT(fd) = 0;
4301:
4302: if ( !ishomo && homo ) {
4303: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
4304: b = (NDV)BDY(t); len = LEN(b);
4305: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
4306: wmax = MAX(TD(DL(a)),wmax);
4307: }
4308: homogenize_order(ord,nvar,&ord1);
4309: nd_init_ord(ord1);
4310: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
4311: for ( t = fd0; t; t = NEXT(t) )
4312: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4313: }
4314:
4315: ndv_setup(m,0,fd0,0,0,1);
1.28 noro 4316: x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24 noro 4317: if ( !x ) {
4318: *rp = 0; return;
4319: }
4320: if ( !ishomo && homo ) {
4321: /* dehomogenization */
4322: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4323: nd_init_ord(ord);
4324: nd_setup_parameters(nvar,0);
4325: }
4326: nd_demand = 0;
4327: x = ndv_reducebase(x,perm);
4328: x = ndv_reduceall(m,x);
4329: nd_setup_parameters(nd_nvar,0);
1.29 noro 4330: get_eg(&eg0);
1.24 noro 4331: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4332: NEXTNODE(r0,r);
4333: if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
4334: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4335: }
4336: if ( r0 ) NEXT(r) = 0;
4337: MKLIST(*rp,r0);
1.29 noro 4338: get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
4339: print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24 noro 4340: }
4341:
1.1 noro 4342: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
4343: {
4344: VL tv,fv,vv,vc,av;
4345: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4346: int e,max,nvar,i;
4347: NDV b;
4348: int ishomo,nalg;
4349: Alg alpha,dp;
4350: P p,zp;
4351: Q dmy;
4352: LIST f1,f2;
4353: Obj obj;
4354: NumberField nf;
4355: struct order_spec *ord1;
4356: int *perm;
4357:
4358: parse_nd_option(current_option);
4359: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4360: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4361: switch ( ord->id ) {
4362: case 1:
4363: if ( ord->nv != nvar )
4364: error("nd_check : invalid order specification");
4365: break;
4366: default:
4367: break;
4368: }
4369: nd_nalg = 0;
4370: av = 0;
4371: if ( !m ) {
4372: get_algtree((Obj)f,&av);
4373: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4374: nd_ntrans = nvar;
4375: nd_nalg = nalg;
4376: /* #i -> t#i */
4377: if ( nalg ) {
4378: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4379: ord = ord1;
4380: f = f1;
4381: }
4382: nvar += nalg;
4383: }
4384: nd_init_ord(ord);
4385: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4386: for ( tv = vv; tv; tv = NEXT(tv) ) {
4387: e = getdeg(tv->v,(P)BDY(t));
4388: max = MAX(e,max);
4389: }
4390: nd_setup_parameters(nvar,max);
4391: ishomo = 1;
4392: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4393: ptozp((P)BDY(t),1,&dmy,&zp);
4394: b = (pointer)ptondv(CO,vv,zp);
4395: if ( ishomo )
4396: ishomo = ishomo && ndv_ishomo(b);
4397: if ( m ) ndv_mod(m,b);
4398: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
4399: }
4400: if ( fd0 ) NEXT(fd) = 0;
1.24 noro 4401: ndv_setup(m,0,fd0,0,1,0);
1.1 noro 4402: for ( x = 0, i = 0; i < nd_psn; i++ )
4403: x = update_base(x,i);
4404: if ( do_check ) {
4405: x = nd_gb(m,ishomo,1,0,&perm);
4406: if ( !x ) {
4407: *rp = 0;
4408: return;
4409: }
4410: } else {
4411: #if 0
4412: /* bug ? */
4413: for ( t = x; t; t = NEXT(t) )
4414: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
4415: #else
4416: conv_ilist(0,0,x,&perm);
4417: #endif
4418: }
4419: x = ndv_reducebase(x,perm);
4420: x = ndv_reduceall(m,x);
4421: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
4422: NEXTNODE(r0,r);
4423: BDY(r) = ndvtop(m,CO,vv,BDY(t));
4424: }
4425: if ( r0 ) NEXT(r) = 0;
4426: if ( !m && nd_nalg )
4427: r0 = postprocess_algcoef(av,alist,r0);
4428: MKLIST(*rp,r0);
4429: }
4430:
4431: NDV recompute_trace(NODE trace,NDV *p,int m);
4432: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
4433:
4434: NDV recompute_trace(NODE ti,NDV *p,int mod)
4435: {
4436: int c,c1,c2,i;
4437: NM mul,m,tail;
4438: ND d,r,rm;
4439: NODE sj;
4440: NDV red;
4441: Obj mj;
4442:
4443: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4444: CM(mul) = 1;
4445: tail = 0;
4446: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
4447: sj = BDY((LIST)BDY(ti));
4448: if ( ARG0(sj) ) {
1.6 noro 4449: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 4450: mj = (Obj)ARG2(sj);
4451: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
4452: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
4453: rm = ndv_mul_nm(mod,mul,red);
4454: if ( !r ) r = rm;
4455: else {
4456: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
4457: if ( d ) {
4458: NEXT(tail) = m; tail = m; LEN(d)++;
4459: } else {
4460: MKND(nd_nvar,m,1,d); tail = BDY(d);
4461: }
4462: }
4463: if ( !m ) return 0; /* failure */
4464: else {
4465: BDY(r) = m;
4466: if ( mod > 0 || mod == -1 ) {
4467: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
4468: DMAR(c1,c2,0,mod,c);
4469: nd_mul_c(mod,rm,c);
4470: } else {
4471: Z t,u;
4472:
4473: chsgnlf(HCZ(r),&t);
4474: divlf(t,HCZ(rm),&u);
4475: nd_mul_c_lf(rm,u);
4476: }
4477: r = nd_add(mod,r,rm);
4478: }
4479: }
4480: }
4481: }
4482: if ( tail ) NEXT(tail) = 0;
4483: d = nd_add(mod,d,r);
4484: nd_mul_c(mod,d,invm(HCM(d),mod));
4485: return ndtondv(mod,d);
4486: }
4487:
4488: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
4489: {
4490: VL tv,fv,vv,vc,av;
4491: NODE fd,fd0,r,r0,t,x,s,xx,alist;
4492: int e,max,nvar,i;
4493: NDV b;
4494: int ishomo,nalg;
4495: Alg alpha,dp;
4496: P p,zp;
4497: Q dmy;
4498: LIST f1,f2;
4499: Obj obj;
4500: NumberField nf;
4501: struct order_spec *ord1;
4502: NODE permtrace,intred,ind,perm,trace,ti;
4503: int len,n,j;
4504: NDV *db,*pb;
4505:
4506: parse_nd_option(current_option);
4507: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4508: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4509: switch ( ord->id ) {
4510: case 1:
4511: if ( ord->nv != nvar )
4512: error("nd_check : invalid order specification");
4513: break;
4514: default:
4515: break;
4516: }
4517: nd_init_ord(ord);
1.6 noro 4518: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 4519: nd_setup_parameters(nvar,0);
4520:
4521: len = length(BDY(f));
4522: db = (NDV *)MALLOC(len*sizeof(NDV *));
4523: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
4524: ptozp((P)BDY(t),1,&dmy,&zp);
4525: b = ptondv(CO,vv,zp);
4526: ndv_mod(m,b);
4527: ndv_mul_c(m,b,invm(HCM(b),m));
4528: db[i] = b;
4529: }
4530:
4531: permtrace = BDY((LIST)ARG2(BDY(tlist)));
4532: intred = BDY((LIST)ARG3(BDY(tlist)));
4533: ind = BDY((LIST)ARG4(BDY(tlist)));
4534: perm = BDY((LIST)ARG0(permtrace));
4535: trace = NEXT(permtrace);
4536:
4537: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 4538: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 4539: if ( j > i ) i = j;
4540: }
4541: n = i+1;
4542: pb = (NDV *)MALLOC(n*sizeof(NDV *));
4543: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
4544: ti = BDY((LIST)BDY(t));
1.6 noro 4545: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 4546: }
4547: for ( t = trace; t; t = NEXT(t) ) {
4548: ti = BDY((LIST)BDY(t));
1.6 noro 4549: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4550: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4551: if ( DP_Print ) {
4552: fprintf(asir_out,"."); fflush(asir_out);
4553: }
4554: }
4555: for ( t = intred; t; t = NEXT(t) ) {
4556: ti = BDY((LIST)BDY(t));
1.6 noro 4557: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
4558: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 4559: if ( DP_Print ) {
4560: fprintf(asir_out,"*"); fflush(asir_out);
4561: }
4562: }
4563: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
4564: NEXTNODE(r0,r);
1.6 noro 4565: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 4566: ndv_mul_c(m,b,invm(HCM(b),m));
4567: #if 0
1.6 noro 4568: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4569: #else
1.6 noro 4570: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 4571: #endif
4572: }
4573: if ( r0 ) NEXT(r) = 0;
4574: MKLIST(*rp,r0);
4575: if ( DP_Print ) fprintf(asir_out,"\n");
4576: }
4577:
1.16 noro 4578: 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 4579: {
4580: VL tv,fv,vv,vc,av;
4581: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
4582: int m,nocheck,nvar,mindex,e,max;
4583: NDV c;
4584: NMV a;
4585: P p,zp;
4586: Q dmy;
4587: EPOS oepos;
4588: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
4589: Alg alpha,dp;
4590: P poly;
4591: LIST f1,f2,zpl;
4592: Obj obj;
4593: NumberField nf;
4594: struct order_spec *ord1;
4595: struct oEGT eg_check,eg0,eg1;
4596: NODE tr,tl1,tl2,tl3,tl4;
4597: LIST l1,l2,l3,l4,l5;
4598: int *perm;
4599: int j,ret;
4600: Z jq,bpe;
1.15 noro 4601: VECT hvect;
1.1 noro 4602:
4603: nd_module = 0;
4604: nd_lf = 0;
4605: parse_nd_option(current_option);
4606: if ( nd_lf ) {
4607: if ( f4 )
4608: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
4609: else
4610: error("nd_gr_trace is not implemented yet over a large finite field");
4611: return;
4612: }
4613: if ( DP_Multiple )
4614: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
4615:
4616: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
4617: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
4618: switch ( ord->id ) {
4619: case 1:
4620: if ( ord->nv != nvar )
4621: error("nd_gr_trace : invalid order specification");
4622: break;
4623: default:
4624: break;
4625: }
4626:
4627: get_algtree((Obj)f,&av);
4628: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
4629: nd_ntrans = nvar;
4630: nd_nalg = nalg;
4631: /* #i -> t#i */
4632: if ( nalg ) {
4633: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
4634: ord = ord1;
4635: f = f1;
4636: }
4637: nvar += nalg;
4638:
4639: nocheck = 0;
4640: mindex = 0;
4641:
4642: if ( Demand ) nd_demand = 1;
4643: else nd_demand = 0;
4644:
4645: /* setup modulus */
4646: if ( trace < 0 ) {
4647: trace = -trace;
4648: nocheck = 1;
4649: }
4650: m = trace > 1 ? trace : get_lprime(mindex);
4651: nd_init_ord(ord);
4652: mrank = 0;
4653: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
4654: for ( tv = vv; tv; tv = NEXT(tv) ) {
4655: if ( nd_module ) {
1.16 noro 4656: if ( OID(BDY(t)) == O_DPM ) {
4657: e = dpm_getdeg((DPM)BDY(t),&trank);
4658: max = MAX(e,max);
4659: mrank = MAX(mrank,trank);
4660: } else {
1.1 noro 4661: s = BDY((LIST)BDY(t));
4662: trank = length(s);
4663: mrank = MAX(mrank,trank);
4664: for ( ; s; s = NEXT(s) ) {
4665: e = getdeg(tv->v,(P)BDY(s));
4666: max = MAX(e,max);
4667: }
1.16 noro 4668: }
1.1 noro 4669: } else {
4670: e = getdeg(tv->v,(P)BDY(t));
4671: max = MAX(e,max);
4672: }
4673: }
4674: nd_setup_parameters(nvar,max);
4675: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
4676: ishomo = 1;
4677: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
4678: if ( nd_module ) {
1.16 noro 4679: if ( OID(BDY(t)) == O_DPM ) {
4680: Z cont;
4681: DPM zdpm;
4682:
1.17 noro 4683: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 4684: else zdpm = (DPM)BDY(t);
4685: c = (pointer)dpmtondv(m,zdpm);
4686: } else {
4687: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
4688: else zpl = (LIST)BDY(t);
1.1 noro 4689: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 4690: }
1.1 noro 4691: } else {
1.16 noro 4692: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
4693: else zp = (P)BDY(t);
4694: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 4695: }
4696: if ( ishomo )
4697: ishomo = ishomo && ndv_ishomo(c);
4698: if ( c ) {
4699: NEXTNODE(in0,in); BDY(in) = (pointer)c;
4700: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
4701: }
4702: }
4703: if ( in0 ) NEXT(in) = 0;
4704: if ( fd0 ) NEXT(fd) = 0;
4705: if ( !ishomo && homo ) {
4706: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
4707: c = (NDV)BDY(t); len = LEN(c);
4708: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
4709: wmax = MAX(TD(DL(a)),wmax);
4710: }
4711: homogenize_order(ord,nvar,&ord1);
4712: nd_init_ord(ord1);
4713: nd_setup_parameters(nvar+1,wmax);
4714: for ( t = fd0; t; t = NEXT(t) )
4715: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
4716: }
4717: if ( MaxDeg > 0 ) nocheck = 1;
4718: while ( 1 ) {
4719: tl1 = tl2 = tl3 = tl4 = 0;
4720: if ( Demand )
4721: nd_demand = 1;
1.24 noro 4722: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1 noro 4723: if ( nd_gentrace ) {
4724: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
4725: }
4726: if ( ret )
4727: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
4728: if ( !ret || !cand ) {
4729: /* failure */
4730: if ( trace > 1 ) { *rp = 0; return; }
4731: else m = get_lprime(++mindex);
4732: continue;
4733: }
1.15 noro 4734: if ( nd_gentrace ) {
4735: MKVECT(hvect,nd_psn);
4736: for ( i = 0; i < nd_psn; i++ )
4737: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
4738: }
1.1 noro 4739: if ( !ishomo && homo ) {
4740: /* dehomogenization */
4741: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
4742: nd_init_ord(ord);
4743: nd_setup_parameters(nvar,0);
4744: }
4745: nd_demand = 0;
4746: cand = ndv_reducebase(cand,perm);
4747: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
4748: cand = ndv_reduceall(0,cand);
4749: cbpe = nd_bpe;
4750: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
4751: get_eg(&eg0);
4752: if ( nocheck )
4753: break;
4754: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
4755: if ( nd_gentrace ) {
4756: tl3 = nd_alltracelist; nd_alltracelist = 0;
4757: } else tl3 = 0;
4758: /* gbcheck : cand is a GB of Id(cand) ? */
4759: if ( nd_vc || nd_gentrace || nd_gensyz )
4760: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
4761: else
4762: ret = nd_f4(0,1,0)!=0;
4763: if ( nd_gentrace && nd_gensyz ) {
4764: tl4 = nd_alltracelist; nd_alltracelist = 0;
4765: } else tl4 = 0;
4766: }
4767: if ( ret ) break;
4768: else if ( trace > 1 ) {
4769: /* failure */
4770: *rp = 0; return;
4771: } else {
4772: /* try the next modulus */
4773: m = get_lprime(++mindex);
4774: /* reset the parameters */
4775: if ( !ishomo && homo ) {
4776: nd_init_ord(ord1);
4777: nd_setup_parameters(nvar+1,wmax);
4778: } else {
4779: nd_init_ord(ord);
4780: nd_setup_parameters(nvar,max);
4781: }
4782: }
4783: }
4784: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
4785: if ( DP_Print )
1.6 noro 4786: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 4787: /* dp->p */
4788: nd_bpe = cbpe;
4789: nd_setup_parameters(nd_nvar,0);
4790: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 4791: if ( nd_module ) {
1.17 noro 4792: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 4793: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 4794: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
4795: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 4796: }
4797: if ( nd_nalg )
4798: cand = postprocess_algcoef(av,alist,cand);
4799: MKLIST(*rp,cand);
4800: if ( nd_gentrace ) {
4801: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
4802: tl3 = reverse_node(tl3);
4803: /* tl2 = [[i,[[*,j,*,*],...]],...] */
4804: for ( t = tl2; t; t = NEXT(t) ) {
4805: /* s = [i,[*,j,*,*],...] */
4806: s = BDY((LIST)BDY(t));
1.6 noro 4807: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 4808: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 4809: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 4810: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
4811: }
4812: }
4813: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 4814: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 4815: }
4816: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
4817: MKLIST(l5,tl4);
1.6 noro 4818: STOZ(nd_bpe,bpe);
1.15 noro 4819: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 4820: }
4821: }
4822:
4823: /* XXX : module element is not considered */
4824:
4825: void dltondl(int n,DL dl,UINT *r)
4826: {
4827: UINT *d;
4828: int i,j,l,s,ord_l;
4829: struct order_pair *op;
4830:
4831: d = (unsigned int *)dl->d;
4832: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
4833: if ( nd_blockmask ) {
4834: l = nd_blockmask->n;
4835: op = nd_blockmask->order_pair;
4836: for ( j = 0, s = 0; j < l; j++ ) {
4837: ord_l = op[j].length;
4838: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
4839: }
4840: TD(r) = ndl_weight(r);
4841: ndl_weight_mask(r);
4842: } else {
4843: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
4844: TD(r) = ndl_weight(r);
4845: }
4846: }
4847:
4848: DL ndltodl(int n,UINT *ndl)
4849: {
4850: DL dl;
4851: int *d;
4852: int i,j,l,s,ord_l;
4853: struct order_pair *op;
4854:
4855: NEWDL(dl,n);
4856: dl->td = TD(ndl);
4857: d = dl->d;
4858: if ( nd_blockmask ) {
4859: l = nd_blockmask->n;
4860: op = nd_blockmask->order_pair;
4861: for ( j = 0, s = 0; j < l; j++ ) {
4862: ord_l = op[j].length;
4863: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4864: }
4865: } else {
4866: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4867: }
4868: return dl;
4869: }
4870:
1.24 noro 4871: void _ndltodl(UINT *ndl,DL dl)
4872: {
4873: int *d;
4874: int i,j,l,s,ord_l,n;
4875: struct order_pair *op;
4876:
4877: n = nd_nvar;
4878: dl->td = TD(ndl);
4879: d = dl->d;
4880: if ( nd_blockmask ) {
4881: l = nd_blockmask->n;
4882: op = nd_blockmask->order_pair;
4883: for ( j = 0, s = 0; j < l; j++ ) {
4884: ord_l = op[j].length;
4885: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
4886: }
4887: } else {
4888: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
4889: }
4890: }
4891:
1.1 noro 4892: void nmtodp(int mod,NM m,DP *r)
4893: {
4894: DP dp;
4895: MP mr;
4896:
4897: NEWMP(mr);
4898: mr->dl = ndltodl(nd_nvar,DL(m));
4899: mr->c = (Obj)ndctop(mod,m->c);
4900: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4901: *r = dp;
4902: }
4903:
1.15 noro 4904: void ndltodp(UINT *d,DP *r)
4905: {
4906: DP dp;
4907: MP mr;
4908:
4909: NEWMP(mr);
4910: mr->dl = ndltodl(nd_nvar,d);
4911: mr->c = (Obj)ONE;
4912: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
4913: *r = dp;
4914: }
4915:
1.1 noro 4916: void ndl_print(UINT *dl)
4917: {
4918: int n;
4919: int i,j,l,ord_l,s,s0;
4920: struct order_pair *op;
4921:
4922: n = nd_nvar;
4923: printf("<<");
4924: if ( nd_blockmask ) {
4925: l = nd_blockmask->n;
4926: op = nd_blockmask->order_pair;
4927: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4928: ord_l = op[j].length;
4929: for ( i = 0; i < ord_l; i++, s++ )
4930: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4931: }
4932: } else {
4933: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4934: }
4935: printf(">>");
4936: if ( nd_module && MPOS(dl) )
4937: printf("*e%d",MPOS(dl));
4938: }
4939:
4940: void nd_print(ND p)
4941: {
4942: NM m;
4943:
4944: if ( !p )
4945: printf("0\n");
4946: else {
4947: for ( m = BDY(p); m; m = NEXT(m) ) {
4948: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 4949: else printf("+%ld*",CM(m));
1.1 noro 4950: ndl_print(DL(m));
4951: }
4952: printf("\n");
4953: }
4954: }
4955:
4956: void nd_print_q(ND p)
4957: {
4958: NM m;
4959:
4960: if ( !p )
4961: printf("0\n");
4962: else {
4963: for ( m = BDY(p); m; m = NEXT(m) ) {
4964: printf("+");
1.6 noro 4965: printexpr(CO,(Obj)CZ(m));
1.1 noro 4966: printf("*");
4967: ndl_print(DL(m));
4968: }
4969: printf("\n");
4970: }
4971: }
4972:
4973: void ndp_print(ND_pairs d)
4974: {
4975: ND_pairs t;
4976:
4977: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4978: printf("\n");
4979: }
4980:
4981: void nd_removecont(int mod,ND p)
4982: {
4983: int i,n;
4984: Z *w;
4985: NM m;
4986: struct oVECT v;
4987:
4988: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4989: else if ( mod == -2 ) {
4990: Z inv;
4991: divlf(ONE,HCZ(p),&inv);
4992: nd_mul_c_lf(p,inv);
4993: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4994: else {
4995: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4996: w = (Z *)MALLOC(n*sizeof(Q));
4997: v.len = n;
4998: v.body = (pointer *)w;
1.6 noro 4999: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5000: removecont_array((P *)w,n,1);
1.6 noro 5001: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5002: }
5003: }
5004:
5005: void nd_removecont2(ND p1,ND p2)
5006: {
5007: int i,n1,n2,n;
5008: Z *w;
5009: NM m;
5010: struct oVECT v;
5011:
5012: n1 = nd_length(p1);
5013: n2 = nd_length(p2);
5014: n = n1+n2;
5015: w = (Z *)MALLOC(n*sizeof(Q));
5016: v.len = n;
5017: v.body = (pointer *)w;
5018: i = 0;
5019: if ( p1 )
1.6 noro 5020: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5021: if ( p2 )
1.6 noro 5022: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 5023: removecont_array((P *)w,n,1);
5024: i = 0;
5025: if ( p1 )
1.6 noro 5026: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5027: if ( p2 )
1.6 noro 5028: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 5029: }
5030:
5031: void ndv_removecont(int mod,NDV p)
5032: {
5033: int i,len,all_p;
5034: Z *c;
5035: P *w;
5036: Z dvr,t;
5037: P g,cont,tp;
5038: NMV m;
5039:
5040: if ( mod == -1 )
5041: ndv_mul_c(mod,p,_invsf(HCM(p)));
5042: else if ( mod == -2 ) {
5043: Z inv;
5044: divlf(ONE,HCZ(p),&inv);
5045: ndv_mul_c_lf(p,inv);
5046: } else if ( mod )
5047: ndv_mul_c(mod,p,invm(HCM(p),mod));
5048: else {
5049: len = p->len;
5050: w = (P *)MALLOC(len*sizeof(P));
5051: c = (Z *)MALLOC(len*sizeof(Q));
5052: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
5053: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
5054: all_p = all_p && !NUM(w[i]);
5055: }
5056: if ( all_p ) {
5057: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
5058: mulp(nd_vc,(P)dvr,g,&cont);
5059: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5060: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
5061: }
5062: } else {
5063: sortbynm((Q *)c,len);
5064: qltozl((Q *)c,len,&dvr);
5065: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
5066: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
5067: }
5068: }
5069: }
5070: }
5071:
5072: /* koko */
5073:
5074: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
5075: {
5076: int len,i,max;
5077: NMV m,mr0,mr,t;
5078:
5079: len = p->len;
1.14 noro 5080: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 5081: max = MAX(max,TD(DL(m)));
5082: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5083: m = (NMV)((char *)mr0+(len-1)*oadv);
5084: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5085: t = (NMV)MALLOC(nmv_adv);
5086: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5087: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 5088: CZ(mr) = CZ(m);
1.1 noro 5089: ndl_copy(DL(t),DL(mr));
5090: }
5091: NV(p)++;
5092: BDY(p) = mr0;
5093: }
5094:
5095: void ndv_dehomogenize(NDV p,struct order_spec *ord)
5096: {
5097: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
5098: int pos;
5099: Q *w;
5100: Q dvr,t;
5101: NMV m,r;
5102:
5103: len = p->len;
5104: newnvar = nd_nvar-1;
5105: newexporigin = nd_get_exporigin(ord);
5106: if ( nd_module ) newmpos = newexporigin-1;
5107: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
5108: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
5109: ndl_dehomogenize(DL(m));
5110: if ( newwpd != nd_wpd ) {
5111: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
5112: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 5113: CZ(r) = CZ(m);
1.1 noro 5114: if ( nd_module ) pos = MPOS(DL(m));
5115: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
5116: adj = nd_exporigin-newexporigin;
5117: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
5118: if ( nd_module ) {
5119: DL(r)[newmpos] = pos;
5120: }
5121: }
5122: }
5123: NV(p)--;
5124: }
5125:
5126: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
5127: {
5128: int i;
5129: P *tpl,*tpl1;
5130: NODE l;
5131: P h,gcd,t;
5132:
5133: tpl = (P *)MALLOC(m*sizeof(P));
5134: tpl1 = (P *)MALLOC(m*sizeof(P));
5135: bcopy(pl,tpl,m*sizeof(P));
5136: gcd = (P)ONE;
5137: for ( l = nd_hcf; l; l = NEXT(l) ) {
5138: h = (P)BDY(l);
5139: while ( 1 ) {
5140: for ( i = 0; i < m; i++ )
5141: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
5142: break;
5143: if ( i == m ) {
5144: bcopy(tpl1,tpl,m*sizeof(P));
5145: mulp(vl,gcd,h,&t); gcd = t;
5146: } else
5147: break;
5148: }
5149: }
5150: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
5151: if ( full ) {
5152: heu_nezgcdnpz(vl,tpl,m,&t);
5153: mulp(vl,gcd,t,pr);
5154: } else
5155: *pr = gcd;
5156: }
5157:
5158: void removecont_array(P *p,int n,int full)
5159: {
5160: int all_p,all_q,i;
5161: Z *c;
5162: P *w;
5163: P t,s;
5164:
5165: for ( all_q = 1, i = 0; i < n; i++ )
5166: all_q = all_q && NUM(p[i]);
5167: if ( all_q ) {
5168: removecont_array_q((Z *)p,n);
5169: } else {
5170: c = (Z *)MALLOC(n*sizeof(Z));
5171: w = (P *)MALLOC(n*sizeof(P));
5172: for ( i = 0; i < n; i++ ) {
5173: ptozp(p[i],1,(Q *)&c[i],&w[i]);
5174: }
5175: removecont_array_q(c,n);
5176: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
5177: for ( i = 0; i < n; i++ ) {
5178: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
5179: }
5180: }
5181: }
5182:
5183: /* c is an int array */
5184:
5185: void removecont_array_q(Z *c,int n)
5186: {
5187: struct oVECT v;
5188: Z d0,d1,a,u,u1,gcd;
5189: int i,j;
5190: Z *q,*r;
5191:
5192: q = (Z *)MALLOC(n*sizeof(Z));
5193: r = (Z *)MALLOC(n*sizeof(Z));
5194: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
5195: gcdvz_estimate(&v,&d0);
5196: for ( i = 0; i < n; i++ ) {
5197: divqrz(c[i],d0,&q[i],&r[i]);
5198: }
5199: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
5200: if ( i < n ) {
5201: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
5202: gcdvz(&v,&d1);
5203: gcdz(d0,d1,&gcd);
1.6 noro 5204: /* exact division */
5205: divsz(d0,gcd,&a);
1.1 noro 5206: for ( i = 0; i < n; i++ ) {
5207: mulz(a,q[i],&u);
5208: if ( r[i] ) {
1.6 noro 5209: /* exact division */
5210: divsz(r[i],gcd,&u1);
1.1 noro 5211: addz(u,u1,&q[i]);
5212: } else
5213: q[i] = u;
5214: }
5215: }
5216: for ( i = 0; i < n; i++ ) c[i] = q[i];
5217: }
5218:
1.4 noro 5219: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
5220:
5221: void mpz_removecont_array(mpz_t *c,int n)
5222: {
5223: mpz_t d0,a,u,u1,gcd;
5224: int i,j;
1.13 noro 5225: static mpz_t *q,*r;
5226: static int c_len = 0;
1.4 noro 5227:
5228: for ( i = 0; i < n; i++ )
5229: if ( mpz_sgn(c[i]) ) break;
5230: if ( i == n ) return;
5231: gcdv_mpz_estimate(d0,c,n);
1.13 noro 5232: if ( n > c_len ) {
5233: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5234: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
5235: c_len = n;
5236: }
1.4 noro 5237: for ( i = 0; i < n; i++ ) {
5238: mpz_init(q[i]); mpz_init(r[i]);
5239: mpz_fdiv_qr(q[i],r[i],c[i],d0);
5240: }
5241: for ( i = 0; i < n; i++ )
5242: if ( mpz_sgn(r[i]) ) break;
5243: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
5244: if ( i < n ) {
5245: mpz_gcd(gcd,d0,r[i]);
5246: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
5247: mpz_div(a,d0,gcd);
5248: for ( i = 0; i < n; i++ ) {
5249: mpz_mul(u,a,q[i]);
5250: if ( mpz_sgn(r[i]) ) {
5251: mpz_div(u1,r[i],gcd);
5252: mpz_add(q[i],u,u1);
5253: } else
5254: mpz_set(q[i],u);
5255: }
5256: }
5257: for ( i = 0; i < n; i++ )
5258: mpz_set(c[i],q[i]);
5259: }
5260:
1.1 noro 5261: void nd_mul_c(int mod,ND p,int mul)
5262: {
5263: NM m;
5264: int c,c1;
5265:
5266: if ( !p ) return;
5267: if ( mul == 1 ) return;
5268: if ( mod == -1 )
5269: for ( m = BDY(p); m; m = NEXT(m) )
5270: CM(m) = _mulsf(CM(m),mul);
5271: else
5272: for ( m = BDY(p); m; m = NEXT(m) ) {
5273: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5274: }
5275: }
5276:
5277: void nd_mul_c_lf(ND p,Z mul)
5278: {
5279: NM m;
5280: Z c;
5281:
5282: if ( !p ) return;
5283: if ( UNIZ(mul) ) return;
5284: for ( m = BDY(p); m; m = NEXT(m) ) {
5285: mullf(CZ(m),mul,&c); CZ(m) = c;
5286: }
5287: }
5288:
5289: void nd_mul_c_q(ND p,P mul)
5290: {
5291: NM m;
5292: P c;
5293:
5294: if ( !p ) return;
5295: if ( UNIQ(mul) ) return;
5296: for ( m = BDY(p); m; m = NEXT(m) ) {
5297: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
5298: }
5299: }
5300:
5301: void nd_mul_c_p(VL vl,ND p,P mul)
5302: {
5303: NM m;
5304: P c;
5305:
5306: if ( !p ) return;
5307: for ( m = BDY(p); m; m = NEXT(m) ) {
5308: mulp(vl,CP(m),mul,&c); CP(m) = c;
5309: }
5310: }
5311:
5312: void nd_free(ND p)
5313: {
5314: NM t,s;
5315:
5316: if ( !p ) return;
5317: t = BDY(p);
5318: while ( t ) {
5319: s = NEXT(t);
5320: FREENM(t);
5321: t = s;
5322: }
5323: FREEND(p);
5324: }
5325:
5326: void ndv_free(NDV p)
5327: {
5328: GCFREE(BDY(p));
5329: }
5330:
5331: void nd_append_red(UINT *d,int i)
5332: {
5333: RHist m,m0;
5334: int h;
5335:
5336: NEWRHist(m);
5337: h = ndl_hash_value(d);
5338: m->index = i;
5339: ndl_copy(d,DL(m));
5340: NEXT(m) = nd_red[h];
5341: nd_red[h] = m;
5342: }
5343:
5344: UINT *ndv_compute_bound(NDV p)
5345: {
5346: UINT *d1,*d2,*t;
5347: UINT u;
5348: int i,j,k,l,len,ind;
5349: NMV m;
5350:
5351: if ( !p )
5352: return 0;
5353: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5354: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5355: len = LEN(p);
5356: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
5357: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
5358: ndl_max(DL(m),d1,d2);
5359: t = d1; d1 = d2; d2 = t;
5360: }
5361: l = nd_nvar+31;
5362: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5363: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5364: u = d1[i];
5365: k = (nd_epw-1)*nd_bpe;
5366: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5367: t[ind] = (u>>k)&nd_mask0;
5368: }
5369: for ( ; ind < l; ind++ ) t[ind] = 0;
5370: return t;
5371: }
5372:
5373: UINT *nd_compute_bound(ND p)
5374: {
5375: UINT *d1,*d2,*t;
5376: UINT u;
5377: int i,j,k,l,len,ind;
5378: NM m;
5379:
5380: if ( !p )
5381: return 0;
5382: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5383: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5384: len = LEN(p);
5385: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
5386: for ( m = NEXT(m); m; m = NEXT(m) ) {
5387: ndl_lcm(DL(m),d1,d2);
5388: t = d1; d1 = d2; d2 = t;
5389: }
5390: l = nd_nvar+31;
5391: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
5392: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
5393: u = d1[i];
5394: k = (nd_epw-1)*nd_bpe;
5395: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
5396: t[ind] = (u>>k)&nd_mask0;
5397: }
5398: for ( ; ind < l; ind++ ) t[ind] = 0;
5399: return t;
5400: }
5401:
5402: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
5403: /* of a term. In this case we need additional 1 word. */
5404:
5405: int nd_get_exporigin(struct order_spec *ord)
5406: {
5407: switch ( ord->id ) {
1.21 noro 5408: case 0: case 2: case 256: case 258: case 300:
1.1 noro 5409: return 1+nd_module;
5410: case 1: case 257:
5411: /* block order */
5412: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
5413: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
5414: return ord->ord.block.length+1+nd_module;
5415: case 3: case 259:
5416: #if 0
5417: error("nd_get_exporigin : composite order is not supported yet.");
5418: #else
5419: return 1+nd_module;
5420: #endif
5421: default:
5422: error("nd_get_exporigin : ivalid argument.");
5423: return 0;
5424: }
5425: }
5426:
5427: void nd_setup_parameters(int nvar,int max) {
5428: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
5429: struct order_pair *op;
5430:
5431: nd_nvar = nvar;
5432: if ( max ) {
5433: /* XXX */
5434: if ( do_weyl ) nd_bpe = 32;
5435: else if ( max < 2 ) nd_bpe = 1;
5436: else if ( max < 4 ) nd_bpe = 2;
5437: else if ( max < 8 ) nd_bpe = 3;
5438: else if ( max < 16 ) nd_bpe = 4;
5439: else if ( max < 32 ) nd_bpe = 5;
5440: else if ( max < 64 ) nd_bpe = 6;
5441: else if ( max < 256 ) nd_bpe = 8;
5442: else if ( max < 1024 ) nd_bpe = 10;
5443: else if ( max < 65536 ) nd_bpe = 16;
5444: else nd_bpe = 32;
5445: }
5446: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
5447: UINT t;
5448: int st;
5449: int *v;
5450: /* t = max(weights) */
5451: t = 0;
5452: if ( current_dl_weight_vector )
5453: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
5454: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
5455: if ( t < st ) t = st;
5456: }
5457: if ( nd_matrix )
5458: for ( i = 0; i < nd_matrix_len; i++ )
5459: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
5460: if ( (st=v[j]) < 0 ) st = -st;
5461: if ( t < st ) t = st;
5462: }
5463: /* i = bitsize of t */
5464: for ( i = 0; t; t >>=1, i++ );
5465: /* i += bitsize of nd_nvar */
5466: for ( t = nd_nvar; t; t >>=1, i++);
5467: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
5468: if ( (nd_bpe+i) >= 31 )
5469: error("nd_setup_parameters : too large weight");
5470: }
5471: nd_epw = (sizeof(UINT)*8)/nd_bpe;
5472: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
5473: nd_exporigin = nd_get_exporigin(nd_ord);
5474: wpd = nd_exporigin+elen;
5475: if ( nd_module )
5476: nd_mpos = nd_exporigin-1;
5477: else
5478: nd_mpos = -1;
5479: if ( wpd != nd_wpd ) {
5480: nd_free_private_storage();
5481: nd_wpd = wpd;
5482: }
5483: if ( nd_bpe < 32 ) {
5484: nd_mask0 = (1<<nd_bpe)-1;
5485: } else {
5486: nd_mask0 = 0xffffffff;
5487: }
5488: bzero(nd_mask,sizeof(nd_mask));
5489: nd_mask1 = 0;
5490: for ( i = 0; i < nd_epw; i++ ) {
5491: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
5492: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
5493: }
5494: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
5495: nd_epos = nd_create_epos(nd_ord);
5496: nd_blockmask = nd_create_blockmask(nd_ord);
5497: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
5498: }
5499:
5500: ND_pairs nd_reconstruct(int trace,ND_pairs d)
5501: {
5502: int i,obpe,oadv,h;
5503: static NM prev_nm_free_list;
5504: static ND_pairs prev_ndp_free_list;
5505: RHist mr0,mr;
5506: RHist r;
5507: RHist *old_red;
5508: ND_pairs s0,s,t;
5509: EPOS oepos;
5510:
5511: obpe = nd_bpe;
5512: oadv = nmv_adv;
5513: oepos = nd_epos;
5514: if ( obpe < 2 ) nd_bpe = 2;
5515: else if ( obpe < 3 ) nd_bpe = 3;
5516: else if ( obpe < 4 ) nd_bpe = 4;
5517: else if ( obpe < 5 ) nd_bpe = 5;
5518: else if ( obpe < 6 ) nd_bpe = 6;
5519: else if ( obpe < 8 ) nd_bpe = 8;
5520: else if ( obpe < 10 ) nd_bpe = 10;
5521: else if ( obpe < 16 ) nd_bpe = 16;
5522: else if ( obpe < 32 ) nd_bpe = 32;
5523: else error("nd_reconstruct : exponent too large");
5524:
5525: nd_setup_parameters(nd_nvar,0);
5526: prev_nm_free_list = _nm_free_list;
5527: prev_ndp_free_list = _ndp_free_list;
5528: _nm_free_list = 0;
5529: _ndp_free_list = 0;
5530: for ( i = nd_psn-1; i >= 0; i-- ) {
5531: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
5532: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
5533: }
5534: if ( trace )
5535: for ( i = nd_psn-1; i >= 0; i-- ) {
5536: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
5537: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
5538: }
5539: s0 = 0;
5540: for ( t = d; t; t = NEXT(t) ) {
5541: NEXTND_pairs(s0,s);
5542: s->i1 = t->i1;
5543: s->i2 = t->i2;
1.24 noro 5544: s->sig = t->sig;
1.1 noro 5545: SG(s) = SG(t);
5546: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
5547: }
5548:
5549: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
5550: for ( i = 0; i < REDTAB_LEN; i++ ) {
5551: old_red[i] = nd_red[i];
5552: nd_red[i] = 0;
5553: }
5554: for ( i = 0; i < REDTAB_LEN; i++ )
5555: for ( r = old_red[i]; r; r = NEXT(r) ) {
5556: NEWRHist(mr);
5557: mr->index = r->index;
5558: SG(mr) = SG(r);
5559: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
5560: h = ndl_hash_value(DL(mr));
5561: NEXT(mr) = nd_red[h];
5562: nd_red[h] = mr;
1.24 noro 5563: mr->sig = r->sig;
1.1 noro 5564: }
5565: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
5566: old_red = 0;
5567: for ( i = 0; i < nd_psn; i++ ) {
5568: NEWRHist(r); SG(r) = SG(nd_psh[i]);
5569: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24 noro 5570: r->sig = nd_psh[i]->sig;
1.1 noro 5571: nd_psh[i] = r;
5572: }
5573: if ( s0 ) NEXT(s) = 0;
5574: prev_nm_free_list = 0;
5575: prev_ndp_free_list = 0;
5576: #if 0
5577: GC_gcollect();
5578: #endif
5579: return s0;
5580: }
5581:
1.30 ! noro 5582: void nd_reconstruct_s(int trace,ND_pairs *d)
! 5583: {
! 5584: int i,obpe,oadv,h;
! 5585: static NM prev_nm_free_list;
! 5586: static ND_pairs prev_ndp_free_list;
! 5587: RHist mr0,mr;
! 5588: RHist r;
! 5589: RHist *old_red;
! 5590: ND_pairs s0,s,t;
! 5591: EPOS oepos;
! 5592:
! 5593: obpe = nd_bpe;
! 5594: oadv = nmv_adv;
! 5595: oepos = nd_epos;
! 5596: if ( obpe < 2 ) nd_bpe = 2;
! 5597: else if ( obpe < 3 ) nd_bpe = 3;
! 5598: else if ( obpe < 4 ) nd_bpe = 4;
! 5599: else if ( obpe < 5 ) nd_bpe = 5;
! 5600: else if ( obpe < 6 ) nd_bpe = 6;
! 5601: else if ( obpe < 8 ) nd_bpe = 8;
! 5602: else if ( obpe < 10 ) nd_bpe = 10;
! 5603: else if ( obpe < 16 ) nd_bpe = 16;
! 5604: else if ( obpe < 32 ) nd_bpe = 32;
! 5605: else error("nd_reconstruct_s : exponent too large");
! 5606:
! 5607: nd_setup_parameters(nd_nvar,0);
! 5608: prev_nm_free_list = _nm_free_list;
! 5609: prev_ndp_free_list = _ndp_free_list;
! 5610: _nm_free_list = 0;
! 5611: _ndp_free_list = 0;
! 5612: for ( i = nd_psn-1; i >= 0; i-- ) {
! 5613: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
! 5614: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
! 5615: }
! 5616: if ( trace )
! 5617: for ( i = nd_psn-1; i >= 0; i-- ) {
! 5618: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
! 5619: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
! 5620: }
! 5621:
! 5622: for ( i = 0; i < nd_nbase; i++ ) {
! 5623: s0 = 0;
! 5624: for ( t = d[i]; t; t = NEXT(t) ) {
! 5625: NEXTND_pairs(s0,s);
! 5626: s->i1 = t->i1;
! 5627: s->i2 = t->i2;
! 5628: s->sig = t->sig;
! 5629: SG(s) = SG(t);
! 5630: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
! 5631: }
! 5632: d[i] = s0;
! 5633: }
! 5634:
! 5635: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
! 5636: for ( i = 0; i < REDTAB_LEN; i++ ) {
! 5637: old_red[i] = nd_red[i];
! 5638: nd_red[i] = 0;
! 5639: }
! 5640: for ( i = 0; i < REDTAB_LEN; i++ )
! 5641: for ( r = old_red[i]; r; r = NEXT(r) ) {
! 5642: NEWRHist(mr);
! 5643: mr->index = r->index;
! 5644: SG(mr) = SG(r);
! 5645: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
! 5646: h = ndl_hash_value(DL(mr));
! 5647: NEXT(mr) = nd_red[h];
! 5648: nd_red[h] = mr;
! 5649: mr->sig = r->sig;
! 5650: }
! 5651: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
! 5652: old_red = 0;
! 5653: for ( i = 0; i < nd_psn; i++ ) {
! 5654: NEWRHist(r); SG(r) = SG(nd_psh[i]);
! 5655: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
! 5656: r->sig = nd_psh[i]->sig;
! 5657: nd_psh[i] = r;
! 5658: }
! 5659: if ( s0 ) NEXT(s) = 0;
! 5660: prev_nm_free_list = 0;
! 5661: prev_ndp_free_list = 0;
! 5662: #if 0
! 5663: GC_gcollect();
! 5664: #endif
! 5665: }
! 5666:
1.1 noro 5667: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
5668: {
5669: int n,i,ei,oepw,omask0,j,s,ord_l,l;
5670: struct order_pair *op;
5671:
5672: n = nd_nvar;
5673: oepw = (sizeof(UINT)*8)/obpe;
5674: omask0 = (1<<obpe)-1;
5675: TD(r) = TD(d);
5676: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
5677: if ( nd_blockmask ) {
5678: l = nd_blockmask->n;
5679: op = nd_blockmask->order_pair;
5680: for ( i = 1; i < nd_exporigin; i++ )
5681: r[i] = d[i];
5682: for ( j = 0, s = 0; j < l; j++ ) {
5683: ord_l = op[j].length;
5684: for ( i = 0; i < ord_l; i++, s++ ) {
5685: ei = GET_EXP_OLD(d,s);
5686: PUT_EXP(r,s,ei);
5687: }
5688: }
5689: } else {
5690: for ( i = 0; i < n; i++ ) {
5691: ei = GET_EXP_OLD(d,i);
5692: PUT_EXP(r,i,ei);
5693: }
5694: }
5695: if ( nd_module ) MPOS(r) = MPOS(d);
5696: }
5697:
5698: ND nd_copy(ND p)
5699: {
5700: NM m,mr,mr0;
5701: int c,n;
5702: ND r;
5703:
5704: if ( !p )
5705: return 0;
5706: else {
5707: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
5708: NEXTNM(mr0,mr);
5709: CM(mr) = CM(m);
5710: ndl_copy(DL(m),DL(mr));
5711: }
5712: NEXT(mr) = 0;
5713: MKND(NV(p),mr0,LEN(p),r);
5714: SG(r) = SG(p);
5715: return r;
5716: }
5717: }
5718:
5719: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
5720: {
5721: NM m1,m2;
5722: NDV p1,p2;
5723: ND t1,t2;
5724: UINT *lcm;
5725: P gp,tp;
5726: Z g,t;
5727: Z iq;
5728: int td;
5729: LIST hist;
5730: NODE node;
5731: DP d;
5732:
5733: if ( !mod && nd_demand ) {
5734: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
5735: } else {
5736: if ( trace ) {
5737: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
5738: } else {
5739: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
5740: }
5741: }
5742: lcm = LCM(p);
5743: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
5744: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
5745: FREENM(m1); return 0;
5746: }
5747: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
5748: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
5749: FREENM(m1); FREENM(m2); return 0;
5750: }
5751:
5752: if ( mod == -1 ) {
5753: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
5754: } else if ( mod > 0 ) {
5755: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
5756: } else if ( mod == -2 ) {
5757: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
5758: } else if ( nd_vc ) {
5759: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
5760: divsp(nd_vc,HCP(p2),gp,&CP(m1));
5761: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
5762: } else {
1.6 noro 5763: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 5764: }
5765: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
5766: *rp = nd_add(mod,t1,t2);
5767: if ( nd_gentrace ) {
5768: /* nd_tracelist is initialized */
1.6 noro 5769: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5770: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 5771: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 5772: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
5773: nd_tracelist = node;
5774: }
1.24 noro 5775: if ( *rp )
5776: (*rp)->sig = p->sig;
1.1 noro 5777: FREENM(m1); FREENM(m2);
5778: return 1;
5779: }
5780:
5781: void ndv_mul_c(int mod,NDV p,int mul)
5782: {
5783: NMV m;
5784: int c,c1,len,i;
5785:
5786: if ( !p ) return;
5787: len = LEN(p);
5788: if ( mod == -1 )
5789: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
5790: CM(m) = _mulsf(CM(m),mul);
5791: else
5792: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5793: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
5794: }
5795: }
5796:
5797: void ndv_mul_c_lf(NDV p,Z mul)
5798: {
5799: NMV m;
5800: Z c;
5801: int len,i;
5802:
5803: if ( !p ) return;
5804: len = LEN(p);
5805: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5806: mullf(CZ(m),mul,&c); CZ(m) = c;
5807: }
5808: }
5809:
5810: /* for nd_det */
5811: void ndv_mul_c_q(NDV p,Z mul)
5812: {
5813: NMV m;
5814: Z c;
5815: int len,i;
5816:
5817: if ( !p ) return;
5818: len = LEN(p);
5819: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 5820: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 5821: }
5822: }
5823:
5824: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
5825: int n2,i,j,l,n,tlen;
5826: UINT *d0;
5827: NM *tab,*psum;
5828: ND s,r;
5829: NM t;
5830: NMV m1;
5831:
5832: if ( !p ) return 0;
5833: n = NV(p); n2 = n>>1;
5834: d0 = DL(m0);
5835: l = LEN(p);
5836: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
5837: tab = (NM *)MALLOC(tlen*sizeof(NM));
5838: psum = (NM *)MALLOC(tlen*sizeof(NM));
5839: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
5840: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
5841: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
5842: /* m0(NM) * m1(NMV) => tab(NM) */
5843: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
5844: for ( j = 0; j < tlen; j++ ) {
5845: if ( tab[j] ) {
5846: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
5847: }
5848: }
5849: }
5850: for ( i = tlen-1, r = 0; i >= 0; i-- )
5851: if ( psum[i] ) {
5852: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
5853: MKND(n,psum[i],j,s);
5854: r = nd_add(mod,r,s);
5855: }
5856: if ( r ) SG(r) = SG(p)+TD(d0);
5857: return r;
5858: }
5859:
5860: /* product of monomials */
5861: /* XXX block order is not handled correctly */
5862:
5863: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
5864: {
5865: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
5866: UINT *d0,*d1,*d,*dt,*ctab;
5867: Z *ctab_q;
5868: Z q,q1;
5869: UINT c0,c1,c;
5870: NM *p;
5871: NM m,t;
5872: int mpos;
5873:
5874: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
5875: if ( !m0 || !m1 ) return;
5876: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
5877: if ( nd_module )
5878: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
5879:
5880: NEWNM(m); d = DL(m);
5881: if ( mod ) {
5882: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
5883: } else if ( nd_vc )
5884: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
5885: else
1.6 noro 5886: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 5887: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
5888: homo = n&1 ? 1 : 0;
5889: if ( homo ) {
5890: /* offset of h-degree */
5891: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
5892: PUT_EXP(DL(m),n-1,h);
5893: TD(DL(m)) = h;
5894: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5895: }
5896: tab[0] = m;
5897: NEWNM(m); d = DL(m);
5898: for ( i = 0, curlen = 1; i < n2; i++ ) {
5899: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
5900: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
5901: /* xi^a*(Di^k*xi^l)*Di^b */
5902: a += l; b += k;
5903: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
5904: if ( !k || !l ) {
5905: for ( j = 0; j < curlen; j++ )
5906: if ( (t = tab[j]) != 0 ) {
5907: dt = DL(t);
5908: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
5909: if ( nd_blockmask ) ndl_weight_mask(dt);
5910: }
5911: curlen *= k+1;
5912: continue;
5913: }
5914: min = MIN(k,l);
5915: if ( mod ) {
5916: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
5917: mkwcm(k,l,mod,(int *)ctab);
5918: } else {
5919: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
5920: mkwc(k,l,ctab_q);
5921: }
5922: for ( j = min; j >= 0; j-- ) {
5923: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
5924: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
5925: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
5926: if ( homo ) {
5927: TD(d) = s;
5928: PUT_EXP(d,n-1,s-h);
5929: } else TD(d) = h;
5930: if ( nd_blockmask ) ndl_weight_mask(d);
5931: if ( mod ) c = ctab[j];
5932: else q = ctab_q[j];
5933: p = tab+curlen*j;
5934: if ( j == 0 ) {
5935: for ( u = 0; u < curlen; u++, p++ ) {
5936: if ( tab[u] ) {
5937: ndl_addto(DL(tab[u]),d);
5938: if ( mod ) {
5939: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
5940: } else if ( nd_vc )
5941: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
5942: else {
1.6 noro 5943: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 5944: }
5945: }
5946: }
5947: } else {
5948: for ( u = 0; u < curlen; u++, p++ ) {
5949: if ( tab[u] ) {
5950: NEWNM(t);
5951: ndl_add(DL(tab[u]),d,DL(t));
5952: if ( mod ) {
5953: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
5954: } else if ( nd_vc )
5955: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
5956: else
1.6 noro 5957: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 5958: *p = t;
5959: }
5960: }
5961: }
5962: }
5963: curlen *= k+1;
5964: }
5965: FREENM(m);
5966: if ( nd_module ) {
5967: mpos = MPOS(d1);
5968: for ( i = 0; i < tlen; i++ )
5969: if ( tab[i] ) {
5970: d = DL(tab[i]);
5971: MPOS(d) = mpos;
5972: TD(d) = ndl_weight(d);
5973: }
5974: }
5975: }
5976:
5977: ND ndv_mul_nm_symbolic(NM m0,NDV p)
5978: {
5979: NM mr,mr0;
5980: NMV m;
5981: UINT *d,*dt,*dm;
5982: int c,n,td,i,c1,c2,len;
5983: Q q;
5984: ND r;
5985:
5986: if ( !p ) return 0;
5987: else {
5988: n = NV(p); m = BDY(p);
5989: d = DL(m0);
5990: len = LEN(p);
5991: mr0 = 0;
5992: td = TD(d);
5993: c = CM(m0);
5994: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5995: NEXTNM(mr0,mr);
5996: CM(mr) = 1;
5997: ndl_add(DL(m),d,DL(mr));
5998: }
5999: NEXT(mr) = 0;
6000: MKND(NV(p),mr0,len,r);
6001: SG(r) = SG(p) + TD(d);
6002: return r;
6003: }
6004: }
6005:
6006: ND ndv_mul_nm(int mod,NM m0,NDV p)
6007: {
6008: NM mr,mr0;
6009: NMV m;
6010: UINT *d,*dt,*dm;
6011: int c,n,td,i,c1,c2,len;
6012: P q;
6013: ND r;
6014:
6015: if ( !p ) return 0;
6016: else if ( do_weyl ) {
6017: if ( mod < 0 ) {
6018: error("ndv_mul_nm : not implemented (weyl)");
6019: return 0;
6020: } else
6021: return weyl_ndv_mul_nm(mod,m0,p);
6022: } else {
6023: n = NV(p); m = BDY(p);
6024: d = DL(m0);
6025: len = LEN(p);
6026: mr0 = 0;
6027: td = TD(d);
6028: if ( mod == -1 ) {
6029: c = CM(m0);
6030: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6031: NEXTNM(mr0,mr);
6032: CM(mr) = _mulsf(CM(m),c);
6033: ndl_add(DL(m),d,DL(mr));
6034: }
6035: } else if ( mod == -2 ) {
6036: Z cl;
6037: cl = CZ(m0);
6038: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6039: NEXTNM(mr0,mr);
6040: mullf(CZ(m),cl,&CZ(mr));
6041: ndl_add(DL(m),d,DL(mr));
6042: }
6043: } else if ( mod ) {
6044: c = CM(m0);
6045: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6046: NEXTNM(mr0,mr);
6047: c1 = CM(m);
6048: DMAR(c1,c,0,mod,c2);
6049: CM(mr) = c2;
6050: ndl_add(DL(m),d,DL(mr));
6051: }
6052: } else {
6053: q = CP(m0);
6054: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6055: NEXTNM(mr0,mr);
6056: mulp(nd_vc,CP(m),q,&CP(mr));
6057: ndl_add(DL(m),d,DL(mr));
6058: }
6059: }
6060: NEXT(mr) = 0;
6061: MKND(NV(p),mr0,len,r);
6062: SG(r) = SG(p) + TD(d);
6063: return r;
6064: }
6065: }
6066:
6067: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
6068: {
6069: NM mq0,mq;
6070: NMV tm;
6071: Q q;
6072: int i,nv,sg,c,c1,c2,hindex;
6073: ND p,t,r;
6074:
6075: if ( bucket->m < 0 ) return 0;
6076: else {
6077: nv = NV(d);
6078: mq0 = 0;
6079: tm = (NMV)MALLOC(nmv_adv);
6080: while ( 1 ) {
6081: if ( mod > 0 || mod == -1 )
6082: hindex = head_pbucket(mod,bucket);
6083: else if ( mod == -2 )
6084: hindex = head_pbucket_lf(bucket);
6085: else
6086: hindex = head_pbucket_q(bucket);
6087: if ( hindex < 0 ) break;
6088: p = bucket->body[hindex];
6089: NEXTNM(mq0,mq);
6090: ndl_sub(HDL(p),HDL(d),DL(mq));
6091: ndl_copy(DL(mq),DL(tm));
6092: if ( mod ) {
6093: c1 = invm(HCM(d),mod); c2 = HCM(p);
6094: DMAR(c1,c2,0,mod,c); CM(mq) = c;
6095: CM(tm) = mod-c;
6096: } else {
1.6 noro 6097: divsz(HCZ(p),HCZ(d),&CZ(mq));
6098: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 6099: }
6100: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
6101: bucket->body[hindex] = nd_remove_head(p);
6102: t = nd_remove_head(t);
6103: add_pbucket(mod,bucket,t);
6104: }
6105: if ( !mq0 )
6106: r = 0;
6107: else {
6108: NEXT(mq) = 0;
6109: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
6110: MKND(nv,mq0,i,r);
6111: /* XXX */
6112: SG(r) = HTD(r);
6113: }
6114: return r;
6115: }
6116: }
6117:
6118: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6119: {
6120: NMV m,mr,mr0,t;
6121: int len,i,k;
6122:
6123: if ( !p ) return;
6124: m = BDY(p); len = LEN(p);
6125: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
6126: m = (NMV)((char *)mr0+(len-1)*oadv);
6127: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
6128: t = (NMV)MALLOC(nmv_adv);
6129: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 6130: CZ(t) = CZ(m);
1.1 noro 6131: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
6132: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 6133: CZ(mr) = CZ(t);
1.1 noro 6134: ndl_copy(DL(t),DL(mr));
6135: }
6136: BDY(p) = mr0;
6137: }
6138:
6139: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
6140: {
6141: NMV m,mr,mr0;
6142: int len,i;
6143: NDV r;
6144:
6145: if ( !p ) return 0;
6146: m = BDY(p); len = LEN(p);
6147: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
6148: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
6149: ndl_zero(DL(mr));
6150: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 6151: CZ(mr) = CZ(m);
1.1 noro 6152: }
6153: MKNDV(NV(p),mr0,len,r);
6154: SG(r) = SG(p);
1.24 noro 6155: r->sig = p->sig;
1.1 noro 6156: return r;
6157: }
6158:
6159: /* duplicate p */
6160:
6161: NDV ndv_dup(int mod,NDV p)
6162: {
6163: NDV d;
6164: NMV t,m,m0;
6165: int i,len;
6166:
6167: if ( !p ) return 0;
6168: len = LEN(p);
6169: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6170: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6171: ndl_copy(DL(t),DL(m));
1.6 noro 6172: CZ(m) = CZ(t);
1.1 noro 6173: }
6174: MKNDV(NV(p),m0,len,d);
6175: SG(d) = SG(p);
6176: return d;
6177: }
6178:
6179: NDV ndv_symbolic(int mod,NDV p)
6180: {
6181: NDV d;
6182: NMV t,m,m0;
6183: int i,len;
6184:
6185: if ( !p ) return 0;
6186: len = LEN(p);
6187: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
6188: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
6189: ndl_copy(DL(t),DL(m));
1.6 noro 6190: CZ(m) = ONE;
1.1 noro 6191: }
6192: MKNDV(NV(p),m0,len,d);
6193: SG(d) = SG(p);
6194: return d;
6195: }
6196:
6197: ND nd_dup(ND p)
6198: {
6199: ND d;
6200: NM t,m,m0;
6201:
6202: if ( !p ) return 0;
6203: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
6204: NEXTNM(m0,m);
6205: ndl_copy(DL(t),DL(m));
1.6 noro 6206: CZ(m) = CZ(t);
1.1 noro 6207: }
6208: if ( m0 ) NEXT(m) = 0;
6209: MKND(NV(p),m0,LEN(p),d);
6210: SG(d) = SG(p);
6211: return d;
6212: }
6213:
6214: /* XXX if p->len == 0 then it represents 0 */
6215:
6216: void ndv_mod(int mod,NDV p)
6217: {
6218: NMV t,d;
6219: int r,s,u;
6220: int i,len,dlen;
6221: P cp;
6222: Z c;
6223: Obj gfs;
6224:
6225: if ( !p ) return;
6226: len = LEN(p);
6227: dlen = 0;
6228: if ( mod == -1 )
6229: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6230: simp_ff((Obj)CP(t),&gfs);
6231: if ( gfs ) {
6232: r = FTOIF(CONT((GFS)gfs));
6233: CM(d) = r;
6234: ndl_copy(DL(t),DL(d));
6235: NMV_ADV(d);
6236: dlen++;
6237: }
6238: }
6239: else if ( mod == -2 )
6240: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6241: simp_ff((Obj)CP(t),&gfs);
6242: if ( gfs ) {
6243: lmtolf((LM)gfs,&CZ(d));
6244: ndl_copy(DL(t),DL(d));
6245: NMV_ADV(d);
6246: dlen++;
6247: }
6248: }
6249: else
6250: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
6251: if ( nd_vc ) {
6252: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
6253: c = (Z)cp;
6254: } else
1.6 noro 6255: c = CZ(t);
1.1 noro 6256: r = remqi((Q)c,mod);
6257: if ( r ) {
6258: CM(d) = r;
6259: ndl_copy(DL(t),DL(d));
6260: NMV_ADV(d);
6261: dlen++;
6262: }
6263: }
6264: LEN(p) = dlen;
6265: }
6266:
6267: NDV ptondv(VL vl,VL dvl,P p)
6268: {
6269: ND nd;
6270:
6271: nd = ptond(vl,dvl,p);
6272: return ndtondv(0,nd);
6273: }
6274:
6275: void pltozpl(LIST l,Q *cont,LIST *pp)
6276: {
1.16 noro 6277: NODE nd,nd1;
6278: int n;
6279: P *pl;
6280: Q *cl;
6281: int i;
6282: P dmy;
6283: Z dvr,inv;
6284: LIST r;
6285:
6286: nd = BDY(l); n = length(nd);
6287: pl = (P *)MALLOC(n*sizeof(P));
6288: cl = (Q *)MALLOC(n*sizeof(Q));
6289: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
6290: ptozp((P)BDY(nd),1,&cl[i],&dmy);
6291: }
6292: qltozl(cl,n,&dvr);
6293: divz(ONE,dvr,&inv);
6294: nd = BDY(l);
6295: for ( i = 0; i < n; i++, nd = NEXT(nd) )
6296: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
6297: nd = 0;
6298: for ( i = n-1; i >= 0; i-- ) {
6299: MKNODE(nd1,pl[i],nd); nd = nd1;
6300: }
6301: MKLIST(r,nd);
6302: *pp = r;
1.1 noro 6303: }
6304:
6305: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
6306:
6307: NDV pltondv(VL vl,VL dvl,LIST p)
6308: {
6309: int i;
6310: NODE t;
6311: ND r,ri;
6312: NM m;
6313:
6314: if ( !nd_module ) error("pltond : module order must be set");
6315: r = 0;
6316: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
6317: ri = ptond(vl,dvl,(P)BDY(t));
6318: if ( ri )
6319: for ( m = BDY(ri); m; m = NEXT(m) ) {
6320: MPOS(DL(m)) = i;
6321: TD(DL(m)) = ndl_weight(DL(m));
6322: if ( nd_blockmask ) ndl_weight_mask(DL(m));
6323: }
6324: r = nd_add(0,r,ri);
6325: }
6326: return ndtondv(0,r);
6327: }
6328:
6329: ND ptond(VL vl,VL dvl,P p)
6330: {
6331: int n,i,j,k,e;
6332: VL tvl;
6333: V v;
6334: DCP dc;
6335: DCP *w;
6336: ND r,s,t,u;
6337: P x;
6338: int c;
6339: UINT *d;
6340: NM m,m0;
6341:
6342: if ( !p )
6343: return 0;
6344: else if ( NUM(p) ) {
6345: NEWNM(m);
6346: ndl_zero(DL(m));
6347: if ( !INT((Q)p) )
6348: error("ptond : input must be integer-coefficient");
1.6 noro 6349: CZ(m) = (Z)p;
1.1 noro 6350: NEXT(m) = 0;
6351: MKND(nd_nvar,m,1,r);
6352: SG(r) = 0;
6353: return r;
6354: } else {
6355: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
6356: w = (DCP *)MALLOC(k*sizeof(DCP));
6357: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
6358: for ( i = 0, tvl = dvl, v = VR(p);
6359: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
6360: if ( !tvl ) {
6361: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
6362: t = ptond(vl,dvl,COEF(w[j]));
6363: pwrp(vl,x,DEG(w[j]),&p);
6364: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
6365: }
6366: return s;
6367: } else {
6368: NEWNM(m0); d = DL(m0);
6369: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 6370: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 6371: TD(d) = MUL_WEIGHT(e,i);
6372: if ( nd_blockmask) ndl_weight_mask(d);
6373: if ( nd_module ) MPOS(d) = 0;
6374: t = ptond(vl,dvl,COEF(w[j]));
6375: for ( m = BDY(t); m; m = NEXT(m) )
6376: ndl_addto(DL(m),d);
6377: SG(t) += TD(d);
6378: s = nd_add(0,s,t);
6379: }
6380: FREENM(m0);
6381: return s;
6382: }
6383: }
6384: }
6385:
6386: P ndvtop(int mod,VL vl,VL dvl,NDV p)
6387: {
6388: VL tvl;
6389: int len,n,j,i,e;
6390: NMV m;
6391: Z q;
6392: P c;
6393: UINT *d;
6394: P s,r,u,t,w;
6395: GFS gfs;
6396:
6397: if ( !p ) return 0;
6398: else {
6399: len = LEN(p);
6400: n = NV(p);
6401: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6402: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
6403: if ( mod == -1 ) {
6404: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6405: } else if ( mod == -2 ) {
6406: c = (P)CZ(m);
6407: } else if ( mod > 0 ) {
1.6 noro 6408: STOZ(CM(m),q); c = (P)q;
1.1 noro 6409: } else
6410: c = CP(m);
6411: d = DL(m);
6412: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6413: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6414: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6415: }
6416: addp(vl,s,t,&u); s = u;
6417: }
6418: return s;
6419: }
6420: }
6421:
6422: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
6423: {
6424: VL tvl;
6425: int len,n,j,i,e;
6426: NMV m;
6427: Z q;
6428: P c;
6429: UINT *d;
6430: P s,r,u,t,w;
6431: GFS gfs;
6432: P *a;
6433: LIST l;
6434: NODE nd,nd1;
6435:
6436: if ( !p ) return 0;
6437: else {
6438: a = (P *)MALLOC((rank+1)*sizeof(P));
6439: for ( i = 0; i <= rank; i++ ) a[i] = 0;
6440: len = LEN(p);
6441: n = NV(p);
6442: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
6443: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
6444: if ( mod == -1 ) {
6445: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
6446: } else if ( mod ) {
1.6 noro 6447: STOZ(CM(m),q); c = (P)q;
1.1 noro 6448: } else
6449: c = CP(m);
6450: d = DL(m);
6451: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 6452: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 6453: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
6454: }
6455: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
6456: }
6457: nd = 0;
6458: for ( i = rank; i > 0; i-- ) {
6459: MKNODE(nd1,a[i],nd); nd = nd1;
6460: }
6461: MKLIST(l,nd);
6462: return l;
6463: }
6464: }
6465:
6466: NDV ndtondv(int mod,ND p)
6467: {
6468: NDV d;
6469: NMV m,m0;
6470: NM t;
6471: int i,len;
6472:
6473: if ( !p ) return 0;
6474: len = LEN(p);
6475: if ( mod > 0 || mod == -1 )
6476: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6477: else
6478: m0 = m = MALLOC(len*nmv_adv);
6479: #if 0
6480: ndv_alloc += nmv_adv*len;
6481: #endif
6482: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
6483: ndl_copy(DL(t),DL(m));
1.6 noro 6484: CZ(m) = CZ(t);
1.1 noro 6485: }
6486: MKNDV(NV(p),m0,len,d);
6487: SG(d) = SG(p);
1.24 noro 6488: d->sig = p->sig;
1.1 noro 6489: return d;
6490: }
6491:
1.16 noro 6492: static int dmm_comp_nv;
6493:
6494: int dmm_comp(DMM *a,DMM *b)
6495: {
6496: return -compdmm(dmm_comp_nv,*a,*b);
6497: }
6498:
6499: void dmm_sort_by_ord(DMM *a,int len,int nv)
6500: {
6501: dmm_comp_nv = nv;
6502: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
6503: }
6504:
6505: void dpm_sort(DPM p,DPM *rp)
6506: {
6507: DMM t,t1;
6508: int len,i,n;
6509: DMM *a;
6510: DPM d;
6511:
6512: if ( !p ) *rp = 0;
6513: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6514: a = (DMM *)MALLOC(len*sizeof(DMM));
6515: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6516: n = p->nv;
6517: dmm_sort_by_ord(a,len,n);
6518: t = 0;
6519: for ( i = len-1; i >= 0; i-- ) {
6520: NEWDMM(t1);
6521: t1->c = a[i]->c;
6522: t1->dl = a[i]->dl;
6523: t1->pos = a[i]->pos;
6524: t1->next = t;
6525: t = t1;
6526: }
6527: MKDPM(n,t,d);
6528: SG(d) = SG(p);
6529: *rp = d;
6530: }
6531:
1.18 noro 6532: int dpm_comp(DPM *a,DPM *b)
6533: {
1.22 noro 6534: return -compdpm(CO,*a,*b);
1.18 noro 6535: }
6536:
6537: NODE dpm_sort_list(NODE l)
6538: {
6539: int i,len;
6540: NODE t,t1;
6541: DPM *a;
6542:
6543: len = length(l);
6544: a = (DPM *)MALLOC(len*sizeof(DPM));
6545: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
6546: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
6547: t = 0;
6548: for ( i = len-1; i >= 0; i-- ) {
6549: MKNODE(t1,(pointer)a[i],t); t = t1;
6550: }
6551: return t;
6552: }
6553:
1.20 noro 6554: int nmv_comp(NMV a,NMV b)
6555: {
1.21 noro 6556: int t;
6557: t = DL_COMPARE(a->dl,b->dl);
6558: return -t;
1.20 noro 6559: }
6560:
1.16 noro 6561: NDV dpmtondv(int mod,DPM p)
6562: {
6563: NDV d;
6564: NMV m,m0;
6565: DMM t;
6566: DMM *a;
6567: int i,len,n;
6568:
6569: if ( !p ) return 0;
6570: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
6571: a = (DMM *)MALLOC(len*sizeof(DMM));
6572: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
6573: n = p->nv;
6574: dmm_sort_by_ord(a,len,n);
6575: if ( mod > 0 || mod == -1 )
6576: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
6577: else
6578: m0 = m = MALLOC(len*nmv_adv);
6579: #if 0
6580: ndv_alloc += nmv_adv*len;
6581: #endif
6582: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
6583: dltondl(n,a[i]->dl,DL(m));
6584: MPOS(DL(m)) = a[i]->pos;
1.20 noro 6585: TD(DL(m)) = ndl_weight(DL(m));
1.16 noro 6586: CZ(m) = (Z)a[i]->c;
6587: }
1.20 noro 6588: qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16 noro 6589: MKNDV(NV(p),m0,len,d);
6590: SG(d) = SG(p);
6591: return d;
6592: }
6593:
1.1 noro 6594: ND ndvtond(int mod,NDV p)
6595: {
6596: ND d;
6597: NM m,m0;
6598: NMV t;
6599: int i,len;
6600:
6601: if ( !p ) return 0;
6602: m0 = 0;
6603: len = p->len;
6604: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6605: NEXTNM(m0,m);
6606: ndl_copy(DL(t),DL(m));
1.6 noro 6607: CZ(m) = CZ(t);
1.1 noro 6608: }
6609: NEXT(m) = 0;
6610: MKND(NV(p),m0,len,d);
6611: SG(d) = SG(p);
1.28 noro 6612: d->sig = p->sig;
1.1 noro 6613: return d;
6614: }
6615:
6616: DP ndvtodp(int mod,NDV p)
6617: {
6618: MP m,m0;
6619: DP d;
6620: NMV t;
6621: int i,len;
6622:
6623: if ( !p ) return 0;
6624: m0 = 0;
6625: len = p->len;
6626: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6627: NEXTMP(m0,m);
6628: m->dl = ndltodl(nd_nvar,DL(t));
6629: m->c = (Obj)ndctop(mod,t->c);
6630: }
6631: NEXT(m) = 0;
6632: MKDP(nd_nvar,m0,d);
6633: SG(d) = SG(p);
6634: return d;
6635: }
6636:
1.16 noro 6637: DPM ndvtodpm(int mod,NDV p)
6638: {
6639: DMM m,m0;
6640: DPM d;
6641: NMV t;
6642: int i,len;
6643:
6644: if ( !p ) return 0;
6645: m0 = 0;
6646: len = p->len;
6647: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
6648: NEXTDMM(m0,m);
6649: m->dl = ndltodl(nd_nvar,DL(t));
6650: m->c = (Obj)ndctop(mod,t->c);
6651: m->pos = MPOS(DL(t));
6652: }
6653: NEXT(m) = 0;
6654: MKDPM(nd_nvar,m0,d);
6655: SG(d) = SG(p);
6656: return d;
6657: }
6658:
6659:
1.1 noro 6660: DP ndtodp(int mod,ND p)
6661: {
6662: MP m,m0;
6663: DP d;
6664: NM t;
6665: int i,len;
6666:
6667: if ( !p ) return 0;
6668: m0 = 0;
6669: len = p->len;
6670: for ( t = BDY(p); t; t = NEXT(t) ) {
6671: NEXTMP(m0,m);
6672: m->dl = ndltodl(nd_nvar,DL(t));
6673: m->c = (Obj)ndctop(mod,t->c);
6674: }
6675: NEXT(m) = 0;
6676: MKDP(nd_nvar,m0,d);
6677: SG(d) = SG(p);
6678: return d;
6679: }
6680:
6681: void ndv_print(NDV p)
6682: {
6683: NMV m;
6684: int i,len;
6685:
6686: if ( !p ) printf("0\n");
6687: else {
6688: len = LEN(p);
6689: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6690: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28 noro 6691: else printf("+%ld*",CM(m));
1.1 noro 6692: ndl_print(DL(m));
6693: }
6694: printf("\n");
6695: }
6696: }
6697:
6698: void ndv_print_q(NDV p)
6699: {
6700: NMV m;
6701: int i,len;
6702:
6703: if ( !p ) printf("0\n");
6704: else {
6705: len = LEN(p);
6706: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
6707: printf("+");
1.6 noro 6708: printexpr(CO,(Obj)CZ(m));
1.1 noro 6709: printf("*");
6710: ndl_print(DL(m));
6711: }
6712: printf("\n");
6713: }
6714: }
6715:
6716: NODE ndv_reducebase(NODE x,int *perm)
6717: {
6718: int len,i,j;
6719: NDVI w;
6720: NODE t,t0;
6721:
6722: len = length(x);
6723: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
6724: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
6725: w[i].p = BDY(t); w[i].i = perm[i];
6726: }
6727: for ( i = 0; i < len; i++ ) {
6728: for ( j = 0; j < i; j++ ) {
6729: if ( w[i].p && w[j].p ) {
6730: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
6731: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
6732: }
6733: }
6734: }
6735: for ( i = j = 0, t0 = 0; i < len; i++ ) {
6736: if ( w[i].p ) {
6737: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
6738: perm[j++] = w[i].i;
6739: }
6740: }
6741: NEXT(t) = 0; x = t0;
6742: return x;
6743: }
6744:
6745: /* XXX incomplete */
6746:
1.21 noro 6747: extern DMMstack dmm_stack;
6748: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16 noro 6749:
1.1 noro 6750: void nd_init_ord(struct order_spec *ord)
6751: {
6752: nd_module = (ord->id >= 256);
6753: if ( nd_module ) {
6754: nd_dcomp = -1;
1.21 noro 6755: nd_module_ordtype = ord->module_ordtype;
1.1 noro 6756: nd_pot_nelim = ord->pot_nelim;
6757: nd_poly_weight_len = ord->nv;
6758: nd_poly_weight = ord->top_weight;
6759: nd_module_rank = ord->module_rank;
6760: nd_module_weight = ord->module_top_weight;
6761: }
6762: nd_matrix = 0;
6763: nd_matrix_len = 0;
6764: switch ( ord->id ) {
6765: case 0:
6766: switch ( ord->ord.simple ) {
6767: case 0:
6768: nd_dcomp = 1;
6769: nd_isrlex = 1;
6770: break;
6771: case 1:
6772: nd_dcomp = 1;
6773: nd_isrlex = 0;
6774: break;
6775: case 2:
6776: nd_dcomp = 0;
6777: nd_isrlex = 0;
6778: ndl_compare_function = ndl_lex_compare;
6779: break;
6780: case 11:
6781: /* XXX */
6782: nd_dcomp = 0;
6783: nd_isrlex = 1;
6784: ndl_compare_function = ndl_ww_lex_compare;
6785: break;
6786: default:
6787: error("nd_gr : unsupported order");
6788: }
6789: break;
6790: case 1:
6791: /* block order */
6792: /* XXX */
6793: nd_dcomp = -1;
6794: nd_isrlex = 0;
6795: ndl_compare_function = ndl_block_compare;
6796: break;
6797: case 2:
6798: /* matrix order */
6799: /* XXX */
6800: nd_dcomp = -1;
6801: nd_isrlex = 0;
6802: nd_matrix_len = ord->ord.matrix.row;
6803: nd_matrix = ord->ord.matrix.matrix;
6804: ndl_compare_function = ndl_matrix_compare;
6805: break;
6806: case 3:
6807: /* composite order */
6808: nd_dcomp = -1;
6809: nd_isrlex = 0;
6810: nd_worb_len = ord->ord.composite.length;
6811: nd_worb = ord->ord.composite.w_or_b;
6812: ndl_compare_function = ndl_composite_compare;
6813: break;
6814:
6815: /* module order */
6816: case 256:
6817: switch ( ord->ord.simple ) {
6818: case 0:
1.21 noro 6819: nd_dcomp = 0;
1.1 noro 6820: nd_isrlex = 1;
1.21 noro 6821: ndl_compare_function = ndl_module_glex_compare;
1.1 noro 6822: break;
6823: case 1:
1.21 noro 6824: nd_dcomp = 0;
1.1 noro 6825: nd_isrlex = 0;
6826: ndl_compare_function = ndl_module_glex_compare;
6827: break;
6828: case 2:
1.21 noro 6829: nd_dcomp = 0;
1.1 noro 6830: nd_isrlex = 0;
1.21 noro 6831: ndl_compare_function = ndl_module_compare;
6832: ndl_base_compare_function = ndl_lex_compare;
1.1 noro 6833: break;
6834: default:
1.21 noro 6835: error("nd_init_ord : unsupported order");
1.1 noro 6836: }
6837: break;
6838: case 257:
6839: /* block order */
6840: nd_isrlex = 0;
1.21 noro 6841: ndl_compare_function = ndl_module_compare;
6842: ndl_base_compare_function = ndl_block_compare;
1.1 noro 6843: break;
6844: case 258:
6845: /* matrix order */
6846: nd_isrlex = 0;
6847: nd_matrix_len = ord->ord.matrix.row;
6848: nd_matrix = ord->ord.matrix.matrix;
1.21 noro 6849: ndl_compare_function = ndl_module_compare;
6850: ndl_base_compare_function = ndl_matrix_compare;
1.1 noro 6851: break;
6852: case 259:
6853: /* composite order */
6854: nd_isrlex = 0;
6855: nd_worb_len = ord->ord.composite.length;
6856: nd_worb = ord->ord.composite.w_or_b;
1.21 noro 6857: ndl_compare_function = ndl_module_compare;
6858: ndl_base_compare_function = ndl_composite_compare;
6859: break;
6860: case 300:
6861: /* schreyer order */
6862: if ( ord->base->id != 256 )
6863: error("nd_init_ord : unsupported base order");
6864: ndl_compare_function = ndl_module_schreyer_compare;
6865: dmm_stack = ord->dmmstack;
6866: switch ( ord->base->ord.simple ) {
6867: case 0:
6868: nd_isrlex = 1;
6869: ndl_base_compare_function = ndl_glex_compare;
6870: dl_base_compare_function = cmpdl_revgradlex;
6871: break;
6872: case 1:
6873: nd_isrlex = 0;
6874: ndl_base_compare_function = ndl_glex_compare;
6875: dl_base_compare_function = cmpdl_gradlex;
6876: break;
6877: case 2:
6878: nd_isrlex = 0;
6879: ndl_base_compare_function = ndl_lex_compare;
6880: dl_base_compare_function = cmpdl_lex;
6881: break;
6882: default:
6883: error("nd_init_ord : unsupported order");
6884: }
1.1 noro 6885: break;
6886: }
6887: nd_ord = ord;
6888: }
6889:
6890: BlockMask nd_create_blockmask(struct order_spec *ord)
6891: {
6892: int n,i,j,s,l;
6893: UINT *t;
6894: BlockMask bm;
6895:
6896: /* we only create mask table for block order */
6897: if ( ord->id != 1 && ord->id != 257 )
6898: return 0;
6899: n = ord->ord.block.length;
6900: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
6901: bm->n = n;
6902: bm->order_pair = ord->ord.block.order_pair;
6903: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
6904: for ( i = 0, s = 0; i < n; i++ ) {
6905: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
6906: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
6907: l = bm->order_pair[i].length;
6908: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
6909: }
6910: return bm;
6911: }
6912:
6913: EPOS nd_create_epos(struct order_spec *ord)
6914: {
6915: int i,j,l,s,ord_l,ord_o;
6916: EPOS epos;
6917: struct order_pair *op;
6918:
6919: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
6920: switch ( ord->id ) {
1.21 noro 6921: case 0: case 256: case 300:
1.1 noro 6922: if ( nd_isrlex ) {
6923: for ( i = 0; i < nd_nvar; i++ ) {
6924: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
6925: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
6926: }
6927: } else {
6928: for ( i = 0; i < nd_nvar; i++ ) {
6929: epos[i].i = nd_exporigin + i/nd_epw;
6930: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6931: }
6932: }
6933: break;
6934: case 1: case 257:
6935: /* block order */
6936: l = ord->ord.block.length;
6937: op = ord->ord.block.order_pair;
6938: for ( j = 0, s = 0; j < l; j++ ) {
6939: ord_o = op[j].order;
6940: ord_l = op[j].length;
6941: if ( !ord_o )
6942: for ( i = 0; i < ord_l; i++ ) {
6943: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
6944: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
6945: }
6946: else
6947: for ( i = 0; i < ord_l; i++ ) {
6948: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
6949: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
6950: }
6951: s += ord_l;
6952: }
6953: break;
6954: case 2:
6955: /* matrix order */
6956: case 3:
6957: /* composite order */
6958: default:
6959: for ( i = 0; i < nd_nvar; i++ ) {
6960: epos[i].i = nd_exporigin + i/nd_epw;
6961: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
6962: }
6963: break;
6964: }
6965: return epos;
6966: }
6967:
6968: /* external interface */
6969:
6970: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
6971: {
6972: NODE t,in0,in;
6973: ND ndf,nf;
6974: NDV ndvf;
6975: VL vv,tv;
6976: int stat,nvar,max,mrank;
6977: union oNDC dn;
6978: Q cont;
6979: P pp;
6980: LIST ppl;
6981:
6982: if ( !f ) {
6983: *rp = 0;
6984: return;
6985: }
6986: pltovl(v,&vv);
6987: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
6988:
6989: /* max=65536 implies nd_bpe=32 */
6990: max = 65536;
6991:
6992: nd_module = 0;
6993: /* nd_module will be set if ord is a module ordering */
6994: nd_init_ord(ord);
6995: nd_setup_parameters(nvar,max);
6996: if ( nd_module && OID(f) != O_LIST )
6997: error("nd_nf_p : the first argument must be a list");
6998: if ( nd_module ) mrank = length(BDY((LIST)f));
6999: /* conversion to ndv */
7000: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
7001: NEXTNODE(in0,in);
7002: if ( nd_module ) {
7003: if ( !BDY(t) || OID(BDY(t)) != O_LIST
7004: || length(BDY((LIST)BDY(t))) != mrank )
7005: error("nd_nf_p : inconsistent basis element");
7006: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
7007: else ppl = (LIST)BDY(t);
7008: BDY(in) = (pointer)pltondv(CO,vv,ppl);
7009: } else {
7010: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
7011: else pp = (P)BDY(t);
7012: BDY(in) = (pointer)ptondv(CO,vv,pp);
7013: }
7014: if ( m ) ndv_mod(m,(NDV)BDY(in));
7015: }
7016: if ( in0 ) NEXT(in) = 0;
7017:
7018: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
7019: else ndvf = ptondv(CO,vv,(P)f);
7020: if ( m ) ndv_mod(m,ndvf);
7021: ndf = (pointer)ndvtond(m,ndvf);
7022:
7023: /* dont sort, dont removecont */
1.24 noro 7024: ndv_setup(m,0,in0,1,1,0);
1.1 noro 7025: nd_scale=2;
1.6 noro 7026: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 7027: if ( !stat )
7028: error("nd_nf_p : exponent too large");
7029: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
7030: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
7031: }
7032:
7033: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
7034: {
7035: NM m;
7036: UINT *t,*s;
7037: int i;
7038:
7039: for ( i = 0; i < n; i++ ) r[i] = 0;
7040: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7041: t = DL(m);
7042: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7043: r[i] = CM(m);
7044: }
7045: for ( i = 0; !r[i]; i++ );
7046: return i;
7047: }
7048:
7049: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
7050: {
7051: NM m;
7052: UINT *t,*s;
7053: int i;
7054:
7055: for ( i = 0; i < n; i++ ) r[i] = 0;
7056: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7057: t = DL(m);
7058: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7059: r[i] = CZ(m);
1.1 noro 7060: }
7061: for ( i = 0; !r[i]; i++ );
7062: return i;
7063: }
7064:
7065: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
7066: {
7067: NM m;
7068: UINT *t,*s;
7069: int i;
7070:
7071: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
7072: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
7073: t = DL(m);
7074: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
7075: mpz_set(r[i],BDY(CZ(m)));
7076: }
7077: for ( i = 0; !mpz_sgn(r[i]); i++ );
7078: return i;
7079: }
7080:
7081: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
7082: {
7083: NM m;
7084: unsigned long *v;
7085: int i,j,h,size;
7086: UINT *s,*t;
7087:
7088: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
7089: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
7090: bzero(v,size);
7091: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
7092: t = DL(m);
7093: h = ndl_hash_value(t);
7094: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7095: v[i/BLEN] |= 1L <<(i%BLEN);
7096: }
7097: return v;
7098: }
7099:
7100: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
7101: {
7102: NMV mr;
7103: UINT *d,*t,*s;
7104: int i,j,len,h,head;
7105:
7106: d = DL(m);
7107: len = LEN(p);
7108: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7109: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7110: ndl_add(d,DL(mr),t);
7111: h = ndl_hash_value(t);
7112: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
7113: if ( j == 0 ) head = i;
7114: v[i/BLEN] |= 1L <<(i%BLEN);
7115: }
7116: return head;
7117: }
7118:
7119: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
7120: {
7121: NM m;
7122: NMV mr;
7123: UINT *d,*t,*s;
7124: NDV p;
7125: int i,j,len;
7126: Z *r;
7127:
7128: m = pair->mul;
7129: d = DL(m);
7130: p = nd_ps[pair->index];
7131: len = LEN(p);
7132: r = (Z *)CALLOC(n,sizeof(Q));
7133: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7134: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
7135: ndl_add(d,DL(mr),t);
7136: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 7137: r[i] = CZ(mr);
1.1 noro 7138: }
7139: return r;
7140: }
7141:
1.11 noro 7142: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 7143: {
7144: NM m;
7145: NMV mr;
1.11 noro 7146: UINT *d,*t,*s,*u;
1.1 noro 7147: NDV p;
7148: unsigned char *ivc;
7149: unsigned short *ivs;
7150: UINT *v,*ivi,*s0v;
1.11 noro 7151: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 7152: IndArray r;
7153:
7154: m = pair->mul;
7155: d = DL(m);
7156: if ( trace )
7157: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
7158: else
7159: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
7160:
7161: len = LEN(p);
7162: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
7163: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 7164: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
7165: ndl_add(d,DL(mr),t);
7166: st = prev;
7167: ed = n;
7168: while ( ed > st ) {
7169: md = (st+ed)/2;
7170: u = s0+md*nd_wpd;
7171: c = DL_COMPARE(u,t);
7172: if ( c == 0 ) break;
7173: else if ( c > 0 ) st = md;
7174: else ed = md;
7175: }
7176: prev = v[j] = md;
1.1 noro 7177: }
7178: r = (IndArray)MALLOC(sizeof(struct oIndArray));
7179: r->head = v[0];
7180: diff = 0;
7181: for ( i = 1; i < len; i++ ) {
7182: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
7183: }
7184: if ( diff < 256 ) {
7185: r->width = 1;
7186: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
7187: r->index.c = ivc;
7188: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
7189: } else if ( diff < 65536 ) {
7190: r->width = 2;
7191: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
7192: r->index.s = ivs;
7193: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
7194: } else {
7195: r->width = 4;
7196: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
7197: r->index.i = ivi;
7198: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
7199: }
7200: return r;
7201: }
7202:
7203: int compress_array(Z *svect,Z *cvect,int n)
7204: {
7205: int i,j;
7206:
7207: for ( i = j = 0; i < n; i++ )
7208: if ( svect[i] ) cvect[j++] = svect[i];
7209: return j;
7210: }
7211:
7212: void expand_array(Z *svect,Z *cvect,int n)
7213: {
7214: int i,j;
7215:
7216: for ( i = j = 0; j < n; i++ )
7217: if ( svect[i] ) svect[i] = cvect[j++];
7218: }
7219:
1.8 noro 7220: #if 0
1.1 noro 7221: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7222: {
7223: int i,j,k,len,pos,prev,nz;
7224: Z cs,mcs,c1,c2,cr,gcd,t;
7225: IndArray ivect;
7226: unsigned char *ivc;
7227: unsigned short *ivs;
7228: unsigned int *ivi;
7229: NDV redv;
7230: NMV mr;
7231: NODE rp;
7232: int maxrs;
7233: double hmag;
7234: Z *cvect;
1.3 noro 7235: int l;
1.1 noro 7236:
7237: maxrs = 0;
7238: for ( i = 0; i < col && !svect[i]; i++ );
7239: if ( i == col ) return maxrs;
7240: hmag = p_mag((P)svect[i])*nd_scale;
7241: cvect = (Z *)MALLOC(col*sizeof(Q));
7242: for ( i = 0; i < nred; i++ ) {
7243: ivect = imat[i];
7244: k = ivect->head;
7245: if ( svect[k] ) {
7246: maxrs = MAX(maxrs,rp0[i]->sugar);
7247: redv = nd_demand?ndv_load(rp0[i]->index)
7248: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7249: len = LEN(redv); mr = BDY(redv);
1.6 noro 7250: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 7251: chsgnz(cs,&mcs);
7252: if ( !UNIQ(cr) ) {
7253: for ( j = 0; j < col; j++ ) {
7254: mulz(svect[j],cr,&c1); svect[j] = c1;
7255: }
7256: }
7257: svect[k] = 0; prev = k;
7258: switch ( ivect->width ) {
7259: case 1:
7260: ivc = ivect->index.c;
7261: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7262: pos = prev+ivc[j]; prev = pos;
1.6 noro 7263: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7264: }
7265: break;
7266: case 2:
7267: ivs = ivect->index.s;
7268: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7269: pos = prev+ivs[j]; prev = pos;
1.6 noro 7270: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7271: }
7272: break;
7273: case 4:
7274: ivi = ivect->index.i;
7275: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7276: pos = prev+ivi[j]; prev = pos;
1.6 noro 7277: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 7278: }
7279: break;
7280: }
7281: for ( j = k+1; j < col && !svect[j]; j++ );
7282: if ( j == col ) break;
7283: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
7284: nz = compress_array(svect,cvect,col);
7285: removecont_array((P *)cvect,nz,1);
7286: expand_array(svect,cvect,nz);
7287: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
7288: }
7289: }
7290: }
7291: nz = compress_array(svect,cvect,col);
7292: removecont_array((P *)cvect,nz,1);
7293: expand_array(svect,cvect,nz);
7294: if ( DP_Print ) {
7295: fprintf(asir_out,"-"); fflush(asir_out);
7296: }
7297: return maxrs;
7298: }
1.4 noro 7299: #else
1.9 noro 7300:
1.4 noro 7301: /* direct mpz version */
7302: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7303: {
7304: int i,j,k,len,pos,prev;
7305: mpz_t cs,cr,gcd;
7306: IndArray ivect;
7307: unsigned char *ivc;
7308: unsigned short *ivs;
7309: unsigned int *ivi;
7310: NDV redv;
7311: NMV mr;
7312: NODE rp;
7313: int maxrs;
7314: double hmag;
7315: int l;
1.13 noro 7316: static mpz_t *svect;
7317: static int svect_len=0;
1.4 noro 7318:
7319: maxrs = 0;
7320: for ( i = 0; i < col && !svect0[i]; i++ );
7321: if ( i == col ) return maxrs;
7322: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 7323: if ( col > svect_len ) {
7324: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7325: svect_len = col;
7326: }
1.4 noro 7327: for ( i = 0; i < col; i++ ) {
7328: mpz_init(svect[i]);
7329: if ( svect0[i] )
7330: mpz_set(svect[i],BDY(svect0[i]));
7331: else
7332: mpz_set_ui(svect[i],0);
7333: }
7334: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
7335: for ( i = 0; i < nred; i++ ) {
7336: ivect = imat[i];
7337: k = ivect->head;
7338: if ( mpz_sgn(svect[k]) ) {
7339: maxrs = MAX(maxrs,rp0[i]->sugar);
7340: redv = nd_demand?ndv_load(rp0[i]->index)
7341: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
7342: len = LEN(redv); mr = BDY(redv);
1.6 noro 7343: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 7344: mpz_div(cs,svect[k],gcd);
1.6 noro 7345: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 7346: mpz_neg(cs,cs);
1.9 noro 7347: if ( MUNIMPZ(cr) )
7348: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
7349: else if ( !UNIMPZ(cr) )
7350: for ( j = 0; j < col; j++ ) {
7351: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
7352: }
1.4 noro 7353: mpz_set_ui(svect[k],0);
7354: prev = k;
7355: switch ( ivect->width ) {
7356: case 1:
7357: ivc = ivect->index.c;
7358: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7359: pos = prev+ivc[j]; prev = pos;
1.6 noro 7360: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7361: }
7362: break;
7363: case 2:
7364: ivs = ivect->index.s;
7365: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7366: pos = prev+ivs[j]; prev = pos;
1.6 noro 7367: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7368: }
7369: break;
7370: case 4:
7371: ivi = ivect->index.i;
7372: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7373: pos = prev+ivi[j]; prev = pos;
1.6 noro 7374: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 7375: }
7376: break;
7377: }
7378: for ( j = k+1; j < col && !svect[j]; j++ );
7379: if ( j == col ) break;
7380: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
7381: mpz_removecont_array(svect,col);
7382: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
7383: }
7384: }
7385: }
7386: mpz_removecont_array(svect,col);
7387: if ( DP_Print ) {
7388: fprintf(asir_out,"-"); fflush(asir_out);
7389: }
7390: for ( i = 0; i < col; i++ )
7391: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
7392: else svect0[i] = 0;
7393: return maxrs;
7394: }
7395: #endif
1.1 noro 7396:
7397: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7398: {
7399: int i,j,k,len,pos,prev;
7400: UINT c,c1,c2,c3,up,lo,dmy;
7401: IndArray ivect;
7402: unsigned char *ivc;
7403: unsigned short *ivs;
7404: unsigned int *ivi;
7405: NDV redv;
7406: NMV mr;
7407: NODE rp;
7408: int maxrs;
7409:
7410: maxrs = 0;
7411: for ( i = 0; i < nred; i++ ) {
7412: ivect = imat[i];
7413: k = ivect->head; svect[k] %= m;
7414: if ( (c = svect[k]) != 0 ) {
7415: maxrs = MAX(maxrs,rp0[i]->sugar);
7416: c = m-c; redv = nd_ps[rp0[i]->index];
7417: len = LEN(redv); mr = BDY(redv);
7418: svect[k] = 0; prev = k;
7419: switch ( ivect->width ) {
7420: case 1:
7421: ivc = ivect->index.c;
7422: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7423: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
7424: if ( c1 ) {
7425: c2 = svect[pos];
7426: DMA(c1,c,c2,up,lo);
7427: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
7428: } else svect[pos] = lo;
7429: }
7430: }
7431: break;
7432: case 2:
7433: ivs = ivect->index.s;
7434: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7435: pos = prev+ivs[j]; c1 = CM(mr);
7436: 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 4:
7446: ivi = ivect->index.i;
7447: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7448: pos = prev+ivi[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: }
7459: }
7460: }
7461: for ( i = 0; i < col; i++ )
7462: if ( svect[i] >= (UINT)m ) svect[i] %= m;
7463: return maxrs;
7464: }
7465:
7466: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7467: {
7468: int i,j,k,len,pos,prev;
7469: UINT c,c1,c2,c3,up,lo,dmy;
7470: IndArray ivect;
7471: unsigned char *ivc;
7472: unsigned short *ivs;
7473: unsigned int *ivi;
7474: NDV redv;
7475: NMV mr;
7476: NODE rp;
7477: int maxrs;
7478:
7479: maxrs = 0;
7480: for ( i = 0; i < nred; i++ ) {
7481: ivect = imat[i];
7482: k = ivect->head;
7483: if ( (c = svect[k]) != 0 ) {
7484: maxrs = MAX(maxrs,rp0[i]->sugar);
7485: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
7486: len = LEN(redv); mr = BDY(redv);
7487: svect[k] = 0; prev = k;
7488: switch ( ivect->width ) {
7489: case 1:
7490: ivc = ivect->index.c;
7491: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7492: pos = prev+ivc[j]; prev = pos;
7493: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7494: }
7495: break;
7496: case 2:
7497: ivs = ivect->index.s;
7498: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7499: pos = prev+ivs[j]; prev = pos;
7500: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7501: }
7502: break;
7503: case 4:
7504: ivi = ivect->index.i;
7505: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7506: pos = prev+ivi[j]; prev = pos;
7507: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
7508: }
7509: break;
7510: }
7511: }
7512: }
7513: return maxrs;
7514: }
7515:
7516: ND nd_add_lf(ND p1,ND p2)
7517: {
7518: int n,c,can;
7519: ND r;
7520: NM m1,m2,mr0,mr,s;
7521: Z t;
7522:
7523: if ( !p1 ) return p2;
7524: else if ( !p2 ) return p1;
7525: else {
7526: can = 0;
7527: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
7528: c = DL_COMPARE(DL(m1),DL(m2));
7529: switch ( c ) {
7530: case 0:
7531: addlf(CZ(m1),CZ(m2),&t);
7532: s = m1; m1 = NEXT(m1);
7533: if ( t ) {
7534: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
7535: } else {
7536: can += 2; FREENM(s);
7537: }
7538: s = m2; m2 = NEXT(m2); FREENM(s);
7539: break;
7540: case 1:
7541: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
7542: break;
7543: case -1:
7544: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
7545: break;
7546: }
7547: }
7548: if ( !mr0 )
7549: if ( m1 ) mr0 = m1;
7550: else if ( m2 ) mr0 = m2;
7551: else return 0;
7552: else if ( m1 ) NEXT(mr) = m1;
7553: else if ( m2 ) NEXT(mr) = m2;
7554: else NEXT(mr) = 0;
7555: BDY(p1) = mr0;
7556: SG(p1) = MAX(SG(p1),SG(p2));
7557: LEN(p1) = LEN(p1)+LEN(p2)-can;
7558: FREEND(p2);
7559: return p1;
7560: }
7561: }
7562:
7563: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
7564: {
7565: int i,j,k,len,pos,prev;
7566: mpz_t c,mc,c1;
7567: IndArray ivect;
7568: unsigned char *ivc;
7569: unsigned short *ivs;
7570: unsigned int *ivi;
7571: NDV redv;
7572: NMV mr;
7573: NODE rp;
7574: int maxrs;
7575:
7576: maxrs = 0;
7577: lf_lazy = 1;
7578: for ( i = 0; i < nred; i++ ) {
7579: ivect = imat[i];
7580: k = ivect->head;
7581: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
7582: if ( mpz_sgn(svect[k]) ) {
7583: maxrs = MAX(maxrs,rp0[i]->sugar);
7584: mpz_neg(svect[k],svect[k]);
7585: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
7586: len = LEN(redv); mr = BDY(redv);
7587: prev = k;
7588: switch ( ivect->width ) {
7589: case 1:
7590: ivc = ivect->index.c;
7591: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7592: pos = prev+ivc[j]; prev = pos;
7593: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7594: }
7595: break;
7596: case 2:
7597: ivs = ivect->index.s;
7598: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7599: pos = prev+ivs[j]; prev = pos;
7600: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7601: }
7602: break;
7603: case 4:
7604: ivi = ivect->index.i;
7605: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
7606: pos = prev+ivi[j]; prev = pos;
7607: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
7608: }
7609: break;
7610: }
7611: mpz_set_ui(svect[k],0);
7612: }
7613: }
7614: lf_lazy=0;
7615: for ( i = 0; i < col; i++ ) {
7616: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
7617: }
7618: return maxrs;
7619: }
7620:
7621: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
7622: {
7623: int i,j,k,l,rank,s;
7624: mpz_t a,a1,inv;
7625: mpz_t *t,*pivot,*pk;
7626: mpz_t **mat;
7627: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
7628: int size,size1;
7629:
7630: mpz_init(inv);
7631: mpz_init(a);
7632: mat = (mpz_t **)mat0;
7633: size = 0;
7634: for ( rank = 0, j = 0; j < col; j++ ) {
7635: for ( i = rank; i < row; i++ ) {
7636: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
7637: }
7638: for ( i = rank; i < row; i++ )
7639: if ( mpz_sgn(mat[i][j]) )
7640: break;
7641: if ( i == row ) {
7642: colstat[j] = 0;
7643: continue;
7644: } else
7645: colstat[j] = 1;
7646: if ( i != rank ) {
7647: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7648: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7649: }
7650: pivot = mat[rank];
7651: s = sugar[rank];
7652: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
7653: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7654: if ( mpz_sgn(*pk) ) {
7655: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
7656: }
7657: for ( i = rank+1; i < row; i++ ) {
7658: t = mat[i];
7659: if ( mpz_sgn(t[j]) ) {
7660: sugar[i] = MAX(sugar[i],s);
7661: mpz_neg(a,t[j]);
7662: red_by_vect_lf(t+j,pivot+j,a,col-j);
7663: }
7664: }
7665: rank++;
7666: }
7667: for ( j = col-1, l = rank-1; j >= 0; j-- )
7668: if ( colstat[j] ) {
7669: pivot = mat[l];
7670: s = sugar[l];
7671: for ( k = j; k < col; k++ )
7672: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
7673: for ( i = 0; i < l; i++ ) {
7674: t = mat[i];
7675: if ( mpz_sgn(t[j]) ) {
7676: sugar[i] = MAX(sugar[i],s);
7677: mpz_neg(a,t[j]);
7678: red_by_vect_lf(t+j,pivot+j,a,col-j);
7679: }
7680: }
7681: l--;
7682: }
7683: for ( j = 0, l = 0; l < rank; j++ )
7684: if ( colstat[j] ) {
7685: t = mat[l];
7686: for ( k = j; k < col; k++ ) {
7687: mpz_mod(t[k],t[k],BDY(current_mod_lf));
7688: }
7689: l++;
7690: }
7691: return rank;
7692: }
7693:
7694:
7695: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
7696: {
7697: int j,k,len;
7698: UINT *p;
7699: UINT c;
7700: NDV r;
7701: NMV mr0,mr;
7702:
7703: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7704: if ( !len ) return 0;
7705: else {
7706: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7707: #if 0
7708: ndv_alloc += nmv_adv*len;
7709: #endif
7710: mr = mr0;
7711: p = s0vect;
7712: for ( j = k = 0; j < col; j++, p += nd_wpd )
7713: if ( !rhead[j] ) {
7714: if ( (c = vect[k++]) != 0 ) {
7715: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
7716: }
7717: }
7718: MKNDV(nd_nvar,mr0,len,r);
7719: return r;
7720: }
7721: }
7722:
7723: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
7724: {
7725: int j,k,len;
7726: UINT *p;
7727: NDV r;
7728: NMV mr0,mr;
7729:
7730: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
7731: if ( !len ) return 0;
7732: else {
7733: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7734: mr = mr0;
7735: p = s0vect;
7736: for ( j = 0; j < col; j++, p += nd_wpd )
7737: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
7738: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
7739: }
7740: MKNDV(nd_nvar,mr0,len,r);
7741: return r;
7742: }
7743: }
7744:
7745: /* for preprocessed vector */
7746:
7747: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
7748: {
1.6 noro 7749: int j,k,len;
7750: UINT *p;
7751: Z c;
7752: NDV r;
7753: NMV mr0,mr;
1.1 noro 7754:
1.6 noro 7755: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
7756: if ( !len ) return 0;
7757: else {
7758: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 7759: #if 0
1.6 noro 7760: ndv_alloc += nmv_adv*len;
1.1 noro 7761: #endif
1.6 noro 7762: mr = mr0;
7763: p = s0vect;
7764: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
7765: if ( !rhead[j] ) {
7766: if ( (c = vect[k++]) != 0 ) {
7767: if ( !INT(c) )
7768: error("vect_to_ndv_q : components must be integers");
7769: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
7770: }
7771: }
1.1 noro 7772: }
1.6 noro 7773: MKNDV(nd_nvar,mr0,len,r);
7774: return r;
7775: }
1.1 noro 7776: }
7777:
7778: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
7779: {
7780: int j,k,len;
7781: UINT *p;
7782: mpz_t c;
7783: NDV r;
7784: NMV mr0,mr;
7785:
7786: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
7787: if ( !len ) return 0;
7788: else {
7789: mr0 = (NMV)MALLOC(nmv_adv*len);
7790: #if 0
7791: ndv_alloc += nmv_adv*len;
7792: #endif
7793: mr = mr0;
7794: p = s0vect;
7795: for ( j = k = 0; j < col; j++, p += nd_wpd )
7796: if ( !rhead[j] ) {
7797: c[0] = vect[k++][0];
7798: if ( mpz_sgn(c) ) {
7799: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
7800: }
7801: }
7802: MKNDV(nd_nvar,mr0,len,r);
7803: return r;
7804: }
7805: }
7806:
7807: /* for plain vector */
7808:
7809: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
7810: {
7811: int j,k,len;
7812: UINT *p;
7813: Z c;
7814: NDV r;
7815: NMV mr0,mr;
7816:
7817: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
7818: if ( !len ) return 0;
7819: else {
7820: mr0 = (NMV)MALLOC(nmv_adv*len);
7821: #if 0
7822: ndv_alloc += nmv_adv*len;
7823: #endif
7824: mr = mr0;
7825: p = s0vect;
7826: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
7827: if ( (c = vect[k]) != 0 ) {
7828: if ( !INT(c) )
1.6 noro 7829: error("plain_vect_to_ndv_q : components must be integers");
7830: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 7831: }
7832: MKNDV(nd_nvar,mr0,len,r);
7833: return r;
7834: }
7835: }
7836:
7837: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
7838: {
7839: ND_pairs t;
7840: NODE sp0,sp;
7841: int stat;
7842: ND spol;
7843:
7844: for ( t = l; t; t = NEXT(t) ) {
7845: stat = nd_sp(m,trace,t,&spol);
7846: if ( !stat ) return 0;
7847: if ( spol ) {
7848: add_pbucket_symbolic(bucket,spol);
7849: }
7850: }
7851: return 1;
7852: }
7853:
7854: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
7855: {
7856: NODE rp0,rp;
7857: NM mul,head,s0,s;
7858: int index,col,i,sugar;
7859: RHist h;
7860: UINT *s0v,*p;
7861: NM_ind_pair pair;
7862: ND red;
7863: NDV *ps;
7864:
7865: s0 = 0; rp0 = 0; col = 0;
7866: if ( nd_demand )
7867: ps = trace?nd_ps_trace_sym:nd_ps_sym;
7868: else
7869: ps = trace?nd_ps_trace:nd_ps;
7870: while ( 1 ) {
7871: head = remove_head_pbucket_symbolic(bucket);
7872: if ( !head ) break;
7873: if ( !s0 ) s0 = head;
7874: else NEXT(s) = head;
7875: s = head;
7876: index = ndl_find_reducer(DL(head));
7877: if ( index >= 0 ) {
7878: h = nd_psh[index];
7879: NEWNM(mul);
7880: ndl_sub(DL(head),DL(h),DL(mul));
7881: if ( ndl_check_bound2(index,DL(mul)) )
7882: return 0;
7883: sugar = TD(DL(mul))+SG(ps[index]);
1.28 noro 7884: MKNM_ind_pair(pair,mul,index,sugar,0);
1.1 noro 7885: red = ndv_mul_nm_symbolic(mul,ps[index]);
7886: add_pbucket_symbolic(bucket,nd_remove_head(red));
7887: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
7888: }
7889: col++;
7890: }
7891: if ( rp0 ) NEXT(rp) = 0;
7892: NEXT(s) = 0;
7893: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
7894: for ( i = 0, p = s0v, s = s0; i < col;
7895: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
7896: *s0vect = s0v;
7897: *r = rp0;
7898: return col;
7899: }
7900:
7901: void print_ndp(ND_pairs l)
7902: {
7903: ND_pairs t;
7904:
7905: for ( t = l; t; t = NEXT(t) )
7906: printf("[%d,%d] ",t->i1,t->i2);
7907: printf("\n");
7908: }
7909:
7910: NODE nd_f4(int m,int checkonly,int **indp)
7911: {
7912: int i,nh,stat,index,f4red;
7913: NODE r,g,tn0,tn,node;
7914: ND_pairs d,l,t,ll0,ll,lh;
7915: LIST l0,l1;
7916: ND spol,red;
7917: NDV nf,redv;
7918: NM s0,s;
7919: NODE rp0,srp0,nflist,nzlist,nzlist_t;
7920: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
7921: UINT c;
7922: UINT **spmat;
7923: UINT *s0vect,*svect,*p,*v;
7924: int *colstat;
7925: IndArray *imat;
7926: int *rhead;
7927: int spcol,sprow;
7928: int sugar,sugarh;
7929: PGeoBucket bucket;
7930: struct oEGT eg0,eg1,eg_f4;
7931: Z i1,i2,sugarq;
1.12 noro 7932:
7933: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 7934: #if 0
7935: ndv_alloc = 0;
7936: #endif
1.11 noro 7937: Nf4_red=0;
1.1 noro 7938: g = 0; d = 0;
7939: for ( i = 0; i < nd_psn; i++ ) {
7940: d = update_pairs(d,g,i,0);
7941: g = update_base(g,i);
7942: }
7943: nzlist = 0;
7944: nzlist_t = nd_nzlist;
7945: f4red = 1;
7946: nd_last_nonzero = 0;
7947: while ( d ) {
7948: get_eg(&eg0);
7949: l = nd_minsugarp(d,&d);
7950: sugar = nd_sugarweight?l->sugar2:SG(l);
7951: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7952: if ( nzlist_t ) {
7953: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 7954: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 7955: tn = BDY((LIST)ARG1(node));
7956: if ( !tn ) {
7957: nzlist_t = NEXT(nzlist_t);
7958: continue;
7959: }
7960: /* tn = [[i1,i2],...] */
7961: lh = nd_ipairtospair(tn);
7962: }
7963: bucket = create_pbucket();
7964: stat = nd_sp_f4(m,0,l,bucket);
7965: if ( !stat ) {
7966: for ( t = l; NEXT(t); t = NEXT(t) );
7967: NEXT(t) = d; d = l;
7968: d = nd_reconstruct(0,d);
7969: continue;
7970: }
7971: if ( bucket->m < 0 ) continue;
7972: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7973: if ( !col ) {
7974: for ( t = l; NEXT(t); t = NEXT(t) );
7975: NEXT(t) = d; d = l;
7976: d = nd_reconstruct(0,d);
7977: continue;
7978: }
1.12 noro 7979: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 7980: if ( DP_Print )
1.6 noro 7981: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 7982: sugar,eg_f4.exectime);
1.1 noro 7983: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
7984: if ( checkonly && nflist ) return 0;
7985: /* adding new bases */
7986: if ( nflist ) nd_last_nonzero = f4red;
7987: for ( r = nflist; r; r = NEXT(r) ) {
7988: nf = (NDV)BDY(r);
7989: ndv_removecont(m,nf);
7990: if ( !m && nd_nalg ) {
7991: ND nf1;
7992:
7993: nf1 = ndvtond(m,nf);
7994: nd_monic(0,&nf1);
7995: nd_removecont(m,nf1);
7996: nf = ndtondv(m,nf1);
7997: }
1.24 noro 7998: nh = ndv_newps(m,nf,0);
1.1 noro 7999: d = update_pairs(d,g,nh,0);
8000: g = update_base(g,nh);
8001: }
8002: if ( DP_Print ) {
8003: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
8004: }
8005: if ( nd_gentrace ) {
8006: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
8007: NEXTNODE(tn0,tn);
1.6 noro 8008: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 8009: node = mknode(2,i1,i2); MKLIST(l0,node);
8010: BDY(tn) = l0;
8011: }
8012: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 8013: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 8014: MKNODE(node,l1,nzlist); nzlist = node;
8015: }
8016: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
8017: f4red++;
8018: if ( nd_f4red && f4red > nd_f4red ) break;
8019: if ( nd_rank0 && !nflist ) break;
8020: }
8021: if ( nd_gentrace ) {
8022: MKLIST(l0,reverse_node(nzlist));
8023: MKNODE(nd_alltracelist,l0,0);
8024: }
8025: #if 0
8026: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8027: #endif
1.12 noro 8028: if ( DP_Print ) {
8029: fprintf(asir_out,"number of red=%d,",Nf4_red);
8030: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
8031: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
8032: }
1.1 noro 8033: conv_ilist(nd_demand,0,g,indp);
8034: return g;
8035: }
8036:
8037: NODE nd_f4_trace(int m,int **indp)
8038: {
8039: int i,nh,stat,index;
8040: NODE r,g;
8041: ND_pairs d,l,l0,t;
8042: ND spol,red;
8043: NDV nf,redv,nfqv,nfv;
8044: NM s0,s;
8045: NODE rp0,srp0,nflist;
8046: int nsp,nred,col,rank,len,k,j,a;
8047: UINT c;
8048: UINT **spmat;
8049: UINT *s0vect,*svect,*p,*v;
8050: int *colstat;
8051: IndArray *imat;
8052: int *rhead;
8053: int spcol,sprow;
8054: int sugar;
8055: PGeoBucket bucket;
8056: struct oEGT eg0,eg1,eg_f4;
8057:
8058: g = 0; d = 0;
8059: for ( i = 0; i < nd_psn; i++ ) {
8060: d = update_pairs(d,g,i,0);
8061: g = update_base(g,i);
8062: }
8063: while ( d ) {
8064: get_eg(&eg0);
8065: l = nd_minsugarp(d,&d);
8066: sugar = SG(l);
8067: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8068: bucket = create_pbucket();
8069: stat = nd_sp_f4(m,0,l,bucket);
8070: if ( !stat ) {
8071: for ( t = l; NEXT(t); t = NEXT(t) );
8072: NEXT(t) = d; d = l;
8073: d = nd_reconstruct(1,d);
8074: continue;
8075: }
8076: if ( bucket->m < 0 ) continue;
8077: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8078: if ( !col ) {
8079: for ( t = l; NEXT(t); t = NEXT(t) );
8080: NEXT(t) = d; d = l;
8081: d = nd_reconstruct(1,d);
8082: continue;
8083: }
8084: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8085: if ( DP_Print )
1.3 noro 8086: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 8087: sugar,eg_f4.exectime);
1.1 noro 8088: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8089: if ( !l0 ) continue;
8090: l = l0;
8091:
8092: /* over Q */
8093: bucket = create_pbucket();
8094: stat = nd_sp_f4(0,1,l,bucket);
8095: if ( !stat ) {
8096: for ( t = l; NEXT(t); t = NEXT(t) );
8097: NEXT(t) = d; d = l;
8098: d = nd_reconstruct(1,d);
8099: continue;
8100: }
8101: if ( bucket->m < 0 ) continue;
8102: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
8103: if ( !col ) {
8104: for ( t = l; NEXT(t); t = NEXT(t) );
8105: NEXT(t) = d; d = l;
8106: d = nd_reconstruct(1,d);
8107: continue;
8108: }
8109: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
8110: /* adding new bases */
8111: for ( r = nflist; r; r = NEXT(r) ) {
8112: nfqv = (NDV)BDY(r);
8113: ndv_removecont(0,nfqv);
1.6 noro 8114: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 8115: if ( nd_nalg ) {
8116: ND nf1;
8117:
8118: nf1 = ndvtond(m,nfqv);
8119: nd_monic(0,&nf1);
8120: nd_removecont(0,nf1);
8121: nfqv = ndtondv(0,nf1); nd_free(nf1);
8122: }
8123: nfv = ndv_dup(0,nfqv);
8124: ndv_mod(m,nfv);
8125: ndv_removecont(m,nfv);
1.24 noro 8126: nh = ndv_newps(0,nfv,nfqv);
1.1 noro 8127: d = update_pairs(d,g,nh,0);
8128: g = update_base(g,nh);
8129: }
8130: }
8131: #if 0
8132: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
8133: #endif
8134: conv_ilist(nd_demand,1,g,indp);
8135: return g;
8136: }
8137:
8138: int rref(matrix mat,int *sugar)
8139: {
8140: int row,col,i,j,k,l,s,wcol,wj;
8141: unsigned long bj;
8142: unsigned long **a;
8143: unsigned long *ai,*ak,*as,*t;
8144: int *pivot;
8145:
8146: row = mat->row;
8147: col = mat->col;
8148: a = mat->a;
8149: wcol = (col+BLEN-1)/BLEN;
8150: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
8151: i = 0;
8152: for ( j = 0; j < col; j++ ) {
8153: wj = j/BLEN; bj = 1L<<(j%BLEN);
8154: for ( k = i; k < row; k++ )
8155: if ( a[k][wj] & bj ) break;
8156: if ( k == row ) continue;
8157: pivot[i] = j;
8158: if ( k != i ) {
8159: t = a[i]; a[i] = a[k]; a[k] = t;
8160: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
8161: }
8162: ai = a[i];
8163: for ( k = i+1; k < row; k++ ) {
8164: ak = a[k];
8165: if ( ak[wj] & bj ) {
8166: for ( l = wj; l < wcol; l++ )
8167: ak[l] ^= ai[l];
8168: sugar[k] = MAX(sugar[k],sugar[i]);
8169: }
8170: }
8171: i++;
8172: }
8173: for ( k = i-1; k >= 0; k-- ) {
8174: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
8175: ak = a[k];
8176: for ( s = 0; s < k; s++ ) {
8177: as = a[s];
8178: if ( as[wj] & bj ) {
8179: for ( l = wj; l < wcol; l++ )
8180: as[l] ^= ak[l];
8181: sugar[s] = MAX(sugar[s],sugar[k]);
8182: }
8183: }
8184: }
8185: return i;
8186: }
8187:
8188: void print_matrix(matrix mat)
8189: {
8190: int row,col,i,j;
8191: unsigned long *ai;
8192:
8193: row = mat->row;
8194: col = mat->col;
8195: printf("%d x %d\n",row,col);
8196: for ( i = 0; i < row; i++ ) {
8197: ai = mat->a[i];
8198: for ( j = 0; j < col; j++ ) {
8199: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
8200: else putchar('0');
8201: }
8202: putchar('\n');
8203: }
8204: }
8205:
8206: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
8207:
8208: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
8209: {
8210: int row,col,wcol,wj,i,j;
8211: unsigned long bj;
8212: unsigned long *ai;
8213: unsigned long **a;
8214: int len;
8215: int *pos;
8216:
8217: row = mat->row;
8218: col = mat->col;
8219: wcol = (col+BLEN-1)/BLEN;
8220: pos = (int *)MALLOC(wcol*sizeof(int));
8221: bzero(pos,wcol*sizeof(int));
8222: for ( i = j = 0; i < wcol; i++ )
8223: if ( v[i] ) pos[j++] = i;;
8224: len = j;
8225: wj = rhead/BLEN;
8226: bj = 1L<<rhead%BLEN;
8227: a = mat->a;
8228: for ( i = 0; i < row; i++ ) {
8229: ai = a[i];
8230: if ( ai[wj]&bj ) {
8231: for ( j = 0; j < len; j++ )
8232: ai[pos[j]] ^= v[pos[j]];
8233: sugar[i] = MAX(sugar[i],rsugar);
8234: }
8235: }
8236: }
8237:
8238: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8239: {
8240: int nsp,nred,i,i0,k,rank,row;
8241: NODE r0,rp;
8242: ND_pairs sp;
8243: ND spol;
8244: NM_ind_pair rt;
8245: int *s0hash;
8246: UINT *s;
8247: int *pivot,*sugar,*head;
8248: matrix mat;
8249: NM m;
8250: NODE r;
8251: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
8252: int rhead,rsugar,size;
8253: unsigned long *v;
8254:
8255: get_eg(&eg0);
8256: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8257: nred = length(rp0);
8258: mat = alloc_matrix(nsp,col);
8259: s0hash = (int *)MALLOC(col*sizeof(int));
8260: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
8261: s0hash[i] = ndl_hash_value(s);
8262:
8263: sugar = (int *)MALLOC(nsp*sizeof(int));
8264: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
8265: nd_sp(2,0,sp,&spol);
8266: if ( spol ) {
8267: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
8268: sugar[i] = SG(spol);
8269: i++;
8270: }
8271: }
8272: mat->row = i;
8273: if ( DP_Print ) {
8274: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
8275: }
8276: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
8277: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
8278: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
8279: rt = (NM_ind_pair)BDY(rp);
8280: bzero(v,size);
8281: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
8282: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
8283: red_by_vect_2(mat,sugar,v,rhead,rsugar);
8284: }
8285:
8286: get_eg(&eg1);
8287: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
8288: rank = rref(mat,sugar);
8289:
8290: for ( i = 0, r0 = 0; i < rank; i++ ) {
8291: NEXTNODE(r0,r);
8292: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
8293: SG((NDV)BDY(r)) = sugar[i];
8294: }
8295: if ( r0 ) NEXT(r) = 0;
8296: get_eg(&eg2);
8297: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
8298: if ( DP_Print ) {
8299: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 8300: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 8301: fflush(asir_out);
8302: }
8303: return r0;
8304: }
8305:
8306:
8307: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
8308: {
8309: IndArray *imat;
1.11 noro 8310: int nsp,nred,i,start;
1.1 noro 8311: int *rhead;
8312: NODE r0,rp;
8313: ND_pairs sp;
8314: NM_ind_pair *rvect;
8315: UINT *s;
8316: int *s0hash;
1.11 noro 8317: struct oEGT eg0,eg1,eg_conv;
1.1 noro 8318:
8319: if ( m == 2 && nd_rref2 )
8320: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
8321:
8322: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
8323: nred = length(rp0);
8324: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
8325: rhead = (int *)MALLOC(col*sizeof(int));
8326: for ( i = 0; i < col; i++ ) rhead[i] = 0;
8327:
8328: /* construction of index arrays */
1.11 noro 8329: get_eg(&eg0);
1.1 noro 8330: if ( DP_Print ) {
1.11 noro 8331: fprintf(asir_out,"%dx%d,",nsp+nred,col);
8332: fflush(asir_out);
1.1 noro 8333: }
8334: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 8335: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 8336: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 8337: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 8338: rhead[imat[i]->head] = 1;
1.11 noro 8339: start = imat[i]->head;
8340: }
1.12 noro 8341: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 8342: if ( DP_Print ) {
8343: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
8344: fflush(asir_out);
1.1 noro 8345: }
8346: if ( m > 0 )
1.7 noro 8347: #if SIZEOF_LONG==8
1.1 noro 8348: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8349: #else
8350: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8351: #endif
8352: else if ( m == -1 )
8353: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
8354: else if ( m == -2 )
8355: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8356: else
8357: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
8358: return r0;
8359: }
8360:
8361: /* for Fp, 2<=p<2^16 */
8362:
8363: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8364: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8365: {
8366: int spcol,sprow,a;
8367: int i,j,k,l,rank;
8368: NODE r0,r;
8369: ND_pairs sp;
8370: ND spol;
8371: UINT **spmat;
8372: UINT *svect,*v;
8373: int *colstat;
8374: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8375: int maxrs;
8376: int *spsugar;
8377: ND_pairs *spactive;
8378:
8379: spcol = col-nred;
8380: get_eg(&eg0);
8381: /* elimination (1st step) */
8382: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8383: svect = (UINT *)MALLOC(col*sizeof(UINT));
8384: spsugar = (int *)MALLOC(nsp*sizeof(int));
8385: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8386: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8387: nd_sp(m,0,sp,&spol);
8388: if ( !spol ) continue;
8389: nd_to_vect(m,s0vect,col,spol,svect);
8390: if ( m == -1 )
8391: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8392: else
8393: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
8394: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8395: if ( i < col ) {
8396: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8397: for ( j = k = 0; j < col; j++ )
8398: if ( !rhead[j] ) v[k++] = svect[j];
8399: spsugar[sprow] = MAX(maxrs,SG(spol));
8400: if ( nz )
8401: spactive[sprow] = sp;
8402: sprow++;
8403: }
8404: nd_free(spol);
8405: }
8406: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8407: if ( DP_Print ) {
1.5 noro 8408: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8409: fflush(asir_out);
8410: }
8411: /* free index arrays */
8412: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8413:
8414: /* elimination (2nd step) */
8415: colstat = (int *)MALLOC(spcol*sizeof(int));
8416: if ( m == -1 )
8417: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8418: else
8419: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
8420: r0 = 0;
8421: for ( i = 0; i < rank; i++ ) {
8422: NEXTNODE(r0,r); BDY(r) =
8423: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8424: SG((NDV)BDY(r)) = spsugar[i];
8425: GCFREE(spmat[i]);
8426: }
8427: if ( r0 ) NEXT(r) = 0;
8428:
8429: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8430: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8431: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8432: if ( DP_Print ) {
1.5 noro 8433: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8434: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8435: nsp,nred,sprow,spcol,rank);
1.5 noro 8436: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8437: }
8438: if ( nz ) {
8439: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8440: if ( rank > 0 ) {
8441: NEXT(spactive[rank-1]) = 0;
8442: *nz = spactive[0];
8443: } else
8444: *nz = 0;
8445: }
8446: return r0;
8447: }
8448:
8449:
8450: /* for small finite fields */
8451:
8452: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
8453: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
8454: {
8455: int spcol,sprow,a;
8456: int i,j,k,l,rank;
8457: NODE r0,r;
8458: ND_pairs sp;
8459: ND spol;
8460: UINT **spmat;
8461: UINT *svect,*v;
8462: int *colstat;
8463: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8464: int maxrs;
8465: int *spsugar;
8466: ND_pairs *spactive;
8467:
8468: spcol = col-nred;
8469: get_eg(&eg0);
8470: /* elimination (1st step) */
8471: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
8472: svect = (UINT *)MALLOC(col*sizeof(UINT));
8473: spsugar = (int *)MALLOC(nsp*sizeof(int));
8474: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
8475: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8476: nd_sp(m,0,sp,&spol);
8477: if ( !spol ) continue;
8478: nd_to_vect(m,s0vect,col,spol,svect);
8479: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
8480: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8481: if ( i < col ) {
8482: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
8483: for ( j = k = 0; j < col; j++ )
8484: if ( !rhead[j] ) v[k++] = svect[j];
8485: spsugar[sprow] = MAX(maxrs,SG(spol));
8486: if ( nz )
8487: spactive[sprow] = sp;
8488: sprow++;
8489: }
8490: nd_free(spol);
8491: }
8492: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8493: if ( DP_Print ) {
1.5 noro 8494: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8495: fflush(asir_out);
8496: }
8497: /* free index arrays */
8498: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
8499:
8500: /* elimination (2nd step) */
8501: colstat = (int *)MALLOC(spcol*sizeof(int));
8502: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
8503: r0 = 0;
8504: for ( i = 0; i < rank; i++ ) {
8505: NEXTNODE(r0,r); BDY(r) =
8506: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
8507: SG((NDV)BDY(r)) = spsugar[i];
8508: GCFREE(spmat[i]);
8509: }
8510: if ( r0 ) NEXT(r) = 0;
8511:
8512: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
8513: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8514: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8515: if ( DP_Print ) {
1.5 noro 8516: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8517: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8518: nsp,nred,sprow,spcol,rank);
1.5 noro 8519: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8520: }
8521: if ( nz ) {
8522: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
8523: if ( rank > 0 ) {
8524: NEXT(spactive[rank-1]) = 0;
8525: *nz = spactive[0];
8526: } else
8527: *nz = 0;
8528: }
8529: return r0;
8530: }
8531:
8532: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8533: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8534: {
8535: int spcol,sprow,a;
8536: int i,j,k,l,rank;
8537: NODE r0,r;
8538: ND_pairs sp;
8539: ND spol;
8540: mpz_t **spmat;
8541: mpz_t *svect,*v;
8542: int *colstat;
8543: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8544: int maxrs;
8545: int *spsugar;
8546: pointer *w;
8547:
8548: spcol = col-nred;
8549: get_eg(&eg0);
8550: /* elimination (1st step) */
8551: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
8552: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
8553: spsugar = (int *)MALLOC(nsp*sizeof(int));
8554: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8555: nd_sp(m,trace,sp,&spol);
8556: if ( !spol ) continue;
8557: nd_to_vect_lf(s0vect,col,spol,svect);
8558: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
8559: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
8560: if ( i < col ) {
8561: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
8562: for ( j = k = 0; j < col; j++ )
8563: if ( !rhead[j] ) v[k++][0] = svect[j][0];
8564: spsugar[sprow] = MAX(maxrs,SG(spol));
8565: sprow++;
8566: }
8567: /* nd_free(spol); */
8568: }
8569: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8570: if ( DP_Print ) {
1.5 noro 8571: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8572: fflush(asir_out);
8573: }
8574: /* free index arrays */
8575: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8576:
8577: /* elimination (2nd step) */
8578: colstat = (int *)MALLOC(spcol*sizeof(int));
8579: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
8580: w = (pointer *)MALLOC(rank*sizeof(pointer));
8581: for ( i = 0; i < rank; i++ ) {
8582: #if 0
8583: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8584: SG((NDV)w[rank-i-1]) = spsugar[i];
8585: #else
8586: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
8587: SG((NDV)w[i]) = spsugar[i];
8588: #endif
8589: /* GCFREE(spmat[i]); */
8590:
8591: }
8592: #if 0
8593: qsort(w,rank,sizeof(NDV),
8594: (int (*)(const void *,const void *))ndv_compare);
8595: #endif
8596: r0 = 0;
8597: for ( i = 0; i < rank; i++ ) {
8598: NEXTNODE(r0,r); BDY(r) = w[i];
8599: }
8600: if ( r0 ) NEXT(r) = 0;
8601:
8602: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8603: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8604: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8605: if ( DP_Print ) {
1.5 noro 8606: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8607: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8608: nsp,nred,sprow,spcol,rank);
1.5 noro 8609: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8610: }
8611: return r0;
8612: }
8613:
8614: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
8615: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
8616: {
8617: int spcol,sprow,a;
8618: int i,j,k,l,rank;
8619: NODE r0,r;
8620: ND_pairs sp;
8621: ND spol;
8622: Z **spmat;
8623: Z *svect,*v;
8624: int *colstat;
8625: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
8626: int maxrs;
8627: int *spsugar;
8628: pointer *w;
8629:
8630: spcol = col-nred;
8631: get_eg(&eg0);
8632: /* elimination (1st step) */
8633: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
8634: svect = (Z *)MALLOC(col*sizeof(Q));
8635: spsugar = (int *)MALLOC(nsp*sizeof(int));
8636: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
8637: nd_sp(0,trace,sp,&spol);
8638: if ( !spol ) continue;
8639: nd_to_vect_q(s0vect,col,spol,svect);
8640: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
8641: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
8642: if ( i < col ) {
8643: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
8644: for ( j = k = 0; j < col; j++ )
8645: if ( !rhead[j] ) v[k++] = svect[j];
8646: spsugar[sprow] = MAX(maxrs,SG(spol));
8647: sprow++;
8648: }
8649: /* nd_free(spol); */
8650: }
8651: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
8652: if ( DP_Print ) {
1.5 noro 8653: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 8654: fflush(asir_out);
8655: }
8656: /* free index arrays */
8657: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
8658:
8659: /* elimination (2nd step) */
8660: colstat = (int *)MALLOC(spcol*sizeof(int));
8661: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
8662: w = (pointer *)MALLOC(rank*sizeof(pointer));
8663: for ( i = 0; i < rank; i++ ) {
8664: #if 0
8665: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8666: SG((NDV)w[rank-i-1]) = spsugar[i];
8667: #else
8668: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
8669: SG((NDV)w[i]) = spsugar[i];
8670: #endif
8671: /* GCFREE(spmat[i]); */
8672: }
8673: #if 0
8674: qsort(w,rank,sizeof(NDV),
8675: (int (*)(const void *,const void *))ndv_compare);
8676: #endif
8677: r0 = 0;
8678: for ( i = 0; i < rank; i++ ) {
8679: NEXTNODE(r0,r); BDY(r) = w[i];
8680: }
8681: if ( r0 ) NEXT(r) = 0;
8682:
8683: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
8684: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
8685: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
8686: if ( DP_Print ) {
1.5 noro 8687: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 8688: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
8689: nsp,nred,sprow,spcol,rank);
1.5 noro 8690: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 8691: }
8692: return r0;
8693: }
8694:
8695: FILE *nd_write,*nd_read;
8696:
8697: void nd_send_int(int a) {
8698: write_int(nd_write,(unsigned int *)&a);
8699: }
8700:
8701: void nd_send_intarray(int *p,int len) {
8702: write_intarray(nd_write,(unsigned int *)p,len);
8703: }
8704:
8705: int nd_recv_int() {
8706: int a;
8707:
8708: read_int(nd_read,(unsigned int *)&a);
8709: return a;
8710: }
8711:
8712: void nd_recv_intarray(int *p,int len) {
8713: read_intarray(nd_read,(unsigned int *)p,len);
8714: }
8715:
8716: void nd_send_ndv(NDV p) {
8717: int len,i;
8718: NMV m;
8719:
8720: if ( !p ) nd_send_int(0);
8721: else {
8722: len = LEN(p);
8723: nd_send_int(len);
8724: m = BDY(p);
8725: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8726: nd_send_int(CM(m));
8727: nd_send_intarray((int *)DL(m),nd_wpd);
8728: }
8729: }
8730: }
8731:
8732: void nd_send_nd(ND p) {
8733: int len,i;
8734: NM m;
8735:
8736: if ( !p ) nd_send_int(0);
8737: else {
8738: len = LEN(p);
8739: nd_send_int(len);
8740: m = BDY(p);
8741: for ( i = 0; i < len; i++, m = NEXT(m) ) {
8742: nd_send_int(CM(m));
8743: nd_send_intarray((int *)DL(m),nd_wpd);
8744: }
8745: }
8746: }
8747:
8748: NDV nd_recv_ndv()
8749: {
8750: int len,i;
8751: NMV m,m0;
8752: NDV r;
8753:
8754: len = nd_recv_int();
8755: if ( !len ) return 0;
8756: else {
8757: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
8758: #if 0
8759: ndv_alloc += len*nmv_adv;
8760: #endif
8761: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8762: CM(m) = nd_recv_int();
8763: nd_recv_intarray((int *)DL(m),nd_wpd);
8764: }
8765: MKNDV(nd_nvar,m0,len,r);
8766: return r;
8767: }
8768: }
8769:
8770: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
8771: {
8772: int i,j,t,c,rank,inv;
8773: int *ci,*ri;
8774: Z dn;
8775: MAT m,nm;
8776:
8777: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
8778: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
8779: for ( i = 0; i < row; i++ )
8780: for ( j = 0; j < col; j++ )
8781: mat0[i][j] = 0;
8782: c = col-rank;
8783: for ( i = 0; i < rank; i++ ) {
8784: mat0[i][ri[i]] = dn;
8785: for ( j = 0; j < c; j++ )
8786: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
8787: }
8788: return rank;
8789: }
8790:
8791: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
8792: {
8793: int i,j,k,l,inv,a,rank,s;
8794: unsigned int *t,*pivot,*pk;
8795: unsigned int **mat;
8796: ND_pairs pair;
8797:
8798: mat = (unsigned int **)mat0;
8799: for ( rank = 0, j = 0; j < col; j++ ) {
8800: for ( i = rank; i < row; i++ )
8801: mat[i][j] %= md;
8802: for ( i = rank; i < row; i++ )
8803: if ( mat[i][j] )
8804: break;
8805: if ( i == row ) {
8806: colstat[j] = 0;
8807: continue;
8808: } else
8809: colstat[j] = 1;
8810: if ( i != rank ) {
8811: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8812: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8813: if ( spactive ) {
8814: pair = spactive[i]; spactive[i] = spactive[rank];
8815: spactive[rank] = pair;
8816: }
8817: }
8818: pivot = mat[rank];
8819: s = sugar[rank];
8820: inv = invm(pivot[j],md);
8821: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8822: if ( *pk ) {
8823: if ( *pk >= (unsigned int)md )
8824: *pk %= md;
8825: DMAR(*pk,inv,0,md,*pk)
8826: }
8827: for ( i = rank+1; i < row; i++ ) {
8828: t = mat[i];
8829: if ( (a = t[j]) != 0 ) {
8830: sugar[i] = MAX(sugar[i],s);
8831: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8832: }
8833: }
8834: rank++;
8835: }
8836: for ( j = col-1, l = rank-1; j >= 0; j-- )
8837: if ( colstat[j] ) {
8838: pivot = mat[l];
8839: s = sugar[l];
8840: for ( i = 0; i < l; i++ ) {
8841: t = mat[i];
8842: t[j] %= md;
8843: if ( (a = t[j]) != 0 ) {
8844: sugar[i] = MAX(sugar[i],s);
8845: red_by_vect(md,t+j,pivot+j,md-a,col-j);
8846: }
8847: }
8848: l--;
8849: }
8850: for ( j = 0, l = 0; l < rank; j++ )
8851: if ( colstat[j] ) {
8852: t = mat[l];
8853: for ( k = j; k < col; k++ )
8854: if ( t[k] >= (unsigned int)md )
8855: t[k] %= md;
8856: l++;
8857: }
8858: return rank;
8859: }
8860:
8861:
1.7 noro 8862: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 8863: {
1.7 noro 8864: int i,j,k,l,inv,a,rank,s;
8865: unsigned int *t,*pivot,*pk;
8866: unsigned int **mat;
8867:
8868: mat = (unsigned int **)mat0;
8869: for ( rank = 0, j = 0; j < col; j++ ) {
8870: for ( i = rank; i < row; i++ )
8871: if ( mat[i][j] )
8872: break;
8873: if ( i == row ) {
8874: colstat[j] = 0;
8875: continue;
8876: } else
8877: colstat[j] = 1;
8878: if ( i != rank ) {
8879: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8880: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8881: }
8882: pivot = mat[rank];
8883: s = sugar[rank];
8884: inv = _invsf(pivot[j]);
8885: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8886: if ( *pk )
8887: *pk = _mulsf(*pk,inv);
8888: for ( i = rank+1; i < row; i++ ) {
8889: t = mat[i];
8890: if ( (a = t[j]) != 0 ) {
8891: sugar[i] = MAX(sugar[i],s);
8892: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8893: }
8894: }
8895: rank++;
8896: }
8897: for ( j = col-1, l = rank-1; j >= 0; j-- )
8898: if ( colstat[j] ) {
8899: pivot = mat[l];
8900: s = sugar[l];
8901: for ( i = 0; i < l; 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: l--;
8909: }
8910: return rank;
8911: }
1.1 noro 8912:
1.7 noro 8913: int ndv_ishomo(NDV p)
8914: {
8915: NMV m;
8916: int len,h;
1.1 noro 8917:
8918: if ( !p ) return 1;
8919: len = LEN(p);
8920: m = BDY(p);
8921: h = TD(DL(m));
8922: NMV_ADV(m);
8923: for ( len--; len; len--, NMV_ADV(m) )
1.20 noro 8924: if ( TD(DL(m)) != h ) {
8925: return 0;
8926: }
1.1 noro 8927: return 1;
8928: }
8929:
8930: void ndv_save(NDV p,int index)
8931: {
8932: FILE *s;
8933: char name[BUFSIZ];
8934: short id;
8935: int nv,sugar,len,n,i,td,e,j;
8936: NMV m;
8937: unsigned int *dl;
8938: int mpos;
8939:
8940: sprintf(name,"%s/%d",Demand,index);
8941: s = fopen(name,"w");
8942: savevl(s,0);
8943: if ( !p ) {
8944: saveobj(s,0);
8945: return;
8946: }
8947: id = O_DP;
8948: nv = NV(p);
8949: sugar = SG(p);
8950: len = LEN(p);
8951: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
8952: write_int(s,(unsigned int *)&len);
8953:
8954: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 8955: saveobj(s,(Obj)CZ(m));
1.1 noro 8956: dl = DL(m);
8957: td = TD(dl);
8958: write_int(s,(unsigned int *)&td);
8959: for ( j = 0; j < nv; j++ ) {
8960: e = GET_EXP(dl,j);
8961: write_int(s,(unsigned int *)&e);
8962: }
8963: if ( nd_module ) {
8964: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
8965: }
8966: }
8967: fclose(s);
8968: }
8969:
8970: void nd_save_mod(ND p,int index)
8971: {
8972: FILE *s;
8973: char name[BUFSIZ];
8974: int nv,sugar,len,c;
8975: NM m;
8976:
8977: sprintf(name,"%s/%d",Demand,index);
8978: s = fopen(name,"w");
8979: if ( !p ) {
8980: len = 0;
8981: write_int(s,(unsigned int *)&len);
8982: fclose(s);
8983: return;
8984: }
8985: nv = NV(p);
8986: sugar = SG(p);
8987: len = LEN(p);
8988: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
8989: for ( m = BDY(p); m; m = NEXT(m) ) {
8990: c = CM(m); write_int(s,(unsigned int *)&c);
8991: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
8992: }
8993: fclose(s);
8994: }
8995:
8996: NDV ndv_load(int index)
8997: {
8998: FILE *s;
8999: char name[BUFSIZ];
9000: short id;
9001: int nv,sugar,len,n,i,td,e,j;
9002: NDV d;
9003: NMV m0,m;
9004: unsigned int *dl;
9005: Obj obj;
9006: int mpos;
9007:
9008: sprintf(name,"%s/%d",Demand,index);
9009: s = fopen(name,"r");
9010: if ( !s ) return 0;
9011:
9012: skipvl(s);
9013: read_short(s,(unsigned short *)&id);
9014: if ( !id ) return 0;
9015: read_int(s,(unsigned int *)&nv);
9016: read_int(s,(unsigned int *)&sugar);
9017: read_int(s,(unsigned int *)&len);
9018:
9019: m0 = m = MALLOC(len*nmv_adv);
9020: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 9021: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 9022: dl = DL(m);
9023: ndl_zero(dl);
9024: read_int(s,(unsigned int *)&td); TD(dl) = td;
9025: for ( j = 0; j < nv; j++ ) {
9026: read_int(s,(unsigned int *)&e);
9027: PUT_EXP(dl,j,e);
9028: }
9029: if ( nd_module ) {
9030: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
9031: }
9032: if ( nd_blockmask ) ndl_weight_mask(dl);
9033: }
9034: fclose(s);
9035: MKNDV(nv,m0,len,d);
9036: SG(d) = sugar;
9037: return d;
9038: }
9039:
9040: ND nd_load_mod(int index)
9041: {
9042: FILE *s;
9043: char name[BUFSIZ];
9044: int nv,sugar,len,i,c;
9045: ND d;
9046: NM m0,m;
9047:
9048: sprintf(name,"%s/%d",Demand,index);
9049: s = fopen(name,"r");
9050: /* if the file does not exist, it means p[index]=0 */
9051: if ( !s ) return 0;
9052:
9053: read_int(s,(unsigned int *)&nv);
9054: if ( !nv ) { fclose(s); return 0; }
9055:
9056: read_int(s,(unsigned int *)&sugar);
9057: read_int(s,(unsigned int *)&len);
9058: for ( m0 = 0, i = 0; i < len; i++ ) {
9059: NEXTNM(m0,m);
9060: read_int(s,(unsigned int *)&c); CM(m) = c;
9061: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
9062: }
9063: NEXT(m) = 0;
9064: MKND(nv,m0,len,d);
9065: SG(d) = sugar;
9066: fclose(s);
9067: return d;
9068: }
9069:
9070: void nd_det(int mod,MAT f,P *rp)
9071: {
9072: VL fv,tv;
9073: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
9074: pointer **m;
9075: P **w;
9076: P mp,r;
9077: NDV **dm;
9078: NDV *t,*mi,*mj;
9079: NDV d,s,mij,mjj;
9080: ND u;
9081: NMV nmv;
9082: UINT *bound;
9083: PGeoBucket bucket;
9084: struct order_spec *ord;
9085: Z dq,dt,ds;
9086: Z mone;
9087: Z gn,qn,dn0,nm,dn;
9088:
9089: create_order_spec(0,0,&ord);
9090: nd_init_ord(ord);
9091: get_vars((Obj)f,&fv);
9092: if ( f->row != f->col )
9093: error("nd_det : non-square matrix");
9094: n = f->row;
9095: m = f->body;
9096: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
9097:
9098: if ( !nvar ) {
9099: if ( !mod )
9100: detp(CO,(P **)m,n,rp);
9101: else {
9102: w = (P **)almat_pointer(n,n);
9103: for ( i = 0; i < n; i++ )
9104: for ( j = 0; j < n; j++ )
9105: ptomp(mod,(P)m[i][j],&w[i][j]);
9106: detmp(CO,mod,w,n,&mp);
9107: mptop(mp,rp);
9108: }
9109: return;
9110: }
9111:
9112: if ( !mod ) {
9113: w = (P **)almat_pointer(n,n);
9114: dq = ONE;
9115: for ( i = 0; i < n; i++ ) {
9116: dn0 = ONE;
9117: for ( j = 0; j < n; j++ ) {
9118: if ( !m[i][j] ) continue;
9119: lgp(m[i][j],&nm,&dn);
1.6 noro 9120: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 9121: }
9122: if ( !UNIZ(dn0) ) {
9123: ds = dn0;
9124: for ( j = 0; j < n; j++ )
9125: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
9126: mulz(dq,ds,&dt); dq = dt;
9127: } else
9128: for ( j = 0; j < n; j++ )
9129: w[i][j] = (P)m[i][j];
9130: }
9131: m = (pointer **)w;
9132: }
9133:
9134: for ( i = 0, max = 1; i < n; i++ )
9135: for ( j = 0; j < n; j++ )
9136: for ( tv = fv; tv; tv = NEXT(tv) ) {
9137: e = getdeg(tv->v,(P)m[i][j]);
9138: max = MAX(e,max);
9139: }
9140: nd_setup_parameters(nvar,max);
9141: dm = (NDV **)almat_pointer(n,n);
9142: for ( i = 0, max = 1; i < n; i++ )
9143: for ( j = 0; j < n; j++ ) {
9144: dm[i][j] = ptondv(CO,fv,m[i][j]);
9145: if ( mod ) ndv_mod(mod,dm[i][j]);
9146: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
9147: }
9148: d = ptondv(CO,fv,(P)ONE);
9149: if ( mod ) ndv_mod(mod,d);
9150: chsgnz(ONE,&mone);
9151: for ( j = 0, sgn = 1; j < n; j++ ) {
9152: if ( DP_Print ) {
9153: fprintf(asir_out,".");
9154: }
9155: for ( i = j; i < n && !dm[i][j]; i++ );
9156: if ( i == n ) {
9157: *rp = 0;
9158: return;
9159: }
9160: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
9161: for ( k = j; k < n; k++ )
9162: for ( l = j; l < n; l++ )
9163: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
9164: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
9165: }
9166: if ( k0 != j ) {
9167: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
9168: sgn = -sgn;
9169: }
9170: if ( l0 != j ) {
9171: for ( k = j; k < n; k++ ) {
9172: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
9173: }
9174: sgn = -sgn;
9175: }
9176: bound = nd_det_compute_bound(dm,n,j);
9177: for ( k = 0; k < nd_nvar; k++ )
9178: if ( bound[k]*2 > nd_mask0 ) break;
9179: if ( k < nd_nvar )
9180: nd_det_reconstruct(dm,n,j,d);
9181:
9182: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
9183: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
9184: mi = dm[i]; mij = mi[j];
9185: if ( mod )
9186: ndv_mul_c(mod,mij,mod-1);
9187: else
9188: ndv_mul_c_q(mij,mone);
9189: for ( k = j+1; k < n; k++ ) {
9190: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
9191: bucket = create_pbucket();
9192: if ( mi[k] ) {
9193: nmv = BDY(mjj); len = LEN(mjj);
9194: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9195: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
9196: add_pbucket(mod,bucket,u);
9197: }
9198: }
9199: if ( mj[k] && mij ) {
9200: nmv = BDY(mij); len = LEN(mij);
9201: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
9202: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
9203: add_pbucket(mod,bucket,u);
9204: }
9205: }
9206: u = nd_quo(mod,bucket,d);
9207: mi[k] = ndtondv(mod,u);
9208: }
9209: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
9210: }
9211: d = mjj;
9212: }
9213: if ( DP_Print ) {
9214: fprintf(asir_out,"\n");
9215: }
9216: if ( sgn < 0 ) {
9217: if ( mod )
9218: ndv_mul_c(mod,d,mod-1);
9219: else
9220: ndv_mul_c_q(d,mone);
9221: }
9222: r = ndvtop(mod,CO,fv,d);
9223: if ( !mod && !UNIQ(dq) )
9224: divsp(CO,r,(P)dq,rp);
9225: else
9226: *rp = r;
9227: }
9228:
9229: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
9230: {
9231: NM mr,mr0;
9232: NM tnm;
9233: NMV m;
9234: UINT *d0,*dt,*dm;
9235: int c,n,td,i,c1,c2,len;
9236: Z q;
9237: ND r;
9238:
9239: if ( !p ) return 0;
9240: else {
9241: n = NV(p); m = BDY(p); len = LEN(p);
9242: d0 = DL(m0);
9243: td = TD(d);
9244: mr0 = 0;
9245: NEWNM(tnm);
9246: if ( mod ) {
9247: c = CM(m0);
9248: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9249: ndl_add(DL(m),d0,DL(tnm));
9250: if ( ndl_reducible(DL(tnm),d) ) {
9251: NEXTNM(mr0,mr);
9252: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
9253: ndl_copy(DL(tnm),DL(mr));
9254: }
9255: }
9256: } else {
1.6 noro 9257: q = CZ(m0);
1.1 noro 9258: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
9259: ndl_add(DL(m),d0,DL(tnm));
9260: if ( ndl_reducible(DL(tnm),d) ) {
9261: NEXTNM(mr0,mr);
1.6 noro 9262: mulz(CZ(m),q,&CZ(mr));
1.1 noro 9263: ndl_copy(DL(tnm),DL(mr));
9264: }
9265: }
9266: }
9267: if ( !mr0 )
9268: return 0;
9269: else {
9270: NEXT(mr) = 0;
9271: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9272: MKND(NV(p),mr0,len,r);
9273: SG(r) = SG(p) + TD(d0);
9274: return r;
9275: }
9276: }
9277: }
9278:
9279: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
9280: {
9281: int i,obpe,oadv,h,k,l;
9282: static NM prev_nm_free_list;
9283: EPOS oepos;
9284:
9285: obpe = nd_bpe;
9286: oadv = nmv_adv;
9287: oepos = nd_epos;
9288: if ( obpe < 2 ) nd_bpe = 2;
9289: else if ( obpe < 3 ) nd_bpe = 3;
9290: else if ( obpe < 4 ) nd_bpe = 4;
9291: else if ( obpe < 5 ) nd_bpe = 5;
9292: else if ( obpe < 6 ) nd_bpe = 6;
9293: else if ( obpe < 8 ) nd_bpe = 8;
9294: else if ( obpe < 10 ) nd_bpe = 10;
9295: else if ( obpe < 16 ) nd_bpe = 16;
9296: else if ( obpe < 32 ) nd_bpe = 32;
9297: else error("nd_det_reconstruct : exponent too large");
9298:
9299: nd_setup_parameters(nd_nvar,0);
9300: prev_nm_free_list = _nm_free_list;
9301: _nm_free_list = 0;
9302: for ( k = j; k < n; k++ )
9303: for (l = j; l < n; l++ )
9304: ndv_realloc(dm[k][l],obpe,oadv,oepos);
9305: ndv_realloc(d,obpe,oadv,oepos);
9306: prev_nm_free_list = 0;
9307: #if 0
9308: GC_gcollect();
9309: #endif
9310: }
9311:
9312: /* returns a UINT array containing degree bounds */
9313:
9314: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
9315: {
9316: UINT *d0,*d1,*d,*t,*r;
9317: int k,l,i;
9318:
9319: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
9320: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
9321: for ( k = j; k < n; k++ )
9322: for ( l = j; l < n; l++ )
9323: if ( dm[k][l] ) {
9324: d = ndv_compute_bound(dm[k][l]);
9325: for ( i = 0; i < nd_nvar; i++ )
9326: d0[i] = MAX(d0[i],d[i]);
9327: }
9328: return d0;
9329: }
9330:
9331: DL nd_separate_d(UINT *d,UINT *trans)
9332: {
9333: int n,td,i,e,j;
9334: DL a;
9335:
9336: ndl_zero(trans);
9337: td = 0;
9338: for ( i = 0; i < nd_ntrans; i++ ) {
9339: e = GET_EXP(d,i);
9340: PUT_EXP(trans,i,e);
9341: td += MUL_WEIGHT(e,i);
9342: }
9343: if ( nd_ntrans+nd_nalg < nd_nvar ) {
9344: /* homogenized */
9345: i = nd_nvar-1;
9346: e = GET_EXP(d,i);
9347: PUT_EXP(trans,i,e);
9348: td += MUL_WEIGHT(e,i);
9349: }
9350: TD(trans) = td;
9351: if ( nd_blockmask) ndl_weight_mask(trans);
9352: NEWDL(a,nd_nalg);
9353: td = 0;
9354: for ( i = 0; i < nd_nalg; i++ ) {
9355: j = nd_ntrans+i;
9356: e = GET_EXP(d,j);
9357: a->d[i] = e;
9358: td += e;
9359: }
9360: a->td = td;
9361: return a;
9362: }
9363:
9364: int nd_monic(int mod,ND *p)
9365: {
9366: UINT *trans,*t;
9367: DL alg;
9368: MP mp0,mp;
9369: NM m,m0,m1,ma0,ma,mb,mr0,mr;
9370: ND r;
9371: DL dl;
9372: DP nm;
9373: NDV ndv;
9374: DAlg inv,cd;
9375: ND s,c;
9376: Z l,mul;
9377: Z ln;
9378: int n,ntrans,i,e,td,is_lc,len;
9379: NumberField nf;
9380: struct oEGT eg0,eg1;
9381:
9382: if ( !(nf = get_numberfield()) )
9383: error("nd_monic : current_numberfield is not set");
9384:
9385: /* Q coef -> DAlg coef */
9386: NEWNM(ma0); ma = ma0;
9387: m = BDY(*p);
9388: is_lc = 1;
9389: while ( 1 ) {
9390: NEWMP(mp0); mp = mp0;
1.6 noro 9391: mp->c = (Obj)CZ(m);
1.1 noro 9392: mp->dl = nd_separate_d(DL(m),DL(ma));
9393: NEWNM(mb);
9394: for ( m = NEXT(m); m; m = NEXT(m) ) {
9395: alg = nd_separate_d(DL(m),DL(mb));
9396: if ( !ndl_equal(DL(ma),DL(mb)) )
9397: break;
1.6 noro 9398: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 9399: }
9400: NEXT(mp) = 0;
9401: MKDP(nd_nalg,mp0,nm);
9402: MKDAlg(nm,ONE,cd);
9403: if ( is_lc == 1 ) {
9404: /* if the lc is a rational number, we have nothing to do */
9405: if ( !mp0->dl->td )
9406: return 1;
9407:
9408: get_eg(&eg0);
9409: invdalg(cd,&inv);
9410: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
9411: /* check the validity of inv */
9412: if ( mod && !remqi((Q)inv->dn,mod) )
9413: return 0;
9414: CA(ma) = nf->one;
9415: is_lc = 0;
9416: ln = ONE;
9417: } else {
9418: muldalg(cd,inv,&CA(ma));
9419: lcmz(ln,CA(ma)->dn,&ln);
9420: }
9421: if ( m ) {
9422: NEXT(ma) = mb; ma = mb;
9423: } else {
9424: NEXT(ma) = 0;
9425: break;
9426: }
9427: }
9428: /* l = lcm(denoms) */
9429: l = ln;
9430: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 9431: divsz(l,CA(m)->dn,&mul);
1.1 noro 9432: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
9433: NEXTNM(mr0,mr);
1.6 noro 9434: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 9435: dl = mp->dl;
9436: td = TD(DL(m));
9437: ndl_copy(DL(m),DL(mr));
9438: for ( i = 0; i < nd_nalg; i++ ) {
9439: e = dl->d[i];
9440: PUT_EXP(DL(mr),i+nd_ntrans,e);
9441: td += MUL_WEIGHT(e,i+nd_ntrans);
9442: }
9443: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
9444: TD(DL(mr)) = td;
9445: if ( nd_blockmask) ndl_weight_mask(DL(mr));
9446: }
9447: }
9448: NEXT(mr) = 0;
9449: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
9450: MKND(NV(*p),mr0,len,r);
9451: /* XXX */
9452: SG(r) = SG(*p);
9453: nd_free(*p);
9454: *p = r;
9455: return 1;
9456: }
9457:
9458: NODE reverse_node(NODE n)
9459: {
9460: NODE t,t1;
9461:
9462: for ( t = 0; n; n = NEXT(n) ) {
9463: MKNODE(t1,BDY(n),t); t = t1;
9464: }
9465: return t;
9466: }
9467:
9468: P ndc_div(int mod,union oNDC a,union oNDC b)
9469: {
9470: union oNDC c;
9471: int inv,t;
9472:
9473: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 9474: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 9475: else if ( mod ) {
9476: inv = invm(b.m,mod);
9477: DMAR(a.m,inv,0,mod,t); c.m = t;
9478: } else if ( nd_vc )
9479: divsp(nd_vc,a.p,b.p,&c.p);
9480: else
9481: divsz(a.z,b.z,&c.z);
9482: return ndctop(mod,c);
9483: }
9484:
9485: P ndctop(int mod,union oNDC c)
9486: {
9487: Z q;
9488: int e;
9489: GFS gfs;
9490:
9491: if ( mod == -1 ) {
9492: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
9493: } else if ( mod == -2 ) {
1.10 noro 9494: q = c.z; return (P)q;
1.1 noro 9495: } else if ( mod > 0 ) {
1.6 noro 9496: STOZ(c.m,q); return (P)q;
1.1 noro 9497: } else
9498: return (P)c.p;
9499: }
9500:
9501: /* [0,0,0,cont] = p -> p/cont */
9502:
9503: void finalize_tracelist(int i,P cont)
9504: {
9505: LIST l;
9506: NODE node;
9507: Z iq;
9508:
9509: if ( !UNIQ(cont) ) {
9510: node = mknode(4,NULLP,NULLP,NULLP,cont);
9511: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
9512: nd_tracelist = node;
9513: }
1.6 noro 9514: STOZ(i,iq);
1.1 noro 9515: nd_tracelist = reverse_node(nd_tracelist);
9516: MKLIST(l,nd_tracelist);
9517: node = mknode(2,iq,l); MKLIST(l,node);
9518: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
9519: nd_alltracelist = node; nd_tracelist = 0;
9520: }
9521:
9522: void conv_ilist(int demand,int trace,NODE g,int **indp)
9523: {
9524: int n,i,j;
9525: int *ind;
9526: NODE t;
9527:
9528: n = length(g);
9529: ind = (int *)MALLOC(n*sizeof(int));
9530: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
9531: j = (long)BDY(t); ind[i] = j;
9532: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
9533: }
9534: if ( indp ) *indp = ind;
9535: }
9536:
1.30 ! noro 9537: NODE conv_ilist_s(int demand,int trace,int **indp)
! 9538: {
! 9539: int n,i,j;
! 9540: int *ind;
! 9541: NODE g0,g;
! 9542:
! 9543: n = nd_psn;
! 9544: ind = (int *)MALLOC(n*sizeof(int));
! 9545: g0 = 0;
! 9546: for ( i = 0; i < n; i++ ) {
! 9547: ind[i] = i;
! 9548: NEXTNODE(g0,g);
! 9549: BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
! 9550: }
! 9551: if ( g0 ) NEXT(g) = 0;
! 9552: if ( indp ) *indp = ind;
! 9553: return g0;
! 9554: }
! 9555:
1.1 noro 9556: void parse_nd_option(NODE opt)
9557: {
9558: NODE t,p,u;
9559: int i,s,n;
9560: char *key;
9561: Obj value;
9562:
9563: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
9564: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
9565: nd_splist = 0; nd_check_splist = 0;
9566: nd_sugarweight = 0;
9567: nd_f4red =0;
9568: nd_rank0 = 0;
9569: for ( t = opt; t; t = NEXT(t) ) {
9570: p = BDY((LIST)BDY(t));
9571: key = BDY((STRING)BDY(p));
9572: value = (Obj)BDY(NEXT(p));
9573: if ( !strcmp(key,"gentrace") )
9574: nd_gentrace = value?1:0;
9575: else if ( !strcmp(key,"gensyz") )
9576: nd_gensyz = value?1:0;
9577: else if ( !strcmp(key,"nora") )
9578: nd_nora = value?1:0;
9579: else if ( !strcmp(key,"gbblock") ) {
9580: if ( value && OID(value) == O_LIST ) {
9581: u = BDY((LIST)value);
9582: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
9583: for ( i = 0; u; u = NEXT(u) ) {
9584: p = BDY((LIST)BDY(u));
1.6 noro 9585: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
9586: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 9587: }
9588: nd_gbblock[i] = -1;
9589: } else
9590: nd_gbblock = 0;
9591: } else if ( !strcmp(key,"newelim") )
9592: nd_newelim = value?1:0;
9593: else if ( !strcmp(key,"intersect") )
9594: nd_intersect = value?1:0;
1.17 noro 9595: else if ( !strcmp(key,"syzgen") )
9596: nd_intersect = ZTOS((Q)value);
1.1 noro 9597: else if ( !strcmp(key,"lf") )
9598: nd_lf = value?1:0;
9599: else if ( !strcmp(key,"trace") ) {
9600: if ( value ) {
9601: u = BDY((LIST)value);
9602: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 9603: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 9604: }
9605: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 9606: nd_f4red = ZTOS((Q)value);
1.1 noro 9607: } else if ( !strcmp(key,"rank0") ) {
9608: nd_rank0 = value?1:0;
9609: } else if ( !strcmp(key,"splist") ) {
9610: nd_splist = value?1:0;
9611: } else if ( !strcmp(key,"check_splist") ) {
9612: nd_check_splist = BDY((LIST)value);
9613: } else if ( !strcmp(key,"sugarweight") ) {
9614: u = BDY((LIST)value);
9615: n = length(u);
9616: nd_sugarweight = MALLOC(n*sizeof(int));
9617: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 9618: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 9619: }
9620: }
9621: }
9622:
9623: ND mdptond(DP d);
9624: ND nd_mul_nm(int mod,NM m0,ND p);
9625: ND nd_mul_nm_lf(NM m0,ND p);
9626: ND *btog(NODE ti,ND **p,int nb,int mod);
9627: ND btog_one(NODE ti,ND *p,int nb,int mod);
9628: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
9629: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
9630:
9631: /* d:monomial */
9632: ND mdptond(DP d)
9633: {
9634: NM m;
9635: ND r;
9636:
9637: if ( OID(d) == 1 )
9638: r = ptond(CO,CO,(P)d);
9639: else {
9640: NEWNM(m);
9641: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 9642: CZ(m) = (Z)BDY(d)->c;
1.1 noro 9643: NEXT(m) = 0;
9644: MKND(NV(d),m,1,r);
9645: }
9646: return r;
9647: }
9648:
9649: ND nd_mul_nm(int mod,NM m0,ND p)
9650: {
9651: UINT *d0;
9652: int c0,c1,c;
9653: NM tm,mr,mr0;
9654: ND r;
9655:
9656: if ( !p ) return 0;
9657: d0 = DL(m0);
9658: c0 = CM(m0);
9659: mr0 = 0;
9660: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9661: NEXTNM(mr0,mr);
9662: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
9663: ndl_add(d0,DL(tm),DL(mr));
9664: }
9665: NEXT(mr) = 0;
9666: MKND(NV(p),mr0,LEN(p),r);
9667: return r;
9668: }
9669:
9670: ND nd_mul_nm_lf(NM m0,ND p)
9671: {
9672: UINT *d0;
9673: Z c0,c1,c;
9674: NM tm,mr,mr0;
9675: ND r;
9676:
9677: if ( !p ) return 0;
9678: d0 = DL(m0);
9679: c0 = CZ(m0);
9680: mr0 = 0;
9681: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
9682: NEXTNM(mr0,mr);
9683: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
9684: ndl_add(d0,DL(tm),DL(mr));
9685: }
9686: NEXT(mr) = 0;
9687: MKND(NV(p),mr0,LEN(p),r);
9688: return r;
9689: }
9690:
9691: ND *btog(NODE ti,ND **p,int nb,int mod)
9692: {
9693: PGeoBucket *r;
9694: int i,ci;
9695: NODE t,s;
9696: ND m,tp;
9697: ND *pi,*rd;
9698: P c;
9699:
9700: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9701: for ( i = 0; i < nb; i++ )
9702: r[i] = create_pbucket();
9703: for ( t = ti; t; t = NEXT(t) ) {
9704: s = BDY((LIST)BDY(t));
9705: if ( ARG0(s) ) {
9706: m = mdptond((DP)ARG2(s));
1.6 noro 9707: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9708: if ( (ci = ((MQ)c)->cont) != 0 ) {
9709: HCM(m) = ci;
1.6 noro 9710: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9711: for ( i = 0; i < nb; i++ ) {
9712: tp = nd_mul_nm(mod,BDY(m),pi[i]);
9713: add_pbucket(mod,r[i],tp);
9714: }
9715: }
9716: ci = 1;
9717: } else {
9718: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9719: ci = invm(ci,mod);
9720: }
9721: }
9722: rd = (ND *)MALLOC(nb*sizeof(ND));
9723: for ( i = 0; i < nb; i++ )
9724: rd[i] = normalize_pbucket(mod,r[i]);
9725: if ( ci != 1 )
9726: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
9727: return rd;
9728: }
9729:
9730: /* YYY */
9731: ND *btog_lf(NODE ti,ND **p,int nb)
9732: {
9733: PGeoBucket *r;
9734: int i;
9735: NODE t,s;
9736: ND m,tp;
9737: ND *pi,*rd;
9738: LM lm;
9739: Z lf,c;
9740:
9741: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
9742: for ( i = 0; i < nb; i++ )
9743: r[i] = create_pbucket();
9744: for ( t = ti; t; t = NEXT(t) ) {
9745: s = BDY((LIST)BDY(t));
9746: if ( ARG0(s) ) {
9747: m = mdptond((DP)ARG2(s));
1.6 noro 9748: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 9749: if ( lm ) {
9750: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 9751: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 9752: for ( i = 0; i < nb; i++ ) {
9753: tp = nd_mul_nm_lf(BDY(m),pi[i]);
9754: add_pbucket(-2,r[i],tp);
9755: }
9756: }
9757: c = ONE;
9758: } else {
9759: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
9760: }
9761: }
9762: rd = (ND *)MALLOC(nb*sizeof(ND));
9763: for ( i = 0; i < nb; i++ )
9764: rd[i] = normalize_pbucket(-2,r[i]);
9765: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
9766: return rd;
9767: }
9768:
9769: ND btog_one(NODE ti,ND *p,int nb,int mod)
9770: {
9771: PGeoBucket r;
9772: int i,ci,j;
9773: NODE t,s;
9774: ND m,tp;
9775: ND pi,rd;
9776: P c;
9777:
9778: r = create_pbucket();
9779: for ( t = ti; t; t = NEXT(t) ) {
9780: s = BDY((LIST)BDY(t));
9781: if ( ARG0(s) ) {
9782: m = mdptond((DP)ARG2(s));
1.6 noro 9783: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 9784: if ( (ci = ((MQ)c)->cont) != 0 ) {
9785: HCM(m) = ci;
1.6 noro 9786: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 9787: if ( !pi ) {
9788: pi = nd_load_mod(j);
9789: tp = nd_mul_nm(mod,BDY(m),pi);
9790: nd_free(pi);
9791: add_pbucket(mod,r,tp);
9792: } else {
9793: tp = nd_mul_nm(mod,BDY(m),pi);
9794: add_pbucket(mod,r,tp);
9795: }
9796: }
9797: ci = 1;
9798: } else {
9799: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
9800: ci = invm(ci,mod);
9801: }
9802: }
9803: rd = normalize_pbucket(mod,r);
9804: free_pbucket(r);
9805: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
9806: return rd;
9807: }
9808:
9809: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
9810:
9811: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
9812: {
9813: int i,j,n,m,nb,pi0,pi1,nvar;
9814: VL fv,tv,vv;
9815: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9816: ND **p;
9817: ND *c;
9818: ND u;
9819: P inv;
9820: MAT mat;
9821:
9822: if ( mod == -2 )
9823: return nd_btog_lf(f,v,ord,tlist,rp);
9824:
9825: parse_nd_option(current_option);
9826: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9827: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9828: switch ( ord->id ) {
9829: case 1:
9830: if ( ord->nv != nvar )
9831: error("nd_check : invalid order specification");
9832: break;
9833: default:
9834: break;
9835: }
9836: nd_init_ord(ord);
9837: #if 0
1.6 noro 9838: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9839: #else
9840: nd_bpe = 32;
9841: #endif
9842: nd_setup_parameters(nvar,0);
9843: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9844: intred = BDY((LIST)ARG3(BDY(tlist)));
9845: ind = BDY((LIST)ARG4(BDY(tlist)));
9846: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9847: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9848: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9849: if ( j > i ) i = j;
9850: }
9851: n = i+1;
9852: nb = length(BDY(f));
9853: p = (ND **)MALLOC(n*sizeof(ND *));
9854: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9855: pi = BDY((LIST)BDY(t));
1.6 noro 9856: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9857: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9858: ptomp(mod,(P)ARG2(pi),&inv);
9859: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9860: u = ptond(CO,vv,(P)ONE);
9861: HCM(u) = ((MQ)inv)->cont;
9862: c[pi1] = u;
9863: }
9864: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9865: printf("%d ",i); fflush(stdout);
9866: ti = BDY((LIST)BDY(t));
1.6 noro 9867: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9868: }
9869: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9870: printf("%d ",i); fflush(stdout);
9871: ti = BDY((LIST)BDY(t));
1.6 noro 9872: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 9873: }
9874: m = length(ind);
9875: MKMAT(mat,nb,m);
9876: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9877: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9878: BDY(mat)[i][j] = ndtodp(mod,c[i]);
9879: return mat;
9880: }
9881:
9882: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
9883: {
9884: int i,j,n,m,nb,pi0,pi1,nvar;
9885: VL fv,tv,vv;
9886: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9887: ND **p;
9888: ND *c;
9889: ND u;
9890: MAT mat;
9891: LM lm;
9892: Z lf,inv;
9893:
9894: parse_nd_option(current_option);
9895: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9896: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9897: switch ( ord->id ) {
9898: case 1:
9899: if ( ord->nv != nvar )
9900: error("nd_check : invalid order specification");
9901: break;
9902: default:
9903: break;
9904: }
9905: nd_init_ord(ord);
9906: #if 0
1.6 noro 9907: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9908: #else
9909: nd_bpe = 32;
9910: #endif
9911: nd_setup_parameters(nvar,0);
9912: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9913: intred = BDY((LIST)ARG3(BDY(tlist)));
9914: ind = BDY((LIST)ARG4(BDY(tlist)));
9915: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9916: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9917: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9918: if ( j > i ) i = j;
9919: }
9920: n = i+1;
9921: nb = length(BDY(f));
9922: p = (ND **)MALLOC(n*sizeof(ND *));
9923: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9924: pi = BDY((LIST)BDY(t));
1.6 noro 9925: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9926: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
9927: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
9928: u = ptond(CO,vv,(P)ONE);
9929: HCZ(u) = inv;
9930: c[pi1] = u;
9931: }
9932: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
9933: printf("%d ",i); fflush(stdout);
9934: ti = BDY((LIST)BDY(t));
1.6 noro 9935: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9936: }
9937: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
9938: printf("%d ",i); fflush(stdout);
9939: ti = BDY((LIST)BDY(t));
1.6 noro 9940: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 9941: }
9942: m = length(ind);
9943: MKMAT(mat,nb,m);
9944: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 9945: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 9946: BDY(mat)[i][j] = ndtodp(-2,c[i]);
9947: return mat;
9948: }
9949:
9950: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
9951: LIST tlist,int pos,MAT *rp)
9952: {
9953: int i,j,n,m,nb,pi0,pi1,nvar;
9954: VL fv,tv,vv;
9955: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9956: ND *p;
9957: ND *c;
9958: ND u;
9959: P inv;
9960: VECT vect;
9961:
9962: if ( mod == -2 )
9963: error("nd_btog_one : not implemented yet for a large finite field");
9964:
9965: parse_nd_option(current_option);
9966: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9967: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9968: switch ( ord->id ) {
9969: case 1:
9970: if ( ord->nv != nvar )
9971: error("nd_check : invalid order specification");
9972: break;
9973: default:
9974: break;
9975: }
9976: nd_init_ord(ord);
9977: #if 0
1.6 noro 9978: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 9979: #else
9980: nd_bpe = 32;
9981: #endif
9982: nd_setup_parameters(nvar,0);
9983: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9984: intred = BDY((LIST)ARG3(BDY(tlist)));
9985: ind = BDY((LIST)ARG4(BDY(tlist)));
9986: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9987: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 9988: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 9989: if ( j > i ) i = j;
9990: }
9991: n = i+1;
9992: nb = length(BDY(f));
9993: p = (ND *)MALLOC(n*sizeof(ND *));
9994: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9995: pi = BDY((LIST)BDY(t));
1.6 noro 9996: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 9997: if ( pi1 == pos ) {
9998: ptomp(mod,(P)ARG2(pi),&inv);
9999: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
10000: u = ptond(CO,vv,(P)ONE);
10001: HCM(u) = ((MQ)inv)->cont;
10002: p[pi0] = u;
10003: }
10004: }
10005: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
10006: printf("%d ",i); fflush(stdout);
10007: ti = BDY((LIST)BDY(t));
1.6 noro 10008: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10009: if ( Demand ) {
10010: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10011: }
10012: }
10013: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
10014: printf("%d ",i); fflush(stdout);
10015: ti = BDY((LIST)BDY(t));
1.6 noro 10016: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 10017: if ( Demand ) {
10018: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
10019: }
10020: }
10021: m = length(ind);
10022: MKVECT(vect,m);
10023: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 10024: u = p[ZTOS((Q)BDY(t))];
1.1 noro 10025: if ( !u ) {
1.6 noro 10026: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 10027: BDY(vect)[j] = ndtodp(mod,u);
10028: nd_free(u);
10029: } else
10030: BDY(vect)[j] = ndtodp(mod,u);
10031: }
10032: return vect;
10033: }
10034:
10035: void ndv_print_lf(NDV p)
10036: {
10037: NMV m;
10038: int i,len;
10039:
10040: if ( !p ) printf("0\n");
10041: else {
10042: len = LEN(p);
10043: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
10044: printf("+");
10045: mpz_out_str(asir_out,10,BDY(CZ(m)));
10046: printf("*");
10047: ndl_print(DL(m));
10048: }
10049: printf("\n");
10050: }
10051: }
10052:
10053: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
10054: {
10055: VL tv,fv,vv,vc,av;
10056: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
10057: int m,nocheck,nvar,mindex,e,max;
10058: NDV c;
10059: NMV a;
10060: P p,zp;
10061: Q dmy;
10062: EPOS oepos;
10063: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
10064: Alg alpha,dp;
10065: P poly;
10066: LIST f1,f2,zpl;
10067: Obj obj;
10068: NumberField nf;
10069: struct order_spec *ord1;
10070: struct oEGT eg_check,eg0,eg1;
10071: NODE tr,tl1,tl2,tl3,tl4;
10072: LIST l1,l2,l3,l4,l5;
10073: int *perm;
10074: int j,ret;
10075: NODE retn;
10076: Q jq,bpe;
10077:
10078: nd_module = 0;
10079: parse_nd_option(current_option);
10080: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
10081: if ( nd_vc )
10082: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
10083: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
10084: switch ( ord->id ) {
10085: case 1:
10086: if ( ord->nv != nvar )
10087: error("nd_f4_lf_trace : invalid order specification");
10088: break;
10089: default:
10090: break;
10091: }
10092:
10093: nd_ntrans = nvar;
10094: nd_nalg = 0;
10095:
10096: nocheck = 0;
10097: mindex = 0;
10098:
10099: /* do not use on-demand load/save */
10100: nd_demand = 0;
10101: m = trace > 1 ? trace : get_lprime(mindex);
10102: nd_init_ord(ord);
10103: mrank = 0;
10104: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
10105: for ( tv = vv; tv; tv = NEXT(tv) ) {
10106: if ( nd_module ) {
10107: s = BDY((LIST)BDY(t));
10108: trank = length(s);
10109: mrank = MAX(mrank,trank);
10110: for ( ; s; s = NEXT(s) ) {
10111: e = getdeg(tv->v,(P)BDY(s));
10112: max = MAX(e,max);
10113: }
10114: } else {
10115: e = getdeg(tv->v,(P)BDY(t));
10116: max = MAX(e,max);
10117: }
10118: }
10119: nd_setup_parameters(nvar,max);
10120: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
10121: ishomo = 1;
10122: /* XXX */
10123: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
10124: if ( nd_module ) {
10125: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
10126: } else {
10127: c = (pointer)ptondv(CO,vv,(P)BDY(t));
10128: }
10129: if ( ishomo )
10130: ishomo = ishomo && ndv_ishomo(c);
10131: if ( c ) {
10132: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
10133: ndv_mod(-2,c);
10134: NEXTNODE(in0,in); BDY(in) = (pointer)c;
10135: }
10136: }
10137: if ( in0 ) NEXT(in) = 0;
10138: if ( fd0 ) NEXT(fd) = 0;
10139: if ( !ishomo && homo ) {
10140: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
10141: c = (NDV)BDY(t); len = LEN(c);
10142: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
10143: wmax = MAX(TD(DL(a)),wmax);
10144: }
10145: homogenize_order(ord,nvar,&ord1);
10146: nd_init_ord(ord1);
10147: nd_setup_parameters(nvar+1,wmax);
10148: for ( t = fd0; t; t = NEXT(t) )
10149: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
10150: }
10151: if ( MaxDeg > 0 ) nocheck = 1;
1.24 noro 10152: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1 noro 10153: if ( ret )
10154: cand = nd_f4_lf_trace_main(m,&perm);
10155: if ( !ret || !cand ) {
10156: *rp = 0; return;
10157: }
10158: if ( !ishomo && homo ) {
10159: /* dehomogenization */
10160: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
10161: nd_init_ord(ord);
10162: nd_setup_parameters(nvar,0);
10163: }
10164: cand = ndv_reducebase(cand,perm);
10165: cand = ndv_reduceall(-2,cand);
10166: cbpe = nd_bpe;
10167: get_eg(&eg0);
10168: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
10169: /* gbcheck : cand is a GB of Id(cand) ? */
10170: retn = nd_f4(-2,0,0);
10171: }
10172: if ( !retn ) {
10173: /* failure */
10174: *rp = 0; return;
10175: }
10176: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
10177: if ( DP_Print )
1.5 noro 10178: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 10179: /* dp->p */
10180: nd_bpe = cbpe;
10181: nd_setup_parameters(nd_nvar,0);
10182: for ( r = cand; r; r = NEXT(r) ) {
10183: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
10184: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
10185: }
10186: MKLIST(*rp,cand);
10187: }
10188:
10189: NODE nd_f4_lf_trace_main(int m,int **indp)
10190: {
10191: int i,nh,stat,index;
10192: NODE r,rm,g;
10193: ND_pairs d,l,l0,t;
10194: ND spol,red;
10195: NDV nf,redv,nfqv,nfv;
10196: NM s0,s;
10197: NODE rp0,srp0,nflist,nflist_lf;
10198: int nsp,nred,col,rank,len,k,j,a;
10199: UINT c;
10200: UINT **spmat;
10201: UINT *s0vect,*svect,*p,*v;
10202: int *colstat;
10203: IndArray *imat;
10204: int *rhead;
10205: int spcol,sprow;
10206: int sugar;
10207: PGeoBucket bucket;
10208: struct oEGT eg0,eg1,eg_f4;
10209:
10210: g = 0; d = 0;
10211: for ( i = 0; i < nd_psn; i++ ) {
10212: d = update_pairs(d,g,i,0);
10213: g = update_base(g,i);
10214: }
10215: while ( d ) {
10216: get_eg(&eg0);
10217: l = nd_minsugarp(d,&d);
10218: sugar = SG(l);
10219: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10220: bucket = create_pbucket();
10221: stat = nd_sp_f4(m,0,l,bucket);
10222: if ( !stat ) {
10223: for ( t = l; NEXT(t); t = NEXT(t) );
10224: NEXT(t) = d; d = l;
10225: d = nd_reconstruct(1,d);
10226: continue;
10227: }
10228: if ( bucket->m < 0 ) continue;
10229: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
10230: if ( !col ) {
10231: for ( t = l; NEXT(t); t = NEXT(t) );
10232: NEXT(t) = d; d = l;
10233: d = nd_reconstruct(1,d);
10234: continue;
10235: }
10236: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
10237: if ( DP_Print )
1.5 noro 10238: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 10239: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
10240: if ( !l0 ) continue;
10241: l = l0;
10242:
10243: /* over LF */
10244: bucket = create_pbucket();
10245: stat = nd_sp_f4(-2,1,l,bucket);
10246: if ( !stat ) {
10247: for ( t = l; NEXT(t); t = NEXT(t) );
10248: NEXT(t) = d; d = l;
10249: d = nd_reconstruct(1,d);
10250: continue;
10251: }
10252: if ( bucket->m < 0 ) continue;
10253: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
10254: if ( !col ) {
10255: for ( t = l; NEXT(t); t = NEXT(t) );
10256: NEXT(t) = d; d = l;
10257: d = nd_reconstruct(1,d);
10258: continue;
10259: }
10260: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
10261: /* adding new bases */
10262: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
10263: nfv = (NDV)BDY(rm);
10264: nfqv = (NDV)BDY(r);
10265: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
10266: ndv_removecont(m,nfv);
10267: ndv_removecont(-2,nfqv);
1.24 noro 10268: nh = ndv_newps(-2,nfv,nfqv);
1.1 noro 10269: d = update_pairs(d,g,nh,0);
10270: g = update_base(g,nh);
10271: }
10272: if ( r || rm ) return 0;
10273: }
10274: conv_ilist(nd_demand,1,g,indp);
10275: return g;
10276: }
10277:
1.7 noro 10278: #if SIZEOF_LONG==8
10279:
10280: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
10281: {
10282: int j,k,len;
10283: UINT *p;
10284: UINT c;
10285: NDV r;
10286: NMV mr0,mr;
10287:
10288: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
10289: if ( !len ) return 0;
10290: else {
10291: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10292: #if 0
10293: ndv_alloc += nmv_adv*len;
10294: #endif
10295: mr = mr0;
10296: p = s0vect;
10297: for ( j = k = 0; j < col; j++, p += nd_wpd )
10298: if ( !rhead[j] ) {
10299: if ( (c = (UINT)vect[k++]) != 0 ) {
10300: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10301: }
10302: }
10303: MKNDV(nd_nvar,mr0,len,r);
10304: return r;
10305: }
10306: }
10307:
1.28 noro 10308: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
10309: {
10310: int j,k,len;
10311: UINT *p;
10312: UINT c;
10313: NDV r;
10314: NMV mr0,mr;
10315:
10316: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
10317: if ( !len ) return 0;
10318: else {
10319: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
10320: mr = mr0;
10321: p = s0vect;
10322: for ( j = k = 0; j < col; j++, p += nd_wpd )
10323: if ( (c = (UINT)vect[k++]) != 0 ) {
10324: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
10325: }
10326: MKNDV(nd_nvar,mr0,len,r);
10327: return r;
10328: }
10329: }
10330:
1.7 noro 10331: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
10332: {
10333: NM m;
1.11 noro 10334: UINT *t,*s,*u;
10335: int i,st,ed,md,prev,c;
1.7 noro 10336:
10337: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 10338: prev = 0;
10339: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
10340: t = DL(m);
10341: st = prev;
10342: ed = n;
10343: while ( ed > st ) {
10344: md = (st+ed)/2;
10345: u = s0+md*nd_wpd;
10346: c = DL_COMPARE(u,t);
10347: if ( c == 0 ) break;
10348: else if ( c > 0 ) st = md;
10349: else ed = md;
10350: }
10351: r[md] = (mp_limb_t)CM(m);
10352: prev = md;
1.7 noro 10353: }
10354: for ( i = 0; !r[i]; i++ );
10355: return i;
10356: }
10357:
10358: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
10359:
1.28 noro 10360: 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 10361: {
10362: int i,j,k,len,pos,prev;
10363: mp_limb_t a,c,c1,c2;
10364: IndArray ivect;
10365: unsigned char *ivc;
10366: unsigned short *ivs;
10367: unsigned int *ivi;
10368: NDV redv;
10369: NMV mr;
10370: NODE rp;
10371: int maxrs;
10372:
10373: for ( i = 0; i < col; i++ ) cvect[i] = 0;
10374: maxrs = 0;
10375: for ( i = 0; i < nred; i++ ) {
10376: ivect = imat[i];
10377: k = ivect->head;
10378: a = svect[k]; c = cvect[k];
10379: MOD128(a,c,m);
10380: svect[k] = a; cvect[k] = 0;
1.28 noro 10381: if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11 noro 10382: Nf4_red++;
1.7 noro 10383: maxrs = MAX(maxrs,rp0[i]->sugar);
10384: c = m-c; redv = nd_ps[rp0[i]->index];
10385: len = LEN(redv); mr = BDY(redv);
10386: svect[k] = 0; prev = k;
10387: switch ( ivect->width ) {
10388: case 1:
10389: ivc = ivect->index.c;
10390: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10391: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 10392: c2 = svect[pos]+c1*c;
10393: if ( c2 < svect[pos] ) cvect[pos]++;
10394: svect[pos] = c2;
1.7 noro 10395: }
10396: break;
10397: case 2:
10398: ivs = ivect->index.s;
10399: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10400: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 10401: c2 = svect[pos]+c1*c;
10402: if ( c2 < svect[pos] ) cvect[pos]++;
10403: svect[pos] = c2;
1.7 noro 10404: }
10405: break;
10406: case 4:
10407: ivi = ivect->index.i;
10408: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
10409: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 10410: c2 = svect[pos]+c1*c;
10411: if ( c2 < svect[pos] ) cvect[pos]++;
10412: svect[pos] = c2;
1.7 noro 10413: }
10414: break;
10415: }
10416: }
10417: }
10418: for ( i = 0; i < col; i++ ) {
10419: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
10420: }
10421: return maxrs;
10422: }
10423:
10424: /* for Fp, 2^15=<p<2^29 */
10425:
10426: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10427: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
10428: {
10429: int spcol,sprow,a;
10430: int i,j,k,l,rank;
10431: NODE r0,r;
10432: ND_pairs sp;
10433: ND spol;
10434: mp_limb_t **spmat;
10435: mp_limb_t *svect,*cvect;
10436: mp_limb_t *v;
10437: int *colstat;
10438: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10439: int maxrs;
10440: int *spsugar;
10441: ND_pairs *spactive;
10442:
10443: spcol = col-nred;
10444: get_eg(&eg0);
10445: /* elimination (1st step) */
10446: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10447: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10448: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10449: spsugar = (int *)MALLOC(nsp*sizeof(int));
10450: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
10451: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10452: nd_sp(m,0,sp,&spol);
10453: if ( !spol ) continue;
10454: nd_to_vect64(m,s0vect,col,spol,svect);
1.28 noro 10455: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7 noro 10456: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10457: if ( i < col ) {
10458: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
10459: for ( j = k = 0; j < col; j++ )
10460: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
10461: spsugar[sprow] = MAX(maxrs,SG(spol));
10462: if ( nz )
10463: spactive[sprow] = sp;
10464: sprow++;
10465: }
10466: nd_free(spol);
10467: }
1.12 noro 10468: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 10469: if ( DP_Print ) {
10470: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10471: fflush(asir_out);
10472: }
10473: /* free index arrays */
10474: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10475:
10476: /* elimination (2nd step) */
10477: colstat = (int *)MALLOC(spcol*sizeof(int));
10478: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
10479: r0 = 0;
10480: for ( i = 0; i < rank; i++ ) {
10481: NEXTNODE(r0,r); BDY(r) =
10482: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
10483: SG((NDV)BDY(r)) = spsugar[i];
10484: GCFREE(spmat[i]);
10485: }
10486: if ( r0 ) NEXT(r) = 0;
10487:
10488: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 10489: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 10490: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10491: if ( DP_Print ) {
10492: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10493: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10494: nsp,nred,sprow,spcol,rank);
10495: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10496: }
10497: if ( nz ) {
10498: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
10499: if ( rank > 0 ) {
10500: NEXT(spactive[rank-1]) = 0;
10501: *nz = spactive[0];
10502: } else
10503: *nz = 0;
10504: }
10505: return r0;
10506: }
10507:
10508: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
10509: {
10510: int i,j,k,l,rank,s;
10511: mp_limb_t inv;
10512: mp_limb_t a;
10513: UINT c;
10514: mp_limb_t *t,*pivot,*pk;
10515: UINT *ck;
10516: UINT **cmat;
10517: UINT *ct;
10518: ND_pairs pair;
10519:
10520: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10521: for ( i = 0; i < row; i++ ) {
10522: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10523: bzero(cmat[i],col*sizeof(UINT));
10524: }
10525:
10526: for ( rank = 0, j = 0; j < col; j++ ) {
10527: for ( i = rank; i < row; i++ ) {
10528: a = mat[i][j]; c = cmat[i][j];
10529: MOD128(a,c,md);
10530: mat[i][j] = a; cmat[i][j] = 0;
10531: }
10532: for ( i = rank; i < row; i++ )
10533: if ( mat[i][j] )
10534: break;
10535: if ( i == row ) {
10536: colstat[j] = 0;
10537: continue;
10538: } else
10539: colstat[j] = 1;
10540: if ( i != rank ) {
10541: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
10542: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
10543: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
10544: if ( spactive ) {
10545: pair = spactive[i]; spactive[i] = spactive[rank];
10546: spactive[rank] = pair;
10547: }
10548: }
10549: /* column j is normalized */
10550: s = sugar[rank];
10551: inv = invm((UINT)mat[rank][j],md);
10552: /* normalize pivot row */
10553: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10554: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10555: }
10556: for ( i = rank+1; i < row; i++ ) {
10557: if ( (a = mat[i][j]) != 0 ) {
10558: sugar[i] = MAX(sugar[i],s);
10559: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 10560: Nf4_red++;
1.7 noro 10561: }
10562: }
10563: rank++;
10564: }
10565: for ( j = col-1, l = rank-1; j >= 0; j-- )
10566: if ( colstat[j] ) {
10567: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10568: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10569: }
10570: s = sugar[l];
10571: for ( i = 0; i < l; i++ ) {
10572: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10573: if ( a ) {
10574: sugar[i] = MAX(sugar[i],s);
10575: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 10576: Nf4_red++;
1.7 noro 10577: }
10578: }
10579: l--;
10580: }
10581: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10582: GCFREE(cmat);
10583: return rank;
10584: }
10585: #endif
10586:
1.28 noro 10587: 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)
10588: {
10589: int i,j,k,l,rank,s,imin;
10590: mp_limb_t inv;
10591: mp_limb_t a;
10592: UINT c;
10593: mp_limb_t *t,*pivot,*pk;
10594: UINT *ck;
10595: UINT **cmat;
10596: UINT *ct;
10597: ND_pairs pair;
10598: SIG sg;
10599:
10600: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
10601: for ( i = 0; i < row; i++ ) {
10602: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
10603: bzero(cmat[i],col*sizeof(UINT));
10604: }
10605:
10606: for ( rank = 0, j = 0; j < col; j++ ) {
10607: for ( i = rank; i < row; i++ ) {
10608: a = mat[i][j]; c = cmat[i][j];
10609: MOD128(a,c,md);
10610: mat[i][j] = a; cmat[i][j] = 0;
10611: }
10612: imin = -1;
10613: for ( i = rank; i < row; i++ )
10614: if ( mat[i][j] && (imin < 0 || comp_sig(sig[imin],sig[i]) > 0) ) imin = i;
10615: if ( imin == -1 ) {
10616: colstat[j] = 0;
10617: continue;
10618: } else
10619: colstat[j] = 1;
10620: if ( imin != rank ) {
10621: t = mat[imin]; mat[imin] = mat[rank]; mat[rank] = t;
10622: ct = cmat[imin]; cmat[imin] = cmat[rank]; cmat[rank] = ct;
10623: s = sugar[imin]; sugar[imin] = sugar[rank]; sugar[rank] = s;
10624: sg = sig[imin]; sig[imin] = sig[rank]; sig[rank] = sg;
10625: if ( spactive ) {
10626: pair = spactive[imin]; spactive[imin] = spactive[rank];
10627: spactive[rank] = pair;
10628: }
10629: }
10630: /* column j is normalized */
10631: s = sugar[rank];
10632: inv = invm((UINT)mat[rank][j],md);
10633: /* normalize pivot row */
10634: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
10635: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
10636: }
10637: for ( i = rank+1; i < row; i++ ) {
10638: if ( (a = mat[i][j]) != 0 ) {
10639: sugar[i] = MAX(sugar[i],s);
10640: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
10641: Nf4_red++;
10642: }
10643: }
10644: rank++;
10645: }
10646: #if 1
10647: for ( j = col-1, l = rank-1; j >= 0; j-- )
10648: if ( colstat[j] ) {
10649: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
10650: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
10651: }
10652: s = sugar[l];
10653: for ( i = 0; i < l; i++ ) {
10654: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
10655: if ( a && comp_sig(sig[i],sig[l]) > 0 ) {
10656: sugar[i] = MAX(sugar[i],s);
10657: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
10658: Nf4_red++;
10659: }
10660: }
10661: l--;
10662: }
10663: #endif
10664: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
10665: GCFREE(cmat);
10666: return rank;
10667: }
10668:
10669: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
10670: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
10671: {
10672: int spcol,sprow,a;
10673: int i,j,k,l,rank;
10674: NODE r0,r;
10675: ND_pairs sp;
10676: ND spol;
10677: mp_limb_t **spmat;
10678: mp_limb_t *svect,*cvect;
10679: mp_limb_t *v;
10680: int *colstat;
10681: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
10682: int maxrs;
10683: int *spsugar;
10684: ND_pairs *spactive;
10685: SIG *spsig;
10686:
10687: get_eg(&eg0);
10688: /* elimination (1st step) */
10689: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
10690: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10691: spsugar = (int *)MALLOC(nsp*sizeof(int));
10692: spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
10693: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
10694: nd_sp(m,0,sp,&spol);
10695: if ( !spol ) {
1.29 noro 10696: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 10697: continue;
10698: }
10699: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
10700: nd_to_vect64(m,s0vect,col,spol,svect);
10701: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
10702: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
10703: if ( i < col ) {
10704: spmat[sprow] = svect;
10705: spsugar[sprow] = MAX(maxrs,SG(spol));
10706: spsig[sprow] = sp->sig;
10707: sprow++;
10708: } else {
1.29 noro 10709: syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28 noro 10710: }
10711: nd_free(spol);
10712: }
10713: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
10714: if ( DP_Print ) {
10715: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
10716: fflush(asir_out);
10717: }
10718: /* free index arrays */
10719: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
10720:
10721: /* elimination (2nd step) */
10722: colstat = (int *)MALLOC(col*sizeof(int));
10723: rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
10724: r0 = 0;
10725: for ( i = 0; i < rank; i++ ) {
10726: NEXTNODE(r0,r);
10727: BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
10728: SG((NDV)BDY(r)) = spsugar[i];
10729: ((NDV)BDY(r))->sig = spsig[i];
10730: GCFREE(spmat[i]);
10731: }
10732: if ( r0 ) NEXT(r) = 0;
10733:
10734: for ( ; i < sprow; i++ ) {
10735: GCFREE(spmat[i]);
1.29 noro 10736: syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28 noro 10737: }
10738: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
10739: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
10740: if ( DP_Print ) {
10741: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
10742: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
10743: nsp,nred,sprow,col,rank);
10744: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
10745: }
10746: return r0;
10747: }
10748:
10749: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
10750: {
10751: IndArray *imat;
10752: int nsp,nred,i,start;
10753: int *rhead;
10754: NODE r0,rp;
10755: ND_pairs sp;
10756: NM_ind_pair *rvect;
10757: UINT *s;
10758: int *s0hash;
10759: struct oEGT eg0,eg1,eg_conv;
10760:
10761: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
10762: nred = length(rp0);
10763: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
10764: rhead = (int *)MALLOC(col*sizeof(int));
10765: for ( i = 0; i < col; i++ ) rhead[i] = 0;
10766:
10767: /* construction of index arrays */
10768: get_eg(&eg0);
10769: if ( DP_Print ) {
10770: fprintf(asir_out,"%dx%d,",nsp+nred,col);
10771: fflush(asir_out);
10772: }
10773: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
10774: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
10775: rvect[i] = (NM_ind_pair)BDY(rp);
10776: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
10777: rhead[imat[i]->head] = 1;
10778: start = imat[i]->head;
10779: }
10780: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
10781: if ( DP_Print ) {
10782: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
10783: fflush(asir_out);
10784: }
10785: if ( m > 0 )
10786: r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
10787: else
10788: // r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
10789: error("nd_f4_red_q_main_s : not implemented yet");
10790: return r0;
10791: }
10792:
10793: INLINE int ndl_find_reducer_minsig(UINT *dg)
10794: {
10795: RHist r;
10796: int i,singular,ret,d,k,imin;
10797: SIG t;
10798: static int wpd,nvar;
10799: static SIG quo,quomin;
10800: static UINT *tmp;
10801:
10802: if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
10803: if ( wpd != nd_wpd ) {
10804: wpd = nd_wpd;
10805: tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
10806: }
10807: #if 0
10808: d = ndl_hash_value(dg);
10809: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
10810: if ( ndl_equal(dg,DL(r)) ) {
10811: return r->index;
10812: }
10813: }
10814: #endif
10815: imin = -1;
10816: for ( i = 0; i < nd_psn; i++ ) {
10817: r = nd_psh[i];
10818: if ( ndl_reducible(dg,DL(r)) ) {
10819: ndl_sub(dg,DL(r),tmp);
10820: _ndltodl(tmp,DL(quo));
10821: _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
10822: quo->pos = nd_psh[i]->sig->pos;
10823: if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
10824: t = quo; quo = quomin; quomin = t;
10825: imin = i;
10826: }
10827: }
10828: }
10829: if ( imin == -1 ) return nd_psn;
10830: else {
10831: #if 0
10832: nd_append_red(dg,i);
10833: #endif
10834: return imin;
10835: }
10836: }
10837:
10838: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
10839: {
10840: NODE rp0,rp;
10841: NM mul,head,s0,s;
10842: int index,col,i,sugar;
10843: RHist h;
10844: UINT *s0v,*p;
10845: NM_ind_pair pair;
10846: ND red;
10847: NDV *ps;
10848: SIG sig;
10849:
10850: s0 = 0; rp0 = 0; col = 0;
10851: if ( nd_demand )
10852: ps = trace?nd_ps_trace_sym:nd_ps_sym;
10853: else
10854: ps = trace?nd_ps_trace:nd_ps;
10855: while ( 1 ) {
10856: head = remove_head_pbucket_symbolic(bucket);
10857: if ( !head ) break;
10858: if ( !s0 ) s0 = head;
10859: else NEXT(s) = head;
10860: s = head;
10861: index = ndl_find_reducer_minsig(DL(head));
10862: if ( index >= 0 && index < nd_psn ) {
10863: h = nd_psh[index];
10864: NEWNM(mul);
10865: ndl_sub(DL(head),DL(h),DL(mul));
10866: if ( ndl_check_bound2(index,DL(mul)) )
10867: return 0;
10868: sugar = TD(DL(mul))+SG(ps[index]);
10869: NEWSIG(sig);
10870: _ndltodl(DL(mul),DL(sig));
10871: _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
10872: sig->pos = nd_psh[index]->sig->pos;
10873: MKNM_ind_pair(pair,mul,index,sugar,sig);
10874: red = ndv_mul_nm_symbolic(mul,ps[index]);
10875: add_pbucket_symbolic(bucket,nd_remove_head(red));
10876: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
10877: }
10878: col++;
10879: }
10880: if ( rp0 ) NEXT(rp) = 0;
10881: NEXT(s) = 0;
10882: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
10883: for ( i = 0, p = s0v, s = s0; i < col;
10884: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
10885: *s0vect = s0v;
10886: *r = rp0;
10887:
10888: return col;
10889: }
10890:
10891: NODE nd_sba_f4(int m,int **indp)
10892: {
10893: int i,nh,stat,index,f4red;
10894: NODE r,g,tn0,tn,node;
10895: ND_pairs d,l,t,ll0,ll,lh;
10896: LIST l0,l1;
10897: ND spol,red;
10898: NDV nf,redv;
10899: NM s0,s;
10900: NODE rp0,srp0,nflist;
10901: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
10902: UINT c;
10903: UINT **spmat;
10904: UINT *s0vect,*svect,*p,*v;
10905: int *colstat;
10906: IndArray *imat;
10907: int *rhead;
10908: int spcol,sprow;
10909: int sugar,sugarh;
1.30 ! noro 10910: int nbase;
1.28 noro 10911: PGeoBucket bucket;
10912: struct oEGT eg0,eg1,eg_f4;
10913: Z i1,i2,sugarq;
1.29 noro 10914: NODE *syzlist;
1.28 noro 10915:
10916: Nf4_red=0;
1.30 ! noro 10917: d = 0;
1.29 noro 10918: syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28 noro 10919: for ( i = 0; i < nd_psn; i++ ) {
1.30 ! noro 10920: d = update_pairs_s(d,i,syzlist);
1.28 noro 10921: }
1.30 ! noro 10922: nd_nbase = nd_psn;
1.28 noro 10923: f4red = 1;
10924: while ( d ) {
10925: l = nd_minsugarp_s(d,&d);
10926: if ( !l ) continue;
10927: sugar = nd_sugarweight?l->sugar2:SG(l);
10928: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
10929: bucket = create_pbucket();
10930: stat = nd_sp_f4(m,0,l,bucket);
10931: if ( !stat ) {
10932: for ( t = l; NEXT(t); t = NEXT(t) );
10933: NEXT(t) = d; d = l;
10934: d = nd_reconstruct(0,d);
10935: continue;
10936: }
10937: if ( bucket->m < 0 ) continue;
10938: col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
10939: if ( !col ) {
10940: for ( t = l; NEXT(t); t = NEXT(t) );
10941: NEXT(t) = d; d = l;
10942: d = nd_reconstruct(0,d);
10943: continue;
10944: }
10945: if ( DP_Print ) fprintf(asir_out,"sugar=%d,",sugar);
10946: l = remove_large_lcm(l);
1.29 noro 10947: nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
1.28 noro 10948: /* adding new bases */
10949: for ( r = nflist; r; r = NEXT(r) ) {
10950: ND tmp,tmpred;
10951: SIG sig;
10952:
10953: nf = (NDV)BDY(r);
10954: sig = nf->sig;
10955: tmp = ndvtond(m,nf);
10956: stat = nd_nf_s(m,0,tmp,nd_ps,!Top,&tmpred);
10957: if ( stat < 0 ) {
10958: // top reducible
10959: if ( DP_Print ) { fprintf(asir_out,"S"); fflush(asir_out); }
10960: } else if ( tmpred ) {
10961: nf = ndtondv(m,tmpred);
10962: ndv_removecont(m,nf);
10963: nh = ndv_newps(m,nf,0);
1.30 ! noro 10964: d = update_pairs_s(d,nh,syzlist);
! 10965: nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.28 noro 10966: } else {
1.29 noro 10967: syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.28 noro 10968: }
10969: }
1.30 ! noro 10970: for ( i = 0; i < nd_nbase; i++ )
! 10971: for ( r = syzlist[i]; r; r = NEXT(r) )
! 10972: d = remove_spair_s(d,(SIG)BDY(r));
1.28 noro 10973: if ( DP_Print ) {
10974: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
10975: }
10976: f4red++;
10977: if ( nd_f4red && f4red > nd_f4red ) break;
10978: if ( nd_rank0 && !nflist ) break;
10979: }
10980: if ( DP_Print ) {
10981: fprintf(asir_out,"number of red=%d,",Nf4_red);
10982: }
1.30 ! noro 10983: g = conv_ilist_s(nd_demand,0,indp);
1.28 noro 10984: return g;
10985: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>