## 2019年11月17日日曜日

### 数学 - Python - 代数学 - 整式の計算 - 展開公式 - 3項、累乗(平方)

1. $\begin{array}{l}{\left(a-2b+c\right)}^{2}\\ ={\left(a-2b\right)}^{2}+2\left(a-2b\right)c+{c}^{2}\\ ={a}^{2}-4ab+4{b}^{2}+2ac-4bc+{c}^{2}\\ ={a}^{2}+4{b}^{2}+{c}^{2}-4ab-4bc+2ca\end{array}$

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

3. $\begin{array}{l}{\left(2a-b+3c\right)}^{2}\\ =4{a}^{2}+{b}^{2}+9{c}^{2}-4ab-6bc+12ca\end{array}$

コード

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

print('15.')

class MyTest(TestCase):

def test(self):
x, y, a, b, c = symbols('x, y, a, b, c')
spam = [(a - 2 * b + c) ** 2,
(x + y - 1) ** 2,
(2 * a - b + 3 * c) ** 2]
egg = [a ** 2 + 4 * b ** 2 + c ** 2 - 4 * a * b - 4 * b * c + 2 * c * a,
x ** 2 + y ** 2 + 1 + 2 * x * y - 2 * y - 2 * x,
4 * a ** 2 + b ** 2 + 9 * c ** 2 - 4 * a * b - 6 * b * c + 12 * c * a]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t.expand())

if __name__ == '__main__':
main()


% ./sample15.py -v
15.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.019s

OK
%