学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、2(連立2次方程式)、問10の解答を求めてみる。
(複号同順)
(複号同順)
学習環境
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、2(連立2次方程式)、問10の解答を求めてみる。
(複号同順)
(複号同順)
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from typing import List
from sympy import symbols, solve, Rational, sqrt
print('10.')
x, y, z = symbols('x, y, z')
def k(d: dict) -> float:
return (d[x], d[y], d[z])
def sorted_xyz(d: List[dict]) -> List[dict]:
return sorted(d, key=k)
class TestEquations(TestCase):
def test1(self):
self.assertEqual(
sorted_xyz(solve([x - 2 * y - 5,
x + y - z + 4,
x ** 2 + 2 * y ** 2 - z ** 2])),
sorted_xyz([{x: 1, y: -2, z: 3},
{x: -Rational(41, 3), y: -Rational(28, 3), z: -19}])
)
def test2(self):
self.assertEqual(
sorted_xyz(solve([x * (x + y + z) - 8,
y * (x + y + z) - 12,
z * (x + y + z) - 5])),
sorted_xyz([{x: s * Rational(8, 5),
y: s * Rational(12, 5),
z: s * 1}
for s in [-1, 1]])
)
def test3(self):
self.assertEqual(
sorted_xyz(solve([x + y + z - 70,
x * y - 420,
x ** 2 + y ** 2 - z ** 2])),
sorted_xyz([{x: 20, y: 21, z: 29},
{x: 21, y: 20, z: 29}])
)
def test4(self):
self.assertEqual(
sorted_xyz(solve([x * y + y * z - 4,
2 * y * z + z * x + 32,
z * x - 8 * x * y + 16])),
sorted_xyz([{x: s * 2 * sqrt(6),
y: s * -sqrt(6) / 3,
z: s * -4 * sqrt(6)}
for s in [-1, 1]])
)
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample10.py -v
10.
test1 (__main__.TestEquations) ... ok
test2 (__main__.TestEquations) ... ok
test3 (__main__.TestEquations) ... ok
test4 (__main__.TestEquations) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.390s
OK
%
0 コメント:
コメントを投稿