2020年3月9日月曜日

学習環境

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



    1. x = 7 ± 7 - 32 4 = 7 ± 5 i 4

    2. x 2 - 2 x + 1 + x 2 - 16 x + 64 - x 2 = 0 x 2 - 18 x + 65 = 0 x - 13 x - 5 = 0 x = 5 , 13

    3. x - 1 + 1 x - 1 + 2 = 0 x x + 1 = 0 x = - 1 , 0

    4. x = 3 ± 3 - 28 2 7 = 3 ± 5 i 2 7 = 21 14 ± 5 7 14 i

    5. x 2 + 2 m x + m + n m - n = 0 x - m - n x - m + n = 0 x = m - n , m + n

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

コード

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

print('1.')

x, m, n = symbols('x, m, n')
fs = [2 * x ** 2 - sqrt(7) * x + 4,
      (x - 1) ** 2 + (x - 8) ** 2 - x ** 2,
      (x - 1) ** 2 + 3 * (x - 1) + 2,
      sqrt(7) * x ** 2 - sqrt(3) * x + sqrt(7),
      x ** 2 + 2 * m * x + (m ** 2 - n ** 2),
      x ** 2 + 2 * m * x + (m ** 2 + 1)]
sign = [-1, 1]


class MyTestCase(TestCase):
    def test(self):
        xss = [{(sqrt(7) + s * 5 * I) / 4 for s in sign},
               {5, 13},
               {-1, 0},
               {sqrt(21) / 14 + s * 5 * sqrt(7) / 14 * I for s in sign},
               {-m + s * n for s in sign},
               {-m + s * I for s in sign}]
        for i, (f, xs) in enumerate(zip(fs, xss), 1):
            print(f'({i})')
            self.assertEqual(solveset(f, x), xs)


p = plot(*[f.subs({m: 1, n: 2}) for f in 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'sample1.png')

if __name__ == "__main__":
    main()

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

% ./sample1.py 
1.
(1)
(2)
(3)
(4)
(5)
(6)
.
----------------------------------------------------------------------
Ran 1 test in 0.509s

OK
%

0 コメント:

コメントを投稿