Números Complexos em Python | Conjunto 1 (Introdução)

Não apenas números reais, o Python também pode lidar com números complexos e suas funções associadas usando o arquivo 'cmath'. Números complexos têm sua utilização em muitas aplicações relacionadas à matemática e python fornece ferramentas úteis para manipulá-los e manipulá-los. Convertendo números reais em números complexos Um número complexo é representado por ' x + sim '. Python converte os números reais xey em complexos usando a função complexo (xy) . A parte real pode ser acessada usando a função real() e a parte imaginária pode ser representada por imagem()

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  )   

Saída
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0  

Uma maneira alternativa de inicializar um número complexo  

Abaixo está a implementação de como podemos fazer um número complexo. sem usar função complexa() .

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  )   

Saída
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0  

Explicação: Fase do número complexo Geometricamente, a fase de um número complexo é a ângulo entre o eixo real positivo e o vetor que representa um número complexo . Isso também é conhecido como o argumento de um número complexo. A fase é retornada usando fase() que toma um número complexo como argumento. A faixa de fase varia de -pi significa + pi. ou seja, 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  ))   

Saída
The phase of complex number is: 3.141592653589793  

Convertendo da forma polar para retangular e vice-versa A conversão para polar é feita usando polar() que retorna um par (rph) denotando o módulo r e fase ângulo ph . módulo pode ser exibido usando abdômen() e fase usando fase() . Um número complexo é convertido em coordenadas retangulares usando reto (r ph) onde r é módulo e ph é ângulo de fase . Ele retorna um valor numericamente igual a r * (matemática.cos(ph) + matemática.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  )   

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


Números Complexos em Python | Conjunto 2 (funções e constantes importantes)