LoginSignup
0
0

More than 5 years have passed since last update.

math > vector > 2つのベクトルの大きさ比較 > ノルムを使う > numpy.linalg.norm()

Last updated at Posted at 2018-05-13
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

やりたいこと

  • 2つのベクトルがある
  • ベクトルの大きさの比較を行う

ユークリッドノルムを使うことになるだろうか。
https://ja.wikipedia.org/wiki/%E3%83%8E%E3%83%AB%E3%83%A0

numpy.linalg.norm

試してみた。

test_vector_norm_180513.py
import numpy as np

avec = [1, 2, 3]
res = np.linalg.norm(avec)
print(res)

res = np.sqrt(1*1 + 2*2 + 3*3)
print(res)

run
$ python3 test_vector_norm_180513.py 
3.7416573867739413
3.7416573867739413

期待の式の結果と同じになった。

始点と終点の座標に対する計算

numpy arrayにて始点と終点の座標が与えられている場合、下記のような計算をする。

test_compare_vector_180513.py
import numpy as np

ray0 = np.array([0, 0, 1])  # start point
ray1 = np.array([1, 2, 4])  # end point
aray1 = [ray0, ray1]
ray2 = np.array([2, 4, 7])  # end point
aray2 = [ray0, ray2]

print(aray1)
wrk = aray1[1] - aray1[0]
res = np.linalg.norm(wrk)
print(res)

print(aray2)
wrk = aray2[1] - aray2[0]
res = np.linalg.norm(wrk)
print(res)

run
$ python3 test_compare_vector_180513.py 
[array([0, 0, 1]), array([1, 2, 4])]
3.7416573867739413
[array([0, 0, 1]), array([2, 4, 7])]
7.483314773547883
0
0
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
0