View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.solvers;
23  
24  import org.hipparchus.analysis.QuinticFunction;
25  import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
26  import org.hipparchus.analysis.function.Sin;
27  import org.hipparchus.util.FastMath;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  
34  /**
35   */
36  final class NewtonRaphsonSolverTest {
37      /**
38       *
39       */
40      @Test
41      void testSinZero() {
42          UnivariateDifferentiableFunction f = new Sin();
43          double result;
44  
45          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
46          result = solver.solve(100, f, 3, 4);
47          assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy());
48  
49          result = solver.solve(100, f, 1, 4);
50          assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy());
51  
52          assertTrue(solver.getEvaluations() > 0);
53      }
54  
55      /**
56       *
57       */
58      @Test
59      void testQuinticZero() {
60          final UnivariateDifferentiableFunction f = new QuinticFunction();
61          double result;
62  
63          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
64          result = solver.solve(100, f, -0.2, 0.2);
65          assertEquals(0, result, solver.getAbsoluteAccuracy());
66  
67          result = solver.solve(100, f, -0.1, 0.3);
68          assertEquals(0, result, solver.getAbsoluteAccuracy());
69  
70          result = solver.solve(100, f, -0.3, 0.45);
71          assertEquals(0, result, solver.getAbsoluteAccuracy());
72  
73          result = solver.solve(100, f, 0.3, 0.7);
74          assertEquals(0.5, result, solver.getAbsoluteAccuracy());
75  
76          result = solver.solve(100, f, 0.2, 0.6);
77          assertEquals(0.5, result, solver.getAbsoluteAccuracy());
78  
79          result = solver.solve(100, f, 0.05, 0.95);
80          assertEquals(0.5, result, solver.getAbsoluteAccuracy());
81  
82          result = solver.solve(100, f, 0.85, 1.25);
83          assertEquals(1.0, result, solver.getAbsoluteAccuracy());
84  
85          result = solver.solve(100, f, 0.8, 1.2);
86          assertEquals(1.0, result, solver.getAbsoluteAccuracy());
87  
88          result = solver.solve(100, f, 0.85, 1.75);
89          assertEquals(1.0, result, solver.getAbsoluteAccuracy());
90  
91          result = solver.solve(100, f, 0.55, 1.45);
92          assertEquals(1.0, result, solver.getAbsoluteAccuracy());
93  
94          result = solver.solve(100, f, 0.85, 5);
95          assertEquals(1.0, result, solver.getAbsoluteAccuracy());
96      }
97  }