Skip to main content

Introduction

A system of linear equations (or linear system) is a collection of one or more linear equations involving the same variables. By representing these systems as matrices, you can leverage NumPy’s efficient computational tools to find solutions quickly.
This guide demonstrates how to model systems of linear equations as matrices, solve them using NumPy, and understand the relationship between matrix properties and solution existence.

Setup

Load the required packages:
import numpy as np
import matplotlib.pyplot as plt

Understanding Linear Systems

What is a Linear System?

Consider this example system: {x1+3x2=7,3x1+2x2=1,\begin{cases} -x_1+3x_2=7, \\ 3x_1+2x_2=1, \end{cases} This system has:
  • Two equations
  • Two unknown variables: x1x_1 and x2x_2
  • A solution is values for x1x_1 and x2x_2 that satisfy both equations simultaneously
A linear system is singular if it has no unique solution (either no solution or infinitely many solutions). Otherwise, it is non-singular.

Matrix Representation

Full System Matrix

The complete system can be represented as an augmented matrix: [137321]\begin{bmatrix} -1 & 3 & 7 \\ 3 & 2 & 1 \end{bmatrix} Each row represents one equation. Columns represent:
  • Column 1: Coefficients of x1x_1
  • Column 2: Coefficients of x2x_2
  • Column 3: Constants (right side of equations)

Coefficient Matrix and Vector

We can separate the system into:
A=[1332]A = \begin{bmatrix} -1 & 3\\ 3 & 2 \end{bmatrix}

Creating Matrices in NumPy

# Coefficient matrix
A = np.array([
    [-1, 3],
    [3, 2]
], dtype=np.dtype(float))

# Constant vector
b = np.array([7, 1], dtype=np.dtype(float))

print("Matrix A:")
print(A)
print("\nArray b:")
print(b)

Checking Dimensions

print(f"Shape of A: {A.shape}")  # Output: (2, 2)
print(f"Shape of b: {b.shape}")  # Output: (2,)
Use the .shape attribute or np.shape() function to verify matrix dimensions before performing operations.

Solving Linear Systems

Using np.linalg.solve()

NumPy’s linear algebra package provides a fast, reliable solver:
# Solve the system
x = np.linalg.solve(A, b)

print(f"Solution: {x}")
# Output: [-1.  2.]
This tells us that x1=1x_1 = -1 and x2=2x_2 = 2.
1

Define Coefficient Matrix

Create matrix AA with equation coefficients
2

Define Constant Vector

Create vector bb with right-hand side values
3

Call np.linalg.solve()

Pass AA and bb to get solution vector xx
4

Verify Solution

Substitute values back into original equations

Verification

Substitute x1=1x_1 = -1 and x2=2x_2 = 2 into the original equations:
  • Equation 1: (1)+3(2)=1+6=7-(-1) + 3(2) = 1 + 6 = 7
  • Equation 2: 3(1)+2(2)=3+4=13(-1) + 2(2) = -3 + 4 = 1

Matrix Determinants

Understanding Determinants

For square matrices, the determinant is a scalar value that characterizes matrix properties.
A linear system has one unique solution if and only if the coefficient matrix has a non-zero determinant.

Calculating Determinants

d = np.linalg.det(A)

print(f"Determinant of matrix A: {d:.2f}")
# Output: Determinant of matrix A: -11.00
Since d0d \neq 0, the system has a unique solution.

Visualizing Linear Systems

Creating the Augmented Matrix

# Horizontally stack A and b
A_system = np.hstack((A, b.reshape((2, 1))))

print(A_system)
# Output:
# [[-1.  3.  7.]
#  [ 3.  2.  1.]]
Use .reshape((2, 1)) to convert the 1-D vector into a 2-D column vector before stacking.

Extracting Rows

# Extract second row (indexing starts at 0)
print(A_system[1])
# Output: [3. 2. 1.]

Plotting the Solution

Each equation represents a line in 2D space. The solution is their intersection point:
# Assuming you have a plot_lines function
plot_lines(A_system)
The lines intersect at (x1,x2)=(1,2)(x_1, x_2) = (-1, 2), confirming our solution.

Systems with No Solutions

Example: Parallel Lines

Consider the system: {x1+3x2=7,3x19x2=1,\begin{cases} -x_1+3x_2=7, \\ 3x_1-9x_2=1, \end{cases}
A_2 = np.array([
    [-1, 3],
    [3, -9]
], dtype=np.dtype(float))

b_2 = np.array([7, 1], dtype=np.dtype(float))

# Check determinant
d_2 = np.linalg.det(A_2)
print(f"Determinant of matrix A_2: {d_2:.2f}")
# Output: Determinant of matrix A_2: 0.00
When the determinant equals zero, the system is singular and has either no solution or infinitely many solutions.

Attempting to Solve

try:
    x_2 = np.linalg.solve(A_2, b_2)
except np.linalg.LinAlgError as err:
    print(err)
    # Output: Singular matrix

Visualization

A_2_system = np.hstack((A_2, b_2.reshape((2, 1))))
plot_lines(A_2_system)
The lines are parallel and never intersect - no solution exists.

Systems with Infinite Solutions

Example: Coincident Lines

Modify the system to make the equations proportional: {x1+3x2=7,3x19x2=21,\begin{cases} -x_1+3x_2=7, \\ 3x_1-9x_2=-21, \end{cases}
b_3 = np.array([7, -21], dtype=np.dtype(float))
A_3_system = np.hstack((A_2, b_3.reshape((2, 1))))

print(A_3_system)
# Output:
# [[ -1.   3.   7.]
#  [  3.  -9. -21.]]

Understanding the Solution

The second equation is just 3-3 times the first equation. This reduces to: {x1+3x2=7,0=0,\begin{cases} -x_1+3x_2=7, \\ 0=0, \end{cases} The solution is: x1=3x27x_1=3x_2-7 where x2x_2 can be any real number.
When equations are proportional, they represent the same line. Every point on this line is a solution, resulting in infinitely many solutions.

Plotting

plot_lines(A_3_system)
You’ll see only one line because both equations represent the same line.

Summary Table

DeterminantSystem TypeNumber of SolutionsGeometric Interpretation
d0d \neq 0Non-singularOne unique solutionLines intersect at one point
d=0d = 0SingularZero or infiniteLines are parallel or coincident

Key Takeaways

  • Systems can be represented as coefficient matrix AA and constant vector bb
  • Augmented matrix combines both: [Ab][A | b]
  • Each row is an equation, each column is a variable’s coefficients
  • Use np.linalg.solve(A, b) for systems with unique solutions
  • Always check the determinant first
  • Verify solutions by substitution
  • Non-zero determinant: unique solution exists
  • Zero determinant: no solution or infinitely many solutions
  • Calculate with np.linalg.det(A)
  • Each equation is a line (in 2D) or hyperplane (in higher dimensions)
  • Solution is the intersection of all lines/planes
  • Parallel lines = no solution
  • Coincident lines = infinite solutions

Next: Matrix Operations

Master matrix multiplication and essential operations for linear algebra

Build docs developers (and LLMs) love