The arguments of Rational have to be literal integers (Python ints or SymPy Integers). See http://docs.sympy.org/gotchas.html#python-numbers-vs-sympy-numbers (by the way Ondrej, when does that become updated with what you just merged?). So 2 and 3 are correct, while 1 and 4 are incorrect.
Rational is just a class to hold things like 1/3 so they don't get evaluated. Stuff like 1/n goes in the Mul class, which happens automatically with the '/' operator. If you insist of using class calls, you can do Pow(n, -1) for 1/n (this is how it is represented internally), or Mul(2, Pow(n, -1)) for 2/n (again, this is how it is represented internally. Look at (2/n).args). Aaron Meurer On Jul 30, 2009, at 6:31 PM, Phillip M. Feldman wrote: > > Actually, n was declared to be an integer. Consider the following > four sums: > > #1: sum(1/3,(n,1,3)) > #2: sum(Rational(1,3),(n,1,3)) > #3: sum(1/n,(n,1,3)) > #4: sum(Rational(1,n),(n,1,3)) > > #s 2 and 3 work, while #s 1 and 4 don't. This seems strange. I > understand that #1 gives a result of zero because of the way that > Python handles integer division, but #4 really should work. > >> The argument of the Rational class must be an integer. Use just an >> expression with symbols, then it works as expected: >> >> In [1]: sum(1/n, (n, 2, 4)) >> Out[1]: >> 13 >> ── >> 12 >> >> 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 -~----------~----~----~----~------~----~------~--~---
