scipy.special.agm#
- scipy.special.agm(a, b, out=None) = <ufunc 'agm'>#
计算 a 和 b 的算术-几何平均值。
从 a_0 = a 和 b_0 = b 开始,并迭代计算:
a_{n+1} = (a_n + b_n)/2 b_{n+1} = sqrt(a_n*b_n)
随着 n 的增加,a_n 和 b_n 趋向于相同的极限;它们的共同极限是 agm(a, b)。
- 参数:
- a, barray_like
仅限实数值。如果两个值都是负数,结果为负数。如果一个值为负数,另一个为正数,则返回 nan。
- 出ndarray,可选
函数值的可选输出数组
- 返回:
- 标量或ndarray
a 和 b 的算术-几何平均值。
示例
>>> import numpy as np >>> from scipy.special import agm >>> a, b = 24.0, 6.0 >>> agm(a, b) 13.458171481725614
将该结果与迭代进行比较:
>>> 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
当给出类似数组的参数时,广播机制适用:
>>> 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]])