5.4.20 · Coding › Scientific Computing (Python)
Jyadatar calculators aur numpy numerical math karte hain: woh 1.4142135 jaisi floating-point numbers ke saath kaam karte hain. SymPy symbolic math karta hai: woh √2 ke idea ko hi handle karta hai. Yeh sqrt(2), pi, x, 1/3 ko exact mathematical objects ki tarah rakhta hai aur inhe waisi hi manipulate karta hai jaise ek insaan kaagaz par karta hai — factoring, differentiating, solving — aur decimal mein tabhi convert karta hai jab aap poochho .
YEH KYU MATTER KARTA HAI: exactness ka matlab hai koi rounding error nahi, aur aapko sirf numbers nahi balki formulas wapas milte hain. Aap isse derive, verify, aur teach karne ke liye use kar sakte hain.
Ek symbol ek named mathematical unknown hota hai (jaise x ) jo Symbol ya symbols se create kiya jaata hai. SymPy expressions symbols aur operations ke trees hote hain, jo tab tak evaluate nahi hote jab tak aap na kahein.
x, y = symbols( 'x y' ) # create two symbols
expr = x ** 2 + 2 * x + 1 # an expression tree, NOT a number
TREE KYU? Kyunki x**2 + 2*x + 1 ki abhi koi value nahi hai — SymPy structure store karta hai taaki baad mein isse factor karke ( x + 1 ) 2 bana sake.
Common mistake Steel-man: "
= se x = 5 define karo."
Yeh sahi lagta hai kyunki normal Python mein x mein koi value honi chahiye. Lekin tab x**2 sirf 25 hai — tumne algebra ko destroy kar diya. Fix: x = symbols('x') declare karo taaki woh unknown rahe. Values baad mein .subs(x, 5) se substitute karo.
Intuition Yeh chaar pencil moves hain
expand — sab kuch multiply out karo (brackets kholna).
factor — ulta (common structure dhundhna).
simplify — "jo sabse clean lage woh karo" (ek heuristic hai, unique nahi).
solve — woh values dhundho jo equation ko sach banaayein.
Worked example Quadratic solve karo — derive karo, memorize mat karo
x 2 − 5 x + 6 = 0 solve karo.
x = symbols( 'x' )
solve(x ** 2 - 5 * x + 6 , x) # -> [2, 3]
Yeh step kyu? SymPy internally ( x − 2 ) ( x − 3 ) factor karta hai; ek product zero hota hai tab hi jab koi factor zero ho, isliye x = 2 , 3 milta hai. Hum machine par trust karte hain lekin hum jaante hain kyun answer sahi hai.
Worked example Substitution + exact evaluation
e = (x + 1 ) ** 2
e.subs(x, sqrt( 2 )) # (sqrt(2) + 1)**2 -- still exact!
simplify(e.subs(x, sqrt( 2 ))) # 2*sqrt(2) + 3
Yeh step kyu? .subs symbol ko swap karta hai; SymPy sqrt(2) ko symbolic rakhta hai. N(...) ya .evalf() sirf tab 5.828... dega jab hum decimal chahenge.
f ′ ( x ) = lim h → 0 h f ( x + h ) − f ( x )
diff(f, x) isko symbolically compute karta hai known rules (chain, product, power) use karke — jo khud isi limit se derive hue hain .
Worked example Verify karo ki derivative IS the limit hai
x, h = symbols( 'x h' )
f = x ** 2
limit((f.subs(x, x + h) - f) / h, h, 0 ) # -> 2*x
diff(f, x) # -> 2*x (same!)
Yeh step kyu? Yeh scratch-se-derivation hai: hum power rule assume nahi karte, hum isse limit definition se recover karte hain, phir confirm karte hain ki diff agree karta hai.
∫ f d x antiderivative hai; integrate(f, x) ise deta hai (+ C nahi dikhata). Ek definite integral integrate(f, (x, a, b)) Fundamental Theorem apply karta hai: ∫ a b f = F ( b ) − F ( a ) .
Worked example Definite integral
integrate(x ** 2 , (x, 0 , 1 )) # -> 1/3 (exact rational!)
Yeh step kyu? ∫ 0 1 x 2 d x = [ x 3 /3 ] 0 1 = 1/3 . Numpy 0.3333... deta; SymPy exact 1/3 deta hai.
Intuition ODE kya poochtha hai
Ek ODE ek function ko uske derivatives se relate karta hai, jaise y ′′ + y = 0 . Solve karne ka matlab hai function y ( x ) dhundhna , koi number nahi. SymPy arbitrary constants C 1 , C 2 ke saath ek family return karta hai.
Worked example Harmonic oscillator, derived
y ′′ + y = 0 solve karo.
x = symbols( 'x' )
y = Function( 'y' )
dsolve(Eq(y(x).diff(x, 2 ) + y(x), 0 ), y(x))
# -> Eq(y(x), C1*sin(x) + C2*cos(x))
Yeh step kyu? y = e r x guess karo: tab r 2 e r x + e r x = 0 ⇒ r 2 + 1 = 0 ⇒ r = ± i . Complex roots ± i oscillations cos x , sin x dete hain. dsolve exactly yahi characteristic-equation logic karta hai.
Worked example Initial conditions
dsolve(Eq(y(x).diff(x) - 2 * y(x), 0 ), y(x),
ics = {y( 0 ): 3 }) # -> Eq(y(x), 3*exp(2*x))
Yeh step kyu? General solution C 1 e 2 x ; y ( 0 ) = 3 set karne se C 1 = 3 pin hota hai. Forecast-then-verify: run karne se pehle e 2 x growth predict karo — 2 seedha y ′ = 2 y se aaya.
Goal
Call
Note
unknown banana
x = symbols('x')
assumptions ke liye positive=True etc. use karo
brackets kholna
expand(e)
ulta karna
factor(e)
saaf karna
simplify(e)
heuristic
plug in karna
e.subs(x, v)
exact
decimal lena
e.evalf() / N(e)
derivative
diff(f, x) / diff(f,x,2)
order extra arg se
integral
integrate(f, x) ya (x,a,b)
limit
limit(f, x, 0)
equation solve karna
solve(Eq(l,r), x)
ODE
dsolve(Eq(...), y(x), ics=...)
Function chahiye
Common mistake Steel-man: "
solve ek number return karta hai jo main compute kar sakta hun."
Yeh sahi lagta hai kyunki solve problem finish karta lagta hai. Lekin solve ek list of symbolic roots return karta hai (often sqrt, I ke saath). Fix: sol[0] se index karo aur agar float chahiye toh .evalf() lagao.
Common mistake Steel-man: "
== se check karo ki do expressions equal hain."
Natural lagta hai. Lekin (x+1)**2 == x**2+2*x+1 False return karta hai — == structural identity check karta hai, mathematical equality nahi. Fix: simplify(a - b) == 0 ya a.equals(b) use karo.
Recall
symbols('x') kya deta hai aur sirf x = 5 kyun nahi?
Ek unknown symbolic object jo algebra preserve karta hai. x=5 sab kuch numbers mein collapse kar deta hai, us structure ko destroy karke jo tum manipulate karna chahte the.
Recall
diff(x**2, x) scratch se sahi kaise confirm karoge?
limit((( x+h)**2 - x**2)/h, h, 0) → 2*x, jo derivative ki limit definition se match karta hai.
Recall
(x+1)**2 == x**2+2*x+1 False kyun deta hai?
== structural identity hai, math equality nahi. simplify(a-b)==0 use karo.
Recall
dsolve kya form return karta hai aur constants kaise fix karte hain?
Ek general Eq(y(x), ...) C1, C2 ke saath; inhe pin karne ke liye ics={y(0): ...} supply karo.
Recall (Feynman, ek 12-saal ke bachche ko samjhao)
Normal calculators sirf numbers jaante hain — inhe √2 do aur yeh panic hokar 1.414... ban jaate hain. SymPy ek robot math teacher ki tarah hai jo "√2", "x", "π" symbols ko board par exactly likha rakhta hai aur wahi pencil rules follow karta hai jo tumne seekhe hain — brackets kholna, factor karna, differentiate karna — aur cheezein tabhi decimals mein badalta hai jab tum .evalf() se politely poochho. Toh yeh kabhi rounding se jhooth nahi bolta, aur tumhe ek formula deta hai jo tum padh sako.
"S.E.F.S.I.D.D." — S ymbols, E xpand, F actor, S olve, I ntegrate, D iff, D solve. ("Some Engineers Factor, Some Integrate, Derive, Dsolve.")
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
SymPy kya manipulate karta hai jo NumPy nahi karta? Exact symbolic objects (x, √2, π, 1/3) ko expression trees ki tarah, floats nahi.
Symbolic unknown x kaise banate hain? x = symbols('x').
SymPy mein ( x + 1 ) 2 expand karo? expand((x+1)**2) → x**2 + 2*x + 1.
x 2 − 5 x + 6 factor karo?factor(x**2-5*x+6) → (x-2)*(x-3).
x 2 − 5 x + 6 = 0 solve karo?solve(x**2-5*x+6, x) → [2, 3].
(x+1)² mein x=√2 substitute karo? ((x+1)**2).subs(x, sqrt(2)), exact rehta hai.
d/dx(x²) ki limit definition compute karo? limit(((x+h)**2-x**2)/h, h, 0) → 2*x.
x² ka 0 se 1 tak definite integral? integrate(x**2,(x,0,1)) → 1/3.
sin(x) ki order 6 tak Taylor series? sin(x).series(x,0,6) → x - x**3/6 + x**5/120 + O(x**6).
y''+y=0 solve karo? dsolve(Eq(y(x).diff(x,2)+y(x),0), y(x)) → C1*sin(x)+C2*cos(x).
Equality check ke liye == reliable kyun nahi hai? Yeh structural identity test karta hai; simplify(a-b)==0 ya a.equals(b) use karo.
Symbolic result se decimal kaise lein? .evalf() ya N(expr).
dsolve mein initial condition y(0)=3 kaise apply karein? ics={y(0): 3} pass karo.