Problem 1

Given matrix \(\mathbf{A} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ -1 & 0 & 4 \\ \end{array}\right]\) compute \(X=AA^T\) and \(Y=A^TA\)

A <- matrix(c(1, -1, 2, 0, 3, 4), 2, 3)

X <- A %*% t(A)
Y <- t(A) %*% A

X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Compute the eigen values and eigenvectors of \(X\) and \(Y\)

X.eigen <- pracma::eigjacobi(X)
Y.eigen <- pracma::eigjacobi(Y)
X.eigen
## $V
##            [,1]      [,2]
## [1,]  0.7533635 0.6576043
## [2,] -0.6576043 0.7533635
## 
## $D
## [1]  4.398198 26.601802
Y.eigen
## $V
##            [,1]       [,2]        [,3]
## [1,]  0.7396003  0.6727903 -0.01856629
## [2,] -0.6471502  0.7184510  0.25499937
## [3,]  0.1849001 -0.1765824  0.96676296
## 
## $D
## [1] -8.909597e-17  4.398198e+00  2.660180e+01

Compute the left-singular, singluar and right-singular vectors of \(A\)

decomp <- svd(A)
decomp
## $d
## [1] 5.157693 2.097188
## 
## $u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
## 
## $v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

Show that the two sets of singular vectors are the eigenvectors of \(X\) and \(Y\)

X.eigen$V
##            [,1]      [,2]
## [1,]  0.7533635 0.6576043
## [2,] -0.6576043 0.7533635
decomp$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
round(X.eigen$V,2) == round(-1*matrix(c(decomp$u[2, ], decomp$u[1, ]), 2, 2), 2)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
round(Y.eigen$V, 4)
##         [,1]    [,2]    [,3]
## [1,]  0.7396  0.6728 -0.0186
## [2,] -0.6472  0.7185  0.2550
## [3,]  0.1849 -0.1766  0.9668
round(decomp$v, 4)
##         [,1]    [,2]
## [1,]  0.0186 -0.6728
## [2,] -0.2550 -0.7185
## [3,] -0.9668  0.1766
round(Y.eigen$V[, c(3, 2)], 4) == round(-1*decomp$v, 4)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE

Visual inspection demonstrates that the eigenvectors of \(X\) comprise the \(u\) portion of the SVD decomposition. Similarly the eigenvectors of \(Y\) comprise the \(v\) portion of the SVD decomposition.

The can be proven mathematically by demonstrating equality between the two matrices. Elementary matrix row operations are used to align the matrices and rounding was performed to address R calculation errors.

Show that the non-zero eigenvalues of both \(X\) and \(Y\) are the same and are squares of the non-zero singular values of \(A\)

X.eigen$D
## [1]  4.398198 26.601802
Y.eigen$D
## [1] -8.909597e-17  4.398198e+00  2.660180e+01
decomp$d
## [1] 5.157693 2.097188

Visual inspection demonstrates that the eigenvalues of \(X\) and \(Y\) (after eliminating the 0 eigenvalue) and the singular values of the SVD decomposition are all equal.

This can be proven mathematically as well. The 0 eigenvalue is removed from the \(Y\) list and all the data is rounded to address R calculation error.

round(X.eigen$D, 2)
## [1]  4.4 26.6
round(Y.eigen$D[abs(Y.eigen$D) > 0.01], 2)
## [1]  4.4 26.6
round(c(decomp$d[2]**2, decomp$d[1]**2), 2)
## [1]  4.4 26.6

Problem 2

Write a function to compute the inverse of a well-conditioned full-rank square matrix using co-factors.

myinverse <- function(.A){
  C <- matrix(0, nrow(.A), ncol(.A))
  for(i in 1:nrow(C)){
    for(j in 1:ncol(C)){
      C[i, j] <- (-1)**(i+j)*det(A[-i, -j])
    }
  }
  return(t(C)/det(A))
}
A <- matrix(c(1, 0, 5, 2, 1, 6, 3, 4, 0), 3, 3)
B <- myinverse(A)

round(A %*% B, 5)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
round(myinverse(A), 5) == round(solve(A), 5)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
A <- matrix(c(0, 3, 0, 0, 2, 7, 0, 1, 8, 1, 1, 0, 6, 0, 2, 1), 4, 4)
B <- myinverse(A)

round(A %*% B, 5)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
round(myinverse(A), 5) == round(solve(A), 5)
##      [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE