## 2018年1月17日水曜日

### 数学 - Python - 線型代数 - 行列式 - 行列式の計算(連立一次方程式の解法)

1. $\begin{array}{}\mathrm{det}\left(\begin{array}{ccc}3& 1& 1\\ 4& 3& -1\\ 5& 4& 1\end{array}\right)\end{array}=\mathrm{det}\left(\begin{array}{ccc}0& 0& 1\\ 7& 4& -1\\ 2& 3& 1\end{array}\right)\\ =21-8\\ =13$
$\begin{array}{}x\\ =\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}-5& 1& 1\\ -2& 3& -1\\ 6& 4& 1\end{array}\right)\end{array}=\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}0& 0& 1\\ -7& 4& -1\\ 11& 3& 1\end{array}\right)\\ =\frac{1}{13}\left(-21-44\right)\\ =-\frac{65}{13}\\ =-5$
$\begin{array}{}y\\ =\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}3& -5& 1\\ 4& -2& -1\\ 5& 6& 1\end{array}\right)\end{array}=\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}0& 0& 1\\ 7& -7& -1\\ 2& 11& 1\end{array}\right)\\ =\frac{1}{13}\left(77+14\right)\\ =\frac{91}{13}\\ =7$
$\begin{array}{}z\\ =\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}3& 1& -5\\ 4& 3& -2\\ 5& 4& 6\end{array}\right)\end{array}=\frac{1}{13}\mathrm{det}\left(\begin{array}{ccc}0& 1& 0\\ -5& 3& 13\\ -7& 4& 26\end{array}\right)\\ =\frac{1}{13}\left(130-91\right)\\ =10-7\\ =3$

2. $\begin{array}{}\mathrm{det}\left(\begin{array}{cccc}1& 2& -1& -1\\ 1& -1& 1& 1\\ 1& -1& -1& 0\\ 1& 0& 0& 1\end{array}\right)\end{array}=\mathrm{det}\left(\begin{array}{cccc}2& 2& -1& 0\\ 0& -1& 1& 0\\ 1& -1& -1& 0\\ 0& 0& 0& 1\end{array}\right)\\ =\left(2+2\right)-\left(-2+1\right)\\ =5$
$\begin{array}{}x\\ =\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}3& 2& -1& -1\\ 3& -1& 1& 1\\ 4& -1& -1& 0\\ 4& 0& 0& 1\end{array}\right)\end{array}=\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}6& 1& 0& 0\\ 3& -1& 1& 1\\ 4& -1& -1& 0\\ 4& 0& 0& 1\end{array}\right)\\ =\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}6& 1& 0& 0\\ -1& -1& 1& 0\\ 4& -1& -1& 0\\ 4& 0& 0& 1\end{array}\right)\\ =\frac{1}{5}\left(\left(6+4\right)-\left(-6+1\right)\right)\\ =3$
$\begin{array}{}y\\ =\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}1& 3& -1& -1\\ 1& 3& 1& 1\\ 1& 4& -1& 0\\ 1& 4& 0& 1\end{array}\right)\end{array}=\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}2& 7& -1& 0\\ 0& -1& 1& 0\\ 1& 4& -1& 0\\ 1& 4& 0& 1\end{array}\right)\\ =\frac{1}{5}\left(\left(2+7\right)-\left(8+1\right)\right)\\ =0$
$\begin{array}{}z\\ =\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}1& 2& 3& -1\\ 1& -1& 3& 1\\ 1& -1& 4& 0\\ 1& 0& 4& 1\end{array}\right)\end{array}=\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}2& 2& 7& 0\\ 0& -1& -1& 0\\ 1& -1& 4& 0\\ 1& 0& 4& 1\end{array}\right)\\ =\frac{1}{5}\left(\left(-8-2\right)-\left(2-7\right)\right)\\ =-1$
$\begin{array}{}w\\ =\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}1& 2& -1& 3\\ 1& -1& 1& 3\\ 1& -1& -1& 4\\ 1& 0& 0& 4\end{array}\right)\end{array}=\frac{1}{5}\mathrm{det}\left(\begin{array}{cccc}1& 2& 1& -1\\ 1& -1& 0& -1\\ 1& -1& -2& 0\\ 1& 0& 0& 0\end{array}\right)\\ =-\frac{1}{5}\left(\left(1-2\right)-4\right)\\ =1$

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, Matrix, solve

x, y, z, w = symbols('x, y, z, w')
exprs = [
(
3 * x + y + z + 5,
4 * x + 3 * y - z + 2,
5 * x + 4 * y + z - 6
),
(
x + 2 * y - z - w - 3,
x - y + z + w - 3,
x - y - z - 4,
x + w - 4
)
]

for i, expr in enumerate(exprs):
print(f'({chr(ord("a") + i)})')
for t in [expr, solve(expr)]:
pprint(t)
print()
print()

$./sample6.py (a) (3⋅x + y + z + 5, 4⋅x + 3⋅y - z + 2, 5⋅x + 4⋅y + z - 6) {x: -5, y: 7, z: 3} (b) (-w + x + 2⋅y - z - 3, w + x - y + z - 3, x - y - z - 4, w + x - 4) {w: 1, x: 3, y: 0, z: -1}$