go-chart/sequence/sequence.go

121 lines
2.3 KiB
Go
Raw Normal View History

2017-04-30 06:12:39 +02:00
package sequence
2016-07-30 03:24:25 +02:00
2017-04-29 01:07:36 +02:00
import "math"
2016-07-30 03:24:25 +02:00
2017-04-30 06:12:39 +02:00
// Provider is a provider for values for a sequence.
type Provider interface {
2017-04-29 01:07:36 +02:00
Len() int
GetValue(int) float64
2016-07-30 03:24:25 +02:00
}
2017-04-30 06:12:39 +02:00
// Seq is a utility wrapper for sequence providers.
type Seq struct {
Provider
2016-07-30 03:24:25 +02:00
}
2017-04-30 09:39:38 +02:00
// Array enumerates the sequence into a slice.
func (s Seq) Array() (output []float64) {
output = make([]float64, s.Len())
for i := 0; i < s.Len(); i++ {
output[i] = s.GetValue(i)
}
return
}
2017-04-29 01:07:36 +02:00
// Each applies the `mapfn` to all values in the value provider.
2017-04-30 06:12:39 +02:00
func (s Seq) Each(mapfn func(int, float64)) {
2017-04-29 01:07:36 +02:00
for i := 0; i < s.Len(); i++ {
mapfn(i, s.GetValue(i))
2016-08-01 01:54:09 +02:00
}
}
2017-04-29 01:07:36 +02:00
// Map applies the `mapfn` to all values in the value provider,
// returning a new sequence.
2017-04-30 06:12:39 +02:00
func (s Seq) Map(mapfn func(i int, v float64) float64) Seq {
2017-04-29 01:07:36 +02:00
output := make([]float64, s.Len())
for i := 0; i < s.Len(); i++ {
mapfn(i, s.GetValue(i))
2016-07-30 03:24:25 +02:00
}
2017-04-30 06:12:39 +02:00
return Seq{Array(output)}
2016-07-30 03:24:25 +02:00
}
2016-08-01 01:54:09 +02:00
2017-04-30 06:12:39 +02:00
// FoldLeft collapses a sequence from left to right.
func (s Seq) FoldLeft(mapfn func(i int, v0, v float64) float64) (v0 float64) {
if s.Len() == 0 {
return 0
}
2017-04-30 08:17:28 +02:00
if s.Len() == 1 {
return s.GetValue(0)
}
2017-04-30 06:12:39 +02:00
v0 = s.GetValue(0)
for i := 1; i < s.Len(); i++ {
v0 = mapfn(i, v0, s.GetValue(i))
}
return
}
// FoldRight collapses a sequence from right to left.
func (s Seq) FoldRight(mapfn func(i int, v0, v float64) float64) (v0 float64) {
if s.Len() == 0 {
return 0
}
2017-04-30 08:17:28 +02:00
if s.Len() == 1 {
return s.GetValue(0)
}
v0 = s.GetValue(s.Len() - 1)
for i := s.Len() - 2; i >= 0; i-- {
2017-04-30 06:12:39 +02:00
v0 = mapfn(i, v0, s.GetValue(i))
}
return
}
// Sum adds all the elements of a series together.
func (s Seq) Sum() (accum float64) {
2017-04-29 01:07:36 +02:00
if s.Len() == 0 {
return 0
2016-08-01 01:54:09 +02:00
}
2017-04-29 01:07:36 +02:00
for i := 0; i < s.Len(); i++ {
accum += s.GetValue(i)
2016-08-01 01:54:09 +02:00
}
2017-04-30 06:12:39 +02:00
return
}
// Average returns the float average of the values in the buffer.
func (s Seq) Average() float64 {
if s.Len() == 0 {
return 0
}
return s.Sum() / float64(s.Len())
2016-08-01 01:54:09 +02:00
}
2017-04-29 01:07:36 +02:00
// Variance computes the variance of the buffer.
2017-04-30 06:12:39 +02:00
func (s Seq) Variance() float64 {
2017-04-29 01:07:36 +02:00
if s.Len() == 0 {
return 0
2016-08-01 01:54:09 +02:00
}
2016-08-01 09:50:32 +02:00
2017-04-29 01:07:36 +02:00
m := s.Average()
var variance, v float64
for i := 0; i < s.Len(); i++ {
v = s.GetValue(i)
variance += (v - m) * (v - m)
2016-08-01 09:50:32 +02:00
}
2017-04-29 01:07:36 +02:00
return variance / float64(s.Len())
2016-08-01 09:50:32 +02:00
}
2017-03-01 02:55:48 +01:00
2017-04-29 01:07:36 +02:00
// StdDev returns the standard deviation.
2017-04-30 06:12:39 +02:00
func (s Seq) StdDev() float64 {
2017-04-29 01:07:36 +02:00
if s.Len() == 0 {
return 0
2017-03-01 02:55:48 +01:00
}
2017-04-29 01:07:36 +02:00
return math.Pow(s.Variance(), 0.5)
2017-03-01 02:55:48 +01:00
}