2020年1月28日火曜日

数学 - Python - 代数学 - 1次方程式, 2次方程式 - 1次方程式 - 解、移項の法則

1. $x=-9$

2. $x=-\frac{2}{5}$

3. $\begin{array}{l}3x=-9\\ x=-3\end{array}$

4. $\begin{array}{l}\frac{1}{2}x=7\\ x=14\end{array}$

5. $\begin{array}{l}6x-3=22+11x\\ 5x=-25\\ x=-5\end{array}$

6. $\begin{array}{l}\frac{8}{20}x-\frac{5}{20}x=\frac{3}{2}+\frac{6}{2}\\ \frac{3}{20}x=\frac{9}{2}\\ x=\frac{9}{2}·\frac{20}{3}=30\end{array}$

7. $x=\frac{1}{\sqrt{2}+1}$

8. $\begin{array}{l}\left(\sqrt{2}+\sqrt{3}-2\sqrt{2}\right)x=\sqrt{6}-2\\ \left(\sqrt{3}-\sqrt{2}\right)x=\sqrt{6}-2\\ x=\frac{\sqrt{6}-2}{\sqrt{3}-\sqrt{2}}\\ =\left(\sqrt{6}-2\right)\left(\sqrt{3}+\sqrt{2}\right)\\ =3\sqrt{2}+2\sqrt{3}-2\sqrt{3}-2\sqrt{2}\\ =\sqrt{2}\end{array}$

9. $\begin{array}{l}{x}^{2}-3x+2={x}^{2}-7x+12\\ 4x=10\\ x=\frac{5}{2}\end{array}$

10. $\begin{array}{l}18{x}^{2}+7x-8=18{x}^{2}-3x-28\\ 10x=-20\\ x=-2\end{array}$

コード

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

print('1.')

class MyTest(TestCase):
def test(self):
x = symbols('x')
eqs = [8 - x - 17,
-25 * x - 10,
2 * x - 9 - 5 * x,
x / 2 - 3 - 4,
3 * (2 * x - 1) - 11 * (2 + x),
2 * x / 5 - Rational(3, 2) - x / 4 - 3,
(sqrt(2) + 1) * x - 1,
sqrt(2) * x - sqrt(3) * (sqrt(2) - x) - 2 * (sqrt(2) * x - 1),
(x - 1) * (x - 2) - (x - 3) * (x - 4),
(2 * x - 1) * (9 * x + 8) - (3 * x - 4) * (6 * x + 7)]
xs = [-9, -Rational(2, 5), -3, 14, -5, 30, 1 /
(sqrt(2) + 1), sqrt(2), Rational(5, 2), -2]
for eq, x0 in zip(eqs, xs):
self.assertEqual((solve(eq, x)[0] - x0).simplify(), 0)

if __name__ == '__main__':
main()


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

----------------------------------------------------------------------
Ran 1 test in 0.631s

OK
%