logo

Kompleksni brojevi u Pythonu | Set 1 (Uvod)

Ne samo s realnim brojevima, Python također može rukovati složenim brojevima i njima pridruženim funkcijama pomoću datoteke 'cmath'. Kompleksni brojevi koriste se u mnogim aplikacijama povezanim s matematikom, a python pruža korisne alate za rukovanje i manipuliranje njima. Pretvaranje realnih brojeva u kompleksne brojeve Kompleksni broj je predstavljen sa ' x + yi '. Python pretvara realne brojeve x i y u složene pomoću funkcije kompleks(xy) . Realnom dijelu se može pristupiti pomoću funkcije stvaran() a imaginarni dio može se prikazati slika()

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) 

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

Alternativni način inicijalizacije kompleksnog broja  



Ispod je implementacija kako možemo napraviti kompleks br. bez korištenja funkcija 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) 

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

Obrazloženje: Faza kompleksnog broja Geometrijski faza kompleksnog broja je kut između pozitivne realne osi i vektora koji predstavlja kompleksni broj . Ovo je također poznato kao argument kompleksnog broja. Faza se vraća pomoću faza() koji kao argument uzima kompleksan broj. Raspon faza leži od -pi znači +pi. tj. od -3,14 do +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)) 

Izlaz
The phase of complex number is: 3.141592653589793 

Pretvaranje iz polarnog u pravokutni oblik i obrnuto Pretvorba u polarnu se vrši pomoću polarni() koji vraća a par (rph) označavajući modul r i faza kut ph . modul se može prikazati pomoću trbušnjaci () i faza pomoću faza() . Složeni broj pretvara se u pravokutne koordinate korištenjem pravo (r ph) gdje r je modul i ph je fazni kut . Vraća vrijednost brojčano jednaku 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) 

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


Kompleksni brojevi u Pythonu | Skup 2 (Važne funkcije i konstante)