OGMatrixAlgebra extends MatrixAlgebra { @Override public Matrix<?> multiply(final Matrix<?> m1, final Matrix<?> m2) { Validate.notNull(m1, "m1"); Validate.notNull(m2, "m2"); if (m1 instanceof IdentityMatrix) { if (m2 instanceof IdentityMatrix) { return multiply((IdentityMatrix) m1, (IdentityMatrix) m2); } else if (m2 instanceof DoubleMatrix1D) { return multiply((IdentityMatrix) m1, (DoubleMatrix1D) m2); } else if (m2 instanceof DoubleMatrix2D) { return multiply((IdentityMatrix) m1, (DoubleMatrix2D) m2); } throw new IllegalArgumentException("can only handle IdentityMatrix by DoubleMatrix2D or DoubleMatrix1D, have " + m1.getClass() + " and " + m2.getClass()); } if (m2 instanceof IdentityMatrix) { if (m1 instanceof DoubleMatrix1D) { return multiply((DoubleMatrix1D) m1, (IdentityMatrix) m2); } else if (m1 instanceof DoubleMatrix2D) { return multiply((DoubleMatrix2D) m1, (IdentityMatrix) m2); } throw new IllegalArgumentException("can only handle DoubleMatrix2D or DoubleMatrix1D by IdentityMatrix, have " + m1.getClass() + " and " + m2.getClass()); } if (m1 instanceof TridiagonalMatrix && m2 instanceof DoubleMatrix1D) { return multiply((TridiagonalMatrix) m1, (DoubleMatrix1D) m2); } else if (m1 instanceof DoubleMatrix1D && m2 instanceof TridiagonalMatrix) { return multiply((DoubleMatrix1D) m1, (TridiagonalMatrix) m2); } else if (m1 instanceof DoubleMatrix2D && m2 instanceof DoubleMatrix2D) { return multiply((DoubleMatrix2D) m1, (DoubleMatrix2D) m2); } else if (m1 instanceof DoubleMatrix2D && m2 instanceof DoubleMatrix1D) { return multiply((DoubleMatrix2D) m1, (DoubleMatrix1D) m2); } else if (m1 instanceof DoubleMatrix1D && m2 instanceof DoubleMatrix2D) { return multiply((DoubleMatrix1D) m1, (DoubleMatrix2D) m2); } throw new IllegalArgumentException( "Can only multiply two DoubleMatrix2D; a DoubleMatrix2D and a DoubleMatrix1D; or a DoubleMatrix1D and a DoubleMatrix2D. have " + m1.getClass() + " and " + m2.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(expectedExceptions = IllegalArgumentException.class) public void testMatrixSizeMismatch() { ALGEBRA.multiply(B, A); }
@Test public void testMultiply() { final DoubleMatrix2D c = (DoubleMatrix2D) ALGEBRA.multiply(A, B); final int rows = c.getNumberOfRows(); final int cols = c.getNumberOfColumns(); int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { assertEquals(c.getEntry(i, j), C.getEntry(i, j), 1e-15); } } final DoubleMatrix1D d = (DoubleMatrix1D) ALGEBRA.multiply(A, D); assertEquals(6, d.getEntry(0), 1e-15); assertEquals(0, d.getEntry(1), 1e-15); assertEquals(-3, d.getEntry(2), 1e-15); }
@Test public void testTridiagonalMultiply() { final int n = 37; double[] l = new double[n - 1]; double[] c = new double[n]; double[] u = new double[n - 1]; double[] x = new double[n]; for (int ii = 0; ii < n; ii++) { c[ii] = RANDOM.nextRandom(); x[ii] = RANDOM.nextRandom(); if (ii < n - 1) { l[ii] = RANDOM.nextRandom(); u[ii] = RANDOM.nextRandom(); } } final TridiagonalMatrix m = new TridiagonalMatrix(c, u, l); final DoubleMatrix1D xVec = new DoubleMatrix1D(x); DoubleMatrix1D y1 = (DoubleMatrix1D) ALGEBRA.multiply(m, xVec); DoubleMatrix2D full = m.toDoubleMatrix2D(); DoubleMatrix1D y2 = (DoubleMatrix1D) ALGEBRA.multiply(full, xVec); for (int i = 0; i < n; i++) { assertEquals(y1.getEntry(i), y2.getEntry(i), 1e-12); } }