Maxima: sublis() versus subst().



On 9/17/07, C. Frangos <cfrangos at telkomsa.net> wrote:
>
> I am trying to use the Maxima function sublis() for parallel substitution
> but
> it does not seem to work for the simplified example below.  Its not clear
> what the reason is.


Sublis uses an internal trick which depends on the LHS of each substitution
being a symbol.  This is ridiculous, of course.

On the other hand the function subst() seems to work ok. Is it possible to
> (somehow set a flag and) do parallel substitution with subst() ??
>

I don't believe so.  The easiest way is probably to perform your
substitution in two steps.

Suppose you have a list sub: [  [ from1, to1], [from2, to2], ...]

twostagesub(l,ex):=
  block([ len: length(l) ],
     for i thru len do ex:subst(concat(subst,i),l[i][1],ex),
     for i thru len do ex:subst(l[i][2],concat(subst,i),ex),
     ex)$

twostagesub([[a,a*b],[b,b*c],[c,a*c]],a*b*c) => a^2*b^2*c^2
  == sublis([a=a*b,b=b*c,c=a*c],a*b*c)
  == sublis(reverse([a=a*b,b=b*c,c=a*c]),a*b*c)

subst([a=a*b,b=b*c,c=a*c],a*b*c) => a^4*b^2*c^3
subst(reverse([a=a*b,b=b*c,c=a*c]),a*b*c)  =>  a^2*b^3*c^2

This is *not* an elegant, efficient way to do things, but it will work.
There are many opportunities for speed improvement....

        -s