-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-matrix_mul.txt
executable file
·237 lines (176 loc) · 5.04 KB
/
100-matrix_mul.txt
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
============================
How to Use 100-matrix_mul.py
============================
This module defines a matrix multiplication function ``matrix_mul(m_a, m_b)``.
Usage
=====
``matrix_mul(...)`` returns a new matrix representing the multiplication of
``m_a`` by ``m_b``.
::
>>> matrix_mul = __import__('100-matrix_mul').matrix_mul
>>> m_a = [
... [1, 2],
... [3, 4],
... ]
>>> m_b = m_a
>>> print(matrix_mul(m_a, m_b))
[[7, 10], [15, 22]]
::
>>> m_a = [[1, 2]]
>>> m_b = [
... [3, 4],
... [5, 6]
... ]
>>> print(matrix_mul(m_a, m_b))
[[13, 16]]
The function also works with floating-point numbers.
::
>>> m_a = [
... [1.2, 5.5, 6.2],
... [4.66, 12.3, -9.2]
... ]
>>> m_b = [
... [5.0, 3.3],
... [-2.9, 4.4],
... [7.2, 4.4]
... ]
>>> print(matrix_mul(m_a, m_b))
[[34.69, 55.44000000000001], [-78.61, 29.018000000000008]]
Integers and floats can be combined.
::
>>> m_a = [
... [1, 2.2, 3.3, 4],
... [5, 6, 7, 8.8],
... ]
>>> m_b = [
... [1.1, 2, 3.3],
... [4.0, 5.5, 6],
... [7, 8, 9],
... [10.01, 11, 12.3]
... ]
>>> print(matrix_mul(m_a, m_b))
[[73.03999999999999, 84.5, 95.4], [166.58800000000002, 195.8, 223.74]]
A minimum of two arguments must be provided. Otherwise, a TypeError is raised.
::
>>> print(matrix_mul()) # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
TypeError: matrix_mul() missing 2 required positional arguments:
'm_a' and 'm_b'
::
>>> print(matrix_mul()) # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
TypeError: matrix_mul() missing 2 required positional arguments:
'm_a' and 'm_b'
ValueErrors
===========
If two matrices cannot be multiplied (ie. the row count of ``m_a`` is not
equal to the column count in ``m_b``), a ValueError is raised.
::
>>> m_a = [
... [1, 2],
... [3, 4],
... ]
>>> m_b = [
... [1, 2],
... [2, 3],
... [4, 5]
... ]
>>> print(matrix_mul(m_a, m_b))
Traceback (most recent call last):
ValueError: m_a and m_b can't be multiplied
The parameters ``m_a`` and ``m_b`` cannot be empty. Otherwise, a ValueError
is raised.
::
>>> print(matrix_mul([], [[1, 2]]))
Traceback (most recent call last):
ValueError: m_a can't be empty
::
>>> print(matrix_mul([[1, 2]], [[]]))
Traceback (most recent call last):
ValueError: m_b can't be empty
::
>>> print(matrix_mul([[]], []))
Traceback (most recent call last):
ValueError: m_a can't be empty
Invalid Matrices
================
The parameters ``m_a`` and ``m_b`` must be lists. If either parameter is
not a list, a TypeError is raised.
::
>>> print(matrix_mul("not a list", [[1, 2]]))
Traceback (most recent call last):
TypeError: m_a must be a list
::
>>> print(matrix_mul([[1, 2]], "also not a list"))
Traceback (most recent call last):
TypeError: m_b must be a list
::
>>> print(matrix_mul("not a list", "also not a list"))
Traceback (most recent call last):
TypeError: m_a must be a list
::
>>> print(matrix_mul(None, None))
Traceback (most recent call last):
TypeError: m_a must be a list
Not just any list - they *must* be lists of lists!
::
>>> print(matrix_mul([1, 2], [[3, 4]]))
Traceback (most recent call last):
TypeError: m_a must be a list of lists
::
>>> print(matrix_mul([[1, 2]], [3, 4]))
Traceback (most recent call last):
TypeError: m_b must be a list of lists
::
>>> print(matrix_mul([1, 2], [3, 4]))
Traceback (most recent call last):
TypeError: m_a must be a list of lists
And not just any list of lists - they *must* be lists of lists containing
integers or floats!
::
>>> print(matrix_mul([[1, "non-number"]], [[3, 4]]))
Traceback (most recent call last):
TypeError: m_a should contain only integers or floats
::
>>> print(matrix_mul([[1, 2]], [[{"a": 1}, 8.8]]))
Traceback (most recent call last):
TypeError: m_b should contain only integers or floats
::
>>> print(matrix_mul([[1, "non-number"]], [[{"a": 1}, 8.8]]))
Traceback (most recent call last):
TypeError: m_a should contain only integers or floats
Finally, the length of all rows in matrices ``m_a`` and ``m_b`` should be
equivalent. Otherwise, a TypeError is raised.
::
>>> m_a = [
... [1, 2],
... [3, 4, 5]
... ]
>>> m_b = [
... [1, 2],
... [3, 4]
... ]
>>> print(matrix_mul(m_a, m_b))
Traceback (most recent call last):
TypeError: each row of m_a must should be of the same size
::
>>> m_a = [
... [1, 2],
... [3, 4]
... ]
>>> m_b = [
... [1, 2],
... [3, 4, 5]
... ]
>>> print(matrix_mul(m_a, m_b))
Traceback (most recent call last):
TypeError: each row of m_b must should be of the same size
::
>>> m_a = [
... [1, 2],
... [3, 4, 5]
... ]
>>> m_b = m_a
>>> print(matrix_mul(m_a, m_b))
Traceback (most recent call last):
TypeError: each row of m_a must should be of the same size