## 2019年12月5日木曜日

### 数学 - Python - 代数学 - 整式の計算 - 式の簡約、工夫

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

コード

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

print('3.')

a, b, c = symbols('a, b, c')
expr = sum([egg ** 2 for egg in [a + b + c, -a + b + c, a - b + c, a + b - c]])

class MyTest(TestCase):
def test(self):
expr = sum(
[egg ** 2 for egg in [a + b + c, -a + b + c, a - b + c, a + b - c]])
self.assertEqual(expr.expand(), 4 *
sum([spam ** 2 for spam in [a, b, c]]))

if __name__ == '__main__':
main()


% ./sample3.py -v
3.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.008s

OK
%