goext/mathext/statistics.go

44 lines
694 B
Go

package mathext
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
func Sum[T langext.NumberConstraint](v []T) T {
total := T(0)
for _, v := range v {
total += v
}
return total
}
func Mean[T langext.FloatConstraint](v []T) T {
return Sum(v) / T(len(v))
}
func Median[T langext.FloatConstraint](v []T) T {
if len(v)%2 == 1 {
return v[len(v)/2]
} else {
return (v[len(v)/2-1] + v[len(v)/2]) / T(2)
}
}
func ArrMin[T langext.OrderedConstraint](v []T) T {
r := v[0]
for _, val := range v {
if val < r {
r = val
}
}
return r
}
func ArrMax[T langext.OrderedConstraint](v []T) T {
r := v[0]
for _, val := range v {
if val > r {
r = val
}
}
return r
}