Annotation of OpenXM/src/asir-contrib/testing/test1-tr.rr, Revision 1.5
1.5 ! takayama 1: /* $Id: test1-tr.rr,v 1.14 2005/04/08 05:00:17 taka Exp $ */
! 2: /* $OpenXM: OpenXM/src/asir-contrib/testing/test1-tr.rr,v 1.4 2005/04/06 09:26:28 takayama Exp $ */
1.2 takayama 3:
4: load("tr.rr")$
5:
6:
7:
8:
9: def test0() {
10: A = quotetolist(quote(1+sin(x)+sin(3*@pi)*sin(0)));
11: P = quotetolist(quote(sin(pn("x")*@pi)));
12: Q = ["qt_sin_int","x"];
13: print(A);
14: print(P);
15: print(Q);
16: print("----------------");
17: print(tr_match0(A,P));
18: A2 = quotetolist(quote(sin(2*@pi)));
19: print(tr_match0(A2,P));
20: print("----------------");
21: print("---- tr_make_binding --------");
22: print(tr_make_binding(A2,P));
23: print("-----tr_rp -------------");
24: R=tr_rp(A,P,Q);
25: print("--------------------");
26: print(R);
27: print("--------------------");
28: return quote_input_form_quote_list(R);
29: }
30:
31: def test1() {
32: Rule1=[quote(sin(pn("x")*@pi)),["qt_sin_int","x"]]; /* sin($B@0?t(B*@pi) --> 0 */
33: Rule2=[quote(0*pn("y")), ["qt_zero"]]; /* 0*any --> 0 */
34: Rule3=[quote(pn("y")*0), ["qt_zero"]]; /* any*0 --> 0 */
35: Rule4=[quote(pn("y")+0), ["qt_id","y"]]; /* any+0 --> any */
36: Rule5=[quote(0+pn("y")), ["qt_id","y"]]; /* 0+any --> any */
37: Rule6=[quote(sin(0)), ["qt_zero"]]; /* sin(0) --> 0 */
38: R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
39: print(print_input_form(R0));
40: R=tr_apply_rule1(R0,Rule1[0],Rule1[1]);
41: print(print_input_form(R));
42: R=tr_apply_rule1(R,Rule2[0],Rule2[1]);
43: print(print_input_form(R));
44: R=tr_apply_rule1(R,Rule4[0],Rule4[1]);
45: print(print_input_form(R));
46: R=tr_apply_rule1(R,Rule6[0],Rule6[1]);
47: print(print_input_form(R));
48: R=tr_apply_rule1(R,Rule4[0],Rule4[1]);
49: print(print_input_form(R));
50: return R;
51: }
52:
53:
54: /* $BITDj@QJ,7W;;$NNc(B
55: c x^n $B$NOB$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
56: $B$$$m$$$m(B $BLdBjE@$"$j(B: $B$?$H$($P(B c $B$,(B $BL5$$$H$-$N=hM}$G$-$:(B.
57: */
58:
59: /* $B1&JU4X?t(B. c x^n $B$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
60: Todo: $B1&JU4X?t$rMF0W$K=q$/J}K!(B.
61: */
62: def r_integral0(C,N) {
63: NN = eval_str(quote_input_form_quote_list(quotetolist(N)));
64: CC = quote_input_form_quote_list(quotetolist(C));
65: if (NN == -1) {
66: R = "quote("+CC+"*log(x))";
67: }else{
68: R = "quote("+CC+"/"+rtostr(NN+1)+"*x^"+rtostr(NN+1)+")";
69: }
70: print("r_integral0:",0);print(R);
71: R = eval_str(R);
72: return quotetolist(R);
73: }
74: /* $B1&JU4X?t(B $B@QJ,$N@~7?@-(B */
75: def r_int_linear(F,G) {
76: FF = quote_input_form_quote_list(quotetolist(F));
77: GG = quote_input_form_quote_list(quotetolist(G));
78: R = "quote(integral("+FF+")+integral("+GG+"))";
79: print("r_int_linear:",0);print(R);
80: R = eval_str(R);
81: return quotetolist(R);
82: }
83: def test3() {
84: R0 = quote(1+integral(2*x^(-1)+2*x^2));
85: return test3a(R0);
86: }
87: def test3a(R0) {
88: Rules=[
89: /* c*x^n --> (c/(n+1))*x^(n+1) or c*log(x) */
90: [quote(integral(pn("c")*x^pn("n"))),["r_integral0","c","n"]],
91: [quote(integral(pn("f")+pn("g"))), ["r_int_linear","f","g"]]
92: ];
93: print("Input=",0); print(print_input_form(R0));
94: N = length(Rules);
95: R = R0;
96: for (J=0; J<3; J++) { /* Todo: $B%U%i%0$,$J$$$N$G(B, $B$H$j$"$($:(B 3 $B2s(B */
97: for (I=0; I<N; I++) {
98: print(print_input_form(R));
99: R=tr_apply_rule1(R,Rules[I][0],Rules[I][1]);
100: }
101: }
1.3 takayama 102: return R;
103: }
104:
105:
106: /* $B4X?t$N%^%C%A(B. N[] $BAjEv(B. test4(). */
107: /* quote(nn(pn(f),qt_is_function(f))); $B$OITMW(B. qt_map_arg $B$,=hM}(B */
108: def test4() {
109: Rule=[quote(nn(pn(f))),[qt_map_arg,nn,f]];
110: R0 = quote(nn(sin(1/2)*cos(1/3)));
111: print(print_input_form(R0));
112: R=tr_apply_rule1(R0,Rule[0],Rule[1]);
113: return R;
114: }
115:
116: /* tr_apply_or_rule $B$N;n:n(B */
117:
118: /* Flag $BIU$-(B $B$N(B tr_rp. $BB0@-$,$J$$$N$G$3$l$G$d$k(B. */
119: def tr_rp_flag(F,P,Q) {
120: Flag = 0;
121: dprint0("tr_rp, F="); dprint(F);
122: dprint0("tr_rp, P="); dprint(P);
123: dprint0("tr_rp, Q="); dprint(Q);
124: if (tr_match0(F,P)) {
125: BindTable = tr_make_binding(F,P);
126: dprint0("BindTable="); dprint(BindTable);
127: return [1,tr_apply_function0(Q,BindTable)];
128: }
129: if (type(F) != 4) return F;
130: Node = qt_node(F);
131: N = qt_nchild(F);
132: Ans = Node;
133: for (I=0; I<N; I++) {
134: T = tr_rp_flag(qt_child(F,I),P,Q);
135: if (T[0] == 1) Flag = 1;
136: Ans = append(Ans,[T[1]]);
137: }
138: return [Flag,Ans];
139: }
140:
1.5 ! takayama 141: extern Debug2$
! 142: Debug2=0$
1.3 takayama 143: /* $B=q$-49$((B flag $BIU$-$N(B tr_apply_rule_flag */
144: def tr_apply_rule1_flag(Obj,L,R) {
145: Flag = 0;
1.5 ! takayama 146: if (Debug2)
! 147: print("-------- start of tr_apply_rule1_flag ------------ ");
! 148: if (Debug2) print(print_input_form(Obj));
1.3 takayama 149: Obj = quotetolist(Obj);
150: L = quotetolist(L);
151: R = tr_rp_flag(Obj,L,R);
152: Flag=R[0]; R=R[1];
153: if (type(R) == 17) R=quotetolist(R);
154: RR = "quote("+listtoquote_str(R)+")";
1.5 ! takayama 155: if (Debug2) {print("==> "+RR+" by "); print(listtoquote_str(L));}
! 156: if (Debug2) print("-------- end of tr_apply_rule1_flag ------------ ");
1.3 takayama 157: return [Flag,eval_str(RR)];
158: }
159:
160: def tr_apply_or_rules(Q,R) {
161: Flag = 1;
162: N = length(R);
163: while (Flag) {
164: Flag = 0;
165: for (I=0; I<N; I++) {
166: Q = tr_apply_rule1_flag(Q,R[I][0],R[I][1]);
167: if (Q[0]) {
168: Flag = 1;
169: dprint("Applied the rule "+rtostr(I));
170: }
171: Q = Q[1];
172: }
173: }
174: return Q;
175: }
176: def test5() {
177: Rule1=[quote(sin(pn(x)*@pi)),[qt_sin_int,x]]; /* sin($B@0?t(B*@pi) --> 0 */
178: Rule2=[quote(0*pn(y)), [qt_zero]]; /* 0*any --> 0 */
179: Rule3=[quote(pn(y)*0), [qt_zero]]; /* any*0 --> 0 */
180: Rule4=[quote(pn(y)+0), [qt_id,y]]; /* any+0 --> any */
181: Rule5=[quote(0+pn(y)), [qt_id,y]]; /* 0+any --> any */
182: Rule6=[quote(sin(0)), [qt_zero]]; /* sin(0) --> 0 */
183: R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
184: print(print_input_form(R0));
185: R=tr_apply_rule1_flag(R0,Rule1[0],Rule1[1]);
186: print([R[0],print_input_form(R[1])]);
187: R=tr_apply_or_rules(R0,[Rule1,Rule2,Rule3,Rule4,Rule5,Rule6]);
1.2 takayama 188: return R;
189: }
190:
1.5 ! takayama 191: /* $BHyJ,4D$N7W;;(B */
! 192: /* x $B$K0MB8$7$F$k$+(B? u, u_0, u_1, u_2, ... $B$O(B x $B$K0MB8$7$F$k(B.*/
! 193: def to_quote(L) {
! 194: return eval_str("quote("+listtoquote_str(L)+")");
! 195: }
! 196: def dep6(Q) {
! 197: if (type(Q) == 4) {
! 198: Q = to_quote(Q);
! 199: }
! 200: if (qt_is_dependent(Q,x)) return 1;
! 201: if (qt_is_dependent(Q,u)) return 1;
! 202: /* $B$H$j$"$($:(B 10 $B<!$^$G$N(B f. --> $B$J$s$H$+$;$h(B. */
! 203: for (I=0; I<10; I++) {
! 204: if (qt_is_dependent(Q,idxtov(u,I))) return 1;
! 205: }
! 206: return 0;
! 207: }
! 208: def diff_lin(F,G) {
! 209: if (type(F) == 4) F=to_quote(F);
! 210: if (type(G) == 4) G=to_quote(G);
! 211: return qt_replace(quote(diff(f)+diff(g)),[[f,F],[g,G]]);
! 212: }
! 213: def diff_mul(F,G) {
! 214: F1 = dep6(F); G1 = dep6(G);
! 215: if (type(F) == 4) F=to_quote(F);
! 216: if (type(G) == 4) G=to_quote(G);
! 217: if (F1 && G1)
! 218: return qt_replace(quote(diff(f)*g+f*diff(g)),[[f,F],[g,G]]);
! 219: if ((F1 == 1) && (G1 == 0))
! 220: return qt_replace(quote(diff(f)*g),[[f,F],[g,G]]);
! 221: if ((F1 == 0) && (G1 == 1))
! 222: return qt_replace(quote(f*diff(g)),[[f,F],[g,G]]);
! 223: if ((F1 == 0) && (G1 == 0))
! 224: return qt_zero();
! 225: }
! 226: def qt_one() {
! 227: return quote(1);
! 228: }
! 229: def diff_x_n(N) {
! 230: N = eval_quote(N);
! 231: N1=N-1;
! 232: if (N1 == 0) return qt_one();
! 233: if (N1 == 1) return quote(2*x);
! 234: if (N1 > 1) return eval_str("quote("+rtostr(N)+"*x^"+rtostr(N1)+")");
! 235: }
! 236: /* F $B$,(B u $B$H$+(B u_0, u_1, ... $B$J$i(B 1 $B$rLa$9(B. */
! 237: /* debug $BMQ$NF~NO(B.
! 238: tr_check_pn(quote(u_1),quote(pn(x,is_u_variable(x))));
! 239: */
! 240: def is_u_variable(F) {
! 241: /* $B=R8l$NA0$N(B check point $B$b(B debugger $B$KM_$7$$(B. */
! 242: /* print("is_u_variable: ",0); print(print_input_form(F)); */
! 243: if (type(F) == 17) F=quotetolist(F);
! 244: if (rtostr(F[0]) == "internal") {
! 245: V = eval_str(rtostr(F[1]));
! 246: if (vtoidx(V)[0] == "u") return 1;
! 247: }
! 248: return 0;
! 249: }
! 250: /* u_i^n $B$NHyJ,$r$9$k(B. n*u_{i+1}*u_i^{n-1}
! 251: Todo: $B$b$C$H4J7i$K(B quote $B$r=q$1$J$$$+(B?
! 252: */
! 253: def diff_u_n(F,N) {
! 254: F = eval_quote(F);
! 255: I = vtoidx(F);
! 256: if (length(I) == 1) I = 0; else I=I[1];
! 257: N = eval_quote(N);
! 258: N1=N-1;
! 259: NextU = "u_"+rtostr(I+1);
! 260: if (I == 0) U = "u"; else U = "u_"+rtostr(I);
! 261:
! 262: NN = objtoquote(N);
! 263: NN1 = objtoquote(N1);
! 264: NextU = objtoquote(eval_str(NextU));
! 265: U = objtoquote(eval_str(U));
! 266:
! 267: if (N1 == 0) return NextU;
! 268: if (N1 == 1) return qt_replace(quote(2*up*uu),[[up,NextU],[uu,U]]);
! 269: if (N1 > 1) return qt_replace(quote(n*up*uu^m),[[up,NextU],[uu,U],
! 270: [n,NN],[m,NN1]]);
! 271: }
! 272:
! 273: def test6b() {
! 274: T1=[quote(diff(x)),[qt_one]];
! 275: T2=[quote(diff(x^pn(n))),[diff_x_n,n]]; /* is_poly? $B$,M_$7$$(B. */
! 276: R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
! 277: R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];
! 278:
! 279: A = quote(diff(2*4*x^3+x));
! 280: print(print_input_form(A));
! 281: R=tr_apply_or_rules(A,[R1,R2,T1,T2]);
! 282: return R;
! 283: }
! 284:
! 285: /* Use Debug2=1; $B$O(B debug $B$K$H$F$bM-1W(B. */
! 286: def test6() {
! 287: T1=[quote(diff(x)),[qt_one]];
! 288: T2=[quote(diff(x^pn(n))),[diff_x_n,n]]; /* is_poly? $B$,M_$7$$(B. */
! 289: T3=[quote(diff(pn(f,is_u_variable(f))^pn(n))),[diff_u_n,f,n]];
! 290: R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
! 291: R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];
! 292:
! 293: /* A = quote(diff(2*x^3+x));*/
! 294: A = quote(diff(2*u^3+x));
! 295: print(print_input_form(A));
! 296: R=tr_apply_or_rules(A,[R1,R2,T1,T2,T3]);
! 297: return R;
! 298: }
1.2 takayama 299: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>