## 2020年1月20日月曜日

### 数学 - Python - 代数学 - 因数分解と分数式 - 整式の最大公約数、最小公倍数

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

よって、最大公約数は

$x-y$

最小公倍数は

$\left({x}^{2}+xy+{y}^{2}\right)\left({x}^{2}+{y}^{2}\right)\left(x+y\right)\left(x-y\right)$

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

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

4. $\begin{array}{l}4{a}^{2}-9\\ =\left(2a-3\right)\left(2a+3\right)\\ 6{a}^{2}+5a-6\\ =\left(2a+3\right)\left(3a-2\right)\\ 6{a}^{2}+13a+6\\ =\left(2a+3\right)\left(3a+2\right)\\ 2a+3\\ \left(2a-3\right)\left(2a+3\right)\left(3a+2\right)\left(3a-2\right)\\ =\left(4{a}^{2}-9\right)\left(9{a}^{2}-4\right)\\ =36{a}^{4}-97{a}^{2}+36\end{array}$

コード

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

print('2.')

x, y, a, b, c = symbols('x, y, a, b, c', real=True)

class MyTest(TestCase):
def test1(self):
exprs = (x ** 4 - y ** 4, x ** 3 - y ** 3)
self.assertEqual(gcd(exprs), x - y)
self.assertEqual(lcm(exprs),
((x ** 2 + x * y + y ** 2) * (x ** 2 + y ** 2) * (x + y) * (x - y)).expand())

def test2(self):
exprs = (2 * a * b - a ** 2 - b ** 2 + c ** 2,
a ** 2 - b ** 2 + c ** 2 + 2 * a * c)
self.assertEqual(gcd(exprs), a - b + c)
self.assertEqual(lcm(exprs),
((a + b + c) * (- a + b + c) * (a - b + c)).expand())

def test3(self):
exprs = (x ** 4 - 27 * x ** 2 * y ** 2 +
y ** 4, x ** 2 + 5 * x * y - y ** 2)
self.assertEqual(gcd(exprs), x ** 2 + 5 * x * y - y ** 2)
self.assertEqual(lcm(exprs), (x ** 4 - 27 * x ** 2 * y ** 2 + y ** 4))

def test4(self):
exprs = (4 * a ** 2 - 9,
6 * a ** 2 + 5 * a - 6,
6 * a ** 2 + 13 * a + 6)
self.assertEqual(gcd(exprs), 2 * a + 3)
self.assertEqual(lcm(exprs), 36 * a ** 4 - 97 * a ** 2 + 36)

if __name__ == '__main__':
main()


% ./sample2.py -v
2.
test1 (__main__.MyTest) ... ok
test2 (__main__.MyTest) ... ok
test3 (__main__.MyTest) ... ok
test4 (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.103s

OK
%