logo

Komplexní čísla v Pythonu | Sada 1 (úvod)

Nejen reálná čísla Python umí také pracovat s komplexními čísly as nimi spojenými funkcemi pomocí souboru 'cmath'. Komplexní čísla mají své použití v mnoha aplikacích souvisejících s matematikou a python poskytuje užitečné nástroje pro manipulaci a manipulaci s nimi. Převod reálných čísel na komplexní Komplexní číslo je reprezentováno ' x + yi '. Python pomocí funkce převádí reálná čísla x a y na komplexní komplex (xy) . Ke skutečné části lze přistupovat pomocí funkce nemovitý() a imaginární část může být reprezentována obrázek()

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) 

Výstup
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0 

Alternativní způsob inicializace komplexního čísla  



Níže je implementace toho, jak můžeme udělat komplexní ne. bez použití komplexní() funkce .

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) 

Výstup
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0 

Vysvětlení: Fáze komplexního čísla Geometricky fáze komplexního čísla je úhel mezi kladnou reálnou osou a vektorem reprezentujícím komplexní číslo . Toto je také známé jako argument komplexního čísla. Fáze je vrácena pomocí fáze() který bere jako argument komplexní číslo. Rozsah fáze leží od -pi znamená +pi. tj. od -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)) 

Výstup
The phase of complex number is: 3.141592653589793 

Převod z polárního na obdélníkový tvar a naopak Převod na polární se provádí pomocí polární() která vrací a pár (rph) označující modul r a fáze úhel ph . modul lze zobrazit pomocí abs() a použití fáze fáze() . Komplexní číslo se převede na pravoúhlé souřadnice pomocí rect(r ph) kde r je modul a ph je fázový úhel . Vrací hodnotu, která se číselně rovná 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) 

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


Komplexní čísla v Pythonu | Sada 2 (Důležité funkce a konstanty)