学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第6章(1次方程式、2次方程式)、練習問題3の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, plot
from sympy.abc import x
print('3.')
abcs = [(1, -1, 1),
(1, 1, 0),
(-1, -3, -1),
(-1, 1, -1)]
class Test(TestCase):
def test(self):
signs = [-1, 1, 1, -1]
for i, (sign, (a, b, c)) in enumerate(zip(signs, abcs), 1):
print(f'({i})')
if sign == -1:
self.assertLess(b ** 2 - 4 * a * c, 0)
else:
self.assertGreater(b ** 2 - 4 * a * c, 0)
ys = [a * x ** 2 + b * x + c
for a, b, c in abcs]
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']
p = plot(*ys,
(x, -5, 5),
ylim=(-5, 5),
legend=True,
show=False)
for o, color in zip(p, colors):
o.line_color = color
p.save('sample3.png')
p.show()
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample3.py -v
3.
test (__main__.Test) ... (1)
(2)
(3)
(4)
ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
%
0 コメント:
コメントを投稿