Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Complex Numbers ใน Python: การคำนวณเลข מרוכב และโมดูล cmath

Python มี built-in type complex สำหรับเลขซ้อน (Complex Numbers) รูปแบบ x + yj โดย x = real part, y = imaginary part, j = √(-1)

สร้าง Complex Number

# Literal syntax
z = 2 + 3j
print(type(z))  # 

# complex() constructor
z1 = complex(9, 5)       # 9+5j
z2 = complex(-6, -2.5)   # -6-2.5j
z3 = complex(1.5j, 2.5j) # -2.5+1.5j (note: complex(real, imag))
z4 = complex(15)         # 15+0j (real only)
z5 = complex('51')       # 51+0j (from string)

Attributes และ Methods

z = 2 + 3j
print(z.real)        # 2.0 - real part
print(z.imag)        # 3.0 - imaginary part (ไม่รวม j)
print(z.conjugate()) # (2-3j) - conjugate: เปลี่ยนเครื่องหมาย imaginary part

Arithmetic Operations

Operation ตัวอย่าง ผลลัพธ์
Addition(6+4j) + (3+6j)9+10j
Subtraction(6+4j) - (3+6j)3-2j
Multiplication(6+4j) * (3+2j)10+24j
Division(2+4j) / (1-2j)-1.2+1.6j

รายละเอียดการคูณ (FOIL method)

(6+4j) * (3+2j)
= 6*3 + 6*2j + 4j*3 + 4j*2j
= 18 + 12j + 12j + 8*(-1)   # j² = -1
= 18 + 24j - 8
= 10 + 24j

รายละเอียดการหาร (Multiply by Conjugate)

(2+4j) / (1-2j)
= (2+4j) * (1+2j) / (1-2j)*(1+2j)  # คูณ conjugate
= (2+4j+4j+8*(-1)) / (1 - 4*(-1))
= (2+8j-8) / (1+4)
= (-6+8j) / 5
= -1.2 + 1.6j

โมดูล cmath: ฟังก์ชันคณิตศาสตร์สำหรับ Complex

math module ใช้กับ float เท่านั้น สำหรับ complex ใช้ cmath

Polar Coordinates (รูปแบบขั้ว)

Complex number z = x + yj → Polar: (r, φ) โดย
r = |z| = modulus = abs(z)
φ = phase angle (radians) = cmath.phase(z)

import cmath

z = 2 + 4j

# Polar representation
r, phi = cmath.polar(z)
print(f"Modulus (r): {r}")           # 4.472
print(f"Phase (φ): {phi} radians")   # 1.107 radians

# ตรวจสอบด้วย abs() และ phase()
print(abs(z))           # 4.472
print(cmath.phase(z))   # 1.107

# กลับมาสู่ Rectangular (Cartesian)
z2 = cmath.rect(r, phi)
print(z2)  # (2+4j)

Trigonometric Functions

z = 2 + 4j
phi = cmath.phase(z)

print(cmath.sin(phi))   # (0.894+0j)
print(cmath.cos(phi))
print(cmath.tan(phi))

# Inverse trigonometric
print(cmath.asin(0.5))
print(cmath.acos(0.5))
print(cmath.atan(1))

Exponential และ Logarithmic

z = 1 + 2j

print(cmath.exp(z))      # e^z = -1.131+2.472j
print(cmath.log(z))      # natural log
print(cmath.log10(z))    # log base 10
print(cmath.sqrt(z))     # square root = 1.272+0.786j

Hyperbolic Functions

print(cmath.sinh(z))
print(cmath.cosh(z))
print(cmath.tanh(z))

ตารางสรุปฟังก์ชัน cmath หลัก

ฟังก์ชัน คำอธิบาย
polar(z)คืน (r, φ) - polar coordinates
rect(r, φ)คืน complex จาก polar
phase(z)Phase angle ใน radians (-π to π)
abs(z)Modulus (built-in)
sin, cos, tanTrigonometric
asin, acos, atanInverse trigonometric
sinh, cosh, tanhHyperbolic
exp(z)e^z
log(z[, base])Natural log (base e)
log10(z)Log base 10
sqrt(z)Square root
isclose(a, b)เปรียบเทียบ complex ใกล้เคียง

การประยุกต์ใช้

  • Signal Processing: FFT, filters, frequency domain
  • Electrical Engineering: Impedance, AC circuits, phasors
  • Quantum Computing: Quantum states, gates
  • Control Systems: Transfer functions, stability analysis
  • Fractals: Mandelbrot set, Julia sets

สรุป

  • Python รองรับ complex numbers เป็น built-in type
  • ใช้ j สำหรับ imaginary unit (ไม่ใช่ i)
  • Arithmetic operations ทำงานโดยอัตโนมัติ
  • ใช้ cmath module สำหรับ advanced math functions
  • Polar ↔ Rectangular conversion ด้วย polar() และ rect()