## 2020年1月6日月曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - ベクトルのノルム - 2つのベクトルの間の角、内積とノルムによる余弦の定義

1. 三角形の 3点を

$\begin{array}{l}A=\left(2,-1,1\right)\\ B=\left(1,-3,-5\right)\\ C=\left(3,-4,-4\right)\end{array}$

各角の余弦について、

$\begin{array}{l}\mathrm{cos}A\\ =\frac{\left(-1,-2,-6\right)·\left(1,-3,-5\right)}{\sqrt{1+4+36}·\sqrt{1+9+25}}\\ =\frac{-1+6+30}{\sqrt{41}\sqrt{35}}\\ =\frac{35}{\sqrt{41}\sqrt{35}}\\ \mathrm{cos}B\\ =\frac{\left(1,2,6\right)·\left(2,-1,1\right)}{\sqrt{1+4+36}\sqrt{4+1+1}}\\ =\frac{6}{\sqrt{41·6}}\\ \mathrm{cos}C\\ =\frac{\left(-1,3,5\right)·\left(-2,1,-1\right)}{\sqrt{1+9+25}\sqrt{4+1+1}}\\ =0\end{array}$

2. $\begin{array}{l}\mathrm{cos}A\\ =\frac{\left(-4,1,0\right)·\left(-1,-3,4\right)}{\sqrt{16+1}\sqrt{1+9+16}}\\ =\frac{1}{\sqrt{17·26}}\\ \mathrm{cos}B\\ =\frac{\left(4,-1,0\right)·\left(3,-4,4\right)}{\sqrt{16+1}\sqrt{9+16+16}}\\ =\frac{16}{\sqrt{17·41}}\\ \mathrm{cos}C\\ =\frac{\left(1,3,-4\right)·\left(-3,4,-4\right)}{\sqrt{1+9+16}\sqrt{9+16+16}}\\ =\frac{-3+12+16}{\sqrt{26·41}}\\ =\frac{25}{\sqrt{26·41}}\end{array}$

コード

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

print('5.')

def cos(a, b):
return a.dot(b) / (a.norm() * b.norm())

class MyTestCase(TestCase):
def test_a(self):
a = Matrix([2, -1, 1])
b = Matrix([1, -3, -5])
c = Matrix([3, -4, -4])
self.assertEqual(cos(b - a, c - a), 35 / sqrt(41 * 35))
self.assertEqual(cos(a - b, c - b), 6 / sqrt(41 * 6))
self.assertEqual(cos(a - c, b - c), 0)

def test_b(self):
a = Matrix([3, 1, 1])
b = Matrix([-1, 2, 1])
c = Matrix([2, -2, 5])
self.assertEqual(cos(b - a, c - a), 1 / sqrt(17 * 26))
self.assertEqual(cos(a - b, c - b), 16 / sqrt(17 * 41))
self.assertEqual(cos(a - c, b - c), 25 / sqrt(26 * 41))

if __name__ == '__main__':
main()

% ./sample5.py -v
5.
test_a (__main__.MyTestCase) ... ok
test_b (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.055s

OK
%