BLAS { public static void gemm(double alpha, DenseMatrix matA, boolean transA, DenseMatrix matB, boolean transB, double beta, DenseMatrix matC) { int ma = transA ? matA.n : matA.m; int na = transA ? matA.m : matA.n; int mb = transB ? matB.n : matB.m; int nb = transB ? matB.m : matB.n; Preconditions.checkArgument(na == mb && ma == matC.m && nb == matC.n, "matrix size mismatched."); final int m = matC.numRows(); final int n = matC.numCols(); final int k = transA ? matA.numRows() : matA.numCols(); final int lda = matA.numRows(); final int ldb = matB.numRows(); final int ldc = matC.numRows(); final String ta = transA ? "T" : "N"; final String tb = transB ? "T" : "N"; NATIVE_BLAS.dgemm(ta, tb, m, n, k, alpha, matA.getData(), lda, matB.getData(), ldb, beta, matC.getData(), ldc); }  static double asum(int n, double[] x, int offset); static double asum(DenseVector x); static double asum(SparseVector x); static void axpy(double a, double[] x, double[] y); static void axpy(double a, DenseVector x, DenseVector y); static void axpy(double a, SparseVector x, DenseVector y); static void axpy(double a, Vector x, DenseVector y); static void axpy(double a, DenseMatrix x, DenseMatrix y); static void axpy(int n, double a, double[] x, int xOffset, double[] y, int yOffset); static double dot(double[] x, double[] y); static double dot(DenseVector x, DenseVector y); static void scal(double a, double[] x); static void scal(double a, double[] x, int xOffset, int length); static void scal(double a, DenseVector x); static void scal(double a, SparseVector x); static void scal(double a, DenseMatrix x); static void gemm(double alpha, DenseMatrix matA, boolean transA, DenseMatrix matB, boolean transB,
                            double beta, DenseMatrix matC); static void gemv(double alpha, DenseMatrix matA, boolean transA,
                            Vector x, double beta, DenseVector y); static void gemv(double alpha, DenseMatrix matA, boolean transA,
                            DenseVector x, double beta, DenseVector y); static void gemv(double alpha, DenseMatrix matA, boolean transA,
                            SparseVector x, double beta, DenseVector y);  }
@Test public void testGemm() throws Exception { DenseMatrix m32 = DenseMatrix.rand(3, 2); DenseMatrix m24 = DenseMatrix.rand(2, 4); DenseMatrix m34 = DenseMatrix.rand(3, 4); DenseMatrix m42 = DenseMatrix.rand(4, 2); DenseMatrix m43 = DenseMatrix.rand(4, 3); DenseMatrix a34 = DenseMatrix.zeros(3, 4); BLAS.gemm(1.0, m32, false, m24, false, 0., a34); Assert.assertArrayEquals(a34.getData(), simpleMM(m32, m24).getData(), TOL); BLAS.gemm(1.0, m32, false, m42, true, 0., a34); Assert.assertArrayEquals(a34.getData(), simpleMM(m32, m42.transpose()).getData(), TOL); DenseMatrix a24 = DenseMatrix.zeros(2, 4); BLAS.gemm(1.0, m32, true, m34, false, 0., a24); Assert.assertArrayEquals(a24.getData(), simpleMM(m32.transpose(), m34).getData(), TOL); BLAS.gemm(1.0, m32, true, m43, true, 0., a24); Assert.assertArrayEquals(a24.getData(), simpleMM(m32.transpose(), m43.transpose()).getData(), TOL); }
@Test public void testGemmSizeCheck() throws Exception { thrown.expect(IllegalArgumentException.class); DenseMatrix m32 = DenseMatrix.rand(3, 2); DenseMatrix m42 = DenseMatrix.rand(4, 2); DenseMatrix a34 = DenseMatrix.zeros(3, 4); BLAS.gemm(1.0, m32, false, m42, false, 0., a34); }
@Test public void testGemmTransposeSizeCheck() throws Exception { thrown.expect(IllegalArgumentException.class); DenseMatrix m32 = DenseMatrix.rand(3, 2); DenseMatrix m42 = DenseMatrix.rand(4, 2); DenseMatrix a34 = DenseMatrix.zeros(3, 4); BLAS.gemm(1.0, m32, true, m42, true, 0., a34); }