2020年4月26日日曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、3(因数定理)、問20の解答を求めてみる。



    1. x-1x2+x-2=x-12x+2

    2. -1-6+5+2=0x+1x2-7x+2

    3. x-1x3-8x+3-27+24+3=0x-1x+3x2-3x+1

    4. 1-8+5+601-8-5+6016-32+10+6=0x-2x3+2x2-4x-327+18-12-30-27+18+12-3=0x-2x+3x2-x-1

    5. 2123+122+12-1=14+14+12-1=02x-1x2+x+1

    6. 16-16-6+6=0x-22x2-3

コード

#!/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 コメント:

コメントを投稿