Problem set 1

1.1 write code in R to compute X = AAT and Y = ATA

\[\mathbf{A} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ -1 & 0 & 4 \end{array}\right] \]

A <- matrix(c(1, -1, 2, 0, 3, 4), nrow=2, ncol=3); A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
X <- A %*% t(A); X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y <- t(A) %*% A; Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

1.2 Compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R

X_e_values <- eigen(X)$values; X_e_values
## [1] 26.601802  4.398198
Y_e_values <- eigen(Y)$values; Y_e_values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
X_e_vectors <- eigen(X)$vectors; X_e_vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
Y_e_vectors <- eigen(Y)$vectors; Y_e_vectors
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001

1.3 Compute the left-singular, singular values, and right-singular vectors of A using the svd command

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

Problem set 2

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

myinverse <- function(C){
  CM <- matrix(0,nrow=nrow(C), ncol(C))
  for (i in 1:ncol(C)){
    for (j in 1:nrow(C)){
      CM[i, j] <- det(A[-i,-j])*(-1)^(i+j)    
    }
  }
  t(CM)/det(C)
}

A <- matrix(c(3, 2, 0, 0, 0, 1, 2, -2, 1), nrow=3, ncol=3) ;A
##      [,1] [,2] [,3]
## [1,]    3    0    2
## [2,]    2    0   -2
## [3,]    0    1    1
B <- myinverse(A);B
##      [,1] [,2] [,3]
## [1,]  0.2  0.2    0
## [2,] -0.2  0.3    1
## [3,]  0.2 -0.3    0
solve(A)
##      [,1] [,2] [,3]
## [1,]  0.2  0.2    0
## [2,] -0.2  0.3    1
## [3,]  0.2 -0.3    0

A x B = I (Identity Matrix)

A %*% B 
##      [,1]          [,2] [,3]
## [1,]    1 -1.110223e-16    0
## [2,]    0  1.000000e+00    0
## [3,]    0  0.000000e+00    1