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