My Coding > Programming language > Python > Python libraries and packages > Python NumPy

Python NumPy (Page: 6)

Go to Page:

  1. NumPy creating;
  2. NumPy array reshaping;
  3. Images with NumPy;
  4. NumPy copy;
  5. NumPy mask;
  6. Geometry;

Please note, that some of these operation are much easier to perform with SciPy module

NumPy is a powerfull tool for algebra and geomentry calculations. I will try to make a very small but usefull library for vector manipulations

Angle between two vectors

This function use formula cos(α) = a·b/|a|·|b| => α = arccos(a·b/|a|·|b|). In this format this function do not count the sign of the angle and give only absoluyte value.


import numpy as np

def unit_vector(vector):
    """ Calculate Unit Vector  """
    return vector / np.linalg.norm(vector)

def angle_between(v1, v2, radian = True):
    """ Angle between vectors is arrcos from scalar (dot) product
        of tweo unit vectors """
    v1_u = unit_vector(v1)
    v2_u = unit_vector(v2)
    ang = np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
    if radian: return ang
    return np.rad2deg(ang)

v1 = (1, 0, 0)
v2 = (0, 1, 0)

print(angle_between(v1, v2, False)) # 90.0

Rotate 2D vector

Rotation of 2D vector in the plane is very easy task. It is nesessary no make dot product of rotation matrix and our vector.

Rotation matrics is R=[[cosθ, -sinθ], [sinθ, cosθ]].


import numpy as np

def rotate_2d(vector, theta, radian = True):
    """Rotate vector by given angle"""
    if not radian: theta = np.deg2rad(theta)
    c, s = np.cos(theta), np.sin(theta)
    rot = np.array([[c, -s],
                    [s,  c]
                  ])
    return np.dot(rot, vector)

print(rotate_2d([1, 0], -45, False)) # [ 0.70710678 -0.70710678]

Rotate 3D vector

Find the rotation between two vectors

How to find the rotation matrix between 2 vectors in 3D space.

r = I + k + np.square(k) * ((1 -c)/(s**2)), np.square(k) squares each element of the matrix. You want np.matmul(k,k) or k @ k which is the matrix multiplied by itself.


def rotation_matrix_from_vectors(v1, v2):
    """ Find the rotation matrix that aligns 3D v1 to v2
    :return mat: A transform matrix (3x3) which when applied to v1, aligns it with v2.
    """
    a = v1 / np.linalg.norm(v1)
    b = v2 / np.linalg.norm(v2)
    v = np.cross(a, b)
    if not any(v): return np.eye(3) #vectors are parallel
    c = np.dot(a, b)
    s = np.linalg.norm(v)
    kmat = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]])
    rotation_matrix = np.eye(3) + kmat + kmat.dot(kmat) * ((1 - c) / (s ** 2))
    return rotation_matrix

# TESTS
vec1 = [1, 0, 0]
vec2 = [0, 2, 0]

mat = rotation_matrix_from_vectors(vec1, vec2)
vec1_rot = mat.dot(vec1)
print( mat )    # [[ 0. -1.  0.]  [ 1.  0.  0.]  [ 0.  0.  1.]] 
print(vec1_rot) # [0. 1. 0.]

How to rotate 3D vector agains another vector

Go to Page: 1; 2; 3; 4; 5; 6;


Published: 2021-10-04 11:48:19
Updated: 2021-11-14 08:41:55

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.