2020年1月7日火曜日

数学 - Python - 代数学 - 因数分解と分数式 - 分数式とその計算 - 繁分数式、加法、乗法、通分

1. $\begin{array}{l}\frac{\frac{1}{x}+\frac{1}{y}}{\frac{1}{x}-\frac{1}{y}}\\ =\frac{y+x}{y-x}\end{array}$

2. $\begin{array}{l}\frac{1}{a-\frac{1}{a+\frac{1}{a}}}+\frac{1}{a+\frac{1}{a-\frac{1}{a}}}\\ =\frac{1}{a-\frac{a}{{a}^{2}+1}}+\frac{1}{a+\frac{a}{{a}^{2}-1}}\\ =\frac{{a}^{2}+1}{{a}^{3}+a-a}+\frac{{a}^{2}-1}{{a}^{3}-a+a}\\ =\frac{2{a}^{2}}{{a}^{3}}\\ =\frac{2}{a}\end{array}$

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

コード

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

print('15.')

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

if __name__ == '__main__':
main()


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

----------------------------------------------------------------------
Ran 1 test in 0.492s

OK
%