Here you can find the source of sumOfProducts(double[]... nums)
Parameter | Description |
---|---|
nums | the sum of products for the give numbers |
public static double sumOfProducts(double[]... nums)
//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; } }