Sage: Ticket #9329: Improve documentation of how to access parts of the expression tree of a symbolic expression
https://trac.sagemath.org/ticket/9329
<pre class="wiki">sage: var('n')
n
sage: g=e^((n*pi-pi*2))
</pre><p>
The desire is to have clear documentation on how to apply (e.g.) factor() to just some piece of this, such as the exponent.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/9329
Trac 1.2Mike HansenFri, 25 Jun 2010 00:41:40 GMT
https://trac.sagemath.org/ticket/9329#comment:1
https://trac.sagemath.org/ticket/9329#comment:1
<p>
Did you have something specific in mind? You can always get at the underlying tree with operand() and operands()
</p>
<pre class="wiki">sage: var('n')
n
sage: g = e^((n*pi-pi*2))
sage: g.operator(), g.operands()
(exp, [-2*pi + pi*n])
</pre><p>
This is what is done in expression_conversions.py
</p>
TicketKarl-Dieter CrismanFri, 25 Jun 2010 13:07:33 GMT
https://trac.sagemath.org/ticket/9329#comment:2
https://trac.sagemath.org/ticket/9329#comment:2
<p>
I think the original request from a user was in order to access the whole tree at once as a "parsed tree", since here of course <code>g.operands()</code> is itself something with an operator and operands. Maybe there isn't such an object per se?
</p>
TicketJason GroutSun, 27 Jun 2010 03:47:15 GMT
https://trac.sagemath.org/ticket/9329#comment:3
https://trac.sagemath.org/ticket/9329#comment:3
<p>
See <a class="ext-link" href="http://sagenb.org/home/pub/1760/"><span class="icon"></span>http://sagenb.org/home/pub/1760/</a> for a simple implementation.
</p>
<pre class="wiki">
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree, expr.operands())
</pre>
TicketBurcin ErocalWed, 30 Jun 2010 12:24:12 GMT
https://trac.sagemath.org/ticket/9329#comment:4
https://trac.sagemath.org/ticket/9329#comment:4
<p>
Here is the thread:
</p>
<p>
<a class="ext-link" href="http://groups.google.com/group/sage-support/t/e4b769a26269b6ed"><span class="icon"></span>http://groups.google.com/group/sage-support/t/e4b769a26269b6ed</a>
</p>
<p>
I think this is more of a documentation problem. We don't have any explanation of how to work with symbolic expressions, either traversing the tree using <code>operator()</code> and <code>operands()</code> or pattern matching with <code>find()</code> and <code>match()</code>.
</p>
<p>
I don't think adding a function like Jason suggests in <a class="ticket" href="https://trac.sagemath.org/ticket/9329#comment:3" title="Comment 3">comment:3</a> would provide anything more than that is already there. Note that in the example worksheet Jason also doesn't use this function.
</p>
<p>
I suggest to close this ticket unless there is a more concrete suggestion.
</p>
TicketKarl-Dieter CrismanWed, 30 Jun 2010 13:12:33 GMTdescription, summary changed
https://trac.sagemath.org/ticket/9329#comment:5
https://trac.sagemath.org/ticket/9329#comment:5
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/9329?action=diff&version=5">diff</a>)
</li>
<li><strong>summary</strong>
changed from <em>Make it easy to access expression tree of a symbolic expression</em> to <em>Improve documentation of how to access parts of the expression tree of a symbolic expression</em>
</li>
</ul>
<p>
Well, this seems very reasonable; I certainly wouldn't have ever figured out the way you do it in that thread. This probably belongs either at the top of one of expression.pyx or in the documentation for <a class="missing wiki">SymbolicExpression?</a>, huh? It would be nice to do an example with simplification as well as substitution.
</p>
<p>
Changing ticket summary
</p>
TicketRoss KyprianouTue, 07 Sep 2010 13:31:52 GMTcc set
https://trac.sagemath.org/ticket/9329#comment:6
https://trac.sagemath.org/ticket/9329#comment:6
<ul>
<li><strong>cc</strong>
<em>Ross Kyprianou</em> added
</li>
</ul>
TicketKarl-Dieter CrismanThu, 06 Dec 2012 03:52:13 GMT
https://trac.sagemath.org/ticket/9329#comment:7
https://trac.sagemath.org/ticket/9329#comment:7
<p>
Resurrecting the original idea because I want this for my discrete math class in a couple days and realize that even having a very basic example of how to use <code>Converter</code> to make an expression tree - say one I could plot! - would be really useful. Right now I'm stuck with Jason's idea, which works but perhaps isn't as "Sage-ic".
</p>
<pre class="wiki">sage: from sage.symbolic.random_tests import random_expr
sage: S = random_expr(20,nvars=2)
sage: S
sinh_integral(-sec(-4*abs(e))/((kronecker_delta(e, v1) - 52)*arctan(elliptic_kc(-2) - 2)))
sage: def tree(expr):
....: if expr.operator() is None:
....: return expr
....: else:
....: return [expr.operator()]+map(tree,expr.operands())
....:
sage: tree(S)
[sinh_integral, [<built-in function mul>, [<built-in function pow>, [<built-in function add>, [kronecker_delta, [exp, 1], v1], -52], -1], [<built-in function pow>, [arctan, [<built-in function add>, [elliptic_kc, -2], -2]], -1], [sec, [<built-in function mul>, [abs, [exp, 1]], -4]], -1]]
</pre><p>
I'm realizing it might even be useful to have a (binary) tree returned from a list of lists... but that wouldn't be here.
</p>
TicketJason GroutThu, 06 Dec 2012 12:43:10 GMT
https://trac.sagemath.org/ticket/9329#comment:8
https://trac.sagemath.org/ticket/9329#comment:8
<p>
By the way, <a class="ext-link" href="http://interact.sagemath.org/node/76"><span class="icon"></span>http://interact.sagemath.org/node/76</a> might be a nice example too (that's an extremely simple expression->Java translator...)
</p>
TicketNils BruinSat, 08 Dec 2012 00:39:47 GMT
https://trac.sagemath.org/ticket/9329#comment:9
https://trac.sagemath.org/ticket/9329#comment:9
<p>
Does
</p>
<pre class="wiki">sage: S._maxima_lib_().ecl().python()
</pre><p>
count?
</p>
TicketKarl-Dieter CrismanSat, 08 Dec 2012 04:16:55 GMT
https://trac.sagemath.org/ticket/9329#comment:10
https://trac.sagemath.org/ticket/9329#comment:10
<blockquote class="citation">
<p>
Does
</p>
<pre class="wiki">sage: S._maxima_lib_().ecl().python()
</pre><p>
count?
</p>
</blockquote>
<p>
That certainly counts as one example. I don't know that people will know what to do with <code>'MTIMES'</code> :-)
</p>
<p>
Basically, an introduction to how to write something like this for people who don't know jack about expression trees (like me, though teaching it certainly helped) is what would be helpful here.
</p>
TicketJeroen DemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/9329#comment:11
https://trac.sagemath.org/ticket/9329#comment:11
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
TicketFor batch modificationsThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/9329#comment:12
https://trac.sagemath.org/ticket/9329#comment:12
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
TicketFor batch modificationsTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/9329#comment:13
https://trac.sagemath.org/ticket/9329#comment:13
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
TicketFor batch modificationsSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/9329#comment:14
https://trac.sagemath.org/ticket/9329#comment:14
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketKarl-Dieter CrismanTue, 10 Feb 2015 16:06:35 GMT
https://trac.sagemath.org/ticket/9329#comment:15
https://trac.sagemath.org/ticket/9329#comment:15
<p>
See also <a class="closed ticket" href="https://trac.sagemath.org/ticket/9424" title="#9424: defect: Provide symbolic sum function with evalf (closed: fixed)">#9424</a>.
</p>
TicketMarcelo ForetsTue, 28 Mar 2017 06:56:55 GMTcc changed
https://trac.sagemath.org/ticket/9329#comment:16
https://trac.sagemath.org/ticket/9329#comment:16
<ul>
<li><strong>cc</strong>
<em>Marcelo Forets</em> added
</li>
</ul>
TicketSamuel LelièvreMon, 21 May 2018 17:10:21 GMTcc, milestone changed; keywords set
https://trac.sagemath.org/ticket/9329#comment:17
https://trac.sagemath.org/ticket/9329#comment:17
<ul>
<li><strong>cc</strong>
<em>Samuel Lelièvre</em> added
</li>
<li><strong>keywords</strong>
<em>expression</em> <em>tree</em> <em>symbolic</em> <em>expression</em> added
</li>
<li><strong>milestone</strong>
changed from <em>sage-6.4</em> to <em>sage-wishlist</em>
</li>
</ul>
TicketSamuel LelièvreFri, 18 Jan 2019 19:32:45 GMT
https://trac.sagemath.org/ticket/9329#comment:18
https://trac.sagemath.org/ticket/9329#comment:18
<p>
Note: the worksheet mentioned by jason in <a class="ticket" href="https://trac.sagemath.org/ticket/9329#comment:3" title="Comment 3">comment:3</a>, originally at
</p>
<ul><li><a class="ext-link" href="http://sagenb.org/home/pub/1760/"><span class="icon"></span>http://sagenb.org/home/pub/1760/</a>
</li></ul><p>
is now available, translated into a .sagews worksheet, at:
</p>
<ul><li><a class="ext-link" href="https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/1701-1801/1760.sagews?viewer=share"><span class="icon"></span>https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/1701-1801/1760.sagews?viewer=share</a>
</li></ul>
Ticket