On 4/3/07, Robert Dodier <robert.dodier at gmail.com> wrote:
> On 4/3/07, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
>
> > If they are consistent linear equations, and so you are certain there
> > is exactly one value returned, the value would be
> > part(solve(...),1,2). I don't recommend this sort of error-prone
> > shortcut in production code, but try it out to see if it does what you
> > want.
>
> I don't recommend calling part when there are functions to
> encapsulate it. Calling a specific function makes the intent
> much clearer to the reader IMNSHO. E.g. rhs, lhs, op, args.
I explicitly said this was an error-prone shortcut. Using something
like rhs(xxx[1]) is not much better than part(xxx,1,2). The clean,
robust way to do this is something like:
/* Solve_val(eq,var) returns the unique value of var which solves eq,
if Maxima can find one *./
solve_val(eq,var) :=
block( [sols],
sols: solve(eq,var),
if atom(sols) /* ALL */
or length(sols) # 1 /* no solutions, or
more than one */
or lhs(sols[1]) # var /*
solve(f(x)=g(x),x) => [g(x)=f(x)] */
or not freeof(rhs(sols[1]),var) /* solve(f(x)=x,x) =>
[x=f(x)] */
then error("Maxima found no unique solution for ",eq,var),
rhs(sols[1]) )$