Forked from suensummit/eigenvectors_from_eigenvaluies.R
Created
November 17, 2019 02:40
-
-
Save GreenEric/134fde3fb8e976526513d58a9a49a0bd 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment