[python] vector operation

Norm

$ \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

inner product

$ \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 $ is the angle between the vector $ \ mathbf {a} $ and the vector $ \ 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

Cross product

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

$ \ theta $ is the angle between the vector $ \ mathbf {a} $ and the vector $ \ 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]

Recommended Posts

[python] vector operation
Python OS operation
[Python] Matrix operation
[Python] Operation of enumerate
Python directory operation summary
Python logical operation stumbling
Python decorator operation memo
[python] Array slice operation
Python
S3 operation with python boto3
Python: Natural language vector representation
Python pywin32 (win32com) Excel operation memorandum
[Python] Operation memo of pandas DataFrame
[python] week1-3: Number type and operation
Vector format operation & conversion with arcpy
Basic Python operation 2nd: Function (argument)
[Automation with python! ] Part 2: File operation
[Python] File operation using if statement
kafka python
Mouse operation using Windows API in Python
Python basic operation 1st: List comprehension notation
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
Python memo
ufo-> python (3)
Python + Selenium Frequently used operation method summary
Python comprehension
install python
Python Singleton
Bit operation
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary