学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、3(因数定理)、問20の解答を求めてみる。
コード
#!/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()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./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
%
0 コメント:
コメントを投稿