Join Our 5-Week ML/AI Engineer Interview Bootcamp đ led by ML Tech Leads at FAANGs
Compute the rank of a matrix, which is the number of linearly independent rows (or columns) in a matrix. This helps you understand how much âunique informationâ the matrix contains in a linear algebra sense.
The rank can be defined as the number of non-zero singular values from an SVD:
where (\sigma_i) are the singular values of (A) and (\varepsilon) is a small tolerance.
Implement the function
Rules:
A as a 2D list (not a NumPy array input), but you may convert it to NumPy internally.eps.np.linalg.matrix_rank (or any other built-in rank helper).Output:
| Argument | Type |
|---|---|
| A | list |
| eps | float |
| Return Name | Type |
|---|---|
| value | int |
Input A is 2D Python list
Use SVD; count singular values > eps
Do not call np.linalg.matrix_rank
Convert the input 2D list A into a numeric matrix representation, then compute its singular values; the rank is how many are non-zero (above a tolerance).
Use np.linalg.svd(A_np, compute_uv=False) to get the 1D array of singular values s; then count sum(s > eps) (strictly greater).
Be careful about shapes and edge cases: empty input, non-rectangular lists, and ensuring you donât call np.linalg.matrix_rank; return an int from int(np.sum(s > eps)).