## 2019年11月7日木曜日

### 数学 - Python - 代数学 - 整式の計算 - 展開公式 - 和、差の平方、和と差の積

1. $\begin{array}{l}{\left(2a+5\right)}^{2}\\ =4{a}^{2}+20a+25\end{array}$

2. $\begin{array}{l}{\left(4x-7y\right)}^{2}\\ =16{x}^{2}-56xy+49{y}^{2}\end{array}$

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

4. $\begin{array}{l}\left(a+b-c\right)\left(a-b+c\right)\\ =\left(a+\left(b-c\right)\right)\left(a-\left(b-c\right)\right)\\ ={a}^{2}-{\left(b-c\right)}^{2}\\ ={a}^{2}-{b}^{2}+2bc-{c}^{2}\end{array}$
• 確認。
• コード

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

print('10.')

class MyTest(TestCase):

def test(self):
a, b, c, x, y = symbols('a, b, c, x, y')
spam = [(2 * a + 5) ** 2,
(4 * x - 7 * y) ** 2,
(x + y / 2) * (x - y / 2),
(a + b - c) * (a - b + c)]
egg = [4 * a ** 2 + 20 * a + 25,
16 * x ** 2 - 56 * x * y + 49 * y ** 2,
x ** 2 - y ** 2 / 4,
a ** 2 - b ** 2 + 2 * b * c - c ** 2]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t)
self.assertEqual(s.factor(), t.factor())

if __name__ == '__main__':
main()


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

% ./sample10.py -v
10.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.042s

OK
%