2020年1月15日水曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第17章(ベクトル)、4(ベクトルのノルム)の練習問題13の解答を求めてみる。


  1. f , g = - 1 1 x · x 2 dx = - 1 1 x 3 dx = 0

    よって、求める g に沿う f の射影は、

    f , g g , g g = 0

    である。

    g , f = 0 g , f f , f f = 0

コード

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

print('13.')


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


def projection(f, g):
    return dot(f, g) / dot(g, g) * g


x = symbols('x')
f = x
g = x ** 2


class MyTestCase(TestCase):
    def test1(self):
        self.assertEqual(projection(f, g), 0)

    def test2(self):
        self.assertEqual(projection(g, f), 0)


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

if __name__ == '__main__':
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample13.py -v
13.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.079s

OK
%

0 コメント:

コメントを投稿