1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  package org.hipparchus.analysis.solvers;
23  
24  import org.hipparchus.analysis.QuinticFunction;
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.function.Expm1;
27  import org.hipparchus.analysis.function.Sin;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  import org.hipparchus.util.FastMath;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.fail;
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  final class RiddersSolverTest {
46      
47  
48  
49      @Test
50      void testSinFunction() {
51          UnivariateFunction f = new Sin();
52          UnivariateSolver solver = new RiddersSolver();
53          double min, max, expected, result, tolerance;
54  
55          min = 3.0; max = 4.0; expected = FastMath.PI;
56          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
57                      FastMath.abs(expected * solver.getRelativeAccuracy()));
58          result = solver.solve(100, f, min, max);
59          assertEquals(expected, result, tolerance);
60  
61          min = -1.0; max = 1.5; expected = 0.0;
62          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
63                      FastMath.abs(expected * solver.getRelativeAccuracy()));
64          result = solver.solve(100, f, min, max);
65          assertEquals(expected, result, tolerance);
66      }
67  
68      
69  
70  
71      @Test
72      void testQuinticFunction() {
73          UnivariateFunction f = new QuinticFunction();
74          UnivariateSolver solver = new RiddersSolver();
75          double min, max, expected, result, tolerance;
76  
77          min = -0.4; max = 0.2; expected = 0.0;
78          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
79                      FastMath.abs(expected * solver.getRelativeAccuracy()));
80          result = solver.solve(100, f, min, max);
81          assertEquals(expected, result, tolerance);
82  
83          min = 0.75; max = 1.5; expected = 1.0;
84          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
85                      FastMath.abs(expected * solver.getRelativeAccuracy()));
86          result = solver.solve(100, f, min, max);
87          assertEquals(expected, result, tolerance);
88  
89          min = -0.9; max = -0.2; expected = -0.5;
90          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
91                      FastMath.abs(expected * solver.getRelativeAccuracy()));
92          result = solver.solve(100, f, min, max);
93          assertEquals(expected, result, tolerance);
94      }
95  
96      
97  
98  
99      @Test
100     void testExpm1Function() {
101         UnivariateFunction f = new Expm1();
102         UnivariateSolver solver = new RiddersSolver();
103         double min, max, expected, result, tolerance;
104 
105         min = -1.0; max = 2.0; expected = 0.0;
106         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
107                     FastMath.abs(expected * solver.getRelativeAccuracy()));
108         result = solver.solve(100, f, min, max);
109         assertEquals(expected, result, tolerance);
110 
111         min = -20.0; max = 10.0; expected = 0.0;
112         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
113                     FastMath.abs(expected * solver.getRelativeAccuracy()));
114         result = solver.solve(100, f, min, max);
115         assertEquals(expected, result, tolerance);
116 
117         min = -50.0; max = 100.0; expected = 0.0;
118         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
119                     FastMath.abs(expected * solver.getRelativeAccuracy()));
120         result = solver.solve(100, f, min, max);
121         assertEquals(expected, result, tolerance);
122     }
123 
124     
125 
126 
127     @Test
128     void testParameters() {
129         UnivariateFunction f = new Sin();
130         UnivariateSolver solver = new RiddersSolver();
131 
132         try {
133             
134             solver.solve(100, f, 1, -1);
135             fail("Expecting MathIllegalArgumentException - bad interval");
136         } catch (MathIllegalArgumentException ex) {
137             
138         }
139         try {
140             
141             solver.solve(100, f, 2, 3);
142             fail("Expecting MathIllegalArgumentException - no bracketing");
143         } catch (MathIllegalArgumentException ex) {
144             
145         }
146     }
147 }