From 9d856e6f9f065b1dacb6ae8ce8039118b168321f Mon Sep 17 00:00:00 2001 From: wangfeng Date: Fri, 3 Feb 2023 11:53:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/where.go | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/stat/where.go b/stat/where.go index 929cd3e..a7d5253 100644 --- a/stat/where.go +++ b/stat/where.go @@ -2,7 +2,6 @@ package stat import ( "gitee.com/quant1x/pandas/algorithms/avx2" - "math" ) //func Where[T int64 | float64](condition any, params ...any) []T { @@ -18,39 +17,6 @@ import ( // params只支持两个默认值x和y, 如果condition为true返回x, 否则返回y // condition和param都可能是基础数据类型,也可能是一个slice, 并且长度可能不一致 // 直接写成序列版本, 可能更简单 -// func Where[T int64 | float64](condition []T, x, y []T) []T { -func Where1(condition []float64, x, y []float64) []float64 { - // 第一步, 找出最长的 - clen := len(condition) - xlen := len(x) - ylen := len(y) - // 第二步, 找出最大长度 - c := []float64{float64(clen), float64(xlen), float64(ylen)} - maxLength := int(avx2.Max(c)) - - // 对齐所有长度 - if clen < maxLength { - condition = align(condition, math.NaN(), maxLength) - } - if xlen < maxLength { - x = align(x, math.NaN(), maxLength) - } - if ylen < maxLength { - y = align(y, math.NaN(), maxLength) - } - // 初始化返回值 - d := make([]float64, maxLength) - for i := 0; i < maxLength; i++ { - // NaN也被认为是真 - if condition[i] != 0 { - d[i] = x[i] - } else { - d[i] = y[i] - } - } - return d -} - func Where[T StatType](condition []T, x, y []T) []T { // 第一步, 找出最长的 clen := len(condition) -- Gitee From 40e84be7098d3d20d9cfd86c46843d5833ff8579 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Fri, 3 Feb 2023 13:19:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#I6CC1H=20=E5=AE=9E=E7=8E=B0abs=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=80=BC=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/abs.go | 14 ++++++++++++++ stat/abs_test.go | 19 +++++++++++++++++++ stat/unsafe.go | 8 ++++++++ stat/unsafe_test.go | 12 ++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 stat/abs.go create mode 100644 stat/abs_test.go create mode 100644 stat/unsafe.go create mode 100644 stat/unsafe_test.go diff --git a/stat/abs.go b/stat/abs.go new file mode 100644 index 0000000..c3af367 --- /dev/null +++ b/stat/abs.go @@ -0,0 +1,14 @@ +package stat + +func Abs[T StatType](x []T) []T { + xlen := len(x) + d := make([]T, xlen) + for i := 0; i < xlen; i++ { + if x[i] < 0 { + d[i] = -1 * (x[i]) + } else { + d[i] = x[i] + } + } + return d +} diff --git a/stat/abs_test.go b/stat/abs_test.go new file mode 100644 index 0000000..45f76f9 --- /dev/null +++ b/stat/abs_test.go @@ -0,0 +1,19 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestAbs(t *testing.T) { + + v1 := []int32{1, -1, 2, -2} + fmt.Println(Abs(v1)) + v2 := []int64{1, -1, 2, -2} + fmt.Println(Abs(v2)) + v3 := []float32{1.1, -1.1, 2.2, -2.2} + fmt.Println(Abs(v3)) + v4 := []float64{1.1, -1.1, 2.2, -2.2} + fmt.Println(Abs(v4)) + +} diff --git a/stat/unsafe.go b/stat/unsafe.go new file mode 100644 index 0000000..45766af --- /dev/null +++ b/stat/unsafe.go @@ -0,0 +1,8 @@ +package stat + +func math_abs[T StatType](v T) T { + if v < 0 { + return v * -1 + } + return v +} diff --git a/stat/unsafe_test.go b/stat/unsafe_test.go new file mode 100644 index 0000000..e53ad50 --- /dev/null +++ b/stat/unsafe_test.go @@ -0,0 +1,12 @@ +package stat + +import ( + "fmt" + "testing" +) + +func Test_frombits(t *testing.T) { + i32_1 := float64(-1) + //fmt.Println(math.Abs(i32_1)) + fmt.Println(math_abs(i32_1)) +} -- Gitee