## 2020年2月12日水曜日

### 数学 - Python - 図形と代数の交錯する世界 - 平面上のベクトル - ベクトルとその演算 - 内積を成分で表すこと - 射影、成分、最短距離

1. $\begin{array}{l}{t}_{0}=\frac{\stackrel{\to }{a}·\stackrel{\to }{b}}{\stackrel{\to }{b}·\stackrel{\to }{b}}\\ =\frac{6+4}{4+1}\\ =2\\ \left|\stackrel{\to }{a}-2\stackrel{\to }{b}\right|\\ =\left|\left(-1,2\right)\right|\\ =\sqrt{5}\end{array}$

2. $\begin{array}{l}{t}_{0}\\ =\frac{-1-3}{1+1}\\ =-2\\ \left|\left(-1,-1\right)\right|\\ =\sqrt{2}\end{array}$

3. $\begin{array}{l}{t}_{0}\\ =\frac{\stackrel{\to }{a}·\stackrel{\to }{b}}{\stackrel{\to }{b}·\stackrel{\to }{b}}\\ =\frac{4}{{\left|\stackrel{\to }{b}\right|}^{2}}\\ =\frac{4}{4}\\ =1\\ {\left|\stackrel{\to }{a}-\stackrel{\to }{b}\right|}^{2}\\ =\left(\stackrel{\to }{a}-\stackrel{\to }{b}\right)·\left(\stackrel{\to }{a}-\stackrel{\to }{b}\right)\\ ={\left|\stackrel{\to }{a}\right|}^{2}-2\stackrel{\to }{a}·\stackrel{\to }{b}+{\left|\stackrel{\to }{b}\right|}^{2}\\ =9-8+4\\ =5\\ \left|\stackrel{\to }{a}-\stackrel{\to }{b}\right|=\sqrt{5}\end{array}$

コード

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

print('20.')

class MyTestCase(TestCase):
def test1(self):
a = Matrix([3, 4])
b = Matrix([2, 1])
t = a.dot(b) / b.dot(b)
self.assertEqual(t, 2)
self.assertEqual((a - t * b).norm(), sqrt(5))

def test2(self):
a = Matrix([1, -3])
b = Matrix([-1, 1])
t = a.dot(b) / b.dot(b)
self.assertEqual(t, -2)
self.assertEqual((a - t * b).norm(), sqrt(2))

if __name__ == "__main__":
main()


% ./sample20.py -v
20.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.043s

OK
%