Java Matrix Copy copyMatrix(final double[][] src, final double[][] dest, int n)

Here you can find the source of copyMatrix(final double[][] src, final double[][] dest, int n)

Description

Copies a quadratic matrix from one array to another without any safety checks.

License

Open Source License

Parameter

Parameter Description
src the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to be copied
dest the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to hold the new copy
n number of rows and columns of source matrix

Declaration

public static void copyMatrix(final double[][] src, final double[][] dest, int n) 

Method Source Code

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

public class Main {
    /**/*  w w  w  . j av  a 2 s.co  m*/
     * 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);
        }
    }

    /**
     * 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. copyMatrix(boolean[][] matrix)
  2. copyMatrix(boolean[][] old)
  3. copyMatrix(final double[][] c)
  4. copyMatrix(final int rowsCount, final int columnsCount, final double[][] origMatrix)
  5. copyMatrix(float[] origin, float destination[])
  6. copyMatrixBlock(final double[][] src, int i_src, int j_src, final double[][] dest, int i_dest, int j_dest, int rows, int cols)
  7. copyMatrixEliminateRowAndColumn(double[][] matrix, int rowToEliminate, int colToEliminate)