NumPy copysign combines the magnitude of one input with the sign of another
Changing a sign need not mean multiplying two full values. NumPy copysign takes magnitude from its first input and sign from its second input. Academia Master Trader presents this English lesson for readers in Mozambique, using constructed arrays to explain the two roles, scalar broadcasting and signed floating-point zero.
The two inputs supply different information
After import numpy as np, define values = np.array([-2.5,0.,7.]) and direction = np.array([1.,-4.,-1.]). The call np.copysign(values,direction) returns [2.5,-0.,-7.].
The first result has magnitude two and a half because that magnitude came from the first input. Its positive sign came from the corresponding positive second input.
The final result has magnitude seven and a negative sign. The second input's magnitude of one does not replace seven. Likewise, the magnitude four in the middle second input does not multiply or scale zero.
The function uses the second input as a sign source, not as a multiplier, a weight or a threshold.

A scalar supplies one sign across an array
The call np.copysign(values,-1.) returns [-2.5,-0.,-7.]. A negative scalar supplies a negative sign to every corresponding output entry.
Using np.copysign(values,1.) returns [2.5,0.,7.]. Both a previously negative magnitude and a previously positive magnitude receive the scalar's positive sign.
This is not the same as ordinary multiplication by minus one. Multiplying minus two and a half by minus one produces positive two and a half; copying a negative sign onto it leaves it negative.
The distinction is useful whenever the requirement is a chosen sign with preserved magnitude, rather than an algebraic reversal of the original sign.
Floating-point zero can carry a sign
The middle result in the opening example is negative zero. Its magnitude is still zero, but its floating-point sign bit is negative.
The call np.copysign(3.,-0.) returns -3.. The sign source is negative zero, even though negative zero compares equal to positive zero numerically.
A sign rule based only on whether the second input is less than zero would miss this distinction. Copysign transfers its sign instead of relying on that comparison.
Signed zero is a numeric representation detail. It does not indicate an extra observation or a value with a nonzero magnitude.
Broadcasting determines the output arrangement
A scalar first input can also combine with several sign sources. The call np.copysign(2.,[-1.,1.,-1.]) returns [-2.,2.,-2.].
For array inputs, their shapes must broadcast to a common output shape. Corresponding magnitudes and signs are then combined element by element under that arrangement.
The operation does not sort values, calculate absolute differences or decide which sign a real-world observation ought to have. The sign source must already express the intended convention.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Name the magnitude source and sign source separately when explaining a sign-copying calculation.