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

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

Description

Divides two arrays element-to-element.

License

Open Source License

Parameter

Parameter Description
array1 Numerator
array2 Denominator

Return

The element-wise division of the input arrays

Declaration

public static double[] divide(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:/*w  ww .  ja  va 2s. c o 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.*;

public class Main {
    /**
     * Divides two arrays element-to-element.
     *
     * @param array1 Numerator
     * @param array2 Denominator
     * @return The element-wise division of the input arrays
     */
    public static double[] divide(int[] array1, int[] array2) {
        double[] out = new double[array1.length];
        for (int i = 0; i < out.length; i++)
            out[i] = (double) array1[i] / array2[i];

        return out;
    }

    /**
     * Divides all elements in an array by a scalar.
     *
     * @param array Input array
     * @param value Scalar
     * @return A new array containing the input elements divided by the scalar
     */
    public static double[] divide(int[] array, double value) {
        double[] out = new double[array.length];
        for (int i = 0; i < out.length; i++)
            out[i] = array[i] / value;

        return out;
    }

    /**
     * Divides all elements in a map by a scalar.
     *
     * @param <A> Key type
     * @param map Input map
     * @param value Scalar
     * @return A new map containing the input elements divided by the scalar
     */
    public static <A> Map<A, Double> divide(Map<A, Integer> map, double value) {
        Map<A, Double> out = new LinkedHashMap<A, Double>();

        for (Map.Entry<A, Integer> entry : map.entrySet()) {
            out.put(entry.getKey(), entry.getValue() / value);
        }

        return out;
    }

    /**
     * Returns the element-wise quotient 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 quotient
     */
    public static <A> Map<A, Double> divide(Map<A, Integer> map1, Map<A, Integer> map2) {
        Map<A, Double> out = new LinkedHashMap<A, Double>();

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

        return out;
    }
}

Related

  1. divide(final double[] numerators, final double[] denominators)
  2. divide(final double[] v1, final double[] v2)
  3. divide(float[] array, float divident)
  4. divide(float[][] a, float num)
  5. divide(float[][][][] img, float val)
  6. divide(long[] array, double value)
  7. divideAbsolute(double[] one, double[] two)
  8. divideAndConquer(int[] array, int num)
  9. divideArray(float[] array, float num)