## 2020年5月4日月曜日

### 数学 - Python - 立体的な広がりの中の図形 - 空間図形 - 空間のベクトル - 座標軸とベクトル - 成分表示、大きさ

1. $\begin{array}{l}\stackrel{\to }{AB}\\ =\left(1,4,-1\right)\\ \left|\stackrel{\to }{AB}\right|\\ =\sqrt{1+16+1}\\ =3\sqrt{2}\end{array}$

2. $\begin{array}{l}\stackrel{\to }{AB}=\left(3,-4,-12\right)\\ \left|\stackrel{\to }{AB}\right|\\ =\sqrt{9+16+144}\\ =13\end{array}$

コード

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

print('9.')

a1 = Matrix([2, 1, 3])
b1 = Matrix([3, 5, 2])
c1 = Matrix([1, 4, -1])
a2 = Matrix([2, 3, 6])
b2 = Matrix([5, -1, - 6])
c2 = Matrix([3, -4, -12])

class TestVector(TestCase):
def test1(self):
self.assertEqual(b1 - a1, c1)
self.assertEqual(c1.norm(), 3 * sqrt(2))

def test2(self):
self.assertEqual(b2 - a2, c2)
self.assertEqual(c2.norm(), 13)

t = symbols('t')
p = plot3d_parametric_line(*[(*(t * v), (t, 0, 1))
for v in [a1, b1, c1, a2, b2, c2]],
*[(*(u + t * v), (t, 0, 1))
for u, v in [(a1, c1), (a2, c2)]],
xlim=(-10, 10),
ylim=(-10, 10),
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.show()
p.save('sample9.png')

if __name__ == "__main__":
main()


% ./sample9.py -v
9.
test1 (__main__.TestVector) ... ok
test2 (__main__.TestVector) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK
%