Newbie: Substituting parts of products



Peter Pfannenschmid <peter.pfannenschmid at guido-kuebler-gmbh.de> writes:
> I have tried subst, but of course it did not work (i*k is not a
> complete subexpression in the example expression). Then I read a hint
> regarding ratsubst, but this does the wrong thing as well. Below is a
> transcription showing the problem:
>
> (%i7) test: (i*k*l + i*k*h)^2;
>                                               2
> (%o7)                          (i k l + h i k)
> (%i8) subst(s, (i*k), test);
>                                               2
> (%o8)                          (i k l + h i k)
> (%i9) ratsubst(s, (i*k), test);
>                             2  2          2    2  2
> (%o9)                      l  s  + 2 h l s  + h  s
>
> Is there any way to achieve what I want?

Well, the lazy answer is that you can tell Maxima to try and factor the
result (which ratsubst multiplied out):

(%i27) factor(ratsubst(s, (i*k), test));
                                         2  2
(%o27)                            (l + h)  s

But I admit that's cheating! Here, another option is to notice that
ratsubst wouldn't have anything to expand if test wasn't squared. With
that in mind, you can do something like

(%i28) map(lambda([x], ratsubst(s, i*k, x)), test);
                                            2
(%o28)                           (l s + h s)

That basically applies the ratsubst step to the two arguments of test
(ikl+hik and 2). This is also rather hacky, I admit!

One last trick, which I have used quite a lot is the following: Notice
that s = i*k is the same as i = s/k (well, as long as k is nonzero). So
you can do this:

(%i30) subst(s/k, i, test);
                                            2
(%o30)                           (l s + h s)

Obviously, this doesn't work so well when you're trying to substitute
for a more complex expression.

I'm hoping that some other users can come up with more "high-brow"
solutions!

Rupert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 315 bytes
Desc: not available
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20130130/7ed10321/attachment-0001.pgp>;