## 2020年3月31日火曜日

### 数学 - Python - 代数学 - 連立方程式と高次方程式 - 連立1次方程式 - 連立2元1次方程式、未知数、加減法、代入法、等値法

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

2. $\begin{array}{l}x=28-6y\\ 56-12y+y=12\\ y=4\\ x=4\end{array}$

3. $\begin{array}{l}\left\{\begin{array}{l}6x-5y=-16\\ 6x+2y=10\end{array}\\ 7y=26\\ y=\frac{26}{7}\\ 3x+\frac{26}{7}=5\\ 3x=\frac{9}{7}\\ x=\frac{3}{7}\end{array}$

4. $\begin{array}{l}1-\frac{2}{3}y=-1+\frac{1}{2}y\\ \left(\frac{1}{2}+\frac{2}{3}\right)y=2\\ \left(3+4\right)y=12\\ y=\frac{12}{7}\\ x=1-\frac{2}{3}·\frac{12}{7}\\ =\frac{21-24}{21}\\ =-\frac{1}{7}\end{array}$

5. $\begin{array}{l}\left\{\begin{array}{l}5x+2y=12\\ 2x+5y=-12\end{array}\\ \left\{\begin{array}{l}10x+4y=24\\ 10x+25y=-60\end{array}\\ 21y=-84\\ y=-4\\ 2x-20=-12\\ x=4\end{array}$

6. $\begin{array}{l}\left\{\begin{array}{l}x-y+3y=3\\ 4x+3y=2\end{array}\\ \left\{\begin{array}{l}x+2y=3\\ 4x+3y=2\end{array}\\ x=3-2y\\ 12-8y+3y=2\\ y=2\\ x=-1\end{array}$

7. $\begin{array}{l}\left\{\begin{array}{l}x+3y=1\\ x-2y=0\end{array}\\ 5y=1\\ y=\frac{1}{5}\\ x=\frac{2}{5}\end{array}$

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

コード

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

print('1.')

x, y = symbols('x, y', real=True)
eqss = [(x + 2 * y + 1, 3 * x - 4 * y - 17),
(2 * x + y - 12, x + 6 * y - 28),
(6 * x - 5 * y + 16, 3 * x + y - 5),
(x + 2 * y / 3 - 1, x - y / 2 + 1),
(Rational(5, 10) * x + Rational(2, 10) * y - Rational(12, 10),
x / 2 + 5 * y / 4 + 3),
((x - y) / 3 + y - 1, 4 * x + 3 * y - 2),
(2 * x + 3 * y - 1 - x, 2 * (x - y) - x),
(4 / x - 3 / y + 1, 6 / x + 1 / y - 4)]
xys = [(3, -2),
(4, 4),
(Rational(3, 7), Rational(26, 7)),
(-Rational(1, 7), Rational(12, 7)),
(4, -4),
(-1, 2),
(Rational(2, 5), Rational(1, 5)),
(2, 1)]

class Test(TestCase):
def test(self):
for i, (eqs, (x0, y0)) in enumerate(zip(eqss, xys), 1):
s = solve(eqs)
d = {x: x0, y: y0}
if type(s) == list:
self.assertEqual(s, [d])
else:
self.assertEqual(s, d)

if __name__ == "__main__":
main()


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

----------------------------------------------------------------------
Ran 1 test in 0.309s

OK
%