library(Matrix)
library(pracma)
## 
## Attaching package: 'pracma'
## The following objects are masked from 'package:Matrix':
## 
##     expm, lu, tril, triu
library(r2symbols)
library(tinytex)
# Utility function to print matrices in proper LaTeX format

print_mat <- function(mat) {
  n <- nrow(mat)
  c('\\begin{bmatrix}',
    paste0(sapply(seq_len(n - 1),
                  function(i) paste0(mat[i, ], collapse = ' & ')),
           ' \\\\'),
    paste0(mat[n, ], collapse = ' & '),
    '\\end{bmatrix}')
} 

##——————– Problem Set 1 ——————–

###———- Q1 ———-

The Rank of a matrix is defined as The maximum number of its linearly independent columns (or rows ) of a matrix is called the rank of a matrix. The rank of a matrix is the numAer of nonzero rows in the reduced matrix. The rank of a matrix cannot exceed the number of its rows or columns

R1 <-c(1,2,3,4)
R2 <-c(-1,0,1,3)
R3 <-c(0,1,-2,1)
R4 <-c(5,4,-2,-3)

A<- rbind(R1,R2,R3,R4)

# Assigning readable Row and Column names

colnames(A) <- c("C1", "C2","C3","C4")
rownames(A) <- c("R1", "R2", "R3", "R4") 

orig_A <- A

# Verifing that (A) is matrix form - sanity check

is.matrix(A)
## [1] TRUE
# Matrix (A) before row operations to derive Reduced Row Echelon Form

print(A)
##    C1 C2 C3 C4
## R1  1  2  3  4
## R2 -1  0  1  3
## R3  0  1 -2  1
## R4  5  4 -2 -3
# The following Row Operations are used to derive the RREF of (A)

R2 <- R1 + R2

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3] [,4]
## R1    1    2    3    4
## R2    0    2    4    7
## R3    0    1   -2    1
## R4    5    4   -2   -3
R4 <- R4 - 5*R1

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3] [,4]
## R1    1    2    3    4
## R2    0    2    4    7
## R3    0    1   -2    1
## R4    0   -6  -17  -23
R2 <- R2/2

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]  [,4]
## R1    1    2    3   4.0
## R2    0    1    2   3.5
## R3    0    1   -2   1.0
## R4    0   -6  -17 -23.0
R1 <- R1 - 2*R2

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]  [,4]
## R1    1    0   -1  -3.0
## R2    0    1    2   3.5
## R3    0    1   -2   1.0
## R4    0   -6  -17 -23.0
R3 <- R3 - R2

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]  [,4]
## R1    1    0   -1  -3.0
## R2    0    1    2   3.5
## R3    0    0   -4  -2.5
## R4    0   -6  -17 -23.0
R4 <- R4 + 6*R2

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3] [,4]
## R1    1    0   -1 -3.0
## R2    0    1    2  3.5
## R3    0    0   -4 -2.5
## R4    0    0   -5 -2.0
R3 <- R3/-4

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]   [,4]
## R1    1    0   -1 -3.000
## R2    0    1    2  3.500
## R3    0    0    1  0.625
## R4    0    0   -5 -2.000
R1 <- R1 + R3

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]   [,4]
## R1    1    0    0 -2.375
## R2    0    1    2  3.500
## R3    0    0    1  0.625
## R4    0    0   -5 -2.000
R2 <- R2 - (2*R3)

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]   [,4]
## R1    1    0    0 -2.375
## R2    0    1    0  2.250
## R3    0    0    1  0.625
## R4    0    0   -5 -2.000
R4 <- R4 + (5*R3)

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]   [,4]
## R1    1    0    0 -2.375
## R2    0    1    0  2.250
## R3    0    0    1  0.625
## R4    0    0    0  1.125
R4 <- 8*R4/9

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]   [,4]
## R1    1    0    0 -2.375
## R2    0    1    0  2.250
## R3    0    0    1  0.625
## R4    0    0    0  1.000
R1 <- R1 + (19*R4/8)

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]  [,4]
## R1    1    0    0 0.000
## R2    0    1    0 2.250
## R3    0    0    1 0.625
## R4    0    0    0 1.000
R2 <- R2-(9*R4/4)

A<- rbind(R1,R2,R3,R4)

print(A)
##    [,1] [,2] [,3]  [,4]
## R1    1    0    0 0.000
## R2    0    1    0 0.000
## R3    0    0    1 0.625
## R4    0    0    0 1.000
R3 = R3 - (5*R4/8)

A<- rbind(R1,R2,R3,R4)

print( A) # The Final RREF Form of A 
##    [,1] [,2] [,3] [,4]
## R1    1    0    0    0
## R2    0    1    0    0
## R3    0    0    1    0
## R4    0    0    0    1
print(paste0("To verify the RREF manipulations above we will use the R Built 
             in function to Verify"))
## [1] "To verify the RREF manipulations above we will use the R Built \n             in function to Verify"
prref <- (rref(orig_A))

print(paste0(" This is the Built in Function"))
## [1] " This is the Built in Function"
print(prref)
##    C1 C2 C3 C4
## R1  1  0  0  0
## R2  0  1  0  0
## R3  0  0  1  0
## R4  0  0  0  1
print(paste0(" Checking that the final RREF matricies are equvalent"))
## [1] " Checking that the final RREF matricies are equvalent"
all.equal(A,prref)
## [1] "Attributes: < Component \"dimnames\": Component 2: target is NULL, current is character >"
print(paste0(" since the RREF result of the given matrix equals the results 
             of the RREF function, we can say that the RREF operations were 
             Successful"))
## [1] " since the RREF result of the given matrix equals the results \n             of the RREF function, we can say that the RREF operations were \n             Successful"

From the result aAove and the Definition given

The RANK of the given matrix is 4

##———- Q2 ———-

The maximum rank of an mxn matrix where m > n would be n since m>n and the maximum rank cannot exceed the lessor of the number of rows or columns

The rank of a null matrix is zero. A null matrix has no non-zero rows or columns. So, there are no independent rows or columns. Hence the rank of a null matrix is zero – Hence the minimum rank that a matrix can have is ** ZERO - 0 **

##———- Q3 ———-

R1 <-c(1,2,1)
R2 <-c(3,6,3)
R3 <-c(2,4,2)

B<- rbind(R1,R2,R3)

# Assigning readable Row and Column names

colnames(B) <- c("C1", "C2","C3")
rownames(B) <- c("R1", "R2", "R3") 

# Verifing that (A) is matrix form - sanity check

is.matrix(B)
## [1] TRUE
print(B)
##    C1 C2 C3
## R1  1  2  1
## R2  3  6  3
## R3  2  4  2
# Matrix (B) before row operations to derive Reduced Row Echelon Form


R2 <- (3*R1)-R2
R3 <- (2*R1)-R3

B<- rbind(R1,R2,R3)

print(B)
##    [,1] [,2] [,3]
## R1    1    2    1
## R2    0    0    0
## R3    0    0    0

since we are able to Row Reduce the matrix to a single row R1 From the Definition we can say that this matrix B Has a Rank of ONE - “1”

##——————– Problem Set 2 ——————–

\[ \begin{aligned} \\ \\ &A = \begin{bmatrix} 1 & 2 & 3 \\ 0 & 4 & 5 \\ 0 & 0& 6 \\ \end{bmatrix} \quad \\ \\ & \text {The characteristics Polynomial is given by :}\\ \\ & Det (A - \lambda I) \\ \\ &= \quad \begin{vmatrix} \begin{bmatrix} 1 & 2 & 3 \\ 0 & 4 & 5 \\ 0 & 0& 6 \\ \end{bmatrix} \quad - \quad \begin{bmatrix} \lambda & 0 & 0 \\ 0 & \lambda & 0 \\ 0 & 0 & \lambda \\ \end{bmatrix} \end{vmatrix} \quad \\ \\ &= \quad \begin{vmatrix} (1 - \lambda) & (2 - 0) & (3 - 0) \\ (0 - 0) & (4 - \lambda) & (5 - 0) \\ (0 - 0) &(0 - 0) & (6 - \lambda) \\ \end{vmatrix} \\ \\ &= \quad \begin{vmatrix} (1 - \lambda) & 2 & 3 \\ 0 & (4-\lambda) & 5 \\ 0 & 0 & (6 - \lambda) \\ \end{vmatrix} \\ \\ &= \quad 1 - \lambda \begin{vmatrix} 4\lambda & 5 \\ 0 & 6 - \lambda \end{vmatrix} \quad - \quad 2 \begin{vmatrix} 0 & 5 \\ 0 & 6 - \lambda \end{vmatrix} \quad + \quad 3 \begin{vmatrix} 0 & 4 - \lambda \\ 0 & 6 \end{vmatrix} \\ \\ &= \quad 1 - \lambda \begin{vmatrix} 4 - \lambda & 5 \\ 0 & 6 - \lambda \end{vmatrix} \\ \\ & \text {Factoring out the final Equation :}\\ \\ &= (1 - \lambda) (4 - \lambda) (6 - \lambda) \\ \\ \\ &= (1 - \lambda)(24 - 4\lambda - 6\lambda + \lambda^2) \\ \\ \\ &= 24 - 4\lambda - 6\lambda + \lambda^2 - 24\lambda + 4\lambda^2 + 6\lambda^2 - \lambda^3 \\ \\ \\ & \text {The final Characteristic Polynomial is given by :}\\ \\ &=-\lambda^3 + 11\lambda^2 - 34\lambda + 24 \\ \\ \\ & \text {The Calculated Eigenvalues are :}\\ \\ &\lambda_1 = 1\\ &\lambda_2 = 4\\ &\lambda_3 = 6\\ \\ &\text {Using the eigehvalues to determine the eigenvectors :}\\ \\ &\text {We will substitute the values of } \lambda \\ &\text {in the folowing Equation:}\\ \\ & Det (A - \lambda I) \\ \\ &\text {We know that }\\ \\ &(A - \lambda I) = \begin{vmatrix} (1 - \lambda) & 2 & 3 \\ 0 & (4-\lambda) & 5 \\ 0 & 0 & (6 - \lambda) \\ \end{vmatrix} \\ \\ &\text {Substituting For } \lambda = 1\\ \\ &\text {We have } \begin{vmatrix} (1 - 1) & 2 & 3 \\ 0 & (4-1) & 5 \\ 0 & 0 & (6 - 1) \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ \\ &\text {The Augmented Matrix to be Row Reduced is : }\\ \\ &\begin{vmatrix} 0 & 2 & 3 \\ 0 & 3 & 5 \\ 0 & 0 & 5 \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ &\text {The following Row Operations are Performed to Row Reduce : }\\ \\ &R_1 = R_1/2\\ &R_2 = r_2-3*R_1\\ &R_2 = R_2*2\\ &R_3 = R_3-5*R_2\\ &R_1 = R_1-3*R_1/2\\ \\ &\text {The Resultant Row Reduced Matrix is : }\\ \\ &\begin{vmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \end{vmatrix} \\ \\ &\text {From this resultant matrix we deduce : }\\ \\ &x_1 = x_1\\ &x_2 = 0\\ &x_3 = 0\\ \\ &\text {The General Solution : }\\ &X = \begin{pmatrix} x_1\\ 0\\ 0\\ \end{pmatrix}\\ \\ \\ &\text {The Solution Set : }\\ \\ &\begin{Bmatrix} x_1&* \begin{pmatrix} 1\\ 0\\ 0 \end{pmatrix} \end{Bmatrix} &\text {Substituting For } \lambda = 4\\ \\ &\text {We have } \begin{vmatrix} (1 - 4) & 2 & 3 \\ 0 & (4-4) & 5 \\ 0 & 0 & (6 - 4) \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ \\ &\text {The Augmented Matrix to be Row Reduced is : }\\ \\ &\begin{vmatrix} -3 & 2 & 3 \\ 0 & 0 & 5 \\ 0 & 0 & 2 \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ &\text {The following Row Operations are Performed to Row Reduce : }\\ \\ &R_1 = -R_1/3\\ &R_2 = r_2/5\\ &R_3 = R_3-2R2\\ &R_1 = R_1-(-1)*R_2\\ \\ &\text {The Resultant Row Reduced Matrix is : }\\ \\ &\begin{vmatrix} 1 & -2/3 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \end{vmatrix} \\ \\ &\text {From this resultant matrix we deduce : }\\ \\ &x_1 = 2x_2/3\\ &x_2 = x_2\\ &x_3 = 0\\ \\ &\text {The General Solution : }\\ &X = \begin{pmatrix} 2x_2/3\\ x_2\\ 0\\ \end{pmatrix}\\ \\ \\ &\text {The Solution Set : }\\ \\ &\begin{Bmatrix} x_2&* \begin{pmatrix} 2/3\\ 1\\ 0 \end{pmatrix} \end{Bmatrix} &\text {Substituting For } \lambda = 6\\ \\ &\text {We have } \begin{vmatrix} (1 - 6) & 2 & 3 \\ 0 & (4-6) & 5 \\ 0 & 0 & (6 - 6) \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ \\ &\text {The Augmented Matrix to be Row Reduced is : }\\ \\ &\begin{vmatrix} -5 & 2 & 3 \\ 0 & -2 & 5 \\ 0 & 0 & 0 \end{vmatrix} \begin{vmatrix} x_1\\ x_2\\ x_3 \end{vmatrix} = \begin{vmatrix} 0\\ 0\\ 0 \end{vmatrix}\\ \\ &\text {The following Row Operations are Performed to Row Reduce : }\\ \\ &R_1 = R_1/-5\\ &R_2 = R_2/-2\\ &R_1 = R_1-(-2/5)R_2\\ \\ &\text {The Resultant Row Reduced Matrix is : }\\ \\ &\begin{vmatrix} 1 & 0 & -8/5 \\ 0 & 1 & -5/2 \\ 0 & 0 & 0 \end{vmatrix} \\ \\ &\text {From this resultant matrix we deduce : }\\ \\ &x_1 = 8x_2/5\\ &x_2 = 5x_3/2\\ &x_3 = x_3\\ \\ &\text {The General Solution : }\\ &X = \begin{pmatrix} 8x_3/5\\ 5x_3/2\\ x_3\\ \end{pmatrix}\\ \\ \\ &\text {The Solution Set : }\\ \\ &\begin{Bmatrix} x_3&* \begin{pmatrix} 8/5\\ 5/2\\ 1 \end{pmatrix} \end{Bmatrix} \end{aligned} \]