2020年5月11日月曜日

学習環境

新装版 数学読本3 (松坂 和夫(著)、岩波書店)の第11章(立体的な広がりの中の図形 - 空間図形)、11.2(空間のベクトル)、ベクトルの内積の問13の解答を求めてみる。



    1. cos θ = a · b a b = - 1 , 0 , 1 · - 1 , 2 , 2 - 1 , 0 , 1 - 1 , 2 , 2 = 1 + 2 1 + 1 1 + 4 + 4 = 3 3 2 = 1 2

      よって求める問題の2つのベクトルのなす角は

      π 4

    2. 4 + 2 - 6 1 + 4 + 9 16 + 1 + 4 = 0 π 2

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, solveset, cos, Interval, pi
from sympy.plotting import plot3d_parametric_line

print('13.')


def f(a, b):
    theta = symbols('θ')
    return solveset(a.dot(b) - a.norm() * b.norm() * cos(theta),
                    domain=Interval(0, pi))


a1 = Matrix([-1, 0, 1])
b1 = Matrix([-1, 2, 2])
a2 = Matrix([1, 2, -3])
b2 = Matrix([4, 1, 2])


class TestVector(TestCase):
    def test1(self):
        self.assertEqual(f(a1, b1), {pi / 4})

    def test2(self):
        self.assertEqual(f(a2, b2), {pi / 2})


t = symbols('t')
p = plot3d_parametric_line(*[(*t * o, (t, -1, 1))
                             for o in [a2, b2]],
                           xlim=(-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.save('sample13.png')
p.show()

if __name__ == "__main__":
    main()

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

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

----------------------------------------------------------------------
Ran 2 tests in 0.470s

OK
%

0 コメント:

コメントを投稿