📝 Worksheet← Lesson
BitWithBite
Computational Mathematics · Quick Reference

SymPy Cheat Sheet

Computational Mathematics · Lesson 3/6
In one line: symPy is Python's library for symbolic mathematics — instead of computing numeric approximations, it manipulates exact mathematical expressions, just like doing algebra by hand.

Key Ideas

1Symbolic vs. Numeric Computation. Numeric computation gives approximate decimal answers (like 1.41421...); symbolic computation keeps exact forms (like sqrt(2)).
2Defining Symbols. Use sp.symbols('x') to create a symbolic variable that SymPy can manipulate algebraically, rather than treating it as a specific number.
3Simplifying and Expanding Expressions. sp.expand() multiplies out expressions like (x+1)**2; sp.simplify() reduces an expression to its simplest equivalent form.
4Solving Equations. sp.solve(equation, variable) finds exact symbolic solutions to equations, including ones with no simple algebraic shortcut.
5Calculus with SymPy. sp.diff() computes derivatives symbolically, and sp.integrate() computes integrals symbolically -- both giving exact, not approximate, results.

Worked Examples

Using SymPy, what does sp.expand((x+1)**2) return?
x**2 + 2*x + 1
Using SymPy, what does sp.solve(x**2 - 4, x) return?
[-2, 2]
Using SymPy, what does sp.diff(x**3, x) return?
3*x**2

Formulas

sp.symbols('x') creates a symbolic variable
sp.solve(eq, x) solves an equation for x
sp.diff(expr, x) differentiates expr with respect to x

Practice Yourself

What does sp.expand((x+2)**2) return?
x**2 + 4*x + 4
What does sp.solve(x - 5, x) return?
[5]
What does sp.diff(x**2, x) return?
2*x