Annotation of OpenXM_contrib2/asir2000/engine/PD.c, Revision 1.5
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.5 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/PD.c,v 1.4 2001/03/29 09:49:57 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:
57: void D_DIVSRP(vl,p1,p2,q,r)
58: VL vl;
59: P p1,p2;
60: P *q,*r;
61: {
62: register int i,j;
63: register DCP dc1,dc2,dc;
64: P m,s,dvr;
65: P *pq,*pr,*pd;
66: V v1,v2;
67: Q deg1,deg2;
68: int d1,d2,sgn;
69:
70: if ( !p1 ) {
71: *q = 0; *r = 0;
72: } else if ( NUM(p2) )
73: if ( NUM(p1) ) {
74: DIVNUM(p1,p2,q); *r = 0;
75: } else {
76: DIVSDCP(vl,p1,p2,q); *r = 0;
77: }
78: else if ( NUM(p1) ) {
79: *q = 0; *r = p1;
80: } else if ( ( v1 = VR(p1) ) == ( v2 = VR(p2) ) ) {
81: dc1 = DC(p1); dc2 = DC(p2);
82: deg1 = DEG(dc1); deg2 = DEG(dc2);
83: sgn = cmpq(deg1,deg2);
84: if ( sgn == 0 ) {
85: DIVSP(vl,COEF(dc1),COEF(dc2),q);
86: MULP(vl,p2,*q,&m); SUBP(vl,p1,m,r);
87: } else if ( sgn < 0 ) {
88: *q = 0; *r = p1;
89: } else {
90: if ( (PL(NM(deg1)) > 1) )
91: error("divsrp : invalid input");
92: d1 = QTOS(deg1); d2 = QTOS(deg2);
93: W_CALLOC(d1-d2,P,pq); W_CALLOC(d1,P,pr); W_CALLOC(d2,P,pd);
94: for ( dc = dc1; dc; dc = NEXT(dc) )
95: pr[QTOS(DEG(dc))] = COEF(dc);
96: for ( dc = dc2; dc; dc = NEXT(dc) )
97: pd[QTOS(DEG(dc))] = COEF(dc);
98: for ( dvr = COEF(dc2), i = d1 - d2; i >= 0; i-- ) {
99: if ( !pr[i+d2] )
100: continue;
101: DIVSP(vl,pr[i+d2],dvr,&pq[i]);
102: for ( j = d2; j >= 0; j-- ) {
103: MULP(vl,pq[i],pd[j],&m);
104: SUBP(vl,pr[i + j],m,&s); pr[i + j] = s;
105: }
106: }
107: plisttop(pq,v1,d1 - d2,q); plisttop(pr,v1,d1 - 1,r);
108: }
109: } else {
110: for ( ; (v1 != vl->v) && (v2 != vl->v); vl = NEXT(vl) );
111: if ( v2 == vl->v ) {
112: *q = 0; *r = p1;
113: } else
114: DIVSRDCP(vl,p1,p2,q,r);
115: }
116: }
117:
118: void D_DIVSRDCP(vl,p1,p2,q,r)
119: VL vl;
120: P p1,p2,*q,*r;
121: {
122:
123: P qc,rc;
124: DCP dc,dcq,dcq0,dcr,dcr0;
125:
126: for ( dc = DC(p1), dcq0 = 0, dcr0 = 0; dc; dc = NEXT(dc) ) {
127: DIVSRP(vl,COEF(dc),p2,&qc,&rc);
128: if ( qc ) {
129: NEXTDC(dcq0,dcq); DEG(dcq) = DEG(dc); COEF(dcq) = qc;
130: }
131: if ( rc ) {
132: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = rc;
133: }
134: }
135: if ( dcq0 ) {
136: NEXT(dcq) = 0; MKP(VR(p1),dcq0,*q);
137: } else
138: *q = 0;
139: if ( dcr0 ) {
140: NEXT(dcr) = 0; MKP(VR(p1),dcr0,*r);
141: } else
142: *r = 0;
143: }
144:
145: void D_DIVSP(vl,p1,p2,q)
146: VL vl;
147: P p1,p2,*q;
148: {
149: P t;
150:
151: DIVSRP(vl,p1,p2,q,&t);
152: if ( t )
153: error("divsp: cannot happen");
154: }
155:
156: void D_DIVSDCP(vl,p1,p2,q)
157: VL vl;
158: P p1,p2,*q;
159: {
160:
161: P m;
162: register DCP dc,dcr,dcr0;
163:
164: for ( dc = DC(p1), dcr0 = 0; dc; dc = NEXT(dc) ) {
165: DIVSP(vl,COEF(dc),p2,&m);
166: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = m; NEXT(dcr) = 0;
167: }
168: MKP(VR(p1),dcr0,*q);
169: }
170:
171: #ifdef FBASE
1.5 ! noro 172: void plisttop(P *f,V v,int n,P *gp)
1.1 noro 173: {
174: int i;
175: DCP dc,dc0;
176:
177: for ( i = n; (i >= 0) && !f[i]; i-- );
178: if ( i < 0 )
179: *gp = 0;
180: else if ( i == 0 )
181: *gp = f[0];
182: else {
183: for ( dc0 = 0; i >= 0; i-- ) {
184: if ( !f[i] )
185: continue;
186: NEXTDC(dc0,dc);
187: if ( i )
188: STOQ(i,DEG(dc));
189: else
190: DEG(dc) = 0;
191: COEF(dc) = f[i];
192: }
193: NEXT(dc) = 0; MKP(v,dc0,*gp);
194: }
1.4 noro 195: }
196:
197: /* for multivariate polynomials over fields */
198:
1.5 ! noro 199: int divtp(VL vl,P p1,P p2,P *q)
1.4 noro 200: {
1.5 ! noro 201: register int i,j;
1.4 noro 202: register DCP dc1,dc2,dc;
203: P m,m1,s,dvr,t;
204: P *pq,*pr,*pd;
205: V v1,v2;
206: Q deg1,deg2;
207: int d1,d2,sgn;
208:
209: if ( !p1 ) {
210: *q = 0;
211: return 1;
212: } else if ( NUM(p2) ) {
213: divsp(vl,p1,p2,q);
214: return 1;
215: } else if ( NUM(p1) ) {
216: *q = 0;
217: return 0;
218: } else if ( ( v1 = VR(p1) ) == ( v2 = VR(p2) ) ) {
219: dc1 = DC(p1); dc2 = DC(p2);
220: deg1 = DEG(dc1); deg2 = DEG(dc2);
221: sgn = cmpq(deg1,deg2);
222: if ( sgn == 0 )
223: if ( !divtp(vl,COEF(dc1),COEF(dc2),&m) ) {
224: *q = 0;
225: return 0;
226: } else {
227: mulp(vl,p2,m,&m1); subp(vl,p1,m1,&s);
228: if ( !s ) {
229: *q = m;
230: return 1;
231: } else {
232: *q = 0;
233: return 0;
234: }
235: }
236: else if ( sgn < 0 ) {
237: *q = 0;
238: return 0;
239: } else {
240: if ( (PL(NM(deg1)) > 1) ) {
241: error("divtp : invalid input");
242: *q = 0;
243: return ( 0 );
244: }
245: d1 = QTOS(deg1); d2 = QTOS(deg2);
246: W_CALLOC(d1-d2,P,pq); W_CALLOC(d1,P,pr); W_CALLOC(d2,P,pd);
247: for ( dc = dc1; dc; dc = NEXT(dc) )
248: pr[QTOS(DEG(dc))] = COEF(dc);
249: for ( dc = dc2; dc; dc = NEXT(dc) )
250: pd[QTOS(DEG(dc))] = COEF(dc);
251: for ( dvr = COEF(dc2), i = d1 - d2; i >= 0; i-- )
252: if ( !pr[i+d2] )
253: continue;
254: else if ( !divtp(vl,pr[i+d2],dvr,&m) ) {
255: *q = 0;
256: return 0;
257: } else {
258: pq[i] = m;
259: for ( j = d2; j >= 0; j-- ) {
260: mulp(vl,pq[i],pd[j],&m);
261: subp(vl,pr[i + j],m,&s); pr[i + j] = s;
262: }
263: }
264: plisttop(pq,v1,d1 - d2,&m); plisttop(pr,v1,d1 - 1,&t);
265: if ( t ) {
266: *q = 0;
267: return 0;
268: } else {
269: *q = m;
270: return 1;
271: }
272: }
273: } else {
274: for ( ; (v1 != vl->v) && (v2 != vl->v); vl = NEXT(vl) );
275: if ( v2 == vl->v ) {
276: *q = 0;
277: return 0;
278: } else
279: return divtdcp(vl,p1,p2,q);
280: }
281: }
282:
1.5 ! noro 283: int divtdcp(VL vl,P p1,P p2,P *q)
1.4 noro 284: {
285:
286: P m;
1.5 ! noro 287: register DCP dc,dcr,dcr0;
1.4 noro 288:
289: for ( dc = DC(p1), dcr0 = 0; dc; dc = NEXT(dc) )
290: if ( !divtp(vl,COEF(dc),p2,&m) ) {
291: *q = 0;
292: return 0;
293: } else {
294: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = m; NEXT(dcr) = 0;
295: }
296: MKP(VR(p1),dcr0,*q);
297: return 1;
1.1 noro 298: }
299:
1.5 ! noro 300: int divtpz(VL vl,P p1,P p2,P *q)
1.1 noro 301: {
302: register int i,j;
303: register DCP dc1,dc2,dc;
304: P m,m1,s,dvr,t;
305: P *pq,*pr,*pd;
306: V v1,v2;
307: Q deg1,deg2;
308: int d1,d2,sgn;
309:
310: if ( !p1 ) {
311: *q = 0;
312: return ( 1 );
313: } else if ( NUM(p2) )
314: if ( NUM(p1) ) {
315: divq((Q)p1,(Q)p2,(Q *)&s);
316: if ( INT((Q)s) ) {
317: *q = s;
318: return ( 1 );
319: } else {
320: *q = 0;
321: return ( 0 );
322: }
323: } else
324: return ( divtdcpz(vl,p1,p2,q) );
325: else if ( NUM(p1) ) {
326: *q = 0;
327: return ( 0 );
328: } else if ( ( v1 = VR(p1) ) == ( v2 = VR(p2) ) ) {
329: Q csum1,csum2;
330:
331: csump(vl,p1,&csum1); csump(vl,p2,&csum2);
332: if ( csum2 && !divtpz(vl,(P)csum1,(P)csum2,&t) ) {
333: *q = 0;
334: return 0;
335: }
336: dc1 = DC(p1); dc2 = DC(p2);
337: deg1 = DEG(dc1); deg2 = DEG(dc2);
338: sgn = cmpq(deg1,deg2);
339: if ( sgn == 0 )
340: if ( !divtpz(vl,COEF(dc1),COEF(dc2),&m) ) {
341: *q = 0;
342: return ( 0 );
343: } else {
344: mulp(vl,p2,m,&m1); subp(vl,p1,m1,&s);
345: if ( !s ) {
346: *q = m;
347: return ( 1 );
348: } else {
349: *q = 0;
350: return ( 0 );
351: }
352: }
353: else if ( sgn < 0 ) {
354: *q = 0;
355: return ( 0 );
356: } else {
357: if ( (PL(NM(deg1)) > 1) ) {
358: error("divtpz : invalid input");
359: *q = 0;
360: return ( 0 );
361: }
362: d1 = QTOS(deg1); d2 = QTOS(deg2);
363: W_CALLOC(d1-d2,P,pq); W_CALLOC(d1,P,pr); W_CALLOC(d2,P,pd);
364: for ( dc = dc1; dc; dc = NEXT(dc) )
365: pr[QTOS(DEG(dc))] = COEF(dc);
366: for ( dc = dc2; dc; dc = NEXT(dc) )
367: pd[QTOS(DEG(dc))] = COEF(dc);
368: for ( dvr = COEF(dc2), i = d1 - d2; i >= 0; i-- )
369: if ( !pr[i+d2] )
370: continue;
371: else if ( !divtpz(vl,pr[i+d2],dvr,&m) ) {
372: *q = 0;
373: return ( 0 );
374: } else {
375: pq[i] = m;
376: for ( j = d2; j >= 0; j-- ) {
377: mulp(vl,pq[i],pd[j],&m);
378: subp(vl,pr[i + j],m,&s); pr[i + j] = s;
379: }
380: }
381: plisttop(pq,v1,d1 - d2,&m); plisttop(pr,v1,d1 - 1,&t);
382: if ( t ) {
383: *q = 0;
384: return ( 0 );
385: } else {
386: *q = m;
387: return ( 1 );
388: }
389: }
390: } else {
391: for ( ; (v1 != vl->v) && (v2 != vl->v); vl = NEXT(vl) );
392: if ( v2 == vl->v ) {
393: *q = 0;
394: return ( 0 );
395: } else
396: return ( divtdcpz(vl,p1,p2,q) ) ;
397: }
398: }
399:
1.5 ! noro 400: int divtdcpz(VL vl,P p1,P p2,P *q)
1.1 noro 401: {
402:
403: P m;
404: register DCP dc,dcr,dcr0;
405:
406: for ( dc = DC(p1), dcr0 = 0; dc; dc = NEXT(dc) )
407: if ( !divtpz(vl,COEF(dc),p2,&m) ) {
408: *q = 0;
409: return ( 0 );
410: } else {
411: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = m; NEXT(dcr) = 0;
412: }
413: MKP(VR(p1),dcr0,*q);
414: return ( 1 );
415: }
416:
1.5 ! noro 417: void udivpz(P f1,P f2,P *fqp,P *frp)
1.1 noro 418: {
419: register int n1,n2,i,j;
420: Q *pq,*pr,*pd,d,m,s,q;
421: DCP dc;
422: N qn,rn;
423:
424: if ( !f2 )
425: error("udivpz: division by 0");
426: else if ( !f1 ) {
427: *fqp = *frp = 0;
428: return;
429: } else if ( NUM(f1) )
430: if ( NUM(f2) ) {
431: divn(NM((Q)f1),NM((Q)f2),&qn,&rn);
432: if ( rn ) {
433: *fqp = *frp = 0;
434: } else {
435: NTOQ(qn,SGN((Q)f1)*SGN((Q)f2),q),*fqp = (P)q; *frp = 0;
436: }
437: return;
438: } else {
439: *fqp = 0; *frp = f1;
440: return;
441: }
442: else if ( NUM(f2) ) {
443: n1 = UDEG(f1); W_CALLOC(n1,Q,pq);
444: for ( dc = DC(f1); dc; dc = NEXT(dc) ) {
445: divn(NM((Q)COEF(dc)),NM((Q)f2),&qn,&rn);
446: if ( rn ) {
447: *fqp = *frp = 0;
448: return;
449: } else {
450: NTOQ(qn,SGN((Q)COEF(dc))*SGN((Q)f2),s); pq[QTOS(DEG(dc))] = s;
451: }
452: }
453: plisttop((P *)pq,VR(f1),n1,fqp);
454: return;
455: }
456: n1 = UDEG(f1); n2 = UDEG(f2);
457: if ( n1 < n2 ) {
458: *fqp = NULL; *frp = f1;
459: return;
460: }
461: W_CALLOC(n1-n2,Q,pq); W_CALLOC(n1,Q,pr); W_CALLOC(n2,Q,pd);
462: for ( dc = DC(f1); dc; dc = NEXT(dc) )
463: pr[QTOS(DEG(dc))] = (Q)COEF(dc);
464: for ( dc = DC(f2); dc; dc = NEXT(dc) )
465: pd[QTOS(DEG(dc))] = (Q)COEF(dc);
466: for ( d = (Q)UCOEF(f2), i = n1 - n2; i >= 0; i-- ) {
467: if ( !pr[i+n2] )
468: continue;
469: divn(NM(pr[i+n2]),NM(d),&qn,&rn);
470: if ( rn ) {
471: *fqp = *frp = 0;
472: return;
473: }
474: NTOQ(qn,SGN(pr[i+n2])*SGN(d),pq[i]);
475: for ( j = n2; j >= 0; j-- ) {
476: mulq(pq[i],pd[j],&m); subq(pr[i+j],m,&s); pr[i+j] = s;
477: }
478: }
479: plisttop((P *)pq,VR(f1),n1-n2,fqp); plisttop((P *)pr,VR(f1),n2-1,frp);
480: }
481:
1.5 ! noro 482: void udivpwm(Q mod,P p1,P p2,P *q,P *r)
1.1 noro 483: {
484: P s,t,u,tq,tr;
485:
486: invl((Q)UCOEF(p2),mod,(Q *)&t); mulpq(p2,t,&s); cmp(mod,s,&u);
487: udivpzwm(mod,p1,u,&tq,&tr);
488: cmp(mod,tr,r); mulpq(tq,t,&s); cmp(mod,s,q);
489: }
490:
1.5 ! noro 491: void udivpzwm(Q mod,P f1,P f2,P *fqp,P *frp)
1.1 noro 492: {
493: register int n1,n2,i,j;
494: Q *pq,*pr,*pd,d,m,s,q;
495: DCP dc;
496: N qn,rn;
497:
498: if ( !f2 )
499: error("udivpz: division by 0");
500: else if ( !f1 ) {
501: *fqp = *frp = 0;
502: return;
503: } else if ( NUM(f1) )
504: if ( NUM(f2) ) {
505: divn(NM((Q)f1),NM((Q)f2),&qn,&rn);
506: if ( rn ) {
507: *fqp = *frp = 0;
508: } else {
509: NTOQ(qn,SGN((Q)f1)*SGN((Q)f2),q),*fqp = (P)q; *frp = 0;
510: }
511: return;
512: } else {
513: *fqp = 0; *frp = f1;
514: return;
515: }
516: else if ( NUM(f2) ) {
517: n1 = UDEG(f1); W_CALLOC(n1,Q,pq);
518: for ( dc = DC(f1); dc; dc = NEXT(dc) ) {
519: divn(NM((Q)COEF(dc)),NM((Q)f2),&qn,&rn);
520: if ( rn ) {
521: *fqp = *frp = 0;
522: return;
523: } else {
524: NTOQ(qn,SGN((Q)COEF(dc))*SGN((Q)f2),s); pq[QTOS(DEG(dc))] = s;
525: }
526: }
527: plisttop((P *)pq,VR(f1),n1,fqp);
528: return;
529: }
530: n1 = UDEG(f1); n2 = UDEG(f2);
531: if ( n1 < n2 ) {
532: *fqp = NULL; *frp = f1;
533: return;
534: }
535: W_CALLOC(n1-n2,Q,pq); W_CALLOC(n1,Q,pr); W_CALLOC(n2,Q,pd);
536: for ( dc = DC(f1); dc; dc = NEXT(dc) )
537: pr[QTOS(DEG(dc))] = (Q)COEF(dc);
538: for ( dc = DC(f2); dc; dc = NEXT(dc) )
539: pd[QTOS(DEG(dc))] = (Q)COEF(dc);
540: for ( d = (Q)UCOEF(f2), i = n1 - n2; i >= 0; i-- ) {
541: if ( !pr[i+n2] )
542: continue;
543: divn(NM(pr[i+n2]),NM(d),&qn,&rn);
544: if ( rn ) {
545: *fqp = *frp = 0;
546: return;
547: }
548: NTOQ(qn,SGN(pr[i+n2])*SGN(d),pq[i]);
549: for ( j = n2; j >= 0; j-- ) {
550: mulq(pq[i],pd[j],&m); remq(m,mod,&s);
551: subq(pr[i+j],s,&m); remq(m,mod,&s); pr[i+j] = s;
552: }
553: }
554: plisttop((P *)pq,VR(f1),n1-n2,fqp); plisttop((P *)pr,VR(f1),n2-1,frp);
555: }
556: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>