Then iterate this operation with a1 taking the place of x and g1 taking the place of y. In this way, two sequences (an) and (gn) are defined:
These two sequences converge to the same number, which is the arithmetic-geometric mean of x and y; it is denoted by M(x, y), or sometimes by agm(x, y).
and then iterate as follows:
The first four iterations give the following values:
The arithmetic-geometric mean of 24 and 6 is the common limit of these two sequences, which is approximately 13.45817148173.
If r > 0, then M(rx, ry) = r M(x, y).
There is a closed form expression for M(x,y):
where K(x) is the complete elliptic integral of the first kind.
named after Carl Friedrich Gauss.
The geometric-harmonic mean can be calculated by an analogous method, using sequences of geometric and harmonic means. The arithmetic-harmonic mean can be similarly defined, but takes the same value as the geometric mean.
from math import sqrt
def avg(a, b, delta=None):if None==delta:delta=(a+b)/2*1E-10if(abs(b-a)>delta):return avg((a+b)/2.0, sqrt(a*b), delta)else:return (a+b)/2.0