Java Array Multiply multiply(final double[] a, double b, final double[] dest, int n)

Here you can find the source of multiply(final double[] a, double b, final double[] dest, int n)

Description

Multiplies the vector a (l1) with the constant b.

License

Open Source License

Parameter

Parameter Description
a a <tt>l1</tt> vector
b a real constant
dest [on return] <tt>l1</tt> vector that holds the product of <tt>a</tt> and <tt>b</tt>
n length of <tt>a</tt>

Declaration

public static void multiply(final double[] a, double b, final double[] dest, int n) 

Method Source Code

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

public class Main {
    /**/*from  w  w  w. j a v  a2s  . c o  m*/
     * Multiplies the vector <tt>a</tt> (l1) with the constant <tt>b</tt>.
     * The result is stored in <code>dest</code> (l1), that is
     * 
     * <pre>
     * dest = a * b
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param a
     *            a <tt>l1</tt> vector
     * @param b
     *            a real constant
     * @param dest
     *            [on return] <tt>l1</tt> vector that holds the
     *            product of <tt>a</tt> and <tt>b</tt>
     * @param n
     *            length of <tt>a</tt>
     */
    public static void multiply(final double[] a, double b, final double[] dest, int n) {
        assert a != null;
        assert dest != null;
        assert a.length >= n;
        assert dest.length >= n;

        for (int i = 0; i < n; i++) {
            dest[i] = a[i] * b;
        }
    }

    /**
     * Multiplies the matrix <tt>A</tt> (l1-by-l2) with the constant <tt>b</tt>.
     * The result is stored in <code>dest</code> (l1-by-l2), that is
     * 
     * <pre>
     * dest = A * b
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            a <tt>l1</tt>-by-<tt>l2</tt> matrix
     * @param b
     *            a real constant
     * @param dest
     *            [on return] <tt>l1</tt>-by-<tt>l2</tt> matrix that holds the
     *            product of <tt>A</tt> and <tt>b</tt>
     * @param l1
     *            number of rows of matrix A
     * @param l2
     *            number of columns of matrix A
     */
    public static void multiply(final double[][] A, double b, final double[][] dest, int l1, int l2) {
        assert hasShape(A, l1, l2);
        assert hasShape(dest, l1, l2);

        for (int i = 0; i < l1; i++) {
            for (int j = 0; j < l2; j++) {
                dest[i][j] = A[i][j] * b;
            }
        }
    }

    /**
     * Multiplies the matrix <tt>A</tt> (l1-by-l2) with the matrix <tt>B</tt>
     * (l2-by-l3). The result is stored in <code>dest</code> (l1-by-l3), that is
     * 
     * <pre>
     * dest = A * B
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            a <tt>l1</tt>-by-<tt>l2</tt> matrix
     * @param B
     *            a <tt>l2</tt>-by-<tt>l3</tt> matrix
     * @param dest
     *            this <tt>l1</tt>-by-<tt>l3</tt> matrix holds the
     *            product of <tt>A</tt> and <tt>B</tt> on return
     * @param l1
     *            number of rows of matrix A
     * @param l2
     *            number of columns of matrix A and rows of matrix B
     * @param l3
     *            number of columns of matrix B
     */
    public static void multiply(final double[][] A, final double[][] B, final double[][] dest, int l1, int l2,
            int l3) {
        assert hasShape(A, l1, l2);
        assert hasShape(B, l2, l3);
        assert hasShape(dest, l1, l3);

        for (int i = 0; i < l1; i++) {
            for (int j = 0; j < l3; j++) {
                dest[i][j] = A[i][0] * B[0][j];
                for (int k = 1; k < l2; k++) {
                    dest[i][j] += A[i][k] * B[k][j];
                }
            }
        }
    }

    /**
     * Multiplies the matrix <tt>A</tt> (l1-by-l2) with column vector
     * <tt>v</tt> (l2-by-1). The result is stored in <code>dest</code>
     * (l1-by-1), that is
     * 
     * <pre>
     * dest = A * v
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            a <tt>l1</tt>-by-<tt>l2</tt> matrix
     * @param v
     *            a column vector of length <tt>l2</tt>
     * @param dest
     *            a column vector of length <tt>l1</tt> that holds the product
     *            of <tt>A</tt> and <tt>v</tt> on return
     * @param l1
     *            number of rows of matrix A
     * @param l2
     *            number of columns of matrix A and length of column vector v
     */
    public static void multiply(final double[][] A, final double[] v, final double[] dest, int l1, int l2) {
        assert hasShape(A, l1, l2);
        assert v != null;
        assert dest != null;
        assert v.length >= l2;
        assert dest.length >= l1;

        for (int i = 0; i < l1; i++) {
            dest[i] = A[i][0] * v[0];
            for (int j = 1; j < l2; j++) {
                dest[i] += A[i][j] * v[j];
            }
        }
    }

    /**
     * Multiplies the row vector <tt>v</tt> (1-by-l1) with the quadratic matrix
     * <tt>A</tt> (l1-by-l2). The result is stored in <code>dest</code> (1-by-l2),
     * that is
     * 
     * <pre>
     * dest = v * A
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param vTransposed
     *            a row vector of length <tt>l1</tt>
     * @param A
     *            a <tt>l1</tt>-by-<tt>l2</tt> matrix
     * @param destTransposed
     *            this row vector of length <tt>l2</tt> hols the product of
     *            <tt>v</tt> and <tt>A</tt> on return
     * @param l1
     *            number of rows of matrix A and length of row vector v
     * @param l2
     *            number of columns of matrix A and length of column vector dest
     */
    public static void multiply(final double[] vTransposed, final double[][] A, final double[] destTransposed,
            int l1, int l2) {
        assert vTransposed != null;
        assert destTransposed != null;
        assert vTransposed.length >= l1;
        assert destTransposed.length >= l2;
        assert hasShape(A, l1, l2);

        for (int j = 0; j < l2; j++) {
            destTransposed[j] = vTransposed[0] * A[0][j];
            for (int i = 1; i < l1; i++) {
                destTransposed[j] += vTransposed[i] * A[i][j];
            }
        }
    }

    /**
     * Multiplies the column vector <tt>v1</tt> (n-by-1) with the row vector
     * <tt>v2</tt> (1-by-n). The result is stored in the quadratic matrix
     * <code>dest</code> (n-by-n), that is
     * 
     * <pre>
     * dest = v1 * v2
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param columnVector
     *            a column vector of length <tt>n</tt>
     * @param rowVector
     *            a row vector of length <tt>n</tt>
     * @param dest
     *            this quadratic <tt>n</tt>-by-<tt>n</tt> matrix holds the
     *            product of <code>columnVector</code> and
     *            <code>rowVector</code> on return
     * @param n
     *            size of the arrays to be multiplied
     */
    public static void multiply(final double[] columnVector, final double[] rowVector, final double[][] dest,
            int n) {
        assert columnVector != null;
        assert rowVector != null;
        assert dest != null;
        assert columnVector.length >= n;
        assert rowVector.length >= n;
        assert hasShape(dest, n, n);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dest[i][j] = columnVector[i] * rowVector[j];
            }
        }
    }

    /**
     * In-place version of the vector product.
     * 
     * <pre>
     * dest = v1 * v2
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param columnVector
     *            a column vector of length <tt>n</tt>
     * @param rowVector
     *            a row vector of length <tt>n</tt>
     * @param dest
     *            this quadratic <tt>n</tt>-by-<tt>n</tt> matrix holds the
     *            product of <code>columnVector</code> and
     *            <code>rowVector</code> on return
     * @param n
     *            size of the arrays to be multiplied
     */
    public static double[][] multiply(final double[] columnVector, final double[] rowVector, int n) {
        assert n > 0;

        final double[][] dest = new double[n][n];
        multiply(columnVector, rowVector, dest, n);
        return dest;
    }

    /**
     * Returns whether matrix mat has size l1-by-l2 or not 
     * @param mat   a matrix
     * @param l1   first dimension (lines)
     * @param l2   second dimension (columns)
     * @return
     */
    public static boolean hasShape(final double[][] mat, int l1, int l2) {
        assert mat != null;
        assert l1 > 0;
        assert l2 > 0;

        if (mat.length != l1) {
            return false;
        }
        for (int i = 0; i < mat.length; i++) {
            if (mat[i].length != l2) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasShape(final double[][][] mat3d, int l1, int l2, int l3) {
        assert mat3d != null;
        assert l1 > 0;
        assert l2 > 0;
        assert l3 > 0;

        if (mat3d.length != l1) {
            return false;
        }
        for (int i = 0; i < mat3d.length; i++) {
            if (mat3d[i].length != l2) {
                return false;
            }
            for (int j = 0; j < mat3d[i].length; j++) {
                if (mat3d[i][j].length != l3) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. multiply(double[] a, double[] b)
  2. multiply(double[] array, double multiplier)
  3. multiply(double[] multiplicand, double[] factor, double multiplicandAdjustment, double factorAdjustment)
  4. multiply(double[] source, double num)
  5. multiply(double[] values, int offset, int stride, int length, double multiplier)
  6. multiply(final double[] inout, final double[] in)
  7. multiply(final double[] lhs, final double[] rhs)
  8. multiply(final double[] v1, final double[] v2)
  9. multiply(final double[] x, final double[] y)