## 2020年2月18日火曜日

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

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

2. $\begin{array}{l}{x}^{2}=-\frac{9}{16}\\ x=±\frac{3}{4}i\end{array}$

3. $\begin{array}{l}{x}^{2}=-\frac{27}{4}\\ x=±\frac{3\sqrt{3}}{2}i\end{array}$

4. $\begin{array}{l}x\\ =\frac{-1±\sqrt{1-4}}{2}\\ =\frac{-1±\sqrt{3}i}{2}\end{array}$

5. $\begin{array}{l}x\\ =\frac{-3±\sqrt{9-32}}{8}\\ =\frac{-3±\sqrt{23}i}{8}\end{array}$

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

7. $\begin{array}{l}x\\ =\frac{-4±\sqrt{16-18}}{2}\\ =\frac{-4±\sqrt{2}i}{2}\end{array}$

8. $\begin{array}{l}x\\ =\frac{6±\sqrt{36-63}}{9}\\ =\frac{6±\sqrt{27}i}{9}\\ =\frac{2±\sqrt{3}i}{3}\end{array}$

コード

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

print('18.')

x = symbols('x', imag=True)
fs = [x ** 2 + 8,
16 * x ** 2 + 9,
4 * x ** 2 + 27,
x ** 2 + x + 1,
4 * x ** 2 + 3 * x + 2,
x ** 2 - 2 * x + 5,
2 * x ** 2 + 8 * x + 9,
9 * x ** 2 - 12 * x + 7,
]

sign = [-1, 1]

class MyTestCase(TestCase):
def test(self):
zss = [{a + s * b * I for s in sign}
for a, b in [(0, 2 * sqrt(2)),
(0, Rational(3, 4)),
(0, 3 * sqrt(3) / 2),
(-Rational(1, 2), sqrt(3) / 2),
(-Rational(3, 8), sqrt(23) / 8),
(1, 2),
(-2, sqrt(2) / 2),
(Rational(2, 3), sqrt(3) / 3)]]
for i, (f, zs) in enumerate(zip(fs, zss), 1):
print(f'({i})')
self.assertEqual(solveset(f), zs)

p = plot(*fs,
ylim=(0, 20),
legend=False,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

for o in zip(fs, colors):
pprint(o)

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

if __name__ == "__main__":
main()


% ./sample18.py -v
18.
⎛ 2         ⎞
⎝x  + 8, red⎠
⎛    2           ⎞
⎝16⋅x  + 9, green⎠
⎛   2           ⎞
⎝4⋅x  + 27, blue⎠
⎛ 2               ⎞
⎝x  + x + 1, brown⎠
⎛   2                  ⎞
⎝4⋅x  + 3⋅x + 2, orange⎠
⎛ 2                  ⎞
⎝x  - 2⋅x + 5, purple⎠
⎛   2                ⎞
⎝2⋅x  + 8⋅x + 9, pink⎠
⎛   2                 ⎞
⎝9⋅x  - 12⋅x + 7, gray⎠
test (__main__.MyTestCase) ... (1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
ok

----------------------------------------------------------------------
Ran 1 test in 0.228s

OK
%