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)
\[\begin{equation*} A = \begin{bmatrix} 1 & 2 & 3\\ -1 & 0 & -4 \end{bmatrix} \end{equation*}\]
\(X = AA^T\)
\(Y = A^TA\)
Eigenvalues of \(X\) and \(Y\)
## Eigenvalues of X: 26.6018 4.398198
## Eigenvalues of Y: 26.6018 4.398198 1.058982e-16
Eigenvectors of \(X\) and \(Y\)
## Eigenvector of X:
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
## Eigenvector of 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
Singular Value Decomposition
## Left-singular vector u:
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
## Singular values of d:
## [1] 5.157693 2.097188
## Right-Singular vector 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.
## [1] 26.601802 4.398198
## [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
## [1] "Eigenvector X"
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
## [1] "Singular Vector 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
## [1] "Eigenvector 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
## [1] "Singular Vector 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*}\]