Lesson - Module Special
CMATH MODULE:
COMPLEX NUMBERS ASAAN SE
math module sirf real numbers par kaam karta hai - math.sqrt(-1) chalao to seedha error. Lekin kabhi-kabhi answer negative bhi ho sakta hai (jaise electrical engineering mein). Tab aata hai cmath module - complex number version of math.
WHY: math module kab kyun fail karta hai
import math
import cmath
print(math.sqrt(16)) # 4.0 - happy
print(math.sqrt(-1)) # ValueError! math real numbers tak simit hai
print(cmath.sqrt(-1)) # 1j - imaginary unit i (Python mein j)
print(cmath.sqrt(4)) # (2+0j) - answer complex type meinMathematics mein sqrt(-1) = i (imaginary unit). Python ise j likhta hai: 1j. cmath har answer ko complex number (a + bj) mein deta hai - chahe real ho ya imaginary.
Real numbers ke liye. Negative root par ValueError.
Complex numbers ke liye. Har answer (a+bj) format mein.
Maths ka i Python mein j hai: 3+4j.
Complex number basics (30 second mein)
z = 3 + 4j # complex number: real=3, imag=4
print(type(z)) # <class 'complex'>
print(z.real) # 3.0
print(z.imag) # 4.0
print(z.conjugate())# (3-4j)
# Direct bhi bana sakte ho
z2 = complex(3, 4) # (3+4j)
z3 = complex(5) # (5+0j)cmath ki main functions
| Function | Kya karta hai | Example |
|---|---|---|
cmath.sqrt(z) | Complex square root | cmath.sqrt(-4) = 2j |
cmath.log(z) | Natural log (complex) | cmath.log(1j) = 1.5708j |
cmath.log10(z) | Base-10 log | cmath.log10(-100) |
cmath.exp(z) | e^z (complex power) | cmath.exp(1j) = 0.54+0.84j |
cmath.sin(z), cos(z), tan(z) | Trigonometry on complex | cmath.sin(1j) = 1.175j |
cmath.phase(z) | Angle (radians) - argument | cmath.phase(3+4j) = 0.927 |
cmath.polar(z) | (r, theta) - magnitude + angle | cmath.polar(3+4j) = (5.0, 0.927) |
cmath.rect(r, theta) | Polar se wapas a+bj | cmath.rect(5, 0.927) = (3+4j) |
import cmath
print(cmath.sqrt(-4)) # 2j
print(cmath.exp(1j)) # (0.5403023058681398+0.8414709848078965j)
print(cmath.log(1j)) # 1.5707963267948966j
# Magnitude aur angle (phasor form)
z = 3 + 4j
print(abs(z)) # 5.0 - magnitude (sqrt(3^2+4^2))
print(cmath.phase(z)) # 0.9272952180016122 radians
print(cmath.polar(z)) # (5.0, 0.9272952180016122)
# Wapas rectangular form
print(cmath.rect(5, 0.9272952180016122)) # (3.0000000000000004+4j)phase(), polar(), rect() - teeno ek hi baat ke 3 chehre
Har complex number z = a + bj ko 2 tarike se likh sakte ho: rectangular (a + bj) aur polar (magnitude r, angle theta). cmath teeno conversion deta hai:
abs(z)yacmath.phase(z)- alag alag nikaalocmath.polar(z)- ek saath(r, theta)tuplecmath.rect(r, theta)- wapasa+bj
Real use: electrical engineering mein AC circuits (phasors), signal processing mein frequency domain - sab polar form se describe hote hain.
Kab math, kab cmath - decision rule
- Answer guaranteed real hai (probability, statistics, normal geometry) ->
math. - Negative number ka root/log lena hai ->
cmath. - Complex result chahiye par real output format mein ->
cmath.sqrt(x).realyaabs(). - Comparisons: complex numbers mein
<,>nahi chalte (sirf==) - magnitude lo pehle.
import cmath, math
# Real output chahiye to .real lagao
r = cmath.sqrt(-9).real # 0.0 (principal root ka real part)
r2 = abs(cmath.sqrt(-9)) # 3.0 - magnitude (zyada useful)
# Complex compare kabhi mat karo - pehle magnitude
z1, z2 = 3+4j, 1+1j
# z1 > z2 -> TypeError
print(abs(z1) > abs(z2)) # True - sahi tarikaCommon mistakes
math.sqrt(-1)par error aata hai - seedhacmath.sqrtpe switch karo.cmath.sqrt(4)=(2+0j),2.0nahi - type complex hamesha. Number chahiye to.realyaabs().(3+4j) > (1+2j)TypeError dega - complex ordering define hi nahi hai.cmath.phaseradians deta hai - degrees chahiye tomath.degrees()se convert karo.
Math module ka deep version chahiye to /learn/ par aur modules dekho - aur practice ke liye Python course bhi hai.