Example usage for org.apache.commons.math3.util MathArrays normalizeArray

List of usage examples for org.apache.commons.math3.util MathArrays normalizeArray

Introduction

In this page you can find the example usage for org.apache.commons.math3.util MathArrays normalizeArray.

Prototype

public static double[] normalizeArray(double[] values, double normalizedSum)
        throws MathIllegalArgumentException, MathArithmeticException 

Source Link

Document

Normalizes an array to make it sum to a specified value.

Usage

From source file:org.apache.solr.client.solrj.io.eval.NormalizeSumEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    Object value = values[0];//from  ww w .j av  a  2 s .c  o m

    double sumTo = 1.0;

    if (values.length == 2) {
        Number n = (Number) values[1];
        sumTo = n.doubleValue();
    }

    if (null == value) {
        return null;
    } else if (value instanceof Matrix) {
        Matrix matrix = (Matrix) value;

        double[][] data = matrix.getData();
        double[][] unitData = new double[data.length][];
        for (int i = 0; i < data.length; i++) {
            double[] row = data[i];
            double[] unitRow = MathArrays.normalizeArray(row, sumTo);
            unitData[i] = unitRow;
        }

        return new Matrix(unitData);
    } else if (value instanceof List) {
        List<Number> vals = (List<Number>) value;
        double[] doubles = new double[vals.size()];
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] = vals.get(i).doubleValue();
        }

        List<Number> unitList = new ArrayList(doubles.length);
        double[] unitArray = MathArrays.normalizeArray(doubles, sumTo);
        for (double d : unitArray) {
            unitList.add(d);
        }

        return unitList;
    } else {
        throw new IOException("The unit function expects either a numeric array or matrix as a parameter");
    }
}