library(rsvd)
set.seed(123)
#Create real random test matrix with dimension (m, n) and rank k
m = 10
n = 3
k = 5
A <- matrix(runif(m*k), m, k)
A <- A %*% t(A)
A <- A[,1:n]
#Randomized SVD, no low-rank approximation
s <- rsvd(A)
Atilde = s$u %*% diag(s$d) %*% t(s$v)
100 * norm( A - Atilde, 'F') / norm( Atilde,'F') #Percentage reconstruction error << 1e-8
#Randomized SVD, low-rank approximation k=3
s <- rsvd(A, k=3)
Atilde = s$u %*% diag(s$d) %*% t(s$v)
100 * norm( A - Atilde, 'F') / norm( Atilde,'F') #Percentage reconstruction error << 1e-8
#Randomized SVD, low-rank approximation k=2
s <- rsvd(A, k=2)
Atilde = s$u %*% diag(s$d) %*% t(s$v)
100 * norm( A - Atilde, 'F') / norm( Atilde,'F') #Percentage reconstruction error < 3.5%
Run the code above in your browser using DataLab