Skip to content
BitVecDotProduct

BitVecDotProduct

Description

Calculate dot product of two bitvectors. Dot product is the count of positions where both bits are 1.

Parameters

Name Direction Description
bv1 in First bitvector
bv2 in Second bitvector

Usage example (from documentation)

  u64 dot_product = BitVecDotProduct(&bv1, &bv2);

Success

Returns the number of positions where both bits are 1. Bits beyond the shorter operand are treated as 0. Neither operand is modified.

Failure

Function cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
        // Dot product over 0/1 bit values is the count of positions where both
        // bits are 1, i.e. the intersection cardinality.
        u64 intersection = BitVecDotProduct(bv1, bv2);
    
        u64 max_length  = MAX2(bv1->length, bv2->length);
        ValidateBitVec(bv2);
    
        u64 dot_product = BitVecDotProduct(bv1, bv2);
        u64 ones1       = BitVecCountOnes(bv1);
        u64 ones2       = BitVecCountOnes(bv2);
    }
    
    u64 BitVecDotProduct(BitVec *bv1, BitVec *bv2) {
        ValidateBitVec(bv1);
        ValidateBitVec(bv2);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecDotProduct basic functionality\n");
    
        BitVec bv1    = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv2, true);
    
        u64 product = BitVecDotProduct(&bv1, &bv2);
        result      = result && (product == 2); // Positions 0 and 3
        BitVecPush(&bv2, true);
    
        product = BitVecDotProduct(&bv1, &bv2);
        result  = result && (product == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecDotProduct edge cases\n");
    
        BitVec bv1    = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test empty bitvectors
        u64 product = BitVecDotProduct(&bv1, &bv2);
        result      = result && (product == 0);
        BitVecPush(&bv2, false);
    
        product = BitVecDotProduct(&bv1, &bv2);
        result  = result && (product == 1); // Only first position counts
        double jaccard     = BitVecJaccardSimilarity(&bv1, &bv2);
        double cosine      = BitVecCosineSimilarity(&bv1, &bv2);
        u64    dot_prod    = BitVecDotProduct(&bv1, &bv2);
        double correlation = BitVecCorrelation(&bv1, &bv2);
        double entropy1    = BitVecEntropy(&bv1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecDotProduct(NULL, bv2) - should fatal\n");
        BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv2, true);
        BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv2, true);
        BitVecDotProduct(NULL, &bv2);
        BitVecDeinit(&bv2);
        DefaultAllocatorDeinit(&alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecDotProduct(bv1, NULL) - should fatal\n");
        BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv1, true);
        BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv1, true);
        BitVecDotProduct(&bv1, NULL);
        BitVecDeinit(&bv1);
        DefaultAllocatorDeinit(&alloc);
Last updated on