Java examples for java.lang:Math Matrix
Computes the standard deviation of the values in the matrix.
//package com.java2s; public class Main { /**/* ww w . j a v a2 s .co m*/ * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of integers whose standard deviation is to be * computed. * @param mean Mean value of the matrix. * @return Standard deviation of the matrix. */ public static double std(int[][] matrix, double mean) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += Math.pow((matrix[j][i] - mean), 2); } } double stDev = Math.sqrt(sum / (width * height - 1)); return stDev; } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of doubles whose standard deviation is to be * computed. * @param mean Mean value of the matrix. * @return Standard deviation of the matrix. */ public static double std(double[][] matrix, double mean) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += Math.pow((matrix[j][i] - mean), 2); } } double stDev = Math.sqrt(sum / (width * height - 1)); return stDev; } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of longs whose standard deviation is to be computed. * @param mean Mean value of the matrix. * @return Standard deviation of the matrix. */ public static double std(long[][] matrix, double mean) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += Math.pow((matrix[j][i] - mean), 2); } } double stDev = Math.sqrt(sum / (width * height - 1)); return stDev; } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of longs whose standard deviation is to be computed. * @param mean Mean value of the matrix. * @return Standard deviation of the matrix. */ public static float std(float[][] matrix, double mean) { int width = matrix.length; int height = matrix[0].length; float sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += Math.pow((matrix[j][i] - mean), 2); } } float stDev = (float) Math.sqrt(sum / (width * height - 1)); return stDev; } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of doubles whose standard deviation is to be * computed. * @param mean Mean value of the matrix. * @return Standard deviation of the matrix. */ public static float std(byte[][] matrix, double mean) { int width = matrix.length; int height = matrix[0].length; float sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += Math.pow((matrix[j][i] - mean), 2); } } float stDev = (float) Math.sqrt(sum / (width * height - 1)); return stDev; } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of doubles whose standard deviation is to be * computed. * @return Standard deviation of the matrix. */ public static double std(double[][] matrix) { double mean = mean(matrix); return std(matrix, mean); } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of integers whose standard deviation is to be * computed. * @return Standard deviation of the matrix. */ public static double std(int[][] matrix) { double mean = mean(matrix); return std(matrix, mean); } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of floats whose standard deviation is to be * computed. * @return Standard deviation of the matrix. */ public static float std(float[][] matrix) { float mean = mean(matrix); return std(matrix, mean); } /** * Computes the standard deviation of the values in the matrix. * * @param matrix Matrix of bytes whose standard deviation is to be computed. * @return Standard deviation of the matrix. */ public static double std(byte[][] matrix) { float mean = mean(matrix); return std(matrix, mean); } /** * Computes the standard deviation of the values in the array. * * @param array Array of doubles whose standard deviation is to be computed. * @param mean Mean value of the array. * @return Standard deviation of the array. */ public static double std(double[] array, double mean) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += Math.pow((array[i] - mean), 2); } double stDev = Math.sqrt(sum / (array.length - 1)); return stDev; } /** * Computes the standard deviation of the values in the array. * * @param array Array of integers whose standard deviation is to be * computed. * @param mean Mean value of the array. * @return Standard deviation of the array. */ public static double std(int[] array, double mean) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += Math.pow((array[i] - mean), 2); } double stDev = Math.sqrt(sum / (array.length - 1)); return stDev; } /** * Computes the standard deviation of the values in the array. * * @param array Array of floats whose standard deviation is to be computed. * @param mean Mean value of the array. * @return Standard deviation of the array. */ public static float std(float[] array, double mean) { float sum = 0; for (int i = 0; i < array.length; i++) { sum += Math.pow((array[i] - mean), 2); } float stDev = (float) Math.sqrt(sum / (array.length - 1)); return stDev; } /** * Computes the standard deviation of the values in the array. * * @param array Array of longs whose standard deviation is to be computed. * @param mean Mean value of the array. * @return Standard deviation of the array. */ public static double std(long[] array, double mean) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += Math.pow((array[i] - mean), 2); } double stDev = Math.sqrt(sum / (array.length - 1)); return stDev; } /** * Computes the standard deviation of the values in the array. * * @param array Array of bytes whose standard deviation is to be computed. * @param mean Mean value of the array. * @return Standard deviation of the array. */ public static float std(byte[] array, double mean) { float sum = 0; for (int i = 0; i < array.length; i++) { sum += Math.pow((array[i] - mean), 2); } float stDev = (float) Math.sqrt(sum / (array.length - 1)); return stDev; } /** * Computes the standard deviation of the values in the array. * * @param array Array of doubles whose standard deviation is to be computed. * @return Standard deviation of the array. */ public static double std(double[] array) { double mean = mean(array); return std(array, mean); } /** * Computes the standard deviation of the values in the array. * * @param array Array of integers whose standard deviation is to be * computed. * @return Standard deviation of the array. */ public static double std(int[] array) { double mean = mean(array); return std(array, mean); } /** * Computes the standard deviation of the values in the array. * * @param array Array of floats whose standard deviation is to be computed. * @return Standard deviation of the array. */ public static float std(float[] array) { double mean = mean(array); return std(array, mean); } /** * Computes the standard deviation of the values in the array. * * @param array Array of longs whose standard deviation is to be computed. * @return Standard deviation of the array. */ public static double std(long[] array) { double mean = mean(array); return std(array, mean); } /** * Computes the standard deviation of the values in the array. * * @param array Array of bytes whose standard deviation is to be computed. * @return Standard deviation of the array. */ public static float std(byte[] array) { double mean = mean(array); return std(array, mean); } /** * Computes the mean value of a matrix of integers. * * @param matrix A matrix of integers whose mean is to be computed. * @return Mean value of the matrix. */ public static double mean(int[][] matrix) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += matrix[j][i]; } } double mean = sum / (width * height); return mean; } /** * Computes the mean value of a matrix of doubles. * * @param matrix Matrix of doubles whose mean is to be computed. * @return Mean value of the matrix. */ public static double mean(double[][] matrix) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += matrix[j][i]; } } double mean = sum / (width * height); return mean; } /** * Computes the mean value of a matrix of longs. * * @param matrix Matrix of longs whose mean is to be computed. * @return Mean value of the matrix. */ public static double mean(long[][] matrix) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += matrix[j][i]; } } double mean = sum / (width * height); return mean; } /** * Computes the mean value of a matrix of floats. * * @param matrix Matrix of floats whose mean is to be computed. * @return Mean value of the matrix. */ public static float mean(float[][] matrix) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += matrix[j][i]; } } float mean = (float) (sum / (width * height)); return mean; } /** * Computes the mean value of a matrix of bytes. * * @param matrix Matrix of bytes whose mean is to be computed. * @return Mean value of the matrix. */ public static float mean(byte[][] matrix) { int width = matrix.length; int height = matrix[0].length; double sum = 0; for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { sum += matrix[j][i]; } } float mean = (float) (sum / (width * height)); return mean; } /** * Computes the mean value of an array of integers. * * @param array Array of integers whose mean is to be computed. * @return Mean value of the array. */ public static double mean(int[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double mean = sum / array.length; return mean; } /** * Computes the mean value of an array of doubles. * * @param array Array of doubles whose mean is to be computed. * @return Mean value of the array. */ public static double mean(double[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double mean = sum / array.length; return mean; } /** * Computes the mean value of an array of longs. * * @param array Array of longs whose mean is to be computed. * @return Mean value of the array. */ public static double mean(long[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double mean = sum / array.length; return mean; } /** * Computes the mean value of an array of floats. * * @param array Array of floats whose mean is to be computed. * @return Mean value of the array. */ public static double mean(float[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double mean = sum / array.length; return mean; } /** * Computes the mean value of an array of bytes. * * @param array Array of bytes whose mean is to be computed. * @return Mean value of the array. */ public static double mean(byte[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double mean = sum / array.length; return mean; } }