## 2020年1月31日金曜日

### 数学 - Python - 線形代数学 - 行列 - 1次方程式 - 複素数における連立一次方程式の解

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

1. $\begin{array}{l}-x-2iy=i\\ x+iy=2\\ -iy=2+i\\ y=-1+2i\\ x\\ =2-iy\\ =2-i\left(-1+2i\right)\\ =4+i\end{array}$

2. $\begin{array}{l}\left\{\begin{array}{l}2x+iy-\left(1+i\right)z=1\\ 2x-4y+2iz=0\\ 2x+2iy-\left(2+4i\right)z=2i\end{array}\\ \left(i+4\right)y+\left(-1-3i\right)z=1\\ iy+\left(-1-3i\right)z=2i-1\\ 4y=2-2i\\ y=\frac{1-i}{2}\\ i·\frac{1-i}{2}+\left(-1-3i\right)z=2i-1\\ \left(-1-3i\right)z=2i-1-\frac{i+1}{2}\\ \left(-1-3i\right)z=\frac{-3+3i}{2}\\ z=\frac{3\left(1-i\right)}{2\left(1+3i\right)}\\ =\frac{3\left(1-i\right)\left(1-3i\right)}{2\left(1+9\right)}\\ =\frac{3\left(-2-4i\right)}{20}\\ =\frac{-3-6i}{10}\\ x\\ =2y-iz\\ =1-i+\frac{3i-6}{10}\\ =\frac{4-7i}{10}\end{array}$

3. $\begin{array}{l}\left(1+2i\right)x=3-i\\ x=\frac{3-i}{1+2i}\\ =\frac{\left(3-i\right)\left(1-2i\right)}{5}\\ =\frac{1-7i}{5}\\ y=\left(1+i\right)\frac{1-7i}{5}\\ =\frac{8-6i}{5}\end{array}$

4. $\begin{array}{l}ix-\left(2+i\right)y=1\\ ix+\left(1+2i\right)y=-1+i\\ \left(-3-3i\right)y=2-i\\ y=\frac{2-i}{-3\left(1+i\right)}\\ =\frac{\left(2-i\right)\left(1-i\right)}{-3·2}..\\ =\frac{1-3i}{-3·2}\\ =\frac{-1+3i}{6}\\ x\\ =1+i-\left(2-i\right)·\frac{-1+3i}{6}\\ =1+i-\frac{1+7i}{6}\\ =\frac{5-i}{6}\end{array}$

コード

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

print('4.')

x, y, z = symbols('x, y, z', imag=True)

class MyTestCase(TestCase):
def test_a(self):
s = solve((I * x - 2 * y - 1, x + I * y - 2))
self.assertEqual(s[x].expand(), 4 + I)
self.assertEqual(s[y].expand(), -1 + 2 * I)

def test_b(self):
self.assertEqual(solve((2 * x + I * y - (1 + I) * z - 1,
x - 2 * y + I * z,
-I * x + y - (2 - I) * z - 1)),
{x: (4 - 7 * I) / 10, y: (1 - I) / 2, z: (-3 - 6 * I) / 10})

def test_c(self):
s = solve(((1 + I) * x - y,
I * x + y - (3 - I)))
self.assertEqual(s[x].expand(), ((1 - 7 * I) / 5).expand())
self.assertEqual(s[y].expand(), ((8 - 6 * I) / 5).expand())

def test_d(self):
s = solve((I * x - (2 + I) * y - 1,
x + (2 - I) * y - (1 + I)))
self.assertEqual(s[x].expand(), ((5 - I) / 6).expand())
self.assertEqual(s[y].expand(), ((-1 + 3 * I) / 6).expand())

if __name__ == '__main__':
main()


% ./sample4.py -v
4.
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.729s

OK
%