## 2020年5月22日金曜日

### 数学 - Python - 線形代数学 - 線形写像と行列 - 線形写像の合成 - 回転、行列の積、三角関数(正弦と余弦)、逆写像、加法定理、単位行列

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

1. $\begin{array}{l}\left[\begin{array}{cc}\mathrm{cos}\theta & -\mathrm{sin}\theta \\ \mathrm{sin}\theta & \mathrm{cos}\theta \end{array}\right]\left[\begin{array}{cc}\mathrm{cos}\theta \text{'}& -\mathrm{sin}\theta \text{'}\\ \mathrm{sin}\theta \text{'}& \mathrm{cos}\theta \text{'}\end{array}\right]\\ =\left[\begin{array}{cc}\mathrm{cos}\theta \mathrm{cos}\theta \text{'}-\mathrm{sin}\theta \mathrm{sin}\theta \text{'}& -\mathrm{cos}\theta \mathrm{sin}\theta \text{'}-\mathrm{sin}\theta \mathrm{cos}\theta \text{'}\\ \mathrm{sin}\theta \mathrm{cos}\theta \text{'}+\mathrm{cos}\theta \mathrm{sin}\theta \text{'}& -\mathrm{sin}\theta \mathrm{sin}\theta +\mathrm{cos}\theta \mathrm{cos}\theta \text{'}\end{array}\right]\\ =\left[\begin{array}{cc}\mathrm{cos}\left(\theta +\theta \text{'}\right)& -\mathrm{sin}\left(\theta +\theta \text{'}\right)\\ \mathrm{sin}\left(\theta +\theta \text{'}\right)& \mathrm{cos}\left(\theta +\theta \text{'}\right)\end{array}\right]\end{array}$

よって、

${F}_{\theta }{F}_{\theta \text{'}}={F}_{\theta +\theta \text{'}}$

逆写像について。

$\begin{array}{l}\left[\begin{array}{cc}\mathrm{cos}\theta & -\mathrm{sin}\theta \\ \mathrm{sin}\theta & \mathrm{cos}\theta \end{array}\right]\left[\begin{array}{cc}\mathrm{cos}\left(-\theta \right)& -\mathrm{sin}\left(-\theta \right)\\ \mathrm{sin}\left(-\theta \right)& \mathrm{cos}\left(-\theta \right)\end{array}\right]\\ =\left[\begin{array}{cc}\mathrm{cos}\theta & -\mathrm{sin}\theta \\ \mathrm{sin}\theta & \mathrm{cos}\theta \end{array}\right]\left[\begin{array}{cc}\mathrm{cos}\theta & \mathrm{sin}\theta \\ -\mathrm{sin}\theta & \mathrm{cos}\theta \end{array}\right]\\ =\left[\begin{array}{cc}1& 0\\ 0& 1\end{array}\right]\end{array}$

よって、

${F}_{\theta }^{-1}={F}_{-\theta }$

（証明終）

コード

#!/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()


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

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

OK
%