## 2020年5月29日金曜日

### 数学 - Python - 線形代数学 - 行列式 - クラーメルの法則 - 連立1次方程式の解

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

1. $\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}3& 1& -1\\ 1& 1& 1\\ 0& 1& -1\end{array}\right)\\ =3\mathrm{det}\left(\begin{array}{cc}1& 1\\ 1& -1\end{array}\right)-\mathrm{det}\left(\begin{array}{cc}1& -1\\ 1& -1\end{array}\right)\\ =3\left(-1-1\right)-\left(-1+1\right)\\ =-6\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}0& 1& -1\\ 0& 1& 1\\ 1& 1& -1\end{array}\right)\\ =1+1\\ =2\\ x=\frac{2}{-6}=-\frac{1}{3}\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}3& 0& -1\\ 1& 0& 1\\ 0& 1& -1\end{array}\right)\\ =-\left(3+1\right)\\ =-4\\ y=\frac{-4}{-6}=\frac{2}{3}\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}3& 1& 0\\ 1& 1& 0\\ 0& 1& 1\end{array}\right)\\ =3-1\\ =2\\ z=\frac{2}{-6}=-\frac{1}{3}\end{array}$

2. $\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}2& -1& 1\\ 1& 3& -2\\ 4& -3& 1\end{array}\right)\\ =\mathrm{det}\left(\begin{array}{ccc}0& 0& 1\\ 5& 1& 0\\ 2& -2& 0\end{array}\right)\\ =-10-2\\ =-12\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}0& -1& 1\\ 0& 3& -2\\ 2& -3& 1\end{array}\right)\\ =2\left(2-3\right)\\ =-2\\ x=\frac{1}{6}\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}2& 0& 1\\ 1& 0& -2\\ 4& 2& 1\end{array}\right)\\ =-2\left(-4-1\right)\\ =10\\ y=-\frac{10}{12}=-\frac{5}{6}\end{array}$
$\begin{array}{l}\mathrm{det}\left(\begin{array}{ccc}2& -1& 0\\ 1& 3& 0\\ 4& -3& 2\end{array}\right)\\ =2\left(6+1\right)\\ =14\\ z=-\frac{7}{6}\end{array}$

コード

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

print('1.')

class Test(TestCase):
def test_a(self):
x, y, z = -Rational(1, 3), Rational(2, 3), -Rational(1, 3)
self.assertEqual(3 * x + y - z, 0)
self.assertEqual(x + y + z, 0)
self.assertEqual(y - z, 1)

def test_b(self):
x, y, z = Rational(1, 6), -Rational(5, 6), -Rational(7, 6)
self.assertEqual(2 * x - y + z, 0)
self.assertEqual(x + 3 * y - 2 * z, 0)
self.assertEqual(4 * x - 3 * y + z, 2)

if __name__ == "__main__":
main()


% ./sample1.py -v
1.
test_a (__main__.Test) ... ok
test_b (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
%