Java Array Sum sum(double[] array)

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

Description

Returns the sum of all elements in the input array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Sum of all array elements

Declaration

public static double sum(double[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*  w w  w  .  j a v a2  s  .  co  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;
import java.util.Map.Entry;

public class Main {
    /**
     * Returns the element-wise sum of two maps.
     *
     * @param <A> Key type
     * @param map1 Input map 1
     * @param map2 Input map 2
     * @return A new map with the element-wise sum
     */
    public static <A> Map<A, Double> sum(Map<A, Double> map1, Map<A, Double> map2) {
        Map<A, Double> out = new LinkedHashMap<A, Double>();

        for (Entry<A, Double> entry : map1.entrySet()) {
            A key = entry.getKey();
            out.put(key, entry.getValue() + map2.get(key));
        }

        return out;
    }

    /**
     * Returns the sum of all elements in the input array.
     *
     * @param array Input array
     * @return Sum of all array elements
     */
    public static double sum(double[] array) {
        double out = 0;
        for (int i = 0; i < array.length; i++)
            out += array[i];

        return out;
    }

    /**
     * Returns the sum of all elements in the input matrix.
     *
     * @param matrix Input matrix
     * @return Sum of all matrix elements
     */
    public static double sum(double[][] matrix) {
        double out = 0;
        for (double[] matrix1 : matrix)
            out += sum(matrix1);

        return out;
    }

    /**
     * Returns the sum of all elements in the input collection.
     *
     * @param collection Input collection
     * @return Sum of all collection values
     */
    public static double sum(Collection<Double> collection) {
        double out = 0;
        for (double value : collection)
            out += value;

        return out;
    }

    /**
     * Returns the sum of all elements in the input collection.
     *
     * @param <A> Key type
     * @param map Input map
     * @return Sum of all map values
     */
    public static <A> double sum(Map<A, Double> map) {
        return sum(map.values());
    }

    /**
     * Returns the element-wise sum of two arrays.
     *
     * @param array1 Input array 1
     * @param array2 Input array 2
     * @return A new array with the element-wise sum
     */
    public static double[] sum(double[] array1, double[] array2) {
        double[] out = new double[array1.length];

        for (int i = 0; i < out.length; i++)
            out[i] = array1[i] + array2[i];

        return out;
    }
}

Related

  1. sum(double[] addends)
  2. sum(double[] array)
  3. sum(double[] array)
  4. sum(double[] array)
  5. sum(double[] array)
  6. sum(Double[] array)
  7. sum(double[] array)
  8. sum(double[] array)
  9. sum(double[] d)