Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-250

Eckart-Young Rank-r Reconstruction

Problem

Reconstruct the best rank-r approximation of a matrix from its SVD, given as U (m x k with singular vectors as columns), singular values S, and V (k right singular vectors of length n).

Return the sum over t < r of S[t] * outer(U[:, t], V[t]).

Hint

Keeping the first r singular triples minimizes the Frobenius error among rank-r matrices.

Test cases

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

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