2019年12月30日月曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第17章(ベクトル)、3(スカラー積)の練習問題7の解答を求めてみる。



    • f , f = x , x = - 1 1 x 2 dx = 2 3

    • g , g = x 2 , x 2 = - 1 1 x 4 dx = 2 5

    • f , g = x , x 2 = - 1 1 x 3 dx = 0

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Integral, plot, Rational

print('7.')

x, c = symbols('x, c', real=True)
f = x
g = x ** 2


def dot(f, g):
    return Integral(f * g, (x, -1, 1)).doit()


class MyTestCase(TestCase):
    def test_ff(self):
        self.assertEqual(dot(f, f), Rational(2, 3))

    def test_gg(self):
        self.assertEqual(dot(g, g), Rational(2, 5))

    def test_fg(self):
        self.assertEqual(dot(f, g), 0)


p = plot(f, g,
         (x, -2, 2),
         ylim=(-2, 2),
         legend=False,
         show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
    o.line_color = color

p.show()
p.save(f'sample7.png')

if __name__ == '__main__':
    main()

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

% ./sample7.py 
7.
...
----------------------------------------------------------------------
Ran 3 tests in 0.060s

OK
kamimura@iMac dir3 % ./sample7.py -v
7.
test_ff (__main__.MyTestCase) ... ok
test_fg (__main__.MyTestCase) ... ok
test_gg (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.060s

OK
%

0 コメント:

コメントを投稿