Repository: OpenMined/PySyft
Pull request #190 (2017-08-27 01:20:39)

added floor & ceil (inplace function)


Patch:
diff --git a/syft/math.py b/syft/math.py
index 34f1189ffa..263e3939af 100644
--- a/syft/math.py
+++ b/syft/math.py
@@ -8,7 +8,7 @@ from .tensor import _ensure_tensorbase
 
 __all__ = [
 
-    'cumprod', 'cumsum', 'ceil', 'dot', 'matmul', 'addmm', 'addcmul',
+    'cumprod', 'cumsum', 'ceil', 'dot', 'floor', 'matmul', 'addmm', 'addcmul',
     'addcdiv', 'addmv', 'addbmm', 'baddbmm', 'sigmoid',
 ]
 
@@ -65,7 +65,7 @@ def ceil(tensor):
     Returns the ceilling input tensor,element wise .
 
     Ceilling of an input scalar is the smallest integer such as :
-    for each floating pount number x : a >= x
+    for each floating point number x : a >= x
 
     Behavior is independent of a tensor's shape.
 
@@ -79,6 +79,23 @@ def ceil(tensor):
     return TensorBase(np.ceil(tensor.data))
 
 
+def floor(tensor):
+    """
+    Returns the floored input tensor,element wise.
+    Floor of an input scalar is the largest integer such as:
+    for each floating point number x : a <= x
+
+    Behavior is independent of a tensor's shape
+    :input: TensorBase tensor\n
+    :return: TensorBase tensor of floored elements .
+    """
+
+    tensor = _ensure_tensorbase(tensor)
+    if tensor.encrypted is True:
+        return NotImplemented
+    return TensorBase(np.floor(tensor.data))
+
+
 def cumsum(tensor, dim=0):
     """
     Returns the cumulative sum of the elements along a given dimension
diff --git a/syft/tensor.py b/syft/tensor.py
index 4364668717..f6cb19e76a 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -224,11 +224,19 @@ class TensorBase(object):
         else:
             return self.data.sum(axis=dim)
 
-    def ceil(self):
+    def ceil_(self):
         """Returns the ceilling of the input tensor elementwise."""
         if self.encrypted:
             return NotImplemented
-        return np.ceil(self.data)
+        self.data = syft.math.ceil(self.data)
+        return self
+
+    def floor_(self):
+        """Inplace floor method"""
+        if self.encrypted:
+            return NotImplemented
+        self.data = syft.math.floor(self.data)
+        return self
 
     def addmm(self, tensor2, mat, beta=1, alpha=1):
         """Performs ((Mat*Beta)+((Tensor1@Tensor2)*Alpha)) and  returns the


Test patch:
diff --git a/tests/test_math.py b/tests/test_math.py
index ad7cdf10bb..8e153730e4 100644
--- a/tests/test_math.py
+++ b/tests/test_math.py
@@ -25,6 +25,13 @@ class CeilTests(unittest.TestCase):
                                                               [8., 9.]])))
 
 
+class FloorTests(unittest.TestCase):
+    def testFloor(self):
+        t1 = TensorBase(np.array([[2.3, 4.1], [7.4, 8.3]]))
+        self.assertTrue(syft.equal(syft.math.floor(t1), TensorBase([[2., 4.],
+                                                                    [7., 8.]])))
+
+
 class CumsumTests(unittest.TestCase):
     def testCumsum(self):
         t1 = TensorBase(np.array([1, 2, 3]))
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index e87deb1af5..195fb686c6 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -27,9 +27,17 @@ class AddTests(unittest.TestCase):
 
 
 class CeilTests(unittest.TestCase):
-    def testCeil(self):
+    def testCeil_(self):
         t = TensorBase(np.array([1.4, 2.7, 6.2]))
-        self.assertTrue(syft.equal(t.ceil(), [2, 3, 7]))
+        t.ceil_()
+        self.assertTrue(syft.equal(t.data, TensorBase([2, 3, 7])))
+
+
+class FloorTests(unittest.TestCase):
+    def testFloor_(self):
+        t = TensorBase(np.array([1.4, 2.7, 6.2]))
+        t.floor_()
+        self.assertTrue(syft.equal(t.data, TensorBase([1., 2., 6.])))
 
 
 class SubTests(unittest.TestCase):
@@ -294,14 +302,14 @@ class baddbmmTests(unittest.TestCase):
 class expTests(unittest.TestCase):
     def testexp(self):
         t3 = TensorBase(np.array([[[1, 3], [3, 5]],
-                                 [[5, 7], [9, 1]]]))
+                                  [[5, 7], [9, 1]]]))
         out = t3.exp()
         self.assertTrue(np.allclose(out.data, [[[2.71828183e+00, 2.00855369e+01], [2.00855369e+01, 1.48413159e+02]],
                                                [[1.48413159e+02, 1.09663316e+03], [8.10308393e+03, 2.71828183e+00]]]))
 
     def testexp_(self):
         t3 = TensorBase(np.array([[[1, 3], [3, 5]],
-                                 [[5, 7], [9, 1]]]))
+                                  [[5, 7], [9, 1]]]))
         t3.exp_()
         self.assertTrue(np.allclose(t3.data, [[[2.71828183e+00, 2.00855369e+01], [2.00855369e+01, 1.48413159e+02]],
                                               [[1.48413159e+02, 1.09663316e+03], [8.10308393e+03, 2.71828183e+00]]]))