2020年2月12日水曜日

学習環境

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



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

      よって、

      A B T = B T A T

    • [ 1 3 11 1 ] T = [ 1 11 3 1 ] [ 1 2 3 1 0 - 1 ] [ 2 3 1 1 - 1 2 ] = [ 1 11 3 1 ]

    • [ 13 7 1 0 2 - 5 ] T = [ 13 0 7 2 1 - 5 ] [ 1 2 3 1 1 1 0 - 1 5 ] [ 2 3 4 0 1 - 1 ] = [ 13 0 7 2 1 - 5 ]

    • A B C T = C T A B T = C T B T A T

コード

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

print('9.')


class MyTestCase(TestCase):
    def test_a(self):
        a = Matrix([[2, 1],
                    [3, 1]])
        b = Matrix([[-1, 1],
                    [1, 0]])
        self.assertEqual((a * b).T, b.T * a.T)

    def test_b(self):
        a = Matrix([[2, 1, -1],
                    [3, 1, 2]])
        b = Matrix([[1, 1],
                    [2, 0],
                    [3, -1]])
        self.assertEqual((a * b).T, b.T * a.T)

    def test_c(self):
        a = Matrix([[2, 4, 1],
                    [3, 0, -1]])
        b = Matrix([[1, 1, 0],
                    [2, 1, -1],
                    [3, 1, 5]])
        self.assertEqual((a * b).T, b.T * a.T)

    def test_d(self):
        for _ in range(10):
            m = random.randrange(1, 10)
            n = random.randrange(1, 10)
            l = random.randrange(1, 10)
            k = random.randrange(1, 10)
            a = MatrixSymbol('A', m, n)
            b = MatrixSymbol('B', n, l)
            c = MatrixSymbol('C', l, k)
            self.assertEqual((a * b * c).T, c.T * b.T * a.T)


if __name__ == '__main__':
    main()

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

% ./sample9.py -v
9.
test_a (__main__.MyTestCase) ... ok
test_b (__main__.MyTestCase) ... ok
test_c (__main__.MyTestCase) ... ok
test_d (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.060s

OK
%

0 コメント:

コメントを投稿