KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > analysis > BisectionSolverTest


1 /*
2  *
3  * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18 package org.apache.commons.math.analysis;
19
20 import org.apache.commons.math.MathException;
21 import org.apache.commons.math.TestUtils;
22
23 import junit.framework.TestCase;
24
25 /**
26  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
27  */

28 public final class BisectionSolverTest extends TestCase {
29     /**
30      *
31      */

32     public void testSinZero() throws MathException {
33         UnivariateRealFunction f = new SinFunction();
34         double result;
35         
36         UnivariateRealSolver solver = new BisectionSolver(f);
37         result = solver.solve(3, 4);
38         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
39
40         result = solver.solve(1, 4);
41         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
42     }
43
44     /**
45      *
46      */

47     public void testQuinticZero() throws MathException {
48         UnivariateRealFunction f = new QuinticFunction();
49         double result;
50
51         UnivariateRealSolver solver = new BisectionSolver(f);
52         result = solver.solve(-0.2, 0.2);
53         assertEquals(result, 0, solver.getAbsoluteAccuracy());
54
55         result = solver.solve(-0.1, 0.3);
56         assertEquals(result, 0, solver.getAbsoluteAccuracy());
57
58         result = solver.solve(-0.3, 0.45);
59         assertEquals(result, 0, solver.getAbsoluteAccuracy());
60
61         result = solver.solve(0.3, 0.7);
62         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
63
64         result = solver.solve(0.2, 0.6);
65         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
66
67         result = solver.solve(0.05, 0.95);
68         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
69
70         result = solver.solve(0.85, 1.25);
71         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
72
73         result = solver.solve(0.8, 1.2);
74         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
75
76         result = solver.solve(0.85, 1.75);
77         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
78
79         result = solver.solve(0.55, 1.45);
80         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
81
82         result = solver.solve(0.85, 5);
83         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
84         
85         assertEquals(result, solver.getResult(), 0);
86         assertTrue(solver.getIterationCount() > 0);
87     }
88     
89     /**
90      *
91      */

92     public void testSetFunctionValueAccuracy(){
93         double expected = 1.0e-2;
94         UnivariateRealFunction f = new QuinticFunction();
95         UnivariateRealSolver solver = new BisectionSolver(f);
96         solver.setFunctionValueAccuracy(expected);
97         assertEquals(expected, solver.getFunctionValueAccuracy(), 1.0e-2);
98     }
99     
100     /**
101      *
102      */

103     public void testResetFunctionValueAccuracy(){
104         double newValue = 1.0e-2;
105         UnivariateRealFunction f = new QuinticFunction();
106         UnivariateRealSolver solver = new BisectionSolver(f);
107         double oldValue = solver.getFunctionValueAccuracy();
108         solver.setFunctionValueAccuracy(newValue);
109         solver.resetFunctionValueAccuracy();
110         assertEquals(oldValue, solver.getFunctionValueAccuracy(), 1.0e-2);
111     }
112     
113     /**
114      *
115      */

116     public void testSetAbsoluteAccuracy(){
117         double expected = 1.0e-2;
118         UnivariateRealFunction f = new QuinticFunction();
119         UnivariateRealSolver solver = new BisectionSolver(f);
120         solver.setAbsoluteAccuracy(expected);
121         assertEquals(expected, solver.getAbsoluteAccuracy(), 1.0e-2);
122     }
123     
124     /**
125      *
126      */

127     public void testResetAbsoluteAccuracy(){
128         double newValue = 1.0e-2;
129         UnivariateRealFunction f = new QuinticFunction();
130         UnivariateRealSolver solver = new BisectionSolver(f);
131         double oldValue = solver.getAbsoluteAccuracy();
132         solver.setAbsoluteAccuracy(newValue);
133         solver.resetAbsoluteAccuracy();
134         assertEquals(oldValue, solver.getAbsoluteAccuracy(), 1.0e-2);
135     }
136     
137     /**
138      *
139      */

140     public void testSetMaximalIterationCount(){
141         int expected = 100;
142         
143         UnivariateRealFunction f = new QuinticFunction();
144         UnivariateRealSolver solver = new BisectionSolver(f);
145         solver.setMaximalIterationCount(expected);
146         assertEquals(expected, solver.getMaximalIterationCount());
147     }
148     
149     /**
150      *
151      */

152     public void testResetMaximalIterationCount(){
153         int newValue = 10000;
154         
155         UnivariateRealFunction f = new QuinticFunction();
156         UnivariateRealSolver solver = new BisectionSolver(f);
157         int oldValue = solver.getMaximalIterationCount();
158         solver.setMaximalIterationCount(newValue);
159         solver.resetMaximalIterationCount();
160         assertEquals(oldValue, solver.getMaximalIterationCount());
161     }
162     
163     /**
164      *
165      */

166     public void testSetRelativeAccuracy(){
167         double expected = 1.0e-2;
168         
169         UnivariateRealFunction f = new QuinticFunction();
170         UnivariateRealSolver solver = new BisectionSolver(f);
171         solver.setRelativeAccuracy(expected);
172         assertEquals(expected, solver.getRelativeAccuracy(), 1.0e-2);
173     }
174     
175     /**
176      *
177      */

178     public void testResetRelativeAccuracy(){
179         double newValue = 1.0e-2;
180         UnivariateRealFunction f = new QuinticFunction();
181         UnivariateRealSolver solver = new BisectionSolver(f);
182         double oldValue = solver.getRelativeAccuracy();
183         solver.setRelativeAccuracy(newValue);
184         solver.resetRelativeAccuracy();
185         assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
186     }
187     
188     /**
189      * Test Serialization and Recovery
190      */

191    public void testSerialization() throws MathException {
192        UnivariateRealFunction f = (UnivariateRealFunction)TestUtils.serializeAndRecover(new QuinticFunction());
193        double result;
194        
195        BisectionSolver solver = new BisectionSolver(f);
196        UnivariateRealSolver solver2 = (UnivariateRealSolver)TestUtils.serializeAndRecover(solver);
197        
198        result = solver.solve(-0.2, 0.2);
199        assertEquals(result, 0, solver.getAbsoluteAccuracy());
200        assertEquals(solver2.solve(-0.2, 0.2), result, solver2.getAbsoluteAccuracy());
201        
202        result = solver.solve(-0.1, 0.3);
203        assertEquals(result, 0, solver.getAbsoluteAccuracy());
204        assertEquals(solver2.solve(-0.1, 0.3), result, solver2.getAbsoluteAccuracy());
205        
206        result = solver.solve(-0.3, 0.45);
207        assertEquals(result, 0, solver.getAbsoluteAccuracy());
208        assertEquals(solver2.solve(-0.3, 0.45), result, solver2.getAbsoluteAccuracy());
209        
210        result = solver.solve(0.3, 0.7);
211        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
212        assertEquals(solver2.solve(0.3, 0.7), result, solver2.getAbsoluteAccuracy());
213        
214        result = solver.solve(0.2, 0.6);
215        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
216        assertEquals(solver2.solve(0.2, 0.6), result, solver2.getAbsoluteAccuracy());
217        
218        result = solver.solve(0.05, 0.95);
219        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
220        assertEquals(solver2.solve(0.05, 0.95), result, solver2.getAbsoluteAccuracy());
221        
222        result = solver.solve(0.85, 1.25);
223        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
224        assertEquals(solver2.solve(0.85, 1.25), result, solver2.getAbsoluteAccuracy());
225        
226        result = solver.solve(0.8, 1.2);
227        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
228        assertEquals(solver2.solve(0.8, 1.2), result, solver2.getAbsoluteAccuracy());
229        
230        result = solver.solve(0.85, 1.75);
231        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
232        assertEquals(solver2.solve(0.85, 1.75), result, solver2.getAbsoluteAccuracy());
233        
234        result = solver.solve(0.55, 1.45);
235        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
236        assertEquals(solver2.solve(0.55, 1.45), result, solver2.getAbsoluteAccuracy());
237        
238        result = solver.solve(0.85, 5);
239        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
240        assertEquals(solver2.solve(0.85, 5), result, solver2.getAbsoluteAccuracy());
241        
242        /* Test Reset */
243        double newValue = 1.0e-2;
244        f = (UnivariateRealFunction)TestUtils.serializeAndRecover(new QuinticFunction());
245        solver = new BisectionSolver(f);
246        
247        double oldValue = solver.getRelativeAccuracy();
248        solver.setRelativeAccuracy(newValue);
249        solver.resetRelativeAccuracy();
250        assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
251        
252        solver2 = (UnivariateRealSolver)TestUtils.serializeAndRecover(solver);
253        
254        assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
255        
256        solver2.setRelativeAccuracy(newValue);
257        solver2.resetRelativeAccuracy();
258        
259        assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
260        
261    }
262    
263 }
264
Popular Tags