## 2020年4月21日火曜日

### 数学 - Python - 代数学 - 連立方程式と高次方程式 - 因数定理 - 整式、剰余の定理

• $f\left(x\right)={x}^{3}+2{x}^{2}-3x-10$

とおく。

$\begin{array}{l}f\left(1\right)\\ =1+2-3-10\\ =-10\end{array}$

• $\begin{array}{l}f\left(-1\right)\\ =-1+2+3-10\\ =-6\end{array}$

• $\begin{array}{l}f\left(2\right)\\ =8+8-6-10\\ =0\end{array}$

• $\begin{array}{l}f\left(-3\right)\\ =-27+18+9-10\\ =-10\end{array}$

• $f\left(x\right)=4{x}^{3}-3{x}^{2}+x+1$

とおく。

$\begin{array}{l}f\left(\frac{1}{2}\right)\\ =4·\frac{1}{8}-\frac{3}{4}+\frac{1}{2}+1\\ =\frac{2-3+2+4}{4}\\ =\frac{5}{4}\end{array}$

• $\begin{array}{l}f\left(-\frac{1}{2}\right)\\ =\frac{-2-3-2+4}{4}\\ =-\frac{3}{4}\end{array}$

• $\begin{array}{l}f\left(\frac{3}{2}\right)\\ =4·\frac{27}{8}-3·\frac{9}{4}+\frac{3}{2}+1\\ =\frac{54-27+6+4}{4}\\ =\frac{37}{4}\end{array}$

コード

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

print('15, 16.')

x = symbols('x')
f = x ** 3 + 2 * x ** 2 - 3 * x - 10
g = 4 * x ** 3 - 3 * x ** 2 + x + 1

class TestFactorTheorem(TestCase):
def test_15(self):
xs = [1, -1, 2, -3]
rs = [-10, -6, 0, -10]
for x0, r in zip(xs, rs):
self.assertEqual(f.subs({x: x0}), r)

def test_16(self):
xs = [Rational(1, 2), -Rational(1, 2), Rational(3, 2)]
rs = [Rational(5, 4), -Rational(3, 4), Rational(37, 4)]
for x0, r in zip(xs, rs):
self.assertEqual(g.subs({x: x0}), r)

p = plot(f, g,
ylim=(-10, 10),
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('sample15.png')

if __name__ == "__main__":
main()


% ./sample15.py -v
15, 16.
test_15 (__main__.TestFactorTheorem) ... ok
test_16 (__main__.TestFactorTheorem) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.006s

OK
%