## 2018年10月31日水曜日

### 数学 - Python - 線形代数学 - R^n におけるベクトル – ベクトルのノルム(射影)

ラング線形代数学(上)(S.ラング (著)、芹沢 正三 (翻訳)、ちくま学芸文庫)の1章(R^n におけるベクトル)、3(ベクトルのノルム)、練習問題3.を取り組んでみる。

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

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

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

4. $\frac{1-6-12}{1+9+16}\left(-1,3,-4\right)=\frac{17}{26}\left(1,-3,4\right)$

5. $\frac{2{\pi }^{2}-9-7}{4{\pi }^{2}+9+27}\left(2\pi ,-3,7\right)=\frac{2{\pi }^{2}-16}{4{\pi }^{2}+36}\left(2\pi ,-3,7\right)=\frac{{\pi }^{2}-8}{2{\pi }^{2}+18}\left(2\pi ,-3-7\right)$

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

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, Matrix, pi

print('3.')

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

for i, (a, b) in enumerate(zip(As, Bs), 1):
print(f'(i)')
A = Matrix(a)
B = Matrix(b)
pprint((A.dot(B) / B.dot(B) * B).T)
print()


コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, Matrix, pi

print('2.')

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

for i, (a, b) in enumerate(zip(As, Bs), 1):
print(f'({i})')
A = Matrix(a)
B = Matrix(b)
pprint((A.dot(B) / B.dot(B) * B).T)
print()


$./sample3.py 2. (1) [3/2 -3/2] (2) [0 3] (3) [-2/3 2/3 2/3] (4) ⎡17 -51 34⎤ ⎢── ──── ──⎥ ⎣26 26 13⎦ (5) ⎡ ⎛ 2⎞ ⎛ 2⎞ ⎛ 2⎞⎤ ⎢2⋅π⋅⎝-16 + 2⋅π ⎠ -3⋅⎝-16 + 2⋅π ⎠ 7⋅⎝-16 + 2⋅π ⎠⎥ ⎢──────────────── ──────────────── ──────────────⎥ ⎢ 2 2 2 ⎥ ⎣ 4⋅π + 58 4⋅π + 58 4⋅π + 58 ⎦ (6) ⎡π⋅(-10 + 15⋅π) 3⋅(-10 + 15⋅π) -(-10 + 15⋅π) ⎤ ⎢────────────── ────────────── ──────────────⎥ ⎢ 2 2 2 ⎥ ⎣ π + 10 π + 10 π + 10 ⎦$