Niet alleen reële getallen Python kan ook omgaan met complexe getallen en de bijbehorende functies met behulp van het bestand 'cmath'. Complexe getallen worden gebruikt in veel toepassingen die verband houden met wiskunde en Python biedt handige hulpmiddelen om ze te hanteren en te manipuleren. Reële getallen omzetten naar complexe getallen Een complex getal wordt weergegeven door ' x + yi '. Python converteert de reële getallen x en y naar complex met behulp van de functie complex(xy) . Het echte deel is toegankelijk via de functie echt() en denkbeeldig deel kan worden weergegeven door afbeelding() .
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)
Uitvoer
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0
Een alternatieve manier om een complex getal te initialiseren
Hieronder vindt u de implementatie van hoe we complexe nee kunnen maken. zonder te gebruiken complexe() functie .
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)
Uitvoer
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Uitleg: Fase van complex getal Geometrisch gezien is de fase van een complex getal de hoek tussen de positieve reële as en de vector die een complex getal vertegenwoordigt . Dit staat ook bekend als het betoog van een complex getal. Fase wordt geretourneerd met behulp van fase() waarbij een complex getal als argument moet worden gebruikt. Het fasebereik ligt van -pi betekent +pi. d.w.z. van -3,14 tot +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))
Uitvoer
The phase of complex number is: 3.141592653589793
Omzetten van polaire naar rechthoekige vorm en omgekeerd Conversie naar polair gebeurt met behulp van polair() die een retourneert paar(rph) ter aanduiding van de modulus r en fase hoek ph . modulus kan worden weergegeven met buikspieren() en fasegebruik fase() . Een complex getal wordt omgezet in rechthoekige coördinaten met behulp van rect(r ph) waar r is modulus En ph is fasehoek . Het retourneert een waarde die numeriek gelijk is aan r * (wiskunde.cos(ph) + wiskunde.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)
Uitvoer
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j)
Complexe getallen in Python | Set 2 (belangrijke functies en constanten)