KnowledgeRefinery/daemon-go/internal/mathutil/vector.go
oho 38a99476d6 Knowledge Refinery: local-first semantic search & 3D concept visualization
macOS app for corpus ingestion, semantic search, and concept universe
visualization powered by local LLMs via LM Studio.

Architecture:
- Go daemon (17MB single binary, zero dependencies)
  - chi router, pure-Go SQLite, tiktoken tokenizer
  - 6-stage pipeline: scan → extract → chunk → embed → annotate → conceptualize
  - Brute-force cosine vector search in memory
  - 89 tests across 8 packages
- SwiftUI app (macOS 15+)
  - Multi-workspace management with auto-start daemons
  - Live pipeline progress, search, concept browser
  - WebGPU 3D universe renderer with Canvas2D fallback
  - Custom crystal app icon
2026-02-13 18:09:46 +01:00

44 lines
965 B
Go

package mathutil
import "math"
// CosineSimilarity computes cosine similarity between two vectors.
func CosineSimilarity(a, b []float32) float64 {
var dot, normA, normB float64
for i := range a {
dot += float64(a[i]) * float64(b[i])
normA += float64(a[i]) * float64(a[i])
normB += float64(b[i]) * float64(b[i])
}
norm := math.Sqrt(normA) * math.Sqrt(normB)
if norm == 0 {
return 0
}
return dot / norm
}
// Normalize normalizes a vector to unit length.
func Normalize(v []float32) []float32 {
var norm float64
for _, x := range v {
norm += float64(x) * float64(x)
}
norm = math.Sqrt(norm)
if norm == 0 {
return make([]float32, len(v))
}
out := make([]float32, len(v))
for i, x := range v {
out[i] = float32(float64(x) / norm)
}
return out
}
// DotProduct computes the dot product of two vectors.
func DotProduct(a, b []float32) float64 {
var sum float64
for i := range a {
sum += float64(a[i]) * float64(b[i])
}
return sum
}