Added ApplyFunc, couple of other minor changes

This commit is contained in:
btracey
2013-06-05 17:13:37 -07:00
parent 8733cc651d
commit c0adf91573

View File

@@ -2,6 +2,35 @@ package sliceops
import "math" import "math"
// TODO: Should there be a standardized 'Unequal lengths' error?
type InsufficientElements struct{}
func (i InsufficientElements) Error() string {
return "Insufficient elements found"
}
// Returns the element-wise sum of the last n slices
// and puts them in place into the first argument.
// 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. If no slices are input,
// the receiver is unchanged.
func Add(receiver []float64, slices ...[]float64) {
if len(slices) == 0 {
return
}
for i, val := range slices[0] {
receiver[i] = val
}
for i := 1; i < len(slices); i++ {
for j, val := range slices[i] {
receiver[j] += val
}
}
return
}
// Adds a constant to all of the values in s // Adds a constant to all of the values in s
func Addconst(s []float64, c float64) { func Addconst(s []float64, c float64) {
for i := range s { for i := range s {
@@ -9,6 +38,14 @@ func Addconst(s []float64, c float64) {
} }
} }
// Applies a function (math.Exp, math.Sin, etc.) to every element
// of the slice
func ApplyFunc(s []float64, f func(float64) float64) {
for i, val := range s {
s[i] = f(val)
}
}
// Finds the cumulative product of the first i elements in // Finds the cumulative product of the first i elements in
// s and puts them in place into the ith element of the // s and puts them in place into the ith element of the
// receiver. Assumes receiver is at least as long as s // receiver. Assumes receiver is at least as long as s
@@ -36,27 +73,6 @@ func Cumsum(receiver, s []float64) {
} }
} }
// Returns the element-wise sum of the last n slices
// and puts them in place into the first argument.
// 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. If no slices are input,
// the receiver is unchanged.
func Elemsum(receiver []float64, slices ...[]float64) {
if len(slices) == 0 {
return
}
for i, val := range slices[0] {
receiver[i] = val
}
for i := 1; i < len(slices); i++ {
for j, val := range slices[i] {
receiver[j] += val
}
}
return
}
// Returns false if |s1[i] - s2[i]| > tol for any i. // Returns false if |s1[i] - s2[i]| > tol for any i.
// Assumes that the slices are of equal length. If this // Assumes that the slices are of equal length. If this
// is in doubt it should be checked with HasEqLen // is in doubt it should be checked with HasEqLen
@@ -100,17 +116,11 @@ func Find(s []float64, f func(float64) bool) (inds []int) {
return inds return inds
} }
type InsufficientElements struct{}
func (i InsufficientElements) Error() string {
return "Insufficient elements found"
}
// Applies a function returning a boolean to the elements of the slice // Applies a function returning a boolean to the elements of the slice
// and returns a list of the first k indices for which the value is true. // and returns a list of the first k indices for which the value is true.
// If there are fewer than k indices for which the value is true, it returns // If there are fewer than k indices for which the value is true, it returns
// the found indices and an error. // the found indices and an error.
func Findfirst(s []float64, f func(float64) bool, k int) (inds []int, err error) { func FindFirst(s []float64, f func(float64) bool, k int) (inds []int, err error) {
count := 0 count := 0
inds = make([]int, 0, k) inds = make([]int, 0, k)
for i, val := range s { for i, val := range s {