数学の準備1

石井俊全著「一般相対性理論を一歩一歩数式で理解する」ベレ出版の第1章をSymPyでできるか試してみました。

ベクトルの内積と外積

In [1]:
from sympy import *
init_printing()

記号の宣言

In [2]:
a_x,a_y,a_z,b_x,b_y,b_z,c_x,c_y,c_z,d_x,d_y,d_z=symbols('a_x,a_y,a_z,b_x,b_y,b_z,c_x,c_y,c_z,d_x,d_y,d_z')
In [3]:
# 省略した書き方
a_x,a_y,a_z,b_x,b_y,b_z,c_x,c_y,c_z,d_x,d_y,d_z=symbols('a_x:z,b_x:z,c_x:z,d_x:z')

ベクトルの定義
$\mathbf{a}=(a_x,a_y,a_z)$
$\mathbf{b}=(b_x,b_y,b_z)$
$\mathbf{c}=(c_x,c_y,c_z)$
$\mathbf{d}=(d_x,d_y,d_z)$

In [4]:
a=Matrix([a_x,a_y,a_z])
b=Matrix([b_x,b_y,b_z])
c=Matrix([c_x,c_y,c_z])
d=Matrix([d_x,d_y,d_z])
(a,b,c,d)
Out[4]:
$$\left ( \left[\begin{matrix}a_{x}\\a_{y}\\a_{z}\end{matrix}\right], \quad \left[\begin{matrix}b_{x}\\b_{y}\\b_{z}\end{matrix}\right], \quad \left[\begin{matrix}c_{x}\\c_{y}\\c_{z}\end{matrix}\right], \quad \left[\begin{matrix}d_{x}\\d_{y}\\d_{z}\end{matrix}\right]\right )$$

美しくないのでちょっと整理します latexの結果をMarkdownで$と$に挟むと\\となってしまいます。これだとうまく数式が表示できません。 さらにprintをつければ\\が\となります。 この方法は「すごい広島 with Python」で教わりました。

In [5]:
n1s=['a','b','c','d']
n2s=[a,b,c,d]
for n1,n2 in zip (n1s,n2s):
    str='\mathbf{%s}=' % n1
    str=str+latex(n2.T)+'\\\\' #.Tは転置
    print(str)
\mathbf{a}=\left[\begin{matrix}a_{x} & a_{y} & a_{z}\end{matrix}\right]\\
\mathbf{b}=\left[\begin{matrix}b_{x} & b_{y} & b_{z}\end{matrix}\right]\\
\mathbf{c}=\left[\begin{matrix}c_{x} & c_{y} & c_{z}\end{matrix}\right]\\
\mathbf{d}=\left[\begin{matrix}d_{x} & d_{y} & d_{z}\end{matrix}\right]\\

$\mathbf{a}=\left[\begin{matrix}a_{x} & a_{y} & a_{z}\end{matrix}\right]\\ \mathbf{b}=\left[\begin{matrix}b_{x} & b_{y} & b_{z}\end{matrix}\right]\\ \mathbf{c}=\left[\begin{matrix}c_{x} & c_{y} & c_{z}\end{matrix}\right]\\ \mathbf{d}=\left[\begin{matrix}d_{x} & d_{y} & d_{z}\end{matrix}\right]\\ $

ベクトルの内積
$\mathbf{a}\dot{}\mathbf{b}=a_{x} b_{x} + a_{y} b_{y} + a_{z} b_{z}$

In [6]:
a.dot(b)
Out[6]:
$$a_{x} b_{x} + a_{y} b_{y} + a_{z} b_{z}$$

ベクトルの外積
$\mathbf{a}\times{}\mathbf{b}= \left[\begin{matrix}a_{y} b_{z} - a_{z} b_{y}\\- a_{x} b_{z} + a_{z} b_{x}\\a_{x} b_{y} - a_{y} b_{x}\end{matrix}\right]$

In [7]:
a.cross(b)
Out[7]:
$$\left[\begin{matrix}a_{y} b_{z} - a_{z} b_{y}\\- a_{x} b_{z} + a_{z} b_{x}\\a_{x} b_{y} - a_{y} b_{x}\end{matrix}\right]$$

$\mathbf{a}\times{}\mathbf{a}=\mathbf{0}$

In [8]:
a.cross(a)
Out[8]:
$$\left[\begin{matrix}0\\0\\0\end{matrix}\right]$$

$(\mathbf{a}\times{}\mathbf{b})\dot{}\mathbf{a}=0$
epandを使って0にしてます

In [9]:
x=(a.cross(b)).dot(a)
(x,expand(x))
Out[9]:
$$\left ( a_{x} \left(a_{y} b_{z} - a_{z} b_{y}\right) + a_{y} \left(- a_{x} b_{z} + a_{z} b_{x}\right) + a_{z} \left(a_{x} b_{y} - a_{y} b_{x}\right), \quad 0\right )$$

$(\mathbf{a}\times{}\mathbf{b})\dot{}\mathbf{b}=0$

In [10]:
x=(a.cross(b)).dot(b)
(x,expand(x))
Out[10]:
$$\left ( b_{x} \left(a_{y} b_{z} - a_{z} b_{y}\right) + b_{y} \left(- a_{x} b_{z} + a_{z} b_{x}\right) + b_{z} \left(a_{x} b_{y} - a_{y} b_{x}\right), \quad 0\right )$$

$\mathbf{a}\dot{}(\mathbf{b}\times{}\mathbf{c})= \mathbf{b}\dot{}(\mathbf{c}\times{}\mathbf{a})= \mathbf{c}\dot{}(\mathbf{a}\times{}\mathbf{b})$

In [11]:
x1=a.dot(b.cross(c))
x2=b.dot(c.cross(a))
x3=c.dot(a.cross(b))
(expand(x1),expand(x2),expand(x3))
Out[11]:
$$\left ( a_{x} b_{y} c_{z} - a_{x} b_{z} c_{y} - a_{y} b_{x} c_{z} + a_{y} b_{z} c_{x} + a_{z} b_{x} c_{y} - a_{z} b_{y} c_{x}, \quad a_{x} b_{y} c_{z} - a_{x} b_{z} c_{y} - a_{y} b_{x} c_{z} + a_{y} b_{z} c_{x} + a_{z} b_{x} c_{y} - a_{z} b_{y} c_{x}, \quad a_{x} b_{y} c_{z} - a_{x} b_{z} c_{y} - a_{y} b_{x} c_{z} + a_{y} b_{z} c_{x} + a_{z} b_{x} c_{y} - a_{z} b_{y} c_{x}\right )$$

expandを使わないと、右辺=左辺とはなりません。

In [12]:
(x1==x2,expand(x1)==expand(x2),expand(x2)==expand(x3))
Out[12]:
(False, True, True)

$(\mathbf{a}\dot{}\mathbf{c})(\mathbf{b}\dot{}\mathbf{d})- (\mathbf{b}\dot{}\mathbf{c})(\mathbf{a}\dot{}\mathbf{d})= (\mathbf{a}\times{}\mathbf{b})\dot{}(\mathbf{c}\times{}\mathbf{d})$

In [13]:
x1=(a.dot(c))*(b.dot(d))-(b.dot(c))*(a.dot(d))
x2=(a.cross(b)).dot(c.cross(d))
(x1,x2,expand(x1),expand(x2))
Out[13]:
$$\left ( \left(a_{x} c_{x} + a_{y} c_{y} + a_{z} c_{z}\right) \left(b_{x} d_{x} + b_{y} d_{y} + b_{z} d_{z}\right) - \left(a_{x} d_{x} + a_{y} d_{y} + a_{z} d_{z}\right) \left(b_{x} c_{x} + b_{y} c_{y} + b_{z} c_{z}\right), \quad \left(a_{x} b_{y} - a_{y} b_{x}\right) \left(c_{x} d_{y} - c_{y} d_{x}\right) + \left(- a_{x} b_{z} + a_{z} b_{x}\right) \left(- c_{x} d_{z} + c_{z} d_{x}\right) + \left(a_{y} b_{z} - a_{z} b_{y}\right) \left(c_{y} d_{z} - c_{z} d_{y}\right), \quad a_{x} b_{y} c_{x} d_{y} - a_{x} b_{y} c_{y} d_{x} + a_{x} b_{z} c_{x} d_{z} - a_{x} b_{z} c_{z} d_{x} - a_{y} b_{x} c_{x} d_{y} + a_{y} b_{x} c_{y} d_{x} + a_{y} b_{z} c_{y} d_{z} - a_{y} b_{z} c_{z} d_{y} - a_{z} b_{x} c_{x} d_{z} + a_{z} b_{x} c_{z} d_{x} - a_{z} b_{y} c_{y} d_{z} + a_{z} b_{y} c_{z} d_{y}, \quad a_{x} b_{y} c_{x} d_{y} - a_{x} b_{y} c_{y} d_{x} + a_{x} b_{z} c_{x} d_{z} - a_{x} b_{z} c_{z} d_{x} - a_{y} b_{x} c_{x} d_{y} + a_{y} b_{x} c_{y} d_{x} + a_{y} b_{z} c_{y} d_{z} - a_{y} b_{z} c_{z} d_{y} - a_{z} b_{x} c_{x} d_{z} + a_{z} b_{x} c_{z} d_{x} - a_{z} b_{y} c_{y} d_{z} + a_{z} b_{y} c_{z} d_{y}\right )$$
In [14]:
expand(x1)==expand(x2)
Out[14]:
True

$(\mathbf{a}\times{}\mathbf{b})\dot{}(\mathbf{c}\times{}\mathbf{d})= \left|\mathbf{a}\times{}\mathbf{b}\right|\left|\mathbf{c}\times{}\mathbf{d}\right|\cos{}\theta{} $

微分の公式

$\left(f(x)g(x)\right)'=f'(x)g(x)+f(x)g'(x)$ 関数の積の微分(ライプニッツ則)
$\left(f(g(x))\right)'=f'(g(x))g'(x)$ 合成関数の微分(連鎖律)

In [15]:
reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [16]:
from sympy import *
init_printing()
In [17]:
x,f,g=symbols('x,f,g')
In [18]:
eq=f(x)*g(x)
eq1=diff(eq,x,evaluate=False)
eq2=diff(eq,x,evaluate=True)
(eq1,eq2)
Out[18]:
$$\left ( \frac{d}{d x}\left(f{\left (x \right )} g{\left (x \right )}\right), \quad f{\left (x \right )} \frac{d}{d x} g{\left (x \right )} + g{\left (x \right )} \frac{d}{d x} f{\left (x \right )}\right )$$
In [19]:
str=latex(eq1)+'='+latex(eq2)
str=str.replace('frac','cfrac') #分数を大きく表示する
print(str) # 結果をMarkdown形式後に$結果$で表示させます
\cfrac{d}{d x}\left(f{\left (x \right )} g{\left (x \right )}\right)=f{\left (x \right )} \cfrac{d}{d x} g{\left (x \right )} + g{\left (x \right )} \cfrac{d}{d x} f{\left (x \right )}

$\cfrac{d}{d x}\left(f{\left (x \right )} g{\left (x \right )}\right)=f{\left (x \right )} \cfrac{d}{d x} g{\left (x \right )} + g{\left (x \right )} \cfrac{d}{d x} f{\left (x \right )}$

In [20]:
t,a_x,a_y,a_z,b_x,b_y,b_z=symbols('t,a_x:z,b_x:z')
a=Matrix([a_x(t),a_y(t),a_z(t)])
b=Matrix([b_x(t),b_y(t),b_z(t)])
(a,b)
Out[20]:
$$\left ( \left[\begin{matrix}\operatorname{a_{x}}{\left (t \right )}\\\operatorname{a_{y}}{\left (t \right )}\\\operatorname{a_{z}}{\left (t \right )}\end{matrix}\right], \quad \left[\begin{matrix}\operatorname{b_{x}}{\left (t \right )}\\\operatorname{b_{y}}{\left (t \right )}\\\operatorname{b_{z}}{\left (t \right )}\end{matrix}\right]\right )$$
In [21]:
str='\mathbf{a}='+latex(a.T)+'\\\\'+'\mathbf{b}='+latex(b.T)
print(str)
\mathbf{a}=\left[\begin{matrix}\operatorname{a_{x}}{\left (t \right )} & \operatorname{a_{y}}{\left (t \right )} & \operatorname{a_{z}}{\left (t \right )}\end{matrix}\right]\\\mathbf{b}=\left[\begin{matrix}\operatorname{b_{x}}{\left (t \right )} & \operatorname{b_{y}}{\left (t \right )} & \operatorname{b_{z}}{\left (t \right )}\end{matrix}\right]

$\mathbf{a}=\left[\begin{matrix}\operatorname{a_{x}}{\left (t \right )} & \operatorname{a_{y}}{\left (t \right )} & \operatorname{a_{z}}{\left (t \right )}\end{matrix}\right]\\\mathbf{b}=\left[\begin{matrix}\operatorname{b_{x}}{\left (t \right )} & \operatorname{b_{y}}{\left (t \right )} & \operatorname{b_{z}}{\left (t \right )}\end{matrix}\right] $

$\left(a(t)\dot{}b(t)\right)'=a'(t)\dot{}b(t)+a(t)\dot{}b'(t)$

In [22]:
eq1=diff(a.dot(b),evaluate=False)
eq2=diff(a.dot(b),evaluate=True)
eq3=diff(a).dot(b)+a.dot(diff(b))
(eq1,eq2,eq3)
Out[22]:
$$\left ( \frac{d}{d t}\left(\operatorname{a_{x}}{\left (t \right )} \operatorname{b_{x}}{\left (t \right )} + \operatorname{a_{y}}{\left (t \right )} \operatorname{b_{y}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \operatorname{b_{z}}{\left (t \right )}\right), \quad \operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} + \operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} + \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )} + \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )} + \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )}, \quad \operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} + \operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} + \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )} + \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )} + \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )}\right )$$
In [23]:
eq2==eq3
Out[23]:
True

$\operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} + \operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} + \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )} + \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )} + \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )}$

$\left(a(t)\times{}b(t)\right)'=a'(t)\times{}b(t)+a(t)\times{}b'(t)$

In [24]:
eq1=diff(a.cross(b),evaluate=False)
eq2=diff(a.cross(b),evaluate=True)
eq3=diff(a).cross(b)+a.cross(diff(b))
(eq1,eq2,eq3)
Out[24]:
$$\left ( \frac{d}{d t} \left[\begin{matrix}\operatorname{a_{y}}{\left (t \right )} \operatorname{b_{z}}{\left (t \right )} - \operatorname{a_{z}}{\left (t \right )} \operatorname{b_{y}}{\left (t \right )}\\- \operatorname{a_{x}}{\left (t \right )} \operatorname{b_{z}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \operatorname{b_{x}}{\left (t \right )}\\\operatorname{a_{x}}{\left (t \right )} \operatorname{b_{y}}{\left (t \right )} - \operatorname{a_{y}}{\left (t \right )} \operatorname{b_{x}}{\left (t \right )}\end{matrix}\right], \quad \left[\begin{matrix}\operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} - \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} - \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )} + \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )}\\- \operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} + \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )} - \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )}\\\operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} - \operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} - \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )} + \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )}\end{matrix}\right], \quad \left[\begin{matrix}\operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} - \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} - \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )} + \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )}\\- \operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{z}}{\left (t \right )} + \operatorname{a_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} + \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{z}}{\left (t \right )} - \operatorname{b_{z}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )}\\\operatorname{a_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{y}}{\left (t \right )} - \operatorname{a_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{b_{x}}{\left (t \right )} - \operatorname{b_{x}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{y}}{\left (t \right )} + \operatorname{b_{y}}{\left (t \right )} \frac{d}{d t} \operatorname{a_{x}}{\left (t \right )}\end{matrix}\right]\right )$$
In [25]:
eq2==eq3
Out[25]:
True

連鎖律
$f(x(t),y(t))$,$f(x(t),y(t),z(t))$ の微分
$ \cfrac{df}{dt}=\cfrac{\partial{}f}{\partial{}x}\dot{}\cfrac{dx}{dt}+\cfrac{\partial{}f}{\partial{}y}\dot{}\cfrac{dy}{dt} $
$ \cfrac{df}{dt}=\cfrac{\partial{}f}{\partial{}x}\dot{}\cfrac{dx}{dt}+\cfrac{\partial{}f}{\partial{}y}\dot{}\cfrac{dy}{dt}+\cfrac{\partial{}f}{\partial{}z}\dot{}\cfrac{dz}{dt} $
$f(x(u,v),y(u,v)),f(x(u,v),y(u,v),z(u,v))$の$u$による偏微分
$ \cfrac{\partial{}f}{\partial{}u}=\cfrac{\partial{}f}{\partial{}x}\dot{}\cfrac{\partial{}x}{\partial{}u}+ \cfrac{\partial{}f}{\partial{}y}\dot{}\cfrac{\partial{}y}{\partial{}u}\\ \cfrac{\partial{}f}{\partial{}u}=\cfrac{\partial{}f}{\partial{}x}\dot{}\cfrac{\partial{}x}{\partial{}u}+ \cfrac{\partial{}f}{\partial{}y}\dot{}\cfrac{\partial{}y}{\partial{}u}+ \cfrac{\partial{}f}{\partial{}z}\dot{}\cfrac{\partial{}z}{\partial{}u} $

In [26]:
t=symbols('t')
x=Function('x')(t)
y=Function('y')(t)
f=Function('f')(x,y)
In [27]:
eq1=diff(f,t,evaluate=False)
eq2=diff(f,t,evaluate=True)
(eq1,eq2)
Out[27]:
$$\left ( \frac{d}{d t} f{\left (x{\left (t \right )},y{\left (t \right )} \right )}, \quad \frac{d}{d t} x{\left (t \right )} \left. \frac{\partial}{\partial \xi_{1}} f{\left (\xi_{1},y{\left (t \right )} \right )} \right|_{\substack{ \xi_{1}=x{\left (t \right )} }} + \frac{d}{d t} y{\left (t \right )} \left. \frac{\partial}{\partial \xi_{2}} f{\left (x{\left (t \right )},\xi_{2} \right )} \right|_{\substack{ \xi_{2}=y{\left (t \right )} }}\right )$$

$\xi_1$に$x(t)$、$\xi_2$に$y(t)$を代入しなさいとでます。Latexで消すことにします。

In [28]:
str=latex(eq1)+'='+latex(eq2)
str=str.replace('\\right|_{\\substack{ \\xi_{1}=x{\\left (t \\right )} }}','')
str=str.replace('\\right|_{\\substack{ \\xi_{2}=y{\\left (t \\right )} }}','')
str=str.replace('\\left.','')
str=str.replace('\\xi_{1}','x(t)').replace('\\xi_{2}','y(t)') #代入
str=str.replace('frac','cfrac')
print(str)
\cfrac{d}{d t} f{\left (x{\left (t \right )},y{\left (t \right )} \right )}=\cfrac{d}{d t} x{\left (t \right )}  \cfrac{\partial}{\partial x(t)} f{\left (x(t),y{\left (t \right )} \right )}  + \cfrac{d}{d t} y{\left (t \right )}  \cfrac{\partial}{\partial y(t)} f{\left (x{\left (t \right )},y(t) \right )} 

$\cfrac{d}{d t} f{\left (x{\left (t \right )},y{\left (t \right )} \right )}=\cfrac{d}{d t} x{\left (t \right )} \cfrac{\partial}{\partial x(t)} f{\left (x(t),y{\left (t \right )} \right )} + \cfrac{d}{d t} y{\left (t \right )} \cfrac{\partial}{\partial y(t)} f{\left (x{\left (t \right )},y(t) \right )} $

In [29]:
reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [30]:
from sympy import *
init_printing()

$ x(t)=\cos{t} $
$ y(t)=\cos{t} $
$ f(x,y)=x^2y+2y$
$ \cfrac{df}{dt}= \cfrac{\partial{f}}{\partial{x}}\cfrac{\partial{x}}{\partial{t}}+ \cfrac{\partial{f}}{\partial{y}}\cfrac{\partial{y}}{\partial{t}}$
であるとき$f(x,y)$の$x(t)$と$y(t)$を$t$の式にして$t$で微分したものと、公式を使って$x$、$y$、$t$で偏微分した結果が一緒であることを示します。

In [31]:
t=symbols('t')
In [32]:
(x,y)=(cos(t),sin(t))
f=x**2*y+2*y
eq1=diff(f,t)
eq2=diff(f,x)*diff(x,t)+diff(f,y)*diff(y,t)
(eq1,eq2,expand(eq2))
Out[32]:
$$\left ( - 2 \sin^{2}{\left (t \right )} \cos{\left (t \right )} + \cos^{3}{\left (t \right )} + 2 \cos{\left (t \right )}, \quad \left(\cos^{2}{\left (t \right )} + 2\right) \cos{\left (t \right )} - 2 \sin^{2}{\left (t \right )} \cos{\left (t \right )}, \quad - 2 \sin^{2}{\left (t \right )} \cos{\left (t \right )} + \cos^{3}{\left (t \right )} + 2 \cos{\left (t \right )}\right )$$
In [33]:
[eq1==eq2,eq1==expand(eq2)]
Out[33]:
[False, True]

勾配

位置$\boldsymbol{x}=(x,y,z)$で$f(\boldsymbol{x})=f(x,y,z)$の値を持つスカラー場に対して、 ベクトル場$\mathrm{grad}f(\boldsymbol{x})$を
$\mathrm{grad}f(\boldsymbol{x})=\left( \cfrac{\partial{}f(x,y,z)}{\partial{}x}+ \cfrac{\partial{}f(x,y,z)}{\partial{}y}+ \cfrac{\partial{}f(x,y,z)}{\partial{}z} \right) =\nabla{}f$
と定めたものを勾配という。

In [34]:
from sympy import *
init_printing()

$f(x,y,z)=e^{x} \cos{\left (y z \right )}$の勾配

In [35]:
x,y,z=symbols('x,y,z')
f=exp(x)*cos(y*z)
f
Out[35]:
$$e^{x} \cos{\left (y z \right )}$$
In [36]:
eq=Matrix([diff(f,x),diff(f,y),diff(f,z)])
eq
Out[36]:
$$\left[\begin{matrix}e^{x} \cos{\left (y z \right )}\\- z e^{x} \sin{\left (y z \right )}\\- y e^{x} \sin{\left (y z \right )}\end{matrix}\right]$$
In [37]:
str='\\nabla{f}='+latex(eq)
print(str)
\nabla{f}=\left[\begin{matrix}e^{x} \cos{\left (y z \right )}\\- z e^{x} \sin{\left (y z \right )}\\- y e^{x} \sin{\left (y z \right )}\end{matrix}\right]

$\nabla{f}=\left[\begin{matrix}e^{x} \cos{\left (y z \right )}\\- z e^{x} \sin{\left (y z \right )}\\- y e^{x} \sin{\left (y z \right )}\end{matrix}\right] $

CoordSys3Dを使う場合

In [38]:
from sympy.vector import CoordSys3D
R=CoordSys3D('R')

R.i、R.j、R.kがx,y,z座標のベクトル、R.x、R.y、R.zがx,y,z座標の値となります。 R.i、R.j、R.kの内積は互いにゼロです。

In [39]:
[R.i.dot(R.j),R.j.dot(R.k),R.k.dot(R.i)]
Out[39]:
$$\left [ 0, \quad 0, \quad 0\right ]$$

R.i、R.j、R.kの3つのうちの1つの外積は残り1つと同じです。

In [40]:
[R.i.cross(R.j)-R.k,R.j.cross(R.k)-R.i,R.k.cross(R.i)-R.j]
Out[40]:
$$\left [ \mathbf{\hat{0}}, \quad \mathbf{\hat{0}}, \quad \mathbf{\hat{0}}\right ]$$
In [41]:
f=exp(R.x)*cos(R.y*R.z)
f
Out[41]:
$$e^{\mathbf{{x}_{R}}} \cos{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )}$$
In [42]:
from sympy.vector import gradient
eq=gradient(f)
eq
Out[42]:
$$(e^{\mathbf{{x}_{R}}} \cos{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )})\mathbf{\hat{i}_{R}} + (- e^{\mathbf{{x}_{R}}} \sin{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )} \mathbf{{z}_{R}})\mathbf{\hat{j}_{R}} + (- e^{\mathbf{{x}_{R}}} \sin{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )} \mathbf{{y}_{R}})\mathbf{\hat{k}_{R}}$$

$\mathbf{\hat{i}_{R}}$、$\mathbf{\hat{j}_{R}}$、$\mathbf{\hat{k}_{R}}$でなくベクトル風にしてみます。

In [43]:
eq1=(eq.coeff(R.i),eq.coeff(R.j),eq.coeff(R.k))
eq1
Out[43]:
$$\left ( e^{\mathbf{{x}_{R}}} \cos{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )}, \quad - e^{\mathbf{{x}_{R}}} \sin{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )} \mathbf{{z}_{R}}, \quad - e^{\mathbf{{x}_{R}}} \sin{\left (\mathbf{{y}_{R}} \mathbf{{z}_{R}} \right )} \mathbf{{y}_{R}}\right )$$
In [44]:
str='\\nabla{f}='+latex(eq1)
str=str.replace('\\mathbf{{x}_{R}}','x').replace('\\mathbf{{y}_{R}}','y').replace('\\mathbf{{z}_{R}}','z')
print(str)
\nabla{f}=\left ( e^{x} \cos{\left (y z \right )}, \quad - e^{x} \sin{\left (y z \right )} z, \quad - e^{x} \sin{\left (y z \right )} y\right )

$\nabla{f}=\left ( e^{x} \cos{\left (y z \right )}, \quad - e^{x} \sin{\left (y z \right )} z, \quad - e^{x} \sin{\left (y z \right )} y\right )$
$\sin(yz)z$は$z\sin(yz)$です。

$n$次元の勾配
$\nabla{}f=\left( \cfrac{\partial{}f(x_1,x_2,\cdots,x_n)}{\partial{x_1}}, \cfrac{\partial{}f(x_1,x_2,\cdots,x_n)}{\partial{x_2}}, \cdots, \cfrac{\partial{}f(x_1,x_2,\cdots,x_n)}{\partial{x_n}}, \right)$

発散

二次元スカラー場$f(x,y)$に対して
$\nabla{f}$と等高線$f=C$は直交する
$f(x(t),y(t))=C$を微分
$\cfrac{\partial{}f\left(x(t),y(t)\right)}{\partial{}x}\dot{}\cfrac{dx(t)}{dt}+ \cfrac{\partial{}f\left(x(t),y(t)\right)}{\partial{}y}\dot{}\cfrac{dy(t)}{dt}=0\\ \nabla{}f\left(x(t),y(t)\right)\dot{}f\left(x(t),y(t)\right)=0$

位置$\boldsymbol{x}=(x,y,z)$において、ベクトル
$\boldsymbol{A}(x,y,z)=\left(A_x(x,y,z),A_y(x,y,z),A_z(x,y,z)\right)$
を対応させるベクトル場に対して、スカラー場$\mathrm{div}\boldsymbol{A}(\boldsymbol{x})$を
$\mathrm{div}\boldsymbol{A}(\boldsymbol{x})=\cfrac{\partial{}A_x}{\partial{}x}+ \cfrac{\partial{}A_y}{\partial{}y}+\cfrac{\partial{}A_z}{\partial{}z}= \nabla{}\dot{}\boldsymbol{A}(\boldsymbol{x}) $
と定めたものを発散という

In [45]:
reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [46]:
from sympy import *
init_printing()

$\mathbf{A}=\left[\begin{matrix}\operatorname{A_{x}}{\left (x,y,z \right )}\\\operatorname{A_{y}}{\left (x,y,z \right )}\\\operatorname{A_{z}}{\left (x,y,z \right )}\end{matrix}\right]$

In [47]:
A_x,A_y,A_z,x,y,z=symbols('A_x,A_y,A_z,x,y,z')
A=Matrix([A_x(x,y,z),A_y(x,y,z),A_z(x,y,z)])
A
Out[47]:
$$\left[\begin{matrix}\operatorname{A_{x}}{\left (x,y,z \right )}\\\operatorname{A_{y}}{\left (x,y,z \right )}\\\operatorname{A_{z}}{\left (x,y,z \right )}\end{matrix}\right]$$

$\nabla{}\dot{}\mathbf{A}=\frac{\partial}{\partial x} \operatorname{A_{x}}{\left (x,y,z \right )} + \frac{\partial}{\partial y} \operatorname{A_{y}}{\left (x,y,z \right )} + \frac{\partial}{\partial z} \operatorname{A_{z}}{\left (x,y,z \right )}$の計算

In [48]:
diff(A_x,x)+diff(A_y,y)+diff(A_z,z)
Out[48]:
$$0$$

このままでは微分にならないので工夫します。

In [49]:
Matrix([A[0],A[1],A[2]])
Out[49]:
$$\left[\begin{matrix}\operatorname{A_{x}}{\left (x,y,z \right )}\\\operatorname{A_{y}}{\left (x,y,z \right )}\\\operatorname{A_{z}}{\left (x,y,z \right )}\end{matrix}\right]$$
In [50]:
diff(A[0],x)+diff(A[1],y)+diff(A[2],z)
Out[50]:
$$\frac{\partial}{\partial x} \operatorname{A_{x}}{\left (x,y,z \right )} + \frac{\partial}{\partial y} \operatorname{A_{y}}{\left (x,y,z \right )} + \frac{\partial}{\partial z} \operatorname{A_{z}}{\left (x,y,z \right )}$$

CoordSys3Dを使う場合

In [51]:
from sympy.vector import CoordSys3D,divergence
R=CoordSys3D('R')
A_i,A_j,A_k=symbols('A_i,A_y,A_z')
A=A_x(R.x,R.y,R.z)*R.i+A_y(R.x,R.y,R.z)*R.j+A_z(R.x,R.y,R.z)*R.k
A
Out[51]:
$$(\operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{i}_{R}} + (\operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{j}_{R}} + (\operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{k}_{R}}$$
In [52]:
divergence(A)
Out[52]:
$$\left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{x}}{\left (\xi_{2},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{x}_{R}} }} + \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\xi_{2},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{y}_{R}} }} + \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\xi_{2} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{z}_{R}} }}$$
In [53]:
str=latex(divergence(A))
str=str.replace('\\xi_{2}','\\mathbf{{x}_{R}}',3).replace('\\xi_{2}','\\mathbf{{y}_{R}}',3).replace('\\xi_{2}','\\mathbf{{z}_{R}}',3)
str=str.replace('\\mathbf{{x}_{R}}=\\mathbf{{x}_{R}}','').replace('\\mathbf{{y}_{R}}=\\mathbf{{y}_{R}}','').replace('\\mathbf{{z}_{R}}=\\mathbf{{z}_{R}}','')
str=str.replace('\\left.','').replace('\\right|_{\\substack{  }}','').replace('frac','cfrac')
print(str)
 \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )}  +  \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )}  +  \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} 

$\xi_2$を$\mathbf{X_R}$,$\mathbf{Y_R}$,$\mathbf{Z_R}$に置き換えると
$ \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} + \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} + \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} $

$\xi_2$を$\mathbf{X_R}$,$\mathbf{Y_R}$,$\mathbf{Z_R}$に置き換えると
$\cfrac{\partial}{\partial\mathbf{X_R}}A_x(\mathbf{X_R},\mathbf{Y_R},\mathbf{Z_R})+ \cfrac{\partial}{\partial\mathbf{Y_R}}A_y(\mathbf{X_R},\mathbf{Y_R},\mathbf{Z_R})+ \cfrac{\partial}{\partial\mathbf{Z_R}}A_z(\mathbf{X_R},\mathbf{Y_R},\mathbf{Z_R}) $
となります。

見やすくするために${\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )}$を無しにします。

In [54]:
str=str.replace('{\\left (\\mathbf{{x}_{R}},\\mathbf{{y}_{R}},\\mathbf{{z}_{R}} \\right )}','')
print(str)
 \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{x}}  +  \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{y}}  +  \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{z}} 

$ \nabla{}\dot{}\mathbf{A}= \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{x}} + \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{y}} + \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{z}} $

保存則
密度$\rho(\boldsymbol{x})$が一定であれば
$\cfrac{\partial\rho(\boldsymbol{x})}{\partial{t}}+\nabla\dot{}\boldsymbol{A}(\boldsymbol{x})=0 $

回転

位置$\boldsymbol{x}=(x,y,z)$において
$\boldsymbol{A}(x,y,z)=\left(A_x(x,y,z),A_y(x,y,z),A_z(x,y,z)\right)$
と与えられるベクトル場に対して、ベクトル場$\mathrm{rot}\boldsymbol{A}(\boldsymbol{x})$を
$\mathrm{rot}\boldsymbol{A}(\boldsymbol{x})=\left( \cfrac{\partial A_z}{\partial y}-\cfrac{\partial A_y}{\partial z}, \cfrac{\partial A_x}{\partial z}-\cfrac{\partial A_z}{\partial x}, \cfrac{\partial A_y}{\partial x}-\cfrac{\partial A_x}{\partial y} \right)=\nabla \times \boldsymbol{A}(\boldsymbol{x}) $
と定めたものを$\boldsymbol{A}(\boldsymbol{x})$の回転という

In [55]:
reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [56]:
from sympy import *
init_printing()
In [57]:
A_x,A_y,A_z,x,y,z=symbols('A_x,A_y,A_z,x,y,z')
A=Matrix([A_x(x,y,z),A_y(x,y,z),A_z(x,y,z)])
A
Out[57]:
$$\left[\begin{matrix}\operatorname{A_{x}}{\left (x,y,z \right )}\\\operatorname{A_{y}}{\left (x,y,z \right )}\\\operatorname{A_{z}}{\left (x,y,z \right )}\end{matrix}\right]$$

$\nabla{}\times{}\mathbf{A}(\mathbf{x})$の計算

diff(A_z,y)にするとゼロになるので、diff(A[2],y)などとします。

In [58]:
rotA=Matrix([diff(A[2],y)-diff(A[1],z),diff(A[0],z)-diff(A[2],x),diff(A[1],x)-diff(A[0],y)])
eq=rotA
eq
Out[58]:
$$\left[\begin{matrix}- \frac{\partial}{\partial z} \operatorname{A_{y}}{\left (x,y,z \right )} + \frac{\partial}{\partial y} \operatorname{A_{z}}{\left (x,y,z \right )}\\\frac{\partial}{\partial z} \operatorname{A_{x}}{\left (x,y,z \right )} - \frac{\partial}{\partial x} \operatorname{A_{z}}{\left (x,y,z \right )}\\- \frac{\partial}{\partial y} \operatorname{A_{x}}{\left (x,y,z \right )} + \frac{\partial}{\partial x} \operatorname{A_{y}}{\left (x,y,z \right )}\end{matrix}\right]$$
In [59]:
str='\\nabla{}\\times{}\\mathbf{A}(\\mathbf{x})='+latex(eq).replace('frac','cfrac')
print(str)
\nabla{}\times{}\mathbf{A}(\mathbf{x})=\left[\begin{matrix}- \cfrac{\partial}{\partial z} \operatorname{A_{y}}{\left (x,y,z \right )} + \cfrac{\partial}{\partial y} \operatorname{A_{z}}{\left (x,y,z \right )}\\\cfrac{\partial}{\partial z} \operatorname{A_{x}}{\left (x,y,z \right )} - \cfrac{\partial}{\partial x} \operatorname{A_{z}}{\left (x,y,z \right )}\\- \cfrac{\partial}{\partial y} \operatorname{A_{x}}{\left (x,y,z \right )} + \cfrac{\partial}{\partial x} \operatorname{A_{y}}{\left (x,y,z \right )}\end{matrix}\right]

$ \nabla{}\times{}\mathbf{A}(\mathbf{x})=\left[\begin{matrix}- \cfrac{\partial}{\partial z} \operatorname{A_{y}}{\left (x,y,z \right )} + \cfrac{\partial}{\partial y} \operatorname{A_{z}}{\left (x,y,z \right )}\\\cfrac{\partial}{\partial z} \operatorname{A_{x}}{\left (x,y,z \right )} - \cfrac{\partial}{\partial x} \operatorname{A_{z}}{\left (x,y,z \right )}\\- \cfrac{\partial}{\partial y} \operatorname{A_{x}}{\left (x,y,z \right )} + \cfrac{\partial}{\partial x} \operatorname{A_{y}}{\left (x,y,z \right )}\end{matrix}\right] $

CoordSys3Dを使う場合

In [60]:
from sympy.vector import CoordSys3D,curl
R=CoordSys3D('R')
A=A_x(R.x,R.y,R.z)*R.i+A_y(R.x,R.y,R.z)*R.j+A_z(R.x,R.y,R.z)*R.k
A
Out[60]:
$$(\operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{i}_{R}} + (\operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{j}_{R}} + (\operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )})\mathbf{\hat{k}_{R}}$$
In [61]:
curl(A)
Out[61]:
$$(- \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{y}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\xi_{2} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{z}_{R}} }} + \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{z}}{\left (\mathbf{{x}_{R}},\xi_{2},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{y}_{R}} }})\mathbf{\hat{i}_{R}} + (\left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\xi_{2} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{z}_{R}} }} - \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{z}}{\left (\xi_{2},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{x}_{R}} }})\mathbf{\hat{j}_{R}} + (- \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{x}}{\left (\mathbf{{x}_{R}},\xi_{2},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{y}_{R}} }} + \left. \frac{\partial}{\partial \xi_{2}} \operatorname{A_{y}}{\left (\xi_{2},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{x}_{R}} }})\mathbf{\hat{k}_{R}}$$

$\xi_2$を$\mathbf{X_R}$,$\mathbf{Y_R}$,$\mathbf{Z_R}$に置き換え、 見やすくするために${\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )}$を無しにします。

In [62]:
str=latex(curl(A))
str=str.replace('\\xi_{2}','\\mathbf{{z}_{R}}',3).replace('\\xi_{2}','\\mathbf{{y}_{R}}',3).replace('\\xi_{2}','\\mathbf{{z}_{R}}',3)
str=str.replace('\\xi_{2}','\\mathbf{{x}_{R}}',3).replace('\\xi_{2}','\\mathbf{{y}_{R}}',3).replace('\\xi_{2}','\\mathbf{{x}_{R}}',3)
str=str.replace('\\mathbf{{x}_{R}}=\\mathbf{{x}_{R}}','').replace('\\mathbf{{y}_{R}}=\\mathbf{{y}_{R}}','').replace('\\mathbf{{z}_{R}}=\\mathbf{{z}_{R}}','')
str=str.replace('{\\left (\\mathbf{{x}_{R}},\\mathbf{{y}_{R}},\\mathbf{{z}_{R}} \\right )}','')
str=str.replace('\\left.','').replace('\\right|_{\\substack{  }}','').replace('frac','cfrac')
str=str.replace('(','\\left(').replace(')','\\right)')
print(str)
\left(-  \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{y}}  +  \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{z}} \right)\mathbf{\hat{i}_{R}} + \left( \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{x}}  -  \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{z}} \right)\mathbf{\hat{j}_{R}} + \left(-  \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{x}}  +  \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{y}} \right)\mathbf{\hat{k}_{R}}

$ \left(- \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{y}} + \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{z}} \right)\mathbf{\hat{i}_{R}} + \left( \cfrac{\partial}{\partial \mathbf{{z}_{R}}} \operatorname{A_{x}} - \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{z}} \right)\mathbf{\hat{j}_{R}} + \left(- \cfrac{\partial}{\partial \mathbf{{y}_{R}}} \operatorname{A_{x}} + \cfrac{\partial}{\partial \mathbf{{x}_{R}}} \operatorname{A_{y}} \right)\mathbf{\hat{k}_{R}} $

ラプラシアン

$xyz$座標でのスカラー場$f(x,y,z)$に関して、スカラー場$\Delta{}f$を
$\Delta{}f=\cfrac{\partial{}^2 f(x,y,z)}{\partial{x^2}}+\cfrac{\partial{}^2 f(x,y,z)}{\partial{y^2}}+ \cfrac{\partial{}^2 f(x,y,z)}{\partial{z^2}}$
と定める。$\Delta{f}$をラプラシアンという。

In [63]:
reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [64]:
from sympy import *
init_printing()
f,x,y,z=symbols('f,x,y,z')

$\mathrm{div}(\mathrm{grad} f)=\nabla{}\dot{}(\nabla{f})=\nabla\dot{}\left(\cfrac{\partial{f}}{\partial{x}}, \cfrac{\partial{f}}{\partial{y}},\cfrac{\partial{f}}{\partial{z}}\right)$ $=\cfrac{\partial^2{f}}{\partial{x^2}}+\cfrac{\partial^2{f}}{\partial{y^2}}+\cfrac{\partial^2{f}}{\partial{z^2}}= \nabla{}^2 f=\Delta{f}$
になるのを見ていきます。

In [65]:
gradf=[diff(f(x,y,z),x),diff(f(x,y,z),y),diff(f(x,y,z),z)]
gradf
Out[65]:
$$\left [ \frac{\partial}{\partial x} f{\left (x,y,z \right )}, \quad \frac{\partial}{\partial y} f{\left (x,y,z \right )}, \quad \frac{\partial}{\partial z} f{\left (x,y,z \right )}\right ]$$
In [66]:
divgradf=diff(gradf[0],x)+diff(gradf[1],y)+diff(gradf[2],z)
divgradf
Out[66]:
$$\frac{\partial^{2}}{\partial x^{2}} f{\left (x,y,z \right )} + \frac{\partial^{2}}{\partial y^{2}} f{\left (x,y,z \right )} + \frac{\partial^{2}}{\partial z^{2}} f{\left (x,y,z \right )}$$

CoordSys3Dを使う場合

In [67]:
from sympy.vector import CoordSys3D,gradient,divergence
R=CoordSys3D('R')
In [68]:
eq=divergence(gradient(f(R.x,R.y,R.z)))
eq
Out[68]:
$$\left. \frac{\partial^{2}}{\partial \xi_{2}^{2}} f{\left (\xi_{2},\mathbf{{y}_{R}},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{x}_{R}} }} + \left. \frac{\partial^{2}}{\partial \xi_{2}^{2}} f{\left (\mathbf{{x}_{R}},\xi_{2},\mathbf{{z}_{R}} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{y}_{R}} }} + \left. \frac{\partial^{2}}{\partial \xi_{2}^{2}} f{\left (\mathbf{{x}_{R}},\mathbf{{y}_{R}},\xi_{2} \right )} \right|_{\substack{ \xi_{2}=\mathbf{{z}_{R}} }}$$

見やすくします。

In [69]:
str='\\nabla{}^{2}f='+latex(eq)
str=str.replace('\\xi_{2}','\\mathbf{{x}_{R}}',3).replace('\\xi_{2}','\\mathbf{{y}_{R}}',3).replace('\\xi_{2}','\\mathbf{{z}_{R}}',3)
str=str.replace('\\mathbf{{x}_{R}}=\\mathbf{{x}_{R}}','').replace('\\mathbf{{y}_{R}}=\\mathbf{{y}_{R}}','').replace('\\mathbf{{z}_{R}}=\\mathbf{{z}_{R}}','')
str=str.replace('{\\left (\\mathbf{{x}_{R}},\\mathbf{{y}_{R}},\\mathbf{{z}_{R}} \\right )}','')
str=str.replace('\\left.','').replace('\\right|_{\\substack{  }}','').replace('frac','cfrac')
# str=str.replace('(','\\left(').replace(')','\\right)')
print(str)
\nabla{}^{2}f= \cfrac{\partial^{2}}{\partial \mathbf{{x}_{R}}^{2}}  f  +  \cfrac{\partial^{2}}{\partial \mathbf{{y}_{R}}^{2}}  f  +  \cfrac{\partial^{2}}{\partial \mathbf{{z}_{R}}^{2}}  f 

$\nabla{}^{2}f= \cfrac{\partial^{2}}{\partial \mathbf{{x}_{R}}^{2}} f + \cfrac{\partial^{2}}{\partial \mathbf{{y}_{R}}^{2}} f + \cfrac{\partial^{2}}{\partial \mathbf{{z}_{R}}^{2}} f $