2020年4月26日日曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第20章(合成微分律と勾配ベクトル)、1(合成微分律)の練習問題10の解答を求めてみる。


  1. g u = f u = f x , f y · cos θ , sin θ = f x cos θ + f y sin θ g v = f x , f y · - sin θ , cos θ = - f x sin θ + f y cos θ g u 2 + g v 2 = f x 2 cos 2 θ + 2 f x f y sin θ cos θ + f y 2 sin 2 θ + f x 2 sin 2 θ - 2 f x f y sin θ cos θ + f y 2 cos 2 θ = f x 2 + f y 2

コード

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

print('10.')

x, y, u, v, theta = symbols('x, y, u, v, θ', real=True)
f = 2 * x + y ** 2
xy = {x: u * cos(theta) - v * sin(theta),
      y: u * sin(theta) + v * cos(theta)}
g = f.subs(xy)


class TestDerivativeChainRule(TestCase):
    def test(self):
        left = g.diff(u, 1) ** 2 + g.diff(v, 1) ** 2
        right = (f.diff(x, 1) ** 2 + f.diff(y, 1) ** 2).subs(xy)
        for _ in range(10):
            d = {o: random.randrange(-100, 101) for o in [x, y, u, v, theta]}
            self.assertEqual(
                float(left.subs(d)),
                float(right.subs(d)))


p = plot3d(*[g.subs({theta: o}) for o in range(-5, 5)], show=True)
p.save('sample10.png')

if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample10.py -v
10.
test (__main__.TestDerivativeChainRule) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.131s

OK
%

0 コメント:

コメントを投稿