diff --git a/stat/arithmetics_mul.go b/stat/arithmetics_mul.go new file mode 100644 index 0000000000000000000000000000000000000000..509cce13ab30c4915095afca7162e28520eec4d5 --- /dev/null +++ b/stat/arithmetics_mul.go @@ -0,0 +1,20 @@ +package stat + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" + "golang.org/x/exp/slices" +) + +// Mul arithmetics 乘法 +func Mul[T Number](x []T, y any) []T { + return binaryOperations(x, y, vek32.Mul, vek.Mul, __mul_go[T]) +} + +func __mul_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_mul_test.go b/stat/arithmetics_mul_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d383696abfe8c642cc45671763b196ca3c671a9d --- /dev/null +++ b/stat/arithmetics_mul_test.go @@ -0,0 +1,19 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestMul(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(Mul(f1, f2)) + fmt.Println(Mul(d2, float64(1))) + fmt.Println(Mul(d3, int32(2))) + fmt.Println(Mul(d4, int64(3))) + fmt.Println(Mul(d4, int64(3))) +}