学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第4章(1次方程式, 2次方程式 )、練習問題2の解答を求めてみる。
コード
#!/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 コメント:
コメントを投稿