通用函数 (cupy.ufunc)#

提示

NumPy API 参考:通用函数 (numpy.ufunc) <https://numpy.org/doc/stable/reference/ufuncs.html>`_

CuPy 提供了通用函数(也称为 ufunc)来支持各种元素级操作。CuPy 的 ufunc 支持 NumPy 的以下特性:

  • 广播

  • 输出类型确定

  • 转换规则

ufunc#

ufunc(name, nin, nout, _Ops ops[, preamble, ...])

通用函数。

方法#

这些方法仅适用于选定的ufuncs。

提示

如果你需要支持其他ufuncs,请在 问题跟踪器 中提交功能请求以及你的用例。

可用的ufuncs#

数学运算#

add(x1, x2, /[, out, casting, dtype])

逐元素相加两个数组。

subtract(x1, x2, /[, out, casting, dtype])

逐元素地减去参数。

multiply(x1, x2, /[, out, casting, dtype])

对两个数组进行逐元素相乘。

matmul

matmul(x1, x2, /, out=None, **kwargs)

divide

true_divide(x1, x2, /, out=None, *, casting='same_kind', dtype=None)

logaddexp(x1, x2, /[, out, casting, dtype])

计算 log(exp(x1) + exp(x2)) 的逐元素值。

logaddexp2(x1, x2, /[, out, casting, dtype])

计算 log2(exp2(x1) + exp2(x2)) 逐元素。

true_divide(x1, x2, /[, out, casting, dtype])

逐元素真除法(即

floor_divide(x1, x2, /[, out, casting, dtype])

逐元素的向下取整除法(即

negative(x, /[, out, casting, dtype])

逐元素取数值负值。

positive(x, /[, out, casting, dtype])

对数值正元素逐元素取值。

power(x1, x2, /[, out, casting, dtype])

计算 x1 ** x2 的元素级结果。

float_power(x1, x2, /[, out, casting, dtype])

第一个数组的元素按第二个数组的元素逐个求幂。

remainder

mod(x1, x2, /, out=None, *, casting='same_kind', dtype=None)

mod(x1, x2, /[, out, casting, dtype])

计算Python除法的余数,逐元素进行。

fmod(x1, x2, /[, out, casting, dtype])

计算 C 除法的余数,逐元素进行。

divmod(x1, x2[, out1, out2], / [[, out, ...])

absolute(x, /[, out, casting, dtype])

逐元素绝对值函数。

fabs(x, /[, out, casting, dtype])

逐元素计算绝对值。

rint(x, /[, out, casting, dtype])

将数组的每个元素四舍五入到最近的整数。

sign(x, /[, out, casting, dtype])

逐元素符号函数。

heaviside(x1, x2, /[, out, casting, dtype])

计算 Heaviside 阶跃函数。

conj

conjugate(x, /, out=None, *, casting='same_kind', dtype=None)

conjugate(x, /[, out, casting, dtype])

返回元素的复共轭。

exp(x, /[, out, casting, dtype])

逐元素指数函数。

exp2(x, /[, out, casting, dtype])

逐元素以2为底的指数运算。

log(x, /[, out, casting, dtype])

逐元素自然对数函数。

log2(x, /[, out, casting, dtype])

逐元素二进制对数函数。

log10(x, /[, out, casting, dtype])

逐元素常用对数函数。

expm1(x, /[, out, casting, dtype])

计算 exp(x) - 1 的逐元素值。

log1p(x, /[, out, casting, dtype])

计算 log(1 + x) 的逐元素值。

sqrt(x, /[, out, casting, dtype])

逐元素平方根函数。

square(x, /[, out, casting, dtype])

逐元素平方函数。

cbrt(x, /[, out, casting, dtype])

逐元素立方根函数。

reciprocal(x, /[, out, casting, dtype])

计算 1 / x 的元素级结果。

gcd(x1, x2, /[, out, casting, dtype])

计算 x1x2 的逐元素最大公约数。

lcm(x1, x2, /[, out, casting, dtype])

计算 x1x2 的逐元素最小公倍数。

三角函数#

sin(x, /[, out, casting, dtype])

逐元素正弦函数。

cos(x, /[, out, casting, dtype])

逐元素余弦函数。

tan(x, /[, out, casting, dtype])

逐元素正切函数。

arcsin(x, /[, out, casting, dtype])

逐元素反正弦函数(也称为

arccos(x, /[, out, casting, dtype])

逐元素反余弦函数(也称为

arctan(x, /[, out, casting, dtype])

逐元素反正切函数(也称为

arctan2(x1, x2, /[, out, casting, dtype])

两个数组比值的逐元素反正切。

hypot(x1, x2, /[, out, casting, dtype])

计算给定长度的正交向量的斜边。

sinh(x, /[, out, casting, dtype])

逐元素的双曲正弦函数。

cosh(x, /[, out, casting, dtype])

逐元素双曲余弦函数。

tanh(x, /[, out, casting, dtype])

逐元素双曲正切函数。

arcsinh(x, /[, out, casting, dtype])

逐元素的双曲正弦函数的反函数。

arccosh(x, /[, out, casting, dtype])

逐元素的双曲余弦函数的反函数。

arctanh(x, /[, out, casting, dtype])

逐元素的双曲正切函数的逆函数。

degrees

rad2deg(x, /, out=None, *, casting='same_kind', dtype=None)

radians(x, /[, out, casting, dtype])

将角度从度数转换为弧度,逐元素进行。

deg2rad

radians(x, /, out=None, *, casting='same_kind', dtype=None)

rad2deg(x, /[, out, casting, dtype])

将角度从弧度转换为逐元素的度数。

位操作函数#

bitwise_and(x1, x2, /[, out, casting, dtype])

计算两个数组元素级的按位与。

bitwise_or(x1, x2, /[, out, casting, dtype])

计算两个数组元素的按位或。

bitwise_xor(x1, x2, /[, out, casting, dtype])

计算两个数组元素级的按位异或。

invert(x, /[, out, casting, dtype])

计算数组元素的按位取反。

left_shift(x1, x2, /[, out, casting, dtype])

将每个整数元素的位向左移动。

right_shift(x1, x2, /[, out, casting, dtype])

将每个整数元素的位向右移动。

比较函数#

greater(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 > x2

greater_equal(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 >= x2

less(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 < x2

less_equal(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 <= x2

not_equal(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 != x2

equal(x1, x2, /[, out, casting, dtype])

逐元素测试 x1 == x2

logical_and(x1, x2, /[, out, casting, dtype])

计算两个数组的逻辑与。

logical_or(x1, x2, /[, out, casting, dtype])

计算两个数组的逻辑或。

logical_xor(x1, x2, /[, out, casting, dtype])

计算两个数组的逻辑异或。

logical_not(x, /[, out, casting, dtype])

计算数组的逻辑非。

maximum(x1, x2, /[, out, casting, dtype])

取两个数组元素的最大值。

minimum(x1, x2, /[, out, casting, dtype])

取两个数组元素的最小值。

fmax(x1, x2, /[, out, casting, dtype])

取两个数组元素的最大值。

fmin(x1, x2, /[, out, casting, dtype])

取两个数组元素的最小值。

浮动函数#

isfinite(x, /[, out, casting, dtype])

逐元素测试有限性。

isinf(x, /[, out, casting, dtype])

测试每个元素是否为正无穷或负无穷。

isnan(x, /[, out, casting, dtype])

测试每个元素是否为 NaN。

fabs(x, /[, out, casting, dtype])

逐元素计算绝对值。

signbit(x, /[, out, casting, dtype])

测试每个元素的符号位是否被设置(即

copysign(x1, x2, /[, out, casting, dtype])

返回第一个参数,其符号位逐元素地由第二个参数决定。

nextafter(x1, x2, /[, out, casting, dtype])

计算向第二个参数方向最近的邻居浮点值。

modf(x[, out1, out2], / [[, out, casting, dtype])

逐元素提取数组的分数部分和整数部分。

ldexp(x1, x2, /[, out, casting, dtype])

计算 x1 * 2 ** x2 的逐元素值。

frexp(x[, out1, out2], / [[, out, casting, ...])

将每个元素分解为尾数和二的指数。

fmod(x1, x2, /[, out, casting, dtype])

计算 C 除法的余数,逐元素进行。

floor(x, /[, out, casting, dtype])

将数组中的每个元素四舍五入为其下限整数。

ceil(x, /[, out, casting, dtype])

将数组的每个元素四舍五入到其上限整数。

trunc(x, /[, out, casting, dtype])

将数组的每个元素向零舍入。

广义通用函数#

除了常规的ufuncs,CuPy还提供了一个包装类,用于将常规的cupy函数转换为NumPy中的通用函数,如 https://numpy.org/doc/stable/reference/c-api/generalized-ufuncs.html 所示。这允许自动使用诸如 axesorderdtype 等关键字参数,而无需在包装的函数中显式实现它们。

GeneralizedUFunc(func, signature, **kwargs)

通过包装用户提供的具有签名的函数来创建一个广义的通用函数。