【Python】共役複素数・絶対値・位相の計算

Python入門者向けに共役複素数・絶対値・位相を計算する方法についてまとめました。

## 【共役複素数】conjugate

Pythonでは、conjugate()メソッドを用いて、共役複素数を求めます。

# -*- coding: utf-8 -*-
ej = 2 + 3j

print(ej.conjugate()) # (2-3j)

## 【絶対値】abs()関数

他の数値型と同じく、複素数型もabs関数で絶対値(大きさ)を計算できます。

# -*- coding: utf-8 -*-
ej = 2 + 3j

print(abs(ej)) # 3.605551275463989

## 【位相】math.atan2、cmath.phase

他の数値型と同じく、複素数型もmathモジュールのatan2メソッド、もしくはcmathモジュールのphaseメソッドで位相(偏角)を計算できます。

# -*- coding: utf-8 -*-
import math

ej = 3 + 2j

# 位相(単位rad)を計算
rad = math.atan2(ej.imag, ej.real)

# 単位を度数(degree)に変換
deg = math.degrees(rad)

print(rad) # 0.5880026035475675
print(deg) # 33.690067525979785
# -*- coding: utf-8 -*-
import cmath
import math

ej = 3 + 2j

rad = cmath.phase(ej)

# 単位を度数(degree)に変換
deg = math.degrees(rad)

print(rad) # 0.5880026035475675
print(deg) # 33.690067525979785
関連記事
1 【Python入門】複素数型(complex型)の使い方
2 Python入門 基本文法
Python
スポンサーリンク

コメント