Bird
0
0

Given the following code, what will be the output of print(M.matvec(b))?

medium📝 Predict Output Q13 of 15
SciPy - Sparse Linear Algebra

Given the following code, what will be the output of print(M.matvec(b))?

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

A = np.array([[2, 0], [0, 5]])
b = np.array([4, 10])
M = LinearOperator(shape=A.shape, matvec=lambda x: x / np.diag(A))
print(M.matvec(b))
A[0.5, 2.0]
B[8.0, 50.0]
C[2.0, 2.0]
D[4.0, 10.0]
Step-by-Step Solution
Solution:
  1. Step 1: Calculate diagonal of A

    Diagonal elements are [2, 5].
  2. Step 2: Apply matvec function

    Divide each element of b by corresponding diagonal: [4/2, 10/5] = [2.0, 2.0].
  3. Final Answer:

    [2.0, 2.0] -> Option C
  4. Quick Check:

    Vector divided by diagonal = [2.0, 2.0] [OK]
Quick Trick: Divide vector elements by diagonal elements to get output [OK]
Common Mistakes:
  • Multiplying instead of dividing
  • Confusing vector and matrix multiplication
  • Using wrong diagonal values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes