Ask Your Question
1

Traversing sage's symbolic expression trees in python

asked 2012-11-12 11:16:40 -0500

rmp251 gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2012-11-13 11:08:19 -0500

Volker Braun gravatar image

Did you read TFM? There is a Converterclass to build your own expressions trees: http://www.sagemath.org/doc/reference...

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

edit flag offensive delete link more

Comments

Thank 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.

rmp251 gravatar imagermp251 ( 2012-11-13 11:53:09 -0500 )edit

@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...

kcrisman gravatar imagekcrisman ( 2012-11-14 05:38:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-11-12 11:16:40 -0500

Seen: 1,042 times

Last updated: Nov 13 '12