## 2020年6月8日月曜日

### 数学 - Pytyhon - 解析学 - ベクトル - ベクトルのノルム - 射影、ノルム、内積、スカラー倍、2次元、3次元

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

2. $\frac{12}{16}\left(0,4\right)=\left(0,3\right)$

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

4. $\begin{array}{l}\frac{1-6-12}{1+9+16}\left(-1,3,-4\right)\\ =-\frac{17}{26}\left(-1,3,-4\right)\\ =\left(\frac{17}{26},-\frac{51}{26},\frac{34}{13}\right)\end{array}$

5. $\begin{array}{l}\frac{2{\pi }^{2}-9-7}{4{\pi }^{2}+9+49}\left(2\pi ,-3,7\right)\\ =\frac{2{\pi }^{2}-16}{4{\pi }^{2}+58}\left(2\pi ,-3,7\right)\\ =\frac{{\pi }^{2}-8}{2{\pi }^{2}+29}\left(2\pi ,-3,7\right)\\ =\left(\frac{2{\pi }^{3}-16\pi }{2{\pi }^{2}+29},\frac{-3{\pi }^{2}+24}{2{\pi }^{2}+29},\frac{7{\pi }^{2}-56}{2{\pi }^{2}+29}\right)\end{array}$

6. $\frac{15\pi -10}{{\pi }^{2}+10}\left(\pi ,3,-1\right)=\left(\frac{15{\pi }^{2}-10\pi }{{\pi }^{2}+10},\frac{45\pi -30}{{\pi }^{2}+10},\frac{-15\pi +10}{{\pi }^{2}+10}\right)$

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

2. $\frac{12}{10}\left(-1,3\right)=\left(-\frac{6}{5},\frac{18}{5}\right)$

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

4. $-\frac{17}{14}\left(-1,-2,3\right)=\left(\frac{17}{14},\frac{17}{7},-\frac{51}{14}\right)$

5. $\frac{2{\pi }^{2}-16}{{\pi }^{2}+10}\left(\pi ,3,-1\right)=\left(\frac{2{\pi }^{3}-16\pi }{{\pi }^{2}+10},\frac{6{\pi }^{2}-48}{{\pi }^{2}+10},\frac{-2{\pi }^{2}+16}{{\pi }^{2}+10}\right)$

6. $\begin{array}{l}\frac{15\pi -10}{225+4+16}\left(15,-2,4\right)\\ =\frac{3\pi -2}{49}\left(15,-2,4\right)\\ =\left(\frac{45\pi -30}{49},\frac{-6\pi +4}{49},\frac{12\pi -8}{49}\right)\end{array}$

コード

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

print('3, 4.')

def eq(a, b):
for a0, b0 in zip(a, b):
if a0.simplify() != b0.simplify():
return False
return True

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 TestProjection(TestCase):
def test_b(self):
projs = [Matrix(t)
for t in [(Rational(3, 2),
-Rational(3, 2)),
(0, 3),
(-Rational(2, 3), Rational(2, 3), Rational(2, 3)),
(Rational(17, 26), -Rational(51, 26), Rational(34, 13)),
((2 * pi ** 3 - 16 * pi) / (2 * pi ** 2 + 29),
(-3 * pi ** 2 + 24) / (2 * pi ** 2 + 29),
(7 * pi ** 2 - 56) / (2 * pi ** 2 + 29)),
((15 * pi ** 2 - 10 * pi) / (pi ** 2 + 10),
(45 * pi - 30) / (pi ** 2 + 10),
(-15 * pi + 10) / (pi ** 2 + 10))]]
for i, (A, B, proj) in enumerate(zip(As, Bs, projs)):
print(f'({chr(ord("a") + i)})')
self.assertTrue(eq(A.dot(B) / B.dot(B) * B, proj))

def test_a(self):
projs = [Matrix(t) for t in [
(-Rational(6, 5), Rational(3, 5)),
(-Rational(6, 5), Rational(18, 5)),
(Rational(2, 15), -Rational(1, 15), Rational(1, 3)),
(Rational(17, 14), Rational(17, 7), -Rational(51, 14)),
((2 * pi ** 3 - 16 * pi) / (pi ** 2 + 10),
(6 * pi ** 2 - 48) / (pi ** 2 + 10),
(-2 * pi ** 2 + 16) / (pi ** 2 + 10)),
((45 * pi - 30) / 49, (-6 * pi + 4) / 49, (12 * pi - 8) / 49)]]
for i, (A, B, proj) in enumerate(zip(As, Bs, projs)):
print(f'({chr(ord("a") + i)})')
self.assertTrue(eq(B.dot(A) / A.dot(A) * A, proj))

if __name__ == "__main__":
main()


% ./sample3.py -v
3, 4.
test_a (__main__.TestProjection) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
test_b (__main__.TestProjection) ... (a)
(b)
(c)
(d)
(e)
(f)
ok

----------------------------------------------------------------------
Ran 2 tests in 1.044s

OK
%