Problem set 1

Calculating X = AA_T and Y = A_TA

A = matrix(c(1,-1,2,0,3,4), nrow=2, byrow=FALSE)
A_T = t(A)

X = A%*%A_T
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y = A_T%*%A
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Calculating Eigenvalues & Eigenvectors of X & Y

eigen_X_val = eigen(X)$values
eigen_X_vec = eigen(X)$vectors

eigen_Y_val = eigen(Y)$values
eigen_Y_vec = eigen(Y)$vectors

Calculating SVD of A

\[ A = U\Sigma V^T \]

svd_A = svd(A)

svd_A_u = svd_A$u # Left Singular vectors (U)
svd_A_v = svd_A$v # Right Singular vectors (V)
svd_A_d = diag(svd_A$d) # Singular values

# Left Singular Vectors of A ~ Eigenvectors of X
eigen_X_vec
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
svd_A_u 
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
# Right Singular Vectors of A ~ Eigenvectors of Y
eigen_Y_vec
##             [,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_v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
# Singular values of A
svd_A_d
##          [,1]     [,2]
## [1,] 5.157693 0.000000
## [2,] 0.000000 2.097188

Reconstructing matrix A from SVD

round(svd_A_u%*%svd_A_d%*%t(svd_A_v),0)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

Problem set 2

Defining Inverse Matrix Function Logic

myinverse <- function(A){

  if (ncol(A) != nrow(A)) {
    print ("Error: Matrix provided not squared")
    return(NULL)
  }
  
  if (det(A) == 0) {
    print ("Error: Matrix determinant is Zero")
    return(NULL)
  }
  
  # Co-Factors Calculation
  
  Rs <- nrow(A)
  Cs <- ncol(A)
  tmp_mat <- matrix(rep(0,length(A)), nrow = Rs, ncol = Cs)
  
  for(i in 1:Cs){
    for (j in 1:Rs){
      tmp_mat[j,i] = det(A[-j,-i])
      if ((j+i) %% 2 != 0){
        tmp_mat[j,i] = -1 * tmp_mat[j,i]
      }
    }
  }
  inv_mat = t(tmp_mat)/det(A)
  return(inv_mat)
}

Testing Inverse Matrix Function

A <- matrix(c(1,3,1,2,3,4,2,5,3), nrow=3, ncol=3, byrow=FALSE)
B <- myinverse(A)
B
##      [,1] [,2] [,3]
## [1,]   11   -2   -4
## [2,]    4   -1   -1
## [3,]   -9    2    3
round(A%*%B,0) # Shold be equal to the Identity Matrix
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1