## 2020年2月2日日曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - 直線と平面 - 2平面の間の角、余弦、定義、内積、ノルム(絶対値、長さ)

1. 2つの平面の法線ベクトルはそれぞれ、

$\begin{array}{l}a=\left(1,1,1\right)\\ b=\left(1,-1,-1\right)\end{array}$

よって、 2平面の間の角を

$\theta$

とおくと、

$\begin{array}{l}\mathrm{cos}\theta =\frac{a·b}{\left|a\parallel b\right|}\\ =\frac{1-1-1}{\sqrt{3}\sqrt{3}}\\ =-\frac{1}{3}\end{array}$

2. $\begin{array}{l}\mathrm{cos}\theta =\frac{2-3-1}{\sqrt{4+9+1}\sqrt{3}}\\ =\frac{-2}{\sqrt{14}\sqrt{3}}\\ =-\frac{2}{\sqrt{42}}\end{array}$

3. $\begin{array}{l}\mathrm{cos}\theta \\ =\frac{-1+6-1}{\sqrt{6}\sqrt{11}}\\ =\frac{4}{\sqrt{66}}\end{array}$

4. $\begin{array}{l}\mathrm{cos}\theta \\ =\frac{-2-1+1}{\sqrt{6}\sqrt{3}}\\ =-\frac{2}{3\sqrt{2}}\\ =-\frac{\sqrt{2}}{3}\end{array}$

コード

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

print('15.')

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

class MyTestCase(TestCase):
def test(self):
ts = [((1, 1, 1), (1, -1, -1)),
((2, 3, -1), (1, -1, 1)),
((1, 2, -1), (-1, 3, 1)),
((2, 1, 1), (-1, -1, 1))]
cosines = [-Rational(1, 3),
-2 / sqrt(42),
4 / sqrt(66),
-sqrt(2) / 3]
for (a, b), c in zip(ts, cosines):
a = Matrix(a)
b = Matrix(b)
self.assertEqual(cos(a, b), c)

if __name__ == "__main__":
main()


% ./sample15.py -v
15.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.054s

OK
%