2020年2月23日日曜日

学習環境

ラング線形代数学(上) (ちくま学現文庫)(S.ラング (著)、芹沢 正三 (翻訳)、筑摩書房)の3章(行列)、2(行列の積)、練習問題20の解答を求めてみる。



    • t r A = 0

    • t r B = 3

    • t r A B = t r [ 4 2 5 - 1 ] = 3

    • t r B A = t r [ 2 4 4 1 ] = 3

コード

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


print('20.')


a = Matrix([[1, 2],
            [3, -1]])
b = Matrix([[2, 0],
            [1, 1]])


class MyTestCase(TestCase):
    def testA(self):
        self.assertEqual(a.trace(), 0)

    def testB(self):
        self.assertEqual(b.trace(), 3)

    def testAB(self):
        self.assertEqual((a * b).trace(), 3)

    def testBA(self):
        self.assertEqual((b * a).trace(), 3)


if __name__ == '__main__':
    main()

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

% ./sample20.py -v
20.
testA (__main__.MyTestCase) ... ok
testAB (__main__.MyTestCase) ... ok
testB (__main__.MyTestCase) ... ok
testBA (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.041s

OK
%

0 コメント:

コメントを投稿