No sólo números reales, Python también puede manejar números complejos y sus funciones asociadas utilizando el archivo 'cmath'. Números complejos Tienen sus usos en muchas aplicaciones relacionadas con las matemáticas y Python proporciona herramientas útiles para manejarlas y manipularlas. Convertir números reales a números complejos Un número complejo está representado por ' x+yi '. Python convierte los números reales xey en complejos usando la función complejo(xy) . Se puede acceder a la parte real mediante la función real() y la parte imaginaria se puede representar por imagen() .
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)
Producción
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Una forma alternativa de inicializar un número complejo
A continuación se muestra la implementación de cómo podemos hacer que el no sea complejo. sin usar función compleja () .
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)
Producción
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Explicación: Fase de número complejo Geométricamente la fase de un número complejo es la ángulo entre el eje real positivo y el vector que representa un número complejo . Esto también se conoce como el argumento de un número complejo. La fase se devuelve usando fase() que toma un número complejo como argumento. El rango de fase va desde -pi significa +pi. es decir 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))
Producción
The phase of complex number is: 3.141592653589793
Convertir de forma polar a rectangular y viceversa La conversión a polar se realiza usando polar() que devuelve un par(rph) denotando el módulo r y fase ángulo ph . El módulo se puede mostrar usando abs() y fase usando fase() . Un número complejo se convierte en coordenadas rectangulares usando recto(r ph) dónde r es módulo y ph es el ángulo de fase . Devuelve un valor numéricamente 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)
Producción
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 complejos en Python | Conjunto 2 (funciones y constantes importantes)