Indeed, here is how to do your sum: In [15]: Sum((i-((-1)**(i+1) + 1)/2)/2, (i, 1, 10)).doit() Out[15]: 25
e.g. just replace i%2 by ((-1)**(i+1) + 1)/2 and things just work. Thanks Aaron! Ondrej On Thu, Mar 4, 2010 at 12:30 PM, Chad File <archeryguru2...@gmail.com> wrote: > Thank Aaron, > I'm not sure why I didn't think of (-1)**i in the first place (or the > cos/sin equivalent). I have to run for now, but tomorrow morning I'll give > that a shot. > > Thanks, > -- > ~~archery~~ > > Aaron S. Meurer wrote: > > You are making your life unnecessarily difficult. The standard way to > alternate between > numbers is to use (-1)**i. In this case, you could do ((-1)**(i+1) + 1)/2. > Another option > is to use sin or cos, for example, here you could use abs(sin(pi/2*i)). > > With that being said, maybe we should create a mod object and have x%y > return it. > > Aaron Meurer > On Mar 4, 2010, at 1:17 PM, Ondrej Certik wrote: > > > > On Thu, Mar 4, 2010 at 11:40 AM, Chad File <archeryguru2...@gmail.com> > wrote: > > > Ondrej, > The only opportunity with the below statement > > Sum(f(i), (i, 1, 2*n)) = Sum(f(2*k)+f(2*k-1), (k, 1, n)) > > is that 2*n would have to be even. And in my case it is certainly not > always true. This summation is part of a function that is called man > > > You can fix that easily for having 2*n+1 as the upper limit too (then > you'll cover all cases). Do you know how to do that, or should I try > it? > > > > hundreds of times. And each time it is called my limit is a different > number... sometimes even, but sometime not. In fact my limit is a function > itself that also uses the '%' operator. > > return (...) + int(mpmath.calculus.nsum(lambda i: (i-i%2)/2, [1,x-Mx%2])) > > where x and Mx are positive integers. I need an integer as a return from > this function, hence the int() conversion. The Sum() function will leave > the value in integer form and is much cleaner and easier to read than the > above nsum() over the iterated lambda function. > > Either way, I tried the f(i) -> f(2*k) + f(2*k-1) substitution. And the > results were inaccurate when x-Mx%2 was odd. If I keep modifying my Sum() > > > Can you give me an example? It is mathematically equivalent, so I > don't understand how it can be inaccurate. > > > > command to account for these discrepancies I believe I will lose the > readability that I'm searching for. > > > What exactly is your use case? Do you need a simple sympy command to > calculate your sum, or do you need to just calculate the sum somehow > (but precisely). > > > > Thanks anyway for all the assistance. I was just hoping there was a simple > switch or rewrite that I was overlooking to accept the '%' operator. > > > I think it is not difficult to implement the above substitution, so > that it happens automatically. After I understand your use case in > more details, I can help you with that, if you are interested. > > 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. > > > > > > -- > 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. > -- 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.