Solving linear systems (solve) in SciPy - Time & Space Complexity
When we solve linear systems using scipy, we want to know how the time it takes changes as the system size grows.
We ask: How does solving a bigger system affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.linalg import solve
n = 10 # example size
A = np.random.rand(n, n) # square matrix of size n
b = np.random.rand(n) # vector of size n
x = solve(A, b) # solve Ax = b
This code solves a system of linear equations with a square matrix A and vector b.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Matrix factorization and substitution steps inside solve.
- How many times: Operations involve nested loops over matrix rows and columns, roughly n times for each dimension.
As the matrix size n grows, the number of calculations grows much faster.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1,000 operations |
| 100 | About 1,000,000 operations |
| 1000 | About 1,000,000,000 operations |
Pattern observation: The work grows roughly by the cube of n, so doubling n makes the work about eight times bigger.
Time Complexity: O(n^3)
This means if you double the size of the system, the time to solve it grows about eight times.
[X] Wrong: "Solving linear systems with solve grows linearly with n because it just loops once over the matrix."
[OK] Correct: The solve function does more than one loop; it performs matrix factorization which involves nested loops over rows and columns, making the work grow much faster than just n.
Understanding how solving linear systems scales helps you explain performance in data science tasks like regression or simulations, showing you know how algorithms behave with bigger data.
"What if the matrix A is sparse instead of dense? How would the time complexity change?"
Practice
scipy.linalg.solve function do in solving linear systems?Solution
Step 1: Understand the purpose of
This function is designed to solve linear systems of the form Ax = b, where A is a matrix and b is a vector.scipy.linalg.solveStep 2: Identify the output of the function
The function returns the vector x that exactly satisfies the equation Ax = b.Final Answer:
It finds the exact solution vector for a system of linear equations. -> Option AQuick Check:
Function purpose = exact solution vector [OK]
- Thinking solve() approximates solutions
- Confusing solve() with matrix multiplication
- Assuming solve() only checks for solution existence
solve function from scipy.linalg?Solution
Step 1: Recall Python import syntax for functions
To import a specific function from a module, usefrom module import function.Step 2: Match the correct module and function
The functionsolveis inside thescipy.linalgmodule, so the correct import isfrom scipy.linalg import solve.Final Answer:
from scipy.linalg import solve -> Option AQuick Check:
Correct import syntax = from scipy.linalg import solve [OK]
- Using 'import solve from ...' which is invalid syntax
- Trying to import submodules as functions
- Using dot notation incorrectly in import statements
import numpy as np from scipy.linalg import solve A = np.array([[2, 1], [1, 3]]) b = np.array([10, 15]) x = solve(A, b) print(x)
Solution
Step 1: Set up the system of equations
The matrix A and vector b represent the system:
2x + y = 10
x + 3y = 15Step 2: Solve the system manually
Solving manually:
Multiply first equation by 3: 6x + 3y = 30
Subtract second equation: 5x = 15 -> x = 3
y = 10 - 2*3 = 4
Thus, x = [3. 4.], which matches the output of solve(A, b).Final Answer:
[3. 4.] -> Option BQuick Check:
solve(A,b) output = [3. 4.] [OK]
- Mixing up order of solution vector
- Trying to solve manually without decimals
- Assuming integer solutions only
import numpy as np from scipy.linalg import solve A = np.array([[1, 2], [2, 4]]) b = np.array([5, 10]) x = solve(A, b) print(x)
Solution
Step 1: Check matrix A properties
Matrix A = [[1, 2], [2, 4]] has rows that are multiples (second row = 2 * first row), so it is singular (determinant = 0).Step 2: Understand solve() behavior with singular matrices
Since A is singular, it cannot be inverted, sosolve()will raise a LinAlgError indicating the matrix is singular.Final Answer:
Matrix A is singular (not invertible), so solve() will raise an error. -> Option DQuick Check:
Singular matrix causes error in solve() [OK]
- Ignoring singular matrix errors
- Assuming solve() works for all matrices
- Confusing vector shape errors with matrix singularity
A = [[1, 2, 3], [0, 1, 4], [5, 6, 0]] b = [11, 7, 28]
Using
scipy.linalg.solve, what is the solution vector x?Solution
Step 1: Write the system of equations
The system is:
1x + 2y + 3z = 11
0x + 1y + 4z = 7
5x + 6y + 0z = 28Step 2: Solve the system using substitution
From second equation: y = 7 - 4z
Substitute into first: x + 2(7 - 4z) + 3z = 11 -> x + 14 - 8z + 3z = 11 -> x - 5z = -3 -> x = -3 + 5z
Substitute into third: 5(-3 + 5z) + 6(7 - 4z) = 28 -> -15 + 25z + 42 - 24z = 28 -> z + 27 = 28 -> z = 1
y = 7 - 4*1 = 3
x = -3 + 5*1 = 2
Thus, x = [2. 3. 1.]Final Answer:
[2. 3. 1.] -> Option CQuick Check:
solve(A,b) = [2. 3. 1.] [OK]
- Manual calculation errors with substitution
- Mixing variable order in solution vector
- Ignoring code output for complex systems
Ready to prove it?
Pass the test and this pattern moves unknown
