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.

LibraryPurposeResult TypeBest For
NumPyNumerical computationFloat approximationFast array math
SciPyScientific algorithmsFloat approximationOptimization, stats
SymPySymbolic computationExact expressionAlgebra, 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.

Sudo Sam with abstract math shapes
SymPy: finally doing calculus without crying.

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š[ˆ\›\ÈوŽˆ

LHJœŠŠŒŠKÊJœŠB•›Û[YH[˜Ý[ێˆŠŠ
LHJœŠŠŒŠBÜš]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ÛB“X^[][H›Û[YNˆ
͋MHÛMOØÛÙOÜ™O‚‚”Þ[TH\š]™Y[˜[]XØ[H]HÜ[X[Þ[[™\ˆ\ÈZYÚ\]X[ÈÚXÙHH˜Y]\È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Ø\
HžHÚ[™Ú[™ÈHÝ\™˜XÙH\™XH[™›Û[YH›Ü›][\ˏÜ‚‚ˆYH™˜\H‘œ™\]Y[H\ÚÙY]Y\Ý[ۜÏÚ‚ÈYH™˜\K\Þ[\K]œË[[\H•Ú[ˆÚÝ[H\ÙHÞ[TH[œÝXYو[TOÏÚς•\ÙHÞ[THÚ[ˆ[ÝH™YY^XÝÞ[X›ÛXÈ[œÝÙ\œÈKHÛÛš[™È\]X][ۜÈ[˜[]XØ[K\š]š[™È›Ü›][\ËÛܚÚ[™ÈÚ]˜][ۘ[[X™\œÈ܈ÜXÚX[ÛۜÝ[ÈZÙHÛÙOœOØÛÙOˆ[™ÛÙOœÜ\
ŠOØÛÙO‹ˆ\ÙH[THÚ[ˆ[ÝH™YY˜\ݝ[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.