2019年12月29日日曜日

学習環境

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



    1. 可換律について。

      - 1 + 1 f x g x dx = - 1 + 1 g x f x dx

    2. 加法の分配律について。

      - 1 + 1 f x g x + h x dx = - 1 + 1 f x g x + f x h x dx = - 1 + 1 f x g x d x + - 1 + 1 f x h x dx

    3. スカラー 倍について。

      - 1 + 1 c f x g x dx = c - 1 + 1 f x g x d x

    4. - 1 + 1 0 · 0 dx = 0 f x 0 - 1 + 1 f x 2 dx > 0

コード

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

print('6.')

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


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


class MyTestCase(TestCase):
    def test_commutative(self):
        self.assertEqual(dot(f, g), dot(g, f))

    def test_distributive(self):
        self.assertEqual(dot(f, g + h), dot(f, g) + dot(f, h))

    def test_distributive_scalar(self):
        self.assertEqual(dot(c * f, g), c * dot(f, g))

    def test_self(self):
        self.assertEqual(dot(0, 0), 0)
        self.assertGreater(dot(f, f), 0)


p = plot(f, g, h,
         (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'sample6.png')

if __name__ == '__main__':
    main()

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

% ./sample6.py -v
6.
test_commutative (__main__.MyTestCase) ... ok
test_distributive (__main__.MyTestCase) ... ok
test_distributive_scalar (__main__.MyTestCase) ... ok
test_self (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.083s

OK
%

0 コメント:

コメントを投稿