VectorsHelper Module

Numeric vector helpers built on NumPy.

class HMB.VectorsHelper.VectorsHelper[source]

Bases: object

Common vector operations built on NumPy.

- Length(vector)

Euclidean (L2) norm of a vector.

- DotProduct(vector1, vector2)

Standard dot product.

- CrossProduct(vector1, vector2)

Cross product for 3D vectors.

- Distance(vector1, vector2)

Euclidean distance between two vectors.

- Angle(vector1, vector2, mode="rad")

Angle between vectors (radians or degrees).

- ChangeBasis(v, *args)

Project vector v onto provided basis vectors.

Length(vector)[source]

Compute the Euclidean (L2) length/norm of a vector.

\[\|v\|_2 = \sqrt{\sum_i v_i^2}\]
Parameters:

vector (array-like) – Input vector (list, tuple or NumPy array).

Returns:

L2 norm. Works for 1D vectors or arrays.

Return type:

float or numpy.ndarray

DotProduct(vector1, vector2)[source]

Compute the dot product between two vectors.

\[\langle a, b \rangle = \sum_i a_i b_i\]
Parameters:
  • vector1 (array-like) – Input vectors (same shape required for dot).

  • vector2 (array-like) – Input vectors (same shape required for dot).

Returns:

Dot product result.

Return type:

scalar or numpy.ndarray

CrossProduct(vector1, vector2)[source]

Compute the cross product (only meaningful for 3-dimensional vectors).

\[a \times b = [a_2 b_3 - a_3 b_2,\; a_3 b_1 - a_1 b_3,\; a_1 b_2 - a_2 b_1]\]
Parameters:
  • vector1 (array-like) – 3-element vectors.

  • vector2 (array-like) – 3-element vectors.

Returns:

Cross product vector.

Return type:

numpy.ndarray

Distance(vector1, vector2)[source]

Compute Euclidean distance between two vectors.

\[d(a, b) = \|a - b\|_2 = \sqrt{\sum_i (a_i - b_i)^2}\]
Parameters:
  • vector1 (array-like) – Input vectors.

  • vector2 (array-like) – Input vectors.

Returns:

Euclidean distance.

Return type:

float or numpy.ndarray

Angle(vector1, vector2, mode='rad')[source]

Compute the angle between two vectors using the arccosine of the normalized dot product.

\[\theta(a,b) = \arccos\left(\frac{\langle a, b \rangle}{\|a\|_2 \; \|b\|_2}\right)\]
Parameters:
  • vector1 (array-like) – Input vectors.

  • vector2 (array-like) – Input vectors.

  • mode (str) – “rad” for radians (default) or “deg” for degrees.

Returns:

Angle in radians or degrees depending on mode.

Return type:

float or numpy.ndarray

Note

No clipping is performed on the cosine input; numerical round-off may cause values slightly outside [-1, 1] which can produce NaNs. Consumers may want to clip the argument.

ChangeBasis(v, *args)[source]

Project vector v onto each of the provided basis vectors.

Parameters:
  • v (array-like) – Vector to project.

  • *args (array-like) – Basis vectors to project onto.

Returns:

List of projection coefficients (one per provided basis vector).

Return type:

list

ProjectVector(v, basis)[source]

Project vector v onto the provided basis vectors.

\[c_i = \frac{\langle v, b_i \rangle}{\langle b_i, b_i \rangle} \quad\text{for each basis vector } b_i\]
Parameters:
  • v (array-like) – Vector to project.

  • basis (list of array-like) – Basis vectors to project onto.

Returns:

List of projection coefficients (one per provided basis vector).

Return type:

list

CosineSimilarity(vector1, vector2)[source]

Compute the cosine similarity between two vectors.

\[\text{cosine}(a, b) = \frac{\langle a, b\rangle}{\|a\|_2 \; \|b\|_2}\]
Parameters:
  • vector1 (array-like) – First input vector.

  • vector2 (array-like) – Second input vector.

Returns:

Cosine similarity value in the range [-1, 1].

Return type:

float

NormalizeVector(vector)[source]

Normalize the input vector to have unit length.

Parameters:

vector (array-like) – Input vector to normalize.

Returns:

Normalized vector with unit length. If input is zero vector, returns zero vector of the same shape.

Return type:

numpy.ndarray