पायथन में कॉम्प्लेक्स नंबर्स बिल्ट-इन टाइप हैं, जिन्हें डायरेक्ट असाइनमेंट या complex() फ़ंक्शन से बनाया जा सकता है।
बनाना और बुनियादी गुण
>>> c = 3 + 6j
>>> type(c)
>>> c1 = complex(3, 6)
>>> c1
(3+6j)
एट्रिब्यूट्स
>>> c = 3 + 6j
>>> c.real # 3.0 (रीयल पार्ट)
>>> c.imag # 6.0 (इमेजिनरी पार्ट)
>>> c.conjugate() # (3-6j) कंजुगेट
गणितीय ऑपरेशन्स
>>> c1 = 3 + 6j
>>> c2 = 6 + 15j
>>> c1 + c2 # (9+21j)
>>> c1 - c2 # (-3-9j)
>>> c1 * c2 # (-72+81j)
>>> c1 / c2 # (0.4137...-0.0344...j)
नोट: कॉम्प्लेक्स नंबर्स तुलना ऑपरेटर्स (<, >, <=, >=) सपोर्ट नहीं करते - TypeError उठता है।
cmath मॉड्यूल: कॉम्प्लेक्स मैथ फ़ंक्शन्स
फेज (आर्गुमेंट)
import cmath, math, numpy
c = 4 + 4j
phase = cmath.phase(c) # 0.785... रेडियन
print(numpy.degrees(phase)) # 45.0 डिग्री
print(cmath.phase(-4-4j)) # -2.356... रेडियन
print(numpy.degrees(cmath.phase(-4-4j))) # -135.0 डिग्री
# math.atan2 से भी
print(math.atan2(2, 1)) # 1.107...
कॉन्स्टेंट्स
print(cmath.pi) # 3.14159...
print(cmath.e) # 2.71828...
print(cmath.tau) # 6.28318...
print(cmath.inf) # inf
print(cmath.infj) # infj
print(cmath.nan) # nan
print(cmath.nanj) # nanj
पावर और लॉग फ़ंक्शन्स
c = 1 + 2j
print(cmath.exp(c)) # e^c
print(cmath.log(c, 2)) # log2(c)
print(cmath.log10(c)) # log10(c)
print(cmath.sqrt(c)) # वर्गमूल
ट्रिग्नोमेट्रिक फ़ंक्शन्स
c = 2 + 4j
print(cmath.sin(c))
print(cmath.cos(c))
print(cmath.tan(c))
print(cmath.asin(c))
print(cmath.acos(c))
print(cmath.atan(c))
हाइपरबोलिक फ़ंक्शन्स
c = 2 + 4j
print(cmath.sinh(c))
print(cmath.cosh(c))
print(cmath.tanh(c))
print(cmath.asinh(c))
print(cmath.acosh(c))
print(cmath.atanh(c))
अनुप्रयोग
- इलेक्ट्रिकल इंजीनियरिंग (वोल्टेज/करंट फेजर)।
- सिग्नल प्रोसेसिंग, कंट्रोल सिस्टम्स।
- ज्यामिति, फ्रैक्टल्स (मैंडेलब्रॉट सेट)।
- क्वांटम मैकेनिक्स सिमुलेशन।
पायथन का बिल्ट-इन कॉम्प्लेक्स सपोर्ट वैज्ञानिक कंप्यूटिंग के लिए इसे शक्तिशाली बनाता है।