Kompleksie skaitļi Python | 1. komplekts (ievads)
Ne tikai reālie skaitļi Python var apstrādāt arī kompleksos skaitļus un ar tiem saistītās funkcijas, izmantojot failu “cmath”. Kompleksie skaitļi tos izmanto daudzās ar matemātiku saistītās lietojumprogrammās, un python nodrošina noderīgus rīkus, lai tos apstrādātu un apstrādātu. Reālu skaitļu pārvēršana kompleksos skaitļos Komplekss skaitlis tiek attēlots ar " x + yi '. Python pārvērš reālos skaitļus x un y kompleksos, izmantojot funkciju komplekss (xy) . Reālajai daļai var piekļūt, izmantojot funkciju īsts () un iedomāto daļu var attēlot ar attēls () .
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 )
Izvade
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Alternatīvs veids, kā inicializēt kompleksu skaitli
Zemāk ir īstenošana, kā mēs varam veikt kompleksu Nr. neizmantojot kompleksā() funkcija .
Python # An alternative way to initialize complex numbers' # importing 'cmath' for complex number operations import cmath # Initializing complex number z = 5 + 3 j # 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 )
Izvade
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Paskaidrojums: Kompleksā skaitļa fāze Ģeometriski kompleksā skaitļa fāze ir leņķis starp pozitīvo reālo asi un vektoru, kas attēlo kompleksu skaitli . Tas ir pazīstams arī kā arguments no kompleksā skaitļa. Fāze tiek atgriezta, izmantojot fāze () kas par argumentu ņem kompleksu skaitli. Fāzes diapazons ir no -pi nozīmē +pi. i., no -3,14 līdz +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 ))
Izvade
The phase of complex number is: 3.141592653589793
Pārveidojot no polāras formas uz taisnstūrveida formu un otrādi Pārvēršana uz polāro tiek veikta, izmantojot polārais () kas atgriež a pāris (rph) apzīmējot modulis r un fāze leņķis ph . moduli var parādīt, izmantojot abs () un fāzes izmantošana fāze () . Komplekss skaitlis tiek pārvērsts taisnstūra koordinātēs, izmantojot taisnais (r ph) kur r ir modulis un ph ir fāzes leņķis . Tas atgriež vērtību, kas skaitliski vienāda ar 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 )
Izvade
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Kompleksie skaitļi Python | 2. kopa (svarīgas funkcijas un konstantes)