News for hackers. Stuff that matters.
Re:Embeddings: What they are and why they matter by oleganza on Wednesday October 25, 2023 @06:18AM (#38011238)
Common mistake when doing practical trigonometry is to compute square roots when that's not necessary:

def cosine_similarity(a, b):

    dot_product = sum(x \* y for x, y in zip(a, b))

    magnitude_a = sum(x \* x for x in a) \* 0.5         # <- no need for \*0.5

    magnitude_b = sum(x \* x for x in b) \* 0.5         # <- no need for \*0.5

    return dot_product / (magnitude_a \* magnitude_b)
If you compare your cosines, you might as well compare their squares and avoid costly root computation.

Similarly, in elliptic curve crypto certain expensive operations, such as inversion (x^-1 mod n) are delayed as much as possible down the computation pipeline or even avoided completely when you need to compare two points instead of computing their canonical values.

Re:Embeddings: What they are and why they matter by dist-epoch on Wednesday October 25, 2023 @06:54AM (#38011445)
This code is written to be easy to understand. Otherwise you would replace it with some low-level SIMD code.