Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.29
1.29 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.28 2003/08/11 06:58:01 noro Exp $ */
1.2 noro 2:
1.1 noro 3: #include "ca.h"
4: #include "inline.h"
5:
6: #if defined(__GNUC__)
7: #define INLINE inline
8: #elif defined(VISUAL)
9: #define INLINE __inline
10: #else
11: #define INLINE
12: #endif
13:
1.28 noro 14: #define USE_GEOBUCKET 1
15:
1.1 noro 16: #define REDTAB_LEN 32003
17:
18: typedef struct oPGeoBucket {
19: int m;
20: struct oND *body[32];
21: } *PGeoBucket;
22:
23: typedef struct oND {
24: struct oNM *body;
25: int nv;
26: int sugar;
27: } *ND;
28:
1.3 noro 29: typedef struct oNDV {
30: struct oNMV *body;
31: int nv;
32: int sugar;
33: int len;
34: } *NDV;
35:
1.1 noro 36: typedef struct oNM {
37: struct oNM *next;
1.14 noro 38: union {
39: int m;
40: Q z;
41: } c;
1.1 noro 42: int td;
43: unsigned int dl[1];
44: } *NM;
45:
1.3 noro 46: typedef struct oNMV {
1.14 noro 47: union {
48: int m;
49: Q z;
50: } c;
1.3 noro 51: int td;
52: unsigned int dl[1];
53: } *NMV;
54:
1.13 noro 55: typedef struct oRHist {
56: struct oRHist *next;
57: int index;
1.20 noro 58: int td,sugar;
1.13 noro 59: unsigned int dl[1];
60: } *RHist;
61:
1.1 noro 62: typedef struct oND_pairs {
63: struct oND_pairs *next;
64: int i1,i2;
65: int td,sugar;
66: unsigned int lcm[1];
67: } *ND_pairs;
68:
1.21 noro 69: double nd_scale=2;
1.1 noro 70: static unsigned int **nd_bound;
1.19 noro 71: int nd_nvar;
1.1 noro 72: int is_rlex;
73: int nd_epw,nd_bpe,nd_wpd;
74: unsigned int nd_mask[32];
75: unsigned int nd_mask0,nd_mask1;
1.20 noro 76:
1.1 noro 77: NM _nm_free_list;
78: ND _nd_free_list;
79: ND_pairs _ndp_free_list;
1.20 noro 80:
81: static NDV *nd_ps;
82: static NDV *nd_psq;
83: int *nd_psl;
84: RHist *nd_psh;
85: int nd_psn,nd_pslen;
86:
1.13 noro 87: RHist *nd_red;
1.1 noro 88: int nd_red_len;
89:
90: int nd_found,nd_create,nd_notfirst;
1.13 noro 91: int nm_adv;
1.20 noro 92: int nmv_adv;
1.1 noro 93:
1.20 noro 94: extern int Top,Reverse;
1.1 noro 95:
96: #define HTD(d) ((d)->body->td)
97: #define HDL(d) ((d)->body->dl)
1.14 noro 98: #define HCM(d) ((d)->body->c.m)
1.16 noro 99: #define HCQ(d) ((d)->body->c.z)
1.14 noro 100: #define CM(a) ((a)->c.m)
1.16 noro 101: #define CQ(a) ((a)->c.z)
1.14 noro 102: #define DL(a) ((a)->dl)
103: #define TD(a) ((a)->td)
104: #define SG(a) ((a)->sugar)
105: #define LEN(a) ((a)->len)
1.1 noro 106:
1.20 noro 107: #define NM_ADV(m) (m = (NM)(((char *)m)+nm_adv))
1.15 noro 108: #define NEWRHist(r) \
109: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(unsigned int)))
1.1 noro 110: #define NEWND_pairs(m) if(!_ndp_free_list)_NDP_alloc(); (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
111: #define NEWNM(m) if(!_nm_free_list)_NM_alloc(); (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
112: #define MKND(n,m,d) if(!_nd_free_list)_ND_alloc(); (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list); (d)->nv=(n); BDY(d)=(m)
113:
1.13 noro 114: #define NEXTRHist(r,c) \
115: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1 noro 116: #define NEXTNM(r,c) \
117: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
118: #define NEXTNM2(r,c,s) \
119: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
120: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
121: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
122: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
123:
124: #define NEXTND_pairs(r,c) \
125: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
126:
1.27 noro 127: int nd_check_candidate(NODE input,NODE cand);
1.20 noro 128: void nd_removecont(int mod,ND p);
1.21 noro 129: void nd_removecont2(ND p1,ND p2);
1.20 noro 130: void ndv_removecont(int mod,NDV p);
1.23 noro 131: void ndv_dehomogenize(NDV p);
1.16 noro 132: void ndv_mul_c_q(NDV p,Q mul);
133: void nd_mul_c_q(ND p,Q mul);
1.25 noro 134: ND normalize_pbucket(int mod,PGeoBucket g);
135: int head_pbucket(int mod,PGeoBucket g);
1.26 noro 136: int head_pbucket_q(PGeoBucket g);
1.25 noro 137: void add_pbucket(int mod,PGeoBucket g,ND d,int l);
138: void free_pbucket(PGeoBucket b);
1.26 noro 139: void mulq_pbucket(PGeoBucket g,Q c);
1.25 noro 140: PGeoBucket create_pbucket();
141: ND nd_remove_head(ND p);
1.16 noro 142:
1.20 noro 143: void GC_gcollect();
144: NODE append_one(NODE,int);
1.27 noro 145: NODE nd_reducebase(NODE x);
1.20 noro 146:
1.21 noro 147: void removecont_array(Q *c,int n);
1.1 noro 148: ND_pairs crit_B( ND_pairs d, int s );
149: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp);
1.23 noro 150: void nd_gr_trace(LIST f,LIST v,int m,int homo,struct order_spec *ord,LIST *rp);
151: void nd_setup(int mod,NODE f);
152: void nd_setup_trace(int mod,NODE f);
1.16 noro 153: int nd_newps(int mod,ND a);
1.20 noro 154: int nd_newps_trace(int mod,ND nf,ND nfq);
1.1 noro 155: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
156: NODE update_base(NODE nd,int ndp);
157: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
158: int crit_2( int dp1, int dp2 );
159: ND_pairs crit_F( ND_pairs d1 );
160: ND_pairs crit_M( ND_pairs d1 );
161: ND_pairs nd_newpairs( NODE g, int t );
162: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
1.27 noro 163: NODE nd_gb(int m,int checkonly);
1.23 noro 164: NODE nd_gb_trace(int m);
1.1 noro 165: void nd_free_private_storage();
166: void _NM_alloc();
167: void _ND_alloc();
168: int ndl_td(unsigned int *d);
1.23 noro 169: int ndl_dehomogenize(unsigned int *p);
1.19 noro 170: ND nd_add(int mod,ND p1,ND p2);
1.17 noro 171: ND nd_add_q(ND p1,ND p2);
172: ND nd_mul_nm(int mod,ND p,NM m0);
173: ND nd_mul_ind_nm(int mod,int index,NM m0);
1.16 noro 174: int nd_sp(int mod,ND_pairs p,ND *nf);
1.6 noro 175: int nd_find_reducer(ND g);
1.23 noro 176: int nd_find_reducer_direct(ND g,NDV *ps,int len);
1.16 noro 177: int nd_nf(int mod,ND g,int full,ND *nf);
1.1 noro 178: ND nd_reduce(ND p1,ND p2);
179: ND nd_reduce_special(ND p1,ND p2);
1.23 noro 180: NODE nd_reduceall(int m,NODE f);
1.27 noro 181: int nd_gbcheck(int m,NODE f);
182: int nd_membercheck(int m,NODE f);
1.1 noro 183: void nd_free(ND p);
1.23 noro 184: void ndv_free(NDV p);
1.1 noro 185: void ndl_print(unsigned int *dl);
186: void nd_print(ND p);
1.16 noro 187: void nd_print_q(ND p);
188: void ndv_print(NDV p);
189: void ndv_print_q(NDV p);
1.1 noro 190: void ndp_print(ND_pairs d);
191: int nd_length(ND p);
1.19 noro 192: void nd_mul_c(int mod,ND p,int mul);
1.1 noro 193: void nd_free_redlist();
194: void nd_append_red(unsigned int *d,int td,int i);
195: unsigned int *nd_compute_bound(ND p);
1.5 noro 196: unsigned int *dp_compute_bound(DP p);
1.20 noro 197: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs ndp);
1.23 noro 198: void nd_reconstruct_direct(int mod,NDV *ps,int len);
1.1 noro 199: void nd_setup_parameters();
1.11 noro 200: void nd_realloc(ND p,int obpe);
1.6 noro 201: ND nd_copy(ND p);
1.1 noro 202: void ndl_dup(int obpe,unsigned int *d,unsigned int *r);
1.4 noro 203:
204: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
205: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
1.14 noro 206: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.19 noro 207: void ndv_mul_c(int mod,NDV p,int mul);
1.16 noro 208: NDV ndtondv(int mod,ND p);
1.23 noro 209: ND ndvtond(int mod,NDV p);
1.25 noro 210: ND ndv_mul_nm(int mod,NDV p,NM m0);
1.11 noro 211: void ndv_realloc(NDV p,int obpe,int oadv);
1.16 noro 212: NDV dptondv(int,DP);
213: DP ndvtodp(int,NDV);
1.17 noro 214: ND dptond(int,DP);
215: DP ndtodp(int,ND);
1.1 noro 216:
217: void nd_free_private_storage()
218: {
219: _nd_free_list = 0;
220: _nm_free_list = 0;
1.5 noro 221: _ndp_free_list = 0;
1.13 noro 222: bzero(nd_red,sizeof(REDTAB_LEN*sizeof(RHist)));
1.1 noro 223: GC_gcollect();
224: }
225:
226: void _NM_alloc()
227: {
228: NM p;
229: int i;
230:
1.11 noro 231: for ( i = 0; i < 1024; i++ ) {
1.1 noro 232: p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
233: p->next = _nm_free_list; _nm_free_list = p;
234: }
235: }
236:
237: void _ND_alloc()
238: {
239: ND p;
240: int i;
241:
242: for ( i = 0; i < 1024; i++ ) {
243: p = (ND)GC_malloc(sizeof(struct oND));
244: p->body = (NM)_nd_free_list; _nd_free_list = p;
245: }
246: }
247:
248: void _NDP_alloc()
249: {
250: ND_pairs p;
251: int i;
252:
1.11 noro 253: for ( i = 0; i < 1024; i++ ) {
1.1 noro 254: p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
255: +(nd_wpd-1)*sizeof(unsigned int));
256: p->next = _ndp_free_list; _ndp_free_list = p;
257: }
258: }
259:
260: INLINE nd_length(ND p)
261: {
262: NM m;
263: int i;
264:
265: if ( !p )
266: return 0;
267: else {
268: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
269: return i;
270: }
271: }
272:
273: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2)
274: {
275: unsigned int u1,u2;
276: int i,j;
277:
278: switch ( nd_bpe ) {
279: case 4:
280: for ( i = 0; i < nd_wpd; i++ ) {
281: u1 = d1[i]; u2 = d2[i];
282: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
283: if ( (u1&0xf000000) < (u2&0xf000000) ) return 0;
284: if ( (u1&0xf00000) < (u2&0xf00000) ) return 0;
285: if ( (u1&0xf0000) < (u2&0xf0000) ) return 0;
286: if ( (u1&0xf000) < (u2&0xf000) ) return 0;
287: if ( (u1&0xf00) < (u2&0xf00) ) return 0;
288: if ( (u1&0xf0) < (u2&0xf0) ) return 0;
289: if ( (u1&0xf) < (u2&0xf) ) return 0;
290: }
291: return 1;
292: break;
293: case 6:
294: for ( i = 0; i < nd_wpd; i++ ) {
295: u1 = d1[i]; u2 = d2[i];
296: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
297: if ( (u1&0xfc0000) < (u2&0xfc0000) ) return 0;
298: if ( (u1&0x3f000) < (u2&0x3f000) ) return 0;
299: if ( (u1&0xfc0) < (u2&0xfc0) ) return 0;
300: if ( (u1&0x3f) < (u2&0x3f) ) return 0;
301: }
302: return 1;
303: break;
304: case 8:
305: for ( i = 0; i < nd_wpd; i++ ) {
306: u1 = d1[i]; u2 = d2[i];
307: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
308: if ( (u1&0xff0000) < (u2&0xff0000) ) return 0;
309: if ( (u1&0xff00) < (u2&0xff00) ) return 0;
310: if ( (u1&0xff) < (u2&0xff) ) return 0;
311: }
312: return 1;
313: break;
314: case 16:
315: for ( i = 0; i < nd_wpd; i++ ) {
316: u1 = d1[i]; u2 = d2[i];
317: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
318: if ( (u1&0xffff) < (u2&0xffff) ) return 0;
319: }
320: return 1;
321: break;
322: case 32:
323: for ( i = 0; i < nd_wpd; i++ )
324: if ( d1[i] < d2[i] ) return 0;
325: return 1;
326: break;
327: default:
328: for ( i = 0; i < nd_wpd; i++ ) {
329: u1 = d1[i]; u2 = d2[i];
330: for ( j = 0; j < nd_epw; j++ )
331: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
332: }
333: return 1;
334: }
335: }
336:
1.23 noro 337: /* returns the exponent of homo variable */
338:
339: int ndl_dehomogenize(unsigned int *d)
340: {
341: unsigned int mask;
342: unsigned int h;
343: int i;
344:
345: if ( is_rlex ) {
346: if ( nd_bpe == 32 ) {
347: h = d[0];
348: for ( i = 1; i < nd_wpd; i++ )
349: d[i-1] = d[i];
350: d[i-1] = 0;
351: return h;
352: } else {
353: mask = (1<<(nd_epw*nd_bpe))-1;
354: h = (d[0]>>((nd_epw-1)*nd_bpe))&nd_mask0;
355: for ( i = 0; i < nd_wpd; i++ )
356: d[i] = ((d[i]<<nd_bpe)&mask)
357: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
358: return h;
359: }
360: } else {
361: /* do nothing */
362: return d[nd_wpd-1];
363: }
364: }
365:
1.1 noro 366: void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d)
367: {
368: unsigned int t1,t2,u,u1,u2;
369: int i,j;
370:
371: switch ( nd_bpe ) {
372: case 4:
373: for ( i = 0; i < nd_wpd; i++ ) {
374: u1 = d1[i]; u2 = d2[i];
375: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
376: t1 = (u1&0xf000000); t2 = (u2&0xf000000); u |= t1>t2?t1:t2;
377: t1 = (u1&0xf00000); t2 = (u2&0xf00000); u |= t1>t2?t1:t2;
378: t1 = (u1&0xf0000); t2 = (u2&0xf0000); u |= t1>t2?t1:t2;
379: t1 = (u1&0xf000); t2 = (u2&0xf000); u |= t1>t2?t1:t2;
380: t1 = (u1&0xf00); t2 = (u2&0xf00); u |= t1>t2?t1:t2;
381: t1 = (u1&0xf0); t2 = (u2&0xf0); u |= t1>t2?t1:t2;
382: t1 = (u1&0xf); t2 = (u2&0xf); u |= t1>t2?t1:t2;
383: d[i] = u;
384: }
385: break;
386: case 6:
387: for ( i = 0; i < nd_wpd; i++ ) {
388: u1 = d1[i]; u2 = d2[i];
389: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
390: t1 = (u1&0xfc0000); t2 = (u2&0xfc0000); u |= t1>t2?t1:t2;
391: t1 = (u1&0x3f000); t2 = (u2&0x3f000); u |= t1>t2?t1:t2;
392: t1 = (u1&0xfc0); t2 = (u2&0xfc0); u |= t1>t2?t1:t2;
393: t1 = (u1&0x3f); t2 = (u2&0x3f); u |= t1>t2?t1:t2;
394: d[i] = u;
395: }
396: break;
397: case 8:
398: for ( i = 0; i < nd_wpd; i++ ) {
399: u1 = d1[i]; u2 = d2[i];
400: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
401: t1 = (u1&0xff0000); t2 = (u2&0xff0000); u |= t1>t2?t1:t2;
402: t1 = (u1&0xff00); t2 = (u2&0xff00); u |= t1>t2?t1:t2;
403: t1 = (u1&0xff); t2 = (u2&0xff); u |= t1>t2?t1:t2;
404: d[i] = u;
405: }
406: break;
407: case 16:
408: for ( i = 0; i < nd_wpd; i++ ) {
409: u1 = d1[i]; u2 = d2[i];
410: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
411: t1 = (u1&0xffff); t2 = (u2&0xffff); u |= t1>t2?t1:t2;
412: d[i] = u;
413: }
414: break;
415: case 32:
416: for ( i = 0; i < nd_wpd; i++ ) {
417: u1 = d1[i]; u2 = d2[i];
418: d[i] = u1>u2?u1:u2;
419: }
420: break;
421: default:
422: for ( i = 0; i < nd_wpd; i++ ) {
423: u1 = d1[i]; u2 = d2[i];
424: for ( j = 0, u = 0; j < nd_epw; j++ ) {
425: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
426: }
427: d[i] = u;
428: }
429: break;
430: }
431: }
432:
433: int ndl_td(unsigned int *d)
434: {
435: unsigned int t,u;
436: int i,j;
437:
438: for ( t = 0, i = 0; i < nd_wpd; i++ ) {
439: u = d[i];
440: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
441: t += (u&nd_mask0);
442: }
443: return t;
444: }
445:
446: INLINE int ndl_compare(unsigned int *d1,unsigned int *d2)
447: {
448: int i;
449:
450: for ( i = 0; i < nd_wpd; i++, d1++, d2++ )
451: if ( *d1 > *d2 )
452: return is_rlex ? -1 : 1;
453: else if ( *d1 < *d2 )
454: return is_rlex ? 1 : -1;
455: return 0;
456: }
457:
458: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2)
459: {
460: int i;
461:
462: for ( i = 0; i < nd_wpd; i++ )
463: if ( d1[i] != d2[i] )
464: return 0;
465: return 1;
466: }
467:
1.6 noro 468: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2)
469: {
470: int i;
471:
472: switch ( nd_wpd ) {
473: case 1:
474: d2[0] = d1[0];
475: break;
476: case 2:
477: d2[0] = d1[0];
478: d2[1] = d1[1];
479: break;
480: default:
481: for ( i = 0; i < nd_wpd; i++ )
482: d2[i] = d1[i];
483: break;
484: }
485: }
486:
1.1 noro 487: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d)
488: {
489: int i;
490:
1.6 noro 491: switch ( nd_wpd ) {
492: case 1:
493: d[0] = d1[0]+d2[0];
494: break;
495: case 2:
496: d[0] = d1[0]+d2[0];
497: d[1] = d1[1]+d2[1];
498: break;
499: default:
500: for ( i = 0; i < nd_wpd; i++ )
501: d[i] = d1[i]+d2[i];
502: break;
503: }
504: }
505:
506: INLINE void ndl_add2(unsigned int *d1,unsigned int *d2)
507: {
508: int i;
509:
510: switch ( nd_wpd ) {
511: case 1:
512: d2[0] += d1[0];
513: break;
514: case 2:
515: d2[0] += d1[0];
516: d2[1] += d1[1];
517: break;
518: default:
519: for ( i = 0; i < nd_wpd; i++ )
520: d2[i] += d1[i];
521: break;
1.1 noro 522: }
523: }
524:
525: void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d)
526: {
527: int i;
528:
529: for ( i = 0; i < nd_wpd; i++ )
530: d[i] = d1[i]-d2[i];
531: }
532:
533: int ndl_disjoint(unsigned int *d1,unsigned int *d2)
534: {
535: unsigned int t1,t2,u,u1,u2;
536: int i,j;
537:
538: switch ( nd_bpe ) {
539: case 4:
540: for ( i = 0; i < nd_wpd; i++ ) {
541: u1 = d1[i]; u2 = d2[i];
542: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
543: t1 = u1&0xf000000; t2 = u2&0xf000000; if ( t1&&t2 ) return 0;
544: t1 = u1&0xf00000; t2 = u2&0xf00000; if ( t1&&t2 ) return 0;
545: t1 = u1&0xf0000; t2 = u2&0xf0000; if ( t1&&t2 ) return 0;
546: t1 = u1&0xf000; t2 = u2&0xf000; if ( t1&&t2 ) return 0;
547: t1 = u1&0xf00; t2 = u2&0xf00; if ( t1&&t2 ) return 0;
548: t1 = u1&0xf0; t2 = u2&0xf0; if ( t1&&t2 ) return 0;
549: t1 = u1&0xf; t2 = u2&0xf; if ( t1&&t2 ) return 0;
550: }
551: return 1;
552: break;
553: case 6:
554: for ( i = 0; i < nd_wpd; i++ ) {
555: u1 = d1[i]; u2 = d2[i];
556: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
557: t1 = u1&0xfc0000; t2 = u2&0xfc0000; if ( t1&&t2 ) return 0;
558: t1 = u1&0x3f000; t2 = u2&0x3f000; if ( t1&&t2 ) return 0;
559: t1 = u1&0xfc0; t2 = u2&0xfc0; if ( t1&&t2 ) return 0;
560: t1 = u1&0x3f; t2 = u2&0x3f; if ( t1&&t2 ) return 0;
561: }
562: return 1;
563: break;
564: case 8:
565: for ( i = 0; i < nd_wpd; i++ ) {
566: u1 = d1[i]; u2 = d2[i];
567: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
568: t1 = u1&0xff0000; t2 = u2&0xff0000; if ( t1&&t2 ) return 0;
569: t1 = u1&0xff00; t2 = u2&0xff00; if ( t1&&t2 ) return 0;
570: t1 = u1&0xff; t2 = u2&0xff; if ( t1&&t2 ) return 0;
571: }
572: return 1;
573: break;
574: case 16:
575: for ( i = 0; i < nd_wpd; i++ ) {
576: u1 = d1[i]; u2 = d2[i];
577: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
578: t1 = u1&0xffff; t2 = u2&0xffff; if ( t1&&t2 ) return 0;
579: }
580: return 1;
581: break;
582: case 32:
583: for ( i = 0; i < nd_wpd; i++ )
584: if ( d1[i] && d2[i] ) return 0;
585: return 1;
586: break;
587: default:
588: for ( i = 0; i < nd_wpd; i++ ) {
589: u1 = d1[i]; u2 = d2[i];
590: for ( j = 0; j < nd_epw; j++ ) {
591: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
592: u1 >>= nd_bpe; u2 >>= nd_bpe;
593: }
594: }
595: return 1;
596: break;
597: }
598: }
599:
1.5 noro 600: int ndl_check_bound2(int index,unsigned int *d2)
1.1 noro 601: {
1.5 noro 602: unsigned int u2;
603: unsigned int *d1;
604: int i,j,ind,k;
1.1 noro 605:
1.5 noro 606: d1 = nd_bound[index];
607: ind = 0;
608: switch ( nd_bpe ) {
609: case 4:
610: for ( i = 0; i < nd_wpd; i++ ) {
611: u2 = d2[i];
612: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
613: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
614: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
615: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
616: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
617: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
618: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
619: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
620: }
621: return 0;
622: break;
623: case 6:
624: for ( i = 0; i < nd_wpd; i++ ) {
625: u2 = d2[i];
626: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
627: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
628: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
629: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
630: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
631: }
632: return 0;
633: break;
634: case 8:
635: for ( i = 0; i < nd_wpd; i++ ) {
636: u2 = d2[i];
637: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
638: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
639: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
640: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
641: }
642: return 0;
643: break;
644: case 16:
645: for ( i = 0; i < nd_wpd; i++ ) {
646: u2 = d2[i];
647: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
648: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
649: }
650: return 0;
651: break;
652: case 32:
653: for ( i = 0; i < nd_wpd; i++ )
654: if ( d1[i]+d2[i]<d1[i] ) return 1;
655: return 0;
656: break;
657: default:
658: for ( i = 0; i < nd_wpd; i++ ) {
659: u2 = d2[i];
660: k = (nd_epw-1)*nd_bpe;
661: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
662: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
663: }
664: return 0;
665: break;
666: }
1.1 noro 667: }
668:
1.23 noro 669: int ndl_check_bound2_direct(unsigned int *d1,unsigned int *d2)
670: {
671: unsigned int u1,u2;
672: int i,j,k;
673:
674: switch ( nd_bpe ) {
675: case 4:
676: for ( i = 0; i < nd_wpd; i++ ) {
677: u1 = d1[i]; u2 = d2[i];
678: if ( ((u1>>28)&0xf)+((u2>>28)&0xf) >= 0x10 ) return 1;
679: if ( ((u1>>24)&0xf)+((u2>>24)&0xf) >= 0x10 ) return 1;
680: if ( ((u1>>20)&0xf)+((u2>>20)&0xf) >= 0x10 ) return 1;
681: if ( ((u1>>16)&0xf)+((u2>>16)&0xf) >= 0x10 ) return 1;
682: if ( ((u1>>12)&0xf)+((u2>>12)&0xf) >= 0x10 ) return 1;
683: if ( ((u1>>8)&0xf)+((u2>>8)&0xf) >= 0x10 ) return 1;
684: if ( ((u1>>4)&0xf)+((u2>>4)&0xf) >= 0x10 ) return 1;
685: if ( (u1&0xf)+(u2&0xf) >= 0x10 ) return 1;
686: }
687: return 0;
688: break;
689: case 6:
690: for ( i = 0; i < nd_wpd; i++ ) {
691: u1 = d1[i]; u2 = d2[i];
692: if ( ((u1>>24)&0x3f)+((u2>>24)&0x3f) >= 0x40 ) return 1;
693: if ( ((u1>>18)&0x3f)+((u2>>18)&0x3f) >= 0x40 ) return 1;
694: if ( ((u1>>12)&0x3f)+((u2>>12)&0x3f) >= 0x40 ) return 1;
695: if ( ((u1>>6)&0x3f)+((u2>>6)&0x3f) >= 0x40 ) return 1;
696: if ( (u1&0x3f)+(u2&0x3f) >= 0x40 ) return 1;
697: }
698: return 0;
699: break;
700: case 8:
701: for ( i = 0; i < nd_wpd; i++ ) {
702: u1 = d1[i]; u2 = d2[i];
703: if ( ((u1>>24)&0xff)+((u2>>24)&0xff) >= 0x100 ) return 1;
704: if ( ((u1>>16)&0xff)+((u2>>16)&0xff) >= 0x100 ) return 1;
705: if ( ((u1>>8)&0xff)+((u2>>8)&0xff) >= 0x100 ) return 1;
706: if ( (u1&0xff)+(u2&0xff) >= 0x100 ) return 1;
707: }
708: return 0;
709: break;
710: case 16:
711: for ( i = 0; i < nd_wpd; i++ ) {
712: u1 = d1[i]; u2 = d2[i];
713: if ( ((u1>>16)&0xffff)+((u2>>16)&0xffff) > 0x10000 ) return 1;
714: if ( (u2&0xffff)+(u2&0xffff) > 0x10000 ) return 1;
715: }
716: return 0;
717: break;
718: case 32:
719: for ( i = 0; i < nd_wpd; i++ )
720: if ( d1[i]+d2[i]<d1[i] ) return 1;
721: return 0;
722: break;
723: default:
724: for ( i = 0; i < nd_wpd; i++ ) {
725: u1 = d1[i]; u2 = d2[i];
726: k = (nd_epw-1)*nd_bpe;
727: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
728: if ( ((u1>>k)&nd_mask0)+((u2>>k)&nd_mask0) > nd_mask0 )
729: return 1;
730: }
731: return 0;
732: break;
733: }
734: }
735:
1.6 noro 736: INLINE int ndl_hash_value(int td,unsigned int *d)
1.1 noro 737: {
738: int i;
739: int r;
740:
741: r = td;
742: for ( i = 0; i < nd_wpd; i++ )
743: r = ((r<<16)+d[i])%REDTAB_LEN;
744: return r;
745: }
746:
1.9 noro 747: INLINE int nd_find_reducer(ND g)
1.1 noro 748: {
1.13 noro 749: RHist r;
1.6 noro 750: int d,k,i;
1.1 noro 751:
752: d = ndl_hash_value(HTD(g),HDL(g));
1.13 noro 753: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.14 noro 754: if ( HTD(g) == TD(r) && ndl_equal(HDL(g),DL(r)) ) {
1.1 noro 755: if ( k > 0 ) nd_notfirst++;
756: nd_found++;
1.13 noro 757: return r->index;
1.1 noro 758: }
759: }
760:
1.13 noro 761: if ( Reverse )
762: for ( i = nd_psn-1; i >= 0; i-- ) {
763: r = nd_psh[i];
1.14 noro 764: if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13 noro 765: nd_create++;
766: nd_append_red(HDL(g),HTD(g),i);
767: return i;
768: }
769: }
770: else
771: for ( i = 0; i < nd_psn; i++ ) {
772: r = nd_psh[i];
1.14 noro 773: if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13 noro 774: nd_create++;
775: nd_append_red(HDL(g),HTD(g),i);
776: return i;
777: }
1.1 noro 778: }
1.6 noro 779: return -1;
1.1 noro 780: }
781:
1.23 noro 782: INLINE int nd_find_reducer_direct(ND g,NDV *ps,int len)
783: {
784: int i;
785: NDV r;
786:
787: if ( Reverse )
788: for ( i = len-1; i >= 0; i-- ) {
789: r = ps[i];
790: if ( HTD(g) >= HTD(r) && ndl_reducible(HDL(g),HDL(r)) ) {
791: nd_append_red(HDL(g),HTD(g),i);
792: return i;
793: }
794: }
795: else
796: for ( i = 0; i < len; i++ ) {
797: r = ps[i];
798: if ( HTD(g) >= HTD(r) && ndl_reducible(HDL(g),HDL(r)) ) {
799: nd_append_red(HDL(g),HTD(g),i);
800: return i;
801: }
802: }
803: return -1;
804: }
805:
1.19 noro 806: ND nd_add(int mod,ND p1,ND p2)
1.1 noro 807: {
808: int n,c;
809: int t;
810: ND r;
811: NM m1,m2,mr0,mr,s;
812:
813: if ( !p1 )
814: return p2;
815: else if ( !p2 )
816: return p1;
1.25 noro 817: else if ( !mod )
818: return nd_add_q(p1,p2);
1.1 noro 819: else {
820: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.14 noro 821: if ( TD(m1) > TD(m2) )
1.1 noro 822: c = 1;
1.14 noro 823: else if ( TD(m1) < TD(m2) )
1.1 noro 824: c = -1;
825: else
1.14 noro 826: c = ndl_compare(DL(m1),DL(m2));
1.1 noro 827: switch ( c ) {
828: case 0:
1.19 noro 829: t = ((CM(m1))+(CM(m2))) - mod;
1.1 noro 830: if ( t < 0 )
1.19 noro 831: t += mod;
1.1 noro 832: s = m1; m1 = NEXT(m1);
833: if ( t ) {
1.14 noro 834: NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1 noro 835: } else {
836: FREENM(s);
837: }
838: s = m2; m2 = NEXT(m2); FREENM(s);
839: break;
840: case 1:
841: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
842: break;
843: case -1:
844: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
845: break;
846: }
847: }
848: if ( !mr0 )
849: if ( m1 )
850: mr0 = m1;
851: else if ( m2 )
852: mr0 = m2;
853: else
854: return 0;
855: else if ( m1 )
856: NEXT(mr) = m1;
857: else if ( m2 )
858: NEXT(mr) = m2;
859: else
860: NEXT(mr) = 0;
861: BDY(p1) = mr0;
1.14 noro 862: SG(p1) = MAX(SG(p1),SG(p2));
1.1 noro 863: FREEND(p2);
864: return p1;
865: }
866: }
867:
1.17 noro 868: ND nd_add_q(ND p1,ND p2)
869: {
870: int n,c;
871: ND r;
872: NM m1,m2,mr0,mr,s;
873: Q t;
874:
875: if ( !p1 )
876: return p2;
877: else if ( !p2 )
878: return p1;
879: else {
880: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
881: if ( TD(m1) > TD(m2) )
882: c = 1;
883: else if ( TD(m1) < TD(m2) )
884: c = -1;
885: else
886: c = ndl_compare(DL(m1),DL(m2));
887: switch ( c ) {
888: case 0:
889: addq(CQ(m1),CQ(m2),&t);
890: s = m1; m1 = NEXT(m1);
891: if ( t ) {
892: NEXTNM2(mr0,mr,s); CQ(mr) = (t);
893: } else {
894: FREENM(s);
895: }
896: s = m2; m2 = NEXT(m2); FREENM(s);
897: break;
898: case 1:
899: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
900: break;
901: case -1:
902: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
903: break;
904: }
905: }
906: if ( !mr0 )
907: if ( m1 )
908: mr0 = m1;
909: else if ( m2 )
910: mr0 = m2;
911: else
912: return 0;
913: else if ( m1 )
914: NEXT(mr) = m1;
915: else if ( m2 )
916: NEXT(mr) = m2;
917: else
918: NEXT(mr) = 0;
919: BDY(p1) = mr0;
920: SG(p1) = MAX(SG(p1),SG(p2));
921: FREEND(p2);
922: return p1;
923: }
924: }
925:
1.28 noro 926: #if !USE_GEOBUCKET
1.1 noro 927: /* ret=1 : success, ret=0 : overflow */
1.16 noro 928: int nd_nf(int mod,ND g,int full,ND *rp)
1.1 noro 929: {
1.11 noro 930: ND d;
1.1 noro 931: NM m,mrd,tail;
1.7 noro 932: NM mul;
1.10 noro 933: int n,sugar,psugar,sugar0,stat,index;
1.6 noro 934: int c,c1,c2;
1.17 noro 935: RHist h;
1.11 noro 936: NDV p,red;
1.16 noro 937: Q cg,cred,gcd;
1.21 noro 938: double hmag;
1.1 noro 939:
940: if ( !g ) {
941: *rp = 0;
942: return 1;
943: }
1.21 noro 944: if ( !mod )
945: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
946:
1.14 noro 947: sugar0 = sugar = SG(g);
1.1 noro 948: n = NV(g);
1.7 noro 949: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.1 noro 950: for ( d = 0; g; ) {
1.6 noro 951: index = nd_find_reducer(g);
952: if ( index >= 0 ) {
1.17 noro 953: h = nd_psh[index];
954: ndl_sub(HDL(g),DL(h),DL(mul));
955: TD(mul) = HTD(g)-TD(h);
1.10 noro 956: #if 0
1.14 noro 957: if ( d && (SG(p)+TD(mul)) > sugar ) {
1.10 noro 958: goto afo;
959: }
960: #endif
1.14 noro 961: if ( ndl_check_bound2(index,DL(mul)) ) {
1.6 noro 962: nd_free(g); nd_free(d);
963: return 0;
964: }
1.16 noro 965: if ( mod ) {
1.17 noro 966: p = nd_ps[index];
1.19 noro 967: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
968: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.16 noro 969: } else {
1.20 noro 970: p = nd_psq[index];
1.17 noro 971: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1.16 noro 972: chsgnq(cg,&CQ(mul));
1.20 noro 973: nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1.16 noro 974: }
1.25 noro 975: g = nd_add(mod,g,ndv_mul_nm(mod,p,mul));
976: sugar = MAX(sugar,SG(p)+TD(mul));
1.22 noro 977: if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1.21 noro 978: nd_removecont2(d,g);
979: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
980: }
1.1 noro 981: } else if ( !full ) {
982: *rp = g;
983: return 1;
984: } else {
1.10 noro 985: afo:
1.1 noro 986: m = BDY(g);
987: if ( NEXT(m) ) {
988: BDY(g) = NEXT(m); NEXT(m) = 0;
989: } else {
990: FREEND(g); g = 0;
991: }
992: if ( d ) {
993: NEXT(tail)=m;
994: tail=m;
995: } else {
996: MKND(n,m,d);
997: tail = BDY(d);
998: }
999: }
1000: }
1001: if ( d )
1.14 noro 1002: SG(d) = sugar;
1.1 noro 1003: *rp = d;
1004: return 1;
1005: }
1.28 noro 1006:
1007: int nd_nf_direct(int mod,ND g,NDV *ps,int len,int full,ND *rp)
1008: {
1009: ND d;
1010: NM m,mrd,tail;
1011: NM mul;
1012: int n,sugar,psugar,sugar0,stat,index;
1013: int c,c1,c2;
1014: RHist h;
1015: NDV p,red;
1016: Q cg,cred,gcd;
1017: double hmag;
1018:
1019: if ( !g ) {
1020: *rp = 0;
1021: return 1;
1022: }
1023: #if 0
1024: if ( !mod )
1025: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1026: #else
1027: /* XXX */
1028: hmag = 0;
1029: #endif
1030:
1031: sugar0 = sugar = SG(g);
1032: n = NV(g);
1033: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1034: for ( d = 0; g; ) {
1035: index = nd_find_reducer_direct(g,ps,len);
1036: if ( index >= 0 ) {
1037: p = ps[index];
1038: ndl_sub(HDL(g),HDL(p),DL(mul));
1039: TD(mul) = HTD(g)-HTD(p);
1040: if ( ndl_check_bound2_direct(HDL(p),DL(mul)) ) {
1041: nd_free(g); nd_free(d);
1042: return 0;
1043: }
1044: if ( mod ) {
1045: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1046: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1047: } else {
1048: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1049: chsgnq(cg,&CQ(mul));
1050: nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1051: }
1052: g = nd_add(mod,g,ndv_mul_nm(mod,p,mul));
1053: sugar = MAX(sugar,SG(p)+TD(mul));
1054: if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1055: nd_removecont2(d,g);
1056: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1057: }
1058: } else if ( !full ) {
1059: *rp = g;
1060: return 1;
1061: } else {
1062: m = BDY(g);
1063: if ( NEXT(m) ) {
1064: BDY(g) = NEXT(m); NEXT(m) = 0;
1065: } else {
1066: FREEND(g); g = 0;
1067: }
1068: if ( d ) {
1069: NEXT(tail)=m;
1070: tail=m;
1071: } else {
1072: MKND(n,m,d);
1073: tail = BDY(d);
1074: }
1075: }
1076: }
1077: if ( d )
1078: SG(d) = sugar;
1079: *rp = d;
1080: return 1;
1081: }
1.25 noro 1082: #else
1083: int nd_nf(int mod,ND g,int full,ND *rp)
1084: {
1085: int hindex,index;
1086: NDV p;
1087: ND u,d,red;
1088: NODE l;
1089: NM mul,m,mrd;
1090: int sugar,psugar,n,h_reducible;
1091: PGeoBucket bucket;
1092: int c,c1,c2;
1.26 noro 1093: Q cg,cred,gcd,zzz;
1.25 noro 1094: RHist h;
1.28 noro 1095: double hmag,gmag;
1.25 noro 1096:
1097: if ( !g ) {
1098: *rp = 0;
1099: return 1;
1100: }
1101: sugar = SG(g);
1102: n = NV(g);
1.28 noro 1103: if ( !mod )
1104: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.25 noro 1105: bucket = create_pbucket();
1106: add_pbucket(mod,bucket,g,nd_length(g));
1107: d = 0;
1108: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1109: while ( 1 ) {
1.26 noro 1110: hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.25 noro 1111: if ( hindex < 0 ) {
1112: if ( d )
1113: SG(d) = sugar;
1114: *rp = d;
1115: return 1;
1116: }
1117: g = bucket->body[hindex];
1118: index = nd_find_reducer(g);
1119: if ( index >= 0 ) {
1120: h = nd_psh[index];
1121: ndl_sub(HDL(g),DL(h),DL(mul));
1122: TD(mul) = HTD(g)-TD(h);
1123: if ( ndl_check_bound2(index,DL(mul)) ) {
1.26 noro 1124: nd_free(d);
1.25 noro 1125: free_pbucket(bucket);
1126: *rp = 0;
1127: return 0;
1128: }
1129: if ( mod ) {
1130: p = nd_ps[index];
1131: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1132: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1133: } else {
1134: p = nd_psq[index];
1135: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1136: chsgnq(cg,&CQ(mul));
1.26 noro 1137: nd_mul_c_q(d,cred);
1138: mulq_pbucket(bucket,cred);
1139: g = bucket->body[hindex];
1.28 noro 1140: gmag = (double)p_mag((P)HCQ(g));
1.25 noro 1141: }
1142: red = ndv_mul_nm(mod,p,mul);
1143: bucket->body[hindex] = nd_remove_head(g);
1144: red = nd_remove_head(red);
1.26 noro 1145: add_pbucket(mod,bucket,red,LEN(p)-1);
1.25 noro 1146: sugar = MAX(sugar,SG(p)+TD(mul));
1.28 noro 1147: if ( !mod && hmag && (gmag > hmag) ) {
1148: g = normalize_pbucket(mod,bucket);
1149: if ( !g ) {
1150: if ( d )
1151: SG(d) = sugar;
1152: *rp = d;
1153: return 1;
1154: }
1155: nd_removecont2(d,g);
1156: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1157: add_pbucket(mod,bucket,g,nd_length(g)-1);
1158: }
1.25 noro 1159: } else if ( !full ) {
1160: g = normalize_pbucket(mod,bucket);
1161: if ( g )
1162: SG(g) = sugar;
1163: *rp = g;
1164: return 1;
1165: } else {
1166: m = BDY(g);
1167: if ( NEXT(m) ) {
1168: BDY(g) = NEXT(m); NEXT(m) = 0;
1169: } else {
1170: FREEND(g); g = 0;
1171: }
1172: bucket->body[hindex] = g;
1173: NEXT(m) = 0;
1174: if ( d ) {
1175: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1176: NEXT(mrd) = m;
1177: } else {
1178: MKND(n,m,d);
1179: }
1180: }
1181: }
1182: }
1.27 noro 1183:
1.23 noro 1184: int nd_nf_direct(int mod,ND g,NDV *ps,int len,int full,ND *rp)
1185: {
1.28 noro 1186: int hindex,index;
1187: NDV p;
1188: ND u,d,red;
1189: NODE l;
1190: NM mul,m,mrd;
1191: int sugar,psugar,n,h_reducible;
1192: PGeoBucket bucket;
1.23 noro 1193: int c,c1,c2;
1.28 noro 1194: Q cg,cred,gcd,zzz;
1.23 noro 1195: RHist h;
1.28 noro 1196: double hmag,gmag;
1.23 noro 1197:
1198: if ( !g ) {
1199: *rp = 0;
1200: return 1;
1201: }
1.28 noro 1202: sugar = SG(g);
1203: n = NV(g);
1.23 noro 1204: #if 0
1205: if ( !mod )
1206: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1207: #else
1208: /* XXX */
1209: hmag = 0;
1210: #endif
1.28 noro 1211: bucket = create_pbucket();
1212: add_pbucket(mod,bucket,g,nd_length(g));
1213: d = 0;
1.23 noro 1214: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.28 noro 1215: while ( 1 ) {
1216: hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1217: if ( hindex < 0 ) {
1218: if ( d )
1219: SG(d) = sugar;
1220: *rp = d;
1221: return 1;
1222: }
1223: g = bucket->body[hindex];
1.23 noro 1224: index = nd_find_reducer_direct(g,ps,len);
1225: if ( index >= 0 ) {
1226: p = ps[index];
1227: ndl_sub(HDL(g),HDL(p),DL(mul));
1228: TD(mul) = HTD(g)-HTD(p);
1229: if ( ndl_check_bound2_direct(HDL(p),DL(mul)) ) {
1.28 noro 1230: nd_free(d);
1231: free_pbucket(bucket);
1232: *rp = 0;
1.23 noro 1233: return 0;
1234: }
1.28 noro 1235: if ( mod ) {
1.23 noro 1236: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1237: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1238: } else {
1239: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1240: chsgnq(cg,&CQ(mul));
1.28 noro 1241: nd_mul_c_q(d,cred);
1242: mulq_pbucket(bucket,cred);
1243: g = bucket->body[hindex];
1244: gmag = (double)p_mag((P)HCQ(g));
1.23 noro 1245: }
1.28 noro 1246: red = ndv_mul_nm(mod,p,mul);
1247: bucket->body[hindex] = nd_remove_head(g);
1248: red = nd_remove_head(red);
1249: add_pbucket(mod,bucket,red,LEN(p)-1);
1.25 noro 1250: sugar = MAX(sugar,SG(p)+TD(mul));
1.28 noro 1251: if ( !mod && hmag && (gmag > hmag) ) {
1252: g = normalize_pbucket(mod,bucket);
1253: if ( !g ) {
1254: if ( d )
1255: SG(d) = sugar;
1256: *rp = d;
1257: return 1;
1258: }
1.23 noro 1259: nd_removecont2(d,g);
1260: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.28 noro 1261: add_pbucket(mod,bucket,g,nd_length(g)-1);
1.23 noro 1262: }
1263: } else if ( !full ) {
1.28 noro 1264: g = normalize_pbucket(mod,bucket);
1265: if ( g )
1266: SG(g) = sugar;
1.23 noro 1267: *rp = g;
1268: return 1;
1269: } else {
1270: m = BDY(g);
1271: if ( NEXT(m) ) {
1272: BDY(g) = NEXT(m); NEXT(m) = 0;
1273: } else {
1274: FREEND(g); g = 0;
1275: }
1.28 noro 1276: bucket->body[hindex] = g;
1277: NEXT(m) = 0;
1.23 noro 1278: if ( d ) {
1.28 noro 1279: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1280: NEXT(mrd) = m;
1.23 noro 1281: } else {
1282: MKND(n,m,d);
1283: }
1284: }
1285: }
1.28 noro 1286: }
1287: #endif
1288:
1289: /* input : list of DP, cand : list of DP */
1290:
1291: int nd_check_candidate(NODE input,NODE cand)
1292: {
1293: int n,i,stat;
1294: ND nf,d;
1295: NODE t;
1296:
1297: nd_setup(0,cand);
1298:
1299: /* membercheck : list is a subset of Id(cand) ? */
1300: for ( t = input; t; t = NEXT(t) ) {
1301: d = dptond(0,(DP)BDY(t));
1302: stat = nd_nf_direct(0,d,nd_psq,n,0,&nf);
1303: if ( !stat )
1304: nd_reconstruct_direct(0,nd_psq,n);
1305: else if ( nf )
1306: return 0;
1307: }
1308: /* gbcheck : cand is a GB of Id(cand) ? */
1309: if ( !nd_gb(0,1) )
1310: return 0;
1311: /* XXX */
1.23 noro 1312: return 1;
1313: }
1.1 noro 1314:
1315: ND nd_remove_head(ND p)
1316: {
1317: NM m;
1318:
1319: m = BDY(p);
1320: if ( !NEXT(m) ) {
1321: FREEND(p);
1322: p = 0;
1323: } else
1324: BDY(p) = NEXT(m);
1325: FREENM(m);
1326: return p;
1327: }
1328:
1329: PGeoBucket create_pbucket()
1330: {
1331: PGeoBucket g;
1332:
1333: g = CALLOC(1,sizeof(struct oPGeoBucket));
1334: g->m = -1;
1335: return g;
1336: }
1337:
1.25 noro 1338: void free_pbucket(PGeoBucket b) {
1339: int i;
1340:
1.26 noro 1341: for ( i = 0; i <= b->m; i++ )
1.25 noro 1342: if ( b->body[i] ) {
1343: nd_free(b->body[i]);
1344: b->body[i] = 0;
1345: }
1346: GC_free(b);
1347: }
1348:
1349: void add_pbucket(int mod,PGeoBucket g,ND d,int l)
1.1 noro 1350: {
1.25 noro 1351: int k,m;
1.1 noro 1352:
1.29 ! noro 1353: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
! 1354: /* 2^(k-1) < l <= 2^k (=m) */
1.19 noro 1355: d = nd_add(mod,g->body[k],d);
1.29 ! noro 1356: for ( ; d && nd_length(d) > m; k++, m <<= 1 ) {
1.1 noro 1357: g->body[k] = 0;
1.25 noro 1358: d = nd_add(mod,g->body[k+1],d);
1.1 noro 1359: }
1360: g->body[k] = d;
1361: g->m = MAX(g->m,k);
1362: }
1363:
1.26 noro 1364: void mulq_pbucket(PGeoBucket g,Q c)
1365: {
1366: int k;
1367:
1368: for ( k = 0; k <= g->m; k++ )
1369: nd_mul_c_q(g->body[k],c);
1370: }
1371:
1.25 noro 1372: /* XXX not completed */
1.19 noro 1373: int head_pbucket(int mod,PGeoBucket g)
1.1 noro 1374: {
1375: int j,i,c,k,nv,sum;
1376: unsigned int *di,*dj;
1377: ND gi,gj;
1378:
1379: k = g->m;
1380: while ( 1 ) {
1381: j = -1;
1382: for ( i = 0; i <= k; i++ ) {
1383: if ( !(gi = g->body[i]) )
1384: continue;
1385: if ( j < 0 ) {
1386: j = i;
1387: gj = g->body[j];
1388: dj = HDL(gj);
1.14 noro 1389: sum = HCM(gj);
1.1 noro 1390: } else {
1391: di = HDL(gi);
1392: nv = NV(gi);
1393: if ( HTD(gi) > HTD(gj) )
1394: c = 1;
1395: else if ( HTD(gi) < HTD(gj) )
1396: c = -1;
1397: else
1398: c = ndl_compare(di,dj);
1399: if ( c > 0 ) {
1400: if ( sum )
1.14 noro 1401: HCM(gj) = sum;
1.1 noro 1402: else
1403: g->body[j] = nd_remove_head(gj);
1404: j = i;
1405: gj = g->body[j];
1406: dj = HDL(gj);
1.14 noro 1407: sum = HCM(gj);
1.1 noro 1408: } else if ( c == 0 ) {
1.19 noro 1409: sum = sum+HCM(gi)-mod;
1.1 noro 1410: if ( sum < 0 )
1.19 noro 1411: sum += mod;
1.1 noro 1412: g->body[i] = nd_remove_head(gi);
1413: }
1414: }
1415: }
1416: if ( j < 0 )
1417: return -1;
1418: else if ( sum ) {
1.14 noro 1419: HCM(gj) = sum;
1.26 noro 1420: return j;
1421: } else
1422: g->body[j] = nd_remove_head(gj);
1423: }
1424: }
1425:
1426: int head_pbucket_q(PGeoBucket g)
1427: {
1428: int j,i,c,k,nv;
1429: Q sum,t;
1430: unsigned int *di,*dj;
1431: ND gi,gj;
1432:
1433: k = g->m;
1434: while ( 1 ) {
1435: j = -1;
1436: for ( i = 0; i <= k; i++ ) {
1437: if ( !(gi = g->body[i]) )
1438: continue;
1439: if ( j < 0 ) {
1440: j = i;
1441: gj = g->body[j];
1442: dj = HDL(gj);
1443: sum = HCQ(gj);
1444: } else {
1445: di = HDL(gi);
1446: nv = NV(gi);
1447: if ( HTD(gi) > HTD(gj) )
1448: c = 1;
1449: else if ( HTD(gi) < HTD(gj) )
1450: c = -1;
1451: else
1452: c = ndl_compare(di,dj);
1453: if ( c > 0 ) {
1454: if ( sum )
1455: HCQ(gj) = sum;
1456: else
1457: g->body[j] = nd_remove_head(gj);
1458: j = i;
1459: gj = g->body[j];
1460: dj = HDL(gj);
1461: sum = HCQ(gj);
1462: } else if ( c == 0 ) {
1463: addq(sum,HCQ(gi),&t);
1464: sum = t;
1465: g->body[i] = nd_remove_head(gi);
1466: }
1467: }
1468: }
1469: if ( j < 0 )
1470: return -1;
1471: else if ( sum ) {
1472: HCQ(gj) = sum;
1.1 noro 1473: return j;
1474: } else
1475: g->body[j] = nd_remove_head(gj);
1476: }
1477: }
1478:
1.25 noro 1479: ND normalize_pbucket(int mod,PGeoBucket g)
1.1 noro 1480: {
1481: int i;
1482: ND r,t;
1483:
1484: r = 0;
1.28 noro 1485: for ( i = 0; i <= g->m; i++ ) {
1.19 noro 1486: r = nd_add(mod,r,g->body[i]);
1.28 noro 1487: g->body[i] = 0;
1488: }
1489: g->m = -1;
1.1 noro 1490: return r;
1491: }
1492:
1.27 noro 1493: /* return value = 0 => input is not a GB */
1494:
1495: NODE nd_gb(int m,int checkonly)
1.1 noro 1496: {
1497: int i,nh,sugar,stat;
1.23 noro 1498: NODE r,g,t;
1.1 noro 1499: ND_pairs d;
1500: ND_pairs l;
1501: ND h,nf;
1502:
1.23 noro 1503: g = 0; d = 0;
1504: for ( i = 0; i < nd_psn; i++ ) {
1.1 noro 1505: d = update_pairs(d,g,i);
1506: g = update_base(g,i);
1507: }
1508: sugar = 0;
1509: while ( d ) {
1510: again:
1511: l = nd_minp(d,&d);
1.14 noro 1512: if ( SG(l) != sugar ) {
1513: sugar = SG(l);
1.1 noro 1514: fprintf(asir_out,"%d",sugar);
1515: }
1.16 noro 1516: stat = nd_sp(m,l,&h);
1.1 noro 1517: if ( !stat ) {
1518: NEXT(l) = d; d = l;
1.20 noro 1519: d = nd_reconstruct(m,0,d);
1.1 noro 1520: goto again;
1521: }
1.16 noro 1522: stat = nd_nf(m,h,!Top,&nf);
1.1 noro 1523: if ( !stat ) {
1524: NEXT(l) = d; d = l;
1.20 noro 1525: d = nd_reconstruct(m,0,d);
1.1 noro 1526: goto again;
1527: } else if ( nf ) {
1.27 noro 1528: if ( checkonly ) return 0;
1.1 noro 1529: printf("+"); fflush(stdout);
1.16 noro 1530: nh = nd_newps(m,nf);
1.1 noro 1531: d = update_pairs(d,g,nh);
1532: g = update_base(g,nh);
1533: FREENDP(l);
1534: } else {
1535: printf("."); fflush(stdout);
1536: FREENDP(l);
1537: }
1538: }
1.23 noro 1539: if ( m )
1540: for ( t = g; t; t = NEXT(t) )
1541: BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1542: else
1543: for ( t = g; t; t = NEXT(t) )
1544: BDY(t) = (pointer)nd_psq[(int)BDY(t)];
1.1 noro 1545: return g;
1546: }
1547:
1.23 noro 1548: NODE nd_gb_trace(int m)
1.20 noro 1549: {
1550: int i,nh,sugar,stat;
1.23 noro 1551: NODE r,g,t;
1.20 noro 1552: ND_pairs d;
1553: ND_pairs l;
1554: ND h,nf,nfq;
1555:
1.23 noro 1556: g = 0; d = 0;
1557: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 1558: d = update_pairs(d,g,i);
1559: g = update_base(g,i);
1560: }
1561: sugar = 0;
1562: while ( d ) {
1563: again:
1564: l = nd_minp(d,&d);
1565: if ( SG(l) != sugar ) {
1566: sugar = SG(l);
1567: fprintf(asir_out,"%d",sugar);
1568: }
1569: stat = nd_sp(m,l,&h);
1570: if ( !stat ) {
1571: NEXT(l) = d; d = l;
1572: d = nd_reconstruct(m,1,d);
1573: goto again;
1574: }
1575: stat = nd_nf(m,h,!Top,&nf);
1576: if ( !stat ) {
1577: NEXT(l) = d; d = l;
1578: d = nd_reconstruct(m,1,d);
1579: goto again;
1580: } else if ( nf ) {
1581: /* overflow does not occur */
1582: nd_sp(0,l,&h);
1583: nd_nf(0,h,!Top,&nfq);
1584: if ( nfq ) {
1585: printf("+"); fflush(stdout);
1586: nh = nd_newps_trace(m,nf,nfq);
1.27 noro 1587: /* failure; m|HC(nfq) */
1588: if ( nf < 0 ) return 0;
1.20 noro 1589: d = update_pairs(d,g,nh);
1590: g = update_base(g,nh);
1591: } else {
1592: printf("*"); fflush(stdout);
1593: }
1594: } else {
1595: printf("."); fflush(stdout);
1596: }
1597: FREENDP(l);
1598: }
1.23 noro 1599: for ( t = g; t; t = NEXT(t) )
1600: BDY(t) = (pointer)nd_psq[(int)BDY(t)];
1.20 noro 1601: return g;
1602: }
1603:
1.23 noro 1604: int ndv_compare(NDV *p1,NDV *p2)
1605: {
1606: int td1,td2;
1607:
1608: td1 = HTD(*p1); td2 = HTD(*p2);
1609: if ( td1 > td2 ) return 1;
1610: else if ( td1 < td2 ) return -1;
1611: else return ndl_compare(HDL(*p1),HDL(*p2));
1612: }
1613:
1614: int ndv_compare_rev(NDV *p1,NDV *p2)
1615: {
1616: return -ndv_compare(p1,p2);
1617: }
1618:
1619: NODE nd_reduceall(int m,NODE f)
1620: {
1621: int i,j,n,stat;
1622: NDV *w,*ps;
1623: ND nf,g;
1624: NODE t,a0,a;
1625:
1626: for ( n = 0, t = f; t; t = NEXT(t), n++ );
1627: ps = (NDV *)ALLOCA(n*sizeof(NDV));
1628: for ( i = 0, t = f; i < n; i++, t = NEXT(t) )
1629: ps[i] = (NDV)BDY(t);
1630: qsort(ps,n,sizeof(NDV),(int (*)(const void *,const void *))ndv_compare);
1631: w = (NDV *)ALLOCA((n-1)*sizeof(NDV));
1632: for ( i = 0; i < n; i++ ) {
1633: for ( j = 0; j < i; j++ )
1634: w[j] = (NDV)ps[j];
1635: for ( j = i+1; j < n; j++ )
1636: w[j-1] = ps[j];
1637: g = ndvtond(m,ps[i]);
1638: stat = nd_nf_direct(m,g,w,n-1,1,&nf);
1639: if ( !stat )
1640: nd_reconstruct_direct(m,ps,n);
1641: else if ( !nf ) {
1642: printf("."); fflush(stdout);
1643: ndv_free(ps[i]);
1644: for ( j = i+1; j < n; j++ )
1645: ps[j-1] = ps[j];
1646: n--;
1647: } else {
1648: printf("."); fflush(stdout);
1649: ndv_free(ps[i]);
1.24 noro 1650: nd_removecont(m,nf);
1.23 noro 1651: ps[i] = ndtondv(m,nf);
1652: nd_free(nf);
1653: }
1654: }
1655: for ( a0 = 0, i = 0; i < n; i++ ) {
1656: NEXTNODE(a0,a);
1657: BDY(a) = (pointer)ps[i];
1658: }
1659: NEXT(a) = 0;
1660: return a0;
1661: }
1662:
1.1 noro 1663: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
1664: {
1665: ND_pairs d1,nd,cur,head,prev,remove;
1666:
1667: if ( !g ) return d;
1668: d = crit_B(d,t);
1669: d1 = nd_newpairs(g,t);
1670: d1 = crit_M(d1);
1671: d1 = crit_F(d1);
1672: prev = 0; cur = head = d1;
1673: while ( cur ) {
1674: if ( crit_2( cur->i1,cur->i2 ) ) {
1675: remove = cur;
1676: if ( !prev ) {
1677: head = cur = NEXT(cur);
1678: } else {
1679: cur = NEXT(prev) = NEXT(cur);
1680: }
1681: FREENDP(remove);
1682: } else {
1683: prev = cur;
1684: cur = NEXT(cur);
1685: }
1686: }
1687: if ( !d )
1688: return head;
1689: else {
1690: nd = d;
1691: while ( NEXT(nd) )
1692: nd = NEXT(nd);
1693: NEXT(nd) = head;
1694: return d;
1695: }
1696: }
1697:
1698: ND_pairs nd_newpairs( NODE g, int t )
1699: {
1700: NODE h;
1701: unsigned int *dl;
1702: int td,ts,s;
1703: ND_pairs r,r0;
1704:
1.20 noro 1705: dl = DL(nd_psh[t]);
1706: td = TD(nd_psh[t]);
1707: ts = SG(nd_psh[t]) - td;
1.1 noro 1708: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1709: NEXTND_pairs(r0,r);
1710: r->i1 = (int)BDY(h);
1711: r->i2 = t;
1.20 noro 1712: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.14 noro 1713: TD(r) = ndl_td(r->lcm);
1.20 noro 1714: s = SG(nd_psh[r->i1])-TD(nd_psh[r->i1]);
1.14 noro 1715: SG(r) = MAX(s,ts) + TD(r);
1.1 noro 1716: }
1717: NEXT(r) = 0;
1718: return r0;
1719: }
1720:
1721: ND_pairs crit_B( ND_pairs d, int s )
1722: {
1723: ND_pairs cur,head,prev,remove;
1724: unsigned int *t,*tl,*lcm;
1725: int td,tdl;
1726:
1727: if ( !d ) return 0;
1.20 noro 1728: t = DL(nd_psh[s]);
1.1 noro 1729: prev = 0;
1730: head = cur = d;
1731: lcm = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1732: while ( cur ) {
1733: tl = cur->lcm;
1734: if ( ndl_reducible(tl,t)
1.20 noro 1735: && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
1736: && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1 noro 1737: remove = cur;
1738: if ( !prev ) {
1739: head = cur = NEXT(cur);
1740: } else {
1741: cur = NEXT(prev) = NEXT(cur);
1742: }
1743: FREENDP(remove);
1744: } else {
1745: prev = cur;
1746: cur = NEXT(cur);
1747: }
1748: }
1749: return head;
1750: }
1751:
1752: ND_pairs crit_M( ND_pairs d1 )
1753: {
1754: ND_pairs e,d2,d3,dd,p;
1755: unsigned int *id,*jd;
1756: int itd,jtd;
1757:
1758: for ( dd = 0, e = d1; e; e = d3 ) {
1759: if ( !(d2 = NEXT(e)) ) {
1760: NEXT(e) = dd;
1761: return e;
1762: }
1763: id = e->lcm;
1.14 noro 1764: itd = TD(e);
1.1 noro 1765: for ( d3 = 0; d2; d2 = p ) {
1766: p = NEXT(d2),
1767: jd = d2->lcm;
1.14 noro 1768: jtd = TD(d2);
1.1 noro 1769: if ( jtd == itd )
1770: if ( id == jd );
1771: else if ( ndl_reducible(jd,id) ) continue;
1772: else if ( ndl_reducible(id,jd) ) goto delit;
1773: else ;
1774: else if ( jtd > itd )
1775: if ( ndl_reducible(jd,id) ) continue;
1776: else ;
1777: else if ( ndl_reducible(id,jd ) ) goto delit;
1778: NEXT(d2) = d3;
1779: d3 = d2;
1780: }
1781: NEXT(e) = dd;
1782: dd = e;
1783: continue;
1784: /**/
1785: delit: NEXT(d2) = d3;
1786: d3 = d2;
1787: for ( ; p; p = d2 ) {
1788: d2 = NEXT(p);
1789: NEXT(p) = d3;
1790: d3 = p;
1791: }
1792: FREENDP(e);
1793: }
1794: return dd;
1795: }
1796:
1797: ND_pairs crit_F( ND_pairs d1 )
1798: {
1799: ND_pairs rest, head,remove;
1800: ND_pairs last, p, r, w;
1801: int s;
1802:
1803: for ( head = last = 0, p = d1; NEXT(p); ) {
1804: r = w = equivalent_pairs(p,&rest);
1.14 noro 1805: s = SG(r);
1.1 noro 1806: w = NEXT(w);
1807: while ( w ) {
1808: if ( crit_2(w->i1,w->i2) ) {
1809: r = w;
1810: w = NEXT(w);
1811: while ( w ) {
1812: remove = w;
1813: w = NEXT(w);
1814: FREENDP(remove);
1815: }
1816: break;
1.14 noro 1817: } else if ( SG(w) < s ) {
1.1 noro 1818: FREENDP(r);
1819: r = w;
1.14 noro 1820: s = SG(r);
1.1 noro 1821: w = NEXT(w);
1822: } else {
1823: remove = w;
1824: w = NEXT(w);
1825: FREENDP(remove);
1826: }
1827: }
1828: if ( last ) NEXT(last) = r;
1829: else head = r;
1830: NEXT(last = r) = 0;
1831: p = rest;
1832: if ( !p ) return head;
1833: }
1834: if ( !last ) return p;
1835: NEXT(last) = p;
1836: return head;
1837: }
1838:
1839: int crit_2( int dp1, int dp2 )
1840: {
1.20 noro 1841: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1 noro 1842: }
1843:
1844: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1845: {
1846: ND_pairs w,p,r,s;
1847: unsigned int *d;
1848: int td;
1849:
1850: w = d1;
1851: d = w->lcm;
1.14 noro 1852: td = TD(w);
1.1 noro 1853: s = NEXT(w);
1854: NEXT(w) = 0;
1855: for ( r = 0; s; s = p ) {
1856: p = NEXT(s);
1.14 noro 1857: if ( td == TD(s) && ndl_equal(d,s->lcm) ) {
1.1 noro 1858: NEXT(s) = w;
1859: w = s;
1860: } else {
1861: NEXT(s) = r;
1862: r = s;
1863: }
1864: }
1865: *prest = r;
1866: return w;
1867: }
1868:
1869: NODE update_base(NODE nd,int ndp)
1870: {
1871: unsigned int *dl, *dln;
1872: NODE last, p, head;
1873: int td,tdn;
1874:
1.20 noro 1875: dl = DL(nd_psh[ndp]);
1876: td = TD(nd_psh[ndp]);
1.1 noro 1877: for ( head = last = 0, p = nd; p; ) {
1.20 noro 1878: dln = DL(nd_psh[(int)BDY(p)]);
1879: tdn = TD(nd_psh[(int)BDY(p)]);
1.1 noro 1880: if ( tdn >= td && ndl_reducible( dln, dl ) ) {
1881: p = NEXT(p);
1882: if ( last ) NEXT(last) = p;
1883: } else {
1884: if ( !last ) head = p;
1885: p = NEXT(last = p);
1886: }
1887: }
1888: head = append_one(head,ndp);
1889: return head;
1890: }
1891:
1892: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
1893: {
1894: ND_pairs m,ml,p,l;
1895: unsigned int *lcm;
1896: int s,td,len,tlen,c;
1897:
1898: if ( !(p = NEXT(m = d)) ) {
1899: *prest = p;
1900: NEXT(m) = 0;
1901: return m;
1902: }
1903: lcm = m->lcm;
1.14 noro 1904: s = SG(m);
1905: td = TD(m);
1.6 noro 1906: len = nd_psl[m->i1]+nd_psl[m->i2];
1.1 noro 1907: for ( ml = 0, l = m; p; p = NEXT(l = p) ) {
1.14 noro 1908: if (SG(p) < s)
1.1 noro 1909: goto find;
1.14 noro 1910: else if ( SG(p) == s ) {
1911: if ( TD(p) < td )
1.1 noro 1912: goto find;
1.14 noro 1913: else if ( TD(p) == td ) {
1.1 noro 1914: c = ndl_compare(p->lcm,lcm);
1915: if ( c < 0 )
1916: goto find;
1.10 noro 1917: #if 0
1.1 noro 1918: else if ( c == 0 ) {
1.6 noro 1919: tlen = nd_psl[p->i1]+nd_psl[p->i2];
1.1 noro 1920: if ( tlen < len )
1921: goto find;
1922: }
1.10 noro 1923: #endif
1.1 noro 1924: }
1925: }
1926: continue;
1927: find:
1928: ml = l;
1929: m = p;
1930: lcm = m->lcm;
1.14 noro 1931: s = SG(m);
1932: td = TD(m);
1.1 noro 1933: len = tlen;
1934: }
1935: if ( !ml ) *prest = NEXT(m);
1936: else {
1937: NEXT(ml) = NEXT(m);
1938: *prest = d;
1939: }
1940: NEXT(m) = 0;
1941: return m;
1942: }
1943:
1.16 noro 1944: int nd_newps(int mod,ND a)
1.1 noro 1945: {
1.3 noro 1946: int len;
1.13 noro 1947: RHist r;
1.20 noro 1948: NDV b;
1.3 noro 1949:
1.1 noro 1950: if ( nd_psn == nd_pslen ) {
1951: nd_pslen *= 2;
1.6 noro 1952: nd_psl = (int *)REALLOC((char *)nd_psl,nd_pslen*sizeof(int));
1.11 noro 1953: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.20 noro 1954: nd_psq = (NDV *)REALLOC((char *)nd_psq,nd_pslen*sizeof(NDV));
1.13 noro 1955: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.1 noro 1956: nd_bound = (unsigned int **)
1957: REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
1958: }
1.20 noro 1959: nd_removecont(mod,a);
1960: nd_bound[nd_psn] = nd_compute_bound(a);
1961: NEWRHist(r); SG(r) = SG(a); TD(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1962: nd_psh[nd_psn] = r;
1963: b = ndtondv(mod,a);
1964: len = LEN(b);
1.16 noro 1965: if ( mod )
1.20 noro 1966: nd_ps[nd_psn] = b;
1.16 noro 1967: else
1.20 noro 1968: nd_psq[nd_psn] = b;
1969: nd_psl[nd_psn] = len;
1.13 noro 1970: nd_free(a);
1.20 noro 1971: return nd_psn++;
1972: }
1973:
1974: int nd_newps_trace(int mod,ND nf,ND nfq)
1975: {
1976: int len;
1977: RHist r;
1978: NDV b;
1979:
1980: if ( nd_psn == nd_pslen ) {
1981: nd_pslen *= 2;
1982: nd_psl = (int *)REALLOC((char *)nd_psl,nd_pslen*sizeof(int));
1983: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1984: nd_psq = (NDV *)REALLOC((char *)nd_psq,nd_pslen*sizeof(NDV));
1985: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1986: nd_bound = (unsigned int **)
1987: REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
1988: }
1.27 noro 1989: if ( !rem(NM(HCQ(nfq)),mod) ) return -1;
1.20 noro 1990: nd_removecont(mod,nf);
1991: nd_ps[nd_psn] = ndtondv(mod,nf);
1992:
1993: nd_removecont(0,nfq);
1994: nd_psq[nd_psn] = ndtondv(0,nfq);
1995:
1996: nd_bound[nd_psn] = nd_compute_bound(nfq);
1997: NEWRHist(r); SG(r) = SG(nf); TD(r) = HTD(nf); ndl_copy(HDL(nf),DL(r));
1998: nd_psh[nd_psn] = r;
1999:
2000: len = LEN(nd_psq[nd_psn]);
2001: nd_psl[nd_psn] = len;
2002:
2003: nd_free(nf); nd_free(nfq);
1.1 noro 2004: return nd_psn++;
2005: }
2006:
1.23 noro 2007: void nd_setup(int mod,NODE f)
1.1 noro 2008: {
1.5 noro 2009: int i,j,td,len,max;
1.1 noro 2010: NODE s,s0,f0;
1.5 noro 2011: unsigned int *d;
1.13 noro 2012: RHist r;
1.20 noro 2013: NDV a;
1.11 noro 2014:
2015: nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1 noro 2016:
2017: nd_psn = length(f); nd_pslen = 2*nd_psn;
1.6 noro 2018: nd_psl = (int *)MALLOC(nd_pslen*sizeof(int));
1.11 noro 2019: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.20 noro 2020: nd_psq = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13 noro 2021: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.1 noro 2022: nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
1.5 noro 2023: for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
2024: nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
2025: for ( j = 0; j < nd_nvar; j++ )
2026: max = MAX(d[j],max);
2027: }
1.11 noro 2028: if ( !nd_red )
1.13 noro 2029: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
2030: bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.5 noro 2031:
2032: if ( max < 2 )
2033: nd_bpe = 2;
2034: else if ( max < 4 )
2035: nd_bpe = 4;
2036: else if ( max < 64 )
2037: nd_bpe = 6;
2038: else if ( max < 256 )
2039: nd_bpe = 8;
2040: else if ( max < 65536 )
2041: nd_bpe = 16;
2042: else
2043: nd_bpe = 32;
1.13 noro 2044:
1.1 noro 2045: nd_setup_parameters();
2046: nd_free_private_storage();
1.3 noro 2047: len = 0;
1.1 noro 2048: for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.20 noro 2049: NEWRHist(r);
2050: a = dptondv(mod,(DP)BDY(f));
2051: ndv_removecont(mod,a);
2052: len = MAX(len,LEN(a));
2053: SG(r) = HTD(a); TD(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.16 noro 2054: if ( mod )
1.20 noro 2055: nd_ps[i] = a;
1.16 noro 2056: else
1.20 noro 2057: nd_psq[i] = a;
1.13 noro 2058: nd_psh[i] = r;
1.1 noro 2059: }
2060: }
2061:
1.23 noro 2062: void nd_setup_trace(int mod,NODE f)
1.20 noro 2063: {
2064: int i,j,td,len,max;
2065: NODE s,s0,f0;
2066: unsigned int *d;
2067: RHist r;
2068: NDV a;
2069:
2070: nd_found = 0; nd_notfirst = 0; nd_create = 0;
2071:
2072: nd_psn = length(f); nd_pslen = 2*nd_psn;
2073: nd_psl = (int *)MALLOC(nd_pslen*sizeof(int));
2074: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
2075: nd_psq = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
2076: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
2077: nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
2078: for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
2079: nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
2080: for ( j = 0; j < nd_nvar; j++ )
2081: max = MAX(d[j],max);
2082: }
2083: if ( !nd_red )
2084: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
2085: bzero(nd_red,REDTAB_LEN*sizeof(RHist));
2086:
2087: if ( max < 2 )
2088: nd_bpe = 2;
2089: else if ( max < 4 )
2090: nd_bpe = 4;
2091: else if ( max < 64 )
2092: nd_bpe = 6;
2093: else if ( max < 256 )
2094: nd_bpe = 8;
2095: else if ( max < 65536 )
2096: nd_bpe = 16;
2097: else
2098: nd_bpe = 32;
2099:
2100: nd_setup_parameters();
2101: nd_free_private_storage();
2102: len = 0;
2103: for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
2104: a = dptondv(mod,(DP)BDY(f)); ndv_removecont(mod,a); nd_ps[i] = a;
2105: a = dptondv(0,(DP)BDY(f)); ndv_removecont(0,a); nd_psq[i] = a;
2106: NEWRHist(r);
2107: len = MAX(len,LEN(a));
2108: SG(r) = HTD(a); TD(r) = HTD(a); ndl_copy(HDL(a),DL(r));
2109: nd_psh[i] = r;
2110: }
2111: }
2112:
1.1 noro 2113: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp)
2114: {
2115: struct order_spec ord1;
2116: VL fv,vv,vc;
2117: NODE fd,fd0,r,r0,t,x,s,xx;
2118: DP a,b,c;
2119:
2120: get_vars((Obj)f,&fv); pltovl(v,&vv);
2121: nd_nvar = length(vv);
2122: if ( ord->id )
2123: error("nd_gr : unsupported order");
2124: switch ( ord->ord.simple ) {
2125: case 0:
2126: is_rlex = 1;
2127: break;
2128: case 1:
2129: is_rlex = 0;
2130: break;
2131: default:
2132: error("nd_gr : unsupported order");
2133: }
2134: initd(ord);
2135: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
2136: ptod(CO,vv,(P)BDY(t),&b);
1.20 noro 2137: NEXTNODE(fd0,fd); BDY(fd) = (pointer)b;
1.1 noro 2138: }
2139: if ( fd0 ) NEXT(fd) = 0;
1.23 noro 2140: nd_setup(m,fd0);
1.27 noro 2141: x = nd_gb(m,0);
1.25 noro 2142: fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
2143: nd_found,nd_notfirst,nd_create);
1.23 noro 2144: x = nd_reduceall(m,x);
2145: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1 noro 2146: NEXTNODE(r0,r);
1.20 noro 2147: if ( m ) {
1.23 noro 2148: a = ndvtodp(m,BDY(t));
1.16 noro 2149: _dtop_mod(CO,vv,a,(P *)&BDY(r));
1.20 noro 2150: } else {
1.23 noro 2151: a = ndvtodp(m,BDY(t));
1.16 noro 2152: dtop(CO,vv,a,(P *)&BDY(r));
1.20 noro 2153: }
2154: }
2155: if ( r0 ) NEXT(r) = 0;
2156: MKLIST(*rp,r0);
2157: }
2158:
1.23 noro 2159: void nd_gr_trace(LIST f,LIST v,int m,int homo,struct order_spec *ord,LIST *rp)
1.20 noro 2160: {
2161: struct order_spec ord1;
2162: VL fv,vv,vc;
1.27 noro 2163: NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.23 noro 2164: DP a,b,c,h;
1.27 noro 2165: P p;
1.20 noro 2166:
2167: get_vars((Obj)f,&fv); pltovl(v,&vv);
2168: nd_nvar = length(vv);
2169: if ( ord->id )
2170: error("nd_gr : unsupported order");
2171: initd(ord);
1.23 noro 2172: if ( homo ) {
2173: homogenize_order(ord,nd_nvar,&ord1);
2174: switch ( ord1.ord.simple ) {
2175: case 0: is_rlex = 1; break;
2176: case 1: is_rlex = 0; break;
2177: default: error("nd_gr : unsupported order");
2178: }
1.27 noro 2179: for ( fd0 = 0, in0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.23 noro 2180: ptod(CO,vv,(P)BDY(t),&c);
2181: if ( c ) {
2182: dp_homo(c,&h); NEXTNODE(fd0,fd); BDY(fd) = (pointer)h;
1.27 noro 2183: NEXTNODE(in0,in); BDY(in) = (pointer)c;
1.23 noro 2184: }
2185: }
2186: if ( fd0 ) NEXT(fd) = 0;
1.27 noro 2187: if ( in0 ) NEXT(in) = 0;
1.23 noro 2188: initd(&ord1);
2189: nd_nvar++;
1.25 noro 2190: } else {
2191: switch ( ord->ord.simple ) {
2192: case 0: is_rlex = 1; break;
2193: case 1: is_rlex = 0; break;
2194: default: error("nd_gr : unsupported order");
2195: }
2196: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
2197: ptod(CO,vv,(P)BDY(t),&c);
2198: if ( c ) {
2199: NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
1.23 noro 2200: }
2201: }
1.25 noro 2202: if ( fd0 ) NEXT(fd) = 0;
1.27 noro 2203: in0 = fd0;
2204: }
2205: do {
1.25 noro 2206: nd_setup_trace(m,fd0);
1.27 noro 2207: cand = nd_gb_trace(m);
2208: if ( !cand ) continue;
2209: if ( homo ) {
2210: /* dehomogenization */
2211: for ( t = cand; t; t = NEXT(t) )
2212: ndv_dehomogenize((NDV)BDY(t));
2213: nd_nvar--;
2214: nd_setup_parameters();
2215: initd(ord);
2216: cand = nd_reducebase(cand);
2217: }
2218: fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
2219: nd_found,nd_notfirst,nd_create);
2220: cand = nd_reduceall(0,cand);
2221: initd(ord);
2222: for ( r0 = 0; cand; cand = NEXT(cand) ) {
2223: NEXTNODE(r0,r);
2224: BDY(r) = (pointer)ndvtodp(0,(NDV)BDY(cand));
2225: }
2226: if ( r0 ) NEXT(r) = 0;
2227: cand = r0;
2228: } while ( !nd_check_candidate(in0,cand) );
2229: /* dp->p */
2230: for ( r = cand; r; r = NEXT(r) ) {
2231: dtop(CO,vv,BDY(r),&p);
2232: BDY(r) = (pointer)p;
1.23 noro 2233: }
1.27 noro 2234: MKLIST(*rp,cand);
1.1 noro 2235: }
2236:
2237: void dltondl(int n,DL dl,unsigned int *r)
2238: {
2239: unsigned int *d;
2240: int i;
2241:
2242: d = dl->d;
2243: bzero(r,nd_wpd*sizeof(unsigned int));
2244: if ( is_rlex )
2245: for ( i = 0; i < n; i++ )
2246: r[(n-1-i)/nd_epw] |= (d[i]<<((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe));
2247: else
2248: for ( i = 0; i < n; i++ )
2249: r[i/nd_epw] |= d[i]<<((nd_epw-(i%nd_epw)-1)*nd_bpe);
2250: }
2251:
2252: DL ndltodl(int n,int td,unsigned int *ndl)
2253: {
2254: DL dl;
2255: int *d;
2256: int i;
2257:
2258: NEWDL(dl,n);
1.14 noro 2259: TD(dl) = td;
1.1 noro 2260: d = dl->d;
2261: if ( is_rlex )
2262: for ( i = 0; i < n; i++ )
2263: d[i] = (ndl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
2264: &((1<<nd_bpe)-1);
2265: else
2266: for ( i = 0; i < n; i++ )
2267: d[i] = (ndl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
2268: &((1<<nd_bpe)-1);
2269: return dl;
2270: }
2271:
1.17 noro 2272: ND dptond(int mod,DP p)
1.1 noro 2273: {
2274: ND d;
2275: NM m0,m;
2276: MP t;
2277: int n;
2278:
2279: if ( !p )
2280: return 0;
2281: n = NV(p);
2282: m0 = 0;
2283: for ( t = BDY(p); t; t = NEXT(t) ) {
2284: NEXTNM(m0,m);
1.17 noro 2285: if ( mod )
2286: CM(m) = ITOS(C(t));
2287: else
2288: CQ(m) = (Q)C(t);
1.14 noro 2289: TD(m) = TD(DL(t));
2290: dltondl(n,DL(t),DL(m));
1.1 noro 2291: }
2292: NEXT(m) = 0;
2293: MKND(n,m0,d);
1.14 noro 2294: NV(d) = n;
2295: SG(d) = SG(p);
1.1 noro 2296: return d;
2297: }
2298:
1.17 noro 2299: DP ndtodp(int mod,ND p)
1.1 noro 2300: {
2301: DP d;
2302: MP m0,m;
2303: NM t;
2304: int n;
2305:
2306: if ( !p )
2307: return 0;
2308: n = NV(p);
2309: m0 = 0;
2310: for ( t = BDY(p); t; t = NEXT(t) ) {
2311: NEXTMP(m0,m);
1.17 noro 2312: if ( mod )
2313: C(m) = STOI(CM(t));
2314: else
2315: C(m) = (P)CQ(t);
1.14 noro 2316: DL(m) = ndltodl(n,TD(t),DL(t));
1.1 noro 2317: }
2318: NEXT(m) = 0;
2319: MKDP(n,m0,d);
1.14 noro 2320: SG(d) = SG(p);
1.1 noro 2321: return d;
2322: }
2323:
2324: void ndl_print(unsigned int *dl)
2325: {
2326: int n;
2327: int i;
2328:
2329: n = nd_nvar;
2330: printf("<<");
2331: if ( is_rlex )
2332: for ( i = 0; i < n; i++ )
2333: printf(i==n-1?"%d":"%d,",
2334: (dl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
2335: &((1<<nd_bpe)-1));
2336: else
2337: for ( i = 0; i < n; i++ )
2338: printf(i==n-1?"%d":"%d,",
2339: (dl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
2340: &((1<<nd_bpe)-1));
2341: printf(">>");
2342: }
2343:
2344: void nd_print(ND p)
2345: {
2346: NM m;
2347:
2348: if ( !p )
2349: printf("0\n");
2350: else {
2351: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 noro 2352: printf("+%d*",CM(m));
2353: ndl_print(DL(m));
1.1 noro 2354: }
2355: printf("\n");
2356: }
2357: }
2358:
1.16 noro 2359: void nd_print_q(ND p)
2360: {
2361: NM m;
2362:
2363: if ( !p )
2364: printf("0\n");
2365: else {
2366: for ( m = BDY(p); m; m = NEXT(m) ) {
2367: printf("+");
2368: printexpr(CO,CQ(m));
2369: printf("*");
2370: ndl_print(DL(m));
2371: }
2372: printf("\n");
2373: }
2374: }
2375:
1.1 noro 2376: void ndp_print(ND_pairs d)
2377: {
2378: ND_pairs t;
2379:
2380: for ( t = d; t; t = NEXT(t) ) {
2381: printf("%d,%d ",t->i1,t->i2);
2382: }
2383: printf("\n");
2384: }
2385:
1.20 noro 2386: void nd_removecont(int mod,ND p)
1.16 noro 2387: {
2388: int i,n;
2389: Q *w;
2390: Q dvr,t;
2391: NM m;
1.21 noro 2392: struct oVECT v;
2393: N q,r;
1.16 noro 2394:
1.20 noro 2395: if ( mod )
2396: nd_mul_c(mod,p,invm(HCM(p),mod));
2397: else {
2398: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
2399: w = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2400: v.len = n;
2401: v.body = (pointer *)w;
1.20 noro 2402: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
2403: w[i] = CQ(m);
1.21 noro 2404: removecont_array(w,n);
2405: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16 noro 2406: }
2407: }
2408:
1.21 noro 2409: void nd_removecont2(ND p1,ND p2)
2410: {
2411: int i,n1,n2,n;
2412: Q *w;
2413: Q dvr,t;
2414: NM m;
2415: struct oVECT v;
2416: N q,r;
2417:
2418: if ( !p1 ) {
2419: nd_removecont(0,p2); return;
2420: } else if ( !p2 ) {
2421: nd_removecont(0,p1); return;
2422: }
2423: n1 = nd_length(p1);
2424: n2 = nd_length(p2);
2425: n = n1+n2;
2426: w = (Q *)ALLOCA(n*sizeof(Q));
2427: v.len = n;
2428: v.body = (pointer *)w;
2429: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ )
2430: w[i] = CQ(m);
2431: for ( m = BDY(p2); i < n; m = NEXT(m), i++ )
2432: w[i] = CQ(m);
2433: removecont_array(w,n);
2434: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
2435: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
2436: }
2437:
1.20 noro 2438: void ndv_removecont(int mod,NDV p)
1.16 noro 2439: {
2440: int i,len;
2441: Q *w;
2442: Q dvr,t;
2443: NMV m;
2444:
1.20 noro 2445: if ( mod )
2446: ndv_mul_c(mod,p,invm(HCM(p),mod));
2447: else {
2448: len = p->len;
2449: w = (Q *)ALLOCA(len*sizeof(Q));
2450: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
2451: w[i] = CQ(m);
2452: sortbynm(w,len);
2453: qltozl(w,len,&dvr);
2454: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
2455: divq(CQ(m),dvr,&t); CQ(m) = t;
2456: }
1.16 noro 2457: }
1.21 noro 2458: }
2459:
1.23 noro 2460: void ndv_dehomogenize(NDV p)
2461: {
2462: int i,len,newnvar,newwpd,newadv;
2463: Q *w;
2464: Q dvr,t;
2465: NMV m,r;
2466: unsigned int *d;
2467: #define NEWADV(m) (m = (NMV)(((char *)m)+newadv))
2468:
2469: len = p->len;
2470: newnvar = nd_nvar-1;
2471: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0);
2472: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
2473: TD(m) -= ndl_dehomogenize(DL(m));
2474: if ( newwpd != nd_wpd ) {
2475: d = (unsigned int *)ALLOCA(newwpd*sizeof(unsigned int));
2476: newadv = sizeof(struct oNMV)+(newwpd-1)*sizeof(unsigned int);
2477: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NEWADV(r), i++ ) {
2478: CQ(r) = CQ(m); TD(r) = TD(m); ndl_copy(DL(m),d); ndl_copy(d,DL(r));
2479: }
2480: }
2481: NV(p)--;
2482: }
2483:
1.21 noro 2484: void removecont_array(Q *c,int n)
2485: {
2486: struct oVECT v;
2487: Q d0,d1,a,u,u1,gcd;
2488: int i;
2489: N qn,rn,gn;
2490: Q *q,*r;
2491:
2492: q = (Q *)ALLOCA(n*sizeof(Q));
2493: r = (Q *)ALLOCA(n*sizeof(Q));
2494: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
2495: igcdv_estimate(&v,&d0);
2496: for ( i = 0; i < n; i++ ) {
2497: divn(NM(c[i]),NM(d0),&qn,&rn);
2498: NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
2499: NTOQ(rn,SGN(c[i]),r[i]);
2500: }
2501: for ( i = 0; i < n; i++ )
2502: if ( r[i] )
2503: break;
2504: if ( i < n ) {
2505: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
2506: igcdv(&v,&d1);
2507: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
2508: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
2509: for ( i = 0; i < n; i++ ) {
2510: mulq(a,q[i],&u);
2511: if ( r[i] ) {
2512: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
2513: addq(u,u1,&q[i]);
2514: } else
2515: q[i] = u;
2516: }
2517: }
2518: for ( i = 0; i < n; i++ )
2519: c[i] = q[i];
1.16 noro 2520: }
2521:
1.19 noro 2522: void nd_mul_c(int mod,ND p,int mul)
1.1 noro 2523: {
2524: NM m;
2525: int c,c1;
2526:
2527: if ( !p )
2528: return;
2529: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 noro 2530: c1 = CM(m);
1.19 noro 2531: DMAR(c1,mul,0,mod,c);
1.14 noro 2532: CM(m) = c;
1.1 noro 2533: }
2534: }
2535:
1.16 noro 2536: void nd_mul_c_q(ND p,Q mul)
2537: {
2538: NM m;
2539: Q c;
2540:
2541: if ( !p )
2542: return;
2543: for ( m = BDY(p); m; m = NEXT(m) ) {
2544: mulq(CQ(m),mul,&c); CQ(m) = c;
2545: }
2546: }
2547:
1.1 noro 2548: void nd_free(ND p)
2549: {
2550: NM t,s;
2551:
2552: if ( !p )
2553: return;
2554: t = BDY(p);
2555: while ( t ) {
2556: s = NEXT(t);
2557: FREENM(t);
2558: t = s;
2559: }
2560: FREEND(p);
2561: }
2562:
1.23 noro 2563: void ndv_free(NDV p)
2564: {
2565: GC_free(BDY(p));
2566: }
2567:
1.1 noro 2568: void nd_append_red(unsigned int *d,int td,int i)
2569: {
1.13 noro 2570: RHist m,m0;
1.1 noro 2571: int h;
2572:
1.13 noro 2573: NEWRHist(m);
1.1 noro 2574: h = ndl_hash_value(td,d);
1.13 noro 2575: m->index = i;
1.14 noro 2576: TD(m) = td;
2577: ndl_copy(d,DL(m));
1.1 noro 2578: NEXT(m) = nd_red[h];
2579: nd_red[h] = m;
2580: }
2581:
1.5 noro 2582: unsigned int *dp_compute_bound(DP p)
2583: {
2584: unsigned int *d,*d1,*d2,*t;
2585: MP m;
1.7 noro 2586: int i,l;
1.5 noro 2587:
2588: if ( !p )
2589: return 0;
2590: d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
2591: d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
2592: m = BDY(p);
1.14 noro 2593: bcopy(DL(m)->d,d1,nd_nvar*sizeof(unsigned int));
1.5 noro 2594: for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14 noro 2595: d = DL(m)->d;
1.5 noro 2596: for ( i = 0; i < nd_nvar; i++ )
2597: d2[i] = d[i] > d1[i] ? d[i] : d1[i];
2598: t = d1; d1 = d2; d2 = t;
2599: }
1.13 noro 2600: l = (nd_nvar+31);
1.7 noro 2601: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
2602: bzero(t,l*sizeof(unsigned int));
1.5 noro 2603: bcopy(d1,t,nd_nvar*sizeof(unsigned int));
2604: return t;
2605: }
2606:
1.1 noro 2607: unsigned int *nd_compute_bound(ND p)
2608: {
2609: unsigned int *d1,*d2,*t;
1.9 noro 2610: int i,l;
1.1 noro 2611: NM m;
2612:
2613: if ( !p )
2614: return 0;
2615: d1 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
2616: d2 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
2617: bcopy(HDL(p),d1,nd_wpd*sizeof(unsigned int));
2618: for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14 noro 2619: ndl_lcm(DL(m),d1,d2);
1.1 noro 2620: t = d1; d1 = d2; d2 = t;
2621: }
1.12 noro 2622: l = nd_nvar+31;
1.9 noro 2623: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
2624: bzero(t,l*sizeof(unsigned int));
1.5 noro 2625: for ( i = 0; i < nd_nvar; i++ )
2626: t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0;
1.1 noro 2627: return t;
2628: }
2629:
2630: void nd_setup_parameters() {
2631: int i;
2632:
2633: nd_epw = (sizeof(unsigned int)*8)/nd_bpe;
2634: nd_wpd = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
2635: if ( nd_bpe < 32 ) {
2636: nd_mask0 = (1<<nd_bpe)-1;
2637: } else {
2638: nd_mask0 = 0xffffffff;
2639: }
2640: bzero(nd_mask,sizeof(nd_mask));
2641: nd_mask1 = 0;
2642: for ( i = 0; i < nd_epw; i++ ) {
2643: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
2644: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
2645: }
1.13 noro 2646: nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
1.3 noro 2647: nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int);
1.1 noro 2648: }
2649:
1.20 noro 2650: /* mod < 0 => realloc nd_ps and pd_psq */
2651:
2652: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1 noro 2653: {
1.11 noro 2654: int i,obpe,oadv;
1.13 noro 2655: NM prev_nm_free_list;
2656: RHist mr0,mr;
2657: RHist r;
1.1 noro 2658: ND_pairs s0,s,t,prev_ndp_free_list;
1.15 noro 2659:
1.1 noro 2660: obpe = nd_bpe;
1.11 noro 2661: oadv = nmv_adv;
1.5 noro 2662: if ( obpe < 4 )
2663: nd_bpe = 4;
2664: else if ( obpe < 6 )
2665: nd_bpe = 6;
2666: else if ( obpe < 8 )
2667: nd_bpe = 8;
2668: else if ( obpe < 16 )
2669: nd_bpe = 16;
2670: else if ( obpe < 32 )
2671: nd_bpe = 32;
2672: else
2673: error("nd_reconstruct : exponent too large");
2674:
1.1 noro 2675: nd_setup_parameters();
2676: prev_nm_free_list = _nm_free_list;
2677: prev_ndp_free_list = _ndp_free_list;
2678: _nm_free_list = 0;
2679: _ndp_free_list = 0;
1.20 noro 2680: if ( mod != 0 )
2681: for ( i = nd_psn-1; i >= 0; i-- )
2682: ndv_realloc(nd_ps[i],obpe,oadv);
2683: if ( !mod || trace )
2684: for ( i = nd_psn-1; i >= 0; i-- )
2685: ndv_realloc(nd_psq[i],obpe,oadv);
1.1 noro 2686: s0 = 0;
2687: for ( t = d; t; t = NEXT(t) ) {
2688: NEXTND_pairs(s0,s);
2689: s->i1 = t->i1;
2690: s->i2 = t->i2;
1.14 noro 2691: TD(s) = TD(t);
2692: SG(s) = SG(t);
1.1 noro 2693: ndl_dup(obpe,t->lcm,s->lcm);
2694: }
1.6 noro 2695: for ( i = 0; i < REDTAB_LEN; i++ ) {
1.13 noro 2696: for ( mr0 = 0, r = nd_red[i]; r; r = NEXT(r) ) {
1.16 noro 2697: NEXTRHist(mr0,mr);
1.13 noro 2698: mr->index = r->index;
1.20 noro 2699: SG(mr) = SG(r);
1.14 noro 2700: TD(mr) = TD(r);
2701: ndl_dup(obpe,DL(r),DL(mr));
1.6 noro 2702: }
2703: if ( mr0 ) NEXT(mr) = 0;
2704: nd_red[i] = mr0;
2705: }
1.11 noro 2706: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 2707: NEWRHist(r); SG(r) = SG(nd_psh[i]);
2708: TD(r) = TD(nd_psh[i]); ndl_dup(obpe,DL(nd_psh[i]),DL(r));
1.13 noro 2709: nd_psh[i] = r;
1.11 noro 2710: }
1.1 noro 2711: if ( s0 ) NEXT(s) = 0;
2712: prev_nm_free_list = 0;
2713: prev_ndp_free_list = 0;
2714: GC_gcollect();
2715: return s0;
2716: }
2717:
1.23 noro 2718: void nd_reconstruct_direct(int mod,NDV *ps,int len)
2719: {
2720: int i,obpe,oadv;
2721: NM prev_nm_free_list;
2722: RHist mr0,mr;
2723: RHist r;
2724: ND_pairs s0,s,t,prev_ndp_free_list;
2725:
2726: obpe = nd_bpe;
2727: oadv = nmv_adv;
2728: if ( obpe < 4 )
2729: nd_bpe = 4;
2730: else if ( obpe < 6 )
2731: nd_bpe = 6;
2732: else if ( obpe < 8 )
2733: nd_bpe = 8;
2734: else if ( obpe < 16 )
2735: nd_bpe = 16;
2736: else if ( obpe < 32 )
2737: nd_bpe = 32;
2738: else
2739: error("nd_reconstruct_direct : exponent too large");
2740:
2741: nd_setup_parameters();
2742: prev_nm_free_list = _nm_free_list;
2743: prev_ndp_free_list = _ndp_free_list;
2744: _nm_free_list = 0;
2745: _ndp_free_list = 0;
2746: if ( mod != 0 )
2747: for ( i = len-1; i >= 0; i-- )
2748: ndv_realloc(ps[i],obpe,oadv);
2749: prev_nm_free_list = 0;
2750: prev_ndp_free_list = 0;
2751: GC_gcollect();
2752: }
2753:
1.1 noro 2754: void ndl_dup(int obpe,unsigned int *d,unsigned int *r)
2755: {
2756: int n,i,ei,oepw,cepw,cbpe;
2757:
2758: n = nd_nvar;
2759: oepw = (sizeof(unsigned int)*8)/obpe;
2760: cepw = nd_epw;
2761: cbpe = nd_bpe;
1.15 noro 2762: for ( i = 0; i < nd_wpd; i++ )
2763: r[i] = 0;
1.1 noro 2764: if ( is_rlex )
2765: for ( i = 0; i < n; i++ ) {
2766: ei = (d[(n-1-i)/oepw]>>((oepw-((n-1-i)%oepw)-1)*obpe))
2767: &((1<<obpe)-1);
2768: r[(n-1-i)/cepw] |= (ei<<((cepw-((n-1-i)%cepw)-1)*cbpe));
2769: }
2770: else
2771: for ( i = 0; i < n; i++ ) {
2772: ei = (d[i/oepw]>>((oepw-(i%oepw)-1)*obpe))
2773: &((1<<obpe)-1);
2774: r[i/cepw] |= (ei<<((cepw-(i%cepw)-1)*cbpe));
2775: }
2776: }
2777:
1.11 noro 2778: void nd_realloc(ND p,int obpe)
1.1 noro 2779: {
2780: NM m,mr,mr0;
2781:
1.11 noro 2782: if ( p ) {
2783: m = BDY(p);
1.1 noro 2784: for ( mr0 = 0; m; m = NEXT(m) ) {
2785: NEXTNM(mr0,mr);
1.14 noro 2786: CM(mr) = CM(m);
2787: TD(mr) = TD(m);
2788: ndl_dup(obpe,DL(m),DL(mr));
1.1 noro 2789: }
2790: NEXT(mr) = 0;
1.11 noro 2791: BDY(p) = mr0;
1.1 noro 2792: }
2793: }
1.3 noro 2794:
1.6 noro 2795: ND nd_copy(ND p)
2796: {
2797: NM m,mr,mr0;
2798: int c,n,s;
2799: ND r;
2800:
2801: if ( !p )
2802: return 0;
2803: else {
2804: s = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
2805: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2806: NEXTNM(mr0,mr);
1.14 noro 2807: CM(mr) = CM(m);
2808: TD(mr) = TD(m);
2809: ndl_copy(DL(m),DL(mr));
1.6 noro 2810: }
2811: NEXT(mr) = 0;
2812: MKND(NV(p),mr0,r);
1.14 noro 2813: SG(r) = SG(p);
1.6 noro 2814: return r;
2815: }
2816: }
2817:
1.16 noro 2818: int nd_sp(int mod,ND_pairs p,ND *rp)
1.11 noro 2819: {
2820: NM m;
2821: NDV p1,p2;
2822: ND t1,t2;
2823: unsigned int *lcm;
2824: int td;
2825:
1.20 noro 2826: if ( mod ) {
2827: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
2828: } else {
2829: p1 = nd_psq[p->i1]; p2 = nd_psq[p->i2];
2830: }
1.11 noro 2831: lcm = p->lcm;
1.14 noro 2832: td = TD(p);
1.11 noro 2833: NEWNM(m);
1.20 noro 2834: CQ(m) = HCQ(p2);
1.16 noro 2835: TD(m) = td-HTD(p1); ndl_sub(lcm,HDL(p1),DL(m));
1.14 noro 2836: if ( ndl_check_bound2(p->i1,DL(m)) )
1.11 noro 2837: return 0;
1.25 noro 2838: t1 = ndv_mul_nm(mod,p1,m);
1.16 noro 2839: if ( mod )
2840: CM(m) = mod-HCM(p1);
2841: else
2842: chsgnq(HCQ(p1),&CQ(m));
2843: TD(m) = td-HTD(p2); ndl_sub(lcm,HDL(p2),DL(m));
1.14 noro 2844: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 2845: nd_free(t1);
2846: return 0;
2847: }
1.25 noro 2848: t2 = ndv_mul_nm(mod,p2,m);
2849: *rp = nd_add(mod,t1,t2);
1.11 noro 2850: FREENM(m);
2851: return 1;
2852: }
2853:
1.19 noro 2854: void ndv_mul_c(int mod,NDV p,int mul)
1.11 noro 2855: {
2856: NMV m;
2857: int c,c1,len,i;
2858:
2859: if ( !p )
2860: return;
1.14 noro 2861: len = LEN(p);
1.11 noro 2862: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14 noro 2863: c1 = CM(m);
1.19 noro 2864: DMAR(c1,mul,0,mod,c);
1.14 noro 2865: CM(m) = c;
1.11 noro 2866: }
2867: }
2868:
1.16 noro 2869: void ndv_mul_c_q(NDV p,Q mul)
2870: {
2871: NMV m;
2872: Q c;
2873: int len,i;
2874:
2875: if ( !p )
2876: return;
2877: len = LEN(p);
2878: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
2879: mulq(CQ(m),mul,&c); CQ(m) = c;
2880: }
2881: }
2882:
1.25 noro 2883: ND ndv_mul_nm(int mod,NDV p,NM m0)
1.9 noro 2884: {
2885: NM mr,mr0;
2886: NMV m;
2887: unsigned int *d,*dt,*dm;
2888: int c,n,td,i,c1,c2,len;
1.16 noro 2889: Q q;
1.9 noro 2890: ND r;
2891:
2892: if ( !p )
2893: return 0;
2894: else {
2895: n = NV(p); m = BDY(p);
1.16 noro 2896: d = DL(m0); td = TD(m0);
1.14 noro 2897: len = LEN(p);
1.9 noro 2898: mr0 = 0;
1.16 noro 2899: if ( mod ) {
2900: c = CM(m0);
2901: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
2902: NEXTNM(mr0,mr);
2903: c1 = CM(m);
1.19 noro 2904: DMAR(c1,c,0,mod,c2);
1.16 noro 2905: CM(mr) = c2;
2906: TD(mr) = TD(m)+td;
2907: ndl_add(DL(m),d,DL(mr));
2908: }
2909: } else {
2910: q = CQ(m0);
2911: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
2912: NEXTNM(mr0,mr);
2913: mulq(CQ(m),q,&CQ(mr));
2914: TD(mr) = TD(m)+td;
2915: ndl_add(DL(m),d,DL(mr));
2916: }
1.4 noro 2917: }
1.9 noro 2918: NEXT(mr) = 0;
2919: MKND(NV(p),mr0,r);
1.14 noro 2920: SG(r) = SG(p) + td;
1.9 noro 2921: return r;
1.4 noro 2922: }
2923: }
2924:
1.11 noro 2925: void ndv_realloc(NDV p,int obpe,int oadv)
2926: {
1.13 noro 2927: NMV m,mr,mr0,t;
2928: int len,i,k;
1.11 noro 2929:
1.13 noro 2930: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
2931: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.11 noro 2932:
2933: if ( p ) {
1.14 noro 2934: m = BDY(p); len = LEN(p);
1.15 noro 2935: if ( nmv_adv > oadv )
2936: mr0 = (NMV)REALLOC(BDY(p),len*nmv_adv);
2937: else
2938: mr0 = BDY(p);
1.13 noro 2939: m = (NMV)((char *)mr0+(len-1)*oadv);
2940: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
2941: t = (NMV)ALLOCA(nmv_adv);
2942: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.16 noro 2943: CQ(t) = CQ(m);
1.14 noro 2944: TD(t) = TD(m);
2945: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
2946: ndl_dup(obpe,DL(m),DL(t));
1.16 noro 2947: CQ(mr) = CQ(t);
1.14 noro 2948: TD(mr) = TD(t);
2949: ndl_copy(DL(t),DL(mr));
1.11 noro 2950: }
2951: BDY(p) = mr0;
2952: }
2953: }
2954:
1.16 noro 2955: NDV ndtondv(int mod,ND p)
1.3 noro 2956: {
2957: NDV d;
2958: NMV m,m0;
2959: NM t;
2960: int i,len;
2961:
2962: if ( !p )
2963: return 0;
2964: len = nd_length(p);
1.16 noro 2965: if ( mod )
2966: m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv);
2967: else
2968: m0 = m = (NMV)MALLOC(len*nmv_adv);
1.3 noro 2969: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
1.14 noro 2970: TD(m) = TD(t);
2971: ndl_copy(DL(t),DL(m));
1.16 noro 2972: CQ(m) = CQ(t);
1.3 noro 2973: }
2974: MKNDV(NV(p),m0,len,d);
1.23 noro 2975: SG(d) = SG(p);
2976: return d;
2977: }
2978:
2979: ND ndvtond(int mod,NDV p)
2980: {
2981: ND d;
2982: NM m,m0;
2983: NMV t;
2984: int i,len;
2985:
2986: if ( !p )
2987: return 0;
2988: m0 = 0;
2989: len = p->len;
2990: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
2991: NEXTNM(m0,m);
2992: TD(m) = TD(t);
2993: ndl_copy(DL(t),DL(m));
2994: CQ(m) = CQ(t);
2995: }
2996: NEXT(m) = 0;
2997: MKND(NV(p),m0,d);
1.14 noro 2998: SG(d) = SG(p);
1.3 noro 2999: return d;
3000: }
3001:
1.16 noro 3002: NDV dptondv(int mod,DP p)
1.11 noro 3003: {
3004: NDV d;
3005: NMV m0,m;
3006: MP t;
1.20 noro 3007: DP q;
1.11 noro 3008: int l,i,n;
3009:
3010: if ( !p )
3011: return 0;
3012: for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ );
1.20 noro 3013: if ( mod ) {
3014: _dp_mod(p,mod,0,&q); p = q;
1.16 noro 3015: m0 = m = (NMV)MALLOC_ATOMIC(l*nmv_adv);
1.20 noro 3016: } else
1.16 noro 3017: m0 = m = (NMV)MALLOC(l*nmv_adv);
1.11 noro 3018: n = NV(p);
1.17 noro 3019: for ( t = BDY(p), i = 0; i < l; i++, t = NEXT(t), NMV_ADV(m) ) {
3020: if ( mod )
1.16 noro 3021: CM(m) = ITOS(C(t));
1.17 noro 3022: else
1.16 noro 3023: CQ(m) = (Q)C(t);
1.17 noro 3024: TD(m) = TD(DL(t));
3025: dltondl(n,DL(t),DL(m));
1.11 noro 3026: }
3027: MKNDV(n,m0,l,d);
1.14 noro 3028: SG(d) = SG(p);
1.11 noro 3029: return d;
3030: }
3031:
1.16 noro 3032: DP ndvtodp(int mod,NDV p)
1.11 noro 3033: {
3034: DP d;
3035: MP m0,m;
3036: NMV t;
3037: int len,i,n;
3038:
3039: if ( !p )
3040: return 0;
3041: m0 = 0;
1.14 noro 3042: len = LEN(p);
1.11 noro 3043: n = NV(p);
1.17 noro 3044: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
3045: NEXTMP(m0,m);
3046: if ( mod )
1.16 noro 3047: C(m) = STOI(CM(t));
1.17 noro 3048: else
1.16 noro 3049: C(m) = (P)CQ(t);
1.17 noro 3050: DL(m) = ndltodl(n,TD(t),DL(t));
1.11 noro 3051: }
3052: NEXT(m) = 0;
3053: MKDP(NV(p),m0,d);
1.14 noro 3054: SG(d) = SG(p);
1.11 noro 3055: return d;
3056: }
3057:
1.3 noro 3058: void ndv_print(NDV p)
3059: {
3060: NMV m;
3061: int i,len;
3062:
3063: if ( !p )
3064: printf("0\n");
3065: else {
1.14 noro 3066: len = LEN(p);
1.3 noro 3067: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14 noro 3068: printf("+%d*",CM(m));
1.16 noro 3069: ndl_print(DL(m));
3070: }
3071: printf("\n");
3072: }
3073: }
3074:
3075: void ndv_print_q(NDV p)
3076: {
3077: NMV m;
3078: int i,len;
3079:
3080: if ( !p )
3081: printf("0\n");
3082: else {
3083: len = LEN(p);
3084: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3085: printf("+");
3086: printexpr(CO,CQ(m));
3087: printf("*");
1.14 noro 3088: ndl_print(DL(m));
1.3 noro 3089: }
3090: printf("\n");
3091: }
1.25 noro 3092: }
3093:
3094: int nd_equal(ND a,ND b)
3095: {
3096: NM ma,mb;
3097:
3098: if ( (!a && b)||(a&& !b)||(NV(a) != NV(b))||(SG(a) != SG(b)) ) return 0;
3099: for ( ma = BDY(a), mb = BDY(b); ma && mb; ma = NEXT(ma), mb = NEXT(mb) ) {
3100: if ( (TD(ma) != TD(mb)) || !ndl_equal(DL(ma),DL(mb)) ) return 0;
3101: }
3102: if ( !ma && !mb ) return 1;
3103: else return 0;
1.27 noro 3104: }
3105:
3106: NODE nd_reducebase(NODE x)
3107: {
3108: int len,i,j;
3109: NDV *w;
3110: NODE t,t0;
3111:
3112: len = length(x);
3113: w = (NDV *)ALLOCA(len*sizeof(NDV));
3114: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
3115: for ( i = 0; i < len; i++ ) {
3116: for ( j = 0; j < i; j++ ) {
3117: if ( w[i] && w[j] )
3118: if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
3119: else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
3120: }
3121: }
3122: for ( i = len-1, t0 = 0; i >= 0; i-- ) {
3123: if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
3124: }
3125: NEXT(t) = 0; x = t0;
3126: return x;
1.11 noro 3127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>