Skip to content

Add missing type hints in matrix directory #6612

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

Merged
merged 17 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a docstring because comments will not appear in docs.

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 Down
80 changes: 44 additions & 36 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 Down Expand Up @@ -54,7 +56,9 @@ class Matrix:
[6. -12. 6.]
[-3. 6. -3.]]
>>> print(matrix.inverse())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the print()

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()
Expand Down Expand Up @@ -101,10 +105,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 +128,54 @@ 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]) - (
self.rows[0][1] * self.rows[1][0]
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 +187,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 +213,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 +246,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 +262,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 +284,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 +305,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 +315,11 @@ 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 +337,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 +356,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
34 changes: 19 additions & 15 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from __future__ import annotations

from typing import Any

def add(*matrix_s: list[list]) -> list[list]:

def add(*matrix_s: list[list[int]]) -> list[list[int]]:
"""
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
[[3, 5], [7, 9]]
Expand All @@ -25,7 +27,7 @@ def add(*matrix_s: list[list]) -> list[list]:
raise TypeError("Expected a matrix, got int/list instead")


def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
def subtract(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[int]]:
"""
>>> subtract([[1,2],[3,4]],[[2,3],[4,5]])
[[-1, -1], [-1, -1]]
Expand All @@ -45,7 +47,7 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
raise TypeError("Expected a matrix, got int/list instead")


def scalar_multiply(matrix: list[list], n: int | float) -> list[list]:
def scalar_multiply(matrix: list[list[int]], n: int | float) -> list[list[float]]:
"""
>>> scalar_multiply([[1,2],[3,4]],5)
[[5, 10], [15, 20]]
Expand All @@ -55,7 +57,7 @@ def scalar_multiply(matrix: list[list], n: int | float) -> list[list]:
return [[x * n for x in row] for row in matrix]


def multiply(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
def multiply(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[int]]:
"""
>>> multiply([[1,2],[3,4]],[[5,5],[7,5]])
[[19, 15], [43, 35]]
Expand All @@ -77,7 +79,7 @@ def multiply(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
]


def identity(n: int) -> list[list]:
def identity(n: int) -> list[list[int]]:
"""
:param n: dimension for nxn matrix
:type n: int
Expand All @@ -89,7 +91,9 @@ def identity(n: int) -> list[list]:
return [[int(row == column) for column in range(n)] for row in range(n)]


def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[list]:
def transpose(
matrix: list[list[int]], return_map: bool = True
) -> list[list[int]] | map[list[int]]:
"""
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
<map object at ...
Expand All @@ -108,7 +112,7 @@ def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[l
raise TypeError("Expected a matrix, got int/list instead")


def minor(matrix: list[list], row: int, column: int) -> list[list]:
def minor(matrix: list[list[int]], row: int, column: int) -> list[list[int]]:
"""
>>> minor([[1, 2], [3, 4]], 1, 1)
[[1]]
Expand All @@ -117,7 +121,7 @@ def minor(matrix: list[list], row: int, column: int) -> list[list]:
return [row[:column] + row[column + 1 :] for row in minor]


def determinant(matrix: list[list]) -> int:
def determinant(matrix: list[list[int]]) -> Any:
"""
>>> determinant([[1, 2], [3, 4]])
-2
Expand All @@ -133,7 +137,7 @@ def determinant(matrix: list[list]) -> int:
)


def inverse(matrix: list[list]) -> list[list] | None:
def inverse(matrix: list[list[int]]) -> list[list[float]] | None:
"""
>>> inverse([[1, 2], [3, 4]])
[[-2.0, 1.0], [1.5, -0.5]]
Expand All @@ -157,27 +161,27 @@ def inverse(matrix: list[list]) -> list[list] | None:
return scalar_multiply(adjugate, 1 / det)


def _check_not_integer(matrix: list[list]) -> bool:
def _check_not_integer(matrix: list[list[int]]) -> bool:
return not isinstance(matrix, int) and not isinstance(matrix[0], int)


def _shape(matrix: list[list]) -> tuple[int, int]:
def _shape(matrix: list[list[int]]) -> tuple[int, int]:
return len(matrix), len(matrix[0])


def _verify_matrix_sizes(
matrix_a: list[list], matrix_b: list[list]
) -> tuple[tuple, tuple]:
matrix_a: list[list[int]], matrix_b: list[list[int]]
) -> tuple[tuple[int, int], tuple[int, int]]:
shape = _shape(matrix_a) + _shape(matrix_b)
if shape[0] != shape[3] or shape[1] != shape[2]:
raise ValueError(
"operands could not be broadcast together with shape "
f"operands could not be broadcast together with shape "
f"({shape[0], shape[1]}), ({shape[2], shape[3]})"
)
return (shape[0], shape[2]), (shape[1], shape[3])


def main():
def main() -> None:
matrix_a = [[12, 10], [3, 9]]
matrix_b = [[3, 4], [7, 4]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
Expand Down
Loading