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.CalculusFieldElement;
26  import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
27  import org.hipparchus.analysis.FieldUnivariateFunction;
28  import org.hipparchus.dfp.Dfp;
29  import org.hipparchus.dfp.DfpField;
30  import org.hipparchus.dfp.DfpMath;
31  import org.hipparchus.exception.MathIllegalArgumentException;
32  import org.hipparchus.exception.MathRuntimeException;
33  import org.hipparchus.util.FastMath;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  import static org.junit.jupiter.api.Assertions.assertThrows;
39  import static org.junit.jupiter.api.Assertions.assertTrue;
40  
41  /**
42   * Test case for {@link FieldBracketingNthOrderBrentSolver bracketing n<sup>th</sup> order Brent} solver.
43   *
44   */
45  final class FieldBracketingNthOrderBrentSolverTest {
46  
47      @Test
48      void testInsufficientOrder3() {
49          assertThrows(MathIllegalArgumentException.class, () -> new FieldBracketingNthOrderBrentSolver<Dfp>(relativeAccuracy,
50                                                      absoluteAccuracy,
51                                                      functionValueAccuracy,
52                                                      1));
53      }
54  
55      @Test
56      void testConstructorOK() {
57          FieldBracketingNthOrderBrentSolver<Dfp> solver =
58                  new FieldBracketingNthOrderBrentSolver<Dfp>(relativeAccuracy, absoluteAccuracy,
59                                                              functionValueAccuracy, 2);
60          assertEquals(2, solver.getMaximalOrder());
61      }
62  
63      @Test
64      void testConvergenceOnFunctionAccuracy() {
65          FieldBracketingNthOrderBrentSolver<Dfp> solver =
66                  new FieldBracketingNthOrderBrentSolver<Dfp>(relativeAccuracy, absoluteAccuracy,
67                                                              field.newDfp(1.0e-20), 20);
68          FieldUnivariateFunction f = new FieldUnivariateFunction() {
69              public <T extends CalculusFieldElement<T>> T value(T x) {
70                  T one     = x.getField().getOne();
71                  T oneHalf = one.half();
72                  T xMo     = x.subtract(one);
73                  T xMh     = x.subtract(oneHalf);
74                  T xPh     = x.add(oneHalf);
75                  T xPo     = x.add(one);
76                  return xMo.multiply(xMh).multiply(x).multiply(xPh).multiply(xPo);
77              }
78          };
79  
80          Dfp result = solver.solve(20, f.toCalculusFieldUnivariateFunction(field), field.newDfp(0.2), field.newDfp(0.9),
81                                    field.newDfp(0.4), AllowedSolution.BELOW_SIDE);
82          assertTrue(f.value(result).abs().lessThan(solver.getFunctionValueAccuracy()));
83          assertTrue(f.value(result).negativeOrNull());
84          assertTrue(result.subtract(field.newDfp(0.5)).subtract(solver.getAbsoluteAccuracy()).positiveOrNull());
85          result = solver.solve(20, f.toCalculusFieldUnivariateFunction(field), field.newDfp(-0.9), field.newDfp(-0.2),
86                                field.newDfp(-0.4), AllowedSolution.ABOVE_SIDE);
87          assertTrue(f.value(result).abs().lessThan(solver.getFunctionValueAccuracy()));
88          assertTrue(f.value(result).positiveOrNull());
89          assertTrue(result.add(field.newDfp(0.5)).subtract(solver.getAbsoluteAccuracy()).negativeOrNull());
90      }
91  
92      @Test
93      void testToleranceLessThanUlp() {
94          // function that is never zero
95          Dfp zero = field.getZero();
96          Dfp one = field.getOne();
97          CalculusFieldUnivariateFunction<Dfp> f = (x) -> x.getReal() <= 2.1 ? one.negate(): one;
98          // tolerance less than 1 ulp(x)
99          FieldBracketingNthOrderBrentSolver<Dfp> solver =
100                 new FieldBracketingNthOrderBrentSolver<>(zero, field.newDfp(1e-55), zero, 5);
101 
102         // make sure it doesn't throw a maxIterations exception
103         Dfp result = solver.solve(200, f, zero, zero.add(5.0), AllowedSolution.LEFT_SIDE);
104         double difference = field.newDfp(2.1).subtract(result).abs().getReal();
105         assertTrue( difference < FastMath.ulp(2.1), "difference: " + difference);
106     }
107 
108     @Test
109     void testNeta() {
110 
111         // the following test functions come from Beny Neta's paper:
112         // "Several New Methods for solving Equations"
113         // intern J. Computer Math Vol 23 pp 265-282
114         // available here: http://www.math.nps.navy.mil/~bneta/SeveralNewMethods.PDF
115         for (AllowedSolution allowed : AllowedSolution.values()) {
116             check(x -> DfpMath.sin(x).subtract(x.half()), 200, -2.0, 2.0, allowed);
117 
118             check(x -> DfpMath.pow(x, 5).add(x).subtract(field.newDfp(10000)), 200, -5.0, 10.0, allowed);
119 
120             check(x -> x.sqrt().subtract(field.getOne().divide(x)).subtract(field.newDfp(3)), 200, 0.001, 10.0, allowed);
121 
122             check(x -> DfpMath.exp(x).add(x).subtract(field.newDfp(20)), 200, -5.0, 5.0, allowed);
123 
124             check(x -> DfpMath.log(x).add(x.sqrt()).subtract(field.newDfp(5)), 200, 0.001, 10.0, allowed);
125 
126             check(x -> x.subtract(field.getOne()).multiply(x.square()).subtract(field.getOne()), 200, -0.5, 1.5, allowed);
127         }
128 
129     }
130 
131     private void check(CalculusFieldUnivariateFunction<Dfp> f, int maxEval, double min, double max,
132                        AllowedSolution allowedSolution) {
133         FieldBracketingNthOrderBrentSolver<Dfp> solver =
134                 new FieldBracketingNthOrderBrentSolver<Dfp>(relativeAccuracy, absoluteAccuracy,
135                                                      functionValueAccuracy, 20);
136         Dfp xResult = solver.solve(maxEval, f, field.newDfp(min), field.newDfp(max),
137                                    allowedSolution);
138         Dfp yResult = f.value(xResult);
139         switch (allowedSolution) {
140         case ANY_SIDE :
141             assertTrue(yResult.abs().lessThan(functionValueAccuracy.twice()));
142             break;
143         case LEFT_SIDE : {
144             boolean increasing = f.value(xResult).add(absoluteAccuracy).greaterThan(yResult);
145             assertTrue(increasing ? yResult.negativeOrNull() : yResult.positiveOrNull());
146             break;
147         }
148         case RIGHT_SIDE : {
149             boolean increasing = f.value(xResult).add(absoluteAccuracy).greaterThan(yResult);
150             assertTrue(increasing ? yResult.positiveOrNull() : yResult.negativeOrNull());
151             break;
152         }
153         case BELOW_SIDE :
154             assertTrue(yResult.negativeOrNull());
155             break;
156         case ABOVE_SIDE :
157             assertTrue(yResult.positiveOrNull());
158             break;
159         default :
160             // this should never happen
161             throw new MathRuntimeException(null);
162         }
163     }
164 
165     @BeforeEach
166     void setUp() {
167         field                 = new DfpField(50);
168         absoluteAccuracy      = field.newDfp(1.0e-45);
169         relativeAccuracy      = field.newDfp(1.0e-45);
170         functionValueAccuracy = field.newDfp(1.0e-45);
171     }
172 
173     private DfpField field;
174     private Dfp      absoluteAccuracy;
175     private Dfp      relativeAccuracy;
176     private Dfp      functionValueAccuracy;
177 
178 }