## Example

Doing the computations by hand, find the determinant of the matrix A (Question C30, Page 354).

For this problem, I will reduce the matrix to a triangular matrix and use the diagonal to find the determinant.

### Matrix A

A <- rbind(c(2,1,1,0,1),c(2,1,2,-1,1),c(0,0,1,2,0),c(1,0,3,1,1),c(2,1,1,2,1))
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    1    1    0    1
## [2,]    2    1    2   -1    1
## [3,]    0    0    1    2    0
## [4,]    1    0    3    1    1
## [5,]    2    1    1    2    1

### Step 1 - Subtract Row 1 from Row 5

A[5,] <- A[5,] - A[1,]
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    1    1    0    1
## [2,]    2    1    2   -1    1
## [3,]    0    0    1    2    0
## [4,]    1    0    3    1    1
## [5,]    0    0    0    2    0

### Step 2 - Subtract Row 1 from Row 2

A[2,] <- A[2,] - A[1,]
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    1    1    0    1
## [2,]    0    0    1   -1    0
## [3,]    0    0    1    2    0
## [4,]    1    0    3    1    1
## [5,]    0    0    0    2    0

### Step 3 - Subtract Row 2 from Row 3

A[3,] <- A[3,] - A[2,]
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    1    1    0    1
## [2,]    0    0    1   -1    0
## [3,]    0    0    0    3    0
## [4,]    1    0    3    1    1
## [5,]    0    0    0    2    0

### Step 4 - Subtract 2 * Row 4 from Row 1

In this step, we see that rows 3 and 5 of this matrix are linearly dependent. This confirms that the determinant of the matrix is zero, but we will continue with row operations until the matrix is triangular.

A[1,] <- A[1,] - (2 * A[4,])
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1   -5   -2   -1
## [2,]    0    0    1   -1    0
## [3,]    0    0    0    3    0
## [4,]    1    0    3    1    1
## [5,]    0    0    0    2    0

### Step 5 - Subtract 1.5 * Row 5 from Row 3

A[3,] <- A[3,] - (1.5 * A[5,])
print(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1   -5   -2   -1
## [2,]    0    0    1   -1    0
## [3,]    0    0    0    0    0
## [4,]    1    0    3    1    1
## [5,]    0    0    0    2    0

### Step 6 - Flip Row Order

Here, we flip the order of the rows to obtain a triangular matrix. Since diagonal contains a zero, this confirms that the determinant of this 5x5 matrix is zero.

B = matrix(nrow=5, ncol=5)
B[1,] <- A[4,]
B[2,] <- A[1,]
B[3,] <- A[2,]
B[4,] <- A[5,]
B[5,] <- A[3,]
print(B)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    3    1    1
## [2,]    0    1   -5   -2   -1
## [3,]    0    0    1   -1    0
## [4,]    0    0    0    2    0
## [5,]    0    0    0    0    0

### Confirmation

The determinant of the matrix is 0 (confirmed using det).

det(rbind(c(2,1,1,0,1),c(2,1,2,-1,1),c(0,0,1,2,0),c(1,0,3,1,1),c(2,1,1,2,1)))
## [1] 0