Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-251

Pseudoinverse via SVD Formula

Problem

Build the Moore-Penrose pseudoinverse from an SVD, given U (m x k, singular vectors as columns), singular values S, and V (k x n, right singular vectors as rows so V[t] is a vector of length n).

Return A+ = sum over t of (1/S[t]) * outer(V[t], U[:, t]), skipping singular values at most 1e-12.

Hint

The pseudoinverse inverts nonzero singular values and formally zeros out the rest.

Test cases

case 1
in: [[[1,0],[0,1]],[3,1],[[1,0],[0,1]]]
expected: [[0.3333333333333333,0],[0,1]]
case 2
in: [[[1,0],[0,1]],[2,0],[[1,0],[0,1]]]
expected: [[0.5,0],[0,0]]
case 3
in: [[[1,0],[0,1]],[1,1],[[0,1],[1,0]]]
expected: [[0,1],[1,0]]
case 4
in: [[[1,0],[0,1]],[0,0],[[1,0],[0,1]]]
expected: [[0,0],[0,0]]
Starter codePython
def pinv_from_svd(U, S, V):
    # Your code here
    pass

4 test cases · run your code against them in the editor