Implement sign() function to Tensor and UniTensor
I suggest adding a sign() function to Tensor and UniTensor classes that returns a new tensor, with each element representing the sign of the corresponding element in the original tensor.
element-wise sign should be already implemented via unary operator-() overload?
https://github.com/Cytnx-dev/Cytnx/blob/f2b2ed76c3ad855c4cef5100fc01cccf0724bc83/include/Tensor.hpp#L156
For the UniTensor I am not sure. You can use -1.*UniTensor for the interim if its not exits.
Yes, but we might need a sign function that provides the following functionality:
For example:
A = cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [2, -3]] )), rowrank=1)
cytnx.sign(A)
should return:
cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [1, -1]] )), rowrank=1)
Is there any existing function that can achieve this?
Oh you want to extract the sign but keep the zero as it is?
On Wed, Mar 12, 2025, 21:58 Chan-Sing-Hong @.***> wrote:
Yes, but we might need a sign function that provides the following functionality:
For example:
A = cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [2, -3]] )), rowrank=1) cytnx.sign(A)
should return:
cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [1, -1]] )), rowrank=1)
Is there any existing function that can achieve this?
— Reply to this email directly, view it on GitHub https://github.com/Cytnx-dev/Cytnx/issues/571#issuecomment-2719550547, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFCX3SLB5UXNA2KOHBNIYZL2UDQ2ZAVCNFSM6AAAAABY2XIXR6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJZGU2TANJUG4 . You are receiving this because you commented.Message ID: @.***> [image: Chan-Sing-Hong]Chan-Sing-Hong left a comment (Cytnx-dev/Cytnx#571) https://github.com/Cytnx-dev/Cytnx/issues/571#issuecomment-2719550547
Yes, but we might need a sign function that provides the following functionality:
For example:
A = cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [2, -3]] )), rowrank=1) cytnx.sign(A)
should return:
cytnx.UniTensor( cytnx.from_numpy( numpy.array( [[0, -1], [1, -1]] )), rowrank=1)
Is there any existing function that can achieve this?
— Reply to this email directly, view it on GitHub https://github.com/Cytnx-dev/Cytnx/issues/571#issuecomment-2719550547, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFCX3SLB5UXNA2KOHBNIYZL2UDQ2ZAVCNFSM6AAAAABY2XIXR6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJZGU2TANJUG4 . You are receiving this because you commented.Message ID: @.***>
Maybe we can have an option to choose whether sign(0)=0 or sign(0)=1.
Are we implementing this function? Is there a corresponding function in numpy?
numpy.sign follows the Array API standard.
If there is a usecase we can implement it. For now, the workaround A / A.Abs() does something similar (but gives +-nan for elements that are zero). Also, Abs is not implemented for UniTensor yet, see #681.