Opened 12 years ago

Last modified 4 years ago

#9329 new enhancement

Improve documentation of how to access parts of the expression tree of a symbolic expression

Reported by: Karl-Dieter Crisman Owned by: Burcin Erocal
Priority: major Milestone: sage-wishlist
Component: symbolics Keywords: expression tree, symbolic expression
Cc: Ross Kyprianou, Marcelo Forets, Samuel Lelièvre Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by Karl-Dieter Crisman)

sage: var('n')
n
sage: g=e^((n*pi-pi*2))

The desire is to have clear documentation on how to apply (e.g.) factor() to just some piece of this, such as the exponent.

Change History (18)

comment:1 Changed 12 years ago by Mike Hansen

Did you have something specific in mind? You can always get at the underlying tree with operand() and operands()

sage: var('n')
n
sage: g = e^((n*pi-pi*2))
sage: g.operator(), g.operands()
(exp, [-2*pi + pi*n])

This is what is done in expression_conversions.py

comment:2 Changed 12 years ago by Karl-Dieter Crisman

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 g.operands() is itself something with an operator and operands. Maybe there isn't such an object per se?

comment:3 Changed 12 years ago by Jason Grout

See http://sagenb.org/home/pub/1760/ for a simple implementation.

def tree(expr): 
    if expr.operator() is None: 
        return expr 
    else: 
        return [expr.operator()]+map(tree, expr.operands()) 

comment:4 Changed 12 years ago by Burcin Erocal

Here is the thread:

http://groups.google.com/group/sage-support/t/e4b769a26269b6ed

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 operator() and operands() or pattern matching with find() and match().

I don't think adding a function like Jason suggests in comment:3 would provide anything more than that is already there. Note that in the example worksheet Jason also doesn't use this function.

I suggest to close this ticket unless there is a more concrete suggestion.

comment:5 Changed 12 years ago by Karl-Dieter Crisman

Description: modified (diff)
Summary: Make it easy to access expression tree of a symbolic expressionImprove documentation of how to access parts of the expression tree of a symbolic expression

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 SymbolicExpression?, huh? It would be nice to do an example with simplification as well as substitution.

Changing ticket summary

comment:6 Changed 12 years ago by Ross Kyprianou

Cc: Ross Kyprianou added

comment:7 Changed 10 years ago by Karl-Dieter Crisman

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 Converter 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".

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]]

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.

comment:8 Changed 10 years ago by Jason Grout

By the way, http://interact.sagemath.org/node/76 might be a nice example too (that's an extremely simple expression->Java translator...)

comment:9 in reply to:  7 ; Changed 10 years ago by Nils Bruin

Does

sage: S._maxima_lib_().ecl().python()

count?

comment:10 in reply to:  9 Changed 10 years ago by Karl-Dieter Crisman

Does

sage: S._maxima_lib_().ecl().python()

count?

That certainly counts as one example. I don't know that people will know what to do with 'MTIMES' :-)

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.

comment:11 Changed 9 years ago by Jeroen Demeyer

Milestone: sage-5.11sage-5.12

comment:12 Changed 9 years ago by For batch modifications

Milestone: sage-6.1sage-6.2

comment:13 Changed 8 years ago by For batch modifications

Milestone: sage-6.2sage-6.3

comment:14 Changed 8 years ago by For batch modifications

Milestone: sage-6.3sage-6.4

comment:15 Changed 8 years ago by Karl-Dieter Crisman

See also #9424.

comment:16 Changed 6 years ago by Marcelo Forets

Cc: Marcelo Forets added

comment:17 Changed 4 years ago by Samuel Lelièvre

Cc: Samuel Lelièvre added
Keywords: expression tree symbolic expression added
Milestone: sage-6.4sage-wishlist

comment:18 Changed 4 years ago by Samuel Lelièvre

Note: the worksheet mentioned by jason in comment:3, originally at

is now available, translated into a .sagews worksheet, at:

Note: See TracTickets for help on using tickets.