2020年1月28日火曜日

学習環境

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



    1. x=-9

    2. x=-25

    3. 3x=-9x=-3

    4. 12x=7x=14

    5. 6x-3=22+11x5x=-25x=-5

    6. 820x-520x=32+62320x=92x=92·203=30

    7. x=12+1

    8. 2+3-22x=6-23-2x=6-2x=6-23-2=6-23+2=32+23-23-22=2

    9. x2-3x+2=x2-7x+124x=10x=52

    10. 18x2+7x-8=18x2-3x-2810x=-20x=-2

コード

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

print('1.')


class MyTest(TestCase):
    def test(self):
        x = symbols('x')
        eqs = [8 - x - 17,
               -25 * x - 10,
               2 * x - 9 - 5 * x,
               x / 2 - 3 - 4,
               3 * (2 * x - 1) - 11 * (2 + x),
               2 * x / 5 - Rational(3, 2) - x / 4 - 3,
               (sqrt(2) + 1) * x - 1,
               sqrt(2) * x - sqrt(3) * (sqrt(2) - x) - 2 * (sqrt(2) * x - 1),
               (x - 1) * (x - 2) - (x - 3) * (x - 4),
               (2 * x - 1) * (9 * x + 8) - (3 * x - 4) * (6 * x + 7)]
        xs = [-9, -Rational(2, 5), -3, 14, -5, 30, 1 /
              (sqrt(2) + 1), sqrt(2), Rational(5, 2), -2]
        for eq, x0 in zip(eqs, xs):
            self.assertEqual((solve(eq, x)[0] - x0).simplify(), 0)


if __name__ == '__main__':
    main()

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

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

----------------------------------------------------------------------
Ran 1 test in 0.631s

OK
%

0 コメント:

コメントを投稿