Changed add method to have the first element be a slice

This commit is contained in:
btracey
2013-07-18 19:30:54 -07:00
parent 135f9bf92d
commit b7c4b27fb3
3 changed files with 73 additions and 8 deletions

View File

@@ -6,13 +6,78 @@ import (
// Set of examples for all the functions // 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} s1 := []float64{1, 2, 3, 4}
s2 := []float64{5, 6, 7, 8} 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("s1 = ", s1)
fmt.Println("s2 = ", s2) fmt.Println("s2 = ", s2)
fmt.Println("s3 = ", s3)
// Output: // Output:
// s1 = [6 8 10 12] // s1 = [7 9 11 13]
// s2 = [5 6 7 8] // 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]
} }

View File

@@ -16,13 +16,13 @@ func (i InsufficientElements) Error() string {
// For computational efficiency, it is assumed that all of // For computational efficiency, it is assumed that all of
// the variadic arguments have the same length. If this is // the variadic arguments have the same length. If this is
// in doubt, EqLengths can be called. // in doubt, EqLengths can be called.
func Add(slices ...[]float64) { func Add(dst []float64, slices ...[]float64) {
if len(slices) == 0 { if len(slices) == 0 {
return return
} }
for i := 1; i < len(slices); i++ { for i := 0; i < len(slices); i++ {
for j, val := range slices[i] { for j, val := range slices[i] {
slices[0][j] += val dst[j] += val
} }
} }
} }

View File

@@ -17,7 +17,7 @@ const (
func AreSlicesEqual(t *testing.T, truth, comp []float64, str string) { func AreSlicesEqual(t *testing.T, truth, comp []float64, str string) {
if !Eq(comp, truth, EQTOLERANCE) { 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) { func benchmarkAdd(b *testing.B, s ...[]float64) {
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Add(s...) Add(s[0], s[1:]...)
} }
} }