DoubleMatrixUtils { public static DoubleMatrix2D getTranspose(final DoubleMatrix2D matrix) { final int rows = matrix.getNumberOfRows(); final int columns = matrix.getNumberOfColumns(); final double[][] primitives = new double[columns][rows]; for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { primitives[i][j] = matrix.getEntry(j, i); } } return new DoubleMatrix2D(primitives); } private  DoubleMatrixUtils(); static DoubleMatrix2D getTranspose(final DoubleMatrix2D matrix); static DoubleMatrix2D getIdentityMatrix2D(final int dimension); static DoubleMatrix2D getTwoDimensionalDiagonalMatrix(final DoubleMatrix1D vector); static DoubleMatrix2D getTwoDimensionalDiagonalMatrix(final double[] vector);  }
@Test public void testTransposeMatrix() { DoubleMatrix2D m = new DoubleMatrix2D(new double[][] {new double[] {1, 2, 3}, new double[] {4, 5, 6}, new double[] {7, 8, 9}}); assertEquals(DoubleMatrixUtils.getTranspose(m), new DoubleMatrix2D(new double[][] {new double[] {1, 4, 7}, new double[] {2, 5, 8}, new double[] {3, 6, 9}})); m = new DoubleMatrix2D(new double[][] {new double[] {1, 2, 3, 4, 5, 6}, new double[] {7, 8, 9, 10, 11, 12}, new double[] {13, 14, 15, 16, 17, 18}}); assertEquals(DoubleMatrixUtils.getTranspose(m), new DoubleMatrix2D(new double[][] {new double[] {1, 7, 13}, new double[] {2, 8, 14}, new double[] {3, 9, 15}, new double[] {4, 10, 16}, new double[] {5, 11, 17}, new double[] {6, 12, 18}})); }