← Back to list

SymPy: Symbolic Mathematics in Python

GitHub

Merin Theres Jose · 2025-08-24 16:13 · 14 claps · 2.8 min read
#python #sympy
Open on Medium ↗
Wiki topics: 🔓 · Open Source 📐 · Mathematics

SymPy: Symbolic Mathematics in Python

GitHub

Mathematics is the language of science, but solving complex equations by hand or writing them in raw code can be frustrating. That’s where SymPy comes in.

SymPy is a Python library for symbolic mathematics. Unlike numerical libraries (like NumPy), which approximate values, SymPy lets you work with formulas, symbols, and exact solutions. You can expand, simplify, differentiate, integrate, and even solve equations all while keeping results symbolic.

SymPy is completely free and open-source. While many professional symbolic systems are paid, SymPy provides similar power without the price tag. It’s widely used in education, research, and engineering projects across the world.

Comparison with Other Systems

Here’s how SymPy stands next to other well known systems:

Features of SymPy (Explained with Examples)

Going through SymPy’s core features, with explanations and code you can try in Google Colab which I have provided below.

  1. Algebra
from sympy import symbols, expand, factor, simplify

x, y = symbols('x y')
expr = (x + y)**2

print("Expanded:", expand(expr))
print("Factored:", factor(x**2 + 2*x*y + y**2))
print("Simplified:", simplify((x**2 + 2*x + 1)/(x+1)))

Unlike NumPy, which only handles numerical arrays, SymPy provides exact symbolic computation. This means operations like expansion or factorization preserve algebraic structure without approximation.

2. Calculus

from sympy import symbols, diff, integrate, limit, sin, init_printing
from IPython.display import display

init_printing("mathjax")

x = symbols('x')
expr = sin(x)**2

derivative = diff(expr, x)
integral = integrate(expr, x)
lim = limit(expr/x, x, 0)

display(expr)
display(derivative)
display(integral)
display(lim)

While SciPy offers numerical differentiation/integration with finite differences or quadrature, SymPy computes derivatives and integrals analytically.

3. Equation Solving

from sympy import symbols, Eq, solve, sin, cos

x = symbols('x')
eq = Eq(sin(x) + cos(x), 0)
solutions = solve(eq, x)
print(solutions)

Most numerical libraries rely on root-finding algorithms (Newton-Raphson, etc.) which approximate solutions. SymPy’s solver computes closed-form exact roots when possible (e.g., solving polynomials, trigonometric systems).

4. Linear Algebra

from sympy import Matrix
A = Matrix([[1, 2], [3, 4]])
A.det(), A.eigenvals()

Numerical libraries like NumPy calculate matrix properties approximately with floating-point errors. SymPy supports symbolic determinants, eigenvalues, and eigenvectors, maintaining exact rational forms.

5. Geometry


from sympy import Point, Line
p1, p2 = Point(1,2), Point(3,4)
Line(p1, p2).equation()

Unlike traditional geometry engines which are numerical, SymPy’s geometry module computes equations of lines, circles, and intersections in exact symbolic form.

6. Physics

from sympy import symbols
from sympy.physics.mechanics import dynamicsymbols, ReferenceFrame, Point
t = symbols('t')
theta = dynamicsymbols('theta')
N = ReferenceFrame('N')
O, P = Point('O'), Point('P')
P.set_pos(O, theta*N.x)
P.pos_from(O)

SymPy’s physics module supports Lagrangian and Hamiltonian mechanics with symbolic variables. In contrast, tools like SciPy simulate numerically. Symbolic representation helps in dynamics derivation, analytical stability analysis, and symbolic equation generation for simulators.

7. Pretty Printing

from sympy import symbols, expand, init_printing
from IPython.display import display
init_printing("mathjax")
x = symbols('x')
display(expand((x+1)**3))

While numerical libraries just print raw arrays, SymPy integrates with MathJax/LaTeX rendering, producing publication-quality equations directly in Jupyter/Colab. Useful in research documentation, automated report generation, and educational tools.

8. Code Generation

from sympy import symbols
from sympy.utilities.codegen import codegen
x, y = symbols('x y')
codegen(("expr", (x+y)**2), "C", "gen", header=False, empty=False)

Unlike pure symbolic engines (e.g., Mathematica), SymPy provides direct code generation in C, Fortran, or CUDA. This bridges symbolic derivation with high-performance computing, enabling use in compilers, embedded systems, and scientific simulation pipelines.

Testing and Development

SymPy has a robust testing system:

  • bin/test → Run all tests
  • bin/doctest → Check examples in docs
  • setup.py test → Run tests with Python tools
  • GitHub Actions → Automatic testing for every update

This ensures reliability before changes reach users.

Getting Started in Colab

You can try SymPy live in Google Colab without installing anything: SymPy in Colab

Why SymPy is Amazing

  • To Learn and visualize math interactively.
  • To Explore complex models symbolically.
  • For Simulating systems exactly.
  • Free, community-driven, and continuously improving.

References


메타데이터
post_id
48872dee7428
slug
sympy-symbolic-mathematics-in-python-48872dee7428
url
https://medium.com/@merintheresjose/sympy-symbolic-mathematics-in-python-48872dee7428
canonical_url
https://medium.com/@merintheresjose/sympy-symbolic-mathematics-in-python-48872dee7428
author_url
https://medium.com/@merintheresjose
status
ok
fetched_at
2026-07-18 00:26:52