2020年2月22日土曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第4章(1次方程式, 2次方程式 )、4(解の公式、解の虚実)、解の虚実、判別式の問19の解答を求めてみる。



    1. D = 2 3 2 - 4 · 9 · 16 = 529 - 576 < 0

      よって、 2つの 虚根をもつ。


    2. D 4 = 1 2 2 - 9 · 16 = 144 - 144 = 0

      重解をもつ。


    3. D = 2 5 2 - 4 · 9 · 16 = 625 - 576 > 0

      異なる2つの実数解をもつ。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, solveset, plot

print('19.')

x = symbols('x', imag=True)
fs = [9 * x ** 2 - 23 * x + 16,
      9 * x ** 2 - 24 * x + 16,
      9 * x ** 2 - 25 * x + 16]


class MyTestCase(TestCase):
    def test1(self):
        f = fs[0]
        s = solveset(f)
        self.assertEqual(len(s), 2)
        for x0 in s:
            _, imag = x0.as_real_imag()
            self.assertNotEqual(imag, 0)

    def test2(self):
        f = fs[1]
        s = solveset(f)
        self.assertEqual(len(s), 1)

    def test3(self):
        f = fs[2]
        s = solveset(f)
        self.assertEqual(len(s), 2)
        for x0 in s:
            _, imag = x0.as_real_imag()
            self.assertEqual(imag, 0)


p = plot(*fs,
         ylim=(-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(f'sample19.png')


if __name__ == "__main__":
    main()

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

% ./sample19.py -v
19.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.095s

OK
%

0 コメント:

コメントを投稿