## 2019年12月29日日曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - スカラー積 - 関数空間におけるスカラー積、連続関数、定積分、スカラー積の4つの法則

1. 可換律について。

$\begin{array}{l}{\int }_{-1}^{+1}f\left(x\right)g\left(x\right)\mathrm{dx}\\ ={\int }_{-1}^{+1}g\left(x\right)f\left(x\right)\mathrm{dx}\end{array}$

2. 加法の分配律について。

$\begin{array}{l}{\int }_{-1}^{+1}f\left(x\right)\left(g\left(x\right)+h\left(x\right)\right)\mathrm{dx}\\ ={\int }_{-1}^{+1}\left(f\left(x\right)g\left(x\right)+f\left(x\right)h\left(x\right)\right)\mathrm{dx}\\ ={\int }_{-1}^{+1}f\left(x\right)g\left(x\right)dx+{\int }_{-1}^{+1}f\left(x\right)h\left(x\right)\mathrm{dx}\end{array}$

3. スカラー 倍について。

$\begin{array}{l}{\int }_{-1}^{+1}\left(cf\left(x\right)\right)g\left(x\right)\mathrm{dx}\\ =c{\int }_{-1}^{+1}f\left(x\right)g\left(x\right)dx\end{array}$

4. $\begin{array}{l}{\int }_{-1}^{+1}0·0\mathrm{dx}=0\\ f\left(x\right)\ne 0\\ {\int }_{-1}^{+1}f{\left(x\right)}^{2}\mathrm{dx}>0\end{array}$

コード

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

print('6.')

x, c = symbols('x, c', real=True)
f = x
g = x ** 2
h = x ** 3

def dot(f, g):
return Integral(f * g, (x, -1, 1)).doit()

class MyTestCase(TestCase):
def test_commutative(self):
self.assertEqual(dot(f, g), dot(g, f))

def test_distributive(self):
self.assertEqual(dot(f, g + h), dot(f, g) + dot(f, h))

def test_distributive_scalar(self):
self.assertEqual(dot(c * f, g), c * dot(f, g))

def test_self(self):
self.assertEqual(dot(0, 0), 0)
self.assertGreater(dot(f, f), 0)

p = plot(f, g, h,
(x, -2, 2),
ylim=(-2, 2),
legend=False,
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'sample6.png')

if __name__ == '__main__':
main()


% ./sample6.py -v
6.
test_commutative (__main__.MyTestCase) ... ok
test_distributive (__main__.MyTestCase) ... ok
test_distributive_scalar (__main__.MyTestCase) ... ok
test_self (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.083s

OK
%