## 2019年12月30日月曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - スカラー積 - 直線、累乗(平方)、奇関数、偶関数、関数空間におけるスカラー積、連続関数、定積分

• $\begin{array}{l}⟨f,f⟩\\ =⟨x,x⟩\\ ={\int }_{-1}^{1}{x}^{2}\mathrm{dx}\\ =\frac{2}{3}\end{array}$

• $\begin{array}{l}⟨g,g⟩\\ =⟨{x}^{2},{x}^{2}⟩\\ ={\int }_{-1}^{1}{x}^{4}\mathrm{dx}\\ =\frac{2}{5}\end{array}$

• $\begin{array}{l}⟨f,g⟩\\ =⟨x,{x}^{2}⟩\\ ={\int }_{-1}^{1}{x}^{3}\mathrm{dx}\\ =0\end{array}$

コード

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

print('7.')

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

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

class MyTestCase(TestCase):
def test_ff(self):
self.assertEqual(dot(f, f), Rational(2, 3))

def test_gg(self):
self.assertEqual(dot(g, g), Rational(2, 5))

def test_fg(self):
self.assertEqual(dot(f, g), 0)

p = plot(f, g,
(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'sample7.png')

if __name__ == '__main__':
main()


% ./sample7.py
7.
...
----------------------------------------------------------------------
Ran 3 tests in 0.060s

OK
kamimura@iMac dir3 % ./sample7.py -v
7.
test_ff (__main__.MyTestCase) ... ok
test_fg (__main__.MyTestCase) ... ok
test_gg (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.060s

OK
%