## 2019年11月16日土曜日

### 数学 - Python - 代数学 - 整式の計算 - 展開公式 - 3乗、計算

1. $\begin{array}{l}{\left(a+2b\right)}^{3}\\ ={a}^{3}+3·{a}^{2}·2b+3a·4{b}^{2}+8{b}^{3}\\ ={a}^{3}+6{a}^{2}b+12a{b}^{2}+8{b}^{3}\end{array}$

2. $\begin{array}{l}{\left(3x-2y\right)}^{3}\\ =27{x}^{3}-3·9{x}^{2}·2y+3·3x·4{y}^{2}-8{y}^{3}\\ =27{x}^{3}-54{x}^{2}y+36x{y}^{2}-8{y}^{3}\end{array}$

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

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

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

コード

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

print('14.')

class MyTest(TestCase):

def test(self):
x, y = symbols('x, y')
spam = [(x + 2 * y) ** 3,
(3 * x - 2 * y) ** 3,
(x + 2) * (x ** 2 - 2 * x + 4),
(x * y - 1) * (x ** 2 * y ** 2 + x * y + 1),
(x - 1 / x) * (x ** 2 + 1 + 1 / x ** 2)]
egg = [x ** 3 + 6 * x ** 2 * y + 12 * x * y ** 2 + 8 * y ** 3,
27 * x ** 3 - 54 * x ** 2 * y + 36 * x * y ** 2 - 8 * y ** 3,
x ** 3 + 8,
x ** 3 * y ** 3 - 1,
x ** 3 - 1 / x ** 3]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t.expand())

if __name__ == '__main__':
main()


% ./sample14.py -v
14.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.021s

OK
%