1 16 package org.apache.commons.math.analysis; 17 18 import org.apache.commons.math.MathException; 19 20 import junit.framework.Test; 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 35 public final class BrentSolverTest extends TestCase { 36 37 public BrentSolverTest(String name) { 38 super(name); 39 } 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(BrentSolverTest.class); 43 suite.setName("UnivariateRealSolver Tests"); 44 return suite; 45 } 46 47 public void testSinZero() throws MathException { 48 UnivariateRealFunction f = new SinFunction(); 52 double result; 53 UnivariateRealSolver solver = new BrentSolver(f); 54 result = solver.solve(3, 4); 56 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 59 assertTrue(solver.getIterationCount() <= 5); 61 result = solver.solve(1, 4); 63 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 66 assertTrue(solver.getIterationCount() <= 6); 68 solver = new SecantSolver(f); 69 result = solver.solve(3, 4); 70 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 73 assertTrue(solver.getIterationCount() <= 5); 75 result = solver.solve(1, 4); 76 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 79 assertTrue(solver.getIterationCount() <= 6); 81 assertEquals(result, solver.getResult(), 0); 82 } 83 84 public void testQuinticZero() throws MathException { 85 UnivariateRealFunction f = new QuinticFunction(); 93 double result; 94 UnivariateRealSolver solver = new BrentSolver(f); 96 result = solver.solve(-0.2, 0.2); 99 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 102 assertTrue(solver.getIterationCount() <= 2); 103 result = solver.solve(-0.1, 0.3); 106 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 109 assertTrue(solver.getIterationCount() <= 6); 111 result = solver.solve(-0.3, 0.45); 113 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 116 assertTrue(solver.getIterationCount() <= 7); 118 result = solver.solve(0.3, 0.7); 120 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 123 assertTrue(solver.getIterationCount() <= 7); 125 result = solver.solve(0.2, 0.6); 127 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 130 assertTrue(solver.getIterationCount() <= 7); 132 result = solver.solve(0.05, 0.95); 134 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 137 assertTrue(solver.getIterationCount() <= 9); 139 result = solver.solve(0.85, 1.25); 142 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 145 assertTrue(solver.getIterationCount() <= 9); 147 result = solver.solve(0.8, 1.2); 149 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 152 assertTrue(solver.getIterationCount() <= 9); 154 result = solver.solve(0.85, 1.75); 156 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 159 assertTrue(solver.getIterationCount() <= 11); 161 result = solver.solve(0.55, 1.45); 163 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 166 assertTrue(solver.getIterationCount() <= 8); 168 result = solver.solve(0.85, 5); 170 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 173 assertTrue(solver.getIterationCount() <= 13); 175 solver = new SecantSolver(f); 177 result = solver.solve(-0.2, 0.2); 178 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 181 assertTrue(solver.getIterationCount() <= 2); 183 result = solver.solve(-0.1, 0.3); 184 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 187 assertTrue(solver.getIterationCount() <= 6); 189 result = solver.solve(-0.3, 0.45); 190 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 193 assertTrue(solver.getIterationCount() <= 7); 195 result = solver.solve(0.3, 0.7); 196 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 199 assertTrue(solver.getIterationCount() <= 8); 201 result = solver.solve(0.2, 0.6); 202 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 205 assertTrue(solver.getIterationCount() <= 7); 207 result = solver.solve(0.05, 0.95); 208 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 211 assertTrue(solver.getIterationCount() <= 9); 213 result = solver.solve(0.85, 1.25); 214 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 217 assertTrue(solver.getIterationCount() <= 11); 219 result = solver.solve(0.8, 1.2); 220 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 223 assertTrue(solver.getIterationCount() <= 9); 225 result = solver.solve(0.85, 1.75); 226 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 229 assertTrue(solver.getIterationCount() <= 15); 231 result = solver.solve(0.55, 1.45); 234 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 237 assertTrue(solver.getIterationCount() <= 8); 239 result = solver.solve(0.85, 5); 240 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 243 assertTrue(solver.getIterationCount() <= 15); 245 result = UnivariateRealSolverUtils.solve(f, -0.2, 0.2); 247 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 248 result = UnivariateRealSolverUtils.solve(f, -0.1, 0.3); 249 assertEquals(result, 0, 1E-8); 250 result = UnivariateRealSolverUtils.solve(f, -0.3, 0.45); 251 assertEquals(result, 0, 1E-6); 252 result = UnivariateRealSolverUtils.solve(f, 0.3, 0.7); 253 assertEquals(result, 0.5, 1E-6); 254 result = UnivariateRealSolverUtils.solve(f, 0.2, 0.6); 255 assertEquals(result, 0.5, 1E-6); 256 result = UnivariateRealSolverUtils.solve(f, 0.05, 0.95); 257 assertEquals(result, 0.5, 1E-6); 258 result = UnivariateRealSolverUtils.solve(f, 0.85, 1.25); 259 assertEquals(result, 1.0, 1E-6); 260 result = UnivariateRealSolverUtils.solve(f, 0.8, 1.2); 261 assertEquals(result, 1.0, 1E-6); 262 result = UnivariateRealSolverUtils.solve(f, 0.85, 1.75); 263 assertEquals(result, 1.0, 1E-6); 264 result = UnivariateRealSolverUtils.solve(f, 0.55, 1.45); 265 assertEquals(result, 1.0, 1E-6); 266 result = UnivariateRealSolverUtils.solve(f, 0.85, 5); 267 assertEquals(result, 1.0, 1E-6); 268 } 269 270 public void testBadEndpoints() throws Exception { 271 UnivariateRealFunction f = new SinFunction(); 272 UnivariateRealSolver solver = new BrentSolver(f); 273 try { solver.solve(1, -1); 275 fail("Expecting IllegalArgumentException - bad interval"); 276 } catch (IllegalArgumentException ex) { 277 } 279 try { solver.solve(1, 1.5); 281 fail("Expecting IllegalArgumentException - non-bracketing"); 282 } catch (IllegalArgumentException ex) { 283 } 285 } 286 } 287 | Popular Tags |