1. Problem Set 1

write code in R to compute X = AA^T and Y = A^TA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R.

Solution:

Let A^T be the transpose martrix of A.

#find the transpose matrix of A : AT
transposeMatrix <- function(A){
  AT <- matrix(, nrow=dim(A)[2], ncol=dim(A)[1])
  for(i in 1:dim(A)[1]){
    AT[,i]<- A[i,]
  }
  return (AT)
}

#A and AT matrixs multiplication 
matrixMultiplication <- function(A){
  AT <-transposeMatrix(A)
  
  solutionMatrix <- matrix(,nrow = dim(A)[1],ncol = dim(AT)[2])
  for(i in 1: dim(A)[1]){
    for(j in 1:dim(AT)[2]){
      solutionMatrix[i,j]<-sum(A[i,] * AT[,j])
    }
  }
  return (solutionMatrix)
}

A = matrix(c(1,2,3,-1,0,4),nrow=2,ncol=3, byrow=TRUE)
AT <-transposeMatrix(A)

A%*%AT # check x which computed by matrixMultiplication function 
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
x <- matrixMultiplication(A) 
x
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
eigen(x) # find eigenvalue and eigenvetors by build-in function
## eigen() decomposition
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
AT%*%A # check y which computed by matrixMultiplication function
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
y <- matrixMultiplication(AT) 
y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
eigen(y) # find eigenvalue and eigenvetors by build-in function
## eigen() decomposition
## $values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
## 
## $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
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

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.

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

Answer: the steps to get inverse matrix using cofactors are following: Step 1: calculating the Matrix of Minors, Step 2: then turn that into the Matrix of Cofactors, Step 3: then the Adjugate, and. Step 4: multiply that by 1/Determinant.

Then test the result by A^(-1) %*% A = I.

myinverse <- function(A){
  
  #step1&2: get the Matrix of Minors:
  #creat an empty matrix to store the values of minors matrix
  minorMarix = matrix(,nrow=dim(A)[1], ncol=dim(A)[2], byrow=TRUE)
  
  for(i in 1:dim(A)[1]){
    temp=A[-i,]
    for(j in 1:dim(A)[2]){
      minors<- temp[,-j]
      minorMarix[i,j] = (-1)^(i+j) * det(minors)
    }
  }
  
  #step3:find the transpose matrix of C : CT
  CT <- matrix(, nrow=dim(minorMarix)[2], ncol=dim(minorMarix)[1])
  for(i in 1:dim(minorMarix)[1]){
    CT[,i]<- minorMarix[i,]
  }
  
  #step4:get inverse matrix of A : A^-1
  inverseMarix <- (1/det(A)) * CT
  
  return(inverseMarix)
}

#sample1
A1 = matrix(c(1,2,3,-1,0,4,2,5,7,4,5,4,7,8,8,9,2,1,0,3,2,3,5,-1,0),nrow=5,ncol=5, byrow=TRUE)
A1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3   -1    0
## [2,]    4    2    5    7    4
## [3,]    5    4    7    8    8
## [4,]    9    2    1    0    3
## [5,]    2    3    5   -1    0
B1 <- myinverse(A1)
B1
##            [,1]        [,2]        [,3]          [,4]       [,5]
## [1,] -0.6756757  0.02702703 -0.05405405  1.081081e-01  0.4324324
## [2,] 10.3243243  1.36036036 -0.72072072  1.081081e-01 -6.5675676
## [3,] -5.3243243 -0.69369369  0.38738739 -1.081081e-01  3.5675676
## [4,]  3.0000000  0.66666667 -0.33333333 -1.800362e-17 -2.0000000
## [5,] -3.0810811 -0.75675676  0.51351351 -2.702703e-02  1.8918919
#test: inverseMarix * A = I
round(A1%*%B1,5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1
#sample2
A2 = matrix(c(1,2,9,-4,2,6,0,4,0),nrow=3,ncol=3, byrow=TRUE)
A2
##      [,1] [,2] [,3]
## [1,]    1    2    9
## [2,]   -4    2    6
## [3,]    0    4    0
B2 <- myinverse(A2)
B2
##           [,1]        [,2]        [,3]
## [1,] 0.1428571 -0.21428571  0.03571429
## [2,] 0.0000000  0.00000000  0.25000000
## [3,] 0.0952381  0.02380952 -0.05952381
#test: inverseMarix * A = I
round(A2%*%B2,5)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1