\[\begin{bmatrix} 1 & 2 & 3 & 4 \\ -1 & 0 & 1 & 3 \\ 0 & 1 & -2 & 1 \\ 5 & 4 & -2 & -3 \end{bmatrix}\] For this problem, I decided to implement a function to calculate the row reduced matrix and the rank to practice my coding skills. I implemented a generic function to calculate any \(`m\times n`\) matrix. I have three cases for ranking a matrix as following:
First, I developed a genertic function to get the echoln form of the matrix \(`get echoln`\) that takes a matrix of any size \(`m\times n`\).
Those were the edge cases we need to work on to built the function.
a_mEn <- matrix(c(1,2,3,4,
-1,0,1,3,
0,1,-2,1,
5,4,-2,-3), 4, byrow=T)
# a <- matrix(c(0,1,2,1,2,7,2,1,8), ncol = 3)
a_mEn
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] -1 0 1 3
## [3,] 0 1 -2 1
## [4,] 5 4 -2 -3
# m > n
a_mbn <- matrix(c(1,2,3,4,
-1,0,1,3), 4, byrow=T)
a_mbn
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] -1 0
## [4,] 1 3
# m < n
a_mln <- matrix(c(1,2,3,4,
-1,0,1,3), 2, byrow=T)
a_mln
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] -1 0 1 3
get_echoln <- function(a) {
U = a
n = ncol(a)
m = nrow(a)
if(m == n) {
for (i in 1:n) {
for (j in 2:m) {
if(U[j,i] != 0 & j > i) {
# Add multiples of the pivot row to each of the lower rows,
# so every element in the pivot column of the lower rows equals 0.
mplier = U[[j,i]]/U[[i,i]]
# reduce by reduction and subtitute in the U matrix
U[j,] = U[j,] - mplier * U[i,]
} else if (U[j,i] != 0 & j == i) {
U[j,] = U[j,] / U[[j,i]]
}# end if
} # end for
} # end for
} else if(m < n) {
for (i in 1:n) {
for (j in 2:m) {
if(U[j,i] != 0 & j > i) {
U[i,] = U[i,] / U[[i,i]]
mplier = U[[j,i]]/U[[i,i]]
U[j,] = U[j,] - mplier * U[i,]
} else if(U[j,i] != 0 & j == i) {
U[i,] = U[i,] / U[[i,i]]
} # end if
} # end for
} # end for
} else if (m > n) {
for (i in 1:n) {
for (j in 2:m) {
if(U[j,i] != 0 & j > i) {
U[i,] = U[i,] / U[[i,i]]
mplier = U[[j,i]]/U[[i,i]]
U[j,] = U[j,] - mplier * U[i,]
} else if(U[j,i] != 0 & j == i) {
U[i,] = U[i,] / U[[i,i]]
} # end if
} # end for
} # end for
} # end if
return(round(U, digits = 1))
}
equal = get_echoln(a_mEn)
equal
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4.0
## [2,] 0 1 2 3.5
## [3,] 0 0 1 0.6
## [4,] 0 0 0 1.0
# greater = get_echoln(a_mbn)
# greater
#
# lesser = get_echoln(a_mln)
# lesser
# rank needs to be modified
ranking = function(cd) {
rank = 0
# sol = as.array(colSums(cd))
# [1] 1 3 6 10
for (i in 1:nrow(cd)) {
if(sum(cd[i,]) > 0 & ncol(cd) == nrow(cd)) {
rank = rank + 1
} else if (sum(cd[i,]) > 0 & ncol(cd) > nrow(cd)) {
rank = rank + 1
# rank = max(nrow(cd), rank)
} else if (sum(cd[i,]) > 0 & ncol(cd) < nrow(cd)){
rank = rank + 1
# rank = max(ncol(cd), rank)
}
}
return(rank)
}
r1 = ranking(equal)
r1
## [1] 4
# r2 = ranking(greater)
# r2
#
# r3 = ranking(lesser)
# r3
If m is greater than n, then the maximum rank of the matrix is n (number of columns).
If m is less than n, then the maximum rank of the matrix is m (number of rows).
\[\begin{bmatrix} 1 & 2 & 1 \\ 3 & 6 & 3 \\ 2 & 4 & 2 \end{bmatrix}\]
B <- matrix(c(1,2,1,
3,6,3,
2,4,2), 3, byrow = T)
B
## [,1] [,2] [,3]
## [1,] 1 2 1
## [2,] 3 6 3
## [3,] 2 4 2
B_echoln = get_echoln(B)
B_echoln
## [,1] [,2] [,3]
## [1,] 1 2 1
## [2,] 0 0 0
## [3,] 0 0 0
B_rank = ranking(B_echoln)
B_rank
## [1] 1
Compute the eigenvalues and eigenvectors of the matrix A. You’ll need to show your work. You’ll need to write out the characteristic polynomial and show your solution.
\[\begin{bmatrix} 1 & 2 & 3 \\ 0 & 4 & 5 \\ 0 & 0 & 6 \end{bmatrix}\]
Steps to solution:-
\[ A = \begin{bmatrix}1 & 2 & 3 \\0 & 4 & 5 \\0 & 0 & 6 \end{bmatrix}\] \[ \lambda\,I_3 = \begin{bmatrix}\lambda & 0 & 0 \\0 & \lambda & 0 \\0 & 0 & \lambda \end{bmatrix}\] \[ det(A-\lambda\,I_n)=0\] \[ det\,\begin{bmatrix}1-\lambda & 2 & 3 \\0 & 4-\lambda & 5 \\0 & 0 & 6-\lambda \end{bmatrix} = 0\] \[(1-\lambda)(4-\lambda)(6-\lambda)=0\] \[ Eigenvalues\,of\,A:\] \[\lambda=1,\, \lambda=4,\, \lambda=6\]
Eigenvectors:
\[\lambda=1\] \[ \begin{bmatrix}1-\lambda & 2 & 3 \\0 & 4-\lambda & 5 \\0 & 0 & 6-\lambda \end{bmatrix}\]
\[ \begin{bmatrix}0 & 2 & 3 \\0 & 3 & 5\\0 & 0 & 5\end{bmatrix}\,\begin{bmatrix}v_1 \\v_2 \\v_3\end{bmatrix}=0\]
\[The\,first\,pivot\,is\,0.\,x_1 = free.\,Let\,the\,value=1.\]
\[3 x_2 + 5 x_3 = 0 \,and\, 5 x_3 = 0\]
\[x_{\lambda=1}\,=\begin{bmatrix}1 \\0 \\0\end{bmatrix}\]
\[\lambda=4\] \[ \begin{bmatrix}-3 & 2 & 3 \\0 & 0 & 5\\0 & 0 & 2\end{bmatrix}\,\begin{bmatrix}v_1 \\v_2 \\v_3\end{bmatrix}=0\] \[Second\,pivot\,is\, 0.\,x_2=free.\,Let\,the\,value=1.\] \[-3x_1+2x_2 +3x_3 = 0\,and\, 2x_3 = 0\] \[x_3=0,\,x_2=1\,and\,x_1=2/3\] \[x_{\lambda=4}\,=\begin{bmatrix}2/3\\1 \\0\end{bmatrix}\]
\[\lambda=6\] \[ \begin{bmatrix}-5 & 2 & 3\\0 & -2 & 5\\0 & 0 & 0\end{bmatrix}\,\begin{bmatrix}v_1 \\v_2 \\v_3\end{bmatrix}=0\]
\[Third\,pivot\,is\, 0.\,x_3=free.\,Let\,the\,value=1.\] \[-5x_1 +2x_2 +3x_3 = 0 and, -2x_2+5x_3 = 0\] \[x_3 = 1,\,x_2 = 5/2,\,and\,x_1=8/5\]
\[x_{\lambda=6}\,=\begin{bmatrix}8/5 \\5/2 \\1\end{bmatrix}\]
Confirm with buit-in function in r
A <- matrix(data = c(1,0,0,
2,4,0,
3,5,6), nrow = 3, ncol = 3, byrow = FALSE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 4 5
## [3,] 0 0 6
eign_value <- (eigen(A))
eign_value
## eigen() decomposition
## $values
## [1] 6 4 1
##
## $vectors
## [,1] [,2] [,3]
## [1,] 0.5108407 0.5547002 1
## [2,] 0.7981886 0.8320503 0
## [3,] 0.3192754 0.0000000 0
# eign_vectors <- (eigen(A))$vectors
# eign_vectors