Cumulative Sum

The cumulative sum function is frequently used for calculating cumulative gains or losses over a field of numbers. Various applications use this to compute running totals for their applications logic. The formal definition is provided below.

CumSum(x1,x2,,xn)=(x1,x1+x2,x1+x2+x3,,i=1nxi)\operatorname{CumSum}(x_1, x_2, \ldots, x_n) = (x_1, x_1+x_2, x_1+x_2+x_3, \ldots, \sum_{i=1}^n x_i)

To implement the CumSum function, see the method signature below. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to vals and output.

function CumSum(int256[] calldata vals, uint8 precision) external returns (int256[] memory);

Under the hood, the following functions are utilized directly in the Neural client.

func (con *NeuralMath) CumSum(gas *big.Int, vals []*big.Int, precision uint8) ([]*big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}

	f, err := toFloats(vals, mul)
	if err != nil {
		return nil, err, nil
	}
	dst := make([]float64, len(vals))
	floats.CumSum(dst, f)
	return toBigArray(dst, mul), nil, nil
}

Last updated