【Python】complex 関数の使い方と実行結果

Python

Python の組み込み関数 complexコンプレックス の使い方です。

complex 関数で、複素数ふくそすうを作ります。

(Python) class complex([real[, imag]]) の説明にあった通り、complex 関数は、『数値すうちまたは文字列もじれつ』を『複素数ふくそすう』に変換することができました。

それらのコード例と、実行結果を載せました。

あと、『エラーになったコード例』も書きました。

エラーになったコード例

complex 関数で、エラーになったコードれいです。

complex('3', 4)
TypeError: complex() can't take second arg if first is a string
complex(3, '4')
TypeError: complex() second arg can't be a string
complex(b'3+4j')
TypeError: complex() first argument must be a string or a number, not 'bytes'
complex(None) # None
TypeError: complex() first argument must be a string or a number, not 'NoneType'
complex(()) # () 空のタプル
TypeError: complex() first argument must be a string or a number, not 'tuple'
complex([]) # [] 空のリスト
TypeError: complex() first argument must be a string or a number, not 'list'
complex(set()) # set() 空の集合
TypeError: complex() first argument must be a string or a number, not 'set'
complex({}) # {} 空の辞書
TypeError: complex() first argument must be a string or a number, not 'dict'
complex(lambda x: x) # <function <lambda> at 0x0000000000000000>
TypeError: complex() first argument must be a string or a number, not 'function'
complex(range(0, 1)) # range(0, 1)
TypeError: complex() first argument must be a string or a number, not 'range'
import datetime
complex(datetime.datetime.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
TypeError: complex() first argument must be a string or a number, not 'datetime.datetime'
import datetime
complex(datetime.timedelta.max) # datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
TypeError: complex() first argument must be a string or a number, not 'datetime.timedelta'

引数無しの結果

complex 関数を、引数ひきすう無しで呼び出した結果です。

(Python) class complex([real[, imag]])

complex() # 引数無し
0j

組み込み定数を渡した結果

complex 関数に、組み込み定数を渡した結果です。

(Python) class complex([real[, imag]])

(Python) False

(Python) True

complex(False)
0j
complex(imag=False)
0j
complex(False, False)
0j
complex(True)
(1+0j)
complex(imag=True)
1j
complex(True, True)
(1+1j)

int を渡した結果

complex 関数に、intイント整数せいすう)を渡した結果です。

int 型イントがたの数値の書き方は(整数リテラルは)、普通の 10 進数じゅっしんすうで書くこともできましたし、接頭辞せっとうじを付けて、2 進数にしんすう8 進数はっしんすう16 進数じゅうろくしんすうの表現で書くこともできました。

(Python) class complex([real[, imag]])

(Python) class int([x])

(Python) 整数リテラル (Integer literals)

10 進数

10 進数の場合です。

complex(real=-3, imag=-4)
(-3-4j)
complex(-3, -4)
(-3-4j)
complex(-3)
(-3+0j)
complex(imag=-4)
-4j
complex(0, 0)
0j
complex(0)
0j
complex(imag=0)
0j

2 進数

2 進数の場合です。

complex(real=-0b011, imag=-0b100)
(-3-4j)
complex(-0b011, -0b100)
(-3-4j)

8 進数

8 進数の場合です。

complex(real=-0o05, imag=-0o14)
(-5-12j)
complex(-0o05, -0o14)
(-5-12j)

16 進数

16 進数の場合です。

complex(real=0x07, imag=-0x18)
(7-24j)
complex(0x07, -0x18)
(7-24j)

float を渡した結果

complex 関数に、floatフロート浮動小数点数ふどうしょうすうてんすう)を渡した結果です。

float 型フロートがたの数値の書き方は(浮動小数点数リテラルは)、普通に『小数(整数と小数点以下のかず)』で書くこともできましたし、『指数しすう』を使用して書くこともできました。

また、複素数に『無限大むげんだい (inf, Infinity) や非数ひすう (nan, Not a Number)』といった概念がいねん を渡すこともできました。

(Python) class complex([real[, imag]])

(Python) class float([x])

(Python) 浮動小数点数ふどうしょうすうてんすうリテラル (Floating point literals)

小数

普通に『小数(整数と小数点以下のかず)』で書いた場合です。

complex(real=-0.5, imag=-1.2)
(-0.5-1.2j)
complex(real=-0.3, imag=-0.4)
(-0.3-0.4j)
complex(-0.3, -0.4)
(-0.3-0.4j)
complex(-0.3)
(-0.3+0j)
complex(imag=-0.4)
-0.4j
complex(-0.0, -0.0)
(-0-0j)
complex(0.0, 0.0)
0j

指数

指数しすう』を使用して書いた場合です。

complex(real=-5.0e1, imag=-1.2e2)
(-50-120j)
complex(-5.0e1, -1.2e2)
(-50-120j)

無限大 (inf) と非数 (nan)

無限大むげんだい (inf, Infinity) や非数ひすう (nan, Not a Number)』を渡した場合です。

complex(float('inf'), float('inf'))
(inf+infj)
complex(float('-inf'), float('-inf'))
(-inf-infj)
complex(float('-inf'))
(-inf+0j)
complex(imag=float('-inf'))
-infj
complex(float('nan'), float('nan'))
(nan+nanj)
complex(float('nan'))
(nan+0j)
complex(imag=float('nan'))
nanj
complex(float('-inf'), float('nan'))
(-inf+nanj)
complex(float('nan'), float('-inf'))
(nan-infj)

complex を渡した結果

complex 関数に、complexコンプレックス複素数ふくそすう)を渡した結果です。

(Python) class complex([real[, imag]])

複素数

complexコンプレックス 関数で作った複素数を渡した場合です。

complex(complex(-3, -4))
(-3-4j)
complex(imag=complex(-3, -4))
(4-3j)
complex(complex(-3, -4), complex(-3, -4))
(1-7j)

虚数リテラル

虚数きょすうリテラルで書いた複素数を渡した場合です。

虚数リテラルは、そのままで複素数として使えましたし、それを complex 関数の実部じつぶ (real) や虚部きょぶ (imag) に渡すこともできました。

また、(Python) 数値リテラル の説明にあった通り、複素数は、『整数リテラル(-3 など)または浮動小数点数リテラル(-0.3 など)』で書いた実数と、『虚数リテラル(-4j, -1.2j など)』で書いた虚数とので作ることができました。

(Python) 整数リテラル (Integer literals)

(Python) 浮動小数点数ふどうしょうすうてんすうリテラル (Floating point literals)

(Python) 虚数リテラル (Imaginary literals)

-25j
(-0-25j)
complex(real=-25j)
(-0-25j)
complex(imag=-25j)
(25-0j)
-3-4j
(-3-4j)
complex(-3-4j)
(-3-4j)
complex(imag=-3-4j)
(4-3j)
complex(real=-3-4j, imag=-3-4j)
(1-7j)
complex(-0.5-1.2j)
(-0.5-1.2j)
complex(imag=-0.5-1.2j)
(1.2-0.5j)
complex(-0.5-1.2j, -0.5-1.2j)
(0.7-1.7j)

Decimal を渡した結果

complex 関数に、Decimal 型デシマルがた十進数型じゅっしんすうがた)を渡した結果です。

(Python) class complex([real[, imag]])

(Python) class decimal.Decimal(value="0", context=None)

from decimal import Decimal
complex(Decimal(-0.3), Decimal(-0.4))
(-0.3-0.4j)
from decimal import Decimal
complex(Decimal(-0.0), Decimal(-0.0))
(-0+0j)
from decimal import Decimal
complex(Decimal(0.0), Decimal(0.0))
0j
from decimal import Decimal
complex(Decimal(-5.0e1), Decimal(-1.2e2))
(-50-120j)
from decimal import Decimal
complex(Decimal('-inf'), Decimal('-inf'))
(-inf-infj)
from decimal import Decimal
complex(Decimal('nan'), Decimal('nan'))
(nan+nanj)
from decimal import Decimal
complex(Decimal('snan'), Decimal('snan'))
ValueError: cannot convert signaling NaN to float

str を渡した結果

complex 関数に、strエスティーアール(文字列)を渡した結果です。

(Python) class complex([real[, imag]])

(Python) class str(object='')

数値

整数せいすう浮動小数点数ふどうしょうすうてんすう虚数単位きょすうたんい'j'』で書いた『数値』の文字列を渡した場合です。

complex('-0.3-0.4j')
(-0.3-0.4j)
complex('-3-4j')
(-3-4j)
complex('-3')
(-3+0j)
complex('-4j')
-4j
complex('-0.0-0.0j')
(-0-0j)
complex('-0-0j')
(-0-0j)
complex('-0')
(-0+0j)
complex('0')
0j
complex('-0j')
-0j
complex('0j')
0j

指数

指数しすう』を使用して書いた場合です。

complex('-5.0e1-1.2e2j')
(-50-120j)
complex('-5.0e1')
(-50+0j)
complex('-1.2e2j')
-120j

無限大 (inf) と非数 (nan)

無限大むげんだい (inf, Infinity) や非数ひすう (nan, Not a Number)』の文字列を渡した場合です。

complex('-inf-infj')
(-inf-infj)
complex('-3-infj')
(-3-infj)
complex('-inf-4j')
(-inf-4j)
complex('-inf')
(-inf+0j)
complex('-infj')
-infj
complex('nan+nanj')
(nan+nanj)
complex('-3+nanj')
(-3+nanj)
complex('nan-4j')
(nan-4j)
complex('nan')
(nan+0j)
complex('nanj')
nanj
complex('-inf+nanj')
(-inf+nanj)
complex('nan-infj')
(nan-infj)
complex('Infinity-NaNj')
(inf+nanj)

以上です。

スポンサーリンク
シェアする(押すとSNS投稿用の『編集ページ』に移動します)
フォローする(RSSフィードに移動します)
スポンサーリンク
シラベルノート
タイトルとURLをコピーしました