github.com/weaviate/weaviate@v1.24.6/usecases/vectorizer/combine.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package vectorizer
    13  
    14  // CombineVectors combines all of the vector into sum of their parts
    15  func CombineVectors(vectors [][]float32) []float32 {
    16  	return CombineVectorsWithWeights(vectors, nil)
    17  }
    18  
    19  func CombineVectorsWithWeights(vectors [][]float32, weights []float32) []float32 {
    20  	maxVectorLength := 0
    21  	for i := range vectors {
    22  		if len(vectors[i]) > maxVectorLength {
    23  			maxVectorLength = len(vectors[i])
    24  		}
    25  	}
    26  	sums := make([]float32, maxVectorLength)
    27  	dividers := make([]int32, maxVectorLength)
    28  	for indx, vector := range vectors {
    29  		for i := 0; i < len(vector); i++ {
    30  			if weights != nil {
    31  				// apply weight to vector value
    32  				sums[i] += vector[i] * weights[indx]
    33  			} else {
    34  				sums[i] += vector[i]
    35  			}
    36  			dividers[i]++
    37  		}
    38  	}
    39  	for i := 0; i < len(sums); i++ {
    40  		sums[i] /= float32(dividers[i])
    41  	}
    42  
    43  	return sums
    44  }