Ask Your Question
1

Traversing sage's symbolic expression trees in python

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

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 18:08:19 +0100

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 18:53:09 +0100 )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 +0100 )edit
slelievre gravatar imageslelievre ( 2021-04-13 21:33:07 +0100 )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 18:16:40 +0100

Seen: 1,675 times

Last updated: Nov 13 '12