Java Matrix Transpose transposeQuad(final double[][] src, int n)

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

Description

Transposes the given quadratic matrix.

License

Open Source License

Parameter

Parameter Description
src the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to be transposed
n size of the matrix

Return

a new n-by-n matrix, transpose of src

Declaration

public static double[][] transposeQuad(final double[][] src, int n) 

Method Source Code

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

public class Main {
    /**//from   w  w w  .j  a v  a2 s .c om
     * Transposes the given quadratic matrix.
     * 
     * @param src
     *            the quadratic <tt>n</tt>-by-<tt>n</tt> matrix to be transposed
     * @param n
     *            size of the matrix
     * @return a new <tt>n</tt>-by-<tt>n</tt> matrix,
     *         transpose of src
     */
    public static double[][] transposeQuad(final double[][] src, int n) {
        assert hasShape(src, n, n);

        final double[][] dest = new double[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                dest[i][j] = src[j][i];
                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. transposeMatrix(double[][] m)
  2. transposeMatrix(double[][] m)
  3. transposeMatrix(final float[] msrc, final int msrc_offset, final float[] mres, final int mres_offset)
  4. transposeMatrix3x3(float[] result, float[] m)
  5. transposematrixmultiply(final double[][] A, final double[] b)
  6. transposeSquareMatrix(T[][] matrixT)
  7. transposeValue(long[] valSet, int DEPTH)