scipy.specialのまとめ

scipy.specialを目にする機会が増えてきたので、ここでまとめてみます。
scipy.specialとは
scipy.specialとは、数理物理学の関数群のことです。難しい数式がたくさん出てきますが、細かいことは気にせずまとめてみます。
尚、scipy.specialの関数もユニバーサル関数の一種です。
scipy.specialの関数群
gamma ガンマ関数
\begin{equation*}
\Gamma(s) = \int_{0}^{\infty} x^{s-1} e^{-x}dx
\end{equation*}
ガンマ関数は、階乗の式を実数にまで範囲を広げたものです。
※階乗とは、3!=3×2×1や5!=5×4×3×2×1などn!で表した場合1からnまでのすべての整数の積を意味します。
gamma( 配列または数字 )
from scipy.special import gamma
print(gamma(3.5))
print(gamma([0,0.5,1,5]))
【実行結果】
3.323350970447843
[ inf 1.77245385 1. 24. ]
gammaln ガンマ関数の自然対数
\begin{equation*}
\ln (|\Gamma(x)|)
\end{equation*}
ガンマ関数の結果を自然対数に変換します。
gammaln( 配列または数字 )
from scipy.special import gammaln
gammaln([1,2,3])
【実行結果】
array([0. , 0. , 0.69314718])
beta ベータ関数
\begin{equation*}
B(a,b) = \int_{0}^{1}x^{a-1}(1-x)^{b-1}dx
\end{equation*}
ベータ関数はガンマ関数の親戚みたいな物です。次のような関係があります。
\begin{equation*}
B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
\end{equation*}
beta( 配列または数字 )
\begin{equation*}
B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
\end{equation*}
import scipy.special as sc
print( sc.beta(2,3) )
print( sc.gamma(2)*sc.gamma(3)/sc.gamma(2+3) )
【実行結果】
0.08333333333333333
0.08333333333333333
erf 誤差関数
\begin{equation*}
erf(x) = \frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^2}dt
\end{equation*}
erfは誤差関数やガウスの誤差関数と呼ばれていて、シグモイド形状の結果を出力します。(下記例参照)
erf( 配列 )
from scipy import special
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3)
plt.plot(x, special.erf(x))
plt.xlabel('$x$')
plt.ylabel('$erfc(x)$')
plt.show()
【実行結果】

erfc 相補誤差関数
\begin{equation*}
erfc(x) = 1 – \frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^2}dt
\end{equation*}
erfcは相補誤差関数と呼ばれ、1からerfを引いた物です。
erfc( 配列 )
from scipy import special
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3)
plt.plot(x, special.erfc(x))
plt.xlabel('$x$')
plt.ylabel('$erfc(x)$')
plt.show()
【実行結果】
