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

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/math/analysis/MullerSolverTest.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.MathException;
20
 
import junit.framework.TestCase;
21
 
 
22
 
/**
23
 
 * Testcase for Muller solver.
24
 
 * <p>
25
 
 * Muller's method converges almost quadratically near roots, but it can
26
 
 * be very slow in regions far away from zeros. Test runs show that for
27
 
 * reasonably good initial values, for a default absolute accuracy of 1E-6,
28
 
 * it generally takes 5 to 10 iterations for the solver to converge.
29
 
 * <p>
30
 
 * Tests for the exponential function illustrate the situations where
31
 
 * Muller solver performs poorly.
32
 
 * 
33
 
 * @version $Revision$ $Date$ 
34
 
 */
35
 
public final class MullerSolverTest extends TestCase {
36
 
 
37
 
    /**
38
 
     * Test of solver for the sine function.
39
 
     */
40
 
    public void testSinFunction() throws MathException {
41
 
        UnivariateRealFunction f = new SinFunction();
42
 
        UnivariateRealSolver solver = new MullerSolver(f);
43
 
        double min, max, expected, result, tolerance;
44
 
 
45
 
        min = 3.0; max = 4.0; expected = Math.PI;
46
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
47
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
48
 
        result = solver.solve(min, max);
49
 
        assertEquals(expected, result, tolerance);
50
 
 
51
 
        min = -1.0; max = 1.5; expected = 0.0;
52
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
53
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
54
 
        result = solver.solve(min, max);
55
 
        assertEquals(expected, result, tolerance);
56
 
    }
57
 
 
58
 
    /**
59
 
     * Test of solver for the sine function using solve2().
60
 
     */
61
 
    public void testSinFunction2() throws MathException {
62
 
        UnivariateRealFunction f = new SinFunction();
63
 
        MullerSolver solver = new MullerSolver(f);
64
 
        double min, max, expected, result, tolerance;
65
 
 
66
 
        min = 3.0; max = 4.0; expected = Math.PI;
67
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
68
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
69
 
        result = solver.solve2(min, max);
70
 
        assertEquals(expected, result, tolerance);
71
 
 
72
 
        min = -1.0; max = 1.5; expected = 0.0;
73
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
74
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
75
 
        result = solver.solve2(min, max);
76
 
        assertEquals(expected, result, tolerance);
77
 
    }
78
 
 
79
 
    /**
80
 
     * Test of solver for the quintic function.
81
 
     */
82
 
    public void testQuinticFunction() throws MathException {
83
 
        UnivariateRealFunction f = new QuinticFunction();
84
 
        UnivariateRealSolver solver = new MullerSolver(f);
85
 
        double min, max, expected, result, tolerance;
86
 
 
87
 
        min = -0.4; max = 0.2; expected = 0.0;
88
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
89
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
90
 
        result = solver.solve(min, max);
91
 
        assertEquals(expected, result, tolerance);
92
 
 
93
 
        min = 0.75; max = 1.5; expected = 1.0;
94
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
95
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
96
 
        result = solver.solve(min, max);
97
 
        assertEquals(expected, result, tolerance);
98
 
 
99
 
        min = -0.9; max = -0.2; expected = -0.5;
100
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
101
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
102
 
        result = solver.solve(min, max);
103
 
        assertEquals(expected, result, tolerance);
104
 
    }
105
 
 
106
 
    /**
107
 
     * Test of solver for the quintic function using solve2().
108
 
     */
109
 
    public void testQuinticFunction2() throws MathException {
110
 
        UnivariateRealFunction f = new QuinticFunction();
111
 
        MullerSolver solver = new MullerSolver(f);
112
 
        double min, max, expected, result, tolerance;
113
 
 
114
 
        min = -0.4; max = 0.2; expected = 0.0;
115
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
116
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
117
 
        result = solver.solve2(min, max);
118
 
        assertEquals(expected, result, tolerance);
119
 
 
120
 
        min = 0.75; max = 1.5; expected = 1.0;
121
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
122
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
123
 
        result = solver.solve2(min, max);
124
 
        assertEquals(expected, result, tolerance);
125
 
 
126
 
        min = -0.9; max = -0.2; expected = -0.5;
127
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
128
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
129
 
        result = solver.solve2(min, max);
130
 
        assertEquals(expected, result, tolerance);
131
 
    }
132
 
 
133
 
    /**
134
 
     * Test of solver for the exponential function.
135
 
     * <p>
136
 
     * It takes 10 to 15 iterations for the last two tests to converge.
137
 
     * In fact, if not for the bisection alternative, the solver would
138
 
     * exceed the default maximal iteration of 100.
139
 
     */
140
 
    public void testExpm1Function() throws MathException {
141
 
        UnivariateRealFunction f = new Expm1Function();
142
 
        UnivariateRealSolver solver = new MullerSolver(f);
143
 
        double min, max, expected, result, tolerance;
144
 
 
145
 
        min = -1.0; max = 2.0; expected = 0.0;
146
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
147
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
148
 
        result = solver.solve(min, max);
149
 
        assertEquals(expected, result, tolerance);
150
 
 
151
 
        min = -20.0; max = 10.0; expected = 0.0;
152
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
153
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
154
 
        result = solver.solve(min, max);
155
 
        assertEquals(expected, result, tolerance);
156
 
 
157
 
        min = -50.0; max = 100.0; expected = 0.0;
158
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
159
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
160
 
        result = solver.solve(min, max);
161
 
        assertEquals(expected, result, tolerance);
162
 
    }
163
 
 
164
 
    /**
165
 
     * Test of solver for the exponential function using solve2().
166
 
     * <p>
167
 
     * It takes 25 to 50 iterations for the last two tests to converge.
168
 
     */
169
 
    public void testExpm1Function2() throws MathException {
170
 
        UnivariateRealFunction f = new Expm1Function();
171
 
        MullerSolver solver = new MullerSolver(f);
172
 
        double min, max, expected, result, tolerance;
173
 
 
174
 
        min = -1.0; max = 2.0; expected = 0.0;
175
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
176
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
177
 
        result = solver.solve2(min, max);
178
 
        assertEquals(expected, result, tolerance);
179
 
 
180
 
        min = -20.0; max = 10.0; expected = 0.0;
181
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
182
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
183
 
        result = solver.solve2(min, max);
184
 
        assertEquals(expected, result, tolerance);
185
 
 
186
 
        min = -50.0; max = 100.0; expected = 0.0;
187
 
        tolerance = Math.max(solver.getAbsoluteAccuracy(),
188
 
                    Math.abs(expected * solver.getRelativeAccuracy()));
189
 
        result = solver.solve2(min, max);
190
 
        assertEquals(expected, result, tolerance);
191
 
    }
192
 
 
193
 
    /**
194
 
     * Test of parameters for the solver.
195
 
     */
196
 
    public void testParameters() throws Exception {
197
 
        UnivariateRealFunction f = new SinFunction();
198
 
        UnivariateRealSolver solver = new MullerSolver(f);
199
 
 
200
 
        try {
201
 
            // bad interval
202
 
            solver.solve(1, -1);
203
 
            fail("Expecting IllegalArgumentException - bad interval");
204
 
        } catch (IllegalArgumentException ex) {
205
 
            // expected
206
 
        }
207
 
        try {
208
 
            // no bracketing
209
 
            solver.solve(2, 3);
210
 
            fail("Expecting IllegalArgumentException - no bracketing");
211
 
        } catch (IllegalArgumentException ex) {
212
 
            // expected
213
 
        }
214
 
    }
215
 
}