2020年5月14日木曜日

学習環境

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



    1. [cosπ2sinπ2-sinπ2cosπ2]=[01-10]

    2. [1212-1212]

    3. [-100-1]

    4. [-100-1]

    5. [12-323212]

    6. [3212-1232]

    7. [-12-1212-12]

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, cos, sin, pi, sqrt, Rational

print('2.')


def f(theta):
    return Matrix([[cos(theta), sin(theta)],
                   [-sin(theta), cos(theta)]])


thetas = [pi / 2, pi / 4, pi, -pi, -pi / 3, pi / 6, 5 * pi / 4]
Xs = [[[0, 1],
       [-1, 0]],
      [[1 / sqrt(2), 1 / sqrt(2)],
       [-1 / sqrt(2), 1 / sqrt(2)]],
      [[-1, 0],
       [0, -1]],
      [[-1, 0],
       [0, -1]],
      [[Rational(1, 2), -sqrt(3) / 2],
       [sqrt(3) / 2, Rational(1, 2)]],
      [[sqrt(3) / 2, Rational(1, 2)],
       [-Rational(1, 2), sqrt(3) / 2]],
      [[-1 / sqrt(2), -1 / sqrt(2)],
       [1 / sqrt(2), -1 / sqrt(2)]]]


class TestRotate(TestCase):
    def test(self):
        for i, (theta, X) in enumerate(zip(thetas, Xs)):
            print(f'({chr(ord("a") + i)})')
            self.assertEqual(f(theta), Matrix(X))


if __name__ == "__main__":
    main()

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

% ./sample2.py -v
2.
test (__main__.TestRotate) ... (a)
(b)
(c)
(d)
(e)
(f)
(g)
ok

----------------------------------------------------------------------
Ran 1 test in 0.054s

OK
%

0 コメント:

コメントを投稿