Bird
0
0

Identify the error in the following code that attempts to create a Jacobi preconditioner:

medium📝 Debug Q14 of 15
SciPy - Sparse Linear Algebra

Identify the error in the following code that attempts to create a Jacobi preconditioner:

import numpy as np
from scipy.sparse.linalg import LinearOperator

A = np.array([[3, 1], [1, 4]])
M = LinearOperator(shape=A.shape, matvec=lambda x: np.diag(A) * x)
print(M.matvec(np.array([1, 2])))
AUsing np.diag(A) incorrectly as a matrix
BMultiplying by diagonal instead of dividing
CShape of LinearOperator is wrong
DInput vector has wrong size
Step-by-Step Solution
Solution:
  1. Step 1: Understand Jacobi preconditioner operation

    Jacobi preconditioner divides vector elements by diagonal elements of A.
  2. Step 2: Check given matvec function

    Code multiplies vector by diagonal instead of dividing, which is incorrect.
  3. Final Answer:

    Multiplying by diagonal instead of dividing -> Option B
  4. Quick Check:

    Jacobi requires division, not multiplication [OK]
Quick Trick: Jacobi preconditioner divides vector by diagonal, not multiply [OK]
Common Mistakes:
  • Confusing multiplication with division
  • Ignoring element-wise operations
  • Not verifying mathematical definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes