Hi.

As far as I know, we don't have a function that does exactly that,
though I could be wrong.  It would be nice to have one, though.

On Sat, Nov 12, 2011 at 11:01 AM, Alexey U. Gudchenko <pr...@goodok.ru> wrote:
> 12.11.2011 21:42, krastanov.ste...@gmail.com пишет:
>> This:
>>
>> import ast
>> ast.parse(repr(expression))

If you want a repr() representation, you should instead use srepr().
(repr() is the same as str()).

>>
>>  will do the trick if repr is well coded.

str() is coded so that it returns the same thing back from sympify(),
but it may not give the same thing directly, because you can have
int/int in an expression.  srepr() should always give the same thing
back.

>>
>> How much faith should I put in the repr strings in sympy? Or there is
>> another way?
>>
>> On 12 November 2011 18:20, krastanov.ste...@gmail.com <
>> krastanov.ste...@gmail.com> wrote:
>>
>>> Is there any way to get the expression tree from an expression (either
>>> using the python abstract syntax tree module or just some tuples):
>>>
>>> for example
>>>
>>> get_tree( x+y*sin(z) ) would return
>>>
>>> (Add, x, (Mul, y, (Sin z)))
>>>
>>> or
>>>
>>> (BinOp, Add, ((Symbol, x), (BinOp, Mul, (blah blah blah))))
>>>
>>
>
> I know only how to obtain the childes:
>
>>>> e = x+y*sin(z) + z
>>>> e.args
> (y*sin(z), z, x)
>
>>>> e.args[0]
>>>> y*sin(z)
>
>>>> e.args[0].args
> (y, sin(z))
>
>
>
> And test the classes:
>
>>>> e.is_Add
> True
>

You can get the class name by using .func:

In [25]: e = x + y

In [26]: e.func
Out[26]: sympy.core.add.Add

In [27]: e.func(*e.args)
Out[27]: x + y

The invariant in [27] should always hold (except for possibly some
differences in assumptions).

Aaron Meurer

>
>
>
> In other words, the somewhat tree of the expressions exists.
>
> How to represent expression-tree in other formats (strings or
> structures), I do not know.
>
> Regards.
>
> --
> Alexey U.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sympy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to