photoprism/pkg/vector/centroid.go
Michael Mayer 03129c9129 Vector: Reorganize package into topic-based files #4669
Split the catch-all values.go into one file per concept, each mirrored
by its test: distance.go, norm.go, stats.go, product.go, centroid.go,
plus the mean methods folded into mean.go and Copy/Dim/Sum into
vector.go. Remove values.go, values_test.go, and values_more_test.go so
functionality and tests live where developers expect them.

Hoist the two 512-dimensional face embeddings shared by the distance,
norm, and cosine tests into fixtures_test.go, removing the previous
triplication, and decompose the monolithic TestVector into per-concept
tests. Close pre-existing coverage gaps in the integer converters and
the GeometricMean/HarmonicMean method wrappers, bringing the package to
100% statement coverage. Pure code movement; no behavior change.
2026-05-30 09:44:03 +00:00

40 lines
691 B
Go

package vector
// Centroid returns the element-wise mean (centroid) of the given vectors as a
// new, independent vector. Vectors whose length differs from the first vector
// are ignored, and the mean is taken over the vectors actually included. It
// returns nil when vs is empty or the first vector has no elements.
func Centroid(vs Vectors) Vector {
if len(vs) == 0 {
return nil
}
dim := len(vs[0])
if dim == 0 {
return nil
}
result := make(Vector, dim)
n := 0
for _, v := range vs {
if len(v) != dim {
continue
}
for j := range dim {
result[j] += v[j]
}
n++
}
inv := 1 / float64(n)
for j := range result {
result[j] *= inv
}
return result
}