恥ずかしながら、Sympyは使ったことがなかった。
座標変換、代数演算の微分積分をする必要があって、
Walfram alpha を使ったら、
思いの外便利で高性能だった。
Walfram alpha だと複数行の入力はできないので、
もっと汎用なものがPythonでできればいい。
ということで、Sympyを触ってみた健忘録だ。
Sympy は matplotlib.pyplot と同じように、
当然のように Jupyter notebook との相性が抜群だ。
今回は、極座標のラプラシアンを求めてみた。
Out[4]:
$$\left [ r \sin{\left (\theta \right )} \cos{\left (\phi \right )}, \quad r \sin{\left (\phi \right )} \sin{\left (\theta \right )}, \quad r \cos{\left (\theta \right )}\right ]$$
Out[5]:
$$\left [ r, \quad \theta, \quad \phi\right ]$$
Out[6]:
$$f{\left (r,\theta,\phi \right )}$$
Out[8]:
$$\left ( 1, \quad r, \quad r \left|{\sin{\left (\theta \right )}}\right|\right )$$
Out[9]:
$$\left[\begin{matrix}\frac{\partial}{\partial r}\left(r \sin{\left (\theta \right )} \cos{\left (\phi \right )}\right) & \frac{\partial}{\partial r}\left(r \sin{\left (\phi \right )} \sin{\left (\theta \right )}\right) & \frac{\partial}{\partial r}\left(r \cos{\left (\theta \right )}\right)\\\frac{\partial}{\partial \theta}\left(r \sin{\left (\theta \right )} \cos{\left (\phi \right )}\right) & \frac{\partial}{\partial \theta}\left(r \sin{\left (\phi \right )} \sin{\left (\theta \right )}\right) & \frac{\partial}{\partial \theta}\left(r \cos{\left (\theta \right )}\right)\\\frac{\partial}{\partial \phi}\left(r \sin{\left (\theta \right )} \cos{\left (\phi \right )}\right) & \frac{\partial}{\partial \phi}\left(r \sin{\left (\phi \right )} \sin{\left (\theta \right )}\right) & \frac{\partial}{\partial \phi}\left(r \cos{\left (\theta \right )}\right)\end{matrix}\right]$$
Out[10]:
$$\left[\begin{matrix}\sin{\left (\theta \right )} \cos{\left (\phi \right )} & \sin{\left (\phi \right )} \sin{\left (\theta \right )} & \cos{\left (\theta \right )}\\r \cos{\left (\phi \right )} \cos{\left (\theta \right )} & r \sin{\left (\phi \right )} \cos{\left (\theta \right )} & - r \sin{\left (\theta \right )}\\- r \sin{\left (\phi \right )} \sin{\left (\theta \right )} & r \sin{\left (\theta \right )} \cos{\left (\phi \right )} & 0\end{matrix}\right]$$
Out[11]:
$$\left[\begin{matrix}\sin{\left (\theta \right )} \cos{\left (\phi \right )} & \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} & - \frac{\sin{\left (\phi \right )}}{r \sin{\left (\theta \right )}}\\\sin{\left (\phi \right )} \sin{\left (\theta \right )} & \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} & \frac{\cos{\left (\phi \right )}}{r \sin{\left (\theta \right )}}\\\cos{\left (\theta \right )} & - \frac{1}{r} \sin{\left (\theta \right )} & 0\end{matrix}\right]$$
Out[12]:
$$\left[\begin{matrix}\frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )}\\\frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\\\frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}\end{matrix}\right]$$
Out[13]:
$$\left[\begin{matrix}\sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} - \frac{\sin{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} + \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\\\sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{\cos{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}}\\\cos{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \sin{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\end{matrix}\right]$$
Out[14]:
$$\left(\sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial^{2}}{\partial r^{2}} f{\left (r,\theta,\phi \right )} - \frac{\frac{\partial^{2}}{\partial \phi\partial r} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} \sin{\left (\phi \right )} + \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{\sin{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r^{2} \sin{\left (\theta \right )}} - \frac{1}{r^{2}} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\right) \sin{\left (\theta \right )} \cos{\left (\phi \right )} - \frac{\sin{\left (\phi \right )}}{r \sin{\left (\theta \right )}} \left(- \sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial^{2}}{\partial \phi\partial r} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} - \frac{\sin{\left (\phi \right )} \frac{\partial^{2}}{\partial \phi^{2}} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} + \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial \phi\partial \theta} f{\left (r,\theta,\phi \right )} - \frac{\cos{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}}\right) + \frac{1}{r} \left(\sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} + \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} - \frac{\frac{\partial^{2}}{\partial \phi\partial \theta} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} \sin{\left (\phi \right )} + \frac{\cos{\left (\theta \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin^{2}{\left (\theta \right )}} \sin{\left (\phi \right )} - \frac{1}{r} \sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial \theta^{2}} f{\left (r,\theta,\phi \right )}\right) \cos{\left (\phi \right )} \cos{\left (\theta \right )}$$
Out[15]:
$$\left(\sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial^{2}}{\partial r^{2}} f{\left (r,\theta,\phi \right )} + \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{\frac{\partial^{2}}{\partial \phi\partial r} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} \cos{\left (\phi \right )} - \frac{1}{r^{2}} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} - \frac{\cos{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r^{2} \sin{\left (\theta \right )}}\right) \sin{\left (\phi \right )} \sin{\left (\theta \right )} + \frac{\cos{\left (\phi \right )}}{r \sin{\left (\theta \right )}} \left(\sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial^{2}}{\partial \phi\partial r} f{\left (r,\theta,\phi \right )} + \sin{\left (\theta \right )} \cos{\left (\phi \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial \phi\partial \theta} f{\left (r,\theta,\phi \right )} - \frac{\sin{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} + \frac{1}{r} \cos{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{\cos{\left (\phi \right )} \frac{\partial^{2}}{\partial \phi^{2}} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}}\right) + \frac{1}{r} \left(\sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} + \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \sin{\left (\phi \right )} \sin{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{1}{r} \sin{\left (\phi \right )} \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial \theta^{2}} f{\left (r,\theta,\phi \right )} + \frac{\frac{\partial^{2}}{\partial \phi\partial \theta} f{\left (r,\theta,\phi \right )}}{r \sin{\left (\theta \right )}} \cos{\left (\phi \right )} - \frac{\cos{\left (\phi \right )} \frac{\partial}{\partial \phi} f{\left (r,\theta,\phi \right )}}{r \sin^{2}{\left (\theta \right )}} \cos{\left (\theta \right )}\right) \sin{\left (\phi \right )} \cos{\left (\theta \right )}$$
Out[16]:
$$\left(\cos{\left (\theta \right )} \frac{\partial^{2}}{\partial r^{2}} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \sin{\left (\theta \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} + \frac{1}{r^{2}} \sin{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\right) \cos{\left (\theta \right )} - \frac{1}{r} \left(- \sin{\left (\theta \right )} \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \cos{\left (\theta \right )} \frac{\partial^{2}}{\partial r\partial \theta} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \sin{\left (\theta \right )} \frac{\partial^{2}}{\partial \theta^{2}} f{\left (r,\theta,\phi \right )} - \frac{1}{r} \cos{\left (\theta \right )} \frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}\right) \sin{\left (\theta \right )}$$
Out[17]:
$$\frac{1}{r^{2}} \left(r^{2} \frac{\partial^{2}}{\partial r^{2}} f{\left (r,\theta,\phi \right )} + 2 r \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \frac{\partial^{2}}{\partial \theta^{2}} f{\left (r,\theta,\phi \right )} + \frac{\frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}}{\tan{\left (\theta \right )}} + \frac{\frac{\partial^{2}}{\partial \phi^{2}} f{\left (r,\theta,\phi \right )}}{\sin^{2}{\left (\theta \right )}}\right)$$
Out[18]:
$$\frac{1}{r^{2}} \left(r^{2} \frac{\partial^{2}}{\partial r^{2}} f{\left (r,\theta,\phi \right )} + 2 r \frac{\partial}{\partial r} f{\left (r,\theta,\phi \right )} + \frac{\partial^{2}}{\partial \theta^{2}} f{\left (r,\theta,\phi \right )} + \frac{\frac{\partial}{\partial \theta} f{\left (r,\theta,\phi \right )}}{\tan{\left (\theta \right )}} + \frac{\frac{\partial^{2}}{\partial \phi^{2}} f{\left (r,\theta,\phi \right )}}{\sin^{2}{\left (\theta \right )}}\right)$$