~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/math/analysis/RiddersSolver.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
package org.apache.commons.math.analysis;
18
 
 
19
 
import org.apache.commons.math.FunctionEvaluationException;
20
 
import org.apache.commons.math.MaxIterationsExceededException;
21
 
import org.apache.commons.math.util.MathUtils;
22
 
 
23
 
/**
24
 
 * Implements the <a href="http://mathworld.wolfram.com/RiddersMethod.html">
25
 
 * Ridders' Method</a> for root finding of real univariate functions. For
26
 
 * reference, see C. Ridders, <i>A new algorithm for computing a single root
27
 
 * of a real continuous function </i>, IEEE Transactions on Circuits and
28
 
 * Systems, 26 (1979), 979 - 980.
29
 
 * <p>
30
 
 * The function should be continuous but not necessarily smooth.</p>
31
 
 *  
32
 
 * @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
33
 
 * @since 1.2
34
 
 */
35
 
public class RiddersSolver extends UnivariateRealSolverImpl {
36
 
 
37
 
    /** serializable version identifier */
38
 
    private static final long serialVersionUID = -4703139035737911735L;
39
 
 
40
 
    /**
41
 
     * Construct a solver for the given function.
42
 
     * 
43
 
     * @param f function to solve
44
 
     */
45
 
    public RiddersSolver(UnivariateRealFunction f) {
46
 
        super(f, 100, 1E-6);
47
 
    }
48
 
 
49
 
    /**
50
 
     * Find a root in the given interval with initial value.
51
 
     * <p>
52
 
     * Requires bracketing condition.</p>
53
 
     * 
54
 
     * @param min the lower bound for the interval
55
 
     * @param max the upper bound for the interval
56
 
     * @param initial the start value to use
57
 
     * @return the point at which the function value is zero
58
 
     * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
59
 
     * @throws FunctionEvaluationException if an error occurs evaluating the
60
 
     * function
61
 
     * @throws IllegalArgumentException if any parameters are invalid
62
 
     */
63
 
    public double solve(double min, double max, double initial) throws
64
 
        MaxIterationsExceededException, FunctionEvaluationException {
65
 
 
66
 
        // check for zeros before verifying bracketing
67
 
        if (f.value(min) == 0.0) { return min; }
68
 
        if (f.value(max) == 0.0) { return max; }
69
 
        if (f.value(initial) == 0.0) { return initial; }
70
 
 
71
 
        verifyBracketing(min, max, f);
72
 
        verifySequence(min, initial, max);
73
 
        if (isBracketing(min, initial, f)) {
74
 
            return solve(min, initial);
75
 
        } else {
76
 
            return solve(initial, max);
77
 
        }
78
 
    }
79
 
 
80
 
    /**
81
 
     * Find a root in the given interval.
82
 
     * <p>
83
 
     * Requires bracketing condition.</p>
84
 
     * 
85
 
     * @param min the lower bound for the interval
86
 
     * @param max the upper bound for the interval
87
 
     * @return the point at which the function value is zero
88
 
     * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
89
 
     * @throws FunctionEvaluationException if an error occurs evaluating the
90
 
     * function 
91
 
     * @throws IllegalArgumentException if any parameters are invalid
92
 
     */
93
 
    public double solve(double min, double max) throws MaxIterationsExceededException, 
94
 
        FunctionEvaluationException {
95
 
 
96
 
        // [x1, x2] is the bracketing interval in each iteration
97
 
        // x3 is the midpoint of [x1, x2]
98
 
        // x is the new root approximation and an endpoint of the new interval
99
 
        double x1, x2, x3, x, oldx, y1, y2, y3, y, delta, correction, tolerance;
100
 
 
101
 
        x1 = min; y1 = f.value(x1);
102
 
        x2 = max; y2 = f.value(x2);
103
 
 
104
 
        // check for zeros before verifying bracketing
105
 
        if (y1 == 0.0) { return min; }
106
 
        if (y2 == 0.0) { return max; }
107
 
        verifyBracketing(min, max, f);
108
 
 
109
 
        int i = 1;
110
 
        oldx = Double.POSITIVE_INFINITY;
111
 
        while (i <= maximalIterationCount) {
112
 
            // calculate the new root approximation
113
 
            x3 = 0.5 * (x1 + x2);
114
 
            y3 = f.value(x3);
115
 
            if (Math.abs(y3) <= functionValueAccuracy) {
116
 
                setResult(x3, i);
117
 
                return result;
118
 
            }
119
 
            delta = 1 - (y1 * y2) / (y3 * y3);  // delta > 1 due to bracketing
120
 
            correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
121
 
                         (x3 - x1) / Math.sqrt(delta);
122
 
            x = x3 - correction;                // correction != 0
123
 
            y = f.value(x);
124
 
 
125
 
            // check for convergence
126
 
            tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
127
 
            if (Math.abs(x - oldx) <= tolerance) {
128
 
                setResult(x, i);
129
 
                return result;
130
 
            }
131
 
            if (Math.abs(y) <= functionValueAccuracy) {
132
 
                setResult(x, i);
133
 
                return result;
134
 
            }
135
 
 
136
 
            // prepare the new interval for next iteration
137
 
            // Ridders' method guarantees x1 < x < x2
138
 
            if (correction > 0.0) {             // x1 < x < x3
139
 
                if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
140
 
                    x2 = x; y2 = y;
141
 
                } else {
142
 
                    x1 = x; x2 = x3;
143
 
                    y1 = y; y2 = y3;
144
 
                }
145
 
            } else {                            // x3 < x < x2
146
 
                if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
147
 
                    x1 = x; y1 = y;
148
 
                } else {
149
 
                    x1 = x3; x2 = x;
150
 
                    y1 = y3; y2 = y;
151
 
                }
152
 
            }
153
 
            oldx = x;
154
 
            i++;
155
 
        }
156
 
        throw new MaxIterationsExceededException(maximalIterationCount);
157
 
    }
158
 
}