Bird
Raised Fist0
SciPydata~20 mins

Why linear algebra is the foundation of scientific computing in SciPy - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linear Algebra Mastery in Scientific Computing
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Matrix multiplication output
What is the output of the following code that multiplies two matrices using SciPy?
SciPy
import numpy as np
from scipy.linalg import blas

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

result = blas.dgemm(alpha=1.0, a=A, b=B)
print(result)
A
[[5. 12.]
 [21. 32.]]
B
[[12. 14.]
 [30. 34.]]
C
[[17. 20.]
 [39. 46.]]
D
[[19. 22.]
 [43. 50.]]
Attempts:
2 left
💡 Hint
Recall how matrix multiplication sums products of rows and columns.
❓ data_output
intermediate
1:30remaining
Shape of matrix after transpose and multiplication
Given a matrix A of shape (3, 4), what is the shape of the matrix resulting from A.T @ A?
SciPy
import numpy as np
A = np.random.rand(3, 4)
result = A.T @ A
print(result.shape)
A(4, 4)
B(3, 3)
C(3, 4)
D(4, 3)
Attempts:
2 left
💡 Hint
Transpose flips rows and columns. Matrix multiplication shape depends on inner dimensions.
❓ visualization
advanced
3:00remaining
Visualizing eigenvectors of a matrix
Which option correctly plots the eigenvectors of matrix M as arrows starting from the origin?
SciPy
import numpy as np
import matplotlib.pyplot as plt

M = np.array([[2, 1], [1, 2]])
eigenvalues, eigenvectors = np.linalg.eig(M)

plt.figure()
plt.axhline(0, color='grey')
plt.axvline(0, color='grey')
for vec in eigenvectors.T:
    plt.arrow(0, 0, vec[0], vec[1], head_width=0.05, head_length=0.1, fc='blue', ec='blue')
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.title('Eigenvectors of M')
plt.show()
APlots eigenvalues as points on x-axis
BPlots two arrows from origin pointing along eigenvectors of M
CPlots matrix M as heatmap
DPlots random vectors unrelated to M
Attempts:
2 left
💡 Hint
Eigenvectors show directions that stretch or shrink under M.
🧠 Conceptual
advanced
1:30remaining
Why is linear algebra essential in scientific computing?
Which statement best explains why linear algebra is the foundation of scientific computing?
AIt provides tools to represent and solve systems of linear equations common in simulations and data analysis.
BIt focuses only on scalar arithmetic which is rarely used in computing.
CIt is mainly about calculus and derivatives, unrelated to matrices.
DIt deals with string manipulation and text processing.
Attempts:
2 left
💡 Hint
Think about how many problems can be expressed as linear systems.
🔧 Debug
expert
2:00remaining
Identify the error in matrix inversion code
What error does the following code raise when trying to invert matrix A?
SciPy
import numpy as np
A = np.array([[1, 2], [2, 4]])
inv_A = np.linalg.inv(A)
print(inv_A)
AValueError: shapes not aligned
BTypeError: unsupported operand type(s)
CLinAlgError: Singular matrix
DNo error, prints inverse matrix
Attempts:
2 left
💡 Hint
Check if matrix A is invertible by its determinant.

Practice

(1/5)
1. Why is linear algebra considered the foundation of scientific computing?
easy
A. Because it uses matrices and vectors to solve many real-world problems
B. Because it only deals with simple addition and subtraction
C. Because it focuses on writing computer programs
D. Because it is unrelated to scientific calculations

Solution

  1. Step 1: Understand the role of linear algebra

    Linear algebra provides tools like matrices and vectors to represent and solve problems in science and engineering.
  2. Step 2: Connect to scientific computing

    Scientific computing relies on these tools to perform calculations efficiently and accurately.
  3. Final Answer:

    Because it uses matrices and vectors to solve many real-world problems -> Option A
  4. Quick Check:

    Linear algebra = foundation of scientific computing [OK]
Hint: Remember: matrices and vectors solve real problems [OK]
Common Mistakes:
  • Thinking linear algebra is only about simple math
  • Confusing programming with mathematical foundations
  • Ignoring the role of matrices and vectors
2. Which of the following is the correct way to create a 2x2 matrix using SciPy's linear algebra module?
easy
A. import numpy as np matrix = np.array([[1, 2], [3, 4]])
B. import scipy.linalg as la matrix = la.array([[1, 2], [3, 4]])
C. import scipy.linalg as la matrix = la.mat([[1, 2], [3, 4]])
D. import scipy.linalg as la matrix = la.matrix([[1, 2], [3, 4]])

Solution

  1. Step 1: Identify the correct module for array creation

    SciPy's linear algebra module (scipy.linalg) does not provide an array function; arrays are created using NumPy.
  2. Step 2: Verify the syntax

    Using np.array from NumPy correctly creates a 2x2 matrix.
  3. Final Answer:

    import numpy as np\nmatrix = np.array([[1, 2], [3, 4]]) -> Option A
  4. Quick Check:

    Use NumPy arrays for matrices [OK]
Hint: Use NumPy arrays, not scipy.linalg, to create matrices [OK]
Common Mistakes:
  • Trying to create arrays directly with scipy.linalg
  • Using non-existent functions like la.array or la.matrix
  • Confusing matrix creation with matrix operations
3. What is the output of this code snippet using SciPy and NumPy?
import numpy as np
from scipy.linalg import inv
A = np.array([[1, 2], [3, 4]])
A_inv = inv(A)
print(np.round(A_inv, 2))
medium
A. [[4.0 -2.0] [-3.0 1.0]]
B. [[-2.0 1.0] [1.5 -0.5]]
C. [[0.5 0.0] [0.0 0.5]]
D. Error: inv() not found

Solution

  1. Step 1: Calculate the inverse of matrix A

    Matrix A is [[1, 2], [3, 4]]. Its inverse is calculated using the formula for 2x2 matrices or scipy.linalg.inv.
  2. Step 2: Round the inverse matrix

    The inverse matrix is [[-2, 1], [1.5, -0.5]] rounded to two decimals.
  3. Final Answer:

    [[-2.0 1.0] [1.5 -0.5]] -> Option B
  4. Quick Check:

    Inverse of A = [[-2,1],[1.5,-0.5]] [OK]
Hint: Recall inverse formula for 2x2 matrices [OK]
Common Mistakes:
  • Confusing inverse with transpose
  • Not rounding output
  • Using wrong matrix elements
4. Identify the error in this code that tries to multiply two matrices using SciPy:
import numpy as np
from scipy.linalg import dot
A = np.array([[1, 0], [0, 1]])
B = np.array([[4, 1], [2, 2]])
C = dot(A, B)
print(C)
medium
A. The print statement is missing parentheses
B. Matrix A is not square
C. The function dot is not in scipy.linalg, it is in numpy
D. Matrix B has wrong dimensions

Solution

  1. Step 1: Check the dot function location

    The dot function for matrix multiplication is in numpy, not scipy.linalg.
  2. Step 2: Understand the error cause

    Importing dot from scipy.linalg causes an ImportError or AttributeError.
  3. Final Answer:

    The function dot is not in scipy.linalg, it is in numpy -> Option C
  4. Quick Check:

    Use numpy.dot for matrix multiplication [OK]
Hint: Use numpy.dot, not scipy.linalg.dot [OK]
Common Mistakes:
  • Assuming dot is in scipy.linalg
  • Ignoring correct import sources
  • Confusing matrix dimensions as error
5. You have a system of linear equations represented by matrix A and vector b. Which SciPy function is best to find the solution vector x for Ax = b efficiently and accurately?
hard
A. scipy.linalg.inv(A) @ b (compute inverse then multiply)
B. numpy.dot(A, b) (dot product of A and b)
C. scipy.linalg.det(A) (compute determinant of A)
D. scipy.linalg.solve(A, b) (direct solver for linear systems)

Solution

  1. Step 1: Understand the problem

    We want to solve the linear system Ax = b for x.
  2. Step 2: Compare methods

    Using scipy.linalg.solve directly solves the system efficiently without computing the inverse, which is slower and less accurate.
  3. Final Answer:

    scipy.linalg.solve(A, b) (direct solver for linear systems) -> Option D
  4. Quick Check:

    Use solve() for Ax=b systems [OK]
Hint: Use solve() instead of inverse for solving Ax=b [OK]
Common Mistakes:
  • Using inverse multiplication which is inefficient
  • Confusing dot product with solving equations
  • Using determinant which does not solve the system
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard