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