## 2020年5月14日木曜日

### 数学 - Python - 線形代数学 - 線形写像と行列 - 線形写像に対応する行列 - 実2次元空間、通常の基底、座標系、回転、三角関数(正弦と余弦)

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

1. $\left[\begin{array}{cc}\mathrm{cos}\frac{\pi }{2}& \mathrm{sin}\frac{\pi }{2}\\ -\mathrm{sin}\frac{\pi }{2}& \mathrm{cos}\frac{\pi }{2}\end{array}\right]=\left[\begin{array}{cc}0& 1\\ -1& 0\end{array}\right]$

2. $\left[\begin{array}{cc}\frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\\ -\frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\end{array}\right]$

3. $\left[\begin{array}{cc}-1& 0\\ 0& -1\end{array}\right]$

4. $\left[\begin{array}{cc}-1& 0\\ 0& -1\end{array}\right]$

5. $\left[\begin{array}{cc}\frac{1}{2}& -\frac{\sqrt{3}}{2}\\ \frac{\sqrt{3}}{2}& \frac{1}{2}\end{array}\right]$

6. $\left[\begin{array}{cc}\frac{\sqrt{3}}{2}& \frac{1}{2}\\ -\frac{1}{2}& \frac{\sqrt{3}}{2}\end{array}\right]$

7. $\left[\begin{array}{cc}-\frac{1}{\sqrt{2}}& -\frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}}& -\frac{1}{\sqrt{2}}\end{array}\right]$

コード

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


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

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

OK
%