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