Skip to main content

agm

Function agm 

Source
pub fn agm<P, E, V>(a: V, b: V) -> V
where P: Policy, E: FloatElement, V: FloatVector<Element = E>,
Expand description

The arithmetic-geometric mean $\mathrm{AGM}(a, b)$ of two non-negative arguments.

The same recurrence agm_complete_ke runs, without that function’s E accumulator and without its a = 1 starting pin:

a_{n+1} = \frac{a_n + b_n}{2}, \qquad b_{n+1} = \sqrt{a_n b_n}

Both sequences converge to the common limit quadratically. From an extreme starting ratio the logarithm of that ratio roughly halves each pass until the two arguments are within a factor of a few, after which the correct digits double per pass, so the iteration cap covers the whole representable range with room to spare, and the loop stays uniform across lanes rather than data-dependent.

Kept beside agm_complete_ke on purpose: they are one recurrence, and a change to either is nearly always a change to both.

§Domain

Defined for a, b >= 0, and symmetric in its arguments. A negative argument makes the geometric mean’s sign ambiguous after the first pass ((a + b)/2 can be negative while sqrt(ab) is not), so negatives give NaN under check_overflow rather than a plausible wrong value. AGM(a, 0) = 0 for every a, a limit the iteration approaches but cannot reach (b is pinned at zero while a merely halves), so it is pinned too, as is AGM(inf, b) = inf, which otherwise leaves the loop as inf - inf. The one pairing with no limit at all, a zero against an infinity, is NaN.

§Range

sqrt(ab) is formed as a single product, so two arguments both above sqrt(MAX) (about 1.3e154 in binary64, 1.8e19 in binary32) overflow to infinity even though the mean itself is perfectly representable. The AGM is homogeneous, $\mathrm{AGM}(ca, cb) = c\,\mathrm{AGM}(a, b)$, so a caller working up there should scale both arguments by a common power of two, which is exact.

Last built: 2026-09-08 21:35:55 UTC