2020年3月10日火曜日

学習環境

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



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

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

    3. a x - 1 b x - 1 = 0 x = 1 a , 1 b

    4. a - b x - c - a x - 1 = 0 x = 1 , c - a a - b

コード

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

print('2.')

a, b, c, x = symbols('a, b, c, x')
abcs = [(a * (a - 1), -(2 * a ** 2 - 1), a * (a + 1)),
        (2 * a, 1 - a ** 2, -a * (a ** 2 + 1)),
        (a * b, -(a + b), 1),
        (a - b, b - c, c - a)]
fs = [c1 * x ** 2 + c2 * x + c3 for c1, c2, c3 in abcs]


class MyTestCase(TestCase):
    def test(self):
        xss = [{(a + 1) / a, a / (a - 1)},
               {a, -(a ** 2 + 1) / (2 * a)},
               {1 / a, 1 / b},
               {1 + 0 * a, (c - a) / (a - b)}]
        for i, (f, xs) in enumerate(zip(fs, xss), 1):
            print(f'({i})')
            self.assertEqual({x0.simplify()for x0 in solveset(f, x)},
                             {x0.simplify() for x0 in xs})


p = plot(*[f.subs({a: 2, b: -1, c: 3}) for f in fs],
         (x, -5, 5),
         ylim=(-5, 5),
         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'sample2.png')

if __name__ == "__main__":
    main()

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

% ./sample2.py
2.
(1)
(2)
(3)
(4)
.
----------------------------------------------------------------------
Ran 1 test in 0.790s

OK
%

0 コメント:

コメントを投稿