#!/usr/bin/env python3fromunittestimport TestCase, main
fromsympyimport symbols, Rational, solve
print('17, 18, 19.')
x = symbols('x', real=True)
classTestFactorTheorem(TestCase):
deftest_17(self):
a = x **3-3* x +2
b = x **3+ x +10
c = x **4+3* x **2-4
ts = [(True, False, True),
(False, False, True),
(True, True, False)]
for x0, blns inzip([1, -1, -2], ts):
for p, bln inzip([a, b, c], blns):
self.assertEqual(p.subs({x: x0}) ==0, bln)
deftest_18(self):
a, b = symbols('a, b', real=True)
p = (2* x **2+ x -1) + a * x + b
self.assertEqual(solve([p.subs({x: -1}) -6,
p.subs({x: Rational(1, 2)}) -3]),
{a: -2, b: 4})
deftest_19(self):
p =-Rational(3, 2)
q =-4
expr = x **3+ p * x **2+ q * x +6for x0 in [-2, 2]:
self.assertEqual(expr.subs({x: x0}), 0)
if__name__=="__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample17.py -v
17, 18, 19.
test_17 (__main__.TestFactorTheorem) ... ok
test_18 (__main__.TestFactorTheorem) ... ok
test_19 (__main__.TestFactorTheorem) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.054s
OK
%
0 コメント:
コメントを投稿