# creating a square matrix 3x3 A, as per below:
A<- matrix(c(1,2,3,4,5,6, 7,8,9), nrow=3, ncol=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
# finding the transpose of A using t()
T<- t(A)
T
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# Comparing
if ((all.equal(A%*%T, T%*%A)) ==TRUE) print (" A%*%T is equal T%*%A ") else print(" A%*%T is not equal T%*%A ")
## [1] " A%*%T is not equal T%*%A "
(A%*%T == T%*%A)
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
# Hence there exist a Matrix A above where transpose(A) A <> A transpose(A)
# 2- For a special type of square matrix A, we get AT A = AAT. Under what conditions
# could this be true? (Hint: The Identity matrix I is an example of such a matrix).
#
# The identity square matrix is special matrix where transpose(A) A == A transpose(A)
A<- matrix(c(1,0,0,0,1,0,0,0,1), nrow=3, ncol=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
T<- t(A)
T
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
if ((all.equal(A%*%T, T%*%A)) ==TRUE) print (" A%*%T is equal T%*%A ") else print(" A%*%T is not equal T%*%A ")
## [1] " A%*%T is equal T%*%A "
(A%*%T == T%*%A)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
# In addition, any square matrix where all elements are identical, the transpose(A) A == A transpose(A) holds true
# please note the below examples
A<- matrix(c(0,0,0,0,0,0,0,0,0), nrow=3, ncol=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
T<- t(A)
T
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
if ((all.equal(A%*%T, T%*%A)) ==TRUE) print (" A%*%T is equal T%*%A ") else print(" A%*%T is not equal T%*%A ")
## [1] " A%*%T is equal T%*%A "
(A%*%T == T%*%A)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
##
A<- matrix(c(1,1,1,1,1,1,1,1,1), nrow=3, ncol=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 1
## [3,] 1 1 1
T<- t(A)
T
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 1
## [3,] 1 1 1
if ((all.equal(A%*%T, T%*%A)) ==TRUE) print (" A%*%T is equal T%*%A ") else print(" A%*%T is not equal T%*%A ")
## [1] " A%*%T is equal T%*%A "
(A%*%T == T%*%A)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
##
##
A<- matrix(c(12,12,12,12,12,12,12,12,12), nrow=3, ncol=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 12 12 12
## [2,] 12 12 12
## [3,] 12 12 12
T<- t(A)
T
## [,1] [,2] [,3]
## [1,] 12 12 12
## [2,] 12 12 12
## [3,] 12 12 12
if ((all.equal(A%*%T, T%*%A)) ==TRUE) print (" A%*%T is equal T%*%A ") else print(" A%*%T is not equal T%*%A ")
## [1] " A%*%T is equal T%*%A "
(A%*%T == T%*%A)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
#####
myfactorize <- function(A) {
# Array size
d =dim(A)[1]
d
# create an identity matrix
y <- diag( dim(A)[1])
y
# initializing parameters
temp = y
temp[2,1] <- -A[2,1]/A[1,1]
ltemp= temp
# initialize the Upper elimination matrix
temp= temp %*% A
L=ltemp
i=1
# initialize the lower elimination matrix
L= solve(L)
# looping through the martix columns
for (j in 1:(d-i)) {
#y <- diag( dim(A)[1])
for (i in 3:d) {
# check if the pivot element. no permutaion is done when pivot is 0.
if (i != j) {
# Check if the element is zero
if (temp[i,j] != 0 ) {
if (temp[j,j] < 0) y[i,j] <- -temp[i,j] / temp[j,j]
if (temp[j,j] > 0 ) y[i,j] <- -temp[i,j]/ temp[j,j]
#place holder for the Uppper matrix U
temp= y%*%temp
# place holder for the Lower matrix L
L= L %*%solve(y)
#print(temp). Please uncomment to see all the Upper elimination matrices
#print(L). Please uncomment to see all the Lower elimination matrices
#reset y to the identity matrix
y <- diag( dim(A)[1])
}
}
}
}
print("Upper Matrix U")
print(temp)
print("Lower Matrix L")
print(L)
U=temp
solution <- L%*%U
print("Compare L%*%U with original Matrix A. Below is the L%*%U matrix: ")
print (solution)
print("Below is the original Matrix A: ")
print(A)
return(all.equal(solution,A))
}
# Testing for 3x3 Matrix. Note: The below 3x3 matrix is we have in the course module 2
A <- matrix(c(1,2,3,1,1,1,2,0,1),nrow=3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 1 1
## [3,] 2 0 1
myfactorize(A)
## [1] "Upper Matrix U"
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 -1 -2
## [3,] 0 0 3
## [1] "Lower Matrix L"
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 1 1 0
## [3,] 2 4 1
## [1] "Compare L%*%U with original Matrix A. Below is the L%*%U matrix: "
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 1 1
## [3,] 2 0 1
## [1] "Below is the original Matrix A: "
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 1 1
## [3,] 2 0 1
## [1] TRUE
# Testing a 4x4 matrix
A<- matrix(c(1,2,3,4,4.0,3,2,1,-5,-6,7,8,-8,-7,-6,5),nrow=4,byrow=TRUE)
A
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 4 3 2 1
## [3,] -5 -6 7 8
## [4,] -8 -7 -6 5
myfactorize(A)
## [1] "Upper Matrix U"
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 0 -5 -10 -15
## [3,] 0 0 14 16
## [4,] 0 0 0 10
## [1] "Lower Matrix L"
## [,1] [,2] [,3] [,4]
## [1,] 1 0.0 0 0
## [2,] 4 1.0 0 0
## [3,] -5 -0.8 1 0
## [4,] -8 -1.8 0 1
## [1] "Compare L%*%U with original Matrix A. Below is the L%*%U matrix: "
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 4 3 2 1
## [3,] -5 -6 7 8
## [4,] -8 -7 -6 5
## [1] "Below is the original Matrix A: "
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 4 3 2 1
## [3,] -5 -6 7 8
## [4,] -8 -7 -6 5
## [1] TRUE
# Testing for a 5x5 matrix. Please note that in the example, a row permutation step was omitted
# specifically row 3 and row 5. However, a validation checks of L%*%U == A resulted into successful test.
A<- matrix(c(1,2,3,4,5,5,4,3,2,1,6,7,8,9,1,1,2,2,1,3,3,3,1,6,1), nrow=5,byrow=TRUE)
A
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 5 4 3 2 1
## [3,] 6 7 8 9 1
## [4,] 1 2 2 1 3
## [5,] 3 3 1 6 1
myfactorize(A)
## [1] "Upper Matrix U"
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 0 -6 -12 -18 -24
## [3,] 0 0 0 0 -9
## [4,] 0 0 -1 -3 -2
## [5,] 0 0 -3 0 -4
## [1] "Lower Matrix L"
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0.0000000 0 0 0
## [2,] 5 1.0000000 0 0 0
## [3,] 6 0.8333333 1 0 0
## [4,] 1 0.0000000 0 1 0
## [5,] 3 0.5000000 0 -1 1
## [1] "Compare L%*%U with original Matrix A. Below is the L%*%U matrix: "
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 5 4 3 2 1
## [3,] 6 7 8 9 1
## [4,] 1 2 2 1 3
## [5,] 3 3 1 6 1
## [1] "Below is the original Matrix A: "
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 5 4 3 2 1
## [3,] 6 7 8 9 1
## [4,] 1 2 2 1 3
## [5,] 3 3 1 6 1
## [1] TRUE
#
# Testing for a 6x6 matrix.
A <- matrix(c(1, 7/2, 2, 6, 1, 7/2,
2, 9/2, 9, 7/4, 9, 1,
1/4, 1, 1, 7/3, 2, 3,
8, 2/3, 5, 2, 8, 6,
7, 6, 2, 3, 6, 3,
3/2, 5, 2, 3, 7, 4/3),nrow=6, byrow=TRUE)
A
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.00 3.5000000 2 6.000000 1 3.500000
## [2,] 2.00 4.5000000 9 1.750000 9 1.000000
## [3,] 0.25 1.0000000 1 2.333333 2 3.000000
## [4,] 8.00 0.6666667 5 2.000000 8 6.000000
## [5,] 7.00 6.0000000 2 3.000000 6 3.000000
## [6,] 1.50 5.0000000 2 3.000000 7 1.333333
myfactorize(A)
## [1] "Upper Matrix U"
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3.500000e+00 2.00 6.000000e+00 1.00000 3.5000000
## [2,] 0 -2.500000e+00 5.00 -1.025000e+01 7.00000 -6.0000000
## [3,] 0 1.923899e-16 0.75 -6.661267e-16 0.00000 -0.4857514
## [4,] 0 1.620886e-14 0.00 9.415741e+01 0.00000 103.2687687
## [5,] 0 -2.181308e-15 0.00 7.105427e-15 18.49909 17.2558757
## [6,] 0 1.807197e-15 0.00 -5.354192e-15 0.00000 -3.3091736
## [1] "Lower Matrix L"
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.00 0.00000 0.00000 0.000000000 0.0000000 0
## [2,] 2.00 1.00000 0.00000 0.000000000 0.0000000 0
## [3,] 0.25 -0.05000 1.00000 0.003407415 0.1135191 0
## [4,] 8.00 10.93333 -87.55556 0.701661914 -4.1371415 0
## [5,] 7.00 7.40000 -65.33333 0.391365916 -2.8541952 0
## [6,] 1.50 0.10000 -2.00000 -0.052837054 0.2594723 1
## [1] "Compare L%*%U with original Matrix A. Below is the L%*%U matrix: "
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.00 3.5000000 2 6.000000 1 3.500000
## [2,] 2.00 4.5000000 9 1.750000 9 1.000000
## [3,] 0.25 1.0000000 1 2.333333 2 3.000000
## [4,] 8.00 0.6666667 5 2.000000 8 6.000000
## [5,] 7.00 6.0000000 2 3.000000 6 3.000000
## [6,] 1.50 5.0000000 2 3.000000 7 1.333333
## [1] "Below is the original Matrix A: "
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.00 3.5000000 2 6.000000 1 3.500000
## [2,] 2.00 4.5000000 9 1.750000 9 1.000000
## [3,] 0.25 1.0000000 1 2.333333 2 3.000000
## [4,] 8.00 0.6666667 5 2.000000 8 6.000000
## [5,] 7.00 6.0000000 2 3.000000 6 3.000000
## [6,] 1.50 5.0000000 2 3.000000 7 1.333333
## [1] TRUE
# Testing for a 10x10 matrix.
#
A<- matrix(c(1, 3, 2, 3, 5, 2/3, 7, 3, 3/2, 8,
9/4, 3/2, 2, 1/2, 9, 2, 2, 8/3, 6, 2,
9, 2, 4, 8, 9/4, 9, 9/4, 2, 8, 1/3,
1, 2, 4, 6, 7, 8, 2, 2/3, 4, 5,
7/2, 2, 7/3, 3, 1, 5, 9, 6, 5/3, 3,
6, 4/3, 1, 1, 5, 2, 1, 6, 7/3, 5,
2/3, 3, 8, 1, 1, 2, 7, 1/3, 7, 1,
2, 8/3, 5, 6, 5/4, 4/3, 1/3, 5, 5, 9/2,
7, 7/4, 3, 5/4, 8, 4, 9/4, 9/4, 1, 6,
3, 6, 1, 3, 7, 9, 5, 9/2, 2, 9), nrow=10, byrow=TRUE)
A
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1.0000000 3.000000 2.000000 3.00 5.00 0.6666667 7.0000000 3.0000000
## [2,] 2.2500000 1.500000 2.000000 0.50 9.00 2.0000000 2.0000000 2.6666667
## [3,] 9.0000000 2.000000 4.000000 8.00 2.25 9.0000000 2.2500000 2.0000000
## [4,] 1.0000000 2.000000 4.000000 6.00 7.00 8.0000000 2.0000000 0.6666667
## [5,] 3.5000000 2.000000 2.333333 3.00 1.00 5.0000000 9.0000000 6.0000000
## [6,] 6.0000000 1.333333 1.000000 1.00 5.00 2.0000000 1.0000000 6.0000000
## [7,] 0.6666667 3.000000 8.000000 1.00 1.00 2.0000000 7.0000000 0.3333333
## [8,] 2.0000000 2.666667 5.000000 6.00 1.25 1.3333333 0.3333333 5.0000000
## [9,] 7.0000000 1.750000 3.000000 1.25 8.00 4.0000000 2.2500000 2.2500000
## [10,] 3.0000000 6.000000 1.000000 3.00 7.00 9.0000000 5.0000000 4.5000000
## [,9] [,10]
## [1,] 1.500000 8.0000000
## [2,] 6.000000 2.0000000
## [3,] 8.000000 0.3333333
## [4,] 4.000000 5.0000000
## [5,] 1.666667 3.0000000
## [6,] 2.333333 5.0000000
## [7,] 7.000000 1.0000000
## [8,] 5.000000 4.5000000
## [9,] 1.000000 6.0000000
## [10,] 2.000000 9.0000000
myfactorize(A)
## [1] "Upper Matrix U"
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3.000000e+00 2.000000 3.000000e+00 5.000000 0.6666667
## [2,] 0 -5.250000e+00 -2.500000 -6.250000e+00 -2.250000 0.5000000
## [3,] 0 -2.967045e-16 -2.095238 2.373636e-15 0.000000 0.0000000
## [4,] 0 2.413348e-15 0.000000 1.690909e+01 0.000000 0.0000000
## [5,] 0 -2.190619e-16 0.000000 1.752495e-15 -4.566756 0.0000000
## [6,] 0 -2.860596e-16 0.000000 2.288477e-15 0.000000 2.4187514
## [7,] 0 -1.326042e-15 0.000000 1.060834e-14 0.000000 0.0000000
## [8,] 0 4.032790e-16 0.000000 -3.226232e-15 0.000000 0.0000000
## [9,] 0 1.453145e-16 0.000000 -1.162516e-15 0.000000 0.0000000
## [10,] 0 2.346907e-15 0.000000 -2.232797e-14 0.000000 0.0000000
## [,7] [,8] [,9] [,10]
## [1,] 7.00000 3.000000e+00 1.500000 8.0000000
## [2,] -13.75000 -4.083333e+00 2.625000 -16.0000000
## [3,] 0.00000 0.000000e+00 0.000000 2.2331876
## [4,] 0.00000 0.000000e+00 0.000000 -14.8818179
## [5,] 0.00000 0.000000e+00 0.000000 0.5288498
## [6,] 0.00000 0.000000e+00 0.000000 2.0195232
## [7,] -28.64815 1.776357e-15 0.000000 25.5519258
## [8,] 0.00000 1.067082e+01 0.000000 1.0078704
## [9,] 0.00000 0.000000e+00 -7.228804 4.9298986
## [10,] 0.00000 0.000000e+00 0.000000 -27.9707877
## [1] "Lower Matrix L"
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [2,] 2.2500000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [3,] 9.0000000 4.7619048 1.0000000 0.6364567 7.0149823 0.2559368
## [4,] 1.0000000 0.1904762 -1.1818182 0.2478239 -0.5317935 2.9924924
## [5,] 3.5000000 1.6190476 0.2954545 0.1548899 2.8153775 0.7678105
## [6,] 6.0000000 3.1746032 1.4621212 0.1680321 3.9102465 -1.4831212
## [7,] 0.6666667 -0.1904762 -2.9545455 -0.1295443 0.6047848 0.6824983
## [8,] 2.0000000 0.6349206 -1.2348485 0.2346817 1.6032011 -0.1312497
## [9,] 7.0000000 3.6666667 0.8750000 0.1872760 4.1057589 -1.0335911
## [10,] 3.0000000 0.5714286 1.7045455 -0.1436252 1.4702527 2.7759304
## [,7] [,8] [,9] [,10]
## [1,] 0.00000000 0.00000000 0.00000000 0
## [2,] 0.00000000 0.00000000 0.00000000 0
## [3,] -0.16497369 -0.52063051 2.49003830 0
## [4,] 0.08311017 -0.14577654 -0.27667092 0
## [5,] -0.23603289 0.19783959 1.08362778 0
## [6,] -0.09252932 0.09024262 2.07503192 0
## [7,] 0.00997322 -0.22907742 -0.89918050 0
## [8,] 0.17231509 0.14924741 -0.04611182 0
## [9,] -0.12798966 -0.35402875 2.64566569 0
## [10,] 0.28423678 -0.20304590 0.55334184 1
## [1] "Compare L%*%U with original Matrix A. Below is the L%*%U matrix: "
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1.0000000 3.000000 2.000000 3.00 5.00 0.6666667 7.0000000 3.0000000
## [2,] 2.2500000 1.500000 2.000000 0.50 9.00 2.0000000 2.0000000 2.6666667
## [3,] 9.0000000 2.000000 4.000000 8.00 2.25 9.0000000 2.2500000 2.0000000
## [4,] 1.0000000 2.000000 4.000000 6.00 7.00 8.0000000 2.0000000 0.6666667
## [5,] 3.5000000 2.000000 2.333333 3.00 1.00 5.0000000 9.0000000 6.0000000
## [6,] 6.0000000 1.333333 1.000000 1.00 5.00 2.0000000 1.0000000 6.0000000
## [7,] 0.6666667 3.000000 8.000000 1.00 1.00 2.0000000 7.0000000 0.3333333
## [8,] 2.0000000 2.666667 5.000000 6.00 1.25 1.3333333 0.3333333 5.0000000
## [9,] 7.0000000 1.750000 3.000000 1.25 8.00 4.0000000 2.2500000 2.2500000
## [10,] 3.0000000 6.000000 1.000000 3.00 7.00 9.0000000 5.0000000 4.5000000
## [,9] [,10]
## [1,] 1.500000 8.0000000
## [2,] 6.000000 2.0000000
## [3,] 8.000000 0.3333333
## [4,] 4.000000 5.0000000
## [5,] 1.666667 3.0000000
## [6,] 2.333333 5.0000000
## [7,] 7.000000 1.0000000
## [8,] 5.000000 4.5000000
## [9,] 1.000000 6.0000000
## [10,] 2.000000 9.0000000
## [1] "Below is the original Matrix A: "
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1.0000000 3.000000 2.000000 3.00 5.00 0.6666667 7.0000000 3.0000000
## [2,] 2.2500000 1.500000 2.000000 0.50 9.00 2.0000000 2.0000000 2.6666667
## [3,] 9.0000000 2.000000 4.000000 8.00 2.25 9.0000000 2.2500000 2.0000000
## [4,] 1.0000000 2.000000 4.000000 6.00 7.00 8.0000000 2.0000000 0.6666667
## [5,] 3.5000000 2.000000 2.333333 3.00 1.00 5.0000000 9.0000000 6.0000000
## [6,] 6.0000000 1.333333 1.000000 1.00 5.00 2.0000000 1.0000000 6.0000000
## [7,] 0.6666667 3.000000 8.000000 1.00 1.00 2.0000000 7.0000000 0.3333333
## [8,] 2.0000000 2.666667 5.000000 6.00 1.25 1.3333333 0.3333333 5.0000000
## [9,] 7.0000000 1.750000 3.000000 1.25 8.00 4.0000000 2.2500000 2.2500000
## [10,] 3.0000000 6.000000 1.000000 3.00 7.00 9.0000000 5.0000000 4.5000000
## [,9] [,10]
## [1,] 1.500000 8.0000000
## [2,] 6.000000 2.0000000
## [3,] 8.000000 0.3333333
## [4,] 4.000000 5.0000000
## [5,] 1.666667 3.0000000
## [6,] 2.333333 5.0000000
## [7,] 7.000000 1.0000000
## [8,] 5.000000 4.5000000
## [9,] 1.000000 6.0000000
## [10,] 2.000000 9.0000000
## [1] TRUE
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.