library("pracma")
library("matlib")

1. 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\)

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

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

\(A\)

A = matrix(c(1,2,3,-1,0,4), nrow=2, byrow = TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

\(X = AA^{T}\)

mutiplicationM<-function(a){
      n=nrow(a)
      M<-matrix(0,nrow=n,ncol=n)
      for (i in 1:n)
        for (j in 1:n)
          M[i,j]=sum(a[i,]*a[j,])
      return(M)
}

X<-mutiplicationM(A)
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17

\(Y = A^{T}A\)

Y<-mutiplicationM(t(A))
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
#eigenvalues and eigenvector of X
eigen(X)
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
#eigenvalues and eigenvector of Y
eigen(Y)
## $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
The 3rd eigenvlue is close to 0, therefore, Y is nearly a sigular matrix.The eigenvlue 0 corresponds to 3rd column of vectorspace, the 3rd vectorspace will be subspace of $R^2$. 

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.

#$d is eigenvalue of A
#$u is singular value of A, same as eignvectors(X=AAT)
#$v is eigenvector of A, close to eignvectors(Y=ATA)
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
Since A is 2\*3 matrix which has one free variable, A is not invertibe matric.Since A is not invertible matric, so A is singular matirx with one free variable. 

ranks(A) =2 , and A has two eigenvlues values, which means A is in space $R^2$.

The sigular value decomposition of A is the factorization of A into the product of three matrices $A=u*d*v^T$ where the column of u is diagonal with positive real entries. 

$u is left sigular vector of A, and equal to eigen(X)$vectors 
$v is right sigular vector of A, and close to eigen(Y)$vectors

Theorem HMOE: Hermitian Matrices have Orthogonal Eigenvectors
Suppose that A is a Hermitian matrix and x and y are two eigenvectors of A for
different eigenvalues. Then x and y are orthogonal vectors.

Therefore, $u and $v are Orthogonal Eigenvectors.


I creat diagonal eigenvectors with using $d
D<-diag(svd(A)$d,nrow=2,ncol=2)
D
##          [,1]     [,2]
## [1,] 5.157693 0.000000
## [2,] 0.000000 2.097188

\(A=u*d*v^T\)

round(svd(A)$u %*% D %*% t(svd(A)$v),5)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
The result of u*D*v^T gets back to null space A.
svd method with 3 decomposition matrics, improves the speed of caluculating large matrixs.

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 \(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.

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

A<-matrix(c(1,1,3,2,-1,5,-1,-2,4),nrow=3,ncol=3, byrow = TRUE)
myinverse<-function(A){
  n=nrow(A)
  m<-matrix(0,nrow=n,ncol=n)
  for (i in 1:n)
    for(j in 1:n)
      m[i,j]=cofactor(A,i,j)
  return(t(m)/det(A)) 
}
B<-myinverse(A)
B
##            [,1]        [,2]        [,3]
## [1,] -0.2727273  0.45454545 -0.36363636
## [2,]  0.5909091 -0.31818182 -0.04545455
## [3,]  0.2272727 -0.04545455  0.13636364
round(A%*%B,5)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Please submit PS1 and PS2 in an R-markdown document with your frst initial and last name.