numpy.float_power#
- numpy.float_power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'float_power'>#
第一个数组的元素提升为第二个数组中对应位置的幂,逐元素操作。
将 x1 中的每个基数提升为 x2 中位置对应的幂。 x1 和 x2 必须能够广播到相同的形状。这与 power 函数不同,因为它会将整数、float16 和 float32 提升为至少 float64 精度的浮点数,因此结果总是不精确的。其目的是使该函数能够为负幂返回可用结果,并且很少对正幂溢出。
负值提升为非整数值时将返回
nan
。要获得复数结果,请将输入转换为复数,或将dtype
指定为complex
(参见下面的示例)。- 参数:
- x1类数组
基数。
- x2类数组
指数。如果
x1.shape != x2.shape
,它们必须能够广播到共同的形状(这将成为输出的形状)。- outndarray、None,或由 ndarray 和 None 组成的元组,可选
存储结果的位置。如果提供,其形状必须与输入广播后的形状一致。如果未提供或为 None,则返回新分配的数组。元组(只能作为关键字参数)的长度必须等于输出的数量。
- where类数组,可选
此条件会在输入上进行广播。在条件为 True 的位置,out 数组将被设置为 ufunc 的结果。在其他位置,out 数组将保留其原始值。请注意,如果通过默认的
out=None
创建了未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化状态。- **kwargs
有关其他仅限关键字的参数,请参阅 ufunc 文档。
- 返回:
- yndarray
将 x1 中的基数提升为 x2 中的指数。如果 x1 和 x2 都是标量,则结果为标量。
另请参阅
power
保留类型的 power 函数
示例
>>> import numpy as np
对列表中每个元素求立方。
>>> x1 = range(6) >>> x1 [0, 1, 2, 3, 4, 5] >>> np.float_power(x1, 3) array([ 0., 1., 8., 27., 64., 125.])
将基数提升为不同的指数。
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] >>> np.float_power(x1, x2) array([ 0., 1., 8., 27., 16., 5.])
广播的效果。
>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) >>> x2 array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) >>> np.float_power(x1, x2) array([[ 0., 1., 8., 27., 16., 5.], [ 0., 1., 8., 27., 16., 5.]])
负值提升为非整数值将导致
nan
(并会生成警告)。>>> x3 = np.array([-1, -4]) >>> with np.errstate(invalid='ignore'): ... p = np.float_power(x3, 1.5) ... >>> p array([nan, nan])
要获得复数结果,请提供参数
dtype=complex
。>>> np.float_power(x3, 1.5, dtype=complex) array([-1.83697020e-16-1.j, -1.46957616e-15-8.j])