Java Array Subtract subtract(double[] a, double[] b)

Here you can find the source of subtract(double[] a, double[] b)

Description

Subtracts the two arrays together (componentwise)

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if thetwo arrays don't have the same length.

Declaration

public static double[] subtract(double[] a, double[] b) 

Method Source Code

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

public class Main {
    /**/*ww w.j  ava  2 s.c  o m*/
     * Subtracts the two arrays together (componentwise)
     *
     * @throws IllegalArgumentException if the
     *                                  two arrays don't have the same length.
     */
    public static double[] subtract(double[] a, double[] b) {
        if (a.length != b.length) {
            throw new IllegalArgumentException(
                    "To add two arrays, they must have the same length : " + a.length + ", " + b.length);
        }
        double[] ans = copy(a);
        for (int i = 0; i < a.length; i++) {
            ans[i] -= b[i];
        }
        return (ans);
    }

    /**
     * Subtracts the two arrays together (componentwise).
     *
     * @throws IllegalArgumentException if the
     *                                  two arrays don't have the same length.
     */
    public static int[] subtract(int[] a, int[] b) {
        if (a.length != b.length) {
            throw new IllegalArgumentException(
                    "To add two arrays, they must have the same length : " + a.length + ", " + b.length);
        }
        int[] ans = copy(a);
        for (int i = 0; i < a.length; i++) {
            ans[i] -= b[i];
        }
        return (ans);
    }

    /**
     * Returns a copy of the array.
     */
    //a call to array.clone() may also work although this is a primitive type. I haven't checked
    //it even may be faster
    public static int[] copy(int[] array) {
        int[] result;
        result = new int[array.length];
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    /**
     * Returns a copy of the array.
     */
    //a call to array.clone() may also work although this is a primitive type. I haven't checked
    //it even may be faster
    public static long[] copy(long[] array) {
        long[] result;
        result = new long[array.length];
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    /**
     * Returns a copy of the array.
     */
    //a call to array.clone() may also work although this is a primitive type. I haven't checked
    //it even may be faster
    public static float[] copy(float[] array) {
        float[] result;
        result = new float[array.length];
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    /**
     * Returns a copy of the array.
     */
    //a call to array.clone() may also work although this is a primitive type. I haven't checked
    //it even may be faster
    public static double[] copy(double[] array) {
        double[] result;
        result = new double[array.length];
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    /**
     * Returns a copy of the array.
     */
    public static double[][] copy(double[][] v) {
        double[][] ans = new double[v.length][];
        for (int k = 0; k < v.length; k++)
            ans[k] = copy(v[k]);
        return (ans);
    }

    /**
     * Returns a copy of the array.
     */
    public static int[][] copy(int[][] v) {
        int[][] ans = new int[v.length][];
        for (int k = 0; k < v.length; k++)
            ans[k] = copy(v[k]);
        return (ans);
    }
}

Related

  1. arraySubtract(double[] x1, double[] x2)
  2. arraySubtract(final Double[] first, final Double[] second)
  3. subtract(byte[] a, byte[] b)
  4. subtract(double[] a, double[] b)
  5. subtract(double[] a, double[] b)
  6. subtract(double[] a, double[] b)
  7. subtract(double[] accumulator, double[] values)
  8. subtract(double[] array, double a)