gitee.com/quant1x/num@v0.3.2/cumsum.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x32"
     5  	"gitee.com/quant1x/num/x64"
     6  	"slices"
     7  )
     8  
     9  // CumSum 计算累和
    10  func CumSum[T Number](x []T) []T {
    11  	return UnaryOperations(x, x32.CumSum, x64.CumSum, __go_cumsum[T])
    12  }
    13  
    14  func __go_cumsum[T Number](x []T) []T {
    15  	d := slices.Clone(x)
    16  	sum := T(0)
    17  	for i := 0; i < len(d); i++ {
    18  		sum += d[i]
    19  		d[i] = sum
    20  	}
    21  	return d
    22  }