## 2020年5月10日日曜日

### 数学 - Python - 代数学 - 連立方程式と高次方程式 - 複素数、等式、実部と虚部

1. $\begin{array}{l}\left\{\begin{array}{l}a+b=4\\ ab=2\end{array}\\ b=4-a\\ a\left(4-a\right)=2\\ {a}^{2}-4a+2=0\\ a=2±\sqrt{2}\\ b=4-\left(2±\sqrt{2}\right)\\ =2\mp \sqrt{2}\end{array}$

（複号同順）

2. $\begin{array}{l}{\left(a+bi\right)}^{2}\\ =\left({a}^{2}-{b}^{2}\right)+2abi\\ \left\{\begin{array}{l}{a}^{2}-{b}^{2}=15\\ 2ab=-8\end{array}\\ b=-\frac{4}{a}\\ {a}^{2}-\frac{16}{{a}^{2}}=15\\ {a}^{4}-15{a}^{2}-16=0\\ {a}^{2}=\frac{15+\sqrt{225+64}}{2}\\ =\frac{15+\sqrt{289}}{2}\\ =\frac{15+17}{2}\\ =16\\ a=±4\\ b=-\frac{4}{±4}=\mp 1\end{array}$

（複号同順）

3. $\begin{array}{l}\left({a}^{2}+2ab+3{b}^{2}\right)+\left(3{a}^{2}-ab-2{b}^{2}\right)i=19\\ \left\{\begin{array}{l}{a}^{2}+2ab+3{b}^{2}=19\\ 3{a}^{2}-ab-2{b}^{2}=0\end{array}\\ \left(3a+2b\right)\left(a-b\right)=0\\ 3a+2b=0\\ b=-\frac{3}{2}a\\ {a}^{2}-3{a}^{2}+\frac{27}{4}{a}^{2}=19\\ {a}^{2}=4\\ a=±2\\ b=\mp 3\\ a-b=0\\ b=a\\ {a}^{2}+2{a}^{2}+3{a}^{2}=19\\ {a}^{2}=\frac{19}{6}\\ a=b=±\sqrt{\frac{19}{6}}\end{array}$

（複号同順）

コード

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

print('6.')

a, b = symbols('a:b', real=True)

class TestImaginaryNumbers(TestCase):
def test1(self):
self.assertEqual(
solve(((a + b) + a * b * I) - (4 + 2 * I)),
[{a: 2-sqrt(2), b: 2+sqrt(2)},
{a: 2+sqrt(2), b: 2-sqrt(2)}]
)

def test2(self):
self.assertEqual(
solve((a + b * I) ** 2 - (15 - 8 * I)),
[{a: -4, b: 1},
{a: 4, b: -1}]
)

def test3(self):
self.assertEqual(
solve(((1 + 3 * I) * a ** 2 + (2 - I) *
a * b + (3 - 2 * I) * b ** 2) - 19),
[{a: -2, b: 3},
{a: 2, b: -3},
{a: -sqrt(Rational(19, 6)), b: -sqrt(Rational(19, 6))},
{a: sqrt(Rational(19, 6)), b: sqrt(Rational(19, 6))}]
)

if __name__ == "__main__":
main()


% ./sample6.py -v
6.
test1 (__main__.TestImaginaryNumbers) ... ok
test2 (__main__.TestImaginaryNumbers) ... ok
test3 (__main__.TestImaginaryNumbers) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.350s

OK
%