-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-matrix_mul.py
executable file
·88 lines (70 loc) · 2.4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3
"""
Module composed by a function that multiplies 2 matrices
"""
def matrix_mul(m_a, m_b):
""" Function that multiplies 2 matrices
Args:
m_a: matrix a
m_b: matrix b
Returns:
result of the multiplication
Raises:
TypeError: if m_a or m_b aren't a list
TypeError: if m_a or m_b aren't a list of a lists
ValueError: if m_a or m_b are empty
TypeError: if the lists of m_a or m_b don't have integers or floats
TypeError: if the rows of m_a or m_b don't have the same size
ValueError: if m_a and m_b can't be multiplied
"""
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 elems in m_a:
if not isinstance(elems, list):
raise TypeError("m_a must be a list of lists")
for elems in m_b:
if not isinstance(elems, 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 elems in lists:
if not type(elems) in (int, float):
raise TypeError("m_a should contain only integers or floats")
for lists in m_b:
for elems in lists:
if not type(elems) in (int, float):
raise TypeError("m_b should contain only integers or floats")
length = 0
for elems in m_a:
if length != 0 and length != len(elems):
raise TypeError("each row of m_a must be of the same size")
length = len(elems)
length = 0
for elems in m_b:
if length != 0 and length != len(elems):
raise TypeError("each row of m_b must be of the same size")
length = len(elems)
if len(m_a[0]) != len(m_b):
raise ValueError("m_a and m_b can't be multiplied")
r1 = []
i1 = 0
for a in m_a:
r2 = []
i2 = 0
num = 0
while (i2 < len(m_b[0])):
num += a[i1] * m_b[i1][i2]
if i1 == len(m_b) - 1:
i1 = 0
i2 += 1
r2.append(num)
num = 0
else:
i1 += 1
r1.append(r2)
return r1