## 2019年11月3日日曜日

### 数学 - Python - 代数学 - 整式の計算 - 整式 - 整数の加法・減法・乗法 - 降べきの順に整理

1. $\begin{array}{l}A+B\\ =-x+1\\ A-B\\ =2{x}^{2}+x-7\\ A-B-C\\ =-3{x}^{3}+x-2\\ A+B-\left(C-D\right)\\ =-x+1-\left(4{x}^{3}+2{x}^{2}-6x-12\right)\\ =-4{x}^{3}-2{x}^{2}+5x+13\end{array}$

2. $\begin{array}{l}A+B\\ =-9{a}^{2}+6ab+9{b}^{2}\\ A-B\\ =-{a}^{2}+8ab-7{b}^{2}\\ A-B-C\\ =-7{a}^{2}+5ab+4{b}^{2}\\ A+B-\left(C-D\right)\\ =-9{a}^{2}+6ab+9{b}^{2}-\left(-3{a}^{2}+11ab-14{b}^{2}\right)\\ =-6{a}^{2}-5ab+23{b}^{2}\end{array}$

コード

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()


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

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

OK
%