gitee.com/quant1x/num@v0.3.2/internal/functions/distance.go (about) 1 package functions 2 3 import ( 4 "gitee.com/quant1x/num/internal/constraints" 5 "gitee.com/quant1x/num/math32" 6 "math" 7 ) 8 9 func Dot_Go[T constraints.Float](x, y []T) T { 10 res := T(0) 11 for i := 0; i < len(x); i++ { 12 res += x[i] * y[i] 13 } 14 return res 15 } 16 17 func Norm_Go_F64(x []float64) float64 { 18 res := float64(0) 19 for i := 0; i < len(x); i++ { 20 res += x[i] * x[i] 21 } 22 return math.Sqrt(res) 23 } 24 25 func Norm_Go_F32(x []float32) float32 { 26 res := float32(0) 27 for i := 0; i < len(x); i++ { 28 res += x[i] * x[i] 29 } 30 return math32.Sqrt(res) 31 } 32 33 func Distance_Go_F64(x, y []float64) float64 { 34 res := float64(0) 35 for i := 0; i < len(x); i++ { 36 res += (x[i] - y[i]) * (x[i] - y[i]) 37 } 38 return math.Sqrt(res) 39 } 40 41 func Distance_Go_F32(x, y []float32) float32 { 42 res := float32(0) 43 for i := 0; i < len(x); i++ { 44 res += (x[i] - y[i]) * (x[i] - y[i]) 45 } 46 return math32.Sqrt(res) 47 } 48 49 func ManhattanNorm_Go_F64(x []float64) float64 { 50 res := float64(0) 51 for i := 0; i < len(x); i++ { 52 res += math.Abs(x[i]) 53 } 54 return res 55 } 56 57 func ManhattanNorm_Go_F32(x []float32) float32 { 58 res := float32(0) 59 for i := 0; i < len(x); i++ { 60 res += math32.Abs(x[i]) 61 } 62 return res 63 } 64 65 func ManhattanDistance_Go_F64(x, y []float64) float64 { 66 res := float64(0) 67 for i := 0; i < len(x); i++ { 68 res += math.Abs(x[i] - y[i]) 69 } 70 return res 71 } 72 73 func ManhattanDistance_Go_F32(x, y []float32) float32 { 74 res := float32(0) 75 for i := 0; i < len(x); i++ { 76 res += math32.Abs(x[i] - y[i]) 77 } 78 return res 79 } 80 81 func CosineSimilarity_Go_F64(x, y []float64) float64 { 82 dp := float64(0) 83 xdp := float64(0) 84 ydp := float64(0) 85 for i := 0; i < len(x); i++ { 86 dp += x[i] * y[i] 87 xdp += x[i] * x[i] 88 ydp += y[i] * y[i] 89 } 90 return dp / math.Sqrt(xdp*ydp) 91 } 92 93 func CosineSimilarity_Go_F32(x, y []float32) float32 { 94 dp := float32(0) 95 xdp := float32(0) 96 ydp := float32(0) 97 for i := 0; i < len(x); i++ { 98 dp += x[i] * y[i] 99 xdp += x[i] * x[i] 100 ydp += y[i] * y[i] 101 } 102 return dp / math32.Sqrt(xdp*ydp) 103 }