diff --git a/stat/arithmetics_div.go b/stat/arithmetics_div.go new file mode 100644 index 0000000000000000000000000000000000000000..c8883044d9e276209ab1804e37d1b2f285c29d87 --- /dev/null +++ b/stat/arithmetics_div.go @@ -0,0 +1,20 @@ +package stat + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" + "golang.org/x/exp/slices" +) + +// Div arithmetics 除法 +func Div[T Number](x []T, y any) []T { + return binaryOperations(x, y, vek32.Div, vek.Div, __div_go[T]) +} + +func __div_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_div_test.go b/stat/arithmetics_div_test.go new file mode 100644 index 0000000000000000000000000000000000000000..bd501098cce156290325e6c3efeb69cd17eff7ad --- /dev/null +++ b/stat/arithmetics_div_test.go @@ -0,0 +1,19 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestDiv(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(Div(f1, f2)) + fmt.Println(Div(d2, float64(1))) + fmt.Println(Div(d3, int32(2))) + fmt.Println(Div(d4, int64(3))) + fmt.Println(Div(d4, int64(3))) +}