Returns a normalised array of the elements in the given array - Java java.lang

Java examples for java.lang:Math Array Function

Description

Returns a normalised array of the elements in the given array

Demo Code

/*//from   ww w.j ava  2 s .c  o m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Vector;

public class Main{
    /**
     * Returns a normalised array of the elements in the given array 
     * 
     * @param array
     */
    public static double[] normaliseIntoNewArray(double[] array) {
        double[] newArray = new double[array.length];
        double mean = MatrixUtils.mean(array);
        double stdDev = MatrixUtils.stdDev(array, mean);
        if (Double.isInfinite(1.0 / stdDev)) {
            // The stdDev is 0, just subtract off mean
            for (int t = 0; t < array.length; t++) {
                newArray[t] = (array[t] - mean);
            }
        } else {
            // stdDev is non zero
            for (int t = 0; t < array.length; t++) {
                newArray[t] = (array[t] - mean) / stdDev;
            }
        }
        return newArray;
    }
    /**
     * Normalises the elements in the given column of the matrix
     * 
     * @param matrix 2D matrix of doubles
     * @param column column number to be normalised
     */
    public static double[] normaliseIntoNewArray(double[][] matrix,
            int column) {
        double[] newArray = new double[matrix.length];
        double mean = MatrixUtils.mean(matrix, column);
        double stdDev = MatrixUtils.stdDev(matrix, column, mean);
        if (Double.isInfinite(1.0 / stdDev)) {
            // The stdDev is 0, just subtract off mean
            for (int t = 0; t < matrix.length; t++) {
                newArray[t] = (matrix[t][column] - mean);
            }
        } else {
            // stdDev is non zero
            for (int t = 0; t < matrix.length; t++) {
                newArray[t] = (matrix[t][column] - mean) / stdDev;
            }
        }
        return newArray;
    }
    /**
     * Normalises the elements along each column of the matrix
     * 
     * @param matrix 2D matrix of doubles
     */
    public static double[][] normaliseIntoNewArray(double[][] matrix) {
        double[] means = means(matrix);
        double[] stds = stdDevs(matrix, means);
        return normaliseIntoNewArray(matrix, means, stds);
    }
    /**
     * Normalises the elements along each column of the matrix
     * 
     * @param matrix 2D matrix of doubles
     */
    public static double[][] normaliseIntoNewArray(double[][] matrix,
            double[] means, double[] stds) {
        double[][] newMatrix = new double[matrix.length][matrix[0].length];
        for (int r = 0; r < newMatrix.length; r++) {
            for (int c = 0; c < newMatrix[r].length; c++) {
                newMatrix[r][c] = matrix[r][c] - means[c];
                if (!Double.isInfinite(1.0 / stds[c])) {
                    newMatrix[r][c] /= stds[c];
                } // else we just subtract off the mean
            }
        }
        return newMatrix;
    }
    public static double mean(int[] input) {
        return sum(input) / (double) input.length;
    }
    public static double mean(double[] input) {
        return sum(input) / (double) input.length;
    }
    public static double mean(double[] input, int startIndex, int length) {
        return sum(input, startIndex, length) / (double) length;
    }
    public static double mean(double[][] input) {
        return sum(input) / (double) (input.length * input[0].length);
    }
    /**
     * Compute the mean along the given column 
     * 
     * @param input
     * @param column
     * @return
     */
    public static double mean(double[][] input, int column) {
        return sum(input, column) / (double) input.length;
    }
    public static double stdDev(double[] array) {
        double mean = 0.0;
        double total = 0.0;
        for (int m = 0; m < array.length; m++) {
            total += array[m];
        }
        mean = total / (double) array.length;

        return stdDev(array, mean);
    }
    public static double stdDev(double[][] matrix, int column) {
        double mean = 0.0;
        double total = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            total += matrix[m][column];
        }
        mean = total / (double) matrix.length;

        return stdDev(matrix, column, mean);
    }
    /**
     * Return the standard deviation of all the elements in array
     * 
     * @param array
     * @param mean
     * @return
     */
    public static double stdDev(double[] array, double mean) {
        return stdDev(array, mean, array.length);
    }
    /**
     * Standard deviation for the first arrayLength terms of array
     * 
     * @param array
     * @param mean
     * @param arrayLength
     * @return
     */
    public static double stdDev(double[] array, double mean, int arrayLength) {
        if (arrayLength == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < arrayLength; m++) {
            sumSqs += (array[m] - mean) * (array[m] - mean);
        }
        double std = sumSqs / (double) (arrayLength - 1);
        std = Math.sqrt(std);
        return std;
    }
    /**
     * Compute the standard deviation along the given column, with the known
     *  given mean.
     * 
     * @param matrix
     * @param column
     * @param mean
     * @return
     */
    public static double stdDev(double[][] matrix, int column, double mean) {
        if (matrix.length == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            sumSqs += (matrix[m][column] - mean)
                    * (matrix[m][column] - mean);
        }
        double std = sumSqs / (double) (matrix.length - 1);
        std = Math.sqrt(std);
        return std;
    }
    /**
     * Compute the standard deviation across all values in the 2D matrix
     * 
     * @param matrix
     * @return
     */
    public static double stdDev(double[][] matrix) {
        double mean = mean(matrix);
        return stdDev(matrix, mean);
    }
    /**
     * Compute the standard deviation across all values in the 2D matrix
     * 
     * @param matrix
     * @param mean
     * @return
     */
    public static double stdDev(double[][] matrix, double mean) {
        if (matrix.length == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            for (int c = 0; c < matrix[m].length; c++) {
                sumSqs += (matrix[m][c] - mean) * (matrix[m][c] - mean);
            }
        }
        double std = sumSqs
                / (double) ((matrix.length * matrix[0].length) - 1);
        std = Math.sqrt(std);
        return std;
    }
    /**
     * Return an array of the means of each column in the 2D input
     * 
     * @param input
     * @return
     */
    public static double[] means(double[][] input) {
        double[] theMeans = sums(input);
        for (int i = 0; i < theMeans.length; i++) {
            theMeans[i] = theMeans[i] / input.length;
        }
        return theMeans;
    }
    /**
     * Return an array of the means of each column in the 2D input
     * 
     * @param input
     * @param startRow which row to start from
     * @param length how many rows to take the mean over
     * @return
     */
    public static double[] means(double[][] input, int startRow, int length) {
        double[] theMeans = sums(input, startRow, length);
        for (int i = 0; i < theMeans.length; i++) {
            theMeans[i] = theMeans[i] / length;
        }
        return theMeans;
    }
    /**
     * Compute the standard deviations along each column
     * 
     * @param matrix
     * @param means
     * @return
     */
    public static double[] stdDevs(double[][] matrix, double[] means) {
        double[] sumSqs = new double[means.length];
        for (int m = 0; m < matrix.length; m++) {
            for (int c = 0; c < matrix[m].length; c++) {
                sumSqs[c] += (matrix[m][c] - means[c])
                        * (matrix[m][c] - means[c]);
            }
        }
        double[] stds = new double[means.length];
        for (int c = 0; c < stds.length; c++) {
            stds[c] = sumSqs[c] / (double) (matrix.length - 1);
            stds[c] = Math.sqrt(stds[c]);
        }
        return stds;
    }
    public static double sum(double[] input) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i];
        }
        return total;
    }
    public static double sum(double[] input, int startIndex, int length) {
        double total = 0;
        for (int i = startIndex; i < startIndex + length; i++) {
            total += input[i];
        }
        return total;
    }
    public static double sum(double[][] input) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input[i].length; j++) {
                total += input[i][j];
            }
        }
        return total;
    }
    public static double sum(double[][] input, int column) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i][column];
        }
        return total;
    }
    public static int sum(int[] input) {
        int total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i];
        }
        return total;
    }
    public static int sum(int[][] input) {
        int total = 0;
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input[i].length; j++) {
                total += input[i][j];
            }
        }
        return total;
    }
    /**
     * Return an array of the sums for each column in the 2D input
     * 
     * @param input
     * @return
     */
    public static double[] sums(double[][] input) {
        double[] theSums = new double[input[0].length];
        for (int r = 0; r < input.length; r++) {
            for (int c = 0; c < input[r].length; c++) {
                theSums[c] += input[r][c];
            }
        }
        return theSums;
    }
    /**
     * Return an array of the sums for each column in the 2D input
     * 
     * @param input
     * @param startRow which row to start from
     * @param length how many rows to take the sum over
     * @return
     */
    public static double[] sums(double[][] input, int startRow, int length) {
        double[] theSums = new double[input[0].length];
        for (int r = startRow; r < startRow + length; r++) {
            for (int c = 0; c < input[r].length; c++) {
                theSums[c] += input[r][c];
            }
        }
        return theSums;
    }
}

Related Tutorials