narupatools.physics.vector

Utility methods for using vectors.

Functions

angle

Calculate the angle in radians between two vectors \(a\) and \(b\).

cross_product

Calculate the cross product of two 3-dimensional vectors \(a\) and \(b\).

cross_product_matrix

Calculate the matrix F that represents the left cross product with \(a\).

distance

Calculate the distance \(d\) between two points \(a\) and \(b\).

dot_product

Calculate the dot product of two N-dimensional vectors \(a\) and \(b\).

left_vector_triple_product_matrix

Calculate the matrix form of the left cross product with \(a\) and \(b\).

magnitude

Get the magnitude of a n-dimensional vector.

normalized

Normalize an n-dimensional vector.

outer_product

Calculate the outer product of two vectors \(a\) and \(b\).

right_cross_product_matrix

Calculate the matrix that represents the right cross product with \(a\).

sqr_distance

Calculate the square distance between two points \(a\) and \(b\).

sqr_magnitude

Get the square magnitude of a n-dimensional vector.

vector

Create a vector from a set of coordinates.

vector_projection

Calculate the vector projection \(a_1\) of \(a\) onto \(b\).

vector_rejection

Calculate the vector rejection \(a_2\) of \(a\) from :math`b`.

zero_vector

Zero vector in three dimensions.

narupatools.physics.vector.angle(vector1: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], vector2: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Calculate the angle in radians between two vectors \(a\) and \(b\).

This uses the relation:

\[a \cdot b = |a| |b| \cos \theta\]
Parameters
  • vector1 – Vector \(a\).

  • vector2 – Vector \(b\).

Raises

ValueError – One of the vectors is zero.

Returns

Angle between the two vectors in radians.

narupatools.physics.vector.cross_product(a: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], b: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the cross product of two 3-dimensional vectors \(a\) and \(b\).

This is defined as:

\[\begin{split}a \times b = \begin{vmatrix} \hat i & \hat j & \hat k \\ a_x & a_y & a_z \\ b_x & b_y & b_z \end{vmatrix}\end{split}\]
Parameters
  • a – Vector \(a\).

  • b – Vector \(b\).

Returns

Cross product \(a \times b\) of the vectors \(a\) and \(b\).

narupatools.physics.vector.cross_product_matrix(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the matrix F that represents the left cross product with \(a\).

This matrix fulfills for all vectors \(v\) the identity:

\[F v = a \times v\]

This converts taking the cross product on the left by \(a\) to a matrix multiplication.

Parameters

vector – Vector \(a\).

Returns

Skew-symmetric matrix \(F\) that satisfies \(F v = a \times v\) for all \(v\).

narupatools.physics.vector.distance(vector1: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], vector2: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Calculate the distance \(d\) between two points \(a\) and \(b\).

Parameters
  • vector1 – Point \(a\).

  • vector2 – Point \(b\).

Returns

Distance between the two points.

narupatools.physics.vector.dot_product(a: numpy.ndarray[Any, numpy.dtype[numpy.float64]], b: numpy.ndarray[Any, numpy.dtype[numpy.float64]], /)float

Calculate the dot product of two N-dimensional vectors \(a\) and \(b\).

This is defined as:

\[a \cdot b = \sum_i a_i b_i\]
Parameters
  • a – Vector \(a\).

  • b – Vector \(b\).

Returns

Dot product \(a \cdot b\) of the vectors \(a\) and \(b\).

narupatools.physics.vector.left_vector_triple_product_matrix(a: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], b: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the matrix form of the left cross product with \(a\) and \(b\).

This matrix fulfills for all vectors \(v\) the identity:

\[F v = a \times (b \times v)\]

This converts taking the cross product on the left by \(b\) and then by \(a\) to a matrix multiplication.

Parameters
  • a – Vector \(a\).

  • b – Vector \(b\).

narupatools.physics.vector.magnitude(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Get the magnitude of a n-dimensional vector.

narupatools.physics.vector.normalized(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Normalize an n-dimensional vector.

narupatools.physics.vector.outer_product(a: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], b: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the outer product of two vectors \(a\) and \(b\).

This is a matrix defined as:

\[\begin{split}A = \begin{pmatrix} a_1 b_1 & \cdots & a_1 b_3 \\ \vdots & \ddots & \vdots \\ a_3 b_1 & \cdots & a_3 b_3 \end{pmatrix}\end{split}\]
Parameters
  • a – Vector \(a\).

  • b – Vector \(b\).

Returns

Matrix formed by the outer product of \(a\) and \(b\).

narupatools.physics.vector.right_cross_product_matrix(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the matrix that represents the right cross product with \(a\).

This matrix fulfills for all vectors \(v\) the identity:

\[F v = v \times a\]

This converts taking the cross product on the right by \(a\) to a matrix multiplication.

Parameters

vector – Vector \(a\).

Returns

Skew-symmetric matrix \(F\) that satisfies \(F v = v \times a\) for all \(v\).

narupatools.physics.vector.sqr_distance(point1: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], point2: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Calculate the square distance between two points \(a\) and \(b\).

Parameters
  • point1 – Point \(a\).

  • point2 – Point \(b\).

Returns

Square distance between the two points.

narupatools.physics.vector.sqr_magnitude(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)float

Get the square magnitude of a n-dimensional vector.

narupatools.physics.vector.vector(*args: Union[float, int])numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Create a vector from a set of coordinates.

This creates a NumPy array from the arguments with its dtype set to float. This is intended as a shorthand for creating vectors which are guaranteed to be floats and not integers.

Parameters

args – Components of the vector.

narupatools.physics.vector.vector_projection(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], onto: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the vector projection \(a_1\) of \(a\) onto \(b\).

This is given by:

\[a_1 = \frac{a \cdot b}{b \cdot b} b\]

The vector projection gives a vector in the same direction as b.

This is also known as the vector component or vector resolution of \(a\) in the direction of \(b\).

Parameters
  • vector – Vector \(a\) to project.

  • onto – Vector \(b\) to project onto.

Returns

Vector projection \(a_1\) of vector \(a\) onto vector \(b\).

narupatools.physics.vector.vector_rejection(vector: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], onto: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the vector rejection \(a_2\) of \(a\) from :math`b`.

This is given by:

\[a_2 = a - a_1 = a - \frac{a \cdot b}{b \cdot b} b\]

where \(a_1\) is the vector projection of \(a\) onto \(b\).

The vector rejection is the component of \(a\) which is perpendicular to \(b\).

Parameters
  • vector – Vector \(a\) to reject.

  • onto – Vector \(b\) to reject from.

Returns

Vector rejection \(a_2\) of vector \(a\) from vector \(b\).

narupatools.physics.vector.zero_vector()numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Zero vector in three dimensions.