In the two previous lessons we always computed "forward": given some coefficients, obtain a linear combination; given \(A\) and \(\mathbf{x}\), obtain \(A,\mathbf{x}\). But in a 3D engine the interesting questions usually run "backward": how hard must each thruster fire to head for the lighthouse? Where do two trajectories cross? What are the coordinates of this point in the ship's axes? All of these are systems of linear equations. In this lesson we will learn how to set them up, how to interpret them geometrically, how to solve them systematically and how to know in advance how many solutions they have.

What a system of linear equations is

A linear equation is an equation in which the unknowns appear only multiplied by numbers and added together: no \(x^2\), \(xy\), \(\sqrt{x}\) or \(\sin x\).

\[ 2x + 3y - z = 5 \quad \text{(linear)} \qquad\qquad x^2 + y = 1, \quad xy = 3 \quad \text{(nonlinear)} \]

A system of linear equations is a set of linear equations that must hold at the same time. A solution is a value for each unknown that satisfies all the equations simultaneously.

Example from the scene. We look at "Stellar Lighthouse" in the top-down view (from above, coordinates \((x, z)\)). The ship, which is at \((8, 6)\), follows a straight route whose points satisfy \(x + z = 14\). A cargo drone follows another straight route, \(x - 2z = 2\). At what point do the routes cross?

\[ \begin{cases} x + z = 14 \ x - 2z = 2 \end{cases} \]

Matrix form (A,\mathbf{x} = \mathbf{b})

Every linear system can be written compactly with one matrix and two vectors:

\[ \underbrace{\begin{pmatrix} 1 & 1 \ 1 & -2 \end{pmatrix}}{A} \underbrace{\begin{pmatrix} x \ z \end{pmatrix}}{\mathbf{x}}

\underbrace{\begin{pmatrix} 14 \ 2 \end{pmatrix}}_{\mathbf{b}} \]

  • \(A\) is the coefficient matrix (one row per equation, one column per unknown).
  • \(\mathbf{x}\) is the vector of unknowns.
  • \(\mathbf{b}\) is the vector of constant terms (the right-hand side).

If you carry out the product \(A,\mathbf{x}\) with the row × column rule from the previous lesson, you recover exactly the two equations. If \(\mathbf{b} = \mathbf{0}\) the system is called homogeneous, and it always has at least the solution \(\mathbf{x} = \mathbf{0}\).

Geometric interpretation: intersecting lines and planes

The same system admits two geometric readings, which correspond to the two ways of reading the matrix-vector product.

Reading by rows: intersection of shapes

Each equation describes a shape, and the solution is the place where they all meet.

  • In 2D, each equation \(ax + bz = c\) is a line.
  • In 3D, each equation \(ax + by + cz = d\) is a plane.
In 2D, two lines can... Solutions
Cross at a point One
Be parallel None
Be the same line Infinitely many

In 3D, three planes can meet at a single point (like two walls and the floor at the corner of a room), along a line (like the pages of an open book), coincide, or have no point common to all three.

Reading by columns: linear combination

Recalling that \(A,\mathbf{x}\) is a linear combination of the columns of \(A\), solving \(A,\mathbf{x} = \mathbf{b}\) means asking: which coefficients must we give the columns of \(A\) to obtain \(\mathbf{b}\)?

\[ x \begin{pmatrix} 1 \ 1 \end{pmatrix} + z \begin{pmatrix} 1 \ -2 \end{pmatrix} = \begin{pmatrix} 14 \ 2 \end{pmatrix} \]

This is the reading that connects with the first lesson: finding the coordinates of a vector with respect to a basis is solving a system.

Elementary methods: substitution and elimination

Substitution

Solve one equation for one unknown and substitute it into the others.

  1. From the first equation: \(x = 14 - z\).
  2. Substitute into the second: \((14 - z) - 2z = 2 \Rightarrow 14 - 3z = 2 \Rightarrow z = 4\).
  3. Go back: \(x = 14 - 4 = 10\).

The routes cross at \((x, z) = (10, 4)\). Check: \(10 + 4 = 14\) ✓ and \(10 - 2 \cdot 4 = 2\) ✓.

Elimination (or the addition method)

Add or subtract equations, multiplied by suitable numbers, so that one unknown disappears. Subtracting the second equation from the first:

\[ (x + z) - (x - 2z) = 14 - 2 ;\Longrightarrow; 3z = 12 ;\Longrightarrow; z = 4 \]

and from there \(x = 10\). Elimination is the seed of the general method: applied in an orderly way, it becomes Gaussian elimination.

Gaussian elimination with the augmented matrix

The augmented matrix

When eliminating, the letters of the unknowns do no work at all; the only things that change are the numbers. So we store them in a single table, the augmented matrix \((A \mid \mathbf{b})\): the coefficient matrix with the vector \(\mathbf{b}\) appended as its last column.

Elementary row operations

There are three operations that turn a system into another one with exactly the same solutions:

Operation Notation Equivalent to...
Swapping two rows \(R_i \leftrightarrow R_j\) Changing the order of the equations
Multiplying a row by a nonzero number \(R_i \leftarrow k,R_i\) Multiplying an equation by \(k\)
Adding a multiple of one row to another \(R_i \leftarrow R_i + k,R_j\) Elimination

The algorithm

The goal is to reach a matrix in row echelon form (upper triangular): zeros below the diagonal. We proceed column by column, from left to right:

  1. Choose the pivot: the diagonal entry of the current column. It must be nonzero; if it is zero, swap the row with a lower one whose entry is not.
  2. Create zeros below the pivot by subtracting the right multiple of the pivot row from each lower row.
  3. Move on to the next column and repeat.

Once the matrix is triangular, the last equation has a single unknown. Solve it and keep substituting upward: this is back substitution.

Example: the lighthouse pulses

The lighthouse emits a sequence with three types of light pulses: short (\(x\)), medium (\(y\)) and long (\(z\)). From the sequence log we know that:

  • there were 10 pulses in total;
  • each short pulse uses 2 units of energy, each medium one 3 and each long one 5, and 31 were used;
  • the pulses last 1, 3 and 2 seconds respectively, and the sequence lasted 22 seconds.

\[ \begin{cases} x + y + z = 10 \ 2x + 3y + 5z = 31 \ x + 3y + 2z = 22 \end{cases} \qquad \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 2 & 3 & 5 & 31 \ 1 & 3 & 2 & 22 \end{array}\right) \]

Column 1. Pivot: the 1 in the first row. We create zeros below it with \(R_2 \leftarrow R_2 - 2R_1\) and \(R_3 \leftarrow R_3 - R_1\):

\[ \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 0 & 1 & 3 & 11 \ 0 & 2 & 1 & 12 \end{array}\right) \]

Column 2. Pivot: the 1 in the second row. We create a zero below it with \(R_3 \leftarrow R_3 - 2R_2\):

\[ \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 0 & 1 & 3 & 11 \ 0 & 0 & -5 & -10 \end{array}\right) \]

Back substitution, from the bottom up:

  • Row 3: \(-5z = -10 \Rightarrow z = 2\).
  • Row 2: \(y + 3z = 11 \Rightarrow y = 11 - 6 = 5\).
  • Row 1: \(x + y + z = 10 \Rightarrow x = 10 - 5 - 2 = 3\).

Solution: 3 short pulses, 5 medium and 2 long. Check in the second equation: \(2 \cdot 3 + 3 \cdot 5 + 5 \cdot 2 = 31\) ✓; in the third: \(3 + 15 + 4 = 22\) ✓.

The Gauss-Jordan method

Gauss-Jordan picks up where Gauss leaves off: instead of substituting upward, it keeps applying row operations until the left-hand part of the augmented matrix is the identity. The solution then appears directly in the last column.

We start from the echelon matrix above.

Pivots to 1. \(R_3 \leftarrow R_3 / (-5)\):

\[ \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 0 & 1 & 3 & 11 \ 0 & 0 & 1 & 2 \end{array}\right) \]

Zeros above the third pivot. \(R_2 \leftarrow R_2 - 3R_3\) and \(R_1 \leftarrow R_1 - R_3\):

\[ \left(\begin{array}{ccc|c} 1 & 1 & 0 & 8 \ 0 & 1 & 0 & 5 \ 0 & 0 & 1 & 2 \end{array}\right) \]

Zero above the second pivot. \(R_1 \leftarrow R_1 - R_2\):

\[ \left(\begin{array}{ccc|c} 1 & 0 & 0 & 3 \ 0 & 1 & 0 & 5 \ 0 & 0 & 1 & 2 \end{array}\right) \]

The matrix says, literally, \(x = 3\), \(y = 5\), \(z = 2\).

Gauss Gauss-Jordan
Final form Upper triangular Identity
Final step Back substitution None: just read off the solution
Work Slightly less Slightly more
Typical use Solving a system Hand calculations and finding the inverse matrix (next lesson)

Cramer's rule

When the system has as many equations as unknowns and \(\det(A) \ne 0\), the determinants from the previous lesson provide a direct formula. Each unknown is a quotient of determinants:

\[ x_i = \frac{\det(A_i)}{\det(A)} \]

where \(A_i\) is the matrix \(A\) with its column \(i\) replaced by \(\mathbf{b}\).

Example: the route crossing.

\[ \det(A) = \det \begin{pmatrix} 1 & 1 \ 1 & -2 \end{pmatrix} = -2 - 1 = -3 \]

\[ x = \frac{\det \begin{pmatrix} 14 & 1 \ 2 & -2 \end{pmatrix}}{-3} = \frac{-28 - 2}{-3} = 10 \qquad z = \frac{\det \begin{pmatrix} 1 & 14 \ 1 & 2 \end{pmatrix}}{-3} = \frac{2 - 14}{-3} = 4 \]

The same result, \((10, 4)\).

Cramer's rule is elegant and practical for 2×2 and 3×3 systems solved by hand or with closed-form formulas (it will turn up, in disguise, in the intersection calculations of Module 3). For large systems it is very inefficient: it requires computing \(n + 1\) determinants. It also lays bare a fundamental fact: if \(\det(A) = 0\), the formula divides by zero and there cannot be a unique solution.

Classifying systems: rank and determinant

The three cases

Type Solutions In 2D
Consistent with a unique solution Exactly one Lines that cross
Consistent with infinitely many solutions Infinitely many The same line
Inconsistent None Parallel lines

There are no other possibilities: a linear system never has, say, exactly two solutions.

How to recognize them during elimination

When you apply Gauss, the "odd" cases give themselves away:

  • A row \((0 ;; 0 ;; 0 \mid c)\) with \(c \ne 0\) means \(0 = c\): a contradiction. The system is inconsistent.
  • A row \((0 ;; 0 ;; 0 \mid 0)\) means \(0 = 0\): that equation was redundant. If there are no contradictions and fewer pivots than unknowns remain, the system is consistent with infinitely many solutions, and the unknowns without a pivot become free parameters.

Examples with the ship's route \(x + z = 14\):

  • With \(2x + 2z = 28\): \(R_2 \leftarrow R_2 - 2R_1\) gives \((0 ;; 0 \mid 0)\). It is the same line written twice; the solutions are all the points \((14 - t,\ t)\), with \(t\) free.
  • With \(x + z = 10\): \(R_2 \leftarrow R_2 - R_1\) gives \((0 ;; 0 \mid -4)\). Parallel lines; there is no solution.

The rank

The rank of a matrix is the number of pivots left after reducing it to echelon form or, equivalently, the number of linearly independent columns (or rows). It is the function np.linalg.matrix_rank that we used as a black box in the first lesson.

By comparing the rank of \(A\) with that of the augmented matrix \((A \mid \mathbf{b})\) and with the number of unknowns \(n\), any system can be classified (the Rouché–Frobenius theorem, also known as Rouché–Capelli):

Condition Type of system
\(\text{rank}(A) < \text{rank}(A \mid \mathbf{b})\) Inconsistent
\(\text{rank}(A) = \text{rank}(A \mid \mathbf{b}) = n\) Consistent with a unique solution
\(\text{rank}(A) = \text{rank}(A \mid \mathbf{b}) < n\) Consistent with infinitely many solutions

The intuition by columns: if appending \(\mathbf{b}\) raises the rank, then \(\mathbf{b}\) brings in a new direction and is therefore not a linear combination of the columns of \(A\): there is no solution.

Relationship with the determinant

For square systems (as many equations as unknowns):

\[ \det(A) \ne 0 \iff \text{rank}(A) = n \iff \text{the system has a unique solution for any } \mathbf{b} \]

If \(\det(A) = 0\), the columns are dependent, space "gets flattened" and the system will be either inconsistent or have infinitely many solutions depending on what \(\mathbf{b}\) is; the determinant alone cannot tell the two apart.

Systems in NumPy and in mini3d

np.linalg.solve

In practice, square systems are solved with np.linalg.solve(a, b), which internally applies an optimized variant of Gaussian elimination.

import numpy as np

def solve_system(a, b):
    """Solve A x = b for a square A with a unique solution."""
    return np.linalg.solve(np.asarray(a, dtype=float), np.asarray(b, dtype=float))

# Route crossing in the top-down view
print(solve_system([[1, 1], [1, -2]], [14, 2]))

# Lighthouse pulses
a = np.array([[1.0, 1.0, 1.0],
              [2.0, 3.0, 5.0],
              [1.0, 3.0, 2.0]])
b = np.array([10.0, 31.0, 22.0])
print(solve_system(a, b))
[10.  4.]
[3. 5. 2.]

If the matrix is singular (\(\det = 0\)), solve raises the np.linalg.LinAlgError exception.

Gaussian elimination step by step

To show that there is no magic involved, here is a teaching version of the algorithm. It includes partial pivoting: in each column it picks as the pivot the entry with the largest absolute value, which avoids zero pivots and reduces rounding errors.

def gaussian_elimination(a, b):
    """Solve A x = b by Gaussian elimination with partial pivoting."""
    m = np.column_stack([a, b]).astype(float)    # augmented matrix
    n = len(b)
    for k in range(n):                           # k: pivot column
        pivot_row = k + np.argmax(np.abs(m[k:, k]))
        if np.isclose(m[pivot_row, k], 0.0):
            raise ValueError("The system has no unique solution")
        m[[k, pivot_row]] = m[[pivot_row, k]]    # row swap
        for i in range(k + 1, n):                # zeros below the pivot
            factor = m[i, k] / m[k, k]
            m[i, k:] -= factor * m[k, k:]
    x = np.zeros(n)
    for i in range(n - 1, -1, -1):               # back substitution
        x[i] = (m[i, -1] - m[i, i + 1:n] @ x[i + 1:]) / m[i, i]
    return x

print(gaussian_elimination(a, b))
[3. 5. 2.]
  • The first loop goes through the columns; np.argmax(np.abs(m[k:, k])) finds the best pivot from row k downward.
  • m[i, k:] -= factor * m[k, k:] is the operation \(R_i \leftarrow R_i - \text{factor} \cdot R_k\).
  • The last loop solves for the unknowns from the bottom up; m[i, i + 1:n] @ x[i + 1:] adds up the terms of the unknowns that are already known.

Classifying a system

def classify_system(a, b):
    """Classify A x = b by comparing ranks (Rouche-Frobenius)."""
    a = np.asarray(a, dtype=float)
    augmented = np.column_stack([a, b])
    rank_a = np.linalg.matrix_rank(a)
    rank_augmented = np.linalg.matrix_rank(augmented)
    if rank_a < rank_augmented:
        return "inconsistent"
    if rank_a == a.shape[1]:
        return "unique solution"
    return "infinitely many solutions"

print(classify_system([[1, 1], [1, -2]], [14, 2]))
print(classify_system([[1, 1], [2, 2]], [14, 28]))
print(classify_system([[1, 1], [1, 1]], [14, 10]))
unique solution
infinitely many solutions
inconsistent

Coordinates with respect to a basis

We can now automate what we did by hand in the first lesson. If the columns of a matrix \(B\) form a basis, the coordinates \(\mathbf{c}\) of a vector \(\mathbf{v}\) in that basis satisfy \(B,\mathbf{c} = \mathbf{v}\).

Example. The ship, at \((8, 3, 6)\), has three thrusters that push in the directions \(\mathbf{p}_1 = (1, 1, 0)\), \(\mathbf{p}_2 = (0, 1, 1)\) and \(\mathbf{p}_3 = (1, 1, 1)\). At what thrust level must each one fire to produce the displacement that takes it to the base of the lighthouse, \((-8, -3, -6)\)?

def coordinates_in_basis(basis, v):
    """Coordinates of v in the basis formed by the COLUMNS of 'basis'."""
    return np.linalg.solve(basis, v)

thrusters = np.column_stack([[1.0, 1.0, 0.0],
                             [0.0, 1.0, 1.0],
                             [1.0, 1.0, 1.0]])
toward_lighthouse = np.array([-8.0, -3.0, -6.0])

thrust_levels = coordinates_in_basis(thrusters, toward_lighthouse)
print(thrust_levels)
print(thrusters @ thrust_levels)       # check
[  3.   5. -11.]
[-8. -3. -6.]

The first must fire at thrust level 3, the second at 5 and the third at −11 (that is, in reverse). The last line checks the result by computing "forward".

Common Mistakes and Tips

  • Operating only on the left-hand part. Every row operation must be applied to the whole row of the augmented matrix, constant term included.
  • Dividing by a zero pivot. If the pivot is 0, swap rows. If the whole column is 0 from there down, the system does not have a unique solution.
  • Multiplying a row by zero. It is not a valid elementary operation: it destroys an equation and changes the solutions.
  • Misreading a row of zeros. \((0;0;0 \mid 0)\) indicates a redundant equation, not that the system has no solution; \((0;0;0 \mid c)\) with \(c \ne 0\) does.
  • Using Cramer for everything. It works well for 2×2 and 3×3; for larger sizes, use elimination.
  • Passing rows where columns belong. In coordinates_in_basis, the basis vectors are the columns of the matrix. np.array([p1, p2, p3]) would place them as rows; use np.column_stack.
  • Tip: always check the solution by substituting it back (a @ x must give b). It is cheap and catches almost any mistake.
  • Tip: to compare the result with the expected one, use np.allclose(a @ x, b) instead of ==.

Exercises

Exercise 1. In the top-down view, a moving beacon follows the line \(2x + z = 14\) and an asteroid follows the line \(x - z = 1\).

a) Find the crossing point by elimination. b) Confirm it with Cramer's rule.

Exercise 2. Polaris Studio defines an alternative basis for a sensor, made up of \(\mathbf{s}_1 = (1, 1, 0)\), \(\mathbf{s}_2 = (0, 1, 1)\) and \(\mathbf{s}_3 = (1, 0, 1)\). Use Gaussian elimination to compute the coordinates of the ship's position, \((8, 3, 6)\), in that basis, and check the result with coordinates_in_basis.

Exercise 3. The lighthouse log from the example has been corrupted and the third equation has changed. Classify each system and, where possible, solve it:

\[ \text{a)} \begin{cases} x + y + z = 10 \ 2x + 3y + 5z = 31 \ 3x + 4y + 6z = 41 \end{cases} \qquad\qquad \text{b)} \begin{cases} x + y + z = 10 \ 2x + 3y + 5z = 31 \ 3x + 4y + 6z = 40 \end{cases} \]

Solutions

Solution 1.

a) Adding the two equations makes \(z\) disappear: \((2x + z) + (x - z) = 14 + 1 \Rightarrow 3x = 15 \Rightarrow x = 5\). From the second, \(z = x - 1 = 4\). Crossing point: \((5, 4)\). Check: \(2 \cdot 5 + 4 = 14\) ✓.

b) \(\det(A) = \det \begin{pmatrix} 2 & 1 \ 1 & -1 \end{pmatrix} = -2 - 1 = -3\).

\[ x = \frac{\det \begin{pmatrix} 14 & 1 \ 1 & -1 \end{pmatrix}}{-3} = \frac{-14 - 1}{-3} = 5 \qquad z = \frac{\det \begin{pmatrix} 2 & 14 \ 1 & 1 \end{pmatrix}}{-3} = \frac{2 - 14}{-3} = 4 \]

Solution 2. We are looking for \(c_1, c_2, c_3\) with \(c_1,\mathbf{s}_1 + c_2,\mathbf{s}_2 + c_3,\mathbf{s}_3 = (8, 3, 6)\). The basis vectors are the columns:

\[ \left(\begin{array}{ccc|c} 1 & 0 & 1 & 8 \ 1 & 1 & 0 & 3 \ 0 & 1 & 1 & 6 \end{array}\right) \xrightarrow{R_2 \leftarrow R_2 - R_1} \left(\begin{array}{ccc|c} 1 & 0 & 1 & 8 \ 0 & 1 & -1 & -5 \ 0 & 1 & 1 & 6 \end{array}\right) \xrightarrow{R_3 \leftarrow R_3 - R_2} \left(\begin{array}{ccc|c} 1 & 0 & 1 & 8 \ 0 & 1 & -1 & -5 \ 0 & 0 & 2 & 11 \end{array}\right) \]

Back substitution: \(c_3 = 5.5\); \(c_2 = -5 + c_3 = 0.5\); \(c_1 = 8 - c_3 = 2.5\). Check: \(2.5,(1,1,0) + 0.5,(0,1,1) + 5.5,(1,0,1) = (8, 3, 6)\) ✓.

sensor = np.column_stack([[1.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 1.0]])
print(coordinates_in_basis(sensor, np.array([8.0, 3.0, 6.0])))
[2.5 0.5 5.5]

Typical mistake: writing the basis vectors as the rows of the matrix. The resulting system is a different one, and its solution is wrong.

Solution 3. The two systems share the first two steps: \(R_2 \leftarrow R_2 - 2R_1\) and \(R_3 \leftarrow R_3 - 3R_1\).

a)

\[ \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 0 & 1 & 3 & 11 \ 0 & 1 & 3 & 11 \end{array}\right) \xrightarrow{R_3 \leftarrow R_3 - R_2} \left(\begin{array}{ccc|c} 1 & 1 & 1 & 10 \ 0 & 1 & 3 & 11 \ 0 & 0 & 0 & 0 \end{array}\right) \]

A full row of zeros: the third equation was redundant (it is the sum of the first two). Rank 2 with 3 unknowns: consistent with infinitely many solutions. We take \(z = t\) as a free parameter: \(y = 11 - 3t\) and \(x = 10 - y - z = -1 + 2t\). Solutions: \((-1 + 2t,\ 11 - 3t,\ t)\). With \(t = 2\) we recover the original solution \((3, 5, 2)\). Geometrically, the three planes meet along a line.

b) The same steps give \((0 ;; 1 ;; 3 \mid 10)\) as the third row, and after \(R_3 \leftarrow R_3 - R_2\) we are left with \((0 ;; 0 ;; 0 \mid -1)\): \(0 = -1\). The system is inconsistent: the log contains contradictory data.

a2 = [[1, 1, 1], [2, 3, 5], [3, 4, 6]]
print(classify_system(a2, [10, 31, 41]))
print(classify_system(a2, [10, 31, 40]))
infinitely many solutions
inconsistent

In both cases \(\det(A) = 0\): the determinant warns that there is no unique solution, but it is the rank of the augmented matrix that distinguishes between infinitely many solutions and none.

Conclusion

A system of linear equations is written \(A,\mathbf{x} = \mathbf{b}\) and can be read as an intersection of lines or planes (by rows) or as the search for the coefficients of a linear combination (by columns). Gaussian elimination solves it systematically through elementary row operations on the augmented matrix; Gauss-Jordan carries the reduction all the way to the identity, and Cramer's rule offers a formula with determinants for small systems. The rank classifies the system as consistent with a unique solution, consistent with infinitely many solutions, or inconsistent, and in the square case \(\det(A) \ne 0\) guarantees a unique solution. mini3d now has solve_system, gaussian_elimination, classify_system and coordinates_in_basis.

One natural question remains: if \(A,\mathbf{x} = \mathbf{b}\) were an equation with numbers, we would solve it by dividing by \(A\). Is there anything like "dividing by a matrix"? Yes: it is the inverse matrix, and we will study it in the next lesson, "Inverse, Transpose and Orthogonal Matrices", where Gauss-Jordan will take center stage once again.

© Copyright 2026. All rights reserved