You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/01/18 14:07:24 UTC
svn commit: r735449 [2/3] - in /commons/proper/math/trunk: ./
src/java/org/apache/commons/math/analysis/
src/java/org/apache/commons/math/analysis/integration/
src/java/org/apache/commons/math/analysis/interpolation/
src/java/org/apache/commons/math/an...
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/MullerSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/MullerSolver.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/MullerSolver.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/MullerSolver.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,349 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxIterationsExceededException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.util.MathUtils;
+
+/**
+ * Implements the <a href="http://mathworld.wolfram.com/MullersMethod.html">
+ * Muller's Method</a> for root finding of real univariate functions. For
+ * reference, see <b>Elementary Numerical Analysis</b>, ISBN 0070124477,
+ * chapter 3.
+ * <p>
+ * Muller's method applies to both real and complex functions, but here we
+ * restrict ourselves to real functions. Methods solve() and solve2() find
+ * real zeros, using different ways to bypass complex arithmetics.</p>
+ *
+ * @version $Revision: 729291 $ $Date: 2008-12-24 11:47:53 +0100 (mer., 24 déc. 2008) $
+ * @since 1.2
+ */
+public class MullerSolver extends UnivariateRealSolverImpl {
+
+ /** serializable version identifier */
+ private static final long serialVersionUID = 7768903775784754323L;
+
+ /**
+ * Construct a solver for the given function.
+ *
+ * @param f function to solve
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method.
+ */
+ @Deprecated
+ public MullerSolver(UnivariateRealFunction f) {
+ super(f, 100, 1E-6);
+ }
+
+ /**
+ * Construct a solver.
+ */
+ public MullerSolver() {
+ super(100, 1E-6);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max, final double initial)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max, initial);
+ }
+
+ /**
+ * Find a real root in the given interval with initial value.
+ * <p>
+ * Requires bracketing condition.</p>
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @param initial the start value to use
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max, final double initial)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ // check for zeros before verifying bracketing
+ if (f.value(min) == 0.0) { return min; }
+ if (f.value(max) == 0.0) { return max; }
+ if (f.value(initial) == 0.0) { return initial; }
+
+ verifyBracketing(min, max, f);
+ verifySequence(min, initial, max);
+ if (isBracketing(min, initial, f)) {
+ return solve(f, min, initial);
+ } else {
+ return solve(f, initial, max);
+ }
+ }
+
+ /**
+ * Find a real root in the given interval.
+ * <p>
+ * Original Muller's method would have function evaluation at complex point.
+ * Since our f(x) is real, we have to find ways to avoid that. Bracketing
+ * condition is one way to go: by requiring bracketing in every iteration,
+ * the newly computed approximation is guaranteed to be real.</p>
+ * <p>
+ * Normally Muller's method converges quadratically in the vicinity of a
+ * zero, however it may be very slow in regions far away from zeros. For
+ * example, f(x) = exp(x) - 1, min = -50, max = 100. In such case we use
+ * bisection as a safety backup if it performs very poorly.</p>
+ * <p>
+ * The formulas here use divided differences directly.</p>
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ // [x0, x2] is the bracketing interval in each iteration
+ // x1 is the last approximation and an interpolation point in (x0, x2)
+ // x is the new root approximation and new x1 for next round
+ // d01, d12, d012 are divided differences
+ double x0, x1, x2, x, oldx, y0, y1, y2, y;
+ double d01, d12, d012, c1, delta, xplus, xminus, tolerance;
+
+ x0 = min; y0 = f.value(x0);
+ x2 = max; y2 = f.value(x2);
+ x1 = 0.5 * (x0 + x2); y1 = f.value(x1);
+
+ // check for zeros before verifying bracketing
+ if (y0 == 0.0) { return min; }
+ if (y2 == 0.0) { return max; }
+ verifyBracketing(min, max, f);
+
+ int i = 1;
+ oldx = Double.POSITIVE_INFINITY;
+ while (i <= maximalIterationCount) {
+ // Muller's method employs quadratic interpolation through
+ // x0, x1, x2 and x is the zero of the interpolating parabola.
+ // Due to bracketing condition, this parabola must have two
+ // real roots and we choose one in [x0, x2] to be x.
+ d01 = (y1 - y0) / (x1 - x0);
+ d12 = (y2 - y1) / (x2 - x1);
+ d012 = (d12 - d01) / (x2 - x0);
+ c1 = d01 + (x1 - x0) * d012;
+ delta = c1 * c1 - 4 * y1 * d012;
+ xplus = x1 + (-2.0 * y1) / (c1 + Math.sqrt(delta));
+ xminus = x1 + (-2.0 * y1) / (c1 - Math.sqrt(delta));
+ // xplus and xminus are two roots of parabola and at least
+ // one of them should lie in (x0, x2)
+ x = isSequence(x0, xplus, x2) ? xplus : xminus;
+ y = f.value(x);
+
+ // check for convergence
+ tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
+ if (Math.abs(x - oldx) <= tolerance) {
+ setResult(x, i);
+ return result;
+ }
+ if (Math.abs(y) <= functionValueAccuracy) {
+ setResult(x, i);
+ return result;
+ }
+
+ // Bisect if convergence is too slow. Bisection would waste
+ // our calculation of x, hopefully it won't happen often.
+ // the real number equality test x == x1 is intentional and
+ // completes the proximity tests above it
+ boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) ||
+ (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) ||
+ (x == x1);
+ // prepare the new bracketing interval for next iteration
+ if (!bisect) {
+ x0 = x < x1 ? x0 : x1;
+ y0 = x < x1 ? y0 : y1;
+ x2 = x > x1 ? x2 : x1;
+ y2 = x > x1 ? y2 : y1;
+ x1 = x; y1 = y;
+ oldx = x;
+ } else {
+ double xm = 0.5 * (x0 + x2);
+ double ym = f.value(xm);
+ if (MathUtils.sign(y0) + MathUtils.sign(ym) == 0.0) {
+ x2 = xm; y2 = ym;
+ } else {
+ x0 = xm; y0 = ym;
+ }
+ x1 = 0.5 * (x0 + x2);
+ y1 = f.value(x1);
+ oldx = Double.POSITIVE_INFINITY;
+ }
+ i++;
+ }
+ throw new MaxIterationsExceededException(maximalIterationCount);
+ }
+
+ /**
+ * Find a real root in the given interval.
+ * <p>
+ * solve2() differs from solve() in the way it avoids complex operations.
+ * Except for the initial [min, max], solve2() does not require bracketing
+ * condition, e.g. f(x0), f(x1), f(x2) can have the same sign. If complex
+ * number arises in the computation, we simply use its modulus as real
+ * approximation.</p>
+ * <p>
+ * Because the interval may not be bracketing, bisection alternative is
+ * not applicable here. However in practice our treatment usually works
+ * well, especially near real zeros where the imaginary part of complex
+ * approximation is often negligible.</p>
+ * <p>
+ * The formulas here do not use divided differences directly.</p>
+ *
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ * @deprecated replaced by {@link #solve2(UnivariateRealFunction, double, double)
+ * since 2.0
+ */
+ @Deprecated
+ public double solve2(final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+ return solve2(f, min, max);
+ }
+
+ /**
+ * Find a real root in the given interval.
+ * <p>
+ * solve2() differs from solve() in the way it avoids complex operations.
+ * Except for the initial [min, max], solve2() does not require bracketing
+ * condition, e.g. f(x0), f(x1), f(x2) can have the same sign. If complex
+ * number arises in the computation, we simply use its modulus as real
+ * approximation.</p>
+ * <p>
+ * Because the interval may not be bracketing, bisection alternative is
+ * not applicable here. However in practice our treatment usually works
+ * well, especially near real zeros where the imaginary part of complex
+ * approximation is often negligible.</p>
+ * <p>
+ * The formulas here do not use divided differences directly.</p>
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ */
+ public double solve2(final UnivariateRealFunction f,
+ final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ // x2 is the last root approximation
+ // x is the new approximation and new x2 for next round
+ // x0 < x1 < x2 does not hold here
+ double x0, x1, x2, x, oldx, y0, y1, y2, y;
+ double q, A, B, C, delta, denominator, tolerance;
+
+ x0 = min; y0 = f.value(x0);
+ x1 = max; y1 = f.value(x1);
+ x2 = 0.5 * (x0 + x1); y2 = f.value(x2);
+
+ // check for zeros before verifying bracketing
+ if (y0 == 0.0) { return min; }
+ if (y1 == 0.0) { return max; }
+ verifyBracketing(min, max, f);
+
+ int i = 1;
+ oldx = Double.POSITIVE_INFINITY;
+ while (i <= maximalIterationCount) {
+ // quadratic interpolation through x0, x1, x2
+ q = (x2 - x1) / (x1 - x0);
+ A = q * (y2 - (1 + q) * y1 + q * y0);
+ B = (2*q + 1) * y2 - (1 + q) * (1 + q) * y1 + q * q * y0;
+ C = (1 + q) * y2;
+ delta = B * B - 4 * A * C;
+ if (delta >= 0.0) {
+ // choose a denominator larger in magnitude
+ double dplus = B + Math.sqrt(delta);
+ double dminus = B - Math.sqrt(delta);
+ denominator = Math.abs(dplus) > Math.abs(dminus) ? dplus : dminus;
+ } else {
+ // take the modulus of (B +/- Math.sqrt(delta))
+ denominator = Math.sqrt(B * B - delta);
+ }
+ if (denominator != 0) {
+ x = x2 - 2.0 * C * (x2 - x1) / denominator;
+ // perturb x if it exactly coincides with x1 or x2
+ // the equality tests here are intentional
+ while (x == x1 || x == x2) {
+ x += absoluteAccuracy;
+ }
+ } else {
+ // extremely rare case, get a random number to skip it
+ x = min + Math.random() * (max - min);
+ oldx = Double.POSITIVE_INFINITY;
+ }
+ y = f.value(x);
+
+ // check for convergence
+ tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
+ if (Math.abs(x - oldx) <= tolerance) {
+ setResult(x, i);
+ return result;
+ }
+ if (Math.abs(y) <= functionValueAccuracy) {
+ setResult(x, i);
+ return result;
+ }
+
+ // prepare the next iteration
+ x0 = x1; y0 = y1;
+ x1 = x2; y1 = y2;
+ x2 = x; y2 = y;
+ oldx = x;
+ i++;
+ }
+ throw new MaxIterationsExceededException(maximalIterationCount);
+ }
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/NewtonSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/NewtonSolver.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/NewtonSolver.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/NewtonSolver.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.analysis.solvers;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.MaxIterationsExceededException;
+import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
+/**
+ * Implements <a href="http://mathworld.wolfram.com/NewtonsMethod.html">
+ * Newton's Method</a> for finding zeros of real univariate functions.
+ * <p>
+ * The function should be continuous but not necessarily smooth.</p>
+ *
+ * @version $Revision: 729291 $ $Date: 2008-12-24 11:47:53 +0100 (mer., 24 déc. 2008) $
+ */
+public class NewtonSolver extends UnivariateRealSolverImpl {
+
+ /** Serializable version identifier */
+ private static final long serialVersionUID = 7579593514004764309L;
+
+ /**
+ * Construct a solver for the given function.
+ * @param f function to solve.
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method.
+ */
+ @Deprecated
+ public NewtonSolver(DifferentiableUnivariateRealFunction f) {
+ super(f, 100, 1E-6);
+ }
+
+ /**
+ * Construct a solver.
+ */
+ public NewtonSolver() {
+ super(100, 1E-6);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max)
+ throws MaxIterationsExceededException,
+ FunctionEvaluationException {
+ return solve(f, min, max);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max, final double startValue)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+ return solve(f, min, max, startValue);
+ }
+
+ /**
+ * Find a zero near the midpoint of <code>min</code> and <code>max</code>.
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @return the value where the function is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function or derivative
+ * @throws IllegalArgumentException if min is not less than max
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+ return solve(f, min, max, UnivariateRealSolverUtils.midpoint(min, max));
+ }
+
+ /**
+ * Find a zero near the value <code>startValue</code>.
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval (ignored).
+ * @param max the upper bound for the interval (ignored).
+ * @param startValue the start value to use.
+ * @return the value where the function is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function or derivative
+ * @throws IllegalArgumentException if startValue is not between min and max or
+ * if function is not a {@link DifferentiableUnivariateRealFunction} instance
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max, final double startValue)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ try {
+
+ final UnivariateRealFunction derivative =
+ ((DifferentiableUnivariateRealFunction) f).derivative();
+ clearResult();
+ verifySequence(min, startValue, max);
+
+ double x0 = startValue;
+ double x1;
+
+ int i = 0;
+ while (i < maximalIterationCount) {
+
+ x1 = x0 - (f.value(x0) / derivative.value(x0));
+ if (Math.abs(x1 - x0) <= absoluteAccuracy) {
+ setResult(x1, i);
+ return x1;
+ }
+
+ x0 = x1;
+ ++i;
+ }
+
+ throw new MaxIterationsExceededException(maximalIterationCount);
+ } catch (ClassCastException cce) {
+ throw MathRuntimeException.createIllegalArgumentException("function is not differentiable",
+ null);
+ }
+ }
+
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxIterationsExceededException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.util.MathUtils;
+
+/**
+ * Implements the <a href="http://mathworld.wolfram.com/RiddersMethod.html">
+ * Ridders' Method</a> for root finding of real univariate functions. For
+ * reference, see C. Ridders, <i>A new algorithm for computing a single root
+ * of a real continuous function </i>, IEEE Transactions on Circuits and
+ * Systems, 26 (1979), 979 - 980.
+ * <p>
+ * The function should be continuous but not necessarily smooth.</p>
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (dim., 07 déc. 2008) $
+ * @since 1.2
+ */
+public class RiddersSolver extends UnivariateRealSolverImpl {
+
+ /** serializable version identifier */
+ private static final long serialVersionUID = -1556464494585337088L;
+
+ /**
+ * Construct a solver for the given function.
+ *
+ * @param f function to solve
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method.
+ */
+ @Deprecated
+ public RiddersSolver(UnivariateRealFunction f) {
+ super(f, 100, 1E-6);
+ }
+
+ /**
+ * Construct a solver.
+ */
+ public RiddersSolver() {
+ super(100, 1E-6);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max, final double initial)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max, initial);
+ }
+
+ /**
+ * Find a root in the given interval with initial value.
+ * <p>
+ * Requires bracketing condition.</p>
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @param initial the start value to use
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max, final double initial)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ // check for zeros before verifying bracketing
+ if (f.value(min) == 0.0) { return min; }
+ if (f.value(max) == 0.0) { return max; }
+ if (f.value(initial) == 0.0) { return initial; }
+
+ verifyBracketing(min, max, f);
+ verifySequence(min, initial, max);
+ if (isBracketing(min, initial, f)) {
+ return solve(f, min, initial);
+ } else {
+ return solve(f, initial, max);
+ }
+ }
+
+ /**
+ * Find a root in the given interval.
+ * <p>
+ * Requires bracketing condition.</p>
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @return the point at which the function value is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if any parameters are invalid
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ // [x1, x2] is the bracketing interval in each iteration
+ // x3 is the midpoint of [x1, x2]
+ // x is the new root approximation and an endpoint of the new interval
+ double x1, x2, x3, x, oldx, y1, y2, y3, y, delta, correction, tolerance;
+
+ x1 = min; y1 = f.value(x1);
+ x2 = max; y2 = f.value(x2);
+
+ // check for zeros before verifying bracketing
+ if (y1 == 0.0) { return min; }
+ if (y2 == 0.0) { return max; }
+ verifyBracketing(min, max, f);
+
+ int i = 1;
+ oldx = Double.POSITIVE_INFINITY;
+ while (i <= maximalIterationCount) {
+ // calculate the new root approximation
+ x3 = 0.5 * (x1 + x2);
+ y3 = f.value(x3);
+ if (Math.abs(y3) <= functionValueAccuracy) {
+ setResult(x3, i);
+ return result;
+ }
+ delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing
+ correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
+ (x3 - x1) / Math.sqrt(delta);
+ x = x3 - correction; // correction != 0
+ y = f.value(x);
+
+ // check for convergence
+ tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
+ if (Math.abs(x - oldx) <= tolerance) {
+ setResult(x, i);
+ return result;
+ }
+ if (Math.abs(y) <= functionValueAccuracy) {
+ setResult(x, i);
+ return result;
+ }
+
+ // prepare the new interval for next iteration
+ // Ridders' method guarantees x1 < x < x2
+ if (correction > 0.0) { // x1 < x < x3
+ if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
+ x2 = x; y2 = y;
+ } else {
+ x1 = x; x2 = x3;
+ y1 = y; y2 = y3;
+ }
+ } else { // x3 < x < x2
+ if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
+ x1 = x; y1 = y;
+ } else {
+ x1 = x3; x2 = x;
+ y1 = y3; y2 = y;
+ }
+ }
+ oldx = x;
+ i++;
+ }
+ throw new MaxIterationsExceededException(maximalIterationCount);
+ }
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/SecantSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/SecantSolver.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/SecantSolver.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/SecantSolver.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+import java.io.Serializable;
+
+import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxIterationsExceededException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
+
+/**
+ * Implements a modified version of the
+ * <a href="http://mathworld.wolfram.com/SecantMethod.html">secant method</a>
+ * for approximating a zero of a real univariate function.
+ * <p>
+ * The algorithm is modified to maintain bracketing of a root by successive
+ * approximations. Because of forced bracketing, convergence may be slower than
+ * the unrestricted secant algorithm. However, this implementation should in
+ * general outperform the
+ * <a href="http://mathworld.wolfram.com/MethodofFalsePosition.html">
+ * regula falsi method.</a></p>
+ * <p>
+ * The function is assumed to be continuous but not necessarily smooth.</p>
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (dim., 07 déc. 2008) $
+ */
+public class SecantSolver extends UnivariateRealSolverImpl implements Serializable {
+
+ /** Serializable version identifier */
+ private static final long serialVersionUID = 2477470651270304246L;
+
+ /**
+ * Construct a solver for the given function.
+ * @param f function to solve.
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method.
+ */
+ @Deprecated
+ public SecantSolver(UnivariateRealFunction f) {
+ super(f, 100, 1E-6);
+ }
+
+ /**
+ * Construct a solver.
+ */
+ public SecantSolver() {
+ super(100, 1E-6);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max);
+ }
+
+ /** {@inheritDoc} */
+ @Deprecated
+ public double solve(final double min, final double max, final double initial)
+ throws ConvergenceException, FunctionEvaluationException {
+ return solve(f, min, max, initial);
+ }
+
+ /**
+ * Find a zero in the given interval.
+ *
+ * @param f the function to solve
+ * @param min the lower bound for the interval
+ * @param max the upper bound for the interval
+ * @param initial the start value to use (ignored)
+ * @return the value where the function is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min is not less than max or the
+ * signs of the values of the function at the endpoints are not opposites
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max, final double initial)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+ return solve(f, min, max);
+ }
+
+ /**
+ * Find a zero in the given interval.
+ * @param f the function to solve
+ * @param min the lower bound for the interval.
+ * @param max the upper bound for the interval.
+ * @return the value where the function is zero
+ * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min is not less than max or the
+ * signs of the values of the function at the endpoints are not opposites
+ */
+ public double solve(final UnivariateRealFunction f,
+ final double min, final double max)
+ throws MaxIterationsExceededException, FunctionEvaluationException {
+
+ clearResult();
+ verifyInterval(min, max);
+
+ // Index 0 is the old approximation for the root.
+ // Index 1 is the last calculated approximation for the root.
+ // Index 2 is a bracket for the root with respect to x0.
+ // OldDelta is the length of the bracketing interval of the last
+ // iteration.
+ double x0 = min;
+ double x1 = max;
+ double y0 = f.value(x0);
+ double y1 = f.value(x1);
+
+ // Verify bracketing
+ if (y0 * y1 >= 0) {
+ throw new IllegalArgumentException
+ ("Function values at endpoints do not have different signs." +
+ " Endpoints: [" + min + "," + max + "]" +
+ " Values: [" + y0 + "," + y1 + "]");
+ }
+
+ double x2 = x0;
+ double y2 = y0;
+ double oldDelta = x2 - x1;
+ int i = 0;
+ while (i < maximalIterationCount) {
+ if (Math.abs(y2) < Math.abs(y1)) {
+ x0 = x1;
+ x1 = x2;
+ x2 = x0;
+ y0 = y1;
+ y1 = y2;
+ y2 = y0;
+ }
+ if (Math.abs(y1) <= functionValueAccuracy) {
+ setResult(x1, i);
+ return result;
+ }
+ if (Math.abs(oldDelta) <
+ Math.max(relativeAccuracy * Math.abs(x1), absoluteAccuracy)) {
+ setResult(x1, i);
+ return result;
+ }
+ double delta;
+ if (Math.abs(y1) > Math.abs(y0)) {
+ // Function value increased in last iteration. Force bisection.
+ delta = 0.5 * oldDelta;
+ } else {
+ delta = (x0 - x1) / (1 - y0 / y1);
+ if (delta / oldDelta > 1) {
+ // New approximation falls outside bracket.
+ // Fall back to bisection.
+ delta = 0.5 * oldDelta;
+ }
+ }
+ x0 = x1;
+ y0 = y1;
+ x1 = x1 + delta;
+ y1 = f.value(x1);
+ if ((y1 > 0) == (y2 > 0)) {
+ // New bracket is (x0,x1).
+ x2 = x0;
+ y2 = y0;
+ }
+ oldDelta = x2 - x1;
+ i++;
+ }
+ throw new MaxIterationsExceededException(maximalIterationCount);
+ }
+
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolver.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolver.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolver.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
+
+/**
+ * Interface for (univariate real) rootfinding algorithms.
+ * <p>
+ * Implementations will search for only one zero in the given interval.</p>
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (Sun, 07 Dec 2008) $
+ */
+public interface UnivariateRealSolver {
+
+ /**
+ * Set the upper limit for the number of iterations.
+ * <p>
+ * Usually a high iteration count indicates convergence problems. However,
+ * the "reasonable value" varies widely for different solvers. Users are
+ * advised to use the default value supplied by the solver.</p>
+ * <p>
+ * A <code>ConvergenceException</code> will be thrown if this number
+ * is exceeded.</p>
+ *
+ * @param count maximum number of iterations
+ */
+ void setMaximalIterationCount(int count);
+
+ /**
+ * Get the upper limit for the number of iterations.
+ *
+ * @return the actual upper limit
+ */
+ int getMaximalIterationCount();
+
+ /**
+ * Reset the upper limit for the number of iterations to the default.
+ * <p>
+ * The default value is supplied by the solver implementation.</p>
+ *
+ * @see #setMaximalIterationCount(int)
+ */
+ void resetMaximalIterationCount();
+
+ /**
+ * Set the absolute accuracy.
+ * <p>
+ * The default is usually choosen so that roots in the interval
+ * -10..-0.1 and +0.1..+10 can be found with a reasonable accuracy. If the
+ * expected absolute value of your roots is of much smaller magnitude, set
+ * this to a smaller value.</p>
+ * <p>
+ * Solvers are advised to do a plausibility check with the relative
+ * accuracy, but clients should not rely on this.</p>
+ *
+ * @param accuracy the accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ void setAbsoluteAccuracy(double accuracy);
+
+ /**
+ * Get the actual absolute accuracy.
+ *
+ * @return the accuracy
+ */
+ double getAbsoluteAccuracy();
+
+ /**
+ * Reset the absolute accuracy to the default.
+ * <p>
+ * The default value is provided by the solver implementation.</p>
+ */
+ void resetAbsoluteAccuracy();
+
+ /**
+ * Set the relative accuracy.
+ * <p>
+ * This is used to stop iterations if the absolute accuracy can't be
+ * achieved due to large values or short mantissa length.</p>
+ * <p>
+ * If this should be the primary criterion for convergence rather then a
+ * safety measure, set the absolute accuracy to a ridiculously small value,
+ * like 1E-1000.</p>
+ *
+ * @param accuracy the relative accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ void setRelativeAccuracy(double accuracy);
+
+ /**
+ * Get the actual relative accuracy.
+ * @return the accuracy
+ */
+ double getRelativeAccuracy();
+
+ /**
+ * Reset the relative accuracy to the default.
+ * The default value is provided by the solver implementation.
+ */
+ void resetRelativeAccuracy();
+
+ /**
+ * Set the function value accuracy.
+ * <p>
+ * This is used to determine when an evaluated function value or some other
+ * value which is used as divisor is zero.</p>
+ * <p>
+ * This is a safety guard and it shouldn't be necessary to change this in
+ * general.</p>
+ *
+ * @param accuracy the accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ void setFunctionValueAccuracy(double accuracy);
+
+ /**
+ * Get the actual function value accuracy.
+ * @return the accuracy
+ */
+ double getFunctionValueAccuracy();
+
+ /**
+ * Reset the actual function accuracy to the default.
+ * The default value is provided by the solver implementation.
+ */
+ void resetFunctionValueAccuracy();
+
+ /**
+ * Solve for a zero root in the given interval.
+ * A solver may require that the interval brackets a single zero root.
+ *
+ * @param min the lower bound for the interval.
+ * @param max the upper bound for the interval.
+ * @return a value where the function is zero
+ * @throws ConvergenceException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise.
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min > max or the endpoints do not
+ * satisfy the requirements specified by the solver
+ * @deprecated replaced by {@link #solve(UnivariateRealFunction, double, double)
+ * since 2.0
+ */
+ @Deprecated
+ double solve(double min, double max) throws ConvergenceException,
+ FunctionEvaluationException;
+
+ /**
+ * Solve for a zero root in the given interval.
+ * A solver may require that the interval brackets a single zero root.
+ *
+ * @param f the function to solve.
+ * @param min the lower bound for the interval.
+ * @param max the upper bound for the interval.
+ * @return a value where the function is zero
+ * @throws ConvergenceException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise.
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min > max or the endpoints do not
+ * satisfy the requirements specified by the solver
+ * @since 2.0
+ */
+ double solve(UnivariateRealFunction f, double min, double max)
+ throws ConvergenceException,
+ FunctionEvaluationException;
+
+ /**
+ * Solve for a zero in the given interval, start at startValue.
+ * A solver may require that the interval brackets a single zero root.
+ *
+ * @param min the lower bound for the interval.
+ * @param max the upper bound for the interval.
+ * @param startValue the start value to use
+ * @return a value where the function is zero
+ * @throws ConvergenceException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise.
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min > max or the arguments do not
+ * satisfy the requirements specified by the solver
+ * @deprecated replaced by {@link #solve(UnivariateRealFunction, double, double, double)
+ * since 2.0
+ */
+ @Deprecated
+ double solve(double min, double max, double startValue)
+ throws ConvergenceException, FunctionEvaluationException;
+
+ /**
+ * Solve for a zero in the given interval, start at startValue.
+ * A solver may require that the interval brackets a single zero root.
+ *
+ * @param f the function to solve.
+ * @param min the lower bound for the interval.
+ * @param max the upper bound for the interval.
+ * @param startValue the start value to use
+ * @return a value where the function is zero
+ * @throws ConvergenceException if the maximum iteration count is exceeded
+ * or the solver detects convergence problems otherwise.
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if min > max or the arguments do not
+ * satisfy the requirements specified by the solver
+ * @since 2.0
+ */
+ double solve(UnivariateRealFunction f, double min, double max, double startValue)
+ throws ConvergenceException, FunctionEvaluationException;
+
+ /**
+ * Get the result of the last run of the solver.
+ *
+ * @return the last result.
+ * @throws IllegalStateException if there is no result available, either
+ * because no result was yet computed or the last attempt failed.
+ */
+ double getResult();
+
+ /**
+ * Get the result of the last run of the solver.
+ *
+ * @return the value of the function at the last result.
+ * @throws IllegalStateException if there is no result available, either
+ * because no result was yet computed or the last attempt failed.
+ */
+ double getFunctionValue();
+
+ /**
+ * Get the number of iterations in the last run of the solver.
+ * <p>
+ * This is mainly meant for testing purposes. It may occasionally
+ * help track down performance problems: if the iteration count
+ * is notoriously high, check whether the function is evaluated
+ * properly, and whether another solver is more amenable to the
+ * problem.</p>
+ *
+ * @return the last iteration count.
+ * @throws IllegalStateException if there is no result available, either
+ * because no result was yet computed or the last attempt failed.
+ */
+ int getIterationCount();
+}
\ No newline at end of file
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactory.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactory.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactory.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+/**
+ * Abstract factory class used to create {@link UnivariateRealSolver} instances.
+ * <p>
+ * Solvers implementing the following algorithms are supported:
+ * <ul>
+ * <li>Bisection</li>
+ * <li>Brent's method</li>
+ * <li>Secant method</li>
+ * </ul>
+ * Concrete factories extending this class also specify a default solver, instances of which
+ * are returned by <code>newDefaultSolver()</code>.</p>
+ * <p>
+ * Common usage:<pre>
+ * SolverFactory factory = UnivariateRealSolverFactory.newInstance();</p>
+ *
+ * // create a Brent solver to use
+ * BrentSolver solver = factory.newBrentSolver();
+ * </pre>
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (dim., 07 déc. 2008) $
+ */
+public abstract class UnivariateRealSolverFactory {
+ /**
+ * Default constructor.
+ */
+ protected UnivariateRealSolverFactory() {
+ }
+
+ /**
+ * Create a new factory.
+ * @return a new factory.
+ */
+ public static UnivariateRealSolverFactory newInstance() {
+ return new UnivariateRealSolverFactoryImpl();
+ }
+
+ /**
+ * Create a new {@link UnivariateRealSolver}. The
+ * actual solver returned is determined by the underlying factory.
+ * @return the new solver.
+ */
+ public abstract UnivariateRealSolver newDefaultSolver();
+
+ /**
+ * Create a new {@link UnivariateRealSolver}. The
+ * solver is an implementation of the bisection method.
+ * @return the new solver.
+ */
+ public abstract UnivariateRealSolver newBisectionSolver();
+
+ /**
+ * Create a new {@link UnivariateRealSolver}. The
+ * solver is an implementation of the Brent method.
+ * @return the new solver.
+ */
+ public abstract UnivariateRealSolver newBrentSolver();
+
+ /**
+ * Create a new {@link UnivariateRealSolver}. The
+ * solver is an implementation of Newton's Method.
+ * @return the new solver.
+ */
+ public abstract UnivariateRealSolver newNewtonSolver();
+
+ /**
+ * Create a new {@link UnivariateRealSolver}. The
+ * solver is an implementation of the secant method.
+ * @return the new solver.
+ */
+ public abstract UnivariateRealSolver newSecantSolver();
+
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactoryImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactoryImpl.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactoryImpl.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverFactoryImpl.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+/**
+ * A concrete {@link UnivariateRealSolverFactory}. This is the default solver factory
+ * used by commons-math.
+ * <p>
+ * The default solver returned by this factory is a {@link BrentSolver}.</p>
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (dim., 07 déc. 2008) $
+ */
+public class UnivariateRealSolverFactoryImpl extends UnivariateRealSolverFactory {
+
+ /**
+ * Default constructor.
+ */
+ public UnivariateRealSolverFactoryImpl() {
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealSolver newDefaultSolver() {
+ return newBrentSolver();
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealSolver newBisectionSolver() {
+ return new BisectionSolver();
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealSolver newBrentSolver() {
+ return new BrentSolver();
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealSolver newNewtonSolver() {
+ return new NewtonSolver();
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealSolver newSecantSolver() {
+ return new SecantSolver();
+ }
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverImpl.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverImpl.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverImpl.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,403 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.analysis.solvers;
+
+import java.io.Serializable;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
+/**
+ * Provide a default implementation for several functions useful to generic
+ * solvers.
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (Sun, 07 Dec 2008) $
+ */
+public abstract class UnivariateRealSolverImpl implements UnivariateRealSolver,
+ Serializable {
+
+ /** Serializable version identifier */
+ private static final long serialVersionUID = 1112491292565386596L;
+
+ /** Maximum absolute error. */
+ protected double absoluteAccuracy;
+
+ /** Maximum relative error. */
+ protected double relativeAccuracy;
+
+ /** Maximum error of function. */
+ protected double functionValueAccuracy;
+
+ /** Maximum number of iterations. */
+ protected int maximalIterationCount;
+
+ /** Default maximum absolute error. */
+ protected double defaultAbsoluteAccuracy;
+
+ /** Default maximum relative error. */
+ protected double defaultRelativeAccuracy;
+
+ /** Default maximum error of function. */
+ protected double defaultFunctionValueAccuracy;
+
+ /** Default maximum number of iterations. */
+ protected int defaultMaximalIterationCount;
+
+ /** Indicates where a root has been computed. */
+ protected boolean resultComputed = false;
+
+ /** The last computed root. */
+ protected double result;
+
+ /** Value of the function at the last computed result. */
+ protected double functionValue;
+
+ // Mainly for test framework.
+ /** The last iteration count. */
+ protected int iterationCount;
+
+ /** The function to solve.
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method. */
+ @Deprecated
+ protected UnivariateRealFunction f;
+
+ /**
+ * Construct a solver with given iteration count and accuracy.
+ *
+ * @param f the function to solve.
+ * @param defaultAbsoluteAccuracy maximum absolute error
+ * @param defaultMaximalIterationCount maximum number of iterations
+ * @throws IllegalArgumentException if f is null or the
+ * defaultAbsoluteAccuracy is not valid
+ * @deprecated as of 2.0 the function to solve is passed as an argument
+ * to the {@link #solve(UnivariateRealFunction, double, double)} or
+ * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
+ * method.
+ */
+ @Deprecated
+ protected UnivariateRealSolverImpl(
+ UnivariateRealFunction f,
+ int defaultMaximalIterationCount,
+ double defaultAbsoluteAccuracy) {
+
+ if (f == null) {
+ throw MathRuntimeException.createIllegalArgumentException("function to solve cannot be null",
+ null);
+ }
+ this.f = f;
+
+ this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy;
+ this.defaultRelativeAccuracy = 1E-14;
+ this.defaultFunctionValueAccuracy = 1E-15;
+ this.absoluteAccuracy = defaultAbsoluteAccuracy;
+ this.relativeAccuracy = defaultRelativeAccuracy;
+ this.functionValueAccuracy = defaultFunctionValueAccuracy;
+ this.defaultMaximalIterationCount = defaultMaximalIterationCount;
+ this.maximalIterationCount = defaultMaximalIterationCount;
+ }
+
+ /**
+ * Construct a solver with given iteration count and accuracy.
+ *
+ * @param defaultAbsoluteAccuracy maximum absolute error
+ * @param defaultMaximalIterationCount maximum number of iterations
+ * @throws IllegalArgumentException if f is null or the
+ * defaultAbsoluteAccuracy is not valid
+ */
+ protected UnivariateRealSolverImpl(int defaultMaximalIterationCount,
+ double defaultAbsoluteAccuracy) {
+ this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy;
+ this.defaultRelativeAccuracy = 1E-14;
+ this.defaultFunctionValueAccuracy = 1E-15;
+ this.absoluteAccuracy = defaultAbsoluteAccuracy;
+ this.relativeAccuracy = defaultRelativeAccuracy;
+ this.functionValueAccuracy = defaultFunctionValueAccuracy;
+ this.defaultMaximalIterationCount = defaultMaximalIterationCount;
+ this.maximalIterationCount = defaultMaximalIterationCount;
+ }
+
+ /**
+ * Access the last computed root.
+ *
+ * @return the last computed root
+ * @throws IllegalStateException if no root has been computed
+ */
+ public double getResult() {
+ if (resultComputed) {
+ return result;
+ } else {
+ throw MathRuntimeException.createIllegalStateException("no result available", null);
+ }
+ }
+
+ /**
+ * Access the value of the function at the last computed result.
+ *
+ * @return the function value at the last result.
+ * @throws IllegalStateException if no value has been computed.
+ */
+ public double getFunctionValue() {
+ if (resultComputed) {
+ return functionValue;
+ } else {
+ throw MathRuntimeException.createIllegalStateException("no result available", null);
+ }
+ }
+
+ /**
+ * Access the last iteration count.
+ *
+ * @return the last iteration count
+ * @throws IllegalStateException if no root has been computed
+ *
+ */
+ public int getIterationCount() {
+ if (resultComputed) {
+ return iterationCount;
+ } else {
+ throw MathRuntimeException.createIllegalStateException("no result available", null);
+ }
+ }
+
+ /**
+ * Convenience function for implementations.
+ *
+ * @param result the result to set
+ * @param iterationCount the iteration count to set
+ */
+ protected final void setResult(double result, int iterationCount) {
+ this.result = result;
+ this.iterationCount = iterationCount;
+ this.resultComputed = true;
+ }
+
+ /**
+ * Convenience function for implementations.
+ *
+ * @param x the result to set
+ * @param fx the result to set
+ * @param iterationCount the iteration count to set
+ */
+ protected final void setResult(double x, double fx, int iterationCount) {
+ this.result = x;
+ this.functionValue = fx;
+ this.iterationCount = iterationCount;
+ this.resultComputed = true;
+ }
+
+ /**
+ * Convenience function for implementations.
+ */
+ protected final void clearResult() {
+ this.resultComputed = false;
+ }
+
+ /**
+ * Set the absolute accuracy.
+ *
+ * @param accuracy the accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ public void setAbsoluteAccuracy(double accuracy) {
+ absoluteAccuracy = accuracy;
+ }
+
+ /**
+ * Get the actual absolute accuracy.
+ *
+ * @return the accuracy
+ */
+ public double getAbsoluteAccuracy() {
+ return absoluteAccuracy;
+ }
+
+ /**
+ * Reset the absolute accuracy to the default.
+ */
+ public void resetAbsoluteAccuracy() {
+ absoluteAccuracy = defaultAbsoluteAccuracy;
+ }
+
+ /**
+ * Set the upper limit for the number of iterations.
+ *
+ * @param count maximum number of iterations
+ */
+ public void setMaximalIterationCount(int count) {
+ maximalIterationCount = count;
+ }
+
+ /**
+ * Get the upper limit for the number of iterations.
+ *
+ * @return the actual upper limit
+ */
+ public int getMaximalIterationCount() {
+ return maximalIterationCount;
+ }
+
+ /**
+ * Reset the upper limit for the number of iterations to the default.
+ */
+ public void resetMaximalIterationCount() {
+ maximalIterationCount = defaultMaximalIterationCount;
+ }
+
+ /**
+ * Set the relative accuracy.
+ *
+ * @param accuracy the relative accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ public void setRelativeAccuracy(double accuracy) {
+ relativeAccuracy = accuracy;
+ }
+
+ /**
+ * Get the actual relative accuracy.
+ * @return the accuracy
+ */
+ public double getRelativeAccuracy() {
+ return relativeAccuracy;
+ }
+
+ /**
+ * Reset the relative accuracy to the default.
+ */
+ public void resetRelativeAccuracy() {
+ relativeAccuracy = defaultRelativeAccuracy;
+ }
+
+ /**
+ * Set the function value accuracy.
+ *
+ * @param accuracy the accuracy.
+ * @throws IllegalArgumentException if the accuracy can't be achieved by
+ * the solver or is otherwise deemed unreasonable.
+ */
+ public void setFunctionValueAccuracy(double accuracy) {
+ functionValueAccuracy = accuracy;
+ }
+
+ /**
+ * Get the actual function value accuracy.
+ * @return the accuracy
+ */
+ public double getFunctionValueAccuracy() {
+ return functionValueAccuracy;
+ }
+
+ /**
+ * Reset the actual function accuracy to the default.
+ */
+ public void resetFunctionValueAccuracy() {
+ functionValueAccuracy = defaultFunctionValueAccuracy;
+ }
+
+
+ /**
+ * Returns true iff the function takes opposite signs at the endpoints.
+ *
+ * @param lower the lower endpoint
+ * @param upper the upper endpoint
+ * @param f the function
+ * @return true if f(lower) * f(upper) < 0
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function at the endpoints
+ */
+ protected boolean isBracketing(double lower, double upper,
+ UnivariateRealFunction f) throws FunctionEvaluationException {
+ double f1 = f.value(lower);
+ double f2 = f.value(upper);
+ return ((f1 > 0 && f2 < 0) || (f1 < 0 && f2 > 0));
+ }
+
+ /**
+ * Returns true if the arguments form a (strictly) increasing sequence
+ *
+ * @param start first number
+ * @param mid second number
+ * @param end third number
+ * @return true if the arguments form an increasing sequence
+ */
+ protected boolean isSequence(double start, double mid, double end) {
+ return (start < mid) && (mid < end);
+ }
+
+ /**
+ * Verifies that the endpoints specify an interval,
+ * throws IllegalArgumentException if not
+ *
+ * @param lower lower endpoint
+ * @param upper upper endpoint
+ * @throws IllegalArgumentException
+ */
+ protected void verifyInterval(double lower, double upper) {
+ if (lower >= upper) {
+ throw new IllegalArgumentException
+ ("Endpoints do not specify an interval: [" + lower +
+ "," + upper + "]");
+ }
+ }
+
+ /**
+ * Verifies that <code>lower < initial < upper</code>
+ * throws IllegalArgumentException if not
+ *
+ * @param lower lower endpoint
+ * @param initial initial value
+ * @param upper upper endpoint
+ * @throws IllegalArgumentException
+ */
+ protected void verifySequence(double lower, double initial, double upper) {
+ if (!isSequence(lower, initial, upper)) {
+ throw new IllegalArgumentException
+ ("Invalid interval, initial value parameters: lower=" +
+ lower + " initial=" + initial + " upper=" + upper);
+ }
+ }
+
+ /**
+ * Verifies that the endpoints specify an interval and the function takes
+ * opposite signs at the enpoints, throws IllegalArgumentException if not
+ *
+ * @param lower lower endpoint
+ * @param upper upper endpoint
+ * @param f function
+ * @throws IllegalArgumentException
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function at the endpoints
+ */
+ protected void verifyBracketing(double lower, double upper,
+ UnivariateRealFunction f) throws FunctionEvaluationException {
+
+ verifyInterval(lower, upper);
+ if (!isBracketing(lower, upper, f)) {
+ throw new IllegalArgumentException
+ ("Function values at endpoints do not have different signs." +
+ " Endpoints: [" + lower + "," + upper + "]" +
+ " Values: [" + f.value(lower) + "," + f.value(upper) + "]");
+ }
+ }
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java Sun Jan 18 05:07:22 2009
@@ -0,0 +1,239 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.solvers;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
+/**
+ * Utility routines for {@link UnivariateRealSolver} objects.
+ *
+ * @version $Revision: 724191 $ $Date: 2008-12-07 21:24:10 +0100 (dim., 07 déc. 2008) $
+ */
+public class UnivariateRealSolverUtils {
+ /**
+ * Default constructor.
+ */
+ private UnivariateRealSolverUtils() {
+ super();
+ }
+
+ /** Cached solver factory */
+ private static UnivariateRealSolverFactory factory = null;
+
+ /**
+ * Convenience method to find a zero of a univariate real function. A default
+ * solver is used.
+ *
+ * @param f the function.
+ * @param x0 the lower bound for the interval.
+ * @param x1 the upper bound for the interval.
+ * @return a value where the function is zero.
+ * @throws ConvergenceException if the iteration count was exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating
+ * the function
+ * @throws IllegalArgumentException if f is null or the endpoints do not
+ * specify a valid interval
+ */
+ public static double solve(UnivariateRealFunction f, double x0, double x1)
+ throws ConvergenceException, FunctionEvaluationException {
+ setup(f);
+ return factory.newDefaultSolver().solve(f, x0, x1);
+ }
+
+ /**
+ * Convenience method to find a zero of a univariate real function. A default
+ * solver is used.
+ *
+ * @param f the function
+ * @param x0 the lower bound for the interval
+ * @param x1 the upper bound for the interval
+ * @param absoluteAccuracy the accuracy to be used by the solver
+ * @return a value where the function is zero
+ * @throws ConvergenceException if the iteration count is exceeded
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if f is null, the endpoints do not
+ * specify a valid interval, or the absoluteAccuracy is not valid for the
+ * default solver
+ */
+ public static double solve(UnivariateRealFunction f, double x0, double x1,
+ double absoluteAccuracy) throws ConvergenceException,
+ FunctionEvaluationException {
+
+ setup(f);
+ UnivariateRealSolver solver = factory.newDefaultSolver();
+ solver.setAbsoluteAccuracy(absoluteAccuracy);
+ return solver.solve(f, x0, x1);
+ }
+
+ /**
+ * This method attempts to find two values a and b satisfying <ul>
+ * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
+ * <li> <code> f(a) * f(b) < 0 </code></li>
+ * </ul>
+ * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
+ * and <code>b</code> bracket a root of f.
+ * <p>
+ * The algorithm starts by setting
+ * <code>a := initial -1; b := initial +1,</code> examines the value of the
+ * function at <code>a</code> and <code>b</code> and keeps moving
+ * the endpoints out by one unit each time through a loop that terminates
+ * when one of the following happens: <ul>
+ * <li> <code> f(a) * f(b) < 0 </code> -- success!</li>
+ * <li> <code> a = lower </code> and <code> b = upper</code>
+ * -- ConvergenceException </li>
+ * <li> <code> Integer.MAX_VALUE</code> iterations elapse
+ * -- ConvergenceException </li>
+ * </ul></p>
+ * <p>
+ * <strong>Note: </strong> this method can take
+ * <code>Integer.MAX_VALUE</code> iterations to throw a
+ * <code>ConvergenceException.</code> Unless you are confident that there
+ * is a root between <code>lowerBound</code> and <code>upperBound</code>
+ * near <code>initial,</code> it is better to use
+ * {@link #bracket(UnivariateRealFunction, double, double, double, int)},
+ * explicitly specifying the maximum number of iterations.</p>
+ *
+ * @param function the function
+ * @param initial initial midpoint of interval being expanded to
+ * bracket a root
+ * @param lowerBound lower bound (a is never lower than this value)
+ * @param upperBound upper bound (b never is greater than this
+ * value)
+ * @return a two element array holding {a, b}
+ * @throws ConvergenceException if a root can not be bracketted
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if function is null, maximumIterations
+ * is not positive, or initial is not between lowerBound and upperBound
+ */
+ public static double[] bracket(UnivariateRealFunction function,
+ double initial, double lowerBound, double upperBound)
+ throws ConvergenceException, FunctionEvaluationException {
+ return bracket( function, initial, lowerBound, upperBound,
+ Integer.MAX_VALUE ) ;
+ }
+
+ /**
+ * This method attempts to find two values a and b satisfying <ul>
+ * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
+ * <li> <code> f(a) * f(b) < 0 </code> </li>
+ * </ul>
+ * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
+ * and <code>b</code> bracket a root of f.
+ * <p>
+ * The algorithm starts by setting
+ * <code>a := initial -1; b := initial +1,</code> examines the value of the
+ * function at <code>a</code> and <code>b</code> and keeps moving
+ * the endpoints out by one unit each time through a loop that terminates
+ * when one of the following happens: <ul>
+ * <li> <code> f(a) * f(b) < 0 </code> -- success!</li>
+ * <li> <code> a = lower </code> and <code> b = upper</code>
+ * -- ConvergenceException </li>
+ * <li> <code> maximumIterations</code> iterations elapse
+ * -- ConvergenceException </li></ul></p>
+ *
+ * @param function the function
+ * @param initial initial midpoint of interval being expanded to
+ * bracket a root
+ * @param lowerBound lower bound (a is never lower than this value)
+ * @param upperBound upper bound (b never is greater than this
+ * value)
+ * @param maximumIterations maximum number of iterations to perform
+ * @return a two element array holding {a, b}.
+ * @throws ConvergenceException if the algorithm fails to find a and b
+ * satisfying the desired conditions
+ * @throws FunctionEvaluationException if an error occurs evaluating the
+ * function
+ * @throws IllegalArgumentException if function is null, maximumIterations
+ * is not positive, or initial is not between lowerBound and upperBound
+ */
+ public static double[] bracket(UnivariateRealFunction function,
+ double initial, double lowerBound, double upperBound,
+ int maximumIterations) throws ConvergenceException,
+ FunctionEvaluationException {
+
+ if (function == null) {
+ throw new IllegalArgumentException ("function is null.");
+ }
+ if (maximumIterations <= 0) {
+ throw new IllegalArgumentException
+ ("bad value for maximumIterations: " + maximumIterations);
+ }
+ if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) {
+ throw new IllegalArgumentException
+ ("Invalid endpoint parameters: lowerBound=" + lowerBound +
+ " initial=" + initial + " upperBound=" + upperBound);
+ }
+ double a = initial;
+ double b = initial;
+ double fa;
+ double fb;
+ int numIterations = 0 ;
+
+ do {
+ a = Math.max(a - 1.0, lowerBound);
+ b = Math.min(b + 1.0, upperBound);
+ fa = function.value(a);
+
+ fb = function.value(b);
+ numIterations++ ;
+ } while ((fa * fb > 0.0) && (numIterations < maximumIterations) &&
+ ((a > lowerBound) || (b < upperBound)));
+
+ if (fa * fb >= 0.0 ) {
+ throw new ConvergenceException
+ ("Number of iterations={0}, maximum iterations={1}, initial={2}, lower bound={3}, upper bound={4}, final a value={5}, final b value={6}, f(a)={7}, f(b)={8}",
+ new Object[] { Integer.valueOf(numIterations), Integer.valueOf(maximumIterations),
+ Double.valueOf(initial), Double.valueOf(lowerBound), Double.valueOf(upperBound),
+ Double.valueOf(a), Double.valueOf(b), Double.valueOf(fa), Double.valueOf(fb) });
+ }
+
+ return new double[]{a, b};
+ }
+
+ /**
+ * Compute the midpoint of two values.
+ *
+ * @param a first value.
+ * @param b second value.
+ * @return the midpoint.
+ */
+ public static double midpoint(double a, double b) {
+ return (a + b) * .5;
+ }
+
+ /**
+ * Checks to see if f is null, throwing IllegalArgumentException if so.
+ * Also initializes factory if factory is null.
+ *
+ * @param f input function
+ * @throws IllegalArgumentException if f is null
+ */
+ private static void setup(UnivariateRealFunction f) {
+
+ if (f == null) {
+ throw new IllegalArgumentException("function can not be null.");
+ }
+
+ if (factory == null) {
+ factory = UnivariateRealSolverFactory.newInstance();
+ }
+ }
+}
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html?rev=735449&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html Sun Jan 18 05:07:22 2009
@@ -0,0 +1,22 @@
+<html>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+ <!-- $Revision$ $Date$ -->
+ <body>
+ Root finding algorithms, for univariate real functions.
+ </body>
+</html>
\ No newline at end of file
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/solvers/package.html
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java?rev=735449&r1=735448&r2=735449&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java Sun Jan 18 05:07:22 2009
@@ -22,7 +22,7 @@
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
-import org.apache.commons.math.analysis.UnivariateRealSolverUtils;
+import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils;
/**
* Base class for continuous distributions. Default implementations are
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/ode/events/EventState.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/ode/events/EventState.java?rev=735449&r1=735448&r2=735449&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/ode/events/EventState.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/ode/events/EventState.java Sun Jan 18 05:07:22 2009
@@ -21,8 +21,8 @@
import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.FunctionEvaluationException;
-import org.apache.commons.math.analysis.BrentSolver;
import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.solvers.BrentSolver;
import org.apache.commons.math.ode.DerivativeException;
import org.apache.commons.math.ode.sampling.StepInterpolator;
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=735449&r1=735448&r2=735449&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Jan 18 05:07:22 2009
@@ -39,6 +39,9 @@
</properties>
<body>
<release version="2.0" date="TBD" description="TBD">
+ <action dev="luc" type="update" >
+ The analysis package has been reorganized with several sub-packages.
+ </action>
<action dev="luc" type="fix" issue="MATH-238" due-to="Chritian Semrau">
Fixed an error in gcd computation for large values.
</action>