Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

la: add vector cosine similarity function and test #219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions la/vector.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module la

import math
import vsl.errors

// vector_apply sets this []T with the scaled components of another []T
// this := a * another ⇒ this[i] := a * another[i]
Expand Down Expand Up @@ -80,3 +81,20 @@ pub fn vector_largest[T](o []T, den T) T {
}
return largest / den
}

// vector_cosine_similarity calculates the cosine similarity between two vectors
pub fn vector_cosine_similarity(a []f64, b []f64) f64 {
if a.len != b.len {
errors.vsl_panic('Vectors must have the same length', .efailed)
}

dot_product := vector_dot(a, b)
norm_a := vector_norm(a)
norm_b := vector_norm(b)

if norm_a == 0 || norm_b == 0 {
return 0
}

return dot_product / (norm_a * norm_b)
}
20 changes: 20 additions & 0 deletions la/vector_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,23 @@ fn test_vector_largest() {
l := 2.0
assert float64.close(vector_largest(a, 2), l)
}

fn test_vector_cosine_similarity() {
a := [1.0, 2.0, 3.0]
b := [4.0, 5.0, 6.0]
c := [1.0, 0.0, 0.0]
d := [0.0, 1.0, 0.0]

// Cosine similarity of a and b (should be close to 0.974631846)
assert float64.tolerance(vector_cosine_similarity(a, b), 0.974631846, 1e-8)

// Cosine similarity of a with itself (should be exactly 1)
assert float64.close(vector_cosine_similarity(a, a), 1.0)

// Cosine similarity of perpendicular vectors (should be 0)
assert float64.close(vector_cosine_similarity(c, d), 0.0)

// Cosine similarity of a zero vector with another vector (should be 0)
zero_vector := [0.0, 0.0, 0.0]
assert float64.close(vector_cosine_similarity(zero_vector, a), 0.0)
}
Loading