## 2020年2月9日日曜日

### 数学 - Python - 線形代数学 - 行列 - 行列の積 - 可換律な場合、一般法則、スカラー倍、単位行列

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

• $\begin{array}{l}CA\\ =\left[\begin{array}{cc}7& 0\\ 0& 7\end{array}\right]\left[\begin{array}{cc}1& 2\\ 3& -1\end{array}\right]\\ =\left[\begin{array}{cc}7& 14\\ 21& -7\end{array}\right]\\ =7\left[\begin{array}{cc}1& 2\\ 3& -1\end{array}\right]\\ =7A\end{array}$

• $\begin{array}{l}AC\\ =\left[\begin{array}{cc}1& 2\\ 3& -1\end{array}\right]\left[\begin{array}{cc}7& 0\\ 0& 7\end{array}\right]\\ =\left[\begin{array}{cc}7& 14\\ 21& -7\end{array}\right]\\ =7\left[\begin{array}{cc}1& 2\\ 3& -1\end{array}\right]\\ =7A\end{array}$

• $\begin{array}{l}CB\\ =\left[\begin{array}{cc}7& 0\\ 0& 7\end{array}\right]\left[\begin{array}{cc}2& 0\\ 1& 1\end{array}\right]\\ =\left[\begin{array}{cc}14& 0\\ 7& 7\end{array}\right]\\ =7\left[\begin{array}{cc}2& 0\\ 1& 1\end{array}\right]\\ =7B\end{array}$

• $\begin{array}{l}BC\\ =\left[\begin{array}{cc}2& 0\\ 1& 1\end{array}\right]\left[\begin{array}{cc}7& 0\\ 0& 7\end{array}\right]\\ =\left[\begin{array}{cc}14& 0\\ 7& 7\end{array}\right]\\ =7\left[\begin{array}{cc}2& 0\\ 1& 1\end{array}\right]\\ =7B\end{array}$

一般的な法則は、

$\begin{array}{l}a\in K=\text{ℝ}\\ \left[\begin{array}{cc}a& 0\\ 0& a\end{array}\right]A\\ =A\left[\begin{array}{cc}a& 0\\ 0& a\end{array}\right]\\ =aA\end{array}$

コード

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

print('6.')

a = Matrix([[1, 2],
[3, -1]])
b = Matrix([[2, 0],
[1, 1]])
c = Matrix([[7, 0],
[0, 7]])

class MyTestCase(TestCase):
def test_ca(self):
self.assertEqual(c * a, 7 * a)

def test_ac(self):
self.assertEqual(a * c, 7 * a)

def test_cb(self):
self.assertEqual(c * b, 7 * b)

def test_bc(self):
self.assertEqual(b * c, 7 * b)

if __name__ == '__main__':
main()


% ./sample6.py -v
6.
test_ac (__main__.MyTestCase) ... ok
test_bc (__main__.MyTestCase) ... ok
test_ca (__main__.MyTestCase) ... ok
test_cb (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK
%