|
From: Stavros M. <mac...@gm...> - 2024-07-15 22:44:10
|
Here's a little quick-and-dirty code to show the internal form of a Maxima
expression as a functional expression.
Of course, you could just look at the Lisp....
Examples:
show_expression( -2/3*(a-b)/(b*sqrt(5)) ) <<< with
inflag:false (default), show the user-friendly version
=> sub(div(mul(2, add(a, sub(b))), mul(3, xsqrt(5), b)))
It uses function names for operators.
show_expression( -2/3*(a-b)/(b*sqrt(5)) ),inflag:true;
<<< with inflag:true, show the actual internal version
=> mul(rat(- 2, 3), power(5, rat(- 1, 2)), add(a, mul(- 1, b)), power(b,
- 1))
grind(%), linel=30. << pretty-print -- choose linel to control
appearance
=>
mul(rat(-2,3),
power(5,rat(-1,2)),
add(a,mul(-1,b)),
power(b,-1))$
It shows verb-functions as V*name* and noun-functions as N*name*.
show_expression('[integrate(x,x), 'integrate(x,x)]);
=> list(Vintegrate(x, x), Nintegrate(x, x))
It doesn't do anything special for *if, do*, etc. since the internal form
is just a list of arguments:
show_expression('(if a then b else c))
=> Vif(a, b, true, c)
show_expression('(for i thru 4 do print(i)))
=> Vdo(i, false, false, false, 4, false, Vprint(i))
show_expression('(for i:2 thru 4 while i^2 < 10 do print(i)))
=> Vdo(i, 2, false, false, 4, greatereq(power(i, 2), 10), Vprint(i))
show_expression('(for i:2 step 1/2 thru 4 while i^2 < 10 do print(i)))
=> Vdo(i, 2, rat(1, 2), false, 4, greatereq(power(i, 2), 10), Vprint(i))
show_expression('(for i:2 thru 4 next i+1/3 while i^2 < 10 do print(i)))
=> Vdo(i, 2, false, add(i, rat(1, 3)), 4, greatereq(power(i, 2), 10),
Vprint(i))
show_expression('(for i in [a,b,c] do print(i)))
=> Vdo_in(i, list(a, b, c), false, false, false, false, Vprint(i))
----------------
/* Show Maxima expression in explicit form.
For example, with inflag:false (the default):
show_expression((a[3]-3)/sqrt(5)) => f/(plus(subsc(a,3),-3),sqrt(5))
with inflag:true (to show the internal form):
show_expression(a[3]/sqrt(5)) => f*(f^(5,rat(-1,2)),subsc(a,3))
*/
show_expression(ex):=
if atom(ex) then ex
elseif subvarp(ex) then
funmake('subsc,cons(op(ex),[maplist(show_expression,args(ex))]))
elseif ratnump(ex) then funmake('rat,[first(ex),second(ex)])
elseif mapatom(ex) then ex
elseif member(part(ex,0), show_special)
then funmake(show_expression_name[part(ex,0)],
maplist(show_expression,args(ex)))
elseif not(atom(part(ex,0)))
then
funmake('apply,[show_expression(part(ex,0)),maplist(show_expression,args(ex))])
else block([pex:part(ex,0)],
funmake(concat(if nounify(pex)=pex then 'N else 'V,pex),
maplist(show_expression,args(ex))))$
show_special: []$
for i in
'[["+",add], ["-",minus], ["*",mul], ["^",power], ["/",div],
[".",ncmul], ["^^", ncpower],
["[",list], ["{",set],
[":",assign], ["::",varassign],
[":=",define], ["::=",definemac]
["'",quote], ["(",prog],
["=",equal], ["#",notequal], [">",greaterp],[">=", greatereq], ["<",
lessp], ["<=",lesseq],
["!",factorial], ["@",field]
]
do (show_expression_name[first(i)]: second(i),
push(first(i),show_special));
On Fri, Jul 12, 2024 at 2:10 PM Eduardo Ochs <edu...@gm...> wrote:
> Hi Robert -
> Fixed! See:
> https://github.com/edrx/luatree/blob/main/luatree2.mac
>
> Example:
>
> (%i1) load("~/luatree/luatree2.mac")$
> (%i2) luatree (a(b,c))$
> a__.
> | |
> b c
> (%i3) luatree (a[1][2](b,c))$
> ap_________.__.
> | | |
> [_]_____. b c
> | |
> [_]__. 2
> | |
> a 1
> (%i4) luatree (a[1][2](b,c)(d))$
> ap_______________.
> | |
> ap_________.__. d
> | | |
> [_]_____. b c
> | |
> [_]__. 2
> | |
> a 1
> (%i5)
>
> [[]], E.
>
>
>
> On Fri, 12 Jul 2024 at 13:40, Robert Dodier <rob...@gm...>
> wrote:
>
>> On Fri, Jul 12, 2024 at 12:13 AM Eduardo Ochs <edu...@gm...>
>> wrote:
>>
>> > "op" and "args" don't distinguish between a(b) and a[b]:
>> >
>> > (%i1) [o:a(b),op(o),args(o)];
>> > (%o1) [a(b), a, [b]]
>> > (%i2) [o:a[b],op(o),args(o)];
>> > (%o2) [a , a, [b]]
>> > b
>> > (%i3)
>> >
>> > what do I need to use to distinguish them?
>>
>> In the luatree code, try changing atom(something) to
>> mapatom(something) -- atom(a[b]) returns false but mapatom(a[b])
>> returns true.
>>
>> In addition to stuff like a[b], you may wish to cover stuff like
>> a[b](x, y), a(b)(x, y), a[b](x, y)(z), which are all legitimate Maxima
>> expressions ... I guess you just need to be prepared for op(something)
>> returning a non-mapatom.
>>
>> All the best,
>>
>> Robert
>>
> _______________________________________________
> Maxima-discuss mailing list
> Max...@li...
> https://lists.sourceforge.net/lists/listinfo/maxima-discuss
>
|