## 2020年3月9日月曜日

### 数学 - Python - 代数学 - 1次方程式, 2次方程式 - 解、複素数の範囲、因数分解、解の公式

1. $\begin{array}{l}x=\frac{\sqrt{7}±\sqrt{7-32}}{4}\\ =\frac{\sqrt{7}±5i}{4}\end{array}$

2. $\begin{array}{l}{x}^{2}-2x+1+{x}^{2}-16x+64-{x}^{2}=0\\ {x}^{2}-18x+65=0\\ \left(x-13\right)\left(x-5\right)=0\\ x=5,13\end{array}$

3. $\begin{array}{l}\left(\left(x-1\right)+1\right)\left(\left(x-1\right)+2\right)=0\\ x\left(x+1\right)=0\\ x=-1,0\end{array}$

4. $\begin{array}{l}x=\frac{\sqrt{3}±\sqrt{3-28}}{2\sqrt{7}}\\ =\frac{\sqrt{3}±5i}{2\sqrt{7}}\\ =\frac{\sqrt{21}}{14}±\frac{5\sqrt{7}}{14}i\end{array}$

5. $\begin{array}{l}{x}^{2}+2mx+\left(m+n\right)\left(m-n\right)=0\\ \left(x-m-n\right)\left(x-m+n\right)=0\\ x=m-n,m+n\end{array}$

6. $\begin{array}{l}x=-m±\sqrt{{m}^{2}-{m}^{2}-1}\\ =-m±i\end{array}$

コード

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

print('1.')

x, m, n = symbols('x, m, n')
fs = [2 * x ** 2 - sqrt(7) * x + 4,
(x - 1) ** 2 + (x - 8) ** 2 - x ** 2,
(x - 1) ** 2 + 3 * (x - 1) + 2,
sqrt(7) * x ** 2 - sqrt(3) * x + sqrt(7),
x ** 2 + 2 * m * x + (m ** 2 - n ** 2),
x ** 2 + 2 * m * x + (m ** 2 + 1)]
sign = [-1, 1]

class MyTestCase(TestCase):
def test(self):
xss = [{(sqrt(7) + s * 5 * I) / 4 for s in sign},
{5, 13},
{-1, 0},
{sqrt(21) / 14 + s * 5 * sqrt(7) / 14 * I for s in sign},
{-m + s * n for s in sign},
{-m + s * I for s in sign}]
for i, (f, xs) in enumerate(zip(fs, xss), 1):
print(f'({i})')
self.assertEqual(solveset(f, x), xs)

p = plot(*[f.subs({m: 1, n: 2}) for f in fs],
ylim=(-10, 10),
legend=True,
show=False)

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color

p.show()
p.save(f'sample1.png')

if __name__ == "__main__":
main()


% ./sample1.py
1.
(1)
(2)
(3)
(4)
(5)
(6)
.
----------------------------------------------------------------------
Ran 1 test in 0.509s

OK
%