version 1.3, 2000/08/22 05:03:59 |
version 1.8, 2004/06/27 03:15:57 |
|
|
* 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/builtin/pf.c,v 1.2 2000/08/21 08:31:20 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/pf.c,v 1.7 2004/06/22 09:17:21 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "math.h" |
#include "math.h" |
Line 65 void simplify_pow(PFINS,Obj *); |
|
Line 65 void simplify_pow(PFINS,Obj *); |
|
|
|
void Pfunctor(),Pargs(),Pfunargs(),Pvtype(),Pcall(),Pdeval(); |
void Pfunctor(),Pargs(),Pfunargs(),Pvtype(),Pcall(),Pdeval(); |
void Pregister_handler(); |
void Pregister_handler(); |
|
void Peval_quote(); |
|
void Pmapat(); |
|
|
struct ftab puref_tab[] = { |
struct ftab puref_tab[] = { |
|
{"mapat",Pmapat,-99999999}, |
{"functor",Pfunctor,1}, |
{"functor",Pfunctor,1}, |
{"args",Pargs,1}, |
{"args",Pargs,1}, |
{"funargs",Pfunargs,1}, |
{"funargs",Pfunargs,1}, |
Line 74 struct ftab puref_tab[] = { |
|
Line 77 struct ftab puref_tab[] = { |
|
{"call",Pcall,2}, |
{"call",Pcall,2}, |
{"vtype",Pvtype,1}, |
{"vtype",Pvtype,1}, |
{"deval",Pdeval,1}, |
{"deval",Pdeval,1}, |
|
{"eval_quote",Peval_quote,1}, |
{0,0,0}, |
{0,0,0}, |
}; |
}; |
|
|
#if PARI |
#if defined(PARI) |
int p_pi(),p_e(); |
int p_pi(),p_e(); |
int p_log(),p_exp(),p_pow(); |
int p_log(),p_exp(),p_pow(); |
int p_sin(),p_cos(),p_tan(),p_asin(),p_acos(),p_atan(); |
int p_sin(),p_cos(),p_tan(),p_asin(),p_acos(),p_atan(); |
|
|
pf = ins->pf; ad = ins->ad; a0 = ad[0].arg; a1 = ad[1].arg; |
pf = ins->pf; ad = ins->ad; a0 = ad[0].arg; a1 = ad[1].arg; |
if ( !a1 ) |
if ( !a1 ) |
*rp = (Obj)ONE; |
*rp = (Obj)ONE; |
else if ( !a0 ) |
else if ( !a0 ) { |
*rp = 0; |
if ( RATN(a1) && SGN((Q)a1)>0 ) |
else if ( NUM(a1) && INT(a1) ) |
*rp = 0; |
|
else if ( RATN(a1) && SGN((Q)a1) < 0 ) |
|
error("simplify_pow : division by 0"); |
|
else { |
|
instov(ins,&v); MKV(v,t); *rp = (Obj)t; |
|
} |
|
} else if ( NUM(a1) && INT(a1) ) |
arf_pwr(CO,a0,a1,rp); |
arf_pwr(CO,a0,a1,rp); |
else { |
else { |
instov(ins,&v); MKV(v,t); *rp = (Obj)t; |
instov(ins,&v); MKV(v,t); *rp = (Obj)t; |
|
|
else { |
else { |
ins = (PFINS)VR(p)->priv; pf = ins->pf; |
ins = (PFINS)VR(p)->priv; pf = ins->pf; |
t = (FUNC)MALLOC(sizeof(struct oFUNC)); |
t = (FUNC)MALLOC(sizeof(struct oFUNC)); |
t->name = pf->name; t->id = A_PURE; t->argc = pf->argc; |
t->name = t->fullname = pf->name; t->id = A_PURE; t->argc = pf->argc; |
t->f.puref = pf; |
t->f.puref = pf; |
makesrvar(t,rp); |
makesrvar(t,rp); |
} |
} |
|
|
else { |
else { |
ins = (PFINS)VR(p)->priv; ad = ins->ad; pf = ins->pf; |
ins = (PFINS)VR(p)->priv; ad = ins->ad; pf = ins->pf; |
t = (FUNC)MALLOC(sizeof(struct oFUNC)); |
t = (FUNC)MALLOC(sizeof(struct oFUNC)); |
t->name = pf->name; t->id = A_PURE; t->argc = pf->argc; |
t->name = t->fullname = pf->name; t->id = A_PURE; t->argc = pf->argc; |
t->f.puref = pf; |
t->f.puref = pf; |
makesrvar(t,&f); |
makesrvar(t,&f); |
n0 = 0; NEXTNODE(n0,n); BDY(n) = (pointer)f; |
n = n0 = 0; NEXTNODE(n0,n); BDY(n) = (pointer)f; |
for ( i = 0; i < pf->argc; i++ ) { |
for ( i = 0; i < pf->argc; i++ ) { |
NEXTNODE(n0,n); BDY(n) = (pointer)ad[i].arg; |
NEXTNODE(n0,n); BDY(n) = (pointer)ad[i].arg; |
} |
} |
|
|
} |
} |
} |
} |
|
|
void Pcall(arg,rp) |
void Pcall(NODE arg,Obj *rp) |
NODE arg; |
|
Obj *rp; |
|
{ |
{ |
P p; |
P p; |
V v; |
V v; |
|
|
*rp = (Obj)bevalf((FUNC)v->priv,BDY((LIST)ARG1(arg))); |
*rp = (Obj)bevalf((FUNC)v->priv,BDY((LIST)ARG1(arg))); |
} |
} |
|
|
|
/* at=position of arg to be used for iteration */ |
|
|
|
void Pmapat(NODE arg,Obj *rp) |
|
{ |
|
LIST args; |
|
NODE node,rest,t0,t,n,r,r0; |
|
P fpoly; |
|
V fvar; |
|
FUNC f; |
|
VECT v,rv; |
|
MAT m,rm; |
|
LIST rl; |
|
int len,row,col,i,j,pos; |
|
Obj iter; |
|
pointer val; |
|
|
|
if ( argc(arg) < 3 ) |
|
error("mapat : too few arguments"); |
|
|
|
fpoly = (P)ARG0(arg); |
|
if ( !fpoly || OID(fpoly) != O_P ) |
|
error("mapat : invalid function specification"); |
|
fvar = VR(fpoly); |
|
if ( fvar->attr != (pointer)V_SR || !(f=(FUNC)fvar->priv) ) |
|
error("mapat : invalid function specification"); |
|
if ( !INT(ARG1(arg)) ) |
|
error("mapat : invalid position"); |
|
pos = QTOS((Q)ARG1(arg)); |
|
node = NEXT(NEXT(arg)); |
|
len = length(node); |
|
if ( pos >= len ) |
|
error("evalmapatf : invalid position"); |
|
r0 = 0; |
|
for ( i = 0, t = node; i < pos; i++, t = NEXT(t) ) { |
|
NEXTNODE(r0,r); |
|
BDY(r) = BDY(t); |
|
} |
|
NEXTNODE(r0,r); |
|
iter = BDY(t); rest = NEXT(t); |
|
if ( !iter ) { |
|
*rp = bevalf(f,node); |
|
return; |
|
} |
|
switch ( OID(iter) ) { |
|
case O_VECT: |
|
v = (VECT)iter; len = v->len; MKVECT(rv,len); |
|
for ( i = 0; i < len; i++ ) { |
|
BDY(r) = BDY(v)[i]; NEXT(r) = rest; |
|
BDY(rv)[i] = bevalf(f,r0); |
|
} |
|
*rp = (Obj)rv; |
|
break; |
|
case O_MAT: |
|
m = (MAT)iter; row = m->row; col = m->col; MKMAT(rm,row,col); |
|
for ( i = 0; i < row; i++ ) |
|
for ( j = 0; j < col; j++ ) { |
|
BDY(r) = BDY(m)[i][j]; NEXT(r) = rest; |
|
BDY(rm)[i][j] = bevalf(f,r0); |
|
} |
|
*rp = (Obj)rm; |
|
break; |
|
case O_LIST: |
|
n = BDY((LIST)iter); |
|
for ( t0 = t = 0; n; n = NEXT(n) ) { |
|
BDY(r) = BDY(n); NEXT(r) = rest; |
|
NEXTNODE(t0,t); BDY(t) = bevalf(f,r0); |
|
} |
|
if ( t0 ) |
|
NEXT(t) = 0; |
|
MKLIST(rl,t0); |
|
*rp = (Obj)rl; |
|
break; |
|
default: |
|
*rp = bevalf(f,node); |
|
break; |
|
} |
|
} |
|
|
void Pdeval(arg,rp) |
void Pdeval(arg,rp) |
NODE arg; |
NODE arg; |
Obj *rp; |
Obj *rp; |
|
|
devalr(CO,(Obj)ARG0(arg),rp); |
devalr(CO,(Obj)ARG0(arg),rp); |
} |
} |
|
|
|
void Peval_quote(arg,rp) |
|
NODE arg; |
|
Obj *rp; |
|
{ |
|
asir_assert(ARG0(arg),O_QUOTE,"eval_quote"); |
|
*rp = eval((FNODE)BDY((QUOTE)ARG0(arg))); |
|
} |