Pythonには複素数に関する種々の計算を行うためのライブラリcmathが用意されています。
これを使うと例えばcmath.sqrt(3+4j) # => (2+1j)のような計算が行えるようになります。
import cmath
cmath.pi # => 3.141592653589793
cmath.tau # => 6.283185307179586
cmath.e # => 2.718281828459045
cmath.inf # => inf
cmath.infj # => infj
cmath.nan # => nan
cmath.nanj # => nanj
cmath.polar(0) # => (0.0, 0.0)
cmath.polar(1j) # => (1.0, 1.5707963267948966)
cmath.polar(-1) # => (1.0, 3.141592653589793)
cmath.polar(1+1j) # => (1.4142135623730951, 0.7853981633974483)
cmath.phase(-1j) # => -1.570796326794896
cmath.phase(1-1j) # => -0.7853981633974483
cmath.phase(1) # => 0.0
cmath.phase(1 + 1j) # => 0.7853981633974483
cmath.phase(1j) # => 1.5707963267948966
cmath.phase(-1) # => 3.141592653589793
cmath.rect(1, math.pi/4) # => (0.7071067811865476+0.7071067811865476j)
cmath.isfinite(1 + cmath.inf * 1j) # => False
cmath.isfinite(cmath.inf + 1j) # => False
cmath.isfinite(1 + 1j) # => True
cmath.isinf(1 + cmath.inf * 1j) # => True
cmath.isinf(cmath.inf + 1j) # => True
cmath.isinf(1 + 1j) # => False
cmath.isnan(1 + cmath.nan * 1j) # => True
cmath.isnan(cmath.nan + 1j) # => True
cmath.isnan(1 + 1j) # => False
rel_tol:相対許容差はaとbの差をa(b)で割った値、
abt_tol:絶対許容差はa,bの差で比較する。
a,bが小さい場合は絶対許容差で比較しやすい。
cmath.isclose(1.000000001+1.000000001j, 1.000000002+1.000000002j) # => True
cmath.isclose(1.00000001+1.00000001j, 1.00000002+1.00000002j) # => False
cmath.isclose(100.01+100.01j, 100.02+100.02j , rel_tol=1e-4) # => True
cmath.isclose(100.01+100.01j, 100.02+100.02j , rel_tol=1e-5) # => False
cmath.isclose(0.000101+0.000101j, 0.000102+0.000102j , rel_tol=1e-5) # => False
cmath.isclose(0.000101+0.000101j, 0.000102+0.000102j , rel_tol=1e-5, abs_tol=1e-6) # => False
cmath.isclose(0.000101+0.000101j, 0.000102+0.000102j , rel_tol=1e-5, abs_tol=1e-5) # => True
cmath.isclose(0.000101+0.000101j, 0.000102+0.000102j , abs_tol=1e-6) # => False
cmath.isclose(0.000101+0.000101j, 0.000102+0.000102j , abs_tol=1e-5) # => True
cmath.sqrt(-1) # => 1j
cmath.sqrt(3+4j) # => (2+1j)
cmath.exp(math.pi*1j) # => (-1+1.2246467991473532e-16j)
base省略時は自然対数。
cmath.log(1+1j) # => (0.34657359027997264+0.7853981633974483j)
cmath.log(1+1j, 2) # => (0.5+1.1330900354567985j)
cmath.log10(1+1j) # => (0.15051499783199057+0.3410940884604603j)
cmath.sin(1+1j) # => (1.2984575814159773+0.6349639147847361j)
cmath.cos(1+1j) # => (0.8337300251311491-0.9888977057628651j)
cmath.tan(1+1j) # => (0.2717525853195118+1.0839233273386946j)
cmath.asin(1.2984575814159773+0.6349639147847361j) # => (1+1j)
cmath.acos(0.8337300251311491-0.9888977057628651j) # => (0.9999999999999999+1j)
cmath.atan(0.2717525853195118+1.0839233273386946j) # => (1+0.9999999999999999j)
cmath.sinh(1+1j) # => (0.6349639147847361+1.2984575814159773j)
cmath.cosh(1+1j) # => (0.8337300251311491+0.9888977057628651j)
cmath.tanh(1+1j) # => (1.0839233273386946+0.2717525853195118j)
cmath.asinh(0.6349639147847361+1.2984575814159773j) # => (1+1j)
cmath.acosh(0.8337300251311491+0.9888977057628651j) # => (1+0.9999999999999999j)
cmath.atanh(1.0839233273386946+0.2717525853195118j) # => (0.9999999999999999+1j)
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント