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 中位置对应的幂. x1x2 必须可以广播到相同的形状. 这与 power 函数不同,因为整数,float16 和 float32 会被提升为精度至少为 float64 的浮点数,以便结果始终是不精确的. 意图是该函数将返回负幂的可用结果,并且很少溢出正幂.

提升为非整数值的负值将返回 nan . 要获得复数结果,请将输入转换为复数,或将 dtype 指定为 complex (请参见下面的示例).

参数:
x1array_like

底数.

x2array_like

指数. 如果 x1.shape != x2.shape ,则它们必须可广播到公共形状(这将成为输出的形状).

outndarray, None, or tuple of ndarray and None, optional

存储结果的位置.如果提供,则它必须具有输入广播到的形状.如果未提供或为None,则返回一个新分配的数组.一个元组(可能仅作为关键字参数)的长度必须等于输出的数量.

其中类数组,可选

此条件会在输入上进行广播.在条件为True的位置, out 数组将被设置为ufunc结果.否则, out 数组将保留其原始值.请注意,如果通过默认的 out=None 创建一个未初始化的 out 数组,则其中条件为False的位置将保持未初始化.

\kwargs

对于其他仅限关键字的参数,请参见 ufunc docs .

返回:
yndarray

x1 中的底数提升为 x2 中的指数. 如果 x1x2 都是标量,则这是一个标量.

参见

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])