From 82c0897af683c9c6ac89c796657b0e76baa2775d Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 12:39:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A0=E9=80=9F?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/init.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 stat/init.go diff --git a/stat/init.go b/stat/init.go new file mode 100644 index 0000000..2942a6c --- /dev/null +++ b/stat/init.go @@ -0,0 +1,10 @@ +package stat + +import "github.com/viterin/vek" + +// 初始化 avx2 +// 可以参考另一个实现库 gonum.org/v1/gonum/stat +func init() { + // 开启加速选项 + vek.SetAcceleration(true) +} -- Gitee From 4cde01ecd3cae1b5c30b81ad08ff6794294e904b Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 13:02:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#I6CYPC=20=E5=AE=9E=E7=8E=B0MAX=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/max.go | 12 ++++++++++++ formula/max_test.go | 15 +++++++++++++++ generic.go | 4 ++++ series.go | 3 ++- stat/maxinum.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ stat/maxinum_test.go | 12 ++++++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 formula/max.go create mode 100644 formula/max_test.go create mode 100644 stat/maxinum.go create mode 100644 stat/maxinum_test.go diff --git a/formula/max.go b/formula/max.go new file mode 100644 index 0000000..c00d93f --- /dev/null +++ b/formula/max.go @@ -0,0 +1,12 @@ +package formula + +import ( + "gitee.com/quant1x/pandas" + "gitee.com/quant1x/pandas/stat" +) + +func MAX(S1, S2 pandas.Series) pandas.Series { + d := stat.Maxinum(S1.Float(), S2.Float()) + return pandas.NewSeries(pandas.SERIES_TYPE_FLOAT32, "", d) + +} diff --git a/formula/max_test.go b/formula/max_test.go new file mode 100644 index 0000000..79a109d --- /dev/null +++ b/formula/max_test.go @@ -0,0 +1,15 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestMax(t *testing.T) { + f1 := []float32{1.1, 2.2, 1.3, 1.4} + f2 := []float32{1.2, 1.2, 3.3} + s1 := pandas.NewSeries(pandas.SERIES_TYPE_FLOAT64, "x1", f1) + s2 := pandas.NewSeries(pandas.SERIES_TYPE_FLOAT64, "x2", f2) + fmt.Println(MAX(s1, s2)) +} diff --git a/generic.go b/generic.go index c031038..87479e9 100644 --- a/generic.go +++ b/generic.go @@ -151,6 +151,10 @@ func (self *NDFrame) NaN() any { } } +func (self *NDFrame) Float() []float32 { + return ToFloat32(self) +} + func (self *NDFrame) Empty() Series { var frame NDFrame if self.type_ == SERIES_TYPE_STRING { diff --git a/series.go b/series.go index e4afc96..2820de3 100644 --- a/series.go +++ b/series.go @@ -37,7 +37,8 @@ type Series interface { // NaN 输出默认的NaN NaN() any - + // Float 强制转成[]float32 + Float() []float32 // sort.Interface // Len 获得行数, 实现sort.Interface接口的获取元素数量方法 diff --git a/stat/maxinum.go b/stat/maxinum.go new file mode 100644 index 0000000..99b7f3b --- /dev/null +++ b/stat/maxinum.go @@ -0,0 +1,46 @@ +package stat + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" + "unsafe" +) + +// Maxinum 两个序列横向比较最大值 +func Maxinum[T Float](f1, f2 []T) []T { + xlen := len(f1) + ylen := len(f2) + // 第找出最大长度 + + maxLength := xlen + if maxLength < ylen { + maxLength = ylen + } + + // 处理默认值 + defaultValue := typeDefault(T(0)) + // 对齐所有长度 + if xlen < maxLength { + f1 = Align(f1, defaultValue, maxLength) + } + if ylen < maxLength { + f2 = Align(f2, defaultValue, maxLength) + } + // 初始化返回值 + var s1, s2 any + s1 = f1 + s2 = f2 + + var d any + + bitSize := unsafe.Sizeof(f1[0]) + if bitSize == 4 { + d = vek32.Maximum(s1.([]float32), s2.([]float32)) + } else if bitSize == 8 { + d = vek.Maximum(s1.([]float64), s2.([]float64)) + } else { + // 应该不会走到这里 + panic("不支持其它类型") + } + return d.([]T) +} diff --git a/stat/maxinum_test.go b/stat/maxinum_test.go new file mode 100644 index 0000000..dfd136d --- /dev/null +++ b/stat/maxinum_test.go @@ -0,0 +1,12 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestMaxinum(t *testing.T) { + f1 := []float32{1.1, 2.2, 1.3, 1.4} + f2 := []float32{1.2, 1.2, 3.3} + fmt.Println(Maxinum(f1, f2)) +} -- Gitee