## 2019年11月9日土曜日

### 数学 - Python - 代数学 - 整式の計算 - 展開公式 - 工夫、共通の部分

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

2. $\begin{array}{l}\left(2a+7b\right)\left(5a-b\right)\\ =10{a}^{2}+33ab-7{b}^{2}\end{array}$

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

コード

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

print('12.')

class MyTest(TestCase):

def test(self):
x, a, b = symbols('x, a, b')
spam = [(6 * x - 5) * (3 * x + 2),
(2 * a + 7 * b) * (5 * a - b),
(x ** 2 - 2 * x + 4) * (x ** 2 - 2 * x - 5)]
egg = [18 * x ** 2 - 3 * x - 10,
10 * a ** 2 + 33 * a * b - 7 * b ** 2,
x ** 4 - 4 * x ** 3 + 3 * x ** 2 + 2 * x - 20]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t)

if __name__ == '__main__':
main()


% ./sample12.py -v
12.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.017s

OK
%