Ei vain reaalilukuja Python pystyy käsittelemään myös kompleksilukuja ja niihin liittyviä toimintoja käyttämällä tiedostoa "cmath". Monimutkaiset luvut Niitä voidaan käyttää monissa matematiikkaan liittyvissä sovelluksissa, ja python tarjoaa hyödyllisiä työkaluja niiden käsittelemiseen ja manipulointiin. Reaalilukujen muuntaminen kompleksiluvuiksi Kompleksilukua edustaa ' x + yi '. Python muuntaa todelliset luvut x ja y komplekseiksi funktion avulla kompleksi(xy) . Varsinaiseen osaan pääsee käsiksi toiminnolla todellinen() ja kuvitteellinen osa voidaan esittää kuva() .
Python# Python code to demonstrate the working of # complex() real() and imag() # importing 'cmath' for complex number operations import cmath # Initializing real numbers x = 5 y = 3 # converting x and y into complex number z = complex(x y) # printing real and imaginary part of complex number print('The real part of complex number is:' z.real) print('The imaginary part of complex number is:' z.imag)
Lähtö
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Vaihtoehtoinen tapa alustaa kompleksiluku
Alla on toteutus siitä, kuinka voimme tehdä kompleksista no. käyttämättä kompleksi()-funktio .
Python# An alternative way to initialize complex numbers' # importing 'cmath' for complex number operations import cmath # Initializing complex number z = 5+3j # Print the parts of Complex No. print('The real part of complex number is : ' end='') print(z.real) print('The imaginary part of complex number is : ' end='') print(z.imag)
Lähtö
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Selitys: Kompleksiluvun vaihe Geometrisesti kompleksiluvun vaihe on positiivisen reaaliakselin ja kompleksilukua edustavan vektorin välinen kulma . Tämä tunnetaan myös nimellä argumentti kompleksiluvusta. Vaihe palautetaan käyttäen vaihe () joka ottaa kompleksiluvun argumentiksi. Vaihealue on alkaen -pi tarkoittaa +pi. eli alkaen -3,14 - +3,14 .
Python# importing 'cmath' for complex number operations import cmath # Initializing real numbers x = -1.0 y = 0.0 # converting x and y into complex number z = complex(x y) # printing phase of a complex number using phase() print('The phase of complex number is:' cmath.phase(z))
Lähtö
The phase of complex number is: 3.141592653589793
Muuntaminen napaisesta suorakaiteen muotoiseksi ja päinvastoin Muuntaminen polaariseksi tapahtuu käyttämällä napainen() joka palauttaa a pari (rph) mikä tarkoittaa moduuli r ja vaihe kulma ph . moduuli voidaan näyttää käyttämällä abs() ja vaihekäyttö vaihe () . Kompleksiluku muunnetaan suorakaiteen muotoisiksi koordinaateiksi käyttämällä rect (r ph) jossa r on moduuli ja ph on vaihekulma . Se palauttaa arvon, joka on numeerisesti yhtä suuri kuin r * (math.cos(ph) + math.sin(ph)*1j)
Python
# importing 'cmath' for complex number operations import cmath import math # Initializing real numbers x = 1.0 y = 1.0 # converting x and y into complex number z = complex(x y) # converting complex number into polar using polar() w = cmath.polar(z) # printing modulus and argument of polar complex number print('The modulus and argument of polar complex number is:' w) # converting complex number into rectangular using rect() w = cmath.rect(1.4142135623730951 0.7853981633974483) # printing rectangular form of complex number print('The rectangular form of complex number is:' w)
Lähtö
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Monimutkaiset numerot Pythonissa | Joukko 2 (tärkeät funktiot ja vakiot)