Opened 9 years ago

Last modified 8 months ago

#9329 new enhancement

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

Reported by: kcrisman Owned by: burcin
Priority: major Milestone: sage-wishlist
Component: symbolics Keywords: expression tree, symbolic expression
Cc: rossk, mforets, slelievre Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by kcrisman)

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 9 years ago by mhansen

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 9 years ago by kcrisman

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 9 years ago by jason

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 9 years ago by burcin

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 9 years ago by kcrisman

  • Description modified (diff)
  • Summary changed from Make it easy to access expression tree of a symbolic expression to Improve 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 9 years ago by rossk

  • Cc rossk added

comment:7 follow-up: Changed 7 years ago by kcrisman

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 7 years ago by jason

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 ; follow-up: Changed 7 years ago by nbruin

Does

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

count?

comment:10 in reply to: ↑ 9 Changed 7 years ago by kcrisman

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 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:12 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:13 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:14 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:15 Changed 5 years ago by kcrisman

See also #9424.

comment:16 Changed 2 years ago by mforets

  • Cc mforets added

comment:17 Changed 16 months ago by slelievre

  • Cc slelievre added
  • Keywords expression tree symbolic expression added
  • Milestone changed from sage-6.4 to sage-wishlist

comment:18 Changed 8 months ago by slelievre

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.