-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path100-matrix_mul.py
executable file
·75 lines (55 loc) · 1.96 KB
/
100-matrix_mul.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
#!/usr/bin/python3
"""6. Matrix multiplication"""
def matrix_mul(m_a, m_b):
"""Function that multiplies 2 matrices"""
if not isinstance(m_a, list):
raise TypeError("m_a must be a list")
if not isinstance(m_b, list):
raise TypeError("m_b must be a list")
for elem in m_a:
if not isinstance(elem, list):
raise TypeError("m_a must be a list of lists")
for elem in m_b:
if not isinstance(elem, list):
raise TypeError("m_b must be a list of lists")
if len(m_a) == 0 or (len(m_a) == 1 and len(m_a[0]) == 0):
raise ValueError("m_a can't be empty")
if len(m_b) == 0 or (len(m_b) == 1 and len(m_b[0]) == 0):
raise ValueError("m_b can't be empty")
for lists in m_a:
for elem in lists:
if not type(elem) in (int, float):
raise TypeError("m_a should contain only integers or floats")
for lists in m_b:
for elem in lists:
if not type(elem) in (int, float):
raise TypeError("m_b should contain only integers or floats")
m_len = 0
for elem in m_a:
if m_len != 0 and m_len != len(elem):
raise TypeError("each row of m_a must be of the same size")
m_len = len(elem)
m_len = 0
for elem in m_b:
if m_len != 0 and m_len != len(elem):
raise TypeError("each row of m_b must be of the same size")
m_len = len(elem)
if len(m_a[0]) != len(m_b):
raise ValueError("m_a and m_b can't be multiplied")
row_1 = []
index_1 = 0
for elem in m_a:
row_2 = []
index_2 = 0
num = 0
while (index_2 < len(m_b[0])):
num += elem[index_1] * m_b[index_1][index_2]
if index_1 == len(m_b) - 1:
index_1 = 0
index_2 += 1
row_2.append(num)
num = 0
else:
index_1 += 1
row_1.append(row_2)
return row_1