BitVecJaccardSimilarity
Description
Calculate Jaccard similarity between two bitvectors. Jaccard similarity = |intersection| / |union|
Parameters
| Name | Direction | Description |
|---|---|---|
bv1 |
in | First bitvector |
bv2 |
in | Second bitvector |
Usage example (from documentation)
double similarity = BitVecJaccardSimilarity(&bv1, &bv2);Success
Returns the Jaccard similarity coefficient in [0.0, 1.0]. Neither operand is modified.
Failure
Returns 0.0 when both operands have no set bits (degenerate union); the caller cannot distinguish that from a true zero coefficient.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:1478:
}
double BitVecJaccardSimilarity(BitVec *bv1, BitVec *bv2) {
ValidateBitVec(bv1);
ValidateBitVec(bv2);- In
Math.c:157:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity basic functionality\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:171:
BitVecPush(&bv2, true);
double similarity = BitVecJaccardSimilarity(&bv1, &bv2);
result = result && (F64Abs(similarity - 1.0) < 0.001);- In
Math.c:182:
BitVecPush(&bv2, true);
similarity = BitVecJaccardSimilarity(&bv1, &bv2);
result = result && (F64Abs(similarity - 0.0) < 0.001);- In
Math.c:196:
// Intersection: 1, Union: 2, Jaccard = 1/2 = 0.5
similarity = BitVecJaccardSimilarity(&bv1, &bv2);
result = result && (F64Abs(similarity - 0.5) < 0.001);- In
Math.c:209:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity edge cases\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:216:
// Test empty bitvectors
double similarity = BitVecJaccardSimilarity(&bv1, &bv2);
result = result && (F64Abs(similarity - 1.0) < 0.001);- In
Math.c:225:
BitVecPush(&bv2, false);
similarity = BitVecJaccardSimilarity(&bv1, &bv2);
result = result && (F64Abs(similarity - 1.0) < 0.001);- In
Math.c:723:
// Test that all functions complete without crashing
u64 hamming = BitVecHammingDistance(&bv1, &bv2);
double jaccard = BitVecJaccardSimilarity(&bv1, &bv2);
double cosine = BitVecCosineSimilarity(&bv1, &bv2);
u64 dot_prod = BitVecDotProduct(&bv1, &bv2);- In
Math.c:791:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity(NULL, bv2) - should fatal\n");
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);- In
Math.c:794:
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);
BitVecJaccardSimilarity(NULL, &bv2);
BitVecDeinit(&bv2);
DefaultAllocatorDeinit(&alloc);- In
Math.c:803:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity(bv1, NULL) - should fatal\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);- In
Math.c:806:
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);
BitVecJaccardSimilarity(&bv1, NULL);
BitVecDeinit(&bv1);
DefaultAllocatorDeinit(&alloc);- In
Math.c:970:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity guard only fires when both empty\n");
BitVec empty = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:979:
// empty vs {1}: intersection 0, union 1 -> Jaccard 0.0 (kills 1427:41).
double s1 = BitVecJaccardSimilarity(&empty, &one);
result = result && (F64Abs(s1 - 0.0) < 0.001);- In
Math.c:983:
// {1} vs empty: same -> 0.0 (kills 1427:21).
double s2 = BitVecJaccardSimilarity(&one, &empty);
result = result && (F64Abs(s2 - 0.0) < 0.001);- In
Math.c:999:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity with bv1 shorter than bv2\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1009:
BitVecPush(&bv2, false);
double s = BitVecJaccardSimilarity(&bv1, &bv2);
bool result = (F64Abs(s - 1.0) < 0.001);- In
Math.c:1025:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity with bv2 shorter than bv1\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1035:
BitVecPush(&bv2, true);
double s = BitVecJaccardSimilarity(&bv1, &bv2);
bool result = (F64Abs(s - 1.0) < 0.001);- In
Math.c:1052:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity reads bv2 bits in valid region\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1063:
BitVecPush(&bv2, true);
double s = BitVecJaccardSimilarity(&bv1, &bv2);
bool result = (F64Abs(s - 0.0) < 0.001);- In
Math.c:1239:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecJaccardSimilarity rejects bad second operand\n");
BitVec empty = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1244:
BitVec bad = {0};
BitVecJaccardSimilarity(&empty, &bad);
BitVecDeinit(&empty);
Last updated on