Annotation of OpenXM_contrib2/asir2000/parse/puref.c, Revision 1.6
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.6 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/puref.c,v 1.5 2003/05/20 06:15:01 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.4 noro 287: void substr(VL vl,int partial,Obj a,V v,Obj b,Obj *c)
1.1 noro 288: {
289: Obj nm,dn,t;
290:
291: if ( !a )
292: *c = 0;
293: else {
294: switch ( OID(a) ) {
295: case O_N:
296: *c = a; break;
297: case O_P:
298: substpr(vl,partial,a,v,b,c); break;
299: case O_R:
300: substpr(vl,partial,(Obj)NM((R)a),v,b,&nm);
301: substpr(vl,partial,(Obj)DN((R)a),v,b,&dn);
302: divr(vl,nm,dn,&t); reductr(vl,t,c);
303: break;
304: default:
305: *c = 0; break;
306: }
307: }
308: }
309:
1.4 noro 310: void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj *pr)
1.1 noro 311: {
312: P x;
313: Obj t,m,c,s,a;
314: DCP dc;
315: Q d;
316: V v;
317: PF pf;
318: PFAD ad,tad;
319: PFINS tins;
320: int i;
321:
322: if ( !p )
323: *pr = 0;
324: else if ( NUM(p) )
325: *pr = (Obj)p;
326: else if ( (v = VR((P)p)) != v0 ) {
327: if ( !partial && ((vid)v->attr == V_PF) ) {
328: ad = ((PFINS)v->priv)->ad; pf = ((PFINS)v->priv)->pf;
329: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
330: tins->pf = pf;
331: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
332: tad[i].d = ad[i].d;
333: substr(vl,partial,ad[i].arg,v0,p0,&tad[i].arg);
334: }
335: simplify_ins(tins,(Obj *)&x);
336: } else
337: MKV(VR((P)p),x);
338: for ( c = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
339: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t);
340: if ( DEG(dc) ) {
341: pwrp(vl,x,DEG(dc),(P *)&s); mulr(vl,s,t,&m);
342: addr(vl,m,c,&a); c = a;
343: } else {
344: addr(vl,t,c,&a); c = a;
345: }
346: }
347: *pr = c;
348: } else {
349: dc = DC((P)p);
350: if ( !partial )
351: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&c);
352: else
353: c = (Obj)COEF(dc);
354: for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) {
355: subq(d,DEG(dc),(Q *)&t); pwrr(vl,p0,t,&s); mulr(vl,s,c,&m);
356: if ( !partial )
357: substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t);
358: else
359: t = (Obj)COEF(dc);
360: addr(vl,m,t,&c);
361: }
362: if ( d ) {
363: pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m);
364: c = m;
365: }
366: *pr = c;
367: }
368: }
369:
1.4 noro 370: void evalr(VL vl,Obj a,int prec,Obj *c)
1.1 noro 371: {
372: Obj nm,dn;
373:
374: if ( !a )
375: *c = 0;
376: else {
377: switch ( OID(a) ) {
378: case O_N:
379: *c = a; break;
380: case O_P:
381: evalp(vl,(P)a,prec,(P *)c); break;
382: case O_R:
383: evalp(vl,NM((R)a),prec,(P *)&nm); evalp(vl,DN((R)a),prec,(P *)&dn);
384: divr(vl,nm,dn,c);
385: break;
386: default:
387: error("evalr : not implemented"); break;
388: }
389: }
390: }
391:
1.4 noro 392: void evalp(VL vl,P p,int prec,P *pr)
1.1 noro 393: {
394: P t;
395: DCP dc,dcr0,dcr;
396: Obj u;
397:
398: if ( !p || NUM(p) )
399: *pr = p;
400: else {
401: for ( dcr0 = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
402: evalp(vl,COEF(dc),prec,&t);
403: if ( t ) {
404: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
405: }
406: }
407: if ( !dcr0 ) {
408: *pr = 0; return;
409: } else {
410: NEXT(dcr) = 0; MKP(VR(p),dcr0,t);
411: }
412: if ( NUM(t) || (VR(t) != VR(p)) || ((vid)VR(p)->attr != V_PF) ) {
413: *pr = t; return;
414: } else {
415: evalv(vl,VR(p),prec,&u); substr(vl,1,(Obj)t,VR(p),u,(Obj *)pr);
416: }
417: }
418: }
419:
1.4 noro 420: void evalv(VL vl,V v,int prec,Obj *rp)
1.1 noro 421: {
422: PFINS ins,tins;
423: PFAD ad,tad;
424: PF pf;
425: P t;
426: int i;
427:
428: if ( (vid)v->attr != V_PF ) {
429: MKV(v,t); *rp = (Obj)t;
430: } else {
431: ins = (PFINS)v->priv; ad = ins->ad; pf = ins->pf;
432: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
433: tins->pf = pf;
434: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
435: tad[i].d = ad[i].d; evalr(vl,ad[i].arg,prec,&tad[i].arg);
436: }
437: evalins(tins,prec,rp);
438: }
439: }
440:
1.4 noro 441: void evalins(PFINS ins,int prec,Obj *rp)
1.1 noro 442: {
443: PF pf;
444: PFAD ad;
445: int i;
446: Q q;
447: V v;
448: P x;
449: NODE n0,n;
450:
451: pf = ins->pf; ad = ins->ad;
452: for ( i = 0; i < pf->argc; i++ )
453: if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) )
454: break;
455: if ( (i != pf->argc) || !pf->pari ) {
456: instov(ins,&v); MKV(v,x); *rp = (Obj)x;
457: } else {
458: for ( n0 = 0, i = 0; i < pf->argc; i++ ) {
459: NEXTNODE(n0,n); BDY(n) = (pointer)ad[i].arg;
460: }
461: if ( prec ) {
462: NEXTNODE(n0,n); STOQ(prec,q); BDY(n) = (pointer)q;
463: }
464: if ( n0 )
465: NEXT(n) = 0;
466: (*pf->pari)(n0,rp);
467: }
468: }
469:
1.4 noro 470: void devalr(VL vl,Obj a,Obj *c)
1.1 noro 471: {
472: Obj nm,dn;
473: double d;
474: Real r;
475:
476: if ( !a )
477: *c = 0;
478: else {
479: switch ( OID(a) ) {
480: case O_N:
481: d = ToReal(a);
482: MKReal(d,r);
483: *c = (Obj)r;
484: break;
485: case O_P:
486: devalp(vl,(P)a,(P *)c); break;
487: case O_R:
488: devalp(vl,NM((R)a),(P *)&nm);
489: devalp(vl,DN((R)a),(P *)&dn);
490: divr(vl,nm,dn,c);
491: break;
492: default:
493: error("devalr : not implemented"); break;
494: }
495: }
496: }
497:
1.4 noro 498: void devalp(VL vl,P p,P *pr)
1.1 noro 499: {
500: P t;
501: DCP dc,dcr0,dcr;
502: Obj u,s;
503: double d;
504: Real r;
505:
506: if ( !p || NUM(p) ) {
507: d = ToReal(p);
508: MKReal(d,r);
509: *pr = (P)r;
510: } else {
511: for ( dcr0 = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) {
512: devalp(vl,COEF(dc),&t);
513: if ( t ) {
514: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
515: }
516: }
517: if ( !dcr0 )
518: *pr = 0;
519: else {
520: NEXT(dcr) = 0; MKP(VR(p),dcr0,t);
521: if ( NUM(t) ) {
522: d = ToReal((Num)t);
523: MKReal(d,r);
524: *pr = (P)r;
525: } else if ( (VR(t) != VR(p)) || (VR(p)->attr != (pointer)V_PF) )
526: *pr = t;
527: else {
528: devalv(vl,VR(p),&u);
529: substr(vl,1,(Obj)t,VR(p),u,&s);
530: if ( s && NUM(s) ) {
531: d = ToReal((Num)s);
532: MKReal(d,r);
533: *pr = (P)r;
534: } else
535: *pr = (P)s;
536: }
537: }
538: }
539: }
540:
1.4 noro 541: void devalv(VL vl,V v,Obj *rp)
1.1 noro 542: {
543: PFINS ins,tins;
544: PFAD ad,tad;
545: PF pf;
546: P t;
547: int i;
548:
549: if ( (vid)v->attr != V_PF ) {
550: MKV(v,t); *rp = (Obj)t;
551: } else {
552: ins = (PFINS)v->priv; ad = ins->ad; pf = ins->pf;
553: tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
554: tins->pf = pf;
555: for ( i = 0, tad = tins->ad; i < pf->argc; i++ ) {
556: tad[i].d = ad[i].d; devalr(vl,ad[i].arg,&tad[i].arg);
557: }
558: devalins(tins,rp);
559: }
560: }
561:
1.4 noro 562: void devalins(PFINS ins,Obj *rp)
1.1 noro 563: {
564: PF pf;
565: PFAD ad;
566: int i;
567: Real r;
568: double d;
569: V v;
570: P x;
571:
572: pf = ins->pf; ad = ins->ad;
573: for ( i = 0; i < pf->argc; i++ )
574: if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) )
575: break;
576: if ( (i != pf->argc) || !pf->libm ) {
577: instov(ins,&v); MKV(v,x); *rp = (Obj)x;
578: } else {
579: switch ( pf->argc ) {
580: case 0:
581: d = (*pf->libm)(); break;
582: case 1:
583: d = (*pf->libm)(ToReal(ad[0].arg)); break;
584: case 2:
585: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg)); break;
586: case 3:
587: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg),
588: ToReal(ad[2].arg)); break;
589: case 4:
590: d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg),
591: ToReal(ad[2].arg),ToReal(ad[3].arg)); break;
592: default:
593: error("devalv : not supported");
594: }
595: MKReal(d,r); *rp = (Obj)r;
596: }
597: }
598:
1.4 noro 599: void simplify_ins(PFINS ins,Obj *rp)
1.1 noro 600: {
601: V v;
602: P t;
603:
604: if ( ins->pf->simplify )
605: (*ins->pf->simplify)(ins,rp);
606: else {
607: instov(ins,&v); MKV(v,t); *rp = (Obj)t;
608: }
609: }
610:
1.4 noro 611: void instov(PFINS ins,V *vp)
1.1 noro 612: {
613: V v;
614:
615: NEWV(v); NAME(v) = 0;
616: v->attr = (pointer)V_PF; v->priv = (pointer)ins;
617: appendpfins(v,vp);
618: }
619:
1.4 noro 620: void substfr(VL vl,Obj a,PF u,PF f,Obj *c)
1.1 noro 621: {
622: Obj nm,dn;
623:
624: if ( !a )
625: *c = 0;
626: else {
627: switch ( OID(a) ) {
628: case O_N:
629: *c = a; break;
630: case O_P:
631: substfp(vl,a,u,f,c); break;
632: case O_R:
633: substfp(vl,(Obj)NM((R)a),u,f,&nm); substfp(vl,(Obj)DN((R)a),u,f,&dn);
634: divr(vl,nm,dn,c);
635: break;
636: default:
637: error("substfr : not implemented"); break;
638: }
639: }
640: }
641:
1.4 noro 642: void substfp(VL vl,Obj p,PF u,PF f,Obj *pr)
1.1 noro 643: {
644: V v;
645: DCP dc;
646: Obj a,c,m,s,t,p0;
647: Q d;
648: P x;
649:
650: if ( !p )
651: *pr = 0;
652: else if ( NUM(p) )
653: *pr = (Obj)p;
654: else {
655: v = VR((P)p); dc = DC((P)p);
656: if ( (int)v->attr != V_PF ) {
657: MKV(VR((P)p),x);
658: for ( c = 0; dc; dc = NEXT(dc) ) {
659: substfp(vl,(Obj)COEF(dc),u,f,&t);
660: if ( DEG(dc) ) {
661: pwrp(vl,x,DEG(dc),(P *)&s); mulr(vl,s,t,&m);
662: addr(vl,m,c,&a); c = a;
663: } else {
664: addr(vl,t,c,&a); c = a;
665: }
666: }
667: } else {
668: substfv(vl,v,u,f,&p0);
669: substfp(vl,(Obj)COEF(dc),u,f,&c);
670: for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) {
671: subq(d,DEG(dc),(Q *)&t); pwrr(vl,p0,t,&s); mulr(vl,s,c,&m);
672: substfp(vl,(Obj)COEF(dc),u,f,&t); addr(vl,m,t,&c);
673: }
674: if ( d ) {
675: pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m);
676: c = m;
677: }
678: }
679: *pr = c;
680: }
681: }
682:
1.4 noro 683: void substfv(VL vl,V v,PF u,PF f,Obj *c)
1.1 noro 684: {
685: P t;
686: Obj r,s,w;
687: int i,j;
688: PFINS ins,tins;
689: PFAD ad,tad;
690:
691: ins = (PFINS)v->priv; ad = ins->ad;
692: if ( ins->pf == u ) {
693: if ( u->argc != f->argc )
694: error("substfv : argument mismatch");
695: if ( !f->body ) {
696: mkpfins(f,f->args,&v); MKV(v,t); r = (Obj)t;
697: } else
698: r = f->body;
699: for ( i = 0; i < f->argc; i++ )
700: for ( j = 0; j < ad[i].d; j++ ) {
701: derivr(vl,r,f->args[i],&s); r = s;
702: }
703: for ( i = 0; i < f->argc; i++ ) {
704: substfr(vl,ad[i].arg,u,f,&w);
705: substr(vl,0,r,f->args[i],w,&s); r = s;
706: }
707: *c = r;
708: } else {
709: tins = (PFINS)MALLOC(sizeof(PF)+f->argc*sizeof(struct oPFAD));
710: tins->pf = ins->pf; tad = tins->ad;
711: for ( i = 0; i < f->argc; i++ ) {
712: tad[i].d = ad[i].d; substfr(vl,ad[i].arg,u,f,&tad[i].arg);
713: }
714: instov(tins,&v); MKV(v,t); *c = (Obj)t;
715: }
716: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>