OGMatrixAlgebra extends MatrixAlgebra { @Override public DoubleMatrix2D getTranspose(final Matrix<?> m) { Validate.notNull(m, "m"); if (m instanceof IdentityMatrix) { return (IdentityMatrix) m; } if (m instanceof DoubleMatrix2D) { final double[][] data = ((DoubleMatrix2D) m).getData(); final int rows = data.length; final int cols = data[0].length; final double[][] res = new double[cols][rows]; int i, j; for (i = 0; i < cols; i++) { for (j = 0; j < rows; j++) { res[i][j] = data[j][i]; } } return new DoubleMatrix2D(res); } throw new IllegalArgumentException("Can only take transpose of DoubleMatrix2D; have " + m.getClass()); }  @Override double getCondition(final Matrix<?> m); @Override double getDeterminant(final Matrix<?> m); @Override double getInnerProduct(final Matrix<?> m1, final Matrix<?> m2); @Override DoubleMatrix2D getInverse(final Matrix<?> m); @Override double getNorm1(final Matrix<?> m); @Override double getNorm2(final Matrix<?> m); @Override double getNormInfinity(final Matrix<?> m); @Override DoubleMatrix2D getOuterProduct(final Matrix<?> m1, final Matrix<?> m2); @Override DoubleMatrix2D getPower(final Matrix<?> m, final int p); @Override double getTrace(final Matrix<?> m); @Override DoubleMatrix2D getTranspose(final Matrix<?> m); @Override Matrix<?> multiply(final Matrix<?> m1, final Matrix<?> m2); @Override DoubleMatrix2D getPower(final Matrix<?> m, final double p);  }
@Test public void testTranspose() { final DoubleMatrix2D a = new DoubleMatrix2D(new double[][] { {1, 2 }, {3, 4 }, {5, 6 } }); assertEquals(3, a.getNumberOfRows()); assertEquals(2, a.getNumberOfColumns()); DoubleMatrix2D aT = ALGEBRA.getTranspose(a); assertEquals(2, aT.getNumberOfRows()); assertEquals(3, aT.getNumberOfColumns()); }