############################################# ## R script for STAT 893 - LA Review II ## First problem set ############################################# options(digits=4) ## create matrix A A<-matrix(c(1,1,-5,1,1,2,3,1,4,0,-1,2,-1,3,-1),nrow=3,ncol=5) square<-A%*%t(A);square eigen(square) ## prints both eigenvalues and eigenvectors (as columns) P<-eigen(square)$vectors ## NOTE: R automatically chooses normalized eigenvectors new<-t(P)%*%square%*%P ## new = P'(AA')P ## cleaning up round-off errors (not necessary) for (i in 1:nrow(P)) for (j in 1:ncol(P)) if (abs(new[i,j])<1e-12) new[i,j]<-0 new ## displays P'(AA')P ## Now, the reverse direction square<-t(A)%*%A;square eigen(square) ## prints both eigenvalues and eigenvectors (as columns) P<-eigen(square)$vectors ## NOTE: R automatically chooses normalized eigenvectors new<-t(P)%*%square%*%P ## now, new = P'(A'A )P (different P, obviously) for (i in 1:nrow(P)) for (j in 1:ncol(P)) if (abs(new[i,j])<1e-12) new[i,j]<-0 new