Java Array Normalize normalize(double[] a)

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

Description

This method normalizes the given array (vector) and returns the normalizing constant.

License

Open Source License

Parameter

Parameter Description
a matrix to be normalized

Declaration

public static double normalize(double[] a) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w  ww.  j  ava 2 s  . c om
     * This method normalizes the given array (vector) and returns the normalizing constant.  <br>
     * @param a matrix to be normalized
     * @return
     */
    public static double normalize(double[] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        if (sum == 0) {
            sum = 1;
        }
        for (int i = 0; i < a.length; i++) {
            a[i] /= sum;
        }
        return sum;
    }

    /**
     * This method normalizes the given matrix and returns the normalizing constant.  <br>
     * However, it treats the whole matrix as an array (vector).
     * @param a matrix to be normalized
     * @return
     */
    public static double normalize(double[][] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                sum += a[i][j];
            }
        }
        if (sum == 0) {
            sum = 1;
        }
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                a[i][j] /= sum;
            }
        }
        return sum;
    }

    /**
     * This method will normalize the matrix on the specified dimension and 
     * returns the normalizing constants.
     * @param a matrix to be normalized
     * @param dim dimension on which the normalization will take place. <br>
     * dim = 1, normalize w.r.t. to the first dimension, aka rows.  <br>
     * dim = 2, normalize w.r.t. to the second dimension, aka columns.
     * @return
     */
    public static double[] normalize(double[][] a, int dim) {
        double[] sum;
        if (dim == 1)
            sum = new double[a.length];
        else if (dim == 2)
            sum = new double[a[0].length];
        else
            throw new IllegalArgumentException("The only valid values for dim are: 1 (rows), 2 (columns).");

        if (dim == 1) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[i].length; j++)
                    sum[i] += a[i][j];

                if (sum[i] == 0) {
                    sum[i] = 1;
                }
                ;
            }

            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[i].length; j++)
                    a[i][j] /= sum[i];
            }
        } else {
            for (int j = 0; j < a[0].length; j++) {
                for (int i = 0; i < a.length; i++)
                    sum[j] += a[i][j];

                if (sum[j] == 0) {
                    sum[j] = 1;
                }
                ;
            }

            for (int j = 0; j < a[0].length; j++) {
                for (int i = 0; i < a.length; i++)
                    a[i][j] /= sum[j];
            }
        }

        return sum;
    }

    /**
     * This method will normalize the matrix on the specified dimension and 
     * returns the normalizing constants.
     * When normalizing w.r.t. rows or columns, it does so for each of element of
     * the third dimension separately.
     * @param a matrix to be normalized
     * @param dim dimension on which the normalization will take place. <br>
     * dim = 1, normalize w.r.t. to the first dimension. <br>
     * dim = 2, normalize w.r.t. to the second dimension, aka rows.  <br>
     * dim = 3, normalize w.r.t. to the third dimension, aka columns.
     * @return
     */
    public static double[][] normalize(double[][][] a, int dim) {
        double[][] sum;
        if (dim == 1)
            sum = new double[a[0].length][a[0][0].length];
        else if (dim == 2)
            sum = new double[a.length][a[0].length];
        else if (dim == 3)
            sum = new double[a.length][a[0][0].length];
        else
            throw new IllegalArgumentException("The only valid values for dim are: 1 (rows), 2 (columns).");

        if (dim == 2) {
            for (int k = 0; k < a.length; k++) {
                for (int i = 0; i < a[k].length; i++) {
                    for (int j = 0; j < a[k][i].length; j++)
                        sum[k][i] += a[k][i][j];

                    if (sum[k][i] == 0) {
                        sum[k][i] = 1;
                    }
                }
            }

            for (int k = 0; k < a.length; k++) {
                for (int i = 0; i < a[k].length; i++) {
                    for (int j = 0; j < a[k][i].length; j++)
                        a[k][i][j] /= sum[k][i];
                }
            }
        } else if (dim == 3) {
            for (int k = 0; k < a.length; k++) {
                for (int j = 0; j < a[k][0].length; j++) {
                    for (int i = 0; i < a[k].length; i++)
                        sum[k][j] += a[k][i][j];

                    if (sum[k][j] == 0) {
                        sum[k][j] = 1;
                    }
                }
            }

            for (int k = 0; k < a.length; k++) {
                for (int j = 0; j < a[k][0].length; j++) {
                    for (int i = 0; i < a[k].length; i++)
                        a[k][i][j] /= sum[k][j];
                }
            }
        } else {
            for (int i = 0; i < a[0].length; i++) {
                for (int j = 0; j < a[0][i].length; j++) {
                    for (int k = 0; k < a.length; k++)
                        sum[i][j] += a[k][i][j];

                    if (sum[i][j] == 0) {
                        sum[i][j] = 1;
                    }
                }
            }

            for (int i = 0; i < a[0].length; i++) {
                for (int j = 0; j < a[0][i].length; j++)
                    for (int k = 0; k < a.length; k++)
                        a[k][i][j] /= sum[i][j];
            }
        }

        return sum;
    }
}

Related

  1. normalize(double wheelSpeeds[])
  2. normalize(double[] a)
  3. normalize(double[] a)
  4. normalize(double[] a)
  5. normalize(double[] a)
  6. normalize(double[] ar)
  7. normalize(double[] array)
  8. normalize(double[] array)
  9. normalize(double[] array)