【Python】複素数の極座標・直交座標の相互変換

Python入門者向けに複素数の極座標・直交座標の相互変換をする方法についてまとめました。

## 【極座標変換】cmath.polar

cmath.polar(ej)で極座標(大きさ、位相)を一気に計算できます。

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

ej = 3 + 2j

abs, rad = cmath.polar(ej)

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

print(abs) # 3.605551275463989
print(deg) # 33.690067525979785

## 【直交座標】cmath.rect

極座標(大きさ、位相)から直交座標(実部、虚部)に変換するにはcmath.rectメソッドを使います。

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

abs = 3.605551275463989    # 大きさ
deg = 33.690067525979785   # 位相(度)
phase = math.radians(deg)  # 位相(ラジン)

# 直交座標に変換
ej = cmath.rect(abs, phase)

print(ej) # (3+1.9999999999999996j)
関連記事
1 【Python入門】複素数型(complex型)の使い方
2 Python入門 基本文法
Python
スポンサーリンク

コメント