Java Array Sum sumOfProducts(double[]... nums)

Here you can find the source of sumOfProducts(double[]... nums)

Description

This returns the sum of products for the given numbers.

License

Apache License

Parameter

Parameter Description
nums the sum of products for the give numbers

Return

the sum of products for the given numbers

Declaration

public static double sumOfProducts(double[]... nums) 

Method Source Code

//package com.java2s;
/*-//from   w  ww  .j  a  v a 2s  .co m
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

public class Main {
    /**
     * This returns the sum of products for the given
     * numbers.
     * @param nums the sum of products for the give numbers
     * @return the sum of products for the given numbers
     */
    public static double sumOfProducts(double[]... nums) {
        if (nums == null || nums.length < 1)
            return 0;
        double sum = 0;

        for (int i = 0; i < nums.length; i++) {
            /* The ith column for all of the rows */
            double[] column = column(i, nums);
            sum += times(column);

        }
        return sum;
    }

    /**
     * This returns the given column over an n arrays
     * @param column the column to getFromOrigin values for
     * @param nums the arrays to extract values from
     * @return a double array containing all of the numbers in that column
     * for all of the arrays.
     * @throws IllegalArgumentException if the index is < 0
     */
    private static double[] column(int column, double[]... nums) throws IllegalArgumentException {

        double[] ret = new double[nums.length];

        for (int i = 0; i < nums.length; i++) {
            double[] curr = nums[i];
            ret[i] = curr[column];
        }
        return ret;
    }

    /**
     * This returns the product of all numbers in the given array.
     * @param nums the numbers to multiply over
     * @return the product of all numbers in the array, or 0
     * if the length is or nums i null
     */
    public static double times(double[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        double ret = 1;
        for (int i = 0; i < nums.length; i++)
            ret *= nums[i];
        return ret;
    }
}

Related

  1. sumOf(int... values)
  2. sumOfArray(double[] array)
  3. sumOfArray(final double[] array)
  4. sumOfMeanDifferencesOnePoint(double[] vector)
  5. sumOfMinimum(double[] a, double[] b)
  6. sumOverVector(float[] a)
  7. sumProd(double[] v1, double[] v2, int i_, int n_)
  8. sumRightShifts(int num, int... shifts)
  9. sumSquared(double[] a)