## 2019年12月26日木曜日

### 数学 - Python - 代数学 - 因数分解と分数式 - 因数分解 - 雑例 - 次数の低い一つの文字について降べきの順に整理

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

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

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

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

5. $\begin{array}{l}2{x}^{2}+xy-{y}^{2}+10x+4y+12\\ =2{x}^{2}+\left(y+10\right)x-\left({y}^{2}-4y-12\right)\\ =2{x}^{2}+\left(y+10\right)x-\left(y-6\right)\left(y+2\right)\\ =\left(2x-y+6\right)\left(x+y+2\right)\end{array}$

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

コード

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

print('6.')

class MyTest(TestCase):
def test1(self):
x, y = symbols('x, y')
spam = [x ** 2 - x * y - 6 * y ** 2 - x + 23 * y - 20,
2 * x ** 2 - 3 * x * y - 2 * y ** 2 + x + 3 * y - 1,
2 * x ** 2 + 5 * x * y - 3 * y ** 2 - 4 * x + 23 * y - 30,
x ** 2 + (2 * y - 3) * x - (3 * y ** 2 + y - 2),
2 * x ** 2 + x * y - y ** 2 + 10 * x + 4 * y + 12,
x * (5 * x + 2 * y) - 19 * x - 6 * y + 12]
egg = [(x + 2 * y - 5) * (x - 3 * y + 4),
(2 * x + y - 1) * (x - 2 * y + 1),
(2 * x - y + 6) * (x + 3 * y - 5),
(x + 3 * y - 2) * (x - y - 1),
(2 * x - y + 6) * (x + y + 2),
(x - 3) * (5 * x + 2 * y - 4)]
for s, t in zip(spam, egg):
self.assertEqual(s.expand(), t.expand())
self.assertEqual(s.factor(), t)

if __name__ == '__main__':
main()


% ./sample6.py -v
6.
test1 (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.121s

OK
%