Java Array Average average(int[] x)

Here you can find the source of average(int[] x)

Description

average

License

Open Source License

Declaration

public static double average(int[] x) 

Method Source Code

//package com.java2s;
/*/*from   w  w w. j a  v  a  2 s  .c om*/
 * Copyright (c) 2010 The Broad Institute
 *
 * 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 {
    public static double average(Collection<? extends Number> x) {
        return sum(x) / x.size();
    }

    public static double average(Collection<Number> numbers, boolean ignoreNan) {
        if (ignoreNan) {
            return sum(numbers, true) / nonNanSize(numbers);
        } else {
            return sum(numbers, false) / nonNanSize(numbers);
        }
    }

    public static double average(List<Long> vals, int maxI) {
        long sum = 0L;

        int i = 0;
        for (long x : vals) {
            if (i > maxI)
                break;
            sum += x;
            i++;
            //System.out.printf(" %d/%d", sum, i);
        }

        //System.out.printf("Sum = %d, n = %d, maxI = %d, avg = %f%n", sum, i, maxI, (1.0 * sum) / i);

        return (1.0 * sum) / i;
    }

    public static double average(List<Long> vals) {
        return average(vals, vals.size());
    }

    public static double average(int[] x) {
        int sum = 0;
        for (int v : x)
            sum += v;
        return (double) sum / x.length;
    }

    public static byte average(byte[] vals) {
        int sum = 0;
        for (byte v : vals) {
            sum += v;
        }
        return (byte) Math.floor(sum / vals.length);
    }

    public static double sum(Collection<? extends Number> numbers) {
        return sum(numbers, false);
    }

    public static double sum(Collection<? extends Number> numbers, boolean ignoreNan) {
        double sum = 0;
        for (Number n : numbers) {
            if (!ignoreNan || !Double.isNaN(n.doubleValue())) {
                sum += n.doubleValue();
            }
        }

        return sum;
    }

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

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

    public static int nonNanSize(Collection<Number> numbers) {
        int size = 0;
        for (Number n : numbers) {
            size += Double.isNaN(n.doubleValue()) ? 0 : 1;
        }

        return size;
    }
}

Related

  1. average(int... values)
  2. average(int[] a)
  3. average(int[] array)
  4. average(int[] pixels)
  5. average(int[] pixels)
  6. average(long[] array)
  7. average(long[] vals)
  8. average(Number[] numbers)
  9. average1(double[] d)