logo

Kompleksna števila v Pythonu | 1. sklop (uvod)

Ne samo realnih števil Python lahko obravnava tudi kompleksna števila in z njimi povezane funkcije z uporabo datoteke 'cmath'. Kompleksna števila se uporabljajo v številnih aplikacijah, povezanih z matematiko, in python nudi uporabna orodja za njihovo obdelavo in manipulacijo. Pretvarjanje realnih števil v kompleksno število Kompleksno število je predstavljeno z ' x + yi '. Python s funkcijo pretvori realna števila x in y v kompleksna kompleks (xy) . Do realnega dela lahko dostopate s funkcijo pravi() in imaginarni del je lahko predstavljen z 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) 

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

Alternativni način inicializacije kompleksnega števila  



Spodaj je izvedba, kako lahko naredimo kompleks št. brez uporabe 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) 

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

Pojasnilo: Faza kompleksnega števila Geometrično je faza kompleksnega števila kot med pozitivno realno osjo in vektorjem, ki predstavlja kompleksno število . To je znano tudi kot argument kompleksnega števila. Faza se vrne z uporabo faza() ki kot argument vzame kompleksno število. Razpon faze je od -pi pomeni +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)) 

Izhod
The phase of complex number is: 3.141592653589793 

Pretvarjanje iz polarne v pravokotno obliko in obratno Pretvorba v polar se izvede z uporabo polarni() ki vrne a par (rph) ki označuje modul r in faza kot ph . modul lahko prikažete z uporabo abs() in fazno uporabo faza() . Kompleksno število pretvori v pravokotne koordinate z uporabo rect(r ph) kjer r je modul in ph je fazni kot . Vrne vrednost, ki je številčno enaka 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) 

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


Kompleksna števila v Pythonu | 2. sklop (pomembne funkcije in konstante)