Grando 4 Homework

Question 1

Given a 3 × 2 matrix A

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

write code in R to compute \(X=A{ A }^{ T }\) and \(Y={ A }^{ T }A\).

aat_function <- function(x) {
    x <- x %*% t(x)
    return(x)
}

ata_function <- function(x) {
    x <- t(x) %*% x
    return(x)
}

(X <- aat_function(A))
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
(Y <- ata_function(A))
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

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

(eigenvalues_x <- eigen(X)[["values"]])
## [1] 26.601802  4.398198
(eigenvectors_x <- eigen(X)[["vectors"]])
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
(eigenvalues_y <- eigen(Y)[["values"]])
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
(eigenvectors_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

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

SVD <- svd(A)
# Singular values are stored in d
(d <- SVD$d)
## [1] 5.157693 2.097188
# Left singular vectors are stored in u
(u <- SVD$u)
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
# Right singular vectors are stored in v
(v <- SVD$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.

# For eigenvalue 26.6, u is an eigenvector with a multiple of -1
all.equal(eigenvectors_x[, 1], u[, 1] * -1)
## [1] TRUE
# For eigenvalue 4.4, u is an eigenvector with a multiple of 1
all.equal(eigenvectors_x[, 2], u[, 2])
## [1] TRUE
# For eigenvalue 26.6, v is an eigenvector with a multiple of -1
all.equal(eigenvectors_y[, 1], v[, 1] * -1)
## [1] TRUE
# For eigenvalue 4.4, v is an eigenvector with a multiple of 1
all.equal(eigenvectors_y[, 2], v[, 2])
## [1] 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.

# Here we can compare each of the nonzero eigenvalues of X an Y and compare them
# to the singular values of A (d variable)
all.equal(eigenvalues_x[1], d[1]^2, tolerance = 1e-04)
## [1] TRUE
all.equal(eigenvalues_x[2], d[2]^2, tolerance = 1e-04)
## [1] TRUE
all.equal(eigenvalues_y[1], d[1]^2, tolerance = 1e-04)
## [1] TRUE
all.equal(eigenvalues_y[2], d[2]^2, tolerance = 1e-04)
## [1] TRUE
# The third vector should be very close to zero for Y
all.equal(eigenvalues_y[3], 0, tolerance = 1e-04)
## [1] TRUE

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.

Question 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.

myinverse <- function(x) {
    if (det(x) == 0) {
        stop("Matrix is not invertible")
    }
    cx <- matrix(c(rep(0, dim(x)[1] * dim(x)[2])), nrow = dim(x)[1])
    for (col in 1:as.integer(dim(x)[2])) {
        for (row in 1:as.integer(dim(x)[1])) {
            cx[row, col] <- (-1)^(row + col) * det(x[-row, -col])
        }
    }
    return(1/det(x) * t(cx))
}
(A <- matrix(c(7, 2, 1, 0, 3, -1, -3, 4, -2, 1, 5, 7, 4, 3, 8, 5), nrow = 4, byrow = TRUE))
##      [,1] [,2] [,3] [,4]
## [1,]    7    2    1    0
## [2,]    3   -1   -3    4
## [3,]   -2    1    5    7
## [4,]    4    3    8    5
B <- myinverse(A)

Time to check:

(A %*% B)
##               [,1]          [,2]          [,3]          [,4]
## [1,]  1.000000e+00 -1.942890e-16 -1.498801e-15  2.331468e-15
## [2,]  5.551115e-16  1.000000e+00  6.661338e-16 -1.609823e-15
## [3,] -5.551115e-16 -1.665335e-16  1.000000e+00  1.665335e-15
## [4,] -8.881784e-16 -1.665335e-16 -3.108624e-15  1.000000e+00
all.equal(A %*% B, diag(dim(A)[1]), tolerance = 1e-04)
## [1] TRUE