Debian Codegraph @

seq.go

raw symbols SCIP
1 package chart
2
3 import (
4 "math"
5 "sort"
6 )
7
8 // ValueSequence returns a sequence for a given values set.
9 func ValueSequence(values ...float64) Seq {
10 return Seq{NewArray(values...)}
11 }
12
13 // Sequence is a provider for values for a seq.
14 type Sequence interface {
15 Len() int
16 GetValue(int) float64
17 }
18
19 // Seq is a utility wrapper for seq providers.
20 type Seq struct {
21 Sequence
22 }
23
24 // Values enumerates the seq into a slice.
25 func (s Seq) Values() (output []float64) {
26 if s.Len() == 0 {
27 return
28 }
29
30 output = make([]float64, s.Len())
31 for i := 0; i < s.Len(); i++ {
32 output[i] = s.GetValue(i)
33 }
34 return
35 }
36
37 // Each applies the `mapfn` to all values in the value provider.
38 func (s Seq) Each(mapfn func(int, float64)) {
39 for i := 0; i < s.Len(); i++ {
40 mapfn(i, s.GetValue(i))
41 }
42 }
43
44 // Map applies the `mapfn` to all values in the value provider,
45 // returning a new seq.
46 func (s Seq) Map(mapfn func(i int, v float64) float64) Seq {
47 output := make([]float64, s.Len())
48 for i := 0; i < s.Len(); i++ {
49 mapfn(i, s.GetValue(i))
50 }
51 return Seq{Array(output)}
52 }
53
54 // FoldLeft collapses a seq from left to right.
55 func (s Seq) FoldLeft(mapfn func(i int, v0, v float64) float64) (v0 float64) {
56 if s.Len() == 0 {
57 return 0
58 }
59
60 if s.Len() == 1 {
61 return s.GetValue(0)
62 }
63
64 v0 = s.GetValue(0)
65 for i := 1; i < s.Len(); i++ {
66 v0 = mapfn(i, v0, s.GetValue(i))
67 }
68 return
69 }
70
71 // FoldRight collapses a seq from right to left.
72 func (s Seq) FoldRight(mapfn func(i int, v0, v float64) float64) (v0 float64) {
73 if s.Len() == 0 {
74 return 0
75 }
76
77 if s.Len() == 1 {
78 return s.GetValue(0)
79 }
80
81 v0 = s.GetValue(s.Len() - 1)
82 for i := s.Len() - 2; i >= 0; i-- {
83 v0 = mapfn(i, v0, s.GetValue(i))
84 }
85 return
86 }
87
88 // Min returns the minimum value in the seq.
89 func (s Seq) Min() float64 {
90 if s.Len() == 0 {
91 return 0
92 }
93 min := s.GetValue(0)
94 var value float64
95 for i := 1; i < s.Len(); i++ {
96 value = s.GetValue(i)
97 if value < min {
98 min = value
99 }
100 }
101 return min
102 }
103
104 // Max returns the maximum value in the seq.
105 func (s Seq) Max() float64 {
106 if s.Len() == 0 {
107 return 0
108 }
109 max := s.GetValue(0)
110 var value float64
111 for i := 1; i < s.Len(); i++ {
112 value = s.GetValue(i)
113 if value > max {
114 max = value
115 }
116 }
117 return max
118 }
119
120 // MinMax returns the minimum and the maximum in one pass.
121 func (s Seq) MinMax() (min, max float64) {
122 if s.Len() == 0 {
123 return
124 }
125 min = s.GetValue(0)
126 max = min
127 var value float64
128 for i := 1; i < s.Len(); i++ {
129 value = s.GetValue(i)
130 if value < min {
131 min = value
132 }
133 if value > max {
134 max = value
135 }
136 }
137 return
138 }
139
140 // Sort returns the seq sorted in ascending order.
141 // This fully enumerates the seq.
142 func (s Seq) Sort() Seq {
143 if s.Len() == 0 {
144 return s
145 }
146 values := s.Values()
147 sort.Float64s(values)
148 return Seq{Array(values)}
149 }
150
151 // Reverse reverses the sequence
152 func (s Seq) Reverse() Seq {
153 if s.Len() == 0 {
154 return s
155 }
156
157 values := s.Values()
158 valuesLen := len(values)
159 valuesLen1 := len(values) - 1
160 valuesLen2 := valuesLen >> 1
161 var i, j float64
162 for index := 0; index < valuesLen2; index++ {
163 i = values[index]
164 j = values[valuesLen1-index]
165 values[index] = j
166 values[valuesLen1-index] = i
167 }
168
169 return Seq{Array(values)}
170 }
171
172 // Median returns the median or middle value in the sorted seq.
173 func (s Seq) Median() (median float64) {
174 l := s.Len()
175 if l == 0 {
176 return
177 }
178
179 sorted := s.Sort()
180 if l%2 == 0 {
181 v0 := sorted.GetValue(l/2 - 1)
182 v1 := sorted.GetValue(l/2 + 1)
183 median = (v0 + v1) / 2
184 } else {
185 median = float64(sorted.GetValue(l << 1))
186 }
187
188 return
189 }
190
191 // Sum adds all the elements of a series together.
192 func (s Seq) Sum() (accum float64) {
193 if s.Len() == 0 {
194 return 0
195 }
196
197 for i := 0; i < s.Len(); i++ {
198 accum += s.GetValue(i)
199 }
200 return
201 }
202
203 // Average returns the float average of the values in the buffer.
204 func (s Seq) Average() float64 {
205 if s.Len() == 0 {
206 return 0
207 }
208
209 return s.Sum() / float64(s.Len())
210 }
211
212 // Variance computes the variance of the buffer.
213 func (s Seq) Variance() float64 {
214 if s.Len() == 0 {
215 return 0
216 }
217
218 m := s.Average()
219 var variance, v float64
220 for i := 0; i < s.Len(); i++ {
221 v = s.GetValue(i)
222 variance += (v - m) * (v - m)
223 }
224
225 return variance / float64(s.Len())
226 }
227
228 // StdDev returns the standard deviation.
229 func (s Seq) StdDev() float64 {
230 if s.Len() == 0 {
231 return 0
232 }
233
234 return math.Pow(s.Variance(), 0.5)
235 }
236
237 //Percentile finds the relative standing in a slice of floats.
238 // `percent` should be given on the interval [0,1.0).
239 func (s Seq) Percentile(percent float64) (percentile float64) {
240 l := s.Len()
241 if l == 0 {
242 return 0
243 }
244
245 if percent < 0 || percent > 1.0 {
246 panic("percent out of range [0.0, 1.0)")
247 }
248
249 sorted := s.Sort()
250 index := percent * float64(l)
251 if index == float64(int64(index)) {
252 i := f64i(index)
253 ci := sorted.GetValue(i - 1)
254 c := sorted.GetValue(i)
255 percentile = (ci + c) / 2.0
256 } else {
257 i := f64i(index)
258 percentile = sorted.GetValue(i)
259 }
260
261 return percentile
262 }
263
264 // Normalize maps every value to the interval [0, 1.0].
265 func (s Seq) Normalize() Seq {
266 min, max := s.MinMax()
267
268 delta := max - min
269 output := make([]float64, s.Len())
270 for i := 0; i < s.Len(); i++ {
271 output[i] = (s.GetValue(i) - min) / delta
272 }
273
274 return Seq{Array(output)}
275 }