Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.18
1.18 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.17 2019/08/28 23:27:34 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);
1.16 noro 91: DPM ndvtodpm(int mod,NDV p);
92: NDV dpmtondv(int mod,DPM p);
93: int dpm_getdeg(DPM p,int *rank);
94: void dpm_ptozp(DPM p,Z *cont,DPM *r);
95: int compdmm(int nv,DMM a,DMM b);
1.1 noro 96:
97: void Pdp_set_weight(NODE,VECT *);
98: void Pox_cmo_rpc(NODE,Obj *);
99:
100: ND nd_add_lf(ND p1,ND p2);
101: void nd_mul_c_lf(ND p,Z mul);
102: void ndv_mul_c_lf(NDV p,Z mul);
103: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
104: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
105: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
106: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
107: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
108: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
109: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
110: NODE nd_f4_lf_trace_main(int m,int **indp);
111: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
112:
113: extern int lf_lazy;
114: extern Z current_mod_lf;
115:
116: extern int Denominator,DP_Multiple,MaxDeg;
117:
118: #define BLEN (8*sizeof(unsigned long))
119:
120: typedef struct matrix {
121: int row,col;
122: unsigned long **a;
123: } *matrix;
124:
125:
126: void nd_free_private_storage()
127: {
128: _nm_free_list = 0;
129: _ndp_free_list = 0;
130: #if 0
131: GC_gcollect();
132: #endif
133: }
134:
135: void _NM_alloc()
136: {
137: NM p;
138: int i;
139:
140: for ( i = 0; i < 1024; i++ ) {
141: p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
142: p->next = _nm_free_list; _nm_free_list = p;
143: }
144: }
145:
146: matrix alloc_matrix(int row,int col)
147: {
148: unsigned long **a;
149: int i,len,blen;
150: matrix mat;
151:
152: mat = (matrix)MALLOC(sizeof(struct matrix));
153: mat->row = row;
154: mat->col = col;
155: mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
156: return mat;
157: }
158:
159:
160: void _ND_alloc()
161: {
162: ND p;
163: int i;
164:
165: for ( i = 0; i < 1024; i++ ) {
166: p = (ND)MALLOC(sizeof(struct oND));
167: p->body = (NM)_nd_free_list; _nd_free_list = p;
168: }
169: }
170:
171: void _NDP_alloc()
172: {
173: ND_pairs p;
174: int i;
175:
176: for ( i = 0; i < 1024; i++ ) {
177: p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
178: +(nd_wpd-1)*sizeof(UINT));
179: p->next = _ndp_free_list; _ndp_free_list = p;
180: }
181: }
182:
183: INLINE int nd_length(ND p)
184: {
185: NM m;
186: int i;
187:
188: if ( !p )
189: return 0;
190: else {
191: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
192: return i;
193: }
194: }
195:
196: extern int dp_negative_weight;
197:
198: INLINE int ndl_reducible(UINT *d1,UINT *d2)
199: {
200: UINT u1,u2;
201: int i,j;
202:
203: if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
204:
205: if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
206: #if USE_UNROLL
207: switch ( nd_bpe ) {
208: case 3:
209: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
210: u1 = d1[i]; u2 = d2[i];
211: if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
212: if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
213: if ( (u1& 0xe00000) < (u2& 0xe00000) ) return 0;
214: if ( (u1& 0x1c0000) < (u2& 0x1c0000) ) return 0;
215: if ( (u1& 0x38000) < (u2& 0x38000) ) return 0;
216: if ( (u1& 0x7000) < (u2& 0x7000) ) return 0;
217: if ( (u1& 0xe00) < (u2& 0xe00) ) return 0;
218: if ( (u1& 0x1c0) < (u2& 0x1c0) ) return 0;
219: if ( (u1& 0x38) < (u2& 0x38) ) return 0;
220: if ( (u1& 0x7) < (u2& 0x7) ) return 0;
221: }
222: return 1;
223: break;
224: case 4:
225: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
226: u1 = d1[i]; u2 = d2[i];
227: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
228: if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
229: if ( (u1& 0xf00000) < (u2& 0xf00000) ) return 0;
230: if ( (u1& 0xf0000) < (u2& 0xf0000) ) return 0;
231: if ( (u1& 0xf000) < (u2& 0xf000) ) return 0;
232: if ( (u1& 0xf00) < (u2& 0xf00) ) return 0;
233: if ( (u1& 0xf0) < (u2& 0xf0) ) return 0;
234: if ( (u1& 0xf) < (u2& 0xf) ) return 0;
235: }
236: return 1;
237: break;
238: case 6:
239: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
240: u1 = d1[i]; u2 = d2[i];
241: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
242: if ( (u1& 0xfc0000) < (u2& 0xfc0000) ) return 0;
243: if ( (u1& 0x3f000) < (u2& 0x3f000) ) return 0;
244: if ( (u1& 0xfc0) < (u2& 0xfc0) ) return 0;
245: if ( (u1& 0x3f) < (u2& 0x3f) ) return 0;
246: }
247: return 1;
248: break;
249: case 8:
250: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
251: u1 = d1[i]; u2 = d2[i];
252: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
253: if ( (u1& 0xff0000) < (u2& 0xff0000) ) return 0;
254: if ( (u1& 0xff00) < (u2& 0xff00) ) return 0;
255: if ( (u1& 0xff) < (u2& 0xff) ) return 0;
256: }
257: return 1;
258: break;
259: case 16:
260: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
261: u1 = d1[i]; u2 = d2[i];
262: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
263: if ( (u1& 0xffff) < (u2& 0xffff) ) return 0;
264: }
265: return 1;
266: break;
267: case 32:
268: for ( i = nd_exporigin; i < nd_wpd; i++ )
269: if ( d1[i] < d2[i] ) return 0;
270: return 1;
271: break;
272: default:
273: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
274: u1 = d1[i]; u2 = d2[i];
275: for ( j = 0; j < nd_epw; j++ )
276: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
277: }
278: return 1;
279: }
280: #else
281: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
282: u1 = d1[i]; u2 = d2[i];
283: for ( j = 0; j < nd_epw; j++ )
284: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
285: }
286: return 1;
287: #endif
288: }
289:
290: /*
291: * If the current order is a block order,
292: * then the last block is length 1 and contains
293: * the homo variable. Otherwise, the original
294: * order is either 0 or 2.
295: */
296:
297: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
298: {
299: int w,i,e,n,omask0;
300:
301: omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
302: n = nd_nvar-1;
303: ndl_zero(r);
304: for ( i = 0; i < n; i++ ) {
305: e = GET_EXP_OLD(d,i);
306: PUT_EXP(r,i,e);
307: }
308: w = TD(d);
309: PUT_EXP(r,nd_nvar-1,weight-w);
310: if ( nd_module ) MPOS(r) = d[ompos];
311: TD(r) = weight;
312: if ( nd_blockmask ) ndl_weight_mask(r);
313: }
314:
315: void ndl_dehomogenize(UINT *d)
316: {
317: UINT mask;
318: UINT h;
319: int i,bits;
320:
321: if ( nd_blockmask ) {
322: h = GET_EXP(d,nd_nvar-1);
323: XOR_EXP(d,nd_nvar-1,h);
324: TD(d) -= h;
325: ndl_weight_mask(d);
326: } else {
327: if ( nd_isrlex ) {
328: if ( nd_bpe == 32 ) {
329: h = d[nd_exporigin];
330: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
331: d[i-1] = d[i];
332: d[i-1] = 0;
333: TD(d) -= h;
334: } else {
335: bits = nd_epw*nd_bpe;
336: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
337: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
338: for ( i = nd_exporigin; i < nd_wpd; i++ )
339: d[i] = ((d[i]<<nd_bpe)&mask)
340: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
341: TD(d) -= h;
342: }
343: } else {
344: h = GET_EXP(d,nd_nvar-1);
345: XOR_EXP(d,nd_nvar-1,h);
346: TD(d) -= h;
347: }
348: }
349: }
350:
351: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
352: {
353: UINT t1,t2,u,u1,u2;
354: int i,j,l;
355:
356: if ( nd_module && (MPOS(d1) != MPOS(d2)) )
357: error("ndl_lcm : inconsistent monomials");
358: #if USE_UNROLL
359: switch ( nd_bpe ) {
360: case 3:
361: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
362: u1 = d1[i]; u2 = d2[i];
363: t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
364: t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
365: t1 = (u1& 0xe00000); t2 = (u2& 0xe00000); u |= t1>t2?t1:t2;
366: t1 = (u1& 0x1c0000); t2 = (u2& 0x1c0000); u |= t1>t2?t1:t2;
367: t1 = (u1& 0x38000); t2 = (u2& 0x38000); u |= t1>t2?t1:t2;
368: t1 = (u1& 0x7000); t2 = (u2& 0x7000); u |= t1>t2?t1:t2;
369: t1 = (u1& 0xe00); t2 = (u2& 0xe00); u |= t1>t2?t1:t2;
370: t1 = (u1& 0x1c0); t2 = (u2& 0x1c0); u |= t1>t2?t1:t2;
371: t1 = (u1& 0x38); t2 = (u2& 0x38); u |= t1>t2?t1:t2;
372: t1 = (u1& 0x7); t2 = (u2& 0x7); u |= t1>t2?t1:t2;
373: d[i] = u;
374: }
375: break;
376: case 4:
377: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
378: u1 = d1[i]; u2 = d2[i];
379: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
380: t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
381: t1 = (u1& 0xf00000); t2 = (u2& 0xf00000); u |= t1>t2?t1:t2;
382: t1 = (u1& 0xf0000); t2 = (u2& 0xf0000); u |= t1>t2?t1:t2;
383: t1 = (u1& 0xf000); t2 = (u2& 0xf000); u |= t1>t2?t1:t2;
384: t1 = (u1& 0xf00); t2 = (u2& 0xf00); u |= t1>t2?t1:t2;
385: t1 = (u1& 0xf0); t2 = (u2& 0xf0); u |= t1>t2?t1:t2;
386: t1 = (u1& 0xf); t2 = (u2& 0xf); u |= t1>t2?t1:t2;
387: d[i] = u;
388: }
389: break;
390: case 6:
391: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
392: u1 = d1[i]; u2 = d2[i];
393: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
394: t1 = (u1& 0xfc0000); t2 = (u2& 0xfc0000); u |= t1>t2?t1:t2;
395: t1 = (u1& 0x3f000); t2 = (u2& 0x3f000); u |= t1>t2?t1:t2;
396: t1 = (u1& 0xfc0); t2 = (u2& 0xfc0); u |= t1>t2?t1:t2;
397: t1 = (u1& 0x3f); t2 = (u2& 0x3f); u |= t1>t2?t1:t2;
398: d[i] = u;
399: }
400: break;
401: case 8:
402: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
403: u1 = d1[i]; u2 = d2[i];
404: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
405: t1 = (u1& 0xff0000); t2 = (u2& 0xff0000); u |= t1>t2?t1:t2;
406: t1 = (u1& 0xff00); t2 = (u2& 0xff00); u |= t1>t2?t1:t2;
407: t1 = (u1& 0xff); t2 = (u2& 0xff); u |= t1>t2?t1:t2;
408: d[i] = u;
409: }
410: break;
411: case 16:
412: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
413: u1 = d1[i]; u2 = d2[i];
414: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
415: t1 = (u1& 0xffff); t2 = (u2& 0xffff); u |= t1>t2?t1:t2;
416: d[i] = u;
417: }
418: break;
419: case 32:
420: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
421: u1 = d1[i]; u2 = d2[i];
422: d[i] = u1>u2?u1:u2;
423: }
424: break;
425: default:
426: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
427: u1 = d1[i]; u2 = d2[i];
428: for ( j = 0, u = 0; j < nd_epw; j++ ) {
429: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
430: }
431: d[i] = u;
432: }
433: break;
434: }
435: #else
436: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
437: u1 = d1[i]; u2 = d2[i];
438: for ( j = 0, u = 0; j < nd_epw; j++ ) {
439: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
440: }
441: d[i] = u;
442: }
443: #endif
444: if ( nd_module ) MPOS(d) = MPOS(d1);
445: TD(d) = ndl_weight(d);
446: if ( nd_blockmask ) ndl_weight_mask(d);
447: }
448:
449: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
450: {
451: UINT t1,t2,u,u1,u2;
452: int i,j,l;
453:
454: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
455: u1 = d1[i]; u2 = d2[i];
456: for ( j = 0, u = 0; j < nd_epw; j++ ) {
457: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
458: }
459: d[i] = u;
460: }
461: }
462:
463: int ndl_weight(UINT *d)
464: {
465: UINT t,u;
466: int i,j;
467:
468: if ( current_dl_weight_vector )
469: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
470: u = GET_EXP(d,i);
471: t += MUL_WEIGHT(u,i);
472: }
473: else
474: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
475: u = d[i];
476: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
477: t += (u&nd_mask0);
478: }
479: if ( nd_module && current_module_weight_vector && MPOS(d) )
480: t += current_module_weight_vector[MPOS(d)];
481: return t;
482: }
483:
484: /* for sugarweight */
485:
486: int ndl_weight2(UINT *d)
487: {
488: int t,u;
489: int i,j;
490:
491: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
492: u = GET_EXP(d,i);
493: t += nd_sugarweight[i]*u;
494: }
495: if ( nd_module && current_module_weight_vector && MPOS(d) )
496: t += current_module_weight_vector[MPOS(d)];
497: return t;
498: }
499:
500: void ndl_weight_mask(UINT *d)
501: {
502: UINT t,u;
503: UINT *mask;
504: int i,j,k,l;
505:
506: l = nd_blockmask->n;
507: for ( k = 0; k < l; k++ ) {
508: mask = nd_blockmask->mask[k];
509: if ( current_dl_weight_vector )
510: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
511: u = GET_EXP_MASK(d,i,mask);
512: t += MUL_WEIGHT(u,i);
513: }
514: else
515: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
516: u = d[i]&mask[i];
517: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
518: t += (u&nd_mask0);
519: }
520: d[k+1] = t;
521: }
522: }
523:
524: int ndl_lex_compare(UINT *d1,UINT *d2)
525: {
526: int i;
527:
528: d1 += nd_exporigin;
529: d2 += nd_exporigin;
530: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
531: if ( *d1 > *d2 )
532: return nd_isrlex ? -1 : 1;
533: else if ( *d1 < *d2 )
534: return nd_isrlex ? 1 : -1;
535: return 0;
536: }
537:
538: int ndl_block_compare(UINT *d1,UINT *d2)
539: {
540: int i,l,j,ord_o,ord_l;
541: struct order_pair *op;
542: UINT t1,t2,m;
543: UINT *mask;
544:
545: l = nd_blockmask->n;
546: op = nd_blockmask->order_pair;
547: for ( j = 0; j < l; j++ ) {
548: mask = nd_blockmask->mask[j];
549: ord_o = op[j].order;
550: if ( ord_o < 2 ) {
551: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
552: else if ( t1 < t2 ) return -1;
553: }
554: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
555: m = mask[i];
556: t1 = d1[i]&m;
557: t2 = d2[i]&m;
558: if ( t1 > t2 )
559: return !ord_o ? -1 : 1;
560: else if ( t1 < t2 )
561: return !ord_o ? 1 : -1;
562: }
563: }
564: return 0;
565: }
566:
567: int ndl_matrix_compare(UINT *d1,UINT *d2)
568: {
569: int i,j,s,row;
570: int *v;
571: Z **mat;
572: Z *w;
573: Z t1;
574: Z t,t2;
575:
1.6 noro 576: for ( j = 0; j < nd_nvar; j++ )
577: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1 noro 578: if ( nd_top_weight ) {
579: if ( OID(nd_top_weight) == O_VECT ) {
1.6 noro 580: mat = (Z **)&BDY((VECT)nd_top_weight);
581: row = 1;
1.1 noro 582: } else {
583: mat = (Z **)BDY((MAT)nd_top_weight);
1.6 noro 584: row = ((MAT)nd_top_weight)->row;
1.1 noro 585: }
586: for ( i = 0; i < row; i++ ) {
1.6 noro 587: w = mat[i];
1.1 noro 588: for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6 noro 589: STOZ(nd_work_vector[j],t1);
1.1 noro 590: mulz(w[j],t1,&t2);
591: addz(t,t2,&t1);
592: t = t1;
593: }
1.6 noro 594: if ( t ) {
595: s = sgnz(t);
1.1 noro 596: if ( s > 0 ) return 1;
597: else if ( s < 0 ) return -1;
1.6 noro 598: }
1.1 noro 599: }
1.6 noro 600: }
601: for ( i = 0; i < nd_matrix_len; i++ ) {
602: v = nd_matrix[i];
603: for ( j = 0, s = 0; j < nd_nvar; j++ )
604: s += v[j]*nd_work_vector[j];
605: if ( s > 0 ) return 1;
606: else if ( s < 0 ) return -1;
607: }
1.1 noro 608: if ( !ndl_equal(d1,d2) )
1.6 noro 609: error("ndl_matrix_compare : invalid matrix");
610: return 0;
1.1 noro 611: }
612:
613: int ndl_composite_compare(UINT *d1,UINT *d2)
614: {
615: int i,j,s,start,end,len,o;
616: int *v;
617: struct sparse_weight *sw;
618:
619: for ( j = 0; j < nd_nvar; j++ )
620: nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
621: for ( i = 0; i < nd_worb_len; i++ ) {
622: len = nd_worb[i].length;
623: switch ( nd_worb[i].type ) {
624: case IS_DENSE_WEIGHT:
625: v = nd_worb[i].body.dense_weight;
626: for ( j = 0, s = 0; j < len; j++ )
627: s += v[j]*nd_work_vector[j];
628: if ( s > 0 ) return 1;
629: else if ( s < 0 ) return -1;
630: break;
631: case IS_SPARSE_WEIGHT:
632: sw = nd_worb[i].body.sparse_weight;
633: for ( j = 0, s = 0; j < len; j++ )
634: s += sw[j].value*nd_work_vector[sw[j].pos];
635: if ( s > 0 ) return 1;
636: else if ( s < 0 ) return -1;
637: break;
638: case IS_BLOCK:
639: o = nd_worb[i].body.block.order;
640: start = nd_worb[i].body.block.start;
641: switch ( o ) {
642: case 0:
643: end = start+len;
644: for ( j = start, s = 0; j < end; j++ )
645: s += MUL_WEIGHT(nd_work_vector[j],j);
646: if ( s > 0 ) return 1;
647: else if ( s < 0 ) return -1;
648: for ( j = end-1; j >= start; j-- )
649: if ( nd_work_vector[j] < 0 ) return 1;
650: else if ( nd_work_vector[j] > 0 ) return -1;
651: break;
652: case 1:
653: end = start+len;
654: for ( j = start, s = 0; j < end; j++ )
655: s += MUL_WEIGHT(nd_work_vector[j],j);
656: if ( s > 0 ) return 1;
657: else if ( s < 0 ) return -1;
658: for ( j = start; j < end; j++ )
659: if ( nd_work_vector[j] > 0 ) return 1;
660: else if ( nd_work_vector[j] < 0 ) return -1;
661: break;
662: case 2:
663: end = start+len;
664: for ( j = start; j < end; j++ )
665: if ( nd_work_vector[j] > 0 ) return 1;
666: else if ( nd_work_vector[j] < 0 ) return -1;
667: break;
668: }
669: break;
670: }
671: }
672: return 0;
673: }
674:
675: /* TDH -> WW -> TD-> RL */
676:
677: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
678: {
679: int i,m,e1,e2;
680:
681: if ( TD(d1) > TD(d2) ) return 1;
682: else if ( TD(d1) < TD(d2) ) return -1;
683: m = nd_nvar>>1;
684: for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
685: e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
686: e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
687: }
688: if ( e1 > e2 ) return 1;
689: else if ( e1 < e2 ) return -1;
690: return ndl_lex_compare(d1,d2);
691: }
692:
693: int ndl_module_weight_compare(UINT *d1,UINT *d2)
694: {
695: int s,j;
696:
697: if ( nd_nvar != nd_poly_weight_len )
698: error("invalid module weight : the length of polynomial weight != the number of variables");
699: s = 0;
700: for ( j = 0; j < nd_nvar; j++ )
701: s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
702: if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
703: s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
704: }
705: if ( s > 0 ) return 1;
706: else if ( s < 0 ) return -1;
707: else return 0;
708: }
709:
710: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
711: {
712: int i,c;
713:
714: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
715: if ( nd_ispot ) {
716: if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
717: if ( TD(d1) > TD(d2) ) return 1;
718: else if ( TD(d1) < TD(d2) ) return -1;
719: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
720: if ( MPOS(d1) < MPOS(d2) ) return 1;
721: else if ( MPOS(d1) > MPOS(d2) ) return -1;
722: return 0;
723: }
724: if ( MPOS(d1) < MPOS(d2) ) return 1;
725: else if ( MPOS(d1) > MPOS(d2) ) return -1;
726: }
727: if ( TD(d1) > TD(d2) ) return 1;
728: else if ( TD(d1) < TD(d2) ) return -1;
729: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
730: if ( !nd_ispot ) {
731: if ( MPOS(d1) < MPOS(d2) ) return 1;
732: else if ( MPOS(d1) > MPOS(d2) ) return -1;
733: }
734: return 0;
735: }
736:
737: int ndl_module_glex_compare(UINT *d1,UINT *d2)
738: {
739: int i,c;
740:
741: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
742: if ( nd_ispot ) {
743: if ( MPOS(d1) < MPOS(d2) ) return 1;
744: else if ( MPOS(d1) > MPOS(d2) ) return -1;
745: }
746: if ( TD(d1) > TD(d2) ) return 1;
747: else if ( TD(d1) < TD(d2) ) return -1;
748: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
749: if ( !nd_ispot ) {
750: if ( MPOS(d1) < MPOS(d2) ) return 1;
751: else if ( MPOS(d1) > MPOS(d2) ) return -1;
752: }
753: return 0;
754: }
755:
756: int ndl_module_lex_compare(UINT *d1,UINT *d2)
757: {
758: int i,c;
759:
760: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) 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: if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
766: if ( !nd_ispot ) {
767: if ( MPOS(d1) < MPOS(d2) ) return 1;
768: else if ( MPOS(d1) > MPOS(d2) ) return -1;
769: }
770: return 0;
771: }
772:
773: int ndl_module_block_compare(UINT *d1,UINT *d2)
774: {
775: int i,c;
776:
777: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) 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: if ( (c = ndl_block_compare(d1,d2)) != 0 ) return c;
783: if ( !nd_ispot ) {
784: if ( MPOS(d1) < MPOS(d2) ) return 1;
785: else if ( MPOS(d1) > MPOS(d2) ) return -1;
786: }
787: return 0;
788: }
789:
790: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
791: {
792: int i,c;
793:
794: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) 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: if ( (c = ndl_matrix_compare(d1,d2)) != 0 ) return c;
800: if ( !nd_ispot ) {
801: if ( MPOS(d1) < MPOS(d2) ) return 1;
802: else if ( MPOS(d1) > MPOS(d2) ) return -1;
803: }
804: return 0;
805: }
806:
807: int ndl_module_composite_compare(UINT *d1,UINT *d2)
808: {
809: int i,c;
810:
811: if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) 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: if ( (c = ndl_composite_compare(d1,d2)) != 0 ) return c;
817: if ( !nd_ispot ) {
818: if ( MPOS(d1) > MPOS(d2) ) return 1;
819: else if ( MPOS(d1) < MPOS(d2) ) return -1;
820: }
821: return 0;
822: }
823:
824: INLINE int ndl_equal(UINT *d1,UINT *d2)
825: {
826: int i;
827:
828: switch ( nd_wpd ) {
829: case 2:
830: if ( TD(d2) != TD(d1) ) return 0;
831: if ( d2[1] != d1[1] ) return 0;
832: return 1;
833: break;
834: case 3:
835: if ( TD(d2) != TD(d1) ) return 0;
836: if ( d2[1] != d1[1] ) return 0;
837: if ( d2[2] != d1[2] ) return 0;
838: return 1;
839: break;
840: default:
841: for ( i = 0; i < nd_wpd; i++ )
842: if ( *d1++ != *d2++ ) return 0;
843: return 1;
844: break;
845: }
846: }
847:
848: INLINE void ndl_copy(UINT *d1,UINT *d2)
849: {
850: int i;
851:
852: switch ( nd_wpd ) {
853: case 2:
854: TD(d2) = TD(d1);
855: d2[1] = d1[1];
856: break;
857: case 3:
858: TD(d2) = TD(d1);
859: d2[1] = d1[1];
860: d2[2] = d1[2];
861: break;
862: default:
863: for ( i = 0; i < nd_wpd; i++ )
864: d2[i] = d1[i];
865: break;
866: }
867: }
868:
869: INLINE void ndl_zero(UINT *d)
870: {
871: int i;
872: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
873: }
874:
875: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
876: {
877: int i;
878:
879: if ( nd_module ) {
880: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
881: error("ndl_add : invalid operation");
882: }
883: #if 1
884: switch ( nd_wpd ) {
885: case 2:
886: TD(d) = TD(d1)+TD(d2);
887: d[1] = d1[1]+d2[1];
888: break;
889: case 3:
890: TD(d) = TD(d1)+TD(d2);
891: d[1] = d1[1]+d2[1];
892: d[2] = d1[2]+d2[2];
893: break;
894: default:
895: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
896: break;
897: }
898: #else
899: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
900: #endif
901: }
902:
903: /* d1 += d2 */
904: INLINE void ndl_addto(UINT *d1,UINT *d2)
905: {
906: int i;
907:
908: if ( nd_module ) {
909: if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
910: error("ndl_addto : invalid operation");
911: }
912: #if 1
913: switch ( nd_wpd ) {
914: case 2:
915: TD(d1) += TD(d2);
916: d1[1] += d2[1];
917: break;
918: case 3:
919: TD(d1) += TD(d2);
920: d1[1] += d2[1];
921: d1[2] += d2[2];
922: break;
923: default:
924: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
925: break;
926: }
927: #else
928: for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
929: #endif
930: }
931:
932: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
933: {
934: int i;
935:
936: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
937: }
938:
939: int ndl_disjoint(UINT *d1,UINT *d2)
940: {
941: UINT t1,t2,u,u1,u2;
942: int i,j;
943:
944: if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
945: #if USE_UNROLL
946: switch ( nd_bpe ) {
947: case 3:
948: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
949: u1 = d1[i]; u2 = d2[i];
950: t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
951: t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
952: t1 = u1& 0xe00000; t2 = u2& 0xe00000; if ( t1&&t2 ) return 0;
953: t1 = u1& 0x1c0000; t2 = u2& 0x1c0000; if ( t1&&t2 ) return 0;
954: t1 = u1& 0x38000; t2 = u2& 0x38000; if ( t1&&t2 ) return 0;
955: t1 = u1& 0x7000; t2 = u2& 0x7000; if ( t1&&t2 ) return 0;
956: t1 = u1& 0xe00; t2 = u2& 0xe00; if ( t1&&t2 ) return 0;
957: t1 = u1& 0x1c0; t2 = u2& 0x1c0; if ( t1&&t2 ) return 0;
958: t1 = u1& 0x38; t2 = u2& 0x38; if ( t1&&t2 ) return 0;
959: t1 = u1& 0x7; t2 = u2& 0x7; if ( t1&&t2 ) return 0;
960: }
961: return 1;
962: break;
963: case 4:
964: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
965: u1 = d1[i]; u2 = d2[i];
966: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
967: t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
968: t1 = u1& 0xf00000; t2 = u2& 0xf00000; if ( t1&&t2 ) return 0;
969: t1 = u1& 0xf0000; t2 = u2& 0xf0000; if ( t1&&t2 ) return 0;
970: t1 = u1& 0xf000; t2 = u2& 0xf000; if ( t1&&t2 ) return 0;
971: t1 = u1& 0xf00; t2 = u2& 0xf00; if ( t1&&t2 ) return 0;
972: t1 = u1& 0xf0; t2 = u2& 0xf0; if ( t1&&t2 ) return 0;
973: t1 = u1& 0xf; t2 = u2& 0xf; if ( t1&&t2 ) return 0;
974: }
975: return 1;
976: break;
977: case 6:
978: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
979: u1 = d1[i]; u2 = d2[i];
980: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
981: t1 = u1& 0xfc0000; t2 = u2& 0xfc0000; if ( t1&&t2 ) return 0;
982: t1 = u1& 0x3f000; t2 = u2& 0x3f000; if ( t1&&t2 ) return 0;
983: t1 = u1& 0xfc0; t2 = u2& 0xfc0; if ( t1&&t2 ) return 0;
984: t1 = u1& 0x3f; t2 = u2& 0x3f; if ( t1&&t2 ) return 0;
985: }
986: return 1;
987: break;
988: case 8:
989: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
990: u1 = d1[i]; u2 = d2[i];
991: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
992: t1 = u1& 0xff0000; t2 = u2& 0xff0000; if ( t1&&t2 ) return 0;
993: t1 = u1& 0xff00; t2 = u2& 0xff00; if ( t1&&t2 ) return 0;
994: t1 = u1& 0xff; t2 = u2& 0xff; if ( t1&&t2 ) return 0;
995: }
996: return 1;
997: break;
998: case 16:
999: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1000: u1 = d1[i]; u2 = d2[i];
1001: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1002: t1 = u1& 0xffff; t2 = u2& 0xffff; if ( t1&&t2 ) return 0;
1003: }
1004: return 1;
1005: break;
1006: case 32:
1007: for ( i = nd_exporigin; i < nd_wpd; i++ )
1008: if ( d1[i] && d2[i] ) return 0;
1009: return 1;
1010: break;
1011: default:
1012: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1013: u1 = d1[i]; u2 = d2[i];
1014: for ( j = 0; j < nd_epw; j++ ) {
1015: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1016: u1 >>= nd_bpe; u2 >>= nd_bpe;
1017: }
1018: }
1019: return 1;
1020: break;
1021: }
1022: #else
1023: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1024: u1 = d1[i]; u2 = d2[i];
1025: for ( j = 0; j < nd_epw; j++ ) {
1026: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
1027: u1 >>= nd_bpe; u2 >>= nd_bpe;
1028: }
1029: }
1030: return 1;
1031: #endif
1032: }
1033:
1034: int ndl_check_bound(UINT *d1,UINT *d2)
1035: {
1036: UINT u2;
1037: int i,j,ind,k;
1038:
1039: ind = 0;
1040: #if USE_UNROLL
1041: switch ( nd_bpe ) {
1042: case 3:
1043: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1044: u2 = d2[i];
1045: if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
1046: if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
1047: if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
1048: if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
1049: if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
1050: if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
1051: if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
1052: if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
1053: if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
1054: if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
1055: }
1056: return 0;
1057: break;
1058: case 4:
1059: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1060: u2 = d2[i];
1061: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
1062: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
1063: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
1064: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
1065: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
1066: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
1067: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
1068: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1069: }
1070: return 0;
1071: break;
1072: case 6:
1073: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1074: u2 = d2[i];
1075: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
1076: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
1077: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
1078: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
1079: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1080: }
1081: return 0;
1082: break;
1083: case 8:
1084: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1085: u2 = d2[i];
1086: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
1087: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
1088: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
1089: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1090: }
1091: return 0;
1092: break;
1093: case 16:
1094: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1095: u2 = d2[i];
1096: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
1097: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1098: }
1099: return 0;
1100: break;
1101: case 32:
1102: for ( i = nd_exporigin; i < nd_wpd; i++ )
1103: if ( d1[i]+d2[i]<d1[i] ) return 1;
1104: return 0;
1105: break;
1106: default:
1107: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1108: u2 = d2[i];
1109: k = (nd_epw-1)*nd_bpe;
1110: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1111: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1112: }
1113: return 0;
1114: break;
1115: }
1116: #else
1117: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1118: u2 = d2[i];
1119: k = (nd_epw-1)*nd_bpe;
1120: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1121: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1122: }
1123: return 0;
1124: #endif
1125: }
1126:
1127: int ndl_check_bound2(int index,UINT *d2)
1128: {
1129: return ndl_check_bound(nd_bound[index],d2);
1130: }
1131:
1132: INLINE int ndl_hash_value(UINT *d)
1133: {
1134: int i;
1.11 noro 1135: UINT r;
1.1 noro 1136:
1137: r = 0;
1138: for ( i = 0; i < nd_wpd; i++ )
1.12 noro 1139: r = (r*1511+d[i]);
1.11 noro 1140: r %= REDTAB_LEN;
1.1 noro 1141: return r;
1142: }
1143:
1144: INLINE int ndl_find_reducer(UINT *dg)
1145: {
1146: RHist r;
1147: int d,k,i;
1148:
1149: d = ndl_hash_value(dg);
1150: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1151: if ( ndl_equal(dg,DL(r)) ) {
1152: if ( k > 0 ) nd_notfirst++;
1153: nd_found++;
1154: return r->index;
1155: }
1156: }
1157: if ( Reverse )
1158: for ( i = nd_psn-1; i >= 0; i-- ) {
1159: r = nd_psh[i];
1160: if ( ndl_reducible(dg,DL(r)) ) {
1161: nd_create++;
1162: nd_append_red(dg,i);
1163: return i;
1164: }
1165: }
1166: else
1167: for ( i = 0; i < nd_psn; i++ ) {
1168: r = nd_psh[i];
1169: if ( ndl_reducible(dg,DL(r)) ) {
1170: nd_create++;
1171: nd_append_red(dg,i);
1172: return i;
1173: }
1174: }
1175: return -1;
1176: }
1177:
1178: ND nd_merge(ND p1,ND p2)
1179: {
1180: int n,c;
1181: int t,can,td1,td2;
1182: ND r;
1183: NM m1,m2,mr0,mr,s;
1184:
1185: if ( !p1 ) return p2;
1186: else if ( !p2 ) return p1;
1187: else {
1188: can = 0;
1189: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1190: c = DL_COMPARE(DL(m1),DL(m2));
1191: switch ( c ) {
1192: case 0:
1193: s = m1; m1 = NEXT(m1);
1194: can++; NEXTNM2(mr0,mr,s);
1195: s = m2; m2 = NEXT(m2); FREENM(s);
1196: break;
1197: case 1:
1198: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1199: break;
1200: case -1:
1201: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1202: break;
1203: }
1204: }
1205: if ( !mr0 )
1206: if ( m1 ) mr0 = m1;
1207: else if ( m2 ) mr0 = m2;
1208: else return 0;
1209: else if ( m1 ) NEXT(mr) = m1;
1210: else if ( m2 ) NEXT(mr) = m2;
1211: else NEXT(mr) = 0;
1212: BDY(p1) = mr0;
1213: SG(p1) = MAX(SG(p1),SG(p2));
1214: LEN(p1) = LEN(p1)+LEN(p2)-can;
1215: FREEND(p2);
1216: return p1;
1217: }
1218: }
1219:
1220: ND nd_add(int mod,ND p1,ND p2)
1221: {
1222: int n,c;
1223: int t,can,td1,td2;
1224: ND r;
1225: NM m1,m2,mr0,mr,s;
1226:
1.11 noro 1227: Nnd_add++;
1.1 noro 1228: if ( !p1 ) return p2;
1229: else if ( !p2 ) return p1;
1230: else if ( mod == -1 ) return nd_add_sf(p1,p2);
1231: else if ( mod == -2 ) return nd_add_lf(p1,p2);
1232: else if ( !mod ) return nd_add_q(p1,p2);
1233: else {
1234: can = 0;
1235: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1236: c = DL_COMPARE(DL(m1),DL(m2));
1237: switch ( c ) {
1238: case 0:
1239: t = ((CM(m1))+(CM(m2))) - mod;
1240: if ( t < 0 ) t += mod;
1241: s = m1; m1 = NEXT(m1);
1242: if ( t ) {
1243: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1244: } else {
1245: can += 2; FREENM(s);
1246: }
1247: s = m2; m2 = NEXT(m2); FREENM(s);
1248: break;
1249: case 1:
1250: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1251: break;
1252: case -1:
1253: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1254: break;
1255: }
1256: }
1257: if ( !mr0 )
1258: if ( m1 ) mr0 = m1;
1259: else if ( m2 ) mr0 = m2;
1260: else return 0;
1261: else if ( m1 ) NEXT(mr) = m1;
1262: else if ( m2 ) NEXT(mr) = m2;
1263: else NEXT(mr) = 0;
1264: BDY(p1) = mr0;
1265: SG(p1) = MAX(SG(p1),SG(p2));
1266: LEN(p1) = LEN(p1)+LEN(p2)-can;
1267: FREEND(p2);
1268: return p1;
1269: }
1270: }
1271:
1272: /* XXX on opteron, the inlined manipulation of destructive additon of
1273: * two NM seems to make gcc optimizer get confused, so the part is
1274: * done in a function.
1275: */
1276:
1277: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1278: {
1279: NM s;
1280: P t;
1281: int can;
1282:
1283: addp(nd_vc,CP(*m1),CP(*m2),&t);
1284: s = *m1; *m1 = NEXT(*m1);
1285: if ( t ) {
1286: can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1287: } else {
1288: can = 2; FREENM(s);
1289: }
1290: s = *m2; *m2 = NEXT(*m2); FREENM(s);
1291: return can;
1292: }
1293:
1294: ND nd_add_q(ND p1,ND p2)
1295: {
1296: int n,c,can;
1297: ND r;
1298: NM m1,m2,mr0,mr,s;
1299: P t;
1300:
1301: if ( !p1 ) return p2;
1302: else if ( !p2 ) return p1;
1303: else {
1304: can = 0;
1305: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1306: c = DL_COMPARE(DL(m1),DL(m2));
1307: switch ( c ) {
1308: case 0:
1309: #if defined(__x86_64__)
1310: can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1311: #else
1312: addp(nd_vc,CP(m1),CP(m2),&t);
1313: s = m1; m1 = NEXT(m1);
1314: if ( t ) {
1315: can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1316: } else {
1317: can += 2; FREENM(s);
1318: }
1319: s = m2; m2 = NEXT(m2); FREENM(s);
1320: #endif
1321: break;
1322: case 1:
1323: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1324: break;
1325: case -1:
1326: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1327: break;
1328: }
1329: }
1330: if ( !mr0 )
1331: if ( m1 ) mr0 = m1;
1332: else if ( m2 ) mr0 = m2;
1333: else return 0;
1334: else if ( m1 ) NEXT(mr) = m1;
1335: else if ( m2 ) NEXT(mr) = m2;
1336: else NEXT(mr) = 0;
1337: BDY(p1) = mr0;
1338: SG(p1) = MAX(SG(p1),SG(p2));
1339: LEN(p1) = LEN(p1)+LEN(p2)-can;
1340: FREEND(p2);
1341: return p1;
1342: }
1343: }
1344:
1345: ND nd_add_sf(ND p1,ND p2)
1346: {
1347: int n,c,can;
1348: ND r;
1349: NM m1,m2,mr0,mr,s;
1350: int t;
1351:
1352: if ( !p1 ) return p2;
1353: else if ( !p2 ) return p1;
1354: else {
1355: can = 0;
1356: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1357: c = DL_COMPARE(DL(m1),DL(m2));
1358: switch ( c ) {
1359: case 0:
1360: t = _addsf(CM(m1),CM(m2));
1361: s = m1; m1 = NEXT(m1);
1362: if ( t ) {
1363: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1364: } else {
1365: can += 2; FREENM(s);
1366: }
1367: s = m2; m2 = NEXT(m2); FREENM(s);
1368: break;
1369: case 1:
1370: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1371: break;
1372: case -1:
1373: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1374: break;
1375: }
1376: }
1377: if ( !mr0 )
1378: if ( m1 ) mr0 = m1;
1379: else if ( m2 ) mr0 = m2;
1380: else return 0;
1381: else if ( m1 ) NEXT(mr) = m1;
1382: else if ( m2 ) NEXT(mr) = m2;
1383: else NEXT(mr) = 0;
1384: BDY(p1) = mr0;
1385: SG(p1) = MAX(SG(p1),SG(p2));
1386: LEN(p1) = LEN(p1)+LEN(p2)-can;
1387: FREEND(p2);
1388: return p1;
1389: }
1390: }
1391:
1392:
1393: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1394: {
1395: int c,c1,c2;
1396: Z cg,cred,gcd,tq;
1397: P cgp,credp,gcdp;
1398: Obj tr,tr1;
1399:
1400: if ( mod == -1 ) {
1401: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1402: *divp = (Obj)ONE;
1403: } else if ( mod == -2 ) {
1404: Z inv,t;
1405: divlf(ONE,HCZ(p),&inv);
1406: chsgnlf(HCZ(g),&t);
1407: mullf(inv,t,&CZ(mul));
1408: *divp = (Obj)ONE;
1409: } else if ( mod ) {
1410: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1411: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1412: *divp = (Obj)ONE;
1413: } else if ( nd_vc ) {
1414: ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
1415: divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
1416: chsgnp(cgp,&CP(mul));
1417: nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
1418: if ( dn ) {
1419: mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
1420: reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
1421: }
1422: *divp = (Obj)credp;
1423: } else {
1.6 noro 1424: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1425: chsgnz(cg,&CZ(mul));
1.1 noro 1426: nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
1427: if ( dn ) {
1428: mulz(dn->z,cred,&tq); dn->z = tq;
1429: }
1430: *divp = (Obj)cred;
1431: }
1432: return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1433: }
1434:
1435: /* ret=1 : success, ret=0 : overflow */
1.6 noro 1436: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1 noro 1437: {
1438: NM m,mrd,tail;
1439: NM mul;
1440: int n,sugar,psugar,sugar0,stat,index;
1441: int c,c1,c2,dummy;
1442: RHist h;
1443: NDV p,red;
1444: Q cg,cred,gcd,tq,qq;
1445: Z iq;
1446: DP dmul;
1447: NODE node;
1448: LIST hist;
1449: double hmag;
1450: P tp,tp1;
1451: Obj tr,tr1,div;
1452: union oNDC hg;
1453: P cont;
1454:
1455: if ( !g ) {
1456: *rp = d;
1457: return 1;
1458: }
1459: if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1460:
1461: sugar0 = sugar = SG(g);
1462: n = NV(g);
1463: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1464: if ( d )
1465: for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
1466: for ( ; g; ) {
1467: index = ndl_find_reducer(HDL(g));
1468: if ( index >= 0 ) {
1469: h = nd_psh[index];
1470: ndl_sub(HDL(g),DL(h),DL(mul));
1471: if ( ndl_check_bound2(index,DL(mul)) ) {
1472: nd_free(g); nd_free(d);
1473: return 0;
1474: }
1475: p = nd_demand ? ndv_load(index) : ps[index];
1476: /* d+g -> div*(d+g)+mul*p */
1.6 noro 1477: g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1 noro 1478: if ( nd_gentrace ) {
1479: /* Trace=[div,index,mul,ONE] */
1.6 noro 1480: STOZ(index,iq);
1.1 noro 1481: nmtodp(mod,mul,&dmul);
1482: node = mknode(4,div,iq,dmul,ONE);
1483: }
1484: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1485: if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1486: hg = HCU(g);
1487: nd_removecont2(d,g);
1.6 noro 1488: if ( nd_gentrace ) {
1.1 noro 1489: /* overwrite cont : Trace=[div,index,mul,cont] */
1.6 noro 1490: /* exact division */
1.1 noro 1491: cont = ndc_div(mod,hg,HCU(g));
1492: if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1493: }
1494: hmag = ((double)p_mag(HCP(g)))*nd_scale;
1495: }
1496: MKLIST(hist,node);
1497: MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1498: } else if ( !full ) {
1499: *rp = g;
1500: return 1;
1501: } else {
1502: m = BDY(g);
1503: if ( NEXT(m) ) {
1504: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1505: } else {
1506: FREEND(g); g = 0;
1507: }
1508: if ( d ) {
1509: NEXT(tail)=m; tail=m; LEN(d)++;
1510: } else {
1511: MKND(n,m,1,d); tail = BDY(d);
1512: }
1513: }
1514: }
1515: if ( d ) SG(d) = sugar;
1516: *rp = d;
1517: return 1;
1518: }
1519:
1520: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1521: {
1522: int hindex,index;
1523: NDV p;
1524: ND u,d,red;
1525: NODE l;
1526: NM mul,m,mrd,tail;
1527: int sugar,psugar,n,h_reducible;
1528: PGeoBucket bucket;
1529: int c,c1,c2;
1530: Z cg,cred,gcd,zzz;
1531: RHist h;
1532: double hmag,gmag;
1533: int count = 0;
1534: int hcount = 0;
1535:
1536: if ( !g ) {
1537: *rp = 0;
1538: return 1;
1539: }
1540: sugar = SG(g);
1541: n = NV(g);
1.6 noro 1542: if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1543: bucket = create_pbucket();
1544: add_pbucket(mod,bucket,g);
1545: d = 0;
1546: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1547: while ( 1 ) {
1548: if ( mod > 0 || mod == -1 )
1549: hindex = head_pbucket(mod,bucket);
1550: else if ( mod == -2 )
1551: hindex = head_pbucket_lf(bucket);
1552: else
1553: hindex = head_pbucket_q(bucket);
1554: if ( hindex < 0 ) {
1555: if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1556: if ( d ) SG(d) = sugar;
1557: *rp = d;
1558: return 1;
1559: }
1560: g = bucket->body[hindex];
1561: index = ndl_find_reducer(HDL(g));
1562: if ( index >= 0 ) {
1563: count++;
1564: if ( !d ) hcount++;
1565: h = nd_psh[index];
1566: ndl_sub(HDL(g),DL(h),DL(mul));
1567: if ( ndl_check_bound2(index,DL(mul)) ) {
1568: nd_free(d);
1569: free_pbucket(bucket);
1570: *rp = 0;
1571: return 0;
1572: }
1573: p = ps[index];
1574: if ( mod == -1 )
1575: CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1576: else if ( mod == -2 ) {
1577: Z inv,t;
1578: divlf(ONE,HCZ(p),&inv);
1579: chsgnlf(HCZ(g),&t);
1580: mullf(inv,t,&CZ(mul));
1581: } else if ( mod ) {
1582: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1583: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1584: } else {
1.6 noro 1585: igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
1586: chsgnz(cg,&CZ(mul));
1.1 noro 1587: nd_mul_c_q(d,(P)cred);
1588: mulq_pbucket(bucket,cred);
1589: g = bucket->body[hindex];
1.6 noro 1590: gmag = (double)p_mag((P)HCZ(g));
1.1 noro 1591: }
1592: red = ndv_mul_nm(mod,mul,p);
1593: bucket->body[hindex] = nd_remove_head(g);
1594: red = nd_remove_head(red);
1595: add_pbucket(mod,bucket,red);
1596: psugar = SG(p)+TD(DL(mul));
1597: sugar = MAX(sugar,psugar);
1598: if ( !mod && hmag && (gmag > hmag) ) {
1599: g = normalize_pbucket(mod,bucket);
1600: if ( !g ) {
1601: if ( d ) SG(d) = sugar;
1602: *rp = d;
1603: return 1;
1604: }
1605: nd_removecont2(d,g);
1.6 noro 1606: hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1 noro 1607: add_pbucket(mod,bucket,g);
1608: }
1609: } else if ( !full ) {
1610: g = normalize_pbucket(mod,bucket);
1611: if ( g ) SG(g) = sugar;
1612: *rp = g;
1613: return 1;
1614: } else {
1615: m = BDY(g);
1616: if ( NEXT(m) ) {
1617: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1618: } else {
1619: FREEND(g); g = 0;
1620: }
1621: bucket->body[hindex] = g;
1622: NEXT(m) = 0;
1623: if ( d ) {
1624: NEXT(tail)=m; tail=m; LEN(d)++;
1625: } else {
1626: MKND(n,m,1,d); tail = BDY(d);
1627: }
1628: }
1629: }
1630: }
1631:
1632: /* input : list of NDV, cand : list of NDV */
1633:
1634: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1635: {
1636: int n,i,stat;
1637: ND nf,d;
1638: NDV r;
1639: NODE t,s;
1640: union oNDC dn;
1641: Z q;
1642: LIST list;
1643:
1644: ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1645: n = length(cand);
1646:
1647: if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1648: /* membercheck : list is a subset of Id(cand) ? */
1649: for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1650: again:
1651: nd_tracelist = 0;
1652: if ( nd_bpe > obpe )
1653: r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
1654: else
1655: r = (NDV)BDY(t);
1656: #if 0
1657: // moved to nd_f4_lf_trace()
1658: if ( m == -2 ) ndv_mod(m,r);
1659: #endif
1660: d = ndvtond(m,r);
1.6 noro 1661: stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1 noro 1662: if ( !stat ) {
1663: nd_reconstruct(0,0);
1664: goto again;
1665: } else if ( nf ) return 0;
1666: if ( nd_gentrace ) {
1667: nd_tracelist = reverse_node(nd_tracelist);
1668: MKLIST(list,nd_tracelist);
1.6 noro 1669: STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1 noro 1670: MKNODE(s,list,nd_alltracelist);
1671: nd_alltracelist = s; nd_tracelist = 0;
1672: }
1673: if ( DP_Print ) { printf("."); fflush(stdout); }
1674: }
1675: if ( DP_Print ) { printf("\n"); }
1676: return 1;
1677: }
1678:
1679: ND nd_remove_head(ND p)
1680: {
1681: NM m;
1682:
1683: m = BDY(p);
1684: if ( !NEXT(m) ) {
1685: FREEND(p); p = 0;
1686: } else {
1687: BDY(p) = NEXT(m); LEN(p)--;
1688: }
1689: FREENM(m);
1690: return p;
1691: }
1692:
1693: ND nd_separate_head(ND p,ND *head)
1694: {
1695: NM m,m0;
1696: ND r;
1697:
1698: m = BDY(p);
1699: if ( !NEXT(m) ) {
1700: *head = p; p = 0;
1701: } else {
1702: m0 = m;
1703: BDY(p) = NEXT(m); LEN(p)--;
1704: NEXT(m0) = 0;
1705: MKND(NV(p),m0,1,r);
1706: *head = r;
1707: }
1708: return p;
1709: }
1710:
1711: PGeoBucket create_pbucket()
1712: {
1713: PGeoBucket g;
1714:
1715: g = CALLOC(1,sizeof(struct oPGeoBucket));
1716: g->m = -1;
1717: return g;
1718: }
1719:
1720: void free_pbucket(PGeoBucket b) {
1721: int i;
1722:
1723: for ( i = 0; i <= b->m; i++ )
1724: if ( b->body[i] ) {
1725: nd_free(b->body[i]);
1726: b->body[i] = 0;
1727: }
1728: GCFREE(b);
1729: }
1730:
1731: void add_pbucket_symbolic(PGeoBucket g,ND d)
1732: {
1733: int l,i,k,m;
1734:
1735: if ( !d )
1736: return;
1737: l = LEN(d);
1738: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1739: /* 2^(k-1) < l <= 2^k (=m) */
1740: d = nd_merge(g->body[k],d);
1741: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1742: g->body[k] = 0;
1743: d = nd_merge(g->body[k+1],d);
1744: }
1745: g->body[k] = d;
1746: g->m = MAX(g->m,k);
1747: }
1748:
1749: void add_pbucket(int mod,PGeoBucket g,ND d)
1750: {
1751: int l,i,k,m;
1752:
1753: if ( !d )
1754: return;
1755: l = LEN(d);
1756: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1757: /* 2^(k-1) < l <= 2^k (=m) */
1758: d = nd_add(mod,g->body[k],d);
1759: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1760: g->body[k] = 0;
1761: d = nd_add(mod,g->body[k+1],d);
1762: }
1763: g->body[k] = d;
1764: g->m = MAX(g->m,k);
1765: }
1766:
1767: void mulq_pbucket(PGeoBucket g,Z c)
1768: {
1769: int k;
1770:
1771: for ( k = 0; k <= g->m; k++ )
1772: nd_mul_c_q(g->body[k],(P)c);
1773: }
1774:
1775: NM remove_head_pbucket_symbolic(PGeoBucket g)
1776: {
1777: int j,i,k,c;
1778: NM head;
1779:
1780: k = g->m;
1781: j = -1;
1782: for ( i = 0; i <= k; i++ ) {
1783: if ( !g->body[i] ) continue;
1784: if ( j < 0 ) j = i;
1785: else {
1786: c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
1787: if ( c > 0 )
1788: j = i;
1789: else if ( c == 0 )
1790: g->body[i] = nd_remove_head(g->body[i]);
1791: }
1792: }
1793: if ( j < 0 ) return 0;
1794: else {
1795: head = BDY(g->body[j]);
1796: if ( !NEXT(head) ) {
1797: FREEND(g->body[j]);
1798: g->body[j] = 0;
1799: } else {
1800: BDY(g->body[j]) = NEXT(head);
1801: LEN(g->body[j])--;
1802: }
1803: return head;
1804: }
1805: }
1806:
1807: int head_pbucket(int mod,PGeoBucket g)
1808: {
1809: int j,i,c,k,nv,sum;
1810: UINT *di,*dj;
1811: ND gi,gj;
1812:
1813: k = g->m;
1814: while ( 1 ) {
1815: j = -1;
1816: for ( i = 0; i <= k; i++ ) {
1817: if ( !(gi = g->body[i]) )
1818: continue;
1819: if ( j < 0 ) {
1820: j = i;
1821: gj = g->body[j];
1822: dj = HDL(gj);
1823: sum = HCM(gj);
1824: } else {
1825: c = DL_COMPARE(HDL(gi),dj);
1826: if ( c > 0 ) {
1827: if ( sum ) HCM(gj) = sum;
1828: else g->body[j] = nd_remove_head(gj);
1829: j = i;
1830: gj = g->body[j];
1831: dj = HDL(gj);
1832: sum = HCM(gj);
1833: } else if ( c == 0 ) {
1834: if ( mod == -1 )
1835: sum = _addsf(sum,HCM(gi));
1836: else {
1837: sum = sum+HCM(gi)-mod;
1838: if ( sum < 0 ) sum += mod;
1839: }
1840: g->body[i] = nd_remove_head(gi);
1841: }
1842: }
1843: }
1844: if ( j < 0 ) return -1;
1845: else if ( sum ) {
1846: HCM(gj) = sum;
1847: return j;
1848: } else
1849: g->body[j] = nd_remove_head(gj);
1850: }
1851: }
1852:
1853: int head_pbucket_q(PGeoBucket g)
1854: {
1855: int j,i,c,k,nv;
1856: Z sum,t;
1857: ND gi,gj;
1858:
1859: k = g->m;
1860: while ( 1 ) {
1861: j = -1;
1862: for ( i = 0; i <= k; i++ ) {
1863: if ( !(gi = g->body[i]) ) continue;
1864: if ( j < 0 ) {
1865: j = i;
1866: gj = g->body[j];
1.6 noro 1867: sum = HCZ(gj);
1.1 noro 1868: } else {
1869: nv = NV(gi);
1870: c = DL_COMPARE(HDL(gi),HDL(gj));
1871: if ( c > 0 ) {
1.6 noro 1872: if ( sum ) HCZ(gj) = sum;
1.1 noro 1873: else g->body[j] = nd_remove_head(gj);
1874: j = i;
1875: gj = g->body[j];
1.6 noro 1876: sum = HCZ(gj);
1.1 noro 1877: } else if ( c == 0 ) {
1.6 noro 1878: addz(sum,HCZ(gi),&t);
1.1 noro 1879: sum = t;
1880: g->body[i] = nd_remove_head(gi);
1881: }
1882: }
1883: }
1884: if ( j < 0 ) return -1;
1885: else if ( sum ) {
1.6 noro 1886: HCZ(gj) = sum;
1.1 noro 1887: return j;
1888: } else
1889: g->body[j] = nd_remove_head(gj);
1890: }
1891: }
1892:
1893: int head_pbucket_lf(PGeoBucket g)
1894: {
1895: int j,i,c,k,nv;
1896: Z sum,t;
1897: ND gi,gj;
1898:
1899: k = g->m;
1900: while ( 1 ) {
1901: j = -1;
1902: for ( i = 0; i <= k; i++ ) {
1903: if ( !(gi = g->body[i]) ) continue;
1904: if ( j < 0 ) {
1905: j = i;
1906: gj = g->body[j];
1907: sum = HCZ(gj);
1908: } else {
1909: nv = NV(gi);
1910: c = DL_COMPARE(HDL(gi),HDL(gj));
1911: if ( c > 0 ) {
1912: if ( sum ) HCZ(gj) = sum;
1913: else g->body[j] = nd_remove_head(gj);
1914: j = i;
1915: gj = g->body[j];
1916: sum = HCZ(gj);
1917: } else if ( c == 0 ) {
1918: addlf(sum,HCZ(gi),&t);
1919: sum = t;
1920: g->body[i] = nd_remove_head(gi);
1921: }
1922: }
1923: }
1924: if ( j < 0 ) return -1;
1925: else if ( sum ) {
1926: HCZ(gj) = sum;
1927: return j;
1928: } else
1929: g->body[j] = nd_remove_head(gj);
1930: }
1931: }
1932:
1933: ND normalize_pbucket(int mod,PGeoBucket g)
1934: {
1935: int i;
1936: ND r,t;
1937:
1938: r = 0;
1939: for ( i = 0; i <= g->m; i++ ) {
1940: r = nd_add(mod,r,g->body[i]);
1941: g->body[i] = 0;
1942: }
1943: g->m = -1;
1944: return r;
1945: }
1946:
1947: #if 0
1948: void register_hcf(NDV p)
1949: {
1950: DCP dc,t;
1951: P hc,h;
1952: int c;
1953: NODE l,l1,prev;
1954:
1955: hc = p->body->c.p;
1956: if ( !nd_vc || NUM(hc) ) return;
1957: fctrp(nd_vc,hc,&dc);
1958: for ( t = dc; t; t = NEXT(t) ) {
1959: h = t->c;
1960: if ( NUM(h) ) continue;
1961: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1962: c = compp(nd_vc,h,(P)BDY(l));
1963: if ( c >= 0 ) break;
1964: }
1965: if ( !l || c > 0 ) {
1966: MKNODE(l1,h,l);
1967: if ( !prev )
1968: nd_hcf = l1;
1969: else
1970: NEXT(prev) = l1;
1971: }
1972: }
1973: }
1974: #else
1975: void register_hcf(NDV p)
1976: {
1977: DCP dc,t;
1978: P hc,h,q;
1979: Q dmy;
1980: int c;
1981: NODE l,l1,prev;
1982:
1983: hc = p->body->c.p;
1984: if ( NUM(hc) ) return;
1985: ptozp(hc,1,&dmy,&h);
1986: #if 1
1987: for ( l = nd_hcf; l; l = NEXT(l) ) {
1988: while ( 1 ) {
1989: if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
1990: else break;
1991: }
1992: }
1993: if ( NUM(h) ) return;
1994: #endif
1995: for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
1996: c = compp(nd_vc,h,(P)BDY(l));
1997: if ( c >= 0 ) break;
1998: }
1999: if ( !l || c > 0 ) {
2000: MKNODE(l1,h,l);
2001: if ( !prev )
2002: nd_hcf = l1;
2003: else
2004: NEXT(prev) = l1;
2005: }
2006: }
2007: #endif
2008:
2009: int do_diagonalize(int sugar,int m)
2010: {
1.6 noro 2011: int i,nh,stat;
2012: NODE r,g,t;
2013: ND h,nf,s,head;
2014: NDV nfv;
2015: Q q;
2016: P nm,nmp,dn,mnp,dnp,cont,cont1;
2017: union oNDC hc;
2018: NODE node;
2019: LIST l;
2020: Z iq;
1.1 noro 2021:
1.6 noro 2022: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2023: if ( nd_gentrace ) {
2024: /* Trace = [1,index,1,1] */
2025: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2026: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2027: }
2028: if ( nd_demand )
2029: nfv = ndv_load(i);
2030: else
2031: nfv = nd_ps[i];
2032: s = ndvtond(m,nfv);
2033: s = nd_separate_head(s,&head);
2034: stat = nd_nf(m,head,s,nd_ps,1,&nf);
2035: if ( !stat ) return 0;
2036: ndv_free(nfv);
2037: hc = HCU(nf); nd_removecont(m,nf);
2038: /* exact division */
2039: cont = ndc_div(m,hc,HCU(nf));
1.1 noro 2040: if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6 noro 2041: nfv = ndtondv(m,nf);
2042: nd_free(nf);
2043: nd_bound[i] = ndv_compute_bound(nfv);
2044: if ( !m ) register_hcf(nfv);
2045: if ( nd_demand ) {
2046: ndv_save(nfv,i);
2047: ndv_free(nfv);
2048: } else
2049: nd_ps[i] = nfv;
2050: }
2051: return 1;
1.1 noro 2052: }
2053:
2054: LIST compute_splist()
2055: {
2056: NODE g,tn0,tn,node;
2057: LIST l0;
2058: ND_pairs d,t;
2059: int i;
2060: Z i1,i2;
2061:
2062: g = 0; d = 0;
2063: for ( i = 0; i < nd_psn; i++ ) {
2064: d = update_pairs(d,g,i,0);
2065: g = update_base(g,i);
2066: }
2067: for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
2068: NEXTNODE(tn0,tn);
1.6 noro 2069: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 2070: node = mknode(2,i1,i2); MKLIST(l0,node);
2071: BDY(tn) = l0;
2072: }
2073: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
2074: return l0;
2075: }
2076:
2077: /* return value = 0 => input is not a GB */
2078:
2079: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
2080: {
1.6 noro 2081: int i,nh,sugar,stat;
2082: NODE r,g,t;
2083: ND_pairs d;
2084: ND_pairs l;
2085: ND h,nf,s,head,nf1;
2086: NDV nfv;
2087: Z q;
2088: union oNDC dn,hc;
2089: int diag_count = 0;
2090: P cont;
2091: LIST list;
2092:
1.11 noro 2093: Nnd_add = 0;
1.6 noro 2094: g = 0; d = 0;
2095: for ( i = 0; i < nd_psn; i++ ) {
2096: d = update_pairs(d,g,i,gensyz);
2097: g = update_base(g,i);
2098: }
2099: sugar = 0;
2100: while ( d ) {
1.1 noro 2101: again:
1.6 noro 2102: l = nd_minp(d,&d);
2103: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2104: if ( SG(l) != sugar ) {
2105: if ( ishomo ) {
2106: diag_count = 0;
2107: stat = do_diagonalize(sugar,m);
1.1 noro 2108: if ( !stat ) {
1.6 noro 2109: NEXT(l) = d; d = l;
2110: d = nd_reconstruct(0,d);
2111: goto again;
1.1 noro 2112: }
1.6 noro 2113: }
2114: sugar = SG(l);
2115: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2116: }
2117: stat = nd_sp(m,0,l,&h);
2118: if ( !stat ) {
2119: NEXT(l) = d; d = l;
2120: d = nd_reconstruct(0,d);
2121: goto again;
2122: }
1.1 noro 2123: #if USE_GEOBUCKET
1.6 noro 2124: stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
2125: :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2126: #else
1.6 noro 2127: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2128: #endif
1.6 noro 2129: if ( !stat ) {
2130: NEXT(l) = d; d = l;
2131: d = nd_reconstruct(0,d);
2132: goto again;
2133: } else if ( nf ) {
2134: if ( checkonly || gensyz ) return 0;
1.1 noro 2135: if ( nd_newelim ) {
2136: if ( nd_module ) {
2137: if ( MPOS(HDL(nf)) > 1 ) return 0;
2138: } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
2139: }
1.6 noro 2140: if ( DP_Print ) { printf("+"); fflush(stdout); }
2141: hc = HCU(nf);
2142: nd_removecont(m,nf);
2143: if ( !m && nd_nalg ) {
2144: nd_monic(0,&nf);
2145: nd_removecont(m,nf);
2146: }
2147: if ( nd_gentrace ) {
2148: /* exact division */
1.1 noro 2149: cont = ndc_div(m,hc,HCU(nf));
2150: if ( m || !UNIQ(cont) ) {
1.6 noro 2151: t = mknode(4,NULLP,NULLP,NULLP,cont);
2152: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1 noro 2153: nd_tracelist = t;
2154: }
2155: }
1.6 noro 2156: nfv = ndtondv(m,nf); nd_free(nf);
2157: nh = ndv_newps(m,nfv,0,0);
2158: if ( !m && (ishomo && ++diag_count == diag_period) ) {
2159: diag_count = 0;
2160: stat = do_diagonalize(sugar,m);
2161: if ( !stat ) {
2162: NEXT(l) = d; d = l;
2163: d = nd_reconstruct(1,d);
2164: goto again;
1.1 noro 2165: }
1.6 noro 2166: }
2167: d = update_pairs(d,g,nh,0);
2168: g = update_base(g,nh);
2169: FREENDP(l);
2170: } else {
2171: if ( nd_gentrace && gensyz ) {
2172: nd_tracelist = reverse_node(nd_tracelist);
2173: MKLIST(list,nd_tracelist);
2174: STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
2175: MKNODE(t,list,nd_alltracelist);
2176: nd_alltracelist = t; nd_tracelist = 0;
2177: }
2178: if ( DP_Print ) { printf("."); fflush(stdout); }
2179: FREENDP(l);
2180: }
2181: }
2182: conv_ilist(nd_demand,0,g,indp);
1.11 noro 2183: if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.1 noro 2184: return g;
2185: }
2186:
2187: /* splist = [[i1,i2],...] */
2188:
2189: int check_splist(int m,NODE splist)
2190: {
2191: NODE t,p;
2192: ND_pairs d,r,l;
2193: int stat;
2194: ND h,nf;
2195:
2196: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2197: p = BDY((LIST)BDY(t));
1.6 noro 2198: NEXTND_pairs(d,r);
2199: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2200: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2201: SG(r) = TD(LCM(r)); /* XXX */
2202: }
2203: if ( d ) NEXT(r) = 0;
2204:
1.6 noro 2205: while ( d ) {
1.1 noro 2206: again:
1.6 noro 2207: l = nd_minp(d,&d);
2208: stat = nd_sp(m,0,l,&h);
2209: if ( !stat ) {
2210: NEXT(l) = d; d = l;
2211: d = nd_reconstruct(0,d);
2212: goto again;
2213: }
2214: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
2215: if ( !stat ) {
2216: NEXT(l) = d; d = l;
2217: d = nd_reconstruct(0,d);
2218: goto again;
2219: } else if ( nf ) return 0;
1.1 noro 2220: if ( DP_Print) { printf("."); fflush(stdout); }
1.6 noro 2221: }
1.1 noro 2222: if ( DP_Print) { printf("done.\n"); fflush(stdout); }
2223: return 1;
2224: }
2225:
2226: int check_splist_f4(int m,NODE splist)
2227: {
2228: UINT *s0vect;
1.6 noro 2229: PGeoBucket bucket;
1.1 noro 2230: NODE p,rp0,t;
2231: ND_pairs d,r,l,ll;
2232: int col,stat;
2233:
2234: for ( d = 0, t = splist; t; t = NEXT(t) ) {
2235: p = BDY((LIST)BDY(t));
1.6 noro 2236: NEXTND_pairs(d,r);
2237: r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
2238: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1 noro 2239: SG(r) = TD(LCM(r)); /* XXX */
2240: }
2241: if ( d ) NEXT(r) = 0;
2242:
1.6 noro 2243: while ( d ) {
2244: l = nd_minsugarp(d,&d);
2245: bucket = create_pbucket();
2246: stat = nd_sp_f4(m,0,l,bucket);
2247: if ( !stat ) {
2248: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2249: NEXT(ll) = d; d = l;
2250: d = nd_reconstruct(0,d);
2251: continue;
2252: }
2253: if ( bucket->m < 0 ) continue;
2254: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
2255: if ( !col ) {
2256: for ( ll = l; NEXT(ll); ll = NEXT(ll) );
2257: NEXT(ll) = d; d = l;
2258: d = nd_reconstruct(0,d);
2259: continue;
1.1 noro 2260: }
1.6 noro 2261: if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
2262: }
2263: return 1;
1.1 noro 2264: }
2265:
2266: int do_diagonalize_trace(int sugar,int m)
2267: {
1.6 noro 2268: int i,nh,stat;
2269: NODE r,g,t;
2270: ND h,nf,nfq,s,head;
2271: NDV nfv,nfqv;
2272: Q q,den,num;
2273: union oNDC hc;
2274: NODE node;
2275: LIST l;
2276: Z iq;
2277: P cont,cont1;
1.1 noro 2278:
1.6 noro 2279: for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
2280: if ( nd_gentrace ) {
2281: /* Trace = [1,index,1,1] */
2282: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
2283: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2284: }
2285: /* for nd_ps */
2286: s = ndvtond(m,nd_ps[i]);
2287: s = nd_separate_head(s,&head);
2288: stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
2289: if ( !stat ) return 0;
2290: nf = nd_add(m,head,nf);
2291: ndv_free(nd_ps[i]);
2292: nd_ps[i] = ndtondv(m,nf);
2293: nd_free(nf);
2294:
2295: /* for nd_ps_trace */
2296: if ( nd_demand )
2297: nfv = ndv_load(i);
2298: else
2299: nfv = nd_ps_trace[i];
2300: s = ndvtond(0,nfv);
2301: s = nd_separate_head(s,&head);
2302: stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
2303: if ( !stat ) return 0;
2304: ndv_free(nfv);
2305: hc = HCU(nf); nd_removecont(0,nf);
2306: /* exact division */
1.1 noro 2307: cont = ndc_div(0,hc,HCU(nf));
1.6 noro 2308: if ( nd_gentrace ) finalize_tracelist(i,cont);
2309: nfv = ndtondv(0,nf);
2310: nd_free(nf);
2311: nd_bound[i] = ndv_compute_bound(nfv);
2312: register_hcf(nfv);
2313: if ( nd_demand ) {
2314: ndv_save(nfv,i);
2315: ndv_free(nfv);
2316: } else
2317: nd_ps_trace[i] = nfv;
2318: }
2319: return 1;
1.1 noro 2320: }
2321:
2322: static struct oEGT eg_invdalg;
2323: struct oEGT eg_le;
2324:
2325: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
2326: {
2327: NODE tn;
2328: P p1;
2329:
2330: for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
2331: substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
2332: }
2333: *r = p;
2334: }
2335:
2336: NODE nd_gb_trace(int m,int ishomo,int **indp)
2337: {
1.6 noro 2338: int i,nh,sugar,stat;
2339: NODE r,g,t;
2340: ND_pairs d;
2341: ND_pairs l;
2342: ND h,nf,nfq,s,head;
2343: NDV nfv,nfqv;
2344: Z q,den,num;
2345: P hc;
2346: union oNDC dn,hnfq;
2347: struct oEGT eg_monic,egm0,egm1;
2348: int diag_count = 0;
2349: P cont;
2350: LIST list;
2351:
2352: init_eg(&eg_monic);
2353: init_eg(&eg_invdalg);
2354: init_eg(&eg_le);
2355: g = 0; d = 0;
2356: for ( i = 0; i < nd_psn; i++ ) {
2357: d = update_pairs(d,g,i,0);
2358: g = update_base(g,i);
2359: }
2360: sugar = 0;
2361: while ( d ) {
1.1 noro 2362: again:
1.6 noro 2363: l = nd_minp(d,&d);
2364: if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
2365: if ( SG(l) != sugar ) {
1.1 noro 2366: #if 1
1.6 noro 2367: if ( ishomo ) {
2368: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2369: stat = do_diagonalize_trace(sugar,m);
2370: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2371: diag_count = 0;
1.1 noro 2372: if ( !stat ) {
1.6 noro 2373: NEXT(l) = d; d = l;
2374: d = nd_reconstruct(1,d);
2375: goto again;
1.1 noro 2376: }
1.6 noro 2377: }
2378: #endif
2379: sugar = SG(l);
2380: if ( DP_Print ) fprintf(asir_out,"%d",sugar);
2381: }
2382: stat = nd_sp(m,0,l,&h);
2383: if ( !stat ) {
2384: NEXT(l) = d; d = l;
2385: d = nd_reconstruct(1,d);
2386: goto again;
2387: }
1.1 noro 2388: #if USE_GEOBUCKET
1.6 noro 2389: stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1 noro 2390: #else
1.6 noro 2391: stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1 noro 2392: #endif
1.6 noro 2393: if ( !stat ) {
2394: NEXT(l) = d; d = l;
2395: d = nd_reconstruct(1,d);
2396: goto again;
2397: } else if ( nf ) {
2398: if ( nd_demand ) {
2399: nfqv = ndv_load(nd_psn);
2400: nfq = ndvtond(0,nfqv);
2401: } else
2402: nfq = 0;
2403: if ( !nfq ) {
2404: if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
2405: NEXT(l) = d; d = l;
2406: d = nd_reconstruct(1,d);
2407: goto again;
2408: }
2409: }
2410: if ( nfq ) {
2411: /* m|HC(nfq) => failure */
2412: if ( nd_vc ) {
2413: nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
2414: } else
2415: q = HCZ(nfq);
2416: if ( !remqi((Q)q,m) ) return 0;
2417:
2418: if ( DP_Print ) { printf("+"); fflush(stdout); }
2419: hnfq = HCU(nfq);
2420: if ( nd_nalg ) {
2421: /* m|DN(HC(nf)^(-1)) => failure */
2422: get_eg(&egm0);
2423: if ( !nd_monic(m,&nfq) ) return 0;
2424: get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
2425: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2426: nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
2427: } else {
2428: nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
2429: nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
2430: }
2431: if ( nd_gentrace ) {
2432: /* exact division */
2433: cont = ndc_div(0,hnfq,HCU(nfqv));
2434: if ( !UNIQ(cont) ) {
2435: t = mknode(4,NULLP,NULLP,NULLP,cont);
2436: MKLIST(list,t); MKNODE(t,list,nd_tracelist);
2437: nd_tracelist = t;
2438: }
2439: }
2440: nh = ndv_newps(0,nfv,nfqv,0);
2441: if ( ishomo && ++diag_count == diag_period ) {
2442: diag_count = 0;
2443: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2444: stat = do_diagonalize_trace(sugar,m);
2445: if ( DP_Print > 2 ) fprintf(asir_out,"|");
2446: if ( !stat ) {
1.1 noro 2447: NEXT(l) = d; d = l;
2448: d = nd_reconstruct(1,d);
2449: goto again;
1.6 noro 2450: }
1.1 noro 2451: }
1.6 noro 2452: d = update_pairs(d,g,nh,0);
2453: g = update_base(g,nh);
2454: } else {
2455: if ( DP_Print ) { printf("*"); fflush(stdout); }
2456: }
2457: } else {
2458: if ( DP_Print ) { printf("."); fflush(stdout); }
1.1 noro 2459: }
1.6 noro 2460: FREENDP(l);
2461: }
2462: if ( nd_nalg ) {
2463: if ( DP_Print ) {
2464: print_eg("monic",&eg_monic);
2465: print_eg("invdalg",&eg_invdalg);
2466: print_eg("le",&eg_le);
1.1 noro 2467: }
1.6 noro 2468: }
1.1 noro 2469: conv_ilist(nd_demand,1,g,indp);
1.6 noro 2470: if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
2471: return g;
1.1 noro 2472: }
2473:
2474: int ndv_compare(NDV *p1,NDV *p2)
2475: {
2476: return DL_COMPARE(HDL(*p1),HDL(*p2));
2477: }
2478:
2479: int ndv_compare_rev(NDV *p1,NDV *p2)
2480: {
2481: return -DL_COMPARE(HDL(*p1),HDL(*p2));
2482: }
2483:
2484: int ndvi_compare(NDVI p1,NDVI p2)
2485: {
2486: return DL_COMPARE(HDL(p1->p),HDL(p2->p));
2487: }
2488:
2489: int ndvi_compare_rev(NDVI p1,NDVI p2)
2490: {
2491: return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
2492: }
2493:
2494: NODE ndv_reduceall(int m,NODE f)
2495: {
2496: int i,j,n,stat;
2497: ND nf,g,head;
2498: NODE t,a0,a;
2499: union oNDC dn;
2500: Q q,num,den;
2501: NODE node;
2502: LIST l;
2503: Z iq,jq;
2504: int *perm;
2505: union oNDC hc;
2506: P cont,cont1;
2507:
2508: if ( nd_nora ) return f;
2509: n = length(f);
2510: ndv_setup(m,0,f,0,1);
2511: perm = (int *)MALLOC(n*sizeof(int));
2512: if ( nd_gentrace ) {
2513: for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6 noro 2514: perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1 noro 2515: }
2516: for ( i = 0; i < n; ) {
2517: if ( nd_gentrace ) {
2518: /* Trace = [1,index,1,1] */
1.6 noro 2519: STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1 noro 2520: MKLIST(l,node); MKNODE(nd_tracelist,l,0);
2521: }
2522: g = ndvtond(m,nd_ps[i]);
2523: g = nd_separate_head(g,&head);
1.6 noro 2524: stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1 noro 2525: if ( !stat )
2526: nd_reconstruct(0,0);
2527: else {
2528: if ( DP_Print ) { printf("."); fflush(stdout); }
2529: ndv_free(nd_ps[i]);
2530: hc = HCU(nf); nd_removecont(m,nf);
2531: if ( nd_gentrace ) {
2532: for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6 noro 2533: jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
2534: STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1 noro 2535: }
1.6 noro 2536: /* exact division */
1.1 noro 2537: cont = ndc_div(m,hc,HCU(nf));
2538: finalize_tracelist(perm[i],cont);
2539: }
2540: nd_ps[i] = ndtondv(m,nf); nd_free(nf);
2541: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
2542: i++;
2543: }
2544: }
2545: if ( DP_Print ) { printf("\n"); }
2546: for ( a0 = 0, i = 0; i < n; i++ ) {
2547: NEXTNODE(a0,a);
2548: if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
2549: else {
2550: for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
2551: BDY(a) = (pointer)nd_ps[j];
2552: }
2553: }
2554: NEXT(a) = 0;
2555: return a0;
2556: }
2557:
2558: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
2559: {
2560: ND_pairs d1,nd,cur,head,prev,remove;
2561:
2562: if ( !g ) return d;
2563: /* for testing */
2564: if ( gensyz && nd_gensyz == 2 ) {
2565: d1 = nd_newpairs(g,t);
2566: if ( !d )
2567: return d1;
2568: else {
2569: nd = d;
2570: while ( NEXT(nd) ) nd = NEXT(nd);
2571: NEXT(nd) = d1;
2572: return d;
2573: }
2574: }
2575: d = crit_B(d,t);
2576: d1 = nd_newpairs(g,t);
2577: d1 = crit_M(d1);
2578: d1 = crit_F(d1);
2579: if ( gensyz || do_weyl )
2580: head = d1;
2581: else {
2582: prev = 0; cur = head = d1;
2583: while ( cur ) {
2584: if ( crit_2( cur->i1,cur->i2 ) ) {
2585: remove = cur;
2586: if ( !prev ) head = cur = NEXT(cur);
2587: else cur = NEXT(prev) = NEXT(cur);
2588: FREENDP(remove);
2589: } else {
2590: prev = cur; cur = NEXT(cur);
2591: }
2592: }
2593: }
2594: if ( !d )
2595: return head;
2596: else {
2597: nd = d;
2598: while ( NEXT(nd) ) nd = NEXT(nd);
2599: NEXT(nd) = head;
2600: return d;
2601: }
2602: }
2603:
2604:
2605: ND_pairs nd_newpairs( NODE g, int t )
2606: {
2607: NODE h;
2608: UINT *dl;
2609: int ts,s,i,t0,min,max;
2610: ND_pairs r,r0;
2611:
2612: dl = DL(nd_psh[t]);
2613: ts = SG(nd_psh[t]) - TD(dl);
1.17 noro 2614: if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1 noro 2615: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
2616: if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
2617: continue;
2618: if ( nd_gbblock ) {
2619: t0 = (long)BDY(h);
2620: for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
2621: min = nd_gbblock[i]; max = nd_gbblock[i+1];
2622: if ( t0 >= min && t0 <= max && t >= min && t <= max )
2623: break;
2624: }
2625: if ( nd_gbblock[i] >= 0 )
2626: continue;
2627: }
2628: NEXTND_pairs(r0,r);
2629: r->i1 = (long)BDY(h);
2630: r->i2 = t;
2631: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
2632: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2633: SG(r) = MAX(s,ts) + TD(LCM(r));
2634: /* experimental */
2635: if ( nd_sugarweight )
2636: r->sugar2 = ndl_weight2(r->lcm);
2637: }
2638: if ( r0 ) NEXT(r) = 0;
2639: return r0;
2640: }
2641:
2642: /* ipair = [i1,i2],[i1,i2],... */
2643: ND_pairs nd_ipairtospair(NODE ipair)
2644: {
2645: int s1,s2;
2646: NODE tn,t;
2647: ND_pairs r,r0;
2648:
2649: for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
2650: NEXTND_pairs(r0,r);
2651: tn = BDY((LIST)BDY(t));
1.6 noro 2652: r->i1 = ZTOS((Q)ARG0(tn));
2653: r->i2 = ZTOS((Q)ARG1(tn));
1.1 noro 2654: ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
2655: s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
2656: s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
2657: SG(r) = MAX(s1,s2) + TD(LCM(r));
2658: /* experimental */
2659: if ( nd_sugarweight )
2660: r->sugar2 = ndl_weight2(r->lcm);
2661: }
2662: if ( r0 ) NEXT(r) = 0;
2663: return r0;
2664: }
2665:
2666: /* kokokara */
2667:
2668: ND_pairs crit_B( ND_pairs d, int s )
2669: {
2670: ND_pairs cur,head,prev,remove;
2671: UINT *t,*tl,*lcm;
2672: int td,tdl;
2673:
2674: if ( !d ) return 0;
2675: t = DL(nd_psh[s]);
2676: prev = 0;
2677: head = cur = d;
2678: lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
2679: while ( cur ) {
2680: tl = cur->lcm;
2681: if ( ndl_reducible(tl,t) ) {
2682: ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
2683: if ( !ndl_equal(lcm,tl) ) {
2684: ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
2685: if (!ndl_equal(lcm,tl)) {
2686: remove = cur;
2687: if ( !prev ) {
2688: head = cur = NEXT(cur);
2689: } else {
2690: cur = NEXT(prev) = NEXT(cur);
2691: }
2692: FREENDP(remove);
2693: } else {
2694: prev = cur; cur = NEXT(cur);
2695: }
2696: } else {
2697: prev = cur; cur = NEXT(cur);
2698: }
2699: } else {
2700: prev = cur; cur = NEXT(cur);
2701: }
2702: }
2703: return head;
2704: }
2705:
2706: ND_pairs crit_M( ND_pairs d1 )
2707: {
2708: ND_pairs e,d2,d3,dd,p;
2709: UINT *id,*jd;
2710:
2711: if ( !d1 ) return d1;
2712: for ( dd = 0, e = d1; e; e = d3 ) {
2713: if ( !(d2 = NEXT(e)) ) {
2714: NEXT(e) = dd;
2715: return e;
2716: }
2717: id = LCM(e);
2718: for ( d3 = 0; d2; d2 = p ) {
2719: p = NEXT(d2);
2720: jd = LCM(d2);
2721: if ( ndl_equal(jd,id) )
2722: ;
2723: else if ( TD(jd) > TD(id) )
2724: if ( ndl_reducible(jd,id) ) continue;
2725: else ;
2726: else if ( ndl_reducible(id,jd) ) goto delit;
2727: NEXT(d2) = d3;
2728: d3 = d2;
2729: }
2730: NEXT(e) = dd;
2731: dd = e;
2732: continue;
2733: /**/
2734: delit: NEXT(d2) = d3;
2735: d3 = d2;
2736: for ( ; p; p = d2 ) {
2737: d2 = NEXT(p);
2738: NEXT(p) = d3;
2739: d3 = p;
2740: }
2741: FREENDP(e);
2742: }
2743: return dd;
2744: }
2745:
2746: ND_pairs crit_F( ND_pairs d1 )
2747: {
2748: ND_pairs rest, head,remove;
2749: ND_pairs last, p, r, w;
2750: int s;
2751:
2752: if ( !d1 ) return d1;
2753: for ( head = last = 0, p = d1; NEXT(p); ) {
2754: r = w = equivalent_pairs(p,&rest);
2755: s = SG(r);
2756: w = NEXT(w);
2757: while ( w ) {
2758: if ( crit_2(w->i1,w->i2) ) {
2759: r = w;
2760: w = NEXT(w);
2761: while ( w ) {
2762: remove = w;
2763: w = NEXT(w);
2764: FREENDP(remove);
2765: }
2766: break;
2767: } else if ( SG(w) < s ) {
2768: FREENDP(r);
2769: r = w;
2770: s = SG(r);
2771: w = NEXT(w);
2772: } else {
2773: remove = w;
2774: w = NEXT(w);
2775: FREENDP(remove);
2776: }
2777: }
2778: if ( last ) NEXT(last) = r;
2779: else head = r;
2780: NEXT(last = r) = 0;
2781: p = rest;
2782: if ( !p ) return head;
2783: }
2784: if ( !last ) return p;
2785: NEXT(last) = p;
2786: return head;
2787: }
2788:
2789: int crit_2( int dp1, int dp2 )
2790: {
2791: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
2792: }
2793:
2794: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
2795: {
2796: ND_pairs w,p,r,s;
2797: UINT *d;
2798:
2799: w = d1;
2800: d = LCM(w);
2801: s = NEXT(w);
2802: NEXT(w) = 0;
2803: for ( r = 0; s; s = p ) {
2804: p = NEXT(s);
2805: if ( ndl_equal(d,LCM(s)) ) {
2806: NEXT(s) = w; w = s;
2807: } else {
2808: NEXT(s) = r; r = s;
2809: }
2810: }
2811: *prest = r;
2812: return w;
2813: }
2814:
2815: NODE update_base(NODE nd,int ndp)
2816: {
2817: UINT *dl, *dln;
2818: NODE last, p, head;
2819:
2820: dl = DL(nd_psh[ndp]);
2821: for ( head = last = 0, p = nd; p; ) {
2822: dln = DL(nd_psh[(long)BDY(p)]);
2823: if ( ndl_reducible( dln, dl ) ) {
2824: p = NEXT(p);
2825: if ( last ) NEXT(last) = p;
2826: } else {
2827: if ( !last ) head = p;
2828: p = NEXT(last = p);
2829: }
2830: }
2831: head = append_one(head,ndp);
2832: return head;
2833: }
2834:
2835: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
2836: {
2837: ND_pairs m,ml,p,l;
2838: UINT *lcm;
2839: int s,td,len,tlen,c,c1;
2840:
2841: if ( !(p = NEXT(m = d)) ) {
2842: *prest = p;
2843: NEXT(m) = 0;
2844: return m;
2845: }
2846: if ( !NoSugar ) {
2847: if ( nd_sugarweight ) {
2848: s = m->sugar2;
2849: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2850: if ( (p->sugar2 < s)
2851: || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2852: ml = l; m = p; s = m->sugar2;
2853: }
2854: } else {
2855: s = SG(m);
2856: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2857: if ( (SG(p) < s)
2858: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
2859: ml = l; m = p; s = SG(m);
2860: }
2861: }
2862: } else {
2863: for ( ml = 0, l = m; p; p = NEXT(l = p) )
2864: if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
2865: ml = l; m = p; s = SG(m);
2866: }
2867: }
2868: if ( !ml ) *prest = NEXT(m);
2869: else {
2870: NEXT(ml) = NEXT(m);
2871: *prest = d;
2872: }
2873: NEXT(m) = 0;
2874: return m;
2875: }
2876:
2877: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
2878: {
2879: int msugar,i;
2880: ND_pairs t,dm0,dm,dr0,dr;
2881:
2882: if ( nd_sugarweight ) {
2883: for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
2884: if ( t->sugar2 < msugar ) msugar = t->sugar2;
2885: dm0 = 0; dr0 = 0;
2886: for ( i = 0, t = d; t; t = NEXT(t) )
2887: if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
2888: if ( dm0 ) NEXT(dm) = t;
2889: else dm0 = t;
2890: dm = t;
2891: i++;
2892: } else {
2893: if ( dr0 ) NEXT(dr) = t;
2894: else dr0 = t;
2895: dr = t;
2896: }
2897: } else {
2898: for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
2899: if ( SG(t) < msugar ) msugar = SG(t);
2900: dm0 = 0; dr0 = 0;
2901: for ( i = 0, t = d; t; t = NEXT(t) )
2902: if ( i < nd_f4_nsp && SG(t) == msugar ) {
2903: if ( dm0 ) NEXT(dm) = t;
2904: else dm0 = t;
2905: dm = t;
2906: i++;
2907: } else {
2908: if ( dr0 ) NEXT(dr) = t;
2909: else dr0 = t;
2910: dr = t;
2911: }
2912: }
2913: NEXT(dm) = 0;
2914: if ( dr0 ) NEXT(dr) = 0;
2915: *prest = dr0;
2916: return dm0;
2917: }
2918:
2919: int nd_tdeg(NDV c)
2920: {
2921: int wmax = 0;
2922: int i,len;
2923: NMV a;
2924:
2925: len = LEN(c);
2926: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2927: wmax = MAX(TD(DL(a)),wmax);
2928: return wmax;
2929: }
2930:
2931: int ndv_newps(int m,NDV a,NDV aq,int f4)
2932: {
2933: int len;
2934: RHist r;
2935: NDV b;
2936: NODE tn;
2937: LIST l;
2938: Z iq;
2939:
2940: if ( nd_psn == nd_pslen ) {
2941: nd_pslen *= 2;
2942: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
2943: nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
2944: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
2945: nd_bound = (UINT **)
2946: REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
2947: nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
2948: nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
2949: }
2950: NEWRHist(r); nd_psh[nd_psn] = r;
2951: nd_ps[nd_psn] = a;
2952: if ( aq ) {
2953: nd_ps_trace[nd_psn] = aq;
2954: if ( !m ) {
2955: register_hcf(aq);
2956: } else if ( m == -2 ) {
2957: /* do nothing */
2958: } else
2959: error("ndv_newps : invalud modulus");
2960: nd_bound[nd_psn] = ndv_compute_bound(aq);
2961: #if 1
2962: SG(r) = SG(aq);
2963: #else
2964: SG(r) = nd_tdeg(aq);
2965: #endif
2966: ndl_copy(HDL(aq),DL(r));
2967: } else {
2968: if ( !m ) register_hcf(a);
2969: nd_bound[nd_psn] = ndv_compute_bound(a);
2970: #if 1
2971: SG(r) = SG(a);
2972: #else
2973: SG(r) = nd_tdeg(a);
2974: #endif
2975: ndl_copy(HDL(a),DL(r));
2976: }
2977: if ( nd_demand ) {
2978: if ( aq ) {
2979: ndv_save(nd_ps_trace[nd_psn],nd_psn);
2980: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2981: nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
2982: nd_ps_trace[nd_psn] = 0;
2983: } else {
2984: ndv_save(nd_ps[nd_psn],nd_psn);
2985: nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
2986: nd_ps[nd_psn] = 0;
2987: }
2988: }
2989: if ( nd_gentrace ) {
2990: /* reverse the tracelist and append it to alltracelist */
2991: nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6 noro 2992: STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1 noro 2993: MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
2994: }
2995: return nd_psn++;
2996: }
2997:
2998: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
2999: /* return 1 if success, 0 if failure (HC(a mod p)) */
3000:
3001: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
3002: {
1.6 noro 3003: int i,j,td,len,max;
3004: NODE s,s0,f0,tn;
3005: UINT *d;
3006: RHist r;
3007: NDVI w;
3008: NDV a,am;
3009: union oNDC hc;
3010: NODE node;
3011: P hcp;
3012: Z iq,jq;
3013: LIST l;
3014:
3015: nd_found = 0; nd_notfirst = 0; nd_create = 0;
3016: /* initialize the tracelist */
3017: nd_tracelist = 0;
3018:
3019: for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
3020: w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
3021: for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
3022: if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
3023: if ( !dont_sort ) {
3024: /* XXX heuristic */
3025: if ( !nd_ord->id && (nd_ord->ord.simple<2) )
3026: qsort(w,nd_psn,sizeof(struct oNDVI),
3027: (int (*)(const void *,const void *))ndvi_compare_rev);
3028: else
3029: qsort(w,nd_psn,sizeof(struct oNDVI),
3030: (int (*)(const void *,const void *))ndvi_compare);
3031: }
3032: nd_pslen = 2*nd_psn;
3033: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3034: nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3035: nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3036: nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
3037: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
3038: nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
3039: nd_hcf = 0;
1.1 noro 3040:
1.6 noro 3041: if ( trace && nd_vc )
3042: makesubst(nd_vc,&nd_subst);
3043: else
3044: nd_subst = 0;
1.1 noro 3045:
1.6 noro 3046: if ( !nd_red )
3047: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
3048: for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
3049: for ( i = 0; i < nd_psn; i++ ) {
3050: hc = HCU(w[i].p);
3051: if ( trace ) {
3052: if ( mod == -2 ) {
3053: /* over a large finite field */
3054: /* trace = small modulus */
3055: a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
3056: ndv_mod(-2,a);
3057: if ( !dont_removecont) ndv_removecont(-2,a);
3058: am = nd_ps[i] = ndv_dup(trace,w[i].p);
3059: ndv_mod(trace,am);
3060: if ( DL_COMPARE(HDL(am),HDL(a)) )
3061: return 0;
3062: ndv_removecont(trace,am);
3063: } else {
3064: a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
3065: if ( !dont_removecont) ndv_removecont(0,a);
3066: register_hcf(a);
3067: am = nd_ps[i] = ndv_dup(mod,a);
3068: ndv_mod(mod,am);
3069: if ( DL_COMPARE(HDL(am),HDL(a)) )
3070: return 0;
3071: ndv_removecont(mod,am);
3072: }
3073: } else {
3074: a = nd_ps[i] = ndv_dup(mod,w[i].p);
3075: if ( mod || !dont_removecont ) ndv_removecont(mod,a);
3076: if ( !mod ) register_hcf(a);
1.1 noro 3077: }
1.6 noro 3078: if ( nd_gentrace ) {
3079: STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
3080: /* exact division */
1.1 noro 3081: if ( !dont_removecont )
1.6 noro 3082: ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
3083: MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
3084: }
3085: NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
3086: nd_bound[i] = ndv_compute_bound(a);
3087: nd_psh[i] = r;
3088: if ( nd_demand ) {
3089: if ( trace ) {
3090: ndv_save(nd_ps_trace[i],i);
3091: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3092: nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
3093: nd_ps_trace[i] = 0;
3094: } else {
3095: ndv_save(nd_ps[i],i);
3096: nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
3097: nd_ps[i] = 0;
3098: }
1.1 noro 3099: }
1.6 noro 3100: }
3101: if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
3102: return 1;
1.1 noro 3103: }
3104:
3105: struct order_spec *append_block(struct order_spec *spec,
3106: int nv,int nalg,int ord);
3107:
3108: extern VECT current_dl_weight_vector_obj;
3109: static VECT prev_weight_vector_obj;
3110:
3111: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
3112: struct order_spec **ord1p,LIST *f1p,NODE *alistp)
3113: {
3114: NODE alist,t,s,r0,r,arg;
3115: VL tv;
3116: P poly;
3117: DP d;
3118: Alg alpha,dp;
3119: DAlg inv,da,hc;
3120: MP m;
3121: int i,nvar,nalg,n;
3122: NumberField nf;
3123: LIST f1,f2;
3124: struct order_spec *current_spec;
3125: VECT obj,obj0;
3126: VECT tmp;
3127:
3128: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
3129: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
3130:
3131: for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
3132: NEXTNODE(alist,t); MKV(tv->v,poly);
3133: MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
3134: tv->v = tv->v->priv;
3135: }
3136: NEXT(t) = 0;
3137:
3138: /* simplification, making polynomials monic */
3139: setfield_dalg(alist);
3140: obj_algtodalg((Obj)f,(Obj *)&f1);
3141: for ( t = BDY(f); t; t = NEXT(t) ) {
3142: initd(ord); ptod(vv,vv,(P)BDY(t),&d);
3143: hc = (DAlg)BDY(d)->c;
3144: if ( NID(hc) == N_DA ) {
3145: invdalg(hc,&inv);
3146: for ( m = BDY(d); m; m = NEXT(m) ) {
3147: muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
3148: }
3149: }
3150: initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
3151: }
3152: obj_dalgtoalg((Obj)f1,(Obj *)&f);
3153:
3154: /* append alg vars to the var list */
3155: for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
3156: NEXT(tv) = av;
3157:
3158: /* append a block to ord */
3159: *ord1p = append_block(ord,nvar,nalg,2);
3160:
3161: /* create generator list */
3162: nf = get_numberfield();
3163: for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
3164: MKAlg(nf->defpoly[i],dp);
3165: MKNODE(s,dp,t); t = s;
3166: }
3167: MKLIST(f1,t);
3168: *alistp = alist;
3169: algobjtorat((Obj)f1,(Obj *)f1p);
3170:
3171: /* creating a new weight vector */
3172: prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
3173: n = nvar+nalg+1;
3174: MKVECT(obj,n);
3175: if ( obj0 && obj0->len == nvar )
3176: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
3177: else
3178: for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
3179: for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
3180: BDY(obj)[n-1] = (pointer)ONE;
3181: arg = mknode(1,obj);
3182: Pdp_set_weight(arg,&tmp);
3183: }
3184:
3185: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
3186: {
3187: NODE s,t,u0,u;
3188: P p;
3189: VL tv;
3190: Obj obj;
3191: VECT tmp;
3192: NODE arg;
3193:
3194: u0 = 0;
3195: for ( t = r; t; t = NEXT(t) ) {
3196: p = (P)BDY(t);
3197: for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
3198: substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
3199: }
3200: if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
3201: NEXTNODE(u0,u);
3202: BDY(u) = (pointer)p;
3203: }
3204: }
3205: arg = mknode(1,prev_weight_vector_obj);
3206: Pdp_set_weight(arg,&tmp);
3207:
3208: return u0;
3209: }
3210:
3211: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
3212: {
3213: VL tv,fv,vv,vc,av;
3214: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3215: int e,max,nvar,i;
3216: NDV b;
3217: int ishomo,nalg,mrank,trank,wmax,len;
3218: NMV a;
3219: Alg alpha,dp;
3220: P p,zp;
3221: Q dmy;
3222: LIST f1,f2,zpl;
3223: Obj obj;
3224: NumberField nf;
3225: struct order_spec *ord1;
3226: NODE tr,tl1,tl2,tl3,tl4,nzlist;
3227: LIST l1,l2,l3,l4,l5;
3228: int j;
3229: Z jq,bpe,last_nonzero;
3230: int *perm;
3231: EPOS oepos;
3232: int obpe,oadv,ompos,cbpe;
1.15 noro 3233: VECT hvect;
1.1 noro 3234:
3235: nd_module = 0;
3236: if ( !m && Demand ) nd_demand = 1;
3237: else nd_demand = 0;
3238: parse_nd_option(current_option);
3239:
3240: if ( DP_Multiple )
3241: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3242: #if 0
3243: ndv_alloc = 0;
3244: #endif
3245: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3246: if ( m && nd_vc )
3247: error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
3248: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3249: switch ( ord->id ) {
3250: case 1:
3251: if ( ord->nv != nvar )
3252: error("nd_{gr,f4} : invalid order specification");
3253: break;
3254: default:
3255: break;
3256: }
3257: nd_nalg = 0;
3258: av = 0;
3259: if ( !m ) {
3260: get_algtree((Obj)f,&av);
3261: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3262: nd_ntrans = nvar;
3263: nd_nalg = nalg;
3264: /* #i -> t#i */
3265: if ( nalg ) {
3266: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3267: ord = ord1;
3268: f = f1;
3269: }
3270: nvar += nalg;
3271: }
3272: nd_init_ord(ord);
3273: mrank = 0;
3274: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3275: for ( tv = vv; tv; tv = NEXT(tv) ) {
3276: if ( nd_module ) {
1.16 noro 3277: if ( OID(BDY(t)) == O_DPM ) {
3278: e = dpm_getdeg((DPM)BDY(t),&trank);
3279: max = MAX(e,max);
3280: mrank = MAX(mrank,trank);
3281: } else {
3282: s = BDY((LIST)BDY(t));
3283: trank = length(s);
3284: mrank = MAX(mrank,trank);
3285: for ( ; s; s = NEXT(s) ) {
3286: e = getdeg(tv->v,(P)BDY(s));
3287: max = MAX(e,max);
3288: }
1.1 noro 3289: }
3290: } else {
3291: e = getdeg(tv->v,(P)BDY(t));
3292: max = MAX(e,max);
3293: }
3294: }
3295: nd_setup_parameters(nvar,nd_nzlist?0:max);
3296: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3297: ishomo = 1;
3298: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3299: if ( nd_module ) {
1.16 noro 3300: if ( OID(BDY(t)) == O_DPM ) {
3301: Z cont;
3302: DPM zdpm;
3303:
3304: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
3305: else zdpm = (DPM)BDY(t);
3306: b = (pointer)dpmtondv(m,zdpm);
3307: } else {
3308: if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3309: else zpl = (LIST)BDY(t);
1.1 noro 3310: b = (pointer)pltondv(CO,vv,zpl);
1.16 noro 3311: }
1.1 noro 3312: } else {
3313: if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3314: else zp = (P)BDY(t);
3315: b = (pointer)ptondv(CO,vv,zp);
3316: }
3317: if ( ishomo )
3318: ishomo = ishomo && ndv_ishomo(b);
3319: if ( m ) ndv_mod(m,b);
3320: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3321: }
3322: if ( fd0 ) NEXT(fd) = 0;
3323:
3324: if ( !ishomo && homo ) {
3325: for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
3326: b = (NDV)BDY(t); len = LEN(b);
3327: for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
3328: wmax = MAX(TD(DL(a)),wmax);
3329: }
3330: homogenize_order(ord,nvar,&ord1);
3331: nd_init_ord(ord1);
3332: nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
3333: for ( t = fd0; t; t = NEXT(t) )
3334: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3335: }
3336:
3337: ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
3338: if ( nd_gentrace ) {
3339: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3340: }
3341: if ( nd_splist ) {
3342: *rp = compute_splist();
3343: return;
3344: }
3345: if ( nd_check_splist ) {
3346: if ( f4 ) {
3347: if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
3348: else *rp = 0;
3349: } else {
3350: if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
3351: else *rp = 0;
3352: }
3353: return;
3354: }
3355: x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
3356: if ( !x ) {
3357: *rp = 0; return;
3358: }
1.15 noro 3359: if ( nd_gentrace ) {
3360: MKVECT(hvect,nd_psn);
3361: for ( i = 0; i < nd_psn; i++ )
3362: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
3363: }
1.1 noro 3364: if ( !ishomo && homo ) {
3365: /* dehomogenization */
3366: for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3367: nd_init_ord(ord);
3368: nd_setup_parameters(nvar,0);
3369: }
3370: nd_demand = 0;
3371: if ( nd_module && nd_intersect ) {
3372: for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17 noro 3373: if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1 noro 3374: MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
3375: }
3376: conv_ilist(nd_demand,0,x,0);
3377: goto FINAL;
3378: }
3379: if ( nd_gentrace && f4 ) { nzlist = nd_alltracelist; }
3380: x = ndv_reducebase(x,perm);
3381: if ( nd_gentrace && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3382: x = ndv_reduceall(m,x);
3383: cbpe = nd_bpe;
3384: if ( nd_gentrace && !f4 ) {
3385: tl2 = nd_alltracelist; nd_alltracelist = 0;
3386: ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
3387: tl3 = nd_alltracelist; nd_alltracelist = 0;
3388: if ( nd_gensyz ) {
3389: nd_gb(m,0,1,1,0);
3390: tl4 = nd_alltracelist; nd_alltracelist = 0;
3391: } else tl4 = 0;
3392: }
3393: nd_bpe = cbpe;
3394: nd_setup_parameters(nd_nvar,0);
3395: FINAL:
3396: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16 noro 3397: NEXTNODE(r0,r);
3398: if ( nd_module ) {
3399: if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
3400: else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
3401: } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
3402: else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1 noro 3403: }
3404: if ( r0 ) NEXT(r) = 0;
3405: if ( !m && nd_nalg )
3406: r0 = postprocess_algcoef(av,alist,r0);
3407: MKLIST(*rp,r0);
3408: if ( nd_gentrace ) {
3409: if ( f4 ) {
1.6 noro 3410: STOZ(16,bpe);
3411: STOZ(nd_last_nonzero,last_nonzero);
1.15 noro 3412: tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1 noro 3413: } else {
3414: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3415: tl3 = reverse_node(tl3);
3416: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3417: for ( t = tl2; t; t = NEXT(t) ) {
3418: /* s = [i,[*,j,*,*],...] */
3419: s = BDY((LIST)BDY(t));
1.6 noro 3420: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3421: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3422: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3423: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3424: }
3425: }
3426: for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3427: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3428: }
3429: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3430: MKLIST(l5,tl4);
1.6 noro 3431: STOZ(nd_bpe,bpe);
1.15 noro 3432: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3433: }
3434: }
3435: #if 0
3436: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
3437: #endif
3438: }
3439:
3440: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
3441: {
3442: VL tv,fv,vv,vc,av;
3443: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3444: int e,max,nvar,i;
3445: NDV b;
3446: int ishomo,nalg;
3447: Alg alpha,dp;
3448: P p,zp;
3449: Q dmy;
3450: LIST f1,f2;
3451: Obj obj;
3452: NumberField nf;
3453: struct order_spec *ord1;
3454: int *perm;
3455:
3456: parse_nd_option(current_option);
3457: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3458: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3459: switch ( ord->id ) {
3460: case 1:
3461: if ( ord->nv != nvar )
3462: error("nd_check : invalid order specification");
3463: break;
3464: default:
3465: break;
3466: }
3467: nd_nalg = 0;
3468: av = 0;
3469: if ( !m ) {
3470: get_algtree((Obj)f,&av);
3471: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3472: nd_ntrans = nvar;
3473: nd_nalg = nalg;
3474: /* #i -> t#i */
3475: if ( nalg ) {
3476: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3477: ord = ord1;
3478: f = f1;
3479: }
3480: nvar += nalg;
3481: }
3482: nd_init_ord(ord);
3483: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3484: for ( tv = vv; tv; tv = NEXT(tv) ) {
3485: e = getdeg(tv->v,(P)BDY(t));
3486: max = MAX(e,max);
3487: }
3488: nd_setup_parameters(nvar,max);
3489: ishomo = 1;
3490: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3491: ptozp((P)BDY(t),1,&dmy,&zp);
3492: b = (pointer)ptondv(CO,vv,zp);
3493: if ( ishomo )
3494: ishomo = ishomo && ndv_ishomo(b);
3495: if ( m ) ndv_mod(m,b);
3496: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
3497: }
3498: if ( fd0 ) NEXT(fd) = 0;
3499: ndv_setup(m,0,fd0,0,1);
3500: for ( x = 0, i = 0; i < nd_psn; i++ )
3501: x = update_base(x,i);
3502: if ( do_check ) {
3503: x = nd_gb(m,ishomo,1,0,&perm);
3504: if ( !x ) {
3505: *rp = 0;
3506: return;
3507: }
3508: } else {
3509: #if 0
3510: /* bug ? */
3511: for ( t = x; t; t = NEXT(t) )
3512: BDY(t) = (pointer)nd_ps[(long)BDY(t)];
3513: #else
3514: conv_ilist(0,0,x,&perm);
3515: #endif
3516: }
3517: x = ndv_reducebase(x,perm);
3518: x = ndv_reduceall(m,x);
3519: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
3520: NEXTNODE(r0,r);
3521: BDY(r) = ndvtop(m,CO,vv,BDY(t));
3522: }
3523: if ( r0 ) NEXT(r) = 0;
3524: if ( !m && nd_nalg )
3525: r0 = postprocess_algcoef(av,alist,r0);
3526: MKLIST(*rp,r0);
3527: }
3528:
3529: NDV recompute_trace(NODE trace,NDV *p,int m);
3530: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
3531:
3532: NDV recompute_trace(NODE ti,NDV *p,int mod)
3533: {
3534: int c,c1,c2,i;
3535: NM mul,m,tail;
3536: ND d,r,rm;
3537: NODE sj;
3538: NDV red;
3539: Obj mj;
3540:
3541: mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
3542: CM(mul) = 1;
3543: tail = 0;
3544: for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
3545: sj = BDY((LIST)BDY(ti));
3546: if ( ARG0(sj) ) {
1.6 noro 3547: red = p[ZTOS((Q)ARG1(sj))];
1.1 noro 3548: mj = (Obj)ARG2(sj);
3549: if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
3550: else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
3551: rm = ndv_mul_nm(mod,mul,red);
3552: if ( !r ) r = rm;
3553: else {
3554: for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
3555: if ( d ) {
3556: NEXT(tail) = m; tail = m; LEN(d)++;
3557: } else {
3558: MKND(nd_nvar,m,1,d); tail = BDY(d);
3559: }
3560: }
3561: if ( !m ) return 0; /* failure */
3562: else {
3563: BDY(r) = m;
3564: if ( mod > 0 || mod == -1 ) {
3565: c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
3566: DMAR(c1,c2,0,mod,c);
3567: nd_mul_c(mod,rm,c);
3568: } else {
3569: Z t,u;
3570:
3571: chsgnlf(HCZ(r),&t);
3572: divlf(t,HCZ(rm),&u);
3573: nd_mul_c_lf(rm,u);
3574: }
3575: r = nd_add(mod,r,rm);
3576: }
3577: }
3578: }
3579: }
3580: if ( tail ) NEXT(tail) = 0;
3581: d = nd_add(mod,d,r);
3582: nd_mul_c(mod,d,invm(HCM(d),mod));
3583: return ndtondv(mod,d);
3584: }
3585:
3586: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
3587: {
3588: VL tv,fv,vv,vc,av;
3589: NODE fd,fd0,r,r0,t,x,s,xx,alist;
3590: int e,max,nvar,i;
3591: NDV b;
3592: int ishomo,nalg;
3593: Alg alpha,dp;
3594: P p,zp;
3595: Q dmy;
3596: LIST f1,f2;
3597: Obj obj;
3598: NumberField nf;
3599: struct order_spec *ord1;
3600: NODE permtrace,intred,ind,perm,trace,ti;
3601: int len,n,j;
3602: NDV *db,*pb;
3603:
3604: parse_nd_option(current_option);
3605: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3606: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3607: switch ( ord->id ) {
3608: case 1:
3609: if ( ord->nv != nvar )
3610: error("nd_check : invalid order specification");
3611: break;
3612: default:
3613: break;
3614: }
3615: nd_init_ord(ord);
1.6 noro 3616: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 3617: nd_setup_parameters(nvar,0);
3618:
3619: len = length(BDY(f));
3620: db = (NDV *)MALLOC(len*sizeof(NDV *));
3621: for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
3622: ptozp((P)BDY(t),1,&dmy,&zp);
3623: b = ptondv(CO,vv,zp);
3624: ndv_mod(m,b);
3625: ndv_mul_c(m,b,invm(HCM(b),m));
3626: db[i] = b;
3627: }
3628:
3629: permtrace = BDY((LIST)ARG2(BDY(tlist)));
3630: intred = BDY((LIST)ARG3(BDY(tlist)));
3631: ind = BDY((LIST)ARG4(BDY(tlist)));
3632: perm = BDY((LIST)ARG0(permtrace));
3633: trace = NEXT(permtrace);
3634:
3635: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 3636: j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1 noro 3637: if ( j > i ) i = j;
3638: }
3639: n = i+1;
3640: pb = (NDV *)MALLOC(n*sizeof(NDV *));
3641: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
3642: ti = BDY((LIST)BDY(t));
1.6 noro 3643: pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1 noro 3644: }
3645: for ( t = trace; t; t = NEXT(t) ) {
3646: ti = BDY((LIST)BDY(t));
1.6 noro 3647: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3648: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3649: if ( DP_Print ) {
3650: fprintf(asir_out,"."); fflush(asir_out);
3651: }
3652: }
3653: for ( t = intred; t; t = NEXT(t) ) {
3654: ti = BDY((LIST)BDY(t));
1.6 noro 3655: pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
3656: if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1 noro 3657: if ( DP_Print ) {
3658: fprintf(asir_out,"*"); fflush(asir_out);
3659: }
3660: }
3661: for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
3662: NEXTNODE(r0,r);
1.6 noro 3663: b = pb[ZTOS((Q)BDY(t))];
1.1 noro 3664: ndv_mul_c(m,b,invm(HCM(b),m));
3665: #if 0
1.6 noro 3666: BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3667: #else
1.6 noro 3668: BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1 noro 3669: #endif
3670: }
3671: if ( r0 ) NEXT(r) = 0;
3672: MKLIST(*rp,r0);
3673: if ( DP_Print ) fprintf(asir_out,"\n");
3674: }
3675:
1.16 noro 3676: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1 noro 3677: {
3678: VL tv,fv,vv,vc,av;
3679: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
3680: int m,nocheck,nvar,mindex,e,max;
3681: NDV c;
3682: NMV a;
3683: P p,zp;
3684: Q dmy;
3685: EPOS oepos;
3686: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
3687: Alg alpha,dp;
3688: P poly;
3689: LIST f1,f2,zpl;
3690: Obj obj;
3691: NumberField nf;
3692: struct order_spec *ord1;
3693: struct oEGT eg_check,eg0,eg1;
3694: NODE tr,tl1,tl2,tl3,tl4;
3695: LIST l1,l2,l3,l4,l5;
3696: int *perm;
3697: int j,ret;
3698: Z jq,bpe;
1.15 noro 3699: VECT hvect;
1.1 noro 3700:
3701: nd_module = 0;
3702: nd_lf = 0;
3703: parse_nd_option(current_option);
3704: if ( nd_lf ) {
3705: if ( f4 )
3706: nd_f4_lf_trace(f,v,trace,homo,ord,rp);
3707: else
3708: error("nd_gr_trace is not implemented yet over a large finite field");
3709: return;
3710: }
3711: if ( DP_Multiple )
3712: nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
3713:
3714: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
3715: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
3716: switch ( ord->id ) {
3717: case 1:
3718: if ( ord->nv != nvar )
3719: error("nd_gr_trace : invalid order specification");
3720: break;
3721: default:
3722: break;
3723: }
3724:
3725: get_algtree((Obj)f,&av);
3726: for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
3727: nd_ntrans = nvar;
3728: nd_nalg = nalg;
3729: /* #i -> t#i */
3730: if ( nalg ) {
3731: preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
3732: ord = ord1;
3733: f = f1;
3734: }
3735: nvar += nalg;
3736:
3737: nocheck = 0;
3738: mindex = 0;
3739:
3740: if ( Demand ) nd_demand = 1;
3741: else nd_demand = 0;
3742:
3743: /* setup modulus */
3744: if ( trace < 0 ) {
3745: trace = -trace;
3746: nocheck = 1;
3747: }
3748: m = trace > 1 ? trace : get_lprime(mindex);
3749: nd_init_ord(ord);
3750: mrank = 0;
3751: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
3752: for ( tv = vv; tv; tv = NEXT(tv) ) {
3753: if ( nd_module ) {
1.16 noro 3754: if ( OID(BDY(t)) == O_DPM ) {
3755: e = dpm_getdeg((DPM)BDY(t),&trank);
3756: max = MAX(e,max);
3757: mrank = MAX(mrank,trank);
3758: } else {
1.1 noro 3759: s = BDY((LIST)BDY(t));
3760: trank = length(s);
3761: mrank = MAX(mrank,trank);
3762: for ( ; s; s = NEXT(s) ) {
3763: e = getdeg(tv->v,(P)BDY(s));
3764: max = MAX(e,max);
3765: }
1.16 noro 3766: }
1.1 noro 3767: } else {
3768: e = getdeg(tv->v,(P)BDY(t));
3769: max = MAX(e,max);
3770: }
3771: }
3772: nd_setup_parameters(nvar,max);
3773: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
3774: ishomo = 1;
3775: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
3776: if ( nd_module ) {
1.16 noro 3777: if ( OID(BDY(t)) == O_DPM ) {
3778: Z cont;
3779: DPM zdpm;
3780:
1.17 noro 3781: if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16 noro 3782: else zdpm = (DPM)BDY(t);
3783: c = (pointer)dpmtondv(m,zdpm);
3784: } else {
3785: if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
3786: else zpl = (LIST)BDY(t);
1.1 noro 3787: c = (pointer)pltondv(CO,vv,zpl);
1.16 noro 3788: }
1.1 noro 3789: } else {
1.16 noro 3790: if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
3791: else zp = (P)BDY(t);
3792: c = (pointer)ptondv(CO,vv,zp);
1.1 noro 3793: }
3794: if ( ishomo )
3795: ishomo = ishomo && ndv_ishomo(c);
3796: if ( c ) {
3797: NEXTNODE(in0,in); BDY(in) = (pointer)c;
3798: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
3799: }
3800: }
3801: if ( in0 ) NEXT(in) = 0;
3802: if ( fd0 ) NEXT(fd) = 0;
3803: if ( !ishomo && homo ) {
3804: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
3805: c = (NDV)BDY(t); len = LEN(c);
3806: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
3807: wmax = MAX(TD(DL(a)),wmax);
3808: }
3809: homogenize_order(ord,nvar,&ord1);
3810: nd_init_ord(ord1);
3811: nd_setup_parameters(nvar+1,wmax);
3812: for ( t = fd0; t; t = NEXT(t) )
3813: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
3814: }
3815: if ( MaxDeg > 0 ) nocheck = 1;
3816: while ( 1 ) {
3817: tl1 = tl2 = tl3 = tl4 = 0;
3818: if ( Demand )
3819: nd_demand = 1;
3820: ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
3821: if ( nd_gentrace ) {
3822: MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
3823: }
3824: if ( ret )
3825: cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
3826: if ( !ret || !cand ) {
3827: /* failure */
3828: if ( trace > 1 ) { *rp = 0; return; }
3829: else m = get_lprime(++mindex);
3830: continue;
3831: }
1.15 noro 3832: if ( nd_gentrace ) {
3833: MKVECT(hvect,nd_psn);
3834: for ( i = 0; i < nd_psn; i++ )
3835: ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
3836: }
1.1 noro 3837: if ( !ishomo && homo ) {
3838: /* dehomogenization */
3839: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
3840: nd_init_ord(ord);
3841: nd_setup_parameters(nvar,0);
3842: }
3843: nd_demand = 0;
3844: cand = ndv_reducebase(cand,perm);
3845: if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
3846: cand = ndv_reduceall(0,cand);
3847: cbpe = nd_bpe;
3848: if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
3849: get_eg(&eg0);
3850: if ( nocheck )
3851: break;
3852: if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
3853: if ( nd_gentrace ) {
3854: tl3 = nd_alltracelist; nd_alltracelist = 0;
3855: } else tl3 = 0;
3856: /* gbcheck : cand is a GB of Id(cand) ? */
3857: if ( nd_vc || nd_gentrace || nd_gensyz )
3858: ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
3859: else
3860: ret = nd_f4(0,1,0)!=0;
3861: if ( nd_gentrace && nd_gensyz ) {
3862: tl4 = nd_alltracelist; nd_alltracelist = 0;
3863: } else tl4 = 0;
3864: }
3865: if ( ret ) break;
3866: else if ( trace > 1 ) {
3867: /* failure */
3868: *rp = 0; return;
3869: } else {
3870: /* try the next modulus */
3871: m = get_lprime(++mindex);
3872: /* reset the parameters */
3873: if ( !ishomo && homo ) {
3874: nd_init_ord(ord1);
3875: nd_setup_parameters(nvar+1,wmax);
3876: } else {
3877: nd_init_ord(ord);
3878: nd_setup_parameters(nvar,max);
3879: }
3880: }
3881: }
3882: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
3883: if ( DP_Print )
1.6 noro 3884: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 3885: /* dp->p */
3886: nd_bpe = cbpe;
3887: nd_setup_parameters(nd_nvar,0);
3888: for ( r = cand; r; r = NEXT(r) ) {
1.16 noro 3889: if ( nd_module ) {
1.17 noro 3890: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16 noro 3891: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17 noro 3892: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
3893: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1 noro 3894: }
3895: if ( nd_nalg )
3896: cand = postprocess_algcoef(av,alist,cand);
3897: MKLIST(*rp,cand);
3898: if ( nd_gentrace ) {
3899: tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
3900: tl3 = reverse_node(tl3);
3901: /* tl2 = [[i,[[*,j,*,*],...]],...] */
3902: for ( t = tl2; t; t = NEXT(t) ) {
3903: /* s = [i,[*,j,*,*],...] */
3904: s = BDY((LIST)BDY(t));
1.6 noro 3905: j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1 noro 3906: for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6 noro 3907: j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1 noro 3908: ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
3909: }
3910: }
3911: for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6 noro 3912: STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1 noro 3913: }
3914: MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
3915: MKLIST(l5,tl4);
1.6 noro 3916: STOZ(nd_bpe,bpe);
1.15 noro 3917: tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1 noro 3918: }
3919: }
3920:
3921: /* XXX : module element is not considered */
3922:
3923: void dltondl(int n,DL dl,UINT *r)
3924: {
3925: UINT *d;
3926: int i,j,l,s,ord_l;
3927: struct order_pair *op;
3928:
3929: d = (unsigned int *)dl->d;
3930: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
3931: if ( nd_blockmask ) {
3932: l = nd_blockmask->n;
3933: op = nd_blockmask->order_pair;
3934: for ( j = 0, s = 0; j < l; j++ ) {
3935: ord_l = op[j].length;
3936: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
3937: }
3938: TD(r) = ndl_weight(r);
3939: ndl_weight_mask(r);
3940: } else {
3941: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
3942: TD(r) = ndl_weight(r);
3943: }
3944: }
3945:
3946: DL ndltodl(int n,UINT *ndl)
3947: {
3948: DL dl;
3949: int *d;
3950: int i,j,l,s,ord_l;
3951: struct order_pair *op;
3952:
3953: NEWDL(dl,n);
3954: dl->td = TD(ndl);
3955: d = dl->d;
3956: if ( nd_blockmask ) {
3957: l = nd_blockmask->n;
3958: op = nd_blockmask->order_pair;
3959: for ( j = 0, s = 0; j < l; j++ ) {
3960: ord_l = op[j].length;
3961: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
3962: }
3963: } else {
3964: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
3965: }
3966: return dl;
3967: }
3968:
3969: void nmtodp(int mod,NM m,DP *r)
3970: {
3971: DP dp;
3972: MP mr;
3973:
3974: NEWMP(mr);
3975: mr->dl = ndltodl(nd_nvar,DL(m));
3976: mr->c = (Obj)ndctop(mod,m->c);
3977: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3978: *r = dp;
3979: }
3980:
1.15 noro 3981: void ndltodp(UINT *d,DP *r)
3982: {
3983: DP dp;
3984: MP mr;
3985:
3986: NEWMP(mr);
3987: mr->dl = ndltodl(nd_nvar,d);
3988: mr->c = (Obj)ONE;
3989: NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
3990: *r = dp;
3991: }
3992:
1.1 noro 3993: void ndl_print(UINT *dl)
3994: {
3995: int n;
3996: int i,j,l,ord_l,s,s0;
3997: struct order_pair *op;
3998:
3999: n = nd_nvar;
4000: printf("<<");
4001: if ( nd_blockmask ) {
4002: l = nd_blockmask->n;
4003: op = nd_blockmask->order_pair;
4004: for ( j = 0, s = s0 = 0; j < l; j++ ) {
4005: ord_l = op[j].length;
4006: for ( i = 0; i < ord_l; i++, s++ )
4007: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
4008: }
4009: } else {
4010: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
4011: }
4012: printf(">>");
4013: if ( nd_module && MPOS(dl) )
4014: printf("*e%d",MPOS(dl));
4015: }
4016:
4017: void nd_print(ND p)
4018: {
4019: NM m;
4020:
4021: if ( !p )
4022: printf("0\n");
4023: else {
4024: for ( m = BDY(p); m; m = NEXT(m) ) {
4025: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
4026: else printf("+%d*",CM(m));
4027: ndl_print(DL(m));
4028: }
4029: printf("\n");
4030: }
4031: }
4032:
4033: void nd_print_q(ND p)
4034: {
4035: NM m;
4036:
4037: if ( !p )
4038: printf("0\n");
4039: else {
4040: for ( m = BDY(p); m; m = NEXT(m) ) {
4041: printf("+");
1.6 noro 4042: printexpr(CO,(Obj)CZ(m));
1.1 noro 4043: printf("*");
4044: ndl_print(DL(m));
4045: }
4046: printf("\n");
4047: }
4048: }
4049:
4050: void ndp_print(ND_pairs d)
4051: {
4052: ND_pairs t;
4053:
4054: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
4055: printf("\n");
4056: }
4057:
4058: void nd_removecont(int mod,ND p)
4059: {
4060: int i,n;
4061: Z *w;
4062: NM m;
4063: struct oVECT v;
4064:
4065: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
4066: else if ( mod == -2 ) {
4067: Z inv;
4068: divlf(ONE,HCZ(p),&inv);
4069: nd_mul_c_lf(p,inv);
4070: } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
4071: else {
4072: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
4073: w = (Z *)MALLOC(n*sizeof(Q));
4074: v.len = n;
4075: v.body = (pointer *)w;
1.6 noro 4076: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4077: removecont_array((P *)w,n,1);
1.6 noro 4078: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4079: }
4080: }
4081:
4082: void nd_removecont2(ND p1,ND p2)
4083: {
4084: int i,n1,n2,n;
4085: Z *w;
4086: NM m;
4087: struct oVECT v;
4088:
4089: n1 = nd_length(p1);
4090: n2 = nd_length(p2);
4091: n = n1+n2;
4092: w = (Z *)MALLOC(n*sizeof(Q));
4093: v.len = n;
4094: v.body = (pointer *)w;
4095: i = 0;
4096: if ( p1 )
1.6 noro 4097: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4098: if ( p2 )
1.6 noro 4099: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1 noro 4100: removecont_array((P *)w,n,1);
4101: i = 0;
4102: if ( p1 )
1.6 noro 4103: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4104: if ( p2 )
1.6 noro 4105: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1 noro 4106: }
4107:
4108: void ndv_removecont(int mod,NDV p)
4109: {
4110: int i,len,all_p;
4111: Z *c;
4112: P *w;
4113: Z dvr,t;
4114: P g,cont,tp;
4115: NMV m;
4116:
4117: if ( mod == -1 )
4118: ndv_mul_c(mod,p,_invsf(HCM(p)));
4119: else if ( mod == -2 ) {
4120: Z inv;
4121: divlf(ONE,HCZ(p),&inv);
4122: ndv_mul_c_lf(p,inv);
4123: } else if ( mod )
4124: ndv_mul_c(mod,p,invm(HCM(p),mod));
4125: else {
4126: len = p->len;
4127: w = (P *)MALLOC(len*sizeof(P));
4128: c = (Z *)MALLOC(len*sizeof(Q));
4129: for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
4130: ptozp(CP(m),1,(Q *)&c[i],&w[i]);
4131: all_p = all_p && !NUM(w[i]);
4132: }
4133: if ( all_p ) {
4134: qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
4135: mulp(nd_vc,(P)dvr,g,&cont);
4136: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4137: divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
4138: }
4139: } else {
4140: sortbynm((Q *)c,len);
4141: qltozl((Q *)c,len,&dvr);
4142: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
4143: divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
4144: }
4145: }
4146: }
4147: }
4148:
4149: /* koko */
4150:
4151: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
4152: {
4153: int len,i,max;
4154: NMV m,mr0,mr,t;
4155:
4156: len = p->len;
1.14 noro 4157: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1 noro 4158: max = MAX(max,TD(DL(m)));
4159: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
4160: m = (NMV)((char *)mr0+(len-1)*oadv);
4161: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
4162: t = (NMV)MALLOC(nmv_adv);
4163: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
4164: ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6 noro 4165: CZ(mr) = CZ(m);
1.1 noro 4166: ndl_copy(DL(t),DL(mr));
4167: }
4168: NV(p)++;
4169: BDY(p) = mr0;
4170: }
4171:
4172: void ndv_dehomogenize(NDV p,struct order_spec *ord)
4173: {
4174: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
4175: int pos;
4176: Q *w;
4177: Q dvr,t;
4178: NMV m,r;
4179:
4180: len = p->len;
4181: newnvar = nd_nvar-1;
4182: newexporigin = nd_get_exporigin(ord);
4183: if ( nd_module ) newmpos = newexporigin-1;
4184: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
4185: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
4186: ndl_dehomogenize(DL(m));
4187: if ( newwpd != nd_wpd ) {
4188: newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
4189: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6 noro 4190: CZ(r) = CZ(m);
1.1 noro 4191: if ( nd_module ) pos = MPOS(DL(m));
4192: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
4193: adj = nd_exporigin-newexporigin;
4194: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
4195: if ( nd_module ) {
4196: DL(r)[newmpos] = pos;
4197: }
4198: }
4199: }
4200: NV(p)--;
4201: }
4202:
4203: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
4204: {
4205: int i;
4206: P *tpl,*tpl1;
4207: NODE l;
4208: P h,gcd,t;
4209:
4210: tpl = (P *)MALLOC(m*sizeof(P));
4211: tpl1 = (P *)MALLOC(m*sizeof(P));
4212: bcopy(pl,tpl,m*sizeof(P));
4213: gcd = (P)ONE;
4214: for ( l = nd_hcf; l; l = NEXT(l) ) {
4215: h = (P)BDY(l);
4216: while ( 1 ) {
4217: for ( i = 0; i < m; i++ )
4218: if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
4219: break;
4220: if ( i == m ) {
4221: bcopy(tpl1,tpl,m*sizeof(P));
4222: mulp(vl,gcd,h,&t); gcd = t;
4223: } else
4224: break;
4225: }
4226: }
4227: if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
4228: if ( full ) {
4229: heu_nezgcdnpz(vl,tpl,m,&t);
4230: mulp(vl,gcd,t,pr);
4231: } else
4232: *pr = gcd;
4233: }
4234:
4235: void removecont_array(P *p,int n,int full)
4236: {
4237: int all_p,all_q,i;
4238: Z *c;
4239: P *w;
4240: P t,s;
4241:
4242: for ( all_q = 1, i = 0; i < n; i++ )
4243: all_q = all_q && NUM(p[i]);
4244: if ( all_q ) {
4245: removecont_array_q((Z *)p,n);
4246: } else {
4247: c = (Z *)MALLOC(n*sizeof(Z));
4248: w = (P *)MALLOC(n*sizeof(P));
4249: for ( i = 0; i < n; i++ ) {
4250: ptozp(p[i],1,(Q *)&c[i],&w[i]);
4251: }
4252: removecont_array_q(c,n);
4253: nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
4254: for ( i = 0; i < n; i++ ) {
4255: divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
4256: }
4257: }
4258: }
4259:
4260: /* c is an int array */
4261:
4262: void removecont_array_q(Z *c,int n)
4263: {
4264: struct oVECT v;
4265: Z d0,d1,a,u,u1,gcd;
4266: int i,j;
4267: Z *q,*r;
4268:
4269: q = (Z *)MALLOC(n*sizeof(Z));
4270: r = (Z *)MALLOC(n*sizeof(Z));
4271: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
4272: gcdvz_estimate(&v,&d0);
4273: for ( i = 0; i < n; i++ ) {
4274: divqrz(c[i],d0,&q[i],&r[i]);
4275: }
4276: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
4277: if ( i < n ) {
4278: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
4279: gcdvz(&v,&d1);
4280: gcdz(d0,d1,&gcd);
1.6 noro 4281: /* exact division */
4282: divsz(d0,gcd,&a);
1.1 noro 4283: for ( i = 0; i < n; i++ ) {
4284: mulz(a,q[i],&u);
4285: if ( r[i] ) {
1.6 noro 4286: /* exact division */
4287: divsz(r[i],gcd,&u1);
1.1 noro 4288: addz(u,u1,&q[i]);
4289: } else
4290: q[i] = u;
4291: }
4292: }
4293: for ( i = 0; i < n; i++ ) c[i] = q[i];
4294: }
4295:
1.4 noro 4296: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
4297:
4298: void mpz_removecont_array(mpz_t *c,int n)
4299: {
4300: mpz_t d0,a,u,u1,gcd;
4301: int i,j;
1.13 noro 4302: static mpz_t *q,*r;
4303: static int c_len = 0;
1.4 noro 4304:
4305: for ( i = 0; i < n; i++ )
4306: if ( mpz_sgn(c[i]) ) break;
4307: if ( i == n ) return;
4308: gcdv_mpz_estimate(d0,c,n);
1.13 noro 4309: if ( n > c_len ) {
4310: q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4311: r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
4312: c_len = n;
4313: }
1.4 noro 4314: for ( i = 0; i < n; i++ ) {
4315: mpz_init(q[i]); mpz_init(r[i]);
4316: mpz_fdiv_qr(q[i],r[i],c[i],d0);
4317: }
4318: for ( i = 0; i < n; i++ )
4319: if ( mpz_sgn(r[i]) ) break;
4320: mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
4321: if ( i < n ) {
4322: mpz_gcd(gcd,d0,r[i]);
4323: for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
4324: mpz_div(a,d0,gcd);
4325: for ( i = 0; i < n; i++ ) {
4326: mpz_mul(u,a,q[i]);
4327: if ( mpz_sgn(r[i]) ) {
4328: mpz_div(u1,r[i],gcd);
4329: mpz_add(q[i],u,u1);
4330: } else
4331: mpz_set(q[i],u);
4332: }
4333: }
4334: for ( i = 0; i < n; i++ )
4335: mpz_set(c[i],q[i]);
4336: }
4337:
1.1 noro 4338: void nd_mul_c(int mod,ND p,int mul)
4339: {
4340: NM m;
4341: int c,c1;
4342:
4343: if ( !p ) return;
4344: if ( mul == 1 ) return;
4345: if ( mod == -1 )
4346: for ( m = BDY(p); m; m = NEXT(m) )
4347: CM(m) = _mulsf(CM(m),mul);
4348: else
4349: for ( m = BDY(p); m; m = NEXT(m) ) {
4350: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4351: }
4352: }
4353:
4354: void nd_mul_c_lf(ND p,Z mul)
4355: {
4356: NM m;
4357: Z c;
4358:
4359: if ( !p ) return;
4360: if ( UNIZ(mul) ) return;
4361: for ( m = BDY(p); m; m = NEXT(m) ) {
4362: mullf(CZ(m),mul,&c); CZ(m) = c;
4363: }
4364: }
4365:
4366: void nd_mul_c_q(ND p,P mul)
4367: {
4368: NM m;
4369: P c;
4370:
4371: if ( !p ) return;
4372: if ( UNIQ(mul) ) return;
4373: for ( m = BDY(p); m; m = NEXT(m) ) {
4374: mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
4375: }
4376: }
4377:
4378: void nd_mul_c_p(VL vl,ND p,P mul)
4379: {
4380: NM m;
4381: P c;
4382:
4383: if ( !p ) return;
4384: for ( m = BDY(p); m; m = NEXT(m) ) {
4385: mulp(vl,CP(m),mul,&c); CP(m) = c;
4386: }
4387: }
4388:
4389: void nd_free(ND p)
4390: {
4391: NM t,s;
4392:
4393: if ( !p ) return;
4394: t = BDY(p);
4395: while ( t ) {
4396: s = NEXT(t);
4397: FREENM(t);
4398: t = s;
4399: }
4400: FREEND(p);
4401: }
4402:
4403: void ndv_free(NDV p)
4404: {
4405: GCFREE(BDY(p));
4406: }
4407:
4408: void nd_append_red(UINT *d,int i)
4409: {
4410: RHist m,m0;
4411: int h;
4412:
4413: NEWRHist(m);
4414: h = ndl_hash_value(d);
4415: m->index = i;
4416: ndl_copy(d,DL(m));
4417: NEXT(m) = nd_red[h];
4418: nd_red[h] = m;
4419: }
4420:
4421: UINT *ndv_compute_bound(NDV p)
4422: {
4423: UINT *d1,*d2,*t;
4424: UINT u;
4425: int i,j,k,l,len,ind;
4426: NMV m;
4427:
4428: if ( !p )
4429: return 0;
4430: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4431: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4432: len = LEN(p);
4433: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
4434: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
4435: ndl_max(DL(m),d1,d2);
4436: t = d1; d1 = d2; d2 = t;
4437: }
4438: l = nd_nvar+31;
4439: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4440: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4441: u = d1[i];
4442: k = (nd_epw-1)*nd_bpe;
4443: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4444: t[ind] = (u>>k)&nd_mask0;
4445: }
4446: for ( ; ind < l; ind++ ) t[ind] = 0;
4447: return t;
4448: }
4449:
4450: UINT *nd_compute_bound(ND p)
4451: {
4452: UINT *d1,*d2,*t;
4453: UINT u;
4454: int i,j,k,l,len,ind;
4455: NM m;
4456:
4457: if ( !p )
4458: return 0;
4459: d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4460: d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
4461: len = LEN(p);
4462: m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
4463: for ( m = NEXT(m); m; m = NEXT(m) ) {
4464: ndl_lcm(DL(m),d1,d2);
4465: t = d1; d1 = d2; d2 = t;
4466: }
4467: l = nd_nvar+31;
4468: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
4469: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
4470: u = d1[i];
4471: k = (nd_epw-1)*nd_bpe;
4472: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
4473: t[ind] = (u>>k)&nd_mask0;
4474: }
4475: for ( ; ind < l; ind++ ) t[ind] = 0;
4476: return t;
4477: }
4478:
4479: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
4480: /* of a term. In this case we need additional 1 word. */
4481:
4482: int nd_get_exporigin(struct order_spec *ord)
4483: {
4484: switch ( ord->id ) {
4485: case 0: case 2: case 256: case 258:
4486: return 1+nd_module;
4487: case 1: case 257:
4488: /* block order */
4489: /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
4490: /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
4491: return ord->ord.block.length+1+nd_module;
4492: case 3: case 259:
4493: #if 0
4494: error("nd_get_exporigin : composite order is not supported yet.");
4495: #else
4496: return 1+nd_module;
4497: #endif
4498: default:
4499: error("nd_get_exporigin : ivalid argument.");
4500: return 0;
4501: }
4502: }
4503:
4504: void nd_setup_parameters(int nvar,int max) {
4505: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
4506: struct order_pair *op;
4507:
4508: nd_nvar = nvar;
4509: if ( max ) {
4510: /* XXX */
4511: if ( do_weyl ) nd_bpe = 32;
4512: else if ( max < 2 ) nd_bpe = 1;
4513: else if ( max < 4 ) nd_bpe = 2;
4514: else if ( max < 8 ) nd_bpe = 3;
4515: else if ( max < 16 ) nd_bpe = 4;
4516: else if ( max < 32 ) nd_bpe = 5;
4517: else if ( max < 64 ) nd_bpe = 6;
4518: else if ( max < 256 ) nd_bpe = 8;
4519: else if ( max < 1024 ) nd_bpe = 10;
4520: else if ( max < 65536 ) nd_bpe = 16;
4521: else nd_bpe = 32;
4522: }
4523: if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
4524: UINT t;
4525: int st;
4526: int *v;
4527: /* t = max(weights) */
4528: t = 0;
4529: if ( current_dl_weight_vector )
4530: for ( i = 0, t = 0; i < nd_nvar; i++ ) {
4531: if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
4532: if ( t < st ) t = st;
4533: }
4534: if ( nd_matrix )
4535: for ( i = 0; i < nd_matrix_len; i++ )
4536: for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
4537: if ( (st=v[j]) < 0 ) st = -st;
4538: if ( t < st ) t = st;
4539: }
4540: /* i = bitsize of t */
4541: for ( i = 0; t; t >>=1, i++ );
4542: /* i += bitsize of nd_nvar */
4543: for ( t = nd_nvar; t; t >>=1, i++);
4544: /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
4545: if ( (nd_bpe+i) >= 31 )
4546: error("nd_setup_parameters : too large weight");
4547: }
4548: nd_epw = (sizeof(UINT)*8)/nd_bpe;
4549: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
4550: nd_exporigin = nd_get_exporigin(nd_ord);
4551: wpd = nd_exporigin+elen;
4552: if ( nd_module )
4553: nd_mpos = nd_exporigin-1;
4554: else
4555: nd_mpos = -1;
4556: if ( wpd != nd_wpd ) {
4557: nd_free_private_storage();
4558: nd_wpd = wpd;
4559: }
4560: if ( nd_bpe < 32 ) {
4561: nd_mask0 = (1<<nd_bpe)-1;
4562: } else {
4563: nd_mask0 = 0xffffffff;
4564: }
4565: bzero(nd_mask,sizeof(nd_mask));
4566: nd_mask1 = 0;
4567: for ( i = 0; i < nd_epw; i++ ) {
4568: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
4569: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
4570: }
4571: nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
4572: nd_epos = nd_create_epos(nd_ord);
4573: nd_blockmask = nd_create_blockmask(nd_ord);
4574: nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
4575: }
4576:
4577: ND_pairs nd_reconstruct(int trace,ND_pairs d)
4578: {
4579: int i,obpe,oadv,h;
4580: static NM prev_nm_free_list;
4581: static ND_pairs prev_ndp_free_list;
4582: RHist mr0,mr;
4583: RHist r;
4584: RHist *old_red;
4585: ND_pairs s0,s,t;
4586: EPOS oepos;
4587:
4588: obpe = nd_bpe;
4589: oadv = nmv_adv;
4590: oepos = nd_epos;
4591: if ( obpe < 2 ) nd_bpe = 2;
4592: else if ( obpe < 3 ) nd_bpe = 3;
4593: else if ( obpe < 4 ) nd_bpe = 4;
4594: else if ( obpe < 5 ) nd_bpe = 5;
4595: else if ( obpe < 6 ) nd_bpe = 6;
4596: else if ( obpe < 8 ) nd_bpe = 8;
4597: else if ( obpe < 10 ) nd_bpe = 10;
4598: else if ( obpe < 16 ) nd_bpe = 16;
4599: else if ( obpe < 32 ) nd_bpe = 32;
4600: else error("nd_reconstruct : exponent too large");
4601:
4602: nd_setup_parameters(nd_nvar,0);
4603: prev_nm_free_list = _nm_free_list;
4604: prev_ndp_free_list = _ndp_free_list;
4605: _nm_free_list = 0;
4606: _ndp_free_list = 0;
4607: for ( i = nd_psn-1; i >= 0; i-- ) {
4608: ndv_realloc(nd_ps[i],obpe,oadv,oepos);
4609: ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
4610: }
4611: if ( trace )
4612: for ( i = nd_psn-1; i >= 0; i-- ) {
4613: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
4614: ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
4615: }
4616: s0 = 0;
4617: for ( t = d; t; t = NEXT(t) ) {
4618: NEXTND_pairs(s0,s);
4619: s->i1 = t->i1;
4620: s->i2 = t->i2;
4621: SG(s) = SG(t);
4622: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
4623: }
4624:
4625: old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
4626: for ( i = 0; i < REDTAB_LEN; i++ ) {
4627: old_red[i] = nd_red[i];
4628: nd_red[i] = 0;
4629: }
4630: for ( i = 0; i < REDTAB_LEN; i++ )
4631: for ( r = old_red[i]; r; r = NEXT(r) ) {
4632: NEWRHist(mr);
4633: mr->index = r->index;
4634: SG(mr) = SG(r);
4635: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
4636: h = ndl_hash_value(DL(mr));
4637: NEXT(mr) = nd_red[h];
4638: nd_red[h] = mr;
4639: }
4640: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
4641: old_red = 0;
4642: for ( i = 0; i < nd_psn; i++ ) {
4643: NEWRHist(r); SG(r) = SG(nd_psh[i]);
4644: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
4645: nd_psh[i] = r;
4646: }
4647: if ( s0 ) NEXT(s) = 0;
4648: prev_nm_free_list = 0;
4649: prev_ndp_free_list = 0;
4650: #if 0
4651: GC_gcollect();
4652: #endif
4653: return s0;
4654: }
4655:
4656: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
4657: {
4658: int n,i,ei,oepw,omask0,j,s,ord_l,l;
4659: struct order_pair *op;
4660:
4661: n = nd_nvar;
4662: oepw = (sizeof(UINT)*8)/obpe;
4663: omask0 = (1<<obpe)-1;
4664: TD(r) = TD(d);
4665: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
4666: if ( nd_blockmask ) {
4667: l = nd_blockmask->n;
4668: op = nd_blockmask->order_pair;
4669: for ( i = 1; i < nd_exporigin; i++ )
4670: r[i] = d[i];
4671: for ( j = 0, s = 0; j < l; j++ ) {
4672: ord_l = op[j].length;
4673: for ( i = 0; i < ord_l; i++, s++ ) {
4674: ei = GET_EXP_OLD(d,s);
4675: PUT_EXP(r,s,ei);
4676: }
4677: }
4678: } else {
4679: for ( i = 0; i < n; i++ ) {
4680: ei = GET_EXP_OLD(d,i);
4681: PUT_EXP(r,i,ei);
4682: }
4683: }
4684: if ( nd_module ) MPOS(r) = MPOS(d);
4685: }
4686:
4687: ND nd_copy(ND p)
4688: {
4689: NM m,mr,mr0;
4690: int c,n;
4691: ND r;
4692:
4693: if ( !p )
4694: return 0;
4695: else {
4696: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
4697: NEXTNM(mr0,mr);
4698: CM(mr) = CM(m);
4699: ndl_copy(DL(m),DL(mr));
4700: }
4701: NEXT(mr) = 0;
4702: MKND(NV(p),mr0,LEN(p),r);
4703: SG(r) = SG(p);
4704: return r;
4705: }
4706: }
4707:
4708: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
4709: {
4710: NM m1,m2;
4711: NDV p1,p2;
4712: ND t1,t2;
4713: UINT *lcm;
4714: P gp,tp;
4715: Z g,t;
4716: Z iq;
4717: int td;
4718: LIST hist;
4719: NODE node;
4720: DP d;
4721:
4722: if ( !mod && nd_demand ) {
4723: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
4724: } else {
4725: if ( trace ) {
4726: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
4727: } else {
4728: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
4729: }
4730: }
4731: lcm = LCM(p);
4732: NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
4733: if ( ndl_check_bound2(p->i1,DL(m1)) ) {
4734: FREENM(m1); return 0;
4735: }
4736: NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
4737: if ( ndl_check_bound2(p->i2,DL(m2)) ) {
4738: FREENM(m1); FREENM(m2); return 0;
4739: }
4740:
4741: if ( mod == -1 ) {
4742: CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
4743: } else if ( mod > 0 ) {
4744: CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
4745: } else if ( mod == -2 ) {
4746: CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
4747: } else if ( nd_vc ) {
4748: ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
4749: divsp(nd_vc,HCP(p2),gp,&CP(m1));
4750: divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
4751: } else {
1.6 noro 4752: igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1 noro 4753: }
4754: t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
4755: *rp = nd_add(mod,t1,t2);
4756: if ( nd_gentrace ) {
4757: /* nd_tracelist is initialized */
1.6 noro 4758: STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4759: MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6 noro 4760: STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1 noro 4761: MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
4762: nd_tracelist = node;
4763: }
4764: FREENM(m1); FREENM(m2);
4765: return 1;
4766: }
4767:
4768: void ndv_mul_c(int mod,NDV p,int mul)
4769: {
4770: NMV m;
4771: int c,c1,len,i;
4772:
4773: if ( !p ) return;
4774: len = LEN(p);
4775: if ( mod == -1 )
4776: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
4777: CM(m) = _mulsf(CM(m),mul);
4778: else
4779: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4780: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
4781: }
4782: }
4783:
4784: void ndv_mul_c_lf(NDV p,Z mul)
4785: {
4786: NMV m;
4787: Z c;
4788: int len,i;
4789:
4790: if ( !p ) return;
4791: len = LEN(p);
4792: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4793: mullf(CZ(m),mul,&c); CZ(m) = c;
4794: }
4795: }
4796:
4797: /* for nd_det */
4798: void ndv_mul_c_q(NDV p,Z mul)
4799: {
4800: NMV m;
4801: Z c;
4802: int len,i;
4803:
4804: if ( !p ) return;
4805: len = LEN(p);
4806: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 4807: mulz(CZ(m),mul,&c); CZ(m) = c;
1.1 noro 4808: }
4809: }
4810:
4811: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
4812: int n2,i,j,l,n,tlen;
4813: UINT *d0;
4814: NM *tab,*psum;
4815: ND s,r;
4816: NM t;
4817: NMV m1;
4818:
4819: if ( !p ) return 0;
4820: n = NV(p); n2 = n>>1;
4821: d0 = DL(m0);
4822: l = LEN(p);
4823: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
4824: tab = (NM *)MALLOC(tlen*sizeof(NM));
4825: psum = (NM *)MALLOC(tlen*sizeof(NM));
4826: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
4827: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
4828: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
4829: /* m0(NM) * m1(NMV) => tab(NM) */
4830: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
4831: for ( j = 0; j < tlen; j++ ) {
4832: if ( tab[j] ) {
4833: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
4834: }
4835: }
4836: }
4837: for ( i = tlen-1, r = 0; i >= 0; i-- )
4838: if ( psum[i] ) {
4839: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
4840: MKND(n,psum[i],j,s);
4841: r = nd_add(mod,r,s);
4842: }
4843: if ( r ) SG(r) = SG(p)+TD(d0);
4844: return r;
4845: }
4846:
4847: /* product of monomials */
4848: /* XXX block order is not handled correctly */
4849:
4850: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
4851: {
4852: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
4853: UINT *d0,*d1,*d,*dt,*ctab;
4854: Z *ctab_q;
4855: Z q,q1;
4856: UINT c0,c1,c;
4857: NM *p;
4858: NM m,t;
4859: int mpos;
4860:
4861: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
4862: if ( !m0 || !m1 ) return;
4863: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
4864: if ( nd_module )
4865: if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
4866:
4867: NEWNM(m); d = DL(m);
4868: if ( mod ) {
4869: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
4870: } else if ( nd_vc )
4871: mulp(nd_vc,CP(m0),CP(m1),&CP(m));
4872: else
1.6 noro 4873: mulz(CZ(m0),CZ(m1),&CZ(m));
1.1 noro 4874: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
4875: homo = n&1 ? 1 : 0;
4876: if ( homo ) {
4877: /* offset of h-degree */
4878: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
4879: PUT_EXP(DL(m),n-1,h);
4880: TD(DL(m)) = h;
4881: if ( nd_blockmask ) ndl_weight_mask(DL(m));
4882: }
4883: tab[0] = m;
4884: NEWNM(m); d = DL(m);
4885: for ( i = 0, curlen = 1; i < n2; i++ ) {
4886: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
4887: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
4888: /* xi^a*(Di^k*xi^l)*Di^b */
4889: a += l; b += k;
4890: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
4891: if ( !k || !l ) {
4892: for ( j = 0; j < curlen; j++ )
4893: if ( (t = tab[j]) != 0 ) {
4894: dt = DL(t);
4895: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
4896: if ( nd_blockmask ) ndl_weight_mask(dt);
4897: }
4898: curlen *= k+1;
4899: continue;
4900: }
4901: min = MIN(k,l);
4902: if ( mod ) {
4903: ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
4904: mkwcm(k,l,mod,(int *)ctab);
4905: } else {
4906: ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
4907: mkwc(k,l,ctab_q);
4908: }
4909: for ( j = min; j >= 0; j-- ) {
4910: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
4911: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
4912: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
4913: if ( homo ) {
4914: TD(d) = s;
4915: PUT_EXP(d,n-1,s-h);
4916: } else TD(d) = h;
4917: if ( nd_blockmask ) ndl_weight_mask(d);
4918: if ( mod ) c = ctab[j];
4919: else q = ctab_q[j];
4920: p = tab+curlen*j;
4921: if ( j == 0 ) {
4922: for ( u = 0; u < curlen; u++, p++ ) {
4923: if ( tab[u] ) {
4924: ndl_addto(DL(tab[u]),d);
4925: if ( mod ) {
4926: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
4927: } else if ( nd_vc )
4928: mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
4929: else {
1.6 noro 4930: mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1 noro 4931: }
4932: }
4933: }
4934: } else {
4935: for ( u = 0; u < curlen; u++, p++ ) {
4936: if ( tab[u] ) {
4937: NEWNM(t);
4938: ndl_add(DL(tab[u]),d,DL(t));
4939: if ( mod ) {
4940: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
4941: } else if ( nd_vc )
4942: mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
4943: else
1.6 noro 4944: mulz(CZ(tab[u]),q,&CZ(t));
1.1 noro 4945: *p = t;
4946: }
4947: }
4948: }
4949: }
4950: curlen *= k+1;
4951: }
4952: FREENM(m);
4953: if ( nd_module ) {
4954: mpos = MPOS(d1);
4955: for ( i = 0; i < tlen; i++ )
4956: if ( tab[i] ) {
4957: d = DL(tab[i]);
4958: MPOS(d) = mpos;
4959: TD(d) = ndl_weight(d);
4960: }
4961: }
4962: }
4963:
4964: ND ndv_mul_nm_symbolic(NM m0,NDV p)
4965: {
4966: NM mr,mr0;
4967: NMV m;
4968: UINT *d,*dt,*dm;
4969: int c,n,td,i,c1,c2,len;
4970: Q q;
4971: ND r;
4972:
4973: if ( !p ) return 0;
4974: else {
4975: n = NV(p); m = BDY(p);
4976: d = DL(m0);
4977: len = LEN(p);
4978: mr0 = 0;
4979: td = TD(d);
4980: c = CM(m0);
4981: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4982: NEXTNM(mr0,mr);
4983: CM(mr) = 1;
4984: ndl_add(DL(m),d,DL(mr));
4985: }
4986: NEXT(mr) = 0;
4987: MKND(NV(p),mr0,len,r);
4988: SG(r) = SG(p) + TD(d);
4989: return r;
4990: }
4991: }
4992:
4993: ND ndv_mul_nm(int mod,NM m0,NDV p)
4994: {
4995: NM mr,mr0;
4996: NMV m;
4997: UINT *d,*dt,*dm;
4998: int c,n,td,i,c1,c2,len;
4999: P q;
5000: ND r;
5001:
5002: if ( !p ) return 0;
5003: else if ( do_weyl ) {
5004: if ( mod < 0 ) {
5005: error("ndv_mul_nm : not implemented (weyl)");
5006: return 0;
5007: } else
5008: return weyl_ndv_mul_nm(mod,m0,p);
5009: } else {
5010: n = NV(p); m = BDY(p);
5011: d = DL(m0);
5012: len = LEN(p);
5013: mr0 = 0;
5014: td = TD(d);
5015: if ( mod == -1 ) {
5016: c = CM(m0);
5017: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5018: NEXTNM(mr0,mr);
5019: CM(mr) = _mulsf(CM(m),c);
5020: ndl_add(DL(m),d,DL(mr));
5021: }
5022: } else if ( mod == -2 ) {
5023: Z cl;
5024: cl = CZ(m0);
5025: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5026: NEXTNM(mr0,mr);
5027: mullf(CZ(m),cl,&CZ(mr));
5028: ndl_add(DL(m),d,DL(mr));
5029: }
5030: } else if ( mod ) {
5031: c = CM(m0);
5032: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5033: NEXTNM(mr0,mr);
5034: c1 = CM(m);
5035: DMAR(c1,c,0,mod,c2);
5036: CM(mr) = c2;
5037: ndl_add(DL(m),d,DL(mr));
5038: }
5039: } else {
5040: q = CP(m0);
5041: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5042: NEXTNM(mr0,mr);
5043: mulp(nd_vc,CP(m),q,&CP(mr));
5044: ndl_add(DL(m),d,DL(mr));
5045: }
5046: }
5047: NEXT(mr) = 0;
5048: MKND(NV(p),mr0,len,r);
5049: SG(r) = SG(p) + TD(d);
5050: return r;
5051: }
5052: }
5053:
5054: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
5055: {
5056: NM mq0,mq;
5057: NMV tm;
5058: Q q;
5059: int i,nv,sg,c,c1,c2,hindex;
5060: ND p,t,r;
5061:
5062: if ( bucket->m < 0 ) return 0;
5063: else {
5064: nv = NV(d);
5065: mq0 = 0;
5066: tm = (NMV)MALLOC(nmv_adv);
5067: while ( 1 ) {
5068: if ( mod > 0 || mod == -1 )
5069: hindex = head_pbucket(mod,bucket);
5070: else if ( mod == -2 )
5071: hindex = head_pbucket_lf(bucket);
5072: else
5073: hindex = head_pbucket_q(bucket);
5074: if ( hindex < 0 ) break;
5075: p = bucket->body[hindex];
5076: NEXTNM(mq0,mq);
5077: ndl_sub(HDL(p),HDL(d),DL(mq));
5078: ndl_copy(DL(mq),DL(tm));
5079: if ( mod ) {
5080: c1 = invm(HCM(d),mod); c2 = HCM(p);
5081: DMAR(c1,c2,0,mod,c); CM(mq) = c;
5082: CM(tm) = mod-c;
5083: } else {
1.6 noro 5084: divsz(HCZ(p),HCZ(d),&CZ(mq));
5085: chsgnz(CZ(mq),&CZ(tm));
1.1 noro 5086: }
5087: t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
5088: bucket->body[hindex] = nd_remove_head(p);
5089: t = nd_remove_head(t);
5090: add_pbucket(mod,bucket,t);
5091: }
5092: if ( !mq0 )
5093: r = 0;
5094: else {
5095: NEXT(mq) = 0;
5096: for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
5097: MKND(nv,mq0,i,r);
5098: /* XXX */
5099: SG(r) = HTD(r);
5100: }
5101: return r;
5102: }
5103: }
5104:
5105: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5106: {
5107: NMV m,mr,mr0,t;
5108: int len,i,k;
5109:
5110: if ( !p ) return;
5111: m = BDY(p); len = LEN(p);
5112: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
5113: m = (NMV)((char *)mr0+(len-1)*oadv);
5114: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
5115: t = (NMV)MALLOC(nmv_adv);
5116: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6 noro 5117: CZ(t) = CZ(m);
1.1 noro 5118: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
5119: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6 noro 5120: CZ(mr) = CZ(t);
1.1 noro 5121: ndl_copy(DL(t),DL(mr));
5122: }
5123: BDY(p) = mr0;
5124: }
5125:
5126: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
5127: {
5128: NMV m,mr,mr0;
5129: int len,i;
5130: NDV r;
5131:
5132: if ( !p ) return 0;
5133: m = BDY(p); len = LEN(p);
5134: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
5135: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
5136: ndl_zero(DL(mr));
5137: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6 noro 5138: CZ(mr) = CZ(m);
1.1 noro 5139: }
5140: MKNDV(NV(p),mr0,len,r);
5141: SG(r) = SG(p);
5142: return r;
5143: }
5144:
5145: /* duplicate p */
5146:
5147: NDV ndv_dup(int mod,NDV p)
5148: {
5149: NDV d;
5150: NMV t,m,m0;
5151: int i,len;
5152:
5153: if ( !p ) return 0;
5154: len = LEN(p);
5155: m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5156: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5157: ndl_copy(DL(t),DL(m));
1.6 noro 5158: CZ(m) = CZ(t);
1.1 noro 5159: }
5160: MKNDV(NV(p),m0,len,d);
5161: SG(d) = SG(p);
5162: return d;
5163: }
5164:
5165: NDV ndv_symbolic(int mod,NDV p)
5166: {
5167: NDV d;
5168: NMV t,m,m0;
5169: int i,len;
5170:
5171: if ( !p ) return 0;
5172: len = LEN(p);
5173: m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
5174: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
5175: ndl_copy(DL(t),DL(m));
1.6 noro 5176: CZ(m) = ONE;
1.1 noro 5177: }
5178: MKNDV(NV(p),m0,len,d);
5179: SG(d) = SG(p);
5180: return d;
5181: }
5182:
5183: ND nd_dup(ND p)
5184: {
5185: ND d;
5186: NM t,m,m0;
5187:
5188: if ( !p ) return 0;
5189: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
5190: NEXTNM(m0,m);
5191: ndl_copy(DL(t),DL(m));
1.6 noro 5192: CZ(m) = CZ(t);
1.1 noro 5193: }
5194: if ( m0 ) NEXT(m) = 0;
5195: MKND(NV(p),m0,LEN(p),d);
5196: SG(d) = SG(p);
5197: return d;
5198: }
5199:
5200: /* XXX if p->len == 0 then it represents 0 */
5201:
5202: void ndv_mod(int mod,NDV p)
5203: {
5204: NMV t,d;
5205: int r,s,u;
5206: int i,len,dlen;
5207: P cp;
5208: Z c;
5209: Obj gfs;
5210:
5211: if ( !p ) return;
5212: len = LEN(p);
5213: dlen = 0;
5214: if ( mod == -1 )
5215: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5216: simp_ff((Obj)CP(t),&gfs);
5217: if ( gfs ) {
5218: r = FTOIF(CONT((GFS)gfs));
5219: CM(d) = r;
5220: ndl_copy(DL(t),DL(d));
5221: NMV_ADV(d);
5222: dlen++;
5223: }
5224: }
5225: else if ( mod == -2 )
5226: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5227: simp_ff((Obj)CP(t),&gfs);
5228: if ( gfs ) {
5229: lmtolf((LM)gfs,&CZ(d));
5230: ndl_copy(DL(t),DL(d));
5231: NMV_ADV(d);
5232: dlen++;
5233: }
5234: }
5235: else
5236: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
5237: if ( nd_vc ) {
5238: nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
5239: c = (Z)cp;
5240: } else
1.6 noro 5241: c = CZ(t);
1.1 noro 5242: r = remqi((Q)c,mod);
5243: if ( r ) {
5244: CM(d) = r;
5245: ndl_copy(DL(t),DL(d));
5246: NMV_ADV(d);
5247: dlen++;
5248: }
5249: }
5250: LEN(p) = dlen;
5251: }
5252:
5253: NDV ptondv(VL vl,VL dvl,P p)
5254: {
5255: ND nd;
5256:
5257: nd = ptond(vl,dvl,p);
5258: return ndtondv(0,nd);
5259: }
5260:
5261: void pltozpl(LIST l,Q *cont,LIST *pp)
5262: {
1.16 noro 5263: NODE nd,nd1;
5264: int n;
5265: P *pl;
5266: Q *cl;
5267: int i;
5268: P dmy;
5269: Z dvr,inv;
5270: LIST r;
5271:
5272: nd = BDY(l); n = length(nd);
5273: pl = (P *)MALLOC(n*sizeof(P));
5274: cl = (Q *)MALLOC(n*sizeof(Q));
5275: for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
5276: ptozp((P)BDY(nd),1,&cl[i],&dmy);
5277: }
5278: qltozl(cl,n,&dvr);
5279: divz(ONE,dvr,&inv);
5280: nd = BDY(l);
5281: for ( i = 0; i < n; i++, nd = NEXT(nd) )
5282: divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
5283: nd = 0;
5284: for ( i = n-1; i >= 0; i-- ) {
5285: MKNODE(nd1,pl[i],nd); nd = nd1;
5286: }
5287: MKLIST(r,nd);
5288: *pp = r;
1.1 noro 5289: }
5290:
5291: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
5292:
5293: NDV pltondv(VL vl,VL dvl,LIST p)
5294: {
5295: int i;
5296: NODE t;
5297: ND r,ri;
5298: NM m;
5299:
5300: if ( !nd_module ) error("pltond : module order must be set");
5301: r = 0;
5302: for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
5303: ri = ptond(vl,dvl,(P)BDY(t));
5304: if ( ri )
5305: for ( m = BDY(ri); m; m = NEXT(m) ) {
5306: MPOS(DL(m)) = i;
5307: TD(DL(m)) = ndl_weight(DL(m));
5308: if ( nd_blockmask ) ndl_weight_mask(DL(m));
5309: }
5310: r = nd_add(0,r,ri);
5311: }
5312: return ndtondv(0,r);
5313: }
5314:
5315: ND ptond(VL vl,VL dvl,P p)
5316: {
5317: int n,i,j,k,e;
5318: VL tvl;
5319: V v;
5320: DCP dc;
5321: DCP *w;
5322: ND r,s,t,u;
5323: P x;
5324: int c;
5325: UINT *d;
5326: NM m,m0;
5327:
5328: if ( !p )
5329: return 0;
5330: else if ( NUM(p) ) {
5331: NEWNM(m);
5332: ndl_zero(DL(m));
5333: if ( !INT((Q)p) )
5334: error("ptond : input must be integer-coefficient");
1.6 noro 5335: CZ(m) = (Z)p;
1.1 noro 5336: NEXT(m) = 0;
5337: MKND(nd_nvar,m,1,r);
5338: SG(r) = 0;
5339: return r;
5340: } else {
5341: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
5342: w = (DCP *)MALLOC(k*sizeof(DCP));
5343: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
5344: for ( i = 0, tvl = dvl, v = VR(p);
5345: tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
5346: if ( !tvl ) {
5347: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
5348: t = ptond(vl,dvl,COEF(w[j]));
5349: pwrp(vl,x,DEG(w[j]),&p);
5350: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
5351: }
5352: return s;
5353: } else {
5354: NEWNM(m0); d = DL(m0);
5355: for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6 noro 5356: ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1 noro 5357: TD(d) = MUL_WEIGHT(e,i);
5358: if ( nd_blockmask) ndl_weight_mask(d);
5359: if ( nd_module ) MPOS(d) = 0;
5360: t = ptond(vl,dvl,COEF(w[j]));
5361: for ( m = BDY(t); m; m = NEXT(m) )
5362: ndl_addto(DL(m),d);
5363: SG(t) += TD(d);
5364: s = nd_add(0,s,t);
5365: }
5366: FREENM(m0);
5367: return s;
5368: }
5369: }
5370: }
5371:
5372: P ndvtop(int mod,VL vl,VL dvl,NDV p)
5373: {
5374: VL tvl;
5375: int len,n,j,i,e;
5376: NMV m;
5377: Z q;
5378: P c;
5379: UINT *d;
5380: P s,r,u,t,w;
5381: GFS gfs;
5382:
5383: if ( !p ) return 0;
5384: else {
5385: len = LEN(p);
5386: n = NV(p);
5387: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5388: for ( j = len-1, s = 0; 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 == -2 ) {
5392: c = (P)CZ(m);
5393: } else if ( mod > 0 ) {
1.6 noro 5394: STOZ(CM(m),q); c = (P)q;
1.1 noro 5395: } else
5396: c = CP(m);
5397: d = DL(m);
5398: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5399: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5400: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5401: }
5402: addp(vl,s,t,&u); s = u;
5403: }
5404: return s;
5405: }
5406: }
5407:
5408: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
5409: {
5410: VL tvl;
5411: int len,n,j,i,e;
5412: NMV m;
5413: Z q;
5414: P c;
5415: UINT *d;
5416: P s,r,u,t,w;
5417: GFS gfs;
5418: P *a;
5419: LIST l;
5420: NODE nd,nd1;
5421:
5422: if ( !p ) return 0;
5423: else {
5424: a = (P *)MALLOC((rank+1)*sizeof(P));
5425: for ( i = 0; i <= rank; i++ ) a[i] = 0;
5426: len = LEN(p);
5427: n = NV(p);
5428: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
5429: for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
5430: if ( mod == -1 ) {
5431: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
5432: } else if ( mod ) {
1.6 noro 5433: STOZ(CM(m),q); c = (P)q;
1.1 noro 5434: } else
5435: c = CP(m);
5436: d = DL(m);
5437: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6 noro 5438: MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1 noro 5439: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
5440: }
5441: addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
5442: }
5443: nd = 0;
5444: for ( i = rank; i > 0; i-- ) {
5445: MKNODE(nd1,a[i],nd); nd = nd1;
5446: }
5447: MKLIST(l,nd);
5448: return l;
5449: }
5450: }
5451:
5452: NDV ndtondv(int mod,ND p)
5453: {
5454: NDV d;
5455: NMV m,m0;
5456: NM t;
5457: int i,len;
5458:
5459: if ( !p ) return 0;
5460: len = LEN(p);
5461: if ( mod > 0 || mod == -1 )
5462: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5463: else
5464: m0 = m = MALLOC(len*nmv_adv);
5465: #if 0
5466: ndv_alloc += nmv_adv*len;
5467: #endif
5468: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
5469: ndl_copy(DL(t),DL(m));
1.6 noro 5470: CZ(m) = CZ(t);
1.1 noro 5471: }
5472: MKNDV(NV(p),m0,len,d);
5473: SG(d) = SG(p);
5474: return d;
5475: }
5476:
1.16 noro 5477: static int dmm_comp_nv;
5478:
5479: int dmm_comp(DMM *a,DMM *b)
5480: {
5481: return -compdmm(dmm_comp_nv,*a,*b);
5482: }
5483:
5484: void dmm_sort_by_ord(DMM *a,int len,int nv)
5485: {
5486: dmm_comp_nv = nv;
5487: qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
5488: }
5489:
5490: void dpm_sort(DPM p,DPM *rp)
5491: {
5492: DMM t,t1;
5493: int len,i,n;
5494: DMM *a;
5495: DPM d;
5496:
5497: if ( !p ) *rp = 0;
5498: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
5499: a = (DMM *)MALLOC(len*sizeof(DMM));
5500: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
5501: n = p->nv;
5502: dmm_sort_by_ord(a,len,n);
5503: t = 0;
5504: for ( i = len-1; i >= 0; i-- ) {
5505: NEWDMM(t1);
5506: t1->c = a[i]->c;
5507: t1->dl = a[i]->dl;
5508: t1->pos = a[i]->pos;
5509: t1->next = t;
5510: t = t1;
5511: }
5512: MKDPM(n,t,d);
5513: SG(d) = SG(p);
5514: *rp = d;
5515: }
5516:
1.18 ! noro 5517: int dpm_comp(DPM *a,DPM *b)
! 5518: {
! 5519: return compdpm(CO,*a,*b);
! 5520: }
! 5521:
! 5522: NODE dpm_sort_list(NODE l)
! 5523: {
! 5524: int i,len;
! 5525: NODE t,t1;
! 5526: DPM *a;
! 5527:
! 5528: len = length(l);
! 5529: a = (DPM *)MALLOC(len*sizeof(DPM));
! 5530: for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
! 5531: qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
! 5532: t = 0;
! 5533: for ( i = len-1; i >= 0; i-- ) {
! 5534: MKNODE(t1,(pointer)a[i],t); t = t1;
! 5535: }
! 5536: return t;
! 5537: }
! 5538:
1.16 noro 5539: NDV dpmtondv(int mod,DPM p)
5540: {
5541: NDV d;
5542: NMV m,m0;
5543: DMM t;
5544: DMM *a;
5545: int i,len,n;
5546:
5547: if ( !p ) return 0;
5548: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
5549: a = (DMM *)MALLOC(len*sizeof(DMM));
5550: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
5551: n = p->nv;
5552: dmm_sort_by_ord(a,len,n);
5553: if ( mod > 0 || mod == -1 )
5554: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
5555: else
5556: m0 = m = MALLOC(len*nmv_adv);
5557: #if 0
5558: ndv_alloc += nmv_adv*len;
5559: #endif
5560: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
5561: dltondl(n,a[i]->dl,DL(m));
5562: MPOS(DL(m)) = a[i]->pos;
5563: CZ(m) = (Z)a[i]->c;
5564: }
5565: MKNDV(NV(p),m0,len,d);
5566: SG(d) = SG(p);
5567: return d;
5568: }
5569:
1.1 noro 5570: ND ndvtond(int mod,NDV p)
5571: {
5572: ND d;
5573: NM m,m0;
5574: NMV t;
5575: int i,len;
5576:
5577: if ( !p ) return 0;
5578: m0 = 0;
5579: len = p->len;
5580: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5581: NEXTNM(m0,m);
5582: ndl_copy(DL(t),DL(m));
1.6 noro 5583: CZ(m) = CZ(t);
1.1 noro 5584: }
5585: NEXT(m) = 0;
5586: MKND(NV(p),m0,len,d);
5587: SG(d) = SG(p);
5588: return d;
5589: }
5590:
5591: DP ndvtodp(int mod,NDV p)
5592: {
5593: MP m,m0;
5594: DP d;
5595: NMV t;
5596: int i,len;
5597:
5598: if ( !p ) return 0;
5599: m0 = 0;
5600: len = p->len;
5601: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5602: NEXTMP(m0,m);
5603: m->dl = ndltodl(nd_nvar,DL(t));
5604: m->c = (Obj)ndctop(mod,t->c);
5605: }
5606: NEXT(m) = 0;
5607: MKDP(nd_nvar,m0,d);
5608: SG(d) = SG(p);
5609: return d;
5610: }
5611:
1.16 noro 5612: DPM ndvtodpm(int mod,NDV p)
5613: {
5614: DMM m,m0;
5615: DPM d;
5616: NMV t;
5617: int i,len;
5618:
5619: if ( !p ) return 0;
5620: m0 = 0;
5621: len = p->len;
5622: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5623: NEXTDMM(m0,m);
5624: m->dl = ndltodl(nd_nvar,DL(t));
5625: m->c = (Obj)ndctop(mod,t->c);
5626: m->pos = MPOS(DL(t));
5627: }
5628: NEXT(m) = 0;
5629: MKDPM(nd_nvar,m0,d);
5630: SG(d) = SG(p);
5631: return d;
5632: }
5633:
5634:
1.1 noro 5635: DP ndtodp(int mod,ND p)
5636: {
5637: MP m,m0;
5638: DP d;
5639: NM t;
5640: int i,len;
5641:
5642: if ( !p ) return 0;
5643: m0 = 0;
5644: len = p->len;
5645: for ( t = BDY(p); t; t = NEXT(t) ) {
5646: NEXTMP(m0,m);
5647: m->dl = ndltodl(nd_nvar,DL(t));
5648: m->c = (Obj)ndctop(mod,t->c);
5649: }
5650: NEXT(m) = 0;
5651: MKDP(nd_nvar,m0,d);
5652: SG(d) = SG(p);
5653: return d;
5654: }
5655:
5656: void ndv_print(NDV p)
5657: {
5658: NMV m;
5659: int i,len;
5660:
5661: if ( !p ) printf("0\n");
5662: else {
5663: len = LEN(p);
5664: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5665: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5666: else printf("+%d*",CM(m));
5667: ndl_print(DL(m));
5668: }
5669: printf("\n");
5670: }
5671: }
5672:
5673: void ndv_print_q(NDV p)
5674: {
5675: NMV m;
5676: int i,len;
5677:
5678: if ( !p ) printf("0\n");
5679: else {
5680: len = LEN(p);
5681: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5682: printf("+");
1.6 noro 5683: printexpr(CO,(Obj)CZ(m));
1.1 noro 5684: printf("*");
5685: ndl_print(DL(m));
5686: }
5687: printf("\n");
5688: }
5689: }
5690:
5691: NODE ndv_reducebase(NODE x,int *perm)
5692: {
5693: int len,i,j;
5694: NDVI w;
5695: NODE t,t0;
5696:
5697: len = length(x);
5698: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5699: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5700: w[i].p = BDY(t); w[i].i = perm[i];
5701: }
5702: for ( i = 0; i < len; i++ ) {
5703: for ( j = 0; j < i; j++ ) {
5704: if ( w[i].p && w[j].p ) {
5705: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5706: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5707: }
5708: }
5709: }
5710: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5711: if ( w[i].p ) {
5712: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5713: perm[j++] = w[i].i;
5714: }
5715: }
5716: NEXT(t) = 0; x = t0;
5717: return x;
5718: }
5719:
5720: /* XXX incomplete */
5721:
1.16 noro 5722: extern int dpm_ordtype;
5723:
1.1 noro 5724: void nd_init_ord(struct order_spec *ord)
5725: {
5726: nd_module = (ord->id >= 256);
5727: if ( nd_module ) {
5728: nd_dcomp = -1;
5729: nd_ispot = ord->ispot;
5730: nd_pot_nelim = ord->pot_nelim;
5731: nd_poly_weight_len = ord->nv;
5732: nd_poly_weight = ord->top_weight;
5733: nd_module_rank = ord->module_rank;
5734: nd_module_weight = ord->module_top_weight;
1.16 noro 5735: dpm_ordtype = ord->ispot;
1.1 noro 5736: }
5737: nd_matrix = 0;
5738: nd_matrix_len = 0;
5739: switch ( ord->id ) {
5740: case 0:
5741: switch ( ord->ord.simple ) {
5742: case 0:
5743: nd_dcomp = 1;
5744: nd_isrlex = 1;
5745: break;
5746: case 1:
5747: nd_dcomp = 1;
5748: nd_isrlex = 0;
5749: break;
5750: case 2:
5751: nd_dcomp = 0;
5752: nd_isrlex = 0;
5753: ndl_compare_function = ndl_lex_compare;
5754: break;
5755: case 11:
5756: /* XXX */
5757: nd_dcomp = 0;
5758: nd_isrlex = 1;
5759: ndl_compare_function = ndl_ww_lex_compare;
5760: break;
5761: default:
5762: error("nd_gr : unsupported order");
5763: }
5764: break;
5765: case 1:
5766: /* block order */
5767: /* XXX */
5768: nd_dcomp = -1;
5769: nd_isrlex = 0;
5770: ndl_compare_function = ndl_block_compare;
5771: break;
5772: case 2:
5773: /* matrix order */
5774: /* XXX */
5775: nd_dcomp = -1;
5776: nd_isrlex = 0;
5777: nd_matrix_len = ord->ord.matrix.row;
5778: nd_matrix = ord->ord.matrix.matrix;
5779: ndl_compare_function = ndl_matrix_compare;
5780: break;
5781: case 3:
5782: /* composite order */
5783: nd_dcomp = -1;
5784: nd_isrlex = 0;
5785: nd_worb_len = ord->ord.composite.length;
5786: nd_worb = ord->ord.composite.w_or_b;
5787: ndl_compare_function = ndl_composite_compare;
5788: break;
5789:
5790: /* module order */
5791: case 256:
5792: switch ( ord->ord.simple ) {
5793: case 0:
5794: nd_isrlex = 1;
5795: ndl_compare_function = ndl_module_grlex_compare;
5796: break;
5797: case 1:
5798: nd_isrlex = 0;
5799: ndl_compare_function = ndl_module_glex_compare;
5800: break;
5801: case 2:
5802: nd_isrlex = 0;
5803: ndl_compare_function = ndl_module_lex_compare;
5804: break;
5805: default:
5806: error("nd_gr : unsupported order");
5807: }
5808: break;
5809: case 257:
5810: /* block order */
5811: nd_isrlex = 0;
5812: ndl_compare_function = ndl_module_block_compare;
5813: break;
5814: case 258:
5815: /* matrix order */
5816: nd_isrlex = 0;
5817: nd_matrix_len = ord->ord.matrix.row;
5818: nd_matrix = ord->ord.matrix.matrix;
5819: ndl_compare_function = ndl_module_matrix_compare;
5820: break;
5821: case 259:
5822: /* composite order */
5823: nd_isrlex = 0;
5824: nd_worb_len = ord->ord.composite.length;
5825: nd_worb = ord->ord.composite.w_or_b;
5826: ndl_compare_function = ndl_module_composite_compare;
5827: break;
5828: }
5829: nd_ord = ord;
5830: }
5831:
5832: BlockMask nd_create_blockmask(struct order_spec *ord)
5833: {
5834: int n,i,j,s,l;
5835: UINT *t;
5836: BlockMask bm;
5837:
5838: /* we only create mask table for block order */
5839: if ( ord->id != 1 && ord->id != 257 )
5840: return 0;
5841: n = ord->ord.block.length;
5842: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5843: bm->n = n;
5844: bm->order_pair = ord->ord.block.order_pair;
5845: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5846: for ( i = 0, s = 0; i < n; i++ ) {
5847: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5848: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5849: l = bm->order_pair[i].length;
5850: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5851: }
5852: return bm;
5853: }
5854:
5855: EPOS nd_create_epos(struct order_spec *ord)
5856: {
5857: int i,j,l,s,ord_l,ord_o;
5858: EPOS epos;
5859: struct order_pair *op;
5860:
5861: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5862: switch ( ord->id ) {
5863: case 0: case 256:
5864: if ( nd_isrlex ) {
5865: for ( i = 0; i < nd_nvar; i++ ) {
5866: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5867: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5868: }
5869: } else {
5870: for ( i = 0; i < nd_nvar; i++ ) {
5871: epos[i].i = nd_exporigin + i/nd_epw;
5872: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5873: }
5874: }
5875: break;
5876: case 1: case 257:
5877: /* block order */
5878: l = ord->ord.block.length;
5879: op = ord->ord.block.order_pair;
5880: for ( j = 0, s = 0; j < l; j++ ) {
5881: ord_o = op[j].order;
5882: ord_l = op[j].length;
5883: if ( !ord_o )
5884: for ( i = 0; i < ord_l; i++ ) {
5885: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5886: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5887: }
5888: else
5889: for ( i = 0; i < ord_l; i++ ) {
5890: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5891: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5892: }
5893: s += ord_l;
5894: }
5895: break;
5896: case 2:
5897: /* matrix order */
5898: case 3:
5899: /* composite order */
5900: default:
5901: for ( i = 0; i < nd_nvar; i++ ) {
5902: epos[i].i = nd_exporigin + i/nd_epw;
5903: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5904: }
5905: break;
5906: }
5907: return epos;
5908: }
5909:
5910: /* external interface */
5911:
5912: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5913: {
5914: NODE t,in0,in;
5915: ND ndf,nf;
5916: NDV ndvf;
5917: VL vv,tv;
5918: int stat,nvar,max,mrank;
5919: union oNDC dn;
5920: Q cont;
5921: P pp;
5922: LIST ppl;
5923:
5924: if ( !f ) {
5925: *rp = 0;
5926: return;
5927: }
5928: pltovl(v,&vv);
5929: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5930:
5931: /* max=65536 implies nd_bpe=32 */
5932: max = 65536;
5933:
5934: nd_module = 0;
5935: /* nd_module will be set if ord is a module ordering */
5936: nd_init_ord(ord);
5937: nd_setup_parameters(nvar,max);
5938: if ( nd_module && OID(f) != O_LIST )
5939: error("nd_nf_p : the first argument must be a list");
5940: if ( nd_module ) mrank = length(BDY((LIST)f));
5941: /* conversion to ndv */
5942: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5943: NEXTNODE(in0,in);
5944: if ( nd_module ) {
5945: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5946: || length(BDY((LIST)BDY(t))) != mrank )
5947: error("nd_nf_p : inconsistent basis element");
5948: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5949: else ppl = (LIST)BDY(t);
5950: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5951: } else {
5952: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5953: else pp = (P)BDY(t);
5954: BDY(in) = (pointer)ptondv(CO,vv,pp);
5955: }
5956: if ( m ) ndv_mod(m,(NDV)BDY(in));
5957: }
5958: if ( in0 ) NEXT(in) = 0;
5959:
5960: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5961: else ndvf = ptondv(CO,vv,(P)f);
5962: if ( m ) ndv_mod(m,ndvf);
5963: ndf = (pointer)ndvtond(m,ndvf);
5964:
5965: /* dont sort, dont removecont */
5966: ndv_setup(m,0,in0,1,1);
5967: nd_scale=2;
1.6 noro 5968: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 5969: if ( !stat )
5970: error("nd_nf_p : exponent too large");
5971: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5972: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5973: }
5974:
5975: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5976: {
5977: NM m;
5978: UINT *t,*s;
5979: int i;
5980:
5981: for ( i = 0; i < n; i++ ) r[i] = 0;
5982: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5983: t = DL(m);
5984: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5985: r[i] = CM(m);
5986: }
5987: for ( i = 0; !r[i]; i++ );
5988: return i;
5989: }
5990:
5991: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
5992: {
5993: NM m;
5994: UINT *t,*s;
5995: int i;
5996:
5997: for ( i = 0; i < n; i++ ) r[i] = 0;
5998: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5999: t = DL(m);
6000: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6001: r[i] = CZ(m);
1.1 noro 6002: }
6003: for ( i = 0; !r[i]; i++ );
6004: return i;
6005: }
6006:
6007: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
6008: {
6009: NM m;
6010: UINT *t,*s;
6011: int i;
6012:
6013: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
6014: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
6015: t = DL(m);
6016: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
6017: mpz_set(r[i],BDY(CZ(m)));
6018: }
6019: for ( i = 0; !mpz_sgn(r[i]); i++ );
6020: return i;
6021: }
6022:
6023: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
6024: {
6025: NM m;
6026: unsigned long *v;
6027: int i,j,h,size;
6028: UINT *s,*t;
6029:
6030: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
6031: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
6032: bzero(v,size);
6033: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
6034: t = DL(m);
6035: h = ndl_hash_value(t);
6036: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6037: v[i/BLEN] |= 1L <<(i%BLEN);
6038: }
6039: return v;
6040: }
6041:
6042: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
6043: {
6044: NMV mr;
6045: UINT *d,*t,*s;
6046: int i,j,len,h,head;
6047:
6048: d = DL(m);
6049: len = LEN(p);
6050: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6051: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6052: ndl_add(d,DL(mr),t);
6053: h = ndl_hash_value(t);
6054: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6055: if ( j == 0 ) head = i;
6056: v[i/BLEN] |= 1L <<(i%BLEN);
6057: }
6058: return head;
6059: }
6060:
6061: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
6062: {
6063: NM m;
6064: NMV mr;
6065: UINT *d,*t,*s;
6066: NDV p;
6067: int i,j,len;
6068: Z *r;
6069:
6070: m = pair->mul;
6071: d = DL(m);
6072: p = nd_ps[pair->index];
6073: len = LEN(p);
6074: r = (Z *)CALLOC(n,sizeof(Q));
6075: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6076: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6077: ndl_add(d,DL(mr),t);
6078: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6079: r[i] = CZ(mr);
1.1 noro 6080: }
6081: return r;
6082: }
6083:
1.11 noro 6084: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 6085: {
6086: NM m;
6087: NMV mr;
1.11 noro 6088: UINT *d,*t,*s,*u;
1.1 noro 6089: NDV p;
6090: unsigned char *ivc;
6091: unsigned short *ivs;
6092: UINT *v,*ivi,*s0v;
1.11 noro 6093: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 6094: IndArray r;
6095:
6096: m = pair->mul;
6097: d = DL(m);
6098: if ( trace )
6099: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
6100: else
6101: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
6102:
6103: len = LEN(p);
6104: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6105: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 6106: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
6107: ndl_add(d,DL(mr),t);
6108: st = prev;
6109: ed = n;
6110: while ( ed > st ) {
6111: md = (st+ed)/2;
6112: u = s0+md*nd_wpd;
6113: c = DL_COMPARE(u,t);
6114: if ( c == 0 ) break;
6115: else if ( c > 0 ) st = md;
6116: else ed = md;
6117: }
6118: prev = v[j] = md;
1.1 noro 6119: }
6120: r = (IndArray)MALLOC(sizeof(struct oIndArray));
6121: r->head = v[0];
6122: diff = 0;
6123: for ( i = 1; i < len; i++ ) {
6124: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
6125: }
6126: if ( diff < 256 ) {
6127: r->width = 1;
6128: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
6129: r->index.c = ivc;
6130: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
6131: } else if ( diff < 65536 ) {
6132: r->width = 2;
6133: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
6134: r->index.s = ivs;
6135: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
6136: } else {
6137: r->width = 4;
6138: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
6139: r->index.i = ivi;
6140: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
6141: }
6142: return r;
6143: }
6144:
6145: int compress_array(Z *svect,Z *cvect,int n)
6146: {
6147: int i,j;
6148:
6149: for ( i = j = 0; i < n; i++ )
6150: if ( svect[i] ) cvect[j++] = svect[i];
6151: return j;
6152: }
6153:
6154: void expand_array(Z *svect,Z *cvect,int n)
6155: {
6156: int i,j;
6157:
6158: for ( i = j = 0; j < n; i++ )
6159: if ( svect[i] ) svect[i] = cvect[j++];
6160: }
6161:
1.8 noro 6162: #if 0
1.1 noro 6163: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6164: {
6165: int i,j,k,len,pos,prev,nz;
6166: Z cs,mcs,c1,c2,cr,gcd,t;
6167: IndArray ivect;
6168: unsigned char *ivc;
6169: unsigned short *ivs;
6170: unsigned int *ivi;
6171: NDV redv;
6172: NMV mr;
6173: NODE rp;
6174: int maxrs;
6175: double hmag;
6176: Z *cvect;
1.3 noro 6177: int l;
1.1 noro 6178:
6179: maxrs = 0;
6180: for ( i = 0; i < col && !svect[i]; i++ );
6181: if ( i == col ) return maxrs;
6182: hmag = p_mag((P)svect[i])*nd_scale;
6183: cvect = (Z *)MALLOC(col*sizeof(Q));
6184: for ( i = 0; i < nred; i++ ) {
6185: ivect = imat[i];
6186: k = ivect->head;
6187: if ( svect[k] ) {
6188: maxrs = MAX(maxrs,rp0[i]->sugar);
6189: redv = nd_demand?ndv_load(rp0[i]->index)
6190: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6191: len = LEN(redv); mr = BDY(redv);
1.6 noro 6192: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6193: chsgnz(cs,&mcs);
6194: if ( !UNIQ(cr) ) {
6195: for ( j = 0; j < col; j++ ) {
6196: mulz(svect[j],cr,&c1); svect[j] = c1;
6197: }
6198: }
6199: svect[k] = 0; prev = k;
6200: switch ( ivect->width ) {
6201: case 1:
6202: ivc = ivect->index.c;
6203: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6204: pos = prev+ivc[j]; prev = pos;
1.6 noro 6205: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6206: }
6207: break;
6208: case 2:
6209: ivs = ivect->index.s;
6210: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6211: pos = prev+ivs[j]; prev = pos;
1.6 noro 6212: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6213: }
6214: break;
6215: case 4:
6216: ivi = ivect->index.i;
6217: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6218: pos = prev+ivi[j]; prev = pos;
1.6 noro 6219: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6220: }
6221: break;
6222: }
6223: for ( j = k+1; j < col && !svect[j]; j++ );
6224: if ( j == col ) break;
6225: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6226: nz = compress_array(svect,cvect,col);
6227: removecont_array((P *)cvect,nz,1);
6228: expand_array(svect,cvect,nz);
6229: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6230: }
6231: }
6232: }
6233: nz = compress_array(svect,cvect,col);
6234: removecont_array((P *)cvect,nz,1);
6235: expand_array(svect,cvect,nz);
6236: if ( DP_Print ) {
6237: fprintf(asir_out,"-"); fflush(asir_out);
6238: }
6239: return maxrs;
6240: }
1.4 noro 6241: #else
1.9 noro 6242:
1.4 noro 6243: /* direct mpz version */
6244: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6245: {
6246: int i,j,k,len,pos,prev;
6247: mpz_t cs,cr,gcd;
6248: IndArray ivect;
6249: unsigned char *ivc;
6250: unsigned short *ivs;
6251: unsigned int *ivi;
6252: NDV redv;
6253: NMV mr;
6254: NODE rp;
6255: int maxrs;
6256: double hmag;
6257: int l;
1.13 noro 6258: static mpz_t *svect;
6259: static int svect_len=0;
1.4 noro 6260:
6261: maxrs = 0;
6262: for ( i = 0; i < col && !svect0[i]; i++ );
6263: if ( i == col ) return maxrs;
6264: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 6265: if ( col > svect_len ) {
6266: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
6267: svect_len = col;
6268: }
1.4 noro 6269: for ( i = 0; i < col; i++ ) {
6270: mpz_init(svect[i]);
6271: if ( svect0[i] )
6272: mpz_set(svect[i],BDY(svect0[i]));
6273: else
6274: mpz_set_ui(svect[i],0);
6275: }
6276: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6277: for ( i = 0; i < nred; i++ ) {
6278: ivect = imat[i];
6279: k = ivect->head;
6280: if ( mpz_sgn(svect[k]) ) {
6281: maxrs = MAX(maxrs,rp0[i]->sugar);
6282: redv = nd_demand?ndv_load(rp0[i]->index)
6283: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6284: len = LEN(redv); mr = BDY(redv);
1.6 noro 6285: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6286: mpz_div(cs,svect[k],gcd);
1.6 noro 6287: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6288: mpz_neg(cs,cs);
1.9 noro 6289: if ( MUNIMPZ(cr) )
6290: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6291: else if ( !UNIMPZ(cr) )
6292: for ( j = 0; j < col; j++ ) {
6293: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6294: }
1.4 noro 6295: mpz_set_ui(svect[k],0);
6296: prev = k;
6297: switch ( ivect->width ) {
6298: case 1:
6299: ivc = ivect->index.c;
6300: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6301: pos = prev+ivc[j]; prev = pos;
1.6 noro 6302: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6303: }
6304: break;
6305: case 2:
6306: ivs = ivect->index.s;
6307: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6308: pos = prev+ivs[j]; prev = pos;
1.6 noro 6309: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6310: }
6311: break;
6312: case 4:
6313: ivi = ivect->index.i;
6314: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6315: pos = prev+ivi[j]; prev = pos;
1.6 noro 6316: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6317: }
6318: break;
6319: }
6320: for ( j = k+1; j < col && !svect[j]; j++ );
6321: if ( j == col ) break;
6322: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6323: mpz_removecont_array(svect,col);
6324: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6325: }
6326: }
6327: }
6328: mpz_removecont_array(svect,col);
6329: if ( DP_Print ) {
6330: fprintf(asir_out,"-"); fflush(asir_out);
6331: }
6332: for ( i = 0; i < col; i++ )
6333: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6334: else svect0[i] = 0;
6335: return maxrs;
6336: }
6337: #endif
1.1 noro 6338:
6339: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6340: {
6341: int i,j,k,len,pos,prev;
6342: UINT c,c1,c2,c3,up,lo,dmy;
6343: IndArray ivect;
6344: unsigned char *ivc;
6345: unsigned short *ivs;
6346: unsigned int *ivi;
6347: NDV redv;
6348: NMV mr;
6349: NODE rp;
6350: int maxrs;
6351:
6352: maxrs = 0;
6353: for ( i = 0; i < nred; i++ ) {
6354: ivect = imat[i];
6355: k = ivect->head; svect[k] %= m;
6356: if ( (c = svect[k]) != 0 ) {
6357: maxrs = MAX(maxrs,rp0[i]->sugar);
6358: c = m-c; redv = nd_ps[rp0[i]->index];
6359: len = LEN(redv); mr = BDY(redv);
6360: svect[k] = 0; prev = k;
6361: switch ( ivect->width ) {
6362: case 1:
6363: ivc = ivect->index.c;
6364: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6365: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6366: if ( c1 ) {
6367: c2 = svect[pos];
6368: DMA(c1,c,c2,up,lo);
6369: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6370: } else svect[pos] = lo;
6371: }
6372: }
6373: break;
6374: case 2:
6375: ivs = ivect->index.s;
6376: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6377: pos = prev+ivs[j]; c1 = CM(mr);
6378: prev = pos;
6379: if ( c1 ) {
6380: c2 = svect[pos];
6381: DMA(c1,c,c2,up,lo);
6382: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6383: } else svect[pos] = lo;
6384: }
6385: }
6386: break;
6387: case 4:
6388: ivi = ivect->index.i;
6389: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6390: pos = prev+ivi[j]; c1 = CM(mr);
6391: prev = pos;
6392: if ( c1 ) {
6393: c2 = svect[pos];
6394: DMA(c1,c,c2,up,lo);
6395: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6396: } else svect[pos] = lo;
6397: }
6398: }
6399: break;
6400: }
6401: }
6402: }
6403: for ( i = 0; i < col; i++ )
6404: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6405: return maxrs;
6406: }
6407:
6408: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6409: {
6410: int i,j,k,len,pos,prev;
6411: UINT c,c1,c2,c3,up,lo,dmy;
6412: IndArray ivect;
6413: unsigned char *ivc;
6414: unsigned short *ivs;
6415: unsigned int *ivi;
6416: NDV redv;
6417: NMV mr;
6418: NODE rp;
6419: int maxrs;
6420:
6421: maxrs = 0;
6422: for ( i = 0; i < nred; i++ ) {
6423: ivect = imat[i];
6424: k = ivect->head;
6425: if ( (c = svect[k]) != 0 ) {
6426: maxrs = MAX(maxrs,rp0[i]->sugar);
6427: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6428: len = LEN(redv); mr = BDY(redv);
6429: svect[k] = 0; prev = k;
6430: switch ( ivect->width ) {
6431: case 1:
6432: ivc = ivect->index.c;
6433: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6434: pos = prev+ivc[j]; prev = pos;
6435: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6436: }
6437: break;
6438: case 2:
6439: ivs = ivect->index.s;
6440: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6441: pos = prev+ivs[j]; prev = pos;
6442: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6443: }
6444: break;
6445: case 4:
6446: ivi = ivect->index.i;
6447: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6448: pos = prev+ivi[j]; prev = pos;
6449: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6450: }
6451: break;
6452: }
6453: }
6454: }
6455: return maxrs;
6456: }
6457:
6458: ND nd_add_lf(ND p1,ND p2)
6459: {
6460: int n,c,can;
6461: ND r;
6462: NM m1,m2,mr0,mr,s;
6463: Z t;
6464:
6465: if ( !p1 ) return p2;
6466: else if ( !p2 ) return p1;
6467: else {
6468: can = 0;
6469: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6470: c = DL_COMPARE(DL(m1),DL(m2));
6471: switch ( c ) {
6472: case 0:
6473: addlf(CZ(m1),CZ(m2),&t);
6474: s = m1; m1 = NEXT(m1);
6475: if ( t ) {
6476: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6477: } else {
6478: can += 2; FREENM(s);
6479: }
6480: s = m2; m2 = NEXT(m2); FREENM(s);
6481: break;
6482: case 1:
6483: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6484: break;
6485: case -1:
6486: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6487: break;
6488: }
6489: }
6490: if ( !mr0 )
6491: if ( m1 ) mr0 = m1;
6492: else if ( m2 ) mr0 = m2;
6493: else return 0;
6494: else if ( m1 ) NEXT(mr) = m1;
6495: else if ( m2 ) NEXT(mr) = m2;
6496: else NEXT(mr) = 0;
6497: BDY(p1) = mr0;
6498: SG(p1) = MAX(SG(p1),SG(p2));
6499: LEN(p1) = LEN(p1)+LEN(p2)-can;
6500: FREEND(p2);
6501: return p1;
6502: }
6503: }
6504:
6505: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6506: {
6507: int i,j,k,len,pos,prev;
6508: mpz_t c,mc,c1;
6509: IndArray ivect;
6510: unsigned char *ivc;
6511: unsigned short *ivs;
6512: unsigned int *ivi;
6513: NDV redv;
6514: NMV mr;
6515: NODE rp;
6516: int maxrs;
6517:
6518: maxrs = 0;
6519: lf_lazy = 1;
6520: for ( i = 0; i < nred; i++ ) {
6521: ivect = imat[i];
6522: k = ivect->head;
6523: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6524: if ( mpz_sgn(svect[k]) ) {
6525: maxrs = MAX(maxrs,rp0[i]->sugar);
6526: mpz_neg(svect[k],svect[k]);
6527: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6528: len = LEN(redv); mr = BDY(redv);
6529: prev = k;
6530: switch ( ivect->width ) {
6531: case 1:
6532: ivc = ivect->index.c;
6533: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6534: pos = prev+ivc[j]; prev = pos;
6535: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6536: }
6537: break;
6538: case 2:
6539: ivs = ivect->index.s;
6540: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6541: pos = prev+ivs[j]; prev = pos;
6542: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6543: }
6544: break;
6545: case 4:
6546: ivi = ivect->index.i;
6547: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6548: pos = prev+ivi[j]; prev = pos;
6549: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6550: }
6551: break;
6552: }
6553: mpz_set_ui(svect[k],0);
6554: }
6555: }
6556: lf_lazy=0;
6557: for ( i = 0; i < col; i++ ) {
6558: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6559: }
6560: return maxrs;
6561: }
6562:
6563: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6564: {
6565: int i,j,k,l,rank,s;
6566: mpz_t a,a1,inv;
6567: mpz_t *t,*pivot,*pk;
6568: mpz_t **mat;
6569: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6570: int size,size1;
6571:
6572: mpz_init(inv);
6573: mpz_init(a);
6574: mat = (mpz_t **)mat0;
6575: size = 0;
6576: for ( rank = 0, j = 0; j < col; j++ ) {
6577: for ( i = rank; i < row; i++ ) {
6578: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6579: }
6580: for ( i = rank; i < row; i++ )
6581: if ( mpz_sgn(mat[i][j]) )
6582: break;
6583: if ( i == row ) {
6584: colstat[j] = 0;
6585: continue;
6586: } else
6587: colstat[j] = 1;
6588: if ( i != rank ) {
6589: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6590: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6591: }
6592: pivot = mat[rank];
6593: s = sugar[rank];
6594: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6595: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6596: if ( mpz_sgn(*pk) ) {
6597: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6598: }
6599: for ( i = rank+1; i < row; i++ ) {
6600: t = mat[i];
6601: if ( mpz_sgn(t[j]) ) {
6602: sugar[i] = MAX(sugar[i],s);
6603: mpz_neg(a,t[j]);
6604: red_by_vect_lf(t+j,pivot+j,a,col-j);
6605: }
6606: }
6607: rank++;
6608: }
6609: for ( j = col-1, l = rank-1; j >= 0; j-- )
6610: if ( colstat[j] ) {
6611: pivot = mat[l];
6612: s = sugar[l];
6613: for ( k = j; k < col; k++ )
6614: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6615: for ( i = 0; i < l; i++ ) {
6616: t = mat[i];
6617: if ( mpz_sgn(t[j]) ) {
6618: sugar[i] = MAX(sugar[i],s);
6619: mpz_neg(a,t[j]);
6620: red_by_vect_lf(t+j,pivot+j,a,col-j);
6621: }
6622: }
6623: l--;
6624: }
6625: for ( j = 0, l = 0; l < rank; j++ )
6626: if ( colstat[j] ) {
6627: t = mat[l];
6628: for ( k = j; k < col; k++ ) {
6629: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6630: }
6631: l++;
6632: }
6633: return rank;
6634: }
6635:
6636:
6637: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6638: {
6639: int j,k,len;
6640: UINT *p;
6641: UINT c;
6642: NDV r;
6643: NMV mr0,mr;
6644:
6645: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6646: if ( !len ) return 0;
6647: else {
6648: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6649: #if 0
6650: ndv_alloc += nmv_adv*len;
6651: #endif
6652: mr = mr0;
6653: p = s0vect;
6654: for ( j = k = 0; j < col; j++, p += nd_wpd )
6655: if ( !rhead[j] ) {
6656: if ( (c = vect[k++]) != 0 ) {
6657: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6658: }
6659: }
6660: MKNDV(nd_nvar,mr0,len,r);
6661: return r;
6662: }
6663: }
6664:
6665: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6666: {
6667: int j,k,len;
6668: UINT *p;
6669: NDV r;
6670: NMV mr0,mr;
6671:
6672: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6673: if ( !len ) return 0;
6674: else {
6675: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6676: mr = mr0;
6677: p = s0vect;
6678: for ( j = 0; j < col; j++, p += nd_wpd )
6679: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6680: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6681: }
6682: MKNDV(nd_nvar,mr0,len,r);
6683: return r;
6684: }
6685: }
6686:
6687: /* for preprocessed vector */
6688:
6689: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6690: {
1.6 noro 6691: int j,k,len;
6692: UINT *p;
6693: Z c;
6694: NDV r;
6695: NMV mr0,mr;
1.1 noro 6696:
1.6 noro 6697: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6698: if ( !len ) return 0;
6699: else {
6700: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6701: #if 0
1.6 noro 6702: ndv_alloc += nmv_adv*len;
1.1 noro 6703: #endif
1.6 noro 6704: mr = mr0;
6705: p = s0vect;
6706: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6707: if ( !rhead[j] ) {
6708: if ( (c = vect[k++]) != 0 ) {
6709: if ( !INT(c) )
6710: error("vect_to_ndv_q : components must be integers");
6711: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6712: }
6713: }
1.1 noro 6714: }
1.6 noro 6715: MKNDV(nd_nvar,mr0,len,r);
6716: return r;
6717: }
1.1 noro 6718: }
6719:
6720: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6721: {
6722: int j,k,len;
6723: UINT *p;
6724: mpz_t c;
6725: NDV r;
6726: NMV mr0,mr;
6727:
6728: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6729: if ( !len ) return 0;
6730: else {
6731: mr0 = (NMV)MALLOC(nmv_adv*len);
6732: #if 0
6733: ndv_alloc += nmv_adv*len;
6734: #endif
6735: mr = mr0;
6736: p = s0vect;
6737: for ( j = k = 0; j < col; j++, p += nd_wpd )
6738: if ( !rhead[j] ) {
6739: c[0] = vect[k++][0];
6740: if ( mpz_sgn(c) ) {
6741: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6742: }
6743: }
6744: MKNDV(nd_nvar,mr0,len,r);
6745: return r;
6746: }
6747: }
6748:
6749: /* for plain vector */
6750:
6751: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6752: {
6753: int j,k,len;
6754: UINT *p;
6755: Z c;
6756: NDV r;
6757: NMV mr0,mr;
6758:
6759: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6760: if ( !len ) return 0;
6761: else {
6762: mr0 = (NMV)MALLOC(nmv_adv*len);
6763: #if 0
6764: ndv_alloc += nmv_adv*len;
6765: #endif
6766: mr = mr0;
6767: p = s0vect;
6768: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6769: if ( (c = vect[k]) != 0 ) {
6770: if ( !INT(c) )
1.6 noro 6771: error("plain_vect_to_ndv_q : components must be integers");
6772: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6773: }
6774: MKNDV(nd_nvar,mr0,len,r);
6775: return r;
6776: }
6777: }
6778:
6779: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6780: {
6781: ND_pairs t;
6782: NODE sp0,sp;
6783: int stat;
6784: ND spol;
6785:
6786: for ( t = l; t; t = NEXT(t) ) {
6787: stat = nd_sp(m,trace,t,&spol);
6788: if ( !stat ) return 0;
6789: if ( spol ) {
6790: add_pbucket_symbolic(bucket,spol);
6791: }
6792: }
6793: return 1;
6794: }
6795:
6796: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6797: {
6798: NODE rp0,rp;
6799: NM mul,head,s0,s;
6800: int index,col,i,sugar;
6801: RHist h;
6802: UINT *s0v,*p;
6803: NM_ind_pair pair;
6804: ND red;
6805: NDV *ps;
6806:
6807: s0 = 0; rp0 = 0; col = 0;
6808: if ( nd_demand )
6809: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6810: else
6811: ps = trace?nd_ps_trace:nd_ps;
6812: while ( 1 ) {
6813: head = remove_head_pbucket_symbolic(bucket);
6814: if ( !head ) break;
6815: if ( !s0 ) s0 = head;
6816: else NEXT(s) = head;
6817: s = head;
6818: index = ndl_find_reducer(DL(head));
6819: if ( index >= 0 ) {
6820: h = nd_psh[index];
6821: NEWNM(mul);
6822: ndl_sub(DL(head),DL(h),DL(mul));
6823: if ( ndl_check_bound2(index,DL(mul)) )
6824: return 0;
6825: sugar = TD(DL(mul))+SG(ps[index]);
6826: MKNM_ind_pair(pair,mul,index,sugar);
6827: red = ndv_mul_nm_symbolic(mul,ps[index]);
6828: add_pbucket_symbolic(bucket,nd_remove_head(red));
6829: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6830: }
6831: col++;
6832: }
6833: if ( rp0 ) NEXT(rp) = 0;
6834: NEXT(s) = 0;
6835: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6836: for ( i = 0, p = s0v, s = s0; i < col;
6837: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6838: *s0vect = s0v;
6839: *r = rp0;
6840: return col;
6841: }
6842:
6843: void print_ndp(ND_pairs l)
6844: {
6845: ND_pairs t;
6846:
6847: for ( t = l; t; t = NEXT(t) )
6848: printf("[%d,%d] ",t->i1,t->i2);
6849: printf("\n");
6850: }
6851:
6852: NODE nd_f4(int m,int checkonly,int **indp)
6853: {
6854: int i,nh,stat,index,f4red;
6855: NODE r,g,tn0,tn,node;
6856: ND_pairs d,l,t,ll0,ll,lh;
6857: LIST l0,l1;
6858: ND spol,red;
6859: NDV nf,redv;
6860: NM s0,s;
6861: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6862: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6863: UINT c;
6864: UINT **spmat;
6865: UINT *s0vect,*svect,*p,*v;
6866: int *colstat;
6867: IndArray *imat;
6868: int *rhead;
6869: int spcol,sprow;
6870: int sugar,sugarh;
6871: PGeoBucket bucket;
6872: struct oEGT eg0,eg1,eg_f4;
6873: Z i1,i2,sugarq;
1.12 noro 6874:
6875: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6876: #if 0
6877: ndv_alloc = 0;
6878: #endif
1.11 noro 6879: Nf4_red=0;
1.1 noro 6880: g = 0; d = 0;
6881: for ( i = 0; i < nd_psn; i++ ) {
6882: d = update_pairs(d,g,i,0);
6883: g = update_base(g,i);
6884: }
6885: nzlist = 0;
6886: nzlist_t = nd_nzlist;
6887: f4red = 1;
6888: nd_last_nonzero = 0;
6889: while ( d ) {
6890: get_eg(&eg0);
6891: l = nd_minsugarp(d,&d);
6892: sugar = nd_sugarweight?l->sugar2:SG(l);
6893: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6894: if ( nzlist_t ) {
6895: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6896: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6897: tn = BDY((LIST)ARG1(node));
6898: if ( !tn ) {
6899: nzlist_t = NEXT(nzlist_t);
6900: continue;
6901: }
6902: /* tn = [[i1,i2],...] */
6903: lh = nd_ipairtospair(tn);
6904: }
6905: bucket = create_pbucket();
6906: stat = nd_sp_f4(m,0,l,bucket);
6907: if ( !stat ) {
6908: for ( t = l; NEXT(t); t = NEXT(t) );
6909: NEXT(t) = d; d = l;
6910: d = nd_reconstruct(0,d);
6911: continue;
6912: }
6913: if ( bucket->m < 0 ) continue;
6914: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6915: if ( !col ) {
6916: for ( t = l; NEXT(t); t = NEXT(t) );
6917: NEXT(t) = d; d = l;
6918: d = nd_reconstruct(0,d);
6919: continue;
6920: }
1.12 noro 6921: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6922: if ( DP_Print )
1.6 noro 6923: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6924: sugar,eg_f4.exectime);
1.1 noro 6925: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6926: if ( checkonly && nflist ) return 0;
6927: /* adding new bases */
6928: if ( nflist ) nd_last_nonzero = f4red;
6929: for ( r = nflist; r; r = NEXT(r) ) {
6930: nf = (NDV)BDY(r);
6931: ndv_removecont(m,nf);
6932: if ( !m && nd_nalg ) {
6933: ND nf1;
6934:
6935: nf1 = ndvtond(m,nf);
6936: nd_monic(0,&nf1);
6937: nd_removecont(m,nf1);
6938: nf = ndtondv(m,nf1);
6939: }
6940: nh = ndv_newps(m,nf,0,1);
6941: d = update_pairs(d,g,nh,0);
6942: g = update_base(g,nh);
6943: }
6944: if ( DP_Print ) {
6945: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6946: }
6947: if ( nd_gentrace ) {
6948: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6949: NEXTNODE(tn0,tn);
1.6 noro 6950: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6951: node = mknode(2,i1,i2); MKLIST(l0,node);
6952: BDY(tn) = l0;
6953: }
6954: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6955: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6956: MKNODE(node,l1,nzlist); nzlist = node;
6957: }
6958: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6959: f4red++;
6960: if ( nd_f4red && f4red > nd_f4red ) break;
6961: if ( nd_rank0 && !nflist ) break;
6962: }
6963: if ( nd_gentrace ) {
6964: MKLIST(l0,reverse_node(nzlist));
6965: MKNODE(nd_alltracelist,l0,0);
6966: }
6967: #if 0
6968: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6969: #endif
1.12 noro 6970: if ( DP_Print ) {
6971: fprintf(asir_out,"number of red=%d,",Nf4_red);
6972: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
6973: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
6974: }
1.1 noro 6975: conv_ilist(nd_demand,0,g,indp);
6976: return g;
6977: }
6978:
6979: NODE nd_f4_trace(int m,int **indp)
6980: {
6981: int i,nh,stat,index;
6982: NODE r,g;
6983: ND_pairs d,l,l0,t;
6984: ND spol,red;
6985: NDV nf,redv,nfqv,nfv;
6986: NM s0,s;
6987: NODE rp0,srp0,nflist;
6988: int nsp,nred,col,rank,len,k,j,a;
6989: UINT c;
6990: UINT **spmat;
6991: UINT *s0vect,*svect,*p,*v;
6992: int *colstat;
6993: IndArray *imat;
6994: int *rhead;
6995: int spcol,sprow;
6996: int sugar;
6997: PGeoBucket bucket;
6998: struct oEGT eg0,eg1,eg_f4;
6999:
7000: g = 0; d = 0;
7001: for ( i = 0; i < nd_psn; i++ ) {
7002: d = update_pairs(d,g,i,0);
7003: g = update_base(g,i);
7004: }
7005: while ( d ) {
7006: get_eg(&eg0);
7007: l = nd_minsugarp(d,&d);
7008: sugar = SG(l);
7009: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
7010: bucket = create_pbucket();
7011: stat = nd_sp_f4(m,0,l,bucket);
7012: if ( !stat ) {
7013: for ( t = l; NEXT(t); t = NEXT(t) );
7014: NEXT(t) = d; d = l;
7015: d = nd_reconstruct(1,d);
7016: continue;
7017: }
7018: if ( bucket->m < 0 ) continue;
7019: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
7020: if ( !col ) {
7021: for ( t = l; NEXT(t); t = NEXT(t) );
7022: NEXT(t) = d; d = l;
7023: d = nd_reconstruct(1,d);
7024: continue;
7025: }
7026: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
7027: if ( DP_Print )
1.3 noro 7028: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 7029: sugar,eg_f4.exectime);
1.1 noro 7030: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
7031: if ( !l0 ) continue;
7032: l = l0;
7033:
7034: /* over Q */
7035: bucket = create_pbucket();
7036: stat = nd_sp_f4(0,1,l,bucket);
7037: if ( !stat ) {
7038: for ( t = l; NEXT(t); t = NEXT(t) );
7039: NEXT(t) = d; d = l;
7040: d = nd_reconstruct(1,d);
7041: continue;
7042: }
7043: if ( bucket->m < 0 ) continue;
7044: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7045: if ( !col ) {
7046: for ( t = l; NEXT(t); t = NEXT(t) );
7047: NEXT(t) = d; d = l;
7048: d = nd_reconstruct(1,d);
7049: continue;
7050: }
7051: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7052: /* adding new bases */
7053: for ( r = nflist; r; r = NEXT(r) ) {
7054: nfqv = (NDV)BDY(r);
7055: ndv_removecont(0,nfqv);
1.6 noro 7056: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 7057: if ( nd_nalg ) {
7058: ND nf1;
7059:
7060: nf1 = ndvtond(m,nfqv);
7061: nd_monic(0,&nf1);
7062: nd_removecont(0,nf1);
7063: nfqv = ndtondv(0,nf1); nd_free(nf1);
7064: }
7065: nfv = ndv_dup(0,nfqv);
7066: ndv_mod(m,nfv);
7067: ndv_removecont(m,nfv);
7068: nh = ndv_newps(0,nfv,nfqv,1);
7069: d = update_pairs(d,g,nh,0);
7070: g = update_base(g,nh);
7071: }
7072: }
7073: #if 0
7074: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7075: #endif
7076: conv_ilist(nd_demand,1,g,indp);
7077: return g;
7078: }
7079:
7080: int rref(matrix mat,int *sugar)
7081: {
7082: int row,col,i,j,k,l,s,wcol,wj;
7083: unsigned long bj;
7084: unsigned long **a;
7085: unsigned long *ai,*ak,*as,*t;
7086: int *pivot;
7087:
7088: row = mat->row;
7089: col = mat->col;
7090: a = mat->a;
7091: wcol = (col+BLEN-1)/BLEN;
7092: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7093: i = 0;
7094: for ( j = 0; j < col; j++ ) {
7095: wj = j/BLEN; bj = 1L<<(j%BLEN);
7096: for ( k = i; k < row; k++ )
7097: if ( a[k][wj] & bj ) break;
7098: if ( k == row ) continue;
7099: pivot[i] = j;
7100: if ( k != i ) {
7101: t = a[i]; a[i] = a[k]; a[k] = t;
7102: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7103: }
7104: ai = a[i];
7105: for ( k = i+1; k < row; k++ ) {
7106: ak = a[k];
7107: if ( ak[wj] & bj ) {
7108: for ( l = wj; l < wcol; l++ )
7109: ak[l] ^= ai[l];
7110: sugar[k] = MAX(sugar[k],sugar[i]);
7111: }
7112: }
7113: i++;
7114: }
7115: for ( k = i-1; k >= 0; k-- ) {
7116: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
7117: ak = a[k];
7118: for ( s = 0; s < k; s++ ) {
7119: as = a[s];
7120: if ( as[wj] & bj ) {
7121: for ( l = wj; l < wcol; l++ )
7122: as[l] ^= ak[l];
7123: sugar[s] = MAX(sugar[s],sugar[k]);
7124: }
7125: }
7126: }
7127: return i;
7128: }
7129:
7130: void print_matrix(matrix mat)
7131: {
7132: int row,col,i,j;
7133: unsigned long *ai;
7134:
7135: row = mat->row;
7136: col = mat->col;
7137: printf("%d x %d\n",row,col);
7138: for ( i = 0; i < row; i++ ) {
7139: ai = mat->a[i];
7140: for ( j = 0; j < col; j++ ) {
7141: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
7142: else putchar('0');
7143: }
7144: putchar('\n');
7145: }
7146: }
7147:
7148: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
7149:
7150: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
7151: {
7152: int row,col,wcol,wj,i,j;
7153: unsigned long bj;
7154: unsigned long *ai;
7155: unsigned long **a;
7156: int len;
7157: int *pos;
7158:
7159: row = mat->row;
7160: col = mat->col;
7161: wcol = (col+BLEN-1)/BLEN;
7162: pos = (int *)MALLOC(wcol*sizeof(int));
7163: bzero(pos,wcol*sizeof(int));
7164: for ( i = j = 0; i < wcol; i++ )
7165: if ( v[i] ) pos[j++] = i;;
7166: len = j;
7167: wj = rhead/BLEN;
7168: bj = 1L<<rhead%BLEN;
7169: a = mat->a;
7170: for ( i = 0; i < row; i++ ) {
7171: ai = a[i];
7172: if ( ai[wj]&bj ) {
7173: for ( j = 0; j < len; j++ )
7174: ai[pos[j]] ^= v[pos[j]];
7175: sugar[i] = MAX(sugar[i],rsugar);
7176: }
7177: }
7178: }
7179:
7180: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7181: {
7182: int nsp,nred,i,i0,k,rank,row;
7183: NODE r0,rp;
7184: ND_pairs sp;
7185: ND spol;
7186: NM_ind_pair rt;
7187: int *s0hash;
7188: UINT *s;
7189: int *pivot,*sugar,*head;
7190: matrix mat;
7191: NM m;
7192: NODE r;
7193: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7194: int rhead,rsugar,size;
7195: unsigned long *v;
7196:
7197: get_eg(&eg0);
7198: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7199: nred = length(rp0);
7200: mat = alloc_matrix(nsp,col);
7201: s0hash = (int *)MALLOC(col*sizeof(int));
7202: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7203: s0hash[i] = ndl_hash_value(s);
7204:
7205: sugar = (int *)MALLOC(nsp*sizeof(int));
7206: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7207: nd_sp(2,0,sp,&spol);
7208: if ( spol ) {
7209: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7210: sugar[i] = SG(spol);
7211: i++;
7212: }
7213: }
7214: mat->row = i;
7215: if ( DP_Print ) {
7216: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7217: }
7218: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7219: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7220: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7221: rt = (NM_ind_pair)BDY(rp);
7222: bzero(v,size);
7223: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7224: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7225: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7226: }
7227:
7228: get_eg(&eg1);
7229: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7230: rank = rref(mat,sugar);
7231:
7232: for ( i = 0, r0 = 0; i < rank; i++ ) {
7233: NEXTNODE(r0,r);
7234: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7235: SG((NDV)BDY(r)) = sugar[i];
7236: }
7237: if ( r0 ) NEXT(r) = 0;
7238: get_eg(&eg2);
7239: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7240: if ( DP_Print ) {
7241: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7242: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7243: fflush(asir_out);
7244: }
7245: return r0;
7246: }
7247:
7248:
7249: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7250: {
7251: IndArray *imat;
1.11 noro 7252: int nsp,nred,i,start;
1.1 noro 7253: int *rhead;
7254: NODE r0,rp;
7255: ND_pairs sp;
7256: NM_ind_pair *rvect;
7257: UINT *s;
7258: int *s0hash;
1.11 noro 7259: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7260:
7261: if ( m == 2 && nd_rref2 )
7262: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7263:
7264: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7265: nred = length(rp0);
7266: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7267: rhead = (int *)MALLOC(col*sizeof(int));
7268: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7269:
7270: /* construction of index arrays */
1.11 noro 7271: get_eg(&eg0);
1.1 noro 7272: if ( DP_Print ) {
1.11 noro 7273: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7274: fflush(asir_out);
1.1 noro 7275: }
7276: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7277: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7278: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7279: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7280: rhead[imat[i]->head] = 1;
1.11 noro 7281: start = imat[i]->head;
7282: }
1.12 noro 7283: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7284: if ( DP_Print ) {
7285: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7286: fflush(asir_out);
1.1 noro 7287: }
7288: if ( m > 0 )
1.7 noro 7289: #if SIZEOF_LONG==8
1.1 noro 7290: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7291: #else
7292: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7293: #endif
7294: else if ( m == -1 )
7295: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7296: else if ( m == -2 )
7297: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7298: else
7299: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7300: return r0;
7301: }
7302:
7303: /* for Fp, 2<=p<2^16 */
7304:
7305: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7306: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7307: {
7308: int spcol,sprow,a;
7309: int i,j,k,l,rank;
7310: NODE r0,r;
7311: ND_pairs sp;
7312: ND spol;
7313: UINT **spmat;
7314: UINT *svect,*v;
7315: int *colstat;
7316: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7317: int maxrs;
7318: int *spsugar;
7319: ND_pairs *spactive;
7320:
7321: spcol = col-nred;
7322: get_eg(&eg0);
7323: /* elimination (1st step) */
7324: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7325: svect = (UINT *)MALLOC(col*sizeof(UINT));
7326: spsugar = (int *)MALLOC(nsp*sizeof(int));
7327: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7328: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7329: nd_sp(m,0,sp,&spol);
7330: if ( !spol ) continue;
7331: nd_to_vect(m,s0vect,col,spol,svect);
7332: if ( m == -1 )
7333: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7334: else
7335: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7336: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7337: if ( i < col ) {
7338: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7339: for ( j = k = 0; j < col; j++ )
7340: if ( !rhead[j] ) v[k++] = svect[j];
7341: spsugar[sprow] = MAX(maxrs,SG(spol));
7342: if ( nz )
7343: spactive[sprow] = sp;
7344: sprow++;
7345: }
7346: nd_free(spol);
7347: }
7348: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7349: if ( DP_Print ) {
1.5 noro 7350: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7351: fflush(asir_out);
7352: }
7353: /* free index arrays */
7354: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7355:
7356: /* elimination (2nd step) */
7357: colstat = (int *)MALLOC(spcol*sizeof(int));
7358: if ( m == -1 )
7359: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7360: else
7361: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7362: r0 = 0;
7363: for ( i = 0; i < rank; i++ ) {
7364: NEXTNODE(r0,r); BDY(r) =
7365: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7366: SG((NDV)BDY(r)) = spsugar[i];
7367: GCFREE(spmat[i]);
7368: }
7369: if ( r0 ) NEXT(r) = 0;
7370:
7371: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7372: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7373: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7374: if ( DP_Print ) {
1.5 noro 7375: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7376: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7377: nsp,nred,sprow,spcol,rank);
1.5 noro 7378: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7379: }
7380: if ( nz ) {
7381: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7382: if ( rank > 0 ) {
7383: NEXT(spactive[rank-1]) = 0;
7384: *nz = spactive[0];
7385: } else
7386: *nz = 0;
7387: }
7388: return r0;
7389: }
7390:
7391:
7392: /* for small finite fields */
7393:
7394: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7395: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7396: {
7397: int spcol,sprow,a;
7398: int i,j,k,l,rank;
7399: NODE r0,r;
7400: ND_pairs sp;
7401: ND spol;
7402: UINT **spmat;
7403: UINT *svect,*v;
7404: int *colstat;
7405: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7406: int maxrs;
7407: int *spsugar;
7408: ND_pairs *spactive;
7409:
7410: spcol = col-nred;
7411: get_eg(&eg0);
7412: /* elimination (1st step) */
7413: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7414: svect = (UINT *)MALLOC(col*sizeof(UINT));
7415: spsugar = (int *)MALLOC(nsp*sizeof(int));
7416: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7417: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7418: nd_sp(m,0,sp,&spol);
7419: if ( !spol ) continue;
7420: nd_to_vect(m,s0vect,col,spol,svect);
7421: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7422: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7423: if ( i < col ) {
7424: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7425: for ( j = k = 0; j < col; j++ )
7426: if ( !rhead[j] ) v[k++] = svect[j];
7427: spsugar[sprow] = MAX(maxrs,SG(spol));
7428: if ( nz )
7429: spactive[sprow] = sp;
7430: sprow++;
7431: }
7432: nd_free(spol);
7433: }
7434: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7435: if ( DP_Print ) {
1.5 noro 7436: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7437: fflush(asir_out);
7438: }
7439: /* free index arrays */
7440: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7441:
7442: /* elimination (2nd step) */
7443: colstat = (int *)MALLOC(spcol*sizeof(int));
7444: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7445: r0 = 0;
7446: for ( i = 0; i < rank; i++ ) {
7447: NEXTNODE(r0,r); BDY(r) =
7448: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7449: SG((NDV)BDY(r)) = spsugar[i];
7450: GCFREE(spmat[i]);
7451: }
7452: if ( r0 ) NEXT(r) = 0;
7453:
7454: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7455: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7456: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7457: if ( DP_Print ) {
1.5 noro 7458: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7459: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7460: nsp,nred,sprow,spcol,rank);
1.5 noro 7461: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7462: }
7463: if ( nz ) {
7464: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7465: if ( rank > 0 ) {
7466: NEXT(spactive[rank-1]) = 0;
7467: *nz = spactive[0];
7468: } else
7469: *nz = 0;
7470: }
7471: return r0;
7472: }
7473:
7474: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7475: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7476: {
7477: int spcol,sprow,a;
7478: int i,j,k,l,rank;
7479: NODE r0,r;
7480: ND_pairs sp;
7481: ND spol;
7482: mpz_t **spmat;
7483: mpz_t *svect,*v;
7484: int *colstat;
7485: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7486: int maxrs;
7487: int *spsugar;
7488: pointer *w;
7489:
7490: spcol = col-nred;
7491: get_eg(&eg0);
7492: /* elimination (1st step) */
7493: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7494: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7495: spsugar = (int *)MALLOC(nsp*sizeof(int));
7496: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7497: nd_sp(m,trace,sp,&spol);
7498: if ( !spol ) continue;
7499: nd_to_vect_lf(s0vect,col,spol,svect);
7500: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7501: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7502: if ( i < col ) {
7503: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7504: for ( j = k = 0; j < col; j++ )
7505: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7506: spsugar[sprow] = MAX(maxrs,SG(spol));
7507: sprow++;
7508: }
7509: /* nd_free(spol); */
7510: }
7511: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7512: if ( DP_Print ) {
1.5 noro 7513: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7514: fflush(asir_out);
7515: }
7516: /* free index arrays */
7517: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7518:
7519: /* elimination (2nd step) */
7520: colstat = (int *)MALLOC(spcol*sizeof(int));
7521: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7522: w = (pointer *)MALLOC(rank*sizeof(pointer));
7523: for ( i = 0; i < rank; i++ ) {
7524: #if 0
7525: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7526: SG((NDV)w[rank-i-1]) = spsugar[i];
7527: #else
7528: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7529: SG((NDV)w[i]) = spsugar[i];
7530: #endif
7531: /* GCFREE(spmat[i]); */
7532:
7533: }
7534: #if 0
7535: qsort(w,rank,sizeof(NDV),
7536: (int (*)(const void *,const void *))ndv_compare);
7537: #endif
7538: r0 = 0;
7539: for ( i = 0; i < rank; i++ ) {
7540: NEXTNODE(r0,r); BDY(r) = w[i];
7541: }
7542: if ( r0 ) NEXT(r) = 0;
7543:
7544: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7545: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7546: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7547: if ( DP_Print ) {
1.5 noro 7548: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7549: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7550: nsp,nred,sprow,spcol,rank);
1.5 noro 7551: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7552: }
7553: return r0;
7554: }
7555:
7556: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7557: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7558: {
7559: int spcol,sprow,a;
7560: int i,j,k,l,rank;
7561: NODE r0,r;
7562: ND_pairs sp;
7563: ND spol;
7564: Z **spmat;
7565: Z *svect,*v;
7566: int *colstat;
7567: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7568: int maxrs;
7569: int *spsugar;
7570: pointer *w;
7571:
7572: spcol = col-nred;
7573: get_eg(&eg0);
7574: /* elimination (1st step) */
7575: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7576: svect = (Z *)MALLOC(col*sizeof(Q));
7577: spsugar = (int *)MALLOC(nsp*sizeof(int));
7578: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7579: nd_sp(0,trace,sp,&spol);
7580: if ( !spol ) continue;
7581: nd_to_vect_q(s0vect,col,spol,svect);
7582: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7583: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7584: if ( i < col ) {
7585: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7586: for ( j = k = 0; j < col; j++ )
7587: if ( !rhead[j] ) v[k++] = svect[j];
7588: spsugar[sprow] = MAX(maxrs,SG(spol));
7589: sprow++;
7590: }
7591: /* nd_free(spol); */
7592: }
7593: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7594: if ( DP_Print ) {
1.5 noro 7595: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7596: fflush(asir_out);
7597: }
7598: /* free index arrays */
7599: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7600:
7601: /* elimination (2nd step) */
7602: colstat = (int *)MALLOC(spcol*sizeof(int));
7603: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7604: w = (pointer *)MALLOC(rank*sizeof(pointer));
7605: for ( i = 0; i < rank; i++ ) {
7606: #if 0
7607: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7608: SG((NDV)w[rank-i-1]) = spsugar[i];
7609: #else
7610: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7611: SG((NDV)w[i]) = spsugar[i];
7612: #endif
7613: /* GCFREE(spmat[i]); */
7614: }
7615: #if 0
7616: qsort(w,rank,sizeof(NDV),
7617: (int (*)(const void *,const void *))ndv_compare);
7618: #endif
7619: r0 = 0;
7620: for ( i = 0; i < rank; i++ ) {
7621: NEXTNODE(r0,r); BDY(r) = w[i];
7622: }
7623: if ( r0 ) NEXT(r) = 0;
7624:
7625: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7626: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7627: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7628: if ( DP_Print ) {
1.5 noro 7629: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7630: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7631: nsp,nred,sprow,spcol,rank);
1.5 noro 7632: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7633: }
7634: return r0;
7635: }
7636:
7637: FILE *nd_write,*nd_read;
7638:
7639: void nd_send_int(int a) {
7640: write_int(nd_write,(unsigned int *)&a);
7641: }
7642:
7643: void nd_send_intarray(int *p,int len) {
7644: write_intarray(nd_write,(unsigned int *)p,len);
7645: }
7646:
7647: int nd_recv_int() {
7648: int a;
7649:
7650: read_int(nd_read,(unsigned int *)&a);
7651: return a;
7652: }
7653:
7654: void nd_recv_intarray(int *p,int len) {
7655: read_intarray(nd_read,(unsigned int *)p,len);
7656: }
7657:
7658: void nd_send_ndv(NDV p) {
7659: int len,i;
7660: NMV m;
7661:
7662: if ( !p ) nd_send_int(0);
7663: else {
7664: len = LEN(p);
7665: nd_send_int(len);
7666: m = BDY(p);
7667: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7668: nd_send_int(CM(m));
7669: nd_send_intarray((int *)DL(m),nd_wpd);
7670: }
7671: }
7672: }
7673:
7674: void nd_send_nd(ND p) {
7675: int len,i;
7676: NM m;
7677:
7678: if ( !p ) nd_send_int(0);
7679: else {
7680: len = LEN(p);
7681: nd_send_int(len);
7682: m = BDY(p);
7683: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7684: nd_send_int(CM(m));
7685: nd_send_intarray((int *)DL(m),nd_wpd);
7686: }
7687: }
7688: }
7689:
7690: NDV nd_recv_ndv()
7691: {
7692: int len,i;
7693: NMV m,m0;
7694: NDV r;
7695:
7696: len = nd_recv_int();
7697: if ( !len ) return 0;
7698: else {
7699: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7700: #if 0
7701: ndv_alloc += len*nmv_adv;
7702: #endif
7703: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7704: CM(m) = nd_recv_int();
7705: nd_recv_intarray((int *)DL(m),nd_wpd);
7706: }
7707: MKNDV(nd_nvar,m0,len,r);
7708: return r;
7709: }
7710: }
7711:
7712: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7713: {
7714: int i,j,t,c,rank,inv;
7715: int *ci,*ri;
7716: Z dn;
7717: MAT m,nm;
7718:
7719: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7720: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7721: for ( i = 0; i < row; i++ )
7722: for ( j = 0; j < col; j++ )
7723: mat0[i][j] = 0;
7724: c = col-rank;
7725: for ( i = 0; i < rank; i++ ) {
7726: mat0[i][ri[i]] = dn;
7727: for ( j = 0; j < c; j++ )
7728: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7729: }
7730: return rank;
7731: }
7732:
7733: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7734: {
7735: int i,j,k,l,inv,a,rank,s;
7736: unsigned int *t,*pivot,*pk;
7737: unsigned int **mat;
7738: ND_pairs pair;
7739:
7740: mat = (unsigned int **)mat0;
7741: for ( rank = 0, j = 0; j < col; j++ ) {
7742: for ( i = rank; i < row; i++ )
7743: mat[i][j] %= md;
7744: for ( i = rank; i < row; i++ )
7745: if ( mat[i][j] )
7746: break;
7747: if ( i == row ) {
7748: colstat[j] = 0;
7749: continue;
7750: } else
7751: colstat[j] = 1;
7752: if ( i != rank ) {
7753: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7754: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7755: if ( spactive ) {
7756: pair = spactive[i]; spactive[i] = spactive[rank];
7757: spactive[rank] = pair;
7758: }
7759: }
7760: pivot = mat[rank];
7761: s = sugar[rank];
7762: inv = invm(pivot[j],md);
7763: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7764: if ( *pk ) {
7765: if ( *pk >= (unsigned int)md )
7766: *pk %= md;
7767: DMAR(*pk,inv,0,md,*pk)
7768: }
7769: for ( i = rank+1; i < row; i++ ) {
7770: t = mat[i];
7771: if ( (a = t[j]) != 0 ) {
7772: sugar[i] = MAX(sugar[i],s);
7773: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7774: }
7775: }
7776: rank++;
7777: }
7778: for ( j = col-1, l = rank-1; j >= 0; j-- )
7779: if ( colstat[j] ) {
7780: pivot = mat[l];
7781: s = sugar[l];
7782: for ( i = 0; i < l; i++ ) {
7783: t = mat[i];
7784: t[j] %= md;
7785: if ( (a = t[j]) != 0 ) {
7786: sugar[i] = MAX(sugar[i],s);
7787: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7788: }
7789: }
7790: l--;
7791: }
7792: for ( j = 0, l = 0; l < rank; j++ )
7793: if ( colstat[j] ) {
7794: t = mat[l];
7795: for ( k = j; k < col; k++ )
7796: if ( t[k] >= (unsigned int)md )
7797: t[k] %= md;
7798: l++;
7799: }
7800: return rank;
7801: }
7802:
7803:
1.7 noro 7804: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7805: {
1.7 noro 7806: int i,j,k,l,inv,a,rank,s;
7807: unsigned int *t,*pivot,*pk;
7808: unsigned int **mat;
7809:
7810: mat = (unsigned int **)mat0;
7811: for ( rank = 0, j = 0; j < col; j++ ) {
7812: for ( i = rank; i < row; i++ )
7813: if ( mat[i][j] )
7814: break;
7815: if ( i == row ) {
7816: colstat[j] = 0;
7817: continue;
7818: } else
7819: colstat[j] = 1;
7820: if ( i != rank ) {
7821: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7822: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7823: }
7824: pivot = mat[rank];
7825: s = sugar[rank];
7826: inv = _invsf(pivot[j]);
7827: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7828: if ( *pk )
7829: *pk = _mulsf(*pk,inv);
7830: for ( i = rank+1; i < row; i++ ) {
7831: t = mat[i];
7832: if ( (a = t[j]) != 0 ) {
7833: sugar[i] = MAX(sugar[i],s);
7834: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7835: }
7836: }
7837: rank++;
7838: }
7839: for ( j = col-1, l = rank-1; j >= 0; j-- )
7840: if ( colstat[j] ) {
7841: pivot = mat[l];
7842: s = sugar[l];
7843: for ( i = 0; i < l; i++ ) {
7844: t = mat[i];
7845: if ( (a = t[j]) != 0 ) {
7846: sugar[i] = MAX(sugar[i],s);
7847: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7848: }
7849: }
7850: l--;
7851: }
7852: return rank;
7853: }
1.1 noro 7854:
1.7 noro 7855: int ndv_ishomo(NDV p)
7856: {
7857: NMV m;
7858: int len,h;
1.1 noro 7859:
7860: if ( !p ) return 1;
7861: len = LEN(p);
7862: m = BDY(p);
7863: h = TD(DL(m));
7864: NMV_ADV(m);
7865: for ( len--; len; len--, NMV_ADV(m) )
7866: if ( TD(DL(m)) != h ) return 0;
7867: return 1;
7868: }
7869:
7870: void ndv_save(NDV p,int index)
7871: {
7872: FILE *s;
7873: char name[BUFSIZ];
7874: short id;
7875: int nv,sugar,len,n,i,td,e,j;
7876: NMV m;
7877: unsigned int *dl;
7878: int mpos;
7879:
7880: sprintf(name,"%s/%d",Demand,index);
7881: s = fopen(name,"w");
7882: savevl(s,0);
7883: if ( !p ) {
7884: saveobj(s,0);
7885: return;
7886: }
7887: id = O_DP;
7888: nv = NV(p);
7889: sugar = SG(p);
7890: len = LEN(p);
7891: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7892: write_int(s,(unsigned int *)&len);
7893:
7894: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7895: saveobj(s,(Obj)CZ(m));
1.1 noro 7896: dl = DL(m);
7897: td = TD(dl);
7898: write_int(s,(unsigned int *)&td);
7899: for ( j = 0; j < nv; j++ ) {
7900: e = GET_EXP(dl,j);
7901: write_int(s,(unsigned int *)&e);
7902: }
7903: if ( nd_module ) {
7904: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7905: }
7906: }
7907: fclose(s);
7908: }
7909:
7910: void nd_save_mod(ND p,int index)
7911: {
7912: FILE *s;
7913: char name[BUFSIZ];
7914: int nv,sugar,len,c;
7915: NM m;
7916:
7917: sprintf(name,"%s/%d",Demand,index);
7918: s = fopen(name,"w");
7919: if ( !p ) {
7920: len = 0;
7921: write_int(s,(unsigned int *)&len);
7922: fclose(s);
7923: return;
7924: }
7925: nv = NV(p);
7926: sugar = SG(p);
7927: len = LEN(p);
7928: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7929: for ( m = BDY(p); m; m = NEXT(m) ) {
7930: c = CM(m); write_int(s,(unsigned int *)&c);
7931: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7932: }
7933: fclose(s);
7934: }
7935:
7936: NDV ndv_load(int index)
7937: {
7938: FILE *s;
7939: char name[BUFSIZ];
7940: short id;
7941: int nv,sugar,len,n,i,td,e,j;
7942: NDV d;
7943: NMV m0,m;
7944: unsigned int *dl;
7945: Obj obj;
7946: int mpos;
7947:
7948: sprintf(name,"%s/%d",Demand,index);
7949: s = fopen(name,"r");
7950: if ( !s ) return 0;
7951:
7952: skipvl(s);
7953: read_short(s,(unsigned short *)&id);
7954: if ( !id ) return 0;
7955: read_int(s,(unsigned int *)&nv);
7956: read_int(s,(unsigned int *)&sugar);
7957: read_int(s,(unsigned int *)&len);
7958:
7959: m0 = m = MALLOC(len*nmv_adv);
7960: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7961: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7962: dl = DL(m);
7963: ndl_zero(dl);
7964: read_int(s,(unsigned int *)&td); TD(dl) = td;
7965: for ( j = 0; j < nv; j++ ) {
7966: read_int(s,(unsigned int *)&e);
7967: PUT_EXP(dl,j,e);
7968: }
7969: if ( nd_module ) {
7970: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7971: }
7972: if ( nd_blockmask ) ndl_weight_mask(dl);
7973: }
7974: fclose(s);
7975: MKNDV(nv,m0,len,d);
7976: SG(d) = sugar;
7977: return d;
7978: }
7979:
7980: ND nd_load_mod(int index)
7981: {
7982: FILE *s;
7983: char name[BUFSIZ];
7984: int nv,sugar,len,i,c;
7985: ND d;
7986: NM m0,m;
7987:
7988: sprintf(name,"%s/%d",Demand,index);
7989: s = fopen(name,"r");
7990: /* if the file does not exist, it means p[index]=0 */
7991: if ( !s ) return 0;
7992:
7993: read_int(s,(unsigned int *)&nv);
7994: if ( !nv ) { fclose(s); return 0; }
7995:
7996: read_int(s,(unsigned int *)&sugar);
7997: read_int(s,(unsigned int *)&len);
7998: for ( m0 = 0, i = 0; i < len; i++ ) {
7999: NEXTNM(m0,m);
8000: read_int(s,(unsigned int *)&c); CM(m) = c;
8001: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
8002: }
8003: NEXT(m) = 0;
8004: MKND(nv,m0,len,d);
8005: SG(d) = sugar;
8006: fclose(s);
8007: return d;
8008: }
8009:
8010: void nd_det(int mod,MAT f,P *rp)
8011: {
8012: VL fv,tv;
8013: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
8014: pointer **m;
8015: P **w;
8016: P mp,r;
8017: NDV **dm;
8018: NDV *t,*mi,*mj;
8019: NDV d,s,mij,mjj;
8020: ND u;
8021: NMV nmv;
8022: UINT *bound;
8023: PGeoBucket bucket;
8024: struct order_spec *ord;
8025: Z dq,dt,ds;
8026: Z mone;
8027: Z gn,qn,dn0,nm,dn;
8028:
8029: create_order_spec(0,0,&ord);
8030: nd_init_ord(ord);
8031: get_vars((Obj)f,&fv);
8032: if ( f->row != f->col )
8033: error("nd_det : non-square matrix");
8034: n = f->row;
8035: m = f->body;
8036: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8037:
8038: if ( !nvar ) {
8039: if ( !mod )
8040: detp(CO,(P **)m,n,rp);
8041: else {
8042: w = (P **)almat_pointer(n,n);
8043: for ( i = 0; i < n; i++ )
8044: for ( j = 0; j < n; j++ )
8045: ptomp(mod,(P)m[i][j],&w[i][j]);
8046: detmp(CO,mod,w,n,&mp);
8047: mptop(mp,rp);
8048: }
8049: return;
8050: }
8051:
8052: if ( !mod ) {
8053: w = (P **)almat_pointer(n,n);
8054: dq = ONE;
8055: for ( i = 0; i < n; i++ ) {
8056: dn0 = ONE;
8057: for ( j = 0; j < n; j++ ) {
8058: if ( !m[i][j] ) continue;
8059: lgp(m[i][j],&nm,&dn);
1.6 noro 8060: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 8061: }
8062: if ( !UNIZ(dn0) ) {
8063: ds = dn0;
8064: for ( j = 0; j < n; j++ )
8065: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8066: mulz(dq,ds,&dt); dq = dt;
8067: } else
8068: for ( j = 0; j < n; j++ )
8069: w[i][j] = (P)m[i][j];
8070: }
8071: m = (pointer **)w;
8072: }
8073:
8074: for ( i = 0, max = 1; i < n; i++ )
8075: for ( j = 0; j < n; j++ )
8076: for ( tv = fv; tv; tv = NEXT(tv) ) {
8077: e = getdeg(tv->v,(P)m[i][j]);
8078: max = MAX(e,max);
8079: }
8080: nd_setup_parameters(nvar,max);
8081: dm = (NDV **)almat_pointer(n,n);
8082: for ( i = 0, max = 1; i < n; i++ )
8083: for ( j = 0; j < n; j++ ) {
8084: dm[i][j] = ptondv(CO,fv,m[i][j]);
8085: if ( mod ) ndv_mod(mod,dm[i][j]);
8086: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8087: }
8088: d = ptondv(CO,fv,(P)ONE);
8089: if ( mod ) ndv_mod(mod,d);
8090: chsgnz(ONE,&mone);
8091: for ( j = 0, sgn = 1; j < n; j++ ) {
8092: if ( DP_Print ) {
8093: fprintf(asir_out,".");
8094: }
8095: for ( i = j; i < n && !dm[i][j]; i++ );
8096: if ( i == n ) {
8097: *rp = 0;
8098: return;
8099: }
8100: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8101: for ( k = j; k < n; k++ )
8102: for ( l = j; l < n; l++ )
8103: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8104: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8105: }
8106: if ( k0 != j ) {
8107: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8108: sgn = -sgn;
8109: }
8110: if ( l0 != j ) {
8111: for ( k = j; k < n; k++ ) {
8112: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8113: }
8114: sgn = -sgn;
8115: }
8116: bound = nd_det_compute_bound(dm,n,j);
8117: for ( k = 0; k < nd_nvar; k++ )
8118: if ( bound[k]*2 > nd_mask0 ) break;
8119: if ( k < nd_nvar )
8120: nd_det_reconstruct(dm,n,j,d);
8121:
8122: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8123: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8124: mi = dm[i]; mij = mi[j];
8125: if ( mod )
8126: ndv_mul_c(mod,mij,mod-1);
8127: else
8128: ndv_mul_c_q(mij,mone);
8129: for ( k = j+1; k < n; k++ ) {
8130: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
8131: bucket = create_pbucket();
8132: if ( mi[k] ) {
8133: nmv = BDY(mjj); len = LEN(mjj);
8134: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8135: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8136: add_pbucket(mod,bucket,u);
8137: }
8138: }
8139: if ( mj[k] && mij ) {
8140: nmv = BDY(mij); len = LEN(mij);
8141: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8142: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8143: add_pbucket(mod,bucket,u);
8144: }
8145: }
8146: u = nd_quo(mod,bucket,d);
8147: mi[k] = ndtondv(mod,u);
8148: }
8149: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
8150: }
8151: d = mjj;
8152: }
8153: if ( DP_Print ) {
8154: fprintf(asir_out,"\n");
8155: }
8156: if ( sgn < 0 ) {
8157: if ( mod )
8158: ndv_mul_c(mod,d,mod-1);
8159: else
8160: ndv_mul_c_q(d,mone);
8161: }
8162: r = ndvtop(mod,CO,fv,d);
8163: if ( !mod && !UNIQ(dq) )
8164: divsp(CO,r,(P)dq,rp);
8165: else
8166: *rp = r;
8167: }
8168:
8169: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8170: {
8171: NM mr,mr0;
8172: NM tnm;
8173: NMV m;
8174: UINT *d0,*dt,*dm;
8175: int c,n,td,i,c1,c2,len;
8176: Z q;
8177: ND r;
8178:
8179: if ( !p ) return 0;
8180: else {
8181: n = NV(p); m = BDY(p); len = LEN(p);
8182: d0 = DL(m0);
8183: td = TD(d);
8184: mr0 = 0;
8185: NEWNM(tnm);
8186: if ( mod ) {
8187: c = CM(m0);
8188: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8189: ndl_add(DL(m),d0,DL(tnm));
8190: if ( ndl_reducible(DL(tnm),d) ) {
8191: NEXTNM(mr0,mr);
8192: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8193: ndl_copy(DL(tnm),DL(mr));
8194: }
8195: }
8196: } else {
1.6 noro 8197: q = CZ(m0);
1.1 noro 8198: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8199: ndl_add(DL(m),d0,DL(tnm));
8200: if ( ndl_reducible(DL(tnm),d) ) {
8201: NEXTNM(mr0,mr);
1.6 noro 8202: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8203: ndl_copy(DL(tnm),DL(mr));
8204: }
8205: }
8206: }
8207: if ( !mr0 )
8208: return 0;
8209: else {
8210: NEXT(mr) = 0;
8211: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8212: MKND(NV(p),mr0,len,r);
8213: SG(r) = SG(p) + TD(d0);
8214: return r;
8215: }
8216: }
8217: }
8218:
8219: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8220: {
8221: int i,obpe,oadv,h,k,l;
8222: static NM prev_nm_free_list;
8223: EPOS oepos;
8224:
8225: obpe = nd_bpe;
8226: oadv = nmv_adv;
8227: oepos = nd_epos;
8228: if ( obpe < 2 ) nd_bpe = 2;
8229: else if ( obpe < 3 ) nd_bpe = 3;
8230: else if ( obpe < 4 ) nd_bpe = 4;
8231: else if ( obpe < 5 ) nd_bpe = 5;
8232: else if ( obpe < 6 ) nd_bpe = 6;
8233: else if ( obpe < 8 ) nd_bpe = 8;
8234: else if ( obpe < 10 ) nd_bpe = 10;
8235: else if ( obpe < 16 ) nd_bpe = 16;
8236: else if ( obpe < 32 ) nd_bpe = 32;
8237: else error("nd_det_reconstruct : exponent too large");
8238:
8239: nd_setup_parameters(nd_nvar,0);
8240: prev_nm_free_list = _nm_free_list;
8241: _nm_free_list = 0;
8242: for ( k = j; k < n; k++ )
8243: for (l = j; l < n; l++ )
8244: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8245: ndv_realloc(d,obpe,oadv,oepos);
8246: prev_nm_free_list = 0;
8247: #if 0
8248: GC_gcollect();
8249: #endif
8250: }
8251:
8252: /* returns a UINT array containing degree bounds */
8253:
8254: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8255: {
8256: UINT *d0,*d1,*d,*t,*r;
8257: int k,l,i;
8258:
8259: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8260: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8261: for ( k = j; k < n; k++ )
8262: for ( l = j; l < n; l++ )
8263: if ( dm[k][l] ) {
8264: d = ndv_compute_bound(dm[k][l]);
8265: for ( i = 0; i < nd_nvar; i++ )
8266: d0[i] = MAX(d0[i],d[i]);
8267: }
8268: return d0;
8269: }
8270:
8271: DL nd_separate_d(UINT *d,UINT *trans)
8272: {
8273: int n,td,i,e,j;
8274: DL a;
8275:
8276: ndl_zero(trans);
8277: td = 0;
8278: for ( i = 0; i < nd_ntrans; i++ ) {
8279: e = GET_EXP(d,i);
8280: PUT_EXP(trans,i,e);
8281: td += MUL_WEIGHT(e,i);
8282: }
8283: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8284: /* homogenized */
8285: i = nd_nvar-1;
8286: e = GET_EXP(d,i);
8287: PUT_EXP(trans,i,e);
8288: td += MUL_WEIGHT(e,i);
8289: }
8290: TD(trans) = td;
8291: if ( nd_blockmask) ndl_weight_mask(trans);
8292: NEWDL(a,nd_nalg);
8293: td = 0;
8294: for ( i = 0; i < nd_nalg; i++ ) {
8295: j = nd_ntrans+i;
8296: e = GET_EXP(d,j);
8297: a->d[i] = e;
8298: td += e;
8299: }
8300: a->td = td;
8301: return a;
8302: }
8303:
8304: int nd_monic(int mod,ND *p)
8305: {
8306: UINT *trans,*t;
8307: DL alg;
8308: MP mp0,mp;
8309: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8310: ND r;
8311: DL dl;
8312: DP nm;
8313: NDV ndv;
8314: DAlg inv,cd;
8315: ND s,c;
8316: Z l,mul;
8317: Z ln;
8318: int n,ntrans,i,e,td,is_lc,len;
8319: NumberField nf;
8320: struct oEGT eg0,eg1;
8321:
8322: if ( !(nf = get_numberfield()) )
8323: error("nd_monic : current_numberfield is not set");
8324:
8325: /* Q coef -> DAlg coef */
8326: NEWNM(ma0); ma = ma0;
8327: m = BDY(*p);
8328: is_lc = 1;
8329: while ( 1 ) {
8330: NEWMP(mp0); mp = mp0;
1.6 noro 8331: mp->c = (Obj)CZ(m);
1.1 noro 8332: mp->dl = nd_separate_d(DL(m),DL(ma));
8333: NEWNM(mb);
8334: for ( m = NEXT(m); m; m = NEXT(m) ) {
8335: alg = nd_separate_d(DL(m),DL(mb));
8336: if ( !ndl_equal(DL(ma),DL(mb)) )
8337: break;
1.6 noro 8338: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8339: }
8340: NEXT(mp) = 0;
8341: MKDP(nd_nalg,mp0,nm);
8342: MKDAlg(nm,ONE,cd);
8343: if ( is_lc == 1 ) {
8344: /* if the lc is a rational number, we have nothing to do */
8345: if ( !mp0->dl->td )
8346: return 1;
8347:
8348: get_eg(&eg0);
8349: invdalg(cd,&inv);
8350: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8351: /* check the validity of inv */
8352: if ( mod && !remqi((Q)inv->dn,mod) )
8353: return 0;
8354: CA(ma) = nf->one;
8355: is_lc = 0;
8356: ln = ONE;
8357: } else {
8358: muldalg(cd,inv,&CA(ma));
8359: lcmz(ln,CA(ma)->dn,&ln);
8360: }
8361: if ( m ) {
8362: NEXT(ma) = mb; ma = mb;
8363: } else {
8364: NEXT(ma) = 0;
8365: break;
8366: }
8367: }
8368: /* l = lcm(denoms) */
8369: l = ln;
8370: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8371: divsz(l,CA(m)->dn,&mul);
1.1 noro 8372: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8373: NEXTNM(mr0,mr);
1.6 noro 8374: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8375: dl = mp->dl;
8376: td = TD(DL(m));
8377: ndl_copy(DL(m),DL(mr));
8378: for ( i = 0; i < nd_nalg; i++ ) {
8379: e = dl->d[i];
8380: PUT_EXP(DL(mr),i+nd_ntrans,e);
8381: td += MUL_WEIGHT(e,i+nd_ntrans);
8382: }
8383: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8384: TD(DL(mr)) = td;
8385: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8386: }
8387: }
8388: NEXT(mr) = 0;
8389: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8390: MKND(NV(*p),mr0,len,r);
8391: /* XXX */
8392: SG(r) = SG(*p);
8393: nd_free(*p);
8394: *p = r;
8395: return 1;
8396: }
8397:
8398: NODE reverse_node(NODE n)
8399: {
8400: NODE t,t1;
8401:
8402: for ( t = 0; n; n = NEXT(n) ) {
8403: MKNODE(t1,BDY(n),t); t = t1;
8404: }
8405: return t;
8406: }
8407:
8408: P ndc_div(int mod,union oNDC a,union oNDC b)
8409: {
8410: union oNDC c;
8411: int inv,t;
8412:
8413: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8414: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8415: else if ( mod ) {
8416: inv = invm(b.m,mod);
8417: DMAR(a.m,inv,0,mod,t); c.m = t;
8418: } else if ( nd_vc )
8419: divsp(nd_vc,a.p,b.p,&c.p);
8420: else
8421: divsz(a.z,b.z,&c.z);
8422: return ndctop(mod,c);
8423: }
8424:
8425: P ndctop(int mod,union oNDC c)
8426: {
8427: Z q;
8428: int e;
8429: GFS gfs;
8430:
8431: if ( mod == -1 ) {
8432: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8433: } else if ( mod == -2 ) {
1.10 noro 8434: q = c.z; return (P)q;
1.1 noro 8435: } else if ( mod > 0 ) {
1.6 noro 8436: STOZ(c.m,q); return (P)q;
1.1 noro 8437: } else
8438: return (P)c.p;
8439: }
8440:
8441: /* [0,0,0,cont] = p -> p/cont */
8442:
8443: void finalize_tracelist(int i,P cont)
8444: {
8445: LIST l;
8446: NODE node;
8447: Z iq;
8448:
8449: if ( !UNIQ(cont) ) {
8450: node = mknode(4,NULLP,NULLP,NULLP,cont);
8451: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8452: nd_tracelist = node;
8453: }
1.6 noro 8454: STOZ(i,iq);
1.1 noro 8455: nd_tracelist = reverse_node(nd_tracelist);
8456: MKLIST(l,nd_tracelist);
8457: node = mknode(2,iq,l); MKLIST(l,node);
8458: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8459: nd_alltracelist = node; nd_tracelist = 0;
8460: }
8461:
8462: void conv_ilist(int demand,int trace,NODE g,int **indp)
8463: {
8464: int n,i,j;
8465: int *ind;
8466: NODE t;
8467:
8468: n = length(g);
8469: ind = (int *)MALLOC(n*sizeof(int));
8470: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8471: j = (long)BDY(t); ind[i] = j;
8472: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8473: }
8474: if ( indp ) *indp = ind;
8475: }
8476:
8477: void parse_nd_option(NODE opt)
8478: {
8479: NODE t,p,u;
8480: int i,s,n;
8481: char *key;
8482: Obj value;
8483:
8484: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8485: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8486: nd_splist = 0; nd_check_splist = 0;
8487: nd_sugarweight = 0;
8488: nd_f4red =0;
8489: nd_rank0 = 0;
8490: for ( t = opt; t; t = NEXT(t) ) {
8491: p = BDY((LIST)BDY(t));
8492: key = BDY((STRING)BDY(p));
8493: value = (Obj)BDY(NEXT(p));
8494: if ( !strcmp(key,"gentrace") )
8495: nd_gentrace = value?1:0;
8496: else if ( !strcmp(key,"gensyz") )
8497: nd_gensyz = value?1:0;
8498: else if ( !strcmp(key,"nora") )
8499: nd_nora = value?1:0;
8500: else if ( !strcmp(key,"gbblock") ) {
8501: if ( value && OID(value) == O_LIST ) {
8502: u = BDY((LIST)value);
8503: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8504: for ( i = 0; u; u = NEXT(u) ) {
8505: p = BDY((LIST)BDY(u));
1.6 noro 8506: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8507: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8508: }
8509: nd_gbblock[i] = -1;
8510: } else
8511: nd_gbblock = 0;
8512: } else if ( !strcmp(key,"newelim") )
8513: nd_newelim = value?1:0;
8514: else if ( !strcmp(key,"intersect") )
8515: nd_intersect = value?1:0;
1.17 noro 8516: else if ( !strcmp(key,"syzgen") )
8517: nd_intersect = ZTOS((Q)value);
1.1 noro 8518: else if ( !strcmp(key,"lf") )
8519: nd_lf = value?1:0;
8520: else if ( !strcmp(key,"trace") ) {
8521: if ( value ) {
8522: u = BDY((LIST)value);
8523: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8524: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8525: }
8526: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8527: nd_f4red = ZTOS((Q)value);
1.1 noro 8528: } else if ( !strcmp(key,"rank0") ) {
8529: nd_rank0 = value?1:0;
8530: } else if ( !strcmp(key,"splist") ) {
8531: nd_splist = value?1:0;
8532: } else if ( !strcmp(key,"check_splist") ) {
8533: nd_check_splist = BDY((LIST)value);
8534: } else if ( !strcmp(key,"sugarweight") ) {
8535: u = BDY((LIST)value);
8536: n = length(u);
8537: nd_sugarweight = MALLOC(n*sizeof(int));
8538: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8539: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8540: }
8541: }
8542: }
8543:
8544: ND mdptond(DP d);
8545: ND nd_mul_nm(int mod,NM m0,ND p);
8546: ND nd_mul_nm_lf(NM m0,ND p);
8547: ND *btog(NODE ti,ND **p,int nb,int mod);
8548: ND btog_one(NODE ti,ND *p,int nb,int mod);
8549: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8550: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8551:
8552: /* d:monomial */
8553: ND mdptond(DP d)
8554: {
8555: NM m;
8556: ND r;
8557:
8558: if ( OID(d) == 1 )
8559: r = ptond(CO,CO,(P)d);
8560: else {
8561: NEWNM(m);
8562: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8563: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8564: NEXT(m) = 0;
8565: MKND(NV(d),m,1,r);
8566: }
8567: return r;
8568: }
8569:
8570: ND nd_mul_nm(int mod,NM m0,ND p)
8571: {
8572: UINT *d0;
8573: int c0,c1,c;
8574: NM tm,mr,mr0;
8575: ND r;
8576:
8577: if ( !p ) return 0;
8578: d0 = DL(m0);
8579: c0 = CM(m0);
8580: mr0 = 0;
8581: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8582: NEXTNM(mr0,mr);
8583: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8584: ndl_add(d0,DL(tm),DL(mr));
8585: }
8586: NEXT(mr) = 0;
8587: MKND(NV(p),mr0,LEN(p),r);
8588: return r;
8589: }
8590:
8591: ND nd_mul_nm_lf(NM m0,ND p)
8592: {
8593: UINT *d0;
8594: Z c0,c1,c;
8595: NM tm,mr,mr0;
8596: ND r;
8597:
8598: if ( !p ) return 0;
8599: d0 = DL(m0);
8600: c0 = CZ(m0);
8601: mr0 = 0;
8602: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8603: NEXTNM(mr0,mr);
8604: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8605: ndl_add(d0,DL(tm),DL(mr));
8606: }
8607: NEXT(mr) = 0;
8608: MKND(NV(p),mr0,LEN(p),r);
8609: return r;
8610: }
8611:
8612: ND *btog(NODE ti,ND **p,int nb,int mod)
8613: {
8614: PGeoBucket *r;
8615: int i,ci;
8616: NODE t,s;
8617: ND m,tp;
8618: ND *pi,*rd;
8619: P c;
8620:
8621: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8622: for ( i = 0; i < nb; i++ )
8623: r[i] = create_pbucket();
8624: for ( t = ti; t; t = NEXT(t) ) {
8625: s = BDY((LIST)BDY(t));
8626: if ( ARG0(s) ) {
8627: m = mdptond((DP)ARG2(s));
1.6 noro 8628: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8629: if ( (ci = ((MQ)c)->cont) != 0 ) {
8630: HCM(m) = ci;
1.6 noro 8631: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8632: for ( i = 0; i < nb; i++ ) {
8633: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8634: add_pbucket(mod,r[i],tp);
8635: }
8636: }
8637: ci = 1;
8638: } else {
8639: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8640: ci = invm(ci,mod);
8641: }
8642: }
8643: rd = (ND *)MALLOC(nb*sizeof(ND));
8644: for ( i = 0; i < nb; i++ )
8645: rd[i] = normalize_pbucket(mod,r[i]);
8646: if ( ci != 1 )
8647: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8648: return rd;
8649: }
8650:
8651: /* YYY */
8652: ND *btog_lf(NODE ti,ND **p,int nb)
8653: {
8654: PGeoBucket *r;
8655: int i;
8656: NODE t,s;
8657: ND m,tp;
8658: ND *pi,*rd;
8659: LM lm;
8660: Z lf,c;
8661:
8662: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8663: for ( i = 0; i < nb; i++ )
8664: r[i] = create_pbucket();
8665: for ( t = ti; t; t = NEXT(t) ) {
8666: s = BDY((LIST)BDY(t));
8667: if ( ARG0(s) ) {
8668: m = mdptond((DP)ARG2(s));
1.6 noro 8669: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8670: if ( lm ) {
8671: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8672: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8673: for ( i = 0; i < nb; i++ ) {
8674: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8675: add_pbucket(-2,r[i],tp);
8676: }
8677: }
8678: c = ONE;
8679: } else {
8680: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8681: }
8682: }
8683: rd = (ND *)MALLOC(nb*sizeof(ND));
8684: for ( i = 0; i < nb; i++ )
8685: rd[i] = normalize_pbucket(-2,r[i]);
8686: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8687: return rd;
8688: }
8689:
8690: ND btog_one(NODE ti,ND *p,int nb,int mod)
8691: {
8692: PGeoBucket r;
8693: int i,ci,j;
8694: NODE t,s;
8695: ND m,tp;
8696: ND pi,rd;
8697: P c;
8698:
8699: r = create_pbucket();
8700: for ( t = ti; t; t = NEXT(t) ) {
8701: s = BDY((LIST)BDY(t));
8702: if ( ARG0(s) ) {
8703: m = mdptond((DP)ARG2(s));
1.6 noro 8704: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8705: if ( (ci = ((MQ)c)->cont) != 0 ) {
8706: HCM(m) = ci;
1.6 noro 8707: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8708: if ( !pi ) {
8709: pi = nd_load_mod(j);
8710: tp = nd_mul_nm(mod,BDY(m),pi);
8711: nd_free(pi);
8712: add_pbucket(mod,r,tp);
8713: } else {
8714: tp = nd_mul_nm(mod,BDY(m),pi);
8715: add_pbucket(mod,r,tp);
8716: }
8717: }
8718: ci = 1;
8719: } else {
8720: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8721: ci = invm(ci,mod);
8722: }
8723: }
8724: rd = normalize_pbucket(mod,r);
8725: free_pbucket(r);
8726: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8727: return rd;
8728: }
8729:
8730: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8731:
8732: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8733: {
8734: int i,j,n,m,nb,pi0,pi1,nvar;
8735: VL fv,tv,vv;
8736: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8737: ND **p;
8738: ND *c;
8739: ND u;
8740: P inv;
8741: MAT mat;
8742:
8743: if ( mod == -2 )
8744: return nd_btog_lf(f,v,ord,tlist,rp);
8745:
8746: parse_nd_option(current_option);
8747: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8748: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8749: switch ( ord->id ) {
8750: case 1:
8751: if ( ord->nv != nvar )
8752: error("nd_check : invalid order specification");
8753: break;
8754: default:
8755: break;
8756: }
8757: nd_init_ord(ord);
8758: #if 0
1.6 noro 8759: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8760: #else
8761: nd_bpe = 32;
8762: #endif
8763: nd_setup_parameters(nvar,0);
8764: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8765: intred = BDY((LIST)ARG3(BDY(tlist)));
8766: ind = BDY((LIST)ARG4(BDY(tlist)));
8767: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8768: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8769: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8770: if ( j > i ) i = j;
8771: }
8772: n = i+1;
8773: nb = length(BDY(f));
8774: p = (ND **)MALLOC(n*sizeof(ND *));
8775: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8776: pi = BDY((LIST)BDY(t));
1.6 noro 8777: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8778: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8779: ptomp(mod,(P)ARG2(pi),&inv);
8780: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8781: u = ptond(CO,vv,(P)ONE);
8782: HCM(u) = ((MQ)inv)->cont;
8783: c[pi1] = u;
8784: }
8785: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8786: printf("%d ",i); fflush(stdout);
8787: ti = BDY((LIST)BDY(t));
1.6 noro 8788: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8789: }
8790: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8791: printf("%d ",i); fflush(stdout);
8792: ti = BDY((LIST)BDY(t));
1.6 noro 8793: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8794: }
8795: m = length(ind);
8796: MKMAT(mat,nb,m);
8797: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8798: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8799: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8800: return mat;
8801: }
8802:
8803: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8804: {
8805: int i,j,n,m,nb,pi0,pi1,nvar;
8806: VL fv,tv,vv;
8807: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8808: ND **p;
8809: ND *c;
8810: ND u;
8811: MAT mat;
8812: LM lm;
8813: Z lf,inv;
8814:
8815: parse_nd_option(current_option);
8816: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8817: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8818: switch ( ord->id ) {
8819: case 1:
8820: if ( ord->nv != nvar )
8821: error("nd_check : invalid order specification");
8822: break;
8823: default:
8824: break;
8825: }
8826: nd_init_ord(ord);
8827: #if 0
1.6 noro 8828: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8829: #else
8830: nd_bpe = 32;
8831: #endif
8832: nd_setup_parameters(nvar,0);
8833: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8834: intred = BDY((LIST)ARG3(BDY(tlist)));
8835: ind = BDY((LIST)ARG4(BDY(tlist)));
8836: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8837: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8838: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8839: if ( j > i ) i = j;
8840: }
8841: n = i+1;
8842: nb = length(BDY(f));
8843: p = (ND **)MALLOC(n*sizeof(ND *));
8844: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8845: pi = BDY((LIST)BDY(t));
1.6 noro 8846: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8847: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8848: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8849: u = ptond(CO,vv,(P)ONE);
8850: HCZ(u) = inv;
8851: c[pi1] = u;
8852: }
8853: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8854: printf("%d ",i); fflush(stdout);
8855: ti = BDY((LIST)BDY(t));
1.6 noro 8856: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8857: }
8858: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8859: printf("%d ",i); fflush(stdout);
8860: ti = BDY((LIST)BDY(t));
1.6 noro 8861: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8862: }
8863: m = length(ind);
8864: MKMAT(mat,nb,m);
8865: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8866: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8867: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8868: return mat;
8869: }
8870:
8871: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8872: LIST tlist,int pos,MAT *rp)
8873: {
8874: int i,j,n,m,nb,pi0,pi1,nvar;
8875: VL fv,tv,vv;
8876: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8877: ND *p;
8878: ND *c;
8879: ND u;
8880: P inv;
8881: VECT vect;
8882:
8883: if ( mod == -2 )
8884: error("nd_btog_one : not implemented yet for a large finite field");
8885:
8886: parse_nd_option(current_option);
8887: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8888: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8889: switch ( ord->id ) {
8890: case 1:
8891: if ( ord->nv != nvar )
8892: error("nd_check : invalid order specification");
8893: break;
8894: default:
8895: break;
8896: }
8897: nd_init_ord(ord);
8898: #if 0
1.6 noro 8899: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8900: #else
8901: nd_bpe = 32;
8902: #endif
8903: nd_setup_parameters(nvar,0);
8904: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8905: intred = BDY((LIST)ARG3(BDY(tlist)));
8906: ind = BDY((LIST)ARG4(BDY(tlist)));
8907: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8908: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8909: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8910: if ( j > i ) i = j;
8911: }
8912: n = i+1;
8913: nb = length(BDY(f));
8914: p = (ND *)MALLOC(n*sizeof(ND *));
8915: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8916: pi = BDY((LIST)BDY(t));
1.6 noro 8917: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8918: if ( pi1 == pos ) {
8919: ptomp(mod,(P)ARG2(pi),&inv);
8920: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8921: u = ptond(CO,vv,(P)ONE);
8922: HCM(u) = ((MQ)inv)->cont;
8923: p[pi0] = u;
8924: }
8925: }
8926: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8927: printf("%d ",i); fflush(stdout);
8928: ti = BDY((LIST)BDY(t));
1.6 noro 8929: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8930: if ( Demand ) {
8931: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8932: }
8933: }
8934: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8935: printf("%d ",i); fflush(stdout);
8936: ti = BDY((LIST)BDY(t));
1.6 noro 8937: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8938: if ( Demand ) {
8939: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8940: }
8941: }
8942: m = length(ind);
8943: MKVECT(vect,m);
8944: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8945: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8946: if ( !u ) {
1.6 noro 8947: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8948: BDY(vect)[j] = ndtodp(mod,u);
8949: nd_free(u);
8950: } else
8951: BDY(vect)[j] = ndtodp(mod,u);
8952: }
8953: return vect;
8954: }
8955:
8956: void ndv_print_lf(NDV p)
8957: {
8958: NMV m;
8959: int i,len;
8960:
8961: if ( !p ) printf("0\n");
8962: else {
8963: len = LEN(p);
8964: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8965: printf("+");
8966: mpz_out_str(asir_out,10,BDY(CZ(m)));
8967: printf("*");
8968: ndl_print(DL(m));
8969: }
8970: printf("\n");
8971: }
8972: }
8973:
8974: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8975: {
8976: VL tv,fv,vv,vc,av;
8977: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8978: int m,nocheck,nvar,mindex,e,max;
8979: NDV c;
8980: NMV a;
8981: P p,zp;
8982: Q dmy;
8983: EPOS oepos;
8984: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8985: Alg alpha,dp;
8986: P poly;
8987: LIST f1,f2,zpl;
8988: Obj obj;
8989: NumberField nf;
8990: struct order_spec *ord1;
8991: struct oEGT eg_check,eg0,eg1;
8992: NODE tr,tl1,tl2,tl3,tl4;
8993: LIST l1,l2,l3,l4,l5;
8994: int *perm;
8995: int j,ret;
8996: NODE retn;
8997: Q jq,bpe;
8998:
8999: nd_module = 0;
9000: parse_nd_option(current_option);
9001: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
9002: if ( nd_vc )
9003: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
9004: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
9005: switch ( ord->id ) {
9006: case 1:
9007: if ( ord->nv != nvar )
9008: error("nd_f4_lf_trace : invalid order specification");
9009: break;
9010: default:
9011: break;
9012: }
9013:
9014: nd_ntrans = nvar;
9015: nd_nalg = 0;
9016:
9017: nocheck = 0;
9018: mindex = 0;
9019:
9020: /* do not use on-demand load/save */
9021: nd_demand = 0;
9022: m = trace > 1 ? trace : get_lprime(mindex);
9023: nd_init_ord(ord);
9024: mrank = 0;
9025: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9026: for ( tv = vv; tv; tv = NEXT(tv) ) {
9027: if ( nd_module ) {
9028: s = BDY((LIST)BDY(t));
9029: trank = length(s);
9030: mrank = MAX(mrank,trank);
9031: for ( ; s; s = NEXT(s) ) {
9032: e = getdeg(tv->v,(P)BDY(s));
9033: max = MAX(e,max);
9034: }
9035: } else {
9036: e = getdeg(tv->v,(P)BDY(t));
9037: max = MAX(e,max);
9038: }
9039: }
9040: nd_setup_parameters(nvar,max);
9041: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9042: ishomo = 1;
9043: /* XXX */
9044: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9045: if ( nd_module ) {
9046: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9047: } else {
9048: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9049: }
9050: if ( ishomo )
9051: ishomo = ishomo && ndv_ishomo(c);
9052: if ( c ) {
9053: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9054: ndv_mod(-2,c);
9055: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9056: }
9057: }
9058: if ( in0 ) NEXT(in) = 0;
9059: if ( fd0 ) NEXT(fd) = 0;
9060: if ( !ishomo && homo ) {
9061: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9062: c = (NDV)BDY(t); len = LEN(c);
9063: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9064: wmax = MAX(TD(DL(a)),wmax);
9065: }
9066: homogenize_order(ord,nvar,&ord1);
9067: nd_init_ord(ord1);
9068: nd_setup_parameters(nvar+1,wmax);
9069: for ( t = fd0; t; t = NEXT(t) )
9070: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9071: }
9072: if ( MaxDeg > 0 ) nocheck = 1;
9073: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9074: if ( ret )
9075: cand = nd_f4_lf_trace_main(m,&perm);
9076: if ( !ret || !cand ) {
9077: *rp = 0; return;
9078: }
9079: if ( !ishomo && homo ) {
9080: /* dehomogenization */
9081: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9082: nd_init_ord(ord);
9083: nd_setup_parameters(nvar,0);
9084: }
9085: cand = ndv_reducebase(cand,perm);
9086: cand = ndv_reduceall(-2,cand);
9087: cbpe = nd_bpe;
9088: get_eg(&eg0);
9089: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9090: /* gbcheck : cand is a GB of Id(cand) ? */
9091: retn = nd_f4(-2,0,0);
9092: }
9093: if ( !retn ) {
9094: /* failure */
9095: *rp = 0; return;
9096: }
9097: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9098: if ( DP_Print )
1.5 noro 9099: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 9100: /* dp->p */
9101: nd_bpe = cbpe;
9102: nd_setup_parameters(nd_nvar,0);
9103: for ( r = cand; r; r = NEXT(r) ) {
9104: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9105: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9106: }
9107: MKLIST(*rp,cand);
9108: }
9109:
9110: NODE nd_f4_lf_trace_main(int m,int **indp)
9111: {
9112: int i,nh,stat,index;
9113: NODE r,rm,g;
9114: ND_pairs d,l,l0,t;
9115: ND spol,red;
9116: NDV nf,redv,nfqv,nfv;
9117: NM s0,s;
9118: NODE rp0,srp0,nflist,nflist_lf;
9119: int nsp,nred,col,rank,len,k,j,a;
9120: UINT c;
9121: UINT **spmat;
9122: UINT *s0vect,*svect,*p,*v;
9123: int *colstat;
9124: IndArray *imat;
9125: int *rhead;
9126: int spcol,sprow;
9127: int sugar;
9128: PGeoBucket bucket;
9129: struct oEGT eg0,eg1,eg_f4;
9130:
9131: g = 0; d = 0;
9132: for ( i = 0; i < nd_psn; i++ ) {
9133: d = update_pairs(d,g,i,0);
9134: g = update_base(g,i);
9135: }
9136: while ( d ) {
9137: get_eg(&eg0);
9138: l = nd_minsugarp(d,&d);
9139: sugar = SG(l);
9140: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9141: bucket = create_pbucket();
9142: stat = nd_sp_f4(m,0,l,bucket);
9143: if ( !stat ) {
9144: for ( t = l; NEXT(t); t = NEXT(t) );
9145: NEXT(t) = d; d = l;
9146: d = nd_reconstruct(1,d);
9147: continue;
9148: }
9149: if ( bucket->m < 0 ) continue;
9150: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9151: if ( !col ) {
9152: for ( t = l; NEXT(t); t = NEXT(t) );
9153: NEXT(t) = d; d = l;
9154: d = nd_reconstruct(1,d);
9155: continue;
9156: }
9157: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9158: if ( DP_Print )
1.5 noro 9159: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 9160: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9161: if ( !l0 ) continue;
9162: l = l0;
9163:
9164: /* over LF */
9165: bucket = create_pbucket();
9166: stat = nd_sp_f4(-2,1,l,bucket);
9167: if ( !stat ) {
9168: for ( t = l; NEXT(t); t = NEXT(t) );
9169: NEXT(t) = d; d = l;
9170: d = nd_reconstruct(1,d);
9171: continue;
9172: }
9173: if ( bucket->m < 0 ) continue;
9174: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
9175: if ( !col ) {
9176: for ( t = l; NEXT(t); t = NEXT(t) );
9177: NEXT(t) = d; d = l;
9178: d = nd_reconstruct(1,d);
9179: continue;
9180: }
9181: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9182: /* adding new bases */
9183: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9184: nfv = (NDV)BDY(rm);
9185: nfqv = (NDV)BDY(r);
9186: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9187: ndv_removecont(m,nfv);
9188: ndv_removecont(-2,nfqv);
9189: nh = ndv_newps(-2,nfv,nfqv,1);
9190: d = update_pairs(d,g,nh,0);
9191: g = update_base(g,nh);
9192: }
9193: if ( r || rm ) return 0;
9194: }
9195: conv_ilist(nd_demand,1,g,indp);
9196: return g;
9197: }
9198:
1.7 noro 9199: #if SIZEOF_LONG==8
9200:
9201: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9202: {
9203: int j,k,len;
9204: UINT *p;
9205: UINT c;
9206: NDV r;
9207: NMV mr0,mr;
9208:
9209: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9210: if ( !len ) return 0;
9211: else {
9212: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9213: #if 0
9214: ndv_alloc += nmv_adv*len;
9215: #endif
9216: mr = mr0;
9217: p = s0vect;
9218: for ( j = k = 0; j < col; j++, p += nd_wpd )
9219: if ( !rhead[j] ) {
9220: if ( (c = (UINT)vect[k++]) != 0 ) {
9221: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9222: }
9223: }
9224: MKNDV(nd_nvar,mr0,len,r);
9225: return r;
9226: }
9227: }
9228:
9229: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9230: {
9231: NM m;
1.11 noro 9232: UINT *t,*s,*u;
9233: int i,st,ed,md,prev,c;
1.7 noro 9234:
9235: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9236: prev = 0;
9237: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9238: t = DL(m);
9239: st = prev;
9240: ed = n;
9241: while ( ed > st ) {
9242: md = (st+ed)/2;
9243: u = s0+md*nd_wpd;
9244: c = DL_COMPARE(u,t);
9245: if ( c == 0 ) break;
9246: else if ( c > 0 ) st = md;
9247: else ed = md;
9248: }
9249: r[md] = (mp_limb_t)CM(m);
9250: prev = md;
1.7 noro 9251: }
9252: for ( i = 0; !r[i]; i++ );
9253: return i;
9254: }
9255:
9256: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9257:
9258: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9259: {
9260: int i,j,k,len,pos,prev;
9261: mp_limb_t a,c,c1,c2;
9262: IndArray ivect;
9263: unsigned char *ivc;
9264: unsigned short *ivs;
9265: unsigned int *ivi;
9266: NDV redv;
9267: NMV mr;
9268: NODE rp;
9269: int maxrs;
9270:
9271: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9272: maxrs = 0;
9273: for ( i = 0; i < nred; i++ ) {
9274: ivect = imat[i];
9275: k = ivect->head;
9276: a = svect[k]; c = cvect[k];
9277: MOD128(a,c,m);
9278: svect[k] = a; cvect[k] = 0;
9279: if ( (c = svect[k]) != 0 ) {
1.11 noro 9280: Nf4_red++;
1.7 noro 9281: maxrs = MAX(maxrs,rp0[i]->sugar);
9282: c = m-c; redv = nd_ps[rp0[i]->index];
9283: len = LEN(redv); mr = BDY(redv);
9284: svect[k] = 0; prev = k;
9285: switch ( ivect->width ) {
9286: case 1:
9287: ivc = ivect->index.c;
9288: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9289: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 9290: c2 = svect[pos]+c1*c;
9291: if ( c2 < svect[pos] ) cvect[pos]++;
9292: svect[pos] = c2;
1.7 noro 9293: }
9294: break;
9295: case 2:
9296: ivs = ivect->index.s;
9297: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9298: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 9299: c2 = svect[pos]+c1*c;
9300: if ( c2 < svect[pos] ) cvect[pos]++;
9301: svect[pos] = c2;
1.7 noro 9302: }
9303: break;
9304: case 4:
9305: ivi = ivect->index.i;
9306: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9307: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 9308: c2 = svect[pos]+c1*c;
9309: if ( c2 < svect[pos] ) cvect[pos]++;
9310: svect[pos] = c2;
1.7 noro 9311: }
9312: break;
9313: }
9314: }
9315: }
9316: for ( i = 0; i < col; i++ ) {
9317: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9318: }
9319: return maxrs;
9320: }
9321:
9322: /* for Fp, 2^15=<p<2^29 */
9323:
9324: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9325: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9326: {
9327: int spcol,sprow,a;
9328: int i,j,k,l,rank;
9329: NODE r0,r;
9330: ND_pairs sp;
9331: ND spol;
9332: mp_limb_t **spmat;
9333: mp_limb_t *svect,*cvect;
9334: mp_limb_t *v;
9335: int *colstat;
9336: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9337: int maxrs;
9338: int *spsugar;
9339: ND_pairs *spactive;
9340:
9341: spcol = col-nred;
9342: get_eg(&eg0);
9343: /* elimination (1st step) */
9344: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9345: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9346: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9347: spsugar = (int *)MALLOC(nsp*sizeof(int));
9348: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9349: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9350: nd_sp(m,0,sp,&spol);
9351: if ( !spol ) continue;
9352: nd_to_vect64(m,s0vect,col,spol,svect);
9353: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9354: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9355: if ( i < col ) {
9356: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9357: for ( j = k = 0; j < col; j++ )
9358: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9359: spsugar[sprow] = MAX(maxrs,SG(spol));
9360: if ( nz )
9361: spactive[sprow] = sp;
9362: sprow++;
9363: }
9364: nd_free(spol);
9365: }
1.12 noro 9366: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9367: if ( DP_Print ) {
9368: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9369: fflush(asir_out);
9370: }
9371: /* free index arrays */
9372: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9373:
9374: /* elimination (2nd step) */
9375: colstat = (int *)MALLOC(spcol*sizeof(int));
9376: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9377: r0 = 0;
9378: for ( i = 0; i < rank; i++ ) {
9379: NEXTNODE(r0,r); BDY(r) =
9380: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9381: SG((NDV)BDY(r)) = spsugar[i];
9382: GCFREE(spmat[i]);
9383: }
9384: if ( r0 ) NEXT(r) = 0;
9385:
9386: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 9387: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9388: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9389: if ( DP_Print ) {
9390: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9391: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9392: nsp,nred,sprow,spcol,rank);
9393: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9394: }
9395: if ( nz ) {
9396: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9397: if ( rank > 0 ) {
9398: NEXT(spactive[rank-1]) = 0;
9399: *nz = spactive[0];
9400: } else
9401: *nz = 0;
9402: }
9403: return r0;
9404: }
9405:
9406: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9407: {
9408: int i,j,k,l,rank,s;
9409: mp_limb_t inv;
9410: mp_limb_t a;
9411: UINT c;
9412: mp_limb_t *t,*pivot,*pk;
9413: UINT *ck;
9414: UINT **cmat;
9415: UINT *ct;
9416: ND_pairs pair;
9417:
9418: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9419: for ( i = 0; i < row; i++ ) {
9420: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9421: bzero(cmat[i],col*sizeof(UINT));
9422: }
9423:
9424: for ( rank = 0, j = 0; j < col; j++ ) {
9425: for ( i = rank; i < row; i++ ) {
9426: a = mat[i][j]; c = cmat[i][j];
9427: MOD128(a,c,md);
9428: mat[i][j] = a; cmat[i][j] = 0;
9429: }
9430: for ( i = rank; i < row; i++ )
9431: if ( mat[i][j] )
9432: break;
9433: if ( i == row ) {
9434: colstat[j] = 0;
9435: continue;
9436: } else
9437: colstat[j] = 1;
9438: if ( i != rank ) {
9439: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9440: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9441: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9442: if ( spactive ) {
9443: pair = spactive[i]; spactive[i] = spactive[rank];
9444: spactive[rank] = pair;
9445: }
9446: }
9447: /* column j is normalized */
9448: s = sugar[rank];
9449: inv = invm((UINT)mat[rank][j],md);
9450: /* normalize pivot row */
9451: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9452: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9453: }
9454: for ( i = rank+1; i < row; i++ ) {
9455: if ( (a = mat[i][j]) != 0 ) {
9456: sugar[i] = MAX(sugar[i],s);
9457: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9458: Nf4_red++;
1.7 noro 9459: }
9460: }
9461: rank++;
9462: }
9463: for ( j = col-1, l = rank-1; j >= 0; j-- )
9464: if ( colstat[j] ) {
9465: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9466: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9467: }
9468: s = sugar[l];
9469: for ( i = 0; i < l; i++ ) {
9470: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9471: if ( a ) {
9472: sugar[i] = MAX(sugar[i],s);
9473: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9474: Nf4_red++;
1.7 noro 9475: }
9476: }
9477: l--;
9478: }
9479: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9480: GCFREE(cmat);
9481: return rank;
9482: }
9483: #endif
9484:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>