I’m doing some independent study of linear algebra because it’s the branch of mathematics that seems to have the most applications in some of my other areas of interest: data analysis, digital image processing, and color science. Nakos and Joyner’s Linear algebra with applications starts with gaussian elimination. SymPy implements Gaussian elimination (AKA Gauss-Jordan elimination) in its linsolve function.

Nakos and Joyner’s example 9 (page 8) starts with the set of equations:

Represented as a matrix:

First we initialize sympy. This is a minimal set of tools to work the textbook example; the sympy.init_session function is probably easier for interactive work.

from sympy import Matrix, init_printing, symbols
from sympy.solvers.solveset import linsolve

init_printing()

Construct the augmented matrix representing the system of equations.

ex9 = Matrix([[ 1, 2,  0,  -3],
              [ 2, 3, -2, -10],
              [-1, 0,  6,   9]])
ex9

Although not explicitly displayed in the results, linsolve does still require the names of the symbols to solve for; they’re only associated with the matrix by their order.

x_1, x_2, x_3 = symbols('x_1 x_2 x_3')
linsolve(ex9, x_1, x_2, x_3)

When the system of equations doesn’t have a single unique solution, then the insistence on having named symbols makes more sense:

ex11 = Matrix([[1, 0, -9, 2],
               [0, 1,  4, 1],
               [0, 0,  0, 0]])
linsolve(ex11, x_1, x_2, x_3)

When no solutions exist, the an EmptySet is returned, which has the class sympy.sets.sets.EmptySet. All of SymPy’s sets have a boolean property is_EmptySet, which is True in this case.

ex12 = Matrix([[2, -1,  1, -2],
               [0,  1, -2, -5],
               [0,  0,  0,  5]])
solution_set = linsolve(ex12, x_1, x_2, x_3)
solution_set