Skip to content

Commit 62d3941

Browse files
committed
Validate the length of the vectors first.
1 parent 2d6d2cc commit 62d3941

File tree

5 files changed

+53
-714
lines changed

5 files changed

+53
-714
lines changed

src/main/java/org/tinystruct/data/tools/CosineSimilarity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static double[] dotProductAndMagnitude(double[] vectorA, double[] vectorB
3434
* @return the cosine similarity between the two vectors, or 0.0 if one of the vectors has zero magnitude
3535
*/
3636
public static double cosineSimilarity(double[] vectorA, double[] vectorB) {
37+
if (vectorA.length != vectorB.length) {
38+
throw new IllegalArgumentException("Vectors must have the same dimension");
39+
}
3740
double[] values = dotProductAndMagnitude(vectorA, vectorB);
3841
double dotProduct = values[0];
3942
double magnitudeA = values[1];

0 commit comments

Comments
 (0)