## 2020年6月9日火曜日

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

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

ここから
mod 3

$\left[\begin{array}{cc}0& 1\\ 2& 0\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\\ 0\end{array}\right]\to \left[\begin{array}{c}0\\ 1\end{array}\right]\to \left[\begin{array}{c}1\\ 0\end{array}\right]$

この逆回りのループをつくる行列。

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

別の行列。

$\left[\begin{array}{cc}0& 1\\ 2& 1\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\\ 2\end{array}\right]\to \left[\begin{array}{c}2\\ 0\end{array}\right]\to \left[\begin{array}{c}0\\ 1\end{array}\right]\to \left[\begin{array}{c}1\\ 1\end{array}\right]\to \left[\begin{array}{c}1\\ 0\end{array}\right]$

この逆回りのループをつくる行列。

$\left[\begin{array}{cc}1& 2\\ 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}0\\ 1\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}0\\ 2\end{array}\right]\to \left[\begin{array}{c}1\\ 0\end{array}\right]$

ここから
mod 4

$\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\\ 2\end{array}\right]\to \left[\begin{array}{c}2\\ 2\end{array}\right]$

別の行列。

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

別の行列。

$\left[\begin{array}{cc}1& 2\\ 1& 1\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}3\\ 2\end{array}\right]\to \left[\begin{array}{c}3\\ 1\end{array}\right]\to \left[\begin{array}{c}1\\ 0\end{array}\right]$

これと逆回りのループをつくる行列。

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

2. mod 6

$\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\\ 0\end{array}\right]\to \left[\begin{array}{c}0\\ 4\end{array}\right]\to \left[\begin{array}{c}4\\ 0\end{array}\right]\to \left[\begin{array}{c}0\\ 2\end{array}\right]$

別の行列。

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

別の行列。

$\begin{array}{l}\left[\begin{array}{cc}1& 2\\ 1& 1\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}3\\ 2\end{array}\right]\to \left[\begin{array}{c}1\\ 5\end{array}\right]\to \left[\begin{array}{c}5\\ 0\end{array}\right]\to \left[\begin{array}{c}5\\ 5\end{array}\right]\to \left[\begin{array}{c}3\\ 4\end{array}\right]\to \left[\begin{array}{c}4\\ 1\end{array}\right]\to \left[\begin{array}{c}0\\ 5\end{array}\right]\to \left[\begin{array}{c}2\\ 5\end{array}\right]\\ \to \left[\begin{array}{c}0\\ 1\end{array}\right]\to \left[\begin{array}{c}2\\ 1\end{array}\right]\to \left[\begin{array}{c}4\\ 3\end{array}\right]\to \left[\begin{array}{c}4\\ 1\end{array}\right]\end{array}$

別の行列。

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

これと逆回りのループをつくる行列。

$\left[\begin{array}{cc}4& 1\\ 1& 0\end{array}\right]\left[\begin{array}{c}1\\ 0\end{array}\right]=\left[\begin{array}{c}4\\ 1\end{array}\right]\to \left[\begin{array}{c}5\\ 4\end{array}\right]\to \left[\begin{array}{c}0\\ 5\end{array}\right]\to \left[\begin{array}{c}5\\ 0\end{array}\right]\to \left[\begin{array}{c}2\\ 5\end{array}\right]\to \left[\begin{array}{c}1\\ 2\end{array}\right]\to \left[\begin{array}{c}0\\ 1\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 TestMod6Loop(TestCase):
def test1(self):
m = Matrix([[0, 1],
[1, 2]])
u = Matrix([[1], [0]])
vs1.append(u)
v = (m * u) % 6
vs1.append(v)
i = 1
while u != v:
v = (m * v) % 6
vs1.append(v)
i += 1
self.assertEqual(i, 8)

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

if __name__ == "__main__":
main()


% ./sample2.py -v
test1 (__main__.TestMod6Loop) ... ok
test2 (__main__.TestMod6Loop) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.031s

OK
%