Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ public static float cosineSimilarity(@Nonnull List<Float> x, @Nonnull List<Float
throw new SKException("Vectors lengths must be equal");
}

float dotProduct = dot(x, y);
float normX = dot(x, x);
float normY = dot(y, y);
float dotProduct = 0.0F;
float normX = 0.0F;
float normY = 0.0F;

for (int i = 0; i < x.size(); i++) {
dotProduct += x.get(i) * y.get(i);
normX += x.get(i) * x.get(i);
normY += y.get(i) * y.get(i);
}

if (normX == 0 || normY == 0) {
throw new SKException("Vectors cannot have zero norm");
}

return dotProduct / (float) (Math.sqrt(normX) * Math.sqrt(normY));
return (dotProduct / (float) (Math.sqrt(normX) * Math.sqrt(normY)));
}

/**
Expand Down