From 004553317c78aa27aeff99178a5a6bfe255b68a8 Mon Sep 17 00:00:00 2001 From: Brendan Tracey Date: Fri, 14 Dec 2018 18:46:30 +0000 Subject: [PATCH] floats: Implement ScaleTo (#765) Fixes #737 --- floats/floats.go | 11 +++++++++++ floats/floats_test.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/floats/floats.go b/floats/floats.go index 591c1349..ae004a62 100644 --- a/floats/floats.go +++ b/floats/floats.go @@ -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 // is equal to the length of the destination. The first element of the destination // is l, the final element of the destination is u. diff --git a/floats/floats_test.go b/floats/floats_test.go index 270ddff4..4bf463f7 100644 --- a/floats/floats_test.go +++ b/floats/floats_test.go @@ -1341,6 +1341,22 @@ func TestScale(t *testing.T) { 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) { receiver1 := make([]float64, 5) truth := []float64{1, 2, 3, 4, 5}