Centralizes common math operations used across core/go-ai, core/go, and core/mining into Poindexter as the math pillar (alongside Borg=data, Enchantrix=encryption). New modules: - stats: Sum, Mean, Variance, StdDev, MinMax, IsUnderrepresented - scale: Lerp, InverseLerp, Remap, RoundToN, Clamp, MinMaxScale - epsilon: ApproxEqual, ApproxZero - score: WeightedScore, Ratio, Delta, DeltaPercent - signal: RampUp, SineWave, Oscillate, Noise (seeded RNG) 235 LOC implementation, 509 LOC tests, zero external deps, WASM-safe. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
40 lines
886 B
Go
40 lines
886 B
Go
package poindexter
|
||
|
||
// Factor is a value–weight pair for composite scoring.
|
||
type Factor struct {
|
||
Value float64
|
||
Weight float64
|
||
}
|
||
|
||
// WeightedScore computes the weighted sum of factors.
|
||
// Each factor contributes Value * Weight to the total.
|
||
// Returns 0 for empty slices.
|
||
func WeightedScore(factors []Factor) float64 {
|
||
var total float64
|
||
for _, f := range factors {
|
||
total += f.Value * f.Weight
|
||
}
|
||
return total
|
||
}
|
||
|
||
// Ratio returns part/whole safely. Returns 0 if whole is 0.
|
||
func Ratio(part, whole float64) float64 {
|
||
if whole == 0 {
|
||
return 0
|
||
}
|
||
return part / whole
|
||
}
|
||
|
||
// Delta returns the difference new_ - old.
|
||
func Delta(old, new_ float64) float64 {
|
||
return new_ - old
|
||
}
|
||
|
||
// DeltaPercent returns the percentage change from old to new_.
|
||
// Returns 0 if old is 0.
|
||
func DeltaPercent(old, new_ float64) float64 {
|
||
if old == 0 {
|
||
return 0
|
||
}
|
||
return (new_ - old) / old * 100
|
||
}
|