๐Ÿ“˜ Lesson 3 of 6 ยท Computational Mathematics

๐Ÿงฎ SymPy

SymPy is Python's library for symbolic mathematics โ€” instead of computing numeric approximations, it manipulates exact mathematical expressions, just like doing algebra by hand.

Course progress: 50%

01 Key Concepts

Symbolic vs. Numeric Computation

Numeric computation gives approximate decimal answers (like 1.41421...); symbolic computation keeps exact forms (like sqrt(2)).

Defining Symbols

Use sp.symbols('x') to create a symbolic variable that SymPy can manipulate algebraically, rather than treating it as a specific number.

Simplifying and Expanding Expressions

sp.expand() multiplies out expressions like (x+1)**2; sp.simplify() reduces an expression to its simplest equivalent form.

Solving Equations

sp.solve(equation, variable) finds exact symbolic solutions to equations, including ones with no simple algebraic shortcut.

Calculus with SymPy

sp.diff() computes derivatives symbolically, and sp.integrate() computes integrals symbolically -- both giving exact, not approximate, results.

02 Key Formulas

03 Solved Examples

Example 1 Using SymPy, what does sp.expand((x+1)**2) return?
  1. Expanding (x+1)^2 algebraically gives x^2 + 2x + 1.
Answer: x**2 + 2*x + 1
Example 2 Using SymPy, what does sp.solve(x**2 - 4, x) return?
  1. Solve x^2-4=0 algebraically: x^2=4, so x=2 or x=-2.
Answer: [-2, 2]
Example 3 Using SymPy, what does sp.diff(x**3, x) return?
  1. Apply the power rule symbolically: d/dx[x^3] = 3x^2.
Answer: 3*x**2

04 Practice Questions

1What does sp.expand((x+2)**2) return?
x**2 + 4*x + 4
2What does sp.solve(x - 5, x) return?
[5]
3What does sp.diff(x**2, x) return?
2*x
4What is the key difference between symbolic and numeric computation?
Symbolic keeps exact forms; numeric gives decimal approximations
5What function creates a symbolic variable in SymPy?
sp.symbols()

๐Ÿ“„ SymPy โ€” Downloadable Worksheet

10 questions with a full answer key. Grab the PDF to print, or try the interactive version in your browser.