Java examples for java.lang:Math Array Function
Return the standard deviation of all the elements in array
/*/*from w w w . java 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/>. */ //package com.java2s; public class Main { 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; } 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 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; } }