Python は実数だけでなく、「cmath」ファイルを使用して複素数とそれに関連する関数も処理できます。 複素数 それらは数学に関連する多くのアプリケーションで使用されており、Python はそれらを処理および操作するための便利なツールを提供します。 実数を複素数に変換する 複素数は「」で表されます。 x + yi '。 Python は関数を使用して実数 x と y を複素数に変換します 複素数(xy) 。実部には関数を使用してアクセスできます。 本物() 虚数部は次のように表すことができます。 画像() 。
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
極形式から直方体形式への変換、またはその逆の変換 極性への変換は次を使用して行われます 極性() を返す ペア(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 (重要な関数と定数)