## 2020年6月2日火曜日

### 数学 - Pytyhon - 解析学 - ベクトル - スカラー積 - 2次元、3次元

1. $\begin{array}{l}A·A\\ =\left(2,-1\right)·\left(2,-1\right)\\ =4+1\\ =5\end{array}$

2. $\begin{array}{l}A·A\\ =1+9\\ =10\end{array}$

3. $4+1+25=30$

4. $1+4+9=14$

5. ${\pi }^{2}+9+1={\pi }^{2}+10$

6. $225+4+16=245$

1. $\begin{array}{l}A·B\\ =\left(2,-1\right)·\left(-1,1\right)\\ =-2-1\\ =-3\end{array}$

2. $12$

3. $-2-1+5=2$

4. $1-6-12=-17$

5. $2{\pi }^{2}-9-7=2{\pi }^{2}-16$

6. $15\pi -6-4=15\pi -10$

コード

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

print('1, 2.')

As = [Matrix(t) for t in [(2, -1), (-1, 3), (2, -1, 5),
(-1, -2, 3), (pi, 3, -1), (15, -2, 4)]]
Bs = [Matrix(t) for t in [(-1, 1), (0, 4), (-1, 1, 1),
(-1, 3, -4), (2 * pi, -3, 7), (pi, 3, -1)]]

class TestScalarProduct(TestCase):
def test1(self):
ns = [5, 10, 30, 14, pi ** 2 + 10, 245]
for i, (A, n) in enumerate(zip(As, ns)):
print(f'({chr(ord("a") + i)})')
self.assertEqual(A.dot(A), n)

def test2(self):
ns = [-3, 12, 2, -17, 2 * pi ** 2 - 16, 15 * pi - 10]
for i, (A, B, n) in enumerate(zip(As, Bs, ns)):
print(f'({chr(ord("a") + i)})')
self.assertEqual(A.dot(B), n)

if __name__ == "__main__":
main()


% ./sample1.py -v
1, 2.
test1 (__main__.TestScalarProduct) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
test2 (__main__.TestScalarProduct) ... (a)
(b)
(c)
(d)
(e)
(f)
ok

----------------------------------------------------------------------
Ran 2 tests in 0.037s

OK
%