Python kann nicht nur reelle Zahlen, sondern auch komplexe Zahlen und die damit verbundenen Funktionen mithilfe der Datei „cmath“ verarbeiten. Komplexe Zahlen haben ihre Verwendung in vielen Anwendungen im Zusammenhang mit der Mathematik und Python bietet nützliche Werkzeuge, um sie zu handhaben und zu manipulieren. Reelle Zahlen in komplexe Zahlen umwandeln Eine komplexe Zahl wird dargestellt durch ' x + yi '. Python wandelt die reellen Zahlen x und y mithilfe der Funktion in komplexe um komplex(xy) . Mit der Funktion kann auf den Realteil zugegriffen werden real() und der Imaginärteil kann dargestellt werden durch imag() .
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)
Ausgabe
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Eine alternative Möglichkeit, eine komplexe Zahl zu initialisieren
Nachfolgend finden Sie die Implementierung, wie wir eine komplexe Nummer erstellen können. ohne zu verwenden complex()-Funktion .
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)
Ausgabe
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Erläuterung: Phase der komplexen Zahl Geometrisch gesehen ist die Phase einer komplexen Zahl die Winkel zwischen der positiven reellen Achse und dem Vektor, der eine komplexe Zahl darstellt . Dies wird auch als bezeichnet das Argument einer komplexen Zahl. Phase wird mit zurückgegeben Phase() welches eine komplexe Zahl als Argument annimmt. Der Phasenbereich liegt bei -pi bedeutet +pi. d.h. von -3,14 bis +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))
Ausgabe
The phase of complex number is: 3.141592653589793
Konvertieren von Polarform in Rechteckform und umgekehrt Die Umrechnung in Polar erfolgt mit Polar() was a zurückgibt Paar(rph) bezeichnet die Modul r und Phase Winkel ph . Modul kann mit angezeigt werden abs() und Phasennutzung Phase() . Eine komplexe Zahl wird mithilfe von in rechtwinklige Koordinaten umgewandelt rect(r ph) Wo r ist Modul Und ph ist der Phasenwinkel . Es wird ein numerischer Wert zurückgegeben, der gleich ist 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)
Ausgabe
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Komplexe Zahlen in Python | Satz 2 (Wichtige Funktionen und Konstanten)