1 Star 0 Fork 2

王布衣 / num

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stddev.go 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-04-16 06:41 . 修订std变量名拼写的错误
package num
import (
"gitee.com/quant1x/num/x32"
"gitee.com/quant1x/num/x64"
"math"
"slices"
)
// Std 计算标准差
func Std[T BaseType](x []T) T {
if len(x) == 0 {
return TypeDefault[T]()
}
var d any
switch fs := any(x).(type) {
case []float32:
d = __go_std_float32(fs)
case []float64:
d = __go_std_float64(fs)
default:
// 应该不会走到这里
panic(ErrUnsupportedType)
}
return d.(T)
}
func __go_std_float64(f []float64) float64 {
values := slices.Clone(f)
// 求平均数
mean := x64.Mean(values)
// 减去 平均数
x64.SubNumber_Inplace(values, mean)
// 计算方差
y := x64.Repeat(2.00, len(f))
x64.Pow_Inplace(values, y)
// 再求方差平均数
mean = x64.Mean(values)
mean = math.Sqrt(mean)
return mean
}
func __go_std_float32(f []float32) float32 {
values := slices.Clone(f)
// 求平均数
mean := x32.Mean(values)
// 减去 平均数
x32.SubNumber_Inplace(values, mean)
// 计算方差
y := x32.Repeat(2.00, len(f))
x32.Pow_Inplace(values, y)
// 再求方差平均数
mean = x32.Mean(values)
mean = float32(math.Sqrt(float64(mean)))
return mean
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/num.git
git@gitee.com:quant1x/num.git
quant1x
num
num
v0.3.2

搜索帮助

Bbcd6f05 5694891 0cc6727d 5694891