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