## 2020年6月7日日曜日

### 数学 - Pytyhon - 解析学 - ベクトル - ベクトルのノルム - 平方、和、平方根、2次元、3次元

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

2. $\sqrt{1+9}=\sqrt{10}$

3. $\sqrt{4+1+25}=\sqrt{30}$

4. $\sqrt{1+4+9}=\sqrt{14}$

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

6. $\sqrt{225+4+16}=\sqrt{245}=7\sqrt{5}$

1. $\sqrt{1+1}=\sqrt{2}$

2. $4$

3. $\sqrt{3}$

4. $\sqrt{1+9+16}=\sqrt{26}$

5. $\sqrt{4{\pi }^{2}+9+49}=\sqrt{4{\pi }^{2}+58}$

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

コード

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

print('1, 2.')

class TestNorm(TestCase):
def test_a(self):
As = [Matrix(t) for t in [(2, -1), (-1, 3), (2, -1, 5),
(-1, -2, 3), (pi, 3, -1), (15, -2, 4)]]
ns = [sqrt(5), sqrt(10), sqrt(30), sqrt(
14), sqrt(pi ** 2 + 10), 7 * sqrt(5)]
for i, (A, n) in enumerate(zip(As, ns)):
print(f'({chr(ord("a") + i)})')
self.assertEqual(A.norm(), n)

def test_b(self):
Bs = [Matrix(t) for t in [(-1, 1), (0, 4), (-1, 1, 1),
(-1, 3, -4), (2 * pi, -3, 7), (pi, 3, -1)]]
ns = [sqrt(2), 4, sqrt(3), sqrt(26), sqrt(4 * pi ** 2 + 58),
sqrt(pi ** 2 + 10)]
for i, (B, n) in enumerate(zip(Bs, ns)):
print(f'({chr(ord("a") + i)})')
self.assertEqual(B.norm(), n)

if __name__ == "__main__":
main()

% ./sample1.py -v
1, 2.
test_a (__main__.TestNorm) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
test_b (__main__.TestNorm) ... (a)
(b)
(c)
(d)
(e)
(f)
ok

----------------------------------------------------------------------
Ran 2 tests in 0.038s

OK
%