Problem set 1

write code in R to compute X = A\(\mathbf{A}^\intercal\) and Y = \(\mathbf{A}^\intercal\)A. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R. Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command. Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y. In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both X and Y are the same and are squares of the non-zero singular values of A. Your code should compute all these vectors and scalars and store them in variables. Please add enough comments in your code to show me how to interpret your steps.

# define matrix A
A <- matrix(cbind(1,2,3,-1,0,4), byrow = T, ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
# compute X using t() function for matrix transpose
X <- A %*% t(A)
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
# compute Y
Y <- t(A) %*% A 
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

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

eigen_X <- eigen(X)

# eigen values of X
eigen_X$values
## [1] 26.601802  4.398198
# eigen vectors of X
eigen_X$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigen_Y <- eigen(Y)
# eigen values of Y
eigen_Y$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
# eigen vectors of Y
eigen_Y$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

compute the left-singular, singular values, and right-singular vectors of A using the svd command.

# svd() function
# d -> vector containing the singular values of given matrix
# u -> matrix whose columns contain the left singular vectors of given marix
# v -> matrix whose columns contain the right singular vectors of given marix


svd_A <- svd(A)
svd_A
## $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

Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.

list(eigen_X$vectors, svd_A$u)
## [[1]]
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
## 
## [[2]]
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
# comparing the left singular value svd_A$u with eigen_X
round(svd(A)$u,4) == round(eigen_X$vectors,4)
##       [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
u_rounded <- round(svd(A)$u,4)

# multiply first column of left singular by -1
u_rounded[,1] = -u_rounded[,1]

round(u_rounded,4) == round(eigen_X$vectors,4)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
list(eigen_Y$vectors, svd_A$v)
## [[1]]
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
## 
## [[2]]
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
# comparing the left singular value svd_A$u with eigen_Y
round(svd(A)$v,4) == round(eigen_Y$vectors[,-3],4)
##       [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
## [3,] FALSE TRUE
v_rounded <- round(svd(A)$v,4)
# multiply first column of right singular by -1
v_rounded[,1] = -v_rounded[,1]
# compare values
round(v_rounded,4) == round(eigen_Y$vectors[,-3],4)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE

In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both X and Y are the same and are squares of the non-zero singular values of A.

round(svd_A$d^2,4) 
## [1] 26.6018  4.3982
round(eigen_X$values,4)
## [1] 26.6018  4.3982
# using format function to not display eigen_Y$values in scientific notation
eigen_Y$values <- format(eigen_Y$values, scientific=F)
# convert the values from string to numeric values and then round off
round(as.numeric(eigen_Y$values), 4)
## [1] 26.6018  4.3982  0.0000

Hence we see the 3rd value is zero (after rounded off else close to 0) for eigen_Y values and it’s first 2 are equal to eigen_X values and squares of the non-zero singular values of A.

Problem set 2

Using the procedure outlined in section 1 of the weekly handout, write a function to compute the inverse of a well-conditioned full-rank square matrix using co-factors. In order to compute the co-factors, you may use built-in commands to compute the determinant. Your function should have the following signature:

B = myinverse(A)

where A is a matrix and B is its inverse and AxB = I. The off-diagonal elements of I should be close to zero, if not zero. Likewise, the diagonal elements should be close to 1, if not 1. Small numerical precision errors are acceptable but the function myinverse should be correct and must use co-factors and determinant of A to compute the inverse.

myinverse <- function(A) {
  
  # get rows and columns
  rows <- nrow(A)
  cols <- ncol(A)
  
  # check if given matrix is a square matrix and full rank
  if(rows!=cols | qr(A)$rank != dim(A)[1]) {
    return("Cant move forward. A should be square and full rank matrix.")
    
  } else {
    # create an empty co-factors matrix
    cofactors <- matrix(rep(0, length(A)), nrow = rows, ncol = cols)
    
    for(i in 1:rows) {
      for(j in 1:cols) {
        # M is a matrix with row i and col j excluded
        M <- A[-i,-j]
        # populate co-factors matrix
        cofactors[i,j] <- (-1)^(i+j) * det(M)
      }
    }
    
    # return inverse of matrix
    B <- t(cofactors)/det(A)
    return(B)
  }
}

Test with non square matrix

A <- matrix(cbind(1,2,3,4,5,6), nrow = 2, byrow = T)
myinverse(A)
## [1] "Cant move forward. A should be square and full rank matrix."

Test with square matrix

A = matrix(c(1,8,3,-1,9,4,6,7,9), nrow = 3, byrow = T)
B <- myinverse(A)
B
##            [,1]        [,2]        [,3]
## [1,]  0.3955224 -0.38059701  0.03731343
## [2,]  0.2462687 -0.06716418 -0.05223881
## [3,] -0.4552239  0.30597015  0.12686567

Product of A and B should be 1 or close to 1

round(A %*% B, 4)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Hence AXB = I