Annotation of OpenXM_contrib2/asir2000/engine/NEZ.c, Revision 1.3
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 ! noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.3 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/NEZ.c,v 1.2 2000/08/21 08:31:25 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51:
52: void nezgcdnpz(vl,ps,m,pr)
53: VL vl;
54: P *ps,*pr;
55: int m;
56: {
57: P t,s,mg;
58: VL tvl,svl,avl,nvl;
59: int i,j,k;
60: N c;
61: Q cq;
62: P *pl,*pm;
63: DCP *ml;
64: Q *cl;
65: P **cp;
66: int *cn;
67:
68: pl = (P *)ALLOCA(m*sizeof(P));
69: ml = (DCP *)ALLOCA(m*sizeof(DCP));
70: cl = (Q *)ALLOCA(m*sizeof(Q));
71: for ( i = 0; i < m; i++ )
72: monomialfctr(vl,ps[i],&pl[i],&ml[i]);
73: gcdmonomial(vl,ml,m,&mg);
74: for ( i = 0; i < m; i++ ) {
75: ptozp(pl[i],1,&cl[i],&t); pl[i] = t;
76: }
77: for ( i = 1, cq = cl[0]; i < m; i++ ) {
78: gcdn(NM(cl[i]),NM(cq),&c); NTOQ(c,1,cq);
79: }
80: for ( i = 0; i < m; i++ )
81: if ( NUM(pl[i]) ) {
82: mulp(vl,(P)cq,mg,pr); return;
83: }
84: for ( i = 0, nvl = vl, avl = 0; nvl && i < m; i++ ) {
85: clctv(vl,pl[i],&tvl);
86: intersectv(nvl,tvl,&svl); nvl = svl;
87: mergev(vl,avl,tvl,&svl); avl = svl;
88: }
89: if ( !nvl ) {
90: mulp(vl,(P)cq,mg,pr); return;
91: }
92: if ( !NEXT(avl) ) {
93: nuezgcdnpzmain(vl,pl,m,&t); mulp(vl,t,(P)cq,&s); mulp(vl,s,mg,pr);
94: return;
95: }
96: for ( tvl = nvl, i = 0; tvl; tvl = NEXT(tvl), i++ );
97: for ( tvl = avl, j = 0; tvl; tvl = NEXT(tvl), j++ );
98: if ( i == j ) {
99: /* all the pl[i]'s have the same variables */
100: sortplistbyhomdeg(pl,m); nezgcdnpzmain(nvl,pl,m,&t);
101: #if 0
102: /* search the minimal degree poly */
103: for ( i = 0; i < m; i++ ) {
104: for ( tvl = nvl; tvl; tvl = NEXT(tvl) ) {
105: dt = getdeg(tvl->v,pl[i]);
106: if ( tvl == nvl || dt < d1 ) {
107: v1 = tvl->v; d1 = dt;
108: }
109: }
110: if ( i == 0 || d1 < d ) {
111: v = v1; d = d1; j = i;
112: }
113: }
114: t = pl[0]; pl[0] = pl[j]; pl[j] = t;
115: if ( v != nvl->v ) {
116: reordvar(nvl,v,&mvl);
117: for ( i = 0; i < m; i++ ) {
118: reorderp(mvl,nvl,pl[i],&t); pl[i] = t;
119: }
120: nezgcdnpzmain(mvl,pl,m,&s); reorderp(nvl,mvl,s,&t);
121: } else
122: nezgcdnpzmain(nvl,pl,m,&t);
123: #endif
124: } else {
125: cp = (P **)ALLOCA(m*sizeof(P *));
126: cn = (int *)ALLOCA(m*sizeof(int));
127: for ( i = 0; i < m; i++ ) {
128: cp[i] = (P *)ALLOCA(lengthp(pl[i])*sizeof(P));
129: cn[i] = pcoef(vl,nvl,pl[i],cp[i]);
130: }
131: for ( i = j = 0; i < m; i++ )
132: j += cn[i];
133: pm = (P *)ALLOCA(j*sizeof(P));
134: for ( i = k = 0; i < m; i++ )
135: for ( j = 0; j < cn[i]; j++ )
136: pm[k++] = cp[i][j];
137: nezgcdnpz(vl,pm,k,&t);
138: }
139: mulp(vl,t,(P)cq,&s); mulp(vl,s,mg,pr);
140: }
141:
142: void sortplistbyhomdeg(p,n)
143: P *p;
144: int n;
145: {
146: int i,j,k;
147: int *l;
148: P t;
149:
150: l = (int *)ALLOCA(n*sizeof(int));
151: for ( i = 0; i < n; i++ )
152: l[i] = homdeg(p[i]);
153: for ( i = 0; i < n; i++ )
154: for ( j = i + 1; j < n; j++ )
155: if ( l[j] < l[i] ) {
156: t = p[i]; p[i] = p[j]; p[j] = t;
157: k = l[i]; l[i] = l[j]; l[j] = k;
158: }
159: }
160:
161: void nuezgcdnpzmain(vl,ps,m,r)
162: VL vl;
163: P *ps;
164: int m;
165: P *r;
166: {
167: P *tps;
168: P f,t;
169: int i;
170:
171: for ( i = 0; i < m; i++ )
172: if ( NUM(ps[i]) ) {
173: *r = (P)ONE; return;
174: }
175: tps = (P *) ALLOCA(m*sizeof(P));
176: for ( i = 0; i < m; i++ )
177: tps[i] = ps[i];
178: sortplist(tps,m);
179: for ( i = 1, f = tps[0]; i < m && !NUM(f); i++ ) {
180: uezgcdpz(vl,f,tps[i],&t); f = t;
181: }
182: *r = f;
183: }
184:
185: void gcdmonomial(vl,dcl,m,r)
186: VL vl;
187: DCP *dcl;
188: int m;
189: P *r;
190: {
191: int i,j,n;
192: P g,x,s,t;
193: Q d;
194: DCP dc;
195: VN vn;
196:
197: for ( i = 0; i < m; i++ )
198: if ( !dcl[i] ) {
199: *r = (P)ONE; return;
200: }
201: for ( n = 0, dc = dcl[0]; dc; dc = NEXT(dc), n++ );
202: vn = (VN)ALLOCA(n*sizeof(struct oVN));
203: for ( i = 0, dc = dcl[0]; i < n; dc = NEXT(dc), i++ ) {
204: vn[i].v = VR(COEF(dc)); vn[i].n = QTOS(DEG(dc));
205: }
206: for ( i = 1; i < m; i++ ) {
207: for ( j = 0; j < n; j++ ) {
208: for ( dc = dcl[i]; dc; dc = NEXT(dc) )
209: if ( VR(COEF(dc)) == vn[j].v ) {
210: vn[j].n = MIN(vn[j].n,QTOS(DEG(dc))); break;
211: }
212: if ( !dc )
213: vn[j].n = 0;
214: }
215: for ( j = n-1; j >= 0 && !vn[j].n; j-- );
216: if ( j < 0 ) {
217: *r = (P)ONE; return;
218: } else
219: n = j+1;
220: }
221: for ( j = 0, g = (P)ONE; j < n; j++ )
222: if ( vn[j].n ) {
223: MKV(vn[j].v,x); STOQ(vn[j].n,d);
224: pwrp(vl,x,d,&t); mulp(vl,t,g,&s); g = s;
225: }
226: *r = g;
227: }
228:
229: void nezgcdnpzmain(vl,pl,m,pr)
230: VL vl;
231: P *pl,*pr;
232: int m;
233: {
234: P *ppl,*pcl;
235: int i;
236: P cont,gcd,t,s;
237: DCP dc;
238:
239: ppl = (P *)ALLOCA(m*sizeof(P));
240: pcl = (P *)ALLOCA(m*sizeof(P));
241: for ( i = 0; i < m; i++ )
242: pcp(vl,pl[i],&ppl[i],&pcl[i]);
243: nezgcdnpz(vl,pcl,m,&cont);
244: sqfrp(vl,ppl[0],&dc);
245: for ( dc = NEXT(dc), gcd = (P)ONE; dc; dc = NEXT(dc) ) {
246: if ( NUM(COEF(dc)) )
247: continue;
248: nezgcdnpp(vl,dc,ppl+1,m-1,&t);
249: if ( NUM(t) )
250: continue;
251: mulp(vl,gcd,t,&s); gcd = s;
252: }
253: mulp(vl,gcd,cont,pr);
254: }
255:
256: void nezgcdnpp(vl,dc,pl,m,r)
257: VL vl;
258: DCP dc;
259: P *pl;
260: int m;
261: P *r;
262: {
263: int i,k;
264: P g,t,s,gcd;
265: P *pm;
266:
267: nezgcdnp_sqfr_primitive(vl,COEF(dc),pl,m,&gcd);
268: if ( NUM(gcd) ) {
269: *r = (P)ONE; return;
270: }
271: pm = (P *) ALLOCA(m*sizeof(P));
272: for ( i = 0; i < m; i++ ) {
273: divsp(vl,pl[i],gcd,&pm[i]);
274: if ( NUM(pm[i]) ) {
275: *r = gcd; return;
276: }
277: }
278: for ( g = gcd, k = QTOS(DEG(dc))-1; k > 0; k-- ) {
279: nezgcdnp_sqfr_primitive(vl,g,pm,m,&t);
280: if ( NUM(t) )
281: break;
282: mulp(vl,gcd,t,&s); gcd = s;
283: for ( i = 0; i < m; i++ ) {
284: divsp(vl,pm[i],t,&s);
285: if ( NUM(s) ) {
286: *r = gcd; return;
287: }
288: pm[i] = s;
289: }
290: }
291: *r = gcd;
292: }
293:
294: /*
295: * pr = gcd(p0,ps[0],...,ps[m-1])
296: *
297: * p0 is already square-free and primitive.
298: * ps[i] is at least primitive.
299: *
300: */
301:
302: void nezgcdnp_sqfr_primitive(vl,p0,ps,m,pr)
303: VL vl;
304: int m;
305: P p0,*ps,*pr;
306: {
307: /* variables */
308: P p00,g,h,g0,h0,a0,b0;
309: P lp0,lgp0,lp00,lg,lg0,cbd,tq,t;
310: P *lc;
311: P *tps;
312: VL nvl1,nvl2,nvl,tvl;
313: V v;
314: int i,j,k,d0,dg,dg0,dmin;
315: VN vn0,vn1,vnt;
316: int nv,flag;
317:
318: /* set-up */
319: if ( NUM(p0) ) {
320: *pr = (P) ONE; return;
321: }
322: for ( v = VR(p0), i = 0; i < m; i++ )
323: if ( NUM(ps[i]) || (v != VR(ps[i])) ) {
324: *pr = (P)ONE; return;
325: }
326: tps = (P *) ALLOCA(m*sizeof(P));
327: for ( i = 0; i < m; i++ )
328: tps[i] = ps[i];
329: sortplist(tps,m);
330: /* deg(tps[0]) <= deg(tps[1]) <= ... */
331:
332: if ( !cmpq(DEG(DC(p0)),ONE) ) {
333: if ( divcheck(vl,tps,m,(P)ONE,p0) )
334: *pr = p0;
335: else
336: *pr = (P)ONE;
337: return;
338: }
339:
340: lp0 = LC(p0); dmin = d0 = deg(v,p0); lc = (P *)ALLOCA((m+1)*sizeof(P));
341: for ( lc[0] = lp0, i = 0; i < m; i++ )
342: lc[i+1] = LC(tps[i]);
343: clctv(vl,p0,&nvl);
344: for ( i = 0; i < m; i++ ) {
345: clctv(vl,tps[i],&nvl1); mergev(vl,nvl,nvl1,&nvl2); nvl = nvl2;
346: }
347: nezgcdnpz(nvl,lc,m+1,&lg);
348:
349: mulp(nvl,p0,lg,&lgp0); k = dbound(v,lgp0)+1; cbound(nvl,lgp0,(Q *)&cbd);
350: for ( nv = 0, tvl = nvl; tvl; tvl = NEXT(tvl), nv++ );
351: W_CALLOC(nv,struct oVN,vn0); W_CALLOC(nv,struct oVN,vnt);
352: W_CALLOC(nv,struct oVN,vn1);
353: for ( i = 0, tvl = NEXT(nvl); tvl; tvl = NEXT(tvl), i++ )
354: vn1[i].v = vn0[i].v = tvl->v;
355:
356: /* main loop */
357: for ( dg = deg(v,tps[0]) + 1; ; next(vn0) )
358: do {
359: for ( i = 0, j = 0; vn0[i].v; i++ )
360: if ( vn0[i].n ) vnt[j++].v = (V)i;
361: vnt[j].n = 0;
362:
363: /* find b s.t. LC(p0)(b), LC(tps[i])(b) != 0 */
364: mulsgn(vn0,vnt,j,vn1); substvp(nvl,p0,vn1,&p00);
365: flag = (!zerovpchk(nvl,lp0,vn1) && sqfrchk(p00));
366: for ( i = 0; flag && (i < m); i++ )
367: flag &= (!zerovpchk(nvl,LC(tps[i]),vn1));
368: if ( !flag )
369: continue;
370:
371: /* substitute y -> b */
372: substvp(nvl,lg,vn1,&lg0); lp00 = LC(p00);
373: /* extended-gcd in 1-variable */
374: uexgcdnp(nvl,p00,tps,m,vn1,(Q)cbd,&g0,&h0,&a0,&b0,(Q *)&tq);
375: if ( NUM(g0) ) {
376: *pr = (P)ONE; return;
377: } else if ( dg > ( dg0 = deg(v,g0) ) ) {
378: dg = dg0;
379: if ( dg == d0 ) {
380: if ( divcheck(nvl,tps,m,lp0,p0) ) {
381: *pr = p0; return;
382: }
383: } else if ( dg == deg(v,tps[0]) ) {
384: if ( divtpz(nvl,p0,tps[0],&t) &&
385: divcheck(nvl,tps+1,m-1,LC(tps[0]),tps[0]) ) {
386: *pr = tps[0]; return;
387: } else
388: break;
389: } else {
390: henmv(nvl,vn1,lgp0,g0,h0,a0,b0,lg,lp0,lg0,lp00,(Q)tq,k,&g,&h);
391: if ( divtpz(nvl,lgp0,g,&t) &&
392: divcheck(nvl,tps,m,lg,g) ) {
393: pcp(nvl,g,pr,&t); return;
394: }
395: }
396: }
397: } while ( !nextbin(vnt,j) );
398: }
399:
400: void intersectv(vl1,vl2,vlp)
401: VL vl1,vl2,*vlp;
402: {
403: int i,n;
404: VL tvl;
405: VN tvn;
406:
407: if ( !vl1 || !vl2 ) {
408: *vlp = 0; return;
409: }
410: for ( n = 0, tvl = vl1; tvl; tvl = NEXT(tvl), n++ );
411: tvn = (VN) ALLOCA(n*sizeof(struct oVN));
412: for ( i = 0, tvl = vl1; i < n; tvl = NEXT(tvl), i++ ) {
413: tvn[i].v = tvl->v; tvn[i].n = 0;
414: }
415: for ( tvl = vl2; tvl; tvl = NEXT(tvl) )
416: for ( i = 0; i < n; i++ )
417: if ( tvn[i].v == tvl->v ) {
418: tvn[i].n = 1; break;
419: }
420: vntovl(tvn,n,vlp);
421: }
422:
423: int pcoef(vl,ivl,p,cp)
424: VL vl,ivl;
425: P p;
426: P *cp;
427: {
428: VL nvl,tvl,svl,mvl,mvl0;
429: P t;
430:
431: if ( NUM(p) ) {
432: cp[0] = p; return 1;
433: } else {
434: clctv(vl,p,&nvl);
435: for ( tvl = nvl, mvl0 = 0; tvl; tvl = NEXT(tvl) ) {
436: for ( svl = ivl; svl; svl = NEXT(svl) )
437: if ( tvl->v == svl->v )
438: break;
439: if ( !svl ) {
440: if ( !mvl0 ) {
441: NEWVL(mvl0); mvl = mvl0;
442: } else {
443: NEWVL(NEXT(mvl)); mvl = NEXT(mvl);
444: }
445: mvl->v = tvl->v;
446: }
447: }
448: if ( mvl0 )
449: NEXT(mvl) = ivl;
450: else
451: mvl0 = ivl;
452: reorderp(mvl0,nvl,p,&t);
453: return pcoef0(mvl0,ivl,t,cp);
454: }
455: }
456:
457: int pcoef0(vl,ivl,p,cp)
458: VL vl,ivl;
459: P p;
460: P *cp;
461: {
462: int cn,n;
463: DCP dc;
464: V v;
465: VL tvl;
466:
467: if ( NUM(p) ) {
468: cp[0] = p; return 1;
469: } else {
470: for ( v = VR(p), tvl = ivl; tvl; tvl = NEXT(tvl) )
471: if ( v == tvl->v )
472: break;
473: if ( tvl ) {
474: cp[0] = p; return 1;
475: } else {
476: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc) ) {
477: cn = pcoef0(vl,ivl,COEF(dc),cp); cp += cn; n += cn;
478: }
479: return n;
480: }
481: }
482: }
483:
484: int lengthp(p)
485: P p;
486: {
487: int n;
488: DCP dc;
489:
490: if ( NUM(p) )
491: return 1;
492: else {
493: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc) )
494: n += lengthp(COEF(dc));
495: return n;
496: }
497: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>