Skip to content
Merged
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
14 changes: 14 additions & 0 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
if (
_check_not_integer(matrix_a)
and _check_not_integer(matrix_b)
and _verify_matrix_sizes(matrix_a, matrix_b)

Check warning on line 44 in matrix/matrix_operation.py

View workflow job for this annotation

GitHub Actions / ty

ty (redundant-condition)

matrix/matrix_operation.py:44:13: redundant-condition: Expression `_verify_matrix_sizes(matrix_a, matrix_b)` is always truthy (has type `tuple[tuple[int, int], tuple[int, int]]`)
):
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
raise TypeError("Expected a matrix, got int/list instead")
Expand Down Expand Up @@ -138,6 +138,19 @@
)


def permanent(matrix: list[list[int]]) -> Any:
"""
>>> permanent([[1, 2], [3, 4]])
10
>>> permanent([[1.5, 2.5], [3, 4]])
13.5
"""
if len(matrix) == 1:
return matrix[0][0]

return sum(x * permanent(minor(matrix, 0, i)) for i, x in enumerate(matrix[0]))


def inverse(matrix: list[list[int]]) -> list[list[float]] | None:
"""
>>> inverse([[1, 2], [3, 4]])
Expand Down Expand Up @@ -193,6 +206,7 @@
print(f"Identity: {identity(5)}\n")
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")
print(f"Permanent of {matrix_b} = {permanent(matrix_b)} \n")
print(f"Inverse of {matrix_d} = {inverse(matrix_d)}\n")


Expand Down
Loading