## 2020年5月12日火曜日

### 数学 - Python - 立体的な広がりの中の図形 - 空間図形 - 空間のベクトル - ベクトルの内積 - 空間上の三点、三角形、三角関数(余弦、正弦)、面積

1. $\begin{array}{l}\stackrel{\to }{AB}\\ =\left(3,-2,4\right)-\left(2,0,1\right)\\ =\left(1,-2,3\right)\\ \stackrel{\to }{AC}\\ =\left(-1,2,0\right)-\left(2,0,1\right)\\ =\left(-3,2,-1\right)\\ \mathrm{cos}\theta \\ =\frac{\stackrel{\to }{AB}·\stackrel{\to }{AC}}{\left|\stackrel{\to }{AB}\right|\left|\stackrel{\to }{AC}\right|}\\ =\frac{-3-4-3}{\sqrt{1+4+9}\sqrt{9+4+1}}\\ =-\frac{10}{14}\\ =-\frac{5}{7}\end{array}$

2. $\begin{array}{l}S\\ =\frac{1}{2}\left|\stackrel{\to }{AB}\right|\left|\stackrel{\to }{AC}\right|\mathrm{sin}\theta \\ =\frac{1}{2}·14·\sqrt{1-{\left(\frac{5}{7}\right)}^{2}}\\ =\sqrt{{7}^{2}-{5}^{2}}\\ =\sqrt{\left(7+5\right)\left(7-5\right)}\\ =\sqrt{12·2}\\ =2\sqrt{6}\end{array}$

コード

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

print('14.')

a = Matrix([2, 0, 1])
b = Matrix([3, -2, 4])
c = Matrix([-1, 2, 0])
ab = b - a
ac = c - a
cos_theta = ab.dot(ac) / (ab.norm() * ac.norm())

class TestTriangle(TestCase):
def test1(self):
self.assertEqual(cos_theta, -Rational(5, 7))

def test2(self):
sin_theta = sqrt(1 - cos_theta ** 2)
self.assertEqual(ab.norm() * ac.norm() * sin_theta / 2, 2 * sqrt(6))

t = symbols('t')
p = plot3d_parametric_line(*[(*(o + t * q), (t, 0, 1))
for (o, q) in [(a, ab), (a, ac), (b, (c - b))]],
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('sample14.png')
p.show()

if __name__ == "__main__":
main()


% ./sample14.py -v
14.
test1 (__main__.TestTriangle) ... ok
test2 (__main__.TestTriangle) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK
%