/** * {@inheritDoc} * The following combinations of input matrices m1 and m2 are allowed: * <ul> * <li> m1 = 2-D matrix, m2 = 2-D matrix, returns $\mathbf{C} = \mathbf{AB}$ * <li> m1 = 2-D matrix, m2 = 1-D matrix, returns $\mathbf{C} = \mathbf{A}b$ * </ul> */ @Override public Matrix multiply(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); ArgChecker.isTrue(!(m1 instanceof DoubleArray), "Cannot have 1D matrix as first argument"); if (m1 instanceof DoubleMatrix) { RealMatrix t1 = CommonsMathWrapper.wrap((DoubleMatrix) m1); RealMatrix t2; if (m2 instanceof DoubleArray) { t2 = CommonsMathWrapper.wrapAsMatrix((DoubleArray) m2); } else if (m2 instanceof DoubleMatrix) { t2 = CommonsMathWrapper.wrap((DoubleMatrix) m2); } else { throw new IllegalArgumentException("Can only have 1D or 2D matrix as second argument"); } return CommonsMathWrapper.unwrap(t1.multiply(t2)); } throw new IllegalArgumentException("Can only multiply 2D and 1D matrices"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNull1DFunction() { CommonsMathWrapper.wrapUnivariate((Function<Double, Double>) null); }
@Override public double getInnerProduct(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); if (m1 instanceof DoubleArray && m2 instanceof DoubleArray) { RealVector t1 = CommonsMathWrapper.wrap((DoubleArray) m1); RealVector t2 = CommonsMathWrapper.wrap((DoubleArray) m2); return t1.dotProduct(t2); } throw new IllegalArgumentException("Can only find inner product of DoubleArray; have " + m1.getClass() + " and " + m2.getClass()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullVector() { CommonsMathWrapper.unwrap((RealVector) null); }
@Test public void testVectorAsMatrix() { RealMatrix commons = CommonsMathWrapper.wrapAsMatrix(OG_VECTOR); double[][] data = commons.getData(); assertEquals(data.length, OG_VECTOR.size()); assertEquals(data[0].length, 1); }
@Override public double getTrace(Matrix m) { ArgChecker.notNull(m, "m"); if (m instanceof DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m); return temp.getTrace(); } throw new IllegalArgumentException("Can only find trace of DoubleMatrix; have " + m.getClass()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullRealMatrix() { CommonsMathWrapper.unwrap((RealMatrix) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNull1DMatrix() { CommonsMathWrapper.wrap((DoubleArray) null); }
/** * Constructor. * @param ch The result of the Cholesky decomposition. */ public CholeskyDecompositionCommonsResult(CholeskyDecomposition ch) { ArgChecker.notNull(ch, "Cholesky decomposition"); _determinant = ch.getDeterminant(); _l = CommonsMathWrapper.unwrap(ch.getL()); _lt = CommonsMathWrapper.unwrap(ch.getLT()); _solver = ch.getSolver(); }
/** * Romberg integration method. Note that the Commons implementation fails if the lower bound is larger than the upper - * in this case, the bounds are reversed and the result negated. * @param f The function to integrate, not null * @param lower The lower bound, not null * @param upper The upper bound, not null * @return The result of the integration */ @Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(f, "f"); ArgChecker.notNull(lower, "lower bound"); ArgChecker.notNull(upper, "upper bound"); try { if (lower < upper) { return integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return -integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower); } catch (MaxCountExceededException | MathIllegalArgumentException e) { throw new MathException(e); } }
@Override public DoubleMatrix getTranspose(Matrix m) { ArgChecker.notNull(m, "m"); if (m instanceof DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m); return CommonsMathWrapper.unwrap(temp.transpose()); } throw new IllegalArgumentException("Can only find transpose of DoubleMatrix; have " + m.getClass()); }
@Override public double getNorm2(Matrix m) { ArgChecker.notNull(m, "m"); if (m instanceof DoubleArray) { RealVector temp = CommonsMathWrapper.wrap((DoubleArray) m); return temp.getNorm(); } else if (m instanceof DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m); SingularValueDecomposition svd = new SingularValueDecomposition(temp); return svd.getNorm(); } throw new IllegalArgumentException("Can only find norm2 of DoubleMatrix; have " + m.getClass()); }
/** * Creates an instance. * * @param qr The result of the QR decomposition, not null */ public QRDecompositionCommonsResult(QRDecomposition qr) { ArgChecker.notNull(qr, "qr"); _q = CommonsMathWrapper.unwrap(qr.getQ()); _r = CommonsMathWrapper.unwrap(qr.getR()); _qTranspose = _q.transpose(); _solver = qr.getSolver(); }
/** * {@inheritDoc} * @throws MathException If the Commons method could not evaluate the function; * if the Commons method could not converge. */ @Override public Double getRoot(Function<Double, Double> function, Double xLow, Double xHigh) { checkInputs(function, xLow, xHigh); UnivariateFunction wrapped = CommonsMathWrapper.wrapUnivariate(function); try { return _ridder.solve(MAX_ITER, wrapped, xLow, xHigh); } catch (TooManyEvaluationsException | NoBracketingException e) { throw new MathException(e); } }
@Override public double getCondition(Matrix m) { ArgChecker.notNull(m, "m"); if (m instanceof DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m); SingularValueDecomposition svd = new SingularValueDecomposition(temp); return svd.getConditionNumber(); } throw new IllegalArgumentException("Can only find condition number of DoubleMatrix; have " + m.getClass()); }
/** * Creates an instance. * * @param svd The result of the SV decomposition, not null */ public SVDecompositionCommonsResult(SingularValueDecomposition svd) { ArgChecker.notNull(svd, "svd"); _condition = svd.getConditionNumber(); _norm = svd.getNorm(); _rank = svd.getRank(); _s = CommonsMathWrapper.unwrap(svd.getS()); _singularValues = svd.getSingularValues(); _u = CommonsMathWrapper.unwrap(svd.getU()); _uTranspose = CommonsMathWrapper.unwrap(svd.getUT()); _v = CommonsMathWrapper.unwrap(svd.getV()); _vTranspose = CommonsMathWrapper.unwrap(svd.getVT()); _solver = svd.getSolver(); }
/** * Trapezoid integration method. Note that the Commons implementation fails if the lower bound is larger than the upper - * in this case, the bounds are reversed and the result negated. * {@inheritDoc} */ @Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(f, "f"); ArgChecker.notNull(lower, "lower"); ArgChecker.notNull(upper, "upper"); try { if (lower < upper) { return INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return -INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower); } catch (MaxCountExceededException | MathIllegalArgumentException e) { throw new MathException(e); } }