## 2020年4月21日火曜日

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

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

とおく。

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

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Function, pprint, Derivative
from sympy.plotting import plot3d

print('5.')

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

class TestPartialDerivative(TestCase):
def test(self):
u = Function('u')(x - y, y - x)
self.assertEqual(u.diff(x, 1) + u.diff(y, 1), 0)

s, t = symbols('s, t')
f = 2 * s ** 2 - 3 * t + 1
g = f.subs({s: x - y, t: y - x})

for o in [x, y]:
d = Derivative(g, o, 1)
for h in [d, d.doit()]:
pprint(h)
print()
pprint(g.diff(x, 1) + g.diff(y, 1))

p = plot3d(g,
show=True)
p.save('sample5.png')

if __name__ == "__main__":
main()


% ./sample5.py -v
5.
∂ ⎛                     2    ⎞
──⎝3⋅x - 3⋅y + 2⋅(x - y)  + 1⎠
∂x

4⋅x - 4⋅y + 3

∂ ⎛                     2    ⎞
──⎝3⋅x - 3⋅y + 2⋅(x - y)  + 1⎠
∂y

-4⋅x + 4⋅y - 3

0
test (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.009s

OK
%