Nombres complexos en Python | Set 1 (Introducció)

No només números reals, Python també pot gestionar nombres complexos i les seves funcions associades mitjançant el fitxer 'cmath'. Nombres complexos tenen els seus usos en moltes aplicacions relacionades amb les matemàtiques i Python proporciona eines útils per manejar-les i manipular-les. Convertir nombres reals en nombres complexos Un nombre complex es representa amb ' x + yi '. Python converteix els nombres reals x i y en complexos mitjançant la funció complex (xy) . Es pot accedir a la part real mitjançant la funció real () i la part imaginària es pot representar per imatge()

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  )   

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

Una forma alternativa d'inicialitzar un nombre complex  

A continuació es mostra la implementació de com podem fer complex no. sense utilitzar funció complex(). .

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  )   

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

Explicació: Fase del nombre complex Geomètricament la fase d'un nombre complex és la angle entre l'eix real positiu i el vector que representa un nombre complex . Això també es coneix com l'argument d'un nombre complex. La fase es torna utilitzant fase () que pren com a argument un nombre complex. El rang de fase es troba a partir de -pi significa +pi. és a dir de -3,14 a +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  ))   

Sortida
The phase of complex number is: 3.141592653589793  

Conversió de forma polar a rectangular i viceversa La conversió a polar es fa utilitzant polar () que retorna a parell (rph) denotant el mòdul r i fase angle ph . el mòdul es pot mostrar mitjançant abs () i utilitzant fases fase () . Un nombre complex es converteix en coordenades rectangulars utilitzant rect (r ph) on r és el mòdul i ph és l'angle de fase . Retorna un valor numèricament igual a 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  )   

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


Nombres complexos en Python | Conjunt 2 (funcions i constants importants)