Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kcrisman opened this issue Jun 24, 2010 · 13 comments

Comments

@kcrisman
Copy link
Member

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.

CC: @sagetrac-rossk @mforets @slel

Component: symbolics

Keywords: expression tree, symbolic expression

Issue created by migration from https://trac.sagemath.org/ticket/9329

@mwhansen
Copy link
Contributor

comment:1

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

@kcrisman
Copy link
Member Author

comment:2

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?

@jasongrout
Copy link
Member

comment:3

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()) 

@burcin
Copy link
Contributor

burcin commented Jun 30, 2010

comment:4

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.

@kcrisman
Copy link
Member Author

comment:5

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

@kcrisman

This comment has been minimized.

@kcrisman kcrisman changed the title Make it easy to access expression tree of a symbolic expression Improve documentation of how to access parts of the expression tree of a symbolic expression Jun 30, 2010
@kcrisman
Copy link
Member Author

kcrisman commented Dec 6, 2012

comment:7

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.

@jasongrout
Copy link
Member

comment:8

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

@nbruin
Copy link
Contributor

nbruin commented Dec 8, 2012

comment:9

Does

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

count?

@kcrisman
Copy link
Member Author

kcrisman commented Dec 8, 2012

comment:10

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.

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@kcrisman
Copy link
Member Author

comment:15

See also #9424.

@slel
Copy link
Member

slel commented May 21, 2018

Changed keywords from none to expression tree, symbolic expression

@slel slel removed this from the sage-6.4 milestone May 21, 2018
@slel
Copy link
Member

slel commented Jan 18, 2019

comment:18

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants