Assume the following two matrices:
\[\mathbf{A} = \left[\begin{array} {rrr} A_{11} & A_{12} \\ A_{21} & A_{22} \end{array}\right] \]
\[\mathbf{A^T} = \left[\begin{array} {rrr} A_{11} & A_{21} \\ A_{12} & A_{22} \end{array}\right] \] If we multiply AAT the value in the first cell (position 1,1) would be \[ A_{11}A_{11}+A_{12}A_{12} \] whereas if we multiply ATA the value in the first cell would be \[A_{11}A_{11}+A_{21}A_{21}\] So clearly the two are not equivalent.
LU_decomp <- function(A){
### Check to make sure matrix is not singular, rectangular, or comprised of zeros on pivot
if(sum(diag(A)==0)>0 | qr(A)$rank<max(nrow(A), ncol(A))) {
message("Error: This matrix cannot be decomposed.")
} else {
U <- A
# Get U by going column by column and algebraically putting zeros in all positions below the diagonal
for (j in 1:(ncol(U)-1)){
for (i in (j+1):nrow(U)){
if (U[i,j]!=0){U[i,] <- U[i, ] - U[j, ]*(U[i,j]/U[j,j])}
}
}
# Get L by restating L U = A as U_inv L_inv = A_inv ,solving for L_inv, and then taking inverse to get L
U_inv <- solve(U)
A_inv <- solve(A)
L_inv <- solve(U_inv, A_inv)
L <- solve(L_inv)
list(L=round(L,2) , U=round(U,2))
}
}
### Test the function
test_A <- rbind(c(1, 1, 3),
c(2, -1, 5),
c(-1, -2, 4))
LU <- LU_decomp(test_A)
\[A= \begin{bmatrix} 1 & 1 & 3 \\ 2 & -1 & 5 \\ -1 & -2 & 4 \\ \end{bmatrix} \]
\[L= \begin{bmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ -1 & 0.33 & 1 \\ \end{bmatrix} \]
\[U= \begin{bmatrix} 1 & 1 & 3 \\ 0 & -3 & -1 \\ 0 & 0 & 7.33 \\ \end{bmatrix} \]
# try matrix with zero pivot
test_A <- rbind(c(0,1,1),
c(1,2,3),
c(7,7,9))
LU_decomp(test_A)
## Error: This matrix cannot be decomposed.
# try matrix that is singular
test_A <- rbind(c(1,1,1),
c(1,2,3),
c(7,7,7))
LU_decomp(test_A)
## Error: This matrix cannot be decomposed.