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.DerivativeStructure;
26  import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
27  import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
28  import org.hipparchus.analysis.function.Sin;
29  import org.hipparchus.util.FastMath;
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  
37  /**
38   */
39  final class NewtonRaphsonSolverTest {
40  
41      @Test
42      void testConstructor() {
43          // GIVEN
44          final double expectedRelativeAccuracy = 1.;
45          final double expectedAbsoluteAccuracy = 2.;
46          // WHEN
47          final NewtonRaphsonSolver solver = new NewtonRaphsonSolver(expectedRelativeAccuracy, expectedAbsoluteAccuracy, 3.);
48          // THEN
49          Assertions.assertEquals(expectedAbsoluteAccuracy, solver.getAbsoluteAccuracy());
50          Assertions.assertEquals(expectedRelativeAccuracy, solver.getRelativeAccuracy());
51      }
52  
53      @Test
54      @Deprecated
55      void testDeprecated() {
56          // GIVEN
57          final NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
58          final Sin sin = new Sin();
59          solver.setup(10, sin, -1., 1., 0.);
60          // WHEN
61          final DerivativeStructure derivativeStructure = solver.computeObjectiveValueAndDerivative(0.);
62          // THEN
63          final UnivariateDerivative1 derivative1 = solver.computeObjectiveValueAndDerivative1(0.);
64          Assertions.assertEquals(derivative1.getDerivative(1), derivativeStructure.getPartialDerivative(1));
65      }
66  
67      /**
68       *
69       */
70      @Test
71      void testSinZero() {
72          UnivariateDifferentiableFunction f = new Sin();
73          double result;
74  
75          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
76          result = solver.solve(100, f, 3, 4);
77          assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy());
78  
79          result = solver.solve(100, f, 1, 4);
80          assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy());
81  
82          assertTrue(solver.getEvaluations() > 0);
83      }
84  
85      /**
86       *
87       */
88      @Test
89      void testQuinticZero() {
90          final UnivariateDifferentiableFunction f = new QuinticFunction();
91          double result;
92  
93          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
94          result = solver.solve(100, f, -0.2, 0.2);
95          assertEquals(0, result, solver.getAbsoluteAccuracy());
96  
97          result = solver.solve(100, f, -0.1, 0.3);
98          assertEquals(0, result, solver.getAbsoluteAccuracy());
99  
100         result = solver.solve(100, f, -0.3, 0.45);
101         assertEquals(0, result, solver.getAbsoluteAccuracy());
102 
103         result = solver.solve(100, f, 0.3, 0.7);
104         assertEquals(0.5, result, solver.getAbsoluteAccuracy());
105 
106         result = solver.solve(100, f, 0.2, 0.6);
107         assertEquals(0.5, result, solver.getAbsoluteAccuracy());
108 
109         result = solver.solve(100, f, 0.05, 0.95);
110         assertEquals(0.5, result, solver.getAbsoluteAccuracy());
111 
112         result = solver.solve(100, f, 0.85, 1.25);
113         assertEquals(1.0, result, solver.getAbsoluteAccuracy());
114 
115         result = solver.solve(100, f, 0.8, 1.2);
116         assertEquals(1.0, result, solver.getAbsoluteAccuracy());
117 
118         result = solver.solve(100, f, 0.85, 1.75);
119         assertEquals(1.0, result, solver.getAbsoluteAccuracy());
120 
121         result = solver.solve(100, f, 0.55, 1.45);
122         assertEquals(1.0, result, solver.getAbsoluteAccuracy());
123 
124         result = solver.solve(100, f, 0.85, 5);
125         assertEquals(1.0, result, solver.getAbsoluteAccuracy());
126     }
127 }