Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@GreenEric GreenEric commented Nov 17, 2019

Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.