Java Array Normalize normalize(double[] a)

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

Description

normalize

License

Open Source License

Declaration

public static void normalize(double[] a) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.util.List;

public class Main {
    public static void normalize(double[] a) {
        double sum = sum(a);
        if (sum <= 0.0)
            return;

        divide(a, sum);// w  ww . jav a2s  .  com
        sum = sum(a);
        a[a.length - 1] = 1.0 - (sum - a[a.length - 1]);
    }

    public static double[][] normalize(double[][] a) {
        double sum = sum(a);
        if (sum <= 0.0)
            return null;

        double[][] result = copy(a);
        divide(result, sum);
        return result;
    }

    public static double sum(List<Double> list) {
        double sum = 0.0;
        for (double v : list) {
            sum += v;
        }
        return sum;
    }

    public static double sum(double[][] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += sum(a[i]);
        }
        return sum;
    }

    public static double sum(double[] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }

    public static int sum(int[] a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }

    public static void divide(double[][] a, double v) {
        for (int i = 0; i < a.length; i++) {
            divide(a[i], v);
        }
    }

    public static void divide(double[] a, double v) {
        for (int i = 0; i < a.length; i++) {
            a[i] /= v;
        }
    }

    private static double[][] copy(double[][] a) {
        double[][] result = new double[a.length][];
        for (int i = 0; i < result.length; i++) {
            result[i] = copy(a[i]);
        }
        return result;
    }

    private static double[] copy(double[] a) {
        double[] result = new double[a.length];
        System.arraycopy(a, 0, result, 0, a.length);
        return result;
    }
}

Related

  1. normaliseSum(double[] a)
  2. normaliseUnicode(String unicodeText, char[] mappings)
  3. normalize(byte[] data, byte maximum, byte minimum)
  4. normalize(double arr[])
  5. normalize(double wheelSpeeds[])
  6. normalize(double[] a)
  7. normalize(double[] a)
  8. normalize(double[] a)
  9. normalize(double[] a)