Skip to content

Commit

Permalink
⚡ Improve some code regarding zero matrix (#4)
Browse files Browse the repository at this point in the history
* ⚡ Improved code regarding zero matrix

* ✅ Added more tests regarding zero matrix

* 🔖 Bumped the version
  • Loading branch information
sarmadgulzar authored Sep 30, 2021
1 parent d8769e7 commit eae4774
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion matops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Matrix Operations"""

__version__ = "0.1.1"
__version__ = "0.1.2"

from .matrix import Matrix
5 changes: 1 addition & 4 deletions matops/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __sub__(self, other: "Matrix") -> "Matrix":
def __mul__(self, other: Union["Matrix", Number]) -> "Matrix":
if isinstance(other, (int, float)):
temp = self._get_zero_matrix()
temp = [[0 for _ in range(self.num_cols)] for _ in range(self.num_rows)]
for i in range(self.num_rows):
for j in range(self.num_cols):
temp[i][j] = self.rows[i][j] * other
Expand Down Expand Up @@ -103,9 +102,7 @@ def is_square_matrix(self) -> bool:
return self.num_rows == self.num_cols

def is_zero_matrix(self) -> bool:
return [
[0 for _ in range(self.num_cols)] for _ in range(self.num_rows)
] == self.rows
return self._get_zero_matrix() == self.rows

def is_symmetric_matrix(self) -> bool:
return self == self.transpose()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ def test_zero_matrix_1():


def test_zero_matrix_2():
m = Matrix(
[
[0],
[0],
]
)
assert m.is_zero_matrix()


def test_zero_matrix_3():
m = Matrix(
[
[1, 0],
Expand Down

0 comments on commit eae4774

Please sign in to comment.