Cumulative Product

The cumulative product function is an operation regularly used for tracking cumulative effects of sequential operations. A common usecase for this operation is calculating cumulative compound interest and analyzing the cumulative returns of investments over time. Additionally, it can be used for implementing recursive algorithms for tracking cumulative effects of sequential operations.

CumProd(x1,x2,,xn)=(x1,x1x2,x1x2x3,,i=1nxi)\operatorname{CumProd}(x_1, x_2, \ldots, x_n) = (x_1, x_1x_2, x_1x_2x_3, \ldots, \prod_{i=1}^n x_i)

To implement the CumProd 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 outputs.

function CumProd(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) CumProd(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.CumProd(dst, f)
	return toBigArray(dst, mul), nil, nil
}

Last updated