学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第20章(合成微分律と勾配ベクトル)、1(合成微分律)の練習問題2の解答を求めてみる。
コード
#!/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()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./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
%
0 コメント:
コメントを投稿