Add missing type hints in matrix directory (#6612)

* Update count_islands_in_matrix.py

* Update matrix_class.py

* Update matrix_operation.py

* Update nth_fibonacci_using_matrix_exponentiation.py

* Update searching_in_sorted_matrix.py

* Update count_islands_in_matrix.py

* Update matrix_class.py

* Update matrix_operation.py

* Update rotate_matrix.py

* Update sherman_morrison.py

* Update spiral_print.py

* Update count_islands_in_matrix.py

* formatting

* formatting

* Update matrix_class.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Rohan R Bharadwaj
2022-10-04 23:35:56 +05:30
committed by GitHub
parent a84fb58271
commit 46842e8c5b
8 changed files with 142 additions and 119 deletions

View File

@ -1,14 +1,18 @@
from __future__ import annotations
from typing import Any
class Matrix:
"""
<class Matrix>
Matrix structure.
"""
def __init__(self, row: int, column: int, default_value: float = 0):
def __init__(self, row: int, column: int, default_value: float = 0) -> None:
"""
<method Matrix.__init__>
Initialize matrix with given size and default value.
Example:
>>> a = Matrix(2, 3, 1)
>>> a
@ -20,7 +24,7 @@ class Matrix:
self.row, self.column = row, column
self.array = [[default_value for c in range(column)] for r in range(row)]
def __str__(self):
def __str__(self) -> str:
"""
<method Matrix.__str__>
Return string representation of this matrix.
@ -37,7 +41,7 @@ class Matrix:
string_format_identifier = "%%%ds" % (max_element_length,)
# Make string and return
def single_line(row_vector):
def single_line(row_vector: list[float]) -> str:
nonlocal string_format_identifier
line = "["
line += ", ".join(string_format_identifier % (obj,) for obj in row_vector)
@ -47,14 +51,13 @@ class Matrix:
s += "\n".join(single_line(row_vector) for row_vector in self.array)
return s
def __repr__(self):
def __repr__(self) -> str:
return str(self)
def validateIndices(self, loc: tuple):
def validateIndices(self, loc: tuple[int, int]) -> bool:
"""
<method Matrix.validateIndices>
Check if given indices are valid to pick element from matrix.
Example:
>>> a = Matrix(2, 6, 0)
>>> a.validateIndices((2, 7))
@ -69,11 +72,10 @@ class Matrix:
else:
return True
def __getitem__(self, loc: tuple):
def __getitem__(self, loc: tuple[int, int]) -> Any:
"""
<method Matrix.__getitem__>
Return array[row][column] where loc = (row, column).
Example:
>>> a = Matrix(3, 2, 7)
>>> a[1, 0]
@ -82,11 +84,10 @@ class Matrix:
assert self.validateIndices(loc)
return self.array[loc[0]][loc[1]]
def __setitem__(self, loc: tuple, value: float):
def __setitem__(self, loc: tuple[int, int], value: float) -> None:
"""
<method Matrix.__setitem__>
Set array[row][column] = value where loc = (row, column).
Example:
>>> a = Matrix(2, 3, 1)
>>> a[1, 2] = 51
@ -98,11 +99,10 @@ class Matrix:
assert self.validateIndices(loc)
self.array[loc[0]][loc[1]] = value
def __add__(self, another):
def __add__(self, another: Matrix) -> Matrix:
"""
<method Matrix.__add__>
Return self + another.
Example:
>>> a = Matrix(2, 1, -4)
>>> b = Matrix(2, 1, 3)
@ -123,11 +123,10 @@ class Matrix:
result[r, c] = self[r, c] + another[r, c]
return result
def __neg__(self):
def __neg__(self) -> Matrix:
"""
<method Matrix.__neg__>
Return -self.
Example:
>>> a = Matrix(2, 2, 3)
>>> a[0, 1] = a[1, 0] = -2
@ -143,14 +142,13 @@ class Matrix:
result[r, c] = -self[r, c]
return result
def __sub__(self, another):
def __sub__(self, another: Matrix) -> Matrix:
return self + (-another)
def __mul__(self, another):
def __mul__(self, another: int | float | Matrix) -> Matrix:
"""
<method Matrix.__mul__>
Return self * another.
Example:
>>> a = Matrix(2, 3, 1)
>>> a[0,2] = a[1,2] = 3
@ -177,11 +175,10 @@ class Matrix:
else:
raise TypeError(f"Unsupported type given for another ({type(another)})")
def transpose(self):
def transpose(self) -> Matrix:
"""
<method Matrix.transpose>
Return self^T.
Example:
>>> a = Matrix(2, 3)
>>> for r in range(2):
@ -201,7 +198,7 @@ class Matrix:
result[c, r] = self[r, c]
return result
def ShermanMorrison(self, u, v):
def ShermanMorrison(self, u: Matrix, v: Matrix) -> Any:
"""
<method Matrix.ShermanMorrison>
Apply Sherman-Morrison formula in O(n^2).
@ -211,7 +208,6 @@ class Matrix:
impossible to calculate.
Warning: This method doesn't check if self is invertible.
Make sure self is invertible before execute this method.
Example:
>>> ainv = Matrix(3, 3, 0)
>>> for i in range(3): ainv[i,i] = 1
@ -243,7 +239,7 @@ class Matrix:
# Testing
if __name__ == "__main__":
def test1():
def test1() -> None:
# a^(-1)
ainv = Matrix(3, 3, 0)
for i in range(3):
@ -256,11 +252,11 @@ if __name__ == "__main__":
v[0, 0], v[1, 0], v[2, 0] = 4, -2, 5
print(f"u is {u}")
print(f"v is {v}")
print(f"uv^T is {u * v.transpose()}")
print("uv^T is %s" % (u * v.transpose()))
# Sherman Morrison
print(f"(a + uv^T)^(-1) is {ainv.ShermanMorrison(u, v)}")
def test2():
def test2() -> None:
import doctest
doctest.testmod()