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.

? 15 min Beginner+Prerequisite: Numbers

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 mein

Mathematics 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.

MATH

Real numbers ke liye. Negative root par ValueError.

CMATH

Complex numbers ke liye. Har answer (a+bj) format mein.

PYTHON = J

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

FunctionKya karta haiExample
cmath.sqrt(z)Complex square rootcmath.sqrt(-4) = 2j
cmath.log(z)Natural log (complex)cmath.log(1j) = 1.5708j
cmath.log10(z)Base-10 logcmath.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 complexcmath.sin(1j) = 1.175j
cmath.phase(z)Angle (radians) - argumentcmath.phase(3+4j) = 0.927
cmath.polar(z)(r, theta) - magnitude + anglecmath.polar(3+4j) = (5.0, 0.927)
cmath.rect(r, theta)Polar se wapas a+bjcmath.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:

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

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 tarika

Common mistakes

cmath clear?

Math module ka deep version chahiye to /learn/ par aur modules dekho - aur practice ke liye Python course bhi hai.

Popular courses: Python Course in Lucknow · Data Science Course in Lucknow · AI Course in Lucknow · Power BI Course · Data Analytics Course