2020年2月18日火曜日

学習環境

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



    1. x 2 = - 8 x = ± - 8 = ± 2 2 i

    2. x 2 = - 9 16 x = ± 3 4 i

    3. x 2 = - 27 4 x = ± 3 3 2 i

    4. x = - 1 ± 1 - 4 2 = - 1 ± 3 i 2

    5. x = - 3 ± 9 - 32 8 = - 3 ± 23 i 8

    6. x = 1 ± 1 - 5 = 1 ± 2 i

    7. x = - 4 ± 16 - 18 2 = - 4 ± 2 i 2

    8. x = 6 ± 36 - 63 9 = 6 ± 27 i 9 = 2 ± 3 i 3

コード

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

print('18.')

x = symbols('x', imag=True)
fs = [x ** 2 + 8,
      16 * x ** 2 + 9,
      4 * x ** 2 + 27,
      x ** 2 + x + 1,
      4 * x ** 2 + 3 * x + 2,
      x ** 2 - 2 * x + 5,
      2 * x ** 2 + 8 * x + 9,
      9 * x ** 2 - 12 * x + 7,
      ]

sign = [-1, 1]


class MyTestCase(TestCase):
    def test(self):
        zss = [{a + s * b * I for s in sign}
               for a, b in [(0, 2 * sqrt(2)),
                            (0, Rational(3, 4)),
                            (0, 3 * sqrt(3) / 2),
                            (-Rational(1, 2), sqrt(3) / 2),
                            (-Rational(3, 8), sqrt(23) / 8),
                            (1, 2),
                            (-2, sqrt(2) / 2),
                            (Rational(2, 3), sqrt(3) / 3)]]
        for i, (f, zs) in enumerate(zip(fs, zss), 1):
            print(f'({i})')
            self.assertEqual(solveset(f), zs)


p = plot(*fs,
         ylim=(0, 20),
         legend=False,
         show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
    o.line_color = color

for o in zip(fs, colors):
    pprint(o)

p.show()
p.save(f'sample18.png')


if __name__ == "__main__":
    main()

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

% ./sample18.py -v
18.
⎛ 2         ⎞
⎝x  + 8, red⎠
⎛    2           ⎞
⎝16⋅x  + 9, green⎠
⎛   2           ⎞
⎝4⋅x  + 27, blue⎠
⎛ 2               ⎞
⎝x  + x + 1, brown⎠
⎛   2                  ⎞
⎝4⋅x  + 3⋅x + 2, orange⎠
⎛ 2                  ⎞
⎝x  - 2⋅x + 5, purple⎠
⎛   2                ⎞
⎝2⋅x  + 8⋅x + 9, pink⎠
⎛   2                 ⎞
⎝9⋅x  - 12⋅x + 7, gray⎠
test (__main__.MyTestCase) ... (1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
ok

----------------------------------------------------------------------
Ran 1 test in 0.228s

OK
%

0 コメント:

コメントを投稿