## Problem set 1

### (1) Show that $A^TA \neq AA^T$ in general. (Proof and demonstration.)

Let $A = \begin{bmatrix}a & b\\ c & d\\ \end{bmatrix}$

and let $A^T = \begin{bmatrix}e & f\\ g & h\\ \end{bmatrix}$

Then $AA^T = \begin{bmatrix}ae+bg & af+bh\\ ce+dg & cf+dh\\ \end{bmatrix}$

and $A^TA = \begin{bmatrix}ea+fc & eb+fd\\ ga+hc & gb+hd\\ \end{bmatrix}$

As can be seen from the above, the 2 matrix products are not equal i.e. this matrix multiplication is not commutative. We can further verify this with an example below

A<-matrix(c(1,0,2,5),nrow=2)
A
##      [,1] [,2]
## [1,]    1    2
## [2,]    0    5
#Let C be it's transpose
C<-t(A)
C
##      [,1] [,2]
## [1,]    1    0
## [2,]    2    5
# Now let's multiply the A and its transpose in both orders (A first and then C first):
A%*%C
##      [,1] [,2]
## [1,]    5   10
## [2,]   10   25
C%*%A
##      [,1] [,2]
## [1,]    1    2
## [2,]    2   29
#As can be seen from this, the 2 matrix products are not equal.

### (2) For a special type of square matrix A, we get $A^TA = AA^T$ Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

This would be true for symmetric matrices, of which the identity matrix is one example. For a symmetric square matrix, the transpose is the same as the original matrix. Therefore the order does not matter when it is multiplied with its transpose. An example is given below:

A<-matrix(c(1,0,2,0,3,0,2,0,1),nrow=3)
A
##      [,1] [,2] [,3]
## [1,]    1    0    2
## [2,]    0    3    0
## [3,]    2    0    1
#Let C be it's transpose
C<-t(A)
C
##      [,1] [,2] [,3]
## [1,]    1    0    2
## [2,]    0    3    0
## [3,]    2    0    1
# Now let's multiply the A and its transpose in both orders (A first and then C first):
A%*%C
##      [,1] [,2] [,3]
## [1,]    5    0    4
## [2,]    0    9    0
## [3,]    4    0    5
C%*%A
##      [,1] [,2] [,3]
## [1,]    5    0    4
## [2,]    0    9    0
## [3,]    4    0    5
#As can be seen from this, the 2 matrix products are equal.

## 2. Problem set 2

### Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. Please submit your response in an R Markdown document using our class naming convention, E.g. LFulton_Assignment2_PS2.png. You don’t have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code. If you doing the entire assignment in R, then please submit only one markdown document for both the problems.

LUdecomp<-function(matrixA) {

numberOfRows<-nrow(matrixA)
numberOfCols<-ncol(matrixA)
matrixU<-matrixA
matrixL<-diag(x=1,nrow=numberOfRows,ncol=numberOfCols)

for (i in 1:(numberOfRows-1)) {
for (j in (i+1):numberOfRows){
if (matrixU[i,i]!=0) {
multiplier<-matrixU[j,i]/matrixU[i,i]
matrixL[j,i]<-multiplier
matrixU[j,]<-matrixU[j,]-multiplier*matrixU[i,]
}
}
}
return(list('U'=matrixU,'L'=matrixL))
}

### Test the LU decomp function on a sample matrix:

A<-matrix(c(1,4,2,0,3,2,5,1,8), nrow=3)
A
##      [,1] [,2] [,3]
## [1,]    1    0    5
## [2,]    4    3    1
## [3,]    2    2    8
B<-LUdecomp(A)

cat("The upper triangular matrix is: \n")
## The upper triangular matrix is:
B$U ## [,1] [,2] [,3] ## [1,] 1 0 5.00000 ## [2,] 0 3 -19.00000 ## [3,] 0 0 10.66667 cat("The lower triangular matrix is: \n") ## The lower triangular matrix is: B$L
##      [,1]      [,2] [,3]
## [1,]    1 0.0000000    0
## [2,]    4 1.0000000    0
## [3,]    2 0.6666667    1
cat("The product of the U and L matrices is :\n")
## The product of the U and L matrices is :
#Multiply the L and U matrices together
B$L %*% B$U
##      [,1] [,2] [,3]
## [1,]    1    0    5
## [2,]    4    3    1
## [3,]    2    2    8
cat("As can be seen, this matrix multiplication returns the original input matrix. This proves that the factorization is correct.\n")
## As can be seen, this matrix multiplication returns the original input matrix. This proves that the factorization is correct.