Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.16
1.16 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.15 2019/04/20 06:04:18 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);
2614: if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
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-- )
3373: if ( MPOS(DL(nd_psh[j])) > 1 ) {
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:
! 3781: if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
! 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 ) {
! 3890: if ( retdp ) BDY(r) = ndvtodpm(0,BDY(t));
! 3891: else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
! 3892: } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(t));
! 3893: else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(t));
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:
! 5517: NDV dpmtondv(int mod,DPM p)
! 5518: {
! 5519: NDV d;
! 5520: NMV m,m0;
! 5521: DMM t;
! 5522: DMM *a;
! 5523: int i,len,n;
! 5524:
! 5525: if ( !p ) return 0;
! 5526: for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
! 5527: a = (DMM *)MALLOC(len*sizeof(DMM));
! 5528: for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
! 5529: n = p->nv;
! 5530: dmm_sort_by_ord(a,len,n);
! 5531: if ( mod > 0 || mod == -1 )
! 5532: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
! 5533: else
! 5534: m0 = m = MALLOC(len*nmv_adv);
! 5535: #if 0
! 5536: ndv_alloc += nmv_adv*len;
! 5537: #endif
! 5538: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
! 5539: dltondl(n,a[i]->dl,DL(m));
! 5540: MPOS(DL(m)) = a[i]->pos;
! 5541: CZ(m) = (Z)a[i]->c;
! 5542: }
! 5543: MKNDV(NV(p),m0,len,d);
! 5544: SG(d) = SG(p);
! 5545: return d;
! 5546: }
! 5547:
1.1 noro 5548: ND ndvtond(int mod,NDV p)
5549: {
5550: ND d;
5551: NM m,m0;
5552: NMV t;
5553: int i,len;
5554:
5555: if ( !p ) return 0;
5556: m0 = 0;
5557: len = p->len;
5558: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5559: NEXTNM(m0,m);
5560: ndl_copy(DL(t),DL(m));
1.6 noro 5561: CZ(m) = CZ(t);
1.1 noro 5562: }
5563: NEXT(m) = 0;
5564: MKND(NV(p),m0,len,d);
5565: SG(d) = SG(p);
5566: return d;
5567: }
5568:
5569: DP ndvtodp(int mod,NDV p)
5570: {
5571: MP m,m0;
5572: DP d;
5573: NMV t;
5574: int i,len;
5575:
5576: if ( !p ) return 0;
5577: m0 = 0;
5578: len = p->len;
5579: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
5580: NEXTMP(m0,m);
5581: m->dl = ndltodl(nd_nvar,DL(t));
5582: m->c = (Obj)ndctop(mod,t->c);
5583: }
5584: NEXT(m) = 0;
5585: MKDP(nd_nvar,m0,d);
5586: SG(d) = SG(p);
5587: return d;
5588: }
5589:
1.16 ! noro 5590: DPM ndvtodpm(int mod,NDV p)
! 5591: {
! 5592: DMM m,m0;
! 5593: DPM d;
! 5594: NMV t;
! 5595: int i,len;
! 5596:
! 5597: if ( !p ) return 0;
! 5598: m0 = 0;
! 5599: len = p->len;
! 5600: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
! 5601: NEXTDMM(m0,m);
! 5602: m->dl = ndltodl(nd_nvar,DL(t));
! 5603: m->c = (Obj)ndctop(mod,t->c);
! 5604: m->pos = MPOS(DL(t));
! 5605: }
! 5606: NEXT(m) = 0;
! 5607: MKDPM(nd_nvar,m0,d);
! 5608: SG(d) = SG(p);
! 5609: return d;
! 5610: }
! 5611:
! 5612:
1.1 noro 5613: DP ndtodp(int mod,ND p)
5614: {
5615: MP m,m0;
5616: DP d;
5617: NM t;
5618: int i,len;
5619:
5620: if ( !p ) return 0;
5621: m0 = 0;
5622: len = p->len;
5623: for ( t = BDY(p); t; t = NEXT(t) ) {
5624: NEXTMP(m0,m);
5625: m->dl = ndltodl(nd_nvar,DL(t));
5626: m->c = (Obj)ndctop(mod,t->c);
5627: }
5628: NEXT(m) = 0;
5629: MKDP(nd_nvar,m0,d);
5630: SG(d) = SG(p);
5631: return d;
5632: }
5633:
5634: void ndv_print(NDV p)
5635: {
5636: NMV m;
5637: int i,len;
5638:
5639: if ( !p ) printf("0\n");
5640: else {
5641: len = LEN(p);
5642: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5643: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
5644: else printf("+%d*",CM(m));
5645: ndl_print(DL(m));
5646: }
5647: printf("\n");
5648: }
5649: }
5650:
5651: void ndv_print_q(NDV p)
5652: {
5653: NMV m;
5654: int i,len;
5655:
5656: if ( !p ) printf("0\n");
5657: else {
5658: len = LEN(p);
5659: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
5660: printf("+");
1.6 noro 5661: printexpr(CO,(Obj)CZ(m));
1.1 noro 5662: printf("*");
5663: ndl_print(DL(m));
5664: }
5665: printf("\n");
5666: }
5667: }
5668:
5669: NODE ndv_reducebase(NODE x,int *perm)
5670: {
5671: int len,i,j;
5672: NDVI w;
5673: NODE t,t0;
5674:
5675: len = length(x);
5676: w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
5677: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
5678: w[i].p = BDY(t); w[i].i = perm[i];
5679: }
5680: for ( i = 0; i < len; i++ ) {
5681: for ( j = 0; j < i; j++ ) {
5682: if ( w[i].p && w[j].p ) {
5683: if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
5684: else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
5685: }
5686: }
5687: }
5688: for ( i = j = 0, t0 = 0; i < len; i++ ) {
5689: if ( w[i].p ) {
5690: NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
5691: perm[j++] = w[i].i;
5692: }
5693: }
5694: NEXT(t) = 0; x = t0;
5695: return x;
5696: }
5697:
5698: /* XXX incomplete */
5699:
1.16 ! noro 5700: extern int dpm_ordtype;
! 5701:
1.1 noro 5702: void nd_init_ord(struct order_spec *ord)
5703: {
5704: nd_module = (ord->id >= 256);
5705: if ( nd_module ) {
5706: nd_dcomp = -1;
5707: nd_ispot = ord->ispot;
5708: nd_pot_nelim = ord->pot_nelim;
5709: nd_poly_weight_len = ord->nv;
5710: nd_poly_weight = ord->top_weight;
5711: nd_module_rank = ord->module_rank;
5712: nd_module_weight = ord->module_top_weight;
1.16 ! noro 5713: dpm_ordtype = ord->ispot;
1.1 noro 5714: }
5715: nd_matrix = 0;
5716: nd_matrix_len = 0;
5717: switch ( ord->id ) {
5718: case 0:
5719: switch ( ord->ord.simple ) {
5720: case 0:
5721: nd_dcomp = 1;
5722: nd_isrlex = 1;
5723: break;
5724: case 1:
5725: nd_dcomp = 1;
5726: nd_isrlex = 0;
5727: break;
5728: case 2:
5729: nd_dcomp = 0;
5730: nd_isrlex = 0;
5731: ndl_compare_function = ndl_lex_compare;
5732: break;
5733: case 11:
5734: /* XXX */
5735: nd_dcomp = 0;
5736: nd_isrlex = 1;
5737: ndl_compare_function = ndl_ww_lex_compare;
5738: break;
5739: default:
5740: error("nd_gr : unsupported order");
5741: }
5742: break;
5743: case 1:
5744: /* block order */
5745: /* XXX */
5746: nd_dcomp = -1;
5747: nd_isrlex = 0;
5748: ndl_compare_function = ndl_block_compare;
5749: break;
5750: case 2:
5751: /* matrix order */
5752: /* XXX */
5753: nd_dcomp = -1;
5754: nd_isrlex = 0;
5755: nd_matrix_len = ord->ord.matrix.row;
5756: nd_matrix = ord->ord.matrix.matrix;
5757: ndl_compare_function = ndl_matrix_compare;
5758: break;
5759: case 3:
5760: /* composite order */
5761: nd_dcomp = -1;
5762: nd_isrlex = 0;
5763: nd_worb_len = ord->ord.composite.length;
5764: nd_worb = ord->ord.composite.w_or_b;
5765: ndl_compare_function = ndl_composite_compare;
5766: break;
5767:
5768: /* module order */
5769: case 256:
5770: switch ( ord->ord.simple ) {
5771: case 0:
5772: nd_isrlex = 1;
5773: ndl_compare_function = ndl_module_grlex_compare;
5774: break;
5775: case 1:
5776: nd_isrlex = 0;
5777: ndl_compare_function = ndl_module_glex_compare;
5778: break;
5779: case 2:
5780: nd_isrlex = 0;
5781: ndl_compare_function = ndl_module_lex_compare;
5782: break;
5783: default:
5784: error("nd_gr : unsupported order");
5785: }
5786: break;
5787: case 257:
5788: /* block order */
5789: nd_isrlex = 0;
5790: ndl_compare_function = ndl_module_block_compare;
5791: break;
5792: case 258:
5793: /* matrix order */
5794: nd_isrlex = 0;
5795: nd_matrix_len = ord->ord.matrix.row;
5796: nd_matrix = ord->ord.matrix.matrix;
5797: ndl_compare_function = ndl_module_matrix_compare;
5798: break;
5799: case 259:
5800: /* composite order */
5801: nd_isrlex = 0;
5802: nd_worb_len = ord->ord.composite.length;
5803: nd_worb = ord->ord.composite.w_or_b;
5804: ndl_compare_function = ndl_module_composite_compare;
5805: break;
5806: }
5807: nd_ord = ord;
5808: }
5809:
5810: BlockMask nd_create_blockmask(struct order_spec *ord)
5811: {
5812: int n,i,j,s,l;
5813: UINT *t;
5814: BlockMask bm;
5815:
5816: /* we only create mask table for block order */
5817: if ( ord->id != 1 && ord->id != 257 )
5818: return 0;
5819: n = ord->ord.block.length;
5820: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
5821: bm->n = n;
5822: bm->order_pair = ord->ord.block.order_pair;
5823: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
5824: for ( i = 0, s = 0; i < n; i++ ) {
5825: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
5826: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
5827: l = bm->order_pair[i].length;
5828: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
5829: }
5830: return bm;
5831: }
5832:
5833: EPOS nd_create_epos(struct order_spec *ord)
5834: {
5835: int i,j,l,s,ord_l,ord_o;
5836: EPOS epos;
5837: struct order_pair *op;
5838:
5839: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
5840: switch ( ord->id ) {
5841: case 0: case 256:
5842: if ( nd_isrlex ) {
5843: for ( i = 0; i < nd_nvar; i++ ) {
5844: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
5845: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
5846: }
5847: } else {
5848: for ( i = 0; i < nd_nvar; i++ ) {
5849: epos[i].i = nd_exporigin + i/nd_epw;
5850: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5851: }
5852: }
5853: break;
5854: case 1: case 257:
5855: /* block order */
5856: l = ord->ord.block.length;
5857: op = ord->ord.block.order_pair;
5858: for ( j = 0, s = 0; j < l; j++ ) {
5859: ord_o = op[j].order;
5860: ord_l = op[j].length;
5861: if ( !ord_o )
5862: for ( i = 0; i < ord_l; i++ ) {
5863: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
5864: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
5865: }
5866: else
5867: for ( i = 0; i < ord_l; i++ ) {
5868: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
5869: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
5870: }
5871: s += ord_l;
5872: }
5873: break;
5874: case 2:
5875: /* matrix order */
5876: case 3:
5877: /* composite order */
5878: default:
5879: for ( i = 0; i < nd_nvar; i++ ) {
5880: epos[i].i = nd_exporigin + i/nd_epw;
5881: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
5882: }
5883: break;
5884: }
5885: return epos;
5886: }
5887:
5888: /* external interface */
5889:
5890: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
5891: {
5892: NODE t,in0,in;
5893: ND ndf,nf;
5894: NDV ndvf;
5895: VL vv,tv;
5896: int stat,nvar,max,mrank;
5897: union oNDC dn;
5898: Q cont;
5899: P pp;
5900: LIST ppl;
5901:
5902: if ( !f ) {
5903: *rp = 0;
5904: return;
5905: }
5906: pltovl(v,&vv);
5907: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
5908:
5909: /* max=65536 implies nd_bpe=32 */
5910: max = 65536;
5911:
5912: nd_module = 0;
5913: /* nd_module will be set if ord is a module ordering */
5914: nd_init_ord(ord);
5915: nd_setup_parameters(nvar,max);
5916: if ( nd_module && OID(f) != O_LIST )
5917: error("nd_nf_p : the first argument must be a list");
5918: if ( nd_module ) mrank = length(BDY((LIST)f));
5919: /* conversion to ndv */
5920: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
5921: NEXTNODE(in0,in);
5922: if ( nd_module ) {
5923: if ( !BDY(t) || OID(BDY(t)) != O_LIST
5924: || length(BDY((LIST)BDY(t))) != mrank )
5925: error("nd_nf_p : inconsistent basis element");
5926: if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
5927: else ppl = (LIST)BDY(t);
5928: BDY(in) = (pointer)pltondv(CO,vv,ppl);
5929: } else {
5930: if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
5931: else pp = (P)BDY(t);
5932: BDY(in) = (pointer)ptondv(CO,vv,pp);
5933: }
5934: if ( m ) ndv_mod(m,(NDV)BDY(in));
5935: }
5936: if ( in0 ) NEXT(in) = 0;
5937:
5938: if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
5939: else ndvf = ptondv(CO,vv,(P)f);
5940: if ( m ) ndv_mod(m,ndvf);
5941: ndf = (pointer)ndvtond(m,ndvf);
5942:
5943: /* dont sort, dont removecont */
5944: ndv_setup(m,0,in0,1,1);
5945: nd_scale=2;
1.6 noro 5946: stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1 noro 5947: if ( !stat )
5948: error("nd_nf_p : exponent too large");
5949: if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
5950: else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
5951: }
5952:
5953: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
5954: {
5955: NM m;
5956: UINT *t,*s;
5957: int i;
5958:
5959: for ( i = 0; i < n; i++ ) r[i] = 0;
5960: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5961: t = DL(m);
5962: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5963: r[i] = CM(m);
5964: }
5965: for ( i = 0; !r[i]; i++ );
5966: return i;
5967: }
5968:
5969: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
5970: {
5971: NM m;
5972: UINT *t,*s;
5973: int i;
5974:
5975: for ( i = 0; i < n; i++ ) r[i] = 0;
5976: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5977: t = DL(m);
5978: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 5979: r[i] = CZ(m);
1.1 noro 5980: }
5981: for ( i = 0; !r[i]; i++ );
5982: return i;
5983: }
5984:
5985: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
5986: {
5987: NM m;
5988: UINT *t,*s;
5989: int i;
5990:
5991: for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
5992: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
5993: t = DL(m);
5994: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
5995: mpz_set(r[i],BDY(CZ(m)));
5996: }
5997: for ( i = 0; !mpz_sgn(r[i]); i++ );
5998: return i;
5999: }
6000:
6001: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
6002: {
6003: NM m;
6004: unsigned long *v;
6005: int i,j,h,size;
6006: UINT *s,*t;
6007:
6008: size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
6009: v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
6010: bzero(v,size);
6011: for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
6012: t = DL(m);
6013: h = ndl_hash_value(t);
6014: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6015: v[i/BLEN] |= 1L <<(i%BLEN);
6016: }
6017: return v;
6018: }
6019:
6020: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
6021: {
6022: NMV mr;
6023: UINT *d,*t,*s;
6024: int i,j,len,h,head;
6025:
6026: d = DL(m);
6027: len = LEN(p);
6028: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6029: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6030: ndl_add(d,DL(mr),t);
6031: h = ndl_hash_value(t);
6032: for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
6033: if ( j == 0 ) head = i;
6034: v[i/BLEN] |= 1L <<(i%BLEN);
6035: }
6036: return head;
6037: }
6038:
6039: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
6040: {
6041: NM m;
6042: NMV mr;
6043: UINT *d,*t,*s;
6044: NDV p;
6045: int i,j,len;
6046: Z *r;
6047:
6048: m = pair->mul;
6049: d = DL(m);
6050: p = nd_ps[pair->index];
6051: len = LEN(p);
6052: r = (Z *)CALLOC(n,sizeof(Q));
6053: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6054: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
6055: ndl_add(d,DL(mr),t);
6056: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6 noro 6057: r[i] = CZ(mr);
1.1 noro 6058: }
6059: return r;
6060: }
6061:
1.11 noro 6062: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1 noro 6063: {
6064: NM m;
6065: NMV mr;
1.11 noro 6066: UINT *d,*t,*s,*u;
1.1 noro 6067: NDV p;
6068: unsigned char *ivc;
6069: unsigned short *ivs;
6070: UINT *v,*ivi,*s0v;
1.11 noro 6071: int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1 noro 6072: IndArray r;
6073:
6074: m = pair->mul;
6075: d = DL(m);
6076: if ( trace )
6077: p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
6078: else
6079: p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
6080:
6081: len = LEN(p);
6082: t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
6083: v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11 noro 6084: for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
6085: ndl_add(d,DL(mr),t);
6086: st = prev;
6087: ed = n;
6088: while ( ed > st ) {
6089: md = (st+ed)/2;
6090: u = s0+md*nd_wpd;
6091: c = DL_COMPARE(u,t);
6092: if ( c == 0 ) break;
6093: else if ( c > 0 ) st = md;
6094: else ed = md;
6095: }
6096: prev = v[j] = md;
1.1 noro 6097: }
6098: r = (IndArray)MALLOC(sizeof(struct oIndArray));
6099: r->head = v[0];
6100: diff = 0;
6101: for ( i = 1; i < len; i++ ) {
6102: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
6103: }
6104: if ( diff < 256 ) {
6105: r->width = 1;
6106: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
6107: r->index.c = ivc;
6108: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
6109: } else if ( diff < 65536 ) {
6110: r->width = 2;
6111: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
6112: r->index.s = ivs;
6113: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
6114: } else {
6115: r->width = 4;
6116: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
6117: r->index.i = ivi;
6118: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
6119: }
6120: return r;
6121: }
6122:
6123: int compress_array(Z *svect,Z *cvect,int n)
6124: {
6125: int i,j;
6126:
6127: for ( i = j = 0; i < n; i++ )
6128: if ( svect[i] ) cvect[j++] = svect[i];
6129: return j;
6130: }
6131:
6132: void expand_array(Z *svect,Z *cvect,int n)
6133: {
6134: int i,j;
6135:
6136: for ( i = j = 0; j < n; i++ )
6137: if ( svect[i] ) svect[i] = cvect[j++];
6138: }
6139:
1.8 noro 6140: #if 0
1.1 noro 6141: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6142: {
6143: int i,j,k,len,pos,prev,nz;
6144: Z cs,mcs,c1,c2,cr,gcd,t;
6145: IndArray ivect;
6146: unsigned char *ivc;
6147: unsigned short *ivs;
6148: unsigned int *ivi;
6149: NDV redv;
6150: NMV mr;
6151: NODE rp;
6152: int maxrs;
6153: double hmag;
6154: Z *cvect;
1.3 noro 6155: int l;
1.1 noro 6156:
6157: maxrs = 0;
6158: for ( i = 0; i < col && !svect[i]; i++ );
6159: if ( i == col ) return maxrs;
6160: hmag = p_mag((P)svect[i])*nd_scale;
6161: cvect = (Z *)MALLOC(col*sizeof(Q));
6162: for ( i = 0; i < nred; i++ ) {
6163: ivect = imat[i];
6164: k = ivect->head;
6165: if ( svect[k] ) {
6166: maxrs = MAX(maxrs,rp0[i]->sugar);
6167: redv = nd_demand?ndv_load(rp0[i]->index)
6168: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6169: len = LEN(redv); mr = BDY(redv);
1.6 noro 6170: igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1 noro 6171: chsgnz(cs,&mcs);
6172: if ( !UNIQ(cr) ) {
6173: for ( j = 0; j < col; j++ ) {
6174: mulz(svect[j],cr,&c1); svect[j] = c1;
6175: }
6176: }
6177: svect[k] = 0; prev = k;
6178: switch ( ivect->width ) {
6179: case 1:
6180: ivc = ivect->index.c;
6181: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6182: pos = prev+ivc[j]; prev = pos;
1.6 noro 6183: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6184: }
6185: break;
6186: case 2:
6187: ivs = ivect->index.s;
6188: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6189: pos = prev+ivs[j]; prev = pos;
1.6 noro 6190: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6191: }
6192: break;
6193: case 4:
6194: ivi = ivect->index.i;
6195: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6196: pos = prev+ivi[j]; prev = pos;
1.6 noro 6197: muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1 noro 6198: }
6199: break;
6200: }
6201: for ( j = k+1; j < col && !svect[j]; j++ );
6202: if ( j == col ) break;
6203: if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
6204: nz = compress_array(svect,cvect,col);
6205: removecont_array((P *)cvect,nz,1);
6206: expand_array(svect,cvect,nz);
6207: hmag = ((double)p_mag((P)svect[j]))*nd_scale;
6208: }
6209: }
6210: }
6211: nz = compress_array(svect,cvect,col);
6212: removecont_array((P *)cvect,nz,1);
6213: expand_array(svect,cvect,nz);
6214: if ( DP_Print ) {
6215: fprintf(asir_out,"-"); fflush(asir_out);
6216: }
6217: return maxrs;
6218: }
1.4 noro 6219: #else
1.9 noro 6220:
1.4 noro 6221: /* direct mpz version */
6222: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6223: {
6224: int i,j,k,len,pos,prev;
6225: mpz_t cs,cr,gcd;
6226: IndArray ivect;
6227: unsigned char *ivc;
6228: unsigned short *ivs;
6229: unsigned int *ivi;
6230: NDV redv;
6231: NMV mr;
6232: NODE rp;
6233: int maxrs;
6234: double hmag;
6235: int l;
1.13 noro 6236: static mpz_t *svect;
6237: static int svect_len=0;
1.4 noro 6238:
6239: maxrs = 0;
6240: for ( i = 0; i < col && !svect0[i]; i++ );
6241: if ( i == col ) return maxrs;
6242: hmag = p_mag((P)svect0[i])*nd_scale;
1.13 noro 6243: if ( col > svect_len ) {
6244: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
6245: svect_len = col;
6246: }
1.4 noro 6247: for ( i = 0; i < col; i++ ) {
6248: mpz_init(svect[i]);
6249: if ( svect0[i] )
6250: mpz_set(svect[i],BDY(svect0[i]));
6251: else
6252: mpz_set_ui(svect[i],0);
6253: }
6254: mpz_init(gcd); mpz_init(cs); mpz_init(cr);
6255: for ( i = 0; i < nred; i++ ) {
6256: ivect = imat[i];
6257: k = ivect->head;
6258: if ( mpz_sgn(svect[k]) ) {
6259: maxrs = MAX(maxrs,rp0[i]->sugar);
6260: redv = nd_demand?ndv_load(rp0[i]->index)
6261: :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
6262: len = LEN(redv); mr = BDY(redv);
1.6 noro 6263: mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4 noro 6264: mpz_div(cs,svect[k],gcd);
1.6 noro 6265: mpz_div(cr,BDY(CZ(mr)),gcd);
1.4 noro 6266: mpz_neg(cs,cs);
1.9 noro 6267: if ( MUNIMPZ(cr) )
6268: for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
6269: else if ( !UNIMPZ(cr) )
6270: for ( j = 0; j < col; j++ ) {
6271: if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
6272: }
1.4 noro 6273: mpz_set_ui(svect[k],0);
6274: prev = k;
6275: switch ( ivect->width ) {
6276: case 1:
6277: ivc = ivect->index.c;
6278: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6279: pos = prev+ivc[j]; prev = pos;
1.6 noro 6280: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6281: }
6282: break;
6283: case 2:
6284: ivs = ivect->index.s;
6285: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6286: pos = prev+ivs[j]; prev = pos;
1.6 noro 6287: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6288: }
6289: break;
6290: case 4:
6291: ivi = ivect->index.i;
6292: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6293: pos = prev+ivi[j]; prev = pos;
1.6 noro 6294: mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4 noro 6295: }
6296: break;
6297: }
6298: for ( j = k+1; j < col && !svect[j]; j++ );
6299: if ( j == col ) break;
6300: if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
6301: mpz_removecont_array(svect,col);
6302: hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
6303: }
6304: }
6305: }
6306: mpz_removecont_array(svect,col);
6307: if ( DP_Print ) {
6308: fprintf(asir_out,"-"); fflush(asir_out);
6309: }
6310: for ( i = 0; i < col; i++ )
6311: if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
6312: else svect0[i] = 0;
6313: return maxrs;
6314: }
6315: #endif
1.1 noro 6316:
6317: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6318: {
6319: int i,j,k,len,pos,prev;
6320: UINT c,c1,c2,c3,up,lo,dmy;
6321: IndArray ivect;
6322: unsigned char *ivc;
6323: unsigned short *ivs;
6324: unsigned int *ivi;
6325: NDV redv;
6326: NMV mr;
6327: NODE rp;
6328: int maxrs;
6329:
6330: maxrs = 0;
6331: for ( i = 0; i < nred; i++ ) {
6332: ivect = imat[i];
6333: k = ivect->head; svect[k] %= m;
6334: if ( (c = svect[k]) != 0 ) {
6335: maxrs = MAX(maxrs,rp0[i]->sugar);
6336: c = m-c; redv = nd_ps[rp0[i]->index];
6337: len = LEN(redv); mr = BDY(redv);
6338: svect[k] = 0; prev = k;
6339: switch ( ivect->width ) {
6340: case 1:
6341: ivc = ivect->index.c;
6342: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6343: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
6344: if ( c1 ) {
6345: c2 = svect[pos];
6346: DMA(c1,c,c2,up,lo);
6347: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6348: } else svect[pos] = lo;
6349: }
6350: }
6351: break;
6352: case 2:
6353: ivs = ivect->index.s;
6354: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6355: pos = prev+ivs[j]; c1 = CM(mr);
6356: prev = pos;
6357: if ( c1 ) {
6358: c2 = svect[pos];
6359: DMA(c1,c,c2,up,lo);
6360: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6361: } else svect[pos] = lo;
6362: }
6363: }
6364: break;
6365: case 4:
6366: ivi = ivect->index.i;
6367: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6368: pos = prev+ivi[j]; c1 = CM(mr);
6369: prev = pos;
6370: if ( c1 ) {
6371: c2 = svect[pos];
6372: DMA(c1,c,c2,up,lo);
6373: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
6374: } else svect[pos] = lo;
6375: }
6376: }
6377: break;
6378: }
6379: }
6380: }
6381: for ( i = 0; i < col; i++ )
6382: if ( svect[i] >= (UINT)m ) svect[i] %= m;
6383: return maxrs;
6384: }
6385:
6386: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6387: {
6388: int i,j,k,len,pos,prev;
6389: UINT c,c1,c2,c3,up,lo,dmy;
6390: IndArray ivect;
6391: unsigned char *ivc;
6392: unsigned short *ivs;
6393: unsigned int *ivi;
6394: NDV redv;
6395: NMV mr;
6396: NODE rp;
6397: int maxrs;
6398:
6399: maxrs = 0;
6400: for ( i = 0; i < nred; i++ ) {
6401: ivect = imat[i];
6402: k = ivect->head;
6403: if ( (c = svect[k]) != 0 ) {
6404: maxrs = MAX(maxrs,rp0[i]->sugar);
6405: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
6406: len = LEN(redv); mr = BDY(redv);
6407: svect[k] = 0; prev = k;
6408: switch ( ivect->width ) {
6409: case 1:
6410: ivc = ivect->index.c;
6411: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6412: pos = prev+ivc[j]; prev = pos;
6413: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6414: }
6415: break;
6416: case 2:
6417: ivs = ivect->index.s;
6418: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6419: pos = prev+ivs[j]; prev = pos;
6420: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6421: }
6422: break;
6423: case 4:
6424: ivi = ivect->index.i;
6425: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6426: pos = prev+ivi[j]; prev = pos;
6427: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
6428: }
6429: break;
6430: }
6431: }
6432: }
6433: return maxrs;
6434: }
6435:
6436: ND nd_add_lf(ND p1,ND p2)
6437: {
6438: int n,c,can;
6439: ND r;
6440: NM m1,m2,mr0,mr,s;
6441: Z t;
6442:
6443: if ( !p1 ) return p2;
6444: else if ( !p2 ) return p1;
6445: else {
6446: can = 0;
6447: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
6448: c = DL_COMPARE(DL(m1),DL(m2));
6449: switch ( c ) {
6450: case 0:
6451: addlf(CZ(m1),CZ(m2),&t);
6452: s = m1; m1 = NEXT(m1);
6453: if ( t ) {
6454: can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
6455: } else {
6456: can += 2; FREENM(s);
6457: }
6458: s = m2; m2 = NEXT(m2); FREENM(s);
6459: break;
6460: case 1:
6461: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
6462: break;
6463: case -1:
6464: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
6465: break;
6466: }
6467: }
6468: if ( !mr0 )
6469: if ( m1 ) mr0 = m1;
6470: else if ( m2 ) mr0 = m2;
6471: else return 0;
6472: else if ( m1 ) NEXT(mr) = m1;
6473: else if ( m2 ) NEXT(mr) = m2;
6474: else NEXT(mr) = 0;
6475: BDY(p1) = mr0;
6476: SG(p1) = MAX(SG(p1),SG(p2));
6477: LEN(p1) = LEN(p1)+LEN(p2)-can;
6478: FREEND(p2);
6479: return p1;
6480: }
6481: }
6482:
6483: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
6484: {
6485: int i,j,k,len,pos,prev;
6486: mpz_t c,mc,c1;
6487: IndArray ivect;
6488: unsigned char *ivc;
6489: unsigned short *ivs;
6490: unsigned int *ivi;
6491: NDV redv;
6492: NMV mr;
6493: NODE rp;
6494: int maxrs;
6495:
6496: maxrs = 0;
6497: lf_lazy = 1;
6498: for ( i = 0; i < nred; i++ ) {
6499: ivect = imat[i];
6500: k = ivect->head;
6501: mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
6502: if ( mpz_sgn(svect[k]) ) {
6503: maxrs = MAX(maxrs,rp0[i]->sugar);
6504: mpz_neg(svect[k],svect[k]);
6505: redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
6506: len = LEN(redv); mr = BDY(redv);
6507: prev = k;
6508: switch ( ivect->width ) {
6509: case 1:
6510: ivc = ivect->index.c;
6511: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6512: pos = prev+ivc[j]; prev = pos;
6513: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6514: }
6515: break;
6516: case 2:
6517: ivs = ivect->index.s;
6518: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6519: pos = prev+ivs[j]; prev = pos;
6520: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6521: }
6522: break;
6523: case 4:
6524: ivi = ivect->index.i;
6525: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
6526: pos = prev+ivi[j]; prev = pos;
6527: mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
6528: }
6529: break;
6530: }
6531: mpz_set_ui(svect[k],0);
6532: }
6533: }
6534: lf_lazy=0;
6535: for ( i = 0; i < col; i++ ) {
6536: mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
6537: }
6538: return maxrs;
6539: }
6540:
6541: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
6542: {
6543: int i,j,k,l,rank,s;
6544: mpz_t a,a1,inv;
6545: mpz_t *t,*pivot,*pk;
6546: mpz_t **mat;
6547: struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
6548: int size,size1;
6549:
6550: mpz_init(inv);
6551: mpz_init(a);
6552: mat = (mpz_t **)mat0;
6553: size = 0;
6554: for ( rank = 0, j = 0; j < col; j++ ) {
6555: for ( i = rank; i < row; i++ ) {
6556: mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
6557: }
6558: for ( i = rank; i < row; i++ )
6559: if ( mpz_sgn(mat[i][j]) )
6560: break;
6561: if ( i == row ) {
6562: colstat[j] = 0;
6563: continue;
6564: } else
6565: colstat[j] = 1;
6566: if ( i != rank ) {
6567: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
6568: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
6569: }
6570: pivot = mat[rank];
6571: s = sugar[rank];
6572: mpz_invert(inv,pivot[j],BDY(current_mod_lf));
6573: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
6574: if ( mpz_sgn(*pk) ) {
6575: mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
6576: }
6577: for ( i = rank+1; i < row; i++ ) {
6578: t = mat[i];
6579: if ( mpz_sgn(t[j]) ) {
6580: sugar[i] = MAX(sugar[i],s);
6581: mpz_neg(a,t[j]);
6582: red_by_vect_lf(t+j,pivot+j,a,col-j);
6583: }
6584: }
6585: rank++;
6586: }
6587: for ( j = col-1, l = rank-1; j >= 0; j-- )
6588: if ( colstat[j] ) {
6589: pivot = mat[l];
6590: s = sugar[l];
6591: for ( k = j; k < col; k++ )
6592: mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
6593: for ( i = 0; i < l; i++ ) {
6594: t = mat[i];
6595: if ( mpz_sgn(t[j]) ) {
6596: sugar[i] = MAX(sugar[i],s);
6597: mpz_neg(a,t[j]);
6598: red_by_vect_lf(t+j,pivot+j,a,col-j);
6599: }
6600: }
6601: l--;
6602: }
6603: for ( j = 0, l = 0; l < rank; j++ )
6604: if ( colstat[j] ) {
6605: t = mat[l];
6606: for ( k = j; k < col; k++ ) {
6607: mpz_mod(t[k],t[k],BDY(current_mod_lf));
6608: }
6609: l++;
6610: }
6611: return rank;
6612: }
6613:
6614:
6615: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
6616: {
6617: int j,k,len;
6618: UINT *p;
6619: UINT c;
6620: NDV r;
6621: NMV mr0,mr;
6622:
6623: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6624: if ( !len ) return 0;
6625: else {
6626: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6627: #if 0
6628: ndv_alloc += nmv_adv*len;
6629: #endif
6630: mr = mr0;
6631: p = s0vect;
6632: for ( j = k = 0; j < col; j++, p += nd_wpd )
6633: if ( !rhead[j] ) {
6634: if ( (c = vect[k++]) != 0 ) {
6635: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
6636: }
6637: }
6638: MKNDV(nd_nvar,mr0,len,r);
6639: return r;
6640: }
6641: }
6642:
6643: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
6644: {
6645: int j,k,len;
6646: UINT *p;
6647: NDV r;
6648: NMV mr0,mr;
6649:
6650: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
6651: if ( !len ) return 0;
6652: else {
6653: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
6654: mr = mr0;
6655: p = s0vect;
6656: for ( j = 0; j < col; j++, p += nd_wpd )
6657: if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
6658: ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
6659: }
6660: MKNDV(nd_nvar,mr0,len,r);
6661: return r;
6662: }
6663: }
6664:
6665: /* for preprocessed vector */
6666:
6667: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
6668: {
1.6 noro 6669: int j,k,len;
6670: UINT *p;
6671: Z c;
6672: NDV r;
6673: NMV mr0,mr;
1.1 noro 6674:
1.6 noro 6675: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
6676: if ( !len ) return 0;
6677: else {
6678: mr0 = (NMV)MALLOC(nmv_adv*len);
1.1 noro 6679: #if 0
1.6 noro 6680: ndv_alloc += nmv_adv*len;
1.1 noro 6681: #endif
1.6 noro 6682: mr = mr0;
6683: p = s0vect;
6684: for ( j = k = 0; j < col; j++, p += nd_wpd ) {
6685: if ( !rhead[j] ) {
6686: if ( (c = vect[k++]) != 0 ) {
6687: if ( !INT(c) )
6688: error("vect_to_ndv_q : components must be integers");
6689: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
6690: }
6691: }
1.1 noro 6692: }
1.6 noro 6693: MKNDV(nd_nvar,mr0,len,r);
6694: return r;
6695: }
1.1 noro 6696: }
6697:
6698: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
6699: {
6700: int j,k,len;
6701: UINT *p;
6702: mpz_t c;
6703: NDV r;
6704: NMV mr0,mr;
6705:
6706: for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
6707: if ( !len ) return 0;
6708: else {
6709: mr0 = (NMV)MALLOC(nmv_adv*len);
6710: #if 0
6711: ndv_alloc += nmv_adv*len;
6712: #endif
6713: mr = mr0;
6714: p = s0vect;
6715: for ( j = k = 0; j < col; j++, p += nd_wpd )
6716: if ( !rhead[j] ) {
6717: c[0] = vect[k++][0];
6718: if ( mpz_sgn(c) ) {
6719: ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
6720: }
6721: }
6722: MKNDV(nd_nvar,mr0,len,r);
6723: return r;
6724: }
6725: }
6726:
6727: /* for plain vector */
6728:
6729: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
6730: {
6731: int j,k,len;
6732: UINT *p;
6733: Z c;
6734: NDV r;
6735: NMV mr0,mr;
6736:
6737: for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
6738: if ( !len ) return 0;
6739: else {
6740: mr0 = (NMV)MALLOC(nmv_adv*len);
6741: #if 0
6742: ndv_alloc += nmv_adv*len;
6743: #endif
6744: mr = mr0;
6745: p = s0vect;
6746: for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
6747: if ( (c = vect[k]) != 0 ) {
6748: if ( !INT(c) )
1.6 noro 6749: error("plain_vect_to_ndv_q : components must be integers");
6750: ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1 noro 6751: }
6752: MKNDV(nd_nvar,mr0,len,r);
6753: return r;
6754: }
6755: }
6756:
6757: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
6758: {
6759: ND_pairs t;
6760: NODE sp0,sp;
6761: int stat;
6762: ND spol;
6763:
6764: for ( t = l; t; t = NEXT(t) ) {
6765: stat = nd_sp(m,trace,t,&spol);
6766: if ( !stat ) return 0;
6767: if ( spol ) {
6768: add_pbucket_symbolic(bucket,spol);
6769: }
6770: }
6771: return 1;
6772: }
6773:
6774: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
6775: {
6776: NODE rp0,rp;
6777: NM mul,head,s0,s;
6778: int index,col,i,sugar;
6779: RHist h;
6780: UINT *s0v,*p;
6781: NM_ind_pair pair;
6782: ND red;
6783: NDV *ps;
6784:
6785: s0 = 0; rp0 = 0; col = 0;
6786: if ( nd_demand )
6787: ps = trace?nd_ps_trace_sym:nd_ps_sym;
6788: else
6789: ps = trace?nd_ps_trace:nd_ps;
6790: while ( 1 ) {
6791: head = remove_head_pbucket_symbolic(bucket);
6792: if ( !head ) break;
6793: if ( !s0 ) s0 = head;
6794: else NEXT(s) = head;
6795: s = head;
6796: index = ndl_find_reducer(DL(head));
6797: if ( index >= 0 ) {
6798: h = nd_psh[index];
6799: NEWNM(mul);
6800: ndl_sub(DL(head),DL(h),DL(mul));
6801: if ( ndl_check_bound2(index,DL(mul)) )
6802: return 0;
6803: sugar = TD(DL(mul))+SG(ps[index]);
6804: MKNM_ind_pair(pair,mul,index,sugar);
6805: red = ndv_mul_nm_symbolic(mul,ps[index]);
6806: add_pbucket_symbolic(bucket,nd_remove_head(red));
6807: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
6808: }
6809: col++;
6810: }
6811: if ( rp0 ) NEXT(rp) = 0;
6812: NEXT(s) = 0;
6813: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
6814: for ( i = 0, p = s0v, s = s0; i < col;
6815: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
6816: *s0vect = s0v;
6817: *r = rp0;
6818: return col;
6819: }
6820:
6821: void print_ndp(ND_pairs l)
6822: {
6823: ND_pairs t;
6824:
6825: for ( t = l; t; t = NEXT(t) )
6826: printf("[%d,%d] ",t->i1,t->i2);
6827: printf("\n");
6828: }
6829:
6830: NODE nd_f4(int m,int checkonly,int **indp)
6831: {
6832: int i,nh,stat,index,f4red;
6833: NODE r,g,tn0,tn,node;
6834: ND_pairs d,l,t,ll0,ll,lh;
6835: LIST l0,l1;
6836: ND spol,red;
6837: NDV nf,redv;
6838: NM s0,s;
6839: NODE rp0,srp0,nflist,nzlist,nzlist_t;
6840: int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
6841: UINT c;
6842: UINT **spmat;
6843: UINT *s0vect,*svect,*p,*v;
6844: int *colstat;
6845: IndArray *imat;
6846: int *rhead;
6847: int spcol,sprow;
6848: int sugar,sugarh;
6849: PGeoBucket bucket;
6850: struct oEGT eg0,eg1,eg_f4;
6851: Z i1,i2,sugarq;
1.12 noro 6852:
6853: init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1 noro 6854: #if 0
6855: ndv_alloc = 0;
6856: #endif
1.11 noro 6857: Nf4_red=0;
1.1 noro 6858: g = 0; d = 0;
6859: for ( i = 0; i < nd_psn; i++ ) {
6860: d = update_pairs(d,g,i,0);
6861: g = update_base(g,i);
6862: }
6863: nzlist = 0;
6864: nzlist_t = nd_nzlist;
6865: f4red = 1;
6866: nd_last_nonzero = 0;
6867: while ( d ) {
6868: get_eg(&eg0);
6869: l = nd_minsugarp(d,&d);
6870: sugar = nd_sugarweight?l->sugar2:SG(l);
6871: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6872: if ( nzlist_t ) {
6873: node = BDY((LIST)BDY(nzlist_t));
1.6 noro 6874: sugarh = ZTOS((Q)ARG0(node));
1.1 noro 6875: tn = BDY((LIST)ARG1(node));
6876: if ( !tn ) {
6877: nzlist_t = NEXT(nzlist_t);
6878: continue;
6879: }
6880: /* tn = [[i1,i2],...] */
6881: lh = nd_ipairtospair(tn);
6882: }
6883: bucket = create_pbucket();
6884: stat = nd_sp_f4(m,0,l,bucket);
6885: if ( !stat ) {
6886: for ( t = l; NEXT(t); t = NEXT(t) );
6887: NEXT(t) = d; d = l;
6888: d = nd_reconstruct(0,d);
6889: continue;
6890: }
6891: if ( bucket->m < 0 ) continue;
6892: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6893: if ( !col ) {
6894: for ( t = l; NEXT(t); t = NEXT(t) );
6895: NEXT(t) = d; d = l;
6896: d = nd_reconstruct(0,d);
6897: continue;
6898: }
1.12 noro 6899: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1 noro 6900: if ( DP_Print )
1.6 noro 6901: fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5 noro 6902: sugar,eg_f4.exectime);
1.1 noro 6903: nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
6904: if ( checkonly && nflist ) return 0;
6905: /* adding new bases */
6906: if ( nflist ) nd_last_nonzero = f4red;
6907: for ( r = nflist; r; r = NEXT(r) ) {
6908: nf = (NDV)BDY(r);
6909: ndv_removecont(m,nf);
6910: if ( !m && nd_nalg ) {
6911: ND nf1;
6912:
6913: nf1 = ndvtond(m,nf);
6914: nd_monic(0,&nf1);
6915: nd_removecont(m,nf1);
6916: nf = ndtondv(m,nf1);
6917: }
6918: nh = ndv_newps(m,nf,0,1);
6919: d = update_pairs(d,g,nh,0);
6920: g = update_base(g,nh);
6921: }
6922: if ( DP_Print ) {
6923: fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
6924: }
6925: if ( nd_gentrace ) {
6926: for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
6927: NEXTNODE(tn0,tn);
1.6 noro 6928: STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1 noro 6929: node = mknode(2,i1,i2); MKLIST(l0,node);
6930: BDY(tn) = l0;
6931: }
6932: if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6 noro 6933: STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1 noro 6934: MKNODE(node,l1,nzlist); nzlist = node;
6935: }
6936: if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
6937: f4red++;
6938: if ( nd_f4red && f4red > nd_f4red ) break;
6939: if ( nd_rank0 && !nflist ) break;
6940: }
6941: if ( nd_gentrace ) {
6942: MKLIST(l0,reverse_node(nzlist));
6943: MKNODE(nd_alltracelist,l0,0);
6944: }
6945: #if 0
6946: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
6947: #endif
1.12 noro 6948: if ( DP_Print ) {
6949: fprintf(asir_out,"number of red=%d,",Nf4_red);
6950: fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
6951: f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
6952: }
1.1 noro 6953: conv_ilist(nd_demand,0,g,indp);
6954: return g;
6955: }
6956:
6957: NODE nd_f4_trace(int m,int **indp)
6958: {
6959: int i,nh,stat,index;
6960: NODE r,g;
6961: ND_pairs d,l,l0,t;
6962: ND spol,red;
6963: NDV nf,redv,nfqv,nfv;
6964: NM s0,s;
6965: NODE rp0,srp0,nflist;
6966: int nsp,nred,col,rank,len,k,j,a;
6967: UINT c;
6968: UINT **spmat;
6969: UINT *s0vect,*svect,*p,*v;
6970: int *colstat;
6971: IndArray *imat;
6972: int *rhead;
6973: int spcol,sprow;
6974: int sugar;
6975: PGeoBucket bucket;
6976: struct oEGT eg0,eg1,eg_f4;
6977:
6978: g = 0; d = 0;
6979: for ( i = 0; i < nd_psn; i++ ) {
6980: d = update_pairs(d,g,i,0);
6981: g = update_base(g,i);
6982: }
6983: while ( d ) {
6984: get_eg(&eg0);
6985: l = nd_minsugarp(d,&d);
6986: sugar = SG(l);
6987: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
6988: bucket = create_pbucket();
6989: stat = nd_sp_f4(m,0,l,bucket);
6990: if ( !stat ) {
6991: for ( t = l; NEXT(t); t = NEXT(t) );
6992: NEXT(t) = d; d = l;
6993: d = nd_reconstruct(1,d);
6994: continue;
6995: }
6996: if ( bucket->m < 0 ) continue;
6997: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
6998: if ( !col ) {
6999: for ( t = l; NEXT(t); t = NEXT(t) );
7000: NEXT(t) = d; d = l;
7001: d = nd_reconstruct(1,d);
7002: continue;
7003: }
7004: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
7005: if ( DP_Print )
1.3 noro 7006: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5 noro 7007: sugar,eg_f4.exectime);
1.1 noro 7008: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
7009: if ( !l0 ) continue;
7010: l = l0;
7011:
7012: /* over Q */
7013: bucket = create_pbucket();
7014: stat = nd_sp_f4(0,1,l,bucket);
7015: if ( !stat ) {
7016: for ( t = l; NEXT(t); t = NEXT(t) );
7017: NEXT(t) = d; d = l;
7018: d = nd_reconstruct(1,d);
7019: continue;
7020: }
7021: if ( bucket->m < 0 ) continue;
7022: col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
7023: if ( !col ) {
7024: for ( t = l; NEXT(t); t = NEXT(t) );
7025: NEXT(t) = d; d = l;
7026: d = nd_reconstruct(1,d);
7027: continue;
7028: }
7029: nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
7030: /* adding new bases */
7031: for ( r = nflist; r; r = NEXT(r) ) {
7032: nfqv = (NDV)BDY(r);
7033: ndv_removecont(0,nfqv);
1.6 noro 7034: if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1 noro 7035: if ( nd_nalg ) {
7036: ND nf1;
7037:
7038: nf1 = ndvtond(m,nfqv);
7039: nd_monic(0,&nf1);
7040: nd_removecont(0,nf1);
7041: nfqv = ndtondv(0,nf1); nd_free(nf1);
7042: }
7043: nfv = ndv_dup(0,nfqv);
7044: ndv_mod(m,nfv);
7045: ndv_removecont(m,nfv);
7046: nh = ndv_newps(0,nfv,nfqv,1);
7047: d = update_pairs(d,g,nh,0);
7048: g = update_base(g,nh);
7049: }
7050: }
7051: #if 0
7052: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
7053: #endif
7054: conv_ilist(nd_demand,1,g,indp);
7055: return g;
7056: }
7057:
7058: int rref(matrix mat,int *sugar)
7059: {
7060: int row,col,i,j,k,l,s,wcol,wj;
7061: unsigned long bj;
7062: unsigned long **a;
7063: unsigned long *ai,*ak,*as,*t;
7064: int *pivot;
7065:
7066: row = mat->row;
7067: col = mat->col;
7068: a = mat->a;
7069: wcol = (col+BLEN-1)/BLEN;
7070: pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
7071: i = 0;
7072: for ( j = 0; j < col; j++ ) {
7073: wj = j/BLEN; bj = 1L<<(j%BLEN);
7074: for ( k = i; k < row; k++ )
7075: if ( a[k][wj] & bj ) break;
7076: if ( k == row ) continue;
7077: pivot[i] = j;
7078: if ( k != i ) {
7079: t = a[i]; a[i] = a[k]; a[k] = t;
7080: s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
7081: }
7082: ai = a[i];
7083: for ( k = i+1; k < row; k++ ) {
7084: ak = a[k];
7085: if ( ak[wj] & bj ) {
7086: for ( l = wj; l < wcol; l++ )
7087: ak[l] ^= ai[l];
7088: sugar[k] = MAX(sugar[k],sugar[i]);
7089: }
7090: }
7091: i++;
7092: }
7093: for ( k = i-1; k >= 0; k-- ) {
7094: j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
7095: ak = a[k];
7096: for ( s = 0; s < k; s++ ) {
7097: as = a[s];
7098: if ( as[wj] & bj ) {
7099: for ( l = wj; l < wcol; l++ )
7100: as[l] ^= ak[l];
7101: sugar[s] = MAX(sugar[s],sugar[k]);
7102: }
7103: }
7104: }
7105: return i;
7106: }
7107:
7108: void print_matrix(matrix mat)
7109: {
7110: int row,col,i,j;
7111: unsigned long *ai;
7112:
7113: row = mat->row;
7114: col = mat->col;
7115: printf("%d x %d\n",row,col);
7116: for ( i = 0; i < row; i++ ) {
7117: ai = mat->a[i];
7118: for ( j = 0; j < col; j++ ) {
7119: if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
7120: else putchar('0');
7121: }
7122: putchar('\n');
7123: }
7124: }
7125:
7126: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
7127:
7128: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
7129: {
7130: int row,col,wcol,wj,i,j;
7131: unsigned long bj;
7132: unsigned long *ai;
7133: unsigned long **a;
7134: int len;
7135: int *pos;
7136:
7137: row = mat->row;
7138: col = mat->col;
7139: wcol = (col+BLEN-1)/BLEN;
7140: pos = (int *)MALLOC(wcol*sizeof(int));
7141: bzero(pos,wcol*sizeof(int));
7142: for ( i = j = 0; i < wcol; i++ )
7143: if ( v[i] ) pos[j++] = i;;
7144: len = j;
7145: wj = rhead/BLEN;
7146: bj = 1L<<rhead%BLEN;
7147: a = mat->a;
7148: for ( i = 0; i < row; i++ ) {
7149: ai = a[i];
7150: if ( ai[wj]&bj ) {
7151: for ( j = 0; j < len; j++ )
7152: ai[pos[j]] ^= v[pos[j]];
7153: sugar[i] = MAX(sugar[i],rsugar);
7154: }
7155: }
7156: }
7157:
7158: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7159: {
7160: int nsp,nred,i,i0,k,rank,row;
7161: NODE r0,rp;
7162: ND_pairs sp;
7163: ND spol;
7164: NM_ind_pair rt;
7165: int *s0hash;
7166: UINT *s;
7167: int *pivot,*sugar,*head;
7168: matrix mat;
7169: NM m;
7170: NODE r;
7171: struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
7172: int rhead,rsugar,size;
7173: unsigned long *v;
7174:
7175: get_eg(&eg0);
7176: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7177: nred = length(rp0);
7178: mat = alloc_matrix(nsp,col);
7179: s0hash = (int *)MALLOC(col*sizeof(int));
7180: for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
7181: s0hash[i] = ndl_hash_value(s);
7182:
7183: sugar = (int *)MALLOC(nsp*sizeof(int));
7184: for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
7185: nd_sp(2,0,sp,&spol);
7186: if ( spol ) {
7187: mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
7188: sugar[i] = SG(spol);
7189: i++;
7190: }
7191: }
7192: mat->row = i;
7193: if ( DP_Print ) {
7194: fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
7195: }
7196: size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
7197: v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
7198: for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
7199: rt = (NM_ind_pair)BDY(rp);
7200: bzero(v,size);
7201: rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
7202: rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
7203: red_by_vect_2(mat,sugar,v,rhead,rsugar);
7204: }
7205:
7206: get_eg(&eg1);
7207: init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
7208: rank = rref(mat,sugar);
7209:
7210: for ( i = 0, r0 = 0; i < rank; i++ ) {
7211: NEXTNODE(r0,r);
7212: BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
7213: SG((NDV)BDY(r)) = sugar[i];
7214: }
7215: if ( r0 ) NEXT(r) = 0;
7216: get_eg(&eg2);
7217: init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
7218: if ( DP_Print ) {
7219: fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5 noro 7220: eg_elim1.exectime,eg_elim2.exectime);
1.1 noro 7221: fflush(asir_out);
7222: }
7223: return r0;
7224: }
7225:
7226:
7227: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
7228: {
7229: IndArray *imat;
1.11 noro 7230: int nsp,nred,i,start;
1.1 noro 7231: int *rhead;
7232: NODE r0,rp;
7233: ND_pairs sp;
7234: NM_ind_pair *rvect;
7235: UINT *s;
7236: int *s0hash;
1.11 noro 7237: struct oEGT eg0,eg1,eg_conv;
1.1 noro 7238:
7239: if ( m == 2 && nd_rref2 )
7240: return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
7241:
7242: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
7243: nred = length(rp0);
7244: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
7245: rhead = (int *)MALLOC(col*sizeof(int));
7246: for ( i = 0; i < col; i++ ) rhead[i] = 0;
7247:
7248: /* construction of index arrays */
1.11 noro 7249: get_eg(&eg0);
1.1 noro 7250: if ( DP_Print ) {
1.11 noro 7251: fprintf(asir_out,"%dx%d,",nsp+nred,col);
7252: fflush(asir_out);
1.1 noro 7253: }
7254: rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11 noro 7255: for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1 noro 7256: rvect[i] = (NM_ind_pair)BDY(rp);
1.11 noro 7257: imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1 noro 7258: rhead[imat[i]->head] = 1;
1.11 noro 7259: start = imat[i]->head;
7260: }
1.12 noro 7261: get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11 noro 7262: if ( DP_Print ) {
7263: fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
7264: fflush(asir_out);
1.1 noro 7265: }
7266: if ( m > 0 )
1.7 noro 7267: #if SIZEOF_LONG==8
1.1 noro 7268: r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7269: #else
7270: r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7271: #endif
7272: else if ( m == -1 )
7273: r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
7274: else if ( m == -2 )
7275: r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7276: else
7277: r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
7278: return r0;
7279: }
7280:
7281: /* for Fp, 2<=p<2^16 */
7282:
7283: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7284: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7285: {
7286: int spcol,sprow,a;
7287: int i,j,k,l,rank;
7288: NODE r0,r;
7289: ND_pairs sp;
7290: ND spol;
7291: UINT **spmat;
7292: UINT *svect,*v;
7293: int *colstat;
7294: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7295: int maxrs;
7296: int *spsugar;
7297: ND_pairs *spactive;
7298:
7299: spcol = col-nred;
7300: get_eg(&eg0);
7301: /* elimination (1st step) */
7302: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7303: svect = (UINT *)MALLOC(col*sizeof(UINT));
7304: spsugar = (int *)MALLOC(nsp*sizeof(int));
7305: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7306: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7307: nd_sp(m,0,sp,&spol);
7308: if ( !spol ) continue;
7309: nd_to_vect(m,s0vect,col,spol,svect);
7310: if ( m == -1 )
7311: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7312: else
7313: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
7314: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7315: if ( i < col ) {
7316: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7317: for ( j = k = 0; j < col; j++ )
7318: if ( !rhead[j] ) v[k++] = svect[j];
7319: spsugar[sprow] = MAX(maxrs,SG(spol));
7320: if ( nz )
7321: spactive[sprow] = sp;
7322: sprow++;
7323: }
7324: nd_free(spol);
7325: }
7326: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7327: if ( DP_Print ) {
1.5 noro 7328: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7329: fflush(asir_out);
7330: }
7331: /* free index arrays */
7332: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7333:
7334: /* elimination (2nd step) */
7335: colstat = (int *)MALLOC(spcol*sizeof(int));
7336: if ( m == -1 )
7337: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7338: else
7339: rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
7340: r0 = 0;
7341: for ( i = 0; i < rank; i++ ) {
7342: NEXTNODE(r0,r); BDY(r) =
7343: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7344: SG((NDV)BDY(r)) = spsugar[i];
7345: GCFREE(spmat[i]);
7346: }
7347: if ( r0 ) NEXT(r) = 0;
7348:
7349: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7350: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7351: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7352: if ( DP_Print ) {
1.5 noro 7353: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7354: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7355: nsp,nred,sprow,spcol,rank);
1.5 noro 7356: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7357: }
7358: if ( nz ) {
7359: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7360: if ( rank > 0 ) {
7361: NEXT(spactive[rank-1]) = 0;
7362: *nz = spactive[0];
7363: } else
7364: *nz = 0;
7365: }
7366: return r0;
7367: }
7368:
7369:
7370: /* for small finite fields */
7371:
7372: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
7373: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
7374: {
7375: int spcol,sprow,a;
7376: int i,j,k,l,rank;
7377: NODE r0,r;
7378: ND_pairs sp;
7379: ND spol;
7380: UINT **spmat;
7381: UINT *svect,*v;
7382: int *colstat;
7383: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7384: int maxrs;
7385: int *spsugar;
7386: ND_pairs *spactive;
7387:
7388: spcol = col-nred;
7389: get_eg(&eg0);
7390: /* elimination (1st step) */
7391: spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
7392: svect = (UINT *)MALLOC(col*sizeof(UINT));
7393: spsugar = (int *)MALLOC(nsp*sizeof(int));
7394: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
7395: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7396: nd_sp(m,0,sp,&spol);
7397: if ( !spol ) continue;
7398: nd_to_vect(m,s0vect,col,spol,svect);
7399: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
7400: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7401: if ( i < col ) {
7402: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
7403: for ( j = k = 0; j < col; j++ )
7404: if ( !rhead[j] ) v[k++] = svect[j];
7405: spsugar[sprow] = MAX(maxrs,SG(spol));
7406: if ( nz )
7407: spactive[sprow] = sp;
7408: sprow++;
7409: }
7410: nd_free(spol);
7411: }
7412: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7413: if ( DP_Print ) {
1.5 noro 7414: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7415: fflush(asir_out);
7416: }
7417: /* free index arrays */
7418: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
7419:
7420: /* elimination (2nd step) */
7421: colstat = (int *)MALLOC(spcol*sizeof(int));
7422: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
7423: r0 = 0;
7424: for ( i = 0; i < rank; i++ ) {
7425: NEXTNODE(r0,r); BDY(r) =
7426: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
7427: SG((NDV)BDY(r)) = spsugar[i];
7428: GCFREE(spmat[i]);
7429: }
7430: if ( r0 ) NEXT(r) = 0;
7431:
7432: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
7433: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7434: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7435: if ( DP_Print ) {
1.5 noro 7436: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7437: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7438: nsp,nred,sprow,spcol,rank);
1.5 noro 7439: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7440: }
7441: if ( nz ) {
7442: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
7443: if ( rank > 0 ) {
7444: NEXT(spactive[rank-1]) = 0;
7445: *nz = spactive[0];
7446: } else
7447: *nz = 0;
7448: }
7449: return r0;
7450: }
7451:
7452: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7453: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7454: {
7455: int spcol,sprow,a;
7456: int i,j,k,l,rank;
7457: NODE r0,r;
7458: ND_pairs sp;
7459: ND spol;
7460: mpz_t **spmat;
7461: mpz_t *svect,*v;
7462: int *colstat;
7463: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7464: int maxrs;
7465: int *spsugar;
7466: pointer *w;
7467:
7468: spcol = col-nred;
7469: get_eg(&eg0);
7470: /* elimination (1st step) */
7471: spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
7472: svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
7473: spsugar = (int *)MALLOC(nsp*sizeof(int));
7474: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7475: nd_sp(m,trace,sp,&spol);
7476: if ( !spol ) continue;
7477: nd_to_vect_lf(s0vect,col,spol,svect);
7478: maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
7479: for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
7480: if ( i < col ) {
7481: spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
7482: for ( j = k = 0; j < col; j++ )
7483: if ( !rhead[j] ) v[k++][0] = svect[j][0];
7484: spsugar[sprow] = MAX(maxrs,SG(spol));
7485: sprow++;
7486: }
7487: /* nd_free(spol); */
7488: }
7489: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7490: if ( DP_Print ) {
1.5 noro 7491: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7492: fflush(asir_out);
7493: }
7494: /* free index arrays */
7495: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7496:
7497: /* elimination (2nd step) */
7498: colstat = (int *)MALLOC(spcol*sizeof(int));
7499: rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
7500: w = (pointer *)MALLOC(rank*sizeof(pointer));
7501: for ( i = 0; i < rank; i++ ) {
7502: #if 0
7503: w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7504: SG((NDV)w[rank-i-1]) = spsugar[i];
7505: #else
7506: w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
7507: SG((NDV)w[i]) = spsugar[i];
7508: #endif
7509: /* GCFREE(spmat[i]); */
7510:
7511: }
7512: #if 0
7513: qsort(w,rank,sizeof(NDV),
7514: (int (*)(const void *,const void *))ndv_compare);
7515: #endif
7516: r0 = 0;
7517: for ( i = 0; i < rank; i++ ) {
7518: NEXTNODE(r0,r); BDY(r) = w[i];
7519: }
7520: if ( r0 ) NEXT(r) = 0;
7521:
7522: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7523: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7524: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7525: if ( DP_Print ) {
1.5 noro 7526: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7527: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7528: nsp,nred,sprow,spcol,rank);
1.5 noro 7529: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7530: }
7531: return r0;
7532: }
7533:
7534: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
7535: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
7536: {
7537: int spcol,sprow,a;
7538: int i,j,k,l,rank;
7539: NODE r0,r;
7540: ND_pairs sp;
7541: ND spol;
7542: Z **spmat;
7543: Z *svect,*v;
7544: int *colstat;
7545: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
7546: int maxrs;
7547: int *spsugar;
7548: pointer *w;
7549:
7550: spcol = col-nred;
7551: get_eg(&eg0);
7552: /* elimination (1st step) */
7553: spmat = (Z **)MALLOC(nsp*sizeof(Q *));
7554: svect = (Z *)MALLOC(col*sizeof(Q));
7555: spsugar = (int *)MALLOC(nsp*sizeof(int));
7556: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
7557: nd_sp(0,trace,sp,&spol);
7558: if ( !spol ) continue;
7559: nd_to_vect_q(s0vect,col,spol,svect);
7560: maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
7561: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
7562: if ( i < col ) {
7563: spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
7564: for ( j = k = 0; j < col; j++ )
7565: if ( !rhead[j] ) v[k++] = svect[j];
7566: spsugar[sprow] = MAX(maxrs,SG(spol));
7567: sprow++;
7568: }
7569: /* nd_free(spol); */
7570: }
7571: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
7572: if ( DP_Print ) {
1.5 noro 7573: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1 noro 7574: fflush(asir_out);
7575: }
7576: /* free index arrays */
7577: /* for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
7578:
7579: /* elimination (2nd step) */
7580: colstat = (int *)MALLOC(spcol*sizeof(int));
7581: rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
7582: w = (pointer *)MALLOC(rank*sizeof(pointer));
7583: for ( i = 0; i < rank; i++ ) {
7584: #if 0
7585: w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7586: SG((NDV)w[rank-i-1]) = spsugar[i];
7587: #else
7588: w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
7589: SG((NDV)w[i]) = spsugar[i];
7590: #endif
7591: /* GCFREE(spmat[i]); */
7592: }
7593: #if 0
7594: qsort(w,rank,sizeof(NDV),
7595: (int (*)(const void *,const void *))ndv_compare);
7596: #endif
7597: r0 = 0;
7598: for ( i = 0; i < rank; i++ ) {
7599: NEXTNODE(r0,r); BDY(r) = w[i];
7600: }
7601: if ( r0 ) NEXT(r) = 0;
7602:
7603: /* for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
7604: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
7605: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
7606: if ( DP_Print ) {
1.5 noro 7607: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1 noro 7608: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
7609: nsp,nred,sprow,spcol,rank);
1.5 noro 7610: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1 noro 7611: }
7612: return r0;
7613: }
7614:
7615: FILE *nd_write,*nd_read;
7616:
7617: void nd_send_int(int a) {
7618: write_int(nd_write,(unsigned int *)&a);
7619: }
7620:
7621: void nd_send_intarray(int *p,int len) {
7622: write_intarray(nd_write,(unsigned int *)p,len);
7623: }
7624:
7625: int nd_recv_int() {
7626: int a;
7627:
7628: read_int(nd_read,(unsigned int *)&a);
7629: return a;
7630: }
7631:
7632: void nd_recv_intarray(int *p,int len) {
7633: read_intarray(nd_read,(unsigned int *)p,len);
7634: }
7635:
7636: void nd_send_ndv(NDV p) {
7637: int len,i;
7638: NMV m;
7639:
7640: if ( !p ) nd_send_int(0);
7641: else {
7642: len = LEN(p);
7643: nd_send_int(len);
7644: m = BDY(p);
7645: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7646: nd_send_int(CM(m));
7647: nd_send_intarray((int *)DL(m),nd_wpd);
7648: }
7649: }
7650: }
7651:
7652: void nd_send_nd(ND p) {
7653: int len,i;
7654: NM m;
7655:
7656: if ( !p ) nd_send_int(0);
7657: else {
7658: len = LEN(p);
7659: nd_send_int(len);
7660: m = BDY(p);
7661: for ( i = 0; i < len; i++, m = NEXT(m) ) {
7662: nd_send_int(CM(m));
7663: nd_send_intarray((int *)DL(m),nd_wpd);
7664: }
7665: }
7666: }
7667:
7668: NDV nd_recv_ndv()
7669: {
7670: int len,i;
7671: NMV m,m0;
7672: NDV r;
7673:
7674: len = nd_recv_int();
7675: if ( !len ) return 0;
7676: else {
7677: m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
7678: #if 0
7679: ndv_alloc += len*nmv_adv;
7680: #endif
7681: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
7682: CM(m) = nd_recv_int();
7683: nd_recv_intarray((int *)DL(m),nd_wpd);
7684: }
7685: MKNDV(nd_nvar,m0,len,r);
7686: return r;
7687: }
7688: }
7689:
7690: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
7691: {
7692: int i,j,t,c,rank,inv;
7693: int *ci,*ri;
7694: Z dn;
7695: MAT m,nm;
7696:
7697: NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
7698: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
7699: for ( i = 0; i < row; i++ )
7700: for ( j = 0; j < col; j++ )
7701: mat0[i][j] = 0;
7702: c = col-rank;
7703: for ( i = 0; i < rank; i++ ) {
7704: mat0[i][ri[i]] = dn;
7705: for ( j = 0; j < c; j++ )
7706: mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
7707: }
7708: return rank;
7709: }
7710:
7711: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
7712: {
7713: int i,j,k,l,inv,a,rank,s;
7714: unsigned int *t,*pivot,*pk;
7715: unsigned int **mat;
7716: ND_pairs pair;
7717:
7718: mat = (unsigned int **)mat0;
7719: for ( rank = 0, j = 0; j < col; j++ ) {
7720: for ( i = rank; i < row; i++ )
7721: mat[i][j] %= md;
7722: for ( i = rank; i < row; i++ )
7723: if ( mat[i][j] )
7724: break;
7725: if ( i == row ) {
7726: colstat[j] = 0;
7727: continue;
7728: } else
7729: colstat[j] = 1;
7730: if ( i != rank ) {
7731: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7732: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7733: if ( spactive ) {
7734: pair = spactive[i]; spactive[i] = spactive[rank];
7735: spactive[rank] = pair;
7736: }
7737: }
7738: pivot = mat[rank];
7739: s = sugar[rank];
7740: inv = invm(pivot[j],md);
7741: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7742: if ( *pk ) {
7743: if ( *pk >= (unsigned int)md )
7744: *pk %= md;
7745: DMAR(*pk,inv,0,md,*pk)
7746: }
7747: for ( i = rank+1; i < row; i++ ) {
7748: t = mat[i];
7749: if ( (a = t[j]) != 0 ) {
7750: sugar[i] = MAX(sugar[i],s);
7751: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7752: }
7753: }
7754: rank++;
7755: }
7756: for ( j = col-1, l = rank-1; j >= 0; j-- )
7757: if ( colstat[j] ) {
7758: pivot = mat[l];
7759: s = sugar[l];
7760: for ( i = 0; i < l; i++ ) {
7761: t = mat[i];
7762: t[j] %= md;
7763: if ( (a = t[j]) != 0 ) {
7764: sugar[i] = MAX(sugar[i],s);
7765: red_by_vect(md,t+j,pivot+j,md-a,col-j);
7766: }
7767: }
7768: l--;
7769: }
7770: for ( j = 0, l = 0; l < rank; j++ )
7771: if ( colstat[j] ) {
7772: t = mat[l];
7773: for ( k = j; k < col; k++ )
7774: if ( t[k] >= (unsigned int)md )
7775: t[k] %= md;
7776: l++;
7777: }
7778: return rank;
7779: }
7780:
7781:
1.7 noro 7782: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1 noro 7783: {
1.7 noro 7784: int i,j,k,l,inv,a,rank,s;
7785: unsigned int *t,*pivot,*pk;
7786: unsigned int **mat;
7787:
7788: mat = (unsigned int **)mat0;
7789: for ( rank = 0, j = 0; j < col; j++ ) {
7790: for ( i = rank; i < row; i++ )
7791: if ( mat[i][j] )
7792: break;
7793: if ( i == row ) {
7794: colstat[j] = 0;
7795: continue;
7796: } else
7797: colstat[j] = 1;
7798: if ( i != rank ) {
7799: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
7800: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
7801: }
7802: pivot = mat[rank];
7803: s = sugar[rank];
7804: inv = _invsf(pivot[j]);
7805: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
7806: if ( *pk )
7807: *pk = _mulsf(*pk,inv);
7808: for ( i = rank+1; i < row; i++ ) {
7809: t = mat[i];
7810: if ( (a = t[j]) != 0 ) {
7811: sugar[i] = MAX(sugar[i],s);
7812: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7813: }
7814: }
7815: rank++;
7816: }
7817: for ( j = col-1, l = rank-1; j >= 0; j-- )
7818: if ( colstat[j] ) {
7819: pivot = mat[l];
7820: s = sugar[l];
7821: for ( i = 0; i < l; i++ ) {
7822: t = mat[i];
7823: if ( (a = t[j]) != 0 ) {
7824: sugar[i] = MAX(sugar[i],s);
7825: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
7826: }
7827: }
7828: l--;
7829: }
7830: return rank;
7831: }
1.1 noro 7832:
1.7 noro 7833: int ndv_ishomo(NDV p)
7834: {
7835: NMV m;
7836: int len,h;
1.1 noro 7837:
7838: if ( !p ) return 1;
7839: len = LEN(p);
7840: m = BDY(p);
7841: h = TD(DL(m));
7842: NMV_ADV(m);
7843: for ( len--; len; len--, NMV_ADV(m) )
7844: if ( TD(DL(m)) != h ) return 0;
7845: return 1;
7846: }
7847:
7848: void ndv_save(NDV p,int index)
7849: {
7850: FILE *s;
7851: char name[BUFSIZ];
7852: short id;
7853: int nv,sugar,len,n,i,td,e,j;
7854: NMV m;
7855: unsigned int *dl;
7856: int mpos;
7857:
7858: sprintf(name,"%s/%d",Demand,index);
7859: s = fopen(name,"w");
7860: savevl(s,0);
7861: if ( !p ) {
7862: saveobj(s,0);
7863: return;
7864: }
7865: id = O_DP;
7866: nv = NV(p);
7867: sugar = SG(p);
7868: len = LEN(p);
7869: write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
7870: write_int(s,(unsigned int *)&len);
7871:
7872: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7873: saveobj(s,(Obj)CZ(m));
1.1 noro 7874: dl = DL(m);
7875: td = TD(dl);
7876: write_int(s,(unsigned int *)&td);
7877: for ( j = 0; j < nv; j++ ) {
7878: e = GET_EXP(dl,j);
7879: write_int(s,(unsigned int *)&e);
7880: }
7881: if ( nd_module ) {
7882: mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
7883: }
7884: }
7885: fclose(s);
7886: }
7887:
7888: void nd_save_mod(ND p,int index)
7889: {
7890: FILE *s;
7891: char name[BUFSIZ];
7892: int nv,sugar,len,c;
7893: NM m;
7894:
7895: sprintf(name,"%s/%d",Demand,index);
7896: s = fopen(name,"w");
7897: if ( !p ) {
7898: len = 0;
7899: write_int(s,(unsigned int *)&len);
7900: fclose(s);
7901: return;
7902: }
7903: nv = NV(p);
7904: sugar = SG(p);
7905: len = LEN(p);
7906: write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
7907: for ( m = BDY(p); m; m = NEXT(m) ) {
7908: c = CM(m); write_int(s,(unsigned int *)&c);
7909: write_intarray(s,(unsigned int *)DL(m),nd_wpd);
7910: }
7911: fclose(s);
7912: }
7913:
7914: NDV ndv_load(int index)
7915: {
7916: FILE *s;
7917: char name[BUFSIZ];
7918: short id;
7919: int nv,sugar,len,n,i,td,e,j;
7920: NDV d;
7921: NMV m0,m;
7922: unsigned int *dl;
7923: Obj obj;
7924: int mpos;
7925:
7926: sprintf(name,"%s/%d",Demand,index);
7927: s = fopen(name,"r");
7928: if ( !s ) return 0;
7929:
7930: skipvl(s);
7931: read_short(s,(unsigned short *)&id);
7932: if ( !id ) return 0;
7933: read_int(s,(unsigned int *)&nv);
7934: read_int(s,(unsigned int *)&sugar);
7935: read_int(s,(unsigned int *)&len);
7936:
7937: m0 = m = MALLOC(len*nmv_adv);
7938: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6 noro 7939: loadobj(s,&obj); CZ(m) = (Z)obj;
1.1 noro 7940: dl = DL(m);
7941: ndl_zero(dl);
7942: read_int(s,(unsigned int *)&td); TD(dl) = td;
7943: for ( j = 0; j < nv; j++ ) {
7944: read_int(s,(unsigned int *)&e);
7945: PUT_EXP(dl,j,e);
7946: }
7947: if ( nd_module ) {
7948: read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
7949: }
7950: if ( nd_blockmask ) ndl_weight_mask(dl);
7951: }
7952: fclose(s);
7953: MKNDV(nv,m0,len,d);
7954: SG(d) = sugar;
7955: return d;
7956: }
7957:
7958: ND nd_load_mod(int index)
7959: {
7960: FILE *s;
7961: char name[BUFSIZ];
7962: int nv,sugar,len,i,c;
7963: ND d;
7964: NM m0,m;
7965:
7966: sprintf(name,"%s/%d",Demand,index);
7967: s = fopen(name,"r");
7968: /* if the file does not exist, it means p[index]=0 */
7969: if ( !s ) return 0;
7970:
7971: read_int(s,(unsigned int *)&nv);
7972: if ( !nv ) { fclose(s); return 0; }
7973:
7974: read_int(s,(unsigned int *)&sugar);
7975: read_int(s,(unsigned int *)&len);
7976: for ( m0 = 0, i = 0; i < len; i++ ) {
7977: NEXTNM(m0,m);
7978: read_int(s,(unsigned int *)&c); CM(m) = c;
7979: read_intarray(s,(unsigned int *)DL(m),nd_wpd);
7980: }
7981: NEXT(m) = 0;
7982: MKND(nv,m0,len,d);
7983: SG(d) = sugar;
7984: fclose(s);
7985: return d;
7986: }
7987:
7988: void nd_det(int mod,MAT f,P *rp)
7989: {
7990: VL fv,tv;
7991: int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
7992: pointer **m;
7993: P **w;
7994: P mp,r;
7995: NDV **dm;
7996: NDV *t,*mi,*mj;
7997: NDV d,s,mij,mjj;
7998: ND u;
7999: NMV nmv;
8000: UINT *bound;
8001: PGeoBucket bucket;
8002: struct order_spec *ord;
8003: Z dq,dt,ds;
8004: Z mone;
8005: Z gn,qn,dn0,nm,dn;
8006:
8007: create_order_spec(0,0,&ord);
8008: nd_init_ord(ord);
8009: get_vars((Obj)f,&fv);
8010: if ( f->row != f->col )
8011: error("nd_det : non-square matrix");
8012: n = f->row;
8013: m = f->body;
8014: for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
8015:
8016: if ( !nvar ) {
8017: if ( !mod )
8018: detp(CO,(P **)m,n,rp);
8019: else {
8020: w = (P **)almat_pointer(n,n);
8021: for ( i = 0; i < n; i++ )
8022: for ( j = 0; j < n; j++ )
8023: ptomp(mod,(P)m[i][j],&w[i][j]);
8024: detmp(CO,mod,w,n,&mp);
8025: mptop(mp,rp);
8026: }
8027: return;
8028: }
8029:
8030: if ( !mod ) {
8031: w = (P **)almat_pointer(n,n);
8032: dq = ONE;
8033: for ( i = 0; i < n; i++ ) {
8034: dn0 = ONE;
8035: for ( j = 0; j < n; j++ ) {
8036: if ( !m[i][j] ) continue;
8037: lgp(m[i][j],&nm,&dn);
1.6 noro 8038: gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1 noro 8039: }
8040: if ( !UNIZ(dn0) ) {
8041: ds = dn0;
8042: for ( j = 0; j < n; j++ )
8043: mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
8044: mulz(dq,ds,&dt); dq = dt;
8045: } else
8046: for ( j = 0; j < n; j++ )
8047: w[i][j] = (P)m[i][j];
8048: }
8049: m = (pointer **)w;
8050: }
8051:
8052: for ( i = 0, max = 1; i < n; i++ )
8053: for ( j = 0; j < n; j++ )
8054: for ( tv = fv; tv; tv = NEXT(tv) ) {
8055: e = getdeg(tv->v,(P)m[i][j]);
8056: max = MAX(e,max);
8057: }
8058: nd_setup_parameters(nvar,max);
8059: dm = (NDV **)almat_pointer(n,n);
8060: for ( i = 0, max = 1; i < n; i++ )
8061: for ( j = 0; j < n; j++ ) {
8062: dm[i][j] = ptondv(CO,fv,m[i][j]);
8063: if ( mod ) ndv_mod(mod,dm[i][j]);
8064: if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
8065: }
8066: d = ptondv(CO,fv,(P)ONE);
8067: if ( mod ) ndv_mod(mod,d);
8068: chsgnz(ONE,&mone);
8069: for ( j = 0, sgn = 1; j < n; j++ ) {
8070: if ( DP_Print ) {
8071: fprintf(asir_out,".");
8072: }
8073: for ( i = j; i < n && !dm[i][j]; i++ );
8074: if ( i == n ) {
8075: *rp = 0;
8076: return;
8077: }
8078: k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
8079: for ( k = j; k < n; k++ )
8080: for ( l = j; l < n; l++ )
8081: if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
8082: k0 = k; l0 = l; len0 = LEN(dm[k][l]);
8083: }
8084: if ( k0 != j ) {
8085: t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
8086: sgn = -sgn;
8087: }
8088: if ( l0 != j ) {
8089: for ( k = j; k < n; k++ ) {
8090: s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
8091: }
8092: sgn = -sgn;
8093: }
8094: bound = nd_det_compute_bound(dm,n,j);
8095: for ( k = 0; k < nd_nvar; k++ )
8096: if ( bound[k]*2 > nd_mask0 ) break;
8097: if ( k < nd_nvar )
8098: nd_det_reconstruct(dm,n,j,d);
8099:
8100: for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
8101: /* if ( DP_Print ) fprintf(asir_out," i=%d\n ",i); */
8102: mi = dm[i]; mij = mi[j];
8103: if ( mod )
8104: ndv_mul_c(mod,mij,mod-1);
8105: else
8106: ndv_mul_c_q(mij,mone);
8107: for ( k = j+1; k < n; k++ ) {
8108: /* if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
8109: bucket = create_pbucket();
8110: if ( mi[k] ) {
8111: nmv = BDY(mjj); len = LEN(mjj);
8112: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8113: u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
8114: add_pbucket(mod,bucket,u);
8115: }
8116: }
8117: if ( mj[k] && mij ) {
8118: nmv = BDY(mij); len = LEN(mij);
8119: for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
8120: u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
8121: add_pbucket(mod,bucket,u);
8122: }
8123: }
8124: u = nd_quo(mod,bucket,d);
8125: mi[k] = ndtondv(mod,u);
8126: }
8127: /* if ( DP_Print ) fprintf(asir_out,"\n",k); */
8128: }
8129: d = mjj;
8130: }
8131: if ( DP_Print ) {
8132: fprintf(asir_out,"\n");
8133: }
8134: if ( sgn < 0 ) {
8135: if ( mod )
8136: ndv_mul_c(mod,d,mod-1);
8137: else
8138: ndv_mul_c_q(d,mone);
8139: }
8140: r = ndvtop(mod,CO,fv,d);
8141: if ( !mod && !UNIQ(dq) )
8142: divsp(CO,r,(P)dq,rp);
8143: else
8144: *rp = r;
8145: }
8146:
8147: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
8148: {
8149: NM mr,mr0;
8150: NM tnm;
8151: NMV m;
8152: UINT *d0,*dt,*dm;
8153: int c,n,td,i,c1,c2,len;
8154: Z q;
8155: ND r;
8156:
8157: if ( !p ) return 0;
8158: else {
8159: n = NV(p); m = BDY(p); len = LEN(p);
8160: d0 = DL(m0);
8161: td = TD(d);
8162: mr0 = 0;
8163: NEWNM(tnm);
8164: if ( mod ) {
8165: c = CM(m0);
8166: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8167: ndl_add(DL(m),d0,DL(tnm));
8168: if ( ndl_reducible(DL(tnm),d) ) {
8169: NEXTNM(mr0,mr);
8170: c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
8171: ndl_copy(DL(tnm),DL(mr));
8172: }
8173: }
8174: } else {
1.6 noro 8175: q = CZ(m0);
1.1 noro 8176: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
8177: ndl_add(DL(m),d0,DL(tnm));
8178: if ( ndl_reducible(DL(tnm),d) ) {
8179: NEXTNM(mr0,mr);
1.6 noro 8180: mulz(CZ(m),q,&CZ(mr));
1.1 noro 8181: ndl_copy(DL(tnm),DL(mr));
8182: }
8183: }
8184: }
8185: if ( !mr0 )
8186: return 0;
8187: else {
8188: NEXT(mr) = 0;
8189: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8190: MKND(NV(p),mr0,len,r);
8191: SG(r) = SG(p) + TD(d0);
8192: return r;
8193: }
8194: }
8195: }
8196:
8197: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
8198: {
8199: int i,obpe,oadv,h,k,l;
8200: static NM prev_nm_free_list;
8201: EPOS oepos;
8202:
8203: obpe = nd_bpe;
8204: oadv = nmv_adv;
8205: oepos = nd_epos;
8206: if ( obpe < 2 ) nd_bpe = 2;
8207: else if ( obpe < 3 ) nd_bpe = 3;
8208: else if ( obpe < 4 ) nd_bpe = 4;
8209: else if ( obpe < 5 ) nd_bpe = 5;
8210: else if ( obpe < 6 ) nd_bpe = 6;
8211: else if ( obpe < 8 ) nd_bpe = 8;
8212: else if ( obpe < 10 ) nd_bpe = 10;
8213: else if ( obpe < 16 ) nd_bpe = 16;
8214: else if ( obpe < 32 ) nd_bpe = 32;
8215: else error("nd_det_reconstruct : exponent too large");
8216:
8217: nd_setup_parameters(nd_nvar,0);
8218: prev_nm_free_list = _nm_free_list;
8219: _nm_free_list = 0;
8220: for ( k = j; k < n; k++ )
8221: for (l = j; l < n; l++ )
8222: ndv_realloc(dm[k][l],obpe,oadv,oepos);
8223: ndv_realloc(d,obpe,oadv,oepos);
8224: prev_nm_free_list = 0;
8225: #if 0
8226: GC_gcollect();
8227: #endif
8228: }
8229:
8230: /* returns a UINT array containing degree bounds */
8231:
8232: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
8233: {
8234: UINT *d0,*d1,*d,*t,*r;
8235: int k,l,i;
8236:
8237: d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
8238: for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
8239: for ( k = j; k < n; k++ )
8240: for ( l = j; l < n; l++ )
8241: if ( dm[k][l] ) {
8242: d = ndv_compute_bound(dm[k][l]);
8243: for ( i = 0; i < nd_nvar; i++ )
8244: d0[i] = MAX(d0[i],d[i]);
8245: }
8246: return d0;
8247: }
8248:
8249: DL nd_separate_d(UINT *d,UINT *trans)
8250: {
8251: int n,td,i,e,j;
8252: DL a;
8253:
8254: ndl_zero(trans);
8255: td = 0;
8256: for ( i = 0; i < nd_ntrans; i++ ) {
8257: e = GET_EXP(d,i);
8258: PUT_EXP(trans,i,e);
8259: td += MUL_WEIGHT(e,i);
8260: }
8261: if ( nd_ntrans+nd_nalg < nd_nvar ) {
8262: /* homogenized */
8263: i = nd_nvar-1;
8264: e = GET_EXP(d,i);
8265: PUT_EXP(trans,i,e);
8266: td += MUL_WEIGHT(e,i);
8267: }
8268: TD(trans) = td;
8269: if ( nd_blockmask) ndl_weight_mask(trans);
8270: NEWDL(a,nd_nalg);
8271: td = 0;
8272: for ( i = 0; i < nd_nalg; i++ ) {
8273: j = nd_ntrans+i;
8274: e = GET_EXP(d,j);
8275: a->d[i] = e;
8276: td += e;
8277: }
8278: a->td = td;
8279: return a;
8280: }
8281:
8282: int nd_monic(int mod,ND *p)
8283: {
8284: UINT *trans,*t;
8285: DL alg;
8286: MP mp0,mp;
8287: NM m,m0,m1,ma0,ma,mb,mr0,mr;
8288: ND r;
8289: DL dl;
8290: DP nm;
8291: NDV ndv;
8292: DAlg inv,cd;
8293: ND s,c;
8294: Z l,mul;
8295: Z ln;
8296: int n,ntrans,i,e,td,is_lc,len;
8297: NumberField nf;
8298: struct oEGT eg0,eg1;
8299:
8300: if ( !(nf = get_numberfield()) )
8301: error("nd_monic : current_numberfield is not set");
8302:
8303: /* Q coef -> DAlg coef */
8304: NEWNM(ma0); ma = ma0;
8305: m = BDY(*p);
8306: is_lc = 1;
8307: while ( 1 ) {
8308: NEWMP(mp0); mp = mp0;
1.6 noro 8309: mp->c = (Obj)CZ(m);
1.1 noro 8310: mp->dl = nd_separate_d(DL(m),DL(ma));
8311: NEWNM(mb);
8312: for ( m = NEXT(m); m; m = NEXT(m) ) {
8313: alg = nd_separate_d(DL(m),DL(mb));
8314: if ( !ndl_equal(DL(ma),DL(mb)) )
8315: break;
1.6 noro 8316: NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1 noro 8317: }
8318: NEXT(mp) = 0;
8319: MKDP(nd_nalg,mp0,nm);
8320: MKDAlg(nm,ONE,cd);
8321: if ( is_lc == 1 ) {
8322: /* if the lc is a rational number, we have nothing to do */
8323: if ( !mp0->dl->td )
8324: return 1;
8325:
8326: get_eg(&eg0);
8327: invdalg(cd,&inv);
8328: get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
8329: /* check the validity of inv */
8330: if ( mod && !remqi((Q)inv->dn,mod) )
8331: return 0;
8332: CA(ma) = nf->one;
8333: is_lc = 0;
8334: ln = ONE;
8335: } else {
8336: muldalg(cd,inv,&CA(ma));
8337: lcmz(ln,CA(ma)->dn,&ln);
8338: }
8339: if ( m ) {
8340: NEXT(ma) = mb; ma = mb;
8341: } else {
8342: NEXT(ma) = 0;
8343: break;
8344: }
8345: }
8346: /* l = lcm(denoms) */
8347: l = ln;
8348: for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6 noro 8349: divsz(l,CA(m)->dn,&mul);
1.1 noro 8350: for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
8351: NEXTNM(mr0,mr);
1.6 noro 8352: mulz((Z)mp->c,mul,&CZ(mr));
1.1 noro 8353: dl = mp->dl;
8354: td = TD(DL(m));
8355: ndl_copy(DL(m),DL(mr));
8356: for ( i = 0; i < nd_nalg; i++ ) {
8357: e = dl->d[i];
8358: PUT_EXP(DL(mr),i+nd_ntrans,e);
8359: td += MUL_WEIGHT(e,i+nd_ntrans);
8360: }
8361: if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
8362: TD(DL(mr)) = td;
8363: if ( nd_blockmask) ndl_weight_mask(DL(mr));
8364: }
8365: }
8366: NEXT(mr) = 0;
8367: for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
8368: MKND(NV(*p),mr0,len,r);
8369: /* XXX */
8370: SG(r) = SG(*p);
8371: nd_free(*p);
8372: *p = r;
8373: return 1;
8374: }
8375:
8376: NODE reverse_node(NODE n)
8377: {
8378: NODE t,t1;
8379:
8380: for ( t = 0; n; n = NEXT(n) ) {
8381: MKNODE(t1,BDY(n),t); t = t1;
8382: }
8383: return t;
8384: }
8385:
8386: P ndc_div(int mod,union oNDC a,union oNDC b)
8387: {
8388: union oNDC c;
8389: int inv,t;
8390:
8391: if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10 noro 8392: else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1 noro 8393: else if ( mod ) {
8394: inv = invm(b.m,mod);
8395: DMAR(a.m,inv,0,mod,t); c.m = t;
8396: } else if ( nd_vc )
8397: divsp(nd_vc,a.p,b.p,&c.p);
8398: else
8399: divsz(a.z,b.z,&c.z);
8400: return ndctop(mod,c);
8401: }
8402:
8403: P ndctop(int mod,union oNDC c)
8404: {
8405: Z q;
8406: int e;
8407: GFS gfs;
8408:
8409: if ( mod == -1 ) {
8410: e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
8411: } else if ( mod == -2 ) {
1.10 noro 8412: q = c.z; return (P)q;
1.1 noro 8413: } else if ( mod > 0 ) {
1.6 noro 8414: STOZ(c.m,q); return (P)q;
1.1 noro 8415: } else
8416: return (P)c.p;
8417: }
8418:
8419: /* [0,0,0,cont] = p -> p/cont */
8420:
8421: void finalize_tracelist(int i,P cont)
8422: {
8423: LIST l;
8424: NODE node;
8425: Z iq;
8426:
8427: if ( !UNIQ(cont) ) {
8428: node = mknode(4,NULLP,NULLP,NULLP,cont);
8429: MKLIST(l,node); MKNODE(node,l,nd_tracelist);
8430: nd_tracelist = node;
8431: }
1.6 noro 8432: STOZ(i,iq);
1.1 noro 8433: nd_tracelist = reverse_node(nd_tracelist);
8434: MKLIST(l,nd_tracelist);
8435: node = mknode(2,iq,l); MKLIST(l,node);
8436: MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
8437: nd_alltracelist = node; nd_tracelist = 0;
8438: }
8439:
8440: void conv_ilist(int demand,int trace,NODE g,int **indp)
8441: {
8442: int n,i,j;
8443: int *ind;
8444: NODE t;
8445:
8446: n = length(g);
8447: ind = (int *)MALLOC(n*sizeof(int));
8448: for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
8449: j = (long)BDY(t); ind[i] = j;
8450: BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
8451: }
8452: if ( indp ) *indp = ind;
8453: }
8454:
8455: void parse_nd_option(NODE opt)
8456: {
8457: NODE t,p,u;
8458: int i,s,n;
8459: char *key;
8460: Obj value;
8461:
8462: nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
8463: nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
8464: nd_splist = 0; nd_check_splist = 0;
8465: nd_sugarweight = 0;
8466: nd_f4red =0;
8467: nd_rank0 = 0;
8468: for ( t = opt; t; t = NEXT(t) ) {
8469: p = BDY((LIST)BDY(t));
8470: key = BDY((STRING)BDY(p));
8471: value = (Obj)BDY(NEXT(p));
8472: if ( !strcmp(key,"gentrace") )
8473: nd_gentrace = value?1:0;
8474: else if ( !strcmp(key,"gensyz") )
8475: nd_gensyz = value?1:0;
8476: else if ( !strcmp(key,"nora") )
8477: nd_nora = value?1:0;
8478: else if ( !strcmp(key,"gbblock") ) {
8479: if ( value && OID(value) == O_LIST ) {
8480: u = BDY((LIST)value);
8481: nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
8482: for ( i = 0; u; u = NEXT(u) ) {
8483: p = BDY((LIST)BDY(u));
1.6 noro 8484: s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
8485: nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1 noro 8486: }
8487: nd_gbblock[i] = -1;
8488: } else
8489: nd_gbblock = 0;
8490: } else if ( !strcmp(key,"newelim") )
8491: nd_newelim = value?1:0;
8492: else if ( !strcmp(key,"intersect") )
8493: nd_intersect = value?1:0;
8494: else if ( !strcmp(key,"lf") )
8495: nd_lf = value?1:0;
8496: else if ( !strcmp(key,"trace") ) {
8497: if ( value ) {
8498: u = BDY((LIST)value);
8499: nd_nzlist = BDY((LIST)ARG2(u));
1.6 noro 8500: nd_bpe = ZTOS((Q)ARG3(u));
1.1 noro 8501: }
8502: } else if ( !strcmp(key,"f4red") ) {
1.6 noro 8503: nd_f4red = ZTOS((Q)value);
1.1 noro 8504: } else if ( !strcmp(key,"rank0") ) {
8505: nd_rank0 = value?1:0;
8506: } else if ( !strcmp(key,"splist") ) {
8507: nd_splist = value?1:0;
8508: } else if ( !strcmp(key,"check_splist") ) {
8509: nd_check_splist = BDY((LIST)value);
8510: } else if ( !strcmp(key,"sugarweight") ) {
8511: u = BDY((LIST)value);
8512: n = length(u);
8513: nd_sugarweight = MALLOC(n*sizeof(int));
8514: for ( i = 0; i < n; i++, u = NEXT(u) )
1.6 noro 8515: nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1 noro 8516: }
8517: }
8518: }
8519:
8520: ND mdptond(DP d);
8521: ND nd_mul_nm(int mod,NM m0,ND p);
8522: ND nd_mul_nm_lf(NM m0,ND p);
8523: ND *btog(NODE ti,ND **p,int nb,int mod);
8524: ND btog_one(NODE ti,ND *p,int nb,int mod);
8525: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
8526: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
8527:
8528: /* d:monomial */
8529: ND mdptond(DP d)
8530: {
8531: NM m;
8532: ND r;
8533:
8534: if ( OID(d) == 1 )
8535: r = ptond(CO,CO,(P)d);
8536: else {
8537: NEWNM(m);
8538: dltondl(NV(d),BDY(d)->dl,DL(m));
1.6 noro 8539: CZ(m) = (Z)BDY(d)->c;
1.1 noro 8540: NEXT(m) = 0;
8541: MKND(NV(d),m,1,r);
8542: }
8543: return r;
8544: }
8545:
8546: ND nd_mul_nm(int mod,NM m0,ND p)
8547: {
8548: UINT *d0;
8549: int c0,c1,c;
8550: NM tm,mr,mr0;
8551: ND r;
8552:
8553: if ( !p ) return 0;
8554: d0 = DL(m0);
8555: c0 = CM(m0);
8556: mr0 = 0;
8557: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8558: NEXTNM(mr0,mr);
8559: c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
8560: ndl_add(d0,DL(tm),DL(mr));
8561: }
8562: NEXT(mr) = 0;
8563: MKND(NV(p),mr0,LEN(p),r);
8564: return r;
8565: }
8566:
8567: ND nd_mul_nm_lf(NM m0,ND p)
8568: {
8569: UINT *d0;
8570: Z c0,c1,c;
8571: NM tm,mr,mr0;
8572: ND r;
8573:
8574: if ( !p ) return 0;
8575: d0 = DL(m0);
8576: c0 = CZ(m0);
8577: mr0 = 0;
8578: for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
8579: NEXTNM(mr0,mr);
8580: c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
8581: ndl_add(d0,DL(tm),DL(mr));
8582: }
8583: NEXT(mr) = 0;
8584: MKND(NV(p),mr0,LEN(p),r);
8585: return r;
8586: }
8587:
8588: ND *btog(NODE ti,ND **p,int nb,int mod)
8589: {
8590: PGeoBucket *r;
8591: int i,ci;
8592: NODE t,s;
8593: ND m,tp;
8594: ND *pi,*rd;
8595: P c;
8596:
8597: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8598: for ( i = 0; i < nb; i++ )
8599: r[i] = create_pbucket();
8600: for ( t = ti; t; t = NEXT(t) ) {
8601: s = BDY((LIST)BDY(t));
8602: if ( ARG0(s) ) {
8603: m = mdptond((DP)ARG2(s));
1.6 noro 8604: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8605: if ( (ci = ((MQ)c)->cont) != 0 ) {
8606: HCM(m) = ci;
1.6 noro 8607: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8608: for ( i = 0; i < nb; i++ ) {
8609: tp = nd_mul_nm(mod,BDY(m),pi[i]);
8610: add_pbucket(mod,r[i],tp);
8611: }
8612: }
8613: ci = 1;
8614: } else {
8615: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8616: ci = invm(ci,mod);
8617: }
8618: }
8619: rd = (ND *)MALLOC(nb*sizeof(ND));
8620: for ( i = 0; i < nb; i++ )
8621: rd[i] = normalize_pbucket(mod,r[i]);
8622: if ( ci != 1 )
8623: for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
8624: return rd;
8625: }
8626:
8627: /* YYY */
8628: ND *btog_lf(NODE ti,ND **p,int nb)
8629: {
8630: PGeoBucket *r;
8631: int i;
8632: NODE t,s;
8633: ND m,tp;
8634: ND *pi,*rd;
8635: LM lm;
8636: Z lf,c;
8637:
8638: r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
8639: for ( i = 0; i < nb; i++ )
8640: r[i] = create_pbucket();
8641: for ( t = ti; t; t = NEXT(t) ) {
8642: s = BDY((LIST)BDY(t));
8643: if ( ARG0(s) ) {
8644: m = mdptond((DP)ARG2(s));
1.6 noro 8645: simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1 noro 8646: if ( lm ) {
8647: lmtolf(lm,&lf); HCZ(m) = lf;
1.6 noro 8648: pi = p[ZTOS((Q)ARG1(s))];
1.1 noro 8649: for ( i = 0; i < nb; i++ ) {
8650: tp = nd_mul_nm_lf(BDY(m),pi[i]);
8651: add_pbucket(-2,r[i],tp);
8652: }
8653: }
8654: c = ONE;
8655: } else {
8656: simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
8657: }
8658: }
8659: rd = (ND *)MALLOC(nb*sizeof(ND));
8660: for ( i = 0; i < nb; i++ )
8661: rd[i] = normalize_pbucket(-2,r[i]);
8662: for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
8663: return rd;
8664: }
8665:
8666: ND btog_one(NODE ti,ND *p,int nb,int mod)
8667: {
8668: PGeoBucket r;
8669: int i,ci,j;
8670: NODE t,s;
8671: ND m,tp;
8672: ND pi,rd;
8673: P c;
8674:
8675: r = create_pbucket();
8676: for ( t = ti; t; t = NEXT(t) ) {
8677: s = BDY((LIST)BDY(t));
8678: if ( ARG0(s) ) {
8679: m = mdptond((DP)ARG2(s));
1.6 noro 8680: ptomp(mod,(P)HCZ(m),&c);
1.1 noro 8681: if ( (ci = ((MQ)c)->cont) != 0 ) {
8682: HCM(m) = ci;
1.6 noro 8683: pi = p[j=ZTOS((Q)ARG1(s))];
1.1 noro 8684: if ( !pi ) {
8685: pi = nd_load_mod(j);
8686: tp = nd_mul_nm(mod,BDY(m),pi);
8687: nd_free(pi);
8688: add_pbucket(mod,r,tp);
8689: } else {
8690: tp = nd_mul_nm(mod,BDY(m),pi);
8691: add_pbucket(mod,r,tp);
8692: }
8693: }
8694: ci = 1;
8695: } else {
8696: ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
8697: ci = invm(ci,mod);
8698: }
8699: }
8700: rd = normalize_pbucket(mod,r);
8701: free_pbucket(r);
8702: if ( ci != 1 ) nd_mul_c(mod,rd,ci);
8703: return rd;
8704: }
8705:
8706: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
8707:
8708: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
8709: {
8710: int i,j,n,m,nb,pi0,pi1,nvar;
8711: VL fv,tv,vv;
8712: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8713: ND **p;
8714: ND *c;
8715: ND u;
8716: P inv;
8717: MAT mat;
8718:
8719: if ( mod == -2 )
8720: return nd_btog_lf(f,v,ord,tlist,rp);
8721:
8722: parse_nd_option(current_option);
8723: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8724: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8725: switch ( ord->id ) {
8726: case 1:
8727: if ( ord->nv != nvar )
8728: error("nd_check : invalid order specification");
8729: break;
8730: default:
8731: break;
8732: }
8733: nd_init_ord(ord);
8734: #if 0
1.6 noro 8735: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8736: #else
8737: nd_bpe = 32;
8738: #endif
8739: nd_setup_parameters(nvar,0);
8740: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8741: intred = BDY((LIST)ARG3(BDY(tlist)));
8742: ind = BDY((LIST)ARG4(BDY(tlist)));
8743: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8744: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8745: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8746: if ( j > i ) i = j;
8747: }
8748: n = i+1;
8749: nb = length(BDY(f));
8750: p = (ND **)MALLOC(n*sizeof(ND *));
8751: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8752: pi = BDY((LIST)BDY(t));
1.6 noro 8753: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8754: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8755: ptomp(mod,(P)ARG2(pi),&inv);
8756: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8757: u = ptond(CO,vv,(P)ONE);
8758: HCM(u) = ((MQ)inv)->cont;
8759: c[pi1] = u;
8760: }
8761: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8762: printf("%d ",i); fflush(stdout);
8763: ti = BDY((LIST)BDY(t));
1.6 noro 8764: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8765: }
8766: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8767: printf("%d ",i); fflush(stdout);
8768: ti = BDY((LIST)BDY(t));
1.6 noro 8769: p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8770: }
8771: m = length(ind);
8772: MKMAT(mat,nb,m);
8773: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8774: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8775: BDY(mat)[i][j] = ndtodp(mod,c[i]);
8776: return mat;
8777: }
8778:
8779: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
8780: {
8781: int i,j,n,m,nb,pi0,pi1,nvar;
8782: VL fv,tv,vv;
8783: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8784: ND **p;
8785: ND *c;
8786: ND u;
8787: MAT mat;
8788: LM lm;
8789: Z lf,inv;
8790:
8791: parse_nd_option(current_option);
8792: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8793: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8794: switch ( ord->id ) {
8795: case 1:
8796: if ( ord->nv != nvar )
8797: error("nd_check : invalid order specification");
8798: break;
8799: default:
8800: break;
8801: }
8802: nd_init_ord(ord);
8803: #if 0
1.6 noro 8804: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8805: #else
8806: nd_bpe = 32;
8807: #endif
8808: nd_setup_parameters(nvar,0);
8809: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8810: intred = BDY((LIST)ARG3(BDY(tlist)));
8811: ind = BDY((LIST)ARG4(BDY(tlist)));
8812: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8813: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8814: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8815: if ( j > i ) i = j;
8816: }
8817: n = i+1;
8818: nb = length(BDY(f));
8819: p = (ND **)MALLOC(n*sizeof(ND *));
8820: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8821: pi = BDY((LIST)BDY(t));
1.6 noro 8822: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8823: p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
8824: simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
8825: u = ptond(CO,vv,(P)ONE);
8826: HCZ(u) = inv;
8827: c[pi1] = u;
8828: }
8829: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8830: printf("%d ",i); fflush(stdout);
8831: ti = BDY((LIST)BDY(t));
1.6 noro 8832: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8833: }
8834: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8835: printf("%d ",i); fflush(stdout);
8836: ti = BDY((LIST)BDY(t));
1.6 noro 8837: p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1 noro 8838: }
8839: m = length(ind);
8840: MKMAT(mat,nb,m);
8841: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6 noro 8842: for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1 noro 8843: BDY(mat)[i][j] = ndtodp(-2,c[i]);
8844: return mat;
8845: }
8846:
8847: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
8848: LIST tlist,int pos,MAT *rp)
8849: {
8850: int i,j,n,m,nb,pi0,pi1,nvar;
8851: VL fv,tv,vv;
8852: NODE permtrace,perm,trace,intred,ind,t,pi,ti;
8853: ND *p;
8854: ND *c;
8855: ND u;
8856: P inv;
8857: VECT vect;
8858:
8859: if ( mod == -2 )
8860: error("nd_btog_one : not implemented yet for a large finite field");
8861:
8862: parse_nd_option(current_option);
8863: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8864: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8865: switch ( ord->id ) {
8866: case 1:
8867: if ( ord->nv != nvar )
8868: error("nd_check : invalid order specification");
8869: break;
8870: default:
8871: break;
8872: }
8873: nd_init_ord(ord);
8874: #if 0
1.6 noro 8875: nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1 noro 8876: #else
8877: nd_bpe = 32;
8878: #endif
8879: nd_setup_parameters(nvar,0);
8880: permtrace = BDY((LIST)ARG2(BDY(tlist)));
8881: intred = BDY((LIST)ARG3(BDY(tlist)));
8882: ind = BDY((LIST)ARG4(BDY(tlist)));
8883: perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
8884: for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6 noro 8885: j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1 noro 8886: if ( j > i ) i = j;
8887: }
8888: n = i+1;
8889: nb = length(BDY(f));
8890: p = (ND *)MALLOC(n*sizeof(ND *));
8891: for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
8892: pi = BDY((LIST)BDY(t));
1.6 noro 8893: pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1 noro 8894: if ( pi1 == pos ) {
8895: ptomp(mod,(P)ARG2(pi),&inv);
8896: ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
8897: u = ptond(CO,vv,(P)ONE);
8898: HCM(u) = ((MQ)inv)->cont;
8899: p[pi0] = u;
8900: }
8901: }
8902: for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
8903: printf("%d ",i); fflush(stdout);
8904: ti = BDY((LIST)BDY(t));
1.6 noro 8905: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8906: if ( Demand ) {
8907: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8908: }
8909: }
8910: for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
8911: printf("%d ",i); fflush(stdout);
8912: ti = BDY((LIST)BDY(t));
1.6 noro 8913: p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1 noro 8914: if ( Demand ) {
8915: nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
8916: }
8917: }
8918: m = length(ind);
8919: MKVECT(vect,m);
8920: for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6 noro 8921: u = p[ZTOS((Q)BDY(t))];
1.1 noro 8922: if ( !u ) {
1.6 noro 8923: u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1 noro 8924: BDY(vect)[j] = ndtodp(mod,u);
8925: nd_free(u);
8926: } else
8927: BDY(vect)[j] = ndtodp(mod,u);
8928: }
8929: return vect;
8930: }
8931:
8932: void ndv_print_lf(NDV p)
8933: {
8934: NMV m;
8935: int i,len;
8936:
8937: if ( !p ) printf("0\n");
8938: else {
8939: len = LEN(p);
8940: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
8941: printf("+");
8942: mpz_out_str(asir_out,10,BDY(CZ(m)));
8943: printf("*");
8944: ndl_print(DL(m));
8945: }
8946: printf("\n");
8947: }
8948: }
8949:
8950: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
8951: {
8952: VL tv,fv,vv,vc,av;
8953: NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
8954: int m,nocheck,nvar,mindex,e,max;
8955: NDV c;
8956: NMV a;
8957: P p,zp;
8958: Q dmy;
8959: EPOS oepos;
8960: int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
8961: Alg alpha,dp;
8962: P poly;
8963: LIST f1,f2,zpl;
8964: Obj obj;
8965: NumberField nf;
8966: struct order_spec *ord1;
8967: struct oEGT eg_check,eg0,eg1;
8968: NODE tr,tl1,tl2,tl3,tl4;
8969: LIST l1,l2,l3,l4,l5;
8970: int *perm;
8971: int j,ret;
8972: NODE retn;
8973: Q jq,bpe;
8974:
8975: nd_module = 0;
8976: parse_nd_option(current_option);
8977: get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
8978: if ( nd_vc )
8979: error("nd_f4_lf_trace : computation over a rational function field is not implemented");
8980: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
8981: switch ( ord->id ) {
8982: case 1:
8983: if ( ord->nv != nvar )
8984: error("nd_f4_lf_trace : invalid order specification");
8985: break;
8986: default:
8987: break;
8988: }
8989:
8990: nd_ntrans = nvar;
8991: nd_nalg = 0;
8992:
8993: nocheck = 0;
8994: mindex = 0;
8995:
8996: /* do not use on-demand load/save */
8997: nd_demand = 0;
8998: m = trace > 1 ? trace : get_lprime(mindex);
8999: nd_init_ord(ord);
9000: mrank = 0;
9001: for ( t = BDY(f), max = 1; t; t = NEXT(t) )
9002: for ( tv = vv; tv; tv = NEXT(tv) ) {
9003: if ( nd_module ) {
9004: s = BDY((LIST)BDY(t));
9005: trank = length(s);
9006: mrank = MAX(mrank,trank);
9007: for ( ; s; s = NEXT(s) ) {
9008: e = getdeg(tv->v,(P)BDY(s));
9009: max = MAX(e,max);
9010: }
9011: } else {
9012: e = getdeg(tv->v,(P)BDY(t));
9013: max = MAX(e,max);
9014: }
9015: }
9016: nd_setup_parameters(nvar,max);
9017: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
9018: ishomo = 1;
9019: /* XXX */
9020: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
9021: if ( nd_module ) {
9022: c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
9023: } else {
9024: c = (pointer)ptondv(CO,vv,(P)BDY(t));
9025: }
9026: if ( ishomo )
9027: ishomo = ishomo && ndv_ishomo(c);
9028: if ( c ) {
9029: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
9030: ndv_mod(-2,c);
9031: NEXTNODE(in0,in); BDY(in) = (pointer)c;
9032: }
9033: }
9034: if ( in0 ) NEXT(in) = 0;
9035: if ( fd0 ) NEXT(fd) = 0;
9036: if ( !ishomo && homo ) {
9037: for ( t = in0, wmax = max; t; t = NEXT(t) ) {
9038: c = (NDV)BDY(t); len = LEN(c);
9039: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
9040: wmax = MAX(TD(DL(a)),wmax);
9041: }
9042: homogenize_order(ord,nvar,&ord1);
9043: nd_init_ord(ord1);
9044: nd_setup_parameters(nvar+1,wmax);
9045: for ( t = fd0; t; t = NEXT(t) )
9046: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
9047: }
9048: if ( MaxDeg > 0 ) nocheck = 1;
9049: ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
9050: if ( ret )
9051: cand = nd_f4_lf_trace_main(m,&perm);
9052: if ( !ret || !cand ) {
9053: *rp = 0; return;
9054: }
9055: if ( !ishomo && homo ) {
9056: /* dehomogenization */
9057: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
9058: nd_init_ord(ord);
9059: nd_setup_parameters(nvar,0);
9060: }
9061: cand = ndv_reducebase(cand,perm);
9062: cand = ndv_reduceall(-2,cand);
9063: cbpe = nd_bpe;
9064: get_eg(&eg0);
9065: if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
9066: /* gbcheck : cand is a GB of Id(cand) ? */
9067: retn = nd_f4(-2,0,0);
9068: }
9069: if ( !retn ) {
9070: /* failure */
9071: *rp = 0; return;
9072: }
9073: get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
9074: if ( DP_Print )
1.5 noro 9075: fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1 noro 9076: /* dp->p */
9077: nd_bpe = cbpe;
9078: nd_setup_parameters(nd_nvar,0);
9079: for ( r = cand; r; r = NEXT(r) ) {
9080: if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
9081: else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
9082: }
9083: MKLIST(*rp,cand);
9084: }
9085:
9086: NODE nd_f4_lf_trace_main(int m,int **indp)
9087: {
9088: int i,nh,stat,index;
9089: NODE r,rm,g;
9090: ND_pairs d,l,l0,t;
9091: ND spol,red;
9092: NDV nf,redv,nfqv,nfv;
9093: NM s0,s;
9094: NODE rp0,srp0,nflist,nflist_lf;
9095: int nsp,nred,col,rank,len,k,j,a;
9096: UINT c;
9097: UINT **spmat;
9098: UINT *s0vect,*svect,*p,*v;
9099: int *colstat;
9100: IndArray *imat;
9101: int *rhead;
9102: int spcol,sprow;
9103: int sugar;
9104: PGeoBucket bucket;
9105: struct oEGT eg0,eg1,eg_f4;
9106:
9107: g = 0; d = 0;
9108: for ( i = 0; i < nd_psn; i++ ) {
9109: d = update_pairs(d,g,i,0);
9110: g = update_base(g,i);
9111: }
9112: while ( d ) {
9113: get_eg(&eg0);
9114: l = nd_minsugarp(d,&d);
9115: sugar = SG(l);
9116: if ( MaxDeg > 0 && sugar > MaxDeg ) break;
9117: bucket = create_pbucket();
9118: stat = nd_sp_f4(m,0,l,bucket);
9119: if ( !stat ) {
9120: for ( t = l; NEXT(t); t = NEXT(t) );
9121: NEXT(t) = d; d = l;
9122: d = nd_reconstruct(1,d);
9123: continue;
9124: }
9125: if ( bucket->m < 0 ) continue;
9126: col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
9127: if ( !col ) {
9128: for ( t = l; NEXT(t); t = NEXT(t) );
9129: NEXT(t) = d; d = l;
9130: d = nd_reconstruct(1,d);
9131: continue;
9132: }
9133: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
9134: if ( DP_Print )
1.5 noro 9135: fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1 noro 9136: nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
9137: if ( !l0 ) continue;
9138: l = l0;
9139:
9140: /* over LF */
9141: bucket = create_pbucket();
9142: stat = nd_sp_f4(-2,1,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,1,&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: nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
9158: /* adding new bases */
9159: for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
9160: nfv = (NDV)BDY(rm);
9161: nfqv = (NDV)BDY(r);
9162: if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
9163: ndv_removecont(m,nfv);
9164: ndv_removecont(-2,nfqv);
9165: nh = ndv_newps(-2,nfv,nfqv,1);
9166: d = update_pairs(d,g,nh,0);
9167: g = update_base(g,nh);
9168: }
9169: if ( r || rm ) return 0;
9170: }
9171: conv_ilist(nd_demand,1,g,indp);
9172: return g;
9173: }
9174:
1.7 noro 9175: #if SIZEOF_LONG==8
9176:
9177: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
9178: {
9179: int j,k,len;
9180: UINT *p;
9181: UINT c;
9182: NDV r;
9183: NMV mr0,mr;
9184:
9185: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
9186: if ( !len ) return 0;
9187: else {
9188: mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
9189: #if 0
9190: ndv_alloc += nmv_adv*len;
9191: #endif
9192: mr = mr0;
9193: p = s0vect;
9194: for ( j = k = 0; j < col; j++, p += nd_wpd )
9195: if ( !rhead[j] ) {
9196: if ( (c = (UINT)vect[k++]) != 0 ) {
9197: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
9198: }
9199: }
9200: MKNDV(nd_nvar,mr0,len,r);
9201: return r;
9202: }
9203: }
9204:
9205: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
9206: {
9207: NM m;
1.11 noro 9208: UINT *t,*s,*u;
9209: int i,st,ed,md,prev,c;
1.7 noro 9210:
9211: for ( i = 0; i < n; i++ ) r[i] = 0;
1.11 noro 9212: prev = 0;
9213: for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
9214: t = DL(m);
9215: st = prev;
9216: ed = n;
9217: while ( ed > st ) {
9218: md = (st+ed)/2;
9219: u = s0+md*nd_wpd;
9220: c = DL_COMPARE(u,t);
9221: if ( c == 0 ) break;
9222: else if ( c > 0 ) st = md;
9223: else ed = md;
9224: }
9225: r[md] = (mp_limb_t)CM(m);
9226: prev = md;
1.7 noro 9227: }
9228: for ( i = 0; !r[i]; i++ );
9229: return i;
9230: }
9231:
9232: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
9233:
9234: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
9235: {
9236: int i,j,k,len,pos,prev;
9237: mp_limb_t a,c,c1,c2;
9238: IndArray ivect;
9239: unsigned char *ivc;
9240: unsigned short *ivs;
9241: unsigned int *ivi;
9242: NDV redv;
9243: NMV mr;
9244: NODE rp;
9245: int maxrs;
9246:
9247: for ( i = 0; i < col; i++ ) cvect[i] = 0;
9248: maxrs = 0;
9249: for ( i = 0; i < nred; i++ ) {
9250: ivect = imat[i];
9251: k = ivect->head;
9252: a = svect[k]; c = cvect[k];
9253: MOD128(a,c,m);
9254: svect[k] = a; cvect[k] = 0;
9255: if ( (c = svect[k]) != 0 ) {
1.11 noro 9256: Nf4_red++;
1.7 noro 9257: maxrs = MAX(maxrs,rp0[i]->sugar);
9258: c = m-c; redv = nd_ps[rp0[i]->index];
9259: len = LEN(redv); mr = BDY(redv);
9260: svect[k] = 0; prev = k;
9261: switch ( ivect->width ) {
9262: case 1:
9263: ivc = ivect->index.c;
9264: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9265: pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12 noro 9266: c2 = svect[pos]+c1*c;
9267: if ( c2 < svect[pos] ) cvect[pos]++;
9268: svect[pos] = c2;
1.7 noro 9269: }
9270: break;
9271: case 2:
9272: ivs = ivect->index.s;
9273: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9274: pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12 noro 9275: c2 = svect[pos]+c1*c;
9276: if ( c2 < svect[pos] ) cvect[pos]++;
9277: svect[pos] = c2;
1.7 noro 9278: }
9279: break;
9280: case 4:
9281: ivi = ivect->index.i;
9282: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
9283: pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12 noro 9284: c2 = svect[pos]+c1*c;
9285: if ( c2 < svect[pos] ) cvect[pos]++;
9286: svect[pos] = c2;
1.7 noro 9287: }
9288: break;
9289: }
9290: }
9291: }
9292: for ( i = 0; i < col; i++ ) {
9293: a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
9294: }
9295: return maxrs;
9296: }
9297:
9298: /* for Fp, 2^15=<p<2^29 */
9299:
9300: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
9301: NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
9302: {
9303: int spcol,sprow,a;
9304: int i,j,k,l,rank;
9305: NODE r0,r;
9306: ND_pairs sp;
9307: ND spol;
9308: mp_limb_t **spmat;
9309: mp_limb_t *svect,*cvect;
9310: mp_limb_t *v;
9311: int *colstat;
9312: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
9313: int maxrs;
9314: int *spsugar;
9315: ND_pairs *spactive;
9316:
9317: spcol = col-nred;
9318: get_eg(&eg0);
9319: /* elimination (1st step) */
9320: spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
9321: svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9322: cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
9323: spsugar = (int *)MALLOC(nsp*sizeof(int));
9324: spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
9325: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
9326: nd_sp(m,0,sp,&spol);
9327: if ( !spol ) continue;
9328: nd_to_vect64(m,s0vect,col,spol,svect);
9329: maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
9330: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
9331: if ( i < col ) {
9332: spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
9333: for ( j = k = 0; j < col; j++ )
9334: if ( !rhead[j] ) v[k++] = (UINT)svect[j];
9335: spsugar[sprow] = MAX(maxrs,SG(spol));
9336: if ( nz )
9337: spactive[sprow] = sp;
9338: sprow++;
9339: }
9340: nd_free(spol);
9341: }
1.12 noro 9342: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7 noro 9343: if ( DP_Print ) {
9344: fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
9345: fflush(asir_out);
9346: }
9347: /* free index arrays */
9348: for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
9349:
9350: /* elimination (2nd step) */
9351: colstat = (int *)MALLOC(spcol*sizeof(int));
9352: rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
9353: r0 = 0;
9354: for ( i = 0; i < rank; i++ ) {
9355: NEXTNODE(r0,r); BDY(r) =
9356: (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
9357: SG((NDV)BDY(r)) = spsugar[i];
9358: GCFREE(spmat[i]);
9359: }
9360: if ( r0 ) NEXT(r) = 0;
9361:
9362: for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12 noro 9363: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7 noro 9364: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
9365: if ( DP_Print ) {
9366: fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
9367: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
9368: nsp,nred,sprow,spcol,rank);
9369: fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
9370: }
9371: if ( nz ) {
9372: for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
9373: if ( rank > 0 ) {
9374: NEXT(spactive[rank-1]) = 0;
9375: *nz = spactive[0];
9376: } else
9377: *nz = 0;
9378: }
9379: return r0;
9380: }
9381:
9382: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
9383: {
9384: int i,j,k,l,rank,s;
9385: mp_limb_t inv;
9386: mp_limb_t a;
9387: UINT c;
9388: mp_limb_t *t,*pivot,*pk;
9389: UINT *ck;
9390: UINT **cmat;
9391: UINT *ct;
9392: ND_pairs pair;
9393:
9394: cmat = (UINT **)MALLOC(row*sizeof(UINT *));
9395: for ( i = 0; i < row; i++ ) {
9396: cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
9397: bzero(cmat[i],col*sizeof(UINT));
9398: }
9399:
9400: for ( rank = 0, j = 0; j < col; j++ ) {
9401: for ( i = rank; i < row; i++ ) {
9402: a = mat[i][j]; c = cmat[i][j];
9403: MOD128(a,c,md);
9404: mat[i][j] = a; cmat[i][j] = 0;
9405: }
9406: for ( i = rank; i < row; i++ )
9407: if ( mat[i][j] )
9408: break;
9409: if ( i == row ) {
9410: colstat[j] = 0;
9411: continue;
9412: } else
9413: colstat[j] = 1;
9414: if ( i != rank ) {
9415: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
9416: ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
9417: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
9418: if ( spactive ) {
9419: pair = spactive[i]; spactive[i] = spactive[rank];
9420: spactive[rank] = pair;
9421: }
9422: }
9423: /* column j is normalized */
9424: s = sugar[rank];
9425: inv = invm((UINT)mat[rank][j],md);
9426: /* normalize pivot row */
9427: for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
9428: a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
9429: }
9430: for ( i = rank+1; i < row; i++ ) {
9431: if ( (a = mat[i][j]) != 0 ) {
9432: sugar[i] = MAX(sugar[i],s);
9433: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11 noro 9434: Nf4_red++;
1.7 noro 9435: }
9436: }
9437: rank++;
9438: }
9439: for ( j = col-1, l = rank-1; j >= 0; j-- )
9440: if ( colstat[j] ) {
9441: for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
9442: a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
9443: }
9444: s = sugar[l];
9445: for ( i = 0; i < l; i++ ) {
9446: a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
9447: if ( a ) {
9448: sugar[i] = MAX(sugar[i],s);
9449: red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11 noro 9450: Nf4_red++;
1.7 noro 9451: }
9452: }
9453: l--;
9454: }
9455: for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
9456: GCFREE(cmat);
9457: return rank;
9458: }
9459: #endif
9460:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>