Show that \[A^T \times A \neq A \times A^T\] (Proof and demonstration.)
While matrix multiplication is possible, the dimensions and matrix value are different.
A = 3 rows x 2 cols A Transpose = 2 rows x 3 cols
When AATranspose, the matrix dimensions are 3 x 3.
When ATA, the matrix dimensions are 2 x 2.
# Define matrix A
A <- matrix(c(1, 3, 5, 2, 4, 6), nrow = 3, ncol = 2, byrow = FALSE)
print(A)
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
# Compute the transpose of A
A_transpose <- t(A)
print(A_transpose)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
# Compute A^T x A
A_transpose_A <- A_transpose %*% A
# Compute A x A^T
A_A_transpose <- A %*% A_transpose
# Print the results
print(A_transpose_A)
## [,1] [,2]
## [1,] 35 44
## [2,] 44 56
# Print the results
print(A_A_transpose)
## [,1] [,2] [,3]
## [1,] 5 11 17
## [2,] 11 25 39
## [3,] 17 39 61
For a special type of square matrix A, we get \[A^T A = AA^T\] Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).
This is true when the matrix is:
Additionally, the zero matrix and symmetrical matrices will also satisfy this equation.
Write an R function to factorize a square matrix A into LU or LDU.
This function performs the LU Decomp by looping through U to transform it into the upper matrix while constructing the L lower matrix with its multipliers.
(Code was generated and explained by ChatGPT)
# LU Decomposition in R
lu_decomposition <- function(A) {
# Get the dimensions of the square matrix A
n <- nrow(A)
# Initialize L as an identity matrix
L <- diag(n)
# Initialize U as a copy of A
U <- A
# Loop through each pivot row from the first to the second last
for (pivot_row in 1:(n - 1)) {
# Loop through each row below the pivot row
for (target_row in (pivot_row + 1):n) {
# Calculate the multiplier for the row operation
multiplier <- U[target_row, pivot_row] / U[pivot_row, pivot_row]
# Store the multiplier in the L matrix
L[target_row, pivot_row] <- multiplier
# Perform row operation to eliminate elements below the diagonal in U
U[target_row, ] <- U[target_row, ] - multiplier * U[pivot_row, ]
}
}
# Return L and U matrices as a list
return(list(L = L, U = U))
}
# Example
A <- matrix(c(4, 3, 0, 2, 4, 1, 0, 1, 3), nrow = 3, ncol = 3)
# Matrix A
print(A)
## [,1] [,2] [,3]
## [1,] 4 2 0
## [2,] 3 4 1
## [3,] 0 1 3
result <- lu_decomposition(A)
# Print L
print(result$L)
## [,1] [,2] [,3]
## [1,] 1.00 0.0 0
## [2,] 0.75 1.0 0
## [3,] 0.00 0.4 1
# Print U
print(result$U)
## [,1] [,2] [,3]
## [1,] 4 2.0 0.0
## [2,] 0 2.5 1.0
## [3,] 0 0.0 2.6
# Verify A = L x U
L = result$L
U = result$U
# The LU multiplication matches matrix A
print( L %*% U )
## [,1] [,2] [,3]
## [1,] 4 2 0
## [2,] 3 4 1
## [3,] 0 1 3
This example uses the matrixcalc package to perform the operations
Source: https://www.rdocumentation.org/packages/matrixcalc/versions/1.0-6/topics/lu.decomposition
#install.packages("matrixcalc")
library(matrixcalc)
# Initialize matrix A
A <- matrix( c ( 1, 2, 2, 1 ), nrow=2, byrow=TRUE)
# LU Decomposition of matrix A
luA <- lu.decomposition( A )
#Construct L lower triangle
L <- luA$L
# Construct U upper triangle
U <- luA$U
# Print L
print( L )
## [,1] [,2]
## [1,] 1 0
## [2,] 2 1
# Print U
print( U )
## [,1] [,2]
## [1,] 1 2
## [2,] 0 -3
# Multiply L with U to recreate A
print( L %*% U )
## [,1] [,2]
## [1,] 1 2
## [2,] 2 1
# Compare it to A
print( A )
## [,1] [,2]
## [1,] 1 2
## [2,] 2 1