2020年4月13日月曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式)、2(連立2次方程式)、問10の解答を求めてみる。



    1. x = 2 y + 5 2 y + 5 + y - z = - 4 z = 3 y + 9 2 y + 5 2 + 2 y 2 - 3 y + 3 2 = 0 4 y 2 + 20 y + 25 + 2 y 2 - 9 y 2 - 54 y - 81 = 0 3 y 2 + 34 y + 56 = 0 y = - 17 ± 1 7 2 - 168 3 = - 17 ± 11 3 = - 2 , - 28 3 { x = 1 y = - 2 z = 3 { x = - 41 3 y = - 28 3 z = - 19

    2. x + y + z 2 = 25 x + y + z = ± 5 { x = ± 8 5 y = ± 12 5 z = ± 1

      (複号同順)


    3. y = 420 x z = 70 - x + y = 70 - x + 420 x x 2 + 420 x 2 = 70 - x + 420 x 2 7 0 2 - 2 · 70 x + 420 x + 2 · 420 = 0 35 - x - 420 x + 6 = 0 x 2 - 41 x + 420 = 0 x - 20 x - 21 = 0 { x = 20 y = 21 z = 29 { x = 21 y = 20 z = 29

    4. y z = 4 - x y 8 - 2 x y + z x = - 32 z x = 2 x y - 40 2 x y - 40 - 8 x y = - 16 - 6 x y = 24 x y = - 4 y z = 8 z x = - 48 x y z 2 = 4 · 8 · 48 x y z 2 = 4 2 · 2 · 4 2 · 3 x y z = ± 16 6 x = ± 2 6 y = 16 48 6 = 6 3 z = 4 6

      (複号同順)

コード

#!/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 コメント:

コメントを投稿