## 2020年6月2日火曜日

### 数学 - Pytyhon - 線型代数学 - 行列による世界旅行 - ループ、長さ、逆回り(mod 5)

1. $\begin{array}{l}\left[\begin{array}{cc}0& 1\\ 2& 3\end{array}\right]\left[\begin{array}{c}1\\ 0\end{array}\right]=\left[\begin{array}{c}0\\ 2\end{array}\right]\\ \to \left[\begin{array}{c}2\\ 1\end{array}\right]\\ \to \left[\begin{array}{c}1\\ 2\end{array}\right]\\ \to \left[\begin{array}{c}2\\ 3\end{array}\right]\\ \to \left[\begin{array}{c}3\\ 3\end{array}\right]\\ \to \left[\begin{array}{c}3\\ 0\end{array}\right]\\ \to \left[\begin{array}{c}0\\ 1\end{array}\right]\\ \to \left[\begin{array}{c}1\\ 3\end{array}\right]\\ \to \left[\begin{array}{c}3\\ 1\end{array}\right]\\ \to \left[\begin{array}{c}1\\ 4\end{array}\right]\end{array}$
$\begin{array}{l}\to \left[\begin{array}{c}4\\ 4\end{array}\right]\\ \to \left[\begin{array}{c}4\\ 0\end{array}\right]\\ \to \left[\begin{array}{c}0\\ 3\end{array}\right]\\ \to \left[\begin{array}{c}3\\ 4\end{array}\right]\\ \to \left[\begin{array}{c}4\\ 3\end{array}\right]\end{array}$
$\begin{array}{l}\to \left[\begin{array}{c}3\\ 2\end{array}\right]\\ \to \left[\begin{array}{c}2\\ 2\end{array}\right]\\ \to \left[\begin{array}{c}2\\ 0\end{array}\right]\\ \to \left[\begin{array}{c}0\\ 4\end{array}\right]\\ \to \left[\begin{array}{c}4\\ 2\end{array}\right]\end{array}$
$\begin{array}{l}\to \left[\begin{array}{c}2\\ 4\end{array}\right]\\ \to \left[\begin{array}{c}4\\ 1\end{array}\right]\\ \to \left[\begin{array}{c}1\\ 1\end{array}\right]\\ \to \left[\begin{array}{c}1\\ 0\end{array}\right]\end{array}$

よって長さ24のループを作る。

逆回りのループをつくる行列、実際に確認。

$\left[\begin{array}{cc}1& 3\\ 1& 0\end{array}\right]\left[\begin{array}{c}1\\ 0\end{array}\right]=\left[\begin{array}{c}1\\ 1\end{array}\right]\to \left[\begin{array}{c}4\\ 1\end{array}\right]\to \left[\begin{array}{c}2\\ 4\end{array}\right]\to \left[\begin{array}{c}4\\ 2\end{array}\right]\to \left[\begin{array}{c}0\\ 4\end{array}\right]$
$\to \left[\begin{array}{c}2\\ 0\end{array}\right]\to \left[\begin{array}{c}2\\ 2\end{array}\right]\to \left[\begin{array}{c}3\\ 2\end{array}\right]\to \left[\begin{array}{c}4\\ 3\end{array}\right]\to \left[\begin{array}{c}3\\ 4\end{array}\right]$
$\to \left[\begin{array}{c}0\\ 3\end{array}\right]\to \left[\begin{array}{c}4\\ 0\end{array}\right]\to \left[\begin{array}{c}4\\ 4\end{array}\right]\to \left[\begin{array}{c}1\\ 4\end{array}\right]\to \left[\begin{array}{c}3\\ 1\end{array}\right]$
$\to \left[\begin{array}{c}1\\ 3\end{array}\right]\to \left[\begin{array}{c}0\\ 1\end{array}\right]\to \left[\begin{array}{c}3\\ 0\end{array}\right]\to \left[\begin{array}{c}3\\ 3\end{array}\right]\to \left[\begin{array}{c}2\\ 3\end{array}\right]$
$\to \left[\begin{array}{c}1\\ 2\end{array}\right]\to \left[\begin{array}{c}2\\ 1\end{array}\right]\to \left[\begin{array}{c}0\\ 2\end{array}\right]\to \left[\begin{array}{c}1\\ 0\end{array}\right]$

コード

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

vs1 = []
vs2 = []

class TestMod5Loop(TestCase):
def test1(self):
m = Matrix([[0, 1],
[2, 3]])
u = Matrix([[1], [0]])
vs1.append(u)
v = (m * u) % 5
vs1.append(v)
i = 1
while u != v:
v = (m * v) % 5
vs1.append(v)
i += 1
self.assertEqual(i, 24)

def test2(self):
m = Matrix([[1, 3],
[1, 0]])
u = Matrix([[1], [0]])
vs2.append(u)
v = (m * u) % 5
i = 1
vs2.append(v)
while u != v:
v = (m * v) % 5
vs2.append(v)
i += 1
self.assertEqual(i, 24)
self.assertEqual(vs1, list(reversed(vs2)))

if __name__ == "__main__":
main()


% ./sample1.py -v
test1 (__main__.TestMod5Loop) ... ok
test2 (__main__.TestMod5Loop) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.041s

OK
%