2020年6月12日金曜日

学習環境

続 解析入門 (原書第2版) (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第1章(ベクトル)、4(ベクトルのノルム)の練習問題8の解答を求めてみる。



    1. A + B 2 + A - B 2 = ( A + B ) · ( A + B ) + ( A - B ) · ( A - B ) = A · A + A · B + B · A + B · B + A · A - A · B - B · A + B · B = A 2 + B 2

    2. A + B 2 = A 2 + A · B + A · B + B 2 = A 2 + B 2 + 2 A · B

    3. A + B 2 - A - B 2 = ( A 2 + B 2 + 2 A · B ) - ( A 2 + B 2 - 2 A · B ) = 4 A · B

      (証明終)

コード

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

print('8.')


class Test(TestCase):
    def test(self):
        a = Matrix(symbols('a:10', real=True))
        b = Matrix(symbols('b:10', real=True))
        for i, (s, t) in enumerate([
            ((a + b).norm() ** 2 + (a - b).norm() ** 2,
             2 * a.norm() ** 2 + 2 * b.norm() ** 2),
            ((a + b).norm() ** 2,
             a.norm() ** 2 + b.norm() ** 2 + 2 * a.dot(b)),
            ((a + b).norm() ** 2 - (a - b).norm() ** 2,
             4 * a.dot(b))
        ]):
            print(f'({chr(ord("a") + i)})')
            self.assertEqual(s.expand(), t.expand())


if __name__ == "__main__":
    main()

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

% ./sample8.py -v
8.
test (__main__.Test) ... (a)
(b)
(c)
ok

----------------------------------------------------------------------
Ran 1 test in 0.119s

OK
%

0 コメント:

コメントを投稿