Annotation of OpenXM_contrib2/asir2000/parse/puref.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/parse/puref.c,v 1.6 2004/03/11 09:52:56 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52:
53: NODE pflist;
54:
1.4 noro 55: void searchpf(char *name,FUNC *fp)
1.1 noro 56: {
57: NODE node;
58: PF pf;
59: FUNC t;
60:
61: for ( node = pflist; node; node = NEXT(node) )
62: if ( !strcmp(name,((PF)node->body)->name) ) {
63: pf = (PF)node->body;
64: *fp = t = (FUNC)MALLOC(sizeof(struct oFUNC));
65: t->name = name; t->id = A_PURE; t->argc = pf->argc;
1.5 noro 66: t->f.puref = pf; t->fullname = name;
1.1 noro 67: return;
68: }
69: *fp = 0;
70: }
71:
1.4 noro 72: void searchc(char *name,FUNC *fp)
1.1 noro 73: {
74: NODE node;
75: PF pf;
76: FUNC t;
77:
78: for ( node = pflist; node; node = NEXT(node) )
79: if ( !strcmp(name,((PF)node->body)->name)
80: && !((PF)node->body)->argc ) {
81: pf = (PF)node->body;
82: *fp = t = (FUNC)MALLOC(sizeof(struct oFUNC));
83: t->name = name; t->id = A_PURE; t->argc = pf->argc;
1.5 noro 84: t->f.puref = pf; t->fullname = name;
1.1 noro 85: return;
86: }
87: *fp = 0;
88: }
89:
1.4 noro 90: void mkpf(char *name,Obj body,int argc,V *args,
91: int (*parif)(),double (*libmf)(), int (*simp)(),PF *pfp)
1.1 noro 92: {
93: PF pf;
94: NODE node;
95:
96: NEWPF(pf); pf->name = name; pf->body = body;
97: pf->argc = argc; pf->args = args; pf->pari = parif; pf->simplify = simp;
98: pf->libm = libmf;
99: for ( node = pflist; node; node = NEXT(node) )
100: if ( !strcmp(((PF)BDY(node))->name,name) )
101: break;
102: if ( !node ) {
103: NEWNODE(node); NEXT(node) = pflist; pflist = node;
104: /* fprintf(stderr,"%s() defined.\n",name); */
105: } else
106: fprintf(stderr,"%s() redefined.\n",name);
107: BDY(node) = (pointer)pf; *pfp = pf;
108: }
109:
110: /*
111: create an instance of a pure function. args are given
112: as an array of V. So we have to create a P object for
113: each arg.
114: */
115:
1.4 noro 116: void mkpfins(PF pf,V *args,V *vp)
1.1 noro 117: {
118: V v;
119: PFINS ins;
120: PFAD ad;
121: int i;
122: P t;
123:
124: NEWV(v); NAME(v) = 0; v->attr = (pointer)V_PF;
125: ins = (PFINS)MALLOC(sizeof(PF)+pf->argc*sizeof(struct oPFAD));
126: bzero((char *)ins,(int)(sizeof(PF)+pf->argc*sizeof(struct oPFAD)));
127: ins->pf = pf;
128: v->priv = (pointer)ins;
129: for ( i = 0, ad = ins->ad; i < pf->argc; i++ ) {
130: ad[i].d = 0; MKV(args[i],t); ad[i].arg = (Obj)t;
131: }
132: appendpfins(v,vp);
133: }
134:
135: /* the same as above. Argements are given as an array of Obj */
136:
1.4 noro 137: void _mkpfins(PF pf,Obj *args,V *vp)
1.1 noro 138: {
139: V v;
140: PFINS ins;
141: PFAD ad;
142: int i;
143:
144: NEWV(v); NAME(v) = 0; v->attr = (pointer)V_PF;
145: ins = (PFINS)MALLOC(sizeof(PF)+pf->argc*sizeof(struct oPFAD));
146: bzero((char *)ins,(int)(sizeof(PF)+pf->argc*sizeof(struct oPFAD)));
147: ins->pf = pf;
148: v->priv = (pointer)ins;
149: for ( i = 0, ad = ins->ad; i < pf->argc; i++ ) {
150: ad[i].d = 0; ad[i].arg = args[i];
151: }
152: appendpfins(v,vp);
153: }
154:
155: /* the same as above. darray is also given */
156:
1.4 noro 157: void _mkpfins_with_darray(PF pf,Obj *args,int *darray,V *vp)
1.1 noro 158: {
159: V v;
160: PFINS ins;
161: PFAD ad;
162: int i;
163:
164: NEWV(v); NAME(v) = 0; v->attr = (pointer)V_PF;
165: ins = (PFINS)MALLOC(sizeof(PF)+pf->argc*sizeof(struct oPFAD));
166: bzero((char *)ins,(int)(sizeof(PF)+pf->argc*sizeof(struct oPFAD)));
167: ins->pf = pf;
168: v->priv = (pointer)ins;
169: for ( i = 0, ad = ins->ad; i < pf->argc; i++ ) {
170: ad[i].d = darray[i]; ad[i].arg = args[i];
171: }
172: appendpfins(v,vp);
173: }
174:
1.4 noro 175: void appendpfins(V v,V *vp)
1.1 noro 176: {
177: PF fdef;
178: PFAD ad,tad;
179: NODE node;
180: int i;
181:
182: fdef = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
183: for ( node = fdef->ins; node; node = NEXT(node) ) {
184: for ( i = 0, tad = ((PFINS)((V)node->body)->priv)->ad;
185: i < fdef->argc; i++ )
1.6 noro 186: if ( (ad[i].d != tad[i].d) || !equalr(CO,ad[i].arg,tad[i].arg) )
1.1 noro 187: break;
188: if ( i == fdef->argc ) {
189: *vp = (V)node->body;
190: return;
191: }
192: }
193: NEWNODE(node); node->body = (pointer)v; NEXT(node) = fdef->ins;
194: fdef->ins = node; appendvar(CO,v); *vp = v;
195: }
196:
1.4 noro 197: void duppfins(V v,V *vp)
1.1 noro 198: {
199: V tv;
200: PFINS tins;
201: int size;
202:
203: NEWV(tv); tv->name = v->name; tv->attr = v->attr;
204: size = sizeof(PF)+((PFINS)v->priv)->pf->argc*sizeof(struct oPFAD);
205: tins = (PFINS)MALLOC(size); bcopy((char *)v->priv,(char *)tins,size);
206: tv->priv = (pointer)tins;
207: *vp = tv;
208: }
209:
1.4 noro 210: void derivvar(VL vl,V pf,V v,Obj *a)
1.1 noro 211: {
212: Obj t,s,u,w,u1;
213: P p;
214: V tv,sv;
215: PF fdef;
216: PFAD ad;
217: int i,j;
218:
219: fdef = ((PFINS)pf->priv)->pf; ad = ((PFINS)pf->priv)->ad;
220: if ( fdef->deriv ) {
221: for ( t = 0, i = 0; i < fdef->argc; i++ ) {
222: derivr(vl,ad[i].arg,v,&s);
223: for ( j = 0, u = fdef->deriv[i]; j < fdef->argc; j++ ) {
224: substr(vl,0,u,fdef->args[j],ad[j].arg,&u1); u = u1;
225: }
226: mulr(vl,s,u,&w); addr(vl,t,w,&s); t = s;
227: }
228: *a = t;
229: } else {
230: for ( t = 0, i = 0; i < fdef->argc; i++ ) {
231: derivr(vl,ad[i].arg,v,&s);
232: duppfins(pf,&tv); (((PFINS)tv->priv)->ad)[i].d++;
233: appendpfins(tv,&sv);
234: MKV(sv,p); mulr(vl,s,(Obj)p,&w); addr(vl,t,w,&s); t = s;
235: }
236: *a = t;
237: }
238: }
239:
1.4 noro 240: void derivr(VL vl,Obj a,V v,Obj *b)
1.1 noro 241: {
242: VL tvl,svl;
243: Obj r,s,t,u,nm,dn,dnm,ddn,m;
244:
245: if ( !a )
246: *b = 0;
247: else
248: switch ( OID(a) ) {
249: case O_N:
250: *b = 0; break;
251: case O_P:
252: clctvr(vl,a,&tvl);
253: for ( dnm = 0, svl = tvl; svl; svl = NEXT(svl) ) {
254: if ( svl->v == v ) {
255: pderivr(vl,a,v,&s); addr(vl,s,dnm,&u); dnm = u;
256: } else if ( (vid)svl->v->attr == V_PF ) {
257: pderivr(vl,a,svl->v,&s); derivvar(vl,svl->v,v,&r);
258: mulr(vl,s,r,&u); addr(vl,u,dnm,&s); dnm = s;
259: }
260: }
261: *b = (Obj)dnm; break;
262: case O_R:
263: clctvr(vl,a,&tvl);
264: nm = (Obj)NM((R)a); dn = (Obj)DN((R)a);
265: for ( dnm = ddn = 0, svl = tvl; svl; svl = NEXT(svl) ) {
266: if ( svl->v == v ) {
267: pderivr(vl,nm,v,&s); addr(vl,s,dnm,&u); dnm = u;
268: pderivr(vl,dn,v,&s); addr(vl,s,ddn,&u); ddn = u;
269: } else if ( (vid)svl->v->attr == V_PF ) {
270: pderivr(vl,nm,svl->v,&s); derivvar(vl,svl->v,v,&r);
271: mulr(vl,s,r,&u); addr(vl,u,dnm,&s); dnm = s;
272: pderivr(vl,dn,svl->v,&s); derivvar(vl,svl->v,v,&r);
273: mulr(vl,s,r,&u); addr(vl,u,ddn,&s); ddn = s;
274: }
275: }
276: mulr(vl,dnm,dn,&t); mulr(vl,ddn,nm,&s);
277: subr(vl,t,s,&u); reductr(vl,u,&t);
278: if ( !t )
279: *b = 0;
280: else {
281: mulp(vl,(P)dn,(P)dn,(P *)&m); divr(vl,t,m,b);
282: }
283: break;
284: }
285: }
286:
1.7 ! noro 287: void gen_pwrr(VL vl,Obj a,Obj d,Obj *r)
! 288: {
! 289: if ( INT(d) )
! 290: pwrr(vl,a,d,r);
! 291: else
! 292: mkpow(vl,a,d,r);
! 293: }
! 294:
1.4 noro 295: void substr(VL vl,int partial,Obj a,V v,Obj b,Obj *c)
1.1 noro 296: {
297: Obj nm,dn,t;
298:
299: if ( !a )
300: *c = 0;
301: else {
302: switch ( OID(a) ) {
303: case O_N:
304: *c = a; break;
305: case O_P:
306: substpr(vl,partial,a,v,b,c); break;
307: case O_R:
308: substpr(vl,partial,(Obj)NM((R)a),v,b,&nm);
309: substpr(vl,partial,(Obj)DN((R)a),v,b,&dn);
310: divr(vl,nm,dn,&t); reductr(vl,t,c);
311: break;
312: default:
313: *c = 0; break;
314: }
315: }
316: }
317:
1.4 noro 318: void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj *pr)
1.1 noro 319: {
320: P x;
321: Obj t,m,c,s,a;
322: DCP dc;
323: Q d;
324: V v;
325: PF pf;
326: PFAD ad,tad;
327: PFINS tins;
328: int i;
329:
330: if ( !p )
331: *pr = 0;
332: else if ( NUM(p) )
333: *pr = (Obj)p;
334: else if ( (v = VR((P)p)) != v0 ) {
335: if ( !partial && ((vid)v->attr == V_PF) ) {
336: ad = ((PFINS)v->priv)->ad; pf = ((PFINS)v->priv)->pf;
337: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
338: tins->pf = pf;
339: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
340: tad[i].d = ad[i].d;
341: substr(vl,partial,ad[i].arg,v0,p0,&tad[i].arg);
342: }
343: simplify_ins(tins,(Obj *)&x);
344: } else
345: MKV(VR((P)p),x);
346: for ( c = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
347: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t);
348: if ( DEG(dc) ) {
1.7 ! noro 349: gen_pwrr(vl,(Obj)x,(Obj)DEG(dc),&s);
! 350: mulr(vl,s,t,&m);
1.1 noro 351: addr(vl,m,c,&a); c = a;
352: } else {
353: addr(vl,t,c,&a); c = a;
354: }
355: }
356: *pr = c;
357: } else {
358: dc = DC((P)p);
359: if ( !partial )
360: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&c);
361: else
362: c = (Obj)COEF(dc);
363: for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) {
1.7 ! noro 364: subq(d,DEG(dc),(Q *)&t);
! 365: gen_pwrr(vl,p0,t,&s); mulr(vl,s,c,&m);
1.1 noro 366: if ( !partial )
367: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t);
368: else
369: t = (Obj)COEF(dc);
370: addr(vl,m,t,&c);
371: }
372: if ( d ) {
1.7 ! noro 373: gen_pwrr(vl,p0,(Obj)d,&t);
! 374: mulr(vl,t,c,&m);
1.1 noro 375: c = m;
376: }
377: *pr = c;
378: }
379: }
380:
1.4 noro 381: void evalr(VL vl,Obj a,int prec,Obj *c)
1.1 noro 382: {
383: Obj nm,dn;
384:
385: if ( !a )
386: *c = 0;
387: else {
388: switch ( OID(a) ) {
389: case O_N:
390: *c = a; break;
391: case O_P:
392: evalp(vl,(P)a,prec,(P *)c); break;
393: case O_R:
394: evalp(vl,NM((R)a),prec,(P *)&nm); evalp(vl,DN((R)a),prec,(P *)&dn);
395: divr(vl,nm,dn,c);
396: break;
397: default:
398: error("evalr : not implemented"); break;
399: }
400: }
401: }
402:
1.4 noro 403: void evalp(VL vl,P p,int prec,P *pr)
1.1 noro 404: {
405: P t;
406: DCP dc,dcr0,dcr;
407: Obj u;
408:
409: if ( !p || NUM(p) )
410: *pr = p;
411: else {
412: for ( dcr0 = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
413: evalp(vl,COEF(dc),prec,&t);
414: if ( t ) {
415: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
416: }
417: }
418: if ( !dcr0 ) {
419: *pr = 0; return;
420: } else {
421: NEXT(dcr) = 0; MKP(VR(p),dcr0,t);
422: }
423: if ( NUM(t) || (VR(t) != VR(p)) || ((vid)VR(p)->attr != V_PF) ) {
424: *pr = t; return;
425: } else {
426: evalv(vl,VR(p),prec,&u); substr(vl,1,(Obj)t,VR(p),u,(Obj *)pr);
427: }
428: }
429: }
430:
1.4 noro 431: void evalv(VL vl,V v,int prec,Obj *rp)
1.1 noro 432: {
433: PFINS ins,tins;
434: PFAD ad,tad;
435: PF pf;
436: P t;
437: int i;
438:
439: if ( (vid)v->attr != V_PF ) {
440: MKV(v,t); *rp = (Obj)t;
441: } else {
442: ins = (PFINS)v->priv; ad = ins->ad; pf = ins->pf;
443: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
444: tins->pf = pf;
445: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
446: tad[i].d = ad[i].d; evalr(vl,ad[i].arg,prec,&tad[i].arg);
447: }
448: evalins(tins,prec,rp);
449: }
450: }
451:
1.4 noro 452: void evalins(PFINS ins,int prec,Obj *rp)
1.1 noro 453: {
454: PF pf;
455: PFAD ad;
456: int i;
457: Q q;
458: V v;
459: P x;
460: NODE n0,n;
461:
462: pf = ins->pf; ad = ins->ad;
463: for ( i = 0; i < pf->argc; i++ )
464: if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) )
465: break;
466: if ( (i != pf->argc) || !pf->pari ) {
467: instov(ins,&v); MKV(v,x); *rp = (Obj)x;
468: } else {
469: for ( n0 = 0, i = 0; i < pf->argc; i++ ) {
470: NEXTNODE(n0,n); BDY(n) = (pointer)ad[i].arg;
471: }
472: if ( prec ) {
473: NEXTNODE(n0,n); STOQ(prec,q); BDY(n) = (pointer)q;
474: }
475: if ( n0 )
476: NEXT(n) = 0;
477: (*pf->pari)(n0,rp);
478: }
479: }
480:
1.4 noro 481: void devalr(VL vl,Obj a,Obj *c)
1.1 noro 482: {
483: Obj nm,dn;
484: double d;
485: Real r;
486:
487: if ( !a )
488: *c = 0;
489: else {
490: switch ( OID(a) ) {
491: case O_N:
492: d = ToReal(a);
493: MKReal(d,r);
494: *c = (Obj)r;
495: break;
496: case O_P:
497: devalp(vl,(P)a,(P *)c); break;
498: case O_R:
499: devalp(vl,NM((R)a),(P *)&nm);
500: devalp(vl,DN((R)a),(P *)&dn);
501: divr(vl,nm,dn,c);
502: break;
503: default:
504: error("devalr : not implemented"); break;
505: }
506: }
507: }
508:
1.4 noro 509: void devalp(VL vl,P p,P *pr)
1.1 noro 510: {
511: P t;
512: DCP dc,dcr0,dcr;
513: Obj u,s;
514: double d;
515: Real r;
516:
517: if ( !p || NUM(p) ) {
518: d = ToReal(p);
519: MKReal(d,r);
520: *pr = (P)r;
521: } else {
522: for ( dcr0 = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
523: devalp(vl,COEF(dc),&t);
524: if ( t ) {
525: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
526: }
527: }
528: if ( !dcr0 )
529: *pr = 0;
530: else {
531: NEXT(dcr) = 0; MKP(VR(p),dcr0,t);
532: if ( NUM(t) ) {
533: d = ToReal((Num)t);
534: MKReal(d,r);
535: *pr = (P)r;
536: } else if ( (VR(t) != VR(p)) || (VR(p)->attr != (pointer)V_PF) )
537: *pr = t;
538: else {
539: devalv(vl,VR(p),&u);
540: substr(vl,1,(Obj)t,VR(p),u,&s);
541: if ( s && NUM(s) ) {
542: d = ToReal((Num)s);
543: MKReal(d,r);
544: *pr = (P)r;
545: } else
546: *pr = (P)s;
547: }
548: }
549: }
550: }
551:
1.4 noro 552: void devalv(VL vl,V v,Obj *rp)
1.1 noro 553: {
554: PFINS ins,tins;
555: PFAD ad,tad;
556: PF pf;
557: P t;
558: int i;
559:
560: if ( (vid)v->attr != V_PF ) {
561: MKV(v,t); *rp = (Obj)t;
562: } else {
563: ins = (PFINS)v->priv; ad = ins->ad; pf = ins->pf;
564: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
565: tins->pf = pf;
566: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
567: tad[i].d = ad[i].d; devalr(vl,ad[i].arg,&tad[i].arg);
568: }
569: devalins(tins,rp);
570: }
571: }
572:
1.4 noro 573: void devalins(PFINS ins,Obj *rp)
1.1 noro 574: {
575: PF pf;
576: PFAD ad;
577: int i;
578: Real r;
579: double d;
580: V v;
581: P x;
582:
583: pf = ins->pf; ad = ins->ad;
584: for ( i = 0; i < pf->argc; i++ )
585: if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) )
586: break;
587: if ( (i != pf->argc) || !pf->libm ) {
588: instov(ins,&v); MKV(v,x); *rp = (Obj)x;
589: } else {
590: switch ( pf->argc ) {
591: case 0:
592: d = (*pf->libm)(); break;
593: case 1:
594: d = (*pf->libm)(ToReal(ad[0].arg)); break;
595: case 2:
596: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg)); break;
597: case 3:
598: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg),
599: ToReal(ad[2].arg)); break;
600: case 4:
601: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg),
602: ToReal(ad[2].arg),ToReal(ad[3].arg)); break;
603: default:
604: error("devalv : not supported");
605: }
606: MKReal(d,r); *rp = (Obj)r;
607: }
608: }
609:
1.4 noro 610: void simplify_ins(PFINS ins,Obj *rp)
1.1 noro 611: {
612: V v;
613: P t;
614:
615: if ( ins->pf->simplify )
616: (*ins->pf->simplify)(ins,rp);
617: else {
618: instov(ins,&v); MKV(v,t); *rp = (Obj)t;
619: }
620: }
621:
1.4 noro 622: void instov(PFINS ins,V *vp)
1.1 noro 623: {
624: V v;
625:
626: NEWV(v); NAME(v) = 0;
627: v->attr = (pointer)V_PF; v->priv = (pointer)ins;
628: appendpfins(v,vp);
629: }
630:
1.4 noro 631: void substfr(VL vl,Obj a,PF u,PF f,Obj *c)
1.1 noro 632: {
633: Obj nm,dn;
634:
635: if ( !a )
636: *c = 0;
637: else {
638: switch ( OID(a) ) {
639: case O_N:
640: *c = a; break;
641: case O_P:
642: substfp(vl,a,u,f,c); break;
643: case O_R:
644: substfp(vl,(Obj)NM((R)a),u,f,&nm); substfp(vl,(Obj)DN((R)a),u,f,&dn);
645: divr(vl,nm,dn,c);
646: break;
647: default:
648: error("substfr : not implemented"); break;
649: }
650: }
651: }
652:
1.4 noro 653: void substfp(VL vl,Obj p,PF u,PF f,Obj *pr)
1.1 noro 654: {
655: V v;
656: DCP dc;
657: Obj a,c,m,s,t,p0;
658: Q d;
659: P x;
660:
661: if ( !p )
662: *pr = 0;
663: else if ( NUM(p) )
664: *pr = (Obj)p;
665: else {
666: v = VR((P)p); dc = DC((P)p);
667: if ( (int)v->attr != V_PF ) {
668: MKV(VR((P)p),x);
669: for ( c = 0; dc; dc = NEXT(dc) ) {
670: substfp(vl,(Obj)COEF(dc),u,f,&t);
671: if ( DEG(dc) ) {
1.7 ! noro 672: gen_pwrr(vl,(Obj)x,(Obj)DEG(dc),&s);
! 673: mulr(vl,s,t,&m);
1.1 noro 674: addr(vl,m,c,&a); c = a;
675: } else {
676: addr(vl,t,c,&a); c = a;
677: }
678: }
679: } else {
680: substfv(vl,v,u,f,&p0);
681: substfp(vl,(Obj)COEF(dc),u,f,&c);
682: for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) {
1.7 ! noro 683: subq(d,DEG(dc),(Q *)&t);
! 684: gen_pwrr(vl,p0,t,&s); mulr(vl,s,c,&m);
1.1 noro 685: substfp(vl,(Obj)COEF(dc),u,f,&t); addr(vl,m,t,&c);
686: }
687: if ( d ) {
1.7 ! noro 688: gen_pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m);
1.1 noro 689: c = m;
690: }
691: }
692: *pr = c;
693: }
694: }
695:
1.4 noro 696: void substfv(VL vl,V v,PF u,PF f,Obj *c)
1.1 noro 697: {
698: P t;
699: Obj r,s,w;
700: int i,j;
701: PFINS ins,tins;
702: PFAD ad,tad;
703:
704: ins = (PFINS)v->priv; ad = ins->ad;
705: if ( ins->pf == u ) {
706: if ( u->argc != f->argc )
707: error("substfv : argument mismatch");
708: if ( !f->body ) {
709: mkpfins(f,f->args,&v); MKV(v,t); r = (Obj)t;
710: } else
711: r = f->body;
712: for ( i = 0; i < f->argc; i++ )
713: for ( j = 0; j < ad[i].d; j++ ) {
714: derivr(vl,r,f->args[i],&s); r = s;
715: }
716: for ( i = 0; i < f->argc; i++ ) {
717: substfr(vl,ad[i].arg,u,f,&w);
718: substr(vl,0,r,f->args[i],w,&s); r = s;
719: }
720: *c = r;
721: } else {
722: tins = (PFINS)MALLOC(sizeof(PF)+f->argc*sizeof(struct oPFAD));
723: tins->pf = ins->pf; tad = tins->ad;
724: for ( i = 0; i < f->argc; i++ ) {
725: tad[i].d = ad[i].d; substfr(vl,ad[i].arg,u,f,&tad[i].arg);
726: }
727: instov(tins,&v); MKV(v,t); *c = (Obj)t;
728: }
729: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>