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