Java stddev std(double a[])

Here you can find the source of std(double a[])

Description

std

License

Open Source License

Declaration

public static double std(double a[]) 

Method Source Code

//package com.java2s;
/*//from w w  w. j  a v  a  2s.c o  m
 * This file is part of TiPi (a Toolkit for Inverse Problems and Imaging)
 * developed by the MitiV project.
 *
 * Copyright (c) 2014 the MiTiV project, http://mitiv.univ-lyon1.fr/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

public class Main {

    public static double std(double a[]) {
        double V = var(a);
        return Math.sqrt(V);
    }

    public static double std(double a[][]) {
        double V = var(a);
        return Math.sqrt(V);
    }

    public static double std(double a[][][]) {
        double V = var(a);
        return Math.sqrt(V);
    }

    public static double var(double a[]) {
        double mean = avg(a);
        double out = 0;
        for (int k = 0; k < a.length; k++) {
            out = out + a[k] * a[k];
        }
        return out / a.length - mean * mean;
    }

    /**
     * Variance of the matrix
     */
    public static double var(double a[][]) {
        double mean = avg(a);
        double out;
        out = avg(hadamardProd(a, a, 0)) - mean * mean;
        return out;
    }

    public static double var(double a[][][]) {
        double x = 0;
        double mean = avg(a);
        double N = 0;
        double tmp = 0;
        for (int k = 0; k < a[0][0].length; k++) {
            for (int j = 0; j < a[0].length; j++) {
                for (int i = 0; i < a.length; i++) {
                    tmp = a[i][j][k] - mean;
                    x += tmp * tmp;
                    N++;
                }
            }
        }
        return x / N;
    }

    /**
     * Average or mean value of array
     */
    public static double avg(double[] a) {
        return sum(a) / (a.length);
    }

    /**
     * Average or mean value of array
     */
    public static double avg(long[] a) {
        return sum(a) / (a.length);
    }

    /**
     * Average or mean value of array
     */
    public static double avg(double[][] a) {
        int H = a.length;
        int W = a[0].length;
        return sum(a) / (H * W);
    }

    /**
     * Average or mean value of array
     */
    public static double avg(double[][][] a) {
        int W = a.length;
        int H = a[0].length;
        int D = a[0][0].length;
        return sum(a) / (D * H * W);
    }

    public static double[][] hadamardProd(double a[][], double b[][], int isComplex) {
        int H = a.length;
        int W = a[0].length;
        double out[][] = new double[H][W];
        if (isComplex == 0) {
            for (int j = 0; j < W; j++) {
                for (int i = 0; i < H; i++) {
                    out[i][j] = a[i][j] * b[i][j];

                }
            }
        } else {
            for (int j = 0; j < W; j++) {
                for (int i = 0; i < H; i++) {
                    out[i][2 * j] = a[i][2 * j] * b[i][2 * j] - a[i][2 * j + 1] * b[i][2 * j + 1];
                    out[i][2 * j + 1] = a[i][2 * j] * b[i][2 * j + 1] + a[i][2 * j + 1] * b[i][2 * j];
                }
            }
        }
        return out;
    }

    /**
     * Sum of the values in the array
     */
    public static double sum(double a[]) {
        int size = a.length;
        double sum = 0;
        for (int i = 0; i < size; i++) {
            sum += a[i];
        }
        return sum;
    }

    /**
     * Sum of the values in the array
     */
    public static double sum(double a[], int begin, int end) {
        double sum = 0;
        for (int i = begin; i <= end; i++) {
            sum += a[i];
        }
        return sum;
    }

    /**
     * Sum of the values in the array
     */
    public static long sum(long array[]) {
        int size = array.length;
        long sum = 0;
        for (int i = 0; i < size; i++) {
            sum += array[i];
        }
        return sum;
    }

    /**
     * Sum of the values in the array
     */
    public static double sum(double array[][]) {
        int H = array.length;
        double sum = 0;
        for (int i = 0; i < H; i++) {
            sum += sum(array[i]);
        }
        return sum;
    }

    /**
     * Sum of the values in the array
     */
    public static double sum(double array[][][]) {
        int D = array.length;
        double sum = 0;
        for (int k = 0; k < D; k++) {
            sum += sum(array[k]);
        }
        return sum;
    }
}

Related

  1. getStandardDeviation(double meanValue, ArrayList values)
  2. standardDeviation(double[] data)
  3. standardDeviation(double[] data, int opt)
  4. std(Collection dist, boolean populationStd)
  5. std(double[] a)
  6. std(double[] arr)
  7. std(double[] array)
  8. std(final double[] vec)