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 7a72925313 [SYSTEMDS-3733] Python API Builtin log
7a72925313 is described below

commit 7a729253135409dd10953eb8797f76b93e98222c
Author: e-strauss <[email protected]>
AuthorDate: Fri Aug 30 23:58:33 2024 +0200

    [SYSTEMDS-3733] Python API Builtin log
    
    Add missing non dml bodied log operator.
    
    Closes #2079
---
 src/main/python/systemds/operator/nodes/matrix.py |  7 +++
 src/main/python/systemds/operator/nodes/scalar.py |  7 +++
 src/main/python/tests/matrix/test_log.py          | 75 +++++++++++++++++++++++
 3 files changed, 89 insertions(+)

diff --git a/src/main/python/systemds/operator/nodes/matrix.py 
b/src/main/python/systemds/operator/nodes/matrix.py
index e71e538ab6..6d1a077b5e 100644
--- a/src/main/python/systemds/operator/nodes/matrix.py
+++ b/src/main/python/systemds/operator/nodes/matrix.py
@@ -294,6 +294,13 @@ class Matrix(OperationNode):
         """
         return Matrix(self.sds_context, 'ceil', [self])
 
+    def log(self) -> 'Matrix':
+        """Calculate logarithm.
+
+        :return: `Matrix` representing operation
+        """
+        return Matrix(self.sds_context, 'log', [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 7ed64bc713..b2a7e898c2 100644
--- a/src/main/python/systemds/operator/nodes/scalar.py
+++ b/src/main/python/systemds/operator/nodes/scalar.py
@@ -177,6 +177,13 @@ class Scalar(OperationNode):
         """
         return Scalar(self.sds_context, 'ceil', [self])
 
+    def log(self) -> 'Scalar':
+        """Calculate logarithm.
+
+        :return: `Scalar` representing operation
+        """
+        return Scalar(self.sds_context, 'log', [self])
+
     def sin(self) -> 'Scalar':
         """Calculate sin.
 
diff --git a/src/main/python/tests/matrix/test_log.py 
b/src/main/python/tests/matrix/test_log.py
new file mode 100644
index 0000000000..d0be8848a4
--- /dev/null
+++ b/src/main/python/tests/matrix/test_log.py
@@ -0,0 +1,75 @@
+# -------------------------------------------------------------
+#
+# 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 TestLOG(unittest.TestCase):
+    def setUp(self):
+        self.sds = SystemDSContext()
+
+    def tearDown(self):
+        self.sds.close()
+
+    def test_log_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.log().compute()
+        np_result_np = np.log(input_matrix)
+        assert np.allclose(sds_result, np_result_np, 1e-9)
+
+    def test_log_basic2(self):
+
+        input_matrix = (
+            np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 
15, 16]])
+            - 8
+        )
+        input_matrix = np.exp(input_matrix)
+
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.log().compute()
+        np_result = np.log(input_matrix)
+        assert np.allclose(sds_result, np_result, 1e-9)
+
+    def test_log_random(self):
+
+        input_matrix = np.random.random((10, 10))
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.log().compute()
+        np_result = np.log(input_matrix)
+        assert np.allclose(sds_result, np_result, 1e-9)
+
+    def test_log_scalar(self):
+        for i in np.random.random(10) * 1000:
+            sds_input = self.sds.scalar(i)
+            sds_result = sds_input.log().compute()
+            np_result_np = np.log(i)
+            assert np.isclose(sds_result, np_result_np, 1e-9)
+
+
+if __name__ == "__main__":
+    unittest.main()

Reply via email to