library MathUint {
    function mul(uint a, uint b) internal pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }
    function sub(uint a, uint b) internal pure returns (uint) {
        require(b <= a);
        return a - b;
    }
    function add(uint a, uint b) internal pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }
    function tolerantSub(uint a, uint b) internal pure returns (uint c) {
        return (a >= b) ? a - b : 0;
    }
    /// @dev calculate the square of Coefficient of Variation (CV)
    /// https://en.wikipedia.org/wiki/Coefficient_of_variation
    function cvsquare(
        uint[] arr,
        uint scale
        )
        internal
        pure
        returns (uint)
    {
        uint len = arr.length;
        require(len > 1);
        require(scale > 0);
        uint avg = 0;
        for (uint i = 0; i < len; i++) {
            avg += arr[i];
        }
        avg = avg / len;
        if (avg == 0) {
            return 0;
        }
        uint cvs = 0;
        uint s;
        uint item;
        for (i = 0; i < len; i++) {
            item = arr[i];
            s = item > avg ? item - avg : avg - item;
            cvs += mul(s, s);
        }
        return ((mul(mul(cvs, scale), scale) / avg) / avg) / (len - 1);
    }
}
/// @title Utility Functions for uint
/// @author Daniel Wang - <daniel@loopring.org>
function cvsquare(
    uint[] arr,
    uint scale
    )
    internal
    pure
    returns (uint)
{
    uint len = arr.length;
    require(len > 1);
    require(scale > 0);
    uint avg = 0;
    for (uint i = 0; i < len; i++) {
        avg += arr[i];
    }
    avg = avg / len;
    if (avg == 0) {
        return 0;
    }
    uint cvs = 0;
    uint s;
    uint item;
    for (i = 0; i < len; i++) {
        item = arr[i];
        s = item > avg ? item - avg : avg - item;
        cvs += mul(s, s);
    }
    return ((mul(mul(cvs, scale), scale) / avg) / avg) / (len - 1);
}

/// @dev calculate the square of Coefficient of Variation (CV)
/// https://en.wikipedia.org/wiki/Coefficient_of_variation