## 2019年10月16日水曜日

### 数学 - Python - 微分積分学 - 平均値の定理 - 連続関数の加減乗除 - 関数とその絶対値の和、符号、零

1. $f\left(x\right)\ge 0$

のとき、

$\begin{array}{l}\phi \left(x\right)\\ =\frac{\left|f\left(x\right)\right|+f\left(x\right)}{2}\\ =\frac{f\left(x\right)+f\left(x\right)}{2}\\ =\frac{2f\left(x\right)}{2}\\ =f\left(x\right)\end{array}$

また、

$f\left(x\right)\le 0$

の場合、

$\begin{array}{l}\phi \left(x\right)\\ =\frac{\left|f\left(x\right)\right|+f\left(x\right)}{2}\\ =\frac{-f\left(x\right)+f\left(x\right)}{2}\\ =0\end{array}$

コード

Python 3

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

print('1.')

x = symbols('x')
f = x ** 3
g = (abs(f) + f) / 2

class MyTest(TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def test(self):
a = symbols('a', positive=True)
b = symbols('b', negative=True)
self.assertGreaterEqual(g.subs({x: a}), 0)
self.assertEqual(g.subs({x: b}), 0)

p = plot((f, (x, -5, 5)),
(g, (x, -5, 0)),
(g, (x, 0, 5)),
ylim=(-5, 5),
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('sample3.png')

if __name__ == '__main__':
main()


% ./sample1.py
1.
.
----------------------------------------------------------------------
Ran 1 test in 0.007s

OK
%