You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.0 KiB
115 lines
3.0 KiB
package common_calc
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"github.com/google/uuid"
|
|
"log"
|
|
"math"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
// Decimal float64 保留小数点后位数
|
|
// value float64 浮点数
|
|
// prec int 需保留小数点后的位数
|
|
func Decimal(value float64, prec int) float64 {
|
|
value, _ = strconv.ParseFloat(strconv.FormatFloat(value, 'f', prec, 64), 64)
|
|
return value
|
|
}
|
|
|
|
// 和scala 计算不一致 注意!
|
|
func UUIDFromString(raw string) string {
|
|
bys := []byte(raw)
|
|
uid := uuid.NewMD5(uuid.Nil, bys)
|
|
log.Println(uid.String(), uid.Version())
|
|
uid = uuid.NewMD5(uuid.NameSpaceURL, bys)
|
|
log.Println(uid.String(), uid.Version())
|
|
uid = uuid.NewMD5(uuid.NameSpaceX500, bys)
|
|
log.Println(uid.String(), uid.Version())
|
|
uid = uuid.NewMD5(uuid.NameSpaceDNS, bys)
|
|
log.Println(uid.String(), uid.Version())
|
|
uid = uuid.NewMD5(uuid.NameSpaceOID, bys)
|
|
log.Println(uid.String(), uid.Version())
|
|
|
|
return uid.String()
|
|
|
|
}
|
|
|
|
func NameUUIDFromString(raw string) string {
|
|
sb := md5.Sum([]byte(raw))
|
|
sb[6] &= 0x0f
|
|
sb[6] |= 0x30
|
|
sb[8] &= 0x3f
|
|
sb[8] |= 0x80
|
|
uid, err := uuid.FromBytes(sb[:])
|
|
if err != nil {
|
|
log.Printf("uid 错误异常=%x", err.Error())
|
|
}
|
|
return uid.String()
|
|
|
|
}
|
|
|
|
func MinMax(input []float64) (float64, float64) {
|
|
sort.Float64s(input)
|
|
return input[0], input[len(input)-1]
|
|
}
|
|
func AbsMax(input []float64) float64 {
|
|
sort.Float64s(input)
|
|
return max(math.Abs(input[0]), math.Abs(input[len(input)-1]))
|
|
}
|
|
func MeanSqrt(data []float64) float64 {
|
|
var sumSquares float64
|
|
for _, v := range data {
|
|
sumSquares += v * v
|
|
}
|
|
return math.Sqrt(sumSquares / float64(len(data)))
|
|
}
|
|
|
|
func GetAvg(dataArray []float64) float64 {
|
|
sum := 0.0
|
|
for _, f := range dataArray {
|
|
sum += f
|
|
}
|
|
return sum / float64(len(dataArray))
|
|
}
|
|
|
|
func GetMedian(dataArray []float64) float64 {
|
|
n := len(dataArray)
|
|
sort.Float64s(dataArray)
|
|
if n%2 == 1 {
|
|
// 如果切片元素个数为奇数,中位数即为中间的元素
|
|
return dataArray[n/2]
|
|
} else {
|
|
// 如果元素个数为偶数,中位数为中间两个元素的平均值
|
|
return (dataArray[n/2-1] + dataArray[n/2]) / 2.0
|
|
}
|
|
}
|
|
|
|
// GetVariance calculates the unbiased population variance from the provided samples as an unsorted array.
|
|
// On a dataset of size N, it will use an N-1 normalizer (Bessel's correction).
|
|
// Returns NaN if data has less than two entries or if any entry is NaN.
|
|
// 方差
|
|
// Translated from Math.Net
|
|
// Source code at https://github.com/mathnet/mathnet-numerics/blob/master/src/Numerics/Statistics/ArrayStatistics.cs
|
|
func GetVariance(samples []float64) float64 {
|
|
if len(samples) <= 1 {
|
|
return math.NaN()
|
|
}
|
|
|
|
var variance float64
|
|
t := samples[0]
|
|
for i := 1; i < len(samples); i++ {
|
|
t += samples[i]
|
|
diff := float64((i+1)*int(samples[i])) - t
|
|
variance += (diff * diff) / (float64(i+1) * float64(i))
|
|
}
|
|
|
|
return variance / float64(len(samples)-1)
|
|
}
|
|
|
|
func StandardDeviation(samples []float64) float64 {
|
|
return math.Sqrt(GetVariance(samples))
|
|
}
|
|
func MeanStandardDeviation(samples []float64) (float64, float64) {
|
|
return GetAvg(samples), StandardDeviation(samples)
|
|
}
|
|
|