在 python 中绘制总和

发布于 2024-11-09 04:00:07 字数 688 浏览 0 评论 0原文

我正在尝试在 python 中绘制一个具有无限总和的函数。现在,由于计算中没有无穷大,对于我的上限,我可以选择一个非常大的数字,这很好。

所以我尝试绘制它:

from scitools.std import *
from math import *
import numpy as np

def f1(t):
    return 0.5*(1+sum((4**(2*n)*cos(2*n*t))/(e**16*factorial(n)) for n in xrange(0,10**100)))

t = linspace(0, 35, 10000)
y1 = f1(t)

plot(t, y1)

xlabel(r'$\tau$')
ylabel(r'P($\tau$)')
legend(r'P($\tau$)')
grid(True)

我尝试减少 xrange (或范围),并增加 linspace (从 0, 35 超过 1000 个点),但我得到的是:

OverflowError: long int too large to convert to int

OverflowError: range() result has too many items

那么问题似乎是什么这里?怎样才能让这笔钱变大呢?求和的语法正确吗?

I'm trying to plot a function in python that has infinite sum in it. Now, since there are no infinities in computing, for my upper bound I can choose a really big number, and that's fine.

So I tried to plot it:

from scitools.std import *
from math import *
import numpy as np

def f1(t):
    return 0.5*(1+sum((4**(2*n)*cos(2*n*t))/(e**16*factorial(n)) for n in xrange(0,10**100)))

t = linspace(0, 35, 10000)
y1 = f1(t)

plot(t, y1)

xlabel(r'$\tau

And I tried to decrease the xrange (or range), and increase the linspace (from 0, 35 in more than 1000 points), but I'm getting either:

OverflowError: long int too large to convert to int

or

OverflowError: range() result has too many items

So what seems to be the problem here? How can I make the sum big? Is the syntax of the sum correct?

) ylabel(r'P($\tau$)') legend(r'P($\tau$)') grid(True)

And I tried to decrease the xrange (or range), and increase the linspace (from 0, 35 in more than 1000 points), but I'm getting either:

or

So what seems to be the problem here? How can I make the sum big? Is the syntax of the sum correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-11-16 04:00:07

这个循环不可能在你的一生中结束。 10 ** 100 是一个非常巨大的数字。它比宇宙中粒子的数量还多,比宇宙诞生以来经过的最微小时间段的数量还要多。在速度快得令人难以置信的计算机上 - 3 * 10 ** 46 千年才能完成循环。要计算无限总和,您希望计算直到总和停止显着变化(例如,被加数已落入某个非常小的阈值)。

另外,Python 2 中的 xrange 和 range 仅限于平台的长整数,这意味着在 32 位机器上不能有高于 2 ** 31 的数字和 2 ** 63 在 64 位上,(后者仍然太大,无法在您的一生中完成),这就是为什么您在 Python 2 中得到 OverflowError 。在 Python 3 中,您会不会出现错误,但求和将永远持续下去。

而且如此大的数字的阶乘计算速度甚至更慢,因此即使在 32 位计算机上,您也没有机会超过最大值。

搜索一个用于计算无限和的函数,或者自己做

>>> from __future__ import division
>>> import itertools
>>> from math import factorial, cos, e
>>> for t in [0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1]:
...     summables = ((4 ** (2 * n) * cos(2 * n * t)) / (e ** 16 * factorial(n)) 
...                  for n in itertools.count())
...     print 0.5 * (1 + sum(itertools.takewhile(lambda x: abs(x) > 1e-80, summables)))
... 
1.0
0.973104754771
0.89599816753
0.77928588758
0.65382602277
0.569532373683
0.529115621076
0.512624956755
0.505673516974
0.502777962546
0.501396442319

另外,我不认识该公式,但这应该是 (e ** 16) * Factorial(n)e ** (16 * 阶乘(n))?我只是想指出,您因为另一个答案而写了前者。

That loop can't possibly end during your lifetime. 10 ** 100 is a really really enormous number. It's bigger than the number of particles in the universe, it's bigger than the number of the tiniest time periods that have passed since the creation of the universe. On an impossibly fast computer - 3 * 10 ** 46 millennia for the loop to complete. To calculate an infinite sum you'd wish to calculate until the sum has stopped changing significantly (e.g. the summands have fallen under certain very small threshold).

Also, xrange and range in Python 2 are limited to the platform's long integers, which means that you can't have numbers higher than 2 ** 31 on a 32 bit machine and 2 ** 63 on a 64 bit one, (the latter is still too big to ever complete in your lifetime), this is why you get an OverflowError in Python 2. In Python 3 you'd get no error, but the summation will continue forever.

And calculation factorial of such a large number is even slower, so you don't have a chance to ever exceed the maximum even on a 32 bit machine.

Search for a function for calculating infinite sums, or do it yourself

>>> from __future__ import division
>>> import itertools
>>> from math import factorial, cos, e
>>> for t in [0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1]:
...     summables = ((4 ** (2 * n) * cos(2 * n * t)) / (e ** 16 * factorial(n)) 
...                  for n in itertools.count())
...     print 0.5 * (1 + sum(itertools.takewhile(lambda x: abs(x) > 1e-80, summables)))
... 
1.0
0.973104754771
0.89599816753
0.77928588758
0.65382602277
0.569532373683
0.529115621076
0.512624956755
0.505673516974
0.502777962546
0.501396442319

Also, I do not recognize the formula, but is this supposed to be (e ** 16) * factorial(n) or e ** (16 * factorial(n))? I just want to point out that you've written the former because of the other answer.

怀中猫帐中妖 2024-11-16 04:00:07

编辑:误读括号

你的总和中有 1/n! 。这意味着项衰减得非常非常快,因此没有必要使总和达到 10*100:尝试改为 100,这对您来说应该是一个非常好的上限。事实上,尝试计算出这种数量级的项是荒谬的,因为这意味着计算机必须计算出 (10**100)!

EDIT: misread the parentheses

Your sum has 1/n! in it. That means that the terms decay REALLY REALLY REALLY fast, so there's no need whatsoever to make the sum go up to 10*100: try instead 100, which should be a perfectly good upper limit for you. In fact, it's absurd to try to work out the terms up to that kind of order of magnitude, because it means the computer has to work out (10**100)!.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文