Matrix multiplication with @ operator in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to multiply two matrices grows as the size of the matrices increases.
How does the number of calculations change when the matrices get bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 10 # example size
A = np.random.rand(n, n)
B = np.random.rand(n, n)
C = A @ B
This code multiplies two square matrices A and B of size n by n using the @ operator.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying and summing elements for each cell in the result matrix.
- How many times: For each of the n rows and n columns, it performs n multiplications and additions.
As the matrix size n grows, the number of calculations grows quickly because each new row and column adds many more multiplications.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1,000 |
| 100 | About 1,000,000 |
| 1000 | About 1,000,000,000 |
Pattern observation: The operations grow roughly by the cube of n, so doubling n makes the work about eight times bigger.
Time Complexity: O(n^3)
This means the time to multiply two n by n matrices grows roughly with the cube of n, so bigger matrices take much more time.
[X] Wrong: "Matrix multiplication with @ operator runs in linear time like simple addition."
[OK] Correct: Multiplying matrices involves many nested calculations, not just one pass through the data, so it takes much more time as size grows.
Understanding how matrix multiplication scales helps you explain performance in data science tasks like machine learning and graphics, showing you know how big data affects calculations.
"What if we multiply a matrix of size n by another matrix of size n by m? How would the time complexity change?"
Practice
@ operator do in numpy when applied between two arrays?Solution
Step 1: Understand the
The@operator purpose@operator in numpy is designed for matrix multiplication, which requires the inner dimensions of the two arrays to match.Step 2: Differentiate from other operations
Element-wise addition or multiplication use+or*respectively, not@. Transpose uses.T.Final Answer:
Performs matrix multiplication if shapes are compatible -> Option AQuick Check:
@means matrix multiply [OK]
@ means matrix multiply, not element-wise [OK]- Confusing
@with element-wise multiplication - Thinking
@adds arrays - Assuming
@transposes arrays
A and B using the @ operator?Solution
Step 1: Identify the
The@operator usage@operator is used asC = A @ Bto perform matrix multiplication in numpy.Step 2: Differentiate from other operations
A * Bis element-wise multiplication,A.dot(B)is a method but not using@, andA + Bis addition.Final Answer:
C = A @ B-> Option DQuick Check:
Use@between arrays for matrix multiply [OK]
@ directly between arrays for matrix multiply [OK]- Using
*instead of@for matrix multiply - Confusing method
dot()with operator@ - Using addition operator
+mistakenly
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = A @ B print(C)
Solution
Step 1: Calculate matrix multiplication manually
Multiply rows of A by columns of B:
First row: (1*5 + 2*7) = 19, (1*6 + 2*8) = 22
Second row: (3*5 + 4*7) = 43, (3*6 + 4*8) = 50Step 2: Confirm output matches calculation
The resulting matrix is [[19, 22], [43, 50]], which matches [[19 22] [43 50]].Final Answer:
[[19 22] [43 50]] -> Option CQuick Check:
Matrix multiply result = [[19 22] [43 50]] [OK]
- Adding elements instead of multiplying and summing
- Mixing element-wise multiplication with matrix multiplication
- Confusing row and column order
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([[7, 8], [9, 10]]) C = A @ B
Solution
Step 1: Check shapes of arrays
Array A shape is (2,3), array B shape is (2,2). For matrix multiplication, A's columns (3) must equal B's rows (2).Step 2: Identify mismatch and error
Since 3 != 2, numpy raises a ValueError about shape misalignment.Final Answer:
ValueError: shapes (2,3) and (2,2) not aligned for matrix multiplication -> Option AQuick Check:
Matrix multiply needs matching inner dimensions [OK]
@ [OK]- Ignoring shape mismatch and expecting output
- Confusing element-wise multiplication with matrix multiplication
- Assuming
@works like addition
A = np.array([[1, 0], [0, 1]]) B = np.array([[2, 3], [4, 5]])
What is the result of
C = A @ B @ A?Solution
Step 1: Multiply A and B
Matrix A is the identity matrix. Multiplying identity with B returns B:
A @ B = B = [[2, 3], [4, 5]]Step 2: Multiply result by A again
Multiplying B by identity matrix A again returns B:
B @ A = B = [[2, 3], [4, 5]]Final Answer:
[[2 3] [4 5]] -> Option BQuick Check:
Identity matrix leaves other matrix unchanged [OK]
A leaves matrix unchanged when multiplied [OK]- Multiplying incorrectly and swapping rows/columns
- Assuming multiplication changes matrix when identity is involved
- Confusing element-wise and matrix multiplication
