2020年5月13日水曜日

学習環境

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



    1. [ 1 0 0 0 0 1 0 0 ]

    2. [ 1 0 0 0 0 1 0 0 0 0 1 0 ] [ 1 0 0 0 0 1 0 0 0 0 0 1 ] [ 1 0 0 0 0 0 1 0 0 0 0 1 ] [ 0 1 0 0 0 0 1 0 0 0 0 1 ]

    3. [ 3 0 0 3 ]

    4. [ 7 0 0 0 7 0 0 0 7 ]

    5. [ - 1 0 0 0 - 1 0 0 0 - 1 ]

    6. [ 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 ]

コード

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

print('1.')

x1, x2, x3, x4 = symbols('x:4', real=True)
X = Matrix([x1, x2, x3, x4]).reshape(4, 1)


class TestKernel(TestCase):
    def test_a(self):
        A = Matrix([[1, 0, 0, 0],
                    [0, 1, 0, 0]])
        self.assertEqual(A * X, Matrix([x1, x2]))

    def test_b(self):
        A = Matrix([[1, 0, 0, 0],
                    [0, 1, 0, 0],
                    [0, 0, 1, 0]])
        self.assertEqual(
            A * Matrix([x1, x2, x3, x4]).reshape(4, 1), Matrix([x1, x2, x3]))

    def test_c(self):
        A = Matrix([[3, 0],
                    [0, 3]])
        self.assertEqual(
            A * Matrix([x1, x2]).reshape(2, 1), Matrix([3 * x1, 3 * x2]))

    def test_f(self):
        A = Matrix([[1, 0, 0, 0],
                    [0, 1, 0, 0],
                    [0, 0, 0, 0],
                    [0, 0, 0, 0]])
        self.assertEqual(
            A * Matrix([x1, x2, x3, x4]).reshape(4, 1), Matrix([x1, x2, 0, 0]))


if __name__ == "__main__":
    main()

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

% ./sample1.py -v 
1.
test_a (__main__.TestKernel) ... ok
test_b (__main__.TestKernel) ... ok
test_c (__main__.TestKernel) ... ok
test_f (__main__.TestKernel) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.002s

OK
%

0 コメント:

コメントを投稿