version 1.108, 2005/12/11 07:21:43 |
version 1.110, 2005/12/14 09:06:54 |
|
|
* 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.107 2005/12/11 05:27:30 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.109 2005/12/14 06:07:30 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
Line 94 void Pflatten_quote(); |
|
Line 94 void Pflatten_quote(); |
|
|
|
void Pqt_is_integer(),Pqt_is_rational(),Pqt_is_number(),Pqt_is_coef(); |
void Pqt_is_integer(),Pqt_is_rational(),Pqt_is_number(),Pqt_is_coef(); |
void Pqt_is_dependent(),Pqt_is_function(),Pqt_is_var(); |
void Pqt_is_dependent(),Pqt_is_function(),Pqt_is_var(); |
void Pqt_set_ord(),Pqt_set_coef(); |
void Pqt_set_ord(),Pqt_set_coef(),Pqt_set_weight(); |
void Pqt_normalize(); |
void Pqt_normalize(); |
void Pnqt_comp(); |
void Pnqt_comp(); |
void Pnqt_match(); |
void Pnqt_match(); |
Line 165 struct ftab str_tab[] = { |
|
Line 165 struct ftab str_tab[] = { |
|
|
|
{"qt_set_coef",Pqt_set_coef,-1}, |
{"qt_set_coef",Pqt_set_coef,-1}, |
{"qt_set_ord",Pqt_set_ord,-1}, |
{"qt_set_ord",Pqt_set_ord,-1}, |
|
{"qt_set_weight",Pqt_set_weight,-1}, |
{"qt_normalize",Pqt_normalize,-2}, |
{"qt_normalize",Pqt_normalize,-2}, |
{"qt_match",Pqt_match,2}, |
{"qt_match",Pqt_match,2}, |
{"nqt_match_rewrite",Pnqt_match_rewrite,3}, |
{"nqt_match_rewrite",Pnqt_match_rewrite,3}, |
Line 2166 VL reordvars(VL vl0,NODE head) |
|
Line 2167 VL reordvars(VL vl0,NODE head) |
|
return vl; |
return vl; |
} |
} |
|
|
|
struct wtab { |
|
V v; |
|
int w; |
|
}; |
|
|
|
struct wtab *qt_weight_tab; |
VL qt_current_ord, qt_current_coef; |
VL qt_current_ord, qt_current_coef; |
LIST qt_current_ord_obj,qt_current_coef_obj; |
LIST qt_current_ord_obj,qt_current_coef_obj,qt_current_weight_obj; |
|
LIST qt_current_weight_obj; |
|
|
void Pqt_set_ord(NODE arg,LIST *rp) |
void Pqt_set_ord(NODE arg,LIST *rp) |
{ |
{ |
Line 2177 void Pqt_set_ord(NODE arg,LIST *rp) |
|
Line 2185 void Pqt_set_ord(NODE arg,LIST *rp) |
|
|
|
if ( !argc(arg) ) |
if ( !argc(arg) ) |
*rp = qt_current_ord_obj; |
*rp = qt_current_ord_obj; |
else { |
else if ( !ARG0(arg) ) { |
|
qt_current_ord_obj = 0; |
|
qt_current_ord = 0; |
|
} else { |
qt_current_ord = reordvars(CO,BDY((LIST)ARG0(arg))); |
qt_current_ord = reordvars(CO,BDY((LIST)ARG0(arg))); |
for ( r0 = 0, vl = qt_current_ord; vl; vl = NEXT(vl) ) { |
for ( r0 = 0, vl = qt_current_ord; vl; vl = NEXT(vl) ) { |
NEXTNODE(r0,r); MKV(vl->v,v); BDY(r) = v; |
NEXTNODE(r0,r); MKV(vl->v,v); BDY(r) = v; |
Line 2188 void Pqt_set_ord(NODE arg,LIST *rp) |
|
Line 2199 void Pqt_set_ord(NODE arg,LIST *rp) |
|
} |
} |
} |
} |
|
|
|
void Pqt_set_weight(NODE arg,LIST *rp) |
|
{ |
|
NODE n,pair; |
|
int l,i; |
|
struct wtab *tab; |
|
|
|
if ( !argc(arg) ) |
|
*rp = qt_current_weight_obj; |
|
else if ( !ARG0(arg) ) { |
|
qt_current_weight_obj = 0; |
|
qt_weight_tab = 0; |
|
} else { |
|
n = BDY((LIST)ARG0(arg)); |
|
l = length(n); |
|
tab = qt_weight_tab = (struct wtab *)MALLOC((l+1)*sizeof(struct wtab)); |
|
for ( i = 0; i < l; i++, n = NEXT(n) ) { |
|
pair = BDY((LIST)BDY(n)); |
|
tab[i].v = VR((P)ARG0(pair)); |
|
tab[i].w = QTOS((Q)ARG1(pair)); |
|
} |
|
tab[i].v = 0; |
|
qt_current_weight_obj = (LIST)ARG0(arg); |
|
*rp = qt_current_weight_obj; |
|
} |
|
} |
|
|
void Pqt_set_coef(NODE arg,LIST *rp) |
void Pqt_set_coef(NODE arg,LIST *rp) |
{ |
{ |
NODE r0,r,n; |
NODE r0,r,n; |
Line 2196 void Pqt_set_coef(NODE arg,LIST *rp) |
|
Line 2233 void Pqt_set_coef(NODE arg,LIST *rp) |
|
|
|
if ( !argc(arg) ) |
if ( !argc(arg) ) |
*rp = qt_current_coef_obj; |
*rp = qt_current_coef_obj; |
else { |
else if ( !ARG0(arg) ) { |
|
qt_current_coef_obj = 0; |
|
qt_current_coef = 0; |
|
} else { |
n = BDY((LIST)ARG0(arg)); |
n = BDY((LIST)ARG0(arg)); |
for ( vl0 = 0, r0 = 0; n; n = NEXT(n) ) { |
for ( vl0 = 0, r0 = 0; n; n = NEXT(n) ) { |
NEXTNODE(r0,r); |
NEXTNODE(r0,r); |
Line 2845 FNODE nfnode_mul(FNODE f1,FNODE f2,int expand) |
|
Line 2885 FNODE nfnode_mul(FNODE f1,FNODE f2,int expand) |
|
FNODE b1,b2,e1,e2,cc,t,t1; |
FNODE b1,b2,e1,e2,cc,t,t1; |
FNODE *m; |
FNODE *m; |
int s; |
int s; |
Obj c1,c2,c; |
Obj c1,c2,c,e; |
Num e; |
|
int l1,l,i,j; |
int l1,l,i,j; |
|
|
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); |
Line 2887 FNODE nfnode_mul(FNODE f1,FNODE f2,int expand) |
|
Line 2926 FNODE nfnode_mul(FNODE f1,FNODE f2,int expand) |
|
else { |
else { |
fnode_base_exp(m[i-1],&b1,&e1); fnode_base_exp(BDY(r),&b2,&e2); |
fnode_base_exp(m[i-1],&b1,&e1); fnode_base_exp(BDY(r),&b2,&e2); |
if ( compfnode(b1,b2) ) break; |
if ( compfnode(b1,b2) ) break; |
addnum(0,eval(e1),eval(e2),&e); |
arf_add(CO,eval(e1),eval(e2),&e); |
if ( !e ) i--; |
if ( !e ) i--; |
else if ( UNIQ(e) ) |
else if ( UNIQ(e) ) |
m[i-1] = b1; |
m[i-1] = b1; |
Line 3083 void fnode_coef_body(FNODE f,Obj *cp,FNODE *bp) |
|
Line 3122 void fnode_coef_body(FNODE f,Obj *cp,FNODE *bp) |
|
} |
} |
} |
} |
|
|
int nfnode_comp_pwr(FNODE f1,FNODE f2); |
int nfnode_weight(struct wtab *tab,FNODE f) |
|
{ |
|
NODE n; |
|
int w,w1; |
|
int i; |
|
Q a2; |
|
V v; |
|
|
|
switch ( f->id ) { |
|
case I_FORMULA: |
|
if ( fnode_is_coef(f) ) return 0; |
|
else if ( fnode_is_var(f) ) { |
|
v = VR((P)FA0(f)); |
|
for ( i = 0; tab[i].v; i++ ) |
|
if ( v == tab[i].v ) return tab[i].w; |
|
return w; |
|
} else return 0; |
|
|
|
/* XXX */ |
|
case I_PVAR: return 1; |
|
/* XXX */ |
|
case I_FUNC: I_FUNC: I_FUNC_QARG: |
|
/* w(f) = 1 */ |
|
/* w(f(a1,...,an)=w(a1)+...+w(an) */ |
|
n = FA0((FNODE)FA1(f)); |
|
for ( w = 0; n; n = NEXT(n) ) |
|
w += nfnode_weight(tab,BDY(n)); |
|
return w; |
|
case I_NARYOP: |
|
n = (NODE)FA1(f); |
|
if ( IS_NARYADD(f) ) |
|
for ( w = nfnode_weight(tab,BDY(n)), |
|
n = NEXT(n); n; n = NEXT(n) ) { |
|
w1 = nfnode_weight(tab,BDY(n)); |
|
w = MAX(w,w1); |
|
} |
|
else |
|
for ( w = 0; n; n = NEXT(n) ) |
|
w += nfnode_weight(tab,BDY(n)); |
|
return w; |
|
case I_BOP: |
|
/* must be binary power */ |
|
/* XXX w(2^x)=0 ? */ |
|
if ( fnode_is_rational(FA2(f)) ) { |
|
a2 = (Q)eval(FA2(f)); |
|
w = QTOS(a2); |
|
} else |
|
w = nfnode_weight(tab,FA2(f)); |
|
return nfnode_weight(tab,FA1(f))*w; |
|
default: |
|
error("nfnode_weight : not_implemented"); |
|
} |
|
} |
|
|
int nfnode_comp(FNODE f1,FNODE f2) |
int nfnode_comp(FNODE f1,FNODE f2) |
{ |
{ |
|
int w1,w2; |
|
|
|
if ( qt_weight_tab ) { |
|
w1 = nfnode_weight(qt_weight_tab,f1); |
|
w2 = nfnode_weight(qt_weight_tab,f2); |
|
if ( w1 > w2 ) return 1; |
|
if ( w1 < w2 ) return -1; |
|
} |
|
return nfnode_comp_lex(f1,f2); |
|
} |
|
|
|
int nfnode_comp_lex(FNODE f1,FNODE f2) |
|
{ |
NODE n1,n2; |
NODE n1,n2; |
int r,i1,i2,ret; |
int r,i1,i2,ret; |
char *nm1,*nm2; |
char *nm1,*nm2; |
FNODE b1,b2,e1,e2,g,a1,a2,fn1,fn2; |
FNODE b1,b2,e1,e2,g,a1,a2,fn1,fn2; |
Num ee,ee1; |
Num ee,ee1; |
Obj c1,c2; |
Obj c1,c2; |
|
int w1,w2; |
|
|
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 = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
if ( r = nfnode_comp_lex(BDY(n1),BDY(n2)) ) return r; |
else { |
else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
Line 3120 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3225 int nfnode_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 = nfnode_comp(b1,b2) ) { |
if ( r = nfnode_comp_lex(b1,b2) ) { |
if ( r > 0 ) |
if ( r > 0 ) |
return nfnode_comp(e1,mkfnode(1,I_FORMULA,0)); |
return nfnode_comp_lex(e1,mkfnode(1,I_FORMULA,0)); |
else if ( r < 0 ) |
else if ( r < 0 ) |
return nfnode_comp(mkfnode(1,I_FORMULA,0),e2); |
return nfnode_comp_lex(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 3140 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3245 int nfnode_comp(FNODE f1,FNODE f2) |
|
MKNODE(n2,g,n2); |
MKNODE(n2,g,n2); |
} |
} |
} else { |
} else { |
r = nfnode_comp(e1,e2); |
r = nfnode_comp_lex(e1,e2); |
if ( r > 0 ) return 1; |
if ( r > 0 ) return 1; |
else if ( r < 0 ) return -1; |
else if ( r < 0 ) return -1; |
} |
} |
} |
} |
} |
} |
} |
} |
if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) ) |
if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) ) { |
return nfnode_comp_pwr(f1,f2); |
fnode_base_exp(f1,&b1,&e1); |
|
fnode_base_exp(f2,&b2,&e2); |
|
if ( r = nfnode_comp_lex(b1,b2) ) { |
|
if ( r > 0 ) |
|
return nfnode_comp_lex(e1,mkfnode(1,I_FORMULA,0)); |
|
else if ( r < 0 ) |
|
return nfnode_comp_lex(mkfnode(1,I_FORMULA,0),e2); |
|
} else return nfnode_comp_lex(e1,e2); |
|
} |
|
|
/* now, IDs of f1 and f2 must be I_FORMULA, I_FUNC, I_IFUNC 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_FUNC_QARG > I_FORMULA */ |
/* I_IFUNC > I_PVAR > I_FUNC=I_FUNC_QARG > I_FORMULA */ |
Line 3160 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3273 int nfnode_comp(FNODE f1,FNODE f2) |
|
case I_FUNC: case I_IFUNC: case I_PVAR: |
case I_FUNC: case I_IFUNC: case I_PVAR: |
return -1; |
return -1; |
default: |
default: |
error("nfnode_comp : undefined"); |
error("nfnode_comp_lex : undefined"); |
} |
} |
break; |
break; |
case I_FUNC: case I_FUNC_QARG: |
case I_FUNC: case I_FUNC_QARG: |
Line 3178 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3291 int nfnode_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 = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
if ( r = nfnode_comp_lex(BDY(n1),BDY(n2)) ) return r; |
else { |
else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
Line 3186 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3299 int nfnode_comp(FNODE f1,FNODE f2) |
|
} |
} |
break; |
break; |
default: |
default: |
error("nfnode_comp : undefined"); |
error("nfnode_comp_lex : undefined"); |
} |
} |
case I_PVAR: |
case I_PVAR: |
switch ( f2->id ) { |
switch ( f2->id ) { |
Line 3200 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3313 int nfnode_comp(FNODE f1,FNODE f2) |
|
else if ( i1 < i2 ) return -1; |
else if ( i1 < i2 ) return -1; |
else return 0; |
else return 0; |
default: |
default: |
error("nfnode_comp : undefined"); |
error("nfnode_comp_lex : undefined"); |
} |
} |
break; |
break; |
case I_IFUNC: |
case I_IFUNC: |
Line 3216 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3329 int nfnode_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 = nfnode_comp(BDY(n1),BDY(n2)) ) return r; |
if ( r = nfnode_comp_lex(BDY(n1),BDY(n2)) ) return r; |
else { |
else { |
n1 = NEXT(n1); n2 = NEXT(n2); |
n1 = NEXT(n1); n2 = NEXT(n2); |
} |
} |
Line 3225 int nfnode_comp(FNODE f1,FNODE f2) |
|
Line 3338 int nfnode_comp(FNODE f1,FNODE f2) |
|
break; |
break; |
|
|
default: |
default: |
error("nfnode_comp : undefined"); |
error("nfnode_comp_lex : undefined"); |
} |
} |
break; |
break; |
default: |
default: |
error("nfnode_comp : undefined"); |
error("nfnode_comp_lex : undefined"); |
} |
} |
} |
|
|
|
int nfnode_comp_pwr(FNODE f1,FNODE f2) |
|
{ |
|
FNODE b1,b2,e1,e2; |
|
int r; |
|
|
|
fnode_base_exp(f1,&b1,&e1); |
|
fnode_base_exp(f2,&b2,&e2); |
|
if ( r = nfnode_comp(b1,b2) ) { |
|
if ( r > 0 ) |
|
return nfnode_comp(e1,mkfnode(1,I_FORMULA,0)); |
|
else if ( r < 0 ) |
|
return nfnode_comp(mkfnode(1,I_FORMULA,0),e2); |
|
} else return nfnode_comp(e1,e2); |
|
} |
} |
|
|
NODE append_node(NODE a1,NODE a2) |
NODE append_node(NODE a1,NODE a2) |