## 2020年6月26日金曜日

### 数学 - Pytyhon - 解析学 - ベクトル - 平面 - 2つのベクトルに直交するベクトル

• 求める問題の2つのベクトルに直交するベクトルを

$\left(a,b,c\right)$

とおくと、

$\left\{\begin{array}{l}a+2b-3c=0\\ 2a-b+3c=0\end{array}$
$\begin{array}{l}3a+b=0\\ b=-3a\end{array}$
$\begin{array}{l}2a+3a+3c=0\\ c=-\frac{5}{3}a\end{array}$

よって、 直交するベクトルの 1つは

$\left(3,-9,-5\right)$

• $\left\{\begin{array}{l}-a+3b+2c=0\\ 2a+b+c=0\end{array}$
$\begin{array}{l}-5a+b=0\\ b=5a\end{array}$
$\begin{array}{l}2a+5a+c=0\\ c=-7a\end{array}$
$\left(1,5,-7\right)$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import solve, Matrix
from sympy.abc import b, c, t
from sympy.plotting import plot3d_parametric_line

print('9.')

class Test(TestCase):
def test1(self):
x = Matrix([3, b, c])
self.assertEqual(
solve([x.dot(Matrix([1, 2, -3])),
x.dot(Matrix([2, -1, 3]))]),
{b: -9, c: -5}
)

def test2(self):
x = Matrix([1, b, c])
self.assertEqual(
solve([x.dot(Matrix([-1, 3, 2])),
x.dot(Matrix([2, 1, 1]))]),
{b: 5, c: -7},
)

p = plot3d_parametric_line(*[(a * t, b * t, c * t, (t, 0, 1))
for a, b, c in [(1, 2, -3),
(2, -1, 3),
(3, -9, -5),
(-1, 3, 2),
(2, 1, 1),
(1, 5, -7)]],
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__.Test) ... ok
test2 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.027s

OK
%