DATA 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS

Assignment 4

Kyle Gilde

9/23/2017

##                 
## prettydoc   TRUE
## pracma      TRUE
## generalCorr TRUE
## subselect   TRUE

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

\[\begin{equation} A = \begin{bmatrix} 1, 2, 3 \\ −1, 0, 4 \end{bmatrix} \end{equation}\]
a <- c(1, 2, 3, -1, 0, 4)
(A <- matrix(a, 2, 3, byrow = T))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
  • write code in R to compute \(X = AA^T\) and \(Y = A^TA\).
(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
  • Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.
(Xeigen <- eigen(X))
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
(Yeigen <- 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
  • Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command.
# d = singular values; u = left-singular vectors; v = right-singular vectors
(Asvd <- 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
  • Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.
# eigenvectors of symmetric matrices have the following property

isTRUE(all.equal(X %*% Xeigen$vectors, Xeigen$vectors %*% diag(Xeigen$values)))
## [1] TRUE
isTRUE(all.equal(Y %*% Yeigen$vectors, Yeigen$vectors %*% diag(Yeigen$values)))
## [1] TRUE
# If these 2 properties are still TRUE (nearly equal) when we substitute in
# right-singular and left-singular vectors, then these singular vectors are
# eigenvectors of X and Y.

isTRUE(all.equal(X %*% Asvd$u, Asvd$u %*% diag(Xeigen$values)))
## [1] TRUE
isTRUE(all.equal(Y %*% Asvd$v, Asvd$v %*% diag(Yeigen$values[1:2])))
## [1] TRUE
# Additionally, the X and Y eigenvectors can be substituted into SVD.
isTRUE(all.equal(Asvd$u %*% diag(Asvd$d) %*% t(Asvd$v), A))
## [1] TRUE
isTRUE(all.equal(Xeigen$vectors %*% diag(Asvd$d, 2, 3) %*% t(Yeigen$vector), 
    A))
## [1] TRUE
  • 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
isTRUE(all.equal(Xeigen$values, Yeigen$values[1:2]))
## [1] TRUE
  • and are squares of the non-zero singular values of A.
isTRUE(all.equal(Asvd$d, sqrt(Yeigen$values[1:2])))
## [1] TRUE
isTRUE(all.equal(sqrt(Xeigen$values), Asvd$d))
## [1] TRUE

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.

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.

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

rand_sq_matrix <- function(min_dim, max_dim, element_range) {
    ### generate random square matrix###
    dim_range <- c(min_dim:max_dim)
    samp_space <- c(-element_range:element_range)
    
    m_dim <- sample(c(dim_range), 1)
    samp_size <- m_dim^2
    
    # select matrix elements
    rand_samp <- sample(samp_space, samp_size, replace = T)
    
    # create matrix
    current_matrix <- matrix(rand_samp, m_dim)
    
    return(current_matrix)
}

myinverse <- function(A) {
    iMax <- nrow(A)
    jMax <- ncol(A)
    Adet <- det(A)
    # validate input
    if (iMax != jMax) {
        result <- "Not a square matrix"
    } else if (Adet == 0) {
        result <- "Singular matrix"
    } else if (qr(A)$rank != iMax) {
        result <- "Rank-deficient matrix"
    } else {
        
        cof_matrix <- matrix(NA, iMax, jMax)
        
        for (i in 1:iMax) {
            for (j in 1:jMax) {
                cof_matrix[i, j] <- det(as.matrix(generalCorr::cofactor(A, i, 
                  j))) * (-1)^(i + j)
            }
            t_cof_matrix <- t(cof_matrix)
            # Outputs
            Ainverse <- t_cof_matrix/Adet
            A_x_Ainverse <- A %*% Ainverse
            condition <- cond(A)
            nearly_equal_to_I <- isTRUE(all.equal(A %*% Ainverse, diag(1, iMax)))
            nearly_equal_to_solveA <- isTRUE(all.equal(Ainverse, solve(A)))
            
            result <- list(A = A, Ainverse = Ainverse, A_x_Ainverse = A_x_Ainverse, 
                condition = condition, nearly_equal_to_I = nearly_equal_to_I, 
                nearly_equal_to_solveA = nearly_equal_to_solveA)
        }
    }
    return(result)
}

A <- rand_sq_matrix(3, 5, 20)
print(myinverse(A))
## $A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    7    6    7   18  -20
## [2,]   19    1    8   -3   17
## [3,]  -15    6  -17   12    6
## [4,]   12  -17  -16  -17   -6
## [5,]  -17   -6   20  -16  -20
## 
## $Ainverse
##             [,1]       [,2]       [,3]        [,4]       [,5]
## [1,] -0.08296689 -0.2285688 -0.2664698 -0.07747655 -0.1680146
## [2,] -0.61091501 -1.5062048 -1.5570846 -0.57205890 -0.9648668
## [3,]  0.15613640  0.3949740  0.3792149  0.12150962  0.2569030
## [4,]  0.36329290  0.8439639  0.8826495  0.30305203  0.5279556
## [5,]  0.11929844  0.3659478  0.3667200  0.11654073  0.2168110
## 
## $A_x_Ainverse
##               [,1]          [,2]          [,3]          [,4]          [,5]
## [1,]  1.000000e+00  4.440892e-15  4.440892e-15  1.776357e-15  7.105427e-15
## [2,]  1.332268e-15  1.000000e+00 -6.217249e-15  5.773160e-15  4.440892e-15
## [3,] -4.440892e-16  7.549517e-15  1.000000e+00 -4.440892e-16  1.287859e-14
## [4,] -3.108624e-15 -2.220446e-15  4.440892e-16  1.000000e+00 -3.064216e-14
## [5,]  3.108624e-15 -3.286260e-14 -2.664535e-15 -3.552714e-15  1.000000e+00
## 
## $condition
## [1] 126.226
## 
## $nearly_equal_to_I
## [1] TRUE
## 
## $nearly_equal_to_solveA
## [1] TRUE
A <- rand_sq_matrix(3, 5, 20)
print(myinverse(A))
## $A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   16   -7   -6   -8   -9
## [2,]    4    2  -16   -7    6
## [3,]  -13    5   16   -2   -6
## [4,]    5   16  -14   16    3
## [5,]  -20  -16    2  -13    1
## 
## $Ainverse
##               [,1]         [,2]         [,3]        [,4]        [,5]
## [1,]  0.0005288857  0.003289795 -0.022798750 -0.03620663 -0.04315140
## [2,] -0.0055249662  0.054919109  0.059594080  0.00322998 -0.03136481
## [3,] -0.0292327380 -0.010703638  0.001211243 -0.05085881 -0.03902893
## [4,] -0.0048260816 -0.071686043 -0.041961410  0.03871884  0.01875655
## [5,] -0.0820953316  0.033990348 -0.050390525 -0.06739042 -0.04297196
## 
## $A_x_Ainverse
##               [,1]          [,2]          [,3]          [,4]          [,5]
## [1,]  1.000000e+00  1.665335e-16 -3.330669e-16 -6.661338e-16 -4.440892e-16
## [2,]  5.551115e-17  1.000000e+00  0.000000e+00  8.881784e-16 -7.771561e-16
## [3,] -5.551115e-17  5.551115e-17  1.000000e+00 -8.881784e-16  9.992007e-16
## [4,]  3.053113e-16 -2.775558e-16  1.110223e-16  1.000000e+00 -9.992007e-16
## [5,] -2.359224e-16  2.428613e-16  4.163336e-17  2.081668e-16  1.000000e+00
## 
## $condition
## [1] 6.143076
## 
## $nearly_equal_to_I
## [1] TRUE
## 
## $nearly_equal_to_solveA
## [1] TRUE
A <- rand_sq_matrix(3, 5, 20)
print(myinverse(A))
## $A
##      [,1] [,2] [,3]
## [1,]   19   20   10
## [2,]   -7   11   -9
## [3,]    9  -19    2
## 
## $Ainverse
##              [,1]        [,2]        [,3]
## [1,]  0.038893239  0.06003654  0.07569825
## [2,]  0.017488906  0.01357348 -0.02636387
## [3,] -0.008874967 -0.14121639 -0.09109893
## 
## $A_x_Ainverse
##               [,1]          [,2]         [,3]
## [1,]  1.000000e+00 -6.661338e-16 3.330669e-16
## [2,]  2.775558e-16  1.000000e+00 2.220446e-16
## [3,] -4.614364e-16 -2.220446e-16 1.000000e+00
## 
## $condition
## [1] 5.981781
## 
## $nearly_equal_to_I
## [1] TRUE
## 
## $nearly_equal_to_solveA
## [1] TRUE