Komplekse tall i Python | Sett 1 (introduksjon)

Ikke bare reelle tall Python kan også håndtere komplekse tall og tilhørende funksjoner ved å bruke filen 'cmath'. Komplekse tall har sine bruksområder i mange programmer relatert til matematikk og python gir nyttige verktøy for å håndtere og manipulere dem. Konvertering av reelle tall til komplekse tall Et komplekst tall er representert med ' x + yi '. Python konverterer de reelle tallene x og y til komplekse ved hjelp av funksjonen kompleks(xy) . Den virkelige delen kan nås ved hjelp av funksjonen virkelig() og imaginær del kan representeres ved bilde()

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  )   

Produksjon
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0  

En alternativ måte å initialisere et komplekst tall på  

Nedenfor er implementeringen av hvordan kan vi lage kompleks nr. uten å bruke kompleks() funksjon .

Python
   # An alternative way to initialize complex numbers'   # importing 'cmath' for complex number operations   import   cmath   # Initializing complex number   z   =   5  +  3  j   # 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  )   

Produksjon
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0  

Forklaring: Fase av komplekst tall Geometrisk er fasen til et komplekst tall vinkelen mellom den positive reelle aksen og vektoren som representerer et komplekst tall . Dette er også kjent som argumentet av et komplekst tall. Fase returneres vha fase() som tar et komplekst tall som argument. Utvalget av fase ligger fra -pi betyr +pi. dvs fra -3,14 til +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  ))   

Produksjon
The phase of complex number is: 3.141592653589793  

Konvertering fra polar til rektangulær form og omvendt Konvertering til polar gjøres ved hjelp av polar() som returnerer en par (rph) angir modul r og fase vinkel ph . modul kan vises ved hjelp av abs() og fase bruk fase() . Et komplekst tall konverteres til rektangulære koordinater ved å bruke rect(r ph) hvor r er modul og ph er fasevinkel . Den returnerer en verdi numerisk lik 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  )   

Produksjon
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)  


Komplekse tall i Python | Sett 2 (viktige funksjoner og konstanter)