diff --git a/matops/__init__.py b/matops/__init__.py index 827703d..20547b0 100644 --- a/matops/__init__.py +++ b/matops/__init__.py @@ -1,5 +1,5 @@ """Matrix Operations""" -__version__ = "0.1.2" +__version__ = "0.1.3" from .matrix import Matrix diff --git a/matops/matrix.py b/matops/matrix.py index 8c8d33c..3e171f9 100644 --- a/matops/matrix.py +++ b/matops/matrix.py @@ -13,6 +13,13 @@ def __init__(self, rows: List[List[Number]]) -> None: def __str__(self) -> str: return str(self.rows) + def __round__(self, ndigits: int = None) -> "Matrix": + temp = self._get_zero_matrix() + for i in range(self.num_rows): + for j in range(self.num_cols): + temp[i][j] = round(self.rows[i][j], ndigits) + return Matrix(temp) + def __add__(self, other: "Matrix") -> "Matrix": temp = self._get_zero_matrix() for i in range(self.num_rows): diff --git a/tests/test_matrix.py b/tests/test_matrix.py index 4228701..c406081 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -564,3 +564,23 @@ def test_proof_1(): ) assert (m1 * m2).transpose() == m2.transpose() * m1.transpose() + + +def test_round_1(): + m1 = Matrix( + [ + [1.1123, -2.982143], + [3.21043, 4.4253], + ] + ) + assert round(m1) == Matrix([[1, -3], [3, 4]]) + + +def test_round_2(): + m1 = Matrix( + [ + [1.1123, -2.982143], + [3.21043, 4.4253], + ] + ) + assert round(m1, 2) == Matrix([[1.11, -2.98], [3.21, 4.43]])