2020年5月22日金曜日

学習環境

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


  1. [cosθ-sinθsinθcosθ][cosθ'-sinθ'sinθ'cosθ']=[cosθcosθ'-sinθsinθ'-cosθsinθ'-sinθcosθ'sinθcosθ'+cosθsinθ'-sinθsinθ+cosθcosθ']=[cosθ+θ'-sinθ+θ'sinθ+θ'cosθ+θ']

    よって、

    FθFθ'=Fθ+θ'

    逆写像について。

    [cosθ-sinθsinθcosθ][cos-θ-sin-θsin-θcos-θ]=[cosθ-sinθsinθcosθ][cosθsinθ-sinθcosθ]=[1001]

    よって、

    Fθ-1=F-θ

    (証明終)

コード

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

print('1.')

theta = symbols('θ')
f = Matrix([[cos(theta), -sin(theta)],
            [sin(theta), cos(theta)]])


def eq(A, B):
    for a, b in zip(A, B):
        if float(a) != float(b):
            return False
    return True


class TestRotate(TestCase):
    def test_composition(self):
        for _ in range(10):
            a = random.randrange(-10, 11)
            b = random.randrange(-10, 10)
            self.assertTrue(eq(f.subs({theta: a}) * f.subs({theta: b}),
                               f.subs({theta: a + b})))

    def test_inverse(self):
        for _ in range(10):
            theta0 = random.randrange(-10, 11)
            self.assertTrue(eq(f.inv().subs({theta: theta0}),
                               f.subs({theta: -theta0})))


if __name__ == "__main__":
    main()

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

% ./sample1.py -v
1.
test_composition (__main__.TestRotate) ... ok
test_inverse (__main__.TestRotate) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.518s

OK
%

0 コメント:

コメントを投稿