Skip to content

Add missing type hints in matrix directory #6409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
9 changes: 5 additions & 4 deletions matrix/count_islands_in_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@


class matrix: # Public class to implement a graph
def __init__(self, row: int, col: int, graph: list):
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
self.ROW = row
self.COL = col
self.graph = graph

def is_safe(self, i, j, visited) -> bool:
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
return (
0 <= i < self.ROW
and 0 <= j < self.COL
and not visited[i][j]
and self.graph[i][j]
)

def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
# Checking all 8 elements surrounding nth element
rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]
visited[i][j] = True # Make those cells visited
Expand All @@ -33,4 +34,4 @@ def count_islands(self) -> int: # And finally, count all islands.
if visited[i][j] is False and self.graph[i][j] == 1:
self.diffs(i, j, visited)
count += 1
return count
return int(count)
84 changes: 41 additions & 43 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# An OOP approach to representing and manipulating matrices

from __future__ import annotations


class Matrix:
"""
Expand All @@ -17,23 +19,19 @@ class Matrix:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]

Matrix rows and columns are available as 2D arrays
>>> print(matrix.rows)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print(matrix.columns())
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Order is returned as a tuple
>>> matrix.order
(3, 3)

Squareness and invertability are represented as bool
>>> matrix.is_square
True
>>> matrix.is_invertable()
False

Identity, Minors, Cofactors and Adjugate are returned as Matrices. Inverse can be
a Matrix or Nonetype
>>> print(matrix.identity())
Expand All @@ -54,12 +52,12 @@ class Matrix:
[6. -12. 6.]
[-3. 6. -3.]]
>>> print(matrix.inverse())
None

Traceback (most recent call last):
...
TypeError: Only matrices with a non-zero determinant have an inverse
Determinant is an int, float, or Nonetype
>>> matrix.determinant()
0

Negation, scalar multiplication, addition, subtraction, multiplication and
exponentiation are available and all return a Matrix
>>> print(-matrix)
Expand All @@ -83,7 +81,6 @@ class Matrix:
[[468. 576. 684.]
[1062. 1305. 1548.]
[1656. 2034. 2412.]]

Matrices can also be modified
>>> matrix.add_row([10, 11, 12])
>>> print(matrix)
Expand All @@ -101,10 +98,9 @@ class Matrix:
[198. 243. 288. 304.]
[306. 378. 450. 472.]
[414. 513. 612. 640.]]

"""

def __init__(self, rows):
def __init__(self, rows: list[list[int]]):
error = TypeError(
"Matrices must be formed from a list of zero or more lists containing at "
"least one and the same number of values, each of which must be of type "
Expand All @@ -125,53 +121,53 @@ def __init__(self, rows):
self.rows = []

# MATRIX INFORMATION
def columns(self):
def columns(self) -> list[list[int]]:
return [[row[i] for row in self.rows] for i in range(len(self.rows[0]))]

@property
def num_rows(self):
def num_rows(self) -> int:
return len(self.rows)

@property
def num_columns(self):
def num_columns(self) -> int:
return len(self.rows[0])

@property
def order(self):
def order(self) -> tuple[int, int]:
return (self.num_rows, self.num_columns)

@property
def is_square(self):
def is_square(self) -> bool:
return self.order[0] == self.order[1]

def identity(self):
def identity(self) -> Matrix:
values = [
[0 if column_num != row_num else 1 for column_num in range(self.num_rows)]
for row_num in range(self.num_rows)
]
return Matrix(values)

def determinant(self):
def determinant(self) -> int:
if not self.is_square:
return None
return 0
if self.order == (0, 0):
return 1
if self.order == (1, 1):
return self.rows[0][0]
return int(self.rows[0][0])
if self.order == (2, 2):
return (self.rows[0][0] * self.rows[1][1]) - (
return int((self.rows[0][0] * self.rows[1][1]) - (
self.rows[0][1] * self.rows[1][0]
)
))
else:
return sum(
self.rows[0][column] * self.cofactors().rows[0][column]
for column in range(self.num_columns)
)

def is_invertable(self):
def is_invertable(self) -> bool:
return bool(self.determinant())

def get_minor(self, row, column):
def get_minor(self, row: int, column: int) -> int:
values = [
[
self.rows[other_row][other_column]
Expand All @@ -183,20 +179,20 @@ def get_minor(self, row, column):
]
return Matrix(values).determinant()

def get_cofactor(self, row, column):
def get_cofactor(self, row: int, column: int) -> int:
if (row + column) % 2 == 0:
return self.get_minor(row, column)
return -1 * self.get_minor(row, column)

def minors(self):
def minors(self) -> Matrix:
return Matrix(
[
[self.get_minor(row, column) for column in range(self.num_columns)]
for row in range(self.num_rows)
]
)

def cofactors(self):
def cofactors(self) -> Matrix:
return Matrix(
[
[
Expand All @@ -209,25 +205,27 @@ def cofactors(self):
]
)

def adjugate(self):
def adjugate(self) -> Matrix:
values = [
[self.cofactors().rows[column][row] for column in range(self.num_columns)]
for row in range(self.num_rows)
]
return Matrix(values)

def inverse(self):
def inverse(self) -> Matrix:
determinant = self.determinant()
return None if not determinant else self.adjugate() * (1 / determinant)
if not determinant:
raise TypeError("Only matrices with a non-zero determinant have an inverse")
return self.adjugate() * (1 / determinant)

def __repr__(self):
def __repr__(self) -> str:
return str(self.rows)

def __str__(self):
def __str__(self) -> str:
if self.num_rows == 0:
return "[]"
if self.num_rows == 1:
return "[[" + ". ".join(self.rows[0]) + "]]"
return "[[" + ". ".join(str(self.rows[0])) + "]]"
return (
"["
+ "\n ".join(
Expand All @@ -240,7 +238,7 @@ def __str__(self):
)

# MATRIX MANIPULATION
def add_row(self, row, position=None):
def add_row(self, row: list[int], position: int | None = None) -> None:
type_error = TypeError("Row must be a list containing all ints and/or floats")
if not isinstance(row, list):
raise type_error
Expand All @@ -256,7 +254,7 @@ def add_row(self, row, position=None):
else:
self.rows = self.rows[0:position] + [row] + self.rows[position:]

def add_column(self, column, position=None):
def add_column(self, column: list[int], position: int | None = None) -> None:
type_error = TypeError(
"Column must be a list containing all ints and/or floats"
)
Expand All @@ -278,18 +276,18 @@ def add_column(self, column, position=None):
]

# MATRIX OPERATIONS
def __eq__(self, other):
def __eq__(self, other: object) -> bool:
if not isinstance(other, Matrix):
raise TypeError("A Matrix can only be compared with another Matrix")
return self.rows == other.rows

def __ne__(self, other):
def __ne__(self, other: object) -> bool:
return not self == other

def __neg__(self):
def __neg__(self) -> Matrix:
return self * -1

def __add__(self, other):
def __add__(self, other: Matrix) -> Matrix:
if self.order != other.order:
raise ValueError("Addition requires matrices of the same order")
return Matrix(
Expand All @@ -299,7 +297,7 @@ def __add__(self, other):
]
)

def __sub__(self, other):
def __sub__(self, other: Matrix) -> Matrix:
if self.order != other.order:
raise ValueError("Subtraction requires matrices of the same order")
return Matrix(
Expand All @@ -309,9 +307,9 @@ def __sub__(self, other):
]
)

def __mul__(self, other):
def __mul__(self, other: Matrix | int | float) -> Matrix:
if isinstance(other, (int, float)):
return Matrix([[element * other for element in row] for row in self.rows])
return Matrix([[int(element * other) for element in row] for row in self.rows])
elif isinstance(other, Matrix):
if self.num_columns != other.num_rows:
raise ValueError(
Expand All @@ -329,7 +327,7 @@ def __mul__(self, other):
"A Matrix can only be multiplied by an int, float, or another matrix"
)

def __pow__(self, other):
def __pow__(self, other: int) -> Matrix:
if not isinstance(other, int):
raise TypeError("A Matrix can only be raised to the power of an int")
if not self.is_square:
Expand All @@ -348,7 +346,7 @@ def __pow__(self, other):
return result

@classmethod
def dot_product(cls, row, column):
def dot_product(cls, row: list[int], column: list[int]) -> int:
return sum(row[i] * column[i] for i in range(len(row)))


Expand Down
Loading