## 2019年12月25日水曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - スカラー積 - 内積

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

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

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

4. $\begin{array}{l}\left(-1,-2,3\right)·\left(-1,3,-4\right)\\ =1-6-12\\ =-17\end{array}$

5. $\begin{array}{l}\left(\pi ,3,-1\right)·\left(2\pi ,-3,7\right)\\ =2{\pi }^{2}-9-7\\ =2{\pi }^{2}-16\end{array}$

6. $\begin{array}{l}\left(15,-2,4\right)·\left(\pi ,3,-1\right)\\ =15\pi -6-4\\ =15\pi -10\end{array}$

コード

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

print('2.')

a = [(2, -1),
(-1, 3),
(2, -1, 5),
(-1, -2, 3),
(pi, 3, -1),
(15, -2, 4)]

b = [(-1, 1),
(0, 4),
(-1, 1, 1),
(-1, 3, -4),
(2 * pi, -3, 7),
(pi, 3, -1)]

class MyTestCase(TestCase):
def test(self):
spam = [-3, 12, 2, -17, 2 * pi ** 2 - 16, 15 * pi - 10]
for a0, b0, s in zip(a, b, spam):
A = Matrix(a0)
B = Matrix(b0)
self.assertEqual(A.dot(B), s)

if __name__ == '__main__':
main()


% ./sample2.py -v
2.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.040s

OK
%