forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_operation.py
154 lines (122 loc) · 4.52 KB
/
matrix_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
function based version of matrix operations, which are just 2D arrays
"""
def add(matrix_a, matrix_b):
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
matrix_c = []
for i in range(rows[0]):
list_1 = []
for j in range(cols[0]):
val = matrix_a[i][j] + matrix_b[i][j]
list_1.append(val)
matrix_c.append(list_1)
return matrix_c
def subtract(matrix_a, matrix_b):
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
matrix_c = []
for i in range(rows[0]):
list_1 = []
for j in range(cols[0]):
val = matrix_a[i][j] - matrix_b[i][j]
list_1.append(val)
matrix_c.append(list_1)
return matrix_c
def scalar_multiply(matrix, n):
return [[x * n for x in row] for row in matrix]
def multiply(matrix_a, matrix_b):
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]:
raise ValueError(
f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) "
f"and ({rows[1]},{cols[1]})"
)
for i in range(rows[0]):
list_1 = []
for j in range(cols[1]):
val = 0
for k in range(cols[1]):
val = val + matrix_a[i][k] * matrix_b[k][j]
list_1.append(val)
matrix_c.append(list_1)
return matrix_c
def identity(n):
"""
:param n: dimension for nxn matrix
:type n: int
:return: Identity matrix of shape [n, n]
"""
n = int(n)
return [[int(row == column) for column in range(n)] for row in range(n)]
def transpose(matrix, return_map=True):
if _check_not_integer(matrix):
if return_map:
return map(list, zip(*matrix))
else:
# mt = []
# for i in range(len(matrix[0])):
# mt.append([row[i] for row in matrix])
# return mt
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
def minor(matrix, row, column):
minor = matrix[:row] + matrix[row + 1 :]
minor = [row[:column] + row[column + 1 :] for row in minor]
return minor
def determinant(matrix):
if len(matrix) == 1:
return matrix[0][0]
res = 0
for x in range(len(matrix)):
res += matrix[0][x] * determinant(minor(matrix, 0, x)) * (-1) ** x
return res
def inverse(matrix):
det = determinant(matrix)
if det == 0:
return None
matrix_minor = [[] for _ in range(len(matrix))]
for i in range(len(matrix)):
for j in range(len(matrix)):
matrix_minor[i].append(determinant(minor(matrix, i, j)))
cofactors = [
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
for row in range(len(matrix))
]
adjugate = transpose(cofactors)
return scalar_multiply(adjugate, 1 / det)
def _check_not_integer(matrix):
if not isinstance(matrix, int) and not isinstance(matrix[0], int):
return True
raise TypeError("Expected a matrix, got int/list instead")
def _shape(matrix):
return list((len(matrix), len(matrix[0])))
def _verify_matrix_sizes(matrix_a, matrix_b):
shape = _shape(matrix_a)
shape += _shape(matrix_b)
if shape[0] != shape[2] or shape[1] != shape[3]:
raise ValueError(
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():
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_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(
"Add Operation, %s + %s = %s \n"
% (matrix_a, matrix_b, (add(matrix_a, matrix_b)))
)
print(
"Multiply Operation, %s * %s = %s \n"
% (matrix_a, matrix_b, multiply(matrix_a, matrix_b))
)
print("Identity: %s \n" % identity(5))
print("Minor of {} = {} \n".format(matrix_c, minor(matrix_c, 1, 2)))
print("Determinant of {} = {} \n".format(matrix_b, determinant(matrix_b)))
print("Inverse of {} = {}\n".format(matrix_d, inverse(matrix_d)))
if __name__ == "__main__":
main()