Simplifying by replacing expressions?



[edA-qa mort-ora-y <eda-qa@disemia.com>, Sun, 26 Sep 2004 13:29:43 +0200]:
> Trying your code I get the following error (which at first glance has
> nothing to do with what I'm trying to do, so I don't know if it is
> your code that does it, or a Maxima problem in general):
> 
> (C569) constWalk( sqrt(a-j^2) );
> Wrong number of arguments to "^"
> #0: cw(expr=SQRT(-j^2+(E_0[3]-P[3])^2+(E_0[2]-P[2])^2+(E_0[1]-P[1])^2))
> #1:
> constWalk(expr=SQRT(-j^2+(E_0[3]-P[3])^2+(E_0[2]-P[2])^2+(E_0[1]-P[1])^2),consts=[])
>   -- an error.  Quitting.  To debug this try DEBUGMODE(TRUE);)

The problem is applying INpart #0 to the ARGS.  You can either replace
INPART by PART, or add INFLAG: TRUE to the block of constWalk, but not
both.  So, e.g.,

constWalk(expr,[consts]) :=
  block([found: [], nfound: 0],
    [second(cw(expr)), found]) ;
cw(expr) :=
  if atom(expr)
  then     if member(expr, consts)
    then [true, expr]
    else [false, expr]
  else
    block([foo : maplist(cw, args(expr))],
        if apply("and", maplist(first, foo))
        then block([sym : concat(const, nfound: nfound + 1)],
                found: cons([sym, expr], found),
                consts: cons(sym, consts),
                [ true, sym ])
        else [ false, apply(part(expr,0), maplist(second, foo)) ]) ;
                            ^^^^
-----------------------------'

should work.

HTH,

Albert.