/// <summary> /// {@inheritDoc} /// </summary> protected internal override double DoSolve() { double min = this.Min; double max = this.Max; this.VerifyInterval(min, max); double absoluteAccuracy = this.AbsoluteAccuracy; double m; double fm; double fmin; while (true) { m = UnivariateSolverUtils.Midpoint(min, max); fmin = this.ComputeObjectiveValue(min); fm = this.ComputeObjectiveValue(m); if (fm * fmin > 0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (FastMath.Abs(max - min) <= absoluteAccuracy) { m = UnivariateSolverUtils.Midpoint(min, max); return(m); } } }
/// <summary> /// {@inheritDoc} /// </summary> protected internal override double DoSolve() { double min = GetMin(); double max = GetMax(); VerifyInterval(min, max); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double absoluteAccuracy = getAbsoluteAccuracy(); double absoluteAccuracy = GetAbsoluteAccuracy(); double m; double fm; double fmin; while (true) { m = UnivariateSolverUtils.Midpoint(min, max); fmin = ComputeObjectiveValue(min); fm = ComputeObjectiveValue(m); if (fm * fmin > 0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (FastMath.Abs(max - min) <= absoluteAccuracy) { m = UnivariateSolverUtils.Midpoint(min, max); return(m); } } }
/// <summary> /// Check that the endpoints specify an interval and the function takes /// opposite signs at the endpoints. /// </summary> /// <param name="lower"> Lower endpoint. </param> /// <param name="upper"> Upper endpoint. </param> /// <exception cref="NullArgumentException"> if the function has not been set. </exception> /// <exception cref="NoBracketingException"> if the function has the same sign at /// the endpoints. </exception> protected internal virtual void VerifyBracketing(double lower, double upper) { UnivariateSolverUtils.VerifyBracketing(this.function, lower, upper); }
/// <summary> /// Check that {@code lower < initial < upper}. /// </summary> /// <param name="lower"> Lower endpoint. </param> /// <param name="initial"> Initial value. </param> /// <param name="upper"> Upper endpoint. </param> /// <exception cref="NumberIsTooLargeException"> if {@code lower >= initial} or /// {@code initial >= upper}. </exception> protected internal virtual void VerifySequence(double lower, double initial, double upper) { UnivariateSolverUtils.VerifySequence(lower, initial, upper); }
/// <summary> /// Check that the endpoints specify an interval. /// </summary> /// <param name="lower"> Lower endpoint. </param> /// <param name="upper"> Upper endpoint. </param> /// <exception cref="NumberIsTooLargeException"> if {@code lower >= upper}. </exception> protected internal virtual void VerifyInterval(double lower, double upper) { UnivariateSolverUtils.VerifyInterval(lower, upper); }
/// <summary> /// Check whether the arguments form a (strictly) increasing sequence. /// </summary> /// <param name="start"> First number. </param> /// <param name="mid"> Second number. </param> /// <param name="end"> Third number. </param> /// <returns> {@code true} if the arguments form an increasing sequence. </returns> protected internal virtual bool IsSequence(double start, double mid, double end) { return(UnivariateSolverUtils.IsSequence(start, mid, end)); }
/// <summary> /// Check whether the function takes opposite signs at the endpoints. /// </summary> /// <param name="lower"> Lower endpoint. </param> /// <param name="upper"> Upper endpoint. </param> /// <returns> {@code true} if the function values have opposite signs at the /// given points. </returns> protected internal virtual bool IsBracketing(double lower, double upper) { return(UnivariateSolverUtils.IsBracketing(this.function, lower, upper)); }
/// <summary> /// Find a zero near the midpoint of {@code min} and {@code max}. /// </summary> /// <param name="f"> Function to solve. </param> /// <param name="min"> Lower bound for the interval. </param> /// <param name="max"> Upper bound for the interval. </param> /// <param name="maxEval"> Maximum number of evaluations. </param> /// <returns> the value where the function is zero. </returns> /// <exception cref="org.apache.commons.math3.exception.TooManyEvaluationsException"> /// if the maximum evaluation count is exceeded. </exception> /// <exception cref="org.apache.commons.math3.exception.NumberIsTooLargeException"> /// if {@code min >= max}. </exception> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: @Override public double solve(int maxEval, final org.apache.commons.math3.analysis.DifferentiableUnivariateFunction f, final double min, final double max) throws org.apache.commons.math3.exception.TooManyEvaluationsException public virtual double Solve(int maxEval, DifferentiableUnivariateFunction f, double min, double max) { return(base.Solve(maxEval, f, UnivariateSolverUtils.Midpoint(min, max))); }
/// <summary> /// Search for a zero inside the provided interval. /// This implementation is based on the algorithm described at page 58 of /// the book /// <quote> /// <b>Algorithms for Minimization Without Derivatives</b> /// <it>Richard P. Brent</it> /// Dover 0-486-41998-3 /// </quote> /// </summary> /// <param name="lo"> Lower bound of the search interval. </param> /// <param name="hi"> Higher bound of the search interval. </param> /// <param name="fLo"> Function value at the lower bound of the search interval. </param> /// <param name="fHi"> Function value at the higher bound of the search interval. </param> /// <returns> the value where the function is zero. </returns> private double Brent(double lo, double hi, double fLo, double fHi) { double a = lo; double fa = fLo; double b = hi; double fb = fHi; double c = a; double fc = fa; double d = b - a; double e = d; double t = this.AbsoluteAccuracy; double eps = this.RelativeAccuracy; while (true) { if (FastMath.Abs(fc) < FastMath.Abs(fb)) { a = b; b = c; c = a; fa = fb; fb = fc; fc = fa; } double tol = 2 * eps * FastMath.Abs(b) + t; double m = 0.5 * (c - b); if (FastMath.Abs(m) <= tol || UnivariateSolverUtils.Equals(fb, 0)) { return(b); } if (FastMath.Abs(e) < tol || FastMath.Abs(fa) <= FastMath.Abs(fb)) { // Force bisection. d = m; e = d; } else { double s = fb / fa; double p; double q; // The equality test (a == c) is intentional, // it is part of the original Brent's method and // it should NOT be replaced by proximity test. if (a == c) { // Linear interpolation. p = 2 * m * s; q = 1 - s; } else { // Inverse quadratic interpolation. q = fa / fc; double r = fb / fc; p = s * (2 * m * q * (q - r) - (b - a) * (r - 1)); q = (q - 1) * (r - 1) * (s - 1); } if (p > 0) { q = -q; } else { p = -p; } s = e; e = d; if (p >= 1.5 * m * q - FastMath.Abs(tol * q) || p >= FastMath.Abs(0.5 * s * q)) { // Inverse quadratic interpolation gives a value // in the wrong direction, or progress is slow. // Fall back to bisection. d = m; e = d; } else { d = p / q; } } a = b; fa = fb; if (FastMath.Abs(d) > tol) { b += d; } else if (m > 0) { b += tol; } else { b -= tol; } fb = this.ComputeObjectiveValue(b); if ((fb > 0 && fc > 0) || (fb <= 0 && fc <= 0)) { c = a; fc = fa; d = b - a; e = d; } } }
/// <summary> /// Find a zero near the midpoint of {@code min} and {@code max}. /// </summary> /// <param name="f"> Function to solve. </param> /// <param name="min"> Lower bound for the interval. </param> /// <param name="max"> Upper bound for the interval. </param> /// <param name="maxEval"> Maximum number of evaluations. </param> /// <returns> the value where the function is zero. </returns> /// <exception cref="org.apache.commons.math3.exception.TooManyEvaluationsException"> /// if the maximum evaluation count is exceeded. </exception> /// <exception cref="org.apache.commons.math3.exception.NumberIsTooLargeException"> /// if {@code min >= max}. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: @Override public double solve(int maxEval, final org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction f, final double min, final double max) throws org.apache.commons.math3.exception.TooManyEvaluationsException //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: public override double Solve(int maxEval, UnivariateDifferentiableFunction f, double min, double max) { return(base.Solve(maxEval, f, UnivariateSolverUtils.Midpoint(min, max))); }