[sympy] Re: Iterating though the args of an expression

2010-09-23 Thread smichr
expr.args gives a python tuple of the arguments that can be iterated through in the normal ways. >>> for i in (1, 42, 'this'): ... print i ... 1 42 this >>> expr = 1+x+42/x >>> for i in expr.args: ... print i ... 1 42/x x >>> for i, a in enumerate(expr.args): ... print i, a ... 0 1 1 4

[sympy] Re: Iterating though the args of an expression

2010-09-23 Thread smichr
h[1] >>> from sympy.tensor import * h[1] >>> M=Indexed('M') h[1] >>> var('i j k', integer=1) (i, j, k) h[2] >>> eq=1+2*M(i,1)+3*j+4*M(i,2); eq 1 + 2*M(i, 1) + 3*j + 4*M(i, 2) h[3] >>> for a in eq.as_Add(): ... ie = [] ... co = [] ... for m in a.as_Mul(): ... if isinsta

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Nicholas Kinar
expr.args gives a python tuple of the arguments that can be iterated through in the normal ways. for i in (1, 42, 'this'): ... print i ... 1 42 this expr = 1+x+42/x for i in expr.args: ... print i ... 1 42/x x for i, a in enumerate(expr.args):

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Andy Ray Terrel
On Thu, Sep 23, 2010 at 2:21 PM, Nicholas Kinar wrote: > >> expr.args gives a python tuple of the arguments that can be iterated >> through in the normal ways. >> >> > > for i in (1, 42, 'this'): > >> >> ...     print i >> ... >> 1 >> 42 >> this >> > > expr = 1+x+42/x > for

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Nicholas Kinar
Try: In [166]: expr = 0.5*p(1, 3)**(1 + n)/(x**2*y**2) In [167]: for n, arg in enumerate(expr.args): if p(1, 3) in arg: m = n break In [171]: expr.coeff(expr.args[m]) Out[171]: 0.5/(x**2*y**2) Much appreciated, Andy; thanks for this code snippet. However, what wo

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Nicholas Kinar
h[1]>>> from sympy.tensor import * h[1]>>> M=Indexed('M') h[1]>>> var('i j k', integer=1) (i, j, k) h[2]>>> eq=1+2*M(i,1)+3*j+4*M(i,2); eq 1 + 2*M(i, 1) + 3*j + 4*M(i, 2) h[3]>>> for a in eq.as_Add(): ... ie = [] ... co = [] ... for m in a.as_Mul(): ... if

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Aaron S. Meurer
You should also know about pre/post_order_traversal (both must be imported from sympy.utilities.iterables), which let you dig all the way down in an expression tree. In [109]: from sympy.utilities.iterables import preorder_traversal, postorder_traversal In [110]: preorder_traversal(sin(x**2) +

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Nicholas Kinar
You should also know about pre/post_order_traversal (both must be imported from sympy.utilities.iterables), which let you dig all the way down in an expression tree. In [109]: from sympy.utilities.iterables import preorder_traversal, postorder_traversal In [110]: preorder_traversal(sin(x**2

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Andy Ray Terrel
The problem with the is_Foo construct is that every basic has to carry it, so every object grows. The reason (maybe historic) there are some there is that its faster to ask is_Foo than to do isinstance a lot and the core needs some faster things. For bits that aren't checked very much we should tr

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Aaron S. Meurer
How does the speed compare if you use hasattr()? Also, is there a good reason that Basic doesn't have __getattr__() implemented? Aaron Meurer On Sep 23, 2010, at 7:42 PM, Andy Ray Terrel wrote: > The problem with the is_Foo construct is that every basic has to carry > it, so every object grows

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Ondrej Certik
On Thu, Sep 23, 2010 at 6:42 PM, Andy Ray Terrel wrote: > The problem with the is_Foo construct is that every basic has to carry > it, so every object grows.  The reason (maybe historic) there are some > there is that its faster to ask is_Foo than to do isinstance a lot and > the core needs some f

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Andy Ray Terrel
How would hasattr help you? It doesn't tell you if there is a instance of a class in the arg tree only if the instance has a specific attribute. I guess nobody has needed __getatrr__ yet. -- Andy On Thu, Sep 23, 2010 at 8:50 PM, Aaron S. Meurer wrote: > How does the speed compare if you use ha

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Aaron S. Meurer
Because, if you assume that each class has is_Class set to True for itself, you can just use getattr(object, 'is_Class', False) (sorry, I meant getattr(), not hasattr()). For __getattr__, you could set that in Basic and make it recognize is_Class. I don't know if that will still have the speed

Re: [sympy] Re: Iterating though the args of an expression

2010-09-23 Thread Andy Ray Terrel
It is supposedly slower. From basic.py: # NOTE NOTE NOTE # -- # # new-style classes + __getattr__ is *very* slow! # def __getattr__(self, name): # raise Warning('no way, *all* attribute access will be 2.5x slower') # here is what we do instead: fo

Re: [sympy] Re: Iterating though the args of an expression

2010-09-24 Thread Øyvind Jensen
to., 23.09.2010 kl. 18.54 -0700, skrev Ondrej Certik: > On Thu, Sep 23, 2010 at 6:42 PM, Andy Ray Terrel > wrote: > > The problem with the is_Foo construct is that every basic has to carry > > it, so every object grows. The reason (maybe historic) there are some > > there is that its faster to a