-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalculator.go
461 lines (419 loc) · 10.2 KB
/
calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// MIT License
// Copyright 2020 Paul Greenberg (greenpau@outlook.com)
package calculator
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"unicode"
)
// Cell provides the facility to perform mathematical operations.
type Cell struct {
data []float64
sortedData []float64
length int
even bool
middleIndex int
err error
calculatedTotal bool
calculatedMean bool
calculatedMedian bool
calculatedRange bool
calculatedVariance bool
calculatedStandardDeviation bool
calculatedSortedMedian bool
calculatedMax bool
calculatedMaxWithIndices bool
calculatedMin bool
calculatedMinWithIndices bool
calculatedModes bool
Register Register
}
// Register provides the means to store calculation results.
type Register struct {
Total float64
Mean float64
Median float64
Range float64
Variance float64
StandardDeviation float64
SortedMedian float64
MaxIndices []int
MaxValue float64
MinIndices []int
MinValue float64
ModeRepeatCount int
Modes []float64
}
// Length returns Cell data element size.
func (c *Cell) Length() int {
return c.length
}
// Even return true if the number of element in the Cell is even.
func (c *Cell) Even() bool {
return c.even
}
func (c *Cell) validate() bool {
if c.err != nil {
return false
}
return true
}
// Total returns the total value of values in Cell.
func (c *Cell) Total() *Cell {
if c.calculatedTotal || c.err != nil {
return c
}
c.Register.Total = 0
for _, v := range c.data {
c.Register.Total += v
}
c.calculatedTotal = true
return c
}
// Mean returns the mean (average) value of values in Cell.
func (c *Cell) Mean() *Cell {
if c.calculatedMean || c.err != nil {
return c
}
if !c.calculatedTotal {
c.Total()
if c.err != nil {
return c
}
}
c.Register.Mean = c.Register.Total / float64(c.length)
c.calculatedMean = true
return c
}
// Variance calculates variance of the values in Cell.
func (c *Cell) Variance() *Cell {
if c.calculatedVariance || c.err != nil {
return c
}
if !c.calculatedMean {
c.Mean()
if c.err != nil {
return c
}
}
for _, i := range c.data {
r := c.Register.Mean - i
c.Register.Variance += r * r
}
c.Register.Variance /= float64(c.length)
c.calculatedVariance = true
return c
}
// Range calculates range of the values in Cell.
func (c *Cell) Range() *Cell {
if c.calculatedRange || c.err != nil {
return c
}
if !c.calculatedMax {
c.Max()
if c.err != nil {
return c
}
}
if !c.calculatedMin {
c.Min()
if c.err != nil {
return c
}
}
c.Register.Range = c.Register.MaxValue - c.Register.MinValue
c.calculatedRange = true
return c
}
// Max calculates the biggest value in Cell.
func (c *Cell) Max() *Cell {
if c.calculatedMax || c.err != nil {
return c
}
c.Register.MaxValue = c.sortedData[c.length-1]
c.calculatedMax = true
return c
}
// MaxWithIndices calculates the biggest value and associated indices in Cell.
func (c *Cell) MaxWithIndices() *Cell {
if c.calculatedMaxWithIndices || c.err != nil {
return c
}
if !c.calculatedMax {
c.Max()
if c.err != nil {
return c
}
}
c.Register.MaxIndices = []int{}
if c.length == 1 {
c.Register.MaxIndices = append(c.Register.MaxIndices, 0)
c.calculatedMaxWithIndices = true
return c
}
for i, v := range c.data {
if v == c.Register.MaxValue {
c.Register.MaxIndices = append(c.Register.MaxIndices, i)
}
}
c.calculatedMaxWithIndices = true
return c
}
// Min calculates the smallest value in Cell.
func (c *Cell) Min() *Cell {
if c.calculatedMin || c.err != nil {
return c
}
c.Register.MinValue = c.sortedData[0]
c.calculatedMax = true
return c
}
// MinWithIndices calculates the smallest value and associated indices in Cell.
func (c *Cell) MinWithIndices() *Cell {
if c.calculatedMinWithIndices || c.err != nil {
return c
}
if !c.calculatedMin {
c.Min()
if c.err != nil {
return c
}
}
c.Register.MinIndices = []int{}
if c.length == 1 {
c.Register.MinIndices = append(c.Register.MinIndices, 0)
c.calculatedMinWithIndices = true
return c
}
for i, v := range c.data {
if v == c.Register.MinValue {
c.Register.MinIndices = append(c.Register.MinIndices, i)
}
}
c.calculatedMinWithIndices = true
return c
}
// Modes calculates the values appearing most often in Cell.
func (c *Cell) Modes() *Cell {
if c.calculatedModes || c.err != nil {
return c
}
occurences := make(map[float64]int)
c.Register.Modes = []float64{}
for _, i := range c.data {
occurences[i]++
if occurences[i] > c.Register.ModeRepeatCount {
c.Register.ModeRepeatCount = occurences[i]
}
}
if len(occurences) == c.length {
c.Register.ModeRepeatCount = 0
c.calculatedModes = true
return c
}
for i, v := range occurences {
if v == c.Register.ModeRepeatCount {
c.Register.Modes = append(c.Register.Modes, i)
}
}
c.calculatedModes = true
return c
}
// Median calculates median of the values in Cell.
func (c *Cell) Median(sorted bool) *Cell {
if (!sorted && c.calculatedMedian) || (sorted && c.calculatedSortedMedian) || c.err != nil {
return c
}
if c.length == 1 {
c.Register.SortedMedian = c.data[0]
c.Register.Median = c.data[0]
c.calculatedSortedMedian = true
c.calculatedMedian = true
return c
}
if sorted {
if !c.even {
c.Register.SortedMedian = c.sortedData[c.middleIndex]
} else {
c.Register.SortedMedian = (c.sortedData[c.middleIndex] + c.sortedData[c.middleIndex-1]) / 2
}
c.calculatedSortedMedian = true
} else {
if !c.even {
c.Register.Median = c.data[c.middleIndex]
} else {
c.Register.Median = (c.data[c.middleIndex] + c.data[c.middleIndex-1]) / 2
}
c.calculatedMedian = true
}
return c
}
// StandardDeviation calculates standard deviation of the values in Cell.
func (c *Cell) StandardDeviation() *Cell {
if c.calculatedStandardDeviation || c.err != nil {
return c
}
if !c.calculatedVariance {
c.Variance()
if c.err != nil {
return c
}
}
c.Register.StandardDeviation = math.Sqrt(c.Register.Variance)
c.calculatedStandardDeviation = true
return c
}
// RunAll performs all available culculations in Cell.
func (c *Cell) RunAll() *Cell {
if valid := c.validate(); !valid {
return c
}
c.Total()
c.StandardDeviation()
c.Variance()
c.Range()
c.MaxWithIndices()
c.MinWithIndices()
c.Median(true)
c.Median(false)
c.Mean()
c.Modes()
return c
}
// Float64 returns float64 representation of the result
//func (c *Cell) Float64() float64 {
// return c.Value
//}
// NewUint64 returns an instance of Cell from uint64 array.
func NewUint64(data []uint64) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewUint32 returns an instance of Cell from uint32 array.
func NewUint32(data []uint32) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewInt64 returns an instance of Cell from int64 array.
func NewInt64(data []int64) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewInt32 returns an instance of Cell from int32 array.
func NewInt32(data []int32) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewUint returns an instance of Cell from uint array.
func NewUint(data []uint) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewInt returns an instance of Cell from int array.
func NewInt(data []int) *Cell {
arr := []float64{}
for _, i := range data {
arr = append(arr, float64(i))
}
return New(arr)
}
// NewString returns an instance of Cell from string array.
func NewString(s string) *Cell {
arr := []float64{}
for _, i := range strings.Split(s, ",") {
i := strings.TrimSpace(i)
if !strings.Contains(i, ".") {
i = i + ".0"
}
j, err := strconv.ParseFloat(i, 64)
if err != nil {
return nil
}
arr = append(arr, j)
}
return New(arr)
}
// New returns an instance of Cell from float64 array.
// If the cell does not initialize successfully, then
// this function returns nil.
func New(data []float64) *Cell {
if len(data) == 0 {
return nil
}
c := &Cell{
data: data,
length: len(data),
even: false,
}
c.sortedData = make([]float64, c.length)
copy(c.sortedData, c.data)
sort.Float64s(c.sortedData)
modInt, modFrac := math.Modf(float64(c.length))
if modFrac == 0.0 {
c.even = true
}
c.middleIndex = int(modInt / 2)
return c
}
// Failed returns true if one or more calculations failed.
func (c *Cell) Failed() bool {
if c.err != nil {
return true
}
return false
}
func addNewLines(s string, max int) string {
if len(s) > max {
var sb strings.Builder
var j int
for _, c := range s {
if unicode.IsSpace(c) {
if j > max {
sb.WriteString("\n ")
j = 2
}
}
sb.WriteRune(c)
j++
}
return sb.String()
}
return s
}
// Print returns a string with the contents of a Cell
func (c *Cell) Print() string {
maxWidth := 40
var sb strings.Builder
sb.WriteString(addNewLines(fmt.Sprintf("Data: %v", c.data), maxWidth))
sb.WriteString(fmt.Sprintf("\nTotal: %f", c.Register.Total))
sb.WriteString(fmt.Sprintf("\nMean: %f", c.Register.Mean))
sb.WriteString(fmt.Sprintf("\nMedian: %f", c.Register.Median))
sb.WriteString(fmt.Sprintf("\nSorted Median: %f", c.Register.SortedMedian))
sb.WriteString(fmt.Sprintf("\nMax: %f", c.Register.MaxValue))
sb.WriteString(addNewLines(fmt.Sprintf("\nMax Indices: %v", c.Register.MaxIndices), maxWidth))
sb.WriteString(fmt.Sprintf("\nMin: %f", c.Register.MinValue))
sb.WriteString(addNewLines(fmt.Sprintf("\nMin Indices: %v", c.Register.MinIndices), maxWidth))
sb.WriteString(fmt.Sprintf("\nVariance: %f", c.Register.Variance))
sb.WriteString(fmt.Sprintf("\nStandard Deviation: %f", c.Register.StandardDeviation))
sb.WriteString(addNewLines(fmt.Sprintf("\nModes: %v", c.Register.Modes), maxWidth))
sb.WriteString(fmt.Sprintf("\nMode Repeat Count: %d", c.Register.ModeRepeatCount))
return sb.String()
}