提交 7bae52b5 编写于 作者: G github-actions

fixup! Format Python code with psf/black push

上级 1e2aa119
......@@ -27,9 +27,11 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
[[-1, -0.5], [-1, -1.5]]
"""
if _check_not_integer(matrix_a)\
and _check_not_integer(matrix_b)\
and _verify_matrix_sizes(matrix_a, matrix_b):
if (
_check_not_integer(matrix_a)
and _check_not_integer(matrix_b)
and _verify_matrix_sizes(matrix_a, matrix_b)
):
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
......@@ -60,8 +62,9 @@ def multiply(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) "
f"and ({rows[1]},{cols[1]})"
)
return [[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)]
for i in matrix_a]
return [
[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a
]
def identity(n: int) -> List[list]:
......@@ -95,8 +98,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]:
>>> minor([[1, 2], [3, 4]], 1, 1)
[[1]]
"""
minor = matrix[: row] + matrix[row + 1:]
return [row[:column] + row[column + 1:] for row in minor]
minor = matrix[:row] + matrix[row + 1 :]
return [row[:column] + row[column + 1 :] for row in minor]
def determinant(matrix: List[list]) -> int:
......@@ -109,8 +112,10 @@ def determinant(matrix: List[list]) -> int:
if len(matrix) == 1:
return matrix[0][0]
return sum(x * determinant(minor(matrix, 0, i)) * (-1) ** i
for i, x in enumerate(matrix[0]))
return sum(
x * determinant(minor(matrix, 0, i)) * (-1) ** i
for i, x in enumerate(matrix[0])
)
def inverse(matrix: List[list]) -> List[list]:
......@@ -124,9 +129,10 @@ def inverse(matrix: List[list]) -> List[list]:
if det == 0:
return None
matrix_minor = [[determinant(minor(matrix, i, j))
for j in range(len(matrix))]
for i in range(len(matrix))]
matrix_minor = [
[determinant(minor(matrix, i, j)) for j in range(len(matrix))]
for i in range(len(matrix))
]
cofactors = [
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
......@@ -159,14 +165,10 @@ def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[li
def main():
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]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(
f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(
f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",
)
print(f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",)
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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册