Java Array Normalize normalizeFromRealSpace(final double[] array)

Here you can find the source of normalizeFromRealSpace(final double[] array)

Description

normalizes the real-space probability array.

License

Open Source License

Parameter

Parameter Description
array the array to be normalized

Return

a newly allocated array corresponding the normalized values in array

Declaration

public static double[] normalizeFromRealSpace(final double[] array) 

Method Source Code

//package com.java2s;
/*/*from   www.  j  a va  2 s  .  co m*/
 * Copyright (c) 2017 NCIC, Institute of Computing Technology, Chinese Academy of Sciences
 *
 * 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.
 */

import java.util.*;

public class Main {
    /**
     * normalizes the real-space probability array.
     *
     * Does not assume anything about the values in the array, beyond that no elements are below 0.  It's ok
     * to have values in the array of > 1, or have the sum go above 0.
     *
     * @param array the array to be normalized
     * @return a newly allocated array corresponding the normalized values in array
     */
    public static double[] normalizeFromRealSpace(final double[] array) {
        if (array.length == 0)
            return array;

        final double sum = sum(array);
        final double[] normalized = new double[array.length];
        if (sum < 0.0)
            throw new IllegalArgumentException("Values in probability array sum to a negative number " + sum);
        for (int i = 0; i < array.length; i++) {
            normalized[i] = array[i] / sum;
        }
        return normalized;
    }

    public static double sum(final double[] values) {
        double s = 0.0;
        for (double v : values)
            s += v;
        return s;
    }

    public static long sum(final int[] x) {
        long total = 0;
        for (int v : x)
            total += v;
        return total;
    }

    public static int sum(final byte[] x) {
        int total = 0;
        for (byte v : x)
            total += (int) v;
        return total;
    }

    public static int sum(final List<Integer> list) {
        int sum = 0;
        for (Integer i : list) {
            sum += i;
        }
        return sum;
    }
}

Related

  1. normalizeFloatArray(float[] buffer, float peak, float target)
  2. normalizeForce(double[] data)
  3. normalizeFromLog10(double[] array)
  4. normalizeFromLog10(double[] array, boolean takeLog10OfOutput)
  5. normalizeFromLog10(final double[] array, final boolean takeLog10OfOutput, final boolean keepInLogSpace)
  6. normalizeHeightMap(float heightMap[])
  7. normalizeHeightMapCopy(float heightMap[])
  8. normalizeHistogram(int[] h)
  9. normalizeHistogram(int[] hist)