Reputation: 3347
I'm finding difficulting what exactly the doctest in the following code is looking for in the function.
Can someone make it more clear for me, please?
- Write functions row_times_column and matrix_mult:
def row_times_column(m1, row, m2, column):
"""
>>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 0)
19
>>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 1)
22
>>> row_times_column([[1, 2], [3, 4]], 1, [[5, 6], [7, 8]], 0)
43
>>> row_times_column([[1, 2], [3, 4]], 1, [[5, 6], [7, 8]], 1)
50
"""
def matrix_mult(m1, m2):
"""
>>> matrix_mult([[1, 2], [3, 4]], [[5, 6], [7, 8]])
[[19, 22], [43, 50]]
>>> matrix_mult([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 1], [2, 3]])
[[31, 19], [85, 55]]
>>> matrix_mult([[7, 8], [9, 1], [2, 3]], [[1, 2, 3], [4, 5, 6]])
[[39, 54, 69], [13, 23, 33], [14, 19, 24]]
"""
Add your new functions to matrices.py and be sure it passes the doctests above.
Upvotes: 1
Views: 656
Reputation: 14420
The first function, first example in docstring: take the zeroth row from the first matrix ([1, 2]
) and multiply it by the zeroth column of the second matrix ([5, 7]
)
1 x 5 + 2 x 7 = 19
The second function is just standard matrix multiplication. You can use the first function to answer this problem.
Upvotes: 3
Reputation: 20277
The first function, row_times_column
multiplies the nth row of the first matrix by the mth column of the second matrix. In the first doctest, for example, n = 0
and m = 0
, so we multiply the row matrix [1, 2]
by the column matrix [5, 7]
to get 1 * 5 + 2 * 7
which is equal to 19
as specified.
The second function is the generalization, and you have to multiply the first matrix by the second. You are probably supposed to use the first function. The linked article shows how to get matrix multiplication from a combination of row by column multiplications.
Upvotes: 1
Reputation: 871
Its asking you to implement some matrix multiplication methods.
In the first one, given a matrix, multiply the row of m1 x the column of m2. The first one given is:
1 2 times 5 6
3 4 7 8
but we only want row 0 times col 0, so it would be
[1 2] x 5
7
= 5+14 = 19. And so on for the others...
The second function wants a full matrix multiplication. See http://en.wikipedia.org/wiki/Matrix_multiplication
Upvotes: 3