Skip to content

Math

Math functions for the AtomicEmbeddings package.

cosine_distance(a, b)

Cosine distance of two vectors.

Source code in src/elementembeddings/utils/math.py
def cosine_distance(
    a: list[int | float],
    b: list[int | float],
) -> int | float:
    """Cosine distance of two vectors."""
    return 1 - cosine_similarity(a, b)

cosine_similarity(a, b)

Cosine similarity of two vectors.

Source code in src/elementembeddings/utils/math.py
def cosine_similarity(
    a: list[int | float],
    b: list[int | float],
) -> int | float:
    """Cosine similarity of two vectors."""
    return dot(a, b) / ((dot(a, a) ** 0.5) * (dot(b, b) ** 0.5))

dot(a, b)

Dot product of two vectors.

Source code in src/elementembeddings/utils/math.py
def dot(a: list[int | float], b: list[int | float]) -> int | float:
    """Dot product of two vectors."""
    return sum(map(operator.mul, a, b))