## 2020年4月17日金曜日

### 数学 - Python - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 変数、内積

• $\begin{array}{l}\frac{\partial z}{\partial r}\\ =\frac{\partial }{\partial r}f\left(x,y\right)\\ =\frac{\partial }{\partial r}f\left(u\left(r,s,t\right),v\left(r,s,t\right)\right)\\ =\left(gradf\right)·\left(\frac{du}{dr},\frac{dv}{dr}\right)\\ =\left(\frac{df}{\mathrm{dx}},\frac{df}{\mathrm{dy}}\right)·\left(\frac{du}{dr},\frac{dv}{dr}\right)\\ =\frac{df}{\mathrm{dx}}\frac{du}{dr}+\frac{df}{\mathrm{dy}}\frac{dv}{dr}\\ =\left({D}_{1}f\right)\frac{du}{dr}+\left({D}_{2}f\right)\frac{dv}{dr}\end{array}$

• $\frac{\partial z}{\partial t}=\frac{df}{\mathrm{dx}}\frac{du}{\mathrm{dt}}+\frac{df}{\mathrm{dy}}\frac{dv}{\mathrm{dt}}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Function

print('1.')

x, y, r, s, t = symbols('x, y, r, s, t')
f = Function('f')(x, y)
u = Function('u')(r, s, t)
v = Function('v')(r, s, t)

class TestSyntheticDerivative(TestCase):
def test_dr(self):
self.assertEqual(
f.subs({x: u, y: v}).diff(t, 1).simplify(),
(f.diff(x, 1) * u.diff(t, 1) +
f.diff(y, 1) * v.diff(t, 1)).subs({x: u, y: v}))

def test_dt(self):
self.assertEqual(
f.subs({x: u, y: v}).diff(r, 1).simplify(),
(f.diff(x, 1) * u.diff(r, 1) +
f.diff(y, 1) * v.diff(r, 1)).subs({x: u, y: v}))

if __name__ == "__main__":
main()


% ./sample1.py -v
1.
test_dr (__main__.TestSyntheticDerivative) ... ok
test_dt (__main__.TestSyntheticDerivative) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.377s

OK
％