Problems C29 and C25 from Chapter D

Calculating the determinant of a 2x2 matrix is quite easy, all you need to do is:

\[det = ad - bc\] However, calculating the determinant of a 3x3 matrix becomes a bit more tricky. I’ve selected problem C30 from the textbook “A First Course in Linear Algebra” to demonstrate this technique. We’ll perform the calculations manually, and then use the “det” function in base R to confirm our results. Let’s get started!

The problem reads as follows:

Doing the computations by hand, find the determinant of the matrix A.

\[A=\begin{vmatrix}2 & 3 & 0 & 2 & 1 \\ 0 & 1 & 1 & 1 & 2 \\ 0 & 0 & 1 & 2 & 3 \\ 0 & 1 & 2 & 1 & 0 \\ 0 & 0 & 0 & 1 & 2\end{vmatrix}\] In order to easily visualize the calculations we need to perform, I’m going to take the first 4 columns of the matrix and add them onto the end of our matrix. This enables us to see our diagonals much easier, which is my favorite approach to calculating determinants.

\[A=\begin{vmatrix}2 & 3 & 0 & 2 & 1 & 2 & 3 & 0 & 2\\ 0 & 1 & 1 & 1 & 2 & 0 & 1 & 1 & 1 \\ 0 & 0 & 1 & 2 & 3 & 0 & 0 & 1 & 2 \\ 0 & 1 & 2 & 1 & 0 & 0 & 1 & 2 & 1 \\ 0 & 0 & 0 & 1 & 2 & 0 & 0 & 0 & 1\end{vmatrix}\]

As you can see in the photo, we’ve created our blue and purple diagonal lines.

Our first step is to multiply the values in each blue line together. We’ll call these l1 through l5 in our code.

Then we want to multiply the values in each of our purple lines together. We’ll call these l6 through l10 in our code.

Next, we want to add l1-l5, but subtract l6-l10. This will give us our final determinant.

l1 <- 2*1*1*1*2
l2 <- 3*1*2*0*0
l3 <- 0*1*3*0*0
l4 <- 2*2*0*1*0
l5 <- 1*0*0*2*1

l6 <- 1*1*1*1*0
l7 <- 2*2*2*2*0
l8 <- 3*0*3*1*0
l9 <- 0*1*0*0*1
l10 <- 2*1*0*0*2

A.total <- l1 + l2 + l3 + l4 + l5 -l6 - l7 -l8 -l9 -l10

#C29
A <- matrix(c(2,0,0,0,0,3,1,0,1,0,0,1,1,2,0,2,1,2,1,1,1,2,3,0,2), nrow = 5, ncol = 5)
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    0    2    1
## [2,]    0    1    1    1    2
## [3,]    0    0    1    2    3
## [4,]    0    1    2    1    0
## [5,]    0    0    0    1    2
#Let's check our work
det(A)
## [1] 2
A.total
## [1] 4
#Try with a 3x3 matrix to see if the diagonal approach works for that
#C25
B <- matrix(c(3,2,2,-1,5,0,4,1,6), nrow = 3)

b1 <- 3*5*6
b2 <- -1*1*2
b3 <- 4*2*0
b4 <- 4*5*2
b5 <- 3*1*0
b6 <- -1*2*6

B.total <- b1 + b2 + b3 - b4 - b5 - b6

#Validate
B.total
## [1] 60
det(B)
## [1] 60

For some reason, with the 5x5 matrix from problem C29, I’m getting 4 in my calculations but the det() function is producing 2 as a solution. When I test the same approach with problem C25 from the text which is a 3x3 matrix, I produce 60 as a result which ties out with what the det() function provides as a solution.

I’m curious to know if there is a specific reason why this approach works on a 3x3 matrix, but not a 5x5 matrix. Either that, or I made a silly mistake. Either way, I’m very open to feedback so please let me know your thoughts!