5.4.20Scientific Computing (Python)

SymPy — symbolic algebra, calculus, ODE solving

1,904 words9 min readdifficulty · medium

1. What is symbolic computation?

x, y = symbols('x y')        # create two symbols
expr = x**2 + 2*x + 1        # an expression tree, NOT a number

WHY a tree? Because x**2 + 2*x + 1 has no value yet — SymPy stores structure so it can later factor it into (x+1)2(x+1)^2.


2. Algebra: simplify, expand, factor, solve


3. Calculus from first principles


4. ODE solving — dsolve

Figure — SymPy — symbolic algebra, calculus, ODE solving

5. The 80/20 cheat-table

Goal Call Note
make unknown x = symbols('x') use positive=True etc. for assumptions
open brackets expand(e)
reverse factor(e)
tidy simplify(e) heuristic
plug in e.subs(x, v) exact
decimal e.evalf() / N(e)
derivative diff(f, x) / diff(f,x,2) order via extra arg
integral integrate(f, x) or (x,a,b)
limit limit(f, x, 0)
solve eqn solve(Eq(l,r), x)
ODE dsolve(Eq(...), y(x), ics=...) needs Function

Active Recall

Recall What does

symbols('x') give you and why not just x = 5? An unknown symbolic object that preserves algebra. x=5 collapses everything to numbers, destroying the structure you wanted to manipulate.

Recall How would you confirm

diff(x**2, x) is correct from scratch? limit((( x+h)**2 - x**2)/h, h, 0)2*x, matching the limit definition of the derivative.

Recall Why does

(x+1)**2 == x**2+2*x+1 give False? == is structural identity, not math equality. Use simplify(a-b)==0.

Recall What form does

dsolve return and how do you fix constants? A general Eq(y(x), ...) with C1, C2; supply ics={y(0): ...} to pin them.

Recall (Feynman, explain to a 12-year-old)

Normal calculators only know numbers — give them √2 and they panic into 1.414.... SymPy is like a robot math teacher who keeps the symbols "√2", "x", "π" written exactly on the board and follows the same pencil rules you learned — open brackets, factor, differentiate — and only turns things into decimals when you ask politely with .evalf(). So it never lies by rounding, and it hands you a formula you can read.

Connections

  • NumPy — numerical arrays (numbers vs symbols: opposite philosophies)
  • Limits and the definition of the derivative
  • Taylor and Maclaurin Series
  • Ordinary Differential Equations — characteristic equation method
  • Lambdify — bridging SymPy to NumPy for plotting
What does SymPy manipulate that NumPy does not?
Exact symbolic objects (x, √2, π, 1/3) as expression trees, not floats.
How do you create a symbolic unknown x?
x = symbols('x').
Expand (x+1)2(x+1)^2 in SymPy?
expand((x+1)**2)x**2 + 2*x + 1.
Factor x25x+6x^2-5x+6?
factor(x**2-5*x+6)(x-2)*(x-3).
Solve x25x+6=0x^2-5x+6=0?
solve(x**2-5*x+6, x)[2, 3].
Substitute x=√2 into (x+1)²?
((x+1)**2).subs(x, sqrt(2)), stays exact.
Compute the limit definition of d/dx(x²)?
limit(((x+h)**2-x**2)/h, h, 0)2*x.
Definite integral of x² from 0 to 1?
integrate(x**2,(x,0,1))1/3.
Taylor series of sin(x) to order 6?
sin(x).series(x,0,6)x - x**3/6 + x**5/120 + O(x**6).
Solve y''+y=0?
dsolve(Eq(y(x).diff(x,2)+y(x),0), y(x))C1*sin(x)+C2*cos(x).
Why is == unreliable for checking equality?
It tests structural identity; use simplify(a-b)==0 or a.equals(b).
How to get a decimal from a symbolic result?
.evalf() or N(expr).
How to apply initial condition y(0)=3 in dsolve?
pass ics={y(0): 3}.

Concept Map

contrasts with

built from

combine into

manipulated by

includes

sets eq to zero

swap symbol with

collapse to number

enables

contains

derived from

equals

Symbolic computation

Numerical math numpy

Symbol x unknown

Expression tree

Algebra ops

subs substitute value

evalf N to decimal

solve eq for x

Calculus

diff derivative

limit definition

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, SymPy ka core idea simple hai: normal calculator aur NumPy sirf numbers ke saath khelte hain — tum √2 doge to woh turant 1.414... bana deta hai aur asli cheez (exactness) kho jaati hai. SymPy ek robot maths teacher ki tarah hai jo x, sqrt(2), pi, 1/3 ko exactly waise hi rakhta hai jaise tum copy par likhte ho. Isliye pehla rule: variable ko x = symbols('x') se banao, kabhi x = 5 mat karo — warna algebra khatam.

Algebra ke 4 main moves hain: expand (brackets kholo), factor (ulta), simplify (saaf karo), aur solve (equation ka root nikaalo). Yaad rakho solve(expr, x) ka matlab expr = 0 hota hai, kyunki har equation a=ba=b ko ab=0a-b=0 likha ja sakta hai. Aur ek bada trap: == se equality mat check karna — woh structure compare karta hai, isliye simplify(a-b)==0 use karo.

Calculus mein diff derivative deta hai, integrate integral, limit limit, aur series Taylor expansion. Best part — tum khud derivation-from-scratch kar sakte ho: limit(((x+h)**2 - x**2)/h, h, 0) chalao aur dekho 2*x aata hai, bilkul power rule jaisa. Yeh confidence deta hai ki machine jhooth nahi bol rahi.

ODE ke liye dsolve hai. Pehle y = Function('y') banao, phir y(x).diff(x,2) likho. y+y=0y''+y=0 ka answer C1*sin(x)+C2*cos(x) aata hai — yeh characteristic equation r2+1=0r^2+1=0 se nikalta hai. Initial condition dene ke liye ics={y(0):3} pass karo, taaki constants fix ho jaayein. Exam aur research dono mein yeh tumhe formula wapas deta hai, sirf number nahi — isi liye SymPy itna powerful hai.

Go deeper — visual, from zero

Test yourself — Scientific Computing (Python)

Connections