I'm trying to do a matrix multiplication in python I have found the following code that I am trying to understand. (I know how to multiply matrices by hand and I want to understand how the following code performs the same action and by that i mean the first element in BA (row 1 column 1) is calculated by doing (1*1 + 3*3 + 3*3 + 1*1) etc.
from numpy import array
A= array([[ 1, 4, 5 ],[ 3, 2, 9], [ 3,6, 2], [ 1,6, 8]])
B=A.T
BA= array([[ 0, 0, 0 ],[ 0,0, 0], [ 0,0, 0] ])
for i in range(len(B)):
for j in range(len(A[0])):
for k in range(len(A)):
BA[i][j] += B[i][k] * A[k][j]
I know that the length command for a list returns how many elements there are in that list. I am not sure how it works here since B is a matrix, I assume it returns how many rows there are.
range of len(B) would be (0,3) corresponding to row 1,2 and 3. for i in range would correspond to i=0, i=1, i= 2
next confusing thing is for j in range len(A[0]) The first element of A is the first row, the length here would thus correspond how many elements there are in the first element of A.
Basically I have a basic understanding of what range and len etc put out for this example but I would like to get a better understand of each value of i, j, k as a result of these as well as the last line which I really don't understand.
BA[i][j] += B[i][k] * A[k][j]
Please explain as basic as possible because I am new to programming and so at this point nothing is trivial to me. Thank you for your time to help others :)
Here is the actual result from your code:
B * A = AB
1 3 3 1 1 4 5 20 34 46
4 2 6 6 3 2 9 34 92 98
5 9 2 8 3 6 2 46 98 174
1 6 8
Assuming i = 0
and j = 0
lets calculate the BA[0][0]
, which is the first element from matrix BA.
BA[0][0] = B[0][k] * A[k][0]
B[0][k]
means the line 0 from matrix B. As k
is iterating over all lines of A, which is the same size as the number of columns in B.
A[k][0]
means the column 0 from matrix A.
The loop for k in range(len(A)):
will reproduce:
B[0][0]*A[0][0] + B[0][1]*A[1][0] + B[0][2]*A[2][0] + B[0][3]*A[3][0]
Resulting in:
1×1 + 3×3 + 3×3 + 1×1 = 20
Which is the value for BA[0][0]
resulted from your code.
The following nested loops will iterate over all columns of A as j
for every line of B as i
in order to perform the multiplication for all (line) x (column) pairs:
for i in range(len(B)):
for j in range(len(A[0])):
Consider here the array as just a more convenient representation of the list you gave to the function.
A is built on top of the list [[ 1, 4, 5 ],[ 3, 2, 9], [ 3,6, 2], [ 1,6, 8]]
, a list of length 4.
range(start, stop)
is a function which returns a generator which produces a sequence of integers, from the start to the stop point, stop not being included. If not provided, start defaults to 0.
B has a length of 4 rows so range(len(B))
will be like range(0, 3)
, which will produce 0,1 and 2 integers, when "asked" by the for loop. i
will subsequently be 0,1 and 2.
A[0]
returns the first row of A, which has a length of 3, so the same way, j will subsequently be 0, 1 and 2 (in this case) ; and k will subsequently be 0, 1, 2 and 3 as A has a length of 4.
BA[i] returns the row of index i
. Which can also be indexed by j
So BA[i][j] is the element of row i and column j, which we increments by the product of the element of row i and index k of matrix B; and the element of row k and index j of matrix A.
In you code sample a matrix is represented aa list of sublists, where each sublist is a row.
So the most outer loop goes over the rows of B:
for i in range(len(B)):
A[0]
is the first row of A, and the number of elements in it is the number of A's columns.
So the second loop goes over the columns of A:
for j in range(len(A[0])):
The most inner loop simply sums the products of the elements in the j-th row of B and the i-th row of A.
BA[i][j] += B[i][k] * A[k][j]
This add to BA[i][j]
the product. +=
adds its right argument to its left one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.