## 2020年2月22日土曜日

### 数学 - Python - 代数学 - 1次方程式, 2次方程式 - 解の公式、解の虚実 - 解の虚実、判別式、実数解、虚数解、重根

1. $\begin{array}{l}D=2{3}^{2}-4·9·16\\ =529-576\\ <0\end{array}$

よって、 2つの 虚根をもつ。

2. $\begin{array}{l}\frac{D}{4}=1{2}^{2}-9·16\\ =144-144\\ =0\end{array}$

重解をもつ。

3. $\begin{array}{l}D=2{5}^{2}-4·9·16\\ =625-576\\ >0\end{array}$

異なる2つの実数解をもつ。

コード

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

print('19.')

x = symbols('x', imag=True)
fs = [9 * x ** 2 - 23 * x + 16,
9 * x ** 2 - 24 * x + 16,
9 * x ** 2 - 25 * x + 16]

class MyTestCase(TestCase):
def test1(self):
f = fs[0]
s = solveset(f)
self.assertEqual(len(s), 2)
for x0 in s:
_, imag = x0.as_real_imag()
self.assertNotEqual(imag, 0)

def test2(self):
f = fs[1]
s = solveset(f)
self.assertEqual(len(s), 1)

def test3(self):
f = fs[2]
s = solveset(f)
self.assertEqual(len(s), 2)
for x0 in s:
_, imag = x0.as_real_imag()
self.assertEqual(imag, 0)

p = plot(*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'sample19.png')

if __name__ == "__main__":
main()


% ./sample19.py -v
19.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.095s

OK
%