MatrixAlgebra { public Matrix<?> kroneckerProduct(final Matrix<?> m1, final Matrix<?> m2) { Validate.notNull(m1, "m1"); Validate.notNull(m2, "m2"); if (m1 instanceof DoubleMatrix2D && m2 instanceof DoubleMatrix2D) { final double[][] a = ((DoubleMatrix2D) m1).getData(); final double[][] b = ((DoubleMatrix2D) m2).getData(); final int aRows = a.length; final int aCols = a[0].length; final int bRows = b.length; final int bCols = b[0].length; final int rRows = aRows * bRows; final int rCols = aCols * bCols; final double[][] res = new double[rRows][rCols]; for (int i = 0; i < aRows; i++) { for (int j = 0; j < aCols; j++) { final double t = a[i][j]; if (t != 0.0) { for (int k = 0; k < bRows; k++) { for (int l = 0; l < bCols; l++) { res[i * bRows + k][j * bCols + l] = t * b[k][l]; } } } } } return new DoubleMatrix2D(res); } throw new IllegalArgumentException("Can only calculate the Kronecker product of two DoubleMatrix2D."); }  Matrix<?> add(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> divide(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> kroneckerProduct(final Matrix<?> m1, final Matrix<?> m2); abstract Matrix<?> multiply(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> scale(final Matrix<?> m, final double scale); Matrix<?> subtract(final Matrix<?> m1, final Matrix<?> m2); abstract double getCondition(final Matrix<?> m); abstract double getDeterminant(final Matrix<?> m); abstract DoubleMatrix2D getInverse(final Matrix<?> m); abstract double getInnerProduct(final Matrix<?> m1, final Matrix<?> m2); abstract DoubleMatrix2D getOuterProduct(final Matrix<?> m1, final Matrix<?> m2); abstract double getNorm1(final Matrix<?> m); abstract double getNorm2(final Matrix<?> m); abstract double getNormInfinity(final Matrix<?> m); abstract DoubleMatrix2D getPower(final Matrix<?> m, final int p); abstract DoubleMatrix2D getPower(Matrix<?> m, double p); abstract double getTrace(final Matrix<?> m); abstract DoubleMatrix2D getTranspose(final Matrix<?> m); DoubleMatrix2D matrixTransposeMultiplyMatrix(DoubleMatrix2D a);  }
@Test public void testKroneckerProduct() { Matrix<?> m = ALGEBRA.kroneckerProduct(M3, M4); assertTrue(m instanceof DoubleMatrix2D); assertMatrixEquals(m, new DoubleMatrix2D(new double[][] { {5, 6, 10, 12 }, {7, 8, 14, 16 }, {15, 18, 20, 24 }, {21, 24, 28, 32 } })); }