2020年3月31日火曜日

学習環境

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



    1. x = - 1 - 2 y 3 - 1 - 2 y - 4 y = 17 - 3 - 6 y - 4 y = 17 y = - 2 x = 3

    2. x = 28 - 6 y 56 - 12 y + y = 12 y = 4 x = 4

    3. { 6 x - 5 y = - 16 6 x + 2 y = 10 7 y = 26 y = 26 7 3 x + 26 7 = 5 3 x = 9 7 x = 3 7

    4. 1 - 2 3 y = - 1 + 1 2 y 1 2 + 2 3 y = 2 3 + 4 y = 12 y = 12 7 x = 1 - 2 3 · 12 7 = 21 - 24 21 = - 1 7

    5. { 5 x + 2 y = 12 2 x + 5 y = - 12 { 10 x + 4 y = 24 10 x + 25 y = - 60 21 y = - 84 y = - 4 2 x - 20 = - 12 x = 4

    6. { x - y + 3 y = 3 4 x + 3 y = 2 { x + 2 y = 3 4 x + 3 y = 2 x = 3 - 2 y 12 - 8 y + 3 y = 2 y = 2 x = - 1

    7. { x + 3 y = 1 x - 2 y = 0 5 y = 1 y = 1 5 x = 2 5

    8. { 4 y - 3 x = - x y 6 y + x = 4 x y x = 6 y 4 y - 1 4 y - 18 y 4 y - 1 = - 6 y 2 4 y - 1 4 4 y - 1 - 18 = - 6 y 2 4 y - 1 - 9 = - 3 y 11 y = 11 y = 1 x = 2

コード

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

print('1.')

x, y = symbols('x, y', real=True)
eqss = [(x + 2 * y + 1, 3 * x - 4 * y - 17),
        (2 * x + y - 12, x + 6 * y - 28),
        (6 * x - 5 * y + 16, 3 * x + y - 5),
        (x + 2 * y / 3 - 1, x - y / 2 + 1),
        (Rational(5, 10) * x + Rational(2, 10) * y - Rational(12, 10),
         x / 2 + 5 * y / 4 + 3),
        ((x - y) / 3 + y - 1, 4 * x + 3 * y - 2),
        (2 * x + 3 * y - 1 - x, 2 * (x - y) - x),
        (4 / x - 3 / y + 1, 6 / x + 1 / y - 4)]
xys = [(3, -2),
       (4, 4),
       (Rational(3, 7), Rational(26, 7)),
       (-Rational(1, 7), Rational(12, 7)),
       (4, -4),
       (-1, 2),
       (Rational(2, 5), Rational(1, 5)),
       (2, 1)]


class Test(TestCase):
    def test(self):
        for i, (eqs, (x0, y0)) in enumerate(zip(eqss, xys), 1):
            s = solve(eqs)
            d = {x: x0, y: y0}
            if type(s) == list:
                self.assertEqual(s, [d])
            else:
                self.assertEqual(s, d)


if __name__ == "__main__":
    main()

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

% ./sample1.py -v
1.
test (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.309s

OK
%

0 コメント:

コメントを投稿