NumPy Assignment– 6
Linear Algebra & Advanced Operations
Basic Questions
- Create two 1D arrays [1, 2, 3] and [4, 5, 6]. Perform dot product using np.dot().
- Create two 2D arrays of shape (2, 2) and perform matrix multiplication using np.dot().
- Perform matrix multiplication on the same (2, 2) arrays using np.matmul() and compare results with np.dot().
- Generate a (3, 3) identity matrix. Multiply it with a (3, 3) random integer matrix and show that the result is unchanged.
- Create a (2, 2) matrix [[4,7],[2,6]]. Compute its determinant using np.linalg.det().
- Check if the determinant of a (3, 3) singular matrix is 0.
- Create a (2, 2) matrix [[1,2],[3,4]]. Find its inverse using np.linalg.inv().
- Verify that multiplying a matrix with its inverse returns the identity matrix.
- Create a (3, 3) matrix and compute its eigenvalues and eigenvectors using np.linalg.eig().
- For the same (3, 3) matrix, verify the eigen decomposition property A*v = λ*v for one eigenvalue/eigenvector pair.
- Solve the system of equations:
2x + y = 5
x – y = 1
using np.linalg.solve().
- Solve the system:
3x + y + z = 1
2x + 4y + z = 2
x + y + 5z = 3
using np.linalg.solve().
- Create a vector [3,4]. Compute its L2 norm using np.linalg.norm().
- Compute the L1 norm of the vector [1,-2,3,-4].
- Create a (3,3) diagonal matrix and compute its Frobenius norm.
- Show that the norm of [0,0,0] is 0.
- Create a (2, 2) matrix and compute its determinant, then square root of determinant using np.sqrt().
- Perform dot product of two random vectors of size 5.
- Multiply two (3,3) matrices using the @ operator (matrix multiplication).
- Find eigenvalues of a diagonal matrix and show they are the diagonal elements.
Intermediate Questions
- Generate two (3,3) random matrices. Perform matrix multiplication with dot() and verify dimensions.
- Compare np.dot(), np.matmul(), and @ operator on the same matrices to confirm equivalence.
- Create a (4,4) random integer matrix and compute its determinant. Interpret the sign of the determinant.
- Show that determinant of a triangular matrix is equal to the product of its diagonal elements.
- Generate a (3,3) invertible matrix and compute its inverse. Multiply the result with the original to verify identity.
- Try computing the inverse of a singular matrix and handle the error with try/except.
- Generate a (4,4) random matrix and compute its eigenvalues and eigenvectors.
- Verify eigen decomposition for all eigenvalues by computing A*v and comparing with λ*v.
- Solve a linear system Ax = b where A is a (3,3) matrix and b is a vector of length 3.
- Solve a system of equations with a randomly generated (5,5) matrix and (5,) vector.
- Compute vector norms for [1,2,3] using L1, L2, and infinity norms.
- Compute the Frobenius norm of a (3,3) matrix and explain its meaning.
- Generate a (3,3) orthogonal matrix and verify A.T @ A = I.
- Check condition number of a (3,3) random matrix using np.linalg.cond().
- Verify that inverse of transpose equals transpose of inverse for a (2,2) invertible matrix.
- Solve for current in a simple electric circuit modeled as V = IR using np.linalg.solve() with random values.
- Compute determinant of covariance matrix of a (5,5) random dataset.
- Use eigenvalues to compute the trace of a (3,3) matrix and compare with np.trace().
- Find eigenvalues of a symmetric matrix and confirm they are real numbers.
- Compute and compare norms of the same vector under L1, L2, and L∞ norms.
Advanced Questions
- Generate a (10,10) random matrix. Compute its determinant and interpret whether the matrix is invertible.
- Create a (6,6) random invertible matrix. Compute its inverse and multiply back to verify identity.
- Compute eigen decomposition of a (5,5) random symmetric matrix and verify A = VΛV⁻¹.
- Write a program to solve 100 random linear systems Ax=b of size (10,10) and report how many were solvable.
- Compute L2 norms of 1000 random vectors of size 100 and store results in an array.
- Build a Gram matrix by computing dot products of 5 random vectors of size 5.
- Simulate PCA step: generate a (5,5) covariance matrix, compute eigenvalues and eigenvectors, and project onto top 2 components.
- Create a (4,4) Hilbert matrix, compute its determinant, inverse, and condition number; explain numerical instability.
- Generate a (3,3) random matrix, compute SVD using np.linalg.svd(), and compare with eigen decomposition.
- Benchmark performance: compare solving Ax=b using np.linalg.inv(A) @ b vs np.linalg.solve(A,b) for large random matrices and discuss efficiency.