Changed Linspace and Logspace to Span and LogSpan, fixed capitalization issues with BenchmarkLogsumexp

This commit is contained in:
btracey
2013-07-23 23:42:45 -07:00
parent 2baac5a098
commit 8e17fa4e3a
2 changed files with 37 additions and 37 deletions

View File

@@ -143,25 +143,13 @@ func FindFirst(s []float64, f func(float64) bool, k int) (inds []int, err error)
return inds, InsufficientElements{} return inds, InsufficientElements{}
} }
// Linspace returns a set of N equally spaced points 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
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
// is equal to the length of the destination. The first element of the destination // 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 // 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 // length < 2. Note that this call will return NaNs if l or u are negative, and
// zeros if l or u is zero. // zeros if l or u is zero.
func Logspace(dst []float64, l, u float64) { func LogSpan(dst []float64, l, u float64) {
Linspace(dst, math.Log(l), math.Log(u)) Span(dst, math.Log(l), math.Log(u))
Apply(dst, math.Exp) 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 // Sub subtracts, element-wise, the first argument from the second. Assumes
// the lengths of s and t match (can be tested with EqLen) // the lengths of s and t match (can be tested with EqLen)
func Sub(s, t []float64) { func Sub(s, t []float64) {

View File

@@ -158,21 +158,10 @@ func TestFindFirst(t *testing.T) {
} }
} }
func TestLinspace(t *testing.T) { func TestLogSpan(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) {
receiver := make([]float64, 6) receiver := make([]float64, 6)
truth := []float64{0.001, 0.01, 0.1, 1, 10, 100} 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) tst := make([]float64, 6)
for i := range truth { for i := range truth {
tst[i] = receiver[i] / truth[i] tst[i] = receiver[i] / truth[i]
@@ -296,6 +285,17 @@ func TestScale(t *testing.T) {
AreSlicesEqual(t, truth, s, "Bad scaling") 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) { func TestSub(t *testing.T) {
s := []float64{3, 4, 1, 7, 5} s := []float64{3, 4, 1, 7, 5}
v := []float64{1, 2, 3, 4, 5} v := []float64{1, 2, 3, 4, 5}
@@ -443,34 +443,34 @@ func BenchmarkAddFourHuge(b *testing.B) {
benchmarkAdd(b, s, t, u, v) benchmarkAdd(b, s, t, u, v)
} }
func benchmarkLogsumexp(b *testing.B, s []float64) { func benchmarkLogSumExp(b *testing.B, s []float64) {
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = LogSumExp(s) _ = LogSumExp(s)
} }
} }
func BenchmarkLogsumexpSmall(b *testing.B) { func BenchmarkLogSumExpSmall(b *testing.B) {
b.StopTimer() b.StopTimer()
s := RandomSlice(SMALL) s := RandomSlice(SMALL)
benchmarkLogsumexp(b, s) benchmarkLogSumExp(b, s)
} }
func BenchmarkLogsumexpMed(b *testing.B) { func BenchmarkLogSumExpMed(b *testing.B) {
b.StopTimer() b.StopTimer()
s := RandomSlice(MEDIUM) s := RandomSlice(MEDIUM)
benchmarkLogsumexp(b, s) benchmarkLogSumExp(b, s)
} }
func BenchmarkLogsumexpLarge(b *testing.B) { func BenchmarkLogSumExpLarge(b *testing.B) {
b.StopTimer() b.StopTimer()
s := RandomSlice(LARGE) s := RandomSlice(LARGE)
benchmarkLogsumexp(b, s) benchmarkLogSumExp(b, s)
} }
func BenchmarkLogsumexpHuge(b *testing.B) { func BenchmarkLogSumExpHuge(b *testing.B) {
b.StopTimer() b.StopTimer()
s := RandomSlice(HUGE) s := RandomSlice(HUGE)
benchmarkLogsumexp(b, s) benchmarkLogSumExp(b, s)
} }
func benchmarkDot(b *testing.B, s1 []float64, s2 []float64) { func benchmarkDot(b *testing.B, s1 []float64, s2 []float64) {