ASSIGNMENT 4 IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - 2014 1. Problem Set 1 In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 × 2 matrix A

A <- matrix(c(1, 2, 3, -1, 0, 4), nrow = 2, byrow = TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

write code in R to compute X = AAT and Y = ATA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands 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.

# compute X = AAT and Y = ATA
X <- matrix(data = NA)
X <- A %*% t(A)
Y <- matrix(data = NA)
Y <- t(A) %*% A
# compute the eigenvalues and eigenvectors

valX <- eigen(X)$values
valX
## [1] 26.601802  4.398198
valY <- eigen(Y)$values
valY
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
vecX <- eigen(X)$vectors
vecX
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
vecY <- eigen(Y)$vectors
vecY
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
# use the svd command to compute the singular, left-singular, and right-singular vectors

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

We see that U contains the eigenvectors of X scaled by -1, V contains the first 2 eigenvectors of Y scaled by -1. We also see that the eigenvalues of X are the square of D.

VX <- matrix(c(0,0,0,0), 2, 2)
VX[1,1] <- valX[1]
VX[2,2] <- valX[2]
VX
##         [,1]     [,2]
## [1,] 26.6018 0.000000
## [2,]  0.0000 4.398198
DX <- matrix(c(0,0,0,0), 2, 2)
DX[1,1] <- svdA$d[1]
DX[2,2] <- svdA$d[2]
DX %*% DX
##         [,1]     [,2]
## [1,] 26.6018 0.000000
## [2,]  0.0000 4.398198

We see that DX = VX.

  1. 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 A×B = 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. Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
myinverse <- function(A) {
#check is A is full rank and a square matrix
     if (nrow(A) == ncol(A) & Matrix::rankMatrix(A)[1] == nrow(A)) {
     full_square <- TRUE
     } else {full_square <- FALSE}
    
    if(full_square == TRUE) {
    n <- nrow(A)
    C <- matrix(nrow = n, ncol = n)
    for (i in 1:n) {
        for (j in 1:n) {
    #create M, which is submatrix of A without the ith and jth row
    M <- A[-i, -j]
    #create C, the matrix of determinants of each M submatrix
    C[i,j] <- ((-1)^(i+j)) * det(M) 
    }
  }  
  #calculate B as the transpose of C, scaled by 1/det(A)
      B <- t(C) / det(A)
        } else { B <- "non-invertible matrix"
    }
    B
}

Then, we test the function.

A <- matrix(c(1, 2, 3, 1, 1, 1, 2, 0, 1), nrow=3)
# we store the answer in 'B'
B <- myinverse(A)
B
##            [,1]       [,2]       [,3]
## [1,] -0.3333333 -0.3333333  0.6666667
## [2,]  0.6666667  1.6666667 -1.3333333
## [3,]  0.3333333 -0.6666667  0.3333333
# then check that A %*% B is the identity matrix
I <- A %*% B
I
##              [,1]         [,2] [,3]
## [1,] 1.000000e+00 0.000000e+00    0
## [2,] 0.000000e+00 1.000000e+00    0
## [3,] 5.551115e-17 1.110223e-16    1