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 35dcec5e0b [SYSTEMDS-3736] Python API Builtin sqrt
35dcec5e0b is described below
commit 35dcec5e0b9593da967f6c080931972e8a249404
Author: e-strauss <[email protected]>
AuthorDate: Fri Aug 30 23:43:11 2024 +0200
[SYSTEMDS-3736] Python API Builtin sqrt
Add missing non dml bodied square root operator
Closes #2076
---
src/main/python/systemds/operator/nodes/matrix.py | 7 +++
src/main/python/systemds/operator/nodes/scalar.py | 7 +++
src/main/python/tests/matrix/test_sqrt.py | 62 +++++++++++++++++++++++
3 files changed, 76 insertions(+)
diff --git a/src/main/python/systemds/operator/nodes/matrix.py
b/src/main/python/systemds/operator/nodes/matrix.py
index 05785e162b..205bef4359 100644
--- a/src/main/python/systemds/operator/nodes/matrix.py
+++ b/src/main/python/systemds/operator/nodes/matrix.py
@@ -266,6 +266,13 @@ class Matrix(OperationNode):
"""
return Matrix(self.sds_context, 'abs', [self])
+ def sqrt(self) -> 'Matrix':
+ """Calculate square root.
+
+ :return: `Matrix` representing operation
+ """
+ return Matrix(self.sds_context, 'sqrt', [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 212ec9c333..161708472d 100644
--- a/src/main/python/systemds/operator/nodes/scalar.py
+++ b/src/main/python/systemds/operator/nodes/scalar.py
@@ -156,6 +156,13 @@ class Scalar(OperationNode):
"""
return Scalar(self.sds_context, 'abs', [self])
+ def sqrt(self) -> 'Scalar':
+ """Calculate square root.
+
+ :return: `Scalar` representing operation
+ """
+ return Scalar(self.sds_context, 'sqrt', [self])
+
def sin(self) -> 'Scalar':
"""Calculate sin.
diff --git a/src/main/python/tests/matrix/test_sqrt.py
b/src/main/python/tests/matrix/test_sqrt.py
new file mode 100644
index 0000000000..3f7791f149
--- /dev/null
+++ b/src/main/python/tests/matrix/test_sqrt.py
@@ -0,0 +1,62 @@
+# -------------------------------------------------------------
+#
+# 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 TestSQRT(unittest.TestCase):
+ def setUp(self):
+ self.sds = SystemDSContext()
+
+ def tearDown(self):
+ self.sds.close()
+
+ def test_sqrt_basic(self):
+
+ input_matrix = np.array([[1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12],
+ [13, 14, 15, 16]])
+
+ sds_input = self.sds.from_numpy(input_matrix)
+ sds_result = sds_input.sqrt().compute()
+ np_result_np = np.sqrt(input_matrix)
+ assert np.allclose(sds_result, np_result_np,1e-9)
+
+ def test_sqrt_random(self):
+
+ input_matrix = np.random.random((10,10))
+ sds_input = self.sds.from_numpy(input_matrix)
+ sds_result = sds_input.sqrt().compute()
+ np_result_np = np.sqrt(input_matrix)
+ assert np.allclose(sds_result, np_result_np,1e-9)
+
+ def test_sqrt_scalar(self):
+ for i in np.random.random(10):
+ sds_input = self.sds.scalar(i)
+ sds_result = sds_input.sqrt().compute()
+ np_result_np = np.sqrt(i)
+ assert np.isclose(sds_result, np_result_np,1e-9)
+
+
+if __name__ == '__main__':
+ unittest.main()