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  
23  package org.hipparchus.analysis.solvers;
24  
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.exception.MathIllegalStateException;
27  import org.hipparchus.util.FastMath;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  /**
32   * Test case for {@link RegulaFalsiSolver Regula Falsi} solver.
33   *
34   */
35  public final class RegulaFalsiSolverTest extends BaseSecantSolverAbstractTest {
36      /** {@inheritDoc} */
37      @Override
38      protected UnivariateSolver getSolver() {
39          UnivariateSolver solver = new RegulaFalsiSolver();
40          checktype(solver, BaseSecantSolver.Method.REGULA_FALSI);
41          return solver;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      protected int[] getQuinticEvalCounts() {
47          // While the Regula Falsi method guarantees convergence, convergence
48          // may be extremely slow. The last test case does not converge within
49          // even a million iterations. As such, it was disabled.
50          return new int[] {3, 7, 8, 19, 18, 11, 67, 55, 288, 151, -1};
51      }
52  
53      @Test(expected=MathIllegalStateException.class)
54      public void testIssue631() {
55          final UnivariateFunction f = new UnivariateFunction() {
56                  /** {@inheritDoc} */
57                  public double value(double x) {
58                      return FastMath.exp(x) - FastMath.pow(Math.PI, 3.0);
59                  }
60              };
61  
62          final UnivariateSolver solver = new RegulaFalsiSolver();
63          final double root = solver.solve(3624, f, 1, 10);
64          Assert.assertEquals(3.4341896575482003, root, 1e-15);
65      }
66  }