2020年4月21日火曜日

学習環境

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



    • f x = x 3 + 2 x 2 - 3 x - 10

      とおく。

      f 1 = 1 + 2 - 3 - 10 = - 10

    • f - 1 = - 1 + 2 + 3 - 10 = - 6

    • f 2 = 8 + 8 - 6 - 10 = 0

    • f - 3 = - 27 + 18 + 9 - 10 = - 10


    • f x = 4 x 3 - 3 x 2 + x + 1

      とおく。

      f 1 2 = 4 · 1 8 - 3 4 + 1 2 + 1 = 2 - 3 + 2 + 4 4 = 5 4

    • f - 1 2 = - 2 - 3 - 2 + 4 4 = - 3 4

    • f 3 2 = 4 · 27 8 - 3 · 9 4 + 3 2 + 1 = 54 - 27 + 6 + 4 4 = 37 4

コード

#!/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()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

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

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

OK
%

0 コメント:

コメントを投稿