2020年6月9日火曜日

学習環境

続 解析入門 (原書第2版) (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第1章(ベクトル)、4(ベクトルのノルム)の練習問題5の解答を求めてみる。



    1. cos θ = 1 , - 2 · 5 , 3 1 , - 2 5 , 3 = 5 - 6 1 + 4 25 + 9 = - 1 5 34 = - 1 170

    2. cos θ = - 6 - 4 5 5 = - 2 5

    3. cos θ = - 3 - 2 + 15 14 35 = 10 7 10 = 10 7

    4. cos θ = 2 - 1 + 12 21 11 = 13 231

    5. cos θ = - 2 + 1 2 6 = - 1 2 3

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, sqrt

print('5.')


class TestCosine(TestCase):
    def test(self):
        As = [Matrix(t) for t in
              [(1, -2), (-3, 4), (1, -2, 3), (-2, 1, 4), (-1, 1, 0)]]
        Bs = [Matrix(t) for t in
              [(5, 3), (2, -1), (-3, 1, 5), (-1, -1, 3), (2, 1, -1)]]
        cs = [-1 / sqrt(170),
              -2 / sqrt(5),
              sqrt(10) / 7,
              13 / sqrt(231),
              -1 / (2 * sqrt(3))]
        for i, (A, B, c) in enumerate(zip(As, Bs, cs)):
            print(f'({chr(ord("a") + i)})')
            self.assertEqual(A.dot(B) / (A.norm() * B.norm()), c)


if __name__ == "__main__":
    main()

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

% ./sample5.py -v
5.
test (__main__.TestCosine) ... (a)
(b)
(c)
(d)
(e)
ok

----------------------------------------------------------------------
Ran 1 test in 0.042s

OK
%

0 コメント:

コメントを投稿