On Thu, Mar 4, 2010 at 10:21 AM, Ondrej Certik <ond...@certik.cz> wrote: > On Thu, Mar 4, 2010 at 10:10 AM, Chad File <archeryguru2...@gmail.com> wrote: >> Ondrej, thanks for the reply. >> >> What I mean is exactly that, i%2. If 'i' is even, i%2 = 0, if 'i' is odd, >> i%2 = 1. How else would one write that? >> >> I have a very large program that actually uses this 'remainder' operator >> many times. However, only in the summation do I have any problems with it. >> I'm currently using the below line in place of what I want. >> >> In [1]: mpmath.calculus.nsum(lambda i: (i-i%2)/2, [1,10]) >> Out[1]: 25.0 >> >> I haven't timed it to check which is faster (cannot time what I cannot >> compute), but I would prefer to do away with the mpmath dependence on the >> summation, if possible. > > > Got it. The % operator isn't implemented for symbols, as you can see here: > > In [1]: var("i") > Out[1]: i > > In [2]: i%2 > --------------------------------------------------------------------------- > TypeError Traceback (most recent call last) > > /home/ondrej/repos/sympy/<ipython console> in <module>() > > TypeError: unsupported operand type(s) for %: 'Symbol' and 'int' > > > So it'd had to be implemented and then the Sum() should be improved to > take this into account. A workaround is to sum over odd and even "i"s > separately and use a substitution i = 2*k and i=2*k+1, e.g.: > > sum_i = sum_i /even/ + sum_i /odd/ = Sum(2*k/2, (k, 1, 5)) + > Sum((2*k+1-1)/2, (k, 0, 4)) > > and when you doit: > > In [6]: (Sum(2*k/2, (k, 1, 5)) + Sum((2*k+1-1)/2, (k, 0, 4))).doit() > Out[6]: 25 > > > Maybe you can improve your program to do the substitution for you.
Better thing is to use a substitution i=2*k-1 for the odd "i"s, because then you can make these equivalent operations: In [7]: (Sum(2*k/2, (k, 1, 5)) + Sum((2*k-1-1)/2, (k, 1, 5))).doit() Out[7]: 25 In [8]: Sum(2*k/2+(2*k-1-1)/2, (k, 1, 5)).doit() Out[8]: 25 And in general, the procedure is: Sum(f(i), (i, 1, 2*n)) = Sum(f(2*k)+f(2*k-1), (k, 1, n)) where f(i) = (i-i%2)/2 and n=5 in your case. Ondrej -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sy...@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.