logo

Комплексні числа в Python | Набір 1 (введення)

Не лише дійсні числа. Python також може обробляти комплексні числа та пов’язані з ними функції за допомогою файлу cmath. Комплексні числа використовуються в багатьох додатках, пов’язаних з математикою, і Python надає корисні інструменти для обробки та маніпулювання ними. Перетворення дійсних чисел у комплексне Комплексне число представлене ' x + yi '. Python перетворює дійсні числа x і y на комплексні за допомогою функції комплекс (xy) . До дійсної частини можна отримати доступ за допомогою функції справжній() і уявна частина може бути представлена image()

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) 

Вихід
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0 

Альтернативний спосіб ініціалізації комплексного числа  



Нижче наведено реалізацію того, як ми можемо зробити комплекс №. без використання функція complex(). .

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) 

Вихід
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0 

Пояснення: Фаза комплексного числа Геометрично фаза комплексного числа є кут між додатною дійсною віссю та вектором, що представляє комплексне число . Це також відомо як аргумент комплексного числа. Фаза повертається за допомогою фаза() яка приймає комплексне число як аргумент. Діапазон фази лежить від -pi означає +pi. тобто від від -3,14 до +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)) 

Вихід
The phase of complex number is: 3.141592653589793 

Перетворення полярної форми в прямокутну і навпаки Перетворення в полярний здійснюється за допомогою полярний() який повертає a пара (rph) позначаючи модуль r і фаза кут ph . модуль можна відобразити за допомогою абс() і використання фази фаза() . Комплексне число перетворюється на прямокутні координати за допомогою прямий (r ph) де r — модуль і ph - фазовий кут . Він повертає значення, чисельно дорівнює 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) 

Вихід
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j) 


Комплексні числа в Python | Набір 2 (Важливі функції та константи)