Problem set 1:

Write code in R to compute X = AAT and Y = ATA. Then, compute the eigenvaluesand eigenvectors of X and Y using the built-in commans in R. 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.

#  COMPUTE X AND Y:

A<- matrix(c(1, -1, 2, 0, 3, 4), 2,3)
(X <-(A)%*%t(A))
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
(Y<- t(A)%*%(A))
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
# COMPUTING EIGEN VALUES AND EIGEN VECTORS OF 'X' AND 'Y' :
(E_X <- eigen(X))
## eigen() decomposition
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
(E_Y<- eigen(Y))
## 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
# COMPUTE LEFT SINGULAR, SINGULAR, RIGHT SINGULAR OF 'A' USING "SVD" FUNCTION :
(left_singular_A <- svd(A)$u)
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
(singular_A <- svd(A)$d)
## [1] 5.157693 2.097188
(right_singular_A <- svd(A)$v)
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

By Examine the results:

The two sets of singular vectors and show that they are indeed eigenvectors of X and Y.

The two non-zero eigenvalues of both X and Y are the same and are squares of the non-zero singular values of A.

Problem set 2:

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.

# CREATING FUNCTION WITH ARGUMENT
myinverse <- function(V){
    
# CHECK FOR SQUARE MATRIX AND DETERMINENT NOT EQUAL TO ZERO
if(is.matrix(V) & nrow(V) == ncol(V) & det(V) != 0){
        print("INVERSE OF MATRIX")
    
# CALCULATING DETERMINENT OF THE MATRIX        
detV <- det(V)

# DEFINING NULL MATRIX        
C <- matrix(0, nrow = nrow(V), ncol = ncol(V))

# USING FOR LOOP TO CALCULATE COFACTOR MATRIX
    for(i in 1:nrow(V)){
     for(j in 1:ncol(V)){
         C[i,j] = (-1)^(i+j)*det(V[-i,-j]) 
     }
}

# CALCULATING INVERSE BY THE FORMULAE
inverse = t(C)/detV

# IF STATEMENT FAILS IT WILL EXECUTE ELSE STATEMENT
    }
    else
        {
        print("Invalid Input")
        }
    }
# DEFINING MATRIX A 
(A <- matrix(c(1,2,0,3,0,4,5,0,6), 3,3))
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    0    0
## [3,]    0    4    6
# CALLING FUNCTION BY IT'S NAME 
(B<- myinverse(A))
## [1] "INVERSE OF MATRIX"
##      [,1] [,2] [,3]
## [1,]    0  0.5  0.0
## [2,]   -3  1.5  2.5
## [3,]    2 -1.0 -1.5
# CHECK FOR "A×B = I"
(I<- A%*%B)
##               [,1]         [,2]          [,3]
## [1,]  1.000000e+00 8.881784e-16 -1.776357e-15
## [2,]  0.000000e+00 1.000000e+00  0.000000e+00
## [3,] -3.552714e-15 0.000000e+00  1.000000e+00
round(I)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1