2020年5月10日日曜日

学習環境

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



    1. { a + b = 4 a b = 2 b = 4 - a a 4 - a = 2 a 2 - 4 a + 2 = 0 a = 2 ± 2 b = 4 - 2 ± 2 = 2 2

      (複号同順)


    2. a + b i 2 = a 2 - b 2 + 2 a b i { a 2 - b 2 = 15 2 a b = - 8 b = - 4 a a 2 - 16 a 2 = 15 a 4 - 15 a 2 - 16 = 0 a 2 = 15 + 225 + 64 2 = 15 + 289 2 = 15 + 17 2 = 16 a = ± 4 b = - 4 ± 4 = 1

      (複号同順)


    3. a 2 + 2 a b + 3 b 2 + 3 a 2 - a b - 2 b 2 i = 19 { a 2 + 2 a b + 3 b 2 = 19 3 a 2 - a b - 2 b 2 = 0 3 a + 2 b a - b = 0 3 a + 2 b = 0 b = - 3 2 a a 2 - 3 a 2 + 27 4 a 2 = 19 a 2 = 4 a = ± 2 b = 3 a - b = 0 b = a a 2 + 2 a 2 + 3 a 2 = 19 a 2 = 19 6 a = b = ± 19 6

      (複号同順)

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, solve, Rational, I, sqrt

print('6.')

a, b = symbols('a:b', real=True)


class TestImaginaryNumbers(TestCase):
    def test1(self):
        self.assertEqual(
            solve(((a + b) + a * b * I) - (4 + 2 * I)),
            [{a: 2-sqrt(2), b: 2+sqrt(2)},
             {a: 2+sqrt(2), b: 2-sqrt(2)}]
        )

    def test2(self):
        self.assertEqual(
            solve((a + b * I) ** 2 - (15 - 8 * I)),
            [{a: -4, b: 1},
             {a: 4, b: -1}]
        )

    def test3(self):
        self.assertEqual(
            solve(((1 + 3 * I) * a ** 2 + (2 - I) *
                   a * b + (3 - 2 * I) * b ** 2) - 19),
            [{a: -2, b: 3},
             {a: 2, b: -3},
             {a: -sqrt(Rational(19, 6)), b: -sqrt(Rational(19, 6))},
             {a: sqrt(Rational(19, 6)), b: sqrt(Rational(19, 6))}]
        )


if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample6.py -v
6.
test1 (__main__.TestImaginaryNumbers) ... ok
test2 (__main__.TestImaginaryNumbers) ... ok
test3 (__main__.TestImaginaryNumbers) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.350s

OK
%

0 コメント:

コメントを投稿