Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.15
1.15 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.14 2019/03/03 05:21:17 noro Exp $ */
1.1 noro 2:
3: #include "nd.h"
4:
1.11 noro 5: int Nnd_add,Nf4_red;
1.12 noro 6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1 noro 7:
8: int diag_period = 6;
9: int weight_check = 1;
10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
11: int nd_dcomp;
12: int nd_rref2;
13: NM _nm_free_list;
14: ND _nd_free_list;
15: ND_pairs _ndp_free_list;
16: NODE nd_hcf;
17:
18: Obj nd_top_weight;
19:
20: static NODE nd_subst;
21: static VL nd_vc;
22: static int nd_ntrans;
23: static int nd_nalg;
24: #if 0
25: static int ndv_alloc;
26: #endif
27: #if 1
28: static int nd_f4_nsp=0x7fffffff;
29: #else
30: static int nd_f4_nsp=50;
31: #endif
32: static double nd_scale=2;
33: static UINT **nd_bound;
34: static struct order_spec *nd_ord;
35: static EPOS nd_epos;
36: static BlockMask nd_blockmask;
37: static int nd_nvar;
38: static int nd_isrlex;
39: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
40: static UINT nd_mask[32];
41: static UINT nd_mask0,nd_mask1;
42:
43: static NDV *nd_ps;
44: static NDV *nd_ps_trace;
45: static NDV *nd_ps_sym;
46: static NDV *nd_ps_trace_sym;
47: static RHist *nd_psh;
48: static int nd_psn,nd_pslen;
49: static RHist *nd_red;
50: static int *nd_work_vector;
51: static int **nd_matrix;
52: static int nd_matrix_len;
53: static struct weight_or_block *nd_worb;
54: static int nd_worb_len;
55: static int nd_found,nd_create,nd_notfirst;
56: static int nmv_adv;
57: static int nd_demand;
58: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
59: static int nd_module_rank,nd_poly_weight_len;
60: static int *nd_poly_weight,*nd_module_weight;
61: static NODE nd_tracelist;
62: static NODE nd_alltracelist;
63: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
64: static int *nd_gbblock;
65: static NODE nd_nzlist,nd_check_splist;
66: static int nd_splist;
67: static int *nd_sugarweight;
68: static int nd_f4red,nd_rank0,nd_last_nonzero;
69:
70: NumberField get_numberfield();
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);
73: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
74: int nd_monic(int m,ND *p);
75: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
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);
79: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
80: void nmtodp(int mod,NM m,DP *r);
1.15 ! noro 81: void ndltodp(UINT *d,DP *r);
1.1 noro 82: NODE reverse_node(NODE n);
83: P ndc_div(int mod,union oNDC a,union oNDC b);
84: P ndctop(int mod,union oNDC c);
85: void finalize_tracelist(int i,P cont);
86: void conv_ilist(int demand,int trace,NODE g,int **indp);
87: void parse_nd_option(NODE opt);
88: void dltondl(int n,DL dl,UINT *r);
89: DP ndvtodp(int mod,NDV p);
90: DP ndtodp(int mod,ND p);
91:
92: void Pdp_set_weight(NODE,VECT *);
93: void Pox_cmo_rpc(NODE,Obj *);
94:
95: ND nd_add_lf(ND p1,ND p2);
96: void nd_mul_c_lf(ND p,Z mul);
97: void ndv_mul_c_lf(NDV p,Z mul);
98: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
99: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
100: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
101: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
102: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
103: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
104: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
105: NODE nd_f4_lf_trace_main(int m,int **indp);
106: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
107:
108: extern int lf_lazy;
109: extern Z current_mod_lf;
110:
111: extern int Denominator,DP_Multiple,MaxDeg;
112:
113: #define BLEN (8*sizeof(unsigned long))
114:
115: typedef struct matrix {
116: int row,col;
117: unsigned long **a;
118: } *matrix;
119:
120:
121: void nd_free_private_storage()
122: {
123: _nm_free_list = 0;
124: _ndp_free_list = 0;
125: #if 0
126: GC_gcollect();
127: #endif
128: }
129:
130: void _NM_alloc()
131: {
132: NM p;
133: int i;
134:
135: for ( i = 0; i < 1024; i++ ) {
136: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
137: p->next = _nm_free_list; _nm_free_list = p;
138: }
139: }
140:
141: matrix alloc_matrix(int row,int col)
142: {
143: unsigned long **a;
144: int i,len,blen;
145: matrix mat;
146:
147: mat = (matrix)MALLOC(sizeof(struct matrix));
148: mat->row = row;
149: mat->col = col;
150: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
151: return mat;
152: }
153:
154:
155: void _ND_alloc()
156: {
157: ND p;
158: int i;
159:
160: for ( i = 0; i < 1024; i++ ) {
161: p = (ND)MALLOC(sizeof(struct oND));
162: p->body = (NM)_nd_free_list; _nd_free_list = p;
163: }
164: }
165:
166: void _NDP_alloc()
167: {
168: ND_pairs p;
169: int i;
170:
171: for ( i = 0; i < 1024; i++ ) {
172: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
173: +(nd_wpd-1)*sizeof(UINT));
174: p->next = _ndp_free_list; _ndp_free_list = p;
175: }
176: }
177:
178: INLINE int nd_length(ND p)
179: {
180: NM m;
181: int i;
182:
183: if ( !p )
184: return 0;
185: else {
186: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
187: return i;
188: }
189: }
190:
191: extern int dp_negative_weight;
192:
193: INLINE int ndl_reducible(UINT *d1,UINT *d2)
194: {
195: UINT u1,u2;
196: int i,j;
197:
198: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
199:
200: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
201: #if USE_UNROLL
202: switch ( nd_bpe ) {
203: case 3:
204: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
205: u1 = d1[i]; u2 = d2[i];
206: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
207: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
208: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
209: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
210: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
211: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
212: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
213: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
214: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
215: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
216: }
217: return 1;
218: break;
219: case 4:
220: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
221: u1 = d1[i]; u2 = d2[i];
222: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
223: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
224: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
225: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
226: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
227: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
228: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
229: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
230: }
231: return 1;
232: break;
233: case 6:
234: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
235: u1 = d1[i]; u2 = d2[i];
236: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
237: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
238: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
239: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
240: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
241: }
242: return 1;
243: break;
244: case 8:
245: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
246: u1 = d1[i]; u2 = d2[i];
247: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
248: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
249: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
250: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
251: }
252: return 1;
253: break;
254: case 16:
255: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
256: u1 = d1[i]; u2 = d2[i];
257: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
258: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
259: }
260: return 1;
261: break;
262: case 32:
263: for ( i = nd_exporigin; i < nd_wpd; i++ )
264: if ( d1[i] < d2[i] ) return 0;
265: return 1;
266: break;
267: default:
268: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
269: u1 = d1[i]; u2 = d2[i];
270: for ( j = 0; j < nd_epw; j++ )
271: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
272: }
273: return 1;
274: }
275: #else
276: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
277: u1 = d1[i]; u2 = d2[i];
278: for ( j = 0; j < nd_epw; j++ )
279: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
280: }
281: return 1;
282: #endif
283: }
284:
285: /*
286: * If the current order is a block order,
287: * then the last block is length 1 and contains
288: * the homo variable. Otherwise, the original
289: * order is either 0 or 2.
290: */
291:
292: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
293: {
294: int w,i,e,n,omask0;
295:
296: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
297: n = nd_nvar-1;
298: ndl_zero(r);
299: for ( i = 0; i < n; i++ ) {
300: e = GET_EXP_OLD(d,i);
301: PUT_EXP(r,i,e);
302: }
303: w = TD(d);
304: PUT_EXP(r,nd_nvar-1,weight-w);
305: if ( nd_module ) MPOS(r) = d[ompos];
306: TD(r) = weight;
307: if ( nd_blockmask ) ndl_weight_mask(r);
308: }
309:
310: void ndl_dehomogenize(UINT *d)
311: {
312: UINT mask;
313: UINT h;
314: int i,bits;
315:
316: if ( nd_blockmask ) {
317: h = GET_EXP(d,nd_nvar-1);
318: XOR_EXP(d,nd_nvar-1,h);
319: TD(d) -= h;
320: ndl_weight_mask(d);
321: } else {
322: if ( nd_isrlex ) {
323: if ( nd_bpe == 32 ) {
324: h = d[nd_exporigin];
325: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
326: d[i-1] = d[i];
327: d[i-1] = 0;
328: TD(d) -= h;
329: } else {
330: bits = nd_epw*nd_bpe;
331: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
332: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
333: for ( i = nd_exporigin; i < nd_wpd; i++ )
334: d[i] = ((d[i]<<nd_bpe)&mask)
335: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
336: TD(d) -= h;
337: }
338: } else {
339: h = GET_EXP(d,nd_nvar-1);
340: XOR_EXP(d,nd_nvar-1,h);
341: TD(d) -= h;
342: }
343: }
344: }
345:
346: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
347: {
348: UINT t1,t2,u,u1,u2;
349: int i,j,l;
350:
351: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
352: error("ndl_lcm : inconsistent monomials");
353: #if USE_UNROLL
354: switch ( nd_bpe ) {
355: case 3:
356: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
357: u1 = d1[i]; u2 = d2[i];
358: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
359: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
360: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
361: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
362: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
363: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
364: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
365: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
366: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
367: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
368: d[i] = u;
369: }
370: break;
371: case 4:
372: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
373: u1 = d1[i]; u2 = d2[i];
374: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
375: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
376: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
377: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
378: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
379: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
380: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
381: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
382: d[i] = u;
383: }
384: break;
385: case 6:
386: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
387: u1 = d1[i]; u2 = d2[i];
388: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
389: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
390: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
391: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
392: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
393: d[i] = u;
394: }
395: break;
396: case 8:
397: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
398: u1 = d1[i]; u2 = d2[i];
399: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
400: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
401: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
402: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
403: d[i] = u;
404: }
405: break;
406: case 16:
407: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
408: u1 = d1[i]; u2 = d2[i];
409: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
410: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
411: d[i] = u;
412: }
413: break;
414: case 32:
415: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
416: u1 = d1[i]; u2 = d2[i];
417: d[i] = u1>u2?u1:u2;
418: }
419: break;
420: default:
421: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
422: u1 = d1[i]; u2 = d2[i];
423: for ( j = 0, u = 0; j < nd_epw; j++ ) {
424: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
425: }
426: d[i] = u;
427: }
428: break;
429: }
430: #else
431: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
432: u1 = d1[i]; u2 = d2[i];
433: for ( j = 0, u = 0; j < nd_epw; j++ ) {
434: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
435: }
436: d[i] = u;
437: }
438: #endif
439: if ( nd_module ) MPOS(d) = MPOS(d1);
440: TD(d) = ndl_weight(d);
441: if ( nd_blockmask ) ndl_weight_mask(d);
442: }
443:
444: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
445: {
446: UINT t1,t2,u,u1,u2;
447: int i,j,l;
448:
449: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
450: u1 = d1[i]; u2 = d2[i];
451: for ( j = 0, u = 0; j < nd_epw; j++ ) {
452: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
453: }
454: d[i] = u;
455: }
456: }
457:
458: int ndl_weight(UINT *d)
459: {
460: UINT t,u;
461: int i,j;
462:
463: if ( current_dl_weight_vector )
464: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
465: u = GET_EXP(d,i);
466: t += MUL_WEIGHT(u,i);
467: }
468: else
469: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
470: u = d[i];
471: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
472: t += (u&nd_mask0);
473: }
474: if ( nd_module && current_module_weight_vector && MPOS(d) )
475: t += current_module_weight_vector[MPOS(d)];
476: return t;
477: }
478:
479: /* for sugarweight */
480:
481: int ndl_weight2(UINT *d)
482: {
483: int t,u;
484: int i,j;
485:
486: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
487: u = GET_EXP(d,i);
488: t += nd_sugarweight[i]*u;
489: }
490: if ( nd_module && current_module_weight_vector && MPOS(d) )
491: t += current_module_weight_vector[MPOS(d)];
492: return t;
493: }
494:
495: void ndl_weight_mask(UINT *d)
496: {
497: UINT t,u;
498: UINT *mask;
499: int i,j,k,l;
500:
501: l = nd_blockmask->n;
502: for ( k = 0; k < l; k++ ) {
503: mask = nd_blockmask->mask[k];
504: if ( current_dl_weight_vector )
505: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
506: u = GET_EXP_MASK(d,i,mask);
507: t += MUL_WEIGHT(u,i);
508: }
509: else
510: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
511: u = d[i]&mask[i];
512: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
513: t += (u&nd_mask0);
514: }
515: d[k+1] = t;
516: }
517: }
518:
519: int ndl_lex_compare(UINT *d1,UINT *d2)
520: {
521: int i;
522:
523: d1 += nd_exporigin;
524: d2 += nd_exporigin;
525: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
526: if ( *d1 > *d2 )
527: return nd_isrlex ? -1 : 1;
528: else if ( *d1 < *d2 )
529: return nd_isrlex ? 1 : -1;
530: return 0;
531: }
532:
533: int ndl_block_compare(UINT *d1,UINT *d2)
534: {
535: int i,l,j,ord_o,ord_l;
536: struct order_pair *op;
537: UINT t1,t2,m;
538: UINT *mask;
539:
540: l = nd_blockmask->n;
541: op = nd_blockmask->order_pair;
542: for ( j = 0; j < l; j++ ) {
543: mask = nd_blockmask->mask[j];
544: ord_o = op[j].order;
545: if ( ord_o < 2 ) {
546: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
547: else if ( t1 < t2 ) return -1;
548: }
549: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
550: m = mask[i];
551: t1 = d1[i]&m;
552: t2 = d2[i]&m;
553: if ( t1 > t2 )
554: return !ord_o ? -1 : 1;
555: else if ( t1 < t2 )
556: return !ord_o ? 1 : -1;
557: }
558: }
559: return 0;
560: }
561:
562: int ndl_matrix_compare(UINT *d1,UINT *d2)
563: {
564: int i,j,s,row;
565: int *v;
566: Z **mat;
567: Z *w;
568: Z t1;
569: Z t,t2;
570:
1.6 noro 571: for ( j = 0; j < nd_nvar; j++ )
572: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 573: if ( nd_top_weight ) {
574: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 575: mat = (Z **)&BDY((VECT)nd_top_weight);
576: row = 1;
1.1 noro 577: } else {
578: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 579: row = ((MAT)nd_top_weight)->row;
1.1 noro 580: }
581: for ( i = 0; i < row; i++ ) {
1.6 noro 582: w = mat[i];
1.1 noro 583: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 584: STOZ(nd_work_vector[j],t1);
1.1 noro 585: mulz(w[j],t1,&t2);
586: addz(t,t2,&t1);
587: t = t1;
588: }
1.6 noro 589: if ( t ) {
590: s = sgnz(t);
1.1 noro 591: if ( s > 0 ) return 1;
592: else if ( s < 0 ) return -1;
1.6 noro 593: }
1.1 noro 594: }
1.6 noro 595: }
596: for ( i = 0; i < nd_matrix_len; i++ ) {
597: v = nd_matrix[i];
598: for ( j = 0, s = 0; j < nd_nvar; j++ )
599: s += v[j]*nd_work_vector[j];
600: if ( s > 0 ) return 1;
601: else if ( s < 0 ) return -1;
602: }
1.1 noro 603: if ( !ndl_equal(d1,d2) )
1.6 noro 604: error("ndl_matrix_compare : invalid matrix");
605: return 0;
1.1 noro 606: }
607:
608: int ndl_composite_compare(UINT *d1,UINT *d2)
609: {
610: int i,j,s,start,end,len,o;
611: int *v;
612: struct sparse_weight *sw;
613:
614: for ( j = 0; j < nd_nvar; j++ )
615: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
616: for ( i = 0; i < nd_worb_len; i++ ) {
617: len = nd_worb[i].length;
618: switch ( nd_worb[i].type ) {
619: case IS_DENSE_WEIGHT:
620: v = nd_worb[i].body.dense_weight;
621: for ( j = 0, s = 0; j < len; j++ )
622: s += v[j]*nd_work_vector[j];
623: if ( s > 0 ) return 1;
624: else if ( s < 0 ) return -1;
625: break;
626: case IS_SPARSE_WEIGHT:
627: sw = nd_worb[i].body.sparse_weight;
628: for ( j = 0, s = 0; j < len; j++ )
629: s += sw[j].value*nd_work_vector[sw[j].pos];
630: if ( s > 0 ) return 1;
631: else if ( s < 0 ) return -1;
632: break;
633: case IS_BLOCK:
634: o = nd_worb[i].body.block.order;
635: start = nd_worb[i].body.block.start;
636: switch ( o ) {
637: case 0:
638: end = start+len;
639: for ( j = start, s = 0; j < end; j++ )
640: s += MUL_WEIGHT(nd_work_vector[j],j);
641: if ( s > 0 ) return 1;
642: else if ( s < 0 ) return -1;
643: for ( j = end-1; j >= start; j-- )
644: if ( nd_work_vector[j] < 0 ) return 1;
645: else if ( nd_work_vector[j] > 0 ) return -1;
646: break;
647: case 1:
648: end = start+len;
649: for ( j = start, s = 0; j < end; j++ )
650: s += MUL_WEIGHT(nd_work_vector[j],j);
651: if ( s > 0 ) return 1;
652: else if ( s < 0 ) return -1;
653: for ( j = start; j < end; j++ )
654: if ( nd_work_vector[j] > 0 ) return 1;
655: else if ( nd_work_vector[j] < 0 ) return -1;
656: break;
657: case 2:
658: end = start+len;
659: for ( j = start; j < end; j++ )
660: if ( nd_work_vector[j] > 0 ) return 1;
661: else if ( nd_work_vector[j] < 0 ) return -1;
662: break;
663: }
664: break;
665: }
666: }
667: return 0;
668: }
669:
670: /* TDH -> WW -> TD-> RL */
671:
672: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
673: {
674: int i,m,e1,e2;
675:
676: if ( TD(d1) > TD(d2) ) return 1;
677: else if ( TD(d1) < TD(d2) ) return -1;
678: m = nd_nvar>>1;
679: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
680: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
681: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
682: }
683: if ( e1 > e2 ) return 1;
684: else if ( e1 < e2 ) return -1;
685: return ndl_lex_compare(d1,d2);
686: }
687:
688: int ndl_module_weight_compare(UINT *d1,UINT *d2)
689: {
690: int s,j;
691:
692: if ( nd_nvar != nd_poly_weight_len )
693: error("invalid module weight : the length of polynomial weight != the number of variables");
694: s = 0;
695: for ( j = 0; j < nd_nvar; j++ )
696: s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
697: if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
698: s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
699: }
700: if ( s > 0 ) return 1;
701: else if ( s < 0 ) return -1;
702: else return 0;
703: }
704:
705: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
706: {
707: int i,c;
708:
709: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
710: if ( nd_ispot ) {
711: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
712: if ( TD(d1) > TD(d2) ) return 1;
713: else if ( TD(d1) < TD(d2) ) return -1;
714: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
715: if ( MPOS(d1) < MPOS(d2) ) return 1;
716: else if ( MPOS(d1) > MPOS(d2) ) return -1;
717: return 0;
718: }
719: if ( MPOS(d1) < MPOS(d2) ) return 1;
720: else if ( MPOS(d1) > MPOS(d2) ) return -1;
721: }
722: if ( TD(d1) > TD(d2) ) return 1;
723: else if ( TD(d1) < TD(d2) ) return -1;
724: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
725: if ( !nd_ispot ) {
726: if ( MPOS(d1) < MPOS(d2) ) return 1;
727: else if ( MPOS(d1) > MPOS(d2) ) return -1;
728: }
729: return 0;
730: }
731:
732: int ndl_module_glex_compare(UINT *d1,UINT *d2)
733: {
734: int i,c;
735:
736: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
737: if ( nd_ispot ) {
738: if ( MPOS(d1) < MPOS(d2) ) return 1;
739: else if ( MPOS(d1) > MPOS(d2) ) return -1;
740: }
741: if ( TD(d1) > TD(d2) ) return 1;
742: else if ( TD(d1) < TD(d2) ) return -1;
743: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
744: if ( !nd_ispot ) {
745: if ( MPOS(d1) < MPOS(d2) ) return 1;
746: else if ( MPOS(d1) > MPOS(d2) ) return -1;
747: }
748: return 0;
749: }
750:
751: int ndl_module_lex_compare(UINT *d1,UINT *d2)
752: {
753: int i,c;
754:
755: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
756: if ( nd_ispot ) {
757: if ( MPOS(d1) < MPOS(d2) ) return 1;
758: else if ( MPOS(d1) > MPOS(d2) ) return -1;
759: }
760: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
761: if ( !nd_ispot ) {
762: if ( MPOS(d1) < MPOS(d2) ) return 1;
763: else if ( MPOS(d1) > MPOS(d2) ) return -1;
764: }
765: return 0;
766: }
767:
768: int ndl_module_block_compare(UINT *d1,UINT *d2)
769: {
770: int i,c;
771:
772: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
773: if ( nd_ispot ) {
774: if ( MPOS(d1) < MPOS(d2) ) return 1;
775: else if ( MPOS(d1) > MPOS(d2) ) return -1;
776: }
777: if ( (c = ndl_block_compare(d1,d2)) != 0 ) return c;
778: if ( !nd_ispot ) {
779: if ( MPOS(d1) < MPOS(d2) ) return 1;
780: else if ( MPOS(d1) > MPOS(d2) ) return -1;
781: }
782: return 0;
783: }
784:
785: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
786: {
787: int i,c;
788:
789: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
790: if ( nd_ispot ) {
791: if ( MPOS(d1) < MPOS(d2) ) return 1;
792: else if ( MPOS(d1) > MPOS(d2) ) return -1;
793: }
794: if ( (c = ndl_matrix_compare(d1,d2)) != 0 ) return c;
795: if ( !nd_ispot ) {
796: if ( MPOS(d1) < MPOS(d2) ) return 1;
797: else if ( MPOS(d1) > MPOS(d2) ) return -1;
798: }
799: return 0;
800: }
801:
802: int ndl_module_composite_compare(UINT *d1,UINT *d2)
803: {
804: int i,c;
805:
806: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
807: if ( nd_ispot ) {
808: if ( MPOS(d1) > MPOS(d2) ) return 1;
809: else if ( MPOS(d1) < MPOS(d2) ) return -1;
810: }
811: if ( (c = ndl_composite_compare(d1,d2)) != 0 ) return c;
812: if ( !nd_ispot ) {
813: if ( MPOS(d1) > MPOS(d2) ) return 1;
814: else if ( MPOS(d1) < MPOS(d2) ) return -1;
815: }
816: return 0;
817: }
818:
819: INLINE int ndl_equal(UINT *d1,UINT *d2)
820: {
821: int i;
822:
823: switch ( nd_wpd ) {
824: case 2:
825: if ( TD(d2) != TD(d1) ) return 0;
826: if ( d2[1] != d1[1] ) return 0;
827: return 1;
828: break;
829: case 3:
830: if ( TD(d2) != TD(d1) ) return 0;
831: if ( d2[1] != d1[1] ) return 0;
832: if ( d2[2] != d1[2] ) return 0;
833: return 1;
834: break;
835: default:
836: for ( i = 0; i < nd_wpd; i++ )
837: if ( *d1++ != *d2++ ) return 0;
838: return 1;
839: break;
840: }
841: }
842:
843: INLINE void ndl_copy(UINT *d1,UINT *d2)
844: {
845: int i;
846:
847: switch ( nd_wpd ) {
848: case 2:
849: TD(d2) = TD(d1);
850: d2[1] = d1[1];
851: break;
852: case 3:
853: TD(d2) = TD(d1);
854: d2[1] = d1[1];
855: d2[2] = d1[2];
856: break;
857: default:
858: for ( i = 0; i < nd_wpd; i++ )
859: d2[i] = d1[i];
860: break;
861: }
862: }
863:
864: INLINE void ndl_zero(UINT *d)
865: {
866: int i;
867: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
868: }
869:
870: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
871: {
872: int i;
873:
874: if ( nd_module ) {
875: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
876: error("ndl_add : invalid operation");
877: }
878: #if 1
879: switch ( nd_wpd ) {
880: case 2:
881: TD(d) = TD(d1)+TD(d2);
882: d[1] = d1[1]+d2[1];
883: break;
884: case 3:
885: TD(d) = TD(d1)+TD(d2);
886: d[1] = d1[1]+d2[1];
887: d[2] = d1[2]+d2[2];
888: break;
889: default:
890: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
891: break;
892: }
893: #else
894: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
895: #endif
896: }
897:
898: /* d1 += d2 */
899: INLINE void ndl_addto(UINT *d1,UINT *d2)
900: {
901: int i;
902:
903: if ( nd_module ) {
904: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
905: error("ndl_addto : invalid operation");
906: }
907: #if 1
908: switch ( nd_wpd ) {
909: case 2:
910: TD(d1) += TD(d2);
911: d1[1] += d2[1];
912: break;
913: case 3:
914: TD(d1) += TD(d2);
915: d1[1] += d2[1];
916: d1[2] += d2[2];
917: break;
918: default:
919: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
920: break;
921: }
922: #else
923: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
924: #endif
925: }
926:
927: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
928: {
929: int i;
930:
931: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
932: }
933:
934: int ndl_disjoint(UINT *d1,UINT *d2)
935: {
936: UINT t1,t2,u,u1,u2;
937: int i,j;
938:
939: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
940: #if USE_UNROLL
941: switch ( nd_bpe ) {
942: case 3:
943: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
944: u1 = d1[i]; u2 = d2[i];
945: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
946: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
947: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
948: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
949: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
950: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
951: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
952: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
953: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
954: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
955: }
956: return 1;
957: break;
958: case 4:
959: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
960: u1 = d1[i]; u2 = d2[i];
961: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
962: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
963: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
964: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
965: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
966: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
967: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
968: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
969: }
970: return 1;
971: break;
972: case 6:
973: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
974: u1 = d1[i]; u2 = d2[i];
975: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
976: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
977: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
978: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
979: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
980: }
981: return 1;
982: break;
983: case 8:
984: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
985: u1 = d1[i]; u2 = d2[i];
986: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
987: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
988: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
989: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
990: }
991: return 1;
992: break;
993: case 16:
994: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
995: u1 = d1[i]; u2 = d2[i];
996: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
997: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
998: }
999: return 1;
1000: break;
1001: case 32:
1002: for ( i = nd_exporigin; i < nd_wpd; i++ )
1003: if ( d1[i] && d2[i] ) return 0;
1004: return 1;
1005: break;
1006: default:
1007: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1008: u1 = d1[i]; u2 = d2[i];
1009: for ( j = 0; j < nd_epw; j++ ) {
1010: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1011: u1 >>= nd_bpe; u2 >>= nd_bpe;
1012: }
1013: }
1014: return 1;
1015: break;
1016: }
1017: #else
1018: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1019: u1 = d1[i]; u2 = d2[i];
1020: for ( j = 0; j < nd_epw; j++ ) {
1021: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1022: u1 >>= nd_bpe; u2 >>= nd_bpe;
1023: }
1024: }
1025: return 1;
1026: #endif
1027: }
1028:
1029: int ndl_check_bound(UINT *d1,UINT *d2)
1030: {
1031: UINT u2;
1032: int i,j,ind,k;
1033:
1034: ind = 0;
1035: #if USE_UNROLL
1036: switch ( nd_bpe ) {
1037: case 3:
1038: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1039: u2 = d2[i];
1040: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1041: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1042: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1043: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1044: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1045: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1046: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1047: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1048: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1049: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1050: }
1051: return 0;
1052: break;
1053: case 4:
1054: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1055: u2 = d2[i];
1056: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1057: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1058: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1059: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1060: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1061: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1062: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1063: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1064: }
1065: return 0;
1066: break;
1067: case 6:
1068: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1069: u2 = d2[i];
1070: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1071: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1072: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1073: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1074: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1075: }
1076: return 0;
1077: break;
1078: case 8:
1079: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1080: u2 = d2[i];
1081: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1082: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1083: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1084: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1085: }
1086: return 0;
1087: break;
1088: case 16:
1089: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1090: u2 = d2[i];
1091: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1092: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1093: }
1094: return 0;
1095: break;
1096: case 32:
1097: for ( i = nd_exporigin; i < nd_wpd; i++ )
1098: if ( d1[i]+d2[i]<d1[i] ) return 1;
1099: return 0;
1100: break;
1101: default:
1102: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1103: u2 = d2[i];
1104: k = (nd_epw-1)*nd_bpe;
1105: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1106: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1107: }
1108: return 0;
1109: break;
1110: }
1111: #else
1112: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1113: u2 = d2[i];
1114: k = (nd_epw-1)*nd_bpe;
1115: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1116: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1117: }
1118: return 0;
1119: #endif
1120: }
1121:
1122: int ndl_check_bound2(int index,UINT *d2)
1123: {
1124: return ndl_check_bound(nd_bound[index],d2);
1125: }
1126:
1127: INLINE int ndl_hash_value(UINT *d)
1128: {
1129: int i;
1.11 noro 1130: UINT r;
1.1 noro 1131:
1132: r = 0;
1133: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1134: r = (r*1511+d[i]);
1.11 noro 1135: r %= REDTAB_LEN;
1.1 noro 1136: return r;
1137: }
1138:
1139: INLINE int ndl_find_reducer(UINT *dg)
1140: {
1141: RHist r;
1142: int d,k,i;
1143:
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;
1171: }
1172:
1173: ND nd_merge(ND p1,ND p2)
1174: {
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: }
1213: }
1214:
1215: ND nd_add(int mod,ND p1,ND p2)
1216: {
1217: int n,c;
1218: int t,can,td1,td2;
1219: ND r;
1220: NM m1,m2,mr0,mr,s;
1221:
1.11 noro 1222: Nnd_add++;
1.1 noro 1223: if ( !p1 ) return p2;
1224: else if ( !p2 ) return p1;
1225: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1226: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1227: else if ( !mod ) return nd_add_q(p1,p2);
1228: else {
1229: can = 0;
1230: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1231: c = DL_COMPARE(DL(m1),DL(m2));
1232: switch ( c ) {
1233: case 0:
1234: t = ((CM(m1))+(CM(m2))) - mod;
1235: if ( t < 0 ) t += mod;
1236: s = m1; m1 = NEXT(m1);
1237: if ( t ) {
1238: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1239: } else {
1240: can += 2; FREENM(s);
1241: }
1242: s = m2; m2 = NEXT(m2); FREENM(s);
1243: break;
1244: case 1:
1245: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1246: break;
1247: case -1:
1248: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1249: break;
1250: }
1251: }
1252: if ( !mr0 )
1253: if ( m1 ) mr0 = m1;
1254: else if ( m2 ) mr0 = m2;
1255: else return 0;
1256: else if ( m1 ) NEXT(mr) = m1;
1257: else if ( m2 ) NEXT(mr) = m2;
1258: else NEXT(mr) = 0;
1259: BDY(p1) = mr0;
1260: SG(p1) = MAX(SG(p1),SG(p2));
1261: LEN(p1) = LEN(p1)+LEN(p2)-can;
1262: FREEND(p2);
1263: return p1;
1264: }
1265: }
1266:
1267: /* XXX on opteron, the inlined manipulation of destructive additon of
1268: * two NM seems to make gcc optimizer get confused, so the part is
1269: * done in a function.
1270: */
1271:
1272: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1273: {
1274: NM s;
1275: P t;
1276: int can;
1277:
1278: addp(nd_vc,CP(*m1),CP(*m2),&t);
1279: s = *m1; *m1 = NEXT(*m1);
1280: if ( t ) {
1281: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1282: } else {
1283: can = 2; FREENM(s);
1284: }
1285: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1286: return can;
1287: }
1288:
1289: ND nd_add_q(ND p1,ND p2)
1290: {
1291: int n,c,can;
1292: ND r;
1293: NM m1,m2,mr0,mr,s;
1294: P t;
1295:
1296: if ( !p1 ) return p2;
1297: else if ( !p2 ) return p1;
1298: else {
1299: can = 0;
1300: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1301: c = DL_COMPARE(DL(m1),DL(m2));
1302: switch ( c ) {
1303: case 0:
1304: #if defined(__x86_64__)
1305: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1306: #else
1307: addp(nd_vc,CP(m1),CP(m2),&t);
1308: s = m1; m1 = NEXT(m1);
1309: if ( t ) {
1310: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1311: } else {
1312: can += 2; FREENM(s);
1313: }
1314: s = m2; m2 = NEXT(m2); FREENM(s);
1315: #endif
1316: break;
1317: case 1:
1318: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1319: break;
1320: case -1:
1321: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1322: break;
1323: }
1324: }
1325: if ( !mr0 )
1326: if ( m1 ) mr0 = m1;
1327: else if ( m2 ) mr0 = m2;
1328: else return 0;
1329: else if ( m1 ) NEXT(mr) = m1;
1330: else if ( m2 ) NEXT(mr) = m2;
1331: else NEXT(mr) = 0;
1332: BDY(p1) = mr0;
1333: SG(p1) = MAX(SG(p1),SG(p2));
1334: LEN(p1) = LEN(p1)+LEN(p2)-can;
1335: FREEND(p2);
1336: return p1;
1337: }
1338: }
1339:
1340: ND nd_add_sf(ND p1,ND p2)
1341: {
1342: int n,c,can;
1343: ND r;
1344: NM m1,m2,mr0,mr,s;
1345: int t;
1346:
1347: if ( !p1 ) return p2;
1348: else if ( !p2 ) return p1;
1349: else {
1350: can = 0;
1351: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1352: c = DL_COMPARE(DL(m1),DL(m2));
1353: switch ( c ) {
1354: case 0:
1355: t = _addsf(CM(m1),CM(m2));
1356: s = m1; m1 = NEXT(m1);
1357: if ( t ) {
1358: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1359: } else {
1360: can += 2; FREENM(s);
1361: }
1362: s = m2; m2 = NEXT(m2); FREENM(s);
1363: break;
1364: case 1:
1365: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1366: break;
1367: case -1:
1368: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1369: break;
1370: }
1371: }
1372: if ( !mr0 )
1373: if ( m1 ) mr0 = m1;
1374: else if ( m2 ) mr0 = m2;
1375: else return 0;
1376: else if ( m1 ) NEXT(mr) = m1;
1377: else if ( m2 ) NEXT(mr) = m2;
1378: else NEXT(mr) = 0;
1379: BDY(p1) = mr0;
1380: SG(p1) = MAX(SG(p1),SG(p2));
1381: LEN(p1) = LEN(p1)+LEN(p2)-can;
1382: FREEND(p2);
1383: return p1;
1384: }
1385: }
1386:
1387:
1388: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1389: {
1390: int c,c1,c2;
1391: Z cg,cred,gcd,tq;
1392: P cgp,credp,gcdp;
1393: Obj tr,tr1;
1394:
1395: if ( mod == -1 ) {
1396: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1397: *divp = (Obj)ONE;
1398: } else if ( mod == -2 ) {
1399: Z inv,t;
1400: divlf(ONE,HCZ(p),&inv);
1401: chsgnlf(HCZ(g),&t);
1402: mullf(inv,t,&CZ(mul));
1403: *divp = (Obj)ONE;
1404: } else if ( mod ) {
1405: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1406: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1407: *divp = (Obj)ONE;
1408: } else if ( nd_vc ) {
1409: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1410: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1411: chsgnp(cgp,&CP(mul));
1412: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1413: if ( dn ) {
1414: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1415: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1416: }
1417: *divp = (Obj)credp;
1418: } else {
1.6 noro 1419: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1420: chsgnz(cg,&CZ(mul));
1.1 noro 1421: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1422: if ( dn ) {
1423: mulz(dn->z,cred,&tq); dn->z = tq;
1424: }
1425: *divp = (Obj)cred;
1426: }
1427: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1428: }
1429:
1430: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1431: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1432: {
1433: NM m,mrd,tail;
1434: NM mul;
1435: int n,sugar,psugar,sugar0,stat,index;
1436: int c,c1,c2,dummy;
1437: RHist h;
1438: NDV p,red;
1439: Q cg,cred,gcd,tq,qq;
1440: Z iq;
1441: DP dmul;
1442: NODE node;
1443: LIST hist;
1444: double hmag;
1445: P tp,tp1;
1446: Obj tr,tr1,div;
1447: union oNDC hg;
1448: P cont;
1449:
1450: if ( !g ) {
1451: *rp = d;
1452: return 1;
1453: }
1454: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1455:
1456: sugar0 = sugar = SG(g);
1457: n = NV(g);
1458: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1459: if ( d )
1460: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1461: for ( ; g; ) {
1462: index = ndl_find_reducer(HDL(g));
1463: if ( index >= 0 ) {
1464: h = nd_psh[index];
1465: ndl_sub(HDL(g),DL(h),DL(mul));
1466: if ( ndl_check_bound2(index,DL(mul)) ) {
1467: nd_free(g); nd_free(d);
1468: return 0;
1469: }
1470: p = nd_demand ? ndv_load(index) : ps[index];
1471: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1472: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1473: if ( nd_gentrace ) {
1474: /* Trace=[div,index,mul,ONE] */
1.6 noro 1475: STOZ(index,iq);
1.1 noro 1476: nmtodp(mod,mul,&dmul);
1477: node = mknode(4,div,iq,dmul,ONE);
1478: }
1479: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1480: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1481: hg = HCU(g);
1482: nd_removecont2(d,g);
1.6 noro 1483: if ( nd_gentrace ) {
1.1 noro 1484: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1485: /* exact division */
1.1 noro 1486: cont = ndc_div(mod,hg,HCU(g));
1487: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1488: }
1489: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1490: }
1491: MKLIST(hist,node);
1492: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1493: } else if ( !full ) {
1494: *rp = g;
1495: return 1;
1496: } else {
1497: m = BDY(g);
1498: if ( NEXT(m) ) {
1499: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1500: } else {
1501: FREEND(g); g = 0;
1502: }
1503: if ( d ) {
1504: NEXT(tail)=m; tail=m; LEN(d)++;
1505: } else {
1506: MKND(n,m,1,d); tail = BDY(d);
1507: }
1508: }
1509: }
1510: if ( d ) SG(d) = sugar;
1511: *rp = d;
1512: return 1;
1513: }
1514:
1515: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1516: {
1517: int hindex,index;
1518: NDV p;
1519: ND u,d,red;
1520: NODE l;
1521: NM mul,m,mrd,tail;
1522: int sugar,psugar,n,h_reducible;
1523: PGeoBucket bucket;
1524: int c,c1,c2;
1525: Z cg,cred,gcd,zzz;
1526: RHist h;
1527: double hmag,gmag;
1528: int count = 0;
1529: int hcount = 0;
1530:
1531: if ( !g ) {
1532: *rp = 0;
1533: return 1;
1534: }
1535: sugar = SG(g);
1536: n = NV(g);
1.6 noro 1537: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1538: bucket = create_pbucket();
1539: add_pbucket(mod,bucket,g);
1540: d = 0;
1541: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1542: while ( 1 ) {
1543: if ( mod > 0 || mod == -1 )
1544: hindex = head_pbucket(mod,bucket);
1545: else if ( mod == -2 )
1546: hindex = head_pbucket_lf(bucket);
1547: else
1548: hindex = head_pbucket_q(bucket);
1549: if ( hindex < 0 ) {
1550: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1551: if ( d ) SG(d) = sugar;
1552: *rp = d;
1553: return 1;
1554: }
1555: g = bucket->body[hindex];
1556: index = ndl_find_reducer(HDL(g));
1557: if ( index >= 0 ) {
1558: count++;
1559: if ( !d ) hcount++;
1560: h = nd_psh[index];
1561: ndl_sub(HDL(g),DL(h),DL(mul));
1562: if ( ndl_check_bound2(index,DL(mul)) ) {
1563: nd_free(d);
1564: free_pbucket(bucket);
1565: *rp = 0;
1566: return 0;
1567: }
1568: p = ps[index];
1569: if ( mod == -1 )
1570: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1571: else if ( mod == -2 ) {
1572: Z inv,t;
1573: divlf(ONE,HCZ(p),&inv);
1574: chsgnlf(HCZ(g),&t);
1575: mullf(inv,t,&CZ(mul));
1576: } else if ( mod ) {
1577: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1578: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1579: } else {
1.6 noro 1580: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1581: chsgnz(cg,&CZ(mul));
1.1 noro 1582: nd_mul_c_q(d,(P)cred);
1583: mulq_pbucket(bucket,cred);
1584: g = bucket->body[hindex];
1.6 noro 1585: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1586: }
1587: red = ndv_mul_nm(mod,mul,p);
1588: bucket->body[hindex] = nd_remove_head(g);
1589: red = nd_remove_head(red);
1590: add_pbucket(mod,bucket,red);
1591: psugar = SG(p)+TD(DL(mul));
1592: sugar = MAX(sugar,psugar);
1593: if ( !mod && hmag && (gmag > hmag) ) {
1594: g = normalize_pbucket(mod,bucket);
1595: if ( !g ) {
1596: if ( d ) SG(d) = sugar;
1597: *rp = d;
1598: return 1;
1599: }
1600: nd_removecont2(d,g);
1.6 noro 1601: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1602: add_pbucket(mod,bucket,g);
1603: }
1604: } else if ( !full ) {
1605: g = normalize_pbucket(mod,bucket);
1606: if ( g ) SG(g) = sugar;
1607: *rp = g;
1608: return 1;
1609: } else {
1610: m = BDY(g);
1611: if ( NEXT(m) ) {
1612: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1613: } else {
1614: FREEND(g); g = 0;
1615: }
1616: bucket->body[hindex] = g;
1617: NEXT(m) = 0;
1618: if ( d ) {
1619: NEXT(tail)=m; tail=m; LEN(d)++;
1620: } else {
1621: MKND(n,m,1,d); tail = BDY(d);
1622: }
1623: }
1624: }
1625: }
1626:
1627: /* input : list of NDV, cand : list of NDV */
1628:
1629: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1630: {
1631: int n,i,stat;
1632: ND nf,d;
1633: NDV r;
1634: NODE t,s;
1635: union oNDC dn;
1636: Z q;
1637: LIST list;
1638:
1639: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1640: n = length(cand);
1641:
1642: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1643: /* membercheck : list is a subset of Id(cand) ? */
1644: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1645: again:
1646: nd_tracelist = 0;
1647: if ( nd_bpe > obpe )
1648: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1649: else
1650: r = (NDV)BDY(t);
1651: #if 0
1652: // moved to nd_f4_lf_trace()
1653: if ( m == -2 ) ndv_mod(m,r);
1654: #endif
1655: d = ndvtond(m,r);
1.6 noro 1656: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1657: if ( !stat ) {
1658: nd_reconstruct(0,0);
1659: goto again;
1660: } else if ( nf ) return 0;
1661: if ( nd_gentrace ) {
1662: nd_tracelist = reverse_node(nd_tracelist);
1663: MKLIST(list,nd_tracelist);
1.6 noro 1664: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1665: MKNODE(s,list,nd_alltracelist);
1666: nd_alltracelist = s; nd_tracelist = 0;
1667: }
1668: if ( DP_Print ) { printf("."); fflush(stdout); }
1669: }
1670: if ( DP_Print ) { printf("\n"); }
1671: return 1;
1672: }
1673:
1674: ND nd_remove_head(ND p)
1675: {
1676: NM m;
1677:
1678: m = BDY(p);
1679: if ( !NEXT(m) ) {
1680: FREEND(p); p = 0;
1681: } else {
1682: BDY(p) = NEXT(m); LEN(p)--;
1683: }
1684: FREENM(m);
1685: return p;
1686: }
1687:
1688: ND nd_separate_head(ND p,ND *head)
1689: {
1690: NM m,m0;
1691: ND r;
1692:
1693: m = BDY(p);
1694: if ( !NEXT(m) ) {
1695: *head = p; p = 0;
1696: } else {
1697: m0 = m;
1698: BDY(p) = NEXT(m); LEN(p)--;
1699: NEXT(m0) = 0;
1700: MKND(NV(p),m0,1,r);
1701: *head = r;
1702: }
1703: return p;
1704: }
1705:
1706: PGeoBucket create_pbucket()
1707: {
1708: PGeoBucket g;
1709:
1710: g = CALLOC(1,sizeof(struct oPGeoBucket));
1711: g->m = -1;
1712: return g;
1713: }
1714:
1715: void free_pbucket(PGeoBucket b) {
1716: int i;
1717:
1718: for ( i = 0; i <= b->m; i++ )
1719: if ( b->body[i] ) {
1720: nd_free(b->body[i]);
1721: b->body[i] = 0;
1722: }
1723: GCFREE(b);
1724: }
1725:
1726: void add_pbucket_symbolic(PGeoBucket g,ND d)
1727: {
1728: int l,i,k,m;
1729:
1730: if ( !d )
1731: return;
1732: l = LEN(d);
1733: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1734: /* 2^(k-1) < l <= 2^k (=m) */
1735: d = nd_merge(g->body[k],d);
1736: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1737: g->body[k] = 0;
1738: d = nd_merge(g->body[k+1],d);
1739: }
1740: g->body[k] = d;
1741: g->m = MAX(g->m,k);
1742: }
1743:
1744: void add_pbucket(int mod,PGeoBucket g,ND d)
1745: {
1746: int l,i,k,m;
1747:
1748: if ( !d )
1749: return;
1750: l = LEN(d);
1751: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1752: /* 2^(k-1) < l <= 2^k (=m) */
1753: d = nd_add(mod,g->body[k],d);
1754: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1755: g->body[k] = 0;
1756: d = nd_add(mod,g->body[k+1],d);
1757: }
1758: g->body[k] = d;
1759: g->m = MAX(g->m,k);
1760: }
1761:
1762: void mulq_pbucket(PGeoBucket g,Z c)
1763: {
1764: int k;
1765:
1766: for ( k = 0; k <= g->m; k++ )
1767: nd_mul_c_q(g->body[k],(P)c);
1768: }
1769:
1770: NM remove_head_pbucket_symbolic(PGeoBucket g)
1771: {
1772: int j,i,k,c;
1773: NM head;
1774:
1775: k = g->m;
1776: j = -1;
1777: for ( i = 0; i <= k; i++ ) {
1778: if ( !g->body[i] ) continue;
1779: if ( j < 0 ) j = i;
1780: else {
1781: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1782: if ( c > 0 )
1783: j = i;
1784: else if ( c == 0 )
1785: g->body[i] = nd_remove_head(g->body[i]);
1786: }
1787: }
1788: if ( j < 0 ) return 0;
1789: else {
1790: head = BDY(g->body[j]);
1791: if ( !NEXT(head) ) {
1792: FREEND(g->body[j]);
1793: g->body[j] = 0;
1794: } else {
1795: BDY(g->body[j]) = NEXT(head);
1796: LEN(g->body[j])--;
1797: }
1798: return head;
1799: }
1800: }
1801:
1802: int head_pbucket(int mod,PGeoBucket g)
1803: {
1804: int j,i,c,k,nv,sum;
1805: UINT *di,*dj;
1806: ND gi,gj;
1807:
1808: k = g->m;
1809: while ( 1 ) {
1810: j = -1;
1811: for ( i = 0; i <= k; i++ ) {
1812: if ( !(gi = g->body[i]) )
1813: continue;
1814: if ( j < 0 ) {
1815: j = i;
1816: gj = g->body[j];
1817: dj = HDL(gj);
1818: sum = HCM(gj);
1819: } else {
1820: c = DL_COMPARE(HDL(gi),dj);
1821: if ( c > 0 ) {
1822: if ( sum ) HCM(gj) = sum;
1823: else g->body[j] = nd_remove_head(gj);
1824: j = i;
1825: gj = g->body[j];
1826: dj = HDL(gj);
1827: sum = HCM(gj);
1828: } else if ( c == 0 ) {
1829: if ( mod == -1 )
1830: sum = _addsf(sum,HCM(gi));
1831: else {
1832: sum = sum+HCM(gi)-mod;
1833: if ( sum < 0 ) sum += mod;
1834: }
1835: g->body[i] = nd_remove_head(gi);
1836: }
1837: }
1838: }
1839: if ( j < 0 ) return -1;
1840: else if ( sum ) {
1841: HCM(gj) = sum;
1842: return j;
1843: } else
1844: g->body[j] = nd_remove_head(gj);
1845: }
1846: }
1847:
1848: int head_pbucket_q(PGeoBucket g)
1849: {
1850: int j,i,c,k,nv;
1851: Z sum,t;
1852: ND gi,gj;
1853:
1854: k = g->m;
1855: while ( 1 ) {
1856: j = -1;
1857: for ( i = 0; i <= k; i++ ) {
1858: if ( !(gi = g->body[i]) ) continue;
1859: if ( j < 0 ) {
1860: j = i;
1861: gj = g->body[j];
1.6 noro 1862: sum = HCZ(gj);
1.1 noro 1863: } else {
1864: nv = NV(gi);
1865: c = DL_COMPARE(HDL(gi),HDL(gj));
1866: if ( c > 0 ) {
1.6 noro 1867: if ( sum ) HCZ(gj) = sum;
1.1 noro 1868: else g->body[j] = nd_remove_head(gj);
1869: j = i;
1870: gj = g->body[j];
1.6 noro 1871: sum = HCZ(gj);
1.1 noro 1872: } else if ( c == 0 ) {
1.6 noro 1873: addz(sum,HCZ(gi),&t);
1.1 noro 1874: sum = t;
1875: g->body[i] = nd_remove_head(gi);
1876: }
1877: }
1878: }
1879: if ( j < 0 ) return -1;
1880: else if ( sum ) {
1.6 noro 1881: HCZ(gj) = sum;
1.1 noro 1882: return j;
1883: } else
1884: g->body[j] = nd_remove_head(gj);
1885: }
1886: }
1887:
1888: int head_pbucket_lf(PGeoBucket g)
1889: {
1890: int j,i,c,k,nv;
1891: Z sum,t;
1892: ND gi,gj;
1893:
1894: k = g->m;
1895: while ( 1 ) {
1896: j = -1;
1897: for ( i = 0; i <= k; i++ ) {
1898: if ( !(gi = g->body[i]) ) continue;
1899: if ( j < 0 ) {
1900: j = i;
1901: gj = g->body[j];
1902: sum = HCZ(gj);
1903: } else {
1904: nv = NV(gi);
1905: c = DL_COMPARE(HDL(gi),HDL(gj));
1906: if ( c > 0 ) {
1907: if ( sum ) HCZ(gj) = sum;
1908: else g->body[j] = nd_remove_head(gj);
1909: j = i;
1910: gj = g->body[j];
1911: sum = HCZ(gj);
1912: } else if ( c == 0 ) {
1913: addlf(sum,HCZ(gi),&t);
1914: sum = t;
1915: g->body[i] = nd_remove_head(gi);
1916: }
1917: }
1918: }
1919: if ( j < 0 ) return -1;
1920: else if ( sum ) {
1921: HCZ(gj) = sum;
1922: return j;
1923: } else
1924: g->body[j] = nd_remove_head(gj);
1925: }
1926: }
1927:
1928: ND normalize_pbucket(int mod,PGeoBucket g)
1929: {
1930: int i;
1931: ND r,t;
1932:
1933: r = 0;
1934: for ( i = 0; i <= g->m; i++ ) {
1935: r = nd_add(mod,r,g->body[i]);
1936: g->body[i] = 0;
1937: }
1938: g->m = -1;
1939: return r;
1940: }
1941:
1942: #if 0
1943: void register_hcf(NDV p)
1944: {
1945: DCP dc,t;
1946: P hc,h;
1947: int c;
1948: NODE l,l1,prev;
1949:
1950: hc = p->body->c.p;
1951: if ( !nd_vc || NUM(hc) ) return;
1952: fctrp(nd_vc,hc,&dc);
1953: for ( t = dc; t; t = NEXT(t) ) {
1954: h = t->c;
1955: if ( NUM(h) ) continue;
1956: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1957: c = compp(nd_vc,h,(P)BDY(l));
1958: if ( c >= 0 ) break;
1959: }
1960: if ( !l || c > 0 ) {
1961: MKNODE(l1,h,l);
1962: if ( !prev )
1963: nd_hcf = l1;
1964: else
1965: NEXT(prev) = l1;
1966: }
1967: }
1968: }
1969: #else
1970: void register_hcf(NDV p)
1971: {
1972: DCP dc,t;
1973: P hc,h,q;
1974: Q dmy;
1975: int c;
1976: NODE l,l1,prev;
1977:
1978: hc = p->body->c.p;
1979: if ( NUM(hc) ) return;
1980: ptozp(hc,1,&dmy,&h);
1981: #if 1
1982: for ( l = nd_hcf; l; l = NEXT(l) ) {
1983: while ( 1 ) {
1984: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
1985: else break;
1986: }
1987: }
1988: if ( NUM(h) ) return;
1989: #endif
1990: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1991: c = compp(nd_vc,h,(P)BDY(l));
1992: if ( c >= 0 ) break;
1993: }
1994: if ( !l || c > 0 ) {
1995: MKNODE(l1,h,l);
1996: if ( !prev )
1997: nd_hcf = l1;
1998: else
1999: NEXT(prev) = l1;
2000: }
2001: }
2002: #endif
2003:
2004: int do_diagonalize(int sugar,int m)
2005: {
1.6 noro 2006: int i,nh,stat;
2007: NODE r,g,t;
2008: ND h,nf,s,head;
2009: NDV nfv;
2010: Q q;
2011: P nm,nmp,dn,mnp,dnp,cont,cont1;
2012: union oNDC hc;
2013: NODE node;
2014: LIST l;
2015: Z iq;
1.1 noro 2016:
1.6 noro 2017: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2018: if ( nd_gentrace ) {
2019: /* Trace = [1,index,1,1] */
2020: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2021: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2022: }
2023: if ( nd_demand )
2024: nfv = ndv_load(i);
2025: else
2026: nfv = nd_ps[i];
2027: s = ndvtond(m,nfv);
2028: s = nd_separate_head(s,&head);
2029: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2030: if ( !stat ) return 0;
2031: ndv_free(nfv);
2032: hc = HCU(nf); nd_removecont(m,nf);
2033: /* exact division */
2034: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2035: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2036: nfv = ndtondv(m,nf);
2037: nd_free(nf);
2038: nd_bound[i] = ndv_compute_bound(nfv);
2039: if ( !m ) register_hcf(nfv);
2040: if ( nd_demand ) {
2041: ndv_save(nfv,i);
2042: ndv_free(nfv);
2043: } else
2044: nd_ps[i] = nfv;
2045: }
2046: return 1;
1.1 noro 2047: }
2048:
2049: LIST compute_splist()
2050: {
2051: NODE g,tn0,tn,node;
2052: LIST l0;
2053: ND_pairs d,t;
2054: int i;
2055: Z i1,i2;
2056:
2057: g = 0; d = 0;
2058: for ( i = 0; i < nd_psn; i++ ) {
2059: d = update_pairs(d,g,i,0);
2060: g = update_base(g,i);
2061: }
2062: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2063: NEXTNODE(tn0,tn);
1.6 noro 2064: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2065: node = mknode(2,i1,i2); MKLIST(l0,node);
2066: BDY(tn) = l0;
2067: }
2068: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2069: return l0;
2070: }
2071:
2072: /* return value = 0 => input is not a GB */
2073:
2074: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2075: {
1.6 noro 2076: int i,nh,sugar,stat;
2077: NODE r,g,t;
2078: ND_pairs d;
2079: ND_pairs l;
2080: ND h,nf,s,head,nf1;
2081: NDV nfv;
2082: Z q;
2083: union oNDC dn,hc;
2084: int diag_count = 0;
2085: P cont;
2086: LIST list;
2087:
1.11 noro 2088: Nnd_add = 0;
1.6 noro 2089: g = 0; d = 0;
2090: for ( i = 0; i < nd_psn; i++ ) {
2091: d = update_pairs(d,g,i,gensyz);
2092: g = update_base(g,i);
2093: }
2094: sugar = 0;
2095: while ( d ) {
1.1 noro 2096: again:
1.6 noro 2097: l = nd_minp(d,&d);
2098: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2099: if ( SG(l) != sugar ) {
2100: if ( ishomo ) {
2101: diag_count = 0;
2102: stat = do_diagonalize(sugar,m);
1.1 noro 2103: if ( !stat ) {
1.6 noro 2104: NEXT(l) = d; d = l;
2105: d = nd_reconstruct(0,d);
2106: goto again;
1.1 noro 2107: }
1.6 noro 2108: }
2109: sugar = SG(l);
2110: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2111: }
2112: stat = nd_sp(m,0,l,&h);
2113: if ( !stat ) {
2114: NEXT(l) = d; d = l;
2115: d = nd_reconstruct(0,d);
2116: goto again;
2117: }
1.1 noro 2118: #if USE_GEOBUCKET
1.6 noro 2119: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2120: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2121: #else
1.6 noro 2122: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2123: #endif
1.6 noro 2124: if ( !stat ) {
2125: NEXT(l) = d; d = l;
2126: d = nd_reconstruct(0,d);
2127: goto again;
2128: } else if ( nf ) {
2129: if ( checkonly || gensyz ) return 0;
1.1 noro 2130: if ( nd_newelim ) {
2131: if ( nd_module ) {
2132: if ( MPOS(HDL(nf)) > 1 ) return 0;
2133: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2134: }
1.6 noro 2135: if ( DP_Print ) { printf("+"); fflush(stdout); }
2136: hc = HCU(nf);
2137: nd_removecont(m,nf);
2138: if ( !m && nd_nalg ) {
2139: nd_monic(0,&nf);
2140: nd_removecont(m,nf);
2141: }
2142: if ( nd_gentrace ) {
2143: /* exact division */
1.1 noro 2144: cont = ndc_div(m,hc,HCU(nf));
2145: if ( m || !UNIQ(cont) ) {
1.6 noro 2146: t = mknode(4,NULLP,NULLP,NULLP,cont);
2147: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2148: nd_tracelist = t;
2149: }
2150: }
1.6 noro 2151: nfv = ndtondv(m,nf); nd_free(nf);
2152: nh = ndv_newps(m,nfv,0,0);
2153: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2154: diag_count = 0;
2155: stat = do_diagonalize(sugar,m);
2156: if ( !stat ) {
2157: NEXT(l) = d; d = l;
2158: d = nd_reconstruct(1,d);
2159: goto again;
1.1 noro 2160: }
1.6 noro 2161: }
2162: d = update_pairs(d,g,nh,0);
2163: g = update_base(g,nh);
2164: FREENDP(l);
2165: } else {
2166: if ( nd_gentrace && gensyz ) {
2167: nd_tracelist = reverse_node(nd_tracelist);
2168: MKLIST(list,nd_tracelist);
2169: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2170: MKNODE(t,list,nd_alltracelist);
2171: nd_alltracelist = t; nd_tracelist = 0;
2172: }
2173: if ( DP_Print ) { printf("."); fflush(stdout); }
2174: FREENDP(l);
2175: }
2176: }
2177: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2178: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.1 noro 2179: return g;
2180: }
2181:
2182: /* splist = [[i1,i2],...] */
2183:
2184: int check_splist(int m,NODE splist)
2185: {
2186: NODE t,p;
2187: ND_pairs d,r,l;
2188: int stat;
2189: ND h,nf;
2190:
2191: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2192: p = BDY((LIST)BDY(t));
1.6 noro 2193: NEXTND_pairs(d,r);
2194: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2195: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2196: SG(r) = TD(LCM(r)); /* XXX */
2197: }
2198: if ( d ) NEXT(r) = 0;
2199:
1.6 noro 2200: while ( d ) {
1.1 noro 2201: again:
1.6 noro 2202: l = nd_minp(d,&d);
2203: stat = nd_sp(m,0,l,&h);
2204: if ( !stat ) {
2205: NEXT(l) = d; d = l;
2206: d = nd_reconstruct(0,d);
2207: goto again;
2208: }
2209: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2210: if ( !stat ) {
2211: NEXT(l) = d; d = l;
2212: d = nd_reconstruct(0,d);
2213: goto again;
2214: } else if ( nf ) return 0;
1.1 noro 2215: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2216: }
1.1 noro 2217: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2218: return 1;
2219: }
2220:
2221: int check_splist_f4(int m,NODE splist)
2222: {
2223: UINT *s0vect;
1.6 noro 2224: PGeoBucket bucket;
1.1 noro 2225: NODE p,rp0,t;
2226: ND_pairs d,r,l,ll;
2227: int col,stat;
2228:
2229: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2230: p = BDY((LIST)BDY(t));
1.6 noro 2231: NEXTND_pairs(d,r);
2232: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2233: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2234: SG(r) = TD(LCM(r)); /* XXX */
2235: }
2236: if ( d ) NEXT(r) = 0;
2237:
1.6 noro 2238: while ( d ) {
2239: l = nd_minsugarp(d,&d);
2240: bucket = create_pbucket();
2241: stat = nd_sp_f4(m,0,l,bucket);
2242: if ( !stat ) {
2243: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2244: NEXT(ll) = d; d = l;
2245: d = nd_reconstruct(0,d);
2246: continue;
2247: }
2248: if ( bucket->m < 0 ) continue;
2249: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2250: if ( !col ) {
2251: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2252: NEXT(ll) = d; d = l;
2253: d = nd_reconstruct(0,d);
2254: continue;
1.1 noro 2255: }
1.6 noro 2256: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2257: }
2258: return 1;
1.1 noro 2259: }
2260:
2261: int do_diagonalize_trace(int sugar,int m)
2262: {
1.6 noro 2263: int i,nh,stat;
2264: NODE r,g,t;
2265: ND h,nf,nfq,s,head;
2266: NDV nfv,nfqv;
2267: Q q,den,num;
2268: union oNDC hc;
2269: NODE node;
2270: LIST l;
2271: Z iq;
2272: P cont,cont1;
1.1 noro 2273:
1.6 noro 2274: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2275: if ( nd_gentrace ) {
2276: /* Trace = [1,index,1,1] */
2277: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2278: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2279: }
2280: /* for nd_ps */
2281: s = ndvtond(m,nd_ps[i]);
2282: s = nd_separate_head(s,&head);
2283: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2284: if ( !stat ) return 0;
2285: nf = nd_add(m,head,nf);
2286: ndv_free(nd_ps[i]);
2287: nd_ps[i] = ndtondv(m,nf);
2288: nd_free(nf);
2289:
2290: /* for nd_ps_trace */
2291: if ( nd_demand )
2292: nfv = ndv_load(i);
2293: else
2294: nfv = nd_ps_trace[i];
2295: s = ndvtond(0,nfv);
2296: s = nd_separate_head(s,&head);
2297: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2298: if ( !stat ) return 0;
2299: ndv_free(nfv);
2300: hc = HCU(nf); nd_removecont(0,nf);
2301: /* exact division */
1.1 noro 2302: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2303: if ( nd_gentrace ) finalize_tracelist(i,cont);
2304: nfv = ndtondv(0,nf);
2305: nd_free(nf);
2306: nd_bound[i] = ndv_compute_bound(nfv);
2307: register_hcf(nfv);
2308: if ( nd_demand ) {
2309: ndv_save(nfv,i);
2310: ndv_free(nfv);
2311: } else
2312: nd_ps_trace[i] = nfv;
2313: }
2314: return 1;
1.1 noro 2315: }
2316:
2317: static struct oEGT eg_invdalg;
2318: struct oEGT eg_le;
2319:
2320: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2321: {
2322: NODE tn;
2323: P p1;
2324:
2325: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2326: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2327: }
2328: *r = p;
2329: }
2330:
2331: NODE nd_gb_trace(int m,int ishomo,int **indp)
2332: {
1.6 noro 2333: int i,nh,sugar,stat;
2334: NODE r,g,t;
2335: ND_pairs d;
2336: ND_pairs l;
2337: ND h,nf,nfq,s,head;
2338: NDV nfv,nfqv;
2339: Z q,den,num;
2340: P hc;
2341: union oNDC dn,hnfq;
2342: struct oEGT eg_monic,egm0,egm1;
2343: int diag_count = 0;
2344: P cont;
2345: LIST list;
2346:
2347: init_eg(&eg_monic);
2348: init_eg(&eg_invdalg);
2349: init_eg(&eg_le);
2350: g = 0; d = 0;
2351: for ( i = 0; i < nd_psn; i++ ) {
2352: d = update_pairs(d,g,i,0);
2353: g = update_base(g,i);
2354: }
2355: sugar = 0;
2356: while ( d ) {
1.1 noro 2357: again:
1.6 noro 2358: l = nd_minp(d,&d);
2359: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2360: if ( SG(l) != sugar ) {
1.1 noro 2361: #if 1
1.6 noro 2362: if ( ishomo ) {
2363: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2364: stat = do_diagonalize_trace(sugar,m);
2365: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2366: diag_count = 0;
1.1 noro 2367: if ( !stat ) {
1.6 noro 2368: NEXT(l) = d; d = l;
2369: d = nd_reconstruct(1,d);
2370: goto again;
1.1 noro 2371: }
1.6 noro 2372: }
2373: #endif
2374: sugar = SG(l);
2375: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2376: }
2377: stat = nd_sp(m,0,l,&h);
2378: if ( !stat ) {
2379: NEXT(l) = d; d = l;
2380: d = nd_reconstruct(1,d);
2381: goto again;
2382: }
1.1 noro 2383: #if USE_GEOBUCKET
1.6 noro 2384: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2385: #else
1.6 noro 2386: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2387: #endif
1.6 noro 2388: if ( !stat ) {
2389: NEXT(l) = d; d = l;
2390: d = nd_reconstruct(1,d);
2391: goto again;
2392: } else if ( nf ) {
2393: if ( nd_demand ) {
2394: nfqv = ndv_load(nd_psn);
2395: nfq = ndvtond(0,nfqv);
2396: } else
2397: nfq = 0;
2398: if ( !nfq ) {
2399: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2400: NEXT(l) = d; d = l;
2401: d = nd_reconstruct(1,d);
2402: goto again;
2403: }
2404: }
2405: if ( nfq ) {
2406: /* m|HC(nfq) => failure */
2407: if ( nd_vc ) {
2408: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2409: } else
2410: q = HCZ(nfq);
2411: if ( !remqi((Q)q,m) ) return 0;
2412:
2413: if ( DP_Print ) { printf("+"); fflush(stdout); }
2414: hnfq = HCU(nfq);
2415: if ( nd_nalg ) {
2416: /* m|DN(HC(nf)^(-1)) => failure */
2417: get_eg(&egm0);
2418: if ( !nd_monic(m,&nfq) ) return 0;
2419: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2420: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2421: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2422: } else {
2423: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2424: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2425: }
2426: if ( nd_gentrace ) {
2427: /* exact division */
2428: cont = ndc_div(0,hnfq,HCU(nfqv));
2429: if ( !UNIQ(cont) ) {
2430: t = mknode(4,NULLP,NULLP,NULLP,cont);
2431: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2432: nd_tracelist = t;
2433: }
2434: }
2435: nh = ndv_newps(0,nfv,nfqv,0);
2436: if ( ishomo && ++diag_count == diag_period ) {
2437: diag_count = 0;
2438: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2439: stat = do_diagonalize_trace(sugar,m);
2440: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2441: if ( !stat ) {
1.1 noro 2442: NEXT(l) = d; d = l;
2443: d = nd_reconstruct(1,d);
2444: goto again;
1.6 noro 2445: }
1.1 noro 2446: }
1.6 noro 2447: d = update_pairs(d,g,nh,0);
2448: g = update_base(g,nh);
2449: } else {
2450: if ( DP_Print ) { printf("*"); fflush(stdout); }
2451: }
2452: } else {
2453: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2454: }
1.6 noro 2455: FREENDP(l);
2456: }
2457: if ( nd_nalg ) {
2458: if ( DP_Print ) {
2459: print_eg("monic",&eg_monic);
2460: print_eg("invdalg",&eg_invdalg);
2461: print_eg("le",&eg_le);
1.1 noro 2462: }
1.6 noro 2463: }
1.1 noro 2464: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2465: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2466: return g;
1.1 noro 2467: }
2468:
2469: int ndv_compare(NDV *p1,NDV *p2)
2470: {
2471: return DL_COMPARE(HDL(*p1),HDL(*p2));
2472: }
2473:
2474: int ndv_compare_rev(NDV *p1,NDV *p2)
2475: {
2476: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2477: }
2478:
2479: int ndvi_compare(NDVI p1,NDVI p2)
2480: {
2481: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2482: }
2483:
2484: int ndvi_compare_rev(NDVI p1,NDVI p2)
2485: {
2486: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2487: }
2488:
2489: NODE ndv_reduceall(int m,NODE f)
2490: {
2491: int i,j,n,stat;
2492: ND nf,g,head;
2493: NODE t,a0,a;
2494: union oNDC dn;
2495: Q q,num,den;
2496: NODE node;
2497: LIST l;
2498: Z iq,jq;
2499: int *perm;
2500: union oNDC hc;
2501: P cont,cont1;
2502:
2503: if ( nd_nora ) return f;
2504: n = length(f);
2505: ndv_setup(m,0,f,0,1);
2506: perm = (int *)MALLOC(n*sizeof(int));
2507: if ( nd_gentrace ) {
2508: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2509: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2510: }
2511: for ( i = 0; i < n; ) {
2512: if ( nd_gentrace ) {
2513: /* Trace = [1,index,1,1] */
1.6 noro 2514: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2515: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2516: }
2517: g = ndvtond(m,nd_ps[i]);
2518: g = nd_separate_head(g,&head);
1.6 noro 2519: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 2520: if ( !stat )
2521: nd_reconstruct(0,0);
2522: else {
2523: if ( DP_Print ) { printf("."); fflush(stdout); }
2524: ndv_free(nd_ps[i]);
2525: hc = HCU(nf); nd_removecont(m,nf);
2526: if ( nd_gentrace ) {
2527: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 2528: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
2529: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 2530: }
1.6 noro 2531: /* exact division */
1.1 noro 2532: cont = ndc_div(m,hc,HCU(nf));
2533: finalize_tracelist(perm[i],cont);
2534: }
2535: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2536: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2537: i++;
2538: }
2539: }
2540: if ( DP_Print ) { printf("\n"); }
2541: for ( a0 = 0, i = 0; i < n; i++ ) {
2542: NEXTNODE(a0,a);
2543: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2544: else {
2545: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2546: BDY(a) = (pointer)nd_ps[j];
2547: }
2548: }
2549: NEXT(a) = 0;
2550: return a0;
2551: }
2552:
2553: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
2554: {
2555: ND_pairs d1,nd,cur,head,prev,remove;
2556:
2557: if ( !g ) return d;
2558: /* for testing */
2559: if ( gensyz && nd_gensyz == 2 ) {
2560: d1 = nd_newpairs(g,t);
2561: if ( !d )
2562: return d1;
2563: else {
2564: nd = d;
2565: while ( NEXT(nd) ) nd = NEXT(nd);
2566: NEXT(nd) = d1;
2567: return d;
2568: }
2569: }
2570: d = crit_B(d,t);
2571: d1 = nd_newpairs(g,t);
2572: d1 = crit_M(d1);
2573: d1 = crit_F(d1);
2574: if ( gensyz || do_weyl )
2575: head = d1;
2576: else {
2577: prev = 0; cur = head = d1;
2578: while ( cur ) {
2579: if ( crit_2( cur->i1,cur->i2 ) ) {
2580: remove = cur;
2581: if ( !prev ) head = cur = NEXT(cur);
2582: else cur = NEXT(prev) = NEXT(cur);
2583: FREENDP(remove);
2584: } else {
2585: prev = cur; cur = NEXT(cur);
2586: }
2587: }
2588: }
2589: if ( !d )
2590: return head;
2591: else {
2592: nd = d;
2593: while ( NEXT(nd) ) nd = NEXT(nd);
2594: NEXT(nd) = head;
2595: return d;
2596: }
2597: }
2598:
2599:
2600: ND_pairs nd_newpairs( NODE g, int t )
2601: {
2602: NODE h;
2603: UINT *dl;
2604: int ts,s,i,t0,min,max;
2605: ND_pairs r,r0;
2606:
2607: dl = DL(nd_psh[t]);
2608: ts = SG(nd_psh[t]) - TD(dl);
2609: if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
2610: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
2611: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
2612: continue;
2613: if ( nd_gbblock ) {
2614: t0 = (long)BDY(h);
2615: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2616: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2617: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2618: break;
2619: }
2620: if ( nd_gbblock[i] >= 0 )
2621: continue;
2622: }
2623: NEXTND_pairs(r0,r);
2624: r->i1 = (long)BDY(h);
2625: r->i2 = t;
2626: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2627: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2628: SG(r) = MAX(s,ts) + TD(LCM(r));
2629: /* experimental */
2630: if ( nd_sugarweight )
2631: r->sugar2 = ndl_weight2(r->lcm);
2632: }
2633: if ( r0 ) NEXT(r) = 0;
2634: return r0;
2635: }
2636:
2637: /* ipair = [i1,i2],[i1,i2],... */
2638: ND_pairs nd_ipairtospair(NODE ipair)
2639: {
2640: int s1,s2;
2641: NODE tn,t;
2642: ND_pairs r,r0;
2643:
2644: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2645: NEXTND_pairs(r0,r);
2646: tn = BDY((LIST)BDY(t));
1.6 noro 2647: r->i1 = ZTOS((Q)ARG0(tn));
2648: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 2649: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2650: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2651: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2652: SG(r) = MAX(s1,s2) + TD(LCM(r));
2653: /* experimental */
2654: if ( nd_sugarweight )
2655: r->sugar2 = ndl_weight2(r->lcm);
2656: }
2657: if ( r0 ) NEXT(r) = 0;
2658: return r0;
2659: }
2660:
2661: /* kokokara */
2662:
2663: ND_pairs crit_B( ND_pairs d, int s )
2664: {
2665: ND_pairs cur,head,prev,remove;
2666: UINT *t,*tl,*lcm;
2667: int td,tdl;
2668:
2669: if ( !d ) return 0;
2670: t = DL(nd_psh[s]);
2671: prev = 0;
2672: head = cur = d;
2673: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
2674: while ( cur ) {
2675: tl = cur->lcm;
2676: if ( ndl_reducible(tl,t) ) {
2677: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
2678: if ( !ndl_equal(lcm,tl) ) {
2679: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2680: if (!ndl_equal(lcm,tl)) {
2681: remove = cur;
2682: if ( !prev ) {
2683: head = cur = NEXT(cur);
2684: } else {
2685: cur = NEXT(prev) = NEXT(cur);
2686: }
2687: FREENDP(remove);
2688: } else {
2689: prev = cur; cur = NEXT(cur);
2690: }
2691: } else {
2692: prev = cur; cur = NEXT(cur);
2693: }
2694: } else {
2695: prev = cur; cur = NEXT(cur);
2696: }
2697: }
2698: return head;
2699: }
2700:
2701: ND_pairs crit_M( ND_pairs d1 )
2702: {
2703: ND_pairs e,d2,d3,dd,p;
2704: UINT *id,*jd;
2705:
2706: if ( !d1 ) return d1;
2707: for ( dd = 0, e = d1; e; e = d3 ) {
2708: if ( !(d2 = NEXT(e)) ) {
2709: NEXT(e) = dd;
2710: return e;
2711: }
2712: id = LCM(e);
2713: for ( d3 = 0; d2; d2 = p ) {
2714: p = NEXT(d2);
2715: jd = LCM(d2);
2716: if ( ndl_equal(jd,id) )
2717: ;
2718: else if ( TD(jd) > TD(id) )
2719: if ( ndl_reducible(jd,id) ) continue;
2720: else ;
2721: else if ( ndl_reducible(id,jd) ) goto delit;
2722: NEXT(d2) = d3;
2723: d3 = d2;
2724: }
2725: NEXT(e) = dd;
2726: dd = e;
2727: continue;
2728: /**/
2729: delit: NEXT(d2) = d3;
2730: d3 = d2;
2731: for ( ; p; p = d2 ) {
2732: d2 = NEXT(p);
2733: NEXT(p) = d3;
2734: d3 = p;
2735: }
2736: FREENDP(e);
2737: }
2738: return dd;
2739: }
2740:
2741: ND_pairs crit_F( ND_pairs d1 )
2742: {
2743: ND_pairs rest, head,remove;
2744: ND_pairs last, p, r, w;
2745: int s;
2746:
2747: if ( !d1 ) return d1;
2748: for ( head = last = 0, p = d1; NEXT(p); ) {
2749: r = w = equivalent_pairs(p,&rest);
2750: s = SG(r);
2751: w = NEXT(w);
2752: while ( w ) {
2753: if ( crit_2(w->i1,w->i2) ) {
2754: r = w;
2755: w = NEXT(w);
2756: while ( w ) {
2757: remove = w;
2758: w = NEXT(w);
2759: FREENDP(remove);
2760: }
2761: break;
2762: } else if ( SG(w) < s ) {
2763: FREENDP(r);
2764: r = w;
2765: s = SG(r);
2766: w = NEXT(w);
2767: } else {
2768: remove = w;
2769: w = NEXT(w);
2770: FREENDP(remove);
2771: }
2772: }
2773: if ( last ) NEXT(last) = r;
2774: else head = r;
2775: NEXT(last = r) = 0;
2776: p = rest;
2777: if ( !p ) return head;
2778: }
2779: if ( !last ) return p;
2780: NEXT(last) = p;
2781: return head;
2782: }
2783:
2784: int crit_2( int dp1, int dp2 )
2785: {
2786: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
2787: }
2788:
2789: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
2790: {
2791: ND_pairs w,p,r,s;
2792: UINT *d;
2793:
2794: w = d1;
2795: d = LCM(w);
2796: s = NEXT(w);
2797: NEXT(w) = 0;
2798: for ( r = 0; s; s = p ) {
2799: p = NEXT(s);
2800: if ( ndl_equal(d,LCM(s)) ) {
2801: NEXT(s) = w; w = s;
2802: } else {
2803: NEXT(s) = r; r = s;
2804: }
2805: }
2806: *prest = r;
2807: return w;
2808: }
2809:
2810: NODE update_base(NODE nd,int ndp)
2811: {
2812: UINT *dl, *dln;
2813: NODE last, p, head;
2814:
2815: dl = DL(nd_psh[ndp]);
2816: for ( head = last = 0, p = nd; p; ) {
2817: dln = DL(nd_psh[(long)BDY(p)]);
2818: if ( ndl_reducible( dln, dl ) ) {
2819: p = NEXT(p);
2820: if ( last ) NEXT(last) = p;
2821: } else {
2822: if ( !last ) head = p;
2823: p = NEXT(last = p);
2824: }
2825: }
2826: head = append_one(head,ndp);
2827: return head;
2828: }
2829:
2830: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2831: {
2832: ND_pairs m,ml,p,l;
2833: UINT *lcm;
2834: int s,td,len,tlen,c,c1;
2835:
2836: if ( !(p = NEXT(m = d)) ) {
2837: *prest = p;
2838: NEXT(m) = 0;
2839: return m;
2840: }
2841: if ( !NoSugar ) {
2842: if ( nd_sugarweight ) {
2843: s = m->sugar2;
2844: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2845: if ( (p->sugar2 < s)
2846: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2847: ml = l; m = p; s = m->sugar2;
2848: }
2849: } else {
2850: s = SG(m);
2851: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2852: if ( (SG(p) < s)
2853: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2854: ml = l; m = p; s = SG(m);
2855: }
2856: }
2857: } else {
2858: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2859: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2860: ml = l; m = p; s = SG(m);
2861: }
2862: }
2863: if ( !ml ) *prest = NEXT(m);
2864: else {
2865: NEXT(ml) = NEXT(m);
2866: *prest = d;
2867: }
2868: NEXT(m) = 0;
2869: return m;
2870: }
2871:
2872: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2873: {
2874: int msugar,i;
2875: ND_pairs t,dm0,dm,dr0,dr;
2876:
2877: if ( nd_sugarweight ) {
2878: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2879: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2880: dm0 = 0; dr0 = 0;
2881: for ( i = 0, t = d; t; t = NEXT(t) )
2882: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2883: if ( dm0 ) NEXT(dm) = t;
2884: else dm0 = t;
2885: dm = t;
2886: i++;
2887: } else {
2888: if ( dr0 ) NEXT(dr) = t;
2889: else dr0 = t;
2890: dr = t;
2891: }
2892: } else {
2893: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2894: if ( SG(t) < msugar ) msugar = SG(t);
2895: dm0 = 0; dr0 = 0;
2896: for ( i = 0, t = d; t; t = NEXT(t) )
2897: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2898: if ( dm0 ) NEXT(dm) = t;
2899: else dm0 = t;
2900: dm = t;
2901: i++;
2902: } else {
2903: if ( dr0 ) NEXT(dr) = t;
2904: else dr0 = t;
2905: dr = t;
2906: }
2907: }
2908: NEXT(dm) = 0;
2909: if ( dr0 ) NEXT(dr) = 0;
2910: *prest = dr0;
2911: return dm0;
2912: }
2913:
2914: int nd_tdeg(NDV c)
2915: {
2916: int wmax = 0;
2917: int i,len;
2918: NMV a;
2919:
2920: len = LEN(c);
2921: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2922: wmax = MAX(TD(DL(a)),wmax);
2923: return wmax;
2924: }
2925:
2926: int ndv_newps(int m,NDV a,NDV aq,int f4)
2927: {
2928: int len;
2929: RHist r;
2930: NDV b;
2931: NODE tn;
2932: LIST l;
2933: Z iq;
2934:
2935: if ( nd_psn == nd_pslen ) {
2936: nd_pslen *= 2;
2937: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
2938: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2939: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2940: nd_bound = (UINT **)
2941: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
2942: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2943: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
2944: }
2945: NEWRHist(r); nd_psh[nd_psn] = r;
2946: nd_ps[nd_psn] = a;
2947: if ( aq ) {
2948: nd_ps_trace[nd_psn] = aq;
2949: if ( !m ) {
2950: register_hcf(aq);
2951: } else if ( m == -2 ) {
2952: /* do nothing */
2953: } else
2954: error("ndv_newps : invalud modulus");
2955: nd_bound[nd_psn] = ndv_compute_bound(aq);
2956: #if 1
2957: SG(r) = SG(aq);
2958: #else
2959: SG(r) = nd_tdeg(aq);
2960: #endif
2961: ndl_copy(HDL(aq),DL(r));
2962: } else {
2963: if ( !m ) register_hcf(a);
2964: nd_bound[nd_psn] = ndv_compute_bound(a);
2965: #if 1
2966: SG(r) = SG(a);
2967: #else
2968: SG(r) = nd_tdeg(a);
2969: #endif
2970: ndl_copy(HDL(a),DL(r));
2971: }
2972: if ( nd_demand ) {
2973: if ( aq ) {
2974: ndv_save(nd_ps_trace[nd_psn],nd_psn);
2975: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2976: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2977: nd_ps_trace[nd_psn] = 0;
2978: } else {
2979: ndv_save(nd_ps[nd_psn],nd_psn);
2980: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
2981: nd_ps[nd_psn] = 0;
2982: }
2983: }
2984: if ( nd_gentrace ) {
2985: /* reverse the tracelist and append it to alltracelist */
2986: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 2987: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 2988: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
2989: }
2990: return nd_psn++;
2991: }
2992:
2993: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
2994: /* return 1 if success, 0 if failure (HC(a mod p)) */
2995:
2996: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
2997: {
1.6 noro 2998: int i,j,td,len,max;
2999: NODE s,s0,f0,tn;
3000: UINT *d;
3001: RHist r;
3002: NDVI w;
3003: NDV a,am;
3004: union oNDC hc;
3005: NODE node;
3006: P hcp;
3007: Z iq,jq;
3008: LIST l;
3009:
3010: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3011: /* initialize the tracelist */
3012: nd_tracelist = 0;
3013:
3014: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3015: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3016: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3017: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3018: if ( !dont_sort ) {
3019: /* XXX heuristic */
3020: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
3021: qsort(w,nd_psn,sizeof(struct oNDVI),
3022: (int (*)(const void *,const void *))ndvi_compare_rev);
3023: else
3024: qsort(w,nd_psn,sizeof(struct oNDVI),
3025: (int (*)(const void *,const void *))ndvi_compare);
3026: }
3027: nd_pslen = 2*nd_psn;
3028: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3029: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3030: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3031: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3032: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3033: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3034: nd_hcf = 0;
1.1 noro 3035:
1.6 noro 3036: if ( trace && nd_vc )
3037: makesubst(nd_vc,&nd_subst);
3038: else
3039: nd_subst = 0;
1.1 noro 3040:
1.6 noro 3041: if ( !nd_red )
3042: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3043: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3044: for ( i = 0; i < nd_psn; i++ ) {
3045: hc = HCU(w[i].p);
3046: if ( trace ) {
3047: if ( mod == -2 ) {
3048: /* over a large finite field */
3049: /* trace = small modulus */
3050: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3051: ndv_mod(-2,a);
3052: if ( !dont_removecont) ndv_removecont(-2,a);
3053: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3054: ndv_mod(trace,am);
3055: if ( DL_COMPARE(HDL(am),HDL(a)) )
3056: return 0;
3057: ndv_removecont(trace,am);
3058: } else {
3059: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3060: if ( !dont_removecont) ndv_removecont(0,a);
3061: register_hcf(a);
3062: am = nd_ps[i] = ndv_dup(mod,a);
3063: ndv_mod(mod,am);
3064: if ( DL_COMPARE(HDL(am),HDL(a)) )
3065: return 0;
3066: ndv_removecont(mod,am);
3067: }
3068: } else {
3069: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3070: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3071: if ( !mod ) register_hcf(a);
1.1 noro 3072: }
1.6 noro 3073: if ( nd_gentrace ) {
3074: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3075: /* exact division */
1.1 noro 3076: if ( !dont_removecont )
1.6 noro 3077: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3078: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3079: }
3080: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3081: nd_bound[i] = ndv_compute_bound(a);
3082: nd_psh[i] = r;
3083: if ( nd_demand ) {
3084: if ( trace ) {
3085: ndv_save(nd_ps_trace[i],i);
3086: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3087: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3088: nd_ps_trace[i] = 0;
3089: } else {
3090: ndv_save(nd_ps[i],i);
3091: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3092: nd_ps[i] = 0;
3093: }
1.1 noro 3094: }
1.6 noro 3095: }
3096: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3097: return 1;
1.1 noro 3098: }
3099:
3100: struct order_spec *append_block(struct order_spec *spec,
3101: int nv,int nalg,int ord);
3102:
3103: extern VECT current_dl_weight_vector_obj;
3104: static VECT prev_weight_vector_obj;
3105:
3106: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3107: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3108: {
3109: NODE alist,t,s,r0,r,arg;
3110: VL tv;
3111: P poly;
3112: DP d;
3113: Alg alpha,dp;
3114: DAlg inv,da,hc;
3115: MP m;
3116: int i,nvar,nalg,n;
3117: NumberField nf;
3118: LIST f1,f2;
3119: struct order_spec *current_spec;
3120: VECT obj,obj0;
3121: VECT tmp;
3122:
3123: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3124: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3125:
3126: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3127: NEXTNODE(alist,t); MKV(tv->v,poly);
3128: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3129: tv->v = tv->v->priv;
3130: }
3131: NEXT(t) = 0;
3132:
3133: /* simplification, making polynomials monic */
3134: setfield_dalg(alist);
3135: obj_algtodalg((Obj)f,(Obj *)&f1);
3136: for ( t = BDY(f); t; t = NEXT(t) ) {
3137: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3138: hc = (DAlg)BDY(d)->c;
3139: if ( NID(hc) == N_DA ) {
3140: invdalg(hc,&inv);
3141: for ( m = BDY(d); m; m = NEXT(m) ) {
3142: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3143: }
3144: }
3145: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3146: }
3147: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3148:
3149: /* append alg vars to the var list */
3150: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3151: NEXT(tv) = av;
3152:
3153: /* append a block to ord */
3154: *ord1p = append_block(ord,nvar,nalg,2);
3155:
3156: /* create generator list */
3157: nf = get_numberfield();
3158: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3159: MKAlg(nf->defpoly[i],dp);
3160: MKNODE(s,dp,t); t = s;
3161: }
3162: MKLIST(f1,t);
3163: *alistp = alist;
3164: algobjtorat((Obj)f1,(Obj *)f1p);
3165:
3166: /* creating a new weight vector */
3167: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3168: n = nvar+nalg+1;
3169: MKVECT(obj,n);
3170: if ( obj0 && obj0->len == nvar )
3171: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3172: else
3173: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3174: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3175: BDY(obj)[n-1] = (pointer)ONE;
3176: arg = mknode(1,obj);
3177: Pdp_set_weight(arg,&tmp);
3178: }
3179:
3180: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3181: {
3182: NODE s,t,u0,u;
3183: P p;
3184: VL tv;
3185: Obj obj;
3186: VECT tmp;
3187: NODE arg;
3188:
3189: u0 = 0;
3190: for ( t = r; t; t = NEXT(t) ) {
3191: p = (P)BDY(t);
3192: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3193: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3194: }
3195: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3196: NEXTNODE(u0,u);
3197: BDY(u) = (pointer)p;
3198: }
3199: }
3200: arg = mknode(1,prev_weight_vector_obj);
3201: Pdp_set_weight(arg,&tmp);
3202:
3203: return u0;
3204: }
3205:
3206: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3207: {
3208: VL tv,fv,vv,vc,av;
3209: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3210: int e,max,nvar,i;
3211: NDV b;
3212: int ishomo,nalg,mrank,trank,wmax,len;
3213: NMV a;
3214: Alg alpha,dp;
3215: P p,zp;
3216: Q dmy;
3217: LIST f1,f2,zpl;
3218: Obj obj;
3219: NumberField nf;
3220: struct order_spec *ord1;
3221: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3222: LIST l1,l2,l3,l4,l5;
3223: int j;
3224: Z jq,bpe,last_nonzero;
3225: int *perm;
3226: EPOS oepos;
3227: int obpe,oadv,ompos,cbpe;
1.15 ! noro 3228: VECT hvect;
1.1 noro 3229:
3230: nd_module = 0;
3231: if ( !m && Demand ) nd_demand = 1;
3232: else nd_demand = 0;
3233: parse_nd_option(current_option);
3234:
3235: if ( DP_Multiple )
3236: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3237: #if 0
3238: ndv_alloc = 0;
3239: #endif
3240: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3241: if ( m && nd_vc )
3242: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3243: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3244: switch ( ord->id ) {
3245: case 1:
3246: if ( ord->nv != nvar )
3247: error("nd_{gr,f4} : invalid order specification");
3248: break;
3249: default:
3250: break;
3251: }
3252: nd_nalg = 0;
3253: av = 0;
3254: if ( !m ) {
3255: get_algtree((Obj)f,&av);
3256: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3257: nd_ntrans = nvar;
3258: nd_nalg = nalg;
3259: /* #i -> t#i */
3260: if ( nalg ) {
3261: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3262: ord = ord1;
3263: f = f1;
3264: }
3265: nvar += nalg;
3266: }
3267: nd_init_ord(ord);
3268: mrank = 0;
3269: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3270: for ( tv = vv; tv; tv = NEXT(tv) ) {
3271: if ( nd_module ) {
3272: s = BDY((LIST)BDY(t));
3273: trank = length(s);
3274: mrank = MAX(mrank,trank);
3275: for ( ; s; s = NEXT(s) ) {
3276: e = getdeg(tv->v,(P)BDY(s));
3277: max = MAX(e,max);
3278: }
3279: } else {
3280: e = getdeg(tv->v,(P)BDY(t));
3281: max = MAX(e,max);
3282: }
3283: }
3284: nd_setup_parameters(nvar,nd_nzlist?0:max);
3285: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3286: ishomo = 1;
3287: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3288: if ( nd_module ) {
3289: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3290: else zpl = (LIST)BDY(t);
3291: b = (pointer)pltondv(CO,vv,zpl);
3292: } else {
3293: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3294: else zp = (P)BDY(t);
3295: b = (pointer)ptondv(CO,vv,zp);
3296: }
3297: if ( ishomo )
3298: ishomo = ishomo && ndv_ishomo(b);
3299: if ( m ) ndv_mod(m,b);
3300: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3301: }
3302: if ( fd0 ) NEXT(fd) = 0;
3303:
3304: if ( !ishomo && homo ) {
3305: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3306: b = (NDV)BDY(t); len = LEN(b);
3307: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3308: wmax = MAX(TD(DL(a)),wmax);
3309: }
3310: homogenize_order(ord,nvar,&ord1);
3311: nd_init_ord(ord1);
3312: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
3313: for ( t = fd0; t; t = NEXT(t) )
3314: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3315: }
3316:
3317: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
3318: if ( nd_gentrace ) {
3319: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3320: }
3321: if ( nd_splist ) {
3322: *rp = compute_splist();
3323: return;
3324: }
3325: if ( nd_check_splist ) {
3326: if ( f4 ) {
3327: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3328: else *rp = 0;
3329: } else {
3330: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3331: else *rp = 0;
3332: }
3333: return;
3334: }
3335: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
3336: if ( !x ) {
3337: *rp = 0; return;
3338: }
1.15 ! noro 3339: if ( nd_gentrace ) {
! 3340: MKVECT(hvect,nd_psn);
! 3341: for ( i = 0; i < nd_psn; i++ )
! 3342: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
! 3343: }
1.1 noro 3344: if ( !ishomo && homo ) {
3345: /* dehomogenization */
3346: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3347: nd_init_ord(ord);
3348: nd_setup_parameters(nvar,0);
3349: }
3350: nd_demand = 0;
3351: if ( nd_module && nd_intersect ) {
3352: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
3353: if ( MPOS(DL(nd_psh[j])) > 1 ) {
3354: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
3355: }
3356: conv_ilist(nd_demand,0,x,0);
3357: goto FINAL;
3358: }
3359: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
3360: x = ndv_reducebase(x,perm);
3361: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3362: x = ndv_reduceall(m,x);
3363: cbpe = nd_bpe;
3364: if ( nd_gentrace && !f4 ) {
3365: tl2 = nd_alltracelist; nd_alltracelist = 0;
3366: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
3367: tl3 = nd_alltracelist; nd_alltracelist = 0;
3368: if ( nd_gensyz ) {
3369: nd_gb(m,0,1,1,0);
3370: tl4 = nd_alltracelist; nd_alltracelist = 0;
3371: } else tl4 = 0;
3372: }
3373: nd_bpe = cbpe;
3374: nd_setup_parameters(nd_nvar,0);
3375: FINAL:
3376: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3377: NEXTNODE(r0,r);
3378: if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
3379: else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
3380: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
3381: }
3382: if ( r0 ) NEXT(r) = 0;
3383: if ( !m && nd_nalg )
3384: r0 = postprocess_algcoef(av,alist,r0);
3385: MKLIST(*rp,r0);
3386: if ( nd_gentrace ) {
3387: if ( f4 ) {
1.6 noro 3388: STOZ(16,bpe);
3389: STOZ(nd_last_nonzero,last_nonzero);
1.15 ! noro 3390: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 3391: } else {
3392: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3393: tl3 = reverse_node(tl3);
3394: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3395: for ( t = tl2; t; t = NEXT(t) ) {
3396: /* s = [i,[*,j,*,*],...] */
3397: s = BDY((LIST)BDY(t));
1.6 noro 3398: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3399: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3400: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3401: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3402: }
3403: }
3404: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3405: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3406: }
3407: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3408: MKLIST(l5,tl4);
1.6 noro 3409: STOZ(nd_bpe,bpe);
1.15 ! noro 3410: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3411: }
3412: }
3413: #if 0
3414: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
3415: #endif
3416: }
3417:
3418: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3419: {
3420: VL tv,fv,vv,vc,av;
3421: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3422: int e,max,nvar,i;
3423: NDV b;
3424: int ishomo,nalg;
3425: Alg alpha,dp;
3426: P p,zp;
3427: Q dmy;
3428: LIST f1,f2;
3429: Obj obj;
3430: NumberField nf;
3431: struct order_spec *ord1;
3432: int *perm;
3433:
3434: parse_nd_option(current_option);
3435: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3436: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3437: switch ( ord->id ) {
3438: case 1:
3439: if ( ord->nv != nvar )
3440: error("nd_check : invalid order specification");
3441: break;
3442: default:
3443: break;
3444: }
3445: nd_nalg = 0;
3446: av = 0;
3447: if ( !m ) {
3448: get_algtree((Obj)f,&av);
3449: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3450: nd_ntrans = nvar;
3451: nd_nalg = nalg;
3452: /* #i -> t#i */
3453: if ( nalg ) {
3454: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3455: ord = ord1;
3456: f = f1;
3457: }
3458: nvar += nalg;
3459: }
3460: nd_init_ord(ord);
3461: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3462: for ( tv = vv; tv; tv = NEXT(tv) ) {
3463: e = getdeg(tv->v,(P)BDY(t));
3464: max = MAX(e,max);
3465: }
3466: nd_setup_parameters(nvar,max);
3467: ishomo = 1;
3468: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3469: ptozp((P)BDY(t),1,&dmy,&zp);
3470: b = (pointer)ptondv(CO,vv,zp);
3471: if ( ishomo )
3472: ishomo = ishomo && ndv_ishomo(b);
3473: if ( m ) ndv_mod(m,b);
3474: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3475: }
3476: if ( fd0 ) NEXT(fd) = 0;
3477: ndv_setup(m,0,fd0,0,1);
3478: for ( x = 0, i = 0; i < nd_psn; i++ )
3479: x = update_base(x,i);
3480: if ( do_check ) {
3481: x = nd_gb(m,ishomo,1,0,&perm);
3482: if ( !x ) {
3483: *rp = 0;
3484: return;
3485: }
3486: } else {
3487: #if 0
3488: /* bug ? */
3489: for ( t = x; t; t = NEXT(t) )
3490: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
3491: #else
3492: conv_ilist(0,0,x,&perm);
3493: #endif
3494: }
3495: x = ndv_reducebase(x,perm);
3496: x = ndv_reduceall(m,x);
3497: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3498: NEXTNODE(r0,r);
3499: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3500: }
3501: if ( r0 ) NEXT(r) = 0;
3502: if ( !m && nd_nalg )
3503: r0 = postprocess_algcoef(av,alist,r0);
3504: MKLIST(*rp,r0);
3505: }
3506:
3507: NDV recompute_trace(NODE trace,NDV *p,int m);
3508: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3509:
3510: NDV recompute_trace(NODE ti,NDV *p,int mod)
3511: {
3512: int c,c1,c2,i;
3513: NM mul,m,tail;
3514: ND d,r,rm;
3515: NODE sj;
3516: NDV red;
3517: Obj mj;
3518:
3519: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3520: CM(mul) = 1;
3521: tail = 0;
3522: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3523: sj = BDY((LIST)BDY(ti));
3524: if ( ARG0(sj) ) {
1.6 noro 3525: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 3526: mj = (Obj)ARG2(sj);
3527: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3528: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3529: rm = ndv_mul_nm(mod,mul,red);
3530: if ( !r ) r = rm;
3531: else {
3532: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3533: if ( d ) {
3534: NEXT(tail) = m; tail = m; LEN(d)++;
3535: } else {
3536: MKND(nd_nvar,m,1,d); tail = BDY(d);
3537: }
3538: }
3539: if ( !m ) return 0; /* failure */
3540: else {
3541: BDY(r) = m;
3542: if ( mod > 0 || mod == -1 ) {
3543: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3544: DMAR(c1,c2,0,mod,c);
3545: nd_mul_c(mod,rm,c);
3546: } else {
3547: Z t,u;
3548:
3549: chsgnlf(HCZ(r),&t);
3550: divlf(t,HCZ(rm),&u);
3551: nd_mul_c_lf(rm,u);
3552: }
3553: r = nd_add(mod,r,rm);
3554: }
3555: }
3556: }
3557: }
3558: if ( tail ) NEXT(tail) = 0;
3559: d = nd_add(mod,d,r);
3560: nd_mul_c(mod,d,invm(HCM(d),mod));
3561: return ndtondv(mod,d);
3562: }
3563:
3564: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3565: {
3566: VL tv,fv,vv,vc,av;
3567: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3568: int e,max,nvar,i;
3569: NDV b;
3570: int ishomo,nalg;
3571: Alg alpha,dp;
3572: P p,zp;
3573: Q dmy;
3574: LIST f1,f2;
3575: Obj obj;
3576: NumberField nf;
3577: struct order_spec *ord1;
3578: NODE permtrace,intred,ind,perm,trace,ti;
3579: int len,n,j;
3580: NDV *db,*pb;
3581:
3582: parse_nd_option(current_option);
3583: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3584: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3585: switch ( ord->id ) {
3586: case 1:
3587: if ( ord->nv != nvar )
3588: error("nd_check : invalid order specification");
3589: break;
3590: default:
3591: break;
3592: }
3593: nd_init_ord(ord);
1.6 noro 3594: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 3595: nd_setup_parameters(nvar,0);
3596:
3597: len = length(BDY(f));
3598: db = (NDV *)MALLOC(len*sizeof(NDV *));
3599: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3600: ptozp((P)BDY(t),1,&dmy,&zp);
3601: b = ptondv(CO,vv,zp);
3602: ndv_mod(m,b);
3603: ndv_mul_c(m,b,invm(HCM(b),m));
3604: db[i] = b;
3605: }
3606:
3607: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3608: intred = BDY((LIST)ARG3(BDY(tlist)));
3609: ind = BDY((LIST)ARG4(BDY(tlist)));
3610: perm = BDY((LIST)ARG0(permtrace));
3611: trace = NEXT(permtrace);
3612:
3613: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 3614: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 3615: if ( j > i ) i = j;
3616: }
3617: n = i+1;
3618: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3619: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3620: ti = BDY((LIST)BDY(t));
1.6 noro 3621: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 3622: }
3623: for ( t = trace; t; t = NEXT(t) ) {
3624: ti = BDY((LIST)BDY(t));
1.6 noro 3625: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3626: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3627: if ( DP_Print ) {
3628: fprintf(asir_out,"."); fflush(asir_out);
3629: }
3630: }
3631: for ( t = intred; t; t = NEXT(t) ) {
3632: ti = BDY((LIST)BDY(t));
1.6 noro 3633: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3634: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3635: if ( DP_Print ) {
3636: fprintf(asir_out,"*"); fflush(asir_out);
3637: }
3638: }
3639: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3640: NEXTNODE(r0,r);
1.6 noro 3641: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 3642: ndv_mul_c(m,b,invm(HCM(b),m));
3643: #if 0
1.6 noro 3644: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3645: #else
1.6 noro 3646: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3647: #endif
3648: }
3649: if ( r0 ) NEXT(r) = 0;
3650: MKLIST(*rp,r0);
3651: if ( DP_Print ) fprintf(asir_out,"\n");
3652: }
3653:
3654: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
3655: {
3656: VL tv,fv,vv,vc,av;
3657: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3658: int m,nocheck,nvar,mindex,e,max;
3659: NDV c;
3660: NMV a;
3661: P p,zp;
3662: Q dmy;
3663: EPOS oepos;
3664: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
3665: Alg alpha,dp;
3666: P poly;
3667: LIST f1,f2,zpl;
3668: Obj obj;
3669: NumberField nf;
3670: struct order_spec *ord1;
3671: struct oEGT eg_check,eg0,eg1;
3672: NODE tr,tl1,tl2,tl3,tl4;
3673: LIST l1,l2,l3,l4,l5;
3674: int *perm;
3675: int j,ret;
3676: Z jq,bpe;
1.15 ! noro 3677: VECT hvect;
1.1 noro 3678:
3679: nd_module = 0;
3680: nd_lf = 0;
3681: parse_nd_option(current_option);
3682: if ( nd_lf ) {
3683: if ( f4 )
3684: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3685: else
3686: error("nd_gr_trace is not implemented yet over a large finite field");
3687: return;
3688: }
3689: if ( DP_Multiple )
3690: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3691:
3692: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3693: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3694: switch ( ord->id ) {
3695: case 1:
3696: if ( ord->nv != nvar )
3697: error("nd_gr_trace : invalid order specification");
3698: break;
3699: default:
3700: break;
3701: }
3702:
3703: get_algtree((Obj)f,&av);
3704: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3705: nd_ntrans = nvar;
3706: nd_nalg = nalg;
3707: /* #i -> t#i */
3708: if ( nalg ) {
3709: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3710: ord = ord1;
3711: f = f1;
3712: }
3713: nvar += nalg;
3714:
3715: nocheck = 0;
3716: mindex = 0;
3717:
3718: if ( Demand ) nd_demand = 1;
3719: else nd_demand = 0;
3720:
3721: /* setup modulus */
3722: if ( trace < 0 ) {
3723: trace = -trace;
3724: nocheck = 1;
3725: }
3726: m = trace > 1 ? trace : get_lprime(mindex);
3727: nd_init_ord(ord);
3728: mrank = 0;
3729: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3730: for ( tv = vv; tv; tv = NEXT(tv) ) {
3731: if ( nd_module ) {
3732: s = BDY((LIST)BDY(t));
3733: trank = length(s);
3734: mrank = MAX(mrank,trank);
3735: for ( ; s; s = NEXT(s) ) {
3736: e = getdeg(tv->v,(P)BDY(s));
3737: max = MAX(e,max);
3738: }
3739: } else {
3740: e = getdeg(tv->v,(P)BDY(t));
3741: max = MAX(e,max);
3742: }
3743: }
3744: nd_setup_parameters(nvar,max);
3745: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3746: ishomo = 1;
3747: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3748: if ( nd_module ) {
3749: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3750: else zpl = (LIST)BDY(t);
3751: c = (pointer)pltondv(CO,vv,zpl);
3752: } else {
3753: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3754: else zp = (P)BDY(t);
3755: c = (pointer)ptondv(CO,vv,zp);
3756: }
3757: if ( ishomo )
3758: ishomo = ishomo && ndv_ishomo(c);
3759: if ( c ) {
3760: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3761: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3762: }
3763: }
3764: if ( in0 ) NEXT(in) = 0;
3765: if ( fd0 ) NEXT(fd) = 0;
3766: if ( !ishomo && homo ) {
3767: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3768: c = (NDV)BDY(t); len = LEN(c);
3769: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3770: wmax = MAX(TD(DL(a)),wmax);
3771: }
3772: homogenize_order(ord,nvar,&ord1);
3773: nd_init_ord(ord1);
3774: nd_setup_parameters(nvar+1,wmax);
3775: for ( t = fd0; t; t = NEXT(t) )
3776: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3777: }
3778: if ( MaxDeg > 0 ) nocheck = 1;
3779: while ( 1 ) {
3780: tl1 = tl2 = tl3 = tl4 = 0;
3781: if ( Demand )
3782: nd_demand = 1;
3783: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
3784: if ( nd_gentrace ) {
3785: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3786: }
3787: if ( ret )
3788: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3789: if ( !ret || !cand ) {
3790: /* failure */
3791: if ( trace > 1 ) { *rp = 0; return; }
3792: else m = get_lprime(++mindex);
3793: continue;
3794: }
1.15 ! noro 3795: if ( nd_gentrace ) {
! 3796: MKVECT(hvect,nd_psn);
! 3797: for ( i = 0; i < nd_psn; i++ )
! 3798: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
! 3799: }
1.1 noro 3800: if ( !ishomo && homo ) {
3801: /* dehomogenization */
3802: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3803: nd_init_ord(ord);
3804: nd_setup_parameters(nvar,0);
3805: }
3806: nd_demand = 0;
3807: cand = ndv_reducebase(cand,perm);
3808: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3809: cand = ndv_reduceall(0,cand);
3810: cbpe = nd_bpe;
3811: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
3812: get_eg(&eg0);
3813: if ( nocheck )
3814: break;
3815: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
3816: if ( nd_gentrace ) {
3817: tl3 = nd_alltracelist; nd_alltracelist = 0;
3818: } else tl3 = 0;
3819: /* gbcheck : cand is a GB of Id(cand) ? */
3820: if ( nd_vc || nd_gentrace || nd_gensyz )
3821: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3822: else
3823: ret = nd_f4(0,1,0)!=0;
3824: if ( nd_gentrace && nd_gensyz ) {
3825: tl4 = nd_alltracelist; nd_alltracelist = 0;
3826: } else tl4 = 0;
3827: }
3828: if ( ret ) break;
3829: else if ( trace > 1 ) {
3830: /* failure */
3831: *rp = 0; return;
3832: } else {
3833: /* try the next modulus */
3834: m = get_lprime(++mindex);
3835: /* reset the parameters */
3836: if ( !ishomo && homo ) {
3837: nd_init_ord(ord1);
3838: nd_setup_parameters(nvar+1,wmax);
3839: } else {
3840: nd_init_ord(ord);
3841: nd_setup_parameters(nvar,max);
3842: }
3843: }
3844: }
3845: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3846: if ( DP_Print )
1.6 noro 3847: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 3848: /* dp->p */
3849: nd_bpe = cbpe;
3850: nd_setup_parameters(nd_nvar,0);
3851: for ( r = cand; r; r = NEXT(r) ) {
3852: if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
3853: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
3854: }
3855: if ( nd_nalg )
3856: cand = postprocess_algcoef(av,alist,cand);
3857: MKLIST(*rp,cand);
3858: if ( nd_gentrace ) {
3859: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3860: tl3 = reverse_node(tl3);
3861: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3862: for ( t = tl2; t; t = NEXT(t) ) {
3863: /* s = [i,[*,j,*,*],...] */
3864: s = BDY((LIST)BDY(t));
1.6 noro 3865: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3866: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3867: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3868: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3869: }
3870: }
3871: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3872: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3873: }
3874: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3875: MKLIST(l5,tl4);
1.6 noro 3876: STOZ(nd_bpe,bpe);
1.15 ! noro 3877: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3878: }
3879: }
3880:
3881: /* XXX : module element is not considered */
3882:
3883: void dltondl(int n,DL dl,UINT *r)
3884: {
3885: UINT *d;
3886: int i,j,l,s,ord_l;
3887: struct order_pair *op;
3888:
3889: d = (unsigned int *)dl->d;
3890: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3891: if ( nd_blockmask ) {
3892: l = nd_blockmask->n;
3893: op = nd_blockmask->order_pair;
3894: for ( j = 0, s = 0; j < l; j++ ) {
3895: ord_l = op[j].length;
3896: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3897: }
3898: TD(r) = ndl_weight(r);
3899: ndl_weight_mask(r);
3900: } else {
3901: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3902: TD(r) = ndl_weight(r);
3903: }
3904: }
3905:
3906: DL ndltodl(int n,UINT *ndl)
3907: {
3908: DL dl;
3909: int *d;
3910: int i,j,l,s,ord_l;
3911: struct order_pair *op;
3912:
3913: NEWDL(dl,n);
3914: dl->td = TD(ndl);
3915: d = dl->d;
3916: if ( nd_blockmask ) {
3917: l = nd_blockmask->n;
3918: op = nd_blockmask->order_pair;
3919: for ( j = 0, s = 0; j < l; j++ ) {
3920: ord_l = op[j].length;
3921: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3922: }
3923: } else {
3924: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3925: }
3926: return dl;
3927: }
3928:
3929: void nmtodp(int mod,NM m,DP *r)
3930: {
3931: DP dp;
3932: MP mr;
3933:
3934: NEWMP(mr);
3935: mr->dl = ndltodl(nd_nvar,DL(m));
3936: mr->c = (Obj)ndctop(mod,m->c);
3937: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3938: *r = dp;
3939: }
3940:
1.15 ! noro 3941: void ndltodp(UINT *d,DP *r)
! 3942: {
! 3943: DP dp;
! 3944: MP mr;
! 3945:
! 3946: NEWMP(mr);
! 3947: mr->dl = ndltodl(nd_nvar,d);
! 3948: mr->c = (Obj)ONE;
! 3949: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
! 3950: *r = dp;
! 3951: }
! 3952:
1.1 noro 3953: void ndl_print(UINT *dl)
3954: {
3955: int n;
3956: int i,j,l,ord_l,s,s0;
3957: struct order_pair *op;
3958:
3959: n = nd_nvar;
3960: printf("<<");
3961: if ( nd_blockmask ) {
3962: l = nd_blockmask->n;
3963: op = nd_blockmask->order_pair;
3964: for ( j = 0, s = s0 = 0; j < l; j++ ) {
3965: ord_l = op[j].length;
3966: for ( i = 0; i < ord_l; i++, s++ )
3967: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
3968: }
3969: } else {
3970: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
3971: }
3972: printf(">>");
3973: if ( nd_module && MPOS(dl) )
3974: printf("*e%d",MPOS(dl));
3975: }
3976:
3977: void nd_print(ND p)
3978: {
3979: NM m;
3980:
3981: if ( !p )
3982: printf("0\n");
3983: else {
3984: for ( m = BDY(p); m; m = NEXT(m) ) {
3985: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3986: else printf("+%d*",CM(m));
3987: ndl_print(DL(m));
3988: }
3989: printf("\n");
3990: }
3991: }
3992:
3993: void nd_print_q(ND p)
3994: {
3995: NM m;
3996:
3997: if ( !p )
3998: printf("0\n");
3999: else {
4000: for ( m = BDY(p); m; m = NEXT(m) ) {
4001: printf("+");
1.6 noro 4002: printexpr(CO,(Obj)CZ(m));
1.1 noro 4003: printf("*");
4004: ndl_print(DL(m));
4005: }
4006: printf("\n");
4007: }
4008: }
4009:
4010: void ndp_print(ND_pairs d)
4011: {
4012: ND_pairs t;
4013:
4014: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4015: printf("\n");
4016: }
4017:
4018: void nd_removecont(int mod,ND p)
4019: {
4020: int i,n;
4021: Z *w;
4022: NM m;
4023: struct oVECT v;
4024:
4025: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4026: else if ( mod == -2 ) {
4027: Z inv;
4028: divlf(ONE,HCZ(p),&inv);
4029: nd_mul_c_lf(p,inv);
4030: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4031: else {
4032: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4033: w = (Z *)MALLOC(n*sizeof(Q));
4034: v.len = n;
4035: v.body = (pointer *)w;
1.6 noro 4036: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4037: removecont_array((P *)w,n,1);
1.6 noro 4038: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4039: }
4040: }
4041:
4042: void nd_removecont2(ND p1,ND p2)
4043: {
4044: int i,n1,n2,n;
4045: Z *w;
4046: NM m;
4047: struct oVECT v;
4048:
4049: n1 = nd_length(p1);
4050: n2 = nd_length(p2);
4051: n = n1+n2;
4052: w = (Z *)MALLOC(n*sizeof(Q));
4053: v.len = n;
4054: v.body = (pointer *)w;
4055: i = 0;
4056: if ( p1 )
1.6 noro 4057: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4058: if ( p2 )
1.6 noro 4059: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4060: removecont_array((P *)w,n,1);
4061: i = 0;
4062: if ( p1 )
1.6 noro 4063: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4064: if ( p2 )
1.6 noro 4065: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4066: }
4067:
4068: void ndv_removecont(int mod,NDV p)
4069: {
4070: int i,len,all_p;
4071: Z *c;
4072: P *w;
4073: Z dvr,t;
4074: P g,cont,tp;
4075: NMV m;
4076:
4077: if ( mod == -1 )
4078: ndv_mul_c(mod,p,_invsf(HCM(p)));
4079: else if ( mod == -2 ) {
4080: Z inv;
4081: divlf(ONE,HCZ(p),&inv);
4082: ndv_mul_c_lf(p,inv);
4083: } else if ( mod )
4084: ndv_mul_c(mod,p,invm(HCM(p),mod));
4085: else {
4086: len = p->len;
4087: w = (P *)MALLOC(len*sizeof(P));
4088: c = (Z *)MALLOC(len*sizeof(Q));
4089: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4090: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4091: all_p = all_p && !NUM(w[i]);
4092: }
4093: if ( all_p ) {
4094: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4095: mulp(nd_vc,(P)dvr,g,&cont);
4096: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4097: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4098: }
4099: } else {
4100: sortbynm((Q *)c,len);
4101: qltozl((Q *)c,len,&dvr);
4102: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4103: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4104: }
4105: }
4106: }
4107: }
4108:
4109: /* koko */
4110:
4111: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4112: {
4113: int len,i,max;
4114: NMV m,mr0,mr,t;
4115:
4116: len = p->len;
1.14 noro 4117: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 4118: max = MAX(max,TD(DL(m)));
4119: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4120: m = (NMV)((char *)mr0+(len-1)*oadv);
4121: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4122: t = (NMV)MALLOC(nmv_adv);
4123: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4124: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4125: CZ(mr) = CZ(m);
1.1 noro 4126: ndl_copy(DL(t),DL(mr));
4127: }
4128: NV(p)++;
4129: BDY(p) = mr0;
4130: }
4131:
4132: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4133: {
4134: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4135: int pos;
4136: Q *w;
4137: Q dvr,t;
4138: NMV m,r;
4139:
4140: len = p->len;
4141: newnvar = nd_nvar-1;
4142: newexporigin = nd_get_exporigin(ord);
4143: if ( nd_module ) newmpos = newexporigin-1;
4144: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4145: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4146: ndl_dehomogenize(DL(m));
4147: if ( newwpd != nd_wpd ) {
4148: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4149: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 4150: CZ(r) = CZ(m);
1.1 noro 4151: if ( nd_module ) pos = MPOS(DL(m));
4152: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4153: adj = nd_exporigin-newexporigin;
4154: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
4155: if ( nd_module ) {
4156: DL(r)[newmpos] = pos;
4157: }
4158: }
4159: }
4160: NV(p)--;
4161: }
4162:
4163: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4164: {
4165: int i;
4166: P *tpl,*tpl1;
4167: NODE l;
4168: P h,gcd,t;
4169:
4170: tpl = (P *)MALLOC(m*sizeof(P));
4171: tpl1 = (P *)MALLOC(m*sizeof(P));
4172: bcopy(pl,tpl,m*sizeof(P));
4173: gcd = (P)ONE;
4174: for ( l = nd_hcf; l; l = NEXT(l) ) {
4175: h = (P)BDY(l);
4176: while ( 1 ) {
4177: for ( i = 0; i < m; i++ )
4178: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4179: break;
4180: if ( i == m ) {
4181: bcopy(tpl1,tpl,m*sizeof(P));
4182: mulp(vl,gcd,h,&t); gcd = t;
4183: } else
4184: break;
4185: }
4186: }
4187: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4188: if ( full ) {
4189: heu_nezgcdnpz(vl,tpl,m,&t);
4190: mulp(vl,gcd,t,pr);
4191: } else
4192: *pr = gcd;
4193: }
4194:
4195: void removecont_array(P *p,int n,int full)
4196: {
4197: int all_p,all_q,i;
4198: Z *c;
4199: P *w;
4200: P t,s;
4201:
4202: for ( all_q = 1, i = 0; i < n; i++ )
4203: all_q = all_q && NUM(p[i]);
4204: if ( all_q ) {
4205: removecont_array_q((Z *)p,n);
4206: } else {
4207: c = (Z *)MALLOC(n*sizeof(Z));
4208: w = (P *)MALLOC(n*sizeof(P));
4209: for ( i = 0; i < n; i++ ) {
4210: ptozp(p[i],1,(Q *)&c[i],&w[i]);
4211: }
4212: removecont_array_q(c,n);
4213: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4214: for ( i = 0; i < n; i++ ) {
4215: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4216: }
4217: }
4218: }
4219:
4220: /* c is an int array */
4221:
4222: void removecont_array_q(Z *c,int n)
4223: {
4224: struct oVECT v;
4225: Z d0,d1,a,u,u1,gcd;
4226: int i,j;
4227: Z *q,*r;
4228:
4229: q = (Z *)MALLOC(n*sizeof(Z));
4230: r = (Z *)MALLOC(n*sizeof(Z));
4231: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4232: gcdvz_estimate(&v,&d0);
4233: for ( i = 0; i < n; i++ ) {
4234: divqrz(c[i],d0,&q[i],&r[i]);
4235: }
4236: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4237: if ( i < n ) {
4238: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4239: gcdvz(&v,&d1);
4240: gcdz(d0,d1,&gcd);
1.6 noro 4241: /* exact division */
4242: divsz(d0,gcd,&a);
1.1 noro 4243: for ( i = 0; i < n; i++ ) {
4244: mulz(a,q[i],&u);
4245: if ( r[i] ) {
1.6 noro 4246: /* exact division */
4247: divsz(r[i],gcd,&u1);
1.1 noro 4248: addz(u,u1,&q[i]);
4249: } else
4250: q[i] = u;
4251: }
4252: }
4253: for ( i = 0; i < n; i++ ) c[i] = q[i];
4254: }
4255:
1.4 noro 4256: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
4257:
4258: void mpz_removecont_array(mpz_t *c,int n)
4259: {
4260: mpz_t d0,a,u,u1,gcd;
4261: int i,j;
1.13 noro 4262: static mpz_t *q,*r;
4263: static int c_len = 0;
1.4 noro 4264:
4265: for ( i = 0; i < n; i++ )
4266: if ( mpz_sgn(c[i]) ) break;
4267: if ( i == n ) return;
4268: gcdv_mpz_estimate(d0,c,n);
1.13 noro 4269: if ( n > c_len ) {
4270: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4271: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4272: c_len = n;
4273: }
1.4 noro 4274: for ( i = 0; i < n; i++ ) {
4275: mpz_init(q[i]); mpz_init(r[i]);
4276: mpz_fdiv_qr(q[i],r[i],c[i],d0);
4277: }
4278: for ( i = 0; i < n; i++ )
4279: if ( mpz_sgn(r[i]) ) break;
4280: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
4281: if ( i < n ) {
4282: mpz_gcd(gcd,d0,r[i]);
4283: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
4284: mpz_div(a,d0,gcd);
4285: for ( i = 0; i < n; i++ ) {
4286: mpz_mul(u,a,q[i]);
4287: if ( mpz_sgn(r[i]) ) {
4288: mpz_div(u1,r[i],gcd);
4289: mpz_add(q[i],u,u1);
4290: } else
4291: mpz_set(q[i],u);
4292: }
4293: }
4294: for ( i = 0; i < n; i++ )
4295: mpz_set(c[i],q[i]);
4296: }
4297:
1.1 noro 4298: void nd_mul_c(int mod,ND p,int mul)
4299: {
4300: NM m;
4301: int c,c1;
4302:
4303: if ( !p ) return;
4304: if ( mul == 1 ) return;
4305: if ( mod == -1 )
4306: for ( m = BDY(p); m; m = NEXT(m) )
4307: CM(m) = _mulsf(CM(m),mul);
4308: else
4309: for ( m = BDY(p); m; m = NEXT(m) ) {
4310: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4311: }
4312: }
4313:
4314: void nd_mul_c_lf(ND p,Z mul)
4315: {
4316: NM m;
4317: Z c;
4318:
4319: if ( !p ) return;
4320: if ( UNIZ(mul) ) return;
4321: for ( m = BDY(p); m; m = NEXT(m) ) {
4322: mullf(CZ(m),mul,&c); CZ(m) = c;
4323: }
4324: }
4325:
4326: void nd_mul_c_q(ND p,P mul)
4327: {
4328: NM m;
4329: P c;
4330:
4331: if ( !p ) return;
4332: if ( UNIQ(mul) ) return;
4333: for ( m = BDY(p); m; m = NEXT(m) ) {
4334: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4335: }
4336: }
4337:
4338: void nd_mul_c_p(VL vl,ND p,P mul)
4339: {
4340: NM m;
4341: P c;
4342:
4343: if ( !p ) return;
4344: for ( m = BDY(p); m; m = NEXT(m) ) {
4345: mulp(vl,CP(m),mul,&c); CP(m) = c;
4346: }
4347: }
4348:
4349: void nd_free(ND p)
4350: {
4351: NM t,s;
4352:
4353: if ( !p ) return;
4354: t = BDY(p);
4355: while ( t ) {
4356: s = NEXT(t);
4357: FREENM(t);
4358: t = s;
4359: }
4360: FREEND(p);
4361: }
4362:
4363: void ndv_free(NDV p)
4364: {
4365: GCFREE(BDY(p));
4366: }
4367:
4368: void nd_append_red(UINT *d,int i)
4369: {
4370: RHist m,m0;
4371: int h;
4372:
4373: NEWRHist(m);
4374: h = ndl_hash_value(d);
4375: m->index = i;
4376: ndl_copy(d,DL(m));
4377: NEXT(m) = nd_red[h];
4378: nd_red[h] = m;
4379: }
4380:
4381: UINT *ndv_compute_bound(NDV p)
4382: {
4383: UINT *d1,*d2,*t;
4384: UINT u;
4385: int i,j,k,l,len,ind;
4386: NMV m;
4387:
4388: if ( !p )
4389: return 0;
4390: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4391: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4392: len = LEN(p);
4393: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4394: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4395: ndl_max(DL(m),d1,d2);
4396: t = d1; d1 = d2; d2 = t;
4397: }
4398: l = nd_nvar+31;
4399: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4400: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4401: u = d1[i];
4402: k = (nd_epw-1)*nd_bpe;
4403: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4404: t[ind] = (u>>k)&nd_mask0;
4405: }
4406: for ( ; ind < l; ind++ ) t[ind] = 0;
4407: return t;
4408: }
4409:
4410: UINT *nd_compute_bound(ND p)
4411: {
4412: UINT *d1,*d2,*t;
4413: UINT u;
4414: int i,j,k,l,len,ind;
4415: NM m;
4416:
4417: if ( !p )
4418: return 0;
4419: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4420: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4421: len = LEN(p);
4422: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4423: for ( m = NEXT(m); m; m = NEXT(m) ) {
4424: ndl_lcm(DL(m),d1,d2);
4425: t = d1; d1 = d2; d2 = t;
4426: }
4427: l = nd_nvar+31;
4428: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4429: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4430: u = d1[i];
4431: k = (nd_epw-1)*nd_bpe;
4432: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4433: t[ind] = (u>>k)&nd_mask0;
4434: }
4435: for ( ; ind < l; ind++ ) t[ind] = 0;
4436: return t;
4437: }
4438:
4439: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4440: /* of a term. In this case we need additional 1 word. */
4441:
4442: int nd_get_exporigin(struct order_spec *ord)
4443: {
4444: switch ( ord->id ) {
4445: case 0: case 2: case 256: case 258:
4446: return 1+nd_module;
4447: case 1: case 257:
4448: /* block order */
4449: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4450: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4451: return ord->ord.block.length+1+nd_module;
4452: case 3: case 259:
4453: #if 0
4454: error("nd_get_exporigin : composite order is not supported yet.");
4455: #else
4456: return 1+nd_module;
4457: #endif
4458: default:
4459: error("nd_get_exporigin : ivalid argument.");
4460: return 0;
4461: }
4462: }
4463:
4464: void nd_setup_parameters(int nvar,int max) {
4465: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4466: struct order_pair *op;
4467:
4468: nd_nvar = nvar;
4469: if ( max ) {
4470: /* XXX */
4471: if ( do_weyl ) nd_bpe = 32;
4472: else if ( max < 2 ) nd_bpe = 1;
4473: else if ( max < 4 ) nd_bpe = 2;
4474: else if ( max < 8 ) nd_bpe = 3;
4475: else if ( max < 16 ) nd_bpe = 4;
4476: else if ( max < 32 ) nd_bpe = 5;
4477: else if ( max < 64 ) nd_bpe = 6;
4478: else if ( max < 256 ) nd_bpe = 8;
4479: else if ( max < 1024 ) nd_bpe = 10;
4480: else if ( max < 65536 ) nd_bpe = 16;
4481: else nd_bpe = 32;
4482: }
4483: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4484: UINT t;
4485: int st;
4486: int *v;
4487: /* t = max(weights) */
4488: t = 0;
4489: if ( current_dl_weight_vector )
4490: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4491: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4492: if ( t < st ) t = st;
4493: }
4494: if ( nd_matrix )
4495: for ( i = 0; i < nd_matrix_len; i++ )
4496: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4497: if ( (st=v[j]) < 0 ) st = -st;
4498: if ( t < st ) t = st;
4499: }
4500: /* i = bitsize of t */
4501: for ( i = 0; t; t >>=1, i++ );
4502: /* i += bitsize of nd_nvar */
4503: for ( t = nd_nvar; t; t >>=1, i++);
4504: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4505: if ( (nd_bpe+i) >= 31 )
4506: error("nd_setup_parameters : too large weight");
4507: }
4508: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4509: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4510: nd_exporigin = nd_get_exporigin(nd_ord);
4511: wpd = nd_exporigin+elen;
4512: if ( nd_module )
4513: nd_mpos = nd_exporigin-1;
4514: else
4515: nd_mpos = -1;
4516: if ( wpd != nd_wpd ) {
4517: nd_free_private_storage();
4518: nd_wpd = wpd;
4519: }
4520: if ( nd_bpe < 32 ) {
4521: nd_mask0 = (1<<nd_bpe)-1;
4522: } else {
4523: nd_mask0 = 0xffffffff;
4524: }
4525: bzero(nd_mask,sizeof(nd_mask));
4526: nd_mask1 = 0;
4527: for ( i = 0; i < nd_epw; i++ ) {
4528: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4529: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4530: }
4531: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4532: nd_epos = nd_create_epos(nd_ord);
4533: nd_blockmask = nd_create_blockmask(nd_ord);
4534: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4535: }
4536:
4537: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4538: {
4539: int i,obpe,oadv,h;
4540: static NM prev_nm_free_list;
4541: static ND_pairs prev_ndp_free_list;
4542: RHist mr0,mr;
4543: RHist r;
4544: RHist *old_red;
4545: ND_pairs s0,s,t;
4546: EPOS oepos;
4547:
4548: obpe = nd_bpe;
4549: oadv = nmv_adv;
4550: oepos = nd_epos;
4551: if ( obpe < 2 ) nd_bpe = 2;
4552: else if ( obpe < 3 ) nd_bpe = 3;
4553: else if ( obpe < 4 ) nd_bpe = 4;
4554: else if ( obpe < 5 ) nd_bpe = 5;
4555: else if ( obpe < 6 ) nd_bpe = 6;
4556: else if ( obpe < 8 ) nd_bpe = 8;
4557: else if ( obpe < 10 ) nd_bpe = 10;
4558: else if ( obpe < 16 ) nd_bpe = 16;
4559: else if ( obpe < 32 ) nd_bpe = 32;
4560: else error("nd_reconstruct : exponent too large");
4561:
4562: nd_setup_parameters(nd_nvar,0);
4563: prev_nm_free_list = _nm_free_list;
4564: prev_ndp_free_list = _ndp_free_list;
4565: _nm_free_list = 0;
4566: _ndp_free_list = 0;
4567: for ( i = nd_psn-1; i >= 0; i-- ) {
4568: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4569: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4570: }
4571: if ( trace )
4572: for ( i = nd_psn-1; i >= 0; i-- ) {
4573: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4574: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4575: }
4576: s0 = 0;
4577: for ( t = d; t; t = NEXT(t) ) {
4578: NEXTND_pairs(s0,s);
4579: s->i1 = t->i1;
4580: s->i2 = t->i2;
4581: SG(s) = SG(t);
4582: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4583: }
4584:
4585: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4586: for ( i = 0; i < REDTAB_LEN; i++ ) {
4587: old_red[i] = nd_red[i];
4588: nd_red[i] = 0;
4589: }
4590: for ( i = 0; i < REDTAB_LEN; i++ )
4591: for ( r = old_red[i]; r; r = NEXT(r) ) {
4592: NEWRHist(mr);
4593: mr->index = r->index;
4594: SG(mr) = SG(r);
4595: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4596: h = ndl_hash_value(DL(mr));
4597: NEXT(mr) = nd_red[h];
4598: nd_red[h] = mr;
4599: }
4600: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4601: old_red = 0;
4602: for ( i = 0; i < nd_psn; i++ ) {
4603: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4604: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4605: nd_psh[i] = r;
4606: }
4607: if ( s0 ) NEXT(s) = 0;
4608: prev_nm_free_list = 0;
4609: prev_ndp_free_list = 0;
4610: #if 0
4611: GC_gcollect();
4612: #endif
4613: return s0;
4614: }
4615:
4616: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4617: {
4618: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4619: struct order_pair *op;
4620:
4621: n = nd_nvar;
4622: oepw = (sizeof(UINT)*8)/obpe;
4623: omask0 = (1<<obpe)-1;
4624: TD(r) = TD(d);
4625: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4626: if ( nd_blockmask ) {
4627: l = nd_blockmask->n;
4628: op = nd_blockmask->order_pair;
4629: for ( i = 1; i < nd_exporigin; i++ )
4630: r[i] = d[i];
4631: for ( j = 0, s = 0; j < l; j++ ) {
4632: ord_l = op[j].length;
4633: for ( i = 0; i < ord_l; i++, s++ ) {
4634: ei = GET_EXP_OLD(d,s);
4635: PUT_EXP(r,s,ei);
4636: }
4637: }
4638: } else {
4639: for ( i = 0; i < n; i++ ) {
4640: ei = GET_EXP_OLD(d,i);
4641: PUT_EXP(r,i,ei);
4642: }
4643: }
4644: if ( nd_module ) MPOS(r) = MPOS(d);
4645: }
4646:
4647: ND nd_copy(ND p)
4648: {
4649: NM m,mr,mr0;
4650: int c,n;
4651: ND r;
4652:
4653: if ( !p )
4654: return 0;
4655: else {
4656: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4657: NEXTNM(mr0,mr);
4658: CM(mr) = CM(m);
4659: ndl_copy(DL(m),DL(mr));
4660: }
4661: NEXT(mr) = 0;
4662: MKND(NV(p),mr0,LEN(p),r);
4663: SG(r) = SG(p);
4664: return r;
4665: }
4666: }
4667:
4668: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4669: {
4670: NM m1,m2;
4671: NDV p1,p2;
4672: ND t1,t2;
4673: UINT *lcm;
4674: P gp,tp;
4675: Z g,t;
4676: Z iq;
4677: int td;
4678: LIST hist;
4679: NODE node;
4680: DP d;
4681:
4682: if ( !mod && nd_demand ) {
4683: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4684: } else {
4685: if ( trace ) {
4686: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4687: } else {
4688: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4689: }
4690: }
4691: lcm = LCM(p);
4692: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4693: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4694: FREENM(m1); return 0;
4695: }
4696: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4697: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4698: FREENM(m1); FREENM(m2); return 0;
4699: }
4700:
4701: if ( mod == -1 ) {
4702: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4703: } else if ( mod > 0 ) {
4704: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4705: } else if ( mod == -2 ) {
4706: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4707: } else if ( nd_vc ) {
4708: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4709: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4710: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4711: } else {
1.6 noro 4712: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 4713: }
4714: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4715: *rp = nd_add(mod,t1,t2);
4716: if ( nd_gentrace ) {
4717: /* nd_tracelist is initialized */
1.6 noro 4718: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4719: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 4720: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4721: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4722: nd_tracelist = node;
4723: }
4724: FREENM(m1); FREENM(m2);
4725: return 1;
4726: }
4727:
4728: void ndv_mul_c(int mod,NDV p,int mul)
4729: {
4730: NMV m;
4731: int c,c1,len,i;
4732:
4733: if ( !p ) return;
4734: len = LEN(p);
4735: if ( mod == -1 )
4736: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4737: CM(m) = _mulsf(CM(m),mul);
4738: else
4739: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4740: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4741: }
4742: }
4743:
4744: void ndv_mul_c_lf(NDV p,Z mul)
4745: {
4746: NMV m;
4747: Z c;
4748: int len,i;
4749:
4750: if ( !p ) return;
4751: len = LEN(p);
4752: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4753: mullf(CZ(m),mul,&c); CZ(m) = c;
4754: }
4755: }
4756:
4757: /* for nd_det */
4758: void ndv_mul_c_q(NDV p,Z mul)
4759: {
4760: NMV m;
4761: Z c;
4762: int len,i;
4763:
4764: if ( !p ) return;
4765: len = LEN(p);
4766: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 4767: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 4768: }
4769: }
4770:
4771: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4772: int n2,i,j,l,n,tlen;
4773: UINT *d0;
4774: NM *tab,*psum;
4775: ND s,r;
4776: NM t;
4777: NMV m1;
4778:
4779: if ( !p ) return 0;
4780: n = NV(p); n2 = n>>1;
4781: d0 = DL(m0);
4782: l = LEN(p);
4783: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4784: tab = (NM *)MALLOC(tlen*sizeof(NM));
4785: psum = (NM *)MALLOC(tlen*sizeof(NM));
4786: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4787: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4788: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4789: /* m0(NM) * m1(NMV) => tab(NM) */
4790: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4791: for ( j = 0; j < tlen; j++ ) {
4792: if ( tab[j] ) {
4793: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4794: }
4795: }
4796: }
4797: for ( i = tlen-1, r = 0; i >= 0; i-- )
4798: if ( psum[i] ) {
4799: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4800: MKND(n,psum[i],j,s);
4801: r = nd_add(mod,r,s);
4802: }
4803: if ( r ) SG(r) = SG(p)+TD(d0);
4804: return r;
4805: }
4806:
4807: /* product of monomials */
4808: /* XXX block order is not handled correctly */
4809:
4810: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4811: {
4812: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4813: UINT *d0,*d1,*d,*dt,*ctab;
4814: Z *ctab_q;
4815: Z q,q1;
4816: UINT c0,c1,c;
4817: NM *p;
4818: NM m,t;
4819: int mpos;
4820:
4821: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4822: if ( !m0 || !m1 ) return;
4823: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4824: if ( nd_module )
4825: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4826:
4827: NEWNM(m); d = DL(m);
4828: if ( mod ) {
4829: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4830: } else if ( nd_vc )
4831: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4832: else
1.6 noro 4833: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 4834: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4835: homo = n&1 ? 1 : 0;
4836: if ( homo ) {
4837: /* offset of h-degree */
4838: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4839: PUT_EXP(DL(m),n-1,h);
4840: TD(DL(m)) = h;
4841: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4842: }
4843: tab[0] = m;
4844: NEWNM(m); d = DL(m);
4845: for ( i = 0, curlen = 1; i < n2; i++ ) {
4846: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4847: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4848: /* xi^a*(Di^k*xi^l)*Di^b */
4849: a += l; b += k;
4850: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4851: if ( !k || !l ) {
4852: for ( j = 0; j < curlen; j++ )
4853: if ( (t = tab[j]) != 0 ) {
4854: dt = DL(t);
4855: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4856: if ( nd_blockmask ) ndl_weight_mask(dt);
4857: }
4858: curlen *= k+1;
4859: continue;
4860: }
4861: min = MIN(k,l);
4862: if ( mod ) {
4863: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4864: mkwcm(k,l,mod,(int *)ctab);
4865: } else {
4866: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4867: mkwc(k,l,ctab_q);
4868: }
4869: for ( j = min; j >= 0; j-- ) {
4870: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4871: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4872: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4873: if ( homo ) {
4874: TD(d) = s;
4875: PUT_EXP(d,n-1,s-h);
4876: } else TD(d) = h;
4877: if ( nd_blockmask ) ndl_weight_mask(d);
4878: if ( mod ) c = ctab[j];
4879: else q = ctab_q[j];
4880: p = tab+curlen*j;
4881: if ( j == 0 ) {
4882: for ( u = 0; u < curlen; u++, p++ ) {
4883: if ( tab[u] ) {
4884: ndl_addto(DL(tab[u]),d);
4885: if ( mod ) {
4886: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4887: } else if ( nd_vc )
4888: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4889: else {
1.6 noro 4890: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 4891: }
4892: }
4893: }
4894: } else {
4895: for ( u = 0; u < curlen; u++, p++ ) {
4896: if ( tab[u] ) {
4897: NEWNM(t);
4898: ndl_add(DL(tab[u]),d,DL(t));
4899: if ( mod ) {
4900: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4901: } else if ( nd_vc )
4902: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4903: else
1.6 noro 4904: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 4905: *p = t;
4906: }
4907: }
4908: }
4909: }
4910: curlen *= k+1;
4911: }
4912: FREENM(m);
4913: if ( nd_module ) {
4914: mpos = MPOS(d1);
4915: for ( i = 0; i < tlen; i++ )
4916: if ( tab[i] ) {
4917: d = DL(tab[i]);
4918: MPOS(d) = mpos;
4919: TD(d) = ndl_weight(d);
4920: }
4921: }
4922: }
4923:
4924: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4925: {
4926: NM mr,mr0;
4927: NMV m;
4928: UINT *d,*dt,*dm;
4929: int c,n,td,i,c1,c2,len;
4930: Q q;
4931: ND r;
4932:
4933: if ( !p ) return 0;
4934: else {
4935: n = NV(p); m = BDY(p);
4936: d = DL(m0);
4937: len = LEN(p);
4938: mr0 = 0;
4939: td = TD(d);
4940: c = CM(m0);
4941: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4942: NEXTNM(mr0,mr);
4943: CM(mr) = 1;
4944: ndl_add(DL(m),d,DL(mr));
4945: }
4946: NEXT(mr) = 0;
4947: MKND(NV(p),mr0,len,r);
4948: SG(r) = SG(p) + TD(d);
4949: return r;
4950: }
4951: }
4952:
4953: ND ndv_mul_nm(int mod,NM m0,NDV p)
4954: {
4955: NM mr,mr0;
4956: NMV m;
4957: UINT *d,*dt,*dm;
4958: int c,n,td,i,c1,c2,len;
4959: P q;
4960: ND r;
4961:
4962: if ( !p ) return 0;
4963: else if ( do_weyl ) {
4964: if ( mod < 0 ) {
4965: error("ndv_mul_nm : not implemented (weyl)");
4966: return 0;
4967: } else
4968: return weyl_ndv_mul_nm(mod,m0,p);
4969: } else {
4970: n = NV(p); m = BDY(p);
4971: d = DL(m0);
4972: len = LEN(p);
4973: mr0 = 0;
4974: td = TD(d);
4975: if ( mod == -1 ) {
4976: c = CM(m0);
4977: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4978: NEXTNM(mr0,mr);
4979: CM(mr) = _mulsf(CM(m),c);
4980: ndl_add(DL(m),d,DL(mr));
4981: }
4982: } else if ( mod == -2 ) {
4983: Z cl;
4984: cl = CZ(m0);
4985: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4986: NEXTNM(mr0,mr);
4987: mullf(CZ(m),cl,&CZ(mr));
4988: ndl_add(DL(m),d,DL(mr));
4989: }
4990: } else if ( mod ) {
4991: c = CM(m0);
4992: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4993: NEXTNM(mr0,mr);
4994: c1 = CM(m);
4995: DMAR(c1,c,0,mod,c2);
4996: CM(mr) = c2;
4997: ndl_add(DL(m),d,DL(mr));
4998: }
4999: } else {
5000: q = CP(m0);
5001: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5002: NEXTNM(mr0,mr);
5003: mulp(nd_vc,CP(m),q,&CP(mr));
5004: ndl_add(DL(m),d,DL(mr));
5005: }
5006: }
5007: NEXT(mr) = 0;
5008: MKND(NV(p),mr0,len,r);
5009: SG(r) = SG(p) + TD(d);
5010: return r;
5011: }
5012: }
5013:
5014: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
5015: {
5016: NM mq0,mq;
5017: NMV tm;
5018: Q q;
5019: int i,nv,sg,c,c1,c2,hindex;
5020: ND p,t,r;
5021:
5022: if ( bucket->m < 0 ) return 0;
5023: else {
5024: nv = NV(d);
5025: mq0 = 0;
5026: tm = (NMV)MALLOC(nmv_adv);
5027: while ( 1 ) {
5028: if ( mod > 0 || mod == -1 )
5029: hindex = head_pbucket(mod,bucket);
5030: else if ( mod == -2 )
5031: hindex = head_pbucket_lf(bucket);
5032: else
5033: hindex = head_pbucket_q(bucket);
5034: if ( hindex < 0 ) break;
5035: p = bucket->body[hindex];
5036: NEXTNM(mq0,mq);
5037: ndl_sub(HDL(p),HDL(d),DL(mq));
5038: ndl_copy(DL(mq),DL(tm));
5039: if ( mod ) {
5040: c1 = invm(HCM(d),mod); c2 = HCM(p);
5041: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5042: CM(tm) = mod-c;
5043: } else {
1.6 noro 5044: divsz(HCZ(p),HCZ(d),&CZ(mq));
5045: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5046: }
5047: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5048: bucket->body[hindex] = nd_remove_head(p);
5049: t = nd_remove_head(t);
5050: add_pbucket(mod,bucket,t);
5051: }
5052: if ( !mq0 )
5053: r = 0;
5054: else {
5055: NEXT(mq) = 0;
5056: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5057: MKND(nv,mq0,i,r);
5058: /* XXX */
5059: SG(r) = HTD(r);
5060: }
5061: return r;
5062: }
5063: }
5064:
5065: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5066: {
5067: NMV m,mr,mr0,t;
5068: int len,i,k;
5069:
5070: if ( !p ) return;
5071: m = BDY(p); len = LEN(p);
5072: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5073: m = (NMV)((char *)mr0+(len-1)*oadv);
5074: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5075: t = (NMV)MALLOC(nmv_adv);
5076: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5077: CZ(t) = CZ(m);
1.1 noro 5078: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5079: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5080: CZ(mr) = CZ(t);
1.1 noro 5081: ndl_copy(DL(t),DL(mr));
5082: }
5083: BDY(p) = mr0;
5084: }
5085:
5086: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5087: {
5088: NMV m,mr,mr0;
5089: int len,i;
5090: NDV r;
5091:
5092: if ( !p ) return 0;
5093: m = BDY(p); len = LEN(p);
5094: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5095: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5096: ndl_zero(DL(mr));
5097: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5098: CZ(mr) = CZ(m);
1.1 noro 5099: }
5100: MKNDV(NV(p),mr0,len,r);
5101: SG(r) = SG(p);
5102: return r;
5103: }
5104:
5105: /* duplicate p */
5106:
5107: NDV ndv_dup(int mod,NDV p)
5108: {
5109: NDV d;
5110: NMV t,m,m0;
5111: int i,len;
5112:
5113: if ( !p ) return 0;
5114: len = LEN(p);
5115: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5116: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5117: ndl_copy(DL(t),DL(m));
1.6 noro 5118: CZ(m) = CZ(t);
1.1 noro 5119: }
5120: MKNDV(NV(p),m0,len,d);
5121: SG(d) = SG(p);
5122: return d;
5123: }
5124:
5125: NDV ndv_symbolic(int mod,NDV p)
5126: {
5127: NDV d;
5128: NMV t,m,m0;
5129: int i,len;
5130:
5131: if ( !p ) return 0;
5132: len = LEN(p);
5133: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5134: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5135: ndl_copy(DL(t),DL(m));
1.6 noro 5136: CZ(m) = ONE;
1.1 noro 5137: }
5138: MKNDV(NV(p),m0,len,d);
5139: SG(d) = SG(p);
5140: return d;
5141: }
5142:
5143: ND nd_dup(ND p)
5144: {
5145: ND d;
5146: NM t,m,m0;
5147:
5148: if ( !p ) return 0;
5149: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5150: NEXTNM(m0,m);
5151: ndl_copy(DL(t),DL(m));
1.6 noro 5152: CZ(m) = CZ(t);
1.1 noro 5153: }
5154: if ( m0 ) NEXT(m) = 0;
5155: MKND(NV(p),m0,LEN(p),d);
5156: SG(d) = SG(p);
5157: return d;
5158: }
5159:
5160: /* XXX if p->len == 0 then it represents 0 */
5161:
5162: void ndv_mod(int mod,NDV p)
5163: {
5164: NMV t,d;
5165: int r,s,u;
5166: int i,len,dlen;
5167: P cp;
5168: Z c;
5169: Obj gfs;
5170:
5171: if ( !p ) return;
5172: len = LEN(p);
5173: dlen = 0;
5174: if ( mod == -1 )
5175: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5176: simp_ff((Obj)CP(t),&gfs);
5177: if ( gfs ) {
5178: r = FTOIF(CONT((GFS)gfs));
5179: CM(d) = r;
5180: ndl_copy(DL(t),DL(d));
5181: NMV_ADV(d);
5182: dlen++;
5183: }
5184: }
5185: else if ( mod == -2 )
5186: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5187: simp_ff((Obj)CP(t),&gfs);
5188: if ( gfs ) {
5189: lmtolf((LM)gfs,&CZ(d));
5190: ndl_copy(DL(t),DL(d));
5191: NMV_ADV(d);
5192: dlen++;
5193: }
5194: }
5195: else
5196: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5197: if ( nd_vc ) {
5198: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5199: c = (Z)cp;
5200: } else
1.6 noro 5201: c = CZ(t);
1.1 noro 5202: r = remqi((Q)c,mod);
5203: if ( r ) {
5204: CM(d) = r;
5205: ndl_copy(DL(t),DL(d));
5206: NMV_ADV(d);
5207: dlen++;
5208: }
5209: }
5210: LEN(p) = dlen;
5211: }
5212:
5213: NDV ptondv(VL vl,VL dvl,P p)
5214: {
5215: ND nd;
5216:
5217: nd = ptond(vl,dvl,p);
5218: return ndtondv(0,nd);
5219: }
5220:
5221: void pltozpl(LIST l,Q *cont,LIST *pp)
5222: {
5223: NODE nd,nd1;
5224: int n;
5225: P *pl;
5226: Q *cl;
5227: int i;
5228: P dmy;
5229: Z dvr;
5230: LIST r;
5231:
5232: nd = BDY(l); n = length(nd);
5233: pl = (P *)MALLOC(n*sizeof(P));
5234: cl = (Q *)MALLOC(n*sizeof(P));
5235: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5236: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5237: qltozl(cl,n,&dvr);
5238: nd = BDY(l);
5239: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5240: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5241: }
5242: nd = 0;
5243: for ( i = n-1; i >= 0; i-- ) {
5244: MKNODE(nd1,pl[i],nd); nd = nd1;
5245: }
5246: MKLIST(r,nd);
5247: *pp = r;
5248: }
5249:
5250: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5251:
5252: NDV pltondv(VL vl,VL dvl,LIST p)
5253: {
5254: int i;
5255: NODE t;
5256: ND r,ri;
5257: NM m;
5258:
5259: if ( !nd_module ) error("pltond : module order must be set");
5260: r = 0;
5261: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5262: ri = ptond(vl,dvl,(P)BDY(t));
5263: if ( ri )
5264: for ( m = BDY(ri); m; m = NEXT(m) ) {
5265: MPOS(DL(m)) = i;
5266: TD(DL(m)) = ndl_weight(DL(m));
5267: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5268: }
5269: r = nd_add(0,r,ri);
5270: }
5271: return ndtondv(0,r);
5272: }
5273:
5274: ND ptond(VL vl,VL dvl,P p)
5275: {
5276: int n,i,j,k,e;
5277: VL tvl;
5278: V v;
5279: DCP dc;
5280: DCP *w;
5281: ND r,s,t,u;
5282: P x;
5283: int c;
5284: UINT *d;
5285: NM m,m0;
5286:
5287: if ( !p )
5288: return 0;
5289: else if ( NUM(p) ) {
5290: NEWNM(m);
5291: ndl_zero(DL(m));
5292: if ( !INT((Q)p) )
5293: error("ptond : input must be integer-coefficient");
1.6 noro 5294: CZ(m) = (Z)p;
1.1 noro 5295: NEXT(m) = 0;
5296: MKND(nd_nvar,m,1,r);
5297: SG(r) = 0;
5298: return r;
5299: } else {
5300: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5301: w = (DCP *)MALLOC(k*sizeof(DCP));
5302: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5303: for ( i = 0, tvl = dvl, v = VR(p);
5304: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5305: if ( !tvl ) {
5306: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5307: t = ptond(vl,dvl,COEF(w[j]));
5308: pwrp(vl,x,DEG(w[j]),&p);
5309: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5310: }
5311: return s;
5312: } else {
5313: NEWNM(m0); d = DL(m0);
5314: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 5315: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 5316: TD(d) = MUL_WEIGHT(e,i);
5317: if ( nd_blockmask) ndl_weight_mask(d);
5318: if ( nd_module ) MPOS(d) = 0;
5319: t = ptond(vl,dvl,COEF(w[j]));
5320: for ( m = BDY(t); m; m = NEXT(m) )
5321: ndl_addto(DL(m),d);
5322: SG(t) += TD(d);
5323: s = nd_add(0,s,t);
5324: }
5325: FREENM(m0);
5326: return s;
5327: }
5328: }
5329: }
5330:
5331: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5332: {
5333: VL tvl;
5334: int len,n,j,i,e;
5335: NMV m;
5336: Z q;
5337: P c;
5338: UINT *d;
5339: P s,r,u,t,w;
5340: GFS gfs;
5341:
5342: if ( !p ) return 0;
5343: else {
5344: len = LEN(p);
5345: n = NV(p);
5346: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5347: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
5348: if ( mod == -1 ) {
5349: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5350: } else if ( mod == -2 ) {
5351: c = (P)CZ(m);
5352: } else if ( mod > 0 ) {
1.6 noro 5353: STOZ(CM(m),q); c = (P)q;
1.1 noro 5354: } else
5355: c = CP(m);
5356: d = DL(m);
5357: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5358: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5359: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5360: }
5361: addp(vl,s,t,&u); s = u;
5362: }
5363: return s;
5364: }
5365: }
5366:
5367: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5368: {
5369: VL tvl;
5370: int len,n,j,i,e;
5371: NMV m;
5372: Z q;
5373: P c;
5374: UINT *d;
5375: P s,r,u,t,w;
5376: GFS gfs;
5377: P *a;
5378: LIST l;
5379: NODE nd,nd1;
5380:
5381: if ( !p ) return 0;
5382: else {
5383: a = (P *)MALLOC((rank+1)*sizeof(P));
5384: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5385: len = LEN(p);
5386: n = NV(p);
5387: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5388: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5389: if ( mod == -1 ) {
5390: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5391: } else if ( mod ) {
1.6 noro 5392: STOZ(CM(m),q); c = (P)q;
1.1 noro 5393: } else
5394: c = CP(m);
5395: d = DL(m);
5396: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5397: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5398: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5399: }
5400: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5401: }
5402: nd = 0;
5403: for ( i = rank; i > 0; i-- ) {
5404: MKNODE(nd1,a[i],nd); nd = nd1;
5405: }
5406: MKLIST(l,nd);
5407: return l;
5408: }
5409: }
5410:
5411: NDV ndtondv(int mod,ND p)
5412: {
5413: NDV d;
5414: NMV m,m0;
5415: NM t;
5416: int i,len;
5417:
5418: if ( !p ) return 0;
5419: len = LEN(p);
5420: if ( mod > 0 || mod == -1 )
5421: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5422: else
5423: m0 = m = MALLOC(len*nmv_adv);
5424: #if 0
5425: ndv_alloc += nmv_adv*len;
5426: #endif
5427: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5428: ndl_copy(DL(t),DL(m));
1.6 noro 5429: CZ(m) = CZ(t);
1.1 noro 5430: }
5431: MKNDV(NV(p),m0,len,d);
5432: SG(d) = SG(p);
5433: return d;
5434: }
5435:
5436: ND ndvtond(int mod,NDV p)
5437: {
5438: ND d;
5439: NM m,m0;
5440: NMV t;
5441: int i,len;
5442:
5443: if ( !p ) return 0;
5444: m0 = 0;
5445: len = p->len;
5446: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5447: NEXTNM(m0,m);
5448: ndl_copy(DL(t),DL(m));
1.6 noro 5449: CZ(m) = CZ(t);
1.1 noro 5450: }
5451: NEXT(m) = 0;
5452: MKND(NV(p),m0,len,d);
5453: SG(d) = SG(p);
5454: return d;
5455: }
5456:
5457: DP ndvtodp(int mod,NDV p)
5458: {
5459: MP m,m0;
5460: DP d;
5461: NMV t;
5462: int i,len;
5463:
5464: if ( !p ) return 0;
5465: m0 = 0;
5466: len = p->len;
5467: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5468: NEXTMP(m0,m);
5469: m->dl = ndltodl(nd_nvar,DL(t));
5470: m->c = (Obj)ndctop(mod,t->c);
5471: }
5472: NEXT(m) = 0;
5473: MKDP(nd_nvar,m0,d);
5474: SG(d) = SG(p);
5475: return d;
5476: }
5477:
5478: DP ndtodp(int mod,ND p)
5479: {
5480: MP m,m0;
5481: DP d;
5482: NM t;
5483: int i,len;
5484:
5485: if ( !p ) return 0;
5486: m0 = 0;
5487: len = p->len;
5488: for ( t = BDY(p); t; t = NEXT(t) ) {
5489: NEXTMP(m0,m);
5490: m->dl = ndltodl(nd_nvar,DL(t));
5491: m->c = (Obj)ndctop(mod,t->c);
5492: }
5493: NEXT(m) = 0;
5494: MKDP(nd_nvar,m0,d);
5495: SG(d) = SG(p);
5496: return d;
5497: }
5498:
5499: void ndv_print(NDV p)
5500: {
5501: NMV m;
5502: int i,len;
5503:
5504: if ( !p ) printf("0\n");
5505: else {
5506: len = LEN(p);
5507: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5508: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5509: else printf("+%d*",CM(m));
5510: ndl_print(DL(m));
5511: }
5512: printf("\n");
5513: }
5514: }
5515:
5516: void ndv_print_q(NDV p)
5517: {
5518: NMV m;
5519: int i,len;
5520:
5521: if ( !p ) printf("0\n");
5522: else {
5523: len = LEN(p);
5524: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5525: printf("+");
1.6 noro 5526: printexpr(CO,(Obj)CZ(m));
1.1 noro 5527: printf("*");
5528: ndl_print(DL(m));
5529: }
5530: printf("\n");
5531: }
5532: }
5533:
5534: NODE ndv_reducebase(NODE x,int *perm)
5535: {
5536: int len,i,j;
5537: NDVI w;
5538: NODE t,t0;
5539:
5540: len = length(x);
5541: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5542: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5543: w[i].p = BDY(t); w[i].i = perm[i];
5544: }
5545: for ( i = 0; i < len; i++ ) {
5546: for ( j = 0; j < i; j++ ) {
5547: if ( w[i].p && w[j].p ) {
5548: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5549: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5550: }
5551: }
5552: }
5553: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5554: if ( w[i].p ) {
5555: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5556: perm[j++] = w[i].i;
5557: }
5558: }
5559: NEXT(t) = 0; x = t0;
5560: return x;
5561: }
5562:
5563: /* XXX incomplete */
5564:
5565: void nd_init_ord(struct order_spec *ord)
5566: {
5567: nd_module = (ord->id >= 256);
5568: if ( nd_module ) {
5569: nd_dcomp = -1;
5570: nd_ispot = ord->ispot;
5571: nd_pot_nelim = ord->pot_nelim;
5572: nd_poly_weight_len = ord->nv;
5573: nd_poly_weight = ord->top_weight;
5574: nd_module_rank = ord->module_rank;
5575: nd_module_weight = ord->module_top_weight;
5576: }
5577: nd_matrix = 0;
5578: nd_matrix_len = 0;
5579: switch ( ord->id ) {
5580: case 0:
5581: switch ( ord->ord.simple ) {
5582: case 0:
5583: nd_dcomp = 1;
5584: nd_isrlex = 1;
5585: break;
5586: case 1:
5587: nd_dcomp = 1;
5588: nd_isrlex = 0;
5589: break;
5590: case 2:
5591: nd_dcomp = 0;
5592: nd_isrlex = 0;
5593: ndl_compare_function = ndl_lex_compare;
5594: break;
5595: case 11:
5596: /* XXX */
5597: nd_dcomp = 0;
5598: nd_isrlex = 1;
5599: ndl_compare_function = ndl_ww_lex_compare;
5600: break;
5601: default:
5602: error("nd_gr : unsupported order");
5603: }
5604: break;
5605: case 1:
5606: /* block order */
5607: /* XXX */
5608: nd_dcomp = -1;
5609: nd_isrlex = 0;
5610: ndl_compare_function = ndl_block_compare;
5611: break;
5612: case 2:
5613: /* matrix order */
5614: /* XXX */
5615: nd_dcomp = -1;
5616: nd_isrlex = 0;
5617: nd_matrix_len = ord->ord.matrix.row;
5618: nd_matrix = ord->ord.matrix.matrix;
5619: ndl_compare_function = ndl_matrix_compare;
5620: break;
5621: case 3:
5622: /* composite order */
5623: nd_dcomp = -1;
5624: nd_isrlex = 0;
5625: nd_worb_len = ord->ord.composite.length;
5626: nd_worb = ord->ord.composite.w_or_b;
5627: ndl_compare_function = ndl_composite_compare;
5628: break;
5629:
5630: /* module order */
5631: case 256:
5632: switch ( ord->ord.simple ) {
5633: case 0:
5634: nd_isrlex = 1;
5635: ndl_compare_function = ndl_module_grlex_compare;
5636: break;
5637: case 1:
5638: nd_isrlex = 0;
5639: ndl_compare_function = ndl_module_glex_compare;
5640: break;
5641: case 2:
5642: nd_isrlex = 0;
5643: ndl_compare_function = ndl_module_lex_compare;
5644: break;
5645: default:
5646: error("nd_gr : unsupported order");
5647: }
5648: break;
5649: case 257:
5650: /* block order */
5651: nd_isrlex = 0;
5652: ndl_compare_function = ndl_module_block_compare;
5653: break;
5654: case 258:
5655: /* matrix order */
5656: nd_isrlex = 0;
5657: nd_matrix_len = ord->ord.matrix.row;
5658: nd_matrix = ord->ord.matrix.matrix;
5659: ndl_compare_function = ndl_module_matrix_compare;
5660: break;
5661: case 259:
5662: /* composite order */
5663: nd_isrlex = 0;
5664: nd_worb_len = ord->ord.composite.length;
5665: nd_worb = ord->ord.composite.w_or_b;
5666: ndl_compare_function = ndl_module_composite_compare;
5667: break;
5668: }
5669: nd_ord = ord;
5670: }
5671:
5672: BlockMask nd_create_blockmask(struct order_spec *ord)
5673: {
5674: int n,i,j,s,l;
5675: UINT *t;
5676: BlockMask bm;
5677:
5678: /* we only create mask table for block order */
5679: if ( ord->id != 1 && ord->id != 257 )
5680: return 0;
5681: n = ord->ord.block.length;
5682: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5683: bm->n = n;
5684: bm->order_pair = ord->ord.block.order_pair;
5685: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5686: for ( i = 0, s = 0; i < n; i++ ) {
5687: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5688: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5689: l = bm->order_pair[i].length;
5690: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5691: }
5692: return bm;
5693: }
5694:
5695: EPOS nd_create_epos(struct order_spec *ord)
5696: {
5697: int i,j,l,s,ord_l,ord_o;
5698: EPOS epos;
5699: struct order_pair *op;
5700:
5701: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5702: switch ( ord->id ) {
5703: case 0: case 256:
5704: if ( nd_isrlex ) {
5705: for ( i = 0; i < nd_nvar; i++ ) {
5706: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5707: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5708: }
5709: } else {
5710: for ( i = 0; i < nd_nvar; i++ ) {
5711: epos[i].i = nd_exporigin + i/nd_epw;
5712: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5713: }
5714: }
5715: break;
5716: case 1: case 257:
5717: /* block order */
5718: l = ord->ord.block.length;
5719: op = ord->ord.block.order_pair;
5720: for ( j = 0, s = 0; j < l; j++ ) {
5721: ord_o = op[j].order;
5722: ord_l = op[j].length;
5723: if ( !ord_o )
5724: for ( i = 0; i < ord_l; i++ ) {
5725: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5726: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5727: }
5728: else
5729: for ( i = 0; i < ord_l; i++ ) {
5730: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5731: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5732: }
5733: s += ord_l;
5734: }
5735: break;
5736: case 2:
5737: /* matrix order */
5738: case 3:
5739: /* composite order */
5740: default:
5741: for ( i = 0; i < nd_nvar; i++ ) {
5742: epos[i].i = nd_exporigin + i/nd_epw;
5743: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5744: }
5745: break;
5746: }
5747: return epos;
5748: }
5749:
5750: /* external interface */
5751:
5752: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5753: {
5754: NODE t,in0,in;
5755: ND ndf,nf;
5756: NDV ndvf;
5757: VL vv,tv;
5758: int stat,nvar,max,mrank;
5759: union oNDC dn;
5760: Q cont;
5761: P pp;
5762: LIST ppl;
5763:
5764: if ( !f ) {
5765: *rp = 0;
5766: return;
5767: }
5768: pltovl(v,&vv);
5769: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5770:
5771: /* max=65536 implies nd_bpe=32 */
5772: max = 65536;
5773:
5774: nd_module = 0;
5775: /* nd_module will be set if ord is a module ordering */
5776: nd_init_ord(ord);
5777: nd_setup_parameters(nvar,max);
5778: if ( nd_module && OID(f) != O_LIST )
5779: error("nd_nf_p : the first argument must be a list");
5780: if ( nd_module ) mrank = length(BDY((LIST)f));
5781: /* conversion to ndv */
5782: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5783: NEXTNODE(in0,in);
5784: if ( nd_module ) {
5785: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5786: || length(BDY((LIST)BDY(t))) != mrank )
5787: error("nd_nf_p : inconsistent basis element");
5788: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5789: else ppl = (LIST)BDY(t);
5790: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5791: } else {
5792: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5793: else pp = (P)BDY(t);
5794: BDY(in) = (pointer)ptondv(CO,vv,pp);
5795: }
5796: if ( m ) ndv_mod(m,(NDV)BDY(in));
5797: }
5798: if ( in0 ) NEXT(in) = 0;
5799:
5800: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5801: else ndvf = ptondv(CO,vv,(P)f);
5802: if ( m ) ndv_mod(m,ndvf);
5803: ndf = (pointer)ndvtond(m,ndvf);
5804:
5805: /* dont sort, dont removecont */
5806: ndv_setup(m,0,in0,1,1);
5807: nd_scale=2;
1.6 noro 5808: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 5809: if ( !stat )
5810: error("nd_nf_p : exponent too large");
5811: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5812: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5813: }
5814:
5815: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5816: {
5817: NM m;
5818: UINT *t,*s;
5819: int i;
5820:
5821: for ( i = 0; i < n; i++ ) r[i] = 0;
5822: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5823: t = DL(m);
5824: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5825: r[i] = CM(m);
5826: }
5827: for ( i = 0; !r[i]; i++ );
5828: return i;
5829: }
5830:
5831: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
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.6 noro 5841: r[i] = CZ(m);
1.1 noro 5842: }
5843: for ( i = 0; !r[i]; i++ );
5844: return i;
5845: }
5846:
5847: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5848: {
5849: NM m;
5850: UINT *t,*s;
5851: int i;
5852:
5853: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5854: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5855: t = DL(m);
5856: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5857: mpz_set(r[i],BDY(CZ(m)));
5858: }
5859: for ( i = 0; !mpz_sgn(r[i]); i++ );
5860: return i;
5861: }
5862:
5863: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
5864: {
5865: NM m;
5866: unsigned long *v;
5867: int i,j,h,size;
5868: UINT *s,*t;
5869:
5870: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
5871: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
5872: bzero(v,size);
5873: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
5874: t = DL(m);
5875: h = ndl_hash_value(t);
5876: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5877: v[i/BLEN] |= 1L <<(i%BLEN);
5878: }
5879: return v;
5880: }
5881:
5882: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
5883: {
5884: NMV mr;
5885: UINT *d,*t,*s;
5886: int i,j,len,h,head;
5887:
5888: d = DL(m);
5889: len = LEN(p);
5890: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5891: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5892: ndl_add(d,DL(mr),t);
5893: h = ndl_hash_value(t);
5894: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
5895: if ( j == 0 ) head = i;
5896: v[i/BLEN] |= 1L <<(i%BLEN);
5897: }
5898: return head;
5899: }
5900:
5901: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
5902: {
5903: NM m;
5904: NMV mr;
5905: UINT *d,*t,*s;
5906: NDV p;
5907: int i,j,len;
5908: Z *r;
5909:
5910: m = pair->mul;
5911: d = DL(m);
5912: p = nd_ps[pair->index];
5913: len = LEN(p);
5914: r = (Z *)CALLOC(n,sizeof(Q));
5915: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5916: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
5917: ndl_add(d,DL(mr),t);
5918: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5919: r[i] = CZ(mr);
1.1 noro 5920: }
5921: return r;
5922: }
5923:
1.11 noro 5924: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 5925: {
5926: NM m;
5927: NMV mr;
1.11 noro 5928: UINT *d,*t,*s,*u;
1.1 noro 5929: NDV p;
5930: unsigned char *ivc;
5931: unsigned short *ivs;
5932: UINT *v,*ivi,*s0v;
1.11 noro 5933: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 5934: IndArray r;
5935:
5936: m = pair->mul;
5937: d = DL(m);
5938: if ( trace )
5939: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
5940: else
5941: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
5942:
5943: len = LEN(p);
5944: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
5945: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 5946: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
5947: ndl_add(d,DL(mr),t);
5948: st = prev;
5949: ed = n;
5950: while ( ed > st ) {
5951: md = (st+ed)/2;
5952: u = s0+md*nd_wpd;
5953: c = DL_COMPARE(u,t);
5954: if ( c == 0 ) break;
5955: else if ( c > 0 ) st = md;
5956: else ed = md;
5957: }
5958: prev = v[j] = md;
1.1 noro 5959: }
5960: r = (IndArray)MALLOC(sizeof(struct oIndArray));
5961: r->head = v[0];
5962: diff = 0;
5963: for ( i = 1; i < len; i++ ) {
5964: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
5965: }
5966: if ( diff < 256 ) {
5967: r->width = 1;
5968: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
5969: r->index.c = ivc;
5970: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
5971: } else if ( diff < 65536 ) {
5972: r->width = 2;
5973: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
5974: r->index.s = ivs;
5975: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
5976: } else {
5977: r->width = 4;
5978: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
5979: r->index.i = ivi;
5980: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
5981: }
5982: return r;
5983: }
5984:
5985: int compress_array(Z *svect,Z *cvect,int n)
5986: {
5987: int i,j;
5988:
5989: for ( i = j = 0; i < n; i++ )
5990: if ( svect[i] ) cvect[j++] = svect[i];
5991: return j;
5992: }
5993:
5994: void expand_array(Z *svect,Z *cvect,int n)
5995: {
5996: int i,j;
5997:
5998: for ( i = j = 0; j < n; i++ )
5999: if ( svect[i] ) svect[i] = cvect[j++];
6000: }
6001:
1.8 noro 6002: #if 0
1.1 noro 6003: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6004: {
6005: int i,j,k,len,pos,prev,nz;
6006: Z cs,mcs,c1,c2,cr,gcd,t;
6007: IndArray ivect;
6008: unsigned char *ivc;
6009: unsigned short *ivs;
6010: unsigned int *ivi;
6011: NDV redv;
6012: NMV mr;
6013: NODE rp;
6014: int maxrs;
6015: double hmag;
6016: Z *cvect;
1.3 noro 6017: int l;
1.1 noro 6018:
6019: maxrs = 0;
6020: for ( i = 0; i < col && !svect[i]; i++ );
6021: if ( i == col ) return maxrs;
6022: hmag = p_mag((P)svect[i])*nd_scale;
6023: cvect = (Z *)MALLOC(col*sizeof(Q));
6024: for ( i = 0; i < nred; i++ ) {
6025: ivect = imat[i];
6026: k = ivect->head;
6027: if ( svect[k] ) {
6028: maxrs = MAX(maxrs,rp0[i]->sugar);
6029: redv = nd_demand?ndv_load(rp0[i]->index)
6030: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6031: len = LEN(redv); mr = BDY(redv);
1.6 noro 6032: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6033: chsgnz(cs,&mcs);
6034: if ( !UNIQ(cr) ) {
6035: for ( j = 0; j < col; j++ ) {
6036: mulz(svect[j],cr,&c1); svect[j] = c1;
6037: }
6038: }
6039: svect[k] = 0; prev = k;
6040: switch ( ivect->width ) {
6041: case 1:
6042: ivc = ivect->index.c;
6043: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6044: pos = prev+ivc[j]; prev = pos;
1.6 noro 6045: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6046: }
6047: break;
6048: case 2:
6049: ivs = ivect->index.s;
6050: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6051: pos = prev+ivs[j]; prev = pos;
1.6 noro 6052: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6053: }
6054: break;
6055: case 4:
6056: ivi = ivect->index.i;
6057: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6058: pos = prev+ivi[j]; prev = pos;
1.6 noro 6059: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6060: }
6061: break;
6062: }
6063: for ( j = k+1; j < col && !svect[j]; j++ );
6064: if ( j == col ) break;
6065: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6066: nz = compress_array(svect,cvect,col);
6067: removecont_array((P *)cvect,nz,1);
6068: expand_array(svect,cvect,nz);
6069: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6070: }
6071: }
6072: }
6073: nz = compress_array(svect,cvect,col);
6074: removecont_array((P *)cvect,nz,1);
6075: expand_array(svect,cvect,nz);
6076: if ( DP_Print ) {
6077: fprintf(asir_out,"-"); fflush(asir_out);
6078: }
6079: return maxrs;
6080: }
1.4 noro 6081: #else
1.9 noro 6082:
1.4 noro 6083: /* direct mpz version */
6084: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6085: {
6086: int i,j,k,len,pos,prev;
6087: mpz_t cs,cr,gcd;
6088: IndArray ivect;
6089: unsigned char *ivc;
6090: unsigned short *ivs;
6091: unsigned int *ivi;
6092: NDV redv;
6093: NMV mr;
6094: NODE rp;
6095: int maxrs;
6096: double hmag;
6097: int l;
1.13 noro 6098: static mpz_t *svect;
6099: static int svect_len=0;
1.4 noro 6100:
6101: maxrs = 0;
6102: for ( i = 0; i < col && !svect0[i]; i++ );
6103: if ( i == col ) return maxrs;
6104: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 6105: if ( col > svect_len ) {
6106: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
6107: svect_len = col;
6108: }
1.4 noro 6109: for ( i = 0; i < col; i++ ) {
6110: mpz_init(svect[i]);
6111: if ( svect0[i] )
6112: mpz_set(svect[i],BDY(svect0[i]));
6113: else
6114: mpz_set_ui(svect[i],0);
6115: }
6116: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6117: for ( i = 0; i < nred; i++ ) {
6118: ivect = imat[i];
6119: k = ivect->head;
6120: if ( mpz_sgn(svect[k]) ) {
6121: maxrs = MAX(maxrs,rp0[i]->sugar);
6122: redv = nd_demand?ndv_load(rp0[i]->index)
6123: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6124: len = LEN(redv); mr = BDY(redv);
1.6 noro 6125: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6126: mpz_div(cs,svect[k],gcd);
1.6 noro 6127: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6128: mpz_neg(cs,cs);
1.9 noro 6129: if ( MUNIMPZ(cr) )
6130: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6131: else if ( !UNIMPZ(cr) )
6132: for ( j = 0; j < col; j++ ) {
6133: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6134: }
1.4 noro 6135: mpz_set_ui(svect[k],0);
6136: prev = k;
6137: switch ( ivect->width ) {
6138: case 1:
6139: ivc = ivect->index.c;
6140: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6141: pos = prev+ivc[j]; prev = pos;
1.6 noro 6142: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6143: }
6144: break;
6145: case 2:
6146: ivs = ivect->index.s;
6147: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6148: pos = prev+ivs[j]; prev = pos;
1.6 noro 6149: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6150: }
6151: break;
6152: case 4:
6153: ivi = ivect->index.i;
6154: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6155: pos = prev+ivi[j]; prev = pos;
1.6 noro 6156: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6157: }
6158: break;
6159: }
6160: for ( j = k+1; j < col && !svect[j]; j++ );
6161: if ( j == col ) break;
6162: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6163: mpz_removecont_array(svect,col);
6164: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6165: }
6166: }
6167: }
6168: mpz_removecont_array(svect,col);
6169: if ( DP_Print ) {
6170: fprintf(asir_out,"-"); fflush(asir_out);
6171: }
6172: for ( i = 0; i < col; i++ )
6173: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6174: else svect0[i] = 0;
6175: return maxrs;
6176: }
6177: #endif
1.1 noro 6178:
6179: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6180: {
6181: int i,j,k,len,pos,prev;
6182: UINT c,c1,c2,c3,up,lo,dmy;
6183: IndArray ivect;
6184: unsigned char *ivc;
6185: unsigned short *ivs;
6186: unsigned int *ivi;
6187: NDV redv;
6188: NMV mr;
6189: NODE rp;
6190: int maxrs;
6191:
6192: maxrs = 0;
6193: for ( i = 0; i < nred; i++ ) {
6194: ivect = imat[i];
6195: k = ivect->head; svect[k] %= m;
6196: if ( (c = svect[k]) != 0 ) {
6197: maxrs = MAX(maxrs,rp0[i]->sugar);
6198: c = m-c; redv = nd_ps[rp0[i]->index];
6199: len = LEN(redv); mr = BDY(redv);
6200: svect[k] = 0; prev = k;
6201: switch ( ivect->width ) {
6202: case 1:
6203: ivc = ivect->index.c;
6204: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6205: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6206: if ( c1 ) {
6207: c2 = svect[pos];
6208: DMA(c1,c,c2,up,lo);
6209: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6210: } else svect[pos] = lo;
6211: }
6212: }
6213: break;
6214: case 2:
6215: ivs = ivect->index.s;
6216: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6217: pos = prev+ivs[j]; c1 = CM(mr);
6218: prev = pos;
6219: if ( c1 ) {
6220: c2 = svect[pos];
6221: DMA(c1,c,c2,up,lo);
6222: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6223: } else svect[pos] = lo;
6224: }
6225: }
6226: break;
6227: case 4:
6228: ivi = ivect->index.i;
6229: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6230: pos = prev+ivi[j]; c1 = CM(mr);
6231: prev = pos;
6232: if ( c1 ) {
6233: c2 = svect[pos];
6234: DMA(c1,c,c2,up,lo);
6235: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6236: } else svect[pos] = lo;
6237: }
6238: }
6239: break;
6240: }
6241: }
6242: }
6243: for ( i = 0; i < col; i++ )
6244: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6245: return maxrs;
6246: }
6247:
6248: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6249: {
6250: int i,j,k,len,pos,prev;
6251: UINT c,c1,c2,c3,up,lo,dmy;
6252: IndArray ivect;
6253: unsigned char *ivc;
6254: unsigned short *ivs;
6255: unsigned int *ivi;
6256: NDV redv;
6257: NMV mr;
6258: NODE rp;
6259: int maxrs;
6260:
6261: maxrs = 0;
6262: for ( i = 0; i < nred; i++ ) {
6263: ivect = imat[i];
6264: k = ivect->head;
6265: if ( (c = svect[k]) != 0 ) {
6266: maxrs = MAX(maxrs,rp0[i]->sugar);
6267: c = _chsgnsf(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]; prev = pos;
6275: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6276: }
6277: break;
6278: case 2:
6279: ivs = ivect->index.s;
6280: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6281: pos = prev+ivs[j]; prev = pos;
6282: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6283: }
6284: break;
6285: case 4:
6286: ivi = ivect->index.i;
6287: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6288: pos = prev+ivi[j]; prev = pos;
6289: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6290: }
6291: break;
6292: }
6293: }
6294: }
6295: return maxrs;
6296: }
6297:
6298: ND nd_add_lf(ND p1,ND p2)
6299: {
6300: int n,c,can;
6301: ND r;
6302: NM m1,m2,mr0,mr,s;
6303: Z t;
6304:
6305: if ( !p1 ) return p2;
6306: else if ( !p2 ) return p1;
6307: else {
6308: can = 0;
6309: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6310: c = DL_COMPARE(DL(m1),DL(m2));
6311: switch ( c ) {
6312: case 0:
6313: addlf(CZ(m1),CZ(m2),&t);
6314: s = m1; m1 = NEXT(m1);
6315: if ( t ) {
6316: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6317: } else {
6318: can += 2; FREENM(s);
6319: }
6320: s = m2; m2 = NEXT(m2); FREENM(s);
6321: break;
6322: case 1:
6323: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6324: break;
6325: case -1:
6326: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6327: break;
6328: }
6329: }
6330: if ( !mr0 )
6331: if ( m1 ) mr0 = m1;
6332: else if ( m2 ) mr0 = m2;
6333: else return 0;
6334: else if ( m1 ) NEXT(mr) = m1;
6335: else if ( m2 ) NEXT(mr) = m2;
6336: else NEXT(mr) = 0;
6337: BDY(p1) = mr0;
6338: SG(p1) = MAX(SG(p1),SG(p2));
6339: LEN(p1) = LEN(p1)+LEN(p2)-can;
6340: FREEND(p2);
6341: return p1;
6342: }
6343: }
6344:
6345: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6346: {
6347: int i,j,k,len,pos,prev;
6348: mpz_t c,mc,c1;
6349: IndArray ivect;
6350: unsigned char *ivc;
6351: unsigned short *ivs;
6352: unsigned int *ivi;
6353: NDV redv;
6354: NMV mr;
6355: NODE rp;
6356: int maxrs;
6357:
6358: maxrs = 0;
6359: lf_lazy = 1;
6360: for ( i = 0; i < nred; i++ ) {
6361: ivect = imat[i];
6362: k = ivect->head;
6363: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6364: if ( mpz_sgn(svect[k]) ) {
6365: maxrs = MAX(maxrs,rp0[i]->sugar);
6366: mpz_neg(svect[k],svect[k]);
6367: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6368: len = LEN(redv); mr = BDY(redv);
6369: prev = k;
6370: switch ( ivect->width ) {
6371: case 1:
6372: ivc = ivect->index.c;
6373: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6374: pos = prev+ivc[j]; prev = pos;
6375: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6376: }
6377: break;
6378: case 2:
6379: ivs = ivect->index.s;
6380: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6381: pos = prev+ivs[j]; prev = pos;
6382: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6383: }
6384: break;
6385: case 4:
6386: ivi = ivect->index.i;
6387: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6388: pos = prev+ivi[j]; prev = pos;
6389: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6390: }
6391: break;
6392: }
6393: mpz_set_ui(svect[k],0);
6394: }
6395: }
6396: lf_lazy=0;
6397: for ( i = 0; i < col; i++ ) {
6398: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6399: }
6400: return maxrs;
6401: }
6402:
6403: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6404: {
6405: int i,j,k,l,rank,s;
6406: mpz_t a,a1,inv;
6407: mpz_t *t,*pivot,*pk;
6408: mpz_t **mat;
6409: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6410: int size,size1;
6411:
6412: mpz_init(inv);
6413: mpz_init(a);
6414: mat = (mpz_t **)mat0;
6415: size = 0;
6416: for ( rank = 0, j = 0; j < col; j++ ) {
6417: for ( i = rank; i < row; i++ ) {
6418: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6419: }
6420: for ( i = rank; i < row; i++ )
6421: if ( mpz_sgn(mat[i][j]) )
6422: break;
6423: if ( i == row ) {
6424: colstat[j] = 0;
6425: continue;
6426: } else
6427: colstat[j] = 1;
6428: if ( i != rank ) {
6429: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6430: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6431: }
6432: pivot = mat[rank];
6433: s = sugar[rank];
6434: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6435: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6436: if ( mpz_sgn(*pk) ) {
6437: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6438: }
6439: for ( i = rank+1; i < row; i++ ) {
6440: t = mat[i];
6441: if ( mpz_sgn(t[j]) ) {
6442: sugar[i] = MAX(sugar[i],s);
6443: mpz_neg(a,t[j]);
6444: red_by_vect_lf(t+j,pivot+j,a,col-j);
6445: }
6446: }
6447: rank++;
6448: }
6449: for ( j = col-1, l = rank-1; j >= 0; j-- )
6450: if ( colstat[j] ) {
6451: pivot = mat[l];
6452: s = sugar[l];
6453: for ( k = j; k < col; k++ )
6454: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6455: for ( i = 0; i < l; i++ ) {
6456: t = mat[i];
6457: if ( mpz_sgn(t[j]) ) {
6458: sugar[i] = MAX(sugar[i],s);
6459: mpz_neg(a,t[j]);
6460: red_by_vect_lf(t+j,pivot+j,a,col-j);
6461: }
6462: }
6463: l--;
6464: }
6465: for ( j = 0, l = 0; l < rank; j++ )
6466: if ( colstat[j] ) {
6467: t = mat[l];
6468: for ( k = j; k < col; k++ ) {
6469: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6470: }
6471: l++;
6472: }
6473: return rank;
6474: }
6475:
6476:
6477: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6478: {
6479: int j,k,len;
6480: UINT *p;
6481: UINT c;
6482: NDV r;
6483: NMV mr0,mr;
6484:
6485: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6486: if ( !len ) return 0;
6487: else {
6488: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6489: #if 0
6490: ndv_alloc += nmv_adv*len;
6491: #endif
6492: mr = mr0;
6493: p = s0vect;
6494: for ( j = k = 0; j < col; j++, p += nd_wpd )
6495: if ( !rhead[j] ) {
6496: if ( (c = vect[k++]) != 0 ) {
6497: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6498: }
6499: }
6500: MKNDV(nd_nvar,mr0,len,r);
6501: return r;
6502: }
6503: }
6504:
6505: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6506: {
6507: int j,k,len;
6508: UINT *p;
6509: NDV r;
6510: NMV mr0,mr;
6511:
6512: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6513: if ( !len ) return 0;
6514: else {
6515: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6516: mr = mr0;
6517: p = s0vect;
6518: for ( j = 0; j < col; j++, p += nd_wpd )
6519: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6520: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6521: }
6522: MKNDV(nd_nvar,mr0,len,r);
6523: return r;
6524: }
6525: }
6526:
6527: /* for preprocessed vector */
6528:
6529: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6530: {
1.6 noro 6531: int j,k,len;
6532: UINT *p;
6533: Z c;
6534: NDV r;
6535: NMV mr0,mr;
1.1 noro 6536:
1.6 noro 6537: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6538: if ( !len ) return 0;
6539: else {
6540: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6541: #if 0
1.6 noro 6542: ndv_alloc += nmv_adv*len;
1.1 noro 6543: #endif
1.6 noro 6544: mr = mr0;
6545: p = s0vect;
6546: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6547: if ( !rhead[j] ) {
6548: if ( (c = vect[k++]) != 0 ) {
6549: if ( !INT(c) )
6550: error("vect_to_ndv_q : components must be integers");
6551: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6552: }
6553: }
1.1 noro 6554: }
1.6 noro 6555: MKNDV(nd_nvar,mr0,len,r);
6556: return r;
6557: }
1.1 noro 6558: }
6559:
6560: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6561: {
6562: int j,k,len;
6563: UINT *p;
6564: mpz_t c;
6565: NDV r;
6566: NMV mr0,mr;
6567:
6568: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6569: if ( !len ) return 0;
6570: else {
6571: mr0 = (NMV)MALLOC(nmv_adv*len);
6572: #if 0
6573: ndv_alloc += nmv_adv*len;
6574: #endif
6575: mr = mr0;
6576: p = s0vect;
6577: for ( j = k = 0; j < col; j++, p += nd_wpd )
6578: if ( !rhead[j] ) {
6579: c[0] = vect[k++][0];
6580: if ( mpz_sgn(c) ) {
6581: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6582: }
6583: }
6584: MKNDV(nd_nvar,mr0,len,r);
6585: return r;
6586: }
6587: }
6588:
6589: /* for plain vector */
6590:
6591: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6592: {
6593: int j,k,len;
6594: UINT *p;
6595: Z c;
6596: NDV r;
6597: NMV mr0,mr;
6598:
6599: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6600: if ( !len ) return 0;
6601: else {
6602: mr0 = (NMV)MALLOC(nmv_adv*len);
6603: #if 0
6604: ndv_alloc += nmv_adv*len;
6605: #endif
6606: mr = mr0;
6607: p = s0vect;
6608: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6609: if ( (c = vect[k]) != 0 ) {
6610: if ( !INT(c) )
1.6 noro 6611: error("plain_vect_to_ndv_q : components must be integers");
6612: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6613: }
6614: MKNDV(nd_nvar,mr0,len,r);
6615: return r;
6616: }
6617: }
6618:
6619: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6620: {
6621: ND_pairs t;
6622: NODE sp0,sp;
6623: int stat;
6624: ND spol;
6625:
6626: for ( t = l; t; t = NEXT(t) ) {
6627: stat = nd_sp(m,trace,t,&spol);
6628: if ( !stat ) return 0;
6629: if ( spol ) {
6630: add_pbucket_symbolic(bucket,spol);
6631: }
6632: }
6633: return 1;
6634: }
6635:
6636: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6637: {
6638: NODE rp0,rp;
6639: NM mul,head,s0,s;
6640: int index,col,i,sugar;
6641: RHist h;
6642: UINT *s0v,*p;
6643: NM_ind_pair pair;
6644: ND red;
6645: NDV *ps;
6646:
6647: s0 = 0; rp0 = 0; col = 0;
6648: if ( nd_demand )
6649: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6650: else
6651: ps = trace?nd_ps_trace:nd_ps;
6652: while ( 1 ) {
6653: head = remove_head_pbucket_symbolic(bucket);
6654: if ( !head ) break;
6655: if ( !s0 ) s0 = head;
6656: else NEXT(s) = head;
6657: s = head;
6658: index = ndl_find_reducer(DL(head));
6659: if ( index >= 0 ) {
6660: h = nd_psh[index];
6661: NEWNM(mul);
6662: ndl_sub(DL(head),DL(h),DL(mul));
6663: if ( ndl_check_bound2(index,DL(mul)) )
6664: return 0;
6665: sugar = TD(DL(mul))+SG(ps[index]);
6666: MKNM_ind_pair(pair,mul,index,sugar);
6667: red = ndv_mul_nm_symbolic(mul,ps[index]);
6668: add_pbucket_symbolic(bucket,nd_remove_head(red));
6669: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6670: }
6671: col++;
6672: }
6673: if ( rp0 ) NEXT(rp) = 0;
6674: NEXT(s) = 0;
6675: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6676: for ( i = 0, p = s0v, s = s0; i < col;
6677: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6678: *s0vect = s0v;
6679: *r = rp0;
6680: return col;
6681: }
6682:
6683: void print_ndp(ND_pairs l)
6684: {
6685: ND_pairs t;
6686:
6687: for ( t = l; t; t = NEXT(t) )
6688: printf("[%d,%d] ",t->i1,t->i2);
6689: printf("\n");
6690: }
6691:
6692: NODE nd_f4(int m,int checkonly,int **indp)
6693: {
6694: int i,nh,stat,index,f4red;
6695: NODE r,g,tn0,tn,node;
6696: ND_pairs d,l,t,ll0,ll,lh;
6697: LIST l0,l1;
6698: ND spol,red;
6699: NDV nf,redv;
6700: NM s0,s;
6701: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6702: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6703: UINT c;
6704: UINT **spmat;
6705: UINT *s0vect,*svect,*p,*v;
6706: int *colstat;
6707: IndArray *imat;
6708: int *rhead;
6709: int spcol,sprow;
6710: int sugar,sugarh;
6711: PGeoBucket bucket;
6712: struct oEGT eg0,eg1,eg_f4;
6713: Z i1,i2,sugarq;
1.12 noro 6714:
6715: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6716: #if 0
6717: ndv_alloc = 0;
6718: #endif
1.11 noro 6719: Nf4_red=0;
1.1 noro 6720: g = 0; d = 0;
6721: for ( i = 0; i < nd_psn; i++ ) {
6722: d = update_pairs(d,g,i,0);
6723: g = update_base(g,i);
6724: }
6725: nzlist = 0;
6726: nzlist_t = nd_nzlist;
6727: f4red = 1;
6728: nd_last_nonzero = 0;
6729: while ( d ) {
6730: get_eg(&eg0);
6731: l = nd_minsugarp(d,&d);
6732: sugar = nd_sugarweight?l->sugar2:SG(l);
6733: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6734: if ( nzlist_t ) {
6735: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6736: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6737: tn = BDY((LIST)ARG1(node));
6738: if ( !tn ) {
6739: nzlist_t = NEXT(nzlist_t);
6740: continue;
6741: }
6742: /* tn = [[i1,i2],...] */
6743: lh = nd_ipairtospair(tn);
6744: }
6745: bucket = create_pbucket();
6746: stat = nd_sp_f4(m,0,l,bucket);
6747: if ( !stat ) {
6748: for ( t = l; NEXT(t); t = NEXT(t) );
6749: NEXT(t) = d; d = l;
6750: d = nd_reconstruct(0,d);
6751: continue;
6752: }
6753: if ( bucket->m < 0 ) continue;
6754: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6755: if ( !col ) {
6756: for ( t = l; NEXT(t); t = NEXT(t) );
6757: NEXT(t) = d; d = l;
6758: d = nd_reconstruct(0,d);
6759: continue;
6760: }
1.12 noro 6761: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6762: if ( DP_Print )
1.6 noro 6763: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6764: sugar,eg_f4.exectime);
1.1 noro 6765: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6766: if ( checkonly && nflist ) return 0;
6767: /* adding new bases */
6768: if ( nflist ) nd_last_nonzero = f4red;
6769: for ( r = nflist; r; r = NEXT(r) ) {
6770: nf = (NDV)BDY(r);
6771: ndv_removecont(m,nf);
6772: if ( !m && nd_nalg ) {
6773: ND nf1;
6774:
6775: nf1 = ndvtond(m,nf);
6776: nd_monic(0,&nf1);
6777: nd_removecont(m,nf1);
6778: nf = ndtondv(m,nf1);
6779: }
6780: nh = ndv_newps(m,nf,0,1);
6781: d = update_pairs(d,g,nh,0);
6782: g = update_base(g,nh);
6783: }
6784: if ( DP_Print ) {
6785: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6786: }
6787: if ( nd_gentrace ) {
6788: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6789: NEXTNODE(tn0,tn);
1.6 noro 6790: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6791: node = mknode(2,i1,i2); MKLIST(l0,node);
6792: BDY(tn) = l0;
6793: }
6794: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6795: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6796: MKNODE(node,l1,nzlist); nzlist = node;
6797: }
6798: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6799: f4red++;
6800: if ( nd_f4red && f4red > nd_f4red ) break;
6801: if ( nd_rank0 && !nflist ) break;
6802: }
6803: if ( nd_gentrace ) {
6804: MKLIST(l0,reverse_node(nzlist));
6805: MKNODE(nd_alltracelist,l0,0);
6806: }
6807: #if 0
6808: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6809: #endif
1.12 noro 6810: if ( DP_Print ) {
6811: fprintf(asir_out,"number of red=%d,",Nf4_red);
6812: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
6813: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
6814: }
1.1 noro 6815: conv_ilist(nd_demand,0,g,indp);
6816: return g;
6817: }
6818:
6819: NODE nd_f4_trace(int m,int **indp)
6820: {
6821: int i,nh,stat,index;
6822: NODE r,g;
6823: ND_pairs d,l,l0,t;
6824: ND spol,red;
6825: NDV nf,redv,nfqv,nfv;
6826: NM s0,s;
6827: NODE rp0,srp0,nflist;
6828: int nsp,nred,col,rank,len,k,j,a;
6829: UINT c;
6830: UINT **spmat;
6831: UINT *s0vect,*svect,*p,*v;
6832: int *colstat;
6833: IndArray *imat;
6834: int *rhead;
6835: int spcol,sprow;
6836: int sugar;
6837: PGeoBucket bucket;
6838: struct oEGT eg0,eg1,eg_f4;
6839:
6840: g = 0; d = 0;
6841: for ( i = 0; i < nd_psn; i++ ) {
6842: d = update_pairs(d,g,i,0);
6843: g = update_base(g,i);
6844: }
6845: while ( d ) {
6846: get_eg(&eg0);
6847: l = nd_minsugarp(d,&d);
6848: sugar = SG(l);
6849: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6850: bucket = create_pbucket();
6851: stat = nd_sp_f4(m,0,l,bucket);
6852: if ( !stat ) {
6853: for ( t = l; NEXT(t); t = NEXT(t) );
6854: NEXT(t) = d; d = l;
6855: d = nd_reconstruct(1,d);
6856: continue;
6857: }
6858: if ( bucket->m < 0 ) continue;
6859: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6860: if ( !col ) {
6861: for ( t = l; NEXT(t); t = NEXT(t) );
6862: NEXT(t) = d; d = l;
6863: d = nd_reconstruct(1,d);
6864: continue;
6865: }
6866: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
6867: if ( DP_Print )
1.3 noro 6868: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 6869: sugar,eg_f4.exectime);
1.1 noro 6870: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
6871: if ( !l0 ) continue;
6872: l = l0;
6873:
6874: /* over Q */
6875: bucket = create_pbucket();
6876: stat = nd_sp_f4(0,1,l,bucket);
6877: if ( !stat ) {
6878: for ( t = l; NEXT(t); t = NEXT(t) );
6879: NEXT(t) = d; d = l;
6880: d = nd_reconstruct(1,d);
6881: continue;
6882: }
6883: if ( bucket->m < 0 ) continue;
6884: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
6885: if ( !col ) {
6886: for ( t = l; NEXT(t); t = NEXT(t) );
6887: NEXT(t) = d; d = l;
6888: d = nd_reconstruct(1,d);
6889: continue;
6890: }
6891: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
6892: /* adding new bases */
6893: for ( r = nflist; r; r = NEXT(r) ) {
6894: nfqv = (NDV)BDY(r);
6895: ndv_removecont(0,nfqv);
1.6 noro 6896: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 6897: if ( nd_nalg ) {
6898: ND nf1;
6899:
6900: nf1 = ndvtond(m,nfqv);
6901: nd_monic(0,&nf1);
6902: nd_removecont(0,nf1);
6903: nfqv = ndtondv(0,nf1); nd_free(nf1);
6904: }
6905: nfv = ndv_dup(0,nfqv);
6906: ndv_mod(m,nfv);
6907: ndv_removecont(m,nfv);
6908: nh = ndv_newps(0,nfv,nfqv,1);
6909: d = update_pairs(d,g,nh,0);
6910: g = update_base(g,nh);
6911: }
6912: }
6913: #if 0
6914: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6915: #endif
6916: conv_ilist(nd_demand,1,g,indp);
6917: return g;
6918: }
6919:
6920: int rref(matrix mat,int *sugar)
6921: {
6922: int row,col,i,j,k,l,s,wcol,wj;
6923: unsigned long bj;
6924: unsigned long **a;
6925: unsigned long *ai,*ak,*as,*t;
6926: int *pivot;
6927:
6928: row = mat->row;
6929: col = mat->col;
6930: a = mat->a;
6931: wcol = (col+BLEN-1)/BLEN;
6932: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
6933: i = 0;
6934: for ( j = 0; j < col; j++ ) {
6935: wj = j/BLEN; bj = 1L<<(j%BLEN);
6936: for ( k = i; k < row; k++ )
6937: if ( a[k][wj] & bj ) break;
6938: if ( k == row ) continue;
6939: pivot[i] = j;
6940: if ( k != i ) {
6941: t = a[i]; a[i] = a[k]; a[k] = t;
6942: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
6943: }
6944: ai = a[i];
6945: for ( k = i+1; k < row; k++ ) {
6946: ak = a[k];
6947: if ( ak[wj] & bj ) {
6948: for ( l = wj; l < wcol; l++ )
6949: ak[l] ^= ai[l];
6950: sugar[k] = MAX(sugar[k],sugar[i]);
6951: }
6952: }
6953: i++;
6954: }
6955: for ( k = i-1; k >= 0; k-- ) {
6956: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
6957: ak = a[k];
6958: for ( s = 0; s < k; s++ ) {
6959: as = a[s];
6960: if ( as[wj] & bj ) {
6961: for ( l = wj; l < wcol; l++ )
6962: as[l] ^= ak[l];
6963: sugar[s] = MAX(sugar[s],sugar[k]);
6964: }
6965: }
6966: }
6967: return i;
6968: }
6969:
6970: void print_matrix(matrix mat)
6971: {
6972: int row,col,i,j;
6973: unsigned long *ai;
6974:
6975: row = mat->row;
6976: col = mat->col;
6977: printf("%d x %d\n",row,col);
6978: for ( i = 0; i < row; i++ ) {
6979: ai = mat->a[i];
6980: for ( j = 0; j < col; j++ ) {
6981: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
6982: else putchar('0');
6983: }
6984: putchar('\n');
6985: }
6986: }
6987:
6988: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
6989:
6990: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
6991: {
6992: int row,col,wcol,wj,i,j;
6993: unsigned long bj;
6994: unsigned long *ai;
6995: unsigned long **a;
6996: int len;
6997: int *pos;
6998:
6999: row = mat->row;
7000: col = mat->col;
7001: wcol = (col+BLEN-1)/BLEN;
7002: pos = (int *)MALLOC(wcol*sizeof(int));
7003: bzero(pos,wcol*sizeof(int));
7004: for ( i = j = 0; i < wcol; i++ )
7005: if ( v[i] ) pos[j++] = i;;
7006: len = j;
7007: wj = rhead/BLEN;
7008: bj = 1L<<rhead%BLEN;
7009: a = mat->a;
7010: for ( i = 0; i < row; i++ ) {
7011: ai = a[i];
7012: if ( ai[wj]&bj ) {
7013: for ( j = 0; j < len; j++ )
7014: ai[pos[j]] ^= v[pos[j]];
7015: sugar[i] = MAX(sugar[i],rsugar);
7016: }
7017: }
7018: }
7019:
7020: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7021: {
7022: int nsp,nred,i,i0,k,rank,row;
7023: NODE r0,rp;
7024: ND_pairs sp;
7025: ND spol;
7026: NM_ind_pair rt;
7027: int *s0hash;
7028: UINT *s;
7029: int *pivot,*sugar,*head;
7030: matrix mat;
7031: NM m;
7032: NODE r;
7033: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7034: int rhead,rsugar,size;
7035: unsigned long *v;
7036:
7037: get_eg(&eg0);
7038: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7039: nred = length(rp0);
7040: mat = alloc_matrix(nsp,col);
7041: s0hash = (int *)MALLOC(col*sizeof(int));
7042: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7043: s0hash[i] = ndl_hash_value(s);
7044:
7045: sugar = (int *)MALLOC(nsp*sizeof(int));
7046: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7047: nd_sp(2,0,sp,&spol);
7048: if ( spol ) {
7049: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7050: sugar[i] = SG(spol);
7051: i++;
7052: }
7053: }
7054: mat->row = i;
7055: if ( DP_Print ) {
7056: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7057: }
7058: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7059: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7060: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7061: rt = (NM_ind_pair)BDY(rp);
7062: bzero(v,size);
7063: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7064: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7065: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7066: }
7067:
7068: get_eg(&eg1);
7069: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7070: rank = rref(mat,sugar);
7071:
7072: for ( i = 0, r0 = 0; i < rank; i++ ) {
7073: NEXTNODE(r0,r);
7074: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7075: SG((NDV)BDY(r)) = sugar[i];
7076: }
7077: if ( r0 ) NEXT(r) = 0;
7078: get_eg(&eg2);
7079: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7080: if ( DP_Print ) {
7081: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7082: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7083: fflush(asir_out);
7084: }
7085: return r0;
7086: }
7087:
7088:
7089: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7090: {
7091: IndArray *imat;
1.11 noro 7092: int nsp,nred,i,start;
1.1 noro 7093: int *rhead;
7094: NODE r0,rp;
7095: ND_pairs sp;
7096: NM_ind_pair *rvect;
7097: UINT *s;
7098: int *s0hash;
1.11 noro 7099: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7100:
7101: if ( m == 2 && nd_rref2 )
7102: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7103:
7104: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7105: nred = length(rp0);
7106: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7107: rhead = (int *)MALLOC(col*sizeof(int));
7108: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7109:
7110: /* construction of index arrays */
1.11 noro 7111: get_eg(&eg0);
1.1 noro 7112: if ( DP_Print ) {
1.11 noro 7113: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7114: fflush(asir_out);
1.1 noro 7115: }
7116: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7117: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7118: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7119: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7120: rhead[imat[i]->head] = 1;
1.11 noro 7121: start = imat[i]->head;
7122: }
1.12 noro 7123: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7124: if ( DP_Print ) {
7125: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7126: fflush(asir_out);
1.1 noro 7127: }
7128: if ( m > 0 )
1.7 noro 7129: #if SIZEOF_LONG==8
1.1 noro 7130: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7131: #else
7132: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7133: #endif
7134: else if ( m == -1 )
7135: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7136: else if ( m == -2 )
7137: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7138: else
7139: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7140: return r0;
7141: }
7142:
7143: /* for Fp, 2<=p<2^16 */
7144:
7145: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7146: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7147: {
7148: int spcol,sprow,a;
7149: int i,j,k,l,rank;
7150: NODE r0,r;
7151: ND_pairs sp;
7152: ND spol;
7153: UINT **spmat;
7154: UINT *svect,*v;
7155: int *colstat;
7156: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7157: int maxrs;
7158: int *spsugar;
7159: ND_pairs *spactive;
7160:
7161: spcol = col-nred;
7162: get_eg(&eg0);
7163: /* elimination (1st step) */
7164: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7165: svect = (UINT *)MALLOC(col*sizeof(UINT));
7166: spsugar = (int *)MALLOC(nsp*sizeof(int));
7167: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7168: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7169: nd_sp(m,0,sp,&spol);
7170: if ( !spol ) continue;
7171: nd_to_vect(m,s0vect,col,spol,svect);
7172: if ( m == -1 )
7173: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7174: else
7175: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7176: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7177: if ( i < col ) {
7178: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7179: for ( j = k = 0; j < col; j++ )
7180: if ( !rhead[j] ) v[k++] = svect[j];
7181: spsugar[sprow] = MAX(maxrs,SG(spol));
7182: if ( nz )
7183: spactive[sprow] = sp;
7184: sprow++;
7185: }
7186: nd_free(spol);
7187: }
7188: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7189: if ( DP_Print ) {
1.5 noro 7190: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7191: fflush(asir_out);
7192: }
7193: /* free index arrays */
7194: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7195:
7196: /* elimination (2nd step) */
7197: colstat = (int *)MALLOC(spcol*sizeof(int));
7198: if ( m == -1 )
7199: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7200: else
7201: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7202: r0 = 0;
7203: for ( i = 0; i < rank; i++ ) {
7204: NEXTNODE(r0,r); BDY(r) =
7205: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7206: SG((NDV)BDY(r)) = spsugar[i];
7207: GCFREE(spmat[i]);
7208: }
7209: if ( r0 ) NEXT(r) = 0;
7210:
7211: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7212: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7213: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7214: if ( DP_Print ) {
1.5 noro 7215: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7216: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7217: nsp,nred,sprow,spcol,rank);
1.5 noro 7218: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7219: }
7220: if ( nz ) {
7221: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7222: if ( rank > 0 ) {
7223: NEXT(spactive[rank-1]) = 0;
7224: *nz = spactive[0];
7225: } else
7226: *nz = 0;
7227: }
7228: return r0;
7229: }
7230:
7231:
7232: /* for small finite fields */
7233:
7234: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7235: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7236: {
7237: int spcol,sprow,a;
7238: int i,j,k,l,rank;
7239: NODE r0,r;
7240: ND_pairs sp;
7241: ND spol;
7242: UINT **spmat;
7243: UINT *svect,*v;
7244: int *colstat;
7245: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7246: int maxrs;
7247: int *spsugar;
7248: ND_pairs *spactive;
7249:
7250: spcol = col-nred;
7251: get_eg(&eg0);
7252: /* elimination (1st step) */
7253: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7254: svect = (UINT *)MALLOC(col*sizeof(UINT));
7255: spsugar = (int *)MALLOC(nsp*sizeof(int));
7256: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7257: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7258: nd_sp(m,0,sp,&spol);
7259: if ( !spol ) continue;
7260: nd_to_vect(m,s0vect,col,spol,svect);
7261: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7262: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7263: if ( i < col ) {
7264: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7265: for ( j = k = 0; j < col; j++ )
7266: if ( !rhead[j] ) v[k++] = svect[j];
7267: spsugar[sprow] = MAX(maxrs,SG(spol));
7268: if ( nz )
7269: spactive[sprow] = sp;
7270: sprow++;
7271: }
7272: nd_free(spol);
7273: }
7274: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7275: if ( DP_Print ) {
1.5 noro 7276: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7277: fflush(asir_out);
7278: }
7279: /* free index arrays */
7280: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7281:
7282: /* elimination (2nd step) */
7283: colstat = (int *)MALLOC(spcol*sizeof(int));
7284: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7285: r0 = 0;
7286: for ( i = 0; i < rank; i++ ) {
7287: NEXTNODE(r0,r); BDY(r) =
7288: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7289: SG((NDV)BDY(r)) = spsugar[i];
7290: GCFREE(spmat[i]);
7291: }
7292: if ( r0 ) NEXT(r) = 0;
7293:
7294: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7295: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7296: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7297: if ( DP_Print ) {
1.5 noro 7298: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7299: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7300: nsp,nred,sprow,spcol,rank);
1.5 noro 7301: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7302: }
7303: if ( nz ) {
7304: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7305: if ( rank > 0 ) {
7306: NEXT(spactive[rank-1]) = 0;
7307: *nz = spactive[0];
7308: } else
7309: *nz = 0;
7310: }
7311: return r0;
7312: }
7313:
7314: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7315: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7316: {
7317: int spcol,sprow,a;
7318: int i,j,k,l,rank;
7319: NODE r0,r;
7320: ND_pairs sp;
7321: ND spol;
7322: mpz_t **spmat;
7323: mpz_t *svect,*v;
7324: int *colstat;
7325: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7326: int maxrs;
7327: int *spsugar;
7328: pointer *w;
7329:
7330: spcol = col-nred;
7331: get_eg(&eg0);
7332: /* elimination (1st step) */
7333: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7334: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7335: spsugar = (int *)MALLOC(nsp*sizeof(int));
7336: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7337: nd_sp(m,trace,sp,&spol);
7338: if ( !spol ) continue;
7339: nd_to_vect_lf(s0vect,col,spol,svect);
7340: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7341: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7342: if ( i < col ) {
7343: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7344: for ( j = k = 0; j < col; j++ )
7345: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7346: spsugar[sprow] = MAX(maxrs,SG(spol));
7347: sprow++;
7348: }
7349: /* nd_free(spol); */
7350: }
7351: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7352: if ( DP_Print ) {
1.5 noro 7353: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7354: fflush(asir_out);
7355: }
7356: /* free index arrays */
7357: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7358:
7359: /* elimination (2nd step) */
7360: colstat = (int *)MALLOC(spcol*sizeof(int));
7361: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7362: w = (pointer *)MALLOC(rank*sizeof(pointer));
7363: for ( i = 0; i < rank; i++ ) {
7364: #if 0
7365: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7366: SG((NDV)w[rank-i-1]) = spsugar[i];
7367: #else
7368: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7369: SG((NDV)w[i]) = spsugar[i];
7370: #endif
7371: /* GCFREE(spmat[i]); */
7372:
7373: }
7374: #if 0
7375: qsort(w,rank,sizeof(NDV),
7376: (int (*)(const void *,const void *))ndv_compare);
7377: #endif
7378: r0 = 0;
7379: for ( i = 0; i < rank; i++ ) {
7380: NEXTNODE(r0,r); BDY(r) = w[i];
7381: }
7382: if ( r0 ) NEXT(r) = 0;
7383:
7384: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7385: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7386: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7387: if ( DP_Print ) {
1.5 noro 7388: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7389: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7390: nsp,nred,sprow,spcol,rank);
1.5 noro 7391: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7392: }
7393: return r0;
7394: }
7395:
7396: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7397: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7398: {
7399: int spcol,sprow,a;
7400: int i,j,k,l,rank;
7401: NODE r0,r;
7402: ND_pairs sp;
7403: ND spol;
7404: Z **spmat;
7405: Z *svect,*v;
7406: int *colstat;
7407: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7408: int maxrs;
7409: int *spsugar;
7410: pointer *w;
7411:
7412: spcol = col-nred;
7413: get_eg(&eg0);
7414: /* elimination (1st step) */
7415: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7416: svect = (Z *)MALLOC(col*sizeof(Q));
7417: spsugar = (int *)MALLOC(nsp*sizeof(int));
7418: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7419: nd_sp(0,trace,sp,&spol);
7420: if ( !spol ) continue;
7421: nd_to_vect_q(s0vect,col,spol,svect);
7422: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7423: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7424: if ( i < col ) {
7425: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7426: for ( j = k = 0; j < col; j++ )
7427: if ( !rhead[j] ) v[k++] = svect[j];
7428: spsugar[sprow] = MAX(maxrs,SG(spol));
7429: sprow++;
7430: }
7431: /* nd_free(spol); */
7432: }
7433: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7434: if ( DP_Print ) {
1.5 noro 7435: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7436: fflush(asir_out);
7437: }
7438: /* free index arrays */
7439: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7440:
7441: /* elimination (2nd step) */
7442: colstat = (int *)MALLOC(spcol*sizeof(int));
7443: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7444: w = (pointer *)MALLOC(rank*sizeof(pointer));
7445: for ( i = 0; i < rank; i++ ) {
7446: #if 0
7447: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7448: SG((NDV)w[rank-i-1]) = spsugar[i];
7449: #else
7450: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7451: SG((NDV)w[i]) = spsugar[i];
7452: #endif
7453: /* GCFREE(spmat[i]); */
7454: }
7455: #if 0
7456: qsort(w,rank,sizeof(NDV),
7457: (int (*)(const void *,const void *))ndv_compare);
7458: #endif
7459: r0 = 0;
7460: for ( i = 0; i < rank; i++ ) {
7461: NEXTNODE(r0,r); BDY(r) = w[i];
7462: }
7463: if ( r0 ) NEXT(r) = 0;
7464:
7465: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7466: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7467: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7468: if ( DP_Print ) {
1.5 noro 7469: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7470: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7471: nsp,nred,sprow,spcol,rank);
1.5 noro 7472: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7473: }
7474: return r0;
7475: }
7476:
7477: FILE *nd_write,*nd_read;
7478:
7479: void nd_send_int(int a) {
7480: write_int(nd_write,(unsigned int *)&a);
7481: }
7482:
7483: void nd_send_intarray(int *p,int len) {
7484: write_intarray(nd_write,(unsigned int *)p,len);
7485: }
7486:
7487: int nd_recv_int() {
7488: int a;
7489:
7490: read_int(nd_read,(unsigned int *)&a);
7491: return a;
7492: }
7493:
7494: void nd_recv_intarray(int *p,int len) {
7495: read_intarray(nd_read,(unsigned int *)p,len);
7496: }
7497:
7498: void nd_send_ndv(NDV p) {
7499: int len,i;
7500: NMV m;
7501:
7502: if ( !p ) nd_send_int(0);
7503: else {
7504: len = LEN(p);
7505: nd_send_int(len);
7506: m = BDY(p);
7507: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7508: nd_send_int(CM(m));
7509: nd_send_intarray((int *)DL(m),nd_wpd);
7510: }
7511: }
7512: }
7513:
7514: void nd_send_nd(ND p) {
7515: int len,i;
7516: NM m;
7517:
7518: if ( !p ) nd_send_int(0);
7519: else {
7520: len = LEN(p);
7521: nd_send_int(len);
7522: m = BDY(p);
7523: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7524: nd_send_int(CM(m));
7525: nd_send_intarray((int *)DL(m),nd_wpd);
7526: }
7527: }
7528: }
7529:
7530: NDV nd_recv_ndv()
7531: {
7532: int len,i;
7533: NMV m,m0;
7534: NDV r;
7535:
7536: len = nd_recv_int();
7537: if ( !len ) return 0;
7538: else {
7539: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7540: #if 0
7541: ndv_alloc += len*nmv_adv;
7542: #endif
7543: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7544: CM(m) = nd_recv_int();
7545: nd_recv_intarray((int *)DL(m),nd_wpd);
7546: }
7547: MKNDV(nd_nvar,m0,len,r);
7548: return r;
7549: }
7550: }
7551:
7552: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7553: {
7554: int i,j,t,c,rank,inv;
7555: int *ci,*ri;
7556: Z dn;
7557: MAT m,nm;
7558:
7559: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7560: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7561: for ( i = 0; i < row; i++ )
7562: for ( j = 0; j < col; j++ )
7563: mat0[i][j] = 0;
7564: c = col-rank;
7565: for ( i = 0; i < rank; i++ ) {
7566: mat0[i][ri[i]] = dn;
7567: for ( j = 0; j < c; j++ )
7568: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7569: }
7570: return rank;
7571: }
7572:
7573: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7574: {
7575: int i,j,k,l,inv,a,rank,s;
7576: unsigned int *t,*pivot,*pk;
7577: unsigned int **mat;
7578: ND_pairs pair;
7579:
7580: mat = (unsigned int **)mat0;
7581: for ( rank = 0, j = 0; j < col; j++ ) {
7582: for ( i = rank; i < row; i++ )
7583: mat[i][j] %= md;
7584: for ( i = rank; i < row; i++ )
7585: if ( mat[i][j] )
7586: break;
7587: if ( i == row ) {
7588: colstat[j] = 0;
7589: continue;
7590: } else
7591: colstat[j] = 1;
7592: if ( i != rank ) {
7593: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7594: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7595: if ( spactive ) {
7596: pair = spactive[i]; spactive[i] = spactive[rank];
7597: spactive[rank] = pair;
7598: }
7599: }
7600: pivot = mat[rank];
7601: s = sugar[rank];
7602: inv = invm(pivot[j],md);
7603: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7604: if ( *pk ) {
7605: if ( *pk >= (unsigned int)md )
7606: *pk %= md;
7607: DMAR(*pk,inv,0,md,*pk)
7608: }
7609: for ( i = rank+1; i < row; i++ ) {
7610: t = mat[i];
7611: if ( (a = t[j]) != 0 ) {
7612: sugar[i] = MAX(sugar[i],s);
7613: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7614: }
7615: }
7616: rank++;
7617: }
7618: for ( j = col-1, l = rank-1; j >= 0; j-- )
7619: if ( colstat[j] ) {
7620: pivot = mat[l];
7621: s = sugar[l];
7622: for ( i = 0; i < l; i++ ) {
7623: t = mat[i];
7624: t[j] %= md;
7625: if ( (a = t[j]) != 0 ) {
7626: sugar[i] = MAX(sugar[i],s);
7627: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7628: }
7629: }
7630: l--;
7631: }
7632: for ( j = 0, l = 0; l < rank; j++ )
7633: if ( colstat[j] ) {
7634: t = mat[l];
7635: for ( k = j; k < col; k++ )
7636: if ( t[k] >= (unsigned int)md )
7637: t[k] %= md;
7638: l++;
7639: }
7640: return rank;
7641: }
7642:
7643:
1.7 noro 7644: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7645: {
1.7 noro 7646: int i,j,k,l,inv,a,rank,s;
7647: unsigned int *t,*pivot,*pk;
7648: unsigned int **mat;
7649:
7650: mat = (unsigned int **)mat0;
7651: for ( rank = 0, j = 0; j < col; j++ ) {
7652: for ( i = rank; i < row; i++ )
7653: if ( mat[i][j] )
7654: break;
7655: if ( i == row ) {
7656: colstat[j] = 0;
7657: continue;
7658: } else
7659: colstat[j] = 1;
7660: if ( i != rank ) {
7661: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7662: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7663: }
7664: pivot = mat[rank];
7665: s = sugar[rank];
7666: inv = _invsf(pivot[j]);
7667: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7668: if ( *pk )
7669: *pk = _mulsf(*pk,inv);
7670: for ( i = rank+1; i < row; i++ ) {
7671: t = mat[i];
7672: if ( (a = t[j]) != 0 ) {
7673: sugar[i] = MAX(sugar[i],s);
7674: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7675: }
7676: }
7677: rank++;
7678: }
7679: for ( j = col-1, l = rank-1; j >= 0; j-- )
7680: if ( colstat[j] ) {
7681: pivot = mat[l];
7682: s = sugar[l];
7683: for ( i = 0; i < l; i++ ) {
7684: t = mat[i];
7685: if ( (a = t[j]) != 0 ) {
7686: sugar[i] = MAX(sugar[i],s);
7687: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7688: }
7689: }
7690: l--;
7691: }
7692: return rank;
7693: }
1.1 noro 7694:
1.7 noro 7695: int ndv_ishomo(NDV p)
7696: {
7697: NMV m;
7698: int len,h;
1.1 noro 7699:
7700: if ( !p ) return 1;
7701: len = LEN(p);
7702: m = BDY(p);
7703: h = TD(DL(m));
7704: NMV_ADV(m);
7705: for ( len--; len; len--, NMV_ADV(m) )
7706: if ( TD(DL(m)) != h ) return 0;
7707: return 1;
7708: }
7709:
7710: void ndv_save(NDV p,int index)
7711: {
7712: FILE *s;
7713: char name[BUFSIZ];
7714: short id;
7715: int nv,sugar,len,n,i,td,e,j;
7716: NMV m;
7717: unsigned int *dl;
7718: int mpos;
7719:
7720: sprintf(name,"%s/%d",Demand,index);
7721: s = fopen(name,"w");
7722: savevl(s,0);
7723: if ( !p ) {
7724: saveobj(s,0);
7725: return;
7726: }
7727: id = O_DP;
7728: nv = NV(p);
7729: sugar = SG(p);
7730: len = LEN(p);
7731: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7732: write_int(s,(unsigned int *)&len);
7733:
7734: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7735: saveobj(s,(Obj)CZ(m));
1.1 noro 7736: dl = DL(m);
7737: td = TD(dl);
7738: write_int(s,(unsigned int *)&td);
7739: for ( j = 0; j < nv; j++ ) {
7740: e = GET_EXP(dl,j);
7741: write_int(s,(unsigned int *)&e);
7742: }
7743: if ( nd_module ) {
7744: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7745: }
7746: }
7747: fclose(s);
7748: }
7749:
7750: void nd_save_mod(ND p,int index)
7751: {
7752: FILE *s;
7753: char name[BUFSIZ];
7754: int nv,sugar,len,c;
7755: NM m;
7756:
7757: sprintf(name,"%s/%d",Demand,index);
7758: s = fopen(name,"w");
7759: if ( !p ) {
7760: len = 0;
7761: write_int(s,(unsigned int *)&len);
7762: fclose(s);
7763: return;
7764: }
7765: nv = NV(p);
7766: sugar = SG(p);
7767: len = LEN(p);
7768: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7769: for ( m = BDY(p); m; m = NEXT(m) ) {
7770: c = CM(m); write_int(s,(unsigned int *)&c);
7771: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7772: }
7773: fclose(s);
7774: }
7775:
7776: NDV ndv_load(int index)
7777: {
7778: FILE *s;
7779: char name[BUFSIZ];
7780: short id;
7781: int nv,sugar,len,n,i,td,e,j;
7782: NDV d;
7783: NMV m0,m;
7784: unsigned int *dl;
7785: Obj obj;
7786: int mpos;
7787:
7788: sprintf(name,"%s/%d",Demand,index);
7789: s = fopen(name,"r");
7790: if ( !s ) return 0;
7791:
7792: skipvl(s);
7793: read_short(s,(unsigned short *)&id);
7794: if ( !id ) return 0;
7795: read_int(s,(unsigned int *)&nv);
7796: read_int(s,(unsigned int *)&sugar);
7797: read_int(s,(unsigned int *)&len);
7798:
7799: m0 = m = MALLOC(len*nmv_adv);
7800: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7801: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7802: dl = DL(m);
7803: ndl_zero(dl);
7804: read_int(s,(unsigned int *)&td); TD(dl) = td;
7805: for ( j = 0; j < nv; j++ ) {
7806: read_int(s,(unsigned int *)&e);
7807: PUT_EXP(dl,j,e);
7808: }
7809: if ( nd_module ) {
7810: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7811: }
7812: if ( nd_blockmask ) ndl_weight_mask(dl);
7813: }
7814: fclose(s);
7815: MKNDV(nv,m0,len,d);
7816: SG(d) = sugar;
7817: return d;
7818: }
7819:
7820: ND nd_load_mod(int index)
7821: {
7822: FILE *s;
7823: char name[BUFSIZ];
7824: int nv,sugar,len,i,c;
7825: ND d;
7826: NM m0,m;
7827:
7828: sprintf(name,"%s/%d",Demand,index);
7829: s = fopen(name,"r");
7830: /* if the file does not exist, it means p[index]=0 */
7831: if ( !s ) return 0;
7832:
7833: read_int(s,(unsigned int *)&nv);
7834: if ( !nv ) { fclose(s); return 0; }
7835:
7836: read_int(s,(unsigned int *)&sugar);
7837: read_int(s,(unsigned int *)&len);
7838: for ( m0 = 0, i = 0; i < len; i++ ) {
7839: NEXTNM(m0,m);
7840: read_int(s,(unsigned int *)&c); CM(m) = c;
7841: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7842: }
7843: NEXT(m) = 0;
7844: MKND(nv,m0,len,d);
7845: SG(d) = sugar;
7846: fclose(s);
7847: return d;
7848: }
7849:
7850: void nd_det(int mod,MAT f,P *rp)
7851: {
7852: VL fv,tv;
7853: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7854: pointer **m;
7855: P **w;
7856: P mp,r;
7857: NDV **dm;
7858: NDV *t,*mi,*mj;
7859: NDV d,s,mij,mjj;
7860: ND u;
7861: NMV nmv;
7862: UINT *bound;
7863: PGeoBucket bucket;
7864: struct order_spec *ord;
7865: Z dq,dt,ds;
7866: Z mone;
7867: Z gn,qn,dn0,nm,dn;
7868:
7869: create_order_spec(0,0,&ord);
7870: nd_init_ord(ord);
7871: get_vars((Obj)f,&fv);
7872: if ( f->row != f->col )
7873: error("nd_det : non-square matrix");
7874: n = f->row;
7875: m = f->body;
7876: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
7877:
7878: if ( !nvar ) {
7879: if ( !mod )
7880: detp(CO,(P **)m,n,rp);
7881: else {
7882: w = (P **)almat_pointer(n,n);
7883: for ( i = 0; i < n; i++ )
7884: for ( j = 0; j < n; j++ )
7885: ptomp(mod,(P)m[i][j],&w[i][j]);
7886: detmp(CO,mod,w,n,&mp);
7887: mptop(mp,rp);
7888: }
7889: return;
7890: }
7891:
7892: if ( !mod ) {
7893: w = (P **)almat_pointer(n,n);
7894: dq = ONE;
7895: for ( i = 0; i < n; i++ ) {
7896: dn0 = ONE;
7897: for ( j = 0; j < n; j++ ) {
7898: if ( !m[i][j] ) continue;
7899: lgp(m[i][j],&nm,&dn);
1.6 noro 7900: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 7901: }
7902: if ( !UNIZ(dn0) ) {
7903: ds = dn0;
7904: for ( j = 0; j < n; j++ )
7905: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
7906: mulz(dq,ds,&dt); dq = dt;
7907: } else
7908: for ( j = 0; j < n; j++ )
7909: w[i][j] = (P)m[i][j];
7910: }
7911: m = (pointer **)w;
7912: }
7913:
7914: for ( i = 0, max = 1; i < n; i++ )
7915: for ( j = 0; j < n; j++ )
7916: for ( tv = fv; tv; tv = NEXT(tv) ) {
7917: e = getdeg(tv->v,(P)m[i][j]);
7918: max = MAX(e,max);
7919: }
7920: nd_setup_parameters(nvar,max);
7921: dm = (NDV **)almat_pointer(n,n);
7922: for ( i = 0, max = 1; i < n; i++ )
7923: for ( j = 0; j < n; j++ ) {
7924: dm[i][j] = ptondv(CO,fv,m[i][j]);
7925: if ( mod ) ndv_mod(mod,dm[i][j]);
7926: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
7927: }
7928: d = ptondv(CO,fv,(P)ONE);
7929: if ( mod ) ndv_mod(mod,d);
7930: chsgnz(ONE,&mone);
7931: for ( j = 0, sgn = 1; j < n; j++ ) {
7932: if ( DP_Print ) {
7933: fprintf(asir_out,".");
7934: }
7935: for ( i = j; i < n && !dm[i][j]; i++ );
7936: if ( i == n ) {
7937: *rp = 0;
7938: return;
7939: }
7940: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
7941: for ( k = j; k < n; k++ )
7942: for ( l = j; l < n; l++ )
7943: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
7944: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
7945: }
7946: if ( k0 != j ) {
7947: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
7948: sgn = -sgn;
7949: }
7950: if ( l0 != j ) {
7951: for ( k = j; k < n; k++ ) {
7952: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
7953: }
7954: sgn = -sgn;
7955: }
7956: bound = nd_det_compute_bound(dm,n,j);
7957: for ( k = 0; k < nd_nvar; k++ )
7958: if ( bound[k]*2 > nd_mask0 ) break;
7959: if ( k < nd_nvar )
7960: nd_det_reconstruct(dm,n,j,d);
7961:
7962: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
7963: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
7964: mi = dm[i]; mij = mi[j];
7965: if ( mod )
7966: ndv_mul_c(mod,mij,mod-1);
7967: else
7968: ndv_mul_c_q(mij,mone);
7969: for ( k = j+1; k < n; k++ ) {
7970: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
7971: bucket = create_pbucket();
7972: if ( mi[k] ) {
7973: nmv = BDY(mjj); len = LEN(mjj);
7974: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7975: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
7976: add_pbucket(mod,bucket,u);
7977: }
7978: }
7979: if ( mj[k] && mij ) {
7980: nmv = BDY(mij); len = LEN(mij);
7981: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
7982: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
7983: add_pbucket(mod,bucket,u);
7984: }
7985: }
7986: u = nd_quo(mod,bucket,d);
7987: mi[k] = ndtondv(mod,u);
7988: }
7989: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
7990: }
7991: d = mjj;
7992: }
7993: if ( DP_Print ) {
7994: fprintf(asir_out,"\n");
7995: }
7996: if ( sgn < 0 ) {
7997: if ( mod )
7998: ndv_mul_c(mod,d,mod-1);
7999: else
8000: ndv_mul_c_q(d,mone);
8001: }
8002: r = ndvtop(mod,CO,fv,d);
8003: if ( !mod && !UNIQ(dq) )
8004: divsp(CO,r,(P)dq,rp);
8005: else
8006: *rp = r;
8007: }
8008:
8009: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8010: {
8011: NM mr,mr0;
8012: NM tnm;
8013: NMV m;
8014: UINT *d0,*dt,*dm;
8015: int c,n,td,i,c1,c2,len;
8016: Z q;
8017: ND r;
8018:
8019: if ( !p ) return 0;
8020: else {
8021: n = NV(p); m = BDY(p); len = LEN(p);
8022: d0 = DL(m0);
8023: td = TD(d);
8024: mr0 = 0;
8025: NEWNM(tnm);
8026: if ( mod ) {
8027: c = CM(m0);
8028: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8029: ndl_add(DL(m),d0,DL(tnm));
8030: if ( ndl_reducible(DL(tnm),d) ) {
8031: NEXTNM(mr0,mr);
8032: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8033: ndl_copy(DL(tnm),DL(mr));
8034: }
8035: }
8036: } else {
1.6 noro 8037: q = CZ(m0);
1.1 noro 8038: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8039: ndl_add(DL(m),d0,DL(tnm));
8040: if ( ndl_reducible(DL(tnm),d) ) {
8041: NEXTNM(mr0,mr);
1.6 noro 8042: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8043: ndl_copy(DL(tnm),DL(mr));
8044: }
8045: }
8046: }
8047: if ( !mr0 )
8048: return 0;
8049: else {
8050: NEXT(mr) = 0;
8051: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8052: MKND(NV(p),mr0,len,r);
8053: SG(r) = SG(p) + TD(d0);
8054: return r;
8055: }
8056: }
8057: }
8058:
8059: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8060: {
8061: int i,obpe,oadv,h,k,l;
8062: static NM prev_nm_free_list;
8063: EPOS oepos;
8064:
8065: obpe = nd_bpe;
8066: oadv = nmv_adv;
8067: oepos = nd_epos;
8068: if ( obpe < 2 ) nd_bpe = 2;
8069: else if ( obpe < 3 ) nd_bpe = 3;
8070: else if ( obpe < 4 ) nd_bpe = 4;
8071: else if ( obpe < 5 ) nd_bpe = 5;
8072: else if ( obpe < 6 ) nd_bpe = 6;
8073: else if ( obpe < 8 ) nd_bpe = 8;
8074: else if ( obpe < 10 ) nd_bpe = 10;
8075: else if ( obpe < 16 ) nd_bpe = 16;
8076: else if ( obpe < 32 ) nd_bpe = 32;
8077: else error("nd_det_reconstruct : exponent too large");
8078:
8079: nd_setup_parameters(nd_nvar,0);
8080: prev_nm_free_list = _nm_free_list;
8081: _nm_free_list = 0;
8082: for ( k = j; k < n; k++ )
8083: for (l = j; l < n; l++ )
8084: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8085: ndv_realloc(d,obpe,oadv,oepos);
8086: prev_nm_free_list = 0;
8087: #if 0
8088: GC_gcollect();
8089: #endif
8090: }
8091:
8092: /* returns a UINT array containing degree bounds */
8093:
8094: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8095: {
8096: UINT *d0,*d1,*d,*t,*r;
8097: int k,l,i;
8098:
8099: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8100: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8101: for ( k = j; k < n; k++ )
8102: for ( l = j; l < n; l++ )
8103: if ( dm[k][l] ) {
8104: d = ndv_compute_bound(dm[k][l]);
8105: for ( i = 0; i < nd_nvar; i++ )
8106: d0[i] = MAX(d0[i],d[i]);
8107: }
8108: return d0;
8109: }
8110:
8111: DL nd_separate_d(UINT *d,UINT *trans)
8112: {
8113: int n,td,i,e,j;
8114: DL a;
8115:
8116: ndl_zero(trans);
8117: td = 0;
8118: for ( i = 0; i < nd_ntrans; i++ ) {
8119: e = GET_EXP(d,i);
8120: PUT_EXP(trans,i,e);
8121: td += MUL_WEIGHT(e,i);
8122: }
8123: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8124: /* homogenized */
8125: i = nd_nvar-1;
8126: e = GET_EXP(d,i);
8127: PUT_EXP(trans,i,e);
8128: td += MUL_WEIGHT(e,i);
8129: }
8130: TD(trans) = td;
8131: if ( nd_blockmask) ndl_weight_mask(trans);
8132: NEWDL(a,nd_nalg);
8133: td = 0;
8134: for ( i = 0; i < nd_nalg; i++ ) {
8135: j = nd_ntrans+i;
8136: e = GET_EXP(d,j);
8137: a->d[i] = e;
8138: td += e;
8139: }
8140: a->td = td;
8141: return a;
8142: }
8143:
8144: int nd_monic(int mod,ND *p)
8145: {
8146: UINT *trans,*t;
8147: DL alg;
8148: MP mp0,mp;
8149: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8150: ND r;
8151: DL dl;
8152: DP nm;
8153: NDV ndv;
8154: DAlg inv,cd;
8155: ND s,c;
8156: Z l,mul;
8157: Z ln;
8158: int n,ntrans,i,e,td,is_lc,len;
8159: NumberField nf;
8160: struct oEGT eg0,eg1;
8161:
8162: if ( !(nf = get_numberfield()) )
8163: error("nd_monic : current_numberfield is not set");
8164:
8165: /* Q coef -> DAlg coef */
8166: NEWNM(ma0); ma = ma0;
8167: m = BDY(*p);
8168: is_lc = 1;
8169: while ( 1 ) {
8170: NEWMP(mp0); mp = mp0;
1.6 noro 8171: mp->c = (Obj)CZ(m);
1.1 noro 8172: mp->dl = nd_separate_d(DL(m),DL(ma));
8173: NEWNM(mb);
8174: for ( m = NEXT(m); m; m = NEXT(m) ) {
8175: alg = nd_separate_d(DL(m),DL(mb));
8176: if ( !ndl_equal(DL(ma),DL(mb)) )
8177: break;
1.6 noro 8178: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8179: }
8180: NEXT(mp) = 0;
8181: MKDP(nd_nalg,mp0,nm);
8182: MKDAlg(nm,ONE,cd);
8183: if ( is_lc == 1 ) {
8184: /* if the lc is a rational number, we have nothing to do */
8185: if ( !mp0->dl->td )
8186: return 1;
8187:
8188: get_eg(&eg0);
8189: invdalg(cd,&inv);
8190: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8191: /* check the validity of inv */
8192: if ( mod && !remqi((Q)inv->dn,mod) )
8193: return 0;
8194: CA(ma) = nf->one;
8195: is_lc = 0;
8196: ln = ONE;
8197: } else {
8198: muldalg(cd,inv,&CA(ma));
8199: lcmz(ln,CA(ma)->dn,&ln);
8200: }
8201: if ( m ) {
8202: NEXT(ma) = mb; ma = mb;
8203: } else {
8204: NEXT(ma) = 0;
8205: break;
8206: }
8207: }
8208: /* l = lcm(denoms) */
8209: l = ln;
8210: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8211: divsz(l,CA(m)->dn,&mul);
1.1 noro 8212: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8213: NEXTNM(mr0,mr);
1.6 noro 8214: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8215: dl = mp->dl;
8216: td = TD(DL(m));
8217: ndl_copy(DL(m),DL(mr));
8218: for ( i = 0; i < nd_nalg; i++ ) {
8219: e = dl->d[i];
8220: PUT_EXP(DL(mr),i+nd_ntrans,e);
8221: td += MUL_WEIGHT(e,i+nd_ntrans);
8222: }
8223: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8224: TD(DL(mr)) = td;
8225: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8226: }
8227: }
8228: NEXT(mr) = 0;
8229: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8230: MKND(NV(*p),mr0,len,r);
8231: /* XXX */
8232: SG(r) = SG(*p);
8233: nd_free(*p);
8234: *p = r;
8235: return 1;
8236: }
8237:
8238: NODE reverse_node(NODE n)
8239: {
8240: NODE t,t1;
8241:
8242: for ( t = 0; n; n = NEXT(n) ) {
8243: MKNODE(t1,BDY(n),t); t = t1;
8244: }
8245: return t;
8246: }
8247:
8248: P ndc_div(int mod,union oNDC a,union oNDC b)
8249: {
8250: union oNDC c;
8251: int inv,t;
8252:
8253: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8254: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8255: else if ( mod ) {
8256: inv = invm(b.m,mod);
8257: DMAR(a.m,inv,0,mod,t); c.m = t;
8258: } else if ( nd_vc )
8259: divsp(nd_vc,a.p,b.p,&c.p);
8260: else
8261: divsz(a.z,b.z,&c.z);
8262: return ndctop(mod,c);
8263: }
8264:
8265: P ndctop(int mod,union oNDC c)
8266: {
8267: Z q;
8268: int e;
8269: GFS gfs;
8270:
8271: if ( mod == -1 ) {
8272: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8273: } else if ( mod == -2 ) {
1.10 noro 8274: q = c.z; return (P)q;
1.1 noro 8275: } else if ( mod > 0 ) {
1.6 noro 8276: STOZ(c.m,q); return (P)q;
1.1 noro 8277: } else
8278: return (P)c.p;
8279: }
8280:
8281: /* [0,0,0,cont] = p -> p/cont */
8282:
8283: void finalize_tracelist(int i,P cont)
8284: {
8285: LIST l;
8286: NODE node;
8287: Z iq;
8288:
8289: if ( !UNIQ(cont) ) {
8290: node = mknode(4,NULLP,NULLP,NULLP,cont);
8291: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8292: nd_tracelist = node;
8293: }
1.6 noro 8294: STOZ(i,iq);
1.1 noro 8295: nd_tracelist = reverse_node(nd_tracelist);
8296: MKLIST(l,nd_tracelist);
8297: node = mknode(2,iq,l); MKLIST(l,node);
8298: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8299: nd_alltracelist = node; nd_tracelist = 0;
8300: }
8301:
8302: void conv_ilist(int demand,int trace,NODE g,int **indp)
8303: {
8304: int n,i,j;
8305: int *ind;
8306: NODE t;
8307:
8308: n = length(g);
8309: ind = (int *)MALLOC(n*sizeof(int));
8310: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8311: j = (long)BDY(t); ind[i] = j;
8312: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8313: }
8314: if ( indp ) *indp = ind;
8315: }
8316:
8317: void parse_nd_option(NODE opt)
8318: {
8319: NODE t,p,u;
8320: int i,s,n;
8321: char *key;
8322: Obj value;
8323:
8324: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8325: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8326: nd_splist = 0; nd_check_splist = 0;
8327: nd_sugarweight = 0;
8328: nd_f4red =0;
8329: nd_rank0 = 0;
8330: for ( t = opt; t; t = NEXT(t) ) {
8331: p = BDY((LIST)BDY(t));
8332: key = BDY((STRING)BDY(p));
8333: value = (Obj)BDY(NEXT(p));
8334: if ( !strcmp(key,"gentrace") )
8335: nd_gentrace = value?1:0;
8336: else if ( !strcmp(key,"gensyz") )
8337: nd_gensyz = value?1:0;
8338: else if ( !strcmp(key,"nora") )
8339: nd_nora = value?1:0;
8340: else if ( !strcmp(key,"gbblock") ) {
8341: if ( value && OID(value) == O_LIST ) {
8342: u = BDY((LIST)value);
8343: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8344: for ( i = 0; u; u = NEXT(u) ) {
8345: p = BDY((LIST)BDY(u));
1.6 noro 8346: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8347: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8348: }
8349: nd_gbblock[i] = -1;
8350: } else
8351: nd_gbblock = 0;
8352: } else if ( !strcmp(key,"newelim") )
8353: nd_newelim = value?1:0;
8354: else if ( !strcmp(key,"intersect") )
8355: nd_intersect = value?1:0;
8356: else if ( !strcmp(key,"lf") )
8357: nd_lf = value?1:0;
8358: else if ( !strcmp(key,"trace") ) {
8359: if ( value ) {
8360: u = BDY((LIST)value);
8361: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8362: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8363: }
8364: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8365: nd_f4red = ZTOS((Q)value);
1.1 noro 8366: } else if ( !strcmp(key,"rank0") ) {
8367: nd_rank0 = value?1:0;
8368: } else if ( !strcmp(key,"splist") ) {
8369: nd_splist = value?1:0;
8370: } else if ( !strcmp(key,"check_splist") ) {
8371: nd_check_splist = BDY((LIST)value);
8372: } else if ( !strcmp(key,"sugarweight") ) {
8373: u = BDY((LIST)value);
8374: n = length(u);
8375: nd_sugarweight = MALLOC(n*sizeof(int));
8376: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8377: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8378: }
8379: }
8380: }
8381:
8382: ND mdptond(DP d);
8383: ND nd_mul_nm(int mod,NM m0,ND p);
8384: ND nd_mul_nm_lf(NM m0,ND p);
8385: ND *btog(NODE ti,ND **p,int nb,int mod);
8386: ND btog_one(NODE ti,ND *p,int nb,int mod);
8387: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8388: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8389:
8390: /* d:monomial */
8391: ND mdptond(DP d)
8392: {
8393: NM m;
8394: ND r;
8395:
8396: if ( OID(d) == 1 )
8397: r = ptond(CO,CO,(P)d);
8398: else {
8399: NEWNM(m);
8400: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8401: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8402: NEXT(m) = 0;
8403: MKND(NV(d),m,1,r);
8404: }
8405: return r;
8406: }
8407:
8408: ND nd_mul_nm(int mod,NM m0,ND p)
8409: {
8410: UINT *d0;
8411: int c0,c1,c;
8412: NM tm,mr,mr0;
8413: ND r;
8414:
8415: if ( !p ) return 0;
8416: d0 = DL(m0);
8417: c0 = CM(m0);
8418: mr0 = 0;
8419: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8420: NEXTNM(mr0,mr);
8421: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8422: ndl_add(d0,DL(tm),DL(mr));
8423: }
8424: NEXT(mr) = 0;
8425: MKND(NV(p),mr0,LEN(p),r);
8426: return r;
8427: }
8428:
8429: ND nd_mul_nm_lf(NM m0,ND p)
8430: {
8431: UINT *d0;
8432: Z c0,c1,c;
8433: NM tm,mr,mr0;
8434: ND r;
8435:
8436: if ( !p ) return 0;
8437: d0 = DL(m0);
8438: c0 = CZ(m0);
8439: mr0 = 0;
8440: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8441: NEXTNM(mr0,mr);
8442: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8443: ndl_add(d0,DL(tm),DL(mr));
8444: }
8445: NEXT(mr) = 0;
8446: MKND(NV(p),mr0,LEN(p),r);
8447: return r;
8448: }
8449:
8450: ND *btog(NODE ti,ND **p,int nb,int mod)
8451: {
8452: PGeoBucket *r;
8453: int i,ci;
8454: NODE t,s;
8455: ND m,tp;
8456: ND *pi,*rd;
8457: P c;
8458:
8459: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8460: for ( i = 0; i < nb; i++ )
8461: r[i] = create_pbucket();
8462: for ( t = ti; t; t = NEXT(t) ) {
8463: s = BDY((LIST)BDY(t));
8464: if ( ARG0(s) ) {
8465: m = mdptond((DP)ARG2(s));
1.6 noro 8466: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8467: if ( (ci = ((MQ)c)->cont) != 0 ) {
8468: HCM(m) = ci;
1.6 noro 8469: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8470: for ( i = 0; i < nb; i++ ) {
8471: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8472: add_pbucket(mod,r[i],tp);
8473: }
8474: }
8475: ci = 1;
8476: } else {
8477: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8478: ci = invm(ci,mod);
8479: }
8480: }
8481: rd = (ND *)MALLOC(nb*sizeof(ND));
8482: for ( i = 0; i < nb; i++ )
8483: rd[i] = normalize_pbucket(mod,r[i]);
8484: if ( ci != 1 )
8485: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8486: return rd;
8487: }
8488:
8489: /* YYY */
8490: ND *btog_lf(NODE ti,ND **p,int nb)
8491: {
8492: PGeoBucket *r;
8493: int i;
8494: NODE t,s;
8495: ND m,tp;
8496: ND *pi,*rd;
8497: LM lm;
8498: Z lf,c;
8499:
8500: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8501: for ( i = 0; i < nb; i++ )
8502: r[i] = create_pbucket();
8503: for ( t = ti; t; t = NEXT(t) ) {
8504: s = BDY((LIST)BDY(t));
8505: if ( ARG0(s) ) {
8506: m = mdptond((DP)ARG2(s));
1.6 noro 8507: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8508: if ( lm ) {
8509: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8510: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8511: for ( i = 0; i < nb; i++ ) {
8512: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8513: add_pbucket(-2,r[i],tp);
8514: }
8515: }
8516: c = ONE;
8517: } else {
8518: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8519: }
8520: }
8521: rd = (ND *)MALLOC(nb*sizeof(ND));
8522: for ( i = 0; i < nb; i++ )
8523: rd[i] = normalize_pbucket(-2,r[i]);
8524: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8525: return rd;
8526: }
8527:
8528: ND btog_one(NODE ti,ND *p,int nb,int mod)
8529: {
8530: PGeoBucket r;
8531: int i,ci,j;
8532: NODE t,s;
8533: ND m,tp;
8534: ND pi,rd;
8535: P c;
8536:
8537: r = create_pbucket();
8538: for ( t = ti; t; t = NEXT(t) ) {
8539: s = BDY((LIST)BDY(t));
8540: if ( ARG0(s) ) {
8541: m = mdptond((DP)ARG2(s));
1.6 noro 8542: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8543: if ( (ci = ((MQ)c)->cont) != 0 ) {
8544: HCM(m) = ci;
1.6 noro 8545: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8546: if ( !pi ) {
8547: pi = nd_load_mod(j);
8548: tp = nd_mul_nm(mod,BDY(m),pi);
8549: nd_free(pi);
8550: add_pbucket(mod,r,tp);
8551: } else {
8552: tp = nd_mul_nm(mod,BDY(m),pi);
8553: add_pbucket(mod,r,tp);
8554: }
8555: }
8556: ci = 1;
8557: } else {
8558: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8559: ci = invm(ci,mod);
8560: }
8561: }
8562: rd = normalize_pbucket(mod,r);
8563: free_pbucket(r);
8564: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8565: return rd;
8566: }
8567:
8568: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8569:
8570: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8571: {
8572: int i,j,n,m,nb,pi0,pi1,nvar;
8573: VL fv,tv,vv;
8574: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8575: ND **p;
8576: ND *c;
8577: ND u;
8578: P inv;
8579: MAT mat;
8580:
8581: if ( mod == -2 )
8582: return nd_btog_lf(f,v,ord,tlist,rp);
8583:
8584: parse_nd_option(current_option);
8585: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8586: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8587: switch ( ord->id ) {
8588: case 1:
8589: if ( ord->nv != nvar )
8590: error("nd_check : invalid order specification");
8591: break;
8592: default:
8593: break;
8594: }
8595: nd_init_ord(ord);
8596: #if 0
1.6 noro 8597: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8598: #else
8599: nd_bpe = 32;
8600: #endif
8601: nd_setup_parameters(nvar,0);
8602: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8603: intred = BDY((LIST)ARG3(BDY(tlist)));
8604: ind = BDY((LIST)ARG4(BDY(tlist)));
8605: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8606: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8607: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8608: if ( j > i ) i = j;
8609: }
8610: n = i+1;
8611: nb = length(BDY(f));
8612: p = (ND **)MALLOC(n*sizeof(ND *));
8613: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8614: pi = BDY((LIST)BDY(t));
1.6 noro 8615: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8616: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8617: ptomp(mod,(P)ARG2(pi),&inv);
8618: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8619: u = ptond(CO,vv,(P)ONE);
8620: HCM(u) = ((MQ)inv)->cont;
8621: c[pi1] = u;
8622: }
8623: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8624: printf("%d ",i); fflush(stdout);
8625: ti = BDY((LIST)BDY(t));
1.6 noro 8626: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8627: }
8628: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8629: printf("%d ",i); fflush(stdout);
8630: ti = BDY((LIST)BDY(t));
1.6 noro 8631: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8632: }
8633: m = length(ind);
8634: MKMAT(mat,nb,m);
8635: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8636: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8637: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8638: return mat;
8639: }
8640:
8641: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8642: {
8643: int i,j,n,m,nb,pi0,pi1,nvar;
8644: VL fv,tv,vv;
8645: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8646: ND **p;
8647: ND *c;
8648: ND u;
8649: MAT mat;
8650: LM lm;
8651: Z lf,inv;
8652:
8653: parse_nd_option(current_option);
8654: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8655: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8656: switch ( ord->id ) {
8657: case 1:
8658: if ( ord->nv != nvar )
8659: error("nd_check : invalid order specification");
8660: break;
8661: default:
8662: break;
8663: }
8664: nd_init_ord(ord);
8665: #if 0
1.6 noro 8666: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8667: #else
8668: nd_bpe = 32;
8669: #endif
8670: nd_setup_parameters(nvar,0);
8671: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8672: intred = BDY((LIST)ARG3(BDY(tlist)));
8673: ind = BDY((LIST)ARG4(BDY(tlist)));
8674: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8675: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8676: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8677: if ( j > i ) i = j;
8678: }
8679: n = i+1;
8680: nb = length(BDY(f));
8681: p = (ND **)MALLOC(n*sizeof(ND *));
8682: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8683: pi = BDY((LIST)BDY(t));
1.6 noro 8684: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8685: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8686: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8687: u = ptond(CO,vv,(P)ONE);
8688: HCZ(u) = inv;
8689: c[pi1] = u;
8690: }
8691: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8692: printf("%d ",i); fflush(stdout);
8693: ti = BDY((LIST)BDY(t));
1.6 noro 8694: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8695: }
8696: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8697: printf("%d ",i); fflush(stdout);
8698: ti = BDY((LIST)BDY(t));
1.6 noro 8699: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8700: }
8701: m = length(ind);
8702: MKMAT(mat,nb,m);
8703: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8704: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8705: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8706: return mat;
8707: }
8708:
8709: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8710: LIST tlist,int pos,MAT *rp)
8711: {
8712: int i,j,n,m,nb,pi0,pi1,nvar;
8713: VL fv,tv,vv;
8714: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8715: ND *p;
8716: ND *c;
8717: ND u;
8718: P inv;
8719: VECT vect;
8720:
8721: if ( mod == -2 )
8722: error("nd_btog_one : not implemented yet for a large finite field");
8723:
8724: parse_nd_option(current_option);
8725: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8726: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8727: switch ( ord->id ) {
8728: case 1:
8729: if ( ord->nv != nvar )
8730: error("nd_check : invalid order specification");
8731: break;
8732: default:
8733: break;
8734: }
8735: nd_init_ord(ord);
8736: #if 0
1.6 noro 8737: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8738: #else
8739: nd_bpe = 32;
8740: #endif
8741: nd_setup_parameters(nvar,0);
8742: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8743: intred = BDY((LIST)ARG3(BDY(tlist)));
8744: ind = BDY((LIST)ARG4(BDY(tlist)));
8745: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8746: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8747: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8748: if ( j > i ) i = j;
8749: }
8750: n = i+1;
8751: nb = length(BDY(f));
8752: p = (ND *)MALLOC(n*sizeof(ND *));
8753: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8754: pi = BDY((LIST)BDY(t));
1.6 noro 8755: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8756: if ( pi1 == pos ) {
8757: ptomp(mod,(P)ARG2(pi),&inv);
8758: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8759: u = ptond(CO,vv,(P)ONE);
8760: HCM(u) = ((MQ)inv)->cont;
8761: p[pi0] = u;
8762: }
8763: }
8764: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8765: printf("%d ",i); fflush(stdout);
8766: ti = BDY((LIST)BDY(t));
1.6 noro 8767: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8768: if ( Demand ) {
8769: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8770: }
8771: }
8772: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8773: printf("%d ",i); fflush(stdout);
8774: ti = BDY((LIST)BDY(t));
1.6 noro 8775: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8776: if ( Demand ) {
8777: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8778: }
8779: }
8780: m = length(ind);
8781: MKVECT(vect,m);
8782: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8783: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8784: if ( !u ) {
1.6 noro 8785: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8786: BDY(vect)[j] = ndtodp(mod,u);
8787: nd_free(u);
8788: } else
8789: BDY(vect)[j] = ndtodp(mod,u);
8790: }
8791: return vect;
8792: }
8793:
8794: void ndv_print_lf(NDV p)
8795: {
8796: NMV m;
8797: int i,len;
8798:
8799: if ( !p ) printf("0\n");
8800: else {
8801: len = LEN(p);
8802: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8803: printf("+");
8804: mpz_out_str(asir_out,10,BDY(CZ(m)));
8805: printf("*");
8806: ndl_print(DL(m));
8807: }
8808: printf("\n");
8809: }
8810: }
8811:
8812: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8813: {
8814: VL tv,fv,vv,vc,av;
8815: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8816: int m,nocheck,nvar,mindex,e,max;
8817: NDV c;
8818: NMV a;
8819: P p,zp;
8820: Q dmy;
8821: EPOS oepos;
8822: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8823: Alg alpha,dp;
8824: P poly;
8825: LIST f1,f2,zpl;
8826: Obj obj;
8827: NumberField nf;
8828: struct order_spec *ord1;
8829: struct oEGT eg_check,eg0,eg1;
8830: NODE tr,tl1,tl2,tl3,tl4;
8831: LIST l1,l2,l3,l4,l5;
8832: int *perm;
8833: int j,ret;
8834: NODE retn;
8835: Q jq,bpe;
8836:
8837: nd_module = 0;
8838: parse_nd_option(current_option);
8839: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8840: if ( nd_vc )
8841: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8842: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8843: switch ( ord->id ) {
8844: case 1:
8845: if ( ord->nv != nvar )
8846: error("nd_f4_lf_trace : invalid order specification");
8847: break;
8848: default:
8849: break;
8850: }
8851:
8852: nd_ntrans = nvar;
8853: nd_nalg = 0;
8854:
8855: nocheck = 0;
8856: mindex = 0;
8857:
8858: /* do not use on-demand load/save */
8859: nd_demand = 0;
8860: m = trace > 1 ? trace : get_lprime(mindex);
8861: nd_init_ord(ord);
8862: mrank = 0;
8863: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
8864: for ( tv = vv; tv; tv = NEXT(tv) ) {
8865: if ( nd_module ) {
8866: s = BDY((LIST)BDY(t));
8867: trank = length(s);
8868: mrank = MAX(mrank,trank);
8869: for ( ; s; s = NEXT(s) ) {
8870: e = getdeg(tv->v,(P)BDY(s));
8871: max = MAX(e,max);
8872: }
8873: } else {
8874: e = getdeg(tv->v,(P)BDY(t));
8875: max = MAX(e,max);
8876: }
8877: }
8878: nd_setup_parameters(nvar,max);
8879: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
8880: ishomo = 1;
8881: /* XXX */
8882: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
8883: if ( nd_module ) {
8884: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
8885: } else {
8886: c = (pointer)ptondv(CO,vv,(P)BDY(t));
8887: }
8888: if ( ishomo )
8889: ishomo = ishomo && ndv_ishomo(c);
8890: if ( c ) {
8891: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
8892: ndv_mod(-2,c);
8893: NEXTNODE(in0,in); BDY(in) = (pointer)c;
8894: }
8895: }
8896: if ( in0 ) NEXT(in) = 0;
8897: if ( fd0 ) NEXT(fd) = 0;
8898: if ( !ishomo && homo ) {
8899: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
8900: c = (NDV)BDY(t); len = LEN(c);
8901: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
8902: wmax = MAX(TD(DL(a)),wmax);
8903: }
8904: homogenize_order(ord,nvar,&ord1);
8905: nd_init_ord(ord1);
8906: nd_setup_parameters(nvar+1,wmax);
8907: for ( t = fd0; t; t = NEXT(t) )
8908: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
8909: }
8910: if ( MaxDeg > 0 ) nocheck = 1;
8911: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
8912: if ( ret )
8913: cand = nd_f4_lf_trace_main(m,&perm);
8914: if ( !ret || !cand ) {
8915: *rp = 0; return;
8916: }
8917: if ( !ishomo && homo ) {
8918: /* dehomogenization */
8919: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
8920: nd_init_ord(ord);
8921: nd_setup_parameters(nvar,0);
8922: }
8923: cand = ndv_reducebase(cand,perm);
8924: cand = ndv_reduceall(-2,cand);
8925: cbpe = nd_bpe;
8926: get_eg(&eg0);
8927: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
8928: /* gbcheck : cand is a GB of Id(cand) ? */
8929: retn = nd_f4(-2,0,0);
8930: }
8931: if ( !retn ) {
8932: /* failure */
8933: *rp = 0; return;
8934: }
8935: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
8936: if ( DP_Print )
1.5 noro 8937: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 8938: /* dp->p */
8939: nd_bpe = cbpe;
8940: nd_setup_parameters(nd_nvar,0);
8941: for ( r = cand; r; r = NEXT(r) ) {
8942: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
8943: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
8944: }
8945: MKLIST(*rp,cand);
8946: }
8947:
8948: NODE nd_f4_lf_trace_main(int m,int **indp)
8949: {
8950: int i,nh,stat,index;
8951: NODE r,rm,g;
8952: ND_pairs d,l,l0,t;
8953: ND spol,red;
8954: NDV nf,redv,nfqv,nfv;
8955: NM s0,s;
8956: NODE rp0,srp0,nflist,nflist_lf;
8957: int nsp,nred,col,rank,len,k,j,a;
8958: UINT c;
8959: UINT **spmat;
8960: UINT *s0vect,*svect,*p,*v;
8961: int *colstat;
8962: IndArray *imat;
8963: int *rhead;
8964: int spcol,sprow;
8965: int sugar;
8966: PGeoBucket bucket;
8967: struct oEGT eg0,eg1,eg_f4;
8968:
8969: g = 0; d = 0;
8970: for ( i = 0; i < nd_psn; i++ ) {
8971: d = update_pairs(d,g,i,0);
8972: g = update_base(g,i);
8973: }
8974: while ( d ) {
8975: get_eg(&eg0);
8976: l = nd_minsugarp(d,&d);
8977: sugar = SG(l);
8978: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
8979: bucket = create_pbucket();
8980: stat = nd_sp_f4(m,0,l,bucket);
8981: if ( !stat ) {
8982: for ( t = l; NEXT(t); t = NEXT(t) );
8983: NEXT(t) = d; d = l;
8984: d = nd_reconstruct(1,d);
8985: continue;
8986: }
8987: if ( bucket->m < 0 ) continue;
8988: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
8989: if ( !col ) {
8990: for ( t = l; NEXT(t); t = NEXT(t) );
8991: NEXT(t) = d; d = l;
8992: d = nd_reconstruct(1,d);
8993: continue;
8994: }
8995: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
8996: if ( DP_Print )
1.5 noro 8997: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 8998: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
8999: if ( !l0 ) continue;
9000: l = l0;
9001:
9002: /* over LF */
9003: bucket = create_pbucket();
9004: stat = nd_sp_f4(-2,1,l,bucket);
9005: if ( !stat ) {
9006: for ( t = l; NEXT(t); t = NEXT(t) );
9007: NEXT(t) = d; d = l;
9008: d = nd_reconstruct(1,d);
9009: continue;
9010: }
9011: if ( bucket->m < 0 ) continue;
9012: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9013: if ( !col ) {
9014: for ( t = l; NEXT(t); t = NEXT(t) );
9015: NEXT(t) = d; d = l;
9016: d = nd_reconstruct(1,d);
9017: continue;
9018: }
9019: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9020: /* adding new bases */
9021: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9022: nfv = (NDV)BDY(rm);
9023: nfqv = (NDV)BDY(r);
9024: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9025: ndv_removecont(m,nfv);
9026: ndv_removecont(-2,nfqv);
9027: nh = ndv_newps(-2,nfv,nfqv,1);
9028: d = update_pairs(d,g,nh,0);
9029: g = update_base(g,nh);
9030: }
9031: if ( r || rm ) return 0;
9032: }
9033: conv_ilist(nd_demand,1,g,indp);
9034: return g;
9035: }
9036:
1.7 noro 9037: #if SIZEOF_LONG==8
9038:
9039: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9040: {
9041: int j,k,len;
9042: UINT *p;
9043: UINT c;
9044: NDV r;
9045: NMV mr0,mr;
9046:
9047: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9048: if ( !len ) return 0;
9049: else {
9050: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9051: #if 0
9052: ndv_alloc += nmv_adv*len;
9053: #endif
9054: mr = mr0;
9055: p = s0vect;
9056: for ( j = k = 0; j < col; j++, p += nd_wpd )
9057: if ( !rhead[j] ) {
9058: if ( (c = (UINT)vect[k++]) != 0 ) {
9059: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9060: }
9061: }
9062: MKNDV(nd_nvar,mr0,len,r);
9063: return r;
9064: }
9065: }
9066:
9067: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9068: {
9069: NM m;
1.11 noro 9070: UINT *t,*s,*u;
9071: int i,st,ed,md,prev,c;
1.7 noro 9072:
9073: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9074: prev = 0;
9075: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9076: t = DL(m);
9077: st = prev;
9078: ed = n;
9079: while ( ed > st ) {
9080: md = (st+ed)/2;
9081: u = s0+md*nd_wpd;
9082: c = DL_COMPARE(u,t);
9083: if ( c == 0 ) break;
9084: else if ( c > 0 ) st = md;
9085: else ed = md;
9086: }
9087: r[md] = (mp_limb_t)CM(m);
9088: prev = md;
1.7 noro 9089: }
9090: for ( i = 0; !r[i]; i++ );
9091: return i;
9092: }
9093:
9094: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9095:
9096: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9097: {
9098: int i,j,k,len,pos,prev;
9099: mp_limb_t a,c,c1,c2;
9100: IndArray ivect;
9101: unsigned char *ivc;
9102: unsigned short *ivs;
9103: unsigned int *ivi;
9104: NDV redv;
9105: NMV mr;
9106: NODE rp;
9107: int maxrs;
9108:
9109: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9110: maxrs = 0;
9111: for ( i = 0; i < nred; i++ ) {
9112: ivect = imat[i];
9113: k = ivect->head;
9114: a = svect[k]; c = cvect[k];
9115: MOD128(a,c,m);
9116: svect[k] = a; cvect[k] = 0;
9117: if ( (c = svect[k]) != 0 ) {
1.11 noro 9118: Nf4_red++;
1.7 noro 9119: maxrs = MAX(maxrs,rp0[i]->sugar);
9120: c = m-c; redv = nd_ps[rp0[i]->index];
9121: len = LEN(redv); mr = BDY(redv);
9122: svect[k] = 0; prev = k;
9123: switch ( ivect->width ) {
9124: case 1:
9125: ivc = ivect->index.c;
9126: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9127: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 9128: c2 = svect[pos]+c1*c;
9129: if ( c2 < svect[pos] ) cvect[pos]++;
9130: svect[pos] = c2;
1.7 noro 9131: }
9132: break;
9133: case 2:
9134: ivs = ivect->index.s;
9135: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9136: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 9137: c2 = svect[pos]+c1*c;
9138: if ( c2 < svect[pos] ) cvect[pos]++;
9139: svect[pos] = c2;
1.7 noro 9140: }
9141: break;
9142: case 4:
9143: ivi = ivect->index.i;
9144: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9145: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 9146: c2 = svect[pos]+c1*c;
9147: if ( c2 < svect[pos] ) cvect[pos]++;
9148: svect[pos] = c2;
1.7 noro 9149: }
9150: break;
9151: }
9152: }
9153: }
9154: for ( i = 0; i < col; i++ ) {
9155: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9156: }
9157: return maxrs;
9158: }
9159:
9160: /* for Fp, 2^15=<p<2^29 */
9161:
9162: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9163: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9164: {
9165: int spcol,sprow,a;
9166: int i,j,k,l,rank;
9167: NODE r0,r;
9168: ND_pairs sp;
9169: ND spol;
9170: mp_limb_t **spmat;
9171: mp_limb_t *svect,*cvect;
9172: mp_limb_t *v;
9173: int *colstat;
9174: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9175: int maxrs;
9176: int *spsugar;
9177: ND_pairs *spactive;
9178:
9179: spcol = col-nred;
9180: get_eg(&eg0);
9181: /* elimination (1st step) */
9182: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9183: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9184: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9185: spsugar = (int *)MALLOC(nsp*sizeof(int));
9186: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9187: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9188: nd_sp(m,0,sp,&spol);
9189: if ( !spol ) continue;
9190: nd_to_vect64(m,s0vect,col,spol,svect);
9191: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9192: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9193: if ( i < col ) {
9194: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9195: for ( j = k = 0; j < col; j++ )
9196: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9197: spsugar[sprow] = MAX(maxrs,SG(spol));
9198: if ( nz )
9199: spactive[sprow] = sp;
9200: sprow++;
9201: }
9202: nd_free(spol);
9203: }
1.12 noro 9204: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9205: if ( DP_Print ) {
9206: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9207: fflush(asir_out);
9208: }
9209: /* free index arrays */
9210: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9211:
9212: /* elimination (2nd step) */
9213: colstat = (int *)MALLOC(spcol*sizeof(int));
9214: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9215: r0 = 0;
9216: for ( i = 0; i < rank; i++ ) {
9217: NEXTNODE(r0,r); BDY(r) =
9218: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9219: SG((NDV)BDY(r)) = spsugar[i];
9220: GCFREE(spmat[i]);
9221: }
9222: if ( r0 ) NEXT(r) = 0;
9223:
9224: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 9225: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9226: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9227: if ( DP_Print ) {
9228: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9229: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9230: nsp,nred,sprow,spcol,rank);
9231: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9232: }
9233: if ( nz ) {
9234: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9235: if ( rank > 0 ) {
9236: NEXT(spactive[rank-1]) = 0;
9237: *nz = spactive[0];
9238: } else
9239: *nz = 0;
9240: }
9241: return r0;
9242: }
9243:
9244: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9245: {
9246: int i,j,k,l,rank,s;
9247: mp_limb_t inv;
9248: mp_limb_t a;
9249: UINT c;
9250: mp_limb_t *t,*pivot,*pk;
9251: UINT *ck;
9252: UINT **cmat;
9253: UINT *ct;
9254: ND_pairs pair;
9255:
9256: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9257: for ( i = 0; i < row; i++ ) {
9258: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9259: bzero(cmat[i],col*sizeof(UINT));
9260: }
9261:
9262: for ( rank = 0, j = 0; j < col; j++ ) {
9263: for ( i = rank; i < row; i++ ) {
9264: a = mat[i][j]; c = cmat[i][j];
9265: MOD128(a,c,md);
9266: mat[i][j] = a; cmat[i][j] = 0;
9267: }
9268: for ( i = rank; i < row; i++ )
9269: if ( mat[i][j] )
9270: break;
9271: if ( i == row ) {
9272: colstat[j] = 0;
9273: continue;
9274: } else
9275: colstat[j] = 1;
9276: if ( i != rank ) {
9277: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9278: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9279: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9280: if ( spactive ) {
9281: pair = spactive[i]; spactive[i] = spactive[rank];
9282: spactive[rank] = pair;
9283: }
9284: }
9285: /* column j is normalized */
9286: s = sugar[rank];
9287: inv = invm((UINT)mat[rank][j],md);
9288: /* normalize pivot row */
9289: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9290: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9291: }
9292: for ( i = rank+1; i < row; i++ ) {
9293: if ( (a = mat[i][j]) != 0 ) {
9294: sugar[i] = MAX(sugar[i],s);
9295: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9296: Nf4_red++;
1.7 noro 9297: }
9298: }
9299: rank++;
9300: }
9301: for ( j = col-1, l = rank-1; j >= 0; j-- )
9302: if ( colstat[j] ) {
9303: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9304: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9305: }
9306: s = sugar[l];
9307: for ( i = 0; i < l; i++ ) {
9308: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9309: if ( a ) {
9310: sugar[i] = MAX(sugar[i],s);
9311: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9312: Nf4_red++;
1.7 noro 9313: }
9314: }
9315: l--;
9316: }
9317: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9318: GCFREE(cmat);
9319: return rank;
9320: }
9321: #endif
9322:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>