2013-04-10 14:58:50 -07:00
|
|
|
from math import sqrt
|
|
|
|
|
|
|
|
|
|
def agm(a0, g0, tolerance=1e-10):
|
|
|
|
|
"""
|
|
|
|
|
Calculating the arithmetic-geometric mean of two numbers a0, g0.
|
|
|
|
|
|
|
|
|
|
tolerance the tolerance for the converged
|
|
|
|
|
value of the arithmetic-geometric mean
|
|
|
|
|
(default value = 1e-10)
|
|
|
|
|
"""
|
2014-04-02 16:56:35 +00:00
|
|
|
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
|
2013-04-10 14:58:50 -07:00
|
|
|
while abs(an - gn) > tolerance:
|
2014-04-02 16:56:35 +00:00
|
|
|
an, gn = (an + gn) / 2.0, sqrt(an * gn)
|
2013-04-10 14:58:50 -07:00
|
|
|
return an
|
|
|
|
|
|
|
|
|
|
print agm(1, 1 / sqrt(2))
|