学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第4章(1次方程式, 2次方程式 )、練習問題8、9、10の解答を求めてみる。
よって異符号の2つの実数解をもつ。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, plot, solve, Rational
print('8, 9, 10.')
x = symbols('x', real=True)
class MyTestCase(TestCase):
def test_8_1(self):
a = symbols('a', positive=True)
c = symbols('c', negative=True)
b = symbols('b', real=True)
eq = a * x ** 2 + b * x + c
xs = solve(eq, x)
self.assertEqual(len(xs), 2)
def test_8_2(self):
a = symbols('a', negative=True)
c = symbols('c', positive=True)
b = symbols('b', real=True)
eq = a * x ** 2 + b * x + c
xs = solve(eq, x)
self.assertEqual(len(xs), 2)
def test_9(self):
for a in [-2, 6]:
eq = x ** 2 - a * x + (a + 3)
xs = solve(eq, x)
self.assertEqual(len(xs), 1)
def test_10_1(self):
for k in [-3, 1]:
eq = x ** 2 + (k - 3) * x - (2 * k - 3)
xs = solve(eq, x)
self.assertEqual(len(xs), 1)
def test_10_2(self):
k = Rational(4, 3)
eq = k * x ** 2 + k * x + k - 1
xs = solve(eq, x)
self.assertEqual(len(xs), 1)
p = plot(*[x ** 2 - a * x + (a + 3) for a in [-2, 6]],
*[x ** 2 + (k - 3) * x for k in [-3, 1]],
*[2 * k - 3 for k in [-3, 1]],
Rational(4, 3) * x ** 2 + Rational(4, 3) * x + Rational(4, 3) - 1,
(x, -10, 10),
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'sample8.png')
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample8.py -v
8, 9, 10.
test_10_1 (__main__.MyTestCase) ... ok
test_10_2 (__main__.MyTestCase) ... ok
test_8_1 (__main__.MyTestCase) ... ok
test_8_2 (__main__.MyTestCase) ... ok
test_9 (__main__.MyTestCase) ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.558s
OK
%
0 コメント:
コメントを投稿