diff --git a/syft/__init__.py b/syft/__init__.py index db1820a59ca..5b6e97d24c6 100644 --- a/syft/__init__.py +++ b/syft/__init__.py @@ -6,7 +6,7 @@ from syft.tensor import equal, TensorBase from syft.math import cumprod, cumsum, ceil, dot, matmul, addmm, addcmul from syft.math import addcdiv, addmv, bmm, addbmm, baddbmm, transpose -from syft.math import unsqueeze, zeros, ones, rand, randn, mm +from syft.math import unsqueeze, zeros, ones, rand, randn, mm, fmod s = str(he) s += str(nn) @@ -19,3 +19,4 @@ s += str(transpose) + str(rand) + str(randn) + str(ones) + str(zeros) s += str(unsqueeze) s += str(mm) +s += str(fmod) diff --git a/syft/math.py b/syft/math.py index 328a5b24ff0..12d7a94d593 100644 --- a/syft/math.py +++ b/syft/math.py @@ -695,3 +695,29 @@ def mm(tensor1, tensor2): return NotImplemented else: return TensorBase(np.array(np.matmul(tensor1.data, tensor2.data))) + + +def fmod(tensor, divisor): + """ + Performs the element-wise division of tensor by divisor. + + Parameters + ---------- + tensor: TensorBase + + divisor: number or TensorBase + + Returns + ------- + TensorBase: + Output Tensor + """ + if tensor.encrypted: + return NotImplemented + + if isinstance(divisor, TensorBase): + if divisor.encrypted: + return NotImplemented + divisor = divisor.data + + return TensorBase(np.fmod(tensor.data, divisor)) diff --git a/syft/tensor.py b/syft/tensor.py index a5745082d36..d8fc25b1343 100644 --- a/syft/tensor.py +++ b/syft/tensor.py @@ -2981,6 +2981,30 @@ def mm(self, tensor): return syft.mm(self, tensor) + def fmod_(self, divisor): + """ + Performs the element-wise division of tensor by divisor. + + Parameters + ---------- + divisor: number or TensorBase + + Returns + ------- + TensorBase: + Output Tensor + """ + if self.encrypted: + return NotImplemented + + if isinstance(divisor, TensorBase): + if divisor.encrypted: + return NotImplemented + + self.data = syft.math.fmod(self, divisor) + + return self + def mv(tensormat, tensorvector): """ diff --git a/tests/test_math.py b/tests/test_math.py index 74c1f5e00b3..aaf70752189 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -243,3 +243,19 @@ def test_mm_3d(self): out = syft.mm(t1, t2) self.assertTrue(np.alltrue( out.data == [[5, 8, 11], [8, 13, 18], [11, 18, 25]])) + + +class fmodTest(unittest.TestCase): + def test_fmod_number(self): + t1 = TensorBase(np.array([-3, -2, -1, 1, 2, 3])) + self.assertTrue(np.array_equal(syft.math.fmod(t1, 2).data, np.array([-1, 0, -1, 1, 0, 1]))) + t2 = TensorBase(np.array([-3.5, -2.5, -1.5, 1.5, 2.5, 3.5])) + self.assertTrue(np.array_equal(syft.math.fmod(t2, 2.).data, np.array([-1.5, -0.5, -1.5, 1.5, 0.5, 1.5]))) + + def test_fmod_tensor(self): + t1 = TensorBase(np.array([-3, -2, -1, 1, 2, 3])) + divisor = np.array([2] * 6) + self.assertTrue(np.array_equal(syft.math.fmod(t1, divisor).data, np.array([-1, 0, -1, 1, 0, 1]))) + t2 = TensorBase(np.array([-3.5, -2.5, -1.5, 1.5, 2.5, 3.5])) + divisor = np.array([2.] * 6) + self.assertTrue(np.array_equal(syft.math.fmod(t2, divisor).data, np.array([-1.5, -0.5, -1.5, 1.5, 0.5, 1.5]))) diff --git a/tests/test_tensor.py b/tests/test_tensor.py index d2b22401331..a71206b816d 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -1119,5 +1119,25 @@ def test_mm_3d(self): self.assertTrue(np.alltrue(out.data == [[5, 8, 11], [8, 13, 18], [11, 18, 25]])) +class fmod_Test(unittest.TestCase): + def test_fmod_number(self): + t1 = TensorBase(np.array([-3, -2, -1, 1, 2, 3])) + t1.fmod_(2) + self.assertTrue(np.array_equal(t1.data, np.array([-1, 0, -1, 1, 0, 1]))) + t2 = TensorBase(np.array([-3.5, -2.5, -1.5, 1.5, 2.5, 3.5])) + t2.fmod_(2.) + self.assertTrue(np.array_equal(t2.data, np.array([-1.5, -0.5, -1.5, 1.5, 0.5, 1.5]))) + + def test_fmod_tensor(self): + t1 = TensorBase(np.array([-3, -2, -1, 1, 2, 3])) + divisor = np.array([2] * 6) + t1.fmod_(divisor) + self.assertTrue(np.array_equal(t1.data, np.array([-1, 0, -1, 1, 0, 1]))) + t2 = TensorBase(np.array([-3.5, -2.5, -1.5, 1.5, 2.5, 3.5])) + divisor = np.array([2.] * 6) + t2.fmod_(divisor) + self.assertTrue(np.array_equal(t2.data, np.array([-1.5, -0.5, -1.5, 1.5, 0.5, 1.5]))) + + if __name__ == "__main__": unittest.main()