Komplexa tal i Python | Set 1 (introduktion)
Inte bara reella tal Python kan också hantera komplexa tal och dess associerade funktioner med hjälp av filen 'cmath'. Komplexa siffror har sina användningsområden i många applikationer relaterade till matematik och python ger användbara verktyg för att hantera och manipulera dem. Konvertera reella tal till komplexa tal Ett komplext tal representeras av ' x + yi '. Python omvandlar de reella talen x och y till komplexa med hjälp av funktionen komplex(xy) . Den verkliga delen kan nås med funktionen verklig() och imaginär del kan representeras av bild() .
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 )
Produktion
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Ett alternativt sätt att initiera ett komplext tal
Nedan är implementeringen av hur kan vi göra komplexa nr. utan att använda komplex() funktion .
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 )
Produktion
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Förklaring: Fas av komplexa tal Geometriskt är fasen för ett komplext tal vinkeln mellan den positiva reella axeln och vektorn som representerar ett komplext tal . Detta är också känt som argumentet av ett komplext tal. Fas returneras med hjälp av fas() som tar ett komplext tal som argument. Fasintervallet ligger från -pi betyder +pi. dvs från -3,14 till +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 ))
Produktion
The phase of complex number is: 3.141592653589793
Konvertering från polär till rektangulär form och vice versa Konvertering till polär görs med hjälp av polär() som returnerar en par (rph) betecknar modul r och fas vinkel ph . modul kan visas med hjälp av abs() och fasanvändning fas() . Ett komplext tal omvandlas till rektangulära koordinater med hjälp av rect(r ph) där r är modul och ph är fasvinkeln . Den returnerar ett värde numeriskt lika med 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 )
Produktion
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Komplexa tal i Python | Set 2 (viktiga funktioner och konstanter)