0
1

More than 3 years have passed since last update.

[Python]ベクトル演算

Last updated at Posted at 2020-04-18

ノルム

$ \left\Vert \mathbf{a} \right\Vert_2 = \sqrt{\sum_{i=1}^n a_i^2} = \sqrt{a_1^2 + a_2^2 + a_3^3 + \cdots + a_n^2} $

import numpy as np
a = np.array([1, 2, 3])
n = np.linalg.norm(a)
print('norm=',n)

norm= 3.7416573867739413

内積

$ \mathbf{a} \cdot \mathbf{b}= \sum_{i=1}^{n} a_i b_i = a_1 b_1 + a_2 b_2 + a_3 b_3 + \cdots + a_n b_n $

$ \mathbf{a} \cdot \mathbf{b}= \left\Vert\mathbf{a}\right\Vert\left\Vert\mathbf{b}\right\Vert\cos (\theta) $

$\theta$はベクトル$\mathbf{a}$とベクトル$\mathbf{b}$の間の角度

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
p = np.dot(a,b)
print('dot product=',p)

dot product= 32

外積

$ \mathbf{a} \times \mathbf{b} =\left\Vert\mathbf{a}\right\Vert\left\Vert\mathbf{b}\right\Vert\sin (\theta) $

$\theta$はベクトル$\mathbf{a}$とベクトル$\mathbf{b}$の間の角度

$ \begin{pmatrix}a_1\ a_2\ a_3 \end{pmatrix} \times \begin{pmatrix}b_1\ b_2\ b_3\end{pmatrix} = \begin{pmatrix}a_2b_3-a_3b_2\ a_3b_1-a_1b_3\ a_1b_2 - a_2b_1 \end{pmatrix} $

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
p = np.cross(a,b)
print('cross product=',p)

cross product= [-3  6 -3]
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1