## 2020年6月10日水曜日

### 数学 - Pytyhon - 解析学 - ベクトル - ベクトルのノルム - 三角形、頂点、各角の余弦

1. $\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}\stackrel{\to }{AB}=\left(-1,-2,-6\right)\\ \stackrel{\to }{AC}=\left(1,-3,-5\right)\\ \stackrel{\to }{BA}=\left(1,2,6\right)\\ \stackrel{\to }{BC}=\left(2,-1,1\right)\\ \stackrel{\to }{CA}=\left(-1,3,5\right)\\ \stackrel{\to }{CB}=\left(-2,1,-1\right)\end{array}$
$\begin{array}{l}\mathrm{cos}\angle CAB\\ =\frac{\stackrel{\to }{AC}·\stackrel{\to }{AB}}{\parallel \stackrel{\to }{AC}\parallel \parallel \stackrel{\to }{AB}\parallel }\\ =\frac{-1+6+30}{\sqrt{1+9+25}\sqrt{1+4+36}}\\ =\frac{35}{\sqrt{35}\sqrt{41}}\\ =\frac{\sqrt{35}}{\sqrt{41}}\end{array}$
$\begin{array}{l}\mathrm{cos}\angle ABC\\ =\frac{2-2+6}{\sqrt{1+4+36}\sqrt{4+1+1}}\\ =\frac{6}{\sqrt{41}\sqrt{6}}\\ =\frac{\sqrt{6}}{\sqrt{41}}\end{array}$
$\mathrm{cos}\angle BCA=0$

2. $A=\left(3,1,1\right),B=\left(-1,2,1\right),C=\left(2,-2,5\right)$
$\begin{array}{l}\stackrel{\to }{AB}=\left(-4,1,0\right)\\ \stackrel{\to }{AC}=\left(-1,-3,4\right)\\ \stackrel{\to }{BA}=\left(4,-1,0\right)\\ \stackrel{\to }{BC}=\left(3,-4,4\right)\\ \stackrel{\to }{CA}=\left(1,3,-4\right)\\ \stackrel{\to }{CB}=\left(-3,4,-4\right)\end{array}$
$\begin{array}{l}\mathrm{cos}\angle CAB\\ =\frac{4-3}{\sqrt{17}\sqrt{26}}\\ =\frac{1}{\sqrt{17}\sqrt{26}}\end{array}$
$\begin{array}{l}\mathrm{cos}\angle ABC\\ =\frac{12+4}{\sqrt{17}\sqrt{41}}\\ =\frac{16}{\sqrt{17·41}}\end{array}$
$\begin{array}{l}\mathrm{cos}\angle BCA\\ =\frac{-3+12+16}{\sqrt{26}\sqrt{41}}\\ =\frac{25}{\sqrt{26·41}}\end{array}$

コード

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

print('6.')

class TestTriangleCosine(TestCase):
def test(self):
As = [Matrix(t) for t in [(2, -1, 1),
(3, 1, 1)]]
Bs = [Matrix(t) for t in [(1, -3, -5),
(-1, 2, 1)]]
Cs = [Matrix(t) for t in [(3, -4, -4),
(2, -2, 5)]]
cs = [(sqrt(35) / sqrt(41), sqrt(6) / sqrt(41), 0),
(1 / sqrt(17 * 26), 16 / sqrt(17 * 41), 25 / sqrt(26 * 41))]
for i, (A, B, C, (ca, cb, cc)) in enumerate(zip(As, Bs, Cs, cs)):
print(f'({chr(ord("a") + i)})')
self.assertEqual((B - A).dot(C - A) / ((B - A).norm() * (C - A).norm()),
ca)
self.assertEqual((C - B).dot(A - B) / ((C - B).norm() * (A - B).norm()),
cb)
self.assertEqual((B - C).dot(A - C) / ((B - C).norm() * (A - C).norm()),
cc)

if __name__ == "__main__":
main()


% ./sample6.py -v
6.
test (__main__.TestTriangleCosine) ... (a)
(b)
ok

----------------------------------------------------------------------
Ran 1 test in 0.048s

OK
%