Annotation of OpenXM/src/asir-contrib/testing/test1-tr.rr, Revision 1.8
1.8 ! takayama 1: /* $Id: test1-tr.rr,v 1.19 2005/05/04 05:48:08 taka Exp $ */
! 2: /* $OpenXM: OpenXM/src/asir-contrib/testing/test1-tr.rr,v 1.7 2005/05/04 05:47:03 takayama Exp $ */
1.2 takayama 3:
4: load("tr.rr")$
5:
1.7 takayama 6: /*
7: qt_ ==> qt.
8: tr_ ==> tr.
9: */
1.2 takayama 10:
11: def test0() {
12: A = quotetolist(quote(1+sin(x)+sin(3*@pi)*sin(0)));
13: P = quotetolist(quote(sin(pn("x")*@pi)));
1.7 takayama 14: Q = ["qt.sin_int","x"];
1.2 takayama 15: print(A);
16: print(P);
17: print(Q);
18: print("----------------");
1.7 takayama 19: print(tr.match0(A,P));
1.2 takayama 20: A2 = quotetolist(quote(sin(2*@pi)));
1.7 takayama 21: print(tr.match0(A2,P));
1.2 takayama 22: print("----------------");
1.7 takayama 23: print("---- tr.make_binding --------");
24: print(tr.make_binding(A2,P));
25: print("-----tr.rp -------------");
26: R=tr.rp(A,P,Q);
1.2 takayama 27: print("--------------------");
28: print(R);
29: print("--------------------");
30: return quote_input_form_quote_list(R);
31: }
32:
33: def test1() {
1.7 takayama 34: Rule1=[quote(sin(pn("x")*@pi)),["qt.sin_int","x"]]; /* sin($B@0?t(B*@pi) --> 0 */
35: Rule2=[quote(0*pn("y")), ["qt.zero"]]; /* 0*any --> 0 */
36: Rule3=[quote(pn("y")*0), ["qt.zero"]]; /* any*0 --> 0 */
37: Rule4=[quote(pn("y")+0), ["qt.id","y"]]; /* any+0 --> any */
38: Rule5=[quote(0+pn("y")), ["qt.id","y"]]; /* 0+any --> any */
39: Rule6=[quote(sin(0)), ["qt.zero"]]; /* sin(0) --> 0 */
1.2 takayama 40: R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
41: print(print_input_form(R0));
1.7 takayama 42: R=tr.apply_rule1(R0,Rule1[0],Rule1[1]);
1.2 takayama 43: print(print_input_form(R));
1.7 takayama 44: R=tr.apply_rule1(R,Rule2[0],Rule2[1]);
1.2 takayama 45: print(print_input_form(R));
1.7 takayama 46: R=tr.apply_rule1(R,Rule4[0],Rule4[1]);
1.2 takayama 47: print(print_input_form(R));
1.7 takayama 48: R=tr.apply_rule1(R,Rule6[0],Rule6[1]);
1.2 takayama 49: print(print_input_form(R));
1.7 takayama 50: R=tr.apply_rule1(R,Rule4[0],Rule4[1]);
1.2 takayama 51: print(print_input_form(R));
52: return R;
53: }
54:
55:
56: /* $BITDj@QJ,7W;;$NNc(B
57: c x^n $B$NOB$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
58: $B$$$m$$$m(B $BLdBjE@$"$j(B: $B$?$H$($P(B c $B$,(B $BL5$$$H$-$N=hM}$G$-$:(B.
59: */
60:
61: /* $B1&JU4X?t(B. c x^n $B$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
62: Todo: $B1&JU4X?t$rMF0W$K=q$/J}K!(B.
63: */
64: def r_integral0(C,N) {
65: NN = eval_str(quote_input_form_quote_list(quotetolist(N)));
66: CC = quote_input_form_quote_list(quotetolist(C));
67: if (NN == -1) {
68: R = "quote("+CC+"*log(x))";
69: }else{
70: R = "quote("+CC+"/"+rtostr(NN+1)+"*x^"+rtostr(NN+1)+")";
71: }
72: print("r_integral0:",0);print(R);
73: R = eval_str(R);
74: return quotetolist(R);
75: }
76: /* $B1&JU4X?t(B $B@QJ,$N@~7?@-(B */
77: def r_int_linear(F,G) {
78: FF = quote_input_form_quote_list(quotetolist(F));
79: GG = quote_input_form_quote_list(quotetolist(G));
80: R = "quote(integral("+FF+")+integral("+GG+"))";
81: print("r_int_linear:",0);print(R);
82: R = eval_str(R);
83: return quotetolist(R);
84: }
85: def test3() {
86: R0 = quote(1+integral(2*x^(-1)+2*x^2));
87: return test3a(R0);
88: }
89: def test3a(R0) {
90: Rules=[
91: /* c*x^n --> (c/(n+1))*x^(n+1) or c*log(x) */
92: [quote(integral(pn("c")*x^pn("n"))),["r_integral0","c","n"]],
93: [quote(integral(pn("f")+pn("g"))), ["r_int_linear","f","g"]]
94: ];
95: print("Input=",0); print(print_input_form(R0));
96: N = length(Rules);
97: R = R0;
98: for (J=0; J<3; J++) { /* Todo: $B%U%i%0$,$J$$$N$G(B, $B$H$j$"$($:(B 3 $B2s(B */
99: for (I=0; I<N; I++) {
100: print(print_input_form(R));
1.7 takayama 101: R=tr.apply_rule1(R,Rules[I][0],Rules[I][1]);
1.2 takayama 102: }
103: }
1.3 takayama 104: return R;
105: }
106:
107:
108: /* $B4X?t$N%^%C%A(B. N[] $BAjEv(B. test4(). */
1.7 takayama 109: /* quote(nn(pn(f),qt.is_function(f))); $B$OITMW(B. qt.map_arg $B$,=hM}(B */
1.3 takayama 110: def test4() {
1.7 takayama 111: Rule=[quote(nn(pn(f))),["qt.map_arg",nn,f]];
1.3 takayama 112: R0 = quote(nn(sin(1/2)*cos(1/3)));
113: print(print_input_form(R0));
1.7 takayama 114: R=tr.apply_rule1(R0,Rule[0],Rule[1]);
1.3 takayama 115: return R;
116: }
117:
118: def test5() {
1.7 takayama 119: Rule1=[quote(sin(pn(x)*@pi)),["qt.sin_int",x]]; /* sin($B@0?t(B*@pi) --> 0 */
120: Rule2=[quote(0*pn(y)), ["qt.zero"]]; /* 0*any --> 0 */
121: Rule3=[quote(pn(y)*0), ["qt.zero"]]; /* any*0 --> 0 */
122: Rule4=[quote(pn(y)+0), ["qt.id",y]]; /* any+0 --> any */
123: Rule5=[quote(0+pn(y)), ["qt.id",y]]; /* 0+any --> any */
124: Rule6=[quote(sin(0)), ["qt.zero"]]; /* sin(0) --> 0 */
1.3 takayama 125: R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
126: print(print_input_form(R0));
1.7 takayama 127: R=tr.apply_rule1_flag(R0,Rule1[0],Rule1[1]);
1.3 takayama 128: print([R[0],print_input_form(R[1])]);
1.7 takayama 129: R=tr.apply_or_rules(R0,[Rule1,Rule2,Rule3,Rule4,Rule5,Rule6]);
1.6 takayama 130: return R;
131: }
132:
1.2 takayama 133:
1.5 takayama 134: /* $BHyJ,4D$N7W;;(B */
135: /* x $B$K0MB8$7$F$k$+(B? u, u_0, u_1, u_2, ... $B$O(B x $B$K0MB8$7$F$k(B.*/
136: def to_quote(L) {
137: return eval_str("quote("+listtoquote_str(L)+")");
138: }
139: def dep6(Q) {
140: if (type(Q) == 4) {
141: Q = to_quote(Q);
142: }
1.7 takayama 143: if (qt.is_dependent(Q,x)) return 1;
144: if (qt.is_dependent(Q,u)) return 1;
1.5 takayama 145: /* $B$H$j$"$($:(B 10 $B<!$^$G$N(B f. --> $B$J$s$H$+$;$h(B. */
146: for (I=0; I<10; I++) {
1.7 takayama 147: if (qt.is_dependent(Q,idxtov(u,I))) return 1;
1.5 takayama 148: }
149: return 0;
150: }
151: def diff_lin(F,G) {
152: if (type(F) == 4) F=to_quote(F);
153: if (type(G) == 4) G=to_quote(G);
1.7 takayama 154: return qt.replace(quote(diff(f)+diff(g)),[[f,F],[g,G]]);
1.5 takayama 155: }
156: def diff_mul(F,G) {
157: F1 = dep6(F); G1 = dep6(G);
158: if (type(F) == 4) F=to_quote(F);
159: if (type(G) == 4) G=to_quote(G);
160: if (F1 && G1)
1.7 takayama 161: return qt.replace(quote(diff(f)*g+f*diff(g)),[[f,F],[g,G]]);
1.5 takayama 162: if ((F1 == 1) && (G1 == 0))
1.7 takayama 163: return qt.replace(quote(diff(f)*g),[[f,F],[g,G]]);
1.5 takayama 164: if ((F1 == 0) && (G1 == 1))
1.7 takayama 165: return qt.replace(quote(f*diff(g)),[[f,F],[g,G]]);
1.5 takayama 166: if ((F1 == 0) && (G1 == 0))
1.7 takayama 167: return qt.zero();
1.5 takayama 168: }
169: def diff_x_n(N) {
170: N = eval_quote(N);
171: N1=N-1;
1.7 takayama 172: if (N1 == 0) return qt.one();
1.5 takayama 173: if (N1 == 1) return quote(2*x);
174: if (N1 > 1) return eval_str("quote("+rtostr(N)+"*x^"+rtostr(N1)+")");
175: }
176: /* F $B$,(B u $B$H$+(B u_0, u_1, ... $B$J$i(B 1 $B$rLa$9(B. */
177: /* debug $BMQ$NF~NO(B.
1.7 takayama 178: tr.check_pn(quote(u_1),quote(pn(x,is_u_variable(x))));
1.5 takayama 179: */
180: def is_u_variable(F) {
181: /* $B=R8l$NA0$N(B check point $B$b(B debugger $B$KM_$7$$(B. */
182: /* print("is_u_variable: ",0); print(print_input_form(F)); */
183: if (type(F) == 17) F=quotetolist(F);
184: if (rtostr(F[0]) == "internal") {
185: V = eval_str(rtostr(F[1]));
186: if (vtoidx(V)[0] == "u") return 1;
187: }
188: return 0;
189: }
190: /* u_i^n $B$NHyJ,$r$9$k(B. n*u_{i+1}*u_i^{n-1}
191: Todo: $B$b$C$H4J7i$K(B quote $B$r=q$1$J$$$+(B?
192: */
193: def diff_u_n(F,N) {
194: F = eval_quote(F);
195: I = vtoidx(F);
196: if (length(I) == 1) I = 0; else I=I[1];
197: N = eval_quote(N);
198: N1=N-1;
199: NextU = "u_"+rtostr(I+1);
200: if (I == 0) U = "u"; else U = "u_"+rtostr(I);
201:
202: NN = objtoquote(N);
203: NN1 = objtoquote(N1);
204: NextU = objtoquote(eval_str(NextU));
205: U = objtoquote(eval_str(U));
206:
207: if (N1 == 0) return NextU;
1.7 takayama 208: if (N1 == 1) return qt.replace(quote(2*up*uu),[[up,NextU],[uu,U]]);
209: if (N1 > 1) return qt.replace(quote(n*up*uu^m),[[up,NextU],[uu,U],
1.5 takayama 210: [n,NN],[m,NN1]]);
211: }
212:
213: def test6b() {
1.7 takayama 214: T1=[quote(diff(x)),["qt.one"]];
1.5 takayama 215: T2=[quote(diff(x^pn(n))),[diff_x_n,n]]; /* is_poly? $B$,M_$7$$(B. */
216: R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
217: R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];
218:
219: A = quote(diff(2*4*x^3+x));
220: print(print_input_form(A));
1.7 takayama 221: R=tr.apply_or_rules(A,[R1,R2,T1,T2]);
1.5 takayama 222: return R;
223: }
224:
225: /* Use Debug2=1; $B$O(B debug $B$K$H$F$bM-1W(B. */
226: def test6() {
1.7 takayama 227: T1=[quote(diff(x)),["qt.one"]];
1.5 takayama 228: T2=[quote(diff(x^pn(n))),[diff_x_n,n]]; /* is_poly? $B$,M_$7$$(B. */
229: T3=[quote(diff(pn(f,is_u_variable(f))^pn(n))),[diff_u_n,f,n]];
230: R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
231: R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];
232:
233: /* A = quote(diff(2*x^3+x));*/
234: A = quote(diff(2*u^3+x));
235: print(print_input_form(A));
1.7 takayama 236: R=tr.apply_or_rules(A,[R1,R2,T1,T2,T3]);
1.5 takayama 237: return R;
238: }
1.2 takayama 239: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>