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  /**
21   * This is an exact replacement for MidPointQuadrature, except that it allows for an inverse
22   * square-root singularity in the integrand at the upper limit b.
23   */
24  public class UpperSquareRootMidPointQuadrature extends MidPointQuadrature {
25  
26      /**
27       * Original upper bound of integration.
28       */
29      private final double borig;
30  
31      /**
32       * Constructor.
33       *
34       * @param a        Lower limit of integration.
35       * @param b        Upper limit of integration.
36       * @param listener listener to evaluate a single dimension function at required points.
37       */
38      public UpperSquareRootMidPointQuadrature(
39              final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener) {
40          super(0.0, Math.sqrt(b - a), listener);
41          borig = b;
42      }
43  
44      /**
45       * Gets type of quadrature.
46       *
47       * @return type of quadrature.
48       */
49      @Override
50      public QuadratureType getType() {
51          return QuadratureType.UPPER_SQUARE_ROOT_MID_POINT;
52      }
53  
54      /**
55       * Evaluates function at 2*x*f(a0+x^2).
56       *
57       * @param x point where function is evaluated.
58       * @return result of evaluation.
59       * @throws EvaluationException if evaluation fails.
60       */
61      @Override
62      protected double func(final double x) throws EvaluationException {
63          return 2.0 * x * listener.evaluate(borig - x * x);
64      }
65  }