scipy.special.agm#

scipy.special.agm(a, b, out=None) = <wrapped_ufunc 'agm'>#

Compute the arithmetic-geometric mean of a and b.

Start with \(a_0 = a\) and \(b_0 = b\) and iteratively compute

\[a_{n+1} = \frac{a_n + b_n}{2}, \quad b_{n+1} = \sqrt{a_n b_n}.\]

\(a_n\) and \(b_n\) converge to the same limit as \(n\) increases; their common limit is \(\operatorname{agm}(a, b)\).

Parameters:
a, barray_like

Real values only. If the values are both negative, the result is negative. If one value is negative and the other is positive, nan is returned.

outndarray, optional

Optional output array for the function values

Returns:
scalar or ndarray

The arithmetic-geometric mean of \(a\) and \(b\).

Notes

Array API Standard Support

agm has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

✅

n/a

CuPy

n/a

⛔

PyTorch

✅

⛔

JAX

✅

⛔

Dask

✅

n/a

For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support out, but typically none of the other ufunc kwargs. out is typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend. out is never supported for JAX because JAX arrays are immutable. agm does not currently support out for the PyTorch backend.

See Array API coverage for more information.

Examples

>>> import numpy as np
>>> from scipy.special import agm
>>> a, b = 24.0, 6.0
>>> agm(a, b)
13.458171481725614

Compare that result to the iteration:

>>> while a != b:
...     a, b = (a + b)/2, np.sqrt(a*b)
...     print("a = %19.16f  b=%19.16f" % (a, b))
...
a = 15.0000000000000000  b=12.0000000000000000
a = 13.5000000000000000  b=13.4164078649987388
a = 13.4582039324993694  b=13.4581390309909850
a = 13.4581714817451772  b=13.4581714817060547
a = 13.4581714817256159  b=13.4581714817256159

When array-like arguments are given, broadcasting applies:

>>> a = np.array([[1.5], [3], [6]])  # a has shape (3, 1).
>>> b = np.array([6, 12, 24, 48])    # b has shape (4,).
>>> agm(a, b)
array([[  3.36454287,   5.42363427,   9.05798751,  15.53650756],
       [  4.37037309,   6.72908574,  10.84726853,  18.11597502],
       [  6.        ,   8.74074619,  13.45817148,  21.69453707]])