Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.246
1.246 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.245 2018/03/29 01:32:52 noro Exp $ */
1.2 noro 2:
1.94 noro 3: #include "nd.h"
1.63 noro 4:
1.198 noro 5: struct oEGT eg_search;
6:
1.131 noro 7: int diag_period = 6;
1.202 noro 8: int weight_check = 1;
1.61 noro 9: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94 noro 10: int nd_dcomp;
1.220 noro 11: int nd_rref2;
1.94 noro 12: NM _nm_free_list;
13: ND _nd_free_list;
14: ND_pairs _ndp_free_list;
1.150 noro 15: NODE nd_hcf;
1.32 noro 16:
1.219 noro 17: Obj nd_top_weight;
18:
1.146 noro 19: static NODE nd_subst;
20: static VL nd_vc;
1.121 noro 21: static int nd_ntrans;
1.117 noro 22: static int nd_nalg;
1.103 noro 23: #if 0
1.74 noro 24: static int ndv_alloc;
1.103 noro 25: #endif
1.87 noro 26: #if 1
1.69 noro 27: static int nd_f4_nsp=0x7fffffff;
1.87 noro 28: #else
29: static int nd_f4_nsp=50;
30: #endif
1.42 noro 31: static double nd_scale=2;
1.61 noro 32: static UINT **nd_bound;
1.42 noro 33: static struct order_spec *nd_ord;
34: static EPOS nd_epos;
1.43 noro 35: static BlockMask nd_blockmask;
1.42 noro 36: static int nd_nvar;
37: static int nd_isrlex;
38: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61 noro 39: static UINT nd_mask[32];
40: static UINT nd_mask0,nd_mask1;
1.42 noro 41:
1.20 noro 42: static NDV *nd_ps;
1.215 noro 43: static NDV *nd_ps_gz;
1.53 noro 44: static NDV *nd_ps_trace;
1.215 noro 45: static NDV *nd_ps_sym;
46: static NDV *nd_ps_trace_sym;
1.42 noro 47: static RHist *nd_psh;
48: static int nd_psn,nd_pslen;
49: static RHist *nd_red;
1.96 noro 50: static int *nd_work_vector;
51: static int **nd_matrix;
52: static int nd_matrix_len;
1.97 noro 53: static struct weight_or_block *nd_worb;
54: static int nd_worb_len;
1.42 noro 55: static int nd_found,nd_create,nd_notfirst;
56: static int nmv_adv;
1.77 noro 57: static int nd_demand;
1.174 noro 58: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
1.224 noro 59: static int nd_module_rank,nd_poly_weight_len;
60: static int *nd_poly_weight,*nd_module_weight;
1.167 noro 61: static NODE nd_tracelist;
62: static NODE nd_alltracelist;
1.234 noro 63: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
1.187 noro 64: static int *nd_gbblock;
1.209 noro 65: static NODE nd_nzlist,nd_check_splist;
66: static int nd_splist;
1.231 noro 67: static int *nd_sugarweight;
1.241 noro 68: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.1 noro 69:
1.119 noro 70: NumberField get_numberfield();
1.114 noro 71: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
72: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
1.152 ohara 73: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
1.118 noro 74: int nd_monic(int m,ND *p);
1.129 noro 75: NDV plain_vect_to_ndv_q(Q *mat,int col,UINT *s0vect);
1.157 noro 76: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
77: NDV pltondv(VL vl,VL dvl,LIST p);
78: void pltozpl(LIST l,Q *cont,LIST *pp);
1.159 noro 79: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
1.167 noro 80: void nmtodp(int mod,NM m,DP *r);
81: NODE reverse_node(NODE n);
82: P ndc_div(int mod,union oNDC a,union oNDC b);
83: P ndctop(int mod,union oNDC c);
84: void finalize_tracelist(int i,P cont);
85: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.172 noro 86: void parse_nd_option(NODE opt);
1.198 noro 87: void dltondl(int n,DL dl,UINT *r);
88: DP ndvtodp(int mod,NDV p);
1.204 noro 89: DP ndtodp(int mod,ND p);
1.215 noro 90: NDV ndvtondvgz(NDV p);
91: NDV ndvgztondv(NDV p);
92: ND ndtondgz(ND p);
93: ND ndgztond(ND p);
1.114 noro 94:
1.221 ohara 95: void Pdp_set_weight(NODE,VECT *);
96: void Pox_cmo_rpc(NODE,Obj *);
97:
1.232 noro 98: ND nd_add_lf(ND p1,ND p2);
99: void nd_mul_c_lf(ND p,GZ mul);
100: void ndv_mul_c_lf(NDV p,GZ mul);
1.239 noro 101: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
102: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
1.240 noro 103: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.239 noro 104: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
1.234 noro 105: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232 noro 106: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
107: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.234 noro 108: NODE nd_f4_lf_trace_main(int m,int **indp);
109: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
110:
1.232 noro 111: extern int lf_lazy;
112: extern GZ current_mod_lf;
113:
1.228 noro 114: extern int Denominator,DP_Multiple,MaxDeg;
1.149 noro 115:
1.220 noro 116: #define BLEN (8*sizeof(unsigned long))
117:
118: typedef struct matrix {
119: int row,col;
120: unsigned long **a;
121: } *matrix;
122:
123:
1.1 noro 124: void nd_free_private_storage()
125: {
1.157 noro 126: _nm_free_list = 0;
127: _ndp_free_list = 0;
1.71 noro 128: #if 0
1.157 noro 129: GC_gcollect();
1.71 noro 130: #endif
1.1 noro 131: }
132:
133: void _NM_alloc()
134: {
1.157 noro 135: NM p;
136: int i;
1.1 noro 137:
1.157 noro 138: for ( i = 0; i < 1024; i++ ) {
1.200 noro 139: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157 noro 140: p->next = _nm_free_list; _nm_free_list = p;
141: }
1.1 noro 142: }
143:
1.220 noro 144: matrix alloc_matrix(int row,int col)
145: {
146: unsigned long **a;
147: int i,len,blen;
148: matrix mat;
149:
150: mat = (matrix)MALLOC(sizeof(struct matrix));
151: mat->row = row;
152: mat->col = col;
153: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
154: return mat;
155: }
156:
157:
1.1 noro 158: void _ND_alloc()
159: {
1.157 noro 160: ND p;
161: int i;
1.1 noro 162:
1.157 noro 163: for ( i = 0; i < 1024; i++ ) {
1.200 noro 164: p = (ND)MALLOC(sizeof(struct oND));
1.157 noro 165: p->body = (NM)_nd_free_list; _nd_free_list = p;
166: }
1.1 noro 167: }
168:
169: void _NDP_alloc()
170: {
1.157 noro 171: ND_pairs p;
172: int i;
1.1 noro 173:
1.157 noro 174: for ( i = 0; i < 1024; i++ ) {
1.200 noro 175: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
1.157 noro 176: +(nd_wpd-1)*sizeof(UINT));
177: p->next = _ndp_free_list; _ndp_free_list = p;
178: }
1.1 noro 179: }
180:
1.30 noro 181: INLINE int nd_length(ND p)
1.1 noro 182: {
1.157 noro 183: NM m;
184: int i;
1.1 noro 185:
1.157 noro 186: if ( !p )
187: return 0;
188: else {
189: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
190: return i;
191: }
1.1 noro 192: }
193:
1.230 noro 194: extern int dp_negative_weight;
195:
1.61 noro 196: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1 noro 197: {
1.157 noro 198: UINT u1,u2;
199: int i,j;
1.1 noro 200:
1.157 noro 201: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
202:
1.230 noro 203: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
1.65 noro 204: #if USE_UNROLL
1.157 noro 205: switch ( nd_bpe ) {
206: case 3:
207: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
208: u1 = d1[i]; u2 = d2[i];
209: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
210: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
211: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
212: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
213: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
214: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
215: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
216: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
217: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
218: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
219: }
220: return 1;
221: break;
222: case 4:
223: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
224: u1 = d1[i]; u2 = d2[i];
225: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
226: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
227: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
228: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
229: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
230: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
231: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
232: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
233: }
234: return 1;
235: break;
236: case 6:
237: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
238: u1 = d1[i]; u2 = d2[i];
239: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
240: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
241: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
242: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
243: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
244: }
245: return 1;
246: break;
247: case 8:
248: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
249: u1 = d1[i]; u2 = d2[i];
250: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
251: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
252: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
253: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
254: }
255: return 1;
256: break;
257: case 16:
258: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
259: u1 = d1[i]; u2 = d2[i];
260: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
261: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
262: }
263: return 1;
264: break;
265: case 32:
266: for ( i = nd_exporigin; i < nd_wpd; i++ )
267: if ( d1[i] < d2[i] ) return 0;
268: return 1;
269: break;
270: default:
271: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
272: u1 = d1[i]; u2 = d2[i];
273: for ( j = 0; j < nd_epw; j++ )
274: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
275: }
276: return 1;
277: }
1.65 noro 278: #else
1.157 noro 279: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
280: u1 = d1[i]; u2 = d2[i];
281: for ( j = 0; j < nd_epw; j++ )
282: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
283: }
284: return 1;
1.65 noro 285: #endif
1.1 noro 286: }
287:
1.61 noro 288: /*
289: * If the current order is a block order,
290: * then the last block is length 1 and contains
291: * the homo variable. Otherwise, the original
292: * order is either 0 or 2.
293: */
294:
1.164 noro 295: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
1.23 noro 296: {
1.157 noro 297: int w,i,e,n,omask0;
1.61 noro 298:
1.157 noro 299: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
300: n = nd_nvar-1;
301: ndl_zero(r);
302: for ( i = 0; i < n; i++ ) {
303: e = GET_EXP_OLD(d,i);
304: PUT_EXP(r,i,e);
305: }
306: w = TD(d);
307: PUT_EXP(r,nd_nvar-1,weight-w);
1.164 noro 308: if ( nd_module ) MPOS(r) = d[ompos];
1.157 noro 309: TD(r) = weight;
310: if ( nd_blockmask ) ndl_weight_mask(r);
1.61 noro 311: }
312:
313: void ndl_dehomogenize(UINT *d)
314: {
1.157 noro 315: UINT mask;
316: UINT h;
317: int i,bits;
318:
319: if ( nd_blockmask ) {
320: h = GET_EXP(d,nd_nvar-1);
321: XOR_EXP(d,nd_nvar-1,h);
322: TD(d) -= h;
323: ndl_weight_mask(d);
324: } else {
325: if ( nd_isrlex ) {
326: if ( nd_bpe == 32 ) {
327: h = d[nd_exporigin];
328: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
329: d[i-1] = d[i];
330: d[i-1] = 0;
331: TD(d) -= h;
332: } else {
333: bits = nd_epw*nd_bpe;
334: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
335: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
336: for ( i = nd_exporigin; i < nd_wpd; i++ )
337: d[i] = ((d[i]<<nd_bpe)&mask)
338: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
339: TD(d) -= h;
340: }
341: } else {
342: h = GET_EXP(d,nd_nvar-1);
343: XOR_EXP(d,nd_nvar-1,h);
344: TD(d) -= h;
345: }
346: }
1.23 noro 347: }
348:
1.61 noro 349: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1 noro 350: {
1.157 noro 351: UINT t1,t2,u,u1,u2;
352: int i,j,l;
353:
354: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
355: error("ndl_lcm : inconsistent monomials");
356: #if USE_UNROLL
357: switch ( nd_bpe ) {
358: case 3:
359: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
360: u1 = d1[i]; u2 = d2[i];
361: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
362: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
363: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
364: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
365: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
366: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
367: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
368: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
369: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
370: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
371: d[i] = u;
372: }
373: break;
374: case 4:
375: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
376: u1 = d1[i]; u2 = d2[i];
377: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
378: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
379: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
380: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
381: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
382: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
383: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
384: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
385: d[i] = u;
386: }
387: break;
388: case 6:
389: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
390: u1 = d1[i]; u2 = d2[i];
391: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
392: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
393: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
394: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
395: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
396: d[i] = u;
397: }
398: break;
399: case 8:
400: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
401: u1 = d1[i]; u2 = d2[i];
402: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
403: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
404: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
405: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
406: d[i] = u;
407: }
408: break;
409: case 16:
410: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
411: u1 = d1[i]; u2 = d2[i];
412: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
413: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
414: d[i] = u;
415: }
416: break;
417: case 32:
418: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
419: u1 = d1[i]; u2 = d2[i];
420: d[i] = u1>u2?u1:u2;
421: }
422: break;
423: default:
424: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
425: u1 = d1[i]; u2 = d2[i];
426: for ( j = 0, u = 0; j < nd_epw; j++ ) {
427: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
428: }
429: d[i] = u;
430: }
431: break;
432: }
433: #else
434: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
435: u1 = d1[i]; u2 = d2[i];
436: for ( j = 0, u = 0; j < nd_epw; j++ ) {
437: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
438: }
439: d[i] = u;
440: }
441: #endif
1.163 noro 442: if ( nd_module ) MPOS(d) = MPOS(d1);
1.157 noro 443: TD(d) = ndl_weight(d);
444: if ( nd_blockmask ) ndl_weight_mask(d);
445: }
446:
1.159 noro 447: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
1.157 noro 448: {
449: UINT t1,t2,u,u1,u2;
450: int i,j,l;
1.1 noro 451:
1.157 noro 452: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
453: u1 = d1[i]; u2 = d2[i];
454: for ( j = 0, u = 0; j < nd_epw; j++ ) {
455: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
456: }
457: d[i] = u;
458: }
1.57 noro 459: }
460:
1.61 noro 461: int ndl_weight(UINT *d)
1.1 noro 462: {
1.157 noro 463: UINT t,u;
464: int i,j;
1.1 noro 465:
1.157 noro 466: if ( current_dl_weight_vector )
467: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
468: u = GET_EXP(d,i);
469: t += MUL_WEIGHT(u,i);
470: }
471: else
472: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
473: u = d[i];
474: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
475: t += (u&nd_mask0);
476: }
1.167 noro 477: if ( nd_module && current_module_weight_vector && MPOS(d) )
478: t += current_module_weight_vector[MPOS(d)];
1.157 noro 479: return t;
1.1 noro 480: }
481:
1.231 noro 482: /* for sugarweight */
483:
484: int ndl_weight2(UINT *d)
485: {
486: int t,u;
487: int i,j;
488:
489: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
490: u = GET_EXP(d,i);
491: t += nd_sugarweight[i]*u;
492: }
493: if ( nd_module && current_module_weight_vector && MPOS(d) )
494: t += current_module_weight_vector[MPOS(d)];
495: return t;
496: }
497:
1.61 noro 498: void ndl_weight_mask(UINT *d)
1.43 noro 499: {
1.157 noro 500: UINT t,u;
501: UINT *mask;
502: int i,j,k,l;
503:
504: l = nd_blockmask->n;
505: for ( k = 0; k < l; k++ ) {
506: mask = nd_blockmask->mask[k];
507: if ( current_dl_weight_vector )
508: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
509: u = GET_EXP_MASK(d,i,mask);
510: t += MUL_WEIGHT(u,i);
511: }
512: else
513: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
514: u = d[i]&mask[i];
515: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
516: t += (u&nd_mask0);
517: }
518: d[k+1] = t;
519: }
1.43 noro 520: }
521:
1.61 noro 522: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1 noro 523: {
1.157 noro 524: int i;
1.1 noro 525:
1.157 noro 526: d1 += nd_exporigin;
527: d2 += nd_exporigin;
528: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
529: if ( *d1 > *d2 )
530: return nd_isrlex ? -1 : 1;
531: else if ( *d1 < *d2 )
532: return nd_isrlex ? 1 : -1;
533: return 0;
1.1 noro 534: }
535:
1.61 noro 536: int ndl_block_compare(UINT *d1,UINT *d2)
1.43 noro 537: {
1.157 noro 538: int i,l,j,ord_o,ord_l;
539: struct order_pair *op;
540: UINT t1,t2,m;
541: UINT *mask;
542:
543: l = nd_blockmask->n;
544: op = nd_blockmask->order_pair;
545: for ( j = 0; j < l; j++ ) {
546: mask = nd_blockmask->mask[j];
547: ord_o = op[j].order;
548: if ( ord_o < 2 )
549: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
550: else if ( t1 < t2 ) return -1;
551: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
552: m = mask[i];
553: t1 = d1[i]&m;
554: t2 = d2[i]&m;
555: if ( t1 > t2 )
556: return !ord_o ? -1 : 1;
557: else if ( t1 < t2 )
558: return !ord_o ? 1 : -1;
559: }
560: }
561: return 0;
1.43 noro 562: }
563:
1.96 noro 564: int ndl_matrix_compare(UINT *d1,UINT *d2)
565: {
1.219 noro 566: int i,j,s,row;
1.157 noro 567: int *v;
1.245 noro 568: Q **mat;
1.219 noro 569: Q *w;
570: Q t,t1,t2;
1.96 noro 571:
1.157 noro 572: for ( j = 0; j < nd_nvar; j++ )
573: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.245 noro 574: if ( nd_top_weight ) {
575: if ( OID(nd_top_weight) == O_VECT ) {
576: mat = (Q **)&BDY((VECT)nd_top_weight);
577: row = 1;
578: } else {
579: mat = (Q **)BDY((MAT)nd_top_weight);
580: row = ((MAT)nd_top_weight)->row;
581: }
582: for ( i = 0; i < row; i++ ) {
583: w = (Q *)mat[i];
584: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
585: STOQ(nd_work_vector[j],t1);
586: mulq(w[j],t1,&t2);
587: addq(t,t2,&t1);
588: t = t1;
589: }
590: if ( t ) {
591: s = SGN(t);
592: if ( s > 0 ) return 1;
593: else if ( s < 0 ) return -1;
594: }
595: }
596: }
1.157 noro 597: for ( i = 0; i < nd_matrix_len; i++ ) {
598: v = nd_matrix[i];
599: for ( j = 0, s = 0; j < nd_nvar; j++ )
600: s += v[j]*nd_work_vector[j];
601: if ( s > 0 ) return 1;
602: else if ( s < 0 ) return -1;
603: }
1.245 noro 604: if ( !ndl_equal(d1,d2) )
605: error("afo");
1.157 noro 606: return 0;
1.96 noro 607: }
608:
1.97 noro 609: int ndl_composite_compare(UINT *d1,UINT *d2)
610: {
1.157 noro 611: int i,j,s,start,end,len,o;
612: int *v;
613: struct sparse_weight *sw;
614:
615: for ( j = 0; j < nd_nvar; j++ )
616: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
617: for ( i = 0; i < nd_worb_len; i++ ) {
618: len = nd_worb[i].length;
619: switch ( nd_worb[i].type ) {
620: case IS_DENSE_WEIGHT:
621: v = nd_worb[i].body.dense_weight;
622: for ( j = 0, s = 0; j < len; j++ )
623: s += v[j]*nd_work_vector[j];
624: if ( s > 0 ) return 1;
625: else if ( s < 0 ) return -1;
626: break;
627: case IS_SPARSE_WEIGHT:
628: sw = nd_worb[i].body.sparse_weight;
629: for ( j = 0, s = 0; j < len; j++ )
630: s += sw[j].value*nd_work_vector[sw[j].pos];
631: if ( s > 0 ) return 1;
632: else if ( s < 0 ) return -1;
633: break;
634: case IS_BLOCK:
635: o = nd_worb[i].body.block.order;
636: start = nd_worb[i].body.block.start;
637: switch ( o ) {
638: case 0:
639: end = start+len;
640: for ( j = start, s = 0; j < end; j++ )
641: s += MUL_WEIGHT(nd_work_vector[j],j);
642: if ( s > 0 ) return 1;
643: else if ( s < 0 ) return -1;
644: for ( j = end-1; j >= start; j-- )
645: if ( nd_work_vector[j] < 0 ) return 1;
646: else if ( nd_work_vector[j] > 0 ) return -1;
647: break;
648: case 1:
649: end = start+len;
650: for ( j = start, s = 0; j < end; j++ )
651: s += MUL_WEIGHT(nd_work_vector[j],j);
652: if ( s > 0 ) return 1;
653: else if ( s < 0 ) return -1;
654: for ( j = start; j < end; j++ )
655: if ( nd_work_vector[j] > 0 ) return 1;
656: else if ( nd_work_vector[j] < 0 ) return -1;
657: break;
658: case 2:
1.236 noro 659: end = start+len;
1.157 noro 660: for ( j = start; j < end; j++ )
661: if ( nd_work_vector[j] > 0 ) return 1;
662: else if ( nd_work_vector[j] < 0 ) return -1;
663: break;
664: }
665: break;
666: }
667: }
668: return 0;
1.97 noro 669: }
670:
1.58 noro 671: /* TDH -> WW -> TD-> RL */
672:
1.61 noro 673: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58 noro 674: {
1.157 noro 675: int i,m,e1,e2;
1.58 noro 676:
1.157 noro 677: if ( TD(d1) > TD(d2) ) return 1;
678: else if ( TD(d1) < TD(d2) ) return -1;
679: m = nd_nvar>>1;
680: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
681: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
682: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
683: }
684: if ( e1 > e2 ) return 1;
685: else if ( e1 < e2 ) return -1;
686: return ndl_lex_compare(d1,d2);
687: }
688:
1.224 noro 689: int ndl_module_weight_compare(UINT *d1,UINT *d2)
690: {
691: int s,j;
692:
693: if ( nd_nvar != nd_poly_weight_len )
694: error("invalid module weight : the length of polynomial weight != the number of variables");
695: s = 0;
696: for ( j = 0; j < nd_nvar; j++ )
697: s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
1.226 noro 698: if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
699: s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
700: }
1.224 noro 701: if ( s > 0 ) return 1;
702: else if ( s < 0 ) return -1;
703: else return 0;
704: }
705:
1.157 noro 706: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
707: {
1.164 noro 708: int i,c;
1.157 noro 709:
1.224 noro 710: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 711: if ( nd_ispot ) {
1.245 noro 712: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
1.174 noro 713: if ( TD(d1) > TD(d2) ) return 1;
714: else if ( TD(d1) < TD(d2) ) return -1;
715: if ( c = ndl_lex_compare(d1,d2) ) return c;
716: if ( MPOS(d1) < MPOS(d2) ) return 1;
717: else if ( MPOS(d1) > MPOS(d2) ) return -1;
718: return 0;
1.245 noro 719: }
1.157 noro 720: if ( MPOS(d1) < MPOS(d2) ) return 1;
721: else if ( MPOS(d1) > MPOS(d2) ) return -1;
722: }
723: if ( TD(d1) > TD(d2) ) return 1;
724: else if ( TD(d1) < TD(d2) ) return -1;
1.167 noro 725: if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160 noro 726: if ( !nd_ispot ) {
1.157 noro 727: if ( MPOS(d1) < MPOS(d2) ) return 1;
728: else if ( MPOS(d1) > MPOS(d2) ) return -1;
729: }
730: return 0;
731: }
732:
733: int ndl_module_glex_compare(UINT *d1,UINT *d2)
734: {
1.164 noro 735: int i,c;
1.157 noro 736:
1.224 noro 737: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 738: if ( nd_ispot ) {
1.157 noro 739: if ( MPOS(d1) < MPOS(d2) ) return 1;
740: else if ( MPOS(d1) > MPOS(d2) ) return -1;
741: }
742: if ( TD(d1) > TD(d2) ) return 1;
743: else if ( TD(d1) < TD(d2) ) return -1;
1.167 noro 744: if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160 noro 745: if ( !nd_ispot ) {
1.157 noro 746: if ( MPOS(d1) < MPOS(d2) ) return 1;
747: else if ( MPOS(d1) > MPOS(d2) ) return -1;
748: }
749: return 0;
750: }
751:
752: int ndl_module_lex_compare(UINT *d1,UINT *d2)
753: {
1.164 noro 754: int i,c;
1.157 noro 755:
1.224 noro 756: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 757: if ( nd_ispot ) {
1.157 noro 758: if ( MPOS(d1) < MPOS(d2) ) return 1;
759: else if ( MPOS(d1) > MPOS(d2) ) return -1;
760: }
1.167 noro 761: if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160 noro 762: if ( !nd_ispot ) {
1.157 noro 763: if ( MPOS(d1) < MPOS(d2) ) return 1;
764: else if ( MPOS(d1) > MPOS(d2) ) return -1;
765: }
766: return 0;
767: }
768:
769: int ndl_module_block_compare(UINT *d1,UINT *d2)
770: {
771: int i,c;
772:
1.224 noro 773: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 774: if ( nd_ispot ) {
1.157 noro 775: if ( MPOS(d1) < MPOS(d2) ) return 1;
776: else if ( MPOS(d1) > MPOS(d2) ) return -1;
777: }
778: if ( c = ndl_block_compare(d1,d2) ) return c;
1.160 noro 779: if ( !nd_ispot ) {
1.157 noro 780: if ( MPOS(d1) < MPOS(d2) ) return 1;
781: else if ( MPOS(d1) > MPOS(d2) ) return -1;
782: }
783: return 0;
784: }
785:
786: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
787: {
788: int i,c;
789:
1.224 noro 790: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 791: if ( nd_ispot ) {
1.157 noro 792: if ( MPOS(d1) < MPOS(d2) ) return 1;
793: else if ( MPOS(d1) > MPOS(d2) ) return -1;
794: }
795: if ( c = ndl_matrix_compare(d1,d2) ) return c;
1.160 noro 796: if ( !nd_ispot ) {
1.157 noro 797: if ( MPOS(d1) < MPOS(d2) ) return 1;
798: else if ( MPOS(d1) > MPOS(d2) ) return -1;
799: }
800: return 0;
801: }
802:
803: int ndl_module_composite_compare(UINT *d1,UINT *d2)
804: {
805: int i,c;
806:
1.224 noro 807: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160 noro 808: if ( nd_ispot ) {
1.157 noro 809: if ( MPOS(d1) > MPOS(d2) ) return 1;
810: else if ( MPOS(d1) < MPOS(d2) ) return -1;
811: }
812: if ( c = ndl_composite_compare(d1,d2) ) return c;
1.160 noro 813: if ( !nd_ispot ) {
1.157 noro 814: if ( MPOS(d1) > MPOS(d2) ) return 1;
815: else if ( MPOS(d1) < MPOS(d2) ) return -1;
816: }
817: return 0;
1.58 noro 818: }
819:
1.61 noro 820: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1 noro 821: {
1.157 noro 822: int i;
1.1 noro 823:
1.157 noro 824: switch ( nd_wpd ) {
825: case 2:
826: if ( TD(d2) != TD(d1) ) return 0;
827: if ( d2[1] != d1[1] ) return 0;
828: return 1;
829: break;
830: case 3:
831: if ( TD(d2) != TD(d1) ) return 0;
832: if ( d2[1] != d1[1] ) return 0;
833: if ( d2[2] != d1[2] ) return 0;
834: return 1;
835: break;
836: default:
837: for ( i = 0; i < nd_wpd; i++ )
838: if ( *d1++ != *d2++ ) return 0;
839: return 1;
840: break;
841: }
1.1 noro 842: }
843:
1.61 noro 844: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6 noro 845: {
1.157 noro 846: int i;
1.6 noro 847:
1.157 noro 848: switch ( nd_wpd ) {
849: case 2:
850: TD(d2) = TD(d1);
851: d2[1] = d1[1];
852: break;
853: case 3:
854: TD(d2) = TD(d1);
855: d2[1] = d1[1];
856: d2[2] = d1[2];
857: break;
858: default:
859: for ( i = 0; i < nd_wpd; i++ )
860: d2[i] = d1[i];
861: break;
862: }
1.6 noro 863: }
864:
1.61 noro 865: INLINE void ndl_zero(UINT *d)
866: {
1.157 noro 867: int i;
868: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
1.61 noro 869: }
870:
871: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1 noro 872: {
1.157 noro 873: int i;
1.1 noro 874:
1.162 noro 875: if ( nd_module ) {
876: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
1.167 noro 877: error("ndl_add : invalid operation");
1.162 noro 878: }
1.43 noro 879: #if 1
1.157 noro 880: switch ( nd_wpd ) {
881: case 2:
882: TD(d) = TD(d1)+TD(d2);
883: d[1] = d1[1]+d2[1];
884: break;
885: case 3:
886: TD(d) = TD(d1)+TD(d2);
887: d[1] = d1[1]+d2[1];
888: d[2] = d1[2]+d2[2];
889: break;
890: default:
891: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
892: break;
893: }
1.43 noro 894: #else
1.157 noro 895: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.43 noro 896: #endif
1.6 noro 897: }
898:
1.55 noro 899: /* d1 += d2 */
1.61 noro 900: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55 noro 901: {
1.157 noro 902: int i;
1.55 noro 903:
1.162 noro 904: if ( nd_module ) {
905: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
906: error("ndl_addto : invalid operation");
907: }
1.55 noro 908: #if 1
1.157 noro 909: switch ( nd_wpd ) {
910: case 2:
911: TD(d1) += TD(d2);
912: d1[1] += d2[1];
913: break;
914: case 3:
915: TD(d1) += TD(d2);
916: d1[1] += d2[1];
917: d1[2] += d2[2];
918: break;
919: default:
920: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
921: break;
922: }
1.55 noro 923: #else
1.157 noro 924: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
1.55 noro 925: #endif
926: }
927:
1.61 noro 928: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6 noro 929: {
1.157 noro 930: int i;
1.6 noro 931:
1.157 noro 932: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1 noro 933: }
934:
1.61 noro 935: int ndl_disjoint(UINT *d1,UINT *d2)
1.1 noro 936: {
1.157 noro 937: UINT t1,t2,u,u1,u2;
938: int i,j;
1.1 noro 939:
1.157 noro 940: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1.65 noro 941: #if USE_UNROLL
1.157 noro 942: switch ( nd_bpe ) {
943: case 3:
944: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
945: u1 = d1[i]; u2 = d2[i];
946: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
947: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
948: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
949: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
950: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
951: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
952: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
953: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
954: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
955: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
956: }
957: return 1;
958: break;
959: case 4:
960: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
961: u1 = d1[i]; u2 = d2[i];
962: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
963: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
964: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
965: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
966: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
967: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
968: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
969: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
970: }
971: return 1;
972: break;
973: case 6:
974: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
975: u1 = d1[i]; u2 = d2[i];
976: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
977: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
978: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
979: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
980: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
981: }
982: return 1;
983: break;
984: case 8:
985: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
986: u1 = d1[i]; u2 = d2[i];
987: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
988: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
989: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
990: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
991: }
992: return 1;
993: break;
994: case 16:
995: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
996: u1 = d1[i]; u2 = d2[i];
997: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
998: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
999: }
1000: return 1;
1001: break;
1002: case 32:
1003: for ( i = nd_exporigin; i < nd_wpd; i++ )
1004: if ( d1[i] && d2[i] ) return 0;
1005: return 1;
1006: break;
1007: default:
1008: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1009: u1 = d1[i]; u2 = d2[i];
1010: for ( j = 0; j < nd_epw; j++ ) {
1011: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1012: u1 >>= nd_bpe; u2 >>= nd_bpe;
1013: }
1014: }
1015: return 1;
1016: break;
1017: }
1.65 noro 1018: #else
1.157 noro 1019: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1020: u1 = d1[i]; u2 = d2[i];
1021: for ( j = 0; j < nd_epw; j++ ) {
1022: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1023: u1 >>= nd_bpe; u2 >>= nd_bpe;
1024: }
1025: }
1026: return 1;
1.65 noro 1027: #endif
1.1 noro 1028: }
1029:
1.114 noro 1030: int ndl_check_bound(UINT *d1,UINT *d2)
1.1 noro 1031: {
1.157 noro 1032: UINT u2;
1033: int i,j,ind,k;
1.1 noro 1034:
1.157 noro 1035: ind = 0;
1.65 noro 1036: #if USE_UNROLL
1.157 noro 1037: switch ( nd_bpe ) {
1038: case 3:
1039: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1040: u2 = d2[i];
1041: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1042: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1043: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1044: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1045: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1046: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1047: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1048: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1049: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1050: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1051: }
1052: return 0;
1053: break;
1054: case 4:
1055: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1056: u2 = d2[i];
1057: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1058: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1059: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1060: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1061: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1062: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1063: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1064: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1065: }
1066: return 0;
1067: break;
1068: case 6:
1069: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1070: u2 = d2[i];
1071: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1072: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1073: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1074: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1075: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1076: }
1077: return 0;
1078: break;
1079: case 8:
1080: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1081: u2 = d2[i];
1082: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1083: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1084: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1085: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1086: }
1087: return 0;
1088: break;
1089: case 16:
1090: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1091: u2 = d2[i];
1092: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1093: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1094: }
1095: return 0;
1096: break;
1097: case 32:
1098: for ( i = nd_exporigin; i < nd_wpd; i++ )
1099: if ( d1[i]+d2[i]<d1[i] ) return 1;
1100: return 0;
1101: break;
1102: default:
1103: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1104: u2 = d2[i];
1105: k = (nd_epw-1)*nd_bpe;
1106: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1107: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1108: }
1109: return 0;
1110: break;
1111: }
1.65 noro 1112: #else
1.157 noro 1113: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1114: u2 = d2[i];
1115: k = (nd_epw-1)*nd_bpe;
1116: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1117: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1118: }
1119: return 0;
1.65 noro 1120: #endif
1.1 noro 1121: }
1122:
1.114 noro 1123: int ndl_check_bound2(int index,UINT *d2)
1124: {
1.157 noro 1125: return ndl_check_bound(nd_bound[index],d2);
1.114 noro 1126: }
1127:
1.61 noro 1128: INLINE int ndl_hash_value(UINT *d)
1.1 noro 1129: {
1.157 noro 1130: int i;
1131: int r;
1.1 noro 1132:
1.157 noro 1133: r = 0;
1134: for ( i = 0; i < nd_wpd; i++ )
1135: r = ((r<<16)+d[i])%REDTAB_LEN;
1136: return r;
1.1 noro 1137: }
1138:
1.63 noro 1139: INLINE int ndl_find_reducer(UINT *dg)
1.1 noro 1140: {
1.157 noro 1141: RHist r;
1142: int d,k,i;
1.1 noro 1143:
1.157 noro 1144: d = ndl_hash_value(dg);
1145: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1146: if ( ndl_equal(dg,DL(r)) ) {
1147: if ( k > 0 ) nd_notfirst++;
1148: nd_found++;
1149: return r->index;
1150: }
1151: }
1152: if ( Reverse )
1153: for ( i = nd_psn-1; i >= 0; i-- ) {
1154: r = nd_psh[i];
1155: if ( ndl_reducible(dg,DL(r)) ) {
1156: nd_create++;
1157: nd_append_red(dg,i);
1158: return i;
1159: }
1160: }
1161: else
1162: for ( i = 0; i < nd_psn; i++ ) {
1163: r = nd_psh[i];
1164: if ( ndl_reducible(dg,DL(r)) ) {
1165: nd_create++;
1166: nd_append_red(dg,i);
1167: return i;
1168: }
1169: }
1170: return -1;
1.1 noro 1171: }
1172:
1.63 noro 1173: ND nd_merge(ND p1,ND p2)
1174: {
1.157 noro 1175: int n,c;
1176: int t,can,td1,td2;
1177: ND r;
1178: NM m1,m2,mr0,mr,s;
1179:
1180: if ( !p1 ) return p2;
1181: else if ( !p2 ) return p1;
1182: else {
1183: can = 0;
1184: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1185: c = DL_COMPARE(DL(m1),DL(m2));
1186: switch ( c ) {
1187: case 0:
1188: s = m1; m1 = NEXT(m1);
1189: can++; NEXTNM2(mr0,mr,s);
1190: s = m2; m2 = NEXT(m2); FREENM(s);
1191: break;
1192: case 1:
1193: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1194: break;
1195: case -1:
1196: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1197: break;
1198: }
1199: }
1200: if ( !mr0 )
1201: if ( m1 ) mr0 = m1;
1202: else if ( m2 ) mr0 = m2;
1203: else return 0;
1204: else if ( m1 ) NEXT(mr) = m1;
1205: else if ( m2 ) NEXT(mr) = m2;
1206: else NEXT(mr) = 0;
1207: BDY(p1) = mr0;
1208: SG(p1) = MAX(SG(p1),SG(p2));
1209: LEN(p1) = LEN(p1)+LEN(p2)-can;
1210: FREEND(p2);
1211: return p1;
1212: }
1.63 noro 1213: }
1214:
1.31 noro 1215: ND nd_add(int mod,ND p1,ND p2)
1.1 noro 1216: {
1.157 noro 1217: int n,c;
1218: int t,can,td1,td2;
1219: ND r;
1220: NM m1,m2,mr0,mr,s;
1221:
1222: if ( !p1 ) return p2;
1223: else if ( !p2 ) return p1;
1224: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.232 noro 1225: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1.157 noro 1226: else if ( !mod ) return nd_add_q(p1,p2);
1227: else {
1228: can = 0;
1229: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1230: c = DL_COMPARE(DL(m1),DL(m2));
1231: switch ( c ) {
1232: case 0:
1233: t = ((CM(m1))+(CM(m2))) - mod;
1234: if ( t < 0 ) t += mod;
1235: s = m1; m1 = NEXT(m1);
1236: if ( t ) {
1237: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1238: } else {
1239: can += 2; FREENM(s);
1240: }
1241: s = m2; m2 = NEXT(m2); FREENM(s);
1242: break;
1243: case 1:
1244: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1245: break;
1246: case -1:
1247: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1248: break;
1249: }
1250: }
1251: if ( !mr0 )
1252: if ( m1 ) mr0 = m1;
1253: else if ( m2 ) mr0 = m2;
1254: else return 0;
1255: else if ( m1 ) NEXT(mr) = m1;
1256: else if ( m2 ) NEXT(mr) = m2;
1257: else NEXT(mr) = 0;
1258: BDY(p1) = mr0;
1259: SG(p1) = MAX(SG(p1),SG(p2));
1260: LEN(p1) = LEN(p1)+LEN(p2)-can;
1261: FREEND(p2);
1262: return p1;
1263: }
1.95 noro 1264: }
1265:
1266: /* XXX on opteron, the inlined manipulation of destructive additon of
1267: * two NM seems to make gcc optimizer get confused, so the part is
1268: * done in a function.
1269: */
1270:
1.113 noro 1271: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95 noro 1272: {
1.157 noro 1273: NM s;
1274: P t;
1275: int can;
1276:
1277: addp(nd_vc,CP(*m1),CP(*m2),&t);
1278: s = *m1; *m1 = NEXT(*m1);
1279: if ( t ) {
1280: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1281: } else {
1282: can = 2; FREENM(s);
1283: }
1284: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1285: return can;
1.95 noro 1286: }
1287:
1.113 noro 1288: ND nd_add_q(ND p1,ND p2)
1.95 noro 1289: {
1.157 noro 1290: int n,c,can;
1291: ND r;
1292: NM m1,m2,mr0,mr,s;
1293: P t;
1294:
1295: if ( !p1 ) return p2;
1296: else if ( !p2 ) return p1;
1297: else {
1298: can = 0;
1299: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1300: c = DL_COMPARE(DL(m1),DL(m2));
1301: switch ( c ) {
1302: case 0:
1.95 noro 1303: #if defined(__x86_64__)
1.157 noro 1304: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95 noro 1305: #else
1.157 noro 1306: addp(nd_vc,CP(m1),CP(m2),&t);
1307: s = m1; m1 = NEXT(m1);
1308: if ( t ) {
1309: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1310: } else {
1311: can += 2; FREENM(s);
1312: }
1313: s = m2; m2 = NEXT(m2); FREENM(s);
1.95 noro 1314: #endif
1.157 noro 1315: break;
1316: case 1:
1317: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1318: break;
1319: case -1:
1320: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1321: break;
1322: }
1323: }
1324: if ( !mr0 )
1325: if ( m1 ) mr0 = m1;
1326: else if ( m2 ) mr0 = m2;
1327: else return 0;
1328: else if ( m1 ) NEXT(mr) = m1;
1329: else if ( m2 ) NEXT(mr) = m2;
1330: else NEXT(mr) = 0;
1331: BDY(p1) = mr0;
1332: SG(p1) = MAX(SG(p1),SG(p2));
1333: LEN(p1) = LEN(p1)+LEN(p2)-can;
1334: FREEND(p2);
1335: return p1;
1336: }
1.17 noro 1337: }
1338:
1.71 noro 1339: ND nd_add_sf(ND p1,ND p2)
1340: {
1.157 noro 1341: int n,c,can;
1342: ND r;
1343: NM m1,m2,mr0,mr,s;
1344: int t;
1345:
1346: if ( !p1 ) return p2;
1347: else if ( !p2 ) return p1;
1348: else {
1349: can = 0;
1350: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1351: c = DL_COMPARE(DL(m1),DL(m2));
1352: switch ( c ) {
1353: case 0:
1354: t = _addsf(CM(m1),CM(m2));
1355: s = m1; m1 = NEXT(m1);
1356: if ( t ) {
1357: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1358: } else {
1359: can += 2; FREENM(s);
1360: }
1361: s = m2; m2 = NEXT(m2); FREENM(s);
1362: break;
1363: case 1:
1364: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1365: break;
1366: case -1:
1367: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1368: break;
1369: }
1370: }
1371: if ( !mr0 )
1372: if ( m1 ) mr0 = m1;
1373: else if ( m2 ) mr0 = m2;
1374: else return 0;
1375: else if ( m1 ) NEXT(mr) = m1;
1376: else if ( m2 ) NEXT(mr) = m2;
1377: else NEXT(mr) = 0;
1378: BDY(p1) = mr0;
1379: SG(p1) = MAX(SG(p1),SG(p2));
1380: LEN(p1) = LEN(p1)+LEN(p2)-can;
1381: FREEND(p2);
1382: return p1;
1383: }
1.71 noro 1384: }
1385:
1.232 noro 1386:
1.167 noro 1387: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1.146 noro 1388: {
1.157 noro 1389: int c,c1,c2;
1390: Q cg,cred,gcd,tq;
1391: P cgp,credp,gcdp;
1392: Obj tr,tr1;
1393:
1.167 noro 1394: if ( mod == -1 ) {
1.157 noro 1395: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.167 noro 1396: *divp = (Obj)ONE;
1.232 noro 1397: } else if ( mod == -2 ) {
1398: GZ inv,t;
1399: divlf(ONEGZ,HCZ(p),&inv);
1400: chsgnlf(HCZ(g),&t);
1401: mullf(inv,t,&CZ(mul));
1402: *divp = (Obj)ONE;
1.167 noro 1403: } else if ( mod ) {
1.157 noro 1404: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1405: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.167 noro 1406: *divp = (Obj)ONE;
1.157 noro 1407: } else if ( nd_vc ) {
1408: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1409: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1410: chsgnp(cgp,&CP(mul));
1411: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1412: if ( dn ) {
1413: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1414: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1415: }
1.167 noro 1416: *divp = (Obj)credp;
1.157 noro 1417: } else {
1418: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1419: chsgnq(cg,&CQ(mul));
1420: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1421: if ( dn ) {
1422: mulq(dn->z,cred,&tq); dn->z = tq;
1423: }
1.167 noro 1424: *divp = (Obj)cred;
1.157 noro 1425: }
1426: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.146 noro 1427: }
1428:
1.1 noro 1429: /* ret=1 : success, ret=0 : overflow */
1.146 noro 1430: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1 noro 1431: {
1.157 noro 1432: NM m,mrd,tail;
1433: NM mul;
1434: int n,sugar,psugar,sugar0,stat,index;
1435: int c,c1,c2,dummy;
1436: RHist h;
1437: NDV p,red;
1.167 noro 1438: Q cg,cred,gcd,tq,qq,iq;
1439: DP dmul;
1440: NODE node;
1441: LIST hist;
1.157 noro 1442: double hmag;
1443: P tp,tp1;
1.167 noro 1444: Obj tr,tr1,div;
1445: union oNDC hg;
1446: P cont;
1.157 noro 1447:
1448: if ( dn ) {
1449: if ( mod )
1450: dn->m = 1;
1451: else if ( nd_vc )
1452: dn->r = (R)ONE;
1453: else
1454: dn->z = ONE;
1455: }
1456: if ( !g ) {
1457: *rp = d;
1458: return 1;
1459: }
1460: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1461:
1462: sugar0 = sugar = SG(g);
1463: n = NV(g);
1.235 noro 1464: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157 noro 1465: if ( d )
1466: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1467: for ( ; g; ) {
1468: index = ndl_find_reducer(HDL(g));
1469: if ( index >= 0 ) {
1470: h = nd_psh[index];
1471: ndl_sub(HDL(g),DL(h),DL(mul));
1472: if ( ndl_check_bound2(index,DL(mul)) ) {
1473: nd_free(g); nd_free(d);
1474: return 0;
1475: }
1476: p = nd_demand ? ndv_load(index) : ps[index];
1.167 noro 1477: /* d+g -> div*(d+g)+mul*p */
1478: g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1.172 noro 1479: if ( nd_gentrace ) {
1.167 noro 1480: /* Trace=[div,index,mul,ONE] */
1481: STOQ(index,iq);
1482: nmtodp(mod,mul,&dmul);
1483: node = mknode(4,div,iq,dmul,ONE);
1484: }
1.157 noro 1485: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.193 noro 1486: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1.167 noro 1487: hg = HCU(g);
1.157 noro 1488: nd_removecont2(d,g);
1.172 noro 1489: if ( dn || nd_gentrace ) {
1.167 noro 1490: /* overwrite cont : Trace=[div,index,mul,cont] */
1491: cont = ndc_div(mod,hg,HCU(g));
1492: if ( dn ) {
1493: if ( nd_vc ) {
1494: divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
1495: reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
1496: } else divq(dn->z,(Q)cont,&dn->z);
1.157 noro 1497: }
1.172 noro 1498: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1.157 noro 1499: }
1500: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1501: }
1.167 noro 1502: MKLIST(hist,node);
1503: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.157 noro 1504: } else if ( !full ) {
1505: *rp = g;
1506: return 1;
1507: } else {
1508: m = BDY(g);
1509: if ( NEXT(m) ) {
1510: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1511: } else {
1512: FREEND(g); g = 0;
1513: }
1514: if ( d ) {
1515: NEXT(tail)=m; tail=m; LEN(d)++;
1516: } else {
1517: MKND(n,m,1,d); tail = BDY(d);
1518: }
1519: }
1520: }
1521: if ( d ) SG(d) = sugar;
1522: *rp = d;
1523: return 1;
1.1 noro 1524: }
1.28 noro 1525:
1.53 noro 1526: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25 noro 1527: {
1.157 noro 1528: int hindex,index;
1529: NDV p;
1530: ND u,d,red;
1531: NODE l;
1532: NM mul,m,mrd,tail;
1533: int sugar,psugar,n,h_reducible;
1534: PGeoBucket bucket;
1535: int c,c1,c2;
1536: Q cg,cred,gcd,zzz;
1537: RHist h;
1538: double hmag,gmag;
1539: int count = 0;
1540: int hcount = 0;
1541:
1542: if ( !g ) {
1543: *rp = 0;
1544: return 1;
1545: }
1546: sugar = SG(g);
1547: n = NV(g);
1548: if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1549: bucket = create_pbucket();
1550: add_pbucket(mod,bucket,g);
1551: d = 0;
1.235 noro 1552: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157 noro 1553: while ( 1 ) {
1.232 noro 1554: if ( mod > 0 || mod == -1 )
1555: hindex = head_pbucket(mod,bucket);
1556: else if ( mod == -2 )
1557: hindex = head_pbucket_lf(bucket);
1558: else
1559: hindex = head_pbucket_q(bucket);
1.157 noro 1560: if ( hindex < 0 ) {
1561: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1562: if ( d ) SG(d) = sugar;
1563: *rp = d;
1564: return 1;
1565: }
1566: g = bucket->body[hindex];
1567: index = ndl_find_reducer(HDL(g));
1568: if ( index >= 0 ) {
1569: count++;
1570: if ( !d ) hcount++;
1571: h = nd_psh[index];
1572: ndl_sub(HDL(g),DL(h),DL(mul));
1573: if ( ndl_check_bound2(index,DL(mul)) ) {
1574: nd_free(d);
1575: free_pbucket(bucket);
1576: *rp = 0;
1577: return 0;
1578: }
1579: p = ps[index];
1580: if ( mod == -1 )
1581: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.232 noro 1582: else if ( mod == -2 ) {
1583: GZ inv,t;
1584: divlf(ONEGZ,HCZ(p),&inv);
1585: chsgnlf(HCZ(g),&t);
1586: mullf(inv,t,&CZ(mul));
1587: } else if ( mod ) {
1.157 noro 1588: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1589: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1590: } else {
1591: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1592: chsgnq(cg,&CQ(mul));
1593: nd_mul_c_q(d,(P)cred);
1594: mulq_pbucket(bucket,cred);
1595: g = bucket->body[hindex];
1596: gmag = (double)p_mag((P)HCQ(g));
1597: }
1598: red = ndv_mul_nm(mod,mul,p);
1599: bucket->body[hindex] = nd_remove_head(g);
1600: red = nd_remove_head(red);
1601: add_pbucket(mod,bucket,red);
1602: psugar = SG(p)+TD(DL(mul));
1603: sugar = MAX(sugar,psugar);
1604: if ( !mod && hmag && (gmag > hmag) ) {
1605: g = normalize_pbucket(mod,bucket);
1606: if ( !g ) {
1607: if ( d ) SG(d) = sugar;
1608: *rp = d;
1609: return 1;
1610: }
1611: nd_removecont2(d,g);
1612: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1613: add_pbucket(mod,bucket,g);
1614: }
1615: } else if ( !full ) {
1616: g = normalize_pbucket(mod,bucket);
1617: if ( g ) SG(g) = sugar;
1618: *rp = g;
1619: return 1;
1620: } else {
1621: m = BDY(g);
1622: if ( NEXT(m) ) {
1623: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1624: } else {
1625: FREEND(g); g = 0;
1626: }
1627: bucket->body[hindex] = g;
1628: NEXT(m) = 0;
1629: if ( d ) {
1630: NEXT(tail)=m; tail=m; LEN(d)++;
1631: } else {
1632: MKND(n,m,1,d); tail = BDY(d);
1633: }
1634: }
1635: }
1.25 noro 1636: }
1.27 noro 1637:
1.61 noro 1638: /* input : list of NDV, cand : list of NDV */
1.28 noro 1639:
1.170 noro 1640: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28 noro 1641: {
1.157 noro 1642: int n,i,stat;
1643: ND nf,d;
1644: NDV r;
1645: NODE t,s;
1646: union oNDC dn;
1.168 noro 1647: Q q;
1648: LIST list;
1.45 noro 1649:
1.172 noro 1650: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1.157 noro 1651: n = length(cand);
1.28 noro 1652:
1.245 noro 1653: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1.157 noro 1654: /* membercheck : list is a subset of Id(cand) ? */
1.168 noro 1655: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1.45 noro 1656: again:
1.245 noro 1657: nd_tracelist = 0;
1.157 noro 1658: if ( nd_bpe > obpe )
1659: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1660: else
1661: r = (NDV)BDY(t);
1.246 ! noro 1662: if ( m == -2 ) ndv_mod(m,r);
1.171 noro 1663: d = ndvtond(m,r);
1664: stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1.157 noro 1665: if ( !stat ) {
1666: nd_reconstruct(0,0);
1667: goto again;
1668: } else if ( nf ) return 0;
1.245 noro 1669: if ( nd_gentrace ) {
1670: nd_tracelist = reverse_node(nd_tracelist);
1671: MKLIST(list,nd_tracelist);
1672: STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1673: MKNODE(s,list,nd_alltracelist);
1674: nd_alltracelist = s; nd_tracelist = 0;
1675: }
1.157 noro 1676: if ( DP_Print ) { printf("."); fflush(stdout); }
1677: }
1678: if ( DP_Print ) { printf("\n"); }
1679: return 1;
1.23 noro 1680: }
1.1 noro 1681:
1682: ND nd_remove_head(ND p)
1683: {
1.157 noro 1684: NM m;
1.1 noro 1685:
1.157 noro 1686: m = BDY(p);
1687: if ( !NEXT(m) ) {
1688: FREEND(p); p = 0;
1689: } else {
1690: BDY(p) = NEXT(m); LEN(p)--;
1691: }
1692: FREENM(m);
1693: return p;
1.1 noro 1694: }
1695:
1.69 noro 1696: ND nd_separate_head(ND p,ND *head)
1697: {
1.157 noro 1698: NM m,m0;
1699: ND r;
1.69 noro 1700:
1.157 noro 1701: m = BDY(p);
1702: if ( !NEXT(m) ) {
1703: *head = p; p = 0;
1704: } else {
1705: m0 = m;
1706: BDY(p) = NEXT(m); LEN(p)--;
1707: NEXT(m0) = 0;
1708: MKND(NV(p),m0,1,r);
1709: *head = r;
1710: }
1711: return p;
1.69 noro 1712: }
1713:
1.1 noro 1714: PGeoBucket create_pbucket()
1715: {
1716: PGeoBucket g;
1.157 noro 1717:
1718: g = CALLOC(1,sizeof(struct oPGeoBucket));
1719: g->m = -1;
1720: return g;
1.1 noro 1721: }
1722:
1.25 noro 1723: void free_pbucket(PGeoBucket b) {
1.157 noro 1724: int i;
1.25 noro 1725:
1.157 noro 1726: for ( i = 0; i <= b->m; i++ )
1727: if ( b->body[i] ) {
1728: nd_free(b->body[i]);
1729: b->body[i] = 0;
1730: }
1.200 noro 1731: GCFREE(b);
1.25 noro 1732: }
1733:
1.63 noro 1734: void add_pbucket_symbolic(PGeoBucket g,ND d)
1735: {
1.157 noro 1736: int l,i,k,m;
1.63 noro 1737:
1.157 noro 1738: if ( !d )
1739: return;
1740: l = LEN(d);
1741: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1742: /* 2^(k-1) < l <= 2^k (=m) */
1743: d = nd_merge(g->body[k],d);
1744: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1745: g->body[k] = 0;
1746: d = nd_merge(g->body[k+1],d);
1747: }
1748: g->body[k] = d;
1749: g->m = MAX(g->m,k);
1.63 noro 1750: }
1751:
1.31 noro 1752: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1 noro 1753: {
1.157 noro 1754: int l,i,k,m;
1.1 noro 1755:
1.157 noro 1756: if ( !d )
1757: return;
1758: l = LEN(d);
1759: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1760: /* 2^(k-1) < l <= 2^k (=m) */
1761: d = nd_add(mod,g->body[k],d);
1762: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1763: g->body[k] = 0;
1764: d = nd_add(mod,g->body[k+1],d);
1765: }
1766: g->body[k] = d;
1767: g->m = MAX(g->m,k);
1.1 noro 1768: }
1769:
1.113 noro 1770: void mulq_pbucket(PGeoBucket g,Q c)
1.26 noro 1771: {
1.157 noro 1772: int k;
1.26 noro 1773:
1.157 noro 1774: for ( k = 0; k <= g->m; k++ )
1775: nd_mul_c_q(g->body[k],(P)c);
1.26 noro 1776: }
1777:
1.63 noro 1778: NM remove_head_pbucket_symbolic(PGeoBucket g)
1779: {
1.157 noro 1780: int j,i,k,c;
1781: NM head;
1782:
1783: k = g->m;
1784: j = -1;
1785: for ( i = 0; i <= k; i++ ) {
1786: if ( !g->body[i] ) continue;
1787: if ( j < 0 ) j = i;
1788: else {
1789: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1790: if ( c > 0 )
1791: j = i;
1792: else if ( c == 0 )
1793: g->body[i] = nd_remove_head(g->body[i]);
1794: }
1795: }
1796: if ( j < 0 ) return 0;
1797: else {
1798: head = BDY(g->body[j]);
1799: if ( !NEXT(head) ) {
1800: FREEND(g->body[j]);
1801: g->body[j] = 0;
1802: } else {
1803: BDY(g->body[j]) = NEXT(head);
1804: LEN(g->body[j])--;
1805: }
1806: return head;
1807: }
1.63 noro 1808: }
1809:
1.19 noro 1810: int head_pbucket(int mod,PGeoBucket g)
1.1 noro 1811: {
1.157 noro 1812: int j,i,c,k,nv,sum;
1813: UINT *di,*dj;
1814: ND gi,gj;
1815:
1816: k = g->m;
1817: while ( 1 ) {
1818: j = -1;
1819: for ( i = 0; i <= k; i++ ) {
1820: if ( !(gi = g->body[i]) )
1821: continue;
1822: if ( j < 0 ) {
1823: j = i;
1824: gj = g->body[j];
1825: dj = HDL(gj);
1826: sum = HCM(gj);
1827: } else {
1828: c = DL_COMPARE(HDL(gi),dj);
1829: if ( c > 0 ) {
1830: if ( sum ) HCM(gj) = sum;
1831: else g->body[j] = nd_remove_head(gj);
1832: j = i;
1833: gj = g->body[j];
1834: dj = HDL(gj);
1835: sum = HCM(gj);
1836: } else if ( c == 0 ) {
1837: if ( mod == -1 )
1838: sum = _addsf(sum,HCM(gi));
1839: else {
1840: sum = sum+HCM(gi)-mod;
1841: if ( sum < 0 ) sum += mod;
1842: }
1843: g->body[i] = nd_remove_head(gi);
1844: }
1845: }
1846: }
1847: if ( j < 0 ) return -1;
1848: else if ( sum ) {
1849: HCM(gj) = sum;
1850: return j;
1851: } else
1852: g->body[j] = nd_remove_head(gj);
1853: }
1.26 noro 1854: }
1855:
1.113 noro 1856: int head_pbucket_q(PGeoBucket g)
1.26 noro 1857: {
1.157 noro 1858: int j,i,c,k,nv;
1859: Q sum,t;
1860: ND gi,gj;
1861:
1862: k = g->m;
1863: while ( 1 ) {
1864: j = -1;
1865: for ( i = 0; i <= k; i++ ) {
1866: if ( !(gi = g->body[i]) ) continue;
1867: if ( j < 0 ) {
1868: j = i;
1869: gj = g->body[j];
1870: sum = HCQ(gj);
1871: } else {
1872: nv = NV(gi);
1873: c = DL_COMPARE(HDL(gi),HDL(gj));
1874: if ( c > 0 ) {
1875: if ( sum ) HCQ(gj) = sum;
1876: else g->body[j] = nd_remove_head(gj);
1877: j = i;
1878: gj = g->body[j];
1879: sum = HCQ(gj);
1880: } else if ( c == 0 ) {
1881: addq(sum,HCQ(gi),&t);
1882: sum = t;
1883: g->body[i] = nd_remove_head(gi);
1884: }
1885: }
1886: }
1887: if ( j < 0 ) return -1;
1888: else if ( sum ) {
1889: HCQ(gj) = sum;
1890: return j;
1891: } else
1892: g->body[j] = nd_remove_head(gj);
1893: }
1.1 noro 1894: }
1895:
1.232 noro 1896: int head_pbucket_lf(PGeoBucket g)
1897: {
1898: int j,i,c,k,nv;
1899: GZ sum,t;
1900: ND gi,gj;
1901:
1902: k = g->m;
1903: while ( 1 ) {
1904: j = -1;
1905: for ( i = 0; i <= k; i++ ) {
1906: if ( !(gi = g->body[i]) ) continue;
1907: if ( j < 0 ) {
1908: j = i;
1909: gj = g->body[j];
1910: sum = HCZ(gj);
1911: } else {
1912: nv = NV(gi);
1913: c = DL_COMPARE(HDL(gi),HDL(gj));
1914: if ( c > 0 ) {
1915: if ( sum ) HCZ(gj) = sum;
1916: else g->body[j] = nd_remove_head(gj);
1917: j = i;
1918: gj = g->body[j];
1919: sum = HCZ(gj);
1920: } else if ( c == 0 ) {
1921: addlf(sum,HCZ(gi),&t);
1922: sum = t;
1923: g->body[i] = nd_remove_head(gi);
1924: }
1925: }
1926: }
1927: if ( j < 0 ) return -1;
1928: else if ( sum ) {
1929: HCZ(gj) = sum;
1930: return j;
1931: } else
1932: g->body[j] = nd_remove_head(gj);
1933: }
1934: }
1935:
1.25 noro 1936: ND normalize_pbucket(int mod,PGeoBucket g)
1.1 noro 1937: {
1.157 noro 1938: int i;
1939: ND r,t;
1.1 noro 1940:
1.157 noro 1941: r = 0;
1942: for ( i = 0; i <= g->m; i++ ) {
1943: r = nd_add(mod,r,g->body[i]);
1944: g->body[i] = 0;
1945: }
1946: g->m = -1;
1947: return r;
1.1 noro 1948: }
1949:
1.150 noro 1950: #if 0
1951: void register_hcf(NDV p)
1952: {
1.157 noro 1953: DCP dc,t;
1954: P hc,h;
1955: int c;
1956: NODE l,l1,prev;
1957:
1958: hc = p->body->c.p;
1959: if ( !nd_vc || NUM(hc) ) return;
1960: fctrp(nd_vc,hc,&dc);
1961: for ( t = dc; t; t = NEXT(t) ) {
1962: h = t->c;
1963: if ( NUM(h) ) continue;
1964: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1965: c = compp(nd_vc,h,(P)BDY(l));
1966: if ( c >= 0 ) break;
1967: }
1968: if ( !l || c > 0 ) {
1969: MKNODE(l1,h,l);
1970: if ( !prev )
1971: nd_hcf = l1;
1972: else
1973: NEXT(prev) = l1;
1974: }
1975: }
1.150 noro 1976: }
1977: #else
1978: void register_hcf(NDV p)
1979: {
1.157 noro 1980: DCP dc,t;
1981: P hc,h,q;
1982: Q dmy;
1983: int c;
1984: NODE l,l1,prev;
1985:
1986: hc = p->body->c.p;
1987: if ( NUM(hc) ) return;
1988: ptozp(hc,1,&dmy,&h);
1.150 noro 1989: #if 1
1.157 noro 1990: for ( l = nd_hcf; l; l = NEXT(l) ) {
1991: while ( 1 ) {
1992: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
1993: else break;
1994: }
1995: }
1996: if ( NUM(h) ) return;
1.150 noro 1997: #endif
1.157 noro 1998: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1999: c = compp(nd_vc,h,(P)BDY(l));
2000: if ( c >= 0 ) break;
2001: }
2002: if ( !l || c > 0 ) {
2003: MKNODE(l1,h,l);
2004: if ( !prev )
2005: nd_hcf = l1;
2006: else
2007: NEXT(prev) = l1;
2008: }
1.150 noro 2009: }
2010: #endif
2011:
1.122 noro 2012: int do_diagonalize(int sugar,int m)
1.92 noro 2013: {
1.157 noro 2014: int i,nh,stat;
2015: NODE r,g,t;
2016: ND h,nf,s,head;
2017: NDV nfv;
2018: Q q,num,den;
1.167 noro 2019: P nm,nmp,dn,mnp,dnp,cont,cont1;
2020: union oNDC hc;
2021: NODE node;
2022: LIST l;
2023: Q iq;
1.157 noro 2024:
2025: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172 noro 2026: if ( nd_gentrace ) {
1.167 noro 2027: /* Trace = [1,index,1,1] */
2028: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2029: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2030: }
1.157 noro 2031: if ( nd_demand )
2032: nfv = ndv_load(i);
2033: else
2034: nfv = nd_ps[i];
2035: s = ndvtond(m,nfv);
2036: s = nd_separate_head(s,&head);
2037: stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
2038: if ( !stat ) return 0;
2039: ndv_free(nfv);
1.167 noro 2040: hc = HCU(nf); nd_removecont(m,nf);
2041: cont = ndc_div(m,hc,HCU(nf));
1.245 noro 2042: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157 noro 2043: nfv = ndtondv(m,nf);
2044: nd_free(nf);
2045: nd_bound[i] = ndv_compute_bound(nfv);
2046: if ( !m ) register_hcf(nfv);
2047: if ( nd_demand ) {
2048: ndv_save(nfv,i);
2049: ndv_free(nfv);
2050: } else
2051: nd_ps[i] = nfv;
2052: }
2053: return 1;
1.92 noro 2054: }
2055:
1.209 noro 2056: LIST compute_splist()
2057: {
1.245 noro 2058: NODE g,tn0,tn,node;
2059: LIST l0;
2060: ND_pairs d,t;
2061: int i;
2062: Q i1,i2;
1.209 noro 2063:
2064: g = 0; d = 0;
2065: for ( i = 0; i < nd_psn; i++ ) {
2066: d = update_pairs(d,g,i,0);
2067: g = update_base(g,i);
2068: }
1.245 noro 2069: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2070: NEXTNODE(tn0,tn);
1.209 noro 2071: STOQ(t->i1,i1); STOQ(t->i2,i2);
2072: node = mknode(2,i1,i2); MKLIST(l0,node);
1.245 noro 2073: BDY(tn) = l0;
2074: }
2075: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2076: return l0;
1.209 noro 2077: }
2078:
1.27 noro 2079: /* return value = 0 => input is not a GB */
2080:
1.168 noro 2081: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
1.1 noro 2082: {
1.157 noro 2083: int i,nh,sugar,stat;
2084: NODE r,g,t;
2085: ND_pairs d;
2086: ND_pairs l;
2087: ND h,nf,s,head,nf1;
2088: NDV nfv;
2089: Q q,num,den;
1.167 noro 2090: union oNDC dn,hc;
1.157 noro 2091: int diag_count = 0;
1.167 noro 2092: P cont;
2093: LIST list;
1.157 noro 2094:
2095: g = 0; d = 0;
2096: for ( i = 0; i < nd_psn; i++ ) {
1.168 noro 2097: d = update_pairs(d,g,i,gensyz);
1.157 noro 2098: g = update_base(g,i);
2099: }
2100: sugar = 0;
2101: while ( d ) {
1.1 noro 2102: again:
1.157 noro 2103: l = nd_minp(d,&d);
1.228 noro 2104: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157 noro 2105: if ( SG(l) != sugar ) {
2106: if ( ishomo ) {
2107: diag_count = 0;
2108: stat = do_diagonalize(sugar,m);
2109: if ( !stat ) {
2110: NEXT(l) = d; d = l;
2111: d = nd_reconstruct(0,d);
2112: goto again;
2113: }
2114: }
2115: sugar = SG(l);
2116: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2117: }
2118: stat = nd_sp(m,0,l,&h);
2119: if ( !stat ) {
2120: NEXT(l) = d; d = l;
2121: d = nd_reconstruct(0,d);
2122: goto again;
2123: }
1.41 noro 2124: #if USE_GEOBUCKET
1.172 noro 2125: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
1.167 noro 2126: :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41 noro 2127: #else
1.157 noro 2128: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41 noro 2129: #endif
1.157 noro 2130: if ( !stat ) {
2131: NEXT(l) = d; d = l;
2132: d = nd_reconstruct(0,d);
2133: goto again;
2134: } else if ( nf ) {
1.168 noro 2135: if ( checkonly || gensyz ) return 0;
1.245 noro 2136: if ( nd_newelim ) {
2137: if ( nd_module ) {
2138: if ( MPOS(HDL(nf)) > 1 ) return 0;
2139: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2140: }
1.157 noro 2141: if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167 noro 2142: hc = HCU(nf);
1.157 noro 2143: nd_removecont(m,nf);
2144: if ( !m && nd_nalg ) {
2145: nd_monic(0,&nf);
2146: nd_removecont(m,nf);
2147: }
1.172 noro 2148: if ( nd_gentrace ) {
1.245 noro 2149: cont = ndc_div(m,hc,HCU(nf));
2150: if ( m || !UNIQ(cont) ) {
1.196 noro 2151: t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167 noro 2152: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.245 noro 2153: nd_tracelist = t;
2154: }
1.167 noro 2155: }
1.157 noro 2156: nfv = ndtondv(m,nf); nd_free(nf);
1.215 noro 2157: nh = ndv_newps(m,nfv,0,0);
1.157 noro 2158: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2159: diag_count = 0;
2160: stat = do_diagonalize(sugar,m);
2161: if ( !stat ) {
2162: NEXT(l) = d; d = l;
2163: d = nd_reconstruct(1,d);
2164: goto again;
2165: }
2166: }
1.168 noro 2167: d = update_pairs(d,g,nh,0);
1.157 noro 2168: g = update_base(g,nh);
2169: FREENDP(l);
2170: } else {
1.245 noro 2171: if ( nd_gentrace && gensyz ) {
1.168 noro 2172: nd_tracelist = reverse_node(nd_tracelist);
1.245 noro 2173: MKLIST(list,nd_tracelist);
1.168 noro 2174: STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2175: MKNODE(t,list,nd_alltracelist);
1.245 noro 2176: nd_alltracelist = t; nd_tracelist = 0;
2177: }
1.157 noro 2178: if ( DP_Print ) { printf("."); fflush(stdout); }
2179: FREENDP(l);
2180: }
2181: }
1.245 noro 2182: conv_ilist(nd_demand,0,g,indp);
1.157 noro 2183: if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
2184: return g;
1.1 noro 2185: }
2186:
1.209 noro 2187: /* splist = [[i1,i2],...] */
2188:
2189: int check_splist(int m,NODE splist)
2190: {
1.245 noro 2191: NODE t,p;
2192: ND_pairs d,r,l;
2193: int stat;
2194: ND h,nf;
1.209 noro 2195:
1.245 noro 2196: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2197: p = BDY((LIST)BDY(t));
1.209 noro 2198: NEXTND_pairs(d,r);
2199: r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
2200: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.245 noro 2201: SG(r) = TD(LCM(r)); /* XXX */
2202: }
2203: if ( d ) NEXT(r) = 0;
1.209 noro 2204:
2205: while ( d ) {
2206: again:
2207: l = nd_minp(d,&d);
2208: stat = nd_sp(m,0,l,&h);
2209: if ( !stat ) {
2210: NEXT(l) = d; d = l;
2211: d = nd_reconstruct(0,d);
2212: goto again;
2213: }
2214: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
2215: if ( !stat ) {
2216: NEXT(l) = d; d = l;
2217: d = nd_reconstruct(0,d);
2218: goto again;
2219: } else if ( nf ) return 0;
1.245 noro 2220: if ( DP_Print) { printf("."); fflush(stdout); }
1.209 noro 2221: }
1.245 noro 2222: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2223: return 1;
1.209 noro 2224: }
2225:
1.214 noro 2226: int check_splist_f4(int m,NODE splist)
2227: {
1.245 noro 2228: UINT *s0vect;
1.214 noro 2229: PGeoBucket bucket;
1.245 noro 2230: NODE p,rp0,t;
2231: ND_pairs d,r,l,ll;
2232: int col,stat;
1.214 noro 2233:
1.245 noro 2234: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2235: p = BDY((LIST)BDY(t));
1.214 noro 2236: NEXTND_pairs(d,r);
2237: r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
2238: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.245 noro 2239: SG(r) = TD(LCM(r)); /* XXX */
2240: }
2241: if ( d ) NEXT(r) = 0;
1.214 noro 2242:
2243: while ( d ) {
2244: l = nd_minsugarp(d,&d);
2245: bucket = create_pbucket();
2246: stat = nd_sp_f4(m,0,l,bucket);
2247: if ( !stat ) {
2248: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2249: NEXT(ll) = d; d = l;
2250: d = nd_reconstruct(0,d);
2251: continue;
2252: }
2253: if ( bucket->m < 0 ) continue;
2254: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2255: if ( !col ) {
2256: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2257: NEXT(ll) = d; d = l;
2258: d = nd_reconstruct(0,d);
2259: continue;
2260: }
2261: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2262: }
2263: return 1;
2264: }
2265:
1.122 noro 2266: int do_diagonalize_trace(int sugar,int m)
1.91 noro 2267: {
1.157 noro 2268: int i,nh,stat;
2269: NODE r,g,t;
2270: ND h,nf,nfq,s,head;
2271: NDV nfv,nfqv;
2272: Q q,den,num;
1.167 noro 2273: union oNDC hc;
2274: NODE node;
2275: LIST l;
2276: Q iq;
2277: P cont,cont1;
1.157 noro 2278:
2279: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172 noro 2280: if ( nd_gentrace ) {
1.167 noro 2281: /* Trace = [1,index,1,1] */
2282: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2283: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2284: }
1.157 noro 2285: /* for nd_ps */
2286: s = ndvtond(m,nd_ps[i]);
2287: s = nd_separate_head(s,&head);
2288: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2289: if ( !stat ) return 0;
2290: nf = nd_add(m,head,nf);
2291: ndv_free(nd_ps[i]);
2292: nd_ps[i] = ndtondv(m,nf);
2293: nd_free(nf);
2294:
2295: /* for nd_ps_trace */
2296: if ( nd_demand )
2297: nfv = ndv_load(i);
2298: else
2299: nfv = nd_ps_trace[i];
2300: s = ndvtond(0,nfv);
2301: s = nd_separate_head(s,&head);
2302: stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
2303: if ( !stat ) return 0;
2304: ndv_free(nfv);
1.167 noro 2305: hc = HCU(nf); nd_removecont(0,nf);
1.245 noro 2306: cont = ndc_div(0,hc,HCU(nf));
1.172 noro 2307: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157 noro 2308: nfv = ndtondv(0,nf);
2309: nd_free(nf);
2310: nd_bound[i] = ndv_compute_bound(nfv);
2311: register_hcf(nfv);
2312: if ( nd_demand ) {
2313: ndv_save(nfv,i);
2314: ndv_free(nfv);
2315: } else
2316: nd_ps_trace[i] = nfv;
2317: }
2318: return 1;
1.91 noro 2319: }
2320:
1.118 noro 2321: static struct oEGT eg_invdalg;
2322: struct oEGT eg_le;
2323:
1.147 noro 2324: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2325: {
1.157 noro 2326: NODE tn;
2327: P p1;
1.147 noro 2328:
1.157 noro 2329: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2330: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2331: }
2332: *r = p;
1.147 noro 2333: }
2334:
1.167 noro 2335: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20 noro 2336: {
1.157 noro 2337: int i,nh,sugar,stat;
2338: NODE r,g,t;
2339: ND_pairs d;
2340: ND_pairs l;
2341: ND h,nf,nfq,s,head;
2342: NDV nfv,nfqv;
2343: Q q,den,num;
2344: P hc;
1.167 noro 2345: union oNDC dn,hnfq;
1.157 noro 2346: struct oEGT eg_monic,egm0,egm1;
2347: int diag_count = 0;
1.167 noro 2348: P cont;
2349: LIST list;
1.157 noro 2350:
2351: init_eg(&eg_monic);
2352: init_eg(&eg_invdalg);
2353: init_eg(&eg_le);
2354: g = 0; d = 0;
2355: for ( i = 0; i < nd_psn; i++ ) {
1.168 noro 2356: d = update_pairs(d,g,i,0);
1.157 noro 2357: g = update_base(g,i);
2358: }
2359: sugar = 0;
2360: while ( d ) {
1.20 noro 2361: again:
1.157 noro 2362: l = nd_minp(d,&d);
1.228 noro 2363: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157 noro 2364: if ( SG(l) != sugar ) {
1.130 noro 2365: #if 1
1.157 noro 2366: if ( ishomo ) {
2367: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2368: stat = do_diagonalize_trace(sugar,m);
2369: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2370: diag_count = 0;
2371: if ( !stat ) {
2372: NEXT(l) = d; d = l;
2373: d = nd_reconstruct(1,d);
2374: goto again;
2375: }
2376: }
1.130 noro 2377: #endif
1.157 noro 2378: sugar = SG(l);
2379: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2380: }
2381: stat = nd_sp(m,0,l,&h);
2382: if ( !stat ) {
2383: NEXT(l) = d; d = l;
2384: d = nd_reconstruct(1,d);
2385: goto again;
2386: }
1.41 noro 2387: #if USE_GEOBUCKET
1.157 noro 2388: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41 noro 2389: #else
1.157 noro 2390: stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41 noro 2391: #endif
1.157 noro 2392: if ( !stat ) {
2393: NEXT(l) = d; d = l;
2394: d = nd_reconstruct(1,d);
2395: goto again;
2396: } else if ( nf ) {
2397: if ( nd_demand ) {
2398: nfqv = ndv_load(nd_psn);
2399: nfq = ndvtond(0,nfqv);
2400: } else
2401: nfq = 0;
2402: if ( !nfq ) {
2403: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
2404: NEXT(l) = d; d = l;
2405: d = nd_reconstruct(1,d);
2406: goto again;
2407: }
2408: }
2409: if ( nfq ) {
2410: /* m|HC(nfq) => failure */
2411: if ( nd_vc ) {
2412: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
2413: } else
2414: q = HCQ(nfq);
2415: if ( !rem(NM(q),m) ) return 0;
2416:
2417: if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167 noro 2418: hnfq = HCU(nfq);
1.157 noro 2419: if ( nd_nalg ) {
2420: /* m|DN(HC(nf)^(-1)) => failure */
2421: get_eg(&egm0);
2422: if ( !nd_monic(m,&nfq) ) return 0;
2423: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2424: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2425: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2426: } else {
2427: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2428: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2429: }
1.172 noro 2430: if ( nd_gentrace ) {
1.245 noro 2431: cont = ndc_div(0,hnfq,HCU(nfqv));
2432: if ( !UNIQ(cont) ) {
1.196 noro 2433: t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167 noro 2434: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.245 noro 2435: nd_tracelist = t;
2436: }
1.167 noro 2437: }
1.215 noro 2438: nh = ndv_newps(0,nfv,nfqv,0);
1.157 noro 2439: if ( ishomo && ++diag_count == diag_period ) {
2440: diag_count = 0;
2441: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2442: stat = do_diagonalize_trace(sugar,m);
2443: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2444: if ( !stat ) {
2445: NEXT(l) = d; d = l;
2446: d = nd_reconstruct(1,d);
2447: goto again;
2448: }
2449: }
1.168 noro 2450: d = update_pairs(d,g,nh,0);
1.157 noro 2451: g = update_base(g,nh);
2452: } else {
2453: if ( DP_Print ) { printf("*"); fflush(stdout); }
2454: }
2455: } else {
2456: if ( DP_Print ) { printf("."); fflush(stdout); }
2457: }
2458: FREENDP(l);
2459: }
2460: if ( nd_nalg ) {
1.227 noro 2461: if ( DP_Print ) {
2462: print_eg("monic",&eg_monic);
2463: print_eg("invdalg",&eg_invdalg);
2464: print_eg("le",&eg_le);
2465: }
1.157 noro 2466: }
1.245 noro 2467: conv_ilist(nd_demand,1,g,indp);
1.157 noro 2468: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2469: return g;
1.20 noro 2470: }
2471:
1.23 noro 2472: int ndv_compare(NDV *p1,NDV *p2)
2473: {
1.157 noro 2474: return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23 noro 2475: }
2476:
2477: int ndv_compare_rev(NDV *p1,NDV *p2)
2478: {
1.157 noro 2479: return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23 noro 2480: }
2481:
1.167 noro 2482: int ndvi_compare(NDVI p1,NDVI p2)
2483: {
2484: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2485: }
2486:
2487: int ndvi_compare_rev(NDVI p1,NDVI p2)
2488: {
2489: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2490: }
2491:
1.61 noro 2492: NODE ndv_reduceall(int m,NODE f)
1.23 noro 2493: {
1.167 noro 2494: int i,j,n,stat;
1.157 noro 2495: ND nf,g,head;
2496: NODE t,a0,a;
2497: union oNDC dn;
2498: Q q,num,den;
1.167 noro 2499: NODE node;
1.245 noro 2500: LIST l;
2501: Q iq,jq;
1.167 noro 2502: int *perm;
2503: union oNDC hc;
2504: P cont,cont1;
1.23 noro 2505:
1.173 noro 2506: if ( nd_nora ) return f;
1.157 noro 2507: n = length(f);
2508: ndv_setup(m,0,f,0,1);
1.245 noro 2509: perm = (int *)MALLOC(n*sizeof(int));
2510: if ( nd_gentrace ) {
2511: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
2512: perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
2513: }
1.157 noro 2514: for ( i = 0; i < n; ) {
1.172 noro 2515: if ( nd_gentrace ) {
1.167 noro 2516: /* Trace = [1,index,1,1] */
2517: STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2518: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2519: }
1.157 noro 2520: g = ndvtond(m,nd_ps[i]);
2521: g = nd_separate_head(g,&head);
2522: stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
2523: if ( !stat )
2524: nd_reconstruct(0,0);
2525: else {
2526: if ( DP_Print ) { printf("."); fflush(stdout); }
2527: ndv_free(nd_ps[i]);
1.167 noro 2528: hc = HCU(nf); nd_removecont(m,nf);
1.172 noro 2529: if ( nd_gentrace ) {
1.245 noro 2530: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.167 noro 2531: jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
2532: STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
2533: }
2534: cont = ndc_div(m,hc,HCU(nf));
2535: finalize_tracelist(perm[i],cont);
2536: }
1.157 noro 2537: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2538: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2539: i++;
2540: }
2541: }
2542: if ( DP_Print ) { printf("\n"); }
2543: for ( a0 = 0, i = 0; i < n; i++ ) {
2544: NEXTNODE(a0,a);
1.245 noro 2545: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2546: else {
2547: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2548: BDY(a) = (pointer)nd_ps[j];
2549: }
1.157 noro 2550: }
2551: NEXT(a) = 0;
2552: return a0;
1.23 noro 2553: }
2554:
1.168 noro 2555: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1 noro 2556: {
1.157 noro 2557: ND_pairs d1,nd,cur,head,prev,remove;
1.1 noro 2558:
1.157 noro 2559: if ( !g ) return d;
1.245 noro 2560: /* for testing */
2561: if ( gensyz && nd_gensyz == 2 ) {
2562: d1 = nd_newpairs(g,t);
2563: if ( !d )
2564: return d1;
2565: else {
2566: nd = d;
2567: while ( NEXT(nd) ) nd = NEXT(nd);
2568: NEXT(nd) = d1;
2569: return d;
2570: }
2571: }
1.157 noro 2572: d = crit_B(d,t);
2573: d1 = nd_newpairs(g,t);
2574: d1 = crit_M(d1);
2575: d1 = crit_F(d1);
1.168 noro 2576: if ( gensyz || do_weyl )
1.157 noro 2577: head = d1;
2578: else {
2579: prev = 0; cur = head = d1;
2580: while ( cur ) {
2581: if ( crit_2( cur->i1,cur->i2 ) ) {
2582: remove = cur;
2583: if ( !prev ) head = cur = NEXT(cur);
2584: else cur = NEXT(prev) = NEXT(cur);
2585: FREENDP(remove);
2586: } else {
2587: prev = cur; cur = NEXT(cur);
2588: }
2589: }
2590: }
2591: if ( !d )
2592: return head;
2593: else {
2594: nd = d;
2595: while ( NEXT(nd) ) nd = NEXT(nd);
2596: NEXT(nd) = head;
2597: return d;
2598: }
1.1 noro 2599: }
2600:
1.157 noro 2601:
1.1 noro 2602: ND_pairs nd_newpairs( NODE g, int t )
2603: {
1.157 noro 2604: NODE h;
2605: UINT *dl;
1.187 noro 2606: int ts,s,i,t0,min,max;
1.157 noro 2607: ND_pairs r,r0;
2608:
2609: dl = DL(nd_psh[t]);
2610: ts = SG(nd_psh[t]) - TD(dl);
1.245 noro 2611: if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
1.157 noro 2612: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159 noro 2613: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157 noro 2614: continue;
1.245 noro 2615: if ( nd_gbblock ) {
2616: t0 = (long)BDY(h);
2617: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2618: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2619: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2620: break;
2621: }
2622: if ( nd_gbblock[i] >= 0 )
2623: continue;
2624: }
1.157 noro 2625: NEXTND_pairs(r0,r);
1.159 noro 2626: r->i1 = (long)BDY(h);
1.157 noro 2627: r->i2 = t;
2628: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2629: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2630: SG(r) = MAX(s,ts) + TD(LCM(r));
1.231 noro 2631: /* experimental */
2632: if ( nd_sugarweight )
2633: r->sugar2 = ndl_weight2(r->lcm);
1.157 noro 2634: }
2635: if ( r0 ) NEXT(r) = 0;
2636: return r0;
1.1 noro 2637: }
2638:
1.231 noro 2639: /* ipair = [i1,i2],[i1,i2],... */
2640: ND_pairs nd_ipairtospair(NODE ipair)
2641: {
2642: int s1,s2;
2643: NODE tn,t;
2644: ND_pairs r,r0;
2645:
2646: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2647: NEXTND_pairs(r0,r);
2648: tn = BDY((LIST)BDY(t));
2649: r->i1 = QTOS((Q)ARG0(tn));
2650: r->i2 = QTOS((Q)ARG1(tn));
2651: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2652: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2653: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2654: SG(r) = MAX(s1,s2) + TD(LCM(r));
2655: /* experimental */
2656: if ( nd_sugarweight )
2657: r->sugar2 = ndl_weight2(r->lcm);
2658: }
2659: if ( r0 ) NEXT(r) = 0;
2660: return r0;
2661: }
2662:
1.157 noro 2663: /* kokokara */
2664:
1.1 noro 2665: ND_pairs crit_B( ND_pairs d, int s )
2666: {
1.157 noro 2667: ND_pairs cur,head,prev,remove;
2668: UINT *t,*tl,*lcm;
2669: int td,tdl;
2670:
2671: if ( !d ) return 0;
2672: t = DL(nd_psh[s]);
2673: prev = 0;
2674: head = cur = d;
1.235 noro 2675: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157 noro 2676: while ( cur ) {
2677: tl = cur->lcm;
1.163 noro 2678: if ( ndl_reducible(tl,t) ) {
2679: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167 noro 2680: if ( !ndl_equal(lcm,tl) ) {
2681: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2682: if (!ndl_equal(lcm,tl)) {
2683: remove = cur;
2684: if ( !prev ) {
2685: head = cur = NEXT(cur);
2686: } else {
2687: cur = NEXT(prev) = NEXT(cur);
2688: }
2689: FREENDP(remove);
2690: } else {
2691: prev = cur; cur = NEXT(cur);
2692: }
2693: } else {
2694: prev = cur; cur = NEXT(cur);
2695: }
1.157 noro 2696: } else {
2697: prev = cur; cur = NEXT(cur);
2698: }
2699: }
2700: return head;
1.1 noro 2701: }
2702:
2703: ND_pairs crit_M( ND_pairs d1 )
2704: {
1.157 noro 2705: ND_pairs e,d2,d3,dd,p;
2706: UINT *id,*jd;
1.1 noro 2707:
1.157 noro 2708: if ( !d1 ) return d1;
2709: for ( dd = 0, e = d1; e; e = d3 ) {
2710: if ( !(d2 = NEXT(e)) ) {
2711: NEXT(e) = dd;
2712: return e;
2713: }
2714: id = LCM(e);
2715: for ( d3 = 0; d2; d2 = p ) {
2716: p = NEXT(d2);
2717: jd = LCM(d2);
2718: if ( ndl_equal(jd,id) )
2719: ;
2720: else if ( TD(jd) > TD(id) )
2721: if ( ndl_reducible(jd,id) ) continue;
2722: else ;
2723: else if ( ndl_reducible(id,jd) ) goto delit;
2724: NEXT(d2) = d3;
2725: d3 = d2;
2726: }
2727: NEXT(e) = dd;
2728: dd = e;
2729: continue;
2730: /**/
2731: delit: NEXT(d2) = d3;
2732: d3 = d2;
2733: for ( ; p; p = d2 ) {
2734: d2 = NEXT(p);
2735: NEXT(p) = d3;
2736: d3 = p;
2737: }
2738: FREENDP(e);
2739: }
2740: return dd;
1.1 noro 2741: }
2742:
2743: ND_pairs crit_F( ND_pairs d1 )
2744: {
1.157 noro 2745: ND_pairs rest, head,remove;
2746: ND_pairs last, p, r, w;
2747: int s;
2748:
2749: if ( !d1 ) return d1;
2750: for ( head = last = 0, p = d1; NEXT(p); ) {
2751: r = w = equivalent_pairs(p,&rest);
2752: s = SG(r);
2753: w = NEXT(w);
2754: while ( w ) {
2755: if ( crit_2(w->i1,w->i2) ) {
2756: r = w;
2757: w = NEXT(w);
2758: while ( w ) {
2759: remove = w;
2760: w = NEXT(w);
2761: FREENDP(remove);
2762: }
2763: break;
2764: } else if ( SG(w) < s ) {
2765: FREENDP(r);
2766: r = w;
2767: s = SG(r);
2768: w = NEXT(w);
2769: } else {
2770: remove = w;
2771: w = NEXT(w);
2772: FREENDP(remove);
2773: }
2774: }
2775: if ( last ) NEXT(last) = r;
2776: else head = r;
2777: NEXT(last = r) = 0;
2778: p = rest;
2779: if ( !p ) return head;
2780: }
2781: if ( !last ) return p;
2782: NEXT(last) = p;
2783: return head;
1.1 noro 2784: }
2785:
2786: int crit_2( int dp1, int dp2 )
2787: {
1.157 noro 2788: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1 noro 2789: }
2790:
1.40 noro 2791: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1 noro 2792: {
1.157 noro 2793: ND_pairs w,p,r,s;
2794: UINT *d;
1.1 noro 2795:
1.157 noro 2796: w = d1;
2797: d = LCM(w);
2798: s = NEXT(w);
2799: NEXT(w) = 0;
2800: for ( r = 0; s; s = p ) {
2801: p = NEXT(s);
2802: if ( ndl_equal(d,LCM(s)) ) {
2803: NEXT(s) = w; w = s;
2804: } else {
2805: NEXT(s) = r; r = s;
2806: }
2807: }
2808: *prest = r;
2809: return w;
1.1 noro 2810: }
2811:
2812: NODE update_base(NODE nd,int ndp)
2813: {
1.157 noro 2814: UINT *dl, *dln;
2815: NODE last, p, head;
1.1 noro 2816:
1.157 noro 2817: dl = DL(nd_psh[ndp]);
2818: for ( head = last = 0, p = nd; p; ) {
1.159 noro 2819: dln = DL(nd_psh[(long)BDY(p)]);
1.157 noro 2820: if ( ndl_reducible( dln, dl ) ) {
2821: p = NEXT(p);
2822: if ( last ) NEXT(last) = p;
2823: } else {
2824: if ( !last ) head = p;
2825: p = NEXT(last = p);
2826: }
2827: }
2828: head = append_one(head,ndp);
2829: return head;
1.1 noro 2830: }
2831:
2832: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2833: {
1.157 noro 2834: ND_pairs m,ml,p,l;
2835: UINT *lcm;
2836: int s,td,len,tlen,c,c1;
2837:
2838: if ( !(p = NEXT(m = d)) ) {
2839: *prest = p;
2840: NEXT(m) = 0;
2841: return m;
2842: }
2843: if ( !NoSugar ) {
1.231 noro 2844: if ( nd_sugarweight ) {
2845: s = m->sugar2;
2846: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2847: if ( (p->sugar2 < s)
2848: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2849: ml = l; m = p; s = m->sugar2;
2850: }
2851: } else {
2852: s = SG(m);
2853: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2854: if ( (SG(p) < s)
2855: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2856: ml = l; m = p; s = SG(m);
2857: }
2858: }
1.157 noro 2859: } else {
2860: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2861: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2862: ml = l; m = p; s = SG(m);
2863: }
2864: }
2865: if ( !ml ) *prest = NEXT(m);
2866: else {
2867: NEXT(ml) = NEXT(m);
2868: *prest = d;
2869: }
2870: NEXT(m) = 0;
2871: return m;
1.1 noro 2872: }
2873:
1.63 noro 2874: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2875: {
1.157 noro 2876: int msugar,i;
2877: ND_pairs t,dm0,dm,dr0,dr;
1.63 noro 2878:
1.231 noro 2879: if ( nd_sugarweight ) {
2880: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2881: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2882: dm0 = 0; dr0 = 0;
2883: for ( i = 0, t = d; t; t = NEXT(t) )
2884: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2885: if ( dm0 ) NEXT(dm) = t;
2886: else dm0 = t;
2887: dm = t;
2888: i++;
2889: } else {
2890: if ( dr0 ) NEXT(dr) = t;
2891: else dr0 = t;
2892: dr = t;
2893: }
2894: } else {
2895: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2896: if ( SG(t) < msugar ) msugar = SG(t);
2897: dm0 = 0; dr0 = 0;
2898: for ( i = 0, t = d; t; t = NEXT(t) )
2899: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2900: if ( dm0 ) NEXT(dm) = t;
2901: else dm0 = t;
2902: dm = t;
2903: i++;
2904: } else {
2905: if ( dr0 ) NEXT(dr) = t;
2906: else dr0 = t;
2907: dr = t;
2908: }
2909: }
1.157 noro 2910: NEXT(dm) = 0;
2911: if ( dr0 ) NEXT(dr) = 0;
2912: *prest = dr0;
2913: return dm0;
1.63 noro 2914: }
2915:
1.239 noro 2916: int nd_tdeg(NDV c)
2917: {
2918: int wmax = 0;
2919: int i,len;
2920: NMV a;
2921:
2922: len = LEN(c);
2923: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2924: wmax = MAX(TD(DL(a)),wmax);
2925: return wmax;
2926: }
2927:
1.215 noro 2928: int ndv_newps(int m,NDV a,NDV aq,int f4)
1.1 noro 2929: {
1.157 noro 2930: int len;
2931: RHist r;
2932: NDV b;
1.167 noro 2933: NODE tn;
2934: LIST l;
2935: Q iq;
1.157 noro 2936:
2937: if ( nd_psn == nd_pslen ) {
2938: nd_pslen *= 2;
2939: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.215 noro 2940: nd_ps_gz = (NDV *)REALLOC((char *)nd_ps_gz,nd_pslen*sizeof(NDV));
1.157 noro 2941: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2942: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2943: nd_bound = (UINT **)
2944: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.215 noro 2945: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2946: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
1.157 noro 2947: }
2948: NEWRHist(r); nd_psh[nd_psn] = r;
2949: nd_ps[nd_psn] = a;
2950: if ( aq ) {
2951: nd_ps_trace[nd_psn] = aq;
1.234 noro 2952: if ( !m ) {
1.245 noro 2953: if ( !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(aq);
1.234 noro 2954: register_hcf(aq);
2955: } else if ( m == -2 ) {
2956: /* do nothing */
2957: } else
2958: error("ndv_newps : invalud modulus");
1.157 noro 2959: nd_bound[nd_psn] = ndv_compute_bound(aq);
1.239 noro 2960: #if 1
2961: SG(r) = SG(aq);
2962: #else
2963: SG(r) = nd_tdeg(aq);
2964: #endif
2965: ndl_copy(HDL(aq),DL(r));
1.157 noro 2966: } else {
2967: if ( !m ) register_hcf(a);
2968: nd_bound[nd_psn] = ndv_compute_bound(a);
1.239 noro 2969: #if 1
2970: SG(r) = SG(a);
2971: #else
2972: SG(r) = nd_tdeg(a);
2973: #endif
2974: ndl_copy(HDL(a),DL(r));
1.245 noro 2975: if ( !m && !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(a);
1.157 noro 2976: }
2977: if ( nd_demand ) {
2978: if ( aq ) {
2979: ndv_save(nd_ps_trace[nd_psn],nd_psn);
1.235 noro 2980: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.215 noro 2981: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.157 noro 2982: nd_ps_trace[nd_psn] = 0;
2983: } else {
2984: ndv_save(nd_ps[nd_psn],nd_psn);
1.215 noro 2985: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
1.157 noro 2986: nd_ps[nd_psn] = 0;
2987: }
2988: }
1.172 noro 2989: if ( nd_gentrace ) {
1.167 noro 2990: /* reverse the tracelist and append it to alltracelist */
2991: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
2992: STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
2993: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
2994: }
1.157 noro 2995: return nd_psn++;
1.1 noro 2996: }
2997:
1.167 noro 2998: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177 noro 2999: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167 noro 3000:
1.177 noro 3001: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1 noro 3002: {
1.157 noro 3003: int i,j,td,len,max;
1.167 noro 3004: NODE s,s0,f0,tn;
1.157 noro 3005: UINT *d;
3006: RHist r;
1.167 noro 3007: NDVI w;
1.157 noro 3008: NDV a,am;
1.167 noro 3009: union oNDC hc;
3010: NODE node;
3011: P hcp;
3012: Q iq,jq,hcq;
3013: LIST l;
1.157 noro 3014:
3015: nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167 noro 3016: /* initialize the tracelist */
3017: nd_tracelist = 0;
1.157 noro 3018:
3019: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.235 noro 3020: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
1.167 noro 3021: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3022: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157 noro 3023: if ( !dont_sort ) {
3024: /* XXX heuristic */
3025: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167 noro 3026: qsort(w,nd_psn,sizeof(struct oNDVI),
3027: (int (*)(const void *,const void *))ndvi_compare_rev);
1.157 noro 3028: else
1.167 noro 3029: qsort(w,nd_psn,sizeof(struct oNDVI),
3030: (int (*)(const void *,const void *))ndvi_compare);
1.157 noro 3031: }
3032: nd_pslen = 2*nd_psn;
3033: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215 noro 3034: nd_ps_gz = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157 noro 3035: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215 noro 3036: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3037: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157 noro 3038: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3039: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3040: nd_hcf = 0;
3041:
3042: if ( trace && nd_vc )
3043: makesubst(nd_vc,&nd_subst);
3044: else
3045: nd_subst = 0;
3046:
3047: if ( !nd_red )
3048: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3049: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3050: for ( i = 0; i < nd_psn; i++ ) {
1.167 noro 3051: hc = HCU(w[i].p);
1.157 noro 3052: if ( trace ) {
1.234 noro 3053: if ( mod == -2 ) {
3054: /* over a large finite field */
3055: /* trace = small modulus */
3056: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3057: ndv_mod(-2,a);
3058: if ( !dont_removecont) ndv_removecont(-2,a);
3059: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3060: ndv_mod(trace,am);
1.245 noro 3061: if ( DL_COMPARE(HDL(am),HDL(a)) )
3062: return 0;
1.234 noro 3063: ndv_removecont(trace,am);
3064: } else {
3065: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.245 noro 3066: if ( !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.234 noro 3067: if ( !dont_removecont) ndv_removecont(0,a);
3068: register_hcf(a);
3069: am = nd_ps[i] = ndv_dup(mod,a);
3070: ndv_mod(mod,am);
1.245 noro 3071: if ( DL_COMPARE(HDL(am),HDL(a)) )
3072: return 0;
1.234 noro 3073: ndv_removecont(mod,am);
3074: }
1.157 noro 3075: } else {
1.167 noro 3076: a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.245 noro 3077: if ( !mod && !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.157 noro 3078: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3079: if ( !mod ) register_hcf(a);
3080: }
1.172 noro 3081: if ( nd_gentrace ) {
1.167 noro 3082: STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.245 noro 3083: if ( !dont_removecont )
1.168 noro 3084: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167 noro 3085: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3086: }
1.157 noro 3087: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3088: nd_bound[i] = ndv_compute_bound(a);
3089: nd_psh[i] = r;
3090: if ( nd_demand ) {
3091: if ( trace ) {
3092: ndv_save(nd_ps_trace[i],i);
1.235 noro 3093: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.215 noro 3094: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.157 noro 3095: nd_ps_trace[i] = 0;
3096: } else {
3097: ndv_save(nd_ps[i],i);
1.215 noro 3098: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
1.157 noro 3099: nd_ps[i] = 0;
3100: }
3101: }
3102: }
1.172 noro 3103: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177 noro 3104: return 1;
1.20 noro 3105: }
3106:
1.119 noro 3107: struct order_spec *append_block(struct order_spec *spec,
3108: int nv,int nalg,int ord);
3109:
1.121 noro 3110: extern VECT current_dl_weight_vector_obj;
3111: static VECT prev_weight_vector_obj;
3112:
1.120 noro 3113: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157 noro 3114: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120 noro 3115: {
1.157 noro 3116: NODE alist,t,s,r0,r,arg;
3117: VL tv;
3118: P poly;
3119: DP d;
3120: Alg alpha,dp;
3121: DAlg inv,da,hc;
3122: MP m;
3123: int i,nvar,nalg,n;
3124: NumberField nf;
3125: LIST f1,f2;
3126: struct order_spec *current_spec;
3127: VECT obj,obj0;
1.232 noro 3128: VECT tmp;
1.157 noro 3129:
3130: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3131: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3132:
3133: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3134: NEXTNODE(alist,t); MKV(tv->v,poly);
3135: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3136: tv->v = tv->v->priv;
3137: }
3138: NEXT(t) = 0;
3139:
1.167 noro 3140: /* simplification, making polynomials monic */
1.157 noro 3141: setfield_dalg(alist);
1.232 noro 3142: obj_algtodalg((Obj)f,(Obj *)&f1);
1.157 noro 3143: for ( t = BDY(f); t; t = NEXT(t) ) {
3144: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3145: hc = (DAlg)BDY(d)->c;
3146: if ( NID(hc) == N_DA ) {
3147: invdalg(hc,&inv);
3148: for ( m = BDY(d); m; m = NEXT(m) ) {
1.238 noro 3149: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
1.157 noro 3150: }
3151: }
1.238 noro 3152: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
1.157 noro 3153: }
1.232 noro 3154: obj_dalgtoalg((Obj)f1,(Obj *)&f);
1.157 noro 3155:
3156: /* append alg vars to the var list */
3157: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3158: NEXT(tv) = av;
3159:
3160: /* append a block to ord */
3161: *ord1p = append_block(ord,nvar,nalg,2);
3162:
3163: /* create generator list */
3164: nf = get_numberfield();
3165: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3166: MKAlg(nf->defpoly[i],dp);
3167: MKNODE(s,dp,t); t = s;
3168: }
3169: MKLIST(f1,t);
3170: *alistp = alist;
1.232 noro 3171: algobjtorat((Obj)f1,(Obj *)f1p);
1.157 noro 3172:
3173: /* creating a new weight vector */
3174: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3175: n = nvar+nalg+1;
3176: MKVECT(obj,n);
3177: if ( obj0 && obj0->len == nvar )
3178: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3179: else
3180: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3181: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3182: BDY(obj)[n-1] = (pointer)ONE;
3183: arg = mknode(1,obj);
3184: Pdp_set_weight(arg,&tmp);
1.121 noro 3185: }
3186:
3187: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3188: {
1.157 noro 3189: NODE s,t,u0,u;
3190: P p;
3191: VL tv;
1.232 noro 3192: Obj obj;
3193: VECT tmp;
1.157 noro 3194: NODE arg;
3195:
3196: u0 = 0;
3197: for ( t = r; t; t = NEXT(t) ) {
3198: p = (P)BDY(t);
3199: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3200: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3201: }
3202: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3203: NEXTNODE(u0,u);
3204: BDY(u) = (pointer)p;
3205: }
3206: }
3207: arg = mknode(1,prev_weight_vector_obj);
3208: Pdp_set_weight(arg,&tmp);
1.121 noro 3209:
1.157 noro 3210: return u0;
1.120 noro 3211: }
3212:
1.199 noro 3213: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1 noro 3214: {
1.157 noro 3215: VL tv,fv,vv,vc,av;
3216: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3217: int e,max,nvar,i;
3218: NDV b;
1.184 noro 3219: int ishomo,nalg,mrank,trank,wmax,len;
1.245 noro 3220: NMV a;
1.157 noro 3221: Alg alpha,dp;
3222: P p,zp;
3223: Q dmy;
3224: LIST f1,f2,zpl;
3225: Obj obj;
3226: NumberField nf;
3227: struct order_spec *ord1;
1.208 noro 3228: NODE tr,tl1,tl2,tl3,tl4,nzlist;
1.170 noro 3229: LIST l1,l2,l3,l4,l5;
1.245 noro 3230: int j;
3231: Q jq,bpe,last_nonzero;
1.167 noro 3232: int *perm;
1.170 noro 3233: EPOS oepos;
1.194 noro 3234: int obpe,oadv,ompos,cbpe;
1.1 noro 3235:
1.174 noro 3236: nd_module = 0;
1.157 noro 3237: if ( !m && Demand ) nd_demand = 1;
3238: else nd_demand = 0;
1.172 noro 3239: parse_nd_option(current_option);
1.78 noro 3240:
1.157 noro 3241: if ( DP_Multiple )
3242: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103 noro 3243: #if 0
1.157 noro 3244: ndv_alloc = 0;
1.103 noro 3245: #endif
1.157 noro 3246: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.229 noro 3247: if ( m && nd_vc )
3248: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
1.157 noro 3249: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3250: switch ( ord->id ) {
3251: case 1:
3252: if ( ord->nv != nvar )
3253: error("nd_{gr,f4} : invalid order specification");
3254: break;
3255: default:
3256: break;
3257: }
3258: nd_nalg = 0;
3259: av = 0;
3260: if ( !m ) {
3261: get_algtree((Obj)f,&av);
3262: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3263: nd_ntrans = nvar;
3264: nd_nalg = nalg;
3265: /* #i -> t#i */
3266: if ( nalg ) {
3267: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3268: ord = ord1;
3269: f = f1;
3270: }
3271: nvar += nalg;
3272: }
3273: nd_init_ord(ord);
3274: mrank = 0;
1.178 noro 3275: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157 noro 3276: for ( tv = vv; tv; tv = NEXT(tv) ) {
3277: if ( nd_module ) {
3278: s = BDY((LIST)BDY(t));
3279: trank = length(s);
3280: mrank = MAX(mrank,trank);
3281: for ( ; s; s = NEXT(s) ) {
3282: e = getdeg(tv->v,(P)BDY(s));
3283: max = MAX(e,max);
3284: }
3285: } else {
3286: e = getdeg(tv->v,(P)BDY(t));
3287: max = MAX(e,max);
3288: }
3289: }
1.208 noro 3290: nd_setup_parameters(nvar,nd_nzlist?0:max);
1.170 noro 3291: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157 noro 3292: ishomo = 1;
3293: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167 noro 3294: if ( nd_module ) {
1.245 noro 3295: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3296: else zpl = (LIST)BDY(t);
1.157 noro 3297: b = (pointer)pltondv(CO,vv,zpl);
3298: } else {
1.245 noro 3299: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3300: else zp = (P)BDY(t);
1.157 noro 3301: b = (pointer)ptondv(CO,vv,zp);
1.167 noro 3302: }
1.157 noro 3303: if ( ishomo )
3304: ishomo = ishomo && ndv_ishomo(b);
3305: if ( m ) ndv_mod(m,b);
3306: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3307: }
3308: if ( fd0 ) NEXT(fd) = 0;
1.184 noro 3309:
1.245 noro 3310: if ( !ishomo && homo ) {
1.184 noro 3311: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3312: b = (NDV)BDY(t); len = LEN(b);
3313: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3314: wmax = MAX(TD(DL(a)),wmax);
3315: }
3316: homogenize_order(ord,nvar,&ord1);
3317: nd_init_ord(ord1);
1.243 noro 3318: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
1.184 noro 3319: for ( t = fd0; t; t = NEXT(t) )
3320: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3321: }
3322:
1.211 noro 3323: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
1.172 noro 3324: if ( nd_gentrace ) {
1.167 noro 3325: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3326: }
1.245 noro 3327: if ( nd_splist ) {
3328: *rp = compute_splist();
3329: return;
3330: }
3331: if ( nd_check_splist ) {
1.214 noro 3332: if ( f4 ) {
3333: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3334: else *rp = 0;
3335: } else {
3336: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3337: else *rp = 0;
3338: }
1.245 noro 3339: return;
3340: }
1.234 noro 3341: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.245 noro 3342: if ( !x ) {
3343: *rp = 0; return;
3344: }
3345: if ( !ishomo && homo ) {
3346: /* dehomogenization */
3347: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3348: nd_init_ord(ord);
3349: nd_setup_parameters(nvar,0);
3350: }
1.157 noro 3351: nd_demand = 0;
1.245 noro 3352: if ( nd_module && nd_intersect ) {
3353: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
3354: if ( MPOS(DL(nd_psh[j])) > 1 ) {
3355: MKNODE(xx,(pointer)j,x); x = xx;
3356: }
3357: conv_ilist(nd_demand,0,x,0);
3358: goto FINAL;
3359: }
1.208 noro 3360: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
1.167 noro 3361: x = ndv_reducebase(x,perm);
1.208 noro 3362: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157 noro 3363: x = ndv_reduceall(m,x);
1.194 noro 3364: cbpe = nd_bpe;
1.208 noro 3365: if ( nd_gentrace && !f4 ) {
1.170 noro 3366: tl2 = nd_alltracelist; nd_alltracelist = 0;
3367: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198 noro 3368: tl3 = nd_alltracelist; nd_alltracelist = 0;
3369: if ( nd_gensyz ) {
1.245 noro 3370: nd_gb(m,0,1,1,0);
1.170 noro 3371: tl4 = nd_alltracelist; nd_alltracelist = 0;
3372: } else tl4 = 0;
3373: }
1.194 noro 3374: nd_bpe = cbpe;
3375: nd_setup_parameters(nd_nvar,0);
1.195 noro 3376: FINAL:
1.157 noro 3377: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3378: NEXTNODE(r0,r);
1.194 noro 3379: if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199 noro 3380: else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.245 noro 3381: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157 noro 3382: }
3383: if ( r0 ) NEXT(r) = 0;
1.208 noro 3384: if ( !m && nd_nalg )
1.157 noro 3385: r0 = postprocess_algcoef(av,alist,r0);
3386: MKLIST(*rp,r0);
1.172 noro 3387: if ( nd_gentrace ) {
1.245 noro 3388: if ( f4 ) {
1.208 noro 3389: STOQ(16,bpe);
1.241 noro 3390: STOQ(nd_last_nonzero,last_nonzero);
3391: tr = mknode(5,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero); MKLIST(*rp,tr);
3392:
1.208 noro 3393: } else {
3394: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3395: tl3 = reverse_node(tl3);
3396: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3397: for ( t = tl2; t; t = NEXT(t) ) {
3398: /* s = [i,[*,j,*,*],...] */
3399: s = BDY((LIST)BDY(t));
3400: j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
3401: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
3402: j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
3403: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3404: }
3405: }
3406: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
3407: STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.167 noro 3408: }
1.208 noro 3409: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3410: MKLIST(l5,tl4);
3411: STOQ(nd_bpe,bpe);
3412: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
3413: }
1.167 noro 3414: }
1.103 noro 3415: #if 0
1.157 noro 3416: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103 noro 3417: #endif
1.127 noro 3418: }
3419:
3420: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3421: {
1.157 noro 3422: VL tv,fv,vv,vc,av;
3423: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3424: int e,max,nvar,i;
3425: NDV b;
3426: int ishomo,nalg;
3427: Alg alpha,dp;
3428: P p,zp;
3429: Q dmy;
3430: LIST f1,f2;
3431: Obj obj;
3432: NumberField nf;
3433: struct order_spec *ord1;
1.167 noro 3434: int *perm;
1.157 noro 3435:
1.197 noro 3436: parse_nd_option(current_option);
1.157 noro 3437: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3438: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3439: switch ( ord->id ) {
3440: case 1:
3441: if ( ord->nv != nvar )
3442: error("nd_check : invalid order specification");
3443: break;
3444: default:
3445: break;
3446: }
3447: nd_nalg = 0;
3448: av = 0;
3449: if ( !m ) {
3450: get_algtree((Obj)f,&av);
3451: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3452: nd_ntrans = nvar;
3453: nd_nalg = nalg;
3454: /* #i -> t#i */
3455: if ( nalg ) {
3456: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3457: ord = ord1;
3458: f = f1;
3459: }
3460: nvar += nalg;
3461: }
3462: nd_init_ord(ord);
1.178 noro 3463: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157 noro 3464: for ( tv = vv; tv; tv = NEXT(tv) ) {
3465: e = getdeg(tv->v,(P)BDY(t));
3466: max = MAX(e,max);
3467: }
3468: nd_setup_parameters(nvar,max);
3469: ishomo = 1;
3470: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3471: ptozp((P)BDY(t),1,&dmy,&zp);
3472: b = (pointer)ptondv(CO,vv,zp);
3473: if ( ishomo )
3474: ishomo = ishomo && ndv_ishomo(b);
3475: if ( m ) ndv_mod(m,b);
3476: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3477: }
3478: if ( fd0 ) NEXT(fd) = 0;
3479: ndv_setup(m,0,fd0,0,1);
3480: for ( x = 0, i = 0; i < nd_psn; i++ )
3481: x = update_base(x,i);
3482: if ( do_check ) {
1.168 noro 3483: x = nd_gb(m,ishomo,1,0,&perm);
1.157 noro 3484: if ( !x ) {
3485: *rp = 0;
3486: return;
3487: }
3488: } else {
1.175 noro 3489: #if 0
1.245 noro 3490: /* bug ? */
1.157 noro 3491: for ( t = x; t; t = NEXT(t) )
1.159 noro 3492: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175 noro 3493: #else
1.245 noro 3494: conv_ilist(0,0,x,&perm);
1.175 noro 3495: #endif
1.157 noro 3496: }
1.167 noro 3497: x = ndv_reducebase(x,perm);
1.157 noro 3498: x = ndv_reduceall(m,x);
3499: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3500: NEXTNODE(r0,r);
3501: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3502: }
3503: if ( r0 ) NEXT(r) = 0;
1.208 noro 3504: if ( !m && nd_nalg )
1.157 noro 3505: r0 = postprocess_algcoef(av,alist,r0);
3506: MKLIST(*rp,r0);
1.20 noro 3507: }
3508:
1.198 noro 3509: NDV recompute_trace(NODE trace,NDV *p,int m);
3510: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3511:
3512: NDV recompute_trace(NODE ti,NDV *p,int mod)
3513: {
1.245 noro 3514: int c,c1,c2,i;
3515: NM mul,m,tail;
3516: ND d,r,rm;
3517: NODE sj;
3518: NDV red;
3519: Obj mj;
3520: static int afo=0;
3521:
3522: afo++;
3523: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3524: CM(mul) = 1;
3525: tail = 0;
3526: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3527: sj = BDY((LIST)BDY(ti));
3528: if ( ARG0(sj) ) {
3529: red = p[QTOS((Q)ARG1(sj))];
3530: mj = (Obj)ARG2(sj);
3531: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3532: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3533: rm = ndv_mul_nm(mod,mul,red);
3534: if ( !r ) r = rm;
3535: else {
3536: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3537: if ( d ) {
3538: NEXT(tail) = m; tail = m; LEN(d)++;
3539: } else {
3540: MKND(nd_nvar,m,1,d); tail = BDY(d);
3541: }
3542: }
3543: if ( !m ) return 0; /* failure */
1.232 noro 3544: else {
1.245 noro 3545: BDY(r) = m;
1.232 noro 3546: if ( mod > 0 || mod == -1 ) {
1.245 noro 3547: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3548: DMAR(c1,c2,0,mod,c);
3549: nd_mul_c(mod,rm,c);
1.232 noro 3550: } else {
3551: GZ t,u;
3552:
3553: chsgnlf(HCZ(r),&t);
3554: divlf(t,HCZ(rm),&u);
3555: nd_mul_c_lf(rm,u);
3556: }
1.245 noro 3557: r = nd_add(mod,r,rm);
3558: }
1.232 noro 3559: }
1.245 noro 3560: }
3561: }
3562: if ( tail ) NEXT(tail) = 0;
3563: d = nd_add(mod,d,r);
3564: nd_mul_c(mod,d,invm(HCM(d),mod));
3565: return ndtondv(mod,d);
1.198 noro 3566: }
3567:
3568: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3569: {
3570: VL tv,fv,vv,vc,av;
3571: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3572: int e,max,nvar,i;
3573: NDV b;
3574: int ishomo,nalg;
3575: Alg alpha,dp;
3576: P p,zp;
3577: Q dmy;
3578: LIST f1,f2;
3579: Obj obj;
3580: NumberField nf;
3581: struct order_spec *ord1;
1.245 noro 3582: NODE permtrace,intred,ind,perm,trace,ti;
3583: int len,n,j;
3584: NDV *db,*pb;
1.198 noro 3585:
3586: parse_nd_option(current_option);
3587: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3588: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3589: switch ( ord->id ) {
3590: case 1:
3591: if ( ord->nv != nvar )
3592: error("nd_check : invalid order specification");
3593: break;
3594: default:
3595: break;
3596: }
3597: nd_init_ord(ord);
1.245 noro 3598: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
1.198 noro 3599: nd_setup_parameters(nvar,0);
3600:
1.245 noro 3601: len = length(BDY(f));
3602: db = (NDV *)MALLOC(len*sizeof(NDV *));
3603: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3604: ptozp((P)BDY(t),1,&dmy,&zp);
3605: b = ptondv(CO,vv,zp);
1.198 noro 3606: ndv_mod(m,b);
1.245 noro 3607: ndv_mul_c(m,b,invm(HCM(b),m));
3608: db[i] = b;
1.198 noro 3609: }
3610:
1.245 noro 3611: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3612: intred = BDY((LIST)ARG3(BDY(tlist)));
3613: ind = BDY((LIST)ARG4(BDY(tlist)));
3614: perm = BDY((LIST)ARG0(permtrace));
3615: trace = NEXT(permtrace);
3616:
3617: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
3618: j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
3619: if ( j > i ) i = j;
3620: }
3621: n = i+1;
3622: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3623: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3624: ti = BDY((LIST)BDY(t));
3625: pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
3626: }
3627: for ( t = trace; t; t = NEXT(t) ) {
3628: ti = BDY((LIST)BDY(t));
3629: pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3630: if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
3631: if ( DP_Print ) {
3632: fprintf(asir_out,"."); fflush(asir_out);
3633: }
3634: }
3635: for ( t = intred; t; t = NEXT(t) ) {
3636: ti = BDY((LIST)BDY(t));
3637: pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3638: if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
3639: if ( DP_Print ) {
3640: fprintf(asir_out,"*"); fflush(asir_out);
3641: }
3642: }
1.198 noro 3643: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3644: NEXTNODE(r0,r);
1.245 noro 3645: b = pb[QTOS((Q)BDY(t))];
1.198 noro 3646: ndv_mul_c(m,b,invm(HCM(b),m));
3647: #if 0
3648: BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
3649: #else
3650: BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
3651: #endif
3652: }
3653: if ( r0 ) NEXT(r) = 0;
3654: MKLIST(*rp,r0);
3655: if ( DP_Print ) fprintf(asir_out,"\n");
3656: }
3657:
1.133 noro 3658: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20 noro 3659: {
1.157 noro 3660: VL tv,fv,vv,vc,av;
3661: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3662: int m,nocheck,nvar,mindex,e,max;
3663: NDV c;
3664: NMV a;
3665: P p,zp;
3666: Q dmy;
3667: EPOS oepos;
1.164 noro 3668: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157 noro 3669: Alg alpha,dp;
3670: P poly;
1.158 noro 3671: LIST f1,f2,zpl;
1.157 noro 3672: Obj obj;
3673: NumberField nf;
3674: struct order_spec *ord1;
3675: struct oEGT eg_check,eg0,eg1;
1.168 noro 3676: NODE tr,tl1,tl2,tl3,tl4;
3677: LIST l1,l2,l3,l4,l5;
1.167 noro 3678: int *perm;
1.168 noro 3679: int j,ret;
1.198 noro 3680: Q jq,bpe;
1.157 noro 3681:
1.167 noro 3682: nd_module = 0;
1.235 noro 3683: nd_lf = 0;
1.172 noro 3684: parse_nd_option(current_option);
1.234 noro 3685: if ( nd_lf ) {
3686: if ( f4 )
3687: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3688: else
3689: error("nd_gr_trace is not implemented yet over a large finite field");
3690: return;
3691: }
1.157 noro 3692: if ( DP_Multiple )
3693: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3694:
3695: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3696: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3697: switch ( ord->id ) {
3698: case 1:
3699: if ( ord->nv != nvar )
3700: error("nd_gr_trace : invalid order specification");
3701: break;
3702: default:
3703: break;
3704: }
3705:
3706: get_algtree((Obj)f,&av);
3707: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3708: nd_ntrans = nvar;
3709: nd_nalg = nalg;
3710: /* #i -> t#i */
3711: if ( nalg ) {
3712: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3713: ord = ord1;
3714: f = f1;
3715: }
3716: nvar += nalg;
3717:
3718: nocheck = 0;
3719: mindex = 0;
3720:
3721: if ( Demand ) nd_demand = 1;
3722: else nd_demand = 0;
3723:
3724: /* setup modulus */
3725: if ( trace < 0 ) {
3726: trace = -trace;
3727: nocheck = 1;
3728: }
3729: m = trace > 1 ? trace : get_lprime(mindex);
1.158 noro 3730: nd_init_ord(ord);
3731: mrank = 0;
1.178 noro 3732: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157 noro 3733: for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158 noro 3734: if ( nd_module ) {
3735: s = BDY((LIST)BDY(t));
3736: trank = length(s);
3737: mrank = MAX(mrank,trank);
3738: for ( ; s; s = NEXT(s) ) {
3739: e = getdeg(tv->v,(P)BDY(s));
3740: max = MAX(e,max);
3741: }
3742: } else {
3743: e = getdeg(tv->v,(P)BDY(t));
3744: max = MAX(e,max);
3745: }
1.157 noro 3746: }
3747: nd_setup_parameters(nvar,max);
1.164 noro 3748: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157 noro 3749: ishomo = 1;
3750: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167 noro 3751: if ( nd_module ) {
1.245 noro 3752: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3753: else zpl = (LIST)BDY(t);
1.158 noro 3754: c = (pointer)pltondv(CO,vv,zpl);
3755: } else {
1.245 noro 3756: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3757: else zp = (P)BDY(t);
1.158 noro 3758: c = (pointer)ptondv(CO,vv,zp);
1.167 noro 3759: }
1.157 noro 3760: if ( ishomo )
3761: ishomo = ishomo && ndv_ishomo(c);
3762: if ( c ) {
3763: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3764: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3765: }
3766: }
3767: if ( in0 ) NEXT(in) = 0;
3768: if ( fd0 ) NEXT(fd) = 0;
3769: if ( !ishomo && homo ) {
3770: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3771: c = (NDV)BDY(t); len = LEN(c);
3772: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3773: wmax = MAX(TD(DL(a)),wmax);
3774: }
3775: homogenize_order(ord,nvar,&ord1);
3776: nd_init_ord(ord1);
3777: nd_setup_parameters(nvar+1,wmax);
3778: for ( t = fd0; t; t = NEXT(t) )
1.164 noro 3779: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157 noro 3780: }
1.228 noro 3781: if ( MaxDeg > 0 ) nocheck = 1;
1.157 noro 3782: while ( 1 ) {
1.245 noro 3783: tl1 = tl2 = tl3 = tl4 = 0;
1.157 noro 3784: if ( Demand )
3785: nd_demand = 1;
1.187 noro 3786: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172 noro 3787: if ( nd_gentrace ) {
1.167 noro 3788: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3789: }
1.177 noro 3790: if ( ret )
3791: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3792: if ( !ret || !cand ) {
1.157 noro 3793: /* failure */
3794: if ( trace > 1 ) { *rp = 0; return; }
3795: else m = get_lprime(++mindex);
3796: continue;
3797: }
3798: if ( !ishomo && homo ) {
3799: /* dehomogenization */
3800: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3801: nd_init_ord(ord);
3802: nd_setup_parameters(nvar,0);
3803: }
3804: nd_demand = 0;
1.167 noro 3805: cand = ndv_reducebase(cand,perm);
1.172 noro 3806: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157 noro 3807: cand = ndv_reduceall(0,cand);
3808: cbpe = nd_bpe;
1.172 noro 3809: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173 noro 3810: get_eg(&eg0);
1.157 noro 3811: if ( nocheck )
3812: break;
1.170 noro 3813: if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172 noro 3814: if ( nd_gentrace ) {
1.245 noro 3815: tl3 = nd_alltracelist; nd_alltracelist = 0;
3816: } else tl3 = 0;
1.168 noro 3817: /* gbcheck : cand is a GB of Id(cand) ? */
1.234 noro 3818: if ( nd_vc || nd_gentrace || nd_gensyz )
3819: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3820: else
3821: ret = nd_f4(0,1,0)!=0;
1.172 noro 3822: if ( nd_gentrace && nd_gensyz ) {
1.245 noro 3823: tl4 = nd_alltracelist; nd_alltracelist = 0;
3824: } else tl4 = 0;
3825: }
3826: if ( ret ) break;
1.157 noro 3827: else if ( trace > 1 ) {
3828: /* failure */
3829: *rp = 0; return;
3830: } else {
3831: /* try the next modulus */
3832: m = get_lprime(++mindex);
3833: /* reset the parameters */
3834: if ( !ishomo && homo ) {
3835: nd_init_ord(ord1);
3836: nd_setup_parameters(nvar+1,wmax);
3837: } else {
3838: nd_init_ord(ord);
3839: nd_setup_parameters(nvar,max);
3840: }
3841: }
3842: }
3843: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3844: if ( DP_Print )
1.241 noro 3845: fprintf(asir_out,"check=%.3fsec,",eg_check.exectime+eg_check.gctime);
1.157 noro 3846: /* dp->p */
3847: nd_bpe = cbpe;
3848: nd_setup_parameters(nd_nvar,0);
1.158 noro 3849: for ( r = cand; r; r = NEXT(r) ) {
1.167 noro 3850: if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158 noro 3851: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
3852: }
1.208 noro 3853: if ( nd_nalg )
1.157 noro 3854: cand = postprocess_algcoef(av,alist,cand);
3855: MKLIST(*rp,cand);
1.172 noro 3856: if ( nd_gentrace ) {
1.167 noro 3857: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.245 noro 3858: tl3 = reverse_node(tl3);
3859: /* tl2 = [[i,[[*,j,*,*],...]],...] */
1.167 noro 3860: for ( t = tl2; t; t = NEXT(t) ) {
1.245 noro 3861: /* s = [i,[*,j,*,*],...] */
1.167 noro 3862: s = BDY((LIST)BDY(t));
3863: j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
1.245 noro 3864: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.167 noro 3865: j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
1.245 noro 3866: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
1.167 noro 3867: }
1.245 noro 3868: }
3869: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
3870: STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
3871: }
1.168 noro 3872: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
1.245 noro 3873: MKLIST(l5,tl4);
3874: STOQ(nd_bpe,bpe);
1.198 noro 3875: tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167 noro 3876: }
1.157 noro 3877: }
1.52 noro 3878:
1.157 noro 3879: /* XXX : module element is not considered */
1.1 noro 3880:
1.61 noro 3881: void dltondl(int n,DL dl,UINT *r)
1.1 noro 3882: {
1.157 noro 3883: UINT *d;
3884: int i,j,l,s,ord_l;
3885: struct order_pair *op;
3886:
3887: d = dl->d;
3888: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3889: if ( nd_blockmask ) {
3890: l = nd_blockmask->n;
3891: op = nd_blockmask->order_pair;
3892: for ( j = 0, s = 0; j < l; j++ ) {
3893: ord_l = op[j].length;
3894: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3895: }
3896: TD(r) = ndl_weight(r);
3897: ndl_weight_mask(r);
3898: } else {
3899: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3900: TD(r) = ndl_weight(r);
3901: }
1.1 noro 3902: }
3903:
1.61 noro 3904: DL ndltodl(int n,UINT *ndl)
1.1 noro 3905: {
1.157 noro 3906: DL dl;
3907: int *d;
3908: int i,j,l,s,ord_l;
3909: struct order_pair *op;
3910:
3911: NEWDL(dl,n);
3912: dl->td = TD(ndl);
3913: d = dl->d;
3914: if ( nd_blockmask ) {
3915: l = nd_blockmask->n;
3916: op = nd_blockmask->order_pair;
3917: for ( j = 0, s = 0; j < l; j++ ) {
3918: ord_l = op[j].length;
3919: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3920: }
3921: } else {
3922: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3923: }
3924: return dl;
1.1 noro 3925: }
3926:
1.167 noro 3927: void nmtodp(int mod,NM m,DP *r)
3928: {
3929: DP dp;
3930: MP mr;
3931:
3932: NEWMP(mr);
3933: mr->dl = ndltodl(nd_nvar,DL(m));
1.238 noro 3934: mr->c = (Obj)ndctop(mod,m->c);
1.167 noro 3935: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3936: *r = dp;
3937: }
3938:
1.61 noro 3939: void ndl_print(UINT *dl)
1.1 noro 3940: {
1.157 noro 3941: int n;
3942: int i,j,l,ord_l,s,s0;
3943: struct order_pair *op;
3944:
3945: n = nd_nvar;
3946: printf("<<");
3947: if ( nd_blockmask ) {
3948: l = nd_blockmask->n;
3949: op = nd_blockmask->order_pair;
3950: for ( j = 0, s = s0 = 0; j < l; j++ ) {
3951: ord_l = op[j].length;
3952: for ( i = 0; i < ord_l; i++, s++ )
3953: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
3954: }
3955: } else {
3956: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
3957: }
3958: printf(">>");
1.236 noro 3959: if ( nd_module && MPOS(dl) )
1.157 noro 3960: printf("*e%d",MPOS(dl));
1.1 noro 3961: }
3962:
3963: void nd_print(ND p)
3964: {
1.157 noro 3965: NM m;
1.1 noro 3966:
1.157 noro 3967: if ( !p )
3968: printf("0\n");
3969: else {
3970: for ( m = BDY(p); m; m = NEXT(m) ) {
3971: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3972: else printf("+%d*",CM(m));
3973: ndl_print(DL(m));
3974: }
3975: printf("\n");
3976: }
1.1 noro 3977: }
3978:
1.113 noro 3979: void nd_print_q(ND p)
1.16 noro 3980: {
1.157 noro 3981: NM m;
1.16 noro 3982:
1.157 noro 3983: if ( !p )
3984: printf("0\n");
3985: else {
3986: for ( m = BDY(p); m; m = NEXT(m) ) {
3987: printf("+");
3988: printexpr(CO,(Obj)CQ(m));
3989: printf("*");
3990: ndl_print(DL(m));
3991: }
3992: printf("\n");
3993: }
1.16 noro 3994: }
3995:
1.1 noro 3996: void ndp_print(ND_pairs d)
3997: {
1.157 noro 3998: ND_pairs t;
1.1 noro 3999:
1.157 noro 4000: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4001: printf("\n");
1.1 noro 4002: }
4003:
1.20 noro 4004: void nd_removecont(int mod,ND p)
1.16 noro 4005: {
1.157 noro 4006: int i,n;
4007: Q *w;
4008: Q dvr,t;
4009: NM m;
4010: struct oVECT v;
4011: N q,r;
4012:
4013: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
1.232 noro 4014: else if ( mod == -2 ) {
4015: GZ inv;
4016: divlf(ONEGZ,HCZ(p),&inv);
4017: nd_mul_c_lf(p,inv);
4018: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.157 noro 4019: else {
4020: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.235 noro 4021: w = (Q *)MALLOC(n*sizeof(Q));
1.157 noro 4022: v.len = n;
4023: v.body = (pointer *)w;
4024: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
4025: removecont_array((P *)w,n,1);
4026: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
4027: }
1.16 noro 4028: }
4029:
1.21 noro 4030: void nd_removecont2(ND p1,ND p2)
4031: {
1.157 noro 4032: int i,n1,n2,n;
4033: Q *w;
4034: Q dvr,t;
4035: NM m;
4036: struct oVECT v;
4037: N q,r;
4038:
4039: n1 = nd_length(p1);
4040: n2 = nd_length(p2);
4041: n = n1+n2;
1.235 noro 4042: w = (Q *)MALLOC(n*sizeof(Q));
1.157 noro 4043: v.len = n;
4044: v.body = (pointer *)w;
4045: i = 0;
4046: if ( p1 )
4047: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
4048: if ( p2 )
4049: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
4050: removecont_array((P *)w,n,1);
4051: i = 0;
4052: if ( p1 )
4053: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
4054: if ( p2 )
4055: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21 noro 4056: }
4057:
1.20 noro 4058: void ndv_removecont(int mod,NDV p)
1.16 noro 4059: {
1.157 noro 4060: int i,len,all_p;
4061: Q *c;
4062: P *w;
4063: Q dvr,t;
4064: P g,cont,tp;
4065: NMV m;
4066:
4067: if ( mod == -1 )
4068: ndv_mul_c(mod,p,_invsf(HCM(p)));
1.232 noro 4069: else if ( mod == -2 ) {
4070: GZ inv;
4071: divlf(ONEGZ,HCZ(p),&inv);
4072: ndv_mul_c_lf(p,inv);
4073: } else if ( mod )
1.157 noro 4074: ndv_mul_c(mod,p,invm(HCM(p),mod));
4075: else {
4076: len = p->len;
1.235 noro 4077: w = (P *)MALLOC(len*sizeof(P));
4078: c = (Q *)MALLOC(len*sizeof(Q));
1.157 noro 4079: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4080: ptozp(CP(m),1,&c[i],&w[i]);
4081: all_p = all_p && !NUM(w[i]);
4082: }
4083: if ( all_p ) {
4084: qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4085: mulp(nd_vc,(P)dvr,g,&cont);
4086: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4087: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4088: }
4089: } else {
4090: sortbynm((Q *)c,len);
4091: qltozl((Q *)c,len,&dvr);
4092: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4093: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4094: }
4095: }
4096: }
1.21 noro 4097: }
4098:
1.157 noro 4099: /* koko */
4100:
1.164 noro 4101: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61 noro 4102: {
1.157 noro 4103: int len,i,max;
4104: NMV m,mr0,mr,t;
1.61 noro 4105:
1.157 noro 4106: len = p->len;
1.178 noro 4107: for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157 noro 4108: max = MAX(max,TD(DL(m)));
4109: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4110: m = (NMV)((char *)mr0+(len-1)*oadv);
4111: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235 noro 4112: t = (NMV)MALLOC(nmv_adv);
1.157 noro 4113: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164 noro 4114: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157 noro 4115: CQ(mr) = CQ(m);
4116: ndl_copy(DL(t),DL(mr));
4117: }
4118: NV(p)++;
4119: BDY(p) = mr0;
1.61 noro 4120: }
4121:
1.45 noro 4122: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23 noro 4123: {
1.164 noro 4124: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167 noro 4125: int pos;
1.157 noro 4126: Q *w;
4127: Q dvr,t;
4128: NMV m,r;
4129:
4130: len = p->len;
4131: newnvar = nd_nvar-1;
4132: newexporigin = nd_get_exporigin(ord);
1.167 noro 4133: if ( nd_module ) newmpos = newexporigin-1;
1.157 noro 4134: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4135: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4136: ndl_dehomogenize(DL(m));
4137: if ( newwpd != nd_wpd ) {
4138: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4139: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
4140: CQ(r) = CQ(m);
1.167 noro 4141: if ( nd_module ) pos = MPOS(DL(m));
1.157 noro 4142: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4143: adj = nd_exporigin-newexporigin;
4144: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167 noro 4145: if ( nd_module ) {
4146: DL(r)[newmpos] = pos;
4147: }
1.157 noro 4148: }
4149: }
4150: NV(p)--;
1.23 noro 4151: }
4152:
1.150 noro 4153: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4154: {
1.157 noro 4155: int i;
4156: P *tpl,*tpl1;
4157: NODE l;
4158: P h,gcd,t;
4159:
1.235 noro 4160: tpl = (P *)MALLOC(m*sizeof(P));
4161: tpl1 = (P *)MALLOC(m*sizeof(P));
1.157 noro 4162: bcopy(pl,tpl,m*sizeof(P));
4163: gcd = (P)ONE;
4164: for ( l = nd_hcf; l; l = NEXT(l) ) {
4165: h = (P)BDY(l);
4166: while ( 1 ) {
4167: for ( i = 0; i < m; i++ )
4168: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4169: break;
4170: if ( i == m ) {
4171: bcopy(tpl1,tpl,m*sizeof(P));
4172: mulp(vl,gcd,h,&t); gcd = t;
4173: } else
4174: break;
4175: }
4176: }
4177: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4178: if ( full ) {
4179: heu_nezgcdnpz(vl,tpl,m,&t);
4180: mulp(vl,gcd,t,pr);
4181: } else
4182: *pr = gcd;
1.150 noro 4183: }
4184:
4185: void removecont_array(P *p,int n,int full)
1.146 noro 4186: {
1.157 noro 4187: int all_p,all_q,i;
4188: Q *c;
4189: P *w;
4190: P t,s;
4191:
4192: for ( all_q = 1, i = 0; i < n; i++ )
4193: all_q = all_q && NUM(p[i]);
4194: if ( all_q ) {
4195: removecont_array_q((Q *)p,n);
4196: } else {
1.235 noro 4197: c = (Q *)MALLOC(n*sizeof(Q));
4198: w = (P *)MALLOC(n*sizeof(P));
1.157 noro 4199: for ( i = 0; i < n; i++ ) {
4200: ptozp(p[i],1,&c[i],&w[i]);
4201: }
4202: removecont_array_q(c,n);
4203: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4204: for ( i = 0; i < n; i++ ) {
4205: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4206: }
4207: }
1.146 noro 4208: }
4209:
4210: void removecont_array_q(Q *c,int n)
1.21 noro 4211: {
1.157 noro 4212: struct oVECT v;
4213: Q d0,d1,a,u,u1,gcd;
4214: int i,j;
4215: N qn,rn,gn;
4216: Q *q,*r;
4217:
1.235 noro 4218: q = (Q *)MALLOC(n*sizeof(Q));
4219: r = (Q *)MALLOC(n*sizeof(Q));
1.157 noro 4220: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4221: igcdv_estimate(&v,&d0);
4222: for ( i = 0; i < n; i++ ) {
4223: divn(NM(c[i]),NM(d0),&qn,&rn);
4224: NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
4225: NTOQ(rn,SGN(c[i]),r[i]);
4226: }
4227: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4228: if ( i < n ) {
4229: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4230: igcdv(&v,&d1);
4231: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
4232: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
4233: for ( i = 0; i < n; i++ ) {
4234: mulq(a,q[i],&u);
4235: if ( r[i] ) {
4236: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
4237: addq(u,u1,&q[i]);
4238: } else
4239: q[i] = u;
4240: }
4241: }
4242: for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16 noro 4243: }
4244:
1.19 noro 4245: void nd_mul_c(int mod,ND p,int mul)
1.1 noro 4246: {
1.157 noro 4247: NM m;
4248: int c,c1;
1.1 noro 4249:
1.157 noro 4250: if ( !p ) return;
4251: if ( mul == 1 ) return;
4252: if ( mod == -1 )
4253: for ( m = BDY(p); m; m = NEXT(m) )
4254: CM(m) = _mulsf(CM(m),mul);
4255: else
4256: for ( m = BDY(p); m; m = NEXT(m) ) {
4257: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4258: }
1.1 noro 4259: }
4260:
1.232 noro 4261: void nd_mul_c_lf(ND p,GZ mul)
4262: {
4263: NM m;
4264: GZ c;
4265:
4266: if ( !p ) return;
4267: if ( UNIGZ(mul) ) return;
4268: for ( m = BDY(p); m; m = NEXT(m) ) {
4269: mullf(CZ(m),mul,&c); CZ(m) = c;
4270: }
4271: }
4272:
1.146 noro 4273: void nd_mul_c_q(ND p,P mul)
1.16 noro 4274: {
1.157 noro 4275: NM m;
4276: P c;
1.16 noro 4277:
1.157 noro 4278: if ( !p ) return;
4279: if ( UNIQ(mul) ) return;
4280: for ( m = BDY(p); m; m = NEXT(m) ) {
4281: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4282: }
1.16 noro 4283: }
4284:
1.61 noro 4285: void nd_mul_c_p(VL vl,ND p,P mul)
4286: {
1.157 noro 4287: NM m;
4288: P c;
1.61 noro 4289:
1.157 noro 4290: if ( !p ) return;
4291: for ( m = BDY(p); m; m = NEXT(m) ) {
4292: mulp(vl,CP(m),mul,&c); CP(m) = c;
4293: }
1.61 noro 4294: }
4295:
1.1 noro 4296: void nd_free(ND p)
4297: {
1.157 noro 4298: NM t,s;
1.1 noro 4299:
1.157 noro 4300: if ( !p ) return;
4301: t = BDY(p);
4302: while ( t ) {
4303: s = NEXT(t);
4304: FREENM(t);
4305: t = s;
4306: }
4307: FREEND(p);
1.1 noro 4308: }
4309:
1.23 noro 4310: void ndv_free(NDV p)
4311: {
1.200 noro 4312: GCFREE(BDY(p));
1.23 noro 4313: }
4314:
1.61 noro 4315: void nd_append_red(UINT *d,int i)
1.1 noro 4316: {
1.157 noro 4317: RHist m,m0;
4318: int h;
1.1 noro 4319:
1.157 noro 4320: NEWRHist(m);
4321: h = ndl_hash_value(d);
4322: m->index = i;
4323: ndl_copy(d,DL(m));
4324: NEXT(m) = nd_red[h];
4325: nd_red[h] = m;
1.1 noro 4326: }
4327:
1.61 noro 4328: UINT *ndv_compute_bound(NDV p)
1.1 noro 4329: {
1.157 noro 4330: UINT *d1,*d2,*t;
4331: UINT u;
4332: int i,j,k,l,len,ind;
4333: NMV m;
4334:
4335: if ( !p )
4336: return 0;
1.235 noro 4337: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4338: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157 noro 4339: len = LEN(p);
4340: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4341: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159 noro 4342: ndl_max(DL(m),d1,d2);
1.157 noro 4343: t = d1; d1 = d2; d2 = t;
4344: }
4345: l = nd_nvar+31;
4346: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4347: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4348: u = d1[i];
4349: k = (nd_epw-1)*nd_bpe;
4350: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4351: t[ind] = (u>>k)&nd_mask0;
4352: }
4353: for ( ; ind < l; ind++ ) t[ind] = 0;
4354: return t;
1.1 noro 4355: }
4356:
1.99 noro 4357: UINT *nd_compute_bound(ND p)
4358: {
1.157 noro 4359: UINT *d1,*d2,*t;
4360: UINT u;
4361: int i,j,k,l,len,ind;
4362: NM m;
4363:
4364: if ( !p )
4365: return 0;
1.235 noro 4366: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4367: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157 noro 4368: len = LEN(p);
4369: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4370: for ( m = NEXT(m); m; m = NEXT(m) ) {
4371: ndl_lcm(DL(m),d1,d2);
4372: t = d1; d1 = d2; d2 = t;
4373: }
4374: l = nd_nvar+31;
4375: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4376: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4377: u = d1[i];
4378: k = (nd_epw-1)*nd_bpe;
4379: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4380: t[ind] = (u>>k)&nd_mask0;
4381: }
4382: for ( ; ind < l; ind++ ) t[ind] = 0;
4383: return t;
1.99 noro 4384: }
4385:
1.157 noro 4386: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4387: /* of a term. In this case we need additional 1 word. */
4388:
1.48 noro 4389: int nd_get_exporigin(struct order_spec *ord)
4390: {
1.157 noro 4391: switch ( ord->id ) {
4392: case 0: case 2: case 256: case 258:
4393: return 1+nd_module;
4394: case 1: case 257:
4395: /* block order */
4396: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4397: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4398: return ord->ord.block.length+1+nd_module;
4399: case 3: case 259:
1.236 noro 4400: #if 0
1.157 noro 4401: error("nd_get_exporigin : composite order is not supported yet.");
1.236 noro 4402: #else
4403: return 1+nd_module;
4404: #endif
1.157 noro 4405: }
1.48 noro 4406: }
4407:
1.61 noro 4408: void nd_setup_parameters(int nvar,int max) {
1.157 noro 4409: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4410: struct order_pair *op;
1.48 noro 4411:
1.157 noro 4412: nd_nvar = nvar;
4413: if ( max ) {
4414: /* XXX */
4415: if ( do_weyl ) nd_bpe = 32;
4416: else if ( max < 2 ) nd_bpe = 1;
4417: else if ( max < 4 ) nd_bpe = 2;
4418: else if ( max < 8 ) nd_bpe = 3;
4419: else if ( max < 16 ) nd_bpe = 4;
4420: else if ( max < 32 ) nd_bpe = 5;
4421: else if ( max < 64 ) nd_bpe = 6;
4422: else if ( max < 256 ) nd_bpe = 8;
4423: else if ( max < 1024 ) nd_bpe = 10;
4424: else if ( max < 65536 ) nd_bpe = 16;
4425: else nd_bpe = 32;
4426: }
1.203 noro 4427: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
1.201 noro 4428: UINT t;
1.245 noro 4429: int st;
1.203 noro 4430: int *v;
1.245 noro 4431: /* t = max(weights) */
1.203 noro 4432: t = 0;
4433: if ( current_dl_weight_vector )
4434: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4435: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4436: if ( t < st ) t = st;
4437: }
4438: if ( nd_matrix )
4439: for ( i = 0; i < nd_matrix_len; i++ )
4440: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4441: if ( (st=v[j]) < 0 ) st = -st;
4442: if ( t < st ) t = st;
1.245 noro 4443: }
1.203 noro 4444: /* i = bitsize of t */
4445: for ( i = 0; t; t >>=1, i++ );
4446: /* i += bitsize of nd_nvar */
4447: for ( t = nd_nvar; t; t >>=1, i++);
4448: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4449: if ( (nd_bpe+i) >= 31 )
4450: error("nd_setup_parameters : too large weight");
4451: }
1.157 noro 4452: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4453: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4454: nd_exporigin = nd_get_exporigin(nd_ord);
4455: wpd = nd_exporigin+elen;
4456: if ( nd_module )
4457: nd_mpos = nd_exporigin-1;
4458: else
4459: nd_mpos = -1;
4460: if ( wpd != nd_wpd ) {
4461: nd_free_private_storage();
4462: nd_wpd = wpd;
4463: }
4464: if ( nd_bpe < 32 ) {
4465: nd_mask0 = (1<<nd_bpe)-1;
4466: } else {
4467: nd_mask0 = 0xffffffff;
4468: }
4469: bzero(nd_mask,sizeof(nd_mask));
4470: nd_mask1 = 0;
4471: for ( i = 0; i < nd_epw; i++ ) {
4472: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4473: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4474: }
4475: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4476: nd_epos = nd_create_epos(nd_ord);
4477: nd_blockmask = nd_create_blockmask(nd_ord);
4478: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1 noro 4479: }
4480:
1.103 noro 4481: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1 noro 4482: {
1.157 noro 4483: int i,obpe,oadv,h;
4484: static NM prev_nm_free_list;
4485: static ND_pairs prev_ndp_free_list;
4486: RHist mr0,mr;
4487: RHist r;
4488: RHist *old_red;
4489: ND_pairs s0,s,t;
4490: EPOS oepos;
4491:
4492: obpe = nd_bpe;
4493: oadv = nmv_adv;
4494: oepos = nd_epos;
4495: if ( obpe < 2 ) nd_bpe = 2;
4496: else if ( obpe < 3 ) nd_bpe = 3;
4497: else if ( obpe < 4 ) nd_bpe = 4;
4498: else if ( obpe < 5 ) nd_bpe = 5;
4499: else if ( obpe < 6 ) nd_bpe = 6;
4500: else if ( obpe < 8 ) nd_bpe = 8;
4501: else if ( obpe < 10 ) nd_bpe = 10;
4502: else if ( obpe < 16 ) nd_bpe = 16;
4503: else if ( obpe < 32 ) nd_bpe = 32;
4504: else error("nd_reconstruct : exponent too large");
4505:
4506: nd_setup_parameters(nd_nvar,0);
4507: prev_nm_free_list = _nm_free_list;
4508: prev_ndp_free_list = _ndp_free_list;
4509: _nm_free_list = 0;
4510: _ndp_free_list = 0;
1.215 noro 4511: for ( i = nd_psn-1; i >= 0; i-- ) {
4512: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4513: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4514: ndv_realloc(nd_ps_gz[i],obpe,oadv,oepos);
4515: }
1.157 noro 4516: if ( trace )
1.215 noro 4517: for ( i = nd_psn-1; i >= 0; i-- ) {
1.157 noro 4518: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.215 noro 4519: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4520: }
1.157 noro 4521: s0 = 0;
4522: for ( t = d; t; t = NEXT(t) ) {
4523: NEXTND_pairs(s0,s);
4524: s->i1 = t->i1;
4525: s->i2 = t->i2;
4526: SG(s) = SG(t);
4527: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4528: }
4529:
1.235 noro 4530: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.157 noro 4531: for ( i = 0; i < REDTAB_LEN; i++ ) {
4532: old_red[i] = nd_red[i];
4533: nd_red[i] = 0;
4534: }
4535: for ( i = 0; i < REDTAB_LEN; i++ )
4536: for ( r = old_red[i]; r; r = NEXT(r) ) {
4537: NEWRHist(mr);
4538: mr->index = r->index;
4539: SG(mr) = SG(r);
4540: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4541: h = ndl_hash_value(DL(mr));
4542: NEXT(mr) = nd_red[h];
4543: nd_red[h] = mr;
4544: }
4545: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4546: old_red = 0;
4547: for ( i = 0; i < nd_psn; i++ ) {
4548: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4549: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4550: nd_psh[i] = r;
4551: }
4552: if ( s0 ) NEXT(s) = 0;
4553: prev_nm_free_list = 0;
4554: prev_ndp_free_list = 0;
1.71 noro 4555: #if 0
1.157 noro 4556: GC_gcollect();
1.71 noro 4557: #endif
1.157 noro 4558: return s0;
1.1 noro 4559: }
4560:
1.61 noro 4561: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1 noro 4562: {
1.157 noro 4563: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4564: struct order_pair *op;
1.1 noro 4565:
1.157 noro 4566: n = nd_nvar;
4567: oepw = (sizeof(UINT)*8)/obpe;
4568: omask0 = (1<<obpe)-1;
4569: TD(r) = TD(d);
4570: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4571: if ( nd_blockmask ) {
4572: l = nd_blockmask->n;
4573: op = nd_blockmask->order_pair;
4574: for ( i = 1; i < nd_exporigin; i++ )
4575: r[i] = d[i];
4576: for ( j = 0, s = 0; j < l; j++ ) {
4577: ord_l = op[j].length;
4578: for ( i = 0; i < ord_l; i++, s++ ) {
4579: ei = GET_EXP_OLD(d,s);
4580: PUT_EXP(r,s,ei);
4581: }
4582: }
4583: } else {
4584: for ( i = 0; i < n; i++ ) {
4585: ei = GET_EXP_OLD(d,i);
4586: PUT_EXP(r,i,ei);
4587: }
4588: }
4589: if ( nd_module ) MPOS(r) = MPOS(d);
1.1 noro 4590: }
1.3 noro 4591:
1.6 noro 4592: ND nd_copy(ND p)
4593: {
1.157 noro 4594: NM m,mr,mr0;
4595: int c,n;
4596: ND r;
4597:
4598: if ( !p )
4599: return 0;
4600: else {
4601: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4602: NEXTNM(mr0,mr);
4603: CM(mr) = CM(m);
4604: ndl_copy(DL(m),DL(mr));
4605: }
4606: NEXT(mr) = 0;
4607: MKND(NV(p),mr0,LEN(p),r);
4608: SG(r) = SG(p);
4609: return r;
4610: }
1.6 noro 4611: }
4612:
1.53 noro 4613: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11 noro 4614: {
1.157 noro 4615: NM m1,m2;
4616: NDV p1,p2;
4617: ND t1,t2;
4618: UINT *lcm;
4619: P gp,tp;
1.167 noro 4620: Q g,t,iq;
1.157 noro 4621: int td;
1.167 noro 4622: LIST hist;
4623: NODE node;
4624: DP d;
1.157 noro 4625:
4626: if ( !mod && nd_demand ) {
4627: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4628: } else {
4629: if ( trace ) {
4630: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4631: } else {
4632: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4633: }
4634: }
4635: lcm = LCM(p);
4636: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4637: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4638: FREENM(m1); return 0;
4639: }
4640: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4641: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4642: FREENM(m1); FREENM(m2); return 0;
4643: }
4644:
4645: if ( mod == -1 ) {
4646: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
1.232 noro 4647: } else if ( mod > 0 ) {
1.157 noro 4648: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
1.232 noro 4649: } else if ( mod == -2 ) {
4650: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
1.157 noro 4651: } else if ( nd_vc ) {
4652: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4653: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4654: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4655: } else {
4656: igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
4657: }
4658: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4659: *rp = nd_add(mod,t1,t2);
1.172 noro 4660: if ( nd_gentrace ) {
1.167 noro 4661: /* nd_tracelist is initialized */
4662: STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
4663: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
4664: STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
4665: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4666: nd_tracelist = node;
4667: }
1.157 noro 4668: FREENM(m1); FREENM(m2);
4669: return 1;
1.11 noro 4670: }
4671:
1.19 noro 4672: void ndv_mul_c(int mod,NDV p,int mul)
1.11 noro 4673: {
1.157 noro 4674: NMV m;
4675: int c,c1,len,i;
1.11 noro 4676:
1.157 noro 4677: if ( !p ) return;
4678: len = LEN(p);
4679: if ( mod == -1 )
4680: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4681: CM(m) = _mulsf(CM(m),mul);
4682: else
4683: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4684: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4685: }
1.11 noro 4686: }
4687:
1.232 noro 4688: void ndv_mul_c_lf(NDV p,GZ mul)
4689: {
4690: NMV m;
4691: GZ c;
4692: int len,i;
4693:
4694: if ( !p ) return;
4695: len = LEN(p);
4696: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4697: mullf(CZ(m),mul,&c); CZ(m) = c;
4698: }
4699: }
4700:
1.113 noro 4701: void ndv_mul_c_q(NDV p,Q mul)
1.16 noro 4702: {
1.157 noro 4703: NMV m;
4704: Q c;
4705: int len,i;
4706:
4707: if ( !p ) return;
4708: len = LEN(p);
4709: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4710: mulq(CQ(m),mul,&c); CQ(m) = c;
4711: }
1.16 noro 4712: }
4713:
1.55 noro 4714: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157 noro 4715: int n2,i,j,l,n,tlen;
4716: UINT *d0;
4717: NM *tab,*psum;
4718: ND s,r;
4719: NM t;
4720: NMV m1;
4721:
4722: if ( !p ) return 0;
4723: n = NV(p); n2 = n>>1;
4724: d0 = DL(m0);
4725: l = LEN(p);
4726: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
1.235 noro 4727: tab = (NM *)MALLOC(tlen*sizeof(NM));
4728: psum = (NM *)MALLOC(tlen*sizeof(NM));
1.157 noro 4729: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4730: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4731: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4732: /* m0(NM) * m1(NMV) => tab(NM) */
4733: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4734: for ( j = 0; j < tlen; j++ ) {
4735: if ( tab[j] ) {
4736: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4737: }
4738: }
4739: }
4740: for ( i = tlen-1, r = 0; i >= 0; i-- )
4741: if ( psum[i] ) {
4742: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4743: MKND(n,psum[i],j,s);
4744: r = nd_add(mod,r,s);
4745: }
4746: if ( r ) SG(r) = SG(p)+TD(d0);
4747: return r;
1.55 noro 4748: }
4749:
1.56 noro 4750: /* product of monomials */
4751: /* XXX block order is not handled correctly */
4752:
1.55 noro 4753: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4754: {
1.157 noro 4755: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4756: UINT *d0,*d1,*d,*dt,*ctab;
4757: Q *ctab_q;
4758: Q q,q1;
4759: UINT c0,c1,c;
4760: NM *p;
4761: NM m,t;
4762: int mpos;
4763:
4764: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4765: if ( !m0 || !m1 ) return;
4766: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166 noro 4767: if ( nd_module )
4768: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4769:
1.157 noro 4770: NEWNM(m); d = DL(m);
4771: if ( mod ) {
4772: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179 noro 4773: } else if ( nd_vc )
4774: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
1.245 noro 4775: else
1.157 noro 4776: mulq(CQ(m0),CQ(m1),&CQ(m));
4777: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4778: homo = n&1 ? 1 : 0;
4779: if ( homo ) {
4780: /* offset of h-degree */
4781: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4782: PUT_EXP(DL(m),n-1,h);
4783: TD(DL(m)) = h;
4784: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4785: }
4786: tab[0] = m;
4787: NEWNM(m); d = DL(m);
4788: for ( i = 0, curlen = 1; i < n2; i++ ) {
4789: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4790: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4791: /* xi^a*(Di^k*xi^l)*Di^b */
4792: a += l; b += k;
4793: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4794: if ( !k || !l ) {
4795: for ( j = 0; j < curlen; j++ )
4796: if ( t = tab[j] ) {
4797: dt = DL(t);
4798: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4799: if ( nd_blockmask ) ndl_weight_mask(dt);
4800: }
4801: curlen *= k+1;
4802: continue;
4803: }
4804: min = MIN(k,l);
4805: if ( mod ) {
1.235 noro 4806: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
1.157 noro 4807: mkwcm(k,l,mod,ctab);
4808: } else {
1.235 noro 4809: ctab_q = (Q *)MALLOC((min+1)*sizeof(Q));
1.157 noro 4810: mkwc(k,l,ctab_q);
4811: }
4812: for ( j = min; j >= 0; j-- ) {
4813: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4814: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4815: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4816: if ( homo ) {
4817: TD(d) = s;
4818: PUT_EXP(d,n-1,s-h);
4819: } else TD(d) = h;
4820: if ( nd_blockmask ) ndl_weight_mask(d);
4821: if ( mod ) c = ctab[j];
4822: else q = ctab_q[j];
4823: p = tab+curlen*j;
4824: if ( j == 0 ) {
4825: for ( u = 0; u < curlen; u++, p++ ) {
4826: if ( tab[u] ) {
4827: ndl_addto(DL(tab[u]),d);
4828: if ( mod ) {
4829: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180 noro 4830: } else if ( nd_vc )
4831: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
1.245 noro 4832: else {
1.157 noro 4833: mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
4834: }
4835: }
4836: }
4837: } else {
4838: for ( u = 0; u < curlen; u++, p++ ) {
4839: if ( tab[u] ) {
4840: NEWNM(t);
4841: ndl_add(DL(tab[u]),d,DL(t));
4842: if ( mod ) {
4843: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181 noro 4844: } else if ( nd_vc )
1.180 noro 4845: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
1.245 noro 4846: else
1.157 noro 4847: mulq(CQ(tab[u]),q,&CQ(t));
4848: *p = t;
4849: }
4850: }
4851: }
4852: }
4853: curlen *= k+1;
4854: }
4855: FREENM(m);
1.167 noro 4856: if ( nd_module ) {
1.166 noro 4857: mpos = MPOS(d1);
1.167 noro 4858: for ( i = 0; i < tlen; i++ )
4859: if ( tab[i] ) {
4860: d = DL(tab[i]);
4861: MPOS(d) = mpos;
4862: TD(d) = ndl_weight(d);
4863: }
4864: }
1.55 noro 4865: }
4866:
1.63 noro 4867: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4868: {
1.157 noro 4869: NM mr,mr0;
4870: NMV m;
4871: UINT *d,*dt,*dm;
4872: int c,n,td,i,c1,c2,len;
4873: Q q;
4874: ND r;
4875:
4876: if ( !p ) return 0;
4877: else {
4878: n = NV(p); m = BDY(p);
4879: d = DL(m0);
4880: len = LEN(p);
4881: mr0 = 0;
4882: td = TD(d);
4883: c = CM(m0);
4884: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4885: NEXTNM(mr0,mr);
4886: CM(mr) = 1;
4887: ndl_add(DL(m),d,DL(mr));
4888: }
4889: NEXT(mr) = 0;
4890: MKND(NV(p),mr0,len,r);
4891: SG(r) = SG(p) + TD(d);
4892: return r;
4893: }
1.63 noro 4894: }
4895:
1.55 noro 4896: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9 noro 4897: {
1.157 noro 4898: NM mr,mr0;
4899: NMV m;
4900: UINT *d,*dt,*dm;
4901: int c,n,td,i,c1,c2,len;
4902: P q;
4903: ND r;
4904:
4905: if ( !p ) return 0;
4906: else if ( do_weyl )
1.232 noro 4907: if ( mod < 0 )
1.157 noro 4908: error("ndv_mul_nm : not implemented (weyl)");
4909: else
4910: return weyl_ndv_mul_nm(mod,m0,p);
4911: else {
4912: n = NV(p); m = BDY(p);
4913: d = DL(m0);
4914: len = LEN(p);
4915: mr0 = 0;
4916: td = TD(d);
4917: if ( mod == -1 ) {
4918: c = CM(m0);
4919: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4920: NEXTNM(mr0,mr);
4921: CM(mr) = _mulsf(CM(m),c);
4922: ndl_add(DL(m),d,DL(mr));
4923: }
1.232 noro 4924: } else if ( mod == -2 ) {
4925: GZ cl;
4926: cl = CZ(m0);
4927: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4928: NEXTNM(mr0,mr);
4929: mullf(CZ(m),cl,&CZ(mr));
4930: ndl_add(DL(m),d,DL(mr));
4931: }
1.157 noro 4932: } else if ( mod ) {
4933: c = CM(m0);
4934: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4935: NEXTNM(mr0,mr);
4936: c1 = CM(m);
4937: DMAR(c1,c,0,mod,c2);
4938: CM(mr) = c2;
4939: ndl_add(DL(m),d,DL(mr));
4940: }
4941: } else {
4942: q = CP(m0);
4943: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4944: NEXTNM(mr0,mr);
4945: mulp(nd_vc,CP(m),q,&CP(mr));
4946: ndl_add(DL(m),d,DL(mr));
4947: }
4948: }
4949: NEXT(mr) = 0;
4950: MKND(NV(p),mr0,len,r);
4951: SG(r) = SG(p) + TD(d);
4952: return r;
4953: }
1.4 noro 4954: }
4955:
1.104 noro 4956: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99 noro 4957: {
1.157 noro 4958: NM mq0,mq;
4959: NMV tm;
4960: Q q;
4961: int i,nv,sg,c,c1,c2,hindex;
4962: ND p,t,r;
4963: N tnm;
4964:
4965: if ( bucket->m < 0 ) return 0;
4966: else {
4967: nv = NV(d);
4968: mq0 = 0;
1.235 noro 4969: tm = (NMV)MALLOC(nmv_adv);
1.157 noro 4970: while ( 1 ) {
1.232 noro 4971: if ( mod > 0 || mod == -1 )
4972: hindex = head_pbucket(mod,bucket);
4973: else if ( mod == -2 )
4974: hindex = head_pbucket_lf(bucket);
4975: else
4976: hindex = head_pbucket_q(bucket);
1.157 noro 4977: if ( hindex < 0 ) break;
4978: p = bucket->body[hindex];
4979: NEXTNM(mq0,mq);
4980: ndl_sub(HDL(p),HDL(d),DL(mq));
4981: ndl_copy(DL(mq),DL(tm));
4982: if ( mod ) {
4983: c1 = invm(HCM(d),mod); c2 = HCM(p);
4984: DMAR(c1,c2,0,mod,c); CM(mq) = c;
4985: CM(tm) = mod-c;
4986: } else {
4987: divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
4988: NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
4989: chsgnq(CQ(mq),&CQ(tm));
4990: }
4991: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
4992: bucket->body[hindex] = nd_remove_head(p);
4993: t = nd_remove_head(t);
4994: add_pbucket(mod,bucket,t);
4995: }
4996: if ( !mq0 )
4997: r = 0;
4998: else {
4999: NEXT(mq) = 0;
5000: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5001: MKND(nv,mq0,i,r);
5002: /* XXX */
5003: SG(r) = HTD(r);
5004: }
5005: return r;
5006: }
1.99 noro 5007: }
5008:
1.43 noro 5009: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11 noro 5010: {
1.157 noro 5011: NMV m,mr,mr0,t;
5012: int len,i,k;
1.11 noro 5013:
1.157 noro 5014: if ( !p ) return;
5015: m = BDY(p); len = LEN(p);
5016: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5017: m = (NMV)((char *)mr0+(len-1)*oadv);
5018: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235 noro 5019: t = (NMV)MALLOC(nmv_adv);
1.157 noro 5020: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
5021: CQ(t) = CQ(m);
5022: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5023: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
5024: CQ(mr) = CQ(t);
5025: ndl_copy(DL(t),DL(mr));
5026: }
5027: BDY(p) = mr0;
1.61 noro 5028: }
5029:
5030: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5031: {
1.157 noro 5032: NMV m,mr,mr0;
5033: int len,i;
5034: NDV r;
5035:
5036: if ( !p ) return 0;
5037: m = BDY(p); len = LEN(p);
5038: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5039: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5040: ndl_zero(DL(mr));
5041: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
5042: CQ(mr) = CQ(m);
5043: }
5044: MKNDV(NV(p),mr0,len,r);
5045: SG(r) = SG(p);
5046: return r;
1.11 noro 5047: }
5048:
1.61 noro 5049: /* duplicate p */
5050:
5051: NDV ndv_dup(int mod,NDV p)
1.3 noro 5052: {
1.157 noro 5053: NDV d;
5054: NMV t,m,m0;
5055: int i,len;
5056:
5057: if ( !p ) return 0;
5058: len = LEN(p);
1.232 noro 5059: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.157 noro 5060: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5061: ndl_copy(DL(t),DL(m));
5062: CQ(m) = CQ(t);
5063: }
5064: MKNDV(NV(p),m0,len,d);
5065: SG(d) = SG(p);
5066: return d;
1.23 noro 5067: }
5068:
1.215 noro 5069: NDV ndvtondvgz(NDV p)
5070: {
1.245 noro 5071: NDV r;
5072: int len,i;
5073: NMV t;
1.215 noro 5074:
1.245 noro 5075: r = ndv_dup(0,p);
5076: len = LEN(p);
1.215 noro 5077: for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CZ(t) = ztogz(CQ(t));
1.245 noro 5078: return r;
1.215 noro 5079: }
5080:
5081: NDV ndvgztondv(NDV p)
5082: {
1.245 noro 5083: NDV r;
5084: int len,i;
5085: NMV t;
1.215 noro 5086:
1.245 noro 5087: r = ndv_dup(0,p);
5088: len = LEN(p);
1.215 noro 5089: for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CQ(t) = gztoz(CZ(t));
1.245 noro 5090: return r;
1.215 noro 5091: }
5092:
5093: NDV ndv_symbolic(int mod,NDV p)
5094: {
5095: NDV d;
5096: NMV t,m,m0;
5097: int i,len;
5098:
5099: if ( !p ) return 0;
5100: len = LEN(p);
1.232 noro 5101: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.215 noro 5102: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5103: ndl_copy(DL(t),DL(m));
5104: CQ(m) = ONE;
5105: }
5106: MKNDV(NV(p),m0,len,d);
5107: SG(d) = SG(p);
5108: return d;
5109: }
5110:
1.63 noro 5111: ND nd_dup(ND p)
5112: {
1.157 noro 5113: ND d;
5114: NM t,m,m0;
1.63 noro 5115:
1.157 noro 5116: if ( !p ) return 0;
5117: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5118: NEXTNM(m0,m);
5119: ndl_copy(DL(t),DL(m));
5120: CQ(m) = CQ(t);
5121: }
5122: if ( m0 ) NEXT(m) = 0;
5123: MKND(NV(p),m0,LEN(p),d);
5124: SG(d) = SG(p);
5125: return d;
1.63 noro 5126: }
5127:
1.215 noro 5128: ND ndtondgz(ND p)
5129: {
1.245 noro 5130: ND r;
5131: NM t;
1.215 noro 5132:
1.245 noro 5133: r = nd_dup(p);
1.215 noro 5134: for ( t = BDY(r); t; t = NEXT(t) ) CZ(t) = ztogz(CQ(t));
1.245 noro 5135: return r;
1.215 noro 5136: }
5137:
5138:
5139: ND ndgztond(ND p)
5140: {
1.245 noro 5141: ND r;
5142: NM t;
1.215 noro 5143:
1.245 noro 5144: r = nd_dup(p);
1.215 noro 5145: for ( t = BDY(r); t; t = NEXT(t) ) CQ(t) = gztoz(CZ(t));
1.245 noro 5146: return r;
1.215 noro 5147: }
5148:
5149:
1.61 noro 5150: /* XXX if p->len == 0 then it represents 0 */
5151:
5152: void ndv_mod(int mod,NDV p)
5153: {
1.157 noro 5154: NMV t,d;
5155: int r,s,u;
5156: int i,len,dlen;
5157: P cp;
5158: Q c;
5159: Obj gfs;
5160:
5161: if ( !p ) return;
5162: len = LEN(p);
5163: dlen = 0;
5164: if ( mod == -1 )
5165: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5166: simp_ff((Obj)CP(t),&gfs);
1.235 noro 5167: if ( gfs ) {
5168: r = FTOIF(CONT((GFS)gfs));
5169: CM(d) = r;
5170: ndl_copy(DL(t),DL(d));
5171: NMV_ADV(d);
5172: dlen++;
5173: }
1.157 noro 5174: }
1.232 noro 5175: else if ( mod == -2 )
5176: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.235 noro 5177: simp_ff((Obj)CP(t),&gfs);
5178: if ( gfs ) {
5179: lmtolf(gfs,&CZ(d));
5180: ndl_copy(DL(t),DL(d));
5181: NMV_ADV(d);
5182: dlen++;
5183: }
1.232 noro 5184: }
1.157 noro 5185: else
5186: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5187: if ( nd_vc ) {
5188: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5189: c = (Q)cp;
5190: } else
5191: c = CQ(t);
5192: r = rem(NM(c),mod);
5193: if ( r ) {
5194: if ( SGN(c) < 0 )
5195: r = mod-r;
5196: if ( DN(c) ) {
5197: s = rem(DN(c),mod);
5198: if ( !s )
5199: error("ndv_mod : division by 0");
5200: s = invm(s,mod);
5201: DMAR(r,s,0,mod,u); r = u;
5202: }
5203: CM(d) = r;
5204: ndl_copy(DL(t),DL(d));
5205: NMV_ADV(d);
5206: dlen++;
5207: }
5208: }
5209: LEN(p) = dlen;
1.61 noro 5210: }
5211:
5212: NDV ptondv(VL vl,VL dvl,P p)
5213: {
1.157 noro 5214: ND nd;
5215:
5216: nd = ptond(vl,dvl,p);
5217: return ndtondv(0,nd);
5218: }
1.61 noro 5219:
1.157 noro 5220: void pltozpl(LIST l,Q *cont,LIST *pp)
5221: {
5222: NODE nd,nd1;
5223: int n;
5224: P *pl;
5225: Q *cl;
5226: int i;
5227: P dmy;
5228: Q dvr;
5229: LIST r;
5230:
5231: nd = BDY(l); n = length(nd);
1.235 noro 5232: pl = (P *)MALLOC(n*sizeof(P));
5233: cl = (Q *)MALLOC(n*sizeof(P));
1.157 noro 5234: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5235: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5236: qltozl(cl,n,&dvr);
5237: nd = BDY(l);
5238: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5239: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5240: }
5241: nd = 0;
5242: for ( i = n-1; i >= 0; i-- ) {
5243: MKNODE(nd1,pl[i],nd); nd = nd1;
5244: }
5245: MKLIST(r,nd);
5246: *pp = r;
5247: }
5248:
5249: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5250:
5251: NDV pltondv(VL vl,VL dvl,LIST p)
5252: {
5253: int i;
5254: NODE t;
5255: ND r,ri;
5256: NM m;
5257:
5258: if ( !nd_module ) error("pltond : module order must be set");
5259: r = 0;
5260: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5261: ri = ptond(vl,dvl,(P)BDY(t));
1.163 noro 5262: if ( ri )
5263: for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167 noro 5264: MPOS(DL(m)) = i;
5265: TD(DL(m)) = ndl_weight(DL(m));
1.163 noro 5266: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5267: }
1.157 noro 5268: r = nd_add(0,r,ri);
5269: }
5270: return ndtondv(0,r);
1.61 noro 5271: }
5272:
5273: ND ptond(VL vl,VL dvl,P p)
1.23 noro 5274: {
1.157 noro 5275: int n,i,j,k,e;
5276: VL tvl;
5277: V v;
5278: DCP dc;
5279: DCP *w;
5280: ND r,s,t,u;
5281: P x;
5282: int c;
5283: UINT *d;
5284: NM m,m0;
5285:
5286: if ( !p )
5287: return 0;
5288: else if ( NUM(p) ) {
5289: NEWNM(m);
5290: ndl_zero(DL(m));
5291: CQ(m) = (Q)p;
5292: NEXT(m) = 0;
5293: MKND(nd_nvar,m,1,r);
5294: SG(r) = 0;
5295: return r;
5296: } else {
5297: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
1.235 noro 5298: w = (DCP *)MALLOC(k*sizeof(DCP));
1.157 noro 5299: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5300: for ( i = 0, tvl = dvl, v = VR(p);
5301: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5302: if ( !tvl ) {
5303: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5304: t = ptond(vl,dvl,COEF(w[j]));
5305: pwrp(vl,x,DEG(w[j]),&p);
5306: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5307: }
5308: return s;
5309: } else {
5310: NEWNM(m0); d = DL(m0);
5311: for ( j = k-1, s = 0; j >= 0; j-- ) {
5312: ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
5313: TD(d) = MUL_WEIGHT(e,i);
5314: if ( nd_blockmask) ndl_weight_mask(d);
5315: if ( nd_module ) MPOS(d) = 0;
5316: t = ptond(vl,dvl,COEF(w[j]));
5317: for ( m = BDY(t); m; m = NEXT(m) )
5318: ndl_addto(DL(m),d);
5319: SG(t) += TD(d);
5320: s = nd_add(0,s,t);
5321: }
5322: FREENM(m0);
5323: return s;
5324: }
5325: }
1.61 noro 5326: }
5327:
5328: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5329: {
1.157 noro 5330: VL tvl;
5331: int len,n,j,i,e;
5332: NMV m;
5333: Q q;
5334: P c;
5335: UINT *d;
5336: P s,r,u,t,w;
5337: GFS gfs;
5338:
5339: if ( !p ) return 0;
5340: else {
5341: len = LEN(p);
5342: n = NV(p);
5343: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5344: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5345: if ( mod == -1 ) {
1.232 noro 5346: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5347: } else if ( mod == -2 ) {
1.238 noro 5348: c = (P)gztoz(CZ(m));
1.232 noro 5349: } else if ( mod > 0 ) {
1.157 noro 5350: STOQ(CM(m),q); c = (P)q;
5351: } else
5352: c = CP(m);
5353: d = DL(m);
5354: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
5355: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
5356: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5357: }
5358: addp(vl,s,t,&u); s = u;
5359: }
5360: return s;
5361: }
5362: }
5363:
5364: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5365: {
5366: VL tvl;
5367: int len,n,j,i,e;
5368: NMV m;
5369: Q q;
5370: P c;
5371: UINT *d;
5372: P s,r,u,t,w;
5373: GFS gfs;
5374: P *a;
5375: LIST l;
5376: NODE nd,nd1;
5377:
5378: if ( !p ) return 0;
5379: else {
1.235 noro 5380: a = (P *)MALLOC((rank+1)*sizeof(P));
1.157 noro 5381: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5382: len = LEN(p);
5383: n = NV(p);
5384: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5385: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5386: if ( mod == -1 ) {
5387: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5388: } else if ( mod ) {
5389: STOQ(CM(m),q); c = (P)q;
5390: } else
5391: c = CP(m);
5392: d = DL(m);
5393: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
5394: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
5395: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5396: }
5397: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5398: }
5399: nd = 0;
5400: for ( i = rank; i > 0; i-- ) {
5401: MKNODE(nd1,a[i],nd); nd = nd1;
5402: }
5403: MKLIST(l,nd);
5404: return l;
5405: }
1.3 noro 5406: }
5407:
1.61 noro 5408: NDV ndtondv(int mod,ND p)
1.11 noro 5409: {
1.157 noro 5410: NDV d;
5411: NMV m,m0;
5412: NM t;
5413: int i,len;
5414:
5415: if ( !p ) return 0;
5416: len = LEN(p);
1.232 noro 5417: if ( mod > 0 || mod == -1 )
1.200 noro 5418: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157 noro 5419: else
5420: m0 = m = MALLOC(len*nmv_adv);
1.103 noro 5421: #if 0
1.157 noro 5422: ndv_alloc += nmv_adv*len;
1.103 noro 5423: #endif
1.157 noro 5424: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5425: ndl_copy(DL(t),DL(m));
5426: CQ(m) = CQ(t);
5427: }
5428: MKNDV(NV(p),m0,len,d);
5429: SG(d) = SG(p);
5430: return d;
1.11 noro 5431: }
5432:
1.61 noro 5433: ND ndvtond(int mod,NDV p)
1.11 noro 5434: {
1.157 noro 5435: ND d;
5436: NM m,m0;
5437: NMV t;
5438: int i,len;
5439:
5440: if ( !p ) return 0;
5441: m0 = 0;
5442: len = p->len;
5443: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5444: NEXTNM(m0,m);
5445: ndl_copy(DL(t),DL(m));
5446: CQ(m) = CQ(t);
5447: }
5448: NEXT(m) = 0;
5449: MKND(NV(p),m0,len,d);
5450: SG(d) = SG(p);
5451: return d;
1.11 noro 5452: }
5453:
1.198 noro 5454: DP ndvtodp(int mod,NDV p)
5455: {
5456: MP m,m0;
1.245 noro 5457: DP d;
1.198 noro 5458: NMV t;
5459: int i,len;
5460:
5461: if ( !p ) return 0;
5462: m0 = 0;
5463: len = p->len;
5464: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5465: NEXTMP(m0,m);
1.245 noro 5466: m->dl = ndltodl(nd_nvar,DL(t));
5467: m->c = (Obj)ndctop(mod,t->c);
1.198 noro 5468: }
5469: NEXT(m) = 0;
1.245 noro 5470: MKDP(nd_nvar,m0,d);
1.198 noro 5471: SG(d) = SG(p);
5472: return d;
5473: }
5474:
1.204 noro 5475: DP ndtodp(int mod,ND p)
5476: {
5477: MP m,m0;
1.245 noro 5478: DP d;
1.204 noro 5479: NM t;
5480: int i,len;
5481:
5482: if ( !p ) return 0;
5483: m0 = 0;
5484: len = p->len;
5485: for ( t = BDY(p); t; t = NEXT(t) ) {
5486: NEXTMP(m0,m);
1.245 noro 5487: m->dl = ndltodl(nd_nvar,DL(t));
5488: m->c = (Obj)ndctop(mod,t->c);
1.204 noro 5489: }
5490: NEXT(m) = 0;
1.245 noro 5491: MKDP(nd_nvar,m0,d);
1.204 noro 5492: SG(d) = SG(p);
5493: return d;
5494: }
5495:
1.3 noro 5496: void ndv_print(NDV p)
5497: {
1.157 noro 5498: NMV m;
5499: int i,len;
1.3 noro 5500:
1.157 noro 5501: if ( !p ) printf("0\n");
5502: else {
5503: len = LEN(p);
5504: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5505: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5506: else printf("+%d*",CM(m));
5507: ndl_print(DL(m));
5508: }
5509: printf("\n");
5510: }
1.16 noro 5511: }
5512:
1.113 noro 5513: void ndv_print_q(NDV p)
1.16 noro 5514: {
1.157 noro 5515: NMV m;
5516: int i,len;
1.16 noro 5517:
1.157 noro 5518: if ( !p ) printf("0\n");
5519: else {
5520: len = LEN(p);
5521: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5522: printf("+");
5523: printexpr(CO,(Obj)CQ(m));
5524: printf("*");
5525: ndl_print(DL(m));
5526: }
5527: printf("\n");
5528: }
1.25 noro 5529: }
5530:
1.167 noro 5531: NODE ndv_reducebase(NODE x,int *perm)
1.27 noro 5532: {
1.157 noro 5533: int len,i,j;
1.167 noro 5534: NDVI w;
1.157 noro 5535: NODE t,t0;
5536:
5537: len = length(x);
1.235 noro 5538: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
1.167 noro 5539: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5540: w[i].p = BDY(t); w[i].i = perm[i];
5541: }
1.157 noro 5542: for ( i = 0; i < len; i++ ) {
5543: for ( j = 0; j < i; j++ ) {
1.167 noro 5544: if ( w[i].p && w[j].p )
5545: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5546: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157 noro 5547: }
5548: }
1.167 noro 5549: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5550: if ( w[i].p ) {
5551: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5552: perm[j++] = w[i].i;
5553: }
1.157 noro 5554: }
5555: NEXT(t) = 0; x = t0;
5556: return x;
1.11 noro 5557: }
1.32 noro 5558:
1.43 noro 5559: /* XXX incomplete */
5560:
1.32 noro 5561: void nd_init_ord(struct order_spec *ord)
5562: {
1.224 noro 5563: nd_module = (ord->id >= 256);
5564: if ( nd_module ) {
5565: nd_dcomp = -1;
5566: nd_ispot = ord->ispot;
5567: nd_pot_nelim = ord->pot_nelim;
5568: nd_poly_weight_len = ord->nv;
5569: nd_poly_weight = ord->top_weight;
5570: nd_module_rank = ord->module_rank;
5571: nd_module_weight = ord->module_top_weight;
5572: }
1.245 noro 5573: nd_matrix = 0;
5574: nd_matrix_len = 0;
1.157 noro 5575: switch ( ord->id ) {
5576: case 0:
5577: switch ( ord->ord.simple ) {
5578: case 0:
5579: nd_dcomp = 1;
5580: nd_isrlex = 1;
5581: break;
5582: case 1:
5583: nd_dcomp = 1;
5584: nd_isrlex = 0;
5585: break;
5586: case 2:
5587: nd_dcomp = 0;
5588: nd_isrlex = 0;
5589: ndl_compare_function = ndl_lex_compare;
5590: break;
5591: case 11:
5592: /* XXX */
5593: nd_dcomp = 0;
5594: nd_isrlex = 1;
5595: ndl_compare_function = ndl_ww_lex_compare;
5596: break;
5597: default:
5598: error("nd_gr : unsupported order");
5599: }
5600: break;
5601: case 1:
5602: /* block order */
5603: /* XXX */
5604: nd_dcomp = -1;
5605: nd_isrlex = 0;
5606: ndl_compare_function = ndl_block_compare;
5607: break;
5608: case 2:
5609: /* matrix order */
5610: /* XXX */
5611: nd_dcomp = -1;
5612: nd_isrlex = 0;
5613: nd_matrix_len = ord->ord.matrix.row;
5614: nd_matrix = ord->ord.matrix.matrix;
5615: ndl_compare_function = ndl_matrix_compare;
5616: break;
5617: case 3:
5618: /* composite order */
5619: nd_dcomp = -1;
5620: nd_isrlex = 0;
5621: nd_worb_len = ord->ord.composite.length;
5622: nd_worb = ord->ord.composite.w_or_b;
5623: ndl_compare_function = ndl_composite_compare;
5624: break;
5625:
5626: /* module order */
5627: case 256:
5628: switch ( ord->ord.simple ) {
5629: case 0:
1.167 noro 5630: nd_isrlex = 1;
1.157 noro 5631: ndl_compare_function = ndl_module_grlex_compare;
5632: break;
5633: case 1:
1.167 noro 5634: nd_isrlex = 0;
1.157 noro 5635: ndl_compare_function = ndl_module_glex_compare;
5636: break;
5637: case 2:
1.167 noro 5638: nd_isrlex = 0;
1.157 noro 5639: ndl_compare_function = ndl_module_lex_compare;
5640: break;
5641: default:
5642: error("nd_gr : unsupported order");
5643: }
5644: break;
5645: case 257:
5646: /* block order */
1.174 noro 5647: nd_isrlex = 0;
1.157 noro 5648: ndl_compare_function = ndl_module_block_compare;
5649: break;
5650: case 258:
5651: /* matrix order */
1.174 noro 5652: nd_isrlex = 0;
1.157 noro 5653: nd_matrix_len = ord->ord.matrix.row;
5654: nd_matrix = ord->ord.matrix.matrix;
5655: ndl_compare_function = ndl_module_matrix_compare;
5656: break;
5657: case 259:
5658: /* composite order */
1.174 noro 5659: nd_isrlex = 0;
1.157 noro 5660: nd_worb_len = ord->ord.composite.length;
5661: nd_worb = ord->ord.composite.w_or_b;
5662: ndl_compare_function = ndl_module_composite_compare;
5663: break;
5664: }
5665: nd_ord = ord;
1.32 noro 5666: }
5667:
1.43 noro 5668: BlockMask nd_create_blockmask(struct order_spec *ord)
5669: {
1.157 noro 5670: int n,i,j,s,l;
5671: UINT *t;
5672: BlockMask bm;
5673:
5674: /* we only create mask table for block order */
1.164 noro 5675: if ( ord->id != 1 && ord->id != 257 )
1.157 noro 5676: return 0;
5677: n = ord->ord.block.length;
5678: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5679: bm->n = n;
5680: bm->order_pair = ord->ord.block.order_pair;
5681: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5682: for ( i = 0, s = 0; i < n; i++ ) {
5683: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5684: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5685: l = bm->order_pair[i].length;
5686: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5687: }
5688: return bm;
1.57 noro 5689: }
5690:
5691: EPOS nd_create_epos(struct order_spec *ord)
5692: {
1.157 noro 5693: int i,j,l,s,ord_l,ord_o;
5694: EPOS epos;
5695: struct order_pair *op;
5696:
5697: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5698: switch ( ord->id ) {
1.164 noro 5699: case 0: case 256:
1.157 noro 5700: if ( nd_isrlex ) {
5701: for ( i = 0; i < nd_nvar; i++ ) {
5702: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5703: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5704: }
5705: } else {
5706: for ( i = 0; i < nd_nvar; i++ ) {
5707: epos[i].i = nd_exporigin + i/nd_epw;
5708: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5709: }
5710: }
5711: break;
1.164 noro 5712: case 1: case 257:
1.157 noro 5713: /* block order */
5714: l = ord->ord.block.length;
5715: op = ord->ord.block.order_pair;
5716: for ( j = 0, s = 0; j < l; j++ ) {
5717: ord_o = op[j].order;
5718: ord_l = op[j].length;
5719: if ( !ord_o )
5720: for ( i = 0; i < ord_l; i++ ) {
5721: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5722: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5723: }
5724: else
5725: for ( i = 0; i < ord_l; i++ ) {
5726: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5727: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5728: }
5729: s += ord_l;
5730: }
5731: break;
5732: case 2:
5733: /* matrix order */
5734: case 3:
5735: /* composite order */
1.167 noro 5736: default:
1.157 noro 5737: for ( i = 0; i < nd_nvar; i++ ) {
5738: epos[i].i = nd_exporigin + i/nd_epw;
5739: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5740: }
5741: break;
5742: }
5743: return epos;
1.43 noro 5744: }
1.59 noro 5745:
5746: /* external interface */
5747:
1.191 noro 5748: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59 noro 5749: {
1.157 noro 5750: NODE t,in0,in;
1.191 noro 5751: ND ndf,nf;
5752: NDV ndvf;
1.157 noro 5753: VL vv,tv;
1.191 noro 5754: int stat,nvar,max,mrank;
1.157 noro 5755: union oNDC dn;
5756: Q cont;
5757: P pp;
1.191 noro 5758: LIST ppl;
1.157 noro 5759:
5760: if ( !f ) {
5761: *rp = 0;
5762: return;
5763: }
5764: pltovl(v,&vv);
5765: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5766:
1.191 noro 5767: /* max=65536 implies nd_bpe=32 */
5768: max = 65536;
1.157 noro 5769:
1.245 noro 5770: nd_module = 0;
5771: /* nd_module will be set if ord is a module ordering */
1.157 noro 5772: nd_init_ord(ord);
5773: nd_setup_parameters(nvar,max);
1.191 noro 5774: if ( nd_module && OID(f) != O_LIST )
5775: error("nd_nf_p : the first argument must be a list");
1.245 noro 5776: if ( nd_module ) mrank = length(BDY((LIST)f));
1.157 noro 5777: /* conversion to ndv */
5778: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5779: NEXTNODE(in0,in);
1.191 noro 5780: if ( nd_module ) {
5781: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5782: || length(BDY((LIST)BDY(t))) != mrank )
5783: error("nd_nf_p : inconsistent basis element");
5784: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5785: else ppl = (LIST)BDY(t);
5786: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5787: } else {
5788: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5789: else pp = (P)BDY(t);
5790: BDY(in) = (pointer)ptondv(CO,vv,pp);
5791: }
1.157 noro 5792: if ( m ) ndv_mod(m,(NDV)BDY(in));
5793: }
1.191 noro 5794: if ( in0 ) NEXT(in) = 0;
5795:
5796: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5797: else ndvf = ptondv(CO,vv,(P)f);
5798: if ( m ) ndv_mod(m,ndvf);
5799: ndf = (pointer)ndvtond(m,ndvf);
1.157 noro 5800:
5801: /* dont sort, dont removecont */
5802: ndv_setup(m,0,in0,1,1);
5803: nd_scale=2;
1.191 noro 5804: stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
5805: if ( !stat )
5806: error("nd_nf_p : exponent too large");
5807: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5808: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63 noro 5809: }
5810:
5811: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5812: {
1.157 noro 5813: NM m;
5814: UINT *t,*s;
5815: int i;
5816:
5817: for ( i = 0; i < n; i++ ) r[i] = 0;
5818: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5819: t = DL(m);
5820: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5821: r[i] = CM(m);
5822: }
5823: for ( i = 0; !r[i]; i++ );
5824: return i;
1.63 noro 5825: }
5826:
1.242 noro 5827: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240 noro 5828:
5829: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
5830:
5831: int nd_to_vect64(int mod,UINT *s0,int n,ND d,U64 *r)
1.239 noro 5832: {
5833: NM m;
5834: UINT *t,*s;
5835: int i;
5836:
5837: for ( i = 0; i < n; i++ ) r[i] = 0;
5838: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5839: t = DL(m);
5840: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.240 noro 5841: r[i] = (U64)CM(m);
1.239 noro 5842: }
5843: for ( i = 0; !r[i]; i++ );
5844: return i;
5845: }
5846: #endif
5847:
1.113 noro 5848: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74 noro 5849: {
1.157 noro 5850: NM m;
5851: UINT *t,*s;
5852: int i;
5853:
5854: for ( i = 0; i < n; i++ ) r[i] = 0;
5855: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5856: t = DL(m);
5857: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5858: r[i] = CQ(m);
5859: }
5860: for ( i = 0; !r[i]; i++ );
5861: return i;
1.74 noro 5862: }
5863:
1.232 noro 5864: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5865: {
5866: NM m;
5867: UINT *t,*s;
5868: int i;
5869:
5870: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5871: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5872: t = DL(m);
5873: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5874: mpz_set(r[i],BDY(CZ(m)));
5875: }
5876: for ( i = 0; !mpz_sgn(r[i]); i++ );
5877: return i;
5878: }
5879:
1.220 noro 5880: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
5881: {
5882: NM m;
5883: unsigned long *v;
5884: int i,j,h,size;
1.245 noro 5885: UINT *s,*t;
1.220 noro 5886:
1.245 noro 5887: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
1.220 noro 5888: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
5889: bzero(v,size);
5890: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
1.245 noro 5891: t = DL(m);
5892: h = ndl_hash_value(t);
1.220 noro 5893: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.245 noro 5894: v[i/BLEN] |= 1L <<(i%BLEN);
1.220 noro 5895: }
5896: return v;
5897: }
5898:
5899: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
5900: {
5901: NMV mr;
5902: UINT *d,*t,*s;
5903: int i,j,len,h,head;
5904:
5905: d = DL(m);
5906: len = LEN(p);
1.235 noro 5907: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.220 noro 5908: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5909: ndl_add(d,DL(mr),t);
1.245 noro 5910: h = ndl_hash_value(t);
1.220 noro 5911: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.245 noro 5912: if ( j == 0 ) head = i;
5913: v[i/BLEN] |= 1L <<(i%BLEN);
1.220 noro 5914: }
5915: return head;
5916: }
5917:
1.129 noro 5918: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
5919: {
1.157 noro 5920: NM m;
5921: NMV mr;
5922: UINT *d,*t,*s;
5923: NDV p;
5924: int i,j,len;
5925: Q *r;
5926:
5927: m = pair->mul;
5928: d = DL(m);
5929: p = nd_ps[pair->index];
5930: len = LEN(p);
5931: r = (Q *)CALLOC(n,sizeof(Q));
1.235 noro 5932: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157 noro 5933: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5934: ndl_add(d,DL(mr),t);
5935: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5936: r[i] = CQ(mr);
5937: }
5938: return r;
1.129 noro 5939: }
5940:
1.235 noro 5941: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
1.64 noro 5942: {
1.157 noro 5943: NM m;
5944: NMV mr;
5945: UINT *d,*t,*s;
5946: NDV p;
5947: unsigned char *ivc;
5948: unsigned short *ivs;
5949: UINT *v,*ivi,*s0v;
1.210 noro 5950: int i,j,len,prev,diff,cdiff,h;
1.157 noro 5951: IndArray r;
1.198 noro 5952: struct oEGT eg0,eg1;
1.157 noro 5953:
5954: m = pair->mul;
5955: d = DL(m);
1.235 noro 5956: if ( trace )
5957: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
5958: else
5959: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
5960:
1.157 noro 5961: len = LEN(p);
1.235 noro 5962: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5963: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.198 noro 5964: get_eg(&eg0);
1.157 noro 5965: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5966: ndl_add(d,DL(mr),t);
1.245 noro 5967: h = ndl_hash_value(t);
1.210 noro 5968: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.157 noro 5969: v[j] = i;
5970: }
1.198 noro 5971: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157 noro 5972: r = (IndArray)MALLOC(sizeof(struct oIndArray));
5973: r->head = v[0];
5974: diff = 0;
5975: for ( i = 1; i < len; i++ ) {
5976: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
5977: }
5978: if ( diff < 256 ) {
5979: r->width = 1;
5980: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
5981: r->index.c = ivc;
5982: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
5983: } else if ( diff < 65536 ) {
5984: r->width = 2;
5985: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
5986: r->index.s = ivs;
5987: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
5988: } else {
5989: r->width = 4;
5990: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
5991: r->index.i = ivi;
5992: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
5993: }
5994: return r;
1.64 noro 5995: }
5996:
1.135 noro 5997: int compress_array(Q *svect,Q *cvect,int n)
5998: {
1.157 noro 5999: int i,j;
1.135 noro 6000:
1.157 noro 6001: for ( i = j = 0; i < n; i++ )
6002: if ( svect[i] ) cvect[j++] = svect[i];
6003: return j;
1.135 noro 6004: }
6005:
6006: void expand_array(Q *svect,Q *cvect,int n)
6007: {
1.157 noro 6008: int i,j;
1.135 noro 6009:
1.157 noro 6010: for ( i = j = 0; j < n; i++ )
6011: if ( svect[i] ) svect[i] = cvect[j++];
1.135 noro 6012: }
6013:
1.133 noro 6014: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107 noro 6015: {
1.157 noro 6016: int i,j,k,len,pos,prev,nz;
6017: Q cs,mcs,c1,c2,cr,gcd,t;
6018: IndArray ivect;
6019: unsigned char *ivc;
6020: unsigned short *ivs;
6021: unsigned int *ivi;
6022: NDV redv;
6023: NMV mr;
6024: NODE rp;
6025: int maxrs;
6026: double hmag;
6027: Q *cvect;
6028:
6029: maxrs = 0;
6030: for ( i = 0; i < col && !svect[i]; i++ );
6031: if ( i == col ) return maxrs;
6032: hmag = p_mag((P)svect[i])*nd_scale;
1.235 noro 6033: cvect = (Q *)MALLOC(col*sizeof(Q));
1.157 noro 6034: for ( i = 0; i < nred; i++ ) {
6035: ivect = imat[i];
6036: k = ivect->head;
6037: if ( svect[k] ) {
6038: maxrs = MAX(maxrs,rp0[i]->sugar);
1.215 noro 6039: redv = nd_demand?ndv_load(rp0[i]->index)
6040: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
1.157 noro 6041: len = LEN(redv); mr = BDY(redv);
6042: igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
6043: chsgnq(cs,&mcs);
6044: if ( !UNIQ(cr) ) {
6045: for ( j = 0; j < col; j++ ) {
6046: mulq(svect[j],cr,&c1); svect[j] = c1;
6047: }
6048: }
6049: svect[k] = 0; prev = k;
6050: switch ( ivect->width ) {
6051: case 1:
6052: ivc = ivect->index.c;
6053: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6054: pos = prev+ivc[j]; prev = pos;
6055: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
6056: }
6057: break;
6058: case 2:
6059: ivs = ivect->index.s;
6060: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6061: pos = prev+ivs[j]; prev = pos;
6062: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
6063: }
6064: break;
6065: case 4:
6066: ivi = ivect->index.i;
6067: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6068: pos = prev+ivi[j]; prev = pos;
6069: mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
6070: }
6071: break;
6072: }
6073: for ( j = k+1; j < col && !svect[j]; j++ );
6074: if ( j == col ) break;
6075: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6076: nz = compress_array(svect,cvect,col);
6077: removecont_array((P *)cvect,nz,1);
6078: expand_array(svect,cvect,nz);
6079: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6080: }
6081: }
6082: }
6083: nz = compress_array(svect,cvect,col);
6084: removecont_array((P *)cvect,nz,1);
6085: expand_array(svect,cvect,nz);
6086: if ( DP_Print ) {
6087: fprintf(asir_out,"-"); fflush(asir_out);
6088: }
6089: return maxrs;
1.107 noro 6090: }
6091:
1.215 noro 6092: int ndv_reduce_vect_gz(GZ *gvect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6093: {
6094: int i,j,k,l,len,pos,prev,nz;
6095: GZ cs,mcs,c1,c2,cr,gcd,t;
6096: IndArray ivect;
6097: unsigned char *ivc;
6098: unsigned short *ivs;
6099: unsigned int *ivi;
6100: NDV redv;
6101: NMV mr;
6102: NODE rp;
6103: int maxrs;
6104: double hmag;
1.245 noro 6105: struct oVECT v;
1.215 noro 6106:
6107: maxrs = 0;
6108: for ( i = 0; i < col && !gvect[i]; i++ );
6109: if ( i == col ) return maxrs;
6110: hmag = (double)n_bits_gz(gvect[i])*nd_scale;
6111: for ( i = 0; i < nred; i++ ) {
6112: ivect = imat[i];
6113: k = ivect->head;
6114: if ( gvect[k] ) {
6115: maxrs = MAX(maxrs,rp0[i]->sugar);
6116: redv = nd_ps_gz[rp0[i]->index];
6117: len = LEN(redv); mr = BDY(redv);
1.245 noro 6118: gcdgz(gvect[k],CZ(mr),&gcd);
6119: divsgz(gvect[k],gcd,&cs);
6120: divsgz(CZ(mr),gcd,&cr);
1.215 noro 6121: chsgngz(cs,&mcs);
1.245 noro 6122: if ( !UNIGZ(cr) ) {
1.215 noro 6123: for ( j = 0; j < col; j++ ) {
6124: mulgz(gvect[j],cr,&c1); gvect[j] = c1;
6125: }
6126: }
6127: gvect[k] = 0; prev = k;
6128: switch ( ivect->width ) {
6129: case 1:
6130: ivc = ivect->index.c;
6131: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6132: pos = prev+ivc[j]; prev = pos;
6133: mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
6134: }
6135: break;
6136: case 2:
6137: ivs = ivect->index.s;
6138: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6139: pos = prev+ivs[j]; prev = pos;
6140: mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
6141: }
6142: break;
6143: case 4:
6144: ivi = ivect->index.i;
6145: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6146: pos = prev+ivi[j]; prev = pos;
6147: mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
6148: }
6149: break;
6150: }
6151: for ( j = k+1; j < col && !gvect[j]; j++ );
6152: if ( j == col ) break;
6153: if ( hmag && ((double)n_bits_gz(gvect[j]) > hmag) ) {
1.245 noro 6154: v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
1.215 noro 6155: #if 1
1.245 noro 6156: for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
1.215 noro 6157: #endif
1.245 noro 6158: hmag = (double)n_bits_gz(gvect[j])*nd_scale;
1.215 noro 6159: }
6160: }
6161: }
6162: for ( j = 0; j < col && !gvect[j]; j++ );
1.245 noro 6163: if ( j < col ) {
6164: v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
6165: for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
6166: }
1.215 noro 6167: if ( DP_Print ) {
6168: fprintf(asir_out,"-"); fflush(asir_out);
6169: }
6170: return maxrs;
6171: }
6172:
1.76 noro 6173: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65 noro 6174: {
1.157 noro 6175: int i,j,k,len,pos,prev;
6176: UINT c,c1,c2,c3,up,lo,dmy;
6177: IndArray ivect;
6178: unsigned char *ivc;
6179: unsigned short *ivs;
6180: unsigned int *ivi;
6181: NDV redv;
6182: NMV mr;
6183: NODE rp;
6184: int maxrs;
6185:
6186: maxrs = 0;
6187: for ( i = 0; i < nred; i++ ) {
6188: ivect = imat[i];
6189: k = ivect->head; svect[k] %= m;
6190: if ( c = svect[k] ) {
6191: maxrs = MAX(maxrs,rp0[i]->sugar);
6192: c = m-c; redv = nd_ps[rp0[i]->index];
6193: len = LEN(redv); mr = BDY(redv);
6194: svect[k] = 0; prev = k;
6195: switch ( ivect->width ) {
6196: case 1:
6197: ivc = ivect->index.c;
6198: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215 noro 6199: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.245 noro 6200: if ( c1 ) {
6201: c2 = svect[pos];
6202: DMA(c1,c,c2,up,lo);
6203: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6204: } else svect[pos] = lo;
6205: }
1.157 noro 6206: }
6207: break;
6208: case 2:
6209: ivs = ivect->index.s;
6210: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215 noro 6211: pos = prev+ivs[j]; c1 = CM(mr);
1.157 noro 6212: prev = pos;
1.245 noro 6213: if ( c1 ) {
6214: c2 = svect[pos];
6215: DMA(c1,c,c2,up,lo);
6216: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6217: } else svect[pos] = lo;
6218: }
1.157 noro 6219: }
6220: break;
6221: case 4:
6222: ivi = ivect->index.i;
6223: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215 noro 6224: pos = prev+ivi[j]; c1 = CM(mr);
1.157 noro 6225: prev = pos;
1.245 noro 6226: if ( c1 ) {
6227: c2 = svect[pos];
6228: DMA(c1,c,c2,up,lo);
6229: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6230: } else svect[pos] = lo;
6231: }
1.157 noro 6232: }
6233: break;
6234: }
6235: }
6236: }
6237: for ( i = 0; i < col; i++ )
6238: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6239: return maxrs;
1.65 noro 6240: }
6241:
1.242 noro 6242: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240 noro 6243:
6244: int ndv_reduce_vect64(int m,U64 *svect,U64 *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.239 noro 6245: {
6246: int i,j,k,len,pos,prev;
1.240 noro 6247: U64 a,c,c1,c2;
1.239 noro 6248: IndArray ivect;
6249: unsigned char *ivc;
6250: unsigned short *ivs;
6251: unsigned int *ivi;
6252: NDV redv;
6253: NMV mr;
6254: NODE rp;
6255: int maxrs;
6256:
1.240 noro 6257: for ( i = 0; i < col; i++ ) cvect[i] = 0;
1.239 noro 6258: maxrs = 0;
6259: for ( i = 0; i < nred; i++ ) {
6260: ivect = imat[i];
1.240 noro 6261: k = ivect->head;
6262: a = svect[k]; c = cvect[k];
6263: MOD128(a,c,m);
6264: svect[k] = a; cvect[k] = 0;
1.239 noro 6265: if ( c = svect[k] ) {
6266: maxrs = MAX(maxrs,rp0[i]->sugar);
6267: c = m-c; redv = nd_ps[rp0[i]->index];
6268: len = LEN(redv); mr = BDY(redv);
6269: svect[k] = 0; prev = k;
6270: switch ( ivect->width ) {
6271: case 1:
6272: ivc = ivect->index.c;
6273: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6274: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.240 noro 6275: if ( c1 ) {
6276: c2 = svect[pos]+c1*c;
6277: if ( c2 < svect[pos] ) cvect[pos]++;
6278: svect[pos] = c2;
6279: }
1.239 noro 6280: }
6281: break;
6282: case 2:
6283: ivs = ivect->index.s;
6284: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6285: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.240 noro 6286: if ( c1 ) {
6287: c2 = svect[pos]+c1*c;
6288: if ( c2 < svect[pos] ) cvect[pos]++;
6289: svect[pos] = c2;
6290: }
1.239 noro 6291: }
6292: break;
6293: case 4:
6294: ivi = ivect->index.i;
6295: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6296: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.240 noro 6297: if ( c1 ) {
6298: c2 = svect[pos]+c1*c;
6299: if ( c2 < svect[pos] ) cvect[pos]++;
6300: svect[pos] = c2;
6301: }
1.239 noro 6302: }
6303: break;
6304: }
6305: }
6306: }
1.240 noro 6307: for ( i = 0; i < col; i++ ) {
6308: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
6309: }
1.239 noro 6310: return maxrs;
6311: }
6312: #endif
6313:
1.76 noro 6314: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72 noro 6315: {
1.157 noro 6316: int i,j,k,len,pos,prev;
6317: UINT c,c1,c2,c3,up,lo,dmy;
6318: IndArray ivect;
6319: unsigned char *ivc;
6320: unsigned short *ivs;
6321: unsigned int *ivi;
6322: NDV redv;
6323: NMV mr;
6324: NODE rp;
6325: int maxrs;
6326:
6327: maxrs = 0;
6328: for ( i = 0; i < nred; i++ ) {
6329: ivect = imat[i];
1.232 noro 6330: k = ivect->head;
1.157 noro 6331: if ( c = svect[k] ) {
6332: maxrs = MAX(maxrs,rp0[i]->sugar);
6333: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6334: len = LEN(redv); mr = BDY(redv);
6335: svect[k] = 0; prev = k;
6336: switch ( ivect->width ) {
6337: case 1:
6338: ivc = ivect->index.c;
6339: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6340: pos = prev+ivc[j]; prev = pos;
6341: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6342: }
6343: break;
6344: case 2:
6345: ivs = ivect->index.s;
6346: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6347: pos = prev+ivs[j]; prev = pos;
6348: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6349: }
6350: break;
6351: case 4:
6352: ivi = ivect->index.i;
6353: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6354: pos = prev+ivi[j]; prev = pos;
6355: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6356: }
6357: break;
6358: }
6359: }
6360: }
6361: return maxrs;
1.72 noro 6362: }
6363:
1.232 noro 6364: ND nd_add_lf(ND p1,ND p2)
6365: {
6366: int n,c,can;
6367: ND r;
6368: NM m1,m2,mr0,mr,s;
6369: GZ t;
6370:
6371: if ( !p1 ) return p2;
6372: else if ( !p2 ) return p1;
6373: else {
6374: can = 0;
6375: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6376: c = DL_COMPARE(DL(m1),DL(m2));
6377: switch ( c ) {
6378: case 0:
6379: addlf(CZ(m1),CZ(m2),&t);
6380: s = m1; m1 = NEXT(m1);
6381: if ( t ) {
6382: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6383: } else {
6384: can += 2; FREENM(s);
6385: }
6386: s = m2; m2 = NEXT(m2); FREENM(s);
6387: break;
6388: case 1:
6389: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6390: break;
6391: case -1:
6392: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6393: break;
6394: }
6395: }
6396: if ( !mr0 )
6397: if ( m1 ) mr0 = m1;
6398: else if ( m2 ) mr0 = m2;
6399: else return 0;
6400: else if ( m1 ) NEXT(mr) = m1;
6401: else if ( m2 ) NEXT(mr) = m2;
6402: else NEXT(mr) = 0;
6403: BDY(p1) = mr0;
6404: SG(p1) = MAX(SG(p1),SG(p2));
6405: LEN(p1) = LEN(p1)+LEN(p2)-can;
6406: FREEND(p2);
6407: return p1;
6408: }
6409: }
6410:
1.234 noro 6411: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.232 noro 6412: {
6413: int i,j,k,len,pos,prev;
6414: mpz_t c,mc,c1;
6415: IndArray ivect;
6416: unsigned char *ivc;
6417: unsigned short *ivs;
6418: unsigned int *ivi;
6419: NDV redv;
6420: NMV mr;
6421: NODE rp;
6422: int maxrs;
6423:
6424: maxrs = 0;
6425: lf_lazy = 1;
6426: for ( i = 0; i < nred; i++ ) {
6427: ivect = imat[i];
6428: k = ivect->head;
6429: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6430: if ( mpz_sgn(svect[k]) ) {
6431: maxrs = MAX(maxrs,rp0[i]->sugar);
6432: mpz_neg(svect[k],svect[k]);
1.234 noro 6433: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
1.232 noro 6434: len = LEN(redv); mr = BDY(redv);
6435: prev = k;
6436: switch ( ivect->width ) {
6437: case 1:
6438: ivc = ivect->index.c;
6439: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6440: pos = prev+ivc[j]; prev = pos;
6441: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6442: }
6443: break;
6444: case 2:
6445: ivs = ivect->index.s;
6446: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6447: pos = prev+ivs[j]; prev = pos;
6448: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6449: }
6450: break;
6451: case 4:
6452: ivi = ivect->index.i;
6453: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6454: pos = prev+ivi[j]; prev = pos;
6455: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6456: }
6457: break;
6458: }
6459: mpz_set_ui(svect[k],0);
6460: }
6461: }
6462: lf_lazy=0;
6463: for ( i = 0; i < col; i++ ) {
6464: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6465: }
6466: return maxrs;
6467: }
6468:
6469: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6470: {
6471: int i,j,k,l,rank,s;
6472: mpz_t a,a1,inv;
6473: mpz_t *t,*pivot,*pk;
6474: mpz_t **mat;
6475: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
1.234 noro 6476: int size,size1;
1.232 noro 6477:
6478: mpz_init(inv);
6479: mpz_init(a);
6480: mat = (mpz_t **)mat0;
1.234 noro 6481: size = 0;
1.232 noro 6482: for ( rank = 0, j = 0; j < col; j++ ) {
6483: for ( i = rank; i < row; i++ ) {
6484: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6485: }
6486: for ( i = rank; i < row; i++ )
6487: if ( mpz_sgn(mat[i][j]) )
6488: break;
6489: if ( i == row ) {
6490: colstat[j] = 0;
6491: continue;
6492: } else
6493: colstat[j] = 1;
6494: if ( i != rank ) {
6495: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6496: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6497: }
6498: pivot = mat[rank];
6499: s = sugar[rank];
6500: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6501: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6502: if ( mpz_sgn(*pk) ) {
6503: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6504: }
6505: for ( i = rank+1; i < row; i++ ) {
6506: t = mat[i];
6507: if ( mpz_sgn(t[j]) ) {
6508: sugar[i] = MAX(sugar[i],s);
6509: mpz_neg(a,t[j]);
6510: red_by_vect_lf(t+j,pivot+j,a,col-j);
6511: }
6512: }
6513: rank++;
6514: }
6515: for ( j = col-1, l = rank-1; j >= 0; j-- )
6516: if ( colstat[j] ) {
6517: pivot = mat[l];
6518: s = sugar[l];
6519: for ( k = j; k < col; k++ )
6520: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6521: for ( i = 0; i < l; i++ ) {
6522: t = mat[i];
6523: if ( mpz_sgn(t[j]) ) {
6524: sugar[i] = MAX(sugar[i],s);
6525: mpz_neg(a,t[j]);
6526: red_by_vect_lf(t+j,pivot+j,a,col-j);
6527: }
6528: }
6529: l--;
6530: }
6531: for ( j = 0, l = 0; l < rank; j++ )
6532: if ( colstat[j] ) {
6533: t = mat[l];
6534: for ( k = j; k < col; k++ ) {
6535: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6536: }
6537: l++;
6538: }
6539: return rank;
6540: }
6541:
6542:
1.65 noro 6543: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6544: {
1.157 noro 6545: int j,k,len;
6546: UINT *p;
6547: UINT c;
6548: NDV r;
6549: NMV mr0,mr;
6550:
6551: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6552: if ( !len ) return 0;
6553: else {
1.200 noro 6554: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103 noro 6555: #if 0
1.157 noro 6556: ndv_alloc += nmv_adv*len;
1.103 noro 6557: #endif
1.157 noro 6558: mr = mr0;
6559: p = s0vect;
6560: for ( j = k = 0; j < col; j++, p += nd_wpd )
6561: if ( !rhead[j] ) {
6562: if ( c = vect[k++] ) {
6563: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6564: }
6565: }
6566: MKNDV(nd_nvar,mr0,len,r);
6567: return r;
6568: }
1.65 noro 6569: }
6570:
1.242 noro 6571: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240 noro 6572: NDV vect64_to_ndv(U64 *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.239 noro 6573: {
6574: int j,k,len;
6575: UINT *p;
6576: UINT c;
6577: NDV r;
6578: NMV mr0,mr;
6579:
6580: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6581: if ( !len ) return 0;
6582: else {
6583: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6584: #if 0
6585: ndv_alloc += nmv_adv*len;
6586: #endif
6587: mr = mr0;
6588: p = s0vect;
6589: for ( j = k = 0; j < col; j++, p += nd_wpd )
6590: if ( !rhead[j] ) {
6591: if ( c = (UINT)vect[k++] ) {
6592: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6593: }
6594: }
6595: MKNDV(nd_nvar,mr0,len,r);
6596: return r;
6597: }
6598: }
6599: #endif
6600:
1.220 noro 6601: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6602: {
6603: int j,k,len;
6604: UINT *p;
6605: NDV r;
6606: NMV mr0,mr;
6607:
6608: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6609: if ( !len ) return 0;
6610: else {
6611: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6612: mr = mr0;
6613: p = s0vect;
6614: for ( j = 0; j < col; j++, p += nd_wpd )
1.245 noro 6615: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
1.220 noro 6616: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6617: }
6618: MKNDV(nd_nvar,mr0,len,r);
6619: return r;
6620: }
6621: }
6622:
1.129 noro 6623: /* for preprocessed vector */
6624:
1.113 noro 6625: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107 noro 6626: {
1.157 noro 6627: int j,k,len;
6628: UINT *p;
6629: Q c;
6630: NDV r;
6631: NMV mr0,mr;
6632:
6633: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6634: if ( !len ) return 0;
6635: else {
1.200 noro 6636: mr0 = (NMV)MALLOC(nmv_adv*len);
1.107 noro 6637: #if 0
1.157 noro 6638: ndv_alloc += nmv_adv*len;
1.107 noro 6639: #endif
1.157 noro 6640: mr = mr0;
6641: p = s0vect;
6642: for ( j = k = 0; j < col; j++, p += nd_wpd )
6643: if ( !rhead[j] ) {
6644: if ( c = vect[k++] ) {
6645: if ( DN(c) )
6646: error("afo");
6647: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6648: }
6649: }
6650: MKNDV(nd_nvar,mr0,len,r);
6651: return r;
6652: }
1.107 noro 6653: }
6654:
1.215 noro 6655: NDV vect_to_ndv_gz(GZ *vect,int spcol,int col,int *rhead,UINT *s0vect)
6656: {
6657: int j,k,len;
6658: UINT *p;
1.232 noro 6659: GZ c;
1.215 noro 6660: NDV r;
6661: NMV mr0,mr;
6662:
6663: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6664: if ( !len ) return 0;
6665: else {
6666: mr0 = (NMV)MALLOC(nmv_adv*len);
6667: #if 0
6668: ndv_alloc += nmv_adv*len;
6669: #endif
6670: mr = mr0;
6671: p = s0vect;
6672: for ( j = k = 0; j < col; j++, p += nd_wpd )
6673: if ( !rhead[j] ) {
6674: if ( c = vect[k++] ) {
6675: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6676: }
6677: }
6678: MKNDV(nd_nvar,mr0,len,r);
6679: return r;
6680: }
6681: }
6682:
1.232 noro 6683: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6684: {
6685: int j,k,len;
6686: UINT *p;
6687: mpz_t c;
6688: NDV r;
6689: NMV mr0,mr;
6690:
6691: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6692: if ( !len ) return 0;
6693: else {
6694: mr0 = (NMV)MALLOC(nmv_adv*len);
6695: #if 0
6696: ndv_alloc += nmv_adv*len;
6697: #endif
6698: mr = mr0;
6699: p = s0vect;
6700: for ( j = k = 0; j < col; j++, p += nd_wpd )
6701: if ( !rhead[j] ) {
6702: c[0] = vect[k++][0];
6703: if ( mpz_sgn(c) ) {
6704: ndl_copy(p,DL(mr)); MPZTOGZ(c,CZ(mr)); NMV_ADV(mr);
6705: }
6706: }
6707: MKNDV(nd_nvar,mr0,len,r);
6708: return r;
6709: }
6710: }
6711:
1.129 noro 6712: /* for plain vector */
6713:
6714: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
6715: {
1.157 noro 6716: int j,k,len;
6717: UINT *p;
6718: Q c;
6719: NDV r;
6720: NMV mr0,mr;
6721:
6722: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6723: if ( !len ) return 0;
6724: else {
1.200 noro 6725: mr0 = (NMV)MALLOC(nmv_adv*len);
1.129 noro 6726: #if 0
1.157 noro 6727: ndv_alloc += nmv_adv*len;
1.129 noro 6728: #endif
1.157 noro 6729: mr = mr0;
6730: p = s0vect;
6731: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6732: if ( c = vect[k] ) {
6733: if ( DN(c) )
6734: error("afo");
6735: ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
6736: }
6737: MKNDV(nd_nvar,mr0,len,r);
6738: return r;
6739: }
1.129 noro 6740: }
6741:
1.133 noro 6742: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65 noro 6743: {
1.157 noro 6744: ND_pairs t;
6745: NODE sp0,sp;
6746: int stat;
6747: ND spol;
6748:
6749: for ( t = l; t; t = NEXT(t) ) {
6750: stat = nd_sp(m,trace,t,&spol);
6751: if ( !stat ) return 0;
6752: if ( spol ) {
6753: add_pbucket_symbolic(bucket,spol);
6754: }
6755: }
6756: return 1;
1.65 noro 6757: }
6758:
1.133 noro 6759: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65 noro 6760: {
1.157 noro 6761: NODE rp0,rp;
6762: NM mul,head,s0,s;
6763: int index,col,i,sugar;
6764: RHist h;
6765: UINT *s0v,*p;
6766: NM_ind_pair pair;
6767: ND red;
6768: NDV *ps;
1.235 noro 6769: static int afo;
1.157 noro 6770:
6771: s0 = 0; rp0 = 0; col = 0;
1.245 noro 6772: if ( nd_demand )
6773: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6774: else
6775: ps = trace?nd_ps_trace:nd_ps;
1.157 noro 6776: while ( 1 ) {
6777: head = remove_head_pbucket_symbolic(bucket);
6778: if ( !head ) break;
6779: if ( !s0 ) s0 = head;
6780: else NEXT(s) = head;
6781: s = head;
6782: index = ndl_find_reducer(DL(head));
6783: if ( index >= 0 ) {
6784: h = nd_psh[index];
6785: NEWNM(mul);
6786: ndl_sub(DL(head),DL(h),DL(mul));
1.235 noro 6787: if ( ndl_check_bound2(index,DL(mul)) )
6788: return 0;
1.157 noro 6789: sugar = TD(DL(mul))+SG(ps[index]);
6790: MKNM_ind_pair(pair,mul,index,sugar);
6791: red = ndv_mul_nm_symbolic(mul,ps[index]);
6792: add_pbucket_symbolic(bucket,nd_remove_head(red));
6793: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6794: }
6795: col++;
6796: }
6797: if ( rp0 ) NEXT(rp) = 0;
6798: NEXT(s) = 0;
6799: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6800: for ( i = 0, p = s0v, s = s0; i < col;
6801: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6802: *s0vect = s0v;
6803: *r = rp0;
6804: return col;
1.65 noro 6805: }
6806:
1.244 noro 6807: void print_ndp(ND_pairs l)
6808: {
6809: ND_pairs t;
6810:
6811: for ( t = l; t; t = NEXT(t) )
6812: printf("[%d,%d] ",t->i1,t->i2);
6813: printf("\n");
6814: }
6815:
1.234 noro 6816: NODE nd_f4(int m,int checkonly,int **indp)
1.69 noro 6817: {
1.234 noro 6818: int i,nh,stat,index,f4red;
1.208 noro 6819: NODE r,g,tn0,tn,node;
1.244 noro 6820: ND_pairs d,l,t,ll0,ll,lh;
1.245 noro 6821: LIST l0,l1;
1.157 noro 6822: ND spol,red;
6823: NDV nf,redv;
6824: NM s0,s;
1.231 noro 6825: NODE rp0,srp0,nflist,nzlist,nzlist_t;
1.208 noro 6826: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
1.157 noro 6827: UINT c;
6828: UINT **spmat;
6829: UINT *s0vect,*svect,*p,*v;
6830: int *colstat;
6831: IndArray *imat;
6832: int *rhead;
6833: int spcol,sprow;
1.244 noro 6834: int sugar,sugarh;
1.157 noro 6835: PGeoBucket bucket;
6836: struct oEGT eg0,eg1,eg_f4;
1.208 noro 6837: Q i1,i2,sugarq;
1.103 noro 6838: #if 0
1.157 noro 6839: ndv_alloc = 0;
1.103 noro 6840: #endif
1.157 noro 6841: g = 0; d = 0;
6842: for ( i = 0; i < nd_psn; i++ ) {
1.244 noro 6843: d = update_pairs(d,g,i,0);
1.157 noro 6844: g = update_base(g,i);
6845: }
1.245 noro 6846: nzlist = 0;
1.231 noro 6847: nzlist_t = nd_nzlist;
1.241 noro 6848: f4red = 1;
6849: nd_last_nonzero = 0;
1.244 noro 6850: while ( d ) {
1.157 noro 6851: get_eg(&eg0);
1.244 noro 6852: l = nd_minsugarp(d,&d);
6853: sugar = nd_sugarweight?l->sugar2:SG(l);
6854: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6855: if ( nzlist_t ) {
1.231 noro 6856: node = BDY((LIST)BDY(nzlist_t));
1.244 noro 6857: sugarh = QTOS((Q)ARG0(node));
1.231 noro 6858: tn = BDY((LIST)ARG1(node));
6859: if ( !tn ) {
6860: nzlist_t = NEXT(nzlist_t);
6861: continue;
6862: }
6863: /* tn = [[i1,i2],...] */
1.244 noro 6864: lh = nd_ipairtospair(tn);
1.208 noro 6865: }
1.157 noro 6866: bucket = create_pbucket();
6867: stat = nd_sp_f4(m,0,l,bucket);
6868: if ( !stat ) {
1.244 noro 6869: for ( t = l; NEXT(t); t = NEXT(t) );
6870: NEXT(t) = d; d = l;
6871: d = nd_reconstruct(0,d);
1.157 noro 6872: continue;
6873: }
6874: if ( bucket->m < 0 ) continue;
6875: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6876: if ( !col ) {
6877: for ( t = l; NEXT(t); t = NEXT(t) );
6878: NEXT(t) = d; d = l;
6879: d = nd_reconstruct(0,d);
6880: continue;
6881: }
6882: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6883: if ( DP_Print )
1.241 noro 6884: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.157 noro 6885: sugar,eg_f4.exectime+eg_f4.gctime);
1.244 noro 6886: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
1.234 noro 6887: if ( checkonly && nflist ) return 0;
1.157 noro 6888: /* adding new bases */
1.241 noro 6889: if ( nflist ) nd_last_nonzero = f4red;
1.157 noro 6890: for ( r = nflist; r; r = NEXT(r) ) {
6891: nf = (NDV)BDY(r);
6892: ndv_removecont(m,nf);
6893: if ( !m && nd_nalg ) {
6894: ND nf1;
6895:
6896: nf1 = ndvtond(m,nf);
6897: nd_monic(0,&nf1);
6898: nd_removecont(m,nf1);
6899: nf = ndtondv(m,nf1);
6900: }
1.215 noro 6901: nh = ndv_newps(m,nf,0,1);
1.244 noro 6902: d = update_pairs(d,g,nh,0);
1.157 noro 6903: g = update_base(g,nh);
6904: }
1.234 noro 6905: if ( DP_Print ) {
1.241 noro 6906: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
1.234 noro 6907: }
1.208 noro 6908: if ( nd_gentrace ) {
1.245 noro 6909: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6910: NEXTNODE(tn0,tn);
1.208 noro 6911: STOQ(t->i1,i1); STOQ(t->i2,i2);
6912: node = mknode(2,i1,i2); MKLIST(l0,node);
1.245 noro 6913: BDY(tn) = l0;
6914: }
6915: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.208 noro 6916: STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
6917: MKNODE(node,l1,nzlist); nzlist = node;
6918: }
1.231 noro 6919: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
1.234 noro 6920: f4red++;
1.241 noro 6921: if ( nd_f4red && f4red > nd_f4red ) break;
1.234 noro 6922: if ( nd_rank0 && !nflist ) break;
1.208 noro 6923: }
6924: if ( nd_gentrace ) {
1.245 noro 6925: MKLIST(l0,reverse_node(nzlist));
1.208 noro 6926: MKNODE(nd_alltracelist,l0,0);
1.157 noro 6927: }
1.103 noro 6928: #if 0
1.157 noro 6929: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103 noro 6930: #endif
1.245 noro 6931: conv_ilist(nd_demand,0,g,indp);
1.157 noro 6932: return g;
1.69 noro 6933: }
1.74 noro 6934:
1.167 noro 6935: NODE nd_f4_trace(int m,int **indp)
1.133 noro 6936: {
1.157 noro 6937: int i,nh,stat,index;
6938: NODE r,g;
6939: ND_pairs d,l,l0,t;
6940: ND spol,red;
6941: NDV nf,redv,nfqv,nfv;
6942: NM s0,s;
6943: NODE rp0,srp0,nflist;
6944: int nsp,nred,col,rank,len,k,j,a;
6945: UINT c;
6946: UINT **spmat;
6947: UINT *s0vect,*svect,*p,*v;
6948: int *colstat;
6949: IndArray *imat;
6950: int *rhead;
6951: int spcol,sprow;
6952: int sugar;
6953: PGeoBucket bucket;
6954: struct oEGT eg0,eg1,eg_f4;
6955:
6956: g = 0; d = 0;
6957: for ( i = 0; i < nd_psn; i++ ) {
1.168 noro 6958: d = update_pairs(d,g,i,0);
1.157 noro 6959: g = update_base(g,i);
6960: }
6961: while ( d ) {
6962: get_eg(&eg0);
6963: l = nd_minsugarp(d,&d);
6964: sugar = SG(l);
1.228 noro 6965: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.157 noro 6966: bucket = create_pbucket();
6967: stat = nd_sp_f4(m,0,l,bucket);
6968: if ( !stat ) {
6969: for ( t = l; NEXT(t); t = NEXT(t) );
6970: NEXT(t) = d; d = l;
6971: d = nd_reconstruct(1,d);
6972: continue;
6973: }
6974: if ( bucket->m < 0 ) continue;
6975: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6976: if ( !col ) {
6977: for ( t = l; NEXT(t); t = NEXT(t) );
6978: NEXT(t) = d; d = l;
6979: d = nd_reconstruct(1,d);
6980: continue;
6981: }
6982: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6983: if ( DP_Print )
1.241 noro 6984: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.157 noro 6985: sugar,eg_f4.exectime+eg_f4.gctime);
6986: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6987: if ( !l0 ) continue;
6988: l = l0;
6989:
6990: /* over Q */
6991: bucket = create_pbucket();
6992: stat = nd_sp_f4(0,1,l,bucket);
6993: if ( !stat ) {
6994: for ( t = l; NEXT(t); t = NEXT(t) );
6995: NEXT(t) = d; d = l;
6996: d = nd_reconstruct(1,d);
6997: continue;
6998: }
6999: if ( bucket->m < 0 ) continue;
7000: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7001: if ( !col ) {
7002: for ( t = l; NEXT(t); t = NEXT(t) );
7003: NEXT(t) = d; d = l;
7004: d = nd_reconstruct(1,d);
7005: continue;
7006: }
7007: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7008: /* adding new bases */
7009: for ( r = nflist; r; r = NEXT(r) ) {
7010: nfqv = (NDV)BDY(r);
7011: ndv_removecont(0,nfqv);
7012: if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
7013: if ( nd_nalg ) {
7014: ND nf1;
7015:
7016: nf1 = ndvtond(m,nfqv);
7017: nd_monic(0,&nf1);
7018: nd_removecont(0,nf1);
7019: nfqv = ndtondv(0,nf1); nd_free(nf1);
7020: }
7021: nfv = ndv_dup(0,nfqv);
7022: ndv_mod(m,nfv);
7023: ndv_removecont(m,nfv);
1.215 noro 7024: nh = ndv_newps(0,nfv,nfqv,1);
1.168 noro 7025: d = update_pairs(d,g,nh,0);
1.157 noro 7026: g = update_base(g,nh);
7027: }
7028: }
1.133 noro 7029: #if 0
1.157 noro 7030: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133 noro 7031: #endif
1.245 noro 7032: conv_ilist(nd_demand,1,g,indp);
1.157 noro 7033: return g;
1.133 noro 7034: }
7035:
1.220 noro 7036: int rref(matrix mat,int *sugar)
7037: {
7038: int row,col,i,j,k,l,s,wcol,wj;
7039: unsigned long bj;
7040: unsigned long **a;
7041: unsigned long *ai,*ak,*as,*t;
7042: int *pivot;
7043:
7044: row = mat->row;
7045: col = mat->col;
7046: a = mat->a;
7047: wcol = (col+BLEN-1)/BLEN;
7048: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7049: i = 0;
7050: for ( j = 0; j < col; j++ ) {
1.245 noro 7051: wj = j/BLEN; bj = 1L<<(j%BLEN);
1.220 noro 7052: for ( k = i; k < row; k++ )
1.245 noro 7053: if ( a[k][wj] & bj ) break;
1.220 noro 7054: if ( k == row ) continue;
1.245 noro 7055: pivot[i] = j;
1.220 noro 7056: if ( k != i ) {
1.245 noro 7057: t = a[i]; a[i] = a[k]; a[k] = t;
7058: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7059: }
7060: ai = a[i];
1.220 noro 7061: for ( k = i+1; k < row; k++ ) {
1.245 noro 7062: ak = a[k];
7063: if ( ak[wj] & bj ) {
7064: for ( l = wj; l < wcol; l++ )
7065: ak[l] ^= ai[l];
7066: sugar[k] = MAX(sugar[k],sugar[i]);
7067: }
7068: }
7069: i++;
1.220 noro 7070: }
7071: for ( k = i-1; k >= 0; k-- ) {
7072: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
1.245 noro 7073: ak = a[k];
1.220 noro 7074: for ( s = 0; s < k; s++ ) {
1.245 noro 7075: as = a[s];
1.220 noro 7076: if ( as[wj] & bj ) {
7077: for ( l = wj; l < wcol; l++ )
1.245 noro 7078: as[l] ^= ak[l];
7079: sugar[s] = MAX(sugar[s],sugar[k]);
7080: }
7081: }
1.220 noro 7082: }
7083: return i;
7084: }
7085:
7086: void print_matrix(matrix mat)
7087: {
7088: int row,col,i,j;
7089: unsigned long *ai;
7090:
7091: row = mat->row;
7092: col = mat->col;
7093: printf("%d x %d\n",row,col);
7094: for ( i = 0; i < row; i++ ) {
1.245 noro 7095: ai = mat->a[i];
1.220 noro 7096: for ( j = 0; j < col; j++ ) {
1.245 noro 7097: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
7098: else putchar('0');
7099: }
7100: putchar('\n');
1.220 noro 7101: }
7102: }
7103:
7104: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
7105:
7106: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
7107: {
7108: int row,col,wcol,wj,i,j;
7109: unsigned long bj;
7110: unsigned long *ai;
7111: unsigned long **a;
7112: int len;
7113: int *pos;
7114:
7115: row = mat->row;
7116: col = mat->col;
7117: wcol = (col+BLEN-1)/BLEN;
1.235 noro 7118: pos = (int *)MALLOC(wcol*sizeof(int));
1.220 noro 7119: bzero(pos,wcol*sizeof(int));
7120: for ( i = j = 0; i < wcol; i++ )
7121: if ( v[i] ) pos[j++] = i;;
7122: len = j;
7123: wj = rhead/BLEN;
7124: bj = 1L<<rhead%BLEN;
7125: a = mat->a;
7126: for ( i = 0; i < row; i++ ) {
1.245 noro 7127: ai = a[i];
1.220 noro 7128: if ( ai[wj]&bj ) {
1.245 noro 7129: for ( j = 0; j < len; j++ )
7130: ai[pos[j]] ^= v[pos[j]];
7131: sugar[i] = MAX(sugar[i],rsugar);
7132: }
1.220 noro 7133: }
7134: }
7135:
7136: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7137: {
7138: int nsp,nred,i,i0,k,rank,row;
7139: NODE r0,rp;
7140: ND_pairs sp;
1.245 noro 7141: ND spol;
7142: NM_ind_pair rt;
1.220 noro 7143: int *s0hash;
1.245 noro 7144: UINT *s;
7145: int *pivot,*sugar,*head;
7146: matrix mat;
1.220 noro 7147: NM m;
7148: NODE r;
1.245 noro 7149: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7150: int rhead,rsugar,size;
1.220 noro 7151: unsigned long *v;
7152:
7153: get_eg(&eg0);
7154: init_eg(&eg_search);
7155: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7156: nred = length(rp0);
7157: mat = alloc_matrix(nsp,col);
1.235 noro 7158: s0hash = (int *)MALLOC(col*sizeof(int));
1.220 noro 7159: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7160: s0hash[i] = ndl_hash_value(s);
7161:
1.245 noro 7162: sugar = (int *)MALLOC(nsp*sizeof(int));
7163: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7164: nd_sp(2,0,sp,&spol);
7165: if ( spol ) {
7166: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7167: sugar[i] = SG(spol);
7168: i++;
7169: }
7170: }
7171: mat->row = i;
1.227 noro 7172: if ( DP_Print ) {
7173: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7174: }
1.245 noro 7175: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7176: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
1.220 noro 7177: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
1.245 noro 7178: rt = (NM_ind_pair)BDY(rp);
7179: bzero(v,size);
1.220 noro 7180: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
1.245 noro 7181: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7182: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7183: }
1.220 noro 7184:
7185: get_eg(&eg1);
7186: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
1.245 noro 7187: rank = rref(mat,sugar);
1.220 noro 7188:
7189: for ( i = 0, r0 = 0; i < rank; i++ ) {
7190: NEXTNODE(r0,r);
1.245 noro 7191: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
1.220 noro 7192: SG((NDV)BDY(r)) = sugar[i];
7193: }
7194: if ( r0 ) NEXT(r) = 0;
7195: get_eg(&eg2);
7196: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7197: if ( DP_Print ) {
1.241 noro 7198: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.245 noro 7199: eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
1.220 noro 7200: fflush(asir_out);
1.245 noro 7201: }
1.220 noro 7202: return r0;
7203: }
7204:
7205:
1.133 noro 7206: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63 noro 7207: {
1.157 noro 7208: IndArray *imat;
7209: int nsp,nred,i;
7210: int *rhead;
7211: NODE r0,rp;
7212: ND_pairs sp;
7213: NM_ind_pair *rvect;
1.210 noro 7214: UINT *s;
7215: int *s0hash;
7216:
1.220 noro 7217: if ( m == 2 && nd_rref2 )
1.245 noro 7218: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
1.220 noro 7219:
1.198 noro 7220: init_eg(&eg_search);
1.157 noro 7221: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7222: nred = length(rp0);
1.235 noro 7223: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7224: rhead = (int *)MALLOC(col*sizeof(int));
1.157 noro 7225: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7226:
7227: /* construction of index arrays */
1.227 noro 7228: if ( DP_Print ) {
1.245 noro 7229: fprintf(asir_out,"%dx%d,",nsp+nred,col);
1.227 noro 7230: }
1.235 noro 7231: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
7232: s0hash = (int *)MALLOC(col*sizeof(int));
1.210 noro 7233: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7234: s0hash[i] = ndl_hash_value(s);
1.157 noro 7235: for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
7236: rvect[i] = (NM_ind_pair)BDY(rp);
1.235 noro 7237: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,s0hash,rvect[i]);
1.157 noro 7238: rhead[imat[i]->head] = 1;
7239: }
1.240 noro 7240: if ( m > 0 )
1.242 noro 7241: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240 noro 7242: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7243: #else
7244: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.239 noro 7245: #endif
7246: else if ( m == -1 )
7247: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.232 noro 7248: else if ( m == -2 )
1.234 noro 7249: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.157 noro 7250: else
1.237 noro 7251: #if defined(VISUAL)
7252: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7253: #else
1.215 noro 7254: r0 = nd_f4_red_gz_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.237 noro 7255: #endif
1.241 noro 7256: #if 0
1.227 noro 7257: if ( DP_Print ) print_eg("search",&eg_search);
1.241 noro 7258: #endif
1.157 noro 7259: return r0;
1.106 noro 7260: }
1.74 noro 7261:
1.239 noro 7262: /* for Fp, 2<=p<2^16 */
7263:
1.106 noro 7264: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133 noro 7265: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106 noro 7266: {
1.157 noro 7267: int spcol,sprow,a;
7268: int i,j,k,l,rank;
7269: NODE r0,r;
7270: ND_pairs sp;
7271: ND spol;
7272: int **spmat;
7273: UINT *svect,*v;
7274: int *colstat;
7275: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7276: int maxrs;
7277: int *spsugar;
7278: ND_pairs *spactive;
7279:
7280: spcol = col-nred;
7281: get_eg(&eg0);
7282: /* elimination (1st step) */
1.235 noro 7283: spmat = (int **)MALLOC(nsp*sizeof(UINT *));
7284: svect = (UINT *)MALLOC(col*sizeof(UINT));
7285: spsugar = (int *)MALLOC(nsp*sizeof(int));
7286: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
1.157 noro 7287: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7288: nd_sp(m,0,sp,&spol);
7289: if ( !spol ) continue;
7290: nd_to_vect(m,s0vect,col,spol,svect);
7291: if ( m == -1 )
7292: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7293: else
7294: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7295: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7296: if ( i < col ) {
7297: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7298: for ( j = k = 0; j < col; j++ )
7299: if ( !rhead[j] ) v[k++] = svect[j];
7300: spsugar[sprow] = MAX(maxrs,SG(spol));
7301: if ( nz )
7302: spactive[sprow] = sp;
7303: sprow++;
7304: }
7305: nd_free(spol);
7306: }
7307: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7308: if ( DP_Print ) {
1.241 noro 7309: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.157 noro 7310: fflush(asir_out);
7311: }
7312: /* free index arrays */
1.200 noro 7313: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157 noro 7314:
7315: /* elimination (2nd step) */
1.235 noro 7316: colstat = (int *)MALLOC(spcol*sizeof(int));
1.157 noro 7317: if ( m == -1 )
7318: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7319: else
7320: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7321: r0 = 0;
7322: for ( i = 0; i < rank; i++ ) {
7323: NEXTNODE(r0,r); BDY(r) =
7324: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7325: SG((NDV)BDY(r)) = spsugar[i];
1.200 noro 7326: GCFREE(spmat[i]);
1.157 noro 7327: }
7328: if ( r0 ) NEXT(r) = 0;
7329:
1.200 noro 7330: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157 noro 7331: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7332: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7333: if ( DP_Print ) {
1.241 noro 7334: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7335: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.157 noro 7336: nsp,nred,sprow,spcol,rank);
1.241 noro 7337: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.157 noro 7338: }
7339: if ( nz ) {
7340: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7341: if ( rank > 0 ) {
7342: NEXT(spactive[rank-1]) = 0;
7343: *nz = spactive[0];
7344: } else
7345: *nz = 0;
7346: }
7347: return r0;
1.74 noro 7348: }
7349:
1.242 noro 7350: #if defined(__GNUC__) && SIZEOF_LONG==8
1.239 noro 7351: /* for Fp, 2^15=<p<2^29 */
7352:
1.240 noro 7353: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.239 noro 7354: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7355: {
7356: int spcol,sprow,a;
7357: int i,j,k,l,rank;
7358: NODE r0,r;
7359: ND_pairs sp;
7360: ND spol;
1.240 noro 7361: U64 **spmat;
7362: U64 *svect,*cvect;
7363: U64 *v;
1.239 noro 7364: int *colstat;
7365: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7366: int maxrs;
7367: int *spsugar;
7368: ND_pairs *spactive;
7369:
7370: spcol = col-nred;
7371: get_eg(&eg0);
7372: /* elimination (1st step) */
1.240 noro 7373: spmat = (U64 **)MALLOC(nsp*sizeof(U64 *));
7374: svect = (U64 *)MALLOC(col*sizeof(U64));
7375: cvect = (U64 *)MALLOC(col*sizeof(U64));
1.239 noro 7376: spsugar = (int *)MALLOC(nsp*sizeof(int));
7377: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7378: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7379: nd_sp(m,0,sp,&spol);
7380: if ( !spol ) continue;
1.240 noro 7381: nd_to_vect64(m,s0vect,col,spol,svect);
7382: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
1.239 noro 7383: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7384: if ( i < col ) {
1.240 noro 7385: spmat[sprow] = v = (U64 *)MALLOC_ATOMIC(spcol*sizeof(U64));
1.239 noro 7386: for ( j = k = 0; j < col; j++ )
7387: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
7388: spsugar[sprow] = MAX(maxrs,SG(spol));
7389: if ( nz )
7390: spactive[sprow] = sp;
7391: sprow++;
7392: }
7393: nd_free(spol);
7394: }
7395: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7396: if ( DP_Print ) {
1.241 noro 7397: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.239 noro 7398: fflush(asir_out);
7399: }
7400: /* free index arrays */
7401: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7402:
7403: /* elimination (2nd step) */
7404: colstat = (int *)MALLOC(spcol*sizeof(int));
1.240 noro 7405: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
1.239 noro 7406: r0 = 0;
7407: for ( i = 0; i < rank; i++ ) {
7408: NEXTNODE(r0,r); BDY(r) =
1.240 noro 7409: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.239 noro 7410: SG((NDV)BDY(r)) = spsugar[i];
7411: GCFREE(spmat[i]);
7412: }
7413: if ( r0 ) NEXT(r) = 0;
7414:
7415: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7416: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7417: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7418: if ( DP_Print ) {
1.241 noro 7419: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7420: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.239 noro 7421: nsp,nred,sprow,spcol,rank);
1.241 noro 7422: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.239 noro 7423: }
7424: if ( nz ) {
7425: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7426: if ( rank > 0 ) {
7427: NEXT(spactive[rank-1]) = 0;
7428: *nz = spactive[0];
7429: } else
7430: *nz = 0;
7431: }
7432: return r0;
7433: }
7434: #endif
7435:
7436: /* for small finite fields */
7437:
7438: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7439: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7440: {
7441: int spcol,sprow,a;
7442: int i,j,k,l,rank;
7443: NODE r0,r;
7444: ND_pairs sp;
7445: ND spol;
7446: int **spmat;
7447: UINT *svect,*v;
7448: int *colstat;
7449: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7450: int maxrs;
7451: int *spsugar;
7452: ND_pairs *spactive;
7453:
7454: spcol = col-nred;
7455: get_eg(&eg0);
7456: /* elimination (1st step) */
7457: spmat = (int **)MALLOC(nsp*sizeof(UINT *));
7458: svect = (UINT *)MALLOC(col*sizeof(UINT));
7459: spsugar = (int *)MALLOC(nsp*sizeof(int));
7460: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7461: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7462: nd_sp(m,0,sp,&spol);
7463: if ( !spol ) continue;
7464: nd_to_vect(m,s0vect,col,spol,svect);
7465: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7466: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7467: if ( i < col ) {
7468: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7469: for ( j = k = 0; j < col; j++ )
7470: if ( !rhead[j] ) v[k++] = svect[j];
7471: spsugar[sprow] = MAX(maxrs,SG(spol));
7472: if ( nz )
7473: spactive[sprow] = sp;
7474: sprow++;
7475: }
7476: nd_free(spol);
7477: }
7478: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7479: if ( DP_Print ) {
1.241 noro 7480: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.239 noro 7481: fflush(asir_out);
7482: }
7483: /* free index arrays */
7484: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7485:
7486: /* elimination (2nd step) */
7487: colstat = (int *)MALLOC(spcol*sizeof(int));
7488: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7489: r0 = 0;
7490: for ( i = 0; i < rank; i++ ) {
7491: NEXTNODE(r0,r); BDY(r) =
7492: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7493: SG((NDV)BDY(r)) = spsugar[i];
7494: GCFREE(spmat[i]);
7495: }
7496: if ( r0 ) NEXT(r) = 0;
7497:
7498: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7499: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7500: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7501: if ( DP_Print ) {
1.241 noro 7502: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7503: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.239 noro 7504: nsp,nred,sprow,spcol,rank);
1.241 noro 7505: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.239 noro 7506: }
7507: if ( nz ) {
7508: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7509: if ( rank > 0 ) {
7510: NEXT(spactive[rank-1]) = 0;
7511: *nz = spactive[0];
7512: } else
7513: *nz = 0;
7514: }
7515: return r0;
7516: }
7517:
1.234 noro 7518: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232 noro 7519: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7520: {
7521: int spcol,sprow,a;
7522: int i,j,k,l,rank;
7523: NODE r0,r;
7524: ND_pairs sp;
7525: ND spol;
7526: mpz_t **spmat;
7527: mpz_t *svect,*v;
7528: int *colstat;
7529: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7530: int maxrs;
7531: int *spsugar;
7532: pointer *w;
7533:
7534: spcol = col-nred;
7535: get_eg(&eg0);
7536: /* elimination (1st step) */
1.235 noro 7537: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7538: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7539: spsugar = (int *)MALLOC(nsp*sizeof(int));
1.232 noro 7540: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
1.234 noro 7541: nd_sp(m,trace,sp,&spol);
1.232 noro 7542: if ( !spol ) continue;
7543: nd_to_vect_lf(s0vect,col,spol,svect);
1.234 noro 7544: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
1.232 noro 7545: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7546: if ( i < col ) {
7547: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7548: for ( j = k = 0; j < col; j++ )
7549: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7550: spsugar[sprow] = MAX(maxrs,SG(spol));
7551: sprow++;
7552: }
7553: /* nd_free(spol); */
7554: }
7555: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7556: if ( DP_Print ) {
1.241 noro 7557: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.232 noro 7558: fflush(asir_out);
7559: }
7560: /* free index arrays */
7561: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7562:
7563: /* elimination (2nd step) */
1.235 noro 7564: colstat = (int *)MALLOC(spcol*sizeof(int));
1.232 noro 7565: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
1.235 noro 7566: w = (pointer *)MALLOC(rank*sizeof(pointer));
1.232 noro 7567: for ( i = 0; i < rank; i++ ) {
7568: #if 0
7569: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7570: SG((NDV)w[rank-i-1]) = spsugar[i];
7571: #else
7572: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7573: SG((NDV)w[i]) = spsugar[i];
7574: #endif
7575: /* GCFREE(spmat[i]); */
1.245 noro 7576:
1.232 noro 7577: }
7578: #if 0
7579: qsort(w,rank,sizeof(NDV),
7580: (int (*)(const void *,const void *))ndv_compare);
7581: #endif
7582: r0 = 0;
7583: for ( i = 0; i < rank; i++ ) {
7584: NEXTNODE(r0,r); BDY(r) = w[i];
7585: }
7586: if ( r0 ) NEXT(r) = 0;
7587:
7588: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7589: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7590: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7591: if ( DP_Print ) {
1.241 noro 7592: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7593: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.232 noro 7594: nsp,nred,sprow,spcol,rank);
1.241 noro 7595: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.232 noro 7596: }
7597: return r0;
7598: }
7599:
1.133 noro 7600: #if 1
7601: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107 noro 7602: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7603: {
1.157 noro 7604: int spcol,sprow,a;
7605: int i,j,k,l,rank;
7606: NODE r0,r;
7607: ND_pairs sp;
7608: ND spol;
7609: Q **spmat;
7610: Q *svect,*v;
7611: int *colstat;
7612: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7613: int maxrs;
7614: int *spsugar;
7615: pointer *w;
7616:
7617: spcol = col-nred;
7618: get_eg(&eg0);
7619: /* elimination (1st step) */
1.235 noro 7620: spmat = (Q **)MALLOC(nsp*sizeof(Q *));
7621: svect = (Q *)MALLOC(col*sizeof(Q));
7622: spsugar = (int *)MALLOC(nsp*sizeof(int));
1.157 noro 7623: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7624: nd_sp(0,trace,sp,&spol);
7625: if ( !spol ) continue;
7626: nd_to_vect_q(s0vect,col,spol,svect);
7627: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7628: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7629: if ( i < col ) {
7630: spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
7631: for ( j = k = 0; j < col; j++ )
7632: if ( !rhead[j] ) v[k++] = svect[j];
7633: spsugar[sprow] = MAX(maxrs,SG(spol));
7634: sprow++;
7635: }
7636: /* nd_free(spol); */
7637: }
7638: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7639: if ( DP_Print ) {
1.241 noro 7640: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.157 noro 7641: fflush(asir_out);
7642: }
7643: /* free index arrays */
1.200 noro 7644: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157 noro 7645:
7646: /* elimination (2nd step) */
1.235 noro 7647: colstat = (int *)MALLOC(spcol*sizeof(int));
1.157 noro 7648: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.235 noro 7649: w = (pointer *)MALLOC(rank*sizeof(pointer));
1.157 noro 7650: for ( i = 0; i < rank; i++ ) {
1.213 noro 7651: #if 0
1.157 noro 7652: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7653: SG((NDV)w[rank-i-1]) = spsugar[i];
1.213 noro 7654: #else
7655: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7656: SG((NDV)w[i]) = spsugar[i];
7657: #endif
1.200 noro 7658: /* GCFREE(spmat[i]); */
1.157 noro 7659: }
1.138 noro 7660: #if 0
1.157 noro 7661: qsort(w,rank,sizeof(NDV),
7662: (int (*)(const void *,const void *))ndv_compare);
1.137 noro 7663: #endif
1.157 noro 7664: r0 = 0;
7665: for ( i = 0; i < rank; i++ ) {
7666: NEXTNODE(r0,r); BDY(r) = w[i];
7667: }
7668: if ( r0 ) NEXT(r) = 0;
7669:
1.200 noro 7670: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157 noro 7671: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7672: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7673: if ( DP_Print ) {
1.241 noro 7674: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7675: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.157 noro 7676: nsp,nred,sprow,spcol,rank);
1.241 noro 7677: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.157 noro 7678: }
7679: return r0;
1.107 noro 7680: }
1.215 noro 7681:
7682: NODE nd_f4_red_gz_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7683: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7684: {
7685: int spcol,sprow,a;
7686: int i,j,k,l,rank;
7687: NODE r0,r;
7688: ND_pairs sp;
7689: ND spol;
7690: GZ **spmat;
7691: GZ *svect,*v;
7692: int *colstat;
7693: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7694: int maxrs;
7695: int *spsugar;
7696: pointer *w;
7697:
7698: spcol = col-nred;
7699: get_eg(&eg0);
7700: /* elimination (1st step) */
1.235 noro 7701: spmat = (GZ **)MALLOC(nsp*sizeof(GZ *));
7702: svect = (GZ *)MALLOC(col*sizeof(GZ));
7703: spsugar = (int *)MALLOC(nsp*sizeof(int));
1.215 noro 7704: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7705: nd_sp(0,trace,sp,&spol);
7706: if ( !spol ) continue;
1.245 noro 7707: spol = ndtondgz(spol);
1.215 noro 7708: nd_to_vect_q(s0vect,col,spol,(Q *)svect);
7709: maxrs = ndv_reduce_vect_gz(svect,trace,col,imat,rvect,nred);
7710: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7711: if ( i < col ) {
7712: spmat[sprow] = v = (GZ *)MALLOC(spcol*sizeof(GZ));
7713: for ( j = k = 0; j < col; j++ )
7714: if ( !rhead[j] ) v[k++] = svect[j];
7715: spsugar[sprow] = MAX(maxrs,SG(spol));
7716: sprow++;
7717: }
7718: /* nd_free(spol); */
7719: }
7720: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7721: if ( DP_Print ) {
1.241 noro 7722: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.215 noro 7723: fflush(asir_out);
7724: }
7725: /* free index arrays */
7726: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7727:
7728: /* elimination (2nd step) */
1.235 noro 7729: colstat = (int *)MALLOC(spcol*sizeof(int));
1.215 noro 7730: rank = nd_gauss_elim_gz(spmat,spsugar,sprow,spcol,colstat);
1.235 noro 7731: w = (pointer *)MALLOC(rank*sizeof(pointer));
1.215 noro 7732: for ( i = 0; i < rank; i++ ) {
7733: #if 0
7734: w[rank-i-1] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.245 noro 7735: w[rank-i-1] = ndvgztondv(w[rank-i-1]);
1.215 noro 7736: SG((NDV)w[rank-i-1]) = spsugar[i];
7737: #else
1.232 noro 7738: w[i] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.245 noro 7739: w[i] = ndvgztondv(w[i]);
1.215 noro 7740: SG((NDV)w[i]) = spsugar[i];
7741: #endif
7742: /* GCFREE(spmat[i]); */
1.245 noro 7743:
1.215 noro 7744: }
7745: #if 0
7746: qsort(w,rank,sizeof(NDV),
7747: (int (*)(const void *,const void *))ndv_compare);
7748: #endif
7749: r0 = 0;
7750: for ( i = 0; i < rank; i++ ) {
7751: NEXTNODE(r0,r); BDY(r) = w[i];
7752: }
7753: if ( r0 ) NEXT(r) = 0;
7754:
7755: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7756: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7757: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7758: if ( DP_Print ) {
1.241 noro 7759: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
7760: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.215 noro 7761: nsp,nred,sprow,spcol,rank);
1.241 noro 7762: fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.215 noro 7763: }
7764: return r0;
7765: }
1.129 noro 7766: #else
7767: void printm(Q **mat,int row,int col)
7768: {
1.157 noro 7769: int i,j;
7770: printf("[");
7771: for ( i = 0; i < row; i++ ) {
7772: for ( j = 0; j < col; j++ ) {
7773: printexpr(CO,mat[i][j]); printf(" ");
7774: }
7775: printf("]\n");
7776: }
1.129 noro 7777: }
7778:
7779: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
7780: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7781: {
1.157 noro 7782: int row,a;
7783: int i,j,rank;
7784: NODE r0,r;
7785: ND_pairs sp;
7786: ND spol;
7787: Q **mat;
7788: int *colstat;
7789: int *sugar;
7790:
7791: row = nsp+nred;
7792: /* make the matrix */
1.235 noro 7793: mat = (Q **)MALLOC(row*sizeof(Q *));
7794: sugar = (int *)MALLOC(row*sizeof(int));
1.157 noro 7795: for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7796: nd_sp(0,0,sp,&spol);
7797: if ( !spol ) continue;
7798: mat[row] = (Q *)MALLOC(col*sizeof(Q));
7799: nd_to_vect_q(s0vect,col,spol,mat[row]);
7800: sugar[row] = SG(spol);
7801: row++;
7802: }
7803: for ( i = 0; i < nred; i++, row++ ) {
7804: mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
7805: sugar[row] = rvect[i]->sugar;
7806: }
7807: /* elimination */
1.235 noro 7808: colstat = (int *)MALLOC(col*sizeof(int));
1.157 noro 7809: rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
7810: r0 = 0;
7811: for ( i = 0; i < rank; i++ ) {
7812: for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
7813: if ( j == col ) error("nd_f4_red_q_main : cannot happen");
7814: if ( rhead[j] ) continue;
7815: NEXTNODE(r0,r); BDY(r) =
7816: (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
7817: SG((NDV)BDY(r)) = sugar[i];
7818: }
7819: if ( r0 ) NEXT(r) = 0;
7820: printf("\n");
7821: return r0;
1.129 noro 7822: }
7823: #endif
1.107 noro 7824:
1.74 noro 7825: FILE *nd_write,*nd_read;
7826:
7827: void nd_send_int(int a) {
1.157 noro 7828: write_int(nd_write,&a);
1.74 noro 7829: }
7830:
7831: void nd_send_intarray(int *p,int len) {
1.157 noro 7832: write_intarray(nd_write,p,len);
1.74 noro 7833: }
7834:
7835: int nd_recv_int() {
1.157 noro 7836: int a;
1.74 noro 7837:
1.157 noro 7838: read_int(nd_read,&a);
7839: return a;
1.74 noro 7840: }
7841:
7842: void nd_recv_intarray(int *p,int len) {
1.157 noro 7843: read_intarray(nd_read,p,len);
1.74 noro 7844: }
7845:
7846: void nd_send_ndv(NDV p) {
1.157 noro 7847: int len,i;
7848: NMV m;
1.74 noro 7849:
1.157 noro 7850: if ( !p ) nd_send_int(0);
7851: else {
7852: len = LEN(p);
7853: nd_send_int(len);
7854: m = BDY(p);
7855: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7856: nd_send_int(CM(m));
7857: nd_send_intarray(DL(m),nd_wpd);
7858: }
7859: }
1.74 noro 7860: }
7861:
7862: void nd_send_nd(ND p) {
1.157 noro 7863: int len,i;
7864: NM m;
1.74 noro 7865:
1.157 noro 7866: if ( !p ) nd_send_int(0);
7867: else {
7868: len = LEN(p);
7869: nd_send_int(len);
7870: m = BDY(p);
7871: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7872: nd_send_int(CM(m));
7873: nd_send_intarray(DL(m),nd_wpd);
7874: }
7875: }
1.74 noro 7876: }
1.65 noro 7877:
1.74 noro 7878: NDV nd_recv_ndv()
7879: {
1.157 noro 7880: int len,i;
7881: NMV m,m0;
7882: NDV r;
7883:
7884: len = nd_recv_int();
7885: if ( !len ) return 0;
7886: else {
1.200 noro 7887: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103 noro 7888: #if 0
1.157 noro 7889: ndv_alloc += len*nmv_adv;
1.103 noro 7890: #endif
1.157 noro 7891: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7892: CM(m) = nd_recv_int();
7893: nd_recv_intarray(DL(m),nd_wpd);
7894: }
7895: MKNDV(nd_nvar,m0,len,r);
7896: return r;
7897: }
1.74 noro 7898: }
1.65 noro 7899:
1.113 noro 7900: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107 noro 7901: {
1.176 noro 7902: int i,j,t,c,rank,inv;
1.157 noro 7903: int *ci,*ri;
7904: Q dn;
7905: MAT m,nm;
7906:
7907: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7908: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7909: for ( i = 0; i < row; i++ )
7910: for ( j = 0; j < col; j++ )
7911: mat0[i][j] = 0;
7912: c = col-rank;
7913: for ( i = 0; i < rank; i++ ) {
7914: mat0[i][ri[i]] = dn;
7915: for ( j = 0; j < c; j++ )
7916: mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
7917: }
7918: return rank;
1.76 noro 7919: }
7920:
1.215 noro 7921: int nd_gauss_elim_gz(GZ **mat0,int *sugar,int row,int col,int *colstat)
7922: {
7923: int i,j,t,c,rank,inv;
7924: int *ci,*ri;
1.216 noro 7925: GZ dn;
1.215 noro 7926: MAT m,nm;
7927:
7928: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.216 noro 7929: rank = gz_generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.215 noro 7930: for ( i = 0; i < row; i++ )
7931: for ( j = 0; j < col; j++ )
7932: mat0[i][j] = 0;
7933: c = col-rank;
7934: for ( i = 0; i < rank; i++ ) {
1.216 noro 7935: mat0[i][ri[i]] = dn;
1.215 noro 7936: for ( j = 0; j < c; j++ )
7937: mat0[i][ci[j]] = (GZ)BDY(nm)[i][j];
7938: }
7939: return rank;
7940: }
7941:
1.133 noro 7942: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76 noro 7943: {
1.157 noro 7944: int i,j,k,l,inv,a,rank,s;
7945: unsigned int *t,*pivot,*pk;
7946: unsigned int **mat;
7947: ND_pairs pair;
7948:
7949: mat = (unsigned int **)mat0;
7950: for ( rank = 0, j = 0; j < col; j++ ) {
7951: for ( i = rank; i < row; i++ )
7952: mat[i][j] %= md;
7953: for ( i = rank; i < row; i++ )
7954: if ( mat[i][j] )
7955: break;
7956: if ( i == row ) {
7957: colstat[j] = 0;
7958: continue;
7959: } else
7960: colstat[j] = 1;
7961: if ( i != rank ) {
7962: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7963: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7964: if ( spactive ) {
7965: pair = spactive[i]; spactive[i] = spactive[rank];
7966: spactive[rank] = pair;
7967: }
7968: }
7969: pivot = mat[rank];
7970: s = sugar[rank];
7971: inv = invm(pivot[j],md);
7972: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7973: if ( *pk ) {
7974: if ( *pk >= (unsigned int)md )
7975: *pk %= md;
7976: DMAR(*pk,inv,0,md,*pk)
7977: }
7978: for ( i = rank+1; i < row; i++ ) {
7979: t = mat[i];
7980: if ( a = t[j] ) {
7981: sugar[i] = MAX(sugar[i],s);
7982: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7983: }
7984: }
7985: rank++;
7986: }
7987: for ( j = col-1, l = rank-1; j >= 0; j-- )
7988: if ( colstat[j] ) {
7989: pivot = mat[l];
7990: s = sugar[l];
7991: for ( i = 0; i < l; i++ ) {
7992: t = mat[i];
7993: t[j] %= md;
7994: if ( a = t[j] ) {
7995: sugar[i] = MAX(sugar[i],s);
7996: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7997: }
7998: }
7999: l--;
8000: }
8001: for ( j = 0, l = 0; l < rank; j++ )
8002: if ( colstat[j] ) {
8003: t = mat[l];
8004: for ( k = j; k < col; k++ )
8005: if ( t[k] >= (unsigned int)md )
8006: t[k] %= md;
8007: l++;
8008: }
8009: return rank;
1.76 noro 8010: }
8011:
1.242 noro 8012: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240 noro 8013:
8014: int nd_gauss_elim_mod64(U64 **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.239 noro 8015: {
1.240 noro 8016: int i,j,k,l,rank,s;
8017: U64 inv;
8018: U64 a;
8019: UINT c;
8020: U64 *t,*pivot,*pk;
8021: UINT *ck;
8022: UINT **cmat;
8023: UINT *ct;
8024: ND_pairs pair;
8025:
8026: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
8027: for ( i = 0; i < row; i++ ) {
8028: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
8029: bzero(cmat[i],col*sizeof(UINT));
8030: }
1.239 noro 8031:
1.240 noro 8032: for ( rank = 0, j = 0; j < col; j++ ) {
8033: for ( i = rank; i < row; i++ ) {
8034: a = mat[i][j]; c = cmat[i][j];
8035: MOD128(a,c,md);
8036: mat[i][j] = a; cmat[i][j] = 0;
8037: }
8038: for ( i = rank; i < row; i++ )
8039: if ( mat[i][j] )
8040: break;
8041: if ( i == row ) {
8042: colstat[j] = 0;
8043: continue;
8044: } else
8045: colstat[j] = 1;
8046: if ( i != rank ) {
8047: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8048: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
8049: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8050: if ( spactive ) {
8051: pair = spactive[i]; spactive[i] = spactive[rank];
8052: spactive[rank] = pair;
8053: }
8054: }
8055: /* column j is normalized */
8056: s = sugar[rank];
8057: inv = invm((UINT)mat[rank][j],md);
8058: /* normalize pivot row */
8059: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
8060: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
8061: }
8062: for ( i = rank+1; i < row; i++ ) {
8063: if ( a = mat[i][j] ) {
8064: sugar[i] = MAX(sugar[i],s);
8065: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
8066: }
1.239 noro 8067: }
1.240 noro 8068: rank++;
8069: }
8070: for ( j = col-1, l = rank-1; j >= 0; j-- )
8071: if ( colstat[j] ) {
8072: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
8073: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
8074: }
8075: s = sugar[l];
8076: for ( i = 0; i < l; i++ ) {
8077: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
8078: if ( a ) {
8079: sugar[i] = MAX(sugar[i],s);
8080: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
8081: }
8082: }
8083: l--;
8084: }
8085: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
8086: GCFREE(cmat);
8087: return rank;
1.239 noro 8088: }
8089: #endif
8090:
1.76 noro 8091: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
8092: {
1.157 noro 8093: int i,j,k,l,inv,a,rank,s;
8094: unsigned int *t,*pivot,*pk;
8095: unsigned int **mat;
8096:
8097: mat = (unsigned int **)mat0;
8098: for ( rank = 0, j = 0; j < col; j++ ) {
8099: for ( i = rank; i < row; i++ )
8100: if ( mat[i][j] )
8101: break;
8102: if ( i == row ) {
8103: colstat[j] = 0;
8104: continue;
8105: } else
8106: colstat[j] = 1;
8107: if ( i != rank ) {
8108: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
8109: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
8110: }
8111: pivot = mat[rank];
8112: s = sugar[rank];
8113: inv = _invsf(pivot[j]);
8114: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
8115: if ( *pk )
8116: *pk = _mulsf(*pk,inv);
8117: for ( i = rank+1; i < row; i++ ) {
8118: t = mat[i];
8119: if ( a = t[j] ) {
8120: sugar[i] = MAX(sugar[i],s);
8121: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8122: }
8123: }
8124: rank++;
8125: }
8126: for ( j = col-1, l = rank-1; j >= 0; j-- )
8127: if ( colstat[j] ) {
8128: pivot = mat[l];
8129: s = sugar[l];
8130: for ( i = 0; i < l; i++ ) {
8131: t = mat[i];
8132: if ( a = t[j] ) {
8133: sugar[i] = MAX(sugar[i],s);
8134: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
8135: }
8136: }
8137: l--;
8138: }
8139: return rank;
1.77 noro 8140: }
8141:
8142: int ndv_ishomo(NDV p)
8143: {
1.157 noro 8144: NMV m;
8145: int len,h;
1.77 noro 8146:
1.157 noro 8147: if ( !p ) return 1;
8148: len = LEN(p);
8149: m = BDY(p);
8150: h = TD(DL(m));
8151: NMV_ADV(m);
8152: for ( len--; len; len--, NMV_ADV(m) )
8153: if ( TD(DL(m)) != h ) return 0;
8154: return 1;
1.77 noro 8155: }
8156:
8157: void ndv_save(NDV p,int index)
8158: {
1.157 noro 8159: FILE *s;
8160: char name[BUFSIZ];
8161: short id;
8162: int nv,sugar,len,n,i,td,e,j;
8163: NMV m;
8164: unsigned int *dl;
8165: int mpos;
8166:
8167: sprintf(name,"%s/%d",Demand,index);
8168: s = fopen(name,"w");
8169: savevl(s,0);
8170: if ( !p ) {
8171: saveobj(s,0);
8172: return;
8173: }
8174: id = O_DP;
8175: nv = NV(p);
8176: sugar = SG(p);
8177: len = LEN(p);
8178: write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
8179: write_int(s,&len);
8180:
8181: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8182: saveobj(s,(Obj)CQ(m));
8183: dl = DL(m);
8184: td = TD(dl);
8185: write_int(s,&td);
8186: for ( j = 0; j < nv; j++ ) {
8187: e = GET_EXP(dl,j);
8188: write_int(s,&e);
8189: }
8190: if ( nd_module ) {
8191: mpos = MPOS(dl); write_int(s,&mpos);
8192: }
8193: }
8194: fclose(s);
1.77 noro 8195: }
8196:
1.206 noro 8197: void nd_save_mod(ND p,int index)
8198: {
8199: FILE *s;
8200: char name[BUFSIZ];
8201: int nv,sugar,len,c;
8202: NM m;
8203:
8204: sprintf(name,"%s/%d",Demand,index);
8205: s = fopen(name,"w");
8206: if ( !p ) {
1.245 noro 8207: len = 0;
8208: write_int(s,&len);
8209: fclose(s);
1.206 noro 8210: return;
8211: }
8212: nv = NV(p);
8213: sugar = SG(p);
8214: len = LEN(p);
8215: write_int(s,&nv); write_int(s,&sugar); write_int(s,&len);
1.245 noro 8216: for ( m = BDY(p); m; m = NEXT(m) ) {
8217: c = CM(m); write_int(s,&c);
8218: write_intarray(s,DL(m),nd_wpd);
8219: }
1.206 noro 8220: fclose(s);
8221: }
8222:
1.77 noro 8223: NDV ndv_load(int index)
8224: {
1.157 noro 8225: FILE *s;
8226: char name[BUFSIZ];
8227: short id;
8228: int nv,sugar,len,n,i,td,e,j;
8229: NDV d;
8230: NMV m0,m;
8231: unsigned int *dl;
8232: Obj obj;
8233: int mpos;
8234:
8235: sprintf(name,"%s/%d",Demand,index);
8236: s = fopen(name,"r");
8237: if ( !s ) return 0;
8238:
8239: skipvl(s);
8240: read_short(s,&id);
8241: if ( !id ) return 0;
8242: read_int(s,&nv);
8243: read_int(s,&sugar);
8244: read_int(s,&len);
8245:
8246: m0 = m = MALLOC(len*nmv_adv);
8247: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8248: loadobj(s,&obj); CQ(m) = (Q)obj;
8249: dl = DL(m);
8250: ndl_zero(dl);
8251: read_int(s,&td); TD(dl) = td;
8252: for ( j = 0; j < nv; j++ ) {
8253: read_int(s,&e);
8254: PUT_EXP(dl,j,e);
8255: }
8256: if ( nd_module ) {
8257: read_int(s,&mpos); MPOS(dl) = mpos;
8258: }
8259: if ( nd_blockmask ) ndl_weight_mask(dl);
8260: }
8261: fclose(s);
8262: MKNDV(nv,m0,len,d);
8263: SG(d) = sugar;
8264: return d;
1.99 noro 8265: }
8266:
1.206 noro 8267: ND nd_load_mod(int index)
8268: {
8269: FILE *s;
8270: char name[BUFSIZ];
8271: int nv,sugar,len,i,c;
1.245 noro 8272: ND d;
1.206 noro 8273: NM m0,m;
8274:
8275: sprintf(name,"%s/%d",Demand,index);
8276: s = fopen(name,"r");
1.245 noro 8277: /* if the file does not exist, it means p[index]=0 */
1.206 noro 8278: if ( !s ) return 0;
8279:
8280: read_int(s,&nv);
1.245 noro 8281: if ( !nv ) { fclose(s); return 0; }
1.206 noro 8282:
8283: read_int(s,&sugar);
8284: read_int(s,&len);
1.245 noro 8285: for ( m0 = 0, i = 0; i < len; i++ ) {
8286: NEXTNM(m0,m);
8287: read_int(s,&c); CM(m) = c;
8288: read_intarray(s,DL(m),nd_wpd);
8289: }
8290: NEXT(m) = 0;
1.206 noro 8291: MKND(nv,m0,len,d);
8292: SG(d) = sugar;
1.245 noro 8293: fclose(s);
1.206 noro 8294: return d;
8295: }
8296:
1.102 noro 8297: void nd_det(int mod,MAT f,P *rp)
1.99 noro 8298: {
1.157 noro 8299: VL fv,tv;
8300: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
8301: pointer **m;
8302: Q mone;
8303: P **w;
8304: P mp,r;
8305: NDV **dm;
8306: NDV *t,*mi,*mj;
8307: NDV d,s,mij,mjj;
8308: ND u;
8309: NMV nmv;
8310: UINT *bound;
8311: PGeoBucket bucket;
8312: struct order_spec *ord;
8313: Q dq,dt,ds;
8314: N gn,qn,dn0,nm,dn;
8315:
8316: create_order_spec(0,0,&ord);
8317: nd_init_ord(ord);
8318: get_vars((Obj)f,&fv);
8319: if ( f->row != f->col )
8320: error("nd_det : non-square matrix");
8321: n = f->row;
8322: m = f->body;
8323: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8324:
8325: if ( !nvar ) {
8326: if ( !mod )
8327: detp(CO,(P **)m,n,rp);
8328: else {
8329: w = (P **)almat_pointer(n,n);
8330: for ( i = 0; i < n; i++ )
8331: for ( j = 0; j < n; j++ )
8332: ptomp(mod,(P)m[i][j],&w[i][j]);
8333: detmp(CO,mod,w,n,&mp);
8334: mptop(mp,rp);
8335: }
8336: return;
8337: }
8338:
8339: if ( !mod ) {
8340: w = (P **)almat_pointer(n,n);
8341: dq = ONE;
8342: for ( i = 0; i < n; i++ ) {
8343: dn0 = ONEN;
8344: for ( j = 0; j < n; j++ ) {
8345: if ( !m[i][j] ) continue;
8346: lgp(m[i][j],&nm,&dn);
8347: gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
8348: }
8349: if ( !UNIN(dn0) ) {
8350: NTOQ(dn0,1,ds);
8351: for ( j = 0; j < n; j++ )
8352: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8353: mulq(dq,ds,&dt); dq = dt;
8354: } else
8355: for ( j = 0; j < n; j++ )
8356: w[i][j] = (P)m[i][j];
8357: }
8358: m = (pointer **)w;
8359: }
8360:
1.178 noro 8361: for ( i = 0, max = 1; i < n; i++ )
1.157 noro 8362: for ( j = 0; j < n; j++ )
8363: for ( tv = fv; tv; tv = NEXT(tv) ) {
8364: e = getdeg(tv->v,(P)m[i][j]);
8365: max = MAX(e,max);
8366: }
8367: nd_setup_parameters(nvar,max);
8368: dm = (NDV **)almat_pointer(n,n);
1.178 noro 8369: for ( i = 0, max = 1; i < n; i++ )
1.157 noro 8370: for ( j = 0; j < n; j++ ) {
8371: dm[i][j] = ptondv(CO,fv,m[i][j]);
8372: if ( mod ) ndv_mod(mod,dm[i][j]);
8373: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8374: }
8375: d = ptondv(CO,fv,(P)ONE);
8376: if ( mod ) ndv_mod(mod,d);
8377: chsgnq(ONE,&mone);
8378: for ( j = 0, sgn = 1; j < n; j++ ) {
1.222 fujimoto 8379: if ( DP_Print ) {
1.245 noro 8380: fprintf(asir_out,".",j);
8381: }
1.157 noro 8382: for ( i = j; i < n && !dm[i][j]; i++ );
8383: if ( i == n ) {
8384: *rp = 0;
8385: return;
8386: }
8387: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8388: for ( k = j; k < n; k++ )
8389: for ( l = j; l < n; l++ )
8390: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8391: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8392: }
8393: if ( k0 != j ) {
8394: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8395: sgn = -sgn;
8396: }
8397: if ( l0 != j ) {
8398: for ( k = j; k < n; k++ ) {
8399: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8400: }
8401: sgn = -sgn;
8402: }
8403: bound = nd_det_compute_bound(dm,n,j);
8404: for ( k = 0; k < nd_nvar; k++ )
8405: if ( bound[k]*2 > nd_mask0 ) break;
8406: if ( k < nd_nvar )
8407: nd_det_reconstruct(dm,n,j,d);
8408:
8409: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.241 noro 8410: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
1.157 noro 8411: mi = dm[i]; mij = mi[j];
8412: if ( mod )
8413: ndv_mul_c(mod,mij,mod-1);
8414: else
8415: ndv_mul_c_q(mij,mone);
8416: for ( k = j+1; k < n; k++ ) {
1.241 noro 8417: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
1.157 noro 8418: bucket = create_pbucket();
8419: if ( mi[k] ) {
8420: nmv = BDY(mjj); len = LEN(mjj);
8421: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8422: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8423: add_pbucket(mod,bucket,u);
8424: }
8425: }
8426: if ( mj[k] && mij ) {
8427: nmv = BDY(mij); len = LEN(mij);
8428: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8429: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8430: add_pbucket(mod,bucket,u);
8431: }
8432: }
8433: u = nd_quo(mod,bucket,d);
8434: mi[k] = ndtondv(mod,u);
8435: }
1.241 noro 8436: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
1.157 noro 8437: }
8438: d = mjj;
8439: }
1.222 fujimoto 8440: if ( DP_Print ) {
1.241 noro 8441: fprintf(asir_out,"\n",k);
1.222 fujimoto 8442: }
1.157 noro 8443: if ( sgn < 0 )
8444: if ( mod )
8445: ndv_mul_c(mod,d,mod-1);
8446: else
8447: ndv_mul_c_q(d,mone);
8448: r = ndvtop(mod,CO,fv,d);
8449: if ( !mod && !UNIQ(dq) )
8450: divsp(CO,r,(P)dq,rp);
8451: else
8452: *rp = r;
1.99 noro 8453: }
8454:
1.102 noro 8455: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99 noro 8456: {
1.157 noro 8457: NM mr,mr0;
8458: NM tnm;
8459: NMV m;
8460: UINT *d0,*dt,*dm;
8461: int c,n,td,i,c1,c2,len;
8462: Q q;
8463: ND r;
8464:
8465: if ( !p ) return 0;
8466: else {
8467: n = NV(p); m = BDY(p); len = LEN(p);
8468: d0 = DL(m0);
8469: td = TD(d);
8470: mr0 = 0;
8471: NEWNM(tnm);
8472: if ( mod ) {
8473: c = CM(m0);
8474: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8475: ndl_add(DL(m),d0,DL(tnm));
8476: if ( ndl_reducible(DL(tnm),d) ) {
8477: NEXTNM(mr0,mr);
8478: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8479: ndl_copy(DL(tnm),DL(mr));
8480: }
8481: }
8482: } else {
8483: q = CQ(m0);
8484: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8485: ndl_add(DL(m),d0,DL(tnm));
8486: if ( ndl_reducible(DL(tnm),d) ) {
8487: NEXTNM(mr0,mr);
8488: mulq(CQ(m),q,&CQ(mr));
8489: ndl_copy(DL(tnm),DL(mr));
8490: }
8491: }
8492: }
8493: if ( !mr0 )
8494: return 0;
8495: else {
8496: NEXT(mr) = 0;
8497: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8498: MKND(NV(p),mr0,len,r);
8499: SG(r) = SG(p) + TD(d0);
8500: return r;
8501: }
8502: }
1.114 noro 8503: }
8504:
8505: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8506: {
1.157 noro 8507: int i,obpe,oadv,h,k,l;
8508: static NM prev_nm_free_list;
8509: EPOS oepos;
8510:
8511: obpe = nd_bpe;
8512: oadv = nmv_adv;
8513: oepos = nd_epos;
8514: if ( obpe < 2 ) nd_bpe = 2;
8515: else if ( obpe < 3 ) nd_bpe = 3;
8516: else if ( obpe < 4 ) nd_bpe = 4;
8517: else if ( obpe < 5 ) nd_bpe = 5;
8518: else if ( obpe < 6 ) nd_bpe = 6;
8519: else if ( obpe < 8 ) nd_bpe = 8;
8520: else if ( obpe < 10 ) nd_bpe = 10;
8521: else if ( obpe < 16 ) nd_bpe = 16;
8522: else if ( obpe < 32 ) nd_bpe = 32;
8523: else error("nd_det_reconstruct : exponent too large");
8524:
8525: nd_setup_parameters(nd_nvar,0);
8526: prev_nm_free_list = _nm_free_list;
8527: _nm_free_list = 0;
8528: for ( k = j; k < n; k++ )
8529: for (l = j; l < n; l++ )
8530: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8531: ndv_realloc(d,obpe,oadv,oepos);
8532: prev_nm_free_list = 0;
1.114 noro 8533: #if 0
1.157 noro 8534: GC_gcollect();
1.114 noro 8535: #endif
8536: }
8537:
1.153 noro 8538: /* returns a UINT array containing degree bounds */
8539:
1.114 noro 8540: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8541: {
1.157 noro 8542: UINT *d0,*d1,*d,*t,*r;
8543: int k,l,i;
1.114 noro 8544:
1.157 noro 8545: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8546: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8547: for ( k = j; k < n; k++ )
8548: for ( l = j; l < n; l++ )
8549: if ( dm[k][l] ) {
8550: d = ndv_compute_bound(dm[k][l]);
8551: for ( i = 0; i < nd_nvar; i++ )
8552: d0[i] = MAX(d0[i],d[i]);
8553: }
8554: return d0;
1.117 noro 8555: }
8556:
8557: DL nd_separate_d(UINT *d,UINT *trans)
8558: {
1.157 noro 8559: int n,td,i,e,j;
8560: DL a;
1.117 noro 8561:
1.157 noro 8562: ndl_zero(trans);
8563: td = 0;
8564: for ( i = 0; i < nd_ntrans; i++ ) {
8565: e = GET_EXP(d,i);
8566: PUT_EXP(trans,i,e);
8567: td += MUL_WEIGHT(e,i);
8568: }
8569: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8570: /* homogenized */
8571: i = nd_nvar-1;
8572: e = GET_EXP(d,i);
8573: PUT_EXP(trans,i,e);
8574: td += MUL_WEIGHT(e,i);
8575: }
8576: TD(trans) = td;
8577: if ( nd_blockmask) ndl_weight_mask(trans);
8578: NEWDL(a,nd_nalg);
8579: td = 0;
8580: for ( i = 0; i < nd_nalg; i++ ) {
8581: j = nd_ntrans+i;
8582: e = GET_EXP(d,j);
8583: a->d[i] = e;
8584: td += e;
8585: }
8586: a->td = td;
8587: return a;
1.117 noro 8588: }
8589:
1.118 noro 8590: int nd_monic(int mod,ND *p)
1.117 noro 8591: {
1.157 noro 8592: UINT *trans,*t;
8593: DL alg;
8594: MP mp0,mp;
8595: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8596: ND r;
8597: DL dl;
8598: DP nm;
8599: NDV ndv;
8600: DAlg inv,cd;
8601: ND s,c;
8602: Q l,mul;
8603: N ln;
8604: int n,ntrans,i,e,td,is_lc,len;
8605: NumberField nf;
8606: struct oEGT eg0,eg1;
8607:
8608: if ( !(nf = get_numberfield()) )
8609: error("nd_monic : current_numberfield is not set");
8610:
8611: /* Q coef -> DAlg coef */
8612: NEWNM(ma0); ma = ma0;
8613: m = BDY(*p);
8614: is_lc = 1;
8615: while ( 1 ) {
8616: NEWMP(mp0); mp = mp0;
1.238 noro 8617: mp->c = (Obj)CQ(m);
1.157 noro 8618: mp->dl = nd_separate_d(DL(m),DL(ma));
8619: NEWNM(mb);
8620: for ( m = NEXT(m); m; m = NEXT(m) ) {
8621: alg = nd_separate_d(DL(m),DL(mb));
8622: if ( !ndl_equal(DL(ma),DL(mb)) )
8623: break;
1.238 noro 8624: NEXTMP(mp0,mp); mp->c = (Obj)CQ(m); mp->dl = alg;
1.157 noro 8625: }
8626: NEXT(mp) = 0;
8627: MKDP(nd_nalg,mp0,nm);
8628: MKDAlg(nm,ONE,cd);
8629: if ( is_lc == 1 ) {
8630: /* if the lc is a rational number, we have nothing to do */
8631: if ( !mp0->dl->td )
8632: return 1;
8633:
8634: get_eg(&eg0);
8635: invdalg(cd,&inv);
8636: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8637: /* check the validity of inv */
8638: if ( mod && !rem(NM(inv->dn),mod) )
8639: return 0;
8640: CA(ma) = nf->one;
8641: is_lc = 0;
8642: ln = ONEN;
8643: } else {
8644: muldalg(cd,inv,&CA(ma));
8645: lcmn(ln,NM(CA(ma)->dn),&ln);
8646: }
8647: if ( m ) {
8648: NEXT(ma) = mb; ma = mb;
8649: } else {
8650: NEXT(ma) = 0;
8651: break;
8652: }
8653: }
8654: /* l = lcm(denoms) */
8655: NTOQ(ln,1,l);
8656: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
8657: divq(l,CA(m)->dn,&mul);
8658: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8659: NEXTNM(mr0,mr);
8660: mulq((Q)mp->c,mul,&CQ(mr));
8661: dl = mp->dl;
8662: td = TD(DL(m));
8663: ndl_copy(DL(m),DL(mr));
8664: for ( i = 0; i < nd_nalg; i++ ) {
8665: e = dl->d[i];
8666: PUT_EXP(DL(mr),i+nd_ntrans,e);
8667: td += MUL_WEIGHT(e,i+nd_ntrans);
8668: }
1.163 noro 8669: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157 noro 8670: TD(DL(mr)) = td;
8671: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8672: }
8673: }
8674: NEXT(mr) = 0;
8675: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8676: MKND(NV(*p),mr0,len,r);
8677: /* XXX */
8678: SG(r) = SG(*p);
8679: nd_free(*p);
8680: *p = r;
8681: return 1;
1.59 noro 8682: }
1.167 noro 8683:
8684: NODE reverse_node(NODE n)
8685: {
8686: NODE t,t1;
8687:
8688: for ( t = 0; n; n = NEXT(n) ) {
8689: MKNODE(t1,BDY(n),t); t = t1;
8690: }
8691: return t;
8692: }
8693:
8694: P ndc_div(int mod,union oNDC a,union oNDC b)
8695: {
8696: union oNDC c;
8697: int inv,t;
8698:
8699: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.232 noro 8700: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.167 noro 8701: else if ( mod ) {
8702: inv = invm(b.m,mod);
8703: DMAR(a.m,inv,0,mod,t); c.m = t;
8704: } else if ( nd_vc )
8705: divsp(nd_vc,a.p,b.p,&c.p);
8706: else
8707: divq(a.z,b.z,&c.z);
8708: return ndctop(mod,c);
8709: }
8710:
8711: P ndctop(int mod,union oNDC c)
8712: {
8713: Q q;
8714: int e;
8715: GFS gfs;
8716:
8717: if ( mod == -1 ) {
8718: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
1.232 noro 8719: } else if ( mod > 0 ) {
1.167 noro 8720: STOQ(c.m,q); return (P)q;
8721: } else
8722: return (P)c.p;
8723: }
8724:
8725: /* [0,0,0,cont] = p -> p/cont */
8726:
8727: void finalize_tracelist(int i,P cont)
8728: {
1.245 noro 8729: LIST l;
8730: NODE node;
1.167 noro 8731: Q iq;
8732:
1.245 noro 8733: if ( !UNIQ(cont) ) {
1.196 noro 8734: node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167 noro 8735: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
1.245 noro 8736: nd_tracelist = node;
8737: }
1.167 noro 8738: STOQ(i,iq);
8739: nd_tracelist = reverse_node(nd_tracelist);
8740: MKLIST(l,nd_tracelist);
8741: node = mknode(2,iq,l); MKLIST(l,node);
8742: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8743: nd_alltracelist = node; nd_tracelist = 0;
8744: }
8745:
8746: void conv_ilist(int demand,int trace,NODE g,int **indp)
8747: {
8748: int n,i,j;
1.245 noro 8749: int *ind;
8750: NODE t;
1.167 noro 8751:
8752: n = length(g);
1.245 noro 8753: ind = (int *)MALLOC(n*sizeof(int));
8754: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8755: j = (long)BDY(t); ind[i] = j;
8756: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8757: }
8758: if ( indp ) *indp = ind;
1.167 noro 8759: }
1.172 noro 8760:
8761: void parse_nd_option(NODE opt)
8762: {
1.187 noro 8763: NODE t,p,u;
1.245 noro 8764: int i,s,n;
1.172 noro 8765: char *key;
8766: Obj value;
8767:
1.187 noro 8768: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.245 noro 8769: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8770: nd_splist = 0; nd_check_splist = 0;
1.231 noro 8771: nd_sugarweight = 0;
1.234 noro 8772: nd_f4red =0;
8773: nd_rank0 = 0;
1.172 noro 8774: for ( t = opt; t; t = NEXT(t) ) {
8775: p = BDY((LIST)BDY(t));
8776: key = BDY((STRING)BDY(p));
8777: value = (Obj)BDY(NEXT(p));
8778: if ( !strcmp(key,"gentrace") )
8779: nd_gentrace = value?1:0;
8780: else if ( !strcmp(key,"gensyz") )
8781: nd_gensyz = value?1:0;
1.173 noro 8782: else if ( !strcmp(key,"nora") )
8783: nd_nora = value?1:0;
1.187 noro 8784: else if ( !strcmp(key,"gbblock") ) {
1.233 noro 8785: if ( value && OID(value) == O_LIST ) {
1.245 noro 8786: u = BDY((LIST)value);
1.233 noro 8787: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.245 noro 8788: for ( i = 0; u; u = NEXT(u) ) {
8789: p = BDY((LIST)BDY(u));
8790: s = nd_gbblock[i++] = QTOS((Q)BDY(p));
8791: nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
8792: }
8793: nd_gbblock[i] = -1;
1.233 noro 8794: } else
8795: nd_gbblock = 0;
1.245 noro 8796: } else if ( !strcmp(key,"newelim") )
1.192 noro 8797: nd_newelim = value?1:0;
1.245 noro 8798: else if ( !strcmp(key,"intersect") )
1.195 noro 8799: nd_intersect = value?1:0;
1.245 noro 8800: else if ( !strcmp(key,"lf") )
1.234 noro 8801: nd_lf = value?1:0;
1.245 noro 8802: else if ( !strcmp(key,"trace") ) {
1.244 noro 8803: if ( value ) {
8804: u = BDY((LIST)value);
1.245 noro 8805: nd_nzlist = BDY((LIST)ARG2(u));
8806: nd_bpe = QTOS((Q)ARG3(u));
1.244 noro 8807: }
1.245 noro 8808: } else if ( !strcmp(key,"f4red") ) {
8809: nd_f4red = QTOS((Q)value);
8810: } else if ( !strcmp(key,"rank0") ) {
1.234 noro 8811: nd_rank0 = value?1:0;
1.245 noro 8812: } else if ( !strcmp(key,"splist") ) {
1.209 noro 8813: nd_splist = value?1:0;
1.245 noro 8814: } else if ( !strcmp(key,"check_splist") ) {
8815: nd_check_splist = BDY((LIST)value);
8816: } else if ( !strcmp(key,"sugarweight") ) {
8817: u = BDY((LIST)value);
1.231 noro 8818: n = length(u);
8819: nd_sugarweight = MALLOC(n*sizeof(int));
1.245 noro 8820: for ( i = 0; i < n; i++, u = NEXT(u) )
1.231 noro 8821: nd_sugarweight[i] = QTOS((Q)BDY(u));
1.245 noro 8822: }
1.172 noro 8823: }
8824: }
1.204 noro 8825:
8826: ND mdptond(DP d);
8827: ND nd_mul_nm(int mod,NM m0,ND p);
1.207 noro 8828: ND *btog(NODE ti,ND **p,int nb,int mod);
8829: ND btog_one(NODE ti,ND *p,int nb,int mod);
1.204 noro 8830: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
1.205 noro 8831: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
1.204 noro 8832:
8833: /* d:monomial */
8834: ND mdptond(DP d)
8835: {
8836: NM m;
8837: ND r;
8838:
8839: if ( OID(d) == 1 )
1.245 noro 8840: r = ptond(CO,CO,(P)d);
1.204 noro 8841: else {
8842: NEWNM(m);
8843: dltondl(NV(d),BDY(d)->dl,DL(m));
8844: CQ(m) = (Q)BDY(d)->c;
8845: NEXT(m) = 0;
8846: MKND(NV(d),m,1,r);
8847: }
8848: return r;
8849: }
8850:
8851: ND nd_mul_nm(int mod,NM m0,ND p)
8852: {
8853: UINT *d0;
8854: int c0,c1,c;
8855: NM tm,mr,mr0;
8856: ND r;
8857:
8858: if ( !p ) return 0;
8859: d0 = DL(m0);
8860: c0 = CM(m0);
8861: mr0 = 0;
8862: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8863: NEXTNM(mr0,mr);
1.245 noro 8864: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8865: ndl_add(d0,DL(tm),DL(mr));
1.204 noro 8866: }
8867: NEXT(mr) = 0;
8868: MKND(NV(p),mr0,LEN(p),r);
8869: return r;
8870: }
8871:
1.207 noro 8872: ND *btog(NODE ti,ND **p,int nb,int mod)
1.204 noro 8873: {
8874: PGeoBucket *r;
8875: int i,ci;
8876: NODE t,s;
8877: ND m,tp;
8878: ND *pi,*rd;
8879: P c;
8880:
8881: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8882: for ( i = 0; i < nb; i++ )
1.245 noro 8883: r[i] = create_pbucket();
1.204 noro 8884: for ( t = ti; t; t = NEXT(t) ) {
1.245 noro 8885: s = BDY((LIST)BDY(t));
1.204 noro 8886: if ( ARG0(s) ) {
1.245 noro 8887: m = mdptond((DP)ARG2(s));
8888: ptomp(mod,(P)HCQ(m),&c);
8889: if ( ci = ((MQ)c)->cont ) {
8890: HCM(m) = ci;
8891: pi = p[QTOS((Q)ARG1(s))];
8892: for ( i = 0; i < nb; i++ ) {
8893: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8894: add_pbucket(mod,r[i],tp);
8895: }
8896: }
8897: ci = 1;
1.204 noro 8898: } else {
1.245 noro 8899: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8900: ci = invm(ci,mod);
8901: }
1.204 noro 8902: }
8903: rd = (ND *)MALLOC(nb*sizeof(ND));
8904: for ( i = 0; i < nb; i++ )
1.245 noro 8905: rd[i] = normalize_pbucket(mod,r[i]);
1.204 noro 8906: if ( ci != 1 )
8907: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8908: return rd;
8909: }
8910:
1.207 noro 8911: ND btog_one(NODE ti,ND *p,int nb,int mod)
1.205 noro 8912: {
8913: PGeoBucket r;
1.206 noro 8914: int i,ci,j;
1.205 noro 8915: NODE t,s;
8916: ND m,tp;
8917: ND pi,rd;
8918: P c;
8919:
8920: r = create_pbucket();
8921: for ( t = ti; t; t = NEXT(t) ) {
1.245 noro 8922: s = BDY((LIST)BDY(t));
1.205 noro 8923: if ( ARG0(s) ) {
1.245 noro 8924: m = mdptond((DP)ARG2(s));
8925: ptomp(mod,(P)HCQ(m),&c);
8926: if ( ci = ((MQ)c)->cont ) {
8927: HCM(m) = ci;
8928: pi = p[j=QTOS((Q)ARG1(s))];
8929: if ( !pi ) {
8930: pi = nd_load_mod(j);
8931: tp = nd_mul_nm(mod,BDY(m),pi);
8932: nd_free(pi);
8933: add_pbucket(mod,r,tp);
1.205 noro 8934: } else {
1.245 noro 8935: tp = nd_mul_nm(mod,BDY(m),pi);
8936: add_pbucket(mod,r,tp);
8937: }
8938: }
8939: ci = 1;
8940: } else {
8941: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8942: ci = invm(ci,mod);
8943: }
1.205 noro 8944: }
8945: rd = normalize_pbucket(mod,r);
1.206 noro 8946: free_pbucket(r);
1.205 noro 8947: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8948: return rd;
8949: }
1.204 noro 8950:
8951: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8952: {
8953: int i,j,n,m,nb,pi0,pi1,nvar;
8954: VL fv,tv,vv;
8955: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8956: ND **p;
8957: ND *c;
8958: ND u;
8959: P inv;
8960: MAT mat;
8961:
8962: parse_nd_option(current_option);
8963: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8964: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8965: switch ( ord->id ) {
8966: case 1:
8967: if ( ord->nv != nvar )
8968: error("nd_check : invalid order specification");
8969: break;
8970: default:
8971: break;
8972: }
8973: nd_init_ord(ord);
8974: #if 0
8975: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
8976: #else
8977: nd_bpe = 32;
8978: #endif
8979: nd_setup_parameters(nvar,0);
8980: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8981: intred = BDY((LIST)ARG3(BDY(tlist)));
8982: ind = BDY((LIST)ARG4(BDY(tlist)));
8983: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8984: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.245 noro 8985: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
8986: if ( j > i ) i = j;
1.204 noro 8987: }
8988: n = i+1;
8989: nb = length(BDY(f));
8990: p = (ND **)MALLOC(n*sizeof(ND *));
8991: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8992: pi = BDY((LIST)BDY(t));
1.245 noro 8993: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
8994: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8995: ptomp(mod,(P)ARG2(pi),&inv);
8996: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8997: u = ptond(CO,vv,(P)ONE);
8998: HCM(u) = ((MQ)inv)->cont;
8999: c[pi1] = u;
1.204 noro 9000: }
9001: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
1.245 noro 9002: printf("%d ",i); fflush(stdout);
1.204 noro 9003: ti = BDY((LIST)BDY(t));
1.207 noro 9004: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204 noro 9005: }
9006: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
1.245 noro 9007: printf("%d ",i); fflush(stdout);
1.204 noro 9008: ti = BDY((LIST)BDY(t));
1.207 noro 9009: p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204 noro 9010: }
9011: m = length(ind);
9012: MKMAT(mat,nb,m);
9013: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
9014: for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
1.245 noro 9015: BDY(mat)[i][j] = ndtodp(mod,c[i]);
1.204 noro 9016: return mat;
9017: }
9018:
1.205 noro 9019: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
9020: LIST tlist,int pos,MAT *rp)
9021: {
9022: int i,j,n,m,nb,pi0,pi1,nvar;
9023: VL fv,tv,vv;
9024: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
9025: ND *p;
9026: ND *c;
9027: ND u;
9028: P inv;
9029: VECT vect;
9030:
9031: parse_nd_option(current_option);
9032: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9033: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9034: switch ( ord->id ) {
9035: case 1:
9036: if ( ord->nv != nvar )
9037: error("nd_check : invalid order specification");
9038: break;
9039: default:
9040: break;
9041: }
9042: nd_init_ord(ord);
9043: #if 0
9044: nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
9045: #else
9046: nd_bpe = 32;
9047: #endif
9048: nd_setup_parameters(nvar,0);
9049: permtrace = BDY((LIST)ARG2(BDY(tlist)));
9050: intred = BDY((LIST)ARG3(BDY(tlist)));
9051: ind = BDY((LIST)ARG4(BDY(tlist)));
9052: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
9053: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.245 noro 9054: j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
9055: if ( j > i ) i = j;
1.205 noro 9056: }
9057: n = i+1;
9058: nb = length(BDY(f));
9059: p = (ND *)MALLOC(n*sizeof(ND *));
9060: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
9061: pi = BDY((LIST)BDY(t));
1.245 noro 9062: pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
9063: if ( pi1 == pos ) {
9064: ptomp(mod,(P)ARG2(pi),&inv);
9065: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
9066: u = ptond(CO,vv,(P)ONE);
9067: HCM(u) = ((MQ)inv)->cont;
9068: p[pi0] = u;
1.205 noro 9069: }
9070: }
9071: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
1.245 noro 9072: printf("%d ",i); fflush(stdout);
1.205 noro 9073: ti = BDY((LIST)BDY(t));
1.207 noro 9074: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206 noro 9075: if ( Demand ) {
9076: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
1.245 noro 9077: }
1.205 noro 9078: }
9079: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
1.245 noro 9080: printf("%d ",i); fflush(stdout);
1.205 noro 9081: ti = BDY((LIST)BDY(t));
1.207 noro 9082: p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206 noro 9083: if ( Demand ) {
9084: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
1.245 noro 9085: }
1.205 noro 9086: }
9087: m = length(ind);
9088: MKVECT(vect,m);
1.206 noro 9089: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.245 noro 9090: u = p[QTOS((Q)BDY(t))];
9091: if ( !u ) {
9092: u = nd_load_mod(QTOS((Q)BDY(t)));
9093: BDY(vect)[j] = ndtodp(mod,u);
9094: nd_free(u);
9095: } else
9096: BDY(vect)[j] = ndtodp(mod,u);
1.206 noro 9097: }
1.205 noro 9098: return vect;
9099: }
1.232 noro 9100:
9101: void ndv_print_lf(NDV p)
9102: {
9103: NMV m;
9104: int i,len;
9105:
9106: if ( !p ) printf("0\n");
9107: else {
9108: len = LEN(p);
9109: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
9110: printf("+");
9111: mpz_out_str(asir_out,10,BDY(CZ(m)));
9112: printf("*");
9113: ndl_print(DL(m));
9114: }
9115: printf("\n");
9116: }
9117: }
1.234 noro 9118:
9119: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
9120: {
9121: VL tv,fv,vv,vc,av;
9122: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
9123: int m,nocheck,nvar,mindex,e,max;
9124: NDV c;
9125: NMV a;
9126: P p,zp;
9127: Q dmy;
9128: EPOS oepos;
9129: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
9130: Alg alpha,dp;
9131: P poly;
9132: LIST f1,f2,zpl;
9133: Obj obj;
9134: NumberField nf;
9135: struct order_spec *ord1;
9136: struct oEGT eg_check,eg0,eg1;
9137: NODE tr,tl1,tl2,tl3,tl4;
9138: LIST l1,l2,l3,l4,l5;
9139: int *perm;
9140: int j,ret;
9141: Q jq,bpe;
9142:
9143: nd_module = 0;
9144: parse_nd_option(current_option);
9145: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9146: if ( nd_vc )
9147: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
9148: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9149: switch ( ord->id ) {
9150: case 1:
9151: if ( ord->nv != nvar )
9152: error("nd_f4_lf_trace : invalid order specification");
9153: break;
9154: default:
9155: break;
9156: }
9157:
9158: nd_ntrans = nvar;
9159: nd_nalg = 0;
9160:
9161: nocheck = 0;
9162: mindex = 0;
9163:
1.235 noro 9164: /* do not use on-demand load/save */
9165: nd_demand = 0;
1.234 noro 9166: m = trace > 1 ? trace : get_lprime(mindex);
9167: nd_init_ord(ord);
9168: mrank = 0;
9169: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9170: for ( tv = vv; tv; tv = NEXT(tv) ) {
9171: if ( nd_module ) {
9172: s = BDY((LIST)BDY(t));
9173: trank = length(s);
9174: mrank = MAX(mrank,trank);
9175: for ( ; s; s = NEXT(s) ) {
9176: e = getdeg(tv->v,(P)BDY(s));
9177: max = MAX(e,max);
9178: }
9179: } else {
9180: e = getdeg(tv->v,(P)BDY(t));
9181: max = MAX(e,max);
9182: }
9183: }
9184: nd_setup_parameters(nvar,max);
9185: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9186: ishomo = 1;
9187: /* XXX */
9188: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9189: if ( nd_module ) {
9190: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9191: } else {
9192: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9193: }
9194: if ( ishomo )
9195: ishomo = ishomo && ndv_ishomo(c);
9196: if ( c ) {
9197: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9198: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9199: }
9200: }
9201: if ( in0 ) NEXT(in) = 0;
9202: if ( fd0 ) NEXT(fd) = 0;
9203: if ( !ishomo && homo ) {
9204: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9205: c = (NDV)BDY(t); len = LEN(c);
9206: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9207: wmax = MAX(TD(DL(a)),wmax);
9208: }
9209: homogenize_order(ord,nvar,&ord1);
9210: nd_init_ord(ord1);
9211: nd_setup_parameters(nvar+1,wmax);
9212: for ( t = fd0; t; t = NEXT(t) )
9213: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9214: }
9215: if ( MaxDeg > 0 ) nocheck = 1;
1.235 noro 9216: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9217: if ( ret )
9218: cand = nd_f4_lf_trace_main(m,&perm);
9219: if ( !ret || !cand ) {
9220: *rp = 0; return;
9221: }
9222: if ( !ishomo && homo ) {
9223: /* dehomogenization */
9224: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9225: nd_init_ord(ord);
9226: nd_setup_parameters(nvar,0);
1.234 noro 9227: }
1.235 noro 9228: cand = ndv_reducebase(cand,perm);
9229: cand = ndv_reduceall(-2,cand);
9230: cbpe = nd_bpe;
9231: get_eg(&eg0);
9232: if ( ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand) ) {
9233: /* gbcheck : cand is a GB of Id(cand) ? */
9234: ret = nd_f4(-2,0,0);
9235: }
1.245 noro 9236: if ( !ret ) {
1.235 noro 9237: /* failure */
9238: *rp = 0; return;
9239: }
1.234 noro 9240: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9241: if ( DP_Print )
1.241 noro 9242: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime+eg_check.gctime);
1.234 noro 9243: /* dp->p */
9244: nd_bpe = cbpe;
9245: nd_setup_parameters(nd_nvar,0);
9246: for ( r = cand; r; r = NEXT(r) ) {
9247: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9248: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9249: }
9250: MKLIST(*rp,cand);
9251: }
9252:
9253: NODE nd_f4_lf_trace_main(int m,int **indp)
9254: {
9255: int i,nh,stat,index;
9256: NODE r,rm,g;
9257: ND_pairs d,l,l0,t;
9258: ND spol,red;
9259: NDV nf,redv,nfqv,nfv;
9260: NM s0,s;
9261: NODE rp0,srp0,nflist,nflist_lf;
9262: int nsp,nred,col,rank,len,k,j,a;
9263: UINT c;
9264: UINT **spmat;
9265: UINT *s0vect,*svect,*p,*v;
9266: int *colstat;
9267: IndArray *imat;
9268: int *rhead;
9269: int spcol,sprow;
9270: int sugar;
9271: PGeoBucket bucket;
9272: struct oEGT eg0,eg1,eg_f4;
9273:
9274: g = 0; d = 0;
9275: for ( i = 0; i < nd_psn; i++ ) {
9276: d = update_pairs(d,g,i,0);
9277: g = update_base(g,i);
9278: }
9279: while ( d ) {
9280: get_eg(&eg0);
9281: l = nd_minsugarp(d,&d);
9282: sugar = SG(l);
9283: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9284: bucket = create_pbucket();
9285: stat = nd_sp_f4(m,0,l,bucket);
9286: if ( !stat ) {
9287: for ( t = l; NEXT(t); t = NEXT(t) );
9288: NEXT(t) = d; d = l;
9289: d = nd_reconstruct(1,d);
9290: continue;
9291: }
9292: if ( bucket->m < 0 ) continue;
9293: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9294: if ( !col ) {
9295: for ( t = l; NEXT(t); t = NEXT(t) );
9296: NEXT(t) = d; d = l;
9297: d = nd_reconstruct(1,d);
9298: continue;
9299: }
9300: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9301: if ( DP_Print )
1.241 noro 9302: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.234 noro 9303: sugar,eg_f4.exectime+eg_f4.gctime);
9304: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9305: if ( !l0 ) continue;
9306: l = l0;
9307:
9308: /* over LF */
9309: bucket = create_pbucket();
9310: stat = nd_sp_f4(-2,1,l,bucket);
9311: if ( !stat ) {
9312: for ( t = l; NEXT(t); t = NEXT(t) );
9313: NEXT(t) = d; d = l;
9314: d = nd_reconstruct(1,d);
9315: continue;
9316: }
9317: if ( bucket->m < 0 ) continue;
9318: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9319: if ( !col ) {
9320: for ( t = l; NEXT(t); t = NEXT(t) );
9321: NEXT(t) = d; d = l;
9322: d = nd_reconstruct(1,d);
9323: continue;
9324: }
9325: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9326: /* adding new bases */
9327: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9328: nfv = (NDV)BDY(rm);
9329: nfqv = (NDV)BDY(r);
9330: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9331: ndv_removecont(m,nfv);
9332: ndv_removecont(-2,nfqv);
9333: nh = ndv_newps(-2,nfv,nfqv,1);
9334: d = update_pairs(d,g,nh,0);
9335: g = update_base(g,nh);
9336: }
9337: if ( r || rm ) return 0;
9338: }
1.245 noro 9339: conv_ilist(nd_demand,1,g,indp);
1.234 noro 9340: return g;
9341: }
9342:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>