On May 31, 2011, at 11:11 AM, Ronan Lamy wrote:

> Le mardi 31 mai 2011 à 09:43 -0700, Vinzent Steinberg a écrit :
>> On May 30, 9:52 pm, Mateusz Paprocki <matt...@gmail.com> wrote:
>>> That could work:
>>> 
>>> ZZ.sum([1, 2, 3]) -> sum([1, 2, 3])
>>> RR.sum([1.0, 2.0]) -> mpmath.fsum([1.0, 2.0])
>>> EX.sum([x, y, z]) -> Add(*[x, y, z])
>>> 
>>> etc.
>> 
>> This is exactly what I have been thinking of.
> 
> But do we really need lots of different sum() functions? Is there a
> difference between ZZ.sum([1, 2, 3]) and EX.sum([1, 2, 3])?

Yes.  You can sum integers perfectly efficiently just by passing them to the 
built-in sum() (also remember that ZZ(1) != S(1) in general because of the 
ground types, but EX(1) == S(1) always).  But something like Add(*(x, -x, x, x, 
-x)) is almost two times faster than sum((x, -x, x, x, -x)):

In [1]: Add(*(x, -x, x, x, -x))
Out[1]: x

In [2]: %timeit Add(*(x, -x, x, x, -x))
1000 loops, best of 3: 170 us per loop

In [3]: sum((x, -x, x, x, -x))
Out[3]: x

In [4]: %timeit sum((x, -x, x, x, -x))
1000 loops, best of 3: 313 us per loop

In [16]: b = map(S, range(100))

In [24]: %timeit Add(*b)
1000 loops, best of 3: 546 us per loop

In [25]: %timeit sum(b)
1000 loops, best of 3: 268 us per loop

(all run without the cache, as suggested above).  Note that [16] ensures that 
sum(b) goes through Add.  Otherwise, you get

In [26]: %timeit Add(*range(100))
1000 loops, best of 3: 1.45 ms per loop

In [27]: %timeit sum(range(100))
100000 loops, best of 3: 2.7 us per loop

which just proves what we already know, which is that Python ints are 
(apparently 10x) faster than SymPy Integers.  

Aaron Meurer

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