Java Array Divide divideNonSingular(int[] array1, int[] array2)

Here you can find the source of divideNonSingular(int[] array1, int[] array2)

Description

Divides two arrays element-to-element, but when numerator and denominator = 0, returns 0 instead of a singularity (NaN)

License

Open Source License

Parameter

Parameter Description
array1 Numerator
array2 Denominator

Return

The element-wise division of the input arrays

Declaration

public static double[] divideNonSingular(int[] array1, int[] array2) 

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://from   ww w.j  ava2  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
 ******************************************************************************/

public class Main {
    /**
     * Divides two arrays element-to-element, but when numerator and denominator = 0, returns 0 instead of a singularity (NaN)
     *
     * @param array1 Numerator
     * @param array2 Denominator
     * @return The element-wise division of the input arrays
     */
    public static double[] divideNonSingular(int[] array1, int[] array2) {
        double[] out = new double[array1.length];
        for (int i = 0; i < out.length; i++)
            out[i] = array1[i] == 0 && array2[i] == 0 ? 0 : (double) array1[i] / array2[i];

        return out;
    }
}

Related

  1. divideElementwise(int[] a, int[] b)
  2. divideImage(float[][] base, float[][] divisor)
  3. divideInPlace(double denominator, double[] target)
  4. divideInPlace(float[] vector, float val)
  5. divideIntoChunks(Object[] objs, int chunkSize)
  6. divideVector(float[] resultVec, float divisor)
  7. divideVectors(float[] v1, float[] v2)
  8. divisors(final int n, final int[] buffer)
  9. divVec2(final float[] result, final float[] vector, final float scale)