floats: Implement ScaleTo (#765)

Fixes #737
This commit is contained in:
Brendan Tracey
2018-12-14 18:46:30 +00:00
committed by GitHub
parent 9d66d7e8f5
commit 004553317c
2 changed files with 27 additions and 0 deletions

View File

@@ -809,6 +809,17 @@ func Scale(c float64, dst []float64) {
} }
} }
// ScaleTo multiplies the elements in s by c and stores the result in dst.
func ScaleTo(dst []float64, c float64, s []float64) []float64 {
if len(dst) != len(s) {
panic("floats: lengths of slices do not match")
}
if len(dst) > 0 {
f64.ScalUnitaryTo(dst, c, s)
}
return dst
}
// Span returns a set of N equally spaced points between l and u, where N // 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 equal to the length of the destination. The first element of the destination
// is l, the final element of the destination is u. // is l, the final element of the destination is u.

View File

@@ -1341,6 +1341,22 @@ func TestScale(t *testing.T) {
areSlicesEqual(t, truth, s, "Bad scaling") areSlicesEqual(t, truth, s, "Bad scaling")
} }
func TestScaleTo(t *testing.T) {
s := []float64{3, 4, 1, 7, 5}
sCopy := make([]float64, len(s))
copy(sCopy, s)
c := 5.0
truth := []float64{15, 20, 5, 35, 25}
dst := make([]float64, len(s))
ScaleTo(dst, c, s)
if !Same(dst, truth) {
t.Errorf("Scale to does not match. Got %v, want %v", dst, truth)
}
if !Same(s, sCopy) {
t.Errorf("Source modified during call. Got %v, want %v", s, sCopy)
}
}
func TestSpan(t *testing.T) { func TestSpan(t *testing.T) {
receiver1 := make([]float64, 5) receiver1 := make([]float64, 5)
truth := []float64{1, 2, 3, 4, 5} truth := []float64{1, 2, 3, 4, 5}