2020年2月8日土曜日

学習環境

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



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

    • B A = [ 2 0 1 1 ] [ 1 2 3 - 1 ] = [ 2 4 4 1 ]

      一般に行列の積について可換律は成り立たない。

コード

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

print('5.')

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


class MyTestCase(TestCase):
    def test1(self):
        self.assertEqual(a * b, Matrix([4, 2, 5, -1]).reshape(2, 2))

    def test2(self):
        self.assertEqual(b * a, Matrix([2, 4, 4, 1]).reshape(2, 2))


if __name__ == '__main__':
    main()

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

% ./sample5.py -v
5.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.040s

OK
%

0 コメント:

コメントを投稿