# Nakafa Learning Content

> For AI agents: use [llms.txt](https://nakafa.com/llms.txt) for the site index. Markdown versions are available by appending `.md` to content URLs or sending `Accept: text/markdown`.

URL: https://nakafa.com/en/subjects/ai-ds/linear-methods/determinant-calculation
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/material/lesson/ai-ds/linear-methods/determinant-calculation/en.mdx

Learn determinant calculation with cofactor expansion, Gaussian elimination, and triangular matrices. Learn efficient methods for AI linear algebra.

---

## Determinant Calculation Methods

After understanding the [basic concepts of determinant](/en/subjects/ai-ds/linear-methods/determinant), we need to know how to calculate it practically. There are several methods that can be used depending on the form of the matrix we encounter.

For small matrices, we can use direct formulas. However, for larger matrices, we need more efficient strategies.

## Basic Case of an Order-One Matrix

For matrices of size $$1 \times 1$$, the determinant is very simple. If $$A \in \mathbb{R}^{1 \times 1}$$ with $$A = (a_{11})$$, then:

Visible text: For matrices of size , the determinant is very simple. If with , then:

```math
\det A = a_{11}
```

This is the most basic case that becomes the foundation for calculating determinants of larger matrices.

## Submatrix Concept

Before discussing the cofactor expansion method, we need to understand the concept of **submatrix**. For matrix $$A \in \mathbb{R}^{n \times n}$$ and indices $$i, j \in \{1, 2, \ldots, n\}$$, submatrix $$A_{ij}$$ is a matrix of size $$(n-1) \times (n-1)$$ obtained by removing row $$i$$ and column $$j$$ from matrix $$A$$.

Visible text: Before discussing the cofactor expansion method, we need to understand the concept of **submatrix**. For matrix and indices , submatrix is a matrix of size obtained by removing row and column from matrix .

Let's look at an example for a $$3 \times 3$$ matrix. Suppose we have:

Visible text: Let's look at an example for a matrix. Suppose we have:

```math
A = \begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{pmatrix}
```

To get submatrix $$A_{12}$$, we remove row $$1$$ and column $$2$$:

Visible text: To get submatrix , we remove row and column :

Component: MathContainer
Children:

```math
A_{12} = \begin{pmatrix} \cancel{a_{11}} & \cancel{a_{12}} & \cancel{a_{13}} \\ a_{21} & \cancel{a_{22}} & a_{23} \\ a_{31} & \cancel{a_{32}} & a_{33} \end{pmatrix}
```

```math
= \begin{pmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{pmatrix}
```

For submatrix $$A_{23}$$, we remove row $$2$$ and column $$3$$:

Visible text: For submatrix , we remove row and column :

Component: MathContainer
Children:

```math
A_{23} = \begin{pmatrix} a_{11} & a_{12} & \cancel{a_{13}} \\ \cancel{a_{21}} & \cancel{a_{22}} & \cancel{a_{23}} \\ a_{31} & a_{32} & \cancel{a_{33}} \end{pmatrix}
```

```math
= \begin{pmatrix} a_{11} & a_{12} \\ a_{31} & a_{32} \end{pmatrix}
```

This process applies to all combinations of rows and columns that are removed.

## Cofactor Expansion

The most common method for calculating determinants is cofactor expansion. For matrix $$A \in \mathbb{R}^{n \times n}$$ with $$n > 1$$, the determinant can be calculated using the formula:

Visible text: The most common method for calculating determinants is cofactor expansion. For matrix with , the determinant can be calculated using the formula:

Component: MathContainer
Children:

```math
\det A = \sum_{i=1}^{n} (-1)^{i+j} \cdot a_{ij} \cdot \det A_{ij}
```

for a fixed and freely chosen column $$j$$.

Visible text: for a fixed and freely chosen column .

In this formula, the term $$(-1)^{i+j} \cdot \det A_{ij}$$ is called the **cofactor** of element $$a_{ij}$$. The sign $$(-1)^{i+j}$$ provides a checkerboard pattern to determine positive or negative signs.

Visible text: In this formula, the term is called the **cofactor** of element . The sign provides a checkerboard pattern to determine positive or negative signs.

### Example of Third-Order Cofactor Expansion

Let's look at an example of cofactor expansion for a $$3 \times 3$$ matrix:

Visible text: Let's look at an example of cofactor expansion for a matrix:

```math
A = \begin{pmatrix} 2 & 1 & 3 \\ 0 & 4 & 1 \\ 1 & 2 & 0 \end{pmatrix}
```

We choose the first row for expansion:

Component: MathContainer
Children:

```math
\det A = 2 \cdot (-1)^{1+1} \det A_{11} + 1 \cdot (-1)^{1+2} \det A_{12} + 3 \cdot (-1)^{1+3} \det A_{13}
```

```math
= 2 \cdot \det \begin{pmatrix} 4 & 1 \\ 2 & 0 \end{pmatrix} - 1 \cdot \det \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} + 3 \cdot \det \begin{pmatrix} 0 & 4 \\ 1 & 2 \end{pmatrix}
```

```math
= 2 \cdot (4 \cdot 0 - 1 \cdot 2) - 1 \cdot (0 \cdot 0 - 1 \cdot 1) + 3 \cdot (0 \cdot 2 - 4 \cdot 1)
```

```math
= 2 \cdot (-2) - 1 \cdot (-1) + 3 \cdot (-4) = -4 + 1 - 12 = -15
```

We can perform expansion based on any row or column. Usually we choose a row or column that has many zeros to simplify the calculation.

## Triangular and Diagonal Matrices

For some special types of matrices, determinant calculation becomes very simple:

### Upper Triangular Matrix

For upper triangular matrix $$R$$:

Visible text: For upper triangular matrix :

Component: MathContainer
Children:

```math
R = \begin{pmatrix} r_{11} & * & \cdots & * \\ 0 & r_{22} & \cdots & * \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & r_{nn} \end{pmatrix}
```

Its determinant is:

Component: MathContainer
Children:

```math
\det R = r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}
```

### Lower Triangular Matrix

For lower triangular matrix $$L$$:

Visible text: For lower triangular matrix :

Component: MathContainer
Children:

```math
L = \begin{pmatrix} l_{11} & 0 & \cdots & 0 \\ * & l_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ * & * & \cdots & l_{nn} \end{pmatrix}
```

Its determinant is:

Component: MathContainer
Children:

```math
\det L = l_{11} \cdot l_{22} \cdot \ldots \cdot l_{nn}
```

### Diagonal Matrix

For diagonal matrix $$D$$:

Visible text: For diagonal matrix :

Component: MathContainer
Children:

```math
D = \begin{pmatrix} d_{11} & 0 & \cdots & 0 \\ 0 & d_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & d_{nn} \end{pmatrix}
```

Its determinant is:

Component: MathContainer
Children:

```math
\det D = d_{11} \cdot d_{22} \cdot \ldots \cdot d_{nn}
```

> For all three types of matrices, the determinant equals the product of all main diagonal elements.

## Elementary Matrices

Elementary matrices are matrices obtained from the identity matrix with one elementary row operation. The determinants of elementary matrices have values that are easy to calculate:

1. **Scalar matrix** $$S_i(\lambda)$$ that multiplies row i by $$\lambda$$:

    
   
   ```math
   \det S_i(\lambda) = \lambda
   ```

2. **Permutation matrix** $$Q_i^j(\lambda)$$ that swaps row i and j:

    
   
   ```math
   \det Q_i^j(\lambda) = 1
   ```

3. **Transvection matrix** $$P_i^j$$ that adds a multiple of row $$j$$ to row $$i$$:

    
   
   ```math
   \det P_i^j = -1
   ```

Visible text: 1. **Scalar matrix** that multiplies row i by :

 
 

2. **Permutation matrix** that swaps row i and j:

 
 

3. **Transvection matrix** that adds a multiple of row to row :

Note that permutation matrices have determinant $$1$$, not $$-1$$ as often mistaken. The negative sign appears when we **perform** row swap operations on other matrices.

Visible text: Note that permutation matrices have determinant , not as often mistaken. The negative sign appears when we **perform** row swap operations on other matrices.

## Gaussian Elimination for Determinant Calculation

One of the most efficient methods for calculating determinants is using Gaussian elimination. The process is to transform the matrix into upper triangular form, then multiply the diagonal elements.

When matrix $$A$$ is transformed into upper triangular form $$R$$ through Gaussian elimination, we need to count how many row swaps are performed. If there are $$p$$ row swaps, then:

Visible text: When matrix is transformed into upper triangular form through Gaussian elimination, we need to count how many row swaps are performed. If there are row swaps, then:

```math
\det A = (-1)^p \cdot \det R
```

Since $$R$$ is an upper triangular matrix:

Visible text: Since is an upper triangular matrix:

```math
\det R = r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}
```

Therefore:

Component: MathContainer
Children:

```math
\det A = (-1)^p \cdot r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}
```

### Method Efficiency

Gaussian elimination has time complexity $$O(\frac{1}{3}n^3)$$, which is much more efficient compared to cofactor expansion which has complexity $$O(n!)$$.

Visible text: Gaussian elimination has time complexity , which is much more efficient compared to cofactor expansion which has complexity .

For large unstructured matrices, Gaussian elimination is the most practical and reliable method.

## Complete Calculation Example

Let's look at an example of determinant calculation using Gaussian elimination:

```math
A = \begin{pmatrix} 0 & 3 & 3 \\ 2 & 2 & 0 \\ 1 & 0 & 1 \end{pmatrix}
```

**Step** $$1$$: Swap rows $$1$$ and $$3$$ to get a non-zero pivot:

Visible text: **Step** : Swap rows and to get a non-zero pivot:

```math
\begin{pmatrix} 1 & 0 & 1 \\ 2 & 2 & 0 \\ 0 & 3 & 3 \end{pmatrix}
```

**Step $$2$$**: Eliminate the first column by subtracting $$2$$ times row $$1$$ from row $$2$$:

Visible text: **Step **: Eliminate the first column by subtracting times row from row :

```math
\begin{pmatrix} 1 & 0 & 1 \\ 0 & 2 & -2 \\ 0 & 3 & 3 \end{pmatrix}
```

**Step $$3$$**: Eliminate the second column by subtracting $$1.5$$ times row $$2$$ from row $$3$$:

Visible text: **Step **: Eliminate the second column by subtracting times row from row :

```math
\begin{pmatrix} 1 & 0 & 1 \\ 0 & 2 & -2 \\ 0 & 0 & 6 \end{pmatrix} = R
```

Since there is one row swap ($$p = 1$$):

Visible text: Since there is one row swap ():

Component: MathContainer
Children:

```math
\det A = (-1)^1 \cdot \det R
```

```math
= (-1)^1 \cdot 1 \cdot 2 \cdot 6
```

```math
= -12
```