diff --git a/sliceops.go b/sliceops.go index 21173327..697d09da 100644 --- a/sliceops.go +++ b/sliceops.go @@ -143,25 +143,13 @@ func FindFirst(s []float64, f func(float64) bool, k int) (inds []int, err error) return inds, InsufficientElements{} } -// Linspace returns a set of N equally spaced points between l and u, where N -// is equal to the length of the destination. The first element of the destination -// is l, the final element of the destination is u. Will panic if the destination has -// length < 2 -func Linspace(dst []float64, l, u float64) { - n := len(dst) - step := (u - l) / float64(n-1) - for i := range dst { - dst[i] = l + step*float64(i) - } -} - -// Logspace returns a set of N equally spaced points in log space between l and u, where N +// LogSpan returns a set of N equally spaced points in log space between l and u, where N // is equal to the length of the destination. The first element of the destination // is l, the final element of the destination is u. Will panic if the destination has // length < 2. Note that this call will return NaNs if l or u are negative, and // zeros if l or u is zero. -func Logspace(dst []float64, l, u float64) { - Linspace(dst, math.Log(l), math.Log(u)) +func LogSpan(dst []float64, l, u float64) { + Span(dst, math.Log(l), math.Log(u)) Apply(dst, math.Exp) } @@ -276,6 +264,18 @@ func Scale(s []float64, c float64) { } } +// Span returns a set of N equally spaced points between l and u, where N +// is equal to the length of the destination. The first element of the destination +// is l, the final element of the destination is u. Will panic if the destination has +// length < 2 +func Span(dst []float64, l, u float64) { + n := len(dst) + step := (u - l) / float64(n-1) + for i := range dst { + dst[i] = l + step*float64(i) + } +} + // Sub subtracts, element-wise, the first argument from the second. Assumes // the lengths of s and t match (can be tested with EqLen) func Sub(s, t []float64) { diff --git a/sliceops_test.go b/sliceops_test.go index 655dd221..c4394359 100644 --- a/sliceops_test.go +++ b/sliceops_test.go @@ -158,21 +158,10 @@ func TestFindFirst(t *testing.T) { } } -func TestLinspace(t *testing.T) { - receiver := make([]float64, 5) - truth := []float64{1, 2, 3, 4, 5} - Linspace(receiver, 1, 5) - AreSlicesEqual(t, truth, receiver, "Improper linspace") - receiver = make([]float64, 6) - truth = []float64{0, 0.2, 0.4, 0.6, 0.8, 1.0} - Linspace(receiver, 0, 1) - AreSlicesEqual(t, truth, receiver, "Improper linspace") -} - -func TestLogspace(t *testing.T) { +func TestLogSpan(t *testing.T) { receiver := make([]float64, 6) truth := []float64{0.001, 0.01, 0.1, 1, 10, 100} - Logspace(receiver, 0.001, 100) + LogSpan(receiver, 0.001, 100) tst := make([]float64, 6) for i := range truth { tst[i] = receiver[i] / truth[i] @@ -296,6 +285,17 @@ func TestScale(t *testing.T) { AreSlicesEqual(t, truth, s, "Bad scaling") } +func TestSpan(t *testing.T) { + receiver := make([]float64, 5) + truth := []float64{1, 2, 3, 4, 5} + Span(receiver, 1, 5) + AreSlicesEqual(t, truth, receiver, "Improper linspace") + receiver = make([]float64, 6) + truth = []float64{0, 0.2, 0.4, 0.6, 0.8, 1.0} + Span(receiver, 0, 1) + AreSlicesEqual(t, truth, receiver, "Improper linspace") +} + func TestSub(t *testing.T) { s := []float64{3, 4, 1, 7, 5} v := []float64{1, 2, 3, 4, 5} @@ -443,34 +443,34 @@ func BenchmarkAddFourHuge(b *testing.B) { benchmarkAdd(b, s, t, u, v) } -func benchmarkLogsumexp(b *testing.B, s []float64) { +func benchmarkLogSumExp(b *testing.B, s []float64) { b.StartTimer() for i := 0; i < b.N; i++ { _ = LogSumExp(s) } } -func BenchmarkLogsumexpSmall(b *testing.B) { +func BenchmarkLogSumExpSmall(b *testing.B) { b.StopTimer() s := RandomSlice(SMALL) - benchmarkLogsumexp(b, s) + benchmarkLogSumExp(b, s) } -func BenchmarkLogsumexpMed(b *testing.B) { +func BenchmarkLogSumExpMed(b *testing.B) { b.StopTimer() s := RandomSlice(MEDIUM) - benchmarkLogsumexp(b, s) + benchmarkLogSumExp(b, s) } -func BenchmarkLogsumexpLarge(b *testing.B) { +func BenchmarkLogSumExpLarge(b *testing.B) { b.StopTimer() s := RandomSlice(LARGE) - benchmarkLogsumexp(b, s) + benchmarkLogSumExp(b, s) } -func BenchmarkLogsumexpHuge(b *testing.B) { +func BenchmarkLogSumExpHuge(b *testing.B) { b.StopTimer() s := RandomSlice(HUGE) - benchmarkLogsumexp(b, s) + benchmarkLogSumExp(b, s) } func benchmarkDot(b *testing.B, s1 []float64, s2 []float64) {