## 2020年6月12日金曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 係数、判別式、符号、頂点、x軸とy軸との交点、下に凸、上に凸

1. $y=a{\left(x+\frac{b}{2a}\right)}^{2}-\frac{{b}^{2}-4ac}{4a}$
$a>0,-\frac{b}{2a}>0,c>0,D<0$
$a>0,b<0,c>0,D<0$

2. $a>0,-\frac{b}{2a}<0,c=0,D>0$
$a>0,b>0,c=0,D>0$

3. $a<0,-\frac{b}{2a}<0,c<0,D>0$
$a<0,b<0,c<0,D>0$

4. $a<0,-\frac{b}{2a}>0,c<0,D<0$
$a<0,b>0,c<0,D<0$

コード

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

print('3.')

abcs = [(1, -1, 1),
(1, 1, 0),
(-1, -3, -1),
(-1, 1, -1)]

class Test(TestCase):
def test(self):
signs = [-1, 1, 1, -1]
for i, (sign, (a, b, c)) in enumerate(zip(signs, abcs), 1):
print(f'({i})')
if sign == -1:
self.assertLess(b ** 2 - 4 * a * c, 0)
else:
self.assertGreater(b ** 2 - 4 * a * c, 0)

ys = [a * x ** 2 + b * x + c
for a, b, c in abcs]
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

p = plot(*ys,
(x, -5, 5),
ylim=(-5, 5),
legend=True,
show=False)
for o, color in zip(p, colors):
o.line_color = color
p.save('sample3.png')
p.show()

if __name__ == "__main__":
main()


% ./sample3.py -v
3.
test (__main__.Test) ... (1)
(2)
(3)
(4)
ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
%