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 884db5c812 [SYSTEMDS-3734] Python API Builtin floor & ceil
884db5c812 is described below

commit 884db5c81286026513dc43da7f38d32582e18bc3
Author: e-strauss <[email protected]>
AuthorDate: Fri Aug 30 23:53:17 2024 +0200

    [SYSTEMDS-3734] Python API Builtin floor & ceil
    
    Add missing non dml bodied floor and ceil operator
    
    Closes #2078
---
 src/main/python/systemds/operator/nodes/matrix.py | 14 +++++
 src/main/python/systemds/operator/nodes/scalar.py | 14 +++++
 src/main/python/tests/matrix/test_ceil.py         | 63 +++++++++++++++++++++++
 src/main/python/tests/matrix/test_floor.py        | 63 +++++++++++++++++++++++
 4 files changed, 154 insertions(+)

diff --git a/src/main/python/systemds/operator/nodes/matrix.py 
b/src/main/python/systemds/operator/nodes/matrix.py
index f06ce315e8..e71e538ab6 100644
--- a/src/main/python/systemds/operator/nodes/matrix.py
+++ b/src/main/python/systemds/operator/nodes/matrix.py
@@ -280,6 +280,20 @@ class Matrix(OperationNode):
         """
         return Matrix(self.sds_context, 'exp', [self])
 
+    def floor(self) -> 'Matrix':
+        """Return the floor of the input, element-wise.
+
+        :return: `Matrix` representing operation
+        """
+        return Matrix(self.sds_context, 'floor', [self])
+
+    def ceil(self) -> 'Matrix':
+        """Return the ceiling of the input, element-wise.
+
+        :return: `Matrix` representing operation
+        """
+        return Matrix(self.sds_context, 'ceil', [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 a764ad3abb..7ed64bc713 100644
--- a/src/main/python/systemds/operator/nodes/scalar.py
+++ b/src/main/python/systemds/operator/nodes/scalar.py
@@ -163,6 +163,20 @@ class Scalar(OperationNode):
         """
         return Scalar(self.sds_context, 'sqrt', [self])
 
+    def floor(self) -> 'Scalar':
+        """Return the floor of the input, element-wise.
+
+        :return: `Scalar` representing operation
+        """
+        return Scalar(self.sds_context, 'floor', [self])
+
+    def ceil(self) -> 'Scalar':
+        """Return the ceiling of the input, element-wise.
+
+        :return: `Scalar` representing operation
+        """
+        return Scalar(self.sds_context, 'ceil', [self])
+
     def sin(self) -> 'Scalar':
         """Calculate sin.
 
diff --git a/src/main/python/tests/matrix/test_ceil.py 
b/src/main/python/tests/matrix/test_ceil.py
new file mode 100644
index 0000000000..c0db01fb37
--- /dev/null
+++ b/src/main/python/tests/matrix/test_ceil.py
@@ -0,0 +1,63 @@
+# -------------------------------------------------------------
+#
+# 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 TestCEIL(unittest.TestCase):
+    def setUp(self):
+        self.sds = SystemDSContext()
+
+    def tearDown(self):
+        self.sds.close()
+
+    def test_ceil_basic(self):
+
+        input_matrix = (
+            np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 
15, 16]])
+            / 10
+        )
+
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.ceil().compute()
+        np_result_np = np.ceil(input_matrix)
+        assert np.allclose(sds_result, np_result_np, 1e-9)
+
+    def test_floor_random(self):
+
+        input_matrix = np.random.random((10, 10)) * 20 - 10
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.ceil().compute()
+        np_result_np = np.ceil(input_matrix)
+        assert np.allclose(sds_result, np_result_np, 1e-9)
+
+    def test_ceil_scalar(self):
+        for i in np.random.random(10) * 20 - 10:
+            sds_input = self.sds.scalar(i)
+            sds_result = sds_input.ceil().compute()
+            np_result_np = np.ceil(i)
+            assert np.isclose(sds_result, np_result_np, 1e-9)
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/src/main/python/tests/matrix/test_floor.py 
b/src/main/python/tests/matrix/test_floor.py
new file mode 100644
index 0000000000..3c6c5b3296
--- /dev/null
+++ b/src/main/python/tests/matrix/test_floor.py
@@ -0,0 +1,63 @@
+# -------------------------------------------------------------
+#
+# 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 TestFLOOR(unittest.TestCase):
+    def setUp(self):
+        self.sds = SystemDSContext()
+
+    def tearDown(self):
+        self.sds.close()
+
+    def test_floor_basic(self):
+
+        input_matrix = (
+            np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 
15, 16]])
+            / 10
+        )
+
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.floor().compute()
+        np_result_np = np.floor(input_matrix)
+        assert np.allclose(sds_result, np_result_np, 1e-9)
+
+    def test_floor_random(self):
+
+        input_matrix = np.random.random((10, 10))
+        sds_input = self.sds.from_numpy(input_matrix)
+        sds_result = sds_input.floor().compute()
+        np_result_np = np.floor(input_matrix)
+        assert np.allclose(sds_result, np_result_np, 1e-9)
+
+    def test_floor_scalar(self):
+        for i in np.random.random(10) * 20 - 10:
+            sds_input = self.sds.scalar(i)
+            sds_result = sds_input.floor().compute()
+            np_result_np = np.floor(i)
+            assert np.isclose(sds_result, np_result_np, 1e-9)
+
+
+if __name__ == "__main__":
+    unittest.main()

Reply via email to