Data 605 Assignment 4

library(knitr)
library(rmdformats)

## Global options
options(max.print="31")
# opts_chunk$set(echo=FALSE,
#                cache=TRUE,
#                prompt=FALSE,
#                tidy=TRUE,
#                comment=NA,
#                message=FALSE,
#                warning=FALSE)
opts_knit$set(width=31)

library(pracma)

Problem Set 1

Original matrix A (given)

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

\[\begin{equation*} A = \begin{bmatrix} 1 & 2 & 3\\ -1 & 0 & -4 \end{bmatrix} \end{equation*}\]

\(X = AA^T\)

\(Y = A^TA\)

# Calculate X and Y
X <- A %*% t(A)
Y <- t(A) %*% A
eigenX <- eigen(X)
eigenY <- eigen(Y)

svd_A <- svd(A)

Eigenvalues of \(X\) and \(Y\)

cat("Eigenvalues of X:", eigenX$values, "\n")
## Eigenvalues of X: 26.6018 4.398198
cat("Eigenvalues of Y:", eigenY$values)
## Eigenvalues of Y: 26.6018 4.398198 1.058982e-16

Eigenvectors of \(X\) and \(Y\)

cat("Eigenvector of X:",  "\n" )
## Eigenvector of X:
eigenX$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
cat("Eigenvector of Y:",  "\n" )
## Eigenvector of Y:
eigenY$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

Singular Value Decomposition

# svd_A
 
 cat("Left-singular vector u:",  "\n" )
## Left-singular vector u:
 print(svd_A$u)
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
 cat("Singular values of d:\n") 
## Singular values of d:
 print(svd_A$d)
## [1] 5.157693 2.097188
 cat("Right-Singular vector v:\n") 
## Right-Singular vector v:
 print(svd_A$v)
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

Comparison: Eigenvalues and Singular Values of \(A\)

First of all, we’ve examined the eigenvalues of X and Y. 2 of the eigenvalues are basically the same. The following code validates that Eigenvalue of the vector X is equal to squares of non-zero singular values in the element d in svd_A.

eigenX$values 
## [1] 26.601802  4.398198
#[1] 26.601802  4.398198
eigenY$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
# [1] 2.660180e+01 4.398198e+00 1.058982e-16
all.equal(round(eigenX$values, digits=10), round(svd_A$d^2, digits=10))
## [1] TRUE

Comparison: Eigenvectors and Singular Vectors of \(A\)

First, let’s check eigenvector of X and singular vector U

print("Eigenvector X")
## [1] "Eigenvector X"
eigenX$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
print("Singular Vector U")
## [1] "Singular Vector U"
svd_A$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043

Multiplying the scalar of -1 to vector \(x_1\) of eigenvector of X,

eigenX$vectors[,1] <- eigenX$vectors[,1] * (-1)
all.equal(round(eigenX$vectors, digits=10), round(svd_A$u, digits=10))
## [1] TRUE

We validated that eigenvector of X is equal to the singular vector U.

Second, let’s check eigenvector of Y and singular vector V

print("Eigenvector Y")
## [1] "Eigenvector Y"
eigenY$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
print("Singular Vector V")
## [1] "Singular Vector V"
svd_A$v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

Doing the same for vector \(y_1\) of eigenvector Y,

eigenY$vectors[,1] <- eigenY$vectors[,1] * (-1)
all.equal(round(eigenY$vectors[,1:2], digits=10), round(svd_A$v, digits=10))
## [1] TRUE

We validated that eigenvector of Y is equal to the singular vector V.

Problem Set 2

myinverse <- function(A){
  # Check to see if A is a square matrix
  if (dim(A)[1]!=dim(A)[2]) {
    return(NA)
  }
  
  # check to see if A is inversible 
  if (det(A)==0) {
    return(NA)
  }
  
  
  # creating an indentify matix of dimension m X N of matrix (A)
  m <- nrow (A)
  n <- ncol (A)
  
  # initialize co-factor matrix C with dimension m X n with 0
  C <- matrix(0, n, n)
   
  for (i in 1:m) {
      for (j in 1:n){
      C[i,j] <- (-1)^(i+j)*det(A[-i,-j])      
      }
  }
  
  # inverse of A is equal to transpose of the co-factor matrix (C) and divide it by the determinant of A
  
  return (t(C)/det(A))
}  

\[\begin{equation*} Let \space A = \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 2\\ 5 & 9 & 7 \end{bmatrix} \end{equation*}\]

Check and see if AB = I

Results

A <- matrix(c(1,2,3,6,5,6,7,8,9), nrow=3)
B <- myinverse(A)
round(A %*% B, digits=10)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1