2020年2月12日水曜日

学習環境

新装版 数学読本3 (松坂 和夫(著)、岩波書店)の第9章(図形と代数の交錯する世界 - 平面上のベクトル)、9.1(ベクトルとその演算)、内積を成分で表すことの問20の解答を求めてみる。



    1. t0=a·bb·b=6+44+1=2a-2b=-1,2=5

    2. t0=-1-31+1=-2-1,-1=2

    3. t0=a·bb·b=4b2=44=1a-b2=a-b·a-b=a2-2a·b+b2=9-8+4=5a-b=5

コード

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

print('20.')


class MyTestCase(TestCase):
    def test1(self):
        a = Matrix([3, 4])
        b = Matrix([2, 1])
        t = a.dot(b) / b.dot(b)
        self.assertEqual(t, 2)
        self.assertEqual((a - t * b).norm(), sqrt(5))

    def test2(self):
        a = Matrix([1, -3])
        b = Matrix([-1, 1])
        t = a.dot(b) / b.dot(b)
        self.assertEqual(t, -2)
        self.assertEqual((a - t * b).norm(), sqrt(2))


if __name__ == "__main__":
    main()

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

% ./sample20.py -v
20.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.043s

OK
%

0 コメント:

コメントを投稿