Java Array Divide divide(double[] dividend, double[] divisor, double dividendAdjustment, double divisorAdjustment)

Here you can find the source of divide(double[] dividend, double[] divisor, double dividendAdjustment, double divisorAdjustment)

Description

Returns an array whose members are the quotient of the dividend array values and the divisor array values.

License

Open Source License

Parameter

Parameter Description
dividend adjustment
divisor adjustment

Exception

Parameter Description
IllegalArgumentException if the two argument arrays are not the same length

Declaration

public static double[] divide(double[] dividend, double[] divisor, double dividendAdjustment,
        double divisorAdjustment) 

Method Source Code

//package com.java2s;
/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/// w w w  . j a  v  a2 s .  com
 * ---------------------------------------------------------------------
 */

public class Main {
    /**
     * Returns an array whose members are the quotient of the dividend array
     * values and the divisor array values.
     *
     * @param dividend
     * @param divisor
     * @param dividend adjustment
     * @param divisor  adjustment
     *
     * @return
     * @throws IllegalArgumentException if the two argument arrays are not the same length
     */
    public static double[] divide(double[] dividend, double[] divisor, double dividendAdjustment,
            double divisorAdjustment) {

        if (dividend.length != divisor.length) {
            throw new IllegalArgumentException("The dividend array and the divisor array must be the same length");
        }
        double[] quotient = new double[dividend.length];
        double denom = 1;
        for (int i = 0; i < dividend.length; i++) {
            quotient[i] = (dividend[i] + dividendAdjustment)
                    / ((denom = divisor[i] + divisorAdjustment) == 0 ? 1 : denom); //Protect against division by 0
        }
        return quotient;
    }

    /**
     * Returns an array whose members are the quotient of the dividend array
     * values and the divisor array values.
     *
     * @param dividend
     * @param divisor
     * @param dividend adjustment
     * @param divisor  adjustment
     *
     * @return
     * @throws IllegalArgumentException if the two argument arrays are not the same length
     */
    public static double[] divide(int[] dividend, int[] divisor) {

        if (dividend.length != divisor.length) {
            throw new IllegalArgumentException("The dividend array and the divisor array must be the same length");
        }
        double[] quotient = new double[dividend.length];
        double denom = 1;
        for (int i = 0; i < dividend.length; i++) {
            quotient[i] = (dividend[i]) / (double) ((denom = divisor[i]) == 0 ? 1 : denom); //Protect against division by 0
        }
        return quotient;
    }

    /**
     * Returns an array whose members are the quotient of the dividend array
     * values and the divisor value.
     *
     * @param dividend
     * @param divisor
     * @param dividend adjustment
     * @param divisor  adjustment
     *
     * @return
     * @throws IllegalArgumentException if the two argument arrays are not the same length
     */
    public static double[] divide(double[] dividend, double divisor) {
        double[] quotient = new double[dividend.length];
        double denom = 1;
        for (int i = 0; i < dividend.length; i++) {
            quotient[i] = (dividend[i]) / (double) ((denom = divisor) == 0 ? 1 : denom); //Protect against division by 0
        }
        return quotient;
    }
}

Related

  1. divide(double[] a, double sum)
  2. divide(double[] a, double v)
  3. divide(double[] a, double[] b)
  4. divide(double[] array, double divisor)
  5. divide(double[] array, double value)
  6. divide(double[] numerator, double[] denominator)
  7. divide(double[] t1, double[] t2)
  8. divide(double[][] arr1, double[][] arr2)
  9. divide(double[][] numerator, double[][] denominator)