2020年3月6日金曜日

数学 - Python - 代数学 - 1次方程式, 2次方程式 - 解の係数の関係、2次式の因数分解 - ある2数を解とする2次方程式

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

2. $\begin{array}{l}\left(x+5+\sqrt{3}\right)\left(x+5-\sqrt{3}\right)=0\\ {x}^{2}+10x+22=0\end{array}$

3. $\begin{array}{l}\left(x-\frac{4+3i}{2}\right)\left(x-\frac{4-3i}{2}\right)=0\\ {x}^{2}-4x+\frac{16+9}{4}=0\\ 4{x}^{2}-16x+25=0\end{array}$

コード

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

print('32.')

x = symbols('x')
abcs = [(6, -5, 1),
(1, 10, 22),
(4, -16, 25)]
fs = [a * x ** 2 + b * x + c for a, b, c in abcs]

class MyTestCase(TestCase):
def test(self):
xss = [{Rational(1, 2), Rational(1, 3)},
{-5 - sqrt(3), -5 + sqrt(3)},
{(4 + 3 * I) / 2, (4 - 3 * I) / 2}]
for i, (f, xs) in enumerate(zip(fs, xss), 1):
print(f'({i})')
self.assertEqual(solveset(f), xs)

p = plot(*fs,
ylim=(-5, 15),
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

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

if __name__ == "__main__":
main()


% ./sample32.py -v
32.
test (__main__.MyTestCase) ... (1)
(2)
(3)
ok

----------------------------------------------------------------------
Ran 1 test in 0.101s

OK
%