Java Matrix Transpose transpose(final double[][] src, final double[][] dest, int l1, int l2)

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

Description

Transposes the source matrix in the destination matrix.

License

Open Source License

Parameter

Parameter Description
src the source <tt>l1</tt>-by-<tt>l2</tt> matrix to be transposed
dest the destination <tt>l2</tt>-by-<tt>l1</tt> matrix, transpose of src
l1 number of rows of source matrix
l2 number of columns of source matrix

Declaration

public static void transpose(final double[][] src, final double[][] dest, int l1, int l2) 

Method Source Code

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

public class Main {
    /**//from  w  w  w. jav a  2s .  co m
     * Transposes the source matrix in the destination matrix.
     * 
     * @param src
     *            the source <tt>l1</tt>-by-<tt>l2</tt> matrix to be transposed
     * @param dest
     *            the destination <tt>l2</tt>-by-<tt>l1</tt> matrix,
     *            transpose of src
     * @param l1
     *            number of rows of source matrix
     * @param l2
     *            number of columns of source matrix
     */
    public static void transpose(final double[][] src, final double[][] dest, int l1, int l2) {
        assert src != null;
        assert dest != null;
        assert hasShape(src, l1, l2);
        assert hasShape(dest, l2, l1);

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

    /**
     * Transposes the source matrix in the destination matrix.
     * 
     * @param src
     *            the source <tt>l1</tt>-by-<tt>l2</tt> matrix to be transposed
     * @param l1
     *            number of rows of source matrix
     * @param l2
     *            number of columns of source matrix
     * @return a new <tt>l2</tt>-by-<tt>l1</tt> matrix,
     *         transpose of src
     */
    public static final double[][] transpose(final double[][] src, int l1, int l2) {
        assert src != null;
        assert hasShape(src, l1, l2);

        final double[][] dest = new double[l2][l1];
        for (int i = 0; i < l1; i++) {
            for (int j = 0; j < l2; j++) {
                dest[j][i] = src[i][j];
            }
        }
        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. transpose(double[][] matrix)
  2. transpose(double[][] matrix)
  3. transpose(final double[][] A)
  4. transpose(final double[][] m)
  5. transpose(final double[][] original)
  6. transpose(float[] mat, float[] dest)
  7. transpose(float[][] data)
  8. transpose(int N, double src[][])
  9. transpose(int[][] input)