Java Array Multiply multiplyElementwise(double[] a, int[] b)

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

Description

Multiplies an array of doubles and an array of integers element-wise.

License

Open Source License

Parameter

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

Exception

Parameter Description
ArithmeticException Throws an exception when the two vectors arenot of the same size.

Return

Element-wise product of the arrays.

Declaration

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

Method Source Code

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

public class Main {
    /**/* ww  w.j  a  v a  2 s.c o m*/
     * Multiplies an array of doubles and an array of integers element-wise.
     *
     * @param a   Array of doubles.
     * @param b   Array of integers.
     * @return   Element-wise product of the arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static double[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two arrays of doubles element-wise.
     *
     * @param a   Array of doubles.
     * @param b   Array of doubles.
     * @return   Element-wise product of the two arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static double[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two arrays of integers element-wise.
     *
     * @param a   Array of integers.
     * @param b   Array of integers.
     * @return   Element-wise product of the two arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static int[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two arrays of floats element-wise.
     *
     * @param a   Array of floats.
     * @param b   Array of floats.
     * @return   Element-wise product of the two arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static float[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two arrays of longs element-wise.
     *
     * @param a   Array of longs.
     * @param b   Array of longs.
     * @return   Element-wise product of the two arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static long[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two arrays of bytes element-wise.
     *
     * @param a   Array of bytes.
     * @param b   Array of bytes.
     * @return   Element-wise product of the two arrays.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same size.
     */
    public static byte[] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two matrices of doubles element-wise.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Element-wise product of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static double[][] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two matrices of integers element-wise.
     *
     * @param a   Matrix of integers.
     * @param b   Matrix of integers.
     * @return   Element-wise product of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static int[][] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two matrices of floats element-wise.
     *
     * @param a   Matrix of floats.
     * @param b   Matrix of floats.
     * @return   Element-wise product of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static float[][] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two matrices of longs element-wise.
     *
     * @param a   Matrix of longs.
     * @param b   Matrix of longs.
     * @return   Element-wise product of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static long[][] multiplyElementwise(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;
        }

    }

    /**
     * Multiplies two matrices of bytes element-wise.
     *
     * @param a   Matrix of bytes.
     * @param b   Matrix of bytes.
     * @return   Element-wise product of the two matrices.
     * @throws ArithmeticException   Throws an exception when the two matrices are
     * not of the same size.
     */
    public static int[][] multiplyElementwise(byte[][] a, byte[][] 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;
        }

    }
}

Related

  1. multiplyByScalar(double scalar, double[] vector)
  2. multiplyBytes(byte[] in, int count, int mul)
  3. multiplyComplex(double[] one, double[] two)
  4. multiplyComplexVectors(float[] cA, float[] cB, long limit)
  5. multiplycst(int k, double[] t)
  6. multiplyHarmonics(float[] powerSpectrumInOut, int nHarmonics)
  7. multiplyInPlace(double[] img, double val)
  8. multiplyInto(double[] out, double[] a, double[] b)
  9. multiplyP(int[] x)