Substituting expressions in Maxima result



Try this:

(%i31) my_power(a,b) := if b = 2 then square(a) elseif b = -2 then 
1/square(a) else pow(a,b)$

(%i32) subst('my_power,"^", x^2 + 1/x^2 + (p+q)^2);
(%o32) my_power(x,2)+my_power(x,-2)+my_power(q+p,2)

(%i33) ev(%, 'my_power);
(%o33) square(x)+1/square(x)+square(q+p)

(%i34) subst('my_power,"^", (1+x^2)^2);
(%o34) my_power(my_power(x,2)+1,2)

(%i35) ev(%, 'my_power);
(%o35) square(square(x)+1)

(%i36) subst('my_power,"^", (1+x^4)^4);
(%o36) my_power(my_power(x,4)+1,4)

(%i37) ev(%, 'my_power);
(%o37) pow(pow(x,4)+1,4)

The need for the ev(%, 'my_power) is unfortunate.  Also, if it's 
important, you can also catch b = 1/2  case.

Barton

D> About the "^" replacement, try: subst("^" = pow, expr);
> 
> That also helps me much. But no matter what I chose instead of "pow" I
> always get something like "mychosenname(variable, 2)". I'd rather have
> something like "square(variable)". Is that also possible?
> 
> You'll have to use pattern-matching for that.  Same thing for sqrt, 
> which Maxima represents as x^(1/2)
> 
>