Cytnx icon indicating copy to clipboard operation
Cytnx copied to clipboard

Implement sign() function to Tensor and UniTensor

Open Chan-Sing-Hong opened this issue 9 months ago • 7 comments

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.

Chan-Sing-Hong avatar Mar 12 '25 06:03 Chan-Sing-Hong

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.

kaihsin avatar Mar 13 '25 01:03 kaihsin

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?

Chan-Sing-Hong avatar Mar 13 '25 01:03 Chan-Sing-Hong

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: @.***>

kaihsin avatar Mar 13 '25 02:03 kaihsin

Maybe we can have an option to choose whether sign(0)=0 or sign(0)=1.

Chan-Sing-Hong avatar Mar 13 '25 02:03 Chan-Sing-Hong

Are we implementing this function? Is there a corresponding function in numpy?

yingjerkao avatar Aug 19 '25 02:08 yingjerkao

numpy.sign follows the Array API standard.

IvanaGyro avatar Aug 19 '25 05:08 IvanaGyro

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.

manuschneider avatar Nov 02 '25 09:11 manuschneider