    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
    24  import junit.framework.TestCase;
    25
    26  /***
    27   * @version $Revision: 348791 $ $Date: 2005-11-24 12:50:36 -0700 (Thu, 24 Nov 2005) $
    28   */
    29  public final class NewtonSolverTest extends TestCase {
    30      /***
    31       *
    32       */
    33      public void testSinZero() throws MathException {
    34          DifferentiableUnivariateRealFunction f = new SinFunction();
    35          double result;
    36
    37          UnivariateRealSolver solver = new NewtonSolver(f);
    38          result = solver.solve(3, 4);
    39          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    40
    41          result = solver.solve(1, 4);
    42          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    43
    44          //TODO:  create abstract solver test class, move these there
    45          assertEquals(result, solver.getResult(), 0);
    46          assertTrue(solver.getIterationCount() > 0);
    47      }
    48
    49      /***
    50       *
    51       */
    52      public void testQuinticZero() throws MathException {
    53          DifferentiableUnivariateRealFunction f = new QuinticFunction();
    54          double result;
    55
    56          UnivariateRealSolver solver = new BisectionSolver(f);
    57          result = solver.solve(-0.2, 0.2);
    58          assertEquals(result, 0, solver.getAbsoluteAccuracy());
    59
    60          result = solver.solve(-0.1, 0.3);
    61          assertEquals(result, 0, solver.getAbsoluteAccuracy());
    62
    63          result = solver.solve(-0.3, 0.45);
    64          assertEquals(result, 0, solver.getAbsoluteAccuracy());
    65
    66          result = solver.solve(0.3, 0.7);
    67          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    68
    69          result = solver.solve(0.2, 0.6);
    70          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    71
    72          result = solver.solve(0.05, 0.95);
    73          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    74
    75          result = solver.solve(0.85, 1.25);
    76          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    77
    78          result = solver.solve(0.8, 1.2);
    79          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    80
    81          result = solver.solve(0.85, 1.75);
    82          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    83
    84          result = solver.solve(0.55, 1.45);
    85          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    86
    87          result = solver.solve(0.85, 5);
    88          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    89      }
    90
    91      /***
    92       * Test Serialization and Recovery
    93       */
    94      public void testSerialization() throws MathException {
    95          DifferentiableUnivariateRealFunction f = new QuinticFunction();
    96          double result;
    97
    98          NewtonSolver solver = new NewtonSolver(f);
    99          NewtonSolver solver2 = (NewtonSolver)TestUtils.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.getAbsoluteAccuracy());
    104
    105         result = solver.solve(-0.1, 0.3);
    106         assertEquals(result, 0, solver.getAbsoluteAccuracy());
    107         assertEquals(solver2.solve(-0.1, 0.3), result, solver2.getAbsoluteAccuracy());
    108
    109         result = solver.solve(-0.3, 0.45);
    110         assertEquals(result, 0, solver.getAbsoluteAccuracy());
    111         assertEquals(solver2.solve(-0.3, 0.45), result, solver2.getAbsoluteAccuracy());
    112
    113         result = solver.solve(0.3, 0.7);
    114         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    115         assertEquals(solver2.solve(0.3, 0.7), result, solver2.getAbsoluteAccuracy());
    116
    117         result = solver.solve(0.2, 0.6);
    118         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    119         assertEquals(solver2.solve(0.2, 0.6), result, solver2.getAbsoluteAccuracy());
    120
    121         result = solver.solve(0.05, 0.95);
    122         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
    123         assertEquals(solver2.solve(0.05, 0.95), result, solver2.getAbsoluteAccuracy());
    124
    125         result = solver.solve(0.85, 1.25);
    126         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    127         assertEquals(solver2.solve(0.85, 1.25), result, solver2.getAbsoluteAccuracy());
    128
    129         result = solver.solve(0.8, 1.2);
    130         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    131         assertEquals(solver2.solve(0.8, 1.2), result, solver2.getAbsoluteAccuracy());
    132
    133         result = solver.solve(0.85, 1.75);
    134         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    135         assertEquals(solver2.solve(0.85, 1.75), result, solver2.getAbsoluteAccuracy());
    136
    137         result = solver.solve(0.55, 1.45);
    138         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    139         assertEquals(solver2.solve(0.55, 1.45), result, solver2.getAbsoluteAccuracy());
    140
    141         result = solver.solve(0.85, 5);
    142         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
    143         assertEquals(solver2.solve(0.85, 5), result, solver2.getAbsoluteAccuracy());
    144
    145         /* Test Reset */
    146         double newValue = 1.0e-2;
    147         f = new QuinticFunction();
    148         solver = new NewtonSolver(f);
    149
    150         double oldValue = solver.getRelativeAccuracy();
    151         solver.setRelativeAccuracy(newValue);
    152         solver.resetRelativeAccuracy();
    153         assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
    154
    155         solver2 = (NewtonSolver)TestUtils.serializeAndRecover(solver);
    156
    157         assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
    158
    159         solver2.setRelativeAccuracy(newValue);
    160         solver2.resetRelativeAccuracy();
    161
    162         assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
    163
    164     }
    165 }