/// <summary> /// {@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> /// </summary> public override Matrix multiply(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); ArgChecker.isTrue(!(m1 is DoubleArray), "Cannot have 1D matrix as first argument"); if (m1 is DoubleMatrix) { RealMatrix t1 = CommonsMathWrapper.wrap((DoubleMatrix)m1); RealMatrix t2; if (m2 is DoubleArray) { t2 = CommonsMathWrapper.wrapAsMatrix((DoubleArray)m2); } else if (m2 is DoubleMatrix) { t2 = CommonsMathWrapper.wrap((DoubleMatrix)m2); } else { throw new System.ArgumentException("Can only have 1D or 2D matrix as second argument"); } return(CommonsMathWrapper.unwrap(t1.multiply(t2))); } throw new System.ArgumentException("Can only multiply 2D and 1D matrices"); }
public override DoubleMatrix getTranspose(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); return(CommonsMathWrapper.unwrap(temp.transpose())); } throw new System.ArgumentException("Can only find transpose of DoubleMatrix; have " + m.GetType()); }
public override double getDeterminant(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); LUDecomposition lud = new LUDecomposition(temp); return(lud.Determinant); } throw new System.ArgumentException("Can only find determinant of DoubleMatrix; have " + m.GetType()); }
public override double getCondition(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); SingularValueDecomposition svd = new SingularValueDecomposition(temp); return(svd.ConditionNumber); } throw new System.ArgumentException("Can only find condition number of DoubleMatrix; have " + m.GetType()); }
public override DoubleMatrix getInverse(Matrix m) { ArgChecker.notNull(m, "matrix was null"); if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); SingularValueDecomposition sv = new SingularValueDecomposition(temp); RealMatrix inv = sv.Solver.Inverse; return(CommonsMathWrapper.unwrap(inv)); } throw new System.ArgumentException("Can only find inverse of DoubleMatrix; have " + m.GetType()); }
public override double getInnerProduct(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); if (m1 is DoubleArray && m2 is DoubleArray) { RealVector t1 = CommonsMathWrapper.wrap((DoubleArray)m1); RealVector t2 = CommonsMathWrapper.wrap((DoubleArray)m2); return(t1.dotProduct(t2)); } throw new System.ArgumentException("Can only find inner product of DoubleArray; have " + m1.GetType() + " and " + m2.GetType()); }
/// <summary> /// {@inheritDoc} </summary> /// <exception cref="MathException"> If the Commons method could not evaluate the function; /// if the Commons method could not converge. </exception> public override double?getRoot(System.Func <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 (Exception e) when(e is TooManyEvaluationsException || e is NoBracketingException) { throw new MathException(e); } }
public override DoubleMatrix getPower(Matrix m, int p) { ArgChecker.notNull(m, "m"); RealMatrix temp; if (m is DoubleMatrix) { temp = CommonsMathWrapper.wrap((DoubleMatrix)m); } else { throw new System.ArgumentException("Can only find powers of DoubleMatrix; have " + m.GetType()); } return(CommonsMathWrapper.unwrap(temp.power(p))); }
public override double getNorm2(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleArray) { RealVector temp = CommonsMathWrapper.wrap((DoubleArray)m); return(temp.Norm); } else if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); SingularValueDecomposition svd = new SingularValueDecomposition(temp); return(svd.Norm); } throw new System.ArgumentException("Can only find norm2 of DoubleMatrix; have " + m.GetType()); }
/// <summary> /// {@inheritDoc} /// </summary> public virtual CholeskyDecompositionResult apply(DoubleMatrix x) { ArgChecker.notNull(x, "x"); RealMatrix temp = CommonsMathWrapper.wrap(x); CholeskyDecomposition cholesky; try { cholesky = new CholeskyDecomposition(temp); } catch (Exception e) { throw new MathException(e.ToString()); } return(new CholeskyDecompositionCommonsResult(cholesky)); }
/// <summary> /// Simpson's integration method. /// <para> /// 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. /// /// </para> /// </summary> /// <param name="f"> The function to integrate, not null </param> /// <param name="lower"> The lower bound, not null </param> /// <param name="upper"> The upper bound, not null </param> /// <returns> The result of the integration </returns> public virtual double?integrate(System.Func <double, double> f, double?lower, double?upper) { ArgChecker.notNull(f, "function"); 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 (Exception e) when(e is NumberIsTooSmallException || e is NumberIsTooLargeException) { throw new MathException(e); } }
/// <summary> /// 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} /// </summary> public virtual double?integrate(System.Func <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 (Exception e) when(e is MaxCountExceededException || e is MathIllegalArgumentException) { throw new MathException(e); } }
public override double getNorm1(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleArray) { RealVector temp = CommonsMathWrapper.wrap((DoubleArray)m); return(temp.L1Norm); } else if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); // TODO find if commons implements this anywhere, so we are not doing it // by hand double max = 0.0; for (int col = temp.ColumnDimension - 1; col >= 0; col--) { max = Math.Max(max, temp.getColumnVector(col).L1Norm); } return(max); } throw new System.ArgumentException("Can only find norm1 of DoubleMatrix; have " + m.GetType()); }
public override double getNormInfinity(Matrix m) { ArgChecker.notNull(m, "m"); if (m is DoubleArray) { RealVector temp = CommonsMathWrapper.wrap((DoubleArray)m); return(temp.LInfNorm); } else if (m is DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix)m); //REVIEW Commons getNorm() is wrong - it returns the column norm // TODO find if commons implements this anywhere, so we are not doing it // by hand double max = 0.0; for (int row = temp.RowDimension - 1; row >= 0; row--) { max = Math.Max(max, temp.getRowVector(row).L1Norm); } return(max); } throw new System.ArgumentException("Can only find normInfinity of DoubleMatrix; have " + m.GetType()); }