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  package org.hipparchus.analysis.integration.gauss;
23  
24  import org.hipparchus.analysis.function.Power;
25  import org.hipparchus.util.FastMath;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  
29  
30  /**
31   * Base class for standard testing of Gaussian quadrature rules,
32   * which are exact for polynomials up to a certain degree. In this test, each
33   * monomial in turn is tested against the specified quadrature rule.
34   *
35   */
36  public abstract class GaussianQuadratureAbstractTest {
37  
38      /**
39       * Returns the expected value of the integral of the specified monomial.
40       * The integration is carried out on the natural interval of the quadrature
41       * rule under test.
42       *
43       * @param n Degree of the monomial.
44       * @return the expected value of the integral of x<sup>n</sup>.
45       */
46      public abstract double getExpectedValue(final int n);
47  
48      /**
49       * Checks that the value of the integral of each monomial
50       *   <code>x<sup>0</sup>, ... , x<sup>p</sup></code>
51       * returned by the quadrature rule under test conforms with the expected
52       * value.
53       * Here {@code p} denotes the degree of the highest polynomial for which
54       * exactness is to be expected.
55       */
56      public void testAllMonomials(GaussIntegrator integrator,
57                                   int maxDegree,
58                                   double eps,
59                                   double numUlps) {
60          for (int n = 0; n <= maxDegree; n++) {
61              final double expected = getExpectedValue(n);
62  
63              final Power monomial = new Power(n);
64              final double actual = integrator.integrate(monomial);
65  
66              // System.out.println(n + "/" + maxDegree + " " + integrator.getNumberOfPoints()
67              //                    + " " + expected + " " + actual + " " + Math.ulp(expected));
68              if (expected == 0) {
69                  assertEquals(expected, actual, eps, "while integrating monomial x**" + n +
70                                      " with a " +
71                                      integrator.getNumberOfPoints() + "-point quadrature rule");
72              } else {
73                  double err = FastMath.abs(actual - expected) / Math.ulp(expected);
74                  assertEquals(expected, actual, Math.ulp(expected) * numUlps, "while integrating monomial x**" + n + " with a " +
75                                      + integrator.getNumberOfPoints() + "-point quadrature rule, " +
76                                      " error was " + err + " ulps");
77              }
78          }
79      }
80  }