Multiply Python arrays

0

I would like to understand the flow of these loops for at each round, since according to what I understood of matrix multiplication and according to the loop: in the first round

  

i, j and k

would have a value of zero, therefore in

  

result [i] [j] that is: result [0] [0]

would be stored the result of the multiplication of X [i] [k] * Y [k] [j], that is, 12 * 5 = 60. and when printing gives 144 according to the output I have put down. and so on with everyone and I do not understand why.

a greeting!

# Program to multiply two matrices using nested loops

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

#output:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
    
asked by Melissa Alvarez 23.11.2018 в 19:01
source

2 answers

0

The expression inside the innermost loop is:

result[i][j] += X[i][k] * Y[k][j]

If you realize, there is no = , but a += , that is, the multiplication result is not saved in result[0][0] , but is added to what that was there.

You'll say, okay, but there was zero so in the end it's the same and you should add 0 + 60 resulting in 60.

That's right, but that's only in the first iteration of the loop where% co_of% is 0. If you look at that expression, it's inside another loop where k varies. Since every time that k varies the result is added to what was in k , the final result in that element, after having iterated every time on the value of result[0][0] , would be equivalent to:

result[0][0] = X[0][0]*Y[0][0] + X[0][1]*Y[1][0] + X[0][2]*Y[2][0]

Therefore the result would be:

result[0][0] = 12*5 + 7*6 + 3*4

What is 114.

That's the way matrices multiply. While in the matrix X you are going through the elements of a row, in the matrix Y you go through those of a column. They multiply each other and add up all of them, and the result is assigned to the element of the resulting matrix that would be at the intersection between that row and that column. The other two outer loops (in k and i ) are to be varying the row and column on which it operates.

In this image:

The lower matrix represents the result. To calculate the value of the violet colored element, multiply each blue element of the first matrix by each red element of the second, and add all those products. With that, only the value of an element of the result will have been calculated. By varying j and i you are going through all the positions in the result matrix, and for each one, you multiply the row j of X by the column i of Y.

    
answered by 23.11.2018 / 19:35
source
2

Look at it this way, when multiplying matrices, fila x columna is realized.

The first iteration, as you said, your control variables, have value of cero , therefore it would be as follows:

The first row of x = [12,7,3]

The first column of y = [5,6,4]

[12,7,3] × [5,6,4] = 12×5+7×6+3×4 = 114

And so on with the other rows and columns.

I leave you a screenshot of the procedure

    
answered by 23.11.2018 в 19:42