mirror of
https://github.com/saymrwulf/KnowledgeRefinery.git
synced 2026-05-14 20:47:51 +00:00
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
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package mathutil
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func TestCosineSimilarityIdentical(t *testing.T) {
|
|
a := []float32{1, 0, 0}
|
|
sim := CosineSimilarity(a, a)
|
|
if math.Abs(sim-1.0) > 1e-6 {
|
|
t.Errorf("expected 1.0, got %f", sim)
|
|
}
|
|
}
|
|
|
|
func TestCosineSimilarityOrthogonal(t *testing.T) {
|
|
a := []float32{1, 0, 0}
|
|
b := []float32{0, 1, 0}
|
|
sim := CosineSimilarity(a, b)
|
|
if math.Abs(sim) > 1e-6 {
|
|
t.Errorf("expected 0.0, got %f", sim)
|
|
}
|
|
}
|
|
|
|
func TestCosineSimilarityOpposite(t *testing.T) {
|
|
a := []float32{1, 0, 0}
|
|
b := []float32{-1, 0, 0}
|
|
sim := CosineSimilarity(a, b)
|
|
if math.Abs(sim-(-1.0)) > 1e-6 {
|
|
t.Errorf("expected -1.0, got %f", sim)
|
|
}
|
|
}
|
|
|
|
func TestCosineSimilarityZeroVector(t *testing.T) {
|
|
a := []float32{0, 0, 0}
|
|
b := []float32{1, 2, 3}
|
|
sim := CosineSimilarity(a, b)
|
|
if sim != 0 {
|
|
t.Errorf("expected 0.0 for zero vector, got %f", sim)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeUnit(t *testing.T) {
|
|
v := []float32{3, 4, 0}
|
|
n := Normalize(v)
|
|
// Should be [0.6, 0.8, 0]
|
|
if math.Abs(float64(n[0])-0.6) > 1e-5 || math.Abs(float64(n[1])-0.8) > 1e-5 {
|
|
t.Errorf("unexpected normalized vector: %v", n)
|
|
}
|
|
// Magnitude should be 1.0
|
|
mag := DotProduct(n, n)
|
|
if math.Abs(mag-1.0) > 1e-5 {
|
|
t.Errorf("normalized vector magnitude %f != 1.0", mag)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeZero(t *testing.T) {
|
|
v := []float32{0, 0, 0}
|
|
n := Normalize(v)
|
|
for i, x := range n {
|
|
if x != 0 {
|
|
t.Errorf("expected zero at index %d, got %f", i, x)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDotProduct(t *testing.T) {
|
|
a := []float32{1, 2, 3}
|
|
b := []float32{4, 5, 6}
|
|
// 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
|
|
got := DotProduct(a, b)
|
|
if math.Abs(got-32.0) > 1e-6 {
|
|
t.Errorf("expected 32.0, got %f", got)
|
|
}
|
|
}
|