calculate the slope of (x values are taken as integer locations in list) - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

calculate the slope of (x values are taken as integer locations in list)

Demo Code

/*//from  ww w.j a v a 2 s  .  com
 * 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{
    /**
     * @param dblList List to calculate the slope of (x values are taken as integer locations in list)
     * @return Slope of list
     */
    public static double slope(final List<Double> dblList) {
        final int listSize = dblList.size();
        if (listSize < 2)
            return Double.NaN;
        double sumxy = 0;
        for (int i = 0; i < listSize; ++i) {
            sumxy += i * dblList.get(i);
        }
        final double sumx = (listSize - 1) * listSize / 2;
        final double sumy = sum(dblList);
        final double sumxx = (listSize - 1) * listSize * (2 * listSize - 1)
                / 6;
        return (listSize * sumxy - sumx * sumy)
                / (listSize * sumxx - sumx * sumx);
    }
    /**
     * 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;
    }
    /**
     * 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