Annotation of OpenXM_contrib2/asir2000/builtin/algnum.c, Revision 1.2
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
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 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: *
! 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/algnum.c,v 1.1.1.1 1999/12/03 07:39:07 noro Exp $
! 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52:
53: void Pdefpoly(), Pnewalg(), Pmainalg(), Palgtorat(), Prattoalg(), Pgetalg();
54: void Palg(), Palgv(), Pgetalgtree();
55:
56: #if defined(THINK_C)
57: void mkalg(P,Alg *);
58: int cmpalgp(P,P);
59: void algptop(P,P *);
60: void algtorat(Num,Obj *);
61: void rattoalg(Obj,Alg *);
62: void ptoalgp(P,P *);
63: #else
64: void mkalg();
65: int cmpalgp();
66: void algptop();
67: void algtorat();
68: void rattoalg();
69: void ptoalgp();
70: void clctalg();
71: #endif
72:
73: struct ftab alg_tab[] = {
74: {"defpoly",Pdefpoly,1},
75: {"newalg",Pnewalg,1},
76: {"mainalg",Pmainalg,1},
77: {"algtorat",Palgtorat,1},
78: {"rattoalg",Prattoalg,1},
79: {"getalg",Pgetalg,1},
80: {"getalgtree",Pgetalgtree,1},
81: {"alg",Palg,1},
82: {"algv",Palgv,1},
83: {0,0,0},
84: };
85:
86: static int UCN,ACNT;
87:
88: void Pnewalg(arg,rp)
89: NODE arg;
90: Alg *rp;
91: {
92: P p;
93: VL vl;
94: P c;
95:
96: p = (P)ARG0(arg);
97: if ( !p || OID(p) != O_P )
98: error("newalg : invalid argument");
99: clctv(CO,p,&vl);
100: if ( NEXT(vl) )
101: error("newalg : invalid argument");
102: c = COEF(DC(p));
103: if ( !NUM(c) || !RATN(c) )
104: error("newalg : invalid argument");
105: mkalg(p,rp);
106: }
107:
108: void mkalg(p,r)
109: P p;
110: Alg *r;
111: {
112: VL vl,mvl,nvl;
113: V a,tv;
114: char buf[BUFSIZ];
115: char *name;
116: P x,t,s;
117: Num c;
118: DCP dc,dcr,dcr0;
119:
120: for ( vl = ALG; vl; vl = NEXT(vl) )
121: if ( !cmpalgp(p,(P)vl->v->attr) ) {
122: a = vl->v; break;
123: }
124: if ( !vl ) {
125: NEWVL(vl); NEXT(vl) = ALG; ALG = vl;
126: NEWV(a); vl->v = a;
127: sprintf(buf,"#%d",ACNT++);
128: name = (char *)MALLOC(strlen(buf)+1);
129: strcpy(name,buf); NAME(a) = name;
130:
131: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
132: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); c = (Num)COEF(dc);
133: if ( NID(c) != N_A )
134: COEF(dcr) = (P)c;
135: else
136: COEF(dcr) = (P)BDY(((Alg)c));
137: }
138: NEXT(dcr) = 0; MKP(a,dcr0,t); a->attr = (pointer)t;
139:
140: sprintf(buf,"t%s",name); makevar(buf,&s);
141:
142: if ( NEXT(ALG) ) {
143: tv = (V)NEXT(ALG)->v->priv;
144: for ( vl = CO; NEXT(NEXT(vl)); vl = NEXT(vl) );
145: nvl = NEXT(vl); NEXT(vl) = 0;
146: for ( vl = CO; NEXT(vl) && (NEXT(vl)->v != tv); vl = NEXT(vl) );
147: mvl = NEXT(vl); NEXT(vl) = nvl; NEXT(nvl) = mvl;
148: }
149:
150: a->priv = (pointer)VR(s); VR(s)->priv = (pointer)a;
151: }
152: MKV(a,x); MKAlg(x,*r);
153: }
154:
155: int cmpalgp(p,defp)
156: P p,defp;
157: {
158: DCP dc,dcd;
159: P t;
160:
161: for ( dc = DC(p), dcd = DC(defp); dc && dcd;
162: dc = NEXT(dc), dcd = NEXT(dcd) ) {
163: if ( cmpq(DEG(dc),DEG(dcd)) )
164: break;
165: t = NID((Num)COEF(dc)) == N_A ? (P)BDY((Alg)COEF(dc)) : COEF(dc);
166: if ( compp(ALG,t,COEF(dcd)) )
167: break;
168: }
169: if ( dc || dcd )
170: return 1;
171: else
172: return 0;
173: }
174:
175: void Pdefpoly(arg,rp)
176: NODE arg;
177: P *rp;
178: {
179: asir_assert(ARG0(arg),O_N,"defpoly");
180: algptop((P)VR((P)BDY((Alg)ARG0(arg)))->attr,rp);
181: }
182:
183: void Pmainalg(arg,r)
184: NODE arg;
185: Alg *r;
186: {
187: Num c;
188: V v;
189: P b;
190:
191: c = (Num)(ARG0(arg));
192: if ( NID(c) <= N_R )
193: *r = 0;
194: else {
195: v = VR((P)BDY((Alg)c)); MKV(v,b); MKAlg(b,*r);
196: }
197: }
198:
199: void Palgtorat(arg,rp)
200: NODE arg;
201: Obj *rp;
202: {
203: asir_assert(ARG0(arg),O_N,"algtorat");
204: algtorat((Num)ARG0(arg),rp);
205: }
206:
207: void Prattoalg(arg,rp)
208: NODE arg;
209: Alg *rp;
210: {
211: asir_assert(ARG0(arg),O_R,"rattoalg");
212: rattoalg((Obj)ARG0(arg),rp);
213: }
214:
215: void Pgetalg(arg,rp)
216: NODE arg;
217: LIST *rp;
218: {
219: Obj t;
220: P p;
221: VL vl;
222: Num a;
223: Alg b;
224: NODE n0,n;
225:
226: if ( !(a = (Num)ARG0(arg)) || NID(a) <= N_R )
227: vl = 0;
228: else {
229: t = BDY((Alg)a);
230: switch ( OID(t) ) {
231: case O_P: case O_R:
232: clctvr(ALG,t,&vl); break;
233: default:
234: vl = 0; break;
235: }
236: }
237: for ( n0 = 0; vl; vl = NEXT(vl) ) {
238: NEXTNODE(n0,n); MKV(vl->v,p); MKAlg(p,b); BDY(n) = (pointer)b;
239: }
240: if ( n0 )
241: NEXT(n) = 0;
242: MKLIST(*rp,n0);
243: }
244:
245: void Pgetalgtree(arg,rp)
246: NODE arg;
247: LIST *rp;
248: {
249: Obj t;
250: P p;
251: VL vl,vl1,vl2;
252: Num a;
253: Alg b;
254: NODE n0,n;
255:
256: if ( !(a = (Num)ARG0(arg)) || NID(a) <= N_R )
257: vl = 0;
258: else {
259: t = BDY((Alg)a);
260: switch ( OID(t) ) {
261: case O_P:
262: clctalg(t,&vl); break;
263: case O_R:
264: clctalg(NM((R)t),&vl1);
265: clctalg(DN((R)t),&vl2);
266: mergev(ALG,vl1,vl2,&vl); break;
267: default:
268: vl = 0; break;
269: }
270: }
271: for ( n0 = 0; vl; vl = NEXT(vl) ) {
272: NEXTNODE(n0,n); MKV(vl->v,p); MKAlg(p,b); BDY(n) = (pointer)b;
273: }
274: if ( n0 )
275: NEXT(n) = 0;
276: MKLIST(*rp,n0);
277: }
278:
279: void clctalg(p,vl)
280: P p;
281: VL *vl;
282: {
283: int n,i;
284: VL tvl;
285: VN vn,vn1;
286: P d;
287: DCP dc;
288:
289: for ( n = 0, tvl = ALG; tvl; tvl = NEXT(tvl), n++ );
290: vn = (VN) ALLOCA((n+1)*sizeof(struct oVN));
291: for ( i = n-1, tvl = ALG; tvl; tvl = NEXT(tvl), i-- ) {
292: vn[i].v = tvl->v;
293: vn[i].n = 0;
294: }
295: markv(vn,n,p);
296: for ( i = n-1; i >= 0; i-- ) {
297: if ( !vn[i].n )
298: continue;
299: d = (P)vn[i].v->attr;
300: for ( dc = DC(d); dc; dc = NEXT(dc) )
301: markv(vn,i,COEF(dc));
302: }
303: vn1 = (VN) ALLOCA((n+1)*sizeof(struct oVN));
304: for ( i = 0; i < n; i++ ) {
305: vn1[i].v = vn[n-1-i].v; vn1[i].n = vn[n-1-i].n;
306: }
307: vntovl(vn1,n,vl);
308: }
309:
310: void Palg(arg,rp)
311: NODE arg;
312: Alg *rp;
313: {
314: Q a;
315: VL vl;
316: P x;
317: int n;
318:
319: a = (Q)ARG0(arg);
320: if ( a && (OID(a) != O_N || NID(a) != N_Q || !INT(a)) )
321: *rp = 0;
322: else {
323: n = ACNT-QTOS(a)-1;
324: for ( vl = ALG; vl && n; vl = NEXT(vl), n-- );
325: if ( vl ) {
326: MKV(vl->v,x); MKAlg(x,*rp);
327: } else
328: *rp = 0;
329: }
330: }
331:
332: void Palgv(arg,rp)
333: NODE arg;
334: Obj *rp;
335: {
336: Q a;
337: VL vl;
338: P x;
339: int n;
340: Alg b;
341:
342: a = (Q)ARG0(arg);
343: if ( a && (OID(a) != O_N || NID(a) != N_Q || !INT(a)) )
344: *rp = 0;
345: else {
346: n = ACNT-QTOS(a)-1;
347: for ( vl = ALG; vl && n; vl = NEXT(vl), n-- );
348: if ( vl ) {
349: MKV(vl->v,x); MKAlg(x,b); algtorat((Num)b,rp);
350: } else
351: *rp = 0;
352: }
353: }
354:
355: void algptop(p,r)
356: P p,*r;
357: {
358: DCP dc,dcr,dcr0;
359:
360: if ( NUM(p) )
361: *r = (P)p;
362: else {
363: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
364: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc);
365: algptop(COEF(dc),&COEF(dcr));
366: }
367: NEXT(dcr) = 0; MKP((V)(VR(p)->priv),dcr0,*r);
368: }
369: }
370:
371: void algtorat(n,r)
372: Num n;
373: Obj *r;
374: {
375: Obj obj;
376: P nm,dn;
377:
378: if ( !n || NID(n) <= N_R )
379: *r = (Obj)n;
380: else {
381: obj = BDY((Alg)n);
382: if ( ID(obj) <= O_P )
383: algptop((P)obj,(P *)r);
384: else {
385: algptop(NM((R)obj),&nm); algptop(DN((R)obj),&dn);
386: divr(CO,(Obj)nm,(Obj)dn,r);
387: }
388: }
389: }
390:
391: void rattoalg(obj,n)
392: Obj obj;
393: Alg *n;
394: {
395: P nm,dn;
396: Obj t;
397:
398: if ( !obj || ID(obj) == O_N )
399: *n = (Alg)obj;
400: else if ( ID(obj) == O_P ) {
401: ptoalgp((P)obj,(P *)&t); MKAlg(t,*n);
402: } else {
403: ptoalgp(NM((R)obj),&nm); ptoalgp(DN((R)obj),&dn);
404: divr(ALG,(Obj)nm,(Obj)dn,&t); MKAlg(t,*n);
405: }
406: }
407:
408: void ptoalgp(p,r)
409: P p,*r;
410: {
411: DCP dc,dcr,dcr0;
412:
413: if ( NUM(p) )
414: *r = (P)p;
415: else {
416: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
417: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc);
418: ptoalgp(COEF(dc),&COEF(dcr));
419: }
420: NEXT(dcr) = 0; MKP((V)(VR(p)->priv),dcr0,*r);
421: }
422: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>