Nielen reálne čísla Python dokáže spracovať aj komplexné čísla a súvisiace funkcie pomocou súboru 'cmath'. Komplexné čísla majú svoje využitie v mnohých aplikáciách súvisiacich s matematikou a python poskytuje užitočné nástroje na ich manipuláciu a manipuláciu. Prevod reálnych čísel na komplexné číslo Komplexné číslo predstavuje „ x + yi '. Python pomocou funkcie konvertuje reálne čísla x a y na komplexné komplex (xy) . K skutočnej časti je možné pristupovať pomocou funkcie skutočný() a imaginárnu časť možno reprezentovať obrázok() .
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
Alternatívny spôsob inicializácie komplexného čísla
Nižšie je uvedená implementácia toho, ako môžeme urobiť komplex č. bez použitia komplexná() funkcia .
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
Vysvetlenie: Fáza komplexného čísla Geometricky je fáza komplexného čísla uhol medzi kladnou reálnou osou a vektorom reprezentujúcim komplexné číslo . Toto je tiež známe ako argument komplexného čísla. Fáza sa vráti pomocou fáza() ktorý berie ako argument komplexné číslo. Rozsah fáz leží od -pi znamená +pi. t.j -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
Premena z polárneho na obdĺžnikový tvar a naopak Konverzia na polárne sa vykonáva pomocou polárny() ktorý vracia a pár (rph) označujúci modul r a fáza uhol ph . modul je možné zobraziť pomocou abs() a použitie fázy fáza() . Komplexné číslo sa prevedie na pravouhlé súradnice pomocou rect(r ph) kde r je modul a ph je fázový uhol . Vráti hodnotu, ktorá sa číselne 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 Pythone | Sada 2 (dôležité funkcie a konštanty)