Annotation of OpenXM_contrib2/asir2000/engine/PD.c, Revision 1.7
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.7 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/PD.c,v 1.6 2004/08/18 00:17:02 noro Exp $
1.2 noro 49: */
1.1 noro 50: #ifndef FBASE
51: #define FBASE
52: #endif
53:
54: #include "b.h"
55: #include "ca.h"
56:
1.6 noro 57: #include "polydiv.c"
1.1 noro 58:
1.5 noro 59: void plisttop(P *f,V v,int n,P *gp)
1.1 noro 60: {
1.7 ! noro 61: int i;
! 62: DCP dc,dc0;
1.1 noro 63:
1.7 ! noro 64: for ( i = n; (i >= 0) && !f[i]; i-- );
! 65: if ( i < 0 )
! 66: *gp = 0;
! 67: else if ( i == 0 )
! 68: *gp = f[0];
! 69: else {
! 70: for ( dc0 = 0; i >= 0; i-- ) {
! 71: if ( !f[i] )
! 72: continue;
! 73: NEXTDC(dc0,dc);
! 74: if ( i )
! 75: STOQ(i,DEG(dc));
! 76: else
! 77: DEG(dc) = 0;
! 78: COEF(dc) = f[i];
! 79: }
! 80: NEXT(dc) = 0; MKP(v,dc0,*gp);
! 81: }
1.4 noro 82: }
83:
84: /* for multivariate polynomials over fields */
85:
1.5 noro 86: int divtp(VL vl,P p1,P p2,P *q)
1.4 noro 87: {
1.7 ! noro 88: register int i,j;
! 89: register DCP dc1,dc2,dc;
! 90: P m,m1,s,dvr,t;
! 91: P *pq,*pr,*pd;
! 92: V v1,v2;
! 93: Q deg1,deg2;
! 94: int d1,d2,sgn;
! 95:
! 96: if ( !p1 ) {
! 97: *q = 0;
! 98: return 1;
! 99: } else if ( NUM(p2) ) {
! 100: divsp(vl,p1,p2,q);
! 101: return 1;
! 102: } else if ( NUM(p1) ) {
! 103: *q = 0;
! 104: return 0;
! 105: } else if ( ( v1 = VR(p1) ) == ( v2 = VR(p2) ) ) {
! 106: dc1 = DC(p1); dc2 = DC(p2);
! 107: deg1 = DEG(dc1); deg2 = DEG(dc2);
! 108: sgn = cmpq(deg1,deg2);
! 109: if ( sgn == 0 )
! 110: if ( !divtp(vl,COEF(dc1),COEF(dc2),&m) ) {
! 111: *q = 0;
! 112: return 0;
! 113: } else {
! 114: mulp(vl,p2,m,&m1); subp(vl,p1,m1,&s);
! 115: if ( !s ) {
! 116: *q = m;
! 117: return 1;
! 118: } else {
! 119: *q = 0;
! 120: return 0;
! 121: }
! 122: }
! 123: else if ( sgn < 0 ) {
! 124: *q = 0;
! 125: return 0;
! 126: } else {
! 127: if ( (PL(NM(deg1)) > 1) ) {
! 128: error("divtp : invalid input");
! 129: *q = 0;
! 130: return ( 0 );
! 131: }
! 132: d1 = QTOS(deg1); d2 = QTOS(deg2);
! 133: W_CALLOC(d1-d2,P,pq); W_CALLOC(d1,P,pr); W_CALLOC(d2,P,pd);
! 134: for ( dc = dc1; dc; dc = NEXT(dc) )
! 135: pr[QTOS(DEG(dc))] = COEF(dc);
! 136: for ( dc = dc2; dc; dc = NEXT(dc) )
! 137: pd[QTOS(DEG(dc))] = COEF(dc);
! 138: for ( dvr = COEF(dc2), i = d1 - d2; i >= 0; i-- )
! 139: if ( !pr[i+d2] )
! 140: continue;
! 141: else if ( !divtp(vl,pr[i+d2],dvr,&m) ) {
! 142: *q = 0;
! 143: return 0;
! 144: } else {
! 145: pq[i] = m;
! 146: for ( j = d2; j >= 0; j-- ) {
! 147: mulp(vl,pq[i],pd[j],&m);
! 148: subp(vl,pr[i + j],m,&s); pr[i + j] = s;
! 149: }
! 150: }
! 151: plisttop(pq,v1,d1 - d2,&m); plisttop(pr,v1,d1 - 1,&t);
! 152: if ( t ) {
! 153: *q = 0;
! 154: return 0;
! 155: } else {
! 156: *q = m;
! 157: return 1;
! 158: }
! 159: }
! 160: } else {
! 161: for ( ; (v1 != vl->v) && (v2 != vl->v); vl = NEXT(vl) );
! 162: if ( v2 == vl->v ) {
! 163: *q = 0;
! 164: return 0;
! 165: } else
! 166: return divtdcp(vl,p1,p2,q);
! 167: }
1.4 noro 168: }
169:
1.5 noro 170: int divtdcp(VL vl,P p1,P p2,P *q)
1.4 noro 171: {
172:
1.7 ! noro 173: P m;
! 174: register DCP dc,dcr,dcr0;
1.4 noro 175:
1.7 ! noro 176: for ( dc = DC(p1), dcr0 = 0; dc; dc = NEXT(dc) )
! 177: if ( !divtp(vl,COEF(dc),p2,&m) ) {
! 178: *q = 0;
! 179: return 0;
! 180: } else {
! 181: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = m; NEXT(dcr) = 0;
! 182: }
! 183: MKP(VR(p1),dcr0,*q);
! 184: return 1;
1.1 noro 185: }
186:
1.5 noro 187: int divtpz(VL vl,P p1,P p2,P *q)
1.1 noro 188: {
1.7 ! noro 189: register int i,j;
! 190: register DCP dc1,dc2,dc;
! 191: P m,m1,s,dvr,t;
! 192: P *pq,*pr,*pd;
! 193: V v1,v2;
! 194: Q deg1,deg2;
! 195: int d1,d2,sgn;
! 196:
! 197: if ( !p1 ) {
! 198: *q = 0;
! 199: return ( 1 );
! 200: } else if ( NUM(p2) )
! 201: if ( NUM(p1) ) {
! 202: divq((Q)p1,(Q)p2,(Q *)&s);
! 203: if ( INT((Q)s) ) {
! 204: *q = s;
! 205: return ( 1 );
! 206: } else {
! 207: *q = 0;
! 208: return ( 0 );
! 209: }
! 210: } else
! 211: return ( divtdcpz(vl,p1,p2,q) );
! 212: else if ( NUM(p1) ) {
! 213: *q = 0;
! 214: return ( 0 );
! 215: } else if ( ( v1 = VR(p1) ) == ( v2 = VR(p2) ) ) {
! 216: Q csum1,csum2;
! 217:
! 218: csump(vl,p1,&csum1); csump(vl,p2,&csum2);
! 219: if ( csum2 && !divtpz(vl,(P)csum1,(P)csum2,&t) ) {
! 220: *q = 0;
! 221: return 0;
! 222: }
! 223: dc1 = DC(p1); dc2 = DC(p2);
! 224: deg1 = DEG(dc1); deg2 = DEG(dc2);
! 225: sgn = cmpq(deg1,deg2);
! 226: if ( sgn == 0 )
! 227: if ( !divtpz(vl,COEF(dc1),COEF(dc2),&m) ) {
! 228: *q = 0;
! 229: return ( 0 );
! 230: } else {
! 231: mulp(vl,p2,m,&m1); subp(vl,p1,m1,&s);
! 232: if ( !s ) {
! 233: *q = m;
! 234: return ( 1 );
! 235: } else {
! 236: *q = 0;
! 237: return ( 0 );
! 238: }
! 239: }
! 240: else if ( sgn < 0 ) {
! 241: *q = 0;
! 242: return ( 0 );
! 243: } else {
! 244: if ( (PL(NM(deg1)) > 1) ) {
! 245: error("divtpz : invalid input");
! 246: *q = 0;
! 247: return ( 0 );
! 248: }
! 249: d1 = QTOS(deg1); d2 = QTOS(deg2);
! 250: W_CALLOC(d1-d2,P,pq); W_CALLOC(d1,P,pr); W_CALLOC(d2,P,pd);
! 251: for ( dc = dc1; dc; dc = NEXT(dc) )
! 252: pr[QTOS(DEG(dc))] = COEF(dc);
! 253: for ( dc = dc2; dc; dc = NEXT(dc) )
! 254: pd[QTOS(DEG(dc))] = COEF(dc);
! 255: for ( dvr = COEF(dc2), i = d1 - d2; i >= 0; i-- )
! 256: if ( !pr[i+d2] )
! 257: continue;
! 258: else if ( !divtpz(vl,pr[i+d2],dvr,&m) ) {
! 259: *q = 0;
! 260: return ( 0 );
! 261: } else {
! 262: pq[i] = m;
! 263: for ( j = d2; j >= 0; j-- ) {
! 264: mulp(vl,pq[i],pd[j],&m);
! 265: subp(vl,pr[i + j],m,&s); pr[i + j] = s;
! 266: }
! 267: }
! 268: plisttop(pq,v1,d1 - d2,&m); plisttop(pr,v1,d1 - 1,&t);
! 269: if ( t ) {
! 270: *q = 0;
! 271: return ( 0 );
! 272: } else {
! 273: *q = m;
! 274: return ( 1 );
! 275: }
! 276: }
! 277: } else {
! 278: for ( ; (v1 != vl->v) && (v2 != vl->v); vl = NEXT(vl) );
! 279: if ( v2 == vl->v ) {
! 280: *q = 0;
! 281: return ( 0 );
! 282: } else
! 283: return ( divtdcpz(vl,p1,p2,q) ) ;
! 284: }
1.1 noro 285: }
286:
1.5 noro 287: int divtdcpz(VL vl,P p1,P p2,P *q)
1.1 noro 288: {
289:
1.7 ! noro 290: P m;
! 291: register DCP dc,dcr,dcr0;
1.1 noro 292:
1.7 ! noro 293: for ( dc = DC(p1), dcr0 = 0; dc; dc = NEXT(dc) )
! 294: if ( !divtpz(vl,COEF(dc),p2,&m) ) {
! 295: *q = 0;
! 296: return ( 0 );
! 297: } else {
! 298: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = m; NEXT(dcr) = 0;
! 299: }
! 300: MKP(VR(p1),dcr0,*q);
! 301: return ( 1 );
1.1 noro 302: }
303:
1.5 noro 304: void udivpz(P f1,P f2,P *fqp,P *frp)
1.1 noro 305: {
1.7 ! noro 306: register int n1,n2,i,j;
! 307: Q *pq,*pr,*pd,d,m,s,q;
! 308: DCP dc;
! 309: N qn,rn;
! 310:
! 311: if ( !f2 )
! 312: error("udivpz: division by 0");
! 313: else if ( !f1 ) {
! 314: *fqp = *frp = 0;
! 315: return;
! 316: } else if ( NUM(f1) )
! 317: if ( NUM(f2) ) {
! 318: divn(NM((Q)f1),NM((Q)f2),&qn,&rn);
! 319: if ( rn ) {
! 320: *fqp = *frp = 0;
! 321: } else {
! 322: NTOQ(qn,SGN((Q)f1)*SGN((Q)f2),q),*fqp = (P)q; *frp = 0;
! 323: }
! 324: return;
! 325: } else {
! 326: *fqp = 0; *frp = f1;
! 327: return;
! 328: }
! 329: else if ( NUM(f2) ) {
! 330: n1 = UDEG(f1); W_CALLOC(n1,Q,pq);
! 331: for ( dc = DC(f1); dc; dc = NEXT(dc) ) {
! 332: divn(NM((Q)COEF(dc)),NM((Q)f2),&qn,&rn);
! 333: if ( rn ) {
! 334: *fqp = *frp = 0;
! 335: return;
! 336: } else {
! 337: NTOQ(qn,SGN((Q)COEF(dc))*SGN((Q)f2),s); pq[QTOS(DEG(dc))] = s;
! 338: }
! 339: }
! 340: plisttop((P *)pq,VR(f1),n1,fqp);
! 341: return;
! 342: }
! 343: n1 = UDEG(f1); n2 = UDEG(f2);
! 344: if ( n1 < n2 ) {
! 345: *fqp = NULL; *frp = f1;
! 346: return;
! 347: }
! 348: W_CALLOC(n1-n2,Q,pq); W_CALLOC(n1,Q,pr); W_CALLOC(n2,Q,pd);
! 349: for ( dc = DC(f1); dc; dc = NEXT(dc) )
! 350: pr[QTOS(DEG(dc))] = (Q)COEF(dc);
! 351: for ( dc = DC(f2); dc; dc = NEXT(dc) )
! 352: pd[QTOS(DEG(dc))] = (Q)COEF(dc);
! 353: for ( d = (Q)UCOEF(f2), i = n1 - n2; i >= 0; i-- ) {
! 354: if ( !pr[i+n2] )
! 355: continue;
! 356: divn(NM(pr[i+n2]),NM(d),&qn,&rn);
! 357: if ( rn ) {
! 358: *fqp = *frp = 0;
! 359: return;
! 360: }
! 361: NTOQ(qn,SGN(pr[i+n2])*SGN(d),pq[i]);
! 362: for ( j = n2; j >= 0; j-- ) {
! 363: mulq(pq[i],pd[j],&m); subq(pr[i+j],m,&s); pr[i+j] = s;
! 364: }
! 365: }
! 366: plisttop((P *)pq,VR(f1),n1-n2,fqp); plisttop((P *)pr,VR(f1),n2-1,frp);
1.1 noro 367: }
368:
1.5 noro 369: void udivpwm(Q mod,P p1,P p2,P *q,P *r)
1.1 noro 370: {
1.7 ! noro 371: P s,t,u,tq,tr;
1.1 noro 372:
1.7 ! noro 373: invl((Q)UCOEF(p2),mod,(Q *)&t); mulpq(p2,t,&s); cmp(mod,s,&u);
! 374: udivpzwm(mod,p1,u,&tq,&tr);
! 375: cmp(mod,tr,r); mulpq(tq,t,&s); cmp(mod,s,q);
1.1 noro 376: }
377:
1.5 noro 378: void udivpzwm(Q mod,P f1,P f2,P *fqp,P *frp)
1.1 noro 379: {
1.7 ! noro 380: register int n1,n2,i,j;
! 381: Q *pq,*pr,*pd,d,m,s,q;
! 382: DCP dc;
! 383: N qn,rn;
! 384:
! 385: if ( !f2 )
! 386: error("udivpz: division by 0");
! 387: else if ( !f1 ) {
! 388: *fqp = *frp = 0;
! 389: return;
! 390: } else if ( NUM(f1) )
! 391: if ( NUM(f2) ) {
! 392: divn(NM((Q)f1),NM((Q)f2),&qn,&rn);
! 393: if ( rn ) {
! 394: *fqp = *frp = 0;
! 395: } else {
! 396: NTOQ(qn,SGN((Q)f1)*SGN((Q)f2),q),*fqp = (P)q; *frp = 0;
! 397: }
! 398: return;
! 399: } else {
! 400: *fqp = 0; *frp = f1;
! 401: return;
! 402: }
! 403: else if ( NUM(f2) ) {
! 404: n1 = UDEG(f1); W_CALLOC(n1,Q,pq);
! 405: for ( dc = DC(f1); dc; dc = NEXT(dc) ) {
! 406: divn(NM((Q)COEF(dc)),NM((Q)f2),&qn,&rn);
! 407: if ( rn ) {
! 408: *fqp = *frp = 0;
! 409: return;
! 410: } else {
! 411: NTOQ(qn,SGN((Q)COEF(dc))*SGN((Q)f2),s); pq[QTOS(DEG(dc))] = s;
! 412: }
! 413: }
! 414: plisttop((P *)pq,VR(f1),n1,fqp);
! 415: return;
! 416: }
! 417: n1 = UDEG(f1); n2 = UDEG(f2);
! 418: if ( n1 < n2 ) {
! 419: *fqp = NULL; *frp = f1;
! 420: return;
! 421: }
! 422: W_CALLOC(n1-n2,Q,pq); W_CALLOC(n1,Q,pr); W_CALLOC(n2,Q,pd);
! 423: for ( dc = DC(f1); dc; dc = NEXT(dc) )
! 424: pr[QTOS(DEG(dc))] = (Q)COEF(dc);
! 425: for ( dc = DC(f2); dc; dc = NEXT(dc) )
! 426: pd[QTOS(DEG(dc))] = (Q)COEF(dc);
! 427: for ( d = (Q)UCOEF(f2), i = n1 - n2; i >= 0; i-- ) {
! 428: if ( !pr[i+n2] )
! 429: continue;
! 430: divn(NM(pr[i+n2]),NM(d),&qn,&rn);
! 431: if ( rn ) {
! 432: *fqp = *frp = 0;
! 433: return;
! 434: }
! 435: NTOQ(qn,SGN(pr[i+n2])*SGN(d),pq[i]);
! 436: for ( j = n2; j >= 0; j-- ) {
! 437: mulq(pq[i],pd[j],&m); remq(m,mod,&s);
! 438: subq(pr[i+j],s,&m); remq(m,mod,&s); pr[i+j] = s;
! 439: }
! 440: }
! 441: plisttop((P *)pq,VR(f1),n1-n2,fqp); plisttop((P *)pr,VR(f1),n2-1,frp);
1.1 noro 442: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>