2020年7月3日金曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第7章(不等式)、2(2次不等式)の問9の解答を求めてみる。


  1. ( x - 5 ) ( x + 2 ) 0 x - 2 , 5 x
    x 2 - 2 x - 40 = 0 x = 1 ± 1 + 40 = 1 ± 41 1 - 41 < x < 1 + 41
    1 - 41 < x - 2 , 5 x < 1 + 41
    6 < 41 < 7

    よって、 問題の2つの 不等式を満たす整数値は

    - 5 , - 4 , - 3 , - 2 , 5 , 6 , 7

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, S, symbols, plot
from sympy.solvers.inequalities import reduce_inequalities

print('9.')

x = symbols('x', integer=True)
f = x ** 2 - 3 * x - 10
g = x ** 2 - 2 * x - 40


class Test(TestCase):
    def test(self):
        blns = [False] + [True] * 4 + [False] * 6 + [True] * 3 + [False]
        for n, bln in zip(list(range(-6, 8)), blns):
            print(f'x = {n}')
            d = {x: n}
            self.assertEqual(f.subs(d) >= 0 and g.subs(d) < 0, bln)


pprint(reduce_inequalities([f >= 0, g < 0], x))

p = plot(f, g,
         (x, -10, 10),
         legend=True,
         show=False)

colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
    o.line_color = color
p.show()
p.save('sample9.png')

if __name__ == "__main__":
    main()

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

 ./sample9.py -v
9.
(5 ≤ x ∧ x < 1 + √41) ∨ (x ≤ -2 ∧ 1 - √41 < x)
test (__main__.Test) ... x = -6
x = -5
x = -4
x = -3
x = -2
x = -1
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
ok

----------------------------------------------------------------------
Ran 1 test in 0.010s

OK
%

0 コメント:

コメントを投稿