diff --git a/examples_test.go b/examples_test.go index 65b11e45..a2fec318 100644 --- a/examples_test.go +++ b/examples_test.go @@ -6,13 +6,78 @@ import ( // Set of examples for all the functions -func ExampleAdd() { +func ExampleAdd_Simple() { + // Adding three slices together. Note that + // the result is stored in the first slice s1 := []float64{1, 2, 3, 4} s2 := []float64{5, 6, 7, 8} - Add(s1, s2) + s3 := []float64{1, 1, 1, 1} + + Add(s1, s2, s3) + fmt.Println("s1 = ", s1) fmt.Println("s2 = ", s2) + fmt.Println("s3 = ", s3) // Output: - // s1 = [6 8 10 12] + // s1 = [7 9 11 13] // s2 = [5 6 7 8] + // s3 = [1 1 1 1] +} + +func ExampleAdd_NewSlice() { + // If one wants to store the result in a + // new container, just make a new slice + s1 := []float64{1, 2, 3, 4} + s2 := []float64{5, 6, 7, 8} + s3 := []float64{1, 1, 1, 1} + dst := make([]float64, len(s1)) + + Add(dst, s1, s2, s3) + + fmt.Println("dst = ", dst) + fmt.Println("s1 = ", s1) + fmt.Println("s2 = ", s2) + fmt.Println("s3 = ", s3) + // Output: + // dst = [7 9 11 13] + // s1 = [1 2 3 4] + // s2 = [5 6 7 8] + // s3 = [1 1 1 1] +} + +func ExampleAdd_UnequalLengths() { + // If the lengths of the slices are unknown, + // use Eqlen to check + s1 := []float64{1, 2, 3} + s2 := []float64{5, 6, 7, 8} + + eq := Eqlen(s1, s2) + if eq { + Add(s1, s2) + } else { + fmt.Println("Unequal lengths") + } + // Output: + // Unequal lengths +} + +func ExampleAdd_SliceOfSliceSum() { + // Add can also be used to take a columnwise sum + // of a slice of slices + + // First, initialize a slice of slices + s := make([][]float64, 5) + for i := range s { + s[i] = make([]float64, 3) + for j := range s[i] { + s[i][j] = float64(j) + } + } + // Remember that the first entry is the destination + result := make([]float64, len(s[0])) + Add(result, s...) + + fmt.Println("result = ", result) + // Output: + // result = [0 5 10] } diff --git a/sliceops.go b/sliceops.go index 1b050d57..b75d06a2 100644 --- a/sliceops.go +++ b/sliceops.go @@ -16,13 +16,13 @@ func (i InsufficientElements) Error() string { // For computational efficiency, it is assumed that all of // the variadic arguments have the same length. If this is // in doubt, EqLengths can be called. -func Add(slices ...[]float64) { +func Add(dst []float64, slices ...[]float64) { if len(slices) == 0 { return } - for i := 1; i < len(slices); i++ { + for i := 0; i < len(slices); i++ { for j, val := range slices[i] { - slices[0][j] += val + dst[j] += val } } } diff --git a/sliceops_test.go b/sliceops_test.go index a02b8f96..020c84ca 100644 --- a/sliceops_test.go +++ b/sliceops_test.go @@ -17,7 +17,7 @@ const ( func AreSlicesEqual(t *testing.T, truth, comp []float64, str string) { if !Eq(comp, truth, EQTOLERANCE) { - t.Errorf(str+"Expected %v, returned %v", truth, comp) + t.Errorf(str+". Expected %v, returned %v", truth, comp) } } @@ -367,7 +367,7 @@ func BenchmarkMinHuge(b *testing.B) { func benchmarkAdd(b *testing.B, s ...[]float64) { b.StartTimer() for i := 0; i < b.N; i++ { - Add(s...) + Add(s[0], s[1:]...) } }