1 /*************** -*- Mode: MACSYMA; Package: MAXIMA -*- ******************/
2 /***************************************************************************
4 *** Copyright (c) 1984 by William Schelter,University of Texas *****
5 *** All rights reserved *****
6 ***************************************************************************/
9 (kill(all),matchdeclare(a,true));
11 (\^rule1_retval : tellsimp(sin(a)^2,1-cos(a)^2), 0);
17 nonzeroandfreeof(x,e):=is(e # 0 and freeof(x,e));
18 nonzeroandfreeof(x,e):=is(e # 0 and freeof(x,e))$
19 matchdeclare(a,nonzeroandfreeof(x),b,freeof(x));
21 defmatch(linear,a*x+b,x);
23 linear(3*z+(y+1)*z+y^2,z);
24 [b = y^2,a = y+4,x = z]$
25 matchdeclare([a,f],true);
27 constinterval(l,h):=constantp(h-l);
28 constinterval(l,h):=constantp(h-l)$
29 matchdeclare(b,constinterval(a));
33 block(remove(integrate,outative),defmatch(checklimits,'integrate(f,x,a,b)),
34 declare(integrate,outative));
36 'integrate(sin(t),t,x+%pi,x+2*%pi);
37 'integrate(sin(t),t,x+%pi,x+2*%pi)$
39 [b = x+2*%pi,a = x+%pi,x = t,f = sin(t)]$
40 'integrate(sin(t),t,0,x);
41 'integrate(sin(t),t,0,t)$
44 (kill(all),matchdeclare(x,freeof(%i)));
48 (sinrule_retval : tellsimp(sin(%i*x),%i*sinh(x)), 0);
50 trigexpand(sin(x+%i*y));
51 sin(x)*cos(%i*y)+%i*cos(x)*sinh(y)$
56 ev((\^rule2_retval : tellsimp(0^0,1), 0),simp:false);
60 remrule ("^", first (\^rule2_retval));
62 (\^rule3_retval : tellsimp(sin(x)^2,1-cos(x)^2), 0);
74 (\^rule4_retval : tellsimp(sin(a)^2,1-cos(a)^2), 0);
80 (kill(all),nonzeroandfreeof(x,e):=is(e # 0 and freeof(x,e)));
81 nonzeroandfreeof(x,e):=is(e # 0 and freeof(x,e))$
82 matchdeclare(a,nonzeroandfreeof(x),b,freeof(x));
84 defmatch(linear,a*x+b,x);
86 linear(3*z+(y+1)*z+y^2,z);
87 [b = y^2,a = y+4,x = z]$
88 matchdeclare([a,f],true);
90 constinterval(l,h):=constantp(h-l);
91 constinterval(l,h):=constantp(h-l)$
92 matchdeclare(b,constinterval(a));
96 block(remove(integrate,outative),defmatch(checklimits,'integrate(f,x,a,b)),
97 declare(integrate,outative));
99 'integrate(sin(t),t,x+%pi,x+2*%pi);
100 'integrate(sin(t),t,x+%pi,x+2*%pi)$
102 [b = x+2*%pi,a = x+%pi,x = t,f = sin(t)]$
103 'integrate(sin(t),t,0,x);
104 'integrate(sin(t),t,0,t)$
110 /* test cases extracted from reference manual for 'let' */
112 (kill(all),matchdeclare([a,a1,a2],true));
114 oneless(x,y):=is(x = y-1);
115 oneless(x,y):=is(x = y-1)$
116 let(a1*a2!,a1!,oneless,a2,a1);
117 ?mtext(a1*a2!,?\ \-\-\>\ ,a1!,?\ where\ ,oneless(a2,a1))$
118 ev(let(a1!/a1,(a1-1)!),letrat);
119 ?mtext(a1!/a1,?\ \-\-\>\ ,(a1-1)!)$
120 ev(letsimp(n*m!*(n-1)!/m),letrat);
122 let(sin(a)^2,1-cos(a)^2);
123 ?mtext(sin(a)^2,?\ \-\-\>\ ,1-cos(a)^2)$
127 cos(x)^4-2*cos(x)^2+1$
129 /* test cases extracted from reference manual for 'letrat' */
131 (kill (all), matchdeclare (n, true))$
135 ?mtext (n!/n, ?\ \-\-\>\ , (n - 1)!);
137 (letrat: false, letsimp (a!/a));
140 (letrat: true, letsimp (a!/a));
146 /* test cases extracted from reference manual for 'letsimp' */
149 e0: e(k) = -(9*y(k))/(5*z)-u(k-1)/(5*z)+(4*y(k))/(5*z^2) +(3*u(k-1))/(5*z^2)+y(k)-(2*u(k-1))/5,
150 matchdeclare (h, any),
151 let (u(h)/z, u(h - 1)));
152 ?mtext (u(h)/z, ?\ \-\-\>\ , u(h - 1));
154 let (y(h)/z, y(h - 1));
155 ?mtext (y(h)/z, ?\ \-\-\>\ , y(h - 1));
157 /* SF bug #4408: "letsimp doesn't simplify before returning result" */
160 e(k) = y(k)-(9*y(k-1))/5-(2*u(k-1))/5+(4*y(k-2))/5-u(k-2)/5+(3*u(k-3))/5$
162 /* SF bug #4407: "letsimp doesn't commute with equation, list, matrix" */
164 letsimp ([rhs (e0)]);
167 letsimp (matrix ([rhs (e0)]));
168 matrix ([''(rhs (e1))]);
170 letsimp (lhs (e0)) = letsimp (rhs (e0));
173 [letsimp (rhs (e0))];
176 matrix ([letsimp (''(rhs (e0)))]);
177 matrix ([''(rhs (e1))]);
183 (2*a^2-b)*cos(x+2*y)-(a*b+5)*sin(u-4*x);
184 (2*a^2-b)*cos(2*y+x)+(a*b+5)*sin(4*x-u)$
185 exp1:outofpois(poisexpt(%,2));
186 1/2*(2*a^2-b)^2*cos(4*y+2*x)-(2*a^2-b)*(-a*b-5)*sin(2*y+5*x-u)
187 +(2*a^2-b)*(-a*b-5)*sin(2*y-3*x+u)
188 -1/2*(-a*b-5)^2*cos(8*x-2*u)+1/2*(-a*b-5)^2
193 ratsimp(outofpois(poisint(exp1,y)));
194 ''(ratsimp(((-1)*(2*a^2-b)*(-5-a*b)*cos(u-3*x+2*y))/2+((2*a^2-b)*(-5-a*b)*cos(-u+5*x+2*y))/2+((2*a^2-b)^2*sin(2*x+4*y))/8))$
196 ratsimp(outofpois(poissimp(%)));
197 ''(ratsimp(1/8*(2*a^2-b)^2*sin(4*y+2*x)+1/2*(2*a^2-b)*(-a*b-5)*cos(2*y+5*x-u)
198 -1/2*(2*a^2-b)*(-a*b-5)*cos(2*y-3*x+u)))$
200 poissimp(sin(x)^5+cos(x)^5);
201 1/16*sin(5*x)+1/16*cos(5*x)-5/16*sin(3*x)+5/16*cos(3*x)+5/8*sin(x)+5/8*cos(x)$
206 /* test for bug #1097: rules and pattern bug */
209 matchdeclare (aa, true, gg, lambda([yy], evenp(yy) and (yy > 1))),
210 let (foo(aa)^gg, (1 - bar(aa)^2)^(gg/2)),
221 bar(x)^4 - 2*bar(x)^2 + 1;
223 /* More tests for bug #1097: pattern variable appears in letsimp result */
226 matchdeclare ([arg1, arg2, exp], true),
227 let (f(arg1, arg2)^exp, g(arg2, arg1)^exp), 0);
230 letsimp (f(x, y)); /* This used to yield g(arg2,arg1) */
233 letsimp (f(x, y)^2); /* This used to yield g(arg2,arg1)^2 */
236 letsimp (f(x, y)^q); /* This used to yield g(arg2,arg1)^q */
239 (remlet (f(arg1, arg2)^exp),
240 remove ([arg1, arg2, exp], matchdeclare),
244 /* letsimp confused by debug stuff in expression car
245 * mailing list 2012-12-23: "let(simp) behaves different when loaded from batch file"
248 test() := block([expr],
250 expr : diff(f(x), x),
251 F(xx) := at(expr, [x = xx]),
252 let(at(diff(f(x),x),[x=0]),5),
257 /* Bug #2709: `letsimp' is wrong if `ratexpand' is called */
265 ratexpand (a * b * c);
268 letsimp (U / n); /* This used to yield 1 */
274 /* Some basic letsimp tests, based on bug #3950 */
288 letsimp (f (1, 2, 3));
294 (let (f (1), g (1)), 0);
306 letsimp (f (1, 2, 3));
312 (let (f (1, 2, 3), g (1, 2, 3)), 0);
324 letsimp (f (1, 2, 3));
327 (remlet (f (1, 2, 3)), 0);
330 /* Bug #3950: letsimp confuses symbols and nullary applications */
332 (let (f (), g ()), 0);
335 /* This used to yield g() */
339 /* This used to yield f() */
346 letsimp (f (1, 2, 3));
352 (matchdeclare (foo, true), 0);
355 (let (foo (), bar ()), 0);
358 /* This used to cause a stack overflow */
362 /* This also used to cause a stack overflow */
366 (remove (foo, matchdeclare),