Skip to content

Instantly share code, notes, and snippets.

@suensummit
Created November 16, 2019 15:46
Show Gist options
  • Save suensummit/be970ff098b3ddc6541747b9900a69ad to your computer and use it in GitHub Desktop.
Save suensummit/be970ff098b3ddc6541747b9900a69ad to your computer and use it in GitHub Desktop.
A demo R script of [the paper EIGENVECTORS FROM EIGENVALUES](https://arxiv.org/pdf/1908.03795.pdf), modified from [Yu-Chen Shu](https://www.facebook.com/yuchen.shu/posts/10157876738839228) rewritten in R.
# Generate random matrix with dim = N
N <- 3
A <- matrix(runif(N*N), N, N, byrow=TRUE)
A <- A+t(A)
# Setting up eigenvalues lambda, true eigenvector V and derived eigenvector U
ev <- eigen(A)
(lambda <- ev$values)
(V <- ev$vectors)
U <- matrix(0L, N, N)
# Calculate from lemma 2 in https://arxiv.org/pdf/1908.03795.pdf
for (i in 1:N) {
lambda_D <- lambda; lambda_D[i] <- NA; lambda_D <- lambda_D[!is.na(lambda_D)]
denominator <- prod(lambda[i]-lambda_D)
for (j in 1:N) {
M <- A; M[,j] = M[j,] <- NA; M <- matrix(M[!is.na(M)], N-1, N-1)
lambda_M <- eigen(M)$values
fraction <- prod(lambda[i]-lambda_M)
U[j,i] <- fraction/denominator
}
}
# Check the infinity norm of DIFF(U, V^2) shows that they are close enough
norm(U - V*V, type="I")
@GreenEric
Copy link

Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment