ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Nov 2012 05:38:26 -0600Traversing sage's symbolic expression trees in pythonhttps://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/I'm writing some python code around sage and I need to build an expression tree of the following basic form:
- each node represents an operation
- each child tree represents an expression tree to which the operation applies
Can anyone point me in the right direction as to how to traverse an instance of `sage.symbolic.expression.Expression`, so as to extract this kind of semantic information? Mon, 12 Nov 2012 11:16:40 -0600https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/Answer by Volker Braun for <p>I'm writing some python code around sage and I need to build an expression tree of the following basic form:</p>
<ul>
<li>each node represents an operation</li>
<li>each child tree represents an expression tree to which the operation applies</li>
</ul>
<p>Can anyone point me in the right direction as to how to traverse an instance of <code>sage.symbolic.expression.Expression</code>, so as to extract this kind of semantic information? </p>
https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?answer=14265#post-id-14265Did you read TFM? There is a `Converter` class to build your own expressions trees: http://www.sagemath.org/doc/reference/sage/symbolic/expression_conversions.html
If you want to do it by hand you can use
sage: eq = x*sin(x)
sage: eq.op
Operands of x*sin(x)
sage: list(eq.op)
[x, sin(x)]
but I'd recommend you use the `Converter`
Tue, 13 Nov 2012 11:08:19 -0600https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?answer=14265#post-id-14265Comment by kcrisman for <p>Did you read TFM? There is a <code>Converter</code>class to build your own expressions trees: <a href="http://www.sagemath.org/doc/reference/sage/symbolic/expression_conversions.html">http://www.sagemath.org/doc/reference...</a></p>
<p>If you want to do it by hand you can use </p>
<pre><code>sage: eq = x*sin(x)
sage: eq.op
Operands of x*sin(x)
sage: list(eq.op)
[x, sin(x)]
</code></pre>
<p>but I'd recommend you use the <code>Converter</code></p>
https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?comment=18709#post-id-18709@Volker - I'm gonna choose not to edit this, but please choose your language more carefully on this forum. I'm proud that Sage (usually) doesn't sink to the level of some other forums...Wed, 14 Nov 2012 05:38:26 -0600https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?comment=18709#post-id-18709Comment by rmp251 for <p>Did you read TFM? There is a <code>Converter</code>class to build your own expressions trees: <a href="http://www.sagemath.org/doc/reference/sage/symbolic/expression_conversions.html">http://www.sagemath.org/doc/reference...</a></p>
<p>If you want to do it by hand you can use </p>
<pre><code>sage: eq = x*sin(x)
sage: eq.op
Operands of x*sin(x)
sage: list(eq.op)
[x, sin(x)]
</code></pre>
<p>but I'd recommend you use the <code>Converter</code></p>
https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?comment=18712#post-id-18712Thank you. op and operator are what I was looking for. I did read TFM but it's pretty... complete. It takes a while to find the needle in the haystack.Tue, 13 Nov 2012 11:53:09 -0600https://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/?comment=18712#post-id-18712