View Javadoc
1   /*
2    * Copyright (C) 2023 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.numerical.integration;
17  
18  import com.irurueta.numerical.EvaluationException;
19  import com.irurueta.numerical.SingleDimensionFunctionEvaluatorListener;
20  import com.irurueta.numerical.interpolation.InterpolationException;
21  import com.irurueta.numerical.interpolation.PolynomialInterpolator;
22  
23  /**
24   * Computes function integration by using Romberg integration.
25   */
26  public class RombergTrapezoidalQuadratureIntegrator extends RombergIntegrator<TrapezoidalQuadrature> {
27  
28      /**
29       * Default accuracy.
30       */
31      public static final double EPS = 1e-10;
32  
33      /**
34       * Maximum number of allowed steps.
35       */
36      private static final int JMAX = 20;
37  
38      /**
39       * Maximum number of allowed steps + 1.
40       */
41      private static final int JMAXP = JMAX + 1;
42  
43      /**
44       * Minimum required number of steps.
45       */
46      private static final int K = 5;
47  
48      /**
49       * Successive trapezoidal approximations.
50       */
51      private final double[] s = new double[JMAX];
52  
53      /**
54       * Successive trapezoidal step sizes.
55       */
56      private final double[] h = new double[JMAXP];
57  
58      /**
59       * Polynomial interpolator.
60       */
61      private final PolynomialInterpolator interpolator = new PolynomialInterpolator(h, s, K, false);
62  
63      /**
64       * Constructor.
65       *
66       * @param a        Lower limit of integration.
67       * @param b        Upper limit of integration.
68       * @param listener listener to evaluate a single dimension function at required points.
69       * @param eps      required accuracy.
70       */
71      public RombergTrapezoidalQuadratureIntegrator(
72              final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener, final double eps) {
73          super(new TrapezoidalQuadrature(a, b, listener), eps);
74      }
75  
76      /**
77       * Constructor with default accuracy.
78       *
79       * @param a        Lower limit of integration.
80       * @param b        Upper limit of integration.
81       * @param listener listener to evaluate a single dimension function at required points.
82       */
83      public RombergTrapezoidalQuadratureIntegrator(
84              final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener) {
85          this(a, b, listener, EPS);
86      }
87  
88      /**
89       * Integrates function between lower and upper limits defined by provided quadrature.
90       * This implementation is suitable for closed intervals.
91       *
92       * @return result of integration.
93       * @throws IntegrationException if integration fails for numerical reasons.
94       */
95      @Override
96      public double integrate() throws IntegrationException {
97          try {
98              h[0] = 1.0;
99              for (var j = 1; j <= JMAX; j++) {
100                 s[j - 1] = q.next();
101                 if (j >= K) {
102                     final var ss = interpolator.rawinterp(j - K, 0.0);
103                     if (Math.abs(interpolator.getDy()) <= eps * Math.abs(ss)) {
104                         return ss;
105                     }
106                 }
107                 h[j] = 0.25 * h[j - 1];
108             }
109         } catch (final EvaluationException | InterpolationException e) {
110             throw new IntegrationException(e);
111         }
112 
113         // Too many steps
114         throw new IntegrationException();
115     }
116 
117     /**
118      * Gets type of quadrature.
119      *
120      * @return type of quadrature.
121      */
122     @Override
123     public QuadratureType getQuadratureType() {
124         return QuadratureType.TRAPEZOIDAL;
125     }
126 }