copysign

paddle. copysign ( x: Tensor, y: Tensor | float, name: str | None = None, *, out: Tensor | None = None ) → Tensor [source]

Create a new floating-point tensor with the magnitude of input x and the sign of y, elementwise.

Equation:
\[\begin{split}copysign(x_{i},y_{i})=\left\{\begin{matrix} & -|x_{i}| & if \space y_{i} <= -0.0\\ & |x_{i}| & if \space y_{i} >= 0.0 \end{matrix}\right.\end{split}\]
Parameters
  • x (Tensor) – The input Tensor, magnitudes, the data type is bool, uint8, int8, int16, int32, int64, bfloat16, float16, float32, float64. Alias: input.

  • y (Tensor|float) – contains value(s) whose signbit(s) are applied to the magnitudes in input. Alias: other.

  • name (str|None, optional) – Name for the operation.

Keyword Arguments

out (Tensor, optional) – The output tensor. Default: None.

Returns

out (Tensor), the output tensor. The data type is the same as the input tensor.

Examples

>>> import paddle
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-1, 1, -1], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
       [-1.,  2., -3.])
>>> import paddle
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-2], dtype='float64')
>>> res = paddle.copysign(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[-1.,  -2.,  -3.])
>>> import paddle
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([0.0], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[1., 2., 3.])
>>> import paddle
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-0.0], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[-1., -2., -3.])