哪种编程语言或库可以处理无限级数?

发布于 2024-08-27 13:54:28 字数 292 浏览 5 评论 0原文

哪种编程语言或库能够处理无限级数(例如几何级数或调和级数)?它可能必须有一些众所周知的系列的数据库,并在收敛的情况下自动给出适当的值,并且可能在发散的情况下生成异常。

例如,在 Python 中,它可能看起来像:

sum  = 0
sign = -1.0
for i in range(1,Infinity,2):
     sign = -sign
     sum += sign / i

那么,sum 必须是 math.pi/4,而不在循环中进行任何计算(因为它是众所周知的和)。

Which programming language or a library is able to process infinite series (like geometric or harmonic)? It perhaps must have a database of some well-known series and automatically give proper values in case of convergence, and maybe generate an exception in case of divergence.

For example, in Python it could look like:

sum  = 0
sign = -1.0
for i in range(1,Infinity,2):
     sign = -sign
     sum += sign / i

then, sum must be math.pi/4 without doing any computations in the loop (because it's a well-known sum).

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

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

发布评论

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

评论(13

や三分注定 2024-09-03 13:54:29

一个值得一看的地方可能是维基百科的计算机代数系统类别。

One place to look might be the Wikipedia category of Computer Algebra Systems.

笑脸一如从前 2024-09-03 13:54:29

除了简单地支持无限列表之外,Haskell 中还有两种工具可以用于此目的。

首先有一个模块支持在 OEIS 中查找序列。这可以应用于级数的前几项,并可以帮助您识别您不知道其封闭形式的级数等。另一个是可计算实数的“CReal”库。如果您有能力对您的值生成不断改进的界限(即通过对前缀求和,您可以将其声明为允许部分排序的可计算实数等。在许多方面,这为您提供了一个可以的值 。

然而,一般来说,计算两个流的相等性需要一个预言机来解决停止问题,因此没有一种语言可以完全通用地完成您想要的事情,尽管像 Mathematica 这样的计算机代数系统可以尝试

There are two tools available in Haskell for this beyond simply supporting infinite lists.

First there is a module that supports looking up sequences in OEIS. This can be applied to the first few terms of your series and can help you identify a series for which you don't know the closed form, etc. The other is the 'CReal' library of computable reals. If you have the ability to generate an ever improving bound on your value (i.e. by summing over the prefix, you can declare that as a computable real number which admits a partial ordering, etc. In many ways this gives you a value that you can use like the sum above.

However in general computing the equality of two streams requires an oracle for the halting problem, so no language will do what you want in full generality, though some computer algebra systems like Mathematica can try.

纵山崖 2024-09-03 13:54:29

Maxima 可以计算一些无限和,但在这种特殊情况下,它似乎找不到答案 :-s

(%i1) sum((-1)^k/(2*k), k, 1, inf), simpsum;
                                 inf
                                 ====       k
                                 \     (- 1)
                                  >    ------
                                 /       k
                                 ====
                                 k = 1
(%o1)                            ------------
                                      2

但例如,这些工作:

(%i2) sum(1/(k^2), k, 1, inf), simpsum;
                                        2
                                     %pi
(%o2)                                ----
                                      6

(%i3) sum((1/2^k), k, 1, inf), simpsum;
(%o3)                                  1

Maxima can calculate some infinite sums, but in this particular case it doesn't seem to find the answer :-s

(%i1) sum((-1)^k/(2*k), k, 1, inf), simpsum;
                                 inf
                                 ====       k
                                 \     (- 1)
                                  >    ------
                                 /       k
                                 ====
                                 k = 1
(%o1)                            ------------
                                      2

but for example, those work:

(%i2) sum(1/(k^2), k, 1, inf), simpsum;
                                        2
                                     %pi
(%o2)                                ----
                                      6

(%i3) sum((1/2^k), k, 1, inf), simpsum;
(%o3)                                  1
放血 2024-09-03 13:54:29

您可以在 Sage(基于 Python 的免费数学软件系统)中解决级数问题,具体如下

sage: k = var('k'); sum((-1)^k/(2*k+1), k, 1, infinity)
1/4*pi - 1

:实际上是使用 Maxima(Sage 的一个组件)。

You can solve the series problem in Sage (a free Python-based math software system) exactly as follows:

sage: k = var('k'); sum((-1)^k/(2*k+1), k, 1, infinity)
1/4*pi - 1

Behind the scenes, this is really using Maxima (a component of Sage).

风月客 2024-09-03 13:54:29

对于 Python,请查看 SymPy - Mathematica 和 Matlab 的克隆。

还有一个更重的基于 Python 的数学处理工具,称为 Sage

For Python check out SymPy - clone of Mathematica and Matlab.

There is also a heavier Python-based math-processing tool called Sage.

薄荷梦 2024-09-03 13:54:29

你需要一些可以进行符号计算的东西,比如 Mathematica
您还可以考虑查询 wolframaplha: sum((-1)^i*1/i, i, 1 , inf)

You need something that can do a symbolic computation like Mathematica.
You can also consider quering wolframaplha: sum((-1)^i*1/i, i, 1 , inf)

心的位置 2024-09-03 13:54:29

有一个名为 mpmath(python) 的库,是 sympy 的一个模块,它提供了对 sympy 的系列支持(我相信它也支持 sage)。

更具体地说,所有系列内容都可以在这里找到:系列文档

There is a library called mpmath(python), a module of sympy, which provides the series support for sympy( I believe it also backs sage).

More specifically, all of the series stuff can be found here: Series documentation

情定在深秋 2024-09-03 13:54:29

C++ iRRAM 库准确地执行实际算术。除此之外,它可以使用 limit 函数精确计算限制。 iRRAM 的主页位于此处。查看文档中的 limit 函数。请注意,我不是在谈论任意精度算术。这是精确算术,是精确的合理定义。以下是他们精确计算 e 的代码,取自其网站上的示例:

//---------------------------------------------------------------------
// Compute an approximation to e=2.71.. up to an error of 2^p
 REAL e_approx (int p)
{
  if ( p >= 2 ) return 0;

  REAL y=1,z=2;
  int i=2;
  while ( !bound(y,p-1) ) {
    y=y/i;
    z=z+y;
    i+=1;
  }
  return z;
};

//---------------------------------------------------------------------
// Compute the exact value of  e=2.71.. 
REAL e()
{
  return limit(e_approx);
};

The C++ iRRAM library performs real arithmetic exactly. Among other things it can compute limits exactly using the limit function. The homepage for iRRAM is here. Check out the limit function in the documentation. Note that I'm not talking about arbitrary precision arithmetic. This is exact arithmetic, for a sensible definition of exact. Here's their code to compute e exactly, pulled from the example on their web site:

//---------------------------------------------------------------------
// Compute an approximation to e=2.71.. up to an error of 2^p
 REAL e_approx (int p)
{
  if ( p >= 2 ) return 0;

  REAL y=1,z=2;
  int i=2;
  while ( !bound(y,p-1) ) {
    y=y/i;
    z=z+y;
    i+=1;
  }
  return z;
};

//---------------------------------------------------------------------
// Compute the exact value of  e=2.71.. 
REAL e()
{
  return limit(e_approx);
};
﹏半生如梦愿梦如真 2024-09-03 13:54:29

ClojureHaskell 从我的脑海中消失。

抱歉,我找不到更好的 Haskell 序列链接,如果其他人有,请告诉我,我会更新。

Clojure and Haskell off the top of my head.

Sorry I couldn't find a better link to haskell's sequences, if someone else has it, please let me know and I'll update.

若水微香 2024-09-03 13:54:29

只需在您的计算机上安装 sympy 即可。然后执行以下代码:

from sympy.abc import i, k, m, n, x
from sympy import Sum, factorial, oo, IndexedBase, Function
Sum((-1)**k/(2*k+1), (k, 0, oo)).doit()

结果将是:pi/4

Just install sympy on your computer. Then do the following code:

from sympy.abc import i, k, m, n, x
from sympy import Sum, factorial, oo, IndexedBase, Function
Sum((-1)**k/(2*k+1), (k, 0, oo)).doit()

Result will be: pi/4

迟到的我 2024-09-03 13:54:29

我曾参与过几个用于研究目的的大数据系列。
我为此使用了 Matlab 。我不知道它可以/不能处理无限系列。

但我认为有这种可能性。
你可以试试:)

I have worked in couple of Huge Data Series for Research purpose.
I used Matlab for that. I didn't know it can/can't process Infinite Series.

But I think there is a possibility.
U can try :)

じ违心 2024-09-03 13:54:29

这可以在例如 sympy 和 sage(开源替代方案中)中完成。下面是使用 sympy 的一些示例:

在 [10] 中: summation(1/k**2,(k,1,oo))
输出[10]:
2
π
──
6

在[11]中:求和(1/k**4, (k,1,oo))
输出[11]:
4
π
──
90

在[12]中:求和( (-1)**k/k, (k,1,oo))
输出[12]:-log(2)

输入[13]:求和( (-1)**(k+1)/k, (k,1,oo))
Out[13]: log(2)

在幕后,这是使用超几何级数理论,一个很好的介绍是 Marko Petkovˇeks、Herbert S. Wilf 的书“A=B”
和 Doron Zeilberger,您可以通过谷歌搜索找到。 ¿什么是超几何级数?

大家都知道什么是几何级数:$X_1, x_2, x_3, \dots, x_k, \dots $ 是几何级数,如果连续项比率 $x_{k+1}/x_k$ 是常数。如果连续项比率是 $k$ 中的有理函数,那么它是超几何的! sympy 基本上可以处理满足最后一个条件的所有无限和,但只能处理很少的其他条件。

This can be done in for instance sympy and sage (among open source alternatives) In the following, a few examples using sympy:

In [10]: summation(1/k**2,(k,1,oo))
Out[10]:
2
π
──
6

In [11]: summation(1/k**4, (k,1,oo))
Out[11]:
4
π
──
90

In [12]: summation( (-1)**k/k, (k,1,oo))
Out[12]: -log(2)

In [13]: summation( (-1)**(k+1)/k, (k,1,oo))
Out[13]: log(2)

Behind the scenes, this is using the theory for hypergeometric series, a nice introduction is the book "A=B" by Marko Petkovˇeks, Herbert S. Wilf
and Doron Zeilberger which you can find by googling. ¿What is a hypergeometric series?

Everybody knows what an geometric series is: $X_1, x_2, x_3, \dots, x_k, \dots $ is geometric if the contecutive terms ratio $x_{k+1}/x_k$ is constant. It is hypergeometric if the consecutive terms ratio is a rational function in $k$! sympy can handle basically all infinite sums where this last condition is fulfilled, but only very few others.

站稳脚跟 2024-09-03 13:54:28

大多数延迟计算的函数式语言都可以模拟无限级数的处理。当然,我相信您知道,在有限计算机上不可能处理无限级数。从我的角度来看,我猜 Mathematica 可以做你想要的大部分事情,我怀疑 Maple 也可以,也许 Sage 和其他计算机代数系统,如果您找不到适合您的 Haskell 实现,我会感到惊讶。

编辑以澄清OP:我不建议生成无限循环。惰性求值允许您编写模拟无限级数的程序(或函数),这些程序本身在时间和空间上是有限的。使用此类语言,您可以以相当高的精度和一定程度的确定性确定模拟无穷级数的许多属性,例如收敛性。尝试Mathematica,或者,如果您无法访问它,请尝试Wolfram Alpha 查看一个系统可以为您做什么。

Most functional languages which evaluate lazily can simulate the processing of infinite series. Of course, on a finite computer it is not possible to process infinite series, as I am sure you are aware. Off the top of my head, I guess Mathematica can do most of what you might want, I suspect that Maple can too, maybe Sage and other computer-algebra systems and I'd be surprised if you can't find a Haskell implementation that suits you.

EDIT to clarify for OP: I do not propose generating infinite loops. Lazy evaluation allows you to write programs (or functions) which simulate infinite series, programs which themselves are finite in time and space. With such languages you can determine many of the properties, such as convergence, of the simulated infinite series with considerable accuracy and some degree of certainty. Try Mathematica or, if you don't have access to it, try Wolfram Alpha to see what one system can do for you.

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