version 1.94, 2005/11/02 10:02:32 |
version 1.96, 2005/11/04 07:03:38 |
|
|
* 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/strobj.c,v 1.93 2005/11/02 09:39:10 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.95 2005/11/03 07:41:22 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
Line 95 void Pflatten_quote(); |
|
Line 95 void Pflatten_quote(); |
|
void Pquote_is_integer(),Pquote_is_rational(),Pquote_is_number(); |
void Pquote_is_integer(),Pquote_is_rational(),Pquote_is_number(); |
void Pquote_is_dependent(),Pquote_is_function(); |
void Pquote_is_dependent(),Pquote_is_function(); |
void Pquote_normalize(); |
void Pquote_normalize(); |
void Pquote_normalize_comp(); |
void Pnquote_comp(); |
void Pquote_normalize_match(); |
void Pnquote_match(); |
|
|
void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name(); |
void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name(); |
void Pquote_match(),Pget_quote_id(),Pquote_match_rewrite(); |
void Pquote_match(),Pget_quote_id(),Pquote_match_rewrite(); |
Line 137 struct ftab str_tab[] = { |
|
Line 137 struct ftab str_tab[] = { |
|
{"quote_is_dependent",Pquote_is_dependent,2}, |
{"quote_is_dependent",Pquote_is_dependent,2}, |
|
|
{"quote_normalize",Pquote_normalize,-2}, |
{"quote_normalize",Pquote_normalize,-2}, |
{"quote_normalize_comp",Pquote_normalize_comp,2}, |
{"quote_match",Pquote_match,2}, |
|
{"quote_match_rewrite",Pquote_match_rewrite,-4}, |
|
{"nquote_comp",Pnquote_comp,2}, |
|
{"nquote_match",Pnquote_match,2}, |
|
|
{"quote_to_nary",Pquote_to_nary,1}, |
{"quote_to_nary",Pquote_to_nary,1}, |
{"quote_to_bin",Pquote_to_bin,2}, |
{"quote_to_bin",Pquote_to_bin,2}, |
|
|
{"quotetotex_tb",Pquotetotex_tb,2}, |
{"quotetotex_tb",Pquotetotex_tb,2}, |
{"quotetotex",Pquotetotex,1}, |
{"quotetotex",Pquotetotex,1}, |
{"quotetotex_env",Pquotetotex_env,-99999999}, |
{"quotetotex_env",Pquotetotex_env,-99999999}, |
{"flatten_quote",Pflatten_quote,-2}, |
{"flatten_quote",Pflatten_quote,-2}, |
{"quote_to_funargs",Pquote_to_funargs,1}, |
{"quote_to_funargs",Pquote_to_funargs,1}, |
{"quote_match",Pquote_match,2}, |
|
{"quote_normalize_match",Pquote_normalize_match,2}, |
|
{"quote_match_rewrite",Pquote_match_rewrite,-4}, |
|
{"funargs_to_quote",Pfunargs_to_quote,1}, |
{"funargs_to_quote",Pfunargs_to_quote,1}, |
{"get_function_name",Pget_function_name,1}, |
{"get_function_name",Pget_function_name,1}, |
{0,0,0}, |
{0,0,0}, |
Line 657 void Pquote_match(NODE arg,Q *rp) |
|
Line 658 void Pquote_match(NODE arg,Q *rp) |
|
*rp = 0; |
*rp = 0; |
} |
} |
|
|
void Pquote_normalize_match(NODE arg,Q *rp) |
void Pnquote_match(NODE arg,Q *rp) |
{ |
{ |
QUOTE fq,pq; |
QUOTE fq,pq; |
FNODE f,p; |
FNODE f,p; |
Line 666 void Pquote_normalize_match(NODE arg,Q *rp) |
|
Line 667 void Pquote_normalize_match(NODE arg,Q *rp) |
|
|
|
fq = (QUOTE)ARG0(arg); Pquote_normalize(mknode(2,fq,0),&fq); f = (FNODE)BDY(fq); |
fq = (QUOTE)ARG0(arg); Pquote_normalize(mknode(2,fq,0),&fq); f = (FNODE)BDY(fq); |
pq = (QUOTE)ARG1(arg); Pquote_normalize(mknode(2,pq,0),&pq); p = (FNODE)BDY(pq); |
pq = (QUOTE)ARG1(arg); Pquote_normalize(mknode(2,pq,0),&pq); p = (FNODE)BDY(pq); |
ret = fnode_normalize_match(f,p,&r); |
ret = nfnode_match(f,p,&r); |
if ( ret ) { |
if ( ret ) { |
fnode_do_assign(r); |
fnode_do_assign(r); |
*rp = ONE; |
*rp = ONE; |
Line 740 void fnode_do_assign(NODE arg) |
|
Line 741 void fnode_do_assign(NODE arg) |
|
int pv; |
int pv; |
FNODE f; |
FNODE f; |
QUOTE value; |
QUOTE value; |
|
QUOTEARG qa; |
|
|
for ( t = arg; t; t = NEXT(t) ) { |
for ( t = arg; t; t = NEXT(t) ) { |
pair = (NODE)BDY(t); |
pair = (NODE)BDY(t); |
pv = (int)BDY(pair); |
pv = (int)BDY(pair); |
f = (FNODE)(BDY(NEXT(pair))); |
f = (FNODE)(BDY(NEXT(pair))); |
MKQUOTE(value,f); |
if ( f->id == I_FUNC_HEAD ) { |
|
/* XXX : I_FUNC_HEAD is a dummy id to pass FUNC */ |
|
MKQUOTEARG(qa,A_func,FA0(f)); |
|
value = (QUOTE)qa; |
|
} else |
|
MKQUOTE(value,f); |
ASSPV(pv,value); |
ASSPV(pv,value); |
} |
} |
} |
} |
Line 2042 void Pquote_normalize(NODE arg,QUOTE *rp) |
|
Line 2049 void Pquote_normalize(NODE arg,QUOTE *rp) |
|
} |
} |
} |
} |
|
|
void Pquote_normalize_comp(NODE arg,Q *rp) |
void Pnquote_comp(NODE arg,Q *rp) |
{ |
{ |
QUOTE q1,q2; |
QUOTE q1,q2; |
FNODE f1,f2; |
FNODE f1,f2; |
Line 2052 void Pquote_normalize_comp(NODE arg,Q *rp) |
|
Line 2059 void Pquote_normalize_comp(NODE arg,Q *rp) |
|
q2 = (QUOTE)ARG1(arg); f2 = (FNODE)BDY(q2); |
q2 = (QUOTE)ARG1(arg); f2 = (FNODE)BDY(q2); |
if ( !q1->normalized ) f1 = fnode_normalize(f1,0); |
if ( !q1->normalized ) f1 = fnode_normalize(f1,0); |
if ( !q2->normalized ) f2 = fnode_normalize(f2,0); |
if ( !q2->normalized ) f2 = fnode_normalize(f2,0); |
r = fnode_normalize_comp(f1,f2); |
r = nfnode_comp(f1,f2); |
STOQ(r,*rp); |
STOQ(r,*rp); |
} |
} |
|
|
Line 2200 int fnode_is_dependent(FNODE f,V v) |
|
Line 2207 int fnode_is_dependent(FNODE f,V v) |
|
} |
} |
} |
} |
|
|
FNODE fnode_normalize_add(FNODE a1,FNODE a2,int expand); |
FNODE nfnode_add(FNODE a1,FNODE a2,int expand); |
FNODE fnode_normalize_mul(FNODE a1,FNODE a2,int expand); |
FNODE nfnode_mul(FNODE a1,FNODE a2,int expand); |
FNODE fnode_normalize_pwr(FNODE a1,FNODE a2,int expand); |
FNODE nfnode_pwr(FNODE a1,FNODE a2,int expand); |
FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expand); |
FNODE nfnode_mul_coef(Num c,FNODE f,int expand); |
FNODE fnode_expand_pwr(FNODE f,int n); |
FNODE fnode_expand_pwr(FNODE f,int n); |
FNODE to_narymul(FNODE f); |
FNODE to_narymul(FNODE f); |
FNODE to_naryadd(FNODE f); |
FNODE to_naryadd(FNODE f); |
Line 2228 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2235 FNODE fnode_normalize(FNODE f,int expand) |
|
break; |
break; |
|
|
case I_MINUS: |
case I_MINUS: |
r = fnode_normalize_mul_coef((Num)q, |
r = nfnode_mul_coef((Num)q, |
fnode_normalize(FA0(f),expand),expand); |
fnode_normalize(FA0(f),expand),expand); |
break; |
break; |
|
|
Line 2238 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2245 FNODE fnode_normalize(FNODE f,int expand) |
|
a2 = fnode_normalize(FA2(f),expand); |
a2 = fnode_normalize(FA2(f),expand); |
switch ( OPNAME(f) ) { |
switch ( OPNAME(f) ) { |
case '+': |
case '+': |
r = fnode_normalize_add(a1,a2,expand); |
r = nfnode_add(a1,a2,expand); |
break; |
break; |
case '-': |
case '-': |
a2 = fnode_normalize_mul_coef((Num)q,a2,expand); |
a2 = nfnode_mul_coef((Num)q,a2,expand); |
r = fnode_normalize_add(a1,a2,expand); |
r = nfnode_add(a1,a2,expand); |
break; |
break; |
case '*': |
case '*': |
r = fnode_normalize_mul(a1,a2,expand); |
r = nfnode_mul(a1,a2,expand); |
break; |
break; |
case '/': |
case '/': |
a2 = fnode_normalize_pwr(a2,mone,expand); |
a2 = nfnode_pwr(a2,mone,expand); |
r = fnode_normalize_mul(a1,a2,expand); |
r = nfnode_mul(a1,a2,expand); |
break; |
break; |
case '^': |
case '^': |
r = fnode_normalize_pwr(a1,a2,expand); |
r = nfnode_pwr(a1,a2,expand); |
break; |
break; |
default: |
default: |
r = mkfnode(3,I_BOP,FA0(f),a1,a2); |
r = mkfnode(3,I_BOP,FA0(f),a1,a2); |
Line 2267 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2274 FNODE fnode_normalize(FNODE f,int expand) |
|
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
for ( ; n; n = NEXT(n) ) { |
for ( ; n; n = NEXT(n) ) { |
a1 = fnode_normalize(BDY(n),expand); |
a1 = fnode_normalize(BDY(n),expand); |
r = fnode_normalize_add(r,a1,expand); |
r = nfnode_add(r,a1,expand); |
} |
} |
break; |
break; |
case '*': |
case '*': |
Line 2275 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2282 FNODE fnode_normalize(FNODE f,int expand) |
|
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
for ( ; n; n = NEXT(n) ) { |
for ( ; n; n = NEXT(n) ) { |
a1 = fnode_normalize(BDY(n),expand); |
a1 = fnode_normalize(BDY(n),expand); |
r = fnode_normalize_mul(r,a1,expand); |
r = nfnode_mul(r,a1,expand); |
} |
} |
break; |
break; |
default: |
default: |
Line 2323 FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand) |
|
Line 2330 FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand) |
|
return r; |
return r; |
} |
} |
|
|
FNODE fnode_normalize_add(FNODE f1,FNODE f2,int expand) |
FNODE nfnode_add(FNODE f1,FNODE f2,int expand) |
{ |
{ |
NODE n1,n2,r0,r; |
NODE n1,n2,r0,r; |
FNODE b1,b2; |
FNODE b1,b2; |
Line 2337 FNODE fnode_normalize_add(FNODE f1,FNODE f2,int expand |
|
Line 2344 FNODE fnode_normalize_add(FNODE f1,FNODE f2,int expand |
|
r0 = 0; |
r0 = 0; |
while ( n1 && n2 ) { |
while ( n1 && n2 ) { |
fnode_coef_body(BDY(n1),&c1,&b1); fnode_coef_body(BDY(n2),&c2,&b2); |
fnode_coef_body(BDY(n1),&c1,&b1); fnode_coef_body(BDY(n2),&c2,&b2); |
if ( (s = fnode_normalize_comp(b1,b2)) > 0 ) { |
if ( (s = nfnode_comp(b1,b2)) > 0 ) { |
NEXTNODE(r0,r); BDY(r) = BDY(n1); n1 = NEXT(n1); |
NEXTNODE(r0,r); BDY(r) = BDY(n1); n1 = NEXT(n1); |
} else if ( s < 0 ) { |
} else if ( s < 0 ) { |
NEXTNODE(r0,r); BDY(r) = BDY(n2); n2 = NEXT(n2); |
NEXTNODE(r0,r); BDY(r) = BDY(n2); n2 = NEXT(n2); |
} else { |
} else { |
addnum(0,c1,c2,&c); |
addnum(0,c1,c2,&c); |
if ( c ) { |
if ( c ) { |
NEXTNODE(r0,r); BDY(r) = fnode_normalize_mul_coef(c,b1,expand); |
NEXTNODE(r0,r); BDY(r) = nfnode_mul_coef(c,b1,expand); |
} |
} |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
Line 2375 FNODE fnode_node_to_narymul(NODE n) |
|
Line 2382 FNODE fnode_node_to_narymul(NODE n) |
|
else return mkfnode(2,I_NARYOP,mulfs,n); |
else return mkfnode(2,I_NARYOP,mulfs,n); |
} |
} |
|
|
FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand) |
FNODE nfnode_mul(FNODE f1,FNODE f2,int expand) |
{ |
{ |
NODE n1,n2,r0,r,r1; |
NODE n1,n2,r0,r,r1; |
FNODE b1,b2,e1,e2,cc,t,t1; |
FNODE b1,b2,e1,e2,cc,t,t1; |
Line 2386 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
Line 2393 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
|
|
if ( IS_ZERO(f1) || IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,0); |
if ( IS_ZERO(f1) || IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,0); |
else if ( fnode_is_number(f1) ) |
else if ( fnode_is_number(f1) ) |
return fnode_normalize_mul_coef((Num)eval(f1),f2,expand); |
return nfnode_mul_coef((Num)eval(f1),f2,expand); |
else if ( fnode_is_number(f2) ) |
else if ( fnode_is_number(f2) ) |
return fnode_normalize_mul_coef((Num)eval(f2),f1,expand); |
return nfnode_mul_coef((Num)eval(f2),f1,expand); |
|
|
if ( expand && IS_NARYADD(f1) ) { |
if ( expand && IS_NARYADD(f1) ) { |
t = mkfnode(1,I_FORMULA,0); |
t = mkfnode(1,I_FORMULA,0); |
for ( n1 = (NODE)FA1(f1); n1; n1 = NEXT(n1) ) { |
for ( n1 = (NODE)FA1(f1); n1; n1 = NEXT(n1) ) { |
t1 = fnode_normalize_mul(BDY(n1),f2,expand); |
t1 = nfnode_mul(BDY(n1),f2,expand); |
t = fnode_normalize_add(t,t1,expand); |
t = nfnode_add(t,t1,expand); |
} |
} |
return t; |
return t; |
} |
} |
if ( expand && IS_NARYADD(f2) ) { |
if ( expand && IS_NARYADD(f2) ) { |
t = mkfnode(1,I_FORMULA,0); |
t = mkfnode(1,I_FORMULA,0); |
for ( n2 = (NODE)FA1(f2); n2; n2 = NEXT(n2) ) { |
for ( n2 = (NODE)FA1(f2); n2; n2 = NEXT(n2) ) { |
t1 = fnode_normalize_mul(f1,BDY(n2),expand); |
t1 = nfnode_mul(f1,BDY(n2),expand); |
t = fnode_normalize_add(t,t1,expand); |
t = nfnode_add(t,t1,expand); |
} |
} |
return t; |
return t; |
} |
} |
Line 2439 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
Line 2446 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
return fnode_node_to_narymul(r); |
return fnode_node_to_narymul(r); |
} |
} |
|
|
FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand) |
FNODE nfnode_pwr(FNODE f1,FNODE f2,int expand) |
{ |
{ |
FNODE b,b1,e1,e,cc,r,mf2,mone,inv; |
FNODE b,b1,e1,e,cc,r,mf2,mone,inv; |
Num c,c1,nf2; |
Num c,c1,nf2; |
Line 2460 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
Line 2467 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
return mkfnode(3,I_BOP,pwrfs,f1,f2); |
return mkfnode(3,I_BOP,pwrfs,f1,f2); |
} else if ( IS_BINARYPWR(f1) ) { |
} else if ( IS_BINARYPWR(f1) ) { |
b1 = FA1(f1); e1 = FA2(f1); |
b1 = FA1(f1); e1 = FA2(f1); |
e = fnode_normalize_mul(e1,f2,expand); |
e = nfnode_mul(e1,f2,expand); |
if ( fnode_is_one(e) ) |
if ( fnode_is_one(e) ) |
return b1; |
return b1; |
else |
else |
Line 2492 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
Line 2499 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
} else if ( expand && fnode_is_integer(f2) |
} else if ( expand && fnode_is_integer(f2) |
&& fnode_is_nonnegative_integer(f2) ) { |
&& fnode_is_nonnegative_integer(f2) ) { |
q = (Q)eval(f2); |
q = (Q)eval(f2); |
if ( PL(NM(q)) > 1 ) error("fnode_normalize_pwr : exponent too large"); |
if ( PL(NM(q)) > 1 ) error("nfnode_pwr : exponent too large"); |
return fnode_expand_pwr(f1,QTOS(q)); |
return fnode_expand_pwr(f1,QTOS(q)); |
} else |
} else |
return mkfnode(3,I_BOP,pwrfs,f1,f2); |
return mkfnode(3,I_BOP,pwrfs,f1,f2); |
Line 2509 FNODE fnode_expand_pwr(FNODE f,int n) |
|
Line 2516 FNODE fnode_expand_pwr(FNODE f,int n) |
|
else { |
else { |
n1 = n/2; |
n1 = n/2; |
f1 = fnode_expand_pwr(f,n1); |
f1 = fnode_expand_pwr(f,n1); |
f2 = fnode_normalize_mul(f1,f1,1); |
f2 = nfnode_mul(f1,f1,1); |
if ( n%2 ) f2 = fnode_normalize_mul(f2,f,1); |
if ( n%2 ) f2 = nfnode_mul(f2,f,1); |
return f2; |
return f2; |
} |
} |
} |
} |
Line 2549 FNODE to_narymul(FNODE f) |
|
Line 2556 FNODE to_narymul(FNODE f) |
|
return r; |
return r; |
} |
} |
|
|
FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expand) |
FNODE nfnode_mul_coef(Num c,FNODE f,int expand) |
{ |
{ |
FNODE b1,cc; |
FNODE b1,cc; |
Num c1,c2; |
Num c1,c2; |
Line 2565 FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expan |
|
Line 2572 FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expan |
|
cc = mkfnode(1,I_FORMULA,c2); |
cc = mkfnode(1,I_FORMULA,c2); |
if ( fnode_is_number(b1) ) { |
if ( fnode_is_number(b1) ) { |
if ( !fnode_is_one(b1) ) |
if ( !fnode_is_one(b1) ) |
error("fnode_normalize_mul_coef : cannot happen"); |
error("nfnode_mul_coef : cannot happen"); |
else |
else |
return cc; |
return cc; |
} else if ( IS_NARYMUL(b1) ) { |
} else if ( IS_NARYMUL(b1) ) { |
Line 2574 FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expan |
|
Line 2581 FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expan |
|
} else if ( expand && IS_NARYADD(b1) ) { |
} else if ( expand && IS_NARYADD(b1) ) { |
for ( r0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) { |
for ( r0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) { |
NEXTNODE(r0,r); |
NEXTNODE(r0,r); |
BDY(r) = fnode_normalize_mul_coef(c2,BDY(n),expand); |
BDY(r) = nfnode_mul_coef(c2,BDY(n),expand); |
} |
} |
if ( r0 ) NEXT(r) = 0; |
if ( r0 ) NEXT(r) = 0; |
return fnode_node_to_naryadd(r0); |
return fnode_node_to_naryadd(r0); |
Line 2603 void fnode_coef_body(FNODE f,Num *cp,FNODE *bp) |
|
Line 2610 void fnode_coef_body(FNODE f,Num *cp,FNODE *bp) |
|
} |
} |
} |
} |
|
|
int fnode_normalize_comp_pwr(FNODE f1,FNODE f2); |
int nfnode_comp_pwr(FNODE f1,FNODE f2); |
|
|
int fnode_normalize_comp(FNODE f1,FNODE f2) |
int nfnode_comp(FNODE f1,FNODE f2) |
{ |
{ |
NODE n1,n2; |
NODE n1,n2; |
int r,i1,i2; |
int r,i1,i2,ret; |
char *nm1,*nm2; |
char *nm1,*nm2; |
FNODE b1,b2,e1,e2,g; |
FNODE b1,b2,e1,e2,g,a1,a2,fn1,fn2; |
Num ee,ee1,c1,c2; |
Num ee,ee1,c1,c2; |
|
|
if ( IS_NARYADD(f1) || IS_NARYADD(f2) ) { |
if ( IS_NARYADD(f1) || IS_NARYADD(f2) ) { |
f1 = to_naryadd(f1); f2 = to_naryadd(f2); |
f1 = to_naryadd(f1); f2 = to_naryadd(f2); |
n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2); |
n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2); |
while ( n1 && n2 ) |
while ( n1 && n2 ) |
if ( r = fnode_normalize_comp(BDY(n1),BDY(n2)) ) return r; |
if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
else { |
else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
Line 2639 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
Line 2646 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
fnode_base_exp(BDY(n1),&b1,&e1); |
fnode_base_exp(BDY(n1),&b1,&e1); |
fnode_base_exp(BDY(n2),&b2,&e2); |
fnode_base_exp(BDY(n2),&b2,&e2); |
|
|
if ( r = fnode_normalize_comp(b1,b2) ) { |
if ( r = nfnode_comp(b1,b2) ) { |
if ( r > 0 ) |
if ( r > 0 ) |
return fnode_normalize_comp(e1,mkfnode(1,I_FORMULA,0)); |
return nfnode_comp(e1,mkfnode(1,I_FORMULA,0)); |
else if ( r < 0 ) |
else if ( r < 0 ) |
return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2); |
return nfnode_comp(mkfnode(1,I_FORMULA,0),e2); |
} else { |
} else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
if ( fnode_is_number(e1) && fnode_is_number(e2) ) { |
if ( fnode_is_number(e1) && fnode_is_number(e2) ) { |
Line 2659 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
Line 2666 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
MKNODE(n2,g,n2); |
MKNODE(n2,g,n2); |
} |
} |
} else { |
} else { |
r = fnode_normalize_comp(e1,e2); |
r = nfnode_comp(e1,e2); |
if ( r > 0 ) return 1; |
if ( r > 0 ) return 1; |
else if ( r < 0 ) return -1; |
else if ( r < 0 ) return -1; |
} |
} |
Line 2667 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
Line 2674 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
} |
} |
} |
} |
if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) ) |
if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) ) |
return fnode_normalize_comp_pwr(f1,f2); |
return nfnode_comp_pwr(f1,f2); |
|
|
/* now, IDs of f1 and f2 must be I_FORMULA, I_FUNC, or I_PVAR */ |
/* now, IDs of f1 and f2 must be I_FORMULA, I_FUNC, I_IFUNC or I_PVAR */ |
|
/* I_IFUNC > I_PVAR > I_FUNC > I_FORMULA */ |
switch ( f1->id ) { |
switch ( f1->id ) { |
case I_FORMULA: |
case I_FORMULA: |
switch ( f2->id ) { |
switch ( f2->id ) { |
case I_FORMULA: |
case I_FORMULA: |
return arf_comp(CO,FA0(f1),FA0(f2)); |
return arf_comp(CO,FA0(f1),FA0(f2)); |
case I_FUNC: case I_PVAR: |
case I_FUNC: case I_IFUNC: case I_PVAR: |
return -1; |
return -1; |
default: |
default: |
error("fnode_normalize_comp : undefined"); |
error("nfnode_comp : undefined"); |
} |
} |
break; |
break; |
case I_FUNC: |
case I_FUNC: |
switch ( f2->id ) { |
switch ( f2->id ) { |
case I_FORMULA: |
case I_FORMULA: |
return 1; |
return 1; |
|
case I_PVAR: case I_IFUNC: |
|
return -1; |
case I_FUNC: |
case I_FUNC: |
nm1 = ((FUNC)FA0(f1))->name; nm2 = ((FUNC)FA0(f2))->name; |
nm1 = ((FUNC)FA0(f1))->name; nm2 = ((FUNC)FA0(f2))->name; |
r = strcmp(nm1,nm2); |
r = strcmp(nm1,nm2); |
Line 2694 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
Line 2704 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
/* compare args */ |
/* compare args */ |
n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2)); |
n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2)); |
while ( n1 && n2 ) |
while ( n1 && n2 ) |
if ( r = fnode_normalize_comp(BDY(n1),BDY(n2)) ) return r; |
if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
else { |
else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
return n1?1:(n2?-1:0); |
return n1?1:(n2?-1:0); |
} |
} |
break; |
break; |
case I_PVAR: |
|
return -1; |
|
default: |
default: |
error("fnode_normalize_comp : undefined"); |
error("nfnode_comp : undefined"); |
} |
} |
case I_PVAR: |
case I_PVAR: |
switch ( f2->id ) { |
switch ( f2->id ) { |
case I_FORMULA: case I_FUNC: |
case I_FORMULA: case I_FUNC: |
return 1; |
return 1; |
|
case I_IFUNC: |
|
return -1; |
case I_PVAR: |
case I_PVAR: |
i1 = (int)FA0(f1); i2 = (int)FA0(f2); |
i1 = (int)FA0(f1); i2 = (int)FA0(f2); |
if ( i1 > i2 ) return 1; |
if ( i1 > i2 ) return 1; |
else if ( i1 < i2 ) return -1; |
else if ( i1 < i2 ) return -1; |
else return 0; |
else return 0; |
default: |
default: |
error("fnode_normalize_comp : undefined"); |
error("nfnode_comp : undefined"); |
} |
} |
break; |
break; |
|
case I_IFUNC: |
|
switch ( f2->id ) { |
|
case I_FORMULA: case I_FUNC: case I_PVAR: |
|
return 1; |
|
case I_IFUNC: |
|
i1 = (int)FA0((FNODE)FA0(f1)); |
|
i2 = (int)FA0((FNODE)FA0(f2)); |
|
if ( i1 > i2 ) return 1; |
|
else if ( i1 < i2 ) return -1; |
|
else { |
|
/* compare args */ |
|
n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2)); |
|
while ( n1 && n2 ) |
|
if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
|
else { |
|
n1 = NEXT(n1); n2 = NEXT(n2); |
|
} |
|
return n1?1:(n2?-1:0); |
|
} |
|
break; |
|
|
|
default: |
|
error("nfnode_comp : undefined"); |
|
} |
|
break; |
default: |
default: |
error("fnode_normalize_comp : undefined"); |
error("nfnode_comp : undefined"); |
} |
} |
} |
} |
|
|
int fnode_normalize_comp_pwr(FNODE f1,FNODE f2) |
int nfnode_comp_pwr(FNODE f1,FNODE f2) |
{ |
{ |
FNODE b1,b2,e1,e2; |
FNODE b1,b2,e1,e2; |
int r; |
int r; |
|
|
fnode_base_exp(f1,&b1,&e1); |
fnode_base_exp(f1,&b1,&e1); |
fnode_base_exp(f2,&b2,&e2); |
fnode_base_exp(f2,&b2,&e2); |
if ( r = fnode_normalize_comp(b1,b2) ) { |
if ( r = nfnode_comp(b1,b2) ) { |
if ( r > 0 ) |
if ( r > 0 ) |
return fnode_normalize_comp(e1,mkfnode(1,I_FORMULA,0)); |
return nfnode_comp(e1,mkfnode(1,I_FORMULA,0)); |
else if ( r < 0 ) |
else if ( r < 0 ) |
return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2); |
return nfnode_comp(mkfnode(1,I_FORMULA,0),e2); |
} else return fnode_normalize_comp(e1,e2); |
} else return nfnode_comp(e1,e2); |
} |
} |
|
|
NODE append_node(NODE a1,NODE a2) |
NODE append_node(NODE a1,NODE a2) |
Line 2754 NODE append_node(NODE a1,NODE a2) |
|
Line 2789 NODE append_node(NODE a1,NODE a2) |
|
} |
} |
} |
} |
|
|
int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
int nfnode_match(FNODE f,FNODE pat,NODE *rp) |
{ |
{ |
NODE m,m1,m2,base,exp,fa,pa,n; |
NODE m,m1,m2,base,exp,fa,pa,n; |
LIST l; |
LIST l; |
QUOTE qp,qf; |
QUOTE qp,qf; |
FNODE fbase,fexp,a; |
FNODE fbase,fexp,a,fh; |
FUNC ff,pf; |
FUNC ff,pf; |
int r; |
int r; |
|
|
Line 2778 int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
|
Line 2813 int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
|
case I_BOP: |
case I_BOP: |
/* OPNAME should be "^" */ |
/* OPNAME should be "^" */ |
if ( !IS_BINARYPWR(pat) ) |
if ( !IS_BINARYPWR(pat) ) |
error("fnode_normalize_match : invalid BOP"); |
error("nfnode_match : invalid BOP"); |
if ( IS_BINARYPWR(f) ) { |
if ( IS_BINARYPWR(f) ) { |
fbase = FA1(f); fexp = FA2(f); |
fbase = FA1(f); fexp = FA2(f); |
} else { |
} else { |
fbase = f; fexp = mkfnode(1,I_FORMULA,ONE); |
fbase = f; fexp = mkfnode(1,I_FORMULA,ONE); |
} |
} |
if ( !fnode_normalize_match(fbase,FA1(pat),&base) ) return 0; |
if ( !nfnode_match(fbase,FA1(pat),&base) ) return 0; |
a = rewrite_fnode(FA2(pat),base); |
a = rewrite_fnode(FA2(pat),base); |
if ( !fnode_normalize_match(fexp,a,&exp) ) return 0; |
if ( !nfnode_match(fexp,a,&exp) ) return 0; |
else { |
else { |
*rp = append_node(base,exp); |
*rp = append_node(base,exp); |
return 1; |
return 1; |
} |
} |
break; |
break; |
|
|
case I_FUNC: |
case I_FUNC: case I_IFUNC: |
if ( f->id != I_FUNC ) return 0; |
if ( f->id != I_FUNC ) return 0; |
ff = (FUNC)FA0(f); pf = (FUNC)FA0(pat); |
ff = (FUNC)FA0(f); |
if ( strcmp(ff->fullname,pf->fullname) ) return 0; |
if ( pat->id == I_FUNC ) { |
|
pf = (FUNC)FA0(pat); |
|
if ( strcmp(ff->fullname,pf->fullname) ) return 0; |
|
m = 0; |
|
} else { |
|
/* XXX : I_FUNC_HEAD is a dummy id to pass FUNC */ |
|
fh = mkfnode(1,I_FUNC_HEAD,FA0(f)); |
|
m = mknode(1,mknode(2,FA0((FNODE)FA0(pat)),fh),0); |
|
} |
/* FA1(f) and FA1(pat) are I_LIST */ |
/* FA1(f) and FA1(pat) are I_LIST */ |
fa = (NODE)FA0((FNODE)FA1(f)); |
fa = (NODE)FA0((FNODE)FA1(f)); |
pa = (NODE)FA0((FNODE)FA1(pat)); |
pa = (NODE)FA0((FNODE)FA1(pat)); |
m = 0; |
|
while ( fa && pa ) { |
while ( fa && pa ) { |
a = rewrite_fnode(BDY(pa),m); |
a = rewrite_fnode(BDY(pa),m); |
if ( !fnode_normalize_match(BDY(fa),a,&m1) ) return 0; |
if ( !nfnode_match(BDY(fa),a,&m1) ) return 0; |
m = append_node(m1,m); |
m = append_node(m1,m); |
fa = NEXT(fa); pa = NEXT(pa); |
fa = NEXT(fa); pa = NEXT(pa); |
} |
} |
Line 2815 int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
|
Line 2857 int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
|
|
|
case I_NARYOP: |
case I_NARYOP: |
if ( IS_NARYADD(pat) ) |
if ( IS_NARYADD(pat) ) |
return fnode_normalize_match_naryadd(f,pat,rp); |
return nfnode_match_naryadd(f,pat,rp); |
else if ( IS_NARYMUL(pat) ) |
else if ( IS_NARYMUL(pat) ) |
return fnode_normalize_match_narymul(f,pat,rp); |
return nfnode_match_narymul(f,pat,rp); |
else |
else |
error("fnode_normalize_match : invalid NARYOP"); |
error("nfnode_match : invalid NARYOP"); |
break; |
break; |
|
|
default: |
default: |
error("fnode_normalize_match : invalid pattern"); |
error("nfnode_match : invalid pattern"); |
} |
} |
} |
} |
|
|
Line 2887 FNODE fnode_right_narymul(FNODE p,int i) |
|
Line 2929 FNODE fnode_right_narymul(FNODE p,int i) |
|
} |
} |
} |
} |
|
|
int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE *rp) |
int nfnode_match_naryadd(FNODE f,FNODE p,NODE *rp) |
{ |
{ |
int fl,pl,fi,pi; |
int fl,pl,fi,pi; |
NODE fa,pa,t,s,m,m1; |
NODE fa,pa,t,s,m,m1; |
Line 2899 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
Line 2941 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
if ( fl < pl ) return 0; |
if ( fl < pl ) return 0; |
else if ( pl == 1 ) { |
else if ( pl == 1 ) { |
if ( fl == 1 ) |
if ( fl == 1 ) |
return fnode_normalize_match(BDY(fa),BDY(pa),rp); |
return nfnode_match(BDY(fa),BDY(pa),rp); |
else |
else |
return 0; |
return 0; |
} else { |
} else { |
Line 2909 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
Line 2951 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
/* all are I_PVAR */ |
/* all are I_PVAR */ |
m = 0; |
m = 0; |
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
fnode_normalize_match(BDY(s),BDY(t),&m1); |
nfnode_match(BDY(s),BDY(t),&m1); |
m = append_node(m1,m); |
m = append_node(m1,m); |
} |
} |
if ( !NEXT(s) ) |
if ( !NEXT(s) ) |
fr = (FNODE)BDY(s); |
fr = (FNODE)BDY(s); |
else |
else |
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
fnode_normalize_match(fr,BDY(t),&m1); |
nfnode_match(fr,BDY(t),&m1); |
*rp = append_node(m1,m); |
*rp = append_node(m1,m); |
return 1; |
return 1; |
} else { |
} else { |
pivot = (FNODE)BDY(t); |
pivot = (FNODE)BDY(t); |
pr = fnode_removeith_naryadd(p,pi); |
pr = fnode_removeith_naryadd(p,pi); |
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
if ( fnode_normalize_match(BDY(s),pivot,&m) ) { |
if ( nfnode_match(BDY(s),pivot,&m) ) { |
fr = fnode_removeith_naryadd(f,fi); |
fr = fnode_removeith_naryadd(f,fi); |
prr = rewrite_fnode(pr,m); |
prr = rewrite_fnode(pr,m); |
if ( fnode_normalize_match(fr,prr,&m1) ) { |
if ( nfnode_match(fr,prr,&m1) ) { |
*rp = append_node(m,m1); |
*rp = append_node(m,m1); |
return 1; |
return 1; |
} |
} |
Line 2937 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
Line 2979 int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE |
|
} |
} |
} |
} |
|
|
int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE *rp) |
int nfnode_match_narymul(FNODE f,FNODE p,NODE *rp) |
{ |
{ |
int fl,pl,fi,pi; |
int fl,pl,fi,pi; |
NODE fa,pa,t,s,m,m1; |
NODE fa,pa,t,s,m,m1; |
Line 2949 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
Line 2991 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
if ( fl < pl ) return 0; |
if ( fl < pl ) return 0; |
else if ( pl == 1 ) { |
else if ( pl == 1 ) { |
if ( fl == 1 ) |
if ( fl == 1 ) |
return fnode_normalize_match(BDY(fa),BDY(pa),rp); |
return nfnode_match(BDY(fa),BDY(pa),rp); |
else |
else |
return 0; |
return 0; |
} else { |
} else { |
Line 2960 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
Line 3002 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
m = 0; |
m = 0; |
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
pr = rewrite_fnode(BDY(t),m); |
pr = rewrite_fnode(BDY(t),m); |
if ( !fnode_normalize_match(BDY(s),pr,&m1) ) return 0; |
if ( !nfnode_match(BDY(s),pr,&m1) ) return 0; |
m = append_node(m1,m); |
m = append_node(m1,m); |
} |
} |
if ( !NEXT(s) ) |
if ( !NEXT(s) ) |
Line 2968 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
Line 3010 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
else |
else |
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
pr = rewrite_fnode(BDY(t),m); |
pr = rewrite_fnode(BDY(t),m); |
if ( !fnode_normalize_match(fr,pr,&m1) ) return 0; |
if ( !nfnode_match(fr,pr,&m1) ) return 0; |
*rp = append_node(m1,m); |
*rp = append_node(m1,m); |
return 1; |
return 1; |
} else { |
} else { |
Line 2977 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
Line 3019 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
pright = fnode_right_narymul(p,pi); |
pright = fnode_right_narymul(p,pi); |
/* XXX : incomplete */ |
/* XXX : incomplete */ |
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
if ( fnode_normalize_match(BDY(s),pivot,&m) ) { |
if ( nfnode_match(BDY(s),pivot,&m) ) { |
fleft = fnode_left_narymul(f,fi); |
fleft = fnode_left_narymul(f,fi); |
pleft1 = rewrite_fnode(pleft,m); |
pleft1 = rewrite_fnode(pleft,m); |
if ( fnode_normalize_match(fleft,pleft1,&m1) ) { |
if ( nfnode_match(fleft,pleft1,&m1) ) { |
m = append_node(m1,m); |
m = append_node(m1,m); |
fright = fnode_right_narymul(f,fi); |
fright = fnode_right_narymul(f,fi); |
pright1 = rewrite_fnode(pright,m); |
pright1 = rewrite_fnode(pright,m); |
if ( fnode_normalize_match(fright,pright1,&m1) ) { |
if ( nfnode_match(fright,pright1,&m1) ) { |
*rp = append_node(m1,m); |
*rp = append_node(m1,m); |
return 1; |
return 1; |
} |
} |
Line 2993 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
Line 3035 int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE |
|
} |
} |
return 0; |
return 0; |
} |
} |
|
} |
|
} |
|
|
|
NODE nfnode_pvars(FNODE pat,NODE found) |
|
{ |
|
int ind; |
|
NODE prev,t; |
|
int *pair; |
|
|
|
switch ( pat->id ) { |
|
case I_PVAR: |
|
ind = (int)FA0(pat); |
|
for ( prev = 0, t = found; t; prev = t, t = NEXT(t) ) { |
|
pair = (int *)BDY(t); |
|
if ( pair[0] == ind ) { |
|
pair[1]++; |
|
return found; |
|
} |
|
} |
|
pair = (int *)MALLOC_ATOMIC(sizeof(int)*2); |
|
pair[0] = ind; pair[1] = 1; |
|
if ( !prev ) |
|
MKNODE(found,pair,0); |
|
else |
|
MKNODE(NEXT(prev),pair,0); |
|
return found; |
|
|
|
case I_FORMULA: |
|
return found; |
|
|
|
case I_BOP: |
|
/* OPNAME should be "^" */ |
|
if ( !IS_BINARYPWR(pat) ) |
|
error("nfnode_pvar : invalid BOP"); |
|
found = nfnode_pvars(FA1(pat),found); |
|
found = nfnode_pvars(FA2(pat),found); |
|
return found; |
|
|
|
case I_FUNC: |
|
t = (NODE)FA0((FNODE)FA1(pat)); |
|
for ( ; t; t = NEXT(t) ) |
|
found = nfnode_pvars(BDY(t),found); |
|
return found; |
|
|
|
case I_NARYOP: |
|
t = (NODE)FA1(pat); |
|
for ( ; t; t = NEXT(t) ) |
|
found = nfnode_pvars(BDY(t),found); |
|
return found; |
|
|
|
default: |
|
error("nfnode_match : invalid pattern"); |
} |
} |
} |
} |