liveBook · Manning

SymPy

As explained in Geometry for Programmers

An open-source Python library for symbolic mathematics, including algebraic manipulation, computation, and code generation.

SymPy is a Python library for symbolic mathematics: instead of only producing a number for one set of inputs, it can manipulate equations into formulas that remain useful for many inputs. It acts as a computer algebra system, letting code carry out algebra that would otherwise be tedious and error-prone by hand. In applied geometry, that makes mathematical notation executable without requiring advanced mathematics. section 1.5 section 1.7

The basic workflow is direct: create symbolic variables, state equations as expressions that equal zero, and pass both the equations and unknowns to `solve`. For two trains covering 450 km in one hour, with one traveling twice as fast as the other, SymPy solves the pair of constraints and returns a mapping from each symbol to its value. section 1.6

Listing 1.1 Numeric solution in SymPy
from sympy import *
 
Va, Vp = symbols('Va Vp')    #1
 
solution = solve([
    Vp - Va * 2,
    Va * 1 + Vp * 1 - 450
], (Va, Vp))
 
print(solution)

This complete listing defines two symbolic speeds, expresses the distance and speed-ratio constraints, and solves them in one call. section 1.6

Replacing fixed values with symbols turns the same calculation into a reusable formula. A distance `D`, travel time `t`, and speed multiplier can remain symbolic, so the result describes both train speeds for any compatible inputs. SymPy can also render a result as Python code or LaTex, connecting derivation, implementation, and publication. section 1.6 section 1.7

For small linear-equation problems, symbolic solving offers practical benefits and limits:

  • Use it to derive a general solution once, then use the generated formula instead of repeatedly calling a generic solver. section 3.7.3
  • Prefer it for systems up to roughly 4 × 4; symbolic expressions grow in time and length factorially as dimensions increase. section 3.7.3
  • Review generated code before adopting it, because it may need grooming for readability and performance. section 3.7.3 section 3.8.4

Least-squares polynomial coefficients

SymPy can solve a matrix equation for unknown polynomial coefficients even when the desired result is numeric. For rowing-session times, form the least-squares matrix and right-hand vector, represent the coefficients with symbols, solve the equation, then collect the returned dictionary values into a plain coefficient list for a general polynomial formula. This symbolic setup adds overhead compared with a numeric solver, but gives a uniform equation-solving workflow. section 6.2.3

How Geometry for Programmers covers it

How deep it goes
Demonstrates symbolic variables, equation systems, generated formulas, code output, and matrix-based least-squares solving, while deferring solver internals.
Examples
Uses train speeds, small linear systems for raytracing, and rowing-time data fitted by a linear polynomial.
What it assumes
Assumes elementary equations, coordinate planes, functions, and ability to read Python snippets; teaches symbolic computation without higher mathematics.
What you can do after
Apply it via SymPy to solve small equation systems, derive reusable formulas, and extract polynomial coefficients.
How it connects
Connects symbolic computation with systems of linear equations, Gaussian elimination, polynomial approximation, NumPy, and Matplotlib.

Also explained in

Compare how 3 books explain SymPy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Geometry for Programmers. The section links point to the parts of the book it draws on.