Normalizes a list of doubles such that the sum of the list equals 1 - Java java.lang

Java examples for java.lang:float

Description

Normalizes a list of doubles such that the sum of the list equals 1

Demo Code

/*//from   w w w  .  ja  v  a 2 s  .  c om
 * Copyright (c) 2010-2011 Ashlie Benjamin Hocking. All Rights reserved.
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main{
    /**
     * Normalizes a list of doubles such that the sum of the list equals 1
     * @param list List to normalize
     * @return Normalized list (note that original list is unmodified)
     */
    public static List<Double> normalize(final List<Double> list) {
        final double listSum = sum(list);
        return divide(list, listSum);
    }
    /**
     * Adds up all of the values in an array of Strings.
     * @param valArray Array of Strings
     * @return Sum of values in the array
     * @exception NumberFormatException if one of the Strings does not contain a parsable number.
     */
    public static double sum(final String[] valArray) {
        return sum(toDoubleList(valArray));
    }
    /**
     * Adds up all of the values in an array of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberArray Array of Numbers
     * @return Sum of values in the array
     */
    public static <T extends Number> double sum(final T[] numberArray) {
        return sum(Arrays.asList(numberArray));
    }
    /**
     * Adds up all of the values in a List of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberList List of Numbers
     * @return Sum of values in the List
     */
    public static <T extends Number> double sum(final List<T> numberList) {
        double retval = 0;
        if (!numberList.isEmpty()) {
            for (final Integer i : new IntegerRange(numberList.size())) {
                retval += numberList.get(i).doubleValue();
            }
        }
        return retval;
    }
    /**
     * Divides elements in the first {@link java.util.List List} by elements in the second {@link java.util.List List}, and returns
     * the result in a {@link java.util.List List} of the same size.
     * @param numerator Numerator {@link java.util.List List}
     * @param denominator Denominator {@link java.util.List List}
     * @return Dividend {@link java.util.List List}
     * @exception IllegalArgumentException if two lists are not of the same size
     * @exception NullPointerException if either list is null
     */
    public static List<Double> divide(final List<Double> numerator,
            final List<Double> denominator) {
        if (numerator.size() != denominator.size())
            throw new IllegalArgumentException(
                    "Lists being divided must be of the same size.");
        final List<Double> retval = new ArrayList<Double>(numerator.size());
        if (!numerator.isEmpty()) {
            for (final Integer i : new IntegerRange(numerator.size())) {
                retval.add(numerator.get(i) / denominator.get(i));
            }
        }
        return retval;
    }
    /**
     * Divides elements in a {@link java.util.List List} by a constant value and returns the result in a {@link java.util.List List}
     * of the same size.
     * @param numerator Numerator {@link java.util.List List}
     * @param val Denominator
     * @return Dividend {@link java.util.List List}
     */
    public static List<Double> divide(final List<Double> numerator,
            final double val) {
        if (numerator == null)
            return null;
        final List<Double> retval = new ArrayList<Double>(numerator.size());
        if (!numerator.isEmpty()) {
            for (final Integer i : new IntegerRange(numerator.size())) {
                retval.add(numerator.get(i) / val);
            }
        }
        return retval;
    }
    /**
     * Converts an array of Strings to a List of Doubles
     * @param valList Array of Strings
     * @return List of Doubles corresponding to the the Array of Strings
     * @exception NumberFormatException if one of the Strings does not contain a parsable number.
     */
    public static List<Double> toDoubleList(final String[] valList) {
        final List<Double> retval = new ArrayList<Double>();
        for (final String s : valList) {
            retval.add(Double.valueOf(s));
        }
        return retval;
    }
    /**
     * Adds up two equal sized Lists into a resultant List of the same size
     * @param list1 First list to add
     * @param list2 Second list to add
     * @pre list1.size() == list2.size()
     * @return Addition of two arrays
     */
    public static List<Double> add(final List<Double> list1,
            final List<Double> list2) {
        if (list1.size() != list2.size())
            throw new IllegalArgumentException(
                    "Lists being added together must be of the same size.");
        final List<Double> retval = new ArrayList<Double>(list1.size());
        if (!list1.isEmpty()) {
            for (final Integer i : new IntegerRange(list1.size())) {
                retval.add(list1.get(i) + list2.get(i));
            }
        }
        return retval;
    }
    /**
     * @param list List to add to
     * @param toAdd Amount to add to the list
     * @return List with all values increased by toAdd
     */
    public static List<Double> add(final List<Double> list,
            final double toAdd) {
        final List<Double> retval = new ArrayList<Double>(list.size());
        if (!list.isEmpty()) {
            for (final Integer i : new IntegerRange(list.size())) {
                retval.add(list.get(i) + toAdd);
            }
        }
        return retval;
    }
}

Related Tutorials