2020年2月12日水曜日

学習環境

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



    • ABT=[-12-23]T=[-1-223]BTAT=[-1110][2311]=[-1-223]

      よって、

      ABT=BTAT

    • [13111]T=[11131][12310-1][2311-12]=[11131]

    • [137102-5]T=[130721-5][1231110-15][23401-1]=[130721-5]

    • ABCT=CTABT=CTBTAT

コード

#!/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 コメント:

コメントを投稿