Replace avg expression with sum/count for optimization.
@return
515896
public AbstractExpression replaceAVG () {
        if (getExpressionType() == ExpressionType.AGGREGATE_AVG) {
            AbstractExpression child = getLeft();
            AbstractExpression left =
                    new AggregateExpression(ExpressionType.AGGREGATE_SUM);
            left.setLeft(child.clone());
            AbstractExpression right =
                    new AggregateExpression(ExpressionType.AGGREGATE_COUNT);
            right.setLeft(child.clone());

            return new OperatorExpression(ExpressionType.OPERATOR_DIVIDE,
                    left, right);
        }

        AbstractExpression lnode = null;
        AbstractExpression rnode = null;
        if (m_left != null) {
            lnode = m_left.replaceAVG();
        }
        if (m_right != null) {
            rnode = m_right.replaceAVG();
        }

        ArrayList<AbstractExpression> newArgs = null;
        boolean changed = false;
        if (m_args != null) {
            newArgs = new ArrayList<>();
            for (AbstractExpression expr: m_args) {
                AbstractExpression ex = expr.replaceAVG();
                newArgs.add(ex);
                if (ex != expr) {
                    changed = true;
                }
            }
        }
        if (m_left != lnode || m_right != rnode || changed) {
            AbstractExpression resExpr = clone();
            resExpr.setLeft(lnode);
            resExpr.setRight(rnode);
            resExpr.setArgs(newArgs);
            return resExpr;
        }

        return this;
    }
698772
public Map<String, Integer> getAggregateResultCountSummary() {

        Map<String, Integer> summaryMap = new LinkedHashMap<String, Integer>();

        for (Entry<String, LinkedHashSet<String>> entry : aggregateResultMap
                .entrySet()) {
            summaryMap.put(entry.getKey(), entry.getValue().size());
        }

        return summaryMap;
    }
314301
private int buildHistogramByAccumulatingCounts() {
		int sum = 0;
		for (int i = 0; i < counterHistogram.length; ++i) {
			int currentSlotValue = counterHistogram[i];
			counterHistogram[i] = sum;
			sum += currentSlotValue;
		}
		return sum;
	}
1184806
function average(expression) {

        //Define output
        var outData;

        //If there's no expression
        if (!expression){

            //Just assign current items
            outData = this.items;
        }
        else{

            //Otherwise apply select condition
            outData = jslinq(this.items)
                .select(expression)
                .toList();
        }

        //If array has no elements, returns null
        if (outData.length === 0){
            return null;
        }
        else{

            //Sum every value
            var valuesSum = 0;

            //Iterate all elements on list
            for (var i = 0; i < outData.length; i++){

                //If element is not number, throw
                if (isNaN(outData[i]))
                    throw new Error("Element '" + outData[i] +
                        "' (index:" + i + ") is not a number");

                //Increment sum
                valuesSum = outData[i] + valuesSum;
            }

            //Calculate average
            var outAverage = valuesSum / outData.length

            //Returns first element
            return outAverage;
        }
    }
1035979
public StringExpression min() {
        if (min == null) {
            min = Expressions.stringOperation(Ops.AggOps.MIN_AGG, mixin);
        }
        return min;
    }
1221564
@Override
    public Expression<Long> count(Expression<?> arg0)
    {
        // TODO Auto-generated method stub
        String arg1 = "Count("+arg0.getAlias()+")";
        return new AggregateExpression(arg0, arg1);
    }
1099677
public static double avg(Array array) throws ExpressionException {
	if (array.size() == 0) return 0;
	return sum(array) / array.size();
    }
707307
public static WindowOver<Long> count(Expression<?> expr) {
        return new WindowOver<Long>(Long.class, Ops.AggOps.COUNT_AGG, expr);
    }
452634
@JmxAttribute(optional = true)
	public double getSmoothedStandardDeviation() {
		if (totalCount == 0) {
			return 0.0;
		}

		double avg = smoothedSum / smoothedCount;
		double variance = smoothedSqr / smoothedCount - avg * avg;
		if (variance < 0.0)
			variance = 0.0;
		return sqrt(variance);
	}
283195
function avg (arr) {
    if (isArray(arr) && !(arr[0] instanceof Array)) {
        const totalSum = sum(arr);
        const len = arr.length || 1;
        return (Number.isNaN(totalSum) || totalSum instanceof InvalidAwareTypes) ?
                 InvalidAwareTypes.NULL : totalSum / len;
    }
    return InvalidAwareTypes.NULL;
}
1145255
def avg(self):
        """"""
        # XXX rename this method
        if len(self.values) > 0:
            return sum(self.values) / float(len(self.values))
        else:
            return None
642541
public double normalize() {
        double propSum = this.getSum();
        if (propSum == s.zero()) {
            this.fill(s.divide(s.one(), s.fromReal(values.length)));
        } else if (propSum == s.posInf()) {
            int count = DoubleArrays.count(values, s.posInf());
            if (count == 0) {
                throw new RuntimeException("Unable to normalize since sum is infinite but contains no infinities: " + Arrays.toString(values));
            }
            double constant = s.divide(s.one(), s.fromReal(count));
            for (int d=0; d<values.length; d++) {
                if (values[d] == s.posInf()) {
                    values[d] = constant;
                } else {
                    values[d] = s.zero();
                }
            }
        } else {
            this.divide(propSum);
            assert !this.containsNaN();
        }
        return propSum;
    }
627972
public static Object sum(Object[] self, Object initialValue) {
        return sum(toList(self), initialValue, false);
    }
1315713
public static <N extends Number> Number average(List<N> numberList) {
        return cal(numberList, DoubleStream::average);
    }
763547
public <T extends Average> T merge(T other) {
        if(Util.productGreaterThan(count, (long)Math.ceil(avg), Long.MAX_VALUE) ||
          Util.productGreaterThan(other.count(), (long)Math.ceil(other.average()), Long.MAX_VALUE)) {
            // the above computation is not correct as the sum of the 2 products can still lead to overflow
            // a non-weighted avg
            avg=avg + other.average() / 2.0;
        }
        else { // compute the new average weighted by count
            long total_count=count + other.count();
            avg=(count * avg + other.count() * other.average()) / total_count;
            count=total_count/2;
        }
        return (T)this;
    }
321894
public static /*@pure@*/ double info(int counts[]) {
    
    int total = 0;
    double x = 0;
    for (int j = 0; j < counts.length; j++) {
      x -= xlogx(counts[j]);
      total += counts[j];
    }
    return x + xlogx(total);
  }
564170
@Override
    public double average()
    {
        readLock.lock();
        try
        {
            double s = 0;
            PrimitiveIterator.OfInt it = modIterator();
            while (it.hasNext())
            {
                s += ring[it.nextInt()];
            }
            return s/(count());
        }
        finally
        {
            readLock.unlock();
        }
    }