To most, understanding the underlying formula for every calculation executed by the code is not that "important" and some doesn't even care at all -- as long as they see something is shown as output. This is why I wanted to take time to at least try my best to explain, why one needs to be curious enough to validate what a program does and tell.
This sample lines of code uses the python library numpy to calculate the output of c = a * b
The output of that equation is:
Now, while that answer is true and correct -- one might wonder, why? Well, I'm glad you asked.
Let's dissect the equation and see how numpy performs the calculation.
NOTE:
- In multiplying matrices, make sure that the number of columns is equal to the number of rows. Ref: https://en.wikipedia.org/wiki/Matrix_(mathematics)
- In multiplying matrices, order matters. (ie. Where c = a * b will not show the same result as c = b * a).
EXPLANATION:
- (i) Take the first column set of a and multiply it with the first row set of b. (ii) Then take the first column set of a and multiply it with the second row set of b. The output should look like
- [[-1(-1), 0(-1), 4(2)], [ -1(1), 0(3), 4(4)]]
- Where [-1(-1), 0(-1), 4(2)] is the output for i and [ -1(1), 0(3), 4(4)] is the output of ii
- When we calculate this, i will result to 1, 0, 8 = 9 and ii will result to -1, 0, 16 = 15
- Thus, making the answer [9, 15]
- (iii) The the second column set of a and multiply it with the first row set of b. (iv) Then take the second column set of a and multiply it with the second row set of b. The output should look like
- [[2(-1), 0(-1), 0(2)], [2(1), 0(3), 0(4)]]
- Where [2(-1), 0(-1), 0(2)] is the output for iii and [2(1), 0(3), 0(4)] is the output of iv
- When we calculate this, iii will result to -2, 0 , 0 = -2 and iv will result to 2, 0, 0 = 2
- Thus, making our answer [-2, 2]
- This is how numpy achieves the correct answer [[9, 15],[-2, 2]] -- in a much faster and accurate way.
Some references in dealing with numbers: