2020年1月9日木曜日

学習環境

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



    1. A+B2+A-B2=A+B·A+B+A-B·A-B=A2+2A·B+B2+A2-2A·B+B2=2A2+2B2=2A2+2B2

    2. A+B2=A+B·A+B=A2+2A·B+B2=A2+B2+2A·B

    3. A+B2-A-B2=A2+2A·B+β2-A2-2A·B+B2=4A·B

コード

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

print('8.')

a = Matrix([1, 2, 3])
b = Matrix([-4, 5, -6])


class DistanceTestCase(TestCase):
    def test_a(self):
        self.assertEqual((a + b).norm() ** 2 + (a - b).norm()
                         ** 2, 2 * a.norm() ** 2 + 2 * b.norm() ** 2)

    def test_b(self):
        self.assertEqual((a + b).norm() ** 2, a.norm() **
                         2 + b.norm() ** 2 + 2 * a.dot(b))

    def test_c(self):
        self.assertEqual((a + b).norm() ** 2 -
                         (a - b).norm() ** 2, 4 * a.dot(b))


if __name__ == '__main__':
    main()

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

% ./sample8.py -v
8.
test_a (__main__.DistanceTestCase) ... ok
test_b (__main__.DistanceTestCase) ... ok
test_c (__main__.DistanceTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.045s

OK
%

0 コメント:

コメントを投稿