Annotation of OpenXM_contrib2/asir2000/engine/A.c, Revision 1.6
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.6 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/A.c,v 1.5 2001/10/09 01:36:09 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
1.5 noro 51:
52: int get_lprime();
1.1 noro 53:
54: void pdiva(vl,p0,p1,p2,pr)
55: VL vl;
56: P p1,p2,p0;
57: P *pr;
58: {
59: Q m,d;
60: P t,q,r;
61:
62: if ( VR(p2) == VR(p0) ) {
63: *pr = p1;
64: return;
65: }
66: if ( VR(p1) == VR(p0) ) {
67: error("pdiva : ???");
68: return;
69: }
70: subq(DEG(DC(p1)),DEG(DC(p2)),&m); addq(m,ONE,&d);
71: pwrq((Q)COEF(DC(p2)),d,&m); mulp(vl,p1,(P)m,&t);
72: divsrp(vl,t,p2,&q,&r); remsdcp(vl,q,p0,pr);
73: }
74:
75: void rema(vl,p0,p1,p2,pr)
76: VL vl;
77: P p1,p2,p0,*pr;
78: {
79: P m,m1,m2,g,lc,x,t;
80: Q q;
81: V v;
82: int d1,d2;
83:
84: if ( !p1 || !p2 || NUM(p2) || ( VR(p2) == VR(p0) ) )
85: *pr = 0;
86: else if ( NUM(p1) )
87: *pr = p1;
88: else if ( ( v = VR(p1) ) == VR(p0) )
89: remsdcp(vl,p1,p0,pr);
90: else if ( ( d1 = deg(v,p1) ) < ( d2 = deg(v,p2) ) )
91: *pr = p1;
92: else if ( d1 == d2 ) {
93: mulp(vl,p1,LC(p2),&m); mulp(vl,p2,LC(p1),&m1);
94: subp(vl,m,m1,&m2);
95: remsdcp(vl,m2,p0,pr);
96: } else {
97: g = p1; lc = LC(p2); MKV(v,x);
98: while ( ( d1 = deg(v,g) ) >= d2 ) {
99: mulp(vl,g,lc,&m); mulp(vl,p2,LC(g),&m1);
100: STOQ(d1-d2,q); pwrp(vl,x,q,&t);
101: mulp(vl,m1,t,&m2); subp(vl,m,m2,&m1);
102: remsdcp(vl,m1,p0,&g);
103: }
104: *pr = g;
105: }
106: }
107:
108: void gcda(vl,p0,p1,p2,pr)
109: VL vl;
110: P p1,p2,p0,*pr;
111: {
112: P g1,g2,r,t,c;
113:
114: remsdcp(vl,p1,p0,&t); pcp(vl,t,&g1,&c); remsdcp(vl,p2,p0,&g2);
115: while ( 1 ) {
116: if ( NUM(g2) || (VR(g2) == VR(p0)) ) {
117: *pr = (P)ONE;
118: return;
119: }
120: pcp(vl,g2,&t,&c); pmonic(vl,p0,t,&g2); rema(vl,p0,g1,g2,&r);
121: if ( !r ) {
122: pmonic(vl,p0,g2,pr);
123: return;
124: }
125: g1 = g2; g2 = r;
126: }
127: }
128:
129: void sqa(vl,p0,p,dcp)
130: VL vl;
131: P p,p0;
132: DCP *dcp;
133: {
134: V v;
135: int i,j,n;
136: P f,f1,f2,gcd,flat,q,r,t;
137: P *a;
138: int *b;
139: DCP dc,dc0;
140:
141: if ( ( v = VR(p) ) == VR(p0) ) {
142: NEWDC(dc); *dcp = dc; COEF(dc) = (P)ONE; DEG(dc) = ONE;
143: NEXT(dc) = 0;
144: } else {
145: n = deg(v,p); W_CALLOC(n,P,a); W_CALLOC(n,int,b);
146: for ( i = 0, f = p; ;i++ ) {
147: if ( VR(f) != v )
148: break;
149: remsdcp(vl,f,p0,&f1); diffp(vl,f1,VR(f1),&f2);
150: while ( 1 ) {
151: pmonic(vl,p0,f2,&t); f2 = t; rema(vl,p0,f1,f2,&r);
152: if ( !r ) {
153: pmonic(vl,p0,f2,&gcd); pqra(vl,p0,f,gcd,&flat,&r);
154: break;
155: }
156: f1 = f2; f2 = r;
157: }
158: if ( VR(gcd) != v ) {
159: a[i] = f; b[i] = 1;
160: break;
161: }
162: for ( j = 1, f = gcd; ; j++ ) {
163: pqra(vl,p0,f,flat,&q,&r);
164: if ( r )
165: break;
166: else
167: f = q;
168: }
169: a[i] = flat; b[i] = j;
170: }
171: for ( i = 0, j = 0, dc0 = 0; a[i]; i++ ) {
172: NEXTDC(dc0,dc); j += b[i]; STOQ(j,DEG(dc));
173: if ( a[i+1] )
174: pqra(vl,p0,a[i],a[i+1],&COEF(dc),&t);
175: else
176: COEF(dc) = a[i];
177: }
178: NEXT(dc) = 0; *dcp = dc0;
179: }
180: }
181:
182: void pqra(vl, p0, p1, p2, p, pr)
183: VL vl;
184: P p0,p1,p2;
185: P *p,*pr;
186: {
187: V v,v1;
188: P m,m1,q,r;
189: Q tq;
190: int n;
191:
192: if ( ( v = VR(p1) ) != ( v1 = VR(p2) ) ) {
193: while ( ( VR(vl) != v ) && ( VR(vl) != v1 ) )
194: vl = NEXT(vl);
195: if ( VR(vl) == v ) {
196: *p = p1; *pr = 0;
197: } else {
198: *p = 0; *pr = p1;
199: }
200: } else if ( (n = deg(v,p1) - deg(v,p2)) < 0 ) {
201: *pr = p1; *p = 0;
202: } else {
203: n++; STOQ(n,tq);
204: pwrp(vl,LC(p2),tq,&m); mulp(vl,m,p1,&m1); divsrp(vl,m1,p2,&q,&r);
205: remsdcp(vl,q,p0,p); remsdcp(vl,r,p0,pr);
206: }
207: }
208:
209: void pmonic(vl, p0, p, pr)
210: VL vl;
211: P p,p0,*pr;
212: {
213: P d,m,pp,c,t;
214:
215: if ( NUM(p) || ( VR(p) == VR(p0) ) )
216: *pr = (P)ONE;
217: else if ( NUM(COEF(DC(p))) )
218: *pr = p;
219: else {
220: ptozp(COEF(DC(p)),1,(Q *)&c,&pp); pinva(p0,pp,&d);
221: mulp(vl,p,d,&m); remsdcp(vl,m,p0,&t); ptozp(t,1,(Q *)&c,pr);
222: }
223: }
224:
225: void remsdcp(vl, p, p0, pr)
226: VL vl;
227: P p,p0,*pr;
228: {
229: P q,r,c;
230: DCP dc,dcr,dcr0;
231:
232: if ( !p )
233: *pr = 0;
234: else if ( NUM(p) )
235: *pr = (P)ONE;
236: else if ( VR(p) == VR(p0) ) {
237: divsrp(vl,p,p0,&q,&r); ptozp(r,1,(Q *)&c,pr);
238: } else {
239: for ( dcr = dcr0 = 0, dc = DC(p); dc; dc = NEXT(dc) ) {
240: divsrp(vl,COEF(dc),p0,&q,&r);
241: if ( r ) {
242: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = r;
243: }
244: }
245: if ( !dcr0 )
246: *pr = 0;
247: else {
248: NEXT(dcr) = 0; MKP(VR(p),dcr0,r); ptozp(r,1,(Q *)&c,pr);
249: }
250: }
251: }
252:
253: void pinva(p0, p, pr)
254: P p,p0,*pr;
255: {
256: V v;
257: Q f,q,q1;
258: Q u,t,a,b,s;
259: P c,c1;
260: P tg,th,tr;
261: P z,zz,zzz;
262: UM wg,wh,ws,wt,wm;
263: int n,m,modres,mod,index,i;
264:
265: v = VR(p); n=deg(v,p); m=deg(v,p0);
266: norm(p,&a); norm(p0,&b);
267: STOQ(m,u); pwrq(a,u,&t); STOQ(n,u); pwrq(b,u,&s);
268: mulq(t,s,&u);
269: factorial(n+m,&t); mulq(u,t,&s); addq(s,s,&f);
270: wg = W_UMALLOC(m+n); wh = W_UMALLOC(m+n);
271: wt = W_UMALLOC(m+n); ws = W_UMALLOC(m+n);
272: wm = W_UMALLOC(m+n);
273: for ( q = ONE, t = 0, c = 0, index = 0; ; ) {
1.4 noro 274: mod = get_lprime(index++);
1.1 noro 275: if ( !rem(NM((Q)LC(p)),mod) || !rem(NM((Q)LC(p0)),mod) )
276: continue;
277: ptomp(mod,p,&tg); ptomp(mod,p0,&th); srchump(mod,tg,th,&tr);
278: if ( !tr )
279: continue;
280: else
281: modres = CONT((MQ)tr);
282: mptoum(tg,wg); mptoum(th,wh);
283: eucum(mod,wg,wh,ws,wt); /* wg * ws + wh * wt = 1 */
284: DEG(wm) = 0; COEF(wm)[0] = modres;
285: mulum(mod,ws,wm,wt);
286: for ( i = DEG(wt); i >= 0; i-- )
287: if ( ( COEF(wt)[i] * 2 ) > mod )
288: COEF(wt)[i] -= mod;
289: chnrem(mod,v,c,q,wt,&c1,&q1);
290: if ( !ucmpp(c,c1) ) {
291: mulp(CO,c,p,&z); divsrp(CO,z,p0,&zz,&zzz);
292: if ( NUM(zzz) ) {
293: q = q1; c = c1; break;
294: }
295: }
296: q = q1; c = c1;
297: if ( cmpq(f,q) < 0 )
298: break;
299: }
300: ptozp(c,1,&s,pr);
1.6 ! noro 301: }
! 302:
! 303: /*
! 304: * compute g s.t. h*p0+g*p = res(v,p0,p)
! 305: */
! 306:
! 307: void inva_chrem(P p0, P p, NODE *pr)
! 308: {
! 309: V v;
! 310: Q f,q,q1;
! 311: Q u,t,a,b,s,dn;
! 312: P c,c0,c1;
! 313: P tg,th,tr;
! 314: P z,zz,zzz;
! 315: UM wg,wh,ws,wt,wm;
! 316: int n,m,modres,mod,index,i;
! 317:
! 318: v = VR(p); n=deg(v,p); m=deg(v,p0);
! 319: norm(p,&a); norm(p0,&b);
! 320: STOQ(m,u); pwrq(a,u,&t); STOQ(n,u); pwrq(b,u,&s);
! 321: mulq(t,s,&u);
! 322: factorial(n+m,&t); mulq(u,t,&s); addq(s,s,&f);
! 323: wg = W_UMALLOC(m+n); wh = W_UMALLOC(m+n);
! 324: wt = W_UMALLOC(m+n); ws = W_UMALLOC(m+n);
! 325: wm = W_UMALLOC(m+n);
! 326: for ( q = ONE, t = 0, c = 0, index = 0; ; ) {
! 327: mod = get_lprime(index++);
! 328: if ( !rem(NM((Q)LC(p)),mod) || !rem(NM((Q)LC(p0)),mod) )
! 329: continue;
! 330: ptomp(mod,p,&tg); ptomp(mod,p0,&th); srchump(mod,tg,th,&tr);
! 331: if ( !tr )
! 332: continue;
! 333: else
! 334: modres = CONT((MQ)tr);
! 335: mptoum(tg,wg); mptoum(th,wh);
! 336: eucum(mod,wg,wh,ws,wt); /* wg * ws + wh * wt = 1 */
! 337: DEG(wm) = 0; COEF(wm)[0] = modres;
! 338: mulum(mod,ws,wm,wt);
! 339: for ( i = DEG(wt); i >= 0; i-- )
! 340: if ( ( COEF(wt)[i] * 2 ) > mod )
! 341: COEF(wt)[i] -= mod;
! 342: chnrem(mod,v,c,q,wt,&c1,&q1);
! 343: if ( !ucmpp(c,c1) ) {
! 344: mulp(CO,c,p,&z); divsrp(CO,z,p0,&zz,&zzz);
! 345: if ( NUM(zzz) ) {
! 346: q = (Q)zzz; break;
! 347: }
! 348: }
! 349: q = q1; c = c1;
! 350: if ( cmpq(f,q) < 0 ) {
! 351: mulp(CO,c,p,&z); divsrp(CO,z,p0,&zz,&zzz);
! 352: q = (Q)zzz;
! 353: break;
! 354: }
! 355: }
! 356: /* c*p = q mod p0 */
! 357: /* c = s*c0 */
! 358: ptozp(c,1,&s,&c0);
! 359: /* c/q = c0/(q/s) */
! 360: divq(q,s,&dn);
! 361: *pr = (NODE)mknode(2,c0,dn);
1.1 noro 362: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>