Skip to content

Vector API

Utilities for manipulating vector data. Vectors can either be represented as dense (using array<numeric>) or sparse (using map<any_type, numeric>)

mismo.vector.norm(vec: T, *, metric: Literal['l1', 'l2'] = 'l2') -> ir.FloatingValue

Compute the norm (length) of a vector.

The vector can either be a dense vector, represented as array, or a sparse vector, represented as map.

PARAMETER DESCRIPTION
vec

The vector to compute the norm of.

TYPE: T

metric

The metric to use. "l1" for Manhattan distance, "l2" for Euclidean distance.

TYPE: ('l1', 'l2') DEFAULT: "l1"

RETURNS DESCRIPTION
The norm of the vector.

Examples:

>>> import ibis
>>> from mismo.vector import norm
>>> v = ibis.array([-3, 4])
>>> norm(v).execute()
np.float64(5.0)
>>> m = ibis.map({"a": -3, "b": 4})
>>> norm(m, metric="l1").execute()
np.float64(7.0)

mismo.vector.normalize(vec: T, *, metric: Literal['l1', 'l2'] = 'l2') -> T

Normalize a vector to have unit length.

The vector can either be a dense vector, represented as array, or a sparse vector, represented as map. The returned vector will have the same type as the input vector.

PARAMETER DESCRIPTION
vec

The vector to normalize.

TYPE: T

metric

The metric to use. "l1" for Manhattan distance, "l2" for Euclidean distance.

TYPE: ('l1', 'l2') DEFAULT: "l1"

RETURNS DESCRIPTION
ArrayValue

The normalized vector.

Examples:

>>> import ibis
>>> ibis.options.interactive = True
>>> from mismo.vector import normalize
>>> normalize(ibis.array([1, 2])).execute()
[0.4472135954999579, 0.8944271909999159]
>>> normalize(ibis.array([1, 2]), metric="l1").execute()
[0.3333333333333333, 0.6666666666666666]
>>> normalize(ibis.map({"a": 1, "b": 2})).execute()
{'a': 0.4472135954999579, 'b': 0.8944271909999159}

mismo.vector.dot(a: T, b: T) -> ir.FloatingValue

Compute the dot product of two vectors

The vectors can either be dense vectors, represented as array, or sparse vectors, represented as map. Both vectors must be of the same type though.

PARAMETER DESCRIPTION
a

The first vector.

TYPE: T

b

The second vector.

TYPE: T

RETURNS DESCRIPTION
FloatingValue

The dot product of the two vectors.

Examples:

>>> import ibis
>>> from mismo.vector import dot
>>> v1 = ibis.array([1, 2])
>>> v2 = ibis.array([4, 5])
>>> dot(v1, v2).execute()  # 1*4 + 2*5
np.float64(14.0)
>>> m1 = ibis.map({"a": 1, "b": 2})
>>> m2 = ibis.map({"b": 3, "c": 4})
>>> dot(m1, m2).execute() # 2*3
np.float64(6.0)

mismo.vector.cosine_similarity(a: T, b: T) -> ir.FloatingValue

Compute the cosine similarity of two vectors

The vectors can either be dense vectors, represented as array, or sparse vectors, represented as map. Both vectors must be of the same type though.

PARAMETER DESCRIPTION
a

The first vector.

TYPE: T

b

The second vector.

TYPE: T

RETURNS DESCRIPTION
FloatingValue

The cosine similarity of the two vectors.

Examples:

>>> import ibis
>>> from mismo.vector import cosine_similarity

Opposite directions:

>>> cosine_similarity(ibis.array([1, 1]), ibis.array([-2, -2])).execute()
-1.0

Orthogonal vectors:

>>> cosine_similarity(ibis.array([1, 0]), ibis.array([0, 1])).execute()
np.float64(0.0)

mismo.vector.mul(a: T, b: T) -> T

Element-wise multiplication of two vectors