2020年2月9日日曜日

学習環境

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



    1. x=5±25+9612=5±12112=5±1112=-12,43

    2. x=-2±22+4·42=-1±1+4=-1±5

    3. x=-8±82+4·162=-4±16+16=-4±42

    4. x=-17±172-24·122·6=-17±112=-32,-43

    5. x=11±121-762=11±452=11±352

    6. x=4±42+4·62·3=2±4+63=2±103

    7. 25x2-10x+1=0x=10±4·52-4·252·25=225=15

    8. x=22±22·112-4·492=11±121-49=11±72=11±62

    9. x=-7±49+4·182·32=-7±49+7262=-7±12162=-7±1162=-32,232=-32,23

    10. x=2·73±22·72·3-4·262·2=73±147-262=73±1212=73±112

コード

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

print('10.')


class MyTestCase(TestCase):
    def test(self):
        x = symbols('x', real=True)
        eqs = [(6, -5, -4),
               (1, 2, -4),
               (1, 8, -16),
               (6, 17, 12),
               (1, -11, 19),
               (3, -4, -2),
               (5, -2, Rational(1, 5)),
               (1, -22, 49),
               (3 * sqrt(2), 7, -3 * sqrt(2)),
               (2, -14 * sqrt(3), 13)]
        xss = [{-Rational(1, 2), Rational(4, 3)},
               {-1 - sqrt(5), -1 + sqrt(5)},
               {-4 - 4 * sqrt(2), -4 + 4 * sqrt(2)},
               {-Rational(3, 2), -Rational(4, 3)},
               {(11 - 3 * sqrt(5)) / 2, (11 + 3 * sqrt(5)) / 2},
               {(2 - sqrt(10)) / 3, (2 + sqrt(10)) / 3},
               {Rational(1, 5)},
               {11 - 6 * sqrt(2), 11 + 6 * sqrt(2)},
               {-3 / sqrt(2), sqrt(2) / 3},
               {(7 * sqrt(3) - 11) / 2, (7 * sqrt(3) + 11) / 2}]
        for (a, b, c), xs in zip(eqs, xss):
            eq = a * x ** 2 + b * x + c
            self.assertEqual(solveset(eq), xs)


if __name__ == "__main__":
    main()

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

% ./sample10.py -v
10.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.599s

OK
%

0 コメント:

コメントを投稿