Here, we can input the matrix \(A\) and use it to create to special matrices X and Y where \(X = AA^T\) and \(Y=A^TA\).
A <- matrix(c(1,-1,2,0,3,4), nrow=2, ncol=3) #Matrix A
A #manual check via console
[,1] [,2] [,3]
[1,] 1 2 3
[2,] -1 0 4
X = A %*% t(A) # A*A^T
Y = t(A) %*% A #A A^T*A
Then, we can computer the eigenvectors of x and y as well as the svd of
eig_x = eigen(X)
eig_x$vectors
[,1] [,2]
[1,] 0.6576043 -0.7533635
[2,] 0.7533635 0.6576043
svd(A)$u #left handed vector
[,1] [,2]
[1,] -0.6576043 -0.7533635
[2,] -0.7533635 0.6576043
eig_y = eigen(Y)
eig_y$vectors[0:1]
[1] -0.01856629
svd(A)$v #right handed one
[,1] [,2]
[1,] 0.01856629 -0.6727903
[2,] -0.25499937 -0.7184510
[3,] -0.96676296 0.1765824
We can also see that the eigenvalues of the svd (X), the significant ones for Y, and the singular square values of A are all the same.
svd(X)$d # eigenvalue of svd(X)
[1] 26.601802 4.398198
svd(Y)$d[1:2] # eigenvalue of svd(Y)
[1] 26.601802 4.398198
squares <- c(svd(A)$d)# single values of A
squares <- (squares^2) #squaring the squares vector
squares
[1] 26.601802 4.398198
eye <- diag(rep(1,3))
myinverse <- function(A){
if (dim(A)[1]==dim(A)[2] & det(A)!=0 ){
C <- A
for(i in 1:dim(A)[1]){
for(j in 1:dim(A)[1]){
C[i,j] <- (det(A[-i,-j])*(-1)^(i+j))
}
}
B <- t(C)/det(A)
return(B)
}
else{
return(-1)
}
}
if (all(myinverse(eye)== eye)){ #identity matrix should equal it's own inverse. Otherwise, my function is broken.
print('function passed!')
}
[1] "function passed!"