2020年4月21日火曜日

学習環境

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



    • fx=x3+2x2-3x-10

      とおく。

      f1=1+2-3-10=-10

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

    • f2=8+8-6-10=0

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


    • fx=4x3-3x2+x+1

      とおく。

      f12=4·18-34+12+1=2-3+2+44=54

    • f-12=-2-3-2+44=-34

    • f32=4·278-3·94+32+1=54-27+6+44=374

コード

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

コメントを投稿