2020年4月1日水曜日

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

1. $\begin{array}{l}4y+11z=25\\ 12x+16y-8z=-44\\ 12x-6y+9z=51\\ 22y-17z=-95\\ 44y-34z=-190\\ 44y+121z=275\\ 155z=465\\ 31z=93\\ z=3\\ 4y+33=25\\ 4y=-8\\ y=-2\\ 2x+6-12=-4\\ x=1\end{array}$

2. $\begin{array}{l}2x=24\\ x=12\\ 2y=14\\ y=7\\ 2z=10\\ z=5\end{array}$

3. $\begin{array}{l}9y-5z=-1\\ 13y-4z=5\\ 36y-20z=-4\\ 65y-20z=25\\ 29y=29\\ y=1\\ 13-4z=5\\ z=2\\ x-4+4=3\\ x=3\end{array}$

4. $\begin{array}{l}2y+2z=10\\ y+z=5\\ z=5-y\\ 5y-15+3y=1\\ 8y=16\\ y=2\\ z=3\\ 2x-2+3=-1\\ x=-1\end{array}$

5. $\begin{array}{l}\left\{\begin{array}{l}3x+2y=8\\ 3y+z=10\\ 5x+y-z=12\end{array}\\ z=10-3y\\ 5x+y-10+3y=12\\ 5x+4y=22\\ 6x+4y=16\\ x=-6\\ -18+2y=8\\ y=13\\ z=-29\end{array}$

6. $\begin{array}{l}3\left(x+y+z+u\right)=30\\ x+y+z+u=10\\ x=1\\ y=2\\ z=7\\ u=4\end{array}$

7. $\begin{array}{l}6\left(x+y+z+u\right)=0\\ x+y+z+u=0\\ 2u=2\\ u=1\\ 2z=-6\\ z=-3\\ y=0\\ x=2\end{array}$

8. $\begin{array}{l}z=4x+4+5=4x+9\\ y=8x+18-4=8x+14\\ x=16x+28+2\\ x=-2\\ y=-2\\ z=1\\ u=-2\end{array}$

コード

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

print('2.')

x, y, z, u = symbols('x, y, z, u', real=True)
eqss = [(2 * x - 3 * y - 4 * z + 4,
3 * x + 4 * y - 2 * z + 11,
4 * x - 2 * y + 3 * z - 17),
(x + y - z - 14,
x - y + z - 10,
x - y - z),
(x - 4 * y + 2 * z - 3,
2 * x + y - z - 5,
3 * x + y + 2 * z - 14),
(2 * x - y + z + 1,
5 * y - 3 * z - 1,
6 * x - y + 5 * z - 7),
((3 * x + 2 * y) / 4 - 2,
(3 * y + z) / 5 - 2,
(5 * x + y - z) / 6 - 2),
(y + z + u - 9,
z + u + x - 8,
u + x + y - 7,
x + y + z - 6),
(x + y + z + 3 * u - 2,
x + y + 3 * z + u + 6,
x + 3 * y + z + u,
3 * x + y + z + u - 4),
(x - (2 * y + 2),
y - (2 * z - 4),
z - (2 * u + 5),
u - (2 * x + 2))]
xyzus = [(1, -2, 3),
(12, 7, 5),
(3, 1, 2),
(-1, 2, 3),
(-6, 13, -29),
(1, 2, 3, 4),
(2, 0, -3, 1),
(-2, -2, 1, -2)]

class Test(TestCase):
def test3(self):
for eqs, (x0, y0, z0) in zip(eqss[0:5], xyzus[:5]):
s = solve(eqs)
d = {x: x0, y: y0, z: z0}
if type(s) == list:
self.assertEqual(s, [d])
else:
self.assertEqual(s, d)

def test4(self):
for eqs, (x0, y0, z0, u0) in zip(eqss[5:], xyzus[5:]):
s = solve(eqs)
d = {x: x0, y: y0, z: z0, u: u0}
if type(s) == list:
self.assertEqual(s, [d])
else:
self.assertEqual(s, d)

if __name__ == "__main__":
main()

% ./sample2.py -v
2.
test3 (__main__.Test) ... ok
test4 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.106s

OK
%