## 2020年2月12日水曜日

### 数学 - Python - 線形代数学 - 行列 - 行列の積 - 積の転置行列と転置行列の積

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

• $\begin{array}{l}{\left(AB\right)}^{T}\\ ={\left[\begin{array}{cc}-1& 2\\ -2& 3\end{array}\right]}^{T}\\ =\left[\begin{array}{cc}-1& -2\\ 2& 3\end{array}\right]\\ {B}^{T}{A}^{T}\\ =\left[\begin{array}{cc}-1& 1\\ 1& 0\end{array}\right]\left[\begin{array}{cc}2& 3\\ 1& 1\end{array}\right]\\ =\left[\begin{array}{cc}-1& -2\\ 2& 3\end{array}\right]\end{array}$

よって、

${\left(AB\right)}^{T}={B}^{T}{A}^{T}$

• $\begin{array}{l}{\left[\begin{array}{cc}1& 3\\ 11& 1\end{array}\right]}^{T}\\ =\left[\begin{array}{cc}1& 11\\ 3& 1\end{array}\right]\\ \left[\begin{array}{ccc}1& 2& 3\\ 1& 0& -1\end{array}\right]\left[\begin{array}{cc}2& 3\\ 1& 1\\ -1& 2\end{array}\right]\\ =\left[\begin{array}{cc}1& 11\\ 3& 1\end{array}\right]\end{array}$

• $\begin{array}{l}{\left[\begin{array}{ccc}13& 7& 1\\ 0& 2& -5\end{array}\right]}^{T}\\ =\left[\begin{array}{cc}13& 0\\ 7& 2\\ 1& -5\end{array}\right]\\ \left[\begin{array}{ccc}1& 2& 3\\ 1& 1& 1\\ 0& -1& 5\end{array}\right]\left[\begin{array}{cc}2& 3\\ 4& 0\\ 1& -1\end{array}\right]\\ =\left[\begin{array}{cc}13& 0\\ 7& 2\\ 1& -5\end{array}\right]\end{array}$

• $\begin{array}{l}{\left(ABC\right)}^{T}\\ ={C}^{T}{\left(AB\right)}^{T}\\ ={C}^{T}{B}^{T}{A}^{T}\end{array}$

コード

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

print('9.')

class MyTestCase(TestCase):
def test_a(self):
a = Matrix([[2, 1],
[3, 1]])
b = Matrix([[-1, 1],
[1, 0]])
self.assertEqual((a * b).T, b.T * a.T)

def test_b(self):
a = Matrix([[2, 1, -1],
[3, 1, 2]])
b = Matrix([[1, 1],
[2, 0],
[3, -1]])
self.assertEqual((a * b).T, b.T * a.T)

def test_c(self):
a = Matrix([[2, 4, 1],
[3, 0, -1]])
b = Matrix([[1, 1, 0],
[2, 1, -1],
[3, 1, 5]])
self.assertEqual((a * b).T, b.T * a.T)

def test_d(self):
for _ in range(10):
m = random.randrange(1, 10)
n = random.randrange(1, 10)
l = random.randrange(1, 10)
k = random.randrange(1, 10)
a = MatrixSymbol('A', m, n)
b = MatrixSymbol('B', n, l)
c = MatrixSymbol('C', l, k)
self.assertEqual((a * b * c).T, c.T * b.T * a.T)

if __name__ == '__main__':
main()


% ./sample9.py -v
9.
test_a (__main__.MyTestCase) ... ok
test_b (__main__.MyTestCase) ... ok
test_c (__main__.MyTestCase) ... ok
test_d (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.060s

OK
%