Intermediate
Have you ever solved an algebra problem only to get a decimal approximation when you wanted the exact symbolic answer? Python’s SymPy library solves this problem by treating mathematics the way a mathematician does — symbolically. Instead of computing pi as 3.14159..., SymPy keeps it as the exact symbol pi. Instead of approximating a square root, it returns sqrt(2). This is symbolic computation, and it transforms Python into a full-featured computer algebra system.
SymPy is a pure Python library — no C extensions, no compiled code — so installation is straightforward with pip install sympy. It works alongside NumPy and SciPy but solves a different problem: those libraries compute numerical answers fast, while SymPy computes exact symbolic answers. You can use SymPy to solve equations, expand polynomials, compute derivatives and integrals, factor expressions, and even generate LaTeX output for publication-quality math.
In this article, we will cover the fundamentals of SymPy from the ground up: how to define symbolic variables, simplify and expand expressions, solve equations, compute limits and derivatives, evaluate integrals, and apply SymPy to a practical calculus problem. By the end, you will be able to use Python as a complete algebra and calculus tool.
SymPy Quick Example
Here is a self-contained example that shows SymPy solving a quadratic equation and computing a derivative — both with exact results:
# quick_sympy.py
from sympy import symbols, solve, diff, expand
x = symbols('x')
# Solve a quadratic equation
equation = x**2 - 5*x + 6
solutions = solve(equation, x)
print("Solutions:", solutions)
# Compute the derivative of x^3 - 2x + 1
expr = x**3 - 2*x + 1
derivative = diff(expr, x)
print("Derivative:", derivative)
# Expand a factored expression
expanded = expand((x + 2) * (x - 3))
print("Expanded:", expanded)
Output:
Solutions: [2, 3]
Derivative: 3*x**2 - 2
Expanded: x**2 - x - 6
These are exact symbolic results. solve() found the roots of the quadratic as integers, not decimals. diff() computed the derivative using the power rule. expand() multiplied out the factors. Every result is a SymPy expression that can be further manipulated, printed as LaTeX, or evaluated numerically.
What Is SymPy and Why Use It?
SymPy is a computer algebra system (CAS) written entirely in Python. A CAS is software that manipulates mathematical expressions in symbolic form, the way a human mathematician writes on paper, rather than computing numerical approximations. SymPy’s closest analogues are Mathematica and Maple, but SymPy is free, open source, and integrates naturally with the Python data science ecosystem.
| Library | Purpose | Result Type | Best For |
|---|---|---|---|
| NumPy | Numerical computation | Float approximation | Fast array math |
| SciPy | Scientific algorithms | Float approximation | Optimization, stats |
| SymPy | Symbolic computation | Exact expression | Algebra, calculus proofs |
Use SymPy when you need exact answers — factoring polynomials, solving equations analytically, or deriving formulas before implementing them numerically.
Defining Symbolic Variables
The foundation of SymPy is the Symbol class. Before using any variable in a symbolic expression, you declare it with symbols(). This tells SymPy “this letter stands for a mathematical unknown, not a Python value.”
# symbols_demo.py
from sympy import symbols, Symbol
# Single symbol
x = symbols('x')
# Multiple symbols at once
a, b, c = symbols('a b c')
# Symbol with assumptions (positive, real, integer, etc.)
n = symbols('n', integer=True, positive=True)
t = symbols('t', real=True)
print(type(x)) #
print(x + x) # 2*x
print(x * x) # x**2
print(a + b + a) # 2*a + b
Output:
<class 'sympy.core.symbol.Symbol'>
2*x
x**2
2*a + b
Assumptions like positive=True or integer=True help SymPy simplify expressions correctly. For example, sqrt(x**2) only simplifies to x when SymPy knows x is non-negative.
Simplifying and Manipulating Expressions
Once you have symbols, you can build expressions and simplify them. SymPy provides simplify(), expand(), factor(), cancel(), and collect() — each targeting different transformation goals.
# simplify_demo.py
from sympy import symbols, simplify, expand, factor, cancel, trigsimp, sin, cos
x, y = symbols('x y')
# simplify -- general-purpose simplification
expr1 = (x**2 - 1) / (x - 1)
print("cancel:", cancel(expr1)) # x + 1
# expand -- distribute multiplication
expr2 = (x + y)**3
print("expand:", expand(expr2)) # x**3 + 3*x**2*y + 3*x*y**2 + y**3
# factor -- reverse of expand
expr3 = x**3 + 3*x**2*y + 3*x*y**2 + y**3
print("factor:", factor(expr3)) # (x + y)**3
# trigsimp -- simplify trig expressions
trig_expr = sin(x)**2 + cos(x)**2
print("trigsimp:", trigsimp(trig_expr)) # 1
Output:
cancel: x + 1
expand: x**3 + 3*x**2*y + 3*x*y**2 + y**3
factor: (x + y)**3
trigsimp: 1
Notice that cancel() correctly identified that (x**2 - 1)/(x - 1) simplifies to x + 1 by canceling the common factor (x - 1). This is exact symbolic cancellation — floating-point arithmetic would have struggled near x = 1.
Solving Equations
The solve() function finds the values of unknowns that satisfy an equation. You can solve for one variable in terms of others, solve systems of equations, and even solve inequalities. Pass the expression (or a list of expressions) and the variable(s) to solve for.
# solve_demo.py
from sympy import symbols, solve, Eq, sqrt
x, y = symbols('x -y')
# Solve x^2 = 9
solutions = solve(x**2 - 9, x)
print("x^2 = 9:", solutions) # [-3, 3]
# Use Eq() for equations with both sides
eq = Eq(2*x + 3, 11)
print("2x + 3 = 11:", solve(eq, x)) # [4]
# System of equations
eq1 = Eq(x + y, 7)
eq2 = Eq(2*x - y, 2)
print("System:", solve([eq1, eq2], [x, y])) # {x: 3, y: 4}
# Solve quadratic formula symbolically
a, b, c = symbols('a b c')
quad = a*x**2 + b*x + c
print("Quadratic formula:", solve(quad, x))
Output:
x^2 = 9: [-3, 3]
2x + 3 = 11: [4]
System: {x: 3, y: 4}
QuAdaÉÑ¥½ÉµÕ±èl µ¬ÍÅÉÐ ´Ð©©¬¨¨È¤¤¼ È©¤° µ´ÍÅÉÐ ´Ð©©¬¨¨È¤¤¼ È©¥tð½½øð½ÁÉø((ñÀùQ¡±
ÍÐÉÍձХÌÑ¡ÅÕ
É
Ñ¥½ÉµÕ±É¥ÙÍåµ½±¥
±±ä´´MåµAäÁɽÙÑ¡½ÉµÕ±Ñ¡
ÐÍÑÕ¹Ñ̵µ½É¥é°©ÕÍÐä
Í¥¹¥ÐѼͽ±Ù¹É
°ÅÕ
É
Ñ¥¸Q¡¥Ì¥ÌÑ¡Á½ÝȽÍåµ½±¥½µÁÕÑ
Ñ¥½¸¸ð½Àø((ñ¥ÕÉÍÑå±ôÑáе
±¥¸é¹ÑÈíµ
É¥¸èÉ´Àìø(ñ¥µÍÉô¡ÑÑÁÌè¼½ÁåÑ¡½¹¡½ÝѽÁɽÉ
´¹½´½ÝÀµ½¹Ñ¹Ð½ÕÁ±½
̼ÈÀÈØ¼ÀнÍåµÁäµÍ½±ÙµÅÕ
Ñ¥½¹Ì¹Á¹
±ÐôÕÉÙ
±¥¹±½Ý¥¹Í½±ÕÑ¥½¸ÍÑå±ôµ
àµÝ¥Ñ èÄÀÀí¡¥¡Ðé
ÕѼí½ÉȵÉ
¥ÕÌèáÁàì¼ø(ñ¥
ÁÑ¥½¸ÍÑå±ô½¹ÐµÍÑå±é¥Ñ
±¥í½±½Èèàààíµ
É¥¸µÑ½ÀèÀ¸Õ´ìùͽ±Ù ¤ÉÑÕɹÌá
ÐÉÍÕ±Ñ̸e½Õȱ½
ÐÁɥͥ½¸¥ÍÍÕÌ
ɽ¥¥
±±äͽµ½¹±ÍÁɽ±´¸ð½¥
ÁÑ¥½¸ø(ð½¥ÕÉø((ñ È¥ô
±Õ±ÕÌù
±Õ±ÕÌèÉ¥Ù
Ñ¥Ṵ̀%¹ÑÉ
±Ì°
¹1¥µ¥ÑÌð½ Èø((ñÀùMåµAä¡
¹±Ì
±°Ñ¡½É½ÁÉ
Ñ¥½¹Ì½
±Õ±Õ̸ñ½ù¥ ¤ð½½ø½µÁÕÑÌÉ¥Ù
Ñ¥Ṵ̀ñ½ù¥¹ÑÉ
Ñ ¤ð½½ø½µÁÕÑÌ
¹Ñ¥É¥Ù
Ñ¥ÙÌ
¹¥¹¥Ñ¥¹ÑÉ
±Ì°
¹ñ½ù±¥µ¥Ð ¤ð½½ø½µÁÕṈ̃¥µ¥ÑÌ
ÐÁ½¥¹Ñ̽È
Ð¥¹¥¹¥Ñä¸ð½Àø((ñÁÉøñ½ø
±Õ±ÕÍ}µ¼¹Áä)ɽ´ÍåµÁ䥵Á½ÉÐÍåµ½±Ì°¥°¥¹ÑÉ
Ѱ±¥µ¥Ð°Í¥¸°½Ì°áÀ°±¸°½¼()àôÍåµ½±Ì ì¤((É¥Ù
Ñ¥Ùè½à½Í¥¸¡à¤¨yà)áÁÈôÍ¥¸¡à¤¨áÀ¡à¤)Äô¥¡áÁȰà¤)ÁÉ¥¹Ð ¥ÉÍÐÉ¥Ù
Ñ¥Ùè°Ä¤(yà©Í¥¸¡à¤¬yà½Ì¡à¤((M½¹É¥Ù
Ñ¥Ù)Èô¥¡áÁȰà°È¤)ÁÉ¥¹Ð M½¹É¥Ù
Ñ¥Ùè°È¤((%¹¥¹¥Ñ¥¹ÑÉ
°)
¹Ñ¥É¥Øô¥¹ÑÉ
ѡਨ̬ȩà°à¤)ÁÉ¥¹Ð ¹Ñ¥É¥Ù
Ñ¥Ùè°
¹Ñ¥É¥Ø¤à¨¨Ð¼Ð¬à¨¨È¦p((¥¹¥Ñ¥¹ÑÉ
°É½´ÀѼÄ)¥¹¥Ñô¥¹ÑÉ
ѡਨȰ¡à°À°Ä¤¤)ÁÉ¥¹Ð ¥¹¥Ñ¥¹ÑÉ
°°¥¹¥Ñ¤Ä¼Ì((1¥µ¥Ð
Ìà
ÁÁɽ
¡ÌÀ)°ô±¥µ¥Ð¡Í¥¸¡à¤¼à°à°À¤)ÁÉ¥¹Ð ±¥´Í¥¸¡à¤½àè°°¤Ä((1¥µ¥Ð
Ìà
ÁÁɽ
¡Ì¥¹¥¹¥Ñä)°Èô±¥µ¥Ð Ľà°à°½¼¤)ÁÉ¥¹Ð ±¥´Ä½à谰ȤÀð½½øð½ÁÉø((ñÀøñÍÑɽ¹ù=ÕÑÁÕÐèð½ÍÑɽ¹øð½Àø((ñÁÉøñ½ù¥ÉÍÐÉ¥Ù
Ñ¥ÙèáÀ¡à¤©Í¥¸¡à¤¬áÀ¡à¤©½Ì¡à¤)M½¹É¥Ù
Ñ¥ÙèÈ©ê@¡à¤©½Ì¡à¤)¹Ñ¥É¥Ù
Ñ¥ÙèਨмЬਨÈ)U¥¹¥Ñ¥¹ÑÉ
°èļÌ)±¥´Í¥¸¡à¤½àèÄ)±¥´Ä½àèÀð½½øð½ÁÉø((ñÀùQ¡±
ÍÍ¥
±Õ±ÕÌÉÍÕ±Ðñ½ù±¥´¡Í¥¸à¤½àôÄð½½ø
Ìà
ÁÁɽ
¡ÌÀ¥Ì½µÁÕÑá
ѱä¸Q¡¥¹¥Ñ¥¹ÑÉ
°½áxÈɽ´ÀѼĥÌá
ѱäļ̴´É
Ñ¥½¹
°¹ÕµÈ°¹½Ð¥µ
°
ÁÁɽ᥵
Ñ¥½¸¸ñ½ù½¼ð½½ø¥ÌMåµAäÌ¥¹¥¹¥ÑäÍåµ½°¸ð½Àø((ñ È¥ô¹ÕµÉ¥µÙ
±Õ
Ñ¥½¸ù
½¹ÙÉÑ¥¹Måµ½±¥IÍÕ±ÑÌѼ9ÕµÉÌð½ Èø((ñÀùMåµ½±¥ÉÍÕ±ÑÌ
ÉÕÍÕ°½È
¹
±å̰ͥÕÐͽµÑ¥µÌå½Ô¹¥µ
°Ù
±Õ¸UÍñ½ø¹Ù
± ¤ð½½øÑ¼Ù
±Õ
ÑMåµAäáÁÉÍÍ¥½¸¹ÕµÉ¥
±±ä°Ý¥Ñ ½¹Ñɽ°½ÙÈÑ¡¹ÕµÈ½Í¥¹¥¥
¹Ð¥¥Ñ̸ð½Àø((ñÁÉøñ½ø¹ÕµÉ¥}Ù
°¹Áä)ɽ´ÍåµÁ䥵Á½ÉÐÁ¤°ÍÅÉа°I
Ñ¥½¹
°°Ù
±°Íåµ½±Ì°¥¹ÑÉ
Ñ((Ù
±Õ
ÑÁ¤Ñ¼ÔÀ¥µ
°Á±
Ì)ÁÉ¥¹Ð¡Á¤¹Ù
± ÔÀ¤¤((á
ÐÉ
Ñ¥½¹
°
ɥѡµÑ¥)ÈôI
Ñ¥½¹
° İ̤¬I
Ñ¥½¹
° İؤ)ÁÉ¥¹Ð ļ̬ļØô°È¤Ä¼È((ÍÅÉРȤá
ÐÙ̹յɥ)ɽ´ÍåµÁ䥵Á½ÉÐÍÅÉÐ)ÌôÍÅÉРȤ)ÁÉ¥¹Ð á
Ðè°Ì¤ÍÅÉРȤ)ÁÉ¥¹Ð 9յɥè°Ì¹Ù
± ¤¤Ä¸ÐÄÐÈÄÌÔØÈÌÜÌÄÀ((MÕÍÑ¥ÑÕÑÙ
±Õ¥¹Ñ¼
¸áÁÉÍÍ¥½¸) ôÍåµ½±Ì à¤)áÁÈôਨȬȩà¬Ä)ÉÍÕ±ÐôáÁȹÍÕÌ¡à°Ì¤)ÁÉ¥¹Ð ÐàôÌè°ÉÍÕ±Ð¤ÄØð½½øð½ÁÉø((ñÀøñÍÑɽ¹ù=ÕÑÁÕÐèð½ÍÑɽ¹øð½Àø((ñÁÉøñ½øÌ¸ÄÐÄÔäÈØÔÌÔàäÜäÌÈÌàÐØÈØÐÌÌàÌÈÜäÔÀÈààÐÄäÜÄØäÌääÌÜÔÄ(ļ̬ļØôļÈ)á
ÐèÍÅÉРȤ)9յɥèĸÐÄÐÈÄÌÔØÈÌÜÌÄÀ)ÐàôÌèÄØð½½øð½ÁÉø((ñÀùQ¡ñ½ø¹ÍÕÌ ¤ð½½øµÑ¡½ÍÕÍÑ¥ÑÕÑ̽¹ÉÑÙ
±Õ̽ÈÍåµ½±Ì¸Q¡¥Ì¥Ì¡½Üå½Ô¼É½´Íåµ½±¥½ÉµÕ±Ñ¼½µÁÕÑÉÍձнÈÍÁ¥¥¥¹ÁÕд´¥ÉÍÐÉ¥ÙÑ¡½ÉµÕ±Íåµ½±¥
±±ä°Ñ¡¸Ù
±Õ
ѥйյɥ
±±ä½È
¹äÁ
É
µÑÈÙ
±ÕÌå½Ô¹¸ð½Àø((ñ¥ÕÉÍÑå±ôÑáе
±¥¸é¹ÑÈíµ
É¥¸èÉ´Àìø(ñ¥µÍÉô¡ÑÑÁÌè¼½ÁåÑ¡½¹¡½ÝѽÁɽÉ
´¹½´½ÝÀµ½¹Ñ¹Ð½ÕÁ±½
̼ÈÀÈØ¼ÀнÍåµÁäµÁɥͥ½¸¹Á¹
±ÐôA$±¥½¹ÙÉÑ¥¹ÕééäѼÁÉ¥ÍÙ
±ÕÌÍÑå±ôµ
àµÝ¥Ñ èÄÀÀí¡¥¡Ðé
ÕѼí½ÉȵÉ
¥ÕÌèáÁàì¼ø(ñ¥
ÁÑ¥½¸ÍÑå±ô½¹ÐµÍÑå±é¥Ñ
±¥í½±½Èèàààíµ
É¥¸µÑ½ÀèÀ¸Õ´ìùQ¡É±¥¹ÌѼե±Õ±°
L¸5
Ñ¡µ
Ñ¥¹¥¹ÉÌ
ɹÉÙ½ÕͱäÍ¥ÁÁ¥¹½¸ð½¥
ÁÑ¥½¸ø(ð½¥ÕÉø((ñ È¥ôÉ
°µ±¥µá
µÁ±ùI
°µ1¥á
µÁ±èMåµ½±¥=ÁÑ¥µ¥é
Ñ¥½¸½A¡åÍ¥ÌAɽ±´ð½ Èø((ñÀù]Ý¥±°ÕÍMåµAäѼͽ±Ù±
ÍÍ¥½ÁÑ¥µ¥é
Ñ¥½¸Áɽ±´É½´
±Õ±ÕÌ襹¥¹Ñ¡¥µ¹Í¥½¹Ì½å±¥¹ÈÝ¥Ñ µ
᥵մٽ±Õµ½È¥áÍÕÉ
ɸQ¡¥Ìµ½¹ÍÑÉ
ÑÌÍåµ½±¥É¥Ù
Ñ¥½¸½
¸
¹
񆥝
°Í½±ÕÑ¥½¸¸ð½Àø((ñÁÉøñ½øå±¥¹É}½ÁÑ¥µ¥é
Ñ¥½¸¹Áä)ɽ´ÍåµÁ䥵Á½ÉÐÍåµ½±Ì°Á¤°Í½±Ù°¥°Í¥µÁ±¥ä°ÍÅÉаI
Ñ¥½¹
°((¥¹Íåµ½±Ì¡Á½Í¥Ñ¥Ù°
ÕÍ¥µ¹Í¥½¹ÌµÕÍÐÁ½Í¥Ñ¥Ù¤)Ȱ ôÍåµ½±Ì È °Á½Í¥Ñ¥ÙõQÉÕ¤((¥áÍÕÉ
ɽ¹ÍÑÉ
¥¹ÐèLôÈ©Á¤©ÉxȬȩÁ¤©È© )M}¥áôÄÀÀ¥áÍÕÉ
É¥¸µxÈ(áÁÉÍÌ ¥¸Ñɵ̽ÈÕÍ¥¹Ñ¡½¹ÍÑÉ
¥¹Ð)LôÈ©Á¤©È¨È¬È©Á¤©È© )¡}áÁÈôͽ±Ù¡L´M}¥á° ¥lÁt)ÁÉ¥¹Ð ¥¸Ñɵ̽Èè°¡}áÁȤ((Y½±Õµ½å±¥¹ÈèXôÁ¤¨ÉxȨ )XôÁ¤¨È¨¨È¨¡}áÁÈ)Y}Í¥µÁ±¥¥ôÍ¥µÁ±¥ä¡Ø¤)ÁÉ¥¹Ð Y½±ÕµÕ¹Ñ¥½¸è°Y}Í¥µÁ±¥¥¤((¥¹É¥Ñ¥
°Á½¥¹ÐèÍÐX½ÈôÀ(eXÈô¥¡Y}Í¥µÁ±¥¥°È¤)ɥѥ
±}Á½¥¹ÑÌôͽ±Ù¡Y}ȰȤ)ÁÉ¥¹Ð
ɥѥ
°ÈÙ
±ÕÌè°É¥Ñ¥
±}Á½¥¹Ñ̤((UÍÑ¡Á½Í¥Ñ¥ÙÉ
°É¥Ñ¥
°Á½¥¹Ð)É}½ÁÐômÀ½ÈÀ¥¸É¥Ñ¥
±}Á½¥¹ÑÌ¥À¹¥Í}É
°
¹ÀøÁulÁt)¡}½ÁÐô¡}áÁȹÍÕ̡ȰÉ}½ÁФ()ÁÉ¥¹Ð¡q¹=ÁÑ¥µ
°É
¥ÕÌèíÍ¥µÁ±¥ä¡É}½ÁÐ¥ô¤)ÁÉ¥¹Ð¡=ÁÑ¥µ
°¡¥¡ÐèíÍ¥µÁ±¥ä¡¡}½ÁÐ¥ô¤)ÁÉ¥¹Ð¡I
Ñ¥¼ ½ÈôíÍ¥µÁ±¥ä¡¡}½ÁмÉ}½ÁÐ¥ô¡Í¡½Õ±È½Èµ
àÙ½±Õµ¤¤((9յɥ
°Ù
±ÕÌ)ÁÉ¥¹Ð¡q¹9յɥ
°É
¥ÕÌèíÉ}½ÁйÙ
± Ð¥ô´¤)ÁÉ¥¹Ð¡9յɥ
°¡¥¡Ðèí¡}½ÁйÙ
± Ð¥ô´¤)ÁÉ¥¹Ð¡5
᥵մٽ±ÕµèímÁ¤(¨É}½ÁШ¨È¨¡}½ÁФ¹Ù
± Ð¥ô´Ôt¤ð½½øð½ÁÉøÝÛÏÝ]]ÜÝÛÏÜOÛÙO[\\ÈÙ
LHJKÊJBÛ[YH[Ý[Û
LHJBÜ]XØ[[Y\ÎÜÜ\
LÊÊJJK\Ü\
LÊÊJJWBÜ[X[Y]\ÎÜ\
ML
KÊÊÜ\
JJBÜ[X[ZYÚÜ\
ML
KÊÊÜ\
JJB][ÈÜH
ÚÝ[HÜX^Û[YJB[Y\XØ[Y]\ÎÌÛB[Y\XØ[ZYÚ
HÛBX^[][HÛ[YN
ÍMHÛMOØÛÙOÜOÞ[TH\]Y[[]XØ[H]HÜ[X[Þ[[\\ÈZYÚ\]X[ÈÚXÙHHY]\ÈKHHÛÝÛ\Ý[ÛHØ[Ý[\È]ÙH\ÝX[H]HÈ\Ý\HÙH[OÝYÙ[O]HÛÛ\][ÈH^XÝÞ[XÛXÈÜ]XØ[Ú[[H^XÝ][Ë[ÝHÛÝ[^[\È^[\HÈÜ[Z^H[HÚ\H
ÞÛÛKÜ\HØ\
HHÚ[Ú[ÈHÝ\XÙH\XH[Û[YHÜ][\ËÜYH\H\]Y[H\ÚÙY]Y\Ý[ÛÏÚÈYH\K\Þ[\K]Ë[[\HÚ[ÚÝ[H\ÙHÞ[TH[ÝXYÙ[TOÏÚÏ\ÙHÞ[THÚ[[ÝHYY^XÝÞ[XÛXÈ[ÝÙ\ÈKHÛÛ[È\]X][ÛÈ[[]XØ[K\][ÈÜ][\ËÛÜÚ[ÈÚ]][Û[[X\ÈÜÜXÚX[ÛÛÝ[ÈZÙHÛÙOOØÛÙO[ÛÙOÜ\
OØÛÙO\ÙH[THÚ[[ÝHYY\Ý[Y\XØ[ÛÛ\]][ÛÛ\^\ÈÙ]K^HÛÛ\[Y[XXÚÝ\\ÙHÞ[THÈ\]HHÜ][K[[\[Y[]Ú][THÜ\ÜX[ÙKÜÈYH\K\ÛÝÈÚH\ÈÞ[THÛÝÈÛÛ\\YÈ[TOÏÚ£7>
Symbolic computation is inherently more complex than numerical computation. SymPy must maintain symbolic relationships and apply algebraic rules, which is computationally expensive. For performance-critical numerical work, use .evalf() for single values or lambdify() to convert a SymPy expression into a fast NumPy function: f = lambdify(x, expr, 'numpy')</code>. This gives you the symbolic derivation from SymPy and the numeric speed of NumPy.
How do I get LaTeX output from SymPy?
Use from sympy import latex; latex(a)</code>. This converts any SymPy expression into a LaTeX string. For example, latex(Integral(sqrt(1-x**2), x)) produces \int \sqrt{1 - x^{2}}\, dx. This is extremely useful for writing papers or generating math-heavy documentation programmatically.
What if solve() cannot find an exact solution?
For transcendental equations (e.g., x / exp(x) = 1), SymPy may return an empty list or raise a NotImplementedError. In these cases, use nsolve(expr, x, initial_guess) for numerical root finding, or convert to SciPy's fsolve(). Always check the return value of solve() -- an empty list means no closed-form solution exists.
Can SymPy solve systems of differential equations?
Yes. SymPy's dsolve() function solves ordinary differential equations symbolically. For example, dsolve(f(x).diff(x) - f(x), f(x)) returns f(x) = C1*exp(x) where C1 is an integration constant. For systems of ODEs, pass a list of equations and a list of functions. For complex or non-linear systems, numerical solvers in SciPy are often more practical.
Conclusion
We have covered the core workflow of SymPy: defining symbolic variables with symbols(), simplifying and transforming expressions with simplify(), factor(), and expand(), solving equations and systems with solve(), computing derivatives with diff(), integrals with integrate(), limits with limit(), and converting symbolic results to numbers with .evalf() and .subs(). The cylinder optimization example showed how these tools combine into a complete analytical workflow.
The natural next step is to explore lambdify() to convert SymPy expressions into fast NumPy functions, and Matrix() for symbolic linear algebra. You can also use SymPy's plotting module to visualize symbolic functions directly, or generate LaTeX output with latex() for publication-quality math typesetting.
Official documentation: docs.sympy.org -- the SymPy Tutorial is an excellent next read.
Related Articles