Problem Set 1

matrix A:

\(\begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{bmatrix}\)

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

Compute \(x\ =A{ A }^{ T }\) and \(y = { A }^{ T }A\)

Solution:

X = A %*% t(A) # Multiply matrix A by the transpose of matrix A
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y = t(A) %*% A # Multiply the transpose of matrix A by the matrix A
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Compute Eigenvalues and Eigenvectors of X and Y

Solution:

eigen_X <- eigen(X) # Finds the eigenvalues and eigenvectors of matrix X
eigen_values_X <- round(eigen_X$values, 3) # Stores eigenvalues of matrix X in eigen_values_X
eigen_values_X
## [1] 26.602  4.398
eigen_vectors_X <- eigen_X$vectors # Stores eigenvectors of matrix X in eigen_vectors_X
eigen_vectors_X
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigen_Y <- eigen(Y) # Finds the eigenvalues and eigenvectors of matrix Y
eigen_values_Y <- round(eigen_Y$values, 3) # Stores eigenvalues in matrx Y in eigen_values_Y
eigen_values_Y
## [1] 26.602  4.398  0.000
eigen_vectors_Y <- eigen_Y$vectors #Stores eigenvectors of matrix Yin eigen_vectors_Y
eigen_vectors_Y
##             [,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 left-singular, singular values, and right-singular vectors of A

Solution:

svd_A <- svd(A) # Finds the left-singular, singular values, and right-singular vectors of matrix A
left_singular <- svd_A$u # Stores the left singular vectors of matrix A in left_singular
left_singular
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
singular_values <- round(svd_A$d, 3) # Stores the singular values of A in singular_values
singular_values
## [1] 5.158 2.097
right_singular <- svd_A$v # Stores the right singular vectors of matrix A in right_singular
right_singular
##             [,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

Solution:

eigen_vectors_X
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
left_singular
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
eigen_vectors_Y
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
right_singular
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

As you can see above, the two sets of singular vectors are indeed eigenvectors of X and Y. The difference being that there are some negative values in the first column for the two sets of singular vectors that the eigenvectors of X and Y have positive. Also the right singlar vectors donโ€™t have a third column, but you can see that the third column for the eigenvectors of Y are close to 0.

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.

eigen_values_X
## [1] 26.602  4.398
eigen_values_Y
## [1] 26.602  4.398  0.000
round(singular_values ^ 2, 3)
## [1] 26.605  4.397

The eigenvalues of both X and Y and the squares of the singular values of A are equal to each other. The only difference being that the singular values are slightly off due to rounding.

Problem Set 2

myinverse <- function(A){
  dimension_A <- nrow(A) # Find the dimension of matrix A and store it in dimension_A
  matrix <- matrix(NA, nrow = dimension_A, ncol = dimension_A) # Create an empty matrix using the dimension of matrix A
  for (i in 1:dimension_A) {
    for (j in 1:dimension_A) {
      matrix[i,j] <- det(A[-i,-j]) * (-1) ^ (i + j) # Use determinant of matrix A to find values for matrix
    }
  }
  tranpose_matrix <- t(matrix) # Take the transpose of created matrix
  inverse_matrix <- tranpose_matrix / det(A) # Find inverse of created matrix by dividing transpose of matrix by the determinant of matrix A
  return(inverse_matrix)
}
A <- matrix(c(1, 2, 0, 6, 7, 8, 9, 3, 4), nrow = 3)
B <- myinverse(A)
B 
##       [,1]  [,2]  [,3]
## [1,]  0.04  0.48 -0.45
## [2,] -0.08  0.04  0.15
## [3,]  0.16 -0.08 -0.05
round(A %*% B, 3) # Verify that multiplying A and B gives the Identity matrix I
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1