From: Eduardo O. <edu...@gm...> - 2024-08-12 02:59:37
|
Hi Stavros, I just tested your function show_expression from https://sourceforge.net/p/maxima/mailman/message/58795759/ on factorized numbers and on three flavors of power series: plain, "trunc", and "taylor"... it turned out that "inflag:true", is enough to make factorized numbers be displayed correctly - as products of powers - but your show_expression doesn't have support for telling if a power series would be displayed with a "...", with a "/T/", or both. Would that be easy to fix? Here is the code: --snip--snip-- 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)); a : factor(1000); "shows a as 2^3 * 5^3"$ show_expression(a), inflag:true; "shows a as 2^3 * 5^3"$ show_expression(a); "shows a as 8 * 125"$ linel : 100; b1 : 1 + 2*x + 2*x^2 + 4/3*x^3; b2 : trunc(b1); "has a ..."$ b3 : taylor(exp(2*x), x, 0, 3); "has a ... and a /T/"$ show_expression(b1); show_expression(b1), inflag:true; show_expression(b2); show_expression(b2), inflag:true; show_expression(b3); show_expression(b3), inflag:true; --snip--snip-- ...and the relevant part of the output: --snip--snip-- (%i22) b1 : 1 + 2*x + 2*x^2 + 4/3*x^3; 3 4 x 2 (%o22) ──── + 2 x + 2 x + 1 3 (%i23) b2 : trunc(b1); "has a ..."$ 3 2 4 x (%o23) 1 + 2 x + 2 x + ──── + . . . 3 (%i25) b3 : taylor(exp(2*x), x, 0, 3); "has a ... and a /T/"$ 3 2 4 x (%o25)/T/ 1 + 2 x + 2 x + ──── + . . . 3 (%i27) show_expression(b1); (%o27) add(div(mul(4, power(x, 3)), 3), mul(2, power(x, 2)), mul(2, x), 1) (%i28) show_expression(b1), inflag:true; (%o28) add(1, mul(2, x), mul(2, power(x, 2)), mul(rat(4, 3), power(x, 3))) (%i29) show_expression(b2); (%o29) add(1, mul(2, x), mul(2, power(x, 2)), div(mul(4, power(x, 3)), 3)) (%i30) show_expression(b2), inflag:true; (%o30) add(1, mul(2, x), mul(2, power(x, 2)), mul(rat(4, 3), power(x, 3))) (%i31) show_expression(b3); (%o31) add(1, mul(2, x), mul(2, power(x, 2)), div(mul(4, power(x, 3)), 3)) (%i32) show_expression(b3), inflag:true; (%o32) mul(rat(1, 3), add(3, mul(6, x), mul(6, power(x, 2)), mul(4, power(x, 3)))) (%i33) --snip--snip-- Cheers =), Eduardo Ochs http://anggtwu.net/eev-maxima.html On Mon, 15 Jul 2024 at 19:43, Stavros Macrakis <mac...@gm...> wrote: > 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 >> > |