1 Star 0 Fork 4

王布衣/num

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
aggregates.go 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-02-12 09:23 . 调整package
package functions
import (
"gitee.com/quant1x/num/internal/constraints"
"gitee.com/quant1x/num/internal/partial"
"math"
"slices"
)
func Sum_Go[T constraints.Float](x []T) T {
sum := T(0)
for i := 0; i < len(x); i++ {
sum += x[i]
}
return sum
}
func CumSum_Go[T constraints.Float](x []T) {
sum := T(0)
for i := 0; i < len(x); i++ {
sum += x[i]
x[i] = sum
}
}
func Prod_Go[T constraints.Float](x []T) T {
prod := T(1)
for i := 0; i < len(x); i++ {
prod *= x[i]
}
return prod
}
func CumProd_Go[T constraints.Float](x []T) {
prod := T(1)
for i := 0; i < len(x); i++ {
prod *= x[i]
x[i] = prod
}
}
func Mean_Go[T constraints.Float](x []T) T {
return Sum_Go(x) / T(len(x))
}
func Median_Go[T constraints.Float](x []T) T {
if len(x)%2 == 1 {
x = slices.Clone(x)
i := len(x) / 2
partial.TopK(x, i+1)
return x[i]
}
return Quantile_Go(x, T(0.5))
}
func Quantile_Go[T constraints.Float](x []T, q T) T {
if len(x) == 1 {
return x[0]
}
if q == T(0) {
return Min_Go(x)
}
if q == T(1) {
return Max_Go(x)
}
x = slices.Clone(x)
f := T(len(x)-1) * q
i := int(math.Floor(float64(f)))
if q < 0.5 {
partial.TopK(x, i+2)
a := Max_Go(x[:i+1])
b := x[i+1]
return a + (b-a)*(f-T(i))
} else {
partial.TopK(x, i+1)
a := x[i]
b := Min_Go(x[i+1:])
return a + (b-a)*(f-T(i))
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/num.git
git@gitee.com:quant1x/num.git
quant1x
num
num
v0.2.6

搜索帮助