### Elina Azrilyan

September 18th, 2019

#### 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 <- matrix(c(1, -1, 2, 0, 3, 4), nrow=2)

A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

Write code in R to compute X = AAt and Y = AtA.

X = AAt:

X <- A %*% t(A)

X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17

Y = AtA

Y <- t(A) %*% A

Y
##      [,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 of X
EValX <- eigen(X)$values round(EValX,2) ##  26.6 4.4 # Eigenvectors of X EVecX <- eigen(X)$vectors
EVecX 
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
# Eigenvalues of Y
EValY <- eigen(Y)$values round(EValY,2) ##  26.6 4.4 0.0 # Eigenvectors of Y EVecY <- eigen(Y)$vectors
EVecY
##             [,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(A)
## $d ##  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 #left-singular vectors u <- svd(A)$u

#singular values
d<- svd(A)$d #right-singular vectors v<- svd(A)$v

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.

Comparing left-singular vectors of A to Eigenvectors of X:

u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
EVecX
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

Comparing right-singular vectors of A to Eigenvectors of Y

v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
EVecY
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001

Comparing singular values of A to Eigenvectors of X and Y:

round(d^2,2)
##  26.6  4.4
round(EValX,2)
##  26.6  4.4
round(EValY,2)
##  26.6  4.4  0.0

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

myinverse <- function(A) {

#Check that the matrix is square
if(nrow(A) != ncol(A)) {
return('Invalid input')
}

#Creating identity matrix to be filled out with cofactors
cofactor <- diag(nrow = dim(A))

#The following loops move through the matrix and fill in cofactor matrix with values:
for (i in 1:dim(A)){ #
for (j in 1:dim(A)){
cofactor[i,j]=((-1)^(i+j))*det(A[-i,-j])
}
}

# Now that we have the cofactor matrix, we return A^(-1) = CT/det(A)
return(t(cofactor)/det(A))
} 

We can test out the function with some examples:

Test 1. Non square Matrix.

A <- matrix(c(2, 4, -4, 1, -4, 3, -6, -9, 5, 3, 4, 2), nrow=3, byrow = TRUE)

myinverse(A)
##  "Invalid input"

Test 2. 3x3 Square Matrix.

A <- matrix(c(2, 4, -4, 1, -4, 3, -6, -9, 5), nrow=3, byrow = TRUE)

round(myinverse(A),2)
##       [,1]  [,2]  [,3]
## [1,]  0.13  0.30 -0.07
## [2,] -0.43 -0.26 -0.19
## [3,] -0.61 -0.11 -0.22
#Let's validate that A×B = I
round(myinverse(A)%*%A,1)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Test 3. 4x4 Square Matrix.

A <- matrix(c(2, 4, -4, 1, -4, 3, -6, -9, 5, 1, 4, 3, 5, 1, -1, 0), nrow=4, byrow = TRUE)
round(myinverse(A),2)
##       [,1]  [,2]  [,3]  [,4]
## [1,] -0.05 -0.01  0.00  0.22
## [2,]  0.17  0.11  0.26 -0.24
## [3,] -0.08  0.07  0.24 -0.15
## [4,]  0.13 -0.12 -0.07 -0.08
#Let's validate that A×B = I
round(myinverse(A)%*%A,1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1