## 2020年4月26日日曜日

### 数学 - Python - 代数学 - 連立方程式と高次方程式 - 因数定理 - 因数分解への応用、有理数の範囲、係数、約数

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

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

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

4. $\begin{array}{l}1-8+5+6\ne 0\\ 1-8-5+6\ne 0\\ 16-32+10+6=0\\ \left(x-2\right)\left({x}^{3}+2{x}^{2}-4x-3\right)\\ 27+18-12-3\ne 0\\ -27+18+12-3=0\\ \left(x-2\right)\left(x+3\right)\left({x}^{2}-x-1\right)\end{array}$

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

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

コード

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

print('20.')

x = symbols('x')

fs = [x ** 3 - 3 * x + 2,
x ** 3 - 6 * x ** 2 - 5 * x + 2,
x ** 4 - x ** 3 - 8 * x ** 2 + 11 * x - 3,
x ** 4 - 8 * x ** 2 + 5 * x + 6,
2 * x ** 3 + x ** 2 + x - 1,
2 * x ** 3 - 4 * x ** 2 - 3 * x + 6]

class TestFactorTheorem(TestCase):
def test_1(self):
self.assertEqual(solveset(fs[0], domain=S.Rationals),
FiniteSet(-2, 1))

def test_2(self):
self.assertEqual(solveset(fs[1], domain=S.Rationals),
FiniteSet(-1))

def test_3(self):
self.assertEqual(solveset(fs[2], domain=S.Rationals),
FiniteSet(1, -3))

def test_4(self):
self.assertEqual(solveset(fs[3], domain=S.Rationals),
FiniteSet(2, -3))

def test_5(self):
self.assertEqual(solveset(fs[4], domain=S.Rationals),
FiniteSet(Rational(1, 2)))

def test_6(self):
self.assertEqual(solveset(fs[5], domain=S.Rationals),
FiniteSet(2))

p = plot(*fs, (x, -5, 5), ylim=(-5, 5), legend=True, show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange', 'pink']

for i, s in enumerate(p):
s.line_color = colors[i]

p.show()
p.save('sample20.png')

if __name__ == "__main__":
main()


% ./sample20.py -v
20.
test_1 (__main__.TestFactorTheorem) ... ok
test_2 (__main__.TestFactorTheorem) ... ok
test_3 (__main__.TestFactorTheorem) ... ok
test_4 (__main__.TestFactorTheorem) ... ok
test_5 (__main__.TestFactorTheorem) ... ok
test_6 (__main__.TestFactorTheorem) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.135s

OK
%