# Nakafa Framework: LLM

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

Output docs content for large language models.

---

export const metadata = {
    title: "Determinant Calculation",
    description: "Master determinant calculation with cofactor expansion, Gaussian elimination, and triangular matrices. Learn efficient methods for AI linear algebra.",
    authors: [{ name: "Nabil Akbarazzima Fatih" }],
    date: "07/10/2025",
    subject: "Linear Methods of AI",
};

## Determinant Calculation Methods

After understanding the [basic concepts of determinant](/subject/university/bachelor/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 1×1 Matrix

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

<BlockMath 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 <InlineMath math="A \in \mathbb{R}^{n \times n}" /> and indices <InlineMath math="i, j \in \{1, 2, \ldots, n\}" />, submatrix <InlineMath math="A_{ij}" /> is a matrix of size <InlineMath math="(n-1) \times (n-1)" /> obtained by removing row <InlineMath math="i" /> and column <InlineMath math="j" /> from matrix <InlineMath math="A" />.

Let's look at an example for a <InlineMath math="3 \times 3" /> matrix. Suppose we have:

<BlockMath 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 <InlineMath math="A_{12}" />, we remove row 1 and column 2:

<MathContainer>
<BlockMath 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}" />
<BlockMath math="= \begin{pmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{pmatrix}" />
</MathContainer>

For submatrix <InlineMath math="A_{23}" />, we remove row 2 and column 3:

<MathContainer>
<BlockMath 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}" />
<BlockMath math="= \begin{pmatrix} a_{11} & a_{12} \\ a_{31} & a_{32} \end{pmatrix}" />
</MathContainer>

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 <InlineMath math="A \in \mathbb{R}^{n \times n}" /> with <InlineMath math="n > 1" />, the determinant can be calculated using the formula:

<MathContainer>
<BlockMath math="\det A = \sum_{i=1}^{n} (-1)^{i+j} \cdot a_{ij} \cdot \det A_{ij}" />
</MathContainer>

for a fixed and freely chosen column <InlineMath math="j" />.

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

### Example of 3×3 Cofactor Expansion

Let's look at an example of cofactor expansion for a 3×3 matrix:

<BlockMath math="A = \begin{pmatrix} 2 & 1 & 3 \\ 0 & 4 & 1 \\ 1 & 2 & 0 \end{pmatrix}" />

We choose the first row for expansion:

<MathContainer>
<BlockMath 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}" />
<BlockMath 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}" />
<BlockMath 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)" />
<BlockMath math="= 2 \cdot (-2) - 1 \cdot (-1) + 3 \cdot (-4) = -4 + 1 - 12 = -15" />
</MathContainer>

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 <InlineMath math="R" />:

<MathContainer>
<BlockMath math="R = \begin{pmatrix} r_{11} & * & \cdots & * \\ 0 & r_{22} & \cdots & * \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & r_{nn} \end{pmatrix}" />
</MathContainer>

Its determinant is:

<MathContainer>
<BlockMath math="\det R = r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}" />
</MathContainer>

### Lower Triangular Matrix

For lower triangular matrix <InlineMath math="L" />:

<MathContainer>
<BlockMath math="L = \begin{pmatrix} l_{11} & 0 & \cdots & 0 \\ * & l_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ * & * & \cdots & l_{nn} \end{pmatrix}" />
</MathContainer>

Its determinant is:

<MathContainer>
<BlockMath math="\det L = l_{11} \cdot l_{22} \cdot \ldots \cdot l_{nn}" />
</MathContainer>

### Diagonal Matrix

For diagonal matrix <InlineMath math="D" />:

<MathContainer>
<BlockMath 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}" />
</MathContainer>

Its determinant is:

<MathContainer>
<BlockMath math="\det D = d_{11} \cdot d_{22} \cdot \ldots \cdot d_{nn}" />
</MathContainer>

> 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** <InlineMath math="S_i(\lambda)" /> that multiplies row i by <InlineMath math="\lambda" />:

    <BlockMath math="\det S_i(\lambda) = \lambda" />

2. **Permutation matrix** <InlineMath math="Q_i^j(\lambda)" /> that swaps row i and j:

    <BlockMath math="\det Q_i^j(\lambda) = 1" />

3. **Transvection matrix** <InlineMath math="P_i^j" /> that adds a multiple of row j to row i:

    <BlockMath math="\det P_i^j = -1" />

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.

## 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 <InlineMath math="A" /> is transformed into upper triangular form <InlineMath math="R" /> through Gaussian elimination, we need to count how many row swaps are performed. If there are <InlineMath math="p" /> row swaps, then:

<BlockMath math="\det A = (-1)^p \cdot \det R" />

Since <InlineMath math="R" /> is an upper triangular matrix:

<BlockMath math="\det R = r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}" />

Therefore:

<MathContainer>
<BlockMath math="\det A = (-1)^p \cdot r_{11} \cdot r_{22} \cdot \ldots \cdot r_{nn}" />
</MathContainer>

### Method Efficiency

Gaussian elimination has time complexity <InlineMath math="O(\frac{1}{3}n^3)" />, which is much more efficient compared to cofactor expansion which has complexity <InlineMath math="O(n!)" />.

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:

<BlockMath 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:

<BlockMath 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:

<BlockMath 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:

<BlockMath math="\begin{pmatrix} 1 & 0 & 1 \\ 0 & 2 & -2 \\ 0 & 0 & 6 \end{pmatrix} = R" />

Since there is one row swap (<InlineMath math="p = 1" />):

<MathContainer>
<BlockMath math="\det A = (-1)^1 \cdot \det R" />
<BlockMath math="= (-1)^1 \cdot 1 \cdot 2 \cdot 6" />
<BlockMath math="= -12" />
</MathContainer>