通用函数 (cupy.ufunc)#

CuPy 提供了通用函数(又称 ufuncs)来支持各种元素wise操作。CuPy 的 ufunc 支持 NumPy 中以下功能

  • 广播

  • 输出类型确定

  • 类型转换规则

ufunc#

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

通用函数。

方法#

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

提示

如果您需要支持其他 ufuncs,请在跟踪器 issue 中提交功能请求并说明您的用例。

可用 ufuncs#

数学运算#

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

元素wise地相加两个数组。

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

元素wise地减去参数。

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

元素wise地乘以两个数组。

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

元素wise计算 log(exp(x1) + exp(x2))

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

元素wise计算 log2(exp2(x1) + exp2(x2))

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

元素wise真除法(即

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

元素wise整除(即

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

元素wise取数值负值。

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

元素wise取数值正值。

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

元素wise计算 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])

元素wise计算 Python 除法的余数。

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

元素wise计算 C 除法的余数。

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

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

元素wise绝对值函数。

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

元素wise计算绝对值。

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

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

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

元素wise符号函数。

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

计算 Heaviside 阶跃函数。

conj

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

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

返回元素wise的复共轭。

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

元素wise指数函数。

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

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

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

元素wise自然对数函数。

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

元素wise二进制对数函数。

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

元素wise常用对数函数。

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

元素wise计算 exp(x) - 1

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

元素wise计算 log(1 + x)

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

元素wise平方根函数。

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

元素wise平方函数。

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

元素wise立方根函数。

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

元素wise计算 1 / x

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

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

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

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

三角函数#

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

元素wise正弦函数。

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

元素wise余弦函数。

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

元素wise正切函数。

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

元素wise反正弦函数(又称

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

元素wise反余弦函数(又称

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

元素wise反正切函数(又称

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

元素wise计算两个数组比值的反正切。

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

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

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

元素wise双曲正弦函数。

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

元素wise双曲余弦函数。

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

元素wise双曲正切函数。

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

元素wise双曲正弦函数的逆函数。

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

元素wise双曲余弦函数的逆函数。

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

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

degrees

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

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

元素wise将角度从度转换为弧度。

deg2rad

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

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

元素wise将角度从弧度转换为度。

位操作函数#

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

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

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

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

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

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

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

元素wise计算数组的按位非。

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

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

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

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

比较函数#

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

元素wise测试是否 x1 > x2

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

元素wise测试是否 x1 >= x2

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

元素wise测试是否 x1 < x2

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

元素wise测试是否 x1 <= x2

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

元素wise测试是否 x1 != x2

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

元素wise测试是否 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])

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

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

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

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

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

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

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

浮点函数#

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

元素wise测试是否有限。

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

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

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

测试每个元素是否为 NaN。

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

元素wise计算绝对值。

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

元素wise测试符号位是否已设置(即

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

元素wise返回第一个参数,其符号位与第二个参数相同。

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

计算朝向第二个参数的最近浮点值。

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

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

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

元素wise计算 x1 * 2 ** x2

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

将每个元素分解为尾数和以2为底的指数。

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

元素wise计算 C 除法的余数。

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

将数组的每个元素向下取整到最近的整数。

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

将数组的每个元素向上取整到最近的整数。

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

将数组的每个元素向零取整。

广义通用函数#

除了常规 ufuncs,CuPy 还提供了一个包装器类,用于将常规 cupy 函数转换为广义通用函数,如同 NumPy 中所示 https://numpy.com.cn/doc/stable/reference/c-api/generalized-ufuncs.html。这使得无需在被包装的函数中显式实现即可自动使用诸如 axesorderdtype 等关键字参数。

GeneralizedUFunc(func, signature, **kwargs)

通过将用户提供的函数及其签名进行包装来创建一个广义通用函数。