4 (load("format"), 'done);
7 /* A basic rearrangement: */
8 format((a+b*x)*(c-x)^2,%poly(x),%factor);
9 b*x^3+(a-2*b*c)*x^2+c*(b*c-2*a)*x+a*c^2$
11 /* A `mathematical' alternative to map or substpart...*/
12 format((1+2*a+a^2)*b + a*(1+2*b+b^2),%sum,%product,%factor);
15 /* Focussing simplifications */
17 foo:x^2*sin(y)^4-2*x^2*sin(y)^2+x^4*cos(y)^4-2*x^4*cos(y)^2+x^4+x^2+1,
18 format(foo,%poly(x),trigsimp));
19 x^4*sin(y)^4+x^2*cos(y)^4+1$
21 format((1+2*eps*(q+r*cos(g))^2)^4,%series(eps,2),%trig(g),factor);
22 1+eps*(4*(r^2+2*q^2)+4*cos(2*g)*r^2+16*cos(g)*q*r)
23 +eps^2*(3*(3*r^4+24*q^2*r^2+8*q^4)+3*cos(4*g)*r^4+24*cos(3*g)*q*r^3
24 +24*cos(g)*q*r*(3*r^2+4*q^2)
25 +12*cos(2*g)*r^2*(r^2+6*q^2))$
27 format((1+2*a+a^2)*b + a*(1+2*b+b^2),%sum,%product,%factor);
30 format(expand((1+a)^2/(1+b)^2),%ratsimp,%factor);
33 block([t1:expand((1+x^4)*y^2+(1+x^8)*y^4)],
34 [format(t1,%p(y),%f), format(t1,%p(y),%f(a^2-2))]);
35 [(x^8+1)*y^4+(x^4+1)*y^2,
36 (x^4-a*x^2+1)*(x^4+a*x^2+1)*y^4+(x^2-a*x+1)*(x^2+a*x+1)*y^2]$
38 format(expand((a+x)^3-a^3),%f-a^3);
41 /* Contrast power series & Taylor series: */
42 format((1+sin(x)-x)^3,%series(x,3),%f);
43 -x^3+3*x^2*(sin(x)+1)-3*x*(sin(x)+1)^2+(sin(x)+1)^3$
45 format((1+sin(x)-x)^3,%taylor(x,3),%f);
48 block([], local(sqrtp),
49 sqrtp(f):=not(atom(f)) and part(f,0)='sqrt,
50 first(solve(a*x^2+b*x-(b-2*a)/4,x)),
51 format(%%,%rhs,%preformat(%p(match(sqrtp))),%match(sqrtp),%arg(1),%f));
52 x = -sqrt((b-a)*(b+2*a))/(2*a)-b/(2*a)$
54 /***********************************************************************
55 Subtemplates & template chains. Compare: */
56 block([t2:expand((1+a)^2*(1+x)^2/(1+b^2)/(1+y)^2)],
57 [format(t2,%r[%p(x)],%f), format(t2,%r[[%p(x),%f]])]);
58 [((a+1)^2*x^2+2*(a+1)^2*x+(a+1)^2)/((b^2+1)*(y+1)^2),
59 ((a+1)^2*x^2+2*(a+1)^2*x+(a+1)^2)/((b^2+1)*y^2+(2*b^2+2)*y+b^2+1)]$
61 /* Contrast these subtemplates */
62 block([l1:[1+2*a+a^2,1+2*b+b^2,1+2*c+c^2]],
63 [format(l1,%list,%f), format(l1,%list[%noop,%f]), format(l1,%list[%noop,%ditto(%f)])]);
64 [[(a+1)^2,(b+1)^2,(c+1)^2],[a^2+2*a+1,(b+1)^2,c^2+2*c+1],
65 [a^2+2*a+1,(b+1)^2,(c+1)^2]]$
67 /* Contrast various templates & subtemplate usage */
68 format((1+a)^2/(1+2*b+b^2),%frac[expand,%f]);
71 format((1+a)^2/(1+2*b+b^2),%expression[expand,%f]);
74 format((1+a)^2/(1+2*b+b^2),%denom,%f);
77 format((1+a)^2/(1+2*b+b^2),%num,expand);
78 (a^2+2*a+1)/(b^2+2*b+1)$
80 /***********************************************************************
81 Bags: Lists, Relations & Matrices */
83 format([a=b,c=d,e=f],%equation);
86 format([a,c,e] = [b,d,f],%list);
89 format(1+2*a+a^2 < 1+2*b+b^2,%rel("<")[%f]);
92 format(1+2*a+a^2 < 1+2*b+b^2,%rhs("<"),%f);
95 format(1+2*a+a^2 < 1+2*b+b^2,%lhs("<"),%f);
98 format(1+2*a+a^2 = 1+2*b+b^2,%lhs,%f);
102 m1:matrix([a^2+2*a+1=q,b^2+2*b+1=r],[c^2+2*c+1=s,d^2+2*d+1=t]),
103 format(m1,%equation,%matrix[%list[%factor]]));
104 matrix([(a+1)^2,b^2+2*b+1],[c^2+2*c+1,d^2+2*d+1]) = matrix([q,r],[s,t])$
107 m1:matrix([a^2+2*a+1=q,b^2+2*b+1=r],[c^2+2*c+1=s,d^2+2*d+1=t]),
108 format(m1,%eq,%element(1,1),%f));
109 matrix([(a+1)^2,b^2+2*b+1],[c^2+2*c+1,d^2+2*d+1]) = matrix([q,r],[s,t])$
112 m1:matrix([a^2+2*a+1=q,b^2+2*b+1=r],[c^2+2*c+1=s,d^2+2*d+1=t]),
113 format(m1,%equation,%matrix[%noop,%list[%noop,%factor]]));
114 matrix([a^2+2*a+1,b^2+2*b+1],[c^2+2*c+1,(d+1)^2]) = matrix([q,r],[s,t])$
117 m1:matrix([a^2+2*a+1=q,b^2+2*b+1=r],[c^2+2*c+1=s,d^2+2*d+1=t]),
118 format(m1,%eq,%element(2,2),%f));
119 matrix([a^2+2*a+1,b^2+2*b+1],[c^2+2*c+1,(d+1)^2]) = matrix([q,r],[s,t])$
121 /***********************************************************************
123 format(a*(1+2*x+x^2)+b*(1+2*y+y^2),%sum,%if(lambda([xx],freeof(x,xx)))[%f,%subst(x=q)]);
124 b*(y+1)^2+a*(q^2+2*q+1)$