python 中的高斯-勒让德算法

发布于 2024-07-09 14:10:03 字数 693 浏览 6 评论 0原文

我需要一些帮助来计算 Pi。 我正在尝试编写一个 python 程序来将 Pi 计算为 X 位数字。 我已经尝试了 python 邮件列表中的几个,但它对我的使用来说太慢了。 我已经阅读过有关 Gauss-Legendre 算法 的内容,并且我尝试将其移植到 Python 中没有成功。

我正在阅读此处,如果有任何关于我在哪里的意见,我将不胜感激出问题了!

它输出:0.163991276262

from __future__ import division
import math
def square(x):return x*x
a = 1
b = 1/math.sqrt(2)
t = 1/4
x = 1
for i in range(1000):
    y = a
    a = (a+b)/2
    b = math.sqrt(b*y)
    t = t - x * square((y-a))
    x = 2* x

pi = (square((a+b)))/4*t
print pi
raw_input()

I need some help calculating Pi. I am trying to write a python program that will calculate Pi to X digits. I have tried several from the python mailing list, and it is to slow for my use.
I have read about the Gauss-Legendre Algorithm, and I have tried porting it to Python with no success.

I am reading from Here, and I would appreciate any input as to where I am going wrong!

It outputs: 0.163991276262

from __future__ import division
import math
def square(x):return x*x
a = 1
b = 1/math.sqrt(2)
t = 1/4
x = 1
for i in range(1000):
    y = a
    a = (a+b)/2
    b = math.sqrt(b*y)
    t = t - x * square((y-a))
    x = 2* x

pi = (square((a+b)))/4*t
print pi
raw_input()

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

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

发布评论

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

评论(3

昵称有卵用 2024-07-16 14:10:03
  1. 您忘记了 4*t 两边的括号:

    pi = (a+b)**2 / (4*t) 
      
  2. 您可以使用decimal进行更高精度的计算。

    #!/usr/bin/env python 
      从 __future__ 导入 with_statement 
      导入小数 
    
      def pi_gauss_legendre(): 
          D = 小数.小数 
          以decimal.localcontext()作为ctx: 
              ctx.prec += 2                 
              a、b、t、p = 1、1/D(2).sqrt()、1/D(4)、1                 
              圆周率 = 无 
              而1: 
                  安 = (a + b) / 2 
                  b = (a * b).sqrt() 
                  t -= p * (a - an) * (a - an) 
                  a, p = an, 2*p 
                  piold = pi 
                  pi = (a + b) * (a + b) / (4 * t) 
                  if pi == piold: # 在给定精度内相等 
                      休息 
          返回+pi 
    
      小数.getcontext().prec = 100 
      打印 pi_gauss_legendre() 
      

输出:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208\
    998628034825342117068
  1. You forgot parentheses around 4*t:

    pi = (a+b)**2 / (4*t)
    
  2. You can use decimal to perform calculation with higher precision.

    #!/usr/bin/env python
    from __future__ import with_statement
    import decimal
    
    def pi_gauss_legendre():
        D = decimal.Decimal
        with decimal.localcontext() as ctx:
            ctx.prec += 2                
            a, b, t, p = 1, 1/D(2).sqrt(), 1/D(4), 1                
            pi = None
            while 1:
                an    = (a + b) / 2
                b     = (a * b).sqrt()
                t    -= p * (a - an) * (a - an)
                a, p  = an, 2*p
                piold = pi
                pi    = (a + b) * (a + b) / (4 * t)
                if pi == piold:  # equal within given precision
                    break
        return +pi
    
    decimal.getcontext().prec = 100
    print pi_gauss_legendre()
    

Output:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208\
    998628034825342117068
百变从容 2024-07-16 14:10:03
  1. 如果要计算 PI 到 1000 位,则需要使用支持 1000 位精度的数据类型(例如,mxNumber)
  2. 你需要计算a、b、t和x直到 |ab| < 10**位,不重复数字次数。
  3. 按照 @JF 建议计算平方和 pi。
  1. If you want to calculate PI to 1000 digits you need to use a data type that supports 1000 digits of precision (e.g., mxNumber)
  2. You need to calculate a,b,t, and x until |a-b| < 10**-digits, not iterate digits times.
  3. Calculate square and pi as @J.F. suggests.
两相知 2024-07-16 14:10:03
pi = (square((a+b)))/4*t

应该

pi = (square((a+b)))/(4*t)
pi = (square((a+b)))/4*t

should be

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