## 2020年6月9日火曜日

### 数学 - Pytyhon - 解析学 - ベクトル - ベクトルのノルム - 2つのベクトルの間の角の余弦、内積、2次元、3次元

1. $\begin{array}{l}\mathrm{cos}\theta \\ =\frac{\left(1,-2\right)·\left(5,3\right)}{∥\left(1,-2\right)∥∥\left(5,3\right)∥}\\ =\frac{5-6}{\sqrt{1+4}\sqrt{25+9}}\\ =-\frac{1}{\sqrt{5}\sqrt{34}}\\ =-\frac{1}{\sqrt{170}}\end{array}$

2. $\mathrm{cos}\theta =\frac{-6-4}{5\sqrt{5}}=-\frac{2}{\sqrt{5}}$

3. $\mathrm{cos}\theta =\frac{-3-2+15}{\sqrt{14}\sqrt{35}}=\frac{10}{7\sqrt{10}}=\frac{\sqrt{10}}{7}$

4. $\mathrm{cos}\theta =\frac{2-1+12}{\sqrt{21}\sqrt{11}}=\frac{13}{\sqrt{231}}$

5. $\mathrm{cos}\theta =\frac{-2+1}{\sqrt{2}\sqrt{6}}=-\frac{1}{2\sqrt{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()


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

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

OK
%