File: cumulative_sum.go

package info (click to toggle)
golang-github-montanaflynn-stats 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: makefile: 27
file content (21 lines) | stat: -rw-r--r-- 380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package stats

// CumulativeSum calculates the cumulative sum of the input slice
func CumulativeSum(input Float64Data) ([]float64, error) {

	if input.Len() == 0 {
		return Float64Data{}, EmptyInput
	}

	cumSum := make([]float64, input.Len())

	for i, val := range input {
		if i == 0 {
			cumSum[i] = val
		} else {
			cumSum[i] = cumSum[i-1] + val
		}
	}

	return cumSum, nil
}