Nu numai numerele reale Python poate gestiona și numere complexe și funcțiile asociate cu ajutorul fișierului „cmath”. Numerele complexe își au utilizările în multe aplicații legate de matematică și python oferă instrumente utile pentru a le manipula și manipula. Conversia numerelor reale în număr complex Un număr complex este reprezentat prin ' x + yi '. Python convertește numerele reale x și y în complexe folosind funcția complex(xy) . Partea reală poate fi accesată folosind funcția real() iar partea imaginară poate fi reprezentată prin imagine() .
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)
Ieșire
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
O modalitate alternativă de a inițializa un număr complex
Mai jos este implementarea cum putem face complexul nr. fără a folosi funcția complex(). .
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)
Ieșire
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Explicaţie: Faza numărului complex Geometric, faza unui număr complex este unghiul dintre axa reală pozitivă și vectorul care reprezintă un număr complex . Acest lucru este cunoscut și ca argumentul a unui număr complex. Faza este returnată folosind fază() care ia ca argument un număr complex. Gama de faze este de la -pi înseamnă +pi. adică de la -3,14 până la +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))
Ieșire
The phase of complex number is: 3.141592653589793
Conversia de la forma polara la forma dreptunghiulara si invers Conversia la polar se face folosind polar() care returnează a pereche (rph) denotând pe modulul r si faza unghiul ph . modulul poate fi afișat folosind abs() și fază folosind fază() . Un număr complex se transformă în coordonate dreptunghiulare utilizând rect(r ph) unde r este modulul şi ph este unghiul de fază . Returnează o valoare egală numeric cu 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)
Ieșire
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Numere complexe în Python | Setul 2 (Funcții și constante importante)