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

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/analysis/solvers/RiddersSolverTest.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.solvers;
 
18
 
 
19
import org.apache.commons.math.MathException;
 
20
import org.apache.commons.math.analysis.Expm1Function;
 
21
import org.apache.commons.math.analysis.QuinticFunction;
 
22
import org.apache.commons.math.analysis.SinFunction;
 
23
import org.apache.commons.math.analysis.UnivariateRealFunction;
 
24
 
 
25
import junit.framework.TestCase;
 
26
 
 
27
/**
 
28
 * Testcase for Ridders solver.
 
29
 * <p>
 
30
 * Ridders' method converges superlinearly, more specific, its rate of
 
31
 * convergence is sqrt(2). Test runs show that for a default absolute
 
32
 * accuracy of 1E-6, it generally takes less than 5 iterations for close
 
33
 * initial bracket and 5 to 10 iterations for distant initial bracket
 
34
 * to converge.
 
35
 * 
 
36
 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 
 
37
 */
 
38
public final class RiddersSolverTest extends TestCase {
 
39
 
 
40
    /**
 
41
     * Test the deprecated APIs.
 
42
     */
 
43
    @Deprecated
 
44
    public void testDeprecated() throws MathException {
 
45
        UnivariateRealFunction f = new SinFunction();
 
46
        UnivariateRealSolver solver = new RiddersSolver(f);
 
47
        double min, max, expected, result, tolerance;
 
48
 
 
49
        min = 3.0; max = 4.0; expected = Math.PI;
 
50
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
51
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
52
        result = solver.solve(min, max);
 
53
        assertEquals(expected, result, tolerance);
 
54
 
 
55
        min = -1.0; max = 1.5; expected = 0.0;
 
56
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
57
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
58
        result = solver.solve(min, max);
 
59
        assertEquals(expected, result, tolerance);
 
60
    }
 
61
 
 
62
    /**
 
63
     * Test of solver for the sine function.
 
64
     */
 
65
    public void testSinFunction() throws MathException {
 
66
        UnivariateRealFunction f = new SinFunction();
 
67
        UnivariateRealSolver solver = new RiddersSolver();
 
68
        double min, max, expected, result, tolerance;
 
69
 
 
70
        min = 3.0; max = 4.0; expected = Math.PI;
 
71
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
72
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
73
        result = solver.solve(f, min, max);
 
74
        assertEquals(expected, result, tolerance);
 
75
 
 
76
        min = -1.0; max = 1.5; expected = 0.0;
 
77
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
78
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
79
        result = solver.solve(f, min, max);
 
80
        assertEquals(expected, result, tolerance);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Test of solver for the quintic function.
 
85
     */
 
86
    public void testQuinticFunction() throws MathException {
 
87
        UnivariateRealFunction f = new QuinticFunction();
 
88
        UnivariateRealSolver solver = new RiddersSolver();
 
89
        double min, max, expected, result, tolerance;
 
90
 
 
91
        min = -0.4; max = 0.2; expected = 0.0;
 
92
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
93
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
94
        result = solver.solve(f, min, max);
 
95
        assertEquals(expected, result, tolerance);
 
96
 
 
97
        min = 0.75; max = 1.5; expected = 1.0;
 
98
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
99
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
100
        result = solver.solve(f, min, max);
 
101
        assertEquals(expected, result, tolerance);
 
102
 
 
103
        min = -0.9; max = -0.2; expected = -0.5;
 
104
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
105
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
106
        result = solver.solve(f, min, max);
 
107
        assertEquals(expected, result, tolerance);
 
108
    }
 
109
 
 
110
    /**
 
111
     * Test of solver for the exponential function.
 
112
     */
 
113
    public void testExpm1Function() throws MathException {
 
114
        UnivariateRealFunction f = new Expm1Function();
 
115
        UnivariateRealSolver solver = new RiddersSolver();
 
116
        double min, max, expected, result, tolerance;
 
117
 
 
118
        min = -1.0; max = 2.0; expected = 0.0;
 
119
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
120
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
121
        result = solver.solve(f, min, max);
 
122
        assertEquals(expected, result, tolerance);
 
123
 
 
124
        min = -20.0; max = 10.0; expected = 0.0;
 
125
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
126
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
127
        result = solver.solve(f, min, max);
 
128
        assertEquals(expected, result, tolerance);
 
129
 
 
130
        min = -50.0; max = 100.0; expected = 0.0;
 
131
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
 
132
                    Math.abs(expected * solver.getRelativeAccuracy()));
 
133
        result = solver.solve(f, min, max);
 
134
        assertEquals(expected, result, tolerance);
 
135
    }
 
136
 
 
137
    /**
 
138
     * Test of parameters for the solver.
 
139
     */
 
140
    public void testParameters() throws Exception {
 
141
        UnivariateRealFunction f = new SinFunction();
 
142
        UnivariateRealSolver solver = new RiddersSolver();
 
143
 
 
144
        try {
 
145
            // bad interval
 
146
            solver.solve(f, 1, -1);
 
147
            fail("Expecting IllegalArgumentException - bad interval");
 
148
        } catch (IllegalArgumentException ex) {
 
149
            // expected
 
150
        }
 
151
        try {
 
152
            // no bracketing
 
153
            solver.solve(f, 2, 3);
 
154
            fail("Expecting IllegalArgumentException - no bracketing");
 
155
        } catch (IllegalArgumentException ex) {
 
156
            // expected
 
157
        }
 
158
    }
 
159
}