2020年4月18日土曜日

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

1. $\begin{array}{l}\frac{d}{\mathrm{dx}}f\left(x,y,z\right)\\ =\frac{d}{\mathrm{dx}}\left({x}^{3}+3xyz-{y}^{2}z\right)\\ =3{x}^{2}+3yz\\ \frac{\partial }{\partial y}f\left(x,y,z\right)=3xz-2yz\\ \frac{\partial }{\partial s}f\left(x,y,z\right)\\ =\left(3{x}^{2}+3yz,3xz-2yz,3xy-{y}^{2}\right)·\left(1,-1,2s\right)\\ =3{x}^{2}+3yz-3xz+2z+6sxy-2s{y}^{2}\\ =3{x}^{2}+5yz-3xz+6sxy-2s{y}^{2}\\ \frac{\partial }{\partial t}f\left(x,y,z\right)\\ =\left(gradf\right)·\left(2,-1,2t\right)\\ =6{x}^{2}+6yz-3xz+2yz+6txy-2t{y}^{2}\\ =6{x}^{2}+8yz-3xz+6txy-2t{y}^{2}\end{array}$

2. $\begin{array}{l}\frac{d}{\mathrm{dx}}f\left(x,y,z\right)\\ =\frac{d}{\mathrm{dx}}\frac{x+y}{1-xy}\\ =\frac{\left(1-xy\right)-\left(x+y\right)\left(-y\right)}{{\left(1-xy\right)}^{2}}\\ =\frac{{y}^{2}+1}{{\left(1-xy\right)}^{2}}\\ \frac{df}{\mathrm{dy}}=\frac{{x}^{2}+1}{{\left(1-xy\right)}^{2}}\\ \frac{df}{ds}\\ =\left(gradf\right)·\left(0,\left(3-s\right)\right)\\ =\frac{\left({1}^{2}+1\right)\left(3-s\right)}{{\left(1-xy\right)}^{2}}\\ \frac{df}{\mathrm{dt}}\\ =\left(gradf\right)·\left(2\mathrm{cos}2t,-3\mathrm{sin}\left(3t-s\right)\right)\\ =\frac{2\left({y}^{2}+1\right)\mathrm{cos}2t-3\left({x}^{2}+1\right)\mathrm{sin}\left(3t-s\right)}{{\left(1-xy\right)}^{2}}\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, sin, cos
from sympy.plotting import plot3d

print('2.')

x, y, z, s, t = symbols('x, y, z, s, t')
fa = x ** 3 + 3 * x * y * z - y ** 2 * z
xa = 2 * t + s
ya = -t - s
za = t ** 2 + s ** 2
da = {x: xa, y: ya, z: za}
fb = (x + y) / (1 - x * y)
xb = sin(2 * t)
yb = cos(3 * t - s)
db = {x: xb, y: yb}

class TestSyntheticDerivative(TestCase):
def test_a_dx(self):
self.assertEqual(fa.diff(x, 1),
3 * x ** 2 + 3 * y * z)

def test_a_dy(self):
self.assertEqual(fa.diff(y, 1),
3 * x * z - 2 * y * z)

def test_a_ds(self):
self.assertEqual(
fa.subs(da).diff(s, 1).simplify(),
(3 * x ** 2 + 5 * y * z - 3 * x * z + 6 * s *
x * y - 2 * s * y ** 2).subs(da).simplify()
)

def test_a_dt(self):
self.assertEqual(
fa.subs(da).diff(t, 1).simplify(),
(6 * x ** 2 + 8 * y * z - 3 * x * z + 6 *
t * x * y - 2 * t * y ** 2).subs(da).simplify()
)

def test_b_dx(self):
self.assertEqual(fb.diff(x, 1).factor(),
((y ** 2 + 1) / (1 - x * y) ** 2).factor())

def test_b_dy(self):
self.assertEqual(fb.diff(y, 1).factor(),
((x ** 2 + 1) / (1 - x * y) ** 2).factor())

def test_b_ds(self):
self.assertEqual(
fb.subs(db).diff(s, 1).factor(),
((x ** 2 + 1) * sin(3 * t - s) / (1 - x * y) ** 2).subs(db).factor())

def test_b_dt(self):
self.assertEqual(
fb.subs(db).diff(t, 1).factor(),
((2 * (y ** 2 + 1) * cos(2 * t) - 3 * (x ** 2 + 1)
* sin(3 * t - s)) / (1 - x * y) ** 2).subs(db).factor()
)

for i, f in enumerate([fa.subs(da), fb.subs(db)]):
p = plot3d(f, show=False)
p.save('sample2_{i}.png')
p.show()

if __name__ == "__main__":
main()


% ./sample2.py -v
2.
test_a_ds (__main__.TestSyntheticDerivative) ... ok
test_a_dt (__main__.TestSyntheticDerivative) ... ok
test_a_dx (__main__.TestSyntheticDerivative) ... ok
test_a_dy (__main__.TestSyntheticDerivative) ... ok
test_b_ds (__main__.TestSyntheticDerivative) ... ok
test_b_dt (__main__.TestSyntheticDerivative) ... ok
test_b_dx (__main__.TestSyntheticDerivative) ... ok
test_b_dy (__main__.TestSyntheticDerivative) ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.636s

OK
%