gcd

paddle. gcd ( x, y, name=None ) [源代码]

计算两个输入的按元素绝对值的最大公约数,输入必须是整型。

注解

gcd(0,0)=0, gcd(0, y)=|y|

如果x和y的shape不一致,会对两个shape进行广播操作,得到一致的shape(并作为输出结果的shape), 请参见 cn_user_guide_broadcasting

参数

  • x (Tensor) - 输入的Tensor,数据类型为:int32,int64。

  • y (Tensor) - 输入的Tensor,数据类型为:int32,int64。

  • name (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 Name

返回

输出Tensor,与输入数据类型相同。

代码示例

import paddle

x1 = paddle.to_tensor(12)
x2 = paddle.to_tensor(20)
paddle.gcd(x1, x2)
# Tensor(shape=[1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [4])

x3 = paddle.arange(6)
paddle.gcd(x3, x2)
# Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [20, 1 , 2 , 1 , 4 , 5])

x4 = paddle.to_tensor(0)
paddle.gcd(x4, x2)
# Tensor(shape=[1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [20])

paddle.gcd(x4, x4)
# Tensor(shape=[1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [0])

x5 = paddle.to_tensor(-20)
paddle.gcd(x1, x5)
# Tensor(shape=[1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [4])