2020年4月22日水曜日

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

1. $\begin{array}{l}s=\frac{y}{x}\\ t=\frac{z}{x}\end{array}$

とおく。

$\begin{array}{l}\frac{\partial u}{\partial x}\\ =\frac{\partial u}{\partial x}{x}^{3}f\left(s,t\right)\\ =3{x}^{2}f\left(s,t\right)+{x}^{3}\frac{\partial }{\partial x}f\left(s,t\right)\\ =3{x}^{2}f\left(s,t\right)+{x}^{3}\left(\frac{df}{ds},\frac{df}{\mathrm{dt}}\right)·\left(-\frac{y}{{x}^{2}},-\frac{z}{{x}^{2}}\right)\\ =3{x}^{2}f\left(s,t\right)-x\left(y\frac{df}{ds}+z\frac{df}{\mathrm{dt}}\right)\\ \frac{\partial u}{\partial y}\\ ={x}^{3}\left(\frac{df}{ds},\frac{df}{\mathrm{dt}}\right)·\left(\frac{1}{x},0\right)\\ ={x}^{2}\frac{df}{ds}\\ \frac{\partial u}{\partial z}\\ ={x}^{3}\left(\frac{df}{ds},\frac{df}{\mathrm{dt}}\right)·\left(0,\frac{1}{x}\right)\\ ={x}^{2}\frac{df}{\mathrm{dt}}\\ x\frac{\partial u}{\partial x}+y\frac{\partial u}{\partial y}+z\frac{\partial u}{\partial z}\\ =3{x}^{3}f\left(s,t\right)-{x}^{2}y\frac{df}{ds}-{x}^{2}z\frac{df}{\mathrm{dt}}+{x}^{2}y\frac{df}{ds}+{x}^{2}z\frac{\mathrm{dt}}{ds}\\ =3u\end{array}$

コード

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

print('6.')

x, y, z = symbols('x, y, z')

class TestDerivativeChainRule(TestCase):
def test(self):
f = Function('f')(y / x, z / x)
u = x ** 3 * f
self.assertEqual(
(x * u.diff(x, 1) + y * u.diff(y, 1) + z * u.diff(z, 1)).simplify(),
3 * u)

if __name__ == "__main__":
main()


% ./sample6.py -v
6.
test (__main__.TestDerivativeChainRule) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.409s

OK
%