Algebraic View of Systems of Equations
Pictures stop helping beyond three variables, so we need precise algebraic language: what a linear equation is, what counts as a solution, and how to store a whole system compactly as a matrix. This vocabulary is used in every later lesson.
Definitions
A linear equation in the variables has the form
where the coefficients and the constant term are numbers. Variables appear only to the first power, multiplied by constants, and never multiplied with each other.
A system of linear equations is a finite list of such equations in the same variables. A solution is an assignment of numbers to that makes every equation true at the same time. The solution set is the set of all solutions. Two systems are equivalent if they have the same solution set.
A system is consistent if it has at least one solution and inconsistent if it has none.
Matrices of a system
Writing over and over is wasteful. All the information sits in the numbers, which we arrange in a rectangular table, a matrix:
The coefficient matrix holds the coefficients, one row per equation and one column per variable (a missing variable gets a 0). The augmented matrix adds the constants as a final column. With the vector the whole system is the single equation .
from sympy import Matrix
A = Matrix([[1, 2, -1], [2, -1, 3], [0, 4, 1]])
b = Matrix([2, 9, 6])
aug = A.row_join(b) # glue b on as an extra column
print("augmented shape:", aug.shape)
for row in aug.tolist():
print(row)
x = Matrix([1, 1, 2]) # a candidate solution
print("A*x =", list(A * x), " b =", list(b), " solution?", A * x == b)augmented shape: (3, 4)
[1, 2, -1, 2]
[2, -1, 3, 9]
[0, 4, 1, 6]
A*x = [1, 7, 6] b = [2, 9, 6] solution? False
The candidate satisfies only the third equation, so it is not a solution. Checking a proposed solution is easy: multiply and compare. Finding solutions needs elimination.
Describing infinitely many solutions
When a system has infinitely many solutions, we describe them with parameters. Take
One equation, three unknowns. Choose and freely; then . The solution set is
This is the general solution in parametric form; and are free parameters, and each choice of them gives one particular solution. Geometrically it is a plane in .
from sympy import symbols, linsolve
x1, x2, x3 = symbols("x1 x2 x3")
print(linsolve([x1 - x2 + 2*x3 - 3], [x1, x2, x3]))
# check a few particular solutions
for s, t in [(0, 0), (1, 0), (2, 5)]:
p = (3 + s - 2*t, s, t)
print(p, "->", p[0] - p[1] + 2*p[2]){(x2 - 2*x3 + 3, x2, x3)}
(3, 0, 0) -> 3
(4, 1, 0) -> 3
(-5, 2, 5) -> 3
SymPy's linsolve writes the same answer with and themselves acting as parameters. SymPy prints x1 and 2*x3 in Python syntax.
Not linear, and why it matters
| Equation | Linear? | Reason |
|---|---|---|
| yes | constants may be any numbers | |
| no | product of variables | |
| no | function of a variable | |
| yes | is just a constant |
Linear systems are special because their solution sets are always "flat" (points, lines, planes and their higher-dimensional cousins) and because a finite, mechanical algorithm decides everything about them. Nonlinear systems have no such guarantee.
In software
- In ML, fitting a linear model writes one equation per data point: thousands of equations, a handful of unknowns (the weights). Such a system is usually inconsistent, which leads to least squares.
- Circuit simulators like SPICE and structural-engineering (finite element) codes assemble a coefficient matrix and a right-hand side and hand them to a solver.
Exercises
Exercise 1: Write the augmented matrix
Write the augmented matrix of , , (variables in the order ).
Answer
.
Solution
Each row lists the coefficients of , with 0 for a missing variable, and then the constant: , , .
Exercise 2: Back to equations
Which system has augmented matrix ? Give its general solution.
Answer
, ; general solution .
Solution
Exercise 3: Solution checker (code)
Write is_solution(A, b, x) with SymPy and test whether solves the system in the lesson (, ).
Answer
No: .
Solution
from sympy import Matrix
def is_solution(A, b, x):
return Matrix(A) * Matrix(x) == Matrix(b)
A = [[1, 2, -1], [2, -1, 3], [0, 4, 1]]
print(list(Matrix(A) * Matrix([2, -1, 1])), is_solution(A, [2, 9, 6], [2, -1, 1]))[-1, 8, -3] False
Exercise 4: Solve the lesson's system (code)
Find the actual solution of the system in the lesson with SymPy and check it.
Answer
.
Solution
from sympy import Matrix
A = Matrix([[1, 2, -1], [2, -1, 3], [0, 4, 1]])
b = Matrix([2, 9, 6])
x = A.solve(b)
print(list(x), A * x == b)[2, 1, 2] True
Test yourself
How many columns does the augmented matrix of a system with 4 equations in 3 unknowns have?
- 3
- 4
- 5
- 7
A system is called inconsistent when…
- it has more equations than unknowns
- it has no solution
- it has infinitely many solutions
- its coefficients are fractions
Which equation is linear in and ?