Bird
0
0

What will the following code print?

medium📝 Predict Output Q5 of 15
SciPy - Sparse Matrices (scipy.sparse)
What will the following code print?
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import spsolve

A = csc_matrix([[1, 2], [3, 4]])
b = np.array([5, 11])
x = spsolve(A, b)
print(np.round(x, 2))
A[1. 2.]
B[2. 1.]
C[5. 11.]
D[0.5 1.5]
Step-by-Step Solution
Solution:
  1. Step 1: Set up the linear system

    Matrix A and vector b represent the system A*x = b.
  2. Step 2: Solve manually or verify solution

    Solving: 1*x0 + 2*x1 = 5 and 3*x0 + 4*x1 = 11 gives x0=1, x1=2.
  3. Final Answer:

    [1. 2.] -> Option A
  4. Quick Check:

    Linear system solution = [1, 2] [OK]
Quick Trick: Solve Ax=b by hand for small matrices to verify [OK]
Common Mistakes:
MISTAKES
  • Mixing up x0 and x1 values
  • Not rounding output
  • Assuming sparse matrix changes solution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes