1 Star 4 Fork 12

王布衣/pandas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
arithmetics.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-02-18 15:26 . !68修订部分问题, 清理package
package stat
// 四则运算 (arithmetics)
// 一元运算 unary operations
func unaryOperations[T Number](x []T, f32 func([]float32) []float32, f64 func([]float64) []float64, cany func([]T) []T) []T {
var t []T
if len(x) == 0 {
return t
}
var d any
var s any
s = x
switch fs := s.(type) {
case []float32:
d = f32(fs)
case []float64:
d = f64(fs)
default:
d = cany(x)
}
return d.([]T)
}
func unaryOperations1[T Number](x []T, f32 func([]float32) float32, f64 func([]float64) float64, cany func([]T) T) T {
var t T
if len(x) == 0 {
return t
}
var d any
var s any
s = x
switch fs := s.(type) {
case []float32:
d = f32(fs)
case []float64:
d = f64(fs)
default:
d = cany(x)
}
return d.(T)
}
// 一元运算 unary operations
//
// 运算和返回值是两种类型
func unaryOperations2[T Number, E Number](x []T, f32 func([]float32) E, f64 func([]float64) E, cany func([]T) E) E {
if len(x) == 0 {
return E(0)
}
var d any
var s any
s = x
switch fs := s.(type) {
case []float32:
d = f32(fs)
case []float64:
d = f64(fs)
default:
d = cany(x)
}
return d.(E)
}
// 二元运算 binary operations
//
// Binary operation
// calculate
func binaryOperations[T Number](x []T, y any, f32 func(x, y []float32) []float32, f64 func(x, y []float64) []float64, cany func(x, y []T) []T) []T {
var d any
length := len(x)
var s any = x
switch vs := s.(type) {
case []float32:
f32s := AnyToSlice[float32](y, length)
d = f32(vs, f32s)
case []float64:
f64s := AnyToSlice[float64](y, length)
d = f64(vs, f64s)
default:
ys := AnyToSlice[T](y, length)
d = cany(x, ys)
}
return d.([]T)
}
func binaryOperations2[T BaseType, E BaseType](x, y []T, f32 func(x, y []float32) []E, f64 func(x, y []float64) []E, cany func(x, y []T) []E) []E {
var d any
length := len(x)
var s any = x
switch vs := s.(type) {
case []float32:
f32s := AnyToSlice[float32](y, length)
d = f32(vs, f32s)
case []float64:
f64s := AnyToSlice[float64](y, length)
d = f64(vs, f64s)
default:
ys := AnyToSlice[T](y, length)
d = cany(x, ys)
}
return d.([]E)
}
// 三元运算 triple operations
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v0.6.19

搜索帮助