Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slope property and Plot function to Equation struct #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions piecewiseLinearApprox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)}
Expand All @@ -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
}
17 changes: 17 additions & 0 deletions piecewiseLinearApprox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ type Pair struct {
type Equation struct {
Expression string
Interval Pair
Slope float64
Plot func(float64) float64
}