Changed order of signature in Nearest

This commit is contained in:
btracey
2013-07-26 11:00:42 -07:00
parent a45071abd3
commit 3e9dc87057
2 changed files with 17 additions and 16 deletions

View File

@@ -30,22 +30,22 @@ func Add(dst []float64, slices ...[]float64) []float64 {
} }
// AddConst adds a constant to all of the values in s // AddConst adds a constant to all of the values in s
func AddConst(s []float64, c float64) { func AddConst(c float64, s []float64) {
for i := range s { for i := range s {
s[i] += c s[i] += c
} }
} }
// ApplyFunc applies a function (math.Exp, math.Sin, etc.) to every element // ApplyFunc applies a function f (math.Exp, math.Sin, etc.) to every element
// of the slice // of the slice s
func Apply(s []float64, f func(float64) float64) { func Apply(f func(float64) float64, s []float64) {
for i, val := range s { for i, val := range s {
s[i] = f(val) s[i] = f(val)
} }
} }
// Count counts the number of elements in s for which f is true // Count counts the number of elements in s for which f is true
func Count(s []float64, f func(float64) bool) int { func Count(f func(float64) bool, s []float64) int {
var n int var n int
for _, val := range s { for _, val := range s {
if f(val) { if f(val) {
@@ -134,7 +134,7 @@ func EqLen(slices ...[]float64) bool {
// found indices to inds. // found indices to inds.
// If k > 0 and there are fewer than k elements in s satisfying f, // If k > 0 and there are fewer than k elements in s satisfying f,
// all of the found elements will be returned along with an error // all of the found elements will be returned along with an error
func Find(inds []int, k int, s []float64, f func(float64) bool) ([]int, error) { func Find(inds []int, f func(float64) bool, s []float64, k int) ([]int, error) {
// inds is also returned to allow for calling with nil // inds is also returned to allow for calling with nil
@@ -179,7 +179,7 @@ func Find(inds []int, k int, s []float64, f func(float64) bool) ([]int, error) {
// will return all zeros if l or u is zero. // will return all zeros if l or u is zero.
func LogSpan(dst []float64, l, u float64) []float64 { func LogSpan(dst []float64, l, u float64) []float64 {
Span(dst, math.Log(l), math.Log(u)) Span(dst, math.Log(l), math.Log(u))
Apply(dst, math.Exp) Apply(math.Exp, dst)
return dst return dst
} }
@@ -232,11 +232,11 @@ func Min(s []float64) (min float64, ind int) {
return min, ind return min, ind
} }
// Nearest returns the index of the element of s whose value is // Nearest returns the index of the element in s
// nearest to v. If several such indices exist, the lowest index // whose value is nearest to v. If several such
// is returned // indices exist, the lowest index is returned
// TODO: Add test // TODO: Add test
func Nearest(v float64, s []float64) (ind int) { func Nearest(s []float64, v float64) (ind int) {
dist := math.Abs(v - s[0]) dist := math.Abs(v - s[0])
ind = 0 ind = 0
for i, val := range s { for i, val := range s {
@@ -254,7 +254,7 @@ func Nearest(v float64, s []float64) (ind int) {
// and bounds l and u // and bounds l and u
// Assumes u > l // Assumes u > l
// TODO: Add in tests // TODO: Add in tests
func NearestInSpan(v float64, n int, l, u float64) int { func NearestInSpan(n int, l, u float64, v float64) int {
return int((v-l)*float64(n-1)/(u-l) + 0.5) return int((v-l)*float64(n-1)/(u-l) + 0.5)
} }
@@ -301,7 +301,7 @@ func Prod(s []float64) (prod float64) {
} }
// Scale multiplies every element in s by c // Scale multiplies every element in s by c
func Scale(s []float64, c float64) { func Scale(c float64, s []float64) {
for i := range s { for i := range s {
s[i] *= c s[i] *= c
} }
@@ -337,7 +337,7 @@ func Sub(s, t []float64) {
// SubDst subtracts, element-wise, the first argument from the second and // SubDst subtracts, element-wise, the first argument from the second and
// store the result in destination. Assumes the lengths of s and t match // store the result in destination. Assumes the lengths of s and t match
// (can be tested with EqLen) // (can be tested with EqLen)
func SubDst(dst, s, t []float64) []float64 { func SubTo(dst, s, t []float64) []float64 {
if len(s) != len(t) { if len(s) != len(t) {
panic("floats: length of subtractor and subtractee do not match") panic("floats: length of subtractor and subtractee do not match")
} }

View File

@@ -283,8 +283,9 @@ func TestMin(t *testing.T) {
} }
func TestNearest(t *testing.T) { func TestNearest(t *testing.T) {
//s := []float64{3, 5, 6.2, 6.2, 8} s := []float64{3, 5, 6.2, 6.2, 8}
//ind := Nearest(s, ) ind := Nearest(2.0, s)
if
} }
func TestNorm(t *testing.T) { func TestNorm(t *testing.T) {