学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、2(連立2次方程式)、問9の解答を求めてみる。
よって解は、
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, solve, Rational, sqrt
print('9.')
x, y = symbols('x, y')
class TestEquations(TestCase):
def test1(self):
self.assertEqual(solve([x ** 2 + y ** 2 - 13, y - x - 1]),
[{x: -3, y: -2}, {x: 2, y: 3}])
def test2(self):
self.assertEqual(solve([x + y + 5, x * y - 6]),
[{x: -3, y: -2}, {x: -2, y: -3}])
def test3(self):
self.assertEqual(solve([x + y - 5,
x ** 2 + x * y + y ** 2 - 21]),
[{x: 1, y: 4}, {x: 4, y: 1}])
def test4(self):
self.assertEqual(solve([x - y - 8, x * y + 15]),
[{x: 3, y: -5}, {x: 5, y: -3}])
def test5(self):
self.assertEqual(solve([y - 2 * x + 1, y ** 2 - x ** 2 - 5]),
[{x: -Rational(2, 3), y: -Rational(7, 3)},
{x: 2, y: 3}])
def test6(self):
self.assertEqual(solve([3 * x + 4 * y - 5,
4 * x ** 2 + x * y - 3 * y ** 2]),
[{x: -5, y: 5},
{x: Rational(3, 5), y: Rational(4, 5)}])
def test7(self):
self.assertEqual(
sorted(solve([x ** 2 + x * y + 2 * y ** 2 - 56,
x ** 2 - 5 * x * y + 6 * y ** 2]),
key=lambda d: d[x]),
sorted([{x: -2 * sqrt(7), y: -sqrt(7)},
{x: 2 * sqrt(7), y: sqrt(7)},
{x: -6, y: -2},
{x: 6, y: 2}],
key=lambda d: d[x]))
def test8(self):
self.assertEqual(
sorted(solve([x ** 2 - 20 * x * y + 36 * y ** 2,
x ** 2 - 2 * x * y + 6 * y ** 2 - 6]),
key=lambda d: d[x]),
sorted([{x: -2, y: -1},
{x: 2, y: 1},
{x: -Rational(18, 7), y: -Rational(1, 7)},
{x: Rational(18, 7), y: Rational(1, 7)}],
key=lambda d: d[x]))
def test9(self):
self.assertEqual(
sorted(solve([x ** 2 + 3 * x * y - 12,
x * y + 3 * y ** 2 - Rational(4, 3)]),
key=lambda d: d[x]),
sorted([{x: -3, y: -Rational(1, 3)},
{x: 3, y: Rational(1, 3)}],
key=lambda d: d[x])
)
def test10(self):
self.assertEqual(
sorted(solve([5 * x * y + 4 * x - 6 * y - 5,
12 * x * y + 9 * x - 14 * y - 11]),
key=lambda d: d[x]),
sorted([{x: 1, y: -1},
{x: Rational(4, 3), y: -Rational(1, 2)}],
key=lambda d: d[x])
)
def test11(self):
self.assertEqual(
sorted(solve([x ** 2 + y ** 2 - 10 * x * y + 5 * (x + y),
x ** 2 + y ** 2 - 5 * (x * y - 1)]),
key=lambda d: d[x]),
sorted([{x: 1, y: 2},
{x: 1, y: 3},
{x: 2, y: 1},
{x: 3, y: 1}],
key=lambda d: d[x])
)
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample9.py -v
9.
test1 (__main__.TestEquations) ... ok
test10 (__main__.TestEquations) ... ok
test11 (__main__.TestEquations) ... ok
test2 (__main__.TestEquations) ... ok
test3 (__main__.TestEquations) ... ok
test4 (__main__.TestEquations) ... ok
test5 (__main__.TestEquations) ... ok
test6 (__main__.TestEquations) ... ok
test7 (__main__.TestEquations) ... ok
test8 (__main__.TestEquations) ... ok
test9 (__main__.TestEquations) ... ok
----------------------------------------------------------------------
Ran 11 tests in 0.519s
OK
%
0 コメント:
コメントを投稿