代码拉取完成,页面将自动刷新
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
/*
const (
__k_calc_add = 1 // 加
__k_calc_sub = 2 // 减
__k_calc_mul = 3 // 乘
__k_calc_div = 4 // 除
__k_calc_mod = 5 // 取模
)
var (
// 加
__calc_add = func(f1, f2 DType) DType {
return f1 + f2
}
// 减
__calc_sub = func(f1, f2 DType) DType {
return f1 - f2
}
// 乘
__calc_mul = func(f1, f2 DType) DType {
return f1 * f2
}
// 除
__calc_div = func(f1, f2 DType) DType {
return f1 / f2
}
// 取模
__calc_mod = func(f1, f2 DType) DType {
return math.Mod(f1, f2)
}
)
// 重构
func __arithmetic[T ~[]E, E Number](x T, y any, c int, calculator func(f1, f2 DType) E) []E {
if __y, ok := y.(Series); ok {
y = __y.Values()
}
var d = []E{}
switch Y := y.(type) {
case nil, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, string:
f2 := Any2DType(Y)
d = __arithmetic_dtype(x, f2, c, calculator)
case []float32:
d = __arithmetic_slice(x, Y, c, calculator)
case []float64:
d = __arithmetic_slice(x, Y, c, calculator)
case []int:
d = __arithmetic_slice(x, Y, c, calculator)
case []int8:
d = __arithmetic_slice(x, Y, c, calculator)
case []int16:
d = __arithmetic_slice(x, Y, c, calculator)
case []int32:
d = __arithmetic_slice(x, Y, c, calculator)
case []int64:
d = __arithmetic_slice(x, Y, c, calculator)
case []uint:
d = __arithmetic_slice(x, Y, c, calculator)
case []uint8:
d = __arithmetic_slice(x, Y, c, calculator)
case []uint16:
d = __arithmetic_slice(x, Y, c, calculator)
case []uint32:
d = __arithmetic_slice(x, Y, c, calculator)
case []uint64:
d = __arithmetic_slice(x, Y, c, calculator)
case []uintptr:
d = __arithmetic_slice(x, Y, c, calculator)
//case []string:
// d = __arithmetic_slice(x, Y, c, calculator)
//case []bool:
// d = __arithmetic_slice(x, Y, c, calculator)
default:
// 其它未知类型抛异常
panic(Throw(y))
}
return d
}
// 切片和dtype对比, 不用考虑slice长度对齐的问题
func __arithmetic_dtype[T ~[]E, E Number](x T, y DType, c int, calculator func(f1, f2 DType) E) []E {
var d any
xLen := len(x)
kind := checkoutRawType(x)
switch {
case kind == SERIES_TYPE_FLOAT64 && c == __k_calc_add:
fs := make([]float64, xLen)
d = vek.AddNumber_Into(fs, any(x).([]float64), y)
case kind == SERIES_TYPE_FLOAT64 && c == __k_calc_sub:
fs := make([]float64, xLen)
d = vek.SubNumber_Into(fs, any(x).([]float64), y)
case kind == SERIES_TYPE_FLOAT64 && c == __k_calc_mul:
fs := make([]float64, xLen)
d = vek.MulNumber_Into(fs, any(x).([]float64), y)
case kind == SERIES_TYPE_FLOAT64 && c == __k_calc_div:
fs := make([]float64, xLen)
d = vek.DivNumber_Into(fs, any(x).([]float64), y)
case kind == SERIES_TYPE_FLOAT32 && c == __k_calc_add:
fs := make([]float32, xLen)
d = vek32.AddNumber_Into(fs, any(x).([]float32), float32(y))
case kind == SERIES_TYPE_FLOAT32 && c == __k_calc_sub:
fs := make([]float32, xLen)
d = vek32.SubNumber_Into(fs, any(x).([]float32), float32(y))
case kind == SERIES_TYPE_FLOAT32 && c == __k_calc_mul:
fs := make([]float32, xLen)
d = vek32.MulNumber_Into(fs, any(x).([]float32), float32(y))
case kind == SERIES_TYPE_FLOAT32 && c == __k_calc_div:
fs := make([]float32, xLen)
d = vek32.DivNumber_Into(fs, any(x).([]float32), float32(y))
default:
b := y
bs := make([]E, xLen)
for i := 0; i < xLen; i++ {
a := Any2DType(x[i])
bs[i] = calculator(a, b)
}
}
return d.([]E)
}
// 切片和切片对比
func __arithmetic_slice[T ~[]E, E Number, T2 ~[]E2, E2 Number](x T, y T2, c int, calculator func(f1, f2 DType) E) []E {
var d any
xLen := len(x)
yLen := len(y)
if xLen >= yLen {
es := make([]E, xLen)
switch xs := any(x).(type) {
case []float64:
vek.Add_Into(es[:yLen], xs[:yLen], any(y).([]float64)[:yLen])
}
switch {
case xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_calc_add:
vek.Add_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_calc_sub:
vek.Sub_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_calc_mul:
vek.Mul_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_calc_div:
vek.Div_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_calc_add:
vek.Add_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_calc_sub:
vek.Sub_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_calc_mul:
vek.Mul_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
case xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_calc_div:
vek.Div_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
}
if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_calc_add {
vek.Gt_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_gte {
es := make([]float64, xLen)
vek.Gte_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_lt {
es := make([]float64, xLen)
vek.Lt_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_lte {
es := make([]float64, xLen)
vek.Lte_Into(es[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_gt {
vek32.Gt_Into(es[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_gte {
vek32.Gte_Into(es[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_lt {
vek32.Lt_Into(es[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_lte {
vek32.Lte_Into(es[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen])
} else if xKind == SERIES_TYPE_BOOL && xKind == yKind && c == __k_compare_and {
vek.And_Into(es[:yLen], any(x).([]bool)[:yLen], any(y).([]bool)[:yLen])
} else {
for i := 0; i < yLen; i++ {
f1 := Any2DType(x[i])
f2 := Any2DType(y[i])
es[i] = calculator(f1, f2)
}
}
for i := yLen; i < xLen; i++ {
f1 := Any2DType(x[i])
f2 := DType(0)
es[i] = calculator(f1, f2)
}
} else {
es = make([]bool, yLen)
if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_gt {
vek.Gt_Into(es[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_gte {
vek.Gte_Into(es[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_lt {
vek.Lt_Into(es[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT64 && xKind == yKind && c == __k_compare_lte {
vek.Lte_Into(es[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_gt {
vek32.Gt_Into(es[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_gte {
vek32.Gte_Into(es[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_lt {
vek32.Lt_Into(es[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen])
} else if xKind == SERIES_TYPE_FLOAT32 && xKind == yKind && c == __k_compare_lte {
vek32.Lte_Into(es[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen])
} else if xKind == SERIES_TYPE_BOOL && xKind == yKind && c == __k_compare_and {
vek.And_Into(es[:xLen], any(x).([]bool)[:xLen], any(y).([]bool)[:xLen])
} else {
for i := 0; i < xLen; i++ {
f1 := Any2DType(x[i])
f2 := Any2DType(y[i])
es[i] = calculator(f1, f2)
}
}
for i := xLen; i < yLen; i++ {
f1 := DType(0)
f2 := Any2DType(y[i])
es[i] = calculator(f1, f2)
}
}
return d.([]E)
}
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。