## 2020年3月7日土曜日

### 数学 - Python - 代数学 - 1次方程式, 2次方程式 - 解の係数の関係、2次式の因数分解 - 和と積、解から2次方程式を作成

1. $\begin{array}{l}\alpha +\beta =-\frac{-5}{2}=\frac{5}{2}\\ \alpha \beta =\frac{-4}{2}=-2\\ \left(x-\left(\alpha +3\right)\right)\left(x-\left(\beta +3\right)\right)=0\\ {x}^{2}-\left(\alpha +\beta +6\right)x+\alpha \beta +3\left(\alpha +\beta \right)+9=0\\ {x}^{2}-\left(\frac{5}{2}+6\right)x-2+\frac{15}{2}+9=0\\ 2{x}^{2}-17x+29=0\end{array}$

2. $\begin{array}{l}\left(x-4\alpha \right)\left(x-4\beta \right)=0\\ {x}^{2}-4\left(\alpha +\beta \right)x+16\alpha \beta =0\\ {x}^{2}-10x-32=0\end{array}$

3. $\begin{array}{l}\left(x-{\alpha }^{2}\right)\left(x-{\beta }^{2}\right)=0\\ {x}^{2}-\left({\alpha }^{2}+{\beta }^{2}\right)x+{\alpha }^{2}{\beta }^{2}=0\\ {x}^{2}-\left({\left(\alpha +\beta \right)}^{2}-2\alpha \beta \right)x+{\left(\alpha \beta \right)}^{2}=0\\ {x}^{2}-\left(\frac{25}{4}+4\right)x+4=0\\ 4{x}^{2}-41x+16=0\end{array}$

4. $\begin{array}{l}\left(x-\left(\alpha +\beta \right)\right)\left(x-\alpha \beta \right)=0\\ \left(x-\frac{5}{2}\right)\left(x+2\right)=0\\ {x}^{2}-\frac{1}{2}x-5=0\\ 2{x}^{2}-x-10=0\end{array}$

5. $\begin{array}{l}\left(x-\frac{1}{\alpha -1}\right)\left(x-\frac{1}{\beta -1}\right)=0\\ {x}^{2}-\left(\frac{1}{\alpha -1}+\frac{1}{\beta -1}\right)x+\frac{1}{\left(\alpha -1\right)\left(\beta -1\right)}=0\\ {x}^{2}-\frac{\alpha +\beta -2}{\alpha \beta -\left(\alpha +\beta \right)+1}x+\frac{1}{\alpha \beta -\left(\alpha +\beta \right)+1}=0\\ {x}^{2}-\frac{1}{2}·\left(-\frac{2}{7}\right)x-\frac{2}{7}=0\\ 7{x}^{2}+x-2=0\end{array}$

コード

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

print('33.')

x = symbols('x')
f = 2 * x ** 2 - 5 * x - 4
abcs = [(2, -17, 29),
(1, -10, -32),
(4, -41, 16),
(2, -1, -10),
(7, 1, -2)]
gs = [a * x ** 2 + b * x + c for a, b, c in abcs]
a, b = solveset(f)

class MyTestCase(TestCase):
def test1(self):
self.assertEqual({a + 3, b + 3}, solveset(gs[0]))

def test2(self):
self.assertEqual({4 * a, 4 * b}, solveset(gs[1]))

def test3(self):
self.assertEqual(
{x0.expand() for x0 in [a ** 2, b ** 2]},
solveset(gs[2]))

def test4(self):
self.assertEqual({x0.expand()
for x0 in [a + b, a * b]},
solveset(gs[3]))

def test5(self):
self.assertEqual({x0.simplify()
for x0 in [1 / (a - 1), 1 / (b - 1)]},
solveset(gs[4]))

p = plot(f, *gs,
(x, -20, 20),
ylim=(-20, 20),
legend=True,
show=False)

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color

p.show()
p.save(f'sample33.png')

if __name__ == "__main__":
main()


% ./sample33.py -v
33.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok
test4 (__main__.MyTestCase) ... ok
test5 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.451s

OK
%