diff --git a/stat/arithmetics_add.go b/stat/arithmetics_add.go new file mode 100644 index 0000000000000000000000000000000000000000..2ff2504386395d3bde6156ac1b2145048fe8c131 --- /dev/null +++ b/stat/arithmetics_add.go @@ -0,0 +1,20 @@ +package stat + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" + "golang.org/x/exp/slices" +) + +// Add arithmetics 加法 +func Add[T Number](x []T, y any) []T { + return binaryOperations(x, y, vek32.Add, vek.Add, __add_go[T]) +} + +func __add_go[T Number](x, y []T) []T { + x = slices.Clone(x) + for i := 0; i < len(x); i++ { + x[i] += y[i] + } + return x +} diff --git a/stat/arithmetics_add_test.go b/stat/arithmetics_add_test.go new file mode 100644 index 0000000000000000000000000000000000000000..640125f08ddd343d6e2a270314fa3805be639010 --- /dev/null +++ b/stat/arithmetics_add_test.go @@ -0,0 +1,18 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestAdd(t *testing.T) { + f1 := []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + f2 := []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + d2 := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + d3 := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + d4 := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + fmt.Println(Add(f1, f2)) + fmt.Println(Add(d2, float64(1))) + fmt.Println(Add(d3, int32(2))) + fmt.Println(Add(d4, int64(3))) +} diff --git a/stat/arithmetics_sub.go b/stat/arithmetics_sub.go index c67aac5f5e35bd7b713536927510a5f65b58aaca..237f5c1e42189521665a71d47f596e373c1df1a7 100644 --- a/stat/arithmetics_sub.go +++ b/stat/arithmetics_sub.go @@ -7,11 +7,11 @@ import ( ) // Sub arithmetics 减法 -func Sub[T StatType](x []T, y any) []T { - return binaryOperations(x, y, vek32.Sub, vek.Sub, __sub[T]) +func Sub[T Number](x []T, y any) []T { + return binaryOperations(x, y, vek32.Sub, vek.Sub, __sub_go[T]) } -func __sub[T StatType](x, y []T) []T { +func __sub_go[T Number](x, y []T) []T { x = slices.Clone(x) for i := 0; i < len(x); i++ { x[i] -= y[i]