This is an automated email from the ASF dual-hosted git repository.
baunsgaard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/systemds.git
The following commit(s) were added to refs/heads/main by this push:
new e3c8cc924c [SYSTEMDS-3738] Missing builtin sign
e3c8cc924c is described below
commit e3c8cc924c56aa64eaea0b0986b9235010ed07c0
Author: e-strauss <[email protected]>
AuthorDate: Sat Aug 31 00:02:09 2024 +0200
[SYSTEMDS-3738] Missing builtin sign
Add missing non dml bodied sign operator.
Closes #2081
---
src/main/python/systemds/operator/nodes/matrix.py | 8 +++
src/main/python/systemds/operator/nodes/scalar.py | 10 +++-
src/main/python/tests/matrix/test_sign.py | 60 +++++++++++++++++++++++
3 files changed, 77 insertions(+), 1 deletion(-)
diff --git a/src/main/python/systemds/operator/nodes/matrix.py
b/src/main/python/systemds/operator/nodes/matrix.py
index 6d1a077b5e..23c40422eb 100644
--- a/src/main/python/systemds/operator/nodes/matrix.py
+++ b/src/main/python/systemds/operator/nodes/matrix.py
@@ -301,6 +301,14 @@ class Matrix(OperationNode):
"""
return Matrix(self.sds_context, 'log', [self])
+ def sign(self) -> 'Matrix':
+ """Returns a matrix representing the signs of the input matrix
elements,
+ where 1 represents positive, 0 represents zero, and -1 represents
negative.
+
+ :return: `Matrix` representing operation
+ """
+ return Matrix(self.sds_context, 'sign', [self])
+
def sin(self) -> 'Matrix':
"""Calculate sin.
diff --git a/src/main/python/systemds/operator/nodes/scalar.py
b/src/main/python/systemds/operator/nodes/scalar.py
index b2a7e898c2..ad29e3f13b 100644
--- a/src/main/python/systemds/operator/nodes/scalar.py
+++ b/src/main/python/systemds/operator/nodes/scalar.py
@@ -196,7 +196,15 @@ class Scalar(OperationNode):
:return: `Scalar` representing operation
"""
- return Scalar(self.sds_context, 'exp', [self])
+ return Scalar(self.sds_context, "exp", [self])
+
+ def sign(self) -> "Scalar":
+ """Returns a the signs of the input,
+ where 1 represents positive, 0 represents zero, and -1 represents
negative.
+
+ :return: `Scalar` representing operation
+ """
+ return Scalar(self.sds_context, "sign", [self])
def cos(self) -> 'Scalar':
"""Calculate cos.
diff --git a/src/main/python/tests/matrix/test_sign.py
b/src/main/python/tests/matrix/test_sign.py
new file mode 100644
index 0000000000..796e30e79e
--- /dev/null
+++ b/src/main/python/tests/matrix/test_sign.py
@@ -0,0 +1,60 @@
+# -------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# -------------------------------------------------------------
+
+import unittest
+import numpy as np
+from systemds.context import SystemDSContext
+
+
+class TestSIGN(unittest.TestCase):
+ def setUp(self):
+ self.sds = SystemDSContext()
+
+ def tearDown(self):
+ self.sds.close()
+
+ def test_sign_basic(self):
+ input_matrix = np.array(
+ [[1, -2, 3, 4], [0, -6, 7, 8], [0, -10, 11, -12], [0, -14, 15,
-16]]
+ )
+
+ sds_input = self.sds.from_numpy(input_matrix)
+ sds_result = sds_input.sign().compute()
+ np_result_np = np.sign(input_matrix)
+ assert np.allclose(sds_result, np_result_np, 1e-9)
+
+ def test_sign_random(self):
+ input_matrix = np.random.random((10, 10)) * 2 - 1
+ sds_input = self.sds.from_numpy(input_matrix)
+ sds_result = sds_input.sign().compute()
+ np_result_np = np.sign(input_matrix)
+ assert np.allclose(sds_result, np_result_np, 1e-9)
+
+ def test_sign_scalar(self):
+ for i in np.random.random(10) * 2 - 1:
+ sds_input = self.sds.scalar(i)
+ sds_result = sds_input.sign().compute()
+ np_result_np = np.sign(i)
+ assert np.isclose(sds_result, np_result_np, 1e-9)
+
+
+if __name__ == "__main__":
+ unittest.main()