开发者

Infinite sums in python

I have heard that python can do infinite sums. For instance if I want to evaluate the infinite sum:

1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...

H开发者_运维技巧ow should I go about? I am a newbie to python. So I would appreciate if someone could write out the entire code and if I need to include/import something.

For instance, in wolfram alpha if I input Summation (-1)^(n-1)/(2*n-1) from n=1 to infinity it gives the answer as 0.785395. I want the answer computed to a desired accuracy say, as in the case of wolfram alpha upto 6 digits.

Further, I was looking at this post here and tried to mimic that but it gives me the following errors:

`NameError: name 'Infinity' is not defined`
`NameError: name 'Inf' is not defined`

Thanks, Adhvaitha


While it still is finite, you can approximate that series using the fractions and decimal modules:

from fractions import Fraction
from decimal import Decimal

repetitions = 100

d = 1
r = Fraction(1, d)

for n in range(repetitions):
    r += Fraction(1, d) - Fraction(1, d + 2)
    d += 4

print(Decimal(r.numerator)/Decimal(r.denominator))

I think this comes closest to what you want to do.


Python has unlimited precision integers, but not unlimited precision floats. There are packages you can use that provide that, though.

And nothing can "complete" an infinite sum, since it involves an infinite number of steps. You'll need to find a closed form for the sum, and then evaluate that, or accept an approximation achieved by terminating the infinite sum when a precision criterion is met.


A bit of a shot in the dark here... I bet when you heard about Python being able to do infinite sums, what they meant was that in Python long integers have unlimited precision.

Clearly, this has nothing to do with summing infinite series.

I am not aware of any facet of Python that would make it particularly well suited to computing this kind of sums (or indeed establishing whether a sum is convergent).

You could try the direct summation of terms, with some reasonable stopping criterion. However, this will only work for well-behaved series.

Finally, just to give you some flavour of the complexity of what you're asking for, academic papers get published whose sole purpose is to deal with the summation of certain small classes of series. The general problem that you're posing isn't as easy as it may seem.


#It may be late answer but the following works well.

repetitions = 50

r = 0.0

for i in range(repetitions):
   ii=i+1 # because in python index start from 0
   r+=((-1)**(ii-1))/(2*ii-1.0)

print r

#the output is r=0.780398663148, you can increase the repetitions for more accuracy


For some series, such as the one shown, you can use the alternating series test to compute the sum to within a desired error. Libraries such as Decimal, GyPy, mpmath, or bigfloat, etc, can be used if your calculation will bump into the precision of built-in floats.

Note on integer approaches:
Although the ratio-of-integers approachs seems more accurate, they are completely impractical for real calculations. The reason for this is: 1) adding fractions requires creating equal denominators, and this basically requires multiplying the denominators, so by then end, the size of the numbers is something like n! (i.e., the factorial); and, 2) for the example series, a precision of m digits requires m terms. Therefore, even for only six digit accuracy, one requires numbers roughly equal to 1000000! = 8×105,565,708. For bigger numbers, it's roughly 1010n, which quickly becomes completely impractical. Meanwhile, a decimal solution calculated to 6 or 7 or even 40 digits is trivial.

For example, running nightcrackers solution, the times and numbers of digits in the denominator or numerator I get are:

    n      t       n_digits_in_denominator
   10      0.0003  14
  100      0.0167  170
 1000      5.5027  1727
10000      ????    ????   (gave up after waiting one hour)

And this becomes impractical for only ~4 digits of accuracy.

So if you want to exactly calculate a finite and small number of terms and express the final result as a ratio, then the integer solutions would be a good choice, but if you want to express the final result as a decimal, you'd be better off just sticking with decimals.


You can do it using sympy like that:

from sympy import oo, Sum
from sympy.abc import n
inf_sum = Sum((-1)**n/(2*n + 1), (n, 0, oo))    # n goes from 0 to infinity
print(inf_sum.doit())

This will give you pi/4 that is the same that you got from Wolfram Alpha.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜