From 9cdd7bd0a1c0c69ce6c2a0b348b1240bc08adbef Mon Sep 17 00:00:00 2001 From: Craig Campbell Date: Mon, 17 Jul 2017 01:52:59 -0400 Subject: [PATCH 1/2] Add Slope and Plot properties to Equation struct --- piecewiseLinearApprox.go | 16 ++++++++++++---- piecewiseLinearApprox_test.go | 17 +++++++++++++++++ types.go | 2 ++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/piecewiseLinearApprox.go b/piecewiseLinearApprox.go index a6cb748..7ba7817 100644 --- a/piecewiseLinearApprox.go +++ b/piecewiseLinearApprox.go @@ -5,6 +5,12 @@ import ( "strconv" ) +func getPlotFunction(slope float64, beginX float64, beginY float64) func(float64) float64 { + return func(x float64) float64 { + return slope * (x - beginX) + beginY + } +} + func PiecewiseLinearApprox(timeSeries []Pair, tollerance float64) []Equation { var expression string var interval Pair @@ -28,13 +34,14 @@ func PiecewiseLinearApprox(timeSeries []Pair, tollerance float64) []Equation { lower = lowerPrime upper = upperPrime } else { + slope := (upper+lower)/2 expression = "y = " + - strconv.FormatFloat((upper+lower)/2, 'g', -1, 64) + "(t - " + + strconv.FormatFloat(slope, 'g', -1, 64) + "(t - " + strconv.FormatFloat(begin.X, 'g', -1, 64) + ") + " + strconv.FormatFloat(begin.Y, 'g', -1, 64) interval = Pair{start, end} - equations = append(equations, Equation{expression, interval}) + equations = append(equations, Equation{expression, interval, slope, getPlotFunction(slope, begin.X, begin.Y)}) begin = Pair{timeSeries[i].X, ValueAtTime(lower, upper, timeSeries[i].X, begin)} @@ -47,13 +54,14 @@ func PiecewiseLinearApprox(timeSeries []Pair, tollerance float64) []Equation { } end = timeSeries[len(timeSeries)-1].X + slope := (upper+lower)/2 expression = "y = " + - strconv.FormatFloat((upper+lower)/2, 'g', -1, 64) + "(t - " + + strconv.FormatFloat(slope, 'g', -1, 64) + "(t - " + strconv.FormatFloat(begin.X, 'g', -1, 64) + ") + " + strconv.FormatFloat(begin.Y, 'g', -1, 64) interval = Pair{start, end} - equations = append(equations, Equation{expression, interval}) + equations = append(equations, Equation{expression, interval, slope, getPlotFunction(slope, begin.X, begin.Y)}) return equations } diff --git a/piecewiseLinearApprox_test.go b/piecewiseLinearApprox_test.go index bf431e6..0e50c3a 100644 --- a/piecewiseLinearApprox_test.go +++ b/piecewiseLinearApprox_test.go @@ -165,6 +165,23 @@ func TestPiecewiseLinearApprox(t *testing.T) { } } + // test the first equation + equation1 := solution[0] + if equation1.Slope != 2 { + t.Errorf("Slope returned %g expecting 2", equation1.Slope) + } + + // try plotting a point + value := equation1.Plot(0) + if value != 0 { + t.Errorf("Plot returned %g expecting 0", value) + } + + value = equation1.Plot(5) + if value != 10 { + t.Errorf("Plot returned %g expecting 10", value) + } + solution2 := piecewiseLinearApproximation.PiecewiseLinearApprox(timeSeries2, tollerance2) for i := 0; i < len(equations2); i++ { expression2 := solution2[i].Expression diff --git a/types.go b/types.go index b8cd38e..4e8cf39 100644 --- a/types.go +++ b/types.go @@ -11,4 +11,6 @@ type Pair struct { type Equation struct { Expression string Interval Pair + Slope float64 + Plot func(float64) float64 } From 5d24884f60c0000b41297bae1d7cc6e56f06dad6 Mon Sep 17 00:00:00 2001 From: Craig Campbell Date: Mon, 17 Jul 2017 01:56:07 -0400 Subject: [PATCH 2/2] Use tabs here to match other styles For some reason my editor was using spaces --- types.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index 4e8cf39..a0f4d85 100644 --- a/types.go +++ b/types.go @@ -11,6 +11,6 @@ type Pair struct { type Equation struct { Expression string Interval Pair - Slope float64 - Plot func(float64) float64 + Slope float64 + Plot func(float64) float64 }