## 2020年6月16日火曜日

### 数学 - Pytyhon - 解析学 - ベクトル - パラメーター表示された直線 - 2点を通る直線のパラメーター方程式

1. $X=\left(1,3,-1\right)+t\left(5,2,-3\right)$

2. $X=\left(-1,5,3\right)+t\left(1,1,-4\right)$

1. $X=\left(1,1,-1\right)+t\left(3,0,-4\right)$

2. $X=\left(-1,5,2\right)+t\left(4,-9,-1\right)$

コード

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

print('1, 2, 3.')

fs = [Matrix([1, 3, -1]) + t * Matrix([5, 2, -3]),
Matrix([-1, 5, 3]) + t * Matrix([1, 1, -4]),
Matrix([1, 1, -1]) + t * Matrix([3, 0, -4]),
Matrix([-1, 5, 2]) + t * Matrix([4, -9, -1])]

class Test(TestCase):
def test1a(self):
self.assertEqual(
len(solve(fs[0] - Matrix([1, 3, -1]))),
1
)
self.assertEqual(
len(solve(fs[0] - Matrix([-4, 1, 2]))),
1
)

def test1b(self):
self.assertEqual(
len(solve(fs[1] - Matrix([-1, 5, 3]))),
1
)
self.assertEqual(
len(solve(fs[1] - Matrix([-2, 4, 7]))),
1
)

def test2(self):
self.assertEqual(
len(solve(fs[2] - Matrix([1, 1, -1]))),
1
)
self.assertEqual(
len(solve(fs[2] - Matrix([-2, 1, 3]))),
1
)

def test3(self):
self.assertEqual(
len(solve(fs[3] - Matrix([-1, 5, 2]))),
1
)
self.assertEqual(
len(solve(fs[3] - Matrix([3, -4, 1]))),
1
)

p = plot3d_parametric_line(*[(*f, (t, -5, 5))
for f in fs],
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('sample1.png')
if __name__ == "__main__":
main()


% ./sample1.py -v
1, 2, 3.
test1a (__main__.Test) ... ok
test1b (__main__.Test) ... ok
test2 (__main__.Test) ... ok
test3 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.031s

OK
%