|
From: Stavros M. <mac...@al...> - 2022-07-10 15:33:04
|
Eduardo,
I don't know what exactly your goal is with *foo1* (maybe just a test), but
a few observations:
- You should always quote literal constants, e.g., *action='op*, even
though *op *evaluates to *op *if it's not bound to some value.
Otherwise, if there happens to be a variable called *op* in your
environment, you will get surprising results.
- I hope you're aware that *op* and *args* work on the *external* form
of an expression, which is designed for human consumption. The internal
form is more uniform and generally better for automatic manipulation. You
can get the internal form by binding *inflag* or by using the
*inpart* function:
*op(1/a)=part(1/a,0)="/" *but *inpart(1/a,0)="^" *(*1/a = a^-1)*.
- A useful argument for *part* is *allbut*, which is perversely
documented under *allbut*, but not under *part*: *inpart(a*b/c,allbut(2))
=> a/c* (because *a*b/c* is internally *a*b*c^-1)*.
- Unfortunately, there is not an *in* version of *args*. Maybe we should
define *(in)part(...,allbut(0))* to be equivalent to *args(...)*.
Hope this is helpful.
-s
On Sat, Jul 9, 2022 at 9:27 PM Eduardo Ochs <edu...@gm...> wrote:
> On Sat, 9 Jul 2022 at 22:15, Stavros Macrakis <mac...@al...>
> wrote:
> >
> > Use a=b to test structural equality.
> >
> > equal(a,b) is for mathematical equality, and is unknown (not false) in
> the absence of assertions (assume) about a and b.
>
> Perfect!
> Many thanks! =)
> Here is a transcript:
>
> (%i1) foo1 : lambda([o, action],
> if action = op then op(o)
> elseif action = args then args(o)
> else args(o)[action]);
> (%o1) lambda([o, action], if action = op then op(o) elseif action = args
> then args(o) else
> args(o) )
>
> action
> (%i2) foo(a, [b]) := xreduce(foo1, b, a);
> (%o2) foo(a, [b]) := xreduce(foo1, b, a)
> (%i3) ex1 : a+b*c;
> (%o3) b c + a
> (%i4) foo(ex1);
> (%o4) b c + a
> (%i5) foo(ex1, op);
> (%o5) +
> (%i6) foo(ex1, args);
> (%o6) [b c, a]
> (%i7) foo(ex1, 1);
> (%o7) b c
> (%i8) foo(ex1, 1, op);
> (%o8) *
> (%i9) foo(ex1, 1, args);
> (%o9) [b, c]
> (%i10) foo(ex1, 1, 1);
> (%o10) b
> (%i11) foo(ex1, 1, 2);
> (%o11) c
> (%i12) foo(ex1, 2);
> (%o12) a
>
> Cheers =),
> Eduardo Ochs
> http://angg.twu.net/eev-maxima.html
>
|