## 2020年3月5日木曜日

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

• 実数の範囲。

$\begin{array}{l}{x}^{4}+{x}^{2}+1\\ ={\left({x}^{2}+1\right)}^{2}-{x}^{2}\\ =\left({x}^{2}+x+1\right)\left({x}^{2}-x+1\right)\end{array}$

• 複素数の範囲。

$\begin{array}{l}{x}^{2}+x+1=0\\ x=\frac{-1±\sqrt{3}i}{2}\\ {x}^{2}-x+1=0\\ x=\frac{1±\sqrt{3}i}{2}\\ {x}^{4}+{x}^{2}+1\\ =\left(x+\frac{1}{2}-\frac{\sqrt{3}}{2}i\right)\left(x+\frac{1}{2}+\frac{\sqrt{3}}{2}i\right)\left(x-\frac{1}{2}-\frac{\sqrt{3}}{2}i\right)\left(x-\frac{1}{2}+\frac{\sqrt{3}}{2}i\right)\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('31.')

x = symbols('x')
f = x ** 4 + x ** 2 + 1

class MyTestCase(TestCase):
def test_real_number(self):
self.assertEqual(f, ((x ** 2 + x + 1) * (x ** 2 - x + 1)).expand())

def test_imag_number(self):
self.assertEqual(f, ((x + Rational(1, 2) - sqrt(3) / 2 * I)
* (x + Rational(1, 2) + sqrt(3) / 2 * I) * (x - Rational(1, 2) - sqrt(3) / 2 * I) * (x - Rational(1, 2) + sqrt(3) / 2 * I)).expand())

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

p = plot(f,
(x, -5, 5),
ylim=(0, 5),
legend=False,
show=False)
for o, color in zip(p, colors):
o.line_color = color

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

if __name__ == "__main__":
main()


% ./sample31.py -v
31.
test_imag_number (__main__.MyTestCase) ... ok
test_real_number (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.019s

OK
%