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    *      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,
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  package org.apache.commons.math4.legacy.analysis.solvers;
18  
19  import org.apache.commons.math4.legacy.analysis.QuinticFunction;
20  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
21  import org.apache.commons.math4.legacy.analysis.function.Sin;
22  import org.apache.commons.math4.core.jdkmath.JdkMath;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /**
27   */
28  public final class BisectionSolverTest {
29      @Test
30      public void testSinZero() {
31          UnivariateFunction f = new Sin();
32          double result;
33  
34          BisectionSolver solver = new BisectionSolver();
35          result = solver.solve(100, f, 3, 4);
36          Assert.assertEquals(result, JdkMath.PI, solver.getAbsoluteAccuracy());
37  
38          result = solver.solve(100, f, 1, 4);
39          Assert.assertEquals(result, JdkMath.PI, solver.getAbsoluteAccuracy());
40      }
41  
42      @Test
43      public void testQuinticZero() {
44          UnivariateFunction f = new QuinticFunction();
45          double result;
46  
47          BisectionSolver solver = new BisectionSolver();
48          result = solver.solve(100, f, -0.2, 0.2);
49          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
50  
51          result = solver.solve(100, f, -0.1, 0.3);
52          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
53  
54          result = solver.solve(100, f, -0.3, 0.45);
55          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
56  
57          result = solver.solve(100, f, 0.3, 0.7);
58          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
59  
60          result = solver.solve(100, f, 0.2, 0.6);
61          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
62  
63          result = solver.solve(100, f, 0.05, 0.95);
64          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
65  
66          result = solver.solve(100, f, 0.85, 1.25);
67          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
68  
69          result = solver.solve(100, f, 0.8, 1.2);
70          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
71  
72          result = solver.solve(100, f, 0.85, 1.75);
73          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
74  
75          result = solver.solve(100, f, 0.55, 1.45);
76          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
77  
78          result = solver.solve(100, f, 0.85, 5);
79          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
80  
81          Assert.assertTrue(solver.getEvaluations() > 0);
82      }
83  
84      @Test
85      public void testMath369() {
86          UnivariateFunction f = new Sin();
87          BisectionSolver solver = new BisectionSolver();
88          Assert.assertEquals(JdkMath.PI, solver.solve(100, f, 3.0, 3.2, 3.1), solver.getAbsoluteAccuracy());
89      }
90  }