## 2020年6月17日水曜日

### 数学 - Pytyhon - 解析学 - ベクトル - パラメーター表示された直線 - 線分の中点、n等分する点

1. $\left(1,3,-1\right)+\frac{1}{2}\left(-5,2,3\right)=\left(-\frac{3}{2},4,\frac{1}{2}\right)$

2. $\begin{array}{l}\left(1,3,-1\right)+\frac{1}{3}\left(-5,2,3\right)=\left(-\frac{2}{3},\frac{11}{3},0\right)\\ \left(1,3,-1\right)+\frac{2}{3}\left(-5,2,3\right)=\left(-\frac{7}{3},\frac{13}{3},1\right)\end{array}$

3. $\left(1,3,-1\right)+\frac{1}{5}\left(-5,2,3\right)=\left(0,\frac{17}{5},-\frac{2}{5}\right)$

4. $\left(1,3,-1\right)+\frac{2}{5}\left(-5,2,3\right)=\left(-1,\frac{19}{5},\frac{1}{5}\right)$

コード

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

print('4.')

p = Matrix([1, 3, -1])
q = Matrix([-4, 5, 2])
qp = q - p
l = p + t * qp

class Test(TestCase):
def test_a(self):
self.assertEqual(
l.subs({t: Rational(1, 2)}),
Matrix([-Rational(3, 2), 4, Rational(1, 2)])
)

def test_b(self):
self.assertEqual(
l.subs({t: Rational(1, 3)}),
Matrix([-Rational(2, 3), Rational(11, 3), 0])
)
self.assertEqual(
l.subs({t: Rational(2, 3)}),
Matrix([-Rational(7, 3), Rational(13, 3), 1])
)

def test_c(self):
self.assertEqual(
l.subs({t: Rational(1, 5)}),
Matrix([0, Rational(17, 5), -Rational(2, 5)])
)

def test_d(self):
self.assertEqual(
l.subs({t: Rational(2, 5)}),
Matrix([-1, Rational(19, 5), Rational(1, 5)])
)

if __name__ == "__main__":
main()


% ./sample4.py -v
4.
test_a (__main__.Test) ... ok
test_b (__main__.Test) ... ok
test_c (__main__.Test) ... ok
test_d (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.005s

OK
%