## 2020年4月13日月曜日

### 数学 - Python - 代数学 - 連立方程式と高次方程式 - 連立2次方程式 - 連立3元2次方程式、未知数、解

1. $\begin{array}{l}x=2y+5\\ 2y+5+y-z=-4\\ z=3y+9\\ {\left(2y+5\right)}^{2}+2{y}^{2}-{\left(3\left(y+3\right)\right)}^{2}=0\\ 4{y}^{2}+20y+25+2{y}^{2}-9{y}^{2}-54y-81=0\\ 3{y}^{2}+34y+56=0\\ y=\frac{-17±\sqrt{1{7}^{2}-168}}{3}\\ =\frac{-17±11}{3}\\ =-2,-\frac{28}{3}\\ \left\{\begin{array}{l}x=1\\ y=-2\\ z=3\end{array}\\ \left\{\begin{array}{l}x=-\frac{41}{3}\\ y=-\frac{28}{3}\\ z=-19\end{array}\end{array}$

2. $\begin{array}{l}{\left(x+y+z\right)}^{2}=25\\ x+y+z=±5\\ \left\{\begin{array}{l}x=±\frac{8}{5}\\ y=±\frac{12}{5}\\ z=±1\end{array}\end{array}$

（複号同順）

3. $\begin{array}{l}y=\frac{420}{x}\\ z=70-\left(x+y\right)\\ =70-\left(x+\frac{420}{x}\right)\\ {x}^{2}+{\left(\frac{420}{x}\right)}^{2}={\left(70-\left(x+\frac{420}{x}\right)\right)}^{2}\\ 7{0}^{2}-2·70\left(x+\frac{420}{x}\right)+2·420=0\\ 35-x-\frac{420}{x}+6=0\\ {x}^{2}-41x+420=0\\ \left(x-20\right)\left(x-21\right)=0\\ \left\{\begin{array}{l}x=20\\ y=21\\ z=29\end{array}\\ \left\{\begin{array}{l}x=21\\ y=20\\ z=29\end{array}\end{array}$

4. $\begin{array}{l}yz=4-xy\\ 8-2xy+zx=-32\\ zx=2xy-40\\ 2xy-40-8xy=-16\\ -6xy=24\\ xy=-4\\ yz=8\\ zx=-48\\ {\left(xyz\right)}^{2}=4·8·48\\ {\left(xyz\right)}^{2}={4}^{2}·2·{4}^{2}·3\\ xyz=±16\sqrt{6}\\ x=±2\sqrt{6}\\ y=\mp \frac{16}{48}\sqrt{6}=\mp \frac{\sqrt{6}}{3}\\ z=\mp 4\sqrt{6}\end{array}$

（複号同順）

コード

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

print('10.')

x, y, z = symbols('x, y, z')

def k(d: dict) -> float:
return (d[x], d[y], d[z])

def sorted_xyz(d: List[dict]) -> List[dict]:
return sorted(d, key=k)

class TestEquations(TestCase):
def test1(self):
self.assertEqual(
sorted_xyz(solve([x - 2 * y - 5,
x + y - z + 4,
x ** 2 + 2 * y ** 2 - z ** 2])),
sorted_xyz([{x: 1, y: -2, z: 3},
{x: -Rational(41, 3), y: -Rational(28, 3), z: -19}])
)

def test2(self):
self.assertEqual(
sorted_xyz(solve([x * (x + y + z) - 8,
y * (x + y + z) - 12,
z * (x + y + z) - 5])),
sorted_xyz([{x: s * Rational(8, 5),
y: s * Rational(12, 5),
z: s * 1}
for s in [-1, 1]])
)

def test3(self):
self.assertEqual(
sorted_xyz(solve([x + y + z - 70,
x * y - 420,
x ** 2 + y ** 2 - z ** 2])),
sorted_xyz([{x: 20, y: 21, z: 29},
{x: 21, y: 20, z: 29}])
)

def test4(self):
self.assertEqual(
sorted_xyz(solve([x * y + y * z - 4,
2 * y * z + z * x + 32,
z * x - 8 * x * y + 16])),
sorted_xyz([{x: s * 2 * sqrt(6),
y: s * -sqrt(6) / 3,
z: s * -4 * sqrt(6)}
for s in [-1, 1]])
)

if __name__ == "__main__":
main()


% ./sample10.py -v
10.
test1 (__main__.TestEquations) ... ok
test2 (__main__.TestEquations) ... ok
test3 (__main__.TestEquations) ... ok
test4 (__main__.TestEquations) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.390s

OK
%