0
0
SciPydata~20 mins

Cholesky decomposition in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cholesky Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Cholesky decomposition with scipy
What is the output of the following code that performs Cholesky decomposition on a positive definite matrix?
SciPy
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
L = cholesky(A, lower=True)
print(L)
A
[[4. 2.]
 [2. 3.]]
B
[[2. 1.]
 [0. 1.41421356]]
C
[[2. 0.]
 [2. 1.41421356]]
D
[[2. 0.]
 [1. 1.41421356]]
Attempts:
2 left
💡 Hint
Remember that the Cholesky decomposition returns a lower triangular matrix L such that A = L * L.T
data_output
intermediate
1:30remaining
Shape of Cholesky factor matrix
Given a 3x3 positive definite matrix, what is the shape of the matrix returned by scipy.linalg.cholesky with lower=True?
SciPy
import numpy as np
from scipy.linalg import cholesky

A = np.array([[6, 3, 4], [3, 6, 5], [4, 5, 10]])
L = cholesky(A, lower=True)
print(L.shape)
A(3, 1)
B(3, 3)
C(1, 3)
D(6, 6)
Attempts:
2 left
💡 Hint
The Cholesky decomposition returns a matrix of the same size as the input matrix.
🔧 Debug
advanced
2:00remaining
Error raised by Cholesky on non-positive definite matrix
What error does the following code raise when trying to compute the Cholesky decomposition of a non-positive definite matrix?
SciPy
import numpy as np
from scipy.linalg import cholesky

A = np.array([[1, 2], [2, 1]])
L = cholesky(A, lower=True)
Anumpy.linalg.LinAlgError: Matrix is not positive definite
BValueError: shapes (2,2) and (2,2) not aligned
CTypeError: unsupported operand type(s) for /: 'int' and 'str'
DNo error, returns a valid matrix
Attempts:
2 left
💡 Hint
Cholesky decomposition requires the matrix to be positive definite.
🚀 Application
advanced
3:00remaining
Using Cholesky decomposition to solve linear systems
Which code snippet correctly uses Cholesky decomposition to solve the system Ax = b for x?
A
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
b = np.array([6, 8])
L = cholesky(A, lower=True)
x = np.dot(L, b)
print(x)
B
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
b = np.array([6, 8])
L = cholesky(A)
x = np.linalg.solve(L, b)
print(x)
C
import numpy as np
from scipy.linalg import cholesky, solve_triangular

A = np.array([[4, 2], [2, 3]])
b = np.array([6, 8])
L = cholesky(A, lower=True)
y = solve_triangular(L, b, lower=True)
x = solve_triangular(L.T, y, lower=False)
print(x)
D
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
b = np.array([6, 8])
L = cholesky(A, lower=True)
x = np.linalg.inv(L).dot(b)
print(x)
Attempts:
2 left
💡 Hint
Use solve_triangular twice: first for Ly = b, then for L.T x = y.
🧠 Conceptual
expert
1:30remaining
Cholesky decomposition properties and usage
Which statement about Cholesky decomposition is TRUE?
ACholesky decomposition requires the matrix to be symmetric and positive definite.
BThe Cholesky factor L is always an upper triangular matrix.
CCholesky decomposition returns two matrices L and U such that A = L * U.
DCholesky decomposition can be applied to any square matrix, regardless of symmetry or definiteness.
Attempts:
2 left
💡 Hint
Think about the conditions on the matrix for Cholesky to work.