Python中的複數?
複數是由實數構成的。Python 複數可以透過直接賦值語句或使用 complex() 函式建立。
複數主要用於需要使用兩個實數的地方。例如,由電壓 (V) 和電流 (C) 定義的電路,以及在幾何、科學計算和微積分中的應用。
語法
complex([real[, imag]])
在 Python 中建立一個簡單的複數
>>> c = 3 +6j >>> print(type(c)) <class 'complex'> >>> print(c) (3+6j) >>> >>> c1 = complex(3,6) >>> print(type(c1)) <class 'complex'> >>> print(c1) (3+6j)
從上面的結果可以看出,Python 複數的型別為 complex。每個複數都包含一個實部和一個虛部。
Python 複數 - 屬性和函式
>>> #Complex Number:
>>> c = (3 + 6j)
>>>
>>> #Real Part of complex number
>>> print('Complex Number: Real Part is = ', c. real)
Complex Number: Real Part is = 3.0
>>>
>>> #Imaginary Part of complex number
>>> print('Complex Number: Imaginary Part is = ', c. imag)
Complex Number: Imaginary Part is = 6.0
>>>
>>> #Conjugate of complex number
>>> print('Complex Number: conjugate Part = ', c. conjugate())
Complex Number: conjugate Part = (3-6j)複數的數學計算
我們可以對複數進行簡單的數學計算
>>> #first complex number
>>> c1 = 3 + 6j
>>> #Second complex number
>>> c2 = 6 + 15j
>>>
>>> #Addition
>>> print("Addition of two complex number =", c1 + c2)
Addition of two complex number = (9+21j)
>>>
>>> #Subtraction
>>> print("Subtraction of two complex number =", c1 - c2)
Subtraction of two complex number = (-3-9j)
>>>
>>> #Multiplication
>>> print("Multiplication of two complex number =", c1 * c2)
Multiplication of two complex number = (-72+81j)
>>>
>>> #Division
>>> print("Division of two complex number =", c1 / c2)
Division of two complex number = (0.4137931034482759-0.03448275862068964j)但是,複數不支援比較運算子,例如 <, >, <=, =>,否則會引發 TypeError 訊息。
>>> c2 <= c2 Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> c2 <= c2 TypeError: '<=' not supported between instances of 'complex' and 'complex'
Python cmath 模組
Python cmath 模組提供了對複數數學函式的訪問。讓我們看看使用 math 模組函式的一些重要的複數特性。
複數的相位
複數的相位是實軸與表示虛部的向量之間的角度。
math 和 cmath 模組返回的相位以弧度表示,我們使用 numpy.degrees() 函式將其轉換為度數。
import cmath, math, numpy
c = 4+ 4j
# phase
phase = cmath.phase(c)
print('4+ 4j Phase =', phase)
print('Phase in Degrees =', numpy.degrees(phase))
print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j)))
# we can get phase using math.atan2() function too
print('Complex number phase using math.atan2() =', math.atan2(2, 1))結果
4+ 4j Phase = 0.7853981633974483 Phase in Degrees = 45.0 -4-4j Phase = -2.356194490192345 radians. Degrees = -135.0 Complex number phase using math.atan2() = 1.1071487177940904
cmath 模組常量
cmath 模組中有一些常量用於複數計算
import cmath
print('π =', cmath.pi)
print('e =', cmath.e)
print('tau =', cmath.tau)
print('Positive infinity =', cmath.inf)
print('Positive Complex infinity =', cmath.infj)
print('NaN =', cmath.nan)
print('NaN Complex =', cmath.nanj)結果
π = 3.141592653589793 e = 2.718281828459045 tau = 6.283185307179586 Positive infinity = inf Positive Complex infinity = infj NaN = nan NaN Complex = nanj
冪函式和對數函式
cmath() 模組提供了一些有用的函式,用於對數和冪運算
import cmath
c = 1 + 2j
print('e^c =', cmath.exp(c))
print('log2(c) =', cmath.log(c, 2))
print('log10(c) =', cmath.log10(c))
print('sqrt(c) =', cmath.sqrt(c))結果
e^c = (-1.1312043837568135+2.4717266720048188j) log2(c) = (1.1609640474436813+1.5972779646881088j) log10(c) = (0.3494850021680094+0.480828578784234j) sqrt(c) = (1.272019649514069+0.7861513777574233j)
三角函式
import cmath
c = 2 + 4j
print('arc sine value:\n ', cmath.asin(c))
print('arc cosine value :\n', cmath.acos(c))
print('arc tangent value of complex number c :\n', cmath.atan(c))
print('sine value:\n', cmath.sin(c))
print('cosine value:\n', cmath.cos(c))
print('tangent value:\n', cmath.tan(c))結果
arc sine value: (0.4538702099631225+2.198573027920936j) arc cosine value : (1.1169261168317741-2.198573027920936j) arc tangent value of complex number c : (1.4670482135772953+0.20058661813123432j) sine value: (24.83130584894638-11.356612711218174j) cosine value: (-11.36423470640106-24.814651485634187j) tangent value: (-0.0005079806234700387+1.0004385132020523j)
雙曲函式
import cmath
c = 2 + 4j
print('Inverse hyperbolic sine value: \n', cmath.asinh(c))
print('Inverse hyperbolic cosine value: \n', cmath.acosh(c))
print('Inverse hyperbolic tangent value: \n', cmath.atanh(c))
print('Hyperbolic sine value: \n', cmath.sinh(c))
print('Hyperbolic cosine value: \n', cmath.cosh(c))
print('Hyperbolic tangent value: \n', cmath.tanh(c))結果
Inverse hyperbolic sine value: (2.183585216564564+1.096921548830143j) Inverse hyperbolic cosine value: (2.198573027920936+1.1169261168317741j) Inverse hyperbolic tangent value: (0.09641562020299617+1.3715351039616865j) Hyperbolic sine value: (-2.370674169352002-2.8472390868488278j) Hyperbolic cosine value: (-2.4591352139173837-2.744817006792154j) Hyperbolic tangent value: (1.0046823121902348+0.03642336924740368j)
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP