-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path101-lazy_matrix_mul.txt
More file actions
executable file
·218 lines (160 loc) · 4.9 KB
/
Copy path101-lazy_matrix_mul.txt
File metadata and controls
executable file
·218 lines (160 loc) · 4.9 KB
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
=================================
How to Use 101-lazy_matrix_mul.py
=================================
This module defines a matrix multiplication function
``lazy_matrix_mul(m_a, m_b)``.
Usage
=====
``lazy_matrix_mul(...)`` returns a new matrix representing the multiplication
of ``m_a`` by ``m_b``.
::
>>> lazy_matrix_mul = __import__('101-lazy_matrix_mul').lazy_matrix_mul
>>> m_a = [
... [1, 2],
... [3, 4],
... ]
>>> m_b = m_a
>>> print(lazy_matrix_mul(m_a, m_b))
[[ 7 10]
[15 22]]
::
>>> m_a = [[1, 2]]
>>> m_b = [
... [3, 4],
... [5, 6]
... ]
>>> print(lazy_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(lazy_matrix_mul(m_a, m_b))
[[ 34.69 55.44 ]
[-78.61 29.018]]
A minimum of two arguments must be provided.
::
>>> print(lazy_matrix_mul(m_a))
Traceback (most recent call last):
TypeError: lazy_matrix_mul() missing 1 required positional argument: 'm_b'
::
>>> print(lazy_matrix_mul()) # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
TypeError: lazy_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(lazy_matrix_mul(m_a, m_b))
Traceback (most recent call last):
ValueError: shapes (2,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)
The parameters ``m_a`` and ``m_b`` cannot be empty. Otherwise, a ValueError
is raised.
::
>>> print(lazy_matrix_mul([[]], [[5, 6], [7, 8]]))
Traceback (most recent call last):
ValueError: shapes (1,0) and (2,2) not aligned: 0 (dim 1) != 2 (dim 0)
::
>>> print(lazy_matrix_mul([[5, 6], [7, 8]], [[]]))
Traceback (most recent call last):
ValueError: shapes (2,2) and (1,0) not aligned: 2 (dim 1) != 1 (dim 0)
Invalid Matrices
================
The parameters ``m_a`` and ``m_b`` must be lists. If either parameter is
not a list, a ValueError is raised.
::
>>> print(lazy_matrix_mul("not a list", [[1, 2]]))
Traceback (most recent call last):
ValueError: Scalar operands are not allowed, use '*' instead
::
>>> print(lazy_matrix_mul([[1, 2]], "also not a list"))
Traceback (most recent call last):
ValueError: Scalar operands are not allowed, use '*' instead
::
>>> print(lazy_matrix_mul("not a list", "also not a list"))
Traceback (most recent call last):
ValueError: Scalar operands are not allowed, use '*' instead
If either parameter is ``None``, a TypeError is raised.
::
>>> print(lazy_matrix_mul(None, None))
Traceback (most recent call last):
TypeError: Object arrays are not currently supported
Not just any list - they *must* be lists of lists! Otherwise, behavior is
undefined.
::
>>> print(lazy_matrix_mul([1, 2], [[3, 4]]))
Traceback (most recent call last):
ValueError: shapes (2,) and (1,2) not aligned: 2 (dim 0) != 1 (dim 0)
::
>>> print(lazy_matrix_mul([[1, 2]], [3, 4]))
[11]
::
>>> print(lazy_matrix_mul([1, 2], [3, 4]))
11
And not just any list of lists - they *must* be lists of lists containing
integers or floats! Otherwise, a ValueError or TypeError is raised
::
>>> print(lazy_matrix_mul([[1, "non-number"]], [[3, 4]]))
Traceback (most recent call last):
ValueError: shapes (1,2) and (1,2) not aligned: 2 (dim 1) != 1 (dim 0)
::
>>> print(lazy_matrix_mul([[5, 6], [7, 8]], [[5, "6"], [7, 8]]))
Traceback (most recent call last):
TypeError: invalid data type for einsum
::
>>> print(lazy_matrix_mul([[1, "non-number"]], [[{"a": 1}, 8.8]]))
Traceback (most recent call last):
TypeError: Object arrays are not currently supported
Finally, the length of all rows in matrices ``m_a`` and ``m_b`` should be
equivalent. Otherwise, a ValueError is raised.
::
>>> m_a = [
... [1, 2],
... [3, 4, 5]
... ]
>>> m_b = [
... [1, 2],
... [3, 4]
... ]
>>> print(lazy_matrix_mul(m_a, m_b))
Traceback (most recent call last):
ValueError: setting an array element with a sequence.
::
>>> m_a = [
... [1, 2],
... [3, 4]
... ]
>>> m_b = [
... [1, 2],
... [3, 4, 5]
... ]
>>> print(lazy_matrix_mul(m_a, m_b))
Traceback (most recent call last):
ValueError: setting an array element with a sequence.
::
>>> m_a = [
... [1, 2],
... [3, 4, 5]
... ]
>>> m_b = m_a
>>> print(lazy_matrix_mul(m_a, m_b))
Traceback (most recent call last):
ValueError: setting an array element with a sequence.