\[\mathbf{A} = \left[\begin{array} {rrr}1&2&3\\4&5&6\\7&8&9\end{array}\right]\]
\[\mathbf{A^T} = \left[\begin{array} {rrr}1&4&7\\2&5&8\\3&6&9\end{array}\right]\]
mat_A <- matrix(c(1,4,7,2,5,8,3,6,9),ncol=3)
mat_A_T <- t(mat_A)
mat_A_dot_At <- mat_A%*%mat_A_T
At_dot_mat_A <- mat_A_T%*%mat_A
##Print the matrices out and test their equality
mat_A_dot_At==At_dot_mat_A## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
\[\mathbf{(A*A^t} = \left[\begin{array} {rrr}14&32&50\\32&77&122\\50&122&194\end{array}\right])\neq\mathbf{(A^t*A} = \left[\begin{array} {rrr}66&78&90\\78&93&108\\90&108&126\end{array}\right])\]
library(dplyr)##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
swap_zero <- function(A,N,x){
##inputs A- Matrix
## N- Row I want to swap with original row
## x-oriignal row
orig_row <- A[x,]
A[x,] <- A[N,]
A[N,] <- orig_row
return(A)}
build_lower_upper <- function(A,size){
##inputs A- Matrix
## size- length input matrix
N=1
L <- diag(size)
### check for row swaps
for (my_col in 1:(dim(A)[1]-1)){
ifelse(A[my_col,my_col]!=0,A,
ifelse(A[my_col+1,my_col]!=0,A <- swap_zero(A,N+1,my_col) ,
ifelse(A[my_col+2,my_col]!=0, A <- swap_zero(A,N+2,my_col),
ifelse(A[my_col+3,my_col]!=0, A <- swap_zero(A,N+3,my_col),"error"))))
## make 0's
for (mat_val in (my_col+1):dim(A)[1]){
if (A[mat_val,my_col]==0){
L[mat_val,1]==0
}else {original_row<- A[mat_val,my_col]
constant <- A[mat_val,my_col]/A[my_col,my_col]
A[mat_val,] <- (-A[mat_val,my_col]/A[my_col,my_col])*A[my_col,]+A[mat_val,]
L[mat_val,my_col] <- constant
}
}
}
return(list(lower=L,upper=A))
}bb <- matrix(c(2,1,-6,4,-4,-9,-4,3,5),ncol=3)
three_three <- build_lower_upper(bb,3)
cc <- matrix(c(1,2,0,0,5,12,4,0,0,5,13,6,0,0,5,11),ncol=4)
four_four <- build_lower_upper(cc,4)
four_four## $lower
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 2 1 0 0
## [3,] 0 2 1 0
## [4,] 0 0 2 1
##
## $upper
## [,1] [,2] [,3] [,4]
## [1,] 1 5 0 0
## [2,] 0 2 5 0
## [3,] 0 0 3 5
## [4,] 0 0 0 1
four_four$lower%*%four_four$upper==cc## [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE
three_three$lower%*%three_three$upper==bb## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE