You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ju...@apache.org on 2022/04/08 16:36:42 UTC
[lucene] branch main updated: Fix rare failures in TestVectorUtil cosine tests
This is an automated email from the ASF dual-hosted git repository.
julietibs pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new ab1394e8407 Fix rare failures in TestVectorUtil cosine tests
ab1394e8407 is described below
commit ab1394e84071bf06753b26ab29d6f941b1b9f6eb
Author: Julie Tibshirani <ju...@apache.org>
AuthorDate: Fri Apr 8 09:26:15 2022 -0700
Fix rare failures in TestVectorUtil cosine tests
If one of the vectors is zero, the cosine is not defined. This change makes sure
the test vectors are non-zero.
---
lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
index 54ed53efdb1..230e187f3b2 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
@@ -80,6 +80,8 @@ public class TestVectorUtil extends LuceneTestCase {
public void testSelfCosine() {
// the dot product of a vector with itself is always equal to 1
float[] v = randomVector();
+ // ensure the vector is non-zero so that cosine is defined
+ v[0] = random().nextFloat() + 0.01f;
assertEquals(1.0f, VectorUtil.cosine(v, v), DELTA);
}
@@ -87,7 +89,8 @@ public class TestVectorUtil extends LuceneTestCase {
// the cosine of two perpendicular vectors is 0
float[] v = new float[2];
v[0] = random().nextInt(100);
- v[1] = random().nextInt(100);
+ // ensure the vector is non-zero so that cosine is defined
+ v[1] = random().nextInt(1, 100);
float[] u = new float[2];
u[0] = v[1];
u[1] = -v[0];