/* $OpenXM: OpenXM/src/asir-contrib/testing/noro/new_rewrite.rr,v 1.3 2006/03/12 00:27:20 noro Exp $ */ /* * test functions for rewriting by rules * * usage : qt_rewrite(Expr,Rules,0|1|2) 0 : do not expand, 1 : expand * */ /* commutativity */ #if 0 Rc0=[`X*Y,`!qt_is_number(X) && nqt_comp(X,Y)<0,`Y*X]$ #else Rc0=[`X*Y,`nqt_comp(Y*X,X*Y)>0,`Y*X]$ #endif Rcomm = [Rc0]$ /* alternativity */ Ra0=[`X*Y,`qt_is_var(X) && qt_is_var(Y) && nqt_comp(Y,X)>0,`-Y*X]$ Ra1=[`X^N,`eval_quote(N)>=2,`0]$ Ra2=[`X*X,`0]$ Ralt = [Ra0,Ra1,Ra2]$ /* simplifier of exp() */ Re1=[`exp(X)*exp(Y),`exp(X+Y)]$ Re2=[`exp(X)^K,`exp(K*X)]$ Re3=[`exp(0),`1]$ Re4=[`exp(N*p*i),`qt_is_integer(N) && eval_quote(N)%2==0,`1]$ Rexp = [Re1,Re2,Re3,Re4]$ R5=[`(V^N)^M,`V^(N*M)]$ /* integration */ Ri1=[`int(F+G,X),`int(F,X)+int(G,X)]$ Ri2=[`int(N*F,X),`qt_is_number(N),`N*int(F,X)]$ Ri3=[`int(F,X),`qt_is_number(F), `F*X]$ Ri4=[`int(X^N,X),`qt_is_number(N) && eval_quote(N)!=-1,`X^(N+1)/(N+1)]$ Ri5=[`int(X^(-1),X),`log(X)]$ Ri6=[`int((A*X+B)^(-1),X),`1/A*log(A*X+B)]$ Rint = [Ri1,Ri2,Ri3,Ri4,Ri5,Ri6]$ /* derivation */ Rd0=[`d(N*X),`qt_is_number(N),`N*d(X)]$ Rd1=[`d(X+Y),`d(X)+d(Y)]$ Rd2=[`d(X*Y),`d(X)*Y+X*d(Y)]$ Rd3=[`d(N),`qt_is_number(N),`0]$ Rd=[Rd1,Rd2,Rd3]$ /* representing an expression as a polynomial w.r.t. x */ /* T = qt_rewrite(qt_rewrite(Expr,[Ru0],1),[Ru1],0) */ Ru0=[`x^N*X,`X*x^N]$ Ru1=[`F*x^N+G*x^N,`(F+G)*x^N]$ #define O_LIST 4 #define O_QUOTE 17 ctrl("print_quote",2)$ def normalize_rule(R,Expand) { return map(qt_normalize,R,Expand); } def qt_rewrite(F,Rules,Expand) { F = qt_normalize(F,Expand); Rules = map(normalize_rule,Rules,Expand); /* rewrite chidren */ F00 = F0 = F; while ( 1 ) { FA = quote_to_funargs(F); for ( R = [FA[0]], T = cdr(FA); T != []; T = cdr(T) ) { E = car(T); TE = type(E); if ( TE == O_QUOTE ) E1 = qt_rewrite(E,Rules,Expand); else if ( TE == O_LIST ) E1 = map(qt_rewrite,E,Rules,Expand); else E1 = E; R = cons(E1,R); } F = qt_normalize(funargs_to_quote(reverse(R)),Expand); if ( F == F0 ) break; else F0 = F; } F0 = F; while ( 1 ) { for ( T = Rules; T != []; T = cdr(T) ) { F1 = nqt_match_rewrite(F,car(T),Expand); if ( F1 != F ) { F = F1; break; } } if ( F == F0 ) break; else F0 = F; } if ( F00 == F ) return F; else return qt_normalize(qt_rewrite(F,Rules,Expand),Expand); } end$