## 2019年11月5日火曜日

### 数学 - Python - 代数学 - 整式の計算 - 整数の加法・減法・乗法 - 整数の乗法 - 次数、降順、昇順

1. $\begin{array}{l}\left(x-1\right)\left({x}^{2}+x+1\right)\\ ={x}^{3}-1\end{array}$

2. $\begin{array}{l}\left(4-x-2{x}^{2}\right)\left(1-2x-8{x}^{2}+5{x}^{3}\right)\\ =4-9x-32{x}^{2}+32{x}^{3}+11{x}^{4}-10{x}^{5}\end{array}$

3. $\begin{array}{l}\left(2{x}^{2}-5x-3\right)\left(3x+1+{x}^{3}\right)\\ =2{x}^{5}-5{x}^{4}+3{x}^{3}-13{x}^{2}-14x-3\end{array}$

4. $\begin{array}{l}\left({a}^{2}+ab+{b}^{2}\right)\left({a}^{2}-2ab+2{b}^{2}\right)\\ ={a}^{4}-{a}^{3}b+{a}^{2}{b}^{2}+2{b}^{4}\end{array}$

コード

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

print('8.')

class MyTest(TestCase):

def test(self):
x, a, b = symbols('x, a, b')
spam = [(x - 1) * (x ** 2 + x + 1),
(4 - x - 2 * x ** 2) * (1 - 2 * x - 8 * x ** 2 + 5 * x ** 3),
(2 * x ** 2 - 5 * x - 3) * (3 * x + 1 + x ** 3),
(a ** 2 + a * b + b ** 2) * (a ** 2 - 2 * a * b + 2 * b ** 2)]
egg = [x ** 3 - 1,
4 - 9 * x - 32 * x ** 2 + 32 * x ** 3 + 11 * x ** 4 - 10 * x ** 5,
2 * x ** 5 - 5 * x ** 4 + 3 * x ** 3 - 13 * x ** 2 - 14 * x - 3,
a ** 4 - a ** 3 * b + a ** 2 * b ** 2 + 2 * b ** 4]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t)

if __name__ == '__main__':
main()


% ./sample8.py -v
8.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.025s

OK
%