2020年2月6日木曜日

学習環境

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



    1. A B C = [ - 1 2 - 2 3 ] [ 1 4 2 3 ] = [ 3 2 4 1 ] A B C = [ 2 1 3 1 ] [ 1 - 1 1 4 ] = [ 3 2 4 1 ]

    2. A B C = [ 1 3 11 1 ] [ 1 3 ] = [ 10 14 ] A B C = [ 2 1 - 1 3 1 2 ] [ 4 2 0 ] = [ 10 14 ]

    3. A B C = [ 13 7 1 0 2 - 5 ] [ 1 2 3 1 - 1 4 ] = [ 33 37 11 - 18 ] A B C = [ 2 4 1 3 0 - 1 ] [ 4 3 6 1 1 27 ] = [ 33 37 11 - 18 ]

コード

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

print('3.')


class MyTestCase(TestCase):
    def test(self):
        ABCDs = [([[2, 1],
                   [3, 1]],
                  [[-1, 1],
                   [1, 0]],
                  [[1, 4],
                   [2, 3]],
                  [[3, 2],
                   [4, 1]]),
                 ([[2, 1, -1],
                   [3, 1, 2]],
                  [[1, 1],
                   [2, 0],
                   [3, -1]],
                  [[1],
                   [3]],
                  [[10],
                   [14]]),
                 ([[2, 4, 1],
                   [3, 0, -1]],
                  [[1, 1, 0],
                   [2, 1, -1],
                   [3, 1, 5]],
                  [[1, 2],
                   [3, 1],
                   [-1, 4]],
                  [[33, 37],
                   [11, -18]])]
        for A, B, C, D in ABCDs:
            A = Matrix(A)
            B = Matrix(B)
            C = Matrix(C)
            D = Matrix(D)
            self.assertEqual((A * B) * C, A * (B * C), D)


if __name__ == '__main__':
    main()

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

% ./sample3.py -v
3.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.042s

OK
%

0 コメント:

コメントを投稿