version 1.6, 2004/03/11 09:52:56 |
version 1.14, 2018/03/28 05:27:22 |
|
|
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* |
* |
* $OpenXM: OpenXM_contrib2/asir2000/parse/puref.c,v 1.5 2003/05/20 06:15:01 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/parse/puref.c,v 1.13 2018/03/27 06:29:19 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
|
|
|
void instoobj(PFINS ins,Obj *rp); |
|
|
NODE pflist; |
NODE pflist; |
|
|
void searchpf(char *name,FUNC *fp) |
void searchpf(char *name,FUNC *fp) |
Line 284 void derivr(VL vl,Obj a,V v,Obj *b) |
|
Line 286 void derivr(VL vl,Obj a,V v,Obj *b) |
|
} |
} |
} |
} |
|
|
|
void simple_derivr(VL vl,Obj a,V v,Obj *b) |
|
{ |
|
Obj r,s,t,u,nm,dn; |
|
|
|
if ( !a || NUM(a) ) |
|
*b = 0; |
|
else |
|
switch ( OID(a) ) { |
|
case O_P: |
|
pderivr(vl,a,v,b); break; |
|
case O_R: |
|
nm = (Obj)NM((R)a); dn = (Obj)DN((R)a); |
|
/* (nm/dn)' = nm'/dn - dn'/dn*nm/dn */ |
|
pderivr(vl,nm,v,&s); divr(vl,s,dn,&u); reductr(vl,u,&t); |
|
pderivr(vl,dn,v,&s); divr(vl,s,dn,&u); reductr(vl,u,&s); mulr(vl,s,a,&u); |
|
subr(vl,t,u,&s); reductr(vl,s,b); |
|
break; |
|
default: |
|
error("simple_derivr : invalid argument"); |
|
} |
|
} |
|
|
|
int obj_is_dependent(Obj a,V v) |
|
{ |
|
if ( !a || OID(a) <= O_N ) return 0; |
|
else if ( OID(a) == O_P ) return poly_is_dependent((P)a,v); |
|
else if ( OID(a) == O_R ) return poly_is_dependent(NM((R)a),v) |
|
|| poly_is_dependent(DN((R)a),v); |
|
else |
|
error("obj_is_dependent : not implemented"); |
|
} |
|
|
|
int poly_is_dependent(P p,V v) |
|
{ |
|
DCP dc; |
|
|
|
if ( !p || OID(p) <= O_N ) return 0; |
|
else if ( v == VR(p) ) return 1; |
|
else { |
|
for ( dc = DC(p); dc; dc = NEXT(dc) ) |
|
if ( poly_is_dependent(COEF(dc),v) ) return 1; |
|
return 0; |
|
} |
|
} |
|
|
|
void gen_pwrr(VL vl,Obj a,Obj d,Obj *r) |
|
{ |
|
if ( INT(d) ) |
|
pwrr(vl,a,d,r); |
|
else |
|
mkpow(vl,a,d,r); |
|
} |
|
|
void substr(VL vl,int partial,Obj a,V v,Obj b,Obj *c) |
void substr(VL vl,int partial,Obj a,V v,Obj b,Obj *c) |
{ |
{ |
Obj nm,dn,t; |
Obj nm,dn,t; |
Line 338 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
Line 393 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
for ( c = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) { |
for ( c = 0, dc = DC((P)p); dc; dc = NEXT(dc) ) { |
substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t); |
substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t); |
if ( DEG(dc) ) { |
if ( DEG(dc) ) { |
pwrp(vl,x,DEG(dc),(P *)&s); mulr(vl,s,t,&m); |
gen_pwrr(vl,(Obj)x,(Obj)DEG(dc),&s); |
|
mulr(vl,s,t,&m); |
addr(vl,m,c,&a); c = a; |
addr(vl,m,c,&a); c = a; |
} else { |
} else { |
addr(vl,t,c,&a); c = a; |
addr(vl,t,c,&a); c = a; |
Line 352 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
Line 408 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
else |
else |
c = (Obj)COEF(dc); |
c = (Obj)COEF(dc); |
for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) { |
for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) { |
subq(d,DEG(dc),(Q *)&t); pwrr(vl,p0,t,&s); mulr(vl,s,c,&m); |
subq(d,DEG(dc),(Q *)&t); |
|
gen_pwrr(vl,p0,t,&s); mulr(vl,s,c,&m); |
if ( !partial ) |
if ( !partial ) |
substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t); |
substpr(vl,partial,(Obj)COEF(dc),v0,p0,&t); |
else |
else |
Line 360 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
Line 417 void substpr(VL vl,int partial,Obj p,V v0,Obj p0,Obj * |
|
addr(vl,m,t,&c); |
addr(vl,m,t,&c); |
} |
} |
if ( d ) { |
if ( d ) { |
pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m); |
gen_pwrr(vl,p0,(Obj)d,&t); |
|
mulr(vl,t,c,&m); |
c = m; |
c = m; |
} |
} |
*pr = c; |
*pr = c; |
Line 441 void evalv(VL vl,V v,int prec,Obj *rp) |
|
Line 499 void evalv(VL vl,V v,int prec,Obj *rp) |
|
void evalins(PFINS ins,int prec,Obj *rp) |
void evalins(PFINS ins,int prec,Obj *rp) |
{ |
{ |
PF pf; |
PF pf; |
PFAD ad; |
PFINS tins; |
|
PFAD ad,tad; |
int i; |
int i; |
Q q; |
Q q; |
V v; |
V v; |
Line 449 void evalins(PFINS ins,int prec,Obj *rp) |
|
Line 508 void evalins(PFINS ins,int prec,Obj *rp) |
|
NODE n0,n; |
NODE n0,n; |
|
|
pf = ins->pf; ad = ins->ad; |
pf = ins->pf; ad = ins->ad; |
|
tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD)); |
|
tins->pf = pf; tad = tins->ad; |
|
for ( i = 0; i < pf->argc; i++ ) { |
|
tad[i].d = ad[i].d; evalr(CO,ad[i].arg,prec,&tad[i].arg); |
|
} |
for ( i = 0; i < pf->argc; i++ ) |
for ( i = 0; i < pf->argc; i++ ) |
if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) ) |
if ( tad[i].d || (tad[i].arg && !NUM(tad[i].arg)) ) break; |
break; |
|
if ( (i != pf->argc) || !pf->pari ) { |
if ( (i != pf->argc) || !pf->pari ) { |
instov(ins,&v); MKV(v,x); *rp = (Obj)x; |
instoobj(tins,rp); |
} else { |
} else { |
for ( n0 = 0, i = 0; i < pf->argc; i++ ) { |
for ( n0 = 0, i = 0; i < pf->argc; i++ ) { |
NEXTNODE(n0,n); BDY(n) = (pointer)ad[i].arg; |
NEXTNODE(n0,n); BDY(n) = (pointer)tad[i].arg; |
} |
} |
if ( prec ) { |
if ( prec ) { |
NEXTNODE(n0,n); STOQ(prec,q); BDY(n) = (pointer)q; |
NEXTNODE(n0,n); STOQ(prec,q); BDY(n) = (pointer)q; |
Line 471 void devalr(VL vl,Obj a,Obj *c) |
|
Line 534 void devalr(VL vl,Obj a,Obj *c) |
|
{ |
{ |
Obj nm,dn; |
Obj nm,dn; |
double d; |
double d; |
Real r; |
Real r,re,im; |
|
C z; |
|
int nid; |
|
|
if ( !a ) |
if ( !a ) |
*c = 0; |
*c = 0; |
else { |
else { |
switch ( OID(a) ) { |
switch ( OID(a) ) { |
case O_N: |
case O_N: |
d = ToReal(a); |
nid = NID((Num)a); |
MKReal(d,r); |
if ( nid == N_C ) { |
*c = (Obj)r; |
d = ToReal(((C)a)->r); MKReal(d,re); |
|
d = ToReal(((C)a)->i); MKReal(d,im); |
|
reimtocplx(re,im,&z); |
|
*c = (Obj)z; |
|
} else if ( nid == N_Q || nid == N_R || nid == N_B ) { |
|
d = ToReal(a); |
|
MKReal(d,r); |
|
*c = (Obj)r; |
|
} else |
|
error("devalr : unsupported"); |
break; |
break; |
case O_P: |
case O_P: |
devalp(vl,(P)a,(P *)c); break; |
devalp(vl,(P)a,(P *)c); break; |
Line 561 void devalv(VL vl,V v,Obj *rp) |
|
Line 635 void devalv(VL vl,V v,Obj *rp) |
|
|
|
void devalins(PFINS ins,Obj *rp) |
void devalins(PFINS ins,Obj *rp) |
{ |
{ |
|
PFINS tins; |
PF pf; |
PF pf; |
PFAD ad; |
PFAD ad,tad; |
int i; |
int i; |
Real r; |
Real r; |
double d; |
double d; |
Line 570 void devalins(PFINS ins,Obj *rp) |
|
Line 645 void devalins(PFINS ins,Obj *rp) |
|
P x; |
P x; |
|
|
pf = ins->pf; ad = ins->ad; |
pf = ins->pf; ad = ins->ad; |
|
tins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD)); |
|
tins->pf = pf; tad = tins->ad; |
|
for ( i = 0; i < pf->argc; i++ ) { |
|
tad[i].d = ad[i].d; devalr(CO,ad[i].arg,&tad[i].arg); |
|
} |
for ( i = 0; i < pf->argc; i++ ) |
for ( i = 0; i < pf->argc; i++ ) |
if ( ad[i].d || (ad[i].arg && !NUM(ad[i].arg)) ) |
if ( tad[i].d || (tad[i].arg && !NUM(tad[i].arg)) ) break; |
break; |
|
if ( (i != pf->argc) || !pf->libm ) { |
if ( (i != pf->argc) || !pf->libm ) { |
instov(ins,&v); MKV(v,x); *rp = (Obj)x; |
instoobj(tins,rp); |
} else { |
} else { |
|
for ( i = 0; i < pf->argc; i++ ) |
|
if ( tad[i].arg && NID((Num)tad[i].arg) == N_C ) |
|
error("devalins : not supported"); |
switch ( pf->argc ) { |
switch ( pf->argc ) { |
case 0: |
case 0: |
d = (*pf->libm)(); break; |
d = (*pf->libm)(); break; |
case 1: |
case 1: |
d = (*pf->libm)(ToReal(ad[0].arg)); break; |
d = (*pf->libm)(ToReal(tad[0].arg)); break; |
case 2: |
case 2: |
d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg)); break; |
d = (*pf->libm)(ToReal(tad[0].arg),ToReal(tad[1].arg)); break; |
case 3: |
case 3: |
d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg), |
d = (*pf->libm)(ToReal(tad[0].arg),ToReal(tad[1].arg), |
ToReal(ad[2].arg)); break; |
ToReal(tad[2].arg)); break; |
case 4: |
case 4: |
d = (*pf->libm)(ToReal(ad[0].arg),ToReal(ad[1].arg), |
d = (*pf->libm)(ToReal(tad[0].arg),ToReal(tad[1].arg), |
ToReal(ad[2].arg),ToReal(ad[3].arg)); break; |
ToReal(tad[2].arg),ToReal(tad[3].arg)); break; |
default: |
default: |
error("devalv : not supported"); |
error("devalins : not supported"); |
} |
} |
MKReal(d,r); *rp = (Obj)r; |
MKReal(d,r); *rp = (Obj)r; |
} |
} |
} |
} |
|
|
|
extern int evalef,bigfloat; |
|
|
|
void simplify_elemfunc_ins(PFINS ins,Obj *rp) |
|
{ |
|
if ( evalef ) { |
|
if ( bigfloat ) evalins(ins,0,rp); |
|
else devalins(ins,rp); |
|
} else instoobj(ins,rp); |
|
} |
|
|
|
void simplify_factorial_ins(PFINS ins,Obj *rp) |
|
{ |
|
PFAD ad; |
|
Obj a; |
|
Q q; |
|
|
|
ad = ins->ad; |
|
a = ad[0].arg; |
|
if ( !ad[0].d && INT(a) && ( !a || (PL(NM((Q)a)) == 1 && SGN((Q)a) > 0) ) ) { |
|
factorial(QTOS((Q)a),&q); |
|
*rp = (Obj)q; |
|
} else simplify_elemfunc_ins(ins,rp); |
|
} |
|
|
|
void simplify_abs_ins(PFINS ins,Obj *rp) |
|
{ |
|
PFAD ad; |
|
Obj a; |
|
Q q; |
|
double t; |
|
Real r; |
|
struct oNODE arg0; |
|
|
|
ad = ins->ad; |
|
a = ad[0].arg; |
|
if ( !ad[0].d && NUM(a) && (!a || RATN(a)) ) { |
|
if ( !a || SGN((Q)a) > 0 ) *rp = (Obj)a; |
|
else { |
|
chsgnq((Q)a,&q); *rp = (Obj)q; |
|
} |
|
} else if ( !ad[0].d && REAL(a) ) { |
|
t = fabs(((Real)a)->body); |
|
MKReal(t,r); *rp = (Obj)r; |
|
} else if ( !ad[0].d && BIGFLOAT(a) ) { |
|
arg0.body = (pointer)a; arg0.next = 0; |
|
mp_abs(&arg0,rp); |
|
} else simplify_elemfunc_ins(ins,rp); |
|
} |
|
|
void simplify_ins(PFINS ins,Obj *rp) |
void simplify_ins(PFINS ins,Obj *rp) |
{ |
{ |
V v; |
V v; |
Line 604 void simplify_ins(PFINS ins,Obj *rp) |
|
Line 735 void simplify_ins(PFINS ins,Obj *rp) |
|
if ( ins->pf->simplify ) |
if ( ins->pf->simplify ) |
(*ins->pf->simplify)(ins,rp); |
(*ins->pf->simplify)(ins,rp); |
else { |
else { |
instov(ins,&v); MKV(v,t); *rp = (Obj)t; |
instoobj(ins,rp); |
} |
} |
} |
} |
|
|
void instov(PFINS ins,V *vp) |
void instoobj(PFINS ins,Obj *rp) |
{ |
{ |
V v; |
V v,newv; |
|
P t; |
|
|
NEWV(v); NAME(v) = 0; |
NEWV(v); NAME(v) = 0; |
v->attr = (pointer)V_PF; v->priv = (pointer)ins; |
v->attr = (pointer)V_PF; v->priv = (pointer)ins; |
appendpfins(v,vp); |
appendpfins(v,&newv); |
|
MKV(newv,t); |
|
*rp = (Obj)t; |
} |
} |
|
|
void substfr(VL vl,Obj a,PF u,PF f,Obj *c) |
void substfr(VL vl,Obj a,PF u,PF f,Obj *c) |
Line 658 void substfp(VL vl,Obj p,PF u,PF f,Obj *pr) |
|
Line 792 void substfp(VL vl,Obj p,PF u,PF f,Obj *pr) |
|
for ( c = 0; dc; dc = NEXT(dc) ) { |
for ( c = 0; dc; dc = NEXT(dc) ) { |
substfp(vl,(Obj)COEF(dc),u,f,&t); |
substfp(vl,(Obj)COEF(dc),u,f,&t); |
if ( DEG(dc) ) { |
if ( DEG(dc) ) { |
pwrp(vl,x,DEG(dc),(P *)&s); mulr(vl,s,t,&m); |
gen_pwrr(vl,(Obj)x,(Obj)DEG(dc),&s); |
|
mulr(vl,s,t,&m); |
addr(vl,m,c,&a); c = a; |
addr(vl,m,c,&a); c = a; |
} else { |
} else { |
addr(vl,t,c,&a); c = a; |
addr(vl,t,c,&a); c = a; |
Line 668 void substfp(VL vl,Obj p,PF u,PF f,Obj *pr) |
|
Line 803 void substfp(VL vl,Obj p,PF u,PF f,Obj *pr) |
|
substfv(vl,v,u,f,&p0); |
substfv(vl,v,u,f,&p0); |
substfp(vl,(Obj)COEF(dc),u,f,&c); |
substfp(vl,(Obj)COEF(dc),u,f,&c); |
for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) { |
for ( d = DEG(dc), dc = NEXT(dc); dc; d = DEG(dc), dc = NEXT(dc) ) { |
subq(d,DEG(dc),(Q *)&t); pwrr(vl,p0,t,&s); mulr(vl,s,c,&m); |
subq(d,DEG(dc),(Q *)&t); |
|
gen_pwrr(vl,p0,t,&s); mulr(vl,s,c,&m); |
substfp(vl,(Obj)COEF(dc),u,f,&t); addr(vl,m,t,&c); |
substfp(vl,(Obj)COEF(dc),u,f,&t); addr(vl,m,t,&c); |
} |
} |
if ( d ) { |
if ( d ) { |
pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m); |
gen_pwrr(vl,p0,(Obj)d,&t); mulr(vl,t,c,&m); |
c = m; |
c = m; |
} |
} |
} |
} |
Line 711 void substfv(VL vl,V v,PF u,PF f,Obj *c) |
|
Line 847 void substfv(VL vl,V v,PF u,PF f,Obj *c) |
|
for ( i = 0; i < f->argc; i++ ) { |
for ( i = 0; i < f->argc; i++ ) { |
tad[i].d = ad[i].d; substfr(vl,ad[i].arg,u,f,&tad[i].arg); |
tad[i].d = ad[i].d; substfr(vl,ad[i].arg,u,f,&tad[i].arg); |
} |
} |
instov(tins,&v); MKV(v,t); *c = (Obj)t; |
instoobj(tins,c); |
} |
} |
} |
} |