## 2017年12月4日月曜日

### 数学 - Python - もう１つの数学の基盤 - 行列と行列式 – 行列とその演算 - 連立1次方程式と行列(2×2行列、逆行列、解法)

1. 逆行列。

$\frac{1}{4-6}\left(\begin{array}{cc}4& -2\\ -3& 1\end{array}\right)=\frac{1}{2}\left(\begin{array}{cc}-4& 2\\ 3& -1\end{array}\right)$
$\left(\begin{array}{c}x\\ y\end{array}\right)=\frac{1}{2}\left(\begin{array}{cc}-4& 2\\ 3& -1\end{array}\right)\left(\begin{array}{c}6\\ 8\end{array}\right)=\frac{1}{2}\left(\begin{array}{c}-24+16\\ 18-8\end{array}\right)=\frac{1}{2}\left(\begin{array}{c}-8\\ 10\end{array}\right)=\left(\begin{array}{c}-4\\ 5\end{array}\right)$

よって、

$x=-4,y=5$

2. $\frac{1}{2}\left(\begin{array}{cc}-4& 2\\ 3& -1\end{array}\right)\left(\begin{array}{c}49\\ 101\end{array}\right)=\frac{1}{2}\left(\begin{array}{c}-196+202\\ 147-101\end{array}\right)=\frac{1}{2}\left(\begin{array}{c}6\\ 46\end{array}\right)=\left(\begin{array}{c}3\\ 23\end{array}\right)$

よって、

$x=3,y=23$

コード(Emacs)

Python 3

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

x, y = symbols('x, y')
A = Matrix([[1, 2],
[3, 4]])
X = Matrix([[x],
[y]])
PS = [Matrix([[6],
[8]]),
Matrix([49, 101]).reshape(2, 1)]

for i, P in enumerate(PS, 1):
print(f'({i})')
pprint(solve(A * X - P, (x, y)))
print()


$./sample24.py (1) {x: -4, y: 5} (2) {x: 3, y: 23}$