## 2019年12月20日金曜日

### 数学 - Python - 代数学 - 因数分解と分数式 - 因数分解 - 共通因数

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

2. $\begin{array}{l}ab-4a+3b-12\\ =a\left(b-4\right)+3\left(b-4\right)\\ =\left(a+3\right)\left(b-4\right)\end{array}$

3. $\begin{array}{l}{x}^{2}-ax-bx+ab\\ =x\left(x-a\right)-b\left(x-a\right)\\ =\left(x-a\right)\left(x-b\right)\end{array}$

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

5. $\begin{array}{l}ab\left({x}^{2}+{y}^{2}\right)+xy\left({a}^{2}+{b}^{2}\right)\\ =ab{x}^{2}+\left({a}^{2}+{b}^{2}\right)yx+ab{y}^{2}\\ =\left(ax+by\right)\left(bx+ay\right)\end{array}$

コード

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

print('1.')

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

if __name__ == '__main__':
main()


% ./sample1.py -v
1.
test (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.060s

OK
%