2019-07-20 03:06:29 +00:00
|
|
|
"""
|
2020-07-03 19:28:16 +00:00
|
|
|
Functions for 2D matrix operations
|
2019-07-20 03:06:29 +00:00
|
|
|
"""
|
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
from typing import List, Tuple
|
2019-06-11 11:24:53 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
|
2020-07-06 22:22:44 +00:00
|
|
|
def add(*matrix_s: List[list]) -> List[list]:
|
2020-07-03 19:28:16 +00:00
|
|
|
"""
|
|
|
|
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
|
|
|
|
[[3, 5], [7, 9]]
|
|
|
|
>>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
|
|
|
|
[[3.2, 5.4], [7, 9]]
|
2020-07-06 22:22:44 +00:00
|
|
|
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
|
|
|
|
[[7, 14], [12, 16]]
|
2020-07-03 19:28:16 +00:00
|
|
|
"""
|
2020-07-06 22:22:44 +00:00
|
|
|
if all(_check_not_integer(m) for m in matrix_s):
|
|
|
|
a, *b = matrix_s
|
|
|
|
for matrix in b:
|
|
|
|
_verify_matrix_sizes(a, matrix)
|
|
|
|
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
|
2019-07-20 03:06:29 +00:00
|
|
|
|
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> subtract([[1,2],[3,4]],[[2,3],[4,5]])
|
|
|
|
[[-1, -1], [-1, -1]]
|
|
|
|
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
|
|
|
|
[[-1, -0.5], [-1, -1.5]]
|
|
|
|
"""
|
2019-07-20 03:06:29 +00:00
|
|
|
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
|
2020-07-03 19:28:16 +00:00
|
|
|
_verify_matrix_sizes(matrix_a, matrix_b)
|
2020-07-06 22:22:44 +00:00
|
|
|
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
|
2019-07-20 03:06:29 +00:00
|
|
|
|
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def scalar_multiply(matrix: List[list], n: int) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> scalar_multiply([[1,2],[3,4]],5)
|
|
|
|
[[5, 10], [15, 20]]
|
|
|
|
>>> scalar_multiply([[1.4,2.3],[3,4]],5)
|
|
|
|
[[7.0, 11.5], [15, 20]]
|
|
|
|
"""
|
2019-03-27 17:50:43 +00:00
|
|
|
return [[x * n for x in row] for row in matrix]
|
2018-10-31 09:47:11 +00:00
|
|
|
|
2019-07-20 03:06:29 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def multiply(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> multiply([[1,2],[3,4]],[[5,5],[7,5]])
|
|
|
|
[[19, 15], [43, 35]]
|
|
|
|
>>> multiply([[1,2.5],[3,4.5]],[[5,5],[7,5]])
|
|
|
|
[[22.5, 17.5], [46.5, 37.5]]
|
|
|
|
"""
|
2019-07-20 03:06:29 +00:00
|
|
|
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
|
|
|
|
matrix_c = []
|
|
|
|
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
|
|
|
|
|
|
|
|
if cols[0] != rows[1]:
|
2019-10-05 05:14:13 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) "
|
|
|
|
f"and ({rows[1]},{cols[1]})"
|
|
|
|
)
|
2019-07-20 03:06:29 +00:00
|
|
|
for i in range(rows[0]):
|
|
|
|
list_1 = []
|
|
|
|
for j in range(cols[1]):
|
|
|
|
val = 0
|
|
|
|
for k in range(cols[1]):
|
2020-07-03 19:28:16 +00:00
|
|
|
val += matrix_a[i][k] * matrix_b[k][j]
|
2019-07-20 03:06:29 +00:00
|
|
|
list_1.append(val)
|
|
|
|
matrix_c.append(list_1)
|
|
|
|
return matrix_c
|
|
|
|
|
2018-10-31 09:47:11 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def identity(n: int) -> List[list]:
|
2019-07-20 03:06:29 +00:00
|
|
|
"""
|
|
|
|
:param n: dimension for nxn matrix
|
|
|
|
:type n: int
|
|
|
|
:return: Identity matrix of shape [n, n]
|
2020-07-03 19:28:16 +00:00
|
|
|
>>> identity(3)
|
|
|
|
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
2019-07-20 03:06:29 +00:00
|
|
|
"""
|
|
|
|
n = int(n)
|
2019-10-05 05:14:13 +00:00
|
|
|
return [[int(row == column) for column in range(n)] for row in range(n)]
|
2019-03-27 17:50:43 +00:00
|
|
|
|
2019-07-20 03:06:29 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def transpose(matrix: List[list], return_map: bool = True) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
|
|
|
|
<map object at ...
|
|
|
|
>>> transpose([[1,2],[3,4]], return_map=False)
|
|
|
|
[[1, 3], [2, 4]]
|
|
|
|
"""
|
2019-07-20 03:06:29 +00:00
|
|
|
if _check_not_integer(matrix):
|
|
|
|
if return_map:
|
|
|
|
return map(list, zip(*matrix))
|
|
|
|
else:
|
|
|
|
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
|
|
|
|
|
2019-03-27 17:50:43 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def minor(matrix: List[list], row: int, column: int) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> minor([[1, 2], [3, 4]], 1, 1)
|
|
|
|
[[1]]
|
|
|
|
"""
|
2020-07-14 08:23:14 +00:00
|
|
|
minor = matrix[:row] + matrix[row + 1 :]
|
|
|
|
return [row[:column] + row[column + 1 :] for row in minor]
|
2019-03-27 17:50:43 +00:00
|
|
|
|
2019-07-20 03:06:29 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def determinant(matrix: List[list]) -> int:
|
|
|
|
"""
|
|
|
|
>>> determinant([[1, 2], [3, 4]])
|
|
|
|
-2
|
|
|
|
>>> determinant([[1.5, 2.5], [3, 4]])
|
|
|
|
-1.5
|
|
|
|
"""
|
2019-07-20 03:06:29 +00:00
|
|
|
if len(matrix) == 1:
|
|
|
|
return matrix[0][0]
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-03-27 17:50:43 +00:00
|
|
|
res = 0
|
|
|
|
for x in range(len(matrix)):
|
2019-07-20 03:06:29 +00:00
|
|
|
res += matrix[0][x] * determinant(minor(matrix, 0, x)) * (-1) ** x
|
2019-03-27 17:50:43 +00:00
|
|
|
return res
|
|
|
|
|
2019-07-20 03:06:29 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def inverse(matrix: List[list]) -> List[list]:
|
|
|
|
"""
|
|
|
|
>>> inverse([[1, 2], [3, 4]])
|
|
|
|
[[-2.0, 1.0], [1.5, -0.5]]
|
|
|
|
>>> inverse([[1, 1], [1, 1]])
|
|
|
|
"""
|
|
|
|
# https://stackoverflow.com/questions/20047519/python-doctests-test-for-none
|
2019-03-27 17:50:43 +00:00
|
|
|
det = determinant(matrix)
|
2019-07-20 03:06:29 +00:00
|
|
|
if det == 0:
|
|
|
|
return None
|
2019-03-27 17:50:43 +00:00
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
matrix_minor = [[] for _ in matrix]
|
2019-03-27 17:50:43 +00:00
|
|
|
for i in range(len(matrix)):
|
|
|
|
for j in range(len(matrix)):
|
2019-07-20 03:06:29 +00:00
|
|
|
matrix_minor[i].append(determinant(minor(matrix, i, j)))
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
cofactors = [
|
|
|
|
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
|
|
|
|
for row in range(len(matrix))
|
|
|
|
]
|
2019-03-27 17:50:43 +00:00
|
|
|
adjugate = transpose(cofactors)
|
2019-10-05 05:14:13 +00:00
|
|
|
return scalar_multiply(adjugate, 1 / det)
|
2019-07-20 03:06:29 +00:00
|
|
|
|
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def _check_not_integer(matrix: List[list]) -> bool:
|
2020-02-11 08:29:09 +00:00
|
|
|
if not isinstance(matrix, int) and not isinstance(matrix[0], int):
|
2019-07-20 03:06:29 +00:00
|
|
|
return True
|
2020-02-11 08:29:09 +00:00
|
|
|
raise TypeError("Expected a matrix, got int/list instead")
|
2019-07-20 03:06:29 +00:00
|
|
|
|
|
|
|
|
2020-07-03 19:28:16 +00:00
|
|
|
def _shape(matrix: List[list]) -> list:
|
2019-07-20 03:06:29 +00:00
|
|
|
return list((len(matrix), len(matrix[0])))
|
|
|
|
|
|
|
|
|
2020-07-14 08:23:14 +00:00
|
|
|
def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[list]:
|
2019-07-20 03:06:29 +00:00
|
|
|
shape = _shape(matrix_a)
|
|
|
|
shape += _shape(matrix_b)
|
|
|
|
if shape[0] != shape[2] or shape[1] != shape[3]:
|
2019-10-05 05:14:13 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"operands could not be broadcast together with shape "
|
|
|
|
f"({shape[0], shape[1]}), ({shape[2], shape[3]})"
|
|
|
|
)
|
2019-07-20 03:06:29 +00:00
|
|
|
return [shape[0], shape[2]], [shape[1], shape[3]]
|
|
|
|
|
2018-10-31 09:47:11 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
matrix_a = [[12, 10], [3, 9]]
|
|
|
|
matrix_b = [[3, 4], [7, 4]]
|
2020-07-14 08:23:14 +00:00
|
|
|
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
|
2019-03-27 17:50:43 +00:00
|
|
|
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
|
2020-07-14 08:23:14 +00:00
|
|
|
print(f"Add Operation, {matrix_a} + {matrix_b} =" f"{add(matrix_a, matrix_b)} \n")
|
2019-10-05 05:14:13 +00:00
|
|
|
print(
|
2020-07-03 19:28:16 +00:00
|
|
|
f"Multiply Operation, {matrix_a} * {matrix_b}",
|
|
|
|
f"= {multiply(matrix_a, matrix_b)} \n",
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2020-07-03 19:28:16 +00:00
|
|
|
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"Inverse of {matrix_d} = {inverse(matrix_d)}\n")
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-07-03 19:28:16 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
2018-10-31 09:47:11 +00:00
|
|
|
main()
|