2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
distance.go 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-06-03 06:07 . 调整vek目录为num
package functions
import (
"github.com/chewxy/math32"
"golang.org/x/exp/constraints"
"math"
)
func Dot_Go[T constraints.Float](x, y []T) T {
res := T(0)
for i := 0; i < len(x); i++ {
res += x[i] * y[i]
}
return res
}
func Norm_Go_F64(x []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += x[i] * x[i]
}
return math.Sqrt(res)
}
func Norm_Go_F32(x []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += x[i] * x[i]
}
return math32.Sqrt(res)
}
func Distance_Go_F64(x, y []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += (x[i] - y[i]) * (x[i] - y[i])
}
return math.Sqrt(res)
}
func Distance_Go_F32(x, y []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += (x[i] - y[i]) * (x[i] - y[i])
}
return math32.Sqrt(res)
}
func ManhattanNorm_Go_F64(x []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += math.Abs(x[i])
}
return res
}
func ManhattanNorm_Go_F32(x []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += math32.Abs(x[i])
}
return res
}
func ManhattanDistance_Go_F64(x, y []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += math.Abs(x[i] - y[i])
}
return res
}
func ManhattanDistance_Go_F32(x, y []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += math32.Abs(x[i] - y[i])
}
return res
}
func CosineSimilarity_Go_F64(x, y []float64) float64 {
dp := float64(0)
xdp := float64(0)
ydp := float64(0)
for i := 0; i < len(x); i++ {
dp += x[i] * y[i]
xdp += x[i] * x[i]
ydp += y[i] * y[i]
}
return dp / math.Sqrt(xdp*ydp)
}
func CosineSimilarity_Go_F32(x, y []float32) float32 {
dp := float32(0)
xdp := float32(0)
ydp := float32(0)
for i := 0; i < len(x); i++ {
dp += x[i] * y[i]
xdp += x[i] * x[i]
ydp += y[i] * y[i]
}
return dp / math32.Sqrt(xdp*ydp)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.7.5

搜索帮助