Ask Your Question

Traversing sage's symbolic expression trees in python

asked 2012-11-12 18:16:40 +0200

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

answered 2012-11-13 18:08:19 +0200

Volker Braun gravatar image

Did you read TFM? There is a Converterclass to build your own expressions trees:

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


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 18:53:09 +0200 )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 12:38:26 +0200 )edit
slelievre gravatar imageslelievre ( 2021-04-13 21:33:07 +0200 )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


Asked: 2012-11-12 18:16:40 +0200

Seen: 1,613 times

Last updated: Nov 13 '12