github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/arguments/nearText/searcher_movements.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 nearText 13 14 import ( 15 "fmt" 16 ) 17 18 type movements struct{} 19 20 func newMovements() *movements { 21 return &movements{} 22 } 23 24 // MoveTo moves one vector toward another 25 func (v *movements) MoveTo(source []float32, target []float32, weight float32, 26 ) ([]float32, error) { 27 multiplier := float32(0.5) 28 29 if len(source) != len(target) { 30 return nil, fmt.Errorf("movement: vector lengths don't match: got %d and %d", 31 len(source), len(target)) 32 } 33 34 if weight < 0 || weight > 1 { 35 return nil, fmt.Errorf("movement: force must be between 0 and 1: got %f", 36 weight) 37 } 38 39 out := make([]float32, len(source)) 40 for i, sourceItem := range source { 41 out[i] = sourceItem*(1-weight*multiplier) + target[i]*(weight*multiplier) 42 } 43 44 return out, nil 45 } 46 47 // MoveAwayFrom moves one vector away from another 48 func (v *movements) MoveAwayFrom(source []float32, target []float32, weight float32, 49 ) ([]float32, error) { 50 multiplier := float32(0.5) // so the movement is fair in comparison with moveTo 51 if len(source) != len(target) { 52 return nil, fmt.Errorf("movement (moveAwayFrom): vector lengths don't match: "+ 53 "got %d and %d", len(source), len(target)) 54 } 55 56 if weight < 0 { 57 return nil, fmt.Errorf("movement (moveAwayFrom): force must be 0 or positive: "+ 58 "got %f", weight) 59 } 60 61 out := make([]float32, len(source)) 62 for i, sourceItem := range source { 63 out[i] = sourceItem + weight*multiplier*(sourceItem-target[i]) 64 } 65 66 return out, nil 67 }