Java Array Subtract subtractElementwise(int[] a, int[] b)

Here you can find the source of subtractElementwise(int[] a, int[] b)

Description

Subtracts the values in the two arrays of integers element-wise.

License

Open Source License

Parameter

Parameter Description
a Array of integers.
b Array of integers.

Exception

Parameter Description
ArithmeticException Throws exception when the arrays differ inlength.

Return

Result of element-wise subtraction of arrays.

Declaration

public static int[] subtractElementwise(int[] a, int[] b) throws ArithmeticException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w  ww  .  ja v a  2  s  . co  m
     * Subtracts the values in the two arrays of integers element-wise.
     *
     * @param a   Array of integers.
     * @param b   Array of integers.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static int[] subtractElementwise(int[] a, int[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            int[] result = new int[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = a[i] - b[i];
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two arrays of doubles element-wise.
     *
     * @param a   Array of doubles.
     * @param b   Array of doubles.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static double[] subtractElementwise(double[] a, double[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            double[] result = new double[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = a[i] - b[i];
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two arrays of longs element-wise.
     *
     * @param a   Array of longs.
     * @param b   Array of longs.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static long[] subtractElementwise(long[] a, long[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            long[] result = new long[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = a[i] - b[i];
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two arrays of floats element-wise.
     *
     * @param a   Array of floats.
     * @param b   Array of floats.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static float[] subtractElementwise(float[] a, float[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            float[] result = new float[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = a[i] - b[i];
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two arrays of bytes element-wise.
     *
     * @param a   Array of bytes.
     * @param b   Array of bytes.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static byte[] subtractElementwise(byte[] a, byte[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            byte[] result = new byte[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = (byte) (a[i] - b[i]);
            }
            return result;
        }

    }

    /**
     * Subtracts the values in an array of doubles and and array of integers
     * element-wise.
     *
     * @param a   Array of doubles.
     * @param b   Array of integers.
     * @return   Result of element-wise subtraction of arrays.
     * @throws ArithmeticException   Throws exception when the arrays differ in
     * length.
     */
    public static double[] subtractElementwise(double[] a, int[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            double[] result = new double[a.length];
            for (int i = 0; i < a.length; i++) {
                result[i] = a[i] - b[i];
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static double[][] subtractElementwise(double[][] a, double[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            double[][] result = new double[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = a[i][j] - b[i][j];
                }
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of integers.
     * @param b   Matrix of integers.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static int[][] subtractElementwise(int[][] a, int[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            int[][] result = new int[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = a[i][j] - b[i][j];
                }
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static float[][] subtractElementwise(float[][] a, float[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            float[][] result = new float[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = a[i][j] - b[i][j];
                }
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static long[][] subtractElementwise(long[][] a, long[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            long[][] result = new long[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = a[i][j] - b[i][j];
                }
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static byte[][] subtractElementwise(byte[][] a, byte[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            byte[][] result = new byte[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = (byte) (a[i][j] - b[i][j]);
                }
            }
            return result;
        }

    }

    /**
     * Subtracts the values in the two matrices element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise sum of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static double[][] subtractElementwise(double[][] a, int[][] b) throws ArithmeticException {

        if (a.length != b.length || a[0].length != b[0].length) {
            throw new ArithmeticException();
        } else {
            double[][] result = new double[a.length][a[0].length];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    result[i][j] = a[i][j] - b[i][j];
                }
            }
            return result;
        }

    }
}

Related

  1. subtract(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)
  2. subtract(int[] a, int[] b)
  3. subtract2(double[] v1, double[] v2, double[] res)
  4. subtractArray(double[] array1, double[] array2)
  5. subtractArray(float[] arr1, float[] arr2)
  6. subtractImages(float[][] top, float[][] base)
  7. subtractInPlace(double[] a, double[] b)
  8. subtractInPlace(double[] first, double[] second)
  9. subtractInPlace(final double[] a, final double[] b)