001: /*
002: *
003: * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy
007: * of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018: package org.apache.commons.math.analysis;
019:
020: import org.apache.commons.math.MathException;
021: import org.apache.commons.math.TestUtils;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * @version $Revision: 348791 $ $Date: 2005-11-24 12:50:36 -0700 (Thu, 24 Nov 2005) $
027: */
028: public final class NewtonSolverTest extends TestCase {
029: /**
030: *
031: */
032: public void testSinZero() throws MathException {
033: DifferentiableUnivariateRealFunction f = new SinFunction();
034: double result;
035:
036: UnivariateRealSolver solver = new NewtonSolver(f);
037: result = solver.solve(3, 4);
038: assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
039:
040: result = solver.solve(1, 4);
041: assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
042:
043: //TODO: create abstract solver test class, move these there
044: assertEquals(result, solver.getResult(), 0);
045: assertTrue(solver.getIterationCount() > 0);
046: }
047:
048: /**
049: *
050: */
051: public void testQuinticZero() throws MathException {
052: DifferentiableUnivariateRealFunction f = new QuinticFunction();
053: double result;
054:
055: UnivariateRealSolver solver = new BisectionSolver(f);
056: result = solver.solve(-0.2, 0.2);
057: assertEquals(result, 0, solver.getAbsoluteAccuracy());
058:
059: result = solver.solve(-0.1, 0.3);
060: assertEquals(result, 0, solver.getAbsoluteAccuracy());
061:
062: result = solver.solve(-0.3, 0.45);
063: assertEquals(result, 0, solver.getAbsoluteAccuracy());
064:
065: result = solver.solve(0.3, 0.7);
066: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
067:
068: result = solver.solve(0.2, 0.6);
069: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
070:
071: result = solver.solve(0.05, 0.95);
072: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
073:
074: result = solver.solve(0.85, 1.25);
075: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
076:
077: result = solver.solve(0.8, 1.2);
078: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
079:
080: result = solver.solve(0.85, 1.75);
081: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
082:
083: result = solver.solve(0.55, 1.45);
084: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
085:
086: result = solver.solve(0.85, 5);
087: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
088: }
089:
090: /**
091: * Test Serialization and Recovery
092: */
093: public void testSerialization() throws MathException {
094: DifferentiableUnivariateRealFunction f = new QuinticFunction();
095: double result;
096:
097: NewtonSolver solver = new NewtonSolver(f);
098: NewtonSolver solver2 = (NewtonSolver) TestUtils
099: .serializeAndRecover(solver);
100:
101: result = solver.solve(-0.2, 0.2);
102: assertEquals(result, 0, solver.getAbsoluteAccuracy());
103: assertEquals(solver2.solve(-0.2, 0.2), result, solver2
104: .getAbsoluteAccuracy());
105:
106: result = solver.solve(-0.1, 0.3);
107: assertEquals(result, 0, solver.getAbsoluteAccuracy());
108: assertEquals(solver2.solve(-0.1, 0.3), result, solver2
109: .getAbsoluteAccuracy());
110:
111: result = solver.solve(-0.3, 0.45);
112: assertEquals(result, 0, solver.getAbsoluteAccuracy());
113: assertEquals(solver2.solve(-0.3, 0.45), result, solver2
114: .getAbsoluteAccuracy());
115:
116: result = solver.solve(0.3, 0.7);
117: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
118: assertEquals(solver2.solve(0.3, 0.7), result, solver2
119: .getAbsoluteAccuracy());
120:
121: result = solver.solve(0.2, 0.6);
122: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
123: assertEquals(solver2.solve(0.2, 0.6), result, solver2
124: .getAbsoluteAccuracy());
125:
126: result = solver.solve(0.05, 0.95);
127: assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
128: assertEquals(solver2.solve(0.05, 0.95), result, solver2
129: .getAbsoluteAccuracy());
130:
131: result = solver.solve(0.85, 1.25);
132: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
133: assertEquals(solver2.solve(0.85, 1.25), result, solver2
134: .getAbsoluteAccuracy());
135:
136: result = solver.solve(0.8, 1.2);
137: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
138: assertEquals(solver2.solve(0.8, 1.2), result, solver2
139: .getAbsoluteAccuracy());
140:
141: result = solver.solve(0.85, 1.75);
142: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
143: assertEquals(solver2.solve(0.85, 1.75), result, solver2
144: .getAbsoluteAccuracy());
145:
146: result = solver.solve(0.55, 1.45);
147: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
148: assertEquals(solver2.solve(0.55, 1.45), result, solver2
149: .getAbsoluteAccuracy());
150:
151: result = solver.solve(0.85, 5);
152: assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
153: assertEquals(solver2.solve(0.85, 5), result, solver2
154: .getAbsoluteAccuracy());
155:
156: /* Test Reset */
157: double newValue = 1.0e-2;
158: f = new QuinticFunction();
159: solver = new NewtonSolver(f);
160:
161: double oldValue = solver.getRelativeAccuracy();
162: solver.setRelativeAccuracy(newValue);
163: solver.resetRelativeAccuracy();
164: assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
165:
166: solver2 = (NewtonSolver) TestUtils.serializeAndRecover(solver);
167:
168: assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
169:
170: solver2.setRelativeAccuracy(newValue);
171: solver2.resetRelativeAccuracy();
172:
173: assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
174:
175: }
176: }
|