2019年11月3日日曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第2章(整式の計算)、1(整式)、整数の加法・減法・乗法の問4の解答を求めてみる。



    1. A + B = - x + 1 A - B = 2 x 2 + x - 7 A - B - C = - 3 x 3 + x - 2 A + B - C - D = - x + 1 - 4 x 3 + 2 x 2 - 6 x - 12 = - 4 x 3 - 2 x 2 + 5 x + 13

    2. A + B = - 9 a 2 + 6 a b + 9 b 2 A - B = - a 2 + 8 a b - 7 b 2 A - B - C = - 7 a 2 + 5 a b + 4 b 2 A + B - C - D = - 9 a 2 + 6 a b + 9 b 2 - - 3 a 2 + 11 a b - 14 b 2 = - 6 a 2 - 5 a b + 23 b 2

コード

Python 3

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

print('4.')


class MyTest(TestCase):

    def test1(self):
        x, y = symbols('x, y')
        A = x ** 2 - 3
        B = 4 - x - x ** 2
        C = 3 * x ** 3 + 2 * x ** 2 - 5
        D = - x ** 3 + 6 * x + 7
        spam = [A + B, A - B, A - B - C, A + B - (C - D)]
        egg = [-x + 1, 2 * x ** 2 + x - 7, -3 * x ** 3 +
               x - 2, - 4 * x ** 3 - 2 * x ** 2 + 5 * x + 13]
        for s, t in zip(spam, egg):
            self.assertEqual(s, t)

    def test2(self):
        a, b = symbols('a, b')
        A = 7 * a * b - 5 * a ** 2 + b ** 2
        B = -4 * a ** 2 + 8 * b ** 2 - a * b
        C = 6 * a ** 2 + 3 * a * b - 11 * b ** 2
        D = -8 * a * b + 3 * b ** 2 + 9 * a ** 2
        spam = [A + B, A - B, A - B - C, A + B - (C - D)]
        egg = [-9 * a ** 2 + 6 * a * b + 9 * b ** 2,
               - a ** 2 + 8 * a * b - 7 * b ** 2,
               - 7 * a ** 2 + 5 * a * b + 4 * b ** 2,
               - 6 * a ** 2 - 5 * a * b + 23 * b ** 2]
        for s, t in zip(spam, egg):
            self.assertEqual(s, t)


if __name__ == '__main__':
    main()

入出力結果(Zsh、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

% ./sample4.py -v
4.
test1 (__main__.MyTest) ... ok
test2 (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.012s

OK
%

0 コメント:

コメントを投稿