version 1.85, 2005/10/26 11:07:50 |
version 1.90, 2005/11/02 05:39:23 |
|
|
* 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.84 2005/10/26 10:47:00 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.89 2005/11/02 05:18:41 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
Line 58 extern jmp_buf environnement; |
|
Line 58 extern jmp_buf environnement; |
|
#endif |
#endif |
#include <string.h> |
#include <string.h> |
|
|
|
#if defined(__GNUC__) |
|
#define INLINE inline |
|
#elif defined(VISUAL) |
|
#define INLINE __inline |
|
#else |
|
#define INLINE |
|
#endif |
|
|
struct TeXSymbol { |
struct TeXSymbol { |
char *text; |
char *text; |
char *symbol; |
char *symbol; |
Line 88 void Pquote_is_integer(),Pquote_is_rational(),Pquote_i |
|
Line 96 void Pquote_is_integer(),Pquote_is_rational(),Pquote_i |
|
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 Pquote_normalize_comp(); |
|
void Pquote_normalize_match(); |
|
|
void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name(); |
void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name(); |
void Pquote_unify(),Pget_quote_id(),Pquote_match_rewrite(); |
void Pquote_match(),Pget_quote_id(),Pquote_match_rewrite(); |
void Pquote_to_nary(),Pquote_to_bin(); |
void Pquote_to_nary(),Pquote_to_bin(); |
|
void fnode_do_assign(NODE arg); |
void do_assign(NODE arg); |
void do_assign(NODE arg); |
void fnodetotex_tb(FNODE f,TB tb); |
void fnodetotex_tb(FNODE f,TB tb); |
char *symbol_name(char *name); |
char *symbol_name(char *name); |
Line 102 void tb_to_string(TB tb,STRING *rp); |
|
Line 112 void tb_to_string(TB tb,STRING *rp); |
|
void fnodenodetotex_tb(NODE n,TB tb); |
void fnodenodetotex_tb(NODE n,TB tb); |
void fargstotex_tb(char *opname,FNODE f,TB tb); |
void fargstotex_tb(char *opname,FNODE f,TB tb); |
int top_is_minus(FNODE f); |
int top_is_minus(FNODE f); |
int quote_unify(Obj f,Obj pat,NODE *rp); |
int quote_match(Obj f,Obj pat,NODE *rp); |
|
|
struct ftab str_tab[] = { |
struct ftab str_tab[] = { |
{"sprintf",Psprintf,-99999999}, |
{"sprintf",Psprintf,-99999999}, |
Line 127 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,0x3}, |
{"quote_normalize_comp",Pquote_normalize_comp,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}, |
Line 136 struct ftab str_tab[] = { |
|
Line 146 struct ftab str_tab[] = { |
|
{"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_unify",Pquote_unify,2}, |
{"quote_match",Pquote_match,2}, |
|
{"quote_normalize_match",Pquote_normalize_match,2}, |
{"quote_match_rewrite",Pquote_match_rewrite,-4}, |
{"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}, |
Line 623 void Pquote_is_dependent(NODE arg,Q *rp) |
|
Line 634 void Pquote_is_dependent(NODE arg,Q *rp) |
|
} |
} |
|
|
|
|
void Pquote_unify(NODE arg,Q *rp) |
void Pquote_match(NODE arg,Q *rp) |
{ |
{ |
FNODE f,g; |
FNODE f,g; |
Obj obj; |
Obj obj; |
Line 634 void Pquote_unify(NODE arg,Q *rp) |
|
Line 645 void Pquote_unify(NODE arg,Q *rp) |
|
#if 0 |
#if 0 |
g = partial_eval(BDY(((QUOTE)ARG0(arg)))); |
g = partial_eval(BDY(((QUOTE)ARG0(arg)))); |
MKQUOTE(q,g); |
MKQUOTE(q,g); |
ret = quote_unify((Obj)q,(Obj)ARG1(arg),&r); |
ret = quote_match((Obj)q,(Obj)ARG1(arg),&r); |
#else |
#else |
obj = (Obj)ARG0(arg); |
obj = (Obj)ARG0(arg); |
ret = quote_unify(obj,(Obj)ARG1(arg),&r); |
ret = quote_match(obj,(Obj)ARG1(arg),&r); |
#endif |
#endif |
if ( ret ) { |
if ( ret ) { |
do_assign(r); |
do_assign(r); |
Line 646 void Pquote_unify(NODE arg,Q *rp) |
|
Line 657 void Pquote_unify(NODE arg,Q *rp) |
|
*rp = 0; |
*rp = 0; |
} |
} |
|
|
|
void Pquote_normalize_match(NODE arg,Q *rp) |
|
{ |
|
QUOTE fq,pq; |
|
FNODE f,p; |
|
int ret; |
|
NODE r; |
|
|
|
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); |
|
ret = fnode_normalize_match(f,p,&r); |
|
if ( ret ) { |
|
fnode_do_assign(r); |
|
*rp = ONE; |
|
} else |
|
*rp = 0; |
|
} |
|
|
FNODE rewrite_fnode(FNODE,NODE); |
FNODE rewrite_fnode(FNODE,NODE); |
|
|
extern Obj VOIDobj; |
extern Obj VOIDobj; |
Line 659 void Pquote_match_rewrite(NODE arg,Obj *rp) |
|
Line 687 void Pquote_match_rewrite(NODE arg,Obj *rp) |
|
int ret,ind,ac; |
int ret,ind,ac; |
|
|
obj = (Obj)ARG0(arg); |
obj = (Obj)ARG0(arg); |
ret = quote_unify(obj,(Obj)ARG1(arg),&r); |
ret = quote_match(obj,(Obj)ARG1(arg),&r); |
if ( ret ) { |
if ( ret ) { |
for ( t = r, s0 = 0; t; t = NEXT(t) ) { |
for ( t = r, s0 = 0; t; t = NEXT(t) ) { |
NEXTNODE(s0,s); |
NEXTNODE(s0,s); |
Line 704 void do_assign(NODE arg) |
|
Line 732 void do_assign(NODE arg) |
|
} |
} |
} |
} |
|
|
|
/* [[index,fnode],...] */ |
|
|
|
void fnode_do_assign(NODE arg) |
|
{ |
|
NODE t,pair; |
|
int pv; |
|
FNODE f; |
|
QUOTE value; |
|
|
|
for ( t = arg; t; t = NEXT(t) ) { |
|
pair = (NODE)BDY(t); |
|
pv = (int)BDY(pair); |
|
f = (FNODE)(BDY(NEXT(pair))); |
|
MKQUOTE(value,f); |
|
ASSPV(pv,value); |
|
} |
|
} |
|
|
/* |
/* |
/* consistency check and merge |
/* consistency check and merge |
*/ |
*/ |
Line 739 int merge_matching_node(NODE n,NODE a,NODE *rp) |
|
Line 785 int merge_matching_node(NODE n,NODE a,NODE *rp) |
|
return 1; |
return 1; |
} |
} |
|
|
int quote_unify_node(NODE f,NODE pat,NODE *rp) { |
int quote_match_node(NODE f,NODE pat,NODE *rp) { |
NODE r,a,tf,tp,r1; |
NODE r,a,tf,tp,r1; |
int ret; |
int ret; |
|
|
if ( length(f) != length(pat) ) return 0; |
if ( length(f) != length(pat) ) return 0; |
r = 0; |
r = 0; |
for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) { |
for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) { |
ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a); |
ret = quote_match((Obj)BDY(tf),(Obj)BDY(tp),&a); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
ret = merge_matching_node(r,a,&r1); |
ret = merge_matching_node(r,a,&r1); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
Line 758 int quote_unify_node(NODE f,NODE pat,NODE *rp) { |
|
Line 804 int quote_unify_node(NODE f,NODE pat,NODE *rp) { |
|
|
|
/* f = [a,b,c,...] pat = [X,Y,...] rpat matches the rest of f */ |
/* f = [a,b,c,...] pat = [X,Y,...] rpat matches the rest of f */ |
|
|
int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp) { |
int quote_match_cons(NODE f,NODE pat,Obj rpat,NODE *rp) { |
QUOTE q; |
QUOTE q; |
Q id; |
Q id; |
FNODE fn; |
FNODE fn; |
Line 770 int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp |
|
Line 816 int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp |
|
if ( length(f) < length(pat) ) return 0; |
if ( length(f) < length(pat) ) return 0; |
r = 0; |
r = 0; |
for ( tf = f, tp = pat; tp; tf = NEXT(tf), tp = NEXT(tp) ) { |
for ( tf = f, tp = pat; tp; tf = NEXT(tf), tp = NEXT(tp) ) { |
ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a); |
ret = quote_match((Obj)BDY(tf),(Obj)BDY(tp),&a); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
ret = merge_matching_node(r,a,&r1); |
ret = merge_matching_node(r,a,&r1); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
Line 782 int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp |
|
Line 828 int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp |
|
MKLIST(alist,a); |
MKLIST(alist,a); |
arg = mknode(1,alist); |
arg = mknode(1,alist); |
Pfunargs_to_quote(arg,&q); |
Pfunargs_to_quote(arg,&q); |
ret = quote_unify((Obj)q,rpat,&a); |
ret = quote_match((Obj)q,rpat,&a); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
ret = merge_matching_node(r,a,&r1); |
ret = merge_matching_node(r,a,&r1); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
Line 801 void get_quote_id_arg(QUOTE f,int *id,NODE *r) |
|
Line 847 void get_quote_id_arg(QUOTE f,int *id,NODE *r) |
|
|
|
/* *rp : [[quote(A),quote(1)],...] */ |
/* *rp : [[quote(A),quote(1)],...] */ |
|
|
int quote_unify(Obj f, Obj pat, NODE *rp) |
int quote_match(Obj f, Obj pat, NODE *rp) |
{ |
{ |
NODE tf,tp,head,body; |
NODE tf,tp,head,body; |
NODE parg,farg,r; |
NODE parg,farg,r; |
Line 820 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
Line 866 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
return 0; |
return 0; |
else if ( OID(pat) == O_LIST ) { |
else if ( OID(pat) == O_LIST ) { |
if ( OID(f) == O_LIST ) |
if ( OID(f) == O_LIST ) |
return quote_unify_node(BDY((LIST)f),BDY((LIST)pat),rp); |
return quote_match_node(BDY((LIST)f),BDY((LIST)pat),rp); |
else |
else |
return 0; |
return 0; |
} else if ( OID(pat) == O_QUOTE ) { |
} else if ( OID(pat) == O_QUOTE ) { |
Line 847 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
Line 893 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
|
|
tp = BDY((LIST)BDY(parg)); |
tp = BDY((LIST)BDY(parg)); |
if ( pid == I_LIST ) |
if ( pid == I_LIST ) |
return quote_unify_node(tf,tp,rp); |
return quote_match_node(tf,tp,rp); |
else { |
else { |
rpat = (Obj)BDY(NEXT(parg)); |
rpat = (Obj)BDY(NEXT(parg)); |
return quote_unify_cons(tf,tp,rpat,rp); |
return quote_match_cons(tf,tp,rpat,rp); |
} |
} |
|
|
case I_PVAR: |
case I_PVAR: |
Line 866 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
Line 912 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
if ( id == I_FUNC ) { |
if ( id == I_FUNC ) { |
r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r); |
r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r); |
head = mknode(1,l); |
head = mknode(1,l); |
ret = quote_unify(BDY(NEXT(farg)), |
ret = quote_match(BDY(NEXT(farg)), |
BDY(NEXT(parg)),&body); |
BDY(NEXT(parg)),&body); |
if ( !ret ) return 0; |
if ( !ret ) return 0; |
else return merge_matching_node(head,body,rp); |
else return merge_matching_node(head,body,rp); |
Line 894 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
Line 940 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
get_quote_id_arg((QUOTE)pat,&pid,&parg); |
get_quote_id_arg((QUOTE)pat,&pid,&parg); |
get_quote_id_arg((QUOTE)f,&id,&farg); |
get_quote_id_arg((QUOTE)f,&id,&farg); |
if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0; |
if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0; |
return quote_unify_node(NEXT(farg),NEXT(parg),rp); |
return quote_match_node(NEXT(farg),NEXT(parg),rp); |
|
|
default: |
default: |
if ( OID(f) != O_QUOTE ) return 0; |
if ( OID(f) != O_QUOTE ) return 0; |
Line 902 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
Line 948 int quote_unify(Obj f, Obj pat, NODE *rp) |
|
if ( id != pid ) return 0; |
if ( id != pid ) return 0; |
get_quote_id_arg((QUOTE)pat,&pid,&parg); |
get_quote_id_arg((QUOTE)pat,&pid,&parg); |
get_quote_id_arg((QUOTE)f,&id,&farg); |
get_quote_id_arg((QUOTE)f,&id,&farg); |
return quote_unify_node(farg,parg,rp); |
return quote_match_node(farg,parg,rp); |
} |
} |
} |
} |
} |
} |
Line 1942 void Pquote_normalize(NODE arg,QUOTE *rp) |
|
Line 1988 void Pquote_normalize(NODE arg,QUOTE *rp) |
|
if ( !q || OID(q) != O_QUOTE ) { |
if ( !q || OID(q) != O_QUOTE ) { |
*rp = q; |
*rp = q; |
return; |
return; |
} else { |
} else if ( q->normalized && (q->expanded || !expand) ) |
|
*rp = q; |
|
else { |
f = fnode_normalize(BDY(q),expand); |
f = fnode_normalize(BDY(q),expand); |
MKQUOTE(r,f); |
MKQUOTE(r,f); |
|
r->normalized = 1; |
|
if ( expand ) r->expanded = 1; |
*rp = r; |
*rp = r; |
} |
} |
} |
} |
|
|
void Pquote_normalize_comp(NODE arg,Q *rp) |
void Pquote_normalize_comp(NODE arg,Q *rp) |
{ |
{ |
|
QUOTE q1,q2; |
FNODE f1,f2; |
FNODE f1,f2; |
int r; |
int r; |
|
|
f1 = BDY((QUOTE)ARG0(arg)); |
q1 = (QUOTE)ARG0(arg); f1 = (FNODE)BDY(q1); |
f2 = BDY((QUOTE)ARG1(arg)); |
q2 = (QUOTE)ARG1(arg); f2 = (FNODE)BDY(q2); |
f1 = fnode_normalize(f1,0); |
if ( !q1->normalized ) f1 = fnode_normalize(f1,0); |
f2 = fnode_normalize(f2,0); |
if ( !q2->normalized ) f2 = fnode_normalize(f2,0); |
r = fnode_normalize_comp(f1,f2); |
r = fnode_normalize_comp(f1,f2); |
STOQ(r,*rp); |
STOQ(r,*rp); |
} |
} |
|
|
int fnode_is_number(FNODE f) |
INLINE int fnode_is_number(FNODE f) |
{ |
{ |
Obj obj; |
Obj obj; |
|
|
Line 2125 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2176 FNODE fnode_normalize(FNODE f,int expand) |
|
NODE n; |
NODE n; |
Q q; |
Q q; |
|
|
|
if ( f->normalized && (f->expanded || !expand) ) return f; |
STOQ(-1,q); |
STOQ(-1,q); |
mone = mkfnode(1,I_FORMULA,q); |
mone = mkfnode(1,I_FORMULA,q); |
switch ( f->id ) { |
switch ( f->id ) { |
case I_PAREN: |
case I_PAREN: |
return fnode_normalize(FA0(f),expand); |
r = fnode_normalize(FA0(f),expand); |
|
break; |
|
|
case I_MINUS: |
case I_MINUS: |
return fnode_normalize_mul_coef((Num)q, |
r = fnode_normalize_mul_coef((Num)q, |
fnode_normalize(FA0(f),expand),expand); |
fnode_normalize(FA0(f),expand),expand); |
|
break; |
|
|
case I_BOP: |
case I_BOP: |
/* arf fnode fnode */ |
/* arf fnode fnode */ |
Line 2141 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2195 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 '+': |
return fnode_normalize_add(a1,a2,expand); |
r = fnode_normalize_add(a1,a2,expand); |
|
break; |
case '-': |
case '-': |
a2 = fnode_normalize_mul_coef((Num)q,a2,expand); |
a2 = fnode_normalize_mul_coef((Num)q,a2,expand); |
return fnode_normalize_add(a1,a2,expand); |
r = fnode_normalize_add(a1,a2,expand); |
|
break; |
case '*': |
case '*': |
return fnode_normalize_mul(a1,a2,expand); |
r = fnode_normalize_mul(a1,a2,expand); |
|
break; |
case '/': |
case '/': |
a2 = fnode_normalize_pwr(a2,mone,expand); |
a2 = fnode_normalize_pwr(a2,mone,expand); |
return fnode_normalize_mul(a1,a2,expand); |
r = fnode_normalize_mul(a1,a2,expand); |
|
break; |
case '^': |
case '^': |
return fnode_normalize_pwr(a1,a2,expand); |
r = fnode_normalize_pwr(a1,a2,expand); |
|
break; |
default: |
default: |
return mkfnode(3,I_BOP,FA0(f),a1,a2); |
r = mkfnode(3,I_BOP,FA0(f),a1,a2); |
|
break; |
} |
} |
break; |
break; |
|
|
Line 2166 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2226 FNODE fnode_normalize(FNODE f,int expand) |
|
a1 = fnode_normalize(BDY(n),expand); |
a1 = fnode_normalize(BDY(n),expand); |
r = fnode_normalize_add(r,a1,expand); |
r = fnode_normalize_add(r,a1,expand); |
} |
} |
return r; |
break; |
case '*': |
case '*': |
n = (NODE)FA1(f); |
n = (NODE)FA1(f); |
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
r = fnode_normalize(BDY(n),expand); n = NEXT(n); |
Line 2174 FNODE fnode_normalize(FNODE f,int expand) |
|
Line 2234 FNODE fnode_normalize(FNODE f,int expand) |
|
a1 = fnode_normalize(BDY(n),expand); |
a1 = fnode_normalize(BDY(n),expand); |
r = fnode_normalize_mul(r,a1,expand); |
r = fnode_normalize_mul(r,a1,expand); |
} |
} |
return r; |
break; |
default: |
default: |
error("fnode_normallize : cannot happen"); |
error("fnode_normallize : cannot happen"); |
} |
} |
|
break; |
|
|
default: |
default: |
return fnode_apply(f,fnode_normalize,expand); |
return fnode_apply(f,fnode_normalize,expand); |
} |
} |
|
r->normalized = 1; |
|
r->expanded = expand; |
|
return r; |
} |
} |
|
|
FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand) |
FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand) |
Line 2334 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
Line 2398 FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand |
|
|
|
FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand) |
FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand) |
{ |
{ |
FNODE b,b1,e1,e,cc,r; |
FNODE b,b1,e1,e,cc,r,mf2,mone,inv; |
Num c,c1; |
Num c,c1,nf2; |
NODE arg,n; |
int ee; |
|
NODE arg,n,t0,t1; |
Q q; |
Q q; |
|
|
if ( IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,ONE); |
if ( IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,ONE); |
Line 2355 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
Line 2420 FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand |
|
return b1; |
return b1; |
else |
else |
return mkfnode(3,I_BOP,FA0(f1),b1,e); |
return mkfnode(3,I_BOP,FA0(f1),b1,e); |
} else if ( IS_NARYMUL(f1) && fnode_is_integer(f2) ) { |
} else if ( expand && IS_NARYMUL(f1) && fnode_is_integer(f2) ) { |
fnode_coef_body(f1,&c1,&b1); |
fnode_coef_body(f1,&c1,&b1); |
pwrnum(0,(Num)c1,(Num)eval(f2),&c); |
nf2 = (Num)eval(f2); |
|
pwrnum(0,(Num)c1,nf2,&c); |
|
ee = QTOS((Q)nf2); |
cc = mkfnode(1,I_FORMULA,c); |
cc = mkfnode(1,I_FORMULA,c); |
b = fnode_normalize_pwr(b1,f2,expand); |
if ( fnode_is_nonnegative_integer(f2) ) |
|
b = fnode_expand_pwr(b1,ee); |
|
else { |
|
STOQ(-1,q); |
|
mone = mkfnode(1,I_FORMULA,q); |
|
for ( t0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) { |
|
inv = mkfnode(3,I_BOP,pwrfs,BDY(n),mone); |
|
MKNODE(t1,inv,t0); t0 = t1; |
|
} |
|
b1 = mkfnode(2,I_NARYOP,FA0(f1),t0); |
|
b = fnode_expand_pwr(b1,-ee); |
|
} |
if ( fnode_is_one(cc) ) |
if ( fnode_is_one(cc) ) |
return b; |
return b; |
else |
else |
Line 2524 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
Line 2602 int fnode_normalize_comp(FNODE f1,FNODE f2) |
|
if ( fnode_is_number(e1) && fnode_is_number(e2) ) { |
if ( fnode_is_number(e1) && fnode_is_number(e2) ) { |
/* f1 = t b^e1 ... , f2 = t b^e2 ... */ |
/* f1 = t b^e1 ... , f2 = t b^e2 ... */ |
subnum(0,eval(e1),eval(e2),&ee); |
subnum(0,eval(e1),eval(e2),&ee); |
if ( ee ) { |
r = compnum(0,ee,0); |
|
if ( r > 0 ) { |
g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee)); |
g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee)); |
MKNODE(n1,g,n1); |
MKNODE(n1,g,n1); |
|
} else if ( r < 0 ) { |
|
chsgnnum(ee,&ee1); |
|
g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee1)); |
|
MKNODE(n2,g,n2); |
} |
} |
} else { |
} else { |
r = fnode_normalize_comp(e1,e2); |
r = fnode_normalize_comp(e1,e2); |
Line 2607 int fnode_normalize_comp_pwr(FNODE f1,FNODE f2) |
|
Line 2690 int fnode_normalize_comp_pwr(FNODE f1,FNODE f2) |
|
else if ( r < 0 ) |
else if ( r < 0 ) |
return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2); |
return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2); |
} else return fnode_normalize_comp(e1,e2); |
} else return fnode_normalize_comp(e1,e2); |
|
} |
|
|
|
NODE append_node(NODE a1,NODE a2) |
|
{ |
|
NODE t,t0; |
|
|
|
if ( !a1 ) |
|
return a2; |
|
else { |
|
for ( t0 = 0; a1; a1 = NEXT(a1) ) { |
|
NEXTNODE(t0,t); BDY(t) = BDY(a1); |
|
} |
|
NEXT(t) = a2; |
|
return t0; |
|
} |
|
} |
|
|
|
int fnode_normalize_match(FNODE f,FNODE pat,NODE *rp) |
|
{ |
|
NODE m,m1,m2,base,exp,fa,pa,n; |
|
LIST l; |
|
QUOTE qp,qf; |
|
FNODE fbase,fexp,a; |
|
FUNC ff,pf; |
|
int r; |
|
|
|
switch ( pat->id ) { |
|
case I_PVAR: |
|
/* [[pat,f]] */ |
|
*rp = mknode(1,mknode(2,(int)FA0(pat),f),0); |
|
return 1; |
|
|
|
case I_FORMULA: |
|
if ( !arf_comp(CO,(Obj)FA0(f),(Obj)FA0(pat)) ) { |
|
*rp = 0; return 1; |
|
} else |
|
return 0; |
|
|
|
case I_BOP: |
|
/* OPNAME should be "^" */ |
|
if ( !IS_BINARYPWR(pat) ) |
|
error("fnode_normalize_match : invalid BOP"); |
|
if ( IS_BINARYPWR(f) ) { |
|
fbase = FA1(f); fexp = FA2(f); |
|
} else { |
|
fbase = f; fexp = mkfnode(1,I_FORMULA,ONE); |
|
} |
|
if ( !fnode_normalize_match(fbase,FA1(pat),&base) ) return 0; |
|
a = rewrite_fnode(FA2(pat),base); |
|
if ( !fnode_normalize_match(fexp,a,&exp) ) return 0; |
|
else { |
|
*rp = append_node(base,exp); |
|
return 1; |
|
} |
|
break; |
|
|
|
case I_FUNC: |
|
if ( f->id != I_FUNC ) return 0; |
|
ff = (FUNC)FA0(f); pf = (FUNC)FA0(pat); |
|
if ( strcmp(ff->fullname,pf->fullname) ) return 0; |
|
/* FA1(f) and FA1(pat) are I_LIST */ |
|
fa = (NODE)FA0((FNODE)FA1(f)); |
|
pa = (NODE)FA0((FNODE)FA1(pat)); |
|
m = 0; |
|
while ( fa && pa ) { |
|
a = rewrite_fnode(BDY(pa),m); |
|
if ( !fnode_normalize_match(BDY(fa),a,&m1) ) return 0; |
|
m = append_node(m1,m); |
|
fa = NEXT(fa); pa = NEXT(pa); |
|
} |
|
if ( fa || pa ) return 0; |
|
else { |
|
*rp = m; |
|
return 1; |
|
} |
|
|
|
case I_NARYOP: |
|
if ( IS_NARYADD(pat) ) |
|
return fnode_normalize_match_naryadd(f,pat,rp); |
|
else if ( IS_NARYMUL(pat) ) |
|
return fnode_normalize_match_narymul(f,pat,rp); |
|
else |
|
error("fnode_normalize_match : invalid NARYOP"); |
|
break; |
|
|
|
default: |
|
error("fnode_normalize_match : invalid pattern"); |
|
} |
|
} |
|
|
|
/* remove i-th element */ |
|
|
|
FNODE fnode_removeith_naryadd(FNODE p,int i) |
|
{ |
|
int k,l; |
|
NODE t,r0,r,a; |
|
|
|
a = (NODE)FA1(p); |
|
l = length(a); |
|
if ( i < 0 || i >= l ) error("fnode_removeith_naryadd: invalid index"); |
|
else if ( i == 0 ) |
|
return fnode_node_to_naryadd(NEXT(a)); |
|
else { |
|
for ( r0 = 0, k = 0, t = a; k < i; k++, t = NEXT(t) ) { |
|
NEXTNODE(r0,r); |
|
BDY(r) = BDY(t); |
|
} |
|
t = NEXT(t); |
|
NEXT(r) = 0; |
|
return fnode_node_to_naryadd(r0); |
|
} |
|
|
|
} |
|
|
|
/* a0,...,a(i-1) */ |
|
FNODE fnode_left_narymul(FNODE p,int i) |
|
{ |
|
int k,l; |
|
NODE t,r0,r,a; |
|
|
|
a = (NODE)FA1(p); |
|
l = length(a); |
|
if ( i < 0 || i >= l ) error("fnode_left_narymul : invalid index"); |
|
if ( i == 0 ) return mkfnode(1,I_FORMULA,ONE); |
|
else if ( i == 1 ) return (FNODE)BDY(a); |
|
else { |
|
for ( r0 = 0, k = 0, t = a; k < i; k++, t = NEXT(t) ) { |
|
NEXTNODE(r0,r); |
|
BDY(r) = BDY(t); |
|
} |
|
NEXT(r) = 0; |
|
return fnode_node_to_narymul(r0); |
|
} |
|
} |
|
|
|
/* a(i+1),...,a(l-1) */ |
|
FNODE fnode_right_narymul(FNODE p,int i) |
|
{ |
|
NODE a,t; |
|
int l,k; |
|
|
|
a = (NODE)FA1(p); |
|
l = length(a); |
|
if ( i < 0 || i >= l ) error("fnode_right_narymul : invalid index"); |
|
if ( i == l-1 ) return mkfnode(1,I_FORMULA,ONE); |
|
else { |
|
for ( k = 0, t = a; k <= i; k++, t = NEXT(t) ); |
|
return fnode_node_to_narymul(t); |
|
} |
|
} |
|
|
|
int fnode_normalize_match_naryadd(FNODE f,FNODE p,NODE *rp) |
|
{ |
|
int fl,pl,fi,pi; |
|
NODE fa,pa,t,s,m,m1; |
|
FNODE fr,pr,prr,pivot; |
|
|
|
f = to_naryadd(f); |
|
fa = (NODE)FA1(f); fl = length(fa); |
|
pa = (NODE)FA1(p); pl = length(pa); |
|
if ( fl < pl ) return 0; |
|
else if ( pl == 1 ) { |
|
if ( fl == 1 ) |
|
return fnode_normalize_match(BDY(fa),BDY(pa),rp); |
|
else |
|
return 0; |
|
} else { |
|
for ( t = pa, pi = 0; t; t = NEXT(t), pi++ ) |
|
if ( ((FNODE)BDY(t))->id != I_PVAR ) break; |
|
if ( !t ) { |
|
/* all are I_PVAR */ |
|
m = 0; |
|
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
|
fnode_normalize_match(BDY(s),BDY(t),&m1); |
|
m = append_node(m1,m); |
|
} |
|
if ( !NEXT(s) ) |
|
fr = (FNODE)BDY(s); |
|
else |
|
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
|
fnode_normalize_match(fr,BDY(t),&m1); |
|
*rp = append_node(m1,m); |
|
return 1; |
|
} else { |
|
pivot = (FNODE)BDY(t); |
|
pr = fnode_removeith_naryadd(p,pi); |
|
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
|
if ( fnode_normalize_match(BDY(s),pivot,&m) ) { |
|
fr = fnode_removeith_naryadd(f,fi); |
|
prr = rewrite_fnode(pr,m); |
|
if ( fnode_normalize_match(fr,prr,&m1) ) { |
|
*rp = append_node(m,m1); |
|
return 1; |
|
} |
|
} |
|
} |
|
return 0; |
|
} |
|
} |
|
} |
|
|
|
int fnode_normalize_match_narymul(FNODE f,FNODE p,NODE *rp) |
|
{ |
|
int fl,pl,fi,pi; |
|
NODE fa,pa,t,s,m,m1; |
|
FNODE fr,pr,pleft,pleft1,pright,pright1,fleft,fright,pivot; |
|
|
|
f = to_narymul(f); |
|
fa = (NODE)FA1(f); fl = length(fa); |
|
pa = (NODE)FA1(p); pl = length(pa); |
|
if ( fl < pl ) return 0; |
|
else if ( pl == 1 ) { |
|
if ( fl == 1 ) |
|
return fnode_normalize_match(BDY(fa),BDY(pa),rp); |
|
else |
|
return 0; |
|
} else { |
|
for ( t = pa, pi = 0; t; t = NEXT(t), pi++ ) |
|
if ( ((FNODE)BDY(t))->id != I_PVAR ) break; |
|
if ( !t ) { |
|
/* all are I_PVAR */ |
|
m = 0; |
|
for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) { |
|
pr = rewrite_fnode(BDY(t),m); |
|
if ( !fnode_normalize_match(BDY(s),pr,&m1) ) return 0; |
|
m = append_node(m1,m); |
|
} |
|
if ( !NEXT(s) ) |
|
fr = (FNODE)BDY(s); |
|
else |
|
fr = mkfnode(2,I_NARYOP,FA0(f),s); |
|
pr = rewrite_fnode(BDY(t),m); |
|
if ( !fnode_normalize_match(fr,pr,&m1) ) return 0; |
|
*rp = append_node(m1,m); |
|
return 1; |
|
} else { |
|
pivot = (FNODE)BDY(t); |
|
pleft = fnode_left_narymul(p,pi); |
|
pright = fnode_right_narymul(p,pi); |
|
/* XXX : incomplete */ |
|
for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) { |
|
if ( fnode_normalize_match(BDY(s),pivot,&m) ) { |
|
fleft = fnode_left_narymul(f,fi); |
|
pleft1 = rewrite_fnode(pleft,m); |
|
if ( fnode_normalize_match(fleft,pleft1,&m1) ) { |
|
m = append_node(m1,m); |
|
fright = fnode_right_narymul(f,fi); |
|
pright1 = rewrite_fnode(pright,m); |
|
if ( fnode_normalize_match(fright,pright1,&m1) ) { |
|
*rp = append_node(m1,m); |
|
return 1; |
|
} |
|
} |
|
} |
|
} |
|
return 0; |
|
} |
|
} |
} |
} |