On Sep 10, 2010, at 11:19 PM, Bill Hart wrote:

> What isn't clear is how expressions like
> 
>  2*x + 3*y + 4*z
> 
> get handled without creating intermediate classes.   Do the Add and
> Mult classes combine subexpressions together within the __new__
> method?

I think it is impossible to create that all in one fell-swoop in Python just by 
typing 2*x + 3*y + 4*z because everything is handled by Python's operator 
evaluation precedence.  So in this case, Python will evaluate 2*x, 3*x, 2*x + 
3*x, 4*z, (2*x + 3*x) + 4*z, in that order, and there is really no way to make 
it do otherwise.  

Now, I suppose we could attempt to make sympify() smarter when passed a string 
form of an expression (sympify('2*x + 3*y + 4*z')), but right now, it isn't 
(more or less, it just calls ast parse, which does the same thing as regular 
Python except it converts x, y, and z into Symbols first) .  I don't know how 
worth it it would be to make it smarter, since the string version is rarely 
used compared to the standard version.

Aaron Meurer

> 
> On Sep 10, 5:10 pm, Ondrej Certik <[email protected]> wrote:
>> On Fri, Sep 10, 2010 at 3:40 PM, Bill Hart <[email protected]> wrote:
>>> I recently viewed Ondrej's 2007 presentation on SymPy on the web:
>>> http://www.google.com/url?sa=t&source=web&cd=6&ved=0CCoQFjAF&url=http....
>>> ]
>> 
>>> I'm curious about the details of the expression management in sympy,
>>> which are discussed on slide 14.  Ondrej notes that the sympy strategy
>>> avoids the creation of intermediate classes, which I think is a great
>>> idea.  However, I don't quite see how this is done when I look at the
>>> code.  Can you provide more details about how this is achieved?
>> 
>> We use the __new__ method of Python classes, which is responsible for
>> classes creation, and so if you do:
>> 
>> a-a
>> 
>> Add.__new__() get's called and inside that class it gets decided to
>> create a class Integer(0) instead (without creating the intermediate
>> Add class), in this case, "0" is also cached, but that's a detail.
>> 
>> 
>> 
>>> I'm interested because I've run into performance bottlenecks in my
>>> Coopr software (specifically in coopr.pyomo), where I'm building
>>> algebraic expressions and I intermediate class construction is a major
>>> bottleneck!
>> 
>> Ondrej
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to