Java Matrix Clone cloneMatrix(final double[][] src, int l1, int l2)

Here you can find the source of cloneMatrix(final double[][] src, int l1, int l2)

Description

Returns a new matrix exactly like a given matrix.

License

Open Source License

Parameter

Parameter Description
src the <tt>l1</tt>-by-<tt>l2</tt> matrix to be copied
l1 number of rows of source matrix
l2 number of columns of source matrix

Return

dest the l1-by-l2 matrix cloned from the source

Declaration

public static double[][] cloneMatrix(final double[][] src, int l1, int l2) 

Method Source Code

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

public class Main {
    /**//  w  w  w  .j  a v  a  2  s  .  com
     * Returns a new matrix exactly like a given matrix.
     * 
     * @param src
     *            the <tt>l1</tt>-by-<tt>l2</tt> matrix to be copied
     * @param l1
     *            number of rows of source matrix
     * @param l2
     *            number of columns of source matrix
     * @return dest
     *            the <tt>l1</tt>-by-<tt>l2</tt> matrix cloned from the source
     */
    public static double[][] cloneMatrix(final double[][] src, int l1, int l2) {
        assert hasShape(src, l1, l2);

        final double[][] mat = empty(l1, l2);
        copyMatrix(src, mat, l1, l2);
        return mat;
    }

    /**
     * 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;
    }

    /**
    * Returns a l1 x l2 matrix without filling anything.
    * @param l1    number of lines of the matrix
    * @param l2    number of columns of the matrix
    * @return      l1 x l2 matrix.
    */
    public static double[][] empty(int l1, int l2) {
        assert l1 > 0;
        assert l2 > 0;
        return new double[l1][l2];
    }

    /**
     * Copies a quadratic matrix from one array to another without any safety
     * checks.
     * 
     * @param src
     *            the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to be copied
     * @param dest
     *            the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to hold the new
     *            copy
     * @param n
     *            number of rows and columns of source matrix
     */
    public static void copyMatrix(final double[][] src, final double[][] dest, int n) {
        assert hasShape(src, n, n);
        assert hasShape(dest, n, n);
        assert src != dest;

        for (int i = 0; i < n; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, n);
        }
    }

    /**
     * Copies a matrix from one array to another without any safety checks.
     * 
     * @param src
     *            the <tt>l1</tt>-by-<tt>l2</tt> matrix to be copied
     * @param dest
     *            the <tt>l1</tt>-by-<tt>l2</tt> matrix to hold the new copy
     * @param l1
     *            number of rows of source matrix
     * @param l2
     *            number of columns of source matrix
     */
    public static void copyMatrix(final double[][] src, final double[][] dest, int l1, int l2) {
        assert hasShape(src, l1, l2);
        assert hasShape(dest, l1, l2);

        for (int i = 0; i < l1; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, l2);
        }
    }
}

Related

  1. cloneMatrix(final double[][] matrix)
  2. cloneMatrix(float[] in)
  3. cloneMatrix(int[][] m)
  4. cloneMatrix(int[][] matrix)